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/workqueue.h>
28 #include <linux/delay.h>
30 #include <linux/clk.h>
31 #include <linux/err.h>
32 #include <linux/spi/spi.h>
34 #include <asm/coldfire.h>
35 #include <asm/mcfqspi.h>
37 #define DRIVER_NAME "mcfqspi"
39 #define MCFQSPI_BUSCLK (MCF_BUSCLK / 2)
41 #define MCFQSPI_QMR 0x00
42 #define MCFQSPI_QMR_MSTR 0x8000
43 #define MCFQSPI_QMR_CPOL 0x0200
44 #define MCFQSPI_QMR_CPHA 0x0100
45 #define MCFQSPI_QDLYR 0x04
46 #define MCFQSPI_QDLYR_SPE 0x8000
47 #define MCFQSPI_QWR 0x08
48 #define MCFQSPI_QWR_HALT 0x8000
49 #define MCFQSPI_QWR_WREN 0x4000
50 #define MCFQSPI_QWR_CSIV 0x1000
51 #define MCFQSPI_QIR 0x0C
52 #define MCFQSPI_QIR_WCEFB 0x8000
53 #define MCFQSPI_QIR_ABRTB 0x4000
54 #define MCFQSPI_QIR_ABRTL 0x1000
55 #define MCFQSPI_QIR_WCEFE 0x0800
56 #define MCFQSPI_QIR_ABRTE 0x0400
57 #define MCFQSPI_QIR_SPIFE 0x0100
58 #define MCFQSPI_QIR_WCEF 0x0008
59 #define MCFQSPI_QIR_ABRT 0x0004
60 #define MCFQSPI_QIR_SPIF 0x0001
61 #define MCFQSPI_QAR 0x010
62 #define MCFQSPI_QAR_TXBUF 0x00
63 #define MCFQSPI_QAR_RXBUF 0x10
64 #define MCFQSPI_QAR_CMDBUF 0x20
65 #define MCFQSPI_QDR 0x014
66 #define MCFQSPI_QCR 0x014
67 #define MCFQSPI_QCR_CONT 0x8000
68 #define MCFQSPI_QCR_BITSE 0x4000
69 #define MCFQSPI_QCR_DT 0x2000
75 struct mcfqspi_cs_control
*cs_control
;
77 wait_queue_head_t waitq
;
79 struct work_struct work
;
80 struct workqueue_struct
*workq
;
82 struct list_head msgq
;
85 static void mcfqspi_wr_qmr(struct mcfqspi
*mcfqspi
, u16 val
)
87 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QMR
);
90 static void mcfqspi_wr_qdlyr(struct mcfqspi
*mcfqspi
, u16 val
)
92 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QDLYR
);
95 static u16
mcfqspi_rd_qdlyr(struct mcfqspi
*mcfqspi
)
97 return readw(mcfqspi
->iobase
+ MCFQSPI_QDLYR
);
100 static void mcfqspi_wr_qwr(struct mcfqspi
*mcfqspi
, u16 val
)
102 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QWR
);
105 static void mcfqspi_wr_qir(struct mcfqspi
*mcfqspi
, u16 val
)
107 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QIR
);
110 static void mcfqspi_wr_qar(struct mcfqspi
*mcfqspi
, u16 val
)
112 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QAR
);
115 static void mcfqspi_wr_qdr(struct mcfqspi
*mcfqspi
, u16 val
)
117 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QDR
);
120 static u16
mcfqspi_rd_qdr(struct mcfqspi
*mcfqspi
)
122 return readw(mcfqspi
->iobase
+ MCFQSPI_QDR
);
125 static void mcfqspi_cs_select(struct mcfqspi
*mcfqspi
, u8 chip_select
,
128 mcfqspi
->cs_control
->select(mcfqspi
->cs_control
, chip_select
, cs_high
);
131 static void mcfqspi_cs_deselect(struct mcfqspi
*mcfqspi
, u8 chip_select
,
134 mcfqspi
->cs_control
->deselect(mcfqspi
->cs_control
, chip_select
, cs_high
);
137 static int mcfqspi_cs_setup(struct mcfqspi
*mcfqspi
)
139 return (mcfqspi
->cs_control
&& mcfqspi
->cs_control
->setup
) ?
140 mcfqspi
->cs_control
->setup(mcfqspi
->cs_control
) : 0;
143 static void mcfqspi_cs_teardown(struct mcfqspi
*mcfqspi
)
145 if (mcfqspi
->cs_control
&& mcfqspi
->cs_control
->teardown
)
146 mcfqspi
->cs_control
->teardown(mcfqspi
->cs_control
);
149 static u8
mcfqspi_qmr_baud(u32 speed_hz
)
151 return clamp((MCFQSPI_BUSCLK
+ speed_hz
- 1) / speed_hz
, 2u, 255u);
154 static bool mcfqspi_qdlyr_spe(struct mcfqspi
*mcfqspi
)
156 return mcfqspi_rd_qdlyr(mcfqspi
) & MCFQSPI_QDLYR_SPE
;
159 static irqreturn_t
mcfqspi_irq_handler(int this_irq
, void *dev_id
)
161 struct mcfqspi
*mcfqspi
= dev_id
;
163 /* clear interrupt */
164 mcfqspi_wr_qir(mcfqspi
, MCFQSPI_QIR_SPIFE
| MCFQSPI_QIR_SPIF
);
165 wake_up(&mcfqspi
->waitq
);
170 static void mcfqspi_transfer_msg8(struct mcfqspi
*mcfqspi
, unsigned count
,
171 const u8
*txbuf
, u8
*rxbuf
)
173 unsigned i
, n
, offset
= 0;
177 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_CMDBUF
);
178 for (i
= 0; i
< n
; ++i
)
179 mcfqspi_wr_qdr(mcfqspi
, MCFQSPI_QCR_BITSE
);
181 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_TXBUF
);
183 for (i
= 0; i
< n
; ++i
)
184 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
186 for (i
= 0; i
< count
; ++i
)
187 mcfqspi_wr_qdr(mcfqspi
, 0);
192 mcfqspi_wr_qwr(mcfqspi
, 0x700);
193 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
196 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
197 mcfqspi_wr_qwr(mcfqspi
, qwr
);
198 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
200 mcfqspi_wr_qar(mcfqspi
,
201 MCFQSPI_QAR_RXBUF
+ offset
);
202 for (i
= 0; i
< 8; ++i
)
203 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
207 mcfqspi_wr_qar(mcfqspi
,
208 MCFQSPI_QAR_TXBUF
+ offset
);
209 for (i
= 0; i
< n
; ++i
)
210 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
212 qwr
= (offset
? 0x808 : 0) + ((n
- 1) << 8);
216 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
217 mcfqspi_wr_qwr(mcfqspi
, qwr
);
218 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
220 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
221 for (i
= 0; i
< 8; ++i
)
222 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
226 mcfqspi_wr_qwr(mcfqspi
, (n
- 1) << 8);
227 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
229 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
231 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
232 for (i
= 0; i
< n
; ++i
)
233 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
237 static void mcfqspi_transfer_msg16(struct mcfqspi
*mcfqspi
, unsigned count
,
238 const u16
*txbuf
, u16
*rxbuf
)
240 unsigned i
, n
, offset
= 0;
244 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_CMDBUF
);
245 for (i
= 0; i
< n
; ++i
)
246 mcfqspi_wr_qdr(mcfqspi
, MCFQSPI_QCR_BITSE
);
248 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_TXBUF
);
250 for (i
= 0; i
< n
; ++i
)
251 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
253 for (i
= 0; i
< count
; ++i
)
254 mcfqspi_wr_qdr(mcfqspi
, 0);
259 mcfqspi_wr_qwr(mcfqspi
, 0x700);
260 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
263 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
264 mcfqspi_wr_qwr(mcfqspi
, qwr
);
265 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
267 mcfqspi_wr_qar(mcfqspi
,
268 MCFQSPI_QAR_RXBUF
+ offset
);
269 for (i
= 0; i
< 8; ++i
)
270 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
274 mcfqspi_wr_qar(mcfqspi
,
275 MCFQSPI_QAR_TXBUF
+ offset
);
276 for (i
= 0; i
< n
; ++i
)
277 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
279 qwr
= (offset
? 0x808 : 0x000) + ((n
- 1) << 8);
283 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
284 mcfqspi_wr_qwr(mcfqspi
, qwr
);
285 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
287 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
288 for (i
= 0; i
< 8; ++i
)
289 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
293 mcfqspi_wr_qwr(mcfqspi
, (n
- 1) << 8);
294 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
296 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
298 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
299 for (i
= 0; i
< n
; ++i
)
300 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
304 static void mcfqspi_work(struct work_struct
*work
)
306 struct mcfqspi
*mcfqspi
= container_of(work
, struct mcfqspi
, work
);
309 spin_lock_irqsave(&mcfqspi
->lock
, flags
);
310 while (!list_empty(&mcfqspi
->msgq
)) {
311 struct spi_message
*msg
;
312 struct spi_device
*spi
;
313 struct spi_transfer
*xfer
;
316 msg
= container_of(mcfqspi
->msgq
.next
, struct spi_message
,
319 list_del_init(&mcfqspi
->msgq
);
320 spin_unlock_irqrestore(&mcfqspi
->lock
, flags
);
324 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
325 bool cs_high
= spi
->mode
& SPI_CS_HIGH
;
326 u16 qmr
= MCFQSPI_QMR_MSTR
;
328 if (xfer
->bits_per_word
)
329 qmr
|= xfer
->bits_per_word
<< 10;
331 qmr
|= spi
->bits_per_word
<< 10;
332 if (spi
->mode
& SPI_CPHA
)
333 qmr
|= MCFQSPI_QMR_CPHA
;
334 if (spi
->mode
& SPI_CPOL
)
335 qmr
|= MCFQSPI_QMR_CPOL
;
337 qmr
|= mcfqspi_qmr_baud(xfer
->speed_hz
);
339 qmr
|= mcfqspi_qmr_baud(spi
->max_speed_hz
);
340 mcfqspi_wr_qmr(mcfqspi
, qmr
);
342 mcfqspi_cs_select(mcfqspi
, spi
->chip_select
, cs_high
);
344 mcfqspi_wr_qir(mcfqspi
, MCFQSPI_QIR_SPIFE
);
345 if ((xfer
->bits_per_word
? xfer
->bits_per_word
:
346 spi
->bits_per_word
) == 8)
347 mcfqspi_transfer_msg8(mcfqspi
, xfer
->len
,
351 mcfqspi_transfer_msg16(mcfqspi
, xfer
->len
/ 2,
354 mcfqspi_wr_qir(mcfqspi
, 0);
356 if (xfer
->delay_usecs
)
357 udelay(xfer
->delay_usecs
);
358 if (xfer
->cs_change
) {
359 if (!list_is_last(&xfer
->transfer_list
,
361 mcfqspi_cs_deselect(mcfqspi
,
365 if (list_is_last(&xfer
->transfer_list
,
367 mcfqspi_cs_deselect(mcfqspi
,
371 msg
->actual_length
+= xfer
->len
;
373 msg
->status
= status
;
374 msg
->complete(msg
->context
);
376 spin_lock_irqsave(&mcfqspi
->lock
, flags
);
378 spin_unlock_irqrestore(&mcfqspi
->lock
, flags
);
381 static int mcfqspi_transfer(struct spi_device
*spi
, struct spi_message
*msg
)
383 struct mcfqspi
*mcfqspi
;
384 struct spi_transfer
*xfer
;
387 mcfqspi
= spi_master_get_devdata(spi
->master
);
389 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
390 if (xfer
->bits_per_word
&& ((xfer
->bits_per_word
< 8)
391 || (xfer
->bits_per_word
> 16))) {
393 "%d bits per word is not supported\n",
394 xfer
->bits_per_word
);
397 if (xfer
->speed_hz
) {
398 u32 real_speed
= MCFQSPI_BUSCLK
/
399 mcfqspi_qmr_baud(xfer
->speed_hz
);
400 if (real_speed
!= xfer
->speed_hz
)
402 "using speed %d instead of %d\n",
403 real_speed
, xfer
->speed_hz
);
406 msg
->status
= -EINPROGRESS
;
407 msg
->actual_length
= 0;
409 spin_lock_irqsave(&mcfqspi
->lock
, flags
);
410 list_add_tail(&msg
->queue
, &mcfqspi
->msgq
);
411 queue_work(mcfqspi
->workq
, &mcfqspi
->work
);
412 spin_unlock_irqrestore(&mcfqspi
->lock
, flags
);
416 msg
->status
= -EINVAL
;
420 static int mcfqspi_setup(struct spi_device
*spi
)
422 if ((spi
->bits_per_word
< 8) || (spi
->bits_per_word
> 16)) {
423 dev_dbg(&spi
->dev
, "%d bits per word is not supported\n",
427 if (spi
->chip_select
>= spi
->master
->num_chipselect
) {
428 dev_dbg(&spi
->dev
, "%d chip select is out of range\n",
433 mcfqspi_cs_deselect(spi_master_get_devdata(spi
->master
),
434 spi
->chip_select
, spi
->mode
& SPI_CS_HIGH
);
437 "bits per word %d, chip select %d, speed %d KHz\n",
438 spi
->bits_per_word
, spi
->chip_select
,
439 (MCFQSPI_BUSCLK
/ mcfqspi_qmr_baud(spi
->max_speed_hz
))
445 static int __devinit
mcfqspi_probe(struct platform_device
*pdev
)
447 struct spi_master
*master
;
448 struct mcfqspi
*mcfqspi
;
449 struct resource
*res
;
450 struct mcfqspi_platform_data
*pdata
;
453 master
= spi_alloc_master(&pdev
->dev
, sizeof(*mcfqspi
));
454 if (master
== NULL
) {
455 dev_dbg(&pdev
->dev
, "spi_alloc_master failed\n");
459 mcfqspi
= spi_master_get_devdata(master
);
461 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
463 dev_dbg(&pdev
->dev
, "platform_get_resource failed\n");
468 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
469 dev_dbg(&pdev
->dev
, "request_mem_region failed\n");
474 mcfqspi
->iobase
= ioremap(res
->start
, resource_size(res
));
475 if (!mcfqspi
->iobase
) {
476 dev_dbg(&pdev
->dev
, "ioremap failed\n");
481 mcfqspi
->irq
= platform_get_irq(pdev
, 0);
482 if (mcfqspi
->irq
< 0) {
483 dev_dbg(&pdev
->dev
, "platform_get_irq failed\n");
488 status
= request_irq(mcfqspi
->irq
, mcfqspi_irq_handler
, IRQF_DISABLED
,
489 pdev
->name
, mcfqspi
);
491 dev_dbg(&pdev
->dev
, "request_irq failed\n");
495 mcfqspi
->clk
= clk_get(&pdev
->dev
, "qspi_clk");
496 if (IS_ERR(mcfqspi
->clk
)) {
497 dev_dbg(&pdev
->dev
, "clk_get failed\n");
498 status
= PTR_ERR(mcfqspi
->clk
);
501 clk_enable(mcfqspi
->clk
);
503 mcfqspi
->workq
= create_singlethread_workqueue(dev_name(master
->dev
.parent
));
504 if (!mcfqspi
->workq
) {
505 dev_dbg(&pdev
->dev
, "create_workqueue failed\n");
509 INIT_WORK(&mcfqspi
->work
, mcfqspi_work
);
510 spin_lock_init(&mcfqspi
->lock
);
511 INIT_LIST_HEAD(&mcfqspi
->msgq
);
512 init_waitqueue_head(&mcfqspi
->waitq
);
514 pdata
= pdev
->dev
.platform_data
;
516 dev_dbg(&pdev
->dev
, "platform data is missing\n");
519 master
->bus_num
= pdata
->bus_num
;
520 master
->num_chipselect
= pdata
->num_chipselect
;
522 mcfqspi
->cs_control
= pdata
->cs_control
;
523 status
= mcfqspi_cs_setup(mcfqspi
);
525 dev_dbg(&pdev
->dev
, "error initializing cs_control\n");
529 master
->mode_bits
= SPI_CS_HIGH
| SPI_CPOL
| SPI_CPHA
;
530 master
->setup
= mcfqspi_setup
;
531 master
->transfer
= mcfqspi_transfer
;
533 platform_set_drvdata(pdev
, master
);
535 status
= spi_register_master(master
);
537 dev_dbg(&pdev
->dev
, "spi_register_master failed\n");
540 dev_info(&pdev
->dev
, "Coldfire QSPI bus driver\n");
545 mcfqspi_cs_teardown(mcfqspi
);
547 destroy_workqueue(mcfqspi
->workq
);
549 clk_disable(mcfqspi
->clk
);
550 clk_put(mcfqspi
->clk
);
552 free_irq(mcfqspi
->irq
, mcfqspi
);
554 iounmap(mcfqspi
->iobase
);
556 release_mem_region(res
->start
, resource_size(res
));
558 spi_master_put(master
);
560 dev_dbg(&pdev
->dev
, "Coldfire QSPI probe failed\n");
565 static int __devexit
mcfqspi_remove(struct platform_device
*pdev
)
567 struct spi_master
*master
= platform_get_drvdata(pdev
);
568 struct mcfqspi
*mcfqspi
= spi_master_get_devdata(master
);
569 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
571 /* disable the hardware (set the baud rate to 0) */
572 mcfqspi_wr_qmr(mcfqspi
, MCFQSPI_QMR_MSTR
);
574 platform_set_drvdata(pdev
, NULL
);
575 mcfqspi_cs_teardown(mcfqspi
);
576 destroy_workqueue(mcfqspi
->workq
);
577 clk_disable(mcfqspi
->clk
);
578 clk_put(mcfqspi
->clk
);
579 free_irq(mcfqspi
->irq
, mcfqspi
);
580 iounmap(mcfqspi
->iobase
);
581 release_mem_region(res
->start
, resource_size(res
));
582 spi_unregister_master(master
);
583 spi_master_put(master
);
590 static int mcfqspi_suspend(struct device
*dev
)
592 struct mcfqspi
*mcfqspi
= platform_get_drvdata(to_platform_device(dev
));
594 clk_disable(mcfqspi
->clk
);
599 static int mcfqspi_resume(struct device
*dev
)
601 struct mcfqspi
*mcfqspi
= platform_get_drvdata(to_platform_device(dev
));
603 clk_enable(mcfqspi
->clk
);
608 static struct dev_pm_ops mcfqspi_dev_pm_ops
= {
609 .suspend
= mcfqspi_suspend
,
610 .resume
= mcfqspi_resume
,
613 #define MCFQSPI_DEV_PM_OPS (&mcfqspi_dev_pm_ops)
615 #define MCFQSPI_DEV_PM_OPS NULL
618 static struct platform_driver mcfqspi_driver
= {
619 .driver
.name
= DRIVER_NAME
,
620 .driver
.owner
= THIS_MODULE
,
621 .driver
.pm
= MCFQSPI_DEV_PM_OPS
,
622 .remove
= __devexit_p(mcfqspi_remove
),
625 static int __init
mcfqspi_init(void)
627 return platform_driver_probe(&mcfqspi_driver
, mcfqspi_probe
);
629 module_init(mcfqspi_init
);
631 static void __exit
mcfqspi_exit(void)
633 platform_driver_unregister(&mcfqspi_driver
);
635 module_exit(mcfqspi_exit
);
637 MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
638 MODULE_DESCRIPTION("Coldfire QSPI Controller Driver");
639 MODULE_LICENSE("GPL");
640 MODULE_ALIAS("platform:" DRIVER_NAME
);