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