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 37struct 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 51struct dioname 52{ 53 int id; 54 const char *name; 55}; 56 57/* useful macro */ 58#define DIONAME(x) { DIO_ID_##x, DIO_DESC_##x } 59#define DIOFBNAME(x) { DIO_ENCODE_ID( DIO_ID_FBUFFER, DIO_ID2_##x), DIO_DESC2_##x } 60 61static struct dioname names[] = 62{ 63 DIONAME(DCA0), DIONAME(DCA0REM), DIONAME(DCA1), DIONAME(DCA1REM), 64 DIONAME(DCM), DIONAME(DCMREM), 65 DIONAME(LAN), 66 DIONAME(FHPIB), DIONAME(NHPIB), 67 DIONAME(SCSI0), DIONAME(SCSI1), DIONAME(SCSI2), DIONAME(SCSI3), 68 DIONAME(FBUFFER), 69 DIONAME(PARALLEL), DIONAME(VME), DIONAME(DCL), DIONAME(DCLREM), 70 DIONAME(MISC0), DIONAME(MISC1), DIONAME(MISC2), DIONAME(MISC3), 71 DIONAME(MISC4), DIONAME(MISC5), DIONAME(MISC6), DIONAME(MISC7), 72 DIONAME(MISC8), DIONAME(MISC9), DIONAME(MISC10), DIONAME(MISC11), 73 DIONAME(MISC12), DIONAME(MISC13), 74 DIOFBNAME(GATORBOX), DIOFBNAME(TOPCAT), DIOFBNAME(RENAISSANCE), 75 DIOFBNAME(LRCATSEYE), DIOFBNAME(HRCCATSEYE), DIOFBNAME(HRMCATSEYE), 76 DIOFBNAME(DAVINCI), DIOFBNAME(XXXCATSEYE), DIOFBNAME(HYPERION), 77 DIOFBNAME(XGENESIS), DIOFBNAME(TIGER), DIOFBNAME(YGENESIS) 78}; 79 80#undef DIONAME 81#undef DIOFBNAME 82 83static const char *unknowndioname 84 = "unknown DIO board -- please email <linux-m68k@lists.linux-m68k.org>!"; 85 86static const char *dio_getname(int id) 87{ 88 /* return pointer to a constant string describing the board with given ID */ 89 unsigned int i; 90 for (i = 0; i < ARRAY_SIZE(names); i++) 91 if (names[i].id == id) 92 return names[i].name; 93 94 return unknowndioname; 95} 96 97#else 98 99static char dio_no_name[] = { 0 }; 100#define dio_getname(_id) (dio_no_name) 101 102#endif /* CONFIG_DIO_CONSTANTS */ 103 104int __init dio_find(int deviceid) 105{ 106 /* Called to find a DIO device before the full bus scan has run. 107 * Only used by the console driver. 108 */ 109 int scode, id; 110 u_char prid, secid, i; 111 mm_segment_t fs; 112 113 for (scode = 0; scode < DIO_SCMAX; scode++) { 114 void *va; 115 unsigned long pa; 116 117 if (DIO_SCINHOLE(scode)) 118 continue; 119 120 pa = dio_scodetophysaddr(scode); 121 122 if (!pa) 123 continue; 124 125 if (scode < DIOII_SCBASE) 126 va = (void *)(pa + DIO_VIRADDRBASE); 127 else 128 va = ioremap(pa, PAGE_SIZE); 129 130 fs = get_fs(); 131 set_fs(KERNEL_DS); 132 133 if (get_user(i, (unsigned char *)va + DIO_IDOFF)) { 134 set_fs(fs); 135 if (scode >= DIOII_SCBASE) 136 iounmap(va); 137 continue; /* no board present at that select code */ 138 } 139 140 set_fs(fs); 141 prid = DIO_ID(va); 142 143 if (DIO_NEEDSSECID(prid)) { 144 secid = DIO_SECID(va); 145 id = DIO_ENCODE_ID(prid, secid); 146 } else 147 id = prid; 148 149 if (id == deviceid) { 150 if (scode >= DIOII_SCBASE) 151 iounmap(va); 152 return scode; 153 } 154 } 155 156 return -1; 157} 158 159/* This is the function that scans the DIO space and works out what 160 * hardware is actually present. 161 */ 162static int __init dio_init(void) 163{ 164 int scode; 165 mm_segment_t fs; 166 int i; 167 struct dio_dev *dev; 168 int error; 169 170 if (!MACH_IS_HP300) 171 return 0; 172 173 printk(KERN_INFO "Scanning for DIO devices...\n"); 174 175 /* Initialize the DIO bus */ 176 INIT_LIST_HEAD(&dio_bus.devices); 177 dev_set_name(&dio_bus.dev, "dio"); 178 error = device_register(&dio_bus.dev); 179 if (error) { 180 pr_err("DIO: Error registering dio_bus\n"); 181 return error; 182 } 183 184 /* Request all resources */ 185 dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2); 186 for (i = 0; i < dio_bus.num_resources; i++) 187 request_resource(&iomem_resource, &dio_bus.resources[i]); 188 189 /* Register all devices */ 190 for (scode = 0; scode < DIO_SCMAX; ++scode) 191 { 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 fs = get_fs(); 210 set_fs(KERNEL_DS); 211 212 if (get_user(i, (unsigned char *)va + DIO_IDOFF)) { 213 set_fs(fs); 214 if (scode >= DIOII_SCBASE) 215 iounmap(va); 216 continue; /* no board present at that select code */ 217 } 218 219 set_fs(fs); 220 221 /* Found a board, allocate it an entry in the list */ 222 dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL); 223 if (!dev) 224 return 0; 225 226 dev->bus = &dio_bus; 227 dev->dev.parent = &dio_bus.dev; 228 dev->dev.bus = &dio_bus_type; 229 dev->scode = scode; 230 dev->resource.start = pa; 231 dev->resource.end = pa + DIO_SIZE(scode, va); 232 dev_set_name(&dev->dev, "%02x", scode); 233 234 /* read the ID byte(s) and encode if necessary. */ 235 prid = DIO_ID(va); 236 237 if (DIO_NEEDSSECID(prid)) { 238 secid = DIO_SECID(va); 239 dev->id = DIO_ENCODE_ID(prid, secid); 240 } else 241 dev->id = prid; 242 243 dev->ipl = DIO_IPL(va); 244 strcpy(dev->name,dio_getname(dev->id)); 245 printk(KERN_INFO "select code %3d: ipl %d: ID %02X", dev->scode, dev->ipl, prid); 246 if (DIO_NEEDSSECID(prid)) 247 printk(":%02X", secid); 248 printk(": %s\n", dev->name); 249 250 if (scode >= DIOII_SCBASE) 251 iounmap(va); 252 error = device_register(&dev->dev); 253 if (error) { 254 pr_err("DIO: Error registering device %s\n", 255 dev->name); 256 continue; 257 } 258 error = dio_create_sysfs_dev_files(dev); 259 if (error) 260 dev_err(&dev->dev, "Error creating sysfs files\n"); 261 } 262 return 0; 263} 264 265subsys_initcall(dio_init); 266 267/* Bear in mind that this is called in the very early stages of initialisation 268 * in order to get the address of the serial port for the console... 269 */ 270unsigned long dio_scodetophysaddr(int scode) 271{ 272 if (scode >= DIOII_SCBASE) { 273 return (DIOII_BASE + (scode - 132) * DIOII_DEVSIZE); 274 } else if (scode > DIO_SCMAX || scode < 0) 275 return 0; 276 else if (DIO_SCINHOLE(scode)) 277 return 0; 278 279 return (DIO_BASE + scode * DIO_DEVSIZE); 280} 281