spi-xilinx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Xilinx SPI controller driver (master mode only)
  3. *
  4. * Author: MontaVista Software, Inc.
  5. * source@mvista.com
  6. *
  7. * Copyright (c) 2010 Secret Lab Technologies, Ltd.
  8. * Copyright (c) 2009 Intel Corporation
  9. * 2002-2007 (c) MontaVista Software, Inc.
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/spi_bitbang.h>
  20. #include <linux/spi/xilinx_spi.h>
  21. #include <linux/io.h>
  22. #define XILINX_SPI_NAME "xilinx_spi"
  23. /* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
  24. * Product Specification", DS464
  25. */
  26. #define XSPI_CR_OFFSET 0x60 /* Control Register */
  27. #define XSPI_CR_LOOP 0x01
  28. #define XSPI_CR_ENABLE 0x02
  29. #define XSPI_CR_MASTER_MODE 0x04
  30. #define XSPI_CR_CPOL 0x08
  31. #define XSPI_CR_CPHA 0x10
  32. #define XSPI_CR_MODE_MASK (XSPI_CR_CPHA | XSPI_CR_CPOL | \
  33. XSPI_CR_LSB_FIRST | XSPI_CR_LOOP)
  34. #define XSPI_CR_TXFIFO_RESET 0x20
  35. #define XSPI_CR_RXFIFO_RESET 0x40
  36. #define XSPI_CR_MANUAL_SSELECT 0x80
  37. #define XSPI_CR_TRANS_INHIBIT 0x100
  38. #define XSPI_CR_LSB_FIRST 0x200
  39. #define XSPI_SR_OFFSET 0x64 /* Status Register */
  40. #define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */
  41. #define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */
  42. #define XSPI_SR_TX_EMPTY_MASK 0x04 /* Transmit FIFO is empty */
  43. #define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */
  44. #define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */
  45. #define XSPI_TXD_OFFSET 0x68 /* Data Transmit Register */
  46. #define XSPI_RXD_OFFSET 0x6c /* Data Receive Register */
  47. #define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */
  48. /* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414
  49. * IPIF registers are 32 bit
  50. */
  51. #define XIPIF_V123B_DGIER_OFFSET 0x1c /* IPIF global int enable reg */
  52. #define XIPIF_V123B_GINTR_ENABLE 0x80000000
  53. #define XIPIF_V123B_IISR_OFFSET 0x20 /* IPIF interrupt status reg */
  54. #define XIPIF_V123B_IIER_OFFSET 0x28 /* IPIF interrupt enable reg */
  55. #define XSPI_INTR_MODE_FAULT 0x01 /* Mode fault error */
  56. #define XSPI_INTR_SLAVE_MODE_FAULT 0x02 /* Selected as slave while
  57. * disabled */
  58. #define XSPI_INTR_TX_EMPTY 0x04 /* TxFIFO is empty */
  59. #define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */
  60. #define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */
  61. #define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */
  62. #define XSPI_INTR_TX_HALF_EMPTY 0x40 /* TxFIFO is half empty */
  63. #define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */
  64. #define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */
  65. struct xilinx_spi {
  66. /* bitbang has to be first */
  67. struct spi_bitbang bitbang;
  68. struct completion done;
  69. void __iomem *regs; /* virt. address of the control registers */
  70. int irq;
  71. u8 *rx_ptr; /* pointer in the Tx buffer */
  72. const u8 *tx_ptr; /* pointer in the Rx buffer */
  73. int remaining_bytes; /* the number of bytes left to transfer */
  74. u8 bits_per_word;
  75. unsigned int (*read_fn)(void __iomem *);
  76. void (*write_fn)(u32, void __iomem *);
  77. void (*tx_fn)(struct xilinx_spi *);
  78. void (*rx_fn)(struct xilinx_spi *);
  79. };
  80. static void xspi_write32(u32 val, void __iomem *addr)
  81. {
  82. iowrite32(val, addr);
  83. }
  84. static unsigned int xspi_read32(void __iomem *addr)
  85. {
  86. return ioread32(addr);
  87. }
  88. static void xspi_write32_be(u32 val, void __iomem *addr)
  89. {
  90. iowrite32be(val, addr);
  91. }
  92. static unsigned int xspi_read32_be(void __iomem *addr)
  93. {
  94. return ioread32be(addr);
  95. }
  96. static void xspi_tx8(struct xilinx_spi *xspi)
  97. {
  98. xspi->write_fn(*xspi->tx_ptr, xspi->regs + XSPI_TXD_OFFSET);
  99. xspi->tx_ptr++;
  100. }
  101. static void xspi_tx16(struct xilinx_spi *xspi)
  102. {
  103. xspi->write_fn(*(u16 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET);
  104. xspi->tx_ptr += 2;
  105. }
  106. static void xspi_tx32(struct xilinx_spi *xspi)
  107. {
  108. xspi->write_fn(*(u32 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET);
  109. xspi->tx_ptr += 4;
  110. }
  111. static void xspi_rx8(struct xilinx_spi *xspi)
  112. {
  113. u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
  114. if (xspi->rx_ptr) {
  115. *xspi->rx_ptr = data & 0xff;
  116. xspi->rx_ptr++;
  117. }
  118. }
  119. static void xspi_rx16(struct xilinx_spi *xspi)
  120. {
  121. u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
  122. if (xspi->rx_ptr) {
  123. *(u16 *)(xspi->rx_ptr) = data & 0xffff;
  124. xspi->rx_ptr += 2;
  125. }
  126. }
  127. static void xspi_rx32(struct xilinx_spi *xspi)
  128. {
  129. u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
  130. if (xspi->rx_ptr) {
  131. *(u32 *)(xspi->rx_ptr) = data;
  132. xspi->rx_ptr += 4;
  133. }
  134. }
  135. static void xspi_init_hw(struct xilinx_spi *xspi)
  136. {
  137. void __iomem *regs_base = xspi->regs;
  138. /* Reset the SPI device */
  139. xspi->write_fn(XIPIF_V123B_RESET_MASK,
  140. regs_base + XIPIF_V123B_RESETR_OFFSET);
  141. /* Disable all the interrupts just in case */
  142. xspi->write_fn(0, regs_base + XIPIF_V123B_IIER_OFFSET);
  143. /* Enable the global IPIF interrupt */
  144. xspi->write_fn(XIPIF_V123B_GINTR_ENABLE,
  145. regs_base + XIPIF_V123B_DGIER_OFFSET);
  146. /* Deselect the slave on the SPI bus */
  147. xspi->write_fn(0xffff, regs_base + XSPI_SSR_OFFSET);
  148. /* Disable the transmitter, enable Manual Slave Select Assertion,
  149. * put SPI controller into master mode, and enable it */
  150. xspi->write_fn(XSPI_CR_TRANS_INHIBIT | XSPI_CR_MANUAL_SSELECT |
  151. XSPI_CR_MASTER_MODE | XSPI_CR_ENABLE | XSPI_CR_TXFIFO_RESET |
  152. XSPI_CR_RXFIFO_RESET, regs_base + XSPI_CR_OFFSET);
  153. }
  154. static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
  155. {
  156. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  157. if (is_on == BITBANG_CS_INACTIVE) {
  158. /* Deselect the slave on the SPI bus */
  159. xspi->write_fn(0xffff, xspi->regs + XSPI_SSR_OFFSET);
  160. } else if (is_on == BITBANG_CS_ACTIVE) {
  161. /* Set the SPI clock phase and polarity */
  162. u16 cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET)
  163. & ~XSPI_CR_MODE_MASK;
  164. if (spi->mode & SPI_CPHA)
  165. cr |= XSPI_CR_CPHA;
  166. if (spi->mode & SPI_CPOL)
  167. cr |= XSPI_CR_CPOL;
  168. if (spi->mode & SPI_LSB_FIRST)
  169. cr |= XSPI_CR_LSB_FIRST;
  170. if (spi->mode & SPI_LOOP)
  171. cr |= XSPI_CR_LOOP;
  172. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  173. /* We do not check spi->max_speed_hz here as the SPI clock
  174. * frequency is not software programmable (the IP block design
  175. * parameter)
  176. */
  177. /* Activate the chip select */
  178. xspi->write_fn(~(0x0001 << spi->chip_select),
  179. xspi->regs + XSPI_SSR_OFFSET);
  180. }
  181. }
  182. /* spi_bitbang requires custom setup_transfer() to be defined if there is a
  183. * custom txrx_bufs().
  184. */
  185. static int xilinx_spi_setup_transfer(struct spi_device *spi,
  186. struct spi_transfer *t)
  187. {
  188. return 0;
  189. }
  190. static int xilinx_spi_fill_tx_fifo(struct xilinx_spi *xspi)
  191. {
  192. u8 sr;
  193. int n_words = 0;
  194. /* Fill the Tx FIFO with as many bytes as possible */
  195. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  196. while ((sr & XSPI_SR_TX_FULL_MASK) == 0 && xspi->remaining_bytes > 0) {
  197. if (xspi->tx_ptr)
  198. xspi->tx_fn(xspi);
  199. else
  200. xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
  201. xspi->remaining_bytes -= xspi->bits_per_word / 8;
  202. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  203. n_words++;
  204. }
  205. return n_words;
  206. }
  207. static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  208. {
  209. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  210. u32 ipif_ier;
  211. /* We get here with transmitter inhibited */
  212. xspi->tx_ptr = t->tx_buf;
  213. xspi->rx_ptr = t->rx_buf;
  214. xspi->remaining_bytes = t->len;
  215. reinit_completion(&xspi->done);
  216. /* Enable the transmit empty interrupt, which we use to determine
  217. * progress on the transmission.
  218. */
  219. ipif_ier = xspi->read_fn(xspi->regs + XIPIF_V123B_IIER_OFFSET);
  220. xspi->write_fn(ipif_ier | XSPI_INTR_TX_EMPTY,
  221. xspi->regs + XIPIF_V123B_IIER_OFFSET);
  222. for (;;) {
  223. u16 cr;
  224. int n_words;
  225. n_words = xilinx_spi_fill_tx_fifo(xspi);
  226. /* Start the transfer by not inhibiting the transmitter any
  227. * longer
  228. */
  229. cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET) &
  230. ~XSPI_CR_TRANS_INHIBIT;
  231. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  232. wait_for_completion(&xspi->done);
  233. /* A transmit has just completed. Process received data and
  234. * check for more data to transmit. Always inhibit the
  235. * transmitter while the Isr refills the transmit register/FIFO,
  236. * or make sure it is stopped if we're done.
  237. */
  238. cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET);
  239. xspi->write_fn(cr | XSPI_CR_TRANS_INHIBIT,
  240. xspi->regs + XSPI_CR_OFFSET);
  241. /* Read out all the data from the Rx FIFO */
  242. while (n_words--)
  243. xspi->rx_fn(xspi);
  244. /* See if there is more data to send */
  245. if (xspi->remaining_bytes <= 0)
  246. break;
  247. }
  248. /* Disable the transmit empty interrupt */
  249. xspi->write_fn(ipif_ier, xspi->regs + XIPIF_V123B_IIER_OFFSET);
  250. return t->len - xspi->remaining_bytes;
  251. }
  252. /* This driver supports single master mode only. Hence Tx FIFO Empty
  253. * is the only interrupt we care about.
  254. * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
  255. * Fault are not to happen.
  256. */
  257. static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
  258. {
  259. struct xilinx_spi *xspi = dev_id;
  260. u32 ipif_isr;
  261. /* Get the IPIF interrupts, and clear them immediately */
  262. ipif_isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET);
  263. xspi->write_fn(ipif_isr, xspi->regs + XIPIF_V123B_IISR_OFFSET);
  264. if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */
  265. complete(&xspi->done);
  266. }
  267. return IRQ_HANDLED;
  268. }
  269. static const struct of_device_id xilinx_spi_of_match[] = {
  270. { .compatible = "xlnx,xps-spi-2.00.a", },
  271. { .compatible = "xlnx,xps-spi-2.00.b", },
  272. {}
  273. };
  274. MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
  275. static int xilinx_spi_probe(struct platform_device *pdev)
  276. {
  277. struct xilinx_spi *xspi;
  278. struct xspi_platform_data *pdata;
  279. struct resource *res;
  280. int ret, num_cs = 0, bits_per_word = 8;
  281. struct spi_master *master;
  282. u32 tmp;
  283. u8 i;
  284. pdata = dev_get_platdata(&pdev->dev);
  285. if (pdata) {
  286. num_cs = pdata->num_chipselect;
  287. bits_per_word = pdata->bits_per_word;
  288. } else {
  289. of_property_read_u32(pdev->dev.of_node, "xlnx,num-ss-bits",
  290. &num_cs);
  291. }
  292. if (!num_cs) {
  293. dev_err(&pdev->dev,
  294. "Missing slave select configuration data\n");
  295. return -EINVAL;
  296. }
  297. master = spi_alloc_master(&pdev->dev, sizeof(struct xilinx_spi));
  298. if (!master)
  299. return -ENODEV;
  300. /* the spi->mode bits understood by this driver: */
  301. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_LOOP;
  302. xspi = spi_master_get_devdata(master);
  303. xspi->bitbang.master = master;
  304. xspi->bitbang.chipselect = xilinx_spi_chipselect;
  305. xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer;
  306. xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;
  307. init_completion(&xspi->done);
  308. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  309. xspi->regs = devm_ioremap_resource(&pdev->dev, res);
  310. if (IS_ERR(xspi->regs)) {
  311. ret = PTR_ERR(xspi->regs);
  312. goto put_master;
  313. }
  314. master->bus_num = pdev->id;
  315. master->num_chipselect = num_cs;
  316. master->dev.of_node = pdev->dev.of_node;
  317. /*
  318. * Detect endianess on the IP via loop bit in CR. Detection
  319. * must be done before reset is sent because incorrect reset
  320. * value generates error interrupt.
  321. * Setup little endian helper functions first and try to use them
  322. * and check if bit was correctly setup or not.
  323. */
  324. xspi->read_fn = xspi_read32;
  325. xspi->write_fn = xspi_write32;
  326. xspi->write_fn(XSPI_CR_LOOP, xspi->regs + XSPI_CR_OFFSET);
  327. tmp = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET);
  328. tmp &= XSPI_CR_LOOP;
  329. if (tmp != XSPI_CR_LOOP) {
  330. xspi->read_fn = xspi_read32_be;
  331. xspi->write_fn = xspi_write32_be;
  332. }
  333. master->bits_per_word_mask = SPI_BPW_MASK(bits_per_word);
  334. xspi->bits_per_word = bits_per_word;
  335. if (xspi->bits_per_word == 8) {
  336. xspi->tx_fn = xspi_tx8;
  337. xspi->rx_fn = xspi_rx8;
  338. } else if (xspi->bits_per_word == 16) {
  339. xspi->tx_fn = xspi_tx16;
  340. xspi->rx_fn = xspi_rx16;
  341. } else if (xspi->bits_per_word == 32) {
  342. xspi->tx_fn = xspi_tx32;
  343. xspi->rx_fn = xspi_rx32;
  344. } else {
  345. ret = -EINVAL;
  346. goto put_master;
  347. }
  348. /* SPI controller initializations */
  349. xspi_init_hw(xspi);
  350. xspi->irq = platform_get_irq(pdev, 0);
  351. if (xspi->irq < 0) {
  352. ret = xspi->irq;
  353. goto put_master;
  354. }
  355. /* Register for SPI Interrupt */
  356. ret = devm_request_irq(&pdev->dev, xspi->irq, xilinx_spi_irq, 0,
  357. dev_name(&pdev->dev), xspi);
  358. if (ret)
  359. goto put_master;
  360. ret = spi_bitbang_start(&xspi->bitbang);
  361. if (ret) {
  362. dev_err(&pdev->dev, "spi_bitbang_start FAILED\n");
  363. goto put_master;
  364. }
  365. dev_info(&pdev->dev, "at 0x%08llX mapped to 0x%p, irq=%d\n",
  366. (unsigned long long)res->start, xspi->regs, xspi->irq);
  367. if (pdata) {
  368. for (i = 0; i < pdata->num_devices; i++)
  369. spi_new_device(master, pdata->devices + i);
  370. }
  371. platform_set_drvdata(pdev, master);
  372. return 0;
  373. put_master:
  374. spi_master_put(master);
  375. return ret;
  376. }
  377. static int xilinx_spi_remove(struct platform_device *pdev)
  378. {
  379. struct spi_master *master = platform_get_drvdata(pdev);
  380. struct xilinx_spi *xspi = spi_master_get_devdata(master);
  381. void __iomem *regs_base = xspi->regs;
  382. spi_bitbang_stop(&xspi->bitbang);
  383. /* Disable all the interrupts just in case */
  384. xspi->write_fn(0, regs_base + XIPIF_V123B_IIER_OFFSET);
  385. /* Disable the global IPIF interrupt */
  386. xspi->write_fn(0, regs_base + XIPIF_V123B_DGIER_OFFSET);
  387. spi_master_put(xspi->bitbang.master);
  388. return 0;
  389. }
  390. /* work with hotplug and coldplug */
  391. MODULE_ALIAS("platform:" XILINX_SPI_NAME);
  392. static struct platform_driver xilinx_spi_driver = {
  393. .probe = xilinx_spi_probe,
  394. .remove = xilinx_spi_remove,
  395. .driver = {
  396. .name = XILINX_SPI_NAME,
  397. .of_match_table = xilinx_spi_of_match,
  398. },
  399. };
  400. module_platform_driver(xilinx_spi_driver);
  401. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  402. MODULE_DESCRIPTION("Xilinx SPI driver");
  403. MODULE_LICENSE("GPL");