2 * Qualcomm Technologies HIDMA DMA engine Management interface
4 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/dmaengine.h>
17 #include <linux/acpi.h>
19 #include <linux/property.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 #include <linux/module.h>
23 #include <linux/uaccess.h>
24 #include <linux/slab.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/bitops.h>
28 #include "hidma_mgmt.h"
30 #define HIDMA_QOS_N_OFFSET 0x300
31 #define HIDMA_CFG_OFFSET 0x400
32 #define HIDMA_MAX_BUS_REQ_LEN_OFFSET 0x41C
33 #define HIDMA_MAX_XACTIONS_OFFSET 0x420
34 #define HIDMA_HW_VERSION_OFFSET 0x424
35 #define HIDMA_CHRESET_TIMEOUT_OFFSET 0x418
37 #define HIDMA_MAX_WR_XACTIONS_MASK GENMASK(4, 0)
38 #define HIDMA_MAX_RD_XACTIONS_MASK GENMASK(4, 0)
39 #define HIDMA_WEIGHT_MASK GENMASK(6, 0)
40 #define HIDMA_MAX_BUS_REQ_LEN_MASK GENMASK(15, 0)
41 #define HIDMA_CHRESET_TIMEOUT_MASK GENMASK(19, 0)
43 #define HIDMA_MAX_WR_XACTIONS_BIT_POS 16
44 #define HIDMA_MAX_BUS_WR_REQ_BIT_POS 16
45 #define HIDMA_WRR_BIT_POS 8
46 #define HIDMA_PRIORITY_BIT_POS 15
48 #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
49 #define HIDMA_MAX_CHANNEL_WEIGHT 15
51 int hidma_mgmt_setup(struct hidma_mgmt_dev
*mgmtdev
)
56 if (!is_power_of_2(mgmtdev
->max_write_request
) ||
57 (mgmtdev
->max_write_request
< 128) ||
58 (mgmtdev
->max_write_request
> 1024)) {
59 dev_err(&mgmtdev
->pdev
->dev
, "invalid write request %d\n",
60 mgmtdev
->max_write_request
);
64 if (!is_power_of_2(mgmtdev
->max_read_request
) ||
65 (mgmtdev
->max_read_request
< 128) ||
66 (mgmtdev
->max_read_request
> 1024)) {
67 dev_err(&mgmtdev
->pdev
->dev
, "invalid read request %d\n",
68 mgmtdev
->max_read_request
);
72 if (mgmtdev
->max_wr_xactions
> HIDMA_MAX_WR_XACTIONS_MASK
) {
73 dev_err(&mgmtdev
->pdev
->dev
,
74 "max_wr_xactions cannot be bigger than %ld\n",
75 HIDMA_MAX_WR_XACTIONS_MASK
);
79 if (mgmtdev
->max_rd_xactions
> HIDMA_MAX_RD_XACTIONS_MASK
) {
80 dev_err(&mgmtdev
->pdev
->dev
,
81 "max_rd_xactions cannot be bigger than %ld\n",
82 HIDMA_MAX_RD_XACTIONS_MASK
);
86 for (i
= 0; i
< mgmtdev
->dma_channels
; i
++) {
87 if (mgmtdev
->priority
[i
] > 1) {
88 dev_err(&mgmtdev
->pdev
->dev
,
89 "priority can be 0 or 1\n");
93 if (mgmtdev
->weight
[i
] > HIDMA_MAX_CHANNEL_WEIGHT
) {
94 dev_err(&mgmtdev
->pdev
->dev
,
95 "max value of weight can be %d.\n",
96 HIDMA_MAX_CHANNEL_WEIGHT
);
100 /* weight needs to be at least one */
101 if (mgmtdev
->weight
[i
] == 0)
102 mgmtdev
->weight
[i
] = 1;
105 pm_runtime_get_sync(&mgmtdev
->pdev
->dev
);
106 val
= readl(mgmtdev
->virtaddr
+ HIDMA_MAX_BUS_REQ_LEN_OFFSET
);
107 val
&= ~(HIDMA_MAX_BUS_REQ_LEN_MASK
<< HIDMA_MAX_BUS_WR_REQ_BIT_POS
);
108 val
|= mgmtdev
->max_write_request
<< HIDMA_MAX_BUS_WR_REQ_BIT_POS
;
109 val
&= ~HIDMA_MAX_BUS_REQ_LEN_MASK
;
110 val
|= mgmtdev
->max_read_request
;
111 writel(val
, mgmtdev
->virtaddr
+ HIDMA_MAX_BUS_REQ_LEN_OFFSET
);
113 val
= readl(mgmtdev
->virtaddr
+ HIDMA_MAX_XACTIONS_OFFSET
);
114 val
&= ~(HIDMA_MAX_WR_XACTIONS_MASK
<< HIDMA_MAX_WR_XACTIONS_BIT_POS
);
115 val
|= mgmtdev
->max_wr_xactions
<< HIDMA_MAX_WR_XACTIONS_BIT_POS
;
116 val
&= ~HIDMA_MAX_RD_XACTIONS_MASK
;
117 val
|= mgmtdev
->max_rd_xactions
;
118 writel(val
, mgmtdev
->virtaddr
+ HIDMA_MAX_XACTIONS_OFFSET
);
120 mgmtdev
->hw_version
=
121 readl(mgmtdev
->virtaddr
+ HIDMA_HW_VERSION_OFFSET
);
122 mgmtdev
->hw_version_major
= (mgmtdev
->hw_version
>> 28) & 0xF;
123 mgmtdev
->hw_version_minor
= (mgmtdev
->hw_version
>> 16) & 0xF;
125 for (i
= 0; i
< mgmtdev
->dma_channels
; i
++) {
126 u32 weight
= mgmtdev
->weight
[i
];
127 u32 priority
= mgmtdev
->priority
[i
];
129 val
= readl(mgmtdev
->virtaddr
+ HIDMA_QOS_N_OFFSET
+ (4 * i
));
130 val
&= ~(1 << HIDMA_PRIORITY_BIT_POS
);
131 val
|= (priority
& 0x1) << HIDMA_PRIORITY_BIT_POS
;
132 val
&= ~(HIDMA_WEIGHT_MASK
<< HIDMA_WRR_BIT_POS
);
133 val
|= (weight
& HIDMA_WEIGHT_MASK
) << HIDMA_WRR_BIT_POS
;
134 writel(val
, mgmtdev
->virtaddr
+ HIDMA_QOS_N_OFFSET
+ (4 * i
));
137 val
= readl(mgmtdev
->virtaddr
+ HIDMA_CHRESET_TIMEOUT_OFFSET
);
138 val
&= ~HIDMA_CHRESET_TIMEOUT_MASK
;
139 val
|= mgmtdev
->chreset_timeout_cycles
& HIDMA_CHRESET_TIMEOUT_MASK
;
140 writel(val
, mgmtdev
->virtaddr
+ HIDMA_CHRESET_TIMEOUT_OFFSET
);
142 pm_runtime_mark_last_busy(&mgmtdev
->pdev
->dev
);
143 pm_runtime_put_autosuspend(&mgmtdev
->pdev
->dev
);
146 EXPORT_SYMBOL_GPL(hidma_mgmt_setup
);
148 static int hidma_mgmt_probe(struct platform_device
*pdev
)
150 struct hidma_mgmt_dev
*mgmtdev
;
151 struct resource
*res
;
152 void __iomem
*virtaddr
;
157 pm_runtime_set_autosuspend_delay(&pdev
->dev
, HIDMA_AUTOSUSPEND_TIMEOUT
);
158 pm_runtime_use_autosuspend(&pdev
->dev
);
159 pm_runtime_set_active(&pdev
->dev
);
160 pm_runtime_enable(&pdev
->dev
);
161 pm_runtime_get_sync(&pdev
->dev
);
163 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
164 virtaddr
= devm_ioremap_resource(&pdev
->dev
, res
);
165 if (IS_ERR(virtaddr
)) {
170 irq
= platform_get_irq(pdev
, 0);
172 dev_err(&pdev
->dev
, "irq resources not found\n");
177 mgmtdev
= devm_kzalloc(&pdev
->dev
, sizeof(*mgmtdev
), GFP_KERNEL
);
183 mgmtdev
->pdev
= pdev
;
184 mgmtdev
->addrsize
= resource_size(res
);
185 mgmtdev
->virtaddr
= virtaddr
;
187 rc
= device_property_read_u32(&pdev
->dev
, "dma-channels",
188 &mgmtdev
->dma_channels
);
190 dev_err(&pdev
->dev
, "number of channels missing\n");
194 rc
= device_property_read_u32(&pdev
->dev
,
195 "channel-reset-timeout-cycles",
196 &mgmtdev
->chreset_timeout_cycles
);
198 dev_err(&pdev
->dev
, "channel reset timeout missing\n");
202 rc
= device_property_read_u32(&pdev
->dev
, "max-write-burst-bytes",
203 &mgmtdev
->max_write_request
);
205 dev_err(&pdev
->dev
, "max-write-burst-bytes missing\n");
209 rc
= device_property_read_u32(&pdev
->dev
, "max-read-burst-bytes",
210 &mgmtdev
->max_read_request
);
212 dev_err(&pdev
->dev
, "max-read-burst-bytes missing\n");
216 rc
= device_property_read_u32(&pdev
->dev
, "max-write-transactions",
217 &mgmtdev
->max_wr_xactions
);
219 dev_err(&pdev
->dev
, "max-write-transactions missing\n");
223 rc
= device_property_read_u32(&pdev
->dev
, "max-read-transactions",
224 &mgmtdev
->max_rd_xactions
);
226 dev_err(&pdev
->dev
, "max-read-transactions missing\n");
230 mgmtdev
->priority
= devm_kcalloc(&pdev
->dev
,
231 mgmtdev
->dma_channels
,
232 sizeof(*mgmtdev
->priority
),
234 if (!mgmtdev
->priority
) {
239 mgmtdev
->weight
= devm_kcalloc(&pdev
->dev
,
240 mgmtdev
->dma_channels
,
241 sizeof(*mgmtdev
->weight
), GFP_KERNEL
);
242 if (!mgmtdev
->weight
) {
247 rc
= hidma_mgmt_setup(mgmtdev
);
249 dev_err(&pdev
->dev
, "setup failed\n");
254 val
= readl(mgmtdev
->virtaddr
+ HIDMA_CFG_OFFSET
);
256 writel(val
, mgmtdev
->virtaddr
+ HIDMA_CFG_OFFSET
);
258 rc
= hidma_mgmt_init_sys(mgmtdev
);
260 dev_err(&pdev
->dev
, "sysfs setup failed\n");
265 "HW rev: %d.%d @ %pa with %d physical channels\n",
266 mgmtdev
->hw_version_major
, mgmtdev
->hw_version_minor
,
267 &res
->start
, mgmtdev
->dma_channels
);
269 platform_set_drvdata(pdev
, mgmtdev
);
270 pm_runtime_mark_last_busy(&pdev
->dev
);
271 pm_runtime_put_autosuspend(&pdev
->dev
);
274 pm_runtime_put_sync_suspend(&pdev
->dev
);
275 pm_runtime_disable(&pdev
->dev
);
279 #if IS_ENABLED(CONFIG_ACPI)
280 static const struct acpi_device_id hidma_mgmt_acpi_ids
[] = {
286 static const struct of_device_id hidma_mgmt_match
[] = {
287 {.compatible
= "qcom,hidma-mgmt-1.0",},
290 MODULE_DEVICE_TABLE(of
, hidma_mgmt_match
);
292 static struct platform_driver hidma_mgmt_driver
= {
293 .probe
= hidma_mgmt_probe
,
295 .name
= "hidma-mgmt",
296 .of_match_table
= hidma_mgmt_match
,
297 .acpi_match_table
= ACPI_PTR(hidma_mgmt_acpi_ids
),
301 module_platform_driver(hidma_mgmt_driver
);
302 MODULE_LICENSE("GPL v2");