source: rtems-libbsd/rtemsbsd/sys/powerpc/compat.c @ 81fc57d

55-freebsd-126-freebsd-12
Last change on this file since 81fc57d was 81fc57d, checked in by Sebastian Huber <sebastian.huber@…>, on 01/17/18 at 13:14:30

linux/of.h: Add of_find_node_by_path()

Update #3277.

  • Property mode set to 100644
File size: 9.7 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2#include <rtems/bsd/local/opt_dpaa.h>
3
4/*
5 * Copyright (c) 2015 embedded brains GmbH
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <linux/slab.h>
31
32MALLOC_DEFINE(M_KMALLOC, "kmalloc", "Linux kmalloc compatibility");
33
34#include <bsp/fdt.h>
35
36#include <linux/of.h>
37
38const void *
39of_get_property(const struct device_node *dn, const char *name, int *len)
40{
41        const void *fdt = bsp_fdt_get();
42
43        return (fdt_getprop(fdt, dn->offset, name, len));
44}
45
46int
47of_property_read_u32_array(const struct device_node *dn, const char *name,
48    u32 *vals, size_t n)
49{
50        const u32 *prop_vals;
51        int len;
52
53        prop_vals = of_get_property(dn, name, &len);
54        if (prop_vals == NULL) {
55                return (-EINVAL);
56        }
57
58        if (len != n * sizeof(*vals)) {
59                return (-EOVERFLOW);
60        }
61
62        while (n > 0) {
63                *vals = fdt32_to_cpu(*prop_vals);
64                ++vals;
65                ++prop_vals;
66                --n;
67        }
68
69        return (0);
70}
71
72bool
73of_device_is_available(const struct device_node *dn)
74{
75        const char *status;
76        int len;
77
78        status = of_get_property(dn, "status", &len);
79        return (status == NULL ||
80            (len > 0 && (strcmp(status, "okay") == 0 ||
81            strcmp(status, "ok") == 0)));
82}
83
84int
85of_device_is_compatible(const struct device_node *dn, const char *name)
86{
87        const void *fdt = bsp_fdt_get();
88
89        return (fdt_node_check_compatible(fdt, dn->offset, name) == 0);
90}
91
92struct device_node *
93of_find_node_by_path(struct device_node *dns, const char *path)
94{
95        const void *fdt = bsp_fdt_get();
96        int node;
97
98        memset(dns, 0, sizeof(*dns));
99
100        node = fdt_path_offset(fdt, path);
101        if (node < 0)
102                return (NULL);
103
104        dns->offset = node;
105        return (dns);
106}
107
108struct device_node *
109of_find_compatible_node(struct device_node *dns, const struct device_node *dn,
110    const char *type, const char *compatible)
111{
112        const void *fdt = bsp_fdt_get();
113        int node;
114
115        (void)type;
116
117        if (dn != NULL) {
118                node = dn->offset;
119        } else {
120                node = 0;
121        }
122
123        memset(dns, 0, sizeof(*dns));
124
125        while (1) {
126                int err;
127
128                node = fdt_next_node(fdt, node, NULL);
129                if (node < 0)
130                        return (NULL);
131
132                err = fdt_node_check_compatible(fdt, node, compatible);
133                if (err == 0) {
134                        dns->offset = node;
135                        return (dns);
136                }
137        }
138}
139
140uint64_t
141of_read_number(const uint32_t *cell, int size)
142{
143        uint64_t number;
144
145        number = 0;
146
147        while (size > 0) {
148                number = (number << 32) | fdt32_to_cpu(*cell);
149                ++cell;
150                --size;
151        }
152
153        return (number);
154}
155
156struct device_node *
157of_parse_phandle(struct device_node *dns, struct device_node *dn,
158    const char *phandle_name, int index)
159{
160        const void *fdt = bsp_fdt_get();
161        const fdt32_t *phandle;
162        int node;
163        int len;
164
165        phandle = fdt_getprop(fdt, dn->offset, phandle_name, &len);
166        if (phandle == NULL || (len / (int) sizeof(*phandle)) <= index) {
167                return (NULL);
168        }
169
170        node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(phandle[index]));
171        if (node < 0) {
172                return (NULL);
173        }
174
175        dns->offset = node;
176        dns->full_name = NULL;
177        return (dns);
178}
179
180int
181of_count_phandle_with_args(struct device_node *dn, const char *list_name,
182    const char *cells_name)
183{
184        const void *fdt = bsp_fdt_get();
185        const fdt32_t *phandle;
186        int len;
187
188        BSD_ASSERT(cells_name == NULL);
189
190        phandle = fdt_getprop(fdt, dn->offset, list_name, &len);
191        if (phandle == NULL) {
192                return (-ENOENT);
193        }
194
195        return (len / (int)sizeof(*phandle));
196}
197
198#include <linux/of_address.h>
199#include <linux/of_irq.h>
200
201static int
202get_cells(const void *fdt, int node, const char *name)
203{
204        const fdt32_t *c;
205        int len;
206        int val;
207
208        do {
209                c = fdt_getprop(fdt, node, name, &len);
210                if (c != NULL) {
211                        if (len != sizeof(*c))
212                                return (-EINVAL);
213                        val = fdt32_to_cpu(*c);
214                        if (val <= 0 ||
215                            val > sizeof(resource_size_t) / sizeof(*c))
216                                return (-EINVAL);
217                        return (val);
218                }
219                node = fdt_parent_offset(fdt, node);
220        } while (node >= 0);
221
222        return (-EINVAL);
223}
224
225static int
226get_address_cells(const void *fdt, int node)
227{
228
229        return (get_cells(fdt, node, "#address-cells"));
230}
231
232static int
233get_size_cells(const void *fdt, int node)
234{
235
236        return (get_cells(fdt, node, "#size-cells"));
237}
238
239int
240of_n_addr_cells(struct device_node *dn)
241{
242
243        return (get_address_cells(bsp_fdt_get(), dn->offset));
244}
245
246int
247of_n_size_cells(struct device_node *dn)
248{
249
250        return (get_size_cells(bsp_fdt_get(), dn->offset));
251}
252
253int
254of_address_to_resource(struct device_node *dn, int index,
255    struct resource *res)
256{
257        const void *fdt = bsp_fdt_get();
258        int ac;
259        int sc;
260        int len;
261        const fdt32_t *p;
262        int i;
263
264        memset(res, 0, sizeof(*res));
265
266        ac = get_address_cells(fdt, dn->offset);
267        if (ac < 0)
268                return (-EINVAL);
269
270        sc = get_size_cells(fdt, dn->offset);
271        if (sc < 0)
272                return (-EINVAL);
273
274        p = fdt_getprop(fdt, dn->offset, "reg", &len);
275        if (p == NULL)
276                return (-EINVAL);
277
278        len /= sizeof(*p);
279        i = index * (ac + sc);
280        if (i + ac + sc > len)
281                return (-EINVAL);
282
283        while (ac > 0) {
284                res->start = (res->start << 32) | fdt32_to_cpu(p[i]);
285                ++i;
286                --ac;
287        }
288
289        while (sc > 0) {
290                res->end = (res->end << 32) | fdt32_to_cpu(p[i]);
291                ++i;
292                --sc;
293        }
294        res->end += res->start;
295
296        return (0);
297}
298
299int
300of_irq_to_resource(struct device_node *dn, int index,
301    struct resource *res)
302{
303        const void *fdt = bsp_fdt_get();
304        int len;
305        const fdt32_t *p;
306        int i;
307        int irq;
308
309        if (res != NULL)
310                memset(res, 0, sizeof(*res));
311
312        p = fdt_getprop(fdt, dn->offset, "interrupts", &len);
313        if (p == NULL)
314                return (-EINVAL);
315
316        i = index * 16;
317        if (i + 16 > len)
318                return (-EINVAL);
319
320        irq = (int)fdt32_to_cpu(p[i / sizeof(*p)]);
321#ifdef __PPC__
322        /* FIXME */
323        irq -= 16;
324#endif
325
326        if (res != NULL) {
327                res->start = irq;
328                res->end = irq;
329        }
330
331        return (irq);
332}
333
334#include <linux/of_net.h>
335#include <linux/if_ether.h>
336#include <linux/phy.h>
337
338static const char * const phy_modes[] = {
339        [PHY_INTERFACE_MODE_MII]        = "mii",
340        [PHY_INTERFACE_MODE_GMII]       = "gmii",
341        [PHY_INTERFACE_MODE_SGMII]      = "sgmii",
342        [PHY_INTERFACE_MODE_TBI]        = "tbi",
343        [PHY_INTERFACE_MODE_REVMII]     = "rev-mii",
344        [PHY_INTERFACE_MODE_RMII]       = "rmii",
345        [PHY_INTERFACE_MODE_RGMII]      = "rgmii",
346        [PHY_INTERFACE_MODE_RGMII_ID]   = "rgmii-id",
347        [PHY_INTERFACE_MODE_RGMII_RXID] = "rgmii-rxid",
348        [PHY_INTERFACE_MODE_RGMII_TXID] = "rgmii-txid",
349        [PHY_INTERFACE_MODE_RTBI]       = "rtbi",
350        [PHY_INTERFACE_MODE_SMII]       = "smii",
351        [PHY_INTERFACE_MODE_XGMII]      = "xgmii",
352        [PHY_INTERFACE_MODE_MOCA]       = "moca",
353        [PHY_INTERFACE_MODE_QSGMII]     = "qsgmii"
354};
355
356int
357of_get_phy_mode(struct device_node *dn)
358{
359        const void *fdt = bsp_fdt_get();
360        int len;
361        const char *p;
362        int i;
363
364        p = fdt_getprop(fdt, dn->offset, "phy-mode", &len);
365
366        if (p == NULL) {
367                p = fdt_getprop(fdt, dn->offset, "phy-connection-type", &len);
368        }
369
370        if (p == NULL) {
371                return (-ENODEV);
372        }
373
374        for (i = 0; i < ARRAY_SIZE(phy_modes); i++) {
375                if (phy_modes[i] != NULL && strcmp(p, phy_modes[i]) == 0) {
376                        return (i);
377                }
378        }
379
380        return (-ENODEV);
381}
382
383static const void *
384get_mac_address(struct device_node *dn, const char *name)
385{
386        const void *fdt = bsp_fdt_get();
387        int len;
388        const fdt32_t *p;
389
390        p = fdt_getprop(fdt, dn->offset, name, &len);
391        if (p == NULL || len != ETH_ALEN) {
392                return (NULL);
393        }
394
395        return (p);
396}
397
398const void *
399of_get_mac_address(struct device_node *dn)
400{
401        const void *addr;
402
403        addr = get_mac_address(dn, "mac-address");
404        if (addr != NULL) {
405                return addr;
406        }
407
408        return get_mac_address(dn, "local-mac-address");
409}
410
411#include <linux/interrupt.h>
412
413struct arg_wrapper {
414        irq_handler_t handler;
415        unsigned int irq;
416        void *arg;
417};
418
419static void
420handler_wrapper(void *arg)
421{
422        struct arg_wrapper *aw = arg;
423
424        (*aw->handler)(aw->irq, aw->arg);
425}
426
427int __must_check
428request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
429    const char *name, void *arg)
430{
431        struct arg_wrapper *aw;
432        rtems_status_code sc;
433
434        aw = kmalloc(sizeof(*aw), GFP_KERNEL);
435        if (aw == NULL)
436                return (-ENOMEM);
437
438        aw->handler = handler;
439        aw->irq = irq;
440        aw->arg = arg;
441        sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE, irq, name,
442            RTEMS_INTERRUPT_SHARED, handler_wrapper, aw);
443        if (sc != RTEMS_SUCCESSFUL)
444                return (-EINVAL);
445
446        return (0);
447}
448
449#include <linux/bitrev.h>
450
451const uint8_t bitrev_nibbles[16] = {
452        0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
453};
454
455#include <linux/platform_device.h>
456
457struct resource *
458platform_get_resource(struct resource *res, struct platform_device *pdev,
459    unsigned int type, unsigned int num)
460{
461        struct device_node *dn;
462        int ret;
463
464        dn = pdev->dev.of_node;
465
466        switch (type) {
467        case IORESOURCE_MEM:
468                ret = of_address_to_resource(dn, num, res);
469                if (ret == 0)
470                        return res;
471        case IORESOURCE_IRQ:
472                ret = of_irq_to_resource(dn, num, res);
473                if (ret >= 0)
474                        return res;
475        default:
476                break;
477        }
478
479        return (NULL);
480}
481
482int platform_get_irq(struct platform_device *pdev, unsigned int num)
483{
484        struct resource res_storage;
485        struct resource *res;
486
487        res = platform_get_resource(&res_storage, pdev, IORESOURCE_IRQ, num);
488        return (res != NULL ? res->start : -ENXIO);
489}
Note: See TracBrowser for help on using the repository browser.