xilinx_spi.c 13 KB

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