blob: 87e1a46875709c7314a4d4c613a171f8108a5bec [file] [log] [blame]
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -08001/* Copyright (c) 2012 Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/io.h>
19#include <linux/clk.h>
20#include <linux/iommu.h>
21#include <linux/interrupt.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/atomic.h>
25#include <linux/of.h>
26#include <linux/of_address.h>
27#include <linux/of_device.h>
28
29#include <mach/iommu_hw-v2.h>
30#include <mach/iommu.h>
31
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080032static int msm_iommu_parse_dt(struct platform_device *pdev,
33 struct msm_iommu_drvdata *drvdata)
34{
35 struct device_node *child;
36 int ret;
37
38 ret = device_move(&pdev->dev, &msm_iommu_root_dev->dev, DPM_ORDER_NONE);
39 if (ret)
40 return ret;
41
42 for_each_child_of_node(pdev->dev.of_node, child) {
43 drvdata->ncb++;
44 if (!of_platform_device_create(child, NULL, &pdev->dev))
45 pr_err("Failed to create %s device\n", child->name);
46 }
47
48 drvdata->name = dev_name(&pdev->dev);
49 return 0;
50}
51
52static atomic_t msm_iommu_next_id = ATOMIC_INIT(-1);
53
54static int __devinit msm_iommu_probe(struct platform_device *pdev)
55{
56 struct msm_iommu_drvdata *drvdata;
57 struct resource *r;
58 int ret;
59
60 if (msm_iommu_root_dev == pdev)
61 return 0;
62
63 if (pdev->id == -1)
64 pdev->id = atomic_inc_return(&msm_iommu_next_id) - 1;
65
66 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
67 if (!drvdata)
68 return -ENOMEM;
69
70 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
71 if (!r)
72 return -EINVAL;
73
74 drvdata->base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
75 if (!drvdata->base)
76 return -ENOMEM;
77
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -070078 drvdata->gdsc = devm_regulator_get(&pdev->dev, "vdd");
79 if (IS_ERR(drvdata->gdsc))
80 return -EINVAL;
81
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -080082 drvdata->pclk = clk_get(&pdev->dev, "iface_clk");
83 if (IS_ERR(drvdata->pclk))
84 return PTR_ERR(drvdata->pclk);
85
86 ret = clk_prepare_enable(drvdata->pclk);
87 if (ret)
88 goto fail_enable;
89
90 drvdata->clk = clk_get(&pdev->dev, "core_clk");
91 if (!IS_ERR(drvdata->clk)) {
92 if (clk_get_rate(drvdata->clk) == 0) {
93 ret = clk_round_rate(drvdata->clk, 1);
94 clk_set_rate(drvdata->clk, ret);
95 }
96
97 ret = clk_prepare_enable(drvdata->clk);
98 if (ret) {
99 clk_put(drvdata->clk);
100 goto fail_pclk;
101 }
102 } else
103 drvdata->clk = NULL;
104
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800105 ret = msm_iommu_parse_dt(pdev, drvdata);
106 if (ret)
107 goto fail_clk;
108
109 pr_info("device %s mapped at %p, with %d ctx banks\n",
110 drvdata->name, drvdata->base, drvdata->ncb);
111
112 platform_set_drvdata(pdev, drvdata);
113
114 if (drvdata->clk)
115 clk_disable_unprepare(drvdata->clk);
116
117 clk_disable_unprepare(drvdata->pclk);
118
119 return 0;
120
121fail_clk:
122 if (drvdata->clk) {
123 clk_disable_unprepare(drvdata->clk);
124 clk_put(drvdata->clk);
125 }
126fail_pclk:
127 clk_disable_unprepare(drvdata->pclk);
128fail_enable:
129 clk_put(drvdata->pclk);
130 return ret;
131}
132
133static int __devexit msm_iommu_remove(struct platform_device *pdev)
134{
135 struct msm_iommu_drvdata *drv = NULL;
136
137 drv = platform_get_drvdata(pdev);
138 if (drv) {
139 if (drv->clk)
140 clk_put(drv->clk);
141 clk_put(drv->pclk);
142 platform_set_drvdata(pdev, NULL);
143 }
144 return 0;
145}
146
147static int msm_iommu_ctx_parse_dt(struct platform_device *pdev,
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800148 struct msm_iommu_ctx_drvdata *ctx_drvdata)
149{
150 struct resource *r, rp;
Sathish Ambleycf045e62012-06-07 12:56:50 -0700151 int irq, ret;
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800152
153 irq = platform_get_irq(pdev, 0);
154 if (irq > 0) {
155 ret = request_threaded_irq(irq, NULL,
156 msm_iommu_fault_handler_v2,
157 IRQF_ONESHOT | IRQF_SHARED,
158 "msm_iommu_nonsecure_irq", pdev);
159 if (ret) {
160 pr_err("Request IRQ %d failed with ret=%d\n", irq, ret);
161 return ret;
162 }
163 }
164
165 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
166 if (!r)
167 return -EINVAL;
168
169 ret = of_address_to_resource(pdev->dev.parent->of_node, 0, &rp);
170 if (ret)
171 return -EINVAL;
172
173 /* Calculate the context bank number using the base addresses. The
174 * first 8 pages belong to the global address space which is followed
175 * by the context banks, hence subtract by 8 to get the context bank
176 * number.
177 */
178 ctx_drvdata->num = ((r->start - rp.start) >> CTX_SHIFT) - 8;
179
180 if (of_property_read_string(pdev->dev.of_node, "qcom,iommu-ctx-name",
181 &ctx_drvdata->name))
182 ctx_drvdata->name = dev_name(&pdev->dev);
183
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800184 return 0;
185}
186
187static int __devinit msm_iommu_ctx_probe(struct platform_device *pdev)
188{
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800189 struct msm_iommu_ctx_drvdata *ctx_drvdata = NULL;
190 int ret;
191
192 if (!pdev->dev.parent)
193 return -EINVAL;
194
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800195 ctx_drvdata = devm_kzalloc(&pdev->dev, sizeof(*ctx_drvdata),
196 GFP_KERNEL);
197 if (!ctx_drvdata)
198 return -ENOMEM;
199
200 ctx_drvdata->pdev = pdev;
201 INIT_LIST_HEAD(&ctx_drvdata->attached_elm);
202 platform_set_drvdata(pdev, ctx_drvdata);
203
Sathish Ambleycf045e62012-06-07 12:56:50 -0700204 ret = msm_iommu_ctx_parse_dt(pdev, ctx_drvdata);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800205 if (!ret)
206 dev_info(&pdev->dev, "context %s using bank %d\n",
207 dev_name(&pdev->dev), ctx_drvdata->num);
208
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800209 return ret;
210}
211
212static int __devexit msm_iommu_ctx_remove(struct platform_device *pdev)
213{
214 platform_set_drvdata(pdev, NULL);
215 return 0;
216}
217
218static struct of_device_id msm_iommu_match_table[] = {
219 { .compatible = "qcom,msm-smmu-v2", },
220 {}
221};
222
223static struct platform_driver msm_iommu_driver = {
224 .driver = {
225 .name = "msm_iommu_v2",
226 .of_match_table = msm_iommu_match_table,
227 },
228 .probe = msm_iommu_probe,
229 .remove = __devexit_p(msm_iommu_remove),
230};
231
232static struct of_device_id msm_iommu_ctx_match_table[] = {
233 { .name = "qcom,iommu-ctx", },
234 {}
235};
236
237static struct platform_driver msm_iommu_ctx_driver = {
238 .driver = {
239 .name = "msm_iommu_ctx_v2",
240 .of_match_table = msm_iommu_ctx_match_table,
241 },
242 .probe = msm_iommu_ctx_probe,
243 .remove = __devexit_p(msm_iommu_ctx_remove),
244};
245
246static int __init msm_iommu_driver_init(void)
247{
248 struct device_node *node;
249 int ret;
250
251 node = of_find_compatible_node(NULL, NULL, "qcom,msm-smmu-v2");
252 if (!node)
253 return -ENODEV;
254
255 of_node_put(node);
256
257 msm_iommu_root_dev = platform_device_register_simple(
258 "msm_iommu", -1, 0, 0);
259 if (!msm_iommu_root_dev) {
260 pr_err("Failed to create root IOMMU device\n");
261 ret = -ENODEV;
262 goto error;
263 }
264
265 atomic_inc(&msm_iommu_next_id);
266
267 ret = platform_driver_register(&msm_iommu_driver);
268 if (ret != 0) {
269 pr_err("Failed to register IOMMU driver\n");
270 goto error;
271 }
272
273 ret = platform_driver_register(&msm_iommu_ctx_driver);
274 if (ret != 0) {
275 pr_err("Failed to register IOMMU context driver\n");
276 goto error;
277 }
278
279error:
280 return ret;
281}
282
283static void __exit msm_iommu_driver_exit(void)
284{
285 platform_driver_unregister(&msm_iommu_ctx_driver);
286 platform_driver_unregister(&msm_iommu_driver);
287 platform_device_unregister(msm_iommu_root_dev);
288}
289
290subsys_initcall(msm_iommu_driver_init);
291module_exit(msm_iommu_driver_exit);
292
293MODULE_LICENSE("GPL v2");