pci-tegra.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  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/clk/tegra.h>
  28. #include <linux/delay.h>
  29. #include <linux/export.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/irq.h>
  32. #include <linux/irqdomain.h>
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/msi.h>
  36. #include <linux/of_address.h>
  37. #include <linux/of_pci.h>
  38. #include <linux/of_platform.h>
  39. #include <linux/pci.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/sizes.h>
  42. #include <linux/slab.h>
  43. #include <linux/tegra-cpuidle.h>
  44. #include <linux/tegra-powergate.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/regulator/consumer.h>
  47. #include <asm/mach/irq.h>
  48. #include <asm/mach/map.h>
  49. #include <asm/mach/pci.h>
  50. #define INT_PCI_MSI_NR (8 * 32)
  51. /* register definitions */
  52. #define AFI_AXI_BAR0_SZ 0x00
  53. #define AFI_AXI_BAR1_SZ 0x04
  54. #define AFI_AXI_BAR2_SZ 0x08
  55. #define AFI_AXI_BAR3_SZ 0x0c
  56. #define AFI_AXI_BAR4_SZ 0x10
  57. #define AFI_AXI_BAR5_SZ 0x14
  58. #define AFI_AXI_BAR0_START 0x18
  59. #define AFI_AXI_BAR1_START 0x1c
  60. #define AFI_AXI_BAR2_START 0x20
  61. #define AFI_AXI_BAR3_START 0x24
  62. #define AFI_AXI_BAR4_START 0x28
  63. #define AFI_AXI_BAR5_START 0x2c
  64. #define AFI_FPCI_BAR0 0x30
  65. #define AFI_FPCI_BAR1 0x34
  66. #define AFI_FPCI_BAR2 0x38
  67. #define AFI_FPCI_BAR3 0x3c
  68. #define AFI_FPCI_BAR4 0x40
  69. #define AFI_FPCI_BAR5 0x44
  70. #define AFI_CACHE_BAR0_SZ 0x48
  71. #define AFI_CACHE_BAR0_ST 0x4c
  72. #define AFI_CACHE_BAR1_SZ 0x50
  73. #define AFI_CACHE_BAR1_ST 0x54
  74. #define AFI_MSI_BAR_SZ 0x60
  75. #define AFI_MSI_FPCI_BAR_ST 0x64
  76. #define AFI_MSI_AXI_BAR_ST 0x68
  77. #define AFI_MSI_VEC0 0x6c
  78. #define AFI_MSI_VEC1 0x70
  79. #define AFI_MSI_VEC2 0x74
  80. #define AFI_MSI_VEC3 0x78
  81. #define AFI_MSI_VEC4 0x7c
  82. #define AFI_MSI_VEC5 0x80
  83. #define AFI_MSI_VEC6 0x84
  84. #define AFI_MSI_VEC7 0x88
  85. #define AFI_MSI_EN_VEC0 0x8c
  86. #define AFI_MSI_EN_VEC1 0x90
  87. #define AFI_MSI_EN_VEC2 0x94
  88. #define AFI_MSI_EN_VEC3 0x98
  89. #define AFI_MSI_EN_VEC4 0x9c
  90. #define AFI_MSI_EN_VEC5 0xa0
  91. #define AFI_MSI_EN_VEC6 0xa4
  92. #define AFI_MSI_EN_VEC7 0xa8
  93. #define AFI_CONFIGURATION 0xac
  94. #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
  95. #define AFI_FPCI_ERROR_MASKS 0xb0
  96. #define AFI_INTR_MASK 0xb4
  97. #define AFI_INTR_MASK_INT_MASK (1 << 0)
  98. #define AFI_INTR_MASK_MSI_MASK (1 << 8)
  99. #define AFI_INTR_CODE 0xb8
  100. #define AFI_INTR_CODE_MASK 0xf
  101. #define AFI_INTR_AXI_SLAVE_ERROR 1
  102. #define AFI_INTR_AXI_DECODE_ERROR 2
  103. #define AFI_INTR_TARGET_ABORT 3
  104. #define AFI_INTR_MASTER_ABORT 4
  105. #define AFI_INTR_INVALID_WRITE 5
  106. #define AFI_INTR_LEGACY 6
  107. #define AFI_INTR_FPCI_DECODE_ERROR 7
  108. #define AFI_INTR_SIGNATURE 0xbc
  109. #define AFI_UPPER_FPCI_ADDRESS 0xc0
  110. #define AFI_SM_INTR_ENABLE 0xc4
  111. #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
  112. #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
  113. #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
  114. #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
  115. #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
  116. #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
  117. #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
  118. #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
  119. #define AFI_AFI_INTR_ENABLE 0xc8
  120. #define AFI_INTR_EN_INI_SLVERR (1 << 0)
  121. #define AFI_INTR_EN_INI_DECERR (1 << 1)
  122. #define AFI_INTR_EN_TGT_SLVERR (1 << 2)
  123. #define AFI_INTR_EN_TGT_DECERR (1 << 3)
  124. #define AFI_INTR_EN_TGT_WRERR (1 << 4)
  125. #define AFI_INTR_EN_DFPCI_DECERR (1 << 5)
  126. #define AFI_INTR_EN_AXI_DECERR (1 << 6)
  127. #define AFI_INTR_EN_FPCI_TIMEOUT (1 << 7)
  128. #define AFI_INTR_EN_PRSNT_SENSE (1 << 8)
  129. #define AFI_PCIE_CONFIG 0x0f8
  130. #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
  131. #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0xe
  132. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
  133. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE (0x0 << 20)
  134. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420 (0x0 << 20)
  135. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL (0x1 << 20)
  136. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222 (0x1 << 20)
  137. #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411 (0x2 << 20)
  138. #define AFI_FUSE 0x104
  139. #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
  140. #define AFI_PEX0_CTRL 0x110
  141. #define AFI_PEX1_CTRL 0x118
  142. #define AFI_PEX2_CTRL 0x128
  143. #define AFI_PEX_CTRL_RST (1 << 0)
  144. #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
  145. #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
  146. #define AFI_PEXBIAS_CTRL_0 0x168
  147. #define RP_VEND_XP 0x00000F00
  148. #define RP_VEND_XP_DL_UP (1 << 30)
  149. #define RP_LINK_CONTROL_STATUS 0x00000090
  150. #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
  151. #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
  152. #define PADS_CTL_SEL 0x0000009C
  153. #define PADS_CTL 0x000000A0
  154. #define PADS_CTL_IDDQ_1L (1 << 0)
  155. #define PADS_CTL_TX_DATA_EN_1L (1 << 6)
  156. #define PADS_CTL_RX_DATA_EN_1L (1 << 10)
  157. #define PADS_PLL_CTL_TEGRA20 0x000000B8
  158. #define PADS_PLL_CTL_TEGRA30 0x000000B4
  159. #define PADS_PLL_CTL_RST_B4SM (1 << 1)
  160. #define PADS_PLL_CTL_LOCKDET (1 << 8)
  161. #define PADS_PLL_CTL_REFCLK_MASK (0x3 << 16)
  162. #define PADS_PLL_CTL_REFCLK_INTERNAL_CML (0 << 16)
  163. #define PADS_PLL_CTL_REFCLK_INTERNAL_CMOS (1 << 16)
  164. #define PADS_PLL_CTL_REFCLK_EXTERNAL (2 << 16)
  165. #define PADS_PLL_CTL_TXCLKREF_MASK (0x1 << 20)
  166. #define PADS_PLL_CTL_TXCLKREF_DIV10 (0 << 20)
  167. #define PADS_PLL_CTL_TXCLKREF_DIV5 (1 << 20)
  168. #define PADS_PLL_CTL_TXCLKREF_BUF_EN (1 << 22)
  169. #define PADS_REFCLK_CFG0 0x000000C8
  170. #define PADS_REFCLK_CFG1 0x000000CC
  171. /*
  172. * Fields in PADS_REFCLK_CFG*. Those registers form an array of 16-bit
  173. * entries, one entry per PCIe port. These field definitions and desired
  174. * values aren't in the TRM, but do come from NVIDIA.
  175. */
  176. #define PADS_REFCLK_CFG_TERM_SHIFT 2 /* 6:2 */
  177. #define PADS_REFCLK_CFG_E_TERM_SHIFT 7
  178. #define PADS_REFCLK_CFG_PREDI_SHIFT 8 /* 11:8 */
  179. #define PADS_REFCLK_CFG_DRVI_SHIFT 12 /* 15:12 */
  180. /* Default value provided by HW engineering is 0xfa5c */
  181. #define PADS_REFCLK_CFG_VALUE \
  182. ( \
  183. (0x17 << PADS_REFCLK_CFG_TERM_SHIFT) | \
  184. (0 << PADS_REFCLK_CFG_E_TERM_SHIFT) | \
  185. (0xa << PADS_REFCLK_CFG_PREDI_SHIFT) | \
  186. (0xf << PADS_REFCLK_CFG_DRVI_SHIFT) \
  187. )
  188. struct tegra_msi {
  189. struct msi_chip chip;
  190. DECLARE_BITMAP(used, INT_PCI_MSI_NR);
  191. struct irq_domain *domain;
  192. unsigned long pages;
  193. struct mutex lock;
  194. int irq;
  195. };
  196. /* used to differentiate between Tegra SoC generations */
  197. struct tegra_pcie_soc_data {
  198. unsigned int num_ports;
  199. unsigned int msi_base_shift;
  200. u32 pads_pll_ctl;
  201. u32 tx_ref_sel;
  202. bool has_pex_clkreq_en;
  203. bool has_pex_bias_ctrl;
  204. bool has_intr_prsnt_sense;
  205. bool has_avdd_supply;
  206. bool has_cml_clk;
  207. };
  208. static inline struct tegra_msi *to_tegra_msi(struct msi_chip *chip)
  209. {
  210. return container_of(chip, struct tegra_msi, chip);
  211. }
  212. struct tegra_pcie {
  213. struct device *dev;
  214. void __iomem *pads;
  215. void __iomem *afi;
  216. int irq;
  217. struct list_head busses;
  218. struct resource *cs;
  219. struct resource io;
  220. struct resource mem;
  221. struct resource prefetch;
  222. struct resource busn;
  223. struct clk *pex_clk;
  224. struct clk *afi_clk;
  225. struct clk *pcie_xclk;
  226. struct clk *pll_e;
  227. struct clk *cml_clk;
  228. struct tegra_msi msi;
  229. struct list_head ports;
  230. unsigned int num_ports;
  231. u32 xbar_config;
  232. struct regulator *pex_clk_supply;
  233. struct regulator *vdd_supply;
  234. struct regulator *avdd_supply;
  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 existis, 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->busses, list)
  351. if (bus->nr == busnr)
  352. return 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->busses);
  357. return 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. tegra_cpuidle_pcie_irqs_in_use();
  533. return pcie->irq;
  534. }
  535. static void tegra_pcie_add_bus(struct pci_bus *bus)
  536. {
  537. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  538. struct tegra_pcie *pcie = sys_to_pcie(bus->sysdata);
  539. bus->msi = &pcie->msi.chip;
  540. }
  541. }
  542. static struct pci_bus *tegra_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  543. {
  544. struct tegra_pcie *pcie = sys_to_pcie(sys);
  545. struct pci_bus *bus;
  546. bus = pci_create_root_bus(pcie->dev, sys->busnr, &tegra_pcie_ops, sys,
  547. &sys->resources);
  548. if (!bus)
  549. return NULL;
  550. pci_scan_child_bus(bus);
  551. return bus;
  552. }
  553. static irqreturn_t tegra_pcie_isr(int irq, void *arg)
  554. {
  555. const char *err_msg[] = {
  556. "Unknown",
  557. "AXI slave error",
  558. "AXI decode error",
  559. "Target abort",
  560. "Master abort",
  561. "Invalid write",
  562. "Response decoding error",
  563. "AXI response decoding error",
  564. "Transaction timeout",
  565. };
  566. struct tegra_pcie *pcie = arg;
  567. u32 code, signature;
  568. code = afi_readl(pcie, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
  569. signature = afi_readl(pcie, AFI_INTR_SIGNATURE);
  570. afi_writel(pcie, 0, AFI_INTR_CODE);
  571. if (code == AFI_INTR_LEGACY)
  572. return IRQ_NONE;
  573. if (code >= ARRAY_SIZE(err_msg))
  574. code = 0;
  575. /*
  576. * do not pollute kernel log with master abort reports since they
  577. * happen a lot during enumeration
  578. */
  579. if (code == AFI_INTR_MASTER_ABORT)
  580. dev_dbg(pcie->dev, "%s, signature: %08x\n", err_msg[code],
  581. signature);
  582. else
  583. dev_err(pcie->dev, "%s, signature: %08x\n", err_msg[code],
  584. signature);
  585. if (code == AFI_INTR_TARGET_ABORT || code == AFI_INTR_MASTER_ABORT ||
  586. code == AFI_INTR_FPCI_DECODE_ERROR) {
  587. u32 fpci = afi_readl(pcie, AFI_UPPER_FPCI_ADDRESS) & 0xff;
  588. u64 address = (u64)fpci << 32 | (signature & 0xfffffffc);
  589. if (code == AFI_INTR_MASTER_ABORT)
  590. dev_dbg(pcie->dev, " FPCI address: %10llx\n", address);
  591. else
  592. dev_err(pcie->dev, " FPCI address: %10llx\n", address);
  593. }
  594. return IRQ_HANDLED;
  595. }
  596. /*
  597. * FPCI map is as follows:
  598. * - 0xfdfc000000: I/O space
  599. * - 0xfdfe000000: type 0 configuration space
  600. * - 0xfdff000000: type 1 configuration space
  601. * - 0xfe00000000: type 0 extended configuration space
  602. * - 0xfe10000000: type 1 extended configuration space
  603. */
  604. static void tegra_pcie_setup_translations(struct tegra_pcie *pcie)
  605. {
  606. u32 fpci_bar, size, axi_address;
  607. /* Bar 0: type 1 extended configuration space */
  608. fpci_bar = 0xfe100000;
  609. size = resource_size(pcie->cs);
  610. axi_address = pcie->cs->start;
  611. afi_writel(pcie, axi_address, AFI_AXI_BAR0_START);
  612. afi_writel(pcie, size >> 12, AFI_AXI_BAR0_SZ);
  613. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR0);
  614. /* Bar 1: downstream IO bar */
  615. fpci_bar = 0xfdfc0000;
  616. size = resource_size(&pcie->io);
  617. axi_address = pcie->io.start;
  618. afi_writel(pcie, axi_address, AFI_AXI_BAR1_START);
  619. afi_writel(pcie, size >> 12, AFI_AXI_BAR1_SZ);
  620. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR1);
  621. /* Bar 2: prefetchable memory BAR */
  622. fpci_bar = (((pcie->prefetch.start >> 12) & 0x0fffffff) << 4) | 0x1;
  623. size = resource_size(&pcie->prefetch);
  624. axi_address = pcie->prefetch.start;
  625. afi_writel(pcie, axi_address, AFI_AXI_BAR2_START);
  626. afi_writel(pcie, size >> 12, AFI_AXI_BAR2_SZ);
  627. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR2);
  628. /* Bar 3: non prefetchable memory BAR */
  629. fpci_bar = (((pcie->mem.start >> 12) & 0x0fffffff) << 4) | 0x1;
  630. size = resource_size(&pcie->mem);
  631. axi_address = pcie->mem.start;
  632. afi_writel(pcie, axi_address, AFI_AXI_BAR3_START);
  633. afi_writel(pcie, size >> 12, AFI_AXI_BAR3_SZ);
  634. afi_writel(pcie, fpci_bar, AFI_FPCI_BAR3);
  635. /* NULL out the remaining BARs as they are not used */
  636. afi_writel(pcie, 0, AFI_AXI_BAR4_START);
  637. afi_writel(pcie, 0, AFI_AXI_BAR4_SZ);
  638. afi_writel(pcie, 0, AFI_FPCI_BAR4);
  639. afi_writel(pcie, 0, AFI_AXI_BAR5_START);
  640. afi_writel(pcie, 0, AFI_AXI_BAR5_SZ);
  641. afi_writel(pcie, 0, AFI_FPCI_BAR5);
  642. /* map all upstream transactions as uncached */
  643. afi_writel(pcie, PHYS_OFFSET, AFI_CACHE_BAR0_ST);
  644. afi_writel(pcie, 0, AFI_CACHE_BAR0_SZ);
  645. afi_writel(pcie, 0, AFI_CACHE_BAR1_ST);
  646. afi_writel(pcie, 0, AFI_CACHE_BAR1_SZ);
  647. /* MSI translations are setup only when needed */
  648. afi_writel(pcie, 0, AFI_MSI_FPCI_BAR_ST);
  649. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  650. afi_writel(pcie, 0, AFI_MSI_AXI_BAR_ST);
  651. afi_writel(pcie, 0, AFI_MSI_BAR_SZ);
  652. }
  653. static int tegra_pcie_enable_controller(struct tegra_pcie *pcie)
  654. {
  655. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  656. struct tegra_pcie_port *port;
  657. unsigned int timeout;
  658. unsigned long value;
  659. /* power down PCIe slot clock bias pad */
  660. if (soc->has_pex_bias_ctrl)
  661. afi_writel(pcie, 0, AFI_PEXBIAS_CTRL_0);
  662. /* configure mode and disable all ports */
  663. value = afi_readl(pcie, AFI_PCIE_CONFIG);
  664. value &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
  665. value |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL | pcie->xbar_config;
  666. list_for_each_entry(port, &pcie->ports, list)
  667. value &= ~AFI_PCIE_CONFIG_PCIE_DISABLE(port->index);
  668. afi_writel(pcie, value, AFI_PCIE_CONFIG);
  669. value = afi_readl(pcie, AFI_FUSE);
  670. value &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
  671. afi_writel(pcie, value, AFI_FUSE);
  672. /* initialze internal PHY, enable up to 16 PCIE lanes */
  673. pads_writel(pcie, 0x0, PADS_CTL_SEL);
  674. /* override IDDQ to 1 on all 4 lanes */
  675. value = pads_readl(pcie, PADS_CTL);
  676. value |= PADS_CTL_IDDQ_1L;
  677. pads_writel(pcie, value, PADS_CTL);
  678. /*
  679. * Set up PHY PLL inputs select PLLE output as refclock,
  680. * set TX ref sel to div10 (not div5).
  681. */
  682. value = pads_readl(pcie, soc->pads_pll_ctl);
  683. value &= ~(PADS_PLL_CTL_REFCLK_MASK | PADS_PLL_CTL_TXCLKREF_MASK);
  684. value |= PADS_PLL_CTL_REFCLK_INTERNAL_CML | soc->tx_ref_sel;
  685. pads_writel(pcie, value, soc->pads_pll_ctl);
  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. /* Configure the reference clock driver */
  691. value = PADS_REFCLK_CFG_VALUE | (PADS_REFCLK_CFG_VALUE << 16);
  692. pads_writel(pcie, value, PADS_REFCLK_CFG0);
  693. if (soc->num_ports > 2)
  694. pads_writel(pcie, PADS_REFCLK_CFG_VALUE, PADS_REFCLK_CFG1);
  695. /* wait for the PLL to lock */
  696. timeout = 300;
  697. do {
  698. value = pads_readl(pcie, soc->pads_pll_ctl);
  699. usleep_range(1000, 2000);
  700. if (--timeout == 0) {
  701. pr_err("Tegra PCIe error: timeout waiting for PLL\n");
  702. return -EBUSY;
  703. }
  704. } while (!(value & PADS_PLL_CTL_LOCKDET));
  705. /* turn off IDDQ override */
  706. value = pads_readl(pcie, PADS_CTL);
  707. value &= ~PADS_CTL_IDDQ_1L;
  708. pads_writel(pcie, value, PADS_CTL);
  709. /* enable TX/RX data */
  710. value = pads_readl(pcie, PADS_CTL);
  711. value |= PADS_CTL_TX_DATA_EN_1L | PADS_CTL_RX_DATA_EN_1L;
  712. pads_writel(pcie, value, PADS_CTL);
  713. /* take the PCIe interface module out of reset */
  714. tegra_periph_reset_deassert(pcie->pcie_xclk);
  715. /* finally enable PCIe */
  716. value = afi_readl(pcie, AFI_CONFIGURATION);
  717. value |= AFI_CONFIGURATION_EN_FPCI;
  718. afi_writel(pcie, value, AFI_CONFIGURATION);
  719. value = AFI_INTR_EN_INI_SLVERR | AFI_INTR_EN_INI_DECERR |
  720. AFI_INTR_EN_TGT_SLVERR | AFI_INTR_EN_TGT_DECERR |
  721. AFI_INTR_EN_TGT_WRERR | AFI_INTR_EN_DFPCI_DECERR;
  722. if (soc->has_intr_prsnt_sense)
  723. value |= AFI_INTR_EN_PRSNT_SENSE;
  724. afi_writel(pcie, value, AFI_AFI_INTR_ENABLE);
  725. afi_writel(pcie, 0xffffffff, AFI_SM_INTR_ENABLE);
  726. /* don't enable MSI for now, only when needed */
  727. afi_writel(pcie, AFI_INTR_MASK_INT_MASK, AFI_INTR_MASK);
  728. /* disable all exceptions */
  729. afi_writel(pcie, 0, AFI_FPCI_ERROR_MASKS);
  730. return 0;
  731. }
  732. static void tegra_pcie_power_off(struct tegra_pcie *pcie)
  733. {
  734. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  735. int err;
  736. /* TODO: disable and unprepare clocks? */
  737. tegra_periph_reset_assert(pcie->pcie_xclk);
  738. tegra_periph_reset_assert(pcie->afi_clk);
  739. tegra_periph_reset_assert(pcie->pex_clk);
  740. tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  741. if (soc->has_avdd_supply) {
  742. err = regulator_disable(pcie->avdd_supply);
  743. if (err < 0)
  744. dev_warn(pcie->dev,
  745. "failed to disable AVDD regulator: %d\n",
  746. err);
  747. }
  748. err = regulator_disable(pcie->pex_clk_supply);
  749. if (err < 0)
  750. dev_warn(pcie->dev, "failed to disable pex-clk regulator: %d\n",
  751. err);
  752. err = regulator_disable(pcie->vdd_supply);
  753. if (err < 0)
  754. dev_warn(pcie->dev, "failed to disable VDD regulator: %d\n",
  755. err);
  756. }
  757. static int tegra_pcie_power_on(struct tegra_pcie *pcie)
  758. {
  759. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  760. int err;
  761. tegra_periph_reset_assert(pcie->pcie_xclk);
  762. tegra_periph_reset_assert(pcie->afi_clk);
  763. tegra_periph_reset_assert(pcie->pex_clk);
  764. tegra_powergate_power_off(TEGRA_POWERGATE_PCIE);
  765. /* enable regulators */
  766. err = regulator_enable(pcie->vdd_supply);
  767. if (err < 0) {
  768. dev_err(pcie->dev, "failed to enable VDD regulator: %d\n", err);
  769. return err;
  770. }
  771. err = regulator_enable(pcie->pex_clk_supply);
  772. if (err < 0) {
  773. dev_err(pcie->dev, "failed to enable pex-clk regulator: %d\n",
  774. err);
  775. return err;
  776. }
  777. if (soc->has_avdd_supply) {
  778. err = regulator_enable(pcie->avdd_supply);
  779. if (err < 0) {
  780. dev_err(pcie->dev,
  781. "failed to enable AVDD regulator: %d\n",
  782. err);
  783. return err;
  784. }
  785. }
  786. err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCIE,
  787. pcie->pex_clk);
  788. if (err) {
  789. dev_err(pcie->dev, "powerup sequence failed: %d\n", err);
  790. return err;
  791. }
  792. tegra_periph_reset_deassert(pcie->afi_clk);
  793. err = clk_prepare_enable(pcie->afi_clk);
  794. if (err < 0) {
  795. dev_err(pcie->dev, "failed to enable AFI clock: %d\n", err);
  796. return err;
  797. }
  798. if (soc->has_cml_clk) {
  799. err = clk_prepare_enable(pcie->cml_clk);
  800. if (err < 0) {
  801. dev_err(pcie->dev, "failed to enable CML clock: %d\n",
  802. err);
  803. return err;
  804. }
  805. }
  806. err = clk_prepare_enable(pcie->pll_e);
  807. if (err < 0) {
  808. dev_err(pcie->dev, "failed to enable PLLE clock: %d\n", err);
  809. return err;
  810. }
  811. return 0;
  812. }
  813. static int tegra_pcie_clocks_get(struct tegra_pcie *pcie)
  814. {
  815. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  816. pcie->pex_clk = devm_clk_get(pcie->dev, "pex");
  817. if (IS_ERR(pcie->pex_clk))
  818. return PTR_ERR(pcie->pex_clk);
  819. pcie->afi_clk = devm_clk_get(pcie->dev, "afi");
  820. if (IS_ERR(pcie->afi_clk))
  821. return PTR_ERR(pcie->afi_clk);
  822. pcie->pcie_xclk = devm_clk_get(pcie->dev, "pcie_xclk");
  823. if (IS_ERR(pcie->pcie_xclk))
  824. return PTR_ERR(pcie->pcie_xclk);
  825. pcie->pll_e = devm_clk_get(pcie->dev, "pll_e");
  826. if (IS_ERR(pcie->pll_e))
  827. return PTR_ERR(pcie->pll_e);
  828. if (soc->has_cml_clk) {
  829. pcie->cml_clk = devm_clk_get(pcie->dev, "cml");
  830. if (IS_ERR(pcie->cml_clk))
  831. return PTR_ERR(pcie->cml_clk);
  832. }
  833. return 0;
  834. }
  835. static int tegra_pcie_get_resources(struct tegra_pcie *pcie)
  836. {
  837. struct platform_device *pdev = to_platform_device(pcie->dev);
  838. struct resource *pads, *afi, *res;
  839. int err;
  840. err = tegra_pcie_clocks_get(pcie);
  841. if (err) {
  842. dev_err(&pdev->dev, "failed to get clocks: %d\n", err);
  843. return err;
  844. }
  845. err = tegra_pcie_power_on(pcie);
  846. if (err) {
  847. dev_err(&pdev->dev, "failed to power up: %d\n", err);
  848. return err;
  849. }
  850. pads = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pads");
  851. pcie->pads = devm_ioremap_resource(&pdev->dev, pads);
  852. if (IS_ERR(pcie->pads)) {
  853. err = PTR_ERR(pcie->pads);
  854. goto poweroff;
  855. }
  856. afi = platform_get_resource_byname(pdev, IORESOURCE_MEM, "afi");
  857. pcie->afi = devm_ioremap_resource(&pdev->dev, afi);
  858. if (IS_ERR(pcie->afi)) {
  859. err = PTR_ERR(pcie->afi);
  860. goto poweroff;
  861. }
  862. /* request configuration space, but remap later, on demand */
  863. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs");
  864. if (!res) {
  865. err = -EADDRNOTAVAIL;
  866. goto poweroff;
  867. }
  868. pcie->cs = devm_request_mem_region(pcie->dev, res->start,
  869. resource_size(res), res->name);
  870. if (!pcie->cs) {
  871. err = -EADDRNOTAVAIL;
  872. goto poweroff;
  873. }
  874. /* request interrupt */
  875. err = platform_get_irq_byname(pdev, "intr");
  876. if (err < 0) {
  877. dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
  878. goto poweroff;
  879. }
  880. pcie->irq = err;
  881. err = request_irq(pcie->irq, tegra_pcie_isr, IRQF_SHARED, "PCIE", pcie);
  882. if (err) {
  883. dev_err(&pdev->dev, "failed to register IRQ: %d\n", err);
  884. goto poweroff;
  885. }
  886. return 0;
  887. poweroff:
  888. tegra_pcie_power_off(pcie);
  889. return err;
  890. }
  891. static int tegra_pcie_put_resources(struct tegra_pcie *pcie)
  892. {
  893. if (pcie->irq > 0)
  894. free_irq(pcie->irq, pcie);
  895. tegra_pcie_power_off(pcie);
  896. return 0;
  897. }
  898. static int tegra_msi_alloc(struct tegra_msi *chip)
  899. {
  900. int msi;
  901. mutex_lock(&chip->lock);
  902. msi = find_first_zero_bit(chip->used, INT_PCI_MSI_NR);
  903. if (msi < INT_PCI_MSI_NR)
  904. set_bit(msi, chip->used);
  905. else
  906. msi = -ENOSPC;
  907. mutex_unlock(&chip->lock);
  908. return msi;
  909. }
  910. static void tegra_msi_free(struct tegra_msi *chip, unsigned long irq)
  911. {
  912. struct device *dev = chip->chip.dev;
  913. mutex_lock(&chip->lock);
  914. if (!test_bit(irq, chip->used))
  915. dev_err(dev, "trying to free unused MSI#%lu\n", irq);
  916. else
  917. clear_bit(irq, chip->used);
  918. mutex_unlock(&chip->lock);
  919. }
  920. static irqreturn_t tegra_pcie_msi_irq(int irq, void *data)
  921. {
  922. struct tegra_pcie *pcie = data;
  923. struct tegra_msi *msi = &pcie->msi;
  924. unsigned int i, processed = 0;
  925. for (i = 0; i < 8; i++) {
  926. unsigned long reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
  927. while (reg) {
  928. unsigned int offset = find_first_bit(&reg, 32);
  929. unsigned int index = i * 32 + offset;
  930. unsigned int irq;
  931. /* clear the interrupt */
  932. afi_writel(pcie, 1 << offset, AFI_MSI_VEC0 + i * 4);
  933. irq = irq_find_mapping(msi->domain, index);
  934. if (irq) {
  935. if (test_bit(index, msi->used))
  936. generic_handle_irq(irq);
  937. else
  938. dev_info(pcie->dev, "unhandled MSI\n");
  939. } else {
  940. /*
  941. * that's weird who triggered this?
  942. * just clear it
  943. */
  944. dev_info(pcie->dev, "unexpected MSI\n");
  945. }
  946. /* see if there's any more pending in this vector */
  947. reg = afi_readl(pcie, AFI_MSI_VEC0 + i * 4);
  948. processed++;
  949. }
  950. }
  951. return processed > 0 ? IRQ_HANDLED : IRQ_NONE;
  952. }
  953. static int tegra_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
  954. struct msi_desc *desc)
  955. {
  956. struct tegra_msi *msi = to_tegra_msi(chip);
  957. struct msi_msg msg;
  958. unsigned int irq;
  959. int hwirq;
  960. hwirq = tegra_msi_alloc(msi);
  961. if (hwirq < 0)
  962. return hwirq;
  963. irq = irq_create_mapping(msi->domain, hwirq);
  964. if (!irq)
  965. return -EINVAL;
  966. irq_set_msi_desc(irq, desc);
  967. msg.address_lo = virt_to_phys((void *)msi->pages);
  968. /* 32 bit address only */
  969. msg.address_hi = 0;
  970. msg.data = hwirq;
  971. write_msi_msg(irq, &msg);
  972. return 0;
  973. }
  974. static void tegra_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
  975. {
  976. struct tegra_msi *msi = to_tegra_msi(chip);
  977. struct irq_data *d = irq_get_irq_data(irq);
  978. tegra_msi_free(msi, d->hwirq);
  979. }
  980. static struct irq_chip tegra_msi_irq_chip = {
  981. .name = "Tegra PCIe MSI",
  982. .irq_enable = unmask_msi_irq,
  983. .irq_disable = mask_msi_irq,
  984. .irq_mask = mask_msi_irq,
  985. .irq_unmask = unmask_msi_irq,
  986. };
  987. static int tegra_msi_map(struct irq_domain *domain, unsigned int irq,
  988. irq_hw_number_t hwirq)
  989. {
  990. irq_set_chip_and_handler(irq, &tegra_msi_irq_chip, handle_simple_irq);
  991. irq_set_chip_data(irq, domain->host_data);
  992. set_irq_flags(irq, IRQF_VALID);
  993. tegra_cpuidle_pcie_irqs_in_use();
  994. return 0;
  995. }
  996. static const struct irq_domain_ops msi_domain_ops = {
  997. .map = tegra_msi_map,
  998. };
  999. static int tegra_pcie_enable_msi(struct tegra_pcie *pcie)
  1000. {
  1001. struct platform_device *pdev = to_platform_device(pcie->dev);
  1002. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  1003. struct tegra_msi *msi = &pcie->msi;
  1004. unsigned long base;
  1005. int err;
  1006. u32 reg;
  1007. mutex_init(&msi->lock);
  1008. msi->chip.dev = pcie->dev;
  1009. msi->chip.setup_irq = tegra_msi_setup_irq;
  1010. msi->chip.teardown_irq = tegra_msi_teardown_irq;
  1011. msi->domain = irq_domain_add_linear(pcie->dev->of_node, INT_PCI_MSI_NR,
  1012. &msi_domain_ops, &msi->chip);
  1013. if (!msi->domain) {
  1014. dev_err(&pdev->dev, "failed to create IRQ domain\n");
  1015. return -ENOMEM;
  1016. }
  1017. err = platform_get_irq_byname(pdev, "msi");
  1018. if (err < 0) {
  1019. dev_err(&pdev->dev, "failed to get IRQ: %d\n", err);
  1020. goto err;
  1021. }
  1022. msi->irq = err;
  1023. err = request_irq(msi->irq, tegra_pcie_msi_irq, 0,
  1024. tegra_msi_irq_chip.name, pcie);
  1025. if (err < 0) {
  1026. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  1027. goto err;
  1028. }
  1029. /* setup AFI/FPCI range */
  1030. msi->pages = __get_free_pages(GFP_KERNEL, 0);
  1031. base = virt_to_phys((void *)msi->pages);
  1032. afi_writel(pcie, base >> soc->msi_base_shift, AFI_MSI_FPCI_BAR_ST);
  1033. afi_writel(pcie, base, AFI_MSI_AXI_BAR_ST);
  1034. /* this register is in 4K increments */
  1035. afi_writel(pcie, 1, AFI_MSI_BAR_SZ);
  1036. /* enable all MSI vectors */
  1037. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC0);
  1038. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC1);
  1039. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC2);
  1040. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC3);
  1041. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC4);
  1042. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC5);
  1043. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC6);
  1044. afi_writel(pcie, 0xffffffff, AFI_MSI_EN_VEC7);
  1045. /* and unmask the MSI interrupt */
  1046. reg = afi_readl(pcie, AFI_INTR_MASK);
  1047. reg |= AFI_INTR_MASK_MSI_MASK;
  1048. afi_writel(pcie, reg, AFI_INTR_MASK);
  1049. return 0;
  1050. err:
  1051. irq_domain_remove(msi->domain);
  1052. return err;
  1053. }
  1054. static int tegra_pcie_disable_msi(struct tegra_pcie *pcie)
  1055. {
  1056. struct tegra_msi *msi = &pcie->msi;
  1057. unsigned int i, irq;
  1058. u32 value;
  1059. /* mask the MSI interrupt */
  1060. value = afi_readl(pcie, AFI_INTR_MASK);
  1061. value &= ~AFI_INTR_MASK_MSI_MASK;
  1062. afi_writel(pcie, value, AFI_INTR_MASK);
  1063. /* disable all MSI vectors */
  1064. afi_writel(pcie, 0, AFI_MSI_EN_VEC0);
  1065. afi_writel(pcie, 0, AFI_MSI_EN_VEC1);
  1066. afi_writel(pcie, 0, AFI_MSI_EN_VEC2);
  1067. afi_writel(pcie, 0, AFI_MSI_EN_VEC3);
  1068. afi_writel(pcie, 0, AFI_MSI_EN_VEC4);
  1069. afi_writel(pcie, 0, AFI_MSI_EN_VEC5);
  1070. afi_writel(pcie, 0, AFI_MSI_EN_VEC6);
  1071. afi_writel(pcie, 0, AFI_MSI_EN_VEC7);
  1072. free_pages(msi->pages, 0);
  1073. if (msi->irq > 0)
  1074. free_irq(msi->irq, pcie);
  1075. for (i = 0; i < INT_PCI_MSI_NR; i++) {
  1076. irq = irq_find_mapping(msi->domain, i);
  1077. if (irq > 0)
  1078. irq_dispose_mapping(irq);
  1079. }
  1080. irq_domain_remove(msi->domain);
  1081. return 0;
  1082. }
  1083. static int tegra_pcie_get_xbar_config(struct tegra_pcie *pcie, u32 lanes,
  1084. u32 *xbar)
  1085. {
  1086. struct device_node *np = pcie->dev->of_node;
  1087. if (of_device_is_compatible(np, "nvidia,tegra30-pcie")) {
  1088. switch (lanes) {
  1089. case 0x00000204:
  1090. dev_info(pcie->dev, "4x1, 2x1 configuration\n");
  1091. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_420;
  1092. return 0;
  1093. case 0x00020202:
  1094. dev_info(pcie->dev, "2x3 configuration\n");
  1095. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_222;
  1096. return 0;
  1097. case 0x00010104:
  1098. dev_info(pcie->dev, "4x1, 1x2 configuration\n");
  1099. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_411;
  1100. return 0;
  1101. }
  1102. } else if (of_device_is_compatible(np, "nvidia,tegra20-pcie")) {
  1103. switch (lanes) {
  1104. case 0x00000004:
  1105. dev_info(pcie->dev, "single-mode configuration\n");
  1106. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_SINGLE;
  1107. return 0;
  1108. case 0x00000202:
  1109. dev_info(pcie->dev, "dual-mode configuration\n");
  1110. *xbar = AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_DUAL;
  1111. return 0;
  1112. }
  1113. }
  1114. return -EINVAL;
  1115. }
  1116. static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
  1117. {
  1118. const struct tegra_pcie_soc_data *soc = pcie->soc_data;
  1119. struct device_node *np = pcie->dev->of_node, *port;
  1120. struct of_pci_range_parser parser;
  1121. struct of_pci_range range;
  1122. struct resource res;
  1123. u32 lanes = 0;
  1124. int err;
  1125. if (of_pci_range_parser_init(&parser, np)) {
  1126. dev_err(pcie->dev, "missing \"ranges\" property\n");
  1127. return -EINVAL;
  1128. }
  1129. pcie->vdd_supply = devm_regulator_get(pcie->dev, "vdd");
  1130. if (IS_ERR(pcie->vdd_supply))
  1131. return PTR_ERR(pcie->vdd_supply);
  1132. pcie->pex_clk_supply = devm_regulator_get(pcie->dev, "pex-clk");
  1133. if (IS_ERR(pcie->pex_clk_supply))
  1134. return PTR_ERR(pcie->pex_clk_supply);
  1135. if (soc->has_avdd_supply) {
  1136. pcie->avdd_supply = devm_regulator_get(pcie->dev, "avdd");
  1137. if (IS_ERR(pcie->avdd_supply))
  1138. return PTR_ERR(pcie->avdd_supply);
  1139. }
  1140. for_each_of_pci_range(&parser, &range) {
  1141. of_pci_range_to_resource(&range, np, &res);
  1142. switch (res.flags & IORESOURCE_TYPE_BITS) {
  1143. case IORESOURCE_IO:
  1144. memcpy(&pcie->io, &res, sizeof(res));
  1145. pcie->io.name = "I/O";
  1146. break;
  1147. case IORESOURCE_MEM:
  1148. if (res.flags & IORESOURCE_PREFETCH) {
  1149. memcpy(&pcie->prefetch, &res, sizeof(res));
  1150. pcie->prefetch.name = "PREFETCH";
  1151. } else {
  1152. memcpy(&pcie->mem, &res, sizeof(res));
  1153. pcie->mem.name = "MEM";
  1154. }
  1155. break;
  1156. }
  1157. }
  1158. err = of_pci_parse_bus_range(np, &pcie->busn);
  1159. if (err < 0) {
  1160. dev_err(pcie->dev, "failed to parse ranges property: %d\n",
  1161. err);
  1162. pcie->busn.name = np->name;
  1163. pcie->busn.start = 0;
  1164. pcie->busn.end = 0xff;
  1165. pcie->busn.flags = IORESOURCE_BUS;
  1166. }
  1167. /* parse root ports */
  1168. for_each_child_of_node(np, port) {
  1169. struct tegra_pcie_port *rp;
  1170. unsigned int index;
  1171. u32 value;
  1172. err = of_pci_get_devfn(port);
  1173. if (err < 0) {
  1174. dev_err(pcie->dev, "failed to parse address: %d\n",
  1175. err);
  1176. return err;
  1177. }
  1178. index = PCI_SLOT(err);
  1179. if (index < 1 || index > soc->num_ports) {
  1180. dev_err(pcie->dev, "invalid port number: %d\n", index);
  1181. return -EINVAL;
  1182. }
  1183. index--;
  1184. err = of_property_read_u32(port, "nvidia,num-lanes", &value);
  1185. if (err < 0) {
  1186. dev_err(pcie->dev, "failed to parse # of lanes: %d\n",
  1187. err);
  1188. return err;
  1189. }
  1190. if (value > 16) {
  1191. dev_err(pcie->dev, "invalid # of lanes: %u\n", value);
  1192. return -EINVAL;
  1193. }
  1194. lanes |= value << (index << 3);
  1195. if (!of_device_is_available(port))
  1196. continue;
  1197. rp = devm_kzalloc(pcie->dev, sizeof(*rp), GFP_KERNEL);
  1198. if (!rp)
  1199. return -ENOMEM;
  1200. err = of_address_to_resource(port, 0, &rp->regs);
  1201. if (err < 0) {
  1202. dev_err(pcie->dev, "failed to parse address: %d\n",
  1203. err);
  1204. return err;
  1205. }
  1206. INIT_LIST_HEAD(&rp->list);
  1207. rp->index = index;
  1208. rp->lanes = value;
  1209. rp->pcie = pcie;
  1210. rp->base = devm_ioremap_resource(pcie->dev, &rp->regs);
  1211. if (IS_ERR(rp->base))
  1212. return PTR_ERR(rp->base);
  1213. list_add_tail(&rp->list, &pcie->ports);
  1214. }
  1215. err = tegra_pcie_get_xbar_config(pcie, lanes, &pcie->xbar_config);
  1216. if (err < 0) {
  1217. dev_err(pcie->dev, "invalid lane configuration\n");
  1218. return err;
  1219. }
  1220. return 0;
  1221. }
  1222. /*
  1223. * FIXME: If there are no PCIe cards attached, then calling this function
  1224. * can result in the increase of the bootup time as there are big timeout
  1225. * loops.
  1226. */
  1227. #define TEGRA_PCIE_LINKUP_TIMEOUT 200 /* up to 1.2 seconds */
  1228. static bool tegra_pcie_port_check_link(struct tegra_pcie_port *port)
  1229. {
  1230. unsigned int retries = 3;
  1231. unsigned long value;
  1232. do {
  1233. unsigned int timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
  1234. do {
  1235. value = readl(port->base + RP_VEND_XP);
  1236. if (value & RP_VEND_XP_DL_UP)
  1237. break;
  1238. usleep_range(1000, 2000);
  1239. } while (--timeout);
  1240. if (!timeout) {
  1241. dev_err(port->pcie->dev, "link %u down, retrying\n",
  1242. port->index);
  1243. goto retry;
  1244. }
  1245. timeout = TEGRA_PCIE_LINKUP_TIMEOUT;
  1246. do {
  1247. value = readl(port->base + RP_LINK_CONTROL_STATUS);
  1248. if (value & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
  1249. return true;
  1250. usleep_range(1000, 2000);
  1251. } while (--timeout);
  1252. retry:
  1253. tegra_pcie_port_reset(port);
  1254. } while (--retries);
  1255. return false;
  1256. }
  1257. static int tegra_pcie_enable(struct tegra_pcie *pcie)
  1258. {
  1259. struct tegra_pcie_port *port, *tmp;
  1260. struct hw_pci hw;
  1261. list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  1262. dev_info(pcie->dev, "probing port %u, using %u lanes\n",
  1263. port->index, port->lanes);
  1264. tegra_pcie_port_enable(port);
  1265. if (tegra_pcie_port_check_link(port))
  1266. continue;
  1267. dev_info(pcie->dev, "link %u down, ignoring\n", port->index);
  1268. tegra_pcie_port_disable(port);
  1269. tegra_pcie_port_free(port);
  1270. }
  1271. memset(&hw, 0, sizeof(hw));
  1272. hw.nr_controllers = 1;
  1273. hw.private_data = (void **)&pcie;
  1274. hw.setup = tegra_pcie_setup;
  1275. hw.map_irq = tegra_pcie_map_irq;
  1276. hw.add_bus = tegra_pcie_add_bus;
  1277. hw.scan = tegra_pcie_scan_bus;
  1278. hw.ops = &tegra_pcie_ops;
  1279. pci_common_init_dev(pcie->dev, &hw);
  1280. return 0;
  1281. }
  1282. static const struct tegra_pcie_soc_data tegra20_pcie_data = {
  1283. .num_ports = 2,
  1284. .msi_base_shift = 0,
  1285. .pads_pll_ctl = PADS_PLL_CTL_TEGRA20,
  1286. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_DIV10,
  1287. .has_pex_clkreq_en = false,
  1288. .has_pex_bias_ctrl = false,
  1289. .has_intr_prsnt_sense = false,
  1290. .has_avdd_supply = false,
  1291. .has_cml_clk = false,
  1292. };
  1293. static const struct tegra_pcie_soc_data tegra30_pcie_data = {
  1294. .num_ports = 3,
  1295. .msi_base_shift = 8,
  1296. .pads_pll_ctl = PADS_PLL_CTL_TEGRA30,
  1297. .tx_ref_sel = PADS_PLL_CTL_TXCLKREF_BUF_EN,
  1298. .has_pex_clkreq_en = true,
  1299. .has_pex_bias_ctrl = true,
  1300. .has_intr_prsnt_sense = true,
  1301. .has_avdd_supply = true,
  1302. .has_cml_clk = true,
  1303. };
  1304. static const struct of_device_id tegra_pcie_of_match[] = {
  1305. { .compatible = "nvidia,tegra30-pcie", .data = &tegra30_pcie_data },
  1306. { .compatible = "nvidia,tegra20-pcie", .data = &tegra20_pcie_data },
  1307. { },
  1308. };
  1309. MODULE_DEVICE_TABLE(of, tegra_pcie_of_match);
  1310. static int tegra_pcie_probe(struct platform_device *pdev)
  1311. {
  1312. const struct of_device_id *match;
  1313. struct tegra_pcie *pcie;
  1314. int err;
  1315. match = of_match_device(tegra_pcie_of_match, &pdev->dev);
  1316. if (!match)
  1317. return -ENODEV;
  1318. pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  1319. if (!pcie)
  1320. return -ENOMEM;
  1321. INIT_LIST_HEAD(&pcie->busses);
  1322. INIT_LIST_HEAD(&pcie->ports);
  1323. pcie->soc_data = match->data;
  1324. pcie->dev = &pdev->dev;
  1325. err = tegra_pcie_parse_dt(pcie);
  1326. if (err < 0)
  1327. return err;
  1328. pcibios_min_mem = 0;
  1329. err = tegra_pcie_get_resources(pcie);
  1330. if (err < 0) {
  1331. dev_err(&pdev->dev, "failed to request resources: %d\n", err);
  1332. return err;
  1333. }
  1334. err = tegra_pcie_enable_controller(pcie);
  1335. if (err)
  1336. goto put_resources;
  1337. /* setup the AFI address translations */
  1338. tegra_pcie_setup_translations(pcie);
  1339. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  1340. err = tegra_pcie_enable_msi(pcie);
  1341. if (err < 0) {
  1342. dev_err(&pdev->dev,
  1343. "failed to enable MSI support: %d\n",
  1344. err);
  1345. goto put_resources;
  1346. }
  1347. }
  1348. err = tegra_pcie_enable(pcie);
  1349. if (err < 0) {
  1350. dev_err(&pdev->dev, "failed to enable PCIe ports: %d\n", err);
  1351. goto disable_msi;
  1352. }
  1353. platform_set_drvdata(pdev, pcie);
  1354. return 0;
  1355. disable_msi:
  1356. if (IS_ENABLED(CONFIG_PCI_MSI))
  1357. tegra_pcie_disable_msi(pcie);
  1358. put_resources:
  1359. tegra_pcie_put_resources(pcie);
  1360. return err;
  1361. }
  1362. static struct platform_driver tegra_pcie_driver = {
  1363. .driver = {
  1364. .name = "tegra-pcie",
  1365. .owner = THIS_MODULE,
  1366. .of_match_table = tegra_pcie_of_match,
  1367. .suppress_bind_attrs = true,
  1368. },
  1369. .probe = tegra_pcie_probe,
  1370. };
  1371. module_platform_driver(tegra_pcie_driver);
  1372. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  1373. MODULE_DESCRIPTION("NVIDIA Tegra PCIe driver");
  1374. MODULE_LICENSE("GPLv2");