macio_asic.c 21 KB

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