xilinx_spi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/mfd/core.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/spi/spi_bitbang.h>
  22. #include <linux/spi/xilinx_spi.h>
  23. #include <linux/io.h>
  24. #define XILINX_SPI_NAME "xilinx_spi"
  25. /* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
  26. * Product Specification", DS464
  27. */
  28. #define XSPI_CR_OFFSET 0x60 /* Control Register */
  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. #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. struct resource mem; /* phys mem */
  70. void __iomem *regs; /* virt. address of the control registers */
  71. u32 irq;
  72. u8 *rx_ptr; /* pointer in the Tx buffer */
  73. const u8 *tx_ptr; /* pointer in the Rx buffer */
  74. int remaining_bytes; /* the number of bytes left to transfer */
  75. u8 bits_per_word;
  76. unsigned int (*read_fn) (void __iomem *);
  77. void (*write_fn) (u32, void __iomem *);
  78. void (*tx_fn) (struct xilinx_spi *);
  79. void (*rx_fn) (struct xilinx_spi *);
  80. };
  81. static void xspi_write32(u32 val, void __iomem *addr)
  82. {
  83. iowrite32(val, addr);
  84. }
  85. static unsigned int xspi_read32(void __iomem *addr)
  86. {
  87. return ioread32(addr);
  88. }
  89. static void xspi_write32_be(u32 val, void __iomem *addr)
  90. {
  91. iowrite32be(val, addr);
  92. }
  93. static unsigned int xspi_read32_be(void __iomem *addr)
  94. {
  95. return ioread32be(addr);
  96. }
  97. static void xspi_tx8(struct xilinx_spi *xspi)
  98. {
  99. xspi->write_fn(*xspi->tx_ptr, xspi->regs + XSPI_TXD_OFFSET);
  100. xspi->tx_ptr++;
  101. }
  102. static void xspi_tx16(struct xilinx_spi *xspi)
  103. {
  104. xspi->write_fn(*(u16 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET);
  105. xspi->tx_ptr += 2;
  106. }
  107. static void xspi_tx32(struct xilinx_spi *xspi)
  108. {
  109. xspi->write_fn(*(u32 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET);
  110. xspi->tx_ptr += 4;
  111. }
  112. static void xspi_rx8(struct xilinx_spi *xspi)
  113. {
  114. u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
  115. if (xspi->rx_ptr) {
  116. *xspi->rx_ptr = data & 0xff;
  117. xspi->rx_ptr++;
  118. }
  119. }
  120. static void xspi_rx16(struct xilinx_spi *xspi)
  121. {
  122. u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
  123. if (xspi->rx_ptr) {
  124. *(u16 *)(xspi->rx_ptr) = data & 0xffff;
  125. xspi->rx_ptr += 2;
  126. }
  127. }
  128. static void xspi_rx32(struct xilinx_spi *xspi)
  129. {
  130. u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
  131. if (xspi->rx_ptr) {
  132. *(u32 *)(xspi->rx_ptr) = data;
  133. xspi->rx_ptr += 4;
  134. }
  135. }
  136. static void xspi_init_hw(struct xilinx_spi *xspi)
  137. {
  138. void __iomem *regs_base = xspi->regs;
  139. /* Reset the SPI device */
  140. xspi->write_fn(XIPIF_V123B_RESET_MASK,
  141. regs_base + XIPIF_V123B_RESETR_OFFSET);
  142. /* Disable all the interrupts just in case */
  143. xspi->write_fn(0, regs_base + XIPIF_V123B_IIER_OFFSET);
  144. /* Enable the global IPIF interrupt */
  145. xspi->write_fn(XIPIF_V123B_GINTR_ENABLE,
  146. regs_base + XIPIF_V123B_DGIER_OFFSET);
  147. /* Deselect the slave on the SPI bus */
  148. xspi->write_fn(0xffff, regs_base + XSPI_SSR_OFFSET);
  149. /* Disable the transmitter, enable Manual Slave Select Assertion,
  150. * put SPI controller into master mode, and enable it */
  151. xspi->write_fn(XSPI_CR_TRANS_INHIBIT | XSPI_CR_MANUAL_SSELECT |
  152. XSPI_CR_MASTER_MODE | XSPI_CR_ENABLE | XSPI_CR_TXFIFO_RESET |
  153. XSPI_CR_RXFIFO_RESET, regs_base + XSPI_CR_OFFSET);
  154. }
  155. static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
  156. {
  157. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  158. if (is_on == BITBANG_CS_INACTIVE) {
  159. /* Deselect the slave on the SPI bus */
  160. xspi->write_fn(0xffff, xspi->regs + XSPI_SSR_OFFSET);
  161. } else if (is_on == BITBANG_CS_ACTIVE) {
  162. /* Set the SPI clock phase and polarity */
  163. u16 cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET)
  164. & ~XSPI_CR_MODE_MASK;
  165. if (spi->mode & SPI_CPHA)
  166. cr |= XSPI_CR_CPHA;
  167. if (spi->mode & SPI_CPOL)
  168. cr |= XSPI_CR_CPOL;
  169. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  170. /* We do not check spi->max_speed_hz here as the SPI clock
  171. * frequency is not software programmable (the IP block design
  172. * parameter)
  173. */
  174. /* Activate the chip select */
  175. xspi->write_fn(~(0x0001 << spi->chip_select),
  176. xspi->regs + XSPI_SSR_OFFSET);
  177. }
  178. }
  179. /* spi_bitbang requires custom setup_transfer() to be defined if there is a
  180. * custom txrx_bufs(). We have nothing to setup here as the SPI IP block
  181. * supports 8 or 16 bits per word which cannot be changed in software.
  182. * SPI clock can't be changed in software either.
  183. * Check for correct bits per word. Chip select delay calculations could be
  184. * added here as soon as bitbang_work() can be made aware of the delay value.
  185. */
  186. static int xilinx_spi_setup_transfer(struct spi_device *spi,
  187. struct spi_transfer *t)
  188. {
  189. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  190. u8 bits_per_word;
  191. bits_per_word = (t && t->bits_per_word)
  192. ? t->bits_per_word : spi->bits_per_word;
  193. if (bits_per_word != xspi->bits_per_word) {
  194. dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
  195. __func__, bits_per_word);
  196. return -EINVAL;
  197. }
  198. return 0;
  199. }
  200. static int xilinx_spi_setup(struct spi_device *spi)
  201. {
  202. /* always return 0, we can not check the number of bits.
  203. * There are cases when SPI setup is called before any driver is
  204. * there, in that case the SPI core defaults to 8 bits, which we
  205. * do not support in some cases. But if we return an error, the
  206. * SPI device would not be registered and no driver can get hold of it
  207. * When the driver is there, it will call SPI setup again with the
  208. * correct number of bits per transfer.
  209. * If a driver setups with the wrong bit number, it will fail when
  210. * it tries to do a transfer
  211. */
  212. return 0;
  213. }
  214. static void xilinx_spi_fill_tx_fifo(struct xilinx_spi *xspi)
  215. {
  216. u8 sr;
  217. /* Fill the Tx FIFO with as many bytes as possible */
  218. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  219. while ((sr & XSPI_SR_TX_FULL_MASK) == 0 && xspi->remaining_bytes > 0) {
  220. if (xspi->tx_ptr)
  221. xspi->tx_fn(xspi);
  222. else
  223. xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
  224. xspi->remaining_bytes -= xspi->bits_per_word / 8;
  225. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  226. }
  227. }
  228. static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  229. {
  230. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  231. u32 ipif_ier;
  232. u16 cr;
  233. /* We get here with transmitter inhibited */
  234. xspi->tx_ptr = t->tx_buf;
  235. xspi->rx_ptr = t->rx_buf;
  236. xspi->remaining_bytes = t->len;
  237. INIT_COMPLETION(xspi->done);
  238. xilinx_spi_fill_tx_fifo(xspi);
  239. /* Enable the transmit empty interrupt, which we use to determine
  240. * progress on the transmission.
  241. */
  242. ipif_ier = xspi->read_fn(xspi->regs + XIPIF_V123B_IIER_OFFSET);
  243. xspi->write_fn(ipif_ier | XSPI_INTR_TX_EMPTY,
  244. xspi->regs + XIPIF_V123B_IIER_OFFSET);
  245. /* Start the transfer by not inhibiting the transmitter any longer */
  246. cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET) &
  247. ~XSPI_CR_TRANS_INHIBIT;
  248. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  249. wait_for_completion(&xspi->done);
  250. /* Disable the transmit empty interrupt */
  251. xspi->write_fn(ipif_ier, xspi->regs + XIPIF_V123B_IIER_OFFSET);
  252. return t->len - xspi->remaining_bytes;
  253. }
  254. /* This driver supports single master mode only. Hence Tx FIFO Empty
  255. * is the only interrupt we care about.
  256. * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
  257. * Fault are not to happen.
  258. */
  259. static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
  260. {
  261. struct xilinx_spi *xspi = dev_id;
  262. u32 ipif_isr;
  263. /* Get the IPIF interrupts, and clear them immediately */
  264. ipif_isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET);
  265. xspi->write_fn(ipif_isr, xspi->regs + XIPIF_V123B_IISR_OFFSET);
  266. if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */
  267. u16 cr;
  268. u8 sr;
  269. /* A transmit has just completed. Process received data and
  270. * check for more data to transmit. Always inhibit the
  271. * transmitter while the Isr refills the transmit register/FIFO,
  272. * or make sure it is stopped if we're done.
  273. */
  274. cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET);
  275. xspi->write_fn(cr | XSPI_CR_TRANS_INHIBIT,
  276. xspi->regs + XSPI_CR_OFFSET);
  277. /* Read out all the data from the Rx FIFO */
  278. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  279. while ((sr & XSPI_SR_RX_EMPTY_MASK) == 0) {
  280. xspi->rx_fn(xspi);
  281. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  282. }
  283. /* See if there is more data to send */
  284. if (xspi->remaining_bytes > 0) {
  285. xilinx_spi_fill_tx_fifo(xspi);
  286. /* Start the transfer by not inhibiting the
  287. * transmitter any longer
  288. */
  289. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  290. } else {
  291. /* No more data to send.
  292. * Indicate the transfer is completed.
  293. */
  294. complete(&xspi->done);
  295. }
  296. }
  297. return IRQ_HANDLED;
  298. }
  299. static const struct of_device_id xilinx_spi_of_match[] = {
  300. { .compatible = "xlnx,xps-spi-2.00.a", },
  301. { .compatible = "xlnx,xps-spi-2.00.b", },
  302. {}
  303. };
  304. MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
  305. struct spi_master *xilinx_spi_init(struct device *dev, struct resource *mem,
  306. u32 irq, s16 bus_num, int num_cs, int little_endian, int bits_per_word)
  307. {
  308. struct spi_master *master;
  309. struct xilinx_spi *xspi;
  310. int ret;
  311. master = spi_alloc_master(dev, sizeof(struct xilinx_spi));
  312. if (!master)
  313. return NULL;
  314. /* the spi->mode bits understood by this driver: */
  315. master->mode_bits = SPI_CPOL | SPI_CPHA;
  316. xspi = spi_master_get_devdata(master);
  317. xspi->bitbang.master = spi_master_get(master);
  318. xspi->bitbang.chipselect = xilinx_spi_chipselect;
  319. xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer;
  320. xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;
  321. xspi->bitbang.master->setup = xilinx_spi_setup;
  322. init_completion(&xspi->done);
  323. if (!request_mem_region(mem->start, resource_size(mem),
  324. XILINX_SPI_NAME))
  325. goto put_master;
  326. xspi->regs = ioremap(mem->start, resource_size(mem));
  327. if (xspi->regs == NULL) {
  328. dev_warn(dev, "ioremap failure\n");
  329. goto map_failed;
  330. }
  331. master->bus_num = bus_num;
  332. master->num_chipselect = num_cs;
  333. master->dev.of_node = dev->of_node;
  334. xspi->mem = *mem;
  335. xspi->irq = irq;
  336. if (little_endian) {
  337. xspi->read_fn = xspi_read32;
  338. xspi->write_fn = xspi_write32;
  339. } else {
  340. xspi->read_fn = xspi_read32_be;
  341. xspi->write_fn = xspi_write32_be;
  342. }
  343. xspi->bits_per_word = bits_per_word;
  344. if (xspi->bits_per_word == 8) {
  345. xspi->tx_fn = xspi_tx8;
  346. xspi->rx_fn = xspi_rx8;
  347. } else if (xspi->bits_per_word == 16) {
  348. xspi->tx_fn = xspi_tx16;
  349. xspi->rx_fn = xspi_rx16;
  350. } else if (xspi->bits_per_word == 32) {
  351. xspi->tx_fn = xspi_tx32;
  352. xspi->rx_fn = xspi_rx32;
  353. } else
  354. goto unmap_io;
  355. /* SPI controller initializations */
  356. xspi_init_hw(xspi);
  357. /* Register for SPI Interrupt */
  358. ret = request_irq(xspi->irq, xilinx_spi_irq, 0, XILINX_SPI_NAME, xspi);
  359. if (ret)
  360. goto unmap_io;
  361. ret = spi_bitbang_start(&xspi->bitbang);
  362. if (ret) {
  363. dev_err(dev, "spi_bitbang_start FAILED\n");
  364. goto free_irq;
  365. }
  366. dev_info(dev, "at 0x%08llX mapped to 0x%p, irq=%d\n",
  367. (unsigned long long)mem->start, xspi->regs, xspi->irq);
  368. return master;
  369. free_irq:
  370. free_irq(xspi->irq, xspi);
  371. unmap_io:
  372. iounmap(xspi->regs);
  373. map_failed:
  374. release_mem_region(mem->start, resource_size(mem));
  375. put_master:
  376. spi_master_put(master);
  377. return NULL;
  378. }
  379. EXPORT_SYMBOL(xilinx_spi_init);
  380. void xilinx_spi_deinit(struct spi_master *master)
  381. {
  382. struct xilinx_spi *xspi;
  383. xspi = spi_master_get_devdata(master);
  384. spi_bitbang_stop(&xspi->bitbang);
  385. free_irq(xspi->irq, xspi);
  386. iounmap(xspi->regs);
  387. release_mem_region(xspi->mem.start, resource_size(&xspi->mem));
  388. spi_master_put(xspi->bitbang.master);
  389. }
  390. EXPORT_SYMBOL(xilinx_spi_deinit);
  391. static int __devinit xilinx_spi_probe(struct platform_device *dev)
  392. {
  393. struct xspi_platform_data *pdata;
  394. struct resource *r;
  395. int irq, num_cs = 0, little_endian = 0, bits_per_word = 8;
  396. struct spi_master *master;
  397. u8 i;
  398. pdata = mfd_get_data(dev);
  399. if (pdata) {
  400. num_cs = pdata->num_chipselect;
  401. little_endian = pdata->little_endian;
  402. bits_per_word = pdata->bits_per_word;
  403. }
  404. #ifdef CONFIG_OF
  405. if (dev->dev.of_node) {
  406. const __be32 *prop;
  407. int len;
  408. /* number of slave select bits is required */
  409. prop = of_get_property(dev->dev.of_node, "xlnx,num-ss-bits",
  410. &len);
  411. if (prop && len >= sizeof(*prop))
  412. num_cs = __be32_to_cpup(prop);
  413. }
  414. #endif
  415. if (!num_cs) {
  416. dev_err(&dev->dev, "Missing slave select configuration data\n");
  417. return -EINVAL;
  418. }
  419. r = platform_get_resource(dev, IORESOURCE_MEM, 0);
  420. if (!r)
  421. return -ENODEV;
  422. irq = platform_get_irq(dev, 0);
  423. if (irq < 0)
  424. return -ENXIO;
  425. master = xilinx_spi_init(&dev->dev, r, irq, dev->id, num_cs,
  426. little_endian, bits_per_word);
  427. if (!master)
  428. return -ENODEV;
  429. if (pdata) {
  430. for (i = 0; i < pdata->num_devices; i++)
  431. spi_new_device(master, pdata->devices + i);
  432. }
  433. platform_set_drvdata(dev, master);
  434. return 0;
  435. }
  436. static int __devexit xilinx_spi_remove(struct platform_device *dev)
  437. {
  438. xilinx_spi_deinit(platform_get_drvdata(dev));
  439. platform_set_drvdata(dev, 0);
  440. return 0;
  441. }
  442. /* work with hotplug and coldplug */
  443. MODULE_ALIAS("platform:" XILINX_SPI_NAME);
  444. static struct platform_driver xilinx_spi_driver = {
  445. .probe = xilinx_spi_probe,
  446. .remove = __devexit_p(xilinx_spi_remove),
  447. .driver = {
  448. .name = XILINX_SPI_NAME,
  449. .owner = THIS_MODULE,
  450. .of_match_table = xilinx_spi_of_match,
  451. },
  452. };
  453. static int __init xilinx_spi_pltfm_init(void)
  454. {
  455. return platform_driver_register(&xilinx_spi_driver);
  456. }
  457. module_init(xilinx_spi_pltfm_init);
  458. static void __exit xilinx_spi_pltfm_exit(void)
  459. {
  460. platform_driver_unregister(&xilinx_spi_driver);
  461. }
  462. module_exit(xilinx_spi_pltfm_exit);
  463. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  464. MODULE_DESCRIPTION("Xilinx SPI driver");
  465. MODULE_LICENSE("GPL");