k3_system_controller.c 8.1 KB

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