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>
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 {
31 unsigned long bootaddr_mask;
35 struct st_rproc_config *config;
36 struct reset_control *sw_reset;
37 struct reset_control *pwr_reset;
40 struct regmap *boot_base;
44 static int st_rproc_start(struct rproc *rproc)
46 struct st_rproc *ddata = rproc->priv;
49 regmap_update_bits(ddata->boot_base, ddata->boot_offset,
50 ddata->config->bootaddr_mask, rproc->bootaddr);
52 err = clk_enable(ddata->clk);
54 dev_err(&rproc->dev, "Failed to enable clock\n");
58 if (ddata->config->sw_reset) {
59 err = reset_control_deassert(ddata->sw_reset);
61 dev_err(&rproc->dev, "Failed to deassert S/W Reset\n");
66 if (ddata->config->pwr_reset) {
67 err = reset_control_deassert(ddata->pwr_reset);
69 dev_err(&rproc->dev, "Failed to deassert Power Reset\n");
74 dev_info(&rproc->dev, "Started from 0x%x\n", rproc->bootaddr);
80 if (ddata->config->pwr_reset)
81 reset_control_assert(ddata->sw_reset);
83 clk_disable(ddata->clk);
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);
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);
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)
133 return !reset_sw && !reset_pwr;
136 static const struct st_rproc_config st40_rproc_cfg = {
139 .bootaddr_mask = GENMASK(28, 1),
142 static const struct st_rproc_config st231_rproc_cfg = {
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;
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);
187 dev_err(dev, "failed to get clock frequency\n");
191 ddata->boot_base = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
192 if (!ddata->boot_base) {
193 dev_err(dev, "Boot base not found\n");
197 err = of_property_read_u32_index(np, "st,syscfg", 1,
198 &ddata->boot_offset);
200 dev_err(dev, "Boot offset not found\n");
204 err = of_reserved_mem_device_init(dev);
206 dev_err(dev, "Failed to obtain shared memory\n");
210 err = clk_prepare(ddata->clk);
212 dev_err(dev, "failed to get clock\n");
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;
227 match = of_match_device(st_rproc_match, dev);
228 if (!match || !match->data) {
229 dev_err(dev, "No device match found\n");
233 rproc = rproc_alloc(dev, np->name, &st_rproc_ops, NULL, sizeof(*ddata));
237 rproc->has_iommu = false;
239 ddata->config = (struct st_rproc_config *)match->data;
241 platform_set_drvdata(pdev, rproc);
243 ret = st_rproc_parse_dt(pdev);
247 enabled = st_rproc_state(pdev);
252 atomic_inc(&rproc->power);
253 rproc->state = RPROC_RUNNING;
255 clk_set_rate(ddata->clk, ddata->clk_rate);
258 ret = rproc_add(rproc);
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;
276 clk_disable_unprepare(ddata->clk);
278 of_reserved_mem_device_release(&pdev->dev);
285 static struct platform_driver st_rproc_driver = {
286 .probe = st_rproc_probe,
287 .remove = st_rproc_remove,
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");