]> Git Repo - linux.git/blame - drivers/spi/spi-lantiq-ssc.c
spi: lantiq: Add fifo size bit mask in SoC specific data structure
[linux.git] / drivers / spi / spi-lantiq-ssc.c
CommitLineData
7876981a 1// SPDX-License-Identifier: GPL-2.0-only
17f84b79
HM
2/*
3 * Copyright (C) 2011-2015 Daniel Schwierzeck <[email protected]>
4 * Copyright (C) 2016 Hauke Mehrtens <[email protected]>
17f84b79
HM
5 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/of_device.h>
10#include <linux/clk.h>
11#include <linux/io.h>
12#include <linux/delay.h>
13#include <linux/interrupt.h>
14#include <linux/sched.h>
15#include <linux/completion.h>
16#include <linux/spinlock.h>
17#include <linux/err.h>
17f84b79
HM
18#include <linux/pm_runtime.h>
19#include <linux/spi/spi.h>
20
21#ifdef CONFIG_LANTIQ
22#include <lantiq_soc.h>
23#endif
24
ad2fca07
HM
25#define LTQ_SPI_RX_IRQ_NAME "spi_rx"
26#define LTQ_SPI_TX_IRQ_NAME "spi_tx"
27#define LTQ_SPI_ERR_IRQ_NAME "spi_err"
28#define LTQ_SPI_FRM_IRQ_NAME "spi_frm"
29
30#define LTQ_SPI_CLC 0x00
31#define LTQ_SPI_PISEL 0x04
32#define LTQ_SPI_ID 0x08
33#define LTQ_SPI_CON 0x10
34#define LTQ_SPI_STAT 0x14
35#define LTQ_SPI_WHBSTATE 0x18
36#define LTQ_SPI_TB 0x20
37#define LTQ_SPI_RB 0x24
38#define LTQ_SPI_RXFCON 0x30
39#define LTQ_SPI_TXFCON 0x34
40#define LTQ_SPI_FSTAT 0x38
41#define LTQ_SPI_BRT 0x40
42#define LTQ_SPI_BRSTAT 0x44
43#define LTQ_SPI_SFCON 0x60
44#define LTQ_SPI_SFSTAT 0x64
45#define LTQ_SPI_GPOCON 0x70
46#define LTQ_SPI_GPOSTAT 0x74
47#define LTQ_SPI_FPGO 0x78
48#define LTQ_SPI_RXREQ 0x80
49#define LTQ_SPI_RXCNT 0x84
50#define LTQ_SPI_DMACON 0xec
51#define LTQ_SPI_IRNEN 0xf4
ad2fca07
HM
52
53#define LTQ_SPI_CLC_SMC_S 16 /* Clock divider for sleep mode */
54#define LTQ_SPI_CLC_SMC_M (0xFF << LTQ_SPI_CLC_SMC_S)
55#define LTQ_SPI_CLC_RMC_S 8 /* Clock divider for normal run mode */
56#define LTQ_SPI_CLC_RMC_M (0xFF << LTQ_SPI_CLC_RMC_S)
57#define LTQ_SPI_CLC_DISS BIT(1) /* Disable status bit */
58#define LTQ_SPI_CLC_DISR BIT(0) /* Disable request bit */
59
60#define LTQ_SPI_ID_TXFS_S 24 /* Implemented TX FIFO size */
ad2fca07 61#define LTQ_SPI_ID_RXFS_S 16 /* Implemented RX FIFO size */
ad2fca07
HM
62#define LTQ_SPI_ID_MOD_S 8 /* Module ID */
63#define LTQ_SPI_ID_MOD_M (0xff << LTQ_SPI_ID_MOD_S)
64#define LTQ_SPI_ID_CFG_S 5 /* DMA interface support */
65#define LTQ_SPI_ID_CFG_M (1 << LTQ_SPI_ID_CFG_S)
66#define LTQ_SPI_ID_REV_M 0x1F /* Hardware revision number */
67
68#define LTQ_SPI_CON_BM_S 16 /* Data width selection */
69#define LTQ_SPI_CON_BM_M (0x1F << LTQ_SPI_CON_BM_S)
70#define LTQ_SPI_CON_EM BIT(24) /* Echo mode */
71#define LTQ_SPI_CON_IDLE BIT(23) /* Idle bit value */
72#define LTQ_SPI_CON_ENBV BIT(22) /* Enable byte valid control */
73#define LTQ_SPI_CON_RUEN BIT(12) /* Receive underflow error enable */
74#define LTQ_SPI_CON_TUEN BIT(11) /* Transmit underflow error enable */
75#define LTQ_SPI_CON_AEN BIT(10) /* Abort error enable */
76#define LTQ_SPI_CON_REN BIT(9) /* Receive overflow error enable */
77#define LTQ_SPI_CON_TEN BIT(8) /* Transmit overflow error enable */
78#define LTQ_SPI_CON_LB BIT(7) /* Loopback control */
79#define LTQ_SPI_CON_PO BIT(6) /* Clock polarity control */
80#define LTQ_SPI_CON_PH BIT(5) /* Clock phase control */
81#define LTQ_SPI_CON_HB BIT(4) /* Heading control */
82#define LTQ_SPI_CON_RXOFF BIT(1) /* Switch receiver off */
83#define LTQ_SPI_CON_TXOFF BIT(0) /* Switch transmitter off */
84
85#define LTQ_SPI_STAT_RXBV_S 28
86#define LTQ_SPI_STAT_RXBV_M (0x7 << LTQ_SPI_STAT_RXBV_S)
87#define LTQ_SPI_STAT_BSY BIT(13) /* Busy flag */
88#define LTQ_SPI_STAT_RUE BIT(12) /* Receive underflow error flag */
89#define LTQ_SPI_STAT_TUE BIT(11) /* Transmit underflow error flag */
90#define LTQ_SPI_STAT_AE BIT(10) /* Abort error flag */
91#define LTQ_SPI_STAT_RE BIT(9) /* Receive error flag */
92#define LTQ_SPI_STAT_TE BIT(8) /* Transmit error flag */
93#define LTQ_SPI_STAT_ME BIT(7) /* Mode error flag */
94#define LTQ_SPI_STAT_MS BIT(1) /* Master/slave select bit */
95#define LTQ_SPI_STAT_EN BIT(0) /* Enable bit */
96#define LTQ_SPI_STAT_ERRORS (LTQ_SPI_STAT_ME | LTQ_SPI_STAT_TE | \
97 LTQ_SPI_STAT_RE | LTQ_SPI_STAT_AE | \
98 LTQ_SPI_STAT_TUE | LTQ_SPI_STAT_RUE)
99
100#define LTQ_SPI_WHBSTATE_SETTUE BIT(15) /* Set transmit underflow error flag */
101#define LTQ_SPI_WHBSTATE_SETAE BIT(14) /* Set abort error flag */
102#define LTQ_SPI_WHBSTATE_SETRE BIT(13) /* Set receive error flag */
103#define LTQ_SPI_WHBSTATE_SETTE BIT(12) /* Set transmit error flag */
104#define LTQ_SPI_WHBSTATE_CLRTUE BIT(11) /* Clear transmit underflow error flag */
105#define LTQ_SPI_WHBSTATE_CLRAE BIT(10) /* Clear abort error flag */
106#define LTQ_SPI_WHBSTATE_CLRRE BIT(9) /* Clear receive error flag */
107#define LTQ_SPI_WHBSTATE_CLRTE BIT(8) /* Clear transmit error flag */
108#define LTQ_SPI_WHBSTATE_SETME BIT(7) /* Set mode error flag */
109#define LTQ_SPI_WHBSTATE_CLRME BIT(6) /* Clear mode error flag */
110#define LTQ_SPI_WHBSTATE_SETRUE BIT(5) /* Set receive underflow error flag */
111#define LTQ_SPI_WHBSTATE_CLRRUE BIT(4) /* Clear receive underflow error flag */
112#define LTQ_SPI_WHBSTATE_SETMS BIT(3) /* Set master select bit */
113#define LTQ_SPI_WHBSTATE_CLRMS BIT(2) /* Clear master select bit */
114#define LTQ_SPI_WHBSTATE_SETEN BIT(1) /* Set enable bit (operational mode) */
115#define LTQ_SPI_WHBSTATE_CLREN BIT(0) /* Clear enable bit (config mode */
116#define LTQ_SPI_WHBSTATE_CLR_ERRORS (LTQ_SPI_WHBSTATE_CLRRUE | \
117 LTQ_SPI_WHBSTATE_CLRME | \
118 LTQ_SPI_WHBSTATE_CLRTE | \
119 LTQ_SPI_WHBSTATE_CLRRE | \
120 LTQ_SPI_WHBSTATE_CLRAE | \
121 LTQ_SPI_WHBSTATE_CLRTUE)
122
123#define LTQ_SPI_RXFCON_RXFITL_S 8 /* FIFO interrupt trigger level */
ad2fca07
HM
124#define LTQ_SPI_RXFCON_RXFLU BIT(1) /* FIFO flush */
125#define LTQ_SPI_RXFCON_RXFEN BIT(0) /* FIFO enable */
126
127#define LTQ_SPI_TXFCON_TXFITL_S 8 /* FIFO interrupt trigger level */
ad2fca07
HM
128#define LTQ_SPI_TXFCON_TXFLU BIT(1) /* FIFO flush */
129#define LTQ_SPI_TXFCON_TXFEN BIT(0) /* FIFO enable */
130
131#define LTQ_SPI_FSTAT_RXFFL_S 0
ad2fca07 132#define LTQ_SPI_FSTAT_TXFFL_S 8
ad2fca07
HM
133
134#define LTQ_SPI_GPOCON_ISCSBN_S 8
135#define LTQ_SPI_GPOCON_INVOUTN_S 0
136
137#define LTQ_SPI_FGPO_SETOUTN_S 8
138#define LTQ_SPI_FGPO_CLROUTN_S 0
139
140#define LTQ_SPI_RXREQ_RXCNT_M 0xFFFF /* Receive count value */
141#define LTQ_SPI_RXCNT_TODO_M 0xFFFF /* Recevie to-do value */
142
143#define LTQ_SPI_IRNEN_TFI BIT(4) /* TX finished interrupt */
144#define LTQ_SPI_IRNEN_F BIT(3) /* Frame end interrupt request */
145#define LTQ_SPI_IRNEN_E BIT(2) /* Error end interrupt request */
146#define LTQ_SPI_IRNEN_T_XWAY BIT(1) /* Transmit end interrupt request */
147#define LTQ_SPI_IRNEN_R_XWAY BIT(0) /* Receive end interrupt request */
148#define LTQ_SPI_IRNEN_R_XRX BIT(1) /* Transmit end interrupt request */
149#define LTQ_SPI_IRNEN_T_XRX BIT(0) /* Receive end interrupt request */
150#define LTQ_SPI_IRNEN_ALL 0x1F
17f84b79
HM
151
152struct lantiq_ssc_hwcfg {
8d19d665
DK
153 unsigned int irnen_r;
154 unsigned int irnen_t;
155 unsigned int irncr;
156 unsigned int irnicr;
94eca904 157 bool irq_ack;
8743d215 158 u32 fifo_size_mask;
17f84b79
HM
159};
160
161struct lantiq_ssc_spi {
162 struct spi_master *master;
163 struct device *dev;
164 void __iomem *regbase;
165 struct clk *spi_clk;
166 struct clk *fpi_clk;
167 const struct lantiq_ssc_hwcfg *hwcfg;
168
169 spinlock_t lock;
170 struct workqueue_struct *wq;
171 struct work_struct work;
172
173 const u8 *tx;
174 u8 *rx;
175 unsigned int tx_todo;
176 unsigned int rx_todo;
177 unsigned int bits_per_word;
178 unsigned int speed_hz;
179 unsigned int tx_fifo_size;
180 unsigned int rx_fifo_size;
181 unsigned int base_cs;
661ccf2b 182 unsigned int fdx_tx_level;
17f84b79
HM
183};
184
185static u32 lantiq_ssc_readl(const struct lantiq_ssc_spi *spi, u32 reg)
186{
187 return __raw_readl(spi->regbase + reg);
188}
189
190static void lantiq_ssc_writel(const struct lantiq_ssc_spi *spi, u32 val,
191 u32 reg)
192{
193 __raw_writel(val, spi->regbase + reg);
194}
195
196static void lantiq_ssc_maskl(const struct lantiq_ssc_spi *spi, u32 clr,
197 u32 set, u32 reg)
198{
199 u32 val = __raw_readl(spi->regbase + reg);
200
201 val &= ~clr;
202 val |= set;
203 __raw_writel(val, spi->regbase + reg);
204}
205
206static unsigned int tx_fifo_level(const struct lantiq_ssc_spi *spi)
207{
8743d215 208 const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
ad2fca07 209 u32 fstat = lantiq_ssc_readl(spi, LTQ_SPI_FSTAT);
17f84b79 210
8743d215 211 return (fstat >> LTQ_SPI_FSTAT_TXFFL_S) & hwcfg->fifo_size_mask;
17f84b79
HM
212}
213
214static unsigned int rx_fifo_level(const struct lantiq_ssc_spi *spi)
215{
8743d215 216 const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
ad2fca07 217 u32 fstat = lantiq_ssc_readl(spi, LTQ_SPI_FSTAT);
17f84b79 218
8743d215 219 return (fstat >> LTQ_SPI_FSTAT_RXFFL_S) & hwcfg->fifo_size_mask;
17f84b79
HM
220}
221
222static unsigned int tx_fifo_free(const struct lantiq_ssc_spi *spi)
223{
224 return spi->tx_fifo_size - tx_fifo_level(spi);
225}
226
227static void rx_fifo_reset(const struct lantiq_ssc_spi *spi)
228{
ad2fca07 229 u32 val = spi->rx_fifo_size << LTQ_SPI_RXFCON_RXFITL_S;
17f84b79 230
ad2fca07
HM
231 val |= LTQ_SPI_RXFCON_RXFEN | LTQ_SPI_RXFCON_RXFLU;
232 lantiq_ssc_writel(spi, val, LTQ_SPI_RXFCON);
17f84b79
HM
233}
234
235static void tx_fifo_reset(const struct lantiq_ssc_spi *spi)
236{
ad2fca07 237 u32 val = 1 << LTQ_SPI_TXFCON_TXFITL_S;
17f84b79 238
ad2fca07
HM
239 val |= LTQ_SPI_TXFCON_TXFEN | LTQ_SPI_TXFCON_TXFLU;
240 lantiq_ssc_writel(spi, val, LTQ_SPI_TXFCON);
17f84b79
HM
241}
242
243static void rx_fifo_flush(const struct lantiq_ssc_spi *spi)
244{
ad2fca07 245 lantiq_ssc_maskl(spi, 0, LTQ_SPI_RXFCON_RXFLU, LTQ_SPI_RXFCON);
17f84b79
HM
246}
247
248static void tx_fifo_flush(const struct lantiq_ssc_spi *spi)
249{
ad2fca07 250 lantiq_ssc_maskl(spi, 0, LTQ_SPI_TXFCON_TXFLU, LTQ_SPI_TXFCON);
17f84b79
HM
251}
252
253static void hw_enter_config_mode(const struct lantiq_ssc_spi *spi)
254{
ad2fca07 255 lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_CLREN, LTQ_SPI_WHBSTATE);
17f84b79
HM
256}
257
258static void hw_enter_active_mode(const struct lantiq_ssc_spi *spi)
259{
ad2fca07 260 lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_SETEN, LTQ_SPI_WHBSTATE);
17f84b79
HM
261}
262
263static void hw_setup_speed_hz(const struct lantiq_ssc_spi *spi,
264 unsigned int max_speed_hz)
265{
266 u32 spi_clk, brt;
267
268 /*
269 * SPI module clock is derived from FPI bus clock dependent on
270 * divider value in CLC.RMS which is always set to 1.
271 *
272 * f_SPI
273 * baudrate = --------------
274 * 2 * (BR + 1)
275 */
276 spi_clk = clk_get_rate(spi->fpi_clk) / 2;
277
278 if (max_speed_hz > spi_clk)
279 brt = 0;
280 else
281 brt = spi_clk / max_speed_hz - 1;
282
283 if (brt > 0xFFFF)
284 brt = 0xFFFF;
285
286 dev_dbg(spi->dev, "spi_clk %u, max_speed_hz %u, brt %u\n",
287 spi_clk, max_speed_hz, brt);
288
ad2fca07 289 lantiq_ssc_writel(spi, brt, LTQ_SPI_BRT);
17f84b79
HM
290}
291
292static void hw_setup_bits_per_word(const struct lantiq_ssc_spi *spi,
293 unsigned int bits_per_word)
294{
295 u32 bm;
296
297 /* CON.BM value = bits_per_word - 1 */
ad2fca07 298 bm = (bits_per_word - 1) << LTQ_SPI_CON_BM_S;
17f84b79 299
ad2fca07 300 lantiq_ssc_maskl(spi, LTQ_SPI_CON_BM_M, bm, LTQ_SPI_CON);
17f84b79
HM
301}
302
303static void hw_setup_clock_mode(const struct lantiq_ssc_spi *spi,
304 unsigned int mode)
305{
306 u32 con_set = 0, con_clr = 0;
307
308 /*
309 * SPI mode mapping in CON register:
310 * Mode CPOL CPHA CON.PO CON.PH
311 * 0 0 0 0 1
312 * 1 0 1 0 0
313 * 2 1 0 1 1
314 * 3 1 1 1 0
315 */
316 if (mode & SPI_CPHA)
ad2fca07 317 con_clr |= LTQ_SPI_CON_PH;
17f84b79 318 else
ad2fca07 319 con_set |= LTQ_SPI_CON_PH;
17f84b79
HM
320
321 if (mode & SPI_CPOL)
ad2fca07 322 con_set |= LTQ_SPI_CON_PO | LTQ_SPI_CON_IDLE;
17f84b79 323 else
ad2fca07 324 con_clr |= LTQ_SPI_CON_PO | LTQ_SPI_CON_IDLE;
17f84b79
HM
325
326 /* Set heading control */
327 if (mode & SPI_LSB_FIRST)
ad2fca07 328 con_clr |= LTQ_SPI_CON_HB;
17f84b79 329 else
ad2fca07 330 con_set |= LTQ_SPI_CON_HB;
17f84b79
HM
331
332 /* Set loopback mode */
333 if (mode & SPI_LOOP)
ad2fca07 334 con_set |= LTQ_SPI_CON_LB;
17f84b79 335 else
ad2fca07 336 con_clr |= LTQ_SPI_CON_LB;
17f84b79 337
ad2fca07 338 lantiq_ssc_maskl(spi, con_clr, con_set, LTQ_SPI_CON);
17f84b79
HM
339}
340
341static void lantiq_ssc_hw_init(const struct lantiq_ssc_spi *spi)
342{
343 const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
344
345 /*
346 * Set clock divider for run mode to 1 to
347 * run at same frequency as FPI bus
348 */
ad2fca07 349 lantiq_ssc_writel(spi, 1 << LTQ_SPI_CLC_RMC_S, LTQ_SPI_CLC);
17f84b79
HM
350
351 /* Put controller into config mode */
352 hw_enter_config_mode(spi);
353
354 /* Clear error flags */
ad2fca07 355 lantiq_ssc_maskl(spi, 0, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
17f84b79
HM
356
357 /* Enable error checking, disable TX/RX */
ad2fca07
HM
358 lantiq_ssc_writel(spi, LTQ_SPI_CON_RUEN | LTQ_SPI_CON_AEN |
359 LTQ_SPI_CON_TEN | LTQ_SPI_CON_REN | LTQ_SPI_CON_TXOFF |
360 LTQ_SPI_CON_RXOFF, LTQ_SPI_CON);
17f84b79
HM
361
362 /* Setup default SPI mode */
363 hw_setup_bits_per_word(spi, spi->bits_per_word);
364 hw_setup_clock_mode(spi, SPI_MODE_0);
365
366 /* Enable master mode and clear error flags */
ad2fca07
HM
367 lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_SETMS |
368 LTQ_SPI_WHBSTATE_CLR_ERRORS,
369 LTQ_SPI_WHBSTATE);
17f84b79
HM
370
371 /* Reset GPIO/CS registers */
ad2fca07
HM
372 lantiq_ssc_writel(spi, 0, LTQ_SPI_GPOCON);
373 lantiq_ssc_writel(spi, 0xFF00, LTQ_SPI_FPGO);
17f84b79
HM
374
375 /* Enable and flush FIFOs */
376 rx_fifo_reset(spi);
377 tx_fifo_reset(spi);
378
379 /* Enable interrupts */
ad2fca07
HM
380 lantiq_ssc_writel(spi, hwcfg->irnen_t | hwcfg->irnen_r |
381 LTQ_SPI_IRNEN_E, LTQ_SPI_IRNEN);
17f84b79
HM
382}
383
384static int lantiq_ssc_setup(struct spi_device *spidev)
385{
386 struct spi_master *master = spidev->master;
387 struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
388 unsigned int cs = spidev->chip_select;
389 u32 gpocon;
390
391 /* GPIOs are used for CS */
95f2fd2e 392 if (spidev->cs_gpiod)
17f84b79
HM
393 return 0;
394
395 dev_dbg(spi->dev, "using internal chipselect %u\n", cs);
396
397 if (cs < spi->base_cs) {
398 dev_err(spi->dev,
399 "chipselect %i too small (min %i)\n", cs, spi->base_cs);
400 return -EINVAL;
401 }
402
403 /* set GPO pin to CS mode */
ad2fca07 404 gpocon = 1 << ((cs - spi->base_cs) + LTQ_SPI_GPOCON_ISCSBN_S);
17f84b79
HM
405
406 /* invert GPO pin */
407 if (spidev->mode & SPI_CS_HIGH)
408 gpocon |= 1 << (cs - spi->base_cs);
409
ad2fca07 410 lantiq_ssc_maskl(spi, 0, gpocon, LTQ_SPI_GPOCON);
17f84b79
HM
411
412 return 0;
413}
414
415static int lantiq_ssc_prepare_message(struct spi_master *master,
416 struct spi_message *message)
417{
418 struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
419
420 hw_enter_config_mode(spi);
421 hw_setup_clock_mode(spi, message->spi->mode);
422 hw_enter_active_mode(spi);
423
424 return 0;
425}
426
427static void hw_setup_transfer(struct lantiq_ssc_spi *spi,
428 struct spi_device *spidev, struct spi_transfer *t)
429{
430 unsigned int speed_hz = t->speed_hz;
431 unsigned int bits_per_word = t->bits_per_word;
432 u32 con;
433
434 if (bits_per_word != spi->bits_per_word ||
435 speed_hz != spi->speed_hz) {
436 hw_enter_config_mode(spi);
437 hw_setup_speed_hz(spi, speed_hz);
438 hw_setup_bits_per_word(spi, bits_per_word);
439 hw_enter_active_mode(spi);
440
441 spi->speed_hz = speed_hz;
442 spi->bits_per_word = bits_per_word;
443 }
444
445 /* Configure transmitter and receiver */
ad2fca07 446 con = lantiq_ssc_readl(spi, LTQ_SPI_CON);
17f84b79 447 if (t->tx_buf)
ad2fca07 448 con &= ~LTQ_SPI_CON_TXOFF;
17f84b79 449 else
ad2fca07 450 con |= LTQ_SPI_CON_TXOFF;
17f84b79
HM
451
452 if (t->rx_buf)
ad2fca07 453 con &= ~LTQ_SPI_CON_RXOFF;
17f84b79 454 else
ad2fca07 455 con |= LTQ_SPI_CON_RXOFF;
17f84b79 456
ad2fca07 457 lantiq_ssc_writel(spi, con, LTQ_SPI_CON);
17f84b79
HM
458}
459
460static int lantiq_ssc_unprepare_message(struct spi_master *master,
461 struct spi_message *message)
462{
463 struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
464
465 flush_workqueue(spi->wq);
466
467 /* Disable transmitter and receiver while idle */
ad2fca07
HM
468 lantiq_ssc_maskl(spi, 0, LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF,
469 LTQ_SPI_CON);
17f84b79
HM
470
471 return 0;
472}
473
474static void tx_fifo_write(struct lantiq_ssc_spi *spi)
475{
476 const u8 *tx8;
477 const u16 *tx16;
478 const u32 *tx32;
479 u32 data;
480 unsigned int tx_free = tx_fifo_free(spi);
481
661ccf2b 482 spi->fdx_tx_level = 0;
17f84b79
HM
483 while (spi->tx_todo && tx_free) {
484 switch (spi->bits_per_word) {
485 case 2 ... 8:
486 tx8 = spi->tx;
487 data = *tx8;
488 spi->tx_todo--;
489 spi->tx++;
490 break;
491 case 16:
492 tx16 = (u16 *) spi->tx;
493 data = *tx16;
494 spi->tx_todo -= 2;
495 spi->tx += 2;
496 break;
497 case 32:
498 tx32 = (u32 *) spi->tx;
499 data = *tx32;
500 spi->tx_todo -= 4;
501 spi->tx += 4;
502 break;
503 default:
504 WARN_ON(1);
505 data = 0;
506 break;
507 }
508
ad2fca07 509 lantiq_ssc_writel(spi, data, LTQ_SPI_TB);
17f84b79 510 tx_free--;
661ccf2b 511 spi->fdx_tx_level++;
17f84b79
HM
512 }
513}
514
515static void rx_fifo_read_full_duplex(struct lantiq_ssc_spi *spi)
516{
517 u8 *rx8;
518 u16 *rx16;
519 u32 *rx32;
520 u32 data;
521 unsigned int rx_fill = rx_fifo_level(spi);
522
661ccf2b
DK
523 /*
524 * Wait until all expected data to be shifted in.
525 * Otherwise, rx overrun may occur.
526 */
527 while (rx_fill != spi->fdx_tx_level)
528 rx_fill = rx_fifo_level(spi);
529
17f84b79 530 while (rx_fill) {
ad2fca07 531 data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
17f84b79
HM
532
533 switch (spi->bits_per_word) {
534 case 2 ... 8:
535 rx8 = spi->rx;
536 *rx8 = data;
537 spi->rx_todo--;
538 spi->rx++;
539 break;
540 case 16:
541 rx16 = (u16 *) spi->rx;
542 *rx16 = data;
543 spi->rx_todo -= 2;
544 spi->rx += 2;
545 break;
546 case 32:
547 rx32 = (u32 *) spi->rx;
548 *rx32 = data;
549 spi->rx_todo -= 4;
550 spi->rx += 4;
551 break;
552 default:
553 WARN_ON(1);
554 break;
555 }
556
557 rx_fill--;
558 }
559}
560
561static void rx_fifo_read_half_duplex(struct lantiq_ssc_spi *spi)
562{
563 u32 data, *rx32;
564 u8 *rx8;
565 unsigned int rxbv, shift;
566 unsigned int rx_fill = rx_fifo_level(spi);
567
568 /*
569 * In RX-only mode the bits per word value is ignored by HW. A value
570 * of 32 is used instead. Thus all 4 bytes per FIFO must be read.
571 * If remaining RX bytes are less than 4, the FIFO must be read
572 * differently. The amount of received and valid bytes is indicated
573 * by STAT.RXBV register value.
574 */
575 while (rx_fill) {
576 if (spi->rx_todo < 4) {
ad2fca07
HM
577 rxbv = (lantiq_ssc_readl(spi, LTQ_SPI_STAT) &
578 LTQ_SPI_STAT_RXBV_M) >> LTQ_SPI_STAT_RXBV_S;
579 data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
17f84b79
HM
580
581 shift = (rxbv - 1) * 8;
582 rx8 = spi->rx;
583
584 while (rxbv) {
585 *rx8++ = (data >> shift) & 0xFF;
586 rxbv--;
587 shift -= 8;
588 spi->rx_todo--;
589 spi->rx++;
590 }
591 } else {
ad2fca07 592 data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
17f84b79
HM
593 rx32 = (u32 *) spi->rx;
594
595 *rx32++ = data;
596 spi->rx_todo -= 4;
597 spi->rx += 4;
598 }
599 rx_fill--;
600 }
601}
602
603static void rx_request(struct lantiq_ssc_spi *spi)
604{
605 unsigned int rxreq, rxreq_max;
606
607 /*
608 * To avoid receive overflows at high clocks it is better to request
609 * only the amount of bytes that fits into all FIFOs. This value
610 * depends on the FIFO size implemented in hardware.
611 */
612 rxreq = spi->rx_todo;
613 rxreq_max = spi->rx_fifo_size * 4;
614 if (rxreq > rxreq_max)
615 rxreq = rxreq_max;
616
ad2fca07 617 lantiq_ssc_writel(spi, rxreq, LTQ_SPI_RXREQ);
17f84b79
HM
618}
619
620static irqreturn_t lantiq_ssc_xmit_interrupt(int irq, void *data)
621{
622 struct lantiq_ssc_spi *spi = data;
94eca904
DK
623 const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
624 u32 val = lantiq_ssc_readl(spi, hwcfg->irncr);
ddf41bf7 625 unsigned long flags;
17f84b79 626
ddf41bf7 627 spin_lock_irqsave(&spi->lock, flags);
94eca904
DK
628 if (hwcfg->irq_ack)
629 lantiq_ssc_writel(spi, val, hwcfg->irncr);
630
17f84b79
HM
631 if (spi->tx) {
632 if (spi->rx && spi->rx_todo)
633 rx_fifo_read_full_duplex(spi);
634
635 if (spi->tx_todo)
636 tx_fifo_write(spi);
637 else if (!tx_fifo_level(spi))
638 goto completed;
639 } else if (spi->rx) {
640 if (spi->rx_todo) {
641 rx_fifo_read_half_duplex(spi);
642
643 if (spi->rx_todo)
644 rx_request(spi);
645 else
646 goto completed;
647 } else {
648 goto completed;
649 }
650 }
651
ddf41bf7 652 spin_unlock_irqrestore(&spi->lock, flags);
17f84b79
HM
653 return IRQ_HANDLED;
654
655completed:
656 queue_work(spi->wq, &spi->work);
ddf41bf7 657 spin_unlock_irqrestore(&spi->lock, flags);
17f84b79
HM
658
659 return IRQ_HANDLED;
660}
661
662static irqreturn_t lantiq_ssc_err_interrupt(int irq, void *data)
663{
664 struct lantiq_ssc_spi *spi = data;
94eca904 665 const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
ad2fca07 666 u32 stat = lantiq_ssc_readl(spi, LTQ_SPI_STAT);
94eca904 667 u32 val = lantiq_ssc_readl(spi, hwcfg->irncr);
ddf41bf7 668 unsigned long flags;
17f84b79 669
ad2fca07 670 if (!(stat & LTQ_SPI_STAT_ERRORS))
17f84b79
HM
671 return IRQ_NONE;
672
ddf41bf7 673 spin_lock_irqsave(&spi->lock, flags);
94eca904
DK
674 if (hwcfg->irq_ack)
675 lantiq_ssc_writel(spi, val, hwcfg->irncr);
676
ad2fca07 677 if (stat & LTQ_SPI_STAT_RUE)
17f84b79 678 dev_err(spi->dev, "receive underflow error\n");
ad2fca07 679 if (stat & LTQ_SPI_STAT_TUE)
17f84b79 680 dev_err(spi->dev, "transmit underflow error\n");
ad2fca07 681 if (stat & LTQ_SPI_STAT_AE)
17f84b79 682 dev_err(spi->dev, "abort error\n");
ad2fca07 683 if (stat & LTQ_SPI_STAT_RE)
17f84b79 684 dev_err(spi->dev, "receive overflow error\n");
ad2fca07 685 if (stat & LTQ_SPI_STAT_TE)
17f84b79 686 dev_err(spi->dev, "transmit overflow error\n");
ad2fca07 687 if (stat & LTQ_SPI_STAT_ME)
17f84b79
HM
688 dev_err(spi->dev, "mode error\n");
689
690 /* Clear error flags */
ad2fca07 691 lantiq_ssc_maskl(spi, 0, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
17f84b79
HM
692
693 /* set bad status so it can be retried */
694 if (spi->master->cur_msg)
695 spi->master->cur_msg->status = -EIO;
696 queue_work(spi->wq, &spi->work);
ddf41bf7 697 spin_unlock_irqrestore(&spi->lock, flags);
17f84b79
HM
698
699 return IRQ_HANDLED;
700}
701
702static int transfer_start(struct lantiq_ssc_spi *spi, struct spi_device *spidev,
703 struct spi_transfer *t)
704{
705 unsigned long flags;
706
707 spin_lock_irqsave(&spi->lock, flags);
708
709 spi->tx = t->tx_buf;
710 spi->rx = t->rx_buf;
711
712 if (t->tx_buf) {
713 spi->tx_todo = t->len;
714
715 /* initially fill TX FIFO */
716 tx_fifo_write(spi);
717 }
718
719 if (spi->rx) {
720 spi->rx_todo = t->len;
721
722 /* start shift clock in RX-only mode */
723 if (!spi->tx)
724 rx_request(spi);
725 }
726
727 spin_unlock_irqrestore(&spi->lock, flags);
728
729 return t->len;
730}
731
732/*
733 * The driver only gets an interrupt when the FIFO is empty, but there
734 * is an additional shift register from which the data is written to
735 * the wire. We get the last interrupt when the controller starts to
736 * write the last word to the wire, not when it is finished. Do busy
737 * waiting till it finishes.
738 */
739static void lantiq_ssc_bussy_work(struct work_struct *work)
740{
741 struct lantiq_ssc_spi *spi;
742 unsigned long long timeout = 8LL * 1000LL;
743 unsigned long end;
744
745 spi = container_of(work, typeof(*spi), work);
746
747 do_div(timeout, spi->speed_hz);
748 timeout += timeout + 100; /* some tolerance */
749
750 end = jiffies + msecs_to_jiffies(timeout);
751 do {
ad2fca07 752 u32 stat = lantiq_ssc_readl(spi, LTQ_SPI_STAT);
17f84b79 753
ad2fca07 754 if (!(stat & LTQ_SPI_STAT_BSY)) {
17f84b79
HM
755 spi_finalize_current_transfer(spi->master);
756 return;
757 }
758
759 cond_resched();
760 } while (!time_after_eq(jiffies, end));
761
762 if (spi->master->cur_msg)
763 spi->master->cur_msg->status = -EIO;
764 spi_finalize_current_transfer(spi->master);
765}
766
767static void lantiq_ssc_handle_err(struct spi_master *master,
768 struct spi_message *message)
769{
770 struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
771
772 /* flush FIFOs on timeout */
773 rx_fifo_flush(spi);
774 tx_fifo_flush(spi);
775}
776
777static void lantiq_ssc_set_cs(struct spi_device *spidev, bool enable)
778{
779 struct lantiq_ssc_spi *spi = spi_master_get_devdata(spidev->master);
780 unsigned int cs = spidev->chip_select;
781 u32 fgpo;
782
783 if (!!(spidev->mode & SPI_CS_HIGH) == enable)
784 fgpo = (1 << (cs - spi->base_cs));
785 else
ad2fca07 786 fgpo = (1 << (cs - spi->base_cs + LTQ_SPI_FGPO_SETOUTN_S));
17f84b79 787
ad2fca07 788 lantiq_ssc_writel(spi, fgpo, LTQ_SPI_FPGO);
17f84b79
HM
789}
790
791static int lantiq_ssc_transfer_one(struct spi_master *master,
792 struct spi_device *spidev,
793 struct spi_transfer *t)
794{
795 struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
796
797 hw_setup_transfer(spi, spidev, t);
798
799 return transfer_start(spi, spidev, t);
800}
801
802static const struct lantiq_ssc_hwcfg lantiq_ssc_xway = {
8d19d665
DK
803 .irnen_r = LTQ_SPI_IRNEN_R_XWAY,
804 .irnen_t = LTQ_SPI_IRNEN_T_XWAY,
805 .irnicr = 0xF8,
806 .irncr = 0xFC,
8743d215 807 .fifo_size_mask = GENMASK(5, 0),
94eca904 808 .irq_ack = false,
17f84b79
HM
809};
810
811static const struct lantiq_ssc_hwcfg lantiq_ssc_xrx = {
8d19d665
DK
812 .irnen_r = LTQ_SPI_IRNEN_R_XRX,
813 .irnen_t = LTQ_SPI_IRNEN_T_XRX,
814 .irnicr = 0xF8,
815 .irncr = 0xFC,
8743d215 816 .fifo_size_mask = GENMASK(5, 0),
94eca904 817 .irq_ack = false,
17f84b79
HM
818};
819
820static const struct of_device_id lantiq_ssc_match[] = {
821 { .compatible = "lantiq,ase-spi", .data = &lantiq_ssc_xway, },
822 { .compatible = "lantiq,falcon-spi", .data = &lantiq_ssc_xrx, },
823 { .compatible = "lantiq,xrx100-spi", .data = &lantiq_ssc_xrx, },
824 {},
825};
826MODULE_DEVICE_TABLE(of, lantiq_ssc_match);
827
828static int lantiq_ssc_probe(struct platform_device *pdev)
829{
830 struct device *dev = &pdev->dev;
831 struct spi_master *master;
17f84b79
HM
832 struct lantiq_ssc_spi *spi;
833 const struct lantiq_ssc_hwcfg *hwcfg;
834 const struct of_device_id *match;
835 int err, rx_irq, tx_irq, err_irq;
836 u32 id, supports_dma, revision;
837 unsigned int num_cs;
838
839 match = of_match_device(lantiq_ssc_match, dev);
840 if (!match) {
841 dev_err(dev, "no device match\n");
842 return -EINVAL;
843 }
844 hwcfg = match->data;
845
ad2fca07 846 rx_irq = platform_get_irq_byname(pdev, LTQ_SPI_RX_IRQ_NAME);
6b8ac10e 847 if (rx_irq < 0)
17f84b79 848 return -ENXIO;
17f84b79 849
ad2fca07 850 tx_irq = platform_get_irq_byname(pdev, LTQ_SPI_TX_IRQ_NAME);
6b8ac10e 851 if (tx_irq < 0)
17f84b79 852 return -ENXIO;
17f84b79 853
ad2fca07 854 err_irq = platform_get_irq_byname(pdev, LTQ_SPI_ERR_IRQ_NAME);
6b8ac10e 855 if (err_irq < 0)
17f84b79 856 return -ENXIO;
17f84b79
HM
857
858 master = spi_alloc_master(dev, sizeof(struct lantiq_ssc_spi));
859 if (!master)
860 return -ENOMEM;
861
862 spi = spi_master_get_devdata(master);
863 spi->master = master;
864 spi->dev = dev;
865 spi->hwcfg = hwcfg;
866 platform_set_drvdata(pdev, spi);
22262695 867 spi->regbase = devm_platform_ioremap_resource(pdev, 0);
17f84b79
HM
868 if (IS_ERR(spi->regbase)) {
869 err = PTR_ERR(spi->regbase);
870 goto err_master_put;
871 }
872
873 err = devm_request_irq(dev, rx_irq, lantiq_ssc_xmit_interrupt,
ad2fca07 874 0, LTQ_SPI_RX_IRQ_NAME, spi);
17f84b79
HM
875 if (err)
876 goto err_master_put;
877
878 err = devm_request_irq(dev, tx_irq, lantiq_ssc_xmit_interrupt,
ad2fca07 879 0, LTQ_SPI_TX_IRQ_NAME, spi);
17f84b79
HM
880 if (err)
881 goto err_master_put;
882
883 err = devm_request_irq(dev, err_irq, lantiq_ssc_err_interrupt,
ad2fca07 884 0, LTQ_SPI_ERR_IRQ_NAME, spi);
17f84b79
HM
885 if (err)
886 goto err_master_put;
887
888 spi->spi_clk = devm_clk_get(dev, "gate");
889 if (IS_ERR(spi->spi_clk)) {
890 err = PTR_ERR(spi->spi_clk);
891 goto err_master_put;
892 }
893 err = clk_prepare_enable(spi->spi_clk);
894 if (err)
895 goto err_master_put;
896
897 /*
898 * Use the old clk_get_fpi() function on Lantiq platform, till it
899 * supports common clk.
900 */
901#if defined(CONFIG_LANTIQ) && !defined(CONFIG_COMMON_CLK)
902 spi->fpi_clk = clk_get_fpi();
903#else
904 spi->fpi_clk = clk_get(dev, "freq");
905#endif
906 if (IS_ERR(spi->fpi_clk)) {
907 err = PTR_ERR(spi->fpi_clk);
908 goto err_clk_disable;
909 }
910
911 num_cs = 8;
912 of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);
913
914 spi->base_cs = 1;
915 of_property_read_u32(pdev->dev.of_node, "base-cs", &spi->base_cs);
916
917 spin_lock_init(&spi->lock);
918 spi->bits_per_word = 8;
919 spi->speed_hz = 0;
920
921 master->dev.of_node = pdev->dev.of_node;
922 master->num_chipselect = num_cs;
95f2fd2e 923 master->use_gpio_descriptors = true;
17f84b79
HM
924 master->setup = lantiq_ssc_setup;
925 master->set_cs = lantiq_ssc_set_cs;
926 master->handle_err = lantiq_ssc_handle_err;
927 master->prepare_message = lantiq_ssc_prepare_message;
928 master->unprepare_message = lantiq_ssc_unprepare_message;
929 master->transfer_one = lantiq_ssc_transfer_one;
930 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH |
931 SPI_LOOP;
932 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 8) |
933 SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
934
935 spi->wq = alloc_ordered_workqueue(dev_name(dev), 0);
936 if (!spi->wq) {
937 err = -ENOMEM;
938 goto err_clk_put;
939 }
940 INIT_WORK(&spi->work, lantiq_ssc_bussy_work);
941
ad2fca07 942 id = lantiq_ssc_readl(spi, LTQ_SPI_ID);
8743d215
DK
943 spi->tx_fifo_size = (id >> LTQ_SPI_ID_TXFS_S) & hwcfg->fifo_size_mask;
944 spi->rx_fifo_size = (id >> LTQ_SPI_ID_RXFS_S) & hwcfg->fifo_size_mask;
ad2fca07
HM
945 supports_dma = (id & LTQ_SPI_ID_CFG_M) >> LTQ_SPI_ID_CFG_S;
946 revision = id & LTQ_SPI_ID_REV_M;
17f84b79
HM
947
948 lantiq_ssc_hw_init(spi);
949
950 dev_info(dev,
951 "Lantiq SSC SPI controller (Rev %i, TXFS %u, RXFS %u, DMA %u)\n",
952 revision, spi->tx_fifo_size, spi->rx_fifo_size, supports_dma);
953
954 err = devm_spi_register_master(dev, master);
955 if (err) {
956 dev_err(dev, "failed to register spi_master\n");
957 goto err_wq_destroy;
958 }
959
960 return 0;
961
962err_wq_destroy:
963 destroy_workqueue(spi->wq);
964err_clk_put:
965 clk_put(spi->fpi_clk);
966err_clk_disable:
967 clk_disable_unprepare(spi->spi_clk);
968err_master_put:
969 spi_master_put(master);
970
971 return err;
972}
973
974static int lantiq_ssc_remove(struct platform_device *pdev)
975{
976 struct lantiq_ssc_spi *spi = platform_get_drvdata(pdev);
977
ad2fca07
HM
978 lantiq_ssc_writel(spi, 0, LTQ_SPI_IRNEN);
979 lantiq_ssc_writel(spi, 0, LTQ_SPI_CLC);
17f84b79
HM
980 rx_fifo_flush(spi);
981 tx_fifo_flush(spi);
982 hw_enter_config_mode(spi);
983
984 destroy_workqueue(spi->wq);
985 clk_disable_unprepare(spi->spi_clk);
986 clk_put(spi->fpi_clk);
987
988 return 0;
989}
990
991static struct platform_driver lantiq_ssc_driver = {
992 .probe = lantiq_ssc_probe,
993 .remove = lantiq_ssc_remove,
994 .driver = {
995 .name = "spi-lantiq-ssc",
17f84b79
HM
996 .of_match_table = lantiq_ssc_match,
997 },
998};
999module_platform_driver(lantiq_ssc_driver);
1000
1001MODULE_DESCRIPTION("Lantiq SSC SPI controller driver");
1002MODULE_AUTHOR("Daniel Schwierzeck <[email protected]>");
1003MODULE_AUTHOR("Hauke Mehrtens <[email protected]>");
1004MODULE_LICENSE("GPL");
1005MODULE_ALIAS("platform:spi-lantiq-ssc");
This page took 2.069712 seconds and 4 git commands to generate.