18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) STMicroelectronics 2016 48c2ecf20Sopenharmony_ci * Author: Benjamin Gaignard <benjamin.gaignard@st.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/bitfield.h> 88c2ecf20Sopenharmony_ci#include <linux/mfd/stm32-timers.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 118c2ecf20Sopenharmony_ci#include <linux/reset.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#define STM32_TIMERS_MAX_REGISTERS 0x3fc 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci/* DIER register DMA enable bits */ 168c2ecf20Sopenharmony_cistatic const u32 stm32_timers_dier_dmaen[STM32_TIMERS_MAX_DMAS] = { 178c2ecf20Sopenharmony_ci TIM_DIER_CC1DE, 188c2ecf20Sopenharmony_ci TIM_DIER_CC2DE, 198c2ecf20Sopenharmony_ci TIM_DIER_CC3DE, 208c2ecf20Sopenharmony_ci TIM_DIER_CC4DE, 218c2ecf20Sopenharmony_ci TIM_DIER_UIE, 228c2ecf20Sopenharmony_ci TIM_DIER_TDE, 238c2ecf20Sopenharmony_ci TIM_DIER_COMDE 248c2ecf20Sopenharmony_ci}; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic void stm32_timers_dma_done(void *p) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci struct stm32_timers_dma *dma = p; 298c2ecf20Sopenharmony_ci struct dma_tx_state state; 308c2ecf20Sopenharmony_ci enum dma_status status; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci status = dmaengine_tx_status(dma->chan, dma->chan->cookie, &state); 338c2ecf20Sopenharmony_ci if (status == DMA_COMPLETE) 348c2ecf20Sopenharmony_ci complete(&dma->completion); 358c2ecf20Sopenharmony_ci} 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/** 388c2ecf20Sopenharmony_ci * stm32_timers_dma_burst_read - Read from timers registers using DMA. 398c2ecf20Sopenharmony_ci * 408c2ecf20Sopenharmony_ci * Read from STM32 timers registers using DMA on a single event. 418c2ecf20Sopenharmony_ci * @dev: reference to stm32_timers MFD device 428c2ecf20Sopenharmony_ci * @buf: DMA'able destination buffer 438c2ecf20Sopenharmony_ci * @id: stm32_timers_dmas event identifier (ch[1..4], up, trig or com) 448c2ecf20Sopenharmony_ci * @reg: registers start offset for DMA to read from (like CCRx for capture) 458c2ecf20Sopenharmony_ci * @num_reg: number of registers to read upon each DMA request, starting @reg. 468c2ecf20Sopenharmony_ci * @bursts: number of bursts to read (e.g. like two for pwm period capture) 478c2ecf20Sopenharmony_ci * @tmo_ms: timeout (milliseconds) 488c2ecf20Sopenharmony_ci */ 498c2ecf20Sopenharmony_ciint stm32_timers_dma_burst_read(struct device *dev, u32 *buf, 508c2ecf20Sopenharmony_ci enum stm32_timers_dmas id, u32 reg, 518c2ecf20Sopenharmony_ci unsigned int num_reg, unsigned int bursts, 528c2ecf20Sopenharmony_ci unsigned long tmo_ms) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci struct stm32_timers *ddata = dev_get_drvdata(dev); 558c2ecf20Sopenharmony_ci unsigned long timeout = msecs_to_jiffies(tmo_ms); 568c2ecf20Sopenharmony_ci struct regmap *regmap = ddata->regmap; 578c2ecf20Sopenharmony_ci struct stm32_timers_dma *dma = &ddata->dma; 588c2ecf20Sopenharmony_ci size_t len = num_reg * bursts * sizeof(u32); 598c2ecf20Sopenharmony_ci struct dma_async_tx_descriptor *desc; 608c2ecf20Sopenharmony_ci struct dma_slave_config config; 618c2ecf20Sopenharmony_ci dma_cookie_t cookie; 628c2ecf20Sopenharmony_ci dma_addr_t dma_buf; 638c2ecf20Sopenharmony_ci u32 dbl, dba; 648c2ecf20Sopenharmony_ci long err; 658c2ecf20Sopenharmony_ci int ret; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci /* Sanity check */ 688c2ecf20Sopenharmony_ci if (id < STM32_TIMERS_DMA_CH1 || id >= STM32_TIMERS_MAX_DMAS) 698c2ecf20Sopenharmony_ci return -EINVAL; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci if (!num_reg || !bursts || reg > STM32_TIMERS_MAX_REGISTERS || 728c2ecf20Sopenharmony_ci (reg + num_reg * sizeof(u32)) > STM32_TIMERS_MAX_REGISTERS) 738c2ecf20Sopenharmony_ci return -EINVAL; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci if (!dma->chans[id]) 768c2ecf20Sopenharmony_ci return -ENODEV; 778c2ecf20Sopenharmony_ci mutex_lock(&dma->lock); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci /* Select DMA channel in use */ 808c2ecf20Sopenharmony_ci dma->chan = dma->chans[id]; 818c2ecf20Sopenharmony_ci dma_buf = dma_map_single(dev, buf, len, DMA_FROM_DEVICE); 828c2ecf20Sopenharmony_ci if (dma_mapping_error(dev, dma_buf)) { 838c2ecf20Sopenharmony_ci ret = -ENOMEM; 848c2ecf20Sopenharmony_ci goto unlock; 858c2ecf20Sopenharmony_ci } 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci /* Prepare DMA read from timer registers, using DMA burst mode */ 888c2ecf20Sopenharmony_ci memset(&config, 0, sizeof(config)); 898c2ecf20Sopenharmony_ci config.src_addr = (dma_addr_t)dma->phys_base + TIM_DMAR; 908c2ecf20Sopenharmony_ci config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 918c2ecf20Sopenharmony_ci ret = dmaengine_slave_config(dma->chan, &config); 928c2ecf20Sopenharmony_ci if (ret) 938c2ecf20Sopenharmony_ci goto unmap; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci desc = dmaengine_prep_slave_single(dma->chan, dma_buf, len, 968c2ecf20Sopenharmony_ci DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT); 978c2ecf20Sopenharmony_ci if (!desc) { 988c2ecf20Sopenharmony_ci ret = -EBUSY; 998c2ecf20Sopenharmony_ci goto unmap; 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci desc->callback = stm32_timers_dma_done; 1038c2ecf20Sopenharmony_ci desc->callback_param = dma; 1048c2ecf20Sopenharmony_ci cookie = dmaengine_submit(desc); 1058c2ecf20Sopenharmony_ci ret = dma_submit_error(cookie); 1068c2ecf20Sopenharmony_ci if (ret) 1078c2ecf20Sopenharmony_ci goto dma_term; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci reinit_completion(&dma->completion); 1108c2ecf20Sopenharmony_ci dma_async_issue_pending(dma->chan); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci /* Setup and enable timer DMA burst mode */ 1138c2ecf20Sopenharmony_ci dbl = FIELD_PREP(TIM_DCR_DBL, bursts - 1); 1148c2ecf20Sopenharmony_ci dba = FIELD_PREP(TIM_DCR_DBA, reg >> 2); 1158c2ecf20Sopenharmony_ci ret = regmap_write(regmap, TIM_DCR, dbl | dba); 1168c2ecf20Sopenharmony_ci if (ret) 1178c2ecf20Sopenharmony_ci goto dma_term; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci /* Clear pending flags before enabling DMA request */ 1208c2ecf20Sopenharmony_ci ret = regmap_write(regmap, TIM_SR, 0); 1218c2ecf20Sopenharmony_ci if (ret) 1228c2ecf20Sopenharmony_ci goto dcr_clr; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci ret = regmap_update_bits(regmap, TIM_DIER, stm32_timers_dier_dmaen[id], 1258c2ecf20Sopenharmony_ci stm32_timers_dier_dmaen[id]); 1268c2ecf20Sopenharmony_ci if (ret) 1278c2ecf20Sopenharmony_ci goto dcr_clr; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci err = wait_for_completion_interruptible_timeout(&dma->completion, 1308c2ecf20Sopenharmony_ci timeout); 1318c2ecf20Sopenharmony_ci if (err == 0) 1328c2ecf20Sopenharmony_ci ret = -ETIMEDOUT; 1338c2ecf20Sopenharmony_ci else if (err < 0) 1348c2ecf20Sopenharmony_ci ret = err; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci regmap_update_bits(regmap, TIM_DIER, stm32_timers_dier_dmaen[id], 0); 1378c2ecf20Sopenharmony_ci regmap_write(regmap, TIM_SR, 0); 1388c2ecf20Sopenharmony_cidcr_clr: 1398c2ecf20Sopenharmony_ci regmap_write(regmap, TIM_DCR, 0); 1408c2ecf20Sopenharmony_cidma_term: 1418c2ecf20Sopenharmony_ci dmaengine_terminate_all(dma->chan); 1428c2ecf20Sopenharmony_ciunmap: 1438c2ecf20Sopenharmony_ci dma_unmap_single(dev, dma_buf, len, DMA_FROM_DEVICE); 1448c2ecf20Sopenharmony_ciunlock: 1458c2ecf20Sopenharmony_ci dma->chan = NULL; 1468c2ecf20Sopenharmony_ci mutex_unlock(&dma->lock); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return ret; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(stm32_timers_dma_burst_read); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic const struct regmap_config stm32_timers_regmap_cfg = { 1538c2ecf20Sopenharmony_ci .reg_bits = 32, 1548c2ecf20Sopenharmony_ci .val_bits = 32, 1558c2ecf20Sopenharmony_ci .reg_stride = sizeof(u32), 1568c2ecf20Sopenharmony_ci .max_register = STM32_TIMERS_MAX_REGISTERS, 1578c2ecf20Sopenharmony_ci}; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cistatic void stm32_timers_get_arr_size(struct stm32_timers *ddata) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci u32 arr; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci /* Backup ARR to restore it after getting the maximum value */ 1648c2ecf20Sopenharmony_ci regmap_read(ddata->regmap, TIM_ARR, &arr); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci /* 1678c2ecf20Sopenharmony_ci * Only the available bits will be written so when readback 1688c2ecf20Sopenharmony_ci * we get the maximum value of auto reload register 1698c2ecf20Sopenharmony_ci */ 1708c2ecf20Sopenharmony_ci regmap_write(ddata->regmap, TIM_ARR, ~0L); 1718c2ecf20Sopenharmony_ci regmap_read(ddata->regmap, TIM_ARR, &ddata->max_arr); 1728c2ecf20Sopenharmony_ci regmap_write(ddata->regmap, TIM_ARR, arr); 1738c2ecf20Sopenharmony_ci} 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic int stm32_timers_dma_probe(struct device *dev, 1768c2ecf20Sopenharmony_ci struct stm32_timers *ddata) 1778c2ecf20Sopenharmony_ci{ 1788c2ecf20Sopenharmony_ci int i; 1798c2ecf20Sopenharmony_ci int ret = 0; 1808c2ecf20Sopenharmony_ci char name[4]; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci init_completion(&ddata->dma.completion); 1838c2ecf20Sopenharmony_ci mutex_init(&ddata->dma.lock); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci /* Optional DMA support: get valid DMA channel(s) or NULL */ 1868c2ecf20Sopenharmony_ci for (i = STM32_TIMERS_DMA_CH1; i <= STM32_TIMERS_DMA_CH4; i++) { 1878c2ecf20Sopenharmony_ci snprintf(name, ARRAY_SIZE(name), "ch%1d", i + 1); 1888c2ecf20Sopenharmony_ci ddata->dma.chans[i] = dma_request_chan(dev, name); 1898c2ecf20Sopenharmony_ci } 1908c2ecf20Sopenharmony_ci ddata->dma.chans[STM32_TIMERS_DMA_UP] = dma_request_chan(dev, "up"); 1918c2ecf20Sopenharmony_ci ddata->dma.chans[STM32_TIMERS_DMA_TRIG] = dma_request_chan(dev, "trig"); 1928c2ecf20Sopenharmony_ci ddata->dma.chans[STM32_TIMERS_DMA_COM] = dma_request_chan(dev, "com"); 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci for (i = STM32_TIMERS_DMA_CH1; i < STM32_TIMERS_MAX_DMAS; i++) { 1958c2ecf20Sopenharmony_ci if (IS_ERR(ddata->dma.chans[i])) { 1968c2ecf20Sopenharmony_ci /* Save the first error code to return */ 1978c2ecf20Sopenharmony_ci if (PTR_ERR(ddata->dma.chans[i]) != -ENODEV && !ret) 1988c2ecf20Sopenharmony_ci ret = PTR_ERR(ddata->dma.chans[i]); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci ddata->dma.chans[i] = NULL; 2018c2ecf20Sopenharmony_ci } 2028c2ecf20Sopenharmony_ci } 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci return ret; 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_cistatic void stm32_timers_dma_remove(struct device *dev, 2088c2ecf20Sopenharmony_ci struct stm32_timers *ddata) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci int i; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci for (i = STM32_TIMERS_DMA_CH1; i < STM32_TIMERS_MAX_DMAS; i++) 2138c2ecf20Sopenharmony_ci if (ddata->dma.chans[i]) 2148c2ecf20Sopenharmony_ci dma_release_channel(ddata->dma.chans[i]); 2158c2ecf20Sopenharmony_ci} 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_cistatic int stm32_timers_probe(struct platform_device *pdev) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 2208c2ecf20Sopenharmony_ci struct stm32_timers *ddata; 2218c2ecf20Sopenharmony_ci struct resource *res; 2228c2ecf20Sopenharmony_ci void __iomem *mmio; 2238c2ecf20Sopenharmony_ci int ret; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL); 2268c2ecf20Sopenharmony_ci if (!ddata) 2278c2ecf20Sopenharmony_ci return -ENOMEM; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2308c2ecf20Sopenharmony_ci mmio = devm_ioremap_resource(dev, res); 2318c2ecf20Sopenharmony_ci if (IS_ERR(mmio)) 2328c2ecf20Sopenharmony_ci return PTR_ERR(mmio); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci /* Timer physical addr for DMA */ 2358c2ecf20Sopenharmony_ci ddata->dma.phys_base = res->start; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci ddata->regmap = devm_regmap_init_mmio_clk(dev, "int", mmio, 2388c2ecf20Sopenharmony_ci &stm32_timers_regmap_cfg); 2398c2ecf20Sopenharmony_ci if (IS_ERR(ddata->regmap)) 2408c2ecf20Sopenharmony_ci return PTR_ERR(ddata->regmap); 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci ddata->clk = devm_clk_get(dev, NULL); 2438c2ecf20Sopenharmony_ci if (IS_ERR(ddata->clk)) 2448c2ecf20Sopenharmony_ci return PTR_ERR(ddata->clk); 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci stm32_timers_get_arr_size(ddata); 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci ret = stm32_timers_dma_probe(dev, ddata); 2498c2ecf20Sopenharmony_ci if (ret) { 2508c2ecf20Sopenharmony_ci stm32_timers_dma_remove(dev, ddata); 2518c2ecf20Sopenharmony_ci return ret; 2528c2ecf20Sopenharmony_ci } 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, ddata); 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); 2578c2ecf20Sopenharmony_ci if (ret) 2588c2ecf20Sopenharmony_ci stm32_timers_dma_remove(dev, ddata); 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci return ret; 2618c2ecf20Sopenharmony_ci} 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_cistatic int stm32_timers_remove(struct platform_device *pdev) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci struct stm32_timers *ddata = platform_get_drvdata(pdev); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci /* 2688c2ecf20Sopenharmony_ci * Don't use devm_ here: enfore of_platform_depopulate() happens before 2698c2ecf20Sopenharmony_ci * DMA are released, to avoid race on DMA. 2708c2ecf20Sopenharmony_ci */ 2718c2ecf20Sopenharmony_ci of_platform_depopulate(&pdev->dev); 2728c2ecf20Sopenharmony_ci stm32_timers_dma_remove(&pdev->dev, ddata); 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci return 0; 2758c2ecf20Sopenharmony_ci} 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_cistatic const struct of_device_id stm32_timers_of_match[] = { 2788c2ecf20Sopenharmony_ci { .compatible = "st,stm32-timers", }, 2798c2ecf20Sopenharmony_ci { /* end node */ }, 2808c2ecf20Sopenharmony_ci}; 2818c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, stm32_timers_of_match); 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_cistatic struct platform_driver stm32_timers_driver = { 2848c2ecf20Sopenharmony_ci .probe = stm32_timers_probe, 2858c2ecf20Sopenharmony_ci .remove = stm32_timers_remove, 2868c2ecf20Sopenharmony_ci .driver = { 2878c2ecf20Sopenharmony_ci .name = "stm32-timers", 2888c2ecf20Sopenharmony_ci .of_match_table = stm32_timers_of_match, 2898c2ecf20Sopenharmony_ci }, 2908c2ecf20Sopenharmony_ci}; 2918c2ecf20Sopenharmony_cimodule_platform_driver(stm32_timers_driver); 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("STMicroelectronics STM32 Timers"); 2948c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 295