2 * ramips_spi.c -- Ralink RT288x/RT305x SPI controller driver
4 * Copyright (C) 2011 Sergiy <piratfm@gmail.com>
5 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/clk.h>
15 #include <linux/err.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
19 #include <linux/spi/spi.h>
21 #define DRIVER_NAME "ramips-spi"
22 #define RALINK_NUM_CHIPSELECTS 1 /* only one slave is supported*/
23 #define RALINK_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
25 #define RAMIPS_SPI_STAT 0x00
26 #define RAMIPS_SPI_CFG 0x10
27 #define RAMIPS_SPI_CTL 0x14
28 #define RAMIPS_SPI_DATA 0x20
30 /* SPISTAT register bit field */
31 #define SPISTAT_BUSY BIT(0)
33 /* SPICFG register bit field */
34 #define SPICFG_LSBFIRST 0
35 #define SPICFG_MSBFIRST BIT(8)
36 #define SPICFG_SPICLKPOL BIT(6)
37 #define SPICFG_RXCLKEDGE_FALLING BIT(5)
38 #define SPICFG_TXCLKEDGE_FALLING BIT(4)
39 #define SPICFG_SPICLK_PRESCALE_MASK 0x7
40 #define SPICFG_SPICLK_DIV2 0
41 #define SPICFG_SPICLK_DIV4 1
42 #define SPICFG_SPICLK_DIV8 2
43 #define SPICFG_SPICLK_DIV16 3
44 #define SPICFG_SPICLK_DIV32 4
45 #define SPICFG_SPICLK_DIV64 5
46 #define SPICFG_SPICLK_DIV128 6
47 #define SPICFG_SPICLK_DISABLE 7
49 /* SPICTL register bit field */
50 #define SPICTL_HIZSDO BIT(3)
51 #define SPICTL_STARTWR BIT(2)
52 #define SPICTL_STARTRD BIT(1)
53 #define SPICTL_SPIENA BIT(0)
56 #define spi_debug(args...) printk(args)
58 #define spi_debug(args...)
62 struct work_struct work
;
64 /* Lock access to transfer list.*/
67 struct list_head msg_queue
;
68 struct spi_master
*master
;
70 unsigned int sys_freq
;
76 static struct workqueue_struct
*ramips_spi_wq
;
78 static inline struct ramips_spi
*ramips_spidev_to_rs(struct spi_device
*spi
)
80 return spi_master_get_devdata(spi
->master
);
83 static inline u32
ramips_spi_read(struct ramips_spi
*rs
, u32 reg
)
85 return ioread32(rs
->base
+ reg
);
88 static inline void ramips_spi_write(struct ramips_spi
*rs
, u32 reg
, u32 val
)
90 iowrite32(val
, rs
->base
+ reg
);
93 static inline void ramips_spi_setbits(struct ramips_spi
*rs
, u32 reg
, u32 mask
)
95 void __iomem
*addr
= rs
->base
+ reg
;
100 iowrite32(val
, addr
);
103 static inline void ramips_spi_clrbits(struct ramips_spi
*rs
, u32 reg
, u32 mask
)
105 void __iomem
*addr
= rs
->base
+ reg
;
108 val
= ioread32(addr
);
110 iowrite32(val
, addr
);
113 static int ramips_spi_baudrate_set(struct spi_device
*spi
, unsigned int speed
)
115 struct ramips_spi
*rs
= ramips_spidev_to_rs(spi
);
120 spi_debug("%s: speed:%u\n", __func__
, speed
);
123 * the supported rates are: 2,4,8...128
124 * round up as we look for equal or less speed
126 rate
= DIV_ROUND_UP(rs
->sys_freq
, speed
);
127 spi_debug("%s: rate-1:%u\n", __func__
, rate
);
128 rate
= roundup_pow_of_two(rate
);
129 spi_debug("%s: rate-2:%u\n", __func__
, rate
);
131 /* check if requested speed is too small */
138 /* Convert the rate to SPI clock divisor value. */
139 prescale
= ilog2(rate
/2);
140 spi_debug("%s: prescale:%u\n", __func__
, prescale
);
142 reg
= ramips_spi_read(rs
, RAMIPS_SPI_CFG
);
143 reg
= ((reg
& ~SPICFG_SPICLK_PRESCALE_MASK
) | prescale
);
144 ramips_spi_write(rs
, RAMIPS_SPI_CFG
, reg
);
150 * called only when no transfer is active on the bus
153 ramips_spi_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
155 struct ramips_spi
*rs
= ramips_spidev_to_rs(spi
);
156 unsigned int speed
= spi
->max_speed_hz
;
158 unsigned int bits_per_word
= 8;
160 if ((t
!= NULL
) && t
->speed_hz
)
163 if ((t
!= NULL
) && t
->bits_per_word
)
164 bits_per_word
= t
->bits_per_word
;
166 if (rs
->speed
!= speed
) {
167 spi_debug("%s: speed_hz:%u\n", __func__
, speed
);
168 rc
= ramips_spi_baudrate_set(spi
, speed
);
173 if (bits_per_word
!= 8) {
174 spi_debug("%s: bad bits_per_word: %u\n", __func__
,
182 static void ramips_spi_set_cs(struct ramips_spi
*rs
, int enable
)
185 ramips_spi_clrbits(rs
, RAMIPS_SPI_CTL
, SPICTL_SPIENA
);
187 ramips_spi_setbits(rs
, RAMIPS_SPI_CTL
, SPICTL_SPIENA
);
190 static inline int ramips_spi_wait_till_ready(struct ramips_spi
*rs
)
194 for (i
= 0; i
< RALINK_SPI_WAIT_RDY_MAX_LOOP
; i
++) {
197 status
= ramips_spi_read(rs
, RAMIPS_SPI_STAT
);
198 if ((status
& SPISTAT_BUSY
) == 0)
208 ramips_spi_write_read(struct spi_device
*spi
, struct spi_transfer
*xfer
)
210 struct ramips_spi
*rs
= ramips_spidev_to_rs(spi
);
212 u8
*rx
= xfer
->rx_buf
;
213 const u8
*tx
= xfer
->tx_buf
;
216 spi_debug("%s(%d): %s %s\n", __func__
, xfer
->len
,
217 (tx
!= NULL
) ? "tx" : " ",
218 (rx
!= NULL
) ? "rx" : " ");
221 for (count
= 0; count
< xfer
->len
; count
++) {
222 ramips_spi_write(rs
, RAMIPS_SPI_DATA
, tx
[count
]);
223 ramips_spi_setbits(rs
, RAMIPS_SPI_CTL
, SPICTL_STARTWR
);
224 err
= ramips_spi_wait_till_ready(rs
);
226 dev_err(&spi
->dev
, "TX failed, err=%d\n", err
);
233 for (count
= 0; count
< xfer
->len
; count
++) {
234 ramips_spi_setbits(rs
, RAMIPS_SPI_CTL
, SPICTL_STARTRD
);
235 err
= ramips_spi_wait_till_ready(rs
);
237 dev_err(&spi
->dev
, "RX failed, err=%d\n", err
);
240 rx
[count
] = (u8
) ramips_spi_read(rs
, RAMIPS_SPI_DATA
);
248 static void ramips_spi_work(struct work_struct
*work
)
250 struct ramips_spi
*rs
=
251 container_of(work
, struct ramips_spi
, work
);
253 spin_lock_irq(&rs
->lock
);
254 while (!list_empty(&rs
->msg_queue
)) {
255 struct spi_message
*m
;
256 struct spi_device
*spi
;
257 struct spi_transfer
*t
= NULL
;
258 int par_override
= 0;
262 m
= container_of(rs
->msg_queue
.next
, struct spi_message
,
265 list_del_init(&m
->queue
);
266 spin_unlock_irq(&rs
->lock
);
271 status
= ramips_spi_setup_transfer(spi
, NULL
);
276 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
277 if (par_override
|| t
->speed_hz
|| t
->bits_per_word
) {
279 status
= ramips_spi_setup_transfer(spi
, t
);
282 if (!t
->speed_hz
&& !t
->bits_per_word
)
287 ramips_spi_set_cs(rs
, 1);
293 ramips_spi_write_read(spi
, t
);
296 udelay(t
->delay_usecs
);
299 ramips_spi_set_cs(rs
, 0);
306 ramips_spi_set_cs(rs
, 0);
309 m
->complete(m
->context
);
311 spin_lock_irq(&rs
->lock
);
314 spin_unlock_irq(&rs
->lock
);
317 static int ramips_spi_setup(struct spi_device
*spi
)
319 struct ramips_spi
*rs
= ramips_spidev_to_rs(spi
);
321 if ((spi
->max_speed_hz
== 0) ||
322 (spi
->max_speed_hz
> (rs
->sys_freq
/ 2)))
323 spi
->max_speed_hz
= (rs
->sys_freq
/ 2);
325 if (spi
->max_speed_hz
< (rs
->sys_freq
/128)) {
326 dev_err(&spi
->dev
, "setup: requested speed too low %d Hz\n",
331 if (spi
->bits_per_word
!= 0 && spi
->bits_per_word
!= 8) {
333 "setup: requested bits per words - os wrong %d bpw\n",
338 if (spi
->bits_per_word
== 0)
339 spi
->bits_per_word
= 8;
342 * baudrate & width will be set ramips_spi_setup_transfer
347 static int ramips_spi_transfer(struct spi_device
*spi
, struct spi_message
*m
)
349 struct ramips_spi
*rs
;
350 struct spi_transfer
*t
= NULL
;
353 m
->actual_length
= 0;
356 /* reject invalid messages and transfers */
357 if (list_empty(&m
->transfers
) || !m
->complete
)
360 rs
= ramips_spidev_to_rs(spi
);
362 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
363 unsigned int bits_per_word
= spi
->bits_per_word
;
365 if (t
->tx_buf
== NULL
&& t
->rx_buf
== NULL
&& t
->len
) {
367 "message rejected : "
368 "invalid transfer data buffers\n");
372 if (t
->bits_per_word
)
373 bits_per_word
= t
->bits_per_word
;
375 if (bits_per_word
!= 8) {
377 "message rejected : "
378 "invalid transfer bits_per_word (%d bits)\n",
383 if (t
->speed_hz
&& t
->speed_hz
< (rs
->sys_freq
/128)) {
385 "message rejected : "
386 "device min speed (%d Hz) exceeds "
387 "required transfer speed (%d Hz)\n",
388 (rs
->sys_freq
/128), t
->speed_hz
);
394 spin_lock_irqsave(&rs
->lock
, flags
);
395 list_add_tail(&m
->queue
, &rs
->msg_queue
);
396 queue_work(ramips_spi_wq
, &rs
->work
);
397 spin_unlock_irqrestore(&rs
->lock
, flags
);
401 /* Message rejected and not queued */
404 m
->complete(m
->context
);
408 static void __init
ramips_spi_reset(struct ramips_spi
*rs
)
410 ramips_spi_write(rs
, RAMIPS_SPI_CFG
,
411 SPICFG_MSBFIRST
| SPICFG_TXCLKEDGE_FALLING
|
412 SPICFG_SPICLK_DIV16
| SPICFG_SPICLKPOL
);
413 ramips_spi_write(rs
, RAMIPS_SPI_CTL
, SPICTL_HIZSDO
| SPICTL_SPIENA
);
416 static int __init
ramips_spi_probe(struct platform_device
*pdev
)
418 struct spi_master
*master
;
419 struct ramips_spi
*rs
;
423 master
= spi_alloc_master(&pdev
->dev
, sizeof(*rs
));
424 if (master
== NULL
) {
425 dev_dbg(&pdev
->dev
, "master allocation failed\n");
430 master
->bus_num
= pdev
->id
;
432 /* we support only mode 0, and no options */
433 master
->mode_bits
= 0;
435 master
->setup
= ramips_spi_setup
;
436 master
->transfer
= ramips_spi_transfer
;
437 master
->num_chipselect
= RALINK_NUM_CHIPSELECTS
;
439 dev_set_drvdata(&pdev
->dev
, master
);
441 rs
= spi_master_get_devdata(master
);
444 rs
->clk
= clk_get(NULL
, "sys");
445 if (IS_ERR(rs
->clk
)) {
446 status
= PTR_ERR(rs
->clk
);
447 dev_err(&pdev
->dev
, "unable to get SYS clock, err=%d\n",
452 status
= clk_enable(rs
->clk
);
456 rs
->sys_freq
= clk_get_rate(rs
->clk
);
457 spi_debug("%s: sys_freq: %ld\n", __func__
, rs
->sys_freq
);
459 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
462 goto out_disable_clk
;
465 if (!request_mem_region(r
->start
, (r
->end
- r
->start
) + 1,
466 dev_name(&pdev
->dev
))) {
468 goto out_disable_clk
;
471 rs
->base
= ioremap(r
->start
, resource_size(r
));
472 if (rs
->base
== NULL
) {
473 dev_err(&pdev
->dev
, "ioremap failed\n");
478 INIT_WORK(&rs
->work
, ramips_spi_work
);
480 spin_lock_init(&rs
->lock
);
481 INIT_LIST_HEAD(&rs
->msg_queue
);
483 ramips_spi_reset(rs
);
485 status
= spi_register_master(master
);
494 release_mem_region(r
->start
, (r
->end
- r
->start
) + 1);
496 clk_disable(rs
->clk
);
500 spi_master_put(master
);
504 static int __devexit
ramips_spi_remove(struct platform_device
*pdev
)
506 struct spi_master
*master
;
507 struct ramips_spi
*rs
;
510 master
= dev_get_drvdata(&pdev
->dev
);
511 rs
= spi_master_get_devdata(master
);
513 cancel_work_sync(&rs
->work
);
516 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
517 release_mem_region(r
->start
, (r
->end
- r
->start
) + 1);
519 clk_disable(rs
->clk
);
521 spi_unregister_master(master
);
526 MODULE_ALIAS("platform:" DRIVER_NAME
);
528 static struct platform_driver ramips_spi_driver
= {
531 .owner
= THIS_MODULE
,
533 .remove
= __devexit_p(ramips_spi_remove
),
536 static int __init
ramips_spi_init(void)
538 ramips_spi_wq
= create_singlethread_workqueue(
539 ramips_spi_driver
.driver
.name
);
540 if (ramips_spi_wq
== NULL
)
543 return platform_driver_probe(&ramips_spi_driver
, ramips_spi_probe
);
545 module_init(ramips_spi_init
);
547 static void __exit
ramips_spi_exit(void)
549 flush_workqueue(ramips_spi_wq
);
550 platform_driver_unregister(&ramips_spi_driver
);
552 destroy_workqueue(ramips_spi_wq
);
554 module_exit(ramips_spi_exit
);
556 MODULE_DESCRIPTION("Ralink SPI driver");
557 MODULE_AUTHOR("Sergiy <piratfm@gmail.com>");
558 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
559 MODULE_LICENSE("GPL");