1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2004 Freescale Semiconductor.
4  */
5 
6 #include <common.h>
7 #include <linux/libfdt.h>
8 #include <fdt_support.h>
9 #include "cadmus.h"
10 
11 #if defined(CONFIG_OF_BOARD_SETUP)
cds_pci_fixup(void * blob)12 static void cds_pci_fixup(void *blob)
13 {
14 	int node;
15 	const char *path;
16 	int len, slot, i;
17 	u32 *map = NULL, *piccells = NULL;
18 	int off, cells;
19 
20 	node = fdt_path_offset(blob, "/aliases");
21 	if (node >= 0) {
22 		path = fdt_getprop(blob, node, "pci0", NULL);
23 		if (path) {
24 			node = fdt_path_offset(blob, path);
25 			if (node >= 0) {
26 				map = fdt_getprop_w(blob, node, "interrupt-map", &len);
27 			}
28 			/* Each item in "interrupt-map" property is translated with
29 			 * following cells:
30 			 * PCI #address-cells, PCI #interrupt-cells,
31 			 * PIC address, PIC #address-cells, PIC #interrupt-cells.
32 			 */
33 			cells = fdt_getprop_u32_default(blob, path, "#address-cells", 1);
34 			cells += fdt_getprop_u32_default(blob, path, "#interrupt-cells", 1);
35 			off = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*(map+cells)));
36 			if (off <= 0)
37 				return;
38 			cells += 1;
39 			piccells = (u32 *)fdt_getprop(blob, off, "#address-cells", NULL);
40 			if (piccells == NULL)
41 				return;
42 			cells += *piccells;
43 			piccells = (u32 *)fdt_getprop(blob, off, "#interrupt-cells", NULL);
44 			if (piccells == NULL)
45 				return;
46 			cells += *piccells;
47 		}
48 	}
49 
50 	if (map) {
51 		len /= sizeof(u32);
52 
53 		slot = get_pci_slot();
54 
55 		for (i=0;i<len;i+=cells) {
56 			/* We rotate the interrupt pins so that the mapping
57 			 * changes depending on the slot the carrier card is in.
58 			 */
59 			map[3] = ((map[3] + slot - 2) % 4) + 1;
60 			map+=cells;
61 		}
62 	}
63 }
64 
ft_board_setup(void * blob,bd_t * bd)65 int ft_board_setup(void *blob, bd_t *bd)
66 {
67 	ft_cpu_setup(blob, bd);
68 #ifdef CONFIG_PCI
69 	ft_pci_setup(blob, bd);
70 	cds_pci_fixup(blob);
71 #endif
72 
73 	return 0;
74 }
75 #endif
76