pci-tegra.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  1. /*
  2. * PCIe host controller driver for Tegra SoCs
  3. *
  4. * Copyright (c) 2010, CompuLab, Ltd.
  5. * Author: Mike Rapoport <mike@compulab.co.il>
  6. *
  7. * Based on NVIDIA PCIe driver
  8. * Copyright (c) 2008-2009, NVIDIA Corporation.
  9. *
  10. * Bits taken from arch/arm/mach-dove/pcie.c
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20. * more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. */
  26. #include <linux/clk.h>
  27. #include <linux/delay.h>
  28. #include <linux/export.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/irq.h>
  31. #include <linux/irqdomain.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/msi.h>
  35. #include <linux/of_address.h>
  36. #include <linux/of_pci.h>
  37. #include <linux/of_platform.h>
  38. #include <linux/pci.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/reset.h>
  41. #include <linux/sizes.h>
  42. #include <linux/slab.h>
  43. #include <linux/tegra-cpuidle.h>
  44. #include <linux/tegra-powergate.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/regulator/consumer.h>
  47. #include <asm/mach/irq.h>
  48. #include <asm/mach/map.h>
  49. #include <asm/mach/pci.h>
  50. #define INT_PCI_MSI_NR (8 * 32)
  51. /* register definitions */
  52. #define AFI_AXI_BAR0_SZ 0x00
  53. #define AFI_AXI_BAR1_SZ 0x04
  54. #define AFI_AXI_BAR2_SZ 0x08
  55. #define AFI_AXI_BAR3_SZ 0x0c
  56. #define AFI_AXI_BAR4_SZ 0x10
  57. #define AFI_AXI_BAR5_SZ 0x14
  58. #define AFI_AXI_BAR0_START 0x18
  59. #define AFI_AXI_BAR1_START 0x1c
  60. #define AFI_AXI_BAR2_START 0x20
  61. #define AFI_AXI_BAR3_START 0x24
  62. #define AFI_AXI_BAR4_START 0x28
  63. #define AFI_AXI_BAR5_START 0x2c
  64. #define AFI_FPCI_BAR0 0x30
  65. #define AFI_FPCI_BAR1 0x34
  66. #define AFI_FPCI_BAR2 0x38
  67. #define AFI_FPCI_BAR3 0x3c
  68. #define AFI_FPCI_BAR4 0x40
  69. #define AFI_FPCI_BAR5 0x44
  70. #define AFI_CACHE_BAR0_SZ 0x48
  71. #define AFI_CACHE_BAR0_ST 0x4c
  72. #define AFI_CACHE_BAR1_SZ 0x50
  73. #define AFI_CACHE_BAR1_ST 0x54
  74. #define AFI_MSI_BAR_SZ 0x60
  75. #define AFI_MSI_FPCI_BAR_ST 0x64
  76. #define AFI_MSI_AXI_BAR_ST 0x68
  77. #define AFI_MSI_VEC0 0x6c
  78. #define AFI_MSI_VEC1 0x70
  79. #define AFI_MSI_VEC2 0x74
  80. #define AFI_MSI_VEC3 0x78
  81. #define AFI_MSI_VEC4 0x7c
  82. #define AFI_MSI_VEC5 0x80
  83. #define AFI_MSI_VEC6 0x84
  84. #define AFI_MSI_VEC7 0x88
  85. #define AFI_MSI_EN_VEC0 0x8c
  86. #define AFI_MSI_EN_VEC1 0x90
  87. #define AFI_MSI_EN_VEC2 0x94
  88. #define AFI_MSI_EN_VEC3 0x98
  89. #define AFI_MSI_EN_VEC4 0x9c
  90. #define AFI_MSI_EN_VEC5 0xa0
  91. #define AFI_MSI_EN_VEC6 0xa4
  92. #define AFI_MSI_EN_VEC7 0xa8
  93. #define AFI_CONFIGURATION 0xac
  94. #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
  95. #define AFI_FPCI_ERROR_MASKS 0xb0
  96. #define AFI_INTR_MASK 0xb4
  97. #define AFI_INTR_MASK_INT_MASK (1 << 0)
  98. #define AFI_INTR_MASK_MSI_MASK (1 << 8)
  99. #define AFI_INTR_CODE 0xb8
  100. #define AFI_INTR_CODE_MASK 0xf
  101. #define AFI_INTR_AXI_SLAVE_ERROR 1
  102. #define AFI_INTR_AXI_DECODE_ERROR 2
  103. #define AFI_INTR_TARGET_ABORT 3
  104. #define AFI_INTR_MASTER_ABORT 4
  105. #define AFI_INTR_INVALID_WRITE 5
  106. #define AFI_INTR_LEGACY 6
  107. #define AFI_INTR_FPCI_DECODE_ERROR 7
  108. #define AFI_INTR_SIGNATURE 0xbc
  109. #define AFI_UPPER_FPCI_ADDRESS 0xc0
  110. #define AFI_SM_INTR_ENABLE 0xc4
  111. #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
  112. #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
  113. #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
  114. #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
  115. #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
  116. #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
  117. #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
  118. #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
  119. #define AFI_AFI_INTR_ENABLE 0xc8
  120. #define AFI_INTR_EN_INI_SLVERR (1 << 0)
  121. #define AFI_INTR_EN_INI_DECERR (1 << 1)
  122. #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
  123. #define AFI_INTR_EN_TGT_DECERR (1 << 3)
  124. #define AFI_INTR_EN_TGT_WRERR (1 << 4)
  125. #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
  126. #define AFI_INTR_EN_AXI_DECERR (1 << 6)
  127. #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
  128. #define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
  129. #define AFI_PCIE_CONFIG 0x0f8
  130. #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
  131. #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
  132. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
  133. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
  134. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
  135. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
  136. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
  137. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
  138. #define AFI_FUSE 0x104
  139. #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
  140. #define AFI_PEX0_CTRL 0x110
  141. #define AFI_PEX1_CTRL 0x118
  142. #define AFI_PEX2_CTRL 0x128
  143. #define AFI_PEX_CTRL_RST (1 << 0)
  144. #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
  145. #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
  146. #define AFI_PEXBIAS_CTRL_0 0x168
  147. #define RP_VEND_XP 0x00000F00
  148. #define RP_VEND_XP_DL_UP (1 << 30)
  149. #define RP_LINK_CONTROL_STATUS 0x00000090
  150. #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
  151. #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
  152. #define PADS_CTL_SEL 0x0000009C
  153. #define PADS_CTL 0x000000A0
  154. #define PADS_CTL_IDDQ_1L (1 << 0)
  155. #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
  156. #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
  157. #define PADS_PLL_CTL_TEGRA20 0x000000B8
  158. #define PADS_PLL_CTL_TEGRA30 0x000000B4
  159. #define PADS_PLL_CTL_RST_B4SM (1 << 1)
  160. #define PADS_PLL_CTL_LOCKDET (1 << 8)
  161. #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
  162. #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
  163. #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
  164. #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
  165. #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
  166. #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
  167. #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
  168. #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
  169. #define PADS_REFCLK_CFG0 0x000000C8
  170. #define PADS_REFCLK_CFG1 0x000000CC
  171. /*
  172. * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
  173. * entries, one entry per PCIe port. These field definitions and desired
  174. * values aren't in the TRM, but do come from NVIDIA.
  175. */
  176. #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
  177. #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
  178. #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
  179. #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
  180. /* Default value provided by HW engineering is 0xfa5c */
  181. #define PADS_REFCLK_CFG_VALUE \
  182. ( \
  183. (0x17 << PADS_REFCLK_CFG_TERM_SHIFT) | \
  184. (0 << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
  185. (0xa << PADS_REFCLK_CFG_PREDI_SHIFT) | \
  186. (0xf << PADS_REFCLK_CFG_DRVI_SHIFT) \
  187. )
  188. struct tegra_msi {
  189. struct msi_chip chip;
  190. DECLARE_BITMAP(used, INT_PCI_MSI_NR);
  191. struct irq_domain *domain;
  192. unsigned long pages;
  193. struct mutex lock;
  194. int irq;
  195. };
  196. /* used to differentiate between Tegra SoC generations */
  197. struct tegra_pcie_soc_data {
  198. unsigned int num_ports;
  199. unsigned int msi_base_shift;
  200. u32 pads_pll_ctl;
  201. u32 tx_ref_sel;
  202. bool has_pex_clkreq_en;
  203. bool has_pex_bias_ctrl;
  204. bool has_intr_prsnt_sense;
  205. bool has_avdd_supply;
  206. bool has_cml_clk;
  207. };
  208. static inline struct tegra_msi *to_tegra_msi(struct msi_chip *chip)
  209. {
  210. return container_of(chip, struct tegra_msi, chip);
  211. }
  212. struct tegra_pcie {
  213. struct device *dev;
  214. void __iomem *pads;
  215. void __iomem *afi;
  216. int irq;
  217. struct list_head buses;
  218. struct resource *cs;
  219. struct resource io;
  220. struct resource mem;
  221. struct resource prefetch;
  222. struct resource busn;
  223. struct clk *pex_clk;
  224. struct clk *afi_clk;
  225. struct clk *pll_e;
  226. struct clk *cml_clk;
  227. struct reset_control *pex_rst;
  228. struct reset_control *afi_rst;
  229. struct reset_control *pcie_xrst;
  230. struct tegra_msi msi;
  231. struct list_head ports;
  232. unsigned int num_ports;
  233. u32 xbar_config;
  234. struct regulator *pex_clk_supply;
  235. struct regulator *vdd_supply;
  236. struct regulator *avdd_supply;
  237. const struct tegra_pcie_soc_data *soc_data;
  238. };
  239. struct tegra_pcie_port {
  240. struct tegra_pcie *pcie;
  241. struct list_head list;
  242. struct resource regs;
  243. void __iomem *base;
  244. unsigned int index;
  245. unsigned int lanes;
  246. };
  247. struct tegra_pcie_bus {
  248. struct vm_struct *area;
  249. struct list_head list;
  250. unsigned int nr;
  251. };
  252. static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
  253. {
  254. return sys->private_data;
  255. }
  256. static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
  257. unsigned long offset)
  258. {
  259. writel(value, pcie->afi + offset);
  260. }
  261. static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
  262. {
  263. return readl(pcie->afi + offset);
  264. }
  265. static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
  266. unsigned long offset)
  267. {
  268. writel(value, pcie->pads + offset);
  269. }
  270. static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
  271. {
  272. return readl(pcie->pads + offset);
  273. }
  274. /*
  275. * The configuration space mapping on Tegra is somewhat similar to the ECAM
  276. * defined by PCIe. However it deviates a bit in how the 4 bits for extended
  277. * register accesses are mapped:
  278. *
  279. * [27:24] extended register number
  280. * [23:16] bus number
  281. * [15:11] device number
  282. * [10: 8] function number
  283. * [ 7: 0] register number
  284. *
  285. * Mapping the whole extended configuration space would require 256 MiB of
  286. * virtual address space, only a small part of which will actually be used.
  287. * To work around this, a 1 MiB of virtual addresses are allocated per bus
  288. * when the bus is first accessed. When the physical range is mapped, the
  289. * the bus number bits are hidden so that the extended register number bits
  290. * appear as bits [19:16]. Therefore the virtual mapping looks like this:
  291. *
  292. * [19:16] extended register number
  293. * [15:11] device number
  294. * [10: 8] function number
  295. * [ 7: 0] register number
  296. *
  297. * This is achieved by stitching together 16 chunks of 64 KiB of physical
  298. * address space via the MMU.
  299. */
  300. static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
  301. {
  302. return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
  303. (PCI_FUNC(devfn) << 8) | (where & 0xfc);
  304. }
  305. static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
  306. unsigned int busnr)
  307. {
  308. pgprot_t prot = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_XN |
  309. L_PTE_MT_DEV_SHARED | L_PTE_SHARED;
  310. phys_addr_t cs = pcie->cs->start;
  311. struct tegra_pcie_bus *bus;
  312. unsigned int i;
  313. int err;
  314. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  315. if (!bus)
  316. return ERR_PTR(-ENOMEM);
  317. INIT_LIST_HEAD(&bus->list);
  318. bus->nr = busnr;
  319. /* allocate 1 MiB of virtual addresses */
  320. bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
  321. if (!bus->area) {
  322. err = -ENOMEM;
  323. goto free;
  324. }
  325. /* map each of the 16 chunks of 64 KiB each */
  326. for (i = 0; i < 16; i++) {
  327. unsigned long virt = (unsigned long)bus->area->addr +
  328. i * SZ_64K;
  329. phys_addr_t phys = cs + i * SZ_1M + busnr * SZ_64K;
  330. err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
  331. if (err < 0) {
  332. dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
  333. err);
  334. goto unmap;
  335. }
  336. }
  337. return bus;
  338. unmap:
  339. vunmap(bus->area->addr);
  340. free:
  341. kfree(bus);
  342. return ERR_PTR(err);
  343. }
  344. /*
  345. * Look up a virtual address mapping for the specified bus number. If no such
  346. * mapping exists, try to create one.
  347. */
  348. static void __iomem *tegra_pcie_bus_map(struct tegra_pcie *pcie,
  349. unsigned int busnr)
  350. {
  351. struct tegra_pcie_bus *bus;
  352. list_for_each_entry(bus, &pcie->buses, list)
  353. if (bus->nr == busnr)
  354. return (void __iomem *)bus->area->addr;
  355. bus = tegra_pcie_bus_alloc(pcie, busnr);
  356. if (IS_ERR(bus))
  357. return NULL;
  358. list_add_tail(&bus->list, &pcie->buses);
  359. return (void __iomem *)bus->area->addr;
  360. }
  361. static void __iomem *tegra_pcie_conf_address(struct pci_bus *bus,
  362. unsigned int devfn,
  363. int where)
  364. {
  365. struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
  366. void __iomem *addr = NULL;
  367. if (bus->number == 0) {
  368. unsigned int slot = PCI_SLOT(devfn);
  369. struct tegra_pcie_port *port;
  370. list_for_each_entry(port, &pcie->ports, list) {
  371. if (port->index + 1 == slot) {
  372. addr = port->base + (where & ~3);
  373. break;
  374. }
  375. }
  376. } else {
  377. addr = tegra_pcie_bus_map(pcie, bus->number);
  378. if (!addr) {
  379. dev_err(pcie->dev,
  380. "failed to map cfg. space for bus %u\n",
  381. bus->number);
  382. return NULL;
  383. }
  384. addr += tegra_pcie_conf_offset(devfn, where);
  385. }
  386. return addr;
  387. }
  388. static int tegra_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
  389. int where, int size, u32 *value)
  390. {
  391. void __iomem *addr;
  392. addr = tegra_pcie_conf_address(bus, devfn, where);
  393. if (!addr) {
  394. *value = 0xffffffff;
  395. return PCIBIOS_DEVICE_NOT_FOUND;
  396. }
  397. *value = readl(addr);
  398. if (size == 1)
  399. *value = (*value >> (8 * (where & 3))) & 0xff;
  400. else if (size == 2)
  401. *value = (*value >> (8 * (where & 3))) & 0xffff;
  402. return PCIBIOS_SUCCESSFUL;
  403. }
  404. static int tegra_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
  405. int where, int size, u32 value)
  406. {
  407. void __iomem *addr;
  408. u32 mask, tmp;
  409. addr = tegra_pcie_conf_address(bus, devfn, where);
  410. if (!addr)
  411. return PCIBIOS_DEVICE_NOT_FOUND;
  412. if (size == 4) {
  413. writel(value, addr);
  414. return PCIBIOS_SUCCESSFUL;
  415. }
  416. if (size == 2)
  417. mask = ~(0xffff << ((where & 0x3) * 8));
  418. else if (size == 1)
  419. mask = ~(0xff << ((where & 0x3) * 8));
  420. else
  421. return PCIBIOS_BAD_REGISTER_NUMBER;
  422. tmp = readl(addr) & mask;
  423. tmp |= value << ((where & 0x3) * 8);
  424. writel(tmp, addr);
  425. return PCIBIOS_SUCCESSFUL;
  426. }
  427. static struct pci_ops tegra_pcie_ops = {
  428. .read = tegra_pcie_read_conf,
  429. .write = tegra_pcie_write_conf,
  430. };
  431. static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
  432. {
  433. unsigned long ret = 0;
  434. switch (port->index) {
  435. case 0:
  436. ret = AFI_PEX0_CTRL;
  437. break;
  438. case 1:
  439. ret = AFI_PEX1_CTRL;
  440. break;
  441. case 2:
  442. ret = AFI_PEX2_CTRL;
  443. break;
  444. }
  445. return ret;
  446. }
  447. static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
  448. {
  449. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  450. unsigned long value;
  451. /* pulse reset signal */
  452. value = afi_readl(port->pcie, ctrl);
  453. value &= ~AFI_PEX_CTRL_RST;
  454. afi_writel(port->pcie, value, ctrl);
  455. usleep_range(1000, 2000);
  456. value = afi_readl(port->pcie, ctrl);
  457. value |= AFI_PEX_CTRL_RST;
  458. afi_writel(port->pcie, value, ctrl);
  459. }
  460. static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
  461. {
  462. const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
  463. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  464. unsigned long value;
  465. /* enable reference clock */
  466. value = afi_readl(port->pcie, ctrl);
  467. value |= AFI_PEX_CTRL_REFCLK_EN;
  468. if (soc->has_pex_clkreq_en)
  469. value |= AFI_PEX_CTRL_CLKREQ_EN;
  470. afi_writel(port->pcie, value, ctrl);
  471. tegra_pcie_port_reset(port);
  472. }
  473. static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
  474. {
  475. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  476. unsigned long value;
  477. /* assert port reset */
  478. value = afi_readl(port->pcie, ctrl);
  479. value &= ~AFI_PEX_CTRL_RST;
  480. afi_writel(port->pcie, value, ctrl);
  481. /* disable reference clock */
  482. value = afi_readl(port->pcie, ctrl);
  483. value &= ~AFI_PEX_CTRL_REFCLK_EN;
  484. afi_writel(port->pcie, value, ctrl);
  485. }
  486. static void tegra_pcie_port_free(struct tegra_pcie_port *port)
  487. {
  488. struct tegra_pcie *pcie = port->pcie;
  489. devm_iounmap(pcie->dev, port->base);
  490. devm_release_mem_region(pcie->dev, port->regs.start,
  491. resource_size(&port->regs));
  492. list_del(&port->list);
  493. devm_kfree(pcie->dev, port);
  494. }
  495. static void tegra_pcie_fixup_bridge(struct pci_dev *dev)
  496. {
  497. u16 reg;
  498. if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
  499. pci_read_config_word(dev, PCI_COMMAND, &reg);
  500. reg |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  501. PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
  502. pci_write_config_word(dev, PCI_COMMAND, reg);
  503. }
  504. }
  505. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_fixup_bridge);
  506. /* Tegra PCIE root complex wrongly reports device class */
  507. static void tegra_pcie_fixup_class(struct pci_dev *dev)
  508. {
  509. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  510. }
  511. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
  512. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
  513. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
  514. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
  515. /* Tegra PCIE requires relaxed ordering */
  516. static void tegra_pcie_relax_enable(struct pci_dev *dev)
  517. {
  518. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
  519. }
  520. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
  521. static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
  522. {
  523. struct tegra_pcie *pcie = sys_to_pcie(sys);
  524. pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
  525. pci_add_resource_offset(&sys->resources, &pcie->prefetch,
  526. sys->mem_offset);
  527. pci_add_resource(&sys->resources, &pcie->busn);
  528. pci_ioremap_io(nr * SZ_64K, pcie->io.start);
  529. return 1;
  530. }
  531. static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
  532. {
  533. struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
  534. tegra_cpuidle_pcie_irqs_in_use();
  535. return pcie->irq;
  536. }
  537. static void tegra_pcie_add_bus(struct pci_bus *bus)
  538. {
  539. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  540. struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
  541. bus->msi = &pcie->msi.chip;
  542. }
  543. }
  544. static struct pci_bus *tegra_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  545. {
  546. struct tegra_pcie *pcie = sys_to_pcie(sys);
  547. struct pci_bus *bus;
  548. bus = pci_create_root_bus(pcie->dev, sys->busnr, &tegra_pcie_ops, sys,
  549. &sys->resources);
  550. if (!bus)
  551. return NULL;
  552. pci_scan_child_bus(bus);
  553. return bus;
  554. }
  555. static irqreturn_t tegra_pcie_isr(int irq, void *arg)
  556. {
  557. const char *err_msg[] = {
  558. "Unknown",
  559. "AXI slave error",
  560. "AXI decode error",
  561. "Target abort",
  562. "Master abort",
  563. "Invalid write",
  564. "Response decoding error",
  565. "AXI response decoding error",
  566. "Transaction timeout",
  567. };
  568. struct tegra_pcie *pcie = arg;
  569. u32 code, signature;
  570. code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
  571. signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
  572. afi_writel(pcie, 0, AFI_INTR_CODE);
  573. if (code == AFI_INTR_LEGACY)
  574. return IRQ_NONE;
  575. if (code >= ARRAY_SIZE(err_msg))
  576. code = 0;
  577. /*
  578. * do not pollute kernel log with master abort reports since they
  579. * happen a lot during enumeration
  580. */
  581. if (code == AFI_INTR_MASTER_ABORT)
  582. dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
  583. signature);
  584. else
  585. dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
  586. signature);
  587. if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
  588. code == AFI_INTR_FPCI_DECODE_ERROR) {
  589. u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
  590. u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
  591. if (code == AFI_INTR_MASTER_ABORT)
  592. dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
  593. else
  594. dev_err(pcie->dev, " FPCI address: %10llx\n", address);
  595. }
  596. return IRQ_HANDLED;
  597. }
  598. /*
  599. * FPCI map is as follows:
  600. * - 0xfdfc000000: I/O space
  601. * - 0xfdfe000000: type 0 configuration space
  602. * - 0xfdff000000: type 1 configuration space
  603. * - 0xfe00000000: type 0 extended configuration space
  604. * - 0xfe10000000: type 1 extended configuration space
  605. */
  606. static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
  607. {
  608. u32 fpci_bar, size, axi_address;
  609. /* Bar 0: type 1 extended configuration space */
  610. fpci_bar = 0xfe100000;
  611. size = resource_size(pcie->cs);
  612. axi_address = pcie->cs->start;
  613. afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
  614. afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
  615. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
  616. /* Bar 1: downstream IO bar */
  617. fpci_bar = 0xfdfc0000;
  618. size = resource_size(&pcie->io);
  619. axi_address = pcie->io.start;
  620. afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
  621. afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
  622. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
  623. /* Bar 2: prefetchable memory BAR */
  624. fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
  625. size = resource_size(&pcie->prefetch);
  626. axi_address = pcie->prefetch.start;
  627. afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
  628. afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
  629. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
  630. /* Bar 3: non prefetchable memory BAR */
  631. fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
  632. size = resource_size(&pcie->mem);
  633. axi_address = pcie->mem.start;
  634. afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
  635. afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
  636. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
  637. /* NULL out the remaining BARs as they are not used */
  638. afi_writel(pcie, 0, AFI_AXI_BAR4_START);
  639. afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
  640. afi_writel(pcie, 0, AFI_FPCI_BAR4);
  641. afi_writel(pcie, 0, AFI_AXI_BAR5_START);
  642. afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
  643. afi_writel(pcie, 0, AFI_FPCI_BAR5);
  644. /* map all upstream transactions as uncached */
  645. afi_writel(pcie, PHYS_OFFSET, AFI_CACHE_BAR0_ST);
  646. afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
  647. afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
  648. afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
  649. /* MSI translations are setup only when needed */
  650. afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
  651. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  652. afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
  653. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  654. }
  655. static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
  656. {
  657. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  658. struct tegra_pcie_port *port;
  659. unsigned int timeout;
  660. unsigned long value;
  661. /* power down PCIe slot clock bias pad */
  662. if (soc->has_pex_bias_ctrl)
  663. afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
  664. /* configure mode and disable all ports */
  665. value = afi_readl(pcie, AFI_PCIE_CONFIG);
  666. value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
  667. value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
  668. list_for_each_entry(port, &pcie->ports, list)
  669. value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
  670. afi_writel(pcie, value, AFI_PCIE_CONFIG);
  671. value = afi_readl(pcie, AFI_FUSE);
  672. value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
  673. afi_writel(pcie, value, AFI_FUSE);
  674. /* initialize internal PHY, enable up to 16 PCIE lanes */
  675. pads_writel(pcie, 0x0, PADS_CTL_SEL);
  676. /* override IDDQ to 1 on all 4 lanes */
  677. value = pads_readl(pcie, PADS_CTL);
  678. value |= PADS_CTL_IDDQ_1L;
  679. pads_writel(pcie, value, PADS_CTL);
  680. /*
  681. * Set up PHY PLL inputs select PLLE output as refclock,
  682. * set TX ref sel to div10 (not div5).
  683. */
  684. value = pads_readl(pcie, soc->pads_pll_ctl);
  685. value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
  686. value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
  687. pads_writel(pcie, value, soc->pads_pll_ctl);
  688. /* take PLL out of reset */
  689. value = pads_readl(pcie, soc->pads_pll_ctl);
  690. value |= PADS_PLL_CTL_RST_B4SM;
  691. pads_writel(pcie, value, soc->pads_pll_ctl);
  692. /* Configure the reference clock driver */
  693. value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
  694. pads_writel(pcie, value, PADS_REFCLK_CFG0);
  695. if (soc->num_ports > 2)
  696. pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
  697. /* wait for the PLL to lock */
  698. timeout = 300;
  699. do {
  700. value = pads_readl(pcie, soc->pads_pll_ctl);
  701. usleep_range(1000, 2000);
  702. if (--timeout == 0) {
  703. pr_err("Tegra PCIe error: timeout waiting for PLL\n");
  704. return -EBUSY;
  705. }
  706. } while (!(value & PADS_PLL_CTL_LOCKDET));
  707. /* turn off IDDQ override */
  708. value = pads_readl(pcie, PADS_CTL);
  709. value &= ~PADS_CTL_IDDQ_1L;
  710. pads_writel(pcie, value, PADS_CTL);
  711. /* enable TX/RX data */
  712. value = pads_readl(pcie, PADS_CTL);
  713. value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
  714. pads_writel(pcie, value, PADS_CTL);
  715. /* take the PCIe interface module out of reset */
  716. reset_control_deassert(pcie->pcie_xrst);
  717. /* finally enable PCIe */
  718. value = afi_readl(pcie, AFI_CONFIGURATION);
  719. value |= AFI_CONFIGURATION_EN_FPCI;
  720. afi_writel(pcie, value, AFI_CONFIGURATION);
  721. value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
  722. AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
  723. AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
  724. if (soc->has_intr_prsnt_sense)
  725. value |= AFI_INTR_EN_PRSNT_SENSE;
  726. afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
  727. afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
  728. /* don't enable MSI for now, only when needed */
  729. afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
  730. /* disable all exceptions */
  731. afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
  732. return 0;
  733. }
  734. static void tegra_pcie_power_off(struct tegra_pcie *pcie)
  735. {
  736. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  737. int err;
  738. /* TODO: disable and unprepare clocks? */
  739. reset_control_assert(pcie->pcie_xrst);
  740. reset_control_assert(pcie->afi_rst);
  741. reset_control_assert(pcie->pex_rst);
  742. tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  743. if (soc->has_avdd_supply) {
  744. err = regulator_disable(pcie->avdd_supply);
  745. if (err < 0)
  746. dev_warn(pcie->dev,
  747. "failed to disable AVDD regulator: %d\n",
  748. err);
  749. }
  750. err = regulator_disable(pcie->pex_clk_supply);
  751. if (err < 0)
  752. dev_warn(pcie->dev, "failed to disable pex-clk regulator: %d\n",
  753. err);
  754. err = regulator_disable(pcie->vdd_supply);
  755. if (err < 0)
  756. dev_warn(pcie->dev, "failed to disable VDD regulator: %d\n",
  757. err);
  758. }
  759. static int tegra_pcie_power_on(struct tegra_pcie *pcie)
  760. {
  761. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  762. int err;
  763. reset_control_assert(pcie->pcie_xrst);
  764. reset_control_assert(pcie->afi_rst);
  765. reset_control_assert(pcie->pex_rst);
  766. tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  767. /* enable regulators */
  768. err = regulator_enable(pcie->vdd_supply);
  769. if (err < 0) {
  770. dev_err(pcie->dev, "failed to enable VDD regulator: %d\n", err);
  771. return err;
  772. }
  773. err = regulator_enable(pcie->pex_clk_supply);
  774. if (err < 0) {
  775. dev_err(pcie->dev, "failed to enable pex-clk regulator: %d\n",
  776. err);
  777. return err;
  778. }
  779. if (soc->has_avdd_supply) {
  780. err = regulator_enable(pcie->avdd_supply);
  781. if (err < 0) {
  782. dev_err(pcie->dev,
  783. "failed to enable AVDD regulator: %d\n",
  784. err);
  785. return err;
  786. }
  787. }
  788. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
  789. pcie->pex_clk,
  790. pcie->pex_rst);
  791. if (err) {
  792. dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
  793. return err;
  794. }
  795. reset_control_deassert(pcie->afi_rst);
  796. err = clk_prepare_enable(pcie->afi_clk);
  797. if (err < 0) {
  798. dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
  799. return err;
  800. }
  801. if (soc->has_cml_clk) {
  802. err = clk_prepare_enable(pcie->cml_clk);
  803. if (err < 0) {
  804. dev_err(pcie->dev, "failed to enable CML clock: %d\n",
  805. err);
  806. return err;
  807. }
  808. }
  809. err = clk_prepare_enable(pcie->pll_e);
  810. if (err < 0) {
  811. dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
  812. return err;
  813. }
  814. return 0;
  815. }
  816. static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
  817. {
  818. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  819. pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
  820. if (IS_ERR(pcie->pex_clk))
  821. return PTR_ERR(pcie->pex_clk);
  822. pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
  823. if (IS_ERR(pcie->afi_clk))
  824. return PTR_ERR(pcie->afi_clk);
  825. pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
  826. if (IS_ERR(pcie->pll_e))
  827. return PTR_ERR(pcie->pll_e);
  828. if (soc->has_cml_clk) {
  829. pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
  830. if (IS_ERR(pcie->cml_clk))
  831. return PTR_ERR(pcie->cml_clk);
  832. }
  833. return 0;
  834. }
  835. static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
  836. {
  837. pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
  838. if (IS_ERR(pcie->pex_rst))
  839. return PTR_ERR(pcie->pex_rst);
  840. pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
  841. if (IS_ERR(pcie->afi_rst))
  842. return PTR_ERR(pcie->afi_rst);
  843. pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
  844. if (IS_ERR(pcie->pcie_xrst))
  845. return PTR_ERR(pcie->pcie_xrst);
  846. return 0;
  847. }
  848. static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
  849. {
  850. struct platform_device *pdev = to_platform_device(pcie->dev);
  851. struct resource *pads, *afi, *res;
  852. int err;
  853. err = tegra_pcie_clocks_get(pcie);
  854. if (err) {
  855. dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
  856. return err;
  857. }
  858. err = tegra_pcie_resets_get(pcie);
  859. if (err) {
  860. dev_err(&pdev->dev, "failed to get resets: %d\n", err);
  861. return err;
  862. }
  863. err = tegra_pcie_power_on(pcie);
  864. if (err) {
  865. dev_err(&pdev->dev, "failed to power up: %d\n", err);
  866. return err;
  867. }
  868. pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
  869. pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
  870. if (IS_ERR(pcie->pads)) {
  871. err = PTR_ERR(pcie->pads);
  872. goto poweroff;
  873. }
  874. afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
  875. pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
  876. if (IS_ERR(pcie->afi)) {
  877. err = PTR_ERR(pcie->afi);
  878. goto poweroff;
  879. }
  880. /* request configuration space, but remap later, on demand */
  881. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
  882. if (!res) {
  883. err = -EADDRNOTAVAIL;
  884. goto poweroff;
  885. }
  886. pcie->cs = devm_request_mem_region(pcie->dev, res->start,
  887. resource_size(res), res->name);
  888. if (!pcie->cs) {
  889. err = -EADDRNOTAVAIL;
  890. goto poweroff;
  891. }
  892. /* request interrupt */
  893. err = platform_get_irq_byname(pdev, "intr");
  894. if (err < 0) {
  895. dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
  896. goto poweroff;
  897. }
  898. pcie->irq = err;
  899. err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
  900. if (err) {
  901. dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
  902. goto poweroff;
  903. }
  904. return 0;
  905. poweroff:
  906. tegra_pcie_power_off(pcie);
  907. return err;
  908. }
  909. static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
  910. {
  911. if (pcie->irq > 0)
  912. free_irq(pcie->irq, pcie);
  913. tegra_pcie_power_off(pcie);
  914. return 0;
  915. }
  916. static int tegra_msi_alloc(struct tegra_msi *chip)
  917. {
  918. int msi;
  919. mutex_lock(&chip->lock);
  920. msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
  921. if (msi < INT_PCI_MSI_NR)
  922. set_bit(msi, chip->used);
  923. else
  924. msi = -ENOSPC;
  925. mutex_unlock(&chip->lock);
  926. return msi;
  927. }
  928. static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
  929. {
  930. struct device *dev = chip->chip.dev;
  931. mutex_lock(&chip->lock);
  932. if (!test_bit(irq, chip->used))
  933. dev_err(dev, "trying to free unused MSI#%lu\n", irq);
  934. else
  935. clear_bit(irq, chip->used);
  936. mutex_unlock(&chip->lock);
  937. }
  938. static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
  939. {
  940. struct tegra_pcie *pcie = data;
  941. struct tegra_msi *msi = &pcie->msi;
  942. unsigned int i, processed = 0;
  943. for (i = 0; i < 8; i++) {
  944. unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
  945. while (reg) {
  946. unsigned int offset = find_first_bit(&reg, 32);
  947. unsigned int index = i * 32 + offset;
  948. unsigned int irq;
  949. /* clear the interrupt */
  950. afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
  951. irq = irq_find_mapping(msi->domain, index);
  952. if (irq) {
  953. if (test_bit(index, msi->used))
  954. generic_handle_irq(irq);
  955. else
  956. dev_info(pcie->dev, "unhandled MSI\n");
  957. } else {
  958. /*
  959. * that's weird who triggered this?
  960. * just clear it
  961. */
  962. dev_info(pcie->dev, "unexpected MSI\n");
  963. }
  964. /* see if there's any more pending in this vector */
  965. reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
  966. processed++;
  967. }
  968. }
  969. return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
  970. }
  971. static int tegra_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
  972. struct msi_desc *desc)
  973. {
  974. struct tegra_msi *msi = to_tegra_msi(chip);
  975. struct msi_msg msg;
  976. unsigned int irq;
  977. int hwirq;
  978. hwirq = tegra_msi_alloc(msi);
  979. if (hwirq < 0)
  980. return hwirq;
  981. irq = irq_create_mapping(msi->domain, hwirq);
  982. if (!irq)
  983. return -EINVAL;
  984. irq_set_msi_desc(irq, desc);
  985. msg.address_lo = virt_to_phys((void *)msi->pages);
  986. /* 32 bit address only */
  987. msg.address_hi = 0;
  988. msg.data = hwirq;
  989. write_msi_msg(irq, &msg);
  990. return 0;
  991. }
  992. static void tegra_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
  993. {
  994. struct tegra_msi *msi = to_tegra_msi(chip);
  995. struct irq_data *d = irq_get_irq_data(irq);
  996. tegra_msi_free(msi, d->hwirq);
  997. }
  998. static struct irq_chip tegra_msi_irq_chip = {
  999. .name = "Tegra PCIe MSI",
  1000. .irq_enable = unmask_msi_irq,
  1001. .irq_disable = mask_msi_irq,
  1002. .irq_mask = mask_msi_irq,
  1003. .irq_unmask = unmask_msi_irq,
  1004. };
  1005. static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
  1006. irq_hw_number_t hwirq)
  1007. {
  1008. irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
  1009. irq_set_chip_data(irq, domain->host_data);
  1010. set_irq_flags(irq, IRQF_VALID);
  1011. tegra_cpuidle_pcie_irqs_in_use();
  1012. return 0;
  1013. }
  1014. static const struct irq_domain_ops msi_domain_ops = {
  1015. .map = tegra_msi_map,
  1016. };
  1017. static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
  1018. {
  1019. struct platform_device *pdev = to_platform_device(pcie->dev);
  1020. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  1021. struct tegra_msi *msi = &pcie->msi;
  1022. unsigned long base;
  1023. int err;
  1024. u32 reg;
  1025. mutex_init(&msi->lock);
  1026. msi->chip.dev = pcie->dev;
  1027. msi->chip.setup_irq = tegra_msi_setup_irq;
  1028. msi->chip.teardown_irq = tegra_msi_teardown_irq;
  1029. msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
  1030. &msi_domain_ops, &msi->chip);
  1031. if (!msi->domain) {
  1032. dev_err(&pdev->dev, "failed to create IRQ domain\n");
  1033. return -ENOMEM;
  1034. }
  1035. err = platform_get_irq_byname(pdev, "msi");
  1036. if (err < 0) {
  1037. dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
  1038. goto err;
  1039. }
  1040. msi->irq = err;
  1041. err = request_irq(msi->irq, tegra_pcie_msi_irq, 0,
  1042. tegra_msi_irq_chip.name, pcie);
  1043. if (err < 0) {
  1044. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  1045. goto err;
  1046. }
  1047. /* setup AFI/FPCI range */
  1048. msi->pages = __get_free_pages(GFP_KERNEL, 0);
  1049. base = virt_to_phys((void *)msi->pages);
  1050. afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
  1051. afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
  1052. /* this register is in 4K increments */
  1053. afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
  1054. /* enable all MSI vectors */
  1055. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
  1056. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
  1057. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
  1058. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
  1059. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
  1060. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
  1061. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
  1062. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
  1063. /* and unmask the MSI interrupt */
  1064. reg = afi_readl(pcie, AFI_INTR_MASK);
  1065. reg |= AFI_INTR_MASK_MSI_MASK;
  1066. afi_writel(pcie, reg, AFI_INTR_MASK);
  1067. return 0;
  1068. err:
  1069. irq_domain_remove(msi->domain);
  1070. return err;
  1071. }
  1072. static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
  1073. {
  1074. struct tegra_msi *msi = &pcie->msi;
  1075. unsigned int i, irq;
  1076. u32 value;
  1077. /* mask the MSI interrupt */
  1078. value = afi_readl(pcie, AFI_INTR_MASK);
  1079. value &= ~AFI_INTR_MASK_MSI_MASK;
  1080. afi_writel(pcie, value, AFI_INTR_MASK);
  1081. /* disable all MSI vectors */
  1082. afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
  1083. afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
  1084. afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
  1085. afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
  1086. afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
  1087. afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
  1088. afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
  1089. afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
  1090. free_pages(msi->pages, 0);
  1091. if (msi->irq > 0)
  1092. free_irq(msi->irq, pcie);
  1093. for (i = 0; i < INT_PCI_MSI_NR; i++) {
  1094. irq = irq_find_mapping(msi->domain, i);
  1095. if (irq > 0)
  1096. irq_dispose_mapping(irq);
  1097. }
  1098. irq_domain_remove(msi->domain);
  1099. return 0;
  1100. }
  1101. static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
  1102. u32 *xbar)
  1103. {
  1104. struct device_node *np = pcie->dev->of_node;
  1105. if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
  1106. switch (lanes) {
  1107. case 0x00000204:
  1108. dev_info(pcie->dev, "4x1, 2x1 configuration\n");
  1109. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
  1110. return 0;
  1111. case 0x00020202:
  1112. dev_info(pcie->dev, "2x3 configuration\n");
  1113. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
  1114. return 0;
  1115. case 0x00010104:
  1116. dev_info(pcie->dev, "4x1, 1x2 configuration\n");
  1117. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
  1118. return 0;
  1119. }
  1120. } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
  1121. switch (lanes) {
  1122. case 0x00000004:
  1123. dev_info(pcie->dev, "single-mode configuration\n");
  1124. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
  1125. return 0;
  1126. case 0x00000202:
  1127. dev_info(pcie->dev, "dual-mode configuration\n");
  1128. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
  1129. return 0;
  1130. }
  1131. }
  1132. return -EINVAL;
  1133. }
  1134. static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
  1135. {
  1136. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  1137. struct device_node *np = pcie->dev->of_node, *port;
  1138. struct of_pci_range_parser parser;
  1139. struct of_pci_range range;
  1140. struct resource res;
  1141. u32 lanes = 0;
  1142. int err;
  1143. if (of_pci_range_parser_init(&parser, np)) {
  1144. dev_err(pcie->dev, "missing \"ranges\" property\n");
  1145. return -EINVAL;
  1146. }
  1147. pcie->vdd_supply = devm_regulator_get(pcie->dev, "vdd");
  1148. if (IS_ERR(pcie->vdd_supply))
  1149. return PTR_ERR(pcie->vdd_supply);
  1150. pcie->pex_clk_supply = devm_regulator_get(pcie->dev, "pex-clk");
  1151. if (IS_ERR(pcie->pex_clk_supply))
  1152. return PTR_ERR(pcie->pex_clk_supply);
  1153. if (soc->has_avdd_supply) {
  1154. pcie->avdd_supply = devm_regulator_get(pcie->dev, "avdd");
  1155. if (IS_ERR(pcie->avdd_supply))
  1156. return PTR_ERR(pcie->avdd_supply);
  1157. }
  1158. for_each_of_pci_range(&parser, &range) {
  1159. of_pci_range_to_resource(&range, np, &res);
  1160. switch (res.flags & IORESOURCE_TYPE_BITS) {
  1161. case IORESOURCE_IO:
  1162. memcpy(&pcie->io, &res, sizeof(res));
  1163. pcie->io.name = "I/O";
  1164. break;
  1165. case IORESOURCE_MEM:
  1166. if (res.flags & IORESOURCE_PREFETCH) {
  1167. memcpy(&pcie->prefetch, &res, sizeof(res));
  1168. pcie->prefetch.name = "PREFETCH";
  1169. } else {
  1170. memcpy(&pcie->mem, &res, sizeof(res));
  1171. pcie->mem.name = "MEM";
  1172. }
  1173. break;
  1174. }
  1175. }
  1176. err = of_pci_parse_bus_range(np, &pcie->busn);
  1177. if (err < 0) {
  1178. dev_err(pcie->dev, "failed to parse ranges property: %d\n",
  1179. err);
  1180. pcie->busn.name = np->name;
  1181. pcie->busn.start = 0;
  1182. pcie->busn.end = 0xff;
  1183. pcie->busn.flags = IORESOURCE_BUS;
  1184. }
  1185. /* parse root ports */
  1186. for_each_child_of_node(np, port) {
  1187. struct tegra_pcie_port *rp;
  1188. unsigned int index;
  1189. u32 value;
  1190. err = of_pci_get_devfn(port);
  1191. if (err < 0) {
  1192. dev_err(pcie->dev, "failed to parse address: %d\n",
  1193. err);
  1194. return err;
  1195. }
  1196. index = PCI_SLOT(err);
  1197. if (index < 1 || index > soc->num_ports) {
  1198. dev_err(pcie->dev, "invalid port number: %d\n", index);
  1199. return -EINVAL;
  1200. }
  1201. index--;
  1202. err = of_property_read_u32(port, "nvidia,num-lanes", &value);
  1203. if (err < 0) {
  1204. dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
  1205. err);
  1206. return err;
  1207. }
  1208. if (value > 16) {
  1209. dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
  1210. return -EINVAL;
  1211. }
  1212. lanes |= value << (index << 3);
  1213. if (!of_device_is_available(port))
  1214. continue;
  1215. rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
  1216. if (!rp)
  1217. return -ENOMEM;
  1218. err = of_address_to_resource(port, 0, &rp->regs);
  1219. if (err < 0) {
  1220. dev_err(pcie->dev, "failed to parse address: %d\n",
  1221. err);
  1222. return err;
  1223. }
  1224. INIT_LIST_HEAD(&rp->list);
  1225. rp->index = index;
  1226. rp->lanes = value;
  1227. rp->pcie = pcie;
  1228. rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
  1229. if (IS_ERR(rp->base))
  1230. return PTR_ERR(rp->base);
  1231. list_add_tail(&rp->list, &pcie->ports);
  1232. }
  1233. err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
  1234. if (err < 0) {
  1235. dev_err(pcie->dev, "invalid lane configuration\n");
  1236. return err;
  1237. }
  1238. return 0;
  1239. }
  1240. /*
  1241. * FIXME: If there are no PCIe cards attached, then calling this function
  1242. * can result in the increase of the bootup time as there are big timeout
  1243. * loops.
  1244. */
  1245. #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
  1246. static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
  1247. {
  1248. unsigned int retries = 3;
  1249. unsigned long value;
  1250. do {
  1251. unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
  1252. do {
  1253. value = readl(port->base + RP_VEND_XP);
  1254. if (value & RP_VEND_XP_DL_UP)
  1255. break;
  1256. usleep_range(1000, 2000);
  1257. } while (--timeout);
  1258. if (!timeout) {
  1259. dev_err(port->pcie->dev, "link %u down, retrying\n",
  1260. port->index);
  1261. goto retry;
  1262. }
  1263. timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
  1264. do {
  1265. value = readl(port->base + RP_LINK_CONTROL_STATUS);
  1266. if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
  1267. return true;
  1268. usleep_range(1000, 2000);
  1269. } while (--timeout);
  1270. retry:
  1271. tegra_pcie_port_reset(port);
  1272. } while (--retries);
  1273. return false;
  1274. }
  1275. static int tegra_pcie_enable(struct tegra_pcie *pcie)
  1276. {
  1277. struct tegra_pcie_port *port, *tmp;
  1278. struct hw_pci hw;
  1279. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  1280. dev_info(pcie->dev, "probing port %u, using %u lanes\n",
  1281. port->index, port->lanes);
  1282. tegra_pcie_port_enable(port);
  1283. if (tegra_pcie_port_check_link(port))
  1284. continue;
  1285. dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
  1286. tegra_pcie_port_disable(port);
  1287. tegra_pcie_port_free(port);
  1288. }
  1289. memset(&hw, 0, sizeof(hw));
  1290. hw.nr_controllers = 1;
  1291. hw.private_data = (void **)&pcie;
  1292. hw.setup = tegra_pcie_setup;
  1293. hw.map_irq = tegra_pcie_map_irq;
  1294. hw.add_bus = tegra_pcie_add_bus;
  1295. hw.scan = tegra_pcie_scan_bus;
  1296. hw.ops = &tegra_pcie_ops;
  1297. pci_common_init_dev(pcie->dev, &hw);
  1298. return 0;
  1299. }
  1300. static const struct tegra_pcie_soc_data tegra20_pcie_data = {
  1301. .num_ports = 2,
  1302. .msi_base_shift = 0,
  1303. .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
  1304. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
  1305. .has_pex_clkreq_en = false,
  1306. .has_pex_bias_ctrl = false,
  1307. .has_intr_prsnt_sense = false,
  1308. .has_avdd_supply = false,
  1309. .has_cml_clk = false,
  1310. };
  1311. static const struct tegra_pcie_soc_data tegra30_pcie_data = {
  1312. .num_ports = 3,
  1313. .msi_base_shift = 8,
  1314. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  1315. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  1316. .has_pex_clkreq_en = true,
  1317. .has_pex_bias_ctrl = true,
  1318. .has_intr_prsnt_sense = true,
  1319. .has_avdd_supply = true,
  1320. .has_cml_clk = true,
  1321. };
  1322. static const struct of_device_id tegra_pcie_of_match[] = {
  1323. { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
  1324. { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
  1325. { },
  1326. };
  1327. MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
  1328. static int tegra_pcie_probe(struct platform_device *pdev)
  1329. {
  1330. const struct of_device_id *match;
  1331. struct tegra_pcie *pcie;
  1332. int err;
  1333. match = of_match_device(tegra_pcie_of_match, &pdev->dev);
  1334. if (!match)
  1335. return -ENODEV;
  1336. pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  1337. if (!pcie)
  1338. return -ENOMEM;
  1339. INIT_LIST_HEAD(&pcie->buses);
  1340. INIT_LIST_HEAD(&pcie->ports);
  1341. pcie->soc_data = match->data;
  1342. pcie->dev = &pdev->dev;
  1343. err = tegra_pcie_parse_dt(pcie);
  1344. if (err < 0)
  1345. return err;
  1346. pcibios_min_mem = 0;
  1347. err = tegra_pcie_get_resources(pcie);
  1348. if (err < 0) {
  1349. dev_err(&pdev->dev, "failed to request resources: %d\n", err);
  1350. return err;
  1351. }
  1352. err = tegra_pcie_enable_controller(pcie);
  1353. if (err)
  1354. goto put_resources;
  1355. /* setup the AFI address translations */
  1356. tegra_pcie_setup_translations(pcie);
  1357. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  1358. err = tegra_pcie_enable_msi(pcie);
  1359. if (err < 0) {
  1360. dev_err(&pdev->dev,
  1361. "failed to enable MSI support: %d\n",
  1362. err);
  1363. goto put_resources;
  1364. }
  1365. }
  1366. err = tegra_pcie_enable(pcie);
  1367. if (err < 0) {
  1368. dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
  1369. goto disable_msi;
  1370. }
  1371. platform_set_drvdata(pdev, pcie);
  1372. return 0;
  1373. disable_msi:
  1374. if (IS_ENABLED(CONFIG_PCI_MSI))
  1375. tegra_pcie_disable_msi(pcie);
  1376. put_resources:
  1377. tegra_pcie_put_resources(pcie);
  1378. return err;
  1379. }
  1380. static struct platform_driver tegra_pcie_driver = {
  1381. .driver = {
  1382. .name = "tegra-pcie",
  1383. .owner = THIS_MODULE,
  1384. .of_match_table = tegra_pcie_of_match,
  1385. .suppress_bind_attrs = true,
  1386. },
  1387. .probe = tegra_pcie_probe,
  1388. };
  1389. module_platform_driver(tegra_pcie_driver);
  1390. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  1391. MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
  1392. MODULE_LICENSE("GPLv2");