dio.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. struct dio_bus dio_bus = {
  37. .resources = {
  38. /* DIO range */
  39. { .name = "DIO mem", .start = 0x00600000, .end = 0x007fffff },
  40. /* DIO-II range */
  41. { .name = "DIO-II mem", .start = 0x01000000, .end = 0x1fffffff }
  42. },
  43. .name = "DIO bus"
  44. };
  45. /* not a real config option yet! */
  46. #define CONFIG_DIO_CONSTANTS
  47. #ifdef CONFIG_DIO_CONSTANTS
  48. /* We associate each numeric ID with an appropriate descriptive string
  49. * using a constant array of these structs.
  50. * FIXME: we should be able to arrange to throw away most of the strings
  51. * using the initdata stuff. Then we wouldn't need to worry about
  52. * carrying them around...
  53. * I think we do this by copying them into newly kmalloc()ed memory and
  54. * marking the names[] array as .initdata ?
  55. */
  56. struct dioname
  57. {
  58. int id;
  59. const char *name;
  60. };
  61. /* useful macro */
  62. #define DIONAME(x) { DIO_ID_##x, DIO_DESC_##x }
  63. #define DIOFBNAME(x) { DIO_ENCODE_ID( DIO_ID_FBUFFER, DIO_ID2_##x), DIO_DESC2_##x }
  64. static struct dioname names[] =
  65. {
  66. DIONAME(DCA0), DIONAME(DCA0REM), DIONAME(DCA1), DIONAME(DCA1REM),
  67. DIONAME(DCM), DIONAME(DCMREM),
  68. DIONAME(LAN),
  69. DIONAME(FHPIB), DIONAME(NHPIB),
  70. DIONAME(SCSI0), DIONAME(SCSI1), DIONAME(SCSI2), DIONAME(SCSI3),
  71. DIONAME(FBUFFER),
  72. DIONAME(PARALLEL), DIONAME(VME), DIONAME(DCL), DIONAME(DCLREM),
  73. DIONAME(MISC0), DIONAME(MISC1), DIONAME(MISC2), DIONAME(MISC3),
  74. DIONAME(MISC4), DIONAME(MISC5), DIONAME(MISC6), DIONAME(MISC7),
  75. DIONAME(MISC8), DIONAME(MISC9), DIONAME(MISC10), DIONAME(MISC11),
  76. DIONAME(MISC12), DIONAME(MISC13),
  77. DIOFBNAME(GATORBOX), DIOFBNAME(TOPCAT), DIOFBNAME(RENAISSANCE),
  78. DIOFBNAME(LRCATSEYE), DIOFBNAME(HRCCATSEYE), DIOFBNAME(HRMCATSEYE),
  79. DIOFBNAME(DAVINCI), DIOFBNAME(XXXCATSEYE), DIOFBNAME(HYPERION),
  80. DIOFBNAME(XGENESIS), DIOFBNAME(TIGER), DIOFBNAME(YGENESIS)
  81. };
  82. #undef DIONAME
  83. #undef DIOFBNAME
  84. #define NUMNAMES (sizeof(names) / sizeof(struct dioname))
  85. static const char *unknowndioname
  86. = "unknown DIO board -- please email <linux-m68k@lists.linux-m68k.org>!";
  87. static const char *dio_getname(int id)
  88. {
  89. /* return pointer to a constant string describing the board with given ID */
  90. unsigned int i;
  91. for (i = 0; i < NUMNAMES; i++)
  92. if (names[i].id == id)
  93. return names[i].name;
  94. return unknowndioname;
  95. }
  96. #else
  97. static char dio_no_name[] = { 0 };
  98. #define dio_getname(_id) (dio_no_name)
  99. #endif /* CONFIG_DIO_CONSTANTS */
  100. int __init dio_find(int deviceid)
  101. {
  102. /* Called to find a DIO device before the full bus scan has run.
  103. * Only used by the console driver.
  104. */
  105. int scode, id;
  106. u_char prid, secid, i;
  107. mm_segment_t fs;
  108. for (scode = 0; scode < DIO_SCMAX; scode++) {
  109. void *va;
  110. unsigned long pa;
  111. if (DIO_SCINHOLE(scode))
  112. continue;
  113. pa = dio_scodetophysaddr(scode);
  114. if (!pa)
  115. continue;
  116. if (scode < DIOII_SCBASE)
  117. va = (void *)(pa + DIO_VIRADDRBASE);
  118. else
  119. va = ioremap(pa, PAGE_SIZE);
  120. fs = get_fs();
  121. set_fs(KERNEL_DS);
  122. if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
  123. set_fs(fs);
  124. if (scode >= DIOII_SCBASE)
  125. iounmap(va);
  126. continue; /* no board present at that select code */
  127. }
  128. set_fs(fs);
  129. prid = DIO_ID(va);
  130. if (DIO_NEEDSSECID(prid)) {
  131. secid = DIO_SECID(va);
  132. id = DIO_ENCODE_ID(prid, secid);
  133. } else
  134. id = prid;
  135. if (id == deviceid) {
  136. if (scode >= DIOII_SCBASE)
  137. iounmap(va);
  138. return scode;
  139. }
  140. }
  141. return -1;
  142. }
  143. /* This is the function that scans the DIO space and works out what
  144. * hardware is actually present.
  145. */
  146. static int __init dio_init(void)
  147. {
  148. int scode;
  149. mm_segment_t fs;
  150. int i;
  151. struct dio_dev *dev;
  152. if (!MACH_IS_HP300)
  153. return 0;
  154. printk(KERN_INFO "Scanning for DIO devices...\n");
  155. /* Initialize the DIO bus */
  156. INIT_LIST_HEAD(&dio_bus.devices);
  157. strcpy(dio_bus.dev.bus_id, "dio");
  158. device_register(&dio_bus.dev);
  159. /* Request all resources */
  160. dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2);
  161. for (i = 0; i < dio_bus.num_resources; i++)
  162. request_resource(&iomem_resource, &dio_bus.resources[i]);
  163. /* Register all devices */
  164. for (scode = 0; scode < DIO_SCMAX; ++scode)
  165. {
  166. u_char prid, secid = 0; /* primary, secondary ID bytes */
  167. u_char *va;
  168. unsigned long pa;
  169. if (DIO_SCINHOLE(scode))
  170. continue;
  171. pa = dio_scodetophysaddr(scode);
  172. if (!pa)
  173. continue;
  174. if (scode < DIOII_SCBASE)
  175. va = (void *)(pa + DIO_VIRADDRBASE);
  176. else
  177. va = ioremap(pa, PAGE_SIZE);
  178. fs = get_fs();
  179. set_fs(KERNEL_DS);
  180. if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
  181. set_fs(fs);
  182. if (scode >= DIOII_SCBASE)
  183. iounmap(va);
  184. continue; /* no board present at that select code */
  185. }
  186. set_fs(fs);
  187. /* Found a board, allocate it an entry in the list */
  188. dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL);
  189. if (!dev)
  190. return 0;
  191. dev->bus = &dio_bus;
  192. dev->dev.parent = &dio_bus.dev;
  193. dev->dev.bus = &dio_bus_type;
  194. dev->scode = scode;
  195. dev->resource.start = pa;
  196. dev->resource.end = pa + DIO_SIZE(scode, va);
  197. sprintf(dev->dev.bus_id,"%02x", scode);
  198. /* read the ID byte(s) and encode if necessary. */
  199. prid = DIO_ID(va);
  200. if (DIO_NEEDSSECID(prid)) {
  201. secid = DIO_SECID(va);
  202. dev->id = DIO_ENCODE_ID(prid, secid);
  203. } else
  204. dev->id = prid;
  205. dev->ipl = DIO_IPL(va);
  206. strcpy(dev->name,dio_getname(dev->id));
  207. printk(KERN_INFO "select code %3d: ipl %d: ID %02X", dev->scode, dev->ipl, prid);
  208. if (DIO_NEEDSSECID(prid))
  209. printk(":%02X", secid);
  210. printk(": %s\n", dev->name);
  211. if (scode >= DIOII_SCBASE)
  212. iounmap(va);
  213. device_register(&dev->dev);
  214. dio_create_sysfs_dev_files(dev);
  215. }
  216. return 0;
  217. }
  218. subsys_initcall(dio_init);
  219. /* Bear in mind that this is called in the very early stages of initialisation
  220. * in order to get the address of the serial port for the console...
  221. */
  222. unsigned long dio_scodetophysaddr(int scode)
  223. {
  224. if (scode >= DIOII_SCBASE) {
  225. return (DIOII_BASE + (scode - 132) * DIOII_DEVSIZE);
  226. } else if (scode > DIO_SCMAX || scode < 0)
  227. return 0;
  228. else if (DIO_SCINHOLE(scode))
  229. return 0;
  230. return (DIO_BASE + scode * DIO_DEVSIZE);
  231. }