cfg80211: Fix array-bounds warning in fragment copy
[linux/fpc-iii.git] / arch / mips / sgi-ip22 / ip22-gio.c
blobcdf1876000101e25a153f04415e622820418ba13
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;
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 },
30 static void gio_bus_release(struct device *dev)
32 kfree(dev);
35 static struct device gio_bus = {
36 .init_name = "gio",
37 .release = &gio_bus_release,
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
46 * Used by a driver to check whether an of_device present in the
47 * system is in its list of supported devices.
49 const struct gio_device_id *gio_match_device(const struct gio_device_id *match,
50 const struct gio_device *dev)
52 const struct gio_device_id *ids;
54 for (ids = match; ids->id != 0xff; ids++)
55 if (ids->id == dev->id.id)
56 return ids;
58 return NULL;
60 EXPORT_SYMBOL_GPL(gio_match_device);
62 struct gio_device *gio_dev_get(struct gio_device *dev)
64 struct device *tmp;
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;
74 EXPORT_SYMBOL_GPL(gio_dev_get);
76 void gio_dev_put(struct gio_device *dev)
78 if (dev)
79 put_device(&dev->dev);
81 EXPORT_SYMBOL_GPL(gio_dev_put);
83 /**
84 * gio_release_dev - free an gio device structure when all users of it are finished.
85 * @dev: device that's been disconnected
87 * Will be called only by the device core when all users of this gio device are
88 * done.
90 void gio_release_dev(struct device *dev)
92 struct gio_device *giodev;
94 giodev = to_gio_device(dev);
95 kfree(giodev);
97 EXPORT_SYMBOL_GPL(gio_release_dev);
99 int gio_device_register(struct gio_device *giodev)
101 giodev->dev.bus = &gio_bus_type;
102 giodev->dev.parent = &gio_bus;
103 return device_register(&giodev->dev);
105 EXPORT_SYMBOL_GPL(gio_device_register);
107 void gio_device_unregister(struct gio_device *giodev)
109 device_unregister(&giodev->dev);
111 EXPORT_SYMBOL_GPL(gio_device_unregister);
113 static int gio_bus_match(struct device *dev, struct device_driver *drv)
115 struct gio_device *gio_dev = to_gio_device(dev);
116 struct gio_driver *gio_drv = to_gio_driver(drv);
118 return gio_match_device(gio_drv->id_table, gio_dev) != NULL;
121 static int gio_device_probe(struct device *dev)
123 int error = -ENODEV;
124 struct gio_driver *drv;
125 struct gio_device *gio_dev;
126 const struct gio_device_id *match;
128 drv = to_gio_driver(dev->driver);
129 gio_dev = to_gio_device(dev);
131 if (!drv->probe)
132 return error;
134 gio_dev_get(gio_dev);
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);
142 return error;
145 static int gio_device_remove(struct device *dev)
147 struct gio_device *gio_dev = to_gio_device(dev);
148 struct gio_driver *drv = to_gio_driver(dev->driver);
150 if (dev->driver && drv->remove)
151 drv->remove(gio_dev);
152 return 0;
155 static void gio_device_shutdown(struct device *dev)
157 struct gio_device *gio_dev = to_gio_device(dev);
158 struct gio_driver *drv = to_gio_driver(dev->driver);
160 if (dev->driver && drv->shutdown)
161 drv->shutdown(gio_dev);
164 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
165 char *buf)
167 struct gio_device *gio_dev = to_gio_device(dev);
168 int len = snprintf(buf, PAGE_SIZE, "gio:%x\n", gio_dev->id.id);
170 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
173 static ssize_t name_show(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct gio_device *giodev;
178 giodev = to_gio_device(dev);
179 return sprintf(buf, "%s", giodev->name);
182 static ssize_t id_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
185 struct gio_device *giodev;
187 giodev = to_gio_device(dev);
188 return sprintf(buf, "%x", giodev->id.id);
191 static struct device_attribute gio_dev_attrs[] = {
192 __ATTR_RO(modalias),
193 __ATTR_RO(name),
194 __ATTR_RO(id),
195 __ATTR_NULL,
198 static int gio_device_uevent(struct device *dev, struct kobj_uevent_env *env)
200 struct gio_device *gio_dev = to_gio_device(dev);
202 add_uevent_var(env, "MODALIAS=gio:%x", gio_dev->id.id);
203 return 0;
206 int gio_register_driver(struct gio_driver *drv)
208 /* initialize common driver fields */
209 if (!drv->driver.name)
210 drv->driver.name = drv->name;
211 if (!drv->driver.owner)
212 drv->driver.owner = drv->owner;
213 drv->driver.bus = &gio_bus_type;
215 /* register with core */
216 return driver_register(&drv->driver);
218 EXPORT_SYMBOL_GPL(gio_register_driver);
220 void gio_unregister_driver(struct gio_driver *drv)
222 driver_unregister(&drv->driver);
224 EXPORT_SYMBOL_GPL(gio_unregister_driver);
226 void gio_set_master(struct gio_device *dev)
228 u32 tmp = sgimc->giopar;
230 switch (dev->slotno) {
231 case 0:
232 tmp |= SGIMC_GIOPAR_MASTERGFX;
233 break;
234 case 1:
235 tmp |= SGIMC_GIOPAR_MASTEREXP0;
236 break;
237 case 2:
238 tmp |= SGIMC_GIOPAR_MASTEREXP1;
239 break;
241 sgimc->giopar = tmp;
243 EXPORT_SYMBOL_GPL(gio_set_master);
245 void ip22_gio_set_64bit(int slotno)
247 u32 tmp = sgimc->giopar;
249 switch (slotno) {
250 case 0:
251 tmp |= SGIMC_GIOPAR_GFX64;
252 break;
253 case 1:
254 tmp |= SGIMC_GIOPAR_EXP064;
255 break;
256 case 2:
257 tmp |= SGIMC_GIOPAR_EXP164;
258 break;
260 sgimc->giopar = tmp;
263 static int ip22_gio_id(unsigned long addr, u32 *res)
265 u8 tmp8;
266 u8 tmp16;
267 u32 tmp32;
268 u8 *ptr8;
269 u16 *ptr16;
270 u32 *ptr32;
272 ptr32 = (void *)CKSEG1ADDR(addr);
273 if (!get_dbe(tmp32, ptr32)) {
275 * We got no DBE, but this doesn't mean anything.
276 * If GIO is pipelined (which can't be disabled
277 * for GFX slot) we don't get a DBE, but we see
278 * the transfer size as data. So we do an 8bit
279 * and a 16bit access and check whether the common
280 * data matches
282 ptr8 = (void *)CKSEG1ADDR(addr + 3);
283 if (get_dbe(tmp8, ptr8)) {
285 * 32bit access worked, but 8bit doesn't
286 * so we don't see phantom reads on
287 * a pipelined bus, but a real card which
288 * doesn't support 8 bit reads
290 *res = tmp32;
291 return 1;
293 ptr16 = (void *)CKSEG1ADDR(addr + 2);
294 get_dbe(tmp16, ptr16);
295 if (tmp8 == (tmp16 & 0xff) &&
296 tmp8 == (tmp32 & 0xff) &&
297 tmp16 == (tmp32 & 0xffff)) {
298 *res = tmp32;
299 return 1;
302 return 0; /* nothing here */
305 #define HQ2_MYSTERY_OFFS 0x6A07C
306 #define NEWPORT_USTATUS_OFFS 0xF133C
308 static int ip22_is_gr2(unsigned long addr)
310 u32 tmp;
311 u32 *ptr;
313 /* HQ2 only allows 32bit accesses */
314 ptr = (void *)CKSEG1ADDR(addr + HQ2_MYSTERY_OFFS);
315 if (!get_dbe(tmp, ptr)) {
316 if (tmp == 0xdeadbeef)
317 return 1;
319 return 0;
323 static void ip22_check_gio(int slotno, unsigned long addr, int irq)
325 const char *name = "Unknown";
326 struct gio_device *gio_dev;
327 u32 tmp;
328 __u8 id;
329 int i;
331 /* first look for GR2/GR3 by checking mystery register */
332 if (ip22_is_gr2(addr))
333 tmp = 0x7f;
334 else {
335 if (!ip22_gio_id(addr, &tmp)) {
337 * no GIO signature at start address of slot
338 * since Newport doesn't have one, we check if
339 * user status register is readable
341 if (ip22_gio_id(addr + NEWPORT_USTATUS_OFFS, &tmp))
342 tmp = 0x7e;
343 else
344 tmp = 0;
347 if (tmp) {
348 id = GIO_ID(tmp);
349 if (tmp & GIO_32BIT_ID) {
350 if (tmp & GIO_64BIT_IFACE)
351 ip22_gio_set_64bit(slotno);
353 for (i = 0; i < ARRAY_SIZE(gio_name_table); i++) {
354 if (id == gio_name_table[i].id) {
355 name = gio_name_table[i].name;
356 break;
359 printk(KERN_INFO "GIO: slot %d : %s (id %x)\n",
360 slotno, name, id);
361 gio_dev = kzalloc(sizeof *gio_dev, GFP_KERNEL);
362 gio_dev->name = name;
363 gio_dev->slotno = slotno;
364 gio_dev->id.id = id;
365 gio_dev->resource.start = addr;
366 gio_dev->resource.end = addr + 0x3fffff;
367 gio_dev->resource.flags = IORESOURCE_MEM;
368 gio_dev->irq = irq;
369 dev_set_name(&gio_dev->dev, "%d", slotno);
370 gio_device_register(gio_dev);
371 } else
372 printk(KERN_INFO "GIO: slot %d : Empty\n", slotno);
375 static struct bus_type gio_bus_type = {
376 .name = "gio",
377 .dev_attrs = gio_dev_attrs,
378 .match = gio_bus_match,
379 .probe = gio_device_probe,
380 .remove = gio_device_remove,
381 .shutdown = gio_device_shutdown,
382 .uevent = gio_device_uevent,
385 static struct resource gio_bus_resource = {
386 .start = GIO_SLOT_GFX_BASE,
387 .end = GIO_SLOT_GFX_BASE + 0x9fffff,
388 .name = "GIO Bus",
389 .flags = IORESOURCE_MEM,
392 int __init ip22_gio_init(void)
394 unsigned int pbdma __maybe_unused;
395 int ret;
397 ret = device_register(&gio_bus);
398 if (ret) {
399 put_device(&gio_bus);
400 return ret;
403 ret = bus_register(&gio_bus_type);
404 if (!ret) {
405 request_resource(&iomem_resource, &gio_bus_resource);
406 printk(KERN_INFO "GIO: Probing bus...\n");
408 if (ip22_is_fullhouse()) {
409 /* Indigo2 */
410 ip22_check_gio(0, GIO_SLOT_GFX_BASE, SGI_GIO_1_IRQ);
411 ip22_check_gio(1, GIO_SLOT_EXP0_BASE, SGI_GIO_1_IRQ);
412 } else {
413 /* Indy/Challenge S */
414 if (get_dbe(pbdma, (unsigned int *)&hpc3c1->pbdma[1]))
415 ip22_check_gio(0, GIO_SLOT_GFX_BASE,
416 SGI_GIO_0_IRQ);
417 ip22_check_gio(1, GIO_SLOT_EXP0_BASE, SGI_GIOEXP0_IRQ);
418 ip22_check_gio(2, GIO_SLOT_EXP1_BASE, SGI_GIOEXP1_IRQ);
420 } else
421 device_unregister(&gio_bus);
423 return ret;
426 subsys_initcall(ip22_gio_init);