drivers.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. 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 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 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 * 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 * alloc_tree_node(struct device *parent, char id)
  384. {
  385. struct match_id_data d = {
  386. .id = id,
  387. };
  388. if (device_for_each_child(parent, &d, match_by_id))
  389. return d.dev;
  390. else
  391. return create_tree_node(id, parent);
  392. }
  393. static struct parisc_device *create_parisc_device(struct hardware_path *modpath)
  394. {
  395. int i;
  396. struct device *parent = &root;
  397. for (i = 0; i < 6; i++) {
  398. if (modpath->bc[i] == -1)
  399. continue;
  400. parent = &alloc_tree_node(parent, modpath->bc[i])->dev;
  401. }
  402. return alloc_tree_node(parent, modpath->mod);
  403. }
  404. struct parisc_device *
  405. alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
  406. {
  407. int status;
  408. unsigned long bytecnt;
  409. u8 iodc_data[32];
  410. struct parisc_device *dev;
  411. const char *name;
  412. /* Check to make sure this device has not already been added - Ryan */
  413. if (find_device_by_addr(hpa) != NULL)
  414. return NULL;
  415. status = pdc_iodc_read(&bytecnt, hpa, 0, &iodc_data, 32);
  416. if (status != PDC_OK)
  417. return NULL;
  418. dev = create_parisc_device(mod_path);
  419. if (dev->id.hw_type != HPHW_FAULTY) {
  420. printk(KERN_ERR "Two devices have hardware path [%s]. "
  421. "IODC data for second device: "
  422. "%02x%02x%02x%02x%02x%02x\n"
  423. "Rearranging GSC cards sometimes helps\n",
  424. parisc_pathname(dev), iodc_data[0], iodc_data[1],
  425. iodc_data[3], iodc_data[4], iodc_data[5], iodc_data[6]);
  426. return NULL;
  427. }
  428. dev->id.hw_type = iodc_data[3] & 0x1f;
  429. dev->id.hversion = (iodc_data[0] << 4) | ((iodc_data[1] & 0xf0) >> 4);
  430. dev->id.hversion_rev = iodc_data[1] & 0x0f;
  431. dev->id.sversion = ((iodc_data[4] & 0x0f) << 16) |
  432. (iodc_data[5] << 8) | iodc_data[6];
  433. dev->hpa.name = parisc_pathname(dev);
  434. dev->hpa.start = hpa;
  435. /* This is awkward. The STI spec says that gfx devices may occupy
  436. * 32MB or 64MB. Unfortunately, we don't know how to tell whether
  437. * it's the former or the latter. Assumptions either way can hurt us.
  438. */
  439. if (hpa == 0xf4000000 || hpa == 0xf8000000) {
  440. dev->hpa.end = hpa + 0x03ffffff;
  441. } else if (hpa == 0xf6000000 || hpa == 0xfa000000) {
  442. dev->hpa.end = hpa + 0x01ffffff;
  443. } else {
  444. dev->hpa.end = hpa + 0xfff;
  445. }
  446. dev->hpa.flags = IORESOURCE_MEM;
  447. name = parisc_hardware_description(&dev->id);
  448. if (name) {
  449. strlcpy(dev->name, name, sizeof(dev->name));
  450. }
  451. /* Silently fail things like mouse ports which are subsumed within
  452. * the keyboard controller
  453. */
  454. if ((hpa & 0xfff) == 0 && insert_resource(&iomem_resource, &dev->hpa))
  455. printk("Unable to claim HPA %lx for device %s\n",
  456. hpa, name);
  457. return dev;
  458. }
  459. static int parisc_generic_match(struct device *dev, struct device_driver *drv)
  460. {
  461. return match_device(to_parisc_driver(drv), to_parisc_device(dev));
  462. }
  463. static ssize_t make_modalias(struct device *dev, char *buf)
  464. {
  465. const struct parisc_device *padev = to_parisc_device(dev);
  466. const struct parisc_device_id *id = &padev->id;
  467. return sprintf(buf, "parisc:t%02Xhv%04Xrev%02Xsv%08X\n",
  468. (u8)id->hw_type, (u16)id->hversion, (u8)id->hversion_rev,
  469. (u32)id->sversion);
  470. }
  471. static int parisc_uevent(struct device *dev, struct kobj_uevent_env *env)
  472. {
  473. const struct parisc_device *padev;
  474. char modalias[40];
  475. if (!dev)
  476. return -ENODEV;
  477. padev = to_parisc_device(dev);
  478. if (!padev)
  479. return -ENODEV;
  480. if (add_uevent_var(env, "PARISC_NAME=%s", padev->name))
  481. return -ENOMEM;
  482. make_modalias(dev, modalias);
  483. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  484. return -ENOMEM;
  485. return 0;
  486. }
  487. #define pa_dev_attr(name, field, format_string) \
  488. static ssize_t name##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  489. { \
  490. struct parisc_device *padev = to_parisc_device(dev); \
  491. return sprintf(buf, format_string, padev->field); \
  492. }
  493. #define pa_dev_attr_id(field, format) pa_dev_attr(field, id.field, format)
  494. pa_dev_attr(irq, irq, "%u\n");
  495. pa_dev_attr_id(hw_type, "0x%02x\n");
  496. pa_dev_attr(rev, id.hversion_rev, "0x%x\n");
  497. pa_dev_attr_id(hversion, "0x%03x\n");
  498. pa_dev_attr_id(sversion, "0x%05x\n");
  499. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  500. {
  501. return make_modalias(dev, buf);
  502. }
  503. static struct device_attribute parisc_device_attrs[] = {
  504. __ATTR_RO(irq),
  505. __ATTR_RO(hw_type),
  506. __ATTR_RO(rev),
  507. __ATTR_RO(hversion),
  508. __ATTR_RO(sversion),
  509. __ATTR_RO(modalias),
  510. __ATTR_NULL,
  511. };
  512. struct bus_type parisc_bus_type = {
  513. .name = "parisc",
  514. .match = parisc_generic_match,
  515. .uevent = parisc_uevent,
  516. .dev_attrs = parisc_device_attrs,
  517. .probe = parisc_driver_probe,
  518. .remove = parisc_driver_remove,
  519. };
  520. /**
  521. * register_parisc_device - Locate a driver to manage this device.
  522. * @dev: The parisc device.
  523. *
  524. * Search the driver list for a driver that is willing to manage
  525. * this device.
  526. */
  527. int register_parisc_device(struct parisc_device *dev)
  528. {
  529. if (!dev)
  530. return 0;
  531. if (dev->driver)
  532. return 1;
  533. return 0;
  534. }
  535. /**
  536. * match_pci_device - Matches a pci device against a given hardware path
  537. * entry.
  538. * @dev: the generic device (known to be contained by a pci_dev).
  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_pci_device(struct device *dev, int index,
  544. struct hardware_path *modpath)
  545. {
  546. struct pci_dev *pdev = to_pci_dev(dev);
  547. int id;
  548. if (index == 5) {
  549. /* we are at the end of the path, and on the actual device */
  550. unsigned int devfn = pdev->devfn;
  551. return ((modpath->bc[5] == PCI_SLOT(devfn)) &&
  552. (modpath->mod == PCI_FUNC(devfn)));
  553. }
  554. /* index might be out of bounds for bc[] */
  555. if (index >= 6)
  556. return 0;
  557. id = PCI_SLOT(pdev->devfn) | (PCI_FUNC(pdev->devfn) << 5);
  558. return (modpath->bc[index] == id);
  559. }
  560. /**
  561. * match_parisc_device - Matches a parisc device against a given hardware
  562. * path entry.
  563. * @dev: the generic device (known to be contained by a parisc_device).
  564. * @index: the current BC index
  565. * @modpath: the hardware path.
  566. * @return: true if the device matches the hardware path.
  567. */
  568. static int match_parisc_device(struct device *dev, int index,
  569. struct hardware_path *modpath)
  570. {
  571. struct parisc_device *curr = to_parisc_device(dev);
  572. char id = (index == 6) ? modpath->mod : modpath->bc[index];
  573. return (curr->hw_path == id);
  574. }
  575. struct parse_tree_data {
  576. int index;
  577. struct hardware_path * modpath;
  578. struct device * dev;
  579. };
  580. static int check_parent(struct device * dev, void * data)
  581. {
  582. struct parse_tree_data * d = data;
  583. if (check_dev(dev)) {
  584. if (dev->bus == &parisc_bus_type) {
  585. if (match_parisc_device(dev, d->index, d->modpath))
  586. d->dev = dev;
  587. } else if (dev_is_pci(dev)) {
  588. if (match_pci_device(dev, d->index, d->modpath))
  589. d->dev = dev;
  590. } else if (dev->bus == NULL) {
  591. /* we are on a bus bridge */
  592. struct device *new = parse_tree_node(dev, d->index, d->modpath);
  593. if (new)
  594. d->dev = new;
  595. }
  596. }
  597. return d->dev != NULL;
  598. }
  599. /**
  600. * parse_tree_node - returns a device entry in the iotree
  601. * @parent: the parent node in the tree
  602. * @index: the current BC index
  603. * @modpath: the hardware_path struct to match a device against
  604. * @return: The corresponding device if found, NULL otherwise.
  605. *
  606. * Checks all the children of @parent for a matching @id. If none
  607. * found, it returns NULL.
  608. */
  609. static struct device *
  610. parse_tree_node(struct device *parent, int index, struct hardware_path *modpath)
  611. {
  612. struct parse_tree_data d = {
  613. .index = index,
  614. .modpath = modpath,
  615. };
  616. struct recurse_struct recurse_data = {
  617. .obj = &d,
  618. .fn = check_parent,
  619. };
  620. if (device_for_each_child(parent, &recurse_data, descend_children))
  621. /* nothing */;
  622. return d.dev;
  623. }
  624. /**
  625. * hwpath_to_device - Finds the generic device corresponding to a given hardware path.
  626. * @modpath: the hardware path.
  627. * @return: The target device, NULL if not found.
  628. */
  629. struct device *hwpath_to_device(struct hardware_path *modpath)
  630. {
  631. int i;
  632. struct device *parent = &root;
  633. for (i = 0; i < 6; i++) {
  634. if (modpath->bc[i] == -1)
  635. continue;
  636. parent = parse_tree_node(parent, i, modpath);
  637. if (!parent)
  638. return NULL;
  639. }
  640. if (dev_is_pci(parent)) /* pci devices already parse MOD */
  641. return parent;
  642. else
  643. return parse_tree_node(parent, 6, modpath);
  644. }
  645. EXPORT_SYMBOL(hwpath_to_device);
  646. /**
  647. * device_to_hwpath - Populates the hwpath corresponding to the given device.
  648. * @param dev the target device
  649. * @param path pointer to a previously allocated hwpath struct to be filled in
  650. */
  651. void device_to_hwpath(struct device *dev, struct hardware_path *path)
  652. {
  653. struct parisc_device *padev;
  654. if (dev->bus == &parisc_bus_type) {
  655. padev = to_parisc_device(dev);
  656. get_node_path(dev->parent, path);
  657. path->mod = padev->hw_path;
  658. } else if (dev_is_pci(dev)) {
  659. get_node_path(dev, path);
  660. }
  661. }
  662. EXPORT_SYMBOL(device_to_hwpath);
  663. #define BC_PORT_MASK 0x8
  664. #define BC_LOWER_PORT 0x8
  665. #define BUS_CONVERTER(dev) \
  666. ((dev->id.hw_type == HPHW_IOA) || (dev->id.hw_type == HPHW_BCPORT))
  667. #define IS_LOWER_PORT(dev) \
  668. ((gsc_readl(dev->hpa.start + offsetof(struct bc_module, io_status)) \
  669. & BC_PORT_MASK) == BC_LOWER_PORT)
  670. #define MAX_NATIVE_DEVICES 64
  671. #define NATIVE_DEVICE_OFFSET 0x1000
  672. #define FLEX_MASK F_EXTEND(0xfffc0000)
  673. #define IO_IO_LOW offsetof(struct bc_module, io_io_low)
  674. #define IO_IO_HIGH offsetof(struct bc_module, io_io_high)
  675. #define READ_IO_IO_LOW(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_LOW)
  676. #define READ_IO_IO_HIGH(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_HIGH)
  677. static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
  678. struct device *parent);
  679. void walk_lower_bus(struct parisc_device *dev)
  680. {
  681. unsigned long io_io_low, io_io_high;
  682. if (!BUS_CONVERTER(dev) || IS_LOWER_PORT(dev))
  683. return;
  684. if (dev->id.hw_type == HPHW_IOA) {
  685. io_io_low = (unsigned long)(signed int)(READ_IO_IO_LOW(dev) << 16);
  686. io_io_high = io_io_low + MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET;
  687. } else {
  688. io_io_low = (READ_IO_IO_LOW(dev) + ~FLEX_MASK) & FLEX_MASK;
  689. io_io_high = (READ_IO_IO_HIGH(dev)+ ~FLEX_MASK) & FLEX_MASK;
  690. }
  691. walk_native_bus(io_io_low, io_io_high, &dev->dev);
  692. }
  693. /**
  694. * walk_native_bus -- Probe a bus for devices
  695. * @io_io_low: Base address of this bus.
  696. * @io_io_high: Last address of this bus.
  697. * @parent: The parent bus device.
  698. *
  699. * A native bus (eg Runway or GSC) may have up to 64 devices on it,
  700. * spaced at intervals of 0x1000 bytes. PDC may not inform us of these
  701. * devices, so we have to probe for them. Unfortunately, we may find
  702. * devices which are not physically connected (such as extra serial &
  703. * keyboard ports). This problem is not yet solved.
  704. */
  705. static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
  706. struct device *parent)
  707. {
  708. int i, devices_found = 0;
  709. unsigned long hpa = io_io_low;
  710. struct hardware_path path;
  711. get_node_path(parent, &path);
  712. do {
  713. for(i = 0; i < MAX_NATIVE_DEVICES; i++, hpa += NATIVE_DEVICE_OFFSET) {
  714. struct parisc_device *dev;
  715. /* Was the device already added by Firmware? */
  716. dev = find_device_by_addr(hpa);
  717. if (!dev) {
  718. path.mod = i;
  719. dev = alloc_pa_dev(hpa, &path);
  720. if (!dev)
  721. continue;
  722. register_parisc_device(dev);
  723. devices_found++;
  724. }
  725. walk_lower_bus(dev);
  726. }
  727. } while(!devices_found && hpa < io_io_high);
  728. }
  729. #define CENTRAL_BUS_ADDR F_EXTEND(0xfff80000)
  730. /**
  731. * walk_central_bus - Find devices attached to the central bus
  732. *
  733. * PDC doesn't tell us about all devices in the system. This routine
  734. * finds devices connected to the central bus.
  735. */
  736. void walk_central_bus(void)
  737. {
  738. walk_native_bus(CENTRAL_BUS_ADDR,
  739. CENTRAL_BUS_ADDR + (MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET),
  740. &root);
  741. }
  742. static void print_parisc_device(struct parisc_device *dev)
  743. {
  744. char hw_path[64];
  745. static int count;
  746. print_pa_hwpath(dev, hw_path);
  747. printk(KERN_INFO "%d. %s at 0x%p [%s] { %d, 0x%x, 0x%.3x, 0x%.5x }",
  748. ++count, dev->name, (void*) dev->hpa.start, hw_path, dev->id.hw_type,
  749. dev->id.hversion_rev, dev->id.hversion, dev->id.sversion);
  750. if (dev->num_addrs) {
  751. int k;
  752. pr_cont(", additional addresses: ");
  753. for (k = 0; k < dev->num_addrs; k++)
  754. pr_cont("0x%lx ", dev->addr[k]);
  755. }
  756. pr_cont("\n");
  757. }
  758. /**
  759. * init_parisc_bus - Some preparation to be done before inventory
  760. */
  761. void init_parisc_bus(void)
  762. {
  763. if (bus_register(&parisc_bus_type))
  764. panic("Could not register PA-RISC bus type\n");
  765. if (device_register(&root))
  766. panic("Could not register PA-RISC root device\n");
  767. get_device(&root);
  768. }
  769. static int print_one_device(struct device * dev, void * data)
  770. {
  771. struct parisc_device * pdev = to_parisc_device(dev);
  772. if (check_dev(dev))
  773. print_parisc_device(pdev);
  774. return 0;
  775. }
  776. /**
  777. * print_parisc_devices - Print out a list of devices found in this system
  778. */
  779. void print_parisc_devices(void)
  780. {
  781. for_each_padev(print_one_device, NULL);
  782. }