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 * XHCI driver for Tegra SoCs.
34 #include "opt_platform.h"
36 #include <sys/param.h>
38 #include <sys/clock.h>
39 #include <sys/condvar.h>
40 #include <sys/firmware.h>
41 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
47 #include <sys/systm.h>
50 #include <vm/vm_extern.h>
51 #include <vm/vm_kern.h>
54 #include <machine/bus.h>
55 #include <machine/resource.h>
57 #include <dev/extres/clk/clk.h>
58 #include <dev/extres/hwreset/hwreset.h>
59 #include <dev/extres/phy/phy.h>
60 #include <dev/extres/regulator/regulator.h>
61 #include <dev/ofw/ofw_bus.h>
62 #include <dev/ofw/ofw_bus_subr.h>
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_process.h>
67 #include <dev/usb/usb_controller.h>
68 #include <dev/usb/usb_bus.h>
69 #include <dev/usb/controller/xhci.h>
70 #include <dev/usb/controller/xhcireg.h>
72 #include <arm/nvidia/tegra_pmc.h>
76 /* FPCI address space */
77 #define T_XUSB_CFG_0 0x000
78 #define T_XUSB_CFG_1 0x004
79 #define CFG_1_BUS_MASTER (1 << 2)
80 #define CFG_1_MEMORY_SPACE (1 << 1)
81 #define CFG_1_IO_SPACE (1 << 0)
83 #define T_XUSB_CFG_2 0x008
84 #define T_XUSB_CFG_3 0x00C
85 #define T_XUSB_CFG_4 0x010
86 #define CFG_4_BASE_ADDRESS(x) (((x) & 0x1FFFF) << 15)
88 #define T_XUSB_CFG_5 0x014
89 #define T_XUSB_CFG_ARU_MAILBOX_CMD 0x0E4
90 #define ARU_MAILBOX_CMD_INT_EN (1U << 31)
91 #define ARU_MAILBOX_CMD_DEST_XHCI (1 << 30)
92 #define ARU_MAILBOX_CMD_DEST_SMI (1 << 29)
93 #define ARU_MAILBOX_CMD_DEST_PME (1 << 28)
94 #define ARU_MAILBOX_CMD_DEST_FALC (1 << 27)
96 #define T_XUSB_CFG_ARU_MAILBOX_DATA_IN 0x0E8
97 #define ARU_MAILBOX_DATA_IN_DATA(x) (((x) & 0xFFFFFF) << 0)
98 #define ARU_MAILBOX_DATA_IN_TYPE(x) (((x) & 0x0000FF) << 24)
100 #define T_XUSB_CFG_ARU_MAILBOX_DATA_OUT 0x0EC
101 #define ARU_MAILBOX_DATA_OUT_DATA(x) (((x) >> 0) & 0xFFFFFF)
102 #define ARU_MAILBOX_DATA_OUT_TYPE(x) (((x) >> 24) & 0x0000FF)
104 #define T_XUSB_CFG_ARU_MAILBOX_OWNER 0x0F0
105 #define ARU_MAILBOX_OWNER_SW 2
106 #define ARU_MAILBOX_OWNER_FW 1
107 #define ARU_MAILBOX_OWNER_NONE 0
109 #define XUSB_CFG_ARU_C11_CSBRANGE 0x41C /* ! UNDOCUMENTED ! */
110 #define ARU_C11_CSBRANGE_PAGE(x) ((x) >> 9)
111 #define ARU_C11_CSBRANGE_ADDR(x) (0x800 + ((x) & 0x1FF))
112 #define XUSB_CFG_ARU_SMI_INTR 0x428 /* ! UNDOCUMENTED ! */
113 #define ARU_SMI_INTR_EN (1 << 3)
114 #define ARU_SMI_INTR_FW_HANG (1 << 1)
115 #define XUSB_CFG_ARU_RST 0x42C /* ! UNDOCUMENTED ! */
116 #define ARU_RST_RESET (1 << 0)
118 #define XUSB_HOST_CONFIGURATION 0x180
119 #define CONFIGURATION_CLKEN_OVERRIDE (1U<< 31)
120 #define CONFIGURATION_PW_NO_DEVSEL_ERR_CYA (1 << 19)
121 #define CONFIGURATION_INITIATOR_READ_IDLE (1 << 18)
122 #define CONFIGURATION_INITIATOR_WRITE_IDLE (1 << 17)
123 #define CONFIGURATION_WDATA_LEAD_CYA (1 << 15)
124 #define CONFIGURATION_WR_INTRLV_CYA (1 << 14)
125 #define CONFIGURATION_TARGET_READ_IDLE (1 << 11)
126 #define CONFIGURATION_TARGET_WRITE_IDLE (1 << 10)
127 #define CONFIGURATION_MSI_VEC_EMPTY (1 << 9)
128 #define CONFIGURATION_UFPCI_MSIAW (1 << 7)
129 #define CONFIGURATION_UFPCI_PWPASSPW (1 << 6)
130 #define CONFIGURATION_UFPCI_PASSPW (1 << 5)
131 #define CONFIGURATION_UFPCI_PWPASSNPW (1 << 4)
132 #define CONFIGURATION_DFPCI_PWPASSNPW (1 << 3)
133 #define CONFIGURATION_DFPCI_RSPPASSPW (1 << 2)
134 #define CONFIGURATION_DFPCI_PASSPW (1 << 1)
135 #define CONFIGURATION_EN_FPCI (1 << 0)
137 /* IPFS address space */
138 #define XUSB_HOST_FPCI_ERROR_MASKS 0x184
139 #define FPCI_ERROR_MASTER_ABORT (1 << 2)
140 #define FPCI_ERRORI_DATA_ERROR (1 << 1)
141 #define FPCI_ERROR_TARGET_ABORT (1 << 0)
143 #define XUSB_HOST_INTR_MASK 0x188
144 #define INTR_IP_INT_MASK (1 << 16)
145 #define INTR_MSI_MASK (1 << 8)
146 #define INTR_INT_MASK (1 << 0)
148 #define XUSB_HOST_CLKGATE_HYSTERESIS 0x1BC
151 #define XUSB_FALCON_CPUCTL 0x100
152 #define CPUCTL_STOPPED (1 << 5)
153 #define CPUCTL_HALTED (1 << 4)
154 #define CPUCTL_HRESET (1 << 3)
155 #define CPUCTL_SRESET (1 << 2)
156 #define CPUCTL_STARTCPU (1 << 1)
157 #define CPUCTL_IINVAL (1 << 0)
159 #define XUSB_FALCON_BOOTVEC 0x104
160 #define XUSB_FALCON_DMACTL 0x10C
161 #define XUSB_FALCON_IMFILLRNG1 0x154
162 #define IMFILLRNG1_TAG_HI(x) (((x) & 0xFFF) << 16)
163 #define IMFILLRNG1_TAG_LO(x) (((x) & 0xFFF) << 0)
164 #define XUSB_FALCON_IMFILLCTL 0x158
167 #define XUSB_CSB_MEMPOOL_APMAP 0x10181C
168 #define APMAP_BOOTPATH (1U << 31)
170 #define XUSB_CSB_MEMPOOL_ILOAD_ATTR 0x101A00
171 #define XUSB_CSB_MEMPOOL_ILOAD_BASE_LO 0x101A04
172 #define XUSB_CSB_MEMPOOL_ILOAD_BASE_HI 0x101A08
173 #define XUSB_CSB_MEMPOOL_L2IMEMOP_SIZE 0x101A10
174 #define L2IMEMOP_SIZE_OFFSET(x) (((x) & 0x3FF) << 8)
175 #define L2IMEMOP_SIZE_SIZE(x) (((x) & 0x0FF) << 24)
177 #define XUSB_CSB_MEMPOOL_L2IMEMOP_TRIG 0x101A14
178 #define L2IMEMOP_INVALIDATE_ALL (0x40 << 24)
179 #define L2IMEMOP_LOAD_LOCKED_RESULT (0x11 << 24)
181 #define XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT 0x101A18
182 #define L2IMEMOP_RESULT_VLD (1U << 31)
184 #define XUSB_CSB_IMEM_BLOCK_SIZE 256
186 #define TEGRA_XHCI_SS_HIGH_SPEED 120000000
187 #define TEGRA_XHCI_SS_LOW_SPEED 12000000
190 #define MBOX_CMD_MSG_ENABLED 1
191 #define MBOX_CMD_INC_FALC_CLOCK 2
192 #define MBOX_CMD_DEC_FALC_CLOCK 3
193 #define MBOX_CMD_INC_SSPI_CLOCK 4
194 #define MBOX_CMD_DEC_SSPI_CLOCK 5
195 #define MBOX_CMD_SET_BW 6
196 #define MBOX_CMD_SET_SS_PWR_GATING 7
197 #define MBOX_CMD_SET_SS_PWR_UNGATING 8
198 #define MBOX_CMD_SAVE_DFE_CTLE_CTX 9
199 #define MBOX_CMD_AIRPLANE_MODE_ENABLED 10
200 #define MBOX_CMD_AIRPLANE_MODE_DISABLED 11
201 #define MBOX_CMD_START_HSIC_IDLE 12
202 #define MBOX_CMD_STOP_HSIC_IDLE 13
203 #define MBOX_CMD_DBC_WAKE_STACK 14
204 #define MBOX_CMD_HSIC_PRETEND_CONNECT 15
205 #define MBOX_CMD_RESET_SSPI 16
206 #define MBOX_CMD_DISABLE_SS_LFPS_DETECTION 17
207 #define MBOX_CMD_ENABLE_SS_LFPS_DETECTION 18
209 /* MBOX responses. */
210 #define MBOX_CMD_ACK (0x80 + 0)
211 #define MBOX_CMD_NAK (0x80 + 1)
213 #define IPFS_WR4(_sc, _r, _v) bus_write_4((_sc)->mem_res_ipfs, (_r), (_v))
214 #define IPFS_RD4(_sc, _r) bus_read_4((_sc)->mem_res_ipfs, (_r))
215 #define FPCI_WR4(_sc, _r, _v) bus_write_4((_sc)->mem_res_fpci, (_r), (_v))
216 #define FPCI_RD4(_sc, _r) bus_read_4((_sc)->mem_res_fpci, (_r))
218 #define LOCK(_sc) mtx_lock(&(_sc)->mtx)
219 #define UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
220 #define SLEEP(_sc, timeout) \
221 mtx_sleep(sc, &sc->mtx, 0, "tegra_xhci", timeout);
222 #define LOCK_INIT(_sc) \
223 mtx_init(&_sc->mtx, device_get_nameunit(_sc->dev), "tegra_xhci", MTX_DEF)
224 #define LOCK_DESTROY(_sc) mtx_destroy(&_sc->mtx)
225 #define ASSERT_LOCKED(_sc) mtx_assert(&_sc->mtx, MA_OWNED)
226 #define ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->mtx, MA_NOTOWNED)
228 struct tegra_xusb_fw_hdr {
229 uint32_t boot_loadaddr_in_imem;
230 uint32_t boot_codedfi_offset;
231 uint32_t boot_codetag;
232 uint32_t boot_codesize;
234 uint32_t phys_memaddr;
235 uint16_t reqphys_memsize;
236 uint16_t alloc_phys_memsize;
238 uint32_t rodata_img_offset;
239 uint32_t rodata_section_start;
240 uint32_t rodata_section_end;
241 uint32_t main_fnaddr;
243 uint32_t fwimg_cksum;
244 uint32_t fwimg_created_time;
246 uint32_t imem_resident_start;
247 uint32_t imem_resident_end;
248 uint32_t idirect_start;
249 uint32_t idirect_end;
250 uint32_t l2_imem_start;
251 uint32_t l2_imem_end;
253 uint8_t init_ddirect;
255 uint32_t phys_addr_log_buffer;
256 uint32_t total_log_entries;
257 uint32_t dequeue_ptr;
261 uint32_t ss_low_power_entry_timeout;
262 uint8_t num_hsic_port;
265 uint8_t padding[137]; /* Pad to 256 bytes */
269 struct tegra_xhci_softc {
270 struct xhci_softc xhci_softc;
272 struct xhci_soc *soc;
274 struct resource *mem_res_fpci;
275 struct resource *mem_res_ipfs;
276 struct resource *irq_res_mbox;
281 clk_t clk_xusb_falcon_src;
283 clk_t clk_xusb_hs_src;
284 clk_t clk_xusb_fs_src;
285 hwreset_t hwreset_xusb_host;
286 hwreset_t hwreset_xusb_ss;
287 regulator_t regulators[16]; /* Safe maximum */
288 phy_t phys[8]; /* Safe maximum */
290 struct intr_config_hook irq_hook;
298 char **regulator_names;
302 /* Tegra 124 config */
303 static char *tegra124_reg_names[] = {
307 "avdd-pll-utmip-supply",
308 "avdd-pll-erefe-supply",
309 "avdd-usb-ss-pll-supply",
310 "hvdd-usb-ss-supply",
311 "hvdd-usb-ss-pll-e-supply",
315 static char *tegra124_phy_names[] = {
323 static struct xhci_soc tegra124_soc =
325 .fw_name = "tegra124_xusb_fw",
326 .regulator_names = tegra124_reg_names,
327 .phy_names = tegra124_phy_names,
330 /* Tegra 210 config */
331 static char *tegra210_reg_names[] = {
335 "avdd-pll-utmip-supply",
336 "avdd-pll-uerefe-supply",
337 "dvdd-usb-ss-pll-supply",
338 "hvdd-usb-ss-pll-e-supply",
342 static char *tegra210_phy_names[] = {
352 static struct xhci_soc tegra210_soc =
354 .fw_name = "tegra210_xusb_fw",
355 .regulator_names = tegra210_reg_names,
356 .phy_names = tegra210_phy_names,
359 /* Compatible devices. */
360 static struct ofw_compat_data compat_data[] = {
361 {"nvidia,tegra124-xusb", (uintptr_t)&tegra124_soc},
362 {"nvidia,tegra210-xusb", (uintptr_t)&tegra210_soc},
368 CSB_RD4(struct tegra_xhci_softc *sc, uint32_t addr)
371 FPCI_WR4(sc, XUSB_CFG_ARU_C11_CSBRANGE, ARU_C11_CSBRANGE_PAGE(addr));
372 return (FPCI_RD4(sc, ARU_C11_CSBRANGE_ADDR(addr)));
376 CSB_WR4(struct tegra_xhci_softc *sc, uint32_t addr, uint32_t val)
379 FPCI_WR4(sc, XUSB_CFG_ARU_C11_CSBRANGE, ARU_C11_CSBRANGE_PAGE(addr));
380 FPCI_WR4(sc, ARU_C11_CSBRANGE_ADDR(addr), val);
384 get_fdt_resources(struct tegra_xhci_softc *sc, phandle_t node)
389 for (i = 0; sc->soc->regulator_names[i] != NULL; i++) {
390 if (i >= nitems(sc->regulators)) {
391 device_printf(sc->dev,
392 "Too many regulators present in DT.\n");
395 rv = regulator_get_by_ofw_property(sc->dev, 0,
396 sc->soc->regulator_names[i], sc->regulators + i);
398 device_printf(sc->dev,
399 "Cannot get '%s' regulator\n",
400 sc->soc->regulator_names[i]);
405 rv = hwreset_get_by_ofw_name(sc->dev, 0, "xusb_host",
406 &sc->hwreset_xusb_host);
408 device_printf(sc->dev, "Cannot get 'xusb_host' reset\n");
411 rv = hwreset_get_by_ofw_name(sc->dev, 0, "xusb_ss",
412 &sc->hwreset_xusb_ss);
414 device_printf(sc->dev, "Cannot get 'xusb_ss' reset\n");
419 for (i = 0; sc->soc->phy_names[i] != NULL; i++) {
420 if (i >= nitems(sc->phys)) {
421 device_printf(sc->dev,
422 "Too many phys present in DT.\n");
425 rv = phy_get_by_ofw_name(sc->dev, 0, sc->soc->phy_names[i],
427 if (rv != 0 && rv != ENOENT) {
428 device_printf(sc->dev, "Cannot get '%s' phy.\n",
429 sc->soc->phy_names[i]);
434 rv = clk_get_by_ofw_name(sc->dev, 0, "xusb_host",
437 device_printf(sc->dev, "Cannot get 'xusb_host' clock\n");
440 rv = clk_get_by_ofw_name(sc->dev, 0, "xusb_falcon_src",
441 &sc->clk_xusb_falcon_src);
443 device_printf(sc->dev, "Cannot get 'xusb_falcon_src' clock\n");
446 rv = clk_get_by_ofw_name(sc->dev, 0, "xusb_ss",
449 device_printf(sc->dev, "Cannot get 'xusb_ss' clock\n");
452 rv = clk_get_by_ofw_name(sc->dev, 0, "xusb_hs_src",
453 &sc->clk_xusb_hs_src);
455 device_printf(sc->dev, "Cannot get 'xusb_hs_src' clock\n");
458 rv = clk_get_by_ofw_name(sc->dev, 0, "xusb_fs_src",
459 &sc->clk_xusb_fs_src);
461 device_printf(sc->dev, "Cannot get 'xusb_fs_src' clock\n");
464 /* Clock xusb_gate is missing in mainstream DT */
465 rv = clk_get_by_name(sc->dev, "xusb_gate", &sc->clk_xusb_gate);
467 device_printf(sc->dev, "Cannot get 'xusb_gate' clock\n");
474 enable_fdt_resources(struct tegra_xhci_softc *sc)
478 rv = hwreset_assert(sc->hwreset_xusb_host);
480 device_printf(sc->dev, "Cannot reset 'xusb_host' reset\n");
483 rv = hwreset_assert(sc->hwreset_xusb_ss);
485 device_printf(sc->dev, "Cannot reset 'xusb_ss' reset\n");
490 for (i = 0; i < nitems(sc->regulators); i++) {
491 if (sc->regulators[i] == NULL)
493 rv = regulator_enable(sc->regulators[i]);
495 device_printf(sc->dev,
496 "Cannot enable '%s' regulator\n",
497 sc->soc->regulator_names[i]);
502 /* Power off XUSB host and XUSB SS domains. */
503 rv = tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
505 device_printf(sc->dev, "Cannot powerdown 'xusba' domain\n");
508 rv = tegra_powergate_power_off(TEGRA_POWERGATE_XUSBC);
510 device_printf(sc->dev, "Cannot powerdown 'xusbc' domain\n");
514 /* Setup XUSB ss_src clock first */
515 clk_set_freq(sc->clk_xusb_ss, TEGRA_XHCI_SS_HIGH_SPEED, 0);
519 /* The XUSB gate clock must be enabled before XUSBA can be powered. */
520 rv = clk_enable(sc->clk_xusb_gate);
522 device_printf(sc->dev,
523 "Cannot enable 'xusb_gate' clock\n");
527 /* Power on XUSB host and XUSB SS domains. */
528 rv = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBC,
529 sc->clk_xusb_host, sc->hwreset_xusb_host);
531 device_printf(sc->dev, "Cannot powerup 'xusbc' domain\n");
534 rv = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBA,
535 sc->clk_xusb_ss, sc->hwreset_xusb_ss);
537 device_printf(sc->dev, "Cannot powerup 'xusba' domain\n");
541 /* Enable rest of clocks */
542 rv = clk_enable(sc->clk_xusb_falcon_src);
544 device_printf(sc->dev,
545 "Cannot enable 'xusb_falcon_src' clock\n");
548 rv = clk_enable(sc->clk_xusb_fs_src);
550 device_printf(sc->dev,
551 "Cannot enable 'xusb_fs_src' clock\n");
554 rv = clk_enable(sc->clk_xusb_hs_src);
556 device_printf(sc->dev,
557 "Cannot enable 'xusb_hs_src' clock\n");
562 for (i = 0; i < nitems(sc->phys); i++) {
563 if (sc->phys[i] == NULL)
565 rv = phy_enable(sc->phys[i]);
567 device_printf(sc->dev, "Cannot enable '%s' phy\n",
568 sc->soc->phy_names[i]);
576 /* Respond by ACK/NAK back to FW */
578 mbox_send_ack(struct tegra_xhci_softc *sc, uint32_t cmd, uint32_t data)
582 reg = ARU_MAILBOX_DATA_IN_TYPE(cmd) | ARU_MAILBOX_DATA_IN_DATA(data);
583 FPCI_WR4(sc, T_XUSB_CFG_ARU_MAILBOX_DATA_IN, reg);
585 reg = FPCI_RD4(sc, T_XUSB_CFG_ARU_MAILBOX_CMD);
586 reg |= ARU_MAILBOX_CMD_DEST_FALC | ARU_MAILBOX_CMD_INT_EN;
587 FPCI_WR4(sc, T_XUSB_CFG_ARU_MAILBOX_CMD, reg);
590 /* Sent command to FW */
592 mbox_send_cmd(struct tegra_xhci_softc *sc, uint32_t cmd, uint32_t data)
597 reg = FPCI_RD4(sc, T_XUSB_CFG_ARU_MAILBOX_OWNER);
598 if (reg != ARU_MAILBOX_OWNER_NONE) {
599 device_printf(sc->dev,
600 "CPU mailbox is busy: 0x%08X\n", reg);
603 /* XXX Is this right? Retry loop? Wait before send? */
604 FPCI_WR4(sc, T_XUSB_CFG_ARU_MAILBOX_OWNER, ARU_MAILBOX_OWNER_SW);
605 reg = FPCI_RD4(sc, T_XUSB_CFG_ARU_MAILBOX_OWNER);
606 if (reg != ARU_MAILBOX_OWNER_SW) {
607 device_printf(sc->dev,
608 "Cannot acquire CPU mailbox: 0x%08X\n", reg);
611 reg = ARU_MAILBOX_DATA_IN_TYPE(cmd) | ARU_MAILBOX_DATA_IN_DATA(data);
612 FPCI_WR4(sc, T_XUSB_CFG_ARU_MAILBOX_DATA_IN, reg);
614 reg = FPCI_RD4(sc, T_XUSB_CFG_ARU_MAILBOX_CMD);
615 reg |= ARU_MAILBOX_CMD_DEST_FALC | ARU_MAILBOX_CMD_INT_EN;
616 FPCI_WR4(sc, T_XUSB_CFG_ARU_MAILBOX_CMD, reg);
618 for (i = 250; i > 0; i--) {
619 reg = FPCI_RD4(sc, T_XUSB_CFG_ARU_MAILBOX_OWNER);
620 if (reg == ARU_MAILBOX_OWNER_NONE)
625 device_printf(sc->dev,
626 "Command response timeout: 0x%08X\n", reg);
634 process_msg(struct tegra_xhci_softc *sc, uint32_t req_cmd, uint32_t req_data,
635 uint32_t *resp_cmd, uint32_t *resp_data)
640 /* In most cases, data are echoed back. */
641 *resp_data = req_data;
643 case MBOX_CMD_INC_FALC_CLOCK:
644 case MBOX_CMD_DEC_FALC_CLOCK:
645 rv = clk_set_freq(sc->clk_xusb_falcon_src, req_data * 1000ULL,
648 rv = clk_get_freq(sc->clk_xusb_falcon_src, &freq);
649 *resp_data = (uint32_t)(freq / 1000);
651 *resp_cmd = rv == 0 ? MBOX_CMD_ACK: MBOX_CMD_NAK;
654 case MBOX_CMD_INC_SSPI_CLOCK:
655 case MBOX_CMD_DEC_SSPI_CLOCK:
656 rv = clk_set_freq(sc->clk_xusb_ss, req_data * 1000ULL,
659 rv = clk_get_freq(sc->clk_xusb_ss, &freq);
660 *resp_data = (uint32_t)(freq / 1000);
662 *resp_cmd = rv == 0 ? MBOX_CMD_ACK: MBOX_CMD_NAK;
665 case MBOX_CMD_SET_BW:
666 /* No respense is expected. */
670 case MBOX_CMD_SET_SS_PWR_GATING:
671 case MBOX_CMD_SET_SS_PWR_UNGATING:
672 *resp_cmd = MBOX_CMD_NAK;
675 case MBOX_CMD_SAVE_DFE_CTLE_CTX:
676 /* Not implemented yet. */
677 *resp_cmd = MBOX_CMD_ACK;
680 case MBOX_CMD_START_HSIC_IDLE:
681 case MBOX_CMD_STOP_HSIC_IDLE:
682 /* Not implemented yet. */
683 *resp_cmd = MBOX_CMD_NAK;
686 case MBOX_CMD_DISABLE_SS_LFPS_DETECTION:
687 case MBOX_CMD_ENABLE_SS_LFPS_DETECTION:
688 /* Not implemented yet. */
689 *resp_cmd = MBOX_CMD_NAK;
692 case MBOX_CMD_AIRPLANE_MODE_ENABLED:
693 case MBOX_CMD_AIRPLANE_MODE_DISABLED:
694 case MBOX_CMD_DBC_WAKE_STACK:
695 case MBOX_CMD_HSIC_PRETEND_CONNECT:
696 case MBOX_CMD_RESET_SSPI:
697 device_printf(sc->dev,
698 "Received unused/unexpected command: %u\n", req_cmd);
703 device_printf(sc->dev,
704 "Received unknown command: %u\n", req_cmd);
711 struct tegra_xhci_softc *sc;
712 uint32_t reg, msg, resp_cmd, resp_data;
714 sc = (struct tegra_xhci_softc *)arg;
716 /* Clear interrupt first */
717 reg = FPCI_RD4(sc, XUSB_CFG_ARU_SMI_INTR);
718 FPCI_WR4(sc, XUSB_CFG_ARU_SMI_INTR, reg);
719 if (reg & ARU_SMI_INTR_FW_HANG) {
720 device_printf(sc->dev,
721 "XUSB CPU firmware hang!!! CPUCTL: 0x%08X\n",
722 CSB_RD4(sc, XUSB_FALCON_CPUCTL));
725 msg = FPCI_RD4(sc, T_XUSB_CFG_ARU_MAILBOX_DATA_OUT);
727 process_msg(sc, ARU_MAILBOX_DATA_OUT_TYPE(msg),
728 ARU_MAILBOX_DATA_OUT_DATA(msg), &resp_cmd, &resp_data);
730 mbox_send_ack(sc, resp_cmd, resp_data);
732 FPCI_WR4(sc, T_XUSB_CFG_ARU_MAILBOX_OWNER,
733 ARU_MAILBOX_OWNER_NONE);
735 reg = FPCI_RD4(sc, T_XUSB_CFG_ARU_MAILBOX_CMD);
736 reg &= ~ARU_MAILBOX_CMD_DEST_SMI;
737 FPCI_WR4(sc, T_XUSB_CFG_ARU_MAILBOX_CMD, reg);
742 load_fw(struct tegra_xhci_softc *sc)
744 const struct firmware *fw;
745 const struct tegra_xusb_fw_hdr *fw_hdr;
746 vm_paddr_t fw_paddr, fw_base;
749 uint32_t code_tags, code_size;
750 struct clocktime fw_clock;
751 struct timespec fw_timespec;
755 FPCI_WR4(sc, XUSB_CFG_ARU_RST, ARU_RST_RESET);
758 /* Check if FALCON already runs */
759 if (CSB_RD4(sc, XUSB_CSB_MEMPOOL_ILOAD_BASE_LO) != 0) {
760 device_printf(sc->dev,
761 "XUSB CPU is already loaded, CPUCTL: 0x%08X\n",
762 CSB_RD4(sc, XUSB_FALCON_CPUCTL));
766 fw = firmware_get(sc->soc->fw_name);
768 device_printf(sc->dev, "Cannot read xusb firmware\n");
772 /* Allocate uncached memory and copy firmware into. */
773 fw_hdr = (const struct tegra_xusb_fw_hdr *)fw->data;
774 fw_size = fw_hdr->fwimg_len;
776 fw_vaddr = kmem_alloc_contig(fw_size, M_WAITOK, 0, -1UL, PAGE_SIZE, 0,
777 VM_MEMATTR_UNCACHEABLE);
778 fw_paddr = vtophys((uintptr_t)fw_vaddr);
779 fw_hdr = (const struct tegra_xusb_fw_hdr *)fw_vaddr;
780 memcpy(fw_vaddr, fw->data, fw_size);
782 firmware_put(fw, FIRMWARE_UNLOAD);
783 sc->fw_vaddr = fw_vaddr;
784 sc->fw_size = fw_size;
786 /* Setup firmware physical address and size. */
787 fw_base = fw_paddr + sizeof(*fw_hdr);
788 CSB_WR4(sc, XUSB_CSB_MEMPOOL_ILOAD_ATTR, fw_size);
789 CSB_WR4(sc, XUSB_CSB_MEMPOOL_ILOAD_BASE_LO, fw_base & 0xFFFFFFFF);
790 CSB_WR4(sc, XUSB_CSB_MEMPOOL_ILOAD_BASE_HI, (uint64_t)fw_base >> 32);
791 CSB_WR4(sc, XUSB_CSB_MEMPOOL_APMAP, APMAP_BOOTPATH);
793 /* Invalidate full L2IMEM context. */
794 CSB_WR4(sc, XUSB_CSB_MEMPOOL_L2IMEMOP_TRIG,
795 L2IMEMOP_INVALIDATE_ALL);
797 /* Program load of L2IMEM by boot code. */
798 code_tags = howmany(fw_hdr->boot_codetag, XUSB_CSB_IMEM_BLOCK_SIZE);
799 code_size = howmany(fw_hdr->boot_codesize, XUSB_CSB_IMEM_BLOCK_SIZE);
800 CSB_WR4(sc, XUSB_CSB_MEMPOOL_L2IMEMOP_SIZE,
801 L2IMEMOP_SIZE_OFFSET(code_tags) |
802 L2IMEMOP_SIZE_SIZE(code_size));
804 /* Execute L2IMEM boot code fetch. */
805 CSB_WR4(sc, XUSB_CSB_MEMPOOL_L2IMEMOP_TRIG,
806 L2IMEMOP_LOAD_LOCKED_RESULT);
808 /* Program FALCON auto-fill range and block count */
809 CSB_WR4(sc, XUSB_FALCON_IMFILLCTL, code_size);
810 CSB_WR4(sc, XUSB_FALCON_IMFILLRNG1,
811 IMFILLRNG1_TAG_LO(code_tags) |
812 IMFILLRNG1_TAG_HI(code_tags + code_size));
814 CSB_WR4(sc, XUSB_FALCON_DMACTL, 0);
816 for (i = 500; i > 0; i--) {
817 if (CSB_RD4(sc, XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT) &
823 device_printf(sc->dev, "Timedout while wating for DMA, "
825 CSB_RD4(sc, XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT));
829 /* Boot FALCON cpu */
830 CSB_WR4(sc, XUSB_FALCON_BOOTVEC, fw_hdr->boot_codetag);
831 CSB_WR4(sc, XUSB_FALCON_CPUCTL, CPUCTL_STARTCPU);
834 for (i = 50; i > 0; i--) {
835 if (CSB_RD4(sc, XUSB_FALCON_CPUCTL) == CPUCTL_STOPPED)
840 device_printf(sc->dev, "Timedout while wating for FALCON cpu, "
841 "state: 0x%08X\n", CSB_RD4(sc, XUSB_FALCON_CPUCTL));
845 fw_timespec.tv_sec = fw_hdr->fwimg_created_time;
846 fw_timespec.tv_nsec = 0;
847 clock_ts_to_ct(&fw_timespec, &fw_clock);
848 device_printf(sc->dev,
849 " Falcon firmware version: %02X.%02X.%04X,"
850 " (%d/%d/%d %d:%02d:%02d UTC)\n",
851 (fw_hdr->version_id >> 24) & 0xFF,(fw_hdr->version_id >> 15) & 0xFF,
852 fw_hdr->version_id & 0xFFFF,
853 fw_clock.day, fw_clock.mon, fw_clock.year,
854 fw_clock.hour, fw_clock.min, fw_clock.sec);
860 init_hw(struct tegra_xhci_softc *sc)
864 rman_res_t base_addr;
866 base_addr = rman_get_start(sc->xhci_softc.sc_io_res);
868 /* Enable FPCI access */
869 reg = IPFS_RD4(sc, XUSB_HOST_CONFIGURATION);
870 reg |= CONFIGURATION_EN_FPCI;
871 IPFS_WR4(sc, XUSB_HOST_CONFIGURATION, reg);
872 IPFS_RD4(sc, XUSB_HOST_CONFIGURATION);
874 /* Program bar for XHCI base address */
875 reg = FPCI_RD4(sc, T_XUSB_CFG_4);
876 reg &= ~CFG_4_BASE_ADDRESS(~0);
877 reg |= CFG_4_BASE_ADDRESS((uint32_t)base_addr >> 15);
878 FPCI_WR4(sc, T_XUSB_CFG_4, reg);
879 FPCI_WR4(sc, T_XUSB_CFG_5, (uint32_t)((uint64_t)(base_addr) >> 32));
881 /* Enable bus master */
882 reg = FPCI_RD4(sc, T_XUSB_CFG_1);
883 reg |= CFG_1_IO_SPACE;
884 reg |= CFG_1_MEMORY_SPACE;
885 reg |= CFG_1_BUS_MASTER;
886 FPCI_WR4(sc, T_XUSB_CFG_1, reg);
888 /* Enable Interrupts */
889 reg = IPFS_RD4(sc, XUSB_HOST_INTR_MASK);
890 reg |= INTR_IP_INT_MASK;
891 IPFS_WR4(sc, XUSB_HOST_INTR_MASK, reg);
894 IPFS_WR4(sc, XUSB_HOST_CLKGATE_HYSTERESIS, 128);
903 tegra_xhci_probe(device_t dev)
906 if (!ofw_bus_status_okay(dev))
909 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
910 device_set_desc(dev, "Nvidia Tegra XHCI controller");
911 return (BUS_PROBE_DEFAULT);
917 tegra_xhci_detach(device_t dev)
919 struct tegra_xhci_softc *sc;
920 struct xhci_softc *xsc;
922 sc = device_get_softc(dev);
923 xsc = &sc->xhci_softc;
925 /* during module unload there are lots of children leftover */
926 device_delete_children(dev);
927 if (sc->xhci_inited) {
928 usb_callout_drain(&xsc->sc_callout);
929 xhci_halt_controller(xsc);
932 if (xsc->sc_irq_res && xsc->sc_intr_hdl) {
933 bus_teardown_intr(dev, xsc->sc_irq_res, xsc->sc_intr_hdl);
934 xsc->sc_intr_hdl = NULL;
936 if (xsc->sc_irq_res) {
937 bus_release_resource(dev, SYS_RES_IRQ,
938 rman_get_rid(xsc->sc_irq_res), xsc->sc_irq_res);
939 xsc->sc_irq_res = NULL;
941 if (xsc->sc_io_res != NULL) {
942 bus_release_resource(dev, SYS_RES_MEMORY,
943 rman_get_rid(xsc->sc_io_res), xsc->sc_io_res);
944 xsc->sc_io_res = NULL;
948 if (sc->irq_hdl_mbox != NULL)
949 bus_teardown_intr(dev, sc->irq_res_mbox, sc->irq_hdl_mbox);
950 if (sc->fw_vaddr != NULL)
951 kmem_free(sc->fw_vaddr, sc->fw_size);
957 tegra_xhci_attach(device_t dev)
959 struct tegra_xhci_softc *sc;
960 struct xhci_softc *xsc;
964 sc = device_get_softc(dev);
966 sc->soc = (struct xhci_soc *)ofw_bus_search_compatible(dev,
967 compat_data)->ocd_data;
968 node = ofw_bus_get_node(dev);
969 xsc = &sc->xhci_softc;
972 rv = get_fdt_resources(sc, node);
977 rv = enable_fdt_resources(sc);
983 /* Allocate resources. */
985 xsc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
987 if (xsc->sc_io_res == NULL) {
989 "Could not allocate HCD memory resources\n");
994 sc->mem_res_fpci = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
996 if (sc->mem_res_fpci == NULL) {
998 "Could not allocate FPCI memory resources\n");
1003 sc->mem_res_ipfs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1005 if (sc->mem_res_ipfs == NULL) {
1007 "Could not allocate IPFS memory resources\n");
1013 xsc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1015 if (xsc->sc_irq_res == NULL) {
1016 device_printf(dev, "Could not allocate HCD IRQ resources\n");
1021 sc->irq_res_mbox = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1023 if (sc->irq_res_mbox == NULL) {
1024 device_printf(dev, "Could not allocate MBOX IRQ resources\n");
1031 device_printf(dev, "Could not initialize XUSB hardware\n");
1035 /* Wakeup and enable firmaware */
1036 rv = mbox_send_cmd(sc, MBOX_CMD_MSG_ENABLED, 0);
1038 device_printf(sc->dev, "Could not enable XUSB firmware\n");
1042 /* Fill data for XHCI driver. */
1043 xsc->sc_bus.parent = dev;
1044 xsc->sc_bus.devices = xsc->sc_devices;
1045 xsc->sc_bus.devices_max = XHCI_MAX_DEVICES;
1047 xsc->sc_io_tag = rman_get_bustag(xsc->sc_io_res);
1048 xsc->sc_io_hdl = rman_get_bushandle(xsc->sc_io_res);
1049 xsc->sc_io_size = rman_get_size(xsc->sc_io_res);
1050 strlcpy(xsc->sc_vendor, "Nvidia", sizeof(xsc->sc_vendor));
1052 /* Add USB bus device. */
1053 xsc->sc_bus.bdev = device_add_child(sc->dev, "usbus", -1);
1054 if (xsc->sc_bus.bdev == NULL) {
1055 device_printf(sc->dev, "Could not add USB device\n");
1059 device_set_ivars(xsc->sc_bus.bdev, &xsc->sc_bus);
1060 device_set_desc(xsc->sc_bus.bdev, "Nvidia USB 3.0 controller");
1062 rv = xhci_init(xsc, sc->dev, 1);
1064 device_printf(sc->dev, "USB init failed: %d\n", rv);
1067 sc->xhci_inited = true;
1068 rv = xhci_start_controller(xsc);
1070 device_printf(sc->dev,
1071 "Could not start XHCI controller: %d\n", rv);
1075 rv = bus_setup_intr(dev, sc->irq_res_mbox, INTR_TYPE_MISC | INTR_MPSAFE,
1076 NULL, intr_mbox, sc, &sc->irq_hdl_mbox);
1078 device_printf(dev, "Could not setup error IRQ: %d\n",rv);
1079 xsc->sc_intr_hdl = NULL;
1083 rv = bus_setup_intr(dev, xsc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
1084 NULL, (driver_intr_t *)xhci_interrupt, xsc, &xsc->sc_intr_hdl);
1086 device_printf(dev, "Could not setup error IRQ: %d\n",rv);
1087 xsc->sc_intr_hdl = NULL;
1091 /* Probe the bus. */
1092 rv = device_probe_and_attach(xsc->sc_bus.bdev);
1094 device_printf(sc->dev, "Could not initialize USB: %d\n", rv);
1102 tegra_xhci_detach(dev);
1106 static device_method_t xhci_methods[] = {
1107 /* Device interface */
1108 DEVMETHOD(device_probe, tegra_xhci_probe),
1109 DEVMETHOD(device_attach, tegra_xhci_attach),
1110 DEVMETHOD(device_detach, tegra_xhci_detach),
1111 DEVMETHOD(device_suspend, bus_generic_suspend),
1112 DEVMETHOD(device_resume, bus_generic_resume),
1113 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1116 DEVMETHOD(bus_print_child, bus_generic_print_child),
1121 static DEFINE_CLASS_0(xhci, xhci_driver, xhci_methods,
1122 sizeof(struct tegra_xhci_softc));
1123 DRIVER_MODULE(tegra_xhci, simplebus, xhci_driver, NULL, NULL);
1124 MODULE_DEPEND(tegra_xhci, usb, 1, 1, 1);