mpc52xx_spi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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/io.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/slab.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 an 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. void __iomem *regs;
  57. int irq0; /* MODF irq */
  58. int irq1; /* SPIF irq */
  59. unsigned int ipb_freq;
  60. /* Statistics; not used now, but will be reintroduced for debugfs */
  61. int msg_count;
  62. int wcol_count;
  63. int wcol_ticks;
  64. u32 wcol_tx_timestamp;
  65. int modf_count;
  66. int byte_count;
  67. struct list_head queue; /* queue of pending messages */
  68. spinlock_t lock;
  69. struct work_struct work;
  70. /* Details of current transfer (length, and buffer pointers) */
  71. struct spi_message *message; /* current message */
  72. struct spi_transfer *transfer; /* current transfer */
  73. int (*state)(int irq, struct mpc52xx_spi *ms, u8 status, u8 data);
  74. int len;
  75. int timestamp;
  76. u8 *rx_buf;
  77. const u8 *tx_buf;
  78. int cs_change;
  79. int gpio_cs_count;
  80. unsigned int *gpio_cs;
  81. };
  82. /*
  83. * CS control function
  84. */
  85. static void mpc52xx_spi_chipsel(struct mpc52xx_spi *ms, int value)
  86. {
  87. int cs;
  88. if (ms->gpio_cs_count > 0) {
  89. cs = ms->message->spi->chip_select;
  90. gpio_set_value(ms->gpio_cs[cs], value ? 0 : 1);
  91. } else
  92. out_8(ms->regs + SPI_PORTDATA, value ? 0 : 0x08);
  93. }
  94. /*
  95. * Start a new transfer. This is called both by the idle state
  96. * for the first transfer in a message, and by the wait state when the
  97. * previous transfer in a message is complete.
  98. */
  99. static void mpc52xx_spi_start_transfer(struct mpc52xx_spi *ms)
  100. {
  101. ms->rx_buf = ms->transfer->rx_buf;
  102. ms->tx_buf = ms->transfer->tx_buf;
  103. ms->len = ms->transfer->len;
  104. /* Activate the chip select */
  105. if (ms->cs_change)
  106. mpc52xx_spi_chipsel(ms, 1);
  107. ms->cs_change = ms->transfer->cs_change;
  108. /* Write out the first byte */
  109. ms->wcol_tx_timestamp = get_tbl();
  110. if (ms->tx_buf)
  111. out_8(ms->regs + SPI_DATA, *ms->tx_buf++);
  112. else
  113. out_8(ms->regs + SPI_DATA, 0);
  114. }
  115. /* Forward declaration of state handlers */
  116. static int mpc52xx_spi_fsmstate_transfer(int irq, struct mpc52xx_spi *ms,
  117. u8 status, u8 data);
  118. static int mpc52xx_spi_fsmstate_wait(int irq, struct mpc52xx_spi *ms,
  119. u8 status, u8 data);
  120. /*
  121. * IDLE state
  122. *
  123. * No transfers are in progress; if another transfer is pending then retrieve
  124. * it and kick it off. Otherwise, stop processing the state machine
  125. */
  126. static int
  127. mpc52xx_spi_fsmstate_idle(int irq, struct mpc52xx_spi *ms, u8 status, u8 data)
  128. {
  129. struct spi_device *spi;
  130. int spr, sppr;
  131. u8 ctrl1;
  132. if (status && (irq != NO_IRQ))
  133. dev_err(&ms->master->dev, "spurious irq, status=0x%.2x\n",
  134. status);
  135. /* Check if there is another transfer waiting. */
  136. if (list_empty(&ms->queue))
  137. return FSM_STOP;
  138. /* get the head of the queue */
  139. ms->message = list_first_entry(&ms->queue, struct spi_message, queue);
  140. list_del_init(&ms->message->queue);
  141. /* Setup the controller parameters */
  142. ctrl1 = SPI_CTRL1_SPIE | SPI_CTRL1_SPE | SPI_CTRL1_MSTR;
  143. spi = ms->message->spi;
  144. if (spi->mode & SPI_CPHA)
  145. ctrl1 |= SPI_CTRL1_CPHA;
  146. if (spi->mode & SPI_CPOL)
  147. ctrl1 |= SPI_CTRL1_CPOL;
  148. if (spi->mode & SPI_LSB_FIRST)
  149. ctrl1 |= SPI_CTRL1_LSBFE;
  150. out_8(ms->regs + SPI_CTRL1, ctrl1);
  151. /* Setup the controller speed */
  152. /* minimum divider is '2'. Also, add '1' to force rounding the
  153. * divider up. */
  154. sppr = ((ms->ipb_freq / ms->message->spi->max_speed_hz) + 1) >> 1;
  155. spr = 0;
  156. if (sppr < 1)
  157. sppr = 1;
  158. while (((sppr - 1) & ~0x7) != 0) {
  159. sppr = (sppr + 1) >> 1; /* add '1' to force rounding up */
  160. spr++;
  161. }
  162. sppr--; /* sppr quantity in register is offset by 1 */
  163. if (spr > 7) {
  164. /* Don't overrun limits of SPI baudrate register */
  165. spr = 7;
  166. sppr = 7;
  167. }
  168. out_8(ms->regs + SPI_BRR, sppr << 4 | spr); /* Set speed */
  169. ms->cs_change = 1;
  170. ms->transfer = container_of(ms->message->transfers.next,
  171. struct spi_transfer, transfer_list);
  172. mpc52xx_spi_start_transfer(ms);
  173. ms->state = mpc52xx_spi_fsmstate_transfer;
  174. return FSM_CONTINUE;
  175. }
  176. /*
  177. * TRANSFER state
  178. *
  179. * In the middle of a transfer. If the SPI core has completed processing
  180. * a byte, then read out the received data and write out the next byte
  181. * (unless this transfer is finished; in which case go on to the wait
  182. * state)
  183. */
  184. static int mpc52xx_spi_fsmstate_transfer(int irq, struct mpc52xx_spi *ms,
  185. u8 status, u8 data)
  186. {
  187. if (!status)
  188. return ms->irq0 ? FSM_STOP : FSM_POLL;
  189. if (status & SPI_STATUS_WCOL) {
  190. /* The SPI controller is stoopid. At slower speeds, it may
  191. * raise the SPIF flag before the state machine is actually
  192. * finished, which causes a collision (internal to the state
  193. * machine only). The manual recommends inserting a delay
  194. * between receiving the interrupt and sending the next byte,
  195. * but it can also be worked around simply by retrying the
  196. * transfer which is what we do here. */
  197. ms->wcol_count++;
  198. ms->wcol_ticks += get_tbl() - ms->wcol_tx_timestamp;
  199. ms->wcol_tx_timestamp = get_tbl();
  200. data = 0;
  201. if (ms->tx_buf)
  202. data = *(ms->tx_buf - 1);
  203. out_8(ms->regs + SPI_DATA, data); /* try again */
  204. return FSM_CONTINUE;
  205. } else if (status & SPI_STATUS_MODF) {
  206. ms->modf_count++;
  207. dev_err(&ms->master->dev, "mode fault\n");
  208. mpc52xx_spi_chipsel(ms, 0);
  209. ms->message->status = -EIO;
  210. ms->message->complete(ms->message->context);
  211. ms->state = mpc52xx_spi_fsmstate_idle;
  212. return FSM_CONTINUE;
  213. }
  214. /* Read data out of the spi device */
  215. ms->byte_count++;
  216. if (ms->rx_buf)
  217. *ms->rx_buf++ = data;
  218. /* Is the transfer complete? */
  219. ms->len--;
  220. if (ms->len == 0) {
  221. ms->timestamp = get_tbl();
  222. ms->timestamp += ms->transfer->delay_usecs * tb_ticks_per_usec;
  223. ms->state = mpc52xx_spi_fsmstate_wait;
  224. return FSM_CONTINUE;
  225. }
  226. /* Write out the next byte */
  227. ms->wcol_tx_timestamp = get_tbl();
  228. if (ms->tx_buf)
  229. out_8(ms->regs + SPI_DATA, *ms->tx_buf++);
  230. else
  231. out_8(ms->regs + SPI_DATA, 0);
  232. return FSM_CONTINUE;
  233. }
  234. /*
  235. * WAIT state
  236. *
  237. * A transfer has completed; need to wait for the delay period to complete
  238. * before starting the next transfer
  239. */
  240. static int
  241. mpc52xx_spi_fsmstate_wait(int irq, struct mpc52xx_spi *ms, u8 status, u8 data)
  242. {
  243. if (status && irq)
  244. dev_err(&ms->master->dev, "spurious irq, status=0x%.2x\n",
  245. status);
  246. if (((int)get_tbl()) - ms->timestamp < 0)
  247. return FSM_POLL;
  248. ms->message->actual_length += ms->transfer->len;
  249. /* Check if there is another transfer in this message. If there
  250. * aren't then deactivate CS, notify sender, and drop back to idle
  251. * to start the next message. */
  252. if (ms->transfer->transfer_list.next == &ms->message->transfers) {
  253. ms->msg_count++;
  254. mpc52xx_spi_chipsel(ms, 0);
  255. ms->message->status = 0;
  256. ms->message->complete(ms->message->context);
  257. ms->state = mpc52xx_spi_fsmstate_idle;
  258. return FSM_CONTINUE;
  259. }
  260. /* There is another transfer; kick it off */
  261. if (ms->cs_change)
  262. mpc52xx_spi_chipsel(ms, 0);
  263. ms->transfer = container_of(ms->transfer->transfer_list.next,
  264. struct spi_transfer, transfer_list);
  265. mpc52xx_spi_start_transfer(ms);
  266. ms->state = mpc52xx_spi_fsmstate_transfer;
  267. return FSM_CONTINUE;
  268. }
  269. /**
  270. * mpc52xx_spi_fsm_process - Finite State Machine iteration function
  271. * @irq: irq number that triggered the FSM or 0 for polling
  272. * @ms: pointer to mpc52xx_spi driver data
  273. */
  274. static void mpc52xx_spi_fsm_process(int irq, struct mpc52xx_spi *ms)
  275. {
  276. int rc = FSM_CONTINUE;
  277. u8 status, data;
  278. while (rc == FSM_CONTINUE) {
  279. /* Interrupt cleared by read of STATUS followed by
  280. * read of DATA registers */
  281. status = in_8(ms->regs + SPI_STATUS);
  282. data = in_8(ms->regs + SPI_DATA);
  283. rc = ms->state(irq, ms, status, data);
  284. }
  285. if (rc == FSM_POLL)
  286. schedule_work(&ms->work);
  287. }
  288. /**
  289. * mpc52xx_spi_irq - IRQ handler
  290. */
  291. static irqreturn_t mpc52xx_spi_irq(int irq, void *_ms)
  292. {
  293. struct mpc52xx_spi *ms = _ms;
  294. spin_lock(&ms->lock);
  295. mpc52xx_spi_fsm_process(irq, ms);
  296. spin_unlock(&ms->lock);
  297. return IRQ_HANDLED;
  298. }
  299. /**
  300. * mpc52xx_spi_wq - Workqueue function for polling the state machine
  301. */
  302. static void mpc52xx_spi_wq(struct work_struct *work)
  303. {
  304. struct mpc52xx_spi *ms = container_of(work, struct mpc52xx_spi, work);
  305. unsigned long flags;
  306. spin_lock_irqsave(&ms->lock, flags);
  307. mpc52xx_spi_fsm_process(0, ms);
  308. spin_unlock_irqrestore(&ms->lock, flags);
  309. }
  310. /*
  311. * spi_master ops
  312. */
  313. static int mpc52xx_spi_setup(struct spi_device *spi)
  314. {
  315. if (spi->bits_per_word % 8)
  316. return -EINVAL;
  317. if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST))
  318. return -EINVAL;
  319. if (spi->chip_select >= spi->master->num_chipselect)
  320. return -EINVAL;
  321. return 0;
  322. }
  323. static int mpc52xx_spi_transfer(struct spi_device *spi, struct spi_message *m)
  324. {
  325. struct mpc52xx_spi *ms = spi_master_get_devdata(spi->master);
  326. unsigned long flags;
  327. m->actual_length = 0;
  328. m->status = -EINPROGRESS;
  329. spin_lock_irqsave(&ms->lock, flags);
  330. list_add_tail(&m->queue, &ms->queue);
  331. spin_unlock_irqrestore(&ms->lock, flags);
  332. schedule_work(&ms->work);
  333. return 0;
  334. }
  335. /*
  336. * OF Platform Bus Binding
  337. */
  338. static int __devinit mpc52xx_spi_probe(struct platform_device *op)
  339. {
  340. struct spi_master *master;
  341. struct mpc52xx_spi *ms;
  342. void __iomem *regs;
  343. u8 ctrl1;
  344. int rc, i = 0;
  345. int gpio_cs;
  346. /* MMIO registers */
  347. dev_dbg(&op->dev, "probing mpc5200 SPI device\n");
  348. regs = of_iomap(op->dev.of_node, 0);
  349. if (!regs)
  350. return -ENODEV;
  351. /* initialize the device */
  352. ctrl1 = SPI_CTRL1_SPIE | SPI_CTRL1_SPE | SPI_CTRL1_MSTR;
  353. out_8(regs + SPI_CTRL1, ctrl1);
  354. out_8(regs + SPI_CTRL2, 0x0);
  355. out_8(regs + SPI_DATADIR, 0xe); /* Set output pins */
  356. out_8(regs + SPI_PORTDATA, 0x8); /* Deassert /SS signal */
  357. /* Clear the status register and re-read it to check for a MODF
  358. * failure. This driver cannot currently handle multiple masters
  359. * on the SPI bus. This fault will also occur if the SPI signals
  360. * are not connected to any pins (port_config setting) */
  361. in_8(regs + SPI_STATUS);
  362. out_8(regs + SPI_CTRL1, ctrl1);
  363. in_8(regs + SPI_DATA);
  364. if (in_8(regs + SPI_STATUS) & SPI_STATUS_MODF) {
  365. dev_err(&op->dev, "mode fault; is port_config correct?\n");
  366. rc = -EIO;
  367. goto err_init;
  368. }
  369. dev_dbg(&op->dev, "allocating spi_master struct\n");
  370. master = spi_alloc_master(&op->dev, sizeof *ms);
  371. if (!master) {
  372. rc = -ENOMEM;
  373. goto err_alloc;
  374. }
  375. master->bus_num = -1;
  376. master->setup = mpc52xx_spi_setup;
  377. master->transfer = mpc52xx_spi_transfer;
  378. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
  379. master->dev.of_node = op->dev.of_node;
  380. dev_set_drvdata(&op->dev, master);
  381. ms = spi_master_get_devdata(master);
  382. ms->master = master;
  383. ms->regs = regs;
  384. ms->irq0 = irq_of_parse_and_map(op->dev.of_node, 0);
  385. ms->irq1 = irq_of_parse_and_map(op->dev.of_node, 1);
  386. ms->state = mpc52xx_spi_fsmstate_idle;
  387. ms->ipb_freq = mpc5xxx_get_bus_frequency(op->dev.of_node);
  388. ms->gpio_cs_count = of_gpio_count(op->dev.of_node);
  389. if (ms->gpio_cs_count > 0) {
  390. master->num_chipselect = ms->gpio_cs_count;
  391. ms->gpio_cs = kmalloc(ms->gpio_cs_count * sizeof(unsigned int),
  392. GFP_KERNEL);
  393. if (!ms->gpio_cs) {
  394. rc = -ENOMEM;
  395. goto err_alloc;
  396. }
  397. for (i = 0; i < ms->gpio_cs_count; i++) {
  398. gpio_cs = of_get_gpio(op->dev.of_node, i);
  399. if (gpio_cs < 0) {
  400. dev_err(&op->dev,
  401. "could not parse the gpio field "
  402. "in oftree\n");
  403. rc = -ENODEV;
  404. goto err_gpio;
  405. }
  406. rc = gpio_request(gpio_cs, dev_name(&op->dev));
  407. if (rc) {
  408. dev_err(&op->dev,
  409. "can't request spi cs gpio #%d "
  410. "on gpio line %d\n", i, gpio_cs);
  411. goto err_gpio;
  412. }
  413. gpio_direction_output(gpio_cs, 1);
  414. ms->gpio_cs[i] = gpio_cs;
  415. }
  416. } else {
  417. master->num_chipselect = 1;
  418. }
  419. spin_lock_init(&ms->lock);
  420. INIT_LIST_HEAD(&ms->queue);
  421. INIT_WORK(&ms->work, mpc52xx_spi_wq);
  422. /* Decide if interrupts can be used */
  423. if (ms->irq0 && ms->irq1) {
  424. rc = request_irq(ms->irq0, mpc52xx_spi_irq, 0,
  425. "mpc5200-spi-modf", ms);
  426. rc |= request_irq(ms->irq1, mpc52xx_spi_irq, 0,
  427. "mpc5200-spi-spif", ms);
  428. if (rc) {
  429. free_irq(ms->irq0, ms);
  430. free_irq(ms->irq1, ms);
  431. ms->irq0 = ms->irq1 = 0;
  432. }
  433. } else {
  434. /* operate in polled mode */
  435. ms->irq0 = ms->irq1 = 0;
  436. }
  437. if (!ms->irq0)
  438. dev_info(&op->dev, "using polled mode\n");
  439. dev_dbg(&op->dev, "registering spi_master struct\n");
  440. rc = spi_register_master(master);
  441. if (rc)
  442. goto err_register;
  443. dev_info(&ms->master->dev, "registered MPC5200 SPI bus\n");
  444. return rc;
  445. err_register:
  446. dev_err(&ms->master->dev, "initialization failed\n");
  447. spi_master_put(master);
  448. err_gpio:
  449. while (i-- > 0)
  450. gpio_free(ms->gpio_cs[i]);
  451. kfree(ms->gpio_cs);
  452. err_alloc:
  453. err_init:
  454. iounmap(regs);
  455. return rc;
  456. }
  457. static int __devexit mpc52xx_spi_remove(struct platform_device *op)
  458. {
  459. struct spi_master *master = dev_get_drvdata(&op->dev);
  460. struct mpc52xx_spi *ms = spi_master_get_devdata(master);
  461. int i;
  462. free_irq(ms->irq0, ms);
  463. free_irq(ms->irq1, ms);
  464. for (i = 0; i < ms->gpio_cs_count; i++)
  465. gpio_free(ms->gpio_cs[i]);
  466. kfree(ms->gpio_cs);
  467. spi_unregister_master(master);
  468. spi_master_put(master);
  469. iounmap(ms->regs);
  470. return 0;
  471. }
  472. static const struct of_device_id mpc52xx_spi_match[] __devinitconst = {
  473. { .compatible = "fsl,mpc5200-spi", },
  474. {}
  475. };
  476. MODULE_DEVICE_TABLE(of, mpc52xx_spi_match);
  477. static struct platform_driver mpc52xx_spi_of_driver = {
  478. .driver = {
  479. .name = "mpc52xx-spi",
  480. .owner = THIS_MODULE,
  481. .of_match_table = mpc52xx_spi_match,
  482. },
  483. .probe = mpc52xx_spi_probe,
  484. .remove = __devexit_p(mpc52xx_spi_remove),
  485. };
  486. static int __init mpc52xx_spi_init(void)
  487. {
  488. return platform_driver_register(&mpc52xx_spi_of_driver);
  489. }
  490. module_init(mpc52xx_spi_init);
  491. static void __exit mpc52xx_spi_exit(void)
  492. {
  493. platform_driver_unregister(&mpc52xx_spi_of_driver);
  494. }
  495. module_exit(mpc52xx_spi_exit);