22 #include <linux/kernel.h>
23 #include <linux/module.h>
25 #include <linux/errno.h>
27 #include <linux/sched.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
130 static void mcfqspi_cs_deselect(
struct mcfqspi *mcfqspi,
u8 chip_select,
136 static int mcfqspi_cs_setup(
struct mcfqspi *mcfqspi)
142 static void mcfqspi_cs_teardown(
struct mcfqspi *mcfqspi)
148 static u8 mcfqspi_qmr_baud(
u32 speed_hz)
153 static bool mcfqspi_qdlyr_spe(
struct mcfqspi *mcfqspi)
160 struct mcfqspi *mcfqspi =
dev_id;
169 static void mcfqspi_transfer_msg8(
struct mcfqspi *mcfqspi,
unsigned count,
170 const u8 *txbuf,
u8 *rxbuf)
177 for (i = 0; i <
n; ++
i)
182 for (i = 0; i <
n; ++
i)
183 mcfqspi_wr_qdr(mcfqspi, *txbuf++);
185 for (i = 0; i <
count; ++
i)
186 mcfqspi_wr_qdr(mcfqspi, 0);
191 mcfqspi_wr_qwr(mcfqspi, 0x700);
196 mcfqspi_wr_qwr(mcfqspi, qwr);
199 mcfqspi_wr_qar(mcfqspi,
201 for (i = 0; i < 8; ++
i)
202 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
206 mcfqspi_wr_qar(mcfqspi,
208 for (i = 0; i <
n; ++
i)
209 mcfqspi_wr_qdr(mcfqspi, *txbuf++);
211 qwr = (offset ? 0x808 : 0) + ((n - 1) << 8);
216 mcfqspi_wr_qwr(mcfqspi, qwr);
220 for (i = 0; i < 8; ++
i)
221 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
225 mcfqspi_wr_qwr(mcfqspi, (n - 1) << 8);
231 for (i = 0; i <
n; ++
i)
232 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
236 static void mcfqspi_transfer_msg16(
struct mcfqspi *mcfqspi,
unsigned count,
237 const u16 *txbuf,
u16 *rxbuf)
239 unsigned i,
n, offset = 0;
244 for (i = 0; i <
n; ++
i)
249 for (i = 0; i <
n; ++
i)
250 mcfqspi_wr_qdr(mcfqspi, *txbuf++);
252 for (i = 0; i <
count; ++
i)
253 mcfqspi_wr_qdr(mcfqspi, 0);
258 mcfqspi_wr_qwr(mcfqspi, 0x700);
263 mcfqspi_wr_qwr(mcfqspi, qwr);
266 mcfqspi_wr_qar(mcfqspi,
268 for (i = 0; i < 8; ++
i)
269 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
273 mcfqspi_wr_qar(mcfqspi,
275 for (i = 0; i <
n; ++
i)
276 mcfqspi_wr_qdr(mcfqspi, *txbuf++);
278 qwr = (offset ? 0x808 : 0x000) + ((n - 1) << 8);
283 mcfqspi_wr_qwr(mcfqspi, qwr);
287 for (i = 0; i < 8; ++
i)
288 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
292 mcfqspi_wr_qwr(mcfqspi, (n - 1) << 8);
298 for (i = 0; i <
n; ++
i)
299 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
303 static int mcfqspi_transfer_one_message(
struct spi_master *master,
306 struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
324 qmr |= mcfqspi_qmr_baud(t->
speed_hz);
327 mcfqspi_wr_qmr(mcfqspi, qmr);
329 mcfqspi_cs_select(mcfqspi, spi->
chip_select, cs_high);
334 mcfqspi_transfer_msg8(mcfqspi, t->
len, t->
tx_buf,
337 mcfqspi_transfer_msg16(mcfqspi, t->
len / 2, t->
tx_buf,
339 mcfqspi_wr_qir(mcfqspi, 0);
361 static int mcfqspi_prepare_transfer_hw(
struct spi_master *master)
363 struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
365 pm_runtime_get_sync(mcfqspi->
dev);
370 static int mcfqspi_unprepare_transfer_hw(
struct spi_master *master)
372 struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
374 pm_runtime_put_sync(mcfqspi->
dev);
379 static int mcfqspi_setup(
struct spi_device *spi)
382 dev_dbg(&spi->
dev,
"%d bits per word is not supported\n",
387 dev_dbg(&spi->
dev,
"%d chip select is out of range\n",
392 mcfqspi_cs_deselect(spi_master_get_devdata(spi->
master),
396 "bits per word %d, chip select %d, speed %d KHz\n",
407 struct mcfqspi *mcfqspi;
413 if (master ==
NULL) {
414 dev_dbg(&pdev->
dev,
"spi_alloc_master failed\n");
418 mcfqspi = spi_master_get_devdata(master);
422 dev_dbg(&pdev->
dev,
"platform_get_resource failed\n");
428 dev_dbg(&pdev->
dev,
"request_mem_region failed\n");
441 if (mcfqspi->
irq < 0) {
442 dev_dbg(&pdev->
dev,
"platform_get_irq failed\n");
448 pdev->
name, mcfqspi);
455 if (IS_ERR(mcfqspi->
clk)) {
457 status = PTR_ERR(mcfqspi->
clk);
462 pdata = pdev->
dev.platform_data;
464 dev_dbg(&pdev->
dev,
"platform data is missing\n");
471 status = mcfqspi_cs_setup(mcfqspi);
473 dev_dbg(&pdev->
dev,
"error initializing cs_control\n");
478 mcfqspi->
dev = &pdev->
dev;
481 master->
setup = mcfqspi_setup;
486 platform_set_drvdata(pdev, master);
490 dev_dbg(&pdev->
dev,
"spi_register_master failed\n");
495 dev_info(&pdev->
dev,
"Coldfire QSPI bus driver\n");
500 mcfqspi_cs_teardown(mcfqspi);
511 spi_master_put(master);
513 dev_dbg(&pdev->
dev,
"Coldfire QSPI probe failed\n");
520 struct spi_master *master = platform_get_drvdata(pdev);
521 struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
524 pm_runtime_disable(mcfqspi->
dev);
528 platform_set_drvdata(pdev,
NULL);
529 mcfqspi_cs_teardown(mcfqspi);
540 #ifdef CONFIG_PM_SLEEP
541 static int mcfqspi_suspend(
struct device *
dev)
544 struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
553 static int mcfqspi_resume(
struct device *
dev)
556 struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
566 #ifdef CONFIG_PM_RUNTIME
567 static int mcfqspi_runtime_suspend(
struct device *
dev)
576 static int mcfqspi_runtime_resume(
struct device *dev)
595 .driver.pm = &mcfqspi_pm,
596 .probe = mcfqspi_probe,