fsl-mc-bus.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Freescale Management Complex (MC) bus driver
  4. *
  5. * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
  6. * Copyright 2019-2020 NXP
  7. * Author: German Rivera <German.Rivera@freescale.com>
  8. *
  9. */
  10. #define pr_fmt(fmt) "fsl-mc: " fmt
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_address.h>
  14. #include <linux/ioport.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/limits.h>
  18. #include <linux/bitops.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/acpi.h>
  21. #include <linux/iommu.h>
  22. #include <linux/dma-map-ops.h>
  23. #include "fsl-mc-private.h"
  24. /*
  25. * Default DMA mask for devices on a fsl-mc bus
  26. */
  27. #define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
  28. static struct fsl_mc_version mc_version;
  29. /**
  30. * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
  31. * @root_mc_bus_dev: fsl-mc device representing the root DPRC
  32. * @num_translation_ranges: number of entries in addr_translation_ranges
  33. * @translation_ranges: array of bus to system address translation ranges
  34. * @fsl_mc_regs: base address of register bank
  35. */
  36. struct fsl_mc {
  37. struct fsl_mc_device *root_mc_bus_dev;
  38. u8 num_translation_ranges;
  39. struct fsl_mc_addr_translation_range *translation_ranges;
  40. void __iomem *fsl_mc_regs;
  41. };
  42. /**
  43. * struct fsl_mc_addr_translation_range - bus to system address translation
  44. * range
  45. * @mc_region_type: Type of MC region for the range being translated
  46. * @start_mc_offset: Start MC offset of the range being translated
  47. * @end_mc_offset: MC offset of the first byte after the range (last MC
  48. * offset of the range is end_mc_offset - 1)
  49. * @start_phys_addr: system physical address corresponding to start_mc_addr
  50. */
  51. struct fsl_mc_addr_translation_range {
  52. enum dprc_region_type mc_region_type;
  53. u64 start_mc_offset;
  54. u64 end_mc_offset;
  55. phys_addr_t start_phys_addr;
  56. };
  57. #define FSL_MC_GCR1 0x0
  58. #define GCR1_P1_STOP BIT(31)
  59. #define GCR1_P2_STOP BIT(30)
  60. #define FSL_MC_FAPR 0x28
  61. #define MC_FAPR_PL BIT(18)
  62. #define MC_FAPR_BMT BIT(17)
  63. static phys_addr_t mc_portal_base_phys_addr;
  64. /**
  65. * fsl_mc_bus_match - device to driver matching callback
  66. * @dev: the fsl-mc device to match against
  67. * @drv: the device driver to search for matching fsl-mc object type
  68. * structures
  69. *
  70. * Returns 1 on success, 0 otherwise.
  71. */
  72. static int fsl_mc_bus_match(struct device *dev, const struct device_driver *drv)
  73. {
  74. const struct fsl_mc_device_id *id;
  75. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  76. const struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
  77. bool found = false;
  78. /* When driver_override is set, only bind to the matching driver */
  79. if (mc_dev->driver_override) {
  80. found = !strcmp(mc_dev->driver_override, mc_drv->driver.name);
  81. goto out;
  82. }
  83. if (!mc_drv->match_id_table)
  84. goto out;
  85. /*
  86. * If the object is not 'plugged' don't match.
  87. * Only exception is the root DPRC, which is a special case.
  88. */
  89. if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
  90. !fsl_mc_is_root_dprc(&mc_dev->dev))
  91. goto out;
  92. /*
  93. * Traverse the match_id table of the given driver, trying to find
  94. * a matching for the given device.
  95. */
  96. for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
  97. if (id->vendor == mc_dev->obj_desc.vendor &&
  98. strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
  99. found = true;
  100. break;
  101. }
  102. }
  103. out:
  104. dev_dbg(dev, "%smatched\n", found ? "" : "not ");
  105. return found;
  106. }
  107. /*
  108. * fsl_mc_bus_uevent - callback invoked when a device is added
  109. */
  110. static int fsl_mc_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
  111. {
  112. const struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  113. if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
  114. mc_dev->obj_desc.vendor,
  115. mc_dev->obj_desc.type))
  116. return -ENOMEM;
  117. return 0;
  118. }
  119. static int fsl_mc_dma_configure(struct device *dev)
  120. {
  121. struct device *dma_dev = dev;
  122. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  123. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
  124. u32 input_id = mc_dev->icid;
  125. int ret;
  126. while (dev_is_fsl_mc(dma_dev))
  127. dma_dev = dma_dev->parent;
  128. if (dev_of_node(dma_dev))
  129. ret = of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
  130. else
  131. ret = acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
  132. if (!ret && !mc_drv->driver_managed_dma) {
  133. ret = iommu_device_use_default_domain(dev);
  134. if (ret)
  135. arch_teardown_dma_ops(dev);
  136. }
  137. return ret;
  138. }
  139. static void fsl_mc_dma_cleanup(struct device *dev)
  140. {
  141. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
  142. if (!mc_drv->driver_managed_dma)
  143. iommu_device_unuse_default_domain(dev);
  144. }
  145. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
  146. char *buf)
  147. {
  148. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  149. return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
  150. mc_dev->obj_desc.type);
  151. }
  152. static DEVICE_ATTR_RO(modalias);
  153. static ssize_t driver_override_store(struct device *dev,
  154. struct device_attribute *attr,
  155. const char *buf, size_t count)
  156. {
  157. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  158. int ret;
  159. if (WARN_ON(dev->bus != &fsl_mc_bus_type))
  160. return -EINVAL;
  161. ret = driver_set_override(dev, &mc_dev->driver_override, buf, count);
  162. if (ret)
  163. return ret;
  164. return count;
  165. }
  166. static ssize_t driver_override_show(struct device *dev,
  167. struct device_attribute *attr, char *buf)
  168. {
  169. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  170. return snprintf(buf, PAGE_SIZE, "%s\n", mc_dev->driver_override);
  171. }
  172. static DEVICE_ATTR_RW(driver_override);
  173. static struct attribute *fsl_mc_dev_attrs[] = {
  174. &dev_attr_modalias.attr,
  175. &dev_attr_driver_override.attr,
  176. NULL,
  177. };
  178. ATTRIBUTE_GROUPS(fsl_mc_dev);
  179. static int scan_fsl_mc_bus(struct device *dev, void *data)
  180. {
  181. struct fsl_mc_device *root_mc_dev;
  182. struct fsl_mc_bus *root_mc_bus;
  183. if (!fsl_mc_is_root_dprc(dev))
  184. goto exit;
  185. root_mc_dev = to_fsl_mc_device(dev);
  186. root_mc_bus = to_fsl_mc_bus(root_mc_dev);
  187. mutex_lock(&root_mc_bus->scan_mutex);
  188. dprc_scan_objects(root_mc_dev, false);
  189. mutex_unlock(&root_mc_bus->scan_mutex);
  190. exit:
  191. return 0;
  192. }
  193. static ssize_t rescan_store(const struct bus_type *bus,
  194. const char *buf, size_t count)
  195. {
  196. unsigned long val;
  197. if (kstrtoul(buf, 0, &val) < 0)
  198. return -EINVAL;
  199. if (val)
  200. bus_for_each_dev(bus, NULL, NULL, scan_fsl_mc_bus);
  201. return count;
  202. }
  203. static BUS_ATTR_WO(rescan);
  204. static int fsl_mc_bus_set_autorescan(struct device *dev, void *data)
  205. {
  206. struct fsl_mc_device *root_mc_dev;
  207. unsigned long val;
  208. char *buf = data;
  209. if (!fsl_mc_is_root_dprc(dev))
  210. goto exit;
  211. root_mc_dev = to_fsl_mc_device(dev);
  212. if (kstrtoul(buf, 0, &val) < 0)
  213. return -EINVAL;
  214. if (val)
  215. enable_dprc_irq(root_mc_dev);
  216. else
  217. disable_dprc_irq(root_mc_dev);
  218. exit:
  219. return 0;
  220. }
  221. static int fsl_mc_bus_get_autorescan(struct device *dev, void *data)
  222. {
  223. struct fsl_mc_device *root_mc_dev;
  224. char *buf = data;
  225. if (!fsl_mc_is_root_dprc(dev))
  226. goto exit;
  227. root_mc_dev = to_fsl_mc_device(dev);
  228. sprintf(buf, "%d\n", get_dprc_irq_state(root_mc_dev));
  229. exit:
  230. return 0;
  231. }
  232. static ssize_t autorescan_store(const struct bus_type *bus,
  233. const char *buf, size_t count)
  234. {
  235. bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_set_autorescan);
  236. return count;
  237. }
  238. static ssize_t autorescan_show(const struct bus_type *bus, char *buf)
  239. {
  240. bus_for_each_dev(bus, NULL, (void *)buf, fsl_mc_bus_get_autorescan);
  241. return strlen(buf);
  242. }
  243. static BUS_ATTR_RW(autorescan);
  244. static struct attribute *fsl_mc_bus_attrs[] = {
  245. &bus_attr_rescan.attr,
  246. &bus_attr_autorescan.attr,
  247. NULL,
  248. };
  249. ATTRIBUTE_GROUPS(fsl_mc_bus);
  250. const struct bus_type fsl_mc_bus_type = {
  251. .name = "fsl-mc",
  252. .match = fsl_mc_bus_match,
  253. .uevent = fsl_mc_bus_uevent,
  254. .dma_configure = fsl_mc_dma_configure,
  255. .dma_cleanup = fsl_mc_dma_cleanup,
  256. .dev_groups = fsl_mc_dev_groups,
  257. .bus_groups = fsl_mc_bus_groups,
  258. };
  259. EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
  260. struct device_type fsl_mc_bus_dprc_type = {
  261. .name = "fsl_mc_bus_dprc"
  262. };
  263. EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
  264. struct device_type fsl_mc_bus_dpni_type = {
  265. .name = "fsl_mc_bus_dpni"
  266. };
  267. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
  268. struct device_type fsl_mc_bus_dpio_type = {
  269. .name = "fsl_mc_bus_dpio"
  270. };
  271. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
  272. struct device_type fsl_mc_bus_dpsw_type = {
  273. .name = "fsl_mc_bus_dpsw"
  274. };
  275. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
  276. struct device_type fsl_mc_bus_dpbp_type = {
  277. .name = "fsl_mc_bus_dpbp"
  278. };
  279. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
  280. struct device_type fsl_mc_bus_dpcon_type = {
  281. .name = "fsl_mc_bus_dpcon"
  282. };
  283. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
  284. struct device_type fsl_mc_bus_dpmcp_type = {
  285. .name = "fsl_mc_bus_dpmcp"
  286. };
  287. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
  288. struct device_type fsl_mc_bus_dpmac_type = {
  289. .name = "fsl_mc_bus_dpmac"
  290. };
  291. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
  292. struct device_type fsl_mc_bus_dprtc_type = {
  293. .name = "fsl_mc_bus_dprtc"
  294. };
  295. EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
  296. struct device_type fsl_mc_bus_dpseci_type = {
  297. .name = "fsl_mc_bus_dpseci"
  298. };
  299. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
  300. struct device_type fsl_mc_bus_dpdmux_type = {
  301. .name = "fsl_mc_bus_dpdmux"
  302. };
  303. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
  304. struct device_type fsl_mc_bus_dpdcei_type = {
  305. .name = "fsl_mc_bus_dpdcei"
  306. };
  307. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
  308. struct device_type fsl_mc_bus_dpaiop_type = {
  309. .name = "fsl_mc_bus_dpaiop"
  310. };
  311. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
  312. struct device_type fsl_mc_bus_dpci_type = {
  313. .name = "fsl_mc_bus_dpci"
  314. };
  315. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
  316. struct device_type fsl_mc_bus_dpdmai_type = {
  317. .name = "fsl_mc_bus_dpdmai"
  318. };
  319. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
  320. struct device_type fsl_mc_bus_dpdbg_type = {
  321. .name = "fsl_mc_bus_dpdbg"
  322. };
  323. EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdbg_type);
  324. static struct device_type *fsl_mc_get_device_type(const char *type)
  325. {
  326. static const struct {
  327. struct device_type *dev_type;
  328. const char *type;
  329. } dev_types[] = {
  330. { &fsl_mc_bus_dprc_type, "dprc" },
  331. { &fsl_mc_bus_dpni_type, "dpni" },
  332. { &fsl_mc_bus_dpio_type, "dpio" },
  333. { &fsl_mc_bus_dpsw_type, "dpsw" },
  334. { &fsl_mc_bus_dpbp_type, "dpbp" },
  335. { &fsl_mc_bus_dpcon_type, "dpcon" },
  336. { &fsl_mc_bus_dpmcp_type, "dpmcp" },
  337. { &fsl_mc_bus_dpmac_type, "dpmac" },
  338. { &fsl_mc_bus_dprtc_type, "dprtc" },
  339. { &fsl_mc_bus_dpseci_type, "dpseci" },
  340. { &fsl_mc_bus_dpdmux_type, "dpdmux" },
  341. { &fsl_mc_bus_dpdcei_type, "dpdcei" },
  342. { &fsl_mc_bus_dpaiop_type, "dpaiop" },
  343. { &fsl_mc_bus_dpci_type, "dpci" },
  344. { &fsl_mc_bus_dpdmai_type, "dpdmai" },
  345. { &fsl_mc_bus_dpdbg_type, "dpdbg" },
  346. { NULL, NULL }
  347. };
  348. int i;
  349. for (i = 0; dev_types[i].dev_type; i++)
  350. if (!strcmp(dev_types[i].type, type))
  351. return dev_types[i].dev_type;
  352. return NULL;
  353. }
  354. static int fsl_mc_driver_probe(struct device *dev)
  355. {
  356. struct fsl_mc_driver *mc_drv;
  357. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  358. int error;
  359. mc_drv = to_fsl_mc_driver(dev->driver);
  360. error = mc_drv->probe(mc_dev);
  361. if (error < 0) {
  362. if (error != -EPROBE_DEFER)
  363. dev_err(dev, "%s failed: %d\n", __func__, error);
  364. return error;
  365. }
  366. return 0;
  367. }
  368. static int fsl_mc_driver_remove(struct device *dev)
  369. {
  370. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
  371. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  372. mc_drv->remove(mc_dev);
  373. return 0;
  374. }
  375. static void fsl_mc_driver_shutdown(struct device *dev)
  376. {
  377. struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
  378. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  379. mc_drv->shutdown(mc_dev);
  380. }
  381. /*
  382. * __fsl_mc_driver_register - registers a child device driver with the
  383. * MC bus
  384. *
  385. * This function is implicitly invoked from the registration function of
  386. * fsl_mc device drivers, which is generated by the
  387. * module_fsl_mc_driver() macro.
  388. */
  389. int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
  390. struct module *owner)
  391. {
  392. int error;
  393. mc_driver->driver.owner = owner;
  394. mc_driver->driver.bus = &fsl_mc_bus_type;
  395. if (mc_driver->probe)
  396. mc_driver->driver.probe = fsl_mc_driver_probe;
  397. if (mc_driver->remove)
  398. mc_driver->driver.remove = fsl_mc_driver_remove;
  399. if (mc_driver->shutdown)
  400. mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
  401. error = driver_register(&mc_driver->driver);
  402. if (error < 0) {
  403. pr_err("driver_register() failed for %s: %d\n",
  404. mc_driver->driver.name, error);
  405. return error;
  406. }
  407. return 0;
  408. }
  409. EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
  410. /*
  411. * fsl_mc_driver_unregister - unregisters a device driver from the
  412. * MC bus
  413. */
  414. void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
  415. {
  416. driver_unregister(&mc_driver->driver);
  417. }
  418. EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
  419. /**
  420. * mc_get_version() - Retrieves the Management Complex firmware
  421. * version information
  422. * @mc_io: Pointer to opaque I/O object
  423. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  424. * @mc_ver_info: Returned version information structure
  425. *
  426. * Return: '0' on Success; Error code otherwise.
  427. */
  428. static int mc_get_version(struct fsl_mc_io *mc_io,
  429. u32 cmd_flags,
  430. struct fsl_mc_version *mc_ver_info)
  431. {
  432. struct fsl_mc_command cmd = { 0 };
  433. struct dpmng_rsp_get_version *rsp_params;
  434. int err;
  435. /* prepare command */
  436. cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
  437. cmd_flags,
  438. 0);
  439. /* send command to mc*/
  440. err = mc_send_command(mc_io, &cmd);
  441. if (err)
  442. return err;
  443. /* retrieve response parameters */
  444. rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
  445. mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
  446. mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
  447. mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
  448. return 0;
  449. }
  450. /**
  451. * fsl_mc_get_version - function to retrieve the MC f/w version information
  452. *
  453. * Return: mc version when called after fsl-mc-bus probe; NULL otherwise.
  454. */
  455. struct fsl_mc_version *fsl_mc_get_version(void)
  456. {
  457. if (mc_version.major)
  458. return &mc_version;
  459. return NULL;
  460. }
  461. EXPORT_SYMBOL_GPL(fsl_mc_get_version);
  462. /*
  463. * fsl_mc_get_root_dprc - function to traverse to the root dprc
  464. */
  465. void fsl_mc_get_root_dprc(struct device *dev,
  466. struct device **root_dprc_dev)
  467. {
  468. if (!dev) {
  469. *root_dprc_dev = NULL;
  470. } else if (!dev_is_fsl_mc(dev)) {
  471. *root_dprc_dev = NULL;
  472. } else {
  473. *root_dprc_dev = dev;
  474. while (dev_is_fsl_mc((*root_dprc_dev)->parent))
  475. *root_dprc_dev = (*root_dprc_dev)->parent;
  476. }
  477. }
  478. static int get_dprc_attr(struct fsl_mc_io *mc_io,
  479. int container_id, struct dprc_attributes *attr)
  480. {
  481. u16 dprc_handle;
  482. int error;
  483. error = dprc_open(mc_io, 0, container_id, &dprc_handle);
  484. if (error < 0) {
  485. dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
  486. return error;
  487. }
  488. memset(attr, 0, sizeof(struct dprc_attributes));
  489. error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
  490. if (error < 0) {
  491. dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
  492. error);
  493. goto common_cleanup;
  494. }
  495. error = 0;
  496. common_cleanup:
  497. (void)dprc_close(mc_io, 0, dprc_handle);
  498. return error;
  499. }
  500. static int get_dprc_icid(struct fsl_mc_io *mc_io,
  501. int container_id, u32 *icid)
  502. {
  503. struct dprc_attributes attr;
  504. int error;
  505. error = get_dprc_attr(mc_io, container_id, &attr);
  506. if (error == 0)
  507. *icid = attr.icid;
  508. return error;
  509. }
  510. static int translate_mc_addr(struct fsl_mc_device *mc_dev,
  511. enum dprc_region_type mc_region_type,
  512. u64 mc_offset, phys_addr_t *phys_addr)
  513. {
  514. int i;
  515. struct device *root_dprc_dev;
  516. struct fsl_mc *mc;
  517. fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
  518. mc = dev_get_drvdata(root_dprc_dev->parent);
  519. if (mc->num_translation_ranges == 0) {
  520. /*
  521. * Do identity mapping:
  522. */
  523. *phys_addr = mc_offset;
  524. return 0;
  525. }
  526. for (i = 0; i < mc->num_translation_ranges; i++) {
  527. struct fsl_mc_addr_translation_range *range =
  528. &mc->translation_ranges[i];
  529. if (mc_region_type == range->mc_region_type &&
  530. mc_offset >= range->start_mc_offset &&
  531. mc_offset < range->end_mc_offset) {
  532. *phys_addr = range->start_phys_addr +
  533. (mc_offset - range->start_mc_offset);
  534. return 0;
  535. }
  536. }
  537. return -EFAULT;
  538. }
  539. static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
  540. struct fsl_mc_device *mc_bus_dev)
  541. {
  542. int i;
  543. int error;
  544. struct resource *regions;
  545. struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
  546. struct device *parent_dev = mc_dev->dev.parent;
  547. enum dprc_region_type mc_region_type;
  548. if (is_fsl_mc_bus_dprc(mc_dev) ||
  549. is_fsl_mc_bus_dpmcp(mc_dev)) {
  550. mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
  551. } else if (is_fsl_mc_bus_dpio(mc_dev)) {
  552. mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
  553. } else {
  554. /*
  555. * This function should not have been called for this MC object
  556. * type, as this object type is not supposed to have MMIO
  557. * regions
  558. */
  559. return -EINVAL;
  560. }
  561. regions = kmalloc_array(obj_desc->region_count,
  562. sizeof(regions[0]), GFP_KERNEL);
  563. if (!regions)
  564. return -ENOMEM;
  565. for (i = 0; i < obj_desc->region_count; i++) {
  566. struct dprc_region_desc region_desc;
  567. error = dprc_get_obj_region(mc_bus_dev->mc_io,
  568. 0,
  569. mc_bus_dev->mc_handle,
  570. obj_desc->type,
  571. obj_desc->id, i, &region_desc);
  572. if (error < 0) {
  573. dev_err(parent_dev,
  574. "dprc_get_obj_region() failed: %d\n", error);
  575. goto error_cleanup_regions;
  576. }
  577. /*
  578. * Older MC only returned region offset and no base address
  579. * If base address is in the region_desc use it otherwise
  580. * revert to old mechanism
  581. */
  582. if (region_desc.base_address) {
  583. regions[i].start = region_desc.base_address +
  584. region_desc.base_offset;
  585. } else {
  586. error = translate_mc_addr(mc_dev, mc_region_type,
  587. region_desc.base_offset,
  588. &regions[i].start);
  589. /*
  590. * Some versions of the MC firmware wrongly report
  591. * 0 for register base address of the DPMCP associated
  592. * with child DPRC objects thus rendering them unusable.
  593. * This is particularly troublesome in ACPI boot
  594. * scenarios where the legacy way of extracting this
  595. * base address from the device tree does not apply.
  596. * Given that DPMCPs share the same base address,
  597. * workaround this by using the base address extracted
  598. * from the root DPRC container.
  599. */
  600. if (is_fsl_mc_bus_dprc(mc_dev) &&
  601. regions[i].start == region_desc.base_offset)
  602. regions[i].start += mc_portal_base_phys_addr;
  603. }
  604. if (error < 0) {
  605. dev_err(parent_dev,
  606. "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
  607. region_desc.base_offset,
  608. obj_desc->type, obj_desc->id, i);
  609. goto error_cleanup_regions;
  610. }
  611. regions[i].end = regions[i].start + region_desc.size - 1;
  612. regions[i].name = "fsl-mc object MMIO region";
  613. regions[i].flags = region_desc.flags & IORESOURCE_BITS;
  614. regions[i].flags |= IORESOURCE_MEM;
  615. }
  616. mc_dev->regions = regions;
  617. return 0;
  618. error_cleanup_regions:
  619. kfree(regions);
  620. return error;
  621. }
  622. /*
  623. * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
  624. */
  625. bool fsl_mc_is_root_dprc(struct device *dev)
  626. {
  627. struct device *root_dprc_dev;
  628. fsl_mc_get_root_dprc(dev, &root_dprc_dev);
  629. if (!root_dprc_dev)
  630. return false;
  631. return dev == root_dprc_dev;
  632. }
  633. static void fsl_mc_device_release(struct device *dev)
  634. {
  635. struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  636. kfree(mc_dev->regions);
  637. if (is_fsl_mc_bus_dprc(mc_dev))
  638. kfree(to_fsl_mc_bus(mc_dev));
  639. else
  640. kfree(mc_dev);
  641. }
  642. /*
  643. * Add a newly discovered fsl-mc device to be visible in Linux
  644. */
  645. int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
  646. struct fsl_mc_io *mc_io,
  647. struct device *parent_dev,
  648. struct fsl_mc_device **new_mc_dev)
  649. {
  650. int error;
  651. struct fsl_mc_device *mc_dev = NULL;
  652. struct fsl_mc_bus *mc_bus = NULL;
  653. struct fsl_mc_device *parent_mc_dev;
  654. if (dev_is_fsl_mc(parent_dev))
  655. parent_mc_dev = to_fsl_mc_device(parent_dev);
  656. else
  657. parent_mc_dev = NULL;
  658. if (strcmp(obj_desc->type, "dprc") == 0) {
  659. /*
  660. * Allocate an MC bus device object:
  661. */
  662. mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
  663. if (!mc_bus)
  664. return -ENOMEM;
  665. mutex_init(&mc_bus->scan_mutex);
  666. mc_dev = &mc_bus->mc_dev;
  667. } else {
  668. /*
  669. * Allocate a regular fsl_mc_device object:
  670. */
  671. mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
  672. if (!mc_dev)
  673. return -ENOMEM;
  674. }
  675. mc_dev->obj_desc = *obj_desc;
  676. mc_dev->mc_io = mc_io;
  677. device_initialize(&mc_dev->dev);
  678. mc_dev->dev.parent = parent_dev;
  679. mc_dev->dev.bus = &fsl_mc_bus_type;
  680. mc_dev->dev.release = fsl_mc_device_release;
  681. mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
  682. if (!mc_dev->dev.type) {
  683. error = -ENODEV;
  684. dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
  685. goto error_cleanup_dev;
  686. }
  687. dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
  688. if (strcmp(obj_desc->type, "dprc") == 0) {
  689. struct fsl_mc_io *mc_io2;
  690. mc_dev->flags |= FSL_MC_IS_DPRC;
  691. /*
  692. * To get the DPRC's ICID, we need to open the DPRC
  693. * in get_dprc_icid(). For child DPRCs, we do so using the
  694. * parent DPRC's MC portal instead of the child DPRC's MC
  695. * portal, in case the child DPRC is already opened with
  696. * its own portal (e.g., the DPRC used by AIOP).
  697. *
  698. * NOTE: There cannot be more than one active open for a
  699. * given MC object, using the same MC portal.
  700. */
  701. if (parent_mc_dev) {
  702. /*
  703. * device being added is a child DPRC device
  704. */
  705. mc_io2 = parent_mc_dev->mc_io;
  706. } else {
  707. /*
  708. * device being added is the root DPRC device
  709. */
  710. if (!mc_io) {
  711. error = -EINVAL;
  712. goto error_cleanup_dev;
  713. }
  714. mc_io2 = mc_io;
  715. }
  716. error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
  717. if (error < 0)
  718. goto error_cleanup_dev;
  719. } else {
  720. /*
  721. * A non-DPRC object has to be a child of a DPRC, use the
  722. * parent's ICID and interrupt domain.
  723. */
  724. mc_dev->icid = parent_mc_dev->icid;
  725. mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
  726. mc_dev->dev.dma_mask = &mc_dev->dma_mask;
  727. mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
  728. dev_set_msi_domain(&mc_dev->dev,
  729. dev_get_msi_domain(&parent_mc_dev->dev));
  730. }
  731. /*
  732. * Get MMIO regions for the device from the MC:
  733. *
  734. * NOTE: the root DPRC is a special case as its MMIO region is
  735. * obtained from the device tree
  736. */
  737. if (parent_mc_dev && obj_desc->region_count != 0) {
  738. error = fsl_mc_device_get_mmio_regions(mc_dev,
  739. parent_mc_dev);
  740. if (error < 0)
  741. goto error_cleanup_dev;
  742. }
  743. /*
  744. * The device-specific probe callback will get invoked by device_add()
  745. */
  746. error = device_add(&mc_dev->dev);
  747. if (error < 0) {
  748. dev_err(parent_dev,
  749. "device_add() failed for device %s: %d\n",
  750. dev_name(&mc_dev->dev), error);
  751. goto error_cleanup_dev;
  752. }
  753. dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
  754. *new_mc_dev = mc_dev;
  755. return 0;
  756. error_cleanup_dev:
  757. kfree(mc_dev->regions);
  758. kfree(mc_bus);
  759. kfree(mc_dev);
  760. return error;
  761. }
  762. EXPORT_SYMBOL_GPL(fsl_mc_device_add);
  763. static struct notifier_block fsl_mc_nb;
  764. /**
  765. * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
  766. * Linux
  767. *
  768. * @mc_dev: Pointer to an fsl-mc device
  769. */
  770. void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
  771. {
  772. kfree(mc_dev->driver_override);
  773. mc_dev->driver_override = NULL;
  774. /*
  775. * The device-specific remove callback will get invoked by device_del()
  776. */
  777. device_del(&mc_dev->dev);
  778. put_device(&mc_dev->dev);
  779. }
  780. EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
  781. struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev,
  782. u16 if_id)
  783. {
  784. struct fsl_mc_device *mc_bus_dev, *endpoint;
  785. struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
  786. struct dprc_endpoint endpoint1 = {{ 0 }};
  787. struct dprc_endpoint endpoint2 = {{ 0 }};
  788. int state, err;
  789. mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
  790. strcpy(endpoint1.type, mc_dev->obj_desc.type);
  791. endpoint1.id = mc_dev->obj_desc.id;
  792. endpoint1.if_id = if_id;
  793. err = dprc_get_connection(mc_bus_dev->mc_io, 0,
  794. mc_bus_dev->mc_handle,
  795. &endpoint1, &endpoint2,
  796. &state);
  797. if (err == -ENOTCONN || state == -1)
  798. return ERR_PTR(-ENOTCONN);
  799. if (err < 0) {
  800. dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
  801. return ERR_PTR(err);
  802. }
  803. strcpy(endpoint_desc.type, endpoint2.type);
  804. endpoint_desc.id = endpoint2.id;
  805. endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
  806. /*
  807. * We know that the device has an endpoint because we verified by
  808. * interrogating the firmware. This is the case when the device was not
  809. * yet discovered by the fsl-mc bus, thus the lookup returned NULL.
  810. * Force a rescan of the devices in this container and retry the lookup.
  811. */
  812. if (!endpoint) {
  813. struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
  814. if (mutex_trylock(&mc_bus->scan_mutex)) {
  815. err = dprc_scan_objects(mc_bus_dev, true);
  816. mutex_unlock(&mc_bus->scan_mutex);
  817. }
  818. if (err < 0)
  819. return ERR_PTR(err);
  820. }
  821. endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
  822. /*
  823. * This means that the endpoint might reside in a different isolation
  824. * context (DPRC/container). Not much to do, so return a permssion
  825. * error.
  826. */
  827. if (!endpoint)
  828. return ERR_PTR(-EPERM);
  829. return endpoint;
  830. }
  831. EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
  832. static int get_mc_addr_translation_ranges(struct device *dev,
  833. struct fsl_mc_addr_translation_range
  834. **ranges,
  835. u8 *num_ranges)
  836. {
  837. struct fsl_mc_addr_translation_range *r;
  838. struct of_range_parser parser;
  839. struct of_range range;
  840. of_range_parser_init(&parser, dev->of_node);
  841. *num_ranges = of_range_count(&parser);
  842. if (!*num_ranges) {
  843. /*
  844. * Missing or empty ranges property ("ranges;") for the
  845. * 'fsl,qoriq-mc' node. In this case, identity mapping
  846. * will be used.
  847. */
  848. *ranges = NULL;
  849. return 0;
  850. }
  851. *ranges = devm_kcalloc(dev, *num_ranges,
  852. sizeof(struct fsl_mc_addr_translation_range),
  853. GFP_KERNEL);
  854. if (!(*ranges))
  855. return -ENOMEM;
  856. r = *ranges;
  857. for_each_of_range(&parser, &range) {
  858. r->mc_region_type = range.flags;
  859. r->start_mc_offset = range.bus_addr;
  860. r->end_mc_offset = range.bus_addr + range.size;
  861. r->start_phys_addr = range.cpu_addr;
  862. r++;
  863. }
  864. return 0;
  865. }
  866. /*
  867. * fsl_mc_bus_probe - callback invoked when the root MC bus is being
  868. * added
  869. */
  870. static int fsl_mc_bus_probe(struct platform_device *pdev)
  871. {
  872. struct fsl_mc_obj_desc obj_desc;
  873. int error;
  874. struct fsl_mc *mc;
  875. struct fsl_mc_device *mc_bus_dev = NULL;
  876. struct fsl_mc_io *mc_io = NULL;
  877. int container_id;
  878. phys_addr_t mc_portal_phys_addr;
  879. u32 mc_portal_size, mc_stream_id;
  880. struct resource *plat_res;
  881. mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
  882. if (!mc)
  883. return -ENOMEM;
  884. platform_set_drvdata(pdev, mc);
  885. plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  886. if (plat_res) {
  887. mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
  888. if (IS_ERR(mc->fsl_mc_regs))
  889. return PTR_ERR(mc->fsl_mc_regs);
  890. }
  891. if (mc->fsl_mc_regs) {
  892. if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
  893. mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
  894. /*
  895. * HW ORs the PL and BMT bit, places the result in bit
  896. * 14 of the StreamID and ORs in the ICID. Calculate it
  897. * accordingly.
  898. */
  899. mc_stream_id = (mc_stream_id & 0xffff) |
  900. ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
  901. BIT(14) : 0);
  902. error = acpi_dma_configure_id(&pdev->dev,
  903. DEV_DMA_COHERENT,
  904. &mc_stream_id);
  905. if (error == -EPROBE_DEFER)
  906. return error;
  907. if (error)
  908. dev_warn(&pdev->dev,
  909. "failed to configure dma: %d.\n",
  910. error);
  911. }
  912. /*
  913. * Some bootloaders pause the MC firmware before booting the
  914. * kernel so that MC will not cause faults as soon as the
  915. * SMMU probes due to the fact that there's no configuration
  916. * in place for MC.
  917. * At this point MC should have all its SMMU setup done so make
  918. * sure it is resumed.
  919. */
  920. writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) &
  921. (~(GCR1_P1_STOP | GCR1_P2_STOP)),
  922. mc->fsl_mc_regs + FSL_MC_GCR1);
  923. }
  924. /*
  925. * Get physical address of MC portal for the root DPRC:
  926. */
  927. plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  928. mc_portal_phys_addr = plat_res->start;
  929. mc_portal_size = resource_size(plat_res);
  930. mc_portal_base_phys_addr = mc_portal_phys_addr & ~0x3ffffff;
  931. error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
  932. mc_portal_size, NULL,
  933. FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
  934. if (error < 0)
  935. return error;
  936. error = mc_get_version(mc_io, 0, &mc_version);
  937. if (error != 0) {
  938. dev_err(&pdev->dev,
  939. "mc_get_version() failed with error %d\n", error);
  940. goto error_cleanup_mc_io;
  941. }
  942. dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
  943. mc_version.major, mc_version.minor, mc_version.revision);
  944. if (dev_of_node(&pdev->dev)) {
  945. error = get_mc_addr_translation_ranges(&pdev->dev,
  946. &mc->translation_ranges,
  947. &mc->num_translation_ranges);
  948. if (error < 0)
  949. goto error_cleanup_mc_io;
  950. }
  951. error = dprc_get_container_id(mc_io, 0, &container_id);
  952. if (error < 0) {
  953. dev_err(&pdev->dev,
  954. "dprc_get_container_id() failed: %d\n", error);
  955. goto error_cleanup_mc_io;
  956. }
  957. memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
  958. error = dprc_get_api_version(mc_io, 0,
  959. &obj_desc.ver_major,
  960. &obj_desc.ver_minor);
  961. if (error < 0)
  962. goto error_cleanup_mc_io;
  963. obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
  964. strcpy(obj_desc.type, "dprc");
  965. obj_desc.id = container_id;
  966. obj_desc.irq_count = 1;
  967. obj_desc.region_count = 0;
  968. error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
  969. if (error < 0)
  970. goto error_cleanup_mc_io;
  971. mc->root_mc_bus_dev = mc_bus_dev;
  972. mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
  973. return 0;
  974. error_cleanup_mc_io:
  975. fsl_destroy_mc_io(mc_io);
  976. return error;
  977. }
  978. /*
  979. * fsl_mc_bus_remove - callback invoked when the root MC bus is being
  980. * removed
  981. */
  982. static void fsl_mc_bus_remove(struct platform_device *pdev)
  983. {
  984. struct fsl_mc *mc = platform_get_drvdata(pdev);
  985. struct fsl_mc_io *mc_io;
  986. mc_io = mc->root_mc_bus_dev->mc_io;
  987. fsl_mc_device_remove(mc->root_mc_bus_dev);
  988. fsl_destroy_mc_io(mc_io);
  989. bus_unregister_notifier(&fsl_mc_bus_type, &fsl_mc_nb);
  990. if (mc->fsl_mc_regs) {
  991. /*
  992. * Pause the MC firmware so that it doesn't crash in certain
  993. * scenarios, such as kexec.
  994. */
  995. writel(readl(mc->fsl_mc_regs + FSL_MC_GCR1) |
  996. (GCR1_P1_STOP | GCR1_P2_STOP),
  997. mc->fsl_mc_regs + FSL_MC_GCR1);
  998. }
  999. }
  1000. static const struct of_device_id fsl_mc_bus_match_table[] = {
  1001. {.compatible = "fsl,qoriq-mc",},
  1002. {},
  1003. };
  1004. MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
  1005. static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
  1006. {"NXP0008", 0 },
  1007. { }
  1008. };
  1009. MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
  1010. static struct platform_driver fsl_mc_bus_driver = {
  1011. .driver = {
  1012. .name = "fsl_mc_bus",
  1013. .pm = NULL,
  1014. .of_match_table = fsl_mc_bus_match_table,
  1015. .acpi_match_table = fsl_mc_bus_acpi_match_table,
  1016. },
  1017. .probe = fsl_mc_bus_probe,
  1018. .remove_new = fsl_mc_bus_remove,
  1019. .shutdown = fsl_mc_bus_remove,
  1020. };
  1021. static int fsl_mc_bus_notifier(struct notifier_block *nb,
  1022. unsigned long action, void *data)
  1023. {
  1024. struct device *dev = data;
  1025. struct resource *res;
  1026. void __iomem *fsl_mc_regs;
  1027. if (action != BUS_NOTIFY_ADD_DEVICE)
  1028. return 0;
  1029. if (!of_match_device(fsl_mc_bus_match_table, dev) &&
  1030. !acpi_match_device(fsl_mc_bus_acpi_match_table, dev))
  1031. return 0;
  1032. res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1);
  1033. if (!res)
  1034. return 0;
  1035. fsl_mc_regs = ioremap(res->start, resource_size(res));
  1036. if (!fsl_mc_regs)
  1037. return 0;
  1038. /*
  1039. * Make sure that the MC firmware is paused before the IOMMU setup for
  1040. * it is done or otherwise the firmware will crash right after the SMMU
  1041. * gets probed and enabled.
  1042. */
  1043. writel(readl(fsl_mc_regs + FSL_MC_GCR1) | (GCR1_P1_STOP | GCR1_P2_STOP),
  1044. fsl_mc_regs + FSL_MC_GCR1);
  1045. iounmap(fsl_mc_regs);
  1046. return 0;
  1047. }
  1048. static struct notifier_block fsl_mc_nb = {
  1049. .notifier_call = fsl_mc_bus_notifier,
  1050. };
  1051. static int __init fsl_mc_bus_driver_init(void)
  1052. {
  1053. int error;
  1054. error = bus_register(&fsl_mc_bus_type);
  1055. if (error < 0) {
  1056. pr_err("bus type registration failed: %d\n", error);
  1057. goto error_cleanup_cache;
  1058. }
  1059. error = platform_driver_register(&fsl_mc_bus_driver);
  1060. if (error < 0) {
  1061. pr_err("platform_driver_register() failed: %d\n", error);
  1062. goto error_cleanup_bus;
  1063. }
  1064. error = dprc_driver_init();
  1065. if (error < 0)
  1066. goto error_cleanup_driver;
  1067. error = fsl_mc_allocator_driver_init();
  1068. if (error < 0)
  1069. goto error_cleanup_dprc_driver;
  1070. return bus_register_notifier(&platform_bus_type, &fsl_mc_nb);
  1071. error_cleanup_dprc_driver:
  1072. dprc_driver_exit();
  1073. error_cleanup_driver:
  1074. platform_driver_unregister(&fsl_mc_bus_driver);
  1075. error_cleanup_bus:
  1076. bus_unregister(&fsl_mc_bus_type);
  1077. error_cleanup_cache:
  1078. return error;
  1079. }
  1080. postcore_initcall(fsl_mc_bus_driver_init);