device.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. void 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. 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 = 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;
  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. dev_dbg(dev, "device is%sbehind an iommu\n",
  129. iommu ? " " : " not ");
  130. arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent);
  131. }
  132. EXPORT_SYMBOL_GPL(of_dma_configure);
  133. int of_device_register(struct platform_device *pdev)
  134. {
  135. device_initialize(&pdev->dev);
  136. return of_device_add(pdev);
  137. }
  138. EXPORT_SYMBOL(of_device_register);
  139. void of_device_unregister(struct platform_device *ofdev)
  140. {
  141. device_unregister(&ofdev->dev);
  142. }
  143. EXPORT_SYMBOL(of_device_unregister);
  144. const void *of_device_get_match_data(const struct device *dev)
  145. {
  146. const struct of_device_id *match;
  147. match = of_match_device(dev->driver->of_match_table, dev);
  148. if (!match)
  149. return NULL;
  150. return match->data;
  151. }
  152. EXPORT_SYMBOL(of_device_get_match_data);
  153. ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
  154. {
  155. const char *compat;
  156. int cplen, i;
  157. ssize_t tsize, csize, repend;
  158. if ((!dev) || (!dev->of_node))
  159. return -ENODEV;
  160. /* Name & Type */
  161. csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
  162. dev->of_node->type);
  163. /* Get compatible property if any */
  164. compat = of_get_property(dev->of_node, "compatible", &cplen);
  165. if (!compat)
  166. return csize;
  167. /* Find true end (we tolerate multiple \0 at the end */
  168. for (i = (cplen - 1); i >= 0 && !compat[i]; i--)
  169. cplen--;
  170. if (!cplen)
  171. return csize;
  172. cplen++;
  173. /* Check space (need cplen+1 chars including final \0) */
  174. tsize = csize + cplen;
  175. repend = tsize;
  176. if (csize >= len) /* @ the limit, all is already filled */
  177. return tsize;
  178. if (tsize >= len) { /* limit compat list */
  179. cplen = len - csize - 1;
  180. repend = len;
  181. }
  182. /* Copy and do char replacement */
  183. memcpy(&str[csize + 1], compat, cplen);
  184. for (i = csize; i < repend; i++) {
  185. char c = str[i];
  186. if (c == '\0')
  187. str[i] = 'C';
  188. else if (c == ' ')
  189. str[i] = '_';
  190. }
  191. return repend;
  192. }
  193. EXPORT_SYMBOL_GPL(of_device_get_modalias);
  194. /**
  195. * of_device_uevent - Display OF related uevent information
  196. */
  197. void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  198. {
  199. const char *compat;
  200. struct alias_prop *app;
  201. int seen = 0, cplen, sl;
  202. if ((!dev) || (!dev->of_node))
  203. return;
  204. add_uevent_var(env, "OF_NAME=%s", dev->of_node->name);
  205. add_uevent_var(env, "OF_FULLNAME=%s", dev->of_node->full_name);
  206. if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0)
  207. add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type);
  208. /* Since the compatible field can contain pretty much anything
  209. * it's not really legal to split it out with commas. We split it
  210. * up using a number of environment variables instead. */
  211. compat = of_get_property(dev->of_node, "compatible", &cplen);
  212. while (compat && *compat && cplen > 0) {
  213. add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
  214. sl = strlen(compat) + 1;
  215. compat += sl;
  216. cplen -= sl;
  217. seen++;
  218. }
  219. add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
  220. seen = 0;
  221. mutex_lock(&of_mutex);
  222. list_for_each_entry(app, &aliases_lookup, link) {
  223. if (dev->of_node == app->np) {
  224. add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
  225. app->alias);
  226. seen++;
  227. }
  228. }
  229. mutex_unlock(&of_mutex);
  230. }
  231. int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
  232. {
  233. int sl;
  234. if ((!dev) || (!dev->of_node))
  235. return -ENODEV;
  236. /* Devicetree modalias is tricky, we add it in 2 steps */
  237. if (add_uevent_var(env, "MODALIAS="))
  238. return -ENOMEM;
  239. sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
  240. sizeof(env->buf) - env->buflen);
  241. if (sl >= (sizeof(env->buf) - env->buflen))
  242. return -ENOMEM;
  243. env->buflen += sl;
  244. return 0;
  245. }
  246. EXPORT_SYMBOL_GPL(of_device_uevent_modalias);