mpc52xx_psc_spi.c 15 KB

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