1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely * Copyright (C) 2009 Texas Instruments.
4ca632f55SGrant Likely * Copyright (C) 2010 EF Johnson Technologies
5ca632f55SGrant Likely */
6ca632f55SGrant Likely
7ca632f55SGrant Likely #include <linux/interrupt.h>
8ca632f55SGrant Likely #include <linux/io.h>
9101a68e7SLinus Walleij #include <linux/gpio/consumer.h>
10ca632f55SGrant Likely #include <linux/module.h>
11ca632f55SGrant Likely #include <linux/delay.h>
12ca632f55SGrant Likely #include <linux/platform_device.h>
13ca632f55SGrant Likely #include <linux/err.h>
14ca632f55SGrant Likely #include <linux/clk.h>
15048177ceSMatt Porter #include <linux/dmaengine.h>
16ca632f55SGrant Likely #include <linux/dma-mapping.h>
17aae7147dSMurali Karicheri #include <linux/of.h>
18ca632f55SGrant Likely #include <linux/spi/spi.h>
19ca632f55SGrant Likely #include <linux/spi/spi_bitbang.h>
20ca632f55SGrant Likely #include <linux/slab.h>
21ca632f55SGrant Likely
22ec2a0833SArnd Bergmann #include <linux/platform_data/spi-davinci.h>
23ca632f55SGrant Likely
24ca632f55SGrant Likely #define CS_DEFAULT 0xFF
25ca632f55SGrant Likely
26ca632f55SGrant Likely #define SPIFMT_PHASE_MASK BIT(16)
27ca632f55SGrant Likely #define SPIFMT_POLARITY_MASK BIT(17)
28ca632f55SGrant Likely #define SPIFMT_DISTIMER_MASK BIT(18)
29ca632f55SGrant Likely #define SPIFMT_SHIFTDIR_MASK BIT(20)
30ca632f55SGrant Likely #define SPIFMT_WAITENA_MASK BIT(21)
31ca632f55SGrant Likely #define SPIFMT_PARITYENA_MASK BIT(22)
32ca632f55SGrant Likely #define SPIFMT_ODD_PARITY_MASK BIT(23)
33ca632f55SGrant Likely #define SPIFMT_WDELAY_MASK 0x3f000000u
34ca632f55SGrant Likely #define SPIFMT_WDELAY_SHIFT 24
35ca632f55SGrant Likely #define SPIFMT_PRESCALE_SHIFT 8
36ca632f55SGrant Likely
37ca632f55SGrant Likely /* SPIPC0 */
38ca632f55SGrant Likely #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
39ca632f55SGrant Likely #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
40ca632f55SGrant Likely #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
41ca632f55SGrant Likely #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
42ca632f55SGrant Likely
43ca632f55SGrant Likely #define SPIINT_MASKALL 0x0101035F
44ca632f55SGrant Likely #define SPIINT_MASKINT 0x0000015F
45ca632f55SGrant Likely #define SPI_INTLVL_1 0x000001FF
46ca632f55SGrant Likely #define SPI_INTLVL_0 0x00000000
47ca632f55SGrant Likely
48ca632f55SGrant Likely /* SPIDAT1 (upper 16 bit defines) */
49ca632f55SGrant Likely #define SPIDAT1_CSHOLD_MASK BIT(12)
50365a7bb3SMurali Karicheri #define SPIDAT1_WDEL BIT(10)
51ca632f55SGrant Likely
52ca632f55SGrant Likely /* SPIGCR1 */
53ca632f55SGrant Likely #define SPIGCR1_CLKMOD_MASK BIT(1)
54ca632f55SGrant Likely #define SPIGCR1_MASTER_MASK BIT(0)
55ca632f55SGrant Likely #define SPIGCR1_POWERDOWN_MASK BIT(8)
56ca632f55SGrant Likely #define SPIGCR1_LOOPBACK_MASK BIT(16)
57ca632f55SGrant Likely #define SPIGCR1_SPIENA_MASK BIT(24)
58ca632f55SGrant Likely
59ca632f55SGrant Likely /* SPIBUF */
60ca632f55SGrant Likely #define SPIBUF_TXFULL_MASK BIT(29)
61ca632f55SGrant Likely #define SPIBUF_RXEMPTY_MASK BIT(31)
62ca632f55SGrant Likely
63ca632f55SGrant Likely /* SPIDELAY */
64ca632f55SGrant Likely #define SPIDELAY_C2TDELAY_SHIFT 24
65ca632f55SGrant Likely #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
66ca632f55SGrant Likely #define SPIDELAY_T2CDELAY_SHIFT 16
67ca632f55SGrant Likely #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
68ca632f55SGrant Likely #define SPIDELAY_T2EDELAY_SHIFT 8
69ca632f55SGrant Likely #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
70ca632f55SGrant Likely #define SPIDELAY_C2EDELAY_SHIFT 0
71ca632f55SGrant Likely #define SPIDELAY_C2EDELAY_MASK 0xFF
72ca632f55SGrant Likely
73ca632f55SGrant Likely /* Error Masks */
74ca632f55SGrant Likely #define SPIFLG_DLEN_ERR_MASK BIT(0)
75ca632f55SGrant Likely #define SPIFLG_TIMEOUT_MASK BIT(1)
76ca632f55SGrant Likely #define SPIFLG_PARERR_MASK BIT(2)
77ca632f55SGrant Likely #define SPIFLG_DESYNC_MASK BIT(3)
78ca632f55SGrant Likely #define SPIFLG_BITERR_MASK BIT(4)
79ca632f55SGrant Likely #define SPIFLG_OVRRUN_MASK BIT(6)
80ca632f55SGrant Likely #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
81ca632f55SGrant Likely #define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
82ca632f55SGrant Likely | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
83ca632f55SGrant Likely | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
84ca632f55SGrant Likely | SPIFLG_OVRRUN_MASK)
85ca632f55SGrant Likely
86ca632f55SGrant Likely #define SPIINT_DMA_REQ_EN BIT(16)
87ca632f55SGrant Likely
88ca632f55SGrant Likely /* SPI Controller registers */
89ca632f55SGrant Likely #define SPIGCR0 0x00
90ca632f55SGrant Likely #define SPIGCR1 0x04
91ca632f55SGrant Likely #define SPIINT 0x08
92ca632f55SGrant Likely #define SPILVL 0x0c
93ca632f55SGrant Likely #define SPIFLG 0x10
94ca632f55SGrant Likely #define SPIPC0 0x14
95ca632f55SGrant Likely #define SPIDAT1 0x3c
96ca632f55SGrant Likely #define SPIBUF 0x40
97ca632f55SGrant Likely #define SPIDELAY 0x48
98ca632f55SGrant Likely #define SPIDEF 0x4c
99ca632f55SGrant Likely #define SPIFMT0 0x50
100ca632f55SGrant Likely
1010718b764SFrode Isaksen #define DMA_MIN_BYTES 16
1020718b764SFrode Isaksen
103ca632f55SGrant Likely /* SPI Controller driver's private data. */
104ca632f55SGrant Likely struct davinci_spi {
105ca632f55SGrant Likely struct spi_bitbang bitbang;
106ca632f55SGrant Likely struct clk *clk;
107ca632f55SGrant Likely
108ca632f55SGrant Likely u8 version;
109ca632f55SGrant Likely resource_size_t pbase;
110ca632f55SGrant Likely void __iomem *base;
111ca632f55SGrant Likely u32 irq;
112ca632f55SGrant Likely struct completion done;
113ca632f55SGrant Likely
114ca632f55SGrant Likely const void *tx;
115ca632f55SGrant Likely void *rx;
116ca632f55SGrant Likely int rcount;
117ca632f55SGrant Likely int wcount;
118048177ceSMatt Porter
119048177ceSMatt Porter struct dma_chan *dma_rx;
120048177ceSMatt Porter struct dma_chan *dma_tx;
121048177ceSMatt Porter
122aae7147dSMurali Karicheri struct davinci_spi_platform_data pdata;
123ca632f55SGrant Likely
124ca632f55SGrant Likely void (*get_rx)(u32 rx_data, struct davinci_spi *);
125ca632f55SGrant Likely u32 (*get_tx)(struct davinci_spi *);
126ca632f55SGrant Likely
1277480e755SMurali Karicheri u8 *bytes_per_word;
128fa466c91SFranklin S Cooper Jr
129fa466c91SFranklin S Cooper Jr u8 prescaler_limit;
130ca632f55SGrant Likely };
131ca632f55SGrant Likely
132ca632f55SGrant Likely static struct davinci_spi_config davinci_spi_default_cfg;
133ca632f55SGrant Likely
davinci_spi_rx_buf_u8(u32 data,struct davinci_spi * dspi)134ca632f55SGrant Likely static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *dspi)
135ca632f55SGrant Likely {
136ca632f55SGrant Likely if (dspi->rx) {
137ca632f55SGrant Likely u8 *rx = dspi->rx;
138ca632f55SGrant Likely *rx++ = (u8)data;
139ca632f55SGrant Likely dspi->rx = rx;
140ca632f55SGrant Likely }
141ca632f55SGrant Likely }
142ca632f55SGrant Likely
davinci_spi_rx_buf_u16(u32 data,struct davinci_spi * dspi)143ca632f55SGrant Likely static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *dspi)
144ca632f55SGrant Likely {
145ca632f55SGrant Likely if (dspi->rx) {
146ca632f55SGrant Likely u16 *rx = dspi->rx;
147ca632f55SGrant Likely *rx++ = (u16)data;
148ca632f55SGrant Likely dspi->rx = rx;
149ca632f55SGrant Likely }
150ca632f55SGrant Likely }
151ca632f55SGrant Likely
davinci_spi_tx_buf_u8(struct davinci_spi * dspi)152ca632f55SGrant Likely static u32 davinci_spi_tx_buf_u8(struct davinci_spi *dspi)
153ca632f55SGrant Likely {
154ca632f55SGrant Likely u32 data = 0;
155859c3377SJingoo Han
156ca632f55SGrant Likely if (dspi->tx) {
157ca632f55SGrant Likely const u8 *tx = dspi->tx;
158859c3377SJingoo Han
159ca632f55SGrant Likely data = *tx++;
160ca632f55SGrant Likely dspi->tx = tx;
161ca632f55SGrant Likely }
162ca632f55SGrant Likely return data;
163ca632f55SGrant Likely }
164ca632f55SGrant Likely
davinci_spi_tx_buf_u16(struct davinci_spi * dspi)165ca632f55SGrant Likely static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
166ca632f55SGrant Likely {
167ca632f55SGrant Likely u32 data = 0;
168859c3377SJingoo Han
169ca632f55SGrant Likely if (dspi->tx) {
170ca632f55SGrant Likely const u16 *tx = dspi->tx;
171859c3377SJingoo Han
172ca632f55SGrant Likely data = *tx++;
173ca632f55SGrant Likely dspi->tx = tx;
174ca632f55SGrant Likely }
175ca632f55SGrant Likely return data;
176ca632f55SGrant Likely }
177ca632f55SGrant Likely
set_io_bits(void __iomem * addr,u32 bits)178ca632f55SGrant Likely static inline void set_io_bits(void __iomem *addr, u32 bits)
179ca632f55SGrant Likely {
180ca632f55SGrant Likely u32 v = ioread32(addr);
181ca632f55SGrant Likely
182ca632f55SGrant Likely v |= bits;
183ca632f55SGrant Likely iowrite32(v, addr);
184ca632f55SGrant Likely }
185ca632f55SGrant Likely
clear_io_bits(void __iomem * addr,u32 bits)186ca632f55SGrant Likely static inline void clear_io_bits(void __iomem *addr, u32 bits)
187ca632f55SGrant Likely {
188ca632f55SGrant Likely u32 v = ioread32(addr);
189ca632f55SGrant Likely
190ca632f55SGrant Likely v &= ~bits;
191ca632f55SGrant Likely iowrite32(v, addr);
192ca632f55SGrant Likely }
193ca632f55SGrant Likely
194ca632f55SGrant Likely /*
195ca632f55SGrant Likely * Interface to control the chip select signal
196ca632f55SGrant Likely */
davinci_spi_chipselect(struct spi_device * spi,int value)197ca632f55SGrant Likely static void davinci_spi_chipselect(struct spi_device *spi, int value)
198ca632f55SGrant Likely {
199ca632f55SGrant Likely struct davinci_spi *dspi;
200365a7bb3SMurali Karicheri struct davinci_spi_config *spicfg = spi->controller_data;
2019e264f3fSAmit Kumar Mahapatra via Alsa-devel u8 chip_sel = spi_get_chipselect(spi, 0);
202ca632f55SGrant Likely u16 spidat1 = CS_DEFAULT;
203ca632f55SGrant Likely
204*ad5602baSYang Yingliang dspi = spi_controller_get_devdata(spi->controller);
205ca632f55SGrant Likely
206365a7bb3SMurali Karicheri /* program delay transfers if tx_delay is non zero */
207563a53f3SBartosz Golaszewski if (spicfg && spicfg->wdelay)
208365a7bb3SMurali Karicheri spidat1 |= SPIDAT1_WDEL;
209365a7bb3SMurali Karicheri
210ca632f55SGrant Likely /*
211ca632f55SGrant Likely * Board specific chip select logic decides the polarity and cs
212ca632f55SGrant Likely * line for the controller
213ca632f55SGrant Likely */
2149e264f3fSAmit Kumar Mahapatra via Alsa-devel if (spi_get_csgpiod(spi, 0)) {
215ca632f55SGrant Likely if (value == BITBANG_CS_ACTIVE)
2169e264f3fSAmit Kumar Mahapatra via Alsa-devel gpiod_set_value(spi_get_csgpiod(spi, 0), 1);
217ca632f55SGrant Likely else
2189e264f3fSAmit Kumar Mahapatra via Alsa-devel gpiod_set_value(spi_get_csgpiod(spi, 0), 0);
219ca632f55SGrant Likely } else {
220ca632f55SGrant Likely if (value == BITBANG_CS_ACTIVE) {
221a3762b13SDavid Lechner if (!(spi->mode & SPI_CS_WORD))
222ca632f55SGrant Likely spidat1 |= SPIDAT1_CSHOLD_MASK;
223ca632f55SGrant Likely spidat1 &= ~(0x1 << chip_sel);
224ca632f55SGrant Likely }
225365a7bb3SMurali Karicheri }
226ca632f55SGrant Likely
227ca632f55SGrant Likely iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
228ca632f55SGrant Likely }
229ca632f55SGrant Likely
230ca632f55SGrant Likely /**
231ca632f55SGrant Likely * davinci_spi_get_prescale - Calculates the correct prescale value
232f6305d27SLee Jones * @dspi: the controller data
233f6305d27SLee Jones * @max_speed_hz: the maximum rate the SPI clock can run at
234ca632f55SGrant Likely *
235ca632f55SGrant Likely * This function calculates the prescale value that generates a clock rate
236ca632f55SGrant Likely * less than or equal to the specified maximum.
237ca632f55SGrant Likely *
238bba732d8SFranklin S Cooper Jr * Returns: calculated prescale value for easy programming into SPI registers
239ca632f55SGrant Likely * or negative error number if valid prescalar cannot be updated.
240ca632f55SGrant Likely */
davinci_spi_get_prescale(struct davinci_spi * dspi,u32 max_speed_hz)241ca632f55SGrant Likely static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
242ca632f55SGrant Likely u32 max_speed_hz)
243ca632f55SGrant Likely {
244ca632f55SGrant Likely int ret;
245ca632f55SGrant Likely
246bba732d8SFranklin S Cooper Jr /* Subtract 1 to match what will be programmed into SPI register. */
247bba732d8SFranklin S Cooper Jr ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz) - 1;
248ca632f55SGrant Likely
249fa466c91SFranklin S Cooper Jr if (ret < dspi->prescaler_limit || ret > 255)
250ca632f55SGrant Likely return -EINVAL;
251ca632f55SGrant Likely
252bba732d8SFranklin S Cooper Jr return ret;
253ca632f55SGrant Likely }
254ca632f55SGrant Likely
255ca632f55SGrant Likely /**
256ca632f55SGrant Likely * davinci_spi_setup_transfer - This functions will determine transfer method
257ca632f55SGrant Likely * @spi: spi device on which data transfer to be done
258ca632f55SGrant Likely * @t: spi transfer in which transfer info is filled
259ca632f55SGrant Likely *
260ca632f55SGrant Likely * This function determines data transfer method (8/16/32 bit transfer).
261ca632f55SGrant Likely * It will also set the SPI Clock Control register according to
262ca632f55SGrant Likely * SPI slave device freq.
263ca632f55SGrant Likely */
davinci_spi_setup_transfer(struct spi_device * spi,struct spi_transfer * t)264ca632f55SGrant Likely static int davinci_spi_setup_transfer(struct spi_device *spi,
265ca632f55SGrant Likely struct spi_transfer *t)
266ca632f55SGrant Likely {
267ca632f55SGrant Likely
268ca632f55SGrant Likely struct davinci_spi *dspi;
269ca632f55SGrant Likely struct davinci_spi_config *spicfg;
270ca632f55SGrant Likely u8 bits_per_word = 0;
27132ea3944SSachin Kamat u32 hz = 0, spifmt = 0;
27232ea3944SSachin Kamat int prescale;
273ca632f55SGrant Likely
274*ad5602baSYang Yingliang dspi = spi_controller_get_devdata(spi->controller);
275365a7bb3SMurali Karicheri spicfg = spi->controller_data;
276ca632f55SGrant Likely if (!spicfg)
277ca632f55SGrant Likely spicfg = &davinci_spi_default_cfg;
278ca632f55SGrant Likely
279ca632f55SGrant Likely if (t) {
280ca632f55SGrant Likely bits_per_word = t->bits_per_word;
281ca632f55SGrant Likely hz = t->speed_hz;
282ca632f55SGrant Likely }
283ca632f55SGrant Likely
284ca632f55SGrant Likely /* if bits_per_word is not set then set it default */
285ca632f55SGrant Likely if (!bits_per_word)
286ca632f55SGrant Likely bits_per_word = spi->bits_per_word;
287ca632f55SGrant Likely
288ca632f55SGrant Likely /*
289ca632f55SGrant Likely * Assign function pointer to appropriate transfer method
290ca632f55SGrant Likely * 8bit, 16bit or 32bit transfer
291ca632f55SGrant Likely */
29224778be2SStephen Warren if (bits_per_word <= 8) {
293ca632f55SGrant Likely dspi->get_rx = davinci_spi_rx_buf_u8;
294ca632f55SGrant Likely dspi->get_tx = davinci_spi_tx_buf_u8;
2959e264f3fSAmit Kumar Mahapatra via Alsa-devel dspi->bytes_per_word[spi_get_chipselect(spi, 0)] = 1;
29624778be2SStephen Warren } else {
297ca632f55SGrant Likely dspi->get_rx = davinci_spi_rx_buf_u16;
298ca632f55SGrant Likely dspi->get_tx = davinci_spi_tx_buf_u16;
2999e264f3fSAmit Kumar Mahapatra via Alsa-devel dspi->bytes_per_word[spi_get_chipselect(spi, 0)] = 2;
30024778be2SStephen Warren }
301ca632f55SGrant Likely
302ca632f55SGrant Likely if (!hz)
303ca632f55SGrant Likely hz = spi->max_speed_hz;
304ca632f55SGrant Likely
305ca632f55SGrant Likely /* Set up SPIFMTn register, unique to this chipselect. */
306ca632f55SGrant Likely
307ca632f55SGrant Likely prescale = davinci_spi_get_prescale(dspi, hz);
308ca632f55SGrant Likely if (prescale < 0)
309ca632f55SGrant Likely return prescale;
310ca632f55SGrant Likely
311ca632f55SGrant Likely spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
312ca632f55SGrant Likely
313ca632f55SGrant Likely if (spi->mode & SPI_LSB_FIRST)
314ca632f55SGrant Likely spifmt |= SPIFMT_SHIFTDIR_MASK;
315ca632f55SGrant Likely
316ca632f55SGrant Likely if (spi->mode & SPI_CPOL)
317ca632f55SGrant Likely spifmt |= SPIFMT_POLARITY_MASK;
318ca632f55SGrant Likely
319ca632f55SGrant Likely if (!(spi->mode & SPI_CPHA))
320ca632f55SGrant Likely spifmt |= SPIFMT_PHASE_MASK;
321ca632f55SGrant Likely
322ca632f55SGrant Likely /*
323365a7bb3SMurali Karicheri * Assume wdelay is used only on SPI peripherals that has this field
324365a7bb3SMurali Karicheri * in SPIFMTn register and when it's configured from board file or DT.
325365a7bb3SMurali Karicheri */
326365a7bb3SMurali Karicheri if (spicfg->wdelay)
327365a7bb3SMurali Karicheri spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
328365a7bb3SMurali Karicheri & SPIFMT_WDELAY_MASK);
329365a7bb3SMurali Karicheri
330365a7bb3SMurali Karicheri /*
331ca632f55SGrant Likely * Version 1 hardware supports two basic SPI modes:
332ca632f55SGrant Likely * - Standard SPI mode uses 4 pins, with chipselect
333ca632f55SGrant Likely * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
334ca632f55SGrant Likely * (distinct from SPI_3WIRE, with just one data wire;
335ca632f55SGrant Likely * or similar variants without MOSI or without MISO)
336ca632f55SGrant Likely *
337ca632f55SGrant Likely * Version 2 hardware supports an optional handshaking signal,
338ca632f55SGrant Likely * so it can support two more modes:
339ca632f55SGrant Likely * - 5 pin SPI variant is standard SPI plus SPI_READY
340ca632f55SGrant Likely * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
341ca632f55SGrant Likely */
342ca632f55SGrant Likely
343ca632f55SGrant Likely if (dspi->version == SPI_VERSION_2) {
344ca632f55SGrant Likely
345ca632f55SGrant Likely u32 delay = 0;
346ca632f55SGrant Likely
347ca632f55SGrant Likely if (spicfg->odd_parity)
348ca632f55SGrant Likely spifmt |= SPIFMT_ODD_PARITY_MASK;
349ca632f55SGrant Likely
350ca632f55SGrant Likely if (spicfg->parity_enable)
351ca632f55SGrant Likely spifmt |= SPIFMT_PARITYENA_MASK;
352ca632f55SGrant Likely
353ca632f55SGrant Likely if (spicfg->timer_disable) {
354ca632f55SGrant Likely spifmt |= SPIFMT_DISTIMER_MASK;
355ca632f55SGrant Likely } else {
356ca632f55SGrant Likely delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
357ca632f55SGrant Likely & SPIDELAY_C2TDELAY_MASK;
358ca632f55SGrant Likely delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
359ca632f55SGrant Likely & SPIDELAY_T2CDELAY_MASK;
360ca632f55SGrant Likely }
361ca632f55SGrant Likely
362ca632f55SGrant Likely if (spi->mode & SPI_READY) {
363ca632f55SGrant Likely spifmt |= SPIFMT_WAITENA_MASK;
364ca632f55SGrant Likely delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
365ca632f55SGrant Likely & SPIDELAY_T2EDELAY_MASK;
366ca632f55SGrant Likely delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
367ca632f55SGrant Likely & SPIDELAY_C2EDELAY_MASK;
368ca632f55SGrant Likely }
369ca632f55SGrant Likely
370ca632f55SGrant Likely iowrite32(delay, dspi->base + SPIDELAY);
371ca632f55SGrant Likely }
372ca632f55SGrant Likely
373ca632f55SGrant Likely iowrite32(spifmt, dspi->base + SPIFMT0);
374ca632f55SGrant Likely
375ca632f55SGrant Likely return 0;
376ca632f55SGrant Likely }
377ca632f55SGrant Likely
davinci_spi_of_setup(struct spi_device * spi)378365a7bb3SMurali Karicheri static int davinci_spi_of_setup(struct spi_device *spi)
379365a7bb3SMurali Karicheri {
380365a7bb3SMurali Karicheri struct davinci_spi_config *spicfg = spi->controller_data;
381365a7bb3SMurali Karicheri struct device_node *np = spi->dev.of_node;
382*ad5602baSYang Yingliang struct davinci_spi *dspi = spi_controller_get_devdata(spi->controller);
383365a7bb3SMurali Karicheri u32 prop;
384365a7bb3SMurali Karicheri
385365a7bb3SMurali Karicheri if (spicfg == NULL && np) {
386365a7bb3SMurali Karicheri spicfg = kzalloc(sizeof(*spicfg), GFP_KERNEL);
387365a7bb3SMurali Karicheri if (!spicfg)
388365a7bb3SMurali Karicheri return -ENOMEM;
389365a7bb3SMurali Karicheri *spicfg = davinci_spi_default_cfg;
390365a7bb3SMurali Karicheri /* override with dt configured values */
391365a7bb3SMurali Karicheri if (!of_property_read_u32(np, "ti,spi-wdelay", &prop))
392365a7bb3SMurali Karicheri spicfg->wdelay = (u8)prop;
393365a7bb3SMurali Karicheri spi->controller_data = spicfg;
3943e2e1258SFabien Parent
3953e2e1258SFabien Parent if (dspi->dma_rx && dspi->dma_tx)
3963e2e1258SFabien Parent spicfg->io_type = SPI_IO_TYPE_DMA;
397365a7bb3SMurali Karicheri }
398365a7bb3SMurali Karicheri
399365a7bb3SMurali Karicheri return 0;
400365a7bb3SMurali Karicheri }
401365a7bb3SMurali Karicheri
402ca632f55SGrant Likely /**
403ca632f55SGrant Likely * davinci_spi_setup - This functions will set default transfer method
404ca632f55SGrant Likely * @spi: spi device on which data transfer to be done
405ca632f55SGrant Likely *
406ca632f55SGrant Likely * This functions sets the default transfer method.
407ca632f55SGrant Likely */
davinci_spi_setup(struct spi_device * spi)408ca632f55SGrant Likely static int davinci_spi_setup(struct spi_device *spi)
409ca632f55SGrant Likely {
410ca632f55SGrant Likely struct davinci_spi *dspi;
411a88e34eaSMurali Karicheri struct device_node *np = spi->dev.of_node;
412a88e34eaSMurali Karicheri bool internal_cs = true;
413ca632f55SGrant Likely
414*ad5602baSYang Yingliang dspi = spi_controller_get_devdata(spi->controller);
415ca632f55SGrant Likely
416ca632f55SGrant Likely if (!(spi->mode & SPI_NO_CS)) {
4179e264f3fSAmit Kumar Mahapatra via Alsa-devel if (np && spi_get_csgpiod(spi, 0))
418a88e34eaSMurali Karicheri internal_cs = false;
419a88e34eaSMurali Karicheri
420101a68e7SLinus Walleij if (internal_cs)
4219e264f3fSAmit Kumar Mahapatra via Alsa-devel set_io_bits(dspi->base + SPIPC0, 1 << spi_get_chipselect(spi, 0));
422a3762b13SDavid Lechner }
423ca632f55SGrant Likely
424ca632f55SGrant Likely if (spi->mode & SPI_READY)
425ca632f55SGrant Likely set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
426ca632f55SGrant Likely
427ca632f55SGrant Likely if (spi->mode & SPI_LOOP)
428ca632f55SGrant Likely set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
429ca632f55SGrant Likely else
430ca632f55SGrant Likely clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
431ca632f55SGrant Likely
432365a7bb3SMurali Karicheri return davinci_spi_of_setup(spi);
433365a7bb3SMurali Karicheri }
434365a7bb3SMurali Karicheri
davinci_spi_cleanup(struct spi_device * spi)435365a7bb3SMurali Karicheri static void davinci_spi_cleanup(struct spi_device *spi)
436365a7bb3SMurali Karicheri {
437365a7bb3SMurali Karicheri struct davinci_spi_config *spicfg = spi->controller_data;
438365a7bb3SMurali Karicheri
439365a7bb3SMurali Karicheri spi->controller_data = NULL;
440365a7bb3SMurali Karicheri if (spi->dev.of_node)
441365a7bb3SMurali Karicheri kfree(spicfg);
442ca632f55SGrant Likely }
443ca632f55SGrant Likely
davinci_spi_can_dma(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)444*ad5602baSYang Yingliang static bool davinci_spi_can_dma(struct spi_controller *host,
4458aedbf58SFabien Parent struct spi_device *spi,
4468aedbf58SFabien Parent struct spi_transfer *xfer)
4478aedbf58SFabien Parent {
4488aedbf58SFabien Parent struct davinci_spi_config *spicfg = spi->controller_data;
4498aedbf58SFabien Parent bool can_dma = false;
4508aedbf58SFabien Parent
4518aedbf58SFabien Parent if (spicfg)
4520718b764SFrode Isaksen can_dma = (spicfg->io_type == SPI_IO_TYPE_DMA) &&
4534dd9becbSFrode Isaksen (xfer->len >= DMA_MIN_BYTES) &&
4544dd9becbSFrode Isaksen !is_vmalloc_addr(xfer->rx_buf) &&
4554dd9becbSFrode Isaksen !is_vmalloc_addr(xfer->tx_buf);
4568aedbf58SFabien Parent
4578aedbf58SFabien Parent return can_dma;
4588aedbf58SFabien Parent }
4598aedbf58SFabien Parent
davinci_spi_check_error(struct davinci_spi * dspi,int int_status)460ca632f55SGrant Likely static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
461ca632f55SGrant Likely {
462ca632f55SGrant Likely struct device *sdev = dspi->bitbang.master->dev.parent;
463ca632f55SGrant Likely
464ca632f55SGrant Likely if (int_status & SPIFLG_TIMEOUT_MASK) {
46521c015b7SSekhar Nori dev_err(sdev, "SPI Time-out Error\n");
466ca632f55SGrant Likely return -ETIMEDOUT;
467ca632f55SGrant Likely }
468ca632f55SGrant Likely if (int_status & SPIFLG_DESYNC_MASK) {
46921c015b7SSekhar Nori dev_err(sdev, "SPI Desynchronization Error\n");
470ca632f55SGrant Likely return -EIO;
471ca632f55SGrant Likely }
472ca632f55SGrant Likely if (int_status & SPIFLG_BITERR_MASK) {
47321c015b7SSekhar Nori dev_err(sdev, "SPI Bit error\n");
474ca632f55SGrant Likely return -EIO;
475ca632f55SGrant Likely }
476ca632f55SGrant Likely
477ca632f55SGrant Likely if (dspi->version == SPI_VERSION_2) {
478ca632f55SGrant Likely if (int_status & SPIFLG_DLEN_ERR_MASK) {
47921c015b7SSekhar Nori dev_err(sdev, "SPI Data Length Error\n");
480ca632f55SGrant Likely return -EIO;
481ca632f55SGrant Likely }
482ca632f55SGrant Likely if (int_status & SPIFLG_PARERR_MASK) {
48321c015b7SSekhar Nori dev_err(sdev, "SPI Parity Error\n");
484ca632f55SGrant Likely return -EIO;
485ca632f55SGrant Likely }
486ca632f55SGrant Likely if (int_status & SPIFLG_OVRRUN_MASK) {
48721c015b7SSekhar Nori dev_err(sdev, "SPI Data Overrun error\n");
488ca632f55SGrant Likely return -EIO;
489ca632f55SGrant Likely }
490ca632f55SGrant Likely if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
49121c015b7SSekhar Nori dev_err(sdev, "SPI Buffer Init Active\n");
492ca632f55SGrant Likely return -EBUSY;
493ca632f55SGrant Likely }
494ca632f55SGrant Likely }
495ca632f55SGrant Likely
496ca632f55SGrant Likely return 0;
497ca632f55SGrant Likely }
498ca632f55SGrant Likely
499ca632f55SGrant Likely /**
500ca632f55SGrant Likely * davinci_spi_process_events - check for and handle any SPI controller events
501ca632f55SGrant Likely * @dspi: the controller data
502ca632f55SGrant Likely *
503ca632f55SGrant Likely * This function will check the SPIFLG register and handle any events that are
504ca632f55SGrant Likely * detected there
505ca632f55SGrant Likely */
davinci_spi_process_events(struct davinci_spi * dspi)506ca632f55SGrant Likely static int davinci_spi_process_events(struct davinci_spi *dspi)
507ca632f55SGrant Likely {
508ca632f55SGrant Likely u32 buf, status, errors = 0, spidat1;
509ca632f55SGrant Likely
510ca632f55SGrant Likely buf = ioread32(dspi->base + SPIBUF);
511ca632f55SGrant Likely
512ca632f55SGrant Likely if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
513ca632f55SGrant Likely dspi->get_rx(buf & 0xFFFF, dspi);
514ca632f55SGrant Likely dspi->rcount--;
515ca632f55SGrant Likely }
516ca632f55SGrant Likely
517ca632f55SGrant Likely status = ioread32(dspi->base + SPIFLG);
518ca632f55SGrant Likely
519ca632f55SGrant Likely if (unlikely(status & SPIFLG_ERROR_MASK)) {
520ca632f55SGrant Likely errors = status & SPIFLG_ERROR_MASK;
521ca632f55SGrant Likely goto out;
522ca632f55SGrant Likely }
523ca632f55SGrant Likely
524ca632f55SGrant Likely if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
525ca632f55SGrant Likely spidat1 = ioread32(dspi->base + SPIDAT1);
526ca632f55SGrant Likely dspi->wcount--;
527ca632f55SGrant Likely spidat1 &= ~0xFFFF;
528ca632f55SGrant Likely spidat1 |= 0xFFFF & dspi->get_tx(dspi);
529ca632f55SGrant Likely iowrite32(spidat1, dspi->base + SPIDAT1);
530ca632f55SGrant Likely }
531ca632f55SGrant Likely
532ca632f55SGrant Likely out:
533ca632f55SGrant Likely return errors;
534ca632f55SGrant Likely }
535ca632f55SGrant Likely
davinci_spi_dma_rx_callback(void * data)536048177ceSMatt Porter static void davinci_spi_dma_rx_callback(void *data)
537ca632f55SGrant Likely {
538048177ceSMatt Porter struct davinci_spi *dspi = (struct davinci_spi *)data;
539ca632f55SGrant Likely
540ca632f55SGrant Likely dspi->rcount = 0;
541048177ceSMatt Porter
542048177ceSMatt Porter if (!dspi->wcount && !dspi->rcount)
543048177ceSMatt Porter complete(&dspi->done);
544ca632f55SGrant Likely }
545ca632f55SGrant Likely
davinci_spi_dma_tx_callback(void * data)546048177ceSMatt Porter static void davinci_spi_dma_tx_callback(void *data)
547048177ceSMatt Porter {
548048177ceSMatt Porter struct davinci_spi *dspi = (struct davinci_spi *)data;
549048177ceSMatt Porter
550048177ceSMatt Porter dspi->wcount = 0;
551048177ceSMatt Porter
552048177ceSMatt Porter if (!dspi->wcount && !dspi->rcount)
553ca632f55SGrant Likely complete(&dspi->done);
554ca632f55SGrant Likely }
555ca632f55SGrant Likely
556ca632f55SGrant Likely /**
557ca632f55SGrant Likely * davinci_spi_bufs - functions which will handle transfer data
558ca632f55SGrant Likely * @spi: spi device on which data transfer to be done
559ca632f55SGrant Likely * @t: spi transfer in which transfer info is filled
560ca632f55SGrant Likely *
561ca632f55SGrant Likely * This function will put data to be transferred into data register
562ca632f55SGrant Likely * of SPI controller and then wait until the completion will be marked
563ca632f55SGrant Likely * by the IRQ Handler.
564ca632f55SGrant Likely */
davinci_spi_bufs(struct spi_device * spi,struct spi_transfer * t)565ca632f55SGrant Likely static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
566ca632f55SGrant Likely {
567ca632f55SGrant Likely struct davinci_spi *dspi;
568048177ceSMatt Porter int data_type, ret = -ENOMEM;
569ca632f55SGrant Likely u32 tx_data, spidat1;
570ca632f55SGrant Likely u32 errors = 0;
571ca632f55SGrant Likely struct davinci_spi_config *spicfg;
572ca632f55SGrant Likely struct davinci_spi_platform_data *pdata;
573ca632f55SGrant Likely
574*ad5602baSYang Yingliang dspi = spi_controller_get_devdata(spi->controller);
575aae7147dSMurali Karicheri pdata = &dspi->pdata;
576ca632f55SGrant Likely spicfg = (struct davinci_spi_config *)spi->controller_data;
577ca632f55SGrant Likely if (!spicfg)
578ca632f55SGrant Likely spicfg = &davinci_spi_default_cfg;
579ca632f55SGrant Likely
580ca632f55SGrant Likely /* convert len to words based on bits_per_word */
5819e264f3fSAmit Kumar Mahapatra via Alsa-devel data_type = dspi->bytes_per_word[spi_get_chipselect(spi, 0)];
582ca632f55SGrant Likely
583ca632f55SGrant Likely dspi->tx = t->tx_buf;
584ca632f55SGrant Likely dspi->rx = t->rx_buf;
585ca632f55SGrant Likely dspi->wcount = t->len / data_type;
586ca632f55SGrant Likely dspi->rcount = dspi->wcount;
587ca632f55SGrant Likely
588ca632f55SGrant Likely spidat1 = ioread32(dspi->base + SPIDAT1);
589ca632f55SGrant Likely
590ca632f55SGrant Likely clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
591ca632f55SGrant Likely set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
592ca632f55SGrant Likely
59316735d02SWolfram Sang reinit_completion(&dspi->done);
594ca632f55SGrant Likely
595*ad5602baSYang Yingliang if (!davinci_spi_can_dma(spi->controller, spi, t)) {
5960718b764SFrode Isaksen if (spicfg->io_type != SPI_IO_TYPE_POLL)
597ca632f55SGrant Likely set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
598ca632f55SGrant Likely /* start the transfer */
599ca632f55SGrant Likely dspi->wcount--;
600ca632f55SGrant Likely tx_data = dspi->get_tx(dspi);
601ca632f55SGrant Likely spidat1 &= 0xFFFF0000;
602ca632f55SGrant Likely spidat1 |= tx_data & 0xFFFF;
603ca632f55SGrant Likely iowrite32(spidat1, dspi->base + SPIDAT1);
604ca632f55SGrant Likely } else {
605048177ceSMatt Porter struct dma_slave_config dma_rx_conf = {
606048177ceSMatt Porter .direction = DMA_DEV_TO_MEM,
607048177ceSMatt Porter .src_addr = (unsigned long)dspi->pbase + SPIBUF,
608048177ceSMatt Porter .src_addr_width = data_type,
609048177ceSMatt Porter .src_maxburst = 1,
610048177ceSMatt Porter };
611048177ceSMatt Porter struct dma_slave_config dma_tx_conf = {
612048177ceSMatt Porter .direction = DMA_MEM_TO_DEV,
613048177ceSMatt Porter .dst_addr = (unsigned long)dspi->pbase + SPIDAT1,
614048177ceSMatt Porter .dst_addr_width = data_type,
615048177ceSMatt Porter .dst_maxburst = 1,
616048177ceSMatt Porter };
617048177ceSMatt Porter struct dma_async_tx_descriptor *rxdesc;
618048177ceSMatt Porter struct dma_async_tx_descriptor *txdesc;
619ca632f55SGrant Likely
620048177ceSMatt Porter dmaengine_slave_config(dspi->dma_rx, &dma_rx_conf);
621048177ceSMatt Porter dmaengine_slave_config(dspi->dma_tx, &dma_tx_conf);
622ca632f55SGrant Likely
623048177ceSMatt Porter rxdesc = dmaengine_prep_slave_sg(dspi->dma_rx,
6248aedbf58SFabien Parent t->rx_sg.sgl, t->rx_sg.nents, DMA_DEV_TO_MEM,
625048177ceSMatt Porter DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
626048177ceSMatt Porter if (!rxdesc)
627048177ceSMatt Porter goto err_desc;
628048177ceSMatt Porter
6296b3a631eSFrode Isaksen if (!t->tx_buf) {
6301234e839SFrode Isaksen /* To avoid errors when doing rx-only transfers with
6311234e839SFrode Isaksen * many SG entries (> 20), use the rx buffer as the
6321234e839SFrode Isaksen * dummy tx buffer so that dma reloads are done at the
6331234e839SFrode Isaksen * same time for rx and tx.
6341234e839SFrode Isaksen */
6356b3a631eSFrode Isaksen t->tx_sg.sgl = t->rx_sg.sgl;
6366b3a631eSFrode Isaksen t->tx_sg.nents = t->rx_sg.nents;
6376b3a631eSFrode Isaksen }
6386b3a631eSFrode Isaksen
639048177ceSMatt Porter txdesc = dmaengine_prep_slave_sg(dspi->dma_tx,
6408aedbf58SFabien Parent t->tx_sg.sgl, t->tx_sg.nents, DMA_MEM_TO_DEV,
641048177ceSMatt Porter DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
642048177ceSMatt Porter if (!txdesc)
643048177ceSMatt Porter goto err_desc;
644048177ceSMatt Porter
645048177ceSMatt Porter rxdesc->callback = davinci_spi_dma_rx_callback;
646048177ceSMatt Porter rxdesc->callback_param = (void *)dspi;
647048177ceSMatt Porter txdesc->callback = davinci_spi_dma_tx_callback;
648048177ceSMatt Porter txdesc->callback_param = (void *)dspi;
649ca632f55SGrant Likely
650ca632f55SGrant Likely if (pdata->cshold_bug)
651ca632f55SGrant Likely iowrite16(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
652ca632f55SGrant Likely
653048177ceSMatt Porter dmaengine_submit(rxdesc);
654048177ceSMatt Porter dmaengine_submit(txdesc);
655048177ceSMatt Porter
656048177ceSMatt Porter dma_async_issue_pending(dspi->dma_rx);
657048177ceSMatt Porter dma_async_issue_pending(dspi->dma_tx);
658048177ceSMatt Porter
659ca632f55SGrant Likely set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
660ca632f55SGrant Likely }
661ca632f55SGrant Likely
662ca632f55SGrant Likely /* Wait for the transfer to complete */
663ca632f55SGrant Likely if (spicfg->io_type != SPI_IO_TYPE_POLL) {
6647f3ac71aSSekhar Nori if (wait_for_completion_timeout(&dspi->done, HZ) == 0)
6657f3ac71aSSekhar Nori errors = SPIFLG_TIMEOUT_MASK;
666ca632f55SGrant Likely } else {
667ca632f55SGrant Likely while (dspi->rcount > 0 || dspi->wcount > 0) {
668ca632f55SGrant Likely errors = davinci_spi_process_events(dspi);
669ca632f55SGrant Likely if (errors)
670ca632f55SGrant Likely break;
671ca632f55SGrant Likely cpu_relax();
672ca632f55SGrant Likely }
673ca632f55SGrant Likely }
674ca632f55SGrant Likely
675ca632f55SGrant Likely clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
676*ad5602baSYang Yingliang if (davinci_spi_can_dma(spi->controller, spi, t))
677ca632f55SGrant Likely clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
678048177ceSMatt Porter
679ca632f55SGrant Likely clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
680ca632f55SGrant Likely set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
681ca632f55SGrant Likely
682ca632f55SGrant Likely /*
683ca632f55SGrant Likely * Check for bit error, desync error,parity error,timeout error and
684ca632f55SGrant Likely * receive overflow errors
685ca632f55SGrant Likely */
686ca632f55SGrant Likely if (errors) {
687ca632f55SGrant Likely ret = davinci_spi_check_error(dspi, errors);
688ca632f55SGrant Likely WARN(!ret, "%s: error reported but no error found!\n",
689ca632f55SGrant Likely dev_name(&spi->dev));
690ca632f55SGrant Likely return ret;
691ca632f55SGrant Likely }
692ca632f55SGrant Likely
693ca632f55SGrant Likely if (dspi->rcount != 0 || dspi->wcount != 0) {
694048177ceSMatt Porter dev_err(&spi->dev, "SPI data transfer error\n");
695ca632f55SGrant Likely return -EIO;
696ca632f55SGrant Likely }
697ca632f55SGrant Likely
698ca632f55SGrant Likely return t->len;
699048177ceSMatt Porter
700048177ceSMatt Porter err_desc:
701048177ceSMatt Porter return ret;
702ca632f55SGrant Likely }
703ca632f55SGrant Likely
704ca632f55SGrant Likely /**
70532310aafSMurali Karicheri * dummy_thread_fn - dummy thread function
70632310aafSMurali Karicheri * @irq: IRQ number for this SPI Master
707f6305d27SLee Jones * @data: structure for SPI Master controller davinci_spi
70832310aafSMurali Karicheri *
70932310aafSMurali Karicheri * This is to satisfy the request_threaded_irq() API so that the irq
71032310aafSMurali Karicheri * handler is called in interrupt context.
71132310aafSMurali Karicheri */
dummy_thread_fn(s32 irq,void * data)71232310aafSMurali Karicheri static irqreturn_t dummy_thread_fn(s32 irq, void *data)
71332310aafSMurali Karicheri {
71432310aafSMurali Karicheri return IRQ_HANDLED;
71532310aafSMurali Karicheri }
71632310aafSMurali Karicheri
71732310aafSMurali Karicheri /**
718ca632f55SGrant Likely * davinci_spi_irq - Interrupt handler for SPI Master Controller
719ca632f55SGrant Likely * @irq: IRQ number for this SPI Master
720f6305d27SLee Jones * @data: structure for SPI Master controller davinci_spi
721ca632f55SGrant Likely *
722ca632f55SGrant Likely * ISR will determine that interrupt arrives either for READ or WRITE command.
723ca632f55SGrant Likely * According to command it will do the appropriate action. It will check
724ca632f55SGrant Likely * transfer length and if it is not zero then dispatch transfer command again.
725ca632f55SGrant Likely * If transfer length is zero then it will indicate the COMPLETION so that
726ca632f55SGrant Likely * davinci_spi_bufs function can go ahead.
727ca632f55SGrant Likely */
davinci_spi_irq(s32 irq,void * data)728ca632f55SGrant Likely static irqreturn_t davinci_spi_irq(s32 irq, void *data)
729ca632f55SGrant Likely {
730ca632f55SGrant Likely struct davinci_spi *dspi = data;
731ca632f55SGrant Likely int status;
732ca632f55SGrant Likely
733ca632f55SGrant Likely status = davinci_spi_process_events(dspi);
734ca632f55SGrant Likely if (unlikely(status != 0))
735ca632f55SGrant Likely clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
736ca632f55SGrant Likely
737ca632f55SGrant Likely if ((!dspi->rcount && !dspi->wcount) || status)
738ca632f55SGrant Likely complete(&dspi->done);
739ca632f55SGrant Likely
740ca632f55SGrant Likely return IRQ_HANDLED;
741ca632f55SGrant Likely }
742ca632f55SGrant Likely
davinci_spi_request_dma(struct davinci_spi * dspi)743ca632f55SGrant Likely static int davinci_spi_request_dma(struct davinci_spi *dspi)
744ca632f55SGrant Likely {
745048177ceSMatt Porter struct device *sdev = dspi->bitbang.master->dev.parent;
746ca632f55SGrant Likely
747fe5fd254SPeter Ujfalusi dspi->dma_rx = dma_request_chan(sdev, "rx");
748fe5fd254SPeter Ujfalusi if (IS_ERR(dspi->dma_rx))
749fe5fd254SPeter Ujfalusi return PTR_ERR(dspi->dma_rx);
750048177ceSMatt Porter
751fe5fd254SPeter Ujfalusi dspi->dma_tx = dma_request_chan(sdev, "tx");
752fe5fd254SPeter Ujfalusi if (IS_ERR(dspi->dma_tx)) {
753fe5fd254SPeter Ujfalusi dma_release_channel(dspi->dma_rx);
754fe5fd254SPeter Ujfalusi return PTR_ERR(dspi->dma_tx);
755ca632f55SGrant Likely }
756ca632f55SGrant Likely
757ca632f55SGrant Likely return 0;
758ca632f55SGrant Likely }
759ca632f55SGrant Likely
760aae7147dSMurali Karicheri #if defined(CONFIG_OF)
761fa466c91SFranklin S Cooper Jr
762fa466c91SFranklin S Cooper Jr /* OF SPI data structure */
763fa466c91SFranklin S Cooper Jr struct davinci_spi_of_data {
764fa466c91SFranklin S Cooper Jr u8 version;
765fa466c91SFranklin S Cooper Jr u8 prescaler_limit;
766fa466c91SFranklin S Cooper Jr };
767fa466c91SFranklin S Cooper Jr
768fa466c91SFranklin S Cooper Jr static const struct davinci_spi_of_data dm6441_spi_data = {
769fa466c91SFranklin S Cooper Jr .version = SPI_VERSION_1,
770fa466c91SFranklin S Cooper Jr .prescaler_limit = 2,
771fa466c91SFranklin S Cooper Jr };
772fa466c91SFranklin S Cooper Jr
773fa466c91SFranklin S Cooper Jr static const struct davinci_spi_of_data da830_spi_data = {
774fa466c91SFranklin S Cooper Jr .version = SPI_VERSION_2,
775fa466c91SFranklin S Cooper Jr .prescaler_limit = 2,
776fa466c91SFranklin S Cooper Jr };
777fa466c91SFranklin S Cooper Jr
778fa466c91SFranklin S Cooper Jr static const struct davinci_spi_of_data keystone_spi_data = {
779fa466c91SFranklin S Cooper Jr .version = SPI_VERSION_1,
780fa466c91SFranklin S Cooper Jr .prescaler_limit = 0,
781fa466c91SFranklin S Cooper Jr };
782fa466c91SFranklin S Cooper Jr
783aae7147dSMurali Karicheri static const struct of_device_id davinci_spi_of_match[] = {
784aae7147dSMurali Karicheri {
785804413f2SManjunathappa, Prakash .compatible = "ti,dm6441-spi",
786fa466c91SFranklin S Cooper Jr .data = &dm6441_spi_data,
787aae7147dSMurali Karicheri },
788aae7147dSMurali Karicheri {
789804413f2SManjunathappa, Prakash .compatible = "ti,da830-spi",
790fa466c91SFranklin S Cooper Jr .data = &da830_spi_data,
791fa466c91SFranklin S Cooper Jr },
792fa466c91SFranklin S Cooper Jr {
793fa466c91SFranklin S Cooper Jr .compatible = "ti,keystone-spi",
794fa466c91SFranklin S Cooper Jr .data = &keystone_spi_data,
795aae7147dSMurali Karicheri },
796aae7147dSMurali Karicheri { },
797aae7147dSMurali Karicheri };
7980d2d0cc5SManjunathappa, Prakash MODULE_DEVICE_TABLE(of, davinci_spi_of_match);
799aae7147dSMurali Karicheri
800aae7147dSMurali Karicheri /**
801aae7147dSMurali Karicheri * spi_davinci_get_pdata - Get platform data from DTS binding
802aae7147dSMurali Karicheri * @pdev: ptr to platform data
803aae7147dSMurali Karicheri * @dspi: ptr to driver data
804aae7147dSMurali Karicheri *
805aae7147dSMurali Karicheri * Parses and populates pdata in dspi from device tree bindings.
806aae7147dSMurali Karicheri *
807aae7147dSMurali Karicheri * NOTE: Not all platform data params are supported currently.
808aae7147dSMurali Karicheri */
spi_davinci_get_pdata(struct platform_device * pdev,struct davinci_spi * dspi)809aae7147dSMurali Karicheri static int spi_davinci_get_pdata(struct platform_device *pdev,
810aae7147dSMurali Karicheri struct davinci_spi *dspi)
811aae7147dSMurali Karicheri {
812aae7147dSMurali Karicheri struct device_node *node = pdev->dev.of_node;
81330700a05STian Tao const struct davinci_spi_of_data *spi_data;
814aae7147dSMurali Karicheri struct davinci_spi_platform_data *pdata;
815aae7147dSMurali Karicheri unsigned int num_cs, intr_line = 0;
816aae7147dSMurali Karicheri
817aae7147dSMurali Karicheri pdata = &dspi->pdata;
818aae7147dSMurali Karicheri
81930700a05STian Tao spi_data = device_get_match_data(&pdev->dev);
820aae7147dSMurali Karicheri
821fa466c91SFranklin S Cooper Jr pdata->version = spi_data->version;
822fa466c91SFranklin S Cooper Jr pdata->prescaler_limit = spi_data->prescaler_limit;
823aae7147dSMurali Karicheri /*
824aae7147dSMurali Karicheri * default num_cs is 1 and all chipsel are internal to the chip
825a88e34eaSMurali Karicheri * indicated by chip_sel being NULL or cs_gpios being NULL or
826a88e34eaSMurali Karicheri * set to -ENOENT. num-cs includes internal as well as gpios.
827aae7147dSMurali Karicheri * indicated by chip_sel being NULL. GPIO based CS is not
828aae7147dSMurali Karicheri * supported yet in DT bindings.
829aae7147dSMurali Karicheri */
830aae7147dSMurali Karicheri num_cs = 1;
831aae7147dSMurali Karicheri of_property_read_u32(node, "num-cs", &num_cs);
832aae7147dSMurali Karicheri pdata->num_chipselect = num_cs;
833aae7147dSMurali Karicheri of_property_read_u32(node, "ti,davinci-spi-intr-line", &intr_line);
834aae7147dSMurali Karicheri pdata->intr_line = intr_line;
835aae7147dSMurali Karicheri return 0;
836aae7147dSMurali Karicheri }
837aae7147dSMurali Karicheri #else
spi_davinci_get_pdata(struct platform_device * pdev,struct davinci_spi * dspi)8382b747a5fSArvind Yadav static int spi_davinci_get_pdata(struct platform_device *pdev,
839aae7147dSMurali Karicheri struct davinci_spi *dspi)
840aae7147dSMurali Karicheri {
841aae7147dSMurali Karicheri return -ENODEV;
842aae7147dSMurali Karicheri }
843aae7147dSMurali Karicheri #endif
844aae7147dSMurali Karicheri
845ca632f55SGrant Likely /**
846ca632f55SGrant Likely * davinci_spi_probe - probe function for SPI Master Controller
847ca632f55SGrant Likely * @pdev: platform_device structure which contains plateform specific data
848ca632f55SGrant Likely *
849ca632f55SGrant Likely * According to Linux Device Model this function will be invoked by Linux
850ca632f55SGrant Likely * with platform_device struct which contains the device specific info.
851ca632f55SGrant Likely * This function will map the SPI controller's memory, register IRQ,
852ca632f55SGrant Likely * Reset SPI controller and setting its registers to default value.
853ca632f55SGrant Likely * It will invoke spi_bitbang_start to create work queue so that client driver
854ca632f55SGrant Likely * can register transfer method to work queue.
855ca632f55SGrant Likely */
davinci_spi_probe(struct platform_device * pdev)856fd4a319bSGrant Likely static int davinci_spi_probe(struct platform_device *pdev)
857ca632f55SGrant Likely {
858*ad5602baSYang Yingliang struct spi_controller *host;
859ca632f55SGrant Likely struct davinci_spi *dspi;
860ca632f55SGrant Likely struct davinci_spi_platform_data *pdata;
8615b3bb596SJingoo Han struct resource *r;
862c0600140SGrygorii Strashko int ret = 0;
863ca632f55SGrant Likely u32 spipc0;
864ca632f55SGrant Likely
865*ad5602baSYang Yingliang host = spi_alloc_host(&pdev->dev, sizeof(struct davinci_spi));
866*ad5602baSYang Yingliang if (host == NULL) {
867ca632f55SGrant Likely ret = -ENOMEM;
868ca632f55SGrant Likely goto err;
869ca632f55SGrant Likely }
870ca632f55SGrant Likely
871*ad5602baSYang Yingliang platform_set_drvdata(pdev, host);
872ca632f55SGrant Likely
873*ad5602baSYang Yingliang dspi = spi_controller_get_devdata(host);
874ca632f55SGrant Likely
8758074cf06SJingoo Han if (dev_get_platdata(&pdev->dev)) {
8768074cf06SJingoo Han pdata = dev_get_platdata(&pdev->dev);
877aae7147dSMurali Karicheri dspi->pdata = *pdata;
878aae7147dSMurali Karicheri } else {
879aae7147dSMurali Karicheri /* update dspi pdata with that from the DT */
880aae7147dSMurali Karicheri ret = spi_davinci_get_pdata(pdev, dspi);
881aae7147dSMurali Karicheri if (ret < 0)
882*ad5602baSYang Yingliang goto free_host;
883aae7147dSMurali Karicheri }
884aae7147dSMurali Karicheri
885aae7147dSMurali Karicheri /* pdata in dspi is now updated and point pdata to that */
886aae7147dSMurali Karicheri pdata = &dspi->pdata;
887aae7147dSMurali Karicheri
888a86854d0SKees Cook dspi->bytes_per_word = devm_kcalloc(&pdev->dev,
889a86854d0SKees Cook pdata->num_chipselect,
890a86854d0SKees Cook sizeof(*dspi->bytes_per_word),
891a86854d0SKees Cook GFP_KERNEL);
8927480e755SMurali Karicheri if (dspi->bytes_per_word == NULL) {
8937480e755SMurali Karicheri ret = -ENOMEM;
894*ad5602baSYang Yingliang goto free_host;
8957480e755SMurali Karicheri }
8967480e755SMurali Karicheri
8972ae3c98bSYangtao Li dspi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &r);
8985b3bb596SJingoo Han if (IS_ERR(dspi->base)) {
8995b3bb596SJingoo Han ret = PTR_ERR(dspi->base);
900*ad5602baSYang Yingliang goto free_host;
901ca632f55SGrant Likely }
9022ae3c98bSYangtao Li dspi->pbase = r->start;
903ca632f55SGrant Likely
90487248dc7SMichele Dionisio init_completion(&dspi->done);
90587248dc7SMichele Dionisio
9068494cdeaSAndrzej Hajda ret = platform_get_irq(pdev, 0);
9078494cdeaSAndrzej Hajda if (ret < 0)
908*ad5602baSYang Yingliang goto free_host;
9098494cdeaSAndrzej Hajda dspi->irq = ret;
910ca632f55SGrant Likely
9115b3bb596SJingoo Han ret = devm_request_threaded_irq(&pdev->dev, dspi->irq, davinci_spi_irq,
9125b3bb596SJingoo Han dummy_thread_fn, 0, dev_name(&pdev->dev), dspi);
913ca632f55SGrant Likely if (ret)
914*ad5602baSYang Yingliang goto free_host;
915ca632f55SGrant Likely
916*ad5602baSYang Yingliang dspi->bitbang.master = host;
917ca632f55SGrant Likely
9185b3bb596SJingoo Han dspi->clk = devm_clk_get(&pdev->dev, NULL);
919ca632f55SGrant Likely if (IS_ERR(dspi->clk)) {
920ca632f55SGrant Likely ret = -ENODEV;
921*ad5602baSYang Yingliang goto free_host;
922ca632f55SGrant Likely }
92335fc3b9fSArvind Yadav ret = clk_prepare_enable(dspi->clk);
92435fc3b9fSArvind Yadav if (ret)
925*ad5602baSYang Yingliang goto free_host;
926ca632f55SGrant Likely
927*ad5602baSYang Yingliang host->use_gpio_descriptors = true;
928*ad5602baSYang Yingliang host->dev.of_node = pdev->dev.of_node;
929*ad5602baSYang Yingliang host->bus_num = pdev->id;
930*ad5602baSYang Yingliang host->num_chipselect = pdata->num_chipselect;
931*ad5602baSYang Yingliang host->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16);
932*ad5602baSYang Yingliang host->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_GPIO_SS;
933*ad5602baSYang Yingliang host->setup = davinci_spi_setup;
934*ad5602baSYang Yingliang host->cleanup = davinci_spi_cleanup;
935*ad5602baSYang Yingliang host->can_dma = davinci_spi_can_dma;
936ca632f55SGrant Likely
937ca632f55SGrant Likely dspi->bitbang.chipselect = davinci_spi_chipselect;
938ca632f55SGrant Likely dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
939fa466c91SFranklin S Cooper Jr dspi->prescaler_limit = pdata->prescaler_limit;
940ca632f55SGrant Likely dspi->version = pdata->version;
941ca632f55SGrant Likely
942a3762b13SDavid Lechner dspi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP | SPI_CS_WORD;
943ca632f55SGrant Likely if (dspi->version == SPI_VERSION_2)
944ca632f55SGrant Likely dspi->bitbang.flags |= SPI_READY;
945ca632f55SGrant Likely
946ca632f55SGrant Likely dspi->bitbang.txrx_bufs = davinci_spi_bufs;
947ca632f55SGrant Likely
948ca632f55SGrant Likely ret = davinci_spi_request_dma(dspi);
949fe5fd254SPeter Ujfalusi if (ret == -EPROBE_DEFER) {
950ca632f55SGrant Likely goto free_clk;
951fe5fd254SPeter Ujfalusi } else if (ret) {
952fe5fd254SPeter Ujfalusi dev_info(&pdev->dev, "DMA is not supported (%d)\n", ret);
953fe5fd254SPeter Ujfalusi dspi->dma_rx = NULL;
954fe5fd254SPeter Ujfalusi dspi->dma_tx = NULL;
955ca632f55SGrant Likely }
956ca632f55SGrant Likely
957ca632f55SGrant Likely dspi->get_rx = davinci_spi_rx_buf_u8;
958ca632f55SGrant Likely dspi->get_tx = davinci_spi_tx_buf_u8;
959ca632f55SGrant Likely
960ca632f55SGrant Likely /* Reset In/OUT SPI module */
961ca632f55SGrant Likely iowrite32(0, dspi->base + SPIGCR0);
962ca632f55SGrant Likely udelay(100);
963ca632f55SGrant Likely iowrite32(1, dspi->base + SPIGCR0);
964ca632f55SGrant Likely
965ca632f55SGrant Likely /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
966ca632f55SGrant Likely spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
967ca632f55SGrant Likely iowrite32(spipc0, dspi->base + SPIPC0);
968ca632f55SGrant Likely
969ca632f55SGrant Likely if (pdata->intr_line)
970ca632f55SGrant Likely iowrite32(SPI_INTLVL_1, dspi->base + SPILVL);
971ca632f55SGrant Likely else
972ca632f55SGrant Likely iowrite32(SPI_INTLVL_0, dspi->base + SPILVL);
973ca632f55SGrant Likely
974ca632f55SGrant Likely iowrite32(CS_DEFAULT, dspi->base + SPIDEF);
975ca632f55SGrant Likely
976*ad5602baSYang Yingliang /* host mode default */
977ca632f55SGrant Likely set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
978ca632f55SGrant Likely set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
979ca632f55SGrant Likely set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
980ca632f55SGrant Likely
981ca632f55SGrant Likely ret = spi_bitbang_start(&dspi->bitbang);
982ca632f55SGrant Likely if (ret)
983ca632f55SGrant Likely goto free_dma;
984ca632f55SGrant Likely
985ca632f55SGrant Likely dev_info(&pdev->dev, "Controller at 0x%p\n", dspi->base);
986ca632f55SGrant Likely
987ca632f55SGrant Likely return ret;
988ca632f55SGrant Likely
989ca632f55SGrant Likely free_dma:
990fe5fd254SPeter Ujfalusi if (dspi->dma_rx) {
991048177ceSMatt Porter dma_release_channel(dspi->dma_rx);
992048177ceSMatt Porter dma_release_channel(dspi->dma_tx);
993fe5fd254SPeter Ujfalusi }
994ca632f55SGrant Likely free_clk:
995aae7147dSMurali Karicheri clk_disable_unprepare(dspi->clk);
996*ad5602baSYang Yingliang free_host:
997*ad5602baSYang Yingliang spi_controller_put(host);
998ca632f55SGrant Likely err:
999ca632f55SGrant Likely return ret;
1000ca632f55SGrant Likely }
1001ca632f55SGrant Likely
1002ca632f55SGrant Likely /**
1003ca632f55SGrant Likely * davinci_spi_remove - remove function for SPI Master Controller
1004ca632f55SGrant Likely * @pdev: platform_device structure which contains plateform specific data
1005ca632f55SGrant Likely *
1006ca632f55SGrant Likely * This function will do the reverse action of davinci_spi_probe function
1007ca632f55SGrant Likely * It will free the IRQ and SPI controller's memory region.
1008ca632f55SGrant Likely * It will also call spi_bitbang_stop to destroy the work queue which was
1009ca632f55SGrant Likely * created by spi_bitbang_start.
1010ca632f55SGrant Likely */
davinci_spi_remove(struct platform_device * pdev)1011d1a9fa8aSUwe Kleine-König static void davinci_spi_remove(struct platform_device *pdev)
1012ca632f55SGrant Likely {
1013ca632f55SGrant Likely struct davinci_spi *dspi;
1014*ad5602baSYang Yingliang struct spi_controller *host;
1015ca632f55SGrant Likely
1016*ad5602baSYang Yingliang host = platform_get_drvdata(pdev);
1017*ad5602baSYang Yingliang dspi = spi_controller_get_devdata(host);
1018ca632f55SGrant Likely
1019ca632f55SGrant Likely spi_bitbang_stop(&dspi->bitbang);
1020ca632f55SGrant Likely
1021aae7147dSMurali Karicheri clk_disable_unprepare(dspi->clk);
1022ca632f55SGrant Likely
1023fe5fd254SPeter Ujfalusi if (dspi->dma_rx) {
1024fe5fd254SPeter Ujfalusi dma_release_channel(dspi->dma_rx);
1025fe5fd254SPeter Ujfalusi dma_release_channel(dspi->dma_tx);
1026fe5fd254SPeter Ujfalusi }
1027fe5fd254SPeter Ujfalusi
1028*ad5602baSYang Yingliang spi_controller_put(host);
1029ca632f55SGrant Likely }
1030ca632f55SGrant Likely
1031ca632f55SGrant Likely static struct platform_driver davinci_spi_driver = {
1032ca632f55SGrant Likely .driver = {
1033ca632f55SGrant Likely .name = "spi_davinci",
1034b53b34f0SAxel Lin .of_match_table = of_match_ptr(davinci_spi_of_match),
1035ca632f55SGrant Likely },
1036940ab889SGrant Likely .probe = davinci_spi_probe,
1037d1a9fa8aSUwe Kleine-König .remove_new = davinci_spi_remove,
1038ca632f55SGrant Likely };
1039940ab889SGrant Likely module_platform_driver(davinci_spi_driver);
1040ca632f55SGrant Likely
1041ca632f55SGrant Likely MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
1042ca632f55SGrant Likely MODULE_LICENSE("GPL");
1043