macio_asic.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. * TODO:
  7. *
  8. * - Don't probe below media bay by default, but instead provide
  9. * some hooks for media bay to dynamically add/remove it's own
  10. * sub-devices.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/string.h>
  14. #include <linux/kernel.h>
  15. #include <linux/pci.h>
  16. #include <linux/pci_ids.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <asm/machdep.h>
  20. #include <asm/macio.h>
  21. #include <asm/pmac_feature.h>
  22. #include <asm/prom.h>
  23. #include <asm/pci-bridge.h>
  24. #undef DEBUG
  25. #define MAX_NODE_NAME_SIZE (BUS_ID_SIZE - 12)
  26. static struct macio_chip *macio_on_hold;
  27. static int macio_bus_match(struct device *dev, struct device_driver *drv)
  28. {
  29. struct macio_dev * macio_dev = to_macio_device(dev);
  30. struct macio_driver * macio_drv = to_macio_driver(drv);
  31. const struct of_match * matches = macio_drv->match_table;
  32. if (!matches)
  33. return 0;
  34. return of_match_device(matches, &macio_dev->ofdev) != NULL;
  35. }
  36. struct macio_dev *macio_dev_get(struct macio_dev *dev)
  37. {
  38. struct device *tmp;
  39. if (!dev)
  40. return NULL;
  41. tmp = get_device(&dev->ofdev.dev);
  42. if (tmp)
  43. return to_macio_device(tmp);
  44. else
  45. return NULL;
  46. }
  47. void macio_dev_put(struct macio_dev *dev)
  48. {
  49. if (dev)
  50. put_device(&dev->ofdev.dev);
  51. }
  52. static int macio_device_probe(struct device *dev)
  53. {
  54. int error = -ENODEV;
  55. struct macio_driver *drv;
  56. struct macio_dev *macio_dev;
  57. const struct of_match *match;
  58. drv = to_macio_driver(dev->driver);
  59. macio_dev = to_macio_device(dev);
  60. if (!drv->probe)
  61. return error;
  62. macio_dev_get(macio_dev);
  63. match = of_match_device(drv->match_table, &macio_dev->ofdev);
  64. if (match)
  65. error = drv->probe(macio_dev, match);
  66. if (error)
  67. macio_dev_put(macio_dev);
  68. return error;
  69. }
  70. static int macio_device_remove(struct device *dev)
  71. {
  72. struct macio_dev * macio_dev = to_macio_device(dev);
  73. struct macio_driver * drv = to_macio_driver(dev->driver);
  74. if (dev->driver && drv->remove)
  75. drv->remove(macio_dev);
  76. macio_dev_put(macio_dev);
  77. return 0;
  78. }
  79. static void macio_device_shutdown(struct device *dev)
  80. {
  81. struct macio_dev * macio_dev = to_macio_device(dev);
  82. struct macio_driver * drv = to_macio_driver(dev->driver);
  83. if (dev->driver && drv->shutdown)
  84. drv->shutdown(macio_dev);
  85. }
  86. static int macio_device_suspend(struct device *dev, pm_message_t state)
  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->suspend)
  91. return drv->suspend(macio_dev, state);
  92. return 0;
  93. }
  94. static int macio_device_resume(struct device * dev)
  95. {
  96. struct macio_dev * macio_dev = to_macio_device(dev);
  97. struct macio_driver * drv = to_macio_driver(dev->driver);
  98. if (dev->driver && drv->resume)
  99. return drv->resume(macio_dev);
  100. return 0;
  101. }
  102. struct bus_type macio_bus_type = {
  103. .name = "macio",
  104. .match = macio_bus_match,
  105. .suspend = macio_device_suspend,
  106. .resume = macio_device_resume,
  107. };
  108. static int __init macio_bus_driver_init(void)
  109. {
  110. return bus_register(&macio_bus_type);
  111. }
  112. postcore_initcall(macio_bus_driver_init);
  113. /**
  114. * macio_release_dev - free a macio device structure when all users of it are finished.
  115. * @dev: device that's been disconnected
  116. *
  117. * Will be called only by the device core when all users of this macio device are
  118. * done. This currently means never as we don't hot remove any macio device yet,
  119. * though that will happen with mediabay based devices in a later implementation.
  120. */
  121. static void macio_release_dev(struct device *dev)
  122. {
  123. struct macio_dev *mdev;
  124. mdev = to_macio_device(dev);
  125. kfree(mdev);
  126. }
  127. /**
  128. * macio_resource_quirks - tweak or skip some resources for a device
  129. * @np: pointer to the device node
  130. * @res: resulting resource
  131. * @index: index of resource in node
  132. *
  133. * If this routine returns non-null, then the resource is completely
  134. * skipped.
  135. */
  136. static int macio_resource_quirks(struct device_node *np, struct resource *res, int index)
  137. {
  138. if (res->flags & IORESOURCE_MEM) {
  139. /* Grand Central has too large resource 0 on some machines */
  140. if (index == 0 && !strcmp(np->name, "gc")) {
  141. np->addrs[0].size = 0x20000;
  142. res->end = res->start + 0x1ffff;
  143. }
  144. /* Airport has bogus resource 2 */
  145. if (index >= 2 && !strcmp(np->name, "radio"))
  146. return 1;
  147. /* DBDMAs may have bogus sizes */
  148. if ((res->start & 0x0001f000) == 0x00008000) {
  149. np->addrs[index].size = 0x100;
  150. res->end = res->start + 0xff;
  151. }
  152. /* ESCC parent eats child resources. We could have added a level of hierarchy,
  153. * but I don't really feel the need for it */
  154. if (!strcmp(np->name, "escc"))
  155. return 1;
  156. /* ESCC has bogus resources >= 3 */
  157. if (index >= 3 && !(strcmp(np->name, "ch-a") && strcmp(np->name, "ch-b")))
  158. return 1;
  159. /* Media bay has too many resources, keep only first one */
  160. if (index > 0 && !strcmp(np->name, "media-bay"))
  161. return 1;
  162. /* Some older IDE resources have bogus sizes */
  163. if (!(strcmp(np->name, "IDE") && strcmp(np->name, "ATA") &&
  164. strcmp(np->type, "ide") && strcmp(np->type, "ata"))) {
  165. if (index == 0 && np->addrs[0].size > 0x1000) {
  166. np->addrs[0].size = 0x1000;
  167. res->end = res->start + 0xfff;
  168. }
  169. if (index == 1 && np->addrs[1].size > 0x100) {
  170. np->addrs[1].size = 0x100;
  171. res->end = res->start + 0xff;
  172. }
  173. }
  174. }
  175. return 0;
  176. }
  177. /**
  178. * macio_add_one_device - Add one device from OF node to the device tree
  179. * @chip: pointer to the macio_chip holding the device
  180. * @np: pointer to the device node in the OF tree
  181. * @in_bay: set to 1 if device is part of a media-bay
  182. *
  183. * When media-bay is changed to hotswap drivers, this function will
  184. * be exposed to the bay driver some way...
  185. */
  186. static struct macio_dev * macio_add_one_device(struct macio_chip *chip, struct device *parent,
  187. struct device_node *np, struct macio_dev *in_bay,
  188. struct resource *parent_res)
  189. {
  190. struct macio_dev *dev;
  191. int i, j;
  192. u32 *reg;
  193. if (np == NULL)
  194. return NULL;
  195. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  196. if (!dev)
  197. return NULL;
  198. memset(dev, 0, sizeof(*dev));
  199. dev->bus = &chip->lbus;
  200. dev->media_bay = in_bay;
  201. dev->ofdev.node = np;
  202. dev->ofdev.dma_mask = 0xffffffffUL;
  203. dev->ofdev.dev.dma_mask = &dev->ofdev.dma_mask;
  204. dev->ofdev.dev.parent = parent;
  205. dev->ofdev.dev.bus = &macio_bus_type;
  206. dev->ofdev.dev.release = macio_release_dev;
  207. #ifdef DEBUG
  208. printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
  209. dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj);
  210. #endif
  211. /* MacIO itself has a different reg, we use it's PCI base */
  212. if (np == chip->of_node) {
  213. sprintf(dev->ofdev.dev.bus_id, "%1d.%08lx:%.*s", chip->lbus.index,
  214. #ifdef CONFIG_PCI
  215. pci_resource_start(chip->lbus.pdev, 0),
  216. #else
  217. 0, /* NuBus may want to do something better here */
  218. #endif
  219. MAX_NODE_NAME_SIZE, np->name);
  220. } else {
  221. reg = (u32 *)get_property(np, "reg", NULL);
  222. sprintf(dev->ofdev.dev.bus_id, "%1d.%08x:%.*s", chip->lbus.index,
  223. reg ? *reg : 0, MAX_NODE_NAME_SIZE, np->name);
  224. }
  225. /* For now, we use pre-parsed entries in the device-tree for
  226. * interrupt routing and addresses, but we should change that
  227. * to dynamically parsed entries and so get rid of most of the
  228. * clutter in struct device_node
  229. */
  230. for (i = j = 0; i < np->n_intrs; i++) {
  231. struct resource *res = &dev->interrupt[j];
  232. if (j >= MACIO_DEV_COUNT_IRQS)
  233. break;
  234. res->start = np->intrs[i].line;
  235. res->flags = IORESOURCE_IO;
  236. if (np->intrs[j].sense)
  237. res->flags |= IORESOURCE_IRQ_LOWLEVEL;
  238. else
  239. res->flags |= IORESOURCE_IRQ_HIGHEDGE;
  240. res->name = dev->ofdev.dev.bus_id;
  241. if (macio_resource_quirks(np, res, i))
  242. memset(res, 0, sizeof(struct resource));
  243. else
  244. j++;
  245. }
  246. dev->n_interrupts = j;
  247. for (i = j = 0; i < np->n_addrs; i++) {
  248. struct resource *res = &dev->resource[j];
  249. if (j >= MACIO_DEV_COUNT_RESOURCES)
  250. break;
  251. res->start = np->addrs[i].address;
  252. res->end = np->addrs[i].address + np->addrs[i].size - 1;
  253. res->flags = IORESOURCE_MEM;
  254. res->name = dev->ofdev.dev.bus_id;
  255. if (macio_resource_quirks(np, res, i))
  256. memset(res, 0, sizeof(struct resource));
  257. else {
  258. j++;
  259. /* Currently, we consider failure as harmless, this may
  260. * change in the future, once I've found all the device
  261. * tree bugs in older machines & worked around them
  262. */
  263. if (insert_resource(parent_res, res))
  264. printk(KERN_WARNING "Can't request resource %d for MacIO"
  265. " device %s\n", i, dev->ofdev.dev.bus_id);
  266. }
  267. }
  268. dev->n_resources = j;
  269. if (of_device_register(&dev->ofdev) != 0) {
  270. printk(KERN_DEBUG"macio: device registration error for %s!\n",
  271. dev->ofdev.dev.bus_id);
  272. kfree(dev);
  273. return NULL;
  274. }
  275. return dev;
  276. }
  277. static int macio_skip_device(struct device_node *np)
  278. {
  279. if (strncmp(np->name, "battery", 7) == 0)
  280. return 1;
  281. if (strncmp(np->name, "escc-legacy", 11) == 0)
  282. return 1;
  283. return 0;
  284. }
  285. /**
  286. * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree
  287. * @chip: pointer to the macio_chip holding the devices
  288. *
  289. * This function will do the job of extracting devices from the
  290. * Open Firmware device tree, build macio_dev structures and add
  291. * them to the Linux device tree.
  292. *
  293. * For now, childs of media-bay are added now as well. This will
  294. * change rsn though.
  295. */
  296. static void macio_pci_add_devices(struct macio_chip *chip)
  297. {
  298. struct device_node *np, *pnode;
  299. struct macio_dev *rdev, *mdev, *mbdev = NULL, *sdev = NULL;
  300. struct device *parent = NULL;
  301. struct resource *root_res = &iomem_resource;
  302. /* Add a node for the macio bus itself */
  303. #ifdef CONFIG_PCI
  304. if (chip->lbus.pdev) {
  305. parent = &chip->lbus.pdev->dev;
  306. root_res = &chip->lbus.pdev->resource[0];
  307. }
  308. #endif
  309. pnode = of_node_get(chip->of_node);
  310. if (pnode == NULL)
  311. return;
  312. /* Add macio itself to hierarchy */
  313. rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res);
  314. if (rdev == NULL)
  315. return;
  316. root_res = &rdev->resource[0];
  317. /* First scan 1st level */
  318. for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
  319. if (!macio_skip_device(np)) {
  320. of_node_get(np);
  321. mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL, root_res);
  322. if (mdev == NULL)
  323. of_node_put(np);
  324. else if (strncmp(np->name, "media-bay", 9) == 0)
  325. mbdev = mdev;
  326. else if (strncmp(np->name, "escc", 4) == 0)
  327. sdev = mdev;
  328. }
  329. }
  330. /* Add media bay devices if any */
  331. if (mbdev)
  332. for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np)) != NULL;)
  333. if (!macio_skip_device(np)) {
  334. of_node_get(np);
  335. if (macio_add_one_device(chip, &mbdev->ofdev.dev, np, mbdev,
  336. root_res) == NULL)
  337. of_node_put(np);
  338. }
  339. /* Add serial ports if any */
  340. if (sdev) {
  341. for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np)) != NULL;)
  342. if (!macio_skip_device(np)) {
  343. of_node_get(np);
  344. if (macio_add_one_device(chip, &sdev->ofdev.dev, np, NULL,
  345. root_res) == NULL)
  346. of_node_put(np);
  347. }
  348. }
  349. }
  350. /**
  351. * macio_register_driver - Registers a new MacIO device driver
  352. * @drv: pointer to the driver definition structure
  353. */
  354. int macio_register_driver(struct macio_driver *drv)
  355. {
  356. int count = 0;
  357. /* initialize common driver fields */
  358. drv->driver.name = drv->name;
  359. drv->driver.bus = &macio_bus_type;
  360. drv->driver.probe = macio_device_probe;
  361. drv->driver.remove = macio_device_remove;
  362. drv->driver.shutdown = macio_device_shutdown;
  363. /* register with core */
  364. count = driver_register(&drv->driver);
  365. return count ? count : 1;
  366. }
  367. /**
  368. * macio_unregister_driver - Unregisters a new MacIO device driver
  369. * @drv: pointer to the driver definition structure
  370. */
  371. void macio_unregister_driver(struct macio_driver *drv)
  372. {
  373. driver_unregister(&drv->driver);
  374. }
  375. /**
  376. * macio_request_resource - Request an MMIO resource
  377. * @dev: pointer to the device holding the resource
  378. * @resource_no: resource number to request
  379. * @name: resource name
  380. *
  381. * Mark memory region number @resource_no associated with MacIO
  382. * device @dev as being reserved by owner @name. Do not access
  383. * any address inside the memory regions unless this call returns
  384. * successfully.
  385. *
  386. * Returns 0 on success, or %EBUSY on error. A warning
  387. * message is also printed on failure.
  388. */
  389. int macio_request_resource(struct macio_dev *dev, int resource_no, const char *name)
  390. {
  391. if (macio_resource_len(dev, resource_no) == 0)
  392. return 0;
  393. if (!request_mem_region(macio_resource_start(dev, resource_no),
  394. macio_resource_len(dev, resource_no),
  395. name))
  396. goto err_out;
  397. return 0;
  398. err_out:
  399. printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx"
  400. " for device %s\n",
  401. resource_no,
  402. macio_resource_len(dev, resource_no),
  403. macio_resource_start(dev, resource_no),
  404. dev->ofdev.dev.bus_id);
  405. return -EBUSY;
  406. }
  407. /**
  408. * macio_release_resource - Release an MMIO resource
  409. * @dev: pointer to the device holding the resource
  410. * @resource_no: resource number to release
  411. */
  412. void macio_release_resource(struct macio_dev *dev, int resource_no)
  413. {
  414. if (macio_resource_len(dev, resource_no) == 0)
  415. return;
  416. release_mem_region(macio_resource_start(dev, resource_no),
  417. macio_resource_len(dev, resource_no));
  418. }
  419. /**
  420. * macio_request_resources - Reserve all memory resources
  421. * @dev: MacIO device whose resources are to be reserved
  422. * @name: Name to be associated with resource.
  423. *
  424. * Mark all memory regions associated with MacIO device @dev as
  425. * being reserved by owner @name. Do not access any address inside
  426. * the memory regions unless this call returns successfully.
  427. *
  428. * Returns 0 on success, or %EBUSY on error. A warning
  429. * message is also printed on failure.
  430. */
  431. int macio_request_resources(struct macio_dev *dev, const char *name)
  432. {
  433. int i;
  434. for (i = 0; i < dev->n_resources; i++)
  435. if (macio_request_resource(dev, i, name))
  436. goto err_out;
  437. return 0;
  438. err_out:
  439. while(--i >= 0)
  440. macio_release_resource(dev, i);
  441. return -EBUSY;
  442. }
  443. /**
  444. * macio_release_resources - Release reserved memory resources
  445. * @dev: MacIO device whose resources were previously reserved
  446. */
  447. void macio_release_resources(struct macio_dev *dev)
  448. {
  449. int i;
  450. for (i = 0; i < dev->n_resources; i++)
  451. macio_release_resource(dev, i);
  452. }
  453. #ifdef CONFIG_PCI
  454. static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  455. {
  456. struct device_node* np;
  457. struct macio_chip* chip;
  458. if (ent->vendor != PCI_VENDOR_ID_APPLE)
  459. return -ENODEV;
  460. /* Note regarding refcounting: We assume pci_device_to_OF_node() is ported
  461. * to new OF APIs and returns a node with refcount incremented. This isn't
  462. * the case today, but on the other hand ppc32 doesn't do refcounting. This
  463. * will have to be fixed when going to ppc64. --BenH.
  464. */
  465. np = pci_device_to_OF_node(pdev);
  466. if (np == NULL)
  467. return -ENODEV;
  468. /* This assumption is wrong, fix that here for now until I fix the arch */
  469. of_node_get(np);
  470. /* We also assume that pmac_feature will have done a get() on nodes stored
  471. * in the macio chips array
  472. */
  473. chip = macio_find(np, macio_unknown);
  474. of_node_put(np);
  475. if (chip == NULL)
  476. return -ENODEV;
  477. /* XXX Need locking ??? */
  478. if (chip->lbus.pdev == NULL) {
  479. chip->lbus.pdev = pdev;
  480. chip->lbus.chip = chip;
  481. pci_set_drvdata(pdev, &chip->lbus);
  482. pci_set_master(pdev);
  483. }
  484. printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n",
  485. chip->name);
  486. /*
  487. * HACK ALERT: The WallStreet PowerBook and some OHare based machines
  488. * have 2 macio ASICs. I must probe the "main" one first or IDE ordering
  489. * will be incorrect. So I put on "hold" the second one since it seem to
  490. * appear first on PCI
  491. */
  492. if (chip->type == macio_gatwick || chip->type == macio_ohareII)
  493. if (macio_chips[0].lbus.pdev == NULL) {
  494. macio_on_hold = chip;
  495. return 0;
  496. }
  497. macio_pci_add_devices(chip);
  498. if (macio_on_hold && macio_chips[0].lbus.pdev != NULL) {
  499. macio_pci_add_devices(macio_on_hold);
  500. macio_on_hold = NULL;
  501. }
  502. return 0;
  503. }
  504. static void __devexit macio_pci_remove(struct pci_dev* pdev)
  505. {
  506. panic("removing of macio-asic not supported !\n");
  507. }
  508. /*
  509. * MacIO is matched against any Apple ID, it's probe() function
  510. * will then decide wether it applies or not
  511. */
  512. static const struct pci_device_id __devinitdata pci_ids [] = { {
  513. .vendor = PCI_VENDOR_ID_APPLE,
  514. .device = PCI_ANY_ID,
  515. .subvendor = PCI_ANY_ID,
  516. .subdevice = PCI_ANY_ID,
  517. }, { /* end: all zeroes */ }
  518. };
  519. MODULE_DEVICE_TABLE (pci, pci_ids);
  520. /* pci driver glue; this is a "new style" PCI driver module */
  521. static struct pci_driver macio_pci_driver = {
  522. .name = (char *) "macio",
  523. .id_table = pci_ids,
  524. .probe = macio_pci_probe,
  525. .remove = macio_pci_remove,
  526. };
  527. #endif /* CONFIG_PCI */
  528. static int __init macio_module_init (void)
  529. {
  530. #ifdef CONFIG_PCI
  531. int rc;
  532. rc = pci_register_driver(&macio_pci_driver);
  533. if (rc)
  534. return rc;
  535. #endif /* CONFIG_PCI */
  536. return 0;
  537. }
  538. module_init(macio_module_init);
  539. EXPORT_SYMBOL(macio_register_driver);
  540. EXPORT_SYMBOL(macio_unregister_driver);
  541. EXPORT_SYMBOL(macio_dev_get);
  542. EXPORT_SYMBOL(macio_dev_put);
  543. EXPORT_SYMBOL(macio_request_resource);
  544. EXPORT_SYMBOL(macio_release_resource);
  545. EXPORT_SYMBOL(macio_request_resources);
  546. EXPORT_SYMBOL(macio_release_resources);