k3_system_controller.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. * @desc: SoC description for this instance
  73. * @seq_nr: Counter for number of messages sent.
  74. */
  75. struct k3_sysctrler_privdata {
  76. struct mbox_chan chan_tx;
  77. struct mbox_chan chan_rx;
  78. struct k3_sysctrler_desc *desc;
  79. u32 seq_nr;
  80. };
  81. static inline
  82. void k3_sysctrler_load_msg_setup(struct k3_sysctrler_load_msg *fw,
  83. struct k3_sysctrler_privdata *priv,
  84. ulong addr, ulong size)
  85. {
  86. fw->hdr.cmd_id = K3_MSG_R5_TO_M3_M3FW;
  87. fw->hdr.host_id = priv->desc->host_id;
  88. fw->hdr.seq_nr = priv->seq_nr++;
  89. fw->hdr.flags = 0x0;
  90. fw->buffer_address = addr;
  91. fw->buffer_size = size;
  92. }
  93. static int k3_sysctrler_load_response(struct udevice *dev, u32 *buf)
  94. {
  95. struct k3_sysctrler_load_msg *fw;
  96. fw = (struct k3_sysctrler_load_msg *)buf;
  97. /* Check for proper response ID */
  98. if (fw->hdr.cmd_id != K3_MSG_M3_TO_R5_CERT_RESULT) {
  99. dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
  100. __func__, K3_MSG_M3_TO_R5_CERT_RESULT, fw->hdr.cmd_id);
  101. return -EINVAL;
  102. }
  103. /* Check for certificate authentication result */
  104. if (fw->hdr.flags == K3_FLAGS_MSG_CERT_AUTH_FAIL) {
  105. dev_err(dev, "%s: Firmware certificate authentication failed\n",
  106. __func__);
  107. return -EINVAL;
  108. } else if (fw->hdr.flags != K3_FLAGS_MSG_CERT_AUTH_PASS) {
  109. dev_err(dev, "%s: Firmware Load response Invalid %d\n",
  110. __func__, fw->hdr.flags);
  111. return -EINVAL;
  112. }
  113. debug("%s: Firmware authentication passed\n", __func__);
  114. return 0;
  115. }
  116. static int k3_sysctrler_boot_notification_response(struct udevice *dev,
  117. u32 *buf)
  118. {
  119. struct k3_sysctrler_boot_notification_msg *boot;
  120. boot = (struct k3_sysctrler_boot_notification_msg *)buf;
  121. /* ToDo: Verify checksum */
  122. /* Check for proper response ID */
  123. if (boot->hdr.cmd_id != K3_MSG_M3_TO_R5_BOOT_NOTIFICATION) {
  124. dev_err(dev, "%s: Command expected 0x%x, but received 0x%x\n",
  125. __func__, K3_MSG_M3_TO_R5_BOOT_NOTIFICATION,
  126. boot->hdr.cmd_id);
  127. return -EINVAL;
  128. }
  129. debug("%s: Boot notification received\n", __func__);
  130. return 0;
  131. }
  132. /**
  133. * k3_sysctrler_load() - Loadup the K3 remote processor
  134. * @dev: corresponding K3 remote processor device
  135. * @addr: Address in memory where image binary is stored
  136. * @size: Size in bytes of the image binary
  137. *
  138. * Return: 0 if all goes good, else appropriate error message.
  139. */
  140. static int k3_sysctrler_load(struct udevice *dev, ulong addr, ulong size)
  141. {
  142. struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
  143. struct k3_sysctrler_load_msg firmware;
  144. struct k3_sec_proxy_msg msg;
  145. int ret;
  146. debug("%s: Loading binary from 0x%08lX, size 0x%08lX\n",
  147. __func__, addr, size);
  148. memset(&firmware, 0, sizeof(firmware));
  149. memset(&msg, 0, sizeof(msg));
  150. /* Setup the message */
  151. k3_sysctrler_load_msg_setup(&firmware, priv, addr, size);
  152. msg.len = sizeof(firmware);
  153. msg.buf = (u32 *)&firmware;
  154. /* Send the message */
  155. ret = mbox_send(&priv->chan_tx, &msg);
  156. if (ret) {
  157. dev_err(dev, "%s: Firmware Loading failed. ret = %d\n",
  158. __func__, ret);
  159. return ret;
  160. }
  161. /* Receive the response */
  162. ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
  163. if (ret) {
  164. dev_err(dev, "%s: Firmware Load response failed. ret = %d\n",
  165. __func__, ret);
  166. return ret;
  167. }
  168. /* Process the response */
  169. ret = k3_sysctrler_load_response(dev, msg.buf);
  170. if (ret)
  171. return ret;
  172. debug("%s: Firmware Loaded successfully on dev %s\n",
  173. __func__, dev->name);
  174. return 0;
  175. }
  176. /**
  177. * k3_sysctrler_start() - Start the remote processor
  178. * Note that while technically the K3 system controller starts up
  179. * automatically after its firmware got loaded we still want to
  180. * utilize the rproc start operation for other startup-related
  181. * tasks.
  182. * @dev: device to operate upon
  183. *
  184. * Return: 0 if all went ok, else return appropriate error
  185. */
  186. static int k3_sysctrler_start(struct udevice *dev)
  187. {
  188. struct k3_sysctrler_privdata *priv = dev_get_priv(dev);
  189. struct k3_sec_proxy_msg msg;
  190. int ret;
  191. debug("%s(dev=%p)\n", __func__, dev);
  192. /* Receive the boot notification. Note that it is sent only once. */
  193. ret = mbox_recv(&priv->chan_rx, &msg, priv->desc->max_rx_timeout_us);
  194. if (ret) {
  195. dev_err(dev, "%s: Boot Notification response failed. ret = %d\n",
  196. __func__, ret);
  197. return ret;
  198. }
  199. /* Process the response */
  200. ret = k3_sysctrler_boot_notification_response(dev, msg.buf);
  201. if (ret)
  202. return ret;
  203. debug("%s: Boot notification received successfully on dev %s\n",
  204. __func__, dev->name);
  205. return 0;
  206. }
  207. static const struct dm_rproc_ops k3_sysctrler_ops = {
  208. .load = k3_sysctrler_load,
  209. .start = k3_sysctrler_start,
  210. };
  211. /**
  212. * k3_of_to_priv() - generate private data from device tree
  213. * @dev: corresponding k3 remote processor device
  214. * @priv: pointer to driver specific private data
  215. *
  216. * Return: 0 if all goes good, else appropriate error message.
  217. */
  218. static int k3_of_to_priv(struct udevice *dev,
  219. struct k3_sysctrler_privdata *priv)
  220. {
  221. int ret;
  222. ret = mbox_get_by_name(dev, "tx", &priv->chan_tx);
  223. if (ret) {
  224. dev_err(dev, "%s: Acquiring Tx channel failed. ret = %d\n",
  225. __func__, ret);
  226. return ret;
  227. }
  228. ret = mbox_get_by_name(dev, "rx", &priv->chan_rx);
  229. if (ret) {
  230. dev_err(dev, "%s: Acquiring Rx channel failed. ret = %d\n",
  231. __func__, ret);
  232. return ret;
  233. }
  234. return 0;
  235. }
  236. /**
  237. * k3_sysctrler_probe() - Basic probe
  238. * @dev: corresponding k3 remote processor device
  239. *
  240. * Return: 0 if all goes good, else appropriate error message.
  241. */
  242. static int k3_sysctrler_probe(struct udevice *dev)
  243. {
  244. struct k3_sysctrler_privdata *priv;
  245. int ret;
  246. debug("%s(dev=%p)\n", __func__, dev);
  247. priv = dev_get_priv(dev);
  248. ret = k3_of_to_priv(dev, priv);
  249. if (ret) {
  250. dev_err(dev, "%s: Probe failed with error %d\n", __func__, ret);
  251. return ret;
  252. }
  253. priv->desc = (void *)dev_get_driver_data(dev);
  254. priv->seq_nr = 0;
  255. return 0;
  256. }
  257. static const struct k3_sysctrler_desc k3_sysctrler_am654_desc = {
  258. .host_id = 4, /* HOST_ID_R5_1 */
  259. .max_rx_timeout_us = 800000,
  260. .max_msg_size = 60,
  261. };
  262. static const struct udevice_id k3_sysctrler_ids[] = {
  263. {
  264. .compatible = "ti,am654-system-controller",
  265. .data = (ulong)&k3_sysctrler_am654_desc,
  266. },
  267. {}
  268. };
  269. U_BOOT_DRIVER(k3_sysctrler) = {
  270. .name = "k3_system_controller",
  271. .of_match = k3_sysctrler_ids,
  272. .id = UCLASS_REMOTEPROC,
  273. .ops = &k3_sysctrler_ops,
  274. .probe = k3_sysctrler_probe,
  275. .priv_auto = sizeof(struct k3_sysctrler_privdata),
  276. };