k3_system_controller.c 8.9 KB

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