Skip to content

Commit e4b3cbd

Browse files
Michal Wilczynskistorulf
authored andcommitted
firmware: thead: Add AON firmware protocol driver
The T-Head TH1520 SoC uses an E902 co-processor running Always-On (AON) firmware to manage power, clock, and other system resources [1]. This patch introduces a driver implementing the AON firmware protocol, allowing the Linux kernel to communicate with the firmware via mailbox channels. Through an RPC-based interface, the kernel can initiate power state transitions, update resource configurations, and perform other AON-related tasks. [1] Link: https://openbeagle.org/beaglev-ahead/beaglev-ahead/-/blob/main/docs/TH1520%20System%20User%20Manual.pdf Signed-off-by: Michal Wilczynski <m.wilczynski@samsung.com> Acked-by: Drew Fustini <drew@pdp7.com> Link: https://lore.kernel.org/r/20250311171900.1549916-3-m.wilczynski@samsung.com Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
1 parent fe59b03 commit e4b3cbd

File tree

5 files changed

+460
-0
lines changed

5 files changed

+460
-0
lines changed

MAINTAINERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20426,10 +20426,12 @@ F: Documentation/devicetree/bindings/net/thead,th1520-gmac.yaml
2042620426
F: Documentation/devicetree/bindings/pinctrl/thead,th1520-pinctrl.yaml
2042720427
F: arch/riscv/boot/dts/thead/
2042820428
F: drivers/clk/thead/clk-th1520-ap.c
20429+
F: drivers/firmware/thead,th1520-aon.c
2042920430
F: drivers/mailbox/mailbox-th1520.c
2043020431
F: drivers/net/ethernet/stmicro/stmmac/dwmac-thead.c
2043120432
F: drivers/pinctrl/pinctrl-th1520.c
2043220433
F: include/dt-bindings/clock/thead,th1520-clk-ap.h
20434+
F: include/linux/firmware/thead/thead,th1520-aon.h
2043320435

2043420436
RNBD BLOCK DRIVERS
2043520437
M: Md. Haris Iqbal <haris.iqbal@ionos.com>

drivers/firmware/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ config SYSFB_SIMPLEFB
212212

213213
If unsure, say Y.
214214

215+
config TH1520_AON_PROTOCOL
216+
tristate "Always-On firmware protocol"
217+
depends on ARCH_THEAD || COMPILE_TEST
218+
help
219+
Power, clock, and resource management capabilities on the TH1520 SoC are
220+
managed by the E902 core. Firmware running on this core communicates with
221+
the kernel through the Always-On protocol, using hardware mailbox as a medium.
222+
Say yes if you need such capabilities.
223+
215224
config TI_SCI_PROTOCOL
216225
tristate "TI System Control Interface (TISCI) Message Protocol"
217226
depends on TI_MESSAGE_MANAGER

drivers/firmware/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ obj-$(CONFIG_RASPBERRYPI_FIRMWARE) += raspberrypi.o
1818
obj-$(CONFIG_FW_CFG_SYSFS) += qemu_fw_cfg.o
1919
obj-$(CONFIG_SYSFB) += sysfb.o
2020
obj-$(CONFIG_SYSFB_SIMPLEFB) += sysfb_simplefb.o
21+
obj-$(CONFIG_TH1520_AON_PROTOCOL) += thead,th1520-aon.o
2122
obj-$(CONFIG_TI_SCI_PROTOCOL) += ti_sci.o
2223
obj-$(CONFIG_TRUSTED_FOUNDATIONS) += trusted_foundations.o
2324
obj-$(CONFIG_TURRIS_MOX_RWTM) += turris-mox-rwtm.o

drivers/firmware/thead,th1520-aon.c

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2021 Alibaba Group Holding Limited.
4+
* Copyright (c) 2024 Samsung Electronics Co., Ltd.
5+
* Author: Michal Wilczynski <m.wilczynski@samsung.com>
6+
*/
7+
8+
#include <linux/device.h>
9+
#include <linux/firmware/thead/thead,th1520-aon.h>
10+
#include <linux/mailbox_client.h>
11+
#include <linux/mailbox_controller.h>
12+
#include <linux/slab.h>
13+
14+
#define MAX_RX_TIMEOUT (msecs_to_jiffies(3000))
15+
#define MAX_TX_TIMEOUT 500
16+
17+
struct th1520_aon_chan {
18+
struct mbox_chan *ch;
19+
struct th1520_aon_rpc_ack_common ack_msg;
20+
struct mbox_client cl;
21+
struct completion done;
22+
23+
/* make sure only one RPC is performed at a time */
24+
struct mutex transaction_lock;
25+
};
26+
27+
struct th1520_aon_msg_req_set_resource_power_mode {
28+
struct th1520_aon_rpc_msg_hdr hdr;
29+
u16 resource;
30+
u16 mode;
31+
u16 reserved[10];
32+
} __packed __aligned(1);
33+
34+
/*
35+
* This type is used to indicate error response for most functions.
36+
*/
37+
enum th1520_aon_error_codes {
38+
LIGHT_AON_ERR_NONE = 0, /* Success */
39+
LIGHT_AON_ERR_VERSION = 1, /* Incompatible API version */
40+
LIGHT_AON_ERR_CONFIG = 2, /* Configuration error */
41+
LIGHT_AON_ERR_PARM = 3, /* Bad parameter */
42+
LIGHT_AON_ERR_NOACCESS = 4, /* Permission error (no access) */
43+
LIGHT_AON_ERR_LOCKED = 5, /* Permission error (locked) */
44+
LIGHT_AON_ERR_UNAVAILABLE = 6, /* Unavailable (out of resources) */
45+
LIGHT_AON_ERR_NOTFOUND = 7, /* Not found */
46+
LIGHT_AON_ERR_NOPOWER = 8, /* No power */
47+
LIGHT_AON_ERR_IPC = 9, /* Generic IPC error */
48+
LIGHT_AON_ERR_BUSY = 10, /* Resource is currently busy/active */
49+
LIGHT_AON_ERR_FAIL = 11, /* General I/O failure */
50+
LIGHT_AON_ERR_LAST
51+
};
52+
53+
static int th1520_aon_linux_errmap[LIGHT_AON_ERR_LAST] = {
54+
0, /* LIGHT_AON_ERR_NONE */
55+
-EINVAL, /* LIGHT_AON_ERR_VERSION */
56+
-EINVAL, /* LIGHT_AON_ERR_CONFIG */
57+
-EINVAL, /* LIGHT_AON_ERR_PARM */
58+
-EACCES, /* LIGHT_AON_ERR_NOACCESS */
59+
-EACCES, /* LIGHT_AON_ERR_LOCKED */
60+
-ERANGE, /* LIGHT_AON_ERR_UNAVAILABLE */
61+
-EEXIST, /* LIGHT_AON_ERR_NOTFOUND */
62+
-EPERM, /* LIGHT_AON_ERR_NOPOWER */
63+
-EPIPE, /* LIGHT_AON_ERR_IPC */
64+
-EBUSY, /* LIGHT_AON_ERR_BUSY */
65+
-EIO, /* LIGHT_AON_ERR_FAIL */
66+
};
67+
68+
static inline int th1520_aon_to_linux_errno(int errno)
69+
{
70+
if (errno >= LIGHT_AON_ERR_NONE && errno < LIGHT_AON_ERR_LAST)
71+
return th1520_aon_linux_errmap[errno];
72+
73+
return -EIO;
74+
}
75+
76+
static void th1520_aon_rx_callback(struct mbox_client *c, void *rx_msg)
77+
{
78+
struct th1520_aon_chan *aon_chan =
79+
container_of(c, struct th1520_aon_chan, cl);
80+
struct th1520_aon_rpc_msg_hdr *hdr =
81+
(struct th1520_aon_rpc_msg_hdr *)rx_msg;
82+
u8 recv_size = sizeof(struct th1520_aon_rpc_msg_hdr) + hdr->size;
83+
84+
if (recv_size != sizeof(struct th1520_aon_rpc_ack_common)) {
85+
dev_err(c->dev, "Invalid ack size, not completing\n");
86+
return;
87+
}
88+
89+
memcpy(&aon_chan->ack_msg, rx_msg, recv_size);
90+
complete(&aon_chan->done);
91+
}
92+
93+
/**
94+
* th1520_aon_call_rpc() - Send an RPC request to the TH1520 AON subsystem
95+
* @aon_chan: Pointer to the AON channel structure
96+
* @msg: Pointer to the message (RPC payload) that will be sent
97+
*
98+
* This function sends an RPC message to the TH1520 AON subsystem via mailbox.
99+
* It takes the provided @msg buffer, formats it with version and service flags,
100+
* then blocks until the RPC completes or times out. The completion is signaled
101+
* by the `aon_chan->done` completion, which is waited upon for a duration
102+
* defined by `MAX_RX_TIMEOUT`.
103+
*
104+
* Return:
105+
* * 0 on success
106+
* * -ETIMEDOUT if the RPC call times out
107+
* * A negative error code if the mailbox send fails or if AON responds with
108+
* a non-zero error code (converted via th1520_aon_to_linux_errno()).
109+
*/
110+
int th1520_aon_call_rpc(struct th1520_aon_chan *aon_chan, void *msg)
111+
{
112+
struct th1520_aon_rpc_msg_hdr *hdr = msg;
113+
int ret;
114+
115+
mutex_lock(&aon_chan->transaction_lock);
116+
reinit_completion(&aon_chan->done);
117+
118+
RPC_SET_VER(hdr, TH1520_AON_RPC_VERSION);
119+
RPC_SET_SVC_ID(hdr, hdr->svc);
120+
RPC_SET_SVC_FLAG_MSG_TYPE(hdr, RPC_SVC_MSG_TYPE_DATA);
121+
RPC_SET_SVC_FLAG_ACK_TYPE(hdr, RPC_SVC_MSG_NEED_ACK);
122+
123+
ret = mbox_send_message(aon_chan->ch, msg);
124+
if (ret < 0) {
125+
dev_err(aon_chan->cl.dev, "RPC send msg failed: %d\n", ret);
126+
goto out;
127+
}
128+
129+
if (!wait_for_completion_timeout(&aon_chan->done, MAX_RX_TIMEOUT)) {
130+
dev_err(aon_chan->cl.dev, "RPC send msg timeout\n");
131+
mutex_unlock(&aon_chan->transaction_lock);
132+
return -ETIMEDOUT;
133+
}
134+
135+
ret = aon_chan->ack_msg.err_code;
136+
137+
out:
138+
mutex_unlock(&aon_chan->transaction_lock);
139+
140+
return th1520_aon_to_linux_errno(ret);
141+
}
142+
EXPORT_SYMBOL_GPL(th1520_aon_call_rpc);
143+
144+
/**
145+
* th1520_aon_power_update() - Change power state of a resource via TH1520 AON
146+
* @aon_chan: Pointer to the AON channel structure
147+
* @rsrc: Resource ID whose power state needs to be updated
148+
* @power_on: Boolean indicating whether the resource should be powered on (true)
149+
* or powered off (false)
150+
*
151+
* This function requests the TH1520 AON subsystem to set the power mode of the
152+
* given resource (@rsrc) to either on or off. It constructs the message in
153+
* `struct th1520_aon_msg_req_set_resource_power_mode` and then invokes
154+
* th1520_aon_call_rpc() to make the request. If the AON call fails, an error
155+
* message is logged along with the specific return code.
156+
*
157+
* Return:
158+
* * 0 on success
159+
* * A negative error code in case of failures (propagated from
160+
* th1520_aon_call_rpc()).
161+
*/
162+
int th1520_aon_power_update(struct th1520_aon_chan *aon_chan, u16 rsrc,
163+
bool power_on)
164+
{
165+
struct th1520_aon_msg_req_set_resource_power_mode msg = {};
166+
struct th1520_aon_rpc_msg_hdr *hdr = &msg.hdr;
167+
int ret;
168+
169+
hdr->svc = TH1520_AON_RPC_SVC_PM;
170+
hdr->func = TH1520_AON_PM_FUNC_SET_RESOURCE_POWER_MODE;
171+
hdr->size = TH1520_AON_RPC_MSG_NUM;
172+
173+
RPC_SET_BE16(&msg.resource, 0, rsrc);
174+
RPC_SET_BE16(&msg.resource, 2,
175+
(power_on ? TH1520_AON_PM_PW_MODE_ON :
176+
TH1520_AON_PM_PW_MODE_OFF));
177+
178+
ret = th1520_aon_call_rpc(aon_chan, &msg);
179+
if (ret)
180+
dev_err(aon_chan->cl.dev, "failed to power %s resource %d ret %d\n",
181+
power_on ? "up" : "off", rsrc, ret);
182+
183+
return ret;
184+
}
185+
EXPORT_SYMBOL_GPL(th1520_aon_power_update);
186+
187+
/**
188+
* th1520_aon_init() - Initialize TH1520 AON firmware protocol interface
189+
* @dev: Device pointer for the AON subsystem
190+
*
191+
* This function initializes the TH1520 AON firmware protocol interface by:
192+
* - Allocating and initializing the AON channel structure
193+
* - Setting up the mailbox client
194+
* - Requesting the AON mailbox channel
195+
* - Initializing synchronization primitives
196+
*
197+
* Return:
198+
* * Valid pointer to th1520_aon_chan structure on success
199+
* * ERR_PTR(-ENOMEM) if memory allocation fails
200+
* * ERR_PTR() with other negative error codes from mailbox operations
201+
*/
202+
struct th1520_aon_chan *th1520_aon_init(struct device *dev)
203+
{
204+
struct th1520_aon_chan *aon_chan;
205+
struct mbox_client *cl;
206+
207+
aon_chan = kzalloc(sizeof(*aon_chan), GFP_KERNEL);
208+
if (!aon_chan)
209+
return ERR_PTR(-ENOMEM);
210+
211+
cl = &aon_chan->cl;
212+
cl->dev = dev;
213+
cl->tx_block = true;
214+
cl->tx_tout = MAX_TX_TIMEOUT;
215+
cl->rx_callback = th1520_aon_rx_callback;
216+
217+
aon_chan->ch = mbox_request_channel_byname(cl, "aon");
218+
if (IS_ERR(aon_chan->ch)) {
219+
dev_err(dev, "Failed to request aon mbox chan\n");
220+
kfree(aon_chan);
221+
return ERR_CAST(aon_chan->ch);
222+
}
223+
224+
mutex_init(&aon_chan->transaction_lock);
225+
init_completion(&aon_chan->done);
226+
227+
return aon_chan;
228+
}
229+
EXPORT_SYMBOL_GPL(th1520_aon_init);
230+
231+
/**
232+
* th1520_aon_deinit() - Clean up TH1520 AON firmware protocol interface
233+
* @aon_chan: Pointer to the AON channel structure to clean up
234+
*
235+
* This function cleans up resources allocated by th1520_aon_init():
236+
* - Frees the mailbox channel
237+
* - Frees the AON channel
238+
*/
239+
void th1520_aon_deinit(struct th1520_aon_chan *aon_chan)
240+
{
241+
mbox_free_channel(aon_chan->ch);
242+
kfree(aon_chan);
243+
}
244+
EXPORT_SYMBOL_GPL(th1520_aon_deinit);
245+
246+
MODULE_AUTHOR("Michal Wilczynski <m.wilczynski@samsung.com>");
247+
MODULE_DESCRIPTION("T-HEAD TH1520 Always-On firmware protocol library");
248+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)