xref: /openbmc/linux/arch/mips/sgi-ip27/ip27-xtalk.c (revision c4f7ac64)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 1999, 2000 Ralf Baechle (ralf@gnu.org)
4  * Copyright (C) 1999, 2000 Silcon Graphics, Inc.
5  * Copyright (C) 2004 Christoph Hellwig.
6  *
7  * Generic XTALK initialization code
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/smp.h>
12 #include <linux/platform_device.h>
13 #include <linux/platform_data/sgi-w1.h>
14 #include <linux/platform_data/xtalk-bridge.h>
15 #include <asm/sn/addrs.h>
16 #include <asm/sn/types.h>
17 #include <asm/sn/klconfig.h>
18 #include <asm/pci/bridge.h>
19 #include <asm/xtalk/xtalk.h>
20 
21 
22 #define XBOW_WIDGET_PART_NUM	0x0
23 #define XXBOW_WIDGET_PART_NUM	0xd000	/* Xbow in Xbridge */
24 #define BASE_XBOW_PORT		8     /* Lowest external port */
25 
26 static void bridge_platform_create(nasid_t nasid, int widget, int masterwid)
27 {
28 	struct xtalk_bridge_platform_data *bd;
29 	struct sgi_w1_platform_data *wd;
30 	struct platform_device *pdev;
31 	struct resource w1_res;
32 	unsigned long offset;
33 
34 	offset = NODE_OFFSET(nasid);
35 
36 	wd = kzalloc(sizeof(*wd), GFP_KERNEL);
37 	if (!wd)
38 		goto no_mem;
39 
40 	snprintf(wd->dev_id, sizeof(wd->dev_id), "bridge-%012lx",
41 		 offset + (widget << SWIN_SIZE_BITS));
42 
43 	memset(&w1_res, 0, sizeof(w1_res));
44 	w1_res.start = offset + (widget << SWIN_SIZE_BITS) +
45 				offsetof(struct bridge_regs, b_nic);
46 	w1_res.end = w1_res.start + 3;
47 	w1_res.flags = IORESOURCE_MEM;
48 
49 	pdev = platform_device_alloc("sgi_w1", PLATFORM_DEVID_AUTO);
50 	if (!pdev) {
51 		kfree(wd);
52 		goto no_mem;
53 	}
54 	platform_device_add_resources(pdev, &w1_res, 1);
55 	platform_device_add_data(pdev, wd, sizeof(*wd));
56 	platform_device_add(pdev);
57 
58 	bd = kzalloc(sizeof(*bd), GFP_KERNEL);
59 	if (!bd)
60 		goto no_mem;
61 	pdev = platform_device_alloc("xtalk-bridge", PLATFORM_DEVID_AUTO);
62 	if (!pdev) {
63 		kfree(bd);
64 		goto no_mem;
65 	}
66 
67 
68 	bd->bridge_addr = RAW_NODE_SWIN_BASE(nasid, widget);
69 	bd->intr_addr	= BIT_ULL(47) + 0x01800000 + PI_INT_PEND_MOD;
70 	bd->nasid	= nasid;
71 	bd->masterwid	= masterwid;
72 
73 	bd->mem.name	= "Bridge PCI MEM";
74 	bd->mem.start	= offset + (widget << SWIN_SIZE_BITS) + BRIDGE_DEVIO0;
75 	bd->mem.end	= offset + (widget << SWIN_SIZE_BITS) + SWIN_SIZE - 1;
76 	bd->mem.flags	= IORESOURCE_MEM;
77 	bd->mem_offset	= offset;
78 
79 	bd->io.name	= "Bridge PCI IO";
80 	bd->io.start	= offset + (widget << SWIN_SIZE_BITS) + BRIDGE_DEVIO0;
81 	bd->io.end	= offset + (widget << SWIN_SIZE_BITS) + SWIN_SIZE - 1;
82 	bd->io.flags	= IORESOURCE_IO;
83 	bd->io_offset	= offset;
84 
85 	platform_device_add_data(pdev, bd, sizeof(*bd));
86 	platform_device_add(pdev);
87 	pr_info("xtalk:n%d/%x bridge widget\n", nasid, widget);
88 	return;
89 
90 no_mem:
91 	pr_warn("xtalk:n%d/%x bridge create out of memory\n", nasid, widget);
92 }
93 
94 static int probe_one_port(nasid_t nasid, int widget, int masterwid)
95 {
96 	widgetreg_t		widget_id;
97 	xwidget_part_num_t	partnum;
98 
99 	widget_id = *(volatile widgetreg_t *)
100 		(RAW_NODE_SWIN_BASE(nasid, widget) + WIDGET_ID);
101 	partnum = XWIDGET_PART_NUM(widget_id);
102 
103 	switch (partnum) {
104 	case BRIDGE_WIDGET_PART_NUM:
105 	case XBRIDGE_WIDGET_PART_NUM:
106 		bridge_platform_create(nasid, widget, masterwid);
107 		break;
108 	default:
109 		pr_info("xtalk:n%d/%d unknown widget (0x%x)\n",
110 			nasid, widget, partnum);
111 		break;
112 	}
113 
114 	return 0;
115 }
116 
117 static int xbow_probe(nasid_t nasid)
118 {
119 	lboard_t *brd;
120 	klxbow_t *xbow_p;
121 	unsigned masterwid, i;
122 
123 	/*
124 	 * found xbow, so may have multiple bridges
125 	 * need to probe xbow
126 	 */
127 	brd = find_lboard((lboard_t *)KL_CONFIG_INFO(nasid), KLTYPE_MIDPLANE8);
128 	if (!brd)
129 		return -ENODEV;
130 
131 	xbow_p = (klxbow_t *)find_component(brd, NULL, KLSTRUCT_XBOW);
132 	if (!xbow_p)
133 		return -ENODEV;
134 
135 	/*
136 	 * Okay, here's a xbow. Let's arbitrate and find
137 	 * out if we should initialize it. Set enabled
138 	 * hub connected at highest or lowest widget as
139 	 * master.
140 	 */
141 #ifdef WIDGET_A
142 	i = HUB_WIDGET_ID_MAX + 1;
143 	do {
144 		i--;
145 	} while ((!XBOW_PORT_TYPE_HUB(xbow_p, i)) ||
146 		 (!XBOW_PORT_IS_ENABLED(xbow_p, i)));
147 #else
148 	i = HUB_WIDGET_ID_MIN - 1;
149 	do {
150 		i++;
151 	} while ((!XBOW_PORT_TYPE_HUB(xbow_p, i)) ||
152 		 (!XBOW_PORT_IS_ENABLED(xbow_p, i)));
153 #endif
154 
155 	masterwid = i;
156 	if (nasid != XBOW_PORT_NASID(xbow_p, i))
157 		return 1;
158 
159 	for (i = HUB_WIDGET_ID_MIN; i <= HUB_WIDGET_ID_MAX; i++) {
160 		if (XBOW_PORT_IS_ENABLED(xbow_p, i) &&
161 		    XBOW_PORT_TYPE_IO(xbow_p, i))
162 			probe_one_port(nasid, i, masterwid);
163 	}
164 
165 	return 0;
166 }
167 
168 static void xtalk_probe_node(nasid_t nasid)
169 {
170 	volatile u64		hubreg;
171 	xwidget_part_num_t	partnum;
172 	widgetreg_t		widget_id;
173 
174 	hubreg = REMOTE_HUB_L(nasid, IIO_LLP_CSR);
175 
176 	/* check whether the link is up */
177 	if (!(hubreg & IIO_LLP_CSR_IS_UP))
178 		return;
179 
180 	widget_id = *(volatile widgetreg_t *)
181 		       (RAW_NODE_SWIN_BASE(nasid, 0x0) + WIDGET_ID);
182 	partnum = XWIDGET_PART_NUM(widget_id);
183 
184 	switch (partnum) {
185 	case BRIDGE_WIDGET_PART_NUM:
186 		bridge_platform_create(nasid, 0x8, 0xa);
187 		break;
188 	case XBOW_WIDGET_PART_NUM:
189 	case XXBOW_WIDGET_PART_NUM:
190 		pr_info("xtalk:n%d/0 xbow widget\n", nasid);
191 		xbow_probe(nasid);
192 		break;
193 	default:
194 		pr_info("xtalk:n%d/0 unknown widget (0x%x)\n", nasid, partnum);
195 		break;
196 	}
197 }
198 
199 static int __init xtalk_init(void)
200 {
201 	nasid_t nasid;
202 
203 	for_each_online_node(nasid)
204 		xtalk_probe_node(nasid);
205 
206 	return 0;
207 }
208 arch_initcall(xtalk_init);
209