spi-mpc512x-psc.c 15 KB

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