eisa-bus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * EISA bus support functions for sysfs.
  4. *
  5. * (C) 2002, 2003 Marc Zyngier <[email protected]>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/device.h>
  9. #include <linux/eisa.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/ioport.h>
  15. #include <asm/io.h>
  16. #define SLOT_ADDRESS(r,n) (r->bus_base_addr + (0x1000 * n))
  17. #define EISA_DEVINFO(i,s) { .id = { .sig = i }, .name = s }
  18. struct eisa_device_info {
  19. struct eisa_device_id id;
  20. char name[50];
  21. };
  22. #ifdef CONFIG_EISA_NAMES
  23. static struct eisa_device_info __initdata eisa_table[] = {
  24. #include "devlist.h"
  25. };
  26. #define EISA_INFOS (sizeof (eisa_table) / (sizeof (struct eisa_device_info)))
  27. #endif
  28. #define EISA_MAX_FORCED_DEV 16
  29. static int enable_dev[EISA_MAX_FORCED_DEV];
  30. static unsigned int enable_dev_count;
  31. static int disable_dev[EISA_MAX_FORCED_DEV];
  32. static unsigned int disable_dev_count;
  33. static int is_forced_dev(int *forced_tab,
  34. int forced_count,
  35. struct eisa_root_device *root,
  36. struct eisa_device *edev)
  37. {
  38. int i, x;
  39. for (i = 0; i < forced_count; i++) {
  40. x = (root->bus_nr << 8) | edev->slot;
  41. if (forced_tab[i] == x)
  42. return 1;
  43. }
  44. return 0;
  45. }
  46. static void __init eisa_name_device(struct eisa_device *edev)
  47. {
  48. #ifdef CONFIG_EISA_NAMES
  49. int i;
  50. for (i = 0; i < EISA_INFOS; i++) {
  51. if (!strcmp(edev->id.sig, eisa_table[i].id.sig)) {
  52. strlcpy(edev->pretty_name,
  53. eisa_table[i].name,
  54. sizeof(edev->pretty_name));
  55. return;
  56. }
  57. }
  58. /* No name was found */
  59. sprintf(edev->pretty_name, "EISA device %.7s", edev->id.sig);
  60. #endif
  61. }
  62. static char __init *decode_eisa_sig(unsigned long addr)
  63. {
  64. static char sig_str[EISA_SIG_LEN];
  65. u8 sig[4];
  66. u16 rev;
  67. int i;
  68. for (i = 0; i < 4; i++) {
  69. #ifdef CONFIG_EISA_VLB_PRIMING
  70. /*
  71. * This ugly stuff is used to wake up VL-bus cards
  72. * (AHA-284x is the only known example), so we can
  73. * read the EISA id.
  74. *
  75. * Thankfully, this only exists on x86...
  76. */
  77. outb(0x80 + i, addr);
  78. #endif
  79. sig[i] = inb(addr + i);
  80. if (!i && (sig[0] & 0x80))
  81. return NULL;
  82. }
  83. sig_str[0] = ((sig[0] >> 2) & 0x1f) + ('A' - 1);
  84. sig_str[1] = (((sig[0] & 3) << 3) | (sig[1] >> 5)) + ('A' - 1);
  85. sig_str[2] = (sig[1] & 0x1f) + ('A' - 1);
  86. rev = (sig[2] << 8) | sig[3];
  87. sprintf(sig_str + 3, "%04X", rev);
  88. return sig_str;
  89. }
  90. static int eisa_bus_match(struct device *dev, struct device_driver *drv)
  91. {
  92. struct eisa_device *edev = to_eisa_device(dev);
  93. struct eisa_driver *edrv = to_eisa_driver(drv);
  94. const struct eisa_device_id *eids = edrv->id_table;
  95. if (!eids)
  96. return 0;
  97. while (strlen(eids->sig)) {
  98. if (!strcmp(eids->sig, edev->id.sig) &&
  99. edev->state & EISA_CONFIG_ENABLED) {
  100. edev->id.driver_data = eids->driver_data;
  101. return 1;
  102. }
  103. eids++;
  104. }
  105. return 0;
  106. }
  107. static int eisa_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
  108. {
  109. struct eisa_device *edev = to_eisa_device(dev);
  110. add_uevent_var(env, "MODALIAS=" EISA_DEVICE_MODALIAS_FMT, edev->id.sig);
  111. return 0;
  112. }
  113. struct bus_type eisa_bus_type = {
  114. .name = "eisa",
  115. .match = eisa_bus_match,
  116. .uevent = eisa_bus_uevent,
  117. };
  118. EXPORT_SYMBOL(eisa_bus_type);
  119. int eisa_driver_register(struct eisa_driver *edrv)
  120. {
  121. edrv->driver.bus = &eisa_bus_type;
  122. return driver_register(&edrv->driver);
  123. }
  124. EXPORT_SYMBOL(eisa_driver_register);
  125. void eisa_driver_unregister(struct eisa_driver *edrv)
  126. {
  127. driver_unregister(&edrv->driver);
  128. }
  129. EXPORT_SYMBOL(eisa_driver_unregister);
  130. static ssize_t signature_show(struct device *dev,
  131. struct device_attribute *attr, char *buf)
  132. {
  133. struct eisa_device *edev = to_eisa_device(dev);
  134. return sprintf(buf, "%s\n", edev->id.sig);
  135. }
  136. static DEVICE_ATTR_RO(signature);
  137. static ssize_t enabled_show(struct device *dev,
  138. struct device_attribute *attr, char *buf)
  139. {
  140. struct eisa_device *edev = to_eisa_device(dev);
  141. return sprintf(buf, "%d\n", edev->state & EISA_CONFIG_ENABLED);
  142. }
  143. static DEVICE_ATTR_RO(enabled);
  144. static ssize_t modalias_show(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. struct eisa_device *edev = to_eisa_device(dev);
  148. return sprintf(buf, EISA_DEVICE_MODALIAS_FMT "\n", edev->id.sig);
  149. }
  150. static DEVICE_ATTR_RO(modalias);
  151. static int __init eisa_init_device(struct eisa_root_device *root,
  152. struct eisa_device *edev,
  153. int slot)
  154. {
  155. char *sig;
  156. unsigned long sig_addr;
  157. int i;
  158. sig_addr = SLOT_ADDRESS(root, slot) + EISA_VENDOR_ID_OFFSET;
  159. sig = decode_eisa_sig(sig_addr);
  160. if (!sig)
  161. return -1; /* No EISA device here */
  162. memcpy(edev->id.sig, sig, EISA_SIG_LEN);
  163. edev->slot = slot;
  164. edev->state = inb(SLOT_ADDRESS(root, slot) + EISA_CONFIG_OFFSET)
  165. & EISA_CONFIG_ENABLED;
  166. edev->base_addr = SLOT_ADDRESS(root, slot);
  167. edev->dma_mask = root->dma_mask; /* Default DMA mask */
  168. eisa_name_device(edev);
  169. edev->dev.parent = root->dev;
  170. edev->dev.bus = &eisa_bus_type;
  171. edev->dev.dma_mask = &edev->dma_mask;
  172. edev->dev.coherent_dma_mask = edev->dma_mask;
  173. dev_set_name(&edev->dev, "%02X:%02X", root->bus_nr, slot);
  174. for (i = 0; i < EISA_MAX_RESOURCES; i++) {
  175. #ifdef CONFIG_EISA_NAMES
  176. edev->res[i].name = edev->pretty_name;
  177. #else
  178. edev->res[i].name = edev->id.sig;
  179. #endif
  180. }
  181. if (is_forced_dev(enable_dev, enable_dev_count, root, edev))
  182. edev->state = EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED;
  183. if (is_forced_dev(disable_dev, disable_dev_count, root, edev))
  184. edev->state = EISA_CONFIG_FORCED;
  185. return 0;
  186. }
  187. static int __init eisa_register_device(struct eisa_device *edev)
  188. {
  189. int rc = device_register(&edev->dev);
  190. if (rc) {
  191. put_device(&edev->dev);
  192. return rc;
  193. }
  194. rc = device_create_file(&edev->dev, &dev_attr_signature);
  195. if (rc)
  196. goto err_devreg;
  197. rc = device_create_file(&edev->dev, &dev_attr_enabled);
  198. if (rc)
  199. goto err_sig;
  200. rc = device_create_file(&edev->dev, &dev_attr_modalias);
  201. if (rc)
  202. goto err_enab;
  203. return 0;
  204. err_enab:
  205. device_remove_file(&edev->dev, &dev_attr_enabled);
  206. err_sig:
  207. device_remove_file(&edev->dev, &dev_attr_signature);
  208. err_devreg:
  209. device_unregister(&edev->dev);
  210. return rc;
  211. }
  212. static int __init eisa_request_resources(struct eisa_root_device *root,
  213. struct eisa_device *edev,
  214. int slot)
  215. {
  216. int i;
  217. for (i = 0; i < EISA_MAX_RESOURCES; i++) {
  218. /* Don't register resource for slot 0, since this is
  219. * very likely to fail... :-( Instead, grab the EISA
  220. * id, now we can display something in /proc/ioports.
  221. */
  222. /* Only one region for mainboard */
  223. if (!slot && i > 0) {
  224. edev->res[i].start = edev->res[i].end = 0;
  225. continue;
  226. }
  227. if (slot) {
  228. edev->res[i].name = NULL;
  229. edev->res[i].start = SLOT_ADDRESS(root, slot)
  230. + (i * 0x400);
  231. edev->res[i].end = edev->res[i].start + 0xff;
  232. edev->res[i].flags = IORESOURCE_IO;
  233. } else {
  234. edev->res[i].name = NULL;
  235. edev->res[i].start = SLOT_ADDRESS(root, slot)
  236. + EISA_VENDOR_ID_OFFSET;
  237. edev->res[i].end = edev->res[i].start + 3;
  238. edev->res[i].flags = IORESOURCE_IO | IORESOURCE_BUSY;
  239. }
  240. if (request_resource(root->res, &edev->res[i]))
  241. goto failed;
  242. }
  243. return 0;
  244. failed:
  245. while (--i >= 0)
  246. release_resource(&edev->res[i]);
  247. return -1;
  248. }
  249. static void __init eisa_release_resources(struct eisa_device *edev)
  250. {
  251. int i;
  252. for (i = 0; i < EISA_MAX_RESOURCES; i++)
  253. if (edev->res[i].start || edev->res[i].end)
  254. release_resource(&edev->res[i]);
  255. }
  256. static int __init eisa_probe(struct eisa_root_device *root)
  257. {
  258. int i, c;
  259. struct eisa_device *edev;
  260. char *enabled_str;
  261. dev_info(root->dev, "Probing EISA bus %d\n", root->bus_nr);
  262. /* First try to get hold of slot 0. If there is no device
  263. * here, simply fail, unless root->force_probe is set. */
  264. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  265. if (!edev)
  266. return -ENOMEM;
  267. if (eisa_request_resources(root, edev, 0)) {
  268. dev_warn(root->dev,
  269. "EISA: Cannot allocate resource for mainboard\n");
  270. kfree(edev);
  271. if (!root->force_probe)
  272. return -EBUSY;
  273. goto force_probe;
  274. }
  275. if (eisa_init_device(root, edev, 0)) {
  276. eisa_release_resources(edev);
  277. kfree(edev);
  278. if (!root->force_probe)
  279. return -ENODEV;
  280. goto force_probe;
  281. }
  282. dev_info(&edev->dev, "EISA: Mainboard %s detected\n", edev->id.sig);
  283. if (eisa_register_device(edev)) {
  284. dev_err(&edev->dev, "EISA: Failed to register %s\n",
  285. edev->id.sig);
  286. eisa_release_resources(edev);
  287. kfree(edev);
  288. }
  289. force_probe:
  290. for (c = 0, i = 1; i <= root->slots; i++) {
  291. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  292. if (!edev) {
  293. dev_err(root->dev, "EISA: Out of memory for slot %d\n",
  294. i);
  295. continue;
  296. }
  297. if (eisa_request_resources(root, edev, i)) {
  298. dev_warn(root->dev,
  299. "Cannot allocate resource for EISA slot %d\n",
  300. i);
  301. kfree(edev);
  302. continue;
  303. }
  304. if (eisa_init_device(root, edev, i)) {
  305. eisa_release_resources(edev);
  306. kfree(edev);
  307. continue;
  308. }
  309. if (edev->state == (EISA_CONFIG_ENABLED | EISA_CONFIG_FORCED))
  310. enabled_str = " (forced enabled)";
  311. else if (edev->state == EISA_CONFIG_FORCED)
  312. enabled_str = " (forced disabled)";
  313. else if (edev->state == 0)
  314. enabled_str = " (disabled)";
  315. else
  316. enabled_str = "";
  317. dev_info(&edev->dev, "EISA: slot %d: %s detected%s\n", i,
  318. edev->id.sig, enabled_str);
  319. c++;
  320. if (eisa_register_device(edev)) {
  321. dev_err(&edev->dev, "EISA: Failed to register %s\n",
  322. edev->id.sig);
  323. eisa_release_resources(edev);
  324. kfree(edev);
  325. }
  326. }
  327. dev_info(root->dev, "EISA: Detected %d card%s\n", c, c == 1 ? "" : "s");
  328. return 0;
  329. }
  330. static struct resource eisa_root_res = {
  331. .name = "EISA root resource",
  332. .start = 0,
  333. .end = 0xffffffff,
  334. .flags = IORESOURCE_IO,
  335. };
  336. static int eisa_bus_count;
  337. int __init eisa_root_register(struct eisa_root_device *root)
  338. {
  339. int err;
  340. /* Use our own resources to check if this bus base address has
  341. * been already registered. This prevents the virtual root
  342. * device from registering after the real one has, for
  343. * example... */
  344. root->eisa_root_res.name = eisa_root_res.name;
  345. root->eisa_root_res.start = root->res->start;
  346. root->eisa_root_res.end = root->res->end;
  347. root->eisa_root_res.flags = IORESOURCE_BUSY;
  348. err = request_resource(&eisa_root_res, &root->eisa_root_res);
  349. if (err)
  350. return err;
  351. root->bus_nr = eisa_bus_count++;
  352. err = eisa_probe(root);
  353. if (err)
  354. release_resource(&root->eisa_root_res);
  355. return err;
  356. }
  357. static int __init eisa_init(void)
  358. {
  359. int r;
  360. r = bus_register(&eisa_bus_type);
  361. if (r)
  362. return r;
  363. printk(KERN_INFO "EISA bus registered\n");
  364. return 0;
  365. }
  366. module_param_array(enable_dev, int, &enable_dev_count, 0444);
  367. module_param_array(disable_dev, int, &disable_dev_count, 0444);
  368. postcore_initcall(eisa_init);
  369. int EISA_bus; /* for legacy drivers */
  370. EXPORT_SYMBOL(EISA_bus);