xref: /linux/arch/mips/sgi-ip22/ip22-gio.c (revision 4d9dd68708f814ddeef4015bc67f2b13d192d0be)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/export.h>
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/slab.h>
6 
7 #include <asm/addrspace.h>
8 #include <asm/paccess.h>
9 #include <asm/gio_device.h>
10 #include <asm/sgi/gio.h>
11 #include <asm/sgi/hpc3.h>
12 #include <asm/sgi/mc.h>
13 #include <asm/sgi/ip22.h>
14 
15 static const struct bus_type gio_bus_type;
16 
17 static struct {
18 	const char *name;
19 	__u8	   id;
20 } gio_name_table[] = {
21 	{ .name = "SGI Impact", .id = 0x10 },
22 	{ .name = "Phobos G160", .id = 0x35 },
23 	{ .name = "Phobos G130", .id = 0x36 },
24 	{ .name = "Phobos G100", .id = 0x37 },
25 	{ .name = "Set Engineering GFE", .id = 0x38 },
26 	/* fake IDs */
27 	{ .name = "SGI Newport", .id = 0x7e },
28 	{ .name = "SGI GR2/GR3", .id = 0x7f },
29 };
30 
31 static struct device *gio_bus;
32 
33 /**
34  * gio_match_device - Tell if an of_device structure has a matching
35  * gio_match structure
36  * @ids: array of of device match structures to search in
37  * @dev: the of device structure to match against
38  *
39  * Used by a driver to check whether an of_device present in the
40  * system is in its list of supported devices.
41  */
42 static const struct gio_device_id *
43 gio_match_device(const struct gio_device_id *match,
44 		 const struct gio_device *dev)
45 {
46 	const struct gio_device_id *ids;
47 
48 	for (ids = match; ids->id != 0xff; ids++)
49 		if (ids->id == dev->id.id)
50 			return ids;
51 
52 	return NULL;
53 }
54 
55 struct gio_device *gio_dev_get(struct gio_device *dev)
56 {
57 	struct device *tmp;
58 
59 	if (!dev)
60 		return NULL;
61 	tmp = get_device(&dev->dev);
62 	if (tmp)
63 		return to_gio_device(tmp);
64 	else
65 		return NULL;
66 }
67 EXPORT_SYMBOL_GPL(gio_dev_get);
68 
69 void gio_dev_put(struct gio_device *dev)
70 {
71 	if (dev)
72 		put_device(&dev->dev);
73 }
74 EXPORT_SYMBOL_GPL(gio_dev_put);
75 
76 /**
77  * gio_release_dev - free an gio device structure when all users of it are finished.
78  * @dev: device that's been disconnected
79  *
80  * Will be called only by the device core when all users of this gio device are
81  * done.
82  */
83 static void gio_release_dev(struct device *dev)
84 {
85 	struct gio_device *giodev;
86 
87 	giodev = to_gio_device(dev);
88 	kfree(giodev);
89 }
90 
91 int gio_device_register(struct gio_device *giodev)
92 {
93 	giodev->dev.bus = &gio_bus_type;
94 	giodev->dev.parent = gio_bus;
95 	giodev->dev.release = gio_release_dev;
96 
97 	return device_register(&giodev->dev);
98 }
99 EXPORT_SYMBOL_GPL(gio_device_register);
100 
101 void gio_device_unregister(struct gio_device *giodev)
102 {
103 	device_unregister(&giodev->dev);
104 }
105 EXPORT_SYMBOL_GPL(gio_device_unregister);
106 
107 static int gio_bus_match(struct device *dev, const struct device_driver *drv)
108 {
109 	struct gio_device *gio_dev = to_gio_device(dev);
110 	struct gio_driver *gio_drv = to_gio_driver(drv);
111 
112 	return gio_match_device(gio_drv->id_table, gio_dev) != NULL;
113 }
114 
115 static int gio_device_probe(struct device *dev)
116 {
117 	int error = -ENODEV;
118 	struct gio_driver *drv;
119 	struct gio_device *gio_dev;
120 	const struct gio_device_id *match;
121 
122 	drv = to_gio_driver(dev->driver);
123 	gio_dev = to_gio_device(dev);
124 
125 	if (!drv->probe)
126 		return error;
127 
128 	match = gio_match_device(drv->id_table, gio_dev);
129 	if (match)
130 		error = drv->probe(gio_dev, match);
131 
132 	return error;
133 }
134 
135 static void gio_device_remove(struct device *dev)
136 {
137 	struct gio_device *gio_dev = to_gio_device(dev);
138 	struct gio_driver *drv = to_gio_driver(dev->driver);
139 
140 	if (drv->remove)
141 		drv->remove(gio_dev);
142 }
143 
144 static void gio_device_shutdown(struct device *dev)
145 {
146 	struct gio_device *gio_dev = to_gio_device(dev);
147 	struct gio_driver *drv = to_gio_driver(dev->driver);
148 
149 	if (dev->driver && drv->shutdown)
150 		drv->shutdown(gio_dev);
151 }
152 
153 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
154 			     char *buf)
155 {
156 	struct gio_device *gio_dev = to_gio_device(dev);
157 
158 	return sysfs_emit(buf, "gio:%x\n", gio_dev->id.id);
159 }
160 static DEVICE_ATTR_RO(modalias);
161 
162 static ssize_t name_show(struct device *dev,
163 			 struct device_attribute *attr, char *buf)
164 {
165 	struct gio_device *giodev;
166 
167 	giodev = to_gio_device(dev);
168 	return sysfs_emit(buf, "%s\n", giodev->name);
169 }
170 static DEVICE_ATTR_RO(name);
171 
172 static ssize_t id_show(struct device *dev,
173 		       struct device_attribute *attr, char *buf)
174 {
175 	struct gio_device *giodev;
176 
177 	giodev = to_gio_device(dev);
178 	return sysfs_emit(buf, "%x\n", giodev->id.id);
179 }
180 static DEVICE_ATTR_RO(id);
181 
182 static struct attribute *gio_dev_attrs[] = {
183 	&dev_attr_modalias.attr,
184 	&dev_attr_name.attr,
185 	&dev_attr_id.attr,
186 	NULL,
187 };
188 ATTRIBUTE_GROUPS(gio_dev);
189 
190 static int gio_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
191 {
192 	const struct gio_device *gio_dev = to_gio_device(dev);
193 
194 	add_uevent_var(env, "MODALIAS=gio:%x", gio_dev->id.id);
195 	return 0;
196 }
197 
198 int gio_register_driver(struct gio_driver *drv)
199 {
200 	/* initialize common driver fields */
201 	if (!drv->driver.name)
202 		drv->driver.name = drv->name;
203 	if (!drv->driver.owner)
204 		drv->driver.owner = drv->owner;
205 	drv->driver.bus = &gio_bus_type;
206 
207 	/* register with core */
208 	return driver_register(&drv->driver);
209 }
210 EXPORT_SYMBOL_GPL(gio_register_driver);
211 
212 void gio_unregister_driver(struct gio_driver *drv)
213 {
214 	driver_unregister(&drv->driver);
215 }
216 EXPORT_SYMBOL_GPL(gio_unregister_driver);
217 
218 void gio_set_master(struct gio_device *dev)
219 {
220 	u32 tmp = sgimc->giopar;
221 
222 	switch (dev->slotno) {
223 	case 0:
224 		tmp |= SGIMC_GIOPAR_MASTERGFX;
225 		break;
226 	case 1:
227 		tmp |= SGIMC_GIOPAR_MASTEREXP0;
228 		break;
229 	case 2:
230 		tmp |= SGIMC_GIOPAR_MASTEREXP1;
231 		break;
232 	}
233 	sgimc->giopar = tmp;
234 }
235 EXPORT_SYMBOL_GPL(gio_set_master);
236 
237 static void ip22_gio_set_64bit(int slotno)
238 {
239 	u32 tmp = sgimc->giopar;
240 
241 	switch (slotno) {
242 	case 0:
243 		tmp |= SGIMC_GIOPAR_GFX64;
244 		break;
245 	case 1:
246 		tmp |= SGIMC_GIOPAR_EXP064;
247 		break;
248 	case 2:
249 		tmp |= SGIMC_GIOPAR_EXP164;
250 		break;
251 	}
252 	sgimc->giopar = tmp;
253 }
254 
255 static int ip22_gio_id(unsigned long addr, u32 *res)
256 {
257 	u8 tmp8;
258 	u8 tmp16;
259 	u32 tmp32;
260 	u8 *ptr8;
261 	u16 *ptr16;
262 	u32 *ptr32;
263 
264 	ptr32 = (void *)CKSEG1ADDR(addr);
265 	if (!get_dbe(tmp32, ptr32)) {
266 		/*
267 		 * We got no DBE, but this doesn't mean anything.
268 		 * If GIO is pipelined (which can't be disabled
269 		 * for GFX slot) we don't get a DBE, but we see
270 		 * the transfer size as data. So we do an 8bit
271 		 * and a 16bit access and check whether the common
272 		 * data matches
273 		 */
274 		ptr8 = (void *)CKSEG1ADDR(addr + 3);
275 		if (get_dbe(tmp8, ptr8)) {
276 			/*
277 			 * 32bit access worked, but 8bit doesn't
278 			 * so we don't see phantom reads on
279 			 * a pipelined bus, but a real card which
280 			 * doesn't support 8 bit reads
281 			 */
282 			*res = tmp32;
283 			return 1;
284 		}
285 		ptr16 = (void *)CKSEG1ADDR(addr + 2);
286 		get_dbe(tmp16, ptr16);
287 		if (tmp8 == (tmp16 & 0xff) &&
288 		    tmp8 == (tmp32 & 0xff) &&
289 		    tmp16 == (tmp32 & 0xffff)) {
290 			*res = tmp32;
291 			return 1;
292 		}
293 	}
294 	return 0; /* nothing here */
295 }
296 
297 #define HQ2_MYSTERY_OFFS       0x6A07C
298 #define NEWPORT_USTATUS_OFFS   0xF133C
299 
300 static int ip22_is_gr2(unsigned long addr)
301 {
302 	u32 tmp;
303 	u32 *ptr;
304 
305 	/* HQ2 only allows 32bit accesses */
306 	ptr = (void *)CKSEG1ADDR(addr + HQ2_MYSTERY_OFFS);
307 	if (!get_dbe(tmp, ptr)) {
308 		if (tmp == 0xdeadbeef)
309 			return 1;
310 	}
311 	return 0;
312 }
313 
314 
315 static void ip22_check_gio(int slotno, unsigned long addr, int irq)
316 {
317 	const char *name = "Unknown";
318 	struct gio_device *gio_dev;
319 	u32 tmp;
320 	__u8 id;
321 	int i;
322 
323 	/* first look for GR2/GR3 by checking mystery register */
324 	if (ip22_is_gr2(addr))
325 		tmp = 0x7f;
326 	else {
327 		if (!ip22_gio_id(addr, &tmp)) {
328 			/*
329 			 * no GIO signature at start address of slot
330 			 * since Newport doesn't have one, we check if
331 			 * user status register is readable
332 			 */
333 			if (ip22_gio_id(addr + NEWPORT_USTATUS_OFFS, &tmp))
334 				tmp = 0x7e;
335 			else
336 				tmp = 0;
337 		}
338 	}
339 	if (tmp) {
340 		id = GIO_ID(tmp);
341 		if (tmp & GIO_32BIT_ID) {
342 			if (tmp & GIO_64BIT_IFACE)
343 				ip22_gio_set_64bit(slotno);
344 		}
345 		for (i = 0; i < ARRAY_SIZE(gio_name_table); i++) {
346 			if (id == gio_name_table[i].id) {
347 				name = gio_name_table[i].name;
348 				break;
349 			}
350 		}
351 		printk(KERN_INFO "GIO: slot %d : %s (id %x)\n",
352 		       slotno, name, id);
353 		gio_dev = kzalloc_obj(*gio_dev);
354 		if (!gio_dev)
355 			return;
356 		gio_dev->name = name;
357 		gio_dev->slotno = slotno;
358 		gio_dev->id.id = id;
359 		gio_dev->resource.start = addr;
360 		gio_dev->resource.end = addr + 0x3fffff;
361 		gio_dev->resource.flags = IORESOURCE_MEM;
362 		gio_dev->irq = irq;
363 		dev_set_name(&gio_dev->dev, "%d", slotno);
364 		if (gio_device_register(gio_dev))
365 			gio_dev_put(gio_dev);
366 	} else
367 		printk(KERN_INFO "GIO: slot %d : Empty\n", slotno);
368 }
369 
370 static const struct bus_type gio_bus_type = {
371 	.name	   = "gio",
372 	.dev_groups = gio_dev_groups,
373 	.match	   = gio_bus_match,
374 	.probe	   = gio_device_probe,
375 	.remove	   = gio_device_remove,
376 	.shutdown  = gio_device_shutdown,
377 	.uevent	   = gio_device_uevent,
378 };
379 
380 static struct resource gio_bus_resource = {
381 	.start = GIO_SLOT_GFX_BASE,
382 	.end   = GIO_SLOT_GFX_BASE + 0x9fffff,
383 	.name  = "GIO Bus",
384 	.flags = IORESOURCE_MEM,
385 };
386 
387 static int __init ip22_gio_init(void)
388 {
389 	unsigned int pbdma __maybe_unused;
390 	int ret;
391 
392 	gio_bus = root_device_register("gio");
393 	if (IS_ERR(gio_bus))
394 		return PTR_ERR(gio_bus);
395 
396 	ret = bus_register(&gio_bus_type);
397 	if (!ret) {
398 		request_resource(&iomem_resource, &gio_bus_resource);
399 		printk(KERN_INFO "GIO: Probing bus...\n");
400 
401 		if (ip22_is_fullhouse()) {
402 			/* Indigo2 */
403 			ip22_check_gio(0, GIO_SLOT_GFX_BASE, SGI_GIO_1_IRQ);
404 			ip22_check_gio(1, GIO_SLOT_EXP0_BASE, SGI_GIO_1_IRQ);
405 		} else {
406 			/* Indy/Challenge S */
407 			if (get_dbe(pbdma, (unsigned int *)&hpc3c1->pbdma[1]))
408 				ip22_check_gio(0, GIO_SLOT_GFX_BASE,
409 					       SGI_GIO_0_IRQ);
410 			ip22_check_gio(1, GIO_SLOT_EXP0_BASE, SGI_GIOEXP0_IRQ);
411 			ip22_check_gio(2, GIO_SLOT_EXP1_BASE, SGI_GIOEXP1_IRQ);
412 		}
413 	} else {
414 		root_device_unregister(gio_bus);
415 	}
416 
417 	return ret;
418 }
419 
420 subsys_initcall(ip22_gio_init);
421