xref: /linux/drivers/dio/dio.c (revision 2e4c77bea3d8b17d94f8ee382411f359b708560f)
11da177e4SLinus Torvalds /* Code to support devices on the DIO and DIO-II bus
21da177e4SLinus Torvalds  * Copyright (C) 05/1998 Peter Maydell <pmaydell@chiark.greenend.org.uk>
31da177e4SLinus Torvalds  * Copyright (C) 2004 Jochen Friedrich <jochen@scram.de>
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This code has basically these routines at the moment:
61da177e4SLinus Torvalds  * int dio_find(u_int deviceid)
71da177e4SLinus Torvalds  *    Search the list of DIO devices and return the select code
81da177e4SLinus Torvalds  *    of the next unconfigured device found that matches the given device ID.
91da177e4SLinus Torvalds  *    Note that the deviceid parameter should be the encoded ID.
101da177e4SLinus Torvalds  *    This means that framebuffers should pass it as
111da177e4SLinus Torvalds  *    DIO_ENCODE_ID(DIO_ID_FBUFFER,DIO_ID2_TOPCAT)
121da177e4SLinus Torvalds  *    (or whatever); everybody else just uses DIO_ID_FOOBAR.
131da177e4SLinus Torvalds  * unsigned long dio_scodetophysaddr(int scode)
141da177e4SLinus Torvalds  *    Return the physical address corresponding to the given select code.
151da177e4SLinus Torvalds  * int dio_scodetoipl(int scode)
161da177e4SLinus Torvalds  *    Every DIO card has a fixed interrupt priority level. This function
171da177e4SLinus Torvalds  *    returns it, whatever it is.
181da177e4SLinus Torvalds  * const char *dio_scodetoname(int scode)
191da177e4SLinus Torvalds  *    Return a character string describing this board [might be "" if
201da177e4SLinus Torvalds  *    not CONFIG_DIO_CONSTANTS]
211da177e4SLinus Torvalds  * void dio_config_board(int scode)     mark board as configured in the list
221da177e4SLinus Torvalds  * void dio_unconfig_board(int scode)   mark board as no longer configured
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * This file is based on the way the Amiga port handles Zorro II cards,
251da177e4SLinus Torvalds  * although we aren't so complicated...
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds #include <linux/module.h>
281da177e4SLinus Torvalds #include <linux/string.h>
291da177e4SLinus Torvalds #include <linux/types.h>
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/init.h>
321da177e4SLinus Torvalds #include <linux/dio.h>
331da177e4SLinus Torvalds #include <linux/slab.h>                         /* kmalloc() */
341da177e4SLinus Torvalds #include <asm/uaccess.h>
351da177e4SLinus Torvalds #include <asm/io.h>                             /* readb() */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds struct dio_bus dio_bus = {
381da177e4SLinus Torvalds 	.resources = {
391da177e4SLinus Torvalds 		/* DIO range */
401da177e4SLinus Torvalds 		{ .name = "DIO mem", .start = 0x00600000, .end = 0x007fffff },
411da177e4SLinus Torvalds 		/* DIO-II range */
421da177e4SLinus Torvalds 		{ .name = "DIO-II mem", .start = 0x01000000, .end = 0x1fffffff }
431da177e4SLinus Torvalds 	},
441da177e4SLinus Torvalds 	.name = "DIO bus"
451da177e4SLinus Torvalds };
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /* not a real config option yet! */
481da177e4SLinus Torvalds #define CONFIG_DIO_CONSTANTS
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #ifdef CONFIG_DIO_CONSTANTS
511da177e4SLinus Torvalds /* We associate each numeric ID with an appropriate descriptive string
521da177e4SLinus Torvalds  * using a constant array of these structs.
531da177e4SLinus Torvalds  * FIXME: we should be able to arrange to throw away most of the strings
541da177e4SLinus Torvalds  * using the initdata stuff. Then we wouldn't need to worry about
551da177e4SLinus Torvalds  * carrying them around...
561da177e4SLinus Torvalds  * I think we do this by copying them into newly kmalloc()ed memory and
571da177e4SLinus Torvalds  * marking the names[] array as .initdata ?
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds struct dioname
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds         int id;
621da177e4SLinus Torvalds         const char *name;
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* useful macro */
661da177e4SLinus Torvalds #define DIONAME(x) { DIO_ID_##x, DIO_DESC_##x }
671da177e4SLinus Torvalds #define DIOFBNAME(x) { DIO_ENCODE_ID( DIO_ID_FBUFFER, DIO_ID2_##x), DIO_DESC2_##x }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds static struct dioname names[] =
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds         DIONAME(DCA0), DIONAME(DCA0REM), DIONAME(DCA1), DIONAME(DCA1REM),
721da177e4SLinus Torvalds         DIONAME(DCM), DIONAME(DCMREM),
731da177e4SLinus Torvalds         DIONAME(LAN),
741da177e4SLinus Torvalds         DIONAME(FHPIB), DIONAME(NHPIB),
751da177e4SLinus Torvalds         DIONAME(SCSI0), DIONAME(SCSI1), DIONAME(SCSI2), DIONAME(SCSI3),
761da177e4SLinus Torvalds         DIONAME(FBUFFER),
771da177e4SLinus Torvalds         DIONAME(PARALLEL), DIONAME(VME), DIONAME(DCL), DIONAME(DCLREM),
781da177e4SLinus Torvalds         DIONAME(MISC0), DIONAME(MISC1), DIONAME(MISC2), DIONAME(MISC3),
791da177e4SLinus Torvalds         DIONAME(MISC4), DIONAME(MISC5), DIONAME(MISC6), DIONAME(MISC7),
801da177e4SLinus Torvalds         DIONAME(MISC8), DIONAME(MISC9), DIONAME(MISC10), DIONAME(MISC11),
811da177e4SLinus Torvalds         DIONAME(MISC12), DIONAME(MISC13),
821da177e4SLinus Torvalds         DIOFBNAME(GATORBOX), DIOFBNAME(TOPCAT), DIOFBNAME(RENAISSANCE),
831da177e4SLinus Torvalds         DIOFBNAME(LRCATSEYE), DIOFBNAME(HRCCATSEYE), DIOFBNAME(HRMCATSEYE),
841da177e4SLinus Torvalds         DIOFBNAME(DAVINCI), DIOFBNAME(XXXCATSEYE), DIOFBNAME(HYPERION),
851da177e4SLinus Torvalds         DIOFBNAME(XGENESIS), DIOFBNAME(TIGER), DIOFBNAME(YGENESIS)
861da177e4SLinus Torvalds };
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #undef DIONAME
891da177e4SLinus Torvalds #undef DIOFBNAME
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds static const char *unknowndioname
921da177e4SLinus Torvalds         = "unknown DIO board -- please email <linux-m68k@lists.linux-m68k.org>!";
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds static const char *dio_getname(int id)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds         /* return pointer to a constant string describing the board with given ID */
971da177e4SLinus Torvalds 	unsigned int i;
980a8320b0SAlejandro Martinez Ruiz 	for (i = 0; i < ARRAY_SIZE(names); i++)
991da177e4SLinus Torvalds                 if (names[i].id == id)
1001da177e4SLinus Torvalds                         return names[i].name;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds         return unknowndioname;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #else
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static char dio_no_name[] = { 0 };
1081da177e4SLinus Torvalds #define dio_getname(_id)	(dio_no_name)
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds #endif /* CONFIG_DIO_CONSTANTS */
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds int __init dio_find(int deviceid)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	/* Called to find a DIO device before the full bus scan has run.
1151da177e4SLinus Torvalds 	 * Only used by the console driver.
1161da177e4SLinus Torvalds 	 */
1171da177e4SLinus Torvalds 	int scode, id;
1181da177e4SLinus Torvalds 	u_char prid, secid, i;
1191da177e4SLinus Torvalds 	mm_segment_t fs;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	for (scode = 0; scode < DIO_SCMAX; scode++) {
1221da177e4SLinus Torvalds 		void *va;
1231da177e4SLinus Torvalds 		unsigned long pa;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds                 if (DIO_SCINHOLE(scode))
1261da177e4SLinus Torvalds                         continue;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds                 pa = dio_scodetophysaddr(scode);
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 		if (!pa)
1311da177e4SLinus Torvalds 			continue;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 		if (scode < DIOII_SCBASE)
1341da177e4SLinus Torvalds 			va = (void *)(pa + DIO_VIRADDRBASE);
1351da177e4SLinus Torvalds 		else
1361da177e4SLinus Torvalds 			va = ioremap(pa, PAGE_SIZE);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 		fs = get_fs();
1391da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds                 if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
1421da177e4SLinus Torvalds 			set_fs(fs);
1431da177e4SLinus Torvalds 			if (scode >= DIOII_SCBASE)
1441da177e4SLinus Torvalds 				iounmap(va);
1451da177e4SLinus Torvalds                         continue;             /* no board present at that select code */
1461da177e4SLinus Torvalds 		}
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 		set_fs(fs);
1491da177e4SLinus Torvalds 		prid = DIO_ID(va);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds                 if (DIO_NEEDSSECID(prid)) {
1521da177e4SLinus Torvalds                         secid = DIO_SECID(va);
1531da177e4SLinus Torvalds                         id = DIO_ENCODE_ID(prid, secid);
1541da177e4SLinus Torvalds                 } else
1551da177e4SLinus Torvalds 			id = prid;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 		if (id == deviceid) {
1581da177e4SLinus Torvalds 			if (scode >= DIOII_SCBASE)
1591da177e4SLinus Torvalds 				iounmap(va);
1601da177e4SLinus Torvalds 			return scode;
1611da177e4SLinus Torvalds 		}
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	return -1;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /* This is the function that scans the DIO space and works out what
1681da177e4SLinus Torvalds  * hardware is actually present.
1691da177e4SLinus Torvalds  */
1701da177e4SLinus Torvalds static int __init dio_init(void)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	int scode;
1731da177e4SLinus Torvalds 	mm_segment_t fs;
1741da177e4SLinus Torvalds 	int i;
1751da177e4SLinus Torvalds 	struct dio_dev *dev;
176*2e4c77beSGeert Uytterhoeven 	int error;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	if (!MACH_IS_HP300)
1791da177e4SLinus Torvalds 		return 0;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds         printk(KERN_INFO "Scanning for DIO devices...\n");
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	/* Initialize the DIO bus */
1841da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dio_bus.devices);
1851da177e4SLinus Torvalds 	strcpy(dio_bus.dev.bus_id, "dio");
186*2e4c77beSGeert Uytterhoeven 	error = device_register(&dio_bus.dev);
187*2e4c77beSGeert Uytterhoeven 	if (error) {
188*2e4c77beSGeert Uytterhoeven 		pr_err("DIO: Error registering dio_bus\n");
189*2e4c77beSGeert Uytterhoeven 		return error;
190*2e4c77beSGeert Uytterhoeven 	}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/* Request all resources */
1931da177e4SLinus Torvalds 	dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2);
1941da177e4SLinus Torvalds 	for (i = 0; i < dio_bus.num_resources; i++)
1951da177e4SLinus Torvalds 		request_resource(&iomem_resource, &dio_bus.resources[i]);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	/* Register all devices */
1981da177e4SLinus Torvalds         for (scode = 0; scode < DIO_SCMAX; ++scode)
1991da177e4SLinus Torvalds         {
2001da177e4SLinus Torvalds                 u_char prid, secid = 0;        /* primary, secondary ID bytes */
2011da177e4SLinus Torvalds                 u_char *va;
2021da177e4SLinus Torvalds 		unsigned long pa;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds                 if (DIO_SCINHOLE(scode))
2051da177e4SLinus Torvalds                         continue;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 		pa = dio_scodetophysaddr(scode);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 		if (!pa)
2101da177e4SLinus Torvalds 			continue;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 		if (scode < DIOII_SCBASE)
2131da177e4SLinus Torvalds 			va = (void *)(pa + DIO_VIRADDRBASE);
2141da177e4SLinus Torvalds 		else
2151da177e4SLinus Torvalds 			va = ioremap(pa, PAGE_SIZE);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 		fs = get_fs();
2181da177e4SLinus Torvalds 		set_fs(KERNEL_DS);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds                 if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
2211da177e4SLinus Torvalds 			set_fs(fs);
2221da177e4SLinus Torvalds 			if (scode >= DIOII_SCBASE)
2231da177e4SLinus Torvalds 				iounmap(va);
2241da177e4SLinus Torvalds                         continue;              /* no board present at that select code */
2251da177e4SLinus Torvalds 		}
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 		set_fs(fs);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds                 /* Found a board, allocate it an entry in the list */
230e66860cbSDeepak Saxena 		dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL);
2311da177e4SLinus Torvalds 		if (!dev)
2321da177e4SLinus Torvalds 			return 0;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 		dev->bus = &dio_bus;
2351da177e4SLinus Torvalds 		dev->dev.parent = &dio_bus.dev;
2361da177e4SLinus Torvalds 		dev->dev.bus = &dio_bus_type;
2371da177e4SLinus Torvalds 		dev->scode = scode;
2381da177e4SLinus Torvalds 		dev->resource.start = pa;
2391da177e4SLinus Torvalds 		dev->resource.end = pa + DIO_SIZE(scode, va);
2401da177e4SLinus Torvalds 		sprintf(dev->dev.bus_id,"%02x", scode);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds                 /* read the ID byte(s) and encode if necessary. */
2431da177e4SLinus Torvalds 		prid = DIO_ID(va);
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds                 if (DIO_NEEDSSECID(prid)) {
2461da177e4SLinus Torvalds                         secid = DIO_SECID(va);
2471da177e4SLinus Torvalds                         dev->id = DIO_ENCODE_ID(prid, secid);
2481da177e4SLinus Torvalds                 } else
2491da177e4SLinus Torvalds                         dev->id = prid;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds                 dev->ipl = DIO_IPL(va);
2521da177e4SLinus Torvalds                 strcpy(dev->name,dio_getname(dev->id));
2531da177e4SLinus Torvalds                 printk(KERN_INFO "select code %3d: ipl %d: ID %02X", dev->scode, dev->ipl, prid);
2541da177e4SLinus Torvalds                 if (DIO_NEEDSSECID(prid))
2551da177e4SLinus Torvalds                         printk(":%02X", secid);
2561da177e4SLinus Torvalds                 printk(": %s\n", dev->name);
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 		if (scode >= DIOII_SCBASE)
2591da177e4SLinus Torvalds 			iounmap(va);
260*2e4c77beSGeert Uytterhoeven 		error = device_register(&dev->dev);
261*2e4c77beSGeert Uytterhoeven 		if (error) {
262*2e4c77beSGeert Uytterhoeven 			pr_err("DIO: Error registering device %s\n",
263*2e4c77beSGeert Uytterhoeven 			       dev->name);
264*2e4c77beSGeert Uytterhoeven 			continue;
265*2e4c77beSGeert Uytterhoeven 		}
266*2e4c77beSGeert Uytterhoeven 		error = dio_create_sysfs_dev_files(dev);
267*2e4c77beSGeert Uytterhoeven 		if (error)
268*2e4c77beSGeert Uytterhoeven 			dev_err(&dev->dev, "Error creating sysfs files\n");
2691da177e4SLinus Torvalds         }
2701da177e4SLinus Torvalds 	return 0;
2711da177e4SLinus Torvalds }
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds subsys_initcall(dio_init);
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds /* Bear in mind that this is called in the very early stages of initialisation
2761da177e4SLinus Torvalds  * in order to get the address of the serial port for the console...
2771da177e4SLinus Torvalds  */
2781da177e4SLinus Torvalds unsigned long dio_scodetophysaddr(int scode)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds         if (scode >= DIOII_SCBASE) {
2811da177e4SLinus Torvalds                 return (DIOII_BASE + (scode - 132) * DIOII_DEVSIZE);
2821da177e4SLinus Torvalds         } else if (scode > DIO_SCMAX || scode < 0)
2831da177e4SLinus Torvalds                 return 0;
2841da177e4SLinus Torvalds         else if (DIO_SCINHOLE(scode))
2851da177e4SLinus Torvalds                 return 0;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds         return (DIO_BASE + scode * DIO_DEVSIZE);
2881da177e4SLinus Torvalds }
289