pci-tegra.c 43 KB

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