spi-mpc52xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MPC52xx SPI bus driver.
  4. *
  5. * Copyright (C) 2008 Secret Lab Technologies Ltd.
  6. *
  7. * This is the driver for the MPC5200's dedicated SPI controller.
  8. *
  9. * Note: this driver does not support the MPC5200 PSC in SPI mode. For
  10. * that driver see drivers/spi/mpc52xx_psc_spi.c
  11. */
  12. #include <linux/module.h>
  13. #include <linux/err.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/gpio/consumer.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/io.h>
  21. #include <linux/slab.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #include <asm/time.h>
  25. #include <asm/mpc52xx.h>
  26. MODULE_AUTHOR("Grant Likely <[email protected]>");
  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. struct gpio_desc **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. gpiod_set_value(ms->gpio_cs[cs], value);
  92. } else {
  93. out_8(ms->regs + SPI_PORTDATA, value ? 0 : 0x08);
  94. }
  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 = mftb();
  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)
  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 += mftb() - ms->wcol_tx_timestamp;
  201. ms->wcol_tx_timestamp = mftb();
  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. if (ms->message->complete)
  213. ms->message->complete(ms->message->context);
  214. ms->state = mpc52xx_spi_fsmstate_idle;
  215. return FSM_CONTINUE;
  216. }
  217. /* Read data out of the spi device */
  218. ms->byte_count++;
  219. if (ms->rx_buf)
  220. *ms->rx_buf++ = data;
  221. /* Is the transfer complete? */
  222. ms->len--;
  223. if (ms->len == 0) {
  224. ms->timestamp = mftb();
  225. if (ms->transfer->delay.unit == SPI_DELAY_UNIT_USECS)
  226. ms->timestamp += ms->transfer->delay.value *
  227. tb_ticks_per_usec;
  228. ms->state = mpc52xx_spi_fsmstate_wait;
  229. return FSM_CONTINUE;
  230. }
  231. /* Write out the next byte */
  232. ms->wcol_tx_timestamp = mftb();
  233. if (ms->tx_buf)
  234. out_8(ms->regs + SPI_DATA, *ms->tx_buf++);
  235. else
  236. out_8(ms->regs + SPI_DATA, 0);
  237. return FSM_CONTINUE;
  238. }
  239. /*
  240. * WAIT state
  241. *
  242. * A transfer has completed; need to wait for the delay period to complete
  243. * before starting the next transfer
  244. */
  245. static int
  246. mpc52xx_spi_fsmstate_wait(int irq, struct mpc52xx_spi *ms, u8 status, u8 data)
  247. {
  248. if (status && irq)
  249. dev_err(&ms->master->dev, "spurious irq, status=0x%.2x\n",
  250. status);
  251. if (((int)mftb()) - ms->timestamp < 0)
  252. return FSM_POLL;
  253. ms->message->actual_length += ms->transfer->len;
  254. /* Check if there is another transfer in this message. If there
  255. * aren't then deactivate CS, notify sender, and drop back to idle
  256. * to start the next message. */
  257. if (ms->transfer->transfer_list.next == &ms->message->transfers) {
  258. ms->msg_count++;
  259. mpc52xx_spi_chipsel(ms, 0);
  260. ms->message->status = 0;
  261. if (ms->message->complete)
  262. ms->message->complete(ms->message->context);
  263. ms->state = mpc52xx_spi_fsmstate_idle;
  264. return FSM_CONTINUE;
  265. }
  266. /* There is another transfer; kick it off */
  267. if (ms->cs_change)
  268. mpc52xx_spi_chipsel(ms, 0);
  269. ms->transfer = container_of(ms->transfer->transfer_list.next,
  270. struct spi_transfer, transfer_list);
  271. mpc52xx_spi_start_transfer(ms);
  272. ms->state = mpc52xx_spi_fsmstate_transfer;
  273. return FSM_CONTINUE;
  274. }
  275. /**
  276. * mpc52xx_spi_fsm_process - Finite State Machine iteration function
  277. * @irq: irq number that triggered the FSM or 0 for polling
  278. * @ms: pointer to mpc52xx_spi driver data
  279. */
  280. static void mpc52xx_spi_fsm_process(int irq, struct mpc52xx_spi *ms)
  281. {
  282. int rc = FSM_CONTINUE;
  283. u8 status, data;
  284. while (rc == FSM_CONTINUE) {
  285. /* Interrupt cleared by read of STATUS followed by
  286. * read of DATA registers */
  287. status = in_8(ms->regs + SPI_STATUS);
  288. data = in_8(ms->regs + SPI_DATA);
  289. rc = ms->state(irq, ms, status, data);
  290. }
  291. if (rc == FSM_POLL)
  292. schedule_work(&ms->work);
  293. }
  294. /**
  295. * mpc52xx_spi_irq - IRQ handler
  296. */
  297. static irqreturn_t mpc52xx_spi_irq(int irq, void *_ms)
  298. {
  299. struct mpc52xx_spi *ms = _ms;
  300. spin_lock(&ms->lock);
  301. mpc52xx_spi_fsm_process(irq, ms);
  302. spin_unlock(&ms->lock);
  303. return IRQ_HANDLED;
  304. }
  305. /**
  306. * mpc52xx_spi_wq - Workqueue function for polling the state machine
  307. */
  308. static void mpc52xx_spi_wq(struct work_struct *work)
  309. {
  310. struct mpc52xx_spi *ms = container_of(work, struct mpc52xx_spi, work);
  311. unsigned long flags;
  312. spin_lock_irqsave(&ms->lock, flags);
  313. mpc52xx_spi_fsm_process(0, ms);
  314. spin_unlock_irqrestore(&ms->lock, flags);
  315. }
  316. /*
  317. * spi_master ops
  318. */
  319. static int mpc52xx_spi_transfer(struct spi_device *spi, struct spi_message *m)
  320. {
  321. struct mpc52xx_spi *ms = spi_master_get_devdata(spi->master);
  322. unsigned long flags;
  323. m->actual_length = 0;
  324. m->status = -EINPROGRESS;
  325. spin_lock_irqsave(&ms->lock, flags);
  326. list_add_tail(&m->queue, &ms->queue);
  327. spin_unlock_irqrestore(&ms->lock, flags);
  328. schedule_work(&ms->work);
  329. return 0;
  330. }
  331. /*
  332. * OF Platform Bus Binding
  333. */
  334. static int mpc52xx_spi_probe(struct platform_device *op)
  335. {
  336. struct spi_master *master;
  337. struct mpc52xx_spi *ms;
  338. struct gpio_desc *gpio_cs;
  339. void __iomem *regs;
  340. u8 ctrl1;
  341. int rc, i = 0;
  342. /* MMIO registers */
  343. dev_dbg(&op->dev, "probing mpc5200 SPI device\n");
  344. regs = of_iomap(op->dev.of_node, 0);
  345. if (!regs)
  346. return -ENODEV;
  347. /* initialize the device */
  348. ctrl1 = SPI_CTRL1_SPIE | SPI_CTRL1_SPE | SPI_CTRL1_MSTR;
  349. out_8(regs + SPI_CTRL1, ctrl1);
  350. out_8(regs + SPI_CTRL2, 0x0);
  351. out_8(regs + SPI_DATADIR, 0xe); /* Set output pins */
  352. out_8(regs + SPI_PORTDATA, 0x8); /* Deassert /SS signal */
  353. /* Clear the status register and re-read it to check for a MODF
  354. * failure. This driver cannot currently handle multiple masters
  355. * on the SPI bus. This fault will also occur if the SPI signals
  356. * are not connected to any pins (port_config setting) */
  357. in_8(regs + SPI_STATUS);
  358. out_8(regs + SPI_CTRL1, ctrl1);
  359. in_8(regs + SPI_DATA);
  360. if (in_8(regs + SPI_STATUS) & SPI_STATUS_MODF) {
  361. dev_err(&op->dev, "mode fault; is port_config correct?\n");
  362. rc = -EIO;
  363. goto err_init;
  364. }
  365. dev_dbg(&op->dev, "allocating spi_master struct\n");
  366. master = spi_alloc_master(&op->dev, sizeof(*ms));
  367. if (!master) {
  368. rc = -ENOMEM;
  369. goto err_alloc;
  370. }
  371. master->transfer = mpc52xx_spi_transfer;
  372. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
  373. master->bits_per_word_mask = SPI_BPW_MASK(8);
  374. master->dev.of_node = op->dev.of_node;
  375. platform_set_drvdata(op, master);
  376. ms = spi_master_get_devdata(master);
  377. ms->master = master;
  378. ms->regs = regs;
  379. ms->irq0 = irq_of_parse_and_map(op->dev.of_node, 0);
  380. ms->irq1 = irq_of_parse_and_map(op->dev.of_node, 1);
  381. ms->state = mpc52xx_spi_fsmstate_idle;
  382. ms->ipb_freq = mpc5xxx_get_bus_frequency(&op->dev);
  383. ms->gpio_cs_count = gpiod_count(&op->dev, NULL);
  384. if (ms->gpio_cs_count > 0) {
  385. master->num_chipselect = ms->gpio_cs_count;
  386. ms->gpio_cs = kmalloc_array(ms->gpio_cs_count,
  387. sizeof(*ms->gpio_cs),
  388. GFP_KERNEL);
  389. if (!ms->gpio_cs) {
  390. rc = -ENOMEM;
  391. goto err_alloc_gpio;
  392. }
  393. for (i = 0; i < ms->gpio_cs_count; i++) {
  394. gpio_cs = gpiod_get_index(&op->dev,
  395. NULL, i, GPIOD_OUT_LOW);
  396. rc = PTR_ERR_OR_ZERO(gpio_cs);
  397. if (rc) {
  398. dev_err(&op->dev,
  399. "failed to get spi cs gpio #%d: %d\n",
  400. i, rc);
  401. goto err_gpio;
  402. }
  403. ms->gpio_cs[i] = gpio_cs;
  404. }
  405. }
  406. spin_lock_init(&ms->lock);
  407. INIT_LIST_HEAD(&ms->queue);
  408. INIT_WORK(&ms->work, mpc52xx_spi_wq);
  409. /* Decide if interrupts can be used */
  410. if (ms->irq0 && ms->irq1) {
  411. rc = request_irq(ms->irq0, mpc52xx_spi_irq, 0,
  412. "mpc5200-spi-modf", ms);
  413. rc |= request_irq(ms->irq1, mpc52xx_spi_irq, 0,
  414. "mpc5200-spi-spif", ms);
  415. if (rc) {
  416. free_irq(ms->irq0, ms);
  417. free_irq(ms->irq1, ms);
  418. ms->irq0 = ms->irq1 = 0;
  419. }
  420. } else {
  421. /* operate in polled mode */
  422. ms->irq0 = ms->irq1 = 0;
  423. }
  424. if (!ms->irq0)
  425. dev_info(&op->dev, "using polled mode\n");
  426. dev_dbg(&op->dev, "registering spi_master struct\n");
  427. rc = spi_register_master(master);
  428. if (rc)
  429. goto err_register;
  430. dev_info(&ms->master->dev, "registered MPC5200 SPI bus\n");
  431. return rc;
  432. err_register:
  433. dev_err(&ms->master->dev, "initialization failed\n");
  434. err_gpio:
  435. while (i-- > 0)
  436. gpiod_put(ms->gpio_cs[i]);
  437. kfree(ms->gpio_cs);
  438. err_alloc_gpio:
  439. spi_master_put(master);
  440. err_alloc:
  441. err_init:
  442. iounmap(regs);
  443. return rc;
  444. }
  445. static int mpc52xx_spi_remove(struct platform_device *op)
  446. {
  447. struct spi_master *master = spi_master_get(platform_get_drvdata(op));
  448. struct mpc52xx_spi *ms = spi_master_get_devdata(master);
  449. int i;
  450. free_irq(ms->irq0, ms);
  451. free_irq(ms->irq1, ms);
  452. for (i = 0; i < ms->gpio_cs_count; i++)
  453. gpiod_put(ms->gpio_cs[i]);
  454. kfree(ms->gpio_cs);
  455. spi_unregister_master(master);
  456. iounmap(ms->regs);
  457. spi_master_put(master);
  458. return 0;
  459. }
  460. static const struct of_device_id mpc52xx_spi_match[] = {
  461. { .compatible = "fsl,mpc5200-spi", },
  462. {}
  463. };
  464. MODULE_DEVICE_TABLE(of, mpc52xx_spi_match);
  465. static struct platform_driver mpc52xx_spi_of_driver = {
  466. .driver = {
  467. .name = "mpc52xx-spi",
  468. .of_match_table = mpc52xx_spi_match,
  469. },
  470. .probe = mpc52xx_spi_probe,
  471. .remove = mpc52xx_spi_remove,
  472. };
  473. module_platform_driver(mpc52xx_spi_of_driver);