source: rtems-libbsd/rtemsbsd/sys/powerpc/compat.c @ 066b536

55-freebsd-126-freebsd-12
Last change on this file since 066b536 was cd089b9, checked in by Sebastian Huber <sebastian.huber@…>, on 05/05/17 at 06:47:39

Linux update to 4.11-rc5

Linux baseline a71c9a1c779f2499fb2afc0553e543f18aff6edf (4.11-rc5).

  • Property mode set to 100644
File size: 9.1 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_address_to_resource(struct device_node *dn, int index,
209    struct resource *res)
210{
211        const void *fdt = bsp_fdt_get();
212        int ac;
213        int sc;
214        int len;
215        const fdt32_t *p;
216        int i;
217
218        memset(res, 0, sizeof(*res));
219
220        ac = get_address_cells(fdt, dn->offset);
221        if (ac < 0)
222                return (-EINVAL);
223
224        sc = get_size_cells(fdt, dn->offset);
225        if (sc < 0)
226                return (-EINVAL);
227
228        p = fdt_getprop(fdt, dn->offset, "reg", &len);
229        if (p == NULL)
230                return (-EINVAL);
231
232        len /= sizeof(*p);
233        i = index * (ac + sc);
234        if (i + ac + sc > len)
235                return (-EINVAL);
236
237        while (ac > 0) {
238                res->start = (res->start << 32) | fdt32_to_cpu(p[i]);
239                ++i;
240                --ac;
241        }
242
243        while (sc > 0) {
244                res->end = (res->end << 32) | fdt32_to_cpu(p[i]);
245                ++i;
246                --sc;
247        }
248        res->end += res->start;
249
250        return (0);
251}
252
253int
254of_irq_to_resource(struct device_node *dn, int index,
255    struct resource *res)
256{
257        const void *fdt = bsp_fdt_get();
258        int len;
259        const fdt32_t *p;
260        int i;
261        int irq;
262
263        if (res != NULL)
264                memset(res, 0, sizeof(*res));
265
266        p = fdt_getprop(fdt, dn->offset, "interrupts", &len);
267        if (p == NULL)
268                return (-EINVAL);
269
270        i = index * 16;
271        if (i + 16 > len)
272                return (-EINVAL);
273
274        irq = (int)fdt32_to_cpu(p[i / sizeof(*p)]);
275#ifdef __PPC__
276        /* FIXME */
277        irq -= 16;
278#endif
279
280        if (res != NULL) {
281                res->start = irq;
282                res->end = irq;
283        }
284
285        return (irq);
286}
287
288#include <linux/of_net.h>
289#include <linux/if_ether.h>
290#include <linux/phy.h>
291
292static const char * const phy_modes[] = {
293        [PHY_INTERFACE_MODE_MII]        = "mii",
294        [PHY_INTERFACE_MODE_GMII]       = "gmii",
295        [PHY_INTERFACE_MODE_SGMII]      = "sgmii",
296        [PHY_INTERFACE_MODE_TBI]        = "tbi",
297        [PHY_INTERFACE_MODE_REVMII]     = "rev-mii",
298        [PHY_INTERFACE_MODE_RMII]       = "rmii",
299        [PHY_INTERFACE_MODE_RGMII]      = "rgmii",
300        [PHY_INTERFACE_MODE_RGMII_ID]   = "rgmii-id",
301        [PHY_INTERFACE_MODE_RGMII_RXID] = "rgmii-rxid",
302        [PHY_INTERFACE_MODE_RGMII_TXID] = "rgmii-txid",
303        [PHY_INTERFACE_MODE_RTBI]       = "rtbi",
304        [PHY_INTERFACE_MODE_SMII]       = "smii",
305        [PHY_INTERFACE_MODE_XGMII]      = "xgmii",
306        [PHY_INTERFACE_MODE_MOCA]       = "moca",
307        [PHY_INTERFACE_MODE_QSGMII]     = "qsgmii"
308};
309
310int
311of_get_phy_mode(struct device_node *dn)
312{
313        const void *fdt = bsp_fdt_get();
314        int len;
315        const char *p;
316        int i;
317
318        p = fdt_getprop(fdt, dn->offset, "phy-mode", &len);
319
320        if (p == NULL) {
321                p = fdt_getprop(fdt, dn->offset, "phy-connection-type", &len);
322        }
323
324        if (p == NULL) {
325                return (-ENODEV);
326        }
327
328        for (i = 0; i < ARRAY_SIZE(phy_modes); i++) {
329                if (phy_modes[i] != NULL && strcmp(p, phy_modes[i]) == 0) {
330                        return (i);
331                }
332        }
333
334        return (-ENODEV);
335}
336
337static const void *
338get_mac_address(struct device_node *dn, const char *name)
339{
340        const void *fdt = bsp_fdt_get();
341        int len;
342        const fdt32_t *p;
343
344        p = fdt_getprop(fdt, dn->offset, name, &len);
345        if (p == NULL || len != ETH_ALEN) {
346                return (NULL);
347        }
348
349        return (p);
350}
351
352const void *
353of_get_mac_address(struct device_node *dn)
354{
355        const void *addr;
356
357        addr = get_mac_address(dn, "mac-address");
358        if (addr != NULL) {
359                return addr;
360        }
361
362        return get_mac_address(dn, "local-mac-address");
363}
364
365#include <linux/interrupt.h>
366
367struct arg_wrapper {
368        irq_handler_t handler;
369        unsigned int irq;
370        void *arg;
371};
372
373static void
374handler_wrapper(void *arg)
375{
376        struct arg_wrapper *aw = arg;
377
378        (*aw->handler)(aw->irq, aw->arg);
379}
380
381int __must_check
382request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
383    const char *name, void *arg)
384{
385        struct arg_wrapper *aw;
386        rtems_status_code sc;
387
388        aw = kmalloc(sizeof(*aw), GFP_KERNEL);
389        if (aw == NULL)
390                return (-ENOMEM);
391
392        aw->handler = handler;
393        aw->irq = irq;
394        aw->arg = arg;
395        sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE, irq, name,
396            RTEMS_INTERRUPT_SHARED, handler_wrapper, aw);
397        if (sc != RTEMS_SUCCESSFUL)
398                return (-EINVAL);
399
400        return (0);
401}
402
403#include <linux/bitrev.h>
404
405const uint8_t bitrev_nibbles[16] = {
406        0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
407};
408
409#include <linux/platform_device.h>
410
411struct resource *
412platform_get_resource(struct resource *res, struct platform_device *pdev,
413    unsigned int type, unsigned int num)
414{
415        struct device_node *dn;
416        int ret;
417
418        dn = pdev->dev.of_node;
419
420        switch (type) {
421        case IORESOURCE_MEM:
422                ret = of_address_to_resource(dn, num, res);
423                if (ret == 0)
424                        return res;
425        case IORESOURCE_IRQ:
426                ret = of_irq_to_resource(dn, num, res);
427                if (ret >= 0)
428                        return res;
429        default:
430                break;
431        }
432
433        return (NULL);
434}
435
436int platform_get_irq(struct platform_device *pdev, unsigned int num)
437{
438        struct resource res_storage;
439        struct resource *res;
440
441        res = platform_get_resource(&res_storage, pdev, IORESOURCE_IRQ, num);
442        return (res != NULL ? res->start : -ENXIO);
443}
Note: See TracBrowser for help on using the repository browser.