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.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/errno.h>
21 #include <linux/platform_device.h>
22 #include <linux/sched.h>
23 #include <linux/delay.h>
25 #include <linux/clk.h>
26 #include <linux/err.h>
27 #include <linux/spi/spi.h>
28 #include <linux/pm_runtime.h>
30 #include <asm/coldfire.h>
31 #include <asm/mcfsim.h>
32 #include <asm/mcfqspi.h>
34 #define DRIVER_NAME "mcfqspi"
36 #define MCFQSPI_BUSCLK (MCF_BUSCLK / 2)
38 #define MCFQSPI_QMR 0x00
39 #define MCFQSPI_QMR_MSTR 0x8000
40 #define MCFQSPI_QMR_CPOL 0x0200
41 #define MCFQSPI_QMR_CPHA 0x0100
42 #define MCFQSPI_QDLYR 0x04
43 #define MCFQSPI_QDLYR_SPE 0x8000
44 #define MCFQSPI_QWR 0x08
45 #define MCFQSPI_QWR_HALT 0x8000
46 #define MCFQSPI_QWR_WREN 0x4000
47 #define MCFQSPI_QWR_CSIV 0x1000
48 #define MCFQSPI_QIR 0x0C
49 #define MCFQSPI_QIR_WCEFB 0x8000
50 #define MCFQSPI_QIR_ABRTB 0x4000
51 #define MCFQSPI_QIR_ABRTL 0x1000
52 #define MCFQSPI_QIR_WCEFE 0x0800
53 #define MCFQSPI_QIR_ABRTE 0x0400
54 #define MCFQSPI_QIR_SPIFE 0x0100
55 #define MCFQSPI_QIR_WCEF 0x0008
56 #define MCFQSPI_QIR_ABRT 0x0004
57 #define MCFQSPI_QIR_SPIF 0x0001
58 #define MCFQSPI_QAR 0x010
59 #define MCFQSPI_QAR_TXBUF 0x00
60 #define MCFQSPI_QAR_RXBUF 0x10
61 #define MCFQSPI_QAR_CMDBUF 0x20
62 #define MCFQSPI_QDR 0x014
63 #define MCFQSPI_QCR 0x014
64 #define MCFQSPI_QCR_CONT 0x8000
65 #define MCFQSPI_QCR_BITSE 0x4000
66 #define MCFQSPI_QCR_DT 0x2000
72 struct mcfqspi_cs_control
*cs_control
;
74 wait_queue_head_t waitq
;
77 static void mcfqspi_wr_qmr(struct mcfqspi
*mcfqspi
, u16 val
)
79 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QMR
);
82 static void mcfqspi_wr_qdlyr(struct mcfqspi
*mcfqspi
, u16 val
)
84 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QDLYR
);
87 static u16
mcfqspi_rd_qdlyr(struct mcfqspi
*mcfqspi
)
89 return readw(mcfqspi
->iobase
+ MCFQSPI_QDLYR
);
92 static void mcfqspi_wr_qwr(struct mcfqspi
*mcfqspi
, u16 val
)
94 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QWR
);
97 static void mcfqspi_wr_qir(struct mcfqspi
*mcfqspi
, u16 val
)
99 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QIR
);
102 static void mcfqspi_wr_qar(struct mcfqspi
*mcfqspi
, u16 val
)
104 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QAR
);
107 static void mcfqspi_wr_qdr(struct mcfqspi
*mcfqspi
, u16 val
)
109 writew(val
, mcfqspi
->iobase
+ MCFQSPI_QDR
);
112 static u16
mcfqspi_rd_qdr(struct mcfqspi
*mcfqspi
)
114 return readw(mcfqspi
->iobase
+ MCFQSPI_QDR
);
117 static void mcfqspi_cs_select(struct mcfqspi
*mcfqspi
, u8 chip_select
,
120 mcfqspi
->cs_control
->select(mcfqspi
->cs_control
, chip_select
, cs_high
);
123 static void mcfqspi_cs_deselect(struct mcfqspi
*mcfqspi
, u8 chip_select
,
126 mcfqspi
->cs_control
->deselect(mcfqspi
->cs_control
, chip_select
, cs_high
);
129 static int mcfqspi_cs_setup(struct mcfqspi
*mcfqspi
)
131 return (mcfqspi
->cs_control
->setup
) ?
132 mcfqspi
->cs_control
->setup(mcfqspi
->cs_control
) : 0;
135 static void mcfqspi_cs_teardown(struct mcfqspi
*mcfqspi
)
137 if (mcfqspi
->cs_control
->teardown
)
138 mcfqspi
->cs_control
->teardown(mcfqspi
->cs_control
);
141 static u8
mcfqspi_qmr_baud(u32 speed_hz
)
143 return clamp((MCFQSPI_BUSCLK
+ speed_hz
- 1) / speed_hz
, 2u, 255u);
146 static bool mcfqspi_qdlyr_spe(struct mcfqspi
*mcfqspi
)
148 return mcfqspi_rd_qdlyr(mcfqspi
) & MCFQSPI_QDLYR_SPE
;
151 static irqreturn_t
mcfqspi_irq_handler(int this_irq
, void *dev_id
)
153 struct mcfqspi
*mcfqspi
= dev_id
;
155 /* clear interrupt */
156 mcfqspi_wr_qir(mcfqspi
, MCFQSPI_QIR_SPIFE
| MCFQSPI_QIR_SPIF
);
157 wake_up(&mcfqspi
->waitq
);
162 static void mcfqspi_transfer_msg8(struct mcfqspi
*mcfqspi
, unsigned count
,
163 const u8
*txbuf
, u8
*rxbuf
)
165 unsigned i
, n
, offset
= 0;
169 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_CMDBUF
);
170 for (i
= 0; i
< n
; ++i
)
171 mcfqspi_wr_qdr(mcfqspi
, MCFQSPI_QCR_BITSE
);
173 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_TXBUF
);
175 for (i
= 0; i
< n
; ++i
)
176 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
178 for (i
= 0; i
< count
; ++i
)
179 mcfqspi_wr_qdr(mcfqspi
, 0);
184 mcfqspi_wr_qwr(mcfqspi
, 0x700);
185 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
188 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
189 mcfqspi_wr_qwr(mcfqspi
, qwr
);
190 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
192 mcfqspi_wr_qar(mcfqspi
,
193 MCFQSPI_QAR_RXBUF
+ offset
);
194 for (i
= 0; i
< 8; ++i
)
195 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
199 mcfqspi_wr_qar(mcfqspi
,
200 MCFQSPI_QAR_TXBUF
+ offset
);
201 for (i
= 0; i
< n
; ++i
)
202 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
204 qwr
= (offset
? 0x808 : 0) + ((n
- 1) << 8);
208 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
209 mcfqspi_wr_qwr(mcfqspi
, qwr
);
210 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
212 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
213 for (i
= 0; i
< 8; ++i
)
214 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
218 mcfqspi_wr_qwr(mcfqspi
, (n
- 1) << 8);
219 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
221 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
223 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
224 for (i
= 0; i
< n
; ++i
)
225 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
229 static void mcfqspi_transfer_msg16(struct mcfqspi
*mcfqspi
, unsigned count
,
230 const u16
*txbuf
, u16
*rxbuf
)
232 unsigned i
, n
, offset
= 0;
236 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_CMDBUF
);
237 for (i
= 0; i
< n
; ++i
)
238 mcfqspi_wr_qdr(mcfqspi
, MCFQSPI_QCR_BITSE
);
240 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_TXBUF
);
242 for (i
= 0; i
< n
; ++i
)
243 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
245 for (i
= 0; i
< count
; ++i
)
246 mcfqspi_wr_qdr(mcfqspi
, 0);
251 mcfqspi_wr_qwr(mcfqspi
, 0x700);
252 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
255 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
256 mcfqspi_wr_qwr(mcfqspi
, qwr
);
257 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
259 mcfqspi_wr_qar(mcfqspi
,
260 MCFQSPI_QAR_RXBUF
+ offset
);
261 for (i
= 0; i
< 8; ++i
)
262 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
266 mcfqspi_wr_qar(mcfqspi
,
267 MCFQSPI_QAR_TXBUF
+ offset
);
268 for (i
= 0; i
< n
; ++i
)
269 mcfqspi_wr_qdr(mcfqspi
, *txbuf
++);
271 qwr
= (offset
? 0x808 : 0x000) + ((n
- 1) << 8);
275 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
276 mcfqspi_wr_qwr(mcfqspi
, qwr
);
277 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
279 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
280 for (i
= 0; i
< 8; ++i
)
281 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
285 mcfqspi_wr_qwr(mcfqspi
, (n
- 1) << 8);
286 mcfqspi_wr_qdlyr(mcfqspi
, MCFQSPI_QDLYR_SPE
);
288 wait_event(mcfqspi
->waitq
, !mcfqspi_qdlyr_spe(mcfqspi
));
290 mcfqspi_wr_qar(mcfqspi
, MCFQSPI_QAR_RXBUF
+ offset
);
291 for (i
= 0; i
< n
; ++i
)
292 *rxbuf
++ = mcfqspi_rd_qdr(mcfqspi
);
296 static void mcfqspi_set_cs(struct spi_device
*spi
, bool enable
)
298 struct mcfqspi
*mcfqspi
= spi_master_get_devdata(spi
->master
);
299 bool cs_high
= spi
->mode
& SPI_CS_HIGH
;
302 mcfqspi_cs_select(mcfqspi
, spi
->chip_select
, cs_high
);
304 mcfqspi_cs_deselect(mcfqspi
, spi
->chip_select
, cs_high
);
307 static int mcfqspi_transfer_one(struct spi_master
*master
,
308 struct spi_device
*spi
,
309 struct spi_transfer
*t
)
311 struct mcfqspi
*mcfqspi
= spi_master_get_devdata(master
);
312 u16 qmr
= MCFQSPI_QMR_MSTR
;
314 qmr
|= t
->bits_per_word
<< 10;
315 if (spi
->mode
& SPI_CPHA
)
316 qmr
|= MCFQSPI_QMR_CPHA
;
317 if (spi
->mode
& SPI_CPOL
)
318 qmr
|= MCFQSPI_QMR_CPOL
;
319 qmr
|= mcfqspi_qmr_baud(t
->speed_hz
);
320 mcfqspi_wr_qmr(mcfqspi
, qmr
);
322 mcfqspi_wr_qir(mcfqspi
, MCFQSPI_QIR_SPIFE
);
323 if (t
->bits_per_word
== 8)
324 mcfqspi_transfer_msg8(mcfqspi
, t
->len
, t
->tx_buf
, t
->rx_buf
);
326 mcfqspi_transfer_msg16(mcfqspi
, t
->len
/ 2, t
->tx_buf
,
328 mcfqspi_wr_qir(mcfqspi
, 0);
333 static int mcfqspi_setup(struct spi_device
*spi
)
335 mcfqspi_cs_deselect(spi_master_get_devdata(spi
->master
),
336 spi
->chip_select
, spi
->mode
& SPI_CS_HIGH
);
339 "bits per word %d, chip select %d, speed %d KHz\n",
340 spi
->bits_per_word
, spi
->chip_select
,
341 (MCFQSPI_BUSCLK
/ mcfqspi_qmr_baud(spi
->max_speed_hz
))
347 static int mcfqspi_probe(struct platform_device
*pdev
)
349 struct spi_master
*master
;
350 struct mcfqspi
*mcfqspi
;
351 struct resource
*res
;
352 struct mcfqspi_platform_data
*pdata
;
355 pdata
= dev_get_platdata(&pdev
->dev
);
357 dev_dbg(&pdev
->dev
, "platform data is missing\n");
361 if (!pdata
->cs_control
) {
362 dev_dbg(&pdev
->dev
, "pdata->cs_control is NULL\n");
366 master
= spi_alloc_master(&pdev
->dev
, sizeof(*mcfqspi
));
367 if (master
== NULL
) {
368 dev_dbg(&pdev
->dev
, "spi_alloc_master failed\n");
372 mcfqspi
= spi_master_get_devdata(master
);
374 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
375 mcfqspi
->iobase
= devm_ioremap_resource(&pdev
->dev
, res
);
376 if (IS_ERR(mcfqspi
->iobase
)) {
377 status
= PTR_ERR(mcfqspi
->iobase
);
381 mcfqspi
->irq
= platform_get_irq(pdev
, 0);
382 if (mcfqspi
->irq
< 0) {
383 dev_dbg(&pdev
->dev
, "platform_get_irq failed\n");
388 status
= devm_request_irq(&pdev
->dev
, mcfqspi
->irq
, mcfqspi_irq_handler
,
389 0, pdev
->name
, mcfqspi
);
391 dev_dbg(&pdev
->dev
, "request_irq failed\n");
395 mcfqspi
->clk
= devm_clk_get(&pdev
->dev
, "qspi_clk");
396 if (IS_ERR(mcfqspi
->clk
)) {
397 dev_dbg(&pdev
->dev
, "clk_get failed\n");
398 status
= PTR_ERR(mcfqspi
->clk
);
401 clk_enable(mcfqspi
->clk
);
403 master
->bus_num
= pdata
->bus_num
;
404 master
->num_chipselect
= pdata
->num_chipselect
;
406 mcfqspi
->cs_control
= pdata
->cs_control
;
407 status
= mcfqspi_cs_setup(mcfqspi
);
409 dev_dbg(&pdev
->dev
, "error initializing cs_control\n");
413 init_waitqueue_head(&mcfqspi
->waitq
);
415 master
->mode_bits
= SPI_CS_HIGH
| SPI_CPOL
| SPI_CPHA
;
416 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(8, 16);
417 master
->setup
= mcfqspi_setup
;
418 master
->set_cs
= mcfqspi_set_cs
;
419 master
->transfer_one
= mcfqspi_transfer_one
;
420 master
->auto_runtime_pm
= true;
422 platform_set_drvdata(pdev
, master
);
424 status
= devm_spi_register_master(&pdev
->dev
, master
);
426 dev_dbg(&pdev
->dev
, "spi_register_master failed\n");
429 pm_runtime_enable(&pdev
->dev
);
431 dev_info(&pdev
->dev
, "Coldfire QSPI bus driver\n");
436 mcfqspi_cs_teardown(mcfqspi
);
438 clk_disable(mcfqspi
->clk
);
440 spi_master_put(master
);
442 dev_dbg(&pdev
->dev
, "Coldfire QSPI probe failed\n");
447 static int mcfqspi_remove(struct platform_device
*pdev
)
449 struct spi_master
*master
= platform_get_drvdata(pdev
);
450 struct mcfqspi
*mcfqspi
= spi_master_get_devdata(master
);
452 pm_runtime_disable(&pdev
->dev
);
453 /* disable the hardware (set the baud rate to 0) */
454 mcfqspi_wr_qmr(mcfqspi
, MCFQSPI_QMR_MSTR
);
456 mcfqspi_cs_teardown(mcfqspi
);
457 clk_disable(mcfqspi
->clk
);
462 #ifdef CONFIG_PM_SLEEP
463 static int mcfqspi_suspend(struct device
*dev
)
465 struct spi_master
*master
= dev_get_drvdata(dev
);
466 struct mcfqspi
*mcfqspi
= spi_master_get_devdata(master
);
469 ret
= spi_master_suspend(master
);
473 clk_disable(mcfqspi
->clk
);
478 static int mcfqspi_resume(struct device
*dev
)
480 struct spi_master
*master
= dev_get_drvdata(dev
);
481 struct mcfqspi
*mcfqspi
= spi_master_get_devdata(master
);
483 clk_enable(mcfqspi
->clk
);
485 return spi_master_resume(master
);
490 static int mcfqspi_runtime_suspend(struct device
*dev
)
492 struct spi_master
*master
= dev_get_drvdata(dev
);
493 struct mcfqspi
*mcfqspi
= spi_master_get_devdata(master
);
495 clk_disable(mcfqspi
->clk
);
500 static int mcfqspi_runtime_resume(struct device
*dev
)
502 struct spi_master
*master
= dev_get_drvdata(dev
);
503 struct mcfqspi
*mcfqspi
= spi_master_get_devdata(master
);
505 clk_enable(mcfqspi
->clk
);
511 static const struct dev_pm_ops mcfqspi_pm
= {
512 SET_SYSTEM_SLEEP_PM_OPS(mcfqspi_suspend
, mcfqspi_resume
)
513 SET_RUNTIME_PM_OPS(mcfqspi_runtime_suspend
, mcfqspi_runtime_resume
,
517 static struct platform_driver mcfqspi_driver
= {
518 .driver
.name
= DRIVER_NAME
,
519 .driver
.owner
= THIS_MODULE
,
520 .driver
.pm
= &mcfqspi_pm
,
521 .probe
= mcfqspi_probe
,
522 .remove
= mcfqspi_remove
,
524 module_platform_driver(mcfqspi_driver
);
526 MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
527 MODULE_DESCRIPTION("Coldfire QSPI Controller Driver");
528 MODULE_LICENSE("GPL");
529 MODULE_ALIAS("platform:" DRIVER_NAME
);