2 * Freescale/Motorola Coldfire Queued SPI driver
4 * Copyright 2010 Steven King <sfking@fdwdc.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/errno.h>
26 #include <linux/platform_device.h>
27 #include <linux/sched.h>
28 #include <linux/workqueue.h>
29 #include <linux/delay.h>
31 #include <linux/clk.h>
32 #include <linux/err.h>
33 #include <linux/spi/spi.h>
35 #include <asm/coldfire.h>
36 #include <asm/mcfsim.h>
37 #include <asm/mcfqspi.h>
39 #define DRIVER_NAME "mcfqspi"
41 #define MCFQSPI_BUSCLK (MCF_BUSCLK / 2)
43 #define MCFQSPI_QMR 0x00
44 #define MCFQSPI_QMR_MSTR 0x8000
45 #define MCFQSPI_QMR_CPOL 0x0200
46 #define MCFQSPI_QMR_CPHA 0x0100
47 #define MCFQSPI_QDLYR 0x04
48 #define MCFQSPI_QDLYR_SPE 0x8000
49 #define MCFQSPI_QWR 0x08
50 #define MCFQSPI_QWR_HALT 0x8000
51 #define MCFQSPI_QWR_WREN 0x4000
52 #define MCFQSPI_QWR_CSIV 0x1000
53 #define MCFQSPI_QIR 0x0C
54 #define MCFQSPI_QIR_WCEFB 0x8000
55 #define MCFQSPI_QIR_ABRTB 0x4000
56 #define MCFQSPI_QIR_ABRTL 0x1000
57 #define MCFQSPI_QIR_WCEFE 0x0800
58 #define MCFQSPI_QIR_ABRTE 0x0400
59 #define MCFQSPI_QIR_SPIFE 0x0100
60 #define MCFQSPI_QIR_WCEF 0x0008
61 #define MCFQSPI_QIR_ABRT 0x0004
62 #define MCFQSPI_QIR_SPIF 0x0001
63 #define MCFQSPI_QAR 0x010
64 #define MCFQSPI_QAR_TXBUF 0x00
65 #define MCFQSPI_QAR_RXBUF 0x10
66 #define MCFQSPI_QAR_CMDBUF 0x20
67 #define MCFQSPI_QDR 0x014
68 #define MCFQSPI_QCR 0x014
69 #define MCFQSPI_QCR_CONT 0x8000
70 #define MCFQSPI_QCR_BITSE 0x4000
71 #define MCFQSPI_QCR_DT 0x2000
77 struct mcfqspi_cs_control
*cs_control
;
79 wait_queue_head_t waitq
;
81 struct work_struct work
;
82 struct workqueue_struct
*workq
;
84 struct list_head msgq
;
87 static void mcfqspi_wr_qmr(struct mcfqspi
*mcfqspi
, u16 val
)
89 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QMR
);
92 static void mcfqspi_wr_qdlyr(struct mcfqspi
*mcfqspi
, u16 val
)
94 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QDLYR
);
97 static u16
mcfqspi_rd_qdlyr(struct mcfqspi
*mcfqspi
)
99 return readw(mcfqspi
->iobase
+ MCFQSPI_QDLYR
);
102 static void mcfqspi_wr_qwr(struct mcfqspi
*mcfqspi
, u16 val
)
104 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QWR
);
107 static void mcfqspi_wr_qir(struct mcfqspi
*mcfqspi
, u16 val
)
109 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QIR
);
112 static void mcfqspi_wr_qar(struct mcfqspi
*mcfqspi
, u16 val
)
114 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QAR
);
117 static void mcfqspi_wr_qdr(struct mcfqspi
*mcfqspi
, u16 val
)
119 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QDR
);
122 static u16
mcfqspi_rd_qdr(struct mcfqspi
*mcfqspi
)
124 return readw(mcfqspi
->iobase
+ MCFQSPI_QDR
);
127 static void mcfqspi_cs_select(struct mcfqspi
*mcfqspi
, u8 chip_select
,
130 mcfqspi
->cs_control
->select(mcfqspi
->cs_control
, chip_select
, cs_high
);
133 static void mcfqspi_cs_deselect(struct mcfqspi
*mcfqspi
, u8 chip_select
,
136 mcfqspi
->cs_control
->deselect(mcfqspi
->cs_control
, chip_select
, cs_high
);
139 static int mcfqspi_cs_setup(struct mcfqspi
*mcfqspi
)
141 return (mcfqspi
->cs_control
&& mcfqspi
->cs_control
->setup
) ?
142 mcfqspi
->cs_control
->setup(mcfqspi
->cs_control
) : 0;
145 static void mcfqspi_cs_teardown(struct mcfqspi
*mcfqspi
)
147 if (mcfqspi
->cs_control
&& mcfqspi
->cs_control
->teardown
)
148 mcfqspi
->cs_control
->teardown(mcfqspi
->cs_control
);
151 static u8
mcfqspi_qmr_baud(u32 speed_hz
)
153 return clamp((MCFQSPI_BUSCLK
+ speed_hz
- 1) / speed_hz
, 2u, 255u);
156 static bool mcfqspi_qdlyr_spe(struct mcfqspi
*mcfqspi
)
158 return mcfqspi_rd_qdlyr(mcfqspi
) & MCFQSPI_QDLYR_SPE
;
161 static irqreturn_t
mcfqspi_irq_handler(int this_irq
, void *dev_id
)
163 struct mcfqspi
*mcfqspi
= dev_id
;
165 /* clear interrupt */
166 mcfqspi_wr_qir(mcfqspi
, MCFQSPI_QIR_SPIFE
| MCFQSPI_QIR_SPIF
);
167 wake_up(&mcfqspi
->waitq
);
172 static void mcfqspi_transfer_msg8(struct mcfqspi
*mcfqspi
, unsigned count
,
173 const u8
*txbuf
, u8
*rxbuf
)
175 unsigned i
, n
, offset
= 0;
179 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_CMDBUF
);
180 for (i
= 0; i
< n
; ++i
)
181 mcfqspi_wr_qdr(mcfqspi
, MCFQSPI_QCR_BITSE
);
183 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_TXBUF
);
185 for (i
= 0; i
< n
; ++i
)
186 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
188 for (i
= 0; i
< count
; ++i
)
189 mcfqspi_wr_qdr(mcfqspi
, 0);
194 mcfqspi_wr_qwr(mcfqspi
, 0x700);
195 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
198 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
199 mcfqspi_wr_qwr(mcfqspi
, qwr
);
200 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
202 mcfqspi_wr_qar(mcfqspi
,
203 MCFQSPI_QAR_RXBUF
+ offset
);
204 for (i
= 0; i
< 8; ++i
)
205 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
209 mcfqspi_wr_qar(mcfqspi
,
210 MCFQSPI_QAR_TXBUF
+ offset
);
211 for (i
= 0; i
< n
; ++i
)
212 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
214 qwr
= (offset
? 0x808 : 0) + ((n
- 1) << 8);
218 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
219 mcfqspi_wr_qwr(mcfqspi
, qwr
);
220 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
222 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
223 for (i
= 0; i
< 8; ++i
)
224 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
228 mcfqspi_wr_qwr(mcfqspi
, (n
- 1) << 8);
229 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
231 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
233 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
234 for (i
= 0; i
< n
; ++i
)
235 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
239 static void mcfqspi_transfer_msg16(struct mcfqspi
*mcfqspi
, unsigned count
,
240 const u16
*txbuf
, u16
*rxbuf
)
242 unsigned i
, n
, offset
= 0;
246 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_CMDBUF
);
247 for (i
= 0; i
< n
; ++i
)
248 mcfqspi_wr_qdr(mcfqspi
, MCFQSPI_QCR_BITSE
);
250 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_TXBUF
);
252 for (i
= 0; i
< n
; ++i
)
253 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
255 for (i
= 0; i
< count
; ++i
)
256 mcfqspi_wr_qdr(mcfqspi
, 0);
261 mcfqspi_wr_qwr(mcfqspi
, 0x700);
262 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
265 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
266 mcfqspi_wr_qwr(mcfqspi
, qwr
);
267 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
269 mcfqspi_wr_qar(mcfqspi
,
270 MCFQSPI_QAR_RXBUF
+ offset
);
271 for (i
= 0; i
< 8; ++i
)
272 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
276 mcfqspi_wr_qar(mcfqspi
,
277 MCFQSPI_QAR_TXBUF
+ offset
);
278 for (i
= 0; i
< n
; ++i
)
279 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
281 qwr
= (offset
? 0x808 : 0x000) + ((n
- 1) << 8);
285 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
286 mcfqspi_wr_qwr(mcfqspi
, qwr
);
287 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
289 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
290 for (i
= 0; i
< 8; ++i
)
291 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
295 mcfqspi_wr_qwr(mcfqspi
, (n
- 1) << 8);
296 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
298 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
300 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
301 for (i
= 0; i
< n
; ++i
)
302 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
306 static void mcfqspi_work(struct work_struct
*work
)
308 struct mcfqspi
*mcfqspi
= container_of(work
, struct mcfqspi
, work
);
311 spin_lock_irqsave(&mcfqspi
->lock
, flags
);
312 while (!list_empty(&mcfqspi
->msgq
)) {
313 struct spi_message
*msg
;
314 struct spi_device
*spi
;
315 struct spi_transfer
*xfer
;
318 msg
= container_of(mcfqspi
->msgq
.next
, struct spi_message
,
321 list_del_init(&msg
->queue
);
322 spin_unlock_irqrestore(&mcfqspi
->lock
, flags
);
326 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
327 bool cs_high
= spi
->mode
& SPI_CS_HIGH
;
328 u16 qmr
= MCFQSPI_QMR_MSTR
;
330 if (xfer
->bits_per_word
)
331 qmr
|= xfer
->bits_per_word
<< 10;
333 qmr
|= spi
->bits_per_word
<< 10;
334 if (spi
->mode
& SPI_CPHA
)
335 qmr
|= MCFQSPI_QMR_CPHA
;
336 if (spi
->mode
& SPI_CPOL
)
337 qmr
|= MCFQSPI_QMR_CPOL
;
339 qmr
|= mcfqspi_qmr_baud(xfer
->speed_hz
);
341 qmr
|= mcfqspi_qmr_baud(spi
->max_speed_hz
);
342 mcfqspi_wr_qmr(mcfqspi
, qmr
);
344 mcfqspi_cs_select(mcfqspi
, spi
->chip_select
, cs_high
);
346 mcfqspi_wr_qir(mcfqspi
, MCFQSPI_QIR_SPIFE
);
347 if ((xfer
->bits_per_word
? xfer
->bits_per_word
:
348 spi
->bits_per_word
) == 8)
349 mcfqspi_transfer_msg8(mcfqspi
, xfer
->len
,
353 mcfqspi_transfer_msg16(mcfqspi
, xfer
->len
/ 2,
356 mcfqspi_wr_qir(mcfqspi
, 0);
358 if (xfer
->delay_usecs
)
359 udelay(xfer
->delay_usecs
);
360 if (xfer
->cs_change
) {
361 if (!list_is_last(&xfer
->transfer_list
,
363 mcfqspi_cs_deselect(mcfqspi
,
367 if (list_is_last(&xfer
->transfer_list
,
369 mcfqspi_cs_deselect(mcfqspi
,
373 msg
->actual_length
+= xfer
->len
;
375 msg
->status
= status
;
376 msg
->complete(msg
->context
);
378 spin_lock_irqsave(&mcfqspi
->lock
, flags
);
380 spin_unlock_irqrestore(&mcfqspi
->lock
, flags
);
383 static int mcfqspi_transfer(struct spi_device
*spi
, struct spi_message
*msg
)
385 struct mcfqspi
*mcfqspi
;
386 struct spi_transfer
*xfer
;
389 mcfqspi
= spi_master_get_devdata(spi
->master
);
391 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
392 if (xfer
->bits_per_word
&& ((xfer
->bits_per_word
< 8)
393 || (xfer
->bits_per_word
> 16))) {
395 "%d bits per word is not supported\n",
396 xfer
->bits_per_word
);
399 if (xfer
->speed_hz
) {
400 u32 real_speed
= MCFQSPI_BUSCLK
/
401 mcfqspi_qmr_baud(xfer
->speed_hz
);
402 if (real_speed
!= xfer
->speed_hz
)
404 "using speed %d instead of %d\n",
405 real_speed
, xfer
->speed_hz
);
408 msg
->status
= -EINPROGRESS
;
409 msg
->actual_length
= 0;
411 spin_lock_irqsave(&mcfqspi
->lock
, flags
);
412 list_add_tail(&msg
->queue
, &mcfqspi
->msgq
);
413 queue_work(mcfqspi
->workq
, &mcfqspi
->work
);
414 spin_unlock_irqrestore(&mcfqspi
->lock
, flags
);
418 msg
->status
= -EINVAL
;
422 static int mcfqspi_setup(struct spi_device
*spi
)
424 if ((spi
->bits_per_word
< 8) || (spi
->bits_per_word
> 16)) {
425 dev_dbg(&spi
->dev
, "%d bits per word is not supported\n",
429 if (spi
->chip_select
>= spi
->master
->num_chipselect
) {
430 dev_dbg(&spi
->dev
, "%d chip select is out of range\n",
435 mcfqspi_cs_deselect(spi_master_get_devdata(spi
->master
),
436 spi
->chip_select
, spi
->mode
& SPI_CS_HIGH
);
439 "bits per word %d, chip select %d, speed %d KHz\n",
440 spi
->bits_per_word
, spi
->chip_select
,
441 (MCFQSPI_BUSCLK
/ mcfqspi_qmr_baud(spi
->max_speed_hz
))
447 static int __devinit
mcfqspi_probe(struct platform_device
*pdev
)
449 struct spi_master
*master
;
450 struct mcfqspi
*mcfqspi
;
451 struct resource
*res
;
452 struct mcfqspi_platform_data
*pdata
;
455 master
= spi_alloc_master(&pdev
->dev
, sizeof(*mcfqspi
));
456 if (master
== NULL
) {
457 dev_dbg(&pdev
->dev
, "spi_alloc_master failed\n");
461 mcfqspi
= spi_master_get_devdata(master
);
463 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
465 dev_dbg(&pdev
->dev
, "platform_get_resource failed\n");
470 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
471 dev_dbg(&pdev
->dev
, "request_mem_region failed\n");
476 mcfqspi
->iobase
= ioremap(res
->start
, resource_size(res
));
477 if (!mcfqspi
->iobase
) {
478 dev_dbg(&pdev
->dev
, "ioremap failed\n");
483 mcfqspi
->irq
= platform_get_irq(pdev
, 0);
484 if (mcfqspi
->irq
< 0) {
485 dev_dbg(&pdev
->dev
, "platform_get_irq failed\n");
490 status
= request_irq(mcfqspi
->irq
, mcfqspi_irq_handler
, IRQF_DISABLED
,
491 pdev
->name
, mcfqspi
);
493 dev_dbg(&pdev
->dev
, "request_irq failed\n");
497 mcfqspi
->clk
= clk_get(&pdev
->dev
, "qspi_clk");
498 if (IS_ERR(mcfqspi
->clk
)) {
499 dev_dbg(&pdev
->dev
, "clk_get failed\n");
500 status
= PTR_ERR(mcfqspi
->clk
);
503 clk_enable(mcfqspi
->clk
);
505 mcfqspi
->workq
= create_singlethread_workqueue(dev_name(master
->dev
.parent
));
506 if (!mcfqspi
->workq
) {
507 dev_dbg(&pdev
->dev
, "create_workqueue failed\n");
511 INIT_WORK(&mcfqspi
->work
, mcfqspi_work
);
512 spin_lock_init(&mcfqspi
->lock
);
513 INIT_LIST_HEAD(&mcfqspi
->msgq
);
514 init_waitqueue_head(&mcfqspi
->waitq
);
516 pdata
= pdev
->dev
.platform_data
;
518 dev_dbg(&pdev
->dev
, "platform data is missing\n");
521 master
->bus_num
= pdata
->bus_num
;
522 master
->num_chipselect
= pdata
->num_chipselect
;
524 mcfqspi
->cs_control
= pdata
->cs_control
;
525 status
= mcfqspi_cs_setup(mcfqspi
);
527 dev_dbg(&pdev
->dev
, "error initializing cs_control\n");
531 master
->mode_bits
= SPI_CS_HIGH
| SPI_CPOL
| SPI_CPHA
;
532 master
->setup
= mcfqspi_setup
;
533 master
->transfer
= mcfqspi_transfer
;
535 platform_set_drvdata(pdev
, master
);
537 status
= spi_register_master(master
);
539 dev_dbg(&pdev
->dev
, "spi_register_master failed\n");
542 dev_info(&pdev
->dev
, "Coldfire QSPI bus driver\n");
547 mcfqspi_cs_teardown(mcfqspi
);
549 destroy_workqueue(mcfqspi
->workq
);
551 clk_disable(mcfqspi
->clk
);
552 clk_put(mcfqspi
->clk
);
554 free_irq(mcfqspi
->irq
, mcfqspi
);
556 iounmap(mcfqspi
->iobase
);
558 release_mem_region(res
->start
, resource_size(res
));
560 spi_master_put(master
);
562 dev_dbg(&pdev
->dev
, "Coldfire QSPI probe failed\n");
567 static int __devexit
mcfqspi_remove(struct platform_device
*pdev
)
569 struct spi_master
*master
= platform_get_drvdata(pdev
);
570 struct mcfqspi
*mcfqspi
= spi_master_get_devdata(master
);
571 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
573 /* disable the hardware (set the baud rate to 0) */
574 mcfqspi_wr_qmr(mcfqspi
, MCFQSPI_QMR_MSTR
);
576 platform_set_drvdata(pdev
, NULL
);
577 mcfqspi_cs_teardown(mcfqspi
);
578 destroy_workqueue(mcfqspi
->workq
);
579 clk_disable(mcfqspi
->clk
);
580 clk_put(mcfqspi
->clk
);
581 free_irq(mcfqspi
->irq
, mcfqspi
);
582 iounmap(mcfqspi
->iobase
);
583 release_mem_region(res
->start
, resource_size(res
));
584 spi_unregister_master(master
);
585 spi_master_put(master
);
592 static int mcfqspi_suspend(struct device
*dev
)
594 struct mcfqspi
*mcfqspi
= platform_get_drvdata(to_platform_device(dev
));
596 clk_disable(mcfqspi
->clk
);
601 static int mcfqspi_resume(struct device
*dev
)
603 struct mcfqspi
*mcfqspi
= platform_get_drvdata(to_platform_device(dev
));
605 clk_enable(mcfqspi
->clk
);
610 static struct dev_pm_ops mcfqspi_dev_pm_ops
= {
611 .suspend
= mcfqspi_suspend
,
612 .resume
= mcfqspi_resume
,
615 #define MCFQSPI_DEV_PM_OPS (&mcfqspi_dev_pm_ops)
617 #define MCFQSPI_DEV_PM_OPS NULL
620 static struct platform_driver mcfqspi_driver
= {
621 .driver
.name
= DRIVER_NAME
,
622 .driver
.owner
= THIS_MODULE
,
623 .driver
.pm
= MCFQSPI_DEV_PM_OPS
,
624 .remove
= __devexit_p(mcfqspi_remove
),
627 static int __init
mcfqspi_init(void)
629 return platform_driver_probe(&mcfqspi_driver
, mcfqspi_probe
);
631 module_init(mcfqspi_init
);
633 static void __exit
mcfqspi_exit(void)
635 platform_driver_unregister(&mcfqspi_driver
);
637 module_exit(mcfqspi_exit
);
639 MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
640 MODULE_DESCRIPTION("Coldfire QSPI Controller Driver");
641 MODULE_LICENSE("GPL");
642 MODULE_ALIAS("platform:" DRIVER_NAME
);