mpc52xx_pci.c 12 KB

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