macio_asic.c 20 KB

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