device.c 8.2 KB

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