xilinx_spi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * xilinx_spi.c
  3. *
  4. * Xilinx SPI controller driver (master mode only)
  5. *
  6. * Author: MontaVista Software, Inc.
  7. * source@mvista.com
  8. *
  9. * 2002-2007 (c) MontaVista Software, Inc. This file is licensed under the
  10. * terms of the GNU General Public License version 2. This program is licensed
  11. * "as is" without any warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_spi.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/spi/spi_bitbang.h>
  22. #include <linux/io.h>
  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 0x62 /* 16-bit Control Register */
  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. #define XSPI_CR_TXFIFO_RESET 0x20
  34. #define XSPI_CR_RXFIFO_RESET 0x40
  35. #define XSPI_CR_MANUAL_SSELECT 0x80
  36. #define XSPI_CR_TRANS_INHIBIT 0x100
  37. #define XSPI_SR_OFFSET 0x67 /* 8-bit Status Register */
  38. #define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */
  39. #define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */
  40. #define XSPI_SR_TX_EMPTY_MASK 0x04 /* Transmit FIFO is empty */
  41. #define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */
  42. #define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */
  43. #define XSPI_TXD_OFFSET 0x6b /* 8-bit Data Transmit Register */
  44. #define XSPI_RXD_OFFSET 0x6f /* 8-bit Data Receive Register */
  45. #define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */
  46. /* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414
  47. * IPIF registers are 32 bit
  48. */
  49. #define XIPIF_V123B_DGIER_OFFSET 0x1c /* IPIF global int enable reg */
  50. #define XIPIF_V123B_GINTR_ENABLE 0x80000000
  51. #define XIPIF_V123B_IISR_OFFSET 0x20 /* IPIF interrupt status reg */
  52. #define XIPIF_V123B_IIER_OFFSET 0x28 /* IPIF interrupt enable reg */
  53. #define XSPI_INTR_MODE_FAULT 0x01 /* Mode fault error */
  54. #define XSPI_INTR_SLAVE_MODE_FAULT 0x02 /* Selected as slave while
  55. * disabled */
  56. #define XSPI_INTR_TX_EMPTY 0x04 /* TxFIFO is empty */
  57. #define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */
  58. #define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */
  59. #define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */
  60. #define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */
  61. #define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */
  62. struct xilinx_spi {
  63. /* bitbang has to be first */
  64. struct spi_bitbang bitbang;
  65. struct completion done;
  66. void __iomem *regs; /* virt. address of the control registers */
  67. u32 irq;
  68. u32 speed_hz; /* SCK has a fixed frequency of speed_hz Hz */
  69. u8 *rx_ptr; /* pointer in the Tx buffer */
  70. const u8 *tx_ptr; /* pointer in the Rx buffer */
  71. int remaining_bytes; /* the number of bytes left to transfer */
  72. };
  73. static void xspi_init_hw(void __iomem *regs_base)
  74. {
  75. /* Reset the SPI device */
  76. out_be32(regs_base + XIPIF_V123B_RESETR_OFFSET,
  77. XIPIF_V123B_RESET_MASK);
  78. /* Disable all the interrupts just in case */
  79. out_be32(regs_base + XIPIF_V123B_IIER_OFFSET, 0);
  80. /* Enable the global IPIF interrupt */
  81. out_be32(regs_base + XIPIF_V123B_DGIER_OFFSET,
  82. XIPIF_V123B_GINTR_ENABLE);
  83. /* Deselect the slave on the SPI bus */
  84. out_be32(regs_base + XSPI_SSR_OFFSET, 0xffff);
  85. /* Disable the transmitter, enable Manual Slave Select Assertion,
  86. * put SPI controller into master mode, and enable it */
  87. out_be16(regs_base + XSPI_CR_OFFSET,
  88. XSPI_CR_TRANS_INHIBIT | XSPI_CR_MANUAL_SSELECT
  89. | XSPI_CR_MASTER_MODE | XSPI_CR_ENABLE);
  90. }
  91. static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
  92. {
  93. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  94. if (is_on == BITBANG_CS_INACTIVE) {
  95. /* Deselect the slave on the SPI bus */
  96. out_be32(xspi->regs + XSPI_SSR_OFFSET, 0xffff);
  97. } else if (is_on == BITBANG_CS_ACTIVE) {
  98. /* Set the SPI clock phase and polarity */
  99. u16 cr = in_be16(xspi->regs + XSPI_CR_OFFSET)
  100. & ~XSPI_CR_MODE_MASK;
  101. if (spi->mode & SPI_CPHA)
  102. cr |= XSPI_CR_CPHA;
  103. if (spi->mode & SPI_CPOL)
  104. cr |= XSPI_CR_CPOL;
  105. out_be16(xspi->regs + XSPI_CR_OFFSET, cr);
  106. /* We do not check spi->max_speed_hz here as the SPI clock
  107. * frequency is not software programmable (the IP block design
  108. * parameter)
  109. */
  110. /* Activate the chip select */
  111. out_be32(xspi->regs + XSPI_SSR_OFFSET,
  112. ~(0x0001 << spi->chip_select));
  113. }
  114. }
  115. /* spi_bitbang requires custom setup_transfer() to be defined if there is a
  116. * custom txrx_bufs(). We have nothing to setup here as the SPI IP block
  117. * supports just 8 bits per word, and SPI clock can't be changed in software.
  118. * Check for 8 bits per word. Chip select delay calculations could be
  119. * added here as soon as bitbang_work() can be made aware of the delay value.
  120. */
  121. static int xilinx_spi_setup_transfer(struct spi_device *spi,
  122. struct spi_transfer *t)
  123. {
  124. u8 bits_per_word;
  125. bits_per_word = (t && t->bits_per_word)
  126. ? t->bits_per_word : spi->bits_per_word;
  127. if (bits_per_word != 8) {
  128. dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
  129. __func__, bits_per_word);
  130. return -EINVAL;
  131. }
  132. return 0;
  133. }
  134. static int xilinx_spi_setup(struct spi_device *spi)
  135. {
  136. struct spi_bitbang *bitbang;
  137. struct xilinx_spi *xspi;
  138. int retval;
  139. xspi = spi_master_get_devdata(spi->master);
  140. bitbang = &xspi->bitbang;
  141. retval = xilinx_spi_setup_transfer(spi, NULL);
  142. if (retval < 0)
  143. return retval;
  144. return 0;
  145. }
  146. static void xilinx_spi_fill_tx_fifo(struct xilinx_spi *xspi)
  147. {
  148. u8 sr;
  149. /* Fill the Tx FIFO with as many bytes as possible */
  150. sr = in_8(xspi->regs + XSPI_SR_OFFSET);
  151. while ((sr & XSPI_SR_TX_FULL_MASK) == 0 && xspi->remaining_bytes > 0) {
  152. if (xspi->tx_ptr) {
  153. out_8(xspi->regs + XSPI_TXD_OFFSET, *xspi->tx_ptr++);
  154. } else {
  155. out_8(xspi->regs + XSPI_TXD_OFFSET, 0);
  156. }
  157. xspi->remaining_bytes--;
  158. sr = in_8(xspi->regs + XSPI_SR_OFFSET);
  159. }
  160. }
  161. static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  162. {
  163. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  164. u32 ipif_ier;
  165. u16 cr;
  166. /* We get here with transmitter inhibited */
  167. xspi->tx_ptr = t->tx_buf;
  168. xspi->rx_ptr = t->rx_buf;
  169. xspi->remaining_bytes = t->len;
  170. INIT_COMPLETION(xspi->done);
  171. xilinx_spi_fill_tx_fifo(xspi);
  172. /* Enable the transmit empty interrupt, which we use to determine
  173. * progress on the transmission.
  174. */
  175. ipif_ier = in_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET);
  176. out_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET,
  177. ipif_ier | XSPI_INTR_TX_EMPTY);
  178. /* Start the transfer by not inhibiting the transmitter any longer */
  179. cr = in_be16(xspi->regs + XSPI_CR_OFFSET) & ~XSPI_CR_TRANS_INHIBIT;
  180. out_be16(xspi->regs + XSPI_CR_OFFSET, cr);
  181. wait_for_completion(&xspi->done);
  182. /* Disable the transmit empty interrupt */
  183. out_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET, ipif_ier);
  184. return t->len - xspi->remaining_bytes;
  185. }
  186. /* This driver supports single master mode only. Hence Tx FIFO Empty
  187. * is the only interrupt we care about.
  188. * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
  189. * Fault are not to happen.
  190. */
  191. static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
  192. {
  193. struct xilinx_spi *xspi = dev_id;
  194. u32 ipif_isr;
  195. /* Get the IPIF interrupts, and clear them immediately */
  196. ipif_isr = in_be32(xspi->regs + XIPIF_V123B_IISR_OFFSET);
  197. out_be32(xspi->regs + XIPIF_V123B_IISR_OFFSET, ipif_isr);
  198. if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */
  199. u16 cr;
  200. u8 sr;
  201. /* A transmit has just completed. Process received data and
  202. * check for more data to transmit. Always inhibit the
  203. * transmitter while the Isr refills the transmit register/FIFO,
  204. * or make sure it is stopped if we're done.
  205. */
  206. cr = in_be16(xspi->regs + XSPI_CR_OFFSET);
  207. out_be16(xspi->regs + XSPI_CR_OFFSET,
  208. cr | XSPI_CR_TRANS_INHIBIT);
  209. /* Read out all the data from the Rx FIFO */
  210. sr = in_8(xspi->regs + XSPI_SR_OFFSET);
  211. while ((sr & XSPI_SR_RX_EMPTY_MASK) == 0) {
  212. u8 data;
  213. data = in_8(xspi->regs + XSPI_RXD_OFFSET);
  214. if (xspi->rx_ptr) {
  215. *xspi->rx_ptr++ = data;
  216. }
  217. sr = in_8(xspi->regs + XSPI_SR_OFFSET);
  218. }
  219. /* See if there is more data to send */
  220. if (xspi->remaining_bytes > 0) {
  221. xilinx_spi_fill_tx_fifo(xspi);
  222. /* Start the transfer by not inhibiting the
  223. * transmitter any longer
  224. */
  225. out_be16(xspi->regs + XSPI_CR_OFFSET, cr);
  226. } else {
  227. /* No more data to send.
  228. * Indicate the transfer is completed.
  229. */
  230. complete(&xspi->done);
  231. }
  232. }
  233. return IRQ_HANDLED;
  234. }
  235. static int __init xilinx_spi_of_probe(struct of_device *ofdev,
  236. const struct of_device_id *match)
  237. {
  238. struct spi_master *master;
  239. struct xilinx_spi *xspi;
  240. struct resource r_irq_struct;
  241. struct resource r_mem_struct;
  242. struct resource *r_irq = &r_irq_struct;
  243. struct resource *r_mem = &r_mem_struct;
  244. int rc = 0;
  245. const u32 *prop;
  246. int len;
  247. /* Get resources(memory, IRQ) associated with the device */
  248. master = spi_alloc_master(&ofdev->dev, sizeof(struct xilinx_spi));
  249. if (master == NULL) {
  250. return -ENOMEM;
  251. }
  252. dev_set_drvdata(&ofdev->dev, master);
  253. rc = of_address_to_resource(ofdev->node, 0, r_mem);
  254. if (rc) {
  255. dev_warn(&ofdev->dev, "invalid address\n");
  256. goto put_master;
  257. }
  258. rc = of_irq_to_resource(ofdev->node, 0, r_irq);
  259. if (rc == NO_IRQ) {
  260. dev_warn(&ofdev->dev, "no IRQ found\n");
  261. goto put_master;
  262. }
  263. /* the spi->mode bits understood by this driver: */
  264. master->mode_bits = SPI_CPOL | SPI_CPHA;
  265. xspi = spi_master_get_devdata(master);
  266. xspi->bitbang.master = spi_master_get(master);
  267. xspi->bitbang.chipselect = xilinx_spi_chipselect;
  268. xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer;
  269. xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;
  270. xspi->bitbang.master->setup = xilinx_spi_setup;
  271. init_completion(&xspi->done);
  272. xspi->irq = r_irq->start;
  273. if (!request_mem_region(r_mem->start,
  274. r_mem->end - r_mem->start + 1, XILINX_SPI_NAME)) {
  275. rc = -ENXIO;
  276. dev_warn(&ofdev->dev, "memory request failure\n");
  277. goto put_master;
  278. }
  279. xspi->regs = ioremap(r_mem->start, r_mem->end - r_mem->start + 1);
  280. if (xspi->regs == NULL) {
  281. rc = -ENOMEM;
  282. dev_warn(&ofdev->dev, "ioremap failure\n");
  283. goto release_mem;
  284. }
  285. xspi->irq = r_irq->start;
  286. /* dynamic bus assignment */
  287. master->bus_num = -1;
  288. /* number of slave select bits is required */
  289. prop = of_get_property(ofdev->node, "xlnx,num-ss-bits", &len);
  290. if (!prop || len < sizeof(*prop)) {
  291. dev_warn(&ofdev->dev, "no 'xlnx,num-ss-bits' property\n");
  292. goto unmap_io;
  293. }
  294. master->num_chipselect = *prop;
  295. /* SPI controller initializations */
  296. xspi_init_hw(xspi->regs);
  297. /* Register for SPI Interrupt */
  298. rc = request_irq(xspi->irq, xilinx_spi_irq, 0, XILINX_SPI_NAME, xspi);
  299. if (rc != 0) {
  300. dev_warn(&ofdev->dev, "irq request failure: %d\n", xspi->irq);
  301. goto unmap_io;
  302. }
  303. rc = spi_bitbang_start(&xspi->bitbang);
  304. if (rc != 0) {
  305. dev_err(&ofdev->dev, "spi_bitbang_start FAILED\n");
  306. goto free_irq;
  307. }
  308. dev_info(&ofdev->dev, "at 0x%08X mapped to 0x%08X, irq=%d\n",
  309. (unsigned int)r_mem->start, (u32)xspi->regs, xspi->irq);
  310. /* Add any subnodes on the SPI bus */
  311. of_register_spi_devices(master, ofdev->node);
  312. return rc;
  313. free_irq:
  314. free_irq(xspi->irq, xspi);
  315. unmap_io:
  316. iounmap(xspi->regs);
  317. release_mem:
  318. release_mem_region(r_mem->start, resource_size(r_mem));
  319. put_master:
  320. spi_master_put(master);
  321. return rc;
  322. }
  323. static int __devexit xilinx_spi_remove(struct of_device *ofdev)
  324. {
  325. struct xilinx_spi *xspi;
  326. struct spi_master *master;
  327. struct resource r_mem;
  328. master = platform_get_drvdata(ofdev);
  329. xspi = spi_master_get_devdata(master);
  330. spi_bitbang_stop(&xspi->bitbang);
  331. free_irq(xspi->irq, xspi);
  332. iounmap(xspi->regs);
  333. if (!of_address_to_resource(ofdev->node, 0, &r_mem))
  334. release_mem_region(r_mem.start, resource_size(&r_mem));
  335. dev_set_drvdata(&ofdev->dev, 0);
  336. spi_master_put(xspi->bitbang.master);
  337. return 0;
  338. }
  339. /* work with hotplug and coldplug */
  340. MODULE_ALIAS("platform:" XILINX_SPI_NAME);
  341. static int __exit xilinx_spi_of_remove(struct of_device *op)
  342. {
  343. return xilinx_spi_remove(op);
  344. }
  345. static struct of_device_id xilinx_spi_of_match[] = {
  346. { .compatible = "xlnx,xps-spi-2.00.a", },
  347. { .compatible = "xlnx,xps-spi-2.00.b", },
  348. {}
  349. };
  350. MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
  351. static struct of_platform_driver xilinx_spi_of_driver = {
  352. .owner = THIS_MODULE,
  353. .name = "xilinx-xps-spi",
  354. .match_table = xilinx_spi_of_match,
  355. .probe = xilinx_spi_of_probe,
  356. .remove = __exit_p(xilinx_spi_of_remove),
  357. .driver = {
  358. .name = "xilinx-xps-spi",
  359. .owner = THIS_MODULE,
  360. },
  361. };
  362. static int __init xilinx_spi_init(void)
  363. {
  364. return of_register_platform_driver(&xilinx_spi_of_driver);
  365. }
  366. module_init(xilinx_spi_init);
  367. static void __exit xilinx_spi_exit(void)
  368. {
  369. of_unregister_platform_driver(&xilinx_spi_of_driver);
  370. }
  371. module_exit(xilinx_spi_exit);
  372. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  373. MODULE_DESCRIPTION("Xilinx SPI driver");
  374. MODULE_LICENSE("GPL");