xref: /freebsd/sys/dev/fdt/fdt_common.c (revision 4310d6deb27da04d3fe079a0584edd557a764e21)
1 /*-
2  * Copyright (c) 2009-2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Semihalf under sponsorship from
6  * the FreeBSD Foundation.
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 
39 #include <machine/fdt.h>
40 #include <machine/resource.h>
41 
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/ofw/openfirm.h>
46 
47 #include "ofw_bus_if.h"
48 
49 #ifdef DEBUG
50 #define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
51     printf(fmt,##args); } while (0)
52 #else
53 #define debugf(fmt, args...)
54 #endif
55 
56 #define FDT_COMPAT_LEN	255
57 #define FDT_TYPE_LEN	64
58 
59 #define FDT_REG_CELLS	4
60 
61 vm_paddr_t fdt_immr_pa;
62 vm_offset_t fdt_immr_va;
63 vm_offset_t fdt_immr_size;
64 
65 int
66 fdt_immr_addr(vm_offset_t immr_va)
67 {
68 	pcell_t ranges[6], *rangesptr;
69 	phandle_t node;
70 	u_long base, size;
71 	pcell_t addr_cells, size_cells, par_addr_cells;
72 	int len, tuple_size, tuples;
73 
74 	/*
75 	 * Try to access the SOC node directly i.e. through /aliases/.
76 	 */
77 	if ((node = OF_finddevice("soc")) != -1)
78 		if (fdt_is_compatible_strict(node, "simple-bus"))
79 			goto moveon;
80 	/*
81 	 * Find the node the long way.
82 	 */
83 	if ((node = OF_finddevice("/")) == -1)
84 		return (ENXIO);
85 
86 	if ((node = fdt_find_compatible(node, "simple-bus", 1)) == 0)
87 		return (ENXIO);
88 
89 moveon:
90 	if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
91 		return (ENXIO);
92 	/*
93 	 * Process 'ranges' property.
94 	 */
95 	par_addr_cells = fdt_parent_addr_cells(node);
96 	if (par_addr_cells > 2)
97 		return (ERANGE);
98 
99 	len = OF_getproplen(node, "ranges");
100 	if (len > sizeof(ranges))
101 		return (ENOMEM);
102 
103 	if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
104 		return (EINVAL);
105 
106 	tuple_size = sizeof(pcell_t) * (addr_cells + par_addr_cells +
107 	    size_cells);
108 	tuples = len / tuple_size;
109 
110 	if (fdt_ranges_verify(ranges, tuples, par_addr_cells,
111 	    addr_cells, size_cells)) {
112 		return (ERANGE);
113 	}
114 	base = 0;
115 	size = 0;
116 	rangesptr = &ranges[0];
117 
118 	base = fdt_data_get((void *)rangesptr, addr_cells);
119 	rangesptr += addr_cells;
120 	base += fdt_data_get((void *)rangesptr, par_addr_cells);
121 	rangesptr += par_addr_cells;
122 	size = fdt_data_get((void *)rangesptr, size_cells);
123 
124 	fdt_immr_pa = base;
125 	fdt_immr_va = immr_va;
126 	fdt_immr_size = size;
127 
128 	return (0);
129 }
130 
131 /*
132  * This routine is an early-usage version of the ofw_bus_is_compatible() when
133  * the ofw_bus I/F is not available (like early console routines and similar).
134  * Note the buffer has to be on the stack since malloc() is usually not
135  * available in such cases either.
136  */
137 int
138 fdt_is_compatible(phandle_t node, const char *compatstr)
139 {
140 	char buf[FDT_COMPAT_LEN];
141 	char *compat;
142 	int len, onelen, l, rv;
143 
144 	if ((len = OF_getproplen(node, "compatible")) <= 0)
145 		return (0);
146 
147 	compat = (char *)&buf;
148 	bzero(compat, FDT_COMPAT_LEN);
149 
150 	if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
151 		return (0);
152 
153 	onelen = strlen(compatstr);
154 	rv = 0;
155 	while (len > 0) {
156 		if (strncasecmp(compat, compatstr, onelen) == 0) {
157 			/* Found it. */
158 			rv = 1;
159 			break;
160 		}
161 		/* Slide to the next sub-string. */
162 		l = strlen(compat) + 1;
163 		compat += l;
164 		len -= l;
165 	}
166 
167 	return (rv);
168 }
169 
170 int
171 fdt_is_compatible_strict(phandle_t node, const char *compatible)
172 {
173 	char compat[FDT_COMPAT_LEN];
174 
175 	if (OF_getproplen(node, "compatible") <= 0)
176 		return (0);
177 
178 	if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
179 		return (0);
180 
181 	if (strncasecmp(compat, compatible, FDT_COMPAT_LEN) == 0)
182 		/* This fits. */
183 		return (1);
184 
185 	return (0);
186 }
187 
188 phandle_t
189 fdt_find_compatible(phandle_t start, const char *compat, int strict)
190 {
191 	phandle_t child;
192 
193 	/*
194 	 * Traverse all children of 'start' node, and find first with
195 	 * matching 'compatible' property.
196 	 */
197 	for (child = OF_child(start); child != 0; child = OF_peer(child))
198 		if (fdt_is_compatible(child, compat)) {
199 			if (strict)
200 				if (!fdt_is_compatible_strict(child, compat))
201 					continue;
202 			return (child);
203 		}
204 	return (0);
205 }
206 
207 int
208 fdt_is_enabled(phandle_t node)
209 {
210 	char *stat;
211 	int ena, len;
212 
213 	len = OF_getprop_alloc(node, "status", sizeof(char),
214 	    (void **)&stat);
215 
216 	if (len <= 0)
217 		/* It is OK if no 'status' property. */
218 		return (1);
219 
220 	/* Anything other than 'okay' means disabled. */
221 	ena = 0;
222 	if (strncmp((char *)stat, "okay", len) == 0)
223 		ena = 1;
224 
225 	free(stat, M_OFWPROP);
226 	return (ena);
227 }
228 
229 int
230 fdt_is_type(phandle_t node, const char *typestr)
231 {
232 	char type[FDT_TYPE_LEN];
233 
234 	if (OF_getproplen(node, "device_type") <= 0)
235 		return (0);
236 
237 	if (OF_getprop(node, "device_type", type, FDT_TYPE_LEN) < 0)
238 		return (0);
239 
240 	if (strncasecmp(type, typestr, FDT_TYPE_LEN) == 0)
241 		/* This fits. */
242 		return (1);
243 
244 	return (0);
245 }
246 
247 int
248 fdt_parent_addr_cells(phandle_t node)
249 {
250 	pcell_t addr_cells;
251 
252 	/* Find out #address-cells of the superior bus. */
253 	if (OF_searchprop(OF_parent(node), "#address-cells", &addr_cells,
254 	    sizeof(addr_cells)) <= 0)
255 		addr_cells = 2;
256 
257 	return ((int)fdt32_to_cpu(addr_cells));
258 }
259 
260 int
261 fdt_data_verify(void *data, int cells)
262 {
263 	uint64_t d64;
264 
265 	if (cells > 1) {
266 		d64 = fdt64_to_cpu(*((uint64_t *)data));
267 		if (((d64 >> 32) & 0xffffffffull) != 0 || cells > 2)
268 			return (ERANGE);
269 	}
270 
271 	return (0);
272 }
273 
274 int
275 fdt_pm_is_enabled(phandle_t node)
276 {
277 	int ret;
278 
279 	ret = 1;
280 
281 #if defined(SOC_MV_KIRKWOOD) || defined(SOC_MV_DISCOVERY)
282 	ret = fdt_pm(node);
283 #endif
284 	return (ret);
285 }
286 
287 u_long
288 fdt_data_get(void *data, int cells)
289 {
290 
291 	if (cells == 1)
292 		return (fdt32_to_cpu(*((uint32_t *)data)));
293 
294 	return (fdt64_to_cpu(*((uint64_t *)data)));
295 }
296 
297 int
298 fdt_addrsize_cells(phandle_t node, int *addr_cells, int *size_cells)
299 {
300 	pcell_t cell;
301 	int cell_size;
302 
303 	/*
304 	 * Retrieve #{address,size}-cells.
305 	 */
306 	cell_size = sizeof(cell);
307 	if (OF_getprop(node, "#address-cells", &cell, cell_size) < cell_size)
308 		cell = 2;
309 	*addr_cells = fdt32_to_cpu((int)cell);
310 
311 	if (OF_getprop(node, "#size-cells", &cell, cell_size) < cell_size)
312 		cell = 1;
313 	*size_cells = fdt32_to_cpu((int)cell);
314 
315 	if (*addr_cells > 3 || *size_cells > 2)
316 		return (ERANGE);
317 	return (0);
318 }
319 
320 int
321 fdt_ranges_verify(pcell_t *ranges, int tuples, int par_addr_cells,
322     int this_addr_cells, int this_size_cells)
323 {
324 	int i, rv, ulsz;
325 
326 	if (par_addr_cells > 2 || this_addr_cells > 2 || this_size_cells > 2)
327 		return (ERANGE);
328 
329 	/*
330 	 * This is the max size the resource manager can handle for addresses
331 	 * and sizes.
332 	 */
333 	ulsz = sizeof(u_long);
334 	if (par_addr_cells <= ulsz && this_addr_cells <= ulsz &&
335 	    this_size_cells <= ulsz)
336 		/* We can handle everything */
337 		return (0);
338 
339 	rv = 0;
340 	for (i = 0; i < tuples; i++) {
341 
342 		if (fdt_data_verify((void *)ranges, par_addr_cells))
343 			goto err;
344 		ranges += par_addr_cells;
345 
346 		if (fdt_data_verify((void *)ranges, this_addr_cells))
347 			goto err;
348 		ranges += this_addr_cells;
349 
350 		if (fdt_data_verify((void *)ranges, this_size_cells))
351 			goto err;
352 		ranges += this_size_cells;
353 	}
354 
355 	return (0);
356 
357 err:
358 	debugf("using address range >%d-bit not supported\n", ulsz * 8);
359 	return (ERANGE);
360 }
361 
362 int
363 fdt_data_to_res(pcell_t *data, int addr_cells, int size_cells, u_long *start,
364     u_long *count)
365 {
366 
367 	/* Address portion. */
368 	if (fdt_data_verify((void *)data, addr_cells))
369 		return (ERANGE);
370 
371 	*start = fdt_data_get((void *)data, addr_cells);
372 	data += addr_cells;
373 
374 	/* Size portion. */
375 	if (fdt_data_verify((void *)data, size_cells))
376 		return (ERANGE);
377 
378 	*count = fdt_data_get((void *)data, size_cells);
379 	return (0);
380 }
381 
382 int
383 fdt_regsize(phandle_t node, u_long *base, u_long *size)
384 {
385 	pcell_t reg[4];
386 	int addr_cells, len, size_cells;
387 
388 	if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells))
389 		return (ENXIO);
390 
391 	if ((sizeof(pcell_t) * (addr_cells + size_cells)) > sizeof(reg))
392 		return (ENOMEM);
393 
394 	len = OF_getprop(node, "reg", &reg, sizeof(reg));
395 	if (len <= 0)
396 		return (EINVAL);
397 
398 	*base = fdt_data_get(&reg[0], addr_cells);
399 	*size = fdt_data_get(&reg[addr_cells], size_cells);
400 	return (0);
401 }
402 
403 int
404 fdt_reg_to_rl(phandle_t node, struct resource_list *rl, u_long base)
405 {
406 	u_long start, end, count;
407 	pcell_t *reg, *regptr;
408 	pcell_t addr_cells, size_cells;
409 	int tuple_size, tuples;
410 	int i, rv;
411 
412 	if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells) != 0)
413 		return (ENXIO);
414 
415 	tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
416 	tuples = OF_getprop_alloc(node, "reg", tuple_size, (void **)&reg);
417 	debugf("addr_cells = %d, size_cells = %d\n", addr_cells, size_cells);
418 	debugf("tuples = %d, tuple size = %d\n", tuples, tuple_size);
419 	if (tuples <= 0)
420 		/* No 'reg' property in this node. */
421 		return (0);
422 
423 	regptr = reg;
424 	for (i = 0; i < tuples; i++) {
425 
426 		rv = fdt_data_to_res(reg, addr_cells, size_cells, &start,
427 		    &count);
428 		if (rv != 0) {
429 			resource_list_free(rl);
430 			goto out;
431 		}
432 		reg += addr_cells + size_cells;
433 
434 		/* Calculate address range relative to base. */
435 		start &= 0x000ffffful;
436 		start = base + start;
437 		end = start + count - 1;
438 
439 		debugf("reg addr start = %lx, end = %lx, count = %lx\n", start,
440 		    end, count);
441 
442 		resource_list_add(rl, SYS_RES_MEMORY, i, start, end,
443 		    count);
444 	}
445 	rv = 0;
446 
447 out:
448 	free(regptr, M_OFWPROP);
449 	return (rv);
450 }
451 
452 int
453 fdt_intr_decode(phandle_t intr_parent, pcell_t *intr, int *interrupt,
454     int *trig, int *pol)
455 {
456 	fdt_pic_decode_t intr_decode;
457 	int i, rv;
458 
459 	for (i = 0; fdt_pic_table[i] != NULL; i++) {
460 
461 		/* XXX check if pic_handle has interrupt-controller prop? */
462 
463 		intr_decode = fdt_pic_table[i];
464 		rv = intr_decode(intr_parent, intr, interrupt, trig, pol);
465 
466 		if (rv == 0)
467 			/* This was recognized as our PIC and decoded. */
468 			return (0);
469 	}
470 
471 	return (ENXIO);
472 }
473 
474 int
475 fdt_intr_to_rl(phandle_t node, struct resource_list *rl,
476     struct fdt_sense_level *intr_sl)
477 {
478 	phandle_t intr_par;
479 	ihandle_t iph;
480 	pcell_t *intr;
481 	pcell_t intr_cells;
482 	int interrupt, trig, pol;
483 	int i, intr_num, irq, rv;
484 
485 	if (OF_getproplen(node, "interrupts") <= 0)
486 		/* Node does not have 'interrupts' property. */
487 		return (0);
488 
489 	/*
490 	 * Find #interrupt-cells of the interrupt domain.
491 	 */
492 	if (OF_getprop(node, "interrupt-parent", &iph, sizeof(iph)) <= 0) {
493 		debugf("no intr-parent phandle\n");
494 		intr_par = OF_parent(node);
495 	} else {
496 		iph = fdt32_to_cpu(iph);
497 		intr_par = OF_instance_to_package(iph);
498 	}
499 
500 	if (OF_getprop(intr_par, "#interrupt-cells", &intr_cells,
501 	    sizeof(intr_cells)) <= 0) {
502 		debugf("no intr-cells defined, defaulting to 1\n");
503 		intr_cells = 1;
504 	}
505 	intr_cells = fdt32_to_cpu(intr_cells);
506 
507 	intr_num = OF_getprop_alloc(node, "interrupts",
508 	    intr_cells * sizeof(pcell_t), (void **)&intr);
509 	if (intr_num <= 0 || intr_num > DI_MAX_INTR_NUM)
510 		return (ERANGE);
511 
512 	rv = 0;
513 	for (i = 0; i < intr_num; i++) {
514 
515 		interrupt = -1;
516 		trig = pol = 0;
517 
518 		if (fdt_intr_decode(intr_par, &intr[i * intr_cells],
519 		    &interrupt, &trig, &pol) != 0) {
520 			rv = ENXIO;
521 			goto out;
522 		}
523 
524 		if (interrupt < 0) {
525 			rv = ERANGE;
526 			goto out;
527 		}
528 
529 		debugf("decoded intr = %d, trig = %d, pol = %d\n", interrupt,
530 		    trig, pol);
531 
532 		intr_sl[i].trig = trig;
533 		intr_sl[i].pol = pol;
534 
535 		irq = FDT_MAP_IRQ(intr_par, interrupt);
536 		resource_list_add(rl, SYS_RES_IRQ, i, irq, irq, 1);
537 	}
538 
539 out:
540 	free(intr, M_OFWPROP);
541 	return (rv);
542 }
543 
544 int
545 fdt_get_phyaddr(phandle_t node, device_t dev, int *phy_addr, void **phy_sc)
546 {
547 	phandle_t phy_node;
548 	ihandle_t phy_ihandle;
549 	pcell_t phy_handle, phy_reg;
550 	uint32_t i;
551 	device_t parent, child;
552 
553 	if (OF_getprop(node, "phy-handle", (void *)&phy_handle,
554 	    sizeof(phy_handle)) <= 0)
555 		return (ENXIO);
556 
557 	phy_ihandle = (ihandle_t)phy_handle;
558 	phy_ihandle = fdt32_to_cpu(phy_ihandle);
559 	phy_node = OF_instance_to_package(phy_ihandle);
560 
561 	if (OF_getprop(phy_node, "reg", (void *)&phy_reg,
562 	    sizeof(phy_reg)) <= 0)
563 		return (ENXIO);
564 
565 	*phy_addr = fdt32_to_cpu(phy_reg);
566 
567 	/*
568 	 * Search for softc used to communicate with phy.
569 	 */
570 
571 	/*
572 	 * Step 1: Search for ancestor of the phy-node with a "phy-handle"
573 	 * property set.
574 	 */
575 	phy_node = OF_parent(phy_node);
576 	while (phy_node != 0) {
577 		if (OF_getprop(phy_node, "phy-handle", (void *)&phy_handle,
578 		    sizeof(phy_handle)) > 0)
579 			break;
580 		phy_node = OF_parent(phy_node);
581 	}
582 	if (phy_node == 0)
583 		return (ENXIO);
584 
585 	/*
586 	 * Step 2: For each device with the same parent and name as ours
587 	 * compare its node with the one found in step 1, ancestor of phy
588 	 * node (stored in phy_node).
589 	 */
590 	parent = device_get_parent(dev);
591 	i = 0;
592 	child = device_find_child(parent, device_get_name(dev), i);
593 	while (child != NULL) {
594 		if (ofw_bus_get_node(child) == phy_node)
595 			break;
596 		i++;
597 		child = device_find_child(parent, device_get_name(dev), i);
598 	}
599 	if (child == NULL)
600 		return (ENXIO);
601 
602 	/*
603 	 * Use softc of the device found.
604 	 */
605 	*phy_sc = (void *)device_get_softc(child);
606 
607 	return (0);
608 }
609 
610 int
611 fdt_get_mem_regions(struct mem_region *mr, int *mrcnt, uint32_t *memsize)
612 {
613 	pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS];
614 	pcell_t *regp;
615 	phandle_t memory;
616 	uint32_t memory_size;
617 	int addr_cells, size_cells;
618 	int i, max_size, reg_len, rv, tuple_size, tuples;
619 
620 	max_size = sizeof(reg);
621 	memory = OF_finddevice("/memory");
622 	if (memory == -1) {
623 		rv = ENXIO;
624 		goto out;
625 	}
626 
627 	if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
628 	    &size_cells)) != 0)
629 		goto out;
630 
631 	if (addr_cells > 2) {
632 		rv = ERANGE;
633 		goto out;
634 	}
635 
636 	tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
637 	reg_len = OF_getproplen(memory, "reg");
638 	if (reg_len <= 0 || reg_len > sizeof(reg)) {
639 		rv = ERANGE;
640 		goto out;
641 	}
642 
643 	if (OF_getprop(memory, "reg", reg, reg_len) <= 0) {
644 		rv = ENXIO;
645 		goto out;
646 	}
647 
648 	memory_size = 0;
649 	tuples = reg_len / tuple_size;
650 	regp = (pcell_t *)&reg;
651 	for (i = 0; i < tuples; i++) {
652 
653 		rv = fdt_data_to_res(regp, addr_cells, size_cells,
654 			(u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
655 
656 		if (rv != 0)
657 			goto out;
658 
659 		regp += addr_cells + size_cells;
660 		memory_size += mr[i].mr_size;
661 	}
662 
663 	if (memory_size == 0) {
664 		rv = ERANGE;
665 		goto out;
666 	}
667 
668 	*mrcnt = i;
669 	*memsize = memory_size;
670 	rv = 0;
671 out:
672 	return (rv);
673 }
674