subsys-pil-bg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/reboot.h>
  24. #include <soc/qcom/subsystem_restart.h>
  25. #include <soc/qcom/ramdump.h>
  26. #include <soc/qcom/subsystem_notif.h>
  27. #include <linux/highmem.h>
  28. #include "peripheral-loader.h"
  29. #include "../../misc/qseecom_kernel.h"
  30. #include "pil_bg_intf.h"
  31. #include "bgcom_interface.h"
  32. #define INVALID_GPIO -1
  33. #define NUM_GPIOS 4
  34. #define SECURE_APP "bgapp"
  35. #define desc_to_data(d) container_of(d, struct pil_bg_data, desc)
  36. #define subsys_to_data(d) container_of(d, struct pil_bg_data, subsys_desc)
  37. #define BG_RAMDUMP_SZ 0x00102000
  38. #define BG_CRASH_IN_TWM -2
  39. /**
  40. * struct pil_bg_data
  41. * @qseecom_handle: handle of TZ app
  42. * @bg_queue: private queue to schedule worker threads for bottom half
  43. * @restart_work: work struct for executing ssr
  44. * @reboot_blk: notification block for reboot event
  45. * @subsys_desc: subsystem descriptor
  46. * @subsys: subsystem device pointer
  47. * @gpios: array to hold all gpio handle
  48. * @desc: PIL descriptor
  49. * @address_fw: address where firmware binaries loaded
  50. * @ramdump_dev: ramdump device pointer
  51. * @size_fw: size of bg firmware binaries
  52. * @errfatal_irq: irq number to indicate bg crash or shutdown
  53. * @status_irq: irq to indicate bg status
  54. * @app_status: status of tz app loading
  55. * @is_ready: Is BG chip up
  56. * @err_ready: The error ready signal
  57. */
  58. struct pil_bg_data {
  59. struct qseecom_handle *qseecom_handle;
  60. struct workqueue_struct *bg_queue;
  61. struct work_struct restart_work;
  62. struct notifier_block reboot_blk;
  63. struct subsys_desc subsys_desc;
  64. struct subsys_device *subsys;
  65. unsigned int gpios[NUM_GPIOS];
  66. int errfatal_irq;
  67. int status_irq;
  68. struct pil_desc desc;
  69. phys_addr_t address_fw;
  70. void *ramdump_dev;
  71. u32 cmd_status;
  72. size_t size_fw;
  73. int app_status;
  74. bool is_ready;
  75. struct completion err_ready;
  76. };
  77. static irqreturn_t bg_status_change(int irq, void *dev_id);
  78. /**
  79. * bg_app_shutdown_notify() - Toggle AP2BG err fatal gpio when
  80. * called by SSR framework.
  81. * @subsys: struct containing private BG data.
  82. *
  83. * Return: none.
  84. */
  85. static void bg_app_shutdown_notify(const struct subsys_desc *subsys)
  86. {
  87. struct pil_bg_data *bg_data = subsys_to_data(subsys);
  88. /* Disable irq if already BG is up */
  89. if (bg_data->is_ready) {
  90. disable_irq(bg_data->status_irq);
  91. disable_irq(bg_data->errfatal_irq);
  92. bg_data->is_ready = false;
  93. }
  94. /* Toggle AP2BG err fatal gpio here to inform apps err fatal event */
  95. if (gpio_is_valid(bg_data->gpios[2])) {
  96. pr_debug("Sending Apps shutdown signal\n");
  97. gpio_set_value(bg_data->gpios[2], 1);
  98. }
  99. }
  100. /**
  101. * bg_app_reboot_notify() - Toggle AP2BG err fatal gpio.
  102. * @nb: struct containing private BG data.
  103. *
  104. * Return: NOTIFY_DONE indicating success.
  105. */
  106. static int bg_app_reboot_notify(struct notifier_block *nb,
  107. unsigned long code, void *unused)
  108. {
  109. struct pil_bg_data *bg_data = container_of(nb,
  110. struct pil_bg_data, reboot_blk);
  111. /* Disable irq if already BG is up */
  112. if (bg_data->is_ready) {
  113. disable_irq(bg_data->status_irq);
  114. disable_irq(bg_data->errfatal_irq);
  115. bg_data->is_ready = false;
  116. }
  117. /* Toggle AP2BG err fatal gpio here to inform apps err fatal event */
  118. if (gpio_is_valid(bg_data->gpios[2])) {
  119. pr_debug("Sending reboot signal\n");
  120. gpio_set_value(bg_data->gpios[2], 1);
  121. }
  122. return NOTIFY_DONE;
  123. }
  124. /**
  125. * get_cmd_rsp_buffers() - Function sets cmd & rsp buffer pointers and
  126. * aligns buffer lengths
  127. * @hdl: index of qseecom_handle
  128. * @cmd: req buffer - set to qseecom_handle.sbuf
  129. * @cmd_len: ptr to req buffer len
  130. * @rsp: rsp buffer - set to qseecom_handle.sbuf + offset
  131. * @rsp_len: ptr to rsp buffer len
  132. *
  133. * Return: Success always .
  134. */
  135. static int get_cmd_rsp_buffers(struct qseecom_handle *handle, void **cmd,
  136. uint32_t *cmd_len, void **rsp, uint32_t *rsp_len)
  137. {
  138. *cmd = handle->sbuf;
  139. if (*cmd_len & QSEECOM_ALIGN_MASK)
  140. *cmd_len = QSEECOM_ALIGN(*cmd_len);
  141. *rsp = handle->sbuf + *cmd_len;
  142. if (*rsp_len & QSEECOM_ALIGN_MASK)
  143. *rsp_len = QSEECOM_ALIGN(*rsp_len);
  144. return 0;
  145. }
  146. /**
  147. * pil_load_bg_tzapp() - Called to load TZ app.
  148. * @pbd: struct containing private BG data.
  149. *
  150. * Return: 0 on success. Error code on failure.
  151. */
  152. static int pil_load_bg_tzapp(struct pil_bg_data *pbd)
  153. {
  154. int rc;
  155. /* return success if already loaded */
  156. if (pbd->qseecom_handle && !pbd->app_status)
  157. return 0;
  158. /* Load the APP */
  159. rc = qseecom_start_app(&pbd->qseecom_handle, SECURE_APP, SZ_4K);
  160. if (rc < 0) {
  161. dev_err(pbd->desc.dev, "BG TZ app load failure\n");
  162. pbd->app_status = RESULT_FAILURE;
  163. return -EIO;
  164. }
  165. pbd->app_status = RESULT_SUCCESS;
  166. return 0;
  167. }
  168. /**
  169. * bgpil_tzapp_comm() - Function called to communicate with TZ APP.
  170. * @req: struct containing command and parameters.
  171. *
  172. * Return: 0 on success. Error code on failure.
  173. */
  174. static long bgpil_tzapp_comm(struct pil_bg_data *pbd,
  175. struct tzapp_bg_req *req)
  176. {
  177. struct tzapp_bg_req *bg_tz_req;
  178. struct tzapp_bg_rsp *bg_tz_rsp;
  179. int rc, req_len, rsp_len;
  180. /* Fill command structure */
  181. req_len = sizeof(struct tzapp_bg_req);
  182. rsp_len = sizeof(struct tzapp_bg_rsp);
  183. rc = get_cmd_rsp_buffers(pbd->qseecom_handle,
  184. (void **)&bg_tz_req, &req_len,
  185. (void **)&bg_tz_rsp, &rsp_len);
  186. if (rc)
  187. goto end;
  188. bg_tz_req->tzapp_bg_cmd = req->tzapp_bg_cmd;
  189. bg_tz_req->address_fw = req->address_fw;
  190. bg_tz_req->size_fw = req->size_fw;
  191. rc = qseecom_send_command(pbd->qseecom_handle,
  192. (void *)bg_tz_req, req_len, (void *)bg_tz_rsp, rsp_len);
  193. pr_debug("BG PIL qseecom returned with value 0x%x and status 0x%x\n",
  194. rc, bg_tz_rsp->status);
  195. if (rc || bg_tz_rsp->status)
  196. pbd->cmd_status = bg_tz_rsp->status;
  197. else
  198. pbd->cmd_status = 0;
  199. end:
  200. return rc;
  201. }
  202. /**
  203. * wait_for_err_ready() - Called in power_up to wait for error ready.
  204. * Signal waiting function.
  205. * @bg_data: BG PIL private structure.
  206. *
  207. * Return: 0 on success. Error code on failure.
  208. */
  209. static int wait_for_err_ready(struct pil_bg_data *bg_data)
  210. {
  211. int ret;
  212. if ((!bg_data->status_irq))
  213. return 0;
  214. ret = wait_for_completion_timeout(&bg_data->err_ready,
  215. msecs_to_jiffies(10000));
  216. if (!ret) {
  217. pr_err("[%s]: Error ready timed out\n", bg_data->desc.name);
  218. return -ETIMEDOUT;
  219. }
  220. return 0;
  221. }
  222. /**
  223. * bg_powerup() - Called by SSR framework on userspace invocation.
  224. * does load tz app and call peripheral loader.
  225. * @subsys: struct containing private BG data.
  226. *
  227. * Return: 0 on success. Error code on failure.
  228. */
  229. static int bg_powerup(const struct subsys_desc *subsys)
  230. {
  231. struct pil_bg_data *bg_data = subsys_to_data(subsys);
  232. int ret;
  233. init_completion(&bg_data->err_ready);
  234. if (!bg_data->qseecom_handle) {
  235. ret = pil_load_bg_tzapp(bg_data);
  236. if (ret) {
  237. dev_err(bg_data->desc.dev,
  238. "%s: BG TZ app load failure\n",
  239. __func__);
  240. return ret;
  241. }
  242. }
  243. pr_debug("bgapp loaded\n");
  244. bg_data->desc.fw_name = subsys->fw_name;
  245. ret = devm_request_irq(bg_data->desc.dev, bg_data->status_irq,
  246. bg_status_change,
  247. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  248. "bg2ap_status", bg_data);
  249. if (ret < 0) {
  250. dev_err(bg_data->desc.dev,
  251. "%s: BG2AP_STATUS IRQ#%d re registration failed, err=%d",
  252. __func__, bg_data->status_irq, ret);
  253. return ret;
  254. }
  255. disable_irq(bg_data->status_irq);
  256. /* Enable status and err fatal irqs */
  257. ret = pil_boot(&bg_data->desc);
  258. if (ret) {
  259. dev_err(bg_data->desc.dev,
  260. "%s: BG PIL Boot failed\n", __func__);
  261. return ret;
  262. }
  263. enable_irq(bg_data->status_irq);
  264. ret = wait_for_err_ready(bg_data);
  265. if (ret) {
  266. dev_err(bg_data->desc.dev,
  267. "[%s:%d]: Timed out waiting for error ready: %s!\n",
  268. current->comm, current->pid, bg_data->desc.name);
  269. return ret;
  270. }
  271. return ret;
  272. }
  273. /**
  274. * bg_shutdown() - Called by SSR framework on userspace invocation.
  275. * disable status interrupt to avoid spurious signal during PRM exit.
  276. * @subsys: struct containing private BG data.
  277. * @force_stop: unused
  278. *
  279. * Return: always success
  280. */
  281. static int bg_shutdown(const struct subsys_desc *subsys, bool force_stop)
  282. {
  283. struct pil_bg_data *bg_data = subsys_to_data(subsys);
  284. if (bg_data->is_ready) {
  285. disable_irq(bg_data->status_irq);
  286. devm_free_irq(bg_data->desc.dev, bg_data->status_irq, bg_data);
  287. disable_irq(bg_data->errfatal_irq);
  288. bg_data->is_ready = false;
  289. }
  290. return 0;
  291. }
  292. /**
  293. * bg_auth_metadata() - Called by Peripheral loader framework
  294. * send command to tz app for authentication of metadata.
  295. * @pil: pil descriptor.
  296. * @metadata: metadata load address
  297. * @size: size of metadata
  298. *
  299. * Return: 0 on success. Error code on failure.
  300. */
  301. static int bg_auth_metadata(struct pil_desc *pil,
  302. const u8 *metadata, size_t size,
  303. phys_addr_t addr, void *sz)
  304. {
  305. struct pil_bg_data *bg_data = desc_to_data(pil);
  306. struct tzapp_bg_req bg_tz_req;
  307. void *mdata_buf;
  308. dma_addr_t mdata_phys;
  309. unsigned long attrs = 0;
  310. struct device dev = {0};
  311. int ret;
  312. arch_setup_dma_ops(&dev, 0, 0, NULL, 0);
  313. dev.coherent_dma_mask = DMA_BIT_MASK(sizeof(dma_addr_t) * 8);
  314. attrs |= DMA_ATTR_STRONGLY_ORDERED;
  315. mdata_buf = dma_alloc_attrs(&dev, size,
  316. &mdata_phys, GFP_KERNEL, attrs);
  317. if (!mdata_buf) {
  318. pr_err("BG_PIL: Allocation for metadata failed.\n");
  319. return -ENOMEM;
  320. }
  321. /* Make sure there are no mappings in PKMAP and fixmap */
  322. kmap_flush_unused();
  323. kmap_atomic_flush_unused();
  324. memcpy(mdata_buf, metadata, size);
  325. bg_tz_req.tzapp_bg_cmd = BGPIL_AUTH_MDT;
  326. bg_tz_req.address_fw = (phys_addr_t)mdata_phys;
  327. bg_tz_req.size_fw = size;
  328. ret = bgpil_tzapp_comm(bg_data, &bg_tz_req);
  329. if (ret || bg_data->cmd_status) {
  330. dev_err(pil->dev,
  331. "%s: BGPIL_AUTH_MDT qseecom call failed\n",
  332. __func__);
  333. return bg_data->cmd_status;
  334. }
  335. dma_free_attrs(&dev, size, mdata_buf, mdata_phys, attrs);
  336. pr_debug("BG MDT Authenticated\n");
  337. return 0;
  338. }
  339. /**
  340. * bg_get_firmware_addr() - Called by Peripheral loader framework
  341. * to get address and size of bg firmware binaries.
  342. * @pil: pil descriptor.
  343. * @addr: fw load address
  344. * @size: size of fw
  345. *
  346. * Return: 0 on success.
  347. */
  348. static int bg_get_firmware_addr(struct pil_desc *pil,
  349. phys_addr_t addr, size_t size)
  350. {
  351. struct pil_bg_data *bg_data = desc_to_data(pil);
  352. bg_data->address_fw = addr;
  353. bg_data->size_fw = size;
  354. pr_debug("BG PIL loads firmware blobs at 0x%x with size 0x%x\n",
  355. addr, size);
  356. return 0;
  357. }
  358. /**
  359. * bg_auth_and_xfer() - Called by Peripheral loader framework
  360. * to signal tz app to authenticate and boot bg chip.
  361. * @pil: pil descriptor.
  362. *
  363. * Return: 0 on success. Error code on failure.
  364. */
  365. static int bg_auth_and_xfer(struct pil_desc *pil)
  366. {
  367. struct pil_bg_data *bg_data = desc_to_data(pil);
  368. struct tzapp_bg_req bg_tz_req;
  369. int ret;
  370. bg_tz_req.tzapp_bg_cmd = BGPIL_IMAGE_LOAD;
  371. bg_tz_req.address_fw = bg_data->address_fw;
  372. bg_tz_req.size_fw = bg_data->size_fw;
  373. ret = bgpil_tzapp_comm(bg_data, &bg_tz_req);
  374. if (bg_data->cmd_status == BG_CRASH_IN_TWM) {
  375. /* Do ramdump and resend boot cmd */
  376. if (is_twm_exit())
  377. bg_data->subsys_desc.ramdump(true,
  378. &bg_data->subsys_desc);
  379. bg_tz_req.tzapp_bg_cmd = BGPIL_DLOAD_CONT;
  380. ret = bgpil_tzapp_comm(bg_data, &bg_tz_req);
  381. }
  382. if (ret || bg_data->cmd_status) {
  383. dev_err(pil->dev,
  384. "%s: BGPIL_IMAGE_LOAD qseecom call failed\n",
  385. __func__);
  386. pil_free_memory(&bg_data->desc);
  387. return bg_data->cmd_status;
  388. }
  389. /* BG Transfer of image is complete, free up the memory */
  390. pr_debug("BG Firmware authentication and transfer done\n");
  391. pil_free_memory(&bg_data->desc);
  392. return 0;
  393. }
  394. /**
  395. * bg_ramdump() - Called by SSR framework to save dump of BG internal
  396. * memory, BG PIL does allocate region from dynamic memory and pass this
  397. * region to tz to dump memory content of BG.
  398. * @subsys: subsystem descriptor.
  399. *
  400. * Return: 0 on success. Error code on failure.
  401. */
  402. static int bg_ramdump(int enable, const struct subsys_desc *subsys)
  403. {
  404. struct pil_bg_data *bg_data = subsys_to_data(subsys);
  405. struct pil_desc desc = bg_data->desc;
  406. struct ramdump_segment *ramdump_segments;
  407. struct tzapp_bg_req bg_tz_req;
  408. phys_addr_t start_addr;
  409. void *region;
  410. int ret;
  411. struct device dev = {0};
  412. arch_setup_dma_ops(&dev, 0, 0, NULL, 0);
  413. desc.attrs = 0;
  414. desc.attrs |= DMA_ATTR_SKIP_ZEROING;
  415. desc.attrs |= DMA_ATTR_STRONGLY_ORDERED;
  416. region = dma_alloc_attrs(desc.dev, BG_RAMDUMP_SZ,
  417. &start_addr, GFP_KERNEL, desc.attrs);
  418. if (region == NULL) {
  419. dev_dbg(desc.dev,
  420. "BG PIL failure to allocate ramdump region of size %zx\n",
  421. BG_RAMDUMP_SZ);
  422. return -ENOMEM;
  423. }
  424. ramdump_segments = kcalloc(1, sizeof(*ramdump_segments), GFP_KERNEL);
  425. if (!ramdump_segments)
  426. return -ENOMEM;
  427. bg_tz_req.tzapp_bg_cmd = BGPIL_RAMDUMP;
  428. bg_tz_req.address_fw = start_addr;
  429. bg_tz_req.size_fw = BG_RAMDUMP_SZ;
  430. ret = bgpil_tzapp_comm(bg_data, &bg_tz_req);
  431. if (ret || bg_data->cmd_status) {
  432. dev_dbg(desc.dev, "%s: BG PIL ramdump collection failed\n",
  433. __func__);
  434. return bg_data->cmd_status;
  435. }
  436. ramdump_segments->address = start_addr;
  437. ramdump_segments->size = BG_RAMDUMP_SZ;
  438. do_ramdump(bg_data->ramdump_dev, ramdump_segments, 1);
  439. kfree(ramdump_segments);
  440. dma_free_attrs(desc.dev, BG_RAMDUMP_SZ, region,
  441. start_addr, desc.attrs);
  442. return 0;
  443. }
  444. static struct pil_reset_ops pil_ops_trusted = {
  445. .init_image = bg_auth_metadata,
  446. .mem_setup = bg_get_firmware_addr,
  447. .auth_and_reset = bg_auth_and_xfer,
  448. .shutdown = NULL,
  449. .proxy_vote = NULL,
  450. .proxy_unvote = NULL,
  451. };
  452. /**
  453. * bg_restart_work() - scheduled by interrupt handler to carry
  454. * out ssr sequence
  455. * @work: work struct.
  456. *
  457. * Return: none.
  458. */
  459. static void bg_restart_work(struct work_struct *work)
  460. {
  461. struct pil_bg_data *drvdata =
  462. container_of(work, struct pil_bg_data, restart_work);
  463. subsystem_restart_dev(drvdata->subsys);
  464. }
  465. static irqreturn_t bg_errfatal(int irq, void *dev_id)
  466. {
  467. struct pil_bg_data *drvdata = (struct pil_bg_data *)dev_id;
  468. if (!drvdata)
  469. return IRQ_HANDLED;
  470. dev_dbg(drvdata->desc.dev, "BG s/w err fatal\n");
  471. queue_work(drvdata->bg_queue, &drvdata->restart_work);
  472. return IRQ_HANDLED;
  473. }
  474. static irqreturn_t bg_status_change(int irq, void *dev_id)
  475. {
  476. bool value;
  477. struct pil_bg_data *drvdata = (struct pil_bg_data *)dev_id;
  478. if (!drvdata)
  479. return IRQ_HANDLED;
  480. value = gpio_get_value(drvdata->gpios[0]);
  481. if (value == true && !drvdata->is_ready) {
  482. dev_info(drvdata->desc.dev,
  483. "BG services are up and running: irq state changed 0->1\n");
  484. drvdata->is_ready = true;
  485. complete(&drvdata->err_ready);
  486. } else if (value == false && drvdata->is_ready) {
  487. dev_err(drvdata->desc.dev,
  488. "BG got unexpected reset: irq state changed 1->0\n");
  489. queue_work(drvdata->bg_queue, &drvdata->restart_work);
  490. } else {
  491. dev_err(drvdata->desc.dev,
  492. "BG status irq: unknown status\n");
  493. }
  494. return IRQ_HANDLED;
  495. }
  496. /**
  497. * setup_bg_gpio_irq() - called in probe to configure input/
  498. * output gpio.
  499. * @drvdata: private data struct for BG.
  500. *
  501. * Return: 0 on success. Error code on failure.
  502. */
  503. static int setup_bg_gpio_irq(struct platform_device *pdev,
  504. struct pil_bg_data *drvdata)
  505. {
  506. int ret = -1;
  507. int irq, i;
  508. if (gpio_request(drvdata->gpios[0], "BG2AP_STATUS")) {
  509. dev_err(&pdev->dev,
  510. "%s Failed to configure BG2AP_STATUS gpio\n",
  511. __func__);
  512. goto err;
  513. }
  514. if (gpio_request(drvdata->gpios[1], "BG2AP_ERRFATAL")) {
  515. dev_err(&pdev->dev,
  516. "%s Failed to configure BG2AP_ERRFATAL gpio\n",
  517. __func__);
  518. goto err;
  519. }
  520. gpio_direction_input(drvdata->gpios[0]);
  521. gpio_direction_input(drvdata->gpios[1]);
  522. /* BG2AP STATUS IRQ */
  523. irq = gpio_to_irq(drvdata->gpios[0]);
  524. if (irq < 0) {
  525. dev_err(&pdev->dev,
  526. "%s: bad BG2AP_STATUS IRQ resource, err = %d\n",
  527. __func__, irq);
  528. goto err;
  529. }
  530. drvdata->status_irq = irq;
  531. /* BG2AP ERR_FATAL irq. */
  532. irq = gpio_to_irq(drvdata->gpios[1]);
  533. if (irq < 0) {
  534. dev_err(&pdev->dev, "bad BG2AP_ERRFATAL IRQ resource\n");
  535. goto err;
  536. }
  537. ret = request_irq(irq, bg_errfatal,
  538. IRQF_TRIGGER_RISING | IRQF_ONESHOT, "bg2ap_errfatal", drvdata);
  539. if (ret < 0) {
  540. dev_err(&pdev->dev,
  541. "%s: BG2AP_ERRFATAL IRQ#%d request failed,\n",
  542. __func__, irq);
  543. goto err;
  544. }
  545. drvdata->errfatal_irq = irq;
  546. /* Configure outgoing GPIO's */
  547. if (gpio_request(drvdata->gpios[2], "AP2BG_ERRFATAL")) {
  548. dev_err(&pdev->dev,
  549. "%s Failed to configure AP2BG_ERRFATAL gpio\n",
  550. __func__);
  551. goto err;
  552. }
  553. if (gpio_request(drvdata->gpios[3], "AP2BG_STATUS")) {
  554. dev_err(&pdev->dev,
  555. "%s Failed to configure AP2BG_STATUS gpio\n",
  556. __func__);
  557. goto err;
  558. }
  559. /*
  560. * Put status gpio in default high state which will
  561. * make transition to low on any sudden reset case of msm
  562. */
  563. gpio_direction_output(drvdata->gpios[2], 0);
  564. gpio_direction_output(drvdata->gpios[3], 1);
  565. /* Inform BG that AP is up */
  566. gpio_set_value(drvdata->gpios[3], 1);
  567. return 0;
  568. err:
  569. for (i = 0; i < NUM_GPIOS; ++i) {
  570. if (gpio_is_valid(drvdata->gpios[i]))
  571. gpio_free(drvdata->gpios[i]);
  572. }
  573. return ret;
  574. }
  575. /**
  576. * bg_dt_parse_gpio() - called in probe to parse gpio's
  577. * @drvdata: private data struct for BG.
  578. *
  579. * Return: 0 on success. Error code on failure.
  580. */
  581. static int bg_dt_parse_gpio(struct platform_device *pdev,
  582. struct pil_bg_data *drvdata)
  583. {
  584. int i, val;
  585. for (i = 0; i < NUM_GPIOS; i++)
  586. drvdata->gpios[i] = INVALID_GPIO;
  587. val = of_get_named_gpio(pdev->dev.of_node,
  588. "qcom,bg2ap-status-gpio", 0);
  589. if (val >= 0)
  590. drvdata->gpios[0] = val;
  591. else {
  592. pr_err("BG status gpio not found, error=%d\n", val);
  593. return -EINVAL;
  594. }
  595. val = of_get_named_gpio(pdev->dev.of_node,
  596. "qcom,bg2ap-errfatal-gpio", 0);
  597. if (val >= 0)
  598. drvdata->gpios[1] = val;
  599. else {
  600. pr_err("BG err-fatal gpio not found, error=%d\n", val);
  601. return -EINVAL;
  602. }
  603. val = of_get_named_gpio(pdev->dev.of_node,
  604. "qcom,ap2bg-errfatal-gpio", 0);
  605. if (val >= 0)
  606. drvdata->gpios[2] = val;
  607. else {
  608. pr_err("ap2bg err-fatal gpio not found, error=%d\n", val);
  609. return -EINVAL;
  610. }
  611. val = of_get_named_gpio(pdev->dev.of_node,
  612. "qcom,ap2bg-status-gpio", 0);
  613. if (val >= 0)
  614. drvdata->gpios[3] = val;
  615. else {
  616. pr_err("ap2bg status gpio not found, error=%d\n", val);
  617. return -EINVAL;
  618. }
  619. return 0;
  620. }
  621. static int pil_bg_driver_probe(struct platform_device *pdev)
  622. {
  623. struct pil_bg_data *bg_data;
  624. int rc;
  625. bg_data = devm_kzalloc(&pdev->dev, sizeof(*bg_data), GFP_KERNEL);
  626. if (!bg_data)
  627. return -ENOMEM;
  628. platform_set_drvdata(pdev, bg_data);
  629. rc = of_property_read_string(pdev->dev.of_node,
  630. "qcom,firmware-name", &bg_data->desc.name);
  631. if (rc)
  632. return rc;
  633. bg_data->desc.dev = &pdev->dev;
  634. bg_data->desc.owner = THIS_MODULE;
  635. bg_data->desc.ops = &pil_ops_trusted;
  636. rc = pil_desc_init(&bg_data->desc);
  637. if (rc)
  638. return rc;
  639. /* Read gpio configuration */
  640. rc = bg_dt_parse_gpio(pdev, bg_data);
  641. if (rc)
  642. return rc;
  643. rc = setup_bg_gpio_irq(pdev, bg_data);
  644. if (rc < 0)
  645. return rc;
  646. bg_data->subsys_desc.name = bg_data->desc.name;
  647. bg_data->subsys_desc.owner = THIS_MODULE;
  648. bg_data->subsys_desc.dev = &pdev->dev;
  649. bg_data->subsys_desc.shutdown = bg_shutdown;
  650. bg_data->subsys_desc.powerup = bg_powerup;
  651. bg_data->subsys_desc.ramdump = bg_ramdump;
  652. bg_data->subsys_desc.free_memory = NULL;
  653. bg_data->subsys_desc.crash_shutdown = bg_app_shutdown_notify;
  654. bg_data->ramdump_dev =
  655. create_ramdump_device(bg_data->subsys_desc.name, &pdev->dev);
  656. if (!bg_data->ramdump_dev) {
  657. rc = -ENOMEM;
  658. goto err_ramdump;
  659. }
  660. bg_data->subsys = subsys_register(&bg_data->subsys_desc);
  661. if (IS_ERR(bg_data->subsys)) {
  662. rc = PTR_ERR(bg_data->subsys);
  663. goto err_subsys;
  664. }
  665. bg_data->reboot_blk.notifier_call = bg_app_reboot_notify;
  666. register_reboot_notifier(&bg_data->reboot_blk);
  667. bg_data->bg_queue = alloc_workqueue("bg_queue", 0, 0);
  668. if (!bg_data->bg_queue) {
  669. dev_err(&pdev->dev, "could not create bg_queue\n");
  670. subsys_unregister(bg_data->subsys);
  671. goto err_subsys;
  672. }
  673. INIT_WORK(&bg_data->restart_work, bg_restart_work);
  674. return 0;
  675. err_subsys:
  676. destroy_ramdump_device(bg_data->ramdump_dev);
  677. err_ramdump:
  678. pil_desc_release(&bg_data->desc);
  679. return rc;
  680. }
  681. static int pil_bg_driver_exit(struct platform_device *pdev)
  682. {
  683. struct pil_bg_data *bg_data = platform_get_drvdata(pdev);
  684. subsys_unregister(bg_data->subsys);
  685. destroy_ramdump_device(bg_data->ramdump_dev);
  686. pil_desc_release(&bg_data->desc);
  687. return 0;
  688. }
  689. const struct of_device_id pil_bg_match_table[] = {
  690. {.compatible = "qcom,pil-blackghost"},
  691. {}
  692. };
  693. static struct platform_driver pil_bg_driver = {
  694. .probe = pil_bg_driver_probe,
  695. .remove = pil_bg_driver_exit,
  696. .driver = {
  697. .name = "subsys-pil-bg",
  698. .of_match_table = pil_bg_match_table,
  699. .owner = THIS_MODULE,
  700. },
  701. };
  702. static int __init pil_bg_init(void)
  703. {
  704. return platform_driver_register(&pil_bg_driver);
  705. }
  706. module_init(pil_bg_init);
  707. static void __exit pil_bg_exit(void)
  708. {
  709. platform_driver_unregister(&pil_bg_driver);
  710. }
  711. module_exit(pil_bg_exit);
  712. MODULE_DESCRIPTION("Support for booting QTI Blackghost SoC");
  713. MODULE_LICENSE("GPL v2");