dio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <[email protected]>
  4. * Copyright (C) 2004 Jochen Friedrich <[email protected]>
  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 <linux/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. 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. DIONAME(DCA0), DIONAME(DCA0REM), DIONAME(DCA1), DIONAME(DCA1REM),
  66. DIONAME(DCM), DIONAME(DCMREM),
  67. DIONAME(LAN),
  68. DIONAME(FHPIB), DIONAME(NHPIB),
  69. DIONAME(SCSI0), DIONAME(SCSI1), DIONAME(SCSI2), DIONAME(SCSI3),
  70. DIONAME(FBUFFER),
  71. DIONAME(PARALLEL), DIONAME(VME), DIONAME(DCL), DIONAME(DCLREM),
  72. DIONAME(MISC0), DIONAME(MISC1), DIONAME(MISC2), DIONAME(MISC3),
  73. DIONAME(MISC4), DIONAME(MISC5), DIONAME(MISC6), DIONAME(MISC7),
  74. DIONAME(MISC8), DIONAME(MISC9), DIONAME(MISC10), DIONAME(MISC11),
  75. DIONAME(MISC12), DIONAME(MISC13),
  76. DIOFBNAME(GATORBOX), DIOFBNAME(TOPCAT), DIOFBNAME(RENAISSANCE),
  77. DIOFBNAME(LRCATSEYE), DIOFBNAME(HRCCATSEYE), DIOFBNAME(HRMCATSEYE),
  78. DIOFBNAME(DAVINCI), DIOFBNAME(XXXCATSEYE), DIOFBNAME(HYPERION),
  79. DIOFBNAME(XGENESIS), DIOFBNAME(TIGER), DIOFBNAME(YGENESIS)
  80. };
  81. #undef DIONAME
  82. #undef DIOFBNAME
  83. static const char unknowndioname[]
  84. = "unknown DIO board, please email [email protected]";
  85. static const char *dio_getname(int id)
  86. {
  87. /* return pointer to a constant string describing the board with given ID */
  88. unsigned int i;
  89. for (i = 0; i < ARRAY_SIZE(names); i++)
  90. if (names[i].id == id)
  91. return names[i].name;
  92. return unknowndioname;
  93. }
  94. #else
  95. static char dio_no_name[] = { 0 };
  96. #define dio_getname(_id) (dio_no_name)
  97. #endif /* CONFIG_DIO_CONSTANTS */
  98. static void dio_dev_release(struct device *dev)
  99. {
  100. struct dio_dev *ddev = container_of(dev, typeof(struct dio_dev), dev);
  101. kfree(ddev);
  102. }
  103. int __init dio_find(int deviceid)
  104. {
  105. /* Called to find a DIO device before the full bus scan has run.
  106. * Only used by the console driver.
  107. */
  108. int scode, id;
  109. u_char prid, secid, i;
  110. for (scode = 0; scode < DIO_SCMAX; scode++) {
  111. void *va;
  112. unsigned long pa;
  113. if (DIO_SCINHOLE(scode))
  114. continue;
  115. pa = dio_scodetophysaddr(scode);
  116. if (!pa)
  117. continue;
  118. if (scode < DIOII_SCBASE)
  119. va = (void *)(pa + DIO_VIRADDRBASE);
  120. else
  121. va = ioremap(pa, PAGE_SIZE);
  122. if (copy_from_kernel_nofault(&i,
  123. (unsigned char *)va + DIO_IDOFF, 1)) {
  124. if (scode >= DIOII_SCBASE)
  125. iounmap(va);
  126. continue; /* no board present at that select code */
  127. }
  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. int i;
  149. struct dio_dev *dev;
  150. int error;
  151. if (!MACH_IS_HP300)
  152. return 0;
  153. printk(KERN_INFO "Scanning for DIO devices...\n");
  154. /* Initialize the DIO bus */
  155. INIT_LIST_HEAD(&dio_bus.devices);
  156. dev_set_name(&dio_bus.dev, "dio");
  157. error = device_register(&dio_bus.dev);
  158. if (error) {
  159. pr_err("DIO: Error registering dio_bus\n");
  160. return error;
  161. }
  162. /* Request all resources */
  163. dio_bus.num_resources = (hp300_model == HP_320 ? 1 : 2);
  164. for (i = 0; i < dio_bus.num_resources; i++)
  165. request_resource(&iomem_resource, &dio_bus.resources[i]);
  166. /* Register all devices */
  167. for (scode = 0; scode < DIO_SCMAX; ++scode) {
  168. u_char prid, secid = 0; /* primary, secondary ID bytes */
  169. u_char *va;
  170. unsigned long pa;
  171. if (DIO_SCINHOLE(scode))
  172. continue;
  173. pa = dio_scodetophysaddr(scode);
  174. if (!pa)
  175. continue;
  176. if (scode < DIOII_SCBASE)
  177. va = (void *)(pa + DIO_VIRADDRBASE);
  178. else
  179. va = ioremap(pa, PAGE_SIZE);
  180. if (copy_from_kernel_nofault(&i,
  181. (unsigned char *)va + DIO_IDOFF, 1)) {
  182. if (scode >= DIOII_SCBASE)
  183. iounmap(va);
  184. continue; /* no board present at that select code */
  185. }
  186. /* Found a board, allocate it an entry in the list */
  187. dev = kzalloc(sizeof(struct dio_dev), GFP_KERNEL);
  188. if (!dev) {
  189. if (scode >= DIOII_SCBASE)
  190. iounmap(va);
  191. return -ENOMEM;
  192. }
  193. dev->bus = &dio_bus;
  194. dev->dev.parent = &dio_bus.dev;
  195. dev->dev.bus = &dio_bus_type;
  196. dev->dev.release = dio_dev_release;
  197. dev->scode = scode;
  198. dev->resource.start = pa;
  199. dev->resource.end = pa + DIO_SIZE(scode, va);
  200. dev_set_name(&dev->dev, "%02x", scode);
  201. /* read the ID byte(s) and encode if necessary. */
  202. prid = DIO_ID(va);
  203. if (DIO_NEEDSSECID(prid)) {
  204. secid = DIO_SECID(va);
  205. dev->id = DIO_ENCODE_ID(prid, secid);
  206. } else
  207. dev->id = prid;
  208. dev->ipl = DIO_IPL(va);
  209. strcpy(dev->name, dio_getname(dev->id));
  210. printk(KERN_INFO "select code %3d: ipl %d: ID %02X", dev->scode, dev->ipl, prid);
  211. if (DIO_NEEDSSECID(prid))
  212. printk(":%02X", secid);
  213. printk(": %s\n", dev->name);
  214. if (scode >= DIOII_SCBASE)
  215. iounmap(va);
  216. error = device_register(&dev->dev);
  217. if (error) {
  218. pr_err("DIO: Error registering device %s\n",
  219. dev->name);
  220. put_device(&dev->dev);
  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. }