macio_asic.c 21 KB

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