dio.c 8.3 KB

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