pcie.c 22 KB

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