macio_asic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Bus & driver management routines for devices within
  3. * a MacIO ASIC. Interface to new driver model mostly
  4. * stolen from the PCI version.
  5. *
  6. * Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * TODO:
  14. *
  15. * - Don't probe below media bay by default, but instead provide
  16. * some hooks for media bay to dynamically add/remove it's own
  17. * sub-devices.
  18. */
  19. #include <linux/string.h>
  20. #include <linux/kernel.h>
  21. #include <linux/pci.h>
  22. #include <linux/pci_ids.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/of_address.h>
  27. #include <linux/of_irq.h>
  28. #include <asm/machdep.h>
  29. #include <asm/macio.h>
  30. #include <asm/pmac_feature.h>
  31. #include <asm/prom.h>
  32. #undef DEBUG
  33. #define MAX_NODE_NAME_SIZE (20 - 12)
  34. static struct macio_chip *macio_on_hold;
  35. static int macio_bus_match(struct device *dev, struct device_driver *drv)
  36. {
  37. const struct of_device_id * matches = drv->of_match_table;
  38. if (!matches)
  39. return 0;
  40. return of_match_device(matches, dev) != NULL;
  41. }
  42. struct macio_dev *macio_dev_get(struct macio_dev *dev)
  43. {
  44. struct device *tmp;
  45. if (!dev)
  46. return NULL;
  47. tmp = get_device(&dev->ofdev.dev);
  48. if (tmp)
  49. return to_macio_device(tmp);
  50. else
  51. return NULL;
  52. }
  53. void macio_dev_put(struct macio_dev *dev)
  54. {
  55. if (dev)
  56. put_device(&dev->ofdev.dev);
  57. }
  58. static int macio_device_probe(struct device *dev)
  59. {
  60. int error = -ENODEV;
  61. struct macio_driver *drv;
  62. struct macio_dev *macio_dev;
  63. const struct of_device_id *match;
  64. drv = to_macio_driver(dev->driver);
  65. macio_dev = to_macio_device(dev);
  66. if (!drv->probe)
  67. return error;
  68. macio_dev_get(macio_dev);
  69. match = of_match_device(drv->driver.of_match_table, dev);
  70. if (match)
  71. error = drv->probe(macio_dev, match);
  72. if (error)
  73. macio_dev_put(macio_dev);
  74. return error;
  75. }
  76. static int macio_device_remove(struct device *dev)
  77. {
  78. struct macio_dev * macio_dev = to_macio_device(dev);
  79. struct macio_driver * drv = to_macio_driver(dev->driver);
  80. if (dev->driver && drv->remove)
  81. drv->remove(macio_dev);
  82. macio_dev_put(macio_dev);
  83. return 0;
  84. }
  85. static void macio_device_shutdown(struct device *dev)
  86. {
  87. struct macio_dev * macio_dev = to_macio_device(dev);
  88. struct macio_driver * drv = to_macio_driver(dev->driver);
  89. if (dev->driver && drv->shutdown)
  90. drv->shutdown(macio_dev);
  91. }
  92. static int macio_device_suspend(struct device *dev, pm_message_t state)
  93. {
  94. struct macio_dev * macio_dev = to_macio_device(dev);
  95. struct macio_driver * drv = to_macio_driver(dev->driver);
  96. if (dev->driver && drv->suspend)
  97. return drv->suspend(macio_dev, state);
  98. return 0;
  99. }
  100. static int macio_device_resume(struct device * dev)
  101. {
  102. struct macio_dev * macio_dev = to_macio_device(dev);
  103. struct macio_driver * drv = to_macio_driver(dev->driver);
  104. if (dev->driver && drv->resume)
  105. return drv->resume(macio_dev);
  106. return 0;
  107. }
  108. extern struct device_attribute macio_dev_attrs[];
  109. struct bus_type macio_bus_type = {
  110. .name = "macio",
  111. .match = macio_bus_match,
  112. .uevent = of_device_uevent_modalias,
  113. .probe = macio_device_probe,
  114. .remove = macio_device_remove,
  115. .shutdown = macio_device_shutdown,
  116. .suspend = macio_device_suspend,
  117. .resume = macio_device_resume,
  118. .dev_attrs = macio_dev_attrs,
  119. };
  120. static int __init macio_bus_driver_init(void)
  121. {
  122. return bus_register(&macio_bus_type);
  123. }
  124. postcore_initcall(macio_bus_driver_init);
  125. /**
  126. * macio_release_dev - free a macio device structure when all users of it are
  127. * finished.
  128. * @dev: device that's been disconnected
  129. *
  130. * Will be called only by the device core when all users of this macio device
  131. * are done. This currently means never as we don't hot remove any macio
  132. * device yet, though that will happen with mediabay based devices in a later
  133. * implementation.
  134. */
  135. static void macio_release_dev(struct device *dev)
  136. {
  137. struct macio_dev *mdev;
  138. mdev = to_macio_device(dev);
  139. kfree(mdev);
  140. }
  141. /**
  142. * macio_resource_quirks - tweak or skip some resources for a device
  143. * @np: pointer to the device node
  144. * @res: resulting resource
  145. * @index: index of resource in node
  146. *
  147. * If this routine returns non-null, then the resource is completely
  148. * skipped.
  149. */
  150. static int macio_resource_quirks(struct device_node *np, struct resource *res,
  151. int index)
  152. {
  153. /* Only quirks for memory resources for now */
  154. if ((res->flags & IORESOURCE_MEM) == 0)
  155. return 0;
  156. /* Grand Central has too large resource 0 on some machines */
  157. if (index == 0 && !strcmp(np->name, "gc"))
  158. res->end = res->start + 0x1ffff;
  159. /* Airport has bogus resource 2 */
  160. if (index >= 2 && !strcmp(np->name, "radio"))
  161. return 1;
  162. #ifndef CONFIG_PPC64
  163. /* DBDMAs may have bogus sizes */
  164. if ((res->start & 0x0001f000) == 0x00008000)
  165. res->end = res->start + 0xff;
  166. #endif /* CONFIG_PPC64 */
  167. /* ESCC parent eats child resources. We could have added a
  168. * level of hierarchy, but I don't really feel the need
  169. * for it
  170. */
  171. if (!strcmp(np->name, "escc"))
  172. return 1;
  173. /* ESCC has bogus resources >= 3 */
  174. if (index >= 3 && !(strcmp(np->name, "ch-a") &&
  175. strcmp(np->name, "ch-b")))
  176. return 1;
  177. /* Media bay has too many resources, keep only first one */
  178. if (index > 0 && !strcmp(np->name, "media-bay"))
  179. return 1;
  180. /* Some older IDE resources have bogus sizes */
  181. if (!(strcmp(np->name, "IDE") && strcmp(np->name, "ATA") &&
  182. strcmp(np->type, "ide") && strcmp(np->type, "ata"))) {
  183. if (index == 0 && (res->end - res->start) > 0xfff)
  184. res->end = res->start + 0xfff;
  185. if (index == 1 && (res->end - res->start) > 0xff)
  186. res->end = res->start + 0xff;
  187. }
  188. return 0;
  189. }
  190. static void macio_create_fixup_irq(struct macio_dev *dev, int index,
  191. unsigned int line)
  192. {
  193. unsigned int irq;
  194. irq = irq_create_mapping(NULL, line);
  195. if (!irq) {
  196. dev->interrupt[index].start = irq;
  197. dev->interrupt[index].flags = IORESOURCE_IRQ;
  198. dev->interrupt[index].name = dev_name(&dev->ofdev.dev);
  199. }
  200. if (dev->n_interrupts <= index)
  201. dev->n_interrupts = index + 1;
  202. }
  203. static void macio_add_missing_resources(struct macio_dev *dev)
  204. {
  205. struct device_node *np = dev->ofdev.dev.of_node;
  206. unsigned int irq_base;
  207. /* Gatwick has some missing interrupts on child nodes */
  208. if (dev->bus->chip->type != macio_gatwick)
  209. return;
  210. /* irq_base is always 64 on gatwick. I have no cleaner way to get
  211. * that value from here at this point
  212. */
  213. irq_base = 64;
  214. /* Fix SCC */
  215. if (strcmp(np->name, "ch-a") == 0) {
  216. macio_create_fixup_irq(dev, 0, 15 + irq_base);
  217. macio_create_fixup_irq(dev, 1, 4 + irq_base);
  218. macio_create_fixup_irq(dev, 2, 5 + irq_base);
  219. printk(KERN_INFO "macio: fixed SCC irqs on gatwick\n");
  220. }
  221. /* Fix media-bay */
  222. if (strcmp(np->name, "media-bay") == 0) {
  223. macio_create_fixup_irq(dev, 0, 29 + irq_base);
  224. printk(KERN_INFO "macio: fixed media-bay irq on gatwick\n");
  225. }
  226. /* Fix left media bay childs */
  227. if (dev->media_bay != NULL && strcmp(np->name, "floppy") == 0) {
  228. macio_create_fixup_irq(dev, 0, 19 + irq_base);
  229. macio_create_fixup_irq(dev, 1, 1 + irq_base);
  230. printk(KERN_INFO "macio: fixed left floppy irqs\n");
  231. }
  232. if (dev->media_bay != NULL && strcasecmp(np->name, "ata4") == 0) {
  233. macio_create_fixup_irq(dev, 0, 14 + irq_base);
  234. macio_create_fixup_irq(dev, 0, 3 + irq_base);
  235. printk(KERN_INFO "macio: fixed left ide irqs\n");
  236. }
  237. }
  238. static void macio_setup_interrupts(struct macio_dev *dev)
  239. {
  240. struct device_node *np = dev->ofdev.dev.of_node;
  241. unsigned int irq;
  242. int i = 0, j = 0;
  243. for (;;) {
  244. struct resource *res;
  245. if (j >= MACIO_DEV_COUNT_IRQS)
  246. break;
  247. res = &dev->interrupt[j];
  248. irq = irq_of_parse_and_map(np, i++);
  249. if (!irq)
  250. break;
  251. res->start = irq;
  252. res->flags = IORESOURCE_IRQ;
  253. res->name = dev_name(&dev->ofdev.dev);
  254. if (macio_resource_quirks(np, res, i - 1)) {
  255. memset(res, 0, sizeof(struct resource));
  256. continue;
  257. } else
  258. j++;
  259. }
  260. dev->n_interrupts = j;
  261. }
  262. static void macio_setup_resources(struct macio_dev *dev,
  263. struct resource *parent_res)
  264. {
  265. struct device_node *np = dev->ofdev.dev.of_node;
  266. struct resource r;
  267. int index;
  268. for (index = 0; of_address_to_resource(np, index, &r) == 0; index++) {
  269. struct resource *res;
  270. if (index >= MACIO_DEV_COUNT_RESOURCES)
  271. break;
  272. res = &dev->resource[index];
  273. *res = r;
  274. res->name = dev_name(&dev->ofdev.dev);
  275. if (macio_resource_quirks(np, res, index)) {
  276. memset(res, 0, sizeof(struct resource));
  277. continue;
  278. }
  279. /* Currently, we consider failure as harmless, this may
  280. * change in the future, once I've found all the device
  281. * tree bugs in older machines & worked around them
  282. */
  283. if (insert_resource(parent_res, res)) {
  284. printk(KERN_WARNING "Can't request resource "
  285. "%d for MacIO device %s\n",
  286. index, dev_name(&dev->ofdev.dev));
  287. }
  288. }
  289. dev->n_resources = index;
  290. }
  291. /**
  292. * macio_add_one_device - Add one device from OF node to the device tree
  293. * @chip: pointer to the macio_chip holding the device
  294. * @np: pointer to the device node in the OF tree
  295. * @in_bay: set to 1 if device is part of a media-bay
  296. *
  297. * When media-bay is changed to hotswap drivers, this function will
  298. * be exposed to the bay driver some way...
  299. */
  300. static struct macio_dev * macio_add_one_device(struct macio_chip *chip,
  301. struct device *parent,
  302. struct device_node *np,
  303. struct macio_dev *in_bay,
  304. struct resource *parent_res)
  305. {
  306. struct macio_dev *dev;
  307. const u32 *reg;
  308. if (np == NULL)
  309. return NULL;
  310. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  311. if (!dev)
  312. return NULL;
  313. dev->bus = &chip->lbus;
  314. dev->media_bay = in_bay;
  315. dev->ofdev.dev.of_node = np;
  316. dev->ofdev.archdata.dma_mask = 0xffffffffUL;
  317. dev->ofdev.dev.dma_mask = &dev->ofdev.archdata.dma_mask;
  318. dev->ofdev.dev.parent = parent;
  319. dev->ofdev.dev.bus = &macio_bus_type;
  320. dev->ofdev.dev.release = macio_release_dev;
  321. dev->ofdev.dev.dma_parms = &dev->dma_parms;
  322. /* Standard DMA paremeters */
  323. dma_set_max_seg_size(&dev->ofdev.dev, 65536);
  324. dma_set_seg_boundary(&dev->ofdev.dev, 0xffffffff);
  325. #ifdef CONFIG_PCI
  326. /* Set the DMA ops to the ones from the PCI device, this could be
  327. * fishy if we didn't know that on PowerMac it's always direct ops
  328. * or iommu ops that will work fine
  329. *
  330. * To get all the fields, copy all archdata
  331. */
  332. dev->ofdev.dev.archdata = chip->lbus.pdev->dev.archdata;
  333. #endif /* CONFIG_PCI */
  334. #ifdef DEBUG
  335. printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
  336. dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj);
  337. #endif
  338. /* MacIO itself has a different reg, we use it's PCI base */
  339. if (np == chip->of_node) {
  340. dev_set_name(&dev->ofdev.dev, "%1d.%08x:%.*s",
  341. chip->lbus.index,
  342. #ifdef CONFIG_PCI
  343. (unsigned int)pci_resource_start(chip->lbus.pdev, 0),
  344. #else
  345. 0, /* NuBus may want to do something better here */
  346. #endif
  347. MAX_NODE_NAME_SIZE, np->name);
  348. } else {
  349. reg = of_get_property(np, "reg", NULL);
  350. dev_set_name(&dev->ofdev.dev, "%1d.%08x:%.*s",
  351. chip->lbus.index,
  352. reg ? *reg : 0, MAX_NODE_NAME_SIZE, np->name);
  353. }
  354. /* Setup interrupts & resources */
  355. macio_setup_interrupts(dev);
  356. macio_setup_resources(dev, parent_res);
  357. macio_add_missing_resources(dev);
  358. /* Register with core */
  359. if (of_device_register(&dev->ofdev) != 0) {
  360. printk(KERN_DEBUG"macio: device registration error for %s!\n",
  361. dev_name(&dev->ofdev.dev));
  362. kfree(dev);
  363. return NULL;
  364. }
  365. return dev;
  366. }
  367. static int macio_skip_device(struct device_node *np)
  368. {
  369. if (strncmp(np->name, "battery", 7) == 0)
  370. return 1;
  371. if (strncmp(np->name, "escc-legacy", 11) == 0)
  372. return 1;
  373. return 0;
  374. }
  375. /**
  376. * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree
  377. * @chip: pointer to the macio_chip holding the devices
  378. *
  379. * This function will do the job of extracting devices from the
  380. * Open Firmware device tree, build macio_dev structures and add
  381. * them to the Linux device tree.
  382. *
  383. * For now, childs of media-bay are added now as well. This will
  384. * change rsn though.
  385. */
  386. static void macio_pci_add_devices(struct macio_chip *chip)
  387. {
  388. struct device_node *np, *pnode;
  389. struct macio_dev *rdev, *mdev, *mbdev = NULL, *sdev = NULL;
  390. struct device *parent = NULL;
  391. struct resource *root_res = &iomem_resource;
  392. /* Add a node for the macio bus itself */
  393. #ifdef CONFIG_PCI
  394. if (chip->lbus.pdev) {
  395. parent = &chip->lbus.pdev->dev;
  396. root_res = &chip->lbus.pdev->resource[0];
  397. }
  398. #endif
  399. pnode = of_node_get(chip->of_node);
  400. if (pnode == NULL)
  401. return;
  402. /* Add macio itself to hierarchy */
  403. rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res);
  404. if (rdev == NULL)
  405. return;
  406. root_res = &rdev->resource[0];
  407. /* First scan 1st level */
  408. for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
  409. if (macio_skip_device(np))
  410. continue;
  411. of_node_get(np);
  412. mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL,
  413. root_res);
  414. if (mdev == NULL)
  415. of_node_put(np);
  416. else if (strncmp(np->name, "media-bay", 9) == 0)
  417. mbdev = mdev;
  418. else if (strncmp(np->name, "escc", 4) == 0)
  419. sdev = mdev;
  420. }
  421. /* Add media bay devices if any */
  422. if (mbdev) {
  423. pnode = mbdev->ofdev.dev.of_node;
  424. for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
  425. if (macio_skip_device(np))
  426. continue;
  427. of_node_get(np);
  428. if (macio_add_one_device(chip, &mbdev->ofdev.dev, np,
  429. mbdev, root_res) == NULL)
  430. of_node_put(np);
  431. }
  432. }
  433. /* Add serial ports if any */
  434. if (sdev) {
  435. pnode = sdev->ofdev.dev.of_node;
  436. for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
  437. if (macio_skip_device(np))
  438. continue;
  439. of_node_get(np);
  440. if (macio_add_one_device(chip, &sdev->ofdev.dev, np,
  441. NULL, root_res) == NULL)
  442. of_node_put(np);
  443. }
  444. }
  445. }
  446. /**
  447. * macio_register_driver - Registers a new MacIO device driver
  448. * @drv: pointer to the driver definition structure
  449. */
  450. int macio_register_driver(struct macio_driver *drv)
  451. {
  452. /* initialize common driver fields */
  453. drv->driver.bus = &macio_bus_type;
  454. /* register with core */
  455. return driver_register(&drv->driver);
  456. }
  457. /**
  458. * macio_unregister_driver - Unregisters a new MacIO device driver
  459. * @drv: pointer to the driver definition structure
  460. */
  461. void macio_unregister_driver(struct macio_driver *drv)
  462. {
  463. driver_unregister(&drv->driver);
  464. }
  465. /* Managed MacIO resources */
  466. struct macio_devres {
  467. u32 res_mask;
  468. };
  469. static void maciom_release(struct device *gendev, void *res)
  470. {
  471. struct macio_dev *dev = to_macio_device(gendev);
  472. struct macio_devres *dr = res;
  473. int i, max;
  474. max = min(dev->n_resources, 32);
  475. for (i = 0; i < max; i++) {
  476. if (dr->res_mask & (1 << i))
  477. macio_release_resource(dev, i);
  478. }
  479. }
  480. int macio_enable_devres(struct macio_dev *dev)
  481. {
  482. struct macio_devres *dr;
  483. dr = devres_find(&dev->ofdev.dev, maciom_release, NULL, NULL);
  484. if (!dr) {
  485. dr = devres_alloc(maciom_release, sizeof(*dr), GFP_KERNEL);
  486. if (!dr)
  487. return -ENOMEM;
  488. }
  489. return devres_get(&dev->ofdev.dev, dr, NULL, NULL) != NULL;
  490. }
  491. static struct macio_devres * find_macio_dr(struct macio_dev *dev)
  492. {
  493. return devres_find(&dev->ofdev.dev, maciom_release, NULL, NULL);
  494. }
  495. /**
  496. * macio_request_resource - Request an MMIO resource
  497. * @dev: pointer to the device holding the resource
  498. * @resource_no: resource number to request
  499. * @name: resource name
  500. *
  501. * Mark memory region number @resource_no associated with MacIO
  502. * device @dev as being reserved by owner @name. Do not access
  503. * any address inside the memory regions unless this call returns
  504. * successfully.
  505. *
  506. * Returns 0 on success, or %EBUSY on error. A warning
  507. * message is also printed on failure.
  508. */
  509. int macio_request_resource(struct macio_dev *dev, int resource_no,
  510. const char *name)
  511. {
  512. struct macio_devres *dr = find_macio_dr(dev);
  513. if (macio_resource_len(dev, resource_no) == 0)
  514. return 0;
  515. if (!request_mem_region(macio_resource_start(dev, resource_no),
  516. macio_resource_len(dev, resource_no),
  517. name))
  518. goto err_out;
  519. if (dr && resource_no < 32)
  520. dr->res_mask |= 1 << resource_no;
  521. return 0;
  522. err_out:
  523. printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx"
  524. " for device %s\n",
  525. resource_no,
  526. macio_resource_len(dev, resource_no),
  527. macio_resource_start(dev, resource_no),
  528. dev_name(&dev->ofdev.dev));
  529. return -EBUSY;
  530. }
  531. /**
  532. * macio_release_resource - Release an MMIO resource
  533. * @dev: pointer to the device holding the resource
  534. * @resource_no: resource number to release
  535. */
  536. void macio_release_resource(struct macio_dev *dev, int resource_no)
  537. {
  538. struct macio_devres *dr = find_macio_dr(dev);
  539. if (macio_resource_len(dev, resource_no) == 0)
  540. return;
  541. release_mem_region(macio_resource_start(dev, resource_no),
  542. macio_resource_len(dev, resource_no));
  543. if (dr && resource_no < 32)
  544. dr->res_mask &= ~(1 << resource_no);
  545. }
  546. /**
  547. * macio_request_resources - Reserve all memory resources
  548. * @dev: MacIO device whose resources are to be reserved
  549. * @name: Name to be associated with resource.
  550. *
  551. * Mark all memory regions associated with MacIO device @dev as
  552. * being reserved by owner @name. Do not access any address inside
  553. * the memory regions unless this call returns successfully.
  554. *
  555. * Returns 0 on success, or %EBUSY on error. A warning
  556. * message is also printed on failure.
  557. */
  558. int macio_request_resources(struct macio_dev *dev, const char *name)
  559. {
  560. int i;
  561. for (i = 0; i < dev->n_resources; i++)
  562. if (macio_request_resource(dev, i, name))
  563. goto err_out;
  564. return 0;
  565. err_out:
  566. while(--i >= 0)
  567. macio_release_resource(dev, i);
  568. return -EBUSY;
  569. }
  570. /**
  571. * macio_release_resources - Release reserved memory resources
  572. * @dev: MacIO device whose resources were previously reserved
  573. */
  574. void macio_release_resources(struct macio_dev *dev)
  575. {
  576. int i;
  577. for (i = 0; i < dev->n_resources; i++)
  578. macio_release_resource(dev, i);
  579. }
  580. #ifdef CONFIG_PCI
  581. static int macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  582. {
  583. struct device_node* np;
  584. struct macio_chip* chip;
  585. if (ent->vendor != PCI_VENDOR_ID_APPLE)
  586. return -ENODEV;
  587. /* Note regarding refcounting: We assume pci_device_to_OF_node() is
  588. * ported to new OF APIs and returns a node with refcount incremented.
  589. */
  590. np = pci_device_to_OF_node(pdev);
  591. if (np == NULL)
  592. return -ENODEV;
  593. /* The above assumption is wrong !!!
  594. * fix that here for now until I fix the arch code
  595. */
  596. of_node_get(np);
  597. /* We also assume that pmac_feature will have done a get() on nodes
  598. * stored in the macio chips array
  599. */
  600. chip = macio_find(np, macio_unknown);
  601. of_node_put(np);
  602. if (chip == NULL)
  603. return -ENODEV;
  604. /* XXX Need locking ??? */
  605. if (chip->lbus.pdev == NULL) {
  606. chip->lbus.pdev = pdev;
  607. chip->lbus.chip = chip;
  608. pci_set_drvdata(pdev, &chip->lbus);
  609. pci_set_master(pdev);
  610. }
  611. printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n",
  612. chip->name);
  613. /*
  614. * HACK ALERT: The WallStreet PowerBook and some OHare based machines
  615. * have 2 macio ASICs. I must probe the "main" one first or IDE
  616. * ordering will be incorrect. So I put on "hold" the second one since
  617. * it seem to appear first on PCI
  618. */
  619. if (chip->type == macio_gatwick || chip->type == macio_ohareII)
  620. if (macio_chips[0].lbus.pdev == NULL) {
  621. macio_on_hold = chip;
  622. return 0;
  623. }
  624. macio_pci_add_devices(chip);
  625. if (macio_on_hold && macio_chips[0].lbus.pdev != NULL) {
  626. macio_pci_add_devices(macio_on_hold);
  627. macio_on_hold = NULL;
  628. }
  629. return 0;
  630. }
  631. static void macio_pci_remove(struct pci_dev* pdev)
  632. {
  633. panic("removing of macio-asic not supported !\n");
  634. }
  635. /*
  636. * MacIO is matched against any Apple ID, it's probe() function
  637. * will then decide wether it applies or not
  638. */
  639. static const struct pci_device_id pci_ids[] = { {
  640. .vendor = PCI_VENDOR_ID_APPLE,
  641. .device = PCI_ANY_ID,
  642. .subvendor = PCI_ANY_ID,
  643. .subdevice = PCI_ANY_ID,
  644. }, { /* end: all zeroes */ }
  645. };
  646. MODULE_DEVICE_TABLE (pci, pci_ids);
  647. /* pci driver glue; this is a "new style" PCI driver module */
  648. static struct pci_driver macio_pci_driver = {
  649. .name = (char *) "macio",
  650. .id_table = pci_ids,
  651. .probe = macio_pci_probe,
  652. .remove = macio_pci_remove,
  653. };
  654. #endif /* CONFIG_PCI */
  655. static int __init macio_module_init (void)
  656. {
  657. #ifdef CONFIG_PCI
  658. int rc;
  659. rc = pci_register_driver(&macio_pci_driver);
  660. if (rc)
  661. return rc;
  662. #endif /* CONFIG_PCI */
  663. return 0;
  664. }
  665. module_init(macio_module_init);
  666. EXPORT_SYMBOL(macio_register_driver);
  667. EXPORT_SYMBOL(macio_unregister_driver);
  668. EXPORT_SYMBOL(macio_dev_get);
  669. EXPORT_SYMBOL(macio_dev_put);
  670. EXPORT_SYMBOL(macio_request_resource);
  671. EXPORT_SYMBOL(macio_release_resource);
  672. EXPORT_SYMBOL(macio_request_resources);
  673. EXPORT_SYMBOL(macio_release_resources);
  674. EXPORT_SYMBOL(macio_enable_devres);