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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/errno.h>
28 #include <linux/timer.h>
29 #include <linux/delay.h>
30 #include <linux/list.h>
31 #include <linux/workqueue.h>
32 #include <linux/interrupt.h>
33 #include <linux/platform_device.h>
35 #include <linux/spi/spi.h>
37 #define SPI_SH_TBR 0x00
38 #define SPI_SH_RBR 0x00
39 #define SPI_SH_CR1 0x08
40 #define SPI_SH_CR2 0x10
41 #define SPI_SH_CR3 0x18
42 #define SPI_SH_CR4 0x20
43 #define SPI_SH_CR5 0x28
46 #define SPI_SH_TBE 0x80
47 #define SPI_SH_TBF 0x40
48 #define SPI_SH_RBE 0x20
49 #define SPI_SH_RBF 0x10
50 #define SPI_SH_PFONRD 0x08
51 #define SPI_SH_SSDB 0x04
52 #define SPI_SH_SSD 0x02
53 #define SPI_SH_SSA 0x01
56 #define SPI_SH_RSTF 0x80
57 #define SPI_SH_LOOPBK 0x40
58 #define SPI_SH_CPOL 0x20
59 #define SPI_SH_CPHA 0x10
60 #define SPI_SH_L1M0 0x08
63 #define SPI_SH_MAX_BYTE 0xFF
66 #define SPI_SH_TBEI 0x80
67 #define SPI_SH_TBFI 0x40
68 #define SPI_SH_RBEI 0x20
69 #define SPI_SH_RBFI 0x10
70 #define SPI_SH_WPABRT 0x04
71 #define SPI_SH_SSS 0x01
74 #define SPI_SH_P1L0 0x80
75 #define SPI_SH_PP1L0 0x40
76 #define SPI_SH_MUXI 0x20
77 #define SPI_SH_MUXIRQ 0x10
79 #define SPI_SH_FIFO_SIZE 32
80 #define SPI_SH_SEND_TIMEOUT (3 * HZ)
81 #define SPI_SH_RECEIVE_TIMEOUT (HZ >> 3)
88 struct spi_master
*master
;
89 struct list_head queue
;
90 struct workqueue_struct
*workqueue
;
91 struct work_struct ws
;
93 wait_queue_head_t wait
;
97 static void spi_sh_write(struct spi_sh_data
*ss
, unsigned long data
,
100 writel(data
, ss
->addr
+ offset
);
103 static unsigned long spi_sh_read(struct spi_sh_data
*ss
, unsigned long offset
)
105 return readl(ss
->addr
+ offset
);
108 static void spi_sh_set_bit(struct spi_sh_data
*ss
, unsigned long val
,
109 unsigned long offset
)
113 tmp
= spi_sh_read(ss
, offset
);
115 spi_sh_write(ss
, tmp
, offset
);
118 static void spi_sh_clear_bit(struct spi_sh_data
*ss
, unsigned long val
,
119 unsigned long offset
)
123 tmp
= spi_sh_read(ss
, offset
);
125 spi_sh_write(ss
, tmp
, offset
);
128 static void clear_fifo(struct spi_sh_data
*ss
)
130 spi_sh_set_bit(ss
, SPI_SH_RSTF
, SPI_SH_CR2
);
131 spi_sh_clear_bit(ss
, SPI_SH_RSTF
, SPI_SH_CR2
);
134 static int spi_sh_wait_receive_buffer(struct spi_sh_data
*ss
)
136 int timeout
= 100000;
138 while (spi_sh_read(ss
, SPI_SH_CR1
) & SPI_SH_RBE
) {
146 static int spi_sh_wait_write_buffer_empty(struct spi_sh_data
*ss
)
148 int timeout
= 100000;
150 while (!(spi_sh_read(ss
, SPI_SH_CR1
) & SPI_SH_TBE
)) {
158 static int spi_sh_send(struct spi_sh_data
*ss
, struct spi_message
*mesg
,
159 struct spi_transfer
*t
)
169 spi_sh_set_bit(ss
, SPI_SH_SSA
, SPI_SH_CR1
);
171 data
= (unsigned char *)t
->tx_buf
;
173 cur_len
= min(SPI_SH_FIFO_SIZE
, remain
);
174 for (i
= 0; i
< cur_len
&&
175 !(spi_sh_read(ss
, SPI_SH_CR4
) &
177 !(spi_sh_read(ss
, SPI_SH_CR1
) & SPI_SH_TBF
);
179 spi_sh_write(ss
, (unsigned long)data
[i
], SPI_SH_TBR
);
181 if (spi_sh_read(ss
, SPI_SH_CR4
) & SPI_SH_WPABRT
) {
182 /* Abort SPI operation */
183 spi_sh_set_bit(ss
, SPI_SH_WPABRT
, SPI_SH_CR4
);
194 ss
->cr1
&= ~SPI_SH_TBE
;
195 spi_sh_set_bit(ss
, SPI_SH_TBE
, SPI_SH_CR4
);
196 ret
= wait_event_interruptible_timeout(ss
->wait
,
197 ss
->cr1
& SPI_SH_TBE
,
198 SPI_SH_SEND_TIMEOUT
);
199 if (ret
== 0 && !(ss
->cr1
& SPI_SH_TBE
)) {
200 printk(KERN_ERR
"%s: timeout\n", __func__
);
206 if (list_is_last(&t
->transfer_list
, &mesg
->transfers
)) {
207 tmp
= spi_sh_read(ss
, SPI_SH_CR1
);
208 tmp
= tmp
& ~(SPI_SH_SSD
| SPI_SH_SSDB
);
209 spi_sh_write(ss
, tmp
, SPI_SH_CR1
);
210 spi_sh_set_bit(ss
, SPI_SH_SSA
, SPI_SH_CR1
);
212 ss
->cr1
&= ~SPI_SH_TBE
;
213 spi_sh_set_bit(ss
, SPI_SH_TBE
, SPI_SH_CR4
);
214 ret
= wait_event_interruptible_timeout(ss
->wait
,
215 ss
->cr1
& SPI_SH_TBE
,
216 SPI_SH_SEND_TIMEOUT
);
217 if (ret
== 0 && (ss
->cr1
& SPI_SH_TBE
)) {
218 printk(KERN_ERR
"%s: timeout\n", __func__
);
226 static int spi_sh_receive(struct spi_sh_data
*ss
, struct spi_message
*mesg
,
227 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 tmp
= spi_sh_read(ss
, SPI_SH_CR1
);
242 tmp
= tmp
& ~(SPI_SH_SSD
| SPI_SH_SSDB
);
243 spi_sh_write(ss
, tmp
, SPI_SH_CR1
);
244 spi_sh_set_bit(ss
, SPI_SH_SSA
, SPI_SH_CR1
);
246 spi_sh_wait_write_buffer_empty(ss
);
248 data
= (unsigned char *)t
->rx_buf
;
250 if (remain
>= SPI_SH_FIFO_SIZE
) {
251 ss
->cr1
&= ~SPI_SH_RBF
;
252 spi_sh_set_bit(ss
, SPI_SH_RBF
, SPI_SH_CR4
);
253 ret
= wait_event_interruptible_timeout(ss
->wait
,
254 ss
->cr1
& SPI_SH_RBF
,
255 SPI_SH_RECEIVE_TIMEOUT
);
257 spi_sh_read(ss
, SPI_SH_CR1
) & SPI_SH_RBE
) {
258 printk(KERN_ERR
"%s: timeout\n", __func__
);
263 cur_len
= min(SPI_SH_FIFO_SIZE
, remain
);
264 for (i
= 0; i
< cur_len
; i
++) {
265 if (spi_sh_wait_receive_buffer(ss
))
267 data
[i
] = (unsigned char)spi_sh_read(ss
, SPI_SH_RBR
);
274 /* deassert CS when SPI is receiving. */
275 if (t
->len
> SPI_SH_MAX_BYTE
) {
277 spi_sh_write(ss
, 1, SPI_SH_CR3
);
279 spi_sh_write(ss
, 0, SPI_SH_CR3
);
285 static void spi_sh_work(struct work_struct
*work
)
287 struct spi_sh_data
*ss
= container_of(work
, struct spi_sh_data
, ws
);
288 struct spi_message
*mesg
;
289 struct spi_transfer
*t
;
293 pr_debug("%s: enter\n", __func__
);
295 spin_lock_irqsave(&ss
->lock
, flags
);
296 while (!list_empty(&ss
->queue
)) {
297 mesg
= list_entry(ss
->queue
.next
, struct spi_message
, queue
);
298 list_del_init(&mesg
->queue
);
300 spin_unlock_irqrestore(&ss
->lock
, flags
);
301 list_for_each_entry(t
, &mesg
->transfers
, transfer_list
) {
302 pr_debug("tx_buf = %p, rx_buf = %p\n",
303 t
->tx_buf
, t
->rx_buf
);
304 pr_debug("len = %d, delay_usecs = %d\n",
305 t
->len
, t
->delay_usecs
);
308 ret
= spi_sh_send(ss
, mesg
, t
);
313 ret
= spi_sh_receive(ss
, mesg
, t
);
317 mesg
->actual_length
+= t
->len
;
319 spin_lock_irqsave(&ss
->lock
, flags
);
322 mesg
->complete(mesg
->context
);
326 spi_sh_set_bit(ss
, SPI_SH_SSD
, SPI_SH_CR1
);
329 spi_sh_clear_bit(ss
, SPI_SH_SSA
| SPI_SH_SSDB
| SPI_SH_SSD
,
334 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 if (!spi
->bits_per_word
)
353 spi
->bits_per_word
= 8;
355 pr_debug("%s: enter\n", __func__
);
357 spi_sh_write(ss
, 0xfe, SPI_SH_CR1
); /* SPI sycle stop */
358 spi_sh_write(ss
, 0x00, SPI_SH_CR1
); /* CR1 init */
359 spi_sh_write(ss
, 0x00, SPI_SH_CR3
); /* CR3 init */
364 spi_sh_write(ss
, spi_sh_read(ss
, SPI_SH_CR2
) | 0x07, SPI_SH_CR2
);
370 static int spi_sh_transfer(struct spi_device
*spi
, struct spi_message
*mesg
)
372 struct spi_sh_data
*ss
= spi_master_get_devdata(spi
->master
);
375 pr_debug("%s: enter\n", __func__
);
376 pr_debug("\tmode = %02x\n", spi
->mode
);
378 spin_lock_irqsave(&ss
->lock
, flags
);
380 mesg
->actual_length
= 0;
381 mesg
->status
= -EINPROGRESS
;
383 spi_sh_clear_bit(ss
, SPI_SH_SSA
, SPI_SH_CR1
);
385 list_add_tail(&mesg
->queue
, &ss
->queue
);
386 queue_work(ss
->workqueue
, &ss
->ws
);
388 spin_unlock_irqrestore(&ss
->lock
, flags
);
393 static void spi_sh_cleanup(struct spi_device
*spi
)
395 struct spi_sh_data
*ss
= spi_master_get_devdata(spi
->master
);
397 pr_debug("%s: enter\n", __func__
);
399 spi_sh_clear_bit(ss
, SPI_SH_SSA
| SPI_SH_SSDB
| SPI_SH_SSD
,
403 static irqreturn_t
spi_sh_irq(int irq
, void *_ss
)
405 struct spi_sh_data
*ss
= (struct spi_sh_data
*)_ss
;
408 cr1
= spi_sh_read(ss
, SPI_SH_CR1
);
409 if (cr1
& SPI_SH_TBE
)
410 ss
->cr1
|= SPI_SH_TBE
;
411 if (cr1
& SPI_SH_TBF
)
412 ss
->cr1
|= SPI_SH_TBF
;
413 if (cr1
& SPI_SH_RBE
)
414 ss
->cr1
|= SPI_SH_RBE
;
415 if (cr1
& SPI_SH_RBF
)
416 ss
->cr1
|= SPI_SH_RBF
;
419 spi_sh_clear_bit(ss
, ss
->cr1
, SPI_SH_CR4
);
426 static int __devexit
spi_sh_remove(struct platform_device
*pdev
)
428 struct spi_sh_data
*ss
= dev_get_drvdata(&pdev
->dev
);
430 spi_unregister_master(ss
->master
);
431 destroy_workqueue(ss
->workqueue
);
432 free_irq(ss
->irq
, ss
);
438 static int __devinit
spi_sh_probe(struct platform_device
*pdev
)
440 struct resource
*res
;
441 struct spi_master
*master
;
442 struct spi_sh_data
*ss
;
446 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
447 if (unlikely(res
== NULL
)) {
448 dev_err(&pdev
->dev
, "invalid resource\n");
452 irq
= platform_get_irq(pdev
, 0);
454 dev_err(&pdev
->dev
, "platform_get_irq error\n");
458 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct spi_sh_data
));
459 if (master
== NULL
) {
460 dev_err(&pdev
->dev
, "spi_alloc_master error.\n");
464 ss
= spi_master_get_devdata(master
);
465 dev_set_drvdata(&pdev
->dev
, ss
);
469 ss
->addr
= ioremap(res
->start
, resource_size(res
));
470 if (ss
->addr
== NULL
) {
471 dev_err(&pdev
->dev
, "ioremap error.\n");
475 INIT_LIST_HEAD(&ss
->queue
);
476 spin_lock_init(&ss
->lock
);
477 INIT_WORK(&ss
->ws
, spi_sh_work
);
478 init_waitqueue_head(&ss
->wait
);
479 ss
->workqueue
= create_singlethread_workqueue(
480 dev_name(master
->dev
.parent
));
481 if (ss
->workqueue
== NULL
) {
482 dev_err(&pdev
->dev
, "create workqueue error\n");
487 ret
= request_irq(irq
, spi_sh_irq
, 0, "spi_sh", ss
);
489 dev_err(&pdev
->dev
, "request_irq error\n");
493 master
->num_chipselect
= 2;
494 master
->bus_num
= pdev
->id
;
495 master
->setup
= spi_sh_setup
;
496 master
->transfer
= spi_sh_transfer
;
497 master
->cleanup
= spi_sh_cleanup
;
499 ret
= spi_register_master(master
);
501 printk(KERN_ERR
"spi_register_master error.\n");
510 destroy_workqueue(ss
->workqueue
);
514 spi_master_put(master
);
519 static struct platform_driver spi_sh_driver
= {
520 .probe
= spi_sh_probe
,
521 .remove
= __devexit_p(spi_sh_remove
),
524 .owner
= THIS_MODULE
,
527 module_platform_driver(spi_sh_driver
);
529 MODULE_DESCRIPTION("SH SPI bus driver");
530 MODULE_LICENSE("GPL");
531 MODULE_AUTHOR("Yoshihiro Shimoda");
532 MODULE_ALIAS("platform:sh_spi");