drivers.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 hppa_dma_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. #ifdef CONFIG_PCI
  240. static inline int is_pci_dev(struct device *dev)
  241. {
  242. return dev->bus == &pci_bus_type;
  243. }
  244. #else
  245. static inline int is_pci_dev(struct device *dev)
  246. {
  247. return 0;
  248. }
  249. #endif
  250. /*
  251. * get_node_path fills in @path with the firmware path to the device.
  252. * Note that if @node is a parisc device, we don't fill in the 'mod' field.
  253. * This is because both callers pass the parent and fill in the mod
  254. * themselves. If @node is a PCI device, we do fill it in, even though this
  255. * is inconsistent.
  256. */
  257. static void get_node_path(struct device *dev, struct hardware_path *path)
  258. {
  259. int i = 5;
  260. memset(&path->bc, -1, 6);
  261. if (is_pci_dev(dev)) {
  262. unsigned int devfn = to_pci_dev(dev)->devfn;
  263. path->mod = PCI_FUNC(devfn);
  264. path->bc[i--] = PCI_SLOT(devfn);
  265. dev = dev->parent;
  266. }
  267. while (dev != &root) {
  268. if (is_pci_dev(dev)) {
  269. unsigned int devfn = to_pci_dev(dev)->devfn;
  270. path->bc[i--] = PCI_SLOT(devfn) | (PCI_FUNC(devfn)<< 5);
  271. } else if (dev->bus == &parisc_bus_type) {
  272. path->bc[i--] = to_parisc_device(dev)->hw_path;
  273. }
  274. dev = dev->parent;
  275. }
  276. }
  277. static char *print_hwpath(struct hardware_path *path, char *output)
  278. {
  279. int i;
  280. for (i = 0; i < 6; i++) {
  281. if (path->bc[i] == -1)
  282. continue;
  283. output += sprintf(output, "%u/", (unsigned char) path->bc[i]);
  284. }
  285. output += sprintf(output, "%u", (unsigned char) path->mod);
  286. return output;
  287. }
  288. /**
  289. * print_pa_hwpath - Returns hardware path for PA devices
  290. * dev: The device to return the path for
  291. * output: Pointer to a previously-allocated array to place the path in.
  292. *
  293. * This function fills in the output array with a human-readable path
  294. * to a PA device. This string is compatible with that used by PDC, and
  295. * may be printed on the outside of the box.
  296. */
  297. char *print_pa_hwpath(struct parisc_device *dev, char *output)
  298. {
  299. struct hardware_path path;
  300. get_node_path(dev->dev.parent, &path);
  301. path.mod = dev->hw_path;
  302. return print_hwpath(&path, output);
  303. }
  304. EXPORT_SYMBOL(print_pa_hwpath);
  305. #if defined(CONFIG_PCI) || defined(CONFIG_ISA)
  306. /**
  307. * get_pci_node_path - Determines the hardware path for a PCI device
  308. * @pdev: The device to return the path for
  309. * @path: Pointer to a previously-allocated array to place the path in.
  310. *
  311. * This function fills in the hardware_path structure with the route to
  312. * the specified PCI device. This structure is suitable for passing to
  313. * PDC calls.
  314. */
  315. void get_pci_node_path(struct pci_dev *pdev, struct hardware_path *path)
  316. {
  317. get_node_path(&pdev->dev, path);
  318. }
  319. EXPORT_SYMBOL(get_pci_node_path);
  320. /**
  321. * print_pci_hwpath - Returns hardware path for PCI devices
  322. * dev: The device to return the path for
  323. * output: Pointer to a previously-allocated array to place the path in.
  324. *
  325. * This function fills in the output array with a human-readable path
  326. * to a PCI device. This string is compatible with that used by PDC, and
  327. * may be printed on the outside of the box.
  328. */
  329. char *print_pci_hwpath(struct pci_dev *dev, char *output)
  330. {
  331. struct hardware_path path;
  332. get_pci_node_path(dev, &path);
  333. return print_hwpath(&path, output);
  334. }
  335. EXPORT_SYMBOL(print_pci_hwpath);
  336. #endif /* defined(CONFIG_PCI) || defined(CONFIG_ISA) */
  337. static void setup_bus_id(struct parisc_device *padev)
  338. {
  339. struct hardware_path path;
  340. char name[20];
  341. char *output = name;
  342. int i;
  343. get_node_path(padev->dev.parent, &path);
  344. for (i = 0; i < 6; i++) {
  345. if (path.bc[i] == -1)
  346. continue;
  347. output += sprintf(output, "%u:", (unsigned char) path.bc[i]);
  348. }
  349. sprintf(output, "%u", (unsigned char) padev->hw_path);
  350. dev_set_name(&padev->dev, name);
  351. }
  352. struct parisc_device * create_tree_node(char id, struct device *parent)
  353. {
  354. struct parisc_device *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  355. if (!dev)
  356. return NULL;
  357. dev->hw_path = id;
  358. dev->id.hw_type = HPHW_FAULTY;
  359. dev->dev.parent = parent;
  360. setup_bus_id(dev);
  361. dev->dev.bus = &parisc_bus_type;
  362. dev->dma_mask = 0xffffffffUL; /* PARISC devices are 32-bit */
  363. /* make the generic dma mask a pointer to the parisc one */
  364. dev->dev.dma_mask = &dev->dma_mask;
  365. dev->dev.coherent_dma_mask = dev->dma_mask;
  366. if (device_register(&dev->dev)) {
  367. kfree(dev);
  368. return NULL;
  369. }
  370. return dev;
  371. }
  372. struct match_id_data {
  373. char id;
  374. struct parisc_device * dev;
  375. };
  376. static int match_by_id(struct device * dev, void * data)
  377. {
  378. struct parisc_device * pdev = to_parisc_device(dev);
  379. struct match_id_data * d = data;
  380. if (pdev->hw_path == d->id) {
  381. d->dev = pdev;
  382. return 1;
  383. }
  384. return 0;
  385. }
  386. /**
  387. * alloc_tree_node - returns a device entry in the iotree
  388. * @parent: the parent node in the tree
  389. * @id: the element of the module path for this entry
  390. *
  391. * Checks all the children of @parent for a matching @id. If none
  392. * found, it allocates a new device and returns it.
  393. */
  394. static struct parisc_device * alloc_tree_node(struct device *parent, char id)
  395. {
  396. struct match_id_data d = {
  397. .id = id,
  398. };
  399. if (device_for_each_child(parent, &d, match_by_id))
  400. return d.dev;
  401. else
  402. return create_tree_node(id, parent);
  403. }
  404. static struct parisc_device *create_parisc_device(struct hardware_path *modpath)
  405. {
  406. int i;
  407. struct device *parent = &root;
  408. for (i = 0; i < 6; i++) {
  409. if (modpath->bc[i] == -1)
  410. continue;
  411. parent = &alloc_tree_node(parent, modpath->bc[i])->dev;
  412. }
  413. return alloc_tree_node(parent, modpath->mod);
  414. }
  415. struct parisc_device *
  416. alloc_pa_dev(unsigned long hpa, struct hardware_path *mod_path)
  417. {
  418. int status;
  419. unsigned long bytecnt;
  420. u8 iodc_data[32];
  421. struct parisc_device *dev;
  422. const char *name;
  423. /* Check to make sure this device has not already been added - Ryan */
  424. if (find_device_by_addr(hpa) != NULL)
  425. return NULL;
  426. status = pdc_iodc_read(&bytecnt, hpa, 0, &iodc_data, 32);
  427. if (status != PDC_OK)
  428. return NULL;
  429. dev = create_parisc_device(mod_path);
  430. if (dev->id.hw_type != HPHW_FAULTY) {
  431. printk(KERN_ERR "Two devices have hardware path [%s]. "
  432. "IODC data for second device: "
  433. "%02x%02x%02x%02x%02x%02x\n"
  434. "Rearranging GSC cards sometimes helps\n",
  435. parisc_pathname(dev), iodc_data[0], iodc_data[1],
  436. iodc_data[3], iodc_data[4], iodc_data[5], iodc_data[6]);
  437. return NULL;
  438. }
  439. dev->id.hw_type = iodc_data[3] & 0x1f;
  440. dev->id.hversion = (iodc_data[0] << 4) | ((iodc_data[1] & 0xf0) >> 4);
  441. dev->id.hversion_rev = iodc_data[1] & 0x0f;
  442. dev->id.sversion = ((iodc_data[4] & 0x0f) << 16) |
  443. (iodc_data[5] << 8) | iodc_data[6];
  444. dev->hpa.name = parisc_pathname(dev);
  445. dev->hpa.start = hpa;
  446. /* This is awkward. The STI spec says that gfx devices may occupy
  447. * 32MB or 64MB. Unfortunately, we don't know how to tell whether
  448. * it's the former or the latter. Assumptions either way can hurt us.
  449. */
  450. if (hpa == 0xf4000000 || hpa == 0xf8000000) {
  451. dev->hpa.end = hpa + 0x03ffffff;
  452. } else if (hpa == 0xf6000000 || hpa == 0xfa000000) {
  453. dev->hpa.end = hpa + 0x01ffffff;
  454. } else {
  455. dev->hpa.end = hpa + 0xfff;
  456. }
  457. dev->hpa.flags = IORESOURCE_MEM;
  458. name = parisc_hardware_description(&dev->id);
  459. if (name) {
  460. strlcpy(dev->name, name, sizeof(dev->name));
  461. }
  462. /* Silently fail things like mouse ports which are subsumed within
  463. * the keyboard controller
  464. */
  465. if ((hpa & 0xfff) == 0 && insert_resource(&iomem_resource, &dev->hpa))
  466. printk("Unable to claim HPA %lx for device %s\n",
  467. hpa, name);
  468. return dev;
  469. }
  470. static int parisc_generic_match(struct device *dev, struct device_driver *drv)
  471. {
  472. return match_device(to_parisc_driver(drv), to_parisc_device(dev));
  473. }
  474. static ssize_t make_modalias(struct device *dev, char *buf)
  475. {
  476. const struct parisc_device *padev = to_parisc_device(dev);
  477. const struct parisc_device_id *id = &padev->id;
  478. return sprintf(buf, "parisc:t%02Xhv%04Xrev%02Xsv%08X\n",
  479. (u8)id->hw_type, (u16)id->hversion, (u8)id->hversion_rev,
  480. (u32)id->sversion);
  481. }
  482. static int parisc_uevent(struct device *dev, struct kobj_uevent_env *env)
  483. {
  484. const struct parisc_device *padev;
  485. char modalias[40];
  486. if (!dev)
  487. return -ENODEV;
  488. padev = to_parisc_device(dev);
  489. if (!padev)
  490. return -ENODEV;
  491. if (add_uevent_var(env, "PARISC_NAME=%s", padev->name))
  492. return -ENOMEM;
  493. make_modalias(dev, modalias);
  494. if (add_uevent_var(env, "MODALIAS=%s", modalias))
  495. return -ENOMEM;
  496. return 0;
  497. }
  498. #define pa_dev_attr(name, field, format_string) \
  499. static ssize_t name##_show(struct device *dev, struct device_attribute *attr, char *buf) \
  500. { \
  501. struct parisc_device *padev = to_parisc_device(dev); \
  502. return sprintf(buf, format_string, padev->field); \
  503. }
  504. #define pa_dev_attr_id(field, format) pa_dev_attr(field, id.field, format)
  505. pa_dev_attr(irq, irq, "%u\n");
  506. pa_dev_attr_id(hw_type, "0x%02x\n");
  507. pa_dev_attr(rev, id.hversion_rev, "0x%x\n");
  508. pa_dev_attr_id(hversion, "0x%03x\n");
  509. pa_dev_attr_id(sversion, "0x%05x\n");
  510. static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
  511. {
  512. return make_modalias(dev, buf);
  513. }
  514. static struct device_attribute parisc_device_attrs[] = {
  515. __ATTR_RO(irq),
  516. __ATTR_RO(hw_type),
  517. __ATTR_RO(rev),
  518. __ATTR_RO(hversion),
  519. __ATTR_RO(sversion),
  520. __ATTR_RO(modalias),
  521. __ATTR_NULL,
  522. };
  523. struct bus_type parisc_bus_type = {
  524. .name = "parisc",
  525. .match = parisc_generic_match,
  526. .uevent = parisc_uevent,
  527. .dev_attrs = parisc_device_attrs,
  528. .probe = parisc_driver_probe,
  529. .remove = parisc_driver_remove,
  530. };
  531. /**
  532. * register_parisc_device - Locate a driver to manage this device.
  533. * @dev: The parisc device.
  534. *
  535. * Search the driver list for a driver that is willing to manage
  536. * this device.
  537. */
  538. int register_parisc_device(struct parisc_device *dev)
  539. {
  540. if (!dev)
  541. return 0;
  542. if (dev->driver)
  543. return 1;
  544. return 0;
  545. }
  546. /**
  547. * match_pci_device - Matches a pci device against a given hardware path
  548. * entry.
  549. * @dev: the generic device (known to be contained by a pci_dev).
  550. * @index: the current BC index
  551. * @modpath: the hardware path.
  552. * @return: true if the device matches the hardware path.
  553. */
  554. static int match_pci_device(struct device *dev, int index,
  555. struct hardware_path *modpath)
  556. {
  557. struct pci_dev *pdev = to_pci_dev(dev);
  558. int id;
  559. if (index == 5) {
  560. /* we are at the end of the path, and on the actual device */
  561. unsigned int devfn = pdev->devfn;
  562. return ((modpath->bc[5] == PCI_SLOT(devfn)) &&
  563. (modpath->mod == PCI_FUNC(devfn)));
  564. }
  565. id = PCI_SLOT(pdev->devfn) | (PCI_FUNC(pdev->devfn) << 5);
  566. return (modpath->bc[index] == id);
  567. }
  568. /**
  569. * match_parisc_device - Matches a parisc device against a given hardware
  570. * path entry.
  571. * @dev: the generic device (known to be contained by a parisc_device).
  572. * @index: the current BC index
  573. * @modpath: the hardware path.
  574. * @return: true if the device matches the hardware path.
  575. */
  576. static int match_parisc_device(struct device *dev, int index,
  577. struct hardware_path *modpath)
  578. {
  579. struct parisc_device *curr = to_parisc_device(dev);
  580. char id = (index == 6) ? modpath->mod : modpath->bc[index];
  581. return (curr->hw_path == id);
  582. }
  583. struct parse_tree_data {
  584. int index;
  585. struct hardware_path * modpath;
  586. struct device * dev;
  587. };
  588. static int check_parent(struct device * dev, void * data)
  589. {
  590. struct parse_tree_data * d = data;
  591. if (check_dev(dev)) {
  592. if (dev->bus == &parisc_bus_type) {
  593. if (match_parisc_device(dev, d->index, d->modpath))
  594. d->dev = dev;
  595. } else if (is_pci_dev(dev)) {
  596. if (match_pci_device(dev, d->index, d->modpath))
  597. d->dev = dev;
  598. } else if (dev->bus == NULL) {
  599. /* we are on a bus bridge */
  600. struct device *new = parse_tree_node(dev, d->index, d->modpath);
  601. if (new)
  602. d->dev = new;
  603. }
  604. }
  605. return d->dev != NULL;
  606. }
  607. /**
  608. * parse_tree_node - returns a device entry in the iotree
  609. * @parent: the parent node in the tree
  610. * @index: the current BC index
  611. * @modpath: the hardware_path struct to match a device against
  612. * @return: The corresponding device if found, NULL otherwise.
  613. *
  614. * Checks all the children of @parent for a matching @id. If none
  615. * found, it returns NULL.
  616. */
  617. static struct device *
  618. parse_tree_node(struct device *parent, int index, struct hardware_path *modpath)
  619. {
  620. struct parse_tree_data d = {
  621. .index = index,
  622. .modpath = modpath,
  623. };
  624. struct recurse_struct recurse_data = {
  625. .obj = &d,
  626. .fn = check_parent,
  627. };
  628. if (device_for_each_child(parent, &recurse_data, descend_children))
  629. /* nothing */;
  630. return d.dev;
  631. }
  632. /**
  633. * hwpath_to_device - Finds the generic device corresponding to a given hardware path.
  634. * @modpath: the hardware path.
  635. * @return: The target device, NULL if not found.
  636. */
  637. struct device *hwpath_to_device(struct hardware_path *modpath)
  638. {
  639. int i;
  640. struct device *parent = &root;
  641. for (i = 0; i < 6; i++) {
  642. if (modpath->bc[i] == -1)
  643. continue;
  644. parent = parse_tree_node(parent, i, modpath);
  645. if (!parent)
  646. return NULL;
  647. }
  648. if (is_pci_dev(parent)) /* pci devices already parse MOD */
  649. return parent;
  650. else
  651. return parse_tree_node(parent, 6, modpath);
  652. }
  653. EXPORT_SYMBOL(hwpath_to_device);
  654. /**
  655. * device_to_hwpath - Populates the hwpath corresponding to the given device.
  656. * @param dev the target device
  657. * @param path pointer to a previously allocated hwpath struct to be filled in
  658. */
  659. void device_to_hwpath(struct device *dev, struct hardware_path *path)
  660. {
  661. struct parisc_device *padev;
  662. if (dev->bus == &parisc_bus_type) {
  663. padev = to_parisc_device(dev);
  664. get_node_path(dev->parent, path);
  665. path->mod = padev->hw_path;
  666. } else if (is_pci_dev(dev)) {
  667. get_node_path(dev, path);
  668. }
  669. }
  670. EXPORT_SYMBOL(device_to_hwpath);
  671. #define BC_PORT_MASK 0x8
  672. #define BC_LOWER_PORT 0x8
  673. #define BUS_CONVERTER(dev) \
  674. ((dev->id.hw_type == HPHW_IOA) || (dev->id.hw_type == HPHW_BCPORT))
  675. #define IS_LOWER_PORT(dev) \
  676. ((gsc_readl(dev->hpa.start + offsetof(struct bc_module, io_status)) \
  677. & BC_PORT_MASK) == BC_LOWER_PORT)
  678. #define MAX_NATIVE_DEVICES 64
  679. #define NATIVE_DEVICE_OFFSET 0x1000
  680. #define FLEX_MASK F_EXTEND(0xfffc0000)
  681. #define IO_IO_LOW offsetof(struct bc_module, io_io_low)
  682. #define IO_IO_HIGH offsetof(struct bc_module, io_io_high)
  683. #define READ_IO_IO_LOW(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_LOW)
  684. #define READ_IO_IO_HIGH(dev) (unsigned long)(signed int)gsc_readl(dev->hpa.start + IO_IO_HIGH)
  685. static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
  686. struct device *parent);
  687. void walk_lower_bus(struct parisc_device *dev)
  688. {
  689. unsigned long io_io_low, io_io_high;
  690. if (!BUS_CONVERTER(dev) || IS_LOWER_PORT(dev))
  691. return;
  692. if (dev->id.hw_type == HPHW_IOA) {
  693. io_io_low = (unsigned long)(signed int)(READ_IO_IO_LOW(dev) << 16);
  694. io_io_high = io_io_low + MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET;
  695. } else {
  696. io_io_low = (READ_IO_IO_LOW(dev) + ~FLEX_MASK) & FLEX_MASK;
  697. io_io_high = (READ_IO_IO_HIGH(dev)+ ~FLEX_MASK) & FLEX_MASK;
  698. }
  699. walk_native_bus(io_io_low, io_io_high, &dev->dev);
  700. }
  701. /**
  702. * walk_native_bus -- Probe a bus for devices
  703. * @io_io_low: Base address of this bus.
  704. * @io_io_high: Last address of this bus.
  705. * @parent: The parent bus device.
  706. *
  707. * A native bus (eg Runway or GSC) may have up to 64 devices on it,
  708. * spaced at intervals of 0x1000 bytes. PDC may not inform us of these
  709. * devices, so we have to probe for them. Unfortunately, we may find
  710. * devices which are not physically connected (such as extra serial &
  711. * keyboard ports). This problem is not yet solved.
  712. */
  713. static void walk_native_bus(unsigned long io_io_low, unsigned long io_io_high,
  714. struct device *parent)
  715. {
  716. int i, devices_found = 0;
  717. unsigned long hpa = io_io_low;
  718. struct hardware_path path;
  719. get_node_path(parent, &path);
  720. do {
  721. for(i = 0; i < MAX_NATIVE_DEVICES; i++, hpa += NATIVE_DEVICE_OFFSET) {
  722. struct parisc_device *dev;
  723. /* Was the device already added by Firmware? */
  724. dev = find_device_by_addr(hpa);
  725. if (!dev) {
  726. path.mod = i;
  727. dev = alloc_pa_dev(hpa, &path);
  728. if (!dev)
  729. continue;
  730. register_parisc_device(dev);
  731. devices_found++;
  732. }
  733. walk_lower_bus(dev);
  734. }
  735. } while(!devices_found && hpa < io_io_high);
  736. }
  737. #define CENTRAL_BUS_ADDR F_EXTEND(0xfff80000)
  738. /**
  739. * walk_central_bus - Find devices attached to the central bus
  740. *
  741. * PDC doesn't tell us about all devices in the system. This routine
  742. * finds devices connected to the central bus.
  743. */
  744. void walk_central_bus(void)
  745. {
  746. walk_native_bus(CENTRAL_BUS_ADDR,
  747. CENTRAL_BUS_ADDR + (MAX_NATIVE_DEVICES * NATIVE_DEVICE_OFFSET),
  748. &root);
  749. }
  750. static void print_parisc_device(struct parisc_device *dev)
  751. {
  752. char hw_path[64];
  753. static int count;
  754. print_pa_hwpath(dev, hw_path);
  755. printk(KERN_INFO "%d. %s at 0x%p [%s] { %d, 0x%x, 0x%.3x, 0x%.5x }",
  756. ++count, dev->name, (void*) dev->hpa.start, hw_path, dev->id.hw_type,
  757. dev->id.hversion_rev, dev->id.hversion, dev->id.sversion);
  758. if (dev->num_addrs) {
  759. int k;
  760. printk(", additional addresses: ");
  761. for (k = 0; k < dev->num_addrs; k++)
  762. printk("0x%lx ", dev->addr[k]);
  763. }
  764. printk("\n");
  765. }
  766. /**
  767. * init_parisc_bus - Some preparation to be done before inventory
  768. */
  769. void init_parisc_bus(void)
  770. {
  771. if (bus_register(&parisc_bus_type))
  772. panic("Could not register PA-RISC bus type\n");
  773. if (device_register(&root))
  774. panic("Could not register PA-RISC root device\n");
  775. get_device(&root);
  776. }
  777. static int print_one_device(struct device * dev, void * data)
  778. {
  779. struct parisc_device * pdev = to_parisc_device(dev);
  780. if (check_dev(dev))
  781. print_parisc_device(pdev);
  782. return 0;
  783. }
  784. /**
  785. * print_parisc_devices - Print out a list of devices found in this system
  786. */
  787. void print_parisc_devices(void)
  788. {
  789. for_each_padev(print_one_device, NULL);
  790. }