spi-coldfire-qspi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * Freescale/Motorola Coldfire Queued SPI driver
  3. *
  4. * Copyright 2010 Steven King <sfking@fdwdc.com>
  5. *
  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.
  10. *
  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.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/errno.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/sched.h>
  22. #include <linux/delay.h>
  23. #include <linux/io.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/pm_runtime.h>
  28. #include <asm/coldfire.h>
  29. #include <asm/mcfsim.h>
  30. #include <asm/mcfqspi.h>
  31. #define DRIVER_NAME "mcfqspi"
  32. #define MCFQSPI_BUSCLK (MCF_BUSCLK / 2)
  33. #define MCFQSPI_QMR 0x00
  34. #define MCFQSPI_QMR_MSTR 0x8000
  35. #define MCFQSPI_QMR_CPOL 0x0200
  36. #define MCFQSPI_QMR_CPHA 0x0100
  37. #define MCFQSPI_QDLYR 0x04
  38. #define MCFQSPI_QDLYR_SPE 0x8000
  39. #define MCFQSPI_QWR 0x08
  40. #define MCFQSPI_QWR_HALT 0x8000
  41. #define MCFQSPI_QWR_WREN 0x4000
  42. #define MCFQSPI_QWR_CSIV 0x1000
  43. #define MCFQSPI_QIR 0x0C
  44. #define MCFQSPI_QIR_WCEFB 0x8000
  45. #define MCFQSPI_QIR_ABRTB 0x4000
  46. #define MCFQSPI_QIR_ABRTL 0x1000
  47. #define MCFQSPI_QIR_WCEFE 0x0800
  48. #define MCFQSPI_QIR_ABRTE 0x0400
  49. #define MCFQSPI_QIR_SPIFE 0x0100
  50. #define MCFQSPI_QIR_WCEF 0x0008
  51. #define MCFQSPI_QIR_ABRT 0x0004
  52. #define MCFQSPI_QIR_SPIF 0x0001
  53. #define MCFQSPI_QAR 0x010
  54. #define MCFQSPI_QAR_TXBUF 0x00
  55. #define MCFQSPI_QAR_RXBUF 0x10
  56. #define MCFQSPI_QAR_CMDBUF 0x20
  57. #define MCFQSPI_QDR 0x014
  58. #define MCFQSPI_QCR 0x014
  59. #define MCFQSPI_QCR_CONT 0x8000
  60. #define MCFQSPI_QCR_BITSE 0x4000
  61. #define MCFQSPI_QCR_DT 0x2000
  62. struct mcfqspi {
  63. void __iomem *iobase;
  64. int irq;
  65. struct clk *clk;
  66. struct mcfqspi_cs_control *cs_control;
  67. wait_queue_head_t waitq;
  68. };
  69. static void mcfqspi_wr_qmr(struct mcfqspi *mcfqspi, u16 val)
  70. {
  71. writew(val, mcfqspi->iobase + MCFQSPI_QMR);
  72. }
  73. static void mcfqspi_wr_qdlyr(struct mcfqspi *mcfqspi, u16 val)
  74. {
  75. writew(val, mcfqspi->iobase + MCFQSPI_QDLYR);
  76. }
  77. static u16 mcfqspi_rd_qdlyr(struct mcfqspi *mcfqspi)
  78. {
  79. return readw(mcfqspi->iobase + MCFQSPI_QDLYR);
  80. }
  81. static void mcfqspi_wr_qwr(struct mcfqspi *mcfqspi, u16 val)
  82. {
  83. writew(val, mcfqspi->iobase + MCFQSPI_QWR);
  84. }
  85. static void mcfqspi_wr_qir(struct mcfqspi *mcfqspi, u16 val)
  86. {
  87. writew(val, mcfqspi->iobase + MCFQSPI_QIR);
  88. }
  89. static void mcfqspi_wr_qar(struct mcfqspi *mcfqspi, u16 val)
  90. {
  91. writew(val, mcfqspi->iobase + MCFQSPI_QAR);
  92. }
  93. static void mcfqspi_wr_qdr(struct mcfqspi *mcfqspi, u16 val)
  94. {
  95. writew(val, mcfqspi->iobase + MCFQSPI_QDR);
  96. }
  97. static u16 mcfqspi_rd_qdr(struct mcfqspi *mcfqspi)
  98. {
  99. return readw(mcfqspi->iobase + MCFQSPI_QDR);
  100. }
  101. static void mcfqspi_cs_select(struct mcfqspi *mcfqspi, u8 chip_select,
  102. bool cs_high)
  103. {
  104. mcfqspi->cs_control->select(mcfqspi->cs_control, chip_select, cs_high);
  105. }
  106. static void mcfqspi_cs_deselect(struct mcfqspi *mcfqspi, u8 chip_select,
  107. bool cs_high)
  108. {
  109. mcfqspi->cs_control->deselect(mcfqspi->cs_control, chip_select, cs_high);
  110. }
  111. static int mcfqspi_cs_setup(struct mcfqspi *mcfqspi)
  112. {
  113. return (mcfqspi->cs_control->setup) ?
  114. mcfqspi->cs_control->setup(mcfqspi->cs_control) : 0;
  115. }
  116. static void mcfqspi_cs_teardown(struct mcfqspi *mcfqspi)
  117. {
  118. if (mcfqspi->cs_control->teardown)
  119. mcfqspi->cs_control->teardown(mcfqspi->cs_control);
  120. }
  121. static u8 mcfqspi_qmr_baud(u32 speed_hz)
  122. {
  123. return clamp((MCFQSPI_BUSCLK + speed_hz - 1) / speed_hz, 2u, 255u);
  124. }
  125. static bool mcfqspi_qdlyr_spe(struct mcfqspi *mcfqspi)
  126. {
  127. return mcfqspi_rd_qdlyr(mcfqspi) & MCFQSPI_QDLYR_SPE;
  128. }
  129. static irqreturn_t mcfqspi_irq_handler(int this_irq, void *dev_id)
  130. {
  131. struct mcfqspi *mcfqspi = dev_id;
  132. /* clear interrupt */
  133. mcfqspi_wr_qir(mcfqspi, MCFQSPI_QIR_SPIFE | MCFQSPI_QIR_SPIF);
  134. wake_up(&mcfqspi->waitq);
  135. return IRQ_HANDLED;
  136. }
  137. static void mcfqspi_transfer_msg8(struct mcfqspi *mcfqspi, unsigned count,
  138. const u8 *txbuf, u8 *rxbuf)
  139. {
  140. unsigned i, n, offset = 0;
  141. n = min(count, 16u);
  142. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_CMDBUF);
  143. for (i = 0; i < n; ++i)
  144. mcfqspi_wr_qdr(mcfqspi, MCFQSPI_QCR_BITSE);
  145. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_TXBUF);
  146. if (txbuf)
  147. for (i = 0; i < n; ++i)
  148. mcfqspi_wr_qdr(mcfqspi, *txbuf++);
  149. else
  150. for (i = 0; i < count; ++i)
  151. mcfqspi_wr_qdr(mcfqspi, 0);
  152. count -= n;
  153. if (count) {
  154. u16 qwr = 0xf08;
  155. mcfqspi_wr_qwr(mcfqspi, 0x700);
  156. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  157. do {
  158. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  159. mcfqspi_wr_qwr(mcfqspi, qwr);
  160. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  161. if (rxbuf) {
  162. mcfqspi_wr_qar(mcfqspi,
  163. MCFQSPI_QAR_RXBUF + offset);
  164. for (i = 0; i < 8; ++i)
  165. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  166. }
  167. n = min(count, 8u);
  168. if (txbuf) {
  169. mcfqspi_wr_qar(mcfqspi,
  170. MCFQSPI_QAR_TXBUF + offset);
  171. for (i = 0; i < n; ++i)
  172. mcfqspi_wr_qdr(mcfqspi, *txbuf++);
  173. }
  174. qwr = (offset ? 0x808 : 0) + ((n - 1) << 8);
  175. offset ^= 8;
  176. count -= n;
  177. } while (count);
  178. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  179. mcfqspi_wr_qwr(mcfqspi, qwr);
  180. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  181. if (rxbuf) {
  182. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
  183. for (i = 0; i < 8; ++i)
  184. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  185. offset ^= 8;
  186. }
  187. } else {
  188. mcfqspi_wr_qwr(mcfqspi, (n - 1) << 8);
  189. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  190. }
  191. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  192. if (rxbuf) {
  193. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
  194. for (i = 0; i < n; ++i)
  195. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  196. }
  197. }
  198. static void mcfqspi_transfer_msg16(struct mcfqspi *mcfqspi, unsigned count,
  199. const u16 *txbuf, u16 *rxbuf)
  200. {
  201. unsigned i, n, offset = 0;
  202. n = min(count, 16u);
  203. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_CMDBUF);
  204. for (i = 0; i < n; ++i)
  205. mcfqspi_wr_qdr(mcfqspi, MCFQSPI_QCR_BITSE);
  206. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_TXBUF);
  207. if (txbuf)
  208. for (i = 0; i < n; ++i)
  209. mcfqspi_wr_qdr(mcfqspi, *txbuf++);
  210. else
  211. for (i = 0; i < count; ++i)
  212. mcfqspi_wr_qdr(mcfqspi, 0);
  213. count -= n;
  214. if (count) {
  215. u16 qwr = 0xf08;
  216. mcfqspi_wr_qwr(mcfqspi, 0x700);
  217. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  218. do {
  219. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  220. mcfqspi_wr_qwr(mcfqspi, qwr);
  221. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  222. if (rxbuf) {
  223. mcfqspi_wr_qar(mcfqspi,
  224. MCFQSPI_QAR_RXBUF + offset);
  225. for (i = 0; i < 8; ++i)
  226. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  227. }
  228. n = min(count, 8u);
  229. if (txbuf) {
  230. mcfqspi_wr_qar(mcfqspi,
  231. MCFQSPI_QAR_TXBUF + offset);
  232. for (i = 0; i < n; ++i)
  233. mcfqspi_wr_qdr(mcfqspi, *txbuf++);
  234. }
  235. qwr = (offset ? 0x808 : 0x000) + ((n - 1) << 8);
  236. offset ^= 8;
  237. count -= n;
  238. } while (count);
  239. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  240. mcfqspi_wr_qwr(mcfqspi, qwr);
  241. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  242. if (rxbuf) {
  243. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
  244. for (i = 0; i < 8; ++i)
  245. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  246. offset ^= 8;
  247. }
  248. } else {
  249. mcfqspi_wr_qwr(mcfqspi, (n - 1) << 8);
  250. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  251. }
  252. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  253. if (rxbuf) {
  254. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
  255. for (i = 0; i < n; ++i)
  256. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  257. }
  258. }
  259. static void mcfqspi_set_cs(struct spi_device *spi, bool enable)
  260. {
  261. struct mcfqspi *mcfqspi = spi_master_get_devdata(spi->master);
  262. bool cs_high = spi->mode & SPI_CS_HIGH;
  263. if (enable)
  264. mcfqspi_cs_select(mcfqspi, spi->chip_select, cs_high);
  265. else
  266. mcfqspi_cs_deselect(mcfqspi, spi->chip_select, cs_high);
  267. }
  268. static int mcfqspi_transfer_one(struct spi_master *master,
  269. struct spi_device *spi,
  270. struct spi_transfer *t)
  271. {
  272. struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
  273. u16 qmr = MCFQSPI_QMR_MSTR;
  274. qmr |= t->bits_per_word << 10;
  275. if (spi->mode & SPI_CPHA)
  276. qmr |= MCFQSPI_QMR_CPHA;
  277. if (spi->mode & SPI_CPOL)
  278. qmr |= MCFQSPI_QMR_CPOL;
  279. qmr |= mcfqspi_qmr_baud(t->speed_hz);
  280. mcfqspi_wr_qmr(mcfqspi, qmr);
  281. mcfqspi_wr_qir(mcfqspi, MCFQSPI_QIR_SPIFE);
  282. if (t->bits_per_word == 8)
  283. mcfqspi_transfer_msg8(mcfqspi, t->len, t->tx_buf, t->rx_buf);
  284. else
  285. mcfqspi_transfer_msg16(mcfqspi, t->len / 2, t->tx_buf,
  286. t->rx_buf);
  287. mcfqspi_wr_qir(mcfqspi, 0);
  288. return 0;
  289. }
  290. static int mcfqspi_setup(struct spi_device *spi)
  291. {
  292. mcfqspi_cs_deselect(spi_master_get_devdata(spi->master),
  293. spi->chip_select, spi->mode & SPI_CS_HIGH);
  294. dev_dbg(&spi->dev,
  295. "bits per word %d, chip select %d, speed %d KHz\n",
  296. spi->bits_per_word, spi->chip_select,
  297. (MCFQSPI_BUSCLK / mcfqspi_qmr_baud(spi->max_speed_hz))
  298. / 1000);
  299. return 0;
  300. }
  301. static int mcfqspi_probe(struct platform_device *pdev)
  302. {
  303. struct spi_master *master;
  304. struct mcfqspi *mcfqspi;
  305. struct resource *res;
  306. struct mcfqspi_platform_data *pdata;
  307. int status;
  308. pdata = dev_get_platdata(&pdev->dev);
  309. if (!pdata) {
  310. dev_dbg(&pdev->dev, "platform data is missing\n");
  311. return -ENOENT;
  312. }
  313. if (!pdata->cs_control) {
  314. dev_dbg(&pdev->dev, "pdata->cs_control is NULL\n");
  315. return -EINVAL;
  316. }
  317. master = spi_alloc_master(&pdev->dev, sizeof(*mcfqspi));
  318. if (master == NULL) {
  319. dev_dbg(&pdev->dev, "spi_alloc_master failed\n");
  320. return -ENOMEM;
  321. }
  322. mcfqspi = spi_master_get_devdata(master);
  323. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  324. mcfqspi->iobase = devm_ioremap_resource(&pdev->dev, res);
  325. if (IS_ERR(mcfqspi->iobase)) {
  326. status = PTR_ERR(mcfqspi->iobase);
  327. goto fail0;
  328. }
  329. mcfqspi->irq = platform_get_irq(pdev, 0);
  330. if (mcfqspi->irq < 0) {
  331. dev_dbg(&pdev->dev, "platform_get_irq failed\n");
  332. status = -ENXIO;
  333. goto fail0;
  334. }
  335. status = devm_request_irq(&pdev->dev, mcfqspi->irq, mcfqspi_irq_handler,
  336. 0, pdev->name, mcfqspi);
  337. if (status) {
  338. dev_dbg(&pdev->dev, "request_irq failed\n");
  339. goto fail0;
  340. }
  341. mcfqspi->clk = devm_clk_get(&pdev->dev, "qspi_clk");
  342. if (IS_ERR(mcfqspi->clk)) {
  343. dev_dbg(&pdev->dev, "clk_get failed\n");
  344. status = PTR_ERR(mcfqspi->clk);
  345. goto fail0;
  346. }
  347. clk_enable(mcfqspi->clk);
  348. master->bus_num = pdata->bus_num;
  349. master->num_chipselect = pdata->num_chipselect;
  350. mcfqspi->cs_control = pdata->cs_control;
  351. status = mcfqspi_cs_setup(mcfqspi);
  352. if (status) {
  353. dev_dbg(&pdev->dev, "error initializing cs_control\n");
  354. goto fail1;
  355. }
  356. init_waitqueue_head(&mcfqspi->waitq);
  357. master->mode_bits = SPI_CS_HIGH | SPI_CPOL | SPI_CPHA;
  358. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16);
  359. master->setup = mcfqspi_setup;
  360. master->set_cs = mcfqspi_set_cs;
  361. master->transfer_one = mcfqspi_transfer_one;
  362. master->auto_runtime_pm = true;
  363. platform_set_drvdata(pdev, master);
  364. pm_runtime_enable(&pdev->dev);
  365. status = devm_spi_register_master(&pdev->dev, master);
  366. if (status) {
  367. dev_dbg(&pdev->dev, "spi_register_master failed\n");
  368. goto fail2;
  369. }
  370. dev_info(&pdev->dev, "Coldfire QSPI bus driver\n");
  371. return 0;
  372. fail2:
  373. pm_runtime_disable(&pdev->dev);
  374. mcfqspi_cs_teardown(mcfqspi);
  375. fail1:
  376. clk_disable(mcfqspi->clk);
  377. fail0:
  378. spi_master_put(master);
  379. dev_dbg(&pdev->dev, "Coldfire QSPI probe failed\n");
  380. return status;
  381. }
  382. static int mcfqspi_remove(struct platform_device *pdev)
  383. {
  384. struct spi_master *master = platform_get_drvdata(pdev);
  385. struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
  386. pm_runtime_disable(&pdev->dev);
  387. /* disable the hardware (set the baud rate to 0) */
  388. mcfqspi_wr_qmr(mcfqspi, MCFQSPI_QMR_MSTR);
  389. mcfqspi_cs_teardown(mcfqspi);
  390. clk_disable(mcfqspi->clk);
  391. return 0;
  392. }
  393. #ifdef CONFIG_PM_SLEEP
  394. static int mcfqspi_suspend(struct device *dev)
  395. {
  396. struct spi_master *master = dev_get_drvdata(dev);
  397. struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
  398. int ret;
  399. ret = spi_master_suspend(master);
  400. if (ret)
  401. return ret;
  402. clk_disable(mcfqspi->clk);
  403. return 0;
  404. }
  405. static int mcfqspi_resume(struct device *dev)
  406. {
  407. struct spi_master *master = dev_get_drvdata(dev);
  408. struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
  409. clk_enable(mcfqspi->clk);
  410. return spi_master_resume(master);
  411. }
  412. #endif
  413. #ifdef CONFIG_PM
  414. static int mcfqspi_runtime_suspend(struct device *dev)
  415. {
  416. struct spi_master *master = dev_get_drvdata(dev);
  417. struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
  418. clk_disable(mcfqspi->clk);
  419. return 0;
  420. }
  421. static int mcfqspi_runtime_resume(struct device *dev)
  422. {
  423. struct spi_master *master = dev_get_drvdata(dev);
  424. struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
  425. clk_enable(mcfqspi->clk);
  426. return 0;
  427. }
  428. #endif
  429. static const struct dev_pm_ops mcfqspi_pm = {
  430. SET_SYSTEM_SLEEP_PM_OPS(mcfqspi_suspend, mcfqspi_resume)
  431. SET_RUNTIME_PM_OPS(mcfqspi_runtime_suspend, mcfqspi_runtime_resume,
  432. NULL)
  433. };
  434. static struct platform_driver mcfqspi_driver = {
  435. .driver.name = DRIVER_NAME,
  436. .driver.owner = THIS_MODULE,
  437. .driver.pm = &mcfqspi_pm,
  438. .probe = mcfqspi_probe,
  439. .remove = mcfqspi_remove,
  440. };
  441. module_platform_driver(mcfqspi_driver);
  442. MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
  443. MODULE_DESCRIPTION("Coldfire QSPI Controller Driver");
  444. MODULE_LICENSE("GPL");
  445. MODULE_ALIAS("platform:" DRIVER_NAME);