mpc52xx_spi.c 15 KB

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