stm32-timers.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics 2016
  4. * Author: Benjamin Gaignard <benjamin.gaignard@st.com>
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/mfd/stm32-timers.h>
  8. #include <linux/module.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/reset.h>
  11. #define STM32_TIMERS_MAX_REGISTERS 0x3fc
  12. /* DIER register DMA enable bits */
  13. static const u32 stm32_timers_dier_dmaen[STM32_TIMERS_MAX_DMAS] = {
  14. TIM_DIER_CC1DE,
  15. TIM_DIER_CC2DE,
  16. TIM_DIER_CC3DE,
  17. TIM_DIER_CC4DE,
  18. TIM_DIER_UIE,
  19. TIM_DIER_TDE,
  20. TIM_DIER_COMDE
  21. };
  22. static void stm32_timers_dma_done(void *p)
  23. {
  24. struct stm32_timers_dma *dma = p;
  25. struct dma_tx_state state;
  26. enum dma_status status;
  27. status = dmaengine_tx_status(dma->chan, dma->chan->cookie, &state);
  28. if (status == DMA_COMPLETE)
  29. complete(&dma->completion);
  30. }
  31. /**
  32. * stm32_timers_dma_burst_read - Read from timers registers using DMA.
  33. *
  34. * Read from STM32 timers registers using DMA on a single event.
  35. * @dev: reference to stm32_timers MFD device
  36. * @buf: DMA'able destination buffer
  37. * @id: stm32_timers_dmas event identifier (ch[1..4], up, trig or com)
  38. * @reg: registers start offset for DMA to read from (like CCRx for capture)
  39. * @num_reg: number of registers to read upon each DMA request, starting @reg.
  40. * @bursts: number of bursts to read (e.g. like two for pwm period capture)
  41. * @tmo_ms: timeout (milliseconds)
  42. */
  43. int stm32_timers_dma_burst_read(struct device *dev, u32 *buf,
  44. enum stm32_timers_dmas id, u32 reg,
  45. unsigned int num_reg, unsigned int bursts,
  46. unsigned long tmo_ms)
  47. {
  48. struct stm32_timers *ddata = dev_get_drvdata(dev);
  49. unsigned long timeout = msecs_to_jiffies(tmo_ms);
  50. struct regmap *regmap = ddata->regmap;
  51. struct stm32_timers_dma *dma = &ddata->dma;
  52. size_t len = num_reg * bursts * sizeof(u32);
  53. struct dma_async_tx_descriptor *desc;
  54. struct dma_slave_config config;
  55. dma_cookie_t cookie;
  56. dma_addr_t dma_buf;
  57. u32 dbl, dba;
  58. long err;
  59. int ret;
  60. /* Sanity check */
  61. if (id < STM32_TIMERS_DMA_CH1 || id >= STM32_TIMERS_MAX_DMAS)
  62. return -EINVAL;
  63. if (!num_reg || !bursts || reg > STM32_TIMERS_MAX_REGISTERS ||
  64. (reg + num_reg * sizeof(u32)) > STM32_TIMERS_MAX_REGISTERS)
  65. return -EINVAL;
  66. if (!dma->chans[id])
  67. return -ENODEV;
  68. mutex_lock(&dma->lock);
  69. /* Select DMA channel in use */
  70. dma->chan = dma->chans[id];
  71. dma_buf = dma_map_single(dev, buf, len, DMA_FROM_DEVICE);
  72. if (dma_mapping_error(dev, dma_buf)) {
  73. ret = -ENOMEM;
  74. goto unlock;
  75. }
  76. /* Prepare DMA read from timer registers, using DMA burst mode */
  77. memset(&config, 0, sizeof(config));
  78. config.src_addr = (dma_addr_t)dma->phys_base + TIM_DMAR;
  79. config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  80. ret = dmaengine_slave_config(dma->chan, &config);
  81. if (ret)
  82. goto unmap;
  83. desc = dmaengine_prep_slave_single(dma->chan, dma_buf, len,
  84. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
  85. if (!desc) {
  86. ret = -EBUSY;
  87. goto unmap;
  88. }
  89. desc->callback = stm32_timers_dma_done;
  90. desc->callback_param = dma;
  91. cookie = dmaengine_submit(desc);
  92. ret = dma_submit_error(cookie);
  93. if (ret)
  94. goto dma_term;
  95. reinit_completion(&dma->completion);
  96. dma_async_issue_pending(dma->chan);
  97. /* Setup and enable timer DMA burst mode */
  98. dbl = FIELD_PREP(TIM_DCR_DBL, bursts - 1);
  99. dba = FIELD_PREP(TIM_DCR_DBA, reg >> 2);
  100. ret = regmap_write(regmap, TIM_DCR, dbl | dba);
  101. if (ret)
  102. goto dma_term;
  103. /* Clear pending flags before enabling DMA request */
  104. ret = regmap_write(regmap, TIM_SR, 0);
  105. if (ret)
  106. goto dcr_clr;
  107. ret = regmap_update_bits(regmap, TIM_DIER, stm32_timers_dier_dmaen[id],
  108. stm32_timers_dier_dmaen[id]);
  109. if (ret)
  110. goto dcr_clr;
  111. err = wait_for_completion_interruptible_timeout(&dma->completion,
  112. timeout);
  113. if (err == 0)
  114. ret = -ETIMEDOUT;
  115. else if (err < 0)
  116. ret = err;
  117. regmap_update_bits(regmap, TIM_DIER, stm32_timers_dier_dmaen[id], 0);
  118. regmap_write(regmap, TIM_SR, 0);
  119. dcr_clr:
  120. regmap_write(regmap, TIM_DCR, 0);
  121. dma_term:
  122. dmaengine_terminate_all(dma->chan);
  123. unmap:
  124. dma_unmap_single(dev, dma_buf, len, DMA_FROM_DEVICE);
  125. unlock:
  126. dma->chan = NULL;
  127. mutex_unlock(&dma->lock);
  128. return ret;
  129. }
  130. EXPORT_SYMBOL_GPL(stm32_timers_dma_burst_read);
  131. static const struct regmap_config stm32_timers_regmap_cfg = {
  132. .reg_bits = 32,
  133. .val_bits = 32,
  134. .reg_stride = sizeof(u32),
  135. .max_register = STM32_TIMERS_MAX_REGISTERS,
  136. };
  137. static void stm32_timers_get_arr_size(struct stm32_timers *ddata)
  138. {
  139. /*
  140. * Only the available bits will be written so when readback
  141. * we get the maximum value of auto reload register
  142. */
  143. regmap_write(ddata->regmap, TIM_ARR, ~0L);
  144. regmap_read(ddata->regmap, TIM_ARR, &ddata->max_arr);
  145. regmap_write(ddata->regmap, TIM_ARR, 0x0);
  146. }
  147. static void stm32_timers_dma_probe(struct device *dev,
  148. struct stm32_timers *ddata)
  149. {
  150. int i;
  151. char name[4];
  152. init_completion(&ddata->dma.completion);
  153. mutex_init(&ddata->dma.lock);
  154. /* Optional DMA support: get valid DMA channel(s) or NULL */
  155. for (i = STM32_TIMERS_DMA_CH1; i <= STM32_TIMERS_DMA_CH4; i++) {
  156. snprintf(name, ARRAY_SIZE(name), "ch%1d", i + 1);
  157. ddata->dma.chans[i] = dma_request_slave_channel(dev, name);
  158. }
  159. ddata->dma.chans[STM32_TIMERS_DMA_UP] =
  160. dma_request_slave_channel(dev, "up");
  161. ddata->dma.chans[STM32_TIMERS_DMA_TRIG] =
  162. dma_request_slave_channel(dev, "trig");
  163. ddata->dma.chans[STM32_TIMERS_DMA_COM] =
  164. dma_request_slave_channel(dev, "com");
  165. }
  166. static void stm32_timers_dma_remove(struct device *dev,
  167. struct stm32_timers *ddata)
  168. {
  169. int i;
  170. for (i = STM32_TIMERS_DMA_CH1; i < STM32_TIMERS_MAX_DMAS; i++)
  171. if (ddata->dma.chans[i])
  172. dma_release_channel(ddata->dma.chans[i]);
  173. }
  174. static int stm32_timers_probe(struct platform_device *pdev)
  175. {
  176. struct device *dev = &pdev->dev;
  177. struct stm32_timers *ddata;
  178. struct resource *res;
  179. void __iomem *mmio;
  180. int ret;
  181. ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
  182. if (!ddata)
  183. return -ENOMEM;
  184. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  185. mmio = devm_ioremap_resource(dev, res);
  186. if (IS_ERR(mmio))
  187. return PTR_ERR(mmio);
  188. /* Timer physical addr for DMA */
  189. ddata->dma.phys_base = res->start;
  190. ddata->regmap = devm_regmap_init_mmio_clk(dev, "int", mmio,
  191. &stm32_timers_regmap_cfg);
  192. if (IS_ERR(ddata->regmap))
  193. return PTR_ERR(ddata->regmap);
  194. ddata->clk = devm_clk_get(dev, NULL);
  195. if (IS_ERR(ddata->clk))
  196. return PTR_ERR(ddata->clk);
  197. stm32_timers_get_arr_size(ddata);
  198. stm32_timers_dma_probe(dev, ddata);
  199. platform_set_drvdata(pdev, ddata);
  200. ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  201. if (ret)
  202. stm32_timers_dma_remove(dev, ddata);
  203. return ret;
  204. }
  205. static int stm32_timers_remove(struct platform_device *pdev)
  206. {
  207. struct stm32_timers *ddata = platform_get_drvdata(pdev);
  208. /*
  209. * Don't use devm_ here: enfore of_platform_depopulate() happens before
  210. * DMA are released, to avoid race on DMA.
  211. */
  212. of_platform_depopulate(&pdev->dev);
  213. stm32_timers_dma_remove(&pdev->dev, ddata);
  214. return 0;
  215. }
  216. static const struct of_device_id stm32_timers_of_match[] = {
  217. { .compatible = "st,stm32-timers", },
  218. { /* end node */ },
  219. };
  220. MODULE_DEVICE_TABLE(of, stm32_timers_of_match);
  221. static struct platform_driver stm32_timers_driver = {
  222. .probe = stm32_timers_probe,
  223. .remove = stm32_timers_remove,
  224. .driver = {
  225. .name = "stm32-timers",
  226. .of_match_table = stm32_timers_of_match,
  227. },
  228. };
  229. module_platform_driver(stm32_timers_driver);
  230. MODULE_DESCRIPTION("STMicroelectronics STM32 Timers");
  231. MODULE_LICENSE("GPL v2");