New upstream version 18.08
[deb_dpdk.git] / drivers / bus / dpaa / base / fman / of.c
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2010-2016 Freescale Semiconductor Inc.
4  * Copyright 2017 NXP
5  *
6  */
7
8 #include <of.h>
9 #include <rte_dpaa_logs.h>
10
11 static int alive;
12 static struct dt_dir root_dir;
13 static const char *base_dir;
14 static COMPAT_LIST_HEAD(linear);
15
16 static int
17 of_open_dir(const char *relative_path, struct dirent ***d)
18 {
19         int ret;
20         char full_path[PATH_MAX];
21
22         snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
23         ret = scandir(full_path, d, 0, versionsort);
24         if (ret < 0)
25                 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
26                              full_path);
27         return ret;
28 }
29
30 static void
31 of_close_dir(struct dirent **d, int num)
32 {
33         while (num--)
34                 free(d[num]);
35         free(d);
36 }
37
38 static int
39 of_open_file(const char *relative_path)
40 {
41         int ret;
42         char full_path[PATH_MAX];
43
44         snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
45         ret = open(full_path, O_RDONLY);
46         if (ret < 0)
47                 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
48                              full_path);
49         return ret;
50 }
51
52 static void
53 process_file(struct dirent *dent, struct dt_dir *parent)
54 {
55         int fd;
56         struct dt_file *f = malloc(sizeof(*f));
57
58         if (!f) {
59                 DPAA_BUS_LOG(DEBUG, "Unable to allocate memory for file node");
60                 return;
61         }
62         f->node.is_file = 1;
63         snprintf(f->node.node.name, NAME_MAX, "%s", dent->d_name);
64         snprintf(f->node.node.full_name, PATH_MAX, "%s/%s",
65                  parent->node.node.full_name, dent->d_name);
66         f->parent = parent;
67         fd = of_open_file(f->node.node.full_name);
68         if (fd < 0) {
69                 DPAA_BUS_LOG(DEBUG, "Unable to open file node");
70                 free(f);
71                 return;
72         }
73         f->len = read(fd, f->buf, OF_FILE_BUF_MAX);
74         close(fd);
75         if (f->len < 0) {
76                 DPAA_BUS_LOG(DEBUG, "Unable to read file node");
77                 free(f);
78                 return;
79         }
80         list_add_tail(&f->node.list, &parent->files);
81 }
82
83 static const struct dt_dir *
84 node2dir(const struct device_node *n)
85 {
86         struct dt_node *dn = container_of((struct device_node *)n,
87                                           struct dt_node, node);
88         const struct dt_dir *d = container_of(dn, struct dt_dir, node);
89
90         assert(!dn->is_file);
91         return d;
92 }
93
94 /* process_dir() calls iterate_dir(), but the latter will also call the former
95  * when recursing into sub-directories, so a predeclaration is needed.
96  */
97 static int process_dir(const char *relative_path, struct dt_dir *dt);
98
99 static int
100 iterate_dir(struct dirent **d, int num, struct dt_dir *dt)
101 {
102         int loop;
103         /* Iterate the directory contents */
104         for (loop = 0; loop < num; loop++) {
105                 struct dt_dir *subdir;
106                 int ret;
107                 /* Ignore dot files of all types (especially "..") */
108                 if (d[loop]->d_name[0] == '.')
109                         continue;
110                 switch (d[loop]->d_type) {
111                 case DT_REG:
112                         process_file(d[loop], dt);
113                         break;
114                 case DT_DIR:
115                         subdir = malloc(sizeof(*subdir));
116                         if (!subdir) {
117                                 perror("malloc");
118                                 return -ENOMEM;
119                         }
120                         snprintf(subdir->node.node.name, NAME_MAX, "%s",
121                                  d[loop]->d_name);
122                         snprintf(subdir->node.node.full_name, PATH_MAX,
123                                  "%s/%s", dt->node.node.full_name,
124                                  d[loop]->d_name);
125                         subdir->parent = dt;
126                         ret = process_dir(subdir->node.node.full_name, subdir);
127                         if (ret)
128                                 return ret;
129                         list_add_tail(&subdir->node.list, &dt->subdirs);
130                         break;
131                 default:
132                         DPAA_BUS_LOG(DEBUG, "Ignoring invalid dt entry %s/%s",
133                                      dt->node.node.full_name, d[loop]->d_name);
134                 }
135         }
136         return 0;
137 }
138
139 static int
140 process_dir(const char *relative_path, struct dt_dir *dt)
141 {
142         struct dirent **d;
143         int ret, num;
144
145         dt->node.is_file = 0;
146         INIT_LIST_HEAD(&dt->subdirs);
147         INIT_LIST_HEAD(&dt->files);
148         ret = of_open_dir(relative_path, &d);
149         if (ret < 0)
150                 return ret;
151         num = ret;
152         ret = iterate_dir(d, num, dt);
153         of_close_dir(d, num);
154         return (ret < 0) ? ret : 0;
155 }
156
157 static void
158 linear_dir(struct dt_dir *d)
159 {
160         struct dt_file *f;
161         struct dt_dir *dd;
162
163         d->compatible = NULL;
164         d->status = NULL;
165         d->lphandle = NULL;
166         d->a_cells = NULL;
167         d->s_cells = NULL;
168         d->reg = NULL;
169         list_for_each_entry(f, &d->files, node.list) {
170                 if (!strcmp(f->node.node.name, "compatible")) {
171                         if (d->compatible)
172                                 DPAA_BUS_LOG(DEBUG, "Duplicate compatible in"
173                                              " %s", d->node.node.full_name);
174                         d->compatible = f;
175                 } else if (!strcmp(f->node.node.name, "status")) {
176                         if (d->status)
177                                 DPAA_BUS_LOG(DEBUG, "Duplicate status in %s",
178                                              d->node.node.full_name);
179                         d->status = f;
180                 } else if (!strcmp(f->node.node.name, "linux,phandle")) {
181                         if (d->lphandle)
182                                 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
183                                              d->node.node.full_name);
184                         d->lphandle = f;
185                 } else if (!strcmp(f->node.node.name, "phandle")) {
186                         if (d->lphandle)
187                                 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
188                                              d->node.node.full_name);
189                         d->lphandle = f;
190                 } else if (!strcmp(f->node.node.name, "#address-cells")) {
191                         if (d->a_cells)
192                                 DPAA_BUS_LOG(DEBUG, "Duplicate a_cells in %s",
193                                              d->node.node.full_name);
194                         d->a_cells = f;
195                 } else if (!strcmp(f->node.node.name, "#size-cells")) {
196                         if (d->s_cells)
197                                 DPAA_BUS_LOG(DEBUG, "Duplicate s_cells in %s",
198                                              d->node.node.full_name);
199                         d->s_cells = f;
200                 } else if (!strcmp(f->node.node.name, "reg")) {
201                         if (d->reg)
202                                 DPAA_BUS_LOG(DEBUG, "Duplicate reg in %s",
203                                              d->node.node.full_name);
204                         d->reg = f;
205                 }
206         }
207
208         list_for_each_entry(dd, &d->subdirs, node.list) {
209                 list_add_tail(&dd->linear, &linear);
210                 linear_dir(dd);
211         }
212 }
213
214 int
215 of_init_path(const char *dt_path)
216 {
217         int ret;
218
219         base_dir = dt_path;
220
221         /* This needs to be singleton initialization */
222         DPAA_BUS_HWWARN(alive, "Double-init of device-tree driver!");
223
224         /* Prepare root node (the remaining fields are set in process_dir()) */
225         root_dir.node.node.name[0] = '\0';
226         root_dir.node.node.full_name[0] = '\0';
227         INIT_LIST_HEAD(&root_dir.node.list);
228         root_dir.parent = NULL;
229
230         /* Kick things off... */
231         ret = process_dir("", &root_dir);
232         if (ret) {
233                 DPAA_BUS_LOG(ERR, "Unable to parse device tree");
234                 return ret;
235         }
236
237         /* Now make a flat, linear list of directories */
238         linear_dir(&root_dir);
239         alive = 1;
240         return 0;
241 }
242
243 static void
244 destroy_dir(struct dt_dir *d)
245 {
246         struct dt_file *f, *tmpf;
247         struct dt_dir *dd, *tmpd;
248
249         list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
250                 list_del(&f->node.list);
251                 free(f);
252         }
253         list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
254                 destroy_dir(dd);
255                 list_del(&dd->node.list);
256                 free(dd);
257         }
258 }
259
260 void
261 of_finish(void)
262 {
263         DPAA_BUS_HWWARN(!alive, "Double-finish of device-tree driver!");
264
265         destroy_dir(&root_dir);
266         INIT_LIST_HEAD(&linear);
267         alive = 0;
268 }
269
270 static const struct dt_dir *
271 next_linear(const struct dt_dir *f)
272 {
273         if (f->linear.next == &linear)
274                 return NULL;
275         return list_entry(f->linear.next, struct dt_dir, linear);
276 }
277
278 static int
279 check_compatible(const struct dt_file *f, const char *compatible)
280 {
281         const char *c = (char *)f->buf;
282         unsigned int len, remains = f->len;
283
284         while (remains) {
285                 len = strlen(c);
286                 if (!strcmp(c, compatible))
287                         return 1;
288
289                 if (remains < len + 1)
290                         break;
291
292                 c += (len + 1);
293                 remains -= (len + 1);
294         }
295         return 0;
296 }
297
298 const struct device_node *
299 of_find_compatible_node(const struct device_node *from,
300                         const char *type __always_unused,
301                         const char *compatible)
302 {
303         const struct dt_dir *d;
304
305         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
306
307         if (list_empty(&linear))
308                 return NULL;
309         if (!from)
310                 d = list_entry(linear.next, struct dt_dir, linear);
311         else
312                 d = node2dir(from);
313         for (d = next_linear(d); d && (!d->compatible ||
314                                        !check_compatible(d->compatible,
315                                        compatible));
316                         d = next_linear(d))
317                 ;
318         if (d)
319                 return &d->node.node;
320         return NULL;
321 }
322
323 const void *
324 of_get_property(const struct device_node *from, const char *name,
325                 size_t *lenp)
326 {
327         const struct dt_dir *d;
328         const struct dt_file *f;
329
330         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
331
332         d = node2dir(from);
333         list_for_each_entry(f, &d->files, node.list)
334                 if (!strcmp(f->node.node.name, name)) {
335                         if (lenp)
336                                 *lenp = f->len;
337                         return f->buf;
338                 }
339         return NULL;
340 }
341
342 bool
343 of_device_is_available(const struct device_node *dev_node)
344 {
345         const struct dt_dir *d;
346
347         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
348         d = node2dir(dev_node);
349         if (!d->status)
350                 return true;
351         if (!strcmp((char *)d->status->buf, "okay"))
352                 return true;
353         if (!strcmp((char *)d->status->buf, "ok"))
354                 return true;
355         return false;
356 }
357
358 const struct device_node *
359 of_find_node_by_phandle(phandle ph)
360 {
361         const struct dt_dir *d;
362
363         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
364         list_for_each_entry(d, &linear, linear)
365                 if (d->lphandle && (d->lphandle->len == 4) &&
366                     !memcmp(d->lphandle->buf, &ph, 4))
367                         return &d->node.node;
368         return NULL;
369 }
370
371 const struct device_node *
372 of_get_parent(const struct device_node *dev_node)
373 {
374         const struct dt_dir *d;
375
376         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
377
378         if (!dev_node)
379                 return NULL;
380         d = node2dir(dev_node);
381         if (!d->parent)
382                 return NULL;
383         return &d->parent->node.node;
384 }
385
386 const struct device_node *
387 of_get_next_child(const struct device_node *dev_node,
388                   const struct device_node *prev)
389 {
390         const struct dt_dir *p, *c;
391
392         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
393
394         if (!dev_node)
395                 return NULL;
396         p = node2dir(dev_node);
397         if (prev) {
398                 c = node2dir(prev);
399                 DPAA_BUS_HWWARN((c->parent != p), "Parent/child mismatch");
400                 if (c->parent != p)
401                         return NULL;
402                 if (c->node.list.next == &p->subdirs)
403                         /* prev was the last child */
404                         return NULL;
405                 c = list_entry(c->node.list.next, struct dt_dir, node.list);
406                 return &c->node.node;
407         }
408         /* Return first child */
409         if (list_empty(&p->subdirs))
410                 return NULL;
411         c = list_entry(p->subdirs.next, struct dt_dir, node.list);
412         return &c->node.node;
413 }
414
415 uint32_t
416 of_n_addr_cells(const struct device_node *dev_node)
417 {
418         const struct dt_dir *d;
419
420         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised");
421         if (!dev_node)
422                 return OF_DEFAULT_NA;
423         d = node2dir(dev_node);
424         while ((d = d->parent))
425                 if (d->a_cells) {
426                         unsigned char *buf =
427                                 (unsigned char *)&d->a_cells->buf[0];
428                         assert(d->a_cells->len == 4);
429                         return ((uint32_t)buf[0] << 24) |
430                                 ((uint32_t)buf[1] << 16) |
431                                 ((uint32_t)buf[2] << 8) |
432                                 (uint32_t)buf[3];
433                 }
434         return OF_DEFAULT_NA;
435 }
436
437 uint32_t
438 of_n_size_cells(const struct device_node *dev_node)
439 {
440         const struct dt_dir *d;
441
442         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
443         if (!dev_node)
444                 return OF_DEFAULT_NA;
445         d = node2dir(dev_node);
446         while ((d = d->parent))
447                 if (d->s_cells) {
448                         unsigned char *buf =
449                                 (unsigned char *)&d->s_cells->buf[0];
450                         assert(d->s_cells->len == 4);
451                         return ((uint32_t)buf[0] << 24) |
452                                 ((uint32_t)buf[1] << 16) |
453                                 ((uint32_t)buf[2] << 8) |
454                                 (uint32_t)buf[3];
455                 }
456         return OF_DEFAULT_NS;
457 }
458
459 const uint32_t *
460 of_get_address(const struct device_node *dev_node, size_t idx,
461                uint64_t *size, uint32_t *flags __rte_unused)
462 {
463         const struct dt_dir *d;
464         const unsigned char *buf;
465         uint32_t na = of_n_addr_cells(dev_node);
466         uint32_t ns = of_n_size_cells(dev_node);
467
468         if (!dev_node)
469                 d = &root_dir;
470         else
471                 d = node2dir(dev_node);
472         if (!d->reg)
473                 return NULL;
474         assert(d->reg->len % ((na + ns) * 4) == 0);
475         assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
476         buf = (const unsigned char *)&d->reg->buf[0];
477         buf += (na + ns) * idx * 4;
478         if (size)
479                 for (*size = 0; ns > 0; ns--, na++)
480                         *size = (*size << 32) +
481                                 (((uint32_t)buf[4 * na] << 24) |
482                                 ((uint32_t)buf[4 * na + 1] << 16) |
483                                 ((uint32_t)buf[4 * na + 2] << 8) |
484                                 (uint32_t)buf[4 * na + 3]);
485         return (const uint32_t *)buf;
486 }
487
488 uint64_t
489 of_translate_address(const struct device_node *dev_node,
490                      const uint32_t *addr)
491 {
492         uint64_t phys_addr, tmp_addr;
493         const struct device_node *parent;
494         const uint32_t *ranges;
495         size_t rlen;
496         uint32_t na, pna;
497
498         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
499         assert(dev_node != NULL);
500
501         na = of_n_addr_cells(dev_node);
502         phys_addr = of_read_number(addr, na);
503
504         dev_node = of_get_parent(dev_node);
505         if (!dev_node)
506                 return 0;
507         else if (node2dir(dev_node) == &root_dir)
508                 return phys_addr;
509
510         do {
511                 pna = of_n_addr_cells(dev_node);
512                 parent = of_get_parent(dev_node);
513                 if (!parent)
514                         return 0;
515
516                 ranges = of_get_property(dev_node, "ranges", &rlen);
517                 /* "ranges" property is missing. Translation breaks */
518                 if (!ranges)
519                         return 0;
520                 /* "ranges" property is empty. Do 1:1 translation */
521                 else if (rlen == 0)
522                         continue;
523                 else
524                         tmp_addr = of_read_number(ranges + na, pna);
525
526                 na = pna;
527                 dev_node = parent;
528                 phys_addr += tmp_addr;
529         } while (node2dir(parent) != &root_dir);
530
531         return phys_addr;
532 }
533
534 bool
535 of_device_is_compatible(const struct device_node *dev_node,
536                         const char *compatible)
537 {
538         const struct dt_dir *d;
539
540         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
541         if (!dev_node)
542                 d = &root_dir;
543         else
544                 d = node2dir(dev_node);
545         if (d->compatible && check_compatible(d->compatible, compatible))
546                 return true;
547         return false;
548 }
549
550 static const void *of_get_mac_addr(const struct device_node *np,
551                 const char *name)
552 {
553         return of_get_property(np, name, NULL);
554 }
555
556 /**
557  * Search the device tree for the best MAC address to use.  'mac-address' is
558  * checked first, because that is supposed to contain to "most recent" MAC
559  * address. If that isn't set, then 'local-mac-address' is checked next,
560  * because that is the default address.  If that isn't set, then the obsolete
561  * 'address' is checked, just in case we're using an old device tree.
562  *
563  * Note that the 'address' property is supposed to contain a virtual address of
564  * the register set, but some DTS files have redefined that property to be the
565  * MAC address.
566  *
567  * All-zero MAC addresses are rejected, because those could be properties that
568  * exist in the device tree, but were not set by U-Boot.  For example, the
569  * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
570  * addresses.  Some older U-Boots only initialized 'local-mac-address'.  In
571  * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
572  * but is all zeros.
573  */
574 const void *of_get_mac_address(const struct device_node *np)
575 {
576         const void *addr;
577
578         addr = of_get_mac_addr(np, "mac-address");
579         if (addr)
580                 return addr;
581
582         addr = of_get_mac_addr(np, "local-mac-address");
583         if (addr)
584                 return addr;
585
586         return of_get_mac_addr(np, "address");
587 }