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/clk.h>
14 #include <linux/err.h>
15 #include <linux/delay.h>
16 #include <linux/platform_device.h>
18 #include <linux/spi/spi.h>
20 #define DRIVER_NAME "ramips-spi"
21 #define RALINK_NUM_CHIPSELECTS 1 /* only one slave is supported*/
22 #define RALINK_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
24 #define RAMIPS_SPI_STAT 0x00
25 #define RAMIPS_SPI_CFG 0x10
26 #define RAMIPS_SPI_CTL 0x14
27 #define RAMIPS_SPI_DATA 0x20
29 /* SPISTAT register bit field */
30 #define SPISTAT_BUSY BIT(0)
32 /* SPICFG register bit field */
33 #define SPICFG_LSBFIRST 0
34 #define SPICFG_MSBFIRST BIT(8)
35 #define SPICFG_SPICLKPOL BIT(6)
36 #define SPICFG_RXCLKEDGE_FALLING BIT(5)
37 #define SPICFG_TXCLKEDGE_FALLING BIT(4)
38 #define SPICFG_SPICLK_PRESCALE_MASK 0x7
39 #define SPICFG_SPICLK_DIV2 0
40 #define SPICFG_SPICLK_DIV4 1
41 #define SPICFG_SPICLK_DIV8 2
42 #define SPICFG_SPICLK_DIV16 3
43 #define SPICFG_SPICLK_DIV32 4
44 #define SPICFG_SPICLK_DIV64 5
45 #define SPICFG_SPICLK_DIV128 6
46 #define SPICFG_SPICLK_DISABLE 7
48 /* SPICTL register bit field */
49 #define SPICTL_HIZSDO BIT(3)
50 #define SPICTL_STARTWR BIT(2)
51 #define SPICTL_STARTRD BIT(1)
52 #define SPICTL_SPIENA BIT(0)
55 #define spi_debug(args...) printk(args)
57 #define spi_debug(args...)
61 struct work_struct work
;
63 /* Lock access to transfer list.*/
66 struct list_head msg_queue
;
67 struct spi_master
*master
;
69 unsigned int sys_freq
;
75 static struct workqueue_struct
*ramips_spi_wq
;
77 static inline struct ramips_spi
*ramips_spidev_to_rs(struct spi_device
*spi
)
79 return spi_master_get_devdata(spi
->master
);
82 static inline u32
ramips_spi_read(struct ramips_spi
*rs
, u32 reg
)
84 return ioread32(rs
->base
+ reg
);
87 static inline void ramips_spi_write(struct ramips_spi
*rs
, u32 reg
, u32 val
)
89 iowrite32(val
, rs
->base
+ reg
);
92 static inline void ramips_spi_setbits(struct ramips_spi
*rs
, u32 reg
, u32 mask
)
94 void __iomem
*addr
= rs
->base
+ reg
;
102 static inline void ramips_spi_clrbits(struct ramips_spi
*rs
, u32 reg
, u32 mask
)
104 void __iomem
*addr
= rs
->base
+ reg
;
107 val
= ioread32(addr
);
109 iowrite32(val
, addr
);
112 static int ramips_spi_baudrate_set(struct spi_device
*spi
, unsigned int speed
)
114 struct ramips_spi
*rs
= ramips_spidev_to_rs(spi
);
119 spi_debug("%s: speed:%u\n", __func__
, speed
);
122 * the supported rates are: 2,4,8...128
123 * round up as we look for equal or less speed
125 rate
= DIV_ROUND_UP(rs
->sys_freq
, speed
);
126 spi_debug("%s: rate-1:%u\n", __func__
, rate
);
127 rate
= roundup_pow_of_two(rate
);
128 spi_debug("%s: rate-2:%u\n", __func__
, rate
);
130 /* check if requested speed is too small */
137 /* Convert the rate to SPI clock divisor value. */
138 prescale
= ilog2(rate
/2);
139 spi_debug("%s: prescale:%u\n", __func__
, prescale
);
141 reg
= ramips_spi_read(rs
, RAMIPS_SPI_CFG
);
142 reg
= ((reg
& ~SPICFG_SPICLK_PRESCALE_MASK
) | prescale
);
143 ramips_spi_write(rs
, RAMIPS_SPI_CFG
, reg
);
149 * called only when no transfer is active on the bus
152 ramips_spi_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
154 struct ramips_spi
*rs
= ramips_spidev_to_rs(spi
);
155 unsigned int speed
= spi
->max_speed_hz
;
157 unsigned int bits_per_word
= 8;
159 if ((t
!= NULL
) && t
->speed_hz
)
162 if ((t
!= NULL
) && t
->bits_per_word
)
163 bits_per_word
= t
->bits_per_word
;
165 if (rs
->speed
!= speed
) {
166 spi_debug("%s: speed_hz:%u\n", __func__
, speed
);
167 rc
= ramips_spi_baudrate_set(spi
, speed
);
172 if (bits_per_word
!= 8) {
173 spi_debug("%s: bad bits_per_word: %u\n", __func__
,
181 static void ramips_spi_set_cs(struct ramips_spi
*rs
, int enable
)
184 ramips_spi_clrbits(rs
, RAMIPS_SPI_CTL
, SPICTL_SPIENA
);
186 ramips_spi_setbits(rs
, RAMIPS_SPI_CTL
, SPICTL_SPIENA
);
189 static inline int ramips_spi_wait_till_ready(struct ramips_spi
*rs
)
193 for (i
= 0; i
< RALINK_SPI_WAIT_RDY_MAX_LOOP
; i
++) {
196 status
= ramips_spi_read(rs
, RAMIPS_SPI_STAT
);
197 if ((status
& SPISTAT_BUSY
) == 0)
207 ramips_spi_write_read(struct spi_device
*spi
, struct spi_transfer
*xfer
)
209 struct ramips_spi
*rs
= ramips_spidev_to_rs(spi
);
211 u8
*rx
= xfer
->rx_buf
;
212 const u8
*tx
= xfer
->tx_buf
;
215 spi_debug("%s(%d): %s %s\n", __func__
, xfer
->len
,
216 (tx
!= NULL
) ? "tx" : " ",
217 (rx
!= NULL
) ? "rx" : " ");
220 for (count
= 0; count
< xfer
->len
; count
++) {
221 ramips_spi_write(rs
, RAMIPS_SPI_DATA
, tx
[count
]);
222 ramips_spi_setbits(rs
, RAMIPS_SPI_CTL
, SPICTL_STARTWR
);
223 err
= ramips_spi_wait_till_ready(rs
);
225 dev_err(&spi
->dev
, "TX failed, err=%d\n", err
);
232 for (count
= 0; count
< xfer
->len
; count
++) {
233 ramips_spi_setbits(rs
, RAMIPS_SPI_CTL
, SPICTL_STARTRD
);
234 err
= ramips_spi_wait_till_ready(rs
);
236 dev_err(&spi
->dev
, "RX failed, err=%d\n", err
);
239 rx
[count
] = (u8
) ramips_spi_read(rs
, RAMIPS_SPI_DATA
);
247 static void ramips_spi_work(struct work_struct
*work
)
249 struct ramips_spi
*rs
=
250 container_of(work
, struct ramips_spi
, work
);
252 spin_lock_irq(&rs
->lock
);
253 while (!list_empty(&rs
->msg_queue
)) {
254 struct spi_message
*m
;
255 struct spi_device
*spi
;
256 struct spi_transfer
*t
= NULL
;
257 int par_override
= 0;
261 m
= container_of(rs
->msg_queue
.next
, struct spi_message
,
264 list_del_init(&m
->queue
);
265 spin_unlock_irq(&rs
->lock
);
270 status
= ramips_spi_setup_transfer(spi
, NULL
);
275 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
276 if (par_override
|| t
->speed_hz
|| t
->bits_per_word
) {
278 status
= ramips_spi_setup_transfer(spi
, t
);
281 if (!t
->speed_hz
&& !t
->bits_per_word
)
286 ramips_spi_set_cs(rs
, 1);
292 ramips_spi_write_read(spi
, t
);
295 udelay(t
->delay_usecs
);
298 ramips_spi_set_cs(rs
, 0);
305 ramips_spi_set_cs(rs
, 0);
308 m
->complete(m
->context
);
310 spin_lock_irq(&rs
->lock
);
313 spin_unlock_irq(&rs
->lock
);
316 static int ramips_spi_setup(struct spi_device
*spi
)
318 struct ramips_spi
*rs
= ramips_spidev_to_rs(spi
);
320 if ((spi
->max_speed_hz
== 0) ||
321 (spi
->max_speed_hz
> (rs
->sys_freq
/ 2)))
322 spi
->max_speed_hz
= (rs
->sys_freq
/ 2);
324 if (spi
->max_speed_hz
< (rs
->sys_freq
/128)) {
325 dev_err(&spi
->dev
, "setup: requested speed too low %d Hz\n",
330 if (spi
->bits_per_word
!= 0 && spi
->bits_per_word
!= 8) {
332 "setup: requested bits per words - os wrong %d bpw\n",
337 if (spi
->bits_per_word
== 0)
338 spi
->bits_per_word
= 8;
341 * baudrate & width will be set ramips_spi_setup_transfer
346 static int ramips_spi_transfer(struct spi_device
*spi
, struct spi_message
*m
)
348 struct ramips_spi
*rs
;
349 struct spi_transfer
*t
= NULL
;
352 m
->actual_length
= 0;
355 /* reject invalid messages and transfers */
356 if (list_empty(&m
->transfers
) || !m
->complete
)
359 rs
= ramips_spidev_to_rs(spi
);
361 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
362 unsigned int bits_per_word
= spi
->bits_per_word
;
364 if (t
->tx_buf
== NULL
&& t
->rx_buf
== NULL
&& t
->len
) {
366 "message rejected : "
367 "invalid transfer data buffers\n");
371 if (t
->bits_per_word
)
372 bits_per_word
= t
->bits_per_word
;
374 if (bits_per_word
!= 8) {
376 "message rejected : "
377 "invalid transfer bits_per_word (%d bits)\n",
382 if (t
->speed_hz
&& t
->speed_hz
< (rs
->sys_freq
/128)) {
384 "message rejected : "
385 "device min speed (%d Hz) exceeds "
386 "required transfer speed (%d Hz)\n",
387 (rs
->sys_freq
/128), t
->speed_hz
);
393 spin_lock_irqsave(&rs
->lock
, flags
);
394 list_add_tail(&m
->queue
, &rs
->msg_queue
);
395 queue_work(ramips_spi_wq
, &rs
->work
);
396 spin_unlock_irqrestore(&rs
->lock
, flags
);
400 /* Message rejected and not queued */
403 m
->complete(m
->context
);
407 static void __init
ramips_spi_reset(struct ramips_spi
*rs
)
409 ramips_spi_write(rs
, RAMIPS_SPI_CFG
,
410 SPICFG_MSBFIRST
| SPICFG_TXCLKEDGE_FALLING
|
411 SPICFG_SPICLK_DIV16
| SPICFG_SPICLKPOL
);
412 ramips_spi_write(rs
, RAMIPS_SPI_CTL
, SPICTL_HIZSDO
| SPICTL_SPIENA
);
415 static int __init
ramips_spi_probe(struct platform_device
*pdev
)
417 struct spi_master
*master
;
418 struct ramips_spi
*rs
;
422 master
= spi_alloc_master(&pdev
->dev
, sizeof(*rs
));
423 if (master
== NULL
) {
424 dev_dbg(&pdev
->dev
, "master allocation failed\n");
429 master
->bus_num
= pdev
->id
;
431 /* we support only mode 0, and no options */
432 master
->mode_bits
= 0;
434 master
->setup
= ramips_spi_setup
;
435 master
->transfer
= ramips_spi_transfer
;
436 master
->num_chipselect
= RALINK_NUM_CHIPSELECTS
;
438 dev_set_drvdata(&pdev
->dev
, master
);
440 rs
= spi_master_get_devdata(master
);
443 rs
->clk
= clk_get(NULL
, "sys");
444 if (IS_ERR(rs
->clk
)) {
445 status
= PTR_ERR(rs
->clk
);
446 dev_err(&pdev
->dev
, "unable to get SYS clock, err=%d\n",
451 status
= clk_enable(rs
->clk
);
455 rs
->sys_freq
= clk_get_rate(rs
->clk
);
456 spi_debug("%s: sys_freq: %ld\n", __func__
, rs
->sys_freq
);
458 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
461 goto out_disable_clk
;
464 if (!request_mem_region(r
->start
, (r
->end
- r
->start
) + 1,
465 dev_name(&pdev
->dev
))) {
467 goto out_disable_clk
;
470 rs
->base
= ioremap(r
->start
, resource_size(r
));
471 if (rs
->base
== NULL
) {
472 dev_err(&pdev
->dev
, "ioremap failed\n");
477 INIT_WORK(&rs
->work
, ramips_spi_work
);
479 spin_lock_init(&rs
->lock
);
480 INIT_LIST_HEAD(&rs
->msg_queue
);
482 ramips_spi_reset(rs
);
484 status
= spi_register_master(master
);
493 release_mem_region(r
->start
, (r
->end
- r
->start
) + 1);
495 clk_disable(rs
->clk
);
499 spi_master_put(master
);
503 static int __devexit
ramips_spi_remove(struct platform_device
*pdev
)
505 struct spi_master
*master
;
506 struct ramips_spi
*rs
;
509 master
= dev_get_drvdata(&pdev
->dev
);
510 rs
= spi_master_get_devdata(master
);
512 cancel_work_sync(&rs
->work
);
515 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
516 release_mem_region(r
->start
, (r
->end
- r
->start
) + 1);
518 clk_disable(rs
->clk
);
520 spi_unregister_master(master
);
525 MODULE_ALIAS("platform:" DRIVER_NAME
);
527 static struct platform_driver ramips_spi_driver
= {
530 .owner
= THIS_MODULE
,
532 .remove
= __devexit_p(ramips_spi_remove
),
535 static int __init
ramips_spi_init(void)
537 ramips_spi_wq
= create_singlethread_workqueue(
538 ramips_spi_driver
.driver
.name
);
539 if (ramips_spi_wq
== NULL
)
542 return platform_driver_probe(&ramips_spi_driver
, ramips_spi_probe
);
544 module_init(ramips_spi_init
);
546 static void __exit
ramips_spi_exit(void)
548 flush_workqueue(ramips_spi_wq
);
549 platform_driver_unregister(&ramips_spi_driver
);
551 destroy_workqueue(ramips_spi_wq
);
553 module_exit(ramips_spi_exit
);
555 MODULE_DESCRIPTION("Ralink SPI driver");
556 MODULE_AUTHOR("Sergiy <piratfm@gmail.com>");
557 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
558 MODULE_LICENSE("GPL");