1 From 3e3dd3612431578cac22ed73064a38c0e30c5284 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 10 Oct 2011 22:29:13 +0200
4 Subject: [PATCH 38/70] SPI: MIPS: lantiq: adds spi xway
7 .../mips/include/asm/mach-lantiq/lantiq_platform.h | 9 +
8 .../mips/include/asm/mach-lantiq/xway/lantiq_irq.h | 2 +
9 drivers/spi/Kconfig | 8 +
10 drivers/spi/Makefile | 1 +
11 drivers/spi/spi-xway.c | 1068 ++++++++++++++++++++
12 5 files changed, 1088 insertions(+), 0 deletions(-)
13 create mode 100644 drivers/spi/spi-xway.c
15 diff --git a/arch/mips/include/asm/mach-lantiq/lantiq_platform.h b/arch/mips/include/asm/mach-lantiq/lantiq_platform.h
16 index a305f1d..38ed938 100644
17 --- a/arch/mips/include/asm/mach-lantiq/lantiq_platform.h
18 +++ b/arch/mips/include/asm/mach-lantiq/lantiq_platform.h
19 @@ -50,4 +50,13 @@ struct ltq_eth_data {
24 +struct ltq_spi_platform_data {
28 +struct ltq_spi_controller_data {
33 diff --git a/arch/mips/include/asm/mach-lantiq/xway/lantiq_irq.h b/arch/mips/include/asm/mach-lantiq/xway/lantiq_irq.h
34 index 2a8d5ad..b7f10e6 100644
35 --- a/arch/mips/include/asm/mach-lantiq/xway/lantiq_irq.h
36 +++ b/arch/mips/include/asm/mach-lantiq/xway/lantiq_irq.h
39 #define LTQ_SSC_TIR (INT_NUM_IM0_IRL0 + 15)
40 #define LTQ_SSC_RIR (INT_NUM_IM0_IRL0 + 14)
41 +#define LTQ_SSC_TIR_AR9 (INT_NUM_IM0_IRL0 + 14)
42 +#define LTQ_SSC_RIR_AR9 (INT_NUM_IM0_IRL0 + 15)
43 #define LTQ_SSC_EIR (INT_NUM_IM0_IRL0 + 16)
45 #define LTQ_MEI_DYING_GASP_INT (INT_NUM_IM1_IRL0 + 21)
46 diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
47 index 41f8336..f0eaeeb 100644
48 --- a/drivers/spi/Kconfig
49 +++ b/drivers/spi/Kconfig
50 @@ -382,6 +382,14 @@ config SPI_NUC900
52 SPI driver for Nuvoton NUC900 series ARM SoCs
55 + tristate "Lantiq XWAY SPI controller"
56 + depends on LANTIQ && SOC_TYPE_XWAY
59 + This driver supports the Lantiq SoC SPI controller in master
63 # Add new SPI master controllers in alphabetical order above this line
65 diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
66 index 570894c..a465d9a 100644
67 --- a/drivers/spi/Makefile
68 +++ b/drivers/spi/Makefile
69 @@ -59,4 +59,5 @@ obj-$(CONFIG_SPI_TLE62X0) += spi-tle62x0.o
70 obj-$(CONFIG_SPI_TOPCLIFF_PCH) += spi-topcliff-pch.o
71 obj-$(CONFIG_SPI_TXX9) += spi-txx9.o
72 obj-$(CONFIG_SPI_XILINX) += spi-xilinx.o
73 +obj-$(CONFIG_SPI_XWAY) += spi-xway.o
75 diff --git a/drivers/spi/spi-xway.c b/drivers/spi/spi-xway.c
77 index 0000000..016a6d0
79 +++ b/drivers/spi/spi-xway.c
82 + * Lantiq SoC SPI controller
84 + * Copyright (C) 2011 Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>
86 + * This program is free software; you can distribute it and/or modify it
87 + * under the terms of the GNU General Public License (Version 2) as
88 + * published by the Free Software Foundation.
91 +#include <linux/init.h>
92 +#include <linux/module.h>
93 +#include <linux/workqueue.h>
94 +#include <linux/platform_device.h>
95 +#include <linux/io.h>
96 +#include <linux/sched.h>
97 +#include <linux/delay.h>
98 +#include <linux/interrupt.h>
99 +#include <linux/completion.h>
100 +#include <linux/spinlock.h>
101 +#include <linux/err.h>
102 +#include <linux/clk.h>
103 +#include <linux/gpio.h>
104 +#include <linux/spi/spi.h>
105 +#include <linux/spi/spi_bitbang.h>
107 +#include <lantiq_soc.h>
108 +#include <lantiq_platform.h>
110 +#define LTQ_SPI_CLC 0x00 /* Clock control */
111 +#define LTQ_SPI_PISEL 0x04 /* Port input select */
112 +#define LTQ_SPI_ID 0x08 /* Identification */
113 +#define LTQ_SPI_CON 0x10 /* Control */
114 +#define LTQ_SPI_STAT 0x14 /* Status */
115 +#define LTQ_SPI_WHBSTATE 0x18 /* Write HW modified state */
116 +#define LTQ_SPI_TB 0x20 /* Transmit buffer */
117 +#define LTQ_SPI_RB 0x24 /* Receive buffer */
118 +#define LTQ_SPI_RXFCON 0x30 /* Receive FIFO control */
119 +#define LTQ_SPI_TXFCON 0x34 /* Transmit FIFO control */
120 +#define LTQ_SPI_FSTAT 0x38 /* FIFO status */
121 +#define LTQ_SPI_BRT 0x40 /* Baudrate timer */
122 +#define LTQ_SPI_BRSTAT 0x44 /* Baudrate timer status */
123 +#define LTQ_SPI_SFCON 0x60 /* Serial frame control */
124 +#define LTQ_SPI_SFSTAT 0x64 /* Serial frame status */
125 +#define LTQ_SPI_GPOCON 0x70 /* General purpose output control */
126 +#define LTQ_SPI_GPOSTAT 0x74 /* General purpose output status */
127 +#define LTQ_SPI_FGPO 0x78 /* Forced general purpose output */
128 +#define LTQ_SPI_RXREQ 0x80 /* Receive request */
129 +#define LTQ_SPI_RXCNT 0x84 /* Receive count */
130 +#define LTQ_SPI_DMACON 0xEC /* DMA control */
131 +#define LTQ_SPI_IRNEN 0xF4 /* Interrupt node enable */
132 +#define LTQ_SPI_IRNICR 0xF8 /* Interrupt node interrupt capture */
133 +#define LTQ_SPI_IRNCR 0xFC /* Interrupt node control */
135 +#define LTQ_SPI_CLC_SMC_SHIFT 16 /* Clock divider for sleep mode */
136 +#define LTQ_SPI_CLC_SMC_MASK 0xFF
137 +#define LTQ_SPI_CLC_RMC_SHIFT 8 /* Clock divider for normal run mode */
138 +#define LTQ_SPI_CLC_RMC_MASK 0xFF
139 +#define LTQ_SPI_CLC_DISS BIT(1) /* Disable status bit */
140 +#define LTQ_SPI_CLC_DISR BIT(0) /* Disable request bit */
142 +#define LTQ_SPI_ID_TXFS_SHIFT 24 /* Implemented TX FIFO size */
143 +#define LTQ_SPI_ID_TXFS_MASK 0x3F
144 +#define LTQ_SPI_ID_RXFS_SHIFT 16 /* Implemented RX FIFO size */
145 +#define LTQ_SPI_ID_RXFS_MASK 0x3F
146 +#define LTQ_SPI_ID_REV_MASK 0x1F /* Hardware revision number */
147 +#define LTQ_SPI_ID_CFG BIT(5) /* DMA interface support */
149 +#define LTQ_SPI_CON_BM_SHIFT 16 /* Data width selection */
150 +#define LTQ_SPI_CON_BM_MASK 0x1F
151 +#define LTQ_SPI_CON_EM BIT(24) /* Echo mode */
152 +#define LTQ_SPI_CON_IDLE BIT(23) /* Idle bit value */
153 +#define LTQ_SPI_CON_ENBV BIT(22) /* Enable byte valid control */
154 +#define LTQ_SPI_CON_RUEN BIT(12) /* Receive underflow error enable */
155 +#define LTQ_SPI_CON_TUEN BIT(11) /* Transmit underflow error enable */
156 +#define LTQ_SPI_CON_AEN BIT(10) /* Abort error enable */
157 +#define LTQ_SPI_CON_REN BIT(9) /* Receive overflow error enable */
158 +#define LTQ_SPI_CON_TEN BIT(8) /* Transmit overflow error enable */
159 +#define LTQ_SPI_CON_LB BIT(7) /* Loopback control */
160 +#define LTQ_SPI_CON_PO BIT(6) /* Clock polarity control */
161 +#define LTQ_SPI_CON_PH BIT(5) /* Clock phase control */
162 +#define LTQ_SPI_CON_HB BIT(4) /* Heading control */
163 +#define LTQ_SPI_CON_RXOFF BIT(1) /* Switch receiver off */
164 +#define LTQ_SPI_CON_TXOFF BIT(0) /* Switch transmitter off */
166 +#define LTQ_SPI_STAT_RXBV_MASK 0x7
167 +#define LTQ_SPI_STAT_RXBV_SHIFT 28
168 +#define LTQ_SPI_STAT_BSY BIT(13) /* Busy flag */
169 +#define LTQ_SPI_STAT_RUE BIT(12) /* Receive underflow error flag */
170 +#define LTQ_SPI_STAT_TUE BIT(11) /* Transmit underflow error flag */
171 +#define LTQ_SPI_STAT_AE BIT(10) /* Abort error flag */
172 +#define LTQ_SPI_STAT_RE BIT(9) /* Receive error flag */
173 +#define LTQ_SPI_STAT_TE BIT(8) /* Transmit error flag */
174 +#define LTQ_SPI_STAT_MS BIT(1) /* Master/slave select bit */
175 +#define LTQ_SPI_STAT_EN BIT(0) /* Enable bit */
177 +#define LTQ_SPI_WHBSTATE_SETTUE BIT(15) /* Set transmit underflow error flag */
178 +#define LTQ_SPI_WHBSTATE_SETAE BIT(14) /* Set abort error flag */
179 +#define LTQ_SPI_WHBSTATE_SETRE BIT(13) /* Set receive error flag */
180 +#define LTQ_SPI_WHBSTATE_SETTE BIT(12) /* Set transmit error flag */
181 +#define LTQ_SPI_WHBSTATE_CLRTUE BIT(11) /* Clear transmit underflow error flag */
182 +#define LTQ_SPI_WHBSTATE_CLRAE BIT(10) /* Clear abort error flag */
183 +#define LTQ_SPI_WHBSTATE_CLRRE BIT(9) /* Clear receive error flag */
184 +#define LTQ_SPI_WHBSTATE_CLRTE BIT(8) /* Clear transmit error flag */
185 +#define LTQ_SPI_WHBSTATE_SETME BIT(7) /* Set mode error flag */
186 +#define LTQ_SPI_WHBSTATE_CLRME BIT(6) /* Clear mode error flag */
187 +#define LTQ_SPI_WHBSTATE_SETRUE BIT(5) /* Set receive underflow error flag */
188 +#define LTQ_SPI_WHBSTATE_CLRRUE BIT(4) /* Clear receive underflow error flag */
189 +#define LTQ_SPI_WHBSTATE_SETMS BIT(3) /* Set master select bit */
190 +#define LTQ_SPI_WHBSTATE_CLRMS BIT(2) /* Clear master select bit */
191 +#define LTQ_SPI_WHBSTATE_SETEN BIT(1) /* Set enable bit (operational mode) */
192 +#define LTQ_SPI_WHBSTATE_CLREN BIT(0) /* Clear enable bit (config mode */
193 +#define LTQ_SPI_WHBSTATE_CLR_ERRORS 0x0F50
195 +#define LTQ_SPI_RXFCON_RXFITL_SHIFT 8 /* FIFO interrupt trigger level */
196 +#define LTQ_SPI_RXFCON_RXFITL_MASK 0x3F
197 +#define LTQ_SPI_RXFCON_RXFLU BIT(1) /* FIFO flush */
198 +#define LTQ_SPI_RXFCON_RXFEN BIT(0) /* FIFO enable */
200 +#define LTQ_SPI_TXFCON_TXFITL_SHIFT 8 /* FIFO interrupt trigger level */
201 +#define LTQ_SPI_TXFCON_TXFITL_MASK 0x3F
202 +#define LTQ_SPI_TXFCON_TXFLU BIT(1) /* FIFO flush */
203 +#define LTQ_SPI_TXFCON_TXFEN BIT(0) /* FIFO enable */
205 +#define LTQ_SPI_FSTAT_RXFFL_MASK 0x3f
206 +#define LTQ_SPI_FSTAT_RXFFL_SHIFT 0
207 +#define LTQ_SPI_FSTAT_TXFFL_MASK 0x3f
208 +#define LTQ_SPI_FSTAT_TXFFL_SHIFT 8
210 +#define LTQ_SPI_GPOCON_ISCSBN_SHIFT 8
211 +#define LTQ_SPI_GPOCON_INVOUTN_SHIFT 0
213 +#define LTQ_SPI_FGPO_SETOUTN_SHIFT 8
214 +#define LTQ_SPI_FGPO_CLROUTN_SHIFT 0
216 +#define LTQ_SPI_RXREQ_RXCNT_MASK 0xFFFF /* Receive count value */
217 +#define LTQ_SPI_RXCNT_TODO_MASK 0xFFFF /* Recevie to-do value */
219 +#define LTQ_SPI_IRNEN_F BIT(3) /* Frame end interrupt request */
220 +#define LTQ_SPI_IRNEN_E BIT(2) /* Error end interrupt request */
221 +#define LTQ_SPI_IRNEN_T BIT(1) /* Transmit end interrupt request */
222 +#define LTQ_SPI_IRNEN_R BIT(0) /* Receive end interrupt request */
223 +#define LTQ_SPI_IRNEN_ALL 0xF
225 +/* Hard-wired GPIOs used by SPI controller */
226 +#define LTQ_SPI_GPIO_DI 16
227 +#define LTQ_SPI_GPIO_DO 17
228 +#define LTQ_SPI_GPIO_CLK 18
231 + struct spi_bitbang bitbang;
232 + struct completion done;
235 + struct device *dev;
236 + void __iomem *base;
237 + struct clk *fpiclk;
238 + struct clk *spiclk;
248 + struct spi_transfer *curr_transfer;
250 + u32 (*get_tx) (struct ltq_spi *);
254 + unsigned dma_support:1;
255 + unsigned cfg_mode:1;
259 +struct ltq_spi_controller_state {
260 + void (*cs_activate) (struct spi_device *);
261 + void (*cs_deactivate) (struct spi_device *);
264 +struct ltq_spi_irq_map {
266 + irq_handler_t handler;
269 +struct ltq_spi_cs_gpio_map {
274 +static inline struct ltq_spi *ltq_spi_to_hw(struct spi_device *spi)
276 + return spi_master_get_devdata(spi->master);
279 +static inline u32 ltq_spi_reg_read(struct ltq_spi *hw, u32 reg)
281 + return ioread32be(hw->base + reg);
284 +static inline void ltq_spi_reg_write(struct ltq_spi *hw, u32 val, u32 reg)
286 + iowrite32be(val, hw->base + reg);
289 +static inline void ltq_spi_reg_setbit(struct ltq_spi *hw, u32 bits, u32 reg)
293 + val = ltq_spi_reg_read(hw, reg);
295 + ltq_spi_reg_write(hw, val, reg);
298 +static inline void ltq_spi_reg_clearbit(struct ltq_spi *hw, u32 bits, u32 reg)
302 + val = ltq_spi_reg_read(hw, reg);
304 + ltq_spi_reg_write(hw, val, reg);
307 +static void ltq_spi_hw_enable(struct ltq_spi *hw)
311 + /* Power-up mdule */
312 + clk_enable(hw->spiclk);
315 + * Set clock divider for run mode to 1 to
316 + * run at same frequency as FPI bus
318 + clc = (1 << LTQ_SPI_CLC_RMC_SHIFT);
319 + ltq_spi_reg_write(hw, clc, LTQ_SPI_CLC);
322 +static void ltq_spi_hw_disable(struct ltq_spi *hw)
324 + /* Set clock divider to 0 and set module disable bit */
325 + ltq_spi_reg_write(hw, LTQ_SPI_CLC_DISS, LTQ_SPI_CLC);
327 + /* Power-down mdule */
328 + clk_disable(hw->spiclk);
331 +static void ltq_spi_reset_fifos(struct ltq_spi *hw)
336 + * Enable and flush FIFOs. Set interrupt trigger level to
337 + * half of FIFO count implemented in hardware.
339 + if (hw->txfs > 1) {
340 + val = hw->txfs << (LTQ_SPI_TXFCON_TXFITL_SHIFT - 1);
341 + val |= LTQ_SPI_TXFCON_TXFEN | LTQ_SPI_TXFCON_TXFLU;
342 + ltq_spi_reg_write(hw, val, LTQ_SPI_TXFCON);
345 + if (hw->rxfs > 1) {
346 + val = hw->rxfs << (LTQ_SPI_RXFCON_RXFITL_SHIFT - 1);
347 + val |= LTQ_SPI_RXFCON_RXFEN | LTQ_SPI_RXFCON_RXFLU;
348 + ltq_spi_reg_write(hw, val, LTQ_SPI_RXFCON);
352 +static inline int ltq_spi_wait_ready(struct ltq_spi *hw)
355 + unsigned long timeout;
357 + timeout = jiffies + msecs_to_jiffies(200);
360 + stat = ltq_spi_reg_read(hw, LTQ_SPI_STAT);
361 + if (!(stat & LTQ_SPI_STAT_BSY))
365 + } while (!time_after_eq(jiffies, timeout));
367 + dev_err(hw->dev, "SPI wait ready timed out\n");
372 +static void ltq_spi_config_mode_set(struct ltq_spi *hw)
378 + * Putting the SPI module in config mode is only safe if no
379 + * transfer is in progress as indicated by busy flag STATE.BSY.
381 + if (ltq_spi_wait_ready(hw)) {
382 + ltq_spi_reset_fifos(hw);
383 + hw->status = -ETIMEDOUT;
385 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_CLREN, LTQ_SPI_WHBSTATE);
390 +static void ltq_spi_run_mode_set(struct ltq_spi *hw)
395 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_SETEN, LTQ_SPI_WHBSTATE);
400 +static u32 ltq_spi_tx_word_u8(struct ltq_spi *hw)
402 + const u8 *tx = hw->tx;
411 +static u32 ltq_spi_tx_word_u16(struct ltq_spi *hw)
413 + const u16 *tx = (u16 *) hw->tx;
422 +static u32 ltq_spi_tx_word_u32(struct ltq_spi *hw)
424 + const u32 *tx = (u32 *) hw->tx;
433 +static void ltq_spi_bits_per_word_set(struct spi_device *spi)
435 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
437 + u8 bits_per_word = spi->bits_per_word;
440 + * Use either default value of SPI device or value
441 + * from current transfer.
443 + if (hw->curr_transfer && hw->curr_transfer->bits_per_word)
444 + bits_per_word = hw->curr_transfer->bits_per_word;
446 + if (bits_per_word <= 8)
447 + hw->get_tx = ltq_spi_tx_word_u8;
448 + else if (bits_per_word <= 16)
449 + hw->get_tx = ltq_spi_tx_word_u16;
450 + else if (bits_per_word <= 32)
451 + hw->get_tx = ltq_spi_tx_word_u32;
453 + /* CON.BM value = bits_per_word - 1 */
454 + bm = (bits_per_word - 1) << LTQ_SPI_CON_BM_SHIFT;
456 + ltq_spi_reg_clearbit(hw, LTQ_SPI_CON_BM_MASK <<
457 + LTQ_SPI_CON_BM_SHIFT, LTQ_SPI_CON);
458 + ltq_spi_reg_setbit(hw, bm, LTQ_SPI_CON);
461 +static void ltq_spi_speed_set(struct spi_device *spi)
463 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
464 + u32 br, max_speed_hz, spi_clk;
465 + u32 speed_hz = spi->max_speed_hz;
468 + * Use either default value of SPI device or value
469 + * from current transfer.
471 + if (hw->curr_transfer && hw->curr_transfer->speed_hz)
472 + speed_hz = hw->curr_transfer->speed_hz;
475 + * SPI module clock is derived from FPI bus clock dependent on
476 + * divider value in CLC.RMS which is always set to 1.
478 + spi_clk = clk_get_rate(hw->fpiclk);
481 + * Maximum SPI clock frequency in master mode is half of
482 + * SPI module clock frequency. Maximum reload value of
483 + * baudrate generator BR is 2^16.
485 + max_speed_hz = spi_clk / 2;
486 + if (speed_hz >= max_speed_hz)
489 + br = (max_speed_hz / speed_hz) - 1;
494 + ltq_spi_reg_write(hw, br, LTQ_SPI_BRT);
497 +static void ltq_spi_clockmode_set(struct spi_device *spi)
499 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
502 + con = ltq_spi_reg_read(hw, LTQ_SPI_CON);
505 + * SPI mode mapping in CON register:
506 + * Mode CPOL CPHA CON.PO CON.PH
512 + if (spi->mode & SPI_CPHA)
513 + con &= ~LTQ_SPI_CON_PH;
515 + con |= LTQ_SPI_CON_PH;
517 + if (spi->mode & SPI_CPOL)
518 + con |= LTQ_SPI_CON_PO;
520 + con &= ~LTQ_SPI_CON_PO;
522 + /* Set heading control */
523 + if (spi->mode & SPI_LSB_FIRST)
524 + con &= ~LTQ_SPI_CON_HB;
526 + con |= LTQ_SPI_CON_HB;
528 + ltq_spi_reg_write(hw, con, LTQ_SPI_CON);
531 +static void ltq_spi_xmit_set(struct ltq_spi *hw, struct spi_transfer *t)
535 + con = ltq_spi_reg_read(hw, LTQ_SPI_CON);
538 + if (t->tx_buf && t->rx_buf) {
539 + con &= ~(LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF);
540 + } else if (t->rx_buf) {
541 + con &= ~LTQ_SPI_CON_RXOFF;
542 + con |= LTQ_SPI_CON_TXOFF;
543 + } else if (t->tx_buf) {
544 + con &= ~LTQ_SPI_CON_TXOFF;
545 + con |= LTQ_SPI_CON_RXOFF;
548 + con |= (LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF);
550 + ltq_spi_reg_write(hw, con, LTQ_SPI_CON);
553 +static void ltq_spi_gpio_cs_activate(struct spi_device *spi)
555 + struct ltq_spi_controller_data *cdata = spi->controller_data;
556 + int val = spi->mode & SPI_CS_HIGH ? 1 : 0;
558 + gpio_set_value(cdata->gpio, val);
561 +static void ltq_spi_gpio_cs_deactivate(struct spi_device *spi)
563 + struct ltq_spi_controller_data *cdata = spi->controller_data;
564 + int val = spi->mode & SPI_CS_HIGH ? 0 : 1;
566 + gpio_set_value(cdata->gpio, val);
569 +static void ltq_spi_internal_cs_activate(struct spi_device *spi)
571 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
574 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_CLROUTN_SHIFT));
575 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
578 +static void ltq_spi_internal_cs_deactivate(struct spi_device *spi)
580 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
583 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_SETOUTN_SHIFT));
584 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
587 +static void ltq_spi_chipselect(struct spi_device *spi, int cs)
589 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
590 + struct ltq_spi_controller_state *cstate = spi->controller_state;
593 + case BITBANG_CS_ACTIVE:
594 + ltq_spi_bits_per_word_set(spi);
595 + ltq_spi_speed_set(spi);
596 + ltq_spi_clockmode_set(spi);
597 + ltq_spi_run_mode_set(hw);
599 + cstate->cs_activate(spi);
602 + case BITBANG_CS_INACTIVE:
603 + cstate->cs_deactivate(spi);
605 + ltq_spi_config_mode_set(hw);
611 +static int ltq_spi_setup_transfer(struct spi_device *spi,
612 + struct spi_transfer *t)
614 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
615 + u8 bits_per_word = spi->bits_per_word;
617 + hw->curr_transfer = t;
619 + if (t && t->bits_per_word)
620 + bits_per_word = t->bits_per_word;
622 + if (bits_per_word > 32)
625 + ltq_spi_config_mode_set(hw);
630 +static const struct ltq_spi_cs_gpio_map ltq_spi_cs[] = {
639 +static int ltq_spi_setup(struct spi_device *spi)
641 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
642 + struct ltq_spi_controller_data *cdata = spi->controller_data;
643 + struct ltq_spi_controller_state *cstate;
647 + /* Set default word length to 8 if not set */
648 + if (!spi->bits_per_word)
649 + spi->bits_per_word = 8;
651 + if (spi->bits_per_word > 32)
654 + if (!spi->controller_state) {
655 + cstate = kzalloc(sizeof(struct ltq_spi_controller_state),
660 + spi->controller_state = cstate;
665 + * Up to six GPIOs can be connected to the SPI module
666 + * via GPIO alternate function to control the chip select lines.
667 + * For more flexibility in board layout this driver can also control
668 + * the CS lines via GPIO API. If GPIOs should be used, board setup code
669 + * have to register the SPI device with struct ltq_spi_controller_data
672 + if (cdata && cdata->gpio) {
673 + ret = gpio_request(cdata->gpio, "spi-cs");
677 + ret = spi->mode & SPI_CS_HIGH ? 0 : 1;
678 + gpio_direction_output(cdata->gpio, ret);
680 + cstate->cs_activate = ltq_spi_gpio_cs_activate;
681 + cstate->cs_deactivate = ltq_spi_gpio_cs_deactivate;
683 + ret = ltq_gpio_request(&spi->dev, ltq_spi_cs[spi->chip_select].gpio,
684 + ltq_spi_cs[spi->chip_select].mux,
689 + gpocon = (1 << (spi->chip_select +
690 + LTQ_SPI_GPOCON_ISCSBN_SHIFT));
692 + if (spi->mode & SPI_CS_HIGH)
693 + gpocon |= (1 << spi->chip_select);
695 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_SETOUTN_SHIFT));
697 + ltq_spi_reg_setbit(hw, gpocon, LTQ_SPI_GPOCON);
698 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
700 + cstate->cs_activate = ltq_spi_internal_cs_activate;
701 + cstate->cs_deactivate = ltq_spi_internal_cs_deactivate;
707 +static void ltq_spi_cleanup(struct spi_device *spi)
709 + struct ltq_spi_controller_data *cdata = spi->controller_data;
710 + struct ltq_spi_controller_state *cstate = spi->controller_state;
713 + if (cdata && cdata->gpio)
714 + gpio = cdata->gpio;
716 + gpio = ltq_spi_cs[spi->chip_select].gpio;
722 +static void ltq_spi_txfifo_write(struct ltq_spi *hw)
727 + /* Determine how much FIFOs are free for TX data */
728 + fstat = ltq_spi_reg_read(hw, LTQ_SPI_FSTAT);
729 + fifo_space = hw->txfs - ((fstat >> LTQ_SPI_FSTAT_TXFFL_SHIFT) &
730 + LTQ_SPI_FSTAT_TXFFL_MASK);
735 + while (hw->tx_cnt < hw->len && fifo_space) {
736 + data = hw->get_tx(hw);
737 + ltq_spi_reg_write(hw, data, LTQ_SPI_TB);
742 +static void ltq_spi_rxfifo_read(struct ltq_spi *hw)
744 + u32 fstat, data, *rx32;
746 + u8 rxbv, shift, *rx8;
748 + /* Determine how much FIFOs are filled with RX data */
749 + fstat = ltq_spi_reg_read(hw, LTQ_SPI_FSTAT);
750 + fifo_fill = ((fstat >> LTQ_SPI_FSTAT_RXFFL_SHIFT)
751 + & LTQ_SPI_FSTAT_RXFFL_MASK);
757 + * The 32 bit FIFO is always used completely independent from the
758 + * bits_per_word value. Thus four bytes have to be read at once
761 + rx32 = (u32 *) hw->rx;
762 + while (hw->len - hw->rx_cnt >= 4 && fifo_fill) {
763 + *rx32++ = ltq_spi_reg_read(hw, LTQ_SPI_RB);
770 + * If there are remaining bytes, read byte count from STAT.RXBV
771 + * register and read the data byte-wise.
773 + while (fifo_fill && hw->rx_cnt < hw->len) {
774 + rxbv = (ltq_spi_reg_read(hw, LTQ_SPI_STAT) >>
775 + LTQ_SPI_STAT_RXBV_SHIFT) & LTQ_SPI_STAT_RXBV_MASK;
776 + data = ltq_spi_reg_read(hw, LTQ_SPI_RB);
778 + shift = (rxbv - 1) * 8;
782 + *rx8++ = (data >> shift) & 0xFF;
793 +static void ltq_spi_rxreq_set(struct ltq_spi *hw)
795 + u32 rxreq, rxreq_max, rxtodo;
797 + rxtodo = ltq_spi_reg_read(hw, LTQ_SPI_RXCNT) & LTQ_SPI_RXCNT_TODO_MASK;
800 + * In RX-only mode the serial clock is activated only after writing
801 + * the expected amount of RX bytes into RXREQ register.
802 + * To avoid receive overflows at high clocks it is better to request
803 + * only the amount of bytes that fits into all FIFOs. This value
804 + * depends on the FIFO size implemented in hardware.
806 + rxreq = hw->len - hw->rx_cnt;
807 + rxreq_max = hw->rxfs << 2;
808 + rxreq = min(rxreq_max, rxreq);
810 + if (!rxtodo && rxreq)
811 + ltq_spi_reg_write(hw, rxreq, LTQ_SPI_RXREQ);
814 +static inline void ltq_spi_complete(struct ltq_spi *hw)
816 + complete(&hw->done);
819 +irqreturn_t ltq_spi_tx_irq(int irq, void *data)
821 + struct ltq_spi *hw = data;
822 + unsigned long flags;
825 + spin_lock_irqsave(&hw->lock, flags);
827 + if (hw->tx_cnt < hw->len)
828 + ltq_spi_txfifo_write(hw);
830 + if (hw->tx_cnt == hw->len)
833 + spin_unlock_irqrestore(&hw->lock, flags);
836 + ltq_spi_complete(hw);
838 + return IRQ_HANDLED;
841 +irqreturn_t ltq_spi_rx_irq(int irq, void *data)
843 + struct ltq_spi *hw = data;
844 + unsigned long flags;
847 + spin_lock_irqsave(&hw->lock, flags);
849 + if (hw->rx_cnt < hw->len) {
850 + ltq_spi_rxfifo_read(hw);
852 + if (hw->tx && hw->tx_cnt < hw->len)
853 + ltq_spi_txfifo_write(hw);
856 + if (hw->rx_cnt == hw->len)
859 + ltq_spi_rxreq_set(hw);
861 + spin_unlock_irqrestore(&hw->lock, flags);
864 + ltq_spi_complete(hw);
866 + return IRQ_HANDLED;
869 +irqreturn_t ltq_spi_err_irq(int irq, void *data)
871 + struct ltq_spi *hw = data;
872 + unsigned long flags;
874 + spin_lock_irqsave(&hw->lock, flags);
876 + /* Disable all interrupts */
877 + ltq_spi_reg_clearbit(hw, LTQ_SPI_IRNEN_ALL, LTQ_SPI_IRNEN);
879 + /* Clear all error flags */
880 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
883 + ltq_spi_reg_setbit(hw, LTQ_SPI_RXFCON_RXFLU, LTQ_SPI_RXFCON);
884 + ltq_spi_reg_setbit(hw, LTQ_SPI_TXFCON_TXFLU, LTQ_SPI_TXFCON);
887 + spin_unlock_irqrestore(&hw->lock, flags);
889 + ltq_spi_complete(hw);
891 + return IRQ_HANDLED;
894 +static int ltq_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
896 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
899 + hw->tx = t->tx_buf;
900 + hw->rx = t->rx_buf;
905 + INIT_COMPLETION(hw->done);
907 + ltq_spi_xmit_set(hw, t);
909 + /* Enable error interrupts */
910 + ltq_spi_reg_setbit(hw, LTQ_SPI_IRNEN_E, LTQ_SPI_IRNEN);
913 + /* Initially fill TX FIFO with as much data as possible */
914 + ltq_spi_txfifo_write(hw);
915 + irq_flags |= LTQ_SPI_IRNEN_T;
917 + /* Always enable RX interrupt in Full Duplex mode */
919 + irq_flags |= LTQ_SPI_IRNEN_R;
920 + } else if (hw->rx) {
921 + /* Start RX clock */
922 + ltq_spi_rxreq_set(hw);
924 + /* Enable RX interrupt to receive data from RX FIFOs */
925 + irq_flags |= LTQ_SPI_IRNEN_R;
928 + /* Enable TX or RX interrupts */
929 + ltq_spi_reg_setbit(hw, irq_flags, LTQ_SPI_IRNEN);
930 + wait_for_completion_interruptible(&hw->done);
932 + /* Disable all interrupts */
933 + ltq_spi_reg_clearbit(hw, LTQ_SPI_IRNEN_ALL, LTQ_SPI_IRNEN);
936 + * Return length of current transfer for bitbang utility code if
937 + * no errors occured during transmission.
940 + hw->status = hw->len;
945 +static const struct ltq_spi_irq_map ltq_spi_irqs[] = {
946 + { "spi_tx", ltq_spi_tx_irq },
947 + { "spi_rx", ltq_spi_rx_irq },
948 + { "spi_err", ltq_spi_err_irq },
951 +static int __init ltq_spi_probe(struct platform_device *pdev)
953 + struct spi_master *master;
954 + struct resource *r;
955 + struct ltq_spi *hw;
956 + struct ltq_spi_platform_data *pdata = pdev->dev.platform_data;
960 + master = spi_alloc_master(&pdev->dev, sizeof(struct ltq_spi));
962 + dev_err(&pdev->dev, "spi_alloc_master\n");
967 + hw = spi_master_get_devdata(master);
969 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
971 + dev_err(&pdev->dev, "platform_get_resource\n");
976 + r = devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
979 + dev_err(&pdev->dev, "devm_request_mem_region\n");
984 + hw->base = devm_ioremap_nocache(&pdev->dev, r->start, resource_size(r));
986 + dev_err(&pdev->dev, "devm_ioremap_nocache\n");
991 + hw->fpiclk = clk_get_fpi();
992 + if (IS_ERR(hw->fpiclk)) {
993 + dev_err(&pdev->dev, "clk_get\n");
994 + ret = PTR_ERR(hw->fpiclk);
998 + hw->spiclk = clk_get(&pdev->dev, NULL);
999 + if (IS_ERR(hw->spiclk)) {
1000 + dev_err(&pdev->dev, "clk_get\n");
1001 + ret = PTR_ERR(hw->spiclk);
1005 + memset(hw->irq, 0, sizeof(hw->irq));
1006 + for (i = 0; i < ARRAY_SIZE(ltq_spi_irqs); i++) {
1007 + ret = platform_get_irq_byname(pdev, ltq_spi_irqs[i].name);
1009 + dev_err(&pdev->dev, "platform_get_irq_byname\n");
1014 + ret = request_irq(hw->irq[i], ltq_spi_irqs[i].handler,
1015 + 0, ltq_spi_irqs[i].name, hw);
1017 + dev_err(&pdev->dev, "request_irq\n");
1022 + hw->bitbang.master = spi_master_get(master);
1023 + hw->bitbang.chipselect = ltq_spi_chipselect;
1024 + hw->bitbang.setup_transfer = ltq_spi_setup_transfer;
1025 + hw->bitbang.txrx_bufs = ltq_spi_txrx_bufs;
1027 + master->bus_num = pdev->id;
1028 + master->num_chipselect = pdata->num_chipselect;
1029 + master->setup = ltq_spi_setup;
1030 + master->cleanup = ltq_spi_cleanup;
1032 + hw->dev = &pdev->dev;
1033 + init_completion(&hw->done);
1034 + spin_lock_init(&hw->lock);
1036 + /* Set GPIO alternate functions to SPI */
1037 + ltq_gpio_request(&pdev->dev, LTQ_SPI_GPIO_DI, 2, 0, "spi-di");
1038 + ltq_gpio_request(&pdev->dev, LTQ_SPI_GPIO_DO, 2, 1, "spi-do");
1039 + ltq_gpio_request(&pdev->dev, LTQ_SPI_GPIO_CLK, 2, 1, "spi-clk");
1041 + ltq_spi_hw_enable(hw);
1043 + /* Read module capabilities */
1044 + id = ltq_spi_reg_read(hw, LTQ_SPI_ID);
1045 + hw->txfs = (id >> LTQ_SPI_ID_TXFS_SHIFT) & LTQ_SPI_ID_TXFS_MASK;
1046 + hw->rxfs = (id >> LTQ_SPI_ID_TXFS_SHIFT) & LTQ_SPI_ID_TXFS_MASK;
1047 + hw->dma_support = (id & LTQ_SPI_ID_CFG) ? 1 : 0;
1049 + ltq_spi_config_mode_set(hw);
1051 + /* Enable error checking, disable TX/RX, set idle value high */
1052 + data = LTQ_SPI_CON_RUEN | LTQ_SPI_CON_AEN |
1053 + LTQ_SPI_CON_TEN | LTQ_SPI_CON_REN |
1054 + LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF | LTQ_SPI_CON_IDLE;
1055 + ltq_spi_reg_write(hw, data, LTQ_SPI_CON);
1057 + /* Enable master mode and clear error flags */
1058 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_SETMS |
1059 + LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
1061 + /* Reset GPIO/CS registers */
1062 + ltq_spi_reg_write(hw, 0x0, LTQ_SPI_GPOCON);
1063 + ltq_spi_reg_write(hw, 0xFF00, LTQ_SPI_FGPO);
1065 + /* Enable and flush FIFOs */
1066 + ltq_spi_reset_fifos(hw);
1068 + ret = spi_bitbang_start(&hw->bitbang);
1070 + dev_err(&pdev->dev, "spi_bitbang_start\n");
1074 + platform_set_drvdata(pdev, hw);
1076 + pr_info("Lantiq SoC SPI controller rev %u (TXFS %u, RXFS %u, DMA %u)\n",
1077 + id & LTQ_SPI_ID_REV_MASK, hw->txfs, hw->rxfs, hw->dma_support);
1082 + ltq_spi_hw_disable(hw);
1085 + clk_put(hw->fpiclk);
1087 + for (; i > 0; i--)
1088 + free_irq(hw->irq[i], hw);
1091 + spi_master_put(master);
1097 +static int __exit ltq_spi_remove(struct platform_device *pdev)
1099 + struct ltq_spi *hw = platform_get_drvdata(pdev);
1102 + ret = spi_bitbang_stop(&hw->bitbang);
1106 + platform_set_drvdata(pdev, NULL);
1108 + ltq_spi_config_mode_set(hw);
1109 + ltq_spi_hw_disable(hw);
1111 + for (i = 0; i < ARRAY_SIZE(hw->irq); i++)
1112 + if (0 < hw->irq[i])
1113 + free_irq(hw->irq[i], hw);
1115 + gpio_free(LTQ_SPI_GPIO_DI);
1116 + gpio_free(LTQ_SPI_GPIO_DO);
1117 + gpio_free(LTQ_SPI_GPIO_CLK);
1119 + clk_put(hw->fpiclk);
1120 + spi_master_put(hw->bitbang.master);
1125 +static struct platform_driver ltq_spi_driver = {
1127 + .name = "ltq_spi",
1128 + .owner = THIS_MODULE,
1130 + .remove = __exit_p(ltq_spi_remove),
1133 +static int __init ltq_spi_init(void)
1135 + return platform_driver_probe(<q_spi_driver, ltq_spi_probe);
1137 +module_init(ltq_spi_init);
1139 +static void __exit ltq_spi_exit(void)
1141 + platform_driver_unregister(<q_spi_driver);
1143 +module_exit(ltq_spi_exit);
1145 +MODULE_DESCRIPTION("Lantiq SoC SPI controller driver");
1146 +MODULE_AUTHOR("Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>");
1147 +MODULE_LICENSE("GPL");
1148 +MODULE_ALIAS("platform:ltq-spi");