source: rtems-libbsd/rtemsbsd/sys/powerpc/compat.c @ 0d421d8

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

linux/of.h: Add of_n_addr_cells()

Update #3277.

  • Property mode set to 100644
File size: 9.2 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_compatible_node(struct device_node *dns, const struct device_node *dn,
94    const char *type, const char *compatible)
95{
96        const void *fdt = bsp_fdt_get();
97        int node;
98
99        (void)type;
100
101        if (dn != NULL) {
102                node = dn->offset;
103        } else {
104                node = 0;
105        }
106
107        memset(dns, 0, sizeof(*dns));
108
109        while (1) {
110                int err;
111
112                node = fdt_next_node(fdt, node, NULL);
113                if (node < 0)
114                        return (NULL);
115
116                err = fdt_node_check_compatible(fdt, node, compatible);
117                if (err == 0) {
118                        dns->offset = node;
119                        return (dns);
120                }
121        }
122}
123
124struct device_node *
125of_parse_phandle(struct device_node *dns, struct device_node *dn,
126    const char *phandle_name, int index)
127{
128        const void *fdt = bsp_fdt_get();
129        const fdt32_t *phandle;
130        int node;
131        int len;
132
133        phandle = fdt_getprop(fdt, dn->offset, phandle_name, &len);
134        if (phandle == NULL || (len / (int) sizeof(*phandle)) <= index) {
135                return (NULL);
136        }
137
138        node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(phandle[index]));
139        if (node < 0) {
140                return (NULL);
141        }
142
143        dns->offset = node;
144        dns->full_name = NULL;
145        return (dns);
146}
147
148int
149of_count_phandle_with_args(struct device_node *dn, const char *list_name,
150    const char *cells_name)
151{
152        const void *fdt = bsp_fdt_get();
153        const fdt32_t *phandle;
154        int len;
155
156        BSD_ASSERT(cells_name == NULL);
157
158        phandle = fdt_getprop(fdt, dn->offset, list_name, &len);
159        if (phandle == NULL) {
160                return (-ENOENT);
161        }
162
163        return (len / (int)sizeof(*phandle));
164}
165
166#include <linux/of_address.h>
167#include <linux/of_irq.h>
168
169static int
170get_cells(const void *fdt, int node, const char *name)
171{
172        const fdt32_t *c;
173        int len;
174        int val;
175
176        do {
177                c = fdt_getprop(fdt, node, name, &len);
178                if (c != NULL) {
179                        if (len != sizeof(*c))
180                                return (-EINVAL);
181                        val = fdt32_to_cpu(*c);
182                        if (val <= 0 ||
183                            val > sizeof(resource_size_t) / sizeof(*c))
184                                return (-EINVAL);
185                        return (val);
186                }
187                node = fdt_parent_offset(fdt, node);
188        } while (node >= 0);
189
190        return (-EINVAL);
191}
192
193static int
194get_address_cells(const void *fdt, int node)
195{
196
197        return (get_cells(fdt, node, "#address-cells"));
198}
199
200static int
201get_size_cells(const void *fdt, int node)
202{
203
204        return (get_cells(fdt, node, "#size-cells"));
205}
206
207int
208of_n_addr_cells(struct device_node *dn)
209{
210
211        return (get_address_cells(bsp_fdt_get(), dn->offset));
212}
213
214int
215of_address_to_resource(struct device_node *dn, int index,
216    struct resource *res)
217{
218        const void *fdt = bsp_fdt_get();
219        int ac;
220        int sc;
221        int len;
222        const fdt32_t *p;
223        int i;
224
225        memset(res, 0, sizeof(*res));
226
227        ac = get_address_cells(fdt, dn->offset);
228        if (ac < 0)
229                return (-EINVAL);
230
231        sc = get_size_cells(fdt, dn->offset);
232        if (sc < 0)
233                return (-EINVAL);
234
235        p = fdt_getprop(fdt, dn->offset, "reg", &len);
236        if (p == NULL)
237                return (-EINVAL);
238
239        len /= sizeof(*p);
240        i = index * (ac + sc);
241        if (i + ac + sc > len)
242                return (-EINVAL);
243
244        while (ac > 0) {
245                res->start = (res->start << 32) | fdt32_to_cpu(p[i]);
246                ++i;
247                --ac;
248        }
249
250        while (sc > 0) {
251                res->end = (res->end << 32) | fdt32_to_cpu(p[i]);
252                ++i;
253                --sc;
254        }
255        res->end += res->start;
256
257        return (0);
258}
259
260int
261of_irq_to_resource(struct device_node *dn, int index,
262    struct resource *res)
263{
264        const void *fdt = bsp_fdt_get();
265        int len;
266        const fdt32_t *p;
267        int i;
268        int irq;
269
270        if (res != NULL)
271                memset(res, 0, sizeof(*res));
272
273        p = fdt_getprop(fdt, dn->offset, "interrupts", &len);
274        if (p == NULL)
275                return (-EINVAL);
276
277        i = index * 16;
278        if (i + 16 > len)
279                return (-EINVAL);
280
281        irq = (int)fdt32_to_cpu(p[i / sizeof(*p)]);
282#ifdef __PPC__
283        /* FIXME */
284        irq -= 16;
285#endif
286
287        if (res != NULL) {
288                res->start = irq;
289                res->end = irq;
290        }
291
292        return (irq);
293}
294
295#include <linux/of_net.h>
296#include <linux/if_ether.h>
297#include <linux/phy.h>
298
299static const char * const phy_modes[] = {
300        [PHY_INTERFACE_MODE_MII]        = "mii",
301        [PHY_INTERFACE_MODE_GMII]       = "gmii",
302        [PHY_INTERFACE_MODE_SGMII]      = "sgmii",
303        [PHY_INTERFACE_MODE_TBI]        = "tbi",
304        [PHY_INTERFACE_MODE_REVMII]     = "rev-mii",
305        [PHY_INTERFACE_MODE_RMII]       = "rmii",
306        [PHY_INTERFACE_MODE_RGMII]      = "rgmii",
307        [PHY_INTERFACE_MODE_RGMII_ID]   = "rgmii-id",
308        [PHY_INTERFACE_MODE_RGMII_RXID] = "rgmii-rxid",
309        [PHY_INTERFACE_MODE_RGMII_TXID] = "rgmii-txid",
310        [PHY_INTERFACE_MODE_RTBI]       = "rtbi",
311        [PHY_INTERFACE_MODE_SMII]       = "smii",
312        [PHY_INTERFACE_MODE_XGMII]      = "xgmii",
313        [PHY_INTERFACE_MODE_MOCA]       = "moca",
314        [PHY_INTERFACE_MODE_QSGMII]     = "qsgmii"
315};
316
317int
318of_get_phy_mode(struct device_node *dn)
319{
320        const void *fdt = bsp_fdt_get();
321        int len;
322        const char *p;
323        int i;
324
325        p = fdt_getprop(fdt, dn->offset, "phy-mode", &len);
326
327        if (p == NULL) {
328                p = fdt_getprop(fdt, dn->offset, "phy-connection-type", &len);
329        }
330
331        if (p == NULL) {
332                return (-ENODEV);
333        }
334
335        for (i = 0; i < ARRAY_SIZE(phy_modes); i++) {
336                if (phy_modes[i] != NULL && strcmp(p, phy_modes[i]) == 0) {
337                        return (i);
338                }
339        }
340
341        return (-ENODEV);
342}
343
344static const void *
345get_mac_address(struct device_node *dn, const char *name)
346{
347        const void *fdt = bsp_fdt_get();
348        int len;
349        const fdt32_t *p;
350
351        p = fdt_getprop(fdt, dn->offset, name, &len);
352        if (p == NULL || len != ETH_ALEN) {
353                return (NULL);
354        }
355
356        return (p);
357}
358
359const void *
360of_get_mac_address(struct device_node *dn)
361{
362        const void *addr;
363
364        addr = get_mac_address(dn, "mac-address");
365        if (addr != NULL) {
366                return addr;
367        }
368
369        return get_mac_address(dn, "local-mac-address");
370}
371
372#include <linux/interrupt.h>
373
374struct arg_wrapper {
375        irq_handler_t handler;
376        unsigned int irq;
377        void *arg;
378};
379
380static void
381handler_wrapper(void *arg)
382{
383        struct arg_wrapper *aw = arg;
384
385        (*aw->handler)(aw->irq, aw->arg);
386}
387
388int __must_check
389request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
390    const char *name, void *arg)
391{
392        struct arg_wrapper *aw;
393        rtems_status_code sc;
394
395        aw = kmalloc(sizeof(*aw), GFP_KERNEL);
396        if (aw == NULL)
397                return (-ENOMEM);
398
399        aw->handler = handler;
400        aw->irq = irq;
401        aw->arg = arg;
402        sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE, irq, name,
403            RTEMS_INTERRUPT_SHARED, handler_wrapper, aw);
404        if (sc != RTEMS_SUCCESSFUL)
405                return (-EINVAL);
406
407        return (0);
408}
409
410#include <linux/bitrev.h>
411
412const uint8_t bitrev_nibbles[16] = {
413        0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
414};
415
416#include <linux/platform_device.h>
417
418struct resource *
419platform_get_resource(struct resource *res, struct platform_device *pdev,
420    unsigned int type, unsigned int num)
421{
422        struct device_node *dn;
423        int ret;
424
425        dn = pdev->dev.of_node;
426
427        switch (type) {
428        case IORESOURCE_MEM:
429                ret = of_address_to_resource(dn, num, res);
430                if (ret == 0)
431                        return res;
432        case IORESOURCE_IRQ:
433                ret = of_irq_to_resource(dn, num, res);
434                if (ret >= 0)
435                        return res;
436        default:
437                break;
438        }
439
440        return (NULL);
441}
442
443int platform_get_irq(struct platform_device *pdev, unsigned int num)
444{
445        struct resource res_storage;
446        struct resource *res;
447
448        res = platform_get_resource(&res_storage, pdev, IORESOURCE_IRQ, num);
449        return (res != NULL ? res->start : -ENXIO);
450}
Note: See TracBrowser for help on using the repository browser.