Merge branch 'r6040-next'
[linux/fpc-iii.git] / drivers / remoteproc / st_remoteproc.c
blob6f056caa8a56c6ab22ad949db5c43d9fbe8e6844
1 /*
2 * ST's Remote Processor Control Driver
4 * Copyright (C) 2015 STMicroelectronics - All Rights Reserved
6 * Author: Ludovic Barre <ludovic.barre@st.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/clk.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/err.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_device.h>
22 #include <linux/of_reserved_mem.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/remoteproc.h>
26 #include <linux/reset.h>
28 struct st_rproc_config {
29 bool sw_reset;
30 bool pwr_reset;
31 unsigned long bootaddr_mask;
34 struct st_rproc {
35 struct st_rproc_config *config;
36 struct reset_control *sw_reset;
37 struct reset_control *pwr_reset;
38 struct clk *clk;
39 u32 clk_rate;
40 struct regmap *boot_base;
41 u32 boot_offset;
44 static int st_rproc_start(struct rproc *rproc)
46 struct st_rproc *ddata = rproc->priv;
47 int err;
49 regmap_update_bits(ddata->boot_base, ddata->boot_offset,
50 ddata->config->bootaddr_mask, rproc->bootaddr);
52 err = clk_enable(ddata->clk);
53 if (err) {
54 dev_err(&rproc->dev, "Failed to enable clock\n");
55 return err;
58 if (ddata->config->sw_reset) {
59 err = reset_control_deassert(ddata->sw_reset);
60 if (err) {
61 dev_err(&rproc->dev, "Failed to deassert S/W Reset\n");
62 goto sw_reset_fail;
66 if (ddata->config->pwr_reset) {
67 err = reset_control_deassert(ddata->pwr_reset);
68 if (err) {
69 dev_err(&rproc->dev, "Failed to deassert Power Reset\n");
70 goto pwr_reset_fail;
74 dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
76 return 0;
79 pwr_reset_fail:
80 if (ddata->config->pwr_reset)
81 reset_control_assert(ddata->sw_reset);
82 sw_reset_fail:
83 clk_disable(ddata->clk);
85 return err;
88 static int st_rproc_stop(struct rproc *rproc)
90 struct st_rproc *ddata = rproc->priv;
91 int sw_err = 0, pwr_err = 0;
93 if (ddata->config->sw_reset) {
94 sw_err = reset_control_assert(ddata->sw_reset);
95 if (sw_err)
96 dev_err(&rproc->dev, "Failed to assert S/W Reset\n");
99 if (ddata->config->pwr_reset) {
100 pwr_err = reset_control_assert(ddata->pwr_reset);
101 if (pwr_err)
102 dev_err(&rproc->dev, "Failed to assert Power Reset\n");
105 clk_disable(ddata->clk);
107 return sw_err ?: pwr_err;
110 static struct rproc_ops st_rproc_ops = {
111 .start = st_rproc_start,
112 .stop = st_rproc_stop,
116 * Fetch state of the processor: 0 is off, 1 is on.
118 static int st_rproc_state(struct platform_device *pdev)
120 struct rproc *rproc = platform_get_drvdata(pdev);
121 struct st_rproc *ddata = rproc->priv;
122 int reset_sw = 0, reset_pwr = 0;
124 if (ddata->config->sw_reset)
125 reset_sw = reset_control_status(ddata->sw_reset);
127 if (ddata->config->pwr_reset)
128 reset_pwr = reset_control_status(ddata->pwr_reset);
130 if (reset_sw < 0 || reset_pwr < 0)
131 return -EINVAL;
133 return !reset_sw && !reset_pwr;
136 static const struct st_rproc_config st40_rproc_cfg = {
137 .sw_reset = true,
138 .pwr_reset = true,
139 .bootaddr_mask = GENMASK(28, 1),
142 static const struct st_rproc_config st231_rproc_cfg = {
143 .sw_reset = true,
144 .pwr_reset = false,
145 .bootaddr_mask = GENMASK(31, 6),
148 static const struct of_device_id st_rproc_match[] = {
149 { .compatible = "st,st40-rproc", .data = &st40_rproc_cfg },
150 { .compatible = "st,st231-rproc", .data = &st231_rproc_cfg },
153 MODULE_DEVICE_TABLE(of, st_rproc_match);
155 static int st_rproc_parse_dt(struct platform_device *pdev)
157 struct device *dev = &pdev->dev;
158 struct rproc *rproc = platform_get_drvdata(pdev);
159 struct st_rproc *ddata = rproc->priv;
160 struct device_node *np = dev->of_node;
161 int err;
163 if (ddata->config->sw_reset) {
164 ddata->sw_reset = devm_reset_control_get(dev, "sw_reset");
165 if (IS_ERR(ddata->sw_reset)) {
166 dev_err(dev, "Failed to get S/W Reset\n");
167 return PTR_ERR(ddata->sw_reset);
171 if (ddata->config->pwr_reset) {
172 ddata->pwr_reset = devm_reset_control_get(dev, "pwr_reset");
173 if (IS_ERR(ddata->pwr_reset)) {
174 dev_err(dev, "Failed to get Power Reset\n");
175 return PTR_ERR(ddata->pwr_reset);
179 ddata->clk = devm_clk_get(dev, NULL);
180 if (IS_ERR(ddata->clk)) {
181 dev_err(dev, "Failed to get clock\n");
182 return PTR_ERR(ddata->clk);
185 err = of_property_read_u32(np, "clock-frequency", &ddata->clk_rate);
186 if (err) {
187 dev_err(dev, "failed to get clock frequency\n");
188 return err;
191 ddata->boot_base = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
192 if (IS_ERR(ddata->boot_base)) {
193 dev_err(dev, "Boot base not found\n");
194 return PTR_ERR(ddata->boot_base);
197 err = of_property_read_u32_index(np, "st,syscfg", 1,
198 &ddata->boot_offset);
199 if (err) {
200 dev_err(dev, "Boot offset not found\n");
201 return -EINVAL;
204 err = of_reserved_mem_device_init(dev);
205 if (err) {
206 dev_err(dev, "Failed to obtain shared memory\n");
207 return err;
210 err = clk_prepare(ddata->clk);
211 if (err)
212 dev_err(dev, "failed to get clock\n");
214 return err;
217 static int st_rproc_probe(struct platform_device *pdev)
219 struct device *dev = &pdev->dev;
220 const struct of_device_id *match;
221 struct st_rproc *ddata;
222 struct device_node *np = dev->of_node;
223 struct rproc *rproc;
224 int enabled;
225 int ret;
227 match = of_match_device(st_rproc_match, dev);
228 if (!match || !match->data) {
229 dev_err(dev, "No device match found\n");
230 return -ENODEV;
233 rproc = rproc_alloc(dev, np->name, &st_rproc_ops, NULL, sizeof(*ddata));
234 if (!rproc)
235 return -ENOMEM;
237 rproc->has_iommu = false;
238 ddata = rproc->priv;
239 ddata->config = (struct st_rproc_config *)match->data;
241 platform_set_drvdata(pdev, rproc);
243 ret = st_rproc_parse_dt(pdev);
244 if (ret)
245 goto free_rproc;
247 enabled = st_rproc_state(pdev);
248 if (enabled < 0)
249 goto free_rproc;
251 if (enabled) {
252 atomic_inc(&rproc->power);
253 rproc->state = RPROC_RUNNING;
254 } else {
255 clk_set_rate(ddata->clk, ddata->clk_rate);
258 ret = rproc_add(rproc);
259 if (ret)
260 goto free_rproc;
262 return 0;
264 free_rproc:
265 rproc_put(rproc);
266 return ret;
269 static int st_rproc_remove(struct platform_device *pdev)
271 struct rproc *rproc = platform_get_drvdata(pdev);
272 struct st_rproc *ddata = rproc->priv;
274 rproc_del(rproc);
276 clk_disable_unprepare(ddata->clk);
278 of_reserved_mem_device_release(&pdev->dev);
280 rproc_put(rproc);
282 return 0;
285 static struct platform_driver st_rproc_driver = {
286 .probe = st_rproc_probe,
287 .remove = st_rproc_remove,
288 .driver = {
289 .name = "st-rproc",
290 .of_match_table = of_match_ptr(st_rproc_match),
293 module_platform_driver(st_rproc_driver);
295 MODULE_DESCRIPTION("ST Remote Processor Control Driver");
296 MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
297 MODULE_LICENSE("GPL v2");