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>
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>
15 static struct bus_type gio_bus_type
;
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 },
27 { .name
= "SGI Newport", .id
= 0x7e },
28 { .name
= "SGI GR2/GR3", .id
= 0x7f },
31 static void gio_bus_release(struct device
*dev
)
36 static struct device gio_bus
= {
38 .release
= &gio_bus_release
,
42 * gio_match_device - Tell if an of_device structure has a matching
44 * @ids: array of of device match structures to search in
45 * @dev: the of device structure to match against
47 * Used by a driver to check whether an of_device present in the
48 * system is in its list of supported devices.
50 static const struct gio_device_id
*
51 gio_match_device(const struct gio_device_id
*match
,
52 const struct gio_device
*dev
)
54 const struct gio_device_id
*ids
;
56 for (ids
= match
; ids
->id
!= 0xff; ids
++)
57 if (ids
->id
== dev
->id
.id
)
63 struct gio_device
*gio_dev_get(struct gio_device
*dev
)
69 tmp
= get_device(&dev
->dev
);
71 return to_gio_device(tmp
);
75 EXPORT_SYMBOL_GPL(gio_dev_get
);
77 void gio_dev_put(struct gio_device
*dev
)
80 put_device(&dev
->dev
);
82 EXPORT_SYMBOL_GPL(gio_dev_put
);
85 * gio_release_dev - free an gio device structure when all users of it are finished.
86 * @dev: device that's been disconnected
88 * Will be called only by the device core when all users of this gio device are
91 void gio_release_dev(struct device
*dev
)
93 struct gio_device
*giodev
;
95 giodev
= to_gio_device(dev
);
98 EXPORT_SYMBOL_GPL(gio_release_dev
);
100 int gio_device_register(struct gio_device
*giodev
)
102 giodev
->dev
.bus
= &gio_bus_type
;
103 giodev
->dev
.parent
= &gio_bus
;
104 return device_register(&giodev
->dev
);
106 EXPORT_SYMBOL_GPL(gio_device_register
);
108 void gio_device_unregister(struct gio_device
*giodev
)
110 device_unregister(&giodev
->dev
);
112 EXPORT_SYMBOL_GPL(gio_device_unregister
);
114 static int gio_bus_match(struct device
*dev
, struct device_driver
*drv
)
116 struct gio_device
*gio_dev
= to_gio_device(dev
);
117 struct gio_driver
*gio_drv
= to_gio_driver(drv
);
119 return gio_match_device(gio_drv
->id_table
, gio_dev
) != NULL
;
122 static int gio_device_probe(struct device
*dev
)
125 struct gio_driver
*drv
;
126 struct gio_device
*gio_dev
;
127 const struct gio_device_id
*match
;
129 drv
= to_gio_driver(dev
->driver
);
130 gio_dev
= to_gio_device(dev
);
135 gio_dev_get(gio_dev
);
137 match
= gio_match_device(drv
->id_table
, gio_dev
);
139 error
= drv
->probe(gio_dev
, match
);
141 gio_dev_put(gio_dev
);
146 static int gio_device_remove(struct device
*dev
)
148 struct gio_device
*gio_dev
= to_gio_device(dev
);
149 struct gio_driver
*drv
= to_gio_driver(dev
->driver
);
151 if (dev
->driver
&& drv
->remove
)
152 drv
->remove(gio_dev
);
156 static void gio_device_shutdown(struct device
*dev
)
158 struct gio_device
*gio_dev
= to_gio_device(dev
);
159 struct gio_driver
*drv
= to_gio_driver(dev
->driver
);
161 if (dev
->driver
&& drv
->shutdown
)
162 drv
->shutdown(gio_dev
);
165 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*a
,
168 struct gio_device
*gio_dev
= to_gio_device(dev
);
169 int len
= snprintf(buf
, PAGE_SIZE
, "gio:%x\n", gio_dev
->id
.id
);
171 return (len
>= PAGE_SIZE
) ? (PAGE_SIZE
- 1) : len
;
173 static DEVICE_ATTR_RO(modalias
);
175 static ssize_t
name_show(struct device
*dev
,
176 struct device_attribute
*attr
, char *buf
)
178 struct gio_device
*giodev
;
180 giodev
= to_gio_device(dev
);
181 return sprintf(buf
, "%s", giodev
->name
);
183 static DEVICE_ATTR_RO(name
);
185 static ssize_t
id_show(struct device
*dev
,
186 struct device_attribute
*attr
, char *buf
)
188 struct gio_device
*giodev
;
190 giodev
= to_gio_device(dev
);
191 return sprintf(buf
, "%x", giodev
->id
.id
);
193 static DEVICE_ATTR_RO(id
);
195 static struct attribute
*gio_dev_attrs
[] = {
196 &dev_attr_modalias
.attr
,
201 ATTRIBUTE_GROUPS(gio_dev
);
203 static int gio_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
205 struct gio_device
*gio_dev
= to_gio_device(dev
);
207 add_uevent_var(env
, "MODALIAS=gio:%x", gio_dev
->id
.id
);
211 int gio_register_driver(struct gio_driver
*drv
)
213 /* initialize common driver fields */
214 if (!drv
->driver
.name
)
215 drv
->driver
.name
= drv
->name
;
216 if (!drv
->driver
.owner
)
217 drv
->driver
.owner
= drv
->owner
;
218 drv
->driver
.bus
= &gio_bus_type
;
220 /* register with core */
221 return driver_register(&drv
->driver
);
223 EXPORT_SYMBOL_GPL(gio_register_driver
);
225 void gio_unregister_driver(struct gio_driver
*drv
)
227 driver_unregister(&drv
->driver
);
229 EXPORT_SYMBOL_GPL(gio_unregister_driver
);
231 void gio_set_master(struct gio_device
*dev
)
233 u32 tmp
= sgimc
->giopar
;
235 switch (dev
->slotno
) {
237 tmp
|= SGIMC_GIOPAR_MASTERGFX
;
240 tmp
|= SGIMC_GIOPAR_MASTEREXP0
;
243 tmp
|= SGIMC_GIOPAR_MASTEREXP1
;
248 EXPORT_SYMBOL_GPL(gio_set_master
);
250 void ip22_gio_set_64bit(int slotno
)
252 u32 tmp
= sgimc
->giopar
;
256 tmp
|= SGIMC_GIOPAR_GFX64
;
259 tmp
|= SGIMC_GIOPAR_EXP064
;
262 tmp
|= SGIMC_GIOPAR_EXP164
;
268 static int ip22_gio_id(unsigned long addr
, u32
*res
)
277 ptr32
= (void *)CKSEG1ADDR(addr
);
278 if (!get_dbe(tmp32
, ptr32
)) {
280 * We got no DBE, but this doesn't mean anything.
281 * If GIO is pipelined (which can't be disabled
282 * for GFX slot) we don't get a DBE, but we see
283 * the transfer size as data. So we do an 8bit
284 * and a 16bit access and check whether the common
287 ptr8
= (void *)CKSEG1ADDR(addr
+ 3);
288 if (get_dbe(tmp8
, ptr8
)) {
290 * 32bit access worked, but 8bit doesn't
291 * so we don't see phantom reads on
292 * a pipelined bus, but a real card which
293 * doesn't support 8 bit reads
298 ptr16
= (void *)CKSEG1ADDR(addr
+ 2);
299 get_dbe(tmp16
, ptr16
);
300 if (tmp8
== (tmp16
& 0xff) &&
301 tmp8
== (tmp32
& 0xff) &&
302 tmp16
== (tmp32
& 0xffff)) {
307 return 0; /* nothing here */
310 #define HQ2_MYSTERY_OFFS 0x6A07C
311 #define NEWPORT_USTATUS_OFFS 0xF133C
313 static int ip22_is_gr2(unsigned long addr
)
318 /* HQ2 only allows 32bit accesses */
319 ptr
= (void *)CKSEG1ADDR(addr
+ HQ2_MYSTERY_OFFS
);
320 if (!get_dbe(tmp
, ptr
)) {
321 if (tmp
== 0xdeadbeef)
328 static void ip22_check_gio(int slotno
, unsigned long addr
, int irq
)
330 const char *name
= "Unknown";
331 struct gio_device
*gio_dev
;
336 /* first look for GR2/GR3 by checking mystery register */
337 if (ip22_is_gr2(addr
))
340 if (!ip22_gio_id(addr
, &tmp
)) {
342 * no GIO signature at start address of slot
343 * since Newport doesn't have one, we check if
344 * user status register is readable
346 if (ip22_gio_id(addr
+ NEWPORT_USTATUS_OFFS
, &tmp
))
354 if (tmp
& GIO_32BIT_ID
) {
355 if (tmp
& GIO_64BIT_IFACE
)
356 ip22_gio_set_64bit(slotno
);
358 for (i
= 0; i
< ARRAY_SIZE(gio_name_table
); i
++) {
359 if (id
== gio_name_table
[i
].id
) {
360 name
= gio_name_table
[i
].name
;
364 printk(KERN_INFO
"GIO: slot %d : %s (id %x)\n",
366 gio_dev
= kzalloc(sizeof *gio_dev
, GFP_KERNEL
);
367 gio_dev
->name
= name
;
368 gio_dev
->slotno
= slotno
;
370 gio_dev
->resource
.start
= addr
;
371 gio_dev
->resource
.end
= addr
+ 0x3fffff;
372 gio_dev
->resource
.flags
= IORESOURCE_MEM
;
374 dev_set_name(&gio_dev
->dev
, "%d", slotno
);
375 gio_device_register(gio_dev
);
377 printk(KERN_INFO
"GIO: slot %d : Empty\n", slotno
);
380 static struct bus_type gio_bus_type
= {
382 .dev_groups
= gio_dev_groups
,
383 .match
= gio_bus_match
,
384 .probe
= gio_device_probe
,
385 .remove
= gio_device_remove
,
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
);
404 put_device(&gio_bus
);
408 ret
= bus_register(&gio_bus_type
);
410 request_resource(&iomem_resource
, &gio_bus_resource
);
411 printk(KERN_INFO
"GIO: Probing bus...\n");
413 if (ip22_is_fullhouse()) {
415 ip22_check_gio(0, GIO_SLOT_GFX_BASE
, SGI_GIO_1_IRQ
);
416 ip22_check_gio(1, GIO_SLOT_EXP0_BASE
, SGI_GIO_1_IRQ
);
418 /* Indy/Challenge S */
419 if (get_dbe(pbdma
, (unsigned int *)&hpc3c1
->pbdma
[1]))
420 ip22_check_gio(0, GIO_SLOT_GFX_BASE
,
422 ip22_check_gio(1, GIO_SLOT_EXP0_BASE
, SGI_GIOEXP0_IRQ
);
423 ip22_check_gio(2, GIO_SLOT_EXP1_BASE
, SGI_GIOEXP1_IRQ
);
426 device_unregister(&gio_bus
);
431 subsys_initcall(ip22_gio_init
);