1 From db447f1a18106aa4d32438ab72ff57024b34cee4 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Thu, 16 Aug 2012 09:57:01 +0200
4 Subject: [PATCH 114/123] SPI: MIPS: lantiq: adds spi-xway
6 This patch adds support for the SPI core found on several Lantiq SoCs.
7 The Driver has been runtime tested in combination with m25p80 Flash Devices
10 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>
11 Signed-off-by: John Crispin <blogic@openwrt.org>
13 drivers/spi/Kconfig | 8 +
14 drivers/spi/Makefile | 1 +
15 drivers/spi/spi-xway.c | 977 ++++++++++++++++++++++++++++++++++++++++++++++++
16 3 files changed, 986 insertions(+)
17 create mode 100644 drivers/spi/spi-xway.c
19 diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
20 index 1acae35..d79a587 100644
21 --- a/drivers/spi/Kconfig
22 +++ b/drivers/spi/Kconfig
23 @@ -434,6 +434,14 @@ config SPI_NUC900
25 SPI driver for Nuvoton NUC900 series ARM SoCs
28 + tristate "Lantiq XWAY SPI controller"
29 + depends on LANTIQ && SOC_TYPE_XWAY
32 + This driver supports the Lantiq SoC SPI controller in master
36 # Add new SPI master controllers in alphabetical order above this line
38 diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
39 index c48df47..7e344a9 100644
40 --- a/drivers/spi/Makefile
41 +++ b/drivers/spi/Makefile
42 @@ -66,4 +66,5 @@ obj-$(CONFIG_SPI_TOPCLIFF_PCH) += spi-topcliff-pch.o
43 obj-$(CONFIG_SPI_TXX9) += spi-txx9.o
44 obj-$(CONFIG_SPI_XCOMM) += spi-xcomm.o
45 obj-$(CONFIG_SPI_XILINX) += spi-xilinx.o
46 +obj-$(CONFIG_SPI_XWAY) += spi-xway.o
48 diff --git a/drivers/spi/spi-xway.c b/drivers/spi/spi-xway.c
50 index 0000000..8441085
52 +++ b/drivers/spi/spi-xway.c
55 + * Lantiq SoC SPI controller
57 + * Copyright (C) 2011 Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>
58 + * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
60 + * This program is free software; you can distribute it and/or modify it
61 + * under the terms of the GNU General Public License (Version 2) as
62 + * published by the Free Software Foundation.
65 +#include <linux/init.h>
66 +#include <linux/module.h>
67 +#include <linux/workqueue.h>
68 +#include <linux/platform_device.h>
69 +#include <linux/io.h>
70 +#include <linux/sched.h>
71 +#include <linux/delay.h>
72 +#include <linux/interrupt.h>
73 +#include <linux/completion.h>
74 +#include <linux/spinlock.h>
75 +#include <linux/err.h>
76 +#include <linux/clk.h>
77 +#include <linux/spi/spi.h>
78 +#include <linux/spi/spi_bitbang.h>
79 +#include <linux/of_irq.h>
81 +#include <lantiq_soc.h>
83 +#define LTQ_SPI_CLC 0x00 /* Clock control */
84 +#define LTQ_SPI_PISEL 0x04 /* Port input select */
85 +#define LTQ_SPI_ID 0x08 /* Identification */
86 +#define LTQ_SPI_CON 0x10 /* Control */
87 +#define LTQ_SPI_STAT 0x14 /* Status */
88 +#define LTQ_SPI_WHBSTATE 0x18 /* Write HW modified state */
89 +#define LTQ_SPI_TB 0x20 /* Transmit buffer */
90 +#define LTQ_SPI_RB 0x24 /* Receive buffer */
91 +#define LTQ_SPI_RXFCON 0x30 /* Receive FIFO control */
92 +#define LTQ_SPI_TXFCON 0x34 /* Transmit FIFO control */
93 +#define LTQ_SPI_FSTAT 0x38 /* FIFO status */
94 +#define LTQ_SPI_BRT 0x40 /* Baudrate timer */
95 +#define LTQ_SPI_BRSTAT 0x44 /* Baudrate timer status */
96 +#define LTQ_SPI_SFCON 0x60 /* Serial frame control */
97 +#define LTQ_SPI_SFSTAT 0x64 /* Serial frame status */
98 +#define LTQ_SPI_GPOCON 0x70 /* General purpose output control */
99 +#define LTQ_SPI_GPOSTAT 0x74 /* General purpose output status */
100 +#define LTQ_SPI_FGPO 0x78 /* Forced general purpose output */
101 +#define LTQ_SPI_RXREQ 0x80 /* Receive request */
102 +#define LTQ_SPI_RXCNT 0x84 /* Receive count */
103 +#define LTQ_SPI_DMACON 0xEC /* DMA control */
104 +#define LTQ_SPI_IRNEN 0xF4 /* Interrupt node enable */
105 +#define LTQ_SPI_IRNICR 0xF8 /* Interrupt node interrupt capture */
106 +#define LTQ_SPI_IRNCR 0xFC /* Interrupt node control */
108 +#define LTQ_SPI_CLC_SMC_SHIFT 16 /* Clock divider for sleep mode */
109 +#define LTQ_SPI_CLC_SMC_MASK 0xFF
110 +#define LTQ_SPI_CLC_RMC_SHIFT 8 /* Clock divider for normal run mode */
111 +#define LTQ_SPI_CLC_RMC_MASK 0xFF
112 +#define LTQ_SPI_CLC_DISS BIT(1) /* Disable status bit */
113 +#define LTQ_SPI_CLC_DISR BIT(0) /* Disable request bit */
115 +#define LTQ_SPI_ID_TXFS_SHIFT 24 /* Implemented TX FIFO size */
116 +#define LTQ_SPI_ID_TXFS_MASK 0x3F
117 +#define LTQ_SPI_ID_RXFS_SHIFT 16 /* Implemented RX FIFO size */
118 +#define LTQ_SPI_ID_RXFS_MASK 0x3F
119 +#define LTQ_SPI_ID_REV_MASK 0x1F /* Hardware revision number */
120 +#define LTQ_SPI_ID_CFG BIT(5) /* DMA interface support */
122 +#define LTQ_SPI_CON_BM_SHIFT 16 /* Data width selection */
123 +#define LTQ_SPI_CON_BM_MASK 0x1F
124 +#define LTQ_SPI_CON_EM BIT(24) /* Echo mode */
125 +#define LTQ_SPI_CON_IDLE BIT(23) /* Idle bit value */
126 +#define LTQ_SPI_CON_ENBV BIT(22) /* Enable byte valid control */
127 +#define LTQ_SPI_CON_RUEN BIT(12) /* Receive underflow error enable */
128 +#define LTQ_SPI_CON_TUEN BIT(11) /* Transmit underflow error enable */
129 +#define LTQ_SPI_CON_AEN BIT(10) /* Abort error enable */
130 +#define LTQ_SPI_CON_REN BIT(9) /* Receive overflow error enable */
131 +#define LTQ_SPI_CON_TEN BIT(8) /* Transmit overflow error enable */
132 +#define LTQ_SPI_CON_LB BIT(7) /* Loopback control */
133 +#define LTQ_SPI_CON_PO BIT(6) /* Clock polarity control */
134 +#define LTQ_SPI_CON_PH BIT(5) /* Clock phase control */
135 +#define LTQ_SPI_CON_HB BIT(4) /* Heading control */
136 +#define LTQ_SPI_CON_RXOFF BIT(1) /* Switch receiver off */
137 +#define LTQ_SPI_CON_TXOFF BIT(0) /* Switch transmitter off */
139 +#define LTQ_SPI_STAT_RXBV_MASK 0x7
140 +#define LTQ_SPI_STAT_RXBV_SHIFT 28
141 +#define LTQ_SPI_STAT_BSY BIT(13) /* Busy flag */
142 +#define LTQ_SPI_STAT_RUE BIT(12) /* Receive underflow error flag */
143 +#define LTQ_SPI_STAT_TUE BIT(11) /* Transmit underflow error flag */
144 +#define LTQ_SPI_STAT_AE BIT(10) /* Abort error flag */
145 +#define LTQ_SPI_STAT_RE BIT(9) /* Receive error flag */
146 +#define LTQ_SPI_STAT_TE BIT(8) /* Transmit error flag */
147 +#define LTQ_SPI_STAT_MS BIT(1) /* Master/slave select bit */
148 +#define LTQ_SPI_STAT_EN BIT(0) /* Enable bit */
150 +#define LTQ_SPI_WHBSTATE_SETTUE BIT(15) /* Set transmit underflow error flag */
151 +#define LTQ_SPI_WHBSTATE_SETAE BIT(14) /* Set abort error flag */
152 +#define LTQ_SPI_WHBSTATE_SETRE BIT(13) /* Set receive error flag */
153 +#define LTQ_SPI_WHBSTATE_SETTE BIT(12) /* Set transmit error flag */
154 +#define LTQ_SPI_WHBSTATE_CLRTUE BIT(11) /* Clear transmit underflow error
156 +#define LTQ_SPI_WHBSTATE_CLRAE BIT(10) /* Clear abort error flag */
157 +#define LTQ_SPI_WHBSTATE_CLRRE BIT(9) /* Clear receive error flag */
158 +#define LTQ_SPI_WHBSTATE_CLRTE BIT(8) /* Clear transmit error flag */
159 +#define LTQ_SPI_WHBSTATE_SETME BIT(7) /* Set mode error flag */
160 +#define LTQ_SPI_WHBSTATE_CLRME BIT(6) /* Clear mode error flag */
161 +#define LTQ_SPI_WHBSTATE_SETRUE BIT(5) /* Set receive underflow error flag */
162 +#define LTQ_SPI_WHBSTATE_CLRRUE BIT(4) /* Clear receive underflow error flag */
163 +#define LTQ_SPI_WHBSTATE_SETMS BIT(3) /* Set master select bit */
164 +#define LTQ_SPI_WHBSTATE_CLRMS BIT(2) /* Clear master select bit */
165 +#define LTQ_SPI_WHBSTATE_SETEN BIT(1) /* Set enable bit (operational mode) */
166 +#define LTQ_SPI_WHBSTATE_CLREN BIT(0) /* Clear enable bit (config mode */
167 +#define LTQ_SPI_WHBSTATE_CLR_ERRORS 0x0F50
169 +#define LTQ_SPI_RXFCON_RXFITL_SHIFT 8 /* FIFO interrupt trigger level */
170 +#define LTQ_SPI_RXFCON_RXFITL_MASK 0x3F
171 +#define LTQ_SPI_RXFCON_RXFLU BIT(1) /* FIFO flush */
172 +#define LTQ_SPI_RXFCON_RXFEN BIT(0) /* FIFO enable */
174 +#define LTQ_SPI_TXFCON_TXFITL_SHIFT 8 /* FIFO interrupt trigger level */
175 +#define LTQ_SPI_TXFCON_TXFITL_MASK 0x3F
176 +#define LTQ_SPI_TXFCON_TXFLU BIT(1) /* FIFO flush */
177 +#define LTQ_SPI_TXFCON_TXFEN BIT(0) /* FIFO enable */
179 +#define LTQ_SPI_FSTAT_RXFFL_MASK 0x3f
180 +#define LTQ_SPI_FSTAT_RXFFL_SHIFT 0
181 +#define LTQ_SPI_FSTAT_TXFFL_MASK 0x3f
182 +#define LTQ_SPI_FSTAT_TXFFL_SHIFT 8
184 +#define LTQ_SPI_GPOCON_ISCSBN_SHIFT 8
185 +#define LTQ_SPI_GPOCON_INVOUTN_SHIFT 0
187 +#define LTQ_SPI_FGPO_SETOUTN_SHIFT 8
188 +#define LTQ_SPI_FGPO_CLROUTN_SHIFT 0
190 +#define LTQ_SPI_RXREQ_RXCNT_MASK 0xFFFF /* Receive count value */
191 +#define LTQ_SPI_RXCNT_TODO_MASK 0xFFFF /* Recevie to-do value */
193 +#define LTQ_SPI_IRNEN_F BIT(3) /* Frame end interrupt request */
194 +#define LTQ_SPI_IRNEN_E BIT(2) /* Error end interrupt request */
195 +#define LTQ_SPI_IRNEN_T BIT(1) /* Transmit end interrupt request */
196 +#define LTQ_SPI_IRNEN_R BIT(0) /* Receive end interrupt request */
197 +#define LTQ_SPI_IRNEN_ALL 0xF
200 + struct spi_bitbang bitbang;
201 + struct completion done;
204 + struct device *dev;
205 + void __iomem *base;
206 + struct clk *fpiclk;
207 + struct clk *spiclk;
217 + struct spi_transfer *curr_transfer;
219 + u32 (*get_tx) (struct ltq_spi *);
223 + unsigned dma_support:1;
224 + unsigned cfg_mode:1;
227 +static inline struct ltq_spi *ltq_spi_to_hw(struct spi_device *spi)
229 + return spi_master_get_devdata(spi->master);
232 +static inline u32 ltq_spi_reg_read(struct ltq_spi *hw, u32 reg)
234 + return ioread32be(hw->base + reg);
237 +static inline void ltq_spi_reg_write(struct ltq_spi *hw, u32 val, u32 reg)
239 + iowrite32be(val, hw->base + reg);
242 +static inline void ltq_spi_reg_setbit(struct ltq_spi *hw, u32 bits, u32 reg)
246 + val = ltq_spi_reg_read(hw, reg);
248 + ltq_spi_reg_write(hw, val, reg);
251 +static inline void ltq_spi_reg_clearbit(struct ltq_spi *hw, u32 bits, u32 reg)
255 + val = ltq_spi_reg_read(hw, reg);
257 + ltq_spi_reg_write(hw, val, reg);
260 +static void ltq_spi_hw_enable(struct ltq_spi *hw)
264 + /* Power-up module */
265 + clk_enable(hw->spiclk);
268 + * Set clock divider for run mode to 1 to
269 + * run at same frequency as FPI bus
271 + clc = (1 << LTQ_SPI_CLC_RMC_SHIFT);
272 + ltq_spi_reg_write(hw, clc, LTQ_SPI_CLC);
275 +static void ltq_spi_hw_disable(struct ltq_spi *hw)
277 + /* Set clock divider to 0 and set module disable bit */
278 + ltq_spi_reg_write(hw, LTQ_SPI_CLC_DISS, LTQ_SPI_CLC);
280 + /* Power-down module */
281 + clk_disable(hw->spiclk);
284 +static void ltq_spi_reset_fifos(struct ltq_spi *hw)
289 + * Enable and flush FIFOs. Set interrupt trigger level to
290 + * half of FIFO count implemented in hardware.
292 + if (hw->txfs > 1) {
293 + val = hw->txfs << (LTQ_SPI_TXFCON_TXFITL_SHIFT - 1);
294 + val |= LTQ_SPI_TXFCON_TXFEN | LTQ_SPI_TXFCON_TXFLU;
295 + ltq_spi_reg_write(hw, val, LTQ_SPI_TXFCON);
298 + if (hw->rxfs > 1) {
299 + val = hw->rxfs << (LTQ_SPI_RXFCON_RXFITL_SHIFT - 1);
300 + val |= LTQ_SPI_RXFCON_RXFEN | LTQ_SPI_RXFCON_RXFLU;
301 + ltq_spi_reg_write(hw, val, LTQ_SPI_RXFCON);
305 +static inline int ltq_spi_wait_ready(struct ltq_spi *hw)
308 + unsigned long timeout;
310 + timeout = jiffies + msecs_to_jiffies(200);
313 + stat = ltq_spi_reg_read(hw, LTQ_SPI_STAT);
314 + if (!(stat & LTQ_SPI_STAT_BSY))
318 + } while (!time_after_eq(jiffies, timeout));
320 + dev_err(hw->dev, "SPI wait ready timed out stat: %x\n", stat);
325 +static void ltq_spi_config_mode_set(struct ltq_spi *hw)
331 + * Putting the SPI module in config mode is only safe if no
332 + * transfer is in progress as indicated by busy flag STATE.BSY.
334 + if (ltq_spi_wait_ready(hw)) {
335 + ltq_spi_reset_fifos(hw);
336 + hw->status = -ETIMEDOUT;
338 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_CLREN, LTQ_SPI_WHBSTATE);
343 +static void ltq_spi_run_mode_set(struct ltq_spi *hw)
348 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_SETEN, LTQ_SPI_WHBSTATE);
353 +static u32 ltq_spi_tx_word_u8(struct ltq_spi *hw)
355 + const u8 *tx = hw->tx;
364 +static u32 ltq_spi_tx_word_u16(struct ltq_spi *hw)
366 + const u16 *tx = (u16 *) hw->tx;
375 +static u32 ltq_spi_tx_word_u32(struct ltq_spi *hw)
377 + const u32 *tx = (u32 *) hw->tx;
386 +static void ltq_spi_bits_per_word_set(struct spi_device *spi)
388 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
390 + u8 bits_per_word = spi->bits_per_word;
393 + * Use either default value of SPI device or value
394 + * from current transfer.
396 + if (hw->curr_transfer && hw->curr_transfer->bits_per_word)
397 + bits_per_word = hw->curr_transfer->bits_per_word;
399 + if (bits_per_word <= 8)
400 + hw->get_tx = ltq_spi_tx_word_u8;
401 + else if (bits_per_word <= 16)
402 + hw->get_tx = ltq_spi_tx_word_u16;
403 + else if (bits_per_word <= 32)
404 + hw->get_tx = ltq_spi_tx_word_u32;
406 + /* CON.BM value = bits_per_word - 1 */
407 + bm = (bits_per_word - 1) << LTQ_SPI_CON_BM_SHIFT;
409 + ltq_spi_reg_clearbit(hw, LTQ_SPI_CON_BM_MASK <<
410 + LTQ_SPI_CON_BM_SHIFT, LTQ_SPI_CON);
411 + ltq_spi_reg_setbit(hw, bm, LTQ_SPI_CON);
414 +static void ltq_spi_speed_set(struct spi_device *spi)
416 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
417 + u32 br, max_speed_hz, spi_clk;
418 + u32 speed_hz = spi->max_speed_hz;
421 + * Use either default value of SPI device or value
422 + * from current transfer.
424 + if (hw->curr_transfer && hw->curr_transfer->speed_hz)
425 + speed_hz = hw->curr_transfer->speed_hz;
428 + * SPI module clock is derived from FPI bus clock dependent on
429 + * divider value in CLC.RMS which is always set to 1.
431 + spi_clk = clk_get_rate(hw->fpiclk);
434 + * Maximum SPI clock frequency in master mode is half of
435 + * SPI module clock frequency. Maximum reload value of
436 + * baudrate generator BR is 2^16.
438 + max_speed_hz = spi_clk / 2;
439 + if (speed_hz >= max_speed_hz)
442 + br = (max_speed_hz / speed_hz) - 1;
447 + ltq_spi_reg_write(hw, br, LTQ_SPI_BRT);
450 +static void ltq_spi_clockmode_set(struct spi_device *spi)
452 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
455 + con = ltq_spi_reg_read(hw, LTQ_SPI_CON);
458 + * SPI mode mapping in CON register:
459 + * Mode CPOL CPHA CON.PO CON.PH
465 + if (spi->mode & SPI_CPHA)
466 + con &= ~LTQ_SPI_CON_PH;
468 + con |= LTQ_SPI_CON_PH;
470 + if (spi->mode & SPI_CPOL)
471 + con |= LTQ_SPI_CON_PO;
473 + con &= ~LTQ_SPI_CON_PO;
475 + /* Set heading control */
476 + if (spi->mode & SPI_LSB_FIRST)
477 + con &= ~LTQ_SPI_CON_HB;
479 + con |= LTQ_SPI_CON_HB;
481 + ltq_spi_reg_write(hw, con, LTQ_SPI_CON);
484 +static void ltq_spi_xmit_set(struct ltq_spi *hw, struct spi_transfer *t)
488 + con = ltq_spi_reg_read(hw, LTQ_SPI_CON);
491 + if (t->tx_buf && t->rx_buf) {
492 + con &= ~(LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF);
493 + } else if (t->rx_buf) {
494 + con &= ~LTQ_SPI_CON_RXOFF;
495 + con |= LTQ_SPI_CON_TXOFF;
496 + } else if (t->tx_buf) {
497 + con &= ~LTQ_SPI_CON_TXOFF;
498 + con |= LTQ_SPI_CON_RXOFF;
501 + con |= (LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF);
503 + ltq_spi_reg_write(hw, con, LTQ_SPI_CON);
506 +static void ltq_spi_internal_cs_activate(struct spi_device *spi)
508 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
511 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_CLROUTN_SHIFT));
512 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
515 +static void ltq_spi_internal_cs_deactivate(struct spi_device *spi)
517 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
520 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_SETOUTN_SHIFT));
521 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
524 +static void ltq_spi_chipselect(struct spi_device *spi, int cs)
526 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
529 + case BITBANG_CS_ACTIVE:
530 + ltq_spi_bits_per_word_set(spi);
531 + ltq_spi_speed_set(spi);
532 + ltq_spi_clockmode_set(spi);
533 + ltq_spi_run_mode_set(hw);
534 + ltq_spi_internal_cs_activate(spi);
537 + case BITBANG_CS_INACTIVE:
538 + ltq_spi_internal_cs_deactivate(spi);
539 + ltq_spi_config_mode_set(hw);
544 +static int ltq_spi_setup_transfer(struct spi_device *spi,
545 + struct spi_transfer *t)
547 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
548 + u8 bits_per_word = spi->bits_per_word;
550 + hw->curr_transfer = t;
552 + if (t && t->bits_per_word)
553 + bits_per_word = t->bits_per_word;
555 + if (bits_per_word > 32)
558 + ltq_spi_config_mode_set(hw);
563 +static int ltq_spi_setup(struct spi_device *spi)
565 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
568 + /* Set default word length to 8 if not set */
569 + if (!spi->bits_per_word)
570 + spi->bits_per_word = 8;
572 + if (spi->bits_per_word > 32)
576 + * Up to six GPIOs can be connected to the SPI module
577 + * via GPIO alternate function to control the chip select lines.
579 + gpocon = (1 << (spi->chip_select +
580 + LTQ_SPI_GPOCON_ISCSBN_SHIFT));
582 + if (spi->mode & SPI_CS_HIGH)
583 + gpocon |= (1 << spi->chip_select);
585 + fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_SETOUTN_SHIFT));
587 + ltq_spi_reg_setbit(hw, gpocon, LTQ_SPI_GPOCON);
588 + ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
593 +static void ltq_spi_cleanup(struct spi_device *spi)
598 +static void ltq_spi_txfifo_write(struct ltq_spi *hw)
603 + /* Determine how much FIFOs are free for TX data */
604 + fstat = ltq_spi_reg_read(hw, LTQ_SPI_FSTAT);
605 + fifo_space = hw->txfs - ((fstat >> LTQ_SPI_FSTAT_TXFFL_SHIFT) &
606 + LTQ_SPI_FSTAT_TXFFL_MASK);
611 + while (hw->tx_cnt < hw->len && fifo_space) {
612 + data = hw->get_tx(hw);
613 + ltq_spi_reg_write(hw, data, LTQ_SPI_TB);
618 +static void ltq_spi_rxfifo_read(struct ltq_spi *hw)
620 + u32 fstat, data, *rx32;
622 + u8 rxbv, shift, *rx8;
624 + /* Determine how much FIFOs are filled with RX data */
625 + fstat = ltq_spi_reg_read(hw, LTQ_SPI_FSTAT);
626 + fifo_fill = ((fstat >> LTQ_SPI_FSTAT_RXFFL_SHIFT)
627 + & LTQ_SPI_FSTAT_RXFFL_MASK);
633 + * The 32 bit FIFO is always used completely independent from the
634 + * bits_per_word value. Thus four bytes have to be read at once
637 + rx32 = (u32 *) hw->rx;
638 + while (hw->len - hw->rx_cnt >= 4 && fifo_fill) {
639 + *rx32++ = ltq_spi_reg_read(hw, LTQ_SPI_RB);
646 + * If there are remaining bytes, read byte count from STAT.RXBV
647 + * register and read the data byte-wise.
649 + while (fifo_fill && hw->rx_cnt < hw->len) {
650 + rxbv = (ltq_spi_reg_read(hw, LTQ_SPI_STAT) >>
651 + LTQ_SPI_STAT_RXBV_SHIFT) & LTQ_SPI_STAT_RXBV_MASK;
652 + data = ltq_spi_reg_read(hw, LTQ_SPI_RB);
654 + shift = (rxbv - 1) * 8;
658 + *rx8++ = (data >> shift) & 0xFF;
669 +static void ltq_spi_rxreq_set(struct ltq_spi *hw)
671 + u32 rxreq, rxreq_max, rxtodo;
673 + rxtodo = ltq_spi_reg_read(hw, LTQ_SPI_RXCNT) & LTQ_SPI_RXCNT_TODO_MASK;
676 + * In RX-only mode the serial clock is activated only after writing
677 + * the expected amount of RX bytes into RXREQ register.
678 + * To avoid receive overflows at high clocks it is better to request
679 + * only the amount of bytes that fits into all FIFOs. This value
680 + * depends on the FIFO size implemented in hardware.
682 + rxreq = hw->len - hw->rx_cnt;
683 + rxreq_max = hw->rxfs << 2;
684 + rxreq = min(rxreq_max, rxreq);
686 + if (!rxtodo && rxreq)
687 + ltq_spi_reg_write(hw, rxreq, LTQ_SPI_RXREQ);
690 +static inline void ltq_spi_complete(struct ltq_spi *hw)
692 + complete(&hw->done);
695 +irqreturn_t ltq_spi_tx_irq(int irq, void *data)
697 + struct ltq_spi *hw = data;
698 + unsigned long flags;
701 + spin_lock_irqsave(&hw->lock, flags);
703 + if (hw->tx_cnt < hw->len)
704 + ltq_spi_txfifo_write(hw);
706 + if (hw->tx_cnt == hw->len)
709 + spin_unlock_irqrestore(&hw->lock, flags);
712 + ltq_spi_complete(hw);
714 + return IRQ_HANDLED;
717 +irqreturn_t ltq_spi_rx_irq(int irq, void *data)
719 + struct ltq_spi *hw = data;
720 + unsigned long flags;
723 + spin_lock_irqsave(&hw->lock, flags);
725 + if (hw->rx_cnt < hw->len) {
726 + ltq_spi_rxfifo_read(hw);
728 + if (hw->tx && hw->tx_cnt < hw->len)
729 + ltq_spi_txfifo_write(hw);
732 + if (hw->rx_cnt == hw->len)
735 + ltq_spi_rxreq_set(hw);
737 + spin_unlock_irqrestore(&hw->lock, flags);
740 + ltq_spi_complete(hw);
742 + return IRQ_HANDLED;
745 +irqreturn_t ltq_spi_err_irq(int irq, void *data)
747 + struct ltq_spi *hw = data;
748 + unsigned long flags;
750 + spin_lock_irqsave(&hw->lock, flags);
752 + /* Disable all interrupts */
753 + ltq_spi_reg_clearbit(hw, LTQ_SPI_IRNEN_ALL, LTQ_SPI_IRNEN);
755 + /* Clear all error flags */
756 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
759 + ltq_spi_reg_setbit(hw, LTQ_SPI_RXFCON_RXFLU, LTQ_SPI_RXFCON);
760 + ltq_spi_reg_setbit(hw, LTQ_SPI_TXFCON_TXFLU, LTQ_SPI_TXFCON);
763 + spin_unlock_irqrestore(&hw->lock, flags);
765 + ltq_spi_complete(hw);
767 + return IRQ_HANDLED;
770 +static int ltq_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
772 + struct ltq_spi *hw = ltq_spi_to_hw(spi);
775 + hw->tx = t->tx_buf;
776 + hw->rx = t->rx_buf;
781 + INIT_COMPLETION(hw->done);
783 + ltq_spi_xmit_set(hw, t);
785 + /* Enable error interrupts */
786 + ltq_spi_reg_setbit(hw, LTQ_SPI_IRNEN_E, LTQ_SPI_IRNEN);
789 + /* Initially fill TX FIFO with as much data as possible */
790 + ltq_spi_txfifo_write(hw);
791 + irq_flags |= LTQ_SPI_IRNEN_T;
793 + /* Always enable RX interrupt in Full Duplex mode */
795 + irq_flags |= LTQ_SPI_IRNEN_R;
796 + } else if (hw->rx) {
797 + /* Start RX clock */
798 + ltq_spi_rxreq_set(hw);
800 + /* Enable RX interrupt to receive data from RX FIFOs */
801 + irq_flags |= LTQ_SPI_IRNEN_R;
804 + /* Enable TX or RX interrupts */
805 + ltq_spi_reg_setbit(hw, irq_flags, LTQ_SPI_IRNEN);
806 + wait_for_completion_interruptible(&hw->done);
808 + /* Disable all interrupts */
809 + ltq_spi_reg_clearbit(hw, LTQ_SPI_IRNEN_ALL, LTQ_SPI_IRNEN);
812 + * Return length of current transfer for bitbang utility code if
813 + * no errors occured during transmission.
816 + hw->status = hw->len;
821 +static const struct ltq_spi_irq_map {
823 + irq_handler_t handler;
824 +} ltq_spi_irqs[] = {
825 + { "spi_rx", ltq_spi_rx_irq },
826 + { "spi_tx", ltq_spi_tx_irq },
827 + { "spi_err", ltq_spi_err_irq },
830 +static int __devinit ltq_spi_probe(struct platform_device *pdev)
832 + struct resource irqres[3];
833 + struct spi_master *master;
834 + struct resource *r;
835 + struct ltq_spi *hw;
839 + if (of_irq_to_resource_table(pdev->dev.of_node, irqres, 3) != 3) {
840 + dev_err(&pdev->dev, "IRQ settings missing in device tree\n");
844 + master = spi_alloc_master(&pdev->dev, sizeof(struct ltq_spi));
846 + dev_err(&pdev->dev, "spi_alloc_master\n");
851 + hw = spi_master_get_devdata(master);
853 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
855 + dev_err(&pdev->dev, "platform_get_resource\n");
860 + r = devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
863 + dev_err(&pdev->dev, "failed to request memory region\n");
868 + hw->base = devm_ioremap_nocache(&pdev->dev, r->start, resource_size(r));
870 + dev_err(&pdev->dev, "failed to remap memory region\n");
875 + memset(hw->irq, 0, sizeof(hw->irq));
876 + for (i = 0; i < ARRAY_SIZE(ltq_spi_irqs); i++) {
877 + hw->irq[i] = irqres[i].start;
878 + ret = request_irq(hw->irq[i], ltq_spi_irqs[i].handler,
879 + 0, ltq_spi_irqs[i].name, hw);
881 + dev_err(&pdev->dev, "failed to request %s irq (%d)\n",
882 + ltq_spi_irqs[i].name, hw->irq[i]);
887 + hw->fpiclk = clk_get_fpi();
888 + if (IS_ERR(hw->fpiclk)) {
889 + dev_err(&pdev->dev, "failed to get fpi clock\n");
890 + ret = PTR_ERR(hw->fpiclk);
894 + hw->spiclk = clk_get(&pdev->dev, NULL);
895 + if (IS_ERR(hw->spiclk)) {
896 + dev_err(&pdev->dev, "failed to get spi clock gate\n");
897 + ret = PTR_ERR(hw->spiclk);
901 + hw->bitbang.master = spi_master_get(master);
902 + hw->bitbang.chipselect = ltq_spi_chipselect;
903 + hw->bitbang.setup_transfer = ltq_spi_setup_transfer;
904 + hw->bitbang.txrx_bufs = ltq_spi_txrx_bufs;
906 + if (of_machine_is_compatible("lantiq,ase"))
907 + master->num_chipselect = 3;
909 + master->num_chipselect = 6;
910 + master->bus_num = pdev->id;
911 + master->setup = ltq_spi_setup;
912 + master->cleanup = ltq_spi_cleanup;
913 + master->dev.of_node = pdev->dev.of_node;
915 + hw->dev = &pdev->dev;
916 + init_completion(&hw->done);
917 + spin_lock_init(&hw->lock);
919 + ltq_spi_hw_enable(hw);
921 + /* Read module capabilities */
922 + id = ltq_spi_reg_read(hw, LTQ_SPI_ID);
923 + hw->txfs = (id >> LTQ_SPI_ID_TXFS_SHIFT) & LTQ_SPI_ID_TXFS_MASK;
924 + hw->rxfs = (id >> LTQ_SPI_ID_TXFS_SHIFT) & LTQ_SPI_ID_TXFS_MASK;
925 + hw->dma_support = (id & LTQ_SPI_ID_CFG) ? 1 : 0;
927 + ltq_spi_config_mode_set(hw);
929 + /* Enable error checking, disable TX/RX, set idle value high */
930 + data = LTQ_SPI_CON_RUEN | LTQ_SPI_CON_AEN |
931 + LTQ_SPI_CON_TEN | LTQ_SPI_CON_REN |
932 + LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF | LTQ_SPI_CON_IDLE;
933 + ltq_spi_reg_write(hw, data, LTQ_SPI_CON);
935 + /* Enable master mode and clear error flags */
936 + ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_SETMS |
937 + LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
939 + /* Reset GPIO/CS registers */
940 + ltq_spi_reg_write(hw, 0x0, LTQ_SPI_GPOCON);
941 + ltq_spi_reg_write(hw, 0xFF00, LTQ_SPI_FGPO);
943 + /* Enable and flush FIFOs */
944 + ltq_spi_reset_fifos(hw);
946 + ret = spi_bitbang_start(&hw->bitbang);
948 + dev_err(&pdev->dev, "spi_bitbang_start failed\n");
952 + platform_set_drvdata(pdev, hw);
954 + pr_info("Lantiq SoC SPI controller rev %u (TXFS %u, RXFS %u, DMA %u)\n",
955 + id & LTQ_SPI_ID_REV_MASK, hw->txfs, hw->rxfs, hw->dma_support);
960 + ltq_spi_hw_disable(hw);
964 + clk_put(hw->fpiclk);
966 + clk_put(hw->spiclk);
969 + clk_put(hw->fpiclk);
972 + free_irq(hw->irq[i], hw);
975 + spi_master_put(master);
981 +static int __devexit ltq_spi_remove(struct platform_device *pdev)
983 + struct ltq_spi *hw = platform_get_drvdata(pdev);
986 + ret = spi_bitbang_stop(&hw->bitbang);
990 + platform_set_drvdata(pdev, NULL);
992 + ltq_spi_config_mode_set(hw);
993 + ltq_spi_hw_disable(hw);
995 + for (i = 0; i < ARRAY_SIZE(hw->irq); i++)
996 + if (0 < hw->irq[i])
997 + free_irq(hw->irq[i], hw);
1000 + clk_put(hw->fpiclk);
1002 + clk_put(hw->spiclk);
1004 + spi_master_put(hw->bitbang.master);
1009 +static const struct of_device_id ltq_spi_match[] = {
1010 + { .compatible = "lantiq,spi-xway" },
1013 +MODULE_DEVICE_TABLE(of, ltq_spi_match);
1015 +static struct platform_driver ltq_spi_driver = {
1016 + .probe = ltq_spi_probe,
1017 + .remove = __devexit_p(ltq_spi_remove),
1019 + .name = "spi-xway",
1020 + .owner = THIS_MODULE,
1021 + .of_match_table = ltq_spi_match,
1025 +module_platform_driver(ltq_spi_driver);
1027 +MODULE_DESCRIPTION("Lantiq SoC SPI controller driver");
1028 +MODULE_AUTHOR("Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>");
1029 +MODULE_LICENSE("GPL");
1030 +MODULE_ALIAS("platform:spi-xway");