mpc52xx_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * PCI code for the Freescale MPC52xx embedded CPU.
  3. *
  4. * Copyright (C) 2006 Secret Lab Technologies Ltd.
  5. * Grant Likely <grant.likely@secretlab.ca>
  6. * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #undef DEBUG
  13. #include <asm/pci.h>
  14. #include <asm/mpc52xx.h>
  15. #include <asm/delay.h>
  16. #include <asm/machdep.h>
  17. #include <linux/kernel.h>
  18. /* ======================================================================== */
  19. /* PCI windows config */
  20. /* ======================================================================== */
  21. #define MPC52xx_PCI_TARGET_IO 0xf0000000
  22. #define MPC52xx_PCI_TARGET_MEM 0x00000000
  23. /* ======================================================================== */
  24. /* Structures mapping & Defines for PCI Unit */
  25. /* ======================================================================== */
  26. #define MPC52xx_PCI_GSCR_BM 0x40000000
  27. #define MPC52xx_PCI_GSCR_PE 0x20000000
  28. #define MPC52xx_PCI_GSCR_SE 0x10000000
  29. #define MPC52xx_PCI_GSCR_XLB2PCI_MASK 0x07000000
  30. #define MPC52xx_PCI_GSCR_XLB2PCI_SHIFT 24
  31. #define MPC52xx_PCI_GSCR_IPG2PCI_MASK 0x00070000
  32. #define MPC52xx_PCI_GSCR_IPG2PCI_SHIFT 16
  33. #define MPC52xx_PCI_GSCR_BME 0x00004000
  34. #define MPC52xx_PCI_GSCR_PEE 0x00002000
  35. #define MPC52xx_PCI_GSCR_SEE 0x00001000
  36. #define MPC52xx_PCI_GSCR_PR 0x00000001
  37. #define MPC52xx_PCI_IWBTAR_TRANSLATION(proc_ad,pci_ad,size) \
  38. ( ( (proc_ad) & 0xff000000 ) | \
  39. ( (((size) - 1) >> 8) & 0x00ff0000 ) | \
  40. ( ((pci_ad) >> 16) & 0x0000ff00 ) )
  41. #define MPC52xx_PCI_IWCR_PACK(win0,win1,win2) (((win0) << 24) | \
  42. ((win1) << 16) | \
  43. ((win2) << 8))
  44. #define MPC52xx_PCI_IWCR_DISABLE 0x0
  45. #define MPC52xx_PCI_IWCR_ENABLE 0x1
  46. #define MPC52xx_PCI_IWCR_READ 0x0
  47. #define MPC52xx_PCI_IWCR_READ_LINE 0x2
  48. #define MPC52xx_PCI_IWCR_READ_MULTI 0x4
  49. #define MPC52xx_PCI_IWCR_MEM 0x0
  50. #define MPC52xx_PCI_IWCR_IO 0x8
  51. #define MPC52xx_PCI_TCR_P 0x01000000
  52. #define MPC52xx_PCI_TCR_LD 0x00010000
  53. #define MPC52xx_PCI_TCR_WCT8 0x00000008
  54. #define MPC52xx_PCI_TBATR_DISABLE 0x0
  55. #define MPC52xx_PCI_TBATR_ENABLE 0x1
  56. struct mpc52xx_pci {
  57. u32 idr; /* PCI + 0x00 */
  58. u32 scr; /* PCI + 0x04 */
  59. u32 ccrir; /* PCI + 0x08 */
  60. u32 cr1; /* PCI + 0x0C */
  61. u32 bar0; /* PCI + 0x10 */
  62. u32 bar1; /* PCI + 0x14 */
  63. u8 reserved1[16]; /* PCI + 0x18 */
  64. u32 ccpr; /* PCI + 0x28 */
  65. u32 sid; /* PCI + 0x2C */
  66. u32 erbar; /* PCI + 0x30 */
  67. u32 cpr; /* PCI + 0x34 */
  68. u8 reserved2[4]; /* PCI + 0x38 */
  69. u32 cr2; /* PCI + 0x3C */
  70. u8 reserved3[32]; /* PCI + 0x40 */
  71. u32 gscr; /* PCI + 0x60 */
  72. u32 tbatr0; /* PCI + 0x64 */
  73. u32 tbatr1; /* PCI + 0x68 */
  74. u32 tcr; /* PCI + 0x6C */
  75. u32 iw0btar; /* PCI + 0x70 */
  76. u32 iw1btar; /* PCI + 0x74 */
  77. u32 iw2btar; /* PCI + 0x78 */
  78. u8 reserved4[4]; /* PCI + 0x7C */
  79. u32 iwcr; /* PCI + 0x80 */
  80. u32 icr; /* PCI + 0x84 */
  81. u32 isr; /* PCI + 0x88 */
  82. u32 arb; /* PCI + 0x8C */
  83. u8 reserved5[104]; /* PCI + 0x90 */
  84. u32 car; /* PCI + 0xF8 */
  85. u8 reserved6[4]; /* PCI + 0xFC */
  86. };
  87. /* MPC5200 device tree match tables */
  88. const struct of_device_id mpc52xx_pci_ids[] __initdata = {
  89. { .type = "pci", .compatible = "fsl,mpc5200-pci", },
  90. { .type = "pci", .compatible = "mpc5200-pci", },
  91. {}
  92. };
  93. /* ======================================================================== */
  94. /* PCI configuration acess */
  95. /* ======================================================================== */
  96. static int
  97. mpc52xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  98. int offset, int len, u32 *val)
  99. {
  100. struct pci_controller *hose = bus->sysdata;
  101. u32 value;
  102. if (ppc_md.pci_exclude_device)
  103. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  104. return PCIBIOS_DEVICE_NOT_FOUND;
  105. out_be32(hose->cfg_addr,
  106. (1 << 31) |
  107. (bus->number << 16) |
  108. (devfn << 8) |
  109. (offset & 0xfc));
  110. mb();
  111. #if defined(CONFIG_PPC_MPC5200_BUGFIX)
  112. if (bus->number) {
  113. /* workaround for the bug 435 of the MPC5200 (L25R);
  114. * Don't do 32 bits config access during type-1 cycles */
  115. switch (len) {
  116. case 1:
  117. value = in_8(((u8 __iomem *)hose->cfg_data) +
  118. (offset & 3));
  119. break;
  120. case 2:
  121. value = in_le16(((u16 __iomem *)hose->cfg_data) +
  122. ((offset>>1) & 1));
  123. break;
  124. default:
  125. value = in_le16((u16 __iomem *)hose->cfg_data) |
  126. (in_le16(((u16 __iomem *)hose->cfg_data) + 1) << 16);
  127. break;
  128. }
  129. }
  130. else
  131. #endif
  132. {
  133. value = in_le32(hose->cfg_data);
  134. if (len != 4) {
  135. value >>= ((offset & 0x3) << 3);
  136. value &= 0xffffffff >> (32 - (len << 3));
  137. }
  138. }
  139. *val = value;
  140. out_be32(hose->cfg_addr, 0);
  141. mb();
  142. return PCIBIOS_SUCCESSFUL;
  143. }
  144. static int
  145. mpc52xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  146. int offset, int len, u32 val)
  147. {
  148. struct pci_controller *hose = bus->sysdata;
  149. u32 value, mask;
  150. if (ppc_md.pci_exclude_device)
  151. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  152. return PCIBIOS_DEVICE_NOT_FOUND;
  153. out_be32(hose->cfg_addr,
  154. (1 << 31) |
  155. (bus->number << 16) |
  156. (devfn << 8) |
  157. (offset & 0xfc));
  158. mb();
  159. #if defined(CONFIG_PPC_MPC5200_BUGFIX)
  160. if (bus->number) {
  161. /* workaround for the bug 435 of the MPC5200 (L25R);
  162. * Don't do 32 bits config access during type-1 cycles */
  163. switch (len) {
  164. case 1:
  165. out_8(((u8 __iomem *)hose->cfg_data) +
  166. (offset & 3), val);
  167. break;
  168. case 2:
  169. out_le16(((u16 __iomem *)hose->cfg_data) +
  170. ((offset>>1) & 1), val);
  171. break;
  172. default:
  173. out_le16((u16 __iomem *)hose->cfg_data,
  174. (u16)val);
  175. out_le16(((u16 __iomem *)hose->cfg_data) + 1,
  176. (u16)(val>>16));
  177. break;
  178. }
  179. }
  180. else
  181. #endif
  182. {
  183. if (len != 4) {
  184. value = in_le32(hose->cfg_data);
  185. offset = (offset & 0x3) << 3;
  186. mask = (0xffffffff >> (32 - (len << 3)));
  187. mask <<= offset;
  188. value &= ~mask;
  189. val = value | ((val << offset) & mask);
  190. }
  191. out_le32(hose->cfg_data, val);
  192. }
  193. mb();
  194. out_be32(hose->cfg_addr, 0);
  195. mb();
  196. return PCIBIOS_SUCCESSFUL;
  197. }
  198. static struct pci_ops mpc52xx_pci_ops = {
  199. .read = mpc52xx_pci_read_config,
  200. .write = mpc52xx_pci_write_config
  201. };
  202. /* ======================================================================== */
  203. /* PCI setup */
  204. /* ======================================================================== */
  205. static void __init
  206. mpc52xx_pci_setup(struct pci_controller *hose,
  207. struct mpc52xx_pci __iomem *pci_regs)
  208. {
  209. struct resource *res;
  210. u32 tmp;
  211. int iwcr0 = 0, iwcr1 = 0, iwcr2 = 0;
  212. pr_debug("mpc52xx_pci_setup(hose=%p, pci_regs=%p)\n", hose, pci_regs);
  213. /* pci_process_bridge_OF_ranges() found all our addresses for us;
  214. * now store them in the right places */
  215. hose->cfg_addr = &pci_regs->car;
  216. hose->cfg_data = hose->io_base_virt;
  217. /* Control regs */
  218. tmp = in_be32(&pci_regs->scr);
  219. tmp |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
  220. out_be32(&pci_regs->scr, tmp);
  221. /* Memory windows */
  222. res = &hose->mem_resources[0];
  223. if (res->flags) {
  224. pr_debug("mem_resource[0] = {.start=%x, .end=%x, .flags=%lx}\n",
  225. res->start, res->end, res->flags);
  226. out_be32(&pci_regs->iw0btar,
  227. MPC52xx_PCI_IWBTAR_TRANSLATION(res->start, res->start,
  228. res->end - res->start + 1));
  229. iwcr0 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_MEM;
  230. if (res->flags & IORESOURCE_PREFETCH)
  231. iwcr0 |= MPC52xx_PCI_IWCR_READ_MULTI;
  232. else
  233. iwcr0 |= MPC52xx_PCI_IWCR_READ;
  234. }
  235. res = &hose->mem_resources[1];
  236. if (res->flags) {
  237. pr_debug("mem_resource[1] = {.start=%x, .end=%x, .flags=%lx}\n",
  238. res->start, res->end, res->flags);
  239. out_be32(&pci_regs->iw1btar,
  240. MPC52xx_PCI_IWBTAR_TRANSLATION(res->start, res->start,
  241. res->end - res->start + 1));
  242. iwcr1 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_MEM;
  243. if (res->flags & IORESOURCE_PREFETCH)
  244. iwcr1 |= MPC52xx_PCI_IWCR_READ_MULTI;
  245. else
  246. iwcr1 |= MPC52xx_PCI_IWCR_READ;
  247. }
  248. /* IO resources */
  249. res = &hose->io_resource;
  250. if (!res) {
  251. printk(KERN_ERR "%s: Didn't find IO resources\n", __FILE__);
  252. return;
  253. }
  254. pr_debug(".io_resource={.start=%x,.end=%x,.flags=%lx} "
  255. ".io_base_phys=0x%p\n",
  256. res->start, res->end, res->flags, (void*)hose->io_base_phys);
  257. out_be32(&pci_regs->iw2btar,
  258. MPC52xx_PCI_IWBTAR_TRANSLATION(hose->io_base_phys,
  259. res->start,
  260. res->end - res->start + 1));
  261. iwcr2 = MPC52xx_PCI_IWCR_ENABLE | MPC52xx_PCI_IWCR_IO;
  262. /* Set all the IWCR fields at once; they're in the same reg */
  263. out_be32(&pci_regs->iwcr, MPC52xx_PCI_IWCR_PACK(iwcr0, iwcr1, iwcr2));
  264. out_be32(&pci_regs->tbatr0,
  265. MPC52xx_PCI_TBATR_ENABLE | MPC52xx_PCI_TARGET_IO );
  266. out_be32(&pci_regs->tbatr1,
  267. MPC52xx_PCI_TBATR_ENABLE | MPC52xx_PCI_TARGET_MEM );
  268. out_be32(&pci_regs->tcr, MPC52xx_PCI_TCR_LD | MPC52xx_PCI_TCR_WCT8);
  269. tmp = in_be32(&pci_regs->gscr);
  270. #if 0
  271. /* Reset the exteral bus ( internal PCI controller is NOT resetted ) */
  272. /* Not necessary and can be a bad thing if for example the bootloader
  273. is displaying a splash screen or ... Just left here for
  274. documentation purpose if anyone need it */
  275. out_be32(&pci_regs->gscr, tmp | MPC52xx_PCI_GSCR_PR);
  276. udelay(50);
  277. #endif
  278. /* Make sure the PCI bridge is out of reset */
  279. out_be32(&pci_regs->gscr, tmp & ~MPC52xx_PCI_GSCR_PR);
  280. }
  281. static void
  282. mpc52xx_pci_fixup_resources(struct pci_dev *dev)
  283. {
  284. int i;
  285. pr_debug("mpc52xx_pci_fixup_resources() %.4x:%.4x\n",
  286. dev->vendor, dev->device);
  287. /* We don't rely on boot loader for PCI and resets all
  288. devices */
  289. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  290. struct resource *res = &dev->resource[i];
  291. if (res->end > res->start) { /* Only valid resources */
  292. res->end -= res->start;
  293. res->start = 0;
  294. res->flags |= IORESOURCE_UNSET;
  295. }
  296. }
  297. /* The PCI Host bridge of MPC52xx has a prefetch memory resource
  298. fixed to 1Gb. Doesn't fit in the resource system so we remove it */
  299. if ( (dev->vendor == PCI_VENDOR_ID_MOTOROLA) &&
  300. ( dev->device == PCI_DEVICE_ID_MOTOROLA_MPC5200
  301. || dev->device == PCI_DEVICE_ID_MOTOROLA_MPC5200B) ) {
  302. struct resource *res = &dev->resource[1];
  303. res->start = res->end = res->flags = 0;
  304. }
  305. }
  306. int __init
  307. mpc52xx_add_bridge(struct device_node *node)
  308. {
  309. int len;
  310. struct mpc52xx_pci __iomem *pci_regs;
  311. struct pci_controller *hose;
  312. const int *bus_range;
  313. struct resource rsrc;
  314. pr_debug("Adding MPC52xx PCI host bridge %s\n", node->full_name);
  315. ppc_pci_flags |= PPC_PCI_REASSIGN_ALL_BUS;
  316. if (of_address_to_resource(node, 0, &rsrc) != 0) {
  317. printk(KERN_ERR "Can't get %s resources\n", node->full_name);
  318. return -EINVAL;
  319. }
  320. bus_range = of_get_property(node, "bus-range", &len);
  321. if (bus_range == NULL || len < 2 * sizeof(int)) {
  322. printk(KERN_WARNING "Can't get %s bus-range, assume bus 0\n",
  323. node->full_name);
  324. bus_range = NULL;
  325. }
  326. /* There are some PCI quirks on the 52xx, register the hook to
  327. * fix them. */
  328. ppc_md.pcibios_fixup_resources = mpc52xx_pci_fixup_resources;
  329. /* Alloc and initialize the pci controller. Values in the device
  330. * tree are needed to configure the 52xx PCI controller. Rather
  331. * than parse the tree here, let pci_process_bridge_OF_ranges()
  332. * do it for us and extract the values after the fact */
  333. hose = pcibios_alloc_controller(node);
  334. if (!hose)
  335. return -ENOMEM;
  336. hose->first_busno = bus_range ? bus_range[0] : 0;
  337. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  338. hose->ops = &mpc52xx_pci_ops;
  339. pci_regs = ioremap(rsrc.start, rsrc.end - rsrc.start + 1);
  340. if (!pci_regs)
  341. return -ENOMEM;
  342. pci_process_bridge_OF_ranges(hose, node, 1);
  343. /* Finish setting up PCI using values obtained by
  344. * pci_proces_bridge_OF_ranges */
  345. mpc52xx_pci_setup(hose, pci_regs);
  346. return 0;
  347. }
  348. void __init mpc52xx_setup_pci(void)
  349. {
  350. struct device_node *pci;
  351. pci = of_find_matching_node(NULL, mpc52xx_pci_ids);
  352. if (!pci)
  353. return;
  354. mpc52xx_add_bridge(pci);
  355. of_node_put(pci);
  356. }