spi-xilinx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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_MAX_CS 32
  23. #define XILINX_SPI_NAME "xilinx_spi"
  24. /* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
  25. * Product Specification", DS464
  26. */
  27. #define XSPI_CR_OFFSET 0x60 /* Control Register */
  28. #define XSPI_CR_LOOP 0x01
  29. #define XSPI_CR_ENABLE 0x02
  30. #define XSPI_CR_MASTER_MODE 0x04
  31. #define XSPI_CR_CPOL 0x08
  32. #define XSPI_CR_CPHA 0x10
  33. #define XSPI_CR_MODE_MASK (XSPI_CR_CPHA | XSPI_CR_CPOL | \
  34. XSPI_CR_LSB_FIRST | XSPI_CR_LOOP)
  35. #define XSPI_CR_TXFIFO_RESET 0x20
  36. #define XSPI_CR_RXFIFO_RESET 0x40
  37. #define XSPI_CR_MANUAL_SSELECT 0x80
  38. #define XSPI_CR_TRANS_INHIBIT 0x100
  39. #define XSPI_CR_LSB_FIRST 0x200
  40. #define XSPI_SR_OFFSET 0x64 /* Status Register */
  41. #define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */
  42. #define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */
  43. #define XSPI_SR_TX_EMPTY_MASK 0x04 /* Transmit FIFO is empty */
  44. #define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */
  45. #define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */
  46. #define XSPI_TXD_OFFSET 0x68 /* Data Transmit Register */
  47. #define XSPI_RXD_OFFSET 0x6c /* Data Receive Register */
  48. #define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */
  49. /* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414
  50. * IPIF registers are 32 bit
  51. */
  52. #define XIPIF_V123B_DGIER_OFFSET 0x1c /* IPIF global int enable reg */
  53. #define XIPIF_V123B_GINTR_ENABLE 0x80000000
  54. #define XIPIF_V123B_IISR_OFFSET 0x20 /* IPIF interrupt status reg */
  55. #define XIPIF_V123B_IIER_OFFSET 0x28 /* IPIF interrupt enable reg */
  56. #define XSPI_INTR_MODE_FAULT 0x01 /* Mode fault error */
  57. #define XSPI_INTR_SLAVE_MODE_FAULT 0x02 /* Selected as slave while
  58. * disabled */
  59. #define XSPI_INTR_TX_EMPTY 0x04 /* TxFIFO is empty */
  60. #define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */
  61. #define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */
  62. #define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */
  63. #define XSPI_INTR_TX_HALF_EMPTY 0x40 /* TxFIFO is half empty */
  64. #define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */
  65. #define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */
  66. struct xilinx_spi {
  67. /* bitbang has to be first */
  68. struct spi_bitbang bitbang;
  69. struct completion done;
  70. void __iomem *regs; /* virt. address of the control registers */
  71. int irq;
  72. u8 *rx_ptr; /* pointer in the Tx buffer */
  73. const u8 *tx_ptr; /* pointer in the Rx buffer */
  74. u8 bytes_per_word;
  75. int buffer_size; /* buffer size in words */
  76. u32 cs_inactive; /* Level of the CS pins when inactive*/
  77. unsigned int (*read_fn)(void __iomem *);
  78. void (*write_fn)(u32, void __iomem *);
  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 xilinx_spi_tx(struct xilinx_spi *xspi)
  97. {
  98. u32 data = 0;
  99. if (!xspi->tx_ptr) {
  100. xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
  101. return;
  102. }
  103. switch (xspi->bytes_per_word) {
  104. case 1:
  105. data = *(u8 *)(xspi->tx_ptr);
  106. break;
  107. case 2:
  108. data = *(u16 *)(xspi->tx_ptr);
  109. break;
  110. case 4:
  111. data = *(u32 *)(xspi->tx_ptr);
  112. break;
  113. }
  114. xspi->write_fn(data, xspi->regs + XSPI_TXD_OFFSET);
  115. xspi->tx_ptr += xspi->bytes_per_word;
  116. }
  117. static void xilinx_spi_rx(struct xilinx_spi *xspi)
  118. {
  119. u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
  120. if (!xspi->rx_ptr)
  121. return;
  122. switch (xspi->bytes_per_word) {
  123. case 1:
  124. *(u8 *)(xspi->rx_ptr) = data;
  125. break;
  126. case 2:
  127. *(u16 *)(xspi->rx_ptr) = data;
  128. break;
  129. case 4:
  130. *(u32 *)(xspi->rx_ptr) = data;
  131. break;
  132. }
  133. xspi->rx_ptr += xspi->bytes_per_word;
  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. /* Enable the transmit empty interrupt, which we use to determine
  142. * progress on the transmission.
  143. */
  144. xspi->write_fn(XSPI_INTR_TX_EMPTY,
  145. regs_base + XIPIF_V123B_IIER_OFFSET);
  146. /* Disable the global IPIF interrupt */
  147. xspi->write_fn(0, regs_base + XIPIF_V123B_DGIER_OFFSET);
  148. /* Deselect the slave on the SPI bus */
  149. xspi->write_fn(0xffff, regs_base + XSPI_SSR_OFFSET);
  150. /* Disable the transmitter, enable Manual Slave Select Assertion,
  151. * put SPI controller into master mode, and enable it */
  152. xspi->write_fn(XSPI_CR_MANUAL_SSELECT | XSPI_CR_MASTER_MODE |
  153. XSPI_CR_ENABLE | XSPI_CR_TXFIFO_RESET | XSPI_CR_RXFIFO_RESET,
  154. regs_base + XSPI_CR_OFFSET);
  155. }
  156. static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
  157. {
  158. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  159. u16 cr;
  160. u32 cs;
  161. if (is_on == BITBANG_CS_INACTIVE) {
  162. /* Deselect the slave on the SPI bus */
  163. xspi->write_fn(xspi->cs_inactive, xspi->regs + XSPI_SSR_OFFSET);
  164. return;
  165. }
  166. /* Set the SPI clock phase and polarity */
  167. cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET) & ~XSPI_CR_MODE_MASK;
  168. if (spi->mode & SPI_CPHA)
  169. cr |= XSPI_CR_CPHA;
  170. if (spi->mode & SPI_CPOL)
  171. cr |= XSPI_CR_CPOL;
  172. if (spi->mode & SPI_LSB_FIRST)
  173. cr |= XSPI_CR_LSB_FIRST;
  174. if (spi->mode & SPI_LOOP)
  175. cr |= XSPI_CR_LOOP;
  176. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  177. /* We do not check spi->max_speed_hz here as the SPI clock
  178. * frequency is not software programmable (the IP block design
  179. * parameter)
  180. */
  181. cs = xspi->cs_inactive;
  182. cs ^= BIT(spi->chip_select);
  183. /* Activate the chip select */
  184. xspi->write_fn(cs, xspi->regs + XSPI_SSR_OFFSET);
  185. }
  186. /* spi_bitbang requires custom setup_transfer() to be defined if there is a
  187. * custom txrx_bufs().
  188. */
  189. static int xilinx_spi_setup_transfer(struct spi_device *spi,
  190. struct spi_transfer *t)
  191. {
  192. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  193. if (spi->mode & SPI_CS_HIGH)
  194. xspi->cs_inactive &= ~BIT(spi->chip_select);
  195. else
  196. xspi->cs_inactive |= BIT(spi->chip_select);
  197. return 0;
  198. }
  199. static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  200. {
  201. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  202. int remaining_words; /* the number of words left to transfer */
  203. bool use_irq = false;
  204. u16 cr = 0;
  205. /* We get here with transmitter inhibited */
  206. xspi->tx_ptr = t->tx_buf;
  207. xspi->rx_ptr = t->rx_buf;
  208. remaining_words = t->len / xspi->bytes_per_word;
  209. if (xspi->irq >= 0 && remaining_words > xspi->buffer_size) {
  210. u32 isr;
  211. use_irq = true;
  212. /* Inhibit irq to avoid spurious irqs on tx_empty*/
  213. cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET);
  214. xspi->write_fn(cr | XSPI_CR_TRANS_INHIBIT,
  215. xspi->regs + XSPI_CR_OFFSET);
  216. /* ACK old irqs (if any) */
  217. isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET);
  218. if (isr)
  219. xspi->write_fn(isr,
  220. xspi->regs + XIPIF_V123B_IISR_OFFSET);
  221. /* Enable the global IPIF interrupt */
  222. xspi->write_fn(XIPIF_V123B_GINTR_ENABLE,
  223. xspi->regs + XIPIF_V123B_DGIER_OFFSET);
  224. reinit_completion(&xspi->done);
  225. }
  226. while (remaining_words) {
  227. int n_words, tx_words, rx_words;
  228. u32 sr;
  229. int stalled;
  230. n_words = min(remaining_words, xspi->buffer_size);
  231. tx_words = n_words;
  232. while (tx_words--)
  233. xilinx_spi_tx(xspi);
  234. /* Start the transfer by not inhibiting the transmitter any
  235. * longer
  236. */
  237. if (use_irq) {
  238. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  239. wait_for_completion(&xspi->done);
  240. /* A transmit has just completed. Process received data
  241. * and check for more data to transmit. Always inhibit
  242. * the transmitter while the Isr refills the transmit
  243. * register/FIFO, or make sure it is stopped if we're
  244. * done.
  245. */
  246. xspi->write_fn(cr | XSPI_CR_TRANS_INHIBIT,
  247. xspi->regs + XSPI_CR_OFFSET);
  248. sr = XSPI_SR_TX_EMPTY_MASK;
  249. } else
  250. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  251. /* Read out all the data from the Rx FIFO */
  252. rx_words = n_words;
  253. stalled = 10;
  254. while (rx_words) {
  255. if (rx_words == n_words && !(stalled--) &&
  256. !(sr & XSPI_SR_TX_EMPTY_MASK) &&
  257. (sr & XSPI_SR_RX_EMPTY_MASK)) {
  258. dev_err(&spi->dev,
  259. "Detected stall. Check C_SPI_MODE and C_SPI_MEMORY\n");
  260. xspi_init_hw(xspi);
  261. return -EIO;
  262. }
  263. if ((sr & XSPI_SR_TX_EMPTY_MASK) && (rx_words > 1)) {
  264. xilinx_spi_rx(xspi);
  265. rx_words--;
  266. continue;
  267. }
  268. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  269. if (!(sr & XSPI_SR_RX_EMPTY_MASK)) {
  270. xilinx_spi_rx(xspi);
  271. rx_words--;
  272. }
  273. }
  274. remaining_words -= n_words;
  275. }
  276. if (use_irq) {
  277. xspi->write_fn(0, xspi->regs + XIPIF_V123B_DGIER_OFFSET);
  278. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  279. }
  280. return t->len;
  281. }
  282. /* This driver supports single master mode only. Hence Tx FIFO Empty
  283. * is the only interrupt we care about.
  284. * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
  285. * Fault are not to happen.
  286. */
  287. static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
  288. {
  289. struct xilinx_spi *xspi = dev_id;
  290. u32 ipif_isr;
  291. /* Get the IPIF interrupts, and clear them immediately */
  292. ipif_isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET);
  293. xspi->write_fn(ipif_isr, xspi->regs + XIPIF_V123B_IISR_OFFSET);
  294. if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */
  295. complete(&xspi->done);
  296. return IRQ_HANDLED;
  297. }
  298. return IRQ_NONE;
  299. }
  300. static int xilinx_spi_find_buffer_size(struct xilinx_spi *xspi)
  301. {
  302. u8 sr;
  303. int n_words = 0;
  304. /*
  305. * Before the buffer_size detection we reset the core
  306. * to make sure we start with a clean state.
  307. */
  308. xspi->write_fn(XIPIF_V123B_RESET_MASK,
  309. xspi->regs + XIPIF_V123B_RESETR_OFFSET);
  310. /* Fill the Tx FIFO with as many words as possible */
  311. do {
  312. xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
  313. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  314. n_words++;
  315. } while (!(sr & XSPI_SR_TX_FULL_MASK));
  316. return n_words;
  317. }
  318. static const struct of_device_id xilinx_spi_of_match[] = {
  319. { .compatible = "xlnx,axi-quad-spi-1.00.a", },
  320. { .compatible = "xlnx,xps-spi-2.00.a", },
  321. { .compatible = "xlnx,xps-spi-2.00.b", },
  322. {}
  323. };
  324. MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
  325. static int xilinx_spi_probe(struct platform_device *pdev)
  326. {
  327. struct xilinx_spi *xspi;
  328. struct xspi_platform_data *pdata;
  329. struct resource *res;
  330. int ret, num_cs = 0, bits_per_word = 8;
  331. struct spi_master *master;
  332. u32 tmp;
  333. u8 i;
  334. pdata = dev_get_platdata(&pdev->dev);
  335. if (pdata) {
  336. num_cs = pdata->num_chipselect;
  337. bits_per_word = pdata->bits_per_word;
  338. } else {
  339. of_property_read_u32(pdev->dev.of_node, "xlnx,num-ss-bits",
  340. &num_cs);
  341. }
  342. if (!num_cs) {
  343. dev_err(&pdev->dev,
  344. "Missing slave select configuration data\n");
  345. return -EINVAL;
  346. }
  347. if (num_cs > XILINX_SPI_MAX_CS) {
  348. dev_err(&pdev->dev, "Invalid number of spi slaves\n");
  349. return -EINVAL;
  350. }
  351. master = spi_alloc_master(&pdev->dev, sizeof(struct xilinx_spi));
  352. if (!master)
  353. return -ENODEV;
  354. /* the spi->mode bits understood by this driver: */
  355. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_LOOP |
  356. SPI_CS_HIGH;
  357. xspi = spi_master_get_devdata(master);
  358. xspi->cs_inactive = 0xffffffff;
  359. xspi->bitbang.master = master;
  360. xspi->bitbang.chipselect = xilinx_spi_chipselect;
  361. xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer;
  362. xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;
  363. init_completion(&xspi->done);
  364. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  365. xspi->regs = devm_ioremap_resource(&pdev->dev, res);
  366. if (IS_ERR(xspi->regs)) {
  367. ret = PTR_ERR(xspi->regs);
  368. goto put_master;
  369. }
  370. master->bus_num = pdev->id;
  371. master->num_chipselect = num_cs;
  372. master->dev.of_node = pdev->dev.of_node;
  373. /*
  374. * Detect endianess on the IP via loop bit in CR. Detection
  375. * must be done before reset is sent because incorrect reset
  376. * value generates error interrupt.
  377. * Setup little endian helper functions first and try to use them
  378. * and check if bit was correctly setup or not.
  379. */
  380. xspi->read_fn = xspi_read32;
  381. xspi->write_fn = xspi_write32;
  382. xspi->write_fn(XSPI_CR_LOOP, xspi->regs + XSPI_CR_OFFSET);
  383. tmp = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET);
  384. tmp &= XSPI_CR_LOOP;
  385. if (tmp != XSPI_CR_LOOP) {
  386. xspi->read_fn = xspi_read32_be;
  387. xspi->write_fn = xspi_write32_be;
  388. }
  389. master->bits_per_word_mask = SPI_BPW_MASK(bits_per_word);
  390. xspi->bytes_per_word = bits_per_word / 8;
  391. xspi->buffer_size = xilinx_spi_find_buffer_size(xspi);
  392. xspi->irq = platform_get_irq(pdev, 0);
  393. if (xspi->irq < 0 && xspi->irq != -ENXIO) {
  394. ret = xspi->irq;
  395. goto put_master;
  396. } else if (xspi->irq >= 0) {
  397. /* Register for SPI Interrupt */
  398. ret = devm_request_irq(&pdev->dev, xspi->irq, xilinx_spi_irq, 0,
  399. dev_name(&pdev->dev), xspi);
  400. if (ret)
  401. goto put_master;
  402. }
  403. /* SPI controller initializations */
  404. xspi_init_hw(xspi);
  405. ret = spi_bitbang_start(&xspi->bitbang);
  406. if (ret) {
  407. dev_err(&pdev->dev, "spi_bitbang_start FAILED\n");
  408. goto put_master;
  409. }
  410. dev_info(&pdev->dev, "at 0x%08llX mapped to 0x%p, irq=%d\n",
  411. (unsigned long long)res->start, xspi->regs, xspi->irq);
  412. if (pdata) {
  413. for (i = 0; i < pdata->num_devices; i++)
  414. spi_new_device(master, pdata->devices + i);
  415. }
  416. platform_set_drvdata(pdev, master);
  417. return 0;
  418. put_master:
  419. spi_master_put(master);
  420. return ret;
  421. }
  422. static int xilinx_spi_remove(struct platform_device *pdev)
  423. {
  424. struct spi_master *master = platform_get_drvdata(pdev);
  425. struct xilinx_spi *xspi = spi_master_get_devdata(master);
  426. void __iomem *regs_base = xspi->regs;
  427. spi_bitbang_stop(&xspi->bitbang);
  428. /* Disable all the interrupts just in case */
  429. xspi->write_fn(0, regs_base + XIPIF_V123B_IIER_OFFSET);
  430. /* Disable the global IPIF interrupt */
  431. xspi->write_fn(0, regs_base + XIPIF_V123B_DGIER_OFFSET);
  432. spi_master_put(xspi->bitbang.master);
  433. return 0;
  434. }
  435. /* work with hotplug and coldplug */
  436. MODULE_ALIAS("platform:" XILINX_SPI_NAME);
  437. static struct platform_driver xilinx_spi_driver = {
  438. .probe = xilinx_spi_probe,
  439. .remove = xilinx_spi_remove,
  440. .driver = {
  441. .name = XILINX_SPI_NAME,
  442. .of_match_table = xilinx_spi_of_match,
  443. },
  444. };
  445. module_platform_driver(xilinx_spi_driver);
  446. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  447. MODULE_DESCRIPTION("Xilinx SPI driver");
  448. MODULE_LICENSE("GPL");