mpc52xx_psc_spi.c 14 KB

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