spi-xlp.c 11 KB

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