42b3a575b67cc4dd08a3ed756f2b9f2a2d0e15fa
[openwrt.git] / target / linux / lantiq / patches-3.7 / 0114-SPI-MIPS-lantiq-adds-spi-xway.patch
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
5
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
8 on Amazon_SE and VR9.
9
10 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>
11 Signed-off-by: John Crispin <blogic@openwrt.org>
12 ---
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
18
19 --- a/drivers/spi/Kconfig
20 +++ b/drivers/spi/Kconfig
21 @@ -443,6 +443,14 @@ config SPI_NUC900
22         help
23           SPI driver for Nuvoton NUC900 series ARM SoCs
24  
25 +config SPI_XWAY
26 +       tristate "Lantiq XWAY SPI controller"
27 +       depends on LANTIQ && SOC_TYPE_XWAY
28 +       select SPI_BITBANG
29 +       help
30 +         This driver supports the Lantiq SoC SPI controller in master
31 +         mode.
32 +
33  #
34  # Add new SPI master controllers in alphabetical order above this line
35  #
36 --- a/drivers/spi/Makefile
37 +++ b/drivers/spi/Makefile
38 @@ -67,4 +67,5 @@ obj-$(CONFIG_SPI_TOPCLIFF_PCH)                += spi-t
39  obj-$(CONFIG_SPI_TXX9)                 += spi-txx9.o
40  obj-$(CONFIG_SPI_XCOMM)                += spi-xcomm.o
41  obj-$(CONFIG_SPI_XILINX)               += spi-xilinx.o
42 +obj-$(CONFIG_SPI_XWAY)         += spi-xway.o
43  
44 --- /dev/null
45 +++ b/drivers/spi/spi-xway.c
46 @@ -0,0 +1,977 @@
47 +/*
48 + * Lantiq SoC SPI controller
49 + *
50 + * Copyright (C) 2011 Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>
51 + * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
52 + *
53 + * This program is free software; you can distribute it and/or modify it
54 + * under the terms of the GNU General Public License (Version 2) as
55 + * published by the Free Software Foundation.
56 + */
57 +
58 +#include <linux/init.h>
59 +#include <linux/module.h>
60 +#include <linux/workqueue.h>
61 +#include <linux/platform_device.h>
62 +#include <linux/io.h>
63 +#include <linux/sched.h>
64 +#include <linux/delay.h>
65 +#include <linux/interrupt.h>
66 +#include <linux/completion.h>
67 +#include <linux/spinlock.h>
68 +#include <linux/err.h>
69 +#include <linux/clk.h>
70 +#include <linux/spi/spi.h>
71 +#include <linux/spi/spi_bitbang.h>
72 +#include <linux/of_irq.h>
73 +
74 +#include <lantiq_soc.h>
75 +
76 +#define LTQ_SPI_CLC            0x00    /* Clock control */
77 +#define LTQ_SPI_PISEL          0x04    /* Port input select */
78 +#define LTQ_SPI_ID             0x08    /* Identification */
79 +#define LTQ_SPI_CON            0x10    /* Control */
80 +#define LTQ_SPI_STAT           0x14    /* Status */
81 +#define LTQ_SPI_WHBSTATE       0x18    /* Write HW modified state */
82 +#define LTQ_SPI_TB             0x20    /* Transmit buffer */
83 +#define LTQ_SPI_RB             0x24    /* Receive buffer */
84 +#define LTQ_SPI_RXFCON         0x30    /* Receive FIFO control */
85 +#define LTQ_SPI_TXFCON         0x34    /* Transmit FIFO control */
86 +#define LTQ_SPI_FSTAT          0x38    /* FIFO status */
87 +#define LTQ_SPI_BRT            0x40    /* Baudrate timer */
88 +#define LTQ_SPI_BRSTAT         0x44    /* Baudrate timer status */
89 +#define LTQ_SPI_SFCON          0x60    /* Serial frame control */
90 +#define LTQ_SPI_SFSTAT         0x64    /* Serial frame status */
91 +#define LTQ_SPI_GPOCON         0x70    /* General purpose output control */
92 +#define LTQ_SPI_GPOSTAT                0x74    /* General purpose output status */
93 +#define LTQ_SPI_FGPO           0x78    /* Forced general purpose output */
94 +#define LTQ_SPI_RXREQ          0x80    /* Receive request */
95 +#define LTQ_SPI_RXCNT          0x84    /* Receive count */
96 +#define LTQ_SPI_DMACON         0xEC    /* DMA control */
97 +#define LTQ_SPI_IRNEN          0xF4    /* Interrupt node enable */
98 +#define LTQ_SPI_IRNICR         0xF8    /* Interrupt node interrupt capture */
99 +#define LTQ_SPI_IRNCR          0xFC    /* Interrupt node control */
100 +
101 +#define LTQ_SPI_CLC_SMC_SHIFT  16      /* Clock divider for sleep mode */
102 +#define LTQ_SPI_CLC_SMC_MASK   0xFF
103 +#define LTQ_SPI_CLC_RMC_SHIFT  8       /* Clock divider for normal run mode */
104 +#define LTQ_SPI_CLC_RMC_MASK   0xFF
105 +#define LTQ_SPI_CLC_DISS       BIT(1)  /* Disable status bit */
106 +#define LTQ_SPI_CLC_DISR       BIT(0)  /* Disable request bit */
107 +
108 +#define LTQ_SPI_ID_TXFS_SHIFT  24      /* Implemented TX FIFO size */
109 +#define LTQ_SPI_ID_TXFS_MASK   0x3F
110 +#define LTQ_SPI_ID_RXFS_SHIFT  16      /* Implemented RX FIFO size */
111 +#define LTQ_SPI_ID_RXFS_MASK   0x3F
112 +#define LTQ_SPI_ID_REV_MASK    0x1F    /* Hardware revision number */
113 +#define LTQ_SPI_ID_CFG         BIT(5)  /* DMA interface support */
114 +
115 +#define LTQ_SPI_CON_BM_SHIFT   16      /* Data width selection */
116 +#define LTQ_SPI_CON_BM_MASK    0x1F
117 +#define LTQ_SPI_CON_EM         BIT(24) /* Echo mode */
118 +#define LTQ_SPI_CON_IDLE       BIT(23) /* Idle bit value */
119 +#define LTQ_SPI_CON_ENBV       BIT(22) /* Enable byte valid control */
120 +#define LTQ_SPI_CON_RUEN       BIT(12) /* Receive underflow error enable */
121 +#define LTQ_SPI_CON_TUEN       BIT(11) /* Transmit underflow error enable */
122 +#define LTQ_SPI_CON_AEN                BIT(10) /* Abort error enable */
123 +#define LTQ_SPI_CON_REN                BIT(9)  /* Receive overflow error enable */
124 +#define LTQ_SPI_CON_TEN                BIT(8)  /* Transmit overflow error enable */
125 +#define LTQ_SPI_CON_LB         BIT(7)  /* Loopback control */
126 +#define LTQ_SPI_CON_PO         BIT(6)  /* Clock polarity control */
127 +#define LTQ_SPI_CON_PH         BIT(5)  /* Clock phase control */
128 +#define LTQ_SPI_CON_HB         BIT(4)  /* Heading control */
129 +#define LTQ_SPI_CON_RXOFF      BIT(1)  /* Switch receiver off */
130 +#define LTQ_SPI_CON_TXOFF      BIT(0)  /* Switch transmitter off */
131 +
132 +#define LTQ_SPI_STAT_RXBV_MASK 0x7
133 +#define LTQ_SPI_STAT_RXBV_SHIFT        28
134 +#define LTQ_SPI_STAT_BSY       BIT(13) /* Busy flag */
135 +#define LTQ_SPI_STAT_RUE       BIT(12) /* Receive underflow error flag */
136 +#define LTQ_SPI_STAT_TUE       BIT(11) /* Transmit underflow error flag */
137 +#define LTQ_SPI_STAT_AE                BIT(10) /* Abort error flag */
138 +#define LTQ_SPI_STAT_RE                BIT(9)  /* Receive error flag */
139 +#define LTQ_SPI_STAT_TE                BIT(8)  /* Transmit error flag */
140 +#define LTQ_SPI_STAT_MS                BIT(1)  /* Master/slave select bit */
141 +#define LTQ_SPI_STAT_EN                BIT(0)  /* Enable bit */
142 +
143 +#define LTQ_SPI_WHBSTATE_SETTUE        BIT(15) /* Set transmit underflow error flag */
144 +#define LTQ_SPI_WHBSTATE_SETAE BIT(14) /* Set abort error flag */
145 +#define LTQ_SPI_WHBSTATE_SETRE BIT(13) /* Set receive error flag */
146 +#define LTQ_SPI_WHBSTATE_SETTE BIT(12) /* Set transmit error flag */
147 +#define LTQ_SPI_WHBSTATE_CLRTUE        BIT(11) /* Clear transmit underflow error
148 +                                               flag */
149 +#define LTQ_SPI_WHBSTATE_CLRAE BIT(10) /* Clear abort error flag */
150 +#define LTQ_SPI_WHBSTATE_CLRRE BIT(9)  /* Clear receive error flag */
151 +#define LTQ_SPI_WHBSTATE_CLRTE BIT(8)  /* Clear transmit error flag */
152 +#define LTQ_SPI_WHBSTATE_SETME BIT(7)  /* Set mode error flag */
153 +#define LTQ_SPI_WHBSTATE_CLRME BIT(6)  /* Clear mode error flag */
154 +#define LTQ_SPI_WHBSTATE_SETRUE        BIT(5)  /* Set receive underflow error flag */
155 +#define LTQ_SPI_WHBSTATE_CLRRUE        BIT(4)  /* Clear receive underflow error flag */
156 +#define LTQ_SPI_WHBSTATE_SETMS BIT(3)  /* Set master select bit */
157 +#define LTQ_SPI_WHBSTATE_CLRMS BIT(2)  /* Clear master select bit */
158 +#define LTQ_SPI_WHBSTATE_SETEN BIT(1)  /* Set enable bit (operational mode) */
159 +#define LTQ_SPI_WHBSTATE_CLREN BIT(0)  /* Clear enable bit (config mode */
160 +#define LTQ_SPI_WHBSTATE_CLR_ERRORS    0x0F50
161 +
162 +#define LTQ_SPI_RXFCON_RXFITL_SHIFT    8 /* FIFO interrupt trigger level */
163 +#define LTQ_SPI_RXFCON_RXFITL_MASK     0x3F
164 +#define LTQ_SPI_RXFCON_RXFLU           BIT(1)  /* FIFO flush */
165 +#define LTQ_SPI_RXFCON_RXFEN           BIT(0)  /* FIFO enable */
166 +
167 +#define LTQ_SPI_TXFCON_TXFITL_SHIFT    8 /* FIFO interrupt trigger level */
168 +#define LTQ_SPI_TXFCON_TXFITL_MASK     0x3F
169 +#define LTQ_SPI_TXFCON_TXFLU           BIT(1)  /* FIFO flush */
170 +#define LTQ_SPI_TXFCON_TXFEN           BIT(0)  /* FIFO enable */
171 +
172 +#define LTQ_SPI_FSTAT_RXFFL_MASK       0x3f
173 +#define LTQ_SPI_FSTAT_RXFFL_SHIFT      0
174 +#define LTQ_SPI_FSTAT_TXFFL_MASK       0x3f
175 +#define LTQ_SPI_FSTAT_TXFFL_SHIFT      8
176 +
177 +#define LTQ_SPI_GPOCON_ISCSBN_SHIFT    8
178 +#define LTQ_SPI_GPOCON_INVOUTN_SHIFT   0
179 +
180 +#define LTQ_SPI_FGPO_SETOUTN_SHIFT     8
181 +#define LTQ_SPI_FGPO_CLROUTN_SHIFT     0
182 +
183 +#define LTQ_SPI_RXREQ_RXCNT_MASK       0xFFFF  /* Receive count value */
184 +#define LTQ_SPI_RXCNT_TODO_MASK                0xFFFF  /* Recevie to-do value */
185 +
186 +#define LTQ_SPI_IRNEN_F                BIT(3)  /* Frame end interrupt request */
187 +#define LTQ_SPI_IRNEN_E                BIT(2)  /* Error end interrupt request */
188 +#define LTQ_SPI_IRNEN_T                BIT(1)  /* Transmit end interrupt request */
189 +#define LTQ_SPI_IRNEN_R                BIT(0)  /* Receive end interrupt request */
190 +#define LTQ_SPI_IRNEN_ALL      0xF
191 +
192 +struct ltq_spi {
193 +       struct spi_bitbang      bitbang;
194 +       struct completion       done;
195 +       spinlock_t              lock;
196 +
197 +       struct device           *dev;
198 +       void __iomem            *base;
199 +       struct clk              *fpiclk;
200 +       struct clk              *spiclk;
201 +
202 +       int                     status;
203 +       int                     irq[3];
204 +
205 +       const u8                *tx;
206 +       u8                      *rx;
207 +       u32                     tx_cnt;
208 +       u32                     rx_cnt;
209 +       u32                     len;
210 +       struct spi_transfer     *curr_transfer;
211 +
212 +       u32 (*get_tx) (struct ltq_spi *);
213 +
214 +       u16                     txfs;
215 +       u16                     rxfs;
216 +       unsigned                dma_support:1;
217 +       unsigned                cfg_mode:1;
218 +};
219 +
220 +static inline struct ltq_spi *ltq_spi_to_hw(struct spi_device *spi)
221 +{
222 +       return spi_master_get_devdata(spi->master);
223 +}
224 +
225 +static inline u32 ltq_spi_reg_read(struct ltq_spi *hw, u32 reg)
226 +{
227 +       return ioread32be(hw->base + reg);
228 +}
229 +
230 +static inline void ltq_spi_reg_write(struct ltq_spi *hw, u32 val, u32 reg)
231 +{
232 +       iowrite32be(val, hw->base + reg);
233 +}
234 +
235 +static inline void ltq_spi_reg_setbit(struct ltq_spi *hw, u32 bits, u32 reg)
236 +{
237 +       u32 val;
238 +
239 +       val = ltq_spi_reg_read(hw, reg);
240 +       val |= bits;
241 +       ltq_spi_reg_write(hw, val, reg);
242 +}
243 +
244 +static inline void ltq_spi_reg_clearbit(struct ltq_spi *hw, u32 bits, u32 reg)
245 +{
246 +       u32 val;
247 +
248 +       val = ltq_spi_reg_read(hw, reg);
249 +       val &= ~bits;
250 +       ltq_spi_reg_write(hw, val, reg);
251 +}
252 +
253 +static void ltq_spi_hw_enable(struct ltq_spi *hw)
254 +{
255 +       u32 clc;
256 +
257 +       /* Power-up module */
258 +       clk_enable(hw->spiclk);
259 +
260 +       /*
261 +        * Set clock divider for run mode to 1 to
262 +        * run at same frequency as FPI bus
263 +        */
264 +       clc = (1 << LTQ_SPI_CLC_RMC_SHIFT);
265 +       ltq_spi_reg_write(hw, clc, LTQ_SPI_CLC);
266 +}
267 +
268 +static void ltq_spi_hw_disable(struct ltq_spi *hw)
269 +{
270 +       /* Set clock divider to 0 and set module disable bit */
271 +       ltq_spi_reg_write(hw, LTQ_SPI_CLC_DISS, LTQ_SPI_CLC);
272 +
273 +       /* Power-down module */
274 +       clk_disable(hw->spiclk);
275 +}
276 +
277 +static void ltq_spi_reset_fifos(struct ltq_spi *hw)
278 +{
279 +       u32 val;
280 +
281 +       /*
282 +        * Enable and flush FIFOs. Set interrupt trigger level to
283 +        * half of FIFO count implemented in hardware.
284 +        */
285 +       if (hw->txfs > 1) {
286 +               val = hw->txfs << (LTQ_SPI_TXFCON_TXFITL_SHIFT - 1);
287 +               val |= LTQ_SPI_TXFCON_TXFEN | LTQ_SPI_TXFCON_TXFLU;
288 +               ltq_spi_reg_write(hw, val, LTQ_SPI_TXFCON);
289 +       }
290 +
291 +       if (hw->rxfs > 1) {
292 +               val = hw->rxfs << (LTQ_SPI_RXFCON_RXFITL_SHIFT - 1);
293 +               val |= LTQ_SPI_RXFCON_RXFEN | LTQ_SPI_RXFCON_RXFLU;
294 +               ltq_spi_reg_write(hw, val, LTQ_SPI_RXFCON);
295 +       }
296 +}
297 +
298 +static inline int ltq_spi_wait_ready(struct ltq_spi *hw)
299 +{
300 +       u32 stat;
301 +       unsigned long timeout;
302 +
303 +       timeout = jiffies + msecs_to_jiffies(200);
304 +
305 +       do {
306 +               stat = ltq_spi_reg_read(hw, LTQ_SPI_STAT);
307 +               if (!(stat & LTQ_SPI_STAT_BSY))
308 +                       return 0;
309 +
310 +               cond_resched();
311 +       } while (!time_after_eq(jiffies, timeout));
312 +
313 +       dev_err(hw->dev, "SPI wait ready timed out stat: %x\n", stat);
314 +
315 +       return -ETIMEDOUT;
316 +}
317 +
318 +static void ltq_spi_config_mode_set(struct ltq_spi *hw)
319 +{
320 +       if (hw->cfg_mode)
321 +               return;
322 +
323 +       /*
324 +        * Putting the SPI module in config mode is only safe if no
325 +        * transfer is in progress as indicated by busy flag STATE.BSY.
326 +        */
327 +       if (ltq_spi_wait_ready(hw)) {
328 +               ltq_spi_reset_fifos(hw);
329 +               hw->status = -ETIMEDOUT;
330 +       }
331 +       ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_CLREN, LTQ_SPI_WHBSTATE);
332 +
333 +       hw->cfg_mode = 1;
334 +}
335 +
336 +static void ltq_spi_run_mode_set(struct ltq_spi *hw)
337 +{
338 +       if (!hw->cfg_mode)
339 +               return;
340 +
341 +       ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_SETEN, LTQ_SPI_WHBSTATE);
342 +
343 +       hw->cfg_mode = 0;
344 +}
345 +
346 +static u32 ltq_spi_tx_word_u8(struct ltq_spi *hw)
347 +{
348 +       const u8 *tx = hw->tx;
349 +       u32 data = *tx++;
350 +
351 +       hw->tx_cnt++;
352 +       hw->tx++;
353 +
354 +       return data;
355 +}
356 +
357 +static u32 ltq_spi_tx_word_u16(struct ltq_spi *hw)
358 +{
359 +       const u16 *tx = (u16 *) hw->tx;
360 +       u32 data = *tx++;
361 +
362 +       hw->tx_cnt += 2;
363 +       hw->tx += 2;
364 +
365 +       return data;
366 +}
367 +
368 +static u32 ltq_spi_tx_word_u32(struct ltq_spi *hw)
369 +{
370 +       const u32 *tx = (u32 *) hw->tx;
371 +       u32 data = *tx++;
372 +
373 +       hw->tx_cnt += 4;
374 +       hw->tx += 4;
375 +
376 +       return data;
377 +}
378 +
379 +static void ltq_spi_bits_per_word_set(struct spi_device *spi)
380 +{
381 +       struct ltq_spi *hw = ltq_spi_to_hw(spi);
382 +       u32 bm;
383 +       u8 bits_per_word = spi->bits_per_word;
384 +
385 +       /*
386 +        * Use either default value of SPI device or value
387 +        * from current transfer.
388 +        */
389 +       if (hw->curr_transfer && hw->curr_transfer->bits_per_word)
390 +               bits_per_word = hw->curr_transfer->bits_per_word;
391 +
392 +       if (bits_per_word <= 8)
393 +               hw->get_tx = ltq_spi_tx_word_u8;
394 +       else if (bits_per_word <= 16)
395 +               hw->get_tx = ltq_spi_tx_word_u16;
396 +       else if (bits_per_word <= 32)
397 +               hw->get_tx = ltq_spi_tx_word_u32;
398 +
399 +       /* CON.BM value = bits_per_word - 1 */
400 +       bm = (bits_per_word - 1) << LTQ_SPI_CON_BM_SHIFT;
401 +
402 +       ltq_spi_reg_clearbit(hw, LTQ_SPI_CON_BM_MASK <<
403 +                            LTQ_SPI_CON_BM_SHIFT, LTQ_SPI_CON);
404 +       ltq_spi_reg_setbit(hw, bm, LTQ_SPI_CON);
405 +}
406 +
407 +static void ltq_spi_speed_set(struct spi_device *spi)
408 +{
409 +       struct ltq_spi *hw = ltq_spi_to_hw(spi);
410 +       u32 br, max_speed_hz, spi_clk;
411 +       u32 speed_hz = spi->max_speed_hz;
412 +
413 +       /*
414 +        * Use either default value of SPI device or value
415 +        * from current transfer.
416 +        */
417 +       if (hw->curr_transfer && hw->curr_transfer->speed_hz)
418 +               speed_hz = hw->curr_transfer->speed_hz;
419 +
420 +       /*
421 +        * SPI module clock is derived from FPI bus clock dependent on
422 +        * divider value in CLC.RMS which is always set to 1.
423 +        */
424 +       spi_clk = clk_get_rate(hw->fpiclk);
425 +
426 +       /*
427 +        * Maximum SPI clock frequency in master mode is half of
428 +        * SPI module clock frequency. Maximum reload value of
429 +        * baudrate generator BR is 2^16.
430 +        */
431 +       max_speed_hz = spi_clk / 2;
432 +       if (speed_hz >= max_speed_hz)
433 +               br = 0;
434 +       else
435 +               br = (max_speed_hz / speed_hz) - 1;
436 +
437 +       if (br > 0xFFFF)
438 +               br = 0xFFFF;
439 +
440 +       ltq_spi_reg_write(hw, br, LTQ_SPI_BRT);
441 +}
442 +
443 +static void ltq_spi_clockmode_set(struct spi_device *spi)
444 +{
445 +       struct ltq_spi *hw = ltq_spi_to_hw(spi);
446 +       u32 con;
447 +
448 +       con = ltq_spi_reg_read(hw, LTQ_SPI_CON);
449 +
450 +       /*
451 +        * SPI mode mapping in CON register:
452 +        * Mode CPOL CPHA CON.PO CON.PH
453 +        *  0    0    0      0      1
454 +        *  1    0    1      0      0
455 +        *  2    1    0      1      1
456 +        *  3    1    1      1      0
457 +        */
458 +       if (spi->mode & SPI_CPHA)
459 +               con &= ~LTQ_SPI_CON_PH;
460 +       else
461 +               con |= LTQ_SPI_CON_PH;
462 +
463 +       if (spi->mode & SPI_CPOL)
464 +               con |= LTQ_SPI_CON_PO;
465 +       else
466 +               con &= ~LTQ_SPI_CON_PO;
467 +
468 +       /* Set heading control */
469 +       if (spi->mode & SPI_LSB_FIRST)
470 +               con &= ~LTQ_SPI_CON_HB;
471 +       else
472 +               con |= LTQ_SPI_CON_HB;
473 +
474 +       ltq_spi_reg_write(hw, con, LTQ_SPI_CON);
475 +}
476 +
477 +static void ltq_spi_xmit_set(struct ltq_spi *hw, struct spi_transfer *t)
478 +{
479 +       u32 con;
480 +
481 +       con = ltq_spi_reg_read(hw, LTQ_SPI_CON);
482 +
483 +       if (t) {
484 +               if (t->tx_buf && t->rx_buf) {
485 +                       con &= ~(LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF);
486 +               } else if (t->rx_buf) {
487 +                       con &= ~LTQ_SPI_CON_RXOFF;
488 +                       con |= LTQ_SPI_CON_TXOFF;
489 +               } else if (t->tx_buf) {
490 +                       con &= ~LTQ_SPI_CON_TXOFF;
491 +                       con |= LTQ_SPI_CON_RXOFF;
492 +               }
493 +       } else
494 +               con |= (LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF);
495 +
496 +       ltq_spi_reg_write(hw, con, LTQ_SPI_CON);
497 +}
498 +
499 +static void ltq_spi_internal_cs_activate(struct spi_device *spi)
500 +{
501 +       struct ltq_spi *hw = ltq_spi_to_hw(spi);
502 +       u32 fgpo;
503 +
504 +       fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_CLROUTN_SHIFT));
505 +       ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
506 +}
507 +
508 +static void ltq_spi_internal_cs_deactivate(struct spi_device *spi)
509 +{
510 +       struct ltq_spi *hw = ltq_spi_to_hw(spi);
511 +       u32 fgpo;
512 +
513 +       fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_SETOUTN_SHIFT));
514 +       ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
515 +}
516 +
517 +static void ltq_spi_chipselect(struct spi_device *spi, int cs)
518 +{
519 +       struct ltq_spi *hw = ltq_spi_to_hw(spi);
520 +
521 +       switch (cs) {
522 +       case BITBANG_CS_ACTIVE:
523 +               ltq_spi_bits_per_word_set(spi);
524 +               ltq_spi_speed_set(spi);
525 +               ltq_spi_clockmode_set(spi);
526 +               ltq_spi_run_mode_set(hw);
527 +               ltq_spi_internal_cs_activate(spi);
528 +               break;
529 +
530 +       case BITBANG_CS_INACTIVE:
531 +               ltq_spi_internal_cs_deactivate(spi);
532 +               ltq_spi_config_mode_set(hw);
533 +               break;
534 +       }
535 +}
536 +
537 +static int ltq_spi_setup_transfer(struct spi_device *spi,
538 +                                 struct spi_transfer *t)
539 +{
540 +       struct ltq_spi *hw = ltq_spi_to_hw(spi);
541 +       u8 bits_per_word = spi->bits_per_word;
542 +
543 +       hw->curr_transfer = t;
544 +
545 +       if (t && t->bits_per_word)
546 +               bits_per_word = t->bits_per_word;
547 +
548 +       if (bits_per_word > 32)
549 +               return -EINVAL;
550 +
551 +       ltq_spi_config_mode_set(hw);
552 +
553 +       return 0;
554 +}
555 +
556 +static int ltq_spi_setup(struct spi_device *spi)
557 +{
558 +       struct ltq_spi *hw = ltq_spi_to_hw(spi);
559 +       u32 gpocon, fgpo;
560 +
561 +       /* Set default word length to 8 if not set */
562 +       if (!spi->bits_per_word)
563 +               spi->bits_per_word = 8;
564 +
565 +       if (spi->bits_per_word > 32)
566 +               return -EINVAL;
567 +
568 +       /*
569 +        * Up to six GPIOs can be connected to the SPI module
570 +        * via GPIO alternate function to control the chip select lines.
571 +        */
572 +       gpocon = (1 << (spi->chip_select +
573 +                       LTQ_SPI_GPOCON_ISCSBN_SHIFT));
574 +
575 +       if (spi->mode & SPI_CS_HIGH)
576 +               gpocon |= (1 << spi->chip_select);
577 +
578 +       fgpo = (1 << (spi->chip_select + LTQ_SPI_FGPO_SETOUTN_SHIFT));
579 +
580 +       ltq_spi_reg_setbit(hw, gpocon, LTQ_SPI_GPOCON);
581 +       ltq_spi_reg_setbit(hw, fgpo, LTQ_SPI_FGPO);
582 +
583 +       return 0;
584 +}
585 +
586 +static void ltq_spi_cleanup(struct spi_device *spi)
587 +{
588 +
589 +}
590 +
591 +static void ltq_spi_txfifo_write(struct ltq_spi *hw)
592 +{
593 +       u32 fstat, data;
594 +       u16 fifo_space;
595 +
596 +       /* Determine how much FIFOs are free for TX data */
597 +       fstat = ltq_spi_reg_read(hw, LTQ_SPI_FSTAT);
598 +       fifo_space = hw->txfs - ((fstat >> LTQ_SPI_FSTAT_TXFFL_SHIFT) &
599 +                                       LTQ_SPI_FSTAT_TXFFL_MASK);
600 +
601 +       if (!fifo_space)
602 +               return;
603 +
604 +       while (hw->tx_cnt < hw->len && fifo_space) {
605 +               data = hw->get_tx(hw);
606 +               ltq_spi_reg_write(hw, data, LTQ_SPI_TB);
607 +               fifo_space--;
608 +       }
609 +}
610 +
611 +static void ltq_spi_rxfifo_read(struct ltq_spi *hw)
612 +{
613 +       u32 fstat, data, *rx32;
614 +       u16 fifo_fill;
615 +       u8 rxbv, shift, *rx8;
616 +
617 +       /* Determine how much FIFOs are filled with RX data */
618 +       fstat = ltq_spi_reg_read(hw, LTQ_SPI_FSTAT);
619 +       fifo_fill = ((fstat >> LTQ_SPI_FSTAT_RXFFL_SHIFT)
620 +                       & LTQ_SPI_FSTAT_RXFFL_MASK);
621 +
622 +       if (!fifo_fill)
623 +               return;
624 +
625 +       /*
626 +        * The 32 bit FIFO is always used completely independent from the
627 +        * bits_per_word value. Thus four bytes have to be read at once
628 +        * per FIFO.
629 +        */
630 +       rx32 = (u32 *) hw->rx;
631 +       while (hw->len - hw->rx_cnt >= 4 && fifo_fill) {
632 +               *rx32++ = ltq_spi_reg_read(hw, LTQ_SPI_RB);
633 +               hw->rx_cnt += 4;
634 +               hw->rx += 4;
635 +               fifo_fill--;
636 +       }
637 +
638 +       /*
639 +        * If there are remaining bytes, read byte count from STAT.RXBV
640 +        * register and read the data byte-wise.
641 +        */
642 +       while (fifo_fill && hw->rx_cnt < hw->len) {
643 +               rxbv = (ltq_spi_reg_read(hw, LTQ_SPI_STAT) >>
644 +                       LTQ_SPI_STAT_RXBV_SHIFT) & LTQ_SPI_STAT_RXBV_MASK;
645 +               data = ltq_spi_reg_read(hw, LTQ_SPI_RB);
646 +
647 +               shift = (rxbv - 1) * 8;
648 +               rx8 = hw->rx;
649 +
650 +               while (rxbv) {
651 +                       *rx8++ = (data >> shift) & 0xFF;
652 +                       rxbv--;
653 +                       shift -= 8;
654 +                       hw->rx_cnt++;
655 +                       hw->rx++;
656 +               }
657 +
658 +               fifo_fill--;
659 +       }
660 +}
661 +
662 +static void ltq_spi_rxreq_set(struct ltq_spi *hw)
663 +{
664 +       u32 rxreq, rxreq_max, rxtodo;
665 +
666 +       rxtodo = ltq_spi_reg_read(hw, LTQ_SPI_RXCNT) & LTQ_SPI_RXCNT_TODO_MASK;
667 +
668 +       /*
669 +        * In RX-only mode the serial clock is activated only after writing
670 +        * the expected amount of RX bytes into RXREQ register.
671 +        * To avoid receive overflows at high clocks it is better to request
672 +        * only the amount of bytes that fits into all FIFOs. This value
673 +        * depends on the FIFO size implemented in hardware.
674 +        */
675 +       rxreq = hw->len - hw->rx_cnt;
676 +       rxreq_max = hw->rxfs << 2;
677 +       rxreq = min(rxreq_max, rxreq);
678 +
679 +       if (!rxtodo && rxreq)
680 +               ltq_spi_reg_write(hw, rxreq, LTQ_SPI_RXREQ);
681 +}
682 +
683 +static inline void ltq_spi_complete(struct ltq_spi *hw)
684 +{
685 +       complete(&hw->done);
686 +}
687 +
688 +irqreturn_t ltq_spi_tx_irq(int irq, void *data)
689 +{
690 +       struct ltq_spi *hw = data;
691 +       unsigned long flags;
692 +       int completed = 0;
693 +
694 +       spin_lock_irqsave(&hw->lock, flags);
695 +
696 +       if (hw->tx_cnt < hw->len)
697 +               ltq_spi_txfifo_write(hw);
698 +
699 +       if (hw->tx_cnt == hw->len)
700 +               completed = 1;
701 +
702 +       spin_unlock_irqrestore(&hw->lock, flags);
703 +
704 +       if (completed)
705 +               ltq_spi_complete(hw);
706 +
707 +       return IRQ_HANDLED;
708 +}
709 +
710 +irqreturn_t ltq_spi_rx_irq(int irq, void *data)
711 +{
712 +       struct ltq_spi *hw = data;
713 +       unsigned long flags;
714 +       int completed = 0;
715 +
716 +       spin_lock_irqsave(&hw->lock, flags);
717 +
718 +       if (hw->rx_cnt < hw->len) {
719 +               ltq_spi_rxfifo_read(hw);
720 +
721 +               if (hw->tx && hw->tx_cnt < hw->len)
722 +                       ltq_spi_txfifo_write(hw);
723 +       }
724 +
725 +       if (hw->rx_cnt == hw->len)
726 +               completed = 1;
727 +       else if (!hw->tx)
728 +               ltq_spi_rxreq_set(hw);
729 +
730 +       spin_unlock_irqrestore(&hw->lock, flags);
731 +
732 +       if (completed)
733 +               ltq_spi_complete(hw);
734 +
735 +       return IRQ_HANDLED;
736 +}
737 +
738 +irqreturn_t ltq_spi_err_irq(int irq, void *data)
739 +{
740 +       struct ltq_spi *hw = data;
741 +       unsigned long flags;
742 +
743 +       spin_lock_irqsave(&hw->lock, flags);
744 +
745 +       /* Disable all interrupts */
746 +       ltq_spi_reg_clearbit(hw, LTQ_SPI_IRNEN_ALL, LTQ_SPI_IRNEN);
747 +
748 +       /* Clear all error flags */
749 +       ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
750 +
751 +       /* Flush FIFOs */
752 +       ltq_spi_reg_setbit(hw, LTQ_SPI_RXFCON_RXFLU, LTQ_SPI_RXFCON);
753 +       ltq_spi_reg_setbit(hw, LTQ_SPI_TXFCON_TXFLU, LTQ_SPI_TXFCON);
754 +
755 +       hw->status = -EIO;
756 +       spin_unlock_irqrestore(&hw->lock, flags);
757 +
758 +       ltq_spi_complete(hw);
759 +
760 +       return IRQ_HANDLED;
761 +}
762 +
763 +static int ltq_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
764 +{
765 +       struct ltq_spi *hw = ltq_spi_to_hw(spi);
766 +       u32 irq_flags = 0;
767 +
768 +       hw->tx = t->tx_buf;
769 +       hw->rx = t->rx_buf;
770 +       hw->len = t->len;
771 +       hw->tx_cnt = 0;
772 +       hw->rx_cnt = 0;
773 +       hw->status = 0;
774 +       INIT_COMPLETION(hw->done);
775 +
776 +       ltq_spi_xmit_set(hw, t);
777 +
778 +       /* Enable error interrupts */
779 +       ltq_spi_reg_setbit(hw, LTQ_SPI_IRNEN_E, LTQ_SPI_IRNEN);
780 +
781 +       if (hw->tx) {
782 +               /* Initially fill TX FIFO with as much data as possible */
783 +               ltq_spi_txfifo_write(hw);
784 +               irq_flags |= LTQ_SPI_IRNEN_T;
785 +
786 +               /* Always enable RX interrupt in Full Duplex mode */
787 +               if (hw->rx)
788 +                       irq_flags |= LTQ_SPI_IRNEN_R;
789 +       } else if (hw->rx) {
790 +               /* Start RX clock */
791 +               ltq_spi_rxreq_set(hw);
792 +
793 +               /* Enable RX interrupt to receive data from RX FIFOs */
794 +               irq_flags |= LTQ_SPI_IRNEN_R;
795 +       }
796 +
797 +       /* Enable TX or RX interrupts */
798 +       ltq_spi_reg_setbit(hw, irq_flags, LTQ_SPI_IRNEN);
799 +       wait_for_completion_interruptible(&hw->done);
800 +
801 +       /* Disable all interrupts */
802 +       ltq_spi_reg_clearbit(hw, LTQ_SPI_IRNEN_ALL, LTQ_SPI_IRNEN);
803 +
804 +       /*
805 +        * Return length of current transfer for bitbang utility code if
806 +        * no errors occured during transmission.
807 +        */
808 +       if (!hw->status)
809 +               hw->status = hw->len;
810 +
811 +       return hw->status;
812 +}
813 +
814 +static const struct ltq_spi_irq_map {
815 +       char *name;
816 +       irq_handler_t handler;
817 +} ltq_spi_irqs[] = {
818 +       { "spi_rx", ltq_spi_rx_irq },
819 +       { "spi_tx", ltq_spi_tx_irq },
820 +       { "spi_err", ltq_spi_err_irq },
821 +};
822 +
823 +static int __devinit ltq_spi_probe(struct platform_device *pdev)
824 +{
825 +       struct resource irqres[3];
826 +       struct spi_master *master;
827 +       struct resource *r;
828 +       struct ltq_spi *hw;
829 +       int ret, i;
830 +       u32 data, id;
831 +
832 +       if (of_irq_to_resource_table(pdev->dev.of_node, irqres, 3) != 3) {
833 +               dev_err(&pdev->dev, "IRQ settings missing in device tree\n");
834 +               return -EINVAL;
835 +       }
836 +
837 +       master = spi_alloc_master(&pdev->dev, sizeof(struct ltq_spi));
838 +       if (!master) {
839 +               dev_err(&pdev->dev, "spi_alloc_master\n");
840 +               ret = -ENOMEM;
841 +               goto err;
842 +       }
843 +
844 +       hw = spi_master_get_devdata(master);
845 +
846 +       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
847 +       if (r == NULL) {
848 +               dev_err(&pdev->dev, "platform_get_resource\n");
849 +               ret = -ENOENT;
850 +               goto err_master;
851 +       }
852 +
853 +       r = devm_request_mem_region(&pdev->dev, r->start, resource_size(r),
854 +                       pdev->name);
855 +       if (!r) {
856 +               dev_err(&pdev->dev, "failed to request memory region\n");
857 +               ret = -ENXIO;
858 +               goto err_master;
859 +       }
860 +
861 +       hw->base = devm_ioremap_nocache(&pdev->dev, r->start, resource_size(r));
862 +       if (!hw->base) {
863 +               dev_err(&pdev->dev, "failed to remap memory region\n");
864 +               ret = -ENXIO;
865 +               goto err_master;
866 +       }
867 +
868 +       memset(hw->irq, 0, sizeof(hw->irq));
869 +       for (i = 0; i < ARRAY_SIZE(ltq_spi_irqs); i++) {
870 +               hw->irq[i] = irqres[i].start;
871 +               ret = request_irq(hw->irq[i], ltq_spi_irqs[i].handler,
872 +                                 0, ltq_spi_irqs[i].name, hw);
873 +               if (ret) {
874 +                       dev_err(&pdev->dev, "failed to request %s irq (%d)\n",
875 +                                       ltq_spi_irqs[i].name, hw->irq[i]);
876 +                       goto err_irq;
877 +               }
878 +       }
879 +
880 +       hw->fpiclk = clk_get_fpi();
881 +       if (IS_ERR(hw->fpiclk)) {
882 +               dev_err(&pdev->dev, "failed to get fpi clock\n");
883 +               ret = PTR_ERR(hw->fpiclk);
884 +               goto err_clk;
885 +       }
886 +
887 +       hw->spiclk = clk_get(&pdev->dev, NULL);
888 +       if (IS_ERR(hw->spiclk)) {
889 +               dev_err(&pdev->dev, "failed to get spi clock gate\n");
890 +               ret = PTR_ERR(hw->spiclk);
891 +               goto err_clk;
892 +       }
893 +
894 +       hw->bitbang.master = spi_master_get(master);
895 +       hw->bitbang.chipselect = ltq_spi_chipselect;
896 +       hw->bitbang.setup_transfer = ltq_spi_setup_transfer;
897 +       hw->bitbang.txrx_bufs = ltq_spi_txrx_bufs;
898 +
899 +       if (of_machine_is_compatible("lantiq,ase"))
900 +               master->num_chipselect = 3;
901 +       else
902 +               master->num_chipselect = 6;
903 +       master->bus_num = pdev->id;
904 +       master->setup = ltq_spi_setup;
905 +       master->cleanup = ltq_spi_cleanup;
906 +       master->dev.of_node = pdev->dev.of_node;
907 +
908 +       hw->dev = &pdev->dev;
909 +       init_completion(&hw->done);
910 +       spin_lock_init(&hw->lock);
911 +
912 +       ltq_spi_hw_enable(hw);
913 +
914 +       /* Read module capabilities */
915 +       id = ltq_spi_reg_read(hw, LTQ_SPI_ID);
916 +       hw->txfs = (id >> LTQ_SPI_ID_TXFS_SHIFT) & LTQ_SPI_ID_TXFS_MASK;
917 +       hw->rxfs = (id >> LTQ_SPI_ID_TXFS_SHIFT) & LTQ_SPI_ID_TXFS_MASK;
918 +       hw->dma_support = (id & LTQ_SPI_ID_CFG) ? 1 : 0;
919 +
920 +       ltq_spi_config_mode_set(hw);
921 +
922 +       /* Enable error checking, disable TX/RX, set idle value high */
923 +       data = LTQ_SPI_CON_RUEN | LTQ_SPI_CON_AEN |
924 +           LTQ_SPI_CON_TEN | LTQ_SPI_CON_REN |
925 +           LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF | LTQ_SPI_CON_IDLE;
926 +       ltq_spi_reg_write(hw, data, LTQ_SPI_CON);
927 +
928 +       /* Enable master mode and clear error flags */
929 +       ltq_spi_reg_write(hw, LTQ_SPI_WHBSTATE_SETMS |
930 +                         LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
931 +
932 +       /* Reset GPIO/CS registers */
933 +       ltq_spi_reg_write(hw, 0x0, LTQ_SPI_GPOCON);
934 +       ltq_spi_reg_write(hw, 0xFF00, LTQ_SPI_FGPO);
935 +
936 +       /* Enable and flush FIFOs */
937 +       ltq_spi_reset_fifos(hw);
938 +
939 +       ret = spi_bitbang_start(&hw->bitbang);
940 +       if (ret) {
941 +               dev_err(&pdev->dev, "spi_bitbang_start failed\n");
942 +               goto err_bitbang;
943 +       }
944 +
945 +       platform_set_drvdata(pdev, hw);
946 +
947 +       pr_info("Lantiq SoC SPI controller rev %u (TXFS %u, RXFS %u, DMA %u)\n",
948 +               id & LTQ_SPI_ID_REV_MASK, hw->txfs, hw->rxfs, hw->dma_support);
949 +
950 +       return 0;
951 +
952 +err_bitbang:
953 +       ltq_spi_hw_disable(hw);
954 +
955 +err_clk:
956 +       if (hw->fpiclk)
957 +               clk_put(hw->fpiclk);
958 +       if (hw->spiclk)
959 +               clk_put(hw->spiclk);
960 +
961 +err_irq:
962 +       clk_put(hw->fpiclk);
963 +
964 +       for (; i > 0; i--)
965 +               free_irq(hw->irq[i], hw);
966 +
967 +err_master:
968 +       spi_master_put(master);
969 +
970 +err:
971 +       return ret;
972 +}
973 +
974 +static int __devexit ltq_spi_remove(struct platform_device *pdev)
975 +{
976 +       struct ltq_spi *hw = platform_get_drvdata(pdev);
977 +       int ret, i;
978 +
979 +       ret = spi_bitbang_stop(&hw->bitbang);
980 +       if (ret)
981 +               return ret;
982 +
983 +       platform_set_drvdata(pdev, NULL);
984 +
985 +       ltq_spi_config_mode_set(hw);
986 +       ltq_spi_hw_disable(hw);
987 +
988 +       for (i = 0; i < ARRAY_SIZE(hw->irq); i++)
989 +               if (0 < hw->irq[i])
990 +                       free_irq(hw->irq[i], hw);
991 +
992 +       if (hw->fpiclk)
993 +               clk_put(hw->fpiclk);
994 +       if (hw->spiclk)
995 +               clk_put(hw->spiclk);
996 +
997 +       spi_master_put(hw->bitbang.master);
998 +
999 +       return 0;
1000 +}
1001 +
1002 +static const struct of_device_id ltq_spi_match[] = {
1003 +       { .compatible = "lantiq,spi-xway" },
1004 +       {},
1005 +};
1006 +MODULE_DEVICE_TABLE(of, ltq_spi_match);
1007 +
1008 +static struct platform_driver ltq_spi_driver = {
1009 +       .probe = ltq_spi_probe,
1010 +       .remove = __devexit_p(ltq_spi_remove),
1011 +       .driver = {
1012 +               .name = "spi-xway",
1013 +               .owner = THIS_MODULE,
1014 +               .of_match_table = ltq_spi_match,
1015 +       },
1016 +};
1017 +
1018 +module_platform_driver(ltq_spi_driver);
1019 +
1020 +MODULE_DESCRIPTION("Lantiq SoC SPI controller driver");
1021 +MODULE_AUTHOR("Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>");
1022 +MODULE_LICENSE("GPL");
1023 +MODULE_ALIAS("platform:spi-xway");