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_LANTIQ_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_OMAP_UWIRE) += omap_uwire.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>
68 +#include <xway_irq.h>
69 +#include <lantiq_platform.h>
71 +#define LTQ_SPI_CLC 0x00 /* Clock control */
72 +#define LTQ_SPI_PISEL 0x04 /* Port input select */
73 +#define LTQ_SPI_ID 0x08 /* Identification */
74 +#define LTQ_SPI_CON 0x10 /* Control */
75 +#define LTQ_SPI_STAT 0x14 /* Status */
76 +#define LTQ_SPI_WHBSTATE 0x18 /* Write HW modified state */
77 +#define LTQ_SPI_TB 0x20 /* Transmit buffer */
78 +#define LTQ_SPI_RB 0x24 /* Receive buffer */
79 +#define LTQ_SPI_RXFCON 0x30 /* Receive FIFO control */
80 +#define LTQ_SPI_TXFCON 0x34 /* Transmit FIFO control */
81 +#define LTQ_SPI_FSTAT 0x38 /* FIFO status */
82 +#define LTQ_SPI_BRT 0x40 /* Baudrate timer */
83 +#define LTQ_SPI_BRSTAT 0x44 /* Baudrate timer status */
84 +#define LTQ_SPI_SFCON 0x60 /* Serial frame control */
85 +#define LTQ_SPI_SFSTAT 0x64 /* Serial frame status */
86 +#define LTQ_SPI_GPOCON 0x70 /* General purpose output control */
87 +#define LTQ_SPI_GPOSTAT 0x74 /* General purpose output status */
88 +#define LTQ_SPI_FGPO 0x78 /* Forced general purpose output */
89 +#define LTQ_SPI_RXREQ 0x80 /* Receive request */
90 +#define LTQ_SPI_RXCNT 0x84 /* Receive count */
91 +#define LTQ_SPI_DMACON 0xEC /* DMA control */
92 +#define LTQ_SPI_IRNEN 0xF4 /* Interrupt node enable */
93 +#define LTQ_SPI_IRNICR 0xF8 /* Interrupt node interrupt capture */
94 +#define LTQ_SPI_IRNCR 0xFC /* Interrupt node control */
96 +#define LTQ_SPI_CLC_SMC_SHIFT 16 /* Clock divider for sleep mode */
97 +#define LTQ_SPI_CLC_SMC_MASK 0xFF
98 +#define LTQ_SPI_CLC_RMC_SHIFT 8 /* Clock divider for normal run mode */
99 +#define LTQ_SPI_CLC_RMC_MASK 0xFF
100 +#define LTQ_SPI_CLC_DISS BIT(1) /* Disable status bit */
101 +#define LTQ_SPI_CLC_DISR BIT(0) /* Disable request bit */
103 +#define LTQ_SPI_ID_TXFS_SHIFT 24 /* Implemented TX FIFO size */
104 +#define LTQ_SPI_ID_TXFS_MASK 0x3F
105 +#define LTQ_SPI_ID_RXFS_SHIFT 16 /* Implemented RX FIFO size */
106 +#define LTQ_SPI_ID_RXFS_MASK 0x3F
107 +#define LTQ_SPI_ID_REV_MASK 0x1F /* Hardware revision number */
108 +#define LTQ_SPI_ID_CFG BIT(5) /* DMA interface support */
110 +#define LTQ_SPI_CON_BM_SHIFT 16 /* Data width selection */
111 +#define LTQ_SPI_CON_BM_MASK 0x1F
112 +#define LTQ_SPI_CON_EM BIT(24) /* Echo mode */
113 +#define LTQ_SPI_CON_IDLE BIT(23) /* Idle bit value */
114 +#define LTQ_SPI_CON_ENBV BIT(22) /* Enable byte valid control */
115 +#define LTQ_SPI_CON_RUEN BIT(12) /* Receive underflow error enable */
116 +#define LTQ_SPI_CON_TUEN BIT(11) /* Transmit underflow error enable */
117 +#define LTQ_SPI_CON_AEN BIT(10) /* Abort error enable */
118 +#define LTQ_SPI_CON_REN BIT(9) /* Receive overflow error enable */
119 +#define LTQ_SPI_CON_TEN BIT(8) /* Transmit overflow error enable */
120 +#define LTQ_SPI_CON_LB BIT(7) /* Loopback control */
121 +#define LTQ_SPI_CON_PO BIT(6) /* Clock polarity control */
122 +#define LTQ_SPI_CON_PH BIT(5) /* Clock phase control */
123 +#define LTQ_SPI_CON_HB BIT(4) /* Heading control */
124 +#define LTQ_SPI_CON_RXOFF BIT(1) /* Switch receiver off */
125 +#define LTQ_SPI_CON_TXOFF BIT(0) /* Switch transmitter off */
127 +#define LTQ_SPI_STAT_RXBV_MASK 0x7
128 +#define LTQ_SPI_STAT_RXBV_SHIFT 28
129 +#define LTQ_SPI_STAT_BSY BIT(13) /* Busy flag */
130 +#define LTQ_SPI_STAT_RUE BIT(12) /* Receive underflow error flag */
131 +#define LTQ_SPI_STAT_TUE BIT(11) /* Transmit underflow error flag */
132 +#define LTQ_SPI_STAT_AE BIT(10) /* Abort error flag */
133 +#define LTQ_SPI_STAT_RE BIT(9) /* Receive error flag */
134 +#define LTQ_SPI_STAT_TE BIT(8) /* Transmit error flag */
135 +#define LTQ_SPI_STAT_MS BIT(1) /* Master/slave select bit */
136 +#define LTQ_SPI_STAT_EN BIT(0) /* Enable bit */
138 +#define LTQ_SPI_WHBSTATE_SETTUE BIT(15) /* Set transmit underflow error flag */
139 +#define LTQ_SPI_WHBSTATE_SETAE BIT(14) /* Set abort error flag */
140 +#define LTQ_SPI_WHBSTATE_SETRE BIT(13) /* Set receive error flag */
141 +#define LTQ_SPI_WHBSTATE_SETTE BIT(12) /* Set transmit error flag */
142 +#define LTQ_SPI_WHBSTATE_CLRTUE BIT(11) /* Clear transmit underflow error flag */
143 +#define LTQ_SPI_WHBSTATE_CLRAE BIT(10) /* Clear abort error flag */
144 +#define LTQ_SPI_WHBSTATE_CLRRE BIT(9) /* Clear receive error flag */
145 +#define LTQ_SPI_WHBSTATE_CLRTE BIT(8) /* Clear transmit error flag */
146 +#define LTQ_SPI_WHBSTATE_SETME BIT(7) /* Set mode error flag */
147 +#define LTQ_SPI_WHBSTATE_CLRME BIT(6) /* Clear mode error flag */
148 +#define LTQ_SPI_WHBSTATE_SETRUE BIT(5) /* Set receive underflow error flag */
149 +#define LTQ_SPI_WHBSTATE_CLRRUE BIT(4) /* Clear receive underflow error flag */
150 +#define LTQ_SPI_WHBSTATE_SETMS BIT(3) /* Set master select bit */
151 +#define LTQ_SPI_WHBSTATE_CLRMS BIT(2) /* Clear master select bit */
152 +#define LTQ_SPI_WHBSTATE_SETEN BIT(1) /* Set enable bit (operational mode) */
153 +#define LTQ_SPI_WHBSTATE_CLREN BIT(0) /* Clear enable bit (config mode */
154 +#define LTQ_SPI_WHBSTATE_CLR_ERRORS 0x0F50
156 +#define LTQ_SPI_RXFCON_RXFITL_SHIFT 8 /* FIFO interrupt trigger level */
157 +#define LTQ_SPI_RXFCON_RXFITL_MASK 0x3F
158 +#define LTQ_SPI_RXFCON_RXFLU BIT(1) /* FIFO flush */
159 +#define LTQ_SPI_RXFCON_RXFEN BIT(0) /* FIFO enable */
161 +#define LTQ_SPI_TXFCON_TXFITL_SHIFT 8 /* FIFO interrupt trigger level */
162 +#define LTQ_SPI_TXFCON_TXFITL_MASK 0x3F
163 +#define LTQ_SPI_TXFCON_TXFLU BIT(1) /* FIFO flush */
164 +#define LTQ_SPI_TXFCON_TXFEN BIT(0) /* FIFO enable */
166 +#define LTQ_SPI_FSTAT_RXFFL_MASK 0x3f
167 +#define LTQ_SPI_FSTAT_RXFFL_SHIFT 0
168 +#define LTQ_SPI_FSTAT_TXFFL_MASK 0x3f
169 +#define LTQ_SPI_FSTAT_TXFFL_SHIFT 8
171 +#define LTQ_SPI_GPOCON_ISCSBN_SHIFT 8
172 +#define LTQ_SPI_GPOCON_INVOUTN_SHIFT 0
174 +#define LTQ_SPI_FGPO_SETOUTN_SHIFT 8
175 +#define LTQ_SPI_FGPO_CLROUTN_SHIFT 0
177 +#define LTQ_SPI_RXREQ_RXCNT_MASK 0xFFFF /* Receive count value */
178 +#define LTQ_SPI_RXCNT_TODO_MASK 0xFFFF /* Recevie to-do value */
180 +#define LTQ_SPI_IRNEN_F BIT(3) /* Frame end interrupt request */
181 +#define LTQ_SPI_IRNEN_E BIT(2) /* Error end interrupt request */
182 +#define LTQ_SPI_IRNEN_T BIT(1) /* Transmit end interrupt request */
183 +#define LTQ_SPI_IRNEN_R BIT(0) /* Receive end interrupt request */
184 +#define LTQ_SPI_IRNEN_ALL 0xF
186 +/* Hard-wired GPIOs used by SPI controller */
187 +#define LTQ_SPI_GPIO_DI 16
188 +#define LTQ_SPI_GPIO_DO 17
189 +#define LTQ_SPI_GPIO_CLK 18
192 + struct spi_bitbang bitbang;
193 + struct completion done;
196 + struct device *dev;
197 + void __iomem *base;
208 + struct spi_transfer *curr_transfer;
210 + u32 (*get_tx) (struct ltq_spi *);
214 + unsigned dma_support:1;
215 + unsigned cfg_mode:1;
219 +struct ltq_spi_controller_state {
220 + void (*cs_activate) (struct spi_device *);
221 + void (*cs_deactivate) (struct spi_device *);
224 +struct ltq_spi_irq_map {
226 + irq_handler_t handler;
229 +struct ltq_spi_cs_gpio_map {
235 +static inline struct ltq_spi *ltq_spi_to_hw(struct spi_device *spi)
237 + return spi_master_get_devdata(spi->master);
240 +static inline u32 ltq_spi_reg_read(struct ltq_spi *hw, u32 reg)
242 + return ioread32be(hw->base + reg);
245 +static inline void ltq_spi_reg_write(struct ltq_spi *hw, u32 val, u32 reg)
247 + iowrite32be(val, hw->base + reg);
250 +static inline void ltq_spi_reg_setbit(struct ltq_spi *hw, u32 bits, u32 reg)
254 + val = ltq_spi_reg_read(hw, reg);
256 + ltq_spi_reg_write(hw, val, reg);
259 +static inline void ltq_spi_reg_clearbit(struct ltq_spi *hw, u32 bits, u32 reg)
263 + val = ltq_spi_reg_read(hw, reg);
265 + ltq_spi_reg_write(hw, val, reg);
268 +static void ltq_spi_hw_enable(struct ltq_spi *hw)
272 + /* Power-up mdule */
273 + ltq_pmu_enable(PMU_SPI);
276 + * Set clock divider for run mode to 1 to
277 + * run at same frequency as FPI bus
279 + clc = (1 << LTQ_SPI_CLC_RMC_SHIFT);
280 + ltq_spi_reg_write(hw, clc, LTQ_SPI_CLC);
283 +static void ltq_spi_hw_disable(struct ltq_spi *hw)
285 + /* Set clock divider to 0 and set module disable bit */
286 + ltq_spi_reg_write(hw, LTQ_SPI_CLC_DISS, LTQ_SPI_CLC);
288 + /* Power-down mdule */
289 + ltq_pmu_disable(PMU_SPI);
292 +static void ltq_spi_reset_fifos(struct ltq_spi *hw)
297 + * Enable and flush FIFOs. Set interrupt trigger level to
298 + * half of FIFO count implemented in hardware.
300 + if (hw->txfs > 1) {
301 + val = hw->txfs << (LTQ_SPI_TXFCON_TXFITL_SHIFT - 1);
302 + val |= LTQ_SPI_TXFCON_TXFEN | LTQ_SPI_TXFCON_TXFLU;
303 + ltq_spi_reg_write(hw, val, LTQ_SPI_TXFCON);
306 + if (hw->rxfs > 1) {
307 + val = hw->rxfs << (LTQ_SPI_RXFCON_RXFITL_SHIFT - 1);
308 + val |= LTQ_SPI_RXFCON_RXFEN | LTQ_SPI_RXFCON_RXFLU;
309 + ltq_spi_reg_write(hw, val, LTQ_SPI_RXFCON);
313 +static inline int ltq_spi_wait_ready(struct ltq_spi *hw)
316 + unsigned long timeout;
318 + timeout = jiffies + msecs_to_jiffies(200);
321 + stat = ltq_spi_reg_read(hw, LTQ_SPI_STAT);
322 + if (!(stat & LTQ_SPI_STAT_BSY))
326 + } while (!time_after_eq(jiffies, timeout));
328 + dev_err(hw->dev, "SPI wait ready timed out\n");
333 +static void ltq_spi_config_mode_set(struct ltq_spi *hw)
339 + * Putting the SPI module in config mode is only safe if no
340 + * transfer is in progress as indicated by busy flag STATE.BSY.
342 + if (ltq_spi_wait_ready(hw)) {
343 + ltq_spi_reset_fifos(hw);
344 + hw->status = -ETIMEDOUT;
346 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_CLREN, LTQ_SPI_WHBSTATE);
351 +static void ltq_spi_run_mode_set(struct ltq_spi *hw)
356 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_SETEN, LTQ_SPI_WHBSTATE);
361 +static u32 ltq_spi_tx_word_u8(struct ltq_spi *hw)
363 + const u8 *tx = hw->tx;
372 +static u32 ltq_spi_tx_word_u16(struct ltq_spi *hw)
374 + const u16 *tx = (u16 *) hw->tx;
383 +static u32 ltq_spi_tx_word_u32(struct ltq_spi *hw)
385 + const u32 *tx = (u32 *) hw->tx;
394 +static void ltq_spi_bits_per_word_set(struct spi_device *spi)
396 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
398 + u8 bits_per_word = spi->bits_per_word;
401 + * Use either default value of SPI device or value
402 + * from current transfer.
404 + if (hw->curr_transfer && hw->curr_transfer->bits_per_word)
405 + bits_per_word = hw->curr_transfer->bits_per_word;
407 + if (bits_per_word <= 8)
408 + hw->get_tx = ltq_spi_tx_word_u8;
409 + else if (bits_per_word <= 16)
410 + hw->get_tx = ltq_spi_tx_word_u16;
411 + else if (bits_per_word <= 32)
412 + hw->get_tx = ltq_spi_tx_word_u32;
414 + /* CON.BM value = bits_per_word - 1 */
415 + bm = (bits_per_word - 1) << LTQ_SPI_CON_BM_SHIFT;
417 + ltq_spi_reg_clearbit(hw, LTQ_SPI_CON_BM_MASK <<
418 + LTQ_SPI_CON_BM_SHIFT, LTQ_SPI_CON);
419 + ltq_spi_reg_setbit(hw, bm, LTQ_SPI_CON);
422 +static void ltq_spi_speed_set(struct spi_device *spi)
424 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
425 + u32 br, max_speed_hz, spi_clk;
426 + u32 speed_hz = spi->max_speed_hz;
429 + * Use either default value of SPI device or value
430 + * from current transfer.
432 + if (hw->curr_transfer && hw->curr_transfer->speed_hz)
433 + speed_hz = hw->curr_transfer->speed_hz;
436 + * SPI module clock is derived from FPI bus clock dependent on
437 + * divider value in CLC.RMS which is always set to 1.
439 + spi_clk = clk_get_rate(hw->clk);
442 + * Maximum SPI clock frequency in master mode is half of
443 + * SPI module clock frequency. Maximum reload value of
444 + * baudrate generator BR is 2^16.
446 + max_speed_hz = spi_clk / 2;
447 + if (speed_hz >= max_speed_hz)
450 + br = (max_speed_hz / speed_hz) - 1;
455 + ltq_spi_reg_write(hw, br, LTQ_SPI_BRT);
458 +static void ltq_spi_clockmode_set(struct spi_device *spi)
460 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
463 + con = ltq_spi_reg_read(hw, LTQ_SPI_CON);
466 + * SPI mode mapping in CON register:
467 + * Mode CPOL CPHA CON.PO CON.PH
473 + if (spi->mode & SPI_CPHA)
474 + con &= ~LTQ_SPI_CON_PH;
476 + con |= LTQ_SPI_CON_PH;
478 + if (spi->mode & SPI_CPOL)
479 + con |= LTQ_SPI_CON_PO;
481 + con &= ~LTQ_SPI_CON_PO;
483 + /* Set heading control */
484 + if (spi->mode & SPI_LSB_FIRST)
485 + con &= ~LTQ_SPI_CON_HB;
487 + con |= LTQ_SPI_CON_HB;
489 + ltq_spi_reg_write(hw, con, LTQ_SPI_CON);
492 +static void ltq_spi_xmit_set(struct ltq_spi *hw, struct spi_transfer *t)
496 + con = ltq_spi_reg_read(hw, LTQ_SPI_CON);
499 + if (t->tx_buf && t->rx_buf) {
500 + con &= ~(LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF);
501 + } else if (t->rx_buf) {
502 + con &= ~LTQ_SPI_CON_RXOFF;
503 + con |= LTQ_SPI_CON_TXOFF;
504 + } else if (t->tx_buf) {
505 + con &= ~LTQ_SPI_CON_TXOFF;
506 + con |= LTQ_SPI_CON_RXOFF;
509 + con |= (LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF);
511 + ltq_spi_reg_write(hw, con, LTQ_SPI_CON);
514 +static void ltq_spi_gpio_cs_activate(struct spi_device *spi)
516 + struct ltq_spi_controller_data *cdata = spi->controller_data;
517 + int val = spi->mode & SPI_CS_HIGH ? 1 : 0;
519 + gpio_set_value(cdata->gpio, val);
522 +static void ltq_spi_gpio_cs_deactivate(struct spi_device *spi)
524 + struct ltq_spi_controller_data *cdata = spi->controller_data;
525 + int val = spi->mode & SPI_CS_HIGH ? 0 : 1;
527 + gpio_set_value(cdata->gpio, val);
530 +static void ltq_spi_internal_cs_activate(struct spi_device *spi)
532 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
535 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_CLROUTN_SHIFT));
536 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
539 +static void ltq_spi_internal_cs_deactivate(struct spi_device *spi)
541 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
544 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_SETOUTN_SHIFT));
545 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
548 +static void ltq_spi_chipselect(struct spi_device *spi, int cs)
550 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
551 + struct ltq_spi_controller_state *cstate = spi->controller_state;
554 + case BITBANG_CS_ACTIVE:
555 + ltq_spi_bits_per_word_set(spi);
556 + ltq_spi_speed_set(spi);
557 + ltq_spi_clockmode_set(spi);
558 + ltq_spi_run_mode_set(hw);
560 + cstate->cs_activate(spi);
563 + case BITBANG_CS_INACTIVE:
564 + cstate->cs_deactivate(spi);
566 + ltq_spi_config_mode_set(hw);
572 +static int ltq_spi_setup_transfer(struct spi_device *spi,
573 + struct spi_transfer *t)
575 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
576 + u8 bits_per_word = spi->bits_per_word;
578 + hw->curr_transfer = t;
580 + if (t && t->bits_per_word)
581 + bits_per_word = t->bits_per_word;
583 + if (bits_per_word > 32)
586 + ltq_spi_config_mode_set(hw);
591 +static const struct ltq_spi_cs_gpio_map ltq_spi_cs[] = {
600 +static int ltq_spi_setup(struct spi_device *spi)
602 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
603 + struct ltq_spi_controller_data *cdata = spi->controller_data;
604 + struct ltq_spi_controller_state *cstate;
608 + /* Set default word length to 8 if not set */
609 + if (!spi->bits_per_word)
610 + spi->bits_per_word = 8;
612 + if (spi->bits_per_word > 32)
615 + if (!spi->controller_state) {
616 + cstate = kzalloc(sizeof(struct ltq_spi_controller_state),
621 + spi->controller_state = cstate;
626 + * Up to six GPIOs can be connected to the SPI module
627 + * via GPIO alternate function to control the chip select lines.
628 + * For more flexibility in board layout this driver can also control
629 + * the CS lines via GPIO API. If GPIOs should be used, board setup code
630 + * have to register the SPI device with struct ltq_spi_controller_data
633 + if (cdata && cdata->gpio) {
634 + ret = gpio_request(cdata->gpio, "spi-cs");
638 + ret = spi->mode & SPI_CS_HIGH ? 0 : 1;
639 + gpio_direction_output(cdata->gpio, ret);
641 + cstate->cs_activate = ltq_spi_gpio_cs_activate;
642 + cstate->cs_deactivate = ltq_spi_gpio_cs_deactivate;
644 + ret = ltq_gpio_request(ltq_spi_cs[spi->chip_select].gpio,
645 + ltq_spi_cs[spi->chip_select].altsel0,
646 + ltq_spi_cs[spi->chip_select].altsel1,
651 + gpocon = (1 << (spi->chip_select +
652 + LTQ_SPI_GPOCON_ISCSBN_SHIFT));
654 + if (spi->mode & SPI_CS_HIGH)
655 + gpocon |= (1 << spi->chip_select);
657 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_SETOUTN_SHIFT));
659 + ltq_spi_reg_setbit(hw, gpocon, LTQ_SPI_GPOCON);
660 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
662 + cstate->cs_activate = ltq_spi_internal_cs_activate;
663 + cstate->cs_deactivate = ltq_spi_internal_cs_deactivate;
669 +static void ltq_spi_cleanup(struct spi_device *spi)
671 + struct ltq_spi_controller_data *cdata = spi->controller_data;
672 + struct ltq_spi_controller_state *cstate = spi->controller_state;
675 + if (cdata && cdata->gpio)
676 + gpio = cdata->gpio;
678 + gpio = ltq_spi_cs[spi->chip_select].gpio;
684 +static void ltq_spi_txfifo_write(struct ltq_spi *hw)
689 + /* Determine how much FIFOs are free for TX data */
690 + fstat = ltq_spi_reg_read(hw, LTQ_SPI_FSTAT);
691 + fifo_space = hw->txfs - ((fstat >> LTQ_SPI_FSTAT_TXFFL_SHIFT) &
692 + LTQ_SPI_FSTAT_TXFFL_MASK);
697 + while (hw->tx_cnt < hw->len && fifo_space) {
698 + data = hw->get_tx(hw);
699 + ltq_spi_reg_write(hw, data, LTQ_SPI_TB);
704 +static void ltq_spi_rxfifo_read(struct ltq_spi *hw)
706 + u32 fstat, data, *rx32;
708 + u8 rxbv, shift, *rx8;
710 + /* Determine how much FIFOs are filled with RX data */
711 + fstat = ltq_spi_reg_read(hw, LTQ_SPI_FSTAT);
712 + fifo_fill = ((fstat >> LTQ_SPI_FSTAT_RXFFL_SHIFT)
713 + & LTQ_SPI_FSTAT_RXFFL_MASK);
719 + * The 32 bit FIFO is always used completely independent from the
720 + * bits_per_word value. Thus four bytes have to be read at once
723 + rx32 = (u32 *) hw->rx;
724 + while (hw->len - hw->rx_cnt >= 4 && fifo_fill) {
725 + *rx32++ = ltq_spi_reg_read(hw, LTQ_SPI_RB);
732 + * If there are remaining bytes, read byte count from STAT.RXBV
733 + * register and read the data byte-wise.
735 + while (fifo_fill && hw->rx_cnt < hw->len) {
736 + rxbv = (ltq_spi_reg_read(hw, LTQ_SPI_STAT) >>
737 + LTQ_SPI_STAT_RXBV_SHIFT) & LTQ_SPI_STAT_RXBV_MASK;
738 + data = ltq_spi_reg_read(hw, LTQ_SPI_RB);
740 + shift = (rxbv - 1) * 8;
744 + *rx8++ = (data >> shift) & 0xFF;
755 +static void ltq_spi_rxreq_set(struct ltq_spi *hw)
757 + u32 rxreq, rxreq_max, rxtodo;
759 + rxtodo = ltq_spi_reg_read(hw, LTQ_SPI_RXCNT) & LTQ_SPI_RXCNT_TODO_MASK;
762 + * In RX-only mode the serial clock is activated only after writing
763 + * the expected amount of RX bytes into RXREQ register.
764 + * To avoid receive overflows at high clocks it is better to request
765 + * only the amount of bytes that fits into all FIFOs. This value
766 + * depends on the FIFO size implemented in hardware.
768 + rxreq = hw->len - hw->rx_cnt;
769 + rxreq_max = hw->rxfs << 2;
770 + rxreq = min(rxreq_max, rxreq);
772 + if (!rxtodo && rxreq)
773 + ltq_spi_reg_write(hw, rxreq, LTQ_SPI_RXREQ);
776 +static inline void ltq_spi_complete(struct ltq_spi *hw)
778 + complete(&hw->done);
781 +irqreturn_t ltq_spi_tx_irq(int irq, void *data)
783 + struct ltq_spi *hw = data;
784 + unsigned long flags;
787 + spin_lock_irqsave(&hw->lock, flags);
789 + if (hw->tx_cnt < hw->len)
790 + ltq_spi_txfifo_write(hw);
792 + if (hw->tx_cnt == hw->len)
795 + spin_unlock_irqrestore(&hw->lock, flags);
798 + ltq_spi_complete(hw);
800 + return IRQ_HANDLED;
803 +irqreturn_t ltq_spi_rx_irq(int irq, void *data)
805 + struct ltq_spi *hw = data;
806 + unsigned long flags;
809 + spin_lock_irqsave(&hw->lock, flags);
811 + if (hw->rx_cnt < hw->len) {
812 + ltq_spi_rxfifo_read(hw);
814 + if (hw->tx && hw->tx_cnt < hw->len)
815 + ltq_spi_txfifo_write(hw);
818 + if (hw->rx_cnt == hw->len)
821 + ltq_spi_rxreq_set(hw);
823 + spin_unlock_irqrestore(&hw->lock, flags);
826 + ltq_spi_complete(hw);
828 + return IRQ_HANDLED;
831 +irqreturn_t ltq_spi_err_irq(int irq, void *data)
833 + struct ltq_spi *hw = data;
834 + unsigned long flags;
836 + spin_lock_irqsave(&hw->lock, flags);
838 + /* Disable all interrupts */
839 + ltq_spi_reg_clearbit(hw, LTQ_SPI_IRNEN_ALL, LTQ_SPI_IRNEN);
841 + /* Clear all error flags */
842 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
845 + ltq_spi_reg_setbit(hw, LTQ_SPI_RXFCON_RXFLU, LTQ_SPI_RXFCON);
846 + ltq_spi_reg_setbit(hw, LTQ_SPI_TXFCON_TXFLU, LTQ_SPI_TXFCON);
849 + spin_unlock_irqrestore(&hw->lock, flags);
851 + ltq_spi_complete(hw);
853 + return IRQ_HANDLED;
856 +static int ltq_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
858 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
861 + hw->tx = t->tx_buf;
862 + hw->rx = t->rx_buf;
867 + INIT_COMPLETION(hw->done);
869 + ltq_spi_xmit_set(hw, t);
871 + /* Enable error interrupts */
872 + ltq_spi_reg_setbit(hw, LTQ_SPI_IRNEN_E, LTQ_SPI_IRNEN);
875 + /* Initially fill TX FIFO with as much data as possible */
876 + ltq_spi_txfifo_write(hw);
877 + irq_flags |= LTQ_SPI_IRNEN_T;
879 + /* Always enable RX interrupt in Full Duplex mode */
881 + irq_flags |= LTQ_SPI_IRNEN_R;
882 + } else if (hw->rx) {
883 + /* Start RX clock */
884 + ltq_spi_rxreq_set(hw);
886 + /* Enable RX interrupt to receive data from RX FIFOs */
887 + irq_flags |= LTQ_SPI_IRNEN_R;
890 + /* Enable TX or RX interrupts */
891 + ltq_spi_reg_setbit(hw, irq_flags, LTQ_SPI_IRNEN);
892 + wait_for_completion_interruptible(&hw->done);
894 + /* Disable all interrupts */
895 + ltq_spi_reg_clearbit(hw, LTQ_SPI_IRNEN_ALL, LTQ_SPI_IRNEN);
898 + * Return length of current transfer for bitbang utility code if
899 + * no errors occured during transmission.
902 + hw->status = hw->len;
907 +static const struct ltq_spi_irq_map ltq_spi_irqs[] = {
908 + { "spi_tx", ltq_spi_tx_irq },
909 + { "spi_rx", ltq_spi_rx_irq },
910 + { "spi_err", ltq_spi_err_irq },
913 +static int __init ltq_spi_probe(struct platform_device *pdev)
915 + struct spi_master *master;
916 + struct resource *r;
917 + struct ltq_spi *hw;
918 + struct ltq_spi_platform_data *pdata = pdev->dev.platform_data;
922 + master = spi_alloc_master(&pdev->dev, sizeof(struct ltq_spi));
924 + dev_err(&pdev->dev, "spi_alloc_master\n");
929 + hw = spi_master_get_devdata(master);
931 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
933 + dev_err(&pdev->dev, "platform_get_resource\n");
938 + r = devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
941 + dev_err(&pdev->dev, "devm_request_mem_region\n");
946 + hw->base = devm_ioremap_nocache(&pdev->dev, r->start, resource_size(r));
948 + dev_err(&pdev->dev, "devm_ioremap_nocache\n");
953 + hw->clk = clk_get(&pdev->dev, "fpi");
954 + if (IS_ERR(hw->clk)) {
955 + dev_err(&pdev->dev, "clk_get\n");
956 + ret = PTR_ERR(hw->clk);
960 + memset(hw->irq, 0, sizeof(hw->irq));
961 + for (i = 0; i < ARRAY_SIZE(ltq_spi_irqs); i++) {
962 + ret = platform_get_irq_byname(pdev, ltq_spi_irqs[i].name);
964 + dev_err(&pdev->dev, "platform_get_irq_byname\n");
969 + ret = request_irq(hw->irq[i], ltq_spi_irqs[i].handler,
970 + 0, ltq_spi_irqs[i].name, hw);
972 + dev_err(&pdev->dev, "request_irq\n");
977 + hw->bitbang.master = spi_master_get(master);
978 + hw->bitbang.chipselect = ltq_spi_chipselect;
979 + hw->bitbang.setup_transfer = ltq_spi_setup_transfer;
980 + hw->bitbang.txrx_bufs = ltq_spi_txrx_bufs;
982 + master->bus_num = pdev->id;
983 + master->num_chipselect = pdata->num_chipselect;
984 + master->setup = ltq_spi_setup;
985 + master->cleanup = ltq_spi_cleanup;
987 + hw->dev = &pdev->dev;
988 + init_completion(&hw->done);
989 + spin_lock_init(&hw->lock);
991 + /* Set GPIO alternate functions to SPI */
992 + ltq_gpio_request(LTQ_SPI_GPIO_DI, 1, 0, 0, "spi-di");
993 + ltq_gpio_request(LTQ_SPI_GPIO_DO, 1, 0, 1, "spi-do");
994 + ltq_gpio_request(LTQ_SPI_GPIO_CLK, 1, 0, 1, "spi-clk");
996 + ltq_spi_hw_enable(hw);
998 + /* Read module capabilities */
999 + id = ltq_spi_reg_read(hw, LTQ_SPI_ID);
1000 + hw->txfs = (id >> LTQ_SPI_ID_TXFS_SHIFT) & LTQ_SPI_ID_TXFS_MASK;
1001 + hw->rxfs = (id >> LTQ_SPI_ID_TXFS_SHIFT) & LTQ_SPI_ID_TXFS_MASK;
1002 + hw->dma_support = (id & LTQ_SPI_ID_CFG) ? 1 : 0;
1004 + ltq_spi_config_mode_set(hw);
1006 + /* Enable error checking, disable TX/RX, set idle value high */
1007 + data = LTQ_SPI_CON_RUEN | LTQ_SPI_CON_AEN |
1008 + LTQ_SPI_CON_TEN | LTQ_SPI_CON_REN |
1009 + LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF | LTQ_SPI_CON_IDLE;
1010 + ltq_spi_reg_write(hw, data, LTQ_SPI_CON);
1012 + /* Enable master mode and clear error flags */
1013 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_SETMS |
1014 + LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
1016 + /* Reset GPIO/CS registers */
1017 + ltq_spi_reg_write(hw, 0x0, LTQ_SPI_GPOCON);
1018 + ltq_spi_reg_write(hw, 0xFF00, LTQ_SPI_FGPO);
1020 + /* Enable and flush FIFOs */
1021 + ltq_spi_reset_fifos(hw);
1023 + ret = spi_bitbang_start(&hw->bitbang);
1025 + dev_err(&pdev->dev, "spi_bitbang_start\n");
1029 + platform_set_drvdata(pdev, hw);
1031 + pr_info("Lantiq SoC SPI controller rev %u (TXFS %u, RXFS %u, DMA %u)\n",
1032 + id & LTQ_SPI_ID_REV_MASK, hw->txfs, hw->rxfs, hw->dma_support);
1037 + ltq_spi_hw_disable(hw);
1042 + for (; i > 0; i--)
1043 + free_irq(hw->irq[i], hw);
1046 + spi_master_put(master);
1052 +static int __exit ltq_spi_remove(struct platform_device *pdev)
1054 + struct ltq_spi *hw = platform_get_drvdata(pdev);
1057 + ret = spi_bitbang_stop(&hw->bitbang);
1061 + platform_set_drvdata(pdev, NULL);
1063 + ltq_spi_config_mode_set(hw);
1064 + ltq_spi_hw_disable(hw);
1066 + for (i = 0; i < ARRAY_SIZE(hw->irq); i++)
1067 + if (0 < hw->irq[i])
1068 + free_irq(hw->irq[i], hw);
1070 + gpio_free(LTQ_SPI_GPIO_DI);
1071 + gpio_free(LTQ_SPI_GPIO_DO);
1072 + gpio_free(LTQ_SPI_GPIO_CLK);
1075 + spi_master_put(hw->bitbang.master);
1080 +static struct platform_driver ltq_spi_driver = {
1082 + .name = "ltq-spi",
1083 + .owner = THIS_MODULE,
1085 + .remove = __exit_p(ltq_spi_remove),
1088 +static int __init ltq_spi_init(void)
1090 + return platform_driver_probe(<q_spi_driver, ltq_spi_probe);
1092 +module_init(ltq_spi_init);
1094 +static void __exit ltq_spi_exit(void)
1096 + platform_driver_unregister(<q_spi_driver);
1098 +module_exit(ltq_spi_exit);
1100 +MODULE_DESCRIPTION("Lantiq SoC SPI controller driver");
1101 +MODULE_AUTHOR("Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>");
1102 +MODULE_LICENSE("GPL");
1103 +MODULE_ALIAS("platform:ltq-spi");