mpc52xx_spi.c 15 KB

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