device.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/of.h>
  4. #include <linux/of_device.h>
  5. #include <linux/of_address.h>
  6. #include <linux/of_iommu.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/slab.h>
  12. #include <linux/pci.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/amba/bus.h>
  15. #include <asm/errno.h>
  16. #include "of_private.h"
  17. /**
  18. * of_match_device - Tell if a struct device matches an of_device_id list
  19. * @ids: array of of device match structures to search in
  20. * @dev: the of device structure to match against
  21. *
  22. * Used by a driver to check whether an platform_device present in the
  23. * system is in its list of supported devices.
  24. */
  25. const struct of_device_id *of_match_device(const struct of_device_id *matches,
  26. const struct device *dev)
  27. {
  28. if ((!matches) || (!dev->of_node))
  29. return NULL;
  30. return of_match_node(matches, dev->of_node);
  31. }
  32. EXPORT_SYMBOL(of_match_device);
  33. struct platform_device *of_dev_get(struct platform_device *dev)
  34. {
  35. struct device *tmp;
  36. if (!dev)
  37. return NULL;
  38. tmp = get_device(&dev->dev);
  39. if (tmp)
  40. return to_platform_device(tmp);
  41. else
  42. return NULL;
  43. }
  44. EXPORT_SYMBOL(of_dev_get);
  45. void of_dev_put(struct platform_device *dev)
  46. {
  47. if (dev)
  48. put_device(&dev->dev);
  49. }
  50. EXPORT_SYMBOL(of_dev_put);
  51. int of_device_add(struct platform_device *ofdev)
  52. {
  53. BUG_ON(ofdev->dev.of_node == NULL);
  54. /* name and id have to be set so that the platform bus doesn't get
  55. * confused on matching */
  56. ofdev->name = dev_name(&ofdev->dev);
  57. ofdev->id = PLATFORM_DEVID_NONE;
  58. /*
  59. * If this device has not binding numa node in devicetree, that is
  60. * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
  61. * device is on the same node as the parent.
  62. */
  63. set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
  64. return device_add(&ofdev->dev);
  65. }
  66. /**
  67. * of_dma_configure - Setup DMA configuration
  68. * @dev: Device to apply DMA configuration
  69. * @np: Pointer to OF node having DMA configuration
  70. *
  71. * Try to get devices's DMA configuration from DT and update it
  72. * accordingly.
  73. *
  74. * If platform code needs to use its own special DMA configuration, it
  75. * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
  76. * to fix up DMA configuration.
  77. */
  78. int of_dma_configure(struct device *dev, struct device_node *np)
  79. {
  80. u64 dma_addr, paddr, size = 0;
  81. int ret;
  82. bool coherent;
  83. unsigned long offset;
  84. const struct iommu_ops *iommu;
  85. u64 mask;
  86. ret = of_dma_get_range(np, &dma_addr, &paddr, &size);
  87. if (ret < 0) {
  88. /*
  89. * For legacy reasons, we have to assume some devices need
  90. * DMA configuration regardless of whether "dma-ranges" is
  91. * correctly specified or not.
  92. */
  93. if (!dev_is_pci(dev) &&
  94. #ifdef CONFIG_ARM_AMBA
  95. dev->bus != &amba_bustype &&
  96. #endif
  97. dev->bus != &platform_bus_type)
  98. return ret == -ENODEV ? 0 : ret;
  99. dma_addr = offset = 0;
  100. } else {
  101. offset = PFN_DOWN(paddr - dma_addr);
  102. /*
  103. * Add a work around to treat the size as mask + 1 in case
  104. * it is defined in DT as a mask.
  105. */
  106. if (size & 1) {
  107. dev_warn(dev, "Invalid size 0x%llx for dma-range\n",
  108. size);
  109. size = size + 1;
  110. }
  111. if (!size) {
  112. dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
  113. return -EINVAL;
  114. }
  115. dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
  116. }
  117. /*
  118. * Set default coherent_dma_mask to 32 bit. Drivers are expected to
  119. * setup the correct supported mask.
  120. */
  121. if (!dev->coherent_dma_mask)
  122. dev->coherent_dma_mask = DMA_BIT_MASK(32);
  123. /*
  124. * Set it to coherent_dma_mask by default if the architecture
  125. * code has not set it.
  126. */
  127. if (!dev->dma_mask)
  128. dev->dma_mask = &dev->coherent_dma_mask;
  129. if (!size)
  130. size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
  131. dev->dma_pfn_offset = offset;
  132. /*
  133. * Limit coherent and dma mask based on size and default mask
  134. * set by the driver.
  135. */
  136. mask = DMA_BIT_MASK(ilog2(dma_addr + size - 1) + 1);
  137. dev->coherent_dma_mask &= mask;
  138. *dev->dma_mask &= mask;
  139. coherent = of_dma_is_coherent(np);
  140. dev_dbg(dev, "device is%sdma coherent\n",
  141. coherent ? " " : " not ");
  142. iommu = of_iommu_configure(dev, np);
  143. if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER)
  144. return -EPROBE_DEFER;
  145. dev_dbg(dev, "device is%sbehind an iommu\n",
  146. iommu ? " " : " not ");
  147. arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent);
  148. return 0;
  149. }
  150. EXPORT_SYMBOL_GPL(of_dma_configure);
  151. /**
  152. * of_dma_deconfigure - Clean up DMA configuration
  153. * @dev: Device for which to clean up DMA configuration
  154. *
  155. * Clean up all configuration performed by of_dma_configure_ops() and free all
  156. * resources that have been allocated.
  157. */
  158. void of_dma_deconfigure(struct device *dev)
  159. {
  160. arch_teardown_dma_ops(dev);
  161. }
  162. int of_device_register(struct platform_device *pdev)
  163. {
  164. device_initialize(&pdev->dev);
  165. return of_device_add(pdev);
  166. }
  167. EXPORT_SYMBOL(of_device_register);
  168. void of_device_unregister(struct platform_device *ofdev)
  169. {
  170. device_unregister(&ofdev->dev);
  171. }
  172. EXPORT_SYMBOL(of_device_unregister);
  173. const void *of_device_get_match_data(const struct device *dev)
  174. {
  175. const struct of_device_id *match;
  176. match = of_match_device(dev->driver->of_match_table, dev);
  177. if (!match)
  178. return NULL;
  179. return match->data;
  180. }
  181. EXPORT_SYMBOL(of_device_get_match_data);
  182. static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
  183. {
  184. const char *compat;
  185. char *c;
  186. struct property *p;
  187. ssize_t csize;
  188. ssize_t tsize;
  189. if ((!dev) || (!dev->of_node))
  190. return -ENODEV;
  191. /* Name & Type */
  192. csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
  193. dev->of_node->type);
  194. tsize = csize;
  195. len -= csize;
  196. if (str)
  197. str += csize;
  198. of_property_for_each_string(dev->of_node, "compatible", p, compat) {
  199. csize = strlen(compat) + 1;
  200. tsize += csize;
  201. if (csize > len)
  202. continue;
  203. csize = snprintf(str, len, "C%s", compat);
  204. for (c = str; c; ) {
  205. c = strchr(c, ' ');
  206. if (c)
  207. *c++ = '_';
  208. }
  209. len -= csize;
  210. str += csize;
  211. }
  212. return tsize;
  213. }
  214. int of_device_request_module(struct device *dev)
  215. {
  216. char *str;
  217. ssize_t size;
  218. int ret;
  219. size = of_device_get_modalias(dev, NULL, 0);
  220. if (size < 0)
  221. return size;
  222. str = kmalloc(size + 1, GFP_KERNEL);
  223. if (!str)
  224. return -ENOMEM;
  225. of_device_get_modalias(dev, str, size);
  226. str[size] = '\0';
  227. ret = request_module(str);
  228. kfree(str);
  229. return ret;
  230. }
  231. EXPORT_SYMBOL_GPL(of_device_request_module);
  232. /**
  233. * of_device_modalias - Fill buffer with newline terminated modalias string
  234. */
  235. ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
  236. {
  237. ssize_t sl = of_device_get_modalias(dev, str, len - 2);
  238. if (sl < 0)
  239. return sl;
  240. if (sl > len - 2)
  241. return -ENOMEM;
  242. str[sl++] = '\n';
  243. str[sl] = 0;
  244. return sl;
  245. }
  246. EXPORT_SYMBOL_GPL(of_device_modalias);
  247. /**
  248. * of_device_uevent - Display OF related uevent information
  249. */
  250. void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  251. {
  252. const char *compat;
  253. struct alias_prop *app;
  254. struct property *p;
  255. int seen = 0;
  256. if ((!dev) || (!dev->of_node))
  257. return;
  258. add_uevent_var(env, "OF_NAME=%s", dev->of_node->name);
  259. add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
  260. if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0)
  261. add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type);
  262. /* Since the compatible field can contain pretty much anything
  263. * it's not really legal to split it out with commas. We split it
  264. * up using a number of environment variables instead. */
  265. of_property_for_each_string(dev->of_node, "compatible", p, compat) {
  266. add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
  267. seen++;
  268. }
  269. add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
  270. seen = 0;
  271. mutex_lock(&of_mutex);
  272. list_for_each_entry(app, &aliases_lookup, link) {
  273. if (dev->of_node == app->np) {
  274. add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
  275. app->alias);
  276. seen++;
  277. }
  278. }
  279. mutex_unlock(&of_mutex);
  280. }
  281. int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
  282. {
  283. int sl;
  284. if ((!dev) || (!dev->of_node))
  285. return -ENODEV;
  286. /* Devicetree modalias is tricky, we add it in 2 steps */
  287. if (add_uevent_var(env, "MODALIAS="))
  288. return -ENOMEM;
  289. sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
  290. sizeof(env->buf) - env->buflen);
  291. if (sl >= (sizeof(env->buf) - env->buflen))
  292. return -ENOMEM;
  293. env->buflen += sl;
  294. return 0;
  295. }
  296. EXPORT_SYMBOL_GPL(of_device_uevent_modalias);