summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/sys/powerpc/compat.c
blob: c0fdebbd5f38d9818da04b8ba993ade076e94b79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include <machine/rtems-bsd-kernel-space.h>
#include <rtems/bsd/local/opt_dpaa.h>

/*
 * Copyright (c) 2015 embedded brains GmbH
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <linux/slab.h>

MALLOC_DEFINE(M_KMALLOC, "kmalloc", "Linux kmalloc compatibility");

#include <bsp/fdt.h>

#include <linux/of.h>

const void *
of_get_property(const struct device_node *dn, const char *name, int *len)
{
	const void *fdt = bsp_fdt_get();

	return (fdt_getprop(fdt, dn->offset, name, len));
}

int
of_property_read_u32_array(const struct device_node *dn, const char *name,
    u32 *vals, size_t n)
{
	const u32 *prop_vals;
	int len;

	prop_vals = of_get_property(dn, name, &len);
	if (prop_vals == NULL) {
		return (-EINVAL);
	}

	if (len != n * sizeof(*vals)) {
		return (-EOVERFLOW);
	}

	while (n > 0) {
		*vals = fdt32_to_cpu(*prop_vals);
		++vals;
		++prop_vals;
		--n;
	}

	return (0);
}

bool
of_device_is_available(const struct device_node *dn)
{
	const char *status;
	int len;

	status = of_get_property(dn, "status", &len);
	return (status == NULL ||
	    (len > 0 && (strcmp(status, "okay") == 0 ||
	    strcmp(status, "ok") == 0)));
}

int
of_device_is_compatible(const struct device_node *dn, const char *name)
{
	const void *fdt = bsp_fdt_get();

	return (fdt_node_check_compatible(fdt, dn->offset, name) == 0);
}

struct device_node *
of_find_node_by_path(struct device_node *dns, const char *path)
{
	const void *fdt = bsp_fdt_get();
	int node;

	memset(dns, 0, sizeof(*dns));

	node = fdt_path_offset(fdt, path);
	if (node < 0)
		return (NULL);

	dns->offset = node;
	return (dns);
}

struct device_node *
of_find_compatible_node(struct device_node *dns, const struct device_node *dn,
    const char *type, const char *compatible)
{
	const void *fdt = bsp_fdt_get();
	int node;

	(void)type;

	if (dn != NULL) {
		node = dn->offset;
	} else {
		node = 0;
	}

	memset(dns, 0, sizeof(*dns));

	while (1) {
		int err;

		node = fdt_next_node(fdt, node, NULL);
		if (node < 0)
			return (NULL);

		err = fdt_node_check_compatible(fdt, node, compatible);
		if (err == 0) {
			dns->offset = node;
			return (dns);
		}
	}
}

uint64_t
of_read_number(const uint32_t *cell, int size)
{
	uint64_t number;

	number = 0;

	while (size > 0) {
		number = (number << 32) | fdt32_to_cpu(*cell);
		++cell;
		--size;
	}

	return (number);
}

struct device_node *
of_parse_phandle(struct device_node *dns, struct device_node *dn,
    const char *phandle_name, int index)
{
	const void *fdt = bsp_fdt_get();
	const fdt32_t *phandle;
	int node;
	int len;

	phandle = fdt_getprop(fdt, dn->offset, phandle_name, &len);
	if (phandle == NULL || (len / (int) sizeof(*phandle)) <= index) {
		return (NULL);
	}

	node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(phandle[index]));
	if (node < 0) {
		return (NULL);
	}

	dns->offset = node;
	dns->full_name = NULL;
	return (dns);
}

int
of_count_phandle_with_args(struct device_node *dn, const char *list_name,
    const char *cells_name)
{
	const void *fdt = bsp_fdt_get();
	const fdt32_t *phandle;
	int len;

	BSD_ASSERT(cells_name == NULL);

	phandle = fdt_getprop(fdt, dn->offset, list_name, &len);
	if (phandle == NULL) {
		return (-ENOENT);
	}

	return (len / (int)sizeof(*phandle));
}

#include <linux/of_address.h>
#include <linux/of_irq.h>

static int
get_cells(const void *fdt, int node, const char *name)
{
	const fdt32_t *c;
	int len;
	int val;

	do {
		c = fdt_getprop(fdt, node, name, &len);
		if (c != NULL) {
			if (len != sizeof(*c))
				return (-EINVAL);
			val = fdt32_to_cpu(*c);
			if (val <= 0 ||
			    val > sizeof(resource_size_t) / sizeof(*c))
				return (-EINVAL);
			return (val);
		}
		node = fdt_parent_offset(fdt, node);
	} while (node >= 0);

	return (-EINVAL);
}

static int
get_address_cells(const void *fdt, int node)
{

	return (get_cells(fdt, node, "#address-cells"));
}

static int
get_size_cells(const void *fdt, int node)
{

	return (get_cells(fdt, node, "#size-cells"));
}

int
of_n_addr_cells(struct device_node *dn)
{

	return (get_address_cells(bsp_fdt_get(), dn->offset));
}

int
of_n_size_cells(struct device_node *dn)
{

	return (get_size_cells(bsp_fdt_get(), dn->offset));
}

int
of_address_to_resource(struct device_node *dn, int index,
    struct resource *res)
{
	const void *fdt = bsp_fdt_get();
	int ac;
	int sc;
	int len;
	const fdt32_t *p;
	int i;

	memset(res, 0, sizeof(*res));

	ac = get_address_cells(fdt, dn->offset);
	if (ac < 0)
		return (-EINVAL);

	sc = get_size_cells(fdt, dn->offset);
	if (sc < 0)
		return (-EINVAL);

	p = fdt_getprop(fdt, dn->offset, "reg", &len);
	if (p == NULL)
		return (-EINVAL);

	len /= sizeof(*p);
	i = index * (ac + sc);
	if (i + ac + sc > len)
		return (-EINVAL);

	while (ac > 0) {
		res->start = (res->start << 32) | fdt32_to_cpu(p[i]);
		++i;
		--ac;
	}

	while (sc > 0) {
		res->end = (res->end << 32) | fdt32_to_cpu(p[i]);
		++i;
		--sc;
	}
	res->end += res->start;

	return (0);
}

int
of_irq_to_resource(struct device_node *dn, int index,
    struct resource *res)
{
	const void *fdt = bsp_fdt_get();
	int len;
	const fdt32_t *p;
	int i;
	int irq;

	if (res != NULL)
		memset(res, 0, sizeof(*res));

	p = fdt_getprop(fdt, dn->offset, "interrupts", &len);
	if (p == NULL)
		return (-EINVAL);

	i = index * 16;
	if (i + 16 > len)
		return (-EINVAL);

	irq = (int)fdt32_to_cpu(p[i / sizeof(*p)]);
#ifdef __PPC__
	/* FIXME */
	irq -= 16;
#endif

	if (res != NULL) {
		res->start = irq;
		res->end = irq;
	}

	return (irq);
}

#include <linux/of_net.h>
#include <linux/if_ether.h>
#include <linux/phy.h>

static const char * const phy_modes[] = {
	[PHY_INTERFACE_MODE_MII]	= "mii",
	[PHY_INTERFACE_MODE_GMII]	= "gmii",
	[PHY_INTERFACE_MODE_SGMII]	= "sgmii",
	[PHY_INTERFACE_MODE_TBI]	= "tbi",
	[PHY_INTERFACE_MODE_REVMII]	= "rev-mii",
	[PHY_INTERFACE_MODE_RMII]	= "rmii",
	[PHY_INTERFACE_MODE_RGMII]	= "rgmii",
	[PHY_INTERFACE_MODE_RGMII_ID]	= "rgmii-id",
	[PHY_INTERFACE_MODE_RGMII_RXID]	= "rgmii-rxid",
	[PHY_INTERFACE_MODE_RGMII_TXID]	= "rgmii-txid",
	[PHY_INTERFACE_MODE_RTBI]	= "rtbi",
	[PHY_INTERFACE_MODE_SMII]	= "smii",
	[PHY_INTERFACE_MODE_XGMII]	= "xgmii",
	[PHY_INTERFACE_MODE_MOCA]	= "moca",
	[PHY_INTERFACE_MODE_QSGMII]	= "qsgmii"
};

int
of_get_phy_mode(struct device_node *dn)
{
	const void *fdt = bsp_fdt_get();
	int len;
	const char *p;
	int i;

	p = fdt_getprop(fdt, dn->offset, "phy-mode", &len);

	if (p == NULL) {
		p = fdt_getprop(fdt, dn->offset, "phy-connection-type", &len);
	}

	if (p == NULL) {
		return (-ENODEV);
	}

	for (i = 0; i < ARRAY_SIZE(phy_modes); i++) {
		if (phy_modes[i] != NULL && strcmp(p, phy_modes[i]) == 0) {
			return (i);
		}
	}

	return (-ENODEV);
}

static const void *
get_mac_address(struct device_node *dn, const char *name)
{
	const void *fdt = bsp_fdt_get();
	int len;
	const fdt32_t *p;

	p = fdt_getprop(fdt, dn->offset, name, &len);
	if (p == NULL || len != ETH_ALEN) {
		return (NULL);
	}

	return (p);
}

const void *
of_get_mac_address(struct device_node *dn)
{
	const void *addr;

	addr = get_mac_address(dn, "mac-address");
	if (addr != NULL) {
		return addr;
	}

	return get_mac_address(dn, "local-mac-address");
}

#include <linux/interrupt.h>

struct arg_wrapper {
	irq_handler_t handler;
	unsigned int irq;
	void *arg;
};

static void
handler_wrapper(void *arg)
{
	struct arg_wrapper *aw = arg;

	(*aw->handler)(aw->irq, aw->arg);
}

int __must_check
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
    const char *name, void *arg)
{
	struct arg_wrapper *aw;
	rtems_status_code sc;

	aw = kmalloc(sizeof(*aw), GFP_KERNEL);
	if (aw == NULL)
		return (-ENOMEM);

	aw->handler = handler;
	aw->irq = irq;
	aw->arg = arg;
	sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE, irq, name,
	    RTEMS_INTERRUPT_SHARED, handler_wrapper, aw);
	if (sc != RTEMS_SUCCESSFUL)
		return (-EINVAL);

	return (0);
}

#include <linux/bitrev.h>

const uint8_t bitrev_nibbles[16] = {
	0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
};

#include <linux/platform_device.h>

struct resource *
platform_get_resource(struct resource *res, struct platform_device *pdev,
    unsigned int type, unsigned int num)
{
	struct device_node *dn;
	int ret;

	dn = pdev->dev.of_node;

	switch (type) {
	case IORESOURCE_MEM:
		ret = of_address_to_resource(dn, num, res);
		if (ret == 0)
			return res;
	case IORESOURCE_IRQ:
		ret = of_irq_to_resource(dn, num, res);
		if (ret >= 0)
			return res;
	default:
		break;
	}

	return (NULL);
}

int platform_get_irq(struct platform_device *pdev, unsigned int num)
{
	struct resource res_storage;
	struct resource *res;

	res = platform_get_resource(&res_storage, pdev, IORESOURCE_IRQ, num);
	return (res != NULL ? res->start : -ENXIO);
}