1aca1545dSVictor Chong /* 2aca1545dSVictor Chong * Copyright (c) 2016, Linaro Limited 3aca1545dSVictor Chong * All rights reserved. 4aca1545dSVictor Chong * 5aca1545dSVictor Chong * Redistribution and use in source and binary forms, with or without 6aca1545dSVictor Chong * modification, are permitted provided that the following conditions are met: 7aca1545dSVictor Chong * 8aca1545dSVictor Chong * 1. Redistributions of source code must retain the above copyright notice, 9aca1545dSVictor Chong * this list of conditions and the following disclaimer. 10aca1545dSVictor Chong * 11aca1545dSVictor Chong * 2. Redistributions in binary form must reproduce the above copyright notice, 12aca1545dSVictor Chong * this list of conditions and the following disclaimer in the documentation 13aca1545dSVictor Chong * and/or other materials provided with the distribution. 14aca1545dSVictor Chong * 15aca1545dSVictor Chong * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16aca1545dSVictor Chong * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17aca1545dSVictor Chong * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18aca1545dSVictor Chong * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19aca1545dSVictor Chong * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20aca1545dSVictor Chong * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21aca1545dSVictor Chong * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22aca1545dSVictor Chong * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23aca1545dSVictor Chong * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24aca1545dSVictor Chong * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25aca1545dSVictor Chong * POSSIBILITY OF SUCH DAMAGE. 26aca1545dSVictor Chong * 27aca1545dSVictor Chong */ 28aca1545dSVictor Chong 29aca1545dSVictor Chong #include <assert.h> 30aca1545dSVictor Chong #include <drivers/pl022_spi.h> 31aca1545dSVictor Chong #include <initcall.h> 32aca1545dSVictor Chong #include <io.h> 33aca1545dSVictor Chong #include <kernel/panic.h> 34aca1545dSVictor Chong #include <kernel/tee_time.h> 35aca1545dSVictor Chong #include <platform_config.h> 36aca1545dSVictor Chong #include <trace.h> 37aca1545dSVictor Chong #include <util.h> 38aca1545dSVictor Chong 39aca1545dSVictor Chong /* SPI register offsets */ 40aca1545dSVictor Chong #define SSPCR0 0x000 41aca1545dSVictor Chong #define SSPCR1 0x004 42aca1545dSVictor Chong #define SSPDR 0x008 43aca1545dSVictor Chong #define SSPSR 0x00C 44aca1545dSVictor Chong #define SSPCPSR 0x010 45aca1545dSVictor Chong #define SSPIMSC 0x014 46aca1545dSVictor Chong #define SSPRIS 0x018 47aca1545dSVictor Chong #define SSPMIS 0x01C 48aca1545dSVictor Chong #define SSPICR 0x020 49aca1545dSVictor Chong #define SSPDMACR 0x024 50aca1545dSVictor Chong 51aca1545dSVictor Chong #ifdef PLATFORM_hikey 52aca1545dSVictor Chong /* HiKey extensions */ 53aca1545dSVictor Chong #define SSPTXFIFOCR 0x028 54aca1545dSVictor Chong #define SSPRXFIFOCR 0x02C 55aca1545dSVictor Chong #define SSPB2BTRANS 0x030 56aca1545dSVictor Chong #endif 57aca1545dSVictor Chong 58aca1545dSVictor Chong /* test registers */ 59aca1545dSVictor Chong #define SSPTCR 0x080 60aca1545dSVictor Chong #define SSPITIP 0x084 61aca1545dSVictor Chong #define SSPITOP 0x088 62aca1545dSVictor Chong #define SSPTDR 0x08C 63aca1545dSVictor Chong 64aca1545dSVictor Chong #define SSPPeriphID0 0xFE0 65aca1545dSVictor Chong #define SSPPeriphID1 0xFE4 66aca1545dSVictor Chong #define SSPPeriphID2 0xFE8 67aca1545dSVictor Chong #define SSPPeriphID3 0xFEC 68aca1545dSVictor Chong 69aca1545dSVictor Chong #define SSPPCellID0 0xFF0 70aca1545dSVictor Chong #define SSPPCellID1 0xFF4 71aca1545dSVictor Chong #define SSPPCellID2 0xFF8 72aca1545dSVictor Chong #define SSPPCellID3 0xFFC 73aca1545dSVictor Chong 74aca1545dSVictor Chong /* SPI register masks */ 75aca1545dSVictor Chong #define SSPCR0_SCR SHIFT_U32(0xFF, 8) 76aca1545dSVictor Chong #define SSPCR0_SPH SHIFT_U32(1, 7) 77aca1545dSVictor Chong #define SSPCR0_SPH1 SHIFT_U32(1, 7) 78aca1545dSVictor Chong #define SSPCR0_SPH0 SHIFT_U32(0, 7) 79aca1545dSVictor Chong #define SSPCR0_SPO SHIFT_U32(1, 6) 80aca1545dSVictor Chong #define SSPCR0_SPO1 SHIFT_U32(1, 6) 81aca1545dSVictor Chong #define SSPCR0_SPO0 SHIFT_U32(0, 6) 82aca1545dSVictor Chong #define SSPCR0_FRF SHIFT_U32(3, 4) 83aca1545dSVictor Chong #define SSPCR0_FRF_SPI SHIFT_U32(0, 4) 84aca1545dSVictor Chong #define SSPCR0_DSS SHIFT_U32(0xFF, 0) 85aca1545dSVictor Chong #define SSPCR0_DSS_16BIT SHIFT_U32(0xF, 0) 86aca1545dSVictor Chong #define SSPCR0_DSS_8BIT SHIFT_U32(7, 0) 87aca1545dSVictor Chong 88aca1545dSVictor Chong #define SSPCR1_SOD SHIFT_U32(1, 3) 89aca1545dSVictor Chong #define SSPCR1_SOD_ENABLE SHIFT_U32(1, 3) 90aca1545dSVictor Chong #define SSPCR1_SOD_DISABLE SHIFT_U32(0, 3) 91aca1545dSVictor Chong #define SSPCR1_MS SHIFT_U32(1, 2) 92aca1545dSVictor Chong #define SSPCR1_MS_SLAVE SHIFT_U32(1, 2) 93aca1545dSVictor Chong #define SSPCR1_MS_MASTER SHIFT_U32(0, 2) 94aca1545dSVictor Chong #define SSPCR1_SSE SHIFT_U32(1, 1) 95aca1545dSVictor Chong #define SSPCR1_SSE_ENABLE SHIFT_U32(1, 1) 96aca1545dSVictor Chong #define SSPCR1_SSE_DISABLE SHIFT_U32(0, 1) 97aca1545dSVictor Chong #define SSPCR1_LBM SHIFT_U32(1, 0) 98aca1545dSVictor Chong #define SSPCR1_LBM_YES SHIFT_U32(1, 0) 99aca1545dSVictor Chong #define SSPCR1_LBM_NO SHIFT_U32(0, 0) 100aca1545dSVictor Chong 101aca1545dSVictor Chong #define SSPDR_DATA SHIFT_U32(0xFFFF, 0) 102aca1545dSVictor Chong 103aca1545dSVictor Chong #define SSPSR_BSY SHIFT_U32(1, 4) 104aca1545dSVictor Chong #define SSPSR_RNF SHIFT_U32(1, 3) 105aca1545dSVictor Chong #define SSPSR_RNE SHIFT_U32(1, 2) 106aca1545dSVictor Chong #define SSPSR_TNF SHIFT_U32(1, 1) 107aca1545dSVictor Chong #define SSPSR_TFE SHIFT_U32(1, 0) 108aca1545dSVictor Chong 109aca1545dSVictor Chong #define SSPCPSR_CPSDVR SHIFT_U32(0xFF, 0) 110aca1545dSVictor Chong 111aca1545dSVictor Chong #define SSPIMSC_TXIM SHIFT_U32(1, 3) 112aca1545dSVictor Chong #define SSPIMSC_RXIM SHIFT_U32(1, 2) 113aca1545dSVictor Chong #define SSPIMSC_RTIM SHIFT_U32(1, 1) 114aca1545dSVictor Chong #define SSPIMSC_RORIM SHIFT_U32(1, 0) 115aca1545dSVictor Chong 116aca1545dSVictor Chong #define SSPRIS_TXRIS SHIFT_U32(1, 3) 117aca1545dSVictor Chong #define SSPRIS_RXRIS SHIFT_U32(1, 2) 118aca1545dSVictor Chong #define SSPRIS_RTRIS SHIFT_U32(1, 1) 119aca1545dSVictor Chong #define SSPRIS_RORRIS SHIFT_U32(1, 0) 120aca1545dSVictor Chong 121aca1545dSVictor Chong #define SSPMIS_TXMIS SHIFT_U32(1, 3) 122aca1545dSVictor Chong #define SSPMIS_RXMIS SHIFT_U32(1, 2) 123aca1545dSVictor Chong #define SSPMIS_RTMIS SHIFT_U32(1, 1) 124aca1545dSVictor Chong #define SSPMIS_RORMIS SHIFT_U32(1, 0) 125aca1545dSVictor Chong 126aca1545dSVictor Chong #define SSPICR_RTIC SHIFT_U32(1, 1) 127aca1545dSVictor Chong #define SSPICR_RORIC SHIFT_U32(1, 0) 128aca1545dSVictor Chong 129aca1545dSVictor Chong #define SSPDMACR_TXDMAE SHIFT_U32(1, 1) 130aca1545dSVictor Chong #define SSPDMACR_RXDMAE SHIFT_U32(1, 0) 131aca1545dSVictor Chong 132aca1545dSVictor Chong #define SSPPeriphID0_PartNumber0 SHIFT_U32(0xFF, 0) /* 0x22 */ 133aca1545dSVictor Chong #define SSPPeriphID1_Designer0 SHIFT_U32(0xF, 4) /* 0x1 */ 134aca1545dSVictor Chong #define SSPPeriphID1_PartNumber1 SHIFT_U32(0xF, 0) /* 0x0 */ 135aca1545dSVictor Chong #define SSPPeriphID2_Revision SHIFT_U32(0xF, 4) 136aca1545dSVictor Chong #define SSPPeriphID2_Designer1 SHIFT_U32(0xF, 0) /* 0x4 */ 137aca1545dSVictor Chong #define SSPPeriphID3_Configuration SHIFT_U32(0xFF, 0) /* 0x00 */ 138aca1545dSVictor Chong 139aca1545dSVictor Chong #define SSPPCellID_0 SHIFT_U32(0xFF, 0) /* 0x0D */ 140aca1545dSVictor Chong #define SSPPCellID_1 SHIFT_U32(0xFF, 0) /* 0xF0 */ 141aca1545dSVictor Chong #define SSPPPCellID_2 SHIFT_U32(0xFF, 0) /* 0x05 */ 142aca1545dSVictor Chong #define SSPPPCellID_3 SHIFT_U32(0xFF, 0) /* 0xB1 */ 143aca1545dSVictor Chong 144aca1545dSVictor Chong #define MASK_32 0xFFFFFFFF 145aca1545dSVictor Chong #define MASK_28 0xFFFFFFF 146aca1545dSVictor Chong #define MASK_24 0xFFFFFF 147aca1545dSVictor Chong #define MASK_20 0xFFFFF 148aca1545dSVictor Chong #define MASK_16 0xFFFF 149aca1545dSVictor Chong #define MASK_12 0xFFF 150aca1545dSVictor Chong #define MASK_8 0xFF 151aca1545dSVictor Chong #define MASK_4 0xF 152aca1545dSVictor Chong /* SPI register masks */ 153aca1545dSVictor Chong 154aca1545dSVictor Chong #define SSP_CPSDVR_MAX 254 155aca1545dSVictor Chong #define SSP_CPSDVR_MIN 2 156aca1545dSVictor Chong #define SSP_SCR_MAX 255 157aca1545dSVictor Chong #define SSP_SCR_MIN 0 158aca1545dSVictor Chong #define SSP_DATASIZE_MAX 16 159aca1545dSVictor Chong 1609a2efe04SVictor Chong static enum spi_result pl022_txrx8(struct spi_chip *chip, uint8_t *wdat, 1619a2efe04SVictor Chong uint8_t *rdat, size_t num_pkts) 162aca1545dSVictor Chong { 163aca1545dSVictor Chong size_t i = 0; 164aca1545dSVictor Chong size_t j = 0; 165aca1545dSVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 166aca1545dSVictor Chong 1679a2efe04SVictor Chong 1689a2efe04SVictor Chong if (pd->data_size_bits != 8) { 1699a2efe04SVictor Chong EMSG("data_size_bits should be 8, not %u", 1709a2efe04SVictor Chong pd->data_size_bits); 1719a2efe04SVictor Chong return SPI_ERR_CFG; 1729a2efe04SVictor Chong } 1739a2efe04SVictor Chong 1742ff86f60SVictor Chong if (wdat) 1752ff86f60SVictor Chong while (i < num_pkts) 1762ff86f60SVictor Chong if (read8(pd->base + SSPSR) & SSPSR_TNF) { 177aca1545dSVictor Chong /* tx 1 packet */ 178aca1545dSVictor Chong write8(wdat[i++], pd->base + SSPDR); 179aca1545dSVictor Chong } 180aca1545dSVictor Chong 1819a2efe04SVictor Chong if (rdat) { 1829a2efe04SVictor Chong while ((j < num_pkts) && 1839a2efe04SVictor Chong (read8(pd->base + SSPSR) & SSPSR_BSY)) 1842ff86f60SVictor Chong if (read8(pd->base + SSPSR) & SSPSR_RNE) { 185aca1545dSVictor Chong /* rx 1 packet */ 186aca1545dSVictor Chong rdat[j++] = read8(pd->base + SSPDR); 1872ff86f60SVictor Chong } 188aca1545dSVictor Chong 1899a2efe04SVictor Chong if (j < num_pkts) { 1909a2efe04SVictor Chong EMSG("Packets requested %zu, received %zu", 1919a2efe04SVictor Chong num_pkts, j); 1929a2efe04SVictor Chong return SPI_ERR_PKTCNT; 1939a2efe04SVictor Chong } 194aca1545dSVictor Chong } 195aca1545dSVictor Chong 1969a2efe04SVictor Chong return SPI_OK; 1979a2efe04SVictor Chong } 1989a2efe04SVictor Chong 1999a2efe04SVictor Chong static enum spi_result pl022_txrx16(struct spi_chip *chip, uint16_t *wdat, 2009a2efe04SVictor Chong uint16_t *rdat, size_t num_pkts) 201aca1545dSVictor Chong { 202aca1545dSVictor Chong size_t i = 0; 203aca1545dSVictor Chong size_t j = 0; 204aca1545dSVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 205aca1545dSVictor Chong 2069a2efe04SVictor Chong if (pd->data_size_bits != 16) { 2079a2efe04SVictor Chong EMSG("data_size_bits should be 16, not %u", 2089a2efe04SVictor Chong pd->data_size_bits); 2099a2efe04SVictor Chong return SPI_ERR_CFG; 2109a2efe04SVictor Chong } 2119a2efe04SVictor Chong 2122ff86f60SVictor Chong if (wdat) 2132ff86f60SVictor Chong while (i < num_pkts) 2142ff86f60SVictor Chong if (read8(pd->base + SSPSR) & SSPSR_TNF) { 215aca1545dSVictor Chong /* tx 1 packet */ 216aca1545dSVictor Chong write16(wdat[i++], pd->base + SSPDR); 217aca1545dSVictor Chong } 218aca1545dSVictor Chong 2199a2efe04SVictor Chong if (rdat) { 2209a2efe04SVictor Chong while ((j < num_pkts) && 2219a2efe04SVictor Chong (read8(pd->base + SSPSR) & SSPSR_BSY)) 2222ff86f60SVictor Chong if (read8(pd->base + SSPSR) & SSPSR_RNE) { 223aca1545dSVictor Chong /* rx 1 packet */ 224aca1545dSVictor Chong rdat[j++] = read16(pd->base + SSPDR); 225aca1545dSVictor Chong } 226aca1545dSVictor Chong 2279a2efe04SVictor Chong if (j < num_pkts) { 2289a2efe04SVictor Chong EMSG("Packets requested %zu, received %zu", 2299a2efe04SVictor Chong num_pkts, j); 2309a2efe04SVictor Chong return SPI_ERR_PKTCNT; 2319a2efe04SVictor Chong } 2329a2efe04SVictor Chong } 2339a2efe04SVictor Chong 2349a2efe04SVictor Chong return SPI_OK; 235aca1545dSVictor Chong } 236aca1545dSVictor Chong 237aca1545dSVictor Chong static void pl022_print_peri_id(struct pl022_data *pd __maybe_unused) 238aca1545dSVictor Chong { 239aca1545dSVictor Chong DMSG("Expected: 0x 22 10 ?4 00"); 240aca1545dSVictor Chong DMSG("Read: 0x %02x %02x %02x %02x", 241aca1545dSVictor Chong read32(pd->base + SSPPeriphID0), 242aca1545dSVictor Chong read32(pd->base + SSPPeriphID1), 243aca1545dSVictor Chong read32(pd->base + SSPPeriphID2), 244aca1545dSVictor Chong read32(pd->base + SSPPeriphID3)); 245aca1545dSVictor Chong } 246aca1545dSVictor Chong 247aca1545dSVictor Chong static void pl022_print_cell_id(struct pl022_data *pd __maybe_unused) 248aca1545dSVictor Chong { 249aca1545dSVictor Chong DMSG("Expected: 0x 0d f0 05 b1"); 250aca1545dSVictor Chong DMSG("Read: 0x %02x %02x %02x %02x", 251aca1545dSVictor Chong read32(pd->base + SSPPCellID0), 252aca1545dSVictor Chong read32(pd->base + SSPPCellID1), 253aca1545dSVictor Chong read32(pd->base + SSPPCellID2), 254aca1545dSVictor Chong read32(pd->base + SSPPCellID3)); 255aca1545dSVictor Chong } 256aca1545dSVictor Chong 257aca1545dSVictor Chong static void pl022_sanity_check(struct pl022_data *pd) 258aca1545dSVictor Chong { 259aca1545dSVictor Chong assert(pd); 260aca1545dSVictor Chong assert(pd->chip.ops); 261*26128b8fSVictor Chong assert(pd->cs_control <= PL022_CS_CTRL_MANUAL); 262*26128b8fSVictor Chong switch (pd->cs_control) { 263*26128b8fSVictor Chong case PL022_CS_CTRL_AUTO_GPIO: 264*26128b8fSVictor Chong assert(pd->cs_data.gpio_data.chip); 265*26128b8fSVictor Chong assert(pd->cs_data.gpio_data.chip->ops); 266*26128b8fSVictor Chong break; 267*26128b8fSVictor Chong case PL022_CS_CTRL_CB: 268*26128b8fSVictor Chong assert(pd->cs_data.cs_cb); 269*26128b8fSVictor Chong break; 270*26128b8fSVictor Chong default: 271*26128b8fSVictor Chong break; 272*26128b8fSVictor Chong } 273aca1545dSVictor Chong assert(pd->clk_hz); 274aca1545dSVictor Chong assert(pd->speed_hz && pd->speed_hz <= pd->clk_hz/2); 275aca1545dSVictor Chong assert(pd->mode <= SPI_MODE3); 276aca1545dSVictor Chong assert(pd->data_size_bits == 8 || pd->data_size_bits == 16); 277aca1545dSVictor Chong 278aca1545dSVictor Chong #ifdef PLATFORM_hikey 279aca1545dSVictor Chong DMSG("SSPB2BTRANS: Expected: 0x2. Read: 0x%x", 280aca1545dSVictor Chong read32(pd->base + SSPB2BTRANS)); 281aca1545dSVictor Chong #endif 282aca1545dSVictor Chong pl022_print_peri_id(pd); 283aca1545dSVictor Chong pl022_print_cell_id(pd); 284aca1545dSVictor Chong } 285aca1545dSVictor Chong 286aca1545dSVictor Chong static inline uint32_t pl022_calc_freq(struct pl022_data *pd, 287aca1545dSVictor Chong uint8_t cpsdvr, uint8_t scr) 288aca1545dSVictor Chong { 289aca1545dSVictor Chong return pd->clk_hz / (cpsdvr * (1 + scr)); 290aca1545dSVictor Chong } 291aca1545dSVictor Chong 292*26128b8fSVictor Chong static void pl022_control_cs(struct spi_chip *chip, enum gpio_level value) 293*26128b8fSVictor Chong { 294*26128b8fSVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 295*26128b8fSVictor Chong 296*26128b8fSVictor Chong switch (pd->cs_control) { 297*26128b8fSVictor Chong case PL022_CS_CTRL_AUTO_GPIO: 298*26128b8fSVictor Chong if (read8(pd->base + SSPSR) & SSPSR_BSY) 299*26128b8fSVictor Chong DMSG("pl022 busy - do NOT set CS!"); 300*26128b8fSVictor Chong while (read8(pd->base + SSPSR) & SSPSR_BSY) 301*26128b8fSVictor Chong ; 302*26128b8fSVictor Chong DMSG("pl022 done - set CS!"); 303*26128b8fSVictor Chong 304*26128b8fSVictor Chong pd->cs_data.gpio_data.chip->ops->set_value( 305*26128b8fSVictor Chong pd->cs_data.gpio_data.pin_num, value); 306*26128b8fSVictor Chong break; 307*26128b8fSVictor Chong case PL022_CS_CTRL_CB: 308*26128b8fSVictor Chong pd->cs_data.cs_cb(value); 309*26128b8fSVictor Chong break; 310*26128b8fSVictor Chong default: 311*26128b8fSVictor Chong break; 312*26128b8fSVictor Chong } 313*26128b8fSVictor Chong } 314*26128b8fSVictor Chong 315aca1545dSVictor Chong static void pl022_calc_clk_divisors(struct pl022_data *pd, 316aca1545dSVictor Chong uint8_t *cpsdvr, uint8_t *scr) 317aca1545dSVictor Chong { 318aca1545dSVictor Chong unsigned int freq1 = 0; 319aca1545dSVictor Chong unsigned int freq2 = 0; 320aca1545dSVictor Chong uint8_t tmp_cpsdvr1; 321aca1545dSVictor Chong uint8_t tmp_scr1; 322aca1545dSVictor Chong uint8_t tmp_cpsdvr2 = 0; 323aca1545dSVictor Chong uint8_t tmp_scr2 = 0; 324aca1545dSVictor Chong 325aca1545dSVictor Chong for (tmp_scr1 = SSP_SCR_MIN; tmp_scr1 < SSP_SCR_MAX; tmp_scr1++) { 326aca1545dSVictor Chong for (tmp_cpsdvr1 = SSP_CPSDVR_MIN; tmp_cpsdvr1 < SSP_CPSDVR_MAX; 327aca1545dSVictor Chong tmp_cpsdvr1++) { 328aca1545dSVictor Chong freq1 = pl022_calc_freq(pd, tmp_cpsdvr1, tmp_scr1); 329aca1545dSVictor Chong if (freq1 == pd->speed_hz) 330aca1545dSVictor Chong goto done; 331aca1545dSVictor Chong else if (freq1 < pd->speed_hz) 332aca1545dSVictor Chong goto stage2; 333aca1545dSVictor Chong } 334aca1545dSVictor Chong } 335aca1545dSVictor Chong 336aca1545dSVictor Chong stage2: 337aca1545dSVictor Chong for (tmp_cpsdvr2 = SSP_CPSDVR_MIN; tmp_cpsdvr2 < SSP_CPSDVR_MAX; 338aca1545dSVictor Chong tmp_cpsdvr2++) { 339aca1545dSVictor Chong for (tmp_scr2 = SSP_SCR_MIN; tmp_scr2 < SSP_SCR_MAX; 340aca1545dSVictor Chong tmp_scr2++) { 341aca1545dSVictor Chong freq2 = pl022_calc_freq(pd, tmp_cpsdvr2, tmp_scr2); 342aca1545dSVictor Chong if (freq2 <= pd->speed_hz) 343aca1545dSVictor Chong goto done; 344aca1545dSVictor Chong } 345aca1545dSVictor Chong } 346aca1545dSVictor Chong 347aca1545dSVictor Chong done: 348aca1545dSVictor Chong if (freq1 >= freq2) { 349aca1545dSVictor Chong *cpsdvr = tmp_cpsdvr1; 350aca1545dSVictor Chong *scr = tmp_scr1; 351aca1545dSVictor Chong DMSG("speed: requested: %u, closest1: %u", 352aca1545dSVictor Chong pd->speed_hz, freq1); 353aca1545dSVictor Chong } else { 354aca1545dSVictor Chong *cpsdvr = tmp_cpsdvr2; 355aca1545dSVictor Chong *scr = tmp_scr2; 356aca1545dSVictor Chong DMSG("speed: requested: %u, closest2: %u", 357aca1545dSVictor Chong pd->speed_hz, freq2); 358aca1545dSVictor Chong } 359aca1545dSVictor Chong DMSG("CPSDVR: %u (0x%x), SCR: %u (0x%x)", 360aca1545dSVictor Chong *cpsdvr, *cpsdvr, *scr, *scr); 361aca1545dSVictor Chong } 362aca1545dSVictor Chong 363aca1545dSVictor Chong static void pl022_flush_fifo(struct pl022_data *pd) 364aca1545dSVictor Chong { 365aca1545dSVictor Chong uint32_t __maybe_unused rdat; 366aca1545dSVictor Chong 367aca1545dSVictor Chong do { 368aca1545dSVictor Chong while (read32(pd->base + SSPSR) & SSPSR_RNE) { 369aca1545dSVictor Chong rdat = read32(pd->base + SSPDR); 370aca1545dSVictor Chong DMSG("rdat: 0x%x", rdat); 371aca1545dSVictor Chong } 372aca1545dSVictor Chong } while (read32(pd->base + SSPSR) & SSPSR_BSY); 373aca1545dSVictor Chong } 374aca1545dSVictor Chong 3756356eeb2SVictor Chong static void pl022_configure(struct spi_chip *chip) 376aca1545dSVictor Chong { 377aca1545dSVictor Chong uint16_t mode; 378aca1545dSVictor Chong uint16_t data_size; 379aca1545dSVictor Chong uint8_t cpsdvr; 380aca1545dSVictor Chong uint8_t scr; 381aca1545dSVictor Chong uint8_t lbm; 3826356eeb2SVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 383aca1545dSVictor Chong 384aca1545dSVictor Chong pl022_sanity_check(pd); 385*26128b8fSVictor Chong 386*26128b8fSVictor Chong switch (pd->cs_control) { 387*26128b8fSVictor Chong case PL022_CS_CTRL_AUTO_GPIO: 388*26128b8fSVictor Chong DMSG("Use auto GPIO CS control"); 389*26128b8fSVictor Chong DMSG("Mask/disable interrupt for CS GPIO"); 390*26128b8fSVictor Chong pd->cs_data.gpio_data.chip->ops->set_interrupt( 391*26128b8fSVictor Chong pd->cs_data.gpio_data.pin_num, 392*26128b8fSVictor Chong GPIO_INTERRUPT_DISABLE); 393*26128b8fSVictor Chong DMSG("Set CS GPIO dir to out"); 394*26128b8fSVictor Chong pd->cs_data.gpio_data.chip->ops->set_direction( 395*26128b8fSVictor Chong pd->cs_data.gpio_data.pin_num, 396*26128b8fSVictor Chong GPIO_DIR_OUT); 397*26128b8fSVictor Chong break; 398*26128b8fSVictor Chong case PL022_CS_CTRL_CB: 399*26128b8fSVictor Chong DMSG("Use registered CS callback"); 400*26128b8fSVictor Chong break; 401*26128b8fSVictor Chong case PL022_CS_CTRL_MANUAL: 402*26128b8fSVictor Chong DMSG("Use manual CS control"); 403*26128b8fSVictor Chong break; 404*26128b8fSVictor Chong default: 405*26128b8fSVictor Chong EMSG("Invalid CS control type: %d", pd->cs_control); 406*26128b8fSVictor Chong panic(); 407*26128b8fSVictor Chong } 408*26128b8fSVictor Chong 409*26128b8fSVictor Chong DMSG("Pull CS high"); 410*26128b8fSVictor Chong pl022_control_cs(chip, GPIO_LEVEL_HIGH); 411*26128b8fSVictor Chong 412aca1545dSVictor Chong pl022_calc_clk_divisors(pd, &cpsdvr, &scr); 413aca1545dSVictor Chong 414aca1545dSVictor Chong /* configure ssp based on platform settings */ 415aca1545dSVictor Chong switch (pd->mode) { 416aca1545dSVictor Chong case SPI_MODE0: 4172ff86f60SVictor Chong DMSG("SPI mode 0"); 4182ff86f60SVictor Chong mode = SSPCR0_SPO0 | SSPCR0_SPH0; 419aca1545dSVictor Chong break; 420aca1545dSVictor Chong case SPI_MODE1: 4212ff86f60SVictor Chong DMSG("SPI mode 1"); 4222ff86f60SVictor Chong mode = SSPCR0_SPO0 | SSPCR0_SPH1; 423aca1545dSVictor Chong break; 424aca1545dSVictor Chong case SPI_MODE2: 4252ff86f60SVictor Chong DMSG("SPI mode 2"); 4262ff86f60SVictor Chong mode = SSPCR0_SPO1 | SSPCR0_SPH0; 427aca1545dSVictor Chong break; 428aca1545dSVictor Chong case SPI_MODE3: 4292ff86f60SVictor Chong DMSG("SPI mode 3"); 4302ff86f60SVictor Chong mode = SSPCR0_SPO1 | SSPCR0_SPH1; 431aca1545dSVictor Chong break; 432aca1545dSVictor Chong default: 433aca1545dSVictor Chong EMSG("Invalid SPI mode: %u", pd->mode); 434aca1545dSVictor Chong panic(); 435aca1545dSVictor Chong } 436aca1545dSVictor Chong 437aca1545dSVictor Chong switch (pd->data_size_bits) { 438aca1545dSVictor Chong case 8: 439aca1545dSVictor Chong DMSG("Data size: 8"); 4402ff86f60SVictor Chong data_size = SSPCR0_DSS_8BIT; 441aca1545dSVictor Chong break; 442aca1545dSVictor Chong case 16: 443aca1545dSVictor Chong DMSG("Data size: 16"); 4442ff86f60SVictor Chong data_size = SSPCR0_DSS_16BIT; 445aca1545dSVictor Chong break; 446aca1545dSVictor Chong default: 447aca1545dSVictor Chong EMSG("Unsupported data size: %u bits", pd->data_size_bits); 448aca1545dSVictor Chong panic(); 449aca1545dSVictor Chong } 450aca1545dSVictor Chong 451aca1545dSVictor Chong if (pd->loopback) { 452aca1545dSVictor Chong DMSG("Starting in loopback mode!"); 453aca1545dSVictor Chong lbm = SSPCR1_LBM_YES; 454aca1545dSVictor Chong } else { 455aca1545dSVictor Chong DMSG("Starting in regular (non-loopback) mode!"); 456aca1545dSVictor Chong lbm = SSPCR1_LBM_NO; 457aca1545dSVictor Chong } 458aca1545dSVictor Chong 459*26128b8fSVictor Chong DMSG("Set Serial Clock Rate (SCR), SPI mode (phase and clock)"); 460*26128b8fSVictor Chong DMSG("Set frame format (SPI) and data size (8- or 16-bit)"); 461aca1545dSVictor Chong io_mask16(pd->base + SSPCR0, SHIFT_U32(scr, 8) | mode | SSPCR0_FRF_SPI | 462aca1545dSVictor Chong data_size, MASK_16); 463aca1545dSVictor Chong 464*26128b8fSVictor Chong DMSG("Set master mode, disable SSP, set loopback mode"); 465aca1545dSVictor Chong io_mask8(pd->base + SSPCR1, SSPCR1_SOD_DISABLE | SSPCR1_MS_MASTER | 466aca1545dSVictor Chong SSPCR1_SSE_DISABLE | lbm, MASK_4); 467aca1545dSVictor Chong 468*26128b8fSVictor Chong DMSG("Set clock prescale"); 469aca1545dSVictor Chong io_mask8(pd->base + SSPCPSR, cpsdvr, SSPCPSR_CPSDVR); 470aca1545dSVictor Chong 471*26128b8fSVictor Chong DMSG("Disable interrupts"); 472aca1545dSVictor Chong io_mask8(pd->base + SSPIMSC, 0, MASK_4); 473aca1545dSVictor Chong 474*26128b8fSVictor Chong DMSG("Clear interrupts"); 4759a2efe04SVictor Chong io_mask8(pd->base + SSPICR, SSPICR_RORIC | SSPICR_RTIC, 4769a2efe04SVictor Chong SSPICR_RORIC | SSPICR_RTIC); 4779a2efe04SVictor Chong 478*26128b8fSVictor Chong DMSG("Empty FIFO before starting"); 4796356eeb2SVictor Chong pl022_flush_fifo(pd); 480aca1545dSVictor Chong } 481aca1545dSVictor Chong 4826356eeb2SVictor Chong static void pl022_start(struct spi_chip *chip) 483aca1545dSVictor Chong { 4846356eeb2SVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 485aca1545dSVictor Chong 486*26128b8fSVictor Chong DMSG("Enable SSP"); 487aca1545dSVictor Chong io_mask8(pd->base + SSPCR1, SSPCR1_SSE_ENABLE, SSPCR1_SSE); 488*26128b8fSVictor Chong 489*26128b8fSVictor Chong pl022_control_cs(chip, GPIO_LEVEL_LOW); 490aca1545dSVictor Chong } 491aca1545dSVictor Chong 4926356eeb2SVictor Chong static void pl022_end(struct spi_chip *chip) 493aca1545dSVictor Chong { 4946356eeb2SVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 4956356eeb2SVictor Chong 496*26128b8fSVictor Chong pl022_control_cs(chip, GPIO_LEVEL_HIGH); 497*26128b8fSVictor Chong 498*26128b8fSVictor Chong DMSG("Disable SSP"); 499aca1545dSVictor Chong io_mask8(pd->base + SSPCR1, SSPCR1_SSE_DISABLE, SSPCR1_SSE); 500aca1545dSVictor Chong } 501aca1545dSVictor Chong 5026356eeb2SVictor Chong static const struct spi_ops pl022_ops = { 5036356eeb2SVictor Chong .configure = pl022_configure, 5046356eeb2SVictor Chong .start = pl022_start, 5056356eeb2SVictor Chong .txrx8 = pl022_txrx8, 5066356eeb2SVictor Chong .txrx16 = pl022_txrx16, 5076356eeb2SVictor Chong .end = pl022_end, 5086356eeb2SVictor Chong }; 5096356eeb2SVictor Chong 5106356eeb2SVictor Chong void pl022_init(struct pl022_data *pd) 5116356eeb2SVictor Chong { 5126356eeb2SVictor Chong assert(pd); 5136356eeb2SVictor Chong pd->chip.ops = &pl022_ops; 5146356eeb2SVictor Chong } 515