spi-mpc512x-psc.c 14 KB

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