wkup_m3_ipc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * AMx3 Wkup M3 IPC driver
  3. *
  4. * Copyright (C) 2015 Texas Instruments, Inc.
  5. *
  6. * Dave Gerlach <d-gerlach@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/err.h>
  18. #include <linux/kernel.h>
  19. #include <linux/kthread.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/omap-mailbox.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/remoteproc.h>
  27. #include <linux/suspend.h>
  28. #include <linux/wkup_m3_ipc.h>
  29. #define AM33XX_CTRL_IPC_REG_COUNT 0x8
  30. #define AM33XX_CTRL_IPC_REG_OFFSET(m) (0x4 + 4 * (m))
  31. /* AM33XX M3_TXEV_EOI register */
  32. #define AM33XX_CONTROL_M3_TXEV_EOI 0x00
  33. #define AM33XX_M3_TXEV_ACK (0x1 << 0)
  34. #define AM33XX_M3_TXEV_ENABLE (0x0 << 0)
  35. #define IPC_CMD_DS0 0x4
  36. #define IPC_CMD_STANDBY 0xc
  37. #define IPC_CMD_IDLE 0x10
  38. #define IPC_CMD_RESET 0xe
  39. #define DS_IPC_DEFAULT 0xffffffff
  40. #define M3_VERSION_UNKNOWN 0x0000ffff
  41. #define M3_BASELINE_VERSION 0x191
  42. #define M3_STATUS_RESP_MASK (0xffff << 16)
  43. #define M3_FW_VERSION_MASK 0xffff
  44. #define M3_STATE_UNKNOWN 0
  45. #define M3_STATE_RESET 1
  46. #define M3_STATE_INITED 2
  47. #define M3_STATE_MSG_FOR_LP 3
  48. #define M3_STATE_MSG_FOR_RESET 4
  49. static struct wkup_m3_ipc *m3_ipc_state;
  50. static void am33xx_txev_eoi(struct wkup_m3_ipc *m3_ipc)
  51. {
  52. writel(AM33XX_M3_TXEV_ACK,
  53. m3_ipc->ipc_mem_base + AM33XX_CONTROL_M3_TXEV_EOI);
  54. }
  55. static void am33xx_txev_enable(struct wkup_m3_ipc *m3_ipc)
  56. {
  57. writel(AM33XX_M3_TXEV_ENABLE,
  58. m3_ipc->ipc_mem_base + AM33XX_CONTROL_M3_TXEV_EOI);
  59. }
  60. static void wkup_m3_ctrl_ipc_write(struct wkup_m3_ipc *m3_ipc,
  61. u32 val, int ipc_reg_num)
  62. {
  63. if (WARN(ipc_reg_num < 0 || ipc_reg_num > AM33XX_CTRL_IPC_REG_COUNT,
  64. "ipc register operation out of range"))
  65. return;
  66. writel(val, m3_ipc->ipc_mem_base +
  67. AM33XX_CTRL_IPC_REG_OFFSET(ipc_reg_num));
  68. }
  69. static unsigned int wkup_m3_ctrl_ipc_read(struct wkup_m3_ipc *m3_ipc,
  70. int ipc_reg_num)
  71. {
  72. if (WARN(ipc_reg_num < 0 || ipc_reg_num > AM33XX_CTRL_IPC_REG_COUNT,
  73. "ipc register operation out of range"))
  74. return 0;
  75. return readl(m3_ipc->ipc_mem_base +
  76. AM33XX_CTRL_IPC_REG_OFFSET(ipc_reg_num));
  77. }
  78. static int wkup_m3_fw_version_read(struct wkup_m3_ipc *m3_ipc)
  79. {
  80. int val;
  81. val = wkup_m3_ctrl_ipc_read(m3_ipc, 2);
  82. return val & M3_FW_VERSION_MASK;
  83. }
  84. static irqreturn_t wkup_m3_txev_handler(int irq, void *ipc_data)
  85. {
  86. struct wkup_m3_ipc *m3_ipc = ipc_data;
  87. struct device *dev = m3_ipc->dev;
  88. int ver = 0;
  89. am33xx_txev_eoi(m3_ipc);
  90. switch (m3_ipc->state) {
  91. case M3_STATE_RESET:
  92. ver = wkup_m3_fw_version_read(m3_ipc);
  93. if (ver == M3_VERSION_UNKNOWN ||
  94. ver < M3_BASELINE_VERSION) {
  95. dev_warn(dev, "CM3 Firmware Version %x not supported\n",
  96. ver);
  97. } else {
  98. dev_info(dev, "CM3 Firmware Version = 0x%x\n", ver);
  99. }
  100. m3_ipc->state = M3_STATE_INITED;
  101. complete(&m3_ipc->sync_complete);
  102. break;
  103. case M3_STATE_MSG_FOR_RESET:
  104. m3_ipc->state = M3_STATE_INITED;
  105. complete(&m3_ipc->sync_complete);
  106. break;
  107. case M3_STATE_MSG_FOR_LP:
  108. complete(&m3_ipc->sync_complete);
  109. break;
  110. case M3_STATE_UNKNOWN:
  111. dev_warn(dev, "Unknown CM3 State\n");
  112. }
  113. am33xx_txev_enable(m3_ipc);
  114. return IRQ_HANDLED;
  115. }
  116. static int wkup_m3_ping(struct wkup_m3_ipc *m3_ipc)
  117. {
  118. struct device *dev = m3_ipc->dev;
  119. mbox_msg_t dummy_msg = 0;
  120. int ret;
  121. if (!m3_ipc->mbox) {
  122. dev_err(dev,
  123. "No IPC channel to communicate with wkup_m3!\n");
  124. return -EIO;
  125. }
  126. /*
  127. * Write a dummy message to the mailbox in order to trigger the RX
  128. * interrupt to alert the M3 that data is available in the IPC
  129. * registers. We must enable the IRQ here and disable it after in
  130. * the RX callback to avoid multiple interrupts being received
  131. * by the CM3.
  132. */
  133. ret = mbox_send_message(m3_ipc->mbox, &dummy_msg);
  134. if (ret < 0) {
  135. dev_err(dev, "%s: mbox_send_message() failed: %d\n",
  136. __func__, ret);
  137. return ret;
  138. }
  139. ret = wait_for_completion_timeout(&m3_ipc->sync_complete,
  140. msecs_to_jiffies(500));
  141. if (!ret) {
  142. dev_err(dev, "MPU<->CM3 sync failure\n");
  143. m3_ipc->state = M3_STATE_UNKNOWN;
  144. return -EIO;
  145. }
  146. mbox_client_txdone(m3_ipc->mbox, 0);
  147. return 0;
  148. }
  149. static int wkup_m3_ping_noirq(struct wkup_m3_ipc *m3_ipc)
  150. {
  151. struct device *dev = m3_ipc->dev;
  152. mbox_msg_t dummy_msg = 0;
  153. int ret;
  154. if (!m3_ipc->mbox) {
  155. dev_err(dev,
  156. "No IPC channel to communicate with wkup_m3!\n");
  157. return -EIO;
  158. }
  159. ret = mbox_send_message(m3_ipc->mbox, &dummy_msg);
  160. if (ret < 0) {
  161. dev_err(dev, "%s: mbox_send_message() failed: %d\n",
  162. __func__, ret);
  163. return ret;
  164. }
  165. mbox_client_txdone(m3_ipc->mbox, 0);
  166. return 0;
  167. }
  168. static int wkup_m3_is_available(struct wkup_m3_ipc *m3_ipc)
  169. {
  170. return ((m3_ipc->state != M3_STATE_RESET) &&
  171. (m3_ipc->state != M3_STATE_UNKNOWN));
  172. }
  173. /* Public functions */
  174. /**
  175. * wkup_m3_set_mem_type - Pass wkup_m3 which type of memory is in use
  176. * @mem_type: memory type value read directly from emif
  177. *
  178. * wkup_m3 must know what memory type is in use to properly suspend
  179. * and resume.
  180. */
  181. static void wkup_m3_set_mem_type(struct wkup_m3_ipc *m3_ipc, int mem_type)
  182. {
  183. m3_ipc->mem_type = mem_type;
  184. }
  185. /**
  186. * wkup_m3_set_resume_address - Pass wkup_m3 resume address
  187. * @addr: Physical address from which resume code should execute
  188. */
  189. static void wkup_m3_set_resume_address(struct wkup_m3_ipc *m3_ipc, void *addr)
  190. {
  191. m3_ipc->resume_addr = (unsigned long)addr;
  192. }
  193. /**
  194. * wkup_m3_request_pm_status - Retrieve wkup_m3 status code after suspend
  195. *
  196. * Returns code representing the status of a low power mode transition.
  197. * 0 - Successful transition
  198. * 1 - Failure to transition to low power state
  199. */
  200. static int wkup_m3_request_pm_status(struct wkup_m3_ipc *m3_ipc)
  201. {
  202. unsigned int i;
  203. int val;
  204. val = wkup_m3_ctrl_ipc_read(m3_ipc, 1);
  205. i = M3_STATUS_RESP_MASK & val;
  206. i >>= __ffs(M3_STATUS_RESP_MASK);
  207. return i;
  208. }
  209. /**
  210. * wkup_m3_prepare_low_power - Request preparation for transition to
  211. * low power state
  212. * @state: A kernel suspend state to enter, either MEM or STANDBY
  213. *
  214. * Returns 0 if preparation was successful, otherwise returns error code
  215. */
  216. static int wkup_m3_prepare_low_power(struct wkup_m3_ipc *m3_ipc, int state)
  217. {
  218. struct device *dev = m3_ipc->dev;
  219. int m3_power_state;
  220. int ret = 0;
  221. if (!wkup_m3_is_available(m3_ipc))
  222. return -ENODEV;
  223. switch (state) {
  224. case WKUP_M3_DEEPSLEEP:
  225. m3_power_state = IPC_CMD_DS0;
  226. break;
  227. case WKUP_M3_STANDBY:
  228. m3_power_state = IPC_CMD_STANDBY;
  229. break;
  230. case WKUP_M3_IDLE:
  231. m3_power_state = IPC_CMD_IDLE;
  232. break;
  233. default:
  234. return 1;
  235. }
  236. /* Program each required IPC register then write defaults to others */
  237. wkup_m3_ctrl_ipc_write(m3_ipc, m3_ipc->resume_addr, 0);
  238. wkup_m3_ctrl_ipc_write(m3_ipc, m3_power_state, 1);
  239. wkup_m3_ctrl_ipc_write(m3_ipc, m3_ipc->mem_type, 4);
  240. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 2);
  241. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 3);
  242. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 5);
  243. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 6);
  244. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 7);
  245. m3_ipc->state = M3_STATE_MSG_FOR_LP;
  246. if (state == WKUP_M3_IDLE)
  247. ret = wkup_m3_ping_noirq(m3_ipc);
  248. else
  249. ret = wkup_m3_ping(m3_ipc);
  250. if (ret) {
  251. dev_err(dev, "Unable to ping CM3\n");
  252. return ret;
  253. }
  254. return 0;
  255. }
  256. /**
  257. * wkup_m3_finish_low_power - Return m3 to reset state
  258. *
  259. * Returns 0 if reset was successful, otherwise returns error code
  260. */
  261. static int wkup_m3_finish_low_power(struct wkup_m3_ipc *m3_ipc)
  262. {
  263. struct device *dev = m3_ipc->dev;
  264. int ret = 0;
  265. if (!wkup_m3_is_available(m3_ipc))
  266. return -ENODEV;
  267. wkup_m3_ctrl_ipc_write(m3_ipc, IPC_CMD_RESET, 1);
  268. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 2);
  269. m3_ipc->state = M3_STATE_MSG_FOR_RESET;
  270. ret = wkup_m3_ping(m3_ipc);
  271. if (ret) {
  272. dev_err(dev, "Unable to ping CM3\n");
  273. return ret;
  274. }
  275. return 0;
  276. }
  277. static struct wkup_m3_ipc_ops ipc_ops = {
  278. .set_mem_type = wkup_m3_set_mem_type,
  279. .set_resume_address = wkup_m3_set_resume_address,
  280. .prepare_low_power = wkup_m3_prepare_low_power,
  281. .finish_low_power = wkup_m3_finish_low_power,
  282. .request_pm_status = wkup_m3_request_pm_status,
  283. };
  284. /**
  285. * wkup_m3_ipc_get - Return handle to wkup_m3_ipc
  286. *
  287. * Returns NULL if the wkup_m3 is not yet available, otherwise returns
  288. * pointer to wkup_m3_ipc struct.
  289. */
  290. struct wkup_m3_ipc *wkup_m3_ipc_get(void)
  291. {
  292. if (m3_ipc_state)
  293. get_device(m3_ipc_state->dev);
  294. else
  295. return NULL;
  296. return m3_ipc_state;
  297. }
  298. EXPORT_SYMBOL_GPL(wkup_m3_ipc_get);
  299. /**
  300. * wkup_m3_ipc_put - Free handle to wkup_m3_ipc returned from wkup_m3_ipc_get
  301. * @m3_ipc: A pointer to wkup_m3_ipc struct returned by wkup_m3_ipc_get
  302. */
  303. void wkup_m3_ipc_put(struct wkup_m3_ipc *m3_ipc)
  304. {
  305. if (m3_ipc_state)
  306. put_device(m3_ipc_state->dev);
  307. }
  308. EXPORT_SYMBOL_GPL(wkup_m3_ipc_put);
  309. static void wkup_m3_rproc_boot_thread(struct wkup_m3_ipc *m3_ipc)
  310. {
  311. struct device *dev = m3_ipc->dev;
  312. int ret;
  313. wait_for_completion(&m3_ipc->rproc->firmware_loading_complete);
  314. init_completion(&m3_ipc->sync_complete);
  315. ret = rproc_boot(m3_ipc->rproc);
  316. if (ret)
  317. dev_err(dev, "rproc_boot failed\n");
  318. do_exit(0);
  319. }
  320. static int wkup_m3_ipc_probe(struct platform_device *pdev)
  321. {
  322. struct device *dev = &pdev->dev;
  323. int irq, ret;
  324. phandle rproc_phandle;
  325. struct rproc *m3_rproc;
  326. struct resource *res;
  327. struct task_struct *task;
  328. struct wkup_m3_ipc *m3_ipc;
  329. m3_ipc = devm_kzalloc(dev, sizeof(*m3_ipc), GFP_KERNEL);
  330. if (!m3_ipc)
  331. return -ENOMEM;
  332. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  333. m3_ipc->ipc_mem_base = devm_ioremap_resource(dev, res);
  334. if (IS_ERR(m3_ipc->ipc_mem_base)) {
  335. dev_err(dev, "could not ioremap ipc_mem\n");
  336. return PTR_ERR(m3_ipc->ipc_mem_base);
  337. }
  338. irq = platform_get_irq(pdev, 0);
  339. if (!irq) {
  340. dev_err(&pdev->dev, "no irq resource\n");
  341. return -ENXIO;
  342. }
  343. ret = devm_request_irq(dev, irq, wkup_m3_txev_handler,
  344. 0, "wkup_m3_txev", m3_ipc);
  345. if (ret) {
  346. dev_err(dev, "request_irq failed\n");
  347. return ret;
  348. }
  349. m3_ipc->mbox_client.dev = dev;
  350. m3_ipc->mbox_client.tx_done = NULL;
  351. m3_ipc->mbox_client.tx_prepare = NULL;
  352. m3_ipc->mbox_client.rx_callback = NULL;
  353. m3_ipc->mbox_client.tx_block = false;
  354. m3_ipc->mbox_client.knows_txdone = false;
  355. m3_ipc->mbox = mbox_request_channel(&m3_ipc->mbox_client, 0);
  356. if (IS_ERR(m3_ipc->mbox)) {
  357. dev_err(dev, "IPC Request for A8->M3 Channel failed! %ld\n",
  358. PTR_ERR(m3_ipc->mbox));
  359. return PTR_ERR(m3_ipc->mbox);
  360. }
  361. if (of_property_read_u32(dev->of_node, "ti,rproc", &rproc_phandle)) {
  362. dev_err(&pdev->dev, "could not get rproc phandle\n");
  363. ret = -ENODEV;
  364. goto err_free_mbox;
  365. }
  366. m3_rproc = rproc_get_by_phandle(rproc_phandle);
  367. if (!m3_rproc) {
  368. dev_err(&pdev->dev, "could not get rproc handle\n");
  369. ret = -EPROBE_DEFER;
  370. goto err_free_mbox;
  371. }
  372. m3_ipc->rproc = m3_rproc;
  373. m3_ipc->dev = dev;
  374. m3_ipc->state = M3_STATE_RESET;
  375. m3_ipc->ops = &ipc_ops;
  376. /*
  377. * Wait for firmware loading completion in a thread so we
  378. * can boot the wkup_m3 as soon as it's ready without holding
  379. * up kernel boot
  380. */
  381. task = kthread_run((void *)wkup_m3_rproc_boot_thread, m3_ipc,
  382. "wkup_m3_rproc_loader");
  383. if (IS_ERR(task)) {
  384. dev_err(dev, "can't create rproc_boot thread\n");
  385. ret = PTR_ERR(task);
  386. goto err_put_rproc;
  387. }
  388. m3_ipc_state = m3_ipc;
  389. return 0;
  390. err_put_rproc:
  391. rproc_put(m3_rproc);
  392. err_free_mbox:
  393. mbox_free_channel(m3_ipc->mbox);
  394. return ret;
  395. }
  396. static int wkup_m3_ipc_remove(struct platform_device *pdev)
  397. {
  398. mbox_free_channel(m3_ipc_state->mbox);
  399. rproc_shutdown(m3_ipc_state->rproc);
  400. rproc_put(m3_ipc_state->rproc);
  401. m3_ipc_state = NULL;
  402. return 0;
  403. }
  404. static const struct of_device_id wkup_m3_ipc_of_match[] = {
  405. { .compatible = "ti,am3352-wkup-m3-ipc", },
  406. { .compatible = "ti,am4372-wkup-m3-ipc", },
  407. {},
  408. };
  409. MODULE_DEVICE_TABLE(of, wkup_m3_ipc_of_match);
  410. static struct platform_driver wkup_m3_ipc_driver = {
  411. .probe = wkup_m3_ipc_probe,
  412. .remove = wkup_m3_ipc_remove,
  413. .driver = {
  414. .name = "wkup_m3_ipc",
  415. .of_match_table = wkup_m3_ipc_of_match,
  416. },
  417. };
  418. module_platform_driver(wkup_m3_ipc_driver);
  419. MODULE_LICENSE("GPL v2");
  420. MODULE_DESCRIPTION("wkup m3 remote processor ipc driver");
  421. MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");