4 * Copyright (C) 2011 Renesas Solutions Corp.
6 * Based on pxa2xx_spi.c:
7 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/timer.h>
24 #include <linux/delay.h>
25 #include <linux/list.h>
26 #include <linux/workqueue.h>
27 #include <linux/interrupt.h>
28 #include <linux/platform_device.h>
30 #include <linux/spi/spi.h>
32 #define SPI_SH_TBR 0x00
33 #define SPI_SH_RBR 0x00
34 #define SPI_SH_CR1 0x08
35 #define SPI_SH_CR2 0x10
36 #define SPI_SH_CR3 0x18
37 #define SPI_SH_CR4 0x20
38 #define SPI_SH_CR5 0x28
41 #define SPI_SH_TBE 0x80
42 #define SPI_SH_TBF 0x40
43 #define SPI_SH_RBE 0x20
44 #define SPI_SH_RBF 0x10
45 #define SPI_SH_PFONRD 0x08
46 #define SPI_SH_SSDB 0x04
47 #define SPI_SH_SSD 0x02
48 #define SPI_SH_SSA 0x01
51 #define SPI_SH_RSTF 0x80
52 #define SPI_SH_LOOPBK 0x40
53 #define SPI_SH_CPOL 0x20
54 #define SPI_SH_CPHA 0x10
55 #define SPI_SH_L1M0 0x08
58 #define SPI_SH_MAX_BYTE 0xFF
61 #define SPI_SH_TBEI 0x80
62 #define SPI_SH_TBFI 0x40
63 #define SPI_SH_RBEI 0x20
64 #define SPI_SH_RBFI 0x10
65 #define SPI_SH_WPABRT 0x04
66 #define SPI_SH_SSS 0x01
69 #define SPI_SH_P1L0 0x80
70 #define SPI_SH_PP1L0 0x40
71 #define SPI_SH_MUXI 0x20
72 #define SPI_SH_MUXIRQ 0x10
74 #define SPI_SH_FIFO_SIZE 32
75 #define SPI_SH_SEND_TIMEOUT (3 * HZ)
76 #define SPI_SH_RECEIVE_TIMEOUT (HZ >> 3)
83 struct spi_master
*master
;
84 struct list_head queue
;
85 struct workqueue_struct
*workqueue
;
86 struct work_struct ws
;
88 wait_queue_head_t wait
;
93 static void spi_sh_write(struct spi_sh_data
*ss
, unsigned long data
,
97 iowrite8(data
, ss
->addr
+ (offset
>> 2));
98 else if (ss
->width
== 32)
99 iowrite32(data
, ss
->addr
+ offset
);
102 static unsigned long spi_sh_read(struct spi_sh_data
*ss
, unsigned long offset
)
105 return ioread8(ss
->addr
+ (offset
>> 2));
106 else if (ss
->width
== 32)
107 return ioread32(ss
->addr
+ offset
);
112 static void spi_sh_set_bit(struct spi_sh_data
*ss
, unsigned long val
,
113 unsigned long offset
)
117 tmp
= spi_sh_read(ss
, offset
);
119 spi_sh_write(ss
, tmp
, offset
);
122 static void spi_sh_clear_bit(struct spi_sh_data
*ss
, unsigned long val
,
123 unsigned long offset
)
127 tmp
= spi_sh_read(ss
, offset
);
129 spi_sh_write(ss
, tmp
, offset
);
132 static void clear_fifo(struct spi_sh_data
*ss
)
134 spi_sh_set_bit(ss
, SPI_SH_RSTF
, SPI_SH_CR2
);
135 spi_sh_clear_bit(ss
, SPI_SH_RSTF
, SPI_SH_CR2
);
138 static int spi_sh_wait_receive_buffer(struct spi_sh_data
*ss
)
140 int timeout
= 100000;
142 while (spi_sh_read(ss
, SPI_SH_CR1
) & SPI_SH_RBE
) {
150 static int spi_sh_wait_write_buffer_empty(struct spi_sh_data
*ss
)
152 int timeout
= 100000;
154 while (!(spi_sh_read(ss
, SPI_SH_CR1
) & SPI_SH_TBE
)) {
162 static int spi_sh_send(struct spi_sh_data
*ss
, struct spi_message
*mesg
,
163 struct spi_transfer
*t
)
172 spi_sh_set_bit(ss
, SPI_SH_SSA
, SPI_SH_CR1
);
174 data
= (unsigned char *)t
->tx_buf
;
176 cur_len
= min(SPI_SH_FIFO_SIZE
, remain
);
177 for (i
= 0; i
< cur_len
&&
178 !(spi_sh_read(ss
, SPI_SH_CR4
) &
180 !(spi_sh_read(ss
, SPI_SH_CR1
) & SPI_SH_TBF
);
182 spi_sh_write(ss
, (unsigned long)data
[i
], SPI_SH_TBR
);
184 if (spi_sh_read(ss
, SPI_SH_CR4
) & SPI_SH_WPABRT
) {
185 /* Abort SPI operation */
186 spi_sh_set_bit(ss
, SPI_SH_WPABRT
, SPI_SH_CR4
);
197 ss
->cr1
&= ~SPI_SH_TBE
;
198 spi_sh_set_bit(ss
, SPI_SH_TBE
, SPI_SH_CR4
);
199 ret
= wait_event_interruptible_timeout(ss
->wait
,
200 ss
->cr1
& SPI_SH_TBE
,
201 SPI_SH_SEND_TIMEOUT
);
202 if (ret
== 0 && !(ss
->cr1
& SPI_SH_TBE
)) {
203 printk(KERN_ERR
"%s: timeout\n", __func__
);
209 if (list_is_last(&t
->transfer_list
, &mesg
->transfers
)) {
210 spi_sh_clear_bit(ss
, SPI_SH_SSD
| SPI_SH_SSDB
, SPI_SH_CR1
);
211 spi_sh_set_bit(ss
, SPI_SH_SSA
, SPI_SH_CR1
);
213 ss
->cr1
&= ~SPI_SH_TBE
;
214 spi_sh_set_bit(ss
, SPI_SH_TBE
, SPI_SH_CR4
);
215 ret
= wait_event_interruptible_timeout(ss
->wait
,
216 ss
->cr1
& SPI_SH_TBE
,
217 SPI_SH_SEND_TIMEOUT
);
218 if (ret
== 0 && (ss
->cr1
& SPI_SH_TBE
)) {
219 printk(KERN_ERR
"%s: timeout\n", __func__
);
227 static int spi_sh_receive(struct spi_sh_data
*ss
, struct spi_message
*mesg
,
228 struct spi_transfer
*t
)
236 if (t
->len
> SPI_SH_MAX_BYTE
)
237 spi_sh_write(ss
, SPI_SH_MAX_BYTE
, SPI_SH_CR3
);
239 spi_sh_write(ss
, t
->len
, SPI_SH_CR3
);
241 spi_sh_clear_bit(ss
, SPI_SH_SSD
| SPI_SH_SSDB
, SPI_SH_CR1
);
242 spi_sh_set_bit(ss
, SPI_SH_SSA
, SPI_SH_CR1
);
244 spi_sh_wait_write_buffer_empty(ss
);
246 data
= (unsigned char *)t
->rx_buf
;
248 if (remain
>= SPI_SH_FIFO_SIZE
) {
249 ss
->cr1
&= ~SPI_SH_RBF
;
250 spi_sh_set_bit(ss
, SPI_SH_RBF
, SPI_SH_CR4
);
251 ret
= wait_event_interruptible_timeout(ss
->wait
,
252 ss
->cr1
& SPI_SH_RBF
,
253 SPI_SH_RECEIVE_TIMEOUT
);
255 spi_sh_read(ss
, SPI_SH_CR1
) & SPI_SH_RBE
) {
256 printk(KERN_ERR
"%s: timeout\n", __func__
);
261 cur_len
= min(SPI_SH_FIFO_SIZE
, remain
);
262 for (i
= 0; i
< cur_len
; i
++) {
263 if (spi_sh_wait_receive_buffer(ss
))
265 data
[i
] = (unsigned char)spi_sh_read(ss
, SPI_SH_RBR
);
272 /* deassert CS when SPI is receiving. */
273 if (t
->len
> SPI_SH_MAX_BYTE
) {
275 spi_sh_write(ss
, 1, SPI_SH_CR3
);
277 spi_sh_write(ss
, 0, SPI_SH_CR3
);
283 static void spi_sh_work(struct work_struct
*work
)
285 struct spi_sh_data
*ss
= container_of(work
, struct spi_sh_data
, ws
);
286 struct spi_message
*mesg
;
287 struct spi_transfer
*t
;
291 pr_debug("%s: enter\n", __func__
);
293 spin_lock_irqsave(&ss
->lock
, flags
);
294 while (!list_empty(&ss
->queue
)) {
295 mesg
= list_entry(ss
->queue
.next
, struct spi_message
, queue
);
296 list_del_init(&mesg
->queue
);
298 spin_unlock_irqrestore(&ss
->lock
, flags
);
299 list_for_each_entry(t
, &mesg
->transfers
, transfer_list
) {
300 pr_debug("tx_buf = %p, rx_buf = %p\n",
301 t
->tx_buf
, t
->rx_buf
);
302 pr_debug("len = %d, delay_usecs = %d\n",
303 t
->len
, t
->delay_usecs
);
306 ret
= spi_sh_send(ss
, mesg
, t
);
311 ret
= spi_sh_receive(ss
, mesg
, t
);
315 mesg
->actual_length
+= t
->len
;
317 spin_lock_irqsave(&ss
->lock
, flags
);
321 mesg
->complete(mesg
->context
);
325 spi_sh_set_bit(ss
, SPI_SH_SSD
, SPI_SH_CR1
);
328 spi_sh_clear_bit(ss
, SPI_SH_SSA
| SPI_SH_SSDB
| SPI_SH_SSD
,
333 spin_unlock_irqrestore(&ss
->lock
, flags
);
340 mesg
->complete(mesg
->context
);
342 spi_sh_clear_bit(ss
, SPI_SH_SSA
| SPI_SH_SSDB
| SPI_SH_SSD
,
348 static int spi_sh_setup(struct spi_device
*spi
)
350 struct spi_sh_data
*ss
= spi_master_get_devdata(spi
->master
);
352 pr_debug("%s: enter\n", __func__
);
354 spi_sh_write(ss
, 0xfe, SPI_SH_CR1
); /* SPI sycle stop */
355 spi_sh_write(ss
, 0x00, SPI_SH_CR1
); /* CR1 init */
356 spi_sh_write(ss
, 0x00, SPI_SH_CR3
); /* CR3 init */
361 spi_sh_write(ss
, spi_sh_read(ss
, SPI_SH_CR2
) | 0x07, SPI_SH_CR2
);
367 static int spi_sh_transfer(struct spi_device
*spi
, struct spi_message
*mesg
)
369 struct spi_sh_data
*ss
= spi_master_get_devdata(spi
->master
);
372 pr_debug("%s: enter\n", __func__
);
373 pr_debug("\tmode = %02x\n", spi
->mode
);
375 spin_lock_irqsave(&ss
->lock
, flags
);
377 mesg
->actual_length
= 0;
378 mesg
->status
= -EINPROGRESS
;
380 spi_sh_clear_bit(ss
, SPI_SH_SSA
, SPI_SH_CR1
);
382 list_add_tail(&mesg
->queue
, &ss
->queue
);
383 queue_work(ss
->workqueue
, &ss
->ws
);
385 spin_unlock_irqrestore(&ss
->lock
, flags
);
390 static void spi_sh_cleanup(struct spi_device
*spi
)
392 struct spi_sh_data
*ss
= spi_master_get_devdata(spi
->master
);
394 pr_debug("%s: enter\n", __func__
);
396 spi_sh_clear_bit(ss
, SPI_SH_SSA
| SPI_SH_SSDB
| SPI_SH_SSD
,
400 static irqreturn_t
spi_sh_irq(int irq
, void *_ss
)
402 struct spi_sh_data
*ss
= (struct spi_sh_data
*)_ss
;
405 cr1
= spi_sh_read(ss
, SPI_SH_CR1
);
406 if (cr1
& SPI_SH_TBE
)
407 ss
->cr1
|= SPI_SH_TBE
;
408 if (cr1
& SPI_SH_TBF
)
409 ss
->cr1
|= SPI_SH_TBF
;
410 if (cr1
& SPI_SH_RBE
)
411 ss
->cr1
|= SPI_SH_RBE
;
412 if (cr1
& SPI_SH_RBF
)
413 ss
->cr1
|= SPI_SH_RBF
;
416 spi_sh_clear_bit(ss
, ss
->cr1
, SPI_SH_CR4
);
423 static int spi_sh_remove(struct platform_device
*pdev
)
425 struct spi_sh_data
*ss
= platform_get_drvdata(pdev
);
427 spi_unregister_master(ss
->master
);
428 destroy_workqueue(ss
->workqueue
);
429 free_irq(ss
->irq
, ss
);
434 static int spi_sh_probe(struct platform_device
*pdev
)
436 struct resource
*res
;
437 struct spi_master
*master
;
438 struct spi_sh_data
*ss
;
442 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
443 if (unlikely(res
== NULL
)) {
444 dev_err(&pdev
->dev
, "invalid resource\n");
448 irq
= platform_get_irq(pdev
, 0);
450 dev_err(&pdev
->dev
, "platform_get_irq error\n");
454 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct spi_sh_data
));
455 if (master
== NULL
) {
456 dev_err(&pdev
->dev
, "spi_alloc_master error.\n");
460 ss
= spi_master_get_devdata(master
);
461 platform_set_drvdata(pdev
, ss
);
463 switch (res
->flags
& IORESOURCE_MEM_TYPE_MASK
) {
464 case IORESOURCE_MEM_8BIT
:
467 case IORESOURCE_MEM_32BIT
:
471 dev_err(&pdev
->dev
, "No support width\n");
477 ss
->addr
= devm_ioremap(&pdev
->dev
, res
->start
, resource_size(res
));
478 if (ss
->addr
== NULL
) {
479 dev_err(&pdev
->dev
, "ioremap error.\n");
483 INIT_LIST_HEAD(&ss
->queue
);
484 spin_lock_init(&ss
->lock
);
485 INIT_WORK(&ss
->ws
, spi_sh_work
);
486 init_waitqueue_head(&ss
->wait
);
487 ss
->workqueue
= create_singlethread_workqueue(
488 dev_name(master
->dev
.parent
));
489 if (ss
->workqueue
== NULL
) {
490 dev_err(&pdev
->dev
, "create workqueue error\n");
495 ret
= request_irq(irq
, spi_sh_irq
, 0, "spi_sh", ss
);
497 dev_err(&pdev
->dev
, "request_irq error\n");
501 master
->num_chipselect
= 2;
502 master
->bus_num
= pdev
->id
;
503 master
->setup
= spi_sh_setup
;
504 master
->transfer
= spi_sh_transfer
;
505 master
->cleanup
= spi_sh_cleanup
;
507 ret
= spi_register_master(master
);
509 printk(KERN_ERR
"spi_register_master error.\n");
518 destroy_workqueue(ss
->workqueue
);
520 spi_master_put(master
);
525 static struct platform_driver spi_sh_driver
= {
526 .probe
= spi_sh_probe
,
527 .remove
= spi_sh_remove
,
532 module_platform_driver(spi_sh_driver
);
534 MODULE_DESCRIPTION("SH SPI bus driver");
535 MODULE_LICENSE("GPL");
536 MODULE_AUTHOR("Yoshihiro Shimoda");
537 MODULE_ALIAS("platform:sh_spi");