1ca632f55SGrant Likely /* 2ca632f55SGrant Likely * A driver for the ARM PL022 PrimeCell SSP/SPI bus master. 3ca632f55SGrant Likely * 4aeef9915SLinus Walleij * Copyright (C) 2008-2012 ST-Ericsson AB 5ca632f55SGrant Likely * Copyright (C) 2006 STMicroelectronics Pvt. Ltd. 6ca632f55SGrant Likely * 7ca632f55SGrant Likely * Author: Linus Walleij <linus.walleij@stericsson.com> 8ca632f55SGrant Likely * 9ca632f55SGrant Likely * Initial version inspired by: 10ca632f55SGrant Likely * linux-2.6.17-rc3-mm1/drivers/spi/pxa2xx_spi.c 11ca632f55SGrant Likely * Initial adoption to PL022 by: 12ca632f55SGrant Likely * Sachin Verma <sachin.verma@st.com> 13ca632f55SGrant Likely * 14ca632f55SGrant Likely * This program is free software; you can redistribute it and/or modify 15ca632f55SGrant Likely * it under the terms of the GNU General Public License as published by 16ca632f55SGrant Likely * the Free Software Foundation; either version 2 of the License, or 17ca632f55SGrant Likely * (at your option) any later version. 18ca632f55SGrant Likely * 19ca632f55SGrant Likely * This program is distributed in the hope that it will be useful, 20ca632f55SGrant Likely * but WITHOUT ANY WARRANTY; without even the implied warranty of 21ca632f55SGrant Likely * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22ca632f55SGrant Likely * GNU General Public License for more details. 23ca632f55SGrant Likely */ 24ca632f55SGrant Likely 25ca632f55SGrant Likely #include <linux/init.h> 26ca632f55SGrant Likely #include <linux/module.h> 27ca632f55SGrant Likely #include <linux/device.h> 28ca632f55SGrant Likely #include <linux/ioport.h> 29ca632f55SGrant Likely #include <linux/errno.h> 30ca632f55SGrant Likely #include <linux/interrupt.h> 31ca632f55SGrant Likely #include <linux/spi/spi.h> 32ca632f55SGrant Likely #include <linux/delay.h> 33ca632f55SGrant Likely #include <linux/clk.h> 34ca632f55SGrant Likely #include <linux/err.h> 35ca632f55SGrant Likely #include <linux/amba/bus.h> 36ca632f55SGrant Likely #include <linux/amba/pl022.h> 37ca632f55SGrant Likely #include <linux/io.h> 38ca632f55SGrant Likely #include <linux/slab.h> 39ca632f55SGrant Likely #include <linux/dmaengine.h> 40ca632f55SGrant Likely #include <linux/dma-mapping.h> 41ca632f55SGrant Likely #include <linux/scatterlist.h> 42bcda6ff8SRabin Vincent #include <linux/pm_runtime.h> 43f6f46de1SRoland Stigge #include <linux/gpio.h> 446d3952a7SRoland Stigge #include <linux/of_gpio.h> 454f5e1b37SPatrice Chotard #include <linux/pinctrl/consumer.h> 46ca632f55SGrant Likely 47ca632f55SGrant Likely /* 48ca632f55SGrant Likely * This macro is used to define some register default values. 49ca632f55SGrant Likely * reg is masked with mask, the OR:ed with an (again masked) 50ca632f55SGrant Likely * val shifted sb steps to the left. 51ca632f55SGrant Likely */ 52ca632f55SGrant Likely #define SSP_WRITE_BITS(reg, val, mask, sb) \ 53ca632f55SGrant Likely ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask)))) 54ca632f55SGrant Likely 55ca632f55SGrant Likely /* 56ca632f55SGrant Likely * This macro is also used to define some default values. 57ca632f55SGrant Likely * It will just shift val by sb steps to the left and mask 58ca632f55SGrant Likely * the result with mask. 59ca632f55SGrant Likely */ 60ca632f55SGrant Likely #define GEN_MASK_BITS(val, mask, sb) \ 61ca632f55SGrant Likely (((val)<<(sb)) & (mask)) 62ca632f55SGrant Likely 63ca632f55SGrant Likely #define DRIVE_TX 0 64ca632f55SGrant Likely #define DO_NOT_DRIVE_TX 1 65ca632f55SGrant Likely 66ca632f55SGrant Likely #define DO_NOT_QUEUE_DMA 0 67ca632f55SGrant Likely #define QUEUE_DMA 1 68ca632f55SGrant Likely 69ca632f55SGrant Likely #define RX_TRANSFER 1 70ca632f55SGrant Likely #define TX_TRANSFER 2 71ca632f55SGrant Likely 72ca632f55SGrant Likely /* 73ca632f55SGrant Likely * Macros to access SSP Registers with their offsets 74ca632f55SGrant Likely */ 75ca632f55SGrant Likely #define SSP_CR0(r) (r + 0x000) 76ca632f55SGrant Likely #define SSP_CR1(r) (r + 0x004) 77ca632f55SGrant Likely #define SSP_DR(r) (r + 0x008) 78ca632f55SGrant Likely #define SSP_SR(r) (r + 0x00C) 79ca632f55SGrant Likely #define SSP_CPSR(r) (r + 0x010) 80ca632f55SGrant Likely #define SSP_IMSC(r) (r + 0x014) 81ca632f55SGrant Likely #define SSP_RIS(r) (r + 0x018) 82ca632f55SGrant Likely #define SSP_MIS(r) (r + 0x01C) 83ca632f55SGrant Likely #define SSP_ICR(r) (r + 0x020) 84ca632f55SGrant Likely #define SSP_DMACR(r) (r + 0x024) 85db4fa45eSAnders Berg #define SSP_CSR(r) (r + 0x030) /* vendor extension */ 86ca632f55SGrant Likely #define SSP_ITCR(r) (r + 0x080) 87ca632f55SGrant Likely #define SSP_ITIP(r) (r + 0x084) 88ca632f55SGrant Likely #define SSP_ITOP(r) (r + 0x088) 89ca632f55SGrant Likely #define SSP_TDR(r) (r + 0x08C) 90ca632f55SGrant Likely 91ca632f55SGrant Likely #define SSP_PID0(r) (r + 0xFE0) 92ca632f55SGrant Likely #define SSP_PID1(r) (r + 0xFE4) 93ca632f55SGrant Likely #define SSP_PID2(r) (r + 0xFE8) 94ca632f55SGrant Likely #define SSP_PID3(r) (r + 0xFEC) 95ca632f55SGrant Likely 96ca632f55SGrant Likely #define SSP_CID0(r) (r + 0xFF0) 97ca632f55SGrant Likely #define SSP_CID1(r) (r + 0xFF4) 98ca632f55SGrant Likely #define SSP_CID2(r) (r + 0xFF8) 99ca632f55SGrant Likely #define SSP_CID3(r) (r + 0xFFC) 100ca632f55SGrant Likely 101ca632f55SGrant Likely /* 102ca632f55SGrant Likely * SSP Control Register 0 - SSP_CR0 103ca632f55SGrant Likely */ 104ca632f55SGrant Likely #define SSP_CR0_MASK_DSS (0x0FUL << 0) 105ca632f55SGrant Likely #define SSP_CR0_MASK_FRF (0x3UL << 4) 106ca632f55SGrant Likely #define SSP_CR0_MASK_SPO (0x1UL << 6) 107ca632f55SGrant Likely #define SSP_CR0_MASK_SPH (0x1UL << 7) 108ca632f55SGrant Likely #define SSP_CR0_MASK_SCR (0xFFUL << 8) 109ca632f55SGrant Likely 110ca632f55SGrant Likely /* 111ca632f55SGrant Likely * The ST version of this block moves som bits 112ca632f55SGrant Likely * in SSP_CR0 and extends it to 32 bits 113ca632f55SGrant Likely */ 114ca632f55SGrant Likely #define SSP_CR0_MASK_DSS_ST (0x1FUL << 0) 115ca632f55SGrant Likely #define SSP_CR0_MASK_HALFDUP_ST (0x1UL << 5) 116ca632f55SGrant Likely #define SSP_CR0_MASK_CSS_ST (0x1FUL << 16) 117ca632f55SGrant Likely #define SSP_CR0_MASK_FRF_ST (0x3UL << 21) 118ca632f55SGrant Likely 119ca632f55SGrant Likely /* 120ca632f55SGrant Likely * SSP Control Register 0 - SSP_CR1 121ca632f55SGrant Likely */ 122ca632f55SGrant Likely #define SSP_CR1_MASK_LBM (0x1UL << 0) 123ca632f55SGrant Likely #define SSP_CR1_MASK_SSE (0x1UL << 1) 124ca632f55SGrant Likely #define SSP_CR1_MASK_MS (0x1UL << 2) 125ca632f55SGrant Likely #define SSP_CR1_MASK_SOD (0x1UL << 3) 126ca632f55SGrant Likely 127ca632f55SGrant Likely /* 128ca632f55SGrant Likely * The ST version of this block adds some bits 129ca632f55SGrant Likely * in SSP_CR1 130ca632f55SGrant Likely */ 131ca632f55SGrant Likely #define SSP_CR1_MASK_RENDN_ST (0x1UL << 4) 132ca632f55SGrant Likely #define SSP_CR1_MASK_TENDN_ST (0x1UL << 5) 133ca632f55SGrant Likely #define SSP_CR1_MASK_MWAIT_ST (0x1UL << 6) 134ca632f55SGrant Likely #define SSP_CR1_MASK_RXIFLSEL_ST (0x7UL << 7) 135ca632f55SGrant Likely #define SSP_CR1_MASK_TXIFLSEL_ST (0x7UL << 10) 136ca632f55SGrant Likely /* This one is only in the PL023 variant */ 137ca632f55SGrant Likely #define SSP_CR1_MASK_FBCLKDEL_ST (0x7UL << 13) 138ca632f55SGrant Likely 139ca632f55SGrant Likely /* 140ca632f55SGrant Likely * SSP Status Register - SSP_SR 141ca632f55SGrant Likely */ 142ca632f55SGrant Likely #define SSP_SR_MASK_TFE (0x1UL << 0) /* Transmit FIFO empty */ 143ca632f55SGrant Likely #define SSP_SR_MASK_TNF (0x1UL << 1) /* Transmit FIFO not full */ 144ca632f55SGrant Likely #define SSP_SR_MASK_RNE (0x1UL << 2) /* Receive FIFO not empty */ 145ca632f55SGrant Likely #define SSP_SR_MASK_RFF (0x1UL << 3) /* Receive FIFO full */ 146ca632f55SGrant Likely #define SSP_SR_MASK_BSY (0x1UL << 4) /* Busy Flag */ 147ca632f55SGrant Likely 148ca632f55SGrant Likely /* 149ca632f55SGrant Likely * SSP Clock Prescale Register - SSP_CPSR 150ca632f55SGrant Likely */ 151ca632f55SGrant Likely #define SSP_CPSR_MASK_CPSDVSR (0xFFUL << 0) 152ca632f55SGrant Likely 153ca632f55SGrant Likely /* 154ca632f55SGrant Likely * SSP Interrupt Mask Set/Clear Register - SSP_IMSC 155ca632f55SGrant Likely */ 156ca632f55SGrant Likely #define SSP_IMSC_MASK_RORIM (0x1UL << 0) /* Receive Overrun Interrupt mask */ 157ca632f55SGrant Likely #define SSP_IMSC_MASK_RTIM (0x1UL << 1) /* Receive timeout Interrupt mask */ 158ca632f55SGrant Likely #define SSP_IMSC_MASK_RXIM (0x1UL << 2) /* Receive FIFO Interrupt mask */ 159ca632f55SGrant Likely #define SSP_IMSC_MASK_TXIM (0x1UL << 3) /* Transmit FIFO Interrupt mask */ 160ca632f55SGrant Likely 161ca632f55SGrant Likely /* 162ca632f55SGrant Likely * SSP Raw Interrupt Status Register - SSP_RIS 163ca632f55SGrant Likely */ 164ca632f55SGrant Likely /* Receive Overrun Raw Interrupt status */ 165ca632f55SGrant Likely #define SSP_RIS_MASK_RORRIS (0x1UL << 0) 166ca632f55SGrant Likely /* Receive Timeout Raw Interrupt status */ 167ca632f55SGrant Likely #define SSP_RIS_MASK_RTRIS (0x1UL << 1) 168ca632f55SGrant Likely /* Receive FIFO Raw Interrupt status */ 169ca632f55SGrant Likely #define SSP_RIS_MASK_RXRIS (0x1UL << 2) 170ca632f55SGrant Likely /* Transmit FIFO Raw Interrupt status */ 171ca632f55SGrant Likely #define SSP_RIS_MASK_TXRIS (0x1UL << 3) 172ca632f55SGrant Likely 173ca632f55SGrant Likely /* 174ca632f55SGrant Likely * SSP Masked Interrupt Status Register - SSP_MIS 175ca632f55SGrant Likely */ 176ca632f55SGrant Likely /* Receive Overrun Masked Interrupt status */ 177ca632f55SGrant Likely #define SSP_MIS_MASK_RORMIS (0x1UL << 0) 178ca632f55SGrant Likely /* Receive Timeout Masked Interrupt status */ 179ca632f55SGrant Likely #define SSP_MIS_MASK_RTMIS (0x1UL << 1) 180ca632f55SGrant Likely /* Receive FIFO Masked Interrupt status */ 181ca632f55SGrant Likely #define SSP_MIS_MASK_RXMIS (0x1UL << 2) 182ca632f55SGrant Likely /* Transmit FIFO Masked Interrupt status */ 183ca632f55SGrant Likely #define SSP_MIS_MASK_TXMIS (0x1UL << 3) 184ca632f55SGrant Likely 185ca632f55SGrant Likely /* 186ca632f55SGrant Likely * SSP Interrupt Clear Register - SSP_ICR 187ca632f55SGrant Likely */ 188ca632f55SGrant Likely /* Receive Overrun Raw Clear Interrupt bit */ 189ca632f55SGrant Likely #define SSP_ICR_MASK_RORIC (0x1UL << 0) 190ca632f55SGrant Likely /* Receive Timeout Clear Interrupt bit */ 191ca632f55SGrant Likely #define SSP_ICR_MASK_RTIC (0x1UL << 1) 192ca632f55SGrant Likely 193ca632f55SGrant Likely /* 194ca632f55SGrant Likely * SSP DMA Control Register - SSP_DMACR 195ca632f55SGrant Likely */ 196ca632f55SGrant Likely /* Receive DMA Enable bit */ 197ca632f55SGrant Likely #define SSP_DMACR_MASK_RXDMAE (0x1UL << 0) 198ca632f55SGrant Likely /* Transmit DMA Enable bit */ 199ca632f55SGrant Likely #define SSP_DMACR_MASK_TXDMAE (0x1UL << 1) 200ca632f55SGrant Likely 201ca632f55SGrant Likely /* 202db4fa45eSAnders Berg * SSP Chip Select Control Register - SSP_CSR 203db4fa45eSAnders Berg * (vendor extension) 204db4fa45eSAnders Berg */ 205db4fa45eSAnders Berg #define SSP_CSR_CSVALUE_MASK (0x1FUL << 0) 206db4fa45eSAnders Berg 207db4fa45eSAnders Berg /* 208ca632f55SGrant Likely * SSP Integration Test control Register - SSP_ITCR 209ca632f55SGrant Likely */ 210ca632f55SGrant Likely #define SSP_ITCR_MASK_ITEN (0x1UL << 0) 211ca632f55SGrant Likely #define SSP_ITCR_MASK_TESTFIFO (0x1UL << 1) 212ca632f55SGrant Likely 213ca632f55SGrant Likely /* 214ca632f55SGrant Likely * SSP Integration Test Input Register - SSP_ITIP 215ca632f55SGrant Likely */ 216ca632f55SGrant Likely #define ITIP_MASK_SSPRXD (0x1UL << 0) 217ca632f55SGrant Likely #define ITIP_MASK_SSPFSSIN (0x1UL << 1) 218ca632f55SGrant Likely #define ITIP_MASK_SSPCLKIN (0x1UL << 2) 219ca632f55SGrant Likely #define ITIP_MASK_RXDMAC (0x1UL << 3) 220ca632f55SGrant Likely #define ITIP_MASK_TXDMAC (0x1UL << 4) 221ca632f55SGrant Likely #define ITIP_MASK_SSPTXDIN (0x1UL << 5) 222ca632f55SGrant Likely 223ca632f55SGrant Likely /* 224ca632f55SGrant Likely * SSP Integration Test output Register - SSP_ITOP 225ca632f55SGrant Likely */ 226ca632f55SGrant Likely #define ITOP_MASK_SSPTXD (0x1UL << 0) 227ca632f55SGrant Likely #define ITOP_MASK_SSPFSSOUT (0x1UL << 1) 228ca632f55SGrant Likely #define ITOP_MASK_SSPCLKOUT (0x1UL << 2) 229ca632f55SGrant Likely #define ITOP_MASK_SSPOEn (0x1UL << 3) 230ca632f55SGrant Likely #define ITOP_MASK_SSPCTLOEn (0x1UL << 4) 231ca632f55SGrant Likely #define ITOP_MASK_RORINTR (0x1UL << 5) 232ca632f55SGrant Likely #define ITOP_MASK_RTINTR (0x1UL << 6) 233ca632f55SGrant Likely #define ITOP_MASK_RXINTR (0x1UL << 7) 234ca632f55SGrant Likely #define ITOP_MASK_TXINTR (0x1UL << 8) 235ca632f55SGrant Likely #define ITOP_MASK_INTR (0x1UL << 9) 236ca632f55SGrant Likely #define ITOP_MASK_RXDMABREQ (0x1UL << 10) 237ca632f55SGrant Likely #define ITOP_MASK_RXDMASREQ (0x1UL << 11) 238ca632f55SGrant Likely #define ITOP_MASK_TXDMABREQ (0x1UL << 12) 239ca632f55SGrant Likely #define ITOP_MASK_TXDMASREQ (0x1UL << 13) 240ca632f55SGrant Likely 241ca632f55SGrant Likely /* 242ca632f55SGrant Likely * SSP Test Data Register - SSP_TDR 243ca632f55SGrant Likely */ 244ca632f55SGrant Likely #define TDR_MASK_TESTDATA (0xFFFFFFFF) 245ca632f55SGrant Likely 246ca632f55SGrant Likely /* 247ca632f55SGrant Likely * Message State 248ca632f55SGrant Likely * we use the spi_message.state (void *) pointer to 249ca632f55SGrant Likely * hold a single state value, that's why all this 250ca632f55SGrant Likely * (void *) casting is done here. 251ca632f55SGrant Likely */ 252ca632f55SGrant Likely #define STATE_START ((void *) 0) 253ca632f55SGrant Likely #define STATE_RUNNING ((void *) 1) 254ca632f55SGrant Likely #define STATE_DONE ((void *) 2) 255ca632f55SGrant Likely #define STATE_ERROR ((void *) -1) 256ca632f55SGrant Likely 257ca632f55SGrant Likely /* 258ca632f55SGrant Likely * SSP State - Whether Enabled or Disabled 259ca632f55SGrant Likely */ 260ca632f55SGrant Likely #define SSP_DISABLED (0) 261ca632f55SGrant Likely #define SSP_ENABLED (1) 262ca632f55SGrant Likely 263ca632f55SGrant Likely /* 264ca632f55SGrant Likely * SSP DMA State - Whether DMA Enabled or Disabled 265ca632f55SGrant Likely */ 266ca632f55SGrant Likely #define SSP_DMA_DISABLED (0) 267ca632f55SGrant Likely #define SSP_DMA_ENABLED (1) 268ca632f55SGrant Likely 269ca632f55SGrant Likely /* 270ca632f55SGrant Likely * SSP Clock Defaults 271ca632f55SGrant Likely */ 272ca632f55SGrant Likely #define SSP_DEFAULT_CLKRATE 0x2 273ca632f55SGrant Likely #define SSP_DEFAULT_PRESCALE 0x40 274ca632f55SGrant Likely 275ca632f55SGrant Likely /* 276ca632f55SGrant Likely * SSP Clock Parameter ranges 277ca632f55SGrant Likely */ 278ca632f55SGrant Likely #define CPSDVR_MIN 0x02 279ca632f55SGrant Likely #define CPSDVR_MAX 0xFE 280ca632f55SGrant Likely #define SCR_MIN 0x00 281ca632f55SGrant Likely #define SCR_MAX 0xFF 282ca632f55SGrant Likely 283ca632f55SGrant Likely /* 284ca632f55SGrant Likely * SSP Interrupt related Macros 285ca632f55SGrant Likely */ 286ca632f55SGrant Likely #define DEFAULT_SSP_REG_IMSC 0x0UL 287ca632f55SGrant Likely #define DISABLE_ALL_INTERRUPTS DEFAULT_SSP_REG_IMSC 28885fa4e1fSAlexander Sverdlin #define ENABLE_ALL_INTERRUPTS ( \ 28985fa4e1fSAlexander Sverdlin SSP_IMSC_MASK_RORIM | \ 29085fa4e1fSAlexander Sverdlin SSP_IMSC_MASK_RTIM | \ 29185fa4e1fSAlexander Sverdlin SSP_IMSC_MASK_RXIM | \ 29285fa4e1fSAlexander Sverdlin SSP_IMSC_MASK_TXIM \ 29385fa4e1fSAlexander Sverdlin ) 294ca632f55SGrant Likely 295ca632f55SGrant Likely #define CLEAR_ALL_INTERRUPTS 0x3 296ca632f55SGrant Likely 297ca632f55SGrant Likely #define SPI_POLLING_TIMEOUT 1000 298ca632f55SGrant Likely 299ca632f55SGrant Likely /* 300ca632f55SGrant Likely * The type of reading going on on this chip 301ca632f55SGrant Likely */ 302ca632f55SGrant Likely enum ssp_reading { 303ca632f55SGrant Likely READING_NULL, 304ca632f55SGrant Likely READING_U8, 305ca632f55SGrant Likely READING_U16, 306ca632f55SGrant Likely READING_U32 307ca632f55SGrant Likely }; 308ca632f55SGrant Likely 309ca632f55SGrant Likely /** 310ca632f55SGrant Likely * The type of writing going on on this chip 311ca632f55SGrant Likely */ 312ca632f55SGrant Likely enum ssp_writing { 313ca632f55SGrant Likely WRITING_NULL, 314ca632f55SGrant Likely WRITING_U8, 315ca632f55SGrant Likely WRITING_U16, 316ca632f55SGrant Likely WRITING_U32 317ca632f55SGrant Likely }; 318ca632f55SGrant Likely 319ca632f55SGrant Likely /** 320ca632f55SGrant Likely * struct vendor_data - vendor-specific config parameters 321ca632f55SGrant Likely * for PL022 derivates 322ca632f55SGrant Likely * @fifodepth: depth of FIFOs (both) 323ca632f55SGrant Likely * @max_bpw: maximum number of bits per word 324ca632f55SGrant Likely * @unidir: supports unidirection transfers 325ca632f55SGrant Likely * @extended_cr: 32 bit wide control register 0 with extra 326ca632f55SGrant Likely * features and extra features in CR1 as found in the ST variants 327ca632f55SGrant Likely * @pl023: supports a subset of the ST extensions called "PL023" 328db4fa45eSAnders Berg * @internal_cs_ctrl: supports chip select control register 329ca632f55SGrant Likely */ 330ca632f55SGrant Likely struct vendor_data { 331ca632f55SGrant Likely int fifodepth; 332ca632f55SGrant Likely int max_bpw; 333ca632f55SGrant Likely bool unidir; 334ca632f55SGrant Likely bool extended_cr; 335ca632f55SGrant Likely bool pl023; 336ca632f55SGrant Likely bool loopback; 337db4fa45eSAnders Berg bool internal_cs_ctrl; 338ca632f55SGrant Likely }; 339ca632f55SGrant Likely 340ca632f55SGrant Likely /** 341ca632f55SGrant Likely * struct pl022 - This is the private SSP driver data structure 342ca632f55SGrant Likely * @adev: AMBA device model hookup 343ca632f55SGrant Likely * @vendor: vendor data for the IP block 344ca632f55SGrant Likely * @phybase: the physical memory where the SSP device resides 345ca632f55SGrant Likely * @virtbase: the virtual memory where the SSP is mapped 346ca632f55SGrant Likely * @clk: outgoing clock "SPICLK" for the SPI bus 347ca632f55SGrant Likely * @master: SPI framework hookup 348ca632f55SGrant Likely * @master_info: controller-specific data from machine setup 349ca632f55SGrant Likely * @pump_transfers: Tasklet used in Interrupt Transfer mode 350ca632f55SGrant Likely * @cur_msg: Pointer to current spi_message being processed 351ca632f55SGrant Likely * @cur_transfer: Pointer to current spi_transfer 352ca632f55SGrant Likely * @cur_chip: pointer to current clients chip(assigned from controller_state) 3538b8d7191SVirupax Sadashivpetimath * @next_msg_cs_active: the next message in the queue has been examined 3548b8d7191SVirupax Sadashivpetimath * and it was found that it uses the same chip select as the previous 3558b8d7191SVirupax Sadashivpetimath * message, so we left it active after the previous transfer, and it's 3568b8d7191SVirupax Sadashivpetimath * active already. 357ca632f55SGrant Likely * @tx: current position in TX buffer to be read 358ca632f55SGrant Likely * @tx_end: end position in TX buffer to be read 359ca632f55SGrant Likely * @rx: current position in RX buffer to be written 360ca632f55SGrant Likely * @rx_end: end position in RX buffer to be written 361ca632f55SGrant Likely * @read: the type of read currently going on 362ca632f55SGrant Likely * @write: the type of write currently going on 363ca632f55SGrant Likely * @exp_fifo_level: expected FIFO level 364ca632f55SGrant Likely * @dma_rx_channel: optional channel for RX DMA 365ca632f55SGrant Likely * @dma_tx_channel: optional channel for TX DMA 366ca632f55SGrant Likely * @sgt_rx: scattertable for the RX transfer 367ca632f55SGrant Likely * @sgt_tx: scattertable for the TX transfer 368ca632f55SGrant Likely * @dummypage: a dummy page used for driving data on the bus with DMA 369f6f46de1SRoland Stigge * @cur_cs: current chip select (gpio) 370f6f46de1SRoland Stigge * @chipselects: list of chipselects (gpios) 371ca632f55SGrant Likely */ 372ca632f55SGrant Likely struct pl022 { 373ca632f55SGrant Likely struct amba_device *adev; 374ca632f55SGrant Likely struct vendor_data *vendor; 375ca632f55SGrant Likely resource_size_t phybase; 376ca632f55SGrant Likely void __iomem *virtbase; 377ca632f55SGrant Likely struct clk *clk; 378ca632f55SGrant Likely struct spi_master *master; 379ca632f55SGrant Likely struct pl022_ssp_controller *master_info; 380ffbbdd21SLinus Walleij /* Message per-transfer pump */ 381ca632f55SGrant Likely struct tasklet_struct pump_transfers; 382ca632f55SGrant Likely struct spi_message *cur_msg; 383ca632f55SGrant Likely struct spi_transfer *cur_transfer; 384ca632f55SGrant Likely struct chip_data *cur_chip; 3858b8d7191SVirupax Sadashivpetimath bool next_msg_cs_active; 386ca632f55SGrant Likely void *tx; 387ca632f55SGrant Likely void *tx_end; 388ca632f55SGrant Likely void *rx; 389ca632f55SGrant Likely void *rx_end; 390ca632f55SGrant Likely enum ssp_reading read; 391ca632f55SGrant Likely enum ssp_writing write; 392ca632f55SGrant Likely u32 exp_fifo_level; 393083be3f0SLinus Walleij enum ssp_rx_level_trig rx_lev_trig; 394083be3f0SLinus Walleij enum ssp_tx_level_trig tx_lev_trig; 395ca632f55SGrant Likely /* DMA settings */ 396ca632f55SGrant Likely #ifdef CONFIG_DMA_ENGINE 397ca632f55SGrant Likely struct dma_chan *dma_rx_channel; 398ca632f55SGrant Likely struct dma_chan *dma_tx_channel; 399ca632f55SGrant Likely struct sg_table sgt_rx; 400ca632f55SGrant Likely struct sg_table sgt_tx; 401ca632f55SGrant Likely char *dummypage; 402ffbbdd21SLinus Walleij bool dma_running; 403ca632f55SGrant Likely #endif 404f6f46de1SRoland Stigge int cur_cs; 405f6f46de1SRoland Stigge int *chipselects; 406ca632f55SGrant Likely }; 407ca632f55SGrant Likely 408ca632f55SGrant Likely /** 409ca632f55SGrant Likely * struct chip_data - To maintain runtime state of SSP for each client chip 410ca632f55SGrant Likely * @cr0: Value of control register CR0 of SSP - on later ST variants this 411ca632f55SGrant Likely * register is 32 bits wide rather than just 16 412ca632f55SGrant Likely * @cr1: Value of control register CR1 of SSP 413ca632f55SGrant Likely * @dmacr: Value of DMA control Register of SSP 414ca632f55SGrant Likely * @cpsr: Value of Clock prescale register 415ca632f55SGrant Likely * @n_bytes: how many bytes(power of 2) reqd for a given data width of client 416ca632f55SGrant Likely * @enable_dma: Whether to enable DMA or not 417ca632f55SGrant Likely * @read: function ptr to be used to read when doing xfer for this chip 418ca632f55SGrant Likely * @write: function ptr to be used to write when doing xfer for this chip 419ca632f55SGrant Likely * @cs_control: chip select callback provided by chip 420ca632f55SGrant Likely * @xfer_type: polling/interrupt/DMA 421ca632f55SGrant Likely * 422ca632f55SGrant Likely * Runtime state of the SSP controller, maintained per chip, 423ca632f55SGrant Likely * This would be set according to the current message that would be served 424ca632f55SGrant Likely */ 425ca632f55SGrant Likely struct chip_data { 426ca632f55SGrant Likely u32 cr0; 427ca632f55SGrant Likely u16 cr1; 428ca632f55SGrant Likely u16 dmacr; 429ca632f55SGrant Likely u16 cpsr; 430ca632f55SGrant Likely u8 n_bytes; 431ca632f55SGrant Likely bool enable_dma; 432ca632f55SGrant Likely enum ssp_reading read; 433ca632f55SGrant Likely enum ssp_writing write; 434ca632f55SGrant Likely void (*cs_control) (u32 command); 435ca632f55SGrant Likely int xfer_type; 436ca632f55SGrant Likely }; 437ca632f55SGrant Likely 438ca632f55SGrant Likely /** 439ca632f55SGrant Likely * null_cs_control - Dummy chip select function 440ca632f55SGrant Likely * @command: select/delect the chip 441ca632f55SGrant Likely * 442ca632f55SGrant Likely * If no chip select function is provided by client this is used as dummy 443ca632f55SGrant Likely * chip select 444ca632f55SGrant Likely */ 445ca632f55SGrant Likely static void null_cs_control(u32 command) 446ca632f55SGrant Likely { 447ca632f55SGrant Likely pr_debug("pl022: dummy chip select control, CS=0x%x\n", command); 448ca632f55SGrant Likely } 449ca632f55SGrant Likely 450db4fa45eSAnders Berg /** 451db4fa45eSAnders Berg * internal_cs_control - Control chip select signals via SSP_CSR. 452db4fa45eSAnders Berg * @pl022: SSP driver private data structure 453db4fa45eSAnders Berg * @command: select/delect the chip 454db4fa45eSAnders Berg * 455db4fa45eSAnders Berg * Used on controller with internal chip select control via SSP_CSR register 456db4fa45eSAnders Berg * (vendor extension). Each of the 5 LSB in the register controls one chip 457db4fa45eSAnders Berg * select signal. 458db4fa45eSAnders Berg */ 459db4fa45eSAnders Berg static void internal_cs_control(struct pl022 *pl022, u32 command) 460db4fa45eSAnders Berg { 461db4fa45eSAnders Berg u32 tmp; 462db4fa45eSAnders Berg 463db4fa45eSAnders Berg tmp = readw(SSP_CSR(pl022->virtbase)); 464db4fa45eSAnders Berg if (command == SSP_CHIP_SELECT) 465db4fa45eSAnders Berg tmp &= ~BIT(pl022->cur_cs); 466db4fa45eSAnders Berg else 467db4fa45eSAnders Berg tmp |= BIT(pl022->cur_cs); 468db4fa45eSAnders Berg writew(tmp, SSP_CSR(pl022->virtbase)); 469db4fa45eSAnders Berg } 470db4fa45eSAnders Berg 471f6f46de1SRoland Stigge static void pl022_cs_control(struct pl022 *pl022, u32 command) 472f6f46de1SRoland Stigge { 473db4fa45eSAnders Berg if (pl022->vendor->internal_cs_ctrl) 474db4fa45eSAnders Berg internal_cs_control(pl022, command); 475db4fa45eSAnders Berg else if (gpio_is_valid(pl022->cur_cs)) 476f6f46de1SRoland Stigge gpio_set_value(pl022->cur_cs, command); 477f6f46de1SRoland Stigge else 478f6f46de1SRoland Stigge pl022->cur_chip->cs_control(command); 479f6f46de1SRoland Stigge } 480f6f46de1SRoland Stigge 481ca632f55SGrant Likely /** 482ca632f55SGrant Likely * giveback - current spi_message is over, schedule next message and call 483ca632f55SGrant Likely * callback of this message. Assumes that caller already 484ca632f55SGrant Likely * set message->status; dma and pio irqs are blocked 485ca632f55SGrant Likely * @pl022: SSP driver private data structure 486ca632f55SGrant Likely */ 487ca632f55SGrant Likely static void giveback(struct pl022 *pl022) 488ca632f55SGrant Likely { 489ca632f55SGrant Likely struct spi_transfer *last_transfer; 4908b8d7191SVirupax Sadashivpetimath pl022->next_msg_cs_active = false; 491ca632f55SGrant Likely 49223e2c2aaSAxel Lin last_transfer = list_last_entry(&pl022->cur_msg->transfers, 49323e2c2aaSAxel Lin struct spi_transfer, transfer_list); 494ca632f55SGrant Likely 495ca632f55SGrant Likely /* Delay if requested before any change in chip select */ 496ca632f55SGrant Likely if (last_transfer->delay_usecs) 497ca632f55SGrant Likely /* 498ca632f55SGrant Likely * FIXME: This runs in interrupt context. 499ca632f55SGrant Likely * Is this really smart? 500ca632f55SGrant Likely */ 501ca632f55SGrant Likely udelay(last_transfer->delay_usecs); 502ca632f55SGrant Likely 5038b8d7191SVirupax Sadashivpetimath if (!last_transfer->cs_change) { 504ca632f55SGrant Likely struct spi_message *next_msg; 505ca632f55SGrant Likely 5068b8d7191SVirupax Sadashivpetimath /* 5078b8d7191SVirupax Sadashivpetimath * cs_change was not set. We can keep the chip select 5088b8d7191SVirupax Sadashivpetimath * enabled if there is message in the queue and it is 5098b8d7191SVirupax Sadashivpetimath * for the same spi device. 510ca632f55SGrant Likely * 511ca632f55SGrant Likely * We cannot postpone this until pump_messages, because 512ca632f55SGrant Likely * after calling msg->complete (below) the driver that 513ca632f55SGrant Likely * sent the current message could be unloaded, which 514ca632f55SGrant Likely * could invalidate the cs_control() callback... 515ca632f55SGrant Likely */ 516ca632f55SGrant Likely /* get a pointer to the next message, if any */ 517ffbbdd21SLinus Walleij next_msg = spi_get_next_queued_message(pl022->master); 518ca632f55SGrant Likely 5198b8d7191SVirupax Sadashivpetimath /* 5208b8d7191SVirupax Sadashivpetimath * see if the next and current messages point 5218b8d7191SVirupax Sadashivpetimath * to the same spi device. 522ca632f55SGrant Likely */ 5238b8d7191SVirupax Sadashivpetimath if (next_msg && next_msg->spi != pl022->cur_msg->spi) 524ca632f55SGrant Likely next_msg = NULL; 5258b8d7191SVirupax Sadashivpetimath if (!next_msg || pl022->cur_msg->state == STATE_ERROR) 526f6f46de1SRoland Stigge pl022_cs_control(pl022, SSP_CHIP_DESELECT); 5278b8d7191SVirupax Sadashivpetimath else 5288b8d7191SVirupax Sadashivpetimath pl022->next_msg_cs_active = true; 529ffbbdd21SLinus Walleij 530ca632f55SGrant Likely } 5318b8d7191SVirupax Sadashivpetimath 5328b8d7191SVirupax Sadashivpetimath pl022->cur_msg = NULL; 5338b8d7191SVirupax Sadashivpetimath pl022->cur_transfer = NULL; 5348b8d7191SVirupax Sadashivpetimath pl022->cur_chip = NULL; 535fd316941SVirupax Sadashivpetimath 536fd316941SVirupax Sadashivpetimath /* disable the SPI/SSP operation */ 537fd316941SVirupax Sadashivpetimath writew((readw(SSP_CR1(pl022->virtbase)) & 538fd316941SVirupax Sadashivpetimath (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase)); 539fd316941SVirupax Sadashivpetimath 540cd6fa8d2SAlexander Sverdlin spi_finalize_current_message(pl022->master); 541ca632f55SGrant Likely } 542ca632f55SGrant Likely 543ca632f55SGrant Likely /** 544ca632f55SGrant Likely * flush - flush the FIFO to reach a clean state 545ca632f55SGrant Likely * @pl022: SSP driver private data structure 546ca632f55SGrant Likely */ 547ca632f55SGrant Likely static int flush(struct pl022 *pl022) 548ca632f55SGrant Likely { 549ca632f55SGrant Likely unsigned long limit = loops_per_jiffy << 1; 550ca632f55SGrant Likely 551ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, "flush\n"); 552ca632f55SGrant Likely do { 553ca632f55SGrant Likely while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE) 554ca632f55SGrant Likely readw(SSP_DR(pl022->virtbase)); 555ca632f55SGrant Likely } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--); 556ca632f55SGrant Likely 557ca632f55SGrant Likely pl022->exp_fifo_level = 0; 558ca632f55SGrant Likely 559ca632f55SGrant Likely return limit; 560ca632f55SGrant Likely } 561ca632f55SGrant Likely 562ca632f55SGrant Likely /** 563ca632f55SGrant Likely * restore_state - Load configuration of current chip 564ca632f55SGrant Likely * @pl022: SSP driver private data structure 565ca632f55SGrant Likely */ 566ca632f55SGrant Likely static void restore_state(struct pl022 *pl022) 567ca632f55SGrant Likely { 568ca632f55SGrant Likely struct chip_data *chip = pl022->cur_chip; 569ca632f55SGrant Likely 570ca632f55SGrant Likely if (pl022->vendor->extended_cr) 571ca632f55SGrant Likely writel(chip->cr0, SSP_CR0(pl022->virtbase)); 572ca632f55SGrant Likely else 573ca632f55SGrant Likely writew(chip->cr0, SSP_CR0(pl022->virtbase)); 574ca632f55SGrant Likely writew(chip->cr1, SSP_CR1(pl022->virtbase)); 575ca632f55SGrant Likely writew(chip->dmacr, SSP_DMACR(pl022->virtbase)); 576ca632f55SGrant Likely writew(chip->cpsr, SSP_CPSR(pl022->virtbase)); 577ca632f55SGrant Likely writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase)); 578ca632f55SGrant Likely writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase)); 579ca632f55SGrant Likely } 580ca632f55SGrant Likely 581ca632f55SGrant Likely /* 582ca632f55SGrant Likely * Default SSP Register Values 583ca632f55SGrant Likely */ 584ca632f55SGrant Likely #define DEFAULT_SSP_REG_CR0 ( \ 585ca632f55SGrant Likely GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \ 586ca632f55SGrant Likely GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \ 587ca632f55SGrant Likely GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \ 588ca632f55SGrant Likely GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \ 589ca632f55SGrant Likely GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \ 590ca632f55SGrant Likely ) 591ca632f55SGrant Likely 592ca632f55SGrant Likely /* ST versions have slightly different bit layout */ 593ca632f55SGrant Likely #define DEFAULT_SSP_REG_CR0_ST ( \ 594ca632f55SGrant Likely GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \ 595ca632f55SGrant Likely GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \ 596ca632f55SGrant Likely GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \ 597ca632f55SGrant Likely GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \ 598ca632f55SGrant Likely GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \ 599ca632f55SGrant Likely GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \ 600ca632f55SGrant Likely GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \ 601ca632f55SGrant Likely ) 602ca632f55SGrant Likely 603ca632f55SGrant Likely /* The PL023 version is slightly different again */ 604ca632f55SGrant Likely #define DEFAULT_SSP_REG_CR0_ST_PL023 ( \ 605ca632f55SGrant Likely GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \ 606ca632f55SGrant Likely GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \ 607ca632f55SGrant Likely GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \ 608ca632f55SGrant Likely GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \ 609ca632f55SGrant Likely ) 610ca632f55SGrant Likely 611ca632f55SGrant Likely #define DEFAULT_SSP_REG_CR1 ( \ 612ca632f55SGrant Likely GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \ 613ca632f55SGrant Likely GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \ 614ca632f55SGrant Likely GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \ 615ca632f55SGrant Likely GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \ 616ca632f55SGrant Likely ) 617ca632f55SGrant Likely 618ca632f55SGrant Likely /* ST versions extend this register to use all 16 bits */ 619ca632f55SGrant Likely #define DEFAULT_SSP_REG_CR1_ST ( \ 620ca632f55SGrant Likely DEFAULT_SSP_REG_CR1 | \ 621ca632f55SGrant Likely GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \ 622ca632f55SGrant Likely GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \ 623ca632f55SGrant Likely GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\ 624ca632f55SGrant Likely GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \ 625ca632f55SGrant Likely GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \ 626ca632f55SGrant Likely ) 627ca632f55SGrant Likely 628ca632f55SGrant Likely /* 629ca632f55SGrant Likely * The PL023 variant has further differences: no loopback mode, no microwire 630ca632f55SGrant Likely * support, and a new clock feedback delay setting. 631ca632f55SGrant Likely */ 632ca632f55SGrant Likely #define DEFAULT_SSP_REG_CR1_ST_PL023 ( \ 633ca632f55SGrant Likely GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \ 634ca632f55SGrant Likely GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \ 635ca632f55SGrant Likely GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \ 636ca632f55SGrant Likely GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \ 637ca632f55SGrant Likely GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \ 638ca632f55SGrant Likely GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \ 639ca632f55SGrant Likely GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \ 640ca632f55SGrant Likely GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \ 641ca632f55SGrant Likely ) 642ca632f55SGrant Likely 643ca632f55SGrant Likely #define DEFAULT_SSP_REG_CPSR ( \ 644ca632f55SGrant Likely GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \ 645ca632f55SGrant Likely ) 646ca632f55SGrant Likely 647ca632f55SGrant Likely #define DEFAULT_SSP_REG_DMACR (\ 648ca632f55SGrant Likely GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \ 649ca632f55SGrant Likely GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \ 650ca632f55SGrant Likely ) 651ca632f55SGrant Likely 652ca632f55SGrant Likely /** 653ca632f55SGrant Likely * load_ssp_default_config - Load default configuration for SSP 654ca632f55SGrant Likely * @pl022: SSP driver private data structure 655ca632f55SGrant Likely */ 656ca632f55SGrant Likely static void load_ssp_default_config(struct pl022 *pl022) 657ca632f55SGrant Likely { 658ca632f55SGrant Likely if (pl022->vendor->pl023) { 659ca632f55SGrant Likely writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase)); 660ca632f55SGrant Likely writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase)); 661ca632f55SGrant Likely } else if (pl022->vendor->extended_cr) { 662ca632f55SGrant Likely writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase)); 663ca632f55SGrant Likely writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase)); 664ca632f55SGrant Likely } else { 665ca632f55SGrant Likely writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase)); 666ca632f55SGrant Likely writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase)); 667ca632f55SGrant Likely } 668ca632f55SGrant Likely writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase)); 669ca632f55SGrant Likely writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase)); 670ca632f55SGrant Likely writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase)); 671ca632f55SGrant Likely writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase)); 672ca632f55SGrant Likely } 673ca632f55SGrant Likely 674ca632f55SGrant Likely /** 675ca632f55SGrant Likely * This will write to TX and read from RX according to the parameters 676ca632f55SGrant Likely * set in pl022. 677ca632f55SGrant Likely */ 678ca632f55SGrant Likely static void readwriter(struct pl022 *pl022) 679ca632f55SGrant Likely { 680ca632f55SGrant Likely 681ca632f55SGrant Likely /* 682ca632f55SGrant Likely * The FIFO depth is different between primecell variants. 683ca632f55SGrant Likely * I believe filling in too much in the FIFO might cause 684ca632f55SGrant Likely * errons in 8bit wide transfers on ARM variants (just 8 words 685ca632f55SGrant Likely * FIFO, means only 8x8 = 64 bits in FIFO) at least. 686ca632f55SGrant Likely * 687ca632f55SGrant Likely * To prevent this issue, the TX FIFO is only filled to the 688ca632f55SGrant Likely * unused RX FIFO fill length, regardless of what the TX 689ca632f55SGrant Likely * FIFO status flag indicates. 690ca632f55SGrant Likely */ 691ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, 692ca632f55SGrant Likely "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n", 693ca632f55SGrant Likely __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end); 694ca632f55SGrant Likely 695ca632f55SGrant Likely /* Read as much as you can */ 696ca632f55SGrant Likely while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE) 697ca632f55SGrant Likely && (pl022->rx < pl022->rx_end)) { 698ca632f55SGrant Likely switch (pl022->read) { 699ca632f55SGrant Likely case READING_NULL: 700ca632f55SGrant Likely readw(SSP_DR(pl022->virtbase)); 701ca632f55SGrant Likely break; 702ca632f55SGrant Likely case READING_U8: 703ca632f55SGrant Likely *(u8 *) (pl022->rx) = 704ca632f55SGrant Likely readw(SSP_DR(pl022->virtbase)) & 0xFFU; 705ca632f55SGrant Likely break; 706ca632f55SGrant Likely case READING_U16: 707ca632f55SGrant Likely *(u16 *) (pl022->rx) = 708ca632f55SGrant Likely (u16) readw(SSP_DR(pl022->virtbase)); 709ca632f55SGrant Likely break; 710ca632f55SGrant Likely case READING_U32: 711ca632f55SGrant Likely *(u32 *) (pl022->rx) = 712ca632f55SGrant Likely readl(SSP_DR(pl022->virtbase)); 713ca632f55SGrant Likely break; 714ca632f55SGrant Likely } 715ca632f55SGrant Likely pl022->rx += (pl022->cur_chip->n_bytes); 716ca632f55SGrant Likely pl022->exp_fifo_level--; 717ca632f55SGrant Likely } 718ca632f55SGrant Likely /* 719ca632f55SGrant Likely * Write as much as possible up to the RX FIFO size 720ca632f55SGrant Likely */ 721ca632f55SGrant Likely while ((pl022->exp_fifo_level < pl022->vendor->fifodepth) 722ca632f55SGrant Likely && (pl022->tx < pl022->tx_end)) { 723ca632f55SGrant Likely switch (pl022->write) { 724ca632f55SGrant Likely case WRITING_NULL: 725ca632f55SGrant Likely writew(0x0, SSP_DR(pl022->virtbase)); 726ca632f55SGrant Likely break; 727ca632f55SGrant Likely case WRITING_U8: 728ca632f55SGrant Likely writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase)); 729ca632f55SGrant Likely break; 730ca632f55SGrant Likely case WRITING_U16: 731ca632f55SGrant Likely writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase)); 732ca632f55SGrant Likely break; 733ca632f55SGrant Likely case WRITING_U32: 734ca632f55SGrant Likely writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase)); 735ca632f55SGrant Likely break; 736ca632f55SGrant Likely } 737ca632f55SGrant Likely pl022->tx += (pl022->cur_chip->n_bytes); 738ca632f55SGrant Likely pl022->exp_fifo_level++; 739ca632f55SGrant Likely /* 740ca632f55SGrant Likely * This inner reader takes care of things appearing in the RX 741ca632f55SGrant Likely * FIFO as we're transmitting. This will happen a lot since the 742ca632f55SGrant Likely * clock starts running when you put things into the TX FIFO, 743ca632f55SGrant Likely * and then things are continuously clocked into the RX FIFO. 744ca632f55SGrant Likely */ 745ca632f55SGrant Likely while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE) 746ca632f55SGrant Likely && (pl022->rx < pl022->rx_end)) { 747ca632f55SGrant Likely switch (pl022->read) { 748ca632f55SGrant Likely case READING_NULL: 749ca632f55SGrant Likely readw(SSP_DR(pl022->virtbase)); 750ca632f55SGrant Likely break; 751ca632f55SGrant Likely case READING_U8: 752ca632f55SGrant Likely *(u8 *) (pl022->rx) = 753ca632f55SGrant Likely readw(SSP_DR(pl022->virtbase)) & 0xFFU; 754ca632f55SGrant Likely break; 755ca632f55SGrant Likely case READING_U16: 756ca632f55SGrant Likely *(u16 *) (pl022->rx) = 757ca632f55SGrant Likely (u16) readw(SSP_DR(pl022->virtbase)); 758ca632f55SGrant Likely break; 759ca632f55SGrant Likely case READING_U32: 760ca632f55SGrant Likely *(u32 *) (pl022->rx) = 761ca632f55SGrant Likely readl(SSP_DR(pl022->virtbase)); 762ca632f55SGrant Likely break; 763ca632f55SGrant Likely } 764ca632f55SGrant Likely pl022->rx += (pl022->cur_chip->n_bytes); 765ca632f55SGrant Likely pl022->exp_fifo_level--; 766ca632f55SGrant Likely } 767ca632f55SGrant Likely } 768ca632f55SGrant Likely /* 769ca632f55SGrant Likely * When we exit here the TX FIFO should be full and the RX FIFO 770ca632f55SGrant Likely * should be empty 771ca632f55SGrant Likely */ 772ca632f55SGrant Likely } 773ca632f55SGrant Likely 774ca632f55SGrant Likely /** 775ca632f55SGrant Likely * next_transfer - Move to the Next transfer in the current spi message 776ca632f55SGrant Likely * @pl022: SSP driver private data structure 777ca632f55SGrant Likely * 778ca632f55SGrant Likely * This function moves though the linked list of spi transfers in the 779ca632f55SGrant Likely * current spi message and returns with the state of current spi 780ca632f55SGrant Likely * message i.e whether its last transfer is done(STATE_DONE) or 781ca632f55SGrant Likely * Next transfer is ready(STATE_RUNNING) 782ca632f55SGrant Likely */ 783ca632f55SGrant Likely static void *next_transfer(struct pl022 *pl022) 784ca632f55SGrant Likely { 785ca632f55SGrant Likely struct spi_message *msg = pl022->cur_msg; 786ca632f55SGrant Likely struct spi_transfer *trans = pl022->cur_transfer; 787ca632f55SGrant Likely 788ca632f55SGrant Likely /* Move to next transfer */ 789ca632f55SGrant Likely if (trans->transfer_list.next != &msg->transfers) { 790ca632f55SGrant Likely pl022->cur_transfer = 791ca632f55SGrant Likely list_entry(trans->transfer_list.next, 792ca632f55SGrant Likely struct spi_transfer, transfer_list); 793ca632f55SGrant Likely return STATE_RUNNING; 794ca632f55SGrant Likely } 795ca632f55SGrant Likely return STATE_DONE; 796ca632f55SGrant Likely } 797ca632f55SGrant Likely 798ca632f55SGrant Likely /* 799ca632f55SGrant Likely * This DMA functionality is only compiled in if we have 800ca632f55SGrant Likely * access to the generic DMA devices/DMA engine. 801ca632f55SGrant Likely */ 802ca632f55SGrant Likely #ifdef CONFIG_DMA_ENGINE 803ca632f55SGrant Likely static void unmap_free_dma_scatter(struct pl022 *pl022) 804ca632f55SGrant Likely { 805ca632f55SGrant Likely /* Unmap and free the SG tables */ 806ca632f55SGrant Likely dma_unmap_sg(pl022->dma_tx_channel->device->dev, pl022->sgt_tx.sgl, 807ca632f55SGrant Likely pl022->sgt_tx.nents, DMA_TO_DEVICE); 808ca632f55SGrant Likely dma_unmap_sg(pl022->dma_rx_channel->device->dev, pl022->sgt_rx.sgl, 809ca632f55SGrant Likely pl022->sgt_rx.nents, DMA_FROM_DEVICE); 810ca632f55SGrant Likely sg_free_table(&pl022->sgt_rx); 811ca632f55SGrant Likely sg_free_table(&pl022->sgt_tx); 812ca632f55SGrant Likely } 813ca632f55SGrant Likely 814ca632f55SGrant Likely static void dma_callback(void *data) 815ca632f55SGrant Likely { 816ca632f55SGrant Likely struct pl022 *pl022 = data; 817ca632f55SGrant Likely struct spi_message *msg = pl022->cur_msg; 818ca632f55SGrant Likely 819ca632f55SGrant Likely BUG_ON(!pl022->sgt_rx.sgl); 820ca632f55SGrant Likely 821ca632f55SGrant Likely #ifdef VERBOSE_DEBUG 822ca632f55SGrant Likely /* 823ca632f55SGrant Likely * Optionally dump out buffers to inspect contents, this is 824ca632f55SGrant Likely * good if you want to convince yourself that the loopback 825ca632f55SGrant Likely * read/write contents are the same, when adopting to a new 826ca632f55SGrant Likely * DMA engine. 827ca632f55SGrant Likely */ 828ca632f55SGrant Likely { 829ca632f55SGrant Likely struct scatterlist *sg; 830ca632f55SGrant Likely unsigned int i; 831ca632f55SGrant Likely 832ca632f55SGrant Likely dma_sync_sg_for_cpu(&pl022->adev->dev, 833ca632f55SGrant Likely pl022->sgt_rx.sgl, 834ca632f55SGrant Likely pl022->sgt_rx.nents, 835ca632f55SGrant Likely DMA_FROM_DEVICE); 836ca632f55SGrant Likely 837ca632f55SGrant Likely for_each_sg(pl022->sgt_rx.sgl, sg, pl022->sgt_rx.nents, i) { 838ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, "SPI RX SG ENTRY: %d", i); 839ca632f55SGrant Likely print_hex_dump(KERN_ERR, "SPI RX: ", 840ca632f55SGrant Likely DUMP_PREFIX_OFFSET, 841ca632f55SGrant Likely 16, 842ca632f55SGrant Likely 1, 843ca632f55SGrant Likely sg_virt(sg), 844ca632f55SGrant Likely sg_dma_len(sg), 845ca632f55SGrant Likely 1); 846ca632f55SGrant Likely } 847ca632f55SGrant Likely for_each_sg(pl022->sgt_tx.sgl, sg, pl022->sgt_tx.nents, i) { 848ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, "SPI TX SG ENTRY: %d", i); 849ca632f55SGrant Likely print_hex_dump(KERN_ERR, "SPI TX: ", 850ca632f55SGrant Likely DUMP_PREFIX_OFFSET, 851ca632f55SGrant Likely 16, 852ca632f55SGrant Likely 1, 853ca632f55SGrant Likely sg_virt(sg), 854ca632f55SGrant Likely sg_dma_len(sg), 855ca632f55SGrant Likely 1); 856ca632f55SGrant Likely } 857ca632f55SGrant Likely } 858ca632f55SGrant Likely #endif 859ca632f55SGrant Likely 860ca632f55SGrant Likely unmap_free_dma_scatter(pl022); 861ca632f55SGrant Likely 862ca632f55SGrant Likely /* Update total bytes transferred */ 863ca632f55SGrant Likely msg->actual_length += pl022->cur_transfer->len; 864ca632f55SGrant Likely if (pl022->cur_transfer->cs_change) 865f6f46de1SRoland Stigge pl022_cs_control(pl022, SSP_CHIP_DESELECT); 866ca632f55SGrant Likely 867ca632f55SGrant Likely /* Move to next transfer */ 868ca632f55SGrant Likely msg->state = next_transfer(pl022); 869ca632f55SGrant Likely tasklet_schedule(&pl022->pump_transfers); 870ca632f55SGrant Likely } 871ca632f55SGrant Likely 872ca632f55SGrant Likely static void setup_dma_scatter(struct pl022 *pl022, 873ca632f55SGrant Likely void *buffer, 874ca632f55SGrant Likely unsigned int length, 875ca632f55SGrant Likely struct sg_table *sgtab) 876ca632f55SGrant Likely { 877ca632f55SGrant Likely struct scatterlist *sg; 878ca632f55SGrant Likely int bytesleft = length; 879ca632f55SGrant Likely void *bufp = buffer; 880ca632f55SGrant Likely int mapbytes; 881ca632f55SGrant Likely int i; 882ca632f55SGrant Likely 883ca632f55SGrant Likely if (buffer) { 884ca632f55SGrant Likely for_each_sg(sgtab->sgl, sg, sgtab->nents, i) { 885ca632f55SGrant Likely /* 886ca632f55SGrant Likely * If there are less bytes left than what fits 887ca632f55SGrant Likely * in the current page (plus page alignment offset) 888ca632f55SGrant Likely * we just feed in this, else we stuff in as much 889ca632f55SGrant Likely * as we can. 890ca632f55SGrant Likely */ 891ca632f55SGrant Likely if (bytesleft < (PAGE_SIZE - offset_in_page(bufp))) 892ca632f55SGrant Likely mapbytes = bytesleft; 893ca632f55SGrant Likely else 894ca632f55SGrant Likely mapbytes = PAGE_SIZE - offset_in_page(bufp); 895ca632f55SGrant Likely sg_set_page(sg, virt_to_page(bufp), 896ca632f55SGrant Likely mapbytes, offset_in_page(bufp)); 897ca632f55SGrant Likely bufp += mapbytes; 898ca632f55SGrant Likely bytesleft -= mapbytes; 899ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, 900ca632f55SGrant Likely "set RX/TX target page @ %p, %d bytes, %d left\n", 901ca632f55SGrant Likely bufp, mapbytes, bytesleft); 902ca632f55SGrant Likely } 903ca632f55SGrant Likely } else { 904ca632f55SGrant Likely /* Map the dummy buffer on every page */ 905ca632f55SGrant Likely for_each_sg(sgtab->sgl, sg, sgtab->nents, i) { 906ca632f55SGrant Likely if (bytesleft < PAGE_SIZE) 907ca632f55SGrant Likely mapbytes = bytesleft; 908ca632f55SGrant Likely else 909ca632f55SGrant Likely mapbytes = PAGE_SIZE; 910ca632f55SGrant Likely sg_set_page(sg, virt_to_page(pl022->dummypage), 911ca632f55SGrant Likely mapbytes, 0); 912ca632f55SGrant Likely bytesleft -= mapbytes; 913ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, 914ca632f55SGrant Likely "set RX/TX to dummy page %d bytes, %d left\n", 915ca632f55SGrant Likely mapbytes, bytesleft); 916ca632f55SGrant Likely 917ca632f55SGrant Likely } 918ca632f55SGrant Likely } 919ca632f55SGrant Likely BUG_ON(bytesleft); 920ca632f55SGrant Likely } 921ca632f55SGrant Likely 922ca632f55SGrant Likely /** 923ca632f55SGrant Likely * configure_dma - configures the channels for the next transfer 924ca632f55SGrant Likely * @pl022: SSP driver's private data structure 925ca632f55SGrant Likely */ 926ca632f55SGrant Likely static int configure_dma(struct pl022 *pl022) 927ca632f55SGrant Likely { 928ca632f55SGrant Likely struct dma_slave_config rx_conf = { 929ca632f55SGrant Likely .src_addr = SSP_DR(pl022->phybase), 930a485df4bSVinod Koul .direction = DMA_DEV_TO_MEM, 931258aea76SViresh Kumar .device_fc = false, 932ca632f55SGrant Likely }; 933ca632f55SGrant Likely struct dma_slave_config tx_conf = { 934ca632f55SGrant Likely .dst_addr = SSP_DR(pl022->phybase), 935a485df4bSVinod Koul .direction = DMA_MEM_TO_DEV, 936258aea76SViresh Kumar .device_fc = false, 937ca632f55SGrant Likely }; 938ca632f55SGrant Likely unsigned int pages; 939ca632f55SGrant Likely int ret; 940ca632f55SGrant Likely int rx_sglen, tx_sglen; 941ca632f55SGrant Likely struct dma_chan *rxchan = pl022->dma_rx_channel; 942ca632f55SGrant Likely struct dma_chan *txchan = pl022->dma_tx_channel; 943ca632f55SGrant Likely struct dma_async_tx_descriptor *rxdesc; 944ca632f55SGrant Likely struct dma_async_tx_descriptor *txdesc; 945ca632f55SGrant Likely 946ca632f55SGrant Likely /* Check that the channels are available */ 947ca632f55SGrant Likely if (!rxchan || !txchan) 948ca632f55SGrant Likely return -ENODEV; 949ca632f55SGrant Likely 950083be3f0SLinus Walleij /* 951083be3f0SLinus Walleij * If supplied, the DMA burstsize should equal the FIFO trigger level. 952083be3f0SLinus Walleij * Notice that the DMA engine uses one-to-one mapping. Since we can 953083be3f0SLinus Walleij * not trigger on 2 elements this needs explicit mapping rather than 954083be3f0SLinus Walleij * calculation. 955083be3f0SLinus Walleij */ 956083be3f0SLinus Walleij switch (pl022->rx_lev_trig) { 957083be3f0SLinus Walleij case SSP_RX_1_OR_MORE_ELEM: 958083be3f0SLinus Walleij rx_conf.src_maxburst = 1; 959083be3f0SLinus Walleij break; 960083be3f0SLinus Walleij case SSP_RX_4_OR_MORE_ELEM: 961083be3f0SLinus Walleij rx_conf.src_maxburst = 4; 962083be3f0SLinus Walleij break; 963083be3f0SLinus Walleij case SSP_RX_8_OR_MORE_ELEM: 964083be3f0SLinus Walleij rx_conf.src_maxburst = 8; 965083be3f0SLinus Walleij break; 966083be3f0SLinus Walleij case SSP_RX_16_OR_MORE_ELEM: 967083be3f0SLinus Walleij rx_conf.src_maxburst = 16; 968083be3f0SLinus Walleij break; 969083be3f0SLinus Walleij case SSP_RX_32_OR_MORE_ELEM: 970083be3f0SLinus Walleij rx_conf.src_maxburst = 32; 971083be3f0SLinus Walleij break; 972083be3f0SLinus Walleij default: 973083be3f0SLinus Walleij rx_conf.src_maxburst = pl022->vendor->fifodepth >> 1; 974083be3f0SLinus Walleij break; 975083be3f0SLinus Walleij } 976083be3f0SLinus Walleij 977083be3f0SLinus Walleij switch (pl022->tx_lev_trig) { 978083be3f0SLinus Walleij case SSP_TX_1_OR_MORE_EMPTY_LOC: 979083be3f0SLinus Walleij tx_conf.dst_maxburst = 1; 980083be3f0SLinus Walleij break; 981083be3f0SLinus Walleij case SSP_TX_4_OR_MORE_EMPTY_LOC: 982083be3f0SLinus Walleij tx_conf.dst_maxburst = 4; 983083be3f0SLinus Walleij break; 984083be3f0SLinus Walleij case SSP_TX_8_OR_MORE_EMPTY_LOC: 985083be3f0SLinus Walleij tx_conf.dst_maxburst = 8; 986083be3f0SLinus Walleij break; 987083be3f0SLinus Walleij case SSP_TX_16_OR_MORE_EMPTY_LOC: 988083be3f0SLinus Walleij tx_conf.dst_maxburst = 16; 989083be3f0SLinus Walleij break; 990083be3f0SLinus Walleij case SSP_TX_32_OR_MORE_EMPTY_LOC: 991083be3f0SLinus Walleij tx_conf.dst_maxburst = 32; 992083be3f0SLinus Walleij break; 993083be3f0SLinus Walleij default: 994083be3f0SLinus Walleij tx_conf.dst_maxburst = pl022->vendor->fifodepth >> 1; 995083be3f0SLinus Walleij break; 996083be3f0SLinus Walleij } 997083be3f0SLinus Walleij 998ca632f55SGrant Likely switch (pl022->read) { 999ca632f55SGrant Likely case READING_NULL: 1000ca632f55SGrant Likely /* Use the same as for writing */ 1001ca632f55SGrant Likely rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; 1002ca632f55SGrant Likely break; 1003ca632f55SGrant Likely case READING_U8: 1004ca632f55SGrant Likely rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 1005ca632f55SGrant Likely break; 1006ca632f55SGrant Likely case READING_U16: 1007ca632f55SGrant Likely rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; 1008ca632f55SGrant Likely break; 1009ca632f55SGrant Likely case READING_U32: 1010ca632f55SGrant Likely rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 1011ca632f55SGrant Likely break; 1012ca632f55SGrant Likely } 1013ca632f55SGrant Likely 1014ca632f55SGrant Likely switch (pl022->write) { 1015ca632f55SGrant Likely case WRITING_NULL: 1016ca632f55SGrant Likely /* Use the same as for reading */ 1017ca632f55SGrant Likely tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED; 1018ca632f55SGrant Likely break; 1019ca632f55SGrant Likely case WRITING_U8: 1020ca632f55SGrant Likely tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 1021ca632f55SGrant Likely break; 1022ca632f55SGrant Likely case WRITING_U16: 1023ca632f55SGrant Likely tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; 1024ca632f55SGrant Likely break; 1025ca632f55SGrant Likely case WRITING_U32: 1026ca632f55SGrant Likely tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 1027ca632f55SGrant Likely break; 1028ca632f55SGrant Likely } 1029ca632f55SGrant Likely 1030ca632f55SGrant Likely /* SPI pecularity: we need to read and write the same width */ 1031ca632f55SGrant Likely if (rx_conf.src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) 1032ca632f55SGrant Likely rx_conf.src_addr_width = tx_conf.dst_addr_width; 1033ca632f55SGrant Likely if (tx_conf.dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) 1034ca632f55SGrant Likely tx_conf.dst_addr_width = rx_conf.src_addr_width; 1035ca632f55SGrant Likely BUG_ON(rx_conf.src_addr_width != tx_conf.dst_addr_width); 1036ca632f55SGrant Likely 1037ca632f55SGrant Likely dmaengine_slave_config(rxchan, &rx_conf); 1038ca632f55SGrant Likely dmaengine_slave_config(txchan, &tx_conf); 1039ca632f55SGrant Likely 1040ca632f55SGrant Likely /* Create sglists for the transfers */ 1041b181565eSViresh Kumar pages = DIV_ROUND_UP(pl022->cur_transfer->len, PAGE_SIZE); 1042ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, "using %d pages for transfer\n", pages); 1043ca632f55SGrant Likely 1044538a18dcSViresh Kumar ret = sg_alloc_table(&pl022->sgt_rx, pages, GFP_ATOMIC); 1045ca632f55SGrant Likely if (ret) 1046ca632f55SGrant Likely goto err_alloc_rx_sg; 1047ca632f55SGrant Likely 1048538a18dcSViresh Kumar ret = sg_alloc_table(&pl022->sgt_tx, pages, GFP_ATOMIC); 1049ca632f55SGrant Likely if (ret) 1050ca632f55SGrant Likely goto err_alloc_tx_sg; 1051ca632f55SGrant Likely 1052ca632f55SGrant Likely /* Fill in the scatterlists for the RX+TX buffers */ 1053ca632f55SGrant Likely setup_dma_scatter(pl022, pl022->rx, 1054ca632f55SGrant Likely pl022->cur_transfer->len, &pl022->sgt_rx); 1055ca632f55SGrant Likely setup_dma_scatter(pl022, pl022->tx, 1056ca632f55SGrant Likely pl022->cur_transfer->len, &pl022->sgt_tx); 1057ca632f55SGrant Likely 1058ca632f55SGrant Likely /* Map DMA buffers */ 1059ca632f55SGrant Likely rx_sglen = dma_map_sg(rxchan->device->dev, pl022->sgt_rx.sgl, 1060ca632f55SGrant Likely pl022->sgt_rx.nents, DMA_FROM_DEVICE); 1061ca632f55SGrant Likely if (!rx_sglen) 1062ca632f55SGrant Likely goto err_rx_sgmap; 1063ca632f55SGrant Likely 1064ca632f55SGrant Likely tx_sglen = dma_map_sg(txchan->device->dev, pl022->sgt_tx.sgl, 1065ca632f55SGrant Likely pl022->sgt_tx.nents, DMA_TO_DEVICE); 1066ca632f55SGrant Likely if (!tx_sglen) 1067ca632f55SGrant Likely goto err_tx_sgmap; 1068ca632f55SGrant Likely 1069ca632f55SGrant Likely /* Send both scatterlists */ 107016052827SAlexandre Bounine rxdesc = dmaengine_prep_slave_sg(rxchan, 1071ca632f55SGrant Likely pl022->sgt_rx.sgl, 1072ca632f55SGrant Likely rx_sglen, 1073a485df4bSVinod Koul DMA_DEV_TO_MEM, 1074ca632f55SGrant Likely DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 1075ca632f55SGrant Likely if (!rxdesc) 1076ca632f55SGrant Likely goto err_rxdesc; 1077ca632f55SGrant Likely 107816052827SAlexandre Bounine txdesc = dmaengine_prep_slave_sg(txchan, 1079ca632f55SGrant Likely pl022->sgt_tx.sgl, 1080ca632f55SGrant Likely tx_sglen, 1081a485df4bSVinod Koul DMA_MEM_TO_DEV, 1082ca632f55SGrant Likely DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 1083ca632f55SGrant Likely if (!txdesc) 1084ca632f55SGrant Likely goto err_txdesc; 1085ca632f55SGrant Likely 1086ca632f55SGrant Likely /* Put the callback on the RX transfer only, that should finish last */ 1087ca632f55SGrant Likely rxdesc->callback = dma_callback; 1088ca632f55SGrant Likely rxdesc->callback_param = pl022; 1089ca632f55SGrant Likely 1090ca632f55SGrant Likely /* Submit and fire RX and TX with TX last so we're ready to read! */ 1091ca632f55SGrant Likely dmaengine_submit(rxdesc); 1092ca632f55SGrant Likely dmaengine_submit(txdesc); 1093ca632f55SGrant Likely dma_async_issue_pending(rxchan); 1094ca632f55SGrant Likely dma_async_issue_pending(txchan); 1095ffbbdd21SLinus Walleij pl022->dma_running = true; 1096ca632f55SGrant Likely 1097ca632f55SGrant Likely return 0; 1098ca632f55SGrant Likely 1099ca632f55SGrant Likely err_txdesc: 1100ca632f55SGrant Likely dmaengine_terminate_all(txchan); 1101ca632f55SGrant Likely err_rxdesc: 1102ca632f55SGrant Likely dmaengine_terminate_all(rxchan); 1103ca632f55SGrant Likely dma_unmap_sg(txchan->device->dev, pl022->sgt_tx.sgl, 1104ca632f55SGrant Likely pl022->sgt_tx.nents, DMA_TO_DEVICE); 1105ca632f55SGrant Likely err_tx_sgmap: 1106ca632f55SGrant Likely dma_unmap_sg(rxchan->device->dev, pl022->sgt_rx.sgl, 11073ffa6158SRay Jui pl022->sgt_rx.nents, DMA_FROM_DEVICE); 1108ca632f55SGrant Likely err_rx_sgmap: 1109ca632f55SGrant Likely sg_free_table(&pl022->sgt_tx); 1110ca632f55SGrant Likely err_alloc_tx_sg: 1111ca632f55SGrant Likely sg_free_table(&pl022->sgt_rx); 1112ca632f55SGrant Likely err_alloc_rx_sg: 1113ca632f55SGrant Likely return -ENOMEM; 1114ca632f55SGrant Likely } 1115ca632f55SGrant Likely 1116fd4a319bSGrant Likely static int pl022_dma_probe(struct pl022 *pl022) 1117ca632f55SGrant Likely { 1118ca632f55SGrant Likely dma_cap_mask_t mask; 1119ca632f55SGrant Likely 1120ca632f55SGrant Likely /* Try to acquire a generic DMA engine slave channel */ 1121ca632f55SGrant Likely dma_cap_zero(mask); 1122ca632f55SGrant Likely dma_cap_set(DMA_SLAVE, mask); 1123ca632f55SGrant Likely /* 1124ca632f55SGrant Likely * We need both RX and TX channels to do DMA, else do none 1125ca632f55SGrant Likely * of them. 1126ca632f55SGrant Likely */ 1127ca632f55SGrant Likely pl022->dma_rx_channel = dma_request_channel(mask, 1128ca632f55SGrant Likely pl022->master_info->dma_filter, 1129ca632f55SGrant Likely pl022->master_info->dma_rx_param); 1130ca632f55SGrant Likely if (!pl022->dma_rx_channel) { 1131ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, "no RX DMA channel!\n"); 1132ca632f55SGrant Likely goto err_no_rxchan; 1133ca632f55SGrant Likely } 1134ca632f55SGrant Likely 1135ca632f55SGrant Likely pl022->dma_tx_channel = dma_request_channel(mask, 1136ca632f55SGrant Likely pl022->master_info->dma_filter, 1137ca632f55SGrant Likely pl022->master_info->dma_tx_param); 1138ca632f55SGrant Likely if (!pl022->dma_tx_channel) { 1139ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, "no TX DMA channel!\n"); 1140ca632f55SGrant Likely goto err_no_txchan; 1141ca632f55SGrant Likely } 1142ca632f55SGrant Likely 1143ca632f55SGrant Likely pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL); 114477538f4aSJingoo Han if (!pl022->dummypage) 1145ca632f55SGrant Likely goto err_no_dummypage; 1146ca632f55SGrant Likely 1147ca632f55SGrant Likely dev_info(&pl022->adev->dev, "setup for DMA on RX %s, TX %s\n", 1148ca632f55SGrant Likely dma_chan_name(pl022->dma_rx_channel), 1149ca632f55SGrant Likely dma_chan_name(pl022->dma_tx_channel)); 1150ca632f55SGrant Likely 1151ca632f55SGrant Likely return 0; 1152ca632f55SGrant Likely 1153ca632f55SGrant Likely err_no_dummypage: 1154ca632f55SGrant Likely dma_release_channel(pl022->dma_tx_channel); 1155ca632f55SGrant Likely err_no_txchan: 1156ca632f55SGrant Likely dma_release_channel(pl022->dma_rx_channel); 1157ca632f55SGrant Likely pl022->dma_rx_channel = NULL; 1158ca632f55SGrant Likely err_no_rxchan: 1159ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1160ca632f55SGrant Likely "Failed to work in dma mode, work without dma!\n"); 1161ca632f55SGrant Likely return -ENODEV; 1162ca632f55SGrant Likely } 1163ca632f55SGrant Likely 1164dc715452SArnd Bergmann static int pl022_dma_autoprobe(struct pl022 *pl022) 1165dc715452SArnd Bergmann { 1166dc715452SArnd Bergmann struct device *dev = &pl022->adev->dev; 1167f3d4bb33SRabin Vincent struct dma_chan *chan; 1168f3d4bb33SRabin Vincent int err; 1169dc715452SArnd Bergmann 1170dc715452SArnd Bergmann /* automatically configure DMA channels from platform, normally using DT */ 1171f3d4bb33SRabin Vincent chan = dma_request_slave_channel_reason(dev, "rx"); 1172f3d4bb33SRabin Vincent if (IS_ERR(chan)) { 1173f3d4bb33SRabin Vincent err = PTR_ERR(chan); 1174dc715452SArnd Bergmann goto err_no_rxchan; 1175f3d4bb33SRabin Vincent } 1176dc715452SArnd Bergmann 1177f3d4bb33SRabin Vincent pl022->dma_rx_channel = chan; 1178f3d4bb33SRabin Vincent 1179f3d4bb33SRabin Vincent chan = dma_request_slave_channel_reason(dev, "tx"); 1180f3d4bb33SRabin Vincent if (IS_ERR(chan)) { 1181f3d4bb33SRabin Vincent err = PTR_ERR(chan); 1182dc715452SArnd Bergmann goto err_no_txchan; 1183f3d4bb33SRabin Vincent } 1184f3d4bb33SRabin Vincent 1185f3d4bb33SRabin Vincent pl022->dma_tx_channel = chan; 1186dc715452SArnd Bergmann 1187dc715452SArnd Bergmann pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL); 1188f3d4bb33SRabin Vincent if (!pl022->dummypage) { 1189f3d4bb33SRabin Vincent err = -ENOMEM; 1190dc715452SArnd Bergmann goto err_no_dummypage; 1191f3d4bb33SRabin Vincent } 1192dc715452SArnd Bergmann 1193dc715452SArnd Bergmann return 0; 1194dc715452SArnd Bergmann 1195dc715452SArnd Bergmann err_no_dummypage: 1196dc715452SArnd Bergmann dma_release_channel(pl022->dma_tx_channel); 1197dc715452SArnd Bergmann pl022->dma_tx_channel = NULL; 1198dc715452SArnd Bergmann err_no_txchan: 1199dc715452SArnd Bergmann dma_release_channel(pl022->dma_rx_channel); 1200dc715452SArnd Bergmann pl022->dma_rx_channel = NULL; 1201dc715452SArnd Bergmann err_no_rxchan: 1202f3d4bb33SRabin Vincent return err; 1203dc715452SArnd Bergmann } 1204dc715452SArnd Bergmann 1205ca632f55SGrant Likely static void terminate_dma(struct pl022 *pl022) 1206ca632f55SGrant Likely { 1207ca632f55SGrant Likely struct dma_chan *rxchan = pl022->dma_rx_channel; 1208ca632f55SGrant Likely struct dma_chan *txchan = pl022->dma_tx_channel; 1209ca632f55SGrant Likely 1210ca632f55SGrant Likely dmaengine_terminate_all(rxchan); 1211ca632f55SGrant Likely dmaengine_terminate_all(txchan); 1212ca632f55SGrant Likely unmap_free_dma_scatter(pl022); 1213ffbbdd21SLinus Walleij pl022->dma_running = false; 1214ca632f55SGrant Likely } 1215ca632f55SGrant Likely 1216ca632f55SGrant Likely static void pl022_dma_remove(struct pl022 *pl022) 1217ca632f55SGrant Likely { 1218ffbbdd21SLinus Walleij if (pl022->dma_running) 1219ca632f55SGrant Likely terminate_dma(pl022); 1220ca632f55SGrant Likely if (pl022->dma_tx_channel) 1221ca632f55SGrant Likely dma_release_channel(pl022->dma_tx_channel); 1222ca632f55SGrant Likely if (pl022->dma_rx_channel) 1223ca632f55SGrant Likely dma_release_channel(pl022->dma_rx_channel); 1224ca632f55SGrant Likely kfree(pl022->dummypage); 1225ca632f55SGrant Likely } 1226ca632f55SGrant Likely 1227ca632f55SGrant Likely #else 1228ca632f55SGrant Likely static inline int configure_dma(struct pl022 *pl022) 1229ca632f55SGrant Likely { 1230ca632f55SGrant Likely return -ENODEV; 1231ca632f55SGrant Likely } 1232ca632f55SGrant Likely 1233dc715452SArnd Bergmann static inline int pl022_dma_autoprobe(struct pl022 *pl022) 1234dc715452SArnd Bergmann { 1235dc715452SArnd Bergmann return 0; 1236dc715452SArnd Bergmann } 1237dc715452SArnd Bergmann 1238ca632f55SGrant Likely static inline int pl022_dma_probe(struct pl022 *pl022) 1239ca632f55SGrant Likely { 1240ca632f55SGrant Likely return 0; 1241ca632f55SGrant Likely } 1242ca632f55SGrant Likely 1243ca632f55SGrant Likely static inline void pl022_dma_remove(struct pl022 *pl022) 1244ca632f55SGrant Likely { 1245ca632f55SGrant Likely } 1246ca632f55SGrant Likely #endif 1247ca632f55SGrant Likely 1248ca632f55SGrant Likely /** 1249ca632f55SGrant Likely * pl022_interrupt_handler - Interrupt handler for SSP controller 1250ca632f55SGrant Likely * 1251ca632f55SGrant Likely * This function handles interrupts generated for an interrupt based transfer. 1252ca632f55SGrant Likely * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the 1253ca632f55SGrant Likely * current message's state as STATE_ERROR and schedule the tasklet 1254ca632f55SGrant Likely * pump_transfers which will do the postprocessing of the current message by 1255ca632f55SGrant Likely * calling giveback(). Otherwise it reads data from RX FIFO till there is no 1256ca632f55SGrant Likely * more data, and writes data in TX FIFO till it is not full. If we complete 1257ca632f55SGrant Likely * the transfer we move to the next transfer and schedule the tasklet. 1258ca632f55SGrant Likely */ 1259ca632f55SGrant Likely static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id) 1260ca632f55SGrant Likely { 1261ca632f55SGrant Likely struct pl022 *pl022 = dev_id; 1262ca632f55SGrant Likely struct spi_message *msg = pl022->cur_msg; 1263ca632f55SGrant Likely u16 irq_status = 0; 1264ca632f55SGrant Likely 1265ca632f55SGrant Likely if (unlikely(!msg)) { 1266ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1267ca632f55SGrant Likely "bad message state in interrupt handler"); 1268ca632f55SGrant Likely /* Never fail */ 1269ca632f55SGrant Likely return IRQ_HANDLED; 1270ca632f55SGrant Likely } 1271ca632f55SGrant Likely 1272ca632f55SGrant Likely /* Read the Interrupt Status Register */ 1273ca632f55SGrant Likely irq_status = readw(SSP_MIS(pl022->virtbase)); 1274ca632f55SGrant Likely 1275ca632f55SGrant Likely if (unlikely(!irq_status)) 1276ca632f55SGrant Likely return IRQ_NONE; 1277ca632f55SGrant Likely 1278ca632f55SGrant Likely /* 1279ca632f55SGrant Likely * This handles the FIFO interrupts, the timeout 1280ca632f55SGrant Likely * interrupts are flatly ignored, they cannot be 1281ca632f55SGrant Likely * trusted. 1282ca632f55SGrant Likely */ 1283ca632f55SGrant Likely if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) { 1284ca632f55SGrant Likely /* 1285ca632f55SGrant Likely * Overrun interrupt - bail out since our Data has been 1286ca632f55SGrant Likely * corrupted 1287ca632f55SGrant Likely */ 1288ca632f55SGrant Likely dev_err(&pl022->adev->dev, "FIFO overrun\n"); 1289ca632f55SGrant Likely if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF) 1290ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1291ca632f55SGrant Likely "RXFIFO is full\n"); 1292ca632f55SGrant Likely 1293ca632f55SGrant Likely /* 1294ca632f55SGrant Likely * Disable and clear interrupts, disable SSP, 1295ca632f55SGrant Likely * mark message with bad status so it can be 1296ca632f55SGrant Likely * retried. 1297ca632f55SGrant Likely */ 1298ca632f55SGrant Likely writew(DISABLE_ALL_INTERRUPTS, 1299ca632f55SGrant Likely SSP_IMSC(pl022->virtbase)); 1300ca632f55SGrant Likely writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase)); 1301ca632f55SGrant Likely writew((readw(SSP_CR1(pl022->virtbase)) & 1302ca632f55SGrant Likely (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase)); 1303ca632f55SGrant Likely msg->state = STATE_ERROR; 1304ca632f55SGrant Likely 1305ca632f55SGrant Likely /* Schedule message queue handler */ 1306ca632f55SGrant Likely tasklet_schedule(&pl022->pump_transfers); 1307ca632f55SGrant Likely return IRQ_HANDLED; 1308ca632f55SGrant Likely } 1309ca632f55SGrant Likely 1310ca632f55SGrant Likely readwriter(pl022); 1311ca632f55SGrant Likely 13127183d1ebSAlexander Sverdlin if (pl022->tx == pl022->tx_end) { 1313172289dfSChris Blair /* Disable Transmit interrupt, enable receive interrupt */ 1314172289dfSChris Blair writew((readw(SSP_IMSC(pl022->virtbase)) & 1315172289dfSChris Blair ~SSP_IMSC_MASK_TXIM) | SSP_IMSC_MASK_RXIM, 1316ca632f55SGrant Likely SSP_IMSC(pl022->virtbase)); 1317ca632f55SGrant Likely } 1318ca632f55SGrant Likely 1319ca632f55SGrant Likely /* 1320ca632f55SGrant Likely * Since all transactions must write as much as shall be read, 1321ca632f55SGrant Likely * we can conclude the entire transaction once RX is complete. 1322ca632f55SGrant Likely * At this point, all TX will always be finished. 1323ca632f55SGrant Likely */ 1324ca632f55SGrant Likely if (pl022->rx >= pl022->rx_end) { 1325ca632f55SGrant Likely writew(DISABLE_ALL_INTERRUPTS, 1326ca632f55SGrant Likely SSP_IMSC(pl022->virtbase)); 1327ca632f55SGrant Likely writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase)); 1328ca632f55SGrant Likely if (unlikely(pl022->rx > pl022->rx_end)) { 1329ca632f55SGrant Likely dev_warn(&pl022->adev->dev, "read %u surplus " 1330ca632f55SGrant Likely "bytes (did you request an odd " 1331ca632f55SGrant Likely "number of bytes on a 16bit bus?)\n", 1332ca632f55SGrant Likely (u32) (pl022->rx - pl022->rx_end)); 1333ca632f55SGrant Likely } 1334ca632f55SGrant Likely /* Update total bytes transferred */ 1335ca632f55SGrant Likely msg->actual_length += pl022->cur_transfer->len; 1336ca632f55SGrant Likely if (pl022->cur_transfer->cs_change) 1337f6f46de1SRoland Stigge pl022_cs_control(pl022, SSP_CHIP_DESELECT); 1338ca632f55SGrant Likely /* Move to next transfer */ 1339ca632f55SGrant Likely msg->state = next_transfer(pl022); 1340ca632f55SGrant Likely tasklet_schedule(&pl022->pump_transfers); 1341ca632f55SGrant Likely return IRQ_HANDLED; 1342ca632f55SGrant Likely } 1343ca632f55SGrant Likely 1344ca632f55SGrant Likely return IRQ_HANDLED; 1345ca632f55SGrant Likely } 1346ca632f55SGrant Likely 1347ca632f55SGrant Likely /** 1348ca632f55SGrant Likely * This sets up the pointers to memory for the next message to 1349ca632f55SGrant Likely * send out on the SPI bus. 1350ca632f55SGrant Likely */ 1351ca632f55SGrant Likely static int set_up_next_transfer(struct pl022 *pl022, 1352ca632f55SGrant Likely struct spi_transfer *transfer) 1353ca632f55SGrant Likely { 1354ca632f55SGrant Likely int residue; 1355ca632f55SGrant Likely 1356ca632f55SGrant Likely /* Sanity check the message for this bus width */ 1357ca632f55SGrant Likely residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes; 1358ca632f55SGrant Likely if (unlikely(residue != 0)) { 1359ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1360ca632f55SGrant Likely "message of %u bytes to transmit but the current " 1361ca632f55SGrant Likely "chip bus has a data width of %u bytes!\n", 1362ca632f55SGrant Likely pl022->cur_transfer->len, 1363ca632f55SGrant Likely pl022->cur_chip->n_bytes); 1364ca632f55SGrant Likely dev_err(&pl022->adev->dev, "skipping this message\n"); 1365ca632f55SGrant Likely return -EIO; 1366ca632f55SGrant Likely } 1367ca632f55SGrant Likely pl022->tx = (void *)transfer->tx_buf; 1368ca632f55SGrant Likely pl022->tx_end = pl022->tx + pl022->cur_transfer->len; 1369ca632f55SGrant Likely pl022->rx = (void *)transfer->rx_buf; 1370ca632f55SGrant Likely pl022->rx_end = pl022->rx + pl022->cur_transfer->len; 1371ca632f55SGrant Likely pl022->write = 1372ca632f55SGrant Likely pl022->tx ? pl022->cur_chip->write : WRITING_NULL; 1373ca632f55SGrant Likely pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL; 1374ca632f55SGrant Likely return 0; 1375ca632f55SGrant Likely } 1376ca632f55SGrant Likely 1377ca632f55SGrant Likely /** 1378ca632f55SGrant Likely * pump_transfers - Tasklet function which schedules next transfer 1379ca632f55SGrant Likely * when running in interrupt or DMA transfer mode. 1380ca632f55SGrant Likely * @data: SSP driver private data structure 1381ca632f55SGrant Likely * 1382ca632f55SGrant Likely */ 1383ca632f55SGrant Likely static void pump_transfers(unsigned long data) 1384ca632f55SGrant Likely { 1385ca632f55SGrant Likely struct pl022 *pl022 = (struct pl022 *) data; 1386ca632f55SGrant Likely struct spi_message *message = NULL; 1387ca632f55SGrant Likely struct spi_transfer *transfer = NULL; 1388ca632f55SGrant Likely struct spi_transfer *previous = NULL; 1389ca632f55SGrant Likely 1390ca632f55SGrant Likely /* Get current state information */ 1391ca632f55SGrant Likely message = pl022->cur_msg; 1392ca632f55SGrant Likely transfer = pl022->cur_transfer; 1393ca632f55SGrant Likely 1394ca632f55SGrant Likely /* Handle for abort */ 1395ca632f55SGrant Likely if (message->state == STATE_ERROR) { 1396ca632f55SGrant Likely message->status = -EIO; 1397ca632f55SGrant Likely giveback(pl022); 1398ca632f55SGrant Likely return; 1399ca632f55SGrant Likely } 1400ca632f55SGrant Likely 1401ca632f55SGrant Likely /* Handle end of message */ 1402ca632f55SGrant Likely if (message->state == STATE_DONE) { 1403ca632f55SGrant Likely message->status = 0; 1404ca632f55SGrant Likely giveback(pl022); 1405ca632f55SGrant Likely return; 1406ca632f55SGrant Likely } 1407ca632f55SGrant Likely 1408ca632f55SGrant Likely /* Delay if requested at end of transfer before CS change */ 1409ca632f55SGrant Likely if (message->state == STATE_RUNNING) { 1410ca632f55SGrant Likely previous = list_entry(transfer->transfer_list.prev, 1411ca632f55SGrant Likely struct spi_transfer, 1412ca632f55SGrant Likely transfer_list); 1413ca632f55SGrant Likely if (previous->delay_usecs) 1414ca632f55SGrant Likely /* 1415ca632f55SGrant Likely * FIXME: This runs in interrupt context. 1416ca632f55SGrant Likely * Is this really smart? 1417ca632f55SGrant Likely */ 1418ca632f55SGrant Likely udelay(previous->delay_usecs); 1419ca632f55SGrant Likely 14208b8d7191SVirupax Sadashivpetimath /* Reselect chip select only if cs_change was requested */ 1421ca632f55SGrant Likely if (previous->cs_change) 1422f6f46de1SRoland Stigge pl022_cs_control(pl022, SSP_CHIP_SELECT); 1423ca632f55SGrant Likely } else { 1424ca632f55SGrant Likely /* STATE_START */ 1425ca632f55SGrant Likely message->state = STATE_RUNNING; 1426ca632f55SGrant Likely } 1427ca632f55SGrant Likely 1428ca632f55SGrant Likely if (set_up_next_transfer(pl022, transfer)) { 1429ca632f55SGrant Likely message->state = STATE_ERROR; 1430ca632f55SGrant Likely message->status = -EIO; 1431ca632f55SGrant Likely giveback(pl022); 1432ca632f55SGrant Likely return; 1433ca632f55SGrant Likely } 1434ca632f55SGrant Likely /* Flush the FIFOs and let's go! */ 1435ca632f55SGrant Likely flush(pl022); 1436ca632f55SGrant Likely 1437ca632f55SGrant Likely if (pl022->cur_chip->enable_dma) { 1438ca632f55SGrant Likely if (configure_dma(pl022)) { 1439ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, 1440ca632f55SGrant Likely "configuration of DMA failed, fall back to interrupt mode\n"); 1441ca632f55SGrant Likely goto err_config_dma; 1442ca632f55SGrant Likely } 1443ca632f55SGrant Likely return; 1444ca632f55SGrant Likely } 1445ca632f55SGrant Likely 1446ca632f55SGrant Likely err_config_dma: 1447172289dfSChris Blair /* enable all interrupts except RX */ 1448172289dfSChris Blair writew(ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM, SSP_IMSC(pl022->virtbase)); 1449ca632f55SGrant Likely } 1450ca632f55SGrant Likely 1451ca632f55SGrant Likely static void do_interrupt_dma_transfer(struct pl022 *pl022) 1452ca632f55SGrant Likely { 1453172289dfSChris Blair /* 1454172289dfSChris Blair * Default is to enable all interrupts except RX - 1455172289dfSChris Blair * this will be enabled once TX is complete 1456172289dfSChris Blair */ 1457d555ea05SMark Brown u32 irqflags = (u32)(ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM); 1458ca632f55SGrant Likely 14598b8d7191SVirupax Sadashivpetimath /* Enable target chip, if not already active */ 14608b8d7191SVirupax Sadashivpetimath if (!pl022->next_msg_cs_active) 1461f6f46de1SRoland Stigge pl022_cs_control(pl022, SSP_CHIP_SELECT); 14628b8d7191SVirupax Sadashivpetimath 1463ca632f55SGrant Likely if (set_up_next_transfer(pl022, pl022->cur_transfer)) { 1464ca632f55SGrant Likely /* Error path */ 1465ca632f55SGrant Likely pl022->cur_msg->state = STATE_ERROR; 1466ca632f55SGrant Likely pl022->cur_msg->status = -EIO; 1467ca632f55SGrant Likely giveback(pl022); 1468ca632f55SGrant Likely return; 1469ca632f55SGrant Likely } 1470ca632f55SGrant Likely /* If we're using DMA, set up DMA here */ 1471ca632f55SGrant Likely if (pl022->cur_chip->enable_dma) { 1472ca632f55SGrant Likely /* Configure DMA transfer */ 1473ca632f55SGrant Likely if (configure_dma(pl022)) { 1474ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, 1475ca632f55SGrant Likely "configuration of DMA failed, fall back to interrupt mode\n"); 1476ca632f55SGrant Likely goto err_config_dma; 1477ca632f55SGrant Likely } 1478ca632f55SGrant Likely /* Disable interrupts in DMA mode, IRQ from DMA controller */ 1479ca632f55SGrant Likely irqflags = DISABLE_ALL_INTERRUPTS; 1480ca632f55SGrant Likely } 1481ca632f55SGrant Likely err_config_dma: 1482ca632f55SGrant Likely /* Enable SSP, turn on interrupts */ 1483ca632f55SGrant Likely writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE), 1484ca632f55SGrant Likely SSP_CR1(pl022->virtbase)); 1485ca632f55SGrant Likely writew(irqflags, SSP_IMSC(pl022->virtbase)); 1486ca632f55SGrant Likely } 1487ca632f55SGrant Likely 1488ca632f55SGrant Likely static void do_polling_transfer(struct pl022 *pl022) 1489ca632f55SGrant Likely { 1490ca632f55SGrant Likely struct spi_message *message = NULL; 1491ca632f55SGrant Likely struct spi_transfer *transfer = NULL; 1492ca632f55SGrant Likely struct spi_transfer *previous = NULL; 1493ca632f55SGrant Likely struct chip_data *chip; 1494ca632f55SGrant Likely unsigned long time, timeout; 1495ca632f55SGrant Likely 1496ca632f55SGrant Likely chip = pl022->cur_chip; 1497ca632f55SGrant Likely message = pl022->cur_msg; 1498ca632f55SGrant Likely 1499ca632f55SGrant Likely while (message->state != STATE_DONE) { 1500ca632f55SGrant Likely /* Handle for abort */ 1501ca632f55SGrant Likely if (message->state == STATE_ERROR) 1502ca632f55SGrant Likely break; 1503ca632f55SGrant Likely transfer = pl022->cur_transfer; 1504ca632f55SGrant Likely 1505ca632f55SGrant Likely /* Delay if requested at end of transfer */ 1506ca632f55SGrant Likely if (message->state == STATE_RUNNING) { 1507ca632f55SGrant Likely previous = 1508ca632f55SGrant Likely list_entry(transfer->transfer_list.prev, 1509ca632f55SGrant Likely struct spi_transfer, transfer_list); 1510ca632f55SGrant Likely if (previous->delay_usecs) 1511ca632f55SGrant Likely udelay(previous->delay_usecs); 1512ca632f55SGrant Likely if (previous->cs_change) 1513f6f46de1SRoland Stigge pl022_cs_control(pl022, SSP_CHIP_SELECT); 1514ca632f55SGrant Likely } else { 1515ca632f55SGrant Likely /* STATE_START */ 1516ca632f55SGrant Likely message->state = STATE_RUNNING; 15178b8d7191SVirupax Sadashivpetimath if (!pl022->next_msg_cs_active) 1518f6f46de1SRoland Stigge pl022_cs_control(pl022, SSP_CHIP_SELECT); 1519ca632f55SGrant Likely } 1520ca632f55SGrant Likely 1521ca632f55SGrant Likely /* Configuration Changing Per Transfer */ 1522ca632f55SGrant Likely if (set_up_next_transfer(pl022, transfer)) { 1523ca632f55SGrant Likely /* Error path */ 1524ca632f55SGrant Likely message->state = STATE_ERROR; 1525ca632f55SGrant Likely break; 1526ca632f55SGrant Likely } 1527ca632f55SGrant Likely /* Flush FIFOs and enable SSP */ 1528ca632f55SGrant Likely flush(pl022); 1529ca632f55SGrant Likely writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE), 1530ca632f55SGrant Likely SSP_CR1(pl022->virtbase)); 1531ca632f55SGrant Likely 1532ca632f55SGrant Likely dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n"); 1533ca632f55SGrant Likely 1534ca632f55SGrant Likely timeout = jiffies + msecs_to_jiffies(SPI_POLLING_TIMEOUT); 1535ca632f55SGrant Likely while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end) { 1536ca632f55SGrant Likely time = jiffies; 1537ca632f55SGrant Likely readwriter(pl022); 1538ca632f55SGrant Likely if (time_after(time, timeout)) { 1539ca632f55SGrant Likely dev_warn(&pl022->adev->dev, 1540ca632f55SGrant Likely "%s: timeout!\n", __func__); 1541ca632f55SGrant Likely message->state = STATE_ERROR; 1542ca632f55SGrant Likely goto out; 1543ca632f55SGrant Likely } 1544ca632f55SGrant Likely cpu_relax(); 1545ca632f55SGrant Likely } 1546ca632f55SGrant Likely 1547ca632f55SGrant Likely /* Update total byte transferred */ 1548ca632f55SGrant Likely message->actual_length += pl022->cur_transfer->len; 1549ca632f55SGrant Likely if (pl022->cur_transfer->cs_change) 1550f6f46de1SRoland Stigge pl022_cs_control(pl022, SSP_CHIP_DESELECT); 1551ca632f55SGrant Likely /* Move to next transfer */ 1552ca632f55SGrant Likely message->state = next_transfer(pl022); 1553ca632f55SGrant Likely } 1554ca632f55SGrant Likely out: 1555ca632f55SGrant Likely /* Handle end of message */ 1556ca632f55SGrant Likely if (message->state == STATE_DONE) 1557ca632f55SGrant Likely message->status = 0; 1558ca632f55SGrant Likely else 1559ca632f55SGrant Likely message->status = -EIO; 1560ca632f55SGrant Likely 1561ca632f55SGrant Likely giveback(pl022); 1562ca632f55SGrant Likely return; 1563ca632f55SGrant Likely } 1564ca632f55SGrant Likely 1565ffbbdd21SLinus Walleij static int pl022_transfer_one_message(struct spi_master *master, 1566ffbbdd21SLinus Walleij struct spi_message *msg) 1567ca632f55SGrant Likely { 1568ffbbdd21SLinus Walleij struct pl022 *pl022 = spi_master_get_devdata(master); 1569ca632f55SGrant Likely 1570ffbbdd21SLinus Walleij /* Initial message state */ 1571ffbbdd21SLinus Walleij pl022->cur_msg = msg; 1572ffbbdd21SLinus Walleij msg->state = STATE_START; 1573ffbbdd21SLinus Walleij 1574ffbbdd21SLinus Walleij pl022->cur_transfer = list_entry(msg->transfers.next, 1575ffbbdd21SLinus Walleij struct spi_transfer, transfer_list); 1576ffbbdd21SLinus Walleij 1577ffbbdd21SLinus Walleij /* Setup the SPI using the per chip configuration */ 1578ffbbdd21SLinus Walleij pl022->cur_chip = spi_get_ctldata(msg->spi); 1579f6f46de1SRoland Stigge pl022->cur_cs = pl022->chipselects[msg->spi->chip_select]; 1580ffbbdd21SLinus Walleij 1581ffbbdd21SLinus Walleij restore_state(pl022); 1582ffbbdd21SLinus Walleij flush(pl022); 1583ffbbdd21SLinus Walleij 1584ffbbdd21SLinus Walleij if (pl022->cur_chip->xfer_type == POLLING_TRANSFER) 1585ffbbdd21SLinus Walleij do_polling_transfer(pl022); 1586ffbbdd21SLinus Walleij else 1587ffbbdd21SLinus Walleij do_interrupt_dma_transfer(pl022); 1588ffbbdd21SLinus Walleij 1589ffbbdd21SLinus Walleij return 0; 1590ffbbdd21SLinus Walleij } 1591ffbbdd21SLinus Walleij 1592ffbbdd21SLinus Walleij static int pl022_unprepare_transfer_hardware(struct spi_master *master) 1593ffbbdd21SLinus Walleij { 1594ffbbdd21SLinus Walleij struct pl022 *pl022 = spi_master_get_devdata(master); 1595ffbbdd21SLinus Walleij 15960ad2deeaSVirupax Sadashivpetimath /* nothing more to do - disable spi/ssp and power off */ 15970ad2deeaSVirupax Sadashivpetimath writew((readw(SSP_CR1(pl022->virtbase)) & 15980ad2deeaSVirupax Sadashivpetimath (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase)); 159953e4aceaSChris Blair 1600ca632f55SGrant Likely return 0; 1601ca632f55SGrant Likely } 1602ca632f55SGrant Likely 1603ca632f55SGrant Likely static int verify_controller_parameters(struct pl022 *pl022, 1604ca632f55SGrant Likely struct pl022_config_chip const *chip_info) 1605ca632f55SGrant Likely { 1606ca632f55SGrant Likely if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI) 1607ca632f55SGrant Likely || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) { 1608ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1609ca632f55SGrant Likely "interface is configured incorrectly\n"); 1610ca632f55SGrant Likely return -EINVAL; 1611ca632f55SGrant Likely } 1612ca632f55SGrant Likely if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) && 1613ca632f55SGrant Likely (!pl022->vendor->unidir)) { 1614ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1615ca632f55SGrant Likely "unidirectional mode not supported in this " 1616ca632f55SGrant Likely "hardware version\n"); 1617ca632f55SGrant Likely return -EINVAL; 1618ca632f55SGrant Likely } 1619ca632f55SGrant Likely if ((chip_info->hierarchy != SSP_MASTER) 1620ca632f55SGrant Likely && (chip_info->hierarchy != SSP_SLAVE)) { 1621ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1622ca632f55SGrant Likely "hierarchy is configured incorrectly\n"); 1623ca632f55SGrant Likely return -EINVAL; 1624ca632f55SGrant Likely } 1625ca632f55SGrant Likely if ((chip_info->com_mode != INTERRUPT_TRANSFER) 1626ca632f55SGrant Likely && (chip_info->com_mode != DMA_TRANSFER) 1627ca632f55SGrant Likely && (chip_info->com_mode != POLLING_TRANSFER)) { 1628ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1629ca632f55SGrant Likely "Communication mode is configured incorrectly\n"); 1630ca632f55SGrant Likely return -EINVAL; 1631ca632f55SGrant Likely } 163278b2b911SLinus Walleij switch (chip_info->rx_lev_trig) { 163378b2b911SLinus Walleij case SSP_RX_1_OR_MORE_ELEM: 163478b2b911SLinus Walleij case SSP_RX_4_OR_MORE_ELEM: 163578b2b911SLinus Walleij case SSP_RX_8_OR_MORE_ELEM: 163678b2b911SLinus Walleij /* These are always OK, all variants can handle this */ 163778b2b911SLinus Walleij break; 163878b2b911SLinus Walleij case SSP_RX_16_OR_MORE_ELEM: 163978b2b911SLinus Walleij if (pl022->vendor->fifodepth < 16) { 1640ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1641ca632f55SGrant Likely "RX FIFO Trigger Level is configured incorrectly\n"); 1642ca632f55SGrant Likely return -EINVAL; 1643ca632f55SGrant Likely } 164478b2b911SLinus Walleij break; 164578b2b911SLinus Walleij case SSP_RX_32_OR_MORE_ELEM: 164678b2b911SLinus Walleij if (pl022->vendor->fifodepth < 32) { 164778b2b911SLinus Walleij dev_err(&pl022->adev->dev, 164878b2b911SLinus Walleij "RX FIFO Trigger Level is configured incorrectly\n"); 164978b2b911SLinus Walleij return -EINVAL; 165078b2b911SLinus Walleij } 165178b2b911SLinus Walleij break; 165278b2b911SLinus Walleij default: 165378b2b911SLinus Walleij dev_err(&pl022->adev->dev, 165478b2b911SLinus Walleij "RX FIFO Trigger Level is configured incorrectly\n"); 165578b2b911SLinus Walleij return -EINVAL; 165678b2b911SLinus Walleij } 165778b2b911SLinus Walleij switch (chip_info->tx_lev_trig) { 165878b2b911SLinus Walleij case SSP_TX_1_OR_MORE_EMPTY_LOC: 165978b2b911SLinus Walleij case SSP_TX_4_OR_MORE_EMPTY_LOC: 166078b2b911SLinus Walleij case SSP_TX_8_OR_MORE_EMPTY_LOC: 166178b2b911SLinus Walleij /* These are always OK, all variants can handle this */ 166278b2b911SLinus Walleij break; 166378b2b911SLinus Walleij case SSP_TX_16_OR_MORE_EMPTY_LOC: 166478b2b911SLinus Walleij if (pl022->vendor->fifodepth < 16) { 1665ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1666ca632f55SGrant Likely "TX FIFO Trigger Level is configured incorrectly\n"); 1667ca632f55SGrant Likely return -EINVAL; 1668ca632f55SGrant Likely } 166978b2b911SLinus Walleij break; 167078b2b911SLinus Walleij case SSP_TX_32_OR_MORE_EMPTY_LOC: 167178b2b911SLinus Walleij if (pl022->vendor->fifodepth < 32) { 167278b2b911SLinus Walleij dev_err(&pl022->adev->dev, 167378b2b911SLinus Walleij "TX FIFO Trigger Level is configured incorrectly\n"); 167478b2b911SLinus Walleij return -EINVAL; 167578b2b911SLinus Walleij } 167678b2b911SLinus Walleij break; 167778b2b911SLinus Walleij default: 167878b2b911SLinus Walleij dev_err(&pl022->adev->dev, 167978b2b911SLinus Walleij "TX FIFO Trigger Level is configured incorrectly\n"); 168078b2b911SLinus Walleij return -EINVAL; 168178b2b911SLinus Walleij } 1682ca632f55SGrant Likely if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) { 1683ca632f55SGrant Likely if ((chip_info->ctrl_len < SSP_BITS_4) 1684ca632f55SGrant Likely || (chip_info->ctrl_len > SSP_BITS_32)) { 1685ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1686ca632f55SGrant Likely "CTRL LEN is configured incorrectly\n"); 1687ca632f55SGrant Likely return -EINVAL; 1688ca632f55SGrant Likely } 1689ca632f55SGrant Likely if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO) 1690ca632f55SGrant Likely && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) { 1691ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1692ca632f55SGrant Likely "Wait State is configured incorrectly\n"); 1693ca632f55SGrant Likely return -EINVAL; 1694ca632f55SGrant Likely } 1695ca632f55SGrant Likely /* Half duplex is only available in the ST Micro version */ 1696ca632f55SGrant Likely if (pl022->vendor->extended_cr) { 1697ca632f55SGrant Likely if ((chip_info->duplex != 1698ca632f55SGrant Likely SSP_MICROWIRE_CHANNEL_FULL_DUPLEX) 1699ca632f55SGrant Likely && (chip_info->duplex != 1700ca632f55SGrant Likely SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) { 1701ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1702ca632f55SGrant Likely "Microwire duplex mode is configured incorrectly\n"); 1703ca632f55SGrant Likely return -EINVAL; 1704ca632f55SGrant Likely } 1705ca632f55SGrant Likely } else { 1706ca632f55SGrant Likely if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX) 1707ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1708ca632f55SGrant Likely "Microwire half duplex mode requested," 1709ca632f55SGrant Likely " but this is only available in the" 1710ca632f55SGrant Likely " ST version of PL022\n"); 1711ca632f55SGrant Likely return -EINVAL; 1712ca632f55SGrant Likely } 1713ca632f55SGrant Likely } 1714ca632f55SGrant Likely return 0; 1715ca632f55SGrant Likely } 1716ca632f55SGrant Likely 17170379b2a3SViresh Kumar static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr) 17180379b2a3SViresh Kumar { 17190379b2a3SViresh Kumar return rate / (cpsdvsr * (1 + scr)); 17200379b2a3SViresh Kumar } 17210379b2a3SViresh Kumar 17220379b2a3SViresh Kumar static int calculate_effective_freq(struct pl022 *pl022, int freq, struct 17230379b2a3SViresh Kumar ssp_clock_params * clk_freq) 1724ca632f55SGrant Likely { 1725ca632f55SGrant Likely /* Lets calculate the frequency parameters */ 17260379b2a3SViresh Kumar u16 cpsdvsr = CPSDVR_MIN, scr = SCR_MIN; 17270379b2a3SViresh Kumar u32 rate, max_tclk, min_tclk, best_freq = 0, best_cpsdvsr = 0, 17280379b2a3SViresh Kumar best_scr = 0, tmp, found = 0; 1729ca632f55SGrant Likely 1730ca632f55SGrant Likely rate = clk_get_rate(pl022->clk); 1731ca632f55SGrant Likely /* cpsdvscr = 2 & scr 0 */ 17320379b2a3SViresh Kumar max_tclk = spi_rate(rate, CPSDVR_MIN, SCR_MIN); 1733ca632f55SGrant Likely /* cpsdvsr = 254 & scr = 255 */ 17340379b2a3SViresh Kumar min_tclk = spi_rate(rate, CPSDVR_MAX, SCR_MAX); 1735ca632f55SGrant Likely 1736ea505bc9SViresh Kumar if (freq > max_tclk) 1737ea505bc9SViresh Kumar dev_warn(&pl022->adev->dev, 1738ea505bc9SViresh Kumar "Max speed that can be programmed is %d Hz, you requested %d\n", 1739ea505bc9SViresh Kumar max_tclk, freq); 1740ea505bc9SViresh Kumar 1741ea505bc9SViresh Kumar if (freq < min_tclk) { 1742ca632f55SGrant Likely dev_err(&pl022->adev->dev, 1743ea505bc9SViresh Kumar "Requested frequency: %d Hz is less than minimum possible %d Hz\n", 1744ea505bc9SViresh Kumar freq, min_tclk); 1745ca632f55SGrant Likely return -EINVAL; 1746ca632f55SGrant Likely } 17470379b2a3SViresh Kumar 17480379b2a3SViresh Kumar /* 17490379b2a3SViresh Kumar * best_freq will give closest possible available rate (<= requested 17500379b2a3SViresh Kumar * freq) for all values of scr & cpsdvsr. 17510379b2a3SViresh Kumar */ 17520379b2a3SViresh Kumar while ((cpsdvsr <= CPSDVR_MAX) && !found) { 17530379b2a3SViresh Kumar while (scr <= SCR_MAX) { 17540379b2a3SViresh Kumar tmp = spi_rate(rate, cpsdvsr, scr); 17550379b2a3SViresh Kumar 17565eb806a3SViresh Kumar if (tmp > freq) { 17575eb806a3SViresh Kumar /* we need lower freq */ 17580379b2a3SViresh Kumar scr++; 17595eb806a3SViresh Kumar continue; 17605eb806a3SViresh Kumar } 17615eb806a3SViresh Kumar 17620379b2a3SViresh Kumar /* 17635eb806a3SViresh Kumar * If found exact value, mark found and break. 17645eb806a3SViresh Kumar * If found more closer value, update and break. 17650379b2a3SViresh Kumar */ 17665eb806a3SViresh Kumar if (tmp > best_freq) { 17670379b2a3SViresh Kumar best_freq = tmp; 17680379b2a3SViresh Kumar best_cpsdvsr = cpsdvsr; 17690379b2a3SViresh Kumar best_scr = scr; 17700379b2a3SViresh Kumar 17710379b2a3SViresh Kumar if (tmp == freq) 17725eb806a3SViresh Kumar found = 1; 17730379b2a3SViresh Kumar } 17745eb806a3SViresh Kumar /* 17755eb806a3SViresh Kumar * increased scr will give lower rates, which are not 17765eb806a3SViresh Kumar * required 17775eb806a3SViresh Kumar */ 17785eb806a3SViresh Kumar break; 17790379b2a3SViresh Kumar } 17800379b2a3SViresh Kumar cpsdvsr += 2; 17810379b2a3SViresh Kumar scr = SCR_MIN; 1782ca632f55SGrant Likely } 1783ca632f55SGrant Likely 17845eb806a3SViresh Kumar WARN(!best_freq, "pl022: Matching cpsdvsr and scr not found for %d Hz rate \n", 17855eb806a3SViresh Kumar freq); 17865eb806a3SViresh Kumar 17870379b2a3SViresh Kumar clk_freq->cpsdvsr = (u8) (best_cpsdvsr & 0xFF); 17880379b2a3SViresh Kumar clk_freq->scr = (u8) (best_scr & 0xFF); 17890379b2a3SViresh Kumar dev_dbg(&pl022->adev->dev, 17900379b2a3SViresh Kumar "SSP Target Frequency is: %u, Effective Frequency is %u\n", 17910379b2a3SViresh Kumar freq, best_freq); 17920379b2a3SViresh Kumar dev_dbg(&pl022->adev->dev, "SSP cpsdvsr = %d, scr = %d\n", 17930379b2a3SViresh Kumar clk_freq->cpsdvsr, clk_freq->scr); 17940379b2a3SViresh Kumar 1795ca632f55SGrant Likely return 0; 1796ca632f55SGrant Likely } 1797ca632f55SGrant Likely 1798ca632f55SGrant Likely /* 1799ca632f55SGrant Likely * A piece of default chip info unless the platform 1800ca632f55SGrant Likely * supplies it. 1801ca632f55SGrant Likely */ 1802ca632f55SGrant Likely static const struct pl022_config_chip pl022_default_chip_info = { 1803ca632f55SGrant Likely .com_mode = POLLING_TRANSFER, 1804ca632f55SGrant Likely .iface = SSP_INTERFACE_MOTOROLA_SPI, 1805ca632f55SGrant Likely .hierarchy = SSP_SLAVE, 1806ca632f55SGrant Likely .slave_tx_disable = DO_NOT_DRIVE_TX, 1807ca632f55SGrant Likely .rx_lev_trig = SSP_RX_1_OR_MORE_ELEM, 1808ca632f55SGrant Likely .tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC, 1809ca632f55SGrant Likely .ctrl_len = SSP_BITS_8, 1810ca632f55SGrant Likely .wait_state = SSP_MWIRE_WAIT_ZERO, 1811ca632f55SGrant Likely .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, 1812ca632f55SGrant Likely .cs_control = null_cs_control, 1813ca632f55SGrant Likely }; 1814ca632f55SGrant Likely 1815ca632f55SGrant Likely /** 1816ca632f55SGrant Likely * pl022_setup - setup function registered to SPI master framework 1817ca632f55SGrant Likely * @spi: spi device which is requesting setup 1818ca632f55SGrant Likely * 1819ca632f55SGrant Likely * This function is registered to the SPI framework for this SPI master 1820ca632f55SGrant Likely * controller. If it is the first time when setup is called by this device, 1821ca632f55SGrant Likely * this function will initialize the runtime state for this chip and save 1822ca632f55SGrant Likely * the same in the device structure. Else it will update the runtime info 1823ca632f55SGrant Likely * with the updated chip info. Nothing is really being written to the 1824ca632f55SGrant Likely * controller hardware here, that is not done until the actual transfer 1825ca632f55SGrant Likely * commence. 1826ca632f55SGrant Likely */ 1827ca632f55SGrant Likely static int pl022_setup(struct spi_device *spi) 1828ca632f55SGrant Likely { 1829ca632f55SGrant Likely struct pl022_config_chip const *chip_info; 18306d3952a7SRoland Stigge struct pl022_config_chip chip_info_dt; 1831ca632f55SGrant Likely struct chip_data *chip; 1832c4a47843SJonas Aaberg struct ssp_clock_params clk_freq = { .cpsdvsr = 0, .scr = 0}; 1833ca632f55SGrant Likely int status = 0; 1834ca632f55SGrant Likely struct pl022 *pl022 = spi_master_get_devdata(spi->master); 1835ca632f55SGrant Likely unsigned int bits = spi->bits_per_word; 1836ca632f55SGrant Likely u32 tmp; 18376d3952a7SRoland Stigge struct device_node *np = spi->dev.of_node; 1838ca632f55SGrant Likely 1839ca632f55SGrant Likely if (!spi->max_speed_hz) 1840ca632f55SGrant Likely return -EINVAL; 1841ca632f55SGrant Likely 1842ca632f55SGrant Likely /* Get controller_state if one is supplied */ 1843ca632f55SGrant Likely chip = spi_get_ctldata(spi); 1844ca632f55SGrant Likely 1845ca632f55SGrant Likely if (chip == NULL) { 1846ca632f55SGrant Likely chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); 184777538f4aSJingoo Han if (!chip) 1848ca632f55SGrant Likely return -ENOMEM; 1849ca632f55SGrant Likely dev_dbg(&spi->dev, 1850ca632f55SGrant Likely "allocated memory for controller's runtime state\n"); 1851ca632f55SGrant Likely } 1852ca632f55SGrant Likely 1853ca632f55SGrant Likely /* Get controller data if one is supplied */ 1854ca632f55SGrant Likely chip_info = spi->controller_data; 1855ca632f55SGrant Likely 1856ca632f55SGrant Likely if (chip_info == NULL) { 18576d3952a7SRoland Stigge if (np) { 18586d3952a7SRoland Stigge chip_info_dt = pl022_default_chip_info; 18596d3952a7SRoland Stigge 18606d3952a7SRoland Stigge chip_info_dt.hierarchy = SSP_MASTER; 18616d3952a7SRoland Stigge of_property_read_u32(np, "pl022,interface", 18626d3952a7SRoland Stigge &chip_info_dt.iface); 18636d3952a7SRoland Stigge of_property_read_u32(np, "pl022,com-mode", 18646d3952a7SRoland Stigge &chip_info_dt.com_mode); 18656d3952a7SRoland Stigge of_property_read_u32(np, "pl022,rx-level-trig", 18666d3952a7SRoland Stigge &chip_info_dt.rx_lev_trig); 18676d3952a7SRoland Stigge of_property_read_u32(np, "pl022,tx-level-trig", 18686d3952a7SRoland Stigge &chip_info_dt.tx_lev_trig); 18696d3952a7SRoland Stigge of_property_read_u32(np, "pl022,ctrl-len", 18706d3952a7SRoland Stigge &chip_info_dt.ctrl_len); 18716d3952a7SRoland Stigge of_property_read_u32(np, "pl022,wait-state", 18726d3952a7SRoland Stigge &chip_info_dt.wait_state); 18736d3952a7SRoland Stigge of_property_read_u32(np, "pl022,duplex", 18746d3952a7SRoland Stigge &chip_info_dt.duplex); 18756d3952a7SRoland Stigge 18766d3952a7SRoland Stigge chip_info = &chip_info_dt; 18776d3952a7SRoland Stigge } else { 1878ca632f55SGrant Likely chip_info = &pl022_default_chip_info; 1879ca632f55SGrant Likely /* spi_board_info.controller_data not is supplied */ 1880ca632f55SGrant Likely dev_dbg(&spi->dev, 1881ca632f55SGrant Likely "using default controller_data settings\n"); 18826d3952a7SRoland Stigge } 1883ca632f55SGrant Likely } else 1884ca632f55SGrant Likely dev_dbg(&spi->dev, 1885ca632f55SGrant Likely "using user supplied controller_data settings\n"); 1886ca632f55SGrant Likely 1887ca632f55SGrant Likely /* 1888ca632f55SGrant Likely * We can override with custom divisors, else we use the board 1889ca632f55SGrant Likely * frequency setting 1890ca632f55SGrant Likely */ 1891ca632f55SGrant Likely if ((0 == chip_info->clk_freq.cpsdvsr) 1892ca632f55SGrant Likely && (0 == chip_info->clk_freq.scr)) { 1893ca632f55SGrant Likely status = calculate_effective_freq(pl022, 1894ca632f55SGrant Likely spi->max_speed_hz, 1895ca632f55SGrant Likely &clk_freq); 1896ca632f55SGrant Likely if (status < 0) 1897ca632f55SGrant Likely goto err_config_params; 1898ca632f55SGrant Likely } else { 1899ca632f55SGrant Likely memcpy(&clk_freq, &chip_info->clk_freq, sizeof(clk_freq)); 1900ca632f55SGrant Likely if ((clk_freq.cpsdvsr % 2) != 0) 1901ca632f55SGrant Likely clk_freq.cpsdvsr = 1902ca632f55SGrant Likely clk_freq.cpsdvsr - 1; 1903ca632f55SGrant Likely } 1904ca632f55SGrant Likely if ((clk_freq.cpsdvsr < CPSDVR_MIN) 1905ca632f55SGrant Likely || (clk_freq.cpsdvsr > CPSDVR_MAX)) { 1906f8db4cc4SGrant Likely status = -EINVAL; 1907ca632f55SGrant Likely dev_err(&spi->dev, 1908ca632f55SGrant Likely "cpsdvsr is configured incorrectly\n"); 1909ca632f55SGrant Likely goto err_config_params; 1910ca632f55SGrant Likely } 1911ca632f55SGrant Likely 1912ca632f55SGrant Likely status = verify_controller_parameters(pl022, chip_info); 1913ca632f55SGrant Likely if (status) { 1914ca632f55SGrant Likely dev_err(&spi->dev, "controller data is incorrect"); 1915ca632f55SGrant Likely goto err_config_params; 1916ca632f55SGrant Likely } 1917ca632f55SGrant Likely 1918083be3f0SLinus Walleij pl022->rx_lev_trig = chip_info->rx_lev_trig; 1919083be3f0SLinus Walleij pl022->tx_lev_trig = chip_info->tx_lev_trig; 1920083be3f0SLinus Walleij 1921ca632f55SGrant Likely /* Now set controller state based on controller data */ 1922ca632f55SGrant Likely chip->xfer_type = chip_info->com_mode; 1923ca632f55SGrant Likely if (!chip_info->cs_control) { 1924ca632f55SGrant Likely chip->cs_control = null_cs_control; 1925f6f46de1SRoland Stigge if (!gpio_is_valid(pl022->chipselects[spi->chip_select])) 1926ca632f55SGrant Likely dev_warn(&spi->dev, 1927f6f46de1SRoland Stigge "invalid chip select\n"); 1928ca632f55SGrant Likely } else 1929ca632f55SGrant Likely chip->cs_control = chip_info->cs_control; 1930ca632f55SGrant Likely 1931eb798c64SVinit Shenoy /* Check bits per word with vendor specific range */ 1932eb798c64SVinit Shenoy if ((bits <= 3) || (bits > pl022->vendor->max_bpw)) { 1933ca632f55SGrant Likely status = -ENOTSUPP; 1934eb798c64SVinit Shenoy dev_err(&spi->dev, "illegal data size for this controller!\n"); 1935eb798c64SVinit Shenoy dev_err(&spi->dev, "This controller can only handle 4 <= n <= %d bit words\n", 1936eb798c64SVinit Shenoy pl022->vendor->max_bpw); 1937ca632f55SGrant Likely goto err_config_params; 1938ca632f55SGrant Likely } else if (bits <= 8) { 1939ca632f55SGrant Likely dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n"); 1940ca632f55SGrant Likely chip->n_bytes = 1; 1941ca632f55SGrant Likely chip->read = READING_U8; 1942ca632f55SGrant Likely chip->write = WRITING_U8; 1943ca632f55SGrant Likely } else if (bits <= 16) { 1944ca632f55SGrant Likely dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n"); 1945ca632f55SGrant Likely chip->n_bytes = 2; 1946ca632f55SGrant Likely chip->read = READING_U16; 1947ca632f55SGrant Likely chip->write = WRITING_U16; 1948ca632f55SGrant Likely } else { 1949ca632f55SGrant Likely dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n"); 1950ca632f55SGrant Likely chip->n_bytes = 4; 1951ca632f55SGrant Likely chip->read = READING_U32; 1952ca632f55SGrant Likely chip->write = WRITING_U32; 1953ca632f55SGrant Likely } 1954ca632f55SGrant Likely 1955ca632f55SGrant Likely /* Now Initialize all register settings required for this chip */ 1956ca632f55SGrant Likely chip->cr0 = 0; 1957ca632f55SGrant Likely chip->cr1 = 0; 1958ca632f55SGrant Likely chip->dmacr = 0; 1959ca632f55SGrant Likely chip->cpsr = 0; 1960ca632f55SGrant Likely if ((chip_info->com_mode == DMA_TRANSFER) 1961ca632f55SGrant Likely && ((pl022->master_info)->enable_dma)) { 1962ca632f55SGrant Likely chip->enable_dma = true; 1963ca632f55SGrant Likely dev_dbg(&spi->dev, "DMA mode set in controller state\n"); 1964ca632f55SGrant Likely SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED, 1965ca632f55SGrant Likely SSP_DMACR_MASK_RXDMAE, 0); 1966ca632f55SGrant Likely SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED, 1967ca632f55SGrant Likely SSP_DMACR_MASK_TXDMAE, 1); 1968ca632f55SGrant Likely } else { 1969ca632f55SGrant Likely chip->enable_dma = false; 1970ca632f55SGrant Likely dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n"); 1971ca632f55SGrant Likely SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED, 1972ca632f55SGrant Likely SSP_DMACR_MASK_RXDMAE, 0); 1973ca632f55SGrant Likely SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED, 1974ca632f55SGrant Likely SSP_DMACR_MASK_TXDMAE, 1); 1975ca632f55SGrant Likely } 1976ca632f55SGrant Likely 1977ca632f55SGrant Likely chip->cpsr = clk_freq.cpsdvsr; 1978ca632f55SGrant Likely 1979ca632f55SGrant Likely /* Special setup for the ST micro extended control registers */ 1980ca632f55SGrant Likely if (pl022->vendor->extended_cr) { 1981ca632f55SGrant Likely u32 etx; 1982ca632f55SGrant Likely 1983ca632f55SGrant Likely if (pl022->vendor->pl023) { 1984ca632f55SGrant Likely /* These bits are only in the PL023 */ 1985ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay, 1986ca632f55SGrant Likely SSP_CR1_MASK_FBCLKDEL_ST, 13); 1987ca632f55SGrant Likely } else { 1988ca632f55SGrant Likely /* These bits are in the PL022 but not PL023 */ 1989ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr0, chip_info->duplex, 1990ca632f55SGrant Likely SSP_CR0_MASK_HALFDUP_ST, 5); 1991ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len, 1992ca632f55SGrant Likely SSP_CR0_MASK_CSS_ST, 16); 1993ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr0, chip_info->iface, 1994ca632f55SGrant Likely SSP_CR0_MASK_FRF_ST, 21); 1995ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr1, chip_info->wait_state, 1996ca632f55SGrant Likely SSP_CR1_MASK_MWAIT_ST, 6); 1997ca632f55SGrant Likely } 1998ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr0, bits - 1, 1999ca632f55SGrant Likely SSP_CR0_MASK_DSS_ST, 0); 2000ca632f55SGrant Likely 2001ca632f55SGrant Likely if (spi->mode & SPI_LSB_FIRST) { 2002ca632f55SGrant Likely tmp = SSP_RX_LSB; 2003ca632f55SGrant Likely etx = SSP_TX_LSB; 2004ca632f55SGrant Likely } else { 2005ca632f55SGrant Likely tmp = SSP_RX_MSB; 2006ca632f55SGrant Likely etx = SSP_TX_MSB; 2007ca632f55SGrant Likely } 2008ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_RENDN_ST, 4); 2009ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr1, etx, SSP_CR1_MASK_TENDN_ST, 5); 2010ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig, 2011ca632f55SGrant Likely SSP_CR1_MASK_RXIFLSEL_ST, 7); 2012ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig, 2013ca632f55SGrant Likely SSP_CR1_MASK_TXIFLSEL_ST, 10); 2014ca632f55SGrant Likely } else { 2015ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr0, bits - 1, 2016ca632f55SGrant Likely SSP_CR0_MASK_DSS, 0); 2017ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr0, chip_info->iface, 2018ca632f55SGrant Likely SSP_CR0_MASK_FRF, 4); 2019ca632f55SGrant Likely } 2020ca632f55SGrant Likely 2021ca632f55SGrant Likely /* Stuff that is common for all versions */ 2022ca632f55SGrant Likely if (spi->mode & SPI_CPOL) 2023ca632f55SGrant Likely tmp = SSP_CLK_POL_IDLE_HIGH; 2024ca632f55SGrant Likely else 2025ca632f55SGrant Likely tmp = SSP_CLK_POL_IDLE_LOW; 2026ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPO, 6); 2027ca632f55SGrant Likely 2028ca632f55SGrant Likely if (spi->mode & SPI_CPHA) 2029ca632f55SGrant Likely tmp = SSP_CLK_SECOND_EDGE; 2030ca632f55SGrant Likely else 2031ca632f55SGrant Likely tmp = SSP_CLK_FIRST_EDGE; 2032ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPH, 7); 2033ca632f55SGrant Likely 2034ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr0, clk_freq.scr, SSP_CR0_MASK_SCR, 8); 2035ca632f55SGrant Likely /* Loopback is available on all versions except PL023 */ 2036ca632f55SGrant Likely if (pl022->vendor->loopback) { 2037ca632f55SGrant Likely if (spi->mode & SPI_LOOP) 2038ca632f55SGrant Likely tmp = LOOPBACK_ENABLED; 2039ca632f55SGrant Likely else 2040ca632f55SGrant Likely tmp = LOOPBACK_DISABLED; 2041ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_LBM, 0); 2042ca632f55SGrant Likely } 2043ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1); 2044ca632f55SGrant Likely SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2); 2045f1e45f86SViresh Kumar SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD, 2046f1e45f86SViresh Kumar 3); 2047ca632f55SGrant Likely 2048ca632f55SGrant Likely /* Save controller_state */ 2049ca632f55SGrant Likely spi_set_ctldata(spi, chip); 2050ca632f55SGrant Likely return status; 2051ca632f55SGrant Likely err_config_params: 2052ca632f55SGrant Likely spi_set_ctldata(spi, NULL); 2053ca632f55SGrant Likely kfree(chip); 2054ca632f55SGrant Likely return status; 2055ca632f55SGrant Likely } 2056ca632f55SGrant Likely 2057ca632f55SGrant Likely /** 2058ca632f55SGrant Likely * pl022_cleanup - cleanup function registered to SPI master framework 2059ca632f55SGrant Likely * @spi: spi device which is requesting cleanup 2060ca632f55SGrant Likely * 2061ca632f55SGrant Likely * This function is registered to the SPI framework for this SPI master 2062ca632f55SGrant Likely * controller. It will free the runtime state of chip. 2063ca632f55SGrant Likely */ 2064ca632f55SGrant Likely static void pl022_cleanup(struct spi_device *spi) 2065ca632f55SGrant Likely { 2066ca632f55SGrant Likely struct chip_data *chip = spi_get_ctldata(spi); 2067ca632f55SGrant Likely 2068ca632f55SGrant Likely spi_set_ctldata(spi, NULL); 2069ca632f55SGrant Likely kfree(chip); 2070ca632f55SGrant Likely } 2071ca632f55SGrant Likely 207239a6ac11SRoland Stigge static struct pl022_ssp_controller * 207339a6ac11SRoland Stigge pl022_platform_data_dt_get(struct device *dev) 207439a6ac11SRoland Stigge { 207539a6ac11SRoland Stigge struct device_node *np = dev->of_node; 207639a6ac11SRoland Stigge struct pl022_ssp_controller *pd; 2077849794c5SRabin Vincent u32 tmp = 0; 207839a6ac11SRoland Stigge 207939a6ac11SRoland Stigge if (!np) { 208039a6ac11SRoland Stigge dev_err(dev, "no dt node defined\n"); 208139a6ac11SRoland Stigge return NULL; 208239a6ac11SRoland Stigge } 208339a6ac11SRoland Stigge 208439a6ac11SRoland Stigge pd = devm_kzalloc(dev, sizeof(struct pl022_ssp_controller), GFP_KERNEL); 208577538f4aSJingoo Han if (!pd) 208639a6ac11SRoland Stigge return NULL; 208739a6ac11SRoland Stigge 208839a6ac11SRoland Stigge pd->bus_id = -1; 2089dbd897b9SLinus Walleij pd->enable_dma = 1; 209039a6ac11SRoland Stigge of_property_read_u32(np, "num-cs", &tmp); 209139a6ac11SRoland Stigge pd->num_chipselect = tmp; 209239a6ac11SRoland Stigge of_property_read_u32(np, "pl022,autosuspend-delay", 209339a6ac11SRoland Stigge &pd->autosuspend_delay); 209439a6ac11SRoland Stigge pd->rt = of_property_read_bool(np, "pl022,rt"); 209539a6ac11SRoland Stigge 209639a6ac11SRoland Stigge return pd; 209739a6ac11SRoland Stigge } 209839a6ac11SRoland Stigge 2099fd4a319bSGrant Likely static int pl022_probe(struct amba_device *adev, const struct amba_id *id) 2100ca632f55SGrant Likely { 2101ca632f55SGrant Likely struct device *dev = &adev->dev; 21028074cf06SJingoo Han struct pl022_ssp_controller *platform_info = 21038074cf06SJingoo Han dev_get_platdata(&adev->dev); 2104ca632f55SGrant Likely struct spi_master *master; 2105ca632f55SGrant Likely struct pl022 *pl022 = NULL; /*Data for this driver */ 21066d3952a7SRoland Stigge struct device_node *np = adev->dev.of_node; 21076d3952a7SRoland Stigge int status = 0, i, num_cs; 2108ca632f55SGrant Likely 2109ca632f55SGrant Likely dev_info(&adev->dev, 2110ca632f55SGrant Likely "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid); 211139a6ac11SRoland Stigge if (!platform_info && IS_ENABLED(CONFIG_OF)) 211239a6ac11SRoland Stigge platform_info = pl022_platform_data_dt_get(dev); 211339a6ac11SRoland Stigge 211439a6ac11SRoland Stigge if (!platform_info) { 211539a6ac11SRoland Stigge dev_err(dev, "probe: no platform data defined\n"); 2116aeef9915SLinus Walleij return -ENODEV; 2117ca632f55SGrant Likely } 2118ca632f55SGrant Likely 21196d3952a7SRoland Stigge if (platform_info->num_chipselect) { 21206d3952a7SRoland Stigge num_cs = platform_info->num_chipselect; 21216d3952a7SRoland Stigge } else { 212239a6ac11SRoland Stigge dev_err(dev, "probe: no chip select defined\n"); 2123aeef9915SLinus Walleij return -ENODEV; 21246d3952a7SRoland Stigge } 21256d3952a7SRoland Stigge 2126ca632f55SGrant Likely /* Allocate master with space for data */ 2127b4b84826SRoland Stigge master = spi_alloc_master(dev, sizeof(struct pl022)); 2128ca632f55SGrant Likely if (master == NULL) { 2129ca632f55SGrant Likely dev_err(&adev->dev, "probe - cannot alloc SPI master\n"); 2130aeef9915SLinus Walleij return -ENOMEM; 2131ca632f55SGrant Likely } 2132ca632f55SGrant Likely 2133ca632f55SGrant Likely pl022 = spi_master_get_devdata(master); 2134ca632f55SGrant Likely pl022->master = master; 2135ca632f55SGrant Likely pl022->master_info = platform_info; 2136ca632f55SGrant Likely pl022->adev = adev; 2137ca632f55SGrant Likely pl022->vendor = id->data; 2138b4b84826SRoland Stigge pl022->chipselects = devm_kzalloc(dev, num_cs * sizeof(int), 2139b4b84826SRoland Stigge GFP_KERNEL); 214073e3f1ebSKiran Padwal if (!pl022->chipselects) { 214173e3f1ebSKiran Padwal status = -ENOMEM; 214273e3f1ebSKiran Padwal goto err_no_mem; 214373e3f1ebSKiran Padwal } 2144ca632f55SGrant Likely 2145ca632f55SGrant Likely /* 2146ca632f55SGrant Likely * Bus Number Which has been Assigned to this SSP controller 2147ca632f55SGrant Likely * on this board 2148ca632f55SGrant Likely */ 2149ca632f55SGrant Likely master->bus_num = platform_info->bus_id; 21506d3952a7SRoland Stigge master->num_chipselect = num_cs; 2151ca632f55SGrant Likely master->cleanup = pl022_cleanup; 2152ca632f55SGrant Likely master->setup = pl022_setup; 215329b6e906SMark Brown master->auto_runtime_pm = true; 2154ffbbdd21SLinus Walleij master->transfer_one_message = pl022_transfer_one_message; 2155ffbbdd21SLinus Walleij master->unprepare_transfer_hardware = pl022_unprepare_transfer_hardware; 2156ffbbdd21SLinus Walleij master->rt = platform_info->rt; 21576d3952a7SRoland Stigge master->dev.of_node = dev->of_node; 2158ca632f55SGrant Likely 21596d3952a7SRoland Stigge if (platform_info->num_chipselect && platform_info->chipselects) { 21606d3952a7SRoland Stigge for (i = 0; i < num_cs; i++) 2161f6f46de1SRoland Stigge pl022->chipselects[i] = platform_info->chipselects[i]; 2162db4fa45eSAnders Berg } else if (pl022->vendor->internal_cs_ctrl) { 2163db4fa45eSAnders Berg for (i = 0; i < num_cs; i++) 2164db4fa45eSAnders Berg pl022->chipselects[i] = i; 21656d3952a7SRoland Stigge } else if (IS_ENABLED(CONFIG_OF)) { 21666d3952a7SRoland Stigge for (i = 0; i < num_cs; i++) { 21676d3952a7SRoland Stigge int cs_gpio = of_get_named_gpio(np, "cs-gpios", i); 21686d3952a7SRoland Stigge 21696d3952a7SRoland Stigge if (cs_gpio == -EPROBE_DEFER) { 21706d3952a7SRoland Stigge status = -EPROBE_DEFER; 21716d3952a7SRoland Stigge goto err_no_gpio; 21726d3952a7SRoland Stigge } 21736d3952a7SRoland Stigge 21746d3952a7SRoland Stigge pl022->chipselects[i] = cs_gpio; 21756d3952a7SRoland Stigge 21766d3952a7SRoland Stigge if (gpio_is_valid(cs_gpio)) { 2177aeef9915SLinus Walleij if (devm_gpio_request(dev, cs_gpio, "ssp-pl022")) 21786d3952a7SRoland Stigge dev_err(&adev->dev, 21796d3952a7SRoland Stigge "could not request %d gpio\n", 21806d3952a7SRoland Stigge cs_gpio); 21816d3952a7SRoland Stigge else if (gpio_direction_output(cs_gpio, 1)) 21826d3952a7SRoland Stigge dev_err(&adev->dev, 218361e89e65SRoland Stigge "could not set gpio %d as output\n", 21846d3952a7SRoland Stigge cs_gpio); 21856d3952a7SRoland Stigge } 21866d3952a7SRoland Stigge } 21876d3952a7SRoland Stigge } 2188f6f46de1SRoland Stigge 2189ca632f55SGrant Likely /* 2190ca632f55SGrant Likely * Supports mode 0-3, loopback, and active low CS. Transfers are 2191ca632f55SGrant Likely * always MS bit first on the original pl022. 2192ca632f55SGrant Likely */ 2193ca632f55SGrant Likely master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; 2194ca632f55SGrant Likely if (pl022->vendor->extended_cr) 2195ca632f55SGrant Likely master->mode_bits |= SPI_LSB_FIRST; 2196ca632f55SGrant Likely 2197ca632f55SGrant Likely dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num); 2198ca632f55SGrant Likely 2199ca632f55SGrant Likely status = amba_request_regions(adev, NULL); 2200ca632f55SGrant Likely if (status) 2201ca632f55SGrant Likely goto err_no_ioregion; 2202ca632f55SGrant Likely 2203ca632f55SGrant Likely pl022->phybase = adev->res.start; 2204aeef9915SLinus Walleij pl022->virtbase = devm_ioremap(dev, adev->res.start, 2205aeef9915SLinus Walleij resource_size(&adev->res)); 2206ca632f55SGrant Likely if (pl022->virtbase == NULL) { 2207ca632f55SGrant Likely status = -ENOMEM; 2208ca632f55SGrant Likely goto err_no_ioremap; 2209ca632f55SGrant Likely } 22102c067509SJingoo Han dev_info(&adev->dev, "mapped registers from %pa to %p\n", 22117085f403SFabio Estevam &adev->res.start, pl022->virtbase); 2212ca632f55SGrant Likely 2213aeef9915SLinus Walleij pl022->clk = devm_clk_get(&adev->dev, NULL); 2214ca632f55SGrant Likely if (IS_ERR(pl022->clk)) { 2215ca632f55SGrant Likely status = PTR_ERR(pl022->clk); 2216ca632f55SGrant Likely dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n"); 2217ca632f55SGrant Likely goto err_no_clk; 2218ca632f55SGrant Likely } 22197ff6bcf0SRussell King 22206cac167bSUlf Hansson status = clk_prepare_enable(pl022->clk); 222171e63e74SUlf Hansson if (status) { 222271e63e74SUlf Hansson dev_err(&adev->dev, "could not enable SSP/SPI bus clock\n"); 222371e63e74SUlf Hansson goto err_no_clk_en; 222471e63e74SUlf Hansson } 222571e63e74SUlf Hansson 2226ffbbdd21SLinus Walleij /* Initialize transfer pump */ 2227ffbbdd21SLinus Walleij tasklet_init(&pl022->pump_transfers, pump_transfers, 2228ffbbdd21SLinus Walleij (unsigned long)pl022); 2229ffbbdd21SLinus Walleij 2230ca632f55SGrant Likely /* Disable SSP */ 2231ca632f55SGrant Likely writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)), 2232ca632f55SGrant Likely SSP_CR1(pl022->virtbase)); 2233ca632f55SGrant Likely load_ssp_default_config(pl022); 2234ca632f55SGrant Likely 2235aeef9915SLinus Walleij status = devm_request_irq(dev, adev->irq[0], pl022_interrupt_handler, 2236aeef9915SLinus Walleij 0, "pl022", pl022); 2237ca632f55SGrant Likely if (status < 0) { 2238ca632f55SGrant Likely dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status); 2239ca632f55SGrant Likely goto err_no_irq; 2240ca632f55SGrant Likely } 2241ca632f55SGrant Likely 2242dc715452SArnd Bergmann /* Get DMA channels, try autoconfiguration first */ 2243dc715452SArnd Bergmann status = pl022_dma_autoprobe(pl022); 2244f3d4bb33SRabin Vincent if (status == -EPROBE_DEFER) { 2245f3d4bb33SRabin Vincent dev_dbg(dev, "deferring probe to get DMA channel\n"); 2246f3d4bb33SRabin Vincent goto err_no_irq; 2247f3d4bb33SRabin Vincent } 2248dc715452SArnd Bergmann 2249dc715452SArnd Bergmann /* If that failed, use channels from platform_info */ 2250dc715452SArnd Bergmann if (status == 0) 2251dc715452SArnd Bergmann platform_info->enable_dma = 1; 2252dc715452SArnd Bergmann else if (platform_info->enable_dma) { 2253ca632f55SGrant Likely status = pl022_dma_probe(pl022); 2254ca632f55SGrant Likely if (status != 0) 2255ca632f55SGrant Likely platform_info->enable_dma = 0; 2256ca632f55SGrant Likely } 2257ca632f55SGrant Likely 2258ca632f55SGrant Likely /* Register with the SPI framework */ 2259ca632f55SGrant Likely amba_set_drvdata(adev, pl022); 226035794a77SJingoo Han status = devm_spi_register_master(&adev->dev, master); 2261ca632f55SGrant Likely if (status != 0) { 2262ca632f55SGrant Likely dev_err(&adev->dev, 2263ca632f55SGrant Likely "probe - problem registering spi master\n"); 2264ca632f55SGrant Likely goto err_spi_register; 2265ca632f55SGrant Likely } 2266ca632f55SGrant Likely dev_dbg(dev, "probe succeeded\n"); 226792b97f0aSRussell King 226892b97f0aSRussell King /* let runtime pm put suspend */ 226953e4aceaSChris Blair if (platform_info->autosuspend_delay > 0) { 227053e4aceaSChris Blair dev_info(&adev->dev, 227153e4aceaSChris Blair "will use autosuspend for runtime pm, delay %dms\n", 227253e4aceaSChris Blair platform_info->autosuspend_delay); 227353e4aceaSChris Blair pm_runtime_set_autosuspend_delay(dev, 227453e4aceaSChris Blair platform_info->autosuspend_delay); 227553e4aceaSChris Blair pm_runtime_use_autosuspend(dev); 227653e4aceaSChris Blair } 22770df34994SUlf Hansson pm_runtime_put(dev); 22780df34994SUlf Hansson 2279ca632f55SGrant Likely return 0; 2280ca632f55SGrant Likely 2281ca632f55SGrant Likely err_spi_register: 22823e3ea716SViresh Kumar if (platform_info->enable_dma) 2283ca632f55SGrant Likely pl022_dma_remove(pl022); 2284ca632f55SGrant Likely err_no_irq: 22856cac167bSUlf Hansson clk_disable_unprepare(pl022->clk); 228671e63e74SUlf Hansson err_no_clk_en: 2287ca632f55SGrant Likely err_no_clk: 2288ca632f55SGrant Likely err_no_ioremap: 2289ca632f55SGrant Likely amba_release_regions(adev); 2290ca632f55SGrant Likely err_no_ioregion: 22916d3952a7SRoland Stigge err_no_gpio: 229273e3f1ebSKiran Padwal err_no_mem: 2293ca632f55SGrant Likely spi_master_put(master); 2294ca632f55SGrant Likely return status; 2295ca632f55SGrant Likely } 2296ca632f55SGrant Likely 2297fd4a319bSGrant Likely static int 2298ca632f55SGrant Likely pl022_remove(struct amba_device *adev) 2299ca632f55SGrant Likely { 2300ca632f55SGrant Likely struct pl022 *pl022 = amba_get_drvdata(adev); 230150658b66SLinus Walleij 2302ca632f55SGrant Likely if (!pl022) 2303ca632f55SGrant Likely return 0; 2304ca632f55SGrant Likely 230592b97f0aSRussell King /* 230692b97f0aSRussell King * undo pm_runtime_put() in probe. I assume that we're not 230792b97f0aSRussell King * accessing the primecell here. 230892b97f0aSRussell King */ 230992b97f0aSRussell King pm_runtime_get_noresume(&adev->dev); 231092b97f0aSRussell King 2311ca632f55SGrant Likely load_ssp_default_config(pl022); 23123e3ea716SViresh Kumar if (pl022->master_info->enable_dma) 2313ca632f55SGrant Likely pl022_dma_remove(pl022); 23143e3ea716SViresh Kumar 23156cac167bSUlf Hansson clk_disable_unprepare(pl022->clk); 2316ca632f55SGrant Likely amba_release_regions(adev); 2317ca632f55SGrant Likely tasklet_disable(&pl022->pump_transfers); 2318ca632f55SGrant Likely return 0; 2319ca632f55SGrant Likely } 2320ca632f55SGrant Likely 232184a5dc41SUlf Hansson #ifdef CONFIG_PM_SLEEP 23226cfa6279SPeter Hüwe static int pl022_suspend(struct device *dev) 2323ca632f55SGrant Likely { 232492b97f0aSRussell King struct pl022 *pl022 = dev_get_drvdata(dev); 2325ffbbdd21SLinus Walleij int ret; 2326ca632f55SGrant Likely 2327ffbbdd21SLinus Walleij ret = spi_master_suspend(pl022->master); 2328ffbbdd21SLinus Walleij if (ret) { 2329ffbbdd21SLinus Walleij dev_warn(dev, "cannot suspend master\n"); 2330ffbbdd21SLinus Walleij return ret; 2331ca632f55SGrant Likely } 23324964a26dSUlf Hansson 233384a5dc41SUlf Hansson ret = pm_runtime_force_suspend(dev); 233484a5dc41SUlf Hansson if (ret) { 233584a5dc41SUlf Hansson spi_master_resume(pl022->master); 233684a5dc41SUlf Hansson return ret; 233784a5dc41SUlf Hansson } 233884a5dc41SUlf Hansson 233984a5dc41SUlf Hansson pinctrl_pm_select_sleep_state(dev); 2340ca632f55SGrant Likely 23416cfa6279SPeter Hüwe dev_dbg(dev, "suspended\n"); 2342ca632f55SGrant Likely return 0; 2343ca632f55SGrant Likely } 2344ca632f55SGrant Likely 234592b97f0aSRussell King static int pl022_resume(struct device *dev) 2346ca632f55SGrant Likely { 234792b97f0aSRussell King struct pl022 *pl022 = dev_get_drvdata(dev); 2348ffbbdd21SLinus Walleij int ret; 2349ca632f55SGrant Likely 235084a5dc41SUlf Hansson ret = pm_runtime_force_resume(dev); 235184a5dc41SUlf Hansson if (ret) 235284a5dc41SUlf Hansson dev_err(dev, "problem resuming\n"); 2353ada7aec7SLinus Walleij 2354ca632f55SGrant Likely /* Start the queue running */ 2355ffbbdd21SLinus Walleij ret = spi_master_resume(pl022->master); 2356ffbbdd21SLinus Walleij if (ret) 2357ffbbdd21SLinus Walleij dev_err(dev, "problem starting queue (%d)\n", ret); 2358ca632f55SGrant Likely else 235992b97f0aSRussell King dev_dbg(dev, "resumed\n"); 2360ca632f55SGrant Likely 2361ffbbdd21SLinus Walleij return ret; 2362ca632f55SGrant Likely } 236384a5dc41SUlf Hansson #endif 2364ca632f55SGrant Likely 2365736198b0SUlf Hansson #ifdef CONFIG_PM 236692b97f0aSRussell King static int pl022_runtime_suspend(struct device *dev) 236792b97f0aSRussell King { 236892b97f0aSRussell King struct pl022 *pl022 = dev_get_drvdata(dev); 236992b97f0aSRussell King 237084a5dc41SUlf Hansson clk_disable_unprepare(pl022->clk); 237184a5dc41SUlf Hansson pinctrl_pm_select_idle_state(dev); 237284a5dc41SUlf Hansson 237392b97f0aSRussell King return 0; 237492b97f0aSRussell King } 237592b97f0aSRussell King 237692b97f0aSRussell King static int pl022_runtime_resume(struct device *dev) 237792b97f0aSRussell King { 237892b97f0aSRussell King struct pl022 *pl022 = dev_get_drvdata(dev); 23794f5e1b37SPatrice Chotard 238084a5dc41SUlf Hansson pinctrl_pm_select_default_state(dev); 238184a5dc41SUlf Hansson clk_prepare_enable(pl022->clk); 238284a5dc41SUlf Hansson 238392b97f0aSRussell King return 0; 238492b97f0aSRussell King } 238592b97f0aSRussell King #endif 238692b97f0aSRussell King 238792b97f0aSRussell King static const struct dev_pm_ops pl022_dev_pm_ops = { 238892b97f0aSRussell King SET_SYSTEM_SLEEP_PM_OPS(pl022_suspend, pl022_resume) 23896ed23b80SRafael J. Wysocki SET_RUNTIME_PM_OPS(pl022_runtime_suspend, pl022_runtime_resume, NULL) 239092b97f0aSRussell King }; 239192b97f0aSRussell King 2392ca632f55SGrant Likely static struct vendor_data vendor_arm = { 2393ca632f55SGrant Likely .fifodepth = 8, 2394ca632f55SGrant Likely .max_bpw = 16, 2395ca632f55SGrant Likely .unidir = false, 2396ca632f55SGrant Likely .extended_cr = false, 2397ca632f55SGrant Likely .pl023 = false, 2398ca632f55SGrant Likely .loopback = true, 2399db4fa45eSAnders Berg .internal_cs_ctrl = false, 2400ca632f55SGrant Likely }; 2401ca632f55SGrant Likely 2402ca632f55SGrant Likely static struct vendor_data vendor_st = { 2403ca632f55SGrant Likely .fifodepth = 32, 2404ca632f55SGrant Likely .max_bpw = 32, 2405ca632f55SGrant Likely .unidir = false, 2406ca632f55SGrant Likely .extended_cr = true, 2407ca632f55SGrant Likely .pl023 = false, 2408ca632f55SGrant Likely .loopback = true, 2409db4fa45eSAnders Berg .internal_cs_ctrl = false, 2410ca632f55SGrant Likely }; 2411ca632f55SGrant Likely 2412ca632f55SGrant Likely static struct vendor_data vendor_st_pl023 = { 2413ca632f55SGrant Likely .fifodepth = 32, 2414ca632f55SGrant Likely .max_bpw = 32, 2415ca632f55SGrant Likely .unidir = false, 2416ca632f55SGrant Likely .extended_cr = true, 2417ca632f55SGrant Likely .pl023 = true, 2418ca632f55SGrant Likely .loopback = false, 2419db4fa45eSAnders Berg .internal_cs_ctrl = false, 2420db4fa45eSAnders Berg }; 2421db4fa45eSAnders Berg 2422db4fa45eSAnders Berg static struct vendor_data vendor_lsi = { 2423db4fa45eSAnders Berg .fifodepth = 8, 2424db4fa45eSAnders Berg .max_bpw = 16, 2425db4fa45eSAnders Berg .unidir = false, 2426db4fa45eSAnders Berg .extended_cr = false, 2427db4fa45eSAnders Berg .pl023 = false, 2428db4fa45eSAnders Berg .loopback = true, 2429db4fa45eSAnders Berg .internal_cs_ctrl = true, 2430ca632f55SGrant Likely }; 2431ca632f55SGrant Likely 2432*5b8d5ad2SArvind Yadav static const struct amba_id pl022_ids[] = { 2433ca632f55SGrant Likely { 2434ca632f55SGrant Likely /* 2435ca632f55SGrant Likely * ARM PL022 variant, this has a 16bit wide 2436ca632f55SGrant Likely * and 8 locations deep TX/RX FIFO 2437ca632f55SGrant Likely */ 2438ca632f55SGrant Likely .id = 0x00041022, 2439ca632f55SGrant Likely .mask = 0x000fffff, 2440ca632f55SGrant Likely .data = &vendor_arm, 2441ca632f55SGrant Likely }, 2442ca632f55SGrant Likely { 2443ca632f55SGrant Likely /* 2444ca632f55SGrant Likely * ST Micro derivative, this has 32bit wide 2445ca632f55SGrant Likely * and 32 locations deep TX/RX FIFO 2446ca632f55SGrant Likely */ 2447ca632f55SGrant Likely .id = 0x01080022, 2448ca632f55SGrant Likely .mask = 0xffffffff, 2449ca632f55SGrant Likely .data = &vendor_st, 2450ca632f55SGrant Likely }, 2451ca632f55SGrant Likely { 2452ca632f55SGrant Likely /* 2453ca632f55SGrant Likely * ST-Ericsson derivative "PL023" (this is not 2454ca632f55SGrant Likely * an official ARM number), this is a PL022 SSP block 2455ca632f55SGrant Likely * stripped to SPI mode only, it has 32bit wide 2456ca632f55SGrant Likely * and 32 locations deep TX/RX FIFO but no extended 2457ca632f55SGrant Likely * CR0/CR1 register 2458ca632f55SGrant Likely */ 2459ca632f55SGrant Likely .id = 0x00080023, 2460ca632f55SGrant Likely .mask = 0xffffffff, 2461ca632f55SGrant Likely .data = &vendor_st_pl023, 2462ca632f55SGrant Likely }, 2463db4fa45eSAnders Berg { 2464db4fa45eSAnders Berg /* 2465db4fa45eSAnders Berg * PL022 variant that has a chip select control register whih 2466db4fa45eSAnders Berg * allows control of 5 output signals nCS[0:4]. 2467db4fa45eSAnders Berg */ 2468db4fa45eSAnders Berg .id = 0x000b6022, 2469db4fa45eSAnders Berg .mask = 0x000fffff, 2470db4fa45eSAnders Berg .data = &vendor_lsi, 2471db4fa45eSAnders Berg }, 2472ca632f55SGrant Likely { 0, 0 }, 2473ca632f55SGrant Likely }; 2474ca632f55SGrant Likely 24757eeac71bSDave Martin MODULE_DEVICE_TABLE(amba, pl022_ids); 24767eeac71bSDave Martin 2477ca632f55SGrant Likely static struct amba_driver pl022_driver = { 2478ca632f55SGrant Likely .drv = { 2479ca632f55SGrant Likely .name = "ssp-pl022", 248092b97f0aSRussell King .pm = &pl022_dev_pm_ops, 2481ca632f55SGrant Likely }, 2482ca632f55SGrant Likely .id_table = pl022_ids, 2483ca632f55SGrant Likely .probe = pl022_probe, 2484fd4a319bSGrant Likely .remove = pl022_remove, 2485ca632f55SGrant Likely }; 2486ca632f55SGrant Likely 2487ca632f55SGrant Likely static int __init pl022_init(void) 2488ca632f55SGrant Likely { 2489ca632f55SGrant Likely return amba_driver_register(&pl022_driver); 2490ca632f55SGrant Likely } 2491ca632f55SGrant Likely subsys_initcall(pl022_init); 2492ca632f55SGrant Likely 2493ca632f55SGrant Likely static void __exit pl022_exit(void) 2494ca632f55SGrant Likely { 2495ca632f55SGrant Likely amba_driver_unregister(&pl022_driver); 2496ca632f55SGrant Likely } 2497ca632f55SGrant Likely module_exit(pl022_exit); 2498ca632f55SGrant Likely 2499ca632f55SGrant Likely MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>"); 2500ca632f55SGrant Likely MODULE_DESCRIPTION("PL022 SSP Controller Driver"); 2501ca632f55SGrant Likely MODULE_LICENSE("GPL"); 2502