pcie.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * arch/arm/mach-tegra/pci.c
  3. *
  4. * PCIe host controller driver for TEGRA(2) SOCs
  5. *
  6. * Copyright (c) 2010, CompuLab, Ltd.
  7. * Author: Mike Rapoport <mike@compulab.co.il>
  8. *
  9. * Based on NVIDIA PCIe driver
  10. * Copyright (c) 2008-2009, NVIDIA Corporation.
  11. *
  12. * Bits taken from arch/arm/mach-dove/pcie.c
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but WITHOUT
  20. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  22. * more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/pci.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/irq.h>
  32. #include <linux/clk.h>
  33. #include <linux/delay.h>
  34. #include <linux/export.h>
  35. #include <asm/sizes.h>
  36. #include <asm/mach/pci.h>
  37. #include <mach/iomap.h>
  38. #include <mach/clk.h>
  39. #include <mach/powergate.h>
  40. #include "board.h"
  41. /* register definitions */
  42. #define AFI_OFFSET 0x3800
  43. #define PADS_OFFSET 0x3000
  44. #define RP0_OFFSET 0x0000
  45. #define RP1_OFFSET 0x1000
  46. #define AFI_AXI_BAR0_SZ 0x00
  47. #define AFI_AXI_BAR1_SZ 0x04
  48. #define AFI_AXI_BAR2_SZ 0x08
  49. #define AFI_AXI_BAR3_SZ 0x0c
  50. #define AFI_AXI_BAR4_SZ 0x10
  51. #define AFI_AXI_BAR5_SZ 0x14
  52. #define AFI_AXI_BAR0_START 0x18
  53. #define AFI_AXI_BAR1_START 0x1c
  54. #define AFI_AXI_BAR2_START 0x20
  55. #define AFI_AXI_BAR3_START 0x24
  56. #define AFI_AXI_BAR4_START 0x28
  57. #define AFI_AXI_BAR5_START 0x2c
  58. #define AFI_FPCI_BAR0 0x30
  59. #define AFI_FPCI_BAR1 0x34
  60. #define AFI_FPCI_BAR2 0x38
  61. #define AFI_FPCI_BAR3 0x3c
  62. #define AFI_FPCI_BAR4 0x40
  63. #define AFI_FPCI_BAR5 0x44
  64. #define AFI_CACHE_BAR0_SZ 0x48
  65. #define AFI_CACHE_BAR0_ST 0x4c
  66. #define AFI_CACHE_BAR1_SZ 0x50
  67. #define AFI_CACHE_BAR1_ST 0x54
  68. #define AFI_MSI_BAR_SZ 0x60
  69. #define AFI_MSI_FPCI_BAR_ST 0x64
  70. #define AFI_MSI_AXI_BAR_ST 0x68
  71. #define AFI_CONFIGURATION 0xac
  72. #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
  73. #define AFI_FPCI_ERROR_MASKS 0xb0
  74. #define AFI_INTR_MASK 0xb4
  75. #define AFI_INTR_MASK_INT_MASK (1 << 0)
  76. #define AFI_INTR_MASK_MSI_MASK (1 << 8)
  77. #define AFI_INTR_CODE 0xb8
  78. #define AFI_INTR_CODE_MASK 0xf
  79. #define AFI_INTR_MASTER_ABORT 4
  80. #define AFI_INTR_LEGACY 6
  81. #define AFI_INTR_SIGNATURE 0xbc
  82. #define AFI_SM_INTR_ENABLE 0xc4
  83. #define AFI_AFI_INTR_ENABLE 0xc8
  84. #define AFI_INTR_EN_INI_SLVERR (1 << 0)
  85. #define AFI_INTR_EN_INI_DECERR (1 << 1)
  86. #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
  87. #define AFI_INTR_EN_TGT_DECERR (1 << 3)
  88. #define AFI_INTR_EN_TGT_WRERR (1 << 4)
  89. #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
  90. #define AFI_INTR_EN_AXI_DECERR (1 << 6)
  91. #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
  92. #define AFI_PCIE_CONFIG 0x0f8
  93. #define AFI_PCIE_CONFIG_PCIEC0_DISABLE_DEVICE (1 << 1)
  94. #define AFI_PCIE_CONFIG_PCIEC1_DISABLE_DEVICE (1 << 2)
  95. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
  96. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
  97. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
  98. #define AFI_FUSE 0x104
  99. #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
  100. #define AFI_PEX0_CTRL 0x110
  101. #define AFI_PEX1_CTRL 0x118
  102. #define AFI_PEX_CTRL_RST (1 << 0)
  103. #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
  104. #define RP_VEND_XP 0x00000F00
  105. #define RP_VEND_XP_DL_UP (1 << 30)
  106. #define RP_LINK_CONTROL_STATUS 0x00000090
  107. #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
  108. #define PADS_CTL_SEL 0x0000009C
  109. #define PADS_CTL 0x000000A0
  110. #define PADS_CTL_IDDQ_1L (1 << 0)
  111. #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
  112. #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
  113. #define PADS_PLL_CTL 0x000000B8
  114. #define PADS_PLL_CTL_RST_B4SM (1 << 1)
  115. #define PADS_PLL_CTL_LOCKDET (1 << 8)
  116. #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
  117. #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
  118. #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
  119. #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
  120. #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
  121. #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
  122. #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
  123. /* PMC access is required for PCIE xclk (un)clamping */
  124. #define PMC_SCRATCH42 0x144
  125. #define PMC_SCRATCH42_PCX_CLAMP (1 << 0)
  126. static void __iomem *reg_pmc_base = IO_ADDRESS(TEGRA_PMC_BASE);
  127. #define pmc_writel(value, reg) \
  128. __raw_writel(value, reg_pmc_base + (reg))
  129. #define pmc_readl(reg) \
  130. __raw_readl(reg_pmc_base + (reg))
  131. /*
  132. * Tegra2 defines 1GB in the AXI address map for PCIe.
  133. *
  134. * That address space is split into different regions, with sizes and
  135. * offsets as follows:
  136. *
  137. * 0x80000000 - 0x80003fff - PCI controller registers
  138. * 0x80004000 - 0x80103fff - PCI configuration space
  139. * 0x80104000 - 0x80203fff - PCI extended configuration space
  140. * 0x80203fff - 0x803fffff - unused
  141. * 0x80400000 - 0x8040ffff - downstream IO
  142. * 0x80410000 - 0x8fffffff - unused
  143. * 0x90000000 - 0x9fffffff - non-prefetchable memory
  144. * 0xa0000000 - 0xbfffffff - prefetchable memory
  145. */
  146. #define PCIE_REGS_SZ SZ_16K
  147. #define PCIE_CFG_OFF PCIE_REGS_SZ
  148. #define PCIE_CFG_SZ SZ_1M
  149. #define PCIE_EXT_CFG_OFF (PCIE_CFG_SZ + PCIE_CFG_OFF)
  150. #define PCIE_EXT_CFG_SZ SZ_1M
  151. #define PCIE_IOMAP_SZ (PCIE_REGS_SZ + PCIE_CFG_SZ + PCIE_EXT_CFG_SZ)
  152. #define MEM_BASE_0 (TEGRA_PCIE_BASE + SZ_256M)
  153. #define MEM_SIZE_0 SZ_128M
  154. #define MEM_BASE_1 (MEM_BASE_0 + MEM_SIZE_0)
  155. #define MEM_SIZE_1 SZ_128M
  156. #define PREFETCH_MEM_BASE_0 (MEM_BASE_1 + MEM_SIZE_1)
  157. #define PREFETCH_MEM_SIZE_0 SZ_128M
  158. #define PREFETCH_MEM_BASE_1 (PREFETCH_MEM_BASE_0 + PREFETCH_MEM_SIZE_0)
  159. #define PREFETCH_MEM_SIZE_1 SZ_128M
  160. #define PCIE_CONF_BUS(b) ((b) << 16)
  161. #define PCIE_CONF_DEV(d) ((d) << 11)
  162. #define PCIE_CONF_FUNC(f) ((f) << 8)
  163. #define PCIE_CONF_REG(r) \
  164. (((r) & ~0x3) | (((r) < 256) ? PCIE_CFG_OFF : PCIE_EXT_CFG_OFF))
  165. struct tegra_pcie_port {
  166. int index;
  167. u8 root_bus_nr;
  168. void __iomem *base;
  169. bool link_up;
  170. char mem_space_name[16];
  171. char prefetch_space_name[20];
  172. struct resource res[2];
  173. };
  174. struct tegra_pcie_info {
  175. struct tegra_pcie_port port[2];
  176. int num_ports;
  177. void __iomem *regs;
  178. struct resource res_mmio;
  179. struct clk *pex_clk;
  180. struct clk *afi_clk;
  181. struct clk *pcie_xclk;
  182. struct clk *pll_e;
  183. };
  184. static struct tegra_pcie_info tegra_pcie;
  185. static inline void afi_writel(u32 value, unsigned long offset)
  186. {
  187. writel(value, offset + AFI_OFFSET + tegra_pcie.regs);
  188. }
  189. static inline u32 afi_readl(unsigned long offset)
  190. {
  191. return readl(offset + AFI_OFFSET + tegra_pcie.regs);
  192. }
  193. static inline void pads_writel(u32 value, unsigned long offset)
  194. {
  195. writel(value, offset + PADS_OFFSET + tegra_pcie.regs);
  196. }
  197. static inline u32 pads_readl(unsigned long offset)
  198. {
  199. return readl(offset + PADS_OFFSET + tegra_pcie.regs);
  200. }
  201. static struct tegra_pcie_port *bus_to_port(int bus)
  202. {
  203. int i;
  204. for (i = tegra_pcie.num_ports - 1; i >= 0; i--) {
  205. int rbus = tegra_pcie.port[i].root_bus_nr;
  206. if (rbus != -1 && rbus == bus)
  207. break;
  208. }
  209. return i >= 0 ? tegra_pcie.port + i : NULL;
  210. }
  211. static int tegra_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
  212. int where, int size, u32 *val)
  213. {
  214. struct tegra_pcie_port *pp = bus_to_port(bus->number);
  215. void __iomem *addr;
  216. if (pp) {
  217. if (devfn != 0) {
  218. *val = 0xffffffff;
  219. return PCIBIOS_DEVICE_NOT_FOUND;
  220. }
  221. addr = pp->base + (where & ~0x3);
  222. } else {
  223. addr = tegra_pcie.regs + (PCIE_CONF_BUS(bus->number) +
  224. PCIE_CONF_DEV(PCI_SLOT(devfn)) +
  225. PCIE_CONF_FUNC(PCI_FUNC(devfn)) +
  226. PCIE_CONF_REG(where));
  227. }
  228. *val = readl(addr);
  229. if (size == 1)
  230. *val = (*val >> (8 * (where & 3))) & 0xff;
  231. else if (size == 2)
  232. *val = (*val >> (8 * (where & 3))) & 0xffff;
  233. return PCIBIOS_SUCCESSFUL;
  234. }
  235. static int tegra_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
  236. int where, int size, u32 val)
  237. {
  238. struct tegra_pcie_port *pp = bus_to_port(bus->number);
  239. void __iomem *addr;
  240. u32 mask;
  241. u32 tmp;
  242. if (pp) {
  243. if (devfn != 0)
  244. return PCIBIOS_DEVICE_NOT_FOUND;
  245. addr = pp->base + (where & ~0x3);
  246. } else {
  247. addr = tegra_pcie.regs + (PCIE_CONF_BUS(bus->number) +
  248. PCIE_CONF_DEV(PCI_SLOT(devfn)) +
  249. PCIE_CONF_FUNC(PCI_FUNC(devfn)) +
  250. PCIE_CONF_REG(where));
  251. }
  252. if (size == 4) {
  253. writel(val, addr);
  254. return PCIBIOS_SUCCESSFUL;
  255. }
  256. if (size == 2)
  257. mask = ~(0xffff << ((where & 0x3) * 8));
  258. else if (size == 1)
  259. mask = ~(0xff << ((where & 0x3) * 8));
  260. else
  261. return PCIBIOS_BAD_REGISTER_NUMBER;
  262. tmp = readl(addr) & mask;
  263. tmp |= val << ((where & 0x3) * 8);
  264. writel(tmp, addr);
  265. return PCIBIOS_SUCCESSFUL;
  266. }
  267. static struct pci_ops tegra_pcie_ops = {
  268. .read = tegra_pcie_read_conf,
  269. .write = tegra_pcie_write_conf,
  270. };
  271. static void __devinit tegra_pcie_fixup_bridge(struct pci_dev *dev)
  272. {
  273. u16 reg;
  274. if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
  275. pci_read_config_word(dev, PCI_COMMAND, &reg);
  276. reg |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  277. PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
  278. pci_write_config_word(dev, PCI_COMMAND, reg);
  279. }
  280. }
  281. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_fixup_bridge);
  282. /* Tegra PCIE root complex wrongly reports device class */
  283. static void __devinit tegra_pcie_fixup_class(struct pci_dev *dev)
  284. {
  285. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  286. }
  287. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
  288. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
  289. /* Tegra PCIE requires relaxed ordering */
  290. static void __devinit tegra_pcie_relax_enable(struct pci_dev *dev)
  291. {
  292. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
  293. }
  294. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
  295. static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
  296. {
  297. struct tegra_pcie_port *pp;
  298. if (nr >= tegra_pcie.num_ports)
  299. return 0;
  300. pp = tegra_pcie.port + nr;
  301. pp->root_bus_nr = sys->busnr;
  302. pci_ioremap_io(nr * SZ_64K, TEGRA_PCIE_IO_BASE);
  303. /*
  304. * IORESOURCE_MEM
  305. */
  306. snprintf(pp->mem_space_name, sizeof(pp->mem_space_name),
  307. "PCIe %d MEM", pp->index);
  308. pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0;
  309. pp->res[0].name = pp->mem_space_name;
  310. if (pp->index == 0) {
  311. pp->res[0].start = MEM_BASE_0;
  312. pp->res[0].end = pp->res[0].start + MEM_SIZE_0 - 1;
  313. } else {
  314. pp->res[0].start = MEM_BASE_1;
  315. pp->res[0].end = pp->res[0].start + MEM_SIZE_1 - 1;
  316. }
  317. pp->res[0].flags = IORESOURCE_MEM;
  318. if (request_resource(&iomem_resource, &pp->res[0]))
  319. panic("Request PCIe Memory resource failed\n");
  320. pci_add_resource_offset(&sys->resources, &pp->res[0], sys->mem_offset);
  321. /*
  322. * IORESOURCE_MEM | IORESOURCE_PREFETCH
  323. */
  324. snprintf(pp->prefetch_space_name, sizeof(pp->prefetch_space_name),
  325. "PCIe %d PREFETCH MEM", pp->index);
  326. pp->prefetch_space_name[sizeof(pp->prefetch_space_name) - 1] = 0;
  327. pp->res[1].name = pp->prefetch_space_name;
  328. if (pp->index == 0) {
  329. pp->res[1].start = PREFETCH_MEM_BASE_0;
  330. pp->res[1].end = pp->res[1].start + PREFETCH_MEM_SIZE_0 - 1;
  331. } else {
  332. pp->res[1].start = PREFETCH_MEM_BASE_1;
  333. pp->res[1].end = pp->res[1].start + PREFETCH_MEM_SIZE_1 - 1;
  334. }
  335. pp->res[1].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  336. if (request_resource(&iomem_resource, &pp->res[1]))
  337. panic("Request PCIe Prefetch Memory resource failed\n");
  338. pci_add_resource_offset(&sys->resources, &pp->res[1], sys->mem_offset);
  339. return 1;
  340. }
  341. static int tegra_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  342. {
  343. return INT_PCIE_INTR;
  344. }
  345. static struct pci_bus __init *tegra_pcie_scan_bus(int nr,
  346. struct pci_sys_data *sys)
  347. {
  348. struct tegra_pcie_port *pp;
  349. if (nr >= tegra_pcie.num_ports)
  350. return NULL;
  351. pp = tegra_pcie.port + nr;
  352. pp->root_bus_nr = sys->busnr;
  353. return pci_scan_root_bus(NULL, sys->busnr, &tegra_pcie_ops, sys,
  354. &sys->resources);
  355. }
  356. static struct hw_pci tegra_pcie_hw __initdata = {
  357. .nr_controllers = 2,
  358. .setup = tegra_pcie_setup,
  359. .scan = tegra_pcie_scan_bus,
  360. .map_irq = tegra_pcie_map_irq,
  361. };
  362. static irqreturn_t tegra_pcie_isr(int irq, void *arg)
  363. {
  364. const char *err_msg[] = {
  365. "Unknown",
  366. "AXI slave error",
  367. "AXI decode error",
  368. "Target abort",
  369. "Master abort",
  370. "Invalid write",
  371. "Response decoding error",
  372. "AXI response decoding error",
  373. "Transcation timeout",
  374. };
  375. u32 code, signature;
  376. code = afi_readl(AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
  377. signature = afi_readl(AFI_INTR_SIGNATURE);
  378. afi_writel(0, AFI_INTR_CODE);
  379. if (code == AFI_INTR_LEGACY)
  380. return IRQ_NONE;
  381. if (code >= ARRAY_SIZE(err_msg))
  382. code = 0;
  383. /*
  384. * do not pollute kernel log with master abort reports since they
  385. * happen a lot during enumeration
  386. */
  387. if (code == AFI_INTR_MASTER_ABORT)
  388. pr_debug("PCIE: %s, signature: %08x\n", err_msg[code], signature);
  389. else
  390. pr_err("PCIE: %s, signature: %08x\n", err_msg[code], signature);
  391. return IRQ_HANDLED;
  392. }
  393. static void tegra_pcie_setup_translations(void)
  394. {
  395. u32 fpci_bar;
  396. u32 size;
  397. u32 axi_address;
  398. /* Bar 0: config Bar */
  399. fpci_bar = ((u32)0xfdff << 16);
  400. size = PCIE_CFG_SZ;
  401. axi_address = TEGRA_PCIE_BASE + PCIE_CFG_OFF;
  402. afi_writel(axi_address, AFI_AXI_BAR0_START);
  403. afi_writel(size >> 12, AFI_AXI_BAR0_SZ);
  404. afi_writel(fpci_bar, AFI_FPCI_BAR0);
  405. /* Bar 1: extended config Bar */
  406. fpci_bar = ((u32)0xfe1 << 20);
  407. size = PCIE_EXT_CFG_SZ;
  408. axi_address = TEGRA_PCIE_BASE + PCIE_EXT_CFG_OFF;
  409. afi_writel(axi_address, AFI_AXI_BAR1_START);
  410. afi_writel(size >> 12, AFI_AXI_BAR1_SZ);
  411. afi_writel(fpci_bar, AFI_FPCI_BAR1);
  412. /* Bar 2: downstream IO bar */
  413. fpci_bar = ((__u32)0xfdfc << 16);
  414. size = SZ_128K;
  415. axi_address = TEGRA_PCIE_IO_BASE;
  416. afi_writel(axi_address, AFI_AXI_BAR2_START);
  417. afi_writel(size >> 12, AFI_AXI_BAR2_SZ);
  418. afi_writel(fpci_bar, AFI_FPCI_BAR2);
  419. /* Bar 3: prefetchable memory BAR */
  420. fpci_bar = (((PREFETCH_MEM_BASE_0 >> 12) & 0x0fffffff) << 4) | 0x1;
  421. size = PREFETCH_MEM_SIZE_0 + PREFETCH_MEM_SIZE_1;
  422. axi_address = PREFETCH_MEM_BASE_0;
  423. afi_writel(axi_address, AFI_AXI_BAR3_START);
  424. afi_writel(size >> 12, AFI_AXI_BAR3_SZ);
  425. afi_writel(fpci_bar, AFI_FPCI_BAR3);
  426. /* Bar 4: non prefetchable memory BAR */
  427. fpci_bar = (((MEM_BASE_0 >> 12) & 0x0FFFFFFF) << 4) | 0x1;
  428. size = MEM_SIZE_0 + MEM_SIZE_1;
  429. axi_address = MEM_BASE_0;
  430. afi_writel(axi_address, AFI_AXI_BAR4_START);
  431. afi_writel(size >> 12, AFI_AXI_BAR4_SZ);
  432. afi_writel(fpci_bar, AFI_FPCI_BAR4);
  433. /* Bar 5: NULL out the remaining BAR as it is not used */
  434. fpci_bar = 0;
  435. size = 0;
  436. axi_address = 0;
  437. afi_writel(axi_address, AFI_AXI_BAR5_START);
  438. afi_writel(size >> 12, AFI_AXI_BAR5_SZ);
  439. afi_writel(fpci_bar, AFI_FPCI_BAR5);
  440. /* map all upstream transactions as uncached */
  441. afi_writel(PHYS_OFFSET, AFI_CACHE_BAR0_ST);
  442. afi_writel(0, AFI_CACHE_BAR0_SZ);
  443. afi_writel(0, AFI_CACHE_BAR1_ST);
  444. afi_writel(0, AFI_CACHE_BAR1_SZ);
  445. /* No MSI */
  446. afi_writel(0, AFI_MSI_FPCI_BAR_ST);
  447. afi_writel(0, AFI_MSI_BAR_SZ);
  448. afi_writel(0, AFI_MSI_AXI_BAR_ST);
  449. afi_writel(0, AFI_MSI_BAR_SZ);
  450. }
  451. static int tegra_pcie_enable_controller(void)
  452. {
  453. u32 val, reg;
  454. int i, timeout;
  455. /* Enable slot clock and pulse the reset signals */
  456. for (i = 0, reg = AFI_PEX0_CTRL; i < 2; i++, reg += 0x8) {
  457. val = afi_readl(reg) | AFI_PEX_CTRL_REFCLK_EN;
  458. afi_writel(val, reg);
  459. val &= ~AFI_PEX_CTRL_RST;
  460. afi_writel(val, reg);
  461. val = afi_readl(reg) | AFI_PEX_CTRL_RST;
  462. afi_writel(val, reg);
  463. }
  464. /* Enable dual controller and both ports */
  465. val = afi_readl(AFI_PCIE_CONFIG);
  466. val &= ~(AFI_PCIE_CONFIG_PCIEC0_DISABLE_DEVICE |
  467. AFI_PCIE_CONFIG_PCIEC1_DISABLE_DEVICE |
  468. AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK);
  469. val |= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
  470. afi_writel(val, AFI_PCIE_CONFIG);
  471. val = afi_readl(AFI_FUSE) & ~AFI_FUSE_PCIE_T0_GEN2_DIS;
  472. afi_writel(val, AFI_FUSE);
  473. /* Initialze internal PHY, enable up to 16 PCIE lanes */
  474. pads_writel(0x0, PADS_CTL_SEL);
  475. /* override IDDQ to 1 on all 4 lanes */
  476. val = pads_readl(PADS_CTL) | PADS_CTL_IDDQ_1L;
  477. pads_writel(val, PADS_CTL);
  478. /*
  479. * set up PHY PLL inputs select PLLE output as refclock,
  480. * set TX ref sel to div10 (not div5)
  481. */
  482. val = pads_readl(PADS_PLL_CTL);
  483. val &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
  484. val |= (PADS_PLL_CTL_REFCLK_INTERNAL_CML | PADS_PLL_CTL_TXCLKREF_DIV10);
  485. pads_writel(val, PADS_PLL_CTL);
  486. /* take PLL out of reset */
  487. val = pads_readl(PADS_PLL_CTL) | PADS_PLL_CTL_RST_B4SM;
  488. pads_writel(val, PADS_PLL_CTL);
  489. /*
  490. * Hack, set the clock voltage to the DEFAULT provided by hw folks.
  491. * This doesn't exist in the documentation
  492. */
  493. pads_writel(0xfa5cfa5c, 0xc8);
  494. /* Wait for the PLL to lock */
  495. timeout = 300;
  496. do {
  497. val = pads_readl(PADS_PLL_CTL);
  498. usleep_range(1000, 1000);
  499. if (--timeout == 0) {
  500. pr_err("Tegra PCIe error: timeout waiting for PLL\n");
  501. return -EBUSY;
  502. }
  503. } while (!(val & PADS_PLL_CTL_LOCKDET));
  504. /* turn off IDDQ override */
  505. val = pads_readl(PADS_CTL) & ~PADS_CTL_IDDQ_1L;
  506. pads_writel(val, PADS_CTL);
  507. /* enable TX/RX data */
  508. val = pads_readl(PADS_CTL);
  509. val |= (PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
  510. pads_writel(val, PADS_CTL);
  511. /* Take the PCIe interface module out of reset */
  512. tegra_periph_reset_deassert(tegra_pcie.pcie_xclk);
  513. /* Finally enable PCIe */
  514. val = afi_readl(AFI_CONFIGURATION) | AFI_CONFIGURATION_EN_FPCI;
  515. afi_writel(val, AFI_CONFIGURATION);
  516. val = (AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
  517. AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
  518. AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR);
  519. afi_writel(val, AFI_AFI_INTR_ENABLE);
  520. afi_writel(0xffffffff, AFI_SM_INTR_ENABLE);
  521. /* FIXME: No MSI for now, only INT */
  522. afi_writel(AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
  523. /* Disable all execptions */
  524. afi_writel(0, AFI_FPCI_ERROR_MASKS);
  525. return 0;
  526. }
  527. static void tegra_pcie_xclk_clamp(bool clamp)
  528. {
  529. u32 reg;
  530. reg = pmc_readl(PMC_SCRATCH42) & ~PMC_SCRATCH42_PCX_CLAMP;
  531. if (clamp)
  532. reg |= PMC_SCRATCH42_PCX_CLAMP;
  533. pmc_writel(reg, PMC_SCRATCH42);
  534. }
  535. static void tegra_pcie_power_off(void)
  536. {
  537. tegra_periph_reset_assert(tegra_pcie.pcie_xclk);
  538. tegra_periph_reset_assert(tegra_pcie.afi_clk);
  539. tegra_periph_reset_assert(tegra_pcie.pex_clk);
  540. tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  541. tegra_pcie_xclk_clamp(true);
  542. }
  543. static int tegra_pcie_power_regate(void)
  544. {
  545. int err;
  546. tegra_pcie_power_off();
  547. tegra_pcie_xclk_clamp(true);
  548. tegra_periph_reset_assert(tegra_pcie.pcie_xclk);
  549. tegra_periph_reset_assert(tegra_pcie.afi_clk);
  550. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
  551. tegra_pcie.pex_clk);
  552. if (err) {
  553. pr_err("PCIE: powerup sequence failed: %d\n", err);
  554. return err;
  555. }
  556. tegra_periph_reset_deassert(tegra_pcie.afi_clk);
  557. tegra_pcie_xclk_clamp(false);
  558. clk_prepare_enable(tegra_pcie.afi_clk);
  559. clk_prepare_enable(tegra_pcie.pex_clk);
  560. return clk_prepare_enable(tegra_pcie.pll_e);
  561. }
  562. static int tegra_pcie_clocks_get(void)
  563. {
  564. int err;
  565. tegra_pcie.pex_clk = clk_get(NULL, "pex");
  566. if (IS_ERR(tegra_pcie.pex_clk))
  567. return PTR_ERR(tegra_pcie.pex_clk);
  568. tegra_pcie.afi_clk = clk_get(NULL, "afi");
  569. if (IS_ERR(tegra_pcie.afi_clk)) {
  570. err = PTR_ERR(tegra_pcie.afi_clk);
  571. goto err_afi_clk;
  572. }
  573. tegra_pcie.pcie_xclk = clk_get(NULL, "pcie_xclk");
  574. if (IS_ERR(tegra_pcie.pcie_xclk)) {
  575. err = PTR_ERR(tegra_pcie.pcie_xclk);
  576. goto err_pcie_xclk;
  577. }
  578. tegra_pcie.pll_e = clk_get_sys(NULL, "pll_e");
  579. if (IS_ERR(tegra_pcie.pll_e)) {
  580. err = PTR_ERR(tegra_pcie.pll_e);
  581. goto err_pll_e;
  582. }
  583. return 0;
  584. err_pll_e:
  585. clk_put(tegra_pcie.pcie_xclk);
  586. err_pcie_xclk:
  587. clk_put(tegra_pcie.afi_clk);
  588. err_afi_clk:
  589. clk_put(tegra_pcie.pex_clk);
  590. return err;
  591. }
  592. static void tegra_pcie_clocks_put(void)
  593. {
  594. clk_put(tegra_pcie.pll_e);
  595. clk_put(tegra_pcie.pcie_xclk);
  596. clk_put(tegra_pcie.afi_clk);
  597. clk_put(tegra_pcie.pex_clk);
  598. }
  599. static int __init tegra_pcie_get_resources(void)
  600. {
  601. int err;
  602. err = tegra_pcie_clocks_get();
  603. if (err) {
  604. pr_err("PCIE: failed to get clocks: %d\n", err);
  605. return err;
  606. }
  607. err = tegra_pcie_power_regate();
  608. if (err) {
  609. pr_err("PCIE: failed to power up: %d\n", err);
  610. goto err_pwr_on;
  611. }
  612. tegra_pcie.regs = ioremap_nocache(TEGRA_PCIE_BASE, PCIE_IOMAP_SZ);
  613. if (tegra_pcie.regs == NULL) {
  614. pr_err("PCIE: Failed to map PCI/AFI registers\n");
  615. err = -ENOMEM;
  616. goto err_map_reg;
  617. }
  618. err = request_irq(INT_PCIE_INTR, tegra_pcie_isr,
  619. IRQF_SHARED, "PCIE", &tegra_pcie);
  620. if (err) {
  621. pr_err("PCIE: Failed to register IRQ: %d\n", err);
  622. goto err_req_io;
  623. }
  624. set_irq_flags(INT_PCIE_INTR, IRQF_VALID);
  625. return 0;
  626. err_req_io:
  627. iounmap(tegra_pcie.regs);
  628. err_map_reg:
  629. tegra_pcie_power_off();
  630. err_pwr_on:
  631. tegra_pcie_clocks_put();
  632. return err;
  633. }
  634. /*
  635. * FIXME: If there are no PCIe cards attached, then calling this function
  636. * can result in the increase of the bootup time as there are big timeout
  637. * loops.
  638. */
  639. #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
  640. static bool tegra_pcie_check_link(struct tegra_pcie_port *pp, int idx,
  641. u32 reset_reg)
  642. {
  643. u32 reg;
  644. int retries = 3;
  645. int timeout;
  646. do {
  647. timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
  648. while (timeout) {
  649. reg = readl(pp->base + RP_VEND_XP);
  650. if (reg & RP_VEND_XP_DL_UP)
  651. break;
  652. mdelay(1);
  653. timeout--;
  654. }
  655. if (!timeout) {
  656. pr_err("PCIE: port %d: link down, retrying\n", idx);
  657. goto retry;
  658. }
  659. timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
  660. while (timeout) {
  661. reg = readl(pp->base + RP_LINK_CONTROL_STATUS);
  662. if (reg & 0x20000000)
  663. return true;
  664. mdelay(1);
  665. timeout--;
  666. }
  667. retry:
  668. /* Pulse the PEX reset */
  669. reg = afi_readl(reset_reg) | AFI_PEX_CTRL_RST;
  670. afi_writel(reg, reset_reg);
  671. mdelay(1);
  672. reg = afi_readl(reset_reg) & ~AFI_PEX_CTRL_RST;
  673. afi_writel(reg, reset_reg);
  674. retries--;
  675. } while (retries);
  676. return false;
  677. }
  678. static void __init tegra_pcie_add_port(int index, u32 offset, u32 reset_reg)
  679. {
  680. struct tegra_pcie_port *pp;
  681. pp = tegra_pcie.port + tegra_pcie.num_ports;
  682. pp->index = -1;
  683. pp->base = tegra_pcie.regs + offset;
  684. pp->link_up = tegra_pcie_check_link(pp, index, reset_reg);
  685. if (!pp->link_up) {
  686. pp->base = NULL;
  687. printk(KERN_INFO "PCIE: port %d: link down, ignoring\n", index);
  688. return;
  689. }
  690. tegra_pcie.num_ports++;
  691. pp->index = index;
  692. pp->root_bus_nr = -1;
  693. memset(pp->res, 0, sizeof(pp->res));
  694. }
  695. int __init tegra_pcie_init(bool init_port0, bool init_port1)
  696. {
  697. int err;
  698. if (!(init_port0 || init_port1))
  699. return -ENODEV;
  700. pcibios_min_mem = 0;
  701. err = tegra_pcie_get_resources();
  702. if (err)
  703. return err;
  704. err = tegra_pcie_enable_controller();
  705. if (err)
  706. return err;
  707. /* setup the AFI address translations */
  708. tegra_pcie_setup_translations();
  709. if (init_port0)
  710. tegra_pcie_add_port(0, RP0_OFFSET, AFI_PEX0_CTRL);
  711. if (init_port1)
  712. tegra_pcie_add_port(1, RP1_OFFSET, AFI_PEX1_CTRL);
  713. pci_common_init(&tegra_pcie_hw);
  714. return 0;
  715. }