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