[brcm63xx] Remove patch 140-new_bcm96348gw_leds because it is really the GW6200 leds...
[15.05/openwrt.git] / target / linux / brcm63xx / patches-2.6.35 / 240-spi.patch
1 --- a/arch/mips/bcm63xx/cpu.c
2 +++ b/arch/mips/bcm63xx/cpu.c
3 @@ -56,6 +56,7 @@ static const unsigned long bcm96338_regs
4  
5  static const int bcm96338_irqs[] = {
6         [IRQ_TIMER]             = BCM_6338_TIMER_IRQ,
7 +       [IRQ_SPI]               = BCM_6338_SPI_IRQ,
8         [IRQ_UART0]             = BCM_6338_UART0_IRQ,
9         [IRQ_DSL]               = BCM_6338_DSL_IRQ,
10         [IRQ_ENET0]             = BCM_6338_ENET0_IRQ,
11 @@ -130,6 +131,7 @@ static const unsigned long bcm96348_regs
12  
13  static const int bcm96348_irqs[] = {
14         [IRQ_TIMER]             = BCM_6348_TIMER_IRQ,
15 +       [IRQ_SPI]               = BCM_6348_SPI_IRQ,
16         [IRQ_UART0]             = BCM_6348_UART0_IRQ,
17         [IRQ_DSL]               = BCM_6348_DSL_IRQ,
18         [IRQ_ENET0]             = BCM_6348_ENET0_IRQ,
19 @@ -173,6 +175,7 @@ static const unsigned long bcm96358_regs
20  
21  static const int bcm96358_irqs[] = {
22         [IRQ_TIMER]             = BCM_6358_TIMER_IRQ,
23 +       [IRQ_SPI]               = BCM_6358_SPI_IRQ,
24         [IRQ_UART0]             = BCM_6358_UART0_IRQ,
25         [IRQ_UART1]             = BCM_6358_UART1_IRQ,
26         [IRQ_DSL]               = BCM_6358_DSL_IRQ,
27 --- /dev/null
28 +++ b/arch/mips/bcm63xx/dev-spi.c
29 @@ -0,0 +1,127 @@
30 +/*
31 + * This file is subject to the terms and conditions of the GNU General Public
32 + * License.  See the file "COPYING" in the main directory of this archive
33 + * for more details.
34 + *
35 + * Copyright (C) 2009 Florian Fainelli <florian@openwrt.org>
36 + */
37 +
38 +#include <linux/init.h>
39 +#include <linux/kernel.h>
40 +#include <linux/platform_device.h>
41 +
42 +#include <bcm63xx_cpu.h>
43 +#include <bcm63xx_dev_spi.h>
44 +#include <bcm63xx_regs.h>
45 +
46 +#ifdef BCMCPU_RUNTIME_DETECT
47 +/*
48 + * register offsets
49 + */
50 +static const unsigned long bcm96338_regs_spi[] = {
51 +       [SPI_CMD]               = SPI_BCM_6338_SPI_CMD,
52 +       [SPI_INT_STATUS]        = SPI_BCM_6338_SPI_INT_STATUS,
53 +       [SPI_INT_MASK_ST]       = SPI_BCM_6338_SPI_MASK_INT_ST,
54 +       [SPI_INT_MASK]          = SPI_BCM_6338_SPI_INT_MASK,
55 +       [SPI_ST]                = SPI_BCM_6338_SPI_ST,
56 +       [SPI_CLK_CFG]           = SPI_BCM_6338_SPI_CLK_CFG,
57 +       [SPI_FILL_BYTE]         = SPI_BCM_6338_SPI_FILL_BYTE,
58 +       [SPI_MSG_TAIL]          = SPI_BCM_6338_SPI_MSG_TAIL,
59 +       [SPI_RX_TAIL]           = SPI_BCM_6338_SPI_RX_TAIL,
60 +       [SPI_MSG_CTL]           = SPI_BCM_6338_SPI_MSG_CTL,
61 +       [SPI_MSG_DATA]          = SPI_BCM_6338_SPI_MSG_DATA,
62 +       [SPI_RX_DATA]           = SPI_BCM_6338_SPI_RX_DATA,
63 +};
64 +
65 +static const unsigned long bcm96348_regs_spi[] = {
66 +       [SPI_CMD]               = SPI_BCM_6348_SPI_CMD,
67 +       [SPI_INT_STATUS]        = SPI_BCM_6348_SPI_INT_STATUS,
68 +       [SPI_INT_MASK_ST]       = SPI_BCM_6348_SPI_MASK_INT_ST,
69 +       [SPI_INT_MASK]          = SPI_BCM_6348_SPI_INT_MASK,
70 +       [SPI_ST]                = SPI_BCM_6348_SPI_ST,
71 +       [SPI_CLK_CFG]           = SPI_BCM_6348_SPI_CLK_CFG,
72 +       [SPI_FILL_BYTE]         = SPI_BCM_6348_SPI_FILL_BYTE,
73 +       [SPI_MSG_TAIL]          = SPI_BCM_6348_SPI_MSG_TAIL,
74 +       [SPI_RX_TAIL]           = SPI_BCM_6348_SPI_RX_TAIL,
75 +       [SPI_MSG_CTL]           = SPI_BCM_6348_SPI_MSG_CTL,
76 +       [SPI_MSG_DATA]          = SPI_BCM_6348_SPI_MSG_DATA,
77 +       [SPI_RX_DATA]           = SPI_BCM_6348_SPI_RX_DATA,
78 +};
79 +
80 +static const unsigned long bcm96358_regs_spi[] = {
81 +       [SPI_CMD]               = SPI_BCM_6358_SPI_CMD,
82 +       [SPI_INT_STATUS]        = SPI_BCM_6358_SPI_INT_STATUS,
83 +       [SPI_INT_MASK_ST]       = SPI_BCM_6358_SPI_MASK_INT_ST,
84 +       [SPI_INT_MASK]          = SPI_BCM_6358_SPI_INT_MASK,
85 +       [SPI_ST]                = SPI_BCM_6358_SPI_STATUS,
86 +       [SPI_CLK_CFG]           = SPI_BCM_6358_SPI_CLK_CFG,
87 +       [SPI_FILL_BYTE]         = SPI_BCM_6358_SPI_FILL_BYTE,
88 +       [SPI_MSG_TAIL]          = SPI_BCM_6358_SPI_MSG_TAIL,
89 +       [SPI_RX_TAIL]           = SPI_BCM_6358_SPI_RX_TAIL,
90 +       [SPI_MSG_CTL]           = SPI_BCM_6358_MSG_CTL,
91 +       [SPI_MSG_DATA]          = SPI_BCM_6358_SPI_MSG_DATA,
92 +       [SPI_RX_DATA]           = SPI_BCM_6358_SPI_RX_DATA,
93 +};
94 +
95 +const unsigned long *bcm63xx_regs_spi;
96 +EXPORT_SYMBOL(bcm63xx_regs_spi);
97 +
98 +static __init void bcm63xx_spi_regs_init(void)
99 +{
100 +       if (BCMCPU_IS_6338())
101 +               bcm63xx_regs_spi = bcm96338_regs_spi;
102 +       if (BCMCPU_IS_6348())
103 +               bcm63xx_regs_spi = bcm96348_regs_spi;
104 +       if (BCMCPU_IS_6358())
105 +               bcm63xx_regs_spi = bcm96358_regs_spi;
106 +}
107 +#else
108 +static __init void bcm63xx_spi_regs_init(void) { }
109 +#endif
110 +
111 +static struct resource spi_resources[] = {
112 +       {
113 +               .start          = -1, /* filled at runtime */
114 +               .end            = -1, /* filled at runtime */
115 +               .flags          = IORESOURCE_MEM,
116 +       },
117 +       {
118 +               .start          = -1, /* filled at runtime */
119 +               .flags          = IORESOURCE_IRQ,
120 +       },
121 +};
122 +
123 +static struct bcm63xx_spi_pdata spi_pdata = {
124 +       .bus_num                = 0,
125 +       .num_chipselect         = 8,
126 +       .speed_hz               = 50000000,     /* Fclk */
127 +};
128 +
129 +static struct platform_device bcm63xx_spi_device = {
130 +       .name           = "bcm63xx-spi",
131 +       .id             = 0,
132 +       .num_resources  = ARRAY_SIZE(spi_resources),
133 +       .resource       = spi_resources,
134 +       .dev            = {
135 +               .platform_data = &spi_pdata,
136 +       },
137 +};
138 +
139 +int __init bcm63xx_spi_register(void)
140 +{
141 +       spi_resources[0].start = bcm63xx_regset_address(RSET_SPI);
142 +       spi_resources[0].end = spi_resources[0].start;
143 +       spi_resources[0].end += RSET_SPI_SIZE - 1;
144 +       spi_resources[1].start = bcm63xx_get_irq_number(IRQ_SPI);
145 +
146 +       /* Fill in platform data */
147 +       if (BCMCPU_IS_6338() || BCMCPU_IS_6348())
148 +               spi_pdata.fifo_size = SPI_BCM_6338_SPI_MSG_DATA_SIZE;
149 +
150 +       if (BCMCPU_IS_6358())
151 +               spi_pdata.fifo_size = SPI_BCM_6358_SPI_MSG_DATA_SIZE;
152 +
153 +       bcm63xx_spi_regs_init();
154 +
155 +       return platform_device_register(&bcm63xx_spi_device);
156 +}
157 --- a/arch/mips/include/asm/mach-bcm63xx/bcm63xx_cpu.h
158 +++ b/arch/mips/include/asm/mach-bcm63xx/bcm63xx_cpu.h
159 @@ -109,6 +109,7 @@ enum bcm63xx_regs_set {
160  #define RSET_WDT_SIZE                  12
161  #define RSET_ENET_SIZE                 2048
162  #define RSET_ENETDMA_SIZE              2048
163 +#define RSET_SPI_SIZE                  256
164  #define RSET_UART_SIZE                 24
165  #define RSET_UDC_SIZE                  256
166  #define RSET_OHCI_SIZE                 256
167 @@ -214,7 +215,7 @@ enum bcm63xx_regs_set {
168  #define BCM_6358_UART0_BASE            (0xfffe0100)
169  #define BCM_6358_UART1_BASE            (0xfffe0120)
170  #define BCM_6358_GPIO_BASE             (0xfffe0080)
171 -#define BCM_6358_SPI_BASE              (0xdeadbeef)
172 +#define BCM_6358_SPI_BASE              (0xfffe0800)
173  #define BCM_6358_UDC0_BASE             (0xfffe0400)
174  #define BCM_6358_OHCI0_BASE            (0xfffe1400)
175  #define BCM_6358_OHCI_PRIV_BASE                (0xdeadbeef)
176 @@ -441,6 +442,7 @@ static inline unsigned long bcm63xx_regs
177   */
178  enum bcm63xx_irq {
179         IRQ_TIMER = 0,
180 +       IRQ_SPI,
181         IRQ_UART0,
182         IRQ_UART1,
183         IRQ_DSL,
184 @@ -507,6 +509,7 @@ enum bcm63xx_irq {
185   * 6348 irqs
186   */
187  #define BCM_6348_TIMER_IRQ             (IRQ_INTERNAL_BASE + 0)
188 +#define BCM_6348_SPI_IRQ               (IRQ_INTERNAL_BASE + 1)
189  #define BCM_6348_UART0_IRQ             (IRQ_INTERNAL_BASE + 2)
190  #define BCM_6348_DSL_IRQ               (IRQ_INTERNAL_BASE + 4)
191  #define BCM_6348_UDC0_IRQ              (IRQ_INTERNAL_BASE + 6)
192 @@ -531,6 +534,7 @@ enum bcm63xx_irq {
193   * 6358 irqs
194   */
195  #define BCM_6358_TIMER_IRQ             (IRQ_INTERNAL_BASE + 0)
196 +#define BCM_6358_SPI_IRQ               (IRQ_INTERNAL_BASE + 1)
197  #define BCM_6358_UART0_IRQ             (IRQ_INTERNAL_BASE + 2)
198  #define BCM_6358_UART1_IRQ             (IRQ_INTERNAL_BASE + 3)
199  #define BCM_6358_OHCI0_IRQ             (IRQ_INTERNAL_BASE + 5)
200 --- a/arch/mips/include/asm/mach-bcm63xx/bcm63xx_regs.h
201 +++ b/arch/mips/include/asm/mach-bcm63xx/bcm63xx_regs.h
202 @@ -769,4 +769,116 @@
203  #define DMIPSPLLCFG_N2_SHIFT           29
204  #define DMIPSPLLCFG_N2_MASK            (0x7 << DMIPSPLLCFG_N2_SHIFT)
205  
206 +/*************************************************************************
207 + * _REG relative to RSET_SPI
208 + *************************************************************************/
209 +
210 +/* BCM 6338 SPI core */
211 +#define SPI_BCM_6338_SPI_CMD           0x00    /* 16-bits register */
212 +#define SPI_BCM_6338_SPI_INT_STATUS    0x02
213 +#define SPI_BCM_6338_SPI_MASK_INT_ST   0x03
214 +#define SPI_BCM_6338_SPI_INT_MASK      0x04
215 +#define SPI_BCM_6338_SPI_ST            0x05
216 +#define SPI_BCM_6338_SPI_CLK_CFG       0x06
217 +#define SPI_BCM_6338_SPI_FILL_BYTE     0x07
218 +#define SPI_BCM_6338_SPI_MSG_TAIL      0x09
219 +#define SPI_BCM_6338_SPI_RX_TAIL       0x0b
220 +#define SPI_BCM_6338_SPI_MSG_CTL       0x40
221 +#define SPI_BCM_6338_SPI_MSG_DATA      0x41
222 +#define SPI_BCM_6338_SPI_MSG_DATA_SIZE 0x3f
223 +#define SPI_BCM_6338_SPI_RX_DATA       0x80
224 +#define SPI_BCM_6338_SPI_RX_DATA_SIZE  0x3f
225 +
226 +/* BCM 6348 SPI core */
227 +#define SPI_BCM_6348_SPI_MASK_INT_ST   0x00
228 +#define SPI_BCM_6348_SPI_INT_STATUS    0x01
229 +#define SPI_BCM_6348_SPI_CMD           0x02    /* 16-bits register */
230 +#define SPI_BCM_6348_SPI_FILL_BYTE     0x04
231 +#define SPI_BCM_6348_SPI_CLK_CFG       0x05
232 +#define SPI_BCM_6348_SPI_ST            0x06
233 +#define SPI_BCM_6348_SPI_INT_MASK      0x07
234 +#define SPI_BCM_6348_SPI_RX_TAIL       0x08
235 +#define SPI_BCM_6348_SPI_MSG_TAIL      0x10
236 +#define SPI_BCM_6348_SPI_MSG_DATA      0x40
237 +#define SPI_BCM_6348_SPI_MSG_CTL       0x42
238 +#define SPI_BCM_6348_SPI_MSG_DATA_SIZE 0x3f
239 +#define SPI_BCM_6348_SPI_RX_DATA       0x80
240 +#define SPI_BCM_6348_SPI_RX_DATA_SIZE  0x3f
241 +
242 +/* BCM 6358 SPI core */
243 +#define SPI_BCM_6358_MSG_CTL           0x00    /* 16-bits register */
244 +
245 +#define SPI_BCM_6358_SPI_MSG_DATA      0x02
246 +#define SPI_BCM_6358_SPI_MSG_DATA_SIZE 0x21e
247 +
248 +#define SPI_BCM_6358_SPI_RX_DATA       0x400
249 +#define SPI_BCM_6358_SPI_RX_DATA_SIZE  0x220
250 +
251 +#define SPI_BCM_6358_SPI_CMD           0x700   /* 16-bits register */
252 +
253 +#define SPI_BCM_6358_SPI_INT_STATUS    0x702
254 +#define SPI_BCM_6358_SPI_MASK_INT_ST   0x703
255 +
256 +#define SPI_BCM_6358_SPI_INT_MASK      0x704
257 +
258 +#define SPI_BCM_6358_SPI_STATUS                0x705
259 +
260 +#define SPI_BCM_6358_SPI_CLK_CFG       0x706
261 +
262 +#define SPI_BCM_6358_SPI_FILL_BYTE     0x707
263 +#define SPI_BCM_6358_SPI_MSG_TAIL      0x709
264 +#define SPI_BCM_6358_SPI_RX_TAIL       0x70B
265 +
266 +/* Shared SPI definitions */
267 +
268 +/* Message configuration */
269 +#define SPI_FD_RW                      0x00
270 +#define SPI_HD_W                       0x01
271 +#define SPI_HD_R                       0x02
272 +#define SPI_BYTE_CNT_SHIFT             0
273 +#define SPI_MSG_TYPE_SHIFT             14
274 +
275 +/* Command */
276 +#define SPI_CMD_NOOP                   0x01
277 +#define SPI_CMD_SOFT_RESET             0x02
278 +#define SPI_CMD_HARD_RESET             0x04
279 +#define SPI_CMD_START_IMMEDIATE                0x08
280 +#define SPI_CMD_COMMAND_SHIFT          0
281 +#define SPI_CMD_COMMAND_MASK           0x000f
282 +#define SPI_CMD_DEVICE_ID_SHIFT                4
283 +#define SPI_CMD_PREPEND_BYTE_CNT_SHIFT 8
284 +#define SPI_CMD_ONE_BYTE_SHIFT         11
285 +#define SPI_CMD_ONE_WIRE_SHIFT         12
286 +#define SPI_DEV_ID_0                   0
287 +#define SPI_DEV_ID_1                   1
288 +#define SPI_DEV_ID_2                   2
289 +#define SPI_DEV_ID_3                   3
290 +
291 +/* Interrupt mask */
292 +#define SPI_INTR_CMD_DONE              0x01
293 +#define SPI_INTR_RX_OVERFLOW           0x02
294 +#define SPI_INTR_TX_UNDERFLOW          0x04
295 +#define SPI_INTR_TX_OVERFLOW           0x08
296 +#define SPI_INTR_RX_UNDERFLOW          0x10
297 +#define SPI_INTR_CLEAR_ALL             0x1f
298 +
299 +/* Status */
300 +#define SPI_RX_EMPTY                   0x02
301 +#define SPI_CMD_BUSY                   0x04
302 +#define SPI_SERIAL_BUSY                        0x08
303 +
304 +/* Clock configuration */
305 +#define SPI_CLK_20MHZ                  0x00
306 +#define SPI_CLK_0_391MHZ               0x01
307 +#define SPI_CLK_0_781MHZ               0x02 /* default */
308 +#define SPI_CLK_1_563MHZ               0x03
309 +#define SPI_CLK_3_125MHZ               0x04
310 +#define SPI_CLK_6_250MHZ               0x05
311 +#define SPI_CLK_12_50MHZ               0x06
312 +#define SPI_CLK_25MHZ                  0x07
313 +#define SPI_CLK_MASK                   0x07
314 +#define SPI_SSOFFTIME_MASK             0x38
315 +#define SPI_SSOFFTIME_SHIFT            3
316 +#define SPI_BYTE_SWAP                  0x80
317 +
318  #endif /* BCM63XX_REGS_H_ */
319 --- /dev/null
320 +++ b/drivers/spi/bcm63xx_spi.c
321 @@ -0,0 +1,479 @@
322 +/*
323 + * Broadcom BCM63xx SPI controller support
324 + *
325 + * Copyright (C) 2009 Florian Fainelli <florian@openwrt.org>
326 + *
327 + * This program is free software; you can redistribute it and/or
328 + * modify it under the terms of the GNU General Public License
329 + * as published by the Free Software Foundation; either version 2
330 + * of the License, or (at your option) any later version.
331 + *
332 + * This program is distributed in the hope that it will be useful,
333 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
334 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
335 + * GNU General Public License for more details.
336 + *
337 + * You should have received a copy of the GNU General Public License
338 + * along with this program; if not, write to the
339 + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
340 + */
341 +
342 +#include <linux/kernel.h>
343 +#include <linux/init.h>
344 +#include <linux/clk.h>
345 +#include <linux/module.h>
346 +#include <linux/platform_device.h>
347 +#include <linux/delay.h>
348 +#include <linux/interrupt.h>
349 +#include <linux/spi/spi.h>
350 +#include <linux/completion.h>
351 +#include <linux/err.h>
352 +
353 +#include <bcm63xx_dev_spi.h>
354 +
355 +#define PFX            KBUILD_MODNAME
356 +#define DRV_VER                "0.1.2"
357 +
358 +struct bcm63xx_spi {
359 +       spinlock_t              lock;
360 +       int                     stopping;
361 +        struct completion      done;
362 +
363 +        void __iomem           *regs;
364 +        int                    irq;
365 +
366 +       /* Platform data */
367 +        u32                    speed_hz;
368 +       unsigned                fifo_size;
369 +
370 +       /* Data buffers */
371 +       const unsigned char     *tx_ptr;
372 +       unsigned char           *rx_ptr;
373 +       int                     remaining_bytes;
374 +
375 +       struct clk              *clk;
376 +       struct platform_device  *pdev;
377 +};
378 +
379 +static int bcm63xx_spi_setup_transfer(struct spi_device *spi,
380 +                                       struct spi_transfer *t)
381 +{
382 +       u8 bits_per_word;
383 +       u8 clk_cfg;
384 +       u32 hz;
385 +       unsigned int div;
386 +
387 +       struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
388 +
389 +       bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
390 +       hz = (t) ? t->speed_hz : spi->max_speed_hz;
391 +       if (bits_per_word != 8) {
392 +               dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
393 +                       __func__, bits_per_word);
394 +               return -EINVAL;
395 +        }
396 +
397 +       if (spi->chip_select > spi->master->num_chipselect) {
398 +               dev_err(&spi->dev, "%s, unsupported slave %d\n",
399 +                       __func__, spi->chip_select);
400 +               return -EINVAL;
401 +       }
402 +
403 +       /* Check clock setting */
404 +       div = (bs->speed_hz / hz);
405 +       switch (div) {
406 +       case 2:
407 +               clk_cfg = SPI_CLK_25MHZ;
408 +               break;
409 +       case 4:
410 +               clk_cfg = SPI_CLK_12_50MHZ;
411 +               break;
412 +       case 8:
413 +               clk_cfg = SPI_CLK_6_250MHZ;
414 +               break;
415 +       case 16:
416 +               clk_cfg = SPI_CLK_3_125MHZ;
417 +               break;
418 +       case 32:
419 +               clk_cfg = SPI_CLK_1_563MHZ;
420 +               break;
421 +       case 128:
422 +               clk_cfg = SPI_CLK_0_781MHZ;
423 +               break;
424 +       case 64:
425 +       default:
426 +               /* Set to slowest mode for compatibility */
427 +               clk_cfg = SPI_CLK_0_781MHZ;
428 +               break;
429 +       }
430 +
431 +       bcm_spi_writeb(clk_cfg, SPI_CLK_CFG);
432 +       dev_dbg(&spi->dev, "Setting clock register to %d (hz %d, cmd %02x)\n",
433 +               div, hz, clk_cfg);
434 +
435 +       return 0;
436 +}
437 +
438 +/* the spi->mode bits understood by this driver: */
439 +#define MODEBITS (SPI_CPOL | SPI_CPHA)
440 +
441 +static int bcm63xx_spi_setup(struct spi_device *spi)
442 +{
443 +       struct bcm63xx_spi *bs;
444 +       int retval;
445 +
446 +       bs = spi_master_get_devdata(spi->master);
447 +
448 +       if (bs->stopping)
449 +               return -ESHUTDOWN;
450 +
451 +       if (!spi->bits_per_word)
452 +               spi->bits_per_word = 8;
453 +
454 +       if (spi->mode & ~MODEBITS) {
455 +               dev_err(&spi->dev, "%s, unsupported mode bits %x\n",
456 +                       __func__, spi->mode & ~MODEBITS);
457 +               return -EINVAL;
458 +       }
459 +
460 +       retval = bcm63xx_spi_setup_transfer(spi, NULL);
461 +       if (retval < 0) {
462 +               dev_err(&spi->dev, "setup: unsupported mode bits %x\n",
463 +                       spi->mode & ~MODEBITS);
464 +               return retval;
465 +       }
466 +
467 +       dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec/bit\n",
468 +               __func__, spi->mode & MODEBITS, spi->bits_per_word, 0);
469 +
470 +       return 0;
471 +}
472 +
473 +/* Fill the TX FIFO with as many bytes as possible */
474 +static void bcm63xx_spi_fill_tx_fifo(struct bcm63xx_spi *bs)
475 +{
476 +        u8 tail;
477 +
478 +        /* Fill the Tx FIFO with as many bytes as possible */
479 +       tail = bcm_spi_readb(SPI_MSG_TAIL);
480 +
481 +        while ((tail < bs->fifo_size) && (bs->remaining_bytes > 0)) {
482 +                if (bs->tx_ptr)
483 +                        bcm_spi_writeb(*bs->tx_ptr++, SPI_MSG_DATA);
484 +               else
485 +                       bcm_spi_writeb(0, SPI_MSG_DATA);
486 +
487 +                bs->remaining_bytes--;
488 +               tail = bcm_spi_readb(SPI_MSG_TAIL);
489 +        }
490 +}
491 +
492 +static int bcm63xx_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
493 +{
494 +       struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
495 +       u16 msg_ctl;
496 +       u16 cmd;
497 +
498 +       dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
499 +               t->tx_buf, t->rx_buf, t->len);
500 +
501 +       /* Transmitter is inhibited */
502 +       bs->tx_ptr = t->tx_buf;
503 +       bs->rx_ptr = t->rx_buf;
504 +       init_completion(&bs->done);
505 +
506 +       if (t->tx_buf) {
507 +               bs->remaining_bytes = t->len;
508 +               bcm63xx_spi_fill_tx_fifo(bs);
509 +       }
510 +
511 +       /* Enable the command done interrupt which
512 +        * we use to determine completion of a command */
513 +       bcm_spi_writeb(SPI_INTR_CMD_DONE, SPI_INT_MASK);
514 +
515 +       /* Fill in the Message control register */
516 +       msg_ctl = (t->len << SPI_BYTE_CNT_SHIFT);
517 +
518 +       if (t->rx_buf && t->tx_buf)
519 +               msg_ctl |= (SPI_FD_RW << SPI_MSG_TYPE_SHIFT);
520 +       else if (t->rx_buf)
521 +               msg_ctl |= (SPI_HD_R << SPI_MSG_TYPE_SHIFT);
522 +       else if (t->tx_buf)
523 +               msg_ctl |= (SPI_HD_W << SPI_MSG_TYPE_SHIFT);
524 +
525 +       bcm_spi_writew(msg_ctl, SPI_MSG_CTL);
526 +
527 +       /* Issue the transfer */
528 +       cmd = SPI_CMD_START_IMMEDIATE;
529 +       cmd |= (0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT);
530 +       bcm_spi_writew(cmd, SPI_CMD);
531 +       wait_for_completion(&bs->done);
532 +
533 +       /* Disable the CMD_DONE interrupt */
534 +       bcm_spi_writeb(0, SPI_INT_MASK);
535 +
536 +       return t->len - bs->remaining_bytes;
537 +}
538 +
539 +static int bcm63xx_transfer(struct spi_device *spi, struct spi_message *msg)
540 +{
541 +       struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
542 +       struct spi_transfer *xfer;
543 +       int ret = 0;
544 +
545 +       if (unlikely(list_empty(&msg->transfers)))
546 +               return -EINVAL;
547 +
548 +       if (bs->stopping)
549 +               return -ESHUTDOWN;
550 +
551 +       list_for_each_entry(xfer, &msg->transfers, transfer_list) {
552 +               ret += bcm63xx_txrx_bufs(spi, xfer);
553 +       }
554 +
555 +       msg->complete(msg->context);
556 +
557 +       return ret;
558 +}
559 +
560 +/* This driver supports single master mode only. Hence
561 + * CMD_DONE is the only interrupt we care about
562 + */
563 +static irqreturn_t bcm63xx_spi_interrupt(int irq, void *dev_id)
564 +{
565 +       struct spi_master *master = (struct spi_master *)dev_id;
566 +       struct bcm63xx_spi *bs = spi_master_get_devdata(master);
567 +       u8 intr;
568 +       u16 cmd;
569 +
570 +       /* Read interupts and clear them immediately */
571 +       intr = bcm_spi_readb(SPI_INT_STATUS);
572 +       bcm_spi_writeb(SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
573 +       bcm_spi_writeb(0, SPI_INT_MASK);
574 +
575 +       /* A tansfer completed */
576 +       if (intr & SPI_INTR_CMD_DONE) {
577 +               u8 rx_tail;
578 +
579 +               rx_tail = bcm_spi_readb(SPI_RX_TAIL);
580 +
581 +               /* Read out all the data */
582 +               if (rx_tail) {
583 +                       u8 data;
584 +                       u8 i = 0;
585 +
586 +                       for(i = 0; i < rx_tail; i++) {
587 +                               data = bcm_spi_readb(SPI_RX_DATA);
588 +                               if (bs->rx_ptr)
589 +                                       *bs->rx_ptr++ = data;
590 +                       }
591 +               }
592 +
593 +               /* See if there is more data to send */
594 +               if (bs->remaining_bytes > 0) {
595 +                       bcm63xx_spi_fill_tx_fifo(bs);
596 +
597 +                       /* Start the transfer */
598 +                       bcm_spi_writew(SPI_HD_W << SPI_MSG_TYPE_SHIFT,
599 +                                      SPI_MSG_CTL);
600 +                       cmd = bcm_spi_readw(SPI_CMD);
601 +                       cmd |= SPI_CMD_START_IMMEDIATE;
602 +                       cmd |= (0 << SPI_CMD_PREPEND_BYTE_CNT_SHIFT);
603 +                       bcm_spi_writeb(SPI_INTR_CMD_DONE, SPI_INT_MASK);
604 +                       bcm_spi_writew(cmd, SPI_CMD);
605 +               } else {
606 +                       complete(&bs->done);
607 +               }
608 +       }
609 +
610 +       return IRQ_HANDLED;
611 +}
612 +
613 +
614 +static int __init bcm63xx_spi_probe(struct platform_device *pdev)
615 +{
616 +       struct resource *r;
617 +       struct device *dev = &pdev->dev;
618 +       struct bcm63xx_spi_pdata *pdata = pdev->dev.platform_data;
619 +       int irq;
620 +       struct spi_master *master;
621 +       struct clk *clk;
622 +       struct bcm63xx_spi *bs;
623 +       int ret;
624 +
625 +       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
626 +       if (!r) {
627 +               dev_err(dev, "no iomem\n");
628 +               ret = -ENXIO;
629 +               goto out;
630 +       }
631 +
632 +       irq = platform_get_irq(pdev, 0);
633 +       if (irq < 0) {
634 +               dev_err(dev, "no irq\n");
635 +               ret = -ENXIO;
636 +               goto out;
637 +       }
638 +
639 +       clk = clk_get(&pdev->dev, "spi");
640 +       if (IS_ERR(clk)) {
641 +               dev_err(dev, "no clock for device\n");
642 +               ret = -ENODEV;
643 +               goto out;
644 +       }
645 +
646 +       master = spi_alloc_master(&pdev->dev, sizeof(struct bcm63xx_spi));
647 +       if (!master) {
648 +               dev_err(dev, "out of memory\n");
649 +               ret = -ENOMEM;
650 +               goto out_free;
651 +       }
652 +
653 +       bs = spi_master_get_devdata(master);
654 +       init_completion(&bs->done);
655 +
656 +       platform_set_drvdata(pdev, master);
657 +        bs->pdev = pdev;
658 +
659 +       if (!request_mem_region(r->start,
660 +                       r->end - r->start, PFX)) {
661 +               dev_err(dev, "iomem request failed\n");
662 +               ret = -ENXIO;
663 +               goto out_put_master;
664 +       }
665 +
666 +        bs->regs = ioremap_nocache(r->start, r->end - r->start);
667 +       if (!bs->regs) {
668 +               dev_err(dev, "unable to ioremap regs\n");
669 +               ret = -ENOMEM;
670 +               goto out_put_master;
671 +       }
672 +       bs->irq = irq;
673 +       bs->clk = clk;
674 +       bs->fifo_size = pdata->fifo_size;
675 +
676 +       ret = request_irq(irq, bcm63xx_spi_interrupt, 0,
677 +                         pdev->name, master);
678 +       if (ret) {
679 +               dev_err(dev, "unable to request irq\n");
680 +               goto out_unmap;
681 +       }
682 +
683 +       master->bus_num = pdata->bus_num;
684 +       master->num_chipselect = pdata->num_chipselect;
685 +       master->setup = bcm63xx_spi_setup;
686 +       master->transfer = bcm63xx_transfer;
687 +       bs->speed_hz = pdata->speed_hz;
688 +       bs->stopping = 0;
689 +       spin_lock_init(&bs->lock);
690 +
691 +       /* Initialize hardware */
692 +       clk_enable(bs->clk);
693 +       bcm_spi_writeb(SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
694 +
695 +       /* register and we are done */
696 +       ret = spi_register_master(master);
697 +       if (ret) {
698 +               dev_err(dev, "spi register failed\n");
699 +               goto out_reset_hw;
700 +       }
701 +
702 +       dev_info(dev, "at 0x%08x (irq %d, FIFOs size %d) v%s\n",
703 +                r->start, irq, bs->fifo_size, DRV_VER);
704 +
705 +       return ret;
706 +
707 +out_reset_hw:
708 +       clk_disable(clk);
709 +       free_irq(irq, master);
710 +out_unmap:
711 +       iounmap(bs->regs);
712 +out_put_master:
713 +       spi_master_put(master);
714 +out_free:
715 +       clk_put(clk);
716 +out:
717 +       return ret;
718 +}
719 +
720 +static int __exit bcm63xx_spi_remove(struct platform_device *pdev)
721 +{
722 +       struct spi_master  *master = platform_get_drvdata(pdev);
723 +       struct bcm63xx_spi *bs = spi_master_get_devdata(master);
724 +       struct resource    *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
725 +
726 +       /* reset spi block */
727 +       bcm_spi_writeb(0, SPI_INT_MASK);
728 +       spin_lock(&bs->lock);
729 +       bs->stopping = 1;
730 +
731 +       /* HW shutdown */
732 +       clk_disable(bs->clk);
733 +       clk_put(bs->clk);
734 +
735 +       spin_unlock(&bs->lock);
736 +
737 +       free_irq(bs->irq, master);
738 +       iounmap(bs->regs);
739 +       release_mem_region(r->start, r->end - r->start);
740 +       platform_set_drvdata(pdev, 0);
741 +       spi_unregister_master(master);
742 +
743 +       return 0;
744 +}
745 +
746 +#ifdef CONFIG_PM
747 +static int bcm63xx_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
748 +{
749 +       struct spi_master       *master = platform_get_drvdata(pdev);
750 +       struct bcm63xx_spi      *bs = spi_master_get_devdata(master);
751 +
752 +        clk_disable(bs->clk);
753 +
754 +       return 0;
755 +}
756 +
757 +static int bcm63xx_spi_resume(struct platform_device *pdev)
758 +{
759 +       struct spi_master       *master = platform_get_drvdata(pdev);
760 +       struct bcm63xx_spi      *bs = spi_master_get_devdata(master);
761 +
762 +       clk_enable(bs->clk);
763 +
764 +       return 0;
765 +}
766 +#else
767 +#define bcm63xx_spi_suspend    NULL
768 +#define bcm63xx_spi_resume     NULL
769 +#endif
770 +
771 +static struct platform_driver bcm63xx_spi_driver = {
772 +       .driver = {
773 +               .name   = "bcm63xx-spi",
774 +               .owner  = THIS_MODULE,
775 +       },
776 +       .probe          = bcm63xx_spi_probe,
777 +       .remove         = bcm63xx_spi_remove,
778 +       .suspend        = bcm63xx_spi_suspend,
779 +       .resume         = bcm63xx_spi_resume,
780 +};
781 +
782 +
783 +static int __init bcm63xx_spi_init(void)
784 +{
785 +       return platform_driver_register(&bcm63xx_spi_driver);
786 +}
787 +
788 +static void __exit bcm63xx_spi_exit(void)
789 +{
790 +       platform_driver_unregister(&bcm63xx_spi_driver);
791 +}
792 +
793 +module_init(bcm63xx_spi_init);
794 +module_exit(bcm63xx_spi_exit);
795 +
796 +MODULE_ALIAS("platform:bcm63xx_spi");
797 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
798 +MODULE_DESCRIPTION("Broadcom BCM63xx SPI Controller driver");
799 +MODULE_LICENSE("GPL");
800 +MODULE_VERSION(DRV_VER);
801 --- a/drivers/spi/Kconfig
802 +++ b/drivers/spi/Kconfig
803 @@ -60,6 +60,12 @@ config SPI_ATMEL
804           This selects a driver for the Atmel SPI Controller, present on
805           many AT32 (AVR32) and AT91 (ARM) chips.
806  
807 +config SPI_BCM63XX
808 +       tristate "Broadcom BCM63xx SPI controller"
809 +       depends on BCM63XX
810 +       help
811 +         This is the SPI controller master driver for Broadcom BCM63xx SoC.
812 +
813  config SPI_BFIN
814         tristate "SPI controller driver for ADI Blackfin5xx"
815         depends on BLACKFIN
816 --- a/drivers/spi/Makefile
817 +++ b/drivers/spi/Makefile
818 @@ -48,6 +48,7 @@ obj-$(CONFIG_SPI_SH_SCI)              += spi_sh_sci.
819  obj-$(CONFIG_SPI_SH_MSIOF)             += spi_sh_msiof.o
820  obj-$(CONFIG_SPI_STMP3XXX)             += spi_stmp.o
821  obj-$(CONFIG_SPI_NUC900)               += spi_nuc900.o
822 +obj-$(CONFIG_SPI_BCM63XX)              += bcm63xx_spi.o
823  
824  # special build for s3c24xx spi driver with fiq support
825  spi_s3c24xx_hw-y                       := spi_s3c24xx.o
826 --- /dev/null
827 +++ b/arch/mips/include/asm/mach-bcm63xx/bcm63xx_dev_spi.h
828 @@ -0,0 +1,136 @@
829 +#ifndef BCM63XX_DEV_SPI_H
830 +#define BCM63XX_DEV_SPI_H
831 +
832 +#include <linux/types.h>
833 +#include <bcm63xx_io.h>
834 +#include <bcm63xx_regs.h>
835 +
836 +int __init bcm63xx_spi_register(void);
837 +
838 +struct bcm63xx_spi_pdata {
839 +       unsigned int    fifo_size;
840 +       int             bus_num;
841 +       int             num_chipselect;
842 +       u32             speed_hz;
843 +};
844 +
845 +enum bcm63xx_regs_spi {
846 +        SPI_CMD,
847 +        SPI_INT_STATUS,
848 +        SPI_INT_MASK_ST,
849 +        SPI_INT_MASK,
850 +        SPI_ST,
851 +        SPI_CLK_CFG,
852 +        SPI_FILL_BYTE,
853 +        SPI_MSG_TAIL,
854 +        SPI_RX_TAIL,
855 +        SPI_MSG_CTL,
856 +        SPI_MSG_DATA,
857 +        SPI_RX_DATA,
858 +};
859 +
860 +static inline unsigned long bcm63xx_spireg(enum bcm63xx_regs_spi reg)
861 +{
862 +#ifdef BCMCPU_RUNTIME_DETECT
863 +       extern const unsigned long *bcm63xx_regs_spi;
864 +        return bcm63xx_regs_spi[reg];
865 +#else
866 +#ifdef CONFIG_BCM63XX_CPU_6338
867 +switch (reg) {
868 +       case SPI_CMD:
869 +               return SPI_BCM_6338_SPI_CMD;
870 +       case SPI_INT_STATUS:
871 +               return SPI_BCM_6338_SPI_INT_STATUS;
872 +       case SPI_INT_MASK_ST:
873 +               return SPI_BCM_6338_SPI_MASK_INT_ST;
874 +       case SPI_INT_MASK:
875 +               return SPI_BCM_6338_SPI_INT_MASK;
876 +       case SPI_ST:
877 +               return SPI_BCM_6338_SPI_ST;
878 +       case SPI_CLK_CFG:
879 +               return SPI_BCM_6338_SPI_CLK_CFG;
880 +       case SPI_FILL_BYTE:
881 +               return SPI_BCM_6338_SPI_FILL_BYTE;
882 +       case SPI_MSG_TAIL:
883 +               return SPI_BCM_6338_SPI_MSG_TAIL;
884 +       case SPI_RX_TAIL:
885 +               return SPI_BCM_6338_SPI_RX_TAIL;
886 +       case SPI_MSG_CTL:
887 +               return SPI_BCM_6338_SPI_MSG_CTL;
888 +       case SPI_MSG_DATA:
889 +               return SPI_BCM_6338_SPI_MSG_DATA;
890 +       case SPI_RX_DATA:
891 +               return SPI_BCM_6338_SPI_RX_DATA;
892 +}
893 +#endif
894 +#ifdef CONFIG_BCM63XX_CPU_6348
895 +switch (reg) {
896 +       case SPI_CMD:
897 +               return SPI_BCM_6348_SPI_CMD;
898 +       case SPI_INT_MASK_ST:
899 +               return SPI_BCM_6348_SPI_MASK_INT_ST;
900 +       case SPI_INT_MASK:
901 +               return SPI_BCM_6348_SPI_INT_MASK;
902 +       case SPI_INT_STATUS:
903 +               return SPI_BCM_6348_SPI_INT_STATUS;
904 +       case SPI_ST:
905 +               return SPI_BCM_6348_SPI_ST;
906 +       case SPI_CLK_CFG:
907 +               return SPI_BCM_6348_SPI_CLK_CFG;
908 +       case SPI_FILL_BYTE:
909 +               return SPI_BCM_6348_SPI_FILL_BYTE;
910 +       case SPI_MSG_TAIL:
911 +               return SPI_BCM_6348_SPI_MSG_TAIL;
912 +       case SPI_RX_TAIL:
913 +               return SPI_BCM_6348_SPI_RX_TAIL;
914 +       case SPI_MSG_CTL:
915 +               return SPI_BCM_6348_SPI_MSG_CTL;
916 +       case SPI_MSG_DATA:
917 +               return SPI_BCM_6348_SPI_MSG_DATA;
918 +       case SPI_RX_DATA:
919 +               return SPI_BCM_6348_SPI_RX_DATA;
920 +}
921 +#endif
922 +#ifdef CONFIG_BCM63XX_CPU_6358
923 +switch (reg) {
924 +       case SPI_CMD:
925 +               return SPI_BCM_6358_SPI_CMD;
926 +       case SPI_INT_STATUS:
927 +               return SPI_BCM_6358_SPI_INT_STATUS;
928 +       case SPI_INT_MASK_ST:
929 +               return SPI_BCM_6358_SPI_MASK_INT_ST;
930 +       case SPI_INT_MASK:
931 +               return SPI_BCM_6358_SPI_INT_MASK;
932 +       case SPI_ST:
933 +               return SPI_BCM_6358_SPI_STATUS;
934 +       case SPI_CLK_CFG:
935 +               return SPI_BCM_6358_SPI_CLK_CFG;
936 +       case SPI_FILL_BYTE:
937 +               return SPI_BCM_6358_SPI_FILL_BYTE;
938 +       case SPI_MSG_TAIL:
939 +               return SPI_BCM_6358_SPI_MSG_TAIL;
940 +       case SPI_RX_TAIL:
941 +               return SPI_BCM_6358_SPI_RX_TAIL;
942 +       case SPI_MSG_CTL:
943 +               return SPI_BCM_6358_MSG_CTL;
944 +       case SPI_MSG_DATA:
945 +               return SPI_BCM_6358_SPI_MSG_DATA;
946 +       case SPI_RX_DATA:
947 +               return SPI_BCM_6358_SPI_RX_DATA;
948 +}
949 +#endif
950 +#endif
951 +       return 0;
952 +}
953 +
954 +/*
955 + * helpers for the SPI register sets
956 + */
957 +#define bcm_spi_readb(o)    bcm_rset_readb(RSET_SPI, bcm63xx_spireg(o))
958 +#define bcm_spi_readw(o)    bcm_rset_readw(RSET_SPI, bcm63xx_spireg(o))
959 +#define bcm_spi_readl(o)    bcm_rset_readl(RSET_SPI, bcm63xx_spireg(o))
960 +#define bcm_spi_writeb(v,o) bcm_rset_writeb(RSET_SPI, (v), bcm63xx_spireg(o))
961 +#define bcm_spi_writew(v,o) bcm_rset_writew(RSET_SPI, (v), bcm63xx_spireg(o))
962 +#define bcm_spi_writel(v,o) bcm_rset_writel(RSET_SPI, (v), bcm63xx_spireg(o))
963 +
964 +#endif /* BCM63XX_DEV_SPI_H */
965 --- a/arch/mips/bcm63xx/Makefile
966 +++ b/arch/mips/bcm63xx/Makefile
967 @@ -1,6 +1,6 @@
968  obj-y          += clk.o cpu.o cs.o gpio.o irq.o prom.o setup.o timer.o \
969                    dev-dsp.o dev-enet.o dev-pcmcia.o dev-uart.o dev-wdt.o \
970 -                  dev-usb-ohci.o dev-usb-ehci.o dev-usb-udc.o
971 +                  dev-usb-ohci.o dev-usb-ehci.o dev-usb-udc.o dev-spi.o
972  obj-$(CONFIG_EARLY_PRINTK)     += early_printk.o
973  
974  obj-y          += boards/
975 --- a/arch/mips/bcm63xx/boards/board_bcm963xx.c
976 +++ b/arch/mips/bcm63xx/boards/board_bcm963xx.c
977 @@ -33,6 +33,7 @@
978  #include <bcm63xx_dev_usb_ohci.h>
979  #include <bcm63xx_dev_usb_ehci.h>
980  #include <bcm63xx_dev_usb_udc.h>
981 +#include <bcm63xx_dev_spi.h>
982  #include <board_bcm963xx.h>
983  
984  #define PFX    "board_bcm963xx: "
985 @@ -1586,6 +1587,9 @@ int __init board_register_devices(void)
986         if (board.num_spis)
987                 spi_register_board_info(board.spis, board.num_spis);
988  
989 +       if (!BCMCPU_IS_6345())
990 +               bcm63xx_spi_register();
991 +
992         /* read base address of boot chip select (0) */
993         if (BCMCPU_IS_6345())
994                 val = 0x1fc00000;