pci-tegra.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288
  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. * Author: Thierry Reding <treding@nvidia.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but WITHOUT
  20. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  22. * more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  27. */
  28. #include <linux/clk.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/delay.h>
  31. #include <linux/export.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/irq.h>
  34. #include <linux/irqdomain.h>
  35. #include <linux/kernel.h>
  36. #include <linux/init.h>
  37. #include <linux/msi.h>
  38. #include <linux/of_address.h>
  39. #include <linux/of_pci.h>
  40. #include <linux/of_platform.h>
  41. #include <linux/pci.h>
  42. #include <linux/phy/phy.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/reset.h>
  45. #include <linux/sizes.h>
  46. #include <linux/slab.h>
  47. #include <linux/vmalloc.h>
  48. #include <linux/regulator/consumer.h>
  49. #include <soc/tegra/cpuidle.h>
  50. #include <soc/tegra/pmc.h>
  51. #include <asm/mach/irq.h>
  52. #include <asm/mach/map.h>
  53. #include <asm/mach/pci.h>
  54. #define INT_PCI_MSI_NR (8 * 32)
  55. /* register definitions */
  56. #define AFI_AXI_BAR0_SZ 0x00
  57. #define AFI_AXI_BAR1_SZ 0x04
  58. #define AFI_AXI_BAR2_SZ 0x08
  59. #define AFI_AXI_BAR3_SZ 0x0c
  60. #define AFI_AXI_BAR4_SZ 0x10
  61. #define AFI_AXI_BAR5_SZ 0x14
  62. #define AFI_AXI_BAR0_START 0x18
  63. #define AFI_AXI_BAR1_START 0x1c
  64. #define AFI_AXI_BAR2_START 0x20
  65. #define AFI_AXI_BAR3_START 0x24
  66. #define AFI_AXI_BAR4_START 0x28
  67. #define AFI_AXI_BAR5_START 0x2c
  68. #define AFI_FPCI_BAR0 0x30
  69. #define AFI_FPCI_BAR1 0x34
  70. #define AFI_FPCI_BAR2 0x38
  71. #define AFI_FPCI_BAR3 0x3c
  72. #define AFI_FPCI_BAR4 0x40
  73. #define AFI_FPCI_BAR5 0x44
  74. #define AFI_CACHE_BAR0_SZ 0x48
  75. #define AFI_CACHE_BAR0_ST 0x4c
  76. #define AFI_CACHE_BAR1_SZ 0x50
  77. #define AFI_CACHE_BAR1_ST 0x54
  78. #define AFI_MSI_BAR_SZ 0x60
  79. #define AFI_MSI_FPCI_BAR_ST 0x64
  80. #define AFI_MSI_AXI_BAR_ST 0x68
  81. #define AFI_MSI_VEC0 0x6c
  82. #define AFI_MSI_VEC1 0x70
  83. #define AFI_MSI_VEC2 0x74
  84. #define AFI_MSI_VEC3 0x78
  85. #define AFI_MSI_VEC4 0x7c
  86. #define AFI_MSI_VEC5 0x80
  87. #define AFI_MSI_VEC6 0x84
  88. #define AFI_MSI_VEC7 0x88
  89. #define AFI_MSI_EN_VEC0 0x8c
  90. #define AFI_MSI_EN_VEC1 0x90
  91. #define AFI_MSI_EN_VEC2 0x94
  92. #define AFI_MSI_EN_VEC3 0x98
  93. #define AFI_MSI_EN_VEC4 0x9c
  94. #define AFI_MSI_EN_VEC5 0xa0
  95. #define AFI_MSI_EN_VEC6 0xa4
  96. #define AFI_MSI_EN_VEC7 0xa8
  97. #define AFI_CONFIGURATION 0xac
  98. #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
  99. #define AFI_FPCI_ERROR_MASKS 0xb0
  100. #define AFI_INTR_MASK 0xb4
  101. #define AFI_INTR_MASK_INT_MASK (1 << 0)
  102. #define AFI_INTR_MASK_MSI_MASK (1 << 8)
  103. #define AFI_INTR_CODE 0xb8
  104. #define AFI_INTR_CODE_MASK 0xf
  105. #define AFI_INTR_INI_SLAVE_ERROR 1
  106. #define AFI_INTR_INI_DECODE_ERROR 2
  107. #define AFI_INTR_TARGET_ABORT 3
  108. #define AFI_INTR_MASTER_ABORT 4
  109. #define AFI_INTR_INVALID_WRITE 5
  110. #define AFI_INTR_LEGACY 6
  111. #define AFI_INTR_FPCI_DECODE_ERROR 7
  112. #define AFI_INTR_AXI_DECODE_ERROR 8
  113. #define AFI_INTR_FPCI_TIMEOUT 9
  114. #define AFI_INTR_PE_PRSNT_SENSE 10
  115. #define AFI_INTR_PE_CLKREQ_SENSE 11
  116. #define AFI_INTR_CLKCLAMP_SENSE 12
  117. #define AFI_INTR_RDY4PD_SENSE 13
  118. #define AFI_INTR_P2P_ERROR 14
  119. #define AFI_INTR_SIGNATURE 0xbc
  120. #define AFI_UPPER_FPCI_ADDRESS 0xc0
  121. #define AFI_SM_INTR_ENABLE 0xc4
  122. #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
  123. #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
  124. #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
  125. #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
  126. #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
  127. #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
  128. #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
  129. #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
  130. #define AFI_AFI_INTR_ENABLE 0xc8
  131. #define AFI_INTR_EN_INI_SLVERR (1 << 0)
  132. #define AFI_INTR_EN_INI_DECERR (1 << 1)
  133. #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
  134. #define AFI_INTR_EN_TGT_DECERR (1 << 3)
  135. #define AFI_INTR_EN_TGT_WRERR (1 << 4)
  136. #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
  137. #define AFI_INTR_EN_AXI_DECERR (1 << 6)
  138. #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
  139. #define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
  140. #define AFI_PCIE_CONFIG 0x0f8
  141. #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
  142. #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
  143. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
  144. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
  145. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
  146. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1 (0x0 << 20)
  147. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
  148. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
  149. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1 (0x1 << 20)
  150. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
  151. #define AFI_FUSE 0x104
  152. #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
  153. #define AFI_PEX0_CTRL 0x110
  154. #define AFI_PEX1_CTRL 0x118
  155. #define AFI_PEX2_CTRL 0x128
  156. #define AFI_PEX_CTRL_RST (1 << 0)
  157. #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
  158. #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
  159. #define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
  160. #define AFI_PLLE_CONTROL 0x160
  161. #define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
  162. #define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
  163. #define AFI_PEXBIAS_CTRL_0 0x168
  164. #define RP_VEND_XP 0x00000f00
  165. #define RP_VEND_XP_DL_UP (1 << 30)
  166. #define RP_PRIV_MISC 0x00000fe0
  167. #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xe << 0)
  168. #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xf << 0)
  169. #define RP_LINK_CONTROL_STATUS 0x00000090
  170. #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
  171. #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
  172. #define PADS_CTL_SEL 0x0000009c
  173. #define PADS_CTL 0x000000a0
  174. #define PADS_CTL_IDDQ_1L (1 << 0)
  175. #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
  176. #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
  177. #define PADS_PLL_CTL_TEGRA20 0x000000b8
  178. #define PADS_PLL_CTL_TEGRA30 0x000000b4
  179. #define PADS_PLL_CTL_RST_B4SM (1 << 1)
  180. #define PADS_PLL_CTL_LOCKDET (1 << 8)
  181. #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
  182. #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
  183. #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
  184. #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
  185. #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
  186. #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
  187. #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
  188. #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
  189. #define PADS_REFCLK_CFG0 0x000000c8
  190. #define PADS_REFCLK_CFG1 0x000000cc
  191. #define PADS_REFCLK_BIAS 0x000000d0
  192. /*
  193. * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
  194. * entries, one entry per PCIe port. These field definitions and desired
  195. * values aren't in the TRM, but do come from NVIDIA.
  196. */
  197. #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
  198. #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
  199. #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
  200. #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
  201. struct tegra_msi {
  202. struct msi_controller chip;
  203. DECLARE_BITMAP(used, INT_PCI_MSI_NR);
  204. struct irq_domain *domain;
  205. unsigned long pages;
  206. struct mutex lock;
  207. int irq;
  208. };
  209. /* used to differentiate between Tegra SoC generations */
  210. struct tegra_pcie_soc {
  211. unsigned int num_ports;
  212. unsigned int msi_base_shift;
  213. u32 pads_pll_ctl;
  214. u32 tx_ref_sel;
  215. u32 pads_refclk_cfg0;
  216. u32 pads_refclk_cfg1;
  217. bool has_pex_clkreq_en;
  218. bool has_pex_bias_ctrl;
  219. bool has_intr_prsnt_sense;
  220. bool has_cml_clk;
  221. bool has_gen2;
  222. };
  223. static inline struct tegra_msi *to_tegra_msi(struct msi_controller *chip)
  224. {
  225. return container_of(chip, struct tegra_msi, chip);
  226. }
  227. struct tegra_pcie {
  228. struct device *dev;
  229. void __iomem *pads;
  230. void __iomem *afi;
  231. int irq;
  232. struct list_head buses;
  233. struct resource *cs;
  234. struct resource io;
  235. struct resource pio;
  236. struct resource mem;
  237. struct resource prefetch;
  238. struct resource busn;
  239. struct {
  240. resource_size_t mem;
  241. resource_size_t io;
  242. } offset;
  243. struct clk *pex_clk;
  244. struct clk *afi_clk;
  245. struct clk *pll_e;
  246. struct clk *cml_clk;
  247. struct reset_control *pex_rst;
  248. struct reset_control *afi_rst;
  249. struct reset_control *pcie_xrst;
  250. bool legacy_phy;
  251. struct phy *phy;
  252. struct tegra_msi msi;
  253. struct list_head ports;
  254. u32 xbar_config;
  255. struct regulator_bulk_data *supplies;
  256. unsigned int num_supplies;
  257. const struct tegra_pcie_soc *soc;
  258. struct dentry *debugfs;
  259. };
  260. struct tegra_pcie_port {
  261. struct tegra_pcie *pcie;
  262. struct device_node *np;
  263. struct list_head list;
  264. struct resource regs;
  265. void __iomem *base;
  266. unsigned int index;
  267. unsigned int lanes;
  268. struct phy **phys;
  269. };
  270. struct tegra_pcie_bus {
  271. struct vm_struct *area;
  272. struct list_head list;
  273. unsigned int nr;
  274. };
  275. static inline struct tegra_pcie *sys_to_pcie(struct pci_sys_data *sys)
  276. {
  277. return sys->private_data;
  278. }
  279. static inline void afi_writel(struct tegra_pcie *pcie, u32 value,
  280. unsigned long offset)
  281. {
  282. writel(value, pcie->afi + offset);
  283. }
  284. static inline u32 afi_readl(struct tegra_pcie *pcie, unsigned long offset)
  285. {
  286. return readl(pcie->afi + offset);
  287. }
  288. static inline void pads_writel(struct tegra_pcie *pcie, u32 value,
  289. unsigned long offset)
  290. {
  291. writel(value, pcie->pads + offset);
  292. }
  293. static inline u32 pads_readl(struct tegra_pcie *pcie, unsigned long offset)
  294. {
  295. return readl(pcie->pads + offset);
  296. }
  297. /*
  298. * The configuration space mapping on Tegra is somewhat similar to the ECAM
  299. * defined by PCIe. However it deviates a bit in how the 4 bits for extended
  300. * register accesses are mapped:
  301. *
  302. * [27:24] extended register number
  303. * [23:16] bus number
  304. * [15:11] device number
  305. * [10: 8] function number
  306. * [ 7: 0] register number
  307. *
  308. * Mapping the whole extended configuration space would require 256 MiB of
  309. * virtual address space, only a small part of which will actually be used.
  310. * To work around this, a 1 MiB of virtual addresses are allocated per bus
  311. * when the bus is first accessed. When the physical range is mapped, the
  312. * the bus number bits are hidden so that the extended register number bits
  313. * appear as bits [19:16]. Therefore the virtual mapping looks like this:
  314. *
  315. * [19:16] extended register number
  316. * [15:11] device number
  317. * [10: 8] function number
  318. * [ 7: 0] register number
  319. *
  320. * This is achieved by stitching together 16 chunks of 64 KiB of physical
  321. * address space via the MMU.
  322. */
  323. static unsigned long tegra_pcie_conf_offset(unsigned int devfn, int where)
  324. {
  325. return ((where & 0xf00) << 8) | (PCI_SLOT(devfn) << 11) |
  326. (PCI_FUNC(devfn) << 8) | (where & 0xfc);
  327. }
  328. static struct tegra_pcie_bus *tegra_pcie_bus_alloc(struct tegra_pcie *pcie,
  329. unsigned int busnr)
  330. {
  331. struct device *dev = pcie->dev;
  332. pgprot_t prot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
  333. L_PTE_XN | L_PTE_MT_DEV_SHARED | L_PTE_SHARED);
  334. phys_addr_t cs = pcie->cs->start;
  335. struct tegra_pcie_bus *bus;
  336. unsigned int i;
  337. int err;
  338. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  339. if (!bus)
  340. return ERR_PTR(-ENOMEM);
  341. INIT_LIST_HEAD(&bus->list);
  342. bus->nr = busnr;
  343. /* allocate 1 MiB of virtual addresses */
  344. bus->area = get_vm_area(SZ_1M, VM_IOREMAP);
  345. if (!bus->area) {
  346. err = -ENOMEM;
  347. goto free;
  348. }
  349. /* map each of the 16 chunks of 64 KiB each */
  350. for (i = 0; i < 16; i++) {
  351. unsigned long virt = (unsigned long)bus->area->addr +
  352. i * SZ_64K;
  353. phys_addr_t phys = cs + i * SZ_16M + busnr * SZ_64K;
  354. err = ioremap_page_range(virt, virt + SZ_64K, phys, prot);
  355. if (err < 0) {
  356. dev_err(dev, "ioremap_page_range() failed: %d\n", err);
  357. goto unmap;
  358. }
  359. }
  360. return bus;
  361. unmap:
  362. vunmap(bus->area->addr);
  363. free:
  364. kfree(bus);
  365. return ERR_PTR(err);
  366. }
  367. static int tegra_pcie_add_bus(struct pci_bus *bus)
  368. {
  369. struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
  370. struct tegra_pcie_bus *b;
  371. b = tegra_pcie_bus_alloc(pcie, bus->number);
  372. if (IS_ERR(b))
  373. return PTR_ERR(b);
  374. list_add_tail(&b->list, &pcie->buses);
  375. return 0;
  376. }
  377. static void tegra_pcie_remove_bus(struct pci_bus *child)
  378. {
  379. struct tegra_pcie *pcie = sys_to_pcie(child->sysdata);
  380. struct tegra_pcie_bus *bus, *tmp;
  381. list_for_each_entry_safe(bus, tmp, &pcie->buses, list) {
  382. if (bus->nr == child->number) {
  383. vunmap(bus->area->addr);
  384. list_del(&bus->list);
  385. kfree(bus);
  386. break;
  387. }
  388. }
  389. }
  390. static void __iomem *tegra_pcie_map_bus(struct pci_bus *bus,
  391. unsigned int devfn,
  392. int where)
  393. {
  394. struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
  395. struct device *dev = pcie->dev;
  396. void __iomem *addr = NULL;
  397. if (bus->number == 0) {
  398. unsigned int slot = PCI_SLOT(devfn);
  399. struct tegra_pcie_port *port;
  400. list_for_each_entry(port, &pcie->ports, list) {
  401. if (port->index + 1 == slot) {
  402. addr = port->base + (where & ~3);
  403. break;
  404. }
  405. }
  406. } else {
  407. struct tegra_pcie_bus *b;
  408. list_for_each_entry(b, &pcie->buses, list)
  409. if (b->nr == bus->number)
  410. addr = (void __iomem *)b->area->addr;
  411. if (!addr) {
  412. dev_err(dev, "failed to map cfg. space for bus %u\n",
  413. bus->number);
  414. return NULL;
  415. }
  416. addr += tegra_pcie_conf_offset(devfn, where);
  417. }
  418. return addr;
  419. }
  420. static struct pci_ops tegra_pcie_ops = {
  421. .add_bus = tegra_pcie_add_bus,
  422. .remove_bus = tegra_pcie_remove_bus,
  423. .map_bus = tegra_pcie_map_bus,
  424. .read = pci_generic_config_read32,
  425. .write = pci_generic_config_write32,
  426. };
  427. static unsigned long tegra_pcie_port_get_pex_ctrl(struct tegra_pcie_port *port)
  428. {
  429. unsigned long ret = 0;
  430. switch (port->index) {
  431. case 0:
  432. ret = AFI_PEX0_CTRL;
  433. break;
  434. case 1:
  435. ret = AFI_PEX1_CTRL;
  436. break;
  437. case 2:
  438. ret = AFI_PEX2_CTRL;
  439. break;
  440. }
  441. return ret;
  442. }
  443. static void tegra_pcie_port_reset(struct tegra_pcie_port *port)
  444. {
  445. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  446. unsigned long value;
  447. /* pulse reset signal */
  448. value = afi_readl(port->pcie, ctrl);
  449. value &= ~AFI_PEX_CTRL_RST;
  450. afi_writel(port->pcie, value, ctrl);
  451. usleep_range(1000, 2000);
  452. value = afi_readl(port->pcie, ctrl);
  453. value |= AFI_PEX_CTRL_RST;
  454. afi_writel(port->pcie, value, ctrl);
  455. }
  456. static void tegra_pcie_port_enable(struct tegra_pcie_port *port)
  457. {
  458. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  459. const struct tegra_pcie_soc *soc = port->pcie->soc;
  460. unsigned long value;
  461. /* enable reference clock */
  462. value = afi_readl(port->pcie, ctrl);
  463. value |= AFI_PEX_CTRL_REFCLK_EN;
  464. if (soc->has_pex_clkreq_en)
  465. value |= AFI_PEX_CTRL_CLKREQ_EN;
  466. value |= AFI_PEX_CTRL_OVERRIDE_EN;
  467. afi_writel(port->pcie, value, ctrl);
  468. tegra_pcie_port_reset(port);
  469. }
  470. static void tegra_pcie_port_disable(struct tegra_pcie_port *port)
  471. {
  472. unsigned long ctrl = tegra_pcie_port_get_pex_ctrl(port);
  473. const struct tegra_pcie_soc *soc = port->pcie->soc;
  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. if (soc->has_pex_clkreq_en)
  482. value &= ~AFI_PEX_CTRL_CLKREQ_EN;
  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. struct device *dev = pcie->dev;
  490. devm_iounmap(dev, port->base);
  491. devm_release_mem_region(dev, port->regs.start,
  492. resource_size(&port->regs));
  493. list_del(&port->list);
  494. devm_kfree(dev, port);
  495. }
  496. /* Tegra PCIE root complex wrongly reports device class */
  497. static void tegra_pcie_fixup_class(struct pci_dev *dev)
  498. {
  499. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  500. }
  501. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf0, tegra_pcie_fixup_class);
  502. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0bf1, tegra_pcie_fixup_class);
  503. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1c, tegra_pcie_fixup_class);
  504. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_NVIDIA, 0x0e1d, tegra_pcie_fixup_class);
  505. /* Tegra PCIE requires relaxed ordering */
  506. static void tegra_pcie_relax_enable(struct pci_dev *dev)
  507. {
  508. pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
  509. }
  510. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, tegra_pcie_relax_enable);
  511. static int tegra_pcie_setup(int nr, struct pci_sys_data *sys)
  512. {
  513. struct tegra_pcie *pcie = sys_to_pcie(sys);
  514. struct device *dev = pcie->dev;
  515. int err;
  516. sys->mem_offset = pcie->offset.mem;
  517. sys->io_offset = pcie->offset.io;
  518. err = devm_request_resource(dev, &iomem_resource, &pcie->io);
  519. if (err < 0)
  520. return err;
  521. err = pci_remap_iospace(&pcie->pio, pcie->io.start);
  522. if (!err)
  523. pci_add_resource_offset(&sys->resources, &pcie->pio,
  524. sys->io_offset);
  525. pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
  526. pci_add_resource_offset(&sys->resources, &pcie->prefetch,
  527. sys->mem_offset);
  528. pci_add_resource(&sys->resources, &pcie->busn);
  529. err = devm_request_pci_bus_resources(dev, &sys->resources);
  530. if (err < 0)
  531. return err;
  532. return 1;
  533. }
  534. static int tegra_pcie_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
  535. {
  536. struct tegra_pcie *pcie = sys_to_pcie(pdev->bus->sysdata);
  537. int irq;
  538. tegra_cpuidle_pcie_irqs_in_use();
  539. irq = of_irq_parse_and_map_pci(pdev, slot, pin);
  540. if (!irq)
  541. irq = pcie->irq;
  542. return irq;
  543. }
  544. static irqreturn_t tegra_pcie_isr(int irq, void *arg)
  545. {
  546. const char *err_msg[] = {
  547. "Unknown",
  548. "AXI slave error",
  549. "AXI decode error",
  550. "Target abort",
  551. "Master abort",
  552. "Invalid write",
  553. "Legacy interrupt",
  554. "Response decoding error",
  555. "AXI response decoding error",
  556. "Transaction timeout",
  557. "Slot present pin change",
  558. "Slot clock request change",
  559. "TMS clock ramp change",
  560. "TMS ready for power down",
  561. "Peer2Peer error",
  562. };
  563. struct tegra_pcie *pcie = arg;
  564. struct device *dev = pcie->dev;
  565. u32 code, signature;
  566. code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
  567. signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
  568. afi_writel(pcie, 0, AFI_INTR_CODE);
  569. if (code == AFI_INTR_LEGACY)
  570. return IRQ_NONE;
  571. if (code >= ARRAY_SIZE(err_msg))
  572. code = 0;
  573. /*
  574. * do not pollute kernel log with master abort reports since they
  575. * happen a lot during enumeration
  576. */
  577. if (code == AFI_INTR_MASTER_ABORT)
  578. dev_dbg(dev, "%s, signature: %08x\n", err_msg[code], signature);
  579. else
  580. dev_err(dev, "%s, signature: %08x\n", err_msg[code], signature);
  581. if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
  582. code == AFI_INTR_FPCI_DECODE_ERROR) {
  583. u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
  584. u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
  585. if (code == AFI_INTR_MASTER_ABORT)
  586. dev_dbg(dev, " FPCI address: %10llx\n", address);
  587. else
  588. dev_err(dev, " FPCI address: %10llx\n", address);
  589. }
  590. return IRQ_HANDLED;
  591. }
  592. /*
  593. * FPCI map is as follows:
  594. * - 0xfdfc000000: I/O space
  595. * - 0xfdfe000000: type 0 configuration space
  596. * - 0xfdff000000: type 1 configuration space
  597. * - 0xfe00000000: type 0 extended configuration space
  598. * - 0xfe10000000: type 1 extended configuration space
  599. */
  600. static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
  601. {
  602. u32 fpci_bar, size, axi_address;
  603. /* Bar 0: type 1 extended configuration space */
  604. fpci_bar = 0xfe100000;
  605. size = resource_size(pcie->cs);
  606. axi_address = pcie->cs->start;
  607. afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
  608. afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
  609. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
  610. /* Bar 1: downstream IO bar */
  611. fpci_bar = 0xfdfc0000;
  612. size = resource_size(&pcie->io);
  613. axi_address = pcie->io.start;
  614. afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
  615. afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
  616. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
  617. /* Bar 2: prefetchable memory BAR */
  618. fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
  619. size = resource_size(&pcie->prefetch);
  620. axi_address = pcie->prefetch.start;
  621. afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
  622. afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
  623. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
  624. /* Bar 3: non prefetchable memory BAR */
  625. fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
  626. size = resource_size(&pcie->mem);
  627. axi_address = pcie->mem.start;
  628. afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
  629. afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
  630. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
  631. /* NULL out the remaining BARs as they are not used */
  632. afi_writel(pcie, 0, AFI_AXI_BAR4_START);
  633. afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
  634. afi_writel(pcie, 0, AFI_FPCI_BAR4);
  635. afi_writel(pcie, 0, AFI_AXI_BAR5_START);
  636. afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
  637. afi_writel(pcie, 0, AFI_FPCI_BAR5);
  638. /* map all upstream transactions as uncached */
  639. afi_writel(pcie, 0, AFI_CACHE_BAR0_ST);
  640. afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
  641. afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
  642. afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
  643. /* MSI translations are setup only when needed */
  644. afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
  645. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  646. afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
  647. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  648. }
  649. static int tegra_pcie_pll_wait(struct tegra_pcie *pcie, unsigned long timeout)
  650. {
  651. const struct tegra_pcie_soc *soc = pcie->soc;
  652. u32 value;
  653. timeout = jiffies + msecs_to_jiffies(timeout);
  654. while (time_before(jiffies, timeout)) {
  655. value = pads_readl(pcie, soc->pads_pll_ctl);
  656. if (value & PADS_PLL_CTL_LOCKDET)
  657. return 0;
  658. }
  659. return -ETIMEDOUT;
  660. }
  661. static int tegra_pcie_phy_enable(struct tegra_pcie *pcie)
  662. {
  663. struct device *dev = pcie->dev;
  664. const struct tegra_pcie_soc *soc = pcie->soc;
  665. u32 value;
  666. int err;
  667. /* initialize internal PHY, enable up to 16 PCIE lanes */
  668. pads_writel(pcie, 0x0, PADS_CTL_SEL);
  669. /* override IDDQ to 1 on all 4 lanes */
  670. value = pads_readl(pcie, PADS_CTL);
  671. value |= PADS_CTL_IDDQ_1L;
  672. pads_writel(pcie, value, PADS_CTL);
  673. /*
  674. * Set up PHY PLL inputs select PLLE output as refclock,
  675. * set TX ref sel to div10 (not div5).
  676. */
  677. value = pads_readl(pcie, soc->pads_pll_ctl);
  678. value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
  679. value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
  680. pads_writel(pcie, value, soc->pads_pll_ctl);
  681. /* reset PLL */
  682. value = pads_readl(pcie, soc->pads_pll_ctl);
  683. value &= ~PADS_PLL_CTL_RST_B4SM;
  684. pads_writel(pcie, value, soc->pads_pll_ctl);
  685. usleep_range(20, 100);
  686. /* take PLL out of reset */
  687. value = pads_readl(pcie, soc->pads_pll_ctl);
  688. value |= PADS_PLL_CTL_RST_B4SM;
  689. pads_writel(pcie, value, soc->pads_pll_ctl);
  690. /* wait for the PLL to lock */
  691. err = tegra_pcie_pll_wait(pcie, 500);
  692. if (err < 0) {
  693. dev_err(dev, "PLL failed to lock: %d\n", err);
  694. return err;
  695. }
  696. /* turn off IDDQ override */
  697. value = pads_readl(pcie, PADS_CTL);
  698. value &= ~PADS_CTL_IDDQ_1L;
  699. pads_writel(pcie, value, PADS_CTL);
  700. /* enable TX/RX data */
  701. value = pads_readl(pcie, PADS_CTL);
  702. value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
  703. pads_writel(pcie, value, PADS_CTL);
  704. return 0;
  705. }
  706. static int tegra_pcie_phy_disable(struct tegra_pcie *pcie)
  707. {
  708. const struct tegra_pcie_soc *soc = pcie->soc;
  709. u32 value;
  710. /* disable TX/RX data */
  711. value = pads_readl(pcie, PADS_CTL);
  712. value &= ~(PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L);
  713. pads_writel(pcie, value, PADS_CTL);
  714. /* override IDDQ */
  715. value = pads_readl(pcie, PADS_CTL);
  716. value |= PADS_CTL_IDDQ_1L;
  717. pads_writel(pcie, value, PADS_CTL);
  718. /* reset PLL */
  719. value = pads_readl(pcie, soc->pads_pll_ctl);
  720. value &= ~PADS_PLL_CTL_RST_B4SM;
  721. pads_writel(pcie, value, soc->pads_pll_ctl);
  722. usleep_range(20, 100);
  723. return 0;
  724. }
  725. static int tegra_pcie_port_phy_power_on(struct tegra_pcie_port *port)
  726. {
  727. struct device *dev = port->pcie->dev;
  728. unsigned int i;
  729. int err;
  730. for (i = 0; i < port->lanes; i++) {
  731. err = phy_power_on(port->phys[i]);
  732. if (err < 0) {
  733. dev_err(dev, "failed to power on PHY#%u: %d\n", i, err);
  734. return err;
  735. }
  736. }
  737. return 0;
  738. }
  739. static int tegra_pcie_port_phy_power_off(struct tegra_pcie_port *port)
  740. {
  741. struct device *dev = port->pcie->dev;
  742. unsigned int i;
  743. int err;
  744. for (i = 0; i < port->lanes; i++) {
  745. err = phy_power_off(port->phys[i]);
  746. if (err < 0) {
  747. dev_err(dev, "failed to power off PHY#%u: %d\n", i,
  748. err);
  749. return err;
  750. }
  751. }
  752. return 0;
  753. }
  754. static int tegra_pcie_phy_power_on(struct tegra_pcie *pcie)
  755. {
  756. struct device *dev = pcie->dev;
  757. const struct tegra_pcie_soc *soc = pcie->soc;
  758. struct tegra_pcie_port *port;
  759. int err;
  760. if (pcie->legacy_phy) {
  761. if (pcie->phy)
  762. err = phy_power_on(pcie->phy);
  763. else
  764. err = tegra_pcie_phy_enable(pcie);
  765. if (err < 0)
  766. dev_err(dev, "failed to power on PHY: %d\n", err);
  767. return err;
  768. }
  769. list_for_each_entry(port, &pcie->ports, list) {
  770. err = tegra_pcie_port_phy_power_on(port);
  771. if (err < 0) {
  772. dev_err(dev,
  773. "failed to power on PCIe port %u PHY: %d\n",
  774. port->index, err);
  775. return err;
  776. }
  777. }
  778. /* Configure the reference clock driver */
  779. pads_writel(pcie, soc->pads_refclk_cfg0, PADS_REFCLK_CFG0);
  780. if (soc->num_ports > 2)
  781. pads_writel(pcie, soc->pads_refclk_cfg1, PADS_REFCLK_CFG1);
  782. return 0;
  783. }
  784. static int tegra_pcie_phy_power_off(struct tegra_pcie *pcie)
  785. {
  786. struct device *dev = pcie->dev;
  787. struct tegra_pcie_port *port;
  788. int err;
  789. if (pcie->legacy_phy) {
  790. if (pcie->phy)
  791. err = phy_power_off(pcie->phy);
  792. else
  793. err = tegra_pcie_phy_disable(pcie);
  794. if (err < 0)
  795. dev_err(dev, "failed to power off PHY: %d\n", err);
  796. return err;
  797. }
  798. list_for_each_entry(port, &pcie->ports, list) {
  799. err = tegra_pcie_port_phy_power_off(port);
  800. if (err < 0) {
  801. dev_err(dev,
  802. "failed to power off PCIe port %u PHY: %d\n",
  803. port->index, err);
  804. return err;
  805. }
  806. }
  807. return 0;
  808. }
  809. static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
  810. {
  811. struct device *dev = pcie->dev;
  812. const struct tegra_pcie_soc *soc = pcie->soc;
  813. struct tegra_pcie_port *port;
  814. unsigned long value;
  815. int err;
  816. /* enable PLL power down */
  817. if (pcie->phy) {
  818. value = afi_readl(pcie, AFI_PLLE_CONTROL);
  819. value &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
  820. value |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
  821. afi_writel(pcie, value, AFI_PLLE_CONTROL);
  822. }
  823. /* power down PCIe slot clock bias pad */
  824. if (soc->has_pex_bias_ctrl)
  825. afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
  826. /* configure mode and disable all ports */
  827. value = afi_readl(pcie, AFI_PCIE_CONFIG);
  828. value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
  829. value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
  830. list_for_each_entry(port, &pcie->ports, list)
  831. value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
  832. afi_writel(pcie, value, AFI_PCIE_CONFIG);
  833. if (soc->has_gen2) {
  834. value = afi_readl(pcie, AFI_FUSE);
  835. value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
  836. afi_writel(pcie, value, AFI_FUSE);
  837. } else {
  838. value = afi_readl(pcie, AFI_FUSE);
  839. value |= AFI_FUSE_PCIE_T0_GEN2_DIS;
  840. afi_writel(pcie, value, AFI_FUSE);
  841. }
  842. err = tegra_pcie_phy_power_on(pcie);
  843. if (err < 0) {
  844. dev_err(dev, "failed to power on PHY(s): %d\n", err);
  845. return err;
  846. }
  847. /* take the PCIe interface module out of reset */
  848. reset_control_deassert(pcie->pcie_xrst);
  849. /* finally enable PCIe */
  850. value = afi_readl(pcie, AFI_CONFIGURATION);
  851. value |= AFI_CONFIGURATION_EN_FPCI;
  852. afi_writel(pcie, value, AFI_CONFIGURATION);
  853. value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
  854. AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
  855. AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
  856. if (soc->has_intr_prsnt_sense)
  857. value |= AFI_INTR_EN_PRSNT_SENSE;
  858. afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
  859. afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
  860. /* don't enable MSI for now, only when needed */
  861. afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
  862. /* disable all exceptions */
  863. afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
  864. return 0;
  865. }
  866. static void tegra_pcie_power_off(struct tegra_pcie *pcie)
  867. {
  868. struct device *dev = pcie->dev;
  869. int err;
  870. /* TODO: disable and unprepare clocks? */
  871. err = tegra_pcie_phy_power_off(pcie);
  872. if (err < 0)
  873. dev_err(dev, "failed to power off PHY(s): %d\n", err);
  874. reset_control_assert(pcie->pcie_xrst);
  875. reset_control_assert(pcie->afi_rst);
  876. reset_control_assert(pcie->pex_rst);
  877. tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  878. err = regulator_bulk_disable(pcie->num_supplies, pcie->supplies);
  879. if (err < 0)
  880. dev_warn(dev, "failed to disable regulators: %d\n", err);
  881. }
  882. static int tegra_pcie_power_on(struct tegra_pcie *pcie)
  883. {
  884. struct device *dev = pcie->dev;
  885. const struct tegra_pcie_soc *soc = pcie->soc;
  886. int err;
  887. reset_control_assert(pcie->pcie_xrst);
  888. reset_control_assert(pcie->afi_rst);
  889. reset_control_assert(pcie->pex_rst);
  890. tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  891. /* enable regulators */
  892. err = regulator_bulk_enable(pcie->num_supplies, pcie->supplies);
  893. if (err < 0)
  894. dev_err(dev, "failed to enable regulators: %d\n", err);
  895. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
  896. pcie->pex_clk,
  897. pcie->pex_rst);
  898. if (err) {
  899. dev_err(dev, "powerup sequence failed: %d\n", err);
  900. return err;
  901. }
  902. reset_control_deassert(pcie->afi_rst);
  903. err = clk_prepare_enable(pcie->afi_clk);
  904. if (err < 0) {
  905. dev_err(dev, "failed to enable AFI clock: %d\n", err);
  906. return err;
  907. }
  908. if (soc->has_cml_clk) {
  909. err = clk_prepare_enable(pcie->cml_clk);
  910. if (err < 0) {
  911. dev_err(dev, "failed to enable CML clock: %d\n", err);
  912. return err;
  913. }
  914. }
  915. err = clk_prepare_enable(pcie->pll_e);
  916. if (err < 0) {
  917. dev_err(dev, "failed to enable PLLE clock: %d\n", err);
  918. return err;
  919. }
  920. return 0;
  921. }
  922. static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
  923. {
  924. struct device *dev = pcie->dev;
  925. const struct tegra_pcie_soc *soc = pcie->soc;
  926. pcie->pex_clk = devm_clk_get(dev, "pex");
  927. if (IS_ERR(pcie->pex_clk))
  928. return PTR_ERR(pcie->pex_clk);
  929. pcie->afi_clk = devm_clk_get(dev, "afi");
  930. if (IS_ERR(pcie->afi_clk))
  931. return PTR_ERR(pcie->afi_clk);
  932. pcie->pll_e = devm_clk_get(dev, "pll_e");
  933. if (IS_ERR(pcie->pll_e))
  934. return PTR_ERR(pcie->pll_e);
  935. if (soc->has_cml_clk) {
  936. pcie->cml_clk = devm_clk_get(dev, "cml");
  937. if (IS_ERR(pcie->cml_clk))
  938. return PTR_ERR(pcie->cml_clk);
  939. }
  940. return 0;
  941. }
  942. static int tegra_pcie_resets_get(struct tegra_pcie *pcie)
  943. {
  944. struct device *dev = pcie->dev;
  945. pcie->pex_rst = devm_reset_control_get(dev, "pex");
  946. if (IS_ERR(pcie->pex_rst))
  947. return PTR_ERR(pcie->pex_rst);
  948. pcie->afi_rst = devm_reset_control_get(dev, "afi");
  949. if (IS_ERR(pcie->afi_rst))
  950. return PTR_ERR(pcie->afi_rst);
  951. pcie->pcie_xrst = devm_reset_control_get(dev, "pcie_x");
  952. if (IS_ERR(pcie->pcie_xrst))
  953. return PTR_ERR(pcie->pcie_xrst);
  954. return 0;
  955. }
  956. static int tegra_pcie_phys_get_legacy(struct tegra_pcie *pcie)
  957. {
  958. struct device *dev = pcie->dev;
  959. int err;
  960. pcie->phy = devm_phy_optional_get(dev, "pcie");
  961. if (IS_ERR(pcie->phy)) {
  962. err = PTR_ERR(pcie->phy);
  963. dev_err(dev, "failed to get PHY: %d\n", err);
  964. return err;
  965. }
  966. err = phy_init(pcie->phy);
  967. if (err < 0) {
  968. dev_err(dev, "failed to initialize PHY: %d\n", err);
  969. return err;
  970. }
  971. pcie->legacy_phy = true;
  972. return 0;
  973. }
  974. static struct phy *devm_of_phy_optional_get_index(struct device *dev,
  975. struct device_node *np,
  976. const char *consumer,
  977. unsigned int index)
  978. {
  979. struct phy *phy;
  980. char *name;
  981. name = kasprintf(GFP_KERNEL, "%s-%u", consumer, index);
  982. if (!name)
  983. return ERR_PTR(-ENOMEM);
  984. phy = devm_of_phy_get(dev, np, name);
  985. kfree(name);
  986. if (IS_ERR(phy) && PTR_ERR(phy) == -ENODEV)
  987. phy = NULL;
  988. return phy;
  989. }
  990. static int tegra_pcie_port_get_phys(struct tegra_pcie_port *port)
  991. {
  992. struct device *dev = port->pcie->dev;
  993. struct phy *phy;
  994. unsigned int i;
  995. int err;
  996. port->phys = devm_kcalloc(dev, sizeof(phy), port->lanes, GFP_KERNEL);
  997. if (!port->phys)
  998. return -ENOMEM;
  999. for (i = 0; i < port->lanes; i++) {
  1000. phy = devm_of_phy_optional_get_index(dev, port->np, "pcie", i);
  1001. if (IS_ERR(phy)) {
  1002. dev_err(dev, "failed to get PHY#%u: %ld\n", i,
  1003. PTR_ERR(phy));
  1004. return PTR_ERR(phy);
  1005. }
  1006. err = phy_init(phy);
  1007. if (err < 0) {
  1008. dev_err(dev, "failed to initialize PHY#%u: %d\n", i,
  1009. err);
  1010. return err;
  1011. }
  1012. port->phys[i] = phy;
  1013. }
  1014. return 0;
  1015. }
  1016. static int tegra_pcie_phys_get(struct tegra_pcie *pcie)
  1017. {
  1018. const struct tegra_pcie_soc *soc = pcie->soc;
  1019. struct device_node *np = pcie->dev->of_node;
  1020. struct tegra_pcie_port *port;
  1021. int err;
  1022. if (!soc->has_gen2 || of_find_property(np, "phys", NULL) != NULL)
  1023. return tegra_pcie_phys_get_legacy(pcie);
  1024. list_for_each_entry(port, &pcie->ports, list) {
  1025. err = tegra_pcie_port_get_phys(port);
  1026. if (err < 0)
  1027. return err;
  1028. }
  1029. return 0;
  1030. }
  1031. static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
  1032. {
  1033. struct device *dev = pcie->dev;
  1034. struct platform_device *pdev = to_platform_device(dev);
  1035. struct resource *pads, *afi, *res;
  1036. int err;
  1037. err = tegra_pcie_clocks_get(pcie);
  1038. if (err) {
  1039. dev_err(dev, "failed to get clocks: %d\n", err);
  1040. return err;
  1041. }
  1042. err = tegra_pcie_resets_get(pcie);
  1043. if (err) {
  1044. dev_err(dev, "failed to get resets: %d\n", err);
  1045. return err;
  1046. }
  1047. err = tegra_pcie_phys_get(pcie);
  1048. if (err < 0) {
  1049. dev_err(dev, "failed to get PHYs: %d\n", err);
  1050. return err;
  1051. }
  1052. err = tegra_pcie_power_on(pcie);
  1053. if (err) {
  1054. dev_err(dev, "failed to power up: %d\n", err);
  1055. return err;
  1056. }
  1057. pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
  1058. pcie->pads = devm_ioremap_resource(dev, pads);
  1059. if (IS_ERR(pcie->pads)) {
  1060. err = PTR_ERR(pcie->pads);
  1061. goto poweroff;
  1062. }
  1063. afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
  1064. pcie->afi = devm_ioremap_resource(dev, afi);
  1065. if (IS_ERR(pcie->afi)) {
  1066. err = PTR_ERR(pcie->afi);
  1067. goto poweroff;
  1068. }
  1069. /* request configuration space, but remap later, on demand */
  1070. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
  1071. if (!res) {
  1072. err = -EADDRNOTAVAIL;
  1073. goto poweroff;
  1074. }
  1075. pcie->cs = devm_request_mem_region(dev, res->start,
  1076. resource_size(res), res->name);
  1077. if (!pcie->cs) {
  1078. err = -EADDRNOTAVAIL;
  1079. goto poweroff;
  1080. }
  1081. /* request interrupt */
  1082. err = platform_get_irq_byname(pdev, "intr");
  1083. if (err < 0) {
  1084. dev_err(dev, "failed to get IRQ: %d\n", err);
  1085. goto poweroff;
  1086. }
  1087. pcie->irq = err;
  1088. err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
  1089. if (err) {
  1090. dev_err(dev, "failed to register IRQ: %d\n", err);
  1091. goto poweroff;
  1092. }
  1093. return 0;
  1094. poweroff:
  1095. tegra_pcie_power_off(pcie);
  1096. return err;
  1097. }
  1098. static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
  1099. {
  1100. struct device *dev = pcie->dev;
  1101. int err;
  1102. if (pcie->irq > 0)
  1103. free_irq(pcie->irq, pcie);
  1104. tegra_pcie_power_off(pcie);
  1105. err = phy_exit(pcie->phy);
  1106. if (err < 0)
  1107. dev_err(dev, "failed to teardown PHY: %d\n", err);
  1108. return 0;
  1109. }
  1110. static int tegra_msi_alloc(struct tegra_msi *chip)
  1111. {
  1112. int msi;
  1113. mutex_lock(&chip->lock);
  1114. msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
  1115. if (msi < INT_PCI_MSI_NR)
  1116. set_bit(msi, chip->used);
  1117. else
  1118. msi = -ENOSPC;
  1119. mutex_unlock(&chip->lock);
  1120. return msi;
  1121. }
  1122. static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
  1123. {
  1124. struct device *dev = chip->chip.dev;
  1125. mutex_lock(&chip->lock);
  1126. if (!test_bit(irq, chip->used))
  1127. dev_err(dev, "trying to free unused MSI#%lu\n", irq);
  1128. else
  1129. clear_bit(irq, chip->used);
  1130. mutex_unlock(&chip->lock);
  1131. }
  1132. static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
  1133. {
  1134. struct tegra_pcie *pcie = data;
  1135. struct device *dev = pcie->dev;
  1136. struct tegra_msi *msi = &pcie->msi;
  1137. unsigned int i, processed = 0;
  1138. for (i = 0; i < 8; i++) {
  1139. unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
  1140. while (reg) {
  1141. unsigned int offset = find_first_bit(&reg, 32);
  1142. unsigned int index = i * 32 + offset;
  1143. unsigned int irq;
  1144. /* clear the interrupt */
  1145. afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
  1146. irq = irq_find_mapping(msi->domain, index);
  1147. if (irq) {
  1148. if (test_bit(index, msi->used))
  1149. generic_handle_irq(irq);
  1150. else
  1151. dev_info(dev, "unhandled MSI\n");
  1152. } else {
  1153. /*
  1154. * that's weird who triggered this?
  1155. * just clear it
  1156. */
  1157. dev_info(dev, "unexpected MSI\n");
  1158. }
  1159. /* see if there's any more pending in this vector */
  1160. reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
  1161. processed++;
  1162. }
  1163. }
  1164. return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
  1165. }
  1166. static int tegra_msi_setup_irq(struct msi_controller *chip,
  1167. struct pci_dev *pdev, struct msi_desc *desc)
  1168. {
  1169. struct tegra_msi *msi = to_tegra_msi(chip);
  1170. struct msi_msg msg;
  1171. unsigned int irq;
  1172. int hwirq;
  1173. hwirq = tegra_msi_alloc(msi);
  1174. if (hwirq < 0)
  1175. return hwirq;
  1176. irq = irq_create_mapping(msi->domain, hwirq);
  1177. if (!irq) {
  1178. tegra_msi_free(msi, hwirq);
  1179. return -EINVAL;
  1180. }
  1181. irq_set_msi_desc(irq, desc);
  1182. msg.address_lo = virt_to_phys((void *)msi->pages);
  1183. /* 32 bit address only */
  1184. msg.address_hi = 0;
  1185. msg.data = hwirq;
  1186. pci_write_msi_msg(irq, &msg);
  1187. return 0;
  1188. }
  1189. static void tegra_msi_teardown_irq(struct msi_controller *chip,
  1190. unsigned int irq)
  1191. {
  1192. struct tegra_msi *msi = to_tegra_msi(chip);
  1193. struct irq_data *d = irq_get_irq_data(irq);
  1194. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1195. irq_dispose_mapping(irq);
  1196. tegra_msi_free(msi, hwirq);
  1197. }
  1198. static struct irq_chip tegra_msi_irq_chip = {
  1199. .name = "Tegra PCIe MSI",
  1200. .irq_enable = pci_msi_unmask_irq,
  1201. .irq_disable = pci_msi_mask_irq,
  1202. .irq_mask = pci_msi_mask_irq,
  1203. .irq_unmask = pci_msi_unmask_irq,
  1204. };
  1205. static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
  1206. irq_hw_number_t hwirq)
  1207. {
  1208. irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
  1209. irq_set_chip_data(irq, domain->host_data);
  1210. tegra_cpuidle_pcie_irqs_in_use();
  1211. return 0;
  1212. }
  1213. static const struct irq_domain_ops msi_domain_ops = {
  1214. .map = tegra_msi_map,
  1215. };
  1216. static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
  1217. {
  1218. struct device *dev = pcie->dev;
  1219. struct platform_device *pdev = to_platform_device(dev);
  1220. const struct tegra_pcie_soc *soc = pcie->soc;
  1221. struct tegra_msi *msi = &pcie->msi;
  1222. unsigned long base;
  1223. int err;
  1224. u32 reg;
  1225. mutex_init(&msi->lock);
  1226. msi->chip.dev = dev;
  1227. msi->chip.setup_irq = tegra_msi_setup_irq;
  1228. msi->chip.teardown_irq = tegra_msi_teardown_irq;
  1229. msi->domain = irq_domain_add_linear(dev->of_node, INT_PCI_MSI_NR,
  1230. &msi_domain_ops, &msi->chip);
  1231. if (!msi->domain) {
  1232. dev_err(dev, "failed to create IRQ domain\n");
  1233. return -ENOMEM;
  1234. }
  1235. err = platform_get_irq_byname(pdev, "msi");
  1236. if (err < 0) {
  1237. dev_err(dev, "failed to get IRQ: %d\n", err);
  1238. goto err;
  1239. }
  1240. msi->irq = err;
  1241. err = request_irq(msi->irq, tegra_pcie_msi_irq, IRQF_NO_THREAD,
  1242. tegra_msi_irq_chip.name, pcie);
  1243. if (err < 0) {
  1244. dev_err(dev, "failed to request IRQ: %d\n", err);
  1245. goto err;
  1246. }
  1247. /* setup AFI/FPCI range */
  1248. msi->pages = __get_free_pages(GFP_KERNEL, 0);
  1249. base = virt_to_phys((void *)msi->pages);
  1250. afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
  1251. afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
  1252. /* this register is in 4K increments */
  1253. afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
  1254. /* enable all MSI vectors */
  1255. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
  1256. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
  1257. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
  1258. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
  1259. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
  1260. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
  1261. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
  1262. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
  1263. /* and unmask the MSI interrupt */
  1264. reg = afi_readl(pcie, AFI_INTR_MASK);
  1265. reg |= AFI_INTR_MASK_MSI_MASK;
  1266. afi_writel(pcie, reg, AFI_INTR_MASK);
  1267. return 0;
  1268. err:
  1269. irq_domain_remove(msi->domain);
  1270. return err;
  1271. }
  1272. static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
  1273. {
  1274. struct tegra_msi *msi = &pcie->msi;
  1275. unsigned int i, irq;
  1276. u32 value;
  1277. /* mask the MSI interrupt */
  1278. value = afi_readl(pcie, AFI_INTR_MASK);
  1279. value &= ~AFI_INTR_MASK_MSI_MASK;
  1280. afi_writel(pcie, value, AFI_INTR_MASK);
  1281. /* disable all MSI vectors */
  1282. afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
  1283. afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
  1284. afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
  1285. afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
  1286. afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
  1287. afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
  1288. afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
  1289. afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
  1290. free_pages(msi->pages, 0);
  1291. if (msi->irq > 0)
  1292. free_irq(msi->irq, pcie);
  1293. for (i = 0; i < INT_PCI_MSI_NR; i++) {
  1294. irq = irq_find_mapping(msi->domain, i);
  1295. if (irq > 0)
  1296. irq_dispose_mapping(irq);
  1297. }
  1298. irq_domain_remove(msi->domain);
  1299. return 0;
  1300. }
  1301. static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
  1302. u32 *xbar)
  1303. {
  1304. struct device *dev = pcie->dev;
  1305. struct device_node *np = dev->of_node;
  1306. if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
  1307. switch (lanes) {
  1308. case 0x0000104:
  1309. dev_info(dev, "4x1, 1x1 configuration\n");
  1310. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X4_X1;
  1311. return 0;
  1312. case 0x0000102:
  1313. dev_info(dev, "2x1, 1x1 configuration\n");
  1314. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_X2_X1;
  1315. return 0;
  1316. }
  1317. } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
  1318. switch (lanes) {
  1319. case 0x00000204:
  1320. dev_info(dev, "4x1, 2x1 configuration\n");
  1321. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
  1322. return 0;
  1323. case 0x00020202:
  1324. dev_info(dev, "2x3 configuration\n");
  1325. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
  1326. return 0;
  1327. case 0x00010104:
  1328. dev_info(dev, "4x1, 1x2 configuration\n");
  1329. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
  1330. return 0;
  1331. }
  1332. } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
  1333. switch (lanes) {
  1334. case 0x00000004:
  1335. dev_info(dev, "single-mode configuration\n");
  1336. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
  1337. return 0;
  1338. case 0x00000202:
  1339. dev_info(dev, "dual-mode configuration\n");
  1340. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
  1341. return 0;
  1342. }
  1343. }
  1344. return -EINVAL;
  1345. }
  1346. /*
  1347. * Check whether a given set of supplies is available in a device tree node.
  1348. * This is used to check whether the new or the legacy device tree bindings
  1349. * should be used.
  1350. */
  1351. static bool of_regulator_bulk_available(struct device_node *np,
  1352. struct regulator_bulk_data *supplies,
  1353. unsigned int num_supplies)
  1354. {
  1355. char property[32];
  1356. unsigned int i;
  1357. for (i = 0; i < num_supplies; i++) {
  1358. snprintf(property, 32, "%s-supply", supplies[i].supply);
  1359. if (of_find_property(np, property, NULL) == NULL)
  1360. return false;
  1361. }
  1362. return true;
  1363. }
  1364. /*
  1365. * Old versions of the device tree binding for this device used a set of power
  1366. * supplies that didn't match the hardware inputs. This happened to work for a
  1367. * number of cases but is not future proof. However to preserve backwards-
  1368. * compatibility with old device trees, this function will try to use the old
  1369. * set of supplies.
  1370. */
  1371. static int tegra_pcie_get_legacy_regulators(struct tegra_pcie *pcie)
  1372. {
  1373. struct device *dev = pcie->dev;
  1374. struct device_node *np = dev->of_node;
  1375. if (of_device_is_compatible(np, "nvidia,tegra30-pcie"))
  1376. pcie->num_supplies = 3;
  1377. else if (of_device_is_compatible(np, "nvidia,tegra20-pcie"))
  1378. pcie->num_supplies = 2;
  1379. if (pcie->num_supplies == 0) {
  1380. dev_err(dev, "device %s not supported in legacy mode\n",
  1381. np->full_name);
  1382. return -ENODEV;
  1383. }
  1384. pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
  1385. sizeof(*pcie->supplies),
  1386. GFP_KERNEL);
  1387. if (!pcie->supplies)
  1388. return -ENOMEM;
  1389. pcie->supplies[0].supply = "pex-clk";
  1390. pcie->supplies[1].supply = "vdd";
  1391. if (pcie->num_supplies > 2)
  1392. pcie->supplies[2].supply = "avdd";
  1393. return devm_regulator_bulk_get(dev, pcie->num_supplies, pcie->supplies);
  1394. }
  1395. /*
  1396. * Obtains the list of regulators required for a particular generation of the
  1397. * IP block.
  1398. *
  1399. * This would've been nice to do simply by providing static tables for use
  1400. * with the regulator_bulk_*() API, but unfortunately Tegra30 is a bit quirky
  1401. * in that it has two pairs or AVDD_PEX and VDD_PEX supplies (PEXA and PEXB)
  1402. * and either seems to be optional depending on which ports are being used.
  1403. */
  1404. static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
  1405. {
  1406. struct device *dev = pcie->dev;
  1407. struct device_node *np = dev->of_node;
  1408. unsigned int i = 0;
  1409. if (of_device_is_compatible(np, "nvidia,tegra124-pcie")) {
  1410. pcie->num_supplies = 7;
  1411. pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
  1412. sizeof(*pcie->supplies),
  1413. GFP_KERNEL);
  1414. if (!pcie->supplies)
  1415. return -ENOMEM;
  1416. pcie->supplies[i++].supply = "avddio-pex";
  1417. pcie->supplies[i++].supply = "dvddio-pex";
  1418. pcie->supplies[i++].supply = "avdd-pex-pll";
  1419. pcie->supplies[i++].supply = "hvdd-pex";
  1420. pcie->supplies[i++].supply = "hvdd-pex-pll-e";
  1421. pcie->supplies[i++].supply = "vddio-pex-ctl";
  1422. pcie->supplies[i++].supply = "avdd-pll-erefe";
  1423. } else if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
  1424. bool need_pexa = false, need_pexb = false;
  1425. /* VDD_PEXA and AVDD_PEXA supply lanes 0 to 3 */
  1426. if (lane_mask & 0x0f)
  1427. need_pexa = true;
  1428. /* VDD_PEXB and AVDD_PEXB supply lanes 4 to 5 */
  1429. if (lane_mask & 0x30)
  1430. need_pexb = true;
  1431. pcie->num_supplies = 4 + (need_pexa ? 2 : 0) +
  1432. (need_pexb ? 2 : 0);
  1433. pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
  1434. sizeof(*pcie->supplies),
  1435. GFP_KERNEL);
  1436. if (!pcie->supplies)
  1437. return -ENOMEM;
  1438. pcie->supplies[i++].supply = "avdd-pex-pll";
  1439. pcie->supplies[i++].supply = "hvdd-pex";
  1440. pcie->supplies[i++].supply = "vddio-pex-ctl";
  1441. pcie->supplies[i++].supply = "avdd-plle";
  1442. if (need_pexa) {
  1443. pcie->supplies[i++].supply = "avdd-pexa";
  1444. pcie->supplies[i++].supply = "vdd-pexa";
  1445. }
  1446. if (need_pexb) {
  1447. pcie->supplies[i++].supply = "avdd-pexb";
  1448. pcie->supplies[i++].supply = "vdd-pexb";
  1449. }
  1450. } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
  1451. pcie->num_supplies = 5;
  1452. pcie->supplies = devm_kcalloc(dev, pcie->num_supplies,
  1453. sizeof(*pcie->supplies),
  1454. GFP_KERNEL);
  1455. if (!pcie->supplies)
  1456. return -ENOMEM;
  1457. pcie->supplies[0].supply = "avdd-pex";
  1458. pcie->supplies[1].supply = "vdd-pex";
  1459. pcie->supplies[2].supply = "avdd-pex-pll";
  1460. pcie->supplies[3].supply = "avdd-plle";
  1461. pcie->supplies[4].supply = "vddio-pex-clk";
  1462. }
  1463. if (of_regulator_bulk_available(dev->of_node, pcie->supplies,
  1464. pcie->num_supplies))
  1465. return devm_regulator_bulk_get(dev, pcie->num_supplies,
  1466. pcie->supplies);
  1467. /*
  1468. * If not all regulators are available for this new scheme, assume
  1469. * that the device tree complies with an older version of the device
  1470. * tree binding.
  1471. */
  1472. dev_info(dev, "using legacy DT binding for power supplies\n");
  1473. devm_kfree(dev, pcie->supplies);
  1474. pcie->num_supplies = 0;
  1475. return tegra_pcie_get_legacy_regulators(pcie);
  1476. }
  1477. static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
  1478. {
  1479. struct device *dev = pcie->dev;
  1480. struct device_node *np = dev->of_node, *port;
  1481. const struct tegra_pcie_soc *soc = pcie->soc;
  1482. struct of_pci_range_parser parser;
  1483. struct of_pci_range range;
  1484. u32 lanes = 0, mask = 0;
  1485. unsigned int lane = 0;
  1486. struct resource res;
  1487. int err;
  1488. if (of_pci_range_parser_init(&parser, np)) {
  1489. dev_err(dev, "missing \"ranges\" property\n");
  1490. return -EINVAL;
  1491. }
  1492. for_each_of_pci_range(&parser, &range) {
  1493. err = of_pci_range_to_resource(&range, np, &res);
  1494. if (err < 0)
  1495. return err;
  1496. switch (res.flags & IORESOURCE_TYPE_BITS) {
  1497. case IORESOURCE_IO:
  1498. /* Track the bus -> CPU I/O mapping offset. */
  1499. pcie->offset.io = res.start - range.pci_addr;
  1500. memcpy(&pcie->pio, &res, sizeof(res));
  1501. pcie->pio.name = np->full_name;
  1502. /*
  1503. * The Tegra PCIe host bridge uses this to program the
  1504. * mapping of the I/O space to the physical address,
  1505. * so we override the .start and .end fields here that
  1506. * of_pci_range_to_resource() converted to I/O space.
  1507. * We also set the IORESOURCE_MEM type to clarify that
  1508. * the resource is in the physical memory space.
  1509. */
  1510. pcie->io.start = range.cpu_addr;
  1511. pcie->io.end = range.cpu_addr + range.size - 1;
  1512. pcie->io.flags = IORESOURCE_MEM;
  1513. pcie->io.name = "I/O";
  1514. memcpy(&res, &pcie->io, sizeof(res));
  1515. break;
  1516. case IORESOURCE_MEM:
  1517. /*
  1518. * Track the bus -> CPU memory mapping offset. This
  1519. * assumes that the prefetchable and non-prefetchable
  1520. * regions will be the last of type IORESOURCE_MEM in
  1521. * the ranges property.
  1522. * */
  1523. pcie->offset.mem = res.start - range.pci_addr;
  1524. if (res.flags & IORESOURCE_PREFETCH) {
  1525. memcpy(&pcie->prefetch, &res, sizeof(res));
  1526. pcie->prefetch.name = "prefetchable";
  1527. } else {
  1528. memcpy(&pcie->mem, &res, sizeof(res));
  1529. pcie->mem.name = "non-prefetchable";
  1530. }
  1531. break;
  1532. }
  1533. }
  1534. err = of_pci_parse_bus_range(np, &pcie->busn);
  1535. if (err < 0) {
  1536. dev_err(dev, "failed to parse ranges property: %d\n", err);
  1537. pcie->busn.name = np->name;
  1538. pcie->busn.start = 0;
  1539. pcie->busn.end = 0xff;
  1540. pcie->busn.flags = IORESOURCE_BUS;
  1541. }
  1542. /* parse root ports */
  1543. for_each_child_of_node(np, port) {
  1544. struct tegra_pcie_port *rp;
  1545. unsigned int index;
  1546. u32 value;
  1547. err = of_pci_get_devfn(port);
  1548. if (err < 0) {
  1549. dev_err(dev, "failed to parse address: %d\n", err);
  1550. return err;
  1551. }
  1552. index = PCI_SLOT(err);
  1553. if (index < 1 || index > soc->num_ports) {
  1554. dev_err(dev, "invalid port number: %d\n", index);
  1555. return -EINVAL;
  1556. }
  1557. index--;
  1558. err = of_property_read_u32(port, "nvidia,num-lanes", &value);
  1559. if (err < 0) {
  1560. dev_err(dev, "failed to parse # of lanes: %d\n",
  1561. err);
  1562. return err;
  1563. }
  1564. if (value > 16) {
  1565. dev_err(dev, "invalid # of lanes: %u\n", value);
  1566. return -EINVAL;
  1567. }
  1568. lanes |= value << (index << 3);
  1569. if (!of_device_is_available(port)) {
  1570. lane += value;
  1571. continue;
  1572. }
  1573. mask |= ((1 << value) - 1) << lane;
  1574. lane += value;
  1575. rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
  1576. if (!rp)
  1577. return -ENOMEM;
  1578. err = of_address_to_resource(port, 0, &rp->regs);
  1579. if (err < 0) {
  1580. dev_err(dev, "failed to parse address: %d\n", err);
  1581. return err;
  1582. }
  1583. INIT_LIST_HEAD(&rp->list);
  1584. rp->index = index;
  1585. rp->lanes = value;
  1586. rp->pcie = pcie;
  1587. rp->np = port;
  1588. rp->base = devm_ioremap_resource(dev, &rp->regs);
  1589. if (IS_ERR(rp->base))
  1590. return PTR_ERR(rp->base);
  1591. list_add_tail(&rp->list, &pcie->ports);
  1592. }
  1593. err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
  1594. if (err < 0) {
  1595. dev_err(dev, "invalid lane configuration\n");
  1596. return err;
  1597. }
  1598. err = tegra_pcie_get_regulators(pcie, mask);
  1599. if (err < 0)
  1600. return err;
  1601. return 0;
  1602. }
  1603. /*
  1604. * FIXME: If there are no PCIe cards attached, then calling this function
  1605. * can result in the increase of the bootup time as there are big timeout
  1606. * loops.
  1607. */
  1608. #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
  1609. static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
  1610. {
  1611. struct device *dev = port->pcie->dev;
  1612. unsigned int retries = 3;
  1613. unsigned long value;
  1614. /* override presence detection */
  1615. value = readl(port->base + RP_PRIV_MISC);
  1616. value &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
  1617. value |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
  1618. writel(value, port->base + RP_PRIV_MISC);
  1619. do {
  1620. unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
  1621. do {
  1622. value = readl(port->base + RP_VEND_XP);
  1623. if (value & RP_VEND_XP_DL_UP)
  1624. break;
  1625. usleep_range(1000, 2000);
  1626. } while (--timeout);
  1627. if (!timeout) {
  1628. dev_err(dev, "link %u down, retrying\n", port->index);
  1629. goto retry;
  1630. }
  1631. timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
  1632. do {
  1633. value = readl(port->base + RP_LINK_CONTROL_STATUS);
  1634. if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
  1635. return true;
  1636. usleep_range(1000, 2000);
  1637. } while (--timeout);
  1638. retry:
  1639. tegra_pcie_port_reset(port);
  1640. } while (--retries);
  1641. return false;
  1642. }
  1643. static int tegra_pcie_enable(struct tegra_pcie *pcie)
  1644. {
  1645. struct device *dev = pcie->dev;
  1646. struct tegra_pcie_port *port, *tmp;
  1647. struct hw_pci hw;
  1648. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  1649. dev_info(dev, "probing port %u, using %u lanes\n",
  1650. port->index, port->lanes);
  1651. tegra_pcie_port_enable(port);
  1652. if (tegra_pcie_port_check_link(port))
  1653. continue;
  1654. dev_info(dev, "link %u down, ignoring\n", port->index);
  1655. tegra_pcie_port_disable(port);
  1656. tegra_pcie_port_free(port);
  1657. }
  1658. memset(&hw, 0, sizeof(hw));
  1659. #ifdef CONFIG_PCI_MSI
  1660. hw.msi_ctrl = &pcie->msi.chip;
  1661. #endif
  1662. hw.nr_controllers = 1;
  1663. hw.private_data = (void **)&pcie;
  1664. hw.setup = tegra_pcie_setup;
  1665. hw.map_irq = tegra_pcie_map_irq;
  1666. hw.ops = &tegra_pcie_ops;
  1667. pci_common_init_dev(dev, &hw);
  1668. return 0;
  1669. }
  1670. static const struct tegra_pcie_soc tegra20_pcie = {
  1671. .num_ports = 2,
  1672. .msi_base_shift = 0,
  1673. .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
  1674. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
  1675. .pads_refclk_cfg0 = 0xfa5cfa5c,
  1676. .has_pex_clkreq_en = false,
  1677. .has_pex_bias_ctrl = false,
  1678. .has_intr_prsnt_sense = false,
  1679. .has_cml_clk = false,
  1680. .has_gen2 = false,
  1681. };
  1682. static const struct tegra_pcie_soc tegra30_pcie = {
  1683. .num_ports = 3,
  1684. .msi_base_shift = 8,
  1685. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  1686. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  1687. .pads_refclk_cfg0 = 0xfa5cfa5c,
  1688. .pads_refclk_cfg1 = 0xfa5cfa5c,
  1689. .has_pex_clkreq_en = true,
  1690. .has_pex_bias_ctrl = true,
  1691. .has_intr_prsnt_sense = true,
  1692. .has_cml_clk = true,
  1693. .has_gen2 = false,
  1694. };
  1695. static const struct tegra_pcie_soc tegra124_pcie = {
  1696. .num_ports = 2,
  1697. .msi_base_shift = 8,
  1698. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  1699. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  1700. .pads_refclk_cfg0 = 0x44ac44ac,
  1701. .has_pex_clkreq_en = true,
  1702. .has_pex_bias_ctrl = true,
  1703. .has_intr_prsnt_sense = true,
  1704. .has_cml_clk = true,
  1705. .has_gen2 = true,
  1706. };
  1707. static const struct of_device_id tegra_pcie_of_match[] = {
  1708. { .compatible = "nvidia,tegra124-pcie", .data = &tegra124_pcie },
  1709. { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie },
  1710. { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie },
  1711. { },
  1712. };
  1713. static void *tegra_pcie_ports_seq_start(struct seq_file *s, loff_t *pos)
  1714. {
  1715. struct tegra_pcie *pcie = s->private;
  1716. if (list_empty(&pcie->ports))
  1717. return NULL;
  1718. seq_printf(s, "Index Status\n");
  1719. return seq_list_start(&pcie->ports, *pos);
  1720. }
  1721. static void *tegra_pcie_ports_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1722. {
  1723. struct tegra_pcie *pcie = s->private;
  1724. return seq_list_next(v, &pcie->ports, pos);
  1725. }
  1726. static void tegra_pcie_ports_seq_stop(struct seq_file *s, void *v)
  1727. {
  1728. }
  1729. static int tegra_pcie_ports_seq_show(struct seq_file *s, void *v)
  1730. {
  1731. bool up = false, active = false;
  1732. struct tegra_pcie_port *port;
  1733. unsigned int value;
  1734. port = list_entry(v, struct tegra_pcie_port, list);
  1735. value = readl(port->base + RP_VEND_XP);
  1736. if (value & RP_VEND_XP_DL_UP)
  1737. up = true;
  1738. value = readl(port->base + RP_LINK_CONTROL_STATUS);
  1739. if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
  1740. active = true;
  1741. seq_printf(s, "%2u ", port->index);
  1742. if (up)
  1743. seq_printf(s, "up");
  1744. if (active) {
  1745. if (up)
  1746. seq_printf(s, ", ");
  1747. seq_printf(s, "active");
  1748. }
  1749. seq_printf(s, "\n");
  1750. return 0;
  1751. }
  1752. static const struct seq_operations tegra_pcie_ports_seq_ops = {
  1753. .start = tegra_pcie_ports_seq_start,
  1754. .next = tegra_pcie_ports_seq_next,
  1755. .stop = tegra_pcie_ports_seq_stop,
  1756. .show = tegra_pcie_ports_seq_show,
  1757. };
  1758. static int tegra_pcie_ports_open(struct inode *inode, struct file *file)
  1759. {
  1760. struct tegra_pcie *pcie = inode->i_private;
  1761. struct seq_file *s;
  1762. int err;
  1763. err = seq_open(file, &tegra_pcie_ports_seq_ops);
  1764. if (err)
  1765. return err;
  1766. s = file->private_data;
  1767. s->private = pcie;
  1768. return 0;
  1769. }
  1770. static const struct file_operations tegra_pcie_ports_ops = {
  1771. .owner = THIS_MODULE,
  1772. .open = tegra_pcie_ports_open,
  1773. .read = seq_read,
  1774. .llseek = seq_lseek,
  1775. .release = seq_release,
  1776. };
  1777. static int tegra_pcie_debugfs_init(struct tegra_pcie *pcie)
  1778. {
  1779. struct dentry *file;
  1780. pcie->debugfs = debugfs_create_dir("pcie", NULL);
  1781. if (!pcie->debugfs)
  1782. return -ENOMEM;
  1783. file = debugfs_create_file("ports", S_IFREG | S_IRUGO, pcie->debugfs,
  1784. pcie, &tegra_pcie_ports_ops);
  1785. if (!file)
  1786. goto remove;
  1787. return 0;
  1788. remove:
  1789. debugfs_remove_recursive(pcie->debugfs);
  1790. pcie->debugfs = NULL;
  1791. return -ENOMEM;
  1792. }
  1793. static int tegra_pcie_probe(struct platform_device *pdev)
  1794. {
  1795. struct device *dev = &pdev->dev;
  1796. struct tegra_pcie *pcie;
  1797. int err;
  1798. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  1799. if (!pcie)
  1800. return -ENOMEM;
  1801. pcie->soc = of_device_get_match_data(dev);
  1802. INIT_LIST_HEAD(&pcie->buses);
  1803. INIT_LIST_HEAD(&pcie->ports);
  1804. pcie->dev = dev;
  1805. err = tegra_pcie_parse_dt(pcie);
  1806. if (err < 0)
  1807. return err;
  1808. err = tegra_pcie_get_resources(pcie);
  1809. if (err < 0) {
  1810. dev_err(dev, "failed to request resources: %d\n", err);
  1811. return err;
  1812. }
  1813. err = tegra_pcie_enable_controller(pcie);
  1814. if (err)
  1815. goto put_resources;
  1816. /* setup the AFI address translations */
  1817. tegra_pcie_setup_translations(pcie);
  1818. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  1819. err = tegra_pcie_enable_msi(pcie);
  1820. if (err < 0) {
  1821. dev_err(dev, "failed to enable MSI support: %d\n", err);
  1822. goto put_resources;
  1823. }
  1824. }
  1825. err = tegra_pcie_enable(pcie);
  1826. if (err < 0) {
  1827. dev_err(dev, "failed to enable PCIe ports: %d\n", err);
  1828. goto disable_msi;
  1829. }
  1830. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  1831. err = tegra_pcie_debugfs_init(pcie);
  1832. if (err < 0)
  1833. dev_err(dev, "failed to setup debugfs: %d\n", err);
  1834. }
  1835. return 0;
  1836. disable_msi:
  1837. if (IS_ENABLED(CONFIG_PCI_MSI))
  1838. tegra_pcie_disable_msi(pcie);
  1839. put_resources:
  1840. tegra_pcie_put_resources(pcie);
  1841. return err;
  1842. }
  1843. static struct platform_driver tegra_pcie_driver = {
  1844. .driver = {
  1845. .name = "tegra-pcie",
  1846. .of_match_table = tegra_pcie_of_match,
  1847. .suppress_bind_attrs = true,
  1848. },
  1849. .probe = tegra_pcie_probe,
  1850. };
  1851. builtin_platform_driver(tegra_pcie_driver);