platform_early.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX--License-Identifier: GPL-2.0
  2. #include <asm/platform_early.h>
  3. #include <linux/mod_devicetable.h>
  4. #include <linux/pm.h>
  5. static __initdata LIST_HEAD(sh_early_platform_driver_list);
  6. static __initdata LIST_HEAD(sh_early_platform_device_list);
  7. static const struct platform_device_id *
  8. platform_match_id(const struct platform_device_id *id,
  9. struct platform_device *pdev)
  10. {
  11. while (id->name[0]) {
  12. if (strcmp(pdev->name, id->name) == 0) {
  13. pdev->id_entry = id;
  14. return id;
  15. }
  16. id++;
  17. }
  18. return NULL;
  19. }
  20. static int platform_match(struct device *dev, struct device_driver *drv)
  21. {
  22. struct platform_device *pdev = to_platform_device(dev);
  23. struct platform_driver *pdrv = to_platform_driver(drv);
  24. /* When driver_override is set, only bind to the matching driver */
  25. if (pdev->driver_override)
  26. return !strcmp(pdev->driver_override, drv->name);
  27. /* Then try to match against the id table */
  28. if (pdrv->id_table)
  29. return platform_match_id(pdrv->id_table, pdev) != NULL;
  30. /* fall-back to driver name match */
  31. return (strcmp(pdev->name, drv->name) == 0);
  32. }
  33. #ifdef CONFIG_PM
  34. static void device_pm_init_common(struct device *dev)
  35. {
  36. if (!dev->power.early_init) {
  37. spin_lock_init(&dev->power.lock);
  38. dev->power.qos = NULL;
  39. dev->power.early_init = true;
  40. }
  41. }
  42. static void pm_runtime_early_init(struct device *dev)
  43. {
  44. dev->power.disable_depth = 1;
  45. device_pm_init_common(dev);
  46. }
  47. #else
  48. static void pm_runtime_early_init(struct device *dev) {}
  49. #endif
  50. /**
  51. * sh_early_platform_driver_register - register early platform driver
  52. * @epdrv: sh_early_platform driver structure
  53. * @buf: string passed from early_param()
  54. *
  55. * Helper function for sh_early_platform_init() / sh_early_platform_init_buffer()
  56. */
  57. int __init sh_early_platform_driver_register(struct sh_early_platform_driver *epdrv,
  58. char *buf)
  59. {
  60. char *tmp;
  61. int n;
  62. /* Simply add the driver to the end of the global list.
  63. * Drivers will by default be put on the list in compiled-in order.
  64. */
  65. if (!epdrv->list.next) {
  66. INIT_LIST_HEAD(&epdrv->list);
  67. list_add_tail(&epdrv->list, &sh_early_platform_driver_list);
  68. }
  69. /* If the user has specified device then make sure the driver
  70. * gets prioritized. The driver of the last device specified on
  71. * command line will be put first on the list.
  72. */
  73. n = strlen(epdrv->pdrv->driver.name);
  74. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  75. list_move(&epdrv->list, &sh_early_platform_driver_list);
  76. /* Allow passing parameters after device name */
  77. if (buf[n] == '\0' || buf[n] == ',')
  78. epdrv->requested_id = -1;
  79. else {
  80. epdrv->requested_id = simple_strtoul(&buf[n + 1],
  81. &tmp, 10);
  82. if (buf[n] != '.' || (tmp == &buf[n + 1])) {
  83. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  84. n = 0;
  85. } else
  86. n += strcspn(&buf[n + 1], ",") + 1;
  87. }
  88. if (buf[n] == ',')
  89. n++;
  90. if (epdrv->bufsize) {
  91. memcpy(epdrv->buffer, &buf[n],
  92. min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
  93. epdrv->buffer[epdrv->bufsize - 1] = '\0';
  94. }
  95. }
  96. return 0;
  97. }
  98. /**
  99. * sh_early_platform_add_devices - adds a number of early platform devices
  100. * @devs: array of early platform devices to add
  101. * @num: number of early platform devices in array
  102. *
  103. * Used by early architecture code to register early platform devices and
  104. * their platform data.
  105. */
  106. void __init sh_early_platform_add_devices(struct platform_device **devs, int num)
  107. {
  108. struct device *dev;
  109. int i;
  110. /* simply add the devices to list */
  111. for (i = 0; i < num; i++) {
  112. dev = &devs[i]->dev;
  113. if (!dev->devres_head.next) {
  114. pm_runtime_early_init(dev);
  115. INIT_LIST_HEAD(&dev->devres_head);
  116. list_add_tail(&dev->devres_head,
  117. &sh_early_platform_device_list);
  118. }
  119. }
  120. }
  121. /**
  122. * sh_early_platform_driver_register_all - register early platform drivers
  123. * @class_str: string to identify early platform driver class
  124. *
  125. * Used by architecture code to register all early platform drivers
  126. * for a certain class. If omitted then only early platform drivers
  127. * with matching kernel command line class parameters will be registered.
  128. */
  129. void __init sh_early_platform_driver_register_all(char *class_str)
  130. {
  131. /* The "class_str" parameter may or may not be present on the kernel
  132. * command line. If it is present then there may be more than one
  133. * matching parameter.
  134. *
  135. * Since we register our early platform drivers using early_param()
  136. * we need to make sure that they also get registered in the case
  137. * when the parameter is missing from the kernel command line.
  138. *
  139. * We use parse_early_options() to make sure the early_param() gets
  140. * called at least once. The early_param() may be called more than
  141. * once since the name of the preferred device may be specified on
  142. * the kernel command line. sh_early_platform_driver_register() handles
  143. * this case for us.
  144. */
  145. parse_early_options(class_str);
  146. }
  147. /**
  148. * sh_early_platform_match - find early platform device matching driver
  149. * @epdrv: early platform driver structure
  150. * @id: id to match against
  151. */
  152. static struct platform_device * __init
  153. sh_early_platform_match(struct sh_early_platform_driver *epdrv, int id)
  154. {
  155. struct platform_device *pd;
  156. list_for_each_entry(pd, &sh_early_platform_device_list, dev.devres_head)
  157. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  158. if (pd->id == id)
  159. return pd;
  160. return NULL;
  161. }
  162. /**
  163. * sh_early_platform_left - check if early platform driver has matching devices
  164. * @epdrv: early platform driver structure
  165. * @id: return true if id or above exists
  166. */
  167. static int __init sh_early_platform_left(struct sh_early_platform_driver *epdrv,
  168. int id)
  169. {
  170. struct platform_device *pd;
  171. list_for_each_entry(pd, &sh_early_platform_device_list, dev.devres_head)
  172. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  173. if (pd->id >= id)
  174. return 1;
  175. return 0;
  176. }
  177. /**
  178. * sh_early_platform_driver_probe_id - probe drivers matching class_str and id
  179. * @class_str: string to identify early platform driver class
  180. * @id: id to match against
  181. * @nr_probe: number of platform devices to successfully probe before exiting
  182. */
  183. static int __init sh_early_platform_driver_probe_id(char *class_str,
  184. int id,
  185. int nr_probe)
  186. {
  187. struct sh_early_platform_driver *epdrv;
  188. struct platform_device *match;
  189. int match_id;
  190. int n = 0;
  191. int left = 0;
  192. list_for_each_entry(epdrv, &sh_early_platform_driver_list, list) {
  193. /* only use drivers matching our class_str */
  194. if (strcmp(class_str, epdrv->class_str))
  195. continue;
  196. if (id == -2) {
  197. match_id = epdrv->requested_id;
  198. left = 1;
  199. } else {
  200. match_id = id;
  201. left += sh_early_platform_left(epdrv, id);
  202. /* skip requested id */
  203. switch (epdrv->requested_id) {
  204. case EARLY_PLATFORM_ID_ERROR:
  205. case EARLY_PLATFORM_ID_UNSET:
  206. break;
  207. default:
  208. if (epdrv->requested_id == id)
  209. match_id = EARLY_PLATFORM_ID_UNSET;
  210. }
  211. }
  212. switch (match_id) {
  213. case EARLY_PLATFORM_ID_ERROR:
  214. pr_warn("%s: unable to parse %s parameter\n",
  215. class_str, epdrv->pdrv->driver.name);
  216. fallthrough;
  217. case EARLY_PLATFORM_ID_UNSET:
  218. match = NULL;
  219. break;
  220. default:
  221. match = sh_early_platform_match(epdrv, match_id);
  222. }
  223. if (match) {
  224. /*
  225. * Set up a sensible init_name to enable
  226. * dev_name() and others to be used before the
  227. * rest of the driver core is initialized.
  228. */
  229. if (!match->dev.init_name && slab_is_available()) {
  230. if (match->id != -1)
  231. match->dev.init_name =
  232. kasprintf(GFP_KERNEL, "%s.%d",
  233. match->name,
  234. match->id);
  235. else
  236. match->dev.init_name =
  237. kasprintf(GFP_KERNEL, "%s",
  238. match->name);
  239. if (!match->dev.init_name)
  240. return -ENOMEM;
  241. }
  242. if (epdrv->pdrv->probe(match))
  243. pr_warn("%s: unable to probe %s early.\n",
  244. class_str, match->name);
  245. else
  246. n++;
  247. }
  248. if (n >= nr_probe)
  249. break;
  250. }
  251. if (left)
  252. return n;
  253. else
  254. return -ENODEV;
  255. }
  256. /**
  257. * sh_early_platform_driver_probe - probe a class of registered drivers
  258. * @class_str: string to identify early platform driver class
  259. * @nr_probe: number of platform devices to successfully probe before exiting
  260. * @user_only: only probe user specified early platform devices
  261. *
  262. * Used by architecture code to probe registered early platform drivers
  263. * within a certain class. For probe to happen a registered early platform
  264. * device matching a registered early platform driver is needed.
  265. */
  266. int __init sh_early_platform_driver_probe(char *class_str,
  267. int nr_probe,
  268. int user_only)
  269. {
  270. int k, n, i;
  271. n = 0;
  272. for (i = -2; n < nr_probe; i++) {
  273. k = sh_early_platform_driver_probe_id(class_str, i, nr_probe - n);
  274. if (k < 0)
  275. break;
  276. n += k;
  277. if (user_only)
  278. break;
  279. }
  280. return n;
  281. }
  282. /**
  283. * early_platform_cleanup - clean up early platform code
  284. */
  285. void __init early_platform_cleanup(void)
  286. {
  287. struct platform_device *pd, *pd2;
  288. /* clean up the devres list used to chain devices */
  289. list_for_each_entry_safe(pd, pd2, &sh_early_platform_device_list,
  290. dev.devres_head) {
  291. list_del(&pd->dev.devres_head);
  292. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  293. }
  294. }