pci-tegra.c 41 KB

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