k3_system_controller.c 8.2 KB

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