1 #include <linux/export.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/slab.h>
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>
14 static struct bus_type gio_bus_type;
19 } gio_name_table[] = {
20 { .name = "SGI Impact", .id = 0x10 },
21 { .name = "Phobos G160", .id = 0x35 },
23 { .name = "SGI Newport", .id = 0x7e },
24 { .name = "SGI GR2/GR3", .id = 0x7f },
27 static struct device gio_bus = {
32 * gio_match_device - Tell if an of_device structure has a matching
34 * @ids: array of of device match structures to search in
35 * @dev: the of device structure to match against
37 * Used by a driver to check whether an of_device present in the
38 * system is in its list of supported devices.
40 const struct gio_device_id *gio_match_device(const struct gio_device_id *match,
41 const struct gio_device *dev)
43 const struct gio_device_id *ids;
45 for (ids = match; ids->id != 0xff; ids++)
46 if (ids->id == dev->id.id)
51 EXPORT_SYMBOL_GPL(gio_match_device);
53 struct gio_device *gio_dev_get(struct gio_device *dev)
59 tmp = get_device(&dev->dev);
61 return to_gio_device(tmp);
65 EXPORT_SYMBOL_GPL(gio_dev_get);
67 void gio_dev_put(struct gio_device *dev)
70 put_device(&dev->dev);
72 EXPORT_SYMBOL_GPL(gio_dev_put);
75 * gio_release_dev - free an gio device structure when all users of it are finished.
76 * @dev: device that's been disconnected
78 * Will be called only by the device core when all users of this gio device are
81 void gio_release_dev(struct device *dev)
83 struct gio_device *giodev;
85 giodev = to_gio_device(dev);
88 EXPORT_SYMBOL_GPL(gio_release_dev);
90 int gio_device_register(struct gio_device *giodev)
92 giodev->dev.bus = &gio_bus_type;
93 giodev->dev.parent = &gio_bus;
94 return device_register(&giodev->dev);
96 EXPORT_SYMBOL_GPL(gio_device_register);
98 void gio_device_unregister(struct gio_device *giodev)
100 device_unregister(&giodev->dev);
102 EXPORT_SYMBOL_GPL(gio_device_unregister);
104 static int gio_bus_match(struct device *dev, struct device_driver *drv)
106 struct gio_device *gio_dev = to_gio_device(dev);
107 struct gio_driver *gio_drv = to_gio_driver(drv);
109 return gio_match_device(gio_drv->id_table, gio_dev) != NULL;
112 static int gio_device_probe(struct device *dev)
115 struct gio_driver *drv;
116 struct gio_device *gio_dev;
117 const struct gio_device_id *match;
119 drv = to_gio_driver(dev->driver);
120 gio_dev = to_gio_device(dev);
125 gio_dev_get(gio_dev);
127 match = gio_match_device(drv->id_table, gio_dev);
129 error = drv->probe(gio_dev, match);
131 gio_dev_put(gio_dev);
136 static int gio_device_remove(struct device *dev)
138 struct gio_device *gio_dev = to_gio_device(dev);
139 struct gio_driver *drv = to_gio_driver(dev->driver);
141 if (dev->driver && drv->remove)
142 drv->remove(gio_dev);
146 static int gio_device_suspend(struct device *dev, pm_message_t state)
148 struct gio_device *gio_dev = to_gio_device(dev);
149 struct gio_driver *drv = to_gio_driver(dev->driver);
152 if (dev->driver && drv->suspend)
153 error = drv->suspend(gio_dev, state);
157 static int gio_device_resume(struct device *dev)
159 struct gio_device *gio_dev = to_gio_device(dev);
160 struct gio_driver *drv = to_gio_driver(dev->driver);
163 if (dev->driver && drv->resume)
164 error = drv->resume(gio_dev);
168 static void gio_device_shutdown(struct device *dev)
170 struct gio_device *gio_dev = to_gio_device(dev);
171 struct gio_driver *drv = to_gio_driver(dev->driver);
173 if (dev->driver && drv->shutdown)
174 drv->shutdown(gio_dev);
177 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
180 struct gio_device *gio_dev = to_gio_device(dev);
181 int len = snprintf(buf, PAGE_SIZE, "gio:%x\n", gio_dev->id.id);
183 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
186 static ssize_t name_show(struct device *dev,
187 struct device_attribute *attr, char *buf)
189 struct gio_device *giodev;
191 giodev = to_gio_device(dev);
192 return sprintf(buf, "%s", giodev->name);
195 static ssize_t id_show(struct device *dev,
196 struct device_attribute *attr, char *buf)
198 struct gio_device *giodev;
200 giodev = to_gio_device(dev);
201 return sprintf(buf, "%x", giodev->id.id);
204 static struct device_attribute gio_dev_attrs[] = {
211 static int gio_device_uevent(struct device *dev, struct kobj_uevent_env *env)
213 struct gio_device *gio_dev = to_gio_device(dev);
215 add_uevent_var(env, "MODALIAS=gio:%x", gio_dev->id.id);
219 int gio_register_driver(struct gio_driver *drv)
221 /* initialize common driver fields */
222 if (!drv->driver.name)
223 drv->driver.name = drv->name;
224 if (!drv->driver.owner)
225 drv->driver.owner = drv->owner;
226 drv->driver.bus = &gio_bus_type;
228 /* register with core */
229 return driver_register(&drv->driver);
231 EXPORT_SYMBOL_GPL(gio_register_driver);
233 void gio_unregister_driver(struct gio_driver *drv)
235 driver_unregister(&drv->driver);
237 EXPORT_SYMBOL_GPL(gio_unregister_driver);
239 void gio_set_master(struct gio_device *dev)
241 u32 tmp = sgimc->giopar;
243 switch (dev->slotno) {
245 tmp |= SGIMC_GIOPAR_MASTERGFX;
248 tmp |= SGIMC_GIOPAR_MASTEREXP0;
251 tmp |= SGIMC_GIOPAR_MASTEREXP1;
256 EXPORT_SYMBOL_GPL(gio_set_master);
258 void ip22_gio_set_64bit(int slotno)
260 u32 tmp = sgimc->giopar;
264 tmp |= SGIMC_GIOPAR_GFX64;
267 tmp |= SGIMC_GIOPAR_EXP064;
270 tmp |= SGIMC_GIOPAR_EXP164;
276 static int ip22_gio_id(unsigned long addr, u32 *res)
285 ptr32 = (void *)CKSEG1ADDR(addr);
286 if (!get_dbe(tmp32, ptr32)) {
288 * We got no DBE, but this doesn't mean anything.
289 * If GIO is pipelined (which can't be disabled
290 * for GFX slot) we don't get a DBE, but we see
291 * the transfer size as data. So we do an 8bit
292 * and a 16bit access and check whether the common
295 ptr8 = (void *)CKSEG1ADDR(addr + 3);
297 ptr16 = (void *)CKSEG1ADDR(addr + 2);
298 get_dbe(tmp16, ptr16);
299 if (tmp8 == (tmp16 & 0xff) &&
300 tmp8 == (tmp32 & 0xff) &&
301 tmp16 == (tmp32 & 0xffff)) {
306 return 0; /* nothing here */
309 #define HQ2_MYSTERY_OFFS 0x6A07C
310 #define NEWPORT_USTATUS_OFFS 0xF133C
312 static int ip22_is_gr2(unsigned long addr)
317 /* HQ2 only allows 32bit accesses */
318 ptr = (void *)CKSEG1ADDR(addr + HQ2_MYSTERY_OFFS);
319 if (!get_dbe(tmp, ptr)) {
320 if (tmp == 0xdeadbeef)
327 static void ip22_check_gio(int slotno, unsigned long addr)
329 const char *name = "Unknown";
330 struct gio_device *gio_dev;
335 /* first look for GR2/GR3 by checking mystery register */
336 if (ip22_is_gr2(addr))
339 if (!ip22_gio_id(addr, &tmp)) {
341 * no GIO signature at start address of slot, but
342 * Newport doesn't have one, so let's check usea
345 if (ip22_gio_id(addr + NEWPORT_USTATUS_OFFS, &tmp))
353 if (tmp & GIO_32BIT_ID) {
354 if (tmp & GIO_64BIT_IFACE)
355 ip22_gio_set_64bit(slotno);
357 for (i = 0; i < ARRAY_SIZE(gio_name_table); i++) {
358 if (id == gio_name_table[i].id) {
359 name = gio_name_table[i].name;
363 printk(KERN_INFO "GIO: slot %d : %s (id %x)\n",
365 gio_dev = kzalloc(sizeof *gio_dev, GFP_KERNEL);
366 gio_dev->name = name;
367 gio_dev->slotno = slotno;
369 gio_dev->resource.start = addr;
370 gio_dev->resource.end = addr + 0x3fffff;
371 gio_dev->resource.flags = IORESOURCE_MEM;
372 dev_set_name(&gio_dev->dev, "%d", slotno);
373 gio_device_register(gio_dev);
375 printk(KERN_INFO "GIO: slot %d : Empty\n", slotno);
378 static struct bus_type gio_bus_type = {
380 .dev_attrs = gio_dev_attrs,
381 .match = gio_bus_match,
382 .probe = gio_device_probe,
383 .remove = gio_device_remove,
384 .suspend = gio_device_suspend,
385 .resume = gio_device_resume,
386 .shutdown = gio_device_shutdown,
387 .uevent = gio_device_uevent,
390 static struct resource gio_bus_resource = {
391 .start = GIO_SLOT_GFX_BASE,
392 .end = GIO_SLOT_GFX_BASE + 0x9fffff,
394 .flags = IORESOURCE_MEM,
397 int __init ip22_gio_init(void)
399 unsigned int pbdma __maybe_unused;
402 ret = device_register(&gio_bus);
406 ret = bus_register(&gio_bus_type);
408 request_resource(&iomem_resource, &gio_bus_resource);
409 printk(KERN_INFO "GIO: Probing bus...\n");
411 if (ip22_is_fullhouse() ||
412 !get_dbe(pbdma, (unsigned int *)&hpc3c1->pbdma[1])) {
413 /* Indigo2 and ChallengeS */
414 ip22_check_gio(0, GIO_SLOT_GFX_BASE);
415 ip22_check_gio(1, GIO_SLOT_EXP0_BASE);
418 ip22_check_gio(0, GIO_SLOT_GFX_BASE);
419 ip22_check_gio(1, GIO_SLOT_EXP0_BASE);
420 ip22_check_gio(2, GIO_SLOT_EXP1_BASE);
423 device_unregister(&gio_bus);
428 subsys_initcall(ip22_gio_init);