blob: 64b710265d3907a404dcc92bd720b145f48b67da [file] [log] [blame]
Stephen Rothwellf85ff302007-05-01 16:40:36 +10001#include <linux/string.h>
2#include <linux/kernel.h>
3#include <linux/of.h>
Stephen Rothwellf898f8d2007-05-01 16:49:51 +10004#include <linux/of_device.h>
Murali Karicheri1f5c69a2015-03-03 12:52:09 -05005#include <linux/of_address.h>
6#include <linux/of_iommu.h>
7#include <linux/dma-mapping.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +10008#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/mod_devicetable.h>
11#include <linux/slab.h>
Robin Murphy72328882017-08-31 11:32:54 +010012#include <linux/pci.h>
13#include <linux/platform_device.h>
14#include <linux/amba/bus.h>
Stephen Rothwellf85ff302007-05-01 16:40:36 +100015
16#include <asm/errno.h>
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -080017#include "of_private.h"
Stephen Rothwellf85ff302007-05-01 16:40:36 +100018
19/**
Grant Likely44504b22010-04-13 16:13:22 -070020 * of_match_device - Tell if a struct device matches an of_device_id list
Stephen Rothwellf85ff302007-05-01 16:40:36 +100021 * @ids: array of of device match structures to search in
22 * @dev: the of device structure to match against
23 *
Grant Likely2dc11582010-08-06 09:25:50 -060024 * Used by a driver to check whether an platform_device present in the
Stephen Rothwellf85ff302007-05-01 16:40:36 +100025 * system is in its list of supported devices.
26 */
27const struct of_device_id *of_match_device(const struct of_device_id *matches,
Grant Likely44504b22010-04-13 16:13:22 -070028 const struct device *dev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100029{
Grant Likely8cec0e72010-06-08 07:48:17 -060030 if ((!matches) || (!dev->of_node))
Stephen Rothwellf85ff302007-05-01 16:40:36 +100031 return NULL;
Grant Likely44504b22010-04-13 16:13:22 -070032 return of_match_node(matches, dev->of_node);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100033}
34EXPORT_SYMBOL(of_match_device);
35
Grant Likely94a0cb12010-07-22 13:59:23 -060036struct platform_device *of_dev_get(struct platform_device *dev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100037{
38 struct device *tmp;
39
40 if (!dev)
41 return NULL;
42 tmp = get_device(&dev->dev);
43 if (tmp)
Grant Likely94a0cb12010-07-22 13:59:23 -060044 return to_platform_device(tmp);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100045 else
46 return NULL;
47}
48EXPORT_SYMBOL(of_dev_get);
49
Grant Likely94a0cb12010-07-22 13:59:23 -060050void of_dev_put(struct platform_device *dev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100051{
52 if (dev)
53 put_device(&dev->dev);
54}
55EXPORT_SYMBOL(of_dev_put);
56
Grant Likely7096d042010-10-20 11:45:13 -060057int of_device_add(struct platform_device *ofdev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +100058{
Grant Likely61c7a082010-04-13 16:12:29 -070059 BUG_ON(ofdev->dev.of_node == NULL);
Jeremy Kerr6098e2e2008-10-26 21:51:25 +000060
Grant Likelyeca39302010-06-08 07:48:21 -060061 /* name and id have to be set so that the platform bus doesn't get
62 * confused on matching */
63 ofdev->name = dev_name(&ofdev->dev);
Andy Shevchenko6d7e3bf82017-08-25 18:33:13 +030064 ofdev->id = PLATFORM_DEVID_NONE;
Grant Likelyeca39302010-06-08 07:48:21 -060065
Zhen Lei56f2de82015-08-25 12:08:22 +080066 /*
67 * If this device has not binding numa node in devicetree, that is
68 * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
69 * device is on the same node as the parent.
70 */
71 set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
Jeremy Kerr6098e2e2008-10-26 21:51:25 +000072
73 return device_add(&ofdev->dev);
Stephen Rothwellf85ff302007-05-01 16:40:36 +100074}
Grant Likely7096d042010-10-20 11:45:13 -060075
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050076/**
77 * of_dma_configure - Setup DMA configuration
78 * @dev: Device to apply DMA configuration
79 * @np: Pointer to OF node having DMA configuration
80 *
81 * Try to get devices's DMA configuration from DT and update it
82 * accordingly.
83 *
84 * If platform code needs to use its own special DMA configuration, it
85 * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
86 * to fix up DMA configuration.
87 */
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +053088int of_dma_configure(struct device *dev, struct device_node *np)
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050089{
Robin Murphy72328882017-08-31 11:32:54 +010090 u64 dma_addr, paddr, size = 0;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050091 int ret;
92 bool coherent;
93 unsigned long offset;
Robin Murphy53c92d72016-04-07 18:42:05 +010094 const struct iommu_ops *iommu;
Robin Murphyee7b1f32017-08-11 17:29:56 +010095 u64 mask;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050096
Murali Karicheri1f5c69a2015-03-03 12:52:09 -050097 ret = of_dma_get_range(np, &dma_addr, &paddr, &size);
98 if (ret < 0) {
Robin Murphy72328882017-08-31 11:32:54 +010099 /*
100 * For legacy reasons, we have to assume some devices need
101 * DMA configuration regardless of whether "dma-ranges" is
102 * correctly specified or not.
103 */
104 if (!dev_is_pci(dev) &&
105#ifdef CONFIG_ARM_AMBA
106 dev->bus != &amba_bustype &&
107#endif
108 dev->bus != &platform_bus_type)
109 return ret == -ENODEV ? 0 : ret;
110
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500111 dma_addr = offset = 0;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500112 } else {
113 offset = PFN_DOWN(paddr - dma_addr);
Murali Karicheri0c79c812015-03-03 12:52:10 -0500114
115 /*
116 * Add a work around to treat the size as mask + 1 in case
117 * it is defined in DT as a mask.
118 */
119 if (size & 1) {
120 dev_warn(dev, "Invalid size 0x%llx for dma-range\n",
121 size);
122 size = size + 1;
123 }
124
125 if (!size) {
126 dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530127 return -EINVAL;
Murali Karicheri0c79c812015-03-03 12:52:10 -0500128 }
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500129 dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
130 }
131
Robin Murphy72328882017-08-31 11:32:54 +0100132 /*
133 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
134 * setup the correct supported mask.
135 */
136 if (!dev->coherent_dma_mask)
137 dev->coherent_dma_mask = DMA_BIT_MASK(32);
138 /*
139 * Set it to coherent_dma_mask by default if the architecture
140 * code has not set it.
141 */
142 if (!dev->dma_mask)
143 dev->dma_mask = &dev->coherent_dma_mask;
144
145 if (!size)
146 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
147
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500148 dev->dma_pfn_offset = offset;
149
Murali Karicheri9a6d7292015-03-03 14:44:57 -0600150 /*
151 * Limit coherent and dma mask based on size and default mask
152 * set by the driver.
153 */
Robin Murphyee7b1f32017-08-11 17:29:56 +0100154 mask = DMA_BIT_MASK(ilog2(dma_addr + size - 1) + 1);
155 dev->coherent_dma_mask &= mask;
156 *dev->dma_mask &= mask;
Murali Karicheri9a6d7292015-03-03 14:44:57 -0600157
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500158 coherent = of_dma_is_coherent(np);
159 dev_dbg(dev, "device is%sdma coherent\n",
160 coherent ? " " : " not ");
161
162 iommu = of_iommu_configure(dev, np);
Sricharan Ra37b19a2017-05-27 19:17:41 +0530163 if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER)
164 return -EPROBE_DEFER;
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530165
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500166 dev_dbg(dev, "device is%sbehind an iommu\n",
167 iommu ? " " : " not ");
168
169 arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent);
Laurent Pinchart7b07cbe2017-04-10 16:51:02 +0530170
171 return 0;
Murali Karicheri1f5c69a2015-03-03 12:52:09 -0500172}
173EXPORT_SYMBOL_GPL(of_dma_configure);
174
Laurent Pinchart3f186672017-04-10 16:50:58 +0530175/**
176 * of_dma_deconfigure - Clean up DMA configuration
177 * @dev: Device for which to clean up DMA configuration
178 *
179 * Clean up all configuration performed by of_dma_configure_ops() and free all
180 * resources that have been allocated.
181 */
182void of_dma_deconfigure(struct device *dev)
183{
184 arch_teardown_dma_ops(dev);
185}
186
Grant Likely7096d042010-10-20 11:45:13 -0600187int of_device_register(struct platform_device *pdev)
188{
189 device_initialize(&pdev->dev);
190 return of_device_add(pdev);
191}
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000192EXPORT_SYMBOL(of_device_register);
193
Grant Likely94a0cb12010-07-22 13:59:23 -0600194void of_device_unregister(struct platform_device *ofdev)
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000195{
Stephen Rothwellf85ff302007-05-01 16:40:36 +1000196 device_unregister(&ofdev->dev);
197}
198EXPORT_SYMBOL(of_device_unregister);
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000199
Joachim Eastwood3386e0f2015-05-06 20:09:09 +0200200const void *of_device_get_match_data(const struct device *dev)
201{
202 const struct of_device_id *match;
203
204 match = of_match_device(dev->driver->of_match_table, dev);
205 if (!match)
206 return NULL;
207
208 return match->data;
209}
210EXPORT_SYMBOL(of_device_get_match_data);
211
Rob Herring0634c292017-03-22 09:16:27 -0500212static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000213{
214 const char *compat;
Rob Herringbc575062017-07-21 15:45:32 -0500215 char *c;
216 struct property *p;
217 ssize_t csize;
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700218 ssize_t tsize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000219
Zhang Ruib9f73062014-01-14 16:46:38 +0800220 if ((!dev) || (!dev->of_node))
221 return -ENODEV;
222
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000223 /* Name & Type */
Grant Likely34a1c1e2010-06-08 07:48:13 -0600224 csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
225 dev->of_node->type);
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700226 tsize = csize;
Rob Herringbc575062017-07-21 15:45:32 -0500227 len -= csize;
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700228 if (str)
229 str += csize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000230
Rob Herringbc575062017-07-21 15:45:32 -0500231 of_property_for_each_string(dev->of_node, "compatible", p, compat) {
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700232 csize = strlen(compat) + 1;
233 tsize += csize;
234 if (csize > len)
235 continue;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000236
Rob Herringbc575062017-07-21 15:45:32 -0500237 csize = snprintf(str, len, "C%s", compat);
238 for (c = str; c; ) {
239 c = strchr(c, ' ');
240 if (c)
241 *c++ = '_';
242 }
243 len -= csize;
244 str += csize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000245 }
246
Bjorn Andersson8c2a75e2017-08-23 18:03:52 -0700247 return tsize;
Stephen Rothwell09e67ca2008-05-16 11:57:45 +1000248}
Grant Likelydd27dcd2010-06-08 07:48:12 -0600249
Stephen Boyd9c829c02016-12-28 14:56:47 -0800250int of_device_request_module(struct device *dev)
251{
252 char *str;
253 ssize_t size;
254 int ret;
255
256 size = of_device_get_modalias(dev, NULL, 0);
257 if (size < 0)
258 return size;
259
260 str = kmalloc(size + 1, GFP_KERNEL);
261 if (!str)
262 return -ENOMEM;
263
264 of_device_get_modalias(dev, str, size);
265 str[size] = '\0';
266 ret = request_module(str);
267 kfree(str);
268
269 return ret;
270}
271EXPORT_SYMBOL_GPL(of_device_request_module);
272
Grant Likelydd27dcd2010-06-08 07:48:12 -0600273/**
Rob Herring0634c292017-03-22 09:16:27 -0500274 * of_device_modalias - Fill buffer with newline terminated modalias string
275 */
276ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
277{
278 ssize_t sl = of_device_get_modalias(dev, str, len - 2);
279 if (sl < 0)
280 return sl;
Bjorn Andersson08ab58d2017-08-23 18:04:04 -0700281 if (sl > len - 2)
282 return -ENOMEM;
Rob Herring0634c292017-03-22 09:16:27 -0500283
284 str[sl++] = '\n';
285 str[sl] = 0;
286 return sl;
287}
288EXPORT_SYMBOL_GPL(of_device_modalias);
289
290/**
Grant Likelydd27dcd2010-06-08 07:48:12 -0600291 * of_device_uevent - Display OF related uevent information
292 */
Grant Likely07d57a32012-02-01 11:22:22 -0700293void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Grant Likelydd27dcd2010-06-08 07:48:12 -0600294{
295 const char *compat;
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800296 struct alias_prop *app;
Rob Herringbc575062017-07-21 15:45:32 -0500297 struct property *p;
298 int seen = 0;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600299
300 if ((!dev) || (!dev->of_node))
Grant Likely07d57a32012-02-01 11:22:22 -0700301 return;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600302
Grant Likely07d57a32012-02-01 11:22:22 -0700303 add_uevent_var(env, "OF_NAME=%s", dev->of_node->name);
Rob Herring0d638a02017-06-01 15:50:55 -0500304 add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
Grant Likely07d57a32012-02-01 11:22:22 -0700305 if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0)
306 add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type);
Grant Likelydd27dcd2010-06-08 07:48:12 -0600307
308 /* Since the compatible field can contain pretty much anything
309 * it's not really legal to split it out with commas. We split it
310 * up using a number of environment variables instead. */
Rob Herringbc575062017-07-21 15:45:32 -0500311 of_property_for_each_string(dev->of_node, "compatible", p, compat) {
Grant Likely07d57a32012-02-01 11:22:22 -0700312 add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
Grant Likelydd27dcd2010-06-08 07:48:12 -0600313 seen++;
314 }
Grant Likely07d57a32012-02-01 11:22:22 -0700315 add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800316
317 seen = 0;
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300318 mutex_lock(&of_mutex);
Stepan Moskovchenkoced4eec2012-12-06 14:55:41 -0800319 list_for_each_entry(app, &aliases_lookup, link) {
320 if (dev->of_node == app->np) {
321 add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
322 app->alias);
323 seen++;
324 }
325 }
Pantelis Antoniouc05aba22014-07-04 19:58:03 +0300326 mutex_unlock(&of_mutex);
Grant Likely07d57a32012-02-01 11:22:22 -0700327}
Grant Likelydd27dcd2010-06-08 07:48:12 -0600328
Grant Likely07d57a32012-02-01 11:22:22 -0700329int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
330{
331 int sl;
Grant Likelydd27dcd2010-06-08 07:48:12 -0600332
Grant Likely07d57a32012-02-01 11:22:22 -0700333 if ((!dev) || (!dev->of_node))
334 return -ENODEV;
335
336 /* Devicetree modalias is tricky, we add it in 2 steps */
Grant Likelydd27dcd2010-06-08 07:48:12 -0600337 if (add_uevent_var(env, "MODALIAS="))
338 return -ENOMEM;
339
Grant Likely34a1c1e2010-06-08 07:48:13 -0600340 sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
Grant Likelydd27dcd2010-06-08 07:48:12 -0600341 sizeof(env->buf) - env->buflen);
342 if (sl >= (sizeof(env->buf) - env->buflen))
343 return -ENOMEM;
344 env->buflen += sl;
345
346 return 0;
347}
Stephen Boyd7a3b7cd2016-12-28 14:56:48 -0800348EXPORT_SYMBOL_GPL(of_device_uevent_modalias);