blob: 3e322c4d7191a61c8d58b5d5d8c39b58f96b71e2 [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 */
Patrick Delaunaye01d5562020-11-06 19:01:34 +01005#define LOG_CATEGORY UCLASS_REMOTEPROC
6
Fabien Dessennee09846a2019-05-31 15:11:34 +02007#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Fabien Dessennee09846a2019-05-31 15:11:34 +020012#include <remoteproc.h>
13#include <reset.h>
Fabien Dessennee09846a2019-05-31 15:11:34 +020014#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060017#include <linux/printk.h>
Fabien Dessennee09846a2019-05-31 15:11:34 +020018
Fabien Dessennee09846a2019-05-31 15:11:34 +020019/**
20 * struct stm32_copro_privdata - power processor private data
21 * @reset_ctl: reset controller handle
Patrick Delaunaya8414892020-10-15 15:01:12 +020022 * @hold_boot: hold boot controller handle
Fabien Dessenneec452e12019-10-30 14:38:33 +010023 * @rsc_table_addr: resource table address
Fabien Dessennee09846a2019-05-31 15:11:34 +020024 */
25struct stm32_copro_privdata {
26 struct reset_ctl reset_ctl;
Patrick Delaunaya8414892020-10-15 15:01:12 +020027 struct reset_ctl hold_boot;
Fabien Dessenneec452e12019-10-30 14:38:33 +010028 ulong rsc_table_addr;
Fabien Dessennee09846a2019-05-31 15:11:34 +020029};
30
31/**
32 * stm32_copro_probe() - Basic probe
33 * @dev: corresponding STM32 remote processor device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010034 * Return: 0 if all went ok, else corresponding -ve error
Fabien Dessennee09846a2019-05-31 15:11:34 +020035 */
36static int stm32_copro_probe(struct udevice *dev)
37{
38 struct stm32_copro_privdata *priv;
Patrick Delaunaya8414892020-10-15 15:01:12 +020039 int ret;
Fabien Dessennee09846a2019-05-31 15:11:34 +020040
41 priv = dev_get_priv(dev);
42
Patrick Delaunaya8414892020-10-15 15:01:12 +020043 ret = reset_get_by_name(dev, "mcu_rst", &priv->reset_ctl);
44 if (ret) {
45 dev_err(dev, "failed to get reset (%d)\n", ret);
46 return ret;
Fabien Dessennee09846a2019-05-31 15:11:34 +020047 }
48
Patrick Delaunaya8414892020-10-15 15:01:12 +020049 ret = reset_get_by_name(dev, "hold_boot", &priv->hold_boot);
Fabien Dessennee09846a2019-05-31 15:11:34 +020050 if (ret) {
Patrick Delaunaya8414892020-10-15 15:01:12 +020051 dev_err(dev, "failed to get hold boot (%d)\n", ret);
Fabien Dessennee09846a2019-05-31 15:11:34 +020052 return ret;
53 }
54
55 dev_dbg(dev, "probed\n");
56
57 return 0;
58}
59
60/**
Fabien Dessennee09846a2019-05-31 15:11:34 +020061 * stm32_copro_device_to_virt() - Convert device address to virtual address
62 * @dev: corresponding STM32 remote processor device
63 * @da: device address
Lokesh Vutlae18166f2019-09-04 16:01:27 +053064 * @size: Size of the memory region @da is pointing to
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010065 * Return: converted virtual address
Fabien Dessennee09846a2019-05-31 15:11:34 +020066 */
Lokesh Vutlae18166f2019-09-04 16:01:27 +053067static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
68 ulong size)
Fabien Dessennee09846a2019-05-31 15:11:34 +020069{
Lokesh Vutlae18166f2019-09-04 16:01:27 +053070 fdt32_t in_addr = cpu_to_be32(da), end_addr;
Fabien Dessennee09846a2019-05-31 15:11:34 +020071 u64 paddr;
72
73 paddr = dev_translate_dma_address(dev, &in_addr);
74 if (paddr == OF_BAD_ADDR) {
75 dev_err(dev, "Unable to convert address %ld\n", da);
76 return NULL;
77 }
78
Lokesh Vutlae18166f2019-09-04 16:01:27 +053079 end_addr = cpu_to_be32(da + size - 1);
80 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
81 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
82 return NULL;
83 }
84
Fabien Dessennee09846a2019-05-31 15:11:34 +020085 return phys_to_virt(paddr);
86}
87
88/**
89 * stm32_copro_load() - Loadup the STM32 remote processor
90 * @dev: corresponding STM32 remote processor device
91 * @addr: Address in memory where image is stored
92 * @size: Size in bytes of the image
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010093 * Return: 0 if all went ok, else corresponding -ve error
Fabien Dessennee09846a2019-05-31 15:11:34 +020094 */
95static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
96{
97 struct stm32_copro_privdata *priv;
Fabien Dessenneec452e12019-10-30 14:38:33 +010098 ulong rsc_table_size;
Fabien Dessennee09846a2019-05-31 15:11:34 +020099 int ret;
100
101 priv = dev_get_priv(dev);
102
Patrick Delaunaya8414892020-10-15 15:01:12 +0200103 ret = reset_assert(&priv->hold_boot);
104 if (ret) {
105 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200106 return ret;
Patrick Delaunaya8414892020-10-15 15:01:12 +0200107 }
Fabien Dessennee09846a2019-05-31 15:11:34 +0200108
109 ret = reset_assert(&priv->reset_ctl);
110 if (ret) {
111 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
112 return ret;
113 }
114
Fabien Dessenneec452e12019-10-30 14:38:33 +0100115 if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
116 &rsc_table_size)) {
117 priv->rsc_table_addr = 0;
118 dev_warn(dev, "No valid resource table for this firmware\n");
119 }
120
Lokesh Vutlab506cba2019-09-04 16:01:28 +0530121 return rproc_elf32_load_image(dev, addr, size);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200122}
123
124/**
125 * stm32_copro_start() - Start the STM32 remote processor
126 * @dev: corresponding STM32 remote processor device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100127 * Return: 0 if all went ok, else corresponding -ve error
Fabien Dessennee09846a2019-05-31 15:11:34 +0200128 */
129static int stm32_copro_start(struct udevice *dev)
130{
Fabien Dessenneec452e12019-10-30 14:38:33 +0100131 struct stm32_copro_privdata *priv;
Fabien Dessennee09846a2019-05-31 15:11:34 +0200132 int ret;
133
Fabien Dessenneec452e12019-10-30 14:38:33 +0100134 priv = dev_get_priv(dev);
135
Patrick Delaunaya8414892020-10-15 15:01:12 +0200136 ret = reset_deassert(&priv->hold_boot);
137 if (ret) {
138 dev_err(dev, "Unable to deassert hold boot (ret=%d)\n", ret);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200139 return ret;
Patrick Delaunaya8414892020-10-15 15:01:12 +0200140 }
Fabien Dessennee09846a2019-05-31 15:11:34 +0200141
142 /*
143 * Once copro running, reset hold boot flag to avoid copro
Patrick Delaunayb78ceb42020-10-15 15:01:13 +0200144 * rebooting autonomously (error should never occur)
Fabien Dessennee09846a2019-05-31 15:11:34 +0200145 */
Patrick Delaunaya8414892020-10-15 15:01:12 +0200146 ret = reset_assert(&priv->hold_boot);
147 if (ret)
148 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
149
Patrick Delaunayb78ceb42020-10-15 15:01:13 +0200150 /* indicates that copro is running */
151 writel(TAMP_COPRO_STATE_CRUN, TAMP_COPRO_STATE);
152 /* Store rsc_address in bkp register */
153 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
Fabien Dessenneec452e12019-10-30 14:38:33 +0100154
Patrick Delaunayb78ceb42020-10-15 15:01:13 +0200155 return 0;
Fabien Dessennee09846a2019-05-31 15:11:34 +0200156}
157
158/**
159 * stm32_copro_reset() - Reset the STM32 remote processor
160 * @dev: corresponding STM32 remote processor device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100161 * Return: 0 if all went ok, else corresponding -ve error
Fabien Dessennee09846a2019-05-31 15:11:34 +0200162 */
163static int stm32_copro_reset(struct udevice *dev)
164{
165 struct stm32_copro_privdata *priv;
166 int ret;
167
168 priv = dev_get_priv(dev);
169
Patrick Delaunaya8414892020-10-15 15:01:12 +0200170 ret = reset_assert(&priv->hold_boot);
171 if (ret) {
172 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200173 return ret;
Patrick Delaunaya8414892020-10-15 15:01:12 +0200174 }
Fabien Dessennee09846a2019-05-31 15:11:34 +0200175
176 ret = reset_assert(&priv->reset_ctl);
177 if (ret) {
178 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
179 return ret;
180 }
181
Fabien Dessenne4446d2f2019-10-30 14:38:31 +0100182 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200183
184 return 0;
185}
186
187/**
188 * stm32_copro_stop() - Stop the STM32 remote processor
189 * @dev: corresponding STM32 remote processor device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100190 * Return: 0 if all went ok, else corresponding -ve error
Fabien Dessennee09846a2019-05-31 15:11:34 +0200191 */
192static int stm32_copro_stop(struct udevice *dev)
193{
194 return stm32_copro_reset(dev);
195}
196
197/**
198 * stm32_copro_is_running() - Is the STM32 remote processor running
199 * @dev: corresponding STM32 remote processor device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100200 * Return: 0 if the remote processor is running, 1 otherwise
Fabien Dessennee09846a2019-05-31 15:11:34 +0200201 */
202static int stm32_copro_is_running(struct udevice *dev)
203{
Fabien Dessenne4446d2f2019-10-30 14:38:31 +0100204 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
Fabien Dessennee09846a2019-05-31 15:11:34 +0200205}
206
207static const struct dm_rproc_ops stm32_copro_ops = {
208 .load = stm32_copro_load,
209 .start = stm32_copro_start,
210 .stop = stm32_copro_stop,
211 .reset = stm32_copro_reset,
212 .is_running = stm32_copro_is_running,
213 .device_to_virt = stm32_copro_device_to_virt,
214};
215
216static const struct udevice_id stm32_copro_ids[] = {
Patrick Delaunay26c24b42019-08-02 15:07:18 +0200217 {.compatible = "st,stm32mp1-m4"},
Fabien Dessennee09846a2019-05-31 15:11:34 +0200218 {}
219};
220
221U_BOOT_DRIVER(stm32_copro) = {
222 .name = "stm32_m4_proc",
223 .of_match = stm32_copro_ids,
224 .id = UCLASS_REMOTEPROC,
225 .ops = &stm32_copro_ops,
226 .probe = stm32_copro_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700227 .priv_auto = sizeof(struct stm32_copro_privdata),
Fabien Dessennee09846a2019-05-31 15:11:34 +0200228};