GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / dio / dio.c
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 static const char *unknowndioname 
92         = "unknown DIO board -- please email <linux-m68k@lists.linux-m68k.org>!";
93
94 static const char *dio_getname(int id)
95 {
96         /* return pointer to a constant string describing the board with given ID */
97         unsigned int i;
98         for (i = 0; i < ARRAY_SIZE(names); i++)
99                 if (names[i].id == id) 
100                         return names[i].name;
101
102         return unknowndioname;
103 }
104
105 #else
106
107 static char dio_no_name[] = { 0 };
108 #define dio_getname(_id)        (dio_no_name)
109
110 #endif /* CONFIG_DIO_CONSTANTS */
111
112 static void dio_dev_release(struct device *dev)
113 {
114         struct dio_dev *ddev = container_of(dev, typeof(struct dio_dev), dev);
115         kfree(ddev);
116 }
117
118 int __init dio_find(int deviceid)
119 {
120         /* Called to find a DIO device before the full bus scan has run.
121          * Only used by the console driver.
122          */
123         int scode, id;
124         u_char prid, secid, i;
125         mm_segment_t fs;
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                 fs = get_fs();
145                 set_fs(KERNEL_DS);
146
147                 if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
148                         set_fs(fs);
149                         if (scode >= DIOII_SCBASE)
150                                 iounmap(va);
151                         continue;             /* no board present at that select code */
152                 }
153
154                 set_fs(fs);
155                 prid = DIO_ID(va);
156
157                 if (DIO_NEEDSSECID(prid)) {
158                         secid = DIO_SECID(va);
159                         id = DIO_ENCODE_ID(prid, secid);
160                 } else
161                         id = prid;
162
163                 if (id == deviceid) {
164                         if (scode >= DIOII_SCBASE)
165                                 iounmap(va);
166                         return scode;
167                 }
168         }
169
170         return -1;
171 }
172
173 /* This is the function that scans the DIO space and works out what
174  * hardware is actually present.
175  */
176 static int __init dio_init(void)
177 {
178         int scode;
179         mm_segment_t fs;
180         int i;
181         struct dio_dev *dev;
182         int error;
183
184         if (!MACH_IS_HP300)
185                 return 0;
186
187         printk(KERN_INFO "Scanning for DIO devices...\n");
188
189         /* Initialize the DIO bus */ 
190         INIT_LIST_HEAD(&dio_bus.devices);
191         dev_set_name(&dio_bus.dev, "dio");
192         error = device_register(&dio_bus.dev);
193         if (error) {
194                 pr_err("DIO: Error registering dio_bus\n");
195                 return error;
196         }
197
198         /* Request all resources */
199         dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2);
200         for (i = 0; i < dio_bus.num_resources; i++)
201                 request_resource(&iomem_resource, &dio_bus.resources[i]);
202
203         /* Register all devices */
204         for (scode = 0; scode < DIO_SCMAX; ++scode)
205         {
206                 u_char prid, secid = 0;        /* primary, secondary ID bytes */
207                 u_char *va;
208                 unsigned long pa;
209                 
210                 if (DIO_SCINHOLE(scode))
211                         continue;
212
213                 pa = dio_scodetophysaddr(scode);
214
215                 if (!pa)
216                         continue;
217
218                 if (scode < DIOII_SCBASE)
219                         va = (void *)(pa + DIO_VIRADDRBASE);
220                 else
221                         va = ioremap(pa, PAGE_SIZE);
222
223                 fs = get_fs();
224                 set_fs(KERNEL_DS);
225
226                 if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
227                         set_fs(fs);
228                         if (scode >= DIOII_SCBASE)
229                                 iounmap(va);
230                         continue;              /* no board present at that select code */
231                 }
232
233                 set_fs(fs);
234
235                 /* Found a board, allocate it an entry in the list */
236                 dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL);
237                 if (!dev)
238                         return 0;
239
240                 dev->bus = &dio_bus;
241                 dev->dev.parent = &dio_bus.dev;
242                 dev->dev.bus = &dio_bus_type;
243                 dev->dev.release = dio_dev_release;
244                 dev->scode = scode;
245                 dev->resource.start = pa;
246                 dev->resource.end = pa + DIO_SIZE(scode, va);
247                 dev_set_name(&dev->dev, "%02x", scode);
248
249                 /* read the ID byte(s) and encode if necessary. */
250                 prid = DIO_ID(va);
251
252                 if (DIO_NEEDSSECID(prid)) {
253                         secid = DIO_SECID(va);
254                         dev->id = DIO_ENCODE_ID(prid, secid);
255                 } else
256                         dev->id = prid;
257
258                 dev->ipl = DIO_IPL(va);
259                 strcpy(dev->name,dio_getname(dev->id));
260                 printk(KERN_INFO "select code %3d: ipl %d: ID %02X", dev->scode, dev->ipl, prid);
261                 if (DIO_NEEDSSECID(prid))
262                         printk(":%02X", secid);
263                 printk(": %s\n", dev->name);
264
265                 if (scode >= DIOII_SCBASE)
266                         iounmap(va);
267                 error = device_register(&dev->dev);
268                 if (error) {
269                         pr_err("DIO: Error registering device %s\n",
270                                dev->name);
271                         put_device(&dev->dev);
272                         continue;
273                 }
274                 error = dio_create_sysfs_dev_files(dev);
275                 if (error)
276                         dev_err(&dev->dev, "Error creating sysfs files\n");
277         }
278         return 0;
279 }
280
281 subsys_initcall(dio_init);
282
283 /* Bear in mind that this is called in the very early stages of initialisation
284  * in order to get the address of the serial port for the console...
285  */
286 unsigned long dio_scodetophysaddr(int scode)
287 {
288         if (scode >= DIOII_SCBASE) {
289                 return (DIOII_BASE + (scode - 132) * DIOII_DEVSIZE);
290         } else if (scode > DIO_SCMAX || scode < 0)
291                 return 0;
292         else if (DIO_SCINHOLE(scode))
293                 return 0;
294
295         return (DIO_BASE + scode * DIO_DEVSIZE);
296 }