pci-tegra.c 42 KB

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