spi-mpc512x-psc.c 14 KB

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