pci-bcm63xx.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <asm/bootinfo.h>
  14. #include "pci-bcm63xx.h"
  15. /*
  16. * Allow PCI to be disabled at runtime depending on board nvram
  17. * configuration
  18. */
  19. int bcm63xx_pci_enabled;
  20. static struct resource bcm_pci_mem_resource = {
  21. .name = "bcm63xx PCI memory space",
  22. .start = BCM_PCI_MEM_BASE_PA,
  23. .end = BCM_PCI_MEM_END_PA,
  24. .flags = IORESOURCE_MEM
  25. };
  26. static struct resource bcm_pci_io_resource = {
  27. .name = "bcm63xx PCI IO space",
  28. .start = BCM_PCI_IO_BASE_PA,
  29. #ifdef CONFIG_CARDBUS
  30. .end = BCM_PCI_IO_HALF_PA,
  31. #else
  32. .end = BCM_PCI_IO_END_PA,
  33. #endif
  34. .flags = IORESOURCE_IO
  35. };
  36. struct pci_controller bcm63xx_controller = {
  37. .pci_ops = &bcm63xx_pci_ops,
  38. .io_resource = &bcm_pci_io_resource,
  39. .mem_resource = &bcm_pci_mem_resource,
  40. };
  41. /*
  42. * We handle cardbus via a fake Cardbus bridge, memory and io spaces
  43. * have to be clearly separated from PCI one since we have different
  44. * memory decoder.
  45. */
  46. #ifdef CONFIG_CARDBUS
  47. static struct resource bcm_cb_mem_resource = {
  48. .name = "bcm63xx Cardbus memory space",
  49. .start = BCM_CB_MEM_BASE_PA,
  50. .end = BCM_CB_MEM_END_PA,
  51. .flags = IORESOURCE_MEM
  52. };
  53. static struct resource bcm_cb_io_resource = {
  54. .name = "bcm63xx Cardbus IO space",
  55. .start = BCM_PCI_IO_HALF_PA + 1,
  56. .end = BCM_PCI_IO_END_PA,
  57. .flags = IORESOURCE_IO
  58. };
  59. struct pci_controller bcm63xx_cb_controller = {
  60. .pci_ops = &bcm63xx_cb_ops,
  61. .io_resource = &bcm_cb_io_resource,
  62. .mem_resource = &bcm_cb_mem_resource,
  63. };
  64. #endif
  65. static struct resource bcm_pcie_mem_resource = {
  66. .name = "bcm63xx PCIe memory space",
  67. .start = BCM_PCIE_MEM_BASE_PA,
  68. .end = BCM_PCIE_MEM_END_PA,
  69. .flags = IORESOURCE_MEM,
  70. };
  71. static struct resource bcm_pcie_io_resource = {
  72. .name = "bcm63xx PCIe IO space",
  73. .start = 0,
  74. .end = 0,
  75. .flags = 0,
  76. };
  77. struct pci_controller bcm63xx_pcie_controller = {
  78. .pci_ops = &bcm63xx_pcie_ops,
  79. .io_resource = &bcm_pcie_io_resource,
  80. .mem_resource = &bcm_pcie_mem_resource,
  81. };
  82. static u32 bcm63xx_int_cfg_readl(u32 reg)
  83. {
  84. u32 tmp;
  85. tmp = reg & MPI_PCICFGCTL_CFGADDR_MASK;
  86. tmp |= MPI_PCICFGCTL_WRITEEN_MASK;
  87. bcm_mpi_writel(tmp, MPI_PCICFGCTL_REG);
  88. iob();
  89. return bcm_mpi_readl(MPI_PCICFGDATA_REG);
  90. }
  91. static void bcm63xx_int_cfg_writel(u32 val, u32 reg)
  92. {
  93. u32 tmp;
  94. tmp = reg & MPI_PCICFGCTL_CFGADDR_MASK;
  95. tmp |= MPI_PCICFGCTL_WRITEEN_MASK;
  96. bcm_mpi_writel(tmp, MPI_PCICFGCTL_REG);
  97. bcm_mpi_writel(val, MPI_PCICFGDATA_REG);
  98. }
  99. void __iomem *pci_iospace_start;
  100. static void __init bcm63xx_reset_pcie(void)
  101. {
  102. u32 val;
  103. /* enable clock */
  104. val = bcm_perf_readl(PERF_CKCTL_REG);
  105. val |= CKCTL_6328_PCIE_EN;
  106. bcm_perf_writel(val, PERF_CKCTL_REG);
  107. /* enable SERDES */
  108. val = bcm_misc_readl(MISC_SERDES_CTRL_REG);
  109. val |= SERDES_PCIE_EN | SERDES_PCIE_EXD_EN;
  110. bcm_misc_writel(val, MISC_SERDES_CTRL_REG);
  111. /* reset the PCIe core */
  112. val = bcm_perf_readl(PERF_SOFTRESET_6328_REG);
  113. val &= ~SOFTRESET_6328_PCIE_MASK;
  114. val &= ~SOFTRESET_6328_PCIE_CORE_MASK;
  115. val &= ~SOFTRESET_6328_PCIE_HARD_MASK;
  116. val &= ~SOFTRESET_6328_PCIE_EXT_MASK;
  117. bcm_perf_writel(val, PERF_SOFTRESET_6328_REG);
  118. mdelay(10);
  119. val |= SOFTRESET_6328_PCIE_MASK;
  120. val |= SOFTRESET_6328_PCIE_CORE_MASK;
  121. val |= SOFTRESET_6328_PCIE_HARD_MASK;
  122. bcm_perf_writel(val, PERF_SOFTRESET_6328_REG);
  123. mdelay(10);
  124. val |= SOFTRESET_6328_PCIE_EXT_MASK;
  125. bcm_perf_writel(val, PERF_SOFTRESET_6328_REG);
  126. mdelay(200);
  127. }
  128. static int __init bcm63xx_register_pcie(void)
  129. {
  130. u32 val;
  131. bcm63xx_reset_pcie();
  132. /* configure the PCIe bridge */
  133. val = bcm_pcie_readl(PCIE_BRIDGE_OPT1_REG);
  134. val |= OPT1_RD_BE_OPT_EN;
  135. val |= OPT1_RD_REPLY_BE_FIX_EN;
  136. val |= OPT1_PCIE_BRIDGE_HOLE_DET_EN;
  137. val |= OPT1_L1_INT_STATUS_MASK_POL;
  138. bcm_pcie_writel(val, PCIE_BRIDGE_OPT1_REG);
  139. /* setup the interrupts */
  140. val = bcm_pcie_readl(PCIE_BRIDGE_RC_INT_MASK_REG);
  141. val |= PCIE_RC_INT_A | PCIE_RC_INT_B | PCIE_RC_INT_C | PCIE_RC_INT_D;
  142. bcm_pcie_writel(val, PCIE_BRIDGE_RC_INT_MASK_REG);
  143. val = bcm_pcie_readl(PCIE_BRIDGE_OPT2_REG);
  144. /* enable credit checking and error checking */
  145. val |= OPT2_TX_CREDIT_CHK_EN;
  146. val |= OPT2_UBUS_UR_DECODE_DIS;
  147. /* set device bus/func for the pcie device */
  148. val |= (PCIE_BUS_DEVICE << OPT2_CFG_TYPE1_BUS_NO_SHIFT);
  149. val |= OPT2_CFG_TYPE1_BD_SEL;
  150. bcm_pcie_writel(val, PCIE_BRIDGE_OPT2_REG);
  151. /* setup class code as bridge */
  152. val = bcm_pcie_readl(PCIE_IDVAL3_REG);
  153. val &= ~IDVAL3_CLASS_CODE_MASK;
  154. val |= (PCI_CLASS_BRIDGE_PCI << IDVAL3_SUBCLASS_SHIFT);
  155. bcm_pcie_writel(val, PCIE_IDVAL3_REG);
  156. /* disable bar1 size */
  157. val = bcm_pcie_readl(PCIE_CONFIG2_REG);
  158. val &= ~CONFIG2_BAR1_SIZE_MASK;
  159. bcm_pcie_writel(val, PCIE_CONFIG2_REG);
  160. /* set bar0 to little endian */
  161. val = (BCM_PCIE_MEM_BASE_PA >> 20) << BASEMASK_BASE_SHIFT;
  162. val |= (BCM_PCIE_MEM_BASE_PA >> 20) << BASEMASK_MASK_SHIFT;
  163. val |= BASEMASK_REMAP_EN;
  164. bcm_pcie_writel(val, PCIE_BRIDGE_BAR0_BASEMASK_REG);
  165. val = (BCM_PCIE_MEM_BASE_PA >> 20) << REBASE_ADDR_BASE_SHIFT;
  166. bcm_pcie_writel(val, PCIE_BRIDGE_BAR0_REBASE_ADDR_REG);
  167. register_pci_controller(&bcm63xx_pcie_controller);
  168. return 0;
  169. }
  170. static int __init bcm63xx_register_pci(void)
  171. {
  172. unsigned int mem_size;
  173. u32 val;
  174. /*
  175. * configuration access are done through IO space, remap 4
  176. * first bytes to access it from CPU.
  177. *
  178. * this means that no io access from CPU should happen while
  179. * we do a configuration cycle, but there's no way we can add
  180. * a spinlock for each io access, so this is currently kind of
  181. * broken on SMP.
  182. */
  183. pci_iospace_start = ioremap_nocache(BCM_PCI_IO_BASE_PA, 4);
  184. if (!pci_iospace_start)
  185. return -ENOMEM;
  186. /* setup local bus to PCI access (PCI memory) */
  187. val = BCM_PCI_MEM_BASE_PA & MPI_L2P_BASE_MASK;
  188. bcm_mpi_writel(val, MPI_L2PMEMBASE1_REG);
  189. bcm_mpi_writel(~(BCM_PCI_MEM_SIZE - 1), MPI_L2PMEMRANGE1_REG);
  190. bcm_mpi_writel(val | MPI_L2PREMAP_ENABLED_MASK, MPI_L2PMEMREMAP1_REG);
  191. /* set Cardbus IDSEL (type 0 cfg access on primary bus for
  192. * this IDSEL will be done on Cardbus instead) */
  193. val = bcm_pcmcia_readl(PCMCIA_C1_REG);
  194. val &= ~PCMCIA_C1_CBIDSEL_MASK;
  195. val |= (CARDBUS_PCI_IDSEL << PCMCIA_C1_CBIDSEL_SHIFT);
  196. bcm_pcmcia_writel(val, PCMCIA_C1_REG);
  197. #ifdef CONFIG_CARDBUS
  198. /* setup local bus to PCI access (Cardbus memory) */
  199. val = BCM_CB_MEM_BASE_PA & MPI_L2P_BASE_MASK;
  200. bcm_mpi_writel(val, MPI_L2PMEMBASE2_REG);
  201. bcm_mpi_writel(~(BCM_CB_MEM_SIZE - 1), MPI_L2PMEMRANGE2_REG);
  202. val |= MPI_L2PREMAP_ENABLED_MASK | MPI_L2PREMAP_IS_CARDBUS_MASK;
  203. bcm_mpi_writel(val, MPI_L2PMEMREMAP2_REG);
  204. #else
  205. /* disable second access windows */
  206. bcm_mpi_writel(0, MPI_L2PMEMREMAP2_REG);
  207. #endif
  208. /* setup local bus to PCI access (IO memory), we have only 1
  209. * IO window for both PCI and cardbus, but it cannot handle
  210. * both at the same time, assume standard PCI for now, if
  211. * cardbus card has IO zone, PCI fixup will change window to
  212. * cardbus */
  213. val = BCM_PCI_IO_BASE_PA & MPI_L2P_BASE_MASK;
  214. bcm_mpi_writel(val, MPI_L2PIOBASE_REG);
  215. bcm_mpi_writel(~(BCM_PCI_IO_SIZE - 1), MPI_L2PIORANGE_REG);
  216. bcm_mpi_writel(val | MPI_L2PREMAP_ENABLED_MASK, MPI_L2PIOREMAP_REG);
  217. /* enable PCI related GPIO pins */
  218. bcm_mpi_writel(MPI_LOCBUSCTL_EN_PCI_GPIO_MASK, MPI_LOCBUSCTL_REG);
  219. /* setup PCI to local bus access, used by PCI device to target
  220. * local RAM while bus mastering */
  221. bcm63xx_int_cfg_writel(0, PCI_BASE_ADDRESS_3);
  222. if (BCMCPU_IS_6358() || BCMCPU_IS_6368())
  223. val = MPI_SP0_REMAP_ENABLE_MASK;
  224. else
  225. val = 0;
  226. bcm_mpi_writel(val, MPI_SP0_REMAP_REG);
  227. bcm63xx_int_cfg_writel(0x0, PCI_BASE_ADDRESS_4);
  228. bcm_mpi_writel(0, MPI_SP1_REMAP_REG);
  229. mem_size = bcm63xx_get_memory_size();
  230. /* 6348 before rev b0 exposes only 16 MB of RAM memory through
  231. * PCI, throw a warning if we have more memory */
  232. if (BCMCPU_IS_6348() && (bcm63xx_get_cpu_rev() & 0xf0) == 0xa0) {
  233. if (mem_size > (16 * 1024 * 1024))
  234. printk(KERN_WARNING "bcm63xx: this CPU "
  235. "revision cannot handle more than 16MB "
  236. "of RAM for PCI bus mastering\n");
  237. } else {
  238. /* setup sp0 range to local RAM size */
  239. bcm_mpi_writel(~(mem_size - 1), MPI_SP0_RANGE_REG);
  240. bcm_mpi_writel(0, MPI_SP1_RANGE_REG);
  241. }
  242. /* change host bridge retry counter to infinite number of
  243. * retry, needed for some broadcom wifi cards with Silicon
  244. * Backplane bus where access to srom seems very slow */
  245. val = bcm63xx_int_cfg_readl(BCMPCI_REG_TIMERS);
  246. val &= ~REG_TIMER_RETRY_MASK;
  247. bcm63xx_int_cfg_writel(val, BCMPCI_REG_TIMERS);
  248. /* enable memory decoder and bus mastering */
  249. val = bcm63xx_int_cfg_readl(PCI_COMMAND);
  250. val |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  251. bcm63xx_int_cfg_writel(val, PCI_COMMAND);
  252. /* enable read prefetching & disable byte swapping for bus
  253. * mastering transfers */
  254. val = bcm_mpi_readl(MPI_PCIMODESEL_REG);
  255. val &= ~MPI_PCIMODESEL_BAR1_NOSWAP_MASK;
  256. val &= ~MPI_PCIMODESEL_BAR2_NOSWAP_MASK;
  257. val &= ~MPI_PCIMODESEL_PREFETCH_MASK;
  258. val |= (8 << MPI_PCIMODESEL_PREFETCH_SHIFT);
  259. bcm_mpi_writel(val, MPI_PCIMODESEL_REG);
  260. /* enable pci interrupt */
  261. val = bcm_mpi_readl(MPI_LOCINT_REG);
  262. val |= MPI_LOCINT_MASK(MPI_LOCINT_EXT_PCI_INT);
  263. bcm_mpi_writel(val, MPI_LOCINT_REG);
  264. register_pci_controller(&bcm63xx_controller);
  265. #ifdef CONFIG_CARDBUS
  266. register_pci_controller(&bcm63xx_cb_controller);
  267. #endif
  268. /* mark memory space used for IO mapping as reserved */
  269. request_mem_region(BCM_PCI_IO_BASE_PA, BCM_PCI_IO_SIZE,
  270. "bcm63xx PCI IO space");
  271. return 0;
  272. }
  273. static int __init bcm63xx_pci_init(void)
  274. {
  275. if (!bcm63xx_pci_enabled)
  276. return -ENODEV;
  277. switch (bcm63xx_get_cpu_id()) {
  278. case BCM6328_CPU_ID:
  279. return bcm63xx_register_pcie();
  280. case BCM6348_CPU_ID:
  281. case BCM6358_CPU_ID:
  282. case BCM6368_CPU_ID:
  283. return bcm63xx_register_pci();
  284. default:
  285. return -ENODEV;
  286. }
  287. }
  288. arch_initcall(bcm63xx_pci_init);