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