spi-mpc52xx-psc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MPC52xx PSC in SPI mode driver.
  4. *
  5. * Maintainer: Dragos Carp
  6. *
  7. * Copyright (C) 2006 TOPTICA Photonics AG.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/completion.h>
  17. #include <linux/io.h>
  18. #include <linux/delay.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/fsl_devices.h>
  21. #include <linux/slab.h>
  22. #include <linux/of_irq.h>
  23. #include <asm/mpc52xx.h>
  24. #include <asm/mpc52xx_psc.h>
  25. #define MCLK 20000000 /* PSC port MClk in hz */
  26. struct mpc52xx_psc_spi {
  27. /* fsl_spi_platform data */
  28. void (*cs_control)(struct spi_device *spi, bool on);
  29. u32 sysclk;
  30. /* driver internal data */
  31. struct mpc52xx_psc __iomem *psc;
  32. struct mpc52xx_psc_fifo __iomem *fifo;
  33. unsigned int irq;
  34. u8 bits_per_word;
  35. struct completion done;
  36. };
  37. /* controller state */
  38. struct mpc52xx_psc_spi_cs {
  39. int bits_per_word;
  40. int speed_hz;
  41. };
  42. /* set clock freq, clock ramp, bits per work
  43. * if t is NULL then reset the values to the default values
  44. */
  45. static int mpc52xx_psc_spi_transfer_setup(struct spi_device *spi,
  46. struct spi_transfer *t)
  47. {
  48. struct mpc52xx_psc_spi_cs *cs = spi->controller_state;
  49. cs->speed_hz = (t && t->speed_hz)
  50. ? t->speed_hz : spi->max_speed_hz;
  51. cs->bits_per_word = (t && t->bits_per_word)
  52. ? t->bits_per_word : spi->bits_per_word;
  53. cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8;
  54. return 0;
  55. }
  56. static void mpc52xx_psc_spi_activate_cs(struct spi_device *spi)
  57. {
  58. struct mpc52xx_psc_spi_cs *cs = spi->controller_state;
  59. struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
  60. struct mpc52xx_psc __iomem *psc = mps->psc;
  61. u32 sicr;
  62. u16 ccr;
  63. sicr = in_be32(&psc->sicr);
  64. /* Set clock phase and polarity */
  65. if (spi->mode & SPI_CPHA)
  66. sicr |= 0x00001000;
  67. else
  68. sicr &= ~0x00001000;
  69. if (spi->mode & SPI_CPOL)
  70. sicr |= 0x00002000;
  71. else
  72. sicr &= ~0x00002000;
  73. if (spi->mode & SPI_LSB_FIRST)
  74. sicr |= 0x10000000;
  75. else
  76. sicr &= ~0x10000000;
  77. out_be32(&psc->sicr, sicr);
  78. /* Set clock frequency and bits per word
  79. * Because psc->ccr is defined as 16bit register instead of 32bit
  80. * just set the lower byte of BitClkDiv
  81. */
  82. ccr = in_be16((u16 __iomem *)&psc->ccr);
  83. ccr &= 0xFF00;
  84. if (cs->speed_hz)
  85. ccr |= (MCLK / cs->speed_hz - 1) & 0xFF;
  86. else /* by default SPI Clk 1MHz */
  87. ccr |= (MCLK / 1000000 - 1) & 0xFF;
  88. out_be16((u16 __iomem *)&psc->ccr, ccr);
  89. mps->bits_per_word = cs->bits_per_word;
  90. if (mps->cs_control)
  91. mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 1 : 0);
  92. }
  93. static void mpc52xx_psc_spi_deactivate_cs(struct spi_device *spi)
  94. {
  95. struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
  96. if (mps->cs_control)
  97. mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 0 : 1);
  98. }
  99. #define MPC52xx_PSC_BUFSIZE (MPC52xx_PSC_RFNUM_MASK + 1)
  100. /* wake up when 80% fifo full */
  101. #define MPC52xx_PSC_RFALARM (MPC52xx_PSC_BUFSIZE * 20 / 100)
  102. static int mpc52xx_psc_spi_transfer_rxtx(struct spi_device *spi,
  103. struct spi_transfer *t)
  104. {
  105. struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
  106. struct mpc52xx_psc __iomem *psc = mps->psc;
  107. struct mpc52xx_psc_fifo __iomem *fifo = mps->fifo;
  108. unsigned rb = 0; /* number of bytes receieved */
  109. unsigned sb = 0; /* number of bytes sent */
  110. unsigned char *rx_buf = (unsigned char *)t->rx_buf;
  111. unsigned char *tx_buf = (unsigned char *)t->tx_buf;
  112. unsigned rfalarm;
  113. unsigned send_at_once = MPC52xx_PSC_BUFSIZE;
  114. unsigned recv_at_once;
  115. int last_block = 0;
  116. if (!t->tx_buf && !t->rx_buf && t->len)
  117. return -EINVAL;
  118. /* enable transmiter/receiver */
  119. out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
  120. while (rb < t->len) {
  121. if (t->len - rb > MPC52xx_PSC_BUFSIZE) {
  122. rfalarm = MPC52xx_PSC_RFALARM;
  123. last_block = 0;
  124. } else {
  125. send_at_once = t->len - sb;
  126. rfalarm = MPC52xx_PSC_BUFSIZE - (t->len - rb);
  127. last_block = 1;
  128. }
  129. dev_dbg(&spi->dev, "send %d bytes...\n", send_at_once);
  130. for (; send_at_once; sb++, send_at_once--) {
  131. /* set EOF flag before the last word is sent */
  132. if (send_at_once == 1 && last_block)
  133. out_8(&psc->ircr2, 0x01);
  134. if (tx_buf)
  135. out_8(&psc->mpc52xx_psc_buffer_8, tx_buf[sb]);
  136. else
  137. out_8(&psc->mpc52xx_psc_buffer_8, 0);
  138. }
  139. /* enable interrupts and wait for wake up
  140. * if just one byte is expected the Rx FIFO genererates no
  141. * FFULL interrupt, so activate the RxRDY interrupt
  142. */
  143. out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
  144. if (t->len - rb == 1) {
  145. out_8(&psc->mode, 0);
  146. } else {
  147. out_8(&psc->mode, MPC52xx_PSC_MODE_FFULL);
  148. out_be16(&fifo->rfalarm, rfalarm);
  149. }
  150. out_be16(&psc->mpc52xx_psc_imr, MPC52xx_PSC_IMR_RXRDY);
  151. wait_for_completion(&mps->done);
  152. recv_at_once = in_be16(&fifo->rfnum);
  153. dev_dbg(&spi->dev, "%d bytes received\n", recv_at_once);
  154. send_at_once = recv_at_once;
  155. if (rx_buf) {
  156. for (; recv_at_once; rb++, recv_at_once--)
  157. rx_buf[rb] = in_8(&psc->mpc52xx_psc_buffer_8);
  158. } else {
  159. for (; recv_at_once; rb++, recv_at_once--)
  160. in_8(&psc->mpc52xx_psc_buffer_8);
  161. }
  162. }
  163. /* disable transmiter/receiver */
  164. out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  165. return 0;
  166. }
  167. int mpc52xx_psc_spi_transfer_one_message(struct spi_controller *ctlr,
  168. struct spi_message *m)
  169. {
  170. struct spi_device *spi;
  171. struct spi_transfer *t = NULL;
  172. unsigned cs_change;
  173. int status;
  174. spi = m->spi;
  175. cs_change = 1;
  176. status = 0;
  177. list_for_each_entry (t, &m->transfers, transfer_list) {
  178. if (t->bits_per_word || t->speed_hz) {
  179. status = mpc52xx_psc_spi_transfer_setup(spi, t);
  180. if (status < 0)
  181. break;
  182. }
  183. if (cs_change)
  184. mpc52xx_psc_spi_activate_cs(spi);
  185. cs_change = t->cs_change;
  186. status = mpc52xx_psc_spi_transfer_rxtx(spi, t);
  187. if (status)
  188. break;
  189. m->actual_length += t->len;
  190. spi_transfer_delay_exec(t);
  191. if (cs_change)
  192. mpc52xx_psc_spi_deactivate_cs(spi);
  193. }
  194. m->status = status;
  195. if (status || !cs_change)
  196. mpc52xx_psc_spi_deactivate_cs(spi);
  197. mpc52xx_psc_spi_transfer_setup(spi, NULL);
  198. spi_finalize_current_message(ctlr);
  199. return 0;
  200. }
  201. static int mpc52xx_psc_spi_setup(struct spi_device *spi)
  202. {
  203. struct mpc52xx_psc_spi_cs *cs = spi->controller_state;
  204. if (spi->bits_per_word%8)
  205. return -EINVAL;
  206. if (!cs) {
  207. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  208. if (!cs)
  209. return -ENOMEM;
  210. spi->controller_state = cs;
  211. }
  212. cs->bits_per_word = spi->bits_per_word;
  213. cs->speed_hz = spi->max_speed_hz;
  214. return 0;
  215. }
  216. static void mpc52xx_psc_spi_cleanup(struct spi_device *spi)
  217. {
  218. kfree(spi->controller_state);
  219. }
  220. static int mpc52xx_psc_spi_port_config(int psc_id, struct mpc52xx_psc_spi *mps)
  221. {
  222. struct mpc52xx_psc __iomem *psc = mps->psc;
  223. struct mpc52xx_psc_fifo __iomem *fifo = mps->fifo;
  224. u32 mclken_div;
  225. int ret;
  226. /* default sysclk is 512MHz */
  227. mclken_div = (mps->sysclk ? mps->sysclk : 512000000) / MCLK;
  228. ret = mpc52xx_set_psc_clkdiv(psc_id, mclken_div);
  229. if (ret)
  230. return ret;
  231. /* Reset the PSC into a known state */
  232. out_8(&psc->command, MPC52xx_PSC_RST_RX);
  233. out_8(&psc->command, MPC52xx_PSC_RST_TX);
  234. out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
  235. /* Disable interrupts, interrupts are based on alarm level */
  236. out_be16(&psc->mpc52xx_psc_imr, 0);
  237. out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
  238. out_8(&fifo->rfcntl, 0);
  239. out_8(&psc->mode, MPC52xx_PSC_MODE_FFULL);
  240. /* Configure 8bit codec mode as a SPI master and use EOF flags */
  241. /* SICR_SIM_CODEC8|SICR_GENCLK|SICR_SPI|SICR_MSTR|SICR_USEEOF */
  242. out_be32(&psc->sicr, 0x0180C800);
  243. out_be16((u16 __iomem *)&psc->ccr, 0x070F); /* default SPI Clk 1MHz */
  244. /* Set 2ms DTL delay */
  245. out_8(&psc->ctur, 0x00);
  246. out_8(&psc->ctlr, 0x84);
  247. mps->bits_per_word = 8;
  248. return 0;
  249. }
  250. static irqreturn_t mpc52xx_psc_spi_isr(int irq, void *dev_id)
  251. {
  252. struct mpc52xx_psc_spi *mps = (struct mpc52xx_psc_spi *)dev_id;
  253. struct mpc52xx_psc __iomem *psc = mps->psc;
  254. /* disable interrupt and wake up the work queue */
  255. if (in_be16(&psc->mpc52xx_psc_isr) & MPC52xx_PSC_IMR_RXRDY) {
  256. out_be16(&psc->mpc52xx_psc_imr, 0);
  257. complete(&mps->done);
  258. return IRQ_HANDLED;
  259. }
  260. return IRQ_NONE;
  261. }
  262. /* bus_num is used only for the case dev->platform_data == NULL */
  263. static int mpc52xx_psc_spi_do_probe(struct device *dev, u32 regaddr,
  264. u32 size, unsigned int irq, s16 bus_num)
  265. {
  266. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  267. struct mpc52xx_psc_spi *mps;
  268. struct spi_master *master;
  269. int ret;
  270. master = spi_alloc_master(dev, sizeof(*mps));
  271. if (master == NULL)
  272. return -ENOMEM;
  273. dev_set_drvdata(dev, master);
  274. mps = spi_master_get_devdata(master);
  275. /* the spi->mode bits understood by this driver: */
  276. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  277. mps->irq = irq;
  278. if (pdata == NULL) {
  279. dev_warn(dev,
  280. "probe called without platform data, no cs_control function will be called\n");
  281. mps->cs_control = NULL;
  282. mps->sysclk = 0;
  283. master->bus_num = bus_num;
  284. master->num_chipselect = 255;
  285. } else {
  286. mps->cs_control = pdata->cs_control;
  287. mps->sysclk = pdata->sysclk;
  288. master->bus_num = pdata->bus_num;
  289. master->num_chipselect = pdata->max_chipselect;
  290. }
  291. master->setup = mpc52xx_psc_spi_setup;
  292. master->transfer_one_message = mpc52xx_psc_spi_transfer_one_message;
  293. master->cleanup = mpc52xx_psc_spi_cleanup;
  294. master->dev.of_node = dev->of_node;
  295. mps->psc = ioremap(regaddr, size);
  296. if (!mps->psc) {
  297. dev_err(dev, "could not ioremap I/O port range\n");
  298. ret = -EFAULT;
  299. goto free_master;
  300. }
  301. /* On the 5200, fifo regs are immediately ajacent to the psc regs */
  302. mps->fifo = ((void __iomem *)mps->psc) + sizeof(struct mpc52xx_psc);
  303. ret = request_irq(mps->irq, mpc52xx_psc_spi_isr, 0, "mpc52xx-psc-spi",
  304. mps);
  305. if (ret)
  306. goto free_master;
  307. ret = mpc52xx_psc_spi_port_config(master->bus_num, mps);
  308. if (ret < 0) {
  309. dev_err(dev, "can't configure PSC! Is it capable of SPI?\n");
  310. goto free_irq;
  311. }
  312. init_completion(&mps->done);
  313. ret = spi_register_master(master);
  314. if (ret < 0)
  315. goto free_irq;
  316. return ret;
  317. free_irq:
  318. free_irq(mps->irq, mps);
  319. free_master:
  320. if (mps->psc)
  321. iounmap(mps->psc);
  322. spi_master_put(master);
  323. return ret;
  324. }
  325. static int mpc52xx_psc_spi_of_probe(struct platform_device *op)
  326. {
  327. const u32 *regaddr_p;
  328. u64 regaddr64, size64;
  329. s16 id = -1;
  330. regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL);
  331. if (!regaddr_p) {
  332. dev_err(&op->dev, "Invalid PSC address\n");
  333. return -EINVAL;
  334. }
  335. regaddr64 = of_translate_address(op->dev.of_node, regaddr_p);
  336. /* get PSC id (1..6, used by port_config) */
  337. if (op->dev.platform_data == NULL) {
  338. const u32 *psc_nump;
  339. psc_nump = of_get_property(op->dev.of_node, "cell-index", NULL);
  340. if (!psc_nump || *psc_nump > 5) {
  341. dev_err(&op->dev, "Invalid cell-index property\n");
  342. return -EINVAL;
  343. }
  344. id = *psc_nump + 1;
  345. }
  346. return mpc52xx_psc_spi_do_probe(&op->dev, (u32)regaddr64, (u32)size64,
  347. irq_of_parse_and_map(op->dev.of_node, 0), id);
  348. }
  349. static int mpc52xx_psc_spi_of_remove(struct platform_device *op)
  350. {
  351. struct spi_master *master = spi_master_get(platform_get_drvdata(op));
  352. struct mpc52xx_psc_spi *mps = spi_master_get_devdata(master);
  353. spi_unregister_master(master);
  354. free_irq(mps->irq, mps);
  355. if (mps->psc)
  356. iounmap(mps->psc);
  357. spi_master_put(master);
  358. return 0;
  359. }
  360. static const struct of_device_id mpc52xx_psc_spi_of_match[] = {
  361. { .compatible = "fsl,mpc5200-psc-spi", },
  362. { .compatible = "mpc5200-psc-spi", }, /* old */
  363. {}
  364. };
  365. MODULE_DEVICE_TABLE(of, mpc52xx_psc_spi_of_match);
  366. static struct platform_driver mpc52xx_psc_spi_of_driver = {
  367. .probe = mpc52xx_psc_spi_of_probe,
  368. .remove = mpc52xx_psc_spi_of_remove,
  369. .driver = {
  370. .name = "mpc52xx-psc-spi",
  371. .of_match_table = mpc52xx_psc_spi_of_match,
  372. },
  373. };
  374. module_platform_driver(mpc52xx_psc_spi_of_driver);
  375. MODULE_AUTHOR("Dragos Carp");
  376. MODULE_DESCRIPTION("MPC52xx PSC SPI Driver");
  377. MODULE_LICENSE("GPL");