spi-mpc512x-psc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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/errno.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_platform.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 <linux/gpio.h>
  30. #include <asm/mpc52xx_psc.h>
  31. struct mpc512x_psc_spi {
  32. void (*cs_control)(struct spi_device *spi, bool on);
  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. struct clk *clk_mclk;
  39. struct clk *clk_ipg;
  40. u32 mclk_rate;
  41. struct completion txisrdone;
  42. };
  43. /* controller state */
  44. struct mpc512x_psc_spi_cs {
  45. int bits_per_word;
  46. int speed_hz;
  47. };
  48. /* set clock freq, clock ramp, bits per work
  49. * if t is NULL then reset the values to the default values
  50. */
  51. static int mpc512x_psc_spi_transfer_setup(struct spi_device *spi,
  52. struct spi_transfer *t)
  53. {
  54. struct mpc512x_psc_spi_cs *cs = spi->controller_state;
  55. cs->speed_hz = (t && t->speed_hz)
  56. ? t->speed_hz : spi->max_speed_hz;
  57. cs->bits_per_word = (t && t->bits_per_word)
  58. ? t->bits_per_word : spi->bits_per_word;
  59. cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8;
  60. return 0;
  61. }
  62. static void mpc512x_psc_spi_activate_cs(struct spi_device *spi)
  63. {
  64. struct mpc512x_psc_spi_cs *cs = spi->controller_state;
  65. struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
  66. struct mpc52xx_psc __iomem *psc = mps->psc;
  67. u32 sicr;
  68. u32 ccr;
  69. int speed;
  70. u16 bclkdiv;
  71. sicr = in_be32(&psc->sicr);
  72. /* Set clock phase and polarity */
  73. if (spi->mode & SPI_CPHA)
  74. sicr |= 0x00001000;
  75. else
  76. sicr &= ~0x00001000;
  77. if (spi->mode & SPI_CPOL)
  78. sicr |= 0x00002000;
  79. else
  80. sicr &= ~0x00002000;
  81. if (spi->mode & SPI_LSB_FIRST)
  82. sicr |= 0x10000000;
  83. else
  84. sicr &= ~0x10000000;
  85. out_be32(&psc->sicr, sicr);
  86. ccr = in_be32(&psc->ccr);
  87. ccr &= 0xFF000000;
  88. speed = cs->speed_hz;
  89. if (!speed)
  90. speed = 1000000; /* default 1MHz */
  91. bclkdiv = (mps->mclk_rate / speed) - 1;
  92. ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
  93. out_be32(&psc->ccr, ccr);
  94. mps->bits_per_word = cs->bits_per_word;
  95. if (mps->cs_control && gpio_is_valid(spi->cs_gpio))
  96. mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 1 : 0);
  97. }
  98. static void mpc512x_psc_spi_deactivate_cs(struct spi_device *spi)
  99. {
  100. struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
  101. if (mps->cs_control && gpio_is_valid(spi->cs_gpio))
  102. mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 0 : 1);
  103. }
  104. /* extract and scale size field in txsz or rxsz */
  105. #define MPC512x_PSC_FIFO_SZ(sz) ((sz & 0x7ff) << 2);
  106. #define EOFBYTE 1
  107. static int mpc512x_psc_spi_transfer_rxtx(struct spi_device *spi,
  108. struct spi_transfer *t)
  109. {
  110. struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
  111. struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
  112. size_t tx_len = t->len;
  113. size_t rx_len = t->len;
  114. u8 *tx_buf = (u8 *)t->tx_buf;
  115. u8 *rx_buf = (u8 *)t->rx_buf;
  116. if (!tx_buf && !rx_buf && t->len)
  117. return -EINVAL;
  118. while (rx_len || tx_len) {
  119. size_t txcount;
  120. u8 data;
  121. size_t fifosz;
  122. size_t rxcount;
  123. int rxtries;
  124. /*
  125. * send the TX bytes in as large a chunk as possible
  126. * but neither exceed the TX nor the RX FIFOs
  127. */
  128. fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->txsz));
  129. txcount = min(fifosz, tx_len);
  130. fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->rxsz));
  131. fifosz -= in_be32(&fifo->rxcnt) + 1;
  132. txcount = min(fifosz, txcount);
  133. if (txcount) {
  134. /* fill the TX FIFO */
  135. while (txcount-- > 0) {
  136. data = tx_buf ? *tx_buf++ : 0;
  137. if (tx_len == EOFBYTE && t->cs_change)
  138. setbits32(&fifo->txcmd,
  139. MPC512x_PSC_FIFO_EOF);
  140. out_8(&fifo->txdata_8, data);
  141. tx_len--;
  142. }
  143. /* have the ISR trigger when the TX FIFO is empty */
  144. reinit_completion(&mps->txisrdone);
  145. out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
  146. out_be32(&fifo->tximr, MPC512x_PSC_FIFO_EMPTY);
  147. wait_for_completion(&mps->txisrdone);
  148. }
  149. /*
  150. * consume as much RX data as the FIFO holds, while we
  151. * iterate over the transfer's TX data length
  152. *
  153. * only insist in draining all the remaining RX bytes
  154. * when the TX bytes were exhausted (that's at the very
  155. * end of this transfer, not when still iterating over
  156. * the transfer's chunks)
  157. */
  158. rxtries = 50;
  159. do {
  160. /*
  161. * grab whatever was in the FIFO when we started
  162. * looking, don't bother fetching what was added to
  163. * the FIFO while we read from it -- we'll return
  164. * here eventually and prefer sending out remaining
  165. * TX data
  166. */
  167. fifosz = in_be32(&fifo->rxcnt);
  168. rxcount = min(fifosz, rx_len);
  169. while (rxcount-- > 0) {
  170. data = in_8(&fifo->rxdata_8);
  171. if (rx_buf)
  172. *rx_buf++ = data;
  173. rx_len--;
  174. }
  175. /*
  176. * come back later if there still is TX data to send,
  177. * bail out of the RX drain loop if all of the TX data
  178. * was sent and all of the RX data was received (i.e.
  179. * when the transmission has completed)
  180. */
  181. if (tx_len)
  182. break;
  183. if (!rx_len)
  184. break;
  185. /*
  186. * TX data transmission has completed while RX data
  187. * is still pending -- that's a transient situation
  188. * which depends on wire speed and specific
  189. * hardware implementation details (buffering) yet
  190. * should resolve very quickly
  191. *
  192. * just yield for a moment to not hog the CPU for
  193. * too long when running SPI at low speed
  194. *
  195. * the timeout range is rather arbitrary and tries
  196. * to balance throughput against system load; the
  197. * chosen values result in a minimal timeout of 50
  198. * times 10us and thus work at speeds as low as
  199. * some 20kbps, while the maximum timeout at the
  200. * transfer's end could be 5ms _if_ nothing else
  201. * ticks in the system _and_ RX data still wasn't
  202. * received, which only occurs in situations that
  203. * are exceptional; removing the unpredictability
  204. * of the timeout either decreases throughput
  205. * (longer timeouts), or puts more load on the
  206. * system (fixed short timeouts) or requires the
  207. * use of a timeout API instead of a counter and an
  208. * unknown inner delay
  209. */
  210. usleep_range(10, 100);
  211. } while (--rxtries > 0);
  212. if (!tx_len && rx_len && !rxtries) {
  213. /*
  214. * not enough RX bytes even after several retries
  215. * and the resulting rather long timeout?
  216. */
  217. rxcount = in_be32(&fifo->rxcnt);
  218. dev_warn(&spi->dev,
  219. "short xfer, missing %zd RX bytes, FIFO level %zd\n",
  220. rx_len, rxcount);
  221. }
  222. /*
  223. * drain and drop RX data which "should not be there" in
  224. * the first place, for undisturbed transmission this turns
  225. * into a NOP (except for the FIFO level fetch)
  226. */
  227. if (!tx_len && !rx_len) {
  228. while (in_be32(&fifo->rxcnt))
  229. in_8(&fifo->rxdata_8);
  230. }
  231. }
  232. return 0;
  233. }
  234. static int mpc512x_psc_spi_msg_xfer(struct spi_master *master,
  235. struct spi_message *m)
  236. {
  237. struct spi_device *spi;
  238. unsigned cs_change;
  239. int status;
  240. struct spi_transfer *t;
  241. spi = m->spi;
  242. cs_change = 1;
  243. status = 0;
  244. list_for_each_entry(t, &m->transfers, transfer_list) {
  245. if (t->bits_per_word || t->speed_hz) {
  246. status = mpc512x_psc_spi_transfer_setup(spi, t);
  247. if (status < 0)
  248. break;
  249. }
  250. if (cs_change)
  251. mpc512x_psc_spi_activate_cs(spi);
  252. cs_change = t->cs_change;
  253. status = mpc512x_psc_spi_transfer_rxtx(spi, t);
  254. if (status)
  255. break;
  256. m->actual_length += t->len;
  257. if (t->delay_usecs)
  258. udelay(t->delay_usecs);
  259. if (cs_change)
  260. mpc512x_psc_spi_deactivate_cs(spi);
  261. }
  262. m->status = status;
  263. if (m->complete)
  264. m->complete(m->context);
  265. if (status || !cs_change)
  266. mpc512x_psc_spi_deactivate_cs(spi);
  267. mpc512x_psc_spi_transfer_setup(spi, NULL);
  268. spi_finalize_current_message(master);
  269. return status;
  270. }
  271. static int mpc512x_psc_spi_prep_xfer_hw(struct spi_master *master)
  272. {
  273. struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
  274. struct mpc52xx_psc __iomem *psc = mps->psc;
  275. dev_dbg(&master->dev, "%s()\n", __func__);
  276. /* Zero MR2 */
  277. in_8(&psc->mode);
  278. out_8(&psc->mode, 0x0);
  279. /* enable transmitter/receiver */
  280. out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
  281. return 0;
  282. }
  283. static int mpc512x_psc_spi_unprep_xfer_hw(struct spi_master *master)
  284. {
  285. struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
  286. struct mpc52xx_psc __iomem *psc = mps->psc;
  287. struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
  288. dev_dbg(&master->dev, "%s()\n", __func__);
  289. /* disable transmitter/receiver and fifo interrupt */
  290. out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  291. out_be32(&fifo->tximr, 0);
  292. return 0;
  293. }
  294. static int mpc512x_psc_spi_setup(struct spi_device *spi)
  295. {
  296. struct mpc512x_psc_spi_cs *cs = spi->controller_state;
  297. int ret;
  298. if (spi->bits_per_word % 8)
  299. return -EINVAL;
  300. if (!cs) {
  301. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  302. if (!cs)
  303. return -ENOMEM;
  304. if (gpio_is_valid(spi->cs_gpio)) {
  305. ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
  306. if (ret) {
  307. dev_err(&spi->dev, "can't get CS gpio: %d\n",
  308. ret);
  309. kfree(cs);
  310. return ret;
  311. }
  312. gpio_direction_output(spi->cs_gpio,
  313. spi->mode & SPI_CS_HIGH ? 0 : 1);
  314. }
  315. spi->controller_state = cs;
  316. }
  317. cs->bits_per_word = spi->bits_per_word;
  318. cs->speed_hz = spi->max_speed_hz;
  319. return 0;
  320. }
  321. static void mpc512x_psc_spi_cleanup(struct spi_device *spi)
  322. {
  323. if (gpio_is_valid(spi->cs_gpio))
  324. gpio_free(spi->cs_gpio);
  325. kfree(spi->controller_state);
  326. }
  327. static int mpc512x_psc_spi_port_config(struct spi_master *master,
  328. struct mpc512x_psc_spi *mps)
  329. {
  330. struct mpc52xx_psc __iomem *psc = mps->psc;
  331. struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
  332. u32 sicr;
  333. u32 ccr;
  334. int speed;
  335. u16 bclkdiv;
  336. /* Reset the PSC into a known state */
  337. out_8(&psc->command, MPC52xx_PSC_RST_RX);
  338. out_8(&psc->command, MPC52xx_PSC_RST_TX);
  339. out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  340. /* Disable psc interrupts all useful interrupts are in fifo */
  341. out_be16(&psc->isr_imr.imr, 0);
  342. /* Disable fifo interrupts, will be enabled later */
  343. out_be32(&fifo->tximr, 0);
  344. out_be32(&fifo->rximr, 0);
  345. /* Setup fifo slice address and size */
  346. /*out_be32(&fifo->txsz, 0x0fe00004);*/
  347. /*out_be32(&fifo->rxsz, 0x0ff00004);*/
  348. sicr = 0x01000000 | /* SIM = 0001 -- 8 bit */
  349. 0x00800000 | /* GenClk = 1 -- internal clk */
  350. 0x00008000 | /* SPI = 1 */
  351. 0x00004000 | /* MSTR = 1 -- SPI master */
  352. 0x00000800; /* UseEOF = 1 -- SS low until EOF */
  353. out_be32(&psc->sicr, sicr);
  354. ccr = in_be32(&psc->ccr);
  355. ccr &= 0xFF000000;
  356. speed = 1000000; /* default 1MHz */
  357. bclkdiv = (mps->mclk_rate / speed) - 1;
  358. ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
  359. out_be32(&psc->ccr, ccr);
  360. /* Set 2ms DTL delay */
  361. out_8(&psc->ctur, 0x00);
  362. out_8(&psc->ctlr, 0x82);
  363. /* we don't use the alarms */
  364. out_be32(&fifo->rxalarm, 0xfff);
  365. out_be32(&fifo->txalarm, 0);
  366. /* Enable FIFO slices for Rx/Tx */
  367. out_be32(&fifo->rxcmd,
  368. MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
  369. out_be32(&fifo->txcmd,
  370. MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
  371. mps->bits_per_word = 8;
  372. return 0;
  373. }
  374. static irqreturn_t mpc512x_psc_spi_isr(int irq, void *dev_id)
  375. {
  376. struct mpc512x_psc_spi *mps = (struct mpc512x_psc_spi *)dev_id;
  377. struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
  378. /* clear interrupt and wake up the rx/tx routine */
  379. if (in_be32(&fifo->txisr) &
  380. in_be32(&fifo->tximr) & MPC512x_PSC_FIFO_EMPTY) {
  381. out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
  382. out_be32(&fifo->tximr, 0);
  383. complete(&mps->txisrdone);
  384. return IRQ_HANDLED;
  385. }
  386. return IRQ_NONE;
  387. }
  388. static void mpc512x_spi_cs_control(struct spi_device *spi, bool onoff)
  389. {
  390. gpio_set_value(spi->cs_gpio, onoff);
  391. }
  392. static int mpc512x_psc_spi_do_probe(struct device *dev, u32 regaddr,
  393. u32 size, unsigned int irq)
  394. {
  395. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  396. struct mpc512x_psc_spi *mps;
  397. struct spi_master *master;
  398. int ret;
  399. void *tempp;
  400. struct clk *clk;
  401. master = spi_alloc_master(dev, sizeof *mps);
  402. if (master == NULL)
  403. return -ENOMEM;
  404. dev_set_drvdata(dev, master);
  405. mps = spi_master_get_devdata(master);
  406. mps->irq = irq;
  407. if (pdata == NULL) {
  408. mps->cs_control = mpc512x_spi_cs_control;
  409. } else {
  410. mps->cs_control = pdata->cs_control;
  411. master->bus_num = pdata->bus_num;
  412. master->num_chipselect = pdata->max_chipselect;
  413. }
  414. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  415. master->setup = mpc512x_psc_spi_setup;
  416. master->prepare_transfer_hardware = mpc512x_psc_spi_prep_xfer_hw;
  417. master->transfer_one_message = mpc512x_psc_spi_msg_xfer;
  418. master->unprepare_transfer_hardware = mpc512x_psc_spi_unprep_xfer_hw;
  419. master->cleanup = mpc512x_psc_spi_cleanup;
  420. master->dev.of_node = dev->of_node;
  421. tempp = devm_ioremap(dev, regaddr, size);
  422. if (!tempp) {
  423. dev_err(dev, "could not ioremap I/O port range\n");
  424. ret = -EFAULT;
  425. goto free_master;
  426. }
  427. mps->psc = tempp;
  428. mps->fifo =
  429. (struct mpc512x_psc_fifo *)(tempp + sizeof(struct mpc52xx_psc));
  430. ret = devm_request_irq(dev, mps->irq, mpc512x_psc_spi_isr, IRQF_SHARED,
  431. "mpc512x-psc-spi", mps);
  432. if (ret)
  433. goto free_master;
  434. init_completion(&mps->txisrdone);
  435. clk = devm_clk_get(dev, "mclk");
  436. if (IS_ERR(clk)) {
  437. ret = PTR_ERR(clk);
  438. goto free_master;
  439. }
  440. ret = clk_prepare_enable(clk);
  441. if (ret)
  442. goto free_master;
  443. mps->clk_mclk = clk;
  444. mps->mclk_rate = clk_get_rate(clk);
  445. clk = devm_clk_get(dev, "ipg");
  446. if (IS_ERR(clk)) {
  447. ret = PTR_ERR(clk);
  448. goto free_mclk_clock;
  449. }
  450. ret = clk_prepare_enable(clk);
  451. if (ret)
  452. goto free_mclk_clock;
  453. mps->clk_ipg = clk;
  454. ret = mpc512x_psc_spi_port_config(master, mps);
  455. if (ret < 0)
  456. goto free_ipg_clock;
  457. ret = devm_spi_register_master(dev, master);
  458. if (ret < 0)
  459. goto free_ipg_clock;
  460. return ret;
  461. free_ipg_clock:
  462. clk_disable_unprepare(mps->clk_ipg);
  463. free_mclk_clock:
  464. clk_disable_unprepare(mps->clk_mclk);
  465. free_master:
  466. spi_master_put(master);
  467. return ret;
  468. }
  469. static int mpc512x_psc_spi_do_remove(struct device *dev)
  470. {
  471. struct spi_master *master = dev_get_drvdata(dev);
  472. struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
  473. clk_disable_unprepare(mps->clk_mclk);
  474. clk_disable_unprepare(mps->clk_ipg);
  475. return 0;
  476. }
  477. static int mpc512x_psc_spi_of_probe(struct platform_device *op)
  478. {
  479. const u32 *regaddr_p;
  480. u64 regaddr64, size64;
  481. regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL);
  482. if (!regaddr_p) {
  483. dev_err(&op->dev, "Invalid PSC address\n");
  484. return -EINVAL;
  485. }
  486. regaddr64 = of_translate_address(op->dev.of_node, regaddr_p);
  487. return mpc512x_psc_spi_do_probe(&op->dev, (u32) regaddr64, (u32) size64,
  488. irq_of_parse_and_map(op->dev.of_node, 0));
  489. }
  490. static int mpc512x_psc_spi_of_remove(struct platform_device *op)
  491. {
  492. return mpc512x_psc_spi_do_remove(&op->dev);
  493. }
  494. static struct of_device_id mpc512x_psc_spi_of_match[] = {
  495. { .compatible = "fsl,mpc5121-psc-spi", },
  496. {},
  497. };
  498. MODULE_DEVICE_TABLE(of, mpc512x_psc_spi_of_match);
  499. static struct platform_driver mpc512x_psc_spi_of_driver = {
  500. .probe = mpc512x_psc_spi_of_probe,
  501. .remove = mpc512x_psc_spi_of_remove,
  502. .driver = {
  503. .name = "mpc512x-psc-spi",
  504. .owner = THIS_MODULE,
  505. .of_match_table = mpc512x_psc_spi_of_match,
  506. },
  507. };
  508. module_platform_driver(mpc512x_psc_spi_of_driver);
  509. MODULE_AUTHOR("John Rigby");
  510. MODULE_DESCRIPTION("MPC512x PSC SPI Driver");
  511. MODULE_LICENSE("GPL");