1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) STMicroelectronics SA 2017
5 * Author(s): M'boumba Cedric Madianga <cedric.madianga@gmail.com>
6 * Pierre-Yves Mordret <pierre-yves.mordret@st.com>
8 * DMA Router driver for STM32 DMA MUX
10 * Based on TI DMA Crossbar driver
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
19 #include <linux/of_dma.h>
20 #include <linux/of_platform.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/reset.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
27 #define STM32_DMAMUX_CCR(x) (0x4 * (x))
28 #define STM32_DMAMUX_MAX_DMA_REQUESTS 32
29 #define STM32_DMAMUX_MAX_REQUESTS 255
37 struct stm32_dmamux_data
{
38 struct dma_router dmarouter
;
41 u32 dma_requests
; /* Number of DMA requests connected to DMAMUX */
42 u32 dmamux_requests
; /* Number of DMA requests routed toward DMAs */
43 spinlock_t lock
; /* Protects register access */
44 DECLARE_BITMAP(dma_inuse
, STM32_DMAMUX_MAX_DMA_REQUESTS
); /* Used DMA channel */
45 u32 ccr
[STM32_DMAMUX_MAX_DMA_REQUESTS
]; /* Used to backup CCR register
48 u32 dma_reqs
[]; /* Number of DMA Request per DMA masters.
49 * [0] holds number of DMA Masters.
50 * To be kept at very end of this structure
54 static inline u32
stm32_dmamux_read(void __iomem
*iomem
, u32 reg
)
56 return readl_relaxed(iomem
+ reg
);
59 static inline void stm32_dmamux_write(void __iomem
*iomem
, u32 reg
, u32 val
)
61 writel_relaxed(val
, iomem
+ reg
);
64 static void stm32_dmamux_free(struct device
*dev
, void *route_data
)
66 struct stm32_dmamux_data
*dmamux
= dev_get_drvdata(dev
);
67 struct stm32_dmamux
*mux
= route_data
;
70 /* Clear dma request */
71 spin_lock_irqsave(&dmamux
->lock
, flags
);
73 stm32_dmamux_write(dmamux
->iomem
, STM32_DMAMUX_CCR(mux
->chan_id
), 0);
74 clear_bit(mux
->chan_id
, dmamux
->dma_inuse
);
76 pm_runtime_put_sync(dev
);
78 spin_unlock_irqrestore(&dmamux
->lock
, flags
);
80 dev_dbg(dev
, "Unmapping DMAMUX(%u) to DMA%u(%u)\n",
81 mux
->request
, mux
->master
, mux
->chan_id
);
86 static void *stm32_dmamux_route_allocate(struct of_phandle_args
*dma_spec
,
89 struct platform_device
*pdev
= of_find_device_by_node(ofdma
->of_node
);
90 struct stm32_dmamux_data
*dmamux
= platform_get_drvdata(pdev
);
91 struct stm32_dmamux
*mux
;
96 if (dma_spec
->args_count
!= 3) {
97 dev_err(&pdev
->dev
, "invalid number of dma mux args\n");
98 return ERR_PTR(-EINVAL
);
101 if (dma_spec
->args
[0] > dmamux
->dmamux_requests
) {
102 dev_err(&pdev
->dev
, "invalid mux request number: %d\n",
104 return ERR_PTR(-EINVAL
);
107 mux
= kzalloc(sizeof(*mux
), GFP_KERNEL
);
109 return ERR_PTR(-ENOMEM
);
111 spin_lock_irqsave(&dmamux
->lock
, flags
);
112 mux
->chan_id
= find_first_zero_bit(dmamux
->dma_inuse
,
113 dmamux
->dma_requests
);
115 if (mux
->chan_id
== dmamux
->dma_requests
) {
116 spin_unlock_irqrestore(&dmamux
->lock
, flags
);
117 dev_err(&pdev
->dev
, "Run out of free DMA requests\n");
121 set_bit(mux
->chan_id
, dmamux
->dma_inuse
);
122 spin_unlock_irqrestore(&dmamux
->lock
, flags
);
124 /* Look for DMA Master */
125 for (i
= 1, min
= 0, max
= dmamux
->dma_reqs
[i
];
126 i
<= dmamux
->dma_reqs
[0];
127 min
+= dmamux
->dma_reqs
[i
], max
+= dmamux
->dma_reqs
[++i
])
128 if (mux
->chan_id
< max
)
132 /* The of_node_put() will be done in of_dma_router_xlate function */
133 dma_spec
->np
= of_parse_phandle(ofdma
->of_node
, "dma-masters", i
- 1);
135 dev_err(&pdev
->dev
, "can't get dma master\n");
140 /* Set dma request */
141 spin_lock_irqsave(&dmamux
->lock
, flags
);
142 ret
= pm_runtime_resume_and_get(&pdev
->dev
);
144 spin_unlock_irqrestore(&dmamux
->lock
, flags
);
147 spin_unlock_irqrestore(&dmamux
->lock
, flags
);
149 mux
->request
= dma_spec
->args
[0];
152 dma_spec
->args
[3] = dma_spec
->args
[2] | mux
->chan_id
<< 16;
153 dma_spec
->args
[2] = dma_spec
->args
[1];
154 dma_spec
->args
[1] = 0;
155 dma_spec
->args
[0] = mux
->chan_id
- min
;
156 dma_spec
->args_count
= 4;
158 stm32_dmamux_write(dmamux
->iomem
, STM32_DMAMUX_CCR(mux
->chan_id
),
160 dev_dbg(&pdev
->dev
, "Mapping DMAMUX(%u) to DMA%u(%u)\n",
161 mux
->request
, mux
->master
, mux
->chan_id
);
166 clear_bit(mux
->chan_id
, dmamux
->dma_inuse
);
173 static const struct of_device_id stm32_stm32dma_master_match
[] __maybe_unused
= {
174 { .compatible
= "st,stm32-dma", },
178 static int stm32_dmamux_probe(struct platform_device
*pdev
)
180 struct device_node
*node
= pdev
->dev
.of_node
;
181 const struct of_device_id
*match
;
182 struct device_node
*dma_node
;
183 struct stm32_dmamux_data
*stm32_dmamux
;
185 struct reset_control
*rst
;
192 count
= device_property_count_u32(&pdev
->dev
, "dma-masters");
194 dev_err(&pdev
->dev
, "Can't get DMA master(s) node\n");
198 stm32_dmamux
= devm_kzalloc(&pdev
->dev
, sizeof(*stm32_dmamux
) +
199 sizeof(u32
) * (count
+ 1), GFP_KERNEL
);
204 for (i
= 1; i
<= count
; i
++) {
205 dma_node
= of_parse_phandle(node
, "dma-masters", i
- 1);
207 match
= of_match_node(stm32_stm32dma_master_match
, dma_node
);
209 dev_err(&pdev
->dev
, "DMA master is not supported\n");
210 of_node_put(dma_node
);
214 if (of_property_read_u32(dma_node
, "dma-requests",
215 &stm32_dmamux
->dma_reqs
[i
])) {
217 "Missing MUX output information, using %u.\n",
218 STM32_DMAMUX_MAX_DMA_REQUESTS
);
219 stm32_dmamux
->dma_reqs
[i
] =
220 STM32_DMAMUX_MAX_DMA_REQUESTS
;
222 dma_req
+= stm32_dmamux
->dma_reqs
[i
];
223 of_node_put(dma_node
);
226 if (dma_req
> STM32_DMAMUX_MAX_DMA_REQUESTS
) {
227 dev_err(&pdev
->dev
, "Too many DMA Master Requests to manage\n");
231 stm32_dmamux
->dma_requests
= dma_req
;
232 stm32_dmamux
->dma_reqs
[0] = count
;
234 if (device_property_read_u32(&pdev
->dev
, "dma-requests",
235 &stm32_dmamux
->dmamux_requests
)) {
236 stm32_dmamux
->dmamux_requests
= STM32_DMAMUX_MAX_REQUESTS
;
237 dev_warn(&pdev
->dev
, "DMAMUX defaulting on %u requests\n",
238 stm32_dmamux
->dmamux_requests
);
240 pm_runtime_get_noresume(&pdev
->dev
);
242 iomem
= devm_platform_ioremap_resource(pdev
, 0);
244 return PTR_ERR(iomem
);
246 spin_lock_init(&stm32_dmamux
->lock
);
248 stm32_dmamux
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
249 if (IS_ERR(stm32_dmamux
->clk
))
250 return dev_err_probe(&pdev
->dev
, PTR_ERR(stm32_dmamux
->clk
),
251 "Missing clock controller\n");
253 ret
= clk_prepare_enable(stm32_dmamux
->clk
);
255 dev_err(&pdev
->dev
, "clk_prep_enable error: %d\n", ret
);
259 rst
= devm_reset_control_get(&pdev
->dev
, NULL
);
262 if (ret
== -EPROBE_DEFER
)
264 } else if (count
> 1) { /* Don't reset if there is only one dma-master */
265 reset_control_assert(rst
);
267 reset_control_deassert(rst
);
270 stm32_dmamux
->iomem
= iomem
;
271 stm32_dmamux
->dmarouter
.dev
= &pdev
->dev
;
272 stm32_dmamux
->dmarouter
.route_free
= stm32_dmamux_free
;
274 platform_set_drvdata(pdev
, stm32_dmamux
);
275 pm_runtime_set_active(&pdev
->dev
);
276 pm_runtime_enable(&pdev
->dev
);
278 pm_runtime_get_noresume(&pdev
->dev
);
280 /* Reset the dmamux */
281 for (i
= 0; i
< stm32_dmamux
->dma_requests
; i
++)
282 stm32_dmamux_write(stm32_dmamux
->iomem
, STM32_DMAMUX_CCR(i
), 0);
284 pm_runtime_put(&pdev
->dev
);
286 ret
= of_dma_router_register(node
, stm32_dmamux_route_allocate
,
287 &stm32_dmamux
->dmarouter
);
294 pm_runtime_disable(&pdev
->dev
);
296 clk_disable_unprepare(stm32_dmamux
->clk
);
302 static int stm32_dmamux_runtime_suspend(struct device
*dev
)
304 struct platform_device
*pdev
= to_platform_device(dev
);
305 struct stm32_dmamux_data
*stm32_dmamux
= platform_get_drvdata(pdev
);
307 clk_disable_unprepare(stm32_dmamux
->clk
);
312 static int stm32_dmamux_runtime_resume(struct device
*dev
)
314 struct platform_device
*pdev
= to_platform_device(dev
);
315 struct stm32_dmamux_data
*stm32_dmamux
= platform_get_drvdata(pdev
);
318 ret
= clk_prepare_enable(stm32_dmamux
->clk
);
320 dev_err(&pdev
->dev
, "failed to prepare_enable clock\n");
328 #ifdef CONFIG_PM_SLEEP
329 static int stm32_dmamux_suspend(struct device
*dev
)
331 struct platform_device
*pdev
= to_platform_device(dev
);
332 struct stm32_dmamux_data
*stm32_dmamux
= platform_get_drvdata(pdev
);
335 ret
= pm_runtime_resume_and_get(dev
);
339 for (i
= 0; i
< stm32_dmamux
->dma_requests
; i
++)
340 stm32_dmamux
->ccr
[i
] = stm32_dmamux_read(stm32_dmamux
->iomem
,
341 STM32_DMAMUX_CCR(i
));
343 pm_runtime_put_sync(dev
);
345 pm_runtime_force_suspend(dev
);
350 static int stm32_dmamux_resume(struct device
*dev
)
352 struct platform_device
*pdev
= to_platform_device(dev
);
353 struct stm32_dmamux_data
*stm32_dmamux
= platform_get_drvdata(pdev
);
356 ret
= pm_runtime_force_resume(dev
);
360 ret
= pm_runtime_resume_and_get(dev
);
364 for (i
= 0; i
< stm32_dmamux
->dma_requests
; i
++)
365 stm32_dmamux_write(stm32_dmamux
->iomem
, STM32_DMAMUX_CCR(i
),
366 stm32_dmamux
->ccr
[i
]);
368 pm_runtime_put_sync(dev
);
374 static const struct dev_pm_ops stm32_dmamux_pm_ops
= {
375 SET_SYSTEM_SLEEP_PM_OPS(stm32_dmamux_suspend
, stm32_dmamux_resume
)
376 SET_RUNTIME_PM_OPS(stm32_dmamux_runtime_suspend
,
377 stm32_dmamux_runtime_resume
, NULL
)
380 static const struct of_device_id stm32_dmamux_match
[] = {
381 { .compatible
= "st,stm32h7-dmamux" },
385 static struct platform_driver stm32_dmamux_driver
= {
386 .probe
= stm32_dmamux_probe
,
388 .name
= "stm32-dmamux",
389 .of_match_table
= stm32_dmamux_match
,
390 .pm
= &stm32_dmamux_pm_ops
,
394 static int __init
stm32_dmamux_init(void)
396 return platform_driver_register(&stm32_dmamux_driver
);
398 arch_initcall(stm32_dmamux_init
);
400 MODULE_DESCRIPTION("DMA Router driver for STM32 DMA MUX");
401 MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>");
402 MODULE_AUTHOR("Pierre-Yves Mordret <pierre-yves.mordret@st.com>");