spi-uniphier.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // SPDX-License-Identifier: GPL-2.0
  2. // spi-uniphier.c - Socionext UniPhier SPI controller driver
  3. // Copyright 2012 Panasonic Corporation
  4. // Copyright 2016-2018 Socionext Inc.
  5. #include <linux/kernel.h>
  6. #include <linux/bitfield.h>
  7. #include <linux/bitops.h>
  8. #include <linux/clk.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/spi/spi.h>
  14. #include <asm/unaligned.h>
  15. #define SSI_TIMEOUT_MS 2000
  16. #define SSI_MAX_CLK_DIVIDER 254
  17. #define SSI_MIN_CLK_DIVIDER 4
  18. struct uniphier_spi_priv {
  19. void __iomem *base;
  20. struct clk *clk;
  21. struct spi_master *master;
  22. struct completion xfer_done;
  23. int error;
  24. unsigned int tx_bytes;
  25. unsigned int rx_bytes;
  26. const u8 *tx_buf;
  27. u8 *rx_buf;
  28. bool is_save_param;
  29. u8 bits_per_word;
  30. u16 mode;
  31. u32 speed_hz;
  32. };
  33. #define SSI_CTL 0x00
  34. #define SSI_CTL_EN BIT(0)
  35. #define SSI_CKS 0x04
  36. #define SSI_CKS_CKRAT_MASK GENMASK(7, 0)
  37. #define SSI_CKS_CKPHS BIT(14)
  38. #define SSI_CKS_CKINIT BIT(13)
  39. #define SSI_CKS_CKDLY BIT(12)
  40. #define SSI_TXWDS 0x08
  41. #define SSI_TXWDS_WDLEN_MASK GENMASK(13, 8)
  42. #define SSI_TXWDS_TDTF_MASK GENMASK(7, 6)
  43. #define SSI_TXWDS_DTLEN_MASK GENMASK(5, 0)
  44. #define SSI_RXWDS 0x0c
  45. #define SSI_RXWDS_DTLEN_MASK GENMASK(5, 0)
  46. #define SSI_FPS 0x10
  47. #define SSI_FPS_FSPOL BIT(15)
  48. #define SSI_FPS_FSTRT BIT(14)
  49. #define SSI_SR 0x14
  50. #define SSI_SR_RNE BIT(0)
  51. #define SSI_IE 0x18
  52. #define SSI_IE_RCIE BIT(3)
  53. #define SSI_IE_RORIE BIT(0)
  54. #define SSI_IS 0x1c
  55. #define SSI_IS_RXRS BIT(9)
  56. #define SSI_IS_RCID BIT(3)
  57. #define SSI_IS_RORID BIT(0)
  58. #define SSI_IC 0x1c
  59. #define SSI_IC_TCIC BIT(4)
  60. #define SSI_IC_RCIC BIT(3)
  61. #define SSI_IC_RORIC BIT(0)
  62. #define SSI_FC 0x20
  63. #define SSI_FC_TXFFL BIT(12)
  64. #define SSI_FC_TXFTH_MASK GENMASK(11, 8)
  65. #define SSI_FC_RXFFL BIT(4)
  66. #define SSI_FC_RXFTH_MASK GENMASK(3, 0)
  67. #define SSI_TXDR 0x24
  68. #define SSI_RXDR 0x24
  69. #define SSI_FIFO_DEPTH 8U
  70. static inline unsigned int bytes_per_word(unsigned int bits)
  71. {
  72. return bits <= 8 ? 1 : (bits <= 16 ? 2 : 4);
  73. }
  74. static inline void uniphier_spi_irq_enable(struct spi_device *spi, u32 mask)
  75. {
  76. struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
  77. u32 val;
  78. val = readl(priv->base + SSI_IE);
  79. val |= mask;
  80. writel(val, priv->base + SSI_IE);
  81. }
  82. static inline void uniphier_spi_irq_disable(struct spi_device *spi, u32 mask)
  83. {
  84. struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
  85. u32 val;
  86. val = readl(priv->base + SSI_IE);
  87. val &= ~mask;
  88. writel(val, priv->base + SSI_IE);
  89. }
  90. static void uniphier_spi_set_mode(struct spi_device *spi)
  91. {
  92. struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
  93. u32 val1, val2;
  94. /*
  95. * clock setting
  96. * CKPHS capture timing. 0:rising edge, 1:falling edge
  97. * CKINIT clock initial level. 0:low, 1:high
  98. * CKDLY clock delay. 0:no delay, 1:delay depending on FSTRT
  99. * (FSTRT=0: 1 clock, FSTRT=1: 0.5 clock)
  100. *
  101. * frame setting
  102. * FSPOL frame signal porarity. 0: low, 1: high
  103. * FSTRT start frame timing
  104. * 0: rising edge of clock, 1: falling edge of clock
  105. */
  106. switch (spi->mode & (SPI_CPOL | SPI_CPHA)) {
  107. case SPI_MODE_0:
  108. /* CKPHS=1, CKINIT=0, CKDLY=1, FSTRT=0 */
  109. val1 = SSI_CKS_CKPHS | SSI_CKS_CKDLY;
  110. val2 = 0;
  111. break;
  112. case SPI_MODE_1:
  113. /* CKPHS=0, CKINIT=0, CKDLY=0, FSTRT=1 */
  114. val1 = 0;
  115. val2 = SSI_FPS_FSTRT;
  116. break;
  117. case SPI_MODE_2:
  118. /* CKPHS=0, CKINIT=1, CKDLY=1, FSTRT=1 */
  119. val1 = SSI_CKS_CKINIT | SSI_CKS_CKDLY;
  120. val2 = SSI_FPS_FSTRT;
  121. break;
  122. case SPI_MODE_3:
  123. /* CKPHS=1, CKINIT=1, CKDLY=0, FSTRT=0 */
  124. val1 = SSI_CKS_CKPHS | SSI_CKS_CKINIT;
  125. val2 = 0;
  126. break;
  127. }
  128. if (!(spi->mode & SPI_CS_HIGH))
  129. val2 |= SSI_FPS_FSPOL;
  130. writel(val1, priv->base + SSI_CKS);
  131. writel(val2, priv->base + SSI_FPS);
  132. val1 = 0;
  133. if (spi->mode & SPI_LSB_FIRST)
  134. val1 |= FIELD_PREP(SSI_TXWDS_TDTF_MASK, 1);
  135. writel(val1, priv->base + SSI_TXWDS);
  136. writel(val1, priv->base + SSI_RXWDS);
  137. }
  138. static void uniphier_spi_set_transfer_size(struct spi_device *spi, int size)
  139. {
  140. struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
  141. u32 val;
  142. val = readl(priv->base + SSI_TXWDS);
  143. val &= ~(SSI_TXWDS_WDLEN_MASK | SSI_TXWDS_DTLEN_MASK);
  144. val |= FIELD_PREP(SSI_TXWDS_WDLEN_MASK, size);
  145. val |= FIELD_PREP(SSI_TXWDS_DTLEN_MASK, size);
  146. writel(val, priv->base + SSI_TXWDS);
  147. val = readl(priv->base + SSI_RXWDS);
  148. val &= ~SSI_RXWDS_DTLEN_MASK;
  149. val |= FIELD_PREP(SSI_RXWDS_DTLEN_MASK, size);
  150. writel(val, priv->base + SSI_RXWDS);
  151. }
  152. static void uniphier_spi_set_baudrate(struct spi_device *spi,
  153. unsigned int speed)
  154. {
  155. struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
  156. u32 val, ckdiv;
  157. /*
  158. * the supported rates are even numbers from 4 to 254. (4,6,8...254)
  159. * round up as we look for equal or less speed
  160. */
  161. ckdiv = DIV_ROUND_UP(clk_get_rate(priv->clk), speed);
  162. ckdiv = round_up(ckdiv, 2);
  163. val = readl(priv->base + SSI_CKS);
  164. val &= ~SSI_CKS_CKRAT_MASK;
  165. val |= ckdiv & SSI_CKS_CKRAT_MASK;
  166. writel(val, priv->base + SSI_CKS);
  167. }
  168. static void uniphier_spi_setup_transfer(struct spi_device *spi,
  169. struct spi_transfer *t)
  170. {
  171. struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
  172. u32 val;
  173. priv->error = 0;
  174. priv->tx_buf = t->tx_buf;
  175. priv->rx_buf = t->rx_buf;
  176. priv->tx_bytes = priv->rx_bytes = t->len;
  177. if (!priv->is_save_param || priv->mode != spi->mode) {
  178. uniphier_spi_set_mode(spi);
  179. priv->mode = spi->mode;
  180. }
  181. if (!priv->is_save_param || priv->bits_per_word != t->bits_per_word) {
  182. uniphier_spi_set_transfer_size(spi, t->bits_per_word);
  183. priv->bits_per_word = t->bits_per_word;
  184. }
  185. if (!priv->is_save_param || priv->speed_hz != t->speed_hz) {
  186. uniphier_spi_set_baudrate(spi, t->speed_hz);
  187. priv->speed_hz = t->speed_hz;
  188. }
  189. if (!priv->is_save_param)
  190. priv->is_save_param = true;
  191. /* reset FIFOs */
  192. val = SSI_FC_TXFFL | SSI_FC_RXFFL;
  193. writel(val, priv->base + SSI_FC);
  194. }
  195. static void uniphier_spi_send(struct uniphier_spi_priv *priv)
  196. {
  197. int wsize;
  198. u32 val = 0;
  199. wsize = min(bytes_per_word(priv->bits_per_word), priv->tx_bytes);
  200. priv->tx_bytes -= wsize;
  201. if (priv->tx_buf) {
  202. switch (wsize) {
  203. case 1:
  204. val = *priv->tx_buf;
  205. break;
  206. case 2:
  207. val = get_unaligned_le16(priv->tx_buf);
  208. break;
  209. case 4:
  210. val = get_unaligned_le32(priv->tx_buf);
  211. break;
  212. }
  213. priv->tx_buf += wsize;
  214. }
  215. writel(val, priv->base + SSI_TXDR);
  216. }
  217. static void uniphier_spi_recv(struct uniphier_spi_priv *priv)
  218. {
  219. int rsize;
  220. u32 val;
  221. rsize = min(bytes_per_word(priv->bits_per_word), priv->rx_bytes);
  222. priv->rx_bytes -= rsize;
  223. val = readl(priv->base + SSI_RXDR);
  224. if (priv->rx_buf) {
  225. switch (rsize) {
  226. case 1:
  227. *priv->rx_buf = val;
  228. break;
  229. case 2:
  230. put_unaligned_le16(val, priv->rx_buf);
  231. break;
  232. case 4:
  233. put_unaligned_le32(val, priv->rx_buf);
  234. break;
  235. }
  236. priv->rx_buf += rsize;
  237. }
  238. }
  239. static void uniphier_spi_fill_tx_fifo(struct uniphier_spi_priv *priv)
  240. {
  241. unsigned int tx_count;
  242. u32 val;
  243. tx_count = DIV_ROUND_UP(priv->tx_bytes,
  244. bytes_per_word(priv->bits_per_word));
  245. tx_count = min(tx_count, SSI_FIFO_DEPTH);
  246. /* set fifo threshold */
  247. val = readl(priv->base + SSI_FC);
  248. val &= ~(SSI_FC_TXFTH_MASK | SSI_FC_RXFTH_MASK);
  249. val |= FIELD_PREP(SSI_FC_TXFTH_MASK, tx_count);
  250. val |= FIELD_PREP(SSI_FC_RXFTH_MASK, tx_count);
  251. writel(val, priv->base + SSI_FC);
  252. while (tx_count--)
  253. uniphier_spi_send(priv);
  254. }
  255. static void uniphier_spi_set_cs(struct spi_device *spi, bool enable)
  256. {
  257. struct uniphier_spi_priv *priv = spi_master_get_devdata(spi->master);
  258. u32 val;
  259. val = readl(priv->base + SSI_FPS);
  260. if (enable)
  261. val |= SSI_FPS_FSPOL;
  262. else
  263. val &= ~SSI_FPS_FSPOL;
  264. writel(val, priv->base + SSI_FPS);
  265. }
  266. static int uniphier_spi_transfer_one(struct spi_master *master,
  267. struct spi_device *spi,
  268. struct spi_transfer *t)
  269. {
  270. struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
  271. int status;
  272. uniphier_spi_setup_transfer(spi, t);
  273. reinit_completion(&priv->xfer_done);
  274. uniphier_spi_fill_tx_fifo(priv);
  275. uniphier_spi_irq_enable(spi, SSI_IE_RCIE | SSI_IE_RORIE);
  276. status = wait_for_completion_timeout(&priv->xfer_done,
  277. msecs_to_jiffies(SSI_TIMEOUT_MS));
  278. uniphier_spi_irq_disable(spi, SSI_IE_RCIE | SSI_IE_RORIE);
  279. if (status < 0)
  280. return status;
  281. return priv->error;
  282. }
  283. static int uniphier_spi_prepare_transfer_hardware(struct spi_master *master)
  284. {
  285. struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
  286. writel(SSI_CTL_EN, priv->base + SSI_CTL);
  287. return 0;
  288. }
  289. static int uniphier_spi_unprepare_transfer_hardware(struct spi_master *master)
  290. {
  291. struct uniphier_spi_priv *priv = spi_master_get_devdata(master);
  292. writel(0, priv->base + SSI_CTL);
  293. return 0;
  294. }
  295. static irqreturn_t uniphier_spi_handler(int irq, void *dev_id)
  296. {
  297. struct uniphier_spi_priv *priv = dev_id;
  298. u32 val, stat;
  299. stat = readl(priv->base + SSI_IS);
  300. val = SSI_IC_TCIC | SSI_IC_RCIC | SSI_IC_RORIC;
  301. writel(val, priv->base + SSI_IC);
  302. /* rx fifo overrun */
  303. if (stat & SSI_IS_RORID) {
  304. priv->error = -EIO;
  305. goto done;
  306. }
  307. /* rx complete */
  308. if ((stat & SSI_IS_RCID) && (stat & SSI_IS_RXRS)) {
  309. while ((readl(priv->base + SSI_SR) & SSI_SR_RNE) &&
  310. (priv->rx_bytes - priv->tx_bytes) > 0)
  311. uniphier_spi_recv(priv);
  312. if ((readl(priv->base + SSI_SR) & SSI_SR_RNE) ||
  313. (priv->rx_bytes != priv->tx_bytes)) {
  314. priv->error = -EIO;
  315. goto done;
  316. } else if (priv->rx_bytes == 0)
  317. goto done;
  318. /* next tx transfer */
  319. uniphier_spi_fill_tx_fifo(priv);
  320. return IRQ_HANDLED;
  321. }
  322. return IRQ_NONE;
  323. done:
  324. complete(&priv->xfer_done);
  325. return IRQ_HANDLED;
  326. }
  327. static int uniphier_spi_probe(struct platform_device *pdev)
  328. {
  329. struct uniphier_spi_priv *priv;
  330. struct spi_master *master;
  331. struct resource *res;
  332. unsigned long clk_rate;
  333. int irq;
  334. int ret;
  335. master = spi_alloc_master(&pdev->dev, sizeof(*priv));
  336. if (!master)
  337. return -ENOMEM;
  338. platform_set_drvdata(pdev, master);
  339. priv = spi_master_get_devdata(master);
  340. priv->master = master;
  341. priv->is_save_param = false;
  342. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  343. priv->base = devm_ioremap_resource(&pdev->dev, res);
  344. if (IS_ERR(priv->base)) {
  345. ret = PTR_ERR(priv->base);
  346. goto out_master_put;
  347. }
  348. priv->clk = devm_clk_get(&pdev->dev, NULL);
  349. if (IS_ERR(priv->clk)) {
  350. dev_err(&pdev->dev, "failed to get clock\n");
  351. ret = PTR_ERR(priv->clk);
  352. goto out_master_put;
  353. }
  354. ret = clk_prepare_enable(priv->clk);
  355. if (ret)
  356. goto out_master_put;
  357. irq = platform_get_irq(pdev, 0);
  358. if (irq < 0) {
  359. dev_err(&pdev->dev, "failed to get IRQ\n");
  360. ret = irq;
  361. goto out_disable_clk;
  362. }
  363. ret = devm_request_irq(&pdev->dev, irq, uniphier_spi_handler,
  364. 0, "uniphier-spi", priv);
  365. if (ret) {
  366. dev_err(&pdev->dev, "failed to request IRQ\n");
  367. goto out_disable_clk;
  368. }
  369. init_completion(&priv->xfer_done);
  370. clk_rate = clk_get_rate(priv->clk);
  371. master->max_speed_hz = DIV_ROUND_UP(clk_rate, SSI_MIN_CLK_DIVIDER);
  372. master->min_speed_hz = DIV_ROUND_UP(clk_rate, SSI_MAX_CLK_DIVIDER);
  373. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  374. master->dev.of_node = pdev->dev.of_node;
  375. master->bus_num = pdev->id;
  376. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  377. master->set_cs = uniphier_spi_set_cs;
  378. master->transfer_one = uniphier_spi_transfer_one;
  379. master->prepare_transfer_hardware
  380. = uniphier_spi_prepare_transfer_hardware;
  381. master->unprepare_transfer_hardware
  382. = uniphier_spi_unprepare_transfer_hardware;
  383. master->num_chipselect = 1;
  384. ret = devm_spi_register_master(&pdev->dev, master);
  385. if (ret)
  386. goto out_disable_clk;
  387. return 0;
  388. out_disable_clk:
  389. clk_disable_unprepare(priv->clk);
  390. out_master_put:
  391. spi_master_put(master);
  392. return ret;
  393. }
  394. static int uniphier_spi_remove(struct platform_device *pdev)
  395. {
  396. struct uniphier_spi_priv *priv = platform_get_drvdata(pdev);
  397. clk_disable_unprepare(priv->clk);
  398. return 0;
  399. }
  400. static const struct of_device_id uniphier_spi_match[] = {
  401. { .compatible = "socionext,uniphier-scssi" },
  402. { /* sentinel */ }
  403. };
  404. MODULE_DEVICE_TABLE(of, uniphier_spi_match);
  405. static struct platform_driver uniphier_spi_driver = {
  406. .probe = uniphier_spi_probe,
  407. .remove = uniphier_spi_remove,
  408. .driver = {
  409. .name = "uniphier-spi",
  410. .of_match_table = uniphier_spi_match,
  411. },
  412. };
  413. module_platform_driver(uniphier_spi_driver);
  414. MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
  415. MODULE_AUTHOR("Keiji Hayashibara <hayashibara.keiji@socionext.com>");
  416. MODULE_DESCRIPTION("Socionext UniPhier SPI controller driver");
  417. MODULE_LICENSE("GPL v2");