1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Qualcomm Technologies HIDMA DMA engine Management interface
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/dmaengine.h>
9*4882a593Smuzhiyun #include <linux/acpi.h>
10*4882a593Smuzhiyun #include <linux/of.h>
11*4882a593Smuzhiyun #include <linux/property.h>
12*4882a593Smuzhiyun #include <linux/of_address.h>
13*4882a593Smuzhiyun #include <linux/of_irq.h>
14*4882a593Smuzhiyun #include <linux/of_platform.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/uaccess.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/pm_runtime.h>
19*4882a593Smuzhiyun #include <linux/bitops.h>
20*4882a593Smuzhiyun #include <linux/dma-mapping.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include "hidma_mgmt.h"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #define HIDMA_QOS_N_OFFSET 0x700
25*4882a593Smuzhiyun #define HIDMA_CFG_OFFSET 0x400
26*4882a593Smuzhiyun #define HIDMA_MAX_BUS_REQ_LEN_OFFSET 0x41C
27*4882a593Smuzhiyun #define HIDMA_MAX_XACTIONS_OFFSET 0x420
28*4882a593Smuzhiyun #define HIDMA_HW_VERSION_OFFSET 0x424
29*4882a593Smuzhiyun #define HIDMA_CHRESET_TIMEOUT_OFFSET 0x418
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define HIDMA_MAX_WR_XACTIONS_MASK GENMASK(4, 0)
32*4882a593Smuzhiyun #define HIDMA_MAX_RD_XACTIONS_MASK GENMASK(4, 0)
33*4882a593Smuzhiyun #define HIDMA_WEIGHT_MASK GENMASK(6, 0)
34*4882a593Smuzhiyun #define HIDMA_MAX_BUS_REQ_LEN_MASK GENMASK(15, 0)
35*4882a593Smuzhiyun #define HIDMA_CHRESET_TIMEOUT_MASK GENMASK(19, 0)
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #define HIDMA_MAX_WR_XACTIONS_BIT_POS 16
38*4882a593Smuzhiyun #define HIDMA_MAX_BUS_WR_REQ_BIT_POS 16
39*4882a593Smuzhiyun #define HIDMA_WRR_BIT_POS 8
40*4882a593Smuzhiyun #define HIDMA_PRIORITY_BIT_POS 15
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
43*4882a593Smuzhiyun #define HIDMA_MAX_CHANNEL_WEIGHT 15
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun static unsigned int max_write_request;
46*4882a593Smuzhiyun module_param(max_write_request, uint, 0644);
47*4882a593Smuzhiyun MODULE_PARM_DESC(max_write_request,
48*4882a593Smuzhiyun "maximum write burst (default: ACPI/DT value)");
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun static unsigned int max_read_request;
51*4882a593Smuzhiyun module_param(max_read_request, uint, 0644);
52*4882a593Smuzhiyun MODULE_PARM_DESC(max_read_request,
53*4882a593Smuzhiyun "maximum read burst (default: ACPI/DT value)");
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun static unsigned int max_wr_xactions;
56*4882a593Smuzhiyun module_param(max_wr_xactions, uint, 0644);
57*4882a593Smuzhiyun MODULE_PARM_DESC(max_wr_xactions,
58*4882a593Smuzhiyun "maximum number of write transactions (default: ACPI/DT value)");
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun static unsigned int max_rd_xactions;
61*4882a593Smuzhiyun module_param(max_rd_xactions, uint, 0644);
62*4882a593Smuzhiyun MODULE_PARM_DESC(max_rd_xactions,
63*4882a593Smuzhiyun "maximum number of read transactions (default: ACPI/DT value)");
64*4882a593Smuzhiyun
hidma_mgmt_setup(struct hidma_mgmt_dev * mgmtdev)65*4882a593Smuzhiyun int hidma_mgmt_setup(struct hidma_mgmt_dev *mgmtdev)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun unsigned int i;
68*4882a593Smuzhiyun u32 val;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun if (!is_power_of_2(mgmtdev->max_write_request) ||
71*4882a593Smuzhiyun (mgmtdev->max_write_request < 128) ||
72*4882a593Smuzhiyun (mgmtdev->max_write_request > 1024)) {
73*4882a593Smuzhiyun dev_err(&mgmtdev->pdev->dev, "invalid write request %d\n",
74*4882a593Smuzhiyun mgmtdev->max_write_request);
75*4882a593Smuzhiyun return -EINVAL;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun if (!is_power_of_2(mgmtdev->max_read_request) ||
79*4882a593Smuzhiyun (mgmtdev->max_read_request < 128) ||
80*4882a593Smuzhiyun (mgmtdev->max_read_request > 1024)) {
81*4882a593Smuzhiyun dev_err(&mgmtdev->pdev->dev, "invalid read request %d\n",
82*4882a593Smuzhiyun mgmtdev->max_read_request);
83*4882a593Smuzhiyun return -EINVAL;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun if (mgmtdev->max_wr_xactions > HIDMA_MAX_WR_XACTIONS_MASK) {
87*4882a593Smuzhiyun dev_err(&mgmtdev->pdev->dev,
88*4882a593Smuzhiyun "max_wr_xactions cannot be bigger than %ld\n",
89*4882a593Smuzhiyun HIDMA_MAX_WR_XACTIONS_MASK);
90*4882a593Smuzhiyun return -EINVAL;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun if (mgmtdev->max_rd_xactions > HIDMA_MAX_RD_XACTIONS_MASK) {
94*4882a593Smuzhiyun dev_err(&mgmtdev->pdev->dev,
95*4882a593Smuzhiyun "max_rd_xactions cannot be bigger than %ld\n",
96*4882a593Smuzhiyun HIDMA_MAX_RD_XACTIONS_MASK);
97*4882a593Smuzhiyun return -EINVAL;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun for (i = 0; i < mgmtdev->dma_channels; i++) {
101*4882a593Smuzhiyun if (mgmtdev->priority[i] > 1) {
102*4882a593Smuzhiyun dev_err(&mgmtdev->pdev->dev,
103*4882a593Smuzhiyun "priority can be 0 or 1\n");
104*4882a593Smuzhiyun return -EINVAL;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun if (mgmtdev->weight[i] > HIDMA_MAX_CHANNEL_WEIGHT) {
108*4882a593Smuzhiyun dev_err(&mgmtdev->pdev->dev,
109*4882a593Smuzhiyun "max value of weight can be %d.\n",
110*4882a593Smuzhiyun HIDMA_MAX_CHANNEL_WEIGHT);
111*4882a593Smuzhiyun return -EINVAL;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* weight needs to be at least one */
115*4882a593Smuzhiyun if (mgmtdev->weight[i] == 0)
116*4882a593Smuzhiyun mgmtdev->weight[i] = 1;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun pm_runtime_get_sync(&mgmtdev->pdev->dev);
120*4882a593Smuzhiyun val = readl(mgmtdev->virtaddr + HIDMA_MAX_BUS_REQ_LEN_OFFSET);
121*4882a593Smuzhiyun val &= ~(HIDMA_MAX_BUS_REQ_LEN_MASK << HIDMA_MAX_BUS_WR_REQ_BIT_POS);
122*4882a593Smuzhiyun val |= mgmtdev->max_write_request << HIDMA_MAX_BUS_WR_REQ_BIT_POS;
123*4882a593Smuzhiyun val &= ~HIDMA_MAX_BUS_REQ_LEN_MASK;
124*4882a593Smuzhiyun val |= mgmtdev->max_read_request;
125*4882a593Smuzhiyun writel(val, mgmtdev->virtaddr + HIDMA_MAX_BUS_REQ_LEN_OFFSET);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun val = readl(mgmtdev->virtaddr + HIDMA_MAX_XACTIONS_OFFSET);
128*4882a593Smuzhiyun val &= ~(HIDMA_MAX_WR_XACTIONS_MASK << HIDMA_MAX_WR_XACTIONS_BIT_POS);
129*4882a593Smuzhiyun val |= mgmtdev->max_wr_xactions << HIDMA_MAX_WR_XACTIONS_BIT_POS;
130*4882a593Smuzhiyun val &= ~HIDMA_MAX_RD_XACTIONS_MASK;
131*4882a593Smuzhiyun val |= mgmtdev->max_rd_xactions;
132*4882a593Smuzhiyun writel(val, mgmtdev->virtaddr + HIDMA_MAX_XACTIONS_OFFSET);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun mgmtdev->hw_version =
135*4882a593Smuzhiyun readl(mgmtdev->virtaddr + HIDMA_HW_VERSION_OFFSET);
136*4882a593Smuzhiyun mgmtdev->hw_version_major = (mgmtdev->hw_version >> 28) & 0xF;
137*4882a593Smuzhiyun mgmtdev->hw_version_minor = (mgmtdev->hw_version >> 16) & 0xF;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun for (i = 0; i < mgmtdev->dma_channels; i++) {
140*4882a593Smuzhiyun u32 weight = mgmtdev->weight[i];
141*4882a593Smuzhiyun u32 priority = mgmtdev->priority[i];
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun val = readl(mgmtdev->virtaddr + HIDMA_QOS_N_OFFSET + (4 * i));
144*4882a593Smuzhiyun val &= ~(1 << HIDMA_PRIORITY_BIT_POS);
145*4882a593Smuzhiyun val |= (priority & 0x1) << HIDMA_PRIORITY_BIT_POS;
146*4882a593Smuzhiyun val &= ~(HIDMA_WEIGHT_MASK << HIDMA_WRR_BIT_POS);
147*4882a593Smuzhiyun val |= (weight & HIDMA_WEIGHT_MASK) << HIDMA_WRR_BIT_POS;
148*4882a593Smuzhiyun writel(val, mgmtdev->virtaddr + HIDMA_QOS_N_OFFSET + (4 * i));
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun val = readl(mgmtdev->virtaddr + HIDMA_CHRESET_TIMEOUT_OFFSET);
152*4882a593Smuzhiyun val &= ~HIDMA_CHRESET_TIMEOUT_MASK;
153*4882a593Smuzhiyun val |= mgmtdev->chreset_timeout_cycles & HIDMA_CHRESET_TIMEOUT_MASK;
154*4882a593Smuzhiyun writel(val, mgmtdev->virtaddr + HIDMA_CHRESET_TIMEOUT_OFFSET);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun pm_runtime_mark_last_busy(&mgmtdev->pdev->dev);
157*4882a593Smuzhiyun pm_runtime_put_autosuspend(&mgmtdev->pdev->dev);
158*4882a593Smuzhiyun return 0;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(hidma_mgmt_setup);
161*4882a593Smuzhiyun
hidma_mgmt_probe(struct platform_device * pdev)162*4882a593Smuzhiyun static int hidma_mgmt_probe(struct platform_device *pdev)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun struct hidma_mgmt_dev *mgmtdev;
165*4882a593Smuzhiyun struct resource *res;
166*4882a593Smuzhiyun void __iomem *virtaddr;
167*4882a593Smuzhiyun int irq;
168*4882a593Smuzhiyun int rc;
169*4882a593Smuzhiyun u32 val;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun pm_runtime_set_autosuspend_delay(&pdev->dev, HIDMA_AUTOSUSPEND_TIMEOUT);
172*4882a593Smuzhiyun pm_runtime_use_autosuspend(&pdev->dev);
173*4882a593Smuzhiyun pm_runtime_set_active(&pdev->dev);
174*4882a593Smuzhiyun pm_runtime_enable(&pdev->dev);
175*4882a593Smuzhiyun pm_runtime_get_sync(&pdev->dev);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
178*4882a593Smuzhiyun virtaddr = devm_ioremap_resource(&pdev->dev, res);
179*4882a593Smuzhiyun if (IS_ERR(virtaddr)) {
180*4882a593Smuzhiyun rc = -ENOMEM;
181*4882a593Smuzhiyun goto out;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun irq = platform_get_irq(pdev, 0);
185*4882a593Smuzhiyun if (irq < 0) {
186*4882a593Smuzhiyun rc = irq;
187*4882a593Smuzhiyun goto out;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun mgmtdev = devm_kzalloc(&pdev->dev, sizeof(*mgmtdev), GFP_KERNEL);
191*4882a593Smuzhiyun if (!mgmtdev) {
192*4882a593Smuzhiyun rc = -ENOMEM;
193*4882a593Smuzhiyun goto out;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun mgmtdev->pdev = pdev;
197*4882a593Smuzhiyun mgmtdev->addrsize = resource_size(res);
198*4882a593Smuzhiyun mgmtdev->virtaddr = virtaddr;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun rc = device_property_read_u32(&pdev->dev, "dma-channels",
201*4882a593Smuzhiyun &mgmtdev->dma_channels);
202*4882a593Smuzhiyun if (rc) {
203*4882a593Smuzhiyun dev_err(&pdev->dev, "number of channels missing\n");
204*4882a593Smuzhiyun goto out;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun rc = device_property_read_u32(&pdev->dev,
208*4882a593Smuzhiyun "channel-reset-timeout-cycles",
209*4882a593Smuzhiyun &mgmtdev->chreset_timeout_cycles);
210*4882a593Smuzhiyun if (rc) {
211*4882a593Smuzhiyun dev_err(&pdev->dev, "channel reset timeout missing\n");
212*4882a593Smuzhiyun goto out;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun rc = device_property_read_u32(&pdev->dev, "max-write-burst-bytes",
216*4882a593Smuzhiyun &mgmtdev->max_write_request);
217*4882a593Smuzhiyun if (rc) {
218*4882a593Smuzhiyun dev_err(&pdev->dev, "max-write-burst-bytes missing\n");
219*4882a593Smuzhiyun goto out;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun if (max_write_request &&
223*4882a593Smuzhiyun (max_write_request != mgmtdev->max_write_request)) {
224*4882a593Smuzhiyun dev_info(&pdev->dev, "overriding max-write-burst-bytes: %d\n",
225*4882a593Smuzhiyun max_write_request);
226*4882a593Smuzhiyun mgmtdev->max_write_request = max_write_request;
227*4882a593Smuzhiyun } else
228*4882a593Smuzhiyun max_write_request = mgmtdev->max_write_request;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun rc = device_property_read_u32(&pdev->dev, "max-read-burst-bytes",
231*4882a593Smuzhiyun &mgmtdev->max_read_request);
232*4882a593Smuzhiyun if (rc) {
233*4882a593Smuzhiyun dev_err(&pdev->dev, "max-read-burst-bytes missing\n");
234*4882a593Smuzhiyun goto out;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun if (max_read_request &&
237*4882a593Smuzhiyun (max_read_request != mgmtdev->max_read_request)) {
238*4882a593Smuzhiyun dev_info(&pdev->dev, "overriding max-read-burst-bytes: %d\n",
239*4882a593Smuzhiyun max_read_request);
240*4882a593Smuzhiyun mgmtdev->max_read_request = max_read_request;
241*4882a593Smuzhiyun } else
242*4882a593Smuzhiyun max_read_request = mgmtdev->max_read_request;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun rc = device_property_read_u32(&pdev->dev, "max-write-transactions",
245*4882a593Smuzhiyun &mgmtdev->max_wr_xactions);
246*4882a593Smuzhiyun if (rc) {
247*4882a593Smuzhiyun dev_err(&pdev->dev, "max-write-transactions missing\n");
248*4882a593Smuzhiyun goto out;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun if (max_wr_xactions &&
251*4882a593Smuzhiyun (max_wr_xactions != mgmtdev->max_wr_xactions)) {
252*4882a593Smuzhiyun dev_info(&pdev->dev, "overriding max-write-transactions: %d\n",
253*4882a593Smuzhiyun max_wr_xactions);
254*4882a593Smuzhiyun mgmtdev->max_wr_xactions = max_wr_xactions;
255*4882a593Smuzhiyun } else
256*4882a593Smuzhiyun max_wr_xactions = mgmtdev->max_wr_xactions;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun rc = device_property_read_u32(&pdev->dev, "max-read-transactions",
259*4882a593Smuzhiyun &mgmtdev->max_rd_xactions);
260*4882a593Smuzhiyun if (rc) {
261*4882a593Smuzhiyun dev_err(&pdev->dev, "max-read-transactions missing\n");
262*4882a593Smuzhiyun goto out;
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun if (max_rd_xactions &&
265*4882a593Smuzhiyun (max_rd_xactions != mgmtdev->max_rd_xactions)) {
266*4882a593Smuzhiyun dev_info(&pdev->dev, "overriding max-read-transactions: %d\n",
267*4882a593Smuzhiyun max_rd_xactions);
268*4882a593Smuzhiyun mgmtdev->max_rd_xactions = max_rd_xactions;
269*4882a593Smuzhiyun } else
270*4882a593Smuzhiyun max_rd_xactions = mgmtdev->max_rd_xactions;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun mgmtdev->priority = devm_kcalloc(&pdev->dev,
273*4882a593Smuzhiyun mgmtdev->dma_channels,
274*4882a593Smuzhiyun sizeof(*mgmtdev->priority),
275*4882a593Smuzhiyun GFP_KERNEL);
276*4882a593Smuzhiyun if (!mgmtdev->priority) {
277*4882a593Smuzhiyun rc = -ENOMEM;
278*4882a593Smuzhiyun goto out;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun mgmtdev->weight = devm_kcalloc(&pdev->dev,
282*4882a593Smuzhiyun mgmtdev->dma_channels,
283*4882a593Smuzhiyun sizeof(*mgmtdev->weight), GFP_KERNEL);
284*4882a593Smuzhiyun if (!mgmtdev->weight) {
285*4882a593Smuzhiyun rc = -ENOMEM;
286*4882a593Smuzhiyun goto out;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun rc = hidma_mgmt_setup(mgmtdev);
290*4882a593Smuzhiyun if (rc) {
291*4882a593Smuzhiyun dev_err(&pdev->dev, "setup failed\n");
292*4882a593Smuzhiyun goto out;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* start the HW */
296*4882a593Smuzhiyun val = readl(mgmtdev->virtaddr + HIDMA_CFG_OFFSET);
297*4882a593Smuzhiyun val |= 1;
298*4882a593Smuzhiyun writel(val, mgmtdev->virtaddr + HIDMA_CFG_OFFSET);
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun rc = hidma_mgmt_init_sys(mgmtdev);
301*4882a593Smuzhiyun if (rc) {
302*4882a593Smuzhiyun dev_err(&pdev->dev, "sysfs setup failed\n");
303*4882a593Smuzhiyun goto out;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun dev_info(&pdev->dev,
307*4882a593Smuzhiyun "HW rev: %d.%d @ %pa with %d physical channels\n",
308*4882a593Smuzhiyun mgmtdev->hw_version_major, mgmtdev->hw_version_minor,
309*4882a593Smuzhiyun &res->start, mgmtdev->dma_channels);
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun platform_set_drvdata(pdev, mgmtdev);
312*4882a593Smuzhiyun pm_runtime_mark_last_busy(&pdev->dev);
313*4882a593Smuzhiyun pm_runtime_put_autosuspend(&pdev->dev);
314*4882a593Smuzhiyun return 0;
315*4882a593Smuzhiyun out:
316*4882a593Smuzhiyun pm_runtime_put_sync_suspend(&pdev->dev);
317*4882a593Smuzhiyun pm_runtime_disable(&pdev->dev);
318*4882a593Smuzhiyun return rc;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_ACPI)
322*4882a593Smuzhiyun static const struct acpi_device_id hidma_mgmt_acpi_ids[] = {
323*4882a593Smuzhiyun {"QCOM8060"},
324*4882a593Smuzhiyun {},
325*4882a593Smuzhiyun };
326*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, hidma_mgmt_acpi_ids);
327*4882a593Smuzhiyun #endif
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun static const struct of_device_id hidma_mgmt_match[] = {
330*4882a593Smuzhiyun {.compatible = "qcom,hidma-mgmt-1.0",},
331*4882a593Smuzhiyun {},
332*4882a593Smuzhiyun };
333*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, hidma_mgmt_match);
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun static struct platform_driver hidma_mgmt_driver = {
336*4882a593Smuzhiyun .probe = hidma_mgmt_probe,
337*4882a593Smuzhiyun .driver = {
338*4882a593Smuzhiyun .name = "hidma-mgmt",
339*4882a593Smuzhiyun .of_match_table = hidma_mgmt_match,
340*4882a593Smuzhiyun .acpi_match_table = ACPI_PTR(hidma_mgmt_acpi_ids),
341*4882a593Smuzhiyun },
342*4882a593Smuzhiyun };
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
345*4882a593Smuzhiyun static int object_counter;
346*4882a593Smuzhiyun
hidma_mgmt_of_populate_channels(struct device_node * np)347*4882a593Smuzhiyun static int __init hidma_mgmt_of_populate_channels(struct device_node *np)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun struct platform_device *pdev_parent = of_find_device_by_node(np);
350*4882a593Smuzhiyun struct platform_device_info pdevinfo;
351*4882a593Smuzhiyun struct device_node *child;
352*4882a593Smuzhiyun struct resource *res;
353*4882a593Smuzhiyun int ret = 0;
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /* allocate a resource array */
356*4882a593Smuzhiyun res = kcalloc(3, sizeof(*res), GFP_KERNEL);
357*4882a593Smuzhiyun if (!res)
358*4882a593Smuzhiyun return -ENOMEM;
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun for_each_available_child_of_node(np, child) {
361*4882a593Smuzhiyun struct platform_device *new_pdev;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun ret = of_address_to_resource(child, 0, &res[0]);
364*4882a593Smuzhiyun if (!ret)
365*4882a593Smuzhiyun goto out;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun ret = of_address_to_resource(child, 1, &res[1]);
368*4882a593Smuzhiyun if (!ret)
369*4882a593Smuzhiyun goto out;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun ret = of_irq_to_resource(child, 0, &res[2]);
372*4882a593Smuzhiyun if (ret <= 0)
373*4882a593Smuzhiyun goto out;
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun memset(&pdevinfo, 0, sizeof(pdevinfo));
376*4882a593Smuzhiyun pdevinfo.fwnode = &child->fwnode;
377*4882a593Smuzhiyun pdevinfo.parent = pdev_parent ? &pdev_parent->dev : NULL;
378*4882a593Smuzhiyun pdevinfo.name = child->name;
379*4882a593Smuzhiyun pdevinfo.id = object_counter++;
380*4882a593Smuzhiyun pdevinfo.res = res;
381*4882a593Smuzhiyun pdevinfo.num_res = 3;
382*4882a593Smuzhiyun pdevinfo.data = NULL;
383*4882a593Smuzhiyun pdevinfo.size_data = 0;
384*4882a593Smuzhiyun pdevinfo.dma_mask = DMA_BIT_MASK(64);
385*4882a593Smuzhiyun new_pdev = platform_device_register_full(&pdevinfo);
386*4882a593Smuzhiyun if (IS_ERR(new_pdev)) {
387*4882a593Smuzhiyun ret = PTR_ERR(new_pdev);
388*4882a593Smuzhiyun goto out;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun new_pdev->dev.of_node = child;
391*4882a593Smuzhiyun of_dma_configure(&new_pdev->dev, child, true);
392*4882a593Smuzhiyun /*
393*4882a593Smuzhiyun * It is assumed that calling of_msi_configure is safe on
394*4882a593Smuzhiyun * platforms with or without MSI support.
395*4882a593Smuzhiyun */
396*4882a593Smuzhiyun of_msi_configure(&new_pdev->dev, child);
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun kfree(res);
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun return ret;
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun out:
404*4882a593Smuzhiyun of_node_put(child);
405*4882a593Smuzhiyun kfree(res);
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun return ret;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun #endif
410*4882a593Smuzhiyun
hidma_mgmt_init(void)411*4882a593Smuzhiyun static int __init hidma_mgmt_init(void)
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
414*4882a593Smuzhiyun struct device_node *child;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun for_each_matching_node(child, hidma_mgmt_match) {
417*4882a593Smuzhiyun /* device tree based firmware here */
418*4882a593Smuzhiyun hidma_mgmt_of_populate_channels(child);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun #endif
421*4882a593Smuzhiyun /*
422*4882a593Smuzhiyun * We do not check for return value here, as it is assumed that
423*4882a593Smuzhiyun * platform_driver_register must not fail. The reason for this is that
424*4882a593Smuzhiyun * the (potential) hidma_mgmt_of_populate_channels calls above are not
425*4882a593Smuzhiyun * cleaned up if it does fail, and to do this work is quite
426*4882a593Smuzhiyun * complicated. In particular, various calls of of_address_to_resource,
427*4882a593Smuzhiyun * of_irq_to_resource, platform_device_register_full, of_dma_configure,
428*4882a593Smuzhiyun * and of_msi_configure which then call other functions and so on, must
429*4882a593Smuzhiyun * be cleaned up - this is not a trivial exercise.
430*4882a593Smuzhiyun *
431*4882a593Smuzhiyun * Currently, this module is not intended to be unloaded, and there is
432*4882a593Smuzhiyun * no module_exit function defined which does the needed cleanup. For
433*4882a593Smuzhiyun * this reason, we have to assume success here.
434*4882a593Smuzhiyun */
435*4882a593Smuzhiyun platform_driver_register(&hidma_mgmt_driver);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun return 0;
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun module_init(hidma_mgmt_init);
440*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
441