2 * spi_txx9.c - TXx9 SPI controller driver.
4 * Based on linux/arch/mips/tx4938/toshiba_rbtx4938/spi_txx9.c
5 * Copyright (C) 2000-2001 Toshiba Corporation
7 * 2003-2005 (c) MontaVista Software, Inc. This file is licensed under the
8 * terms of the GNU General Public License version 2. This program is
9 * licensed "as is" without any warranty of any kind, whether express
12 * Support for TX4938 in 2.6 - Manish Lachwani (mlachwani@mvista.com)
14 * Convert to generic SPI framework - Atsushi Nemoto (anemo@mba.ocn.ne.jp)
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched.h>
22 #include <linux/spinlock.h>
23 #include <linux/workqueue.h>
24 #include <linux/spi/spi.h>
25 #include <linux/err.h>
26 #include <linux/clk.h>
31 #define SPI_FIFO_SIZE 4
33 #define TXx9_SPMCR 0x00
34 #define TXx9_SPCR0 0x04
35 #define TXx9_SPCR1 0x08
36 #define TXx9_SPFS 0x0c
37 #define TXx9_SPSR 0x14
38 #define TXx9_SPDR 0x18
40 /* SPMCR : SPI Master Control */
41 #define TXx9_SPMCR_OPMODE 0xc0
42 #define TXx9_SPMCR_CONFIG 0x40
43 #define TXx9_SPMCR_ACTIVE 0x80
44 #define TXx9_SPMCR_SPSTP 0x02
45 #define TXx9_SPMCR_BCLR 0x01
47 /* SPCR0 : SPI Control 0 */
48 #define TXx9_SPCR0_TXIFL_MASK 0xc000
49 #define TXx9_SPCR0_RXIFL_MASK 0x3000
50 #define TXx9_SPCR0_SIDIE 0x0800
51 #define TXx9_SPCR0_SOEIE 0x0400
52 #define TXx9_SPCR0_RBSIE 0x0200
53 #define TXx9_SPCR0_TBSIE 0x0100
54 #define TXx9_SPCR0_IFSPSE 0x0010
55 #define TXx9_SPCR0_SBOS 0x0004
56 #define TXx9_SPCR0_SPHA 0x0002
57 #define TXx9_SPCR0_SPOL 0x0001
59 /* SPSR : SPI Status */
60 #define TXx9_SPSR_TBSI 0x8000
61 #define TXx9_SPSR_RBSI 0x4000
62 #define TXx9_SPSR_TBS_MASK 0x3800
63 #define TXx9_SPSR_RBS_MASK 0x0700
64 #define TXx9_SPSR_SPOE 0x0080
65 #define TXx9_SPSR_IFSD 0x0008
66 #define TXx9_SPSR_SIDLE 0x0004
67 #define TXx9_SPSR_STRDY 0x0002
68 #define TXx9_SPSR_SRRDY 0x0001
72 struct workqueue_struct
*workqueue
;
73 struct work_struct work
;
74 spinlock_t lock
; /* protect 'queue' */
75 struct list_head queue
;
76 wait_queue_head_t waitq
;
77 void __iomem
*membase
;
80 u32 max_speed_hz
, min_speed_hz
;
82 int last_chipselect_val
;
85 static u32
txx9spi_rd(struct txx9spi
*c
, int reg
)
87 return __raw_readl(c
->membase
+ reg
);
89 static void txx9spi_wr(struct txx9spi
*c
, u32 val
, int reg
)
91 __raw_writel(val
, c
->membase
+ reg
);
94 static void txx9spi_cs_func(struct spi_device
*spi
, struct txx9spi
*c
,
95 int on
, unsigned int cs_delay
)
97 int val
= (spi
->mode
& SPI_CS_HIGH
) ? on
: !on
;
99 /* deselect the chip with cs_change hint in last transfer */
100 if (c
->last_chipselect
>= 0)
101 gpio_set_value(c
->last_chipselect
,
102 !c
->last_chipselect_val
);
103 c
->last_chipselect
= spi
->chip_select
;
104 c
->last_chipselect_val
= val
;
106 c
->last_chipselect
= -1;
107 ndelay(cs_delay
); /* CS Hold Time */
109 gpio_set_value(spi
->chip_select
, val
);
110 ndelay(cs_delay
); /* CS Setup Time / CS Recovery Time */
113 static int txx9spi_setup(struct spi_device
*spi
)
115 struct txx9spi
*c
= spi_master_get_devdata(spi
->master
);
118 if (!spi
->max_speed_hz
119 || spi
->max_speed_hz
> c
->max_speed_hz
120 || spi
->max_speed_hz
< c
->min_speed_hz
)
123 bits_per_word
= spi
->bits_per_word
;
124 if (bits_per_word
!= 8 && bits_per_word
!= 16)
127 if (gpio_direction_output(spi
->chip_select
,
128 !(spi
->mode
& SPI_CS_HIGH
))) {
129 dev_err(&spi
->dev
, "Cannot setup GPIO for chipselect.\n");
135 txx9spi_cs_func(spi
, c
, 0, (NSEC_PER_SEC
/ 2) / spi
->max_speed_hz
);
136 spin_unlock(&c
->lock
);
141 static irqreturn_t
txx9spi_interrupt(int irq
, void *dev_id
)
143 struct txx9spi
*c
= dev_id
;
145 /* disable rx intr */
146 txx9spi_wr(c
, txx9spi_rd(c
, TXx9_SPCR0
) & ~TXx9_SPCR0_RBSIE
,
152 static void txx9spi_work_one(struct txx9spi
*c
, struct spi_message
*m
)
154 struct spi_device
*spi
= m
->spi
;
155 struct spi_transfer
*t
;
156 unsigned int cs_delay
;
157 unsigned int cs_change
= 1;
160 u32 prev_speed_hz
= 0;
161 u8 prev_bits_per_word
= 0;
163 /* CS setup/hold/recovery time in nsec */
164 cs_delay
= 100 + (NSEC_PER_SEC
/ 2) / spi
->max_speed_hz
;
166 mcr
= txx9spi_rd(c
, TXx9_SPMCR
);
167 if (unlikely((mcr
& TXx9_SPMCR_OPMODE
) == TXx9_SPMCR_ACTIVE
)) {
168 dev_err(&spi
->dev
, "Bad mode.\n");
172 mcr
&= ~(TXx9_SPMCR_OPMODE
| TXx9_SPMCR_SPSTP
| TXx9_SPMCR_BCLR
);
174 /* enter config mode */
175 txx9spi_wr(c
, mcr
| TXx9_SPMCR_CONFIG
| TXx9_SPMCR_BCLR
, TXx9_SPMCR
);
176 txx9spi_wr(c
, TXx9_SPCR0_SBOS
177 | ((spi
->mode
& SPI_CPOL
) ? TXx9_SPCR0_SPOL
: 0)
178 | ((spi
->mode
& SPI_CPHA
) ? TXx9_SPCR0_SPHA
: 0)
182 list_for_each_entry (t
, &m
->transfers
, transfer_list
) {
183 const void *txbuf
= t
->tx_buf
;
184 void *rxbuf
= t
->rx_buf
;
186 unsigned int len
= t
->len
;
188 u32 speed_hz
= t
->speed_hz
? : spi
->max_speed_hz
;
189 u8 bits_per_word
= t
->bits_per_word
? : spi
->bits_per_word
;
191 bits_per_word
= bits_per_word
? : 8;
192 wsize
= bits_per_word
>> 3; /* in bytes */
194 if (prev_speed_hz
!= speed_hz
195 || prev_bits_per_word
!= bits_per_word
) {
196 u32 n
= (c
->baseclk
+ speed_hz
- 1) / speed_hz
;
201 /* enter config mode */
202 txx9spi_wr(c
, mcr
| TXx9_SPMCR_CONFIG
| TXx9_SPMCR_BCLR
,
204 txx9spi_wr(c
, (n
<< 8) | bits_per_word
, TXx9_SPCR1
);
205 /* enter active mode */
206 txx9spi_wr(c
, mcr
| TXx9_SPMCR_ACTIVE
, TXx9_SPMCR
);
208 prev_speed_hz
= speed_hz
;
209 prev_bits_per_word
= bits_per_word
;
213 txx9spi_cs_func(spi
, c
, 1, cs_delay
);
214 cs_change
= t
->cs_change
;
216 unsigned int count
= SPI_FIFO_SIZE
;
220 if (len
< count
* wsize
)
222 /* now tx must be idle... */
223 while (!(txx9spi_rd(c
, TXx9_SPSR
) & TXx9_SPSR_SIDLE
))
225 cr0
= txx9spi_rd(c
, TXx9_SPCR0
);
226 cr0
&= ~TXx9_SPCR0_RXIFL_MASK
;
227 cr0
|= (count
- 1) << 12;
229 cr0
|= TXx9_SPCR0_RBSIE
;
230 txx9spi_wr(c
, cr0
, TXx9_SPCR0
);
232 for (i
= 0; i
< count
; i
++) {
236 : *(const u16
*)txbuf
;
237 txx9spi_wr(c
, data
, TXx9_SPDR
);
240 txx9spi_wr(c
, 0, TXx9_SPDR
);
242 /* wait all rx data */
244 txx9spi_rd(c
, TXx9_SPSR
) & TXx9_SPSR_RBSI
);
246 for (i
= 0; i
< count
; i
++) {
247 data
= txx9spi_rd(c
, TXx9_SPDR
);
252 *(u16
*)rxbuf
= data
;
256 len
-= count
* wsize
;
258 m
->actual_length
+= t
->len
;
260 udelay(t
->delay_usecs
);
264 if (t
->transfer_list
.next
== &m
->transfers
)
266 /* sometimes a short mid-message deselect of the chip
267 * may be needed to terminate a mode or command
269 txx9spi_cs_func(spi
, c
, 0, cs_delay
);
274 m
->complete(m
->context
);
276 /* normally deactivate chipselect ... unless no error and
277 * cs_change has hinted that the next message will probably
278 * be for this chip too.
280 if (!(status
== 0 && cs_change
))
281 txx9spi_cs_func(spi
, c
, 0, cs_delay
);
283 /* enter config mode */
284 txx9spi_wr(c
, mcr
| TXx9_SPMCR_CONFIG
| TXx9_SPMCR_BCLR
, TXx9_SPMCR
);
287 static void txx9spi_work(struct work_struct
*work
)
289 struct txx9spi
*c
= container_of(work
, struct txx9spi
, work
);
292 spin_lock_irqsave(&c
->lock
, flags
);
293 while (!list_empty(&c
->queue
)) {
294 struct spi_message
*m
;
296 m
= container_of(c
->queue
.next
, struct spi_message
, queue
);
297 list_del_init(&m
->queue
);
298 spin_unlock_irqrestore(&c
->lock
, flags
);
300 txx9spi_work_one(c
, m
);
302 spin_lock_irqsave(&c
->lock
, flags
);
304 spin_unlock_irqrestore(&c
->lock
, flags
);
307 static int txx9spi_transfer(struct spi_device
*spi
, struct spi_message
*m
)
309 struct spi_master
*master
= spi
->master
;
310 struct txx9spi
*c
= spi_master_get_devdata(master
);
311 struct spi_transfer
*t
;
314 m
->actual_length
= 0;
316 /* check each transfer's parameters */
317 list_for_each_entry (t
, &m
->transfers
, transfer_list
) {
318 u32 speed_hz
= t
->speed_hz
? : spi
->max_speed_hz
;
319 u8 bits_per_word
= t
->bits_per_word
? : spi
->bits_per_word
;
321 bits_per_word
= bits_per_word
? : 8;
322 if (!t
->tx_buf
&& !t
->rx_buf
&& t
->len
)
324 if (bits_per_word
!= 8 && bits_per_word
!= 16)
326 if (t
->len
& ((bits_per_word
>> 3) - 1))
328 if (speed_hz
< c
->min_speed_hz
|| speed_hz
> c
->max_speed_hz
)
332 spin_lock_irqsave(&c
->lock
, flags
);
333 list_add_tail(&m
->queue
, &c
->queue
);
334 queue_work(c
->workqueue
, &c
->work
);
335 spin_unlock_irqrestore(&c
->lock
, flags
);
340 static int __init
txx9spi_probe(struct platform_device
*dev
)
342 struct spi_master
*master
;
344 struct resource
*res
;
349 master
= spi_alloc_master(&dev
->dev
, sizeof(*c
));
352 c
= spi_master_get_devdata(master
);
353 platform_set_drvdata(dev
, master
);
355 INIT_WORK(&c
->work
, txx9spi_work
);
356 spin_lock_init(&c
->lock
);
357 INIT_LIST_HEAD(&c
->queue
);
358 init_waitqueue_head(&c
->waitq
);
360 c
->clk
= clk_get(&dev
->dev
, "spi-baseclk");
361 if (IS_ERR(c
->clk
)) {
362 ret
= PTR_ERR(c
->clk
);
366 ret
= clk_enable(c
->clk
);
372 c
->baseclk
= clk_get_rate(c
->clk
);
373 c
->min_speed_hz
= (c
->baseclk
+ 0xff - 1) / 0xff;
374 c
->max_speed_hz
= c
->baseclk
;
376 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
379 if (!devm_request_mem_region(&dev
->dev
,
380 res
->start
, res
->end
- res
->start
+ 1,
383 c
->membase
= devm_ioremap(&dev
->dev
,
384 res
->start
, res
->end
- res
->start
+ 1);
388 /* enter config mode */
389 mcr
= txx9spi_rd(c
, TXx9_SPMCR
);
390 mcr
&= ~(TXx9_SPMCR_OPMODE
| TXx9_SPMCR_SPSTP
| TXx9_SPMCR_BCLR
);
391 txx9spi_wr(c
, mcr
| TXx9_SPMCR_CONFIG
| TXx9_SPMCR_BCLR
, TXx9_SPMCR
);
393 irq
= platform_get_irq(dev
, 0);
396 ret
= devm_request_irq(&dev
->dev
, irq
, txx9spi_interrupt
, 0,
401 c
->workqueue
= create_singlethread_workqueue(
402 dev_name(master
->dev
.parent
));
405 c
->last_chipselect
= -1;
407 dev_info(&dev
->dev
, "at %#llx, irq %d, %dMHz\n",
408 (unsigned long long)res
->start
, irq
,
409 (c
->baseclk
+ 500000) / 1000000);
411 /* the spi->mode bits understood by this driver: */
412 master
->mode_bits
= SPI_CS_HIGH
| SPI_CPOL
| SPI_CPHA
;
414 master
->bus_num
= dev
->id
;
415 master
->setup
= txx9spi_setup
;
416 master
->transfer
= txx9spi_transfer
;
417 master
->num_chipselect
= (u16
)UINT_MAX
; /* any GPIO numbers */
419 ret
= spi_register_master(master
);
427 destroy_workqueue(c
->workqueue
);
432 platform_set_drvdata(dev
, NULL
);
433 spi_master_put(master
);
437 static int __exit
txx9spi_remove(struct platform_device
*dev
)
439 struct spi_master
*master
= spi_master_get(platform_get_drvdata(dev
));
440 struct txx9spi
*c
= spi_master_get_devdata(master
);
442 spi_unregister_master(master
);
443 platform_set_drvdata(dev
, NULL
);
444 destroy_workqueue(c
->workqueue
);
447 spi_master_put(master
);
451 /* work with hotplug and coldplug */
452 MODULE_ALIAS("platform:spi_txx9");
454 static struct platform_driver txx9spi_driver
= {
455 .remove
= __exit_p(txx9spi_remove
),
458 .owner
= THIS_MODULE
,
462 static int __init
txx9spi_init(void)
464 return platform_driver_probe(&txx9spi_driver
, txx9spi_probe
);
466 subsys_initcall(txx9spi_init
);
468 static void __exit
txx9spi_exit(void)
470 platform_driver_unregister(&txx9spi_driver
);
472 module_exit(txx9spi_exit
);
474 MODULE_DESCRIPTION("TXx9 SPI Driver");
475 MODULE_LICENSE("GPL");