2 * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Nvidia Integrated PCI/PCI-Express controller driver.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
44 #include <sys/endian.h>
45 #include <sys/devmap.h>
47 #include <machine/intr.h>
52 #include <dev/extres/clk/clk.h>
53 #include <dev/extres/hwreset/hwreset.h>
54 #include <dev/extres/phy/phy.h>
55 #include <dev/extres/regulator/regulator.h>
56 #include <dev/fdt/fdt_common.h>
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_bus_subr.h>
59 #include <dev/ofw/ofw_pci.h>
60 #include <dev/ofw/ofwpci.h>
61 #include <dev/pci/pcivar.h>
62 #include <dev/pci/pcireg.h>
63 #include <dev/pci/pcib_private.h>
65 #include <machine/resource.h>
66 #include <machine/bus.h>
68 #include <arm/nvidia/tegra_pmc.h>
70 #include "ofw_bus_if.h"
75 #define AFI_AXI_BAR0_SZ 0x000
76 #define AFI_AXI_BAR1_SZ 0x004
77 #define AFI_AXI_BAR2_SZ 0x008
78 #define AFI_AXI_BAR3_SZ 0x00c
79 #define AFI_AXI_BAR4_SZ 0x010
80 #define AFI_AXI_BAR5_SZ 0x014
81 #define AFI_AXI_BAR0_START 0x018
82 #define AFI_AXI_BAR1_START 0x01c
83 #define AFI_AXI_BAR2_START 0x020
84 #define AFI_AXI_BAR3_START 0x024
85 #define AFI_AXI_BAR4_START 0x028
86 #define AFI_AXI_BAR5_START 0x02c
87 #define AFI_FPCI_BAR0 0x030
88 #define AFI_FPCI_BAR1 0x034
89 #define AFI_FPCI_BAR2 0x038
90 #define AFI_FPCI_BAR3 0x03c
91 #define AFI_FPCI_BAR4 0x040
92 #define AFI_FPCI_BAR5 0x044
93 #define AFI_MSI_BAR_SZ 0x060
94 #define AFI_MSI_FPCI_BAR_ST 0x064
95 #define AFI_MSI_AXI_BAR_ST 0x068
98 #define AFI_AXI_BAR6_SZ 0x134
99 #define AFI_AXI_BAR7_SZ 0x138
100 #define AFI_AXI_BAR8_SZ 0x13c
101 #define AFI_AXI_BAR6_START 0x140
102 #define AFI_AXI_BAR7_START 0x144
103 #define AFI_AXI_BAR8_START 0x148
104 #define AFI_FPCI_BAR6 0x14c
105 #define AFI_FPCI_BAR7 0x150
106 #define AFI_FPCI_BAR8 0x154
108 #define AFI_CONFIGURATION 0x0ac
109 #define AFI_CONFIGURATION_EN_FPCI (1 << 0)
111 #define AFI_FPCI_ERROR_MASKS 0x0b0
112 #define AFI_INTR_MASK 0x0b4
113 #define AFI_INTR_MASK_MSI_MASK (1 << 8)
114 #define AFI_INTR_MASK_INT_MASK (1 << 0)
116 #define AFI_INTR_CODE 0x0b8
117 #define AFI_INTR_CODE_MASK 0xf
118 #define AFI_INTR_CODE_INT_CODE_INI_SLVERR 1
119 #define AFI_INTR_CODE_INT_CODE_INI_DECERR 2
120 #define AFI_INTR_CODE_INT_CODE_TGT_SLVERR 3
121 #define AFI_INTR_CODE_INT_CODE_TGT_DECERR 4
122 #define AFI_INTR_CODE_INT_CODE_TGT_WRERR 5
123 #define AFI_INTR_CODE_INT_CODE_SM_MSG 6
124 #define AFI_INTR_CODE_INT_CODE_DFPCI_DECERR 7
125 #define AFI_INTR_CODE_INT_CODE_AXI_DECERR 8
126 #define AFI_INTR_CODE_INT_CODE_FPCI_TIMEOUT 9
127 #define AFI_INTR_CODE_INT_CODE_PE_PRSNT_SENSE 10
128 #define AFI_INTR_CODE_INT_CODE_PE_CLKREQ_SENSE 11
129 #define AFI_INTR_CODE_INT_CODE_CLKCLAMP_SENSE 12
130 #define AFI_INTR_CODE_INT_CODE_RDY4PD_SENSE 13
131 #define AFI_INTR_CODE_INT_CODE_P2P_ERROR 14
134 #define AFI_INTR_SIGNATURE 0x0bc
135 #define AFI_UPPER_FPCI_ADDRESS 0x0c0
136 #define AFI_SM_INTR_ENABLE 0x0c4
137 #define AFI_SM_INTR_RP_DEASSERT (1 << 14)
138 #define AFI_SM_INTR_RP_ASSERT (1 << 13)
139 #define AFI_SM_INTR_HOTPLUG (1 << 12)
140 #define AFI_SM_INTR_PME (1 << 11)
141 #define AFI_SM_INTR_FATAL_ERROR (1 << 10)
142 #define AFI_SM_INTR_UNCORR_ERROR (1 << 9)
143 #define AFI_SM_INTR_CORR_ERROR (1 << 8)
144 #define AFI_SM_INTR_INTD_DEASSERT (1 << 7)
145 #define AFI_SM_INTR_INTC_DEASSERT (1 << 6)
146 #define AFI_SM_INTR_INTB_DEASSERT (1 << 5)
147 #define AFI_SM_INTR_INTA_DEASSERT (1 << 4)
148 #define AFI_SM_INTR_INTD_ASSERT (1 << 3)
149 #define AFI_SM_INTR_INTC_ASSERT (1 << 2)
150 #define AFI_SM_INTR_INTB_ASSERT (1 << 1)
151 #define AFI_SM_INTR_INTA_ASSERT (1 << 0)
153 #define AFI_AFI_INTR_ENABLE 0x0c8
154 #define AFI_AFI_INTR_ENABLE_CODE(code) (1 << (code))
156 #define AFI_PCIE_CONFIG 0x0f8
157 #define AFI_PCIE_CONFIG_PCIE_DISABLE(x) (1 << ((x) + 1))
158 #define AFI_PCIE_CONFIG_PCIE_DISABLE_ALL 0x6
159 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK (0xf << 20)
160 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_XBAR2_1 (0x0 << 20)
161 #define AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_XBAR4_1 (0x1 << 20)
163 #define AFI_FUSE 0x104
164 #define AFI_FUSE_PCIE_T0_GEN2_DIS (1 << 2)
166 #define AFI_PEX0_CTRL 0x110
167 #define AFI_PEX1_CTRL 0x118
168 #define AFI_PEX2_CTRL 0x128
169 #define AFI_PEX_CTRL_OVERRIDE_EN (1 << 4)
170 #define AFI_PEX_CTRL_REFCLK_EN (1 << 3)
171 #define AFI_PEX_CTRL_CLKREQ_EN (1 << 1)
172 #define AFI_PEX_CTRL_RST_L (1 << 0)
174 #define AFI_AXI_BAR6_SZ 0x134
175 #define AFI_AXI_BAR7_SZ 0x138
176 #define AFI_AXI_BAR8_SZ 0x13c
177 #define AFI_AXI_BAR6_START 0x140
178 #define AFI_AXI_BAR7_START 0x144
179 #define AFI_AXI_BAR8_START 0x148
180 #define AFI_FPCI_BAR6 0x14c
181 #define AFI_FPCI_BAR7 0x150
182 #define AFI_FPCI_BAR8 0x154
183 #define AFI_PLLE_CONTROL 0x160
184 #define AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL (1 << 9)
185 #define AFI_PLLE_CONTROL_BYPASS_PCIE2PLLE_CONTROL (1 << 8)
186 #define AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN (1 << 1)
187 #define AFI_PLLE_CONTROL_PCIE2PLLE_CONTROL_EN (1 << 0)
189 #define AFI_PEXBIAS_CTRL 0x168
191 /* FPCI Address space */
192 #define FPCI_MAP_IO 0xfdfc000000ULL
193 #define FPCI_MAP_TYPE0_CONFIG 0xfdfc000000ULL
194 #define FPCI_MAP_TYPE1_CONFIG 0xfdff000000ULL
195 #define FPCI_MAP_EXT_TYPE0_CONFIG 0xfe00000000ULL
196 #define FPCI_MAP_EXT_TYPE1_CONFIG 0xfe10000000ULL
198 /* Configuration space */
199 #define RP_VEND_XP 0x00000F00
200 #define RP_VEND_XP_DL_UP (1 << 30)
202 #define RP_PRIV_MISC 0x00000FE0
203 #define RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT (0xE << 0)
204 #define RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT (0xF << 0)
206 #define RP_LINK_CONTROL_STATUS 0x00000090
207 #define RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE 0x20000000
208 #define RP_LINK_CONTROL_STATUS_LINKSTAT_MASK 0x3fff0000
210 #define TEGRA_PCIE_LINKUP_TIMEOUT 200
214 #define debugf(fmt, args...) do { printf(fmt,##args); } while (0)
216 #define debugf(fmt, args...)
220 * Configuration space format:
221 * [27:24] extended register
223 * [15:11] slot (device)
227 #define PCI_CFG_EXT_REG(reg) ((((reg) >> 8) & 0x0f) << 24)
228 #define PCI_CFG_BUS(bus) (((bus) & 0xff) << 16)
229 #define PCI_CFG_DEV(dev) (((dev) & 0x1f) << 11)
230 #define PCI_CFG_FUN(fun) (((fun) & 0x07) << 8)
231 #define PCI_CFG_BASE_REG(reg) ((reg) & 0xff)
233 #define PADS_WR4(_sc, _r, _v) bus_write_4((_sc)-pads_mem_res, (_r), (_v))
234 #define PADS_RD4(_sc, _r) bus_read_4((_sc)->pads_mem_res, (_r))
235 #define AFI_WR4(_sc, _r, _v) bus_write_4((_sc)->afi_mem_res, (_r), (_v))
236 #define AFI_RD4(_sc, _r) bus_read_4((_sc)->afi_mem_res, (_r))
239 bus_size_t axi_start;
240 bus_size_t fpci_start;
243 {AFI_AXI_BAR0_START, AFI_FPCI_BAR0, AFI_AXI_BAR0_SZ}, /* BAR 0 */
244 {AFI_AXI_BAR1_START, AFI_FPCI_BAR1, AFI_AXI_BAR1_SZ}, /* BAR 1 */
245 {AFI_AXI_BAR2_START, AFI_FPCI_BAR2, AFI_AXI_BAR2_SZ}, /* BAR 2 */
246 {AFI_AXI_BAR3_START, AFI_FPCI_BAR3, AFI_AXI_BAR3_SZ}, /* BAR 3 */
247 {AFI_AXI_BAR4_START, AFI_FPCI_BAR4, AFI_AXI_BAR4_SZ}, /* BAR 4 */
248 {AFI_AXI_BAR5_START, AFI_FPCI_BAR5, AFI_AXI_BAR5_SZ}, /* BAR 5 */
249 {AFI_AXI_BAR6_START, AFI_FPCI_BAR6, AFI_AXI_BAR6_SZ}, /* BAR 6 */
250 {AFI_AXI_BAR7_START, AFI_FPCI_BAR7, AFI_AXI_BAR7_SZ}, /* BAR 7 */
251 {AFI_AXI_BAR8_START, AFI_FPCI_BAR8, AFI_AXI_BAR8_SZ}, /* BAR 8 */
252 {AFI_MSI_AXI_BAR_ST, AFI_MSI_FPCI_BAR_ST, AFI_MSI_BAR_SZ}, /* MSI 9 */
255 /* Compatible devices. */
256 static struct ofw_compat_data compat_data[] = {
257 {"nvidia,tegra124-pcie", 1},
261 struct tegra_pcib_port {
263 int port_idx; /* chip port index */
264 int num_lanes; /* number of lanes */
265 bus_size_t afi_pex_ctrl; /* offset of afi_pex_ctrl */
267 /* Config space properties. */
268 bus_addr_t rp_base_addr; /* PA of config window */
269 bus_size_t rp_size; /* size of config window */
270 bus_space_handle_t cfg_handle; /* handle of config window */
273 #define TEGRA_PCIB_MAX_PORTS 3
274 struct tegra_pcib_softc {
275 struct ofw_pci_softc ofw_pci;
278 struct resource *pads_mem_res;
279 struct resource *afi_mem_res;
280 struct resource *cfg_mem_res;
281 struct resource *irq_res;
282 struct resource *msi_irq_res;
284 void *msi_intr_cookie;
286 struct ofw_pci_range mem_range;
287 struct ofw_pci_range pref_mem_range;
288 struct ofw_pci_range io_range;
295 hwreset_t hwreset_pex;
296 hwreset_t hwreset_afi;
297 hwreset_t hwreset_pcie_x;
298 regulator_t supply_avddio_pex;
299 regulator_t supply_dvddio_pex;
300 regulator_t supply_avdd_pex_pll;
301 regulator_t supply_hvdd_pex;
302 regulator_t supply_hvdd_pex_pll_e;
303 regulator_t supply_vddio_pex_ctl;
304 regulator_t supply_avdd_pll_erefe;
307 bus_addr_t cfg_base_addr; /* base address of config */
308 bus_size_t cfg_cur_offs; /* currently mapped window */
309 bus_space_handle_t cfg_handle; /* handle of config window */
310 bus_space_tag_t bus_tag; /* tag of config window */
313 struct tegra_pcib_port *ports[TEGRA_PCIB_MAX_PORTS];
318 tegra_pcib_maxslots(device_t dev)
324 tegra_pcib_route_interrupt(device_t bus, device_t dev, int pin)
326 struct tegra_pcib_softc *sc;
328 sc = device_get_softc(bus);
329 device_printf(bus, "route pin %d for device %d.%d to %ju\n",
330 pin, pci_get_slot(dev), pci_get_function(dev),
331 rman_get_start(sc->irq_res));
333 return (rman_get_start(sc->irq_res));
337 tegra_pcbib_map_cfg(struct tegra_pcib_softc *sc, u_int bus, u_int slot,
338 u_int func, u_int reg)
343 offs = sc->cfg_base_addr;
344 offs |= PCI_CFG_BUS(bus) | PCI_CFG_DEV(slot) | PCI_CFG_FUN(func) |
345 PCI_CFG_EXT_REG(reg);
346 if ((sc->cfg_handle != 0) && (sc->cfg_cur_offs == offs))
348 if (sc->cfg_handle != 0)
349 bus_space_unmap(sc->bus_tag, sc->cfg_handle, 0x800);
351 rv = bus_space_map(sc->bus_tag, offs, 0x800, 0, &sc->cfg_handle);
353 device_printf(sc->dev, "Cannot map config space\n");
355 sc->cfg_cur_offs = offs;
360 tegra_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
361 u_int reg, int bytes)
363 struct tegra_pcib_softc *sc;
364 bus_space_handle_t hndl;
369 sc = device_get_softc(dev);
373 for (i = 0; i < TEGRA_PCIB_MAX_PORTS; i++) {
374 if ((sc->ports[i] != NULL) &&
375 (sc->ports[i]->port_idx == slot)) {
376 hndl = sc->ports[i]->cfg_handle;
381 if (i >= TEGRA_PCIB_MAX_PORTS)
384 rv = tegra_pcbib_map_cfg(sc, bus, slot, func, reg);
387 hndl = sc->cfg_handle;
388 off = PCI_CFG_BASE_REG(reg);
391 val = bus_space_read_4(sc->bus_tag, hndl, off & ~3);
401 val >>= ((off & 3) << 3);
409 tegra_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
410 u_int reg, uint32_t val, int bytes)
412 struct tegra_pcib_softc *sc;
413 bus_space_handle_t hndl;
418 sc = device_get_softc(dev);
422 for (i = 0; i < TEGRA_PCIB_MAX_PORTS; i++) {
423 if ((sc->ports[i] != NULL) &&
424 (sc->ports[i]->port_idx == slot)) {
425 hndl = sc->ports[i]->cfg_handle;
430 if (i >= TEGRA_PCIB_MAX_PORTS)
433 rv = tegra_pcbib_map_cfg(sc, bus, slot, func, reg);
436 hndl = sc->cfg_handle;
437 off = PCI_CFG_BASE_REG(reg);
442 bus_space_write_4(sc->bus_tag, hndl, off, val);
445 val2 = bus_space_read_4(sc->bus_tag, hndl, off & ~3);
446 val2 &= ~(0xffff << ((off & 3) << 3));
447 val2 |= ((val & 0xffff) << ((off & 3) << 3));
448 bus_space_write_4(sc->bus_tag, hndl, off & ~3, val2);
451 val2 = bus_space_read_4(sc->bus_tag, hndl, off & ~3);
452 val2 &= ~(0xff << ((off & 3) << 3));
453 val2 |= ((val & 0xff) << ((off & 3) << 3));
454 bus_space_write_4(sc->bus_tag, hndl, off & ~3, val2);
459 static int tegra_pci_intr(void *arg)
461 struct tegra_pcib_softc *sc = arg;
462 uint32_t code, signature;
464 code = bus_read_4(sc->afi_mem_res, AFI_INTR_CODE) & AFI_INTR_CODE_MASK;
465 signature = bus_read_4(sc->afi_mem_res, AFI_INTR_SIGNATURE);
466 bus_write_4(sc->afi_mem_res, AFI_INTR_CODE, 0);
467 if (code == AFI_INTR_CODE_INT_CODE_SM_MSG)
468 return(FILTER_STRAY);
470 printf("tegra_pci_intr: code %x sig %x\n", code, signature);
471 return (FILTER_HANDLED);
474 #if defined(TEGRA_PCI_MSI)
476 tegra_pcib_map_msi(device_t dev, device_t child, int irq, uint64_t *addr,
479 struct tegra_pcib_softc *sc;
481 sc = device_get_softc(dev);
484 /* validate parameters */
485 if (isclr(&sc->msi_bitmap, irq)) {
486 device_printf(dev, "invalid MSI 0x%x\n", irq);
490 tegra_msi_data(irq, addr, data);
492 debugf("%s: irq: %d addr: %jx data: %x\n",
493 __func__, irq, *addr, *data);
499 tegra_pcib_alloc_msi(device_t dev, device_t child, int count,
500 int maxcount __unused, int *irqs)
502 struct tegra_pcib_softc *sc;
505 if (powerof2(count) == 0 || count > MSI_IRQ_NUM)
508 sc = device_get_softc(dev);
511 for (start = 0; (start + count) < MSI_IRQ_NUM; start++) {
512 for (i = start; i < start + count; i++) {
513 if (isset(&sc->msi_bitmap, i))
516 if (i == start + count)
520 if ((start + count) == MSI_IRQ_NUM) {
521 mtx_unlock(&sc->mtx);
525 for (i = start; i < start + count; i++) {
526 setbit(&sc->msi_bitmap, i);
527 irqs[i] = MSI_IRQ + i;
529 debugf("%s: start: %x count: %x\n", __func__, start, count);
531 mtx_unlock(&sc->mtx);
536 tegra_pcib_release_msi(device_t dev, device_t child, int count, int *irqs)
538 struct tegra_pcib_softc *sc;
541 sc = device_get_softc(dev);
544 for (i = 0; i < count; i++)
545 clrbit(&sc->msi_bitmap, irqs[i] - MSI_IRQ);
547 mtx_unlock(&sc->mtx);
553 tegra_pcib_pex_ctrl(struct tegra_pcib_softc *sc, int port)
555 if (port >= TEGRA_PCIB_MAX_PORTS)
556 panic("invalid port number: %d\n", port);
559 return (AFI_PEX0_CTRL);
561 return (AFI_PEX1_CTRL);
563 return (AFI_PEX2_CTRL);
565 panic("invalid port number: %d\n", port);
569 tegra_pcib_enable_fdt_resources(struct tegra_pcib_softc *sc)
573 rv = hwreset_assert(sc->hwreset_pcie_x);
575 device_printf(sc->dev, "Cannot assert 'pcie_x' reset\n");
578 rv = hwreset_assert(sc->hwreset_afi);
580 device_printf(sc->dev, "Cannot assert 'afi' reset\n");
583 rv = hwreset_assert(sc->hwreset_pex);
585 device_printf(sc->dev, "Cannot assert 'pex' reset\n");
589 tegra_powergate_power_off(TEGRA_POWERGATE_PCX);
591 /* Power supplies. */
592 rv = regulator_enable(sc->supply_avddio_pex);
594 device_printf(sc->dev,
595 "Cannot enable 'avddio_pex' regulator\n");
598 rv = regulator_enable(sc->supply_dvddio_pex);
600 device_printf(sc->dev,
601 "Cannot enable 'dvddio_pex' regulator\n");
604 rv = regulator_enable(sc->supply_avdd_pex_pll);
606 device_printf(sc->dev,
607 "Cannot enable 'avdd-pex-pll' regulator\n");
610 rv = regulator_enable(sc->supply_hvdd_pex);
612 device_printf(sc->dev,
613 "Cannot enable 'hvdd-pex-supply' regulator\n");
616 rv = regulator_enable(sc->supply_hvdd_pex_pll_e);
618 device_printf(sc->dev,
619 "Cannot enable 'hvdd-pex-pll-e-supply' regulator\n");
622 rv = regulator_enable(sc->supply_vddio_pex_ctl);
624 device_printf(sc->dev,
625 "Cannot enable 'vddio-pex-ctl' regulator\n");
628 rv = regulator_enable(sc->supply_avdd_pll_erefe);
630 device_printf(sc->dev,
631 "Cannot enable 'avdd-pll-erefe-supply' regulator\n");
635 rv = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_PCX,
636 sc->clk_pex, sc->hwreset_pex);
638 device_printf(sc->dev, "Cannot enable 'PCX' powergate\n");
642 rv = hwreset_deassert(sc->hwreset_afi);
644 device_printf(sc->dev, "Cannot unreset 'afi' reset\n");
648 rv = clk_enable(sc->clk_afi);
650 device_printf(sc->dev, "Cannot enable 'afi' clock\n");
653 rv = clk_enable(sc->clk_cml);
655 device_printf(sc->dev, "Cannot enable 'cml' clock\n");
658 rv = clk_enable(sc->clk_pll_e);
660 device_printf(sc->dev, "Cannot enable 'pll_e' clock\n");
666 static struct tegra_pcib_port *
667 tegra_pcib_parse_port(struct tegra_pcib_softc *sc, phandle_t node)
669 struct tegra_pcib_port *port;
674 port = malloc(sizeof(struct tegra_pcib_port), M_DEVBUF, M_WAITOK);
676 rv = OF_getprop(node, "status", tmpstr, sizeof(tmpstr));
677 if (rv <= 0 || strcmp(tmpstr, "okay") == 0 ||
678 strcmp(tmpstr, "ok") == 0)
683 rv = OF_getencprop(node, "assigned-addresses", tmp, sizeof(tmp));
684 if (rv != sizeof(tmp)) {
685 device_printf(sc->dev, "Cannot parse assigned-address: %d\n",
689 port->rp_base_addr = tmp[2];
690 port->rp_size = tmp[4];
691 port->port_idx = OFW_PCI_PHYS_HI_DEVICE(tmp[0]) - 1;
692 if (port->port_idx >= TEGRA_PCIB_MAX_PORTS) {
693 device_printf(sc->dev, "Invalid port index: %d\n",
698 * Implement proper function for parsing pci "reg" property:
699 * - it have PCI bus format
700 * - its relative to matching "assigned-addresses"
702 rv = OF_getencprop(node, "reg", tmp, sizeof(tmp));
703 if (rv != sizeof(tmp)) {
704 device_printf(sc->dev, "Cannot parse reg: %d\n", rv);
707 port->rp_base_addr += tmp[2];
709 rv = OF_getencprop(node, "nvidia,num-lanes", &port->num_lanes,
710 sizeof(port->num_lanes));
711 if (rv != sizeof(port->num_lanes)) {
712 device_printf(sc->dev, "Cannot parse nvidia,num-lanes: %d\n",
716 if (port->num_lanes > 4) {
717 device_printf(sc->dev, "Invalid nvidia,num-lanes: %d\n",
722 port->afi_pex_ctrl = tegra_pcib_pex_ctrl(sc, port->port_idx);
723 sc->lanes_cfg |= port->num_lanes << (4 * port->port_idx);
727 free(port, M_DEVBUF);
733 tegra_pcib_parse_fdt_resources(struct tegra_pcib_softc *sc, phandle_t node)
736 struct tegra_pcib_port *port;
739 /* Power supplies. */
740 rv = regulator_get_by_ofw_property(sc->dev, 0, "avddio-pex-supply",
741 &sc->supply_avddio_pex);
743 device_printf(sc->dev,
744 "Cannot get 'avddio-pex' regulator\n");
747 rv = regulator_get_by_ofw_property(sc->dev, 0, "dvddio-pex-supply",
748 &sc->supply_dvddio_pex);
750 device_printf(sc->dev,
751 "Cannot get 'dvddio-pex' regulator\n");
754 rv = regulator_get_by_ofw_property(sc->dev, 0, "avdd-pex-pll-supply",
755 &sc->supply_avdd_pex_pll);
757 device_printf(sc->dev,
758 "Cannot get 'avdd-pex-pll' regulator\n");
761 rv = regulator_get_by_ofw_property(sc->dev, 0, "hvdd-pex-supply",
762 &sc->supply_hvdd_pex);
764 device_printf(sc->dev,
765 "Cannot get 'hvdd-pex' regulator\n");
768 rv = regulator_get_by_ofw_property(sc->dev, 0, "hvdd-pex-pll-e-supply",
769 &sc->supply_hvdd_pex_pll_e);
771 device_printf(sc->dev,
772 "Cannot get 'hvdd-pex-pll-e' regulator\n");
775 rv = regulator_get_by_ofw_property(sc->dev, 0, "vddio-pex-ctl-supply",
776 &sc->supply_vddio_pex_ctl);
778 device_printf(sc->dev,
779 "Cannot get 'vddio-pex-ctl' regulator\n");
782 rv = regulator_get_by_ofw_property(sc->dev, 0, "avdd-pll-erefe-supply",
783 &sc->supply_avdd_pll_erefe);
785 device_printf(sc->dev,
786 "Cannot get 'avdd-pll-erefe' regulator\n");
791 rv = hwreset_get_by_ofw_name(sc->dev, 0, "pex", &sc->hwreset_pex);
793 device_printf(sc->dev, "Cannot get 'pex' reset\n");
796 rv = hwreset_get_by_ofw_name(sc->dev, 0, "afi", &sc->hwreset_afi);
798 device_printf(sc->dev, "Cannot get 'afi' reset\n");
801 rv = hwreset_get_by_ofw_name(sc->dev, 0, "pcie_x", &sc->hwreset_pcie_x);
803 device_printf(sc->dev, "Cannot get 'pcie_x' reset\n");
808 rv = clk_get_by_ofw_name(sc->dev, 0, "pex", &sc->clk_pex);
810 device_printf(sc->dev, "Cannot get 'pex' clock\n");
813 rv = clk_get_by_ofw_name(sc->dev, 0, "afi", &sc->clk_afi);
815 device_printf(sc->dev, "Cannot get 'afi' clock\n");
818 rv = clk_get_by_ofw_name(sc->dev, 0, "pll_e", &sc->clk_pll_e);
820 device_printf(sc->dev, "Cannot get 'pll_e' clock\n");
823 rv = clk_get_by_ofw_name(sc->dev, 0, "cml", &sc->clk_cml);
825 device_printf(sc->dev, "Cannot get 'cml' clock\n");
830 rv = phy_get_by_ofw_name(sc->dev, 0, "pcie", &sc->phy);
832 device_printf(sc->dev, "Cannot get 'pcie' phy\n");
838 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
839 port = tegra_pcib_parse_port(sc, child);
841 device_printf(sc->dev, "Cannot parse PCIe port node\n");
844 sc->ports[sc->num_ports++] = port;
851 tegra_pcib_decode_ranges(struct tegra_pcib_softc *sc,
852 struct ofw_pci_range *ranges, int nranges)
856 for (i = 2; i < nranges; i++) {
857 if ((ranges[i].pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
858 OFW_PCI_PHYS_HI_SPACE_IO) {
859 if (sc->io_range.size != 0) {
860 device_printf(sc->dev,
861 "Duplicated IO range found in DT\n");
864 sc->io_range = ranges[i];
866 if (((ranges[i].pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) ==
867 OFW_PCI_PHYS_HI_SPACE_MEM32)) {
868 if (ranges[i].pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) {
869 if (sc->pref_mem_range.size != 0) {
870 device_printf(sc->dev,
871 "Duplicated memory range found "
875 sc->pref_mem_range = ranges[i];
877 if (sc->mem_range.size != 0) {
878 device_printf(sc->dev,
879 "Duplicated memory range found "
883 sc->mem_range = ranges[i];
887 if ((sc->io_range.size == 0) || (sc->mem_range.size == 0)
888 || (sc->pref_mem_range.size == 0)) {
889 device_printf(sc->dev,
890 " Not all required ranges are found in DT\n");
900 tegra_pcib_wait_for_link(struct tegra_pcib_softc *sc,
901 struct tegra_pcib_port *port)
907 /* Setup link detection. */
908 reg = tegra_pcib_read_config(sc->dev, 0, port->port_idx, 0,
910 reg &= ~RP_PRIV_MISC_PRSNT_MAP_EP_ABSNT;
911 reg |= RP_PRIV_MISC_PRSNT_MAP_EP_PRSNT;
912 tegra_pcib_write_config(sc->dev, 0, port->port_idx, 0,
913 RP_PRIV_MISC, reg, 4);
915 for (i = TEGRA_PCIE_LINKUP_TIMEOUT; i > 0; i--) {
916 reg = tegra_pcib_read_config(sc->dev, 0, port->port_idx, 0,
918 if (reg & RP_VEND_XP_DL_UP)
925 for (i = TEGRA_PCIE_LINKUP_TIMEOUT; i > 0; i--) {
926 reg = tegra_pcib_read_config(sc->dev, 0, port->port_idx, 0,
927 RP_LINK_CONTROL_STATUS, 4);
928 if (reg & RP_LINK_CONTROL_STATUS_DL_LINK_ACTIVE)
938 tegra_pcib_port_enable(struct tegra_pcib_softc *sc, int port_num)
940 struct tegra_pcib_port *port;
944 port = sc->ports[port_num];
946 /* Put port to reset. */
947 reg = AFI_RD4(sc, port->afi_pex_ctrl);
948 reg &= ~AFI_PEX_CTRL_RST_L;
949 AFI_WR4(sc, port->afi_pex_ctrl, reg);
950 AFI_RD4(sc, port->afi_pex_ctrl);
954 reg |= AFI_PEX_CTRL_REFCLK_EN;
955 reg |= AFI_PEX_CTRL_CLKREQ_EN;
956 reg |= AFI_PEX_CTRL_OVERRIDE_EN;
957 AFI_WR4(sc, port->afi_pex_ctrl, reg);
958 AFI_RD4(sc, port->afi_pex_ctrl);
962 reg |= AFI_PEX_CTRL_RST_L;
963 AFI_WR4(sc, port->afi_pex_ctrl, reg);
965 rv = tegra_pcib_wait_for_link(sc, port);
967 device_printf(sc->dev, " port %d (%d lane%s): Link is %s\n",
968 port->port_idx, port->num_lanes,
969 port->num_lanes > 1 ? "s": "",
970 rv == 0 ? "up": "down");
975 tegra_pcib_port_disable(struct tegra_pcib_softc *sc, uint32_t port_num)
977 struct tegra_pcib_port *port;
980 port = sc->ports[port_num];
982 /* Put port to reset. */
983 reg = AFI_RD4(sc, port->afi_pex_ctrl);
984 reg &= ~AFI_PEX_CTRL_RST_L;
985 AFI_WR4(sc, port->afi_pex_ctrl, reg);
986 AFI_RD4(sc, port->afi_pex_ctrl);
989 /* Disable clocks. */
990 reg &= ~AFI_PEX_CTRL_CLKREQ_EN;
991 reg &= ~AFI_PEX_CTRL_REFCLK_EN;
992 AFI_WR4(sc, port->afi_pex_ctrl, reg);
995 device_printf(sc->dev, " port %d (%d lane%s): Disabled\n",
996 port->port_idx, port->num_lanes,
997 port->num_lanes > 1 ? "s": "");
1001 tegra_pcib_set_bar(struct tegra_pcib_softc *sc, int bar, uint32_t axi,
1002 uint64_t fpci, uint32_t size, int is_memory)
1008 axi_reg = axi & ~0xFFF;
1009 size_reg = size >> 12;
1010 fpci_reg = (uint32_t)(fpci >> 8) & ~0xF;
1011 fpci_reg |= is_memory ? 0x1 : 0x0;
1012 AFI_WR4(sc, bars[bar].axi_start, axi_reg);
1013 AFI_WR4(sc, bars[bar].size, size_reg);
1014 AFI_WR4(sc, bars[bar].fpci_start, fpci_reg);
1018 tegra_pcib_enable(struct tegra_pcib_softc *sc)
1024 rv = tegra_pcib_enable_fdt_resources(sc);
1026 device_printf(sc->dev, "Cannot enable FDT resources\n");
1029 /* Enable PLLE control. */
1030 reg = AFI_RD4(sc, AFI_PLLE_CONTROL);
1031 reg &= ~AFI_PLLE_CONTROL_BYPASS_PADS2PLLE_CONTROL;
1032 reg |= AFI_PLLE_CONTROL_PADS2PLLE_CONTROL_EN;
1033 AFI_WR4(sc, AFI_PLLE_CONTROL, reg);
1036 AFI_WR4(sc, AFI_PEXBIAS_CTRL, 0);
1038 /* Configure mode and ports. */
1039 reg = AFI_RD4(sc, AFI_PCIE_CONFIG);
1040 reg &= ~AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_MASK;
1041 if (sc->lanes_cfg == 0x14) {
1043 device_printf(sc->dev,
1044 "Using x1,x4 configuration\n");
1045 reg |= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_XBAR4_1;
1046 } else if (sc->lanes_cfg == 0x12) {
1048 device_printf(sc->dev,
1049 "Using x1,x2 configuration\n");
1050 reg |= AFI_PCIE_CONFIG_SM2TMS0_XBAR_CONFIG_XBAR2_1;
1052 device_printf(sc->dev,
1053 "Unsupported lanes configuration: 0x%X\n", sc->lanes_cfg);
1055 reg |= AFI_PCIE_CONFIG_PCIE_DISABLE_ALL;
1056 for (i = 0; i < TEGRA_PCIB_MAX_PORTS; i++) {
1057 if ((sc->ports[i] != NULL))
1059 ~AFI_PCIE_CONFIG_PCIE_DISABLE(sc->ports[i]->port_idx);
1061 AFI_WR4(sc, AFI_PCIE_CONFIG, reg);
1063 /* Enable Gen2 support. */
1064 reg = AFI_RD4(sc, AFI_FUSE);
1065 reg &= ~AFI_FUSE_PCIE_T0_GEN2_DIS;
1066 AFI_WR4(sc, AFI_FUSE, reg);
1068 /* Enable PCIe phy. */
1069 rv = phy_enable(sc->dev, sc->phy);
1071 device_printf(sc->dev, "Cannot enable phy\n");
1075 rv = hwreset_deassert(sc->hwreset_pcie_x);
1077 device_printf(sc->dev, "Cannot unreset 'pci_x' reset\n");
1081 /* Enable config space. */
1082 reg = AFI_RD4(sc, AFI_CONFIGURATION);
1083 reg |= AFI_CONFIGURATION_EN_FPCI;
1084 AFI_WR4(sc, AFI_CONFIGURATION, reg);
1086 /* Enable AFI errors. */
1088 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_INI_SLVERR);
1089 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_INI_DECERR);
1090 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_TGT_SLVERR);
1091 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_TGT_DECERR);
1092 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_TGT_WRERR);
1093 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_SM_MSG);
1094 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_DFPCI_DECERR);
1095 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_AXI_DECERR);
1096 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_FPCI_TIMEOUT);
1097 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_PE_PRSNT_SENSE);
1098 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_PE_CLKREQ_SENSE);
1099 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_CLKCLAMP_SENSE);
1100 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_RDY4PD_SENSE);
1101 reg |= AFI_AFI_INTR_ENABLE_CODE(AFI_INTR_CODE_INT_CODE_P2P_ERROR);
1102 AFI_WR4(sc, AFI_AFI_INTR_ENABLE, reg);
1103 AFI_WR4(sc, AFI_SM_INTR_ENABLE, 0xffffffff);
1105 /* Enable INT, disable MSI. */
1106 AFI_WR4(sc, AFI_INTR_MASK, AFI_INTR_MASK_INT_MASK);
1108 /* Mask all FPCI errors. */
1109 AFI_WR4(sc, AFI_FPCI_ERROR_MASKS, 0);
1111 /* Setup AFI translation windows. */
1112 /* BAR 0 - type 1 extended configuration. */
1113 tegra_pcib_set_bar(sc, 0, rman_get_start(sc->cfg_mem_res),
1114 FPCI_MAP_EXT_TYPE1_CONFIG, rman_get_size(sc->cfg_mem_res), 0);
1116 /* BAR 1 - downstream I/O. */
1117 tegra_pcib_set_bar(sc, 1, sc->io_range.host, FPCI_MAP_IO,
1118 sc->io_range.size, 0);
1120 /* BAR 2 - downstream prefetchable memory 1:1. */
1121 tegra_pcib_set_bar(sc, 2, sc->pref_mem_range.host,
1122 sc->pref_mem_range.host, sc->pref_mem_range.size, 1);
1124 /* BAR 3 - downstream not prefetchable memory 1:1 .*/
1125 tegra_pcib_set_bar(sc, 3, sc->mem_range.host,
1126 sc->mem_range.host, sc->mem_range.size, 1);
1128 /* BAR 3-8 clear. */
1129 tegra_pcib_set_bar(sc, 4, 0, 0, 0, 0);
1130 tegra_pcib_set_bar(sc, 5, 0, 0, 0, 0);
1131 tegra_pcib_set_bar(sc, 6, 0, 0, 0, 0);
1132 tegra_pcib_set_bar(sc, 7, 0, 0, 0, 0);
1133 tegra_pcib_set_bar(sc, 8, 0, 0, 0, 0);
1135 /* MSI BAR - clear. */
1136 tegra_pcib_set_bar(sc, 9, 0, 0, 0, 0);
1141 tegra_pcib_probe(device_t dev)
1143 if (!ofw_bus_status_okay(dev))
1146 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
1147 device_set_desc(dev, "Nvidia Integrated PCI/PCI-E Controller");
1148 return (BUS_PROBE_DEFAULT);
1154 tegra_pcib_attach(device_t dev)
1156 struct tegra_pcib_softc *sc;
1160 struct tegra_pcib_port *port;
1163 sc = device_get_softc(dev);
1165 mtx_init(&sc->mtx, "msi_mtx", NULL, MTX_DEF);
1167 node = ofw_bus_get_node(dev);
1169 rv = tegra_pcib_parse_fdt_resources(sc, node);
1171 device_printf(dev, "Cannot get FDT resources\n");
1175 /* Allocate bus_space resources. */
1177 sc->pads_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1179 if (sc->pads_mem_res == NULL) {
1180 device_printf(dev, "Cannot allocate PADS register\n");
1186 * tag for config space is not filled when RF_ALLOCATED flag is used.
1188 sc->bus_tag = rman_get_bustag(sc->pads_mem_res);
1191 sc->afi_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1193 if (sc->afi_mem_res == NULL) {
1194 device_printf(dev, "Cannot allocate AFI register\n");
1200 sc->cfg_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1202 if (sc->cfg_mem_res == NULL) {
1203 device_printf(dev, "Cannot allocate config space memory\n");
1207 sc->cfg_base_addr = rman_get_start(sc->cfg_mem_res);
1211 for (i = 0; i < TEGRA_PCIB_MAX_PORTS; i++) {
1212 if (sc->ports[i] == NULL)
1214 port = sc->ports[i];
1215 rv = bus_space_map(sc->bus_tag, port->rp_base_addr,
1216 port->rp_size, 0, &port->cfg_handle);
1218 device_printf(sc->dev, "Cannot allocate memory for "
1229 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1230 RF_ACTIVE | RF_SHAREABLE);
1231 if (sc->irq_res == NULL) {
1232 device_printf(dev, "Cannot allocate IRQ resources\n");
1238 sc->msi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1240 if (sc->irq_res == NULL) {
1241 device_printf(dev, "Cannot allocate MSI IRQ resources\n");
1246 sc->ofw_pci.sc_range_mask = 0x3;
1247 rv = ofw_pci_init(dev);
1251 rv = tegra_pcib_decode_ranges(sc, sc->ofw_pci.sc_range,
1252 sc->ofw_pci.sc_nrange);
1256 if (bus_setup_intr(dev, sc->irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
1257 tegra_pci_intr, NULL, sc, &sc->intr_cookie)) {
1258 device_printf(dev, "cannot setup interrupt handler\n");
1264 * Enable PCIE device.
1266 rv = tegra_pcib_enable(sc);
1269 for (i = 0; i < TEGRA_PCIB_MAX_PORTS; i++) {
1270 if (sc->ports[i] == NULL)
1272 if (sc->ports[i]->enabled)
1273 tegra_pcib_port_enable(sc, i);
1275 tegra_pcib_port_disable(sc, i);
1278 device_add_child(dev, "pci", -1);
1280 return (bus_generic_attach(dev));
1288 static device_method_t tegra_pcib_methods[] = {
1289 /* Device interface */
1290 DEVMETHOD(device_probe, tegra_pcib_probe),
1291 DEVMETHOD(device_attach, tegra_pcib_attach),
1294 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
1295 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
1297 /* pcib interface */
1298 DEVMETHOD(pcib_maxslots, tegra_pcib_maxslots),
1299 DEVMETHOD(pcib_read_config, tegra_pcib_read_config),
1300 DEVMETHOD(pcib_write_config, tegra_pcib_write_config),
1301 DEVMETHOD(pcib_route_interrupt, tegra_pcib_route_interrupt),
1303 #if defined(TEGRA_PCI_MSI)
1304 DEVMETHOD(pcib_alloc_msi, tegra_pcib_alloc_msi),
1305 DEVMETHOD(pcib_release_msi, tegra_pcib_release_msi),
1306 DEVMETHOD(pcib_map_msi, tegra_pcib_map_msi),
1309 /* OFW bus interface */
1310 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
1311 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
1312 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
1313 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
1314 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
1319 DEFINE_CLASS_1(pcib, tegra_pcib_driver, tegra_pcib_methods,
1320 sizeof(struct tegra_pcib_softc), ofw_pci_driver);
1321 devclass_t pcib_devclass;
1322 DRIVER_MODULE(pcib, simplebus, tegra_pcib_driver, pcib_devclass, 0, 0);