mtd: spinand: toshiba: Support for new Kioxia Serial NAND
[oweals/u-boot.git] / drivers / remoteproc / k3_system_controller.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Texas Instruments' K3 System Controller Driver
4  *
5  * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
6  *      Lokesh Vutla <lokeshvutla@ti.com>
7  */
8
9 #include <common.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <remoteproc.h>
13 #include <errno.h>
14 #include <mailbox.h>
15 #include <dm/device_compat.h>
16 #include <linux/soc/ti/k3-sec-proxy.h>
17
18 #define K3_MSG_R5_TO_M3_M3FW                    0x8105
19 #define K3_MSG_M3_TO_R5_CERT_RESULT             0x8805
20 #define K3_MSG_M3_TO_R5_BOOT_NOTIFICATION       0x000A
21
22 #define K3_FLAGS_MSG_CERT_AUTH_PASS             0x555555
23 #define K3_FLAGS_MSG_CERT_AUTH_FAIL             0xffffff
24
25 /**
26  * struct k3_sysctrler_msg_hdr - Generic Header for Messages and responses.
27  * @cmd_id:     Message ID. One of K3_MSG_*
28  * @host_id:    Host ID of the message
29  * @seq_ne:     Message identifier indicating a transfer sequence.
30  * @flags:      Flags for the message.
31  */
32 struct k3_sysctrler_msg_hdr {
33         u16 cmd_id;
34         u8 host_id;
35         u8 seq_nr;
36         u32 flags;
37 } __packed;
38
39 /**
40  * struct k3_sysctrler_load_msg - Message format for Firmware loading
41  * @hdr:                Generic message hdr
42  * @buffer_address:     Address at which firmware is located.
43  * @buffer_size:        Size of the firmware.
44  */
45 struct k3_sysctrler_load_msg {
46         struct k3_sysctrler_msg_hdr hdr;
47         u32 buffer_address;
48         u32 buffer_size;
49 } __packed;
50
51 /**
52  * struct k3_sysctrler_boot_notification_msg - Message format for boot
53  *                                             notification
54  * @checksum:           Checksum for the entire message
55  * @reserved:           Reserved for future use.
56  * @hdr:                Generic message hdr
57  */
58 struct k3_sysctrler_boot_notification_msg {
59         u16 checksum;
60         u16 reserved;
61         struct k3_sysctrler_msg_hdr hdr;
62 } __packed;
63
64 /**
65  * struct k3_sysctrler_desc - Description of SoC integration.
66  * @host_id:    Host identifier representing the compute entity
67  * @max_rx_timeout_ms:  Timeout for communication with SoC (in Milliseconds)
68  * @max_msg_size: Maximum size of data per message that can be handled.
69  */
70 struct k3_sysctrler_desc {
71         u8 host_id;
72         int max_rx_timeout_us;
73         int max_msg_size;
74 };
75
76 /**
77  * struct k3_sysctrler_privdata - Structure representing System Controller data.
78  * @chan_tx:    Transmit mailbox channel
79  * @chan_rx:    Receive mailbox channel
80  * @desc:       SoC description for this instance
81  * @seq_nr:     Counter for number of messages sent.
82  */
83 struct k3_sysctrler_privdata {
84         struct mbox_chan chan_tx;
85         struct mbox_chan chan_rx;
86         struct k3_sysctrler_desc *desc;
87         u32 seq_nr;
88 };
89
90 static inline
91 void k3_sysctrler_load_msg_setup(struct k3_sysctrler_load_msg *fw,
92                                  struct k3_sysctrler_privdata *priv,
93                                  ulong addr, ulong size)
94 {
95         fw->hdr.cmd_id = K3_MSG_R5_TO_M3_M3FW;
96         fw->hdr.host_id = priv->desc->host_id;
97         fw->hdr.seq_nr = priv->seq_nr++;
98         fw->hdr.flags = 0x0;
99         fw->buffer_address = addr;
100         fw->buffer_size = size;
101 }
102
103 static int k3_sysctrler_load_response(u32 *buf)
104 {
105         struct k3_sysctrler_load_msg *fw;
106
107         fw = (struct k3_sysctrler_load_msg *)buf;
108
109         /* Check for proper response ID */
110         if (fw->hdr.cmd_id != K3_MSG_M3_TO_R5_CERT_RESULT) {
111                 dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
112                         __func__, K3_MSG_M3_TO_R5_CERT_RESULT, fw->hdr.cmd_id);
113                 return -EINVAL;
114         }
115
116         /* Check for certificate authentication result */
117         if (fw->hdr.flags == K3_FLAGS_MSG_CERT_AUTH_FAIL) {
118                 dev_err(dev, "%s: Firmware certificate authentication failed\n",
119                         __func__);
120                 return -EINVAL;
121         } else if (fw->hdr.flags != K3_FLAGS_MSG_CERT_AUTH_PASS) {
122                 dev_err(dev, "%s: Firmware Load response Invalid %d\n",
123                         __func__, fw->hdr.flags);
124                 return -EINVAL;
125         }
126
127         debug("%s: Firmware authentication passed\n", __func__);
128
129         return 0;
130 }
131
132 static int k3_sysctrler_boot_notification_response(u32 *buf)
133 {
134         struct k3_sysctrler_boot_notification_msg *boot;
135
136         boot = (struct k3_sysctrler_boot_notification_msg *)buf;
137
138         /* ToDo: Verify checksum */
139
140         /* Check for proper response ID */
141         if (boot->hdr.cmd_id != K3_MSG_M3_TO_R5_BOOT_NOTIFICATION) {
142                 dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
143                         __func__, K3_MSG_M3_TO_R5_BOOT_NOTIFICATION,
144                         boot->hdr.cmd_id);
145                 return -EINVAL;
146         }
147
148         debug("%s: Boot notification received\n", __func__);
149
150         return 0;
151 }
152
153 /**
154  * k3_sysctrler_load() - Loadup the K3 remote processor
155  * @dev:        corresponding K3 remote processor device
156  * @addr:       Address in memory where image binary is stored
157  * @size:       Size in bytes of the image binary
158  *
159  * Return: 0 if all goes good, else appropriate error message.
160  */
161 static int k3_sysctrler_load(struct udevice *dev, ulong addr, ulong size)
162 {
163         struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
164         struct k3_sysctrler_load_msg firmware;
165         struct k3_sec_proxy_msg msg;
166         int ret;
167
168         debug("%s: Loading binary from 0x%08lX, size 0x%08lX\n",
169               __func__, addr, size);
170
171         memset(&firmware, 0, sizeof(firmware));
172         memset(&msg, 0, sizeof(msg));
173
174         /* Setup the message */
175         k3_sysctrler_load_msg_setup(&firmware, priv, addr, size);
176         msg.len = sizeof(firmware);
177         msg.buf = (u32 *)&firmware;
178
179         /* Send the message */
180         ret = mbox_send(&priv->chan_tx, &msg);
181         if (ret) {
182                 dev_err(dev, "%s: Firmware Loading failed. ret = %d\n",
183                         __func__, ret);
184                 return ret;
185         }
186
187         /* Receive the response */
188         ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
189         if (ret) {
190                 dev_err(dev, "%s: Firmware Load response failed. ret = %d\n",
191                         __func__, ret);
192                 return ret;
193         }
194
195         /* Process the response */
196         ret = k3_sysctrler_load_response(msg.buf);
197         if (ret)
198                 return ret;
199
200         debug("%s: Firmware Loaded successfully on dev %s\n",
201               __func__, dev->name);
202
203         return 0;
204 }
205
206 /**
207  * k3_sysctrler_start() - Start the remote processor
208  *              Note that while technically the K3 system controller starts up
209  *              automatically after its firmware got loaded we still want to
210  *              utilize the rproc start operation for other startup-related
211  *              tasks.
212  * @dev:        device to operate upon
213  *
214  * Return: 0 if all went ok, else return appropriate error
215  */
216 static int k3_sysctrler_start(struct udevice *dev)
217 {
218         struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
219         struct k3_sec_proxy_msg msg;
220         int ret;
221
222         debug("%s(dev=%p)\n", __func__, dev);
223
224         /* Receive the boot notification. Note that it is sent only once. */
225         ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
226         if (ret) {
227                 dev_err(dev, "%s: Boot Notification response failed. ret = %d\n",
228                         __func__, ret);
229                 return ret;
230         }
231
232         /* Process the response */
233         ret = k3_sysctrler_boot_notification_response(msg.buf);
234         if (ret)
235                 return ret;
236
237         debug("%s: Boot notification received successfully on dev %s\n",
238               __func__, dev->name);
239
240         return 0;
241 }
242
243 static const struct dm_rproc_ops k3_sysctrler_ops = {
244         .load = k3_sysctrler_load,
245         .start = k3_sysctrler_start,
246 };
247
248 /**
249  * k3_of_to_priv() - generate private data from device tree
250  * @dev:        corresponding k3 remote processor device
251  * @priv:       pointer to driver specific private data
252  *
253  * Return: 0 if all goes good, else appropriate error message.
254  */
255 static int k3_of_to_priv(struct udevice *dev,
256                          struct k3_sysctrler_privdata *priv)
257 {
258         int ret;
259
260         ret = mbox_get_by_name(dev, "tx", &priv->chan_tx);
261         if (ret) {
262                 dev_err(dev, "%s: Acquiring Tx channel failed. ret = %d\n",
263                         __func__, ret);
264                 return ret;
265         }
266
267         ret = mbox_get_by_name(dev, "rx", &priv->chan_rx);
268         if (ret) {
269                 dev_err(dev, "%s: Acquiring Rx channel failed. ret = %d\n",
270                         __func__, ret);
271                 return ret;
272         }
273
274         return 0;
275 }
276
277 /**
278  * k3_sysctrler_probe() - Basic probe
279  * @dev:        corresponding k3 remote processor device
280  *
281  * Return: 0 if all goes good, else appropriate error message.
282  */
283 static int k3_sysctrler_probe(struct udevice *dev)
284 {
285         struct k3_sysctrler_privdata *priv;
286         int ret;
287
288         debug("%s(dev=%p)\n", __func__, dev);
289
290         priv = dev_get_priv(dev);
291
292         ret = k3_of_to_priv(dev, priv);
293         if (ret) {
294                 dev_err(dev, "%s: Probe failed with error %d\n", __func__, ret);
295                 return ret;
296         }
297
298         priv->desc = (void *)dev_get_driver_data(dev);
299         priv->seq_nr = 0;
300
301         return 0;
302 }
303
304 static const struct k3_sysctrler_desc k3_sysctrler_am654_desc = {
305         .host_id = 4,                           /* HOST_ID_R5_1 */
306         .max_rx_timeout_us = 800000,
307         .max_msg_size = 60,
308 };
309
310 static const struct udevice_id k3_sysctrler_ids[] = {
311         {
312                 .compatible = "ti,am654-system-controller",
313                 .data = (ulong)&k3_sysctrler_am654_desc,
314         },
315         {}
316 };
317
318 U_BOOT_DRIVER(k3_sysctrler) = {
319         .name = "k3_system_controller",
320         .of_match = k3_sysctrler_ids,
321         .id = UCLASS_REMOTEPROC,
322         .ops = &k3_sysctrler_ops,
323         .probe = k3_sysctrler_probe,
324         .priv_auto_alloc_size = sizeof(struct k3_sysctrler_privdata),
325 };