1 From: Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>
2 Date: Thu, 3 Mar 2011 17:15:30 +0000 (+0100)
3 Subject: SPI: lantiq: Add driver for Lantiq SoC SPI controller
4 X-Git-Url: http://nbd.name/gitweb.cgi?p=lantiq.git;a=commitdiff_plain;h=653c95b8b9066c9c6ac08bd64d0ceee439e9fd90;hp=3d21b04682ae8eb1c1965aba39d1796e8c5ad84b
6 SPI: lantiq: Add driver for Lantiq SoC SPI controller
8 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>
11 --- a/drivers/spi/Kconfig
12 +++ b/drivers/spi/Kconfig
14 This enables using the Freescale i.MX SPI controllers in master
18 + tristate "Lantiq SoC SPI controller"
19 + depends on SOC_TYPE_XWAY
22 + This driver supports the Lantiq SoC SPI controller in master
26 tristate "Parallel port adapter for LM70 eval board (DEVELOPMENT)"
27 depends on PARPORT && EXPERIMENTAL
28 --- a/drivers/spi/Makefile
29 +++ b/drivers/spi/Makefile
31 obj-$(CONFIG_SPI_GPIO) += spi_gpio.o
32 obj-$(CONFIG_SPI_GPIO_OLD) += spi_gpio_old.o
33 obj-$(CONFIG_SPI_IMX) += spi_imx.o
34 +obj-$(CONFIG_SPI_LANTIQ) += spi_lantiq.o
35 obj-$(CONFIG_SPI_LM70_LLP) += spi_lm70llp.o
36 obj-$(CONFIG_SPI_PXA2XX) += pxa2xx_spi.o
37 obj-$(CONFIG_SPI_PXA2XX_PCI) += pxa2xx_spi_pci.o
39 +++ b/drivers/spi/spi_lantiq.c
42 + * Lantiq SoC SPI controller
44 + * Copyright (C) 2011 Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>
46 + * This program is free software; you can distribute it and/or modify it
47 + * under the terms of the GNU General Public License (Version 2) as
48 + * published by the Free Software Foundation.
51 +#include <linux/init.h>
52 +#include <linux/module.h>
53 +#include <linux/workqueue.h>
54 +#include <linux/platform_device.h>
55 +#include <linux/io.h>
56 +#include <linux/sched.h>
57 +#include <linux/delay.h>
58 +#include <linux/interrupt.h>
59 +#include <linux/completion.h>
60 +#include <linux/spinlock.h>
61 +#include <linux/err.h>
62 +#include <linux/clk.h>
63 +#include <linux/gpio.h>
64 +#include <linux/spi/spi.h>
65 +#include <linux/spi/spi_bitbang.h>
67 +#include <lantiq_soc.h>
68 +#include <lantiq_platform.h>
70 +#define LTQ_SPI_CLC 0x00 /* Clock control */
71 +#define LTQ_SPI_PISEL 0x04 /* Port input select */
72 +#define LTQ_SPI_ID 0x08 /* Identification */
73 +#define LTQ_SPI_CON 0x10 /* Control */
74 +#define LTQ_SPI_STAT 0x14 /* Status */
75 +#define LTQ_SPI_WHBSTATE 0x18 /* Write HW modified state */
76 +#define LTQ_SPI_TB 0x20 /* Transmit buffer */
77 +#define LTQ_SPI_RB 0x24 /* Receive buffer */
78 +#define LTQ_SPI_RXFCON 0x30 /* Receive FIFO control */
79 +#define LTQ_SPI_TXFCON 0x34 /* Transmit FIFO control */
80 +#define LTQ_SPI_FSTAT 0x38 /* FIFO status */
81 +#define LTQ_SPI_BRT 0x40 /* Baudrate timer */
82 +#define LTQ_SPI_BRSTAT 0x44 /* Baudrate timer status */
83 +#define LTQ_SPI_SFCON 0x60 /* Serial frame control */
84 +#define LTQ_SPI_SFSTAT 0x64 /* Serial frame status */
85 +#define LTQ_SPI_GPOCON 0x70 /* General purpose output control */
86 +#define LTQ_SPI_GPOSTAT 0x74 /* General purpose output status */
87 +#define LTQ_SPI_FGPO 0x78 /* Forced general purpose output */
88 +#define LTQ_SPI_RXREQ 0x80 /* Receive request */
89 +#define LTQ_SPI_RXCNT 0x84 /* Receive count */
90 +#define LTQ_SPI_DMACON 0xEC /* DMA control */
91 +#define LTQ_SPI_IRNEN 0xF4 /* Interrupt node enable */
92 +#define LTQ_SPI_IRNICR 0xF8 /* Interrupt node interrupt capture */
93 +#define LTQ_SPI_IRNCR 0xFC /* Interrupt node control */
95 +#define LTQ_SPI_CLC_SMC_SHIFT 16 /* Clock divider for sleep mode */
96 +#define LTQ_SPI_CLC_SMC_MASK 0xFF
97 +#define LTQ_SPI_CLC_RMC_SHIFT 8 /* Clock divider for normal run mode */
98 +#define LTQ_SPI_CLC_RMC_MASK 0xFF
99 +#define LTQ_SPI_CLC_DISS BIT(1) /* Disable status bit */
100 +#define LTQ_SPI_CLC_DISR BIT(0) /* Disable request bit */
102 +#define LTQ_SPI_ID_TXFS_SHIFT 24 /* Implemented TX FIFO size */
103 +#define LTQ_SPI_ID_TXFS_MASK 0x3F
104 +#define LTQ_SPI_ID_RXFS_SHIFT 16 /* Implemented RX FIFO size */
105 +#define LTQ_SPI_ID_RXFS_MASK 0x3F
106 +#define LTQ_SPI_ID_REV_MASK 0x1F /* Hardware revision number */
107 +#define LTQ_SPI_ID_CFG BIT(5) /* DMA interface support */
109 +#define LTQ_SPI_CON_BM_SHIFT 16 /* Data width selection */
110 +#define LTQ_SPI_CON_BM_MASK 0x1F
111 +#define LTQ_SPI_CON_EM BIT(24) /* Echo mode */
112 +#define LTQ_SPI_CON_IDLE BIT(23) /* Idle bit value */
113 +#define LTQ_SPI_CON_ENBV BIT(22) /* Enable byte valid control */
114 +#define LTQ_SPI_CON_RUEN BIT(12) /* Receive underflow error enable */
115 +#define LTQ_SPI_CON_TUEN BIT(11) /* Transmit underflow error enable */
116 +#define LTQ_SPI_CON_AEN BIT(10) /* Abort error enable */
117 +#define LTQ_SPI_CON_REN BIT(9) /* Receive overflow error enable */
118 +#define LTQ_SPI_CON_TEN BIT(8) /* Transmit overflow error enable */
119 +#define LTQ_SPI_CON_LB BIT(7) /* Loopback control */
120 +#define LTQ_SPI_CON_PO BIT(6) /* Clock polarity control */
121 +#define LTQ_SPI_CON_PH BIT(5) /* Clock phase control */
122 +#define LTQ_SPI_CON_HB BIT(4) /* Heading control */
123 +#define LTQ_SPI_CON_RXOFF BIT(1) /* Switch receiver off */
124 +#define LTQ_SPI_CON_TXOFF BIT(0) /* Switch transmitter off */
126 +#define LTQ_SPI_STAT_RXBV_MASK 0x7
127 +#define LTQ_SPI_STAT_RXBV_SHIFT 28
128 +#define LTQ_SPI_STAT_BSY BIT(13) /* Busy flag */
129 +#define LTQ_SPI_STAT_RUE BIT(12) /* Receive underflow error flag */
130 +#define LTQ_SPI_STAT_TUE BIT(11) /* Transmit underflow error flag */
131 +#define LTQ_SPI_STAT_AE BIT(10) /* Abort error flag */
132 +#define LTQ_SPI_STAT_RE BIT(9) /* Receive error flag */
133 +#define LTQ_SPI_STAT_TE BIT(8) /* Transmit error flag */
134 +#define LTQ_SPI_STAT_MS BIT(1) /* Master/slave select bit */
135 +#define LTQ_SPI_STAT_EN BIT(0) /* Enable bit */
137 +#define LTQ_SPI_WHBSTATE_SETTUE BIT(15) /* Set transmit underflow error flag */
138 +#define LTQ_SPI_WHBSTATE_SETAE BIT(14) /* Set abort error flag */
139 +#define LTQ_SPI_WHBSTATE_SETRE BIT(13) /* Set receive error flag */
140 +#define LTQ_SPI_WHBSTATE_SETTE BIT(12) /* Set transmit error flag */
141 +#define LTQ_SPI_WHBSTATE_CLRTUE BIT(11) /* Clear transmit underflow error flag */
142 +#define LTQ_SPI_WHBSTATE_CLRAE BIT(10) /* Clear abort error flag */
143 +#define LTQ_SPI_WHBSTATE_CLRRE BIT(9) /* Clear receive error flag */
144 +#define LTQ_SPI_WHBSTATE_CLRTE BIT(8) /* Clear transmit error flag */
145 +#define LTQ_SPI_WHBSTATE_SETME BIT(7) /* Set mode error flag */
146 +#define LTQ_SPI_WHBSTATE_CLRME BIT(6) /* Clear mode error flag */
147 +#define LTQ_SPI_WHBSTATE_SETRUE BIT(5) /* Set receive underflow error flag */
148 +#define LTQ_SPI_WHBSTATE_CLRRUE BIT(4) /* Clear receive underflow error flag */
149 +#define LTQ_SPI_WHBSTATE_SETMS BIT(3) /* Set master select bit */
150 +#define LTQ_SPI_WHBSTATE_CLRMS BIT(2) /* Clear master select bit */
151 +#define LTQ_SPI_WHBSTATE_SETEN BIT(1) /* Set enable bit (operational mode) */
152 +#define LTQ_SPI_WHBSTATE_CLREN BIT(0) /* Clear enable bit (config mode */
153 +#define LTQ_SPI_WHBSTATE_CLR_ERRORS 0x0F50
155 +#define LTQ_SPI_RXFCON_RXFITL_SHIFT 8 /* FIFO interrupt trigger level */
156 +#define LTQ_SPI_RXFCON_RXFITL_MASK 0x3F
157 +#define LTQ_SPI_RXFCON_RXFLU BIT(1) /* FIFO flush */
158 +#define LTQ_SPI_RXFCON_RXFEN BIT(0) /* FIFO enable */
160 +#define LTQ_SPI_TXFCON_TXFITL_SHIFT 8 /* FIFO interrupt trigger level */
161 +#define LTQ_SPI_TXFCON_TXFITL_MASK 0x3F
162 +#define LTQ_SPI_TXFCON_TXFLU BIT(1) /* FIFO flush */
163 +#define LTQ_SPI_TXFCON_TXFEN BIT(0) /* FIFO enable */
165 +#define LTQ_SPI_FSTAT_RXFFL_MASK 0x3f
166 +#define LTQ_SPI_FSTAT_RXFFL_SHIFT 0
167 +#define LTQ_SPI_FSTAT_TXFFL_MASK 0x3f
168 +#define LTQ_SPI_FSTAT_TXFFL_SHIFT 8
170 +#define LTQ_SPI_GPOCON_ISCSBN_SHIFT 8
171 +#define LTQ_SPI_GPOCON_INVOUTN_SHIFT 0
173 +#define LTQ_SPI_FGPO_SETOUTN_SHIFT 8
174 +#define LTQ_SPI_FGPO_CLROUTN_SHIFT 0
176 +#define LTQ_SPI_RXREQ_RXCNT_MASK 0xFFFF /* Receive count value */
177 +#define LTQ_SPI_RXCNT_TODO_MASK 0xFFFF /* Recevie to-do value */
179 +#define LTQ_SPI_IRNEN_F BIT(3) /* Frame end interrupt request */
180 +#define LTQ_SPI_IRNEN_E BIT(2) /* Error end interrupt request */
181 +#define LTQ_SPI_IRNEN_T BIT(1) /* Transmit end interrupt request */
182 +#define LTQ_SPI_IRNEN_R BIT(0) /* Receive end interrupt request */
183 +#define LTQ_SPI_IRNEN_ALL 0xF
185 +/* Hard-wired GPIOs used by SPI controller */
186 +#define LTQ_SPI_GPIO_DI 16
187 +#define LTQ_SPI_GPIO_DO 17
188 +#define LTQ_SPI_GPIO_CLK 18
191 + struct spi_bitbang bitbang;
192 + struct completion done;
195 + struct device *dev;
196 + void __iomem *base;
207 + struct spi_transfer *curr_transfer;
209 + u32 (*get_tx) (struct ltq_spi *);
213 + unsigned dma_support:1;
214 + unsigned cfg_mode:1;
218 +struct ltq_spi_controller_state {
219 + void (*cs_activate) (struct spi_device *);
220 + void (*cs_deactivate) (struct spi_device *);
223 +struct ltq_spi_irq_map {
225 + irq_handler_t handler;
228 +struct ltq_spi_cs_gpio_map {
234 +static inline struct ltq_spi *ltq_spi_to_hw(struct spi_device *spi)
236 + return spi_master_get_devdata(spi->master);
239 +static inline u32 ltq_spi_reg_read(struct ltq_spi *hw, u32 reg)
241 + return ioread32be(hw->base + reg);
244 +static inline void ltq_spi_reg_write(struct ltq_spi *hw, u32 val, u32 reg)
246 + iowrite32be(val, hw->base + reg);
249 +static inline void ltq_spi_reg_setbit(struct ltq_spi *hw, u32 bits, u32 reg)
253 + val = ltq_spi_reg_read(hw, reg);
255 + ltq_spi_reg_write(hw, val, reg);
258 +static inline void ltq_spi_reg_clearbit(struct ltq_spi *hw, u32 bits, u32 reg)
262 + val = ltq_spi_reg_read(hw, reg);
264 + ltq_spi_reg_write(hw, val, reg);
267 +static void ltq_spi_hw_enable(struct ltq_spi *hw)
271 + /* Power-up mdule */
272 + ltq_pmu_enable(PMU_SPI);
275 + * Set clock divider for run mode to 1 to
276 + * run at same frequency as FPI bus
278 + clc = (1 << LTQ_SPI_CLC_RMC_SHIFT);
279 + ltq_spi_reg_write(hw, clc, LTQ_SPI_CLC);
282 +static void ltq_spi_hw_disable(struct ltq_spi *hw)
284 + /* Set clock divider to 0 and set module disable bit */
285 + ltq_spi_reg_write(hw, LTQ_SPI_CLC_DISS, LTQ_SPI_CLC);
287 + /* Power-down mdule */
288 + ltq_pmu_disable(PMU_SPI);
291 +static void ltq_spi_reset_fifos(struct ltq_spi *hw)
296 + * Enable and flush FIFOs. Set interrupt trigger level to
297 + * half of FIFO count implemented in hardware.
299 + if (hw->txfs > 1) {
300 + val = hw->txfs << (LTQ_SPI_TXFCON_TXFITL_SHIFT - 1);
301 + val |= LTQ_SPI_TXFCON_TXFEN | LTQ_SPI_TXFCON_TXFLU;
302 + ltq_spi_reg_write(hw, val, LTQ_SPI_TXFCON);
305 + if (hw->rxfs > 1) {
306 + val = hw->rxfs << (LTQ_SPI_RXFCON_RXFITL_SHIFT - 1);
307 + val |= LTQ_SPI_RXFCON_RXFEN | LTQ_SPI_RXFCON_RXFLU;
308 + ltq_spi_reg_write(hw, val, LTQ_SPI_RXFCON);
312 +static inline int ltq_spi_wait_ready(struct ltq_spi *hw)
315 + unsigned long timeout;
317 + timeout = jiffies + msecs_to_jiffies(200);
320 + stat = ltq_spi_reg_read(hw, LTQ_SPI_STAT);
321 + if (!(stat & LTQ_SPI_STAT_BSY))
325 + } while (!time_after_eq(jiffies, timeout));
327 + dev_err(hw->dev, "SPI wait ready timed out\n");
332 +static void ltq_spi_config_mode_set(struct ltq_spi *hw)
338 + * Putting the SPI module in config mode is only safe if no
339 + * transfer is in progress as indicated by busy flag STATE.BSY.
341 + if (ltq_spi_wait_ready(hw)) {
342 + ltq_spi_reset_fifos(hw);
343 + hw->status = -ETIMEDOUT;
345 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_CLREN, LTQ_SPI_WHBSTATE);
350 +static void ltq_spi_run_mode_set(struct ltq_spi *hw)
355 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_SETEN, LTQ_SPI_WHBSTATE);
360 +static u32 ltq_spi_tx_word_u8(struct ltq_spi *hw)
362 + const u8 *tx = hw->tx;
371 +static u32 ltq_spi_tx_word_u16(struct ltq_spi *hw)
373 + const u16 *tx = (u16 *) hw->tx;
382 +static u32 ltq_spi_tx_word_u32(struct ltq_spi *hw)
384 + const u32 *tx = (u32 *) hw->tx;
393 +static void ltq_spi_bits_per_word_set(struct spi_device *spi)
395 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
397 + u8 bits_per_word = spi->bits_per_word;
400 + * Use either default value of SPI device or value
401 + * from current transfer.
403 + if (hw->curr_transfer && hw->curr_transfer->bits_per_word)
404 + bits_per_word = hw->curr_transfer->bits_per_word;
406 + if (bits_per_word <= 8)
407 + hw->get_tx = ltq_spi_tx_word_u8;
408 + else if (bits_per_word <= 16)
409 + hw->get_tx = ltq_spi_tx_word_u16;
410 + else if (bits_per_word <= 32)
411 + hw->get_tx = ltq_spi_tx_word_u32;
413 + /* CON.BM value = bits_per_word - 1 */
414 + bm = (bits_per_word - 1) << LTQ_SPI_CON_BM_SHIFT;
416 + ltq_spi_reg_clearbit(hw, LTQ_SPI_CON_BM_MASK <<
417 + LTQ_SPI_CON_BM_SHIFT, LTQ_SPI_CON);
418 + ltq_spi_reg_setbit(hw, bm, LTQ_SPI_CON);
421 +static void ltq_spi_speed_set(struct spi_device *spi)
423 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
424 + u32 br, max_speed_hz, spi_clk;
425 + u32 speed_hz = spi->max_speed_hz;
428 + * Use either default value of SPI device or value
429 + * from current transfer.
431 + if (hw->curr_transfer && hw->curr_transfer->speed_hz)
432 + speed_hz = hw->curr_transfer->speed_hz;
435 + * SPI module clock is derived from FPI bus clock dependent on
436 + * divider value in CLC.RMS which is always set to 1.
438 + spi_clk = clk_get_rate(hw->clk);
441 + * Maximum SPI clock frequency in master mode is half of
442 + * SPI module clock frequency. Maximum reload value of
443 + * baudrate generator BR is 2^16.
445 + max_speed_hz = spi_clk / 2;
446 + if (speed_hz >= max_speed_hz)
449 + br = (max_speed_hz / speed_hz) - 1;
454 + ltq_spi_reg_write(hw, br, LTQ_SPI_BRT);
457 +static void ltq_spi_clockmode_set(struct spi_device *spi)
459 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
462 + con = ltq_spi_reg_read(hw, LTQ_SPI_CON);
465 + * SPI mode mapping in CON register:
466 + * Mode CPOL CPHA CON.PO CON.PH
472 + if (spi->mode & SPI_CPHA)
473 + con &= ~LTQ_SPI_CON_PH;
475 + con |= LTQ_SPI_CON_PH;
477 + if (spi->mode & SPI_CPOL)
478 + con |= LTQ_SPI_CON_PO;
480 + con &= ~LTQ_SPI_CON_PO;
482 + /* Set heading control */
483 + if (spi->mode & SPI_LSB_FIRST)
484 + con &= ~LTQ_SPI_CON_HB;
486 + con |= LTQ_SPI_CON_HB;
488 + ltq_spi_reg_write(hw, con, LTQ_SPI_CON);
491 +static void ltq_spi_xmit_set(struct ltq_spi *hw, struct spi_transfer *t)
495 + con = ltq_spi_reg_read(hw, LTQ_SPI_CON);
498 + if (t->tx_buf && t->rx_buf) {
499 + con &= ~(LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF);
500 + } else if (t->rx_buf) {
501 + con &= ~LTQ_SPI_CON_RXOFF;
502 + con |= LTQ_SPI_CON_TXOFF;
503 + } else if (t->tx_buf) {
504 + con &= ~LTQ_SPI_CON_TXOFF;
505 + con |= LTQ_SPI_CON_RXOFF;
508 + con |= (LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF);
510 + ltq_spi_reg_write(hw, con, LTQ_SPI_CON);
513 +static void ltq_spi_gpio_cs_activate(struct spi_device *spi)
515 + struct ltq_spi_controller_data *cdata = spi->controller_data;
516 + int val = spi->mode & SPI_CS_HIGH ? 1 : 0;
518 + gpio_set_value(cdata->gpio, val);
521 +static void ltq_spi_gpio_cs_deactivate(struct spi_device *spi)
523 + struct ltq_spi_controller_data *cdata = spi->controller_data;
524 + int val = spi->mode & SPI_CS_HIGH ? 0 : 1;
526 + gpio_set_value(cdata->gpio, val);
529 +static void ltq_spi_internal_cs_activate(struct spi_device *spi)
531 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
534 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_CLROUTN_SHIFT));
535 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
538 +static void ltq_spi_internal_cs_deactivate(struct spi_device *spi)
540 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
543 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_SETOUTN_SHIFT));
544 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
547 +static void ltq_spi_chipselect(struct spi_device *spi, int cs)
549 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
550 + struct ltq_spi_controller_state *cstate = spi->controller_state;
553 + case BITBANG_CS_ACTIVE:
554 + ltq_spi_bits_per_word_set(spi);
555 + ltq_spi_speed_set(spi);
556 + ltq_spi_clockmode_set(spi);
557 + ltq_spi_run_mode_set(hw);
559 + cstate->cs_activate(spi);
562 + case BITBANG_CS_INACTIVE:
563 + cstate->cs_deactivate(spi);
565 + ltq_spi_config_mode_set(hw);
571 +static int ltq_spi_setup_transfer(struct spi_device *spi,
572 + struct spi_transfer *t)
574 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
575 + u8 bits_per_word = spi->bits_per_word;
577 + hw->curr_transfer = t;
579 + if (t && t->bits_per_word)
580 + bits_per_word = t->bits_per_word;
582 + if (bits_per_word > 32)
585 + ltq_spi_config_mode_set(hw);
590 +static const struct ltq_spi_cs_gpio_map ltq_spi_cs[] = {
599 +static int ltq_spi_setup(struct spi_device *spi)
601 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
602 + struct ltq_spi_controller_data *cdata = spi->controller_data;
603 + struct ltq_spi_controller_state *cstate;
607 + /* Set default word length to 8 if not set */
608 + if (!spi->bits_per_word)
609 + spi->bits_per_word = 8;
611 + if (spi->bits_per_word > 32)
614 + if (!spi->controller_state) {
615 + cstate = kzalloc(sizeof(struct ltq_spi_controller_state),
620 + spi->controller_state = cstate;
625 + * Up to six GPIOs can be connected to the SPI module
626 + * via GPIO alternate function to control the chip select lines.
627 + * For more flexibility in board layout this driver can also control
628 + * the CS lines via GPIO API. If GPIOs should be used, board setup code
629 + * have to register the SPI device with struct ltq_spi_controller_data
632 + if (cdata && cdata->gpio) {
633 + ret = gpio_request(cdata->gpio, "spi-cs");
637 + ret = spi->mode & SPI_CS_HIGH ? 0 : 1;
638 + gpio_direction_output(cdata->gpio, ret);
640 + cstate->cs_activate = ltq_spi_gpio_cs_activate;
641 + cstate->cs_deactivate = ltq_spi_gpio_cs_deactivate;
643 + ret = ltq_gpio_request(ltq_spi_cs[spi->chip_select].gpio,
644 + ltq_spi_cs[spi->chip_select].altsel0,
645 + ltq_spi_cs[spi->chip_select].altsel1,
650 + gpocon = (1 << (spi->chip_select +
651 + LTQ_SPI_GPOCON_ISCSBN_SHIFT));
653 + if (spi->mode & SPI_CS_HIGH)
654 + gpocon |= (1 << spi->chip_select);
656 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_SETOUTN_SHIFT));
658 + ltq_spi_reg_setbit(hw, gpocon, LTQ_SPI_GPOCON);
659 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
661 + cstate->cs_activate = ltq_spi_internal_cs_activate;
662 + cstate->cs_deactivate = ltq_spi_internal_cs_deactivate;
668 +static void ltq_spi_cleanup(struct spi_device *spi)
670 + struct ltq_spi_controller_data *cdata = spi->controller_data;
671 + struct ltq_spi_controller_state *cstate = spi->controller_state;
674 + if (cdata && cdata->gpio)
675 + gpio = cdata->gpio;
677 + gpio = ltq_spi_cs[spi->chip_select].gpio;
683 +static void ltq_spi_txfifo_write(struct ltq_spi *hw)
688 + /* Determine how much FIFOs are free for TX data */
689 + fstat = ltq_spi_reg_read(hw, LTQ_SPI_FSTAT);
690 + fifo_space = hw->txfs - ((fstat >> LTQ_SPI_FSTAT_TXFFL_SHIFT) &
691 + LTQ_SPI_FSTAT_TXFFL_MASK);
696 + while (hw->tx_cnt < hw->len && fifo_space) {
697 + data = hw->get_tx(hw);
698 + ltq_spi_reg_write(hw, data, LTQ_SPI_TB);
703 +static void ltq_spi_rxfifo_read(struct ltq_spi *hw)
705 + u32 fstat, data, *rx32;
707 + u8 rxbv, shift, *rx8;
709 + /* Determine how much FIFOs are filled with RX data */
710 + fstat = ltq_spi_reg_read(hw, LTQ_SPI_FSTAT);
711 + fifo_fill = ((fstat >> LTQ_SPI_FSTAT_RXFFL_SHIFT)
712 + & LTQ_SPI_FSTAT_RXFFL_MASK);
718 + * The 32 bit FIFO is always used completely independent from the
719 + * bits_per_word value. Thus four bytes have to be read at once
722 + rx32 = (u32 *) hw->rx;
723 + while (hw->len - hw->rx_cnt >= 4 && fifo_fill) {
724 + *rx32++ = ltq_spi_reg_read(hw, LTQ_SPI_RB);
731 + * If there are remaining bytes, read byte count from STAT.RXBV
732 + * register and read the data byte-wise.
734 + while (fifo_fill && hw->rx_cnt < hw->len) {
735 + rxbv = (ltq_spi_reg_read(hw, LTQ_SPI_STAT) >>
736 + LTQ_SPI_STAT_RXBV_SHIFT) & LTQ_SPI_STAT_RXBV_MASK;
737 + data = ltq_spi_reg_read(hw, LTQ_SPI_RB);
739 + shift = (rxbv - 1) * 8;
743 + *rx8++ = (data >> shift) & 0xFF;
754 +static void ltq_spi_rxreq_set(struct ltq_spi *hw)
756 + u32 rxreq, rxreq_max, rxtodo;
758 + rxtodo = ltq_spi_reg_read(hw, LTQ_SPI_RXCNT) & LTQ_SPI_RXCNT_TODO_MASK;
761 + * In RX-only mode the serial clock is activated only after writing
762 + * the expected amount of RX bytes into RXREQ register.
763 + * To avoid receive overflows at high clocks it is better to request
764 + * only the amount of bytes that fits into all FIFOs. This value
765 + * depends on the FIFO size implemented in hardware.
767 + rxreq = hw->len - hw->rx_cnt;
768 + rxreq_max = hw->rxfs << 2;
769 + rxreq = min(rxreq_max, rxreq);
771 + if (!rxtodo && rxreq)
772 + ltq_spi_reg_write(hw, rxreq, LTQ_SPI_RXREQ);
775 +static inline void ltq_spi_complete(struct ltq_spi *hw)
777 + complete(&hw->done);
780 +irqreturn_t ltq_spi_tx_irq(int irq, void *data)
782 + struct ltq_spi *hw = data;
783 + unsigned long flags;
786 + spin_lock_irqsave(&hw->lock, flags);
788 + if (hw->tx_cnt < hw->len)
789 + ltq_spi_txfifo_write(hw);
791 + if (hw->tx_cnt == hw->len)
794 + spin_unlock_irqrestore(&hw->lock, flags);
797 + ltq_spi_complete(hw);
799 + return IRQ_HANDLED;
802 +irqreturn_t ltq_spi_rx_irq(int irq, void *data)
804 + struct ltq_spi *hw = data;
805 + unsigned long flags;
808 + spin_lock_irqsave(&hw->lock, flags);
810 + if (hw->rx_cnt < hw->len) {
811 + ltq_spi_rxfifo_read(hw);
813 + if (hw->tx && hw->tx_cnt < hw->len)
814 + ltq_spi_txfifo_write(hw);
817 + if (hw->rx_cnt == hw->len)
820 + ltq_spi_rxreq_set(hw);
822 + spin_unlock_irqrestore(&hw->lock, flags);
825 + ltq_spi_complete(hw);
827 + return IRQ_HANDLED;
830 +irqreturn_t ltq_spi_err_irq(int irq, void *data)
832 + struct ltq_spi *hw = data;
833 + unsigned long flags;
835 + spin_lock_irqsave(&hw->lock, flags);
837 + /* Disable all interrupts */
838 + ltq_spi_reg_clearbit(hw, LTQ_SPI_IRNEN_ALL, LTQ_SPI_IRNEN);
840 + /* Clear all error flags */
841 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
844 + ltq_spi_reg_setbit(hw, LTQ_SPI_RXFCON_RXFLU, LTQ_SPI_RXFCON);
845 + ltq_spi_reg_setbit(hw, LTQ_SPI_TXFCON_TXFLU, LTQ_SPI_TXFCON);
848 + spin_unlock_irqrestore(&hw->lock, flags);
850 + ltq_spi_complete(hw);
852 + return IRQ_HANDLED;
855 +static int ltq_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
857 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
860 + hw->tx = t->tx_buf;
861 + hw->rx = t->rx_buf;
866 + INIT_COMPLETION(hw->done);
868 + ltq_spi_xmit_set(hw, t);
870 + /* Enable error interrupts */
871 + ltq_spi_reg_setbit(hw, LTQ_SPI_IRNEN_E, LTQ_SPI_IRNEN);
874 + /* Initially fill TX FIFO with as much data as possible */
875 + ltq_spi_txfifo_write(hw);
876 + irq_flags |= LTQ_SPI_IRNEN_T;
878 + /* Always enable RX interrupt in Full Duplex mode */
880 + irq_flags |= LTQ_SPI_IRNEN_R;
881 + } else if (hw->rx) {
882 + /* Start RX clock */
883 + ltq_spi_rxreq_set(hw);
885 + /* Enable RX interrupt to receive data from RX FIFOs */
886 + irq_flags |= LTQ_SPI_IRNEN_R;
889 + /* Enable TX or RX interrupts */
890 + ltq_spi_reg_setbit(hw, irq_flags, LTQ_SPI_IRNEN);
891 + wait_for_completion_interruptible(&hw->done);
893 + /* Disable all interrupts */
894 + ltq_spi_reg_clearbit(hw, LTQ_SPI_IRNEN_ALL, LTQ_SPI_IRNEN);
897 + * Return length of current transfer for bitbang utility code if
898 + * no errors occured during transmission.
901 + hw->status = hw->len;
906 +static const struct ltq_spi_irq_map ltq_spi_irqs[] = {
907 + { "spi_tx", ltq_spi_tx_irq },
908 + { "spi_rx", ltq_spi_rx_irq },
909 + { "spi_err", ltq_spi_err_irq },
912 +static int __init ltq_spi_probe(struct platform_device *pdev)
914 + struct spi_master *master;
915 + struct resource *r;
916 + struct ltq_spi *hw;
917 + struct ltq_spi_platform_data *pdata = pdev->dev.platform_data;
921 + master = spi_alloc_master(&pdev->dev, sizeof(struct ltq_spi));
923 + dev_err(&pdev->dev, "spi_alloc_master\n");
928 + hw = spi_master_get_devdata(master);
930 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
932 + dev_err(&pdev->dev, "platform_get_resource\n");
937 + r = devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
940 + dev_err(&pdev->dev, "devm_request_mem_region\n");
945 + hw->base = devm_ioremap_nocache(&pdev->dev, r->start, resource_size(r));
947 + dev_err(&pdev->dev, "devm_ioremap_nocache\n");
952 + hw->clk = clk_get(&pdev->dev, "fpi");
953 + if (IS_ERR(hw->clk)) {
954 + dev_err(&pdev->dev, "clk_get\n");
955 + ret = PTR_ERR(hw->clk);
959 + memset(hw->irq, 0, sizeof(hw->irq));
960 + for (i = 0; i < ARRAY_SIZE(ltq_spi_irqs); i++) {
961 + ret = platform_get_irq_byname(pdev, ltq_spi_irqs[i].name);
963 + dev_err(&pdev->dev, "platform_get_irq_byname\n");
968 + ret = request_irq(hw->irq[i], ltq_spi_irqs[i].handler,
969 + 0, ltq_spi_irqs[i].name, hw);
971 + dev_err(&pdev->dev, "request_irq\n");
976 + hw->bitbang.master = spi_master_get(master);
977 + hw->bitbang.chipselect = ltq_spi_chipselect;
978 + hw->bitbang.setup_transfer = ltq_spi_setup_transfer;
979 + hw->bitbang.txrx_bufs = ltq_spi_txrx_bufs;
981 + master->bus_num = pdev->id;
982 + master->num_chipselect = pdata->num_chipselect;
983 + master->setup = ltq_spi_setup;
984 + master->cleanup = ltq_spi_cleanup;
986 + hw->dev = &pdev->dev;
987 + init_completion(&hw->done);
988 + spin_lock_init(&hw->lock);
990 + /* Set GPIO alternate functions to SPI */
991 + ltq_gpio_request(LTQ_SPI_GPIO_DI, 1, 0, 0, "spi-di");
992 + ltq_gpio_request(LTQ_SPI_GPIO_DO, 1, 0, 1, "spi-do");
993 + ltq_gpio_request(LTQ_SPI_GPIO_CLK, 1, 0, 1, "spi-clk");
995 + ltq_spi_hw_enable(hw);
997 + /* Read module capabilities */
998 + id = ltq_spi_reg_read(hw, LTQ_SPI_ID);
999 + hw->txfs = (id >> LTQ_SPI_ID_TXFS_SHIFT) & LTQ_SPI_ID_TXFS_MASK;
1000 + hw->rxfs = (id >> LTQ_SPI_ID_TXFS_SHIFT) & LTQ_SPI_ID_TXFS_MASK;
1001 + hw->dma_support = (id & LTQ_SPI_ID_CFG) ? 1 : 0;
1003 + ltq_spi_config_mode_set(hw);
1005 + /* Enable error checking, disable TX/RX, set idle value high */
1006 + data = LTQ_SPI_CON_RUEN | LTQ_SPI_CON_AEN |
1007 + LTQ_SPI_CON_TEN | LTQ_SPI_CON_REN |
1008 + LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF | LTQ_SPI_CON_IDLE;
1009 + ltq_spi_reg_write(hw, data, LTQ_SPI_CON);
1011 + /* Enable master mode and clear error flags */
1012 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_SETMS |
1013 + LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
1015 + /* Reset GPIO/CS registers */
1016 + ltq_spi_reg_write(hw, 0x0, LTQ_SPI_GPOCON);
1017 + ltq_spi_reg_write(hw, 0xFF00, LTQ_SPI_FGPO);
1019 + /* Enable and flush FIFOs */
1020 + ltq_spi_reset_fifos(hw);
1022 + ret = spi_bitbang_start(&hw->bitbang);
1024 + dev_err(&pdev->dev, "spi_bitbang_start\n");
1028 + platform_set_drvdata(pdev, hw);
1030 + pr_info("Lantiq SoC SPI controller rev %u (TXFS %u, RXFS %u, DMA %u)\n",
1031 + id & LTQ_SPI_ID_REV_MASK, hw->txfs, hw->rxfs, hw->dma_support);
1036 + ltq_spi_hw_disable(hw);
1041 + for (; i > 0; i--)
1042 + free_irq(hw->irq[i], hw);
1045 + spi_master_put(master);
1051 +static int __exit ltq_spi_remove(struct platform_device *pdev)
1053 + struct ltq_spi *hw = platform_get_drvdata(pdev);
1056 + ret = spi_bitbang_stop(&hw->bitbang);
1060 + platform_set_drvdata(pdev, NULL);
1062 + ltq_spi_config_mode_set(hw);
1063 + ltq_spi_hw_disable(hw);
1065 + for (i = 0; i < ARRAY_SIZE(hw->irq); i++)
1066 + if (0 < hw->irq[i])
1067 + free_irq(hw->irq[i], hw);
1069 + gpio_free(LTQ_SPI_GPIO_DI);
1070 + gpio_free(LTQ_SPI_GPIO_DO);
1071 + gpio_free(LTQ_SPI_GPIO_CLK);
1074 + spi_master_put(hw->bitbang.master);
1079 +static struct platform_driver ltq_spi_driver = {
1081 + .name = "ltq-spi",
1082 + .owner = THIS_MODULE,
1084 + .remove = __exit_p(ltq_spi_remove),
1087 +static int __init ltq_spi_init(void)
1089 + return platform_driver_probe(<q_spi_driver, ltq_spi_probe);
1091 +module_init(ltq_spi_init);
1093 +static void __exit ltq_spi_exit(void)
1095 + platform_driver_unregister(<q_spi_driver);
1097 +module_exit(ltq_spi_exit);
1099 +MODULE_DESCRIPTION("Lantiq SoC SPI controller driver");
1100 +MODULE_AUTHOR("Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>");
1101 +MODULE_LICENSE("GPL");
1102 +MODULE_ALIAS("platform:ltq-spi");