spi-xlp.c 12 KB

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