mpc52xx_pci.c 12 KB

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