spi-xlp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (C) 2003-2015 Broadcom Corporation
  3. * All Rights Reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 (GPL v2)
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/of.h>
  20. #include <linux/interrupt.h>
  21. /* SPI Configuration Register */
  22. #define XLP_SPI_CONFIG 0x00
  23. #define XLP_SPI_CPHA BIT(0)
  24. #define XLP_SPI_CPOL BIT(1)
  25. #define XLP_SPI_CS_POL BIT(2)
  26. #define XLP_SPI_TXMISO_EN BIT(3)
  27. #define XLP_SPI_TXMOSI_EN BIT(4)
  28. #define XLP_SPI_RXMISO_EN BIT(5)
  29. #define XLP_SPI_CS_LSBFE BIT(10)
  30. #define XLP_SPI_RXCAP_EN BIT(11)
  31. /* SPI Frequency Divider Register */
  32. #define XLP_SPI_FDIV 0x04
  33. /* SPI Command Register */
  34. #define XLP_SPI_CMD 0x08
  35. #define XLP_SPI_CMD_IDLE_MASK 0x0
  36. #define XLP_SPI_CMD_TX_MASK 0x1
  37. #define XLP_SPI_CMD_RX_MASK 0x2
  38. #define XLP_SPI_CMD_TXRX_MASK 0x3
  39. #define XLP_SPI_CMD_CONT BIT(4)
  40. #define XLP_SPI_XFR_BITCNT_SHIFT 16
  41. /* SPI Status Register */
  42. #define XLP_SPI_STATUS 0x0c
  43. #define XLP_SPI_XFR_PENDING BIT(0)
  44. #define XLP_SPI_XFR_DONE BIT(1)
  45. #define XLP_SPI_TX_INT BIT(2)
  46. #define XLP_SPI_RX_INT BIT(3)
  47. #define XLP_SPI_TX_UF BIT(4)
  48. #define XLP_SPI_RX_OF BIT(5)
  49. #define XLP_SPI_STAT_MASK 0x3f
  50. /* SPI Interrupt Enable Register */
  51. #define XLP_SPI_INTR_EN 0x10
  52. #define XLP_SPI_INTR_DONE BIT(0)
  53. #define XLP_SPI_INTR_TXTH BIT(1)
  54. #define XLP_SPI_INTR_RXTH BIT(2)
  55. #define XLP_SPI_INTR_TXUF BIT(3)
  56. #define XLP_SPI_INTR_RXOF BIT(4)
  57. /* SPI FIFO Threshold Register */
  58. #define XLP_SPI_FIFO_THRESH 0x14
  59. /* SPI FIFO Word Count Register */
  60. #define XLP_SPI_FIFO_WCNT 0x18
  61. #define XLP_SPI_RXFIFO_WCNT_MASK 0xf
  62. #define XLP_SPI_TXFIFO_WCNT_MASK 0xf0
  63. #define XLP_SPI_TXFIFO_WCNT_SHIFT 4
  64. /* SPI Transmit Data FIFO Register */
  65. #define XLP_SPI_TXDATA_FIFO 0x1c
  66. /* SPI Receive Data FIFO Register */
  67. #define XLP_SPI_RXDATA_FIFO 0x20
  68. /* SPI System Control Register */
  69. #define XLP_SPI_SYSCTRL 0x100
  70. #define XLP_SPI_SYS_RESET BIT(0)
  71. #define XLP_SPI_SYS_CLKDIS BIT(1)
  72. #define XLP_SPI_SYS_PMEN BIT(8)
  73. #define SPI_CS_OFFSET 0x40
  74. #define XLP_SPI_TXRXTH 0x80
  75. #define XLP_SPI_FIFO_SIZE 8
  76. #define XLP_SPI_MAX_CS 4
  77. #define XLP_SPI_DEFAULT_FREQ 133333333
  78. #define XLP_SPI_FDIV_MIN 4
  79. #define XLP_SPI_FDIV_MAX 65535
  80. /*
  81. * SPI can transfer only 28 bytes properly at a time. So split the
  82. * transfer into 28 bytes size.
  83. */
  84. #define XLP_SPI_XFER_SIZE 28
  85. struct xlp_spi_priv {
  86. struct device dev; /* device structure */
  87. void __iomem *base; /* spi registers base address */
  88. const u8 *tx_buf; /* tx data buffer */
  89. u8 *rx_buf; /* rx data buffer */
  90. int tx_len; /* tx xfer length */
  91. int rx_len; /* rx xfer length */
  92. int txerrors; /* TXFIFO underflow count */
  93. int rxerrors; /* RXFIFO overflow count */
  94. int cs; /* slave device chip select */
  95. u32 spi_clk; /* spi clock frequency */
  96. bool cmd_cont; /* cs active */
  97. struct completion done; /* completion notification */
  98. };
  99. static inline u32 xlp_spi_reg_read(struct xlp_spi_priv *priv,
  100. int cs, int regoff)
  101. {
  102. return readl(priv->base + regoff + cs * SPI_CS_OFFSET);
  103. }
  104. static inline void xlp_spi_reg_write(struct xlp_spi_priv *priv, int cs,
  105. int regoff, u32 val)
  106. {
  107. writel(val, priv->base + regoff + cs * SPI_CS_OFFSET);
  108. }
  109. static inline void xlp_spi_sysctl_write(struct xlp_spi_priv *priv,
  110. int regoff, u32 val)
  111. {
  112. writel(val, priv->base + regoff);
  113. }
  114. /*
  115. * Setup global SPI_SYSCTRL register for all SPI channels.
  116. */
  117. static void xlp_spi_sysctl_setup(struct xlp_spi_priv *xspi)
  118. {
  119. int cs;
  120. for (cs = 0; cs < XLP_SPI_MAX_CS; cs++)
  121. xlp_spi_sysctl_write(xspi, XLP_SPI_SYSCTRL,
  122. XLP_SPI_SYS_RESET << cs);
  123. xlp_spi_sysctl_write(xspi, XLP_SPI_SYSCTRL, XLP_SPI_SYS_PMEN);
  124. }
  125. static int xlp_spi_setup(struct spi_device *spi)
  126. {
  127. struct xlp_spi_priv *xspi;
  128. u32 fdiv, cfg;
  129. int cs;
  130. xspi = spi_master_get_devdata(spi->master);
  131. cs = spi->chip_select;
  132. /*
  133. * The value of fdiv must be between 4 and 65535.
  134. */
  135. fdiv = DIV_ROUND_UP(xspi->spi_clk, spi->max_speed_hz);
  136. if (fdiv > XLP_SPI_FDIV_MAX)
  137. fdiv = XLP_SPI_FDIV_MAX;
  138. else if (fdiv < XLP_SPI_FDIV_MIN)
  139. fdiv = XLP_SPI_FDIV_MIN;
  140. xlp_spi_reg_write(xspi, cs, XLP_SPI_FDIV, fdiv);
  141. xlp_spi_reg_write(xspi, cs, XLP_SPI_FIFO_THRESH, XLP_SPI_TXRXTH);
  142. cfg = xlp_spi_reg_read(xspi, cs, XLP_SPI_CONFIG);
  143. if (spi->mode & SPI_CPHA)
  144. cfg |= XLP_SPI_CPHA;
  145. else
  146. cfg &= ~XLP_SPI_CPHA;
  147. if (spi->mode & SPI_CPOL)
  148. cfg |= XLP_SPI_CPOL;
  149. else
  150. cfg &= ~XLP_SPI_CPOL;
  151. if (!(spi->mode & SPI_CS_HIGH))
  152. cfg |= XLP_SPI_CS_POL;
  153. else
  154. cfg &= ~XLP_SPI_CS_POL;
  155. if (spi->mode & SPI_LSB_FIRST)
  156. cfg |= XLP_SPI_CS_LSBFE;
  157. else
  158. cfg &= ~XLP_SPI_CS_LSBFE;
  159. cfg |= XLP_SPI_TXMOSI_EN | XLP_SPI_RXMISO_EN;
  160. if (fdiv == 4)
  161. cfg |= XLP_SPI_RXCAP_EN;
  162. xlp_spi_reg_write(xspi, cs, XLP_SPI_CONFIG, cfg);
  163. return 0;
  164. }
  165. static void xlp_spi_read_rxfifo(struct xlp_spi_priv *xspi)
  166. {
  167. u32 rx_data, rxfifo_cnt;
  168. int i, j, nbytes;
  169. rxfifo_cnt = xlp_spi_reg_read(xspi, xspi->cs, XLP_SPI_FIFO_WCNT);
  170. rxfifo_cnt &= XLP_SPI_RXFIFO_WCNT_MASK;
  171. while (rxfifo_cnt) {
  172. rx_data = xlp_spi_reg_read(xspi, xspi->cs, XLP_SPI_RXDATA_FIFO);
  173. j = 0;
  174. nbytes = min(xspi->rx_len, 4);
  175. for (i = nbytes - 1; i >= 0; i--, j++)
  176. xspi->rx_buf[i] = (rx_data >> (j * 8)) & 0xff;
  177. xspi->rx_len -= nbytes;
  178. xspi->rx_buf += nbytes;
  179. rxfifo_cnt--;
  180. }
  181. }
  182. static void xlp_spi_fill_txfifo(struct xlp_spi_priv *xspi)
  183. {
  184. u32 tx_data, txfifo_cnt;
  185. int i, j, nbytes;
  186. txfifo_cnt = xlp_spi_reg_read(xspi, xspi->cs, XLP_SPI_FIFO_WCNT);
  187. txfifo_cnt &= XLP_SPI_TXFIFO_WCNT_MASK;
  188. txfifo_cnt >>= XLP_SPI_TXFIFO_WCNT_SHIFT;
  189. while (xspi->tx_len && (txfifo_cnt < XLP_SPI_FIFO_SIZE)) {
  190. j = 0;
  191. tx_data = 0;
  192. nbytes = min(xspi->tx_len, 4);
  193. for (i = nbytes - 1; i >= 0; i--, j++)
  194. tx_data |= xspi->tx_buf[i] << (j * 8);
  195. xlp_spi_reg_write(xspi, xspi->cs, XLP_SPI_TXDATA_FIFO, tx_data);
  196. xspi->tx_len -= nbytes;
  197. xspi->tx_buf += nbytes;
  198. txfifo_cnt++;
  199. }
  200. }
  201. static irqreturn_t xlp_spi_interrupt(int irq, void *dev_id)
  202. {
  203. struct xlp_spi_priv *xspi = dev_id;
  204. u32 stat;
  205. stat = xlp_spi_reg_read(xspi, xspi->cs, XLP_SPI_STATUS) &
  206. XLP_SPI_STAT_MASK;
  207. if (!stat)
  208. return IRQ_NONE;
  209. if (stat & XLP_SPI_TX_INT) {
  210. if (xspi->tx_len)
  211. xlp_spi_fill_txfifo(xspi);
  212. if (stat & XLP_SPI_TX_UF)
  213. xspi->txerrors++;
  214. }
  215. if (stat & XLP_SPI_RX_INT) {
  216. if (xspi->rx_len)
  217. xlp_spi_read_rxfifo(xspi);
  218. if (stat & XLP_SPI_RX_OF)
  219. xspi->rxerrors++;
  220. }
  221. /* write status back to clear interrupts */
  222. xlp_spi_reg_write(xspi, xspi->cs, XLP_SPI_STATUS, stat);
  223. if (stat & XLP_SPI_XFR_DONE)
  224. complete(&xspi->done);
  225. return IRQ_HANDLED;
  226. }
  227. static void xlp_spi_send_cmd(struct xlp_spi_priv *xspi, int xfer_len,
  228. int cmd_cont)
  229. {
  230. u32 cmd = 0;
  231. if (xspi->tx_buf)
  232. cmd |= XLP_SPI_CMD_TX_MASK;
  233. if (xspi->rx_buf)
  234. cmd |= XLP_SPI_CMD_RX_MASK;
  235. if (cmd_cont)
  236. cmd |= XLP_SPI_CMD_CONT;
  237. cmd |= ((xfer_len * 8 - 1) << XLP_SPI_XFR_BITCNT_SHIFT);
  238. xlp_spi_reg_write(xspi, xspi->cs, XLP_SPI_CMD, cmd);
  239. }
  240. static int xlp_spi_xfer_block(struct xlp_spi_priv *xs,
  241. const unsigned char *tx_buf,
  242. unsigned char *rx_buf, int xfer_len, int cmd_cont)
  243. {
  244. int timeout;
  245. u32 intr_mask = 0;
  246. xs->tx_buf = tx_buf;
  247. xs->rx_buf = rx_buf;
  248. xs->tx_len = (xs->tx_buf == NULL) ? 0 : xfer_len;
  249. xs->rx_len = (xs->rx_buf == NULL) ? 0 : xfer_len;
  250. xs->txerrors = xs->rxerrors = 0;
  251. /* fill TXDATA_FIFO, then send the CMD */
  252. if (xs->tx_len)
  253. xlp_spi_fill_txfifo(xs);
  254. xlp_spi_send_cmd(xs, xfer_len, cmd_cont);
  255. /*
  256. * We are getting some spurious tx interrupts, so avoid enabling
  257. * tx interrupts when only rx is in process.
  258. * Enable all the interrupts in tx case.
  259. */
  260. if (xs->tx_len)
  261. intr_mask |= XLP_SPI_INTR_TXTH | XLP_SPI_INTR_TXUF |
  262. XLP_SPI_INTR_RXTH | XLP_SPI_INTR_RXOF;
  263. else
  264. intr_mask |= XLP_SPI_INTR_RXTH | XLP_SPI_INTR_RXOF;
  265. intr_mask |= XLP_SPI_INTR_DONE;
  266. xlp_spi_reg_write(xs, xs->cs, XLP_SPI_INTR_EN, intr_mask);
  267. timeout = wait_for_completion_timeout(&xs->done,
  268. msecs_to_jiffies(1000));
  269. /* Disable interrupts */
  270. xlp_spi_reg_write(xs, xs->cs, XLP_SPI_INTR_EN, 0x0);
  271. if (!timeout) {
  272. dev_err(&xs->dev, "xfer timedout!\n");
  273. goto out;
  274. }
  275. if (xs->txerrors || xs->rxerrors)
  276. dev_err(&xs->dev, "Over/Underflow rx %d tx %d xfer %d!\n",
  277. xs->rxerrors, xs->txerrors, xfer_len);
  278. return xfer_len;
  279. out:
  280. return -ETIMEDOUT;
  281. }
  282. static int xlp_spi_txrx_bufs(struct xlp_spi_priv *xs, struct spi_transfer *t)
  283. {
  284. int bytesleft, sz;
  285. unsigned char *rx_buf;
  286. const unsigned char *tx_buf;
  287. tx_buf = t->tx_buf;
  288. rx_buf = t->rx_buf;
  289. bytesleft = t->len;
  290. while (bytesleft) {
  291. if (bytesleft > XLP_SPI_XFER_SIZE)
  292. sz = xlp_spi_xfer_block(xs, tx_buf, rx_buf,
  293. XLP_SPI_XFER_SIZE, 1);
  294. else
  295. sz = xlp_spi_xfer_block(xs, tx_buf, rx_buf,
  296. bytesleft, xs->cmd_cont);
  297. if (sz < 0)
  298. return sz;
  299. bytesleft -= sz;
  300. if (tx_buf)
  301. tx_buf += sz;
  302. if (rx_buf)
  303. rx_buf += sz;
  304. }
  305. return bytesleft;
  306. }
  307. static int xlp_spi_transfer_one(struct spi_master *master,
  308. struct spi_device *spi,
  309. struct spi_transfer *t)
  310. {
  311. struct xlp_spi_priv *xspi = spi_master_get_devdata(master);
  312. int ret = 0;
  313. xspi->cs = spi->chip_select;
  314. xspi->dev = spi->dev;
  315. if (spi_transfer_is_last(master, t))
  316. xspi->cmd_cont = 0;
  317. else
  318. xspi->cmd_cont = 1;
  319. if (xlp_spi_txrx_bufs(xspi, t))
  320. ret = -EIO;
  321. spi_finalize_current_transfer(master);
  322. return ret;
  323. }
  324. static int xlp_spi_probe(struct platform_device *pdev)
  325. {
  326. struct spi_master *master;
  327. struct xlp_spi_priv *xspi;
  328. struct resource *res;
  329. struct clk *clk;
  330. int irq, err;
  331. xspi = devm_kzalloc(&pdev->dev, sizeof(*xspi), GFP_KERNEL);
  332. if (!xspi)
  333. return -ENOMEM;
  334. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  335. xspi->base = devm_ioremap_resource(&pdev->dev, res);
  336. if (IS_ERR(xspi->base))
  337. return PTR_ERR(xspi->base);
  338. irq = platform_get_irq(pdev, 0);
  339. if (irq < 0) {
  340. dev_err(&pdev->dev, "no IRQ resource found: %d\n", irq);
  341. return irq;
  342. }
  343. err = devm_request_irq(&pdev->dev, irq, xlp_spi_interrupt, 0,
  344. pdev->name, xspi);
  345. if (err) {
  346. dev_err(&pdev->dev, "unable to request irq %d\n", irq);
  347. return err;
  348. }
  349. clk = devm_clk_get(&pdev->dev, NULL);
  350. if (IS_ERR(clk)) {
  351. dev_err(&pdev->dev, "could not get spi clock\n");
  352. return -ENODEV;
  353. }
  354. xspi->spi_clk = clk_get_rate(clk);
  355. master = spi_alloc_master(&pdev->dev, 0);
  356. if (!master) {
  357. dev_err(&pdev->dev, "could not alloc master\n");
  358. return -ENOMEM;
  359. }
  360. master->bus_num = 0;
  361. master->num_chipselect = XLP_SPI_MAX_CS;
  362. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  363. master->setup = xlp_spi_setup;
  364. master->transfer_one = xlp_spi_transfer_one;
  365. master->dev.of_node = pdev->dev.of_node;
  366. init_completion(&xspi->done);
  367. spi_master_set_devdata(master, xspi);
  368. xlp_spi_sysctl_setup(xspi);
  369. /* register spi controller */
  370. err = devm_spi_register_master(&pdev->dev, master);
  371. if (err) {
  372. dev_err(&pdev->dev, "spi register master failed!\n");
  373. spi_master_put(master);
  374. return err;
  375. }
  376. return 0;
  377. }
  378. static const struct of_device_id xlp_spi_dt_id[] = {
  379. { .compatible = "netlogic,xlp832-spi" },
  380. { },
  381. };
  382. static struct platform_driver xlp_spi_driver = {
  383. .probe = xlp_spi_probe,
  384. .driver = {
  385. .name = "xlp-spi",
  386. .of_match_table = xlp_spi_dt_id,
  387. },
  388. };
  389. module_platform_driver(xlp_spi_driver);
  390. MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
  391. MODULE_DESCRIPTION("Netlogic XLP SPI controller driver");
  392. MODULE_LICENSE("GPL v2");