xilinx_spi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 : spi->bits_per_word;
  126. if (bits_per_word != 8) {
  127. dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
  128. __func__, bits_per_word);
  129. return -EINVAL;
  130. }
  131. return 0;
  132. }
  133. static int xilinx_spi_setup(struct spi_device *spi)
  134. {
  135. struct spi_bitbang *bitbang;
  136. struct xilinx_spi *xspi;
  137. int retval;
  138. xspi = spi_master_get_devdata(spi->master);
  139. bitbang = &xspi->bitbang;
  140. retval = xilinx_spi_setup_transfer(spi, NULL);
  141. if (retval < 0)
  142. return retval;
  143. return 0;
  144. }
  145. static void xilinx_spi_fill_tx_fifo(struct xilinx_spi *xspi)
  146. {
  147. u8 sr;
  148. /* Fill the Tx FIFO with as many bytes as possible */
  149. sr = in_8(xspi->regs + XSPI_SR_OFFSET);
  150. while ((sr & XSPI_SR_TX_FULL_MASK) == 0 && xspi->remaining_bytes > 0) {
  151. if (xspi->tx_ptr) {
  152. out_8(xspi->regs + XSPI_TXD_OFFSET, *xspi->tx_ptr++);
  153. } else {
  154. out_8(xspi->regs + XSPI_TXD_OFFSET, 0);
  155. }
  156. xspi->remaining_bytes--;
  157. sr = in_8(xspi->regs + XSPI_SR_OFFSET);
  158. }
  159. }
  160. static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  161. {
  162. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  163. u32 ipif_ier;
  164. u16 cr;
  165. /* We get here with transmitter inhibited */
  166. xspi->tx_ptr = t->tx_buf;
  167. xspi->rx_ptr = t->rx_buf;
  168. xspi->remaining_bytes = t->len;
  169. INIT_COMPLETION(xspi->done);
  170. xilinx_spi_fill_tx_fifo(xspi);
  171. /* Enable the transmit empty interrupt, which we use to determine
  172. * progress on the transmission.
  173. */
  174. ipif_ier = in_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET);
  175. out_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET,
  176. ipif_ier | XSPI_INTR_TX_EMPTY);
  177. /* Start the transfer by not inhibiting the transmitter any longer */
  178. cr = in_be16(xspi->regs + XSPI_CR_OFFSET) & ~XSPI_CR_TRANS_INHIBIT;
  179. out_be16(xspi->regs + XSPI_CR_OFFSET, cr);
  180. wait_for_completion(&xspi->done);
  181. /* Disable the transmit empty interrupt */
  182. out_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET, ipif_ier);
  183. return t->len - xspi->remaining_bytes;
  184. }
  185. /* This driver supports single master mode only. Hence Tx FIFO Empty
  186. * is the only interrupt we care about.
  187. * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
  188. * Fault are not to happen.
  189. */
  190. static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
  191. {
  192. struct xilinx_spi *xspi = dev_id;
  193. u32 ipif_isr;
  194. /* Get the IPIF interrupts, and clear them immediately */
  195. ipif_isr = in_be32(xspi->regs + XIPIF_V123B_IISR_OFFSET);
  196. out_be32(xspi->regs + XIPIF_V123B_IISR_OFFSET, ipif_isr);
  197. if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */
  198. u16 cr;
  199. u8 sr;
  200. /* A transmit has just completed. Process received data and
  201. * check for more data to transmit. Always inhibit the
  202. * transmitter while the Isr refills the transmit register/FIFO,
  203. * or make sure it is stopped if we're done.
  204. */
  205. cr = in_be16(xspi->regs + XSPI_CR_OFFSET);
  206. out_be16(xspi->regs + XSPI_CR_OFFSET,
  207. cr | XSPI_CR_TRANS_INHIBIT);
  208. /* Read out all the data from the Rx FIFO */
  209. sr = in_8(xspi->regs + XSPI_SR_OFFSET);
  210. while ((sr & XSPI_SR_RX_EMPTY_MASK) == 0) {
  211. u8 data;
  212. data = in_8(xspi->regs + XSPI_RXD_OFFSET);
  213. if (xspi->rx_ptr) {
  214. *xspi->rx_ptr++ = data;
  215. }
  216. sr = in_8(xspi->regs + XSPI_SR_OFFSET);
  217. }
  218. /* See if there is more data to send */
  219. if (xspi->remaining_bytes > 0) {
  220. xilinx_spi_fill_tx_fifo(xspi);
  221. /* Start the transfer by not inhibiting the
  222. * transmitter any longer
  223. */
  224. out_be16(xspi->regs + XSPI_CR_OFFSET, cr);
  225. } else {
  226. /* No more data to send.
  227. * Indicate the transfer is completed.
  228. */
  229. complete(&xspi->done);
  230. }
  231. }
  232. return IRQ_HANDLED;
  233. }
  234. static int __init xilinx_spi_of_probe(struct of_device *ofdev,
  235. const struct of_device_id *match)
  236. {
  237. struct spi_master *master;
  238. struct xilinx_spi *xspi;
  239. struct resource r_irq_struct;
  240. struct resource r_mem_struct;
  241. struct resource *r_irq = &r_irq_struct;
  242. struct resource *r_mem = &r_mem_struct;
  243. int rc = 0;
  244. const u32 *prop;
  245. int len;
  246. /* Get resources(memory, IRQ) associated with the device */
  247. master = spi_alloc_master(&ofdev->dev, sizeof(struct xilinx_spi));
  248. if (master == NULL) {
  249. return -ENOMEM;
  250. }
  251. dev_set_drvdata(&ofdev->dev, master);
  252. rc = of_address_to_resource(ofdev->node, 0, r_mem);
  253. if (rc) {
  254. dev_warn(&ofdev->dev, "invalid address\n");
  255. goto put_master;
  256. }
  257. rc = of_irq_to_resource(ofdev->node, 0, r_irq);
  258. if (rc == NO_IRQ) {
  259. dev_warn(&ofdev->dev, "no IRQ found\n");
  260. goto put_master;
  261. }
  262. /* the spi->mode bits understood by this driver: */
  263. master->mode_bits = SPI_CPOL | SPI_CPHA;
  264. xspi = spi_master_get_devdata(master);
  265. xspi->bitbang.master = spi_master_get(master);
  266. xspi->bitbang.chipselect = xilinx_spi_chipselect;
  267. xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer;
  268. xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;
  269. xspi->bitbang.master->setup = xilinx_spi_setup;
  270. init_completion(&xspi->done);
  271. xspi->irq = r_irq->start;
  272. if (!request_mem_region(r_mem->start,
  273. r_mem->end - r_mem->start + 1, XILINX_SPI_NAME)) {
  274. rc = -ENXIO;
  275. dev_warn(&ofdev->dev, "memory request failure\n");
  276. goto put_master;
  277. }
  278. xspi->regs = ioremap(r_mem->start, r_mem->end - r_mem->start + 1);
  279. if (xspi->regs == NULL) {
  280. rc = -ENOMEM;
  281. dev_warn(&ofdev->dev, "ioremap failure\n");
  282. goto release_mem;
  283. }
  284. xspi->irq = r_irq->start;
  285. /* dynamic bus assignment */
  286. master->bus_num = -1;
  287. /* number of slave select bits is required */
  288. prop = of_get_property(ofdev->node, "xlnx,num-ss-bits", &len);
  289. if (!prop || len < sizeof(*prop)) {
  290. dev_warn(&ofdev->dev, "no 'xlnx,num-ss-bits' property\n");
  291. goto unmap_io;
  292. }
  293. master->num_chipselect = *prop;
  294. /* SPI controller initializations */
  295. xspi_init_hw(xspi->regs);
  296. /* Register for SPI Interrupt */
  297. rc = request_irq(xspi->irq, xilinx_spi_irq, 0, XILINX_SPI_NAME, xspi);
  298. if (rc != 0) {
  299. dev_warn(&ofdev->dev, "irq request failure: %d\n", xspi->irq);
  300. goto unmap_io;
  301. }
  302. rc = spi_bitbang_start(&xspi->bitbang);
  303. if (rc != 0) {
  304. dev_err(&ofdev->dev, "spi_bitbang_start FAILED\n");
  305. goto free_irq;
  306. }
  307. dev_info(&ofdev->dev, "at 0x%08X mapped to 0x%08X, irq=%d\n",
  308. (unsigned int)r_mem->start, (u32)xspi->regs, xspi->irq);
  309. /* Add any subnodes on the SPI bus */
  310. of_register_spi_devices(master, ofdev->node);
  311. return rc;
  312. free_irq:
  313. free_irq(xspi->irq, xspi);
  314. unmap_io:
  315. iounmap(xspi->regs);
  316. release_mem:
  317. release_mem_region(r_mem->start, resource_size(r_mem));
  318. put_master:
  319. spi_master_put(master);
  320. return rc;
  321. }
  322. static int __devexit xilinx_spi_remove(struct of_device *ofdev)
  323. {
  324. struct xilinx_spi *xspi;
  325. struct spi_master *master;
  326. struct resource r_mem;
  327. master = platform_get_drvdata(ofdev);
  328. xspi = spi_master_get_devdata(master);
  329. spi_bitbang_stop(&xspi->bitbang);
  330. free_irq(xspi->irq, xspi);
  331. iounmap(xspi->regs);
  332. if (!of_address_to_resource(ofdev->node, 0, &r_mem))
  333. release_mem_region(r_mem.start, resource_size(&r_mem));
  334. dev_set_drvdata(&ofdev->dev, 0);
  335. spi_master_put(xspi->bitbang.master);
  336. return 0;
  337. }
  338. /* work with hotplug and coldplug */
  339. MODULE_ALIAS("platform:" XILINX_SPI_NAME);
  340. static int __exit xilinx_spi_of_remove(struct of_device *op)
  341. {
  342. return xilinx_spi_remove(op);
  343. }
  344. static struct of_device_id xilinx_spi_of_match[] = {
  345. { .compatible = "xlnx,xps-spi-2.00.a", },
  346. { .compatible = "xlnx,xps-spi-2.00.b", },
  347. {}
  348. };
  349. MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
  350. static struct of_platform_driver xilinx_spi_of_driver = {
  351. .owner = THIS_MODULE,
  352. .name = "xilinx-xps-spi",
  353. .match_table = xilinx_spi_of_match,
  354. .probe = xilinx_spi_of_probe,
  355. .remove = __exit_p(xilinx_spi_of_remove),
  356. .driver = {
  357. .name = "xilinx-xps-spi",
  358. .owner = THIS_MODULE,
  359. },
  360. };
  361. static int __init xilinx_spi_init(void)
  362. {
  363. return of_register_platform_driver(&xilinx_spi_of_driver);
  364. }
  365. module_init(xilinx_spi_init);
  366. static void __exit xilinx_spi_exit(void)
  367. {
  368. of_unregister_platform_driver(&xilinx_spi_of_driver);
  369. }
  370. module_exit(xilinx_spi_exit);
  371. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  372. MODULE_DESCRIPTION("Xilinx SPI driver");
  373. MODULE_LICENSE("GPL");