mpc52xx_spi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * MPC52xx SPI bus driver.
  3. *
  4. * Copyright (C) 2008 Secret Lab Technologies Ltd.
  5. *
  6. * This file is released under the GPLv2
  7. *
  8. * This is the driver for the MPC5200's dedicated SPI controller.
  9. *
  10. * Note: this driver does not support the MPC5200 PSC in SPI mode. For
  11. * that driver see drivers/spi/mpc52xx_psc_spi.c
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/delay.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/spi/mpc52xx_spi.h>
  21. #include <linux/of_spi.h>
  22. #include <linux/io.h>
  23. #include <asm/time.h>
  24. #include <asm/mpc52xx.h>
  25. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  26. MODULE_DESCRIPTION("MPC52xx SPI (non-PSC) Driver");
  27. MODULE_LICENSE("GPL");
  28. /* Register offsets */
  29. #define SPI_CTRL1 0x00
  30. #define SPI_CTRL1_SPIE (1 << 7)
  31. #define SPI_CTRL1_SPE (1 << 6)
  32. #define SPI_CTRL1_MSTR (1 << 4)
  33. #define SPI_CTRL1_CPOL (1 << 3)
  34. #define SPI_CTRL1_CPHA (1 << 2)
  35. #define SPI_CTRL1_SSOE (1 << 1)
  36. #define SPI_CTRL1_LSBFE (1 << 0)
  37. #define SPI_CTRL2 0x01
  38. #define SPI_BRR 0x04
  39. #define SPI_STATUS 0x05
  40. #define SPI_STATUS_SPIF (1 << 7)
  41. #define SPI_STATUS_WCOL (1 << 6)
  42. #define SPI_STATUS_MODF (1 << 4)
  43. #define SPI_DATA 0x09
  44. #define SPI_PORTDATA 0x0d
  45. #define SPI_DATADIR 0x10
  46. /* FSM state return values */
  47. #define FSM_STOP 0 /* Nothing more for the state machine to */
  48. /* do. If something interesting happens */
  49. /* then and IRQ will be received */
  50. #define FSM_POLL 1 /* need to poll for completion, an IRQ is */
  51. /* not expected */
  52. #define FSM_CONTINUE 2 /* Keep iterating the state machine */
  53. /* Driver internal data */
  54. struct mpc52xx_spi {
  55. struct spi_master *master;
  56. u32 sysclk;
  57. void __iomem *regs;
  58. int irq0; /* MODF irq */
  59. int irq1; /* SPIF irq */
  60. int ipb_freq;
  61. /* Statistics */
  62. int msg_count;
  63. int wcol_count;
  64. int wcol_ticks;
  65. u32 wcol_tx_timestamp;
  66. int modf_count;
  67. int byte_count;
  68. struct list_head queue; /* queue of pending messages */
  69. spinlock_t lock;
  70. struct work_struct work;
  71. /* Details of current transfer (length, and buffer pointers) */
  72. struct spi_message *message; /* current message */
  73. struct spi_transfer *transfer; /* current transfer */
  74. int (*state)(int irq, struct mpc52xx_spi *ms, u8 status, u8 data);
  75. int len;
  76. int timestamp;
  77. u8 *rx_buf;
  78. const u8 *tx_buf;
  79. int cs_change;
  80. };
  81. /*
  82. * CS control function
  83. */
  84. static void mpc52xx_spi_chipsel(struct mpc52xx_spi *ms, int value)
  85. {
  86. out_8(ms->regs + SPI_PORTDATA, value ? 0 : 0x08);
  87. }
  88. /*
  89. * Start a new transfer. This is called both by the idle state
  90. * for the first transfer in a message, and by the wait state when the
  91. * previous transfer in a message is complete.
  92. */
  93. static void mpc52xx_spi_start_transfer(struct mpc52xx_spi *ms)
  94. {
  95. ms->rx_buf = ms->transfer->rx_buf;
  96. ms->tx_buf = ms->transfer->tx_buf;
  97. ms->len = ms->transfer->len;
  98. /* Activate the chip select */
  99. if (ms->cs_change)
  100. mpc52xx_spi_chipsel(ms, 1);
  101. ms->cs_change = ms->transfer->cs_change;
  102. /* Write out the first byte */
  103. ms->wcol_tx_timestamp = get_tbl();
  104. if (ms->tx_buf)
  105. out_8(ms->regs + SPI_DATA, *ms->tx_buf++);
  106. else
  107. out_8(ms->regs + SPI_DATA, 0);
  108. }
  109. /* Forward declaration of state handlers */
  110. static int mpc52xx_spi_fsmstate_transfer(int irq, struct mpc52xx_spi *ms,
  111. u8 status, u8 data);
  112. static int mpc52xx_spi_fsmstate_wait(int irq, struct mpc52xx_spi *ms,
  113. u8 status, u8 data);
  114. /*
  115. * IDLE state
  116. *
  117. * No transfers are in progress; if another transfer is pending then retrieve
  118. * it and kick it off. Otherwise, stop processing the state machine
  119. */
  120. static int
  121. mpc52xx_spi_fsmstate_idle(int irq, struct mpc52xx_spi *ms, u8 status, u8 data)
  122. {
  123. struct spi_device *spi;
  124. int spr, sppr;
  125. u8 ctrl1;
  126. if (status && (irq != NO_IRQ))
  127. dev_err(&ms->master->dev, "spurious irq, status=0x%.2x\n",
  128. status);
  129. /* Check if there is another transfer waiting. */
  130. if (list_empty(&ms->queue))
  131. return FSM_STOP;
  132. /* get the head of the queue */
  133. ms->message = list_first_entry(&ms->queue, struct spi_message, queue);
  134. list_del_init(&ms->message->queue);
  135. /* Setup the controller parameters */
  136. ctrl1 = SPI_CTRL1_SPIE | SPI_CTRL1_SPE | SPI_CTRL1_MSTR;
  137. spi = ms->message->spi;
  138. if (spi->mode & SPI_CPHA)
  139. ctrl1 |= SPI_CTRL1_CPHA;
  140. if (spi->mode & SPI_CPOL)
  141. ctrl1 |= SPI_CTRL1_CPOL;
  142. if (spi->mode & SPI_LSB_FIRST)
  143. ctrl1 |= SPI_CTRL1_LSBFE;
  144. out_8(ms->regs + SPI_CTRL1, ctrl1);
  145. /* Setup the controller speed */
  146. /* minimum divider is '2'. Also, add '1' to force rounding the
  147. * divider up. */
  148. sppr = ((ms->ipb_freq / ms->message->spi->max_speed_hz) + 1) >> 1;
  149. spr = 0;
  150. if (sppr < 1)
  151. sppr = 1;
  152. while (((sppr - 1) & ~0x7) != 0) {
  153. sppr = (sppr + 1) >> 1; /* add '1' to force rounding up */
  154. spr++;
  155. }
  156. sppr--; /* sppr quantity in register is offset by 1 */
  157. if (spr > 7) {
  158. /* Don't overrun limits of SPI baudrate register */
  159. spr = 7;
  160. sppr = 7;
  161. }
  162. out_8(ms->regs + SPI_BRR, sppr << 4 | spr); /* Set speed */
  163. ms->cs_change = 1;
  164. ms->transfer = container_of(ms->message->transfers.next,
  165. struct spi_transfer, transfer_list);
  166. mpc52xx_spi_start_transfer(ms);
  167. ms->state = mpc52xx_spi_fsmstate_transfer;
  168. return FSM_CONTINUE;
  169. }
  170. /*
  171. * TRANSFER state
  172. *
  173. * In the middle of a transfer. If the SPI core has completed processing
  174. * a byte, then read out the received data and write out the next byte
  175. * (unless this transfer is finished; in which case go on to the wait
  176. * state)
  177. */
  178. static int mpc52xx_spi_fsmstate_transfer(int irq, struct mpc52xx_spi *ms,
  179. u8 status, u8 data)
  180. {
  181. if (!status)
  182. return ms->irq0 ? FSM_STOP : FSM_POLL;
  183. if (status & SPI_STATUS_WCOL) {
  184. /* The SPI controller is stoopid. At slower speeds, it may
  185. * raise the SPIF flag before the state machine is actually
  186. * finished, which causes a collision (internal to the state
  187. * machine only). The manual recommends inserting a delay
  188. * between receiving the interrupt and sending the next byte,
  189. * but it can also be worked around simply by retrying the
  190. * transfer which is what we do here. */
  191. ms->wcol_count++;
  192. ms->wcol_ticks += get_tbl() - ms->wcol_tx_timestamp;
  193. ms->wcol_tx_timestamp = get_tbl();
  194. data = 0;
  195. if (ms->tx_buf)
  196. data = *(ms->tx_buf-1);
  197. out_8(ms->regs + SPI_DATA, data); /* try again */
  198. return FSM_CONTINUE;
  199. } else if (status & SPI_STATUS_MODF) {
  200. ms->modf_count++;
  201. dev_err(&ms->master->dev, "mode fault\n");
  202. mpc52xx_spi_chipsel(ms, 0);
  203. ms->message->status = -EIO;
  204. ms->message->complete(ms->message->context);
  205. ms->state = mpc52xx_spi_fsmstate_idle;
  206. return FSM_CONTINUE;
  207. }
  208. /* Read data out of the spi device */
  209. ms->byte_count++;
  210. if (ms->rx_buf)
  211. *ms->rx_buf++ = data;
  212. /* Is the transfer complete? */
  213. ms->len--;
  214. if (ms->len == 0) {
  215. ms->timestamp = get_tbl();
  216. ms->timestamp += ms->transfer->delay_usecs * tb_ticks_per_usec;
  217. ms->state = mpc52xx_spi_fsmstate_wait;
  218. return FSM_CONTINUE;
  219. }
  220. /* Write out the next byte */
  221. ms->wcol_tx_timestamp = get_tbl();
  222. if (ms->tx_buf)
  223. out_8(ms->regs + SPI_DATA, *ms->tx_buf++);
  224. else
  225. out_8(ms->regs + SPI_DATA, 0);
  226. return FSM_CONTINUE;
  227. }
  228. /*
  229. * WAIT state
  230. *
  231. * A transfer has completed; need to wait for the delay period to complete
  232. * before starting the next transfer
  233. */
  234. static int
  235. mpc52xx_spi_fsmstate_wait(int irq, struct mpc52xx_spi *ms, u8 status, u8 data)
  236. {
  237. if (status && irq)
  238. dev_err(&ms->master->dev, "spurious irq, status=0x%.2x\n",
  239. status);
  240. if (((int)get_tbl()) - ms->timestamp < 0)
  241. return FSM_POLL;
  242. ms->message->actual_length += ms->transfer->len;
  243. /* Check if there is another transfer in this message. If there
  244. * aren't then deactivate CS, notify sender, and drop back to idle
  245. * to start the next message. */
  246. if (ms->transfer->transfer_list.next == &ms->message->transfers) {
  247. ms->msg_count++;
  248. mpc52xx_spi_chipsel(ms, 0);
  249. ms->message->status = 0;
  250. ms->message->complete(ms->message->context);
  251. ms->state = mpc52xx_spi_fsmstate_idle;
  252. return FSM_CONTINUE;
  253. }
  254. /* There is another transfer; kick it off */
  255. if (ms->cs_change)
  256. mpc52xx_spi_chipsel(ms, 0);
  257. ms->transfer = container_of(ms->transfer->transfer_list.next,
  258. struct spi_transfer, transfer_list);
  259. mpc52xx_spi_start_transfer(ms);
  260. ms->state = mpc52xx_spi_fsmstate_transfer;
  261. return FSM_CONTINUE;
  262. }
  263. /**
  264. * mpc52xx_spi_fsm_process - Finite State Machine iteration function
  265. * @irq: irq number that triggered the FSM or 0 for polling
  266. * @ms: pointer to mpc52xx_spi driver data
  267. */
  268. static void mpc52xx_spi_fsm_process(int irq, struct mpc52xx_spi *ms)
  269. {
  270. int rc = FSM_CONTINUE;
  271. u8 status, data;
  272. while (rc == FSM_CONTINUE) {
  273. /* Interrupt cleared by read of STATUS followed by
  274. * read of DATA registers */
  275. status = in_8(ms->regs + SPI_STATUS);
  276. data = in_8(ms->regs + SPI_DATA);
  277. rc = ms->state(irq, ms, status, data);
  278. }
  279. if (rc == FSM_POLL)
  280. schedule_work(&ms->work);
  281. }
  282. /**
  283. * mpc52xx_spi_irq - IRQ handler
  284. */
  285. static irqreturn_t mpc52xx_spi_irq(int irq, void *_ms)
  286. {
  287. struct mpc52xx_spi *ms = _ms;
  288. spin_lock(&ms->lock);
  289. mpc52xx_spi_fsm_process(irq, ms);
  290. spin_unlock(&ms->lock);
  291. return IRQ_HANDLED;
  292. }
  293. /**
  294. * mpc52xx_spi_wq - Workqueue function for polling the state machine
  295. */
  296. static void mpc52xx_spi_wq(struct work_struct *work)
  297. {
  298. struct mpc52xx_spi *ms = container_of(work, struct mpc52xx_spi, work);
  299. unsigned long flags;
  300. spin_lock_irqsave(&ms->lock, flags);
  301. mpc52xx_spi_fsm_process(0, ms);
  302. spin_unlock_irqrestore(&ms->lock, flags);
  303. }
  304. /*
  305. * spi_master ops
  306. */
  307. static int mpc52xx_spi_setup(struct spi_device *spi)
  308. {
  309. if (spi->bits_per_word % 8)
  310. return -EINVAL;
  311. if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST))
  312. return -EINVAL;
  313. if (spi->chip_select >= spi->master->num_chipselect)
  314. return -EINVAL;
  315. return 0;
  316. }
  317. static int mpc52xx_spi_transfer(struct spi_device *spi, struct spi_message *m)
  318. {
  319. struct mpc52xx_spi *ms = spi_master_get_devdata(spi->master);
  320. unsigned long flags;
  321. m->actual_length = 0;
  322. m->status = -EINPROGRESS;
  323. spin_lock_irqsave(&ms->lock, flags);
  324. list_add_tail(&m->queue, &ms->queue);
  325. spin_unlock_irqrestore(&ms->lock, flags);
  326. schedule_work(&ms->work);
  327. return 0;
  328. }
  329. /*
  330. * OF Platform Bus Binding
  331. */
  332. static int __devinit mpc52xx_spi_probe(struct of_device *op,
  333. const struct of_device_id *match)
  334. {
  335. struct spi_master *master;
  336. struct mpc52xx_spi *ms;
  337. void __iomem *regs;
  338. int rc;
  339. /* MMIO registers */
  340. dev_dbg(&op->dev, "probing mpc5200 SPI device\n");
  341. regs = of_iomap(op->node, 0);
  342. if (!regs)
  343. return -ENODEV;
  344. /* initialize the device */
  345. out_8(regs+SPI_CTRL1, SPI_CTRL1_SPIE | SPI_CTRL1_SPE | SPI_CTRL1_MSTR);
  346. out_8(regs + SPI_CTRL2, 0x0);
  347. out_8(regs + SPI_DATADIR, 0xe); /* Set output pins */
  348. out_8(regs + SPI_PORTDATA, 0x8); /* Deassert /SS signal */
  349. /* Clear the status register and re-read it to check for a MODF
  350. * failure. This driver cannot currently handle multiple masters
  351. * on the SPI bus. This fault will also occur if the SPI signals
  352. * are not connected to any pins (port_config setting) */
  353. in_8(regs + SPI_STATUS);
  354. in_8(regs + SPI_DATA);
  355. if (in_8(regs + SPI_STATUS) & SPI_STATUS_MODF) {
  356. dev_err(&op->dev, "mode fault; is port_config correct?\n");
  357. rc = -EIO;
  358. goto err_init;
  359. }
  360. dev_dbg(&op->dev, "allocating spi_master struct\n");
  361. master = spi_alloc_master(&op->dev, sizeof *ms);
  362. if (!master) {
  363. rc = -ENOMEM;
  364. goto err_alloc;
  365. }
  366. master->bus_num = -1;
  367. master->num_chipselect = 1;
  368. master->setup = mpc52xx_spi_setup;
  369. master->transfer = mpc52xx_spi_transfer;
  370. dev_set_drvdata(&op->dev, master);
  371. ms = spi_master_get_devdata(master);
  372. ms->master = master;
  373. ms->regs = regs;
  374. ms->irq0 = irq_of_parse_and_map(op->node, 0);
  375. ms->irq1 = irq_of_parse_and_map(op->node, 1);
  376. ms->state = mpc52xx_spi_fsmstate_idle;
  377. ms->ipb_freq = mpc5xxx_get_bus_frequency(op->node);
  378. spin_lock_init(&ms->lock);
  379. INIT_LIST_HEAD(&ms->queue);
  380. INIT_WORK(&ms->work, mpc52xx_spi_wq);
  381. /* Decide if interrupts can be used */
  382. if (ms->irq0 && ms->irq1) {
  383. rc = request_irq(ms->irq0, mpc52xx_spi_irq, IRQF_SAMPLE_RANDOM,
  384. "mpc5200-spi-modf", ms);
  385. rc |= request_irq(ms->irq1, mpc52xx_spi_irq, IRQF_SAMPLE_RANDOM,
  386. "mpc5200-spi-spiF", ms);
  387. if (rc) {
  388. free_irq(ms->irq0, ms);
  389. free_irq(ms->irq1, ms);
  390. ms->irq0 = ms->irq1 = 0;
  391. }
  392. } else {
  393. /* operate in polled mode */
  394. ms->irq0 = ms->irq1 = 0;
  395. }
  396. if (!ms->irq0)
  397. dev_info(&op->dev, "using polled mode\n");
  398. dev_dbg(&op->dev, "registering spi_master struct\n");
  399. rc = spi_register_master(master);
  400. if (rc)
  401. goto err_register;
  402. of_register_spi_devices(master, op->node);
  403. dev_info(&ms->master->dev, "registered MPC5200 SPI bus\n");
  404. return rc;
  405. err_register:
  406. dev_err(&ms->master->dev, "initialization failed\n");
  407. spi_master_put(master);
  408. err_alloc:
  409. err_init:
  410. iounmap(regs);
  411. return rc;
  412. }
  413. static int __devexit mpc52xx_spi_remove(struct of_device *op)
  414. {
  415. struct spi_master *master = dev_get_drvdata(&op->dev);
  416. struct mpc52xx_spi *ms = spi_master_get_devdata(master);
  417. free_irq(ms->irq0, ms);
  418. free_irq(ms->irq1, ms);
  419. spi_unregister_master(master);
  420. spi_master_put(master);
  421. iounmap(ms->regs);
  422. return 0;
  423. }
  424. static struct of_device_id mpc52xx_spi_match[] __devinitdata = {
  425. { .compatible = "fsl,mpc5200-spi", },
  426. {}
  427. };
  428. MODULE_DEVICE_TABLE(of, mpc52xx_spi_match);
  429. static struct of_platform_driver mpc52xx_spi_of_driver = {
  430. .owner = THIS_MODULE,
  431. .name = "mpc52xx-spi",
  432. .match_table = mpc52xx_spi_match,
  433. .probe = mpc52xx_spi_probe,
  434. .remove = __exit_p(mpc52xx_spi_remove),
  435. };
  436. static int __init mpc52xx_spi_init(void)
  437. {
  438. return of_register_platform_driver(&mpc52xx_spi_of_driver);
  439. }
  440. module_init(mpc52xx_spi_init);
  441. static void __exit mpc52xx_spi_exit(void)
  442. {
  443. of_unregister_platform_driver(&mpc52xx_spi_of_driver);
  444. }
  445. module_exit(mpc52xx_spi_exit);