wkup_m3_ipc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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_WAKE_SRC_MASK 0xff
  45. #define M3_STATE_UNKNOWN 0
  46. #define M3_STATE_RESET 1
  47. #define M3_STATE_INITED 2
  48. #define M3_STATE_MSG_FOR_LP 3
  49. #define M3_STATE_MSG_FOR_RESET 4
  50. static struct wkup_m3_ipc *m3_ipc_state;
  51. static const struct wkup_m3_wakeup_src wakeups[] = {
  52. {.irq_nr = 35, .src = "USB0_PHY"},
  53. {.irq_nr = 36, .src = "USB1_PHY"},
  54. {.irq_nr = 40, .src = "I2C0"},
  55. {.irq_nr = 41, .src = "RTC Timer"},
  56. {.irq_nr = 42, .src = "RTC Alarm"},
  57. {.irq_nr = 43, .src = "Timer0"},
  58. {.irq_nr = 44, .src = "Timer1"},
  59. {.irq_nr = 45, .src = "UART"},
  60. {.irq_nr = 46, .src = "GPIO0"},
  61. {.irq_nr = 48, .src = "MPU_WAKE"},
  62. {.irq_nr = 49, .src = "WDT0"},
  63. {.irq_nr = 50, .src = "WDT1"},
  64. {.irq_nr = 51, .src = "ADC_TSC"},
  65. {.irq_nr = 0, .src = "Unknown"},
  66. };
  67. static void am33xx_txev_eoi(struct wkup_m3_ipc *m3_ipc)
  68. {
  69. writel(AM33XX_M3_TXEV_ACK,
  70. m3_ipc->ipc_mem_base + AM33XX_CONTROL_M3_TXEV_EOI);
  71. }
  72. static void am33xx_txev_enable(struct wkup_m3_ipc *m3_ipc)
  73. {
  74. writel(AM33XX_M3_TXEV_ENABLE,
  75. m3_ipc->ipc_mem_base + AM33XX_CONTROL_M3_TXEV_EOI);
  76. }
  77. static void wkup_m3_ctrl_ipc_write(struct wkup_m3_ipc *m3_ipc,
  78. u32 val, int ipc_reg_num)
  79. {
  80. if (WARN(ipc_reg_num < 0 || ipc_reg_num > AM33XX_CTRL_IPC_REG_COUNT,
  81. "ipc register operation out of range"))
  82. return;
  83. writel(val, m3_ipc->ipc_mem_base +
  84. AM33XX_CTRL_IPC_REG_OFFSET(ipc_reg_num));
  85. }
  86. static unsigned int wkup_m3_ctrl_ipc_read(struct wkup_m3_ipc *m3_ipc,
  87. int ipc_reg_num)
  88. {
  89. if (WARN(ipc_reg_num < 0 || ipc_reg_num > AM33XX_CTRL_IPC_REG_COUNT,
  90. "ipc register operation out of range"))
  91. return 0;
  92. return readl(m3_ipc->ipc_mem_base +
  93. AM33XX_CTRL_IPC_REG_OFFSET(ipc_reg_num));
  94. }
  95. static int wkup_m3_fw_version_read(struct wkup_m3_ipc *m3_ipc)
  96. {
  97. int val;
  98. val = wkup_m3_ctrl_ipc_read(m3_ipc, 2);
  99. return val & M3_FW_VERSION_MASK;
  100. }
  101. static irqreturn_t wkup_m3_txev_handler(int irq, void *ipc_data)
  102. {
  103. struct wkup_m3_ipc *m3_ipc = ipc_data;
  104. struct device *dev = m3_ipc->dev;
  105. int ver = 0;
  106. am33xx_txev_eoi(m3_ipc);
  107. switch (m3_ipc->state) {
  108. case M3_STATE_RESET:
  109. ver = wkup_m3_fw_version_read(m3_ipc);
  110. if (ver == M3_VERSION_UNKNOWN ||
  111. ver < M3_BASELINE_VERSION) {
  112. dev_warn(dev, "CM3 Firmware Version %x not supported\n",
  113. ver);
  114. } else {
  115. dev_info(dev, "CM3 Firmware Version = 0x%x\n", ver);
  116. }
  117. m3_ipc->state = M3_STATE_INITED;
  118. complete(&m3_ipc->sync_complete);
  119. break;
  120. case M3_STATE_MSG_FOR_RESET:
  121. m3_ipc->state = M3_STATE_INITED;
  122. complete(&m3_ipc->sync_complete);
  123. break;
  124. case M3_STATE_MSG_FOR_LP:
  125. complete(&m3_ipc->sync_complete);
  126. break;
  127. case M3_STATE_UNKNOWN:
  128. dev_warn(dev, "Unknown CM3 State\n");
  129. }
  130. am33xx_txev_enable(m3_ipc);
  131. return IRQ_HANDLED;
  132. }
  133. static int wkup_m3_ping(struct wkup_m3_ipc *m3_ipc)
  134. {
  135. struct device *dev = m3_ipc->dev;
  136. mbox_msg_t dummy_msg = 0;
  137. int ret;
  138. if (!m3_ipc->mbox) {
  139. dev_err(dev,
  140. "No IPC channel to communicate with wkup_m3!\n");
  141. return -EIO;
  142. }
  143. /*
  144. * Write a dummy message to the mailbox in order to trigger the RX
  145. * interrupt to alert the M3 that data is available in the IPC
  146. * registers. We must enable the IRQ here and disable it after in
  147. * the RX callback to avoid multiple interrupts being received
  148. * by the CM3.
  149. */
  150. ret = mbox_send_message(m3_ipc->mbox, &dummy_msg);
  151. if (ret < 0) {
  152. dev_err(dev, "%s: mbox_send_message() failed: %d\n",
  153. __func__, ret);
  154. return ret;
  155. }
  156. ret = wait_for_completion_timeout(&m3_ipc->sync_complete,
  157. msecs_to_jiffies(500));
  158. if (!ret) {
  159. dev_err(dev, "MPU<->CM3 sync failure\n");
  160. m3_ipc->state = M3_STATE_UNKNOWN;
  161. return -EIO;
  162. }
  163. mbox_client_txdone(m3_ipc->mbox, 0);
  164. return 0;
  165. }
  166. static int wkup_m3_ping_noirq(struct wkup_m3_ipc *m3_ipc)
  167. {
  168. struct device *dev = m3_ipc->dev;
  169. mbox_msg_t dummy_msg = 0;
  170. int ret;
  171. if (!m3_ipc->mbox) {
  172. dev_err(dev,
  173. "No IPC channel to communicate with wkup_m3!\n");
  174. return -EIO;
  175. }
  176. ret = mbox_send_message(m3_ipc->mbox, &dummy_msg);
  177. if (ret < 0) {
  178. dev_err(dev, "%s: mbox_send_message() failed: %d\n",
  179. __func__, ret);
  180. return ret;
  181. }
  182. mbox_client_txdone(m3_ipc->mbox, 0);
  183. return 0;
  184. }
  185. static int wkup_m3_is_available(struct wkup_m3_ipc *m3_ipc)
  186. {
  187. return ((m3_ipc->state != M3_STATE_RESET) &&
  188. (m3_ipc->state != M3_STATE_UNKNOWN));
  189. }
  190. /* Public functions */
  191. /**
  192. * wkup_m3_set_mem_type - Pass wkup_m3 which type of memory is in use
  193. * @mem_type: memory type value read directly from emif
  194. *
  195. * wkup_m3 must know what memory type is in use to properly suspend
  196. * and resume.
  197. */
  198. static void wkup_m3_set_mem_type(struct wkup_m3_ipc *m3_ipc, int mem_type)
  199. {
  200. m3_ipc->mem_type = mem_type;
  201. }
  202. /**
  203. * wkup_m3_set_resume_address - Pass wkup_m3 resume address
  204. * @addr: Physical address from which resume code should execute
  205. */
  206. static void wkup_m3_set_resume_address(struct wkup_m3_ipc *m3_ipc, void *addr)
  207. {
  208. m3_ipc->resume_addr = (unsigned long)addr;
  209. }
  210. /**
  211. * wkup_m3_request_pm_status - Retrieve wkup_m3 status code after suspend
  212. *
  213. * Returns code representing the status of a low power mode transition.
  214. * 0 - Successful transition
  215. * 1 - Failure to transition to low power state
  216. */
  217. static int wkup_m3_request_pm_status(struct wkup_m3_ipc *m3_ipc)
  218. {
  219. unsigned int i;
  220. int val;
  221. val = wkup_m3_ctrl_ipc_read(m3_ipc, 1);
  222. i = M3_STATUS_RESP_MASK & val;
  223. i >>= __ffs(M3_STATUS_RESP_MASK);
  224. return i;
  225. }
  226. /**
  227. * wkup_m3_prepare_low_power - Request preparation for transition to
  228. * low power state
  229. * @state: A kernel suspend state to enter, either MEM or STANDBY
  230. *
  231. * Returns 0 if preparation was successful, otherwise returns error code
  232. */
  233. static int wkup_m3_prepare_low_power(struct wkup_m3_ipc *m3_ipc, int state)
  234. {
  235. struct device *dev = m3_ipc->dev;
  236. int m3_power_state;
  237. int ret = 0;
  238. if (!wkup_m3_is_available(m3_ipc))
  239. return -ENODEV;
  240. switch (state) {
  241. case WKUP_M3_DEEPSLEEP:
  242. m3_power_state = IPC_CMD_DS0;
  243. break;
  244. case WKUP_M3_STANDBY:
  245. m3_power_state = IPC_CMD_STANDBY;
  246. break;
  247. case WKUP_M3_IDLE:
  248. m3_power_state = IPC_CMD_IDLE;
  249. break;
  250. default:
  251. return 1;
  252. }
  253. /* Program each required IPC register then write defaults to others */
  254. wkup_m3_ctrl_ipc_write(m3_ipc, m3_ipc->resume_addr, 0);
  255. wkup_m3_ctrl_ipc_write(m3_ipc, m3_power_state, 1);
  256. wkup_m3_ctrl_ipc_write(m3_ipc, m3_ipc->mem_type, 4);
  257. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 2);
  258. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 3);
  259. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 5);
  260. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 6);
  261. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 7);
  262. m3_ipc->state = M3_STATE_MSG_FOR_LP;
  263. if (state == WKUP_M3_IDLE)
  264. ret = wkup_m3_ping_noirq(m3_ipc);
  265. else
  266. ret = wkup_m3_ping(m3_ipc);
  267. if (ret) {
  268. dev_err(dev, "Unable to ping CM3\n");
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. /**
  274. * wkup_m3_finish_low_power - Return m3 to reset state
  275. *
  276. * Returns 0 if reset was successful, otherwise returns error code
  277. */
  278. static int wkup_m3_finish_low_power(struct wkup_m3_ipc *m3_ipc)
  279. {
  280. struct device *dev = m3_ipc->dev;
  281. int ret = 0;
  282. if (!wkup_m3_is_available(m3_ipc))
  283. return -ENODEV;
  284. wkup_m3_ctrl_ipc_write(m3_ipc, IPC_CMD_RESET, 1);
  285. wkup_m3_ctrl_ipc_write(m3_ipc, DS_IPC_DEFAULT, 2);
  286. m3_ipc->state = M3_STATE_MSG_FOR_RESET;
  287. ret = wkup_m3_ping(m3_ipc);
  288. if (ret) {
  289. dev_err(dev, "Unable to ping CM3\n");
  290. return ret;
  291. }
  292. return 0;
  293. }
  294. /**
  295. * wkup_m3_request_wake_src - Get the wakeup source info passed from wkup_m3
  296. * @m3_ipc: Pointer to wkup_m3_ipc context
  297. */
  298. static const char *wkup_m3_request_wake_src(struct wkup_m3_ipc *m3_ipc)
  299. {
  300. unsigned int wakeup_src_idx;
  301. int j, val;
  302. val = wkup_m3_ctrl_ipc_read(m3_ipc, 6);
  303. wakeup_src_idx = val & M3_WAKE_SRC_MASK;
  304. for (j = 0; j < ARRAY_SIZE(wakeups) - 1; j++) {
  305. if (wakeups[j].irq_nr == wakeup_src_idx)
  306. return wakeups[j].src;
  307. }
  308. return wakeups[j].src;
  309. }
  310. /**
  311. * wkup_m3_set_rtc_only - Set the rtc_only flag
  312. * @wkup_m3_wakeup: struct wkup_m3_wakeup_src * gets assigned the
  313. * wakeup src value
  314. */
  315. static void wkup_m3_set_rtc_only(struct wkup_m3_ipc *m3_ipc)
  316. {
  317. if (m3_ipc_state)
  318. m3_ipc_state->is_rtc_only = true;
  319. }
  320. static struct wkup_m3_ipc_ops ipc_ops = {
  321. .set_mem_type = wkup_m3_set_mem_type,
  322. .set_resume_address = wkup_m3_set_resume_address,
  323. .prepare_low_power = wkup_m3_prepare_low_power,
  324. .finish_low_power = wkup_m3_finish_low_power,
  325. .request_pm_status = wkup_m3_request_pm_status,
  326. .request_wake_src = wkup_m3_request_wake_src,
  327. .set_rtc_only = wkup_m3_set_rtc_only,
  328. };
  329. /**
  330. * wkup_m3_ipc_get - Return handle to wkup_m3_ipc
  331. *
  332. * Returns NULL if the wkup_m3 is not yet available, otherwise returns
  333. * pointer to wkup_m3_ipc struct.
  334. */
  335. struct wkup_m3_ipc *wkup_m3_ipc_get(void)
  336. {
  337. if (m3_ipc_state)
  338. get_device(m3_ipc_state->dev);
  339. else
  340. return NULL;
  341. return m3_ipc_state;
  342. }
  343. EXPORT_SYMBOL_GPL(wkup_m3_ipc_get);
  344. /**
  345. * wkup_m3_ipc_put - Free handle to wkup_m3_ipc returned from wkup_m3_ipc_get
  346. * @m3_ipc: A pointer to wkup_m3_ipc struct returned by wkup_m3_ipc_get
  347. */
  348. void wkup_m3_ipc_put(struct wkup_m3_ipc *m3_ipc)
  349. {
  350. if (m3_ipc_state)
  351. put_device(m3_ipc_state->dev);
  352. }
  353. EXPORT_SYMBOL_GPL(wkup_m3_ipc_put);
  354. static void wkup_m3_rproc_boot_thread(struct wkup_m3_ipc *m3_ipc)
  355. {
  356. struct device *dev = m3_ipc->dev;
  357. int ret;
  358. init_completion(&m3_ipc->sync_complete);
  359. ret = rproc_boot(m3_ipc->rproc);
  360. if (ret)
  361. dev_err(dev, "rproc_boot failed\n");
  362. else
  363. m3_ipc_state = m3_ipc;
  364. do_exit(0);
  365. }
  366. static int wkup_m3_ipc_probe(struct platform_device *pdev)
  367. {
  368. struct device *dev = &pdev->dev;
  369. int irq, ret;
  370. phandle rproc_phandle;
  371. struct rproc *m3_rproc;
  372. struct resource *res;
  373. struct task_struct *task;
  374. struct wkup_m3_ipc *m3_ipc;
  375. m3_ipc = devm_kzalloc(dev, sizeof(*m3_ipc), GFP_KERNEL);
  376. if (!m3_ipc)
  377. return -ENOMEM;
  378. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  379. m3_ipc->ipc_mem_base = devm_ioremap_resource(dev, res);
  380. if (IS_ERR(m3_ipc->ipc_mem_base)) {
  381. dev_err(dev, "could not ioremap ipc_mem\n");
  382. return PTR_ERR(m3_ipc->ipc_mem_base);
  383. }
  384. irq = platform_get_irq(pdev, 0);
  385. if (!irq) {
  386. dev_err(&pdev->dev, "no irq resource\n");
  387. return -ENXIO;
  388. }
  389. ret = devm_request_irq(dev, irq, wkup_m3_txev_handler,
  390. 0, "wkup_m3_txev", m3_ipc);
  391. if (ret) {
  392. dev_err(dev, "request_irq failed\n");
  393. return ret;
  394. }
  395. m3_ipc->mbox_client.dev = dev;
  396. m3_ipc->mbox_client.tx_done = NULL;
  397. m3_ipc->mbox_client.tx_prepare = NULL;
  398. m3_ipc->mbox_client.rx_callback = NULL;
  399. m3_ipc->mbox_client.tx_block = false;
  400. m3_ipc->mbox_client.knows_txdone = false;
  401. m3_ipc->mbox = mbox_request_channel(&m3_ipc->mbox_client, 0);
  402. if (IS_ERR(m3_ipc->mbox)) {
  403. dev_err(dev, "IPC Request for A8->M3 Channel failed! %ld\n",
  404. PTR_ERR(m3_ipc->mbox));
  405. return PTR_ERR(m3_ipc->mbox);
  406. }
  407. if (of_property_read_u32(dev->of_node, "ti,rproc", &rproc_phandle)) {
  408. dev_err(&pdev->dev, "could not get rproc phandle\n");
  409. ret = -ENODEV;
  410. goto err_free_mbox;
  411. }
  412. m3_rproc = rproc_get_by_phandle(rproc_phandle);
  413. if (!m3_rproc) {
  414. dev_err(&pdev->dev, "could not get rproc handle\n");
  415. ret = -EPROBE_DEFER;
  416. goto err_free_mbox;
  417. }
  418. m3_ipc->rproc = m3_rproc;
  419. m3_ipc->dev = dev;
  420. m3_ipc->state = M3_STATE_RESET;
  421. m3_ipc->ops = &ipc_ops;
  422. /*
  423. * Wait for firmware loading completion in a thread so we
  424. * can boot the wkup_m3 as soon as it's ready without holding
  425. * up kernel boot
  426. */
  427. task = kthread_run((void *)wkup_m3_rproc_boot_thread, m3_ipc,
  428. "wkup_m3_rproc_loader");
  429. if (IS_ERR(task)) {
  430. dev_err(dev, "can't create rproc_boot thread\n");
  431. ret = PTR_ERR(task);
  432. goto err_put_rproc;
  433. }
  434. return 0;
  435. err_put_rproc:
  436. rproc_put(m3_rproc);
  437. err_free_mbox:
  438. mbox_free_channel(m3_ipc->mbox);
  439. return ret;
  440. }
  441. static int wkup_m3_ipc_remove(struct platform_device *pdev)
  442. {
  443. mbox_free_channel(m3_ipc_state->mbox);
  444. rproc_shutdown(m3_ipc_state->rproc);
  445. rproc_put(m3_ipc_state->rproc);
  446. m3_ipc_state = NULL;
  447. return 0;
  448. }
  449. static int __maybe_unused wkup_m3_ipc_suspend(struct device *dev)
  450. {
  451. /*
  452. * Nothing needs to be done on suspend even with rtc_only flag set
  453. */
  454. return 0;
  455. }
  456. static int __maybe_unused wkup_m3_ipc_resume(struct device *dev)
  457. {
  458. if (m3_ipc_state->is_rtc_only) {
  459. rproc_shutdown(m3_ipc_state->rproc);
  460. rproc_boot(m3_ipc_state->rproc);
  461. }
  462. m3_ipc_state->is_rtc_only = false;
  463. return 0;
  464. }
  465. static const struct dev_pm_ops wkup_m3_ipc_pm_ops = {
  466. SET_SYSTEM_SLEEP_PM_OPS(wkup_m3_ipc_suspend, wkup_m3_ipc_resume)
  467. };
  468. static const struct of_device_id wkup_m3_ipc_of_match[] = {
  469. { .compatible = "ti,am3352-wkup-m3-ipc", },
  470. { .compatible = "ti,am4372-wkup-m3-ipc", },
  471. {},
  472. };
  473. MODULE_DEVICE_TABLE(of, wkup_m3_ipc_of_match);
  474. static struct platform_driver wkup_m3_ipc_driver = {
  475. .probe = wkup_m3_ipc_probe,
  476. .remove = wkup_m3_ipc_remove,
  477. .driver = {
  478. .name = "wkup_m3_ipc",
  479. .of_match_table = wkup_m3_ipc_of_match,
  480. .pm = &wkup_m3_ipc_pm_ops,
  481. },
  482. };
  483. module_platform_driver(wkup_m3_ipc_driver);
  484. MODULE_LICENSE("GPL v2");
  485. MODULE_DESCRIPTION("wkup m3 remote processor ipc driver");
  486. MODULE_AUTHOR("Dave Gerlach <d-gerlach@ti.com>");