pci-tegra.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  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_cml_clk;
  206. };
  207. static inline struct tegra_msi *to_tegra_msi(struct msi_chip *chip)
  208. {
  209. return container_of(chip, struct tegra_msi, chip);
  210. }
  211. struct tegra_pcie {
  212. struct device *dev;
  213. void __iomem *pads;
  214. void __iomem *afi;
  215. int irq;
  216. struct list_head buses;
  217. struct resource *cs;
  218. struct resource io;
  219. struct resource mem;
  220. struct resource prefetch;
  221. struct resource busn;
  222. struct clk *pex_clk;
  223. struct clk *afi_clk;
  224. struct clk *pll_e;
  225. struct clk *cml_clk;
  226. struct reset_control *pex_rst;
  227. struct reset_control *afi_rst;
  228. struct reset_control *pcie_xrst;
  229. struct tegra_msi msi;
  230. struct list_head ports;
  231. unsigned int num_ports;
  232. u32 xbar_config;
  233. struct regulator_bulk_data *supplies;
  234. unsigned int num_supplies;
  235. const struct tegra_pcie_soc_data *soc_data;
  236. };
  237. struct tegra_pcie_port {
  238. struct tegra_pcie *pcie;
  239. struct list_head list;
  240. struct resource regs;
  241. void __iomem *base;
  242. unsigned int index;
  243. unsigned int lanes;
  244. };
  245. struct tegra_pcie_bus {
  246. struct vm_struct *area;
  247. struct list_head list;
  248. unsigned int nr;
  249. };
  250. static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
  251. {
  252. return sys->private_data;
  253. }
  254. static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
  255. unsigned long offset)
  256. {
  257. writel(value, pcie->afi + offset);
  258. }
  259. static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
  260. {
  261. return readl(pcie->afi + offset);
  262. }
  263. static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
  264. unsigned long offset)
  265. {
  266. writel(value, pcie->pads + offset);
  267. }
  268. static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
  269. {
  270. return readl(pcie->pads + offset);
  271. }
  272. /*
  273. * The configuration space mapping on Tegra is somewhat similar to the ECAM
  274. * defined by PCIe. However it deviates a bit in how the 4 bits for extended
  275. * register accesses are mapped:
  276. *
  277. * [27:24] extended register number
  278. * [23:16] bus number
  279. * [15:11] device number
  280. * [10: 8] function number
  281. * [ 7: 0] register number
  282. *
  283. * Mapping the whole extended configuration space would require 256 MiB of
  284. * virtual address space, only a small part of which will actually be used.
  285. * To work around this, a 1 MiB of virtual addresses are allocated per bus
  286. * when the bus is first accessed. When the physical range is mapped, the
  287. * the bus number bits are hidden so that the extended register number bits
  288. * appear as bits [19:16]. Therefore the virtual mapping looks like this:
  289. *
  290. * [19:16] extended register number
  291. * [15:11] device number
  292. * [10: 8] function number
  293. * [ 7: 0] register number
  294. *
  295. * This is achieved by stitching together 16 chunks of 64 KiB of physical
  296. * address space via the MMU.
  297. */
  298. static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
  299. {
  300. return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
  301. (PCI_FUNC(devfn) << 8) | (where & 0xfc);
  302. }
  303. static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
  304. unsigned int busnr)
  305. {
  306. pgprot_t prot = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_XN |
  307. L_PTE_MT_DEV_SHARED | L_PTE_SHARED;
  308. phys_addr_t cs = pcie->cs->start;
  309. struct tegra_pcie_bus *bus;
  310. unsigned int i;
  311. int err;
  312. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  313. if (!bus)
  314. return ERR_PTR(-ENOMEM);
  315. INIT_LIST_HEAD(&bus->list);
  316. bus->nr = busnr;
  317. /* allocate 1 MiB of virtual addresses */
  318. bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
  319. if (!bus->area) {
  320. err = -ENOMEM;
  321. goto free;
  322. }
  323. /* map each of the 16 chunks of 64 KiB each */
  324. for (i = 0; i < 16; i++) {
  325. unsigned long virt = (unsigned long)bus->area->addr +
  326. i * SZ_64K;
  327. phys_addr_t phys = cs + i * SZ_1M + busnr * SZ_64K;
  328. err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
  329. if (err < 0) {
  330. dev_err(pcie->dev, "ioremap_page_range() failed: %d\n",
  331. err);
  332. goto unmap;
  333. }
  334. }
  335. return bus;
  336. unmap:
  337. vunmap(bus->area->addr);
  338. free:
  339. kfree(bus);
  340. return ERR_PTR(err);
  341. }
  342. /*
  343. * Look up a virtual address mapping for the specified bus number. If no such
  344. * mapping exists, try to create one.
  345. */
  346. static void __iomem *tegra_pcie_bus_map(struct tegra_pcie *pcie,
  347. unsigned int busnr)
  348. {
  349. struct tegra_pcie_bus *bus;
  350. list_for_each_entry(bus, &pcie->buses, list)
  351. if (bus->nr == busnr)
  352. return (void __iomem *)bus->area->addr;
  353. bus = tegra_pcie_bus_alloc(pcie, busnr);
  354. if (IS_ERR(bus))
  355. return NULL;
  356. list_add_tail(&bus->list, &pcie->buses);
  357. return (void __iomem *)bus->area->addr;
  358. }
  359. static void __iomem *tegra_pcie_conf_address(struct pci_bus *bus,
  360. unsigned int devfn,
  361. int where)
  362. {
  363. struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
  364. void __iomem *addr = NULL;
  365. if (bus->number == 0) {
  366. unsigned int slot = PCI_SLOT(devfn);
  367. struct tegra_pcie_port *port;
  368. list_for_each_entry(port, &pcie->ports, list) {
  369. if (port->index + 1 == slot) {
  370. addr = port->base + (where & ~3);
  371. break;
  372. }
  373. }
  374. } else {
  375. addr = tegra_pcie_bus_map(pcie, bus->number);
  376. if (!addr) {
  377. dev_err(pcie->dev,
  378. "failed to map cfg. space for bus %u\n",
  379. bus->number);
  380. return NULL;
  381. }
  382. addr += tegra_pcie_conf_offset(devfn, where);
  383. }
  384. return addr;
  385. }
  386. static int tegra_pcie_read_conf(struct pci_bus *bus, unsigned int devfn,
  387. int where, int size, u32 *value)
  388. {
  389. void __iomem *addr;
  390. addr = tegra_pcie_conf_address(bus, devfn, where);
  391. if (!addr) {
  392. *value = 0xffffffff;
  393. return PCIBIOS_DEVICE_NOT_FOUND;
  394. }
  395. *value = readl(addr);
  396. if (size == 1)
  397. *value = (*value >> (8 * (where & 3))) & 0xff;
  398. else if (size == 2)
  399. *value = (*value >> (8 * (where & 3))) & 0xffff;
  400. return PCIBIOS_SUCCESSFUL;
  401. }
  402. static int tegra_pcie_write_conf(struct pci_bus *bus, unsigned int devfn,
  403. int where, int size, u32 value)
  404. {
  405. void __iomem *addr;
  406. u32 mask, tmp;
  407. addr = tegra_pcie_conf_address(bus, devfn, where);
  408. if (!addr)
  409. return PCIBIOS_DEVICE_NOT_FOUND;
  410. if (size == 4) {
  411. writel(value, addr);
  412. return PCIBIOS_SUCCESSFUL;
  413. }
  414. if (size == 2)
  415. mask = ~(0xffff << ((where & 0x3) * 8));
  416. else if (size == 1)
  417. mask = ~(0xff << ((where & 0x3) * 8));
  418. else
  419. return PCIBIOS_BAD_REGISTER_NUMBER;
  420. tmp = readl(addr) & mask;
  421. tmp |= value << ((where & 0x3) * 8);
  422. writel(tmp, addr);
  423. return PCIBIOS_SUCCESSFUL;
  424. }
  425. static struct pci_ops tegra_pcie_ops = {
  426. .read = tegra_pcie_read_conf,
  427. .write = tegra_pcie_write_conf,
  428. };
  429. static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
  430. {
  431. unsigned long ret = 0;
  432. switch (port->index) {
  433. case 0:
  434. ret = AFI_PEX0_CTRL;
  435. break;
  436. case 1:
  437. ret = AFI_PEX1_CTRL;
  438. break;
  439. case 2:
  440. ret = AFI_PEX2_CTRL;
  441. break;
  442. }
  443. return ret;
  444. }
  445. static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
  446. {
  447. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  448. unsigned long value;
  449. /* pulse reset signal */
  450. value = afi_readl(port->pcie, ctrl);
  451. value &= ~AFI_PEX_CTRL_RST;
  452. afi_writel(port->pcie, value, ctrl);
  453. usleep_range(1000, 2000);
  454. value = afi_readl(port->pcie, ctrl);
  455. value |= AFI_PEX_CTRL_RST;
  456. afi_writel(port->pcie, value, ctrl);
  457. }
  458. static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
  459. {
  460. const struct tegra_pcie_soc_data *soc = port->pcie->soc_data;
  461. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  462. unsigned long value;
  463. /* enable reference clock */
  464. value = afi_readl(port->pcie, ctrl);
  465. value |= AFI_PEX_CTRL_REFCLK_EN;
  466. if (soc->has_pex_clkreq_en)
  467. value |= AFI_PEX_CTRL_CLKREQ_EN;
  468. afi_writel(port->pcie, value, ctrl);
  469. tegra_pcie_port_reset(port);
  470. }
  471. static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
  472. {
  473. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  474. unsigned long value;
  475. /* assert port reset */
  476. value = afi_readl(port->pcie, ctrl);
  477. value &= ~AFI_PEX_CTRL_RST;
  478. afi_writel(port->pcie, value, ctrl);
  479. /* disable reference clock */
  480. value = afi_readl(port->pcie, ctrl);
  481. value &= ~AFI_PEX_CTRL_REFCLK_EN;
  482. afi_writel(port->pcie, value, ctrl);
  483. }
  484. static void tegra_pcie_port_free(struct tegra_pcie_port *port)
  485. {
  486. struct tegra_pcie *pcie = port->pcie;
  487. devm_iounmap(pcie->dev, port->base);
  488. devm_release_mem_region(pcie->dev, port->regs.start,
  489. resource_size(&port->regs));
  490. list_del(&port->list);
  491. devm_kfree(pcie->dev, port);
  492. }
  493. static void tegra_pcie_fixup_bridge(struct pci_dev *dev)
  494. {
  495. u16 reg;
  496. if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
  497. pci_read_config_word(dev, PCI_COMMAND, &reg);
  498. reg |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  499. PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
  500. pci_write_config_word(dev, PCI_COMMAND, reg);
  501. }
  502. }
  503. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_fixup_bridge);
  504. /* Tegra PCIE root complex wrongly reports device class */
  505. static void tegra_pcie_fixup_class(struct pci_dev *dev)
  506. {
  507. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  508. }
  509. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
  510. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
  511. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
  512. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
  513. /* Tegra PCIE requires relaxed ordering */
  514. static void tegra_pcie_relax_enable(struct pci_dev *dev)
  515. {
  516. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
  517. }
  518. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
  519. static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
  520. {
  521. struct tegra_pcie *pcie = sys_to_pcie(sys);
  522. pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
  523. pci_add_resource_offset(&sys->resources, &pcie->prefetch,
  524. sys->mem_offset);
  525. pci_add_resource(&sys->resources, &pcie->busn);
  526. pci_ioremap_io(nr * SZ_64K, pcie->io.start);
  527. return 1;
  528. }
  529. static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
  530. {
  531. struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
  532. int irq;
  533. tegra_cpuidle_pcie_irqs_in_use();
  534. irq = of_irq_parse_and_map_pci(pdev, slot, pin);
  535. if (!irq)
  536. irq = pcie->irq;
  537. return irq;
  538. }
  539. static void tegra_pcie_add_bus(struct pci_bus *bus)
  540. {
  541. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  542. struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
  543. bus->msi = &pcie->msi.chip;
  544. }
  545. }
  546. static struct pci_bus *tegra_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  547. {
  548. struct tegra_pcie *pcie = sys_to_pcie(sys);
  549. struct pci_bus *bus;
  550. bus = pci_create_root_bus(pcie->dev, sys->busnr, &tegra_pcie_ops, sys,
  551. &sys->resources);
  552. if (!bus)
  553. return NULL;
  554. pci_scan_child_bus(bus);
  555. return bus;
  556. }
  557. static irqreturn_t tegra_pcie_isr(int irq, void *arg)
  558. {
  559. const char *err_msg[] = {
  560. "Unknown",
  561. "AXI slave error",
  562. "AXI decode error",
  563. "Target abort",
  564. "Master abort",
  565. "Invalid write",
  566. "Response decoding error",
  567. "AXI response decoding error",
  568. "Transaction timeout",
  569. };
  570. struct tegra_pcie *pcie = arg;
  571. u32 code, signature;
  572. code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
  573. signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
  574. afi_writel(pcie, 0, AFI_INTR_CODE);
  575. if (code == AFI_INTR_LEGACY)
  576. return IRQ_NONE;
  577. if (code >= ARRAY_SIZE(err_msg))
  578. code = 0;
  579. /*
  580. * do not pollute kernel log with master abort reports since they
  581. * happen a lot during enumeration
  582. */
  583. if (code == AFI_INTR_MASTER_ABORT)
  584. dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
  585. signature);
  586. else
  587. dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
  588. signature);
  589. if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
  590. code == AFI_INTR_FPCI_DECODE_ERROR) {
  591. u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
  592. u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
  593. if (code == AFI_INTR_MASTER_ABORT)
  594. dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
  595. else
  596. dev_err(pcie->dev, " FPCI address: %10llx\n", address);
  597. }
  598. return IRQ_HANDLED;
  599. }
  600. /*
  601. * FPCI map is as follows:
  602. * - 0xfdfc000000: I/O space
  603. * - 0xfdfe000000: type 0 configuration space
  604. * - 0xfdff000000: type 1 configuration space
  605. * - 0xfe00000000: type 0 extended configuration space
  606. * - 0xfe10000000: type 1 extended configuration space
  607. */
  608. static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
  609. {
  610. u32 fpci_bar, size, axi_address;
  611. /* Bar 0: type 1 extended configuration space */
  612. fpci_bar = 0xfe100000;
  613. size = resource_size(pcie->cs);
  614. axi_address = pcie->cs->start;
  615. afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
  616. afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
  617. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
  618. /* Bar 1: downstream IO bar */
  619. fpci_bar = 0xfdfc0000;
  620. size = resource_size(&pcie->io);
  621. axi_address = pcie->io.start;
  622. afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
  623. afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
  624. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
  625. /* Bar 2: prefetchable memory BAR */
  626. fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
  627. size = resource_size(&pcie->prefetch);
  628. axi_address = pcie->prefetch.start;
  629. afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
  630. afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
  631. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
  632. /* Bar 3: non prefetchable memory BAR */
  633. fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
  634. size = resource_size(&pcie->mem);
  635. axi_address = pcie->mem.start;
  636. afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
  637. afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
  638. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
  639. /* NULL out the remaining BARs as they are not used */
  640. afi_writel(pcie, 0, AFI_AXI_BAR4_START);
  641. afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
  642. afi_writel(pcie, 0, AFI_FPCI_BAR4);
  643. afi_writel(pcie, 0, AFI_AXI_BAR5_START);
  644. afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
  645. afi_writel(pcie, 0, AFI_FPCI_BAR5);
  646. /* map all upstream transactions as uncached */
  647. afi_writel(pcie, PHYS_OFFSET, AFI_CACHE_BAR0_ST);
  648. afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
  649. afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
  650. afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
  651. /* MSI translations are setup only when needed */
  652. afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
  653. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  654. afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
  655. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  656. }
  657. static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
  658. {
  659. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  660. struct tegra_pcie_port *port;
  661. unsigned int timeout;
  662. unsigned long value;
  663. /* power down PCIe slot clock bias pad */
  664. if (soc->has_pex_bias_ctrl)
  665. afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
  666. /* configure mode and disable all ports */
  667. value = afi_readl(pcie, AFI_PCIE_CONFIG);
  668. value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
  669. value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
  670. list_for_each_entry(port, &pcie->ports, list)
  671. value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
  672. afi_writel(pcie, value, AFI_PCIE_CONFIG);
  673. value = afi_readl(pcie, AFI_FUSE);
  674. value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
  675. afi_writel(pcie, value, AFI_FUSE);
  676. /* initialize internal PHY, enable up to 16 PCIE lanes */
  677. pads_writel(pcie, 0x0, PADS_CTL_SEL);
  678. /* override IDDQ to 1 on all 4 lanes */
  679. value = pads_readl(pcie, PADS_CTL);
  680. value |= PADS_CTL_IDDQ_1L;
  681. pads_writel(pcie, value, PADS_CTL);
  682. /*
  683. * Set up PHY PLL inputs select PLLE output as refclock,
  684. * set TX ref sel to div10 (not div5).
  685. */
  686. value = pads_readl(pcie, soc->pads_pll_ctl);
  687. value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
  688. value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
  689. pads_writel(pcie, value, soc->pads_pll_ctl);
  690. /* take PLL out of reset */
  691. value = pads_readl(pcie, soc->pads_pll_ctl);
  692. value |= PADS_PLL_CTL_RST_B4SM;
  693. pads_writel(pcie, value, soc->pads_pll_ctl);
  694. /* Configure the reference clock driver */
  695. value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
  696. pads_writel(pcie, value, PADS_REFCLK_CFG0);
  697. if (soc->num_ports > 2)
  698. pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
  699. /* wait for the PLL to lock */
  700. timeout = 300;
  701. do {
  702. value = pads_readl(pcie, soc->pads_pll_ctl);
  703. usleep_range(1000, 2000);
  704. if (--timeout == 0) {
  705. pr_err("Tegra PCIe error: timeout waiting for PLL\n");
  706. return -EBUSY;
  707. }
  708. } while (!(value & PADS_PLL_CTL_LOCKDET));
  709. /* turn off IDDQ override */
  710. value = pads_readl(pcie, PADS_CTL);
  711. value &= ~PADS_CTL_IDDQ_1L;
  712. pads_writel(pcie, value, PADS_CTL);
  713. /* enable TX/RX data */
  714. value = pads_readl(pcie, PADS_CTL);
  715. value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
  716. pads_writel(pcie, value, PADS_CTL);
  717. /* take the PCIe interface module out of reset */
  718. reset_control_deassert(pcie->pcie_xrst);
  719. /* finally enable PCIe */
  720. value = afi_readl(pcie, AFI_CONFIGURATION);
  721. value |= AFI_CONFIGURATION_EN_FPCI;
  722. afi_writel(pcie, value, AFI_CONFIGURATION);
  723. value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
  724. AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
  725. AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
  726. if (soc->has_intr_prsnt_sense)
  727. value |= AFI_INTR_EN_PRSNT_SENSE;
  728. afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
  729. afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
  730. /* don't enable MSI for now, only when needed */
  731. afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
  732. /* disable all exceptions */
  733. afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
  734. return 0;
  735. }
  736. static void tegra_pcie_power_off(struct tegra_pcie *pcie)
  737. {
  738. int err;
  739. /* TODO: disable and unprepare clocks? */
  740. reset_control_assert(pcie->pcie_xrst);
  741. reset_control_assert(pcie->afi_rst);
  742. reset_control_assert(pcie->pex_rst);
  743. tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  744. err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
  745. if (err < 0)
  746. dev_warn(pcie->dev, "failed to disable regulators: %d\n", err);
  747. }
  748. static int tegra_pcie_power_on(struct tegra_pcie *pcie)
  749. {
  750. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  751. int err;
  752. reset_control_assert(pcie->pcie_xrst);
  753. reset_control_assert(pcie->afi_rst);
  754. reset_control_assert(pcie->pex_rst);
  755. tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  756. /* enable regulators */
  757. err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
  758. if (err < 0)
  759. dev_err(pcie->dev, "failed to enable regulators: %d\n", err);
  760. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
  761. pcie->pex_clk,
  762. pcie->pex_rst);
  763. if (err) {
  764. dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
  765. return err;
  766. }
  767. reset_control_deassert(pcie->afi_rst);
  768. err = clk_prepare_enable(pcie->afi_clk);
  769. if (err < 0) {
  770. dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
  771. return err;
  772. }
  773. if (soc->has_cml_clk) {
  774. err = clk_prepare_enable(pcie->cml_clk);
  775. if (err < 0) {
  776. dev_err(pcie->dev, "failed to enable CML clock: %d\n",
  777. err);
  778. return err;
  779. }
  780. }
  781. err = clk_prepare_enable(pcie->pll_e);
  782. if (err < 0) {
  783. dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
  784. return err;
  785. }
  786. return 0;
  787. }
  788. static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
  789. {
  790. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  791. pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
  792. if (IS_ERR(pcie->pex_clk))
  793. return PTR_ERR(pcie->pex_clk);
  794. pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
  795. if (IS_ERR(pcie->afi_clk))
  796. return PTR_ERR(pcie->afi_clk);
  797. pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
  798. if (IS_ERR(pcie->pll_e))
  799. return PTR_ERR(pcie->pll_e);
  800. if (soc->has_cml_clk) {
  801. pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
  802. if (IS_ERR(pcie->cml_clk))
  803. return PTR_ERR(pcie->cml_clk);
  804. }
  805. return 0;
  806. }
  807. static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
  808. {
  809. pcie->pex_rst = devm_reset_control_get(pcie->dev, "pex");
  810. if (IS_ERR(pcie->pex_rst))
  811. return PTR_ERR(pcie->pex_rst);
  812. pcie->afi_rst = devm_reset_control_get(pcie->dev, "afi");
  813. if (IS_ERR(pcie->afi_rst))
  814. return PTR_ERR(pcie->afi_rst);
  815. pcie->pcie_xrst = devm_reset_control_get(pcie->dev, "pcie_x");
  816. if (IS_ERR(pcie->pcie_xrst))
  817. return PTR_ERR(pcie->pcie_xrst);
  818. return 0;
  819. }
  820. static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
  821. {
  822. struct platform_device *pdev = to_platform_device(pcie->dev);
  823. struct resource *pads, *afi, *res;
  824. int err;
  825. err = tegra_pcie_clocks_get(pcie);
  826. if (err) {
  827. dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
  828. return err;
  829. }
  830. err = tegra_pcie_resets_get(pcie);
  831. if (err) {
  832. dev_err(&pdev->dev, "failed to get resets: %d\n", err);
  833. return err;
  834. }
  835. err = tegra_pcie_power_on(pcie);
  836. if (err) {
  837. dev_err(&pdev->dev, "failed to power up: %d\n", err);
  838. return err;
  839. }
  840. pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
  841. pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
  842. if (IS_ERR(pcie->pads)) {
  843. err = PTR_ERR(pcie->pads);
  844. goto poweroff;
  845. }
  846. afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
  847. pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
  848. if (IS_ERR(pcie->afi)) {
  849. err = PTR_ERR(pcie->afi);
  850. goto poweroff;
  851. }
  852. /* request configuration space, but remap later, on demand */
  853. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
  854. if (!res) {
  855. err = -EADDRNOTAVAIL;
  856. goto poweroff;
  857. }
  858. pcie->cs = devm_request_mem_region(pcie->dev, res->start,
  859. resource_size(res), res->name);
  860. if (!pcie->cs) {
  861. err = -EADDRNOTAVAIL;
  862. goto poweroff;
  863. }
  864. /* request interrupt */
  865. err = platform_get_irq_byname(pdev, "intr");
  866. if (err < 0) {
  867. dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
  868. goto poweroff;
  869. }
  870. pcie->irq = err;
  871. err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
  872. if (err) {
  873. dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
  874. goto poweroff;
  875. }
  876. return 0;
  877. poweroff:
  878. tegra_pcie_power_off(pcie);
  879. return err;
  880. }
  881. static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
  882. {
  883. if (pcie->irq > 0)
  884. free_irq(pcie->irq, pcie);
  885. tegra_pcie_power_off(pcie);
  886. return 0;
  887. }
  888. static int tegra_msi_alloc(struct tegra_msi *chip)
  889. {
  890. int msi;
  891. mutex_lock(&chip->lock);
  892. msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
  893. if (msi < INT_PCI_MSI_NR)
  894. set_bit(msi, chip->used);
  895. else
  896. msi = -ENOSPC;
  897. mutex_unlock(&chip->lock);
  898. return msi;
  899. }
  900. static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
  901. {
  902. struct device *dev = chip->chip.dev;
  903. mutex_lock(&chip->lock);
  904. if (!test_bit(irq, chip->used))
  905. dev_err(dev, "trying to free unused MSI#%lu\n", irq);
  906. else
  907. clear_bit(irq, chip->used);
  908. mutex_unlock(&chip->lock);
  909. }
  910. static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
  911. {
  912. struct tegra_pcie *pcie = data;
  913. struct tegra_msi *msi = &pcie->msi;
  914. unsigned int i, processed = 0;
  915. for (i = 0; i < 8; i++) {
  916. unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
  917. while (reg) {
  918. unsigned int offset = find_first_bit(&reg, 32);
  919. unsigned int index = i * 32 + offset;
  920. unsigned int irq;
  921. /* clear the interrupt */
  922. afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
  923. irq = irq_find_mapping(msi->domain, index);
  924. if (irq) {
  925. if (test_bit(index, msi->used))
  926. generic_handle_irq(irq);
  927. else
  928. dev_info(pcie->dev, "unhandled MSI\n");
  929. } else {
  930. /*
  931. * that's weird who triggered this?
  932. * just clear it
  933. */
  934. dev_info(pcie->dev, "unexpected MSI\n");
  935. }
  936. /* see if there's any more pending in this vector */
  937. reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
  938. processed++;
  939. }
  940. }
  941. return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
  942. }
  943. static int tegra_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
  944. struct msi_desc *desc)
  945. {
  946. struct tegra_msi *msi = to_tegra_msi(chip);
  947. struct msi_msg msg;
  948. unsigned int irq;
  949. int hwirq;
  950. hwirq = tegra_msi_alloc(msi);
  951. if (hwirq < 0)
  952. return hwirq;
  953. irq = irq_create_mapping(msi->domain, hwirq);
  954. if (!irq)
  955. return -EINVAL;
  956. irq_set_msi_desc(irq, desc);
  957. msg.address_lo = virt_to_phys((void *)msi->pages);
  958. /* 32 bit address only */
  959. msg.address_hi = 0;
  960. msg.data = hwirq;
  961. write_msi_msg(irq, &msg);
  962. return 0;
  963. }
  964. static void tegra_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
  965. {
  966. struct tegra_msi *msi = to_tegra_msi(chip);
  967. struct irq_data *d = irq_get_irq_data(irq);
  968. tegra_msi_free(msi, d->hwirq);
  969. }
  970. static struct irq_chip tegra_msi_irq_chip = {
  971. .name = "Tegra PCIe MSI",
  972. .irq_enable = unmask_msi_irq,
  973. .irq_disable = mask_msi_irq,
  974. .irq_mask = mask_msi_irq,
  975. .irq_unmask = unmask_msi_irq,
  976. };
  977. static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
  978. irq_hw_number_t hwirq)
  979. {
  980. irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
  981. irq_set_chip_data(irq, domain->host_data);
  982. set_irq_flags(irq, IRQF_VALID);
  983. tegra_cpuidle_pcie_irqs_in_use();
  984. return 0;
  985. }
  986. static const struct irq_domain_ops msi_domain_ops = {
  987. .map = tegra_msi_map,
  988. };
  989. static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
  990. {
  991. struct platform_device *pdev = to_platform_device(pcie->dev);
  992. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  993. struct tegra_msi *msi = &pcie->msi;
  994. unsigned long base;
  995. int err;
  996. u32 reg;
  997. mutex_init(&msi->lock);
  998. msi->chip.dev = pcie->dev;
  999. msi->chip.setup_irq = tegra_msi_setup_irq;
  1000. msi->chip.teardown_irq = tegra_msi_teardown_irq;
  1001. msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
  1002. &msi_domain_ops, &msi->chip);
  1003. if (!msi->domain) {
  1004. dev_err(&pdev->dev, "failed to create IRQ domain\n");
  1005. return -ENOMEM;
  1006. }
  1007. err = platform_get_irq_byname(pdev, "msi");
  1008. if (err < 0) {
  1009. dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
  1010. goto err;
  1011. }
  1012. msi->irq = err;
  1013. err = request_irq(msi->irq, tegra_pcie_msi_irq, 0,
  1014. tegra_msi_irq_chip.name, pcie);
  1015. if (err < 0) {
  1016. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  1017. goto err;
  1018. }
  1019. /* setup AFI/FPCI range */
  1020. msi->pages = __get_free_pages(GFP_KERNEL, 0);
  1021. base = virt_to_phys((void *)msi->pages);
  1022. afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
  1023. afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
  1024. /* this register is in 4K increments */
  1025. afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
  1026. /* enable all MSI vectors */
  1027. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
  1028. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
  1029. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
  1030. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
  1031. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
  1032. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
  1033. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
  1034. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
  1035. /* and unmask the MSI interrupt */
  1036. reg = afi_readl(pcie, AFI_INTR_MASK);
  1037. reg |= AFI_INTR_MASK_MSI_MASK;
  1038. afi_writel(pcie, reg, AFI_INTR_MASK);
  1039. return 0;
  1040. err:
  1041. irq_domain_remove(msi->domain);
  1042. return err;
  1043. }
  1044. static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
  1045. {
  1046. struct tegra_msi *msi = &pcie->msi;
  1047. unsigned int i, irq;
  1048. u32 value;
  1049. /* mask the MSI interrupt */
  1050. value = afi_readl(pcie, AFI_INTR_MASK);
  1051. value &= ~AFI_INTR_MASK_MSI_MASK;
  1052. afi_writel(pcie, value, AFI_INTR_MASK);
  1053. /* disable all MSI vectors */
  1054. afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
  1055. afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
  1056. afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
  1057. afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
  1058. afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
  1059. afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
  1060. afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
  1061. afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
  1062. free_pages(msi->pages, 0);
  1063. if (msi->irq > 0)
  1064. free_irq(msi->irq, pcie);
  1065. for (i = 0; i < INT_PCI_MSI_NR; i++) {
  1066. irq = irq_find_mapping(msi->domain, i);
  1067. if (irq > 0)
  1068. irq_dispose_mapping(irq);
  1069. }
  1070. irq_domain_remove(msi->domain);
  1071. return 0;
  1072. }
  1073. static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
  1074. u32 *xbar)
  1075. {
  1076. struct device_node *np = pcie->dev->of_node;
  1077. if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
  1078. switch (lanes) {
  1079. case 0x00000204:
  1080. dev_info(pcie->dev, "4x1, 2x1 configuration\n");
  1081. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
  1082. return 0;
  1083. case 0x00020202:
  1084. dev_info(pcie->dev, "2x3 configuration\n");
  1085. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
  1086. return 0;
  1087. case 0x00010104:
  1088. dev_info(pcie->dev, "4x1, 1x2 configuration\n");
  1089. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
  1090. return 0;
  1091. }
  1092. } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
  1093. switch (lanes) {
  1094. case 0x00000004:
  1095. dev_info(pcie->dev, "single-mode configuration\n");
  1096. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
  1097. return 0;
  1098. case 0x00000202:
  1099. dev_info(pcie->dev, "dual-mode configuration\n");
  1100. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
  1101. return 0;
  1102. }
  1103. }
  1104. return -EINVAL;
  1105. }
  1106. /*
  1107. * Check whether a given set of supplies is available in a device tree node.
  1108. * This is used to check whether the new or the legacy device tree bindings
  1109. * should be used.
  1110. */
  1111. static bool of_regulator_bulk_available(struct device_node *np,
  1112. struct regulator_bulk_data *supplies,
  1113. unsigned int num_supplies)
  1114. {
  1115. char property[32];
  1116. unsigned int i;
  1117. for (i = 0; i < num_supplies; i++) {
  1118. snprintf(property, 32, "%s-supply", supplies[i].supply);
  1119. if (of_find_property(np, property, NULL) == NULL)
  1120. return false;
  1121. }
  1122. return true;
  1123. }
  1124. /*
  1125. * Old versions of the device tree binding for this device used a set of power
  1126. * supplies that didn't match the hardware inputs. This happened to work for a
  1127. * number of cases but is not future proof. However to preserve backwards-
  1128. * compatibility with old device trees, this function will try to use the old
  1129. * set of supplies.
  1130. */
  1131. static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
  1132. {
  1133. struct device_node *np = pcie->dev->of_node;
  1134. if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
  1135. pcie->num_supplies = 3;
  1136. else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
  1137. pcie->num_supplies = 2;
  1138. if (pcie->num_supplies == 0) {
  1139. dev_err(pcie->dev, "device %s not supported in legacy mode\n",
  1140. np->full_name);
  1141. return -ENODEV;
  1142. }
  1143. pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
  1144. sizeof(*pcie->supplies),
  1145. GFP_KERNEL);
  1146. if (!pcie->supplies)
  1147. return -ENOMEM;
  1148. pcie->supplies[0].supply = "pex-clk";
  1149. pcie->supplies[1].supply = "vdd";
  1150. if (pcie->num_supplies > 2)
  1151. pcie->supplies[2].supply = "avdd";
  1152. return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
  1153. pcie->supplies);
  1154. }
  1155. /*
  1156. * Obtains the list of regulators required for a particular generation of the
  1157. * IP block.
  1158. *
  1159. * This would've been nice to do simply by providing static tables for use
  1160. * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
  1161. * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
  1162. * and either seems to be optional depending on which ports are being used.
  1163. */
  1164. static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
  1165. {
  1166. struct device_node *np = pcie->dev->of_node;
  1167. unsigned int i = 0;
  1168. if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
  1169. bool need_pexa = false, need_pexb = false;
  1170. /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
  1171. if (lane_mask & 0x0f)
  1172. need_pexa = true;
  1173. /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
  1174. if (lane_mask & 0x30)
  1175. need_pexb = true;
  1176. pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
  1177. (need_pexb ? 2 : 0);
  1178. pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
  1179. sizeof(*pcie->supplies),
  1180. GFP_KERNEL);
  1181. if (!pcie->supplies)
  1182. return -ENOMEM;
  1183. pcie->supplies[i++].supply = "avdd-pex-pll";
  1184. pcie->supplies[i++].supply = "hvdd-pex";
  1185. pcie->supplies[i++].supply = "vddio-pex-ctl";
  1186. pcie->supplies[i++].supply = "avdd-plle";
  1187. if (need_pexa) {
  1188. pcie->supplies[i++].supply = "avdd-pexa";
  1189. pcie->supplies[i++].supply = "vdd-pexa";
  1190. }
  1191. if (need_pexb) {
  1192. pcie->supplies[i++].supply = "avdd-pexb";
  1193. pcie->supplies[i++].supply = "vdd-pexb";
  1194. }
  1195. } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
  1196. pcie->num_supplies = 5;
  1197. pcie->supplies = devm_kcalloc(pcie->dev, pcie->num_supplies,
  1198. sizeof(*pcie->supplies),
  1199. GFP_KERNEL);
  1200. if (!pcie->supplies)
  1201. return -ENOMEM;
  1202. pcie->supplies[0].supply = "avdd-pex";
  1203. pcie->supplies[1].supply = "vdd-pex";
  1204. pcie->supplies[2].supply = "avdd-pex-pll";
  1205. pcie->supplies[3].supply = "avdd-plle";
  1206. pcie->supplies[4].supply = "vddio-pex-clk";
  1207. }
  1208. if (of_regulator_bulk_available(pcie->dev->of_node, pcie->supplies,
  1209. pcie->num_supplies))
  1210. return devm_regulator_bulk_get(pcie->dev, pcie->num_supplies,
  1211. pcie->supplies);
  1212. /*
  1213. * If not all regulators are available for this new scheme, assume
  1214. * that the device tree complies with an older version of the device
  1215. * tree binding.
  1216. */
  1217. dev_info(pcie->dev, "using legacy DT binding for power supplies\n");
  1218. devm_kfree(pcie->dev, pcie->supplies);
  1219. pcie->num_supplies = 0;
  1220. return tegra_pcie_get_legacy_regulators(pcie);
  1221. }
  1222. static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
  1223. {
  1224. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  1225. struct device_node *np = pcie->dev->of_node, *port;
  1226. struct of_pci_range_parser parser;
  1227. struct of_pci_range range;
  1228. u32 lanes = 0, mask = 0;
  1229. unsigned int lane = 0;
  1230. struct resource res;
  1231. int err;
  1232. if (of_pci_range_parser_init(&parser, np)) {
  1233. dev_err(pcie->dev, "missing \"ranges\" property\n");
  1234. return -EINVAL;
  1235. }
  1236. for_each_of_pci_range(&parser, &range) {
  1237. of_pci_range_to_resource(&range, np, &res);
  1238. switch (res.flags & IORESOURCE_TYPE_BITS) {
  1239. case IORESOURCE_IO:
  1240. memcpy(&pcie->io, &res, sizeof(res));
  1241. pcie->io.name = "I/O";
  1242. break;
  1243. case IORESOURCE_MEM:
  1244. if (res.flags & IORESOURCE_PREFETCH) {
  1245. memcpy(&pcie->prefetch, &res, sizeof(res));
  1246. pcie->prefetch.name = "PREFETCH";
  1247. } else {
  1248. memcpy(&pcie->mem, &res, sizeof(res));
  1249. pcie->mem.name = "MEM";
  1250. }
  1251. break;
  1252. }
  1253. }
  1254. err = of_pci_parse_bus_range(np, &pcie->busn);
  1255. if (err < 0) {
  1256. dev_err(pcie->dev, "failed to parse ranges property: %d\n",
  1257. err);
  1258. pcie->busn.name = np->name;
  1259. pcie->busn.start = 0;
  1260. pcie->busn.end = 0xff;
  1261. pcie->busn.flags = IORESOURCE_BUS;
  1262. }
  1263. /* parse root ports */
  1264. for_each_child_of_node(np, port) {
  1265. struct tegra_pcie_port *rp;
  1266. unsigned int index;
  1267. u32 value;
  1268. err = of_pci_get_devfn(port);
  1269. if (err < 0) {
  1270. dev_err(pcie->dev, "failed to parse address: %d\n",
  1271. err);
  1272. return err;
  1273. }
  1274. index = PCI_SLOT(err);
  1275. if (index < 1 || index > soc->num_ports) {
  1276. dev_err(pcie->dev, "invalid port number: %d\n", index);
  1277. return -EINVAL;
  1278. }
  1279. index--;
  1280. err = of_property_read_u32(port, "nvidia,num-lanes", &value);
  1281. if (err < 0) {
  1282. dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
  1283. err);
  1284. return err;
  1285. }
  1286. if (value > 16) {
  1287. dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
  1288. return -EINVAL;
  1289. }
  1290. lanes |= value << (index << 3);
  1291. if (!of_device_is_available(port)) {
  1292. lane += value;
  1293. continue;
  1294. }
  1295. mask |= ((1 << value) - 1) << lane;
  1296. lane += value;
  1297. rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
  1298. if (!rp)
  1299. return -ENOMEM;
  1300. err = of_address_to_resource(port, 0, &rp->regs);
  1301. if (err < 0) {
  1302. dev_err(pcie->dev, "failed to parse address: %d\n",
  1303. err);
  1304. return err;
  1305. }
  1306. INIT_LIST_HEAD(&rp->list);
  1307. rp->index = index;
  1308. rp->lanes = value;
  1309. rp->pcie = pcie;
  1310. rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
  1311. if (IS_ERR(rp->base))
  1312. return PTR_ERR(rp->base);
  1313. list_add_tail(&rp->list, &pcie->ports);
  1314. }
  1315. err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
  1316. if (err < 0) {
  1317. dev_err(pcie->dev, "invalid lane configuration\n");
  1318. return err;
  1319. }
  1320. err = tegra_pcie_get_regulators(pcie, mask);
  1321. if (err < 0)
  1322. return err;
  1323. return 0;
  1324. }
  1325. /*
  1326. * FIXME: If there are no PCIe cards attached, then calling this function
  1327. * can result in the increase of the bootup time as there are big timeout
  1328. * loops.
  1329. */
  1330. #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
  1331. static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
  1332. {
  1333. unsigned int retries = 3;
  1334. unsigned long value;
  1335. do {
  1336. unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
  1337. do {
  1338. value = readl(port->base + RP_VEND_XP);
  1339. if (value & RP_VEND_XP_DL_UP)
  1340. break;
  1341. usleep_range(1000, 2000);
  1342. } while (--timeout);
  1343. if (!timeout) {
  1344. dev_err(port->pcie->dev, "link %u down, retrying\n",
  1345. port->index);
  1346. goto retry;
  1347. }
  1348. timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
  1349. do {
  1350. value = readl(port->base + RP_LINK_CONTROL_STATUS);
  1351. if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
  1352. return true;
  1353. usleep_range(1000, 2000);
  1354. } while (--timeout);
  1355. retry:
  1356. tegra_pcie_port_reset(port);
  1357. } while (--retries);
  1358. return false;
  1359. }
  1360. static int tegra_pcie_enable(struct tegra_pcie *pcie)
  1361. {
  1362. struct tegra_pcie_port *port, *tmp;
  1363. struct hw_pci hw;
  1364. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  1365. dev_info(pcie->dev, "probing port %u, using %u lanes\n",
  1366. port->index, port->lanes);
  1367. tegra_pcie_port_enable(port);
  1368. if (tegra_pcie_port_check_link(port))
  1369. continue;
  1370. dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
  1371. tegra_pcie_port_disable(port);
  1372. tegra_pcie_port_free(port);
  1373. }
  1374. memset(&hw, 0, sizeof(hw));
  1375. hw.nr_controllers = 1;
  1376. hw.private_data = (void **)&pcie;
  1377. hw.setup = tegra_pcie_setup;
  1378. hw.map_irq = tegra_pcie_map_irq;
  1379. hw.add_bus = tegra_pcie_add_bus;
  1380. hw.scan = tegra_pcie_scan_bus;
  1381. hw.ops = &tegra_pcie_ops;
  1382. pci_common_init_dev(pcie->dev, &hw);
  1383. return 0;
  1384. }
  1385. static const struct tegra_pcie_soc_data tegra20_pcie_data = {
  1386. .num_ports = 2,
  1387. .msi_base_shift = 0,
  1388. .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
  1389. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
  1390. .has_pex_clkreq_en = false,
  1391. .has_pex_bias_ctrl = false,
  1392. .has_intr_prsnt_sense = false,
  1393. .has_cml_clk = false,
  1394. };
  1395. static const struct tegra_pcie_soc_data tegra30_pcie_data = {
  1396. .num_ports = 3,
  1397. .msi_base_shift = 8,
  1398. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  1399. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  1400. .has_pex_clkreq_en = true,
  1401. .has_pex_bias_ctrl = true,
  1402. .has_intr_prsnt_sense = true,
  1403. .has_cml_clk = true,
  1404. };
  1405. static const struct of_device_id tegra_pcie_of_match[] = {
  1406. { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
  1407. { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
  1408. { },
  1409. };
  1410. MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
  1411. static int tegra_pcie_probe(struct platform_device *pdev)
  1412. {
  1413. const struct of_device_id *match;
  1414. struct tegra_pcie *pcie;
  1415. int err;
  1416. match = of_match_device(tegra_pcie_of_match, &pdev->dev);
  1417. if (!match)
  1418. return -ENODEV;
  1419. pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  1420. if (!pcie)
  1421. return -ENOMEM;
  1422. INIT_LIST_HEAD(&pcie->buses);
  1423. INIT_LIST_HEAD(&pcie->ports);
  1424. pcie->soc_data = match->data;
  1425. pcie->dev = &pdev->dev;
  1426. err = tegra_pcie_parse_dt(pcie);
  1427. if (err < 0)
  1428. return err;
  1429. pcibios_min_mem = 0;
  1430. err = tegra_pcie_get_resources(pcie);
  1431. if (err < 0) {
  1432. dev_err(&pdev->dev, "failed to request resources: %d\n", err);
  1433. return err;
  1434. }
  1435. err = tegra_pcie_enable_controller(pcie);
  1436. if (err)
  1437. goto put_resources;
  1438. /* setup the AFI address translations */
  1439. tegra_pcie_setup_translations(pcie);
  1440. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  1441. err = tegra_pcie_enable_msi(pcie);
  1442. if (err < 0) {
  1443. dev_err(&pdev->dev,
  1444. "failed to enable MSI support: %d\n",
  1445. err);
  1446. goto put_resources;
  1447. }
  1448. }
  1449. err = tegra_pcie_enable(pcie);
  1450. if (err < 0) {
  1451. dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
  1452. goto disable_msi;
  1453. }
  1454. platform_set_drvdata(pdev, pcie);
  1455. return 0;
  1456. disable_msi:
  1457. if (IS_ENABLED(CONFIG_PCI_MSI))
  1458. tegra_pcie_disable_msi(pcie);
  1459. put_resources:
  1460. tegra_pcie_put_resources(pcie);
  1461. return err;
  1462. }
  1463. static struct platform_driver tegra_pcie_driver = {
  1464. .driver = {
  1465. .name = "tegra-pcie",
  1466. .owner = THIS_MODULE,
  1467. .of_match_table = tegra_pcie_of_match,
  1468. .suppress_bind_attrs = true,
  1469. },
  1470. .probe = tegra_pcie_probe,
  1471. };
  1472. module_platform_driver(tegra_pcie_driver);
  1473. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  1474. MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
  1475. MODULE_LICENSE("GPLv2");