1*1bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause 2aca1545dSVictor Chong /* 3aca1545dSVictor Chong * Copyright (c) 2016, Linaro Limited 4aca1545dSVictor Chong * All rights reserved. 5aca1545dSVictor Chong * 6aca1545dSVictor Chong * Redistribution and use in source and binary forms, with or without 7aca1545dSVictor Chong * modification, are permitted provided that the following conditions are met: 8aca1545dSVictor Chong * 9aca1545dSVictor Chong * 1. Redistributions of source code must retain the above copyright notice, 10aca1545dSVictor Chong * this list of conditions and the following disclaimer. 11aca1545dSVictor Chong * 12aca1545dSVictor Chong * 2. Redistributions in binary form must reproduce the above copyright notice, 13aca1545dSVictor Chong * this list of conditions and the following disclaimer in the documentation 14aca1545dSVictor Chong * and/or other materials provided with the distribution. 15aca1545dSVictor Chong * 16aca1545dSVictor Chong * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17aca1545dSVictor Chong * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18aca1545dSVictor Chong * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19aca1545dSVictor Chong * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20aca1545dSVictor Chong * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21aca1545dSVictor Chong * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22aca1545dSVictor Chong * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23aca1545dSVictor Chong * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24aca1545dSVictor Chong * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25aca1545dSVictor Chong * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26aca1545dSVictor Chong * POSSIBILITY OF SUCH DAMAGE. 27aca1545dSVictor Chong * 28aca1545dSVictor Chong */ 29aca1545dSVictor Chong 30aca1545dSVictor Chong #include <assert.h> 31aca1545dSVictor Chong #include <drivers/pl022_spi.h> 32aca1545dSVictor Chong #include <initcall.h> 33aca1545dSVictor Chong #include <io.h> 34aca1545dSVictor Chong #include <kernel/panic.h> 35aca1545dSVictor Chong #include <kernel/tee_time.h> 36aca1545dSVictor Chong #include <platform_config.h> 37aca1545dSVictor Chong #include <trace.h> 38aca1545dSVictor Chong #include <util.h> 39aca1545dSVictor Chong 40aca1545dSVictor Chong /* SPI register offsets */ 41aca1545dSVictor Chong #define SSPCR0 0x000 42aca1545dSVictor Chong #define SSPCR1 0x004 43aca1545dSVictor Chong #define SSPDR 0x008 44aca1545dSVictor Chong #define SSPSR 0x00C 45aca1545dSVictor Chong #define SSPCPSR 0x010 46aca1545dSVictor Chong #define SSPIMSC 0x014 47aca1545dSVictor Chong #define SSPRIS 0x018 48aca1545dSVictor Chong #define SSPMIS 0x01C 49aca1545dSVictor Chong #define SSPICR 0x020 50aca1545dSVictor Chong #define SSPDMACR 0x024 51aca1545dSVictor Chong 52aca1545dSVictor Chong #ifdef PLATFORM_hikey 53aca1545dSVictor Chong /* HiKey extensions */ 54aca1545dSVictor Chong #define SSPTXFIFOCR 0x028 55aca1545dSVictor Chong #define SSPRXFIFOCR 0x02C 56aca1545dSVictor Chong #define SSPB2BTRANS 0x030 57aca1545dSVictor Chong #endif 58aca1545dSVictor Chong 59aca1545dSVictor Chong /* test registers */ 60aca1545dSVictor Chong #define SSPTCR 0x080 61aca1545dSVictor Chong #define SSPITIP 0x084 62aca1545dSVictor Chong #define SSPITOP 0x088 63aca1545dSVictor Chong #define SSPTDR 0x08C 64aca1545dSVictor Chong 65aca1545dSVictor Chong #define SSPPeriphID0 0xFE0 66aca1545dSVictor Chong #define SSPPeriphID1 0xFE4 67aca1545dSVictor Chong #define SSPPeriphID2 0xFE8 68aca1545dSVictor Chong #define SSPPeriphID3 0xFEC 69aca1545dSVictor Chong 70aca1545dSVictor Chong #define SSPPCellID0 0xFF0 71aca1545dSVictor Chong #define SSPPCellID1 0xFF4 72aca1545dSVictor Chong #define SSPPCellID2 0xFF8 73aca1545dSVictor Chong #define SSPPCellID3 0xFFC 74aca1545dSVictor Chong 75aca1545dSVictor Chong /* SPI register masks */ 76aca1545dSVictor Chong #define SSPCR0_SCR SHIFT_U32(0xFF, 8) 77aca1545dSVictor Chong #define SSPCR0_SPH SHIFT_U32(1, 7) 78aca1545dSVictor Chong #define SSPCR0_SPH1 SHIFT_U32(1, 7) 79aca1545dSVictor Chong #define SSPCR0_SPH0 SHIFT_U32(0, 7) 80aca1545dSVictor Chong #define SSPCR0_SPO SHIFT_U32(1, 6) 81aca1545dSVictor Chong #define SSPCR0_SPO1 SHIFT_U32(1, 6) 82aca1545dSVictor Chong #define SSPCR0_SPO0 SHIFT_U32(0, 6) 83aca1545dSVictor Chong #define SSPCR0_FRF SHIFT_U32(3, 4) 84aca1545dSVictor Chong #define SSPCR0_FRF_SPI SHIFT_U32(0, 4) 85aca1545dSVictor Chong #define SSPCR0_DSS SHIFT_U32(0xFF, 0) 86aca1545dSVictor Chong #define SSPCR0_DSS_16BIT SHIFT_U32(0xF, 0) 87aca1545dSVictor Chong #define SSPCR0_DSS_8BIT SHIFT_U32(7, 0) 88aca1545dSVictor Chong 89aca1545dSVictor Chong #define SSPCR1_SOD SHIFT_U32(1, 3) 90aca1545dSVictor Chong #define SSPCR1_SOD_ENABLE SHIFT_U32(1, 3) 91aca1545dSVictor Chong #define SSPCR1_SOD_DISABLE SHIFT_U32(0, 3) 92aca1545dSVictor Chong #define SSPCR1_MS SHIFT_U32(1, 2) 93aca1545dSVictor Chong #define SSPCR1_MS_SLAVE SHIFT_U32(1, 2) 94aca1545dSVictor Chong #define SSPCR1_MS_MASTER SHIFT_U32(0, 2) 95aca1545dSVictor Chong #define SSPCR1_SSE SHIFT_U32(1, 1) 96aca1545dSVictor Chong #define SSPCR1_SSE_ENABLE SHIFT_U32(1, 1) 97aca1545dSVictor Chong #define SSPCR1_SSE_DISABLE SHIFT_U32(0, 1) 98aca1545dSVictor Chong #define SSPCR1_LBM SHIFT_U32(1, 0) 99aca1545dSVictor Chong #define SSPCR1_LBM_YES SHIFT_U32(1, 0) 100aca1545dSVictor Chong #define SSPCR1_LBM_NO SHIFT_U32(0, 0) 101aca1545dSVictor Chong 102aca1545dSVictor Chong #define SSPDR_DATA SHIFT_U32(0xFFFF, 0) 103aca1545dSVictor Chong 104aca1545dSVictor Chong #define SSPSR_BSY SHIFT_U32(1, 4) 105aca1545dSVictor Chong #define SSPSR_RNF SHIFT_U32(1, 3) 106aca1545dSVictor Chong #define SSPSR_RNE SHIFT_U32(1, 2) 107aca1545dSVictor Chong #define SSPSR_TNF SHIFT_U32(1, 1) 108aca1545dSVictor Chong #define SSPSR_TFE SHIFT_U32(1, 0) 109aca1545dSVictor Chong 110aca1545dSVictor Chong #define SSPCPSR_CPSDVR SHIFT_U32(0xFF, 0) 111aca1545dSVictor Chong 112aca1545dSVictor Chong #define SSPIMSC_TXIM SHIFT_U32(1, 3) 113aca1545dSVictor Chong #define SSPIMSC_RXIM SHIFT_U32(1, 2) 114aca1545dSVictor Chong #define SSPIMSC_RTIM SHIFT_U32(1, 1) 115aca1545dSVictor Chong #define SSPIMSC_RORIM SHIFT_U32(1, 0) 116aca1545dSVictor Chong 117aca1545dSVictor Chong #define SSPRIS_TXRIS SHIFT_U32(1, 3) 118aca1545dSVictor Chong #define SSPRIS_RXRIS SHIFT_U32(1, 2) 119aca1545dSVictor Chong #define SSPRIS_RTRIS SHIFT_U32(1, 1) 120aca1545dSVictor Chong #define SSPRIS_RORRIS SHIFT_U32(1, 0) 121aca1545dSVictor Chong 122aca1545dSVictor Chong #define SSPMIS_TXMIS SHIFT_U32(1, 3) 123aca1545dSVictor Chong #define SSPMIS_RXMIS SHIFT_U32(1, 2) 124aca1545dSVictor Chong #define SSPMIS_RTMIS SHIFT_U32(1, 1) 125aca1545dSVictor Chong #define SSPMIS_RORMIS SHIFT_U32(1, 0) 126aca1545dSVictor Chong 127aca1545dSVictor Chong #define SSPICR_RTIC SHIFT_U32(1, 1) 128aca1545dSVictor Chong #define SSPICR_RORIC SHIFT_U32(1, 0) 129aca1545dSVictor Chong 130aca1545dSVictor Chong #define SSPDMACR_TXDMAE SHIFT_U32(1, 1) 131aca1545dSVictor Chong #define SSPDMACR_RXDMAE SHIFT_U32(1, 0) 132aca1545dSVictor Chong 133aca1545dSVictor Chong #define SSPPeriphID0_PartNumber0 SHIFT_U32(0xFF, 0) /* 0x22 */ 134aca1545dSVictor Chong #define SSPPeriphID1_Designer0 SHIFT_U32(0xF, 4) /* 0x1 */ 135aca1545dSVictor Chong #define SSPPeriphID1_PartNumber1 SHIFT_U32(0xF, 0) /* 0x0 */ 136aca1545dSVictor Chong #define SSPPeriphID2_Revision SHIFT_U32(0xF, 4) 137aca1545dSVictor Chong #define SSPPeriphID2_Designer1 SHIFT_U32(0xF, 0) /* 0x4 */ 138aca1545dSVictor Chong #define SSPPeriphID3_Configuration SHIFT_U32(0xFF, 0) /* 0x00 */ 139aca1545dSVictor Chong 140aca1545dSVictor Chong #define SSPPCellID_0 SHIFT_U32(0xFF, 0) /* 0x0D */ 141aca1545dSVictor Chong #define SSPPCellID_1 SHIFT_U32(0xFF, 0) /* 0xF0 */ 142aca1545dSVictor Chong #define SSPPPCellID_2 SHIFT_U32(0xFF, 0) /* 0x05 */ 143aca1545dSVictor Chong #define SSPPPCellID_3 SHIFT_U32(0xFF, 0) /* 0xB1 */ 144aca1545dSVictor Chong 145aca1545dSVictor Chong #define MASK_32 0xFFFFFFFF 146aca1545dSVictor Chong #define MASK_28 0xFFFFFFF 147aca1545dSVictor Chong #define MASK_24 0xFFFFFF 148aca1545dSVictor Chong #define MASK_20 0xFFFFF 149aca1545dSVictor Chong #define MASK_16 0xFFFF 150aca1545dSVictor Chong #define MASK_12 0xFFF 151aca1545dSVictor Chong #define MASK_8 0xFF 152aca1545dSVictor Chong #define MASK_4 0xF 153aca1545dSVictor Chong /* SPI register masks */ 154aca1545dSVictor Chong 155aca1545dSVictor Chong #define SSP_CPSDVR_MAX 254 156aca1545dSVictor Chong #define SSP_CPSDVR_MIN 2 157aca1545dSVictor Chong #define SSP_SCR_MAX 255 158aca1545dSVictor Chong #define SSP_SCR_MIN 0 159aca1545dSVictor Chong #define SSP_DATASIZE_MAX 16 160aca1545dSVictor Chong 1619a2efe04SVictor Chong static enum spi_result pl022_txrx8(struct spi_chip *chip, uint8_t *wdat, 1629a2efe04SVictor Chong uint8_t *rdat, size_t num_pkts) 163aca1545dSVictor Chong { 164aca1545dSVictor Chong size_t i = 0; 165aca1545dSVictor Chong size_t j = 0; 166aca1545dSVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 167aca1545dSVictor Chong 1689a2efe04SVictor Chong 1699a2efe04SVictor Chong if (pd->data_size_bits != 8) { 1709a2efe04SVictor Chong EMSG("data_size_bits should be 8, not %u", 1719a2efe04SVictor Chong pd->data_size_bits); 1729a2efe04SVictor Chong return SPI_ERR_CFG; 1739a2efe04SVictor Chong } 1749a2efe04SVictor Chong 1752ff86f60SVictor Chong if (wdat) 17672057c7cSVictor Chong while (i < num_pkts) { 1772ff86f60SVictor Chong if (read8(pd->base + SSPSR) & SSPSR_TNF) { 178aca1545dSVictor Chong /* tx 1 packet */ 179aca1545dSVictor Chong write8(wdat[i++], pd->base + SSPDR); 180aca1545dSVictor Chong } 181aca1545dSVictor Chong 18272057c7cSVictor Chong if (rdat) 18372057c7cSVictor Chong if (read8(pd->base + SSPSR) & SSPSR_RNE) { 18472057c7cSVictor Chong /* rx 1 packet */ 18572057c7cSVictor Chong rdat[j++] = read8(pd->base + SSPDR); 18672057c7cSVictor Chong } 18772057c7cSVictor Chong } 18872057c7cSVictor Chong 18972057c7cSVictor Chong /* Capture remaining rdat not read above */ 1909a2efe04SVictor Chong if (rdat) { 1919a2efe04SVictor Chong while ((j < num_pkts) && 1929a2efe04SVictor Chong (read8(pd->base + SSPSR) & SSPSR_BSY)) 1932ff86f60SVictor Chong if (read8(pd->base + SSPSR) & SSPSR_RNE) { 194aca1545dSVictor Chong /* rx 1 packet */ 195aca1545dSVictor Chong rdat[j++] = read8(pd->base + SSPDR); 1962ff86f60SVictor Chong } 197aca1545dSVictor Chong 1989a2efe04SVictor Chong if (j < num_pkts) { 1999a2efe04SVictor Chong EMSG("Packets requested %zu, received %zu", 2009a2efe04SVictor Chong num_pkts, j); 2019a2efe04SVictor Chong return SPI_ERR_PKTCNT; 2029a2efe04SVictor Chong } 203aca1545dSVictor Chong } 204aca1545dSVictor Chong 2059a2efe04SVictor Chong return SPI_OK; 2069a2efe04SVictor Chong } 2079a2efe04SVictor Chong 2089a2efe04SVictor Chong static enum spi_result pl022_txrx16(struct spi_chip *chip, uint16_t *wdat, 2099a2efe04SVictor Chong uint16_t *rdat, size_t num_pkts) 210aca1545dSVictor Chong { 211aca1545dSVictor Chong size_t i = 0; 212aca1545dSVictor Chong size_t j = 0; 213aca1545dSVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 214aca1545dSVictor Chong 2159a2efe04SVictor Chong if (pd->data_size_bits != 16) { 2169a2efe04SVictor Chong EMSG("data_size_bits should be 16, not %u", 2179a2efe04SVictor Chong pd->data_size_bits); 2189a2efe04SVictor Chong return SPI_ERR_CFG; 2199a2efe04SVictor Chong } 2209a2efe04SVictor Chong 2212ff86f60SVictor Chong if (wdat) 22272057c7cSVictor Chong while (i < num_pkts) { 2232ff86f60SVictor Chong if (read8(pd->base + SSPSR) & SSPSR_TNF) { 224aca1545dSVictor Chong /* tx 1 packet */ 225aca1545dSVictor Chong write16(wdat[i++], pd->base + SSPDR); 226aca1545dSVictor Chong } 227aca1545dSVictor Chong 22872057c7cSVictor Chong if (rdat) 22972057c7cSVictor Chong if (read8(pd->base + SSPSR) & SSPSR_RNE) { 23072057c7cSVictor Chong /* rx 1 packet */ 23172057c7cSVictor Chong rdat[j++] = read16(pd->base + SSPDR); 23272057c7cSVictor Chong } 23372057c7cSVictor Chong } 23472057c7cSVictor Chong 23572057c7cSVictor Chong /* Capture remaining rdat not read above */ 2369a2efe04SVictor Chong if (rdat) { 2379a2efe04SVictor Chong while ((j < num_pkts) && 2389a2efe04SVictor Chong (read8(pd->base + SSPSR) & SSPSR_BSY)) 2392ff86f60SVictor Chong if (read8(pd->base + SSPSR) & SSPSR_RNE) { 240aca1545dSVictor Chong /* rx 1 packet */ 241aca1545dSVictor Chong rdat[j++] = read16(pd->base + SSPDR); 242aca1545dSVictor Chong } 243aca1545dSVictor Chong 2449a2efe04SVictor Chong if (j < num_pkts) { 2459a2efe04SVictor Chong EMSG("Packets requested %zu, received %zu", 2469a2efe04SVictor Chong num_pkts, j); 2479a2efe04SVictor Chong return SPI_ERR_PKTCNT; 2489a2efe04SVictor Chong } 2499a2efe04SVictor Chong } 2509a2efe04SVictor Chong 2519a2efe04SVictor Chong return SPI_OK; 252aca1545dSVictor Chong } 253aca1545dSVictor Chong 254aca1545dSVictor Chong static void pl022_print_peri_id(struct pl022_data *pd __maybe_unused) 255aca1545dSVictor Chong { 256aca1545dSVictor Chong DMSG("Expected: 0x 22 10 ?4 00"); 257aca1545dSVictor Chong DMSG("Read: 0x %02x %02x %02x %02x", 258aca1545dSVictor Chong read32(pd->base + SSPPeriphID0), 259aca1545dSVictor Chong read32(pd->base + SSPPeriphID1), 260aca1545dSVictor Chong read32(pd->base + SSPPeriphID2), 261aca1545dSVictor Chong read32(pd->base + SSPPeriphID3)); 262aca1545dSVictor Chong } 263aca1545dSVictor Chong 264aca1545dSVictor Chong static void pl022_print_cell_id(struct pl022_data *pd __maybe_unused) 265aca1545dSVictor Chong { 266aca1545dSVictor Chong DMSG("Expected: 0x 0d f0 05 b1"); 267aca1545dSVictor Chong DMSG("Read: 0x %02x %02x %02x %02x", 268aca1545dSVictor Chong read32(pd->base + SSPPCellID0), 269aca1545dSVictor Chong read32(pd->base + SSPPCellID1), 270aca1545dSVictor Chong read32(pd->base + SSPPCellID2), 271aca1545dSVictor Chong read32(pd->base + SSPPCellID3)); 272aca1545dSVictor Chong } 273aca1545dSVictor Chong 274aca1545dSVictor Chong static void pl022_sanity_check(struct pl022_data *pd) 275aca1545dSVictor Chong { 276aca1545dSVictor Chong assert(pd); 277aca1545dSVictor Chong assert(pd->chip.ops); 27826128b8fSVictor Chong assert(pd->cs_control <= PL022_CS_CTRL_MANUAL); 27926128b8fSVictor Chong switch (pd->cs_control) { 28026128b8fSVictor Chong case PL022_CS_CTRL_AUTO_GPIO: 28126128b8fSVictor Chong assert(pd->cs_data.gpio_data.chip); 28226128b8fSVictor Chong assert(pd->cs_data.gpio_data.chip->ops); 28326128b8fSVictor Chong break; 28426128b8fSVictor Chong case PL022_CS_CTRL_CB: 28526128b8fSVictor Chong assert(pd->cs_data.cs_cb); 28626128b8fSVictor Chong break; 28726128b8fSVictor Chong default: 28826128b8fSVictor Chong break; 28926128b8fSVictor Chong } 290aca1545dSVictor Chong assert(pd->clk_hz); 291aca1545dSVictor Chong assert(pd->speed_hz && pd->speed_hz <= pd->clk_hz/2); 292aca1545dSVictor Chong assert(pd->mode <= SPI_MODE3); 293aca1545dSVictor Chong assert(pd->data_size_bits == 8 || pd->data_size_bits == 16); 294aca1545dSVictor Chong 295aca1545dSVictor Chong #ifdef PLATFORM_hikey 296aca1545dSVictor Chong DMSG("SSPB2BTRANS: Expected: 0x2. Read: 0x%x", 297aca1545dSVictor Chong read32(pd->base + SSPB2BTRANS)); 298aca1545dSVictor Chong #endif 299aca1545dSVictor Chong pl022_print_peri_id(pd); 300aca1545dSVictor Chong pl022_print_cell_id(pd); 301aca1545dSVictor Chong } 302aca1545dSVictor Chong 303aca1545dSVictor Chong static inline uint32_t pl022_calc_freq(struct pl022_data *pd, 304aca1545dSVictor Chong uint8_t cpsdvr, uint8_t scr) 305aca1545dSVictor Chong { 306aca1545dSVictor Chong return pd->clk_hz / (cpsdvr * (1 + scr)); 307aca1545dSVictor Chong } 308aca1545dSVictor Chong 30926128b8fSVictor Chong static void pl022_control_cs(struct spi_chip *chip, enum gpio_level value) 31026128b8fSVictor Chong { 31126128b8fSVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 31226128b8fSVictor Chong 31326128b8fSVictor Chong switch (pd->cs_control) { 31426128b8fSVictor Chong case PL022_CS_CTRL_AUTO_GPIO: 31526128b8fSVictor Chong if (read8(pd->base + SSPSR) & SSPSR_BSY) 31626128b8fSVictor Chong DMSG("pl022 busy - do NOT set CS!"); 31726128b8fSVictor Chong while (read8(pd->base + SSPSR) & SSPSR_BSY) 31826128b8fSVictor Chong ; 31926128b8fSVictor Chong DMSG("pl022 done - set CS!"); 32026128b8fSVictor Chong 32126128b8fSVictor Chong pd->cs_data.gpio_data.chip->ops->set_value( 32226128b8fSVictor Chong pd->cs_data.gpio_data.pin_num, value); 32326128b8fSVictor Chong break; 32426128b8fSVictor Chong case PL022_CS_CTRL_CB: 32526128b8fSVictor Chong pd->cs_data.cs_cb(value); 32626128b8fSVictor Chong break; 32726128b8fSVictor Chong default: 32826128b8fSVictor Chong break; 32926128b8fSVictor Chong } 33026128b8fSVictor Chong } 33126128b8fSVictor Chong 332aca1545dSVictor Chong static void pl022_calc_clk_divisors(struct pl022_data *pd, 333aca1545dSVictor Chong uint8_t *cpsdvr, uint8_t *scr) 334aca1545dSVictor Chong { 335aca1545dSVictor Chong unsigned int freq1 = 0; 336aca1545dSVictor Chong unsigned int freq2 = 0; 337aca1545dSVictor Chong uint8_t tmp_cpsdvr1; 338aca1545dSVictor Chong uint8_t tmp_scr1; 339aca1545dSVictor Chong uint8_t tmp_cpsdvr2 = 0; 340aca1545dSVictor Chong uint8_t tmp_scr2 = 0; 341aca1545dSVictor Chong 342aca1545dSVictor Chong for (tmp_scr1 = SSP_SCR_MIN; tmp_scr1 < SSP_SCR_MAX; tmp_scr1++) { 343aca1545dSVictor Chong for (tmp_cpsdvr1 = SSP_CPSDVR_MIN; tmp_cpsdvr1 < SSP_CPSDVR_MAX; 344aca1545dSVictor Chong tmp_cpsdvr1++) { 345aca1545dSVictor Chong freq1 = pl022_calc_freq(pd, tmp_cpsdvr1, tmp_scr1); 346aca1545dSVictor Chong if (freq1 == pd->speed_hz) 347aca1545dSVictor Chong goto done; 348aca1545dSVictor Chong else if (freq1 < pd->speed_hz) 349aca1545dSVictor Chong goto stage2; 350aca1545dSVictor Chong } 351aca1545dSVictor Chong } 352aca1545dSVictor Chong 353aca1545dSVictor Chong stage2: 354aca1545dSVictor Chong for (tmp_cpsdvr2 = SSP_CPSDVR_MIN; tmp_cpsdvr2 < SSP_CPSDVR_MAX; 355aca1545dSVictor Chong tmp_cpsdvr2++) { 356aca1545dSVictor Chong for (tmp_scr2 = SSP_SCR_MIN; tmp_scr2 < SSP_SCR_MAX; 357aca1545dSVictor Chong tmp_scr2++) { 358aca1545dSVictor Chong freq2 = pl022_calc_freq(pd, tmp_cpsdvr2, tmp_scr2); 359aca1545dSVictor Chong if (freq2 <= pd->speed_hz) 360aca1545dSVictor Chong goto done; 361aca1545dSVictor Chong } 362aca1545dSVictor Chong } 363aca1545dSVictor Chong 364aca1545dSVictor Chong done: 365aca1545dSVictor Chong if (freq1 >= freq2) { 366aca1545dSVictor Chong *cpsdvr = tmp_cpsdvr1; 367aca1545dSVictor Chong *scr = tmp_scr1; 368aca1545dSVictor Chong DMSG("speed: requested: %u, closest1: %u", 369aca1545dSVictor Chong pd->speed_hz, freq1); 370aca1545dSVictor Chong } else { 371aca1545dSVictor Chong *cpsdvr = tmp_cpsdvr2; 372aca1545dSVictor Chong *scr = tmp_scr2; 373aca1545dSVictor Chong DMSG("speed: requested: %u, closest2: %u", 374aca1545dSVictor Chong pd->speed_hz, freq2); 375aca1545dSVictor Chong } 376aca1545dSVictor Chong DMSG("CPSDVR: %u (0x%x), SCR: %u (0x%x)", 377aca1545dSVictor Chong *cpsdvr, *cpsdvr, *scr, *scr); 378aca1545dSVictor Chong } 379aca1545dSVictor Chong 380aca1545dSVictor Chong static void pl022_flush_fifo(struct pl022_data *pd) 381aca1545dSVictor Chong { 382aca1545dSVictor Chong uint32_t __maybe_unused rdat; 383aca1545dSVictor Chong 384aca1545dSVictor Chong do { 385aca1545dSVictor Chong while (read32(pd->base + SSPSR) & SSPSR_RNE) { 386aca1545dSVictor Chong rdat = read32(pd->base + SSPDR); 387aca1545dSVictor Chong DMSG("rdat: 0x%x", rdat); 388aca1545dSVictor Chong } 389aca1545dSVictor Chong } while (read32(pd->base + SSPSR) & SSPSR_BSY); 390aca1545dSVictor Chong } 391aca1545dSVictor Chong 3926356eeb2SVictor Chong static void pl022_configure(struct spi_chip *chip) 393aca1545dSVictor Chong { 394aca1545dSVictor Chong uint16_t mode; 395aca1545dSVictor Chong uint16_t data_size; 396aca1545dSVictor Chong uint8_t cpsdvr; 397aca1545dSVictor Chong uint8_t scr; 398aca1545dSVictor Chong uint8_t lbm; 3996356eeb2SVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 400aca1545dSVictor Chong 401aca1545dSVictor Chong pl022_sanity_check(pd); 40226128b8fSVictor Chong 40326128b8fSVictor Chong switch (pd->cs_control) { 40426128b8fSVictor Chong case PL022_CS_CTRL_AUTO_GPIO: 40526128b8fSVictor Chong DMSG("Use auto GPIO CS control"); 40626128b8fSVictor Chong DMSG("Mask/disable interrupt for CS GPIO"); 40726128b8fSVictor Chong pd->cs_data.gpio_data.chip->ops->set_interrupt( 40826128b8fSVictor Chong pd->cs_data.gpio_data.pin_num, 40926128b8fSVictor Chong GPIO_INTERRUPT_DISABLE); 41026128b8fSVictor Chong DMSG("Set CS GPIO dir to out"); 41126128b8fSVictor Chong pd->cs_data.gpio_data.chip->ops->set_direction( 41226128b8fSVictor Chong pd->cs_data.gpio_data.pin_num, 41326128b8fSVictor Chong GPIO_DIR_OUT); 41426128b8fSVictor Chong break; 41526128b8fSVictor Chong case PL022_CS_CTRL_CB: 41626128b8fSVictor Chong DMSG("Use registered CS callback"); 41726128b8fSVictor Chong break; 41826128b8fSVictor Chong case PL022_CS_CTRL_MANUAL: 41926128b8fSVictor Chong DMSG("Use manual CS control"); 42026128b8fSVictor Chong break; 42126128b8fSVictor Chong default: 42226128b8fSVictor Chong EMSG("Invalid CS control type: %d", pd->cs_control); 42326128b8fSVictor Chong panic(); 42426128b8fSVictor Chong } 42526128b8fSVictor Chong 42626128b8fSVictor Chong DMSG("Pull CS high"); 42726128b8fSVictor Chong pl022_control_cs(chip, GPIO_LEVEL_HIGH); 42826128b8fSVictor Chong 429aca1545dSVictor Chong pl022_calc_clk_divisors(pd, &cpsdvr, &scr); 430aca1545dSVictor Chong 431aca1545dSVictor Chong /* configure ssp based on platform settings */ 432aca1545dSVictor Chong switch (pd->mode) { 433aca1545dSVictor Chong case SPI_MODE0: 4342ff86f60SVictor Chong DMSG("SPI mode 0"); 4352ff86f60SVictor Chong mode = SSPCR0_SPO0 | SSPCR0_SPH0; 436aca1545dSVictor Chong break; 437aca1545dSVictor Chong case SPI_MODE1: 4382ff86f60SVictor Chong DMSG("SPI mode 1"); 4392ff86f60SVictor Chong mode = SSPCR0_SPO0 | SSPCR0_SPH1; 440aca1545dSVictor Chong break; 441aca1545dSVictor Chong case SPI_MODE2: 4422ff86f60SVictor Chong DMSG("SPI mode 2"); 4432ff86f60SVictor Chong mode = SSPCR0_SPO1 | SSPCR0_SPH0; 444aca1545dSVictor Chong break; 445aca1545dSVictor Chong case SPI_MODE3: 4462ff86f60SVictor Chong DMSG("SPI mode 3"); 4472ff86f60SVictor Chong mode = SSPCR0_SPO1 | SSPCR0_SPH1; 448aca1545dSVictor Chong break; 449aca1545dSVictor Chong default: 450aca1545dSVictor Chong EMSG("Invalid SPI mode: %u", pd->mode); 451aca1545dSVictor Chong panic(); 452aca1545dSVictor Chong } 453aca1545dSVictor Chong 454aca1545dSVictor Chong switch (pd->data_size_bits) { 455aca1545dSVictor Chong case 8: 456aca1545dSVictor Chong DMSG("Data size: 8"); 4572ff86f60SVictor Chong data_size = SSPCR0_DSS_8BIT; 458aca1545dSVictor Chong break; 459aca1545dSVictor Chong case 16: 460aca1545dSVictor Chong DMSG("Data size: 16"); 4612ff86f60SVictor Chong data_size = SSPCR0_DSS_16BIT; 462aca1545dSVictor Chong break; 463aca1545dSVictor Chong default: 464aca1545dSVictor Chong EMSG("Unsupported data size: %u bits", pd->data_size_bits); 465aca1545dSVictor Chong panic(); 466aca1545dSVictor Chong } 467aca1545dSVictor Chong 468aca1545dSVictor Chong if (pd->loopback) { 469aca1545dSVictor Chong DMSG("Starting in loopback mode!"); 470aca1545dSVictor Chong lbm = SSPCR1_LBM_YES; 471aca1545dSVictor Chong } else { 472aca1545dSVictor Chong DMSG("Starting in regular (non-loopback) mode!"); 473aca1545dSVictor Chong lbm = SSPCR1_LBM_NO; 474aca1545dSVictor Chong } 475aca1545dSVictor Chong 47626128b8fSVictor Chong DMSG("Set Serial Clock Rate (SCR), SPI mode (phase and clock)"); 47726128b8fSVictor Chong DMSG("Set frame format (SPI) and data size (8- or 16-bit)"); 478aca1545dSVictor Chong io_mask16(pd->base + SSPCR0, SHIFT_U32(scr, 8) | mode | SSPCR0_FRF_SPI | 479aca1545dSVictor Chong data_size, MASK_16); 480aca1545dSVictor Chong 48126128b8fSVictor Chong DMSG("Set master mode, disable SSP, set loopback mode"); 482aca1545dSVictor Chong io_mask8(pd->base + SSPCR1, SSPCR1_SOD_DISABLE | SSPCR1_MS_MASTER | 483aca1545dSVictor Chong SSPCR1_SSE_DISABLE | lbm, MASK_4); 484aca1545dSVictor Chong 48526128b8fSVictor Chong DMSG("Set clock prescale"); 486aca1545dSVictor Chong io_mask8(pd->base + SSPCPSR, cpsdvr, SSPCPSR_CPSDVR); 487aca1545dSVictor Chong 48826128b8fSVictor Chong DMSG("Disable interrupts"); 489aca1545dSVictor Chong io_mask8(pd->base + SSPIMSC, 0, MASK_4); 490aca1545dSVictor Chong 49126128b8fSVictor Chong DMSG("Clear interrupts"); 4929a2efe04SVictor Chong io_mask8(pd->base + SSPICR, SSPICR_RORIC | SSPICR_RTIC, 4939a2efe04SVictor Chong SSPICR_RORIC | SSPICR_RTIC); 4949a2efe04SVictor Chong 49526128b8fSVictor Chong DMSG("Empty FIFO before starting"); 4966356eeb2SVictor Chong pl022_flush_fifo(pd); 497aca1545dSVictor Chong } 498aca1545dSVictor Chong 4996356eeb2SVictor Chong static void pl022_start(struct spi_chip *chip) 500aca1545dSVictor Chong { 5016356eeb2SVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 502aca1545dSVictor Chong 50326128b8fSVictor Chong DMSG("Enable SSP"); 504aca1545dSVictor Chong io_mask8(pd->base + SSPCR1, SSPCR1_SSE_ENABLE, SSPCR1_SSE); 50526128b8fSVictor Chong 50626128b8fSVictor Chong pl022_control_cs(chip, GPIO_LEVEL_LOW); 507aca1545dSVictor Chong } 508aca1545dSVictor Chong 5096356eeb2SVictor Chong static void pl022_end(struct spi_chip *chip) 510aca1545dSVictor Chong { 5116356eeb2SVictor Chong struct pl022_data *pd = container_of(chip, struct pl022_data, chip); 5126356eeb2SVictor Chong 51326128b8fSVictor Chong pl022_control_cs(chip, GPIO_LEVEL_HIGH); 51426128b8fSVictor Chong 51526128b8fSVictor Chong DMSG("Disable SSP"); 516aca1545dSVictor Chong io_mask8(pd->base + SSPCR1, SSPCR1_SSE_DISABLE, SSPCR1_SSE); 517aca1545dSVictor Chong } 518aca1545dSVictor Chong 5196356eeb2SVictor Chong static const struct spi_ops pl022_ops = { 5206356eeb2SVictor Chong .configure = pl022_configure, 5216356eeb2SVictor Chong .start = pl022_start, 5226356eeb2SVictor Chong .txrx8 = pl022_txrx8, 5236356eeb2SVictor Chong .txrx16 = pl022_txrx16, 5246356eeb2SVictor Chong .end = pl022_end, 5256356eeb2SVictor Chong }; 5266356eeb2SVictor Chong 5276356eeb2SVictor Chong void pl022_init(struct pl022_data *pd) 5286356eeb2SVictor Chong { 5296356eeb2SVictor Chong assert(pd); 5306356eeb2SVictor Chong pd->chip.ops = &pl022_ops; 5316356eeb2SVictor Chong } 532