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 <asm/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 {
62 int id;
63 const char *name;
64 };
65
66 /* useful macro */
67 #define DIONAME(x) { DIO_ID_##x, DIO_DESC_##x }
68 #define DIOFBNAME(x) { DIO_ENCODE_ID( DIO_ID_FBUFFER, DIO_ID2_##x), DIO_DESC2_##x }
69
70 static struct dioname names[] =
71 {
72 DIONAME(DCA0), DIONAME(DCA0REM), DIONAME(DCA1), DIONAME(DCA1REM),
73 DIONAME(DCM), DIONAME(DCMREM),
74 DIONAME(LAN),
75 DIONAME(FHPIB), DIONAME(NHPIB),
76 DIONAME(SCSI0), DIONAME(SCSI1), DIONAME(SCSI2), DIONAME(SCSI3),
77 DIONAME(FBUFFER),
78 DIONAME(PARALLEL), DIONAME(VME), DIONAME(DCL), DIONAME(DCLREM),
79 DIONAME(MISC0), DIONAME(MISC1), DIONAME(MISC2), DIONAME(MISC3),
80 DIONAME(MISC4), DIONAME(MISC5), DIONAME(MISC6), DIONAME(MISC7),
81 DIONAME(MISC8), DIONAME(MISC9), DIONAME(MISC10), DIONAME(MISC11),
82 DIONAME(MISC12), DIONAME(MISC13),
83 DIOFBNAME(GATORBOX), DIOFBNAME(TOPCAT), DIOFBNAME(RENAISSANCE),
84 DIOFBNAME(LRCATSEYE), DIOFBNAME(HRCCATSEYE), DIOFBNAME(HRMCATSEYE),
85 DIOFBNAME(DAVINCI), DIOFBNAME(XXXCATSEYE), DIOFBNAME(HYPERION),
86 DIOFBNAME(XGENESIS), DIOFBNAME(TIGER), DIOFBNAME(YGENESIS)
87 };
88
89 #undef DIONAME
90 #undef DIOFBNAME
91
92 static const char unknowndioname[]
93 = "unknown DIO board, please email linux-m68k@lists.linux-m68k.org";
94
dio_getname(int id)95 static const char *dio_getname(int id)
96 {
97 /* return pointer to a constant string describing the board with given ID */
98 unsigned int i;
99 for (i = 0; i < ARRAY_SIZE(names); i++)
100 if (names[i].id == id)
101 return names[i].name;
102
103 return unknowndioname;
104 }
105
106 #else
107
108 static char dio_no_name[] = { 0 };
109 #define dio_getname(_id) (dio_no_name)
110
111 #endif /* CONFIG_DIO_CONSTANTS */
112
dio_dev_release(struct device * dev)113 static void dio_dev_release(struct device *dev)
114 {
115 struct dio_dev *ddev = container_of(dev, typeof(struct dio_dev), dev);
116 kfree(ddev);
117 }
118
dio_find(int deviceid)119 int __init dio_find(int deviceid)
120 {
121 /* Called to find a DIO device before the full bus scan has run.
122 * Only used by the console driver.
123 */
124 int scode, id;
125 u_char prid, secid, i;
126
127 for (scode = 0; scode < DIO_SCMAX; scode++) {
128 void *va;
129 unsigned long pa;
130
131 if (DIO_SCINHOLE(scode))
132 continue;
133
134 pa = dio_scodetophysaddr(scode);
135
136 if (!pa)
137 continue;
138
139 if (scode < DIOII_SCBASE)
140 va = (void *)(pa + DIO_VIRADDRBASE);
141 else
142 va = ioremap(pa, PAGE_SIZE);
143
144 if (copy_from_kernel_nofault(&i,
145 (unsigned char *)va + DIO_IDOFF, 1)) {
146 if (scode >= DIOII_SCBASE)
147 iounmap(va);
148 continue; /* no board present at that select code */
149 }
150
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 */
dio_init(void)172 static int __init dio_init(void)
173 {
174 int scode;
175 int i;
176 struct dio_dev *dev;
177 int error;
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 dev_set_name(&dio_bus.dev, "dio");
187 error = device_register(&dio_bus.dev);
188 if (error) {
189 pr_err("DIO: Error registering dio_bus\n");
190 return error;
191 }
192
193 /* Request all resources */
194 dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2);
195 for (i = 0; i < dio_bus.num_resources; i++)
196 request_resource(&iomem_resource, &dio_bus.resources[i]);
197
198 /* Register all devices */
199 for (scode = 0; scode < DIO_SCMAX; ++scode)
200 {
201 u_char prid, secid = 0; /* primary, secondary ID bytes */
202 u_char *va;
203 unsigned long pa;
204
205 if (DIO_SCINHOLE(scode))
206 continue;
207
208 pa = dio_scodetophysaddr(scode);
209
210 if (!pa)
211 continue;
212
213 if (scode < DIOII_SCBASE)
214 va = (void *)(pa + DIO_VIRADDRBASE);
215 else
216 va = ioremap(pa, PAGE_SIZE);
217
218 if (copy_from_kernel_nofault(&i,
219 (unsigned char *)va + DIO_IDOFF, 1)) {
220 if (scode >= DIOII_SCBASE)
221 iounmap(va);
222 continue; /* no board present at that select code */
223 }
224
225 /* Found a board, allocate it an entry in the list */
226 dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL);
227 if (!dev)
228 return -ENOMEM;
229
230 dev->bus = &dio_bus;
231 dev->dev.parent = &dio_bus.dev;
232 dev->dev.bus = &dio_bus_type;
233 dev->dev.release = dio_dev_release;
234 dev->scode = scode;
235 dev->resource.start = pa;
236 dev->resource.end = pa + DIO_SIZE(scode, va);
237 dev_set_name(&dev->dev, "%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 error = device_register(&dev->dev);
258 if (error) {
259 pr_err("DIO: Error registering device %s\n",
260 dev->name);
261 put_device(&dev->dev);
262 continue;
263 }
264 error = dio_create_sysfs_dev_files(dev);
265 if (error)
266 dev_err(&dev->dev, "Error creating sysfs files\n");
267 }
268 return 0;
269 }
270
271 subsys_initcall(dio_init);
272
273 /* Bear in mind that this is called in the very early stages of initialisation
274 * in order to get the address of the serial port for the console...
275 */
dio_scodetophysaddr(int scode)276 unsigned long dio_scodetophysaddr(int scode)
277 {
278 if (scode >= DIOII_SCBASE) {
279 return (DIOII_BASE + (scode - 132) * DIOII_DEVSIZE);
280 } else if (scode > DIO_SCMAX || scode < 0)
281 return 0;
282 else if (DIO_SCINHOLE(scode))
283 return 0;
284
285 return (DIO_BASE + scode * DIO_DEVSIZE);
286 }
287