spi-mpc512x-psc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. while (len) {
  127. int count;
  128. int i;
  129. u8 data;
  130. size_t fifosz;
  131. int rxcount;
  132. /*
  133. * The number of bytes that can be sent at a time
  134. * depends on the fifo size.
  135. */
  136. fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->txsz));
  137. count = min(fifosz, len);
  138. for (i = count; i > 0; i--) {
  139. data = tx_buf ? *tx_buf++ : 0;
  140. if (len == EOFBYTE && t->cs_change)
  141. setbits32(&fifo->txcmd, MPC512x_PSC_FIFO_EOF);
  142. out_8(&fifo->txdata_8, data);
  143. len--;
  144. }
  145. INIT_COMPLETION(mps->done);
  146. /* interrupt on tx fifo empty */
  147. out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
  148. out_be32(&fifo->tximr, MPC512x_PSC_FIFO_EMPTY);
  149. /* enable transmiter/receiver */
  150. out_8(&psc->command,
  151. MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
  152. wait_for_completion(&mps->done);
  153. mdelay(1);
  154. /* rx fifo should have count bytes in it */
  155. rxcount = in_be32(&fifo->rxcnt);
  156. if (rxcount != count)
  157. mdelay(1);
  158. rxcount = in_be32(&fifo->rxcnt);
  159. if (rxcount != count) {
  160. dev_warn(&spi->dev, "expected %d bytes in rx fifo "
  161. "but got %d\n", count, rxcount);
  162. }
  163. rxcount = min(rxcount, count);
  164. for (i = rxcount; i > 0; i--) {
  165. data = in_8(&fifo->rxdata_8);
  166. if (rx_buf)
  167. *rx_buf++ = data;
  168. }
  169. while (in_be32(&fifo->rxcnt)) {
  170. in_8(&fifo->rxdata_8);
  171. }
  172. out_8(&psc->command,
  173. MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  174. }
  175. /* disable transmiter/receiver and fifo interrupt */
  176. out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  177. out_be32(&fifo->tximr, 0);
  178. return 0;
  179. }
  180. static void mpc512x_psc_spi_work(struct work_struct *work)
  181. {
  182. struct mpc512x_psc_spi *mps = container_of(work,
  183. struct mpc512x_psc_spi,
  184. work);
  185. spin_lock_irq(&mps->lock);
  186. mps->busy = 1;
  187. while (!list_empty(&mps->queue)) {
  188. struct spi_message *m;
  189. struct spi_device *spi;
  190. struct spi_transfer *t = NULL;
  191. unsigned cs_change;
  192. int status;
  193. m = container_of(mps->queue.next, struct spi_message, queue);
  194. list_del_init(&m->queue);
  195. spin_unlock_irq(&mps->lock);
  196. spi = m->spi;
  197. cs_change = 1;
  198. status = 0;
  199. list_for_each_entry(t, &m->transfers, transfer_list) {
  200. if (t->bits_per_word || t->speed_hz) {
  201. status = mpc512x_psc_spi_transfer_setup(spi, t);
  202. if (status < 0)
  203. break;
  204. }
  205. if (cs_change)
  206. mpc512x_psc_spi_activate_cs(spi);
  207. cs_change = t->cs_change;
  208. status = mpc512x_psc_spi_transfer_rxtx(spi, t);
  209. if (status)
  210. break;
  211. m->actual_length += t->len;
  212. if (t->delay_usecs)
  213. udelay(t->delay_usecs);
  214. if (cs_change)
  215. mpc512x_psc_spi_deactivate_cs(spi);
  216. }
  217. m->status = status;
  218. m->complete(m->context);
  219. if (status || !cs_change)
  220. mpc512x_psc_spi_deactivate_cs(spi);
  221. mpc512x_psc_spi_transfer_setup(spi, NULL);
  222. spin_lock_irq(&mps->lock);
  223. }
  224. mps->busy = 0;
  225. spin_unlock_irq(&mps->lock);
  226. }
  227. static int mpc512x_psc_spi_setup(struct spi_device *spi)
  228. {
  229. struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
  230. struct mpc512x_psc_spi_cs *cs = spi->controller_state;
  231. unsigned long flags;
  232. int ret;
  233. if (spi->bits_per_word % 8)
  234. return -EINVAL;
  235. if (!cs) {
  236. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  237. if (!cs)
  238. return -ENOMEM;
  239. if (gpio_is_valid(spi->cs_gpio)) {
  240. ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
  241. if (ret) {
  242. dev_err(&spi->dev, "can't get CS gpio: %d\n",
  243. ret);
  244. kfree(cs);
  245. return ret;
  246. }
  247. gpio_direction_output(spi->cs_gpio,
  248. spi->mode & SPI_CS_HIGH ? 0 : 1);
  249. }
  250. spi->controller_state = cs;
  251. }
  252. cs->bits_per_word = spi->bits_per_word;
  253. cs->speed_hz = spi->max_speed_hz;
  254. spin_lock_irqsave(&mps->lock, flags);
  255. if (!mps->busy)
  256. mpc512x_psc_spi_deactivate_cs(spi);
  257. spin_unlock_irqrestore(&mps->lock, flags);
  258. return 0;
  259. }
  260. static int mpc512x_psc_spi_transfer(struct spi_device *spi,
  261. struct spi_message *m)
  262. {
  263. struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
  264. unsigned long flags;
  265. m->actual_length = 0;
  266. m->status = -EINPROGRESS;
  267. spin_lock_irqsave(&mps->lock, flags);
  268. list_add_tail(&m->queue, &mps->queue);
  269. queue_work(mps->workqueue, &mps->work);
  270. spin_unlock_irqrestore(&mps->lock, flags);
  271. return 0;
  272. }
  273. static void mpc512x_psc_spi_cleanup(struct spi_device *spi)
  274. {
  275. if (gpio_is_valid(spi->cs_gpio))
  276. gpio_free(spi->cs_gpio);
  277. kfree(spi->controller_state);
  278. }
  279. static int mpc512x_psc_spi_port_config(struct spi_master *master,
  280. struct mpc512x_psc_spi *mps)
  281. {
  282. struct mpc52xx_psc __iomem *psc = mps->psc;
  283. struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
  284. struct clk *spiclk;
  285. int ret = 0;
  286. char name[32];
  287. u32 sicr;
  288. u32 ccr;
  289. u16 bclkdiv;
  290. sprintf(name, "psc%d_mclk", master->bus_num);
  291. spiclk = clk_get(&master->dev, name);
  292. clk_enable(spiclk);
  293. mps->mclk = clk_get_rate(spiclk);
  294. clk_put(spiclk);
  295. /* Reset the PSC into a known state */
  296. out_8(&psc->command, MPC52xx_PSC_RST_RX);
  297. out_8(&psc->command, MPC52xx_PSC_RST_TX);
  298. out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  299. /* Disable psc interrupts all useful interrupts are in fifo */
  300. out_be16(&psc->isr_imr.imr, 0);
  301. /* Disable fifo interrupts, will be enabled later */
  302. out_be32(&fifo->tximr, 0);
  303. out_be32(&fifo->rximr, 0);
  304. /* Setup fifo slice address and size */
  305. /*out_be32(&fifo->txsz, 0x0fe00004);*/
  306. /*out_be32(&fifo->rxsz, 0x0ff00004);*/
  307. sicr = 0x01000000 | /* SIM = 0001 -- 8 bit */
  308. 0x00800000 | /* GenClk = 1 -- internal clk */
  309. 0x00008000 | /* SPI = 1 */
  310. 0x00004000 | /* MSTR = 1 -- SPI master */
  311. 0x00000800; /* UseEOF = 1 -- SS low until EOF */
  312. out_be32(&psc->sicr, sicr);
  313. ccr = in_be32(&psc->ccr);
  314. ccr &= 0xFF000000;
  315. bclkdiv = (mps->mclk / 1000000) - 1; /* default 1MHz */
  316. ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
  317. out_be32(&psc->ccr, ccr);
  318. /* Set 2ms DTL delay */
  319. out_8(&psc->ctur, 0x00);
  320. out_8(&psc->ctlr, 0x82);
  321. /* we don't use the alarms */
  322. out_be32(&fifo->rxalarm, 0xfff);
  323. out_be32(&fifo->txalarm, 0);
  324. /* Enable FIFO slices for Rx/Tx */
  325. out_be32(&fifo->rxcmd,
  326. MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
  327. out_be32(&fifo->txcmd,
  328. MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
  329. mps->bits_per_word = 8;
  330. return ret;
  331. }
  332. static irqreturn_t mpc512x_psc_spi_isr(int irq, void *dev_id)
  333. {
  334. struct mpc512x_psc_spi *mps = (struct mpc512x_psc_spi *)dev_id;
  335. struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
  336. /* clear interrupt and wake up the work queue */
  337. if (in_be32(&fifo->txisr) &
  338. in_be32(&fifo->tximr) & MPC512x_PSC_FIFO_EMPTY) {
  339. out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
  340. out_be32(&fifo->tximr, 0);
  341. complete(&mps->done);
  342. return IRQ_HANDLED;
  343. }
  344. return IRQ_NONE;
  345. }
  346. static void mpc512x_spi_cs_control(struct spi_device *spi, bool onoff)
  347. {
  348. gpio_set_value(spi->cs_gpio, onoff);
  349. }
  350. /* bus_num is used only for the case dev->platform_data == NULL */
  351. static int mpc512x_psc_spi_do_probe(struct device *dev, u32 regaddr,
  352. u32 size, unsigned int irq,
  353. s16 bus_num)
  354. {
  355. struct fsl_spi_platform_data *pdata = dev->platform_data;
  356. struct mpc512x_psc_spi *mps;
  357. struct spi_master *master;
  358. int ret;
  359. void *tempp;
  360. master = spi_alloc_master(dev, sizeof *mps);
  361. if (master == NULL)
  362. return -ENOMEM;
  363. dev_set_drvdata(dev, master);
  364. mps = spi_master_get_devdata(master);
  365. mps->irq = irq;
  366. if (pdata == NULL) {
  367. mps->cs_control = mpc512x_spi_cs_control;
  368. mps->sysclk = 0;
  369. master->bus_num = bus_num;
  370. } else {
  371. mps->cs_control = pdata->cs_control;
  372. mps->sysclk = pdata->sysclk;
  373. master->bus_num = pdata->bus_num;
  374. master->num_chipselect = pdata->max_chipselect;
  375. }
  376. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  377. master->setup = mpc512x_psc_spi_setup;
  378. master->transfer = mpc512x_psc_spi_transfer;
  379. master->cleanup = mpc512x_psc_spi_cleanup;
  380. master->dev.of_node = dev->of_node;
  381. tempp = ioremap(regaddr, size);
  382. if (!tempp) {
  383. dev_err(dev, "could not ioremap I/O port range\n");
  384. ret = -EFAULT;
  385. goto free_master;
  386. }
  387. mps->psc = tempp;
  388. mps->fifo =
  389. (struct mpc512x_psc_fifo *)(tempp + sizeof(struct mpc52xx_psc));
  390. ret = request_irq(mps->irq, mpc512x_psc_spi_isr, IRQF_SHARED,
  391. "mpc512x-psc-spi", mps);
  392. if (ret)
  393. goto free_master;
  394. ret = mpc512x_psc_spi_port_config(master, mps);
  395. if (ret < 0)
  396. goto free_irq;
  397. spin_lock_init(&mps->lock);
  398. init_completion(&mps->done);
  399. INIT_WORK(&mps->work, mpc512x_psc_spi_work);
  400. INIT_LIST_HEAD(&mps->queue);
  401. mps->workqueue =
  402. create_singlethread_workqueue(dev_name(master->dev.parent));
  403. if (mps->workqueue == NULL) {
  404. ret = -EBUSY;
  405. goto free_irq;
  406. }
  407. ret = spi_register_master(master);
  408. if (ret < 0)
  409. goto unreg_master;
  410. return ret;
  411. unreg_master:
  412. destroy_workqueue(mps->workqueue);
  413. free_irq:
  414. free_irq(mps->irq, mps);
  415. free_master:
  416. if (mps->psc)
  417. iounmap(mps->psc);
  418. spi_master_put(master);
  419. return ret;
  420. }
  421. static int mpc512x_psc_spi_do_remove(struct device *dev)
  422. {
  423. struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
  424. struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
  425. flush_workqueue(mps->workqueue);
  426. destroy_workqueue(mps->workqueue);
  427. spi_unregister_master(master);
  428. free_irq(mps->irq, mps);
  429. if (mps->psc)
  430. iounmap(mps->psc);
  431. spi_master_put(master);
  432. return 0;
  433. }
  434. static int mpc512x_psc_spi_of_probe(struct platform_device *op)
  435. {
  436. const u32 *regaddr_p;
  437. u64 regaddr64, size64;
  438. s16 id = -1;
  439. regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL);
  440. if (!regaddr_p) {
  441. dev_err(&op->dev, "Invalid PSC address\n");
  442. return -EINVAL;
  443. }
  444. regaddr64 = of_translate_address(op->dev.of_node, regaddr_p);
  445. /* get PSC id (0..11, used by port_config) */
  446. id = of_alias_get_id(op->dev.of_node, "spi");
  447. if (id < 0) {
  448. dev_err(&op->dev, "no alias id for %s\n",
  449. op->dev.of_node->full_name);
  450. return id;
  451. }
  452. return mpc512x_psc_spi_do_probe(&op->dev, (u32) regaddr64, (u32) size64,
  453. irq_of_parse_and_map(op->dev.of_node, 0), id);
  454. }
  455. static int mpc512x_psc_spi_of_remove(struct platform_device *op)
  456. {
  457. return mpc512x_psc_spi_do_remove(&op->dev);
  458. }
  459. static struct of_device_id mpc512x_psc_spi_of_match[] = {
  460. { .compatible = "fsl,mpc5121-psc-spi", },
  461. {},
  462. };
  463. MODULE_DEVICE_TABLE(of, mpc512x_psc_spi_of_match);
  464. static struct platform_driver mpc512x_psc_spi_of_driver = {
  465. .probe = mpc512x_psc_spi_of_probe,
  466. .remove = mpc512x_psc_spi_of_remove,
  467. .driver = {
  468. .name = "mpc512x-psc-spi",
  469. .owner = THIS_MODULE,
  470. .of_match_table = mpc512x_psc_spi_of_match,
  471. },
  472. };
  473. module_platform_driver(mpc512x_psc_spi_of_driver);
  474. MODULE_AUTHOR("John Rigby");
  475. MODULE_DESCRIPTION("MPC512x PSC SPI Driver");
  476. MODULE_LICENSE("GPL");