omap_remoteproc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OMAP Remote Processor driver
  4. *
  5. * Copyright (C) 2011-2020 Texas Instruments Incorporated - http://www.ti.com/
  6. * Copyright (C) 2011 Google, Inc.
  7. *
  8. * Ohad Ben-Cohen <ohad@wizery.com>
  9. * Brian Swetland <swetland@google.com>
  10. * Fernando Guzman Lugo <fernando.lugo@ti.com>
  11. * Mark Grosen <mgrosen@ti.com>
  12. * Suman Anna <s-anna@ti.com>
  13. * Hari Kanigeri <h-kanigeri2@ti.com>
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/clk.h>
  18. #include <linux/clk/ti.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_reserved_mem.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/remoteproc.h>
  28. #include <linux/mailbox_client.h>
  29. #include <linux/omap-iommu.h>
  30. #include <linux/omap-mailbox.h>
  31. #include <linux/regmap.h>
  32. #include <linux/mfd/syscon.h>
  33. #include <linux/reset.h>
  34. #include <clocksource/timer-ti-dm.h>
  35. #include <linux/platform_data/dmtimer-omap.h>
  36. #include "omap_remoteproc.h"
  37. #include "remoteproc_internal.h"
  38. /* default auto-suspend delay (ms) */
  39. #define DEFAULT_AUTOSUSPEND_DELAY 10000
  40. /**
  41. * struct omap_rproc_boot_data - boot data structure for the DSP omap rprocs
  42. * @syscon: regmap handle for the system control configuration module
  43. * @boot_reg: boot register offset within the @syscon regmap
  44. * @boot_reg_shift: bit-field shift required for the boot address value in
  45. * @boot_reg
  46. */
  47. struct omap_rproc_boot_data {
  48. struct regmap *syscon;
  49. unsigned int boot_reg;
  50. unsigned int boot_reg_shift;
  51. };
  52. /**
  53. * struct omap_rproc_mem - internal memory structure
  54. * @cpu_addr: MPU virtual address of the memory region
  55. * @bus_addr: bus address used to access the memory region
  56. * @dev_addr: device address of the memory region from DSP view
  57. * @size: size of the memory region
  58. */
  59. struct omap_rproc_mem {
  60. void __iomem *cpu_addr;
  61. phys_addr_t bus_addr;
  62. u32 dev_addr;
  63. size_t size;
  64. };
  65. /**
  66. * struct omap_rproc_timer - data structure for a timer used by a omap rproc
  67. * @odt: timer pointer
  68. * @timer_ops: OMAP dmtimer ops for @odt timer
  69. * @irq: timer irq
  70. */
  71. struct omap_rproc_timer {
  72. struct omap_dm_timer *odt;
  73. const struct omap_dm_timer_ops *timer_ops;
  74. int irq;
  75. };
  76. /**
  77. * struct omap_rproc - omap remote processor state
  78. * @mbox: mailbox channel handle
  79. * @client: mailbox client to request the mailbox channel
  80. * @boot_data: boot data structure for setting processor boot address
  81. * @mem: internal memory regions data
  82. * @num_mems: number of internal memory regions
  83. * @num_timers: number of rproc timer(s)
  84. * @num_wd_timers: number of rproc watchdog timers
  85. * @timers: timer(s) info used by rproc
  86. * @autosuspend_delay: auto-suspend delay value to be used for runtime pm
  87. * @need_resume: if true a resume is needed in the system resume callback
  88. * @rproc: rproc handle
  89. * @reset: reset handle
  90. * @pm_comp: completion primitive to sync for suspend response
  91. * @fck: functional clock for the remoteproc
  92. * @suspend_acked: state machine flag to store the suspend request ack
  93. */
  94. struct omap_rproc {
  95. struct mbox_chan *mbox;
  96. struct mbox_client client;
  97. struct omap_rproc_boot_data *boot_data;
  98. struct omap_rproc_mem *mem;
  99. int num_mems;
  100. int num_timers;
  101. int num_wd_timers;
  102. struct omap_rproc_timer *timers;
  103. int autosuspend_delay;
  104. bool need_resume;
  105. struct rproc *rproc;
  106. struct reset_control *reset;
  107. struct completion pm_comp;
  108. struct clk *fck;
  109. bool suspend_acked;
  110. };
  111. /**
  112. * struct omap_rproc_mem_data - memory definitions for an omap remote processor
  113. * @name: name for this memory entry
  114. * @dev_addr: device address for the memory entry
  115. */
  116. struct omap_rproc_mem_data {
  117. const char *name;
  118. const u32 dev_addr;
  119. };
  120. /**
  121. * struct omap_rproc_dev_data - device data for the omap remote processor
  122. * @device_name: device name of the remote processor
  123. * @mems: memory definitions for this remote processor
  124. */
  125. struct omap_rproc_dev_data {
  126. const char *device_name;
  127. const struct omap_rproc_mem_data *mems;
  128. };
  129. /**
  130. * omap_rproc_request_timer() - request a timer for a remoteproc
  131. * @dev: device requesting the timer
  132. * @np: device node pointer to the desired timer
  133. * @timer: handle to a struct omap_rproc_timer to return the timer handle
  134. *
  135. * This helper function is used primarily to request a timer associated with
  136. * a remoteproc. The returned handle is stored in the .odt field of the
  137. * @timer structure passed in, and is used to invoke other timer specific
  138. * ops (like starting a timer either during device initialization or during
  139. * a resume operation, or for stopping/freeing a timer).
  140. *
  141. * Return: 0 on success, otherwise an appropriate failure
  142. */
  143. static int omap_rproc_request_timer(struct device *dev, struct device_node *np,
  144. struct omap_rproc_timer *timer)
  145. {
  146. int ret;
  147. timer->odt = timer->timer_ops->request_by_node(np);
  148. if (!timer->odt) {
  149. dev_err(dev, "request for timer node %p failed\n", np);
  150. return -EBUSY;
  151. }
  152. ret = timer->timer_ops->set_source(timer->odt, OMAP_TIMER_SRC_SYS_CLK);
  153. if (ret) {
  154. dev_err(dev, "error setting OMAP_TIMER_SRC_SYS_CLK as source for timer node %p\n",
  155. np);
  156. timer->timer_ops->free(timer->odt);
  157. return ret;
  158. }
  159. /* clean counter, remoteproc code will set the value */
  160. timer->timer_ops->set_load(timer->odt, 0);
  161. return 0;
  162. }
  163. /**
  164. * omap_rproc_start_timer() - start a timer for a remoteproc
  165. * @timer: handle to a OMAP rproc timer
  166. *
  167. * This helper function is used to start a timer associated with a remoteproc,
  168. * obtained using the request_timer ops. The helper function needs to be
  169. * invoked by the driver to start the timer (during device initialization)
  170. * or to just resume the timer.
  171. *
  172. * Return: 0 on success, otherwise a failure as returned by DMTimer ops
  173. */
  174. static inline int omap_rproc_start_timer(struct omap_rproc_timer *timer)
  175. {
  176. return timer->timer_ops->start(timer->odt);
  177. }
  178. /**
  179. * omap_rproc_stop_timer() - stop a timer for a remoteproc
  180. * @timer: handle to a OMAP rproc timer
  181. *
  182. * This helper function is used to disable a timer associated with a
  183. * remoteproc, and needs to be called either during a device shutdown
  184. * or suspend operation. The separate helper function allows the driver
  185. * to just stop a timer without having to release the timer during a
  186. * suspend operation.
  187. *
  188. * Return: 0 on success, otherwise a failure as returned by DMTimer ops
  189. */
  190. static inline int omap_rproc_stop_timer(struct omap_rproc_timer *timer)
  191. {
  192. return timer->timer_ops->stop(timer->odt);
  193. }
  194. /**
  195. * omap_rproc_release_timer() - release a timer for a remoteproc
  196. * @timer: handle to a OMAP rproc timer
  197. *
  198. * This helper function is used primarily to release a timer associated
  199. * with a remoteproc. The dmtimer will be available for other clients to
  200. * use once released.
  201. *
  202. * Return: 0 on success, otherwise a failure as returned by DMTimer ops
  203. */
  204. static inline int omap_rproc_release_timer(struct omap_rproc_timer *timer)
  205. {
  206. return timer->timer_ops->free(timer->odt);
  207. }
  208. /**
  209. * omap_rproc_get_timer_irq() - get the irq for a timer
  210. * @timer: handle to a OMAP rproc timer
  211. *
  212. * This function is used to get the irq associated with a watchdog timer. The
  213. * function is called by the OMAP remoteproc driver to register a interrupt
  214. * handler to handle watchdog events on the remote processor.
  215. *
  216. * Return: irq id on success, otherwise a failure as returned by DMTimer ops
  217. */
  218. static inline int omap_rproc_get_timer_irq(struct omap_rproc_timer *timer)
  219. {
  220. return timer->timer_ops->get_irq(timer->odt);
  221. }
  222. /**
  223. * omap_rproc_ack_timer_irq() - acknowledge a timer irq
  224. * @timer: handle to a OMAP rproc timer
  225. *
  226. * This function is used to clear the irq associated with a watchdog timer. The
  227. * The function is called by the OMAP remoteproc upon a watchdog event on the
  228. * remote processor to clear the interrupt status of the watchdog timer.
  229. */
  230. static inline void omap_rproc_ack_timer_irq(struct omap_rproc_timer *timer)
  231. {
  232. timer->timer_ops->write_status(timer->odt, OMAP_TIMER_INT_OVERFLOW);
  233. }
  234. /**
  235. * omap_rproc_watchdog_isr() - Watchdog ISR handler for remoteproc device
  236. * @irq: IRQ number associated with a watchdog timer
  237. * @data: IRQ handler data
  238. *
  239. * This ISR routine executes the required necessary low-level code to
  240. * acknowledge a watchdog timer interrupt. There can be multiple watchdog
  241. * timers associated with a rproc (like IPUs which have 2 watchdog timers,
  242. * one per Cortex M3/M4 core), so a lookup has to be performed to identify
  243. * the timer to acknowledge its interrupt.
  244. *
  245. * The function also invokes rproc_report_crash to report the watchdog event
  246. * to the remoteproc driver core, to trigger a recovery.
  247. *
  248. * Return: IRQ_HANDLED on success, otherwise IRQ_NONE
  249. */
  250. static irqreturn_t omap_rproc_watchdog_isr(int irq, void *data)
  251. {
  252. struct rproc *rproc = data;
  253. struct omap_rproc *oproc = rproc->priv;
  254. struct device *dev = rproc->dev.parent;
  255. struct omap_rproc_timer *timers = oproc->timers;
  256. struct omap_rproc_timer *wd_timer = NULL;
  257. int num_timers = oproc->num_timers + oproc->num_wd_timers;
  258. int i;
  259. for (i = oproc->num_timers; i < num_timers; i++) {
  260. if (timers[i].irq > 0 && irq == timers[i].irq) {
  261. wd_timer = &timers[i];
  262. break;
  263. }
  264. }
  265. if (!wd_timer) {
  266. dev_err(dev, "invalid timer\n");
  267. return IRQ_NONE;
  268. }
  269. omap_rproc_ack_timer_irq(wd_timer);
  270. rproc_report_crash(rproc, RPROC_WATCHDOG);
  271. return IRQ_HANDLED;
  272. }
  273. /**
  274. * omap_rproc_enable_timers() - enable the timers for a remoteproc
  275. * @rproc: handle of a remote processor
  276. * @configure: boolean flag used to acquire and configure the timer handle
  277. *
  278. * This function is used primarily to enable the timers associated with
  279. * a remoteproc. The configure flag is provided to allow the driver to
  280. * to either acquire and start a timer (during device initialization) or
  281. * to just start a timer (during a resume operation).
  282. *
  283. * Return: 0 on success, otherwise an appropriate failure
  284. */
  285. static int omap_rproc_enable_timers(struct rproc *rproc, bool configure)
  286. {
  287. int i;
  288. int ret = 0;
  289. struct platform_device *tpdev;
  290. struct dmtimer_platform_data *tpdata;
  291. const struct omap_dm_timer_ops *timer_ops;
  292. struct omap_rproc *oproc = rproc->priv;
  293. struct omap_rproc_timer *timers = oproc->timers;
  294. struct device *dev = rproc->dev.parent;
  295. struct device_node *np = NULL;
  296. int num_timers = oproc->num_timers + oproc->num_wd_timers;
  297. if (!num_timers)
  298. return 0;
  299. if (!configure)
  300. goto start_timers;
  301. for (i = 0; i < num_timers; i++) {
  302. if (i < oproc->num_timers)
  303. np = of_parse_phandle(dev->of_node, "ti,timers", i);
  304. else
  305. np = of_parse_phandle(dev->of_node,
  306. "ti,watchdog-timers",
  307. (i - oproc->num_timers));
  308. if (!np) {
  309. ret = -ENXIO;
  310. dev_err(dev, "device node lookup for timer at index %d failed: %d\n",
  311. i < oproc->num_timers ? i :
  312. i - oproc->num_timers, ret);
  313. goto free_timers;
  314. }
  315. tpdev = of_find_device_by_node(np);
  316. if (!tpdev) {
  317. ret = -ENODEV;
  318. dev_err(dev, "could not get timer platform device\n");
  319. goto put_node;
  320. }
  321. tpdata = dev_get_platdata(&tpdev->dev);
  322. put_device(&tpdev->dev);
  323. if (!tpdata) {
  324. ret = -EINVAL;
  325. dev_err(dev, "dmtimer pdata structure NULL\n");
  326. goto put_node;
  327. }
  328. timer_ops = tpdata->timer_ops;
  329. if (!timer_ops || !timer_ops->request_by_node ||
  330. !timer_ops->set_source || !timer_ops->set_load ||
  331. !timer_ops->free || !timer_ops->start ||
  332. !timer_ops->stop || !timer_ops->get_irq ||
  333. !timer_ops->write_status) {
  334. ret = -EINVAL;
  335. dev_err(dev, "device does not have required timer ops\n");
  336. goto put_node;
  337. }
  338. timers[i].irq = -1;
  339. timers[i].timer_ops = timer_ops;
  340. ret = omap_rproc_request_timer(dev, np, &timers[i]);
  341. if (ret) {
  342. dev_err(dev, "request for timer %p failed: %d\n", np,
  343. ret);
  344. goto put_node;
  345. }
  346. of_node_put(np);
  347. if (i >= oproc->num_timers) {
  348. timers[i].irq = omap_rproc_get_timer_irq(&timers[i]);
  349. if (timers[i].irq < 0) {
  350. dev_err(dev, "get_irq for timer %p failed: %d\n",
  351. np, timers[i].irq);
  352. ret = -EBUSY;
  353. goto free_timers;
  354. }
  355. ret = request_irq(timers[i].irq,
  356. omap_rproc_watchdog_isr, IRQF_SHARED,
  357. "rproc-wdt", rproc);
  358. if (ret) {
  359. dev_err(dev, "error requesting irq for timer %p\n",
  360. np);
  361. omap_rproc_release_timer(&timers[i]);
  362. timers[i].odt = NULL;
  363. timers[i].timer_ops = NULL;
  364. timers[i].irq = -1;
  365. goto free_timers;
  366. }
  367. }
  368. }
  369. start_timers:
  370. for (i = 0; i < num_timers; i++) {
  371. ret = omap_rproc_start_timer(&timers[i]);
  372. if (ret) {
  373. dev_err(dev, "start timer %p failed failed: %d\n", np,
  374. ret);
  375. break;
  376. }
  377. }
  378. if (ret) {
  379. while (i >= 0) {
  380. omap_rproc_stop_timer(&timers[i]);
  381. i--;
  382. }
  383. goto put_node;
  384. }
  385. return 0;
  386. put_node:
  387. if (configure)
  388. of_node_put(np);
  389. free_timers:
  390. while (i--) {
  391. if (i >= oproc->num_timers)
  392. free_irq(timers[i].irq, rproc);
  393. omap_rproc_release_timer(&timers[i]);
  394. timers[i].odt = NULL;
  395. timers[i].timer_ops = NULL;
  396. timers[i].irq = -1;
  397. }
  398. return ret;
  399. }
  400. /**
  401. * omap_rproc_disable_timers() - disable the timers for a remoteproc
  402. * @rproc: handle of a remote processor
  403. * @configure: boolean flag used to release the timer handle
  404. *
  405. * This function is used primarily to disable the timers associated with
  406. * a remoteproc. The configure flag is provided to allow the driver to
  407. * to either stop and release a timer (during device shutdown) or to just
  408. * stop a timer (during a suspend operation).
  409. *
  410. * Return: 0 on success or no timers
  411. */
  412. static int omap_rproc_disable_timers(struct rproc *rproc, bool configure)
  413. {
  414. int i;
  415. struct omap_rproc *oproc = rproc->priv;
  416. struct omap_rproc_timer *timers = oproc->timers;
  417. int num_timers = oproc->num_timers + oproc->num_wd_timers;
  418. if (!num_timers)
  419. return 0;
  420. for (i = 0; i < num_timers; i++) {
  421. omap_rproc_stop_timer(&timers[i]);
  422. if (configure) {
  423. if (i >= oproc->num_timers)
  424. free_irq(timers[i].irq, rproc);
  425. omap_rproc_release_timer(&timers[i]);
  426. timers[i].odt = NULL;
  427. timers[i].timer_ops = NULL;
  428. timers[i].irq = -1;
  429. }
  430. }
  431. return 0;
  432. }
  433. /**
  434. * omap_rproc_mbox_callback() - inbound mailbox message handler
  435. * @client: mailbox client pointer used for requesting the mailbox channel
  436. * @data: mailbox payload
  437. *
  438. * This handler is invoked by omap's mailbox driver whenever a mailbox
  439. * message is received. Usually, the mailbox payload simply contains
  440. * the index of the virtqueue that is kicked by the remote processor,
  441. * and we let remoteproc core handle it.
  442. *
  443. * In addition to virtqueue indices, we also have some out-of-band values
  444. * that indicates different events. Those values are deliberately very
  445. * big so they don't coincide with virtqueue indices.
  446. */
  447. static void omap_rproc_mbox_callback(struct mbox_client *client, void *data)
  448. {
  449. struct omap_rproc *oproc = container_of(client, struct omap_rproc,
  450. client);
  451. struct device *dev = oproc->rproc->dev.parent;
  452. const char *name = oproc->rproc->name;
  453. u32 msg = (u32)data;
  454. dev_dbg(dev, "mbox msg: 0x%x\n", msg);
  455. switch (msg) {
  456. case RP_MBOX_CRASH:
  457. /*
  458. * remoteproc detected an exception, notify the rproc core.
  459. * The remoteproc core will handle the recovery.
  460. */
  461. dev_err(dev, "omap rproc %s crashed\n", name);
  462. rproc_report_crash(oproc->rproc, RPROC_FATAL_ERROR);
  463. break;
  464. case RP_MBOX_ECHO_REPLY:
  465. dev_info(dev, "received echo reply from %s\n", name);
  466. break;
  467. case RP_MBOX_SUSPEND_ACK:
  468. case RP_MBOX_SUSPEND_CANCEL:
  469. oproc->suspend_acked = msg == RP_MBOX_SUSPEND_ACK;
  470. complete(&oproc->pm_comp);
  471. break;
  472. default:
  473. if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
  474. return;
  475. if (msg > oproc->rproc->max_notifyid) {
  476. dev_dbg(dev, "dropping unknown message 0x%x", msg);
  477. return;
  478. }
  479. /* msg contains the index of the triggered vring */
  480. if (rproc_vq_interrupt(oproc->rproc, msg) == IRQ_NONE)
  481. dev_dbg(dev, "no message was found in vqid %d\n", msg);
  482. }
  483. }
  484. /* kick a virtqueue */
  485. static void omap_rproc_kick(struct rproc *rproc, int vqid)
  486. {
  487. struct omap_rproc *oproc = rproc->priv;
  488. struct device *dev = rproc->dev.parent;
  489. int ret;
  490. /* wake up the rproc before kicking it */
  491. ret = pm_runtime_get_sync(dev);
  492. if (WARN_ON(ret < 0)) {
  493. dev_err(dev, "pm_runtime_get_sync() failed during kick, ret = %d\n",
  494. ret);
  495. pm_runtime_put_noidle(dev);
  496. return;
  497. }
  498. /* send the index of the triggered virtqueue in the mailbox payload */
  499. ret = mbox_send_message(oproc->mbox, (void *)vqid);
  500. if (ret < 0)
  501. dev_err(dev, "failed to send mailbox message, status = %d\n",
  502. ret);
  503. pm_runtime_mark_last_busy(dev);
  504. pm_runtime_put_autosuspend(dev);
  505. }
  506. /**
  507. * omap_rproc_write_dsp_boot_addr() - set boot address for DSP remote processor
  508. * @rproc: handle of a remote processor
  509. *
  510. * Set boot address for a supported DSP remote processor.
  511. *
  512. * Return: 0 on success, or -EINVAL if boot address is not aligned properly
  513. */
  514. static int omap_rproc_write_dsp_boot_addr(struct rproc *rproc)
  515. {
  516. struct device *dev = rproc->dev.parent;
  517. struct omap_rproc *oproc = rproc->priv;
  518. struct omap_rproc_boot_data *bdata = oproc->boot_data;
  519. u32 offset = bdata->boot_reg;
  520. u32 value;
  521. u32 mask;
  522. if (rproc->bootaddr & (SZ_1K - 1)) {
  523. dev_err(dev, "invalid boot address 0x%llx, must be aligned on a 1KB boundary\n",
  524. rproc->bootaddr);
  525. return -EINVAL;
  526. }
  527. value = rproc->bootaddr >> bdata->boot_reg_shift;
  528. mask = ~(SZ_1K - 1) >> bdata->boot_reg_shift;
  529. return regmap_update_bits(bdata->syscon, offset, mask, value);
  530. }
  531. /*
  532. * Power up the remote processor.
  533. *
  534. * This function will be invoked only after the firmware for this rproc
  535. * was loaded, parsed successfully, and all of its resource requirements
  536. * were met.
  537. */
  538. static int omap_rproc_start(struct rproc *rproc)
  539. {
  540. struct omap_rproc *oproc = rproc->priv;
  541. struct device *dev = rproc->dev.parent;
  542. int ret;
  543. struct mbox_client *client = &oproc->client;
  544. if (oproc->boot_data) {
  545. ret = omap_rproc_write_dsp_boot_addr(rproc);
  546. if (ret)
  547. return ret;
  548. }
  549. client->dev = dev;
  550. client->tx_done = NULL;
  551. client->rx_callback = omap_rproc_mbox_callback;
  552. client->tx_block = false;
  553. client->knows_txdone = false;
  554. oproc->mbox = mbox_request_channel(client, 0);
  555. if (IS_ERR(oproc->mbox)) {
  556. ret = -EBUSY;
  557. dev_err(dev, "mbox_request_channel failed: %ld\n",
  558. PTR_ERR(oproc->mbox));
  559. return ret;
  560. }
  561. /*
  562. * Ping the remote processor. this is only for sanity-sake;
  563. * there is no functional effect whatsoever.
  564. *
  565. * Note that the reply will _not_ arrive immediately: this message
  566. * will wait in the mailbox fifo until the remote processor is booted.
  567. */
  568. ret = mbox_send_message(oproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
  569. if (ret < 0) {
  570. dev_err(dev, "mbox_send_message failed: %d\n", ret);
  571. goto put_mbox;
  572. }
  573. ret = omap_rproc_enable_timers(rproc, true);
  574. if (ret) {
  575. dev_err(dev, "omap_rproc_enable_timers failed: %d\n", ret);
  576. goto put_mbox;
  577. }
  578. ret = reset_control_deassert(oproc->reset);
  579. if (ret) {
  580. dev_err(dev, "reset control deassert failed: %d\n", ret);
  581. goto disable_timers;
  582. }
  583. /*
  584. * remote processor is up, so update the runtime pm status and
  585. * enable the auto-suspend. The device usage count is incremented
  586. * manually for balancing it for auto-suspend
  587. */
  588. pm_runtime_set_active(dev);
  589. pm_runtime_use_autosuspend(dev);
  590. pm_runtime_get_noresume(dev);
  591. pm_runtime_enable(dev);
  592. pm_runtime_mark_last_busy(dev);
  593. pm_runtime_put_autosuspend(dev);
  594. return 0;
  595. disable_timers:
  596. omap_rproc_disable_timers(rproc, true);
  597. put_mbox:
  598. mbox_free_channel(oproc->mbox);
  599. return ret;
  600. }
  601. /* power off the remote processor */
  602. static int omap_rproc_stop(struct rproc *rproc)
  603. {
  604. struct device *dev = rproc->dev.parent;
  605. struct omap_rproc *oproc = rproc->priv;
  606. int ret;
  607. /*
  608. * cancel any possible scheduled runtime suspend by incrementing
  609. * the device usage count, and resuming the device. The remoteproc
  610. * also needs to be woken up if suspended, to avoid the remoteproc
  611. * OS to continue to remember any context that it has saved, and
  612. * avoid potential issues in misindentifying a subsequent device
  613. * reboot as a power restore boot
  614. */
  615. ret = pm_runtime_get_sync(dev);
  616. if (ret < 0) {
  617. pm_runtime_put_noidle(dev);
  618. return ret;
  619. }
  620. ret = reset_control_assert(oproc->reset);
  621. if (ret)
  622. goto out;
  623. ret = omap_rproc_disable_timers(rproc, true);
  624. if (ret)
  625. goto enable_device;
  626. mbox_free_channel(oproc->mbox);
  627. /*
  628. * update the runtime pm states and status now that the remoteproc
  629. * has stopped
  630. */
  631. pm_runtime_disable(dev);
  632. pm_runtime_dont_use_autosuspend(dev);
  633. pm_runtime_put_noidle(dev);
  634. pm_runtime_set_suspended(dev);
  635. return 0;
  636. enable_device:
  637. reset_control_deassert(oproc->reset);
  638. out:
  639. /* schedule the next auto-suspend */
  640. pm_runtime_mark_last_busy(dev);
  641. pm_runtime_put_autosuspend(dev);
  642. return ret;
  643. }
  644. /**
  645. * omap_rproc_da_to_va() - internal memory translation helper
  646. * @rproc: remote processor to apply the address translation for
  647. * @da: device address to translate
  648. * @len: length of the memory buffer
  649. *
  650. * Custom function implementing the rproc .da_to_va ops to provide address
  651. * translation (device address to kernel virtual address) for internal RAMs
  652. * present in a DSP or IPU device). The translated addresses can be used
  653. * either by the remoteproc core for loading, or by any rpmsg bus drivers.
  654. *
  655. * Return: translated virtual address in kernel memory space on success,
  656. * or NULL on failure.
  657. */
  658. static void *omap_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
  659. {
  660. struct omap_rproc *oproc = rproc->priv;
  661. int i;
  662. u32 offset;
  663. if (len <= 0)
  664. return NULL;
  665. if (!oproc->num_mems)
  666. return NULL;
  667. for (i = 0; i < oproc->num_mems; i++) {
  668. if (da >= oproc->mem[i].dev_addr && da + len <=
  669. oproc->mem[i].dev_addr + oproc->mem[i].size) {
  670. offset = da - oproc->mem[i].dev_addr;
  671. /* __force to make sparse happy with type conversion */
  672. return (__force void *)(oproc->mem[i].cpu_addr +
  673. offset);
  674. }
  675. }
  676. return NULL;
  677. }
  678. static const struct rproc_ops omap_rproc_ops = {
  679. .start = omap_rproc_start,
  680. .stop = omap_rproc_stop,
  681. .kick = omap_rproc_kick,
  682. .da_to_va = omap_rproc_da_to_va,
  683. };
  684. #ifdef CONFIG_PM
  685. static bool _is_rproc_in_standby(struct omap_rproc *oproc)
  686. {
  687. return ti_clk_is_in_standby(oproc->fck);
  688. }
  689. /* 1 sec is long enough time to let the remoteproc side suspend the device */
  690. #define DEF_SUSPEND_TIMEOUT 1000
  691. static int _omap_rproc_suspend(struct rproc *rproc, bool auto_suspend)
  692. {
  693. struct device *dev = rproc->dev.parent;
  694. struct omap_rproc *oproc = rproc->priv;
  695. unsigned long to = msecs_to_jiffies(DEF_SUSPEND_TIMEOUT);
  696. unsigned long ta = jiffies + to;
  697. u32 suspend_msg = auto_suspend ?
  698. RP_MBOX_SUSPEND_AUTO : RP_MBOX_SUSPEND_SYSTEM;
  699. int ret;
  700. reinit_completion(&oproc->pm_comp);
  701. oproc->suspend_acked = false;
  702. ret = mbox_send_message(oproc->mbox, (void *)suspend_msg);
  703. if (ret < 0) {
  704. dev_err(dev, "PM mbox_send_message failed: %d\n", ret);
  705. return ret;
  706. }
  707. ret = wait_for_completion_timeout(&oproc->pm_comp, to);
  708. if (!oproc->suspend_acked)
  709. return -EBUSY;
  710. /*
  711. * The remoteproc side is returning the ACK message before saving the
  712. * context, because the context saving is performed within a SYS/BIOS
  713. * function, and it cannot have any inter-dependencies against the IPC
  714. * layer. Also, as the SYS/BIOS needs to preserve properly the processor
  715. * register set, sending this ACK or signalling the completion of the
  716. * context save through a shared memory variable can never be the
  717. * absolute last thing to be executed on the remoteproc side, and the
  718. * MPU cannot use the ACK message as a sync point to put the remoteproc
  719. * into reset. The only way to ensure that the remote processor has
  720. * completed saving the context is to check that the module has reached
  721. * STANDBY state (after saving the context, the SYS/BIOS executes the
  722. * appropriate target-specific WFI instruction causing the module to
  723. * enter STANDBY).
  724. */
  725. while (!_is_rproc_in_standby(oproc)) {
  726. if (time_after(jiffies, ta))
  727. return -ETIME;
  728. schedule();
  729. }
  730. ret = reset_control_assert(oproc->reset);
  731. if (ret) {
  732. dev_err(dev, "reset assert during suspend failed %d\n", ret);
  733. return ret;
  734. }
  735. ret = omap_rproc_disable_timers(rproc, false);
  736. if (ret) {
  737. dev_err(dev, "disabling timers during suspend failed %d\n",
  738. ret);
  739. goto enable_device;
  740. }
  741. /*
  742. * IOMMUs would have to be disabled specifically for runtime suspend.
  743. * They are handled automatically through System PM callbacks for
  744. * regular system suspend
  745. */
  746. if (auto_suspend) {
  747. ret = omap_iommu_domain_deactivate(rproc->domain);
  748. if (ret) {
  749. dev_err(dev, "iommu domain deactivate failed %d\n",
  750. ret);
  751. goto enable_timers;
  752. }
  753. }
  754. return 0;
  755. enable_timers:
  756. /* ignore errors on re-enabling code */
  757. omap_rproc_enable_timers(rproc, false);
  758. enable_device:
  759. reset_control_deassert(oproc->reset);
  760. return ret;
  761. }
  762. static int _omap_rproc_resume(struct rproc *rproc, bool auto_suspend)
  763. {
  764. struct device *dev = rproc->dev.parent;
  765. struct omap_rproc *oproc = rproc->priv;
  766. int ret;
  767. /*
  768. * IOMMUs would have to be enabled specifically for runtime resume.
  769. * They would have been already enabled automatically through System
  770. * PM callbacks for regular system resume
  771. */
  772. if (auto_suspend) {
  773. ret = omap_iommu_domain_activate(rproc->domain);
  774. if (ret) {
  775. dev_err(dev, "omap_iommu activate failed %d\n", ret);
  776. goto out;
  777. }
  778. }
  779. /* boot address could be lost after suspend, so restore it */
  780. if (oproc->boot_data) {
  781. ret = omap_rproc_write_dsp_boot_addr(rproc);
  782. if (ret) {
  783. dev_err(dev, "boot address restore failed %d\n", ret);
  784. goto suspend_iommu;
  785. }
  786. }
  787. ret = omap_rproc_enable_timers(rproc, false);
  788. if (ret) {
  789. dev_err(dev, "enabling timers during resume failed %d\n", ret);
  790. goto suspend_iommu;
  791. }
  792. ret = reset_control_deassert(oproc->reset);
  793. if (ret) {
  794. dev_err(dev, "reset deassert during resume failed %d\n", ret);
  795. goto disable_timers;
  796. }
  797. return 0;
  798. disable_timers:
  799. omap_rproc_disable_timers(rproc, false);
  800. suspend_iommu:
  801. if (auto_suspend)
  802. omap_iommu_domain_deactivate(rproc->domain);
  803. out:
  804. return ret;
  805. }
  806. static int __maybe_unused omap_rproc_suspend(struct device *dev)
  807. {
  808. struct platform_device *pdev = to_platform_device(dev);
  809. struct rproc *rproc = platform_get_drvdata(pdev);
  810. struct omap_rproc *oproc = rproc->priv;
  811. int ret = 0;
  812. mutex_lock(&rproc->lock);
  813. if (rproc->state == RPROC_OFFLINE)
  814. goto out;
  815. if (rproc->state == RPROC_SUSPENDED)
  816. goto out;
  817. if (rproc->state != RPROC_RUNNING) {
  818. ret = -EBUSY;
  819. goto out;
  820. }
  821. ret = _omap_rproc_suspend(rproc, false);
  822. if (ret) {
  823. dev_err(dev, "suspend failed %d\n", ret);
  824. goto out;
  825. }
  826. /*
  827. * remoteproc is running at the time of system suspend, so remember
  828. * it so as to wake it up during system resume
  829. */
  830. oproc->need_resume = true;
  831. rproc->state = RPROC_SUSPENDED;
  832. out:
  833. mutex_unlock(&rproc->lock);
  834. return ret;
  835. }
  836. static int __maybe_unused omap_rproc_resume(struct device *dev)
  837. {
  838. struct platform_device *pdev = to_platform_device(dev);
  839. struct rproc *rproc = platform_get_drvdata(pdev);
  840. struct omap_rproc *oproc = rproc->priv;
  841. int ret = 0;
  842. mutex_lock(&rproc->lock);
  843. if (rproc->state == RPROC_OFFLINE)
  844. goto out;
  845. if (rproc->state != RPROC_SUSPENDED) {
  846. ret = -EBUSY;
  847. goto out;
  848. }
  849. /*
  850. * remoteproc was auto-suspended at the time of system suspend,
  851. * so no need to wake-up the processor (leave it in suspended
  852. * state, will be woken up during a subsequent runtime_resume)
  853. */
  854. if (!oproc->need_resume)
  855. goto out;
  856. ret = _omap_rproc_resume(rproc, false);
  857. if (ret) {
  858. dev_err(dev, "resume failed %d\n", ret);
  859. goto out;
  860. }
  861. oproc->need_resume = false;
  862. rproc->state = RPROC_RUNNING;
  863. pm_runtime_mark_last_busy(dev);
  864. out:
  865. mutex_unlock(&rproc->lock);
  866. return ret;
  867. }
  868. static int omap_rproc_runtime_suspend(struct device *dev)
  869. {
  870. struct rproc *rproc = dev_get_drvdata(dev);
  871. struct omap_rproc *oproc = rproc->priv;
  872. int ret;
  873. mutex_lock(&rproc->lock);
  874. if (rproc->state == RPROC_CRASHED) {
  875. dev_dbg(dev, "rproc cannot be runtime suspended when crashed!\n");
  876. ret = -EBUSY;
  877. goto out;
  878. }
  879. if (WARN_ON(rproc->state != RPROC_RUNNING)) {
  880. dev_err(dev, "rproc cannot be runtime suspended when not running!\n");
  881. ret = -EBUSY;
  882. goto out;
  883. }
  884. /*
  885. * do not even attempt suspend if the remote processor is not
  886. * idled for runtime auto-suspend
  887. */
  888. if (!_is_rproc_in_standby(oproc)) {
  889. ret = -EBUSY;
  890. goto abort;
  891. }
  892. ret = _omap_rproc_suspend(rproc, true);
  893. if (ret)
  894. goto abort;
  895. rproc->state = RPROC_SUSPENDED;
  896. mutex_unlock(&rproc->lock);
  897. return 0;
  898. abort:
  899. pm_runtime_mark_last_busy(dev);
  900. out:
  901. mutex_unlock(&rproc->lock);
  902. return ret;
  903. }
  904. static int omap_rproc_runtime_resume(struct device *dev)
  905. {
  906. struct rproc *rproc = dev_get_drvdata(dev);
  907. int ret;
  908. mutex_lock(&rproc->lock);
  909. if (WARN_ON(rproc->state != RPROC_SUSPENDED)) {
  910. dev_err(dev, "rproc cannot be runtime resumed if not suspended! state=%d\n",
  911. rproc->state);
  912. ret = -EBUSY;
  913. goto out;
  914. }
  915. ret = _omap_rproc_resume(rproc, true);
  916. if (ret) {
  917. dev_err(dev, "runtime resume failed %d\n", ret);
  918. goto out;
  919. }
  920. rproc->state = RPROC_RUNNING;
  921. out:
  922. mutex_unlock(&rproc->lock);
  923. return ret;
  924. }
  925. #endif /* CONFIG_PM */
  926. static const struct omap_rproc_mem_data ipu_mems[] = {
  927. { .name = "l2ram", .dev_addr = 0x20000000 },
  928. { },
  929. };
  930. static const struct omap_rproc_mem_data dra7_dsp_mems[] = {
  931. { .name = "l2ram", .dev_addr = 0x800000 },
  932. { .name = "l1pram", .dev_addr = 0xe00000 },
  933. { .name = "l1dram", .dev_addr = 0xf00000 },
  934. { },
  935. };
  936. static const struct omap_rproc_dev_data omap4_dsp_dev_data = {
  937. .device_name = "dsp",
  938. };
  939. static const struct omap_rproc_dev_data omap4_ipu_dev_data = {
  940. .device_name = "ipu",
  941. .mems = ipu_mems,
  942. };
  943. static const struct omap_rproc_dev_data omap5_dsp_dev_data = {
  944. .device_name = "dsp",
  945. };
  946. static const struct omap_rproc_dev_data omap5_ipu_dev_data = {
  947. .device_name = "ipu",
  948. .mems = ipu_mems,
  949. };
  950. static const struct omap_rproc_dev_data dra7_dsp_dev_data = {
  951. .device_name = "dsp",
  952. .mems = dra7_dsp_mems,
  953. };
  954. static const struct omap_rproc_dev_data dra7_ipu_dev_data = {
  955. .device_name = "ipu",
  956. .mems = ipu_mems,
  957. };
  958. static const struct of_device_id omap_rproc_of_match[] = {
  959. {
  960. .compatible = "ti,omap4-dsp",
  961. .data = &omap4_dsp_dev_data,
  962. },
  963. {
  964. .compatible = "ti,omap4-ipu",
  965. .data = &omap4_ipu_dev_data,
  966. },
  967. {
  968. .compatible = "ti,omap5-dsp",
  969. .data = &omap5_dsp_dev_data,
  970. },
  971. {
  972. .compatible = "ti,omap5-ipu",
  973. .data = &omap5_ipu_dev_data,
  974. },
  975. {
  976. .compatible = "ti,dra7-dsp",
  977. .data = &dra7_dsp_dev_data,
  978. },
  979. {
  980. .compatible = "ti,dra7-ipu",
  981. .data = &dra7_ipu_dev_data,
  982. },
  983. {
  984. /* end */
  985. },
  986. };
  987. MODULE_DEVICE_TABLE(of, omap_rproc_of_match);
  988. static const char *omap_rproc_get_firmware(struct platform_device *pdev)
  989. {
  990. const char *fw_name;
  991. int ret;
  992. ret = of_property_read_string(pdev->dev.of_node, "firmware-name",
  993. &fw_name);
  994. if (ret)
  995. return ERR_PTR(ret);
  996. return fw_name;
  997. }
  998. static int omap_rproc_get_boot_data(struct platform_device *pdev,
  999. struct rproc *rproc)
  1000. {
  1001. struct device_node *np = pdev->dev.of_node;
  1002. struct omap_rproc *oproc = rproc->priv;
  1003. const struct omap_rproc_dev_data *data;
  1004. int ret;
  1005. data = of_device_get_match_data(&pdev->dev);
  1006. if (!data)
  1007. return -ENODEV;
  1008. if (!of_property_read_bool(np, "ti,bootreg"))
  1009. return 0;
  1010. oproc->boot_data = devm_kzalloc(&pdev->dev, sizeof(*oproc->boot_data),
  1011. GFP_KERNEL);
  1012. if (!oproc->boot_data)
  1013. return -ENOMEM;
  1014. oproc->boot_data->syscon =
  1015. syscon_regmap_lookup_by_phandle(np, "ti,bootreg");
  1016. if (IS_ERR(oproc->boot_data->syscon)) {
  1017. ret = PTR_ERR(oproc->boot_data->syscon);
  1018. return ret;
  1019. }
  1020. if (of_property_read_u32_index(np, "ti,bootreg", 1,
  1021. &oproc->boot_data->boot_reg)) {
  1022. dev_err(&pdev->dev, "couldn't get the boot register\n");
  1023. return -EINVAL;
  1024. }
  1025. of_property_read_u32_index(np, "ti,bootreg", 2,
  1026. &oproc->boot_data->boot_reg_shift);
  1027. return 0;
  1028. }
  1029. static int omap_rproc_of_get_internal_memories(struct platform_device *pdev,
  1030. struct rproc *rproc)
  1031. {
  1032. struct omap_rproc *oproc = rproc->priv;
  1033. struct device *dev = &pdev->dev;
  1034. const struct omap_rproc_dev_data *data;
  1035. struct resource *res;
  1036. int num_mems;
  1037. int i;
  1038. data = of_device_get_match_data(dev);
  1039. if (!data)
  1040. return -ENODEV;
  1041. if (!data->mems)
  1042. return 0;
  1043. num_mems = of_property_count_elems_of_size(dev->of_node, "reg",
  1044. sizeof(u32)) / 2;
  1045. oproc->mem = devm_kcalloc(dev, num_mems, sizeof(*oproc->mem),
  1046. GFP_KERNEL);
  1047. if (!oproc->mem)
  1048. return -ENOMEM;
  1049. for (i = 0; data->mems[i].name; i++) {
  1050. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1051. data->mems[i].name);
  1052. if (!res) {
  1053. dev_err(dev, "no memory defined for %s\n",
  1054. data->mems[i].name);
  1055. return -ENOMEM;
  1056. }
  1057. oproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
  1058. if (IS_ERR(oproc->mem[i].cpu_addr)) {
  1059. dev_err(dev, "failed to parse and map %s memory\n",
  1060. data->mems[i].name);
  1061. return PTR_ERR(oproc->mem[i].cpu_addr);
  1062. }
  1063. oproc->mem[i].bus_addr = res->start;
  1064. oproc->mem[i].dev_addr = data->mems[i].dev_addr;
  1065. oproc->mem[i].size = resource_size(res);
  1066. dev_dbg(dev, "memory %8s: bus addr %pa size 0x%x va %pK da 0x%x\n",
  1067. data->mems[i].name, &oproc->mem[i].bus_addr,
  1068. oproc->mem[i].size, oproc->mem[i].cpu_addr,
  1069. oproc->mem[i].dev_addr);
  1070. }
  1071. oproc->num_mems = num_mems;
  1072. return 0;
  1073. }
  1074. #ifdef CONFIG_OMAP_REMOTEPROC_WATCHDOG
  1075. static int omap_rproc_count_wdog_timers(struct device *dev)
  1076. {
  1077. struct device_node *np = dev->of_node;
  1078. int ret;
  1079. ret = of_count_phandle_with_args(np, "ti,watchdog-timers", NULL);
  1080. if (ret <= 0) {
  1081. dev_dbg(dev, "device does not have watchdog timers, status = %d\n",
  1082. ret);
  1083. ret = 0;
  1084. }
  1085. return ret;
  1086. }
  1087. #else
  1088. static int omap_rproc_count_wdog_timers(struct device *dev)
  1089. {
  1090. return 0;
  1091. }
  1092. #endif
  1093. static int omap_rproc_of_get_timers(struct platform_device *pdev,
  1094. struct rproc *rproc)
  1095. {
  1096. struct device_node *np = pdev->dev.of_node;
  1097. struct omap_rproc *oproc = rproc->priv;
  1098. struct device *dev = &pdev->dev;
  1099. int num_timers;
  1100. /*
  1101. * Timer nodes are directly used in client nodes as phandles, so
  1102. * retrieve the count using appropriate size
  1103. */
  1104. oproc->num_timers = of_count_phandle_with_args(np, "ti,timers", NULL);
  1105. if (oproc->num_timers <= 0) {
  1106. dev_dbg(dev, "device does not have timers, status = %d\n",
  1107. oproc->num_timers);
  1108. oproc->num_timers = 0;
  1109. }
  1110. oproc->num_wd_timers = omap_rproc_count_wdog_timers(dev);
  1111. num_timers = oproc->num_timers + oproc->num_wd_timers;
  1112. if (num_timers) {
  1113. oproc->timers = devm_kcalloc(dev, num_timers,
  1114. sizeof(*oproc->timers),
  1115. GFP_KERNEL);
  1116. if (!oproc->timers)
  1117. return -ENOMEM;
  1118. dev_dbg(dev, "device has %d tick timers and %d watchdog timers\n",
  1119. oproc->num_timers, oproc->num_wd_timers);
  1120. }
  1121. return 0;
  1122. }
  1123. static int omap_rproc_probe(struct platform_device *pdev)
  1124. {
  1125. struct device_node *np = pdev->dev.of_node;
  1126. struct omap_rproc *oproc;
  1127. struct rproc *rproc;
  1128. const char *firmware;
  1129. int ret;
  1130. struct reset_control *reset;
  1131. if (!np) {
  1132. dev_err(&pdev->dev, "only DT-based devices are supported\n");
  1133. return -ENODEV;
  1134. }
  1135. reset = devm_reset_control_array_get_exclusive(&pdev->dev);
  1136. if (IS_ERR(reset))
  1137. return PTR_ERR(reset);
  1138. firmware = omap_rproc_get_firmware(pdev);
  1139. if (IS_ERR(firmware))
  1140. return PTR_ERR(firmware);
  1141. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  1142. if (ret) {
  1143. dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
  1144. return ret;
  1145. }
  1146. rproc = rproc_alloc(&pdev->dev, dev_name(&pdev->dev), &omap_rproc_ops,
  1147. firmware, sizeof(*oproc));
  1148. if (!rproc)
  1149. return -ENOMEM;
  1150. oproc = rproc->priv;
  1151. oproc->rproc = rproc;
  1152. oproc->reset = reset;
  1153. /* All existing OMAP IPU and DSP processors have an MMU */
  1154. rproc->has_iommu = true;
  1155. ret = omap_rproc_of_get_internal_memories(pdev, rproc);
  1156. if (ret)
  1157. goto free_rproc;
  1158. ret = omap_rproc_get_boot_data(pdev, rproc);
  1159. if (ret)
  1160. goto free_rproc;
  1161. ret = omap_rproc_of_get_timers(pdev, rproc);
  1162. if (ret)
  1163. goto free_rproc;
  1164. init_completion(&oproc->pm_comp);
  1165. oproc->autosuspend_delay = DEFAULT_AUTOSUSPEND_DELAY;
  1166. of_property_read_u32(pdev->dev.of_node, "ti,autosuspend-delay-ms",
  1167. &oproc->autosuspend_delay);
  1168. pm_runtime_set_autosuspend_delay(&pdev->dev, oproc->autosuspend_delay);
  1169. oproc->fck = devm_clk_get(&pdev->dev, 0);
  1170. if (IS_ERR(oproc->fck)) {
  1171. ret = PTR_ERR(oproc->fck);
  1172. goto free_rproc;
  1173. }
  1174. ret = of_reserved_mem_device_init(&pdev->dev);
  1175. if (ret) {
  1176. dev_warn(&pdev->dev, "device does not have specific CMA pool.\n");
  1177. dev_warn(&pdev->dev, "Typically this should be provided,\n");
  1178. dev_warn(&pdev->dev, "only omit if you know what you are doing.\n");
  1179. }
  1180. platform_set_drvdata(pdev, rproc);
  1181. ret = rproc_add(rproc);
  1182. if (ret)
  1183. goto release_mem;
  1184. return 0;
  1185. release_mem:
  1186. of_reserved_mem_device_release(&pdev->dev);
  1187. free_rproc:
  1188. rproc_free(rproc);
  1189. return ret;
  1190. }
  1191. static int omap_rproc_remove(struct platform_device *pdev)
  1192. {
  1193. struct rproc *rproc = platform_get_drvdata(pdev);
  1194. rproc_del(rproc);
  1195. rproc_free(rproc);
  1196. of_reserved_mem_device_release(&pdev->dev);
  1197. return 0;
  1198. }
  1199. static const struct dev_pm_ops omap_rproc_pm_ops = {
  1200. SET_SYSTEM_SLEEP_PM_OPS(omap_rproc_suspend, omap_rproc_resume)
  1201. SET_RUNTIME_PM_OPS(omap_rproc_runtime_suspend,
  1202. omap_rproc_runtime_resume, NULL)
  1203. };
  1204. static struct platform_driver omap_rproc_driver = {
  1205. .probe = omap_rproc_probe,
  1206. .remove = omap_rproc_remove,
  1207. .driver = {
  1208. .name = "omap-rproc",
  1209. .pm = &omap_rproc_pm_ops,
  1210. .of_match_table = omap_rproc_of_match,
  1211. },
  1212. };
  1213. module_platform_driver(omap_rproc_driver);
  1214. MODULE_LICENSE("GPL v2");
  1215. MODULE_DESCRIPTION("OMAP Remote Processor control driver");