dio.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. static const char *unknowndioname
  85. = "unknown DIO board -- please email <linux-m68k@lists.linux-m68k.org>!";
  86. static 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. return unknowndioname;
  94. }
  95. #else
  96. static char dio_no_name[] = { 0 };
  97. #define dio_getname(_id) (dio_no_name)
  98. #endif /* CONFIG_DIO_CONSTANTS */
  99. int __init dio_find(int deviceid)
  100. {
  101. /* Called to find a DIO device before the full bus scan has run.
  102. * Only used by the console driver.
  103. */
  104. int scode, id;
  105. u_char prid, secid, i;
  106. mm_segment_t fs;
  107. for (scode = 0; scode < DIO_SCMAX; scode++) {
  108. void *va;
  109. unsigned long pa;
  110. if (DIO_SCINHOLE(scode))
  111. continue;
  112. pa = dio_scodetophysaddr(scode);
  113. if (!pa)
  114. continue;
  115. if (scode < DIOII_SCBASE)
  116. va = (void *)(pa + DIO_VIRADDRBASE);
  117. else
  118. va = ioremap(pa, PAGE_SIZE);
  119. fs = get_fs();
  120. set_fs(KERNEL_DS);
  121. if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
  122. set_fs(fs);
  123. if (scode >= DIOII_SCBASE)
  124. iounmap(va);
  125. continue; /* no board present at that select code */
  126. }
  127. set_fs(fs);
  128. prid = DIO_ID(va);
  129. if (DIO_NEEDSSECID(prid)) {
  130. secid = DIO_SECID(va);
  131. id = DIO_ENCODE_ID(prid, secid);
  132. } else
  133. id = prid;
  134. if (id == deviceid) {
  135. if (scode >= DIOII_SCBASE)
  136. iounmap(va);
  137. return scode;
  138. }
  139. }
  140. return -1;
  141. }
  142. /* This is the function that scans the DIO space and works out what
  143. * hardware is actually present.
  144. */
  145. static int __init dio_init(void)
  146. {
  147. int scode;
  148. mm_segment_t fs;
  149. int i;
  150. struct dio_dev *dev;
  151. int error;
  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. dev_set_name(&dio_bus.dev, "dio");
  158. error = device_register(&dio_bus.dev);
  159. if (error) {
  160. pr_err("DIO: Error registering dio_bus\n");
  161. return error;
  162. }
  163. /* Request all resources */
  164. dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2);
  165. for (i = 0; i < dio_bus.num_resources; i++)
  166. request_resource(&iomem_resource, &dio_bus.resources[i]);
  167. /* Register all devices */
  168. for (scode = 0; scode < DIO_SCMAX; ++scode)
  169. {
  170. u_char prid, secid = 0; /* primary, secondary ID bytes */
  171. u_char *va;
  172. unsigned long pa;
  173. if (DIO_SCINHOLE(scode))
  174. continue;
  175. pa = dio_scodetophysaddr(scode);
  176. if (!pa)
  177. continue;
  178. if (scode < DIOII_SCBASE)
  179. va = (void *)(pa + DIO_VIRADDRBASE);
  180. else
  181. va = ioremap(pa, PAGE_SIZE);
  182. fs = get_fs();
  183. set_fs(KERNEL_DS);
  184. if (get_user(i, (unsigned char *)va + DIO_IDOFF)) {
  185. set_fs(fs);
  186. if (scode >= DIOII_SCBASE)
  187. iounmap(va);
  188. continue; /* no board present at that select code */
  189. }
  190. set_fs(fs);
  191. /* Found a board, allocate it an entry in the list */
  192. dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL);
  193. if (!dev)
  194. return 0;
  195. dev->bus = &dio_bus;
  196. dev->dev.parent = &dio_bus.dev;
  197. dev->dev.bus = &dio_bus_type;
  198. dev->scode = scode;
  199. dev->resource.start = pa;
  200. dev->resource.end = pa + DIO_SIZE(scode, va);
  201. dev_set_name(&dev->dev, "%02x", scode);
  202. /* read the ID byte(s) and encode if necessary. */
  203. prid = DIO_ID(va);
  204. if (DIO_NEEDSSECID(prid)) {
  205. secid = DIO_SECID(va);
  206. dev->id = DIO_ENCODE_ID(prid, secid);
  207. } else
  208. dev->id = prid;
  209. dev->ipl = DIO_IPL(va);
  210. strcpy(dev->name,dio_getname(dev->id));
  211. printk(KERN_INFO "select code %3d: ipl %d: ID %02X", dev->scode, dev->ipl, prid);
  212. if (DIO_NEEDSSECID(prid))
  213. printk(":%02X", secid);
  214. printk(": %s\n", dev->name);
  215. if (scode >= DIOII_SCBASE)
  216. iounmap(va);
  217. error = device_register(&dev->dev);
  218. if (error) {
  219. pr_err("DIO: Error registering device %s\n",
  220. dev->name);
  221. continue;
  222. }
  223. error = dio_create_sysfs_dev_files(dev);
  224. if (error)
  225. dev_err(&dev->dev, "Error creating sysfs files\n");
  226. }
  227. return 0;
  228. }
  229. subsys_initcall(dio_init);
  230. /* Bear in mind that this is called in the very early stages of initialisation
  231. * in order to get the address of the serial port for the console...
  232. */
  233. unsigned long dio_scodetophysaddr(int scode)
  234. {
  235. if (scode >= DIOII_SCBASE) {
  236. return (DIOII_BASE + (scode - 132) * DIOII_DEVSIZE);
  237. } else if (scode > DIO_SCMAX || scode < 0)
  238. return 0;
  239. else if (DIO_SCINHOLE(scode))
  240. return 0;
  241. return (DIO_BASE + scode * DIO_DEVSIZE);
  242. }