blob: dc87cb794e4fdd61b75a869ab7bbded63f53123f [file] [log] [blame]
Fabien Dessennee09846a2019-05-31 15:11:34 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
4 */
5#define pr_fmt(fmt) "%s: " fmt, __func__
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Fabien Dessennee09846a2019-05-31 15:11:34 +020011#include <remoteproc.h>
12#include <reset.h>
Fabien Dessennee09846a2019-05-31 15:11:34 +020013#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <linux/err.h>
Fabien Dessennee09846a2019-05-31 15:11:34 +020016
Fabien Dessennee09846a2019-05-31 15:11:34 +020017/**
18 * struct stm32_copro_privdata - power processor private data
19 * @reset_ctl: reset controller handle
Patrick Delaunaya8414892020-10-15 15:01:12 +020020 * @hold_boot: hold boot controller handle
Fabien Dessenneec452e12019-10-30 14:38:33 +010021 * @rsc_table_addr: resource table address
Fabien Dessennee09846a2019-05-31 15:11:34 +020022 */
23struct stm32_copro_privdata {
24 struct reset_ctl reset_ctl;
Patrick Delaunaya8414892020-10-15 15:01:12 +020025 struct reset_ctl hold_boot;
Fabien Dessenneec452e12019-10-30 14:38:33 +010026 ulong rsc_table_addr;
Fabien Dessennee09846a2019-05-31 15:11:34 +020027};
28
29/**
30 * stm32_copro_probe() - Basic probe
31 * @dev: corresponding STM32 remote processor device
32 * @return 0 if all went ok, else corresponding -ve error
33 */
34static int stm32_copro_probe(struct udevice *dev)
35{
36 struct stm32_copro_privdata *priv;
Patrick Delaunaya8414892020-10-15 15:01:12 +020037 int ret;
Fabien Dessennee09846a2019-05-31 15:11:34 +020038
39 priv = dev_get_priv(dev);
40
Patrick Delaunaya8414892020-10-15 15:01:12 +020041 ret = reset_get_by_name(dev, "mcu_rst", &priv->reset_ctl);
42 if (ret) {
43 dev_err(dev, "failed to get reset (%d)\n", ret);
44 return ret;
Fabien Dessennee09846a2019-05-31 15:11:34 +020045 }
46
Patrick Delaunaya8414892020-10-15 15:01:12 +020047 ret = reset_get_by_name(dev, "hold_boot", &priv->hold_boot);
Fabien Dessennee09846a2019-05-31 15:11:34 +020048 if (ret) {
Patrick Delaunaya8414892020-10-15 15:01:12 +020049 dev_err(dev, "failed to get hold boot (%d)\n", ret);
Fabien Dessennee09846a2019-05-31 15:11:34 +020050 return ret;
51 }
52
53 dev_dbg(dev, "probed\n");
54
55 return 0;
56}
57
58/**
Fabien Dessennee09846a2019-05-31 15:11:34 +020059 * stm32_copro_device_to_virt() - Convert device address to virtual address
60 * @dev: corresponding STM32 remote processor device
61 * @da: device address
Lokesh Vutlae18166f2019-09-04 16:01:27 +053062 * @size: Size of the memory region @da is pointing to
Fabien Dessennee09846a2019-05-31 15:11:34 +020063 * @return converted virtual address
64 */
Lokesh Vutlae18166f2019-09-04 16:01:27 +053065static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
66 ulong size)
Fabien Dessennee09846a2019-05-31 15:11:34 +020067{
Lokesh Vutlae18166f2019-09-04 16:01:27 +053068 fdt32_t in_addr = cpu_to_be32(da), end_addr;
Fabien Dessennee09846a2019-05-31 15:11:34 +020069 u64 paddr;
70
71 paddr = dev_translate_dma_address(dev, &in_addr);
72 if (paddr == OF_BAD_ADDR) {
73 dev_err(dev, "Unable to convert address %ld\n", da);
74 return NULL;
75 }
76
Lokesh Vutlae18166f2019-09-04 16:01:27 +053077 end_addr = cpu_to_be32(da + size - 1);
78 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
79 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
80 return NULL;
81 }
82
Fabien Dessennee09846a2019-05-31 15:11:34 +020083 return phys_to_virt(paddr);
84}
85
86/**
87 * stm32_copro_load() - Loadup the STM32 remote processor
88 * @dev: corresponding STM32 remote processor device
89 * @addr: Address in memory where image is stored
90 * @size: Size in bytes of the image
91 * @return 0 if all went ok, else corresponding -ve error
92 */
93static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
94{
95 struct stm32_copro_privdata *priv;
Fabien Dessenneec452e12019-10-30 14:38:33 +010096 ulong rsc_table_size;
Fabien Dessennee09846a2019-05-31 15:11:34 +020097 int ret;
98
99 priv = dev_get_priv(dev);
100
Patrick Delaunaya8414892020-10-15 15:01:12 +0200101 ret = reset_assert(&priv->hold_boot);
102 if (ret) {
103 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200104 return ret;
Patrick Delaunaya8414892020-10-15 15:01:12 +0200105 }
Fabien Dessennee09846a2019-05-31 15:11:34 +0200106
107 ret = reset_assert(&priv->reset_ctl);
108 if (ret) {
109 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
110 return ret;
111 }
112
Fabien Dessenneec452e12019-10-30 14:38:33 +0100113 if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
114 &rsc_table_size)) {
115 priv->rsc_table_addr = 0;
116 dev_warn(dev, "No valid resource table for this firmware\n");
117 }
118
Lokesh Vutlab506cba2019-09-04 16:01:28 +0530119 return rproc_elf32_load_image(dev, addr, size);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200120}
121
122/**
123 * stm32_copro_start() - Start the STM32 remote processor
124 * @dev: corresponding STM32 remote processor device
125 * @return 0 if all went ok, else corresponding -ve error
126 */
127static int stm32_copro_start(struct udevice *dev)
128{
Fabien Dessenneec452e12019-10-30 14:38:33 +0100129 struct stm32_copro_privdata *priv;
Fabien Dessennee09846a2019-05-31 15:11:34 +0200130 int ret;
131
Fabien Dessenneec452e12019-10-30 14:38:33 +0100132 priv = dev_get_priv(dev);
133
Patrick Delaunaya8414892020-10-15 15:01:12 +0200134 ret = reset_deassert(&priv->hold_boot);
135 if (ret) {
136 dev_err(dev, "Unable to deassert hold boot (ret=%d)\n", ret);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200137 return ret;
Patrick Delaunaya8414892020-10-15 15:01:12 +0200138 }
Fabien Dessennee09846a2019-05-31 15:11:34 +0200139
140 /*
141 * Once copro running, reset hold boot flag to avoid copro
Patrick Delaunayb78ceb42020-10-15 15:01:13 +0200142 * rebooting autonomously (error should never occur)
Fabien Dessennee09846a2019-05-31 15:11:34 +0200143 */
Patrick Delaunaya8414892020-10-15 15:01:12 +0200144 ret = reset_assert(&priv->hold_boot);
145 if (ret)
146 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
147
Patrick Delaunayb78ceb42020-10-15 15:01:13 +0200148 /* indicates that copro is running */
149 writel(TAMP_COPRO_STATE_CRUN, TAMP_COPRO_STATE);
150 /* Store rsc_address in bkp register */
151 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
Fabien Dessenneec452e12019-10-30 14:38:33 +0100152
Patrick Delaunayb78ceb42020-10-15 15:01:13 +0200153 return 0;
Fabien Dessennee09846a2019-05-31 15:11:34 +0200154}
155
156/**
157 * stm32_copro_reset() - Reset the STM32 remote processor
158 * @dev: corresponding STM32 remote processor device
159 * @return 0 if all went ok, else corresponding -ve error
160 */
161static int stm32_copro_reset(struct udevice *dev)
162{
163 struct stm32_copro_privdata *priv;
164 int ret;
165
166 priv = dev_get_priv(dev);
167
Patrick Delaunaya8414892020-10-15 15:01:12 +0200168 ret = reset_assert(&priv->hold_boot);
169 if (ret) {
170 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200171 return ret;
Patrick Delaunaya8414892020-10-15 15:01:12 +0200172 }
Fabien Dessennee09846a2019-05-31 15:11:34 +0200173
174 ret = reset_assert(&priv->reset_ctl);
175 if (ret) {
176 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
177 return ret;
178 }
179
Fabien Dessenne4446d2f2019-10-30 14:38:31 +0100180 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200181
182 return 0;
183}
184
185/**
186 * stm32_copro_stop() - Stop the STM32 remote processor
187 * @dev: corresponding STM32 remote processor device
188 * @return 0 if all went ok, else corresponding -ve error
189 */
190static int stm32_copro_stop(struct udevice *dev)
191{
192 return stm32_copro_reset(dev);
193}
194
195/**
196 * stm32_copro_is_running() - Is the STM32 remote processor running
197 * @dev: corresponding STM32 remote processor device
Fabien Dessenne4446d2f2019-10-30 14:38:31 +0100198 * @return 0 if the remote processor is running, 1 otherwise
Fabien Dessennee09846a2019-05-31 15:11:34 +0200199 */
200static int stm32_copro_is_running(struct udevice *dev)
201{
Fabien Dessenne4446d2f2019-10-30 14:38:31 +0100202 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200203}
204
205static const struct dm_rproc_ops stm32_copro_ops = {
206 .load = stm32_copro_load,
207 .start = stm32_copro_start,
208 .stop = stm32_copro_stop,
209 .reset = stm32_copro_reset,
210 .is_running = stm32_copro_is_running,
211 .device_to_virt = stm32_copro_device_to_virt,
212};
213
214static const struct udevice_id stm32_copro_ids[] = {
Patrick Delaunay26c24b42019-08-02 15:07:18 +0200215 {.compatible = "st,stm32mp1-m4"},
Fabien Dessennee09846a2019-05-31 15:11:34 +0200216 {}
217};
218
219U_BOOT_DRIVER(stm32_copro) = {
220 .name = "stm32_m4_proc",
221 .of_match = stm32_copro_ids,
222 .id = UCLASS_REMOTEPROC,
223 .ops = &stm32_copro_ops,
224 .probe = stm32_copro_probe,
225 .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata),
226};