drivers.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. * drivers.c
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Copyright (c) 1999 The Puffin Group
  10. * Copyright (c) 2001 Matthew Wilcox for Hewlett Packard
  11. * Copyright (c) 2001 Helge Deller <deller@gmx.de>
  12. * Copyright (c) 2001,2002 Ryan Bradetich
  13. * Copyright (c) 2004-2005 Thibaut VARENE <varenet@parisc-linux.org>
  14. *
  15. * The file handles registering devices and drivers, then matching them.
  16. * It's the closest we get to a dating agency.
  17. *
  18. * If you're thinking about modifying this file, here are some gotchas to
  19. * bear in mind:
  20. * - 715/Mirage device paths have a dummy device between Lasi and its children
  21. * - The EISA adapter may show up as a sibling or child of Wax
  22. * - Dino has an optionally functional serial port. If firmware enables it,
  23. * it shows up as a child of Dino. If firmware disables it, the buswalk
  24. * finds it and it shows up as a child of Cujo
  25. * - Dino has both parisc and pci devices as children
  26. * - parisc devices are discovered in a random order, including children
  27. * before parents in some cases.
  28. */
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/pci.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/string.h>
  35. #include <linux/export.h>
  36. #include <asm/hardware.h>
  37. #include <asm/io.h>
  38. #include <asm/pdc.h>
  39. #include <asm/parisc-device.h>
  40. /* See comments in include/asm-parisc/pci.h */
  41. const struct dma_map_ops *hppa_dma_ops __read_mostly;
  42. EXPORT_SYMBOL(hppa_dma_ops);
  43. static struct device root = {
  44. .init_name = "parisc",
  45. };
  46. static inline int check_dev(struct device *dev)
  47. {
  48. if (dev->bus == &parisc_bus_type) {
  49. struct parisc_device *pdev;
  50. pdev = to_parisc_device(dev);
  51. return pdev->id.hw_type != HPHW_FAULTY;
  52. }
  53. return 1;
  54. }
  55. static struct device *
  56. parse_tree_node(struct device *parent, int index, struct hardware_path *modpath);
  57. struct recurse_struct {
  58. void * obj;
  59. int (*fn)(struct device *, void *);
  60. };
  61. static int descend_children(struct device * dev, void * data)
  62. {
  63. struct recurse_struct * recurse_data = (struct recurse_struct *)data;
  64. if (recurse_data->fn(dev, recurse_data->obj))
  65. return 1;
  66. else
  67. return device_for_each_child(dev, recurse_data, descend_children);
  68. }
  69. /**
  70. * for_each_padev - Iterate over all devices in the tree
  71. * @fn: Function to call for each device.
  72. * @data: Data to pass to the called function.
  73. *
  74. * This performs a depth-first traversal of the tree, calling the
  75. * function passed for each node. It calls the function for parents
  76. * before children.
  77. */
  78. static int for_each_padev(int (*fn)(struct device *, void *), void * data)
  79. {
  80. struct recurse_struct recurse_data = {
  81. .obj = data,
  82. .fn = fn,
  83. };
  84. return device_for_each_child(&root, &recurse_data, descend_children);
  85. }
  86. /**
  87. * match_device - Report whether this driver can handle this device
  88. * @driver: the PA-RISC driver to try
  89. * @dev: the PA-RISC device to try
  90. */
  91. static int match_device(struct parisc_driver *driver, struct parisc_device *dev)
  92. {
  93. const struct parisc_device_id *ids;
  94. for (ids = driver->id_table; ids->sversion; ids++) {
  95. if ((ids->sversion != SVERSION_ANY_ID) &&
  96. (ids->sversion != dev->id.sversion))
  97. continue;
  98. if ((ids->hw_type != HWTYPE_ANY_ID) &&
  99. (ids->hw_type != dev->id.hw_type))
  100. continue;
  101. if ((ids->hversion != HVERSION_ANY_ID) &&
  102. (ids->hversion != dev->id.hversion))
  103. continue;
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. static int parisc_driver_probe(struct device *dev)
  109. {
  110. int rc;
  111. struct parisc_device *pa_dev = to_parisc_device(dev);
  112. struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
  113. rc = pa_drv->probe(pa_dev);
  114. if (!rc)
  115. pa_dev->driver = pa_drv;
  116. return rc;
  117. }
  118. static int __exit parisc_driver_remove(struct device *dev)
  119. {
  120. struct parisc_device *pa_dev = to_parisc_device(dev);
  121. struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
  122. if (pa_drv->remove)
  123. pa_drv->remove(pa_dev);
  124. return 0;
  125. }
  126. /**
  127. * register_parisc_driver - Register this driver if it can handle a device
  128. * @driver: the PA-RISC driver to try
  129. */
  130. int register_parisc_driver(struct parisc_driver *driver)
  131. {
  132. /* FIXME: we need this because apparently the sti
  133. * driver can be registered twice */
  134. if(driver->drv.name) {
  135. printk(KERN_WARNING
  136. "BUG: skipping previously registered driver %s\n",
  137. driver->name);
  138. return 1;
  139. }
  140. if (!driver->probe) {
  141. printk(KERN_WARNING
  142. "BUG: driver %s has no probe routine\n",
  143. driver->name);
  144. return 1;
  145. }
  146. driver->drv.bus = &parisc_bus_type;
  147. /* We install our own probe and remove routines */
  148. WARN_ON(driver->drv.probe != NULL);
  149. WARN_ON(driver->drv.remove != NULL);
  150. driver->drv.name = driver->name;
  151. return driver_register(&driver->drv);
  152. }
  153. EXPORT_SYMBOL(register_parisc_driver);
  154. struct match_count {
  155. struct parisc_driver * driver;
  156. int count;
  157. };
  158. static int match_and_count(struct device * dev, void * data)
  159. {
  160. struct match_count * m = data;
  161. struct parisc_device * pdev = to_parisc_device(dev);
  162. if (check_dev(dev)) {
  163. if (match_device(m->driver, pdev))
  164. m->count++;
  165. }
  166. return 0;
  167. }
  168. /**
  169. * count_parisc_driver - count # of devices this driver would match
  170. * @driver: the PA-RISC driver to try
  171. *
  172. * Use by IOMMU support to "guess" the right size IOPdir.
  173. * Formula is something like memsize/(num_iommu * entry_size).
  174. */
  175. int __init count_parisc_driver(struct parisc_driver *driver)
  176. {
  177. struct match_count m = {
  178. .driver = driver,
  179. .count = 0,
  180. };
  181. for_each_padev(match_and_count, &m);
  182. return m.count;
  183. }
  184. /**
  185. * unregister_parisc_driver - Unregister this driver from the list of drivers
  186. * @driver: the PA-RISC driver to unregister
  187. */
  188. int unregister_parisc_driver(struct parisc_driver *driver)
  189. {
  190. driver_unregister(&driver->drv);
  191. return 0;
  192. }
  193. EXPORT_SYMBOL(unregister_parisc_driver);
  194. struct find_data {
  195. unsigned long hpa;
  196. struct parisc_device * dev;
  197. };
  198. static int find_device(struct device * dev, void * data)
  199. {
  200. struct parisc_device * pdev = to_parisc_device(dev);
  201. struct find_data * d = (struct find_data*)data;
  202. if (check_dev(dev)) {
  203. if (pdev->hpa.start == d->hpa) {
  204. d->dev = pdev;
  205. return 1;
  206. }
  207. }
  208. return 0;
  209. }
  210. static struct parisc_device *find_device_by_addr(unsigned long hpa)
  211. {
  212. struct find_data d = {
  213. .hpa = hpa,
  214. };
  215. int ret;
  216. ret = for_each_padev(find_device, &d);
  217. return ret ? d.dev : NULL;
  218. }
  219. /**
  220. * find_pa_parent_type - Find a parent of a specific type
  221. * @dev: The device to start searching from
  222. * @type: The device type to search for.
  223. *
  224. * Walks up the device tree looking for a device of the specified type.
  225. * If it finds it, it returns it. If not, it returns NULL.
  226. */
  227. const struct parisc_device *
  228. find_pa_parent_type(const struct parisc_device *padev, int type)
  229. {
  230. const struct device *dev = &padev->dev;
  231. while (dev != &root) {
  232. struct parisc_device *candidate = to_parisc_device(dev);
  233. if (candidate->id.hw_type == type)
  234. return candidate;
  235. dev = dev->parent;
  236. }
  237. return NULL;
  238. }
  239. /*
  240. * get_node_path fills in @path with the firmware path to the device.
  241. * Note that if @node is a parisc device, we don't fill in the 'mod' field.
  242. * This is because both callers pass the parent and fill in the mod
  243. * themselves. If @node is a PCI device, we do fill it in, even though this
  244. * is inconsistent.
  245. */
  246. static void get_node_path(struct device *dev, struct hardware_path *path)
  247. {
  248. int i = 5;
  249. memset(&path->bc, -1, 6);
  250. if (dev_is_pci(dev)) {
  251. unsigned int devfn = to_pci_dev(dev)->devfn;
  252. path->mod = PCI_FUNC(devfn);
  253. path->bc[i--] = PCI_SLOT(devfn);
  254. dev = dev->parent;
  255. }
  256. while (dev != &root) {
  257. if (dev_is_pci(dev)) {
  258. unsigned int devfn = to_pci_dev(dev)->devfn;
  259. path->bc[i--] = PCI_SLOT(devfn) | (PCI_FUNC(devfn)<< 5);
  260. } else if (dev->bus == &parisc_bus_type) {
  261. path->bc[i--] = to_parisc_device(dev)->hw_path;
  262. }
  263. dev = dev->parent;
  264. }
  265. }
  266. static char *print_hwpath(struct hardware_path *path, char *output)
  267. {
  268. int i;
  269. for (i = 0; i < 6; i++) {
  270. if (path->bc[i] == -1)
  271. continue;
  272. output += sprintf(output, "%u/", (unsigned char) path->bc[i]);
  273. }
  274. output += sprintf(output, "%u", (unsigned char) path->mod);
  275. return output;
  276. }
  277. /**
  278. * print_pa_hwpath - Returns hardware path for PA devices
  279. * dev: The device to return the path for
  280. * output: Pointer to a previously-allocated array to place the path in.
  281. *
  282. * This function fills in the output array with a human-readable path
  283. * to a PA device. This string is compatible with that used by PDC, and
  284. * may be printed on the outside of the box.
  285. */
  286. char *print_pa_hwpath(struct parisc_device *dev, char *output)
  287. {
  288. struct hardware_path path;
  289. get_node_path(dev->dev.parent, &path);
  290. path.mod = dev->hw_path;
  291. return print_hwpath(&path, output);
  292. }
  293. EXPORT_SYMBOL(print_pa_hwpath);
  294. #if defined(CONFIG_PCI) || defined(CONFIG_ISA)
  295. /**
  296. * get_pci_node_path - Determines the hardware path for a PCI device
  297. * @pdev: The device to return the path for
  298. * @path: Pointer to a previously-allocated array to place the path in.
  299. *
  300. * This function fills in the hardware_path structure with the route to
  301. * the specified PCI device. This structure is suitable for passing to
  302. * PDC calls.
  303. */
  304. void get_pci_node_path(struct pci_dev *pdev, struct hardware_path *path)
  305. {
  306. get_node_path(&pdev->dev, path);
  307. }
  308. EXPORT_SYMBOL(get_pci_node_path);
  309. /**
  310. * print_pci_hwpath - Returns hardware path for PCI devices
  311. * dev: The device to return the path for
  312. * output: Pointer to a previously-allocated array to place the path in.
  313. *
  314. * This function fills in the output array with a human-readable path
  315. * to a PCI device. This string is compatible with that used by PDC, and
  316. * may be printed on the outside of the box.
  317. */
  318. char *print_pci_hwpath(struct pci_dev *dev, char *output)
  319. {
  320. struct hardware_path path;
  321. get_pci_node_path(dev, &path);
  322. return print_hwpath(&path, output);
  323. }
  324. EXPORT_SYMBOL(print_pci_hwpath);
  325. #endif /* defined(CONFIG_PCI) || defined(CONFIG_ISA) */
  326. static void setup_bus_id(struct parisc_device *padev)
  327. {
  328. struct hardware_path path;
  329. char name[28];
  330. char *output = name;
  331. int i;
  332. get_node_path(padev->dev.parent, &path);
  333. for (i = 0; i < 6; i++) {
  334. if (path.bc[i] == -1)
  335. continue;
  336. output += sprintf(output, "%u:", (unsigned char) path.bc[i]);
  337. }
  338. sprintf(output, "%u", (unsigned char) padev->hw_path);
  339. dev_set_name(&padev->dev, name);
  340. }
  341. struct parisc_device * __init create_tree_node(char id, struct device *parent)
  342. {
  343. struct parisc_device *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  344. if (!dev)
  345. return NULL;
  346. dev->hw_path = id;
  347. dev->id.hw_type = HPHW_FAULTY;
  348. dev->dev.parent = parent;
  349. setup_bus_id(dev);
  350. dev->dev.bus = &parisc_bus_type;
  351. dev->dma_mask = 0xffffffffUL; /* PARISC devices are 32-bit */
  352. /* make the generic dma mask a pointer to the parisc one */
  353. dev->dev.dma_mask = &dev->dma_mask;
  354. dev->dev.coherent_dma_mask = dev->dma_mask;
  355. if (device_register(&dev->dev)) {
  356. kfree(dev);
  357. return NULL;
  358. }
  359. return dev;
  360. }
  361. struct match_id_data {
  362. char id;
  363. struct parisc_device * dev;
  364. };
  365. static int match_by_id(struct device * dev, void * data)
  366. {
  367. struct parisc_device * pdev = to_parisc_device(dev);
  368. struct match_id_data * d = data;
  369. if (pdev->hw_path == d->id) {
  370. d->dev = pdev;
  371. return 1;
  372. }
  373. return 0;
  374. }
  375. /**
  376. * alloc_tree_node - returns a device entry in the iotree
  377. * @parent: the parent node in the tree
  378. * @id: the element of the module path for this entry
  379. *
  380. * Checks all the children of @parent for a matching @id. If none
  381. * found, it allocates a new device and returns it.
  382. */
  383. static struct parisc_device * __init alloc_tree_node(
  384. struct device *parent, char id)
  385. {
  386. struct match_id_data d = {
  387. .id = id,
  388. };
  389. if (device_for_each_child(parent, &d, match_by_id))
  390. return d.dev;
  391. else
  392. return create_tree_node(id, parent);
  393. }
  394. static struct parisc_device *create_parisc_device(struct hardware_path *modpath)
  395. {
  396. int i;
  397. struct device *parent = &root;
  398. for (i = 0; i < 6; i++) {
  399. if (modpath->bc[i] == -1)
  400. continue;
  401. parent = &alloc_tree_node(parent, modpath->bc[i])->dev;
  402. }
  403. return alloc_tree_node(parent, modpath->mod);
  404. }
  405. struct parisc_device * __init
  406. alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
  407. {
  408. int status;
  409. unsigned long bytecnt;
  410. u8 iodc_data[32];
  411. struct parisc_device *dev;
  412. const char *name;
  413. /* Check to make sure this device has not already been added - Ryan */
  414. if (find_device_by_addr(hpa) != NULL)
  415. return NULL;
  416. status = pdc_iodc_read(&bytecnt, hpa, 0, &iodc_data, 32);
  417. if (status != PDC_OK)
  418. return NULL;
  419. dev = create_parisc_device(mod_path);
  420. if (dev->id.hw_type != HPHW_FAULTY) {
  421. printk(KERN_ERR "Two devices have hardware path [%s]. "
  422. "IODC data for second device: "
  423. "%02x%02x%02x%02x%02x%02x\n"
  424. "Rearranging GSC cards sometimes helps\n",
  425. parisc_pathname(dev), iodc_data[0], iodc_data[1],
  426. iodc_data[3], iodc_data[4], iodc_data[5], iodc_data[6]);
  427. return NULL;
  428. }
  429. dev->id.hw_type = iodc_data[3] & 0x1f;
  430. dev->id.hversion = (iodc_data[0] << 4) | ((iodc_data[1] & 0xf0) >> 4);
  431. dev->id.hversion_rev = iodc_data[1] & 0x0f;
  432. dev->id.sversion = ((iodc_data[4] & 0x0f) << 16) |
  433. (iodc_data[5] << 8) | iodc_data[6];
  434. dev->hpa.name = parisc_pathname(dev);
  435. dev->hpa.start = hpa;
  436. /* This is awkward. The STI spec says that gfx devices may occupy
  437. * 32MB or 64MB. Unfortunately, we don't know how to tell whether
  438. * it's the former or the latter. Assumptions either way can hurt us.
  439. */
  440. if (hpa == 0xf4000000 || hpa == 0xf8000000) {
  441. dev->hpa.end = hpa + 0x03ffffff;
  442. } else if (hpa == 0xf6000000 || hpa == 0xfa000000) {
  443. dev->hpa.end = hpa + 0x01ffffff;
  444. } else {
  445. dev->hpa.end = hpa + 0xfff;
  446. }
  447. dev->hpa.flags = IORESOURCE_MEM;
  448. name = parisc_hardware_description(&dev->id);
  449. if (name) {
  450. strlcpy(dev->name, name, sizeof(dev->name));
  451. }
  452. /* Silently fail things like mouse ports which are subsumed within
  453. * the keyboard controller
  454. */
  455. if ((hpa & 0xfff) == 0 && insert_resource(&iomem_resource, &dev->hpa))
  456. printk("Unable to claim HPA %lx for device %s\n",
  457. hpa, name);
  458. return dev;
  459. }
  460. static int parisc_generic_match(struct device *dev, struct device_driver *drv)
  461. {
  462. return match_device(to_parisc_driver(drv), to_parisc_device(dev));
  463. }
  464. static ssize_t make_modalias(struct device *dev, char *buf)
  465. {
  466. const struct parisc_device *padev = to_parisc_device(dev);
  467. const struct parisc_device_id *id = &padev->id;
  468. return sprintf(buf, "parisc:t%02Xhv%04Xrev%02Xsv%08X\n",
  469. (u8)id->hw_type, (u16)id->hversion, (u8)id->hversion_rev,
  470. (u32)id->sversion);
  471. }
  472. static int parisc_uevent(struct device *dev, struct kobj_uevent_env *env)
  473. {
  474. const struct parisc_device *padev;
  475. char modalias[40];
  476. if (!dev)
  477. return -ENODEV;
  478. padev = to_parisc_device(dev);
  479. if (!padev)
  480. return -ENODEV;
  481. if (add_uevent_var(env, "PARISC_NAME=%s", padev->name))
  482. return -ENOMEM;
  483. make_modalias(dev, modalias);
  484. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  485. return -ENOMEM;
  486. return 0;
  487. }
  488. #define pa_dev_attr(name, field, format_string) \
  489. static ssize_t name##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  490. { \
  491. struct parisc_device *padev = to_parisc_device(dev); \
  492. return sprintf(buf, format_string, padev->field); \
  493. } \
  494. static DEVICE_ATTR_RO(name);
  495. #define pa_dev_attr_id(field, format) pa_dev_attr(field, id.field, format)
  496. pa_dev_attr(irq, irq, "%u\n");
  497. pa_dev_attr_id(hw_type, "0x%02x\n");
  498. pa_dev_attr(rev, id.hversion_rev, "0x%x\n");
  499. pa_dev_attr_id(hversion, "0x%03x\n");
  500. pa_dev_attr_id(sversion, "0x%05x\n");
  501. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  502. {
  503. return make_modalias(dev, buf);
  504. }
  505. static DEVICE_ATTR_RO(modalias);
  506. static struct attribute *parisc_device_attrs[] = {
  507. &dev_attr_irq.attr,
  508. &dev_attr_hw_type.attr,
  509. &dev_attr_rev.attr,
  510. &dev_attr_hversion.attr,
  511. &dev_attr_sversion.attr,
  512. &dev_attr_modalias.attr,
  513. NULL,
  514. };
  515. ATTRIBUTE_GROUPS(parisc_device);
  516. struct bus_type parisc_bus_type = {
  517. .name = "parisc",
  518. .match = parisc_generic_match,
  519. .uevent = parisc_uevent,
  520. .dev_groups = parisc_device_groups,
  521. .probe = parisc_driver_probe,
  522. .remove = __exit_p(parisc_driver_remove),
  523. };
  524. /**
  525. * register_parisc_device - Locate a driver to manage this device.
  526. * @dev: The parisc device.
  527. *
  528. * Search the driver list for a driver that is willing to manage
  529. * this device.
  530. */
  531. int __init register_parisc_device(struct parisc_device *dev)
  532. {
  533. if (!dev)
  534. return 0;
  535. if (dev->driver)
  536. return 1;
  537. return 0;
  538. }
  539. /**
  540. * match_pci_device - Matches a pci device against a given hardware path
  541. * entry.
  542. * @dev: the generic device (known to be contained by a pci_dev).
  543. * @index: the current BC index
  544. * @modpath: the hardware path.
  545. * @return: true if the device matches the hardware path.
  546. */
  547. static int match_pci_device(struct device *dev, int index,
  548. struct hardware_path *modpath)
  549. {
  550. struct pci_dev *pdev = to_pci_dev(dev);
  551. int id;
  552. if (index == 5) {
  553. /* we are at the end of the path, and on the actual device */
  554. unsigned int devfn = pdev->devfn;
  555. return ((modpath->bc[5] == PCI_SLOT(devfn)) &&
  556. (modpath->mod == PCI_FUNC(devfn)));
  557. }
  558. /* index might be out of bounds for bc[] */
  559. if (index >= 6)
  560. return 0;
  561. id = PCI_SLOT(pdev->devfn) | (PCI_FUNC(pdev->devfn) << 5);
  562. return (modpath->bc[index] == id);
  563. }
  564. /**
  565. * match_parisc_device - Matches a parisc device against a given hardware
  566. * path entry.
  567. * @dev: the generic device (known to be contained by a parisc_device).
  568. * @index: the current BC index
  569. * @modpath: the hardware path.
  570. * @return: true if the device matches the hardware path.
  571. */
  572. static int match_parisc_device(struct device *dev, int index,
  573. struct hardware_path *modpath)
  574. {
  575. struct parisc_device *curr = to_parisc_device(dev);
  576. char id = (index == 6) ? modpath->mod : modpath->bc[index];
  577. return (curr->hw_path == id);
  578. }
  579. struct parse_tree_data {
  580. int index;
  581. struct hardware_path * modpath;
  582. struct device * dev;
  583. };
  584. static int check_parent(struct device * dev, void * data)
  585. {
  586. struct parse_tree_data * d = data;
  587. if (check_dev(dev)) {
  588. if (dev->bus == &parisc_bus_type) {
  589. if (match_parisc_device(dev, d->index, d->modpath))
  590. d->dev = dev;
  591. } else if (dev_is_pci(dev)) {
  592. if (match_pci_device(dev, d->index, d->modpath))
  593. d->dev = dev;
  594. } else if (dev->bus == NULL) {
  595. /* we are on a bus bridge */
  596. struct device *new = parse_tree_node(dev, d->index, d->modpath);
  597. if (new)
  598. d->dev = new;
  599. }
  600. }
  601. return d->dev != NULL;
  602. }
  603. /**
  604. * parse_tree_node - returns a device entry in the iotree
  605. * @parent: the parent node in the tree
  606. * @index: the current BC index
  607. * @modpath: the hardware_path struct to match a device against
  608. * @return: The corresponding device if found, NULL otherwise.
  609. *
  610. * Checks all the children of @parent for a matching @id. If none
  611. * found, it returns NULL.
  612. */
  613. static struct device *
  614. parse_tree_node(struct device *parent, int index, struct hardware_path *modpath)
  615. {
  616. struct parse_tree_data d = {
  617. .index = index,
  618. .modpath = modpath,
  619. };
  620. struct recurse_struct recurse_data = {
  621. .obj = &d,
  622. .fn = check_parent,
  623. };
  624. if (device_for_each_child(parent, &recurse_data, descend_children))
  625. /* nothing */;
  626. return d.dev;
  627. }
  628. /**
  629. * hwpath_to_device - Finds the generic device corresponding to a given hardware path.
  630. * @modpath: the hardware path.
  631. * @return: The target device, NULL if not found.
  632. */
  633. struct device *hwpath_to_device(struct hardware_path *modpath)
  634. {
  635. int i;
  636. struct device *parent = &root;
  637. for (i = 0; i < 6; i++) {
  638. if (modpath->bc[i] == -1)
  639. continue;
  640. parent = parse_tree_node(parent, i, modpath);
  641. if (!parent)
  642. return NULL;
  643. }
  644. if (dev_is_pci(parent)) /* pci devices already parse MOD */
  645. return parent;
  646. else
  647. return parse_tree_node(parent, 6, modpath);
  648. }
  649. EXPORT_SYMBOL(hwpath_to_device);
  650. /**
  651. * device_to_hwpath - Populates the hwpath corresponding to the given device.
  652. * @param dev the target device
  653. * @param path pointer to a previously allocated hwpath struct to be filled in
  654. */
  655. void device_to_hwpath(struct device *dev, struct hardware_path *path)
  656. {
  657. struct parisc_device *padev;
  658. if (dev->bus == &parisc_bus_type) {
  659. padev = to_parisc_device(dev);
  660. get_node_path(dev->parent, path);
  661. path->mod = padev->hw_path;
  662. } else if (dev_is_pci(dev)) {
  663. get_node_path(dev, path);
  664. }
  665. }
  666. EXPORT_SYMBOL(device_to_hwpath);
  667. #define BC_PORT_MASK 0x8
  668. #define BC_LOWER_PORT 0x8
  669. #define BUS_CONVERTER(dev) \
  670. ((dev->id.hw_type == HPHW_IOA) || (dev->id.hw_type == HPHW_BCPORT))
  671. #define IS_LOWER_PORT(dev) \
  672. ((gsc_readl(dev->hpa.start + offsetof(struct bc_module, io_status)) \
  673. & BC_PORT_MASK) == BC_LOWER_PORT)
  674. #define MAX_NATIVE_DEVICES 64
  675. #define NATIVE_DEVICE_OFFSET 0x1000
  676. #define FLEX_MASK F_EXTEND(0xfffc0000)
  677. #define IO_IO_LOW offsetof(struct bc_module, io_io_low)
  678. #define IO_IO_HIGH offsetof(struct bc_module, io_io_high)
  679. #define READ_IO_IO_LOW(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_LOW)
  680. #define READ_IO_IO_HIGH(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_HIGH)
  681. static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
  682. struct device *parent);
  683. static void walk_lower_bus(struct parisc_device *dev)
  684. {
  685. unsigned long io_io_low, io_io_high;
  686. if (!BUS_CONVERTER(dev) || IS_LOWER_PORT(dev))
  687. return;
  688. if (dev->id.hw_type == HPHW_IOA) {
  689. io_io_low = (unsigned long)(signed int)(READ_IO_IO_LOW(dev) << 16);
  690. io_io_high = io_io_low + MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET;
  691. } else {
  692. io_io_low = (READ_IO_IO_LOW(dev) + ~FLEX_MASK) & FLEX_MASK;
  693. io_io_high = (READ_IO_IO_HIGH(dev)+ ~FLEX_MASK) & FLEX_MASK;
  694. }
  695. walk_native_bus(io_io_low, io_io_high, &dev->dev);
  696. }
  697. /**
  698. * walk_native_bus -- Probe a bus for devices
  699. * @io_io_low: Base address of this bus.
  700. * @io_io_high: Last address of this bus.
  701. * @parent: The parent bus device.
  702. *
  703. * A native bus (eg Runway or GSC) may have up to 64 devices on it,
  704. * spaced at intervals of 0x1000 bytes. PDC may not inform us of these
  705. * devices, so we have to probe for them. Unfortunately, we may find
  706. * devices which are not physically connected (such as extra serial &
  707. * keyboard ports). This problem is not yet solved.
  708. */
  709. static void __init walk_native_bus(unsigned long io_io_low,
  710. unsigned long io_io_high, struct device *parent)
  711. {
  712. int i, devices_found = 0;
  713. unsigned long hpa = io_io_low;
  714. struct hardware_path path;
  715. get_node_path(parent, &path);
  716. do {
  717. for(i = 0; i < MAX_NATIVE_DEVICES; i++, hpa += NATIVE_DEVICE_OFFSET) {
  718. struct parisc_device *dev;
  719. /* Was the device already added by Firmware? */
  720. dev = find_device_by_addr(hpa);
  721. if (!dev) {
  722. path.mod = i;
  723. dev = alloc_pa_dev(hpa, &path);
  724. if (!dev)
  725. continue;
  726. register_parisc_device(dev);
  727. devices_found++;
  728. }
  729. walk_lower_bus(dev);
  730. }
  731. } while(!devices_found && hpa < io_io_high);
  732. }
  733. #define CENTRAL_BUS_ADDR F_EXTEND(0xfff80000)
  734. /**
  735. * walk_central_bus - Find devices attached to the central bus
  736. *
  737. * PDC doesn't tell us about all devices in the system. This routine
  738. * finds devices connected to the central bus.
  739. */
  740. void __init walk_central_bus(void)
  741. {
  742. walk_native_bus(CENTRAL_BUS_ADDR,
  743. CENTRAL_BUS_ADDR + (MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET),
  744. &root);
  745. }
  746. static void print_parisc_device(struct parisc_device *dev)
  747. {
  748. char hw_path[64];
  749. static int count;
  750. print_pa_hwpath(dev, hw_path);
  751. printk(KERN_INFO "%d. %s at 0x%px [%s] { %d, 0x%x, 0x%.3x, 0x%.5x }",
  752. ++count, dev->name, (void*) dev->hpa.start, hw_path, dev->id.hw_type,
  753. dev->id.hversion_rev, dev->id.hversion, dev->id.sversion);
  754. if (dev->num_addrs) {
  755. int k;
  756. pr_cont(", additional addresses: ");
  757. for (k = 0; k < dev->num_addrs; k++)
  758. pr_cont("0x%lx ", dev->addr[k]);
  759. }
  760. pr_cont("\n");
  761. }
  762. /**
  763. * init_parisc_bus - Some preparation to be done before inventory
  764. */
  765. void __init init_parisc_bus(void)
  766. {
  767. if (bus_register(&parisc_bus_type))
  768. panic("Could not register PA-RISC bus type\n");
  769. if (device_register(&root))
  770. panic("Could not register PA-RISC root device\n");
  771. get_device(&root);
  772. }
  773. static __init void qemu_header(void)
  774. {
  775. int num;
  776. unsigned long *p;
  777. pr_info("--- cut here ---\n");
  778. pr_info("/* AUTO-GENERATED HEADER FILE FOR SEABIOS FIRMWARE */\n");
  779. pr_cont("/* generated with Linux kernel */\n");
  780. pr_cont("/* search for PARISC_QEMU_MACHINE_HEADER in Linux */\n\n");
  781. pr_info("#define PARISC_MODEL \"%s\"\n\n",
  782. boot_cpu_data.pdc.sys_model_name);
  783. pr_info("#define PARISC_PDC_MODEL 0x%lx, 0x%lx, 0x%lx, "
  784. "0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx\n\n",
  785. #define p ((unsigned long *)&boot_cpu_data.pdc.model)
  786. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8]);
  787. #undef p
  788. pr_info("#define PARISC_PDC_VERSION 0x%04lx\n\n",
  789. boot_cpu_data.pdc.versions);
  790. pr_info("#define PARISC_PDC_CPUID 0x%04lx\n\n",
  791. boot_cpu_data.pdc.cpuid);
  792. pr_info("#define PARISC_PDC_CAPABILITIES 0x%04lx\n\n",
  793. boot_cpu_data.pdc.capabilities);
  794. pr_info("#define PARISC_PDC_ENTRY_ORG 0x%04lx\n\n",
  795. #ifdef CONFIG_64BIT
  796. (unsigned long)(PAGE0->mem_pdc_hi) << 32 |
  797. #endif
  798. (unsigned long)PAGE0->mem_pdc);
  799. pr_info("#define PARISC_PDC_CACHE_INFO");
  800. p = (unsigned long *) &cache_info;
  801. for (num = 0; num < sizeof(cache_info); num += sizeof(unsigned long)) {
  802. if (((num % 5) == 0)) {
  803. pr_cont(" \\\n");
  804. pr_info("\t");
  805. }
  806. pr_cont("%s0x%04lx",
  807. num?", ":"", *p++);
  808. }
  809. pr_cont("\n\n");
  810. }
  811. static __init int qemu_print_hpa(struct device *lin_dev, void *data)
  812. {
  813. struct parisc_device *dev = to_parisc_device(lin_dev);
  814. unsigned long hpa = dev->hpa.start;
  815. pr_cont("\t{\t.hpa = 0x%08lx,\\\n", hpa);
  816. pr_cont("\t\t.iodc = &iodc_data_hpa_%08lx,\\\n", hpa);
  817. pr_cont("\t\t.mod_info = &mod_info_hpa_%08lx,\\\n", hpa);
  818. pr_cont("\t\t.mod_path = &mod_path_hpa_%08lx,\\\n", hpa);
  819. pr_cont("\t\t.num_addr = HPA_%08lx_num_addr,\\\n", hpa);
  820. pr_cont("\t\t.add_addr = { HPA_%08lx_add_addr } },\\\n", hpa);
  821. return 0;
  822. }
  823. static __init void qemu_footer(void)
  824. {
  825. pr_info("\n\n#define PARISC_DEVICE_LIST \\\n");
  826. for_each_padev(qemu_print_hpa, NULL);
  827. pr_cont("\t{ 0, }\n");
  828. pr_info("--- cut here ---\n");
  829. }
  830. /* print iodc data of the various hpa modules for qemu inclusion */
  831. static __init int qemu_print_iodc_data(struct device *lin_dev, void *data)
  832. {
  833. struct parisc_device *dev = to_parisc_device(lin_dev);
  834. unsigned long count;
  835. unsigned long hpa = dev->hpa.start;
  836. int status;
  837. struct pdc_iodc iodc_data;
  838. int mod_index;
  839. struct pdc_system_map_mod_info pdc_mod_info;
  840. struct pdc_module_path mod_path;
  841. status = pdc_iodc_read(&count, hpa, 0,
  842. &iodc_data, sizeof(iodc_data));
  843. if (status != PDC_OK) {
  844. pr_info("No IODC data for hpa 0x%08lx\n", hpa);
  845. return 0;
  846. }
  847. pr_info("\n");
  848. pr_info("#define HPA_%08lx_DESCRIPTION \"%s\"\n",
  849. hpa, parisc_hardware_description(&dev->id));
  850. mod_index = 0;
  851. do {
  852. status = pdc_system_map_find_mods(&pdc_mod_info,
  853. &mod_path, mod_index++);
  854. } while (status == PDC_OK && pdc_mod_info.mod_addr != hpa);
  855. pr_info("static struct pdc_system_map_mod_info"
  856. " mod_info_hpa_%08lx = {\n", hpa);
  857. #define DO(member) \
  858. pr_cont("\t." #member " = 0x%x,\n", \
  859. (unsigned int)pdc_mod_info.member)
  860. DO(mod_addr);
  861. DO(mod_pgs);
  862. DO(add_addrs);
  863. pr_cont("};\n");
  864. #undef DO
  865. pr_info("static struct pdc_module_path "
  866. "mod_path_hpa_%08lx = {\n", hpa);
  867. pr_cont("\t.path = { ");
  868. pr_cont(".flags = 0x%x, ", mod_path.path.flags);
  869. pr_cont(".bc = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x }, ",
  870. (unsigned char)mod_path.path.bc[0],
  871. (unsigned char)mod_path.path.bc[1],
  872. (unsigned char)mod_path.path.bc[2],
  873. (unsigned char)mod_path.path.bc[3],
  874. (unsigned char)mod_path.path.bc[4],
  875. (unsigned char)mod_path.path.bc[5]);
  876. pr_cont(".mod = 0x%x ", mod_path.path.mod);
  877. pr_cont(" },\n");
  878. pr_cont("\t.layers = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x }\n",
  879. mod_path.layers[0], mod_path.layers[1], mod_path.layers[2],
  880. mod_path.layers[3], mod_path.layers[4], mod_path.layers[5]);
  881. pr_cont("};\n");
  882. pr_info("static struct pdc_iodc iodc_data_hpa_%08lx = {\n", hpa);
  883. #define DO(member) \
  884. pr_cont("\t." #member " = 0x%04lx,\n", \
  885. (unsigned long)iodc_data.member)
  886. DO(hversion_model);
  887. DO(hversion);
  888. DO(spa);
  889. DO(type);
  890. DO(sversion_rev);
  891. DO(sversion_model);
  892. DO(sversion_opt);
  893. DO(rev);
  894. DO(dep);
  895. DO(features);
  896. DO(checksum);
  897. DO(length);
  898. #undef DO
  899. pr_cont("\t/* pad: 0x%04x, 0x%04x */\n",
  900. iodc_data.pad[0], iodc_data.pad[1]);
  901. pr_cont("};\n");
  902. pr_info("#define HPA_%08lx_num_addr %d\n", hpa, dev->num_addrs);
  903. pr_info("#define HPA_%08lx_add_addr ", hpa);
  904. count = 0;
  905. if (dev->num_addrs == 0)
  906. pr_cont("0");
  907. while (count < dev->num_addrs) {
  908. pr_cont("0x%08lx, ", dev->addr[count]);
  909. count++;
  910. }
  911. pr_cont("\n\n");
  912. return 0;
  913. }
  914. static int print_one_device(struct device * dev, void * data)
  915. {
  916. struct parisc_device * pdev = to_parisc_device(dev);
  917. if (check_dev(dev))
  918. print_parisc_device(pdev);
  919. return 0;
  920. }
  921. /**
  922. * print_parisc_devices - Print out a list of devices found in this system
  923. */
  924. void __init print_parisc_devices(void)
  925. {
  926. for_each_padev(print_one_device, NULL);
  927. #define PARISC_QEMU_MACHINE_HEADER 0
  928. if (PARISC_QEMU_MACHINE_HEADER) {
  929. qemu_header();
  930. for_each_padev(qemu_print_iodc_data, NULL);
  931. qemu_footer();
  932. }
  933. }