xref: /openbmc/linux/drivers/dio/dio.c (revision e868d61272caa648214046a096e5a6bfc068dc8c)
1 /* Code to support devices on the DIO and DIO-II bus
2  * Copyright (C) 05/1998 Peter Maydell <pmaydell@chiark.greenend.org.uk>
3  * Copyright (C) 2004 Jochen Friedrich <jochen@scram.de>
4  *
5  * This code has basically these routines at the moment:
6  * int dio_find(u_int deviceid)
7  *    Search the list of DIO devices and return the select code
8  *    of the next unconfigured device found that matches the given device ID.
9  *    Note that the deviceid parameter should be the encoded ID.
10  *    This means that framebuffers should pass it as
11  *    DIO_ENCODE_ID(DIO_ID_FBUFFER,DIO_ID2_TOPCAT)
12  *    (or whatever); everybody else just uses DIO_ID_FOOBAR.
13  * unsigned long dio_scodetophysaddr(int scode)
14  *    Return the physical address corresponding to the given select code.
15  * int dio_scodetoipl(int scode)
16  *    Every DIO card has a fixed interrupt priority level. This function
17  *    returns it, whatever it is.
18  * const char *dio_scodetoname(int scode)
19  *    Return a character string describing this board [might be "" if
20  *    not CONFIG_DIO_CONSTANTS]
21  * void dio_config_board(int scode)     mark board as configured in the list
22  * void dio_unconfig_board(int scode)   mark board as no longer configured
23  *
24  * This file is based on the way the Amiga port handles Zorro II cards,
25  * although we aren't so complicated...
26  */
27 #include <linux/module.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/kernel.h>
31 #include <linux/init.h>
32 #include <linux/dio.h>
33 #include <linux/slab.h>                         /* kmalloc() */
34 #include <asm/uaccess.h>
35 #include <asm/io.h>                             /* readb() */
36 
37 struct dio_bus dio_bus = {
38 	.resources = {
39 		/* DIO range */
40 		{ .name = "DIO mem", .start = 0x00600000, .end = 0x007fffff },
41 		/* DIO-II range */
42 		{ .name = "DIO-II mem", .start = 0x01000000, .end = 0x1fffffff }
43 	},
44 	.name = "DIO bus"
45 };
46 
47 /* not a real config option yet! */
48 #define CONFIG_DIO_CONSTANTS
49 
50 #ifdef CONFIG_DIO_CONSTANTS
51 /* We associate each numeric ID with an appropriate descriptive string
52  * using a constant array of these structs.
53  * FIXME: we should be able to arrange to throw away most of the strings
54  * using the initdata stuff. Then we wouldn't need to worry about
55  * carrying them around...
56  * I think we do this by copying them into newly kmalloc()ed memory and
57  * marking the names[] array as .initdata ?
58  */
59 struct dioname
60 {
61         int id;
62         const char *name;
63 };
64 
65 /* useful macro */
66 #define DIONAME(x) { DIO_ID_##x, DIO_DESC_##x }
67 #define DIOFBNAME(x) { DIO_ENCODE_ID( DIO_ID_FBUFFER, DIO_ID2_##x), DIO_DESC2_##x }
68 
69 static struct dioname names[] =
70 {
71         DIONAME(DCA0), DIONAME(DCA0REM), DIONAME(DCA1), DIONAME(DCA1REM),
72         DIONAME(DCM), DIONAME(DCMREM),
73         DIONAME(LAN),
74         DIONAME(FHPIB), DIONAME(NHPIB),
75         DIONAME(SCSI0), DIONAME(SCSI1), DIONAME(SCSI2), DIONAME(SCSI3),
76         DIONAME(FBUFFER),
77         DIONAME(PARALLEL), DIONAME(VME), DIONAME(DCL), DIONAME(DCLREM),
78         DIONAME(MISC0), DIONAME(MISC1), DIONAME(MISC2), DIONAME(MISC3),
79         DIONAME(MISC4), DIONAME(MISC5), DIONAME(MISC6), DIONAME(MISC7),
80         DIONAME(MISC8), DIONAME(MISC9), DIONAME(MISC10), DIONAME(MISC11),
81         DIONAME(MISC12), DIONAME(MISC13),
82         DIOFBNAME(GATORBOX), DIOFBNAME(TOPCAT), DIOFBNAME(RENAISSANCE),
83         DIOFBNAME(LRCATSEYE), DIOFBNAME(HRCCATSEYE), DIOFBNAME(HRMCATSEYE),
84         DIOFBNAME(DAVINCI), DIOFBNAME(XXXCATSEYE), DIOFBNAME(HYPERION),
85         DIOFBNAME(XGENESIS), DIOFBNAME(TIGER), DIOFBNAME(YGENESIS)
86 };
87 
88 #undef DIONAME
89 #undef DIOFBNAME
90 
91 #define NUMNAMES (sizeof(names) / sizeof(struct dioname))
92 
93 static const char *unknowndioname
94         = "unknown DIO board -- please email <linux-m68k@lists.linux-m68k.org>!";
95 
96 static const char *dio_getname(int id)
97 {
98         /* return pointer to a constant string describing the board with given ID */
99 	unsigned int i;
100         for (i = 0; i < NUMNAMES; i++)
101                 if (names[i].id == id)
102                         return names[i].name;
103 
104         return unknowndioname;
105 }
106 
107 #else
108 
109 static char dio_no_name[] = { 0 };
110 #define dio_getname(_id)	(dio_no_name)
111 
112 #endif /* CONFIG_DIO_CONSTANTS */
113 
114 int __init dio_find(int deviceid)
115 {
116 	/* Called to find a DIO device before the full bus scan has run.
117 	 * Only used by the console driver.
118 	 */
119 	int scode, id;
120 	u_char prid, secid, i;
121 	mm_segment_t fs;
122 
123 	for (scode = 0; scode < DIO_SCMAX; scode++) {
124 		void *va;
125 		unsigned long pa;
126 
127                 if (DIO_SCINHOLE(scode))
128                         continue;
129 
130                 pa = dio_scodetophysaddr(scode);
131 
132 		if (!pa)
133 			continue;
134 
135 		if (scode < DIOII_SCBASE)
136 			va = (void *)(pa + DIO_VIRADDRBASE);
137 		else
138 			va = ioremap(pa, PAGE_SIZE);
139 
140 		fs = get_fs();
141 		set_fs(KERNEL_DS);
142 
143                 if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
144 			set_fs(fs);
145 			if (scode >= DIOII_SCBASE)
146 				iounmap(va);
147                         continue;             /* no board present at that select code */
148 		}
149 
150 		set_fs(fs);
151 		prid = DIO_ID(va);
152 
153                 if (DIO_NEEDSSECID(prid)) {
154                         secid = DIO_SECID(va);
155                         id = DIO_ENCODE_ID(prid, secid);
156                 } else
157 			id = prid;
158 
159 		if (id == deviceid) {
160 			if (scode >= DIOII_SCBASE)
161 				iounmap(va);
162 			return scode;
163 		}
164 	}
165 
166 	return -1;
167 }
168 
169 /* This is the function that scans the DIO space and works out what
170  * hardware is actually present.
171  */
172 static int __init dio_init(void)
173 {
174 	int scode;
175 	mm_segment_t fs;
176 	int i;
177 	struct dio_dev *dev;
178 
179 	if (!MACH_IS_HP300)
180 		return 0;
181 
182         printk(KERN_INFO "Scanning for DIO devices...\n");
183 
184 	/* Initialize the DIO bus */
185 	INIT_LIST_HEAD(&dio_bus.devices);
186 	strcpy(dio_bus.dev.bus_id, "dio");
187 	device_register(&dio_bus.dev);
188 
189 	/* Request all resources */
190 	dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2);
191 	for (i = 0; i < dio_bus.num_resources; i++)
192 		request_resource(&iomem_resource, &dio_bus.resources[i]);
193 
194 	/* Register all devices */
195         for (scode = 0; scode < DIO_SCMAX; ++scode)
196         {
197                 u_char prid, secid = 0;        /* primary, secondary ID bytes */
198                 u_char *va;
199 		unsigned long pa;
200 
201                 if (DIO_SCINHOLE(scode))
202                         continue;
203 
204 		pa = dio_scodetophysaddr(scode);
205 
206 		if (!pa)
207 			continue;
208 
209 		if (scode < DIOII_SCBASE)
210 			va = (void *)(pa + DIO_VIRADDRBASE);
211 		else
212 			va = ioremap(pa, PAGE_SIZE);
213 
214 		fs = get_fs();
215 		set_fs(KERNEL_DS);
216 
217                 if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
218 			set_fs(fs);
219 			if (scode >= DIOII_SCBASE)
220 				iounmap(va);
221                         continue;              /* no board present at that select code */
222 		}
223 
224 		set_fs(fs);
225 
226                 /* Found a board, allocate it an entry in the list */
227 		dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL);
228 		if (!dev)
229 			return 0;
230 
231 		dev->bus = &dio_bus;
232 		dev->dev.parent = &dio_bus.dev;
233 		dev->dev.bus = &dio_bus_type;
234 		dev->scode = scode;
235 		dev->resource.start = pa;
236 		dev->resource.end = pa + DIO_SIZE(scode, va);
237 		sprintf(dev->dev.bus_id,"%02x", scode);
238 
239                 /* read the ID byte(s) and encode if necessary. */
240 		prid = DIO_ID(va);
241 
242                 if (DIO_NEEDSSECID(prid)) {
243                         secid = DIO_SECID(va);
244                         dev->id = DIO_ENCODE_ID(prid, secid);
245                 } else
246                         dev->id = prid;
247 
248                 dev->ipl = DIO_IPL(va);
249                 strcpy(dev->name,dio_getname(dev->id));
250                 printk(KERN_INFO "select code %3d: ipl %d: ID %02X", dev->scode, dev->ipl, prid);
251                 if (DIO_NEEDSSECID(prid))
252                         printk(":%02X", secid);
253                 printk(": %s\n", dev->name);
254 
255 		if (scode >= DIOII_SCBASE)
256 			iounmap(va);
257 		device_register(&dev->dev);
258 		dio_create_sysfs_dev_files(dev);
259         }
260 	return 0;
261 }
262 
263 subsys_initcall(dio_init);
264 
265 /* Bear in mind that this is called in the very early stages of initialisation
266  * in order to get the address of the serial port for the console...
267  */
268 unsigned long dio_scodetophysaddr(int scode)
269 {
270         if (scode >= DIOII_SCBASE) {
271                 return (DIOII_BASE + (scode - 132) * DIOII_DEVSIZE);
272         } else if (scode > DIO_SCMAX || scode < 0)
273                 return 0;
274         else if (DIO_SCINHOLE(scode))
275                 return 0;
276 
277         return (DIO_BASE + scode * DIO_DEVSIZE);
278 }
279