mpc512x_psc_spi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * MPC512x PSC in SPI mode driver.
  3. *
  4. * Copyright (C) 2007,2008 Freescale Semiconductor Inc.
  5. * Original port from 52xx driver:
  6. * Hongjun Chen <hong-jun.chen@freescale.com>
  7. *
  8. * Fork of mpc52xx_psc_spi.c:
  9. * Copyright (C) 2006 TOPTICA Photonics AG., Dragos Carp
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/errno.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/completion.h>
  24. #include <linux/io.h>
  25. #include <linux/delay.h>
  26. #include <linux/clk.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/fsl_devices.h>
  29. #include <asm/mpc52xx_psc.h>
  30. struct mpc512x_psc_spi {
  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 mpc512x_psc_fifo __iomem *fifo;
  36. unsigned int irq;
  37. u8 bits_per_word;
  38. u8 busy;
  39. u32 mclk;
  40. u8 eofbyte;
  41. struct workqueue_struct *workqueue;
  42. struct work_struct work;
  43. struct list_head queue;
  44. spinlock_t lock; /* Message queue lock */
  45. struct completion done;
  46. };
  47. /* controller state */
  48. struct mpc512x_psc_spi_cs {
  49. int bits_per_word;
  50. int speed_hz;
  51. };
  52. /* set clock freq, clock ramp, bits per work
  53. * if t is NULL then reset the values to the default values
  54. */
  55. static int mpc512x_psc_spi_transfer_setup(struct spi_device *spi,
  56. struct spi_transfer *t)
  57. {
  58. struct mpc512x_psc_spi_cs *cs = spi->controller_state;
  59. cs->speed_hz = (t && t->speed_hz)
  60. ? t->speed_hz : spi->max_speed_hz;
  61. cs->bits_per_word = (t && t->bits_per_word)
  62. ? t->bits_per_word : spi->bits_per_word;
  63. cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8;
  64. return 0;
  65. }
  66. static void mpc512x_psc_spi_activate_cs(struct spi_device *spi)
  67. {
  68. struct mpc512x_psc_spi_cs *cs = spi->controller_state;
  69. struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
  70. struct mpc52xx_psc __iomem *psc = mps->psc;
  71. u32 sicr;
  72. u32 ccr;
  73. u16 bclkdiv;
  74. sicr = in_be32(&psc->sicr);
  75. /* Set clock phase and polarity */
  76. if (spi->mode & SPI_CPHA)
  77. sicr |= 0x00001000;
  78. else
  79. sicr &= ~0x00001000;
  80. if (spi->mode & SPI_CPOL)
  81. sicr |= 0x00002000;
  82. else
  83. sicr &= ~0x00002000;
  84. if (spi->mode & SPI_LSB_FIRST)
  85. sicr |= 0x10000000;
  86. else
  87. sicr &= ~0x10000000;
  88. out_be32(&psc->sicr, sicr);
  89. ccr = in_be32(&psc->ccr);
  90. ccr &= 0xFF000000;
  91. if (cs->speed_hz)
  92. bclkdiv = (mps->mclk / cs->speed_hz) - 1;
  93. else
  94. bclkdiv = (mps->mclk / 1000000) - 1; /* default 1MHz */
  95. ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
  96. out_be32(&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 mpc512x_psc_spi_deactivate_cs(struct spi_device *spi)
  102. {
  103. struct mpc512x_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. /* extract and scale size field in txsz or rxsz */
  108. #define MPC512x_PSC_FIFO_SZ(sz) ((sz & 0x7ff) << 2);
  109. #define EOFBYTE 1
  110. static int mpc512x_psc_spi_transfer_rxtx(struct spi_device *spi,
  111. struct spi_transfer *t)
  112. {
  113. struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
  114. struct mpc52xx_psc __iomem *psc = mps->psc;
  115. struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
  116. size_t len = t->len;
  117. u8 *tx_buf = (u8 *)t->tx_buf;
  118. u8 *rx_buf = (u8 *)t->rx_buf;
  119. if (!tx_buf && !rx_buf && t->len)
  120. return -EINVAL;
  121. /* Zero MR2 */
  122. in_8(&psc->mode);
  123. out_8(&psc->mode, 0x0);
  124. while (len) {
  125. int count;
  126. int i;
  127. u8 data;
  128. size_t fifosz;
  129. int rxcount;
  130. /*
  131. * The number of bytes that can be sent at a time
  132. * depends on the fifo size.
  133. */
  134. fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->txsz));
  135. count = min(fifosz, len);
  136. for (i = count; i > 0; i--) {
  137. data = tx_buf ? *tx_buf++ : 0;
  138. if (len == EOFBYTE)
  139. setbits32(&fifo->txcmd, MPC512x_PSC_FIFO_EOF);
  140. out_8(&fifo->txdata_8, data);
  141. len--;
  142. }
  143. INIT_COMPLETION(mps->done);
  144. /* interrupt on tx fifo empty */
  145. out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
  146. out_be32(&fifo->tximr, MPC512x_PSC_FIFO_EMPTY);
  147. /* enable transmiter/receiver */
  148. out_8(&psc->command,
  149. MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
  150. wait_for_completion(&mps->done);
  151. mdelay(1);
  152. /* rx fifo should have count bytes in it */
  153. rxcount = in_be32(&fifo->rxcnt);
  154. if (rxcount != count)
  155. mdelay(1);
  156. rxcount = in_be32(&fifo->rxcnt);
  157. if (rxcount != count) {
  158. dev_warn(&spi->dev, "expected %d bytes in rx fifo "
  159. "but got %d\n", count, rxcount);
  160. }
  161. rxcount = min(rxcount, count);
  162. for (i = rxcount; i > 0; i--) {
  163. data = in_8(&fifo->rxdata_8);
  164. if (rx_buf)
  165. *rx_buf++ = data;
  166. }
  167. while (in_be32(&fifo->rxcnt)) {
  168. in_8(&fifo->rxdata_8);
  169. }
  170. out_8(&psc->command,
  171. MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  172. }
  173. /* disable transmiter/receiver and fifo interrupt */
  174. out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  175. out_be32(&fifo->tximr, 0);
  176. return 0;
  177. }
  178. static void mpc512x_psc_spi_work(struct work_struct *work)
  179. {
  180. struct mpc512x_psc_spi *mps = container_of(work,
  181. struct mpc512x_psc_spi,
  182. work);
  183. spin_lock_irq(&mps->lock);
  184. mps->busy = 1;
  185. while (!list_empty(&mps->queue)) {
  186. struct spi_message *m;
  187. struct spi_device *spi;
  188. struct spi_transfer *t = NULL;
  189. unsigned cs_change;
  190. int status;
  191. m = container_of(mps->queue.next, struct spi_message, queue);
  192. list_del_init(&m->queue);
  193. spin_unlock_irq(&mps->lock);
  194. spi = m->spi;
  195. cs_change = 1;
  196. status = 0;
  197. list_for_each_entry(t, &m->transfers, transfer_list) {
  198. if (t->bits_per_word || t->speed_hz) {
  199. status = mpc512x_psc_spi_transfer_setup(spi, t);
  200. if (status < 0)
  201. break;
  202. }
  203. if (cs_change)
  204. mpc512x_psc_spi_activate_cs(spi);
  205. cs_change = t->cs_change;
  206. status = mpc512x_psc_spi_transfer_rxtx(spi, t);
  207. if (status)
  208. break;
  209. m->actual_length += t->len;
  210. if (t->delay_usecs)
  211. udelay(t->delay_usecs);
  212. if (cs_change)
  213. mpc512x_psc_spi_deactivate_cs(spi);
  214. }
  215. m->status = status;
  216. m->complete(m->context);
  217. if (status || !cs_change)
  218. mpc512x_psc_spi_deactivate_cs(spi);
  219. mpc512x_psc_spi_transfer_setup(spi, NULL);
  220. spin_lock_irq(&mps->lock);
  221. }
  222. mps->busy = 0;
  223. spin_unlock_irq(&mps->lock);
  224. }
  225. static int mpc512x_psc_spi_setup(struct spi_device *spi)
  226. {
  227. struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
  228. struct mpc512x_psc_spi_cs *cs = spi->controller_state;
  229. unsigned long flags;
  230. if (spi->bits_per_word % 8)
  231. return -EINVAL;
  232. if (!cs) {
  233. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  234. if (!cs)
  235. return -ENOMEM;
  236. spi->controller_state = cs;
  237. }
  238. cs->bits_per_word = spi->bits_per_word;
  239. cs->speed_hz = spi->max_speed_hz;
  240. spin_lock_irqsave(&mps->lock, flags);
  241. if (!mps->busy)
  242. mpc512x_psc_spi_deactivate_cs(spi);
  243. spin_unlock_irqrestore(&mps->lock, flags);
  244. return 0;
  245. }
  246. static int mpc512x_psc_spi_transfer(struct spi_device *spi,
  247. struct spi_message *m)
  248. {
  249. struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
  250. unsigned long flags;
  251. m->actual_length = 0;
  252. m->status = -EINPROGRESS;
  253. spin_lock_irqsave(&mps->lock, flags);
  254. list_add_tail(&m->queue, &mps->queue);
  255. queue_work(mps->workqueue, &mps->work);
  256. spin_unlock_irqrestore(&mps->lock, flags);
  257. return 0;
  258. }
  259. static void mpc512x_psc_spi_cleanup(struct spi_device *spi)
  260. {
  261. kfree(spi->controller_state);
  262. }
  263. static int mpc512x_psc_spi_port_config(struct spi_master *master,
  264. struct mpc512x_psc_spi *mps)
  265. {
  266. struct mpc52xx_psc __iomem *psc = mps->psc;
  267. struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
  268. struct clk *spiclk;
  269. int ret = 0;
  270. char name[32];
  271. u32 sicr;
  272. u32 ccr;
  273. u16 bclkdiv;
  274. sprintf(name, "psc%d_mclk", master->bus_num);
  275. spiclk = clk_get(&master->dev, name);
  276. clk_enable(spiclk);
  277. mps->mclk = clk_get_rate(spiclk);
  278. clk_put(spiclk);
  279. /* Reset the PSC into a known state */
  280. out_8(&psc->command, MPC52xx_PSC_RST_RX);
  281. out_8(&psc->command, MPC52xx_PSC_RST_TX);
  282. out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  283. /* Disable psc interrupts all useful interrupts are in fifo */
  284. out_be16(&psc->isr_imr.imr, 0);
  285. /* Disable fifo interrupts, will be enabled later */
  286. out_be32(&fifo->tximr, 0);
  287. out_be32(&fifo->rximr, 0);
  288. /* Setup fifo slice address and size */
  289. /*out_be32(&fifo->txsz, 0x0fe00004);*/
  290. /*out_be32(&fifo->rxsz, 0x0ff00004);*/
  291. sicr = 0x01000000 | /* SIM = 0001 -- 8 bit */
  292. 0x00800000 | /* GenClk = 1 -- internal clk */
  293. 0x00008000 | /* SPI = 1 */
  294. 0x00004000 | /* MSTR = 1 -- SPI master */
  295. 0x00000800; /* UseEOF = 1 -- SS low until EOF */
  296. out_be32(&psc->sicr, sicr);
  297. ccr = in_be32(&psc->ccr);
  298. ccr &= 0xFF000000;
  299. bclkdiv = (mps->mclk / 1000000) - 1; /* default 1MHz */
  300. ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
  301. out_be32(&psc->ccr, ccr);
  302. /* Set 2ms DTL delay */
  303. out_8(&psc->ctur, 0x00);
  304. out_8(&psc->ctlr, 0x82);
  305. /* we don't use the alarms */
  306. out_be32(&fifo->rxalarm, 0xfff);
  307. out_be32(&fifo->txalarm, 0);
  308. /* Enable FIFO slices for Rx/Tx */
  309. out_be32(&fifo->rxcmd,
  310. MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
  311. out_be32(&fifo->txcmd,
  312. MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
  313. mps->bits_per_word = 8;
  314. return ret;
  315. }
  316. static irqreturn_t mpc512x_psc_spi_isr(int irq, void *dev_id)
  317. {
  318. struct mpc512x_psc_spi *mps = (struct mpc512x_psc_spi *)dev_id;
  319. struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
  320. /* clear interrupt and wake up the work queue */
  321. if (in_be32(&fifo->txisr) &
  322. in_be32(&fifo->tximr) & MPC512x_PSC_FIFO_EMPTY) {
  323. out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
  324. out_be32(&fifo->tximr, 0);
  325. complete(&mps->done);
  326. return IRQ_HANDLED;
  327. }
  328. return IRQ_NONE;
  329. }
  330. /* bus_num is used only for the case dev->platform_data == NULL */
  331. static int __init mpc512x_psc_spi_do_probe(struct device *dev, u32 regaddr,
  332. u32 size, unsigned int irq,
  333. s16 bus_num)
  334. {
  335. struct fsl_spi_platform_data *pdata = dev->platform_data;
  336. struct mpc512x_psc_spi *mps;
  337. struct spi_master *master;
  338. int ret;
  339. void *tempp;
  340. master = spi_alloc_master(dev, sizeof *mps);
  341. if (master == NULL)
  342. return -ENOMEM;
  343. dev_set_drvdata(dev, master);
  344. mps = spi_master_get_devdata(master);
  345. mps->irq = irq;
  346. if (pdata == NULL) {
  347. dev_err(dev, "probe called without platform data, no "
  348. "cs_control function will be called\n");
  349. mps->cs_control = NULL;
  350. mps->sysclk = 0;
  351. master->bus_num = bus_num;
  352. master->num_chipselect = 255;
  353. } else {
  354. mps->cs_control = pdata->cs_control;
  355. mps->sysclk = pdata->sysclk;
  356. master->bus_num = pdata->bus_num;
  357. master->num_chipselect = pdata->max_chipselect;
  358. }
  359. master->setup = mpc512x_psc_spi_setup;
  360. master->transfer = mpc512x_psc_spi_transfer;
  361. master->cleanup = mpc512x_psc_spi_cleanup;
  362. tempp = ioremap(regaddr, size);
  363. if (!tempp) {
  364. dev_err(dev, "could not ioremap I/O port range\n");
  365. ret = -EFAULT;
  366. goto free_master;
  367. }
  368. mps->psc = tempp;
  369. mps->fifo =
  370. (struct mpc512x_psc_fifo *)(tempp + sizeof(struct mpc52xx_psc));
  371. ret = request_irq(mps->irq, mpc512x_psc_spi_isr, IRQF_SHARED,
  372. "mpc512x-psc-spi", mps);
  373. if (ret)
  374. goto free_master;
  375. ret = mpc512x_psc_spi_port_config(master, mps);
  376. if (ret < 0)
  377. goto free_irq;
  378. spin_lock_init(&mps->lock);
  379. init_completion(&mps->done);
  380. INIT_WORK(&mps->work, mpc512x_psc_spi_work);
  381. INIT_LIST_HEAD(&mps->queue);
  382. mps->workqueue =
  383. create_singlethread_workqueue(dev_name(master->dev.parent));
  384. if (mps->workqueue == NULL) {
  385. ret = -EBUSY;
  386. goto free_irq;
  387. }
  388. ret = spi_register_master(master);
  389. if (ret < 0)
  390. goto unreg_master;
  391. return ret;
  392. unreg_master:
  393. destroy_workqueue(mps->workqueue);
  394. free_irq:
  395. free_irq(mps->irq, mps);
  396. free_master:
  397. if (mps->psc)
  398. iounmap(mps->psc);
  399. spi_master_put(master);
  400. return ret;
  401. }
  402. static int __exit mpc512x_psc_spi_do_remove(struct device *dev)
  403. {
  404. struct spi_master *master = dev_get_drvdata(dev);
  405. struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
  406. flush_workqueue(mps->workqueue);
  407. destroy_workqueue(mps->workqueue);
  408. spi_unregister_master(master);
  409. free_irq(mps->irq, mps);
  410. if (mps->psc)
  411. iounmap(mps->psc);
  412. return 0;
  413. }
  414. static int __init mpc512x_psc_spi_of_probe(struct of_device *op,
  415. const struct of_device_id *match)
  416. {
  417. const u32 *regaddr_p;
  418. u64 regaddr64, size64;
  419. s16 id = -1;
  420. regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL);
  421. if (!regaddr_p) {
  422. dev_err(&op->dev, "Invalid PSC address\n");
  423. return -EINVAL;
  424. }
  425. regaddr64 = of_translate_address(op->dev.of_node, regaddr_p);
  426. /* get PSC id (0..11, used by port_config) */
  427. if (op->dev.platform_data == NULL) {
  428. const u32 *psc_nump;
  429. psc_nump = of_get_property(op->dev.of_node, "cell-index", NULL);
  430. if (!psc_nump || *psc_nump > 11) {
  431. dev_err(&op->dev, "mpc512x_psc_spi: Device node %s "
  432. "has invalid cell-index property\n",
  433. op->dev.of_node->full_name);
  434. return -EINVAL;
  435. }
  436. id = *psc_nump;
  437. }
  438. return mpc512x_psc_spi_do_probe(&op->dev, (u32) regaddr64, (u32) size64,
  439. irq_of_parse_and_map(op->dev.of_node, 0), id);
  440. }
  441. static int __exit mpc512x_psc_spi_of_remove(struct of_device *op)
  442. {
  443. return mpc512x_psc_spi_do_remove(&op->dev);
  444. }
  445. static struct of_device_id mpc512x_psc_spi_of_match[] = {
  446. { .compatible = "fsl,mpc5121-psc-spi", },
  447. {},
  448. };
  449. MODULE_DEVICE_TABLE(of, mpc512x_psc_spi_of_match);
  450. static struct of_platform_driver mpc512x_psc_spi_of_driver = {
  451. .probe = mpc512x_psc_spi_of_probe,
  452. .remove = __exit_p(mpc512x_psc_spi_of_remove),
  453. .driver = {
  454. .name = "mpc512x-psc-spi",
  455. .owner = THIS_MODULE,
  456. .of_match_table = mpc512x_psc_spi_of_match,
  457. },
  458. };
  459. static int __init mpc512x_psc_spi_init(void)
  460. {
  461. return of_register_platform_driver(&mpc512x_psc_spi_of_driver);
  462. }
  463. module_init(mpc512x_psc_spi_init);
  464. static void __exit mpc512x_psc_spi_exit(void)
  465. {
  466. of_unregister_platform_driver(&mpc512x_psc_spi_of_driver);
  467. }
  468. module_exit(mpc512x_psc_spi_exit);
  469. MODULE_AUTHOR("John Rigby");
  470. MODULE_DESCRIPTION("MPC512x PSC SPI Driver");
  471. MODULE_LICENSE("GPL");