spi-mpc52xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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/errno.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/delay.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/io.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/slab.h>
  22. #include <asm/time.h>
  23. #include <asm/mpc52xx.h>
  24. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  25. MODULE_DESCRIPTION("MPC52xx SPI (non-PSC) Driver");
  26. MODULE_LICENSE("GPL");
  27. /* Register offsets */
  28. #define SPI_CTRL1 0x00
  29. #define SPI_CTRL1_SPIE (1 << 7)
  30. #define SPI_CTRL1_SPE (1 << 6)
  31. #define SPI_CTRL1_MSTR (1 << 4)
  32. #define SPI_CTRL1_CPOL (1 << 3)
  33. #define SPI_CTRL1_CPHA (1 << 2)
  34. #define SPI_CTRL1_SSOE (1 << 1)
  35. #define SPI_CTRL1_LSBFE (1 << 0)
  36. #define SPI_CTRL2 0x01
  37. #define SPI_BRR 0x04
  38. #define SPI_STATUS 0x05
  39. #define SPI_STATUS_SPIF (1 << 7)
  40. #define SPI_STATUS_WCOL (1 << 6)
  41. #define SPI_STATUS_MODF (1 << 4)
  42. #define SPI_DATA 0x09
  43. #define SPI_PORTDATA 0x0d
  44. #define SPI_DATADIR 0x10
  45. /* FSM state return values */
  46. #define FSM_STOP 0 /* Nothing more for the state machine to */
  47. /* do. If something interesting happens */
  48. /* then an IRQ will be received */
  49. #define FSM_POLL 1 /* need to poll for completion, an IRQ is */
  50. /* not expected */
  51. #define FSM_CONTINUE 2 /* Keep iterating the state machine */
  52. /* Driver internal data */
  53. struct mpc52xx_spi {
  54. struct spi_master *master;
  55. void __iomem *regs;
  56. int irq0; /* MODF irq */
  57. int irq1; /* SPIF irq */
  58. unsigned int ipb_freq;
  59. /* Statistics; not used now, but will be reintroduced for debugfs */
  60. int msg_count;
  61. int wcol_count;
  62. int wcol_ticks;
  63. u32 wcol_tx_timestamp;
  64. int modf_count;
  65. int byte_count;
  66. struct list_head queue; /* queue of pending messages */
  67. spinlock_t lock;
  68. struct work_struct work;
  69. /* Details of current transfer (length, and buffer pointers) */
  70. struct spi_message *message; /* current message */
  71. struct spi_transfer *transfer; /* current transfer */
  72. int (*state)(int irq, struct mpc52xx_spi *ms, u8 status, u8 data);
  73. int len;
  74. int timestamp;
  75. u8 *rx_buf;
  76. const u8 *tx_buf;
  77. int cs_change;
  78. int gpio_cs_count;
  79. unsigned int *gpio_cs;
  80. };
  81. /*
  82. * CS control function
  83. */
  84. static void mpc52xx_spi_chipsel(struct mpc52xx_spi *ms, int value)
  85. {
  86. int cs;
  87. if (ms->gpio_cs_count > 0) {
  88. cs = ms->message->spi->chip_select;
  89. gpio_set_value(ms->gpio_cs[cs], value ? 0 : 1);
  90. } else
  91. out_8(ms->regs + SPI_PORTDATA, value ? 0 : 0x08);
  92. }
  93. /*
  94. * Start a new transfer. This is called both by the idle state
  95. * for the first transfer in a message, and by the wait state when the
  96. * previous transfer in a message is complete.
  97. */
  98. static void mpc52xx_spi_start_transfer(struct mpc52xx_spi *ms)
  99. {
  100. ms->rx_buf = ms->transfer->rx_buf;
  101. ms->tx_buf = ms->transfer->tx_buf;
  102. ms->len = ms->transfer->len;
  103. /* Activate the chip select */
  104. if (ms->cs_change)
  105. mpc52xx_spi_chipsel(ms, 1);
  106. ms->cs_change = ms->transfer->cs_change;
  107. /* Write out the first byte */
  108. ms->wcol_tx_timestamp = get_tbl();
  109. if (ms->tx_buf)
  110. out_8(ms->regs + SPI_DATA, *ms->tx_buf++);
  111. else
  112. out_8(ms->regs + SPI_DATA, 0);
  113. }
  114. /* Forward declaration of state handlers */
  115. static int mpc52xx_spi_fsmstate_transfer(int irq, struct mpc52xx_spi *ms,
  116. u8 status, u8 data);
  117. static int mpc52xx_spi_fsmstate_wait(int irq, struct mpc52xx_spi *ms,
  118. u8 status, u8 data);
  119. /*
  120. * IDLE state
  121. *
  122. * No transfers are in progress; if another transfer is pending then retrieve
  123. * it and kick it off. Otherwise, stop processing the state machine
  124. */
  125. static int
  126. mpc52xx_spi_fsmstate_idle(int irq, struct mpc52xx_spi *ms, u8 status, u8 data)
  127. {
  128. struct spi_device *spi;
  129. int spr, sppr;
  130. u8 ctrl1;
  131. if (status && (irq != NO_IRQ))
  132. dev_err(&ms->master->dev, "spurious irq, status=0x%.2x\n",
  133. status);
  134. /* Check if there is another transfer waiting. */
  135. if (list_empty(&ms->queue))
  136. return FSM_STOP;
  137. /* get the head of the queue */
  138. ms->message = list_first_entry(&ms->queue, struct spi_message, queue);
  139. list_del_init(&ms->message->queue);
  140. /* Setup the controller parameters */
  141. ctrl1 = SPI_CTRL1_SPIE | SPI_CTRL1_SPE | SPI_CTRL1_MSTR;
  142. spi = ms->message->spi;
  143. if (spi->mode & SPI_CPHA)
  144. ctrl1 |= SPI_CTRL1_CPHA;
  145. if (spi->mode & SPI_CPOL)
  146. ctrl1 |= SPI_CTRL1_CPOL;
  147. if (spi->mode & SPI_LSB_FIRST)
  148. ctrl1 |= SPI_CTRL1_LSBFE;
  149. out_8(ms->regs + SPI_CTRL1, ctrl1);
  150. /* Setup the controller speed */
  151. /* minimum divider is '2'. Also, add '1' to force rounding the
  152. * divider up. */
  153. sppr = ((ms->ipb_freq / ms->message->spi->max_speed_hz) + 1) >> 1;
  154. spr = 0;
  155. if (sppr < 1)
  156. sppr = 1;
  157. while (((sppr - 1) & ~0x7) != 0) {
  158. sppr = (sppr + 1) >> 1; /* add '1' to force rounding up */
  159. spr++;
  160. }
  161. sppr--; /* sppr quantity in register is offset by 1 */
  162. if (spr > 7) {
  163. /* Don't overrun limits of SPI baudrate register */
  164. spr = 7;
  165. sppr = 7;
  166. }
  167. out_8(ms->regs + SPI_BRR, sppr << 4 | spr); /* Set speed */
  168. ms->cs_change = 1;
  169. ms->transfer = container_of(ms->message->transfers.next,
  170. struct spi_transfer, transfer_list);
  171. mpc52xx_spi_start_transfer(ms);
  172. ms->state = mpc52xx_spi_fsmstate_transfer;
  173. return FSM_CONTINUE;
  174. }
  175. /*
  176. * TRANSFER state
  177. *
  178. * In the middle of a transfer. If the SPI core has completed processing
  179. * a byte, then read out the received data and write out the next byte
  180. * (unless this transfer is finished; in which case go on to the wait
  181. * state)
  182. */
  183. static int mpc52xx_spi_fsmstate_transfer(int irq, struct mpc52xx_spi *ms,
  184. u8 status, u8 data)
  185. {
  186. if (!status)
  187. return ms->irq0 ? FSM_STOP : FSM_POLL;
  188. if (status & SPI_STATUS_WCOL) {
  189. /* The SPI controller is stoopid. At slower speeds, it may
  190. * raise the SPIF flag before the state machine is actually
  191. * finished, which causes a collision (internal to the state
  192. * machine only). The manual recommends inserting a delay
  193. * between receiving the interrupt and sending the next byte,
  194. * but it can also be worked around simply by retrying the
  195. * transfer which is what we do here. */
  196. ms->wcol_count++;
  197. ms->wcol_ticks += get_tbl() - ms->wcol_tx_timestamp;
  198. ms->wcol_tx_timestamp = get_tbl();
  199. data = 0;
  200. if (ms->tx_buf)
  201. data = *(ms->tx_buf - 1);
  202. out_8(ms->regs + SPI_DATA, data); /* try again */
  203. return FSM_CONTINUE;
  204. } else if (status & SPI_STATUS_MODF) {
  205. ms->modf_count++;
  206. dev_err(&ms->master->dev, "mode fault\n");
  207. mpc52xx_spi_chipsel(ms, 0);
  208. ms->message->status = -EIO;
  209. if (ms->message->complete)
  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. if (ms->message->complete)
  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_transfer(struct spi_device *spi, struct spi_message *m)
  315. {
  316. struct mpc52xx_spi *ms = spi_master_get_devdata(spi->master);
  317. unsigned long flags;
  318. m->actual_length = 0;
  319. m->status = -EINPROGRESS;
  320. spin_lock_irqsave(&ms->lock, flags);
  321. list_add_tail(&m->queue, &ms->queue);
  322. spin_unlock_irqrestore(&ms->lock, flags);
  323. schedule_work(&ms->work);
  324. return 0;
  325. }
  326. /*
  327. * OF Platform Bus Binding
  328. */
  329. static int mpc52xx_spi_probe(struct platform_device *op)
  330. {
  331. struct spi_master *master;
  332. struct mpc52xx_spi *ms;
  333. void __iomem *regs;
  334. u8 ctrl1;
  335. int rc, i = 0;
  336. int gpio_cs;
  337. /* MMIO registers */
  338. dev_dbg(&op->dev, "probing mpc5200 SPI device\n");
  339. regs = of_iomap(op->dev.of_node, 0);
  340. if (!regs)
  341. return -ENODEV;
  342. /* initialize the device */
  343. ctrl1 = SPI_CTRL1_SPIE | SPI_CTRL1_SPE | SPI_CTRL1_MSTR;
  344. out_8(regs + SPI_CTRL1, ctrl1);
  345. out_8(regs + SPI_CTRL2, 0x0);
  346. out_8(regs + SPI_DATADIR, 0xe); /* Set output pins */
  347. out_8(regs + SPI_PORTDATA, 0x8); /* Deassert /SS signal */
  348. /* Clear the status register and re-read it to check for a MODF
  349. * failure. This driver cannot currently handle multiple masters
  350. * on the SPI bus. This fault will also occur if the SPI signals
  351. * are not connected to any pins (port_config setting) */
  352. in_8(regs + SPI_STATUS);
  353. out_8(regs + SPI_CTRL1, ctrl1);
  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->transfer = mpc52xx_spi_transfer;
  367. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
  368. master->bits_per_word_mask = SPI_BPW_MASK(8);
  369. master->dev.of_node = op->dev.of_node;
  370. platform_set_drvdata(op, 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->dev.of_node, 0);
  375. ms->irq1 = irq_of_parse_and_map(op->dev.of_node, 1);
  376. ms->state = mpc52xx_spi_fsmstate_idle;
  377. ms->ipb_freq = mpc5xxx_get_bus_frequency(op->dev.of_node);
  378. ms->gpio_cs_count = of_gpio_count(op->dev.of_node);
  379. if (ms->gpio_cs_count > 0) {
  380. master->num_chipselect = ms->gpio_cs_count;
  381. ms->gpio_cs = kmalloc(ms->gpio_cs_count * sizeof(unsigned int),
  382. GFP_KERNEL);
  383. if (!ms->gpio_cs) {
  384. rc = -ENOMEM;
  385. goto err_alloc_gpio;
  386. }
  387. for (i = 0; i < ms->gpio_cs_count; i++) {
  388. gpio_cs = of_get_gpio(op->dev.of_node, i);
  389. if (gpio_cs < 0) {
  390. dev_err(&op->dev,
  391. "could not parse the gpio field "
  392. "in oftree\n");
  393. rc = -ENODEV;
  394. goto err_gpio;
  395. }
  396. rc = gpio_request(gpio_cs, dev_name(&op->dev));
  397. if (rc) {
  398. dev_err(&op->dev,
  399. "can't request spi cs gpio #%d "
  400. "on gpio line %d\n", i, gpio_cs);
  401. goto err_gpio;
  402. }
  403. gpio_direction_output(gpio_cs, 1);
  404. ms->gpio_cs[i] = gpio_cs;
  405. }
  406. }
  407. spin_lock_init(&ms->lock);
  408. INIT_LIST_HEAD(&ms->queue);
  409. INIT_WORK(&ms->work, mpc52xx_spi_wq);
  410. /* Decide if interrupts can be used */
  411. if (ms->irq0 && ms->irq1) {
  412. rc = request_irq(ms->irq0, mpc52xx_spi_irq, 0,
  413. "mpc5200-spi-modf", ms);
  414. rc |= request_irq(ms->irq1, mpc52xx_spi_irq, 0,
  415. "mpc5200-spi-spif", ms);
  416. if (rc) {
  417. free_irq(ms->irq0, ms);
  418. free_irq(ms->irq1, ms);
  419. ms->irq0 = ms->irq1 = 0;
  420. }
  421. } else {
  422. /* operate in polled mode */
  423. ms->irq0 = ms->irq1 = 0;
  424. }
  425. if (!ms->irq0)
  426. dev_info(&op->dev, "using polled mode\n");
  427. dev_dbg(&op->dev, "registering spi_master struct\n");
  428. rc = spi_register_master(master);
  429. if (rc)
  430. goto err_register;
  431. dev_info(&ms->master->dev, "registered MPC5200 SPI bus\n");
  432. return rc;
  433. err_register:
  434. dev_err(&ms->master->dev, "initialization failed\n");
  435. err_gpio:
  436. while (i-- > 0)
  437. gpio_free(ms->gpio_cs[i]);
  438. kfree(ms->gpio_cs);
  439. err_alloc_gpio:
  440. spi_master_put(master);
  441. err_alloc:
  442. err_init:
  443. iounmap(regs);
  444. return rc;
  445. }
  446. static int mpc52xx_spi_remove(struct platform_device *op)
  447. {
  448. struct spi_master *master = spi_master_get(platform_get_drvdata(op));
  449. struct mpc52xx_spi *ms = spi_master_get_devdata(master);
  450. int i;
  451. free_irq(ms->irq0, ms);
  452. free_irq(ms->irq1, ms);
  453. for (i = 0; i < ms->gpio_cs_count; i++)
  454. gpio_free(ms->gpio_cs[i]);
  455. kfree(ms->gpio_cs);
  456. spi_unregister_master(master);
  457. iounmap(ms->regs);
  458. spi_master_put(master);
  459. return 0;
  460. }
  461. static const struct of_device_id mpc52xx_spi_match[] = {
  462. { .compatible = "fsl,mpc5200-spi", },
  463. {}
  464. };
  465. MODULE_DEVICE_TABLE(of, mpc52xx_spi_match);
  466. static struct platform_driver mpc52xx_spi_of_driver = {
  467. .driver = {
  468. .name = "mpc52xx-spi",
  469. .of_match_table = mpc52xx_spi_match,
  470. },
  471. .probe = mpc52xx_spi_probe,
  472. .remove = mpc52xx_spi_remove,
  473. };
  474. module_platform_driver(mpc52xx_spi_of_driver);