mpc52xx_pci.c 12 KB

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