drivers.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 <asm/hardware.h>
  36. #include <asm/io.h>
  37. #include <asm/pdc.h>
  38. #include <asm/parisc-device.h>
  39. /* See comments in include/asm-parisc/pci.h */
  40. struct hppa_dma_ops *hppa_dma_ops __read_mostly;
  41. EXPORT_SYMBOL(hppa_dma_ops);
  42. static struct device root = {
  43. .bus_id = "parisc",
  44. };
  45. static inline int check_dev(struct device *dev)
  46. {
  47. if (dev->bus == &parisc_bus_type) {
  48. struct parisc_device *pdev;
  49. pdev = to_parisc_device(dev);
  50. return pdev->id.hw_type != HPHW_FAULTY;
  51. }
  52. return 1;
  53. }
  54. static struct device *
  55. parse_tree_node(struct device *parent, int index, struct hardware_path *modpath);
  56. struct recurse_struct {
  57. void * obj;
  58. int (*fn)(struct device *, void *);
  59. };
  60. static int descend_children(struct device * dev, void * data)
  61. {
  62. struct recurse_struct * recurse_data = (struct recurse_struct *)data;
  63. if (recurse_data->fn(dev, recurse_data->obj))
  64. return 1;
  65. else
  66. return device_for_each_child(dev, recurse_data, descend_children);
  67. }
  68. /**
  69. * for_each_padev - Iterate over all devices in the tree
  70. * @fn: Function to call for each device.
  71. * @data: Data to pass to the called function.
  72. *
  73. * This performs a depth-first traversal of the tree, calling the
  74. * function passed for each node. It calls the function for parents
  75. * before children.
  76. */
  77. static int for_each_padev(int (*fn)(struct device *, void *), void * data)
  78. {
  79. struct recurse_struct recurse_data = {
  80. .obj = data,
  81. .fn = fn,
  82. };
  83. return device_for_each_child(&root, &recurse_data, descend_children);
  84. }
  85. /**
  86. * match_device - Report whether this driver can handle this device
  87. * @driver: the PA-RISC driver to try
  88. * @dev: the PA-RISC device to try
  89. */
  90. static int match_device(struct parisc_driver *driver, struct parisc_device *dev)
  91. {
  92. const struct parisc_device_id *ids;
  93. for (ids = driver->id_table; ids->sversion; ids++) {
  94. if ((ids->sversion != SVERSION_ANY_ID) &&
  95. (ids->sversion != dev->id.sversion))
  96. continue;
  97. if ((ids->hw_type != HWTYPE_ANY_ID) &&
  98. (ids->hw_type != dev->id.hw_type))
  99. continue;
  100. if ((ids->hversion != HVERSION_ANY_ID) &&
  101. (ids->hversion != dev->id.hversion))
  102. continue;
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. static int parisc_driver_probe(struct device *dev)
  108. {
  109. int rc;
  110. struct parisc_device *pa_dev = to_parisc_device(dev);
  111. struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
  112. rc = pa_drv->probe(pa_dev);
  113. if (!rc)
  114. pa_dev->driver = pa_drv;
  115. return rc;
  116. }
  117. static int parisc_driver_remove(struct device *dev)
  118. {
  119. struct parisc_device *pa_dev = to_parisc_device(dev);
  120. struct parisc_driver *pa_drv = to_parisc_driver(dev->driver);
  121. if (pa_drv->remove)
  122. pa_drv->remove(pa_dev);
  123. return 0;
  124. }
  125. /**
  126. * register_parisc_driver - Register this driver if it can handle a device
  127. * @driver: the PA-RISC driver to try
  128. */
  129. int register_parisc_driver(struct parisc_driver *driver)
  130. {
  131. /* FIXME: we need this because apparently the sti
  132. * driver can be registered twice */
  133. if(driver->drv.name) {
  134. printk(KERN_WARNING
  135. "BUG: skipping previously registered driver %s\n",
  136. driver->name);
  137. return 1;
  138. }
  139. if (!driver->probe) {
  140. printk(KERN_WARNING
  141. "BUG: driver %s has no probe routine\n",
  142. driver->name);
  143. return 1;
  144. }
  145. driver->drv.bus = &parisc_bus_type;
  146. /* We install our own probe and remove routines */
  147. WARN_ON(driver->drv.probe != NULL);
  148. WARN_ON(driver->drv.remove != NULL);
  149. driver->drv.name = driver->name;
  150. return driver_register(&driver->drv);
  151. }
  152. EXPORT_SYMBOL(register_parisc_driver);
  153. struct match_count {
  154. struct parisc_driver * driver;
  155. int count;
  156. };
  157. static int match_and_count(struct device * dev, void * data)
  158. {
  159. struct match_count * m = data;
  160. struct parisc_device * pdev = to_parisc_device(dev);
  161. if (check_dev(dev)) {
  162. if (match_device(m->driver, pdev))
  163. m->count++;
  164. }
  165. return 0;
  166. }
  167. /**
  168. * count_parisc_driver - count # of devices this driver would match
  169. * @driver: the PA-RISC driver to try
  170. *
  171. * Use by IOMMU support to "guess" the right size IOPdir.
  172. * Formula is something like memsize/(num_iommu * entry_size).
  173. */
  174. int count_parisc_driver(struct parisc_driver *driver)
  175. {
  176. struct match_count m = {
  177. .driver = driver,
  178. .count = 0,
  179. };
  180. for_each_padev(match_and_count, &m);
  181. return m.count;
  182. }
  183. /**
  184. * unregister_parisc_driver - Unregister this driver from the list of drivers
  185. * @driver: the PA-RISC driver to unregister
  186. */
  187. int unregister_parisc_driver(struct parisc_driver *driver)
  188. {
  189. driver_unregister(&driver->drv);
  190. return 0;
  191. }
  192. EXPORT_SYMBOL(unregister_parisc_driver);
  193. struct find_data {
  194. unsigned long hpa;
  195. struct parisc_device * dev;
  196. };
  197. static int find_device(struct device * dev, void * data)
  198. {
  199. struct parisc_device * pdev = to_parisc_device(dev);
  200. struct find_data * d = (struct find_data*)data;
  201. if (check_dev(dev)) {
  202. if (pdev->hpa.start == d->hpa) {
  203. d->dev = pdev;
  204. return 1;
  205. }
  206. }
  207. return 0;
  208. }
  209. static struct parisc_device *find_device_by_addr(unsigned long hpa)
  210. {
  211. struct find_data d = {
  212. .hpa = hpa,
  213. };
  214. int ret;
  215. ret = for_each_padev(find_device, &d);
  216. return ret ? d.dev : NULL;
  217. }
  218. /**
  219. * find_pa_parent_type - Find a parent of a specific type
  220. * @dev: The device to start searching from
  221. * @type: The device type to search for.
  222. *
  223. * Walks up the device tree looking for a device of the specified type.
  224. * If it finds it, it returns it. If not, it returns NULL.
  225. */
  226. const struct parisc_device *
  227. find_pa_parent_type(const struct parisc_device *padev, int type)
  228. {
  229. const struct device *dev = &padev->dev;
  230. while (dev != &root) {
  231. struct parisc_device *candidate = to_parisc_device(dev);
  232. if (candidate->id.hw_type == type)
  233. return candidate;
  234. dev = dev->parent;
  235. }
  236. return NULL;
  237. }
  238. #ifdef CONFIG_PCI
  239. static inline int is_pci_dev(struct device *dev)
  240. {
  241. return dev->bus == &pci_bus_type;
  242. }
  243. #else
  244. static inline int is_pci_dev(struct device *dev)
  245. {
  246. return 0;
  247. }
  248. #endif
  249. /*
  250. * get_node_path fills in @path with the firmware path to the device.
  251. * Note that if @node is a parisc device, we don't fill in the 'mod' field.
  252. * This is because both callers pass the parent and fill in the mod
  253. * themselves. If @node is a PCI device, we do fill it in, even though this
  254. * is inconsistent.
  255. */
  256. static void get_node_path(struct device *dev, struct hardware_path *path)
  257. {
  258. int i = 5;
  259. memset(&path->bc, -1, 6);
  260. if (is_pci_dev(dev)) {
  261. unsigned int devfn = to_pci_dev(dev)->devfn;
  262. path->mod = PCI_FUNC(devfn);
  263. path->bc[i--] = PCI_SLOT(devfn);
  264. dev = dev->parent;
  265. }
  266. while (dev != &root) {
  267. if (is_pci_dev(dev)) {
  268. unsigned int devfn = to_pci_dev(dev)->devfn;
  269. path->bc[i--] = PCI_SLOT(devfn) | (PCI_FUNC(devfn)<< 5);
  270. } else if (dev->bus == &parisc_bus_type) {
  271. path->bc[i--] = to_parisc_device(dev)->hw_path;
  272. }
  273. dev = dev->parent;
  274. }
  275. }
  276. static char *print_hwpath(struct hardware_path *path, char *output)
  277. {
  278. int i;
  279. for (i = 0; i < 6; i++) {
  280. if (path->bc[i] == -1)
  281. continue;
  282. output += sprintf(output, "%u/", (unsigned char) path->bc[i]);
  283. }
  284. output += sprintf(output, "%u", (unsigned char) path->mod);
  285. return output;
  286. }
  287. /**
  288. * print_pa_hwpath - Returns hardware path for PA devices
  289. * dev: The device to return the path for
  290. * output: Pointer to a previously-allocated array to place the path in.
  291. *
  292. * This function fills in the output array with a human-readable path
  293. * to a PA device. This string is compatible with that used by PDC, and
  294. * may be printed on the outside of the box.
  295. */
  296. char *print_pa_hwpath(struct parisc_device *dev, char *output)
  297. {
  298. struct hardware_path path;
  299. get_node_path(dev->dev.parent, &path);
  300. path.mod = dev->hw_path;
  301. return print_hwpath(&path, output);
  302. }
  303. EXPORT_SYMBOL(print_pa_hwpath);
  304. #if defined(CONFIG_PCI) || defined(CONFIG_ISA)
  305. /**
  306. * get_pci_node_path - Determines the hardware path for a PCI device
  307. * @pdev: The device to return the path for
  308. * @path: Pointer to a previously-allocated array to place the path in.
  309. *
  310. * This function fills in the hardware_path structure with the route to
  311. * the specified PCI device. This structure is suitable for passing to
  312. * PDC calls.
  313. */
  314. void get_pci_node_path(struct pci_dev *pdev, struct hardware_path *path)
  315. {
  316. get_node_path(&pdev->dev, path);
  317. }
  318. EXPORT_SYMBOL(get_pci_node_path);
  319. /**
  320. * print_pci_hwpath - Returns hardware path for PCI devices
  321. * dev: The device to return the path for
  322. * output: Pointer to a previously-allocated array to place the path in.
  323. *
  324. * This function fills in the output array with a human-readable path
  325. * to a PCI device. This string is compatible with that used by PDC, and
  326. * may be printed on the outside of the box.
  327. */
  328. char *print_pci_hwpath(struct pci_dev *dev, char *output)
  329. {
  330. struct hardware_path path;
  331. get_pci_node_path(dev, &path);
  332. return print_hwpath(&path, output);
  333. }
  334. EXPORT_SYMBOL(print_pci_hwpath);
  335. #endif /* defined(CONFIG_PCI) || defined(CONFIG_ISA) */
  336. static void setup_bus_id(struct parisc_device *padev)
  337. {
  338. struct hardware_path path;
  339. char *output = padev->dev.bus_id;
  340. int i;
  341. get_node_path(padev->dev.parent, &path);
  342. for (i = 0; i < 6; i++) {
  343. if (path.bc[i] == -1)
  344. continue;
  345. output += sprintf(output, "%u:", (unsigned char) path.bc[i]);
  346. }
  347. sprintf(output, "%u", (unsigned char) padev->hw_path);
  348. }
  349. struct parisc_device * create_tree_node(char id, struct device *parent)
  350. {
  351. struct parisc_device *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  352. if (!dev)
  353. return NULL;
  354. dev->hw_path = id;
  355. dev->id.hw_type = HPHW_FAULTY;
  356. dev->dev.parent = parent;
  357. setup_bus_id(dev);
  358. dev->dev.bus = &parisc_bus_type;
  359. dev->dma_mask = 0xffffffffUL; /* PARISC devices are 32-bit */
  360. /* make the generic dma mask a pointer to the parisc one */
  361. dev->dev.dma_mask = &dev->dma_mask;
  362. dev->dev.coherent_dma_mask = dev->dma_mask;
  363. if (device_register(&dev->dev)) {
  364. kfree(dev);
  365. return NULL;
  366. }
  367. return dev;
  368. }
  369. struct match_id_data {
  370. char id;
  371. struct parisc_device * dev;
  372. };
  373. static int match_by_id(struct device * dev, void * data)
  374. {
  375. struct parisc_device * pdev = to_parisc_device(dev);
  376. struct match_id_data * d = data;
  377. if (pdev->hw_path == d->id) {
  378. d->dev = pdev;
  379. return 1;
  380. }
  381. return 0;
  382. }
  383. /**
  384. * alloc_tree_node - returns a device entry in the iotree
  385. * @parent: the parent node in the tree
  386. * @id: the element of the module path for this entry
  387. *
  388. * Checks all the children of @parent for a matching @id. If none
  389. * found, it allocates a new device and returns it.
  390. */
  391. static struct parisc_device * alloc_tree_node(struct device *parent, char id)
  392. {
  393. struct match_id_data d = {
  394. .id = id,
  395. };
  396. if (device_for_each_child(parent, &d, match_by_id))
  397. return d.dev;
  398. else
  399. return create_tree_node(id, parent);
  400. }
  401. static struct parisc_device *create_parisc_device(struct hardware_path *modpath)
  402. {
  403. int i;
  404. struct device *parent = &root;
  405. for (i = 0; i < 6; i++) {
  406. if (modpath->bc[i] == -1)
  407. continue;
  408. parent = &alloc_tree_node(parent, modpath->bc[i])->dev;
  409. }
  410. return alloc_tree_node(parent, modpath->mod);
  411. }
  412. struct parisc_device *
  413. alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
  414. {
  415. int status;
  416. unsigned long bytecnt;
  417. u8 iodc_data[32];
  418. struct parisc_device *dev;
  419. const char *name;
  420. /* Check to make sure this device has not already been added - Ryan */
  421. if (find_device_by_addr(hpa) != NULL)
  422. return NULL;
  423. status = pdc_iodc_read(&bytecnt, hpa, 0, &iodc_data, 32);
  424. if (status != PDC_OK)
  425. return NULL;
  426. dev = create_parisc_device(mod_path);
  427. if (dev->id.hw_type != HPHW_FAULTY) {
  428. printk(KERN_ERR "Two devices have hardware path [%s]. "
  429. "IODC data for second device: "
  430. "%02x%02x%02x%02x%02x%02x\n"
  431. "Rearranging GSC cards sometimes helps\n",
  432. parisc_pathname(dev), iodc_data[0], iodc_data[1],
  433. iodc_data[3], iodc_data[4], iodc_data[5], iodc_data[6]);
  434. return NULL;
  435. }
  436. dev->id.hw_type = iodc_data[3] & 0x1f;
  437. dev->id.hversion = (iodc_data[0] << 4) | ((iodc_data[1] & 0xf0) >> 4);
  438. dev->id.hversion_rev = iodc_data[1] & 0x0f;
  439. dev->id.sversion = ((iodc_data[4] & 0x0f) << 16) |
  440. (iodc_data[5] << 8) | iodc_data[6];
  441. dev->hpa.name = parisc_pathname(dev);
  442. dev->hpa.start = hpa;
  443. /* This is awkward. The STI spec says that gfx devices may occupy
  444. * 32MB or 64MB. Unfortunately, we don't know how to tell whether
  445. * it's the former or the latter. Assumptions either way can hurt us.
  446. */
  447. if (hpa == 0xf4000000 || hpa == 0xf8000000) {
  448. dev->hpa.end = hpa + 0x03ffffff;
  449. } else if (hpa == 0xf6000000 || hpa == 0xfa000000) {
  450. dev->hpa.end = hpa + 0x01ffffff;
  451. } else {
  452. dev->hpa.end = hpa + 0xfff;
  453. }
  454. dev->hpa.flags = IORESOURCE_MEM;
  455. name = parisc_hardware_description(&dev->id);
  456. if (name) {
  457. strlcpy(dev->name, name, sizeof(dev->name));
  458. }
  459. /* Silently fail things like mouse ports which are subsumed within
  460. * the keyboard controller
  461. */
  462. if ((hpa & 0xfff) == 0 && insert_resource(&iomem_resource, &dev->hpa))
  463. printk("Unable to claim HPA %lx for device %s\n",
  464. hpa, name);
  465. return dev;
  466. }
  467. static int parisc_generic_match(struct device *dev, struct device_driver *drv)
  468. {
  469. return match_device(to_parisc_driver(drv), to_parisc_device(dev));
  470. }
  471. #define pa_dev_attr(name, field, format_string) \
  472. static ssize_t name##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  473. { \
  474. struct parisc_device *padev = to_parisc_device(dev); \
  475. return sprintf(buf, format_string, padev->field); \
  476. }
  477. #define pa_dev_attr_id(field, format) pa_dev_attr(field, id.field, format)
  478. pa_dev_attr(irq, irq, "%u\n");
  479. pa_dev_attr_id(hw_type, "0x%02x\n");
  480. pa_dev_attr(rev, id.hversion_rev, "0x%x\n");
  481. pa_dev_attr_id(hversion, "0x%03x\n");
  482. pa_dev_attr_id(sversion, "0x%05x\n");
  483. static struct device_attribute parisc_device_attrs[] = {
  484. __ATTR_RO(irq),
  485. __ATTR_RO(hw_type),
  486. __ATTR_RO(rev),
  487. __ATTR_RO(hversion),
  488. __ATTR_RO(sversion),
  489. __ATTR_NULL,
  490. };
  491. struct bus_type parisc_bus_type = {
  492. .name = "parisc",
  493. .match = parisc_generic_match,
  494. .dev_attrs = parisc_device_attrs,
  495. .probe = parisc_driver_probe,
  496. .remove = parisc_driver_remove,
  497. };
  498. /**
  499. * register_parisc_device - Locate a driver to manage this device.
  500. * @dev: The parisc device.
  501. *
  502. * Search the driver list for a driver that is willing to manage
  503. * this device.
  504. */
  505. int register_parisc_device(struct parisc_device *dev)
  506. {
  507. if (!dev)
  508. return 0;
  509. if (dev->driver)
  510. return 1;
  511. return 0;
  512. }
  513. /**
  514. * match_pci_device - Matches a pci device against a given hardware path
  515. * entry.
  516. * @dev: the generic device (known to be contained by a pci_dev).
  517. * @index: the current BC index
  518. * @modpath: the hardware path.
  519. * @return: true if the device matches the hardware path.
  520. */
  521. static int match_pci_device(struct device *dev, int index,
  522. struct hardware_path *modpath)
  523. {
  524. struct pci_dev *pdev = to_pci_dev(dev);
  525. int id;
  526. if (index == 5) {
  527. /* we are at the end of the path, and on the actual device */
  528. unsigned int devfn = pdev->devfn;
  529. return ((modpath->bc[5] == PCI_SLOT(devfn)) &&
  530. (modpath->mod == PCI_FUNC(devfn)));
  531. }
  532. id = PCI_SLOT(pdev->devfn) | (PCI_FUNC(pdev->devfn) << 5);
  533. return (modpath->bc[index] == id);
  534. }
  535. /**
  536. * match_parisc_device - Matches a parisc device against a given hardware
  537. * path entry.
  538. * @dev: the generic device (known to be contained by a parisc_device).
  539. * @index: the current BC index
  540. * @modpath: the hardware path.
  541. * @return: true if the device matches the hardware path.
  542. */
  543. static int match_parisc_device(struct device *dev, int index,
  544. struct hardware_path *modpath)
  545. {
  546. struct parisc_device *curr = to_parisc_device(dev);
  547. char id = (index == 6) ? modpath->mod : modpath->bc[index];
  548. return (curr->hw_path == id);
  549. }
  550. struct parse_tree_data {
  551. int index;
  552. struct hardware_path * modpath;
  553. struct device * dev;
  554. };
  555. static int check_parent(struct device * dev, void * data)
  556. {
  557. struct parse_tree_data * d = data;
  558. if (check_dev(dev)) {
  559. if (dev->bus == &parisc_bus_type) {
  560. if (match_parisc_device(dev, d->index, d->modpath))
  561. d->dev = dev;
  562. } else if (is_pci_dev(dev)) {
  563. if (match_pci_device(dev, d->index, d->modpath))
  564. d->dev = dev;
  565. } else if (dev->bus == NULL) {
  566. /* we are on a bus bridge */
  567. struct device *new = parse_tree_node(dev, d->index, d->modpath);
  568. if (new)
  569. d->dev = new;
  570. }
  571. }
  572. return d->dev != NULL;
  573. }
  574. /**
  575. * parse_tree_node - returns a device entry in the iotree
  576. * @parent: the parent node in the tree
  577. * @index: the current BC index
  578. * @modpath: the hardware_path struct to match a device against
  579. * @return: The corresponding device if found, NULL otherwise.
  580. *
  581. * Checks all the children of @parent for a matching @id. If none
  582. * found, it returns NULL.
  583. */
  584. static struct device *
  585. parse_tree_node(struct device *parent, int index, struct hardware_path *modpath)
  586. {
  587. struct parse_tree_data d = {
  588. .index = index,
  589. .modpath = modpath,
  590. };
  591. struct recurse_struct recurse_data = {
  592. .obj = &d,
  593. .fn = check_parent,
  594. };
  595. device_for_each_child(parent, &recurse_data, descend_children);
  596. return d.dev;
  597. }
  598. /**
  599. * hwpath_to_device - Finds the generic device corresponding to a given hardware path.
  600. * @modpath: the hardware path.
  601. * @return: The target device, NULL if not found.
  602. */
  603. struct device *hwpath_to_device(struct hardware_path *modpath)
  604. {
  605. int i;
  606. struct device *parent = &root;
  607. for (i = 0; i < 6; i++) {
  608. if (modpath->bc[i] == -1)
  609. continue;
  610. parent = parse_tree_node(parent, i, modpath);
  611. if (!parent)
  612. return NULL;
  613. }
  614. if (is_pci_dev(parent)) /* pci devices already parse MOD */
  615. return parent;
  616. else
  617. return parse_tree_node(parent, 6, modpath);
  618. }
  619. EXPORT_SYMBOL(hwpath_to_device);
  620. /**
  621. * device_to_hwpath - Populates the hwpath corresponding to the given device.
  622. * @param dev the target device
  623. * @param path pointer to a previously allocated hwpath struct to be filled in
  624. */
  625. void device_to_hwpath(struct device *dev, struct hardware_path *path)
  626. {
  627. struct parisc_device *padev;
  628. if (dev->bus == &parisc_bus_type) {
  629. padev = to_parisc_device(dev);
  630. get_node_path(dev->parent, path);
  631. path->mod = padev->hw_path;
  632. } else if (is_pci_dev(dev)) {
  633. get_node_path(dev, path);
  634. }
  635. }
  636. EXPORT_SYMBOL(device_to_hwpath);
  637. #define BC_PORT_MASK 0x8
  638. #define BC_LOWER_PORT 0x8
  639. #define BUS_CONVERTER(dev) \
  640. ((dev->id.hw_type == HPHW_IOA) || (dev->id.hw_type == HPHW_BCPORT))
  641. #define IS_LOWER_PORT(dev) \
  642. ((gsc_readl(dev->hpa.start + offsetof(struct bc_module, io_status)) \
  643. & BC_PORT_MASK) == BC_LOWER_PORT)
  644. #define MAX_NATIVE_DEVICES 64
  645. #define NATIVE_DEVICE_OFFSET 0x1000
  646. #define FLEX_MASK F_EXTEND(0xfffc0000)
  647. #define IO_IO_LOW offsetof(struct bc_module, io_io_low)
  648. #define IO_IO_HIGH offsetof(struct bc_module, io_io_high)
  649. #define READ_IO_IO_LOW(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_LOW)
  650. #define READ_IO_IO_HIGH(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_HIGH)
  651. static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
  652. struct device *parent);
  653. void walk_lower_bus(struct parisc_device *dev)
  654. {
  655. unsigned long io_io_low, io_io_high;
  656. if (!BUS_CONVERTER(dev) || IS_LOWER_PORT(dev))
  657. return;
  658. if (dev->id.hw_type == HPHW_IOA) {
  659. io_io_low = (unsigned long)(signed int)(READ_IO_IO_LOW(dev) << 16);
  660. io_io_high = io_io_low + MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET;
  661. } else {
  662. io_io_low = (READ_IO_IO_LOW(dev) + ~FLEX_MASK) & FLEX_MASK;
  663. io_io_high = (READ_IO_IO_HIGH(dev)+ ~FLEX_MASK) & FLEX_MASK;
  664. }
  665. walk_native_bus(io_io_low, io_io_high, &dev->dev);
  666. }
  667. /**
  668. * walk_native_bus -- Probe a bus for devices
  669. * @io_io_low: Base address of this bus.
  670. * @io_io_high: Last address of this bus.
  671. * @parent: The parent bus device.
  672. *
  673. * A native bus (eg Runway or GSC) may have up to 64 devices on it,
  674. * spaced at intervals of 0x1000 bytes. PDC may not inform us of these
  675. * devices, so we have to probe for them. Unfortunately, we may find
  676. * devices which are not physically connected (such as extra serial &
  677. * keyboard ports). This problem is not yet solved.
  678. */
  679. static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
  680. struct device *parent)
  681. {
  682. int i, devices_found = 0;
  683. unsigned long hpa = io_io_low;
  684. struct hardware_path path;
  685. get_node_path(parent, &path);
  686. do {
  687. for(i = 0; i < MAX_NATIVE_DEVICES; i++, hpa += NATIVE_DEVICE_OFFSET) {
  688. struct parisc_device *dev;
  689. /* Was the device already added by Firmware? */
  690. dev = find_device_by_addr(hpa);
  691. if (!dev) {
  692. path.mod = i;
  693. dev = alloc_pa_dev(hpa, &path);
  694. if (!dev)
  695. continue;
  696. register_parisc_device(dev);
  697. devices_found++;
  698. }
  699. walk_lower_bus(dev);
  700. }
  701. } while(!devices_found && hpa < io_io_high);
  702. }
  703. #define CENTRAL_BUS_ADDR F_EXTEND(0xfff80000)
  704. /**
  705. * walk_central_bus - Find devices attached to the central bus
  706. *
  707. * PDC doesn't tell us about all devices in the system. This routine
  708. * finds devices connected to the central bus.
  709. */
  710. void walk_central_bus(void)
  711. {
  712. walk_native_bus(CENTRAL_BUS_ADDR,
  713. CENTRAL_BUS_ADDR + (MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET),
  714. &root);
  715. }
  716. static void print_parisc_device(struct parisc_device *dev)
  717. {
  718. char hw_path[64];
  719. static int count;
  720. print_pa_hwpath(dev, hw_path);
  721. printk(KERN_INFO "%d. %s at 0x%lx [%s] { %d, 0x%x, 0x%.3x, 0x%.5x }",
  722. ++count, dev->name, dev->hpa.start, hw_path, dev->id.hw_type,
  723. dev->id.hversion_rev, dev->id.hversion, dev->id.sversion);
  724. if (dev->num_addrs) {
  725. int k;
  726. printk(", additional addresses: ");
  727. for (k = 0; k < dev->num_addrs; k++)
  728. printk("0x%lx ", dev->addr[k]);
  729. }
  730. printk("\n");
  731. }
  732. /**
  733. * init_parisc_bus - Some preparation to be done before inventory
  734. */
  735. void init_parisc_bus(void)
  736. {
  737. if (bus_register(&parisc_bus_type))
  738. panic("Could not register PA-RISC bus type\n");
  739. if (device_register(&root))
  740. panic("Could not register PA-RISC root device\n");
  741. get_device(&root);
  742. }
  743. static int print_one_device(struct device * dev, void * data)
  744. {
  745. struct parisc_device * pdev = to_parisc_device(dev);
  746. if (check_dev(dev))
  747. print_parisc_device(pdev);
  748. return 0;
  749. }
  750. /**
  751. * print_parisc_devices - Print out a list of devices found in this system
  752. */
  753. void print_parisc_devices(void)
  754. {
  755. for_each_padev(print_one_device, NULL);
  756. }