device.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. /* device_add will assume that this device is on the same node as
  56. * the parent. If there is no parent defined, set the node
  57. * explicitly */
  58. if (!ofdev->dev.parent)
  59. set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
  60. return device_add(&ofdev->dev);
  61. }
  62. /**
  63. * of_dma_configure - Setup DMA configuration
  64. * @dev: Device to apply DMA configuration
  65. * @np: Pointer to OF node having DMA configuration
  66. *
  67. * Try to get devices's DMA configuration from DT and update it
  68. * accordingly.
  69. *
  70. * If platform code needs to use its own special DMA configuration, it
  71. * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
  72. * to fix up DMA configuration.
  73. */
  74. void of_dma_configure(struct device *dev, struct device_node *np)
  75. {
  76. u64 dma_addr, paddr, size;
  77. int ret;
  78. bool coherent;
  79. unsigned long offset;
  80. struct iommu_ops *iommu;
  81. /*
  82. * Set default coherent_dma_mask to 32 bit. Drivers are expected to
  83. * setup the correct supported mask.
  84. */
  85. if (!dev->coherent_dma_mask)
  86. dev->coherent_dma_mask = DMA_BIT_MASK(32);
  87. /*
  88. * Set it to coherent_dma_mask by default if the architecture
  89. * code has not set it.
  90. */
  91. if (!dev->dma_mask)
  92. dev->dma_mask = &dev->coherent_dma_mask;
  93. ret = of_dma_get_range(np, &dma_addr, &paddr, &size);
  94. if (ret < 0) {
  95. dma_addr = offset = 0;
  96. size = dev->coherent_dma_mask + 1;
  97. } else {
  98. offset = PFN_DOWN(paddr - dma_addr);
  99. /*
  100. * Add a work around to treat the size as mask + 1 in case
  101. * it is defined in DT as a mask.
  102. */
  103. if (size & 1) {
  104. dev_warn(dev, "Invalid size 0x%llx for dma-range\n",
  105. size);
  106. size = size + 1;
  107. }
  108. if (!size) {
  109. dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
  110. return;
  111. }
  112. dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
  113. }
  114. dev->dma_pfn_offset = offset;
  115. /*
  116. * Limit coherent and dma mask based on size and default mask
  117. * set by the driver.
  118. */
  119. dev->coherent_dma_mask = min(dev->coherent_dma_mask,
  120. DMA_BIT_MASK(ilog2(dma_addr + size)));
  121. *dev->dma_mask = min((*dev->dma_mask),
  122. DMA_BIT_MASK(ilog2(dma_addr + size)));
  123. coherent = of_dma_is_coherent(np);
  124. dev_dbg(dev, "device is%sdma coherent\n",
  125. coherent ? " " : " not ");
  126. iommu = of_iommu_configure(dev, np);
  127. dev_dbg(dev, "device is%sbehind an iommu\n",
  128. iommu ? " " : " not ");
  129. arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent);
  130. }
  131. EXPORT_SYMBOL_GPL(of_dma_configure);
  132. int of_device_register(struct platform_device *pdev)
  133. {
  134. device_initialize(&pdev->dev);
  135. return of_device_add(pdev);
  136. }
  137. EXPORT_SYMBOL(of_device_register);
  138. void of_device_unregister(struct platform_device *ofdev)
  139. {
  140. device_unregister(&ofdev->dev);
  141. }
  142. EXPORT_SYMBOL(of_device_unregister);
  143. const void *of_device_get_match_data(const struct device *dev)
  144. {
  145. const struct of_device_id *match;
  146. match = of_match_device(dev->driver->of_match_table, dev);
  147. if (!match)
  148. return NULL;
  149. return match->data;
  150. }
  151. EXPORT_SYMBOL(of_device_get_match_data);
  152. ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
  153. {
  154. const char *compat;
  155. int cplen, i;
  156. ssize_t tsize, csize, repend;
  157. if ((!dev) || (!dev->of_node))
  158. return -ENODEV;
  159. /* Name & Type */
  160. csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
  161. dev->of_node->type);
  162. /* Get compatible property if any */
  163. compat = of_get_property(dev->of_node, "compatible", &cplen);
  164. if (!compat)
  165. return csize;
  166. /* Find true end (we tolerate multiple \0 at the end */
  167. for (i = (cplen - 1); i >= 0 && !compat[i]; i--)
  168. cplen--;
  169. if (!cplen)
  170. return csize;
  171. cplen++;
  172. /* Check space (need cplen+1 chars including final \0) */
  173. tsize = csize + cplen;
  174. repend = tsize;
  175. if (csize >= len) /* @ the limit, all is already filled */
  176. return tsize;
  177. if (tsize >= len) { /* limit compat list */
  178. cplen = len - csize - 1;
  179. repend = len;
  180. }
  181. /* Copy and do char replacement */
  182. memcpy(&str[csize + 1], compat, cplen);
  183. for (i = csize; i < repend; i++) {
  184. char c = str[i];
  185. if (c == '\0')
  186. str[i] = 'C';
  187. else if (c == ' ')
  188. str[i] = '_';
  189. }
  190. return tsize;
  191. }
  192. /**
  193. * of_device_uevent - Display OF related uevent information
  194. */
  195. void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  196. {
  197. const char *compat;
  198. struct alias_prop *app;
  199. int seen = 0, cplen, sl;
  200. if ((!dev) || (!dev->of_node))
  201. return;
  202. add_uevent_var(env, "OF_NAME=%s", dev->of_node->name);
  203. add_uevent_var(env, "OF_FULLNAME=%s", dev->of_node->full_name);
  204. if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0)
  205. add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type);
  206. /* Since the compatible field can contain pretty much anything
  207. * it's not really legal to split it out with commas. We split it
  208. * up using a number of environment variables instead. */
  209. compat = of_get_property(dev->of_node, "compatible", &cplen);
  210. while (compat && *compat && cplen > 0) {
  211. add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
  212. sl = strlen(compat) + 1;
  213. compat += sl;
  214. cplen -= sl;
  215. seen++;
  216. }
  217. add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
  218. seen = 0;
  219. mutex_lock(&of_mutex);
  220. list_for_each_entry(app, &aliases_lookup, link) {
  221. if (dev->of_node == app->np) {
  222. add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
  223. app->alias);
  224. seen++;
  225. }
  226. }
  227. mutex_unlock(&of_mutex);
  228. }
  229. int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
  230. {
  231. int sl;
  232. if ((!dev) || (!dev->of_node))
  233. return -ENODEV;
  234. /* Devicetree modalias is tricky, we add it in 2 steps */
  235. if (add_uevent_var(env, "MODALIAS="))
  236. return -ENOMEM;
  237. sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
  238. sizeof(env->buf) - env->buflen);
  239. if (sl >= (sizeof(env->buf) - env->buflen))
  240. return -ENOMEM;
  241. env->buflen += sl;
  242. return 0;
  243. }