spi-st-ssc4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. static int spi_st_clk_enable(struct spi_st *spi_st)
  64. {
  65. /*
  66. * Current platforms use one of the core clocks for SPI and I2C.
  67. * If we attempt to disable the clock, the system will hang.
  68. *
  69. * TODO: Remove this when platform supports power domains.
  70. */
  71. return 0;
  72. return clk_prepare_enable(spi_st->clk);
  73. }
  74. static void spi_st_clk_disable(struct spi_st *spi_st)
  75. {
  76. /*
  77. * Current platforms use one of the core clocks for SPI and I2C.
  78. * If we attempt to disable the clock, the system will hang.
  79. *
  80. * TODO: Remove this when platform supports power domains.
  81. */
  82. return;
  83. clk_disable_unprepare(spi_st->clk);
  84. }
  85. /* Load the TX FIFO */
  86. static void ssc_write_tx_fifo(struct spi_st *spi_st)
  87. {
  88. unsigned int count, i;
  89. uint32_t word = 0;
  90. if (spi_st->words_remaining > FIFO_SIZE)
  91. count = FIFO_SIZE;
  92. else
  93. count = spi_st->words_remaining;
  94. for (i = 0; i < count; i++) {
  95. if (spi_st->tx_ptr) {
  96. if (spi_st->bytes_per_word == 1) {
  97. word = *spi_st->tx_ptr++;
  98. } else {
  99. word = *spi_st->tx_ptr++;
  100. word = *spi_st->tx_ptr++ | (word << 8);
  101. }
  102. }
  103. writel_relaxed(word, spi_st->base + SSC_TBUF);
  104. }
  105. }
  106. /* Read the RX FIFO */
  107. static void ssc_read_rx_fifo(struct spi_st *spi_st)
  108. {
  109. unsigned int count, i;
  110. uint32_t word = 0;
  111. if (spi_st->words_remaining > FIFO_SIZE)
  112. count = FIFO_SIZE;
  113. else
  114. count = spi_st->words_remaining;
  115. for (i = 0; i < count; i++) {
  116. word = readl_relaxed(spi_st->base + SSC_RBUF);
  117. if (spi_st->rx_ptr) {
  118. if (spi_st->bytes_per_word == 1) {
  119. *spi_st->rx_ptr++ = (uint8_t)word;
  120. } else {
  121. *spi_st->rx_ptr++ = (word >> 8);
  122. *spi_st->rx_ptr++ = word & 0xff;
  123. }
  124. }
  125. }
  126. spi_st->words_remaining -= count;
  127. }
  128. static int spi_st_transfer_one(struct spi_master *master,
  129. struct spi_device *spi, struct spi_transfer *t)
  130. {
  131. struct spi_st *spi_st = spi_master_get_devdata(master);
  132. uint32_t ctl = 0;
  133. /* Setup transfer */
  134. spi_st->tx_ptr = t->tx_buf;
  135. spi_st->rx_ptr = t->rx_buf;
  136. if (spi->bits_per_word > 8) {
  137. /*
  138. * Anything greater than 8 bits-per-word requires 2
  139. * bytes-per-word in the RX/TX buffers
  140. */
  141. spi_st->bytes_per_word = 2;
  142. spi_st->words_remaining = t->len / 2;
  143. } else if (spi->bits_per_word == 8 && !(t->len & 0x1)) {
  144. /*
  145. * If transfer is even-length, and 8 bits-per-word, then
  146. * implement as half-length 16 bits-per-word transfer
  147. */
  148. spi_st->bytes_per_word = 2;
  149. spi_st->words_remaining = t->len / 2;
  150. /* Set SSC_CTL to 16 bits-per-word */
  151. ctl = readl_relaxed(spi_st->base + SSC_CTL);
  152. writel_relaxed((ctl | 0xf), spi_st->base + SSC_CTL);
  153. readl_relaxed(spi_st->base + SSC_RBUF);
  154. } else {
  155. spi_st->bytes_per_word = 1;
  156. spi_st->words_remaining = t->len;
  157. }
  158. reinit_completion(&spi_st->done);
  159. /* Start transfer by writing to the TX FIFO */
  160. ssc_write_tx_fifo(spi_st);
  161. writel_relaxed(SSC_IEN_TEEN, spi_st->base + SSC_IEN);
  162. /* Wait for transfer to complete */
  163. wait_for_completion(&spi_st->done);
  164. /* Restore SSC_CTL if necessary */
  165. if (ctl)
  166. writel_relaxed(ctl, spi_st->base + SSC_CTL);
  167. spi_finalize_current_transfer(spi->master);
  168. return t->len;
  169. }
  170. static void spi_st_cleanup(struct spi_device *spi)
  171. {
  172. int cs = spi->cs_gpio;
  173. if (gpio_is_valid(cs))
  174. devm_gpio_free(&spi->dev, cs);
  175. }
  176. /* the spi->mode bits understood by this driver: */
  177. #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_LOOP | SPI_CS_HIGH)
  178. static int spi_st_setup(struct spi_device *spi)
  179. {
  180. struct spi_st *spi_st = spi_master_get_devdata(spi->master);
  181. u32 spi_st_clk, sscbrg, var;
  182. u32 hz = spi->max_speed_hz;
  183. int cs = spi->cs_gpio;
  184. int ret;
  185. if (!hz) {
  186. dev_err(&spi->dev, "max_speed_hz unspecified\n");
  187. return -EINVAL;
  188. }
  189. if (!gpio_is_valid(cs)) {
  190. dev_err(&spi->dev, "%d is not a valid gpio\n", cs);
  191. return -EINVAL;
  192. }
  193. if (devm_gpio_request(&spi->dev, cs, dev_name(&spi->dev))) {
  194. dev_err(&spi->dev, "could not request gpio:%d\n", cs);
  195. return -EINVAL;
  196. }
  197. ret = gpio_direction_output(cs, spi->mode & SPI_CS_HIGH);
  198. if (ret)
  199. return ret;
  200. spi_st_clk = clk_get_rate(spi_st->clk);
  201. /* Set SSC_BRF */
  202. sscbrg = spi_st_clk / (2 * hz);
  203. if (sscbrg < 0x07 || sscbrg > BIT(16)) {
  204. dev_err(&spi->dev,
  205. "baudrate %d outside valid range %d\n", sscbrg, hz);
  206. return -EINVAL;
  207. }
  208. spi_st->baud = spi_st_clk / (2 * sscbrg);
  209. if (sscbrg == BIT(16)) /* 16-bit counter wraps */
  210. sscbrg = 0x0;
  211. writel_relaxed(sscbrg, spi_st->base + SSC_BRG);
  212. dev_dbg(&spi->dev,
  213. "setting baudrate:target= %u hz, actual= %u hz, sscbrg= %u\n",
  214. hz, spi_st->baud, sscbrg);
  215. /* Set SSC_CTL and enable SSC */
  216. var = readl_relaxed(spi_st->base + SSC_CTL);
  217. var |= SSC_CTL_MS;
  218. if (spi->mode & SPI_CPOL)
  219. var |= SSC_CTL_PO;
  220. else
  221. var &= ~SSC_CTL_PO;
  222. if (spi->mode & SPI_CPHA)
  223. var |= SSC_CTL_PH;
  224. else
  225. var &= ~SSC_CTL_PH;
  226. if ((spi->mode & SPI_LSB_FIRST) == 0)
  227. var |= SSC_CTL_HB;
  228. else
  229. var &= ~SSC_CTL_HB;
  230. if (spi->mode & SPI_LOOP)
  231. var |= SSC_CTL_LPB;
  232. else
  233. var &= ~SSC_CTL_LPB;
  234. var &= ~SSC_CTL_DATA_WIDTH_MSK;
  235. var |= (spi->bits_per_word - 1);
  236. var |= SSC_CTL_EN_TX_FIFO | SSC_CTL_EN_RX_FIFO;
  237. var |= SSC_CTL_EN;
  238. writel_relaxed(var, spi_st->base + SSC_CTL);
  239. /* Clear the status register */
  240. readl_relaxed(spi_st->base + SSC_RBUF);
  241. return 0;
  242. }
  243. /* Interrupt fired when TX shift register becomes empty */
  244. static irqreturn_t spi_st_irq(int irq, void *dev_id)
  245. {
  246. struct spi_st *spi_st = (struct spi_st *)dev_id;
  247. /* Read RX FIFO */
  248. ssc_read_rx_fifo(spi_st);
  249. /* Fill TX FIFO */
  250. if (spi_st->words_remaining) {
  251. ssc_write_tx_fifo(spi_st);
  252. } else {
  253. /* TX/RX complete */
  254. writel_relaxed(0x0, spi_st->base + SSC_IEN);
  255. /*
  256. * read SSC_IEN to ensure that this bit is set
  257. * before re-enabling interrupt
  258. */
  259. readl(spi_st->base + SSC_IEN);
  260. complete(&spi_st->done);
  261. }
  262. return IRQ_HANDLED;
  263. }
  264. static int spi_st_probe(struct platform_device *pdev)
  265. {
  266. struct device_node *np = pdev->dev.of_node;
  267. struct spi_master *master;
  268. struct resource *res;
  269. struct spi_st *spi_st;
  270. int irq, ret = 0;
  271. u32 var;
  272. master = spi_alloc_master(&pdev->dev, sizeof(*spi_st));
  273. if (!master)
  274. return -ENOMEM;
  275. master->dev.of_node = np;
  276. master->mode_bits = MODEBITS;
  277. master->setup = spi_st_setup;
  278. master->cleanup = spi_st_cleanup;
  279. master->transfer_one = spi_st_transfer_one;
  280. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  281. master->auto_runtime_pm = true;
  282. master->bus_num = pdev->id;
  283. spi_st = spi_master_get_devdata(master);
  284. spi_st->clk = devm_clk_get(&pdev->dev, "ssc");
  285. if (IS_ERR(spi_st->clk)) {
  286. dev_err(&pdev->dev, "Unable to request clock\n");
  287. return PTR_ERR(spi_st->clk);
  288. }
  289. ret = spi_st_clk_enable(spi_st);
  290. if (ret)
  291. return ret;
  292. init_completion(&spi_st->done);
  293. /* Get resources */
  294. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  295. spi_st->base = devm_ioremap_resource(&pdev->dev, res);
  296. if (IS_ERR(spi_st->base)) {
  297. ret = PTR_ERR(spi_st->base);
  298. goto clk_disable;
  299. }
  300. /* Disable I2C and Reset SSC */
  301. writel_relaxed(0x0, spi_st->base + SSC_I2C);
  302. var = readw_relaxed(spi_st->base + SSC_CTL);
  303. var |= SSC_CTL_SR;
  304. writel_relaxed(var, spi_st->base + SSC_CTL);
  305. udelay(1);
  306. var = readl_relaxed(spi_st->base + SSC_CTL);
  307. var &= ~SSC_CTL_SR;
  308. writel_relaxed(var, spi_st->base + SSC_CTL);
  309. /* Set SSC into slave mode before reconfiguring PIO pins */
  310. var = readl_relaxed(spi_st->base + SSC_CTL);
  311. var &= ~SSC_CTL_MS;
  312. writel_relaxed(var, spi_st->base + SSC_CTL);
  313. irq = irq_of_parse_and_map(np, 0);
  314. if (!irq) {
  315. dev_err(&pdev->dev, "IRQ missing or invalid\n");
  316. ret = -EINVAL;
  317. goto clk_disable;
  318. }
  319. ret = devm_request_irq(&pdev->dev, irq, spi_st_irq, 0,
  320. pdev->name, spi_st);
  321. if (ret) {
  322. dev_err(&pdev->dev, "Failed to request irq %d\n", irq);
  323. goto clk_disable;
  324. }
  325. /* by default the device is on */
  326. pm_runtime_set_active(&pdev->dev);
  327. pm_runtime_enable(&pdev->dev);
  328. platform_set_drvdata(pdev, master);
  329. ret = devm_spi_register_master(&pdev->dev, master);
  330. if (ret) {
  331. dev_err(&pdev->dev, "Failed to register master\n");
  332. goto clk_disable;
  333. }
  334. return 0;
  335. clk_disable:
  336. spi_st_clk_disable(spi_st);
  337. return ret;
  338. }
  339. static int spi_st_remove(struct platform_device *pdev)
  340. {
  341. struct spi_master *master = platform_get_drvdata(pdev);
  342. struct spi_st *spi_st = spi_master_get_devdata(master);
  343. spi_st_clk_disable(spi_st);
  344. pinctrl_pm_select_sleep_state(&pdev->dev);
  345. return 0;
  346. }
  347. #ifdef CONFIG_PM
  348. static int spi_st_runtime_suspend(struct device *dev)
  349. {
  350. struct spi_master *master = dev_get_drvdata(dev);
  351. struct spi_st *spi_st = spi_master_get_devdata(master);
  352. writel_relaxed(0, spi_st->base + SSC_IEN);
  353. pinctrl_pm_select_sleep_state(dev);
  354. spi_st_clk_disable(spi_st);
  355. return 0;
  356. }
  357. static int spi_st_runtime_resume(struct device *dev)
  358. {
  359. struct spi_master *master = dev_get_drvdata(dev);
  360. struct spi_st *spi_st = spi_master_get_devdata(master);
  361. int ret;
  362. ret = spi_st_clk_enable(spi_st);
  363. pinctrl_pm_select_default_state(dev);
  364. return ret;
  365. }
  366. #endif
  367. #ifdef CONFIG_PM_SLEEP
  368. static int spi_st_suspend(struct device *dev)
  369. {
  370. struct spi_master *master = dev_get_drvdata(dev);
  371. int ret;
  372. ret = spi_master_suspend(master);
  373. if (ret)
  374. return ret;
  375. return pm_runtime_force_suspend(dev);
  376. }
  377. static int spi_st_resume(struct device *dev)
  378. {
  379. struct spi_master *master = dev_get_drvdata(dev);
  380. int ret;
  381. ret = spi_master_resume(master);
  382. if (ret)
  383. return ret;
  384. return pm_runtime_force_resume(dev);
  385. }
  386. #endif
  387. static const struct dev_pm_ops spi_st_pm = {
  388. SET_SYSTEM_SLEEP_PM_OPS(spi_st_suspend, spi_st_resume)
  389. SET_RUNTIME_PM_OPS(spi_st_runtime_suspend, spi_st_runtime_resume, NULL)
  390. };
  391. static const struct of_device_id stm_spi_match[] = {
  392. { .compatible = "st,comms-ssc4-spi", },
  393. {},
  394. };
  395. MODULE_DEVICE_TABLE(of, stm_spi_match);
  396. static struct platform_driver spi_st_driver = {
  397. .driver = {
  398. .name = "spi-st",
  399. .pm = &spi_st_pm,
  400. .of_match_table = of_match_ptr(stm_spi_match),
  401. },
  402. .probe = spi_st_probe,
  403. .remove = spi_st_remove,
  404. };
  405. module_platform_driver(spi_st_driver);
  406. MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
  407. MODULE_DESCRIPTION("STM SSC SPI driver");
  408. MODULE_LICENSE("GPL v2");