xilinx_spi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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/spi/spi.h>
  17. #include <linux/spi/spi_bitbang.h>
  18. #include <linux/io.h>
  19. #include "xilinx_spi.h"
  20. #include <linux/spi/xilinx_spi.h>
  21. #define XILINX_SPI_NAME "xilinx_spi"
  22. /* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
  23. * Product Specification", DS464
  24. */
  25. #define XSPI_CR_OFFSET 0x60 /* Control Register */
  26. #define XSPI_CR_ENABLE 0x02
  27. #define XSPI_CR_MASTER_MODE 0x04
  28. #define XSPI_CR_CPOL 0x08
  29. #define XSPI_CR_CPHA 0x10
  30. #define XSPI_CR_MODE_MASK (XSPI_CR_CPHA | XSPI_CR_CPOL)
  31. #define XSPI_CR_TXFIFO_RESET 0x20
  32. #define XSPI_CR_RXFIFO_RESET 0x40
  33. #define XSPI_CR_MANUAL_SSELECT 0x80
  34. #define XSPI_CR_TRANS_INHIBIT 0x100
  35. #define XSPI_CR_LSB_FIRST 0x200
  36. #define XSPI_SR_OFFSET 0x64 /* Status Register */
  37. #define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */
  38. #define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */
  39. #define XSPI_SR_TX_EMPTY_MASK 0x04 /* Transmit FIFO is empty */
  40. #define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */
  41. #define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */
  42. #define XSPI_TXD_OFFSET 0x68 /* Data Transmit Register */
  43. #define XSPI_RXD_OFFSET 0x6c /* Data Receive Register */
  44. #define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */
  45. /* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414
  46. * IPIF registers are 32 bit
  47. */
  48. #define XIPIF_V123B_DGIER_OFFSET 0x1c /* IPIF global int enable reg */
  49. #define XIPIF_V123B_GINTR_ENABLE 0x80000000
  50. #define XIPIF_V123B_IISR_OFFSET 0x20 /* IPIF interrupt status reg */
  51. #define XIPIF_V123B_IIER_OFFSET 0x28 /* IPIF interrupt enable reg */
  52. #define XSPI_INTR_MODE_FAULT 0x01 /* Mode fault error */
  53. #define XSPI_INTR_SLAVE_MODE_FAULT 0x02 /* Selected as slave while
  54. * disabled */
  55. #define XSPI_INTR_TX_EMPTY 0x04 /* TxFIFO is empty */
  56. #define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */
  57. #define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */
  58. #define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */
  59. #define XSPI_INTR_TX_HALF_EMPTY 0x40 /* TxFIFO is half empty */
  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. struct resource mem; /* phys mem */
  67. void __iomem *regs; /* virt. address of the control registers */
  68. u32 irq;
  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. u8 bits_per_word;
  73. unsigned int (*read_fn) (void __iomem *);
  74. void (*write_fn) (u32, void __iomem *);
  75. void (*tx_fn) (struct xilinx_spi *);
  76. void (*rx_fn) (struct xilinx_spi *);
  77. };
  78. static void xspi_write32(u32 val, void __iomem *addr)
  79. {
  80. iowrite32(val, addr);
  81. }
  82. static unsigned int xspi_read32(void __iomem *addr)
  83. {
  84. return ioread32(addr);
  85. }
  86. static void xspi_write32_be(u32 val, void __iomem *addr)
  87. {
  88. iowrite32be(val, addr);
  89. }
  90. static unsigned int xspi_read32_be(void __iomem *addr)
  91. {
  92. return ioread32be(addr);
  93. }
  94. static void xspi_tx8(struct xilinx_spi *xspi)
  95. {
  96. xspi->write_fn(*xspi->tx_ptr, xspi->regs + XSPI_TXD_OFFSET);
  97. xspi->tx_ptr++;
  98. }
  99. static void xspi_tx16(struct xilinx_spi *xspi)
  100. {
  101. xspi->write_fn(*(u16 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET);
  102. xspi->tx_ptr += 2;
  103. }
  104. static void xspi_tx32(struct xilinx_spi *xspi)
  105. {
  106. xspi->write_fn(*(u32 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET);
  107. xspi->tx_ptr += 4;
  108. }
  109. static void xspi_rx8(struct xilinx_spi *xspi)
  110. {
  111. u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
  112. if (xspi->rx_ptr) {
  113. *xspi->rx_ptr = data & 0xff;
  114. xspi->rx_ptr++;
  115. }
  116. }
  117. static void xspi_rx16(struct xilinx_spi *xspi)
  118. {
  119. u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
  120. if (xspi->rx_ptr) {
  121. *(u16 *)(xspi->rx_ptr) = data & 0xffff;
  122. xspi->rx_ptr += 2;
  123. }
  124. }
  125. static void xspi_rx32(struct xilinx_spi *xspi)
  126. {
  127. u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
  128. if (xspi->rx_ptr) {
  129. *(u32 *)(xspi->rx_ptr) = data;
  130. xspi->rx_ptr += 4;
  131. }
  132. }
  133. static void xspi_init_hw(struct xilinx_spi *xspi)
  134. {
  135. void __iomem *regs_base = xspi->regs;
  136. /* Reset the SPI device */
  137. xspi->write_fn(XIPIF_V123B_RESET_MASK,
  138. regs_base + XIPIF_V123B_RESETR_OFFSET);
  139. /* Disable all the interrupts just in case */
  140. xspi->write_fn(0, regs_base + XIPIF_V123B_IIER_OFFSET);
  141. /* Enable the global IPIF interrupt */
  142. xspi->write_fn(XIPIF_V123B_GINTR_ENABLE,
  143. regs_base + XIPIF_V123B_DGIER_OFFSET);
  144. /* Deselect the slave on the SPI bus */
  145. xspi->write_fn(0xffff, regs_base + XSPI_SSR_OFFSET);
  146. /* Disable the transmitter, enable Manual Slave Select Assertion,
  147. * put SPI controller into master mode, and enable it */
  148. xspi->write_fn(XSPI_CR_TRANS_INHIBIT | XSPI_CR_MANUAL_SSELECT |
  149. XSPI_CR_MASTER_MODE | XSPI_CR_ENABLE | XSPI_CR_TXFIFO_RESET |
  150. XSPI_CR_RXFIFO_RESET, regs_base + XSPI_CR_OFFSET);
  151. }
  152. static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
  153. {
  154. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  155. if (is_on == BITBANG_CS_INACTIVE) {
  156. /* Deselect the slave on the SPI bus */
  157. xspi->write_fn(0xffff, xspi->regs + XSPI_SSR_OFFSET);
  158. } else if (is_on == BITBANG_CS_ACTIVE) {
  159. /* Set the SPI clock phase and polarity */
  160. u16 cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET)
  161. & ~XSPI_CR_MODE_MASK;
  162. if (spi->mode & SPI_CPHA)
  163. cr |= XSPI_CR_CPHA;
  164. if (spi->mode & SPI_CPOL)
  165. cr |= XSPI_CR_CPOL;
  166. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  167. /* We do not check spi->max_speed_hz here as the SPI clock
  168. * frequency is not software programmable (the IP block design
  169. * parameter)
  170. */
  171. /* Activate the chip select */
  172. xspi->write_fn(~(0x0001 << spi->chip_select),
  173. xspi->regs + XSPI_SSR_OFFSET);
  174. }
  175. }
  176. /* spi_bitbang requires custom setup_transfer() to be defined if there is a
  177. * custom txrx_bufs(). We have nothing to setup here as the SPI IP block
  178. * supports 8 or 16 bits per word which cannot be changed in software.
  179. * SPI clock can't be changed in software either.
  180. * Check for correct bits per word. Chip select delay calculations could be
  181. * added here as soon as bitbang_work() can be made aware of the delay value.
  182. */
  183. static int xilinx_spi_setup_transfer(struct spi_device *spi,
  184. struct spi_transfer *t)
  185. {
  186. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  187. u8 bits_per_word;
  188. bits_per_word = (t && t->bits_per_word)
  189. ? t->bits_per_word : spi->bits_per_word;
  190. if (bits_per_word != xspi->bits_per_word) {
  191. dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
  192. __func__, bits_per_word);
  193. return -EINVAL;
  194. }
  195. return 0;
  196. }
  197. static int xilinx_spi_setup(struct spi_device *spi)
  198. {
  199. /* always return 0, we can not check the number of bits.
  200. * There are cases when SPI setup is called before any driver is
  201. * there, in that case the SPI core defaults to 8 bits, which we
  202. * do not support in some cases. But if we return an error, the
  203. * SPI device would not be registered and no driver can get hold of it
  204. * When the driver is there, it will call SPI setup again with the
  205. * correct number of bits per transfer.
  206. * If a driver setups with the wrong bit number, it will fail when
  207. * it tries to do a transfer
  208. */
  209. return 0;
  210. }
  211. static void xilinx_spi_fill_tx_fifo(struct xilinx_spi *xspi)
  212. {
  213. u8 sr;
  214. /* Fill the Tx FIFO with as many bytes as possible */
  215. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  216. while ((sr & XSPI_SR_TX_FULL_MASK) == 0 && xspi->remaining_bytes > 0) {
  217. if (xspi->tx_ptr)
  218. xspi->tx_fn(xspi);
  219. else
  220. xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
  221. xspi->remaining_bytes -= xspi->bits_per_word / 8;
  222. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  223. }
  224. }
  225. static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  226. {
  227. struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
  228. u32 ipif_ier;
  229. u16 cr;
  230. /* We get here with transmitter inhibited */
  231. xspi->tx_ptr = t->tx_buf;
  232. xspi->rx_ptr = t->rx_buf;
  233. xspi->remaining_bytes = t->len;
  234. INIT_COMPLETION(xspi->done);
  235. xilinx_spi_fill_tx_fifo(xspi);
  236. /* Enable the transmit empty interrupt, which we use to determine
  237. * progress on the transmission.
  238. */
  239. ipif_ier = xspi->read_fn(xspi->regs + XIPIF_V123B_IIER_OFFSET);
  240. xspi->write_fn(ipif_ier | XSPI_INTR_TX_EMPTY,
  241. xspi->regs + XIPIF_V123B_IIER_OFFSET);
  242. /* Start the transfer by not inhibiting the transmitter any longer */
  243. cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET) &
  244. ~XSPI_CR_TRANS_INHIBIT;
  245. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  246. wait_for_completion(&xspi->done);
  247. /* Disable the transmit empty interrupt */
  248. xspi->write_fn(ipif_ier, xspi->regs + XIPIF_V123B_IIER_OFFSET);
  249. return t->len - xspi->remaining_bytes;
  250. }
  251. /* This driver supports single master mode only. Hence Tx FIFO Empty
  252. * is the only interrupt we care about.
  253. * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
  254. * Fault are not to happen.
  255. */
  256. static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
  257. {
  258. struct xilinx_spi *xspi = dev_id;
  259. u32 ipif_isr;
  260. /* Get the IPIF interrupts, and clear them immediately */
  261. ipif_isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET);
  262. xspi->write_fn(ipif_isr, xspi->regs + XIPIF_V123B_IISR_OFFSET);
  263. if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */
  264. u16 cr;
  265. u8 sr;
  266. /* A transmit has just completed. Process received data and
  267. * check for more data to transmit. Always inhibit the
  268. * transmitter while the Isr refills the transmit register/FIFO,
  269. * or make sure it is stopped if we're done.
  270. */
  271. cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET);
  272. xspi->write_fn(cr | XSPI_CR_TRANS_INHIBIT,
  273. xspi->regs + XSPI_CR_OFFSET);
  274. /* Read out all the data from the Rx FIFO */
  275. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  276. while ((sr & XSPI_SR_RX_EMPTY_MASK) == 0) {
  277. xspi->rx_fn(xspi);
  278. sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
  279. }
  280. /* See if there is more data to send */
  281. if (xspi->remaining_bytes > 0) {
  282. xilinx_spi_fill_tx_fifo(xspi);
  283. /* Start the transfer by not inhibiting the
  284. * transmitter any longer
  285. */
  286. xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
  287. } else {
  288. /* No more data to send.
  289. * Indicate the transfer is completed.
  290. */
  291. complete(&xspi->done);
  292. }
  293. }
  294. return IRQ_HANDLED;
  295. }
  296. struct spi_master *xilinx_spi_init(struct device *dev, struct resource *mem,
  297. u32 irq, s16 bus_num)
  298. {
  299. struct spi_master *master;
  300. struct xilinx_spi *xspi;
  301. struct xspi_platform_data *pdata = dev->platform_data;
  302. int ret;
  303. if (!pdata) {
  304. dev_err(dev, "No platform data attached\n");
  305. return NULL;
  306. }
  307. master = spi_alloc_master(dev, sizeof(struct xilinx_spi));
  308. if (!master)
  309. return NULL;
  310. /* the spi->mode bits understood by this driver: */
  311. master->mode_bits = SPI_CPOL | SPI_CPHA;
  312. xspi = spi_master_get_devdata(master);
  313. xspi->bitbang.master = spi_master_get(master);
  314. xspi->bitbang.chipselect = xilinx_spi_chipselect;
  315. xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer;
  316. xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;
  317. xspi->bitbang.master->setup = xilinx_spi_setup;
  318. init_completion(&xspi->done);
  319. if (!request_mem_region(mem->start, resource_size(mem),
  320. XILINX_SPI_NAME))
  321. goto put_master;
  322. xspi->regs = ioremap(mem->start, resource_size(mem));
  323. if (xspi->regs == NULL) {
  324. dev_warn(dev, "ioremap failure\n");
  325. goto map_failed;
  326. }
  327. master->bus_num = bus_num;
  328. master->num_chipselect = pdata->num_chipselect;
  329. xspi->mem = *mem;
  330. xspi->irq = irq;
  331. if (pdata->little_endian) {
  332. xspi->read_fn = xspi_read32;
  333. xspi->write_fn = xspi_write32;
  334. } else {
  335. xspi->read_fn = xspi_read32_be;
  336. xspi->write_fn = xspi_write32_be;
  337. }
  338. xspi->bits_per_word = pdata->bits_per_word;
  339. if (xspi->bits_per_word == 8) {
  340. xspi->tx_fn = xspi_tx8;
  341. xspi->rx_fn = xspi_rx8;
  342. } else if (xspi->bits_per_word == 16) {
  343. xspi->tx_fn = xspi_tx16;
  344. xspi->rx_fn = xspi_rx16;
  345. } else if (xspi->bits_per_word == 32) {
  346. xspi->tx_fn = xspi_tx32;
  347. xspi->rx_fn = xspi_rx32;
  348. } else
  349. goto unmap_io;
  350. /* SPI controller initializations */
  351. xspi_init_hw(xspi);
  352. /* Register for SPI Interrupt */
  353. ret = request_irq(xspi->irq, xilinx_spi_irq, 0, XILINX_SPI_NAME, xspi);
  354. if (ret)
  355. goto unmap_io;
  356. ret = spi_bitbang_start(&xspi->bitbang);
  357. if (ret) {
  358. dev_err(dev, "spi_bitbang_start FAILED\n");
  359. goto free_irq;
  360. }
  361. dev_info(dev, "at 0x%08llX mapped to 0x%p, irq=%d\n",
  362. (unsigned long long)mem->start, xspi->regs, xspi->irq);
  363. return master;
  364. free_irq:
  365. free_irq(xspi->irq, xspi);
  366. unmap_io:
  367. iounmap(xspi->regs);
  368. map_failed:
  369. release_mem_region(mem->start, resource_size(mem));
  370. put_master:
  371. spi_master_put(master);
  372. return NULL;
  373. }
  374. EXPORT_SYMBOL(xilinx_spi_init);
  375. void xilinx_spi_deinit(struct spi_master *master)
  376. {
  377. struct xilinx_spi *xspi;
  378. xspi = spi_master_get_devdata(master);
  379. spi_bitbang_stop(&xspi->bitbang);
  380. free_irq(xspi->irq, xspi);
  381. iounmap(xspi->regs);
  382. release_mem_region(xspi->mem.start, resource_size(&xspi->mem));
  383. spi_master_put(xspi->bitbang.master);
  384. }
  385. EXPORT_SYMBOL(xilinx_spi_deinit);
  386. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  387. MODULE_DESCRIPTION("Xilinx SPI driver");
  388. MODULE_LICENSE("GPL");