2 * SPI controller driver for the Mikrotik RB4xx boards
4 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
6 * This file was based on the patches for Linux 2.6.27.39 published by
7 * MikroTik for their RouterBoard 4xx series devices.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/spinlock.h>
19 #include <linux/workqueue.h>
20 #include <linux/platform_device.h>
21 #include <linux/spi/spi.h>
23 #include <asm/mach-ar71xx/ar71xx.h>
25 #define DRV_NAME "rb4xx-spi"
26 #define DRV_DESC "Mikrotik RB4xx SPI controller driver"
27 #define DRV_VERSION "0.1.0"
29 #define SPI_CTRL_FASTEST 0x40
30 #define SPI_FLASH_HZ 33333334
31 #define SPI_CPLD_HZ 33333334
33 #define CPLD_CMD_READ_FAST 0x0b
35 #undef RB4XX_SPI_DEBUG
39 struct spi_master
*master
;
41 unsigned spi_ctrl_flash
;
42 unsigned spi_ctrl_fread
;
45 struct list_head queue
;
50 static unsigned spi_clk_low
= SPI_IOC_CS1
;
52 #ifdef RB4XX_SPI_DEBUG
53 static inline void do_spi_delay(void)
58 static inline void do_spi_delay(void) { }
61 static inline void do_spi_init(struct spi_device
*spi
)
63 unsigned cs
= SPI_IOC_CS0
| SPI_IOC_CS1
;
65 if (!(spi
->mode
& SPI_CS_HIGH
))
66 cs
^= (spi
->chip_select
== 2) ? SPI_IOC_CS1
: SPI_IOC_CS0
;
71 static inline void do_spi_finish(void __iomem
*base
)
74 __raw_writel(SPI_IOC_CS0
| SPI_IOC_CS1
, base
+ SPI_REG_IOC
);
77 static inline void do_spi_clk(void __iomem
*base
, int bit
)
79 unsigned bval
= spi_clk_low
| ((bit
& 1) ? SPI_IOC_DO
: 0);
82 __raw_writel(bval
, base
+ SPI_REG_IOC
);
84 __raw_writel(bval
| SPI_IOC_CLK
, base
+ SPI_REG_IOC
);
87 static void do_spi_byte(void __iomem
*base
, unsigned char byte
)
89 do_spi_clk(base
, byte
>> 7);
90 do_spi_clk(base
, byte
>> 6);
91 do_spi_clk(base
, byte
>> 5);
92 do_spi_clk(base
, byte
>> 4);
93 do_spi_clk(base
, byte
>> 3);
94 do_spi_clk(base
, byte
>> 2);
95 do_spi_clk(base
, byte
>> 1);
96 do_spi_clk(base
, byte
);
98 #ifdef RB4XX_SPI_DEBUG
99 printk("spi_byte sent 0x%02x got 0x%02x\n",
101 (unsigned char)__raw_readl(base
+ SPI_REG_RDS
));
105 static inline void do_spi_clk_fast(void __iomem
*base
, unsigned bit1
,
108 unsigned bval
= (spi_clk_low
|
109 ((bit1
& 1) ? SPI_IOC_DO
: 0) |
110 ((bit2
& 1) ? SPI_IOC_CS2
: 0));
112 __raw_writel(bval
, base
+ SPI_REG_IOC
);
114 __raw_writel(bval
| SPI_IOC_CLK
, base
+ SPI_REG_IOC
);
117 static void do_spi_byte_fast(void __iomem
*base
, unsigned char byte
)
119 do_spi_clk_fast(base
, byte
>> 7, byte
>> 6);
120 do_spi_clk_fast(base
, byte
>> 5, byte
>> 4);
121 do_spi_clk_fast(base
, byte
>> 3, byte
>> 2);
122 do_spi_clk_fast(base
, byte
>> 1, byte
>> 0);
124 #ifdef RB4XX_SPI_DEBUG
125 printk("spi_byte_fast sent 0x%02x got 0x%02x\n",
127 (unsigned char) __raw_readl(base
+ SPI_REG_RDS
));
131 static int rb4xx_spi_txrx(void __iomem
*base
, struct spi_transfer
*t
)
133 const unsigned char *rxv_ptr
= NULL
;
134 const unsigned char *tx_ptr
= t
->tx_buf
;
135 unsigned char *rx_ptr
= t
->rx_buf
;
138 #ifdef RB4XX_SPI_DEBUG
139 printk("spi_txrx len %u tx %u rx %u\n",
142 (t
->rx_buf
? 1 : 0));
150 for (i
= 0; i
< t
->len
; ++i
) {
151 unsigned char sdata
= tx_ptr
? tx_ptr
[i
] : 0;
154 do_spi_byte_fast(base
, sdata
);
156 do_spi_byte(base
, sdata
);
159 rx_ptr
[i
] = __raw_readl(base
+ SPI_REG_RDS
) & 0xff;
160 } else if (rxv_ptr
) {
161 unsigned char c
= __raw_readl(base
+ SPI_REG_RDS
);
170 static int rb4xx_spi_read_fast(struct rb4xx_spi
*rbspi
,
171 struct spi_message
*m
)
173 struct spi_transfer
*t
;
174 const unsigned char *tx_ptr
;
176 void __iomem
*base
= rbspi
->base
;
178 /* check for exactly two transfers */
179 if (list_empty(&m
->transfers
) ||
180 list_is_last(m
->transfers
.next
, &m
->transfers
) ||
181 !list_is_last(m
->transfers
.next
->next
, &m
->transfers
)) {
185 /* first transfer contains command and address */
186 t
= list_entry(m
->transfers
.next
,
187 struct spi_transfer
, transfer_list
);
189 if (t
->len
!= 5 || t
->tx_buf
== NULL
)
193 if (tx_ptr
[0] != CPLD_CMD_READ_FAST
)
197 addr
= tx_ptr
[2] | (addr
<< 8);
198 addr
= tx_ptr
[3] | (addr
<< 8);
199 addr
+= (unsigned) base
;
201 m
->actual_length
+= t
->len
;
203 /* second transfer contains data itself */
204 t
= list_entry(m
->transfers
.next
->next
,
205 struct spi_transfer
, transfer_list
);
207 if (t
->tx_buf
&& !t
->verify
)
210 __raw_writel(SPI_FS_GPIO
, base
+ SPI_REG_FS
);
211 __raw_writel(rbspi
->spi_ctrl_fread
, base
+ SPI_REG_CTRL
);
212 __raw_writel(0, base
+ SPI_REG_FS
);
215 memcpy(t
->rx_buf
, (const void *)addr
, t
->len
);
216 } else if (t
->tx_buf
) {
217 unsigned char buf
[t
->len
];
218 memcpy(buf
, (const void *)addr
, t
->len
);
219 if (memcmp(t
->tx_buf
, buf
, t
->len
) != 0)
220 m
->status
= -EMSGSIZE
;
222 m
->actual_length
+= t
->len
;
224 if (rbspi
->spi_ctrl_flash
!= rbspi
->spi_ctrl_fread
) {
225 __raw_writel(SPI_FS_GPIO
, base
+ SPI_REG_FS
);
226 __raw_writel(rbspi
->spi_ctrl_flash
, base
+ SPI_REG_CTRL
);
227 __raw_writel(0, base
+ SPI_REG_FS
);
233 static int rb4xx_spi_msg(struct rb4xx_spi
*rbspi
, struct spi_message
*m
)
235 struct spi_transfer
*t
= NULL
;
236 void __iomem
*base
= rbspi
->base
;
239 if (list_empty(&m
->transfers
))
243 if (rb4xx_spi_read_fast(rbspi
, m
) == 0)
246 __raw_writel(SPI_FS_GPIO
, base
+ SPI_REG_FS
);
247 __raw_writel(SPI_CTRL_FASTEST
, base
+ SPI_REG_CTRL
);
250 list_for_each_entry (t
, &m
->transfers
, transfer_list
) {
253 len
= rb4xx_spi_txrx(base
, t
);
255 m
->status
= -EMSGSIZE
;
258 m
->actual_length
+= len
;
261 if (list_is_last(&t
->transfer_list
, &m
->transfers
)) {
262 /* wait for continuation */
263 return m
->spi
->chip_select
;
271 __raw_writel(rbspi
->spi_ctrl_flash
, base
+ SPI_REG_CTRL
);
272 __raw_writel(0, base
+ SPI_REG_FS
);
276 static void rb4xx_spi_process_queue_locked(struct rb4xx_spi
*rbspi
,
277 unsigned long *flags
)
279 int cs
= rbspi
->cs_wait
;
282 while (!list_empty(&rbspi
->queue
)) {
283 struct spi_message
*m
;
285 list_for_each_entry(m
, &rbspi
->queue
, queue
)
286 if (cs
< 0 || cs
== m
->spi
->chip_select
)
289 if (&m
->queue
== &rbspi
->queue
)
292 list_del_init(&m
->queue
);
293 spin_unlock_irqrestore(&rbspi
->lock
, *flags
);
295 cs
= rb4xx_spi_msg(rbspi
, m
);
296 m
->complete(m
->context
);
298 spin_lock_irqsave(&rbspi
->lock
, *flags
);
305 /* TODO: add timer to unlock cs after 1s inactivity */
309 static int rb4xx_spi_transfer(struct spi_device
*spi
,
310 struct spi_message
*m
)
312 struct rb4xx_spi
*rbspi
= spi_master_get_devdata(spi
->master
);
315 m
->actual_length
= 0;
316 m
->status
= -EINPROGRESS
;
318 spin_lock_irqsave(&rbspi
->lock
, flags
);
319 list_add_tail(&m
->queue
, &rbspi
->queue
);
321 (rbspi
->cs_wait
>= 0 && rbspi
->cs_wait
!= m
->spi
->chip_select
)) {
322 /* job will be done later */
323 spin_unlock_irqrestore(&rbspi
->lock
, flags
);
327 /* process job in current context */
328 rb4xx_spi_process_queue_locked(rbspi
, &flags
);
329 spin_unlock_irqrestore(&rbspi
->lock
, flags
);
334 static int rb4xx_spi_setup(struct spi_device
*spi
)
336 struct rb4xx_spi
*rbspi
= spi_master_get_devdata(spi
->master
);
339 if (spi
->mode
& ~(SPI_CS_HIGH
)) {
340 dev_err(&spi
->dev
, "mode %x not supported\n",
341 (unsigned) spi
->mode
);
345 if (spi
->bits_per_word
!= 8 && spi
->bits_per_word
!= 0) {
346 dev_err(&spi
->dev
, "bits_per_word %u not supported\n",
347 (unsigned) spi
->bits_per_word
);
351 spin_lock_irqsave(&rbspi
->lock
, flags
);
352 if (rbspi
->cs_wait
== spi
->chip_select
&& !rbspi
->busy
) {
354 rb4xx_spi_process_queue_locked(rbspi
, &flags
);
356 spin_unlock_irqrestore(&rbspi
->lock
, flags
);
361 static unsigned get_spi_ctrl(unsigned hz_max
, const char *name
)
365 div
= (ar71xx_ahb_freq
- 1) / (2 * hz_max
);
368 * CPU has a bug at (div == 0) - first bit read is random
374 unsigned ahb_khz
= (ar71xx_ahb_freq
+ 500) / 1000;
375 unsigned div_real
= 2 * (div
+ 1);
376 pr_debug("rb4xx: %s SPI clock %u kHz (AHB %u kHz / %u)\n",
382 return SPI_CTRL_FASTEST
+ div
;
385 static int rb4xx_spi_probe(struct platform_device
*pdev
)
387 struct spi_master
*master
;
388 struct rb4xx_spi
*rbspi
;
392 master
= spi_alloc_master(&pdev
->dev
, sizeof(*rbspi
));
393 if (master
== NULL
) {
394 dev_err(&pdev
->dev
, "no memory for spi_master\n");
400 master
->num_chipselect
= 3;
401 master
->setup
= rb4xx_spi_setup
;
402 master
->transfer
= rb4xx_spi_transfer
;
404 rbspi
= spi_master_get_devdata(master
);
405 platform_set_drvdata(pdev
, rbspi
);
407 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
413 rbspi
->base
= ioremap(r
->start
, r
->end
- r
->start
+ 1);
419 rbspi
->master
= master
;
420 rbspi
->spi_ctrl_flash
= get_spi_ctrl(SPI_FLASH_HZ
, "FLASH");
421 rbspi
->spi_ctrl_fread
= get_spi_ctrl(SPI_CPLD_HZ
, "CPLD");
424 spin_lock_init(&rbspi
->lock
);
425 INIT_LIST_HEAD(&rbspi
->queue
);
427 err
= spi_register_master(master
);
429 dev_err(&pdev
->dev
, "failed to register SPI master\n");
436 iounmap(rbspi
->base
);
438 platform_set_drvdata(pdev
, NULL
);
439 spi_master_put(master
);
444 static int rb4xx_spi_remove(struct platform_device
*pdev
)
446 struct rb4xx_spi
*rbspi
= platform_get_drvdata(pdev
);
448 iounmap(rbspi
->base
);
449 platform_set_drvdata(pdev
, NULL
);
450 spi_master_put(rbspi
->master
);
455 static struct platform_driver rb4xx_spi_drv
= {
456 .probe
= rb4xx_spi_probe
,
457 .remove
= rb4xx_spi_remove
,
460 .owner
= THIS_MODULE
,
464 static int __init
rb4xx_spi_init(void)
466 return platform_driver_register(&rb4xx_spi_drv
);
468 subsys_initcall(rb4xx_spi_init
);
470 static void __exit
rb4xx_spi_exit(void)
472 platform_driver_unregister(&rb4xx_spi_drv
);
475 module_exit(rb4xx_spi_exit
);
477 MODULE_DESCRIPTION(DRV_DESC
);
478 MODULE_VERSION(DRV_VERSION
);
479 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
480 MODULE_LICENSE("GPL v2");