pmac_pci.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * Support for PCI bridges found on Power Macintoshes.
  3. * At present the "bandit" and "chaos" bridges are supported.
  4. * Fortunately you access configuration space in the same
  5. * way with either bridge.
  6. *
  7. * Copyright (C) 2003 Benjamin Herrenschmuidt (benh@kernel.crashing.org)
  8. * Copyright (C) 1997 Paul Mackerras (paulus@samba.org)
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/pci.h>
  17. #include <linux/delay.h>
  18. #include <linux/string.h>
  19. #include <linux/init.h>
  20. #include <linux/bootmem.h>
  21. #include <asm/sections.h>
  22. #include <asm/io.h>
  23. #include <asm/prom.h>
  24. #include <asm/pci-bridge.h>
  25. #include <asm/machdep.h>
  26. #include <asm/pmac_feature.h>
  27. #include <asm/iommu.h>
  28. #include "pci.h"
  29. #include "pmac.h"
  30. #define DEBUG
  31. #ifdef DEBUG
  32. #define DBG(x...) printk(x)
  33. #else
  34. #define DBG(x...)
  35. #endif
  36. /* XXX Could be per-controller, but I don't think we risk anything by
  37. * assuming we won't have both UniNorth and Bandit */
  38. static int has_uninorth;
  39. static struct pci_controller *u3_agp;
  40. struct device_node *k2_skiplist[2];
  41. static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
  42. {
  43. for (; node != 0;node = node->sibling) {
  44. int * bus_range;
  45. unsigned int *class_code;
  46. int len;
  47. /* For PCI<->PCI bridges or CardBus bridges, we go down */
  48. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  49. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  50. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  51. continue;
  52. bus_range = (int *) get_property(node, "bus-range", &len);
  53. if (bus_range != NULL && len > 2 * sizeof(int)) {
  54. if (bus_range[1] > higher)
  55. higher = bus_range[1];
  56. }
  57. higher = fixup_one_level_bus_range(node->child, higher);
  58. }
  59. return higher;
  60. }
  61. /* This routine fixes the "bus-range" property of all bridges in the
  62. * system since they tend to have their "last" member wrong on macs
  63. *
  64. * Note that the bus numbers manipulated here are OF bus numbers, they
  65. * are not Linux bus numbers.
  66. */
  67. static void __init fixup_bus_range(struct device_node *bridge)
  68. {
  69. int * bus_range;
  70. int len;
  71. /* Lookup the "bus-range" property for the hose */
  72. bus_range = (int *) get_property(bridge, "bus-range", &len);
  73. if (bus_range == NULL || len < 2 * sizeof(int)) {
  74. printk(KERN_WARNING "Can't get bus-range for %s\n",
  75. bridge->full_name);
  76. return;
  77. }
  78. bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
  79. }
  80. /*
  81. * Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.
  82. *
  83. * The "Bandit" version is present in all early PCI PowerMacs,
  84. * and up to the first ones using Grackle. Some machines may
  85. * have 2 bandit controllers (2 PCI busses).
  86. *
  87. * "Chaos" is used in some "Bandit"-type machines as a bridge
  88. * for the separate display bus. It is accessed the same
  89. * way as bandit, but cannot be probed for devices. It therefore
  90. * has its own config access functions.
  91. *
  92. * The "UniNorth" version is present in all Core99 machines
  93. * (iBook, G4, new IMacs, and all the recent Apple machines).
  94. * It contains 3 controllers in one ASIC.
  95. *
  96. * The U3 is the bridge used on G5 machines. It contains on
  97. * AGP bus which is dealt with the old UniNorth access routines
  98. * and an HyperTransport bus which uses its own set of access
  99. * functions.
  100. */
  101. #define MACRISC_CFA0(devfn, off) \
  102. ((1 << (unsigned long)PCI_SLOT(dev_fn)) \
  103. | (((unsigned long)PCI_FUNC(dev_fn)) << 8) \
  104. | (((unsigned long)(off)) & 0xFCUL))
  105. #define MACRISC_CFA1(bus, devfn, off) \
  106. ((((unsigned long)(bus)) << 16) \
  107. |(((unsigned long)(devfn)) << 8) \
  108. |(((unsigned long)(off)) & 0xFCUL) \
  109. |1UL)
  110. static unsigned long __pmac macrisc_cfg_access(struct pci_controller* hose,
  111. u8 bus, u8 dev_fn, u8 offset)
  112. {
  113. unsigned int caddr;
  114. if (bus == hose->first_busno) {
  115. if (dev_fn < (11 << 3))
  116. return 0;
  117. caddr = MACRISC_CFA0(dev_fn, offset);
  118. } else
  119. caddr = MACRISC_CFA1(bus, dev_fn, offset);
  120. /* Uninorth will return garbage if we don't read back the value ! */
  121. do {
  122. out_le32(hose->cfg_addr, caddr);
  123. } while (in_le32(hose->cfg_addr) != caddr);
  124. offset &= has_uninorth ? 0x07 : 0x03;
  125. return ((unsigned long)hose->cfg_data) + offset;
  126. }
  127. static int __pmac macrisc_read_config(struct pci_bus *bus, unsigned int devfn,
  128. int offset, int len, u32 *val)
  129. {
  130. struct pci_controller *hose;
  131. unsigned long addr;
  132. hose = pci_bus_to_host(bus);
  133. if (hose == NULL)
  134. return PCIBIOS_DEVICE_NOT_FOUND;
  135. addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
  136. if (!addr)
  137. return PCIBIOS_DEVICE_NOT_FOUND;
  138. /*
  139. * Note: the caller has already checked that offset is
  140. * suitably aligned and that len is 1, 2 or 4.
  141. */
  142. switch (len) {
  143. case 1:
  144. *val = in_8((u8 *)addr);
  145. break;
  146. case 2:
  147. *val = in_le16((u16 *)addr);
  148. break;
  149. default:
  150. *val = in_le32((u32 *)addr);
  151. break;
  152. }
  153. return PCIBIOS_SUCCESSFUL;
  154. }
  155. static int __pmac macrisc_write_config(struct pci_bus *bus, unsigned int devfn,
  156. int offset, int len, u32 val)
  157. {
  158. struct pci_controller *hose;
  159. unsigned long addr;
  160. hose = pci_bus_to_host(bus);
  161. if (hose == NULL)
  162. return PCIBIOS_DEVICE_NOT_FOUND;
  163. addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
  164. if (!addr)
  165. return PCIBIOS_DEVICE_NOT_FOUND;
  166. /*
  167. * Note: the caller has already checked that offset is
  168. * suitably aligned and that len is 1, 2 or 4.
  169. */
  170. switch (len) {
  171. case 1:
  172. out_8((u8 *)addr, val);
  173. (void) in_8((u8 *)addr);
  174. break;
  175. case 2:
  176. out_le16((u16 *)addr, val);
  177. (void) in_le16((u16 *)addr);
  178. break;
  179. default:
  180. out_le32((u32 *)addr, val);
  181. (void) in_le32((u32 *)addr);
  182. break;
  183. }
  184. return PCIBIOS_SUCCESSFUL;
  185. }
  186. static struct pci_ops macrisc_pci_ops =
  187. {
  188. macrisc_read_config,
  189. macrisc_write_config
  190. };
  191. /*
  192. * These versions of U3 HyperTransport config space access ops do not
  193. * implement self-view of the HT host yet
  194. */
  195. /*
  196. * This function deals with some "special cases" devices.
  197. *
  198. * 0 -> No special case
  199. * 1 -> Skip the device but act as if the access was successfull
  200. * (return 0xff's on reads, eventually, cache config space
  201. * accesses in a later version)
  202. * -1 -> Hide the device (unsuccessful acess)
  203. */
  204. static int u3_ht_skip_device(struct pci_controller *hose,
  205. struct pci_bus *bus, unsigned int devfn)
  206. {
  207. struct device_node *busdn, *dn;
  208. int i;
  209. /* We only allow config cycles to devices that are in OF device-tree
  210. * as we are apparently having some weird things going on with some
  211. * revs of K2 on recent G5s
  212. */
  213. if (bus->self)
  214. busdn = pci_device_to_OF_node(bus->self);
  215. else
  216. busdn = hose->arch_data;
  217. for (dn = busdn->child; dn; dn = dn->sibling)
  218. if (dn->devfn == devfn)
  219. break;
  220. if (dn == NULL)
  221. return -1;
  222. /*
  223. * When a device in K2 is powered down, we die on config
  224. * cycle accesses. Fix that here.
  225. */
  226. for (i=0; i<2; i++)
  227. if (k2_skiplist[i] == dn)
  228. return 1;
  229. return 0;
  230. }
  231. #define U3_HT_CFA0(devfn, off) \
  232. ((((unsigned long)devfn) << 8) | offset)
  233. #define U3_HT_CFA1(bus, devfn, off) \
  234. (U3_HT_CFA0(devfn, off) \
  235. + (((unsigned long)bus) << 16) \
  236. + 0x01000000UL)
  237. static unsigned long __pmac u3_ht_cfg_access(struct pci_controller* hose,
  238. u8 bus, u8 devfn, u8 offset)
  239. {
  240. if (bus == hose->first_busno) {
  241. /* For now, we don't self probe U3 HT bridge */
  242. if (PCI_SLOT(devfn) == 0)
  243. return 0;
  244. return ((unsigned long)hose->cfg_data) + U3_HT_CFA0(devfn, offset);
  245. } else
  246. return ((unsigned long)hose->cfg_data) + U3_HT_CFA1(bus, devfn, offset);
  247. }
  248. static int __pmac u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
  249. int offset, int len, u32 *val)
  250. {
  251. struct pci_controller *hose;
  252. unsigned long addr;
  253. hose = pci_bus_to_host(bus);
  254. if (hose == NULL)
  255. return PCIBIOS_DEVICE_NOT_FOUND;
  256. addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
  257. if (!addr)
  258. return PCIBIOS_DEVICE_NOT_FOUND;
  259. switch (u3_ht_skip_device(hose, bus, devfn)) {
  260. case 0:
  261. break;
  262. case 1:
  263. switch (len) {
  264. case 1:
  265. *val = 0xff; break;
  266. case 2:
  267. *val = 0xffff; break;
  268. default:
  269. *val = 0xfffffffful; break;
  270. }
  271. return PCIBIOS_SUCCESSFUL;
  272. default:
  273. return PCIBIOS_DEVICE_NOT_FOUND;
  274. }
  275. /*
  276. * Note: the caller has already checked that offset is
  277. * suitably aligned and that len is 1, 2 or 4.
  278. */
  279. switch (len) {
  280. case 1:
  281. *val = in_8((u8 *)addr);
  282. break;
  283. case 2:
  284. *val = in_le16((u16 *)addr);
  285. break;
  286. default:
  287. *val = in_le32((u32 *)addr);
  288. break;
  289. }
  290. return PCIBIOS_SUCCESSFUL;
  291. }
  292. static int __pmac u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
  293. int offset, int len, u32 val)
  294. {
  295. struct pci_controller *hose;
  296. unsigned long addr;
  297. hose = pci_bus_to_host(bus);
  298. if (hose == NULL)
  299. return PCIBIOS_DEVICE_NOT_FOUND;
  300. addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
  301. if (!addr)
  302. return PCIBIOS_DEVICE_NOT_FOUND;
  303. switch (u3_ht_skip_device(hose, bus, devfn)) {
  304. case 0:
  305. break;
  306. case 1:
  307. return PCIBIOS_SUCCESSFUL;
  308. default:
  309. return PCIBIOS_DEVICE_NOT_FOUND;
  310. }
  311. /*
  312. * Note: the caller has already checked that offset is
  313. * suitably aligned and that len is 1, 2 or 4.
  314. */
  315. switch (len) {
  316. case 1:
  317. out_8((u8 *)addr, val);
  318. (void) in_8((u8 *)addr);
  319. break;
  320. case 2:
  321. out_le16((u16 *)addr, val);
  322. (void) in_le16((u16 *)addr);
  323. break;
  324. default:
  325. out_le32((u32 *)addr, val);
  326. (void) in_le32((u32 *)addr);
  327. break;
  328. }
  329. return PCIBIOS_SUCCESSFUL;
  330. }
  331. static struct pci_ops u3_ht_pci_ops =
  332. {
  333. u3_ht_read_config,
  334. u3_ht_write_config
  335. };
  336. static void __init setup_u3_agp(struct pci_controller* hose)
  337. {
  338. /* On G5, we move AGP up to high bus number so we don't need
  339. * to reassign bus numbers for HT. If we ever have P2P bridges
  340. * on AGP, we'll have to move pci_assign_all_busses to the
  341. * pci_controller structure so we enable it for AGP and not for
  342. * HT childs.
  343. * We hard code the address because of the different size of
  344. * the reg address cell, we shall fix that by killing struct
  345. * reg_property and using some accessor functions instead
  346. */
  347. hose->first_busno = 0xf0;
  348. hose->last_busno = 0xff;
  349. has_uninorth = 1;
  350. hose->ops = &macrisc_pci_ops;
  351. hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
  352. hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
  353. u3_agp = hose;
  354. }
  355. static void __init setup_u3_ht(struct pci_controller* hose)
  356. {
  357. struct device_node *np = (struct device_node *)hose->arch_data;
  358. int i, cur;
  359. hose->ops = &u3_ht_pci_ops;
  360. /* We hard code the address because of the different size of
  361. * the reg address cell, we shall fix that by killing struct
  362. * reg_property and using some accessor functions instead
  363. */
  364. hose->cfg_data = (volatile unsigned char *)ioremap(0xf2000000, 0x02000000);
  365. /*
  366. * /ht node doesn't expose a "ranges" property, so we "remove" regions that
  367. * have been allocated to AGP. So far, this version of the code doesn't assign
  368. * any of the 0xfxxxxxxx "fine" memory regions to /ht.
  369. * We need to fix that sooner or later by either parsing all child "ranges"
  370. * properties or figuring out the U3 address space decoding logic and
  371. * then read it's configuration register (if any).
  372. */
  373. hose->io_base_phys = 0xf4000000;
  374. hose->io_base_virt = ioremap(hose->io_base_phys, 0x00400000);
  375. isa_io_base = pci_io_base = (unsigned long) hose->io_base_virt;
  376. hose->io_resource.name = np->full_name;
  377. hose->io_resource.start = 0;
  378. hose->io_resource.end = 0x003fffff;
  379. hose->io_resource.flags = IORESOURCE_IO;
  380. hose->pci_mem_offset = 0;
  381. hose->first_busno = 0;
  382. hose->last_busno = 0xef;
  383. hose->mem_resources[0].name = np->full_name;
  384. hose->mem_resources[0].start = 0x80000000;
  385. hose->mem_resources[0].end = 0xefffffff;
  386. hose->mem_resources[0].flags = IORESOURCE_MEM;
  387. if (u3_agp == NULL) {
  388. DBG("U3 has no AGP, using full resource range\n");
  389. return;
  390. }
  391. /* We "remove" the AGP resources from the resources allocated to HT, that
  392. * is we create "holes". However, that code does assumptions that so far
  393. * happen to be true (cross fingers...), typically that resources in the
  394. * AGP node are properly ordered
  395. */
  396. cur = 0;
  397. for (i=0; i<3; i++) {
  398. struct resource *res = &u3_agp->mem_resources[i];
  399. if (res->flags != IORESOURCE_MEM)
  400. continue;
  401. /* We don't care about "fine" resources */
  402. if (res->start >= 0xf0000000)
  403. continue;
  404. /* Check if it's just a matter of "shrinking" us in one direction */
  405. if (hose->mem_resources[cur].start == res->start) {
  406. DBG("U3/HT: shrink start of %d, %08lx -> %08lx\n",
  407. cur, hose->mem_resources[cur].start, res->end + 1);
  408. hose->mem_resources[cur].start = res->end + 1;
  409. continue;
  410. }
  411. if (hose->mem_resources[cur].end == res->end) {
  412. DBG("U3/HT: shrink end of %d, %08lx -> %08lx\n",
  413. cur, hose->mem_resources[cur].end, res->start - 1);
  414. hose->mem_resources[cur].end = res->start - 1;
  415. continue;
  416. }
  417. /* No, it's not the case, we need a hole */
  418. if (cur == 2) {
  419. /* not enough resources for a hole, we drop part of the range */
  420. printk(KERN_WARNING "Running out of resources for /ht host !\n");
  421. hose->mem_resources[cur].end = res->start - 1;
  422. continue;
  423. }
  424. cur++;
  425. DBG("U3/HT: hole, %d end at %08lx, %d start at %08lx\n",
  426. cur-1, res->start - 1, cur, res->end + 1);
  427. hose->mem_resources[cur].name = np->full_name;
  428. hose->mem_resources[cur].flags = IORESOURCE_MEM;
  429. hose->mem_resources[cur].start = res->end + 1;
  430. hose->mem_resources[cur].end = hose->mem_resources[cur-1].end;
  431. hose->mem_resources[cur-1].end = res->start - 1;
  432. }
  433. }
  434. static void __init pmac_process_bridge_OF_ranges(struct pci_controller *hose,
  435. struct device_node *dev, int primary)
  436. {
  437. static unsigned int static_lc_ranges[2024];
  438. unsigned int *dt_ranges, *lc_ranges, *ranges, *prev;
  439. unsigned int size;
  440. int rlen = 0, orig_rlen;
  441. int memno = 0;
  442. struct resource *res;
  443. int np, na = prom_n_addr_cells(dev);
  444. np = na + 5;
  445. /* First we try to merge ranges to fix a problem with some pmacs
  446. * that can have more than 3 ranges, fortunately using contiguous
  447. * addresses -- BenH
  448. */
  449. dt_ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  450. if (!dt_ranges)
  451. return;
  452. /* lc_ranges = alloc_bootmem(rlen);*/
  453. lc_ranges = static_lc_ranges;
  454. if (!lc_ranges)
  455. return; /* what can we do here ? */
  456. memcpy(lc_ranges, dt_ranges, rlen);
  457. orig_rlen = rlen;
  458. /* Let's work on a copy of the "ranges" property instead of damaging
  459. * the device-tree image in memory
  460. */
  461. ranges = lc_ranges;
  462. prev = NULL;
  463. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  464. if (prev) {
  465. if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
  466. (prev[2] + prev[na+4]) == ranges[2] &&
  467. (prev[na+2] + prev[na+4]) == ranges[na+2]) {
  468. prev[na+4] += ranges[na+4];
  469. ranges[0] = 0;
  470. ranges += np;
  471. continue;
  472. }
  473. }
  474. prev = ranges;
  475. ranges += np;
  476. }
  477. /*
  478. * The ranges property is laid out as an array of elements,
  479. * each of which comprises:
  480. * cells 0 - 2: a PCI address
  481. * cells 3 or 3+4: a CPU physical address
  482. * (size depending on dev->n_addr_cells)
  483. * cells 4+5 or 5+6: the size of the range
  484. */
  485. ranges = lc_ranges;
  486. rlen = orig_rlen;
  487. while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
  488. res = NULL;
  489. size = ranges[na+4];
  490. switch (ranges[0] >> 24) {
  491. case 1: /* I/O space */
  492. if (ranges[2] != 0)
  493. break;
  494. hose->io_base_phys = ranges[na+2];
  495. /* limit I/O space to 16MB */
  496. if (size > 0x01000000)
  497. size = 0x01000000;
  498. hose->io_base_virt = ioremap(ranges[na+2], size);
  499. if (primary)
  500. isa_io_base = (unsigned long) hose->io_base_virt;
  501. res = &hose->io_resource;
  502. res->flags = IORESOURCE_IO;
  503. res->start = ranges[2];
  504. break;
  505. case 2: /* memory space */
  506. memno = 0;
  507. if (ranges[1] == 0 && ranges[2] == 0
  508. && ranges[na+4] <= (16 << 20)) {
  509. /* 1st 16MB, i.e. ISA memory area */
  510. #if 0
  511. if (primary)
  512. isa_mem_base = ranges[na+2];
  513. #endif
  514. memno = 1;
  515. }
  516. while (memno < 3 && hose->mem_resources[memno].flags)
  517. ++memno;
  518. if (memno == 0)
  519. hose->pci_mem_offset = ranges[na+2] - ranges[2];
  520. if (memno < 3) {
  521. res = &hose->mem_resources[memno];
  522. res->flags = IORESOURCE_MEM;
  523. res->start = ranges[na+2];
  524. }
  525. break;
  526. }
  527. if (res != NULL) {
  528. res->name = dev->full_name;
  529. res->end = res->start + size - 1;
  530. res->parent = NULL;
  531. res->sibling = NULL;
  532. res->child = NULL;
  533. }
  534. ranges += np;
  535. }
  536. }
  537. /*
  538. * We assume that if we have a G3 powermac, we have one bridge called
  539. * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
  540. * if we have one or more bandit or chaos bridges, we don't have a MPC106.
  541. */
  542. static int __init add_bridge(struct device_node *dev)
  543. {
  544. int len;
  545. struct pci_controller *hose;
  546. char* disp_name;
  547. int *bus_range;
  548. int primary = 1;
  549. struct property *of_prop;
  550. DBG("Adding PCI host bridge %s\n", dev->full_name);
  551. bus_range = (int *) get_property(dev, "bus-range", &len);
  552. if (bus_range == NULL || len < 2 * sizeof(int)) {
  553. printk(KERN_WARNING "Can't get bus-range for %s, assume bus 0\n",
  554. dev->full_name);
  555. }
  556. hose = alloc_bootmem(sizeof(struct pci_controller));
  557. if (hose == NULL)
  558. return -ENOMEM;
  559. pci_setup_pci_controller(hose);
  560. hose->arch_data = dev;
  561. hose->first_busno = bus_range ? bus_range[0] : 0;
  562. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  563. of_prop = alloc_bootmem(sizeof(struct property) +
  564. sizeof(hose->global_number));
  565. if (of_prop) {
  566. memset(of_prop, 0, sizeof(struct property));
  567. of_prop->name = "linux,pci-domain";
  568. of_prop->length = sizeof(hose->global_number);
  569. of_prop->value = (unsigned char *)&of_prop[1];
  570. memcpy(of_prop->value, &hose->global_number, sizeof(hose->global_number));
  571. prom_add_property(dev, of_prop);
  572. }
  573. disp_name = NULL;
  574. if (device_is_compatible(dev, "u3-agp")) {
  575. setup_u3_agp(hose);
  576. disp_name = "U3-AGP";
  577. primary = 0;
  578. } else if (device_is_compatible(dev, "u3-ht")) {
  579. setup_u3_ht(hose);
  580. disp_name = "U3-HT";
  581. primary = 1;
  582. }
  583. printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n",
  584. disp_name, hose->first_busno, hose->last_busno);
  585. /* Interpret the "ranges" property */
  586. /* This also maps the I/O region and sets isa_io/mem_base */
  587. pmac_process_bridge_OF_ranges(hose, dev, primary);
  588. /* Fixup "bus-range" OF property */
  589. fixup_bus_range(dev);
  590. return 0;
  591. }
  592. /*
  593. * We use our own read_irq_line here because PCI_INTERRUPT_PIN is
  594. * crap on some of Apple ASICs. We unconditionally use the Open Firmware
  595. * interrupt number as this is always right.
  596. */
  597. static int pmac_pci_read_irq_line(struct pci_dev *pci_dev)
  598. {
  599. struct device_node *node;
  600. node = pci_device_to_OF_node(pci_dev);
  601. if (node == NULL)
  602. return -1;
  603. if (node->n_intrs == 0)
  604. return -1;
  605. pci_dev->irq = node->intrs[0].line;
  606. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
  607. return 0;
  608. }
  609. void __init pmac_pcibios_fixup(void)
  610. {
  611. struct pci_dev *dev = NULL;
  612. for_each_pci_dev(dev)
  613. pmac_pci_read_irq_line(dev);
  614. }
  615. static void __init pmac_fixup_phb_resources(void)
  616. {
  617. struct pci_controller *hose, *tmp;
  618. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  619. unsigned long offset = (unsigned long)hose->io_base_virt - pci_io_base;
  620. hose->io_resource.start += offset;
  621. hose->io_resource.end += offset;
  622. printk(KERN_INFO "PCI Host %d, io start: %lx; io end: %lx\n",
  623. hose->global_number,
  624. hose->io_resource.start, hose->io_resource.end);
  625. }
  626. }
  627. void __init pmac_pci_init(void)
  628. {
  629. struct device_node *np, *root;
  630. struct device_node *ht = NULL;
  631. /* Probe root PCI hosts, that is on U3 the AGP host and the
  632. * HyperTransport host. That one is actually "kept" around
  633. * and actually added last as it's resource management relies
  634. * on the AGP resources to have been setup first
  635. */
  636. root = of_find_node_by_path("/");
  637. if (root == NULL) {
  638. printk(KERN_CRIT "pmac_find_bridges: can't find root of device tree\n");
  639. return;
  640. }
  641. for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
  642. if (np->name == NULL)
  643. continue;
  644. if (strcmp(np->name, "pci") == 0) {
  645. if (add_bridge(np) == 0)
  646. of_node_get(np);
  647. }
  648. if (strcmp(np->name, "ht") == 0) {
  649. of_node_get(np);
  650. ht = np;
  651. }
  652. }
  653. of_node_put(root);
  654. /* Now setup the HyperTransport host if we found any
  655. */
  656. if (ht && add_bridge(ht) != 0)
  657. of_node_put(ht);
  658. /* Fixup the IO resources on our host bridges as the common code
  659. * does it only for childs of the host bridges
  660. */
  661. pmac_fixup_phb_resources();
  662. /* Setup the linkage between OF nodes and PHBs */
  663. pci_devs_phb_init();
  664. /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
  665. * assume there is no P2P bridge on the AGP bus, which should be a
  666. * safe assumptions hopefully.
  667. */
  668. if (u3_agp) {
  669. struct device_node *np = u3_agp->arch_data;
  670. np->busno = 0xf0;
  671. for (np = np->child; np; np = np->sibling)
  672. np->busno = 0xf0;
  673. }
  674. pmac_check_ht_link();
  675. /* Tell pci.c to not use the common resource allocation mecanism */
  676. pci_probe_only = 1;
  677. /* Allow all IO */
  678. io_page_mask = -1;
  679. }
  680. /*
  681. * Disable second function on K2-SATA, it's broken
  682. * and disable IO BARs on first one
  683. */
  684. static void fixup_k2_sata(struct pci_dev* dev)
  685. {
  686. int i;
  687. u16 cmd;
  688. if (PCI_FUNC(dev->devfn) > 0) {
  689. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  690. cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  691. pci_write_config_word(dev, PCI_COMMAND, cmd);
  692. for (i = 0; i < 6; i++) {
  693. dev->resource[i].start = dev->resource[i].end = 0;
  694. dev->resource[i].flags = 0;
  695. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
  696. }
  697. } else {
  698. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  699. cmd &= ~PCI_COMMAND_IO;
  700. pci_write_config_word(dev, PCI_COMMAND, cmd);
  701. for (i = 0; i < 5; i++) {
  702. dev->resource[i].start = dev->resource[i].end = 0;
  703. dev->resource[i].flags = 0;
  704. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
  705. }
  706. }
  707. }
  708. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);