pci_fire.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* pci_fire.c: Sun4u platform PCI-E controller support.
  2. *
  3. * Copyright (C) 2007 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/pci.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <asm/oplib.h>
  10. #include <asm/prom.h>
  11. #include "pci_impl.h"
  12. #define fire_read(__reg) \
  13. ({ u64 __ret; \
  14. __asm__ __volatile__("ldxa [%1] %2, %0" \
  15. : "=r" (__ret) \
  16. : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
  17. : "memory"); \
  18. __ret; \
  19. })
  20. #define fire_write(__reg, __val) \
  21. __asm__ __volatile__("stxa %0, [%1] %2" \
  22. : /* no outputs */ \
  23. : "r" (__val), "r" (__reg), \
  24. "i" (ASI_PHYS_BYPASS_EC_E) \
  25. : "memory")
  26. /* Fire config space address format is nearly identical to
  27. * that of SCHIZO and PSYCHO, except that in order to accomodate
  28. * PCI-E extended config space the encoding can handle 12 bits
  29. * of register address:
  30. *
  31. * 32 28 27 20 19 15 14 12 11 2 1 0
  32. * -------------------------------------------------
  33. * |0 0 0 0 0| bus | device | function | reg | 0 0 |
  34. * -------------------------------------------------
  35. */
  36. #define FIRE_CONFIG_BASE(PBM) ((PBM)->config_space)
  37. #define FIRE_CONFIG_ENCODE(BUS, DEVFN, REG) \
  38. (((unsigned long)(BUS) << 20) | \
  39. ((unsigned long)(DEVFN) << 12) | \
  40. ((unsigned long)(REG)))
  41. static void *fire_pci_config_mkaddr(struct pci_pbm_info *pbm,
  42. unsigned char bus,
  43. unsigned int devfn,
  44. int where)
  45. {
  46. if (!pbm)
  47. return NULL;
  48. return (void *)
  49. (FIRE_CONFIG_BASE(pbm) |
  50. FIRE_CONFIG_ENCODE(bus, devfn, where));
  51. }
  52. /* FIRE PCI configuration space accessors. */
  53. static int fire_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  54. int where, int size, u32 *value)
  55. {
  56. struct pci_pbm_info *pbm = bus_dev->sysdata;
  57. unsigned char bus = bus_dev->number;
  58. u32 *addr;
  59. u16 tmp16;
  60. u8 tmp8;
  61. if (bus_dev == pbm->pci_bus && devfn == 0x00)
  62. return pci_host_bridge_read_pci_cfg(bus_dev, devfn, where,
  63. size, value);
  64. switch (size) {
  65. case 1:
  66. *value = 0xff;
  67. break;
  68. case 2:
  69. *value = 0xffff;
  70. break;
  71. case 4:
  72. *value = 0xffffffff;
  73. break;
  74. }
  75. addr = fire_pci_config_mkaddr(pbm, bus, devfn, where);
  76. if (!addr)
  77. return PCIBIOS_SUCCESSFUL;
  78. switch (size) {
  79. case 1:
  80. pci_config_read8((u8 *)addr, &tmp8);
  81. *value = tmp8;
  82. break;
  83. case 2:
  84. if (where & 0x01) {
  85. printk("pci_read_config_word: misaligned reg [%x]\n",
  86. where);
  87. return PCIBIOS_SUCCESSFUL;
  88. }
  89. pci_config_read16((u16 *)addr, &tmp16);
  90. *value = tmp16;
  91. break;
  92. case 4:
  93. if (where & 0x03) {
  94. printk("pci_read_config_dword: misaligned reg [%x]\n",
  95. where);
  96. return PCIBIOS_SUCCESSFUL;
  97. }
  98. pci_config_read32(addr, value);
  99. break;
  100. }
  101. return PCIBIOS_SUCCESSFUL;
  102. }
  103. static int fire_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
  104. int where, int size, u32 value)
  105. {
  106. struct pci_pbm_info *pbm = bus_dev->sysdata;
  107. unsigned char bus = bus_dev->number;
  108. u32 *addr;
  109. if (bus_dev == pbm->pci_bus && devfn == 0x00)
  110. return pci_host_bridge_write_pci_cfg(bus_dev, devfn, where,
  111. size, value);
  112. addr = fire_pci_config_mkaddr(pbm, bus, devfn, where);
  113. if (!addr)
  114. return PCIBIOS_SUCCESSFUL;
  115. switch (size) {
  116. case 1:
  117. pci_config_write8((u8 *)addr, value);
  118. break;
  119. case 2:
  120. if (where & 0x01) {
  121. printk("pci_write_config_word: misaligned reg [%x]\n",
  122. where);
  123. return PCIBIOS_SUCCESSFUL;
  124. }
  125. pci_config_write16((u16 *)addr, value);
  126. break;
  127. case 4:
  128. if (where & 0x03) {
  129. printk("pci_write_config_dword: misaligned reg [%x]\n",
  130. where);
  131. return PCIBIOS_SUCCESSFUL;
  132. }
  133. pci_config_write32(addr, value);
  134. }
  135. return PCIBIOS_SUCCESSFUL;
  136. }
  137. static struct pci_ops pci_fire_ops = {
  138. .read = fire_read_pci_cfg,
  139. .write = fire_write_pci_cfg,
  140. };
  141. static void pci_fire_scan_bus(struct pci_pbm_info *pbm)
  142. {
  143. pbm->pci_bus = pci_scan_one_pbm(pbm);
  144. /* XXX register error interrupt handlers XXX */
  145. }
  146. #define FIRE_IOMMU_CONTROL 0x40000UL
  147. #define FIRE_IOMMU_TSBBASE 0x40008UL
  148. #define FIRE_IOMMU_FLUSH 0x40100UL
  149. #define FIRE_IOMMU_FLUSHINV 0x40100UL
  150. static void pci_fire_pbm_iommu_init(struct pci_pbm_info *pbm)
  151. {
  152. struct iommu *iommu = pbm->iommu;
  153. u32 vdma[2], dma_mask;
  154. u64 control;
  155. int tsbsize;
  156. /* No virtual-dma property on these guys, use largest size. */
  157. vdma[0] = 0xc0000000; /* base */
  158. vdma[1] = 0x40000000; /* size */
  159. dma_mask = 0xffffffff;
  160. tsbsize = 128;
  161. /* Register addresses. */
  162. iommu->iommu_control = pbm->pbm_regs + FIRE_IOMMU_CONTROL;
  163. iommu->iommu_tsbbase = pbm->pbm_regs + FIRE_IOMMU_TSBBASE;
  164. iommu->iommu_flush = pbm->pbm_regs + FIRE_IOMMU_FLUSH;
  165. iommu->iommu_flushinv = pbm->pbm_regs + FIRE_IOMMU_FLUSHINV;
  166. /* We use the main control/status register of FIRE as the write
  167. * completion register.
  168. */
  169. iommu->write_complete_reg = pbm->controller_regs + 0x410000UL;
  170. /*
  171. * Invalidate TLB Entries.
  172. */
  173. fire_write(iommu->iommu_flushinv, ~(u64)0);
  174. pci_iommu_table_init(iommu, tsbsize * 8 * 1024, vdma[0], dma_mask);
  175. fire_write(iommu->iommu_tsbbase, __pa(iommu->page_table) | 0x7UL);
  176. control = fire_read(iommu->iommu_control);
  177. control |= (0x00000400 /* TSB cache snoop enable */ |
  178. 0x00000300 /* Cache mode */ |
  179. 0x00000002 /* Bypass enable */ |
  180. 0x00000001 /* Translation enable */);
  181. fire_write(iommu->iommu_control, control);
  182. }
  183. /* Based at pbm->controller_regs */
  184. #define FIRE_PARITY_CONTROL 0x470010UL
  185. #define FIRE_PARITY_ENAB 0x8000000000000000UL
  186. #define FIRE_FATAL_RESET_CTL 0x471028UL
  187. #define FIRE_FATAL_RESET_SPARE 0x0000000004000000UL
  188. #define FIRE_FATAL_RESET_MB 0x0000000002000000UL
  189. #define FIRE_FATAL_RESET_CPE 0x0000000000008000UL
  190. #define FIRE_FATAL_RESET_APE 0x0000000000004000UL
  191. #define FIRE_FATAL_RESET_PIO 0x0000000000000040UL
  192. #define FIRE_FATAL_RESET_JW 0x0000000000000004UL
  193. #define FIRE_FATAL_RESET_JI 0x0000000000000002UL
  194. #define FIRE_FATAL_RESET_JR 0x0000000000000001UL
  195. #define FIRE_CORE_INTR_ENABLE 0x471800UL
  196. /* Based at pbm->pbm_regs */
  197. #define FIRE_TLU_CTRL 0x80000UL
  198. #define FIRE_TLU_CTRL_TIM 0x00000000da000000UL
  199. #define FIRE_TLU_CTRL_QDET 0x0000000000000100UL
  200. #define FIRE_TLU_CTRL_CFG 0x0000000000000001UL
  201. #define FIRE_TLU_DEV_CTRL 0x90008UL
  202. #define FIRE_TLU_LINK_CTRL 0x90020UL
  203. #define FIRE_TLU_LINK_CTRL_CLK 0x0000000000000040UL
  204. #define FIRE_LPU_RESET 0xe2008UL
  205. #define FIRE_LPU_LLCFG 0xe2200UL
  206. #define FIRE_LPU_LLCFG_VC0 0x0000000000000100UL
  207. #define FIRE_LPU_FCTRL_UCTRL 0xe2240UL
  208. #define FIRE_LPU_FCTRL_UCTRL_N 0x0000000000000002UL
  209. #define FIRE_LPU_FCTRL_UCTRL_P 0x0000000000000001UL
  210. #define FIRE_LPU_TXL_FIFOP 0xe2430UL
  211. #define FIRE_LPU_LTSSM_CFG2 0xe2788UL
  212. #define FIRE_LPU_LTSSM_CFG3 0xe2790UL
  213. #define FIRE_LPU_LTSSM_CFG4 0xe2798UL
  214. #define FIRE_LPU_LTSSM_CFG5 0xe27a0UL
  215. #define FIRE_DMC_IENAB 0x31800UL
  216. #define FIRE_DMC_DBG_SEL_A 0x53000UL
  217. #define FIRE_DMC_DBG_SEL_B 0x53008UL
  218. #define FIRE_PEC_IENAB 0x51800UL
  219. static void pci_fire_hw_init(struct pci_pbm_info *pbm)
  220. {
  221. u64 val;
  222. fire_write(pbm->controller_regs + FIRE_PARITY_CONTROL,
  223. FIRE_PARITY_ENAB);
  224. fire_write(pbm->controller_regs + FIRE_FATAL_RESET_CTL,
  225. (FIRE_FATAL_RESET_SPARE |
  226. FIRE_FATAL_RESET_MB |
  227. FIRE_FATAL_RESET_CPE |
  228. FIRE_FATAL_RESET_APE |
  229. FIRE_FATAL_RESET_PIO |
  230. FIRE_FATAL_RESET_JW |
  231. FIRE_FATAL_RESET_JI |
  232. FIRE_FATAL_RESET_JR));
  233. fire_write(pbm->controller_regs + FIRE_CORE_INTR_ENABLE, ~(u64)0);
  234. val = fire_read(pbm->pbm_regs + FIRE_TLU_CTRL);
  235. val |= (FIRE_TLU_CTRL_TIM |
  236. FIRE_TLU_CTRL_QDET |
  237. FIRE_TLU_CTRL_CFG);
  238. fire_write(pbm->pbm_regs + FIRE_TLU_CTRL, val);
  239. fire_write(pbm->pbm_regs + FIRE_TLU_DEV_CTRL, 0);
  240. fire_write(pbm->pbm_regs + FIRE_TLU_LINK_CTRL,
  241. FIRE_TLU_LINK_CTRL_CLK);
  242. fire_write(pbm->pbm_regs + FIRE_LPU_RESET, 0);
  243. fire_write(pbm->pbm_regs + FIRE_LPU_LLCFG,
  244. FIRE_LPU_LLCFG_VC0);
  245. fire_write(pbm->pbm_regs + FIRE_LPU_FCTRL_UCTRL,
  246. (FIRE_LPU_FCTRL_UCTRL_N |
  247. FIRE_LPU_FCTRL_UCTRL_P));
  248. fire_write(pbm->pbm_regs + FIRE_LPU_TXL_FIFOP,
  249. ((0xffff << 16) | (0x0000 << 0)));
  250. fire_write(pbm->pbm_regs + FIRE_LPU_LTSSM_CFG2, 3000000);
  251. fire_write(pbm->pbm_regs + FIRE_LPU_LTSSM_CFG3, 500000);
  252. fire_write(pbm->pbm_regs + FIRE_LPU_LTSSM_CFG4,
  253. (2 << 16) | (140 << 8));
  254. fire_write(pbm->pbm_regs + FIRE_LPU_LTSSM_CFG5, 0);
  255. fire_write(pbm->pbm_regs + FIRE_DMC_IENAB, ~(u64)0);
  256. fire_write(pbm->pbm_regs + FIRE_DMC_DBG_SEL_A, 0);
  257. fire_write(pbm->pbm_regs + FIRE_DMC_DBG_SEL_B, 0);
  258. fire_write(pbm->pbm_regs + FIRE_PEC_IENAB, ~(u64)0);
  259. }
  260. static void pci_fire_pbm_init(struct pci_controller_info *p,
  261. struct device_node *dp, u32 portid)
  262. {
  263. const struct linux_prom64_registers *regs;
  264. struct pci_pbm_info *pbm;
  265. if ((portid & 1) == 0)
  266. pbm = &p->pbm_A;
  267. else
  268. pbm = &p->pbm_B;
  269. pbm->next = pci_pbm_root;
  270. pci_pbm_root = pbm;
  271. pbm->scan_bus = pci_fire_scan_bus;
  272. pbm->pci_ops = &pci_fire_ops;
  273. pbm->index = pci_num_pbms++;
  274. pbm->portid = portid;
  275. pbm->parent = p;
  276. pbm->prom_node = dp;
  277. pbm->name = dp->full_name;
  278. regs = of_get_property(dp, "reg", NULL);
  279. pbm->pbm_regs = regs[0].phys_addr;
  280. pbm->controller_regs = regs[1].phys_addr - 0x410000UL;
  281. printk("%s: SUN4U PCIE Bus Module\n", pbm->name);
  282. pci_determine_mem_io_space(pbm);
  283. pci_get_pbm_props(pbm);
  284. pci_fire_hw_init(pbm);
  285. pci_fire_pbm_iommu_init(pbm);
  286. }
  287. static inline int portid_compare(u32 x, u32 y)
  288. {
  289. if (x == (y ^ 1))
  290. return 1;
  291. return 0;
  292. }
  293. void fire_pci_init(struct device_node *dp, const char *model_name)
  294. {
  295. struct pci_controller_info *p;
  296. u32 portid = of_getintprop_default(dp, "portid", 0xff);
  297. struct iommu *iommu;
  298. struct pci_pbm_info *pbm;
  299. for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
  300. if (portid_compare(pbm->portid, portid)) {
  301. pci_fire_pbm_init(pbm->parent, dp, portid);
  302. return;
  303. }
  304. }
  305. p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
  306. if (!p)
  307. goto fatal_memory_error;
  308. iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
  309. if (!iommu)
  310. goto fatal_memory_error;
  311. p->pbm_A.iommu = iommu;
  312. iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
  313. if (!iommu)
  314. goto fatal_memory_error;
  315. p->pbm_B.iommu = iommu;
  316. /* XXX MSI support XXX */
  317. /* Like PSYCHO and SCHIZO we have a 2GB aligned area
  318. * for memory space.
  319. */
  320. pci_memspace_mask = 0x7fffffffUL;
  321. pci_fire_pbm_init(p, dp, portid);
  322. return;
  323. fatal_memory_error:
  324. prom_printf("PCI_FIRE: Fatal memory allocation error.\n");
  325. prom_halt();
  326. }