pci_fire.c 10 KB

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