macio_asic.c 20 KB

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