spi-st-ssc4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (c) 2008-2014 STMicroelectronics Limited
  3. *
  4. * Author: Angus Clark <Angus.Clark@st.com>
  5. * Patrice Chotard <patrice.chotard@st.com>
  6. * Lee Jones <lee.jones@linaro.org>
  7. *
  8. * SPI master mode controller driver, used in STMicroelectronics devices.
  9. *
  10. * May be copied or modified under the terms of the GNU General Public
  11. * License Version 2.0 only. See linux/COPYING for more information.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/of.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/spi/spi_bitbang.h>
  26. /* SSC registers */
  27. #define SSC_BRG 0x000
  28. #define SSC_TBUF 0x004
  29. #define SSC_RBUF 0x008
  30. #define SSC_CTL 0x00C
  31. #define SSC_IEN 0x010
  32. #define SSC_I2C 0x018
  33. /* SSC Control */
  34. #define SSC_CTL_DATA_WIDTH_9 0x8
  35. #define SSC_CTL_DATA_WIDTH_MSK 0xf
  36. #define SSC_CTL_BM 0xf
  37. #define SSC_CTL_HB BIT(4)
  38. #define SSC_CTL_PH BIT(5)
  39. #define SSC_CTL_PO BIT(6)
  40. #define SSC_CTL_SR BIT(7)
  41. #define SSC_CTL_MS BIT(8)
  42. #define SSC_CTL_EN BIT(9)
  43. #define SSC_CTL_LPB BIT(10)
  44. #define SSC_CTL_EN_TX_FIFO BIT(11)
  45. #define SSC_CTL_EN_RX_FIFO BIT(12)
  46. #define SSC_CTL_EN_CLST_RX BIT(13)
  47. /* SSC Interrupt Enable */
  48. #define SSC_IEN_TEEN BIT(2)
  49. #define FIFO_SIZE 8
  50. struct spi_st {
  51. /* SSC SPI Controller */
  52. void __iomem *base;
  53. struct clk *clk;
  54. struct device *dev;
  55. /* SSC SPI current transaction */
  56. const u8 *tx_ptr;
  57. u8 *rx_ptr;
  58. u16 bytes_per_word;
  59. unsigned int words_remaining;
  60. unsigned int baud;
  61. struct completion done;
  62. };
  63. /* Load the TX FIFO */
  64. static void ssc_write_tx_fifo(struct spi_st *spi_st)
  65. {
  66. unsigned int count, i;
  67. uint32_t word = 0;
  68. if (spi_st->words_remaining > FIFO_SIZE)
  69. count = FIFO_SIZE;
  70. else
  71. count = spi_st->words_remaining;
  72. for (i = 0; i < count; i++) {
  73. if (spi_st->tx_ptr) {
  74. if (spi_st->bytes_per_word == 1) {
  75. word = *spi_st->tx_ptr++;
  76. } else {
  77. word = *spi_st->tx_ptr++;
  78. word = *spi_st->tx_ptr++ | (word << 8);
  79. }
  80. }
  81. writel_relaxed(word, spi_st->base + SSC_TBUF);
  82. }
  83. }
  84. /* Read the RX FIFO */
  85. static void ssc_read_rx_fifo(struct spi_st *spi_st)
  86. {
  87. unsigned int count, i;
  88. uint32_t word = 0;
  89. if (spi_st->words_remaining > FIFO_SIZE)
  90. count = FIFO_SIZE;
  91. else
  92. count = spi_st->words_remaining;
  93. for (i = 0; i < count; i++) {
  94. word = readl_relaxed(spi_st->base + SSC_RBUF);
  95. if (spi_st->rx_ptr) {
  96. if (spi_st->bytes_per_word == 1) {
  97. *spi_st->rx_ptr++ = (uint8_t)word;
  98. } else {
  99. *spi_st->rx_ptr++ = (word >> 8);
  100. *spi_st->rx_ptr++ = word & 0xff;
  101. }
  102. }
  103. }
  104. spi_st->words_remaining -= count;
  105. }
  106. static int spi_st_transfer_one(struct spi_master *master,
  107. struct spi_device *spi, struct spi_transfer *t)
  108. {
  109. struct spi_st *spi_st = spi_master_get_devdata(master);
  110. uint32_t ctl = 0;
  111. /* Setup transfer */
  112. spi_st->tx_ptr = t->tx_buf;
  113. spi_st->rx_ptr = t->rx_buf;
  114. if (spi->bits_per_word > 8) {
  115. /*
  116. * Anything greater than 8 bits-per-word requires 2
  117. * bytes-per-word in the RX/TX buffers
  118. */
  119. spi_st->bytes_per_word = 2;
  120. spi_st->words_remaining = t->len / 2;
  121. } else if (spi->bits_per_word == 8 && !(t->len & 0x1)) {
  122. /*
  123. * If transfer is even-length, and 8 bits-per-word, then
  124. * implement as half-length 16 bits-per-word transfer
  125. */
  126. spi_st->bytes_per_word = 2;
  127. spi_st->words_remaining = t->len / 2;
  128. /* Set SSC_CTL to 16 bits-per-word */
  129. ctl = readl_relaxed(spi_st->base + SSC_CTL);
  130. writel_relaxed((ctl | 0xf), spi_st->base + SSC_CTL);
  131. readl_relaxed(spi_st->base + SSC_RBUF);
  132. } else {
  133. spi_st->bytes_per_word = 1;
  134. spi_st->words_remaining = t->len;
  135. }
  136. reinit_completion(&spi_st->done);
  137. /* Start transfer by writing to the TX FIFO */
  138. ssc_write_tx_fifo(spi_st);
  139. writel_relaxed(SSC_IEN_TEEN, spi_st->base + SSC_IEN);
  140. /* Wait for transfer to complete */
  141. wait_for_completion(&spi_st->done);
  142. /* Restore SSC_CTL if necessary */
  143. if (ctl)
  144. writel_relaxed(ctl, spi_st->base + SSC_CTL);
  145. spi_finalize_current_transfer(spi->master);
  146. return t->len;
  147. }
  148. static void spi_st_cleanup(struct spi_device *spi)
  149. {
  150. gpio_free(spi->cs_gpio);
  151. }
  152. /* the spi->mode bits understood by this driver: */
  153. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_LOOP | SPI_CS_HIGH)
  154. static int spi_st_setup(struct spi_device *spi)
  155. {
  156. struct spi_st *spi_st = spi_master_get_devdata(spi->master);
  157. u32 spi_st_clk, sscbrg, var;
  158. u32 hz = spi->max_speed_hz;
  159. int cs = spi->cs_gpio;
  160. int ret;
  161. if (!hz) {
  162. dev_err(&spi->dev, "max_speed_hz unspecified\n");
  163. return -EINVAL;
  164. }
  165. if (!gpio_is_valid(cs)) {
  166. dev_err(&spi->dev, "%d is not a valid gpio\n", cs);
  167. return -EINVAL;
  168. }
  169. ret = gpio_request(cs, dev_name(&spi->dev));
  170. if (ret) {
  171. dev_err(&spi->dev, "could not request gpio:%d\n", cs);
  172. return ret;
  173. }
  174. ret = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
  175. if (ret)
  176. goto out_free_gpio;
  177. spi_st_clk = clk_get_rate(spi_st->clk);
  178. /* Set SSC_BRF */
  179. sscbrg = spi_st_clk / (2 * hz);
  180. if (sscbrg < 0x07 || sscbrg > BIT(16)) {
  181. dev_err(&spi->dev,
  182. "baudrate %d outside valid range %d\n", sscbrg, hz);
  183. ret = -EINVAL;
  184. goto out_free_gpio;
  185. }
  186. spi_st->baud = spi_st_clk / (2 * sscbrg);
  187. if (sscbrg == BIT(16)) /* 16-bit counter wraps */
  188. sscbrg = 0x0;
  189. writel_relaxed(sscbrg, spi_st->base + SSC_BRG);
  190. dev_dbg(&spi->dev,
  191. "setting baudrate:target= %u hz, actual= %u hz, sscbrg= %u\n",
  192. hz, spi_st->baud, sscbrg);
  193. /* Set SSC_CTL and enable SSC */
  194. var = readl_relaxed(spi_st->base + SSC_CTL);
  195. var |= SSC_CTL_MS;
  196. if (spi->mode & SPI_CPOL)
  197. var |= SSC_CTL_PO;
  198. else
  199. var &= ~SSC_CTL_PO;
  200. if (spi->mode & SPI_CPHA)
  201. var |= SSC_CTL_PH;
  202. else
  203. var &= ~SSC_CTL_PH;
  204. if ((spi->mode & SPI_LSB_FIRST) == 0)
  205. var |= SSC_CTL_HB;
  206. else
  207. var &= ~SSC_CTL_HB;
  208. if (spi->mode & SPI_LOOP)
  209. var |= SSC_CTL_LPB;
  210. else
  211. var &= ~SSC_CTL_LPB;
  212. var &= ~SSC_CTL_DATA_WIDTH_MSK;
  213. var |= (spi->bits_per_word - 1);
  214. var |= SSC_CTL_EN_TX_FIFO | SSC_CTL_EN_RX_FIFO;
  215. var |= SSC_CTL_EN;
  216. writel_relaxed(var, spi_st->base + SSC_CTL);
  217. /* Clear the status register */
  218. readl_relaxed(spi_st->base + SSC_RBUF);
  219. return 0;
  220. out_free_gpio:
  221. gpio_free(cs);
  222. return ret;
  223. }
  224. /* Interrupt fired when TX shift register becomes empty */
  225. static irqreturn_t spi_st_irq(int irq, void *dev_id)
  226. {
  227. struct spi_st *spi_st = (struct spi_st *)dev_id;
  228. /* Read RX FIFO */
  229. ssc_read_rx_fifo(spi_st);
  230. /* Fill TX FIFO */
  231. if (spi_st->words_remaining) {
  232. ssc_write_tx_fifo(spi_st);
  233. } else {
  234. /* TX/RX complete */
  235. writel_relaxed(0x0, spi_st->base + SSC_IEN);
  236. /*
  237. * read SSC_IEN to ensure that this bit is set
  238. * before re-enabling interrupt
  239. */
  240. readl(spi_st->base + SSC_IEN);
  241. complete(&spi_st->done);
  242. }
  243. return IRQ_HANDLED;
  244. }
  245. static int spi_st_probe(struct platform_device *pdev)
  246. {
  247. struct device_node *np = pdev->dev.of_node;
  248. struct spi_master *master;
  249. struct resource *res;
  250. struct spi_st *spi_st;
  251. int irq, ret = 0;
  252. u32 var;
  253. master = spi_alloc_master(&pdev->dev, sizeof(*spi_st));
  254. if (!master)
  255. return -ENOMEM;
  256. master->dev.of_node = np;
  257. master->mode_bits = MODEBITS;
  258. master->setup = spi_st_setup;
  259. master->cleanup = spi_st_cleanup;
  260. master->transfer_one = spi_st_transfer_one;
  261. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  262. master->auto_runtime_pm = true;
  263. master->bus_num = pdev->id;
  264. spi_st = spi_master_get_devdata(master);
  265. spi_st->clk = devm_clk_get(&pdev->dev, "ssc");
  266. if (IS_ERR(spi_st->clk)) {
  267. dev_err(&pdev->dev, "Unable to request clock\n");
  268. ret = PTR_ERR(spi_st->clk);
  269. goto put_master;
  270. }
  271. ret = clk_prepare_enable(spi_st->clk);
  272. if (ret)
  273. goto put_master;
  274. init_completion(&spi_st->done);
  275. /* Get resources */
  276. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  277. spi_st->base = devm_ioremap_resource(&pdev->dev, res);
  278. if (IS_ERR(spi_st->base)) {
  279. ret = PTR_ERR(spi_st->base);
  280. goto clk_disable;
  281. }
  282. /* Disable I2C and Reset SSC */
  283. writel_relaxed(0x0, spi_st->base + SSC_I2C);
  284. var = readw_relaxed(spi_st->base + SSC_CTL);
  285. var |= SSC_CTL_SR;
  286. writel_relaxed(var, spi_st->base + SSC_CTL);
  287. udelay(1);
  288. var = readl_relaxed(spi_st->base + SSC_CTL);
  289. var &= ~SSC_CTL_SR;
  290. writel_relaxed(var, spi_st->base + SSC_CTL);
  291. /* Set SSC into slave mode before reconfiguring PIO pins */
  292. var = readl_relaxed(spi_st->base + SSC_CTL);
  293. var &= ~SSC_CTL_MS;
  294. writel_relaxed(var, spi_st->base + SSC_CTL);
  295. irq = irq_of_parse_and_map(np, 0);
  296. if (!irq) {
  297. dev_err(&pdev->dev, "IRQ missing or invalid\n");
  298. ret = -EINVAL;
  299. goto clk_disable;
  300. }
  301. ret = devm_request_irq(&pdev->dev, irq, spi_st_irq, 0,
  302. pdev->name, spi_st);
  303. if (ret) {
  304. dev_err(&pdev->dev, "Failed to request irq %d\n", irq);
  305. goto clk_disable;
  306. }
  307. /* by default the device is on */
  308. pm_runtime_set_active(&pdev->dev);
  309. pm_runtime_enable(&pdev->dev);
  310. platform_set_drvdata(pdev, master);
  311. ret = devm_spi_register_master(&pdev->dev, master);
  312. if (ret) {
  313. dev_err(&pdev->dev, "Failed to register master\n");
  314. goto clk_disable;
  315. }
  316. return 0;
  317. clk_disable:
  318. pm_runtime_disable(&pdev->dev);
  319. clk_disable_unprepare(spi_st->clk);
  320. put_master:
  321. spi_master_put(master);
  322. return ret;
  323. }
  324. static int spi_st_remove(struct platform_device *pdev)
  325. {
  326. struct spi_master *master = platform_get_drvdata(pdev);
  327. struct spi_st *spi_st = spi_master_get_devdata(master);
  328. pm_runtime_disable(&pdev->dev);
  329. clk_disable_unprepare(spi_st->clk);
  330. pinctrl_pm_select_sleep_state(&pdev->dev);
  331. return 0;
  332. }
  333. #ifdef CONFIG_PM
  334. static int spi_st_runtime_suspend(struct device *dev)
  335. {
  336. struct spi_master *master = dev_get_drvdata(dev);
  337. struct spi_st *spi_st = spi_master_get_devdata(master);
  338. writel_relaxed(0, spi_st->base + SSC_IEN);
  339. pinctrl_pm_select_sleep_state(dev);
  340. clk_disable_unprepare(spi_st->clk);
  341. return 0;
  342. }
  343. static int spi_st_runtime_resume(struct device *dev)
  344. {
  345. struct spi_master *master = dev_get_drvdata(dev);
  346. struct spi_st *spi_st = spi_master_get_devdata(master);
  347. int ret;
  348. ret = clk_prepare_enable(spi_st->clk);
  349. pinctrl_pm_select_default_state(dev);
  350. return ret;
  351. }
  352. #endif
  353. #ifdef CONFIG_PM_SLEEP
  354. static int spi_st_suspend(struct device *dev)
  355. {
  356. struct spi_master *master = dev_get_drvdata(dev);
  357. int ret;
  358. ret = spi_master_suspend(master);
  359. if (ret)
  360. return ret;
  361. return pm_runtime_force_suspend(dev);
  362. }
  363. static int spi_st_resume(struct device *dev)
  364. {
  365. struct spi_master *master = dev_get_drvdata(dev);
  366. int ret;
  367. ret = spi_master_resume(master);
  368. if (ret)
  369. return ret;
  370. return pm_runtime_force_resume(dev);
  371. }
  372. #endif
  373. static const struct dev_pm_ops spi_st_pm = {
  374. SET_SYSTEM_SLEEP_PM_OPS(spi_st_suspend, spi_st_resume)
  375. SET_RUNTIME_PM_OPS(spi_st_runtime_suspend, spi_st_runtime_resume, NULL)
  376. };
  377. static const struct of_device_id stm_spi_match[] = {
  378. { .compatible = "st,comms-ssc4-spi", },
  379. {},
  380. };
  381. MODULE_DEVICE_TABLE(of, stm_spi_match);
  382. static struct platform_driver spi_st_driver = {
  383. .driver = {
  384. .name = "spi-st",
  385. .pm = &spi_st_pm,
  386. .of_match_table = of_match_ptr(stm_spi_match),
  387. },
  388. .probe = spi_st_probe,
  389. .remove = spi_st_remove,
  390. };
  391. module_platform_driver(spi_st_driver);
  392. MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
  393. MODULE_DESCRIPTION("STM SSC SPI driver");
  394. MODULE_LICENSE("GPL v2");