coldfire_qspi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. * 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
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/errno.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/sched.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/delay.h>
  29. #include <linux/io.h>
  30. #include <linux/clk.h>
  31. #include <linux/err.h>
  32. #include <linux/spi/spi.h>
  33. #include <asm/coldfire.h>
  34. #include <asm/mcfsim.h>
  35. #include <asm/mcfqspi.h>
  36. #define DRIVER_NAME "mcfqspi"
  37. #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
  67. struct mcfqspi {
  68. void __iomem *iobase;
  69. int irq;
  70. struct clk *clk;
  71. struct mcfqspi_cs_control *cs_control;
  72. wait_queue_head_t waitq;
  73. struct work_struct work;
  74. struct workqueue_struct *workq;
  75. spinlock_t lock;
  76. struct list_head msgq;
  77. };
  78. static void mcfqspi_wr_qmr(struct mcfqspi *mcfqspi, u16 val)
  79. {
  80. writew(val, mcfqspi->iobase + MCFQSPI_QMR);
  81. }
  82. static void mcfqspi_wr_qdlyr(struct mcfqspi *mcfqspi, u16 val)
  83. {
  84. writew(val, mcfqspi->iobase + MCFQSPI_QDLYR);
  85. }
  86. static u16 mcfqspi_rd_qdlyr(struct mcfqspi *mcfqspi)
  87. {
  88. return readw(mcfqspi->iobase + MCFQSPI_QDLYR);
  89. }
  90. static void mcfqspi_wr_qwr(struct mcfqspi *mcfqspi, u16 val)
  91. {
  92. writew(val, mcfqspi->iobase + MCFQSPI_QWR);
  93. }
  94. static void mcfqspi_wr_qir(struct mcfqspi *mcfqspi, u16 val)
  95. {
  96. writew(val, mcfqspi->iobase + MCFQSPI_QIR);
  97. }
  98. static void mcfqspi_wr_qar(struct mcfqspi *mcfqspi, u16 val)
  99. {
  100. writew(val, mcfqspi->iobase + MCFQSPI_QAR);
  101. }
  102. static void mcfqspi_wr_qdr(struct mcfqspi *mcfqspi, u16 val)
  103. {
  104. writew(val, mcfqspi->iobase + MCFQSPI_QDR);
  105. }
  106. static u16 mcfqspi_rd_qdr(struct mcfqspi *mcfqspi)
  107. {
  108. return readw(mcfqspi->iobase + MCFQSPI_QDR);
  109. }
  110. static void mcfqspi_cs_select(struct mcfqspi *mcfqspi, u8 chip_select,
  111. bool cs_high)
  112. {
  113. mcfqspi->cs_control->select(mcfqspi->cs_control, chip_select, cs_high);
  114. }
  115. static void mcfqspi_cs_deselect(struct mcfqspi *mcfqspi, u8 chip_select,
  116. bool cs_high)
  117. {
  118. mcfqspi->cs_control->deselect(mcfqspi->cs_control, chip_select, cs_high);
  119. }
  120. static int mcfqspi_cs_setup(struct mcfqspi *mcfqspi)
  121. {
  122. return (mcfqspi->cs_control && mcfqspi->cs_control->setup) ?
  123. mcfqspi->cs_control->setup(mcfqspi->cs_control) : 0;
  124. }
  125. static void mcfqspi_cs_teardown(struct mcfqspi *mcfqspi)
  126. {
  127. if (mcfqspi->cs_control && mcfqspi->cs_control->teardown)
  128. mcfqspi->cs_control->teardown(mcfqspi->cs_control);
  129. }
  130. static u8 mcfqspi_qmr_baud(u32 speed_hz)
  131. {
  132. return clamp((MCFQSPI_BUSCLK + speed_hz - 1) / speed_hz, 2u, 255u);
  133. }
  134. static bool mcfqspi_qdlyr_spe(struct mcfqspi *mcfqspi)
  135. {
  136. return mcfqspi_rd_qdlyr(mcfqspi) & MCFQSPI_QDLYR_SPE;
  137. }
  138. static irqreturn_t mcfqspi_irq_handler(int this_irq, void *dev_id)
  139. {
  140. struct mcfqspi *mcfqspi = dev_id;
  141. /* clear interrupt */
  142. mcfqspi_wr_qir(mcfqspi, MCFQSPI_QIR_SPIFE | MCFQSPI_QIR_SPIF);
  143. wake_up(&mcfqspi->waitq);
  144. return IRQ_HANDLED;
  145. }
  146. static void mcfqspi_transfer_msg8(struct mcfqspi *mcfqspi, unsigned count,
  147. const u8 *txbuf, u8 *rxbuf)
  148. {
  149. unsigned i, n, offset = 0;
  150. n = min(count, 16u);
  151. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_CMDBUF);
  152. for (i = 0; i < n; ++i)
  153. mcfqspi_wr_qdr(mcfqspi, MCFQSPI_QCR_BITSE);
  154. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_TXBUF);
  155. if (txbuf)
  156. for (i = 0; i < n; ++i)
  157. mcfqspi_wr_qdr(mcfqspi, *txbuf++);
  158. else
  159. for (i = 0; i < count; ++i)
  160. mcfqspi_wr_qdr(mcfqspi, 0);
  161. count -= n;
  162. if (count) {
  163. u16 qwr = 0xf08;
  164. mcfqspi_wr_qwr(mcfqspi, 0x700);
  165. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  166. do {
  167. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  168. mcfqspi_wr_qwr(mcfqspi, qwr);
  169. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  170. if (rxbuf) {
  171. mcfqspi_wr_qar(mcfqspi,
  172. MCFQSPI_QAR_RXBUF + offset);
  173. for (i = 0; i < 8; ++i)
  174. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  175. }
  176. n = min(count, 8u);
  177. if (txbuf) {
  178. mcfqspi_wr_qar(mcfqspi,
  179. MCFQSPI_QAR_TXBUF + offset);
  180. for (i = 0; i < n; ++i)
  181. mcfqspi_wr_qdr(mcfqspi, *txbuf++);
  182. }
  183. qwr = (offset ? 0x808 : 0) + ((n - 1) << 8);
  184. offset ^= 8;
  185. count -= n;
  186. } while (count);
  187. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  188. mcfqspi_wr_qwr(mcfqspi, qwr);
  189. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  190. if (rxbuf) {
  191. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
  192. for (i = 0; i < 8; ++i)
  193. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  194. offset ^= 8;
  195. }
  196. } else {
  197. mcfqspi_wr_qwr(mcfqspi, (n - 1) << 8);
  198. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  199. }
  200. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  201. if (rxbuf) {
  202. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
  203. for (i = 0; i < n; ++i)
  204. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  205. }
  206. }
  207. static void mcfqspi_transfer_msg16(struct mcfqspi *mcfqspi, unsigned count,
  208. const u16 *txbuf, u16 *rxbuf)
  209. {
  210. unsigned i, n, offset = 0;
  211. n = min(count, 16u);
  212. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_CMDBUF);
  213. for (i = 0; i < n; ++i)
  214. mcfqspi_wr_qdr(mcfqspi, MCFQSPI_QCR_BITSE);
  215. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_TXBUF);
  216. if (txbuf)
  217. for (i = 0; i < n; ++i)
  218. mcfqspi_wr_qdr(mcfqspi, *txbuf++);
  219. else
  220. for (i = 0; i < count; ++i)
  221. mcfqspi_wr_qdr(mcfqspi, 0);
  222. count -= n;
  223. if (count) {
  224. u16 qwr = 0xf08;
  225. mcfqspi_wr_qwr(mcfqspi, 0x700);
  226. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  227. do {
  228. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  229. mcfqspi_wr_qwr(mcfqspi, qwr);
  230. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  231. if (rxbuf) {
  232. mcfqspi_wr_qar(mcfqspi,
  233. MCFQSPI_QAR_RXBUF + offset);
  234. for (i = 0; i < 8; ++i)
  235. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  236. }
  237. n = min(count, 8u);
  238. if (txbuf) {
  239. mcfqspi_wr_qar(mcfqspi,
  240. MCFQSPI_QAR_TXBUF + offset);
  241. for (i = 0; i < n; ++i)
  242. mcfqspi_wr_qdr(mcfqspi, *txbuf++);
  243. }
  244. qwr = (offset ? 0x808 : 0x000) + ((n - 1) << 8);
  245. offset ^= 8;
  246. count -= n;
  247. } while (count);
  248. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  249. mcfqspi_wr_qwr(mcfqspi, qwr);
  250. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  251. if (rxbuf) {
  252. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
  253. for (i = 0; i < 8; ++i)
  254. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  255. offset ^= 8;
  256. }
  257. } else {
  258. mcfqspi_wr_qwr(mcfqspi, (n - 1) << 8);
  259. mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
  260. }
  261. wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
  262. if (rxbuf) {
  263. mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
  264. for (i = 0; i < n; ++i)
  265. *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
  266. }
  267. }
  268. static void mcfqspi_work(struct work_struct *work)
  269. {
  270. struct mcfqspi *mcfqspi = container_of(work, struct mcfqspi, work);
  271. unsigned long flags;
  272. spin_lock_irqsave(&mcfqspi->lock, flags);
  273. while (!list_empty(&mcfqspi->msgq)) {
  274. struct spi_message *msg;
  275. struct spi_device *spi;
  276. struct spi_transfer *xfer;
  277. int status = 0;
  278. msg = container_of(mcfqspi->msgq.next, struct spi_message,
  279. queue);
  280. list_del_init(&msg->queue);
  281. spin_unlock_irqrestore(&mcfqspi->lock, flags);
  282. spi = msg->spi;
  283. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  284. bool cs_high = spi->mode & SPI_CS_HIGH;
  285. u16 qmr = MCFQSPI_QMR_MSTR;
  286. if (xfer->bits_per_word)
  287. qmr |= xfer->bits_per_word << 10;
  288. else
  289. qmr |= spi->bits_per_word << 10;
  290. if (spi->mode & SPI_CPHA)
  291. qmr |= MCFQSPI_QMR_CPHA;
  292. if (spi->mode & SPI_CPOL)
  293. qmr |= MCFQSPI_QMR_CPOL;
  294. if (xfer->speed_hz)
  295. qmr |= mcfqspi_qmr_baud(xfer->speed_hz);
  296. else
  297. qmr |= mcfqspi_qmr_baud(spi->max_speed_hz);
  298. mcfqspi_wr_qmr(mcfqspi, qmr);
  299. mcfqspi_cs_select(mcfqspi, spi->chip_select, cs_high);
  300. mcfqspi_wr_qir(mcfqspi, MCFQSPI_QIR_SPIFE);
  301. if ((xfer->bits_per_word ? xfer->bits_per_word :
  302. spi->bits_per_word) == 8)
  303. mcfqspi_transfer_msg8(mcfqspi, xfer->len,
  304. xfer->tx_buf,
  305. xfer->rx_buf);
  306. else
  307. mcfqspi_transfer_msg16(mcfqspi, xfer->len / 2,
  308. xfer->tx_buf,
  309. xfer->rx_buf);
  310. mcfqspi_wr_qir(mcfqspi, 0);
  311. if (xfer->delay_usecs)
  312. udelay(xfer->delay_usecs);
  313. if (xfer->cs_change) {
  314. if (!list_is_last(&xfer->transfer_list,
  315. &msg->transfers))
  316. mcfqspi_cs_deselect(mcfqspi,
  317. spi->chip_select,
  318. cs_high);
  319. } else {
  320. if (list_is_last(&xfer->transfer_list,
  321. &msg->transfers))
  322. mcfqspi_cs_deselect(mcfqspi,
  323. spi->chip_select,
  324. cs_high);
  325. }
  326. msg->actual_length += xfer->len;
  327. }
  328. msg->status = status;
  329. msg->complete(msg->context);
  330. spin_lock_irqsave(&mcfqspi->lock, flags);
  331. }
  332. spin_unlock_irqrestore(&mcfqspi->lock, flags);
  333. }
  334. static int mcfqspi_transfer(struct spi_device *spi, struct spi_message *msg)
  335. {
  336. struct mcfqspi *mcfqspi;
  337. struct spi_transfer *xfer;
  338. unsigned long flags;
  339. mcfqspi = spi_master_get_devdata(spi->master);
  340. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  341. if (xfer->bits_per_word && ((xfer->bits_per_word < 8)
  342. || (xfer->bits_per_word > 16))) {
  343. dev_dbg(&spi->dev,
  344. "%d bits per word is not supported\n",
  345. xfer->bits_per_word);
  346. goto fail;
  347. }
  348. if (xfer->speed_hz) {
  349. u32 real_speed = MCFQSPI_BUSCLK /
  350. mcfqspi_qmr_baud(xfer->speed_hz);
  351. if (real_speed != xfer->speed_hz)
  352. dev_dbg(&spi->dev,
  353. "using speed %d instead of %d\n",
  354. real_speed, xfer->speed_hz);
  355. }
  356. }
  357. msg->status = -EINPROGRESS;
  358. msg->actual_length = 0;
  359. spin_lock_irqsave(&mcfqspi->lock, flags);
  360. list_add_tail(&msg->queue, &mcfqspi->msgq);
  361. queue_work(mcfqspi->workq, &mcfqspi->work);
  362. spin_unlock_irqrestore(&mcfqspi->lock, flags);
  363. return 0;
  364. fail:
  365. msg->status = -EINVAL;
  366. return -EINVAL;
  367. }
  368. static int mcfqspi_setup(struct spi_device *spi)
  369. {
  370. if ((spi->bits_per_word < 8) || (spi->bits_per_word > 16)) {
  371. dev_dbg(&spi->dev, "%d bits per word is not supported\n",
  372. spi->bits_per_word);
  373. return -EINVAL;
  374. }
  375. if (spi->chip_select >= spi->master->num_chipselect) {
  376. dev_dbg(&spi->dev, "%d chip select is out of range\n",
  377. spi->chip_select);
  378. return -EINVAL;
  379. }
  380. mcfqspi_cs_deselect(spi_master_get_devdata(spi->master),
  381. spi->chip_select, spi->mode & SPI_CS_HIGH);
  382. dev_dbg(&spi->dev,
  383. "bits per word %d, chip select %d, speed %d KHz\n",
  384. spi->bits_per_word, spi->chip_select,
  385. (MCFQSPI_BUSCLK / mcfqspi_qmr_baud(spi->max_speed_hz))
  386. / 1000);
  387. return 0;
  388. }
  389. static int __devinit mcfqspi_probe(struct platform_device *pdev)
  390. {
  391. struct spi_master *master;
  392. struct mcfqspi *mcfqspi;
  393. struct resource *res;
  394. struct mcfqspi_platform_data *pdata;
  395. int status;
  396. master = spi_alloc_master(&pdev->dev, sizeof(*mcfqspi));
  397. if (master == NULL) {
  398. dev_dbg(&pdev->dev, "spi_alloc_master failed\n");
  399. return -ENOMEM;
  400. }
  401. mcfqspi = spi_master_get_devdata(master);
  402. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  403. if (!res) {
  404. dev_dbg(&pdev->dev, "platform_get_resource failed\n");
  405. status = -ENXIO;
  406. goto fail0;
  407. }
  408. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  409. dev_dbg(&pdev->dev, "request_mem_region failed\n");
  410. status = -EBUSY;
  411. goto fail0;
  412. }
  413. mcfqspi->iobase = ioremap(res->start, resource_size(res));
  414. if (!mcfqspi->iobase) {
  415. dev_dbg(&pdev->dev, "ioremap failed\n");
  416. status = -ENOMEM;
  417. goto fail1;
  418. }
  419. mcfqspi->irq = platform_get_irq(pdev, 0);
  420. if (mcfqspi->irq < 0) {
  421. dev_dbg(&pdev->dev, "platform_get_irq failed\n");
  422. status = -ENXIO;
  423. goto fail2;
  424. }
  425. status = request_irq(mcfqspi->irq, mcfqspi_irq_handler, IRQF_DISABLED,
  426. pdev->name, mcfqspi);
  427. if (status) {
  428. dev_dbg(&pdev->dev, "request_irq failed\n");
  429. goto fail2;
  430. }
  431. mcfqspi->clk = clk_get(&pdev->dev, "qspi_clk");
  432. if (IS_ERR(mcfqspi->clk)) {
  433. dev_dbg(&pdev->dev, "clk_get failed\n");
  434. status = PTR_ERR(mcfqspi->clk);
  435. goto fail3;
  436. }
  437. clk_enable(mcfqspi->clk);
  438. mcfqspi->workq = create_singlethread_workqueue(dev_name(master->dev.parent));
  439. if (!mcfqspi->workq) {
  440. dev_dbg(&pdev->dev, "create_workqueue failed\n");
  441. status = -ENOMEM;
  442. goto fail4;
  443. }
  444. INIT_WORK(&mcfqspi->work, mcfqspi_work);
  445. spin_lock_init(&mcfqspi->lock);
  446. INIT_LIST_HEAD(&mcfqspi->msgq);
  447. init_waitqueue_head(&mcfqspi->waitq);
  448. pdata = pdev->dev.platform_data;
  449. if (!pdata) {
  450. dev_dbg(&pdev->dev, "platform data is missing\n");
  451. goto fail5;
  452. }
  453. master->bus_num = pdata->bus_num;
  454. master->num_chipselect = pdata->num_chipselect;
  455. mcfqspi->cs_control = pdata->cs_control;
  456. status = mcfqspi_cs_setup(mcfqspi);
  457. if (status) {
  458. dev_dbg(&pdev->dev, "error initializing cs_control\n");
  459. goto fail5;
  460. }
  461. master->mode_bits = SPI_CS_HIGH | SPI_CPOL | SPI_CPHA;
  462. master->setup = mcfqspi_setup;
  463. master->transfer = mcfqspi_transfer;
  464. platform_set_drvdata(pdev, master);
  465. status = spi_register_master(master);
  466. if (status) {
  467. dev_dbg(&pdev->dev, "spi_register_master failed\n");
  468. goto fail6;
  469. }
  470. dev_info(&pdev->dev, "Coldfire QSPI bus driver\n");
  471. return 0;
  472. fail6:
  473. mcfqspi_cs_teardown(mcfqspi);
  474. fail5:
  475. destroy_workqueue(mcfqspi->workq);
  476. fail4:
  477. clk_disable(mcfqspi->clk);
  478. clk_put(mcfqspi->clk);
  479. fail3:
  480. free_irq(mcfqspi->irq, mcfqspi);
  481. fail2:
  482. iounmap(mcfqspi->iobase);
  483. fail1:
  484. release_mem_region(res->start, resource_size(res));
  485. fail0:
  486. spi_master_put(master);
  487. dev_dbg(&pdev->dev, "Coldfire QSPI probe failed\n");
  488. return status;
  489. }
  490. static int __devexit mcfqspi_remove(struct platform_device *pdev)
  491. {
  492. struct spi_master *master = platform_get_drvdata(pdev);
  493. struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
  494. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  495. /* disable the hardware (set the baud rate to 0) */
  496. mcfqspi_wr_qmr(mcfqspi, MCFQSPI_QMR_MSTR);
  497. platform_set_drvdata(pdev, NULL);
  498. mcfqspi_cs_teardown(mcfqspi);
  499. destroy_workqueue(mcfqspi->workq);
  500. clk_disable(mcfqspi->clk);
  501. clk_put(mcfqspi->clk);
  502. free_irq(mcfqspi->irq, mcfqspi);
  503. iounmap(mcfqspi->iobase);
  504. release_mem_region(res->start, resource_size(res));
  505. spi_unregister_master(master);
  506. spi_master_put(master);
  507. return 0;
  508. }
  509. #ifdef CONFIG_PM
  510. static int mcfqspi_suspend(struct device *dev)
  511. {
  512. struct mcfqspi *mcfqspi = platform_get_drvdata(to_platform_device(dev));
  513. clk_disable(mcfqspi->clk);
  514. return 0;
  515. }
  516. static int mcfqspi_resume(struct device *dev)
  517. {
  518. struct mcfqspi *mcfqspi = platform_get_drvdata(to_platform_device(dev));
  519. clk_enable(mcfqspi->clk);
  520. return 0;
  521. }
  522. static struct dev_pm_ops mcfqspi_dev_pm_ops = {
  523. .suspend = mcfqspi_suspend,
  524. .resume = mcfqspi_resume,
  525. };
  526. #define MCFQSPI_DEV_PM_OPS (&mcfqspi_dev_pm_ops)
  527. #else
  528. #define MCFQSPI_DEV_PM_OPS NULL
  529. #endif
  530. static struct platform_driver mcfqspi_driver = {
  531. .driver.name = DRIVER_NAME,
  532. .driver.owner = THIS_MODULE,
  533. .driver.pm = MCFQSPI_DEV_PM_OPS,
  534. .remove = __devexit_p(mcfqspi_remove),
  535. };
  536. static int __init mcfqspi_init(void)
  537. {
  538. return platform_driver_probe(&mcfqspi_driver, mcfqspi_probe);
  539. }
  540. module_init(mcfqspi_init);
  541. static void __exit mcfqspi_exit(void)
  542. {
  543. platform_driver_unregister(&mcfqspi_driver);
  544. }
  545. module_exit(mcfqspi_exit);
  546. MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
  547. MODULE_DESCRIPTION("Coldfire QSPI Controller Driver");
  548. MODULE_LICENSE("GPL");
  549. MODULE_ALIAS("platform:" DRIVER_NAME);