pci_common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /* $Id: pci_common.c,v 1.29 2002/02/01 00:56:03 davem Exp $
  2. * pci_common.c: PCI controller common support.
  3. *
  4. * Copyright (C) 1999 David S. Miller (davem@redhat.com)
  5. */
  6. #include <linux/string.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/pci.h>
  10. #include <linux/device.h>
  11. #include <asm/pbm.h>
  12. #include <asm/prom.h>
  13. #include <asm/of_device.h>
  14. #include "pci_impl.h"
  15. /* Fix self device of BUS and hook it into BUS->self.
  16. * The pci_scan_bus does not do this for the host bridge.
  17. */
  18. void __init pci_fixup_host_bridge_self(struct pci_bus *pbus)
  19. {
  20. struct pci_dev *pdev;
  21. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  22. if (pdev->class >> 8 == PCI_CLASS_BRIDGE_HOST) {
  23. pbus->self = pdev;
  24. return;
  25. }
  26. }
  27. prom_printf("PCI: Critical error, cannot find host bridge PDEV.\n");
  28. prom_halt();
  29. }
  30. /* Find the OBP PROM device tree node for a PCI device. */
  31. static struct device_node * __init
  32. find_device_prom_node(struct pci_pbm_info *pbm, struct pci_dev *pdev,
  33. struct device_node *bus_node,
  34. struct linux_prom_pci_registers **pregs,
  35. int *nregs)
  36. {
  37. struct device_node *dp;
  38. *nregs = 0;
  39. /*
  40. * Return the PBM's PROM node in case we are it's PCI device,
  41. * as the PBM's reg property is different to standard PCI reg
  42. * properties. We would delete this device entry otherwise,
  43. * which confuses XFree86's device probing...
  44. */
  45. if ((pdev->bus->number == pbm->pci_bus->number) && (pdev->devfn == 0) &&
  46. (pdev->vendor == PCI_VENDOR_ID_SUN) &&
  47. (pdev->device == PCI_DEVICE_ID_SUN_PBM ||
  48. pdev->device == PCI_DEVICE_ID_SUN_SCHIZO ||
  49. pdev->device == PCI_DEVICE_ID_SUN_TOMATILLO ||
  50. pdev->device == PCI_DEVICE_ID_SUN_SABRE ||
  51. pdev->device == PCI_DEVICE_ID_SUN_HUMMINGBIRD))
  52. return bus_node;
  53. dp = bus_node->child;
  54. while (dp) {
  55. struct linux_prom_pci_registers *regs;
  56. struct property *prop;
  57. int len;
  58. prop = of_find_property(dp, "reg", &len);
  59. if (!prop)
  60. goto do_next_sibling;
  61. regs = prop->value;
  62. if (((regs[0].phys_hi >> 8) & 0xff) == pdev->devfn) {
  63. *pregs = regs;
  64. *nregs = len / sizeof(struct linux_prom_pci_registers);
  65. return dp;
  66. }
  67. do_next_sibling:
  68. dp = dp->sibling;
  69. }
  70. return NULL;
  71. }
  72. /* Older versions of OBP on PCI systems encode 64-bit MEM
  73. * space assignments incorrectly, this fixes them up. We also
  74. * take the opportunity here to hide other kinds of bogus
  75. * assignments.
  76. */
  77. static void __init fixup_obp_assignments(struct pci_dev *pdev,
  78. struct pcidev_cookie *pcp)
  79. {
  80. int i;
  81. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  82. (pdev->device == PCI_DEVICE_ID_AL_M7101 ||
  83. pdev->device == PCI_DEVICE_ID_AL_M1533)) {
  84. int i;
  85. /* Zap all of the normal resources, they are
  86. * meaningless and generate bogus resource collision
  87. * messages. This is OpenBoot's ill-fated attempt to
  88. * represent the implicit resources that these devices
  89. * have.
  90. */
  91. pcp->num_prom_assignments = 0;
  92. for (i = 0; i < 6; i++) {
  93. pdev->resource[i].start =
  94. pdev->resource[i].end =
  95. pdev->resource[i].flags = 0;
  96. }
  97. pdev->resource[PCI_ROM_RESOURCE].start =
  98. pdev->resource[PCI_ROM_RESOURCE].end =
  99. pdev->resource[PCI_ROM_RESOURCE].flags = 0;
  100. return;
  101. }
  102. for (i = 0; i < pcp->num_prom_assignments; i++) {
  103. struct linux_prom_pci_registers *ap;
  104. int space;
  105. ap = &pcp->prom_assignments[i];
  106. space = ap->phys_hi >> 24;
  107. if ((space & 0x3) == 2 &&
  108. (space & 0x4) != 0) {
  109. ap->phys_hi &= ~(0x7 << 24);
  110. ap->phys_hi |= 0x3 << 24;
  111. }
  112. }
  113. }
  114. static ssize_t
  115. show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  116. {
  117. struct pci_dev *pdev;
  118. struct pcidev_cookie *sysdata;
  119. pdev = to_pci_dev(dev);
  120. sysdata = pdev->sysdata;
  121. return snprintf (buf, PAGE_SIZE, "%s\n", sysdata->prom_node->full_name);
  122. }
  123. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
  124. /* Fill in the PCI device cookie sysdata for the given
  125. * PCI device. This cookie is the means by which one
  126. * can get to OBP and PCI controller specific information
  127. * for a PCI device.
  128. */
  129. static void __init pdev_cookie_fillin(struct pci_pbm_info *pbm,
  130. struct pci_dev *pdev,
  131. struct device_node *bus_node)
  132. {
  133. struct linux_prom_pci_registers *pregs = NULL;
  134. struct pcidev_cookie *pcp;
  135. struct device_node *dp;
  136. struct property *prop;
  137. int nregs, len, err;
  138. dp = find_device_prom_node(pbm, pdev, bus_node,
  139. &pregs, &nregs);
  140. if (!dp) {
  141. /* If it is not in the OBP device tree then
  142. * there must be a damn good reason for it.
  143. *
  144. * So what we do is delete the device from the
  145. * PCI device tree completely. This scenario
  146. * is seen, for example, on CP1500 for the
  147. * second EBUS/HappyMeal pair if the external
  148. * connector for it is not present.
  149. */
  150. pci_remove_bus_device(pdev);
  151. return;
  152. }
  153. pcp = kzalloc(sizeof(*pcp), GFP_ATOMIC);
  154. if (pcp == NULL) {
  155. prom_printf("PCI_COOKIE: Fatal malloc error, aborting...\n");
  156. prom_halt();
  157. }
  158. pcp->pbm = pbm;
  159. pcp->prom_node = dp;
  160. pcp->op = of_find_device_by_node(dp);
  161. memcpy(pcp->prom_regs, pregs,
  162. nregs * sizeof(struct linux_prom_pci_registers));
  163. pcp->num_prom_regs = nregs;
  164. /* We can't have the pcidev_cookie assignments be just
  165. * direct pointers into the property value, since they
  166. * are potentially modified by the probing process.
  167. */
  168. prop = of_find_property(dp, "assigned-addresses", &len);
  169. if (!prop) {
  170. pcp->num_prom_assignments = 0;
  171. } else {
  172. memcpy(pcp->prom_assignments, prop->value, len);
  173. pcp->num_prom_assignments =
  174. (len / sizeof(pcp->prom_assignments[0]));
  175. }
  176. if (strcmp(dp->name, "ebus") == 0) {
  177. struct linux_prom_ebus_ranges *erng;
  178. int iter;
  179. /* EBUS is special... */
  180. prop = of_find_property(dp, "ranges", &len);
  181. if (!prop) {
  182. prom_printf("EBUS: Fatal error, no range property\n");
  183. prom_halt();
  184. }
  185. erng = prop->value;
  186. len = (len / sizeof(erng[0]));
  187. for (iter = 0; iter < len; iter++) {
  188. struct linux_prom_ebus_ranges *ep = &erng[iter];
  189. struct linux_prom_pci_registers *ap;
  190. ap = &pcp->prom_assignments[iter];
  191. ap->phys_hi = ep->parent_phys_hi;
  192. ap->phys_mid = ep->parent_phys_mid;
  193. ap->phys_lo = ep->parent_phys_lo;
  194. ap->size_hi = 0;
  195. ap->size_lo = ep->size;
  196. }
  197. pcp->num_prom_assignments = len;
  198. }
  199. fixup_obp_assignments(pdev, pcp);
  200. pdev->sysdata = pcp;
  201. /* we don't really care if we can create this file or not,
  202. * but we need to assign the result of the call or the world will fall
  203. * under alien invasion and everybody will be frozen on a spaceship
  204. * ready to be eaten on alpha centauri by some green and jelly humanoid.
  205. */
  206. err = sysfs_create_file(&pdev->dev.kobj, &dev_attr_obppath.attr);
  207. }
  208. void __init pci_fill_in_pbm_cookies(struct pci_bus *pbus,
  209. struct pci_pbm_info *pbm,
  210. struct device_node *dp)
  211. {
  212. struct pci_dev *pdev, *pdev_next;
  213. struct pci_bus *this_pbus, *pbus_next;
  214. /* This must be _safe because the cookie fillin
  215. routine can delete devices from the tree. */
  216. list_for_each_entry_safe(pdev, pdev_next, &pbus->devices, bus_list)
  217. pdev_cookie_fillin(pbm, pdev, dp);
  218. list_for_each_entry_safe(this_pbus, pbus_next, &pbus->children, node) {
  219. struct pcidev_cookie *pcp = this_pbus->self->sysdata;
  220. pci_fill_in_pbm_cookies(this_pbus, pbm, pcp->prom_node);
  221. }
  222. }
  223. static void __init bad_assignment(struct pci_dev *pdev,
  224. struct linux_prom_pci_registers *ap,
  225. struct resource *res,
  226. int do_prom_halt)
  227. {
  228. prom_printf("PCI: Bogus PROM assignment. BUS[%02x] DEVFN[%x]\n",
  229. pdev->bus->number, pdev->devfn);
  230. if (ap)
  231. prom_printf("PCI: phys[%08x:%08x:%08x] size[%08x:%08x]\n",
  232. ap->phys_hi, ap->phys_mid, ap->phys_lo,
  233. ap->size_hi, ap->size_lo);
  234. if (res)
  235. prom_printf("PCI: RES[%016lx-->%016lx:(%lx)]\n",
  236. res->start, res->end, res->flags);
  237. if (do_prom_halt)
  238. prom_halt();
  239. }
  240. static struct resource *
  241. __init get_root_resource(struct linux_prom_pci_registers *ap,
  242. struct pci_pbm_info *pbm)
  243. {
  244. int space = (ap->phys_hi >> 24) & 3;
  245. switch (space) {
  246. case 0:
  247. /* Configuration space, silently ignore it. */
  248. return NULL;
  249. case 1:
  250. /* 16-bit IO space */
  251. return &pbm->io_space;
  252. case 2:
  253. /* 32-bit MEM space */
  254. return &pbm->mem_space;
  255. case 3:
  256. /* 64-bit MEM space, these are allocated out of
  257. * the 32-bit mem_space range for the PBM, ie.
  258. * we just zero out the upper 32-bits.
  259. */
  260. return &pbm->mem_space;
  261. default:
  262. printk("PCI: What is resource space %x?\n", space);
  263. return NULL;
  264. };
  265. }
  266. static struct resource *
  267. __init get_device_resource(struct linux_prom_pci_registers *ap,
  268. struct pci_dev *pdev)
  269. {
  270. struct resource *res;
  271. int breg = (ap->phys_hi & 0xff);
  272. switch (breg) {
  273. case PCI_ROM_ADDRESS:
  274. /* Unfortunately I have seen several cases where
  275. * buggy FCODE uses a space value of '1' (I/O space)
  276. * in the register property for the ROM address
  277. * so disable this sanity check for now.
  278. */
  279. #if 0
  280. {
  281. int space = (ap->phys_hi >> 24) & 3;
  282. /* It had better be MEM space. */
  283. if (space != 2)
  284. bad_assignment(pdev, ap, NULL, 0);
  285. }
  286. #endif
  287. res = &pdev->resource[PCI_ROM_RESOURCE];
  288. break;
  289. case PCI_BASE_ADDRESS_0:
  290. case PCI_BASE_ADDRESS_1:
  291. case PCI_BASE_ADDRESS_2:
  292. case PCI_BASE_ADDRESS_3:
  293. case PCI_BASE_ADDRESS_4:
  294. case PCI_BASE_ADDRESS_5:
  295. res = &pdev->resource[(breg - PCI_BASE_ADDRESS_0) / 4];
  296. break;
  297. default:
  298. bad_assignment(pdev, ap, NULL, 0);
  299. res = NULL;
  300. break;
  301. };
  302. return res;
  303. }
  304. static void __init pdev_record_assignments(struct pci_pbm_info *pbm,
  305. struct pci_dev *pdev)
  306. {
  307. struct pcidev_cookie *pcp = pdev->sysdata;
  308. int i;
  309. for (i = 0; i < pcp->num_prom_assignments; i++) {
  310. struct linux_prom_pci_registers *ap;
  311. struct resource *root, *res;
  312. /* The format of this property is specified in
  313. * the PCI Bus Binding to IEEE1275-1994.
  314. */
  315. ap = &pcp->prom_assignments[i];
  316. root = get_root_resource(ap, pbm);
  317. res = get_device_resource(ap, pdev);
  318. if (root == NULL || res == NULL ||
  319. res->flags == 0)
  320. continue;
  321. /* Ok we know which resource this PROM assignment is
  322. * for, sanity check it.
  323. */
  324. if ((res->start & 0xffffffffUL) != ap->phys_lo)
  325. bad_assignment(pdev, ap, res, 1);
  326. /* If it is a 64-bit MEM space assignment, verify that
  327. * the resource is too and that the upper 32-bits match.
  328. */
  329. if (((ap->phys_hi >> 24) & 3) == 3) {
  330. if (((res->flags & IORESOURCE_MEM) == 0) ||
  331. ((res->flags & PCI_BASE_ADDRESS_MEM_TYPE_MASK)
  332. != PCI_BASE_ADDRESS_MEM_TYPE_64))
  333. bad_assignment(pdev, ap, res, 1);
  334. if ((res->start >> 32) != ap->phys_mid)
  335. bad_assignment(pdev, ap, res, 1);
  336. /* PBM cannot generate cpu initiated PIOs
  337. * to the full 64-bit space. Therefore the
  338. * upper 32-bits better be zero. If it is
  339. * not, just skip it and we will assign it
  340. * properly ourselves.
  341. */
  342. if ((res->start >> 32) != 0UL) {
  343. printk(KERN_ERR "PCI: OBP assigns out of range MEM address "
  344. "%016lx for region %ld on device %s\n",
  345. res->start, (res - &pdev->resource[0]), pci_name(pdev));
  346. continue;
  347. }
  348. }
  349. /* Adjust the resource into the physical address space
  350. * of this PBM.
  351. */
  352. pbm->parent->resource_adjust(pdev, res, root);
  353. if (request_resource(root, res) < 0) {
  354. int rnum;
  355. /* OK, there is some conflict. But this is fine
  356. * since we'll reassign it in the fixup pass.
  357. *
  358. * Do not print the warning for ROM resources
  359. * as such a conflict is quite common and
  360. * harmless as the ROM bar is disabled.
  361. */
  362. rnum = (res - &pdev->resource[0]);
  363. if (rnum != PCI_ROM_RESOURCE)
  364. printk(KERN_ERR "PCI: Resource collision, "
  365. "region %d "
  366. "[%016lx:%016lx] of device %s\n",
  367. rnum,
  368. res->start, res->end,
  369. pci_name(pdev));
  370. }
  371. }
  372. }
  373. void __init pci_record_assignments(struct pci_pbm_info *pbm,
  374. struct pci_bus *pbus)
  375. {
  376. struct pci_dev *dev;
  377. struct pci_bus *bus;
  378. list_for_each_entry(dev, &pbus->devices, bus_list)
  379. pdev_record_assignments(pbm, dev);
  380. list_for_each_entry(bus, &pbus->children, node)
  381. pci_record_assignments(pbm, bus);
  382. }
  383. /* Return non-zero if PDEV has implicit I/O resources even
  384. * though it may not have an I/O base address register
  385. * active.
  386. */
  387. static int __init has_implicit_io(struct pci_dev *pdev)
  388. {
  389. int class = pdev->class >> 8;
  390. if (class == PCI_CLASS_NOT_DEFINED ||
  391. class == PCI_CLASS_NOT_DEFINED_VGA ||
  392. class == PCI_CLASS_STORAGE_IDE ||
  393. (pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
  394. return 1;
  395. return 0;
  396. }
  397. static void __init pdev_assign_unassigned(struct pci_pbm_info *pbm,
  398. struct pci_dev *pdev)
  399. {
  400. u32 reg;
  401. u16 cmd;
  402. int i, io_seen, mem_seen;
  403. io_seen = mem_seen = 0;
  404. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  405. struct resource *root, *res;
  406. unsigned long size, min, max, align;
  407. res = &pdev->resource[i];
  408. if (res->flags & IORESOURCE_IO)
  409. io_seen++;
  410. else if (res->flags & IORESOURCE_MEM)
  411. mem_seen++;
  412. /* If it is already assigned or the resource does
  413. * not exist, there is nothing to do.
  414. */
  415. if (res->parent != NULL || res->flags == 0UL)
  416. continue;
  417. /* Determine the root we allocate from. */
  418. if (res->flags & IORESOURCE_IO) {
  419. root = &pbm->io_space;
  420. min = root->start + 0x400UL;
  421. max = root->end;
  422. } else {
  423. root = &pbm->mem_space;
  424. min = root->start;
  425. max = min + 0x80000000UL;
  426. }
  427. size = res->end - res->start;
  428. align = size + 1;
  429. if (allocate_resource(root, res, size + 1, min, max, align, NULL, NULL) < 0) {
  430. /* uh oh */
  431. prom_printf("PCI: Failed to allocate resource %d for %s\n",
  432. i, pci_name(pdev));
  433. prom_halt();
  434. }
  435. /* Update PCI config space. */
  436. pbm->parent->base_address_update(pdev, i);
  437. }
  438. /* Special case, disable the ROM. Several devices
  439. * act funny (ie. do not respond to memory space writes)
  440. * when it is left enabled. A good example are Qlogic,ISP
  441. * adapters.
  442. */
  443. pci_read_config_dword(pdev, PCI_ROM_ADDRESS, &reg);
  444. reg &= ~PCI_ROM_ADDRESS_ENABLE;
  445. pci_write_config_dword(pdev, PCI_ROM_ADDRESS, reg);
  446. /* If we saw I/O or MEM resources, enable appropriate
  447. * bits in PCI command register.
  448. */
  449. if (io_seen || mem_seen) {
  450. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  451. if (io_seen || has_implicit_io(pdev))
  452. cmd |= PCI_COMMAND_IO;
  453. if (mem_seen)
  454. cmd |= PCI_COMMAND_MEMORY;
  455. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  456. }
  457. /* If this is a PCI bridge or an IDE controller,
  458. * enable bus mastering. In the former case also
  459. * set the cache line size correctly.
  460. */
  461. if (((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI) ||
  462. (((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) &&
  463. ((pdev->class & 0x80) != 0))) {
  464. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  465. cmd |= PCI_COMMAND_MASTER;
  466. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  467. if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
  468. pci_write_config_byte(pdev,
  469. PCI_CACHE_LINE_SIZE,
  470. (64 / sizeof(u32)));
  471. }
  472. }
  473. void __init pci_assign_unassigned(struct pci_pbm_info *pbm,
  474. struct pci_bus *pbus)
  475. {
  476. struct pci_dev *dev;
  477. struct pci_bus *bus;
  478. list_for_each_entry(dev, &pbus->devices, bus_list)
  479. pdev_assign_unassigned(pbm, dev);
  480. list_for_each_entry(bus, &pbus->children, node)
  481. pci_assign_unassigned(pbm, bus);
  482. }
  483. static void __init pdev_fixup_irq(struct pci_dev *pdev)
  484. {
  485. struct pcidev_cookie *pcp = pdev->sysdata;
  486. struct of_device *op = pcp->op;
  487. if (op->irqs[0] == 0xffffffff) {
  488. pdev->irq = PCI_IRQ_NONE;
  489. return;
  490. }
  491. pdev->irq = op->irqs[0];
  492. pci_write_config_byte(pdev, PCI_INTERRUPT_LINE,
  493. pdev->irq & PCI_IRQ_INO);
  494. }
  495. void __init pci_fixup_irq(struct pci_pbm_info *pbm,
  496. struct pci_bus *pbus)
  497. {
  498. struct pci_dev *dev;
  499. struct pci_bus *bus;
  500. list_for_each_entry(dev, &pbus->devices, bus_list)
  501. pdev_fixup_irq(dev);
  502. list_for_each_entry(bus, &pbus->children, node)
  503. pci_fixup_irq(pbm, bus);
  504. }
  505. static void pdev_setup_busmastering(struct pci_dev *pdev, int is_66mhz)
  506. {
  507. u16 cmd;
  508. u8 hdr_type, min_gnt, ltimer;
  509. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  510. cmd |= PCI_COMMAND_MASTER;
  511. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  512. /* Read it back, if the mastering bit did not
  513. * get set, the device does not support bus
  514. * mastering so we have nothing to do here.
  515. */
  516. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  517. if ((cmd & PCI_COMMAND_MASTER) == 0)
  518. return;
  519. /* Set correct cache line size, 64-byte on all
  520. * Sparc64 PCI systems. Note that the value is
  521. * measured in 32-bit words.
  522. */
  523. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  524. 64 / sizeof(u32));
  525. pci_read_config_byte(pdev, PCI_HEADER_TYPE, &hdr_type);
  526. hdr_type &= ~0x80;
  527. if (hdr_type != PCI_HEADER_TYPE_NORMAL)
  528. return;
  529. /* If the latency timer is already programmed with a non-zero
  530. * value, assume whoever set it (OBP or whoever) knows what
  531. * they are doing.
  532. */
  533. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &ltimer);
  534. if (ltimer != 0)
  535. return;
  536. /* XXX Since I'm tipping off the min grant value to
  537. * XXX choose a suitable latency timer value, I also
  538. * XXX considered making use of the max latency value
  539. * XXX as well. Unfortunately I've seen too many bogusly
  540. * XXX low settings for it to the point where it lacks
  541. * XXX any usefulness. In one case, an ethernet card
  542. * XXX claimed a min grant of 10 and a max latency of 5.
  543. * XXX Now, if I had two such cards on the same bus I
  544. * XXX could not set the desired burst period (calculated
  545. * XXX from min grant) without violating the max latency
  546. * XXX bound. Duh...
  547. * XXX
  548. * XXX I blame dumb PC bios implementors for stuff like
  549. * XXX this, most of them don't even try to do something
  550. * XXX sensible with latency timer values and just set some
  551. * XXX default value (usually 32) into every device.
  552. */
  553. pci_read_config_byte(pdev, PCI_MIN_GNT, &min_gnt);
  554. if (min_gnt == 0) {
  555. /* If no min_gnt setting then use a default
  556. * value.
  557. */
  558. if (is_66mhz)
  559. ltimer = 16;
  560. else
  561. ltimer = 32;
  562. } else {
  563. int shift_factor;
  564. if (is_66mhz)
  565. shift_factor = 2;
  566. else
  567. shift_factor = 3;
  568. /* Use a default value when the min_gnt value
  569. * is erroneously high.
  570. */
  571. if (((unsigned int) min_gnt << shift_factor) > 512 ||
  572. ((min_gnt << shift_factor) & 0xff) == 0) {
  573. ltimer = 8 << shift_factor;
  574. } else {
  575. ltimer = min_gnt << shift_factor;
  576. }
  577. }
  578. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, ltimer);
  579. }
  580. void pci_determine_66mhz_disposition(struct pci_pbm_info *pbm,
  581. struct pci_bus *pbus)
  582. {
  583. struct pci_dev *pdev;
  584. int all_are_66mhz;
  585. u16 status;
  586. if (pbm->is_66mhz_capable == 0) {
  587. all_are_66mhz = 0;
  588. goto out;
  589. }
  590. all_are_66mhz = 1;
  591. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  592. pci_read_config_word(pdev, PCI_STATUS, &status);
  593. if (!(status & PCI_STATUS_66MHZ)) {
  594. all_are_66mhz = 0;
  595. break;
  596. }
  597. }
  598. out:
  599. pbm->all_devs_66mhz = all_are_66mhz;
  600. printk("PCI%d(PBM%c): Bus running at %dMHz\n",
  601. pbm->parent->index,
  602. (pbm == &pbm->parent->pbm_A) ? 'A' : 'B',
  603. (all_are_66mhz ? 66 : 33));
  604. }
  605. void pci_setup_busmastering(struct pci_pbm_info *pbm,
  606. struct pci_bus *pbus)
  607. {
  608. struct pci_dev *dev;
  609. struct pci_bus *bus;
  610. int is_66mhz;
  611. is_66mhz = pbm->is_66mhz_capable && pbm->all_devs_66mhz;
  612. list_for_each_entry(dev, &pbus->devices, bus_list)
  613. pdev_setup_busmastering(dev, is_66mhz);
  614. list_for_each_entry(bus, &pbus->children, node)
  615. pci_setup_busmastering(pbm, bus);
  616. }
  617. void pci_register_legacy_regions(struct resource *io_res,
  618. struct resource *mem_res)
  619. {
  620. struct resource *p;
  621. /* VGA Video RAM. */
  622. p = kzalloc(sizeof(*p), GFP_KERNEL);
  623. if (!p)
  624. return;
  625. p->name = "Video RAM area";
  626. p->start = mem_res->start + 0xa0000UL;
  627. p->end = p->start + 0x1ffffUL;
  628. p->flags = IORESOURCE_BUSY;
  629. request_resource(mem_res, p);
  630. p = kzalloc(sizeof(*p), GFP_KERNEL);
  631. if (!p)
  632. return;
  633. p->name = "System ROM";
  634. p->start = mem_res->start + 0xf0000UL;
  635. p->end = p->start + 0xffffUL;
  636. p->flags = IORESOURCE_BUSY;
  637. request_resource(mem_res, p);
  638. p = kzalloc(sizeof(*p), GFP_KERNEL);
  639. if (!p)
  640. return;
  641. p->name = "Video ROM";
  642. p->start = mem_res->start + 0xc0000UL;
  643. p->end = p->start + 0x7fffUL;
  644. p->flags = IORESOURCE_BUSY;
  645. request_resource(mem_res, p);
  646. }
  647. /* Generic helper routines for PCI error reporting. */
  648. void pci_scan_for_target_abort(struct pci_controller_info *p,
  649. struct pci_pbm_info *pbm,
  650. struct pci_bus *pbus)
  651. {
  652. struct pci_dev *pdev;
  653. struct pci_bus *bus;
  654. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  655. u16 status, error_bits;
  656. pci_read_config_word(pdev, PCI_STATUS, &status);
  657. error_bits =
  658. (status & (PCI_STATUS_SIG_TARGET_ABORT |
  659. PCI_STATUS_REC_TARGET_ABORT));
  660. if (error_bits) {
  661. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  662. printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n",
  663. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  664. pci_name(pdev), status);
  665. }
  666. }
  667. list_for_each_entry(bus, &pbus->children, node)
  668. pci_scan_for_target_abort(p, pbm, bus);
  669. }
  670. void pci_scan_for_master_abort(struct pci_controller_info *p,
  671. struct pci_pbm_info *pbm,
  672. struct pci_bus *pbus)
  673. {
  674. struct pci_dev *pdev;
  675. struct pci_bus *bus;
  676. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  677. u16 status, error_bits;
  678. pci_read_config_word(pdev, PCI_STATUS, &status);
  679. error_bits =
  680. (status & (PCI_STATUS_REC_MASTER_ABORT));
  681. if (error_bits) {
  682. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  683. printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n",
  684. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  685. pci_name(pdev), status);
  686. }
  687. }
  688. list_for_each_entry(bus, &pbus->children, node)
  689. pci_scan_for_master_abort(p, pbm, bus);
  690. }
  691. void pci_scan_for_parity_error(struct pci_controller_info *p,
  692. struct pci_pbm_info *pbm,
  693. struct pci_bus *pbus)
  694. {
  695. struct pci_dev *pdev;
  696. struct pci_bus *bus;
  697. list_for_each_entry(pdev, &pbus->devices, bus_list) {
  698. u16 status, error_bits;
  699. pci_read_config_word(pdev, PCI_STATUS, &status);
  700. error_bits =
  701. (status & (PCI_STATUS_PARITY |
  702. PCI_STATUS_DETECTED_PARITY));
  703. if (error_bits) {
  704. pci_write_config_word(pdev, PCI_STATUS, error_bits);
  705. printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n",
  706. p->index, ((pbm == &p->pbm_A) ? 'A' : 'B'),
  707. pci_name(pdev), status);
  708. }
  709. }
  710. list_for_each_entry(bus, &pbus->children, node)
  711. pci_scan_for_parity_error(p, pbm, bus);
  712. }