spi-mpc52xx-psc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * MPC52xx PSC in SPI mode driver.
  3. *
  4. * Maintainer: Dragos Carp
  5. *
  6. * Copyright (C) 2006 TOPTICA Photonics AG.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/completion.h>
  21. #include <linux/io.h>
  22. #include <linux/delay.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/fsl_devices.h>
  25. #include <linux/slab.h>
  26. #include <asm/mpc52xx.h>
  27. #include <asm/mpc52xx_psc.h>
  28. #define MCLK 20000000 /* PSC port MClk in hz */
  29. struct mpc52xx_psc_spi {
  30. /* fsl_spi_platform data */
  31. void (*cs_control)(struct spi_device *spi, bool on);
  32. u32 sysclk;
  33. /* driver internal data */
  34. struct mpc52xx_psc __iomem *psc;
  35. struct mpc52xx_psc_fifo __iomem *fifo;
  36. unsigned int irq;
  37. u8 bits_per_word;
  38. u8 busy;
  39. struct workqueue_struct *workqueue;
  40. struct work_struct work;
  41. struct list_head queue;
  42. spinlock_t lock;
  43. struct completion done;
  44. };
  45. /* controller state */
  46. struct mpc52xx_psc_spi_cs {
  47. int bits_per_word;
  48. int speed_hz;
  49. };
  50. /* set clock freq, clock ramp, bits per work
  51. * if t is NULL then reset the values to the default values
  52. */
  53. static int mpc52xx_psc_spi_transfer_setup(struct spi_device *spi,
  54. struct spi_transfer *t)
  55. {
  56. struct mpc52xx_psc_spi_cs *cs = spi->controller_state;
  57. cs->speed_hz = (t && t->speed_hz)
  58. ? t->speed_hz : spi->max_speed_hz;
  59. cs->bits_per_word = (t && t->bits_per_word)
  60. ? t->bits_per_word : spi->bits_per_word;
  61. cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8;
  62. return 0;
  63. }
  64. static void mpc52xx_psc_spi_activate_cs(struct spi_device *spi)
  65. {
  66. struct mpc52xx_psc_spi_cs *cs = spi->controller_state;
  67. struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
  68. struct mpc52xx_psc __iomem *psc = mps->psc;
  69. u32 sicr;
  70. u16 ccr;
  71. sicr = in_be32(&psc->sicr);
  72. /* Set clock phase and polarity */
  73. if (spi->mode & SPI_CPHA)
  74. sicr |= 0x00001000;
  75. else
  76. sicr &= ~0x00001000;
  77. if (spi->mode & SPI_CPOL)
  78. sicr |= 0x00002000;
  79. else
  80. sicr &= ~0x00002000;
  81. if (spi->mode & SPI_LSB_FIRST)
  82. sicr |= 0x10000000;
  83. else
  84. sicr &= ~0x10000000;
  85. out_be32(&psc->sicr, sicr);
  86. /* Set clock frequency and bits per word
  87. * Because psc->ccr is defined as 16bit register instead of 32bit
  88. * just set the lower byte of BitClkDiv
  89. */
  90. ccr = in_be16((u16 __iomem *)&psc->ccr);
  91. ccr &= 0xFF00;
  92. if (cs->speed_hz)
  93. ccr |= (MCLK / cs->speed_hz - 1) & 0xFF;
  94. else /* by default SPI Clk 1MHz */
  95. ccr |= (MCLK / 1000000 - 1) & 0xFF;
  96. out_be16((u16 __iomem *)&psc->ccr, ccr);
  97. mps->bits_per_word = cs->bits_per_word;
  98. if (mps->cs_control)
  99. mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 1 : 0);
  100. }
  101. static void mpc52xx_psc_spi_deactivate_cs(struct spi_device *spi)
  102. {
  103. struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
  104. if (mps->cs_control)
  105. mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 0 : 1);
  106. }
  107. #define MPC52xx_PSC_BUFSIZE (MPC52xx_PSC_RFNUM_MASK + 1)
  108. /* wake up when 80% fifo full */
  109. #define MPC52xx_PSC_RFALARM (MPC52xx_PSC_BUFSIZE * 20 / 100)
  110. static int mpc52xx_psc_spi_transfer_rxtx(struct spi_device *spi,
  111. struct spi_transfer *t)
  112. {
  113. struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
  114. struct mpc52xx_psc __iomem *psc = mps->psc;
  115. struct mpc52xx_psc_fifo __iomem *fifo = mps->fifo;
  116. unsigned rb = 0; /* number of bytes receieved */
  117. unsigned sb = 0; /* number of bytes sent */
  118. unsigned char *rx_buf = (unsigned char *)t->rx_buf;
  119. unsigned char *tx_buf = (unsigned char *)t->tx_buf;
  120. unsigned rfalarm;
  121. unsigned send_at_once = MPC52xx_PSC_BUFSIZE;
  122. unsigned recv_at_once;
  123. int last_block = 0;
  124. if (!t->tx_buf && !t->rx_buf && t->len)
  125. return -EINVAL;
  126. /* enable transmiter/receiver */
  127. out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
  128. while (rb < t->len) {
  129. if (t->len - rb > MPC52xx_PSC_BUFSIZE) {
  130. rfalarm = MPC52xx_PSC_RFALARM;
  131. last_block = 0;
  132. } else {
  133. send_at_once = t->len - sb;
  134. rfalarm = MPC52xx_PSC_BUFSIZE - (t->len - rb);
  135. last_block = 1;
  136. }
  137. dev_dbg(&spi->dev, "send %d bytes...\n", send_at_once);
  138. for (; send_at_once; sb++, send_at_once--) {
  139. /* set EOF flag before the last word is sent */
  140. if (send_at_once == 1 && last_block)
  141. out_8(&psc->ircr2, 0x01);
  142. if (tx_buf)
  143. out_8(&psc->mpc52xx_psc_buffer_8, tx_buf[sb]);
  144. else
  145. out_8(&psc->mpc52xx_psc_buffer_8, 0);
  146. }
  147. /* enable interrupts and wait for wake up
  148. * if just one byte is expected the Rx FIFO genererates no
  149. * FFULL interrupt, so activate the RxRDY interrupt
  150. */
  151. out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
  152. if (t->len - rb == 1) {
  153. out_8(&psc->mode, 0);
  154. } else {
  155. out_8(&psc->mode, MPC52xx_PSC_MODE_FFULL);
  156. out_be16(&fifo->rfalarm, rfalarm);
  157. }
  158. out_be16(&psc->mpc52xx_psc_imr, MPC52xx_PSC_IMR_RXRDY);
  159. wait_for_completion(&mps->done);
  160. recv_at_once = in_be16(&fifo->rfnum);
  161. dev_dbg(&spi->dev, "%d bytes received\n", recv_at_once);
  162. send_at_once = recv_at_once;
  163. if (rx_buf) {
  164. for (; recv_at_once; rb++, recv_at_once--)
  165. rx_buf[rb] = in_8(&psc->mpc52xx_psc_buffer_8);
  166. } else {
  167. for (; recv_at_once; rb++, recv_at_once--)
  168. in_8(&psc->mpc52xx_psc_buffer_8);
  169. }
  170. }
  171. /* disable transmiter/receiver */
  172. out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  173. return 0;
  174. }
  175. static void mpc52xx_psc_spi_work(struct work_struct *work)
  176. {
  177. struct mpc52xx_psc_spi *mps =
  178. container_of(work, struct mpc52xx_psc_spi, work);
  179. spin_lock_irq(&mps->lock);
  180. mps->busy = 1;
  181. while (!list_empty(&mps->queue)) {
  182. struct spi_message *m;
  183. struct spi_device *spi;
  184. struct spi_transfer *t = NULL;
  185. unsigned cs_change;
  186. int status;
  187. m = container_of(mps->queue.next, struct spi_message, queue);
  188. list_del_init(&m->queue);
  189. spin_unlock_irq(&mps->lock);
  190. spi = m->spi;
  191. cs_change = 1;
  192. status = 0;
  193. list_for_each_entry (t, &m->transfers, transfer_list) {
  194. if (t->bits_per_word || t->speed_hz) {
  195. status = mpc52xx_psc_spi_transfer_setup(spi, t);
  196. if (status < 0)
  197. break;
  198. }
  199. if (cs_change)
  200. mpc52xx_psc_spi_activate_cs(spi);
  201. cs_change = t->cs_change;
  202. status = mpc52xx_psc_spi_transfer_rxtx(spi, t);
  203. if (status)
  204. break;
  205. m->actual_length += t->len;
  206. if (t->delay_usecs)
  207. udelay(t->delay_usecs);
  208. if (cs_change)
  209. mpc52xx_psc_spi_deactivate_cs(spi);
  210. }
  211. m->status = status;
  212. if (m->complete)
  213. m->complete(m->context);
  214. if (status || !cs_change)
  215. mpc52xx_psc_spi_deactivate_cs(spi);
  216. mpc52xx_psc_spi_transfer_setup(spi, NULL);
  217. spin_lock_irq(&mps->lock);
  218. }
  219. mps->busy = 0;
  220. spin_unlock_irq(&mps->lock);
  221. }
  222. static int mpc52xx_psc_spi_setup(struct spi_device *spi)
  223. {
  224. struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
  225. struct mpc52xx_psc_spi_cs *cs = spi->controller_state;
  226. unsigned long flags;
  227. if (spi->bits_per_word%8)
  228. return -EINVAL;
  229. if (!cs) {
  230. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  231. if (!cs)
  232. return -ENOMEM;
  233. spi->controller_state = cs;
  234. }
  235. cs->bits_per_word = spi->bits_per_word;
  236. cs->speed_hz = spi->max_speed_hz;
  237. spin_lock_irqsave(&mps->lock, flags);
  238. if (!mps->busy)
  239. mpc52xx_psc_spi_deactivate_cs(spi);
  240. spin_unlock_irqrestore(&mps->lock, flags);
  241. return 0;
  242. }
  243. static int mpc52xx_psc_spi_transfer(struct spi_device *spi,
  244. struct spi_message *m)
  245. {
  246. struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
  247. unsigned long flags;
  248. m->actual_length = 0;
  249. m->status = -EINPROGRESS;
  250. spin_lock_irqsave(&mps->lock, flags);
  251. list_add_tail(&m->queue, &mps->queue);
  252. queue_work(mps->workqueue, &mps->work);
  253. spin_unlock_irqrestore(&mps->lock, flags);
  254. return 0;
  255. }
  256. static void mpc52xx_psc_spi_cleanup(struct spi_device *spi)
  257. {
  258. kfree(spi->controller_state);
  259. }
  260. static int mpc52xx_psc_spi_port_config(int psc_id, struct mpc52xx_psc_spi *mps)
  261. {
  262. struct mpc52xx_psc __iomem *psc = mps->psc;
  263. struct mpc52xx_psc_fifo __iomem *fifo = mps->fifo;
  264. u32 mclken_div;
  265. int ret;
  266. /* default sysclk is 512MHz */
  267. mclken_div = (mps->sysclk ? mps->sysclk : 512000000) / MCLK;
  268. ret = mpc52xx_set_psc_clkdiv(psc_id, mclken_div);
  269. if (ret)
  270. return ret;
  271. /* Reset the PSC into a known state */
  272. out_8(&psc->command, MPC52xx_PSC_RST_RX);
  273. out_8(&psc->command, MPC52xx_PSC_RST_TX);
  274. out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  275. /* Disable interrupts, interrupts are based on alarm level */
  276. out_be16(&psc->mpc52xx_psc_imr, 0);
  277. out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
  278. out_8(&fifo->rfcntl, 0);
  279. out_8(&psc->mode, MPC52xx_PSC_MODE_FFULL);
  280. /* Configure 8bit codec mode as a SPI master and use EOF flags */
  281. /* SICR_SIM_CODEC8|SICR_GENCLK|SICR_SPI|SICR_MSTR|SICR_USEEOF */
  282. out_be32(&psc->sicr, 0x0180C800);
  283. out_be16((u16 __iomem *)&psc->ccr, 0x070F); /* default SPI Clk 1MHz */
  284. /* Set 2ms DTL delay */
  285. out_8(&psc->ctur, 0x00);
  286. out_8(&psc->ctlr, 0x84);
  287. mps->bits_per_word = 8;
  288. return 0;
  289. }
  290. static irqreturn_t mpc52xx_psc_spi_isr(int irq, void *dev_id)
  291. {
  292. struct mpc52xx_psc_spi *mps = (struct mpc52xx_psc_spi *)dev_id;
  293. struct mpc52xx_psc __iomem *psc = mps->psc;
  294. /* disable interrupt and wake up the work queue */
  295. if (in_be16(&psc->mpc52xx_psc_isr) & MPC52xx_PSC_IMR_RXRDY) {
  296. out_be16(&psc->mpc52xx_psc_imr, 0);
  297. complete(&mps->done);
  298. return IRQ_HANDLED;
  299. }
  300. return IRQ_NONE;
  301. }
  302. /* bus_num is used only for the case dev->platform_data == NULL */
  303. static int mpc52xx_psc_spi_do_probe(struct device *dev, u32 regaddr,
  304. u32 size, unsigned int irq, s16 bus_num)
  305. {
  306. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  307. struct mpc52xx_psc_spi *mps;
  308. struct spi_master *master;
  309. int ret;
  310. master = spi_alloc_master(dev, sizeof *mps);
  311. if (master == NULL)
  312. return -ENOMEM;
  313. dev_set_drvdata(dev, master);
  314. mps = spi_master_get_devdata(master);
  315. /* the spi->mode bits understood by this driver: */
  316. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  317. mps->irq = irq;
  318. if (pdata == NULL) {
  319. dev_warn(dev,
  320. "probe called without platform data, no cs_control function will be called\n");
  321. mps->cs_control = NULL;
  322. mps->sysclk = 0;
  323. master->bus_num = bus_num;
  324. master->num_chipselect = 255;
  325. } else {
  326. mps->cs_control = pdata->cs_control;
  327. mps->sysclk = pdata->sysclk;
  328. master->bus_num = pdata->bus_num;
  329. master->num_chipselect = pdata->max_chipselect;
  330. }
  331. master->setup = mpc52xx_psc_spi_setup;
  332. master->transfer = mpc52xx_psc_spi_transfer;
  333. master->cleanup = mpc52xx_psc_spi_cleanup;
  334. master->dev.of_node = dev->of_node;
  335. mps->psc = ioremap(regaddr, size);
  336. if (!mps->psc) {
  337. dev_err(dev, "could not ioremap I/O port range\n");
  338. ret = -EFAULT;
  339. goto free_master;
  340. }
  341. /* On the 5200, fifo regs are immediately ajacent to the psc regs */
  342. mps->fifo = ((void __iomem *)mps->psc) + sizeof(struct mpc52xx_psc);
  343. ret = request_irq(mps->irq, mpc52xx_psc_spi_isr, 0, "mpc52xx-psc-spi",
  344. mps);
  345. if (ret)
  346. goto free_master;
  347. ret = mpc52xx_psc_spi_port_config(master->bus_num, mps);
  348. if (ret < 0) {
  349. dev_err(dev, "can't configure PSC! Is it capable of SPI?\n");
  350. goto free_irq;
  351. }
  352. spin_lock_init(&mps->lock);
  353. init_completion(&mps->done);
  354. INIT_WORK(&mps->work, mpc52xx_psc_spi_work);
  355. INIT_LIST_HEAD(&mps->queue);
  356. mps->workqueue = create_singlethread_workqueue(
  357. dev_name(master->dev.parent));
  358. if (mps->workqueue == NULL) {
  359. ret = -EBUSY;
  360. goto free_irq;
  361. }
  362. ret = spi_register_master(master);
  363. if (ret < 0)
  364. goto unreg_master;
  365. return ret;
  366. unreg_master:
  367. destroy_workqueue(mps->workqueue);
  368. free_irq:
  369. free_irq(mps->irq, mps);
  370. free_master:
  371. if (mps->psc)
  372. iounmap(mps->psc);
  373. spi_master_put(master);
  374. return ret;
  375. }
  376. static int mpc52xx_psc_spi_of_probe(struct platform_device *op)
  377. {
  378. const u32 *regaddr_p;
  379. u64 regaddr64, size64;
  380. s16 id = -1;
  381. regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL);
  382. if (!regaddr_p) {
  383. dev_err(&op->dev, "Invalid PSC address\n");
  384. return -EINVAL;
  385. }
  386. regaddr64 = of_translate_address(op->dev.of_node, regaddr_p);
  387. /* get PSC id (1..6, used by port_config) */
  388. if (op->dev.platform_data == NULL) {
  389. const u32 *psc_nump;
  390. psc_nump = of_get_property(op->dev.of_node, "cell-index", NULL);
  391. if (!psc_nump || *psc_nump > 5) {
  392. dev_err(&op->dev, "Invalid cell-index property\n");
  393. return -EINVAL;
  394. }
  395. id = *psc_nump + 1;
  396. }
  397. return mpc52xx_psc_spi_do_probe(&op->dev, (u32)regaddr64, (u32)size64,
  398. irq_of_parse_and_map(op->dev.of_node, 0), id);
  399. }
  400. static int mpc52xx_psc_spi_of_remove(struct platform_device *op)
  401. {
  402. struct spi_master *master = spi_master_get(platform_get_drvdata(op));
  403. struct mpc52xx_psc_spi *mps = spi_master_get_devdata(master);
  404. flush_workqueue(mps->workqueue);
  405. destroy_workqueue(mps->workqueue);
  406. spi_unregister_master(master);
  407. free_irq(mps->irq, mps);
  408. if (mps->psc)
  409. iounmap(mps->psc);
  410. spi_master_put(master);
  411. return 0;
  412. }
  413. static const struct of_device_id mpc52xx_psc_spi_of_match[] = {
  414. { .compatible = "fsl,mpc5200-psc-spi", },
  415. { .compatible = "mpc5200-psc-spi", }, /* old */
  416. {}
  417. };
  418. MODULE_DEVICE_TABLE(of, mpc52xx_psc_spi_of_match);
  419. static struct platform_driver mpc52xx_psc_spi_of_driver = {
  420. .probe = mpc52xx_psc_spi_of_probe,
  421. .remove = mpc52xx_psc_spi_of_remove,
  422. .driver = {
  423. .name = "mpc52xx-psc-spi",
  424. .of_match_table = mpc52xx_psc_spi_of_match,
  425. },
  426. };
  427. module_platform_driver(mpc52xx_psc_spi_of_driver);
  428. MODULE_AUTHOR("Dragos Carp");
  429. MODULE_DESCRIPTION("MPC52xx PSC SPI Driver");
  430. MODULE_LICENSE("GPL");