2 * xhci-tegra.c - Nvidia xHCI host controller driver
4 * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/clk.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/clk.h>
24 #include <linux/ioport.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/irq.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/platform_data/tegra_usb.h>
29 #include <linux/uaccess.h>
30 #include <linux/circ_buf.h>
31 #include <linux/vmalloc.h>
32 #include <linux/debugfs.h>
33 #include <linux/kthread.h>
34 #include <linux/gpio.h>
35 #include <linux/usb/otg.h>
36 #include <linux/clk/tegra.h>
37 #include <linux/tegra-powergate.h>
38 #include <linux/firmware.h>
39 #include <linux/pm_runtime.h>
41 #include <linux/of_device.h>
42 #include <linux/of_gpio.h>
44 #include <mach/tegra_usb_pad_ctrl.h>
45 #include <mach/tegra_usb_pmc.h>
46 #include <mach/pm_domains.h>
48 #include <mach/xusb.h>
50 #include "xhci-tegra.h"
52 #include "../../../arch/arm/mach-tegra/fuse.h"
55 #define FW_IOCTL_LOG_DEQUEUE_LOW (4)
56 #define FW_IOCTL_LOG_DEQUEUE_HIGH (5)
57 #define FW_IOCTL_DATA_SHIFT (0)
58 #define FW_IOCTL_DATA_MASK (0x00ffffff)
59 #define FW_IOCTL_TYPE_SHIFT (24)
60 #define FW_IOCTL_TYPE_MASK (0xff000000)
61 #define FW_LOG_SIZE (sizeof(struct log_entry))
62 #define FW_LOG_COUNT (4096)
63 #define FW_LOG_RING_SIZE (FW_LOG_SIZE * FW_LOG_COUNT)
64 #define FW_LOG_PAYLOAD_SIZE (27)
66 #define CIRC_BUF_SIZE (4 * (1 << 20)) /* 4MB */
67 #define FW_LOG_THREAD_RELAX (msecs_to_jiffies(100))
69 /* tegra_xhci_firmware_log.flags bits */
70 #define FW_LOG_CONTEXT_VALID (0)
71 #define FW_LOG_FILE_OPENED (1)
73 #define PAGE_SELECT_MASK 0xFFFFFE00
74 #define PAGE_SELECT_SHIFT 9
75 #define PAGE_OFFSET_MASK 0x000001FF
76 #define CSB_PAGE_SELECT(_addr) \
78 typecheck(u32, _addr); \
79 ((_addr & PAGE_SELECT_MASK) >> PAGE_SELECT_SHIFT); \
81 #define CSB_PAGE_OFFSET(_addr) \
83 typecheck(u32, _addr); \
84 (_addr & PAGE_OFFSET_MASK); \
87 #define reg_dump(_dev, _base, _reg) \
88 dev_dbg(_dev, "%s: %s @%x = 0x%x\n", __func__, #_reg, \
89 _reg, readl(_base + _reg))
91 #define PMC_PORTMAP_MASK(map, pad) (((map) >> 4*(pad)) & 0xF)
93 #define PMC_USB_DEBOUNCE_DEL_0 0xec
94 #define UTMIP_LINE_DEB_CNT(x) (((x) & 0xf) << 16)
95 #define UTMIP_LINE_DEB_CNT_MASK (0xf << 16)
97 #define PMC_UTMIP_UHSIC_SLEEP_CFG_0 0x1fc
99 /* private data types */
100 /* command requests from the firmware */
102 MBOX_CMD_MSG_ENABLED = 1,
103 MBOX_CMD_INC_FALC_CLOCK,
104 MBOX_CMD_DEC_FALC_CLOCK,
105 MBOX_CMD_INC_SSPI_CLOCK,
106 MBOX_CMD_DEC_SSPI_CLOCK, /* 5 */
108 MBOX_CMD_SET_SS_PWR_GATING,
109 MBOX_CMD_SET_SS_PWR_UNGATING, /* 8 */
110 MBOX_CMD_SAVE_DFE_CTLE_CTX,
111 MBOX_CMD_AIRPLANE_MODE_ENABLED, /* unused */
112 MBOX_CMD_AIRPLANE_MODE_DISABLED, /* 11, unused */
113 MBOX_CMD_STAR_HSIC_IDLE,
114 MBOX_CMD_STOP_HSIC_IDLE,
115 MBOX_CMD_DBC_WAKE_STACK, /* unused */
116 MBOX_CMD_HSIC_PRETEND_CONNECT,
118 /* needs to be the last cmd */
121 /* resp msg to ack above commands */
128 u8 data[FW_LOG_PAYLOAD_SIZE];
132 /* Usb3 Firmware Cfg Table */
134 u32 boot_loadaddr_in_imem;
135 u32 boot_codedfi_offset;
139 /* Physical memory reserved by Bootloader/BIOS */
142 u16 alloc_phys_memsize;
144 /* .rodata section */
145 u32 rodata_img_offset;
146 u32 rodata_section_start;
147 u32 rodata_section_end;
151 u32 fwimg_created_time;
153 /* Fields that get filled by linker during linking phase
154 * or initialized in the FW code.
156 u32 imem_resident_start;
157 u32 imem_resident_end;
165 u32 phys_addr_log_buffer;
166 u32 total_log_entries;
169 /* Below two dummy variables are used to replace
170 * L2IMemSymTabOffsetInDFI and L2IMemSymTabSize in order to
171 * retain the size of struct _CFG_TBL used by other AP/Module.
179 u32 SS_low_power_entry_timeout;
181 u8 padding[139]; /* padding bytes to makeup 256-bytes cfgtbl */
184 struct xusb_save_regs {
190 u32 fpci_error_masks;
192 u32 ipfs_intr_enable;
194 u32 clkgate_hysteresis;
195 u32 xusb_host_mccif_fifo_cntrl;
197 /* PG does not mention below */
207 u32 tap1_val[XUSB_SS_PORT_COUNT];
208 u32 amp_val[XUSB_SS_PORT_COUNT];
209 u32 ctle_z_val[XUSB_SS_PORT_COUNT];
210 u32 ctle_g_val[XUSB_SS_PORT_COUNT];
213 struct tegra_xhci_firmware {
214 void *data; /* kernel virtual address */
215 size_t size; /* firmware size */
216 dma_addr_t dma; /* dma address for controller */
219 struct tegra_xhci_firmware_log {
220 dma_addr_t phys_addr; /* dma-able address */
221 void *virt_addr; /* kernel va of the shared log buffer */
222 struct log_entry *dequeue; /* current dequeue pointer (va) */
223 struct circ_buf circ; /* big circular buffer */
224 u32 seq; /* log sequence number */
226 struct task_struct *thread; /* a thread to consume log */
228 wait_queue_head_t read_wait;
229 wait_queue_head_t write_wait;
230 wait_queue_head_t intr_wait;
232 struct dentry *log_file;
236 /* structure to hold the offsets of padctl registers */
237 struct tegra_xusb_padctl_regs {
246 u16 usb2_bchrg_otgpad0_ctl0_0;
247 u16 usb2_bchrg_otgpad0_ctl1_0;
248 u16 usb2_bchrg_otgpad1_ctl0_0;
249 u16 usb2_bchrg_otgpad1_ctl1_0;
250 u16 usb2_bchrg_otgpad2_ctl0_0;
251 u16 usb2_bchrg_otgpad2_ctl1_0;
252 u16 usb2_bchrg_bias_pad_0;
253 u16 usb2_bchrg_tdcd_dbnc_timer_0;
254 u16 iophy_pll_p0_ctl1_0;
255 u16 iophy_pll_p0_ctl2_0;
256 u16 iophy_pll_p0_ctl3_0;
257 u16 iophy_pll_p0_ctl4_0;
258 u16 iophy_usb3_pad0_ctl1_0;
259 u16 iophy_usb3_pad1_ctl1_0;
260 u16 iophy_usb3_pad0_ctl2_0;
261 u16 iophy_usb3_pad1_ctl2_0;
262 u16 iophy_usb3_pad0_ctl3_0;
263 u16 iophy_usb3_pad1_ctl3_0;
264 u16 iophy_usb3_pad0_ctl4_0;
265 u16 iophy_usb3_pad1_ctl4_0;
266 u16 iophy_misc_pad_p0_ctl1_0;
267 u16 iophy_misc_pad_p1_ctl1_0;
268 u16 iophy_misc_pad_p0_ctl2_0;
269 u16 iophy_misc_pad_p1_ctl2_0;
270 u16 iophy_misc_pad_p0_ctl3_0;
271 u16 iophy_misc_pad_p1_ctl3_0;
272 u16 iophy_misc_pad_p0_ctl4_0;
273 u16 iophy_misc_pad_p1_ctl4_0;
274 u16 iophy_misc_pad_p0_ctl5_0;
275 u16 iophy_misc_pad_p1_ctl5_0;
276 u16 iophy_misc_pad_p0_ctl6_0;
277 u16 iophy_misc_pad_p1_ctl6_0;
278 u16 usb2_otg_pad0_ctl0_0;
279 u16 usb2_otg_pad1_ctl0_0;
280 u16 usb2_otg_pad2_ctl0_0;
281 u16 usb2_otg_pad0_ctl1_0;
282 u16 usb2_otg_pad1_ctl1_0;
283 u16 usb2_otg_pad2_ctl1_0;
284 u16 usb2_bias_pad_ctl0_0;
285 u16 usb2_bias_pad_ctl1_0;
286 u16 usb2_hsic_pad0_ctl0_0;
287 u16 usb2_hsic_pad1_ctl0_0;
288 u16 usb2_hsic_pad0_ctl1_0;
289 u16 usb2_hsic_pad1_ctl1_0;
290 u16 usb2_hsic_pad0_ctl2_0;
291 u16 usb2_hsic_pad1_ctl2_0;
292 u16 ulpi_link_trim_ctl0;
293 u16 ulpi_null_clk_trim_ctl0;
294 u16 hsic_strb_trim_ctl0;
297 u16 iophy_misc_pad_p2_ctl1_0;
298 u16 iophy_misc_pad_p3_ctl1_0;
299 u16 iophy_misc_pad_p4_ctl1_0;
300 u16 iophy_misc_pad_p2_ctl2_0;
301 u16 iophy_misc_pad_p3_ctl2_0;
302 u16 iophy_misc_pad_p4_ctl2_0;
303 u16 iophy_misc_pad_p2_ctl3_0;
304 u16 iophy_misc_pad_p3_ctl3_0;
305 u16 iophy_misc_pad_p4_ctl3_0;
306 u16 iophy_misc_pad_p2_ctl4_0;
307 u16 iophy_misc_pad_p3_ctl4_0;
308 u16 iophy_misc_pad_p4_ctl4_0;
309 u16 iophy_misc_pad_p2_ctl5_0;
310 u16 iophy_misc_pad_p3_ctl5_0;
311 u16 iophy_misc_pad_p4_ctl5_0;
312 u16 iophy_misc_pad_p2_ctl6_0;
313 u16 iophy_misc_pad_p3_ctl6_0;
314 u16 iophy_misc_pad_p4_ctl6_0;
316 u16 iophy_pll_s0_ctl1_0;
317 u16 iophy_pll_s0_ctl2_0;
318 u16 iophy_pll_s0_ctl3_0;
319 u16 iophy_pll_s0_ctl4_0;
320 u16 iophy_misc_pad_s0_ctl1_0;
321 u16 iophy_misc_pad_s0_ctl2_0;
322 u16 iophy_misc_pad_s0_ctl3_0;
323 u16 iophy_misc_pad_s0_ctl4_0;
324 u16 iophy_misc_pad_s0_ctl5_0;
325 u16 iophy_misc_pad_s0_ctl6_0;
328 struct tegra_xhci_hcd {
329 struct platform_device *pdev;
330 struct xhci_hcd *xhci;
334 struct mutex sync_lock;
345 bool host_resume_req;
347 bool dfe_ctx_saved[XUSB_SS_PORT_COUNT];
348 bool ctle_ctx_saved[XUSB_SS_PORT_COUNT];
349 unsigned long last_jiffies;
350 unsigned long host_phy_base;
351 unsigned long host_phy_size;
352 void __iomem *host_phy_virt_base;
354 void __iomem *padctl_base;
355 void __iomem *fpci_base;
356 void __iomem *ipfs_base;
358 struct tegra_xusb_platform_data *pdata;
359 struct tegra_xusb_board_data *bdata;
360 struct tegra_xusb_chip_calib *cdata;
361 struct tegra_xusb_padctl_regs *padregs;
362 const struct tegra_xusb_soc_config *soc_config;
363 u64 tegra_xusb_dmamask;
365 /* mailbox variables */
366 struct mutex mbox_lock;
371 struct regulator *xusb_utmi_vbus_regs[XUSB_UTMI_COUNT];
373 struct regulator *xusb_s1p05v_reg;
374 struct regulator *xusb_s3p3v_reg;
375 struct regulator *xusb_s1p8v_reg;
376 struct regulator *vddio_hsic_reg;
377 int vddio_hsic_refcnt;
379 struct work_struct mbox_work;
380 struct work_struct ss_elpg_exit_work;
381 struct work_struct host_elpg_exit_work;
383 struct clk *host_clk;
386 /* XUSB Falcon SuperSpeed Clock */
387 struct clk *falc_clk;
391 /* XUSB SS PI Clock */
392 struct clk *ss_src_clk;
394 struct clk *plle_clk;
395 struct clk *pll_u_480M;
398 struct clk *pll_re_vco_clk;
400 * XUSB/IPFS specific registers these need to be saved/restored in
401 * addition to spec defined registers
403 struct xusb_save_regs sregs;
404 bool usb2_rh_suspend;
405 bool usb3_rh_suspend;
408 /* otg transceiver */
409 struct usb_phy *transceiver;
410 struct notifier_block otgnb;
412 unsigned long usb2_rh_remote_wakeup_ports; /* one bit per port */
413 unsigned long usb3_rh_remote_wakeup_ports; /* one bit per port */
414 /* firmware loading related */
415 struct tegra_xhci_firmware firmware;
417 struct tegra_xhci_firmware_log log;
422 static int tegra_xhci_probe2(struct tegra_xhci_hcd *tegra);
423 static int tegra_xhci_remove(struct platform_device *pdev);
424 static void init_filesystem_firmware_done(const struct firmware *fw,
427 static struct tegra_usb_pmc_data pmc_data[XUSB_UTMI_COUNT];
428 static struct tegra_usb_pmc_data pmc_hsic_data[XUSB_HSIC_COUNT];
429 static void save_ctle_context(struct tegra_xhci_hcd *tegra,
430 u8 port) __attribute__ ((unused));
432 #ifdef CONFIG_TEGRA_XUSB_USB_BOOTLOADER_FIRMWARE
433 static bool use_bootloader_firmware = true;
435 static bool use_bootloader_firmware;
437 module_param(use_bootloader_firmware, bool, S_IRUGO);
438 MODULE_PARM_DESC(use_bootloader_firmware, "take bootloader initialized firmware");
440 #define FIRMWARE_FILE CONFIG_TEGRA_XUSB_FIRMWARE_FILE
441 static char *firmware_file = FIRMWARE_FILE;
442 #define FIRMWARE_FILE_HELP \
443 "used to specify firmware file of Tegra XHCI host controller. "\
444 "This takes effect only if \"use_bootloader_firmware\" is \"N\". " \
445 "Default value is \"" FIRMWARE_FILE "\"."
447 module_param(firmware_file, charp, S_IRUGO);
448 MODULE_PARM_DESC(firmware_file, FIRMWARE_FILE_HELP);
451 static inline struct tegra_xhci_hcd *hcd_to_tegra_xhci(struct usb_hcd *hcd)
453 return (struct tegra_xhci_hcd *) dev_get_drvdata(hcd->self.controller);
456 static inline void must_have_sync_lock(struct tegra_xhci_hcd *tegra)
458 #if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
459 WARN_ON(tegra->sync_lock.owner != current);
463 #define for_each_enabled_hsic_pad(_pad, _tegra_xhci_hcd) \
464 for (_pad = find_next_enabled_hsic_pad(_tegra_xhci_hcd, 0); \
465 (_pad < XUSB_HSIC_COUNT) && (_pad >= 0); \
466 _pad = find_next_enabled_hsic_pad(_tegra_xhci_hcd, _pad + 1))
468 static inline int find_next_enabled_pad(struct tegra_xhci_hcd *tegra,
471 unsigned long portmap = tegra->bdata->portmap;
472 return find_next_bit(&portmap, last , start);
475 static inline int find_next_enabled_hsic_pad(struct tegra_xhci_hcd *tegra,
478 int start = XUSB_HSIC_INDEX + curr_pad;
479 int last = XUSB_HSIC_INDEX + XUSB_HSIC_COUNT;
481 if ((curr_pad < 0) || (curr_pad >= XUSB_HSIC_COUNT))
484 return find_next_enabled_pad(tegra, start, last) - XUSB_HSIC_INDEX;
487 static void tegra_xhci_setup_gpio_for_ss_lane(struct tegra_xhci_hcd *tegra)
491 if (!tegra->bdata->gpio_controls_muxed_ss_lanes)
494 if (tegra->bdata->lane_owner & BIT(0)) {
495 /* USB3_SS port1 is using SATA lane so set (MUX_SATA)
498 err = gpio_request((tegra->bdata->gpio_ss1_sata & 0xffff),
501 pr_err("%s: gpio_ss1_sata gpio_request failed %d\n",
503 err = gpio_direction_output((tegra->bdata->gpio_ss1_sata
506 pr_err("%s: gpio_ss1_sata gpio_direction failed %d\n",
508 __gpio_set_value((tegra->bdata->gpio_ss1_sata & 0xffff),
509 ((tegra->bdata->gpio_ss1_sata >> 16)));
513 static u32 xhci_read_portsc(struct xhci_hcd *xhci, unsigned int port)
515 int num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
516 __le32 __iomem *addr;
518 if (port >= num_ports) {
519 xhci_err(xhci, "%s invalid port %u\n", __func__, port);
523 addr = &xhci->op_regs->port_status_base + (NUM_PORT_REGS * port);
524 return xhci_readl(xhci, addr);
527 static void debug_print_portsc(struct xhci_hcd *xhci)
529 __le32 __iomem *addr = &xhci->op_regs->port_status_base;
534 ports = HCS_MAX_PORTS(xhci->hcs_params1);
535 for (i = 0; i < ports; i++) {
536 reg = xhci_read_portsc(xhci, i);
537 xhci_dbg(xhci, "@%p port %d status reg = 0x%x\n",
538 addr, i, (unsigned int) reg);
539 addr += NUM_PORT_REGS;
542 static void tegra_xhci_war_for_tctrl_rctrl(struct tegra_xhci_hcd *tegra);
544 static bool is_otg_host(struct tegra_xhci_hcd *tegra)
546 if (!tegra->transceiver)
548 else if (tegra->transceiver->state == OTG_STATE_A_HOST)
554 static int update_speed(struct tegra_xhci_hcd *tegra, u8 port)
556 struct usb_hcd *hcd = xhci_to_hcd(tegra->xhci);
559 portsc = readl(hcd->regs + BAR0_XHCI_OP_PORTSC(port));
560 if (DEV_FULLSPEED(portsc))
561 return USB_PMC_PORT_SPEED_FULL;
562 else if (DEV_HIGHSPEED(portsc))
563 return USB_PMC_PORT_SPEED_HIGH;
564 else if (DEV_LOWSPEED(portsc))
565 return USB_PMC_PORT_SPEED_LOW;
566 else if (DEV_SUPERSPEED(portsc))
567 return USB_PMC_PORT_SPEED_SUPER;
569 return USB_PMC_PORT_SPEED_UNKNOWN;
572 static void pmc_init(struct tegra_xhci_hcd *tegra)
574 struct tegra_usb_pmc_data *pmc;
575 struct device *dev = &tegra->pdev->dev;
578 for (pad = 0; pad < XUSB_UTMI_COUNT; pad++) {
579 if (BIT(XUSB_UTMI_INDEX + pad) & tegra->bdata->portmap) {
580 dev_dbg(dev, "%s utmi pad %d\n", __func__, pad);
581 pmc = &pmc_data[pad];
582 if (tegra->soc_config->pmc_portmap)
583 pmc->instance = PMC_PORTMAP_MASK(
584 tegra->soc_config->pmc_portmap,
588 pmc->phy_type = TEGRA_USB_PHY_INTF_UTMI;
589 pmc->port_speed = USB_PMC_PORT_SPEED_UNKNOWN;
590 pmc->controller_type = TEGRA_USB_3_0;
591 tegra_usb_pmc_init(pmc);
595 for_each_enabled_hsic_pad(pad, tegra) {
596 dev_dbg(dev, "%s hsic pad %d\n", __func__, pad);
597 pmc = &pmc_hsic_data[pad];
598 pmc->instance = pad + 1;
599 pmc->phy_type = TEGRA_USB_PHY_INTF_HSIC;
600 pmc->port_speed = USB_PMC_PORT_SPEED_HIGH;
601 pmc->controller_type = TEGRA_USB_3_0;
602 tegra_usb_pmc_init(pmc);
606 static void pmc_setup_wake_detect(struct tegra_xhci_hcd *tegra)
608 struct tegra_usb_pmc_data *pmc;
609 struct device *dev = &tegra->pdev->dev;
614 for_each_enabled_hsic_pad(pad, tegra) {
615 dev_dbg(dev, "%s hsic pad %d\n", __func__, pad);
617 pmc = &pmc_hsic_data[pad];
618 port = hsic_pad_to_port(pad);
619 portsc = xhci_read_portsc(tegra->xhci, port);
620 dev_dbg(dev, "%s hsic pad %d portsc 0x%x\n",
621 __func__, pad, portsc);
623 if (((int) portsc != -1) && (portsc & PORT_CONNECT))
624 pmc->pmc_ops->setup_pmc_wake_detect(pmc);
627 for (pad = 0; pad < XUSB_UTMI_COUNT; pad++) {
628 if (BIT(XUSB_UTMI_INDEX + pad) & tegra->bdata->portmap) {
629 dev_dbg(dev, "%s utmi pad %d\n", __func__, pad);
630 pmc = &pmc_data[pad];
631 pmc->port_speed = update_speed(tegra, pad);
633 if (is_otg_host(tegra))
634 pmc->pmc_ops->setup_pmc_wake_detect(
637 pmc->pmc_ops->setup_pmc_wake_detect(pmc);
642 static void pmc_disable_bus_ctrl(struct tegra_xhci_hcd *tegra)
644 struct tegra_usb_pmc_data *pmc;
645 struct device *dev = &tegra->pdev->dev;
648 for_each_enabled_hsic_pad(pad, tegra) {
649 dev_dbg(dev, "%s hsic pad %d\n", __func__, pad);
651 pmc = &pmc_hsic_data[pad];
652 pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
655 for (pad = 0; pad < XUSB_UTMI_COUNT; pad++) {
656 if (BIT(XUSB_UTMI_INDEX + pad) & tegra->bdata->portmap) {
657 dev_dbg(dev, "%s utmi pad %d\n", __func__, pad);
658 pmc = &pmc_data[pad];
659 pmc->pmc_ops->disable_pmc_bus_ctrl(pmc, 0);
664 u32 csb_read(struct tegra_xhci_hcd *tegra, u32 addr)
666 void __iomem *fpci_base = tegra->fpci_base;
667 struct platform_device *pdev = tegra->pdev;
672 /* to select the appropriate CSB page to write to */
673 csb_page_select = CSB_PAGE_SELECT(addr);
675 dev_dbg(&pdev->dev, "csb_read: csb_page_select= 0x%08x\n",
678 iowrite32(csb_page_select, fpci_base + XUSB_CFG_ARU_C11_CSBRANGE);
680 /* selects the appropriate offset in the page to read from */
681 input_addr = CSB_PAGE_OFFSET(addr);
682 data = ioread32(fpci_base + XUSB_CFG_CSB_BASE_ADDR + input_addr);
684 dev_dbg(&pdev->dev, "csb_read: input_addr = 0x%08x data = 0x%08x\n",
689 void csb_write(struct tegra_xhci_hcd *tegra, u32 addr, u32 data)
691 void __iomem *fpci_base = tegra->fpci_base;
692 struct platform_device *pdev = tegra->pdev;
696 /* to select the appropriate CSB page to write to */
697 csb_page_select = CSB_PAGE_SELECT(addr);
699 dev_dbg(&pdev->dev, "csb_write:csb_page_selectx = 0x%08x\n",
702 iowrite32(csb_page_select, fpci_base + XUSB_CFG_ARU_C11_CSBRANGE);
704 /* selects the appropriate offset in the page to write to */
705 input_addr = CSB_PAGE_OFFSET(addr);
706 iowrite32(data, fpci_base + XUSB_CFG_CSB_BASE_ADDR + input_addr);
708 dev_dbg(&pdev->dev, "csb_write: input_addr = 0x%08x data = %0x08x\n",
712 static int fw_message_send(struct tegra_xhci_hcd *tegra,
713 enum MBOX_CMD_TYPE type, u32 data)
715 struct device *dev = &tegra->pdev->dev;
716 void __iomem *base = tegra->fpci_base;
717 unsigned long target;
720 dev_dbg(dev, "%s type %d data 0x%x\n", __func__, type, data);
722 mutex_lock(&tegra->mbox_lock);
724 target = jiffies + msecs_to_jiffies(20);
725 /* wait mailbox to become idle, timeout in 20ms */
726 while (((reg = readl(base + XUSB_CFG_ARU_MBOX_OWNER)) != 0) &&
727 time_is_after_jiffies(target)) {
728 mutex_unlock(&tegra->mbox_lock);
729 usleep_range(100, 200);
730 mutex_lock(&tegra->mbox_lock);
734 dev_err(dev, "%s mailbox is still busy\n", __func__);
738 target = jiffies + msecs_to_jiffies(10);
739 /* acquire mailbox , timeout in 10ms */
740 writel(MBOX_OWNER_SW, base + XUSB_CFG_ARU_MBOX_OWNER);
741 while (((reg = readl(base + XUSB_CFG_ARU_MBOX_OWNER)) != MBOX_OWNER_SW)
742 && time_is_after_jiffies(target)) {
743 mutex_unlock(&tegra->mbox_lock);
744 usleep_range(100, 200);
745 mutex_lock(&tegra->mbox_lock);
746 writel(MBOX_OWNER_SW, base + XUSB_CFG_ARU_MBOX_OWNER);
749 if (reg != MBOX_OWNER_SW) {
750 dev_err(dev, "%s acquire mailbox timeout\n", __func__);
754 reg = CMD_TYPE(type) | CMD_DATA(data);
755 writel(reg, base + XUSB_CFG_ARU_MBOX_DATA_IN);
757 reg = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
758 reg |= MBOX_INT_EN | MBOX_FALC_INT_EN;
759 writel(reg, tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
761 mutex_unlock(&tegra->mbox_lock);
765 reg_dump(dev, base, XUSB_CFG_ARU_MBOX_CMD);
766 reg_dump(dev, base, XUSB_CFG_ARU_MBOX_DATA_IN);
767 reg_dump(dev, base, XUSB_CFG_ARU_MBOX_DATA_OUT);
768 reg_dump(dev, base, XUSB_CFG_ARU_MBOX_OWNER);
769 mutex_unlock(&tegra->mbox_lock);
774 * fw_log_next - find next log entry in a tegra_xhci_firmware_log context.
775 * This function takes care of wrapping. That means when current log entry
776 * is the last one, it returns with the first one.
778 * @param log The tegra_xhci_firmware_log context.
779 * @param this The current log entry.
780 * @return The log entry which is next to the current one.
782 static inline struct log_entry *fw_log_next(
783 struct tegra_xhci_firmware_log *log, struct log_entry *this)
785 struct log_entry *first = (struct log_entry *) log->virt_addr;
786 struct log_entry *last = first + FW_LOG_COUNT - 1;
788 WARN((this < first) || (this > last), "%s: invalid input\n", __func__);
790 return (this == last) ? first : (this + 1);
794 * fw_log_update_dequeue_pointer - update dequeue pointer to both firmware and
795 * tegra_xhci_firmware_log.dequeue.
797 * @param log The tegra_xhci_firmware_log context.
798 * @param n Counts of log entries to fast-forward.
800 static inline void fw_log_update_deq_pointer(
801 struct tegra_xhci_firmware_log *log, int n)
803 struct tegra_xhci_hcd *tegra =
804 container_of(log, struct tegra_xhci_hcd, log);
805 struct device *dev = &tegra->pdev->dev;
806 struct log_entry *deq = tegra->log.dequeue;
807 dma_addr_t physical_addr;
810 dev_dbg(dev, "curr 0x%p fast-forward %d entries\n", deq, n);
812 deq = fw_log_next(log, deq);
814 tegra->log.dequeue = deq;
815 physical_addr = tegra->log.phys_addr +
816 ((u8 *)deq - (u8 *)tegra->log.virt_addr);
818 /* update dequeue pointer to firmware */
819 reg = (FW_IOCTL_LOG_DEQUEUE_LOW << FW_IOCTL_TYPE_SHIFT);
820 reg |= (physical_addr & 0xffff); /* lower 16-bits */
821 iowrite32(reg, tegra->fpci_base + XUSB_CFG_ARU_FW_SCRATCH);
823 reg = (FW_IOCTL_LOG_DEQUEUE_HIGH << FW_IOCTL_TYPE_SHIFT);
824 reg |= ((physical_addr >> 16) & 0xffff); /* higher 16-bits */
825 iowrite32(reg, tegra->fpci_base + XUSB_CFG_ARU_FW_SCRATCH);
827 dev_dbg(dev, "new 0x%p physical addr 0x%x\n", deq, (u32)physical_addr);
830 static inline bool circ_buffer_full(struct circ_buf *circ)
832 int space = CIRC_SPACE(circ->head, circ->tail, CIRC_BUF_SIZE);
834 return (space <= FW_LOG_SIZE);
837 static inline bool fw_log_available(struct tegra_xhci_hcd *tegra)
839 return (tegra->log.dequeue->owner == DRIVER);
843 * fw_log_wait_empty_timeout - wait firmware log thread to clean up shared
845 * @param tegra: tegra_xhci_hcd context
846 * @param msec: timeout value in millisecond
847 * @return true: shared log buffer is empty,
848 * false: shared log buffer isn't empty.
850 static inline bool fw_log_wait_empty_timeout(struct tegra_xhci_hcd *tegra,
853 unsigned long target = jiffies + msecs_to_jiffies(timeout);
856 mutex_lock(&tegra->log.mutex);
858 while (fw_log_available(tegra) && time_is_after_jiffies(target)) {
859 mutex_unlock(&tegra->log.mutex);
860 usleep_range(1000, 2000);
861 mutex_lock(&tegra->log.mutex);
864 ret = fw_log_available(tegra);
865 mutex_unlock(&tegra->log.mutex);
871 * fw_log_copy - copy firmware log from device's buffer to driver's circular
873 * @param tegra tegra_xhci_hcd context
874 * @return true, We still have firmware log in device's buffer to copy.
875 * This function returned due the driver's circular buffer
876 * is full. Caller should invoke this function again as
877 * soon as there is space in driver's circular buffer.
878 * false, Device's buffer is empty.
880 static inline bool fw_log_copy(struct tegra_xhci_hcd *tegra)
882 struct device *dev = &tegra->pdev->dev;
883 struct circ_buf *circ = &tegra->log.circ;
885 int buffer_len, copy_len;
886 struct log_entry *entry;
887 struct log_entry *first = tegra->log.virt_addr;
889 while (fw_log_available(tegra)) {
891 /* calculate maximum contiguous driver buffer length */
893 tail = ACCESS_ONCE(circ->tail);
894 buffer_len = CIRC_SPACE_TO_END(head, tail, CIRC_BUF_SIZE);
895 /* round down to FW_LOG_SIZE */
896 buffer_len -= (buffer_len % FW_LOG_SIZE);
898 return true; /* log available but no space left */
900 /* calculate maximum contiguous log copy length */
901 entry = tegra->log.dequeue;
904 if (tegra->log.seq != entry->sequence_no) {
906 "%s: discontinuous seq no, expect %u get %u\n",
907 __func__, tegra->log.seq, entry->sequence_no);
909 tegra->log.seq = entry->sequence_no + 1;
911 copy_len += FW_LOG_SIZE;
912 buffer_len -= FW_LOG_SIZE;
914 break; /* no space left */
915 entry = fw_log_next(&tegra->log, entry);
916 } while ((entry->owner == DRIVER) && (entry != first));
918 memcpy(&circ->buf[head], tegra->log.dequeue, copy_len);
919 memset(tegra->log.dequeue, 0, copy_len);
920 circ->head = (circ->head + copy_len) & (CIRC_BUF_SIZE - 1);
924 fw_log_update_deq_pointer(&tegra->log, copy_len/FW_LOG_SIZE);
926 dev_dbg(dev, "copied %d entries, new dequeue 0x%p\n",
927 copy_len/FW_LOG_SIZE, tegra->log.dequeue);
928 wake_up_interruptible(&tegra->log.read_wait);
934 static int fw_log_thread(void *data)
936 struct tegra_xhci_hcd *tegra = data;
937 struct device *dev = &tegra->pdev->dev;
938 struct circ_buf *circ = &tegra->log.circ;
941 dev_dbg(dev, "start firmware log thread\n");
944 mutex_lock(&tegra->log.mutex);
945 if (circ_buffer_full(circ)) {
946 mutex_unlock(&tegra->log.mutex);
947 dev_info(dev, "%s: circ buffer full\n", __func__);
948 wait_event_interruptible(tegra->log.write_wait,
949 kthread_should_stop() || !circ_buffer_full(circ));
950 mutex_lock(&tegra->log.mutex);
953 logs_left = fw_log_copy(tegra);
954 mutex_unlock(&tegra->log.mutex);
956 /* relax if no logs left */
958 wait_event_interruptible_timeout(tegra->log.intr_wait,
959 fw_log_available(tegra), FW_LOG_THREAD_RELAX);
960 } while (!kthread_should_stop());
962 dev_dbg(dev, "stop firmware log thread\n");
966 static inline bool circ_buffer_empty(struct circ_buf *circ)
968 return (CIRC_CNT(circ->head, circ->tail, CIRC_BUF_SIZE) == 0);
971 static ssize_t fw_log_file_read(struct file *file, char __user *buf,
972 size_t count, loff_t *offp)
974 struct tegra_xhci_hcd *tegra = file->private_data;
975 struct platform_device *pdev = tegra->pdev;
976 struct circ_buf *circ = &tegra->log.circ;
981 mutex_lock(&tegra->log.mutex);
983 while (circ_buffer_empty(circ)) {
984 mutex_unlock(&tegra->log.mutex);
985 if (file->f_flags & O_NONBLOCK)
986 return -EAGAIN; /* non-blocking read */
988 dev_dbg(&pdev->dev, "%s: nothing to read\n", __func__);
990 if (wait_event_interruptible(tegra->log.read_wait,
991 !circ_buffer_empty(circ)))
994 if (mutex_lock_interruptible(&tegra->log.mutex))
999 head = ACCESS_ONCE(circ->head);
1001 s = min_t(int, count,
1002 CIRC_CNT_TO_END(head, tail, CIRC_BUF_SIZE));
1005 if (copy_to_user(&buf[n], &circ->buf[tail], s)) {
1006 dev_warn(&pdev->dev, "copy_to_user failed\n");
1007 mutex_unlock(&tegra->log.mutex);
1010 circ->tail = (circ->tail + s) & (CIRC_BUF_SIZE - 1);
1018 mutex_unlock(&tegra->log.mutex);
1020 wake_up_interruptible(&tegra->log.write_wait);
1022 dev_dbg(&pdev->dev, "%s: %d bytes\n", __func__, n);
1027 static int fw_log_file_open(struct inode *inode, struct file *file)
1029 struct tegra_xhci_hcd *tegra;
1030 file->private_data = inode->i_private;
1031 tegra = file->private_data;
1033 if (test_and_set_bit(FW_LOG_FILE_OPENED, &tegra->log.flags)) {
1034 dev_info(&tegra->pdev->dev, "%s: already opened\n", __func__);
1041 static int fw_log_file_close(struct inode *inode, struct file *file)
1043 struct tegra_xhci_hcd *tegra = file->private_data;
1045 clear_bit(FW_LOG_FILE_OPENED, &tegra->log.flags);
1050 static const struct file_operations firmware_log_fops = {
1051 .open = fw_log_file_open,
1052 .release = fw_log_file_close,
1053 .read = fw_log_file_read,
1054 .owner = THIS_MODULE,
1057 static int fw_log_init(struct tegra_xhci_hcd *tegra)
1059 struct platform_device *pdev = tegra->pdev;
1062 /* allocate buffer to be shared between driver and firmware */
1063 tegra->log.virt_addr = dma_alloc_writecombine(&pdev->dev,
1064 FW_LOG_RING_SIZE, &tegra->log.phys_addr, GFP_KERNEL);
1066 if (!tegra->log.virt_addr) {
1067 dev_err(&pdev->dev, "dma_alloc_writecombine() size %d failed\n",
1072 dev_info(&pdev->dev,
1073 "%d bytes log buffer physical 0x%u virtual 0x%p\n",
1074 FW_LOG_RING_SIZE, (u32)tegra->log.phys_addr,
1075 tegra->log.virt_addr);
1077 memset(tegra->log.virt_addr, 0, FW_LOG_RING_SIZE);
1078 tegra->log.dequeue = tegra->log.virt_addr;
1080 tegra->log.circ.buf = vmalloc(CIRC_BUF_SIZE);
1081 if (!tegra->log.circ.buf) {
1082 dev_err(&pdev->dev, "vmalloc size %d failed\n", CIRC_BUF_SIZE);
1084 goto error_free_dma;
1087 tegra->log.circ.head = 0;
1088 tegra->log.circ.tail = 0;
1090 init_waitqueue_head(&tegra->log.read_wait);
1091 init_waitqueue_head(&tegra->log.write_wait);
1092 init_waitqueue_head(&tegra->log.intr_wait);
1094 mutex_init(&tegra->log.mutex);
1096 tegra->log.path = debugfs_create_dir("tegra_xhci", NULL);
1097 if (IS_ERR_OR_NULL(tegra->log.path)) {
1098 dev_warn(&pdev->dev, "debugfs_create_dir() failed\n");
1100 goto error_free_mem;
1103 tegra->log.log_file = debugfs_create_file("firmware_log",
1104 S_IRUGO, tegra->log.path, tegra, &firmware_log_fops);
1105 if ((!tegra->log.log_file) ||
1106 (tegra->log.log_file == ERR_PTR(-ENODEV))) {
1107 dev_warn(&pdev->dev, "debugfs_create_file() failed\n");
1109 goto error_remove_debugfs_path;
1112 tegra->log.thread = kthread_run(fw_log_thread, tegra, "xusb-fw-log");
1113 if (IS_ERR(tegra->log.thread)) {
1114 dev_warn(&pdev->dev, "kthread_run() failed\n");
1116 goto error_remove_debugfs_file;
1119 set_bit(FW_LOG_CONTEXT_VALID, &tegra->log.flags);
1122 error_remove_debugfs_file:
1123 debugfs_remove(tegra->log.log_file);
1124 error_remove_debugfs_path:
1125 debugfs_remove(tegra->log.path);
1127 vfree(tegra->log.circ.buf);
1129 dma_free_writecombine(&pdev->dev, FW_LOG_RING_SIZE,
1130 tegra->log.virt_addr, tegra->log.phys_addr);
1131 memset(&tegra->log, sizeof(tegra->log), 0);
1135 static void fw_log_deinit(struct tegra_xhci_hcd *tegra)
1137 struct platform_device *pdev = tegra->pdev;
1139 if (test_and_clear_bit(FW_LOG_CONTEXT_VALID, &tegra->log.flags)) {
1141 debugfs_remove(tegra->log.log_file);
1142 debugfs_remove(tegra->log.path);
1144 wake_up_interruptible(&tegra->log.read_wait);
1145 wake_up_interruptible(&tegra->log.write_wait);
1146 kthread_stop(tegra->log.thread);
1148 mutex_lock(&tegra->log.mutex);
1149 dma_free_writecombine(&pdev->dev, FW_LOG_RING_SIZE,
1150 tegra->log.virt_addr, tegra->log.phys_addr);
1151 vfree(tegra->log.circ.buf);
1152 tegra->log.circ.head = tegra->log.circ.tail = 0;
1153 mutex_unlock(&tegra->log.mutex);
1155 mutex_destroy(&tegra->log.mutex);
1159 /* hsic pad operations */
1161 * HSIC pads need VDDIO_HSIC power rail turned on to be functional. There is
1162 * only one VDDIO_HSIC power rail shared by all HSIC pads.
1164 static int hsic_power_rail_enable(struct tegra_xhci_hcd *tegra)
1166 struct device *dev = &tegra->pdev->dev;
1167 struct tegra_xusb_regulator_name *supply = &tegra->bdata->supply;
1170 if (tegra->vddio_hsic_reg)
1173 tegra->vddio_hsic_reg = devm_regulator_get(dev, supply->vddio_hsic);
1174 if (IS_ERR_OR_NULL(tegra->vddio_hsic_reg)) {
1175 dev_err(dev, "%s get vddio_hsic failed\n", __func__);
1176 ret = PTR_ERR(tegra->vddio_hsic_reg);
1180 dev_dbg(dev, "%s regulator_enable vddio_hsic\n", __func__);
1181 ret = regulator_enable(tegra->vddio_hsic_reg);
1183 dev_err(dev, "%s enable vddio_hsic failed\n", __func__);
1188 tegra->vddio_hsic_refcnt++;
1189 WARN(tegra->vddio_hsic_refcnt > XUSB_HSIC_COUNT,
1190 "vddio_hsic_refcnt exceeds\n");
1194 devm_regulator_put(tegra->vddio_hsic_reg);
1196 tegra->vddio_hsic_reg = NULL;
1200 static int hsic_power_rail_disable(struct tegra_xhci_hcd *tegra)
1202 struct device *dev = &tegra->pdev->dev;
1205 WARN_ON(!tegra->vddio_hsic_reg || !tegra->vddio_hsic_refcnt);
1207 tegra->vddio_hsic_refcnt--;
1208 if (tegra->vddio_hsic_refcnt)
1211 dev_dbg(dev, "%s regulator_disable vddio_hsic\n", __func__);
1212 ret = regulator_disable(tegra->vddio_hsic_reg);
1214 dev_err(dev, "%s disable vddio_hsic failed\n", __func__);
1215 tegra->vddio_hsic_refcnt++;
1219 devm_regulator_put(tegra->vddio_hsic_reg);
1220 tegra->vddio_hsic_reg = NULL;
1225 static int hsic_pad_enable(struct tegra_xhci_hcd *tegra, unsigned pad)
1227 struct device *dev = &tegra->pdev->dev;
1228 void __iomem *base = tegra->padctl_base;
1229 struct tegra_xusb_hsic_config *hsic = &tegra->bdata->hsic[pad];
1232 if (pad >= XUSB_HSIC_COUNT) {
1233 dev_err(dev, "%s invalid HSIC pad number %d\n", __func__, pad);
1237 dev_dbg(dev, "%s pad %u\n", __func__, pad);
1239 reg = readl(base + HSIC_PAD_CTL_2(pad));
1240 reg &= ~(RX_STROBE_TRIM(~0) | RX_DATA_TRIM(~0));
1241 reg |= RX_STROBE_TRIM(hsic->rx_strobe_trim);
1242 reg |= RX_DATA_TRIM(hsic->rx_data_trim);
1243 writel(reg, base + HSIC_PAD_CTL_2(pad));
1245 reg = readl(base + HSIC_PAD_CTL_0(pad));
1246 reg &= ~(TX_RTUNEP(~0) | TX_RTUNEN(~0) | TX_SLEWP(~0) | TX_SLEWN(~0));
1247 reg |= TX_RTUNEP(hsic->tx_rtune_p);
1248 reg |= TX_RTUNEN(hsic->tx_rtune_n);
1249 reg |= TX_SLEWP(hsic->tx_slew_p);
1250 reg |= TX_SLEWN(hsic->tx_slew_n);
1251 writel(reg, base + HSIC_PAD_CTL_0(pad));
1253 reg = readl(base + HSIC_PAD_CTL_1(pad));
1254 reg &= ~(RPD_DATA | RPD_STROBE | RPU_DATA | RPU_STROBE);
1255 reg |= (RPD_DATA | RPU_STROBE); /* keep HSIC in IDLE */
1256 if (hsic->auto_term_en)
1257 reg |= AUTO_TERM_EN;
1259 reg &= ~AUTO_TERM_EN;
1260 reg &= ~(PD_RX | HSIC_PD_ZI | PD_TRX | PD_TX);
1261 writel(reg, base + HSIC_PAD_CTL_1(pad));
1263 reg = readl(base + HSIC_STRB_TRIM_CONTROL);
1264 reg &= ~(STRB_TRIM_VAL(~0));
1265 reg |= STRB_TRIM_VAL(hsic->strb_trim_val);
1266 writel(reg, base + HSIC_STRB_TRIM_CONTROL);
1268 reg = readl(base + USB2_PAD_MUX);
1269 reg |= USB2_HSIC_PAD_PORT(pad);
1270 writel(reg, base + USB2_PAD_MUX);
1272 reg_dump(dev, base, HSIC_PAD_CTL_0(pad));
1273 reg_dump(dev, base, HSIC_PAD_CTL_1(pad));
1274 reg_dump(dev, base, HSIC_PAD_CTL_2(pad));
1275 reg_dump(dev, base, HSIC_STRB_TRIM_CONTROL);
1276 reg_dump(dev, base, USB2_PAD_MUX);
1280 static void hsic_pad_pretend_connect(struct tegra_xhci_hcd *tegra)
1282 struct device *dev = &tegra->pdev->dev;
1283 struct tegra_xusb_hsic_config *hsic;
1284 struct usb_device *hs_root_hub = tegra->xhci->main_hcd->self.root_hub;
1288 int enabled_pads = 0;
1289 unsigned long wait_ports = 0;
1290 unsigned long target;
1292 for_each_enabled_hsic_pad(pad, tegra) {
1293 hsic = &tegra->bdata->hsic[pad];
1294 if (hsic->pretend_connect)
1298 if (enabled_pads == 0) {
1299 dev_dbg(dev, "%s no hsic pretend_connect enabled\n", __func__);
1303 usb_disable_autosuspend(hs_root_hub);
1305 for_each_enabled_hsic_pad(pad, tegra) {
1306 hsic = &tegra->bdata->hsic[pad];
1307 if (!hsic->pretend_connect)
1310 port = hsic_pad_to_port(pad);
1311 portsc = xhci_read_portsc(tegra->xhci, port);
1312 dev_dbg(dev, "%s pad %u portsc 0x%x\n", __func__, pad, portsc);
1314 if (!(portsc & PORT_CONNECT)) {
1315 /* firmware wants 1-based port index */
1316 fw_message_send(tegra,
1317 MBOX_CMD_HSIC_PRETEND_CONNECT, BIT(port + 1));
1320 set_bit(port, &wait_ports);
1323 /* wait till port reaches U0 */
1324 target = jiffies + msecs_to_jiffies(500);
1326 for_each_set_bit(port, &wait_ports, BITS_PER_LONG) {
1327 portsc = xhci_read_portsc(tegra->xhci, port);
1328 pad = port_to_hsic_pad(port);
1329 dev_dbg(dev, "%s pad %u portsc 0x%x\n", __func__,
1331 if ((PORT_PLS_MASK & portsc) == XDEV_U0)
1332 clear_bit(port, &wait_ports);
1336 usleep_range(1000, 5000);
1337 } while (wait_ports && time_is_after_jiffies(target));
1340 dev_warn(dev, "%s HSIC pad(s) didn't reach U0.\n", __func__);
1342 usb_enable_autosuspend(hs_root_hub);
1347 static int hsic_pad_disable(struct tegra_xhci_hcd *tegra, unsigned pad)
1349 struct device *dev = &tegra->pdev->dev;
1350 void __iomem *base = tegra->padctl_base;
1353 if (pad >= XUSB_HSIC_COUNT) {
1354 dev_err(dev, "%s invalid HSIC pad number %d\n", __func__, pad);
1358 dev_dbg(dev, "%s pad %u\n", __func__, pad);
1360 reg = readl(base + USB2_PAD_MUX);
1361 reg &= ~USB2_HSIC_PAD_PORT(pad);
1362 writel(reg, base + USB2_PAD_MUX);
1364 reg = readl(base + HSIC_PAD_CTL_1(pad));
1365 reg |= (PD_RX | HSIC_PD_ZI | PD_TRX | PD_TX);
1366 writel(reg, base + HSIC_PAD_CTL_1(pad));
1368 reg_dump(dev, base, HSIC_PAD_CTL_0(pad));
1369 reg_dump(dev, base, HSIC_PAD_CTL_1(pad));
1370 reg_dump(dev, base, HSIC_PAD_CTL_2(pad));
1371 reg_dump(dev, base, USB2_PAD_MUX);
1375 enum hsic_pad_pupd {
1381 static int hsic_pad_pupd_set(struct tegra_xhci_hcd *tegra, unsigned pad,
1382 enum hsic_pad_pupd pupd)
1384 struct device *dev = &tegra->pdev->dev;
1385 void __iomem *base = tegra->padctl_base;
1388 if (pad >= XUSB_HSIC_COUNT) {
1389 dev_err(dev, "%s invalid HSIC pad number %u\n", __func__, pad);
1393 dev_dbg(dev, "%s pad %u pupd %d\n", __func__, pad, pupd);
1395 reg = readl(base + HSIC_PAD_CTL_1(pad));
1396 reg &= ~(RPD_DATA | RPD_STROBE | RPU_DATA | RPU_STROBE);
1398 if (pupd == PUPD_IDLE)
1399 reg |= (RPD_DATA | RPU_STROBE);
1400 else if (pupd == PUPD_RESET)
1401 reg |= (RPD_DATA | RPD_STROBE);
1402 else if (pupd != PUPD_DISABLE) {
1403 dev_err(dev, "%s invalid pupd %d\n", __func__, pupd);
1407 writel(reg, base + HSIC_PAD_CTL_1(pad));
1409 reg_dump(dev, base, HSIC_PAD_CTL_1(pad));
1415 static void tegra_xhci_debug_read_pads(struct tegra_xhci_hcd *tegra)
1417 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1418 struct xhci_hcd *xhci = tegra->xhci;
1421 xhci_info(xhci, "============ PADCTL VALUES START =================\n");
1422 reg = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
1423 xhci_info(xhci, " PAD MUX = %x\n", reg);
1424 reg = readl(tegra->padctl_base + padregs->usb2_port_cap_0);
1425 xhci_info(xhci, " PORT CAP = %x\n", reg);
1426 reg = readl(tegra->padctl_base + padregs->snps_oc_map_0);
1427 xhci_info(xhci, " SNPS OC MAP = %x\n", reg);
1428 reg = readl(tegra->padctl_base + padregs->usb2_oc_map_0);
1429 xhci_info(xhci, " USB2 OC MAP = %x\n", reg);
1430 reg = readl(tegra->padctl_base + padregs->ss_port_map_0);
1431 xhci_info(xhci, " SS PORT MAP = %x\n", reg);
1432 reg = readl(tegra->padctl_base + padregs->oc_det_0);
1433 xhci_info(xhci, " OC DET 0= %x\n", reg);
1434 reg = readl(tegra->padctl_base + padregs->iophy_usb3_pad0_ctl2_0);
1435 xhci_info(xhci, " iophy_usb3_pad0_ctl2_0= %x\n", reg);
1436 reg = readl(tegra->padctl_base + padregs->iophy_usb3_pad1_ctl2_0);
1437 xhci_info(xhci, " iophy_usb3_pad1_ctl2_0= %x\n", reg);
1438 reg = readl(tegra->padctl_base + padregs->usb2_otg_pad0_ctl0_0);
1439 xhci_info(xhci, " usb2_otg_pad0_ctl0_0= %x\n", reg);
1440 reg = readl(tegra->padctl_base + padregs->usb2_otg_pad1_ctl0_0);
1441 xhci_info(xhci, " usb2_otg_pad1_ctl0_0= %x\n", reg);
1442 reg = readl(tegra->padctl_base + padregs->usb2_otg_pad0_ctl1_0);
1443 xhci_info(xhci, " usb2_otg_pad0_ctl1_0= %x\n", reg);
1444 reg = readl(tegra->padctl_base + padregs->usb2_otg_pad1_ctl1_0);
1445 xhci_info(xhci, " usb2_otg_pad1_ctl1_0= %x\n", reg);
1446 reg = readl(tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
1447 xhci_info(xhci, " usb2_bias_pad_ctl0_0= %x\n", reg);
1448 reg = readl(tegra->padctl_base + padregs->usb2_hsic_pad0_ctl0_0);
1449 xhci_info(xhci, " usb2_hsic_pad0_ctl0_0= %x\n", reg);
1450 reg = readl(tegra->padctl_base + padregs->usb2_hsic_pad1_ctl0_0);
1451 xhci_info(xhci, " usb2_hsic_pad1_ctl0_0= %x\n", reg);
1452 xhci_info(xhci, "============ PADCTL VALUES END=================\n");
1455 static void tegra_xhci_cfg(struct tegra_xhci_hcd *tegra)
1459 reg = readl(tegra->ipfs_base + IPFS_XUSB_HOST_CONFIGURATION_0);
1460 reg |= IPFS_EN_FPCI;
1461 writel(reg, tegra->ipfs_base + IPFS_XUSB_HOST_CONFIGURATION_0);
1464 /* Program Bar0 Space */
1465 reg = readl(tegra->fpci_base + XUSB_CFG_4);
1466 reg |= tegra->host_phy_base;
1467 writel(reg, tegra->fpci_base + XUSB_CFG_4);
1468 usleep_range(100, 200);
1470 /* Enable Bus Master */
1471 reg = readl(tegra->fpci_base + XUSB_CFG_1);
1473 writel(reg, tegra->fpci_base + XUSB_CFG_1);
1475 /* Set intr mask to enable intr assertion */
1476 reg = readl(tegra->ipfs_base + IPFS_XUSB_HOST_INTR_MASK_0);
1477 reg |= IPFS_IP_INT_MASK;
1478 writel(reg, tegra->ipfs_base + IPFS_XUSB_HOST_INTR_MASK_0);
1480 /* Set hysteris to 0x80 */
1481 writel(0x80, tegra->ipfs_base + IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
1484 static int tegra_xusb_regulator_init(struct tegra_xhci_hcd *tegra,
1485 struct platform_device *pdev)
1487 struct tegra_xusb_regulator_name *supply = &tegra->bdata->supply;
1491 tegra->xusb_s3p3v_reg =
1492 devm_regulator_get(&pdev->dev, supply->s3p3v);
1493 if (IS_ERR(tegra->xusb_s3p3v_reg)) {
1494 dev_err(&pdev->dev, "3p3v: regulator not found: %ld."
1495 , PTR_ERR(tegra->xusb_s3p3v_reg));
1496 err = PTR_ERR(tegra->xusb_s3p3v_reg);
1497 goto err_null_regulator;
1499 err = regulator_enable(tegra->xusb_s3p3v_reg);
1502 "3p3v: regulator enable failed:%d\n", err);
1503 goto err_null_regulator;
1507 /* enable utmi vbuses */
1508 memset(tegra->xusb_utmi_vbus_regs, 0,
1509 sizeof(tegra->xusb_utmi_vbus_regs));
1510 for (i = 0; i < XUSB_UTMI_COUNT; i++) {
1511 struct regulator *reg = NULL;
1512 const char *reg_name = supply->utmi_vbuses[i];
1513 if (BIT(XUSB_UTMI_INDEX + i) & tegra->bdata->portmap) {
1514 if (i == 0 && tegra->transceiver)
1516 reg = devm_regulator_get(&pdev->dev, reg_name);
1519 "%s regulator not found: %ld.",
1520 reg_name, PTR_ERR(reg));
1523 err = regulator_enable(reg);
1526 "%s: regulator enable failed: %d\n",
1531 goto err_put_utmi_vbus_reg;
1533 tegra->xusb_utmi_vbus_regs[i] = reg;
1536 tegra->xusb_s1p8v_reg =
1537 devm_regulator_get(&pdev->dev, supply->s1p8v);
1538 if (IS_ERR(tegra->xusb_s1p8v_reg)) {
1539 dev_err(&pdev->dev, "1p8v regulator not found: %ld."
1540 , PTR_ERR(tegra->xusb_s1p8v_reg));
1541 err = PTR_ERR(tegra->xusb_s1p8v_reg);
1542 goto err_put_utmi_vbus_reg;
1544 err = regulator_enable(tegra->xusb_s1p8v_reg);
1547 "1p8v: regulator enable failed:%d\n", err);
1548 goto err_put_utmi_vbus_reg;
1552 tegra->xusb_s1p05v_reg =
1553 devm_regulator_get(&pdev->dev, supply->s1p05v);
1554 if (IS_ERR(tegra->xusb_s1p05v_reg)) {
1555 dev_err(&pdev->dev, "1p05v: regulator not found: %ld."
1556 , PTR_ERR(tegra->xusb_s1p05v_reg));
1557 err = PTR_ERR(tegra->xusb_s1p05v_reg);
1558 goto err_put_s1p8v_reg;
1560 err = regulator_enable(tegra->xusb_s1p05v_reg);
1563 "1p05v: regulator enable failed:%d\n", err);
1564 goto err_put_s1p8v_reg;
1571 regulator_disable(tegra->xusb_s1p8v_reg);
1572 err_put_utmi_vbus_reg:
1573 for (i = 0; i < XUSB_UTMI_COUNT; i++) {
1574 struct regulator *reg = tegra->xusb_utmi_vbus_regs[i];
1575 if (!IS_ERR_OR_NULL(reg))
1576 regulator_disable(reg);
1578 regulator_disable(tegra->xusb_s3p3v_reg);
1580 for (i = 0; i < XUSB_UTMI_COUNT; i++)
1581 tegra->xusb_utmi_vbus_regs[i] = NULL;
1582 tegra->xusb_s1p05v_reg = NULL;
1583 tegra->xusb_s3p3v_reg = NULL;
1584 tegra->xusb_s1p8v_reg = NULL;
1588 static void tegra_xusb_regulator_deinit(struct tegra_xhci_hcd *tegra)
1592 regulator_disable(tegra->xusb_s1p05v_reg);
1593 regulator_disable(tegra->xusb_s1p8v_reg);
1595 for (i = 0; i < XUSB_UTMI_COUNT; i++) {
1596 if (BIT(XUSB_UTMI_INDEX + i) & tegra->bdata->portmap) {
1597 struct regulator *reg = tegra->xusb_utmi_vbus_regs[i];
1598 if (!IS_ERR_OR_NULL(reg))
1599 regulator_disable(reg);
1600 tegra->xusb_utmi_vbus_regs[i] = NULL;
1604 regulator_disable(tegra->xusb_s3p3v_reg);
1606 tegra->xusb_s1p05v_reg = NULL;
1607 tegra->xusb_s1p8v_reg = NULL;
1608 tegra->xusb_s3p3v_reg = NULL;
1612 * We need to enable only plle_clk as pllu_clk, utmip_clk and plle_re_vco_clk
1613 * are under hardware control
1615 static int tegra_usb2_clocks_init(struct tegra_xhci_hcd *tegra)
1617 struct platform_device *pdev = tegra->pdev;
1620 tegra->plle_clk = devm_clk_get(&pdev->dev, "pll_e");
1621 if (IS_ERR(tegra->plle_clk)) {
1622 dev_err(&pdev->dev, "%s: Failed to get plle clock\n", __func__);
1623 err = PTR_ERR(tegra->plle_clk);
1626 err = clk_enable(tegra->plle_clk);
1628 dev_err(&pdev->dev, "%s: could not enable plle clock\n",
1636 static void tegra_usb2_clocks_deinit(struct tegra_xhci_hcd *tegra)
1638 clk_disable(tegra->plle_clk);
1639 tegra->plle_clk = NULL;
1642 static int tegra_xusb_partitions_clk_init(struct tegra_xhci_hcd *tegra)
1644 struct platform_device *pdev = tegra->pdev;
1647 tegra->emc_clk = devm_clk_get(&pdev->dev, "emc");
1648 if (IS_ERR(tegra->emc_clk)) {
1649 dev_err(&pdev->dev, "Failed to get xusb.emc clock\n");
1650 return PTR_ERR(tegra->emc_clk);
1653 if (tegra->soc_config->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2) {
1654 tegra->pll_re_vco_clk = devm_clk_get(&pdev->dev, "pll_re_vco");
1655 if (IS_ERR(tegra->pll_re_vco_clk)) {
1656 dev_err(&pdev->dev, "Failed to get refPLLE clock\n");
1657 err = PTR_ERR(tegra->pll_re_vco_clk);
1658 goto get_pll_re_vco_clk_failed;
1662 /* get the clock handle of 120MHz clock source */
1663 tegra->pll_u_480M = devm_clk_get(&pdev->dev, "pll_u_480M");
1664 if (IS_ERR(tegra->pll_u_480M)) {
1665 dev_err(&pdev->dev, "Failed to get pll_u_480M clk handle\n");
1666 err = PTR_ERR(tegra->pll_u_480M);
1667 goto get_pll_u_480M_failed;
1670 /* get the clock handle of 12MHz clock source */
1671 tegra->clk_m = devm_clk_get(&pdev->dev, "clk_m");
1672 if (IS_ERR(tegra->clk_m)) {
1673 dev_err(&pdev->dev, "Failed to get clk_m clk handle\n");
1674 err = PTR_ERR(tegra->clk_m);
1675 goto clk_get_clk_m_failed;
1678 tegra->ss_src_clk = devm_clk_get(&pdev->dev, "ss_src");
1679 if (IS_ERR(tegra->ss_src_clk)) {
1680 dev_err(&pdev->dev, "Failed to get SSPI clk\n");
1681 err = PTR_ERR(tegra->ss_src_clk);
1682 tegra->ss_src_clk = NULL;
1683 goto get_ss_src_clk_failed;
1686 tegra->host_clk = devm_clk_get(&pdev->dev, "host");
1687 if (IS_ERR(tegra->host_clk)) {
1688 dev_err(&pdev->dev, "Failed to get host partition clk\n");
1689 err = PTR_ERR(tegra->host_clk);
1690 tegra->host_clk = NULL;
1691 goto get_host_clk_failed;
1694 tegra->ss_clk = devm_clk_get(&pdev->dev, "ss");
1695 if (IS_ERR(tegra->ss_clk)) {
1696 dev_err(&pdev->dev, "Failed to get ss partition clk\n");
1697 err = PTR_ERR(tegra->ss_clk);
1698 tegra->ss_clk = NULL;
1699 goto get_ss_clk_failed;
1702 if (tegra->soc_config->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2) {
1703 err = clk_enable(tegra->pll_re_vco_clk);
1705 dev_err(&pdev->dev, "Failed to enable refPLLE clk\n");
1706 goto enable_pll_re_vco_clk_failed;
1711 enable_pll_re_vco_clk_failed:
1712 tegra->ss_clk = NULL;
1715 tegra->host_clk = NULL;
1717 get_host_clk_failed:
1718 tegra->ss_src_clk = NULL;
1720 get_ss_src_clk_failed:
1721 tegra->clk_m = NULL;
1723 clk_get_clk_m_failed:
1724 tegra->pll_u_480M = NULL;
1726 get_pll_u_480M_failed:
1727 if (tegra->soc_config->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
1728 tegra->pll_re_vco_clk = NULL;
1730 get_pll_re_vco_clk_failed:
1731 tegra->emc_clk = NULL;
1736 static void tegra_xusb_partitions_clk_deinit(struct tegra_xhci_hcd *tegra)
1738 clk_disable(tegra->ss_clk);
1739 clk_disable(tegra->host_clk);
1740 if (tegra->soc_config->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
1741 clk_disable(tegra->pll_re_vco_clk);
1742 tegra->ss_clk = NULL;
1743 tegra->host_clk = NULL;
1744 tegra->ss_src_clk = NULL;
1745 tegra->clk_m = NULL;
1746 tegra->pll_u_480M = NULL;
1747 if (tegra->soc_config->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
1748 tegra->pll_re_vco_clk = NULL;
1751 static void tegra_xhci_rx_idle_mode_override(struct tegra_xhci_hcd *tegra,
1754 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1757 /* Issue is only applicable for T114 */
1758 if (XUSB_DEVICE_ID_T114 != tegra->device_id)
1761 if (tegra->bdata->portmap & TEGRA_XUSB_SS_P0) {
1762 reg = readl(tegra->padctl_base +
1763 padregs->iophy_misc_pad_p0_ctl3_0);
1765 reg &= ~RX_IDLE_MODE;
1766 reg |= RX_IDLE_MODE_OVRD;
1768 reg |= RX_IDLE_MODE;
1769 reg &= ~RX_IDLE_MODE_OVRD;
1771 writel(reg, tegra->padctl_base +
1772 padregs->iophy_misc_pad_p0_ctl3_0);
1775 if (tegra->bdata->portmap & TEGRA_XUSB_SS_P1) {
1776 reg = readl(tegra->padctl_base +
1777 padregs->iophy_misc_pad_p1_ctl3_0);
1779 reg &= ~RX_IDLE_MODE;
1780 reg |= RX_IDLE_MODE_OVRD;
1782 reg |= RX_IDLE_MODE;
1783 reg &= ~RX_IDLE_MODE_OVRD;
1785 writel(reg, tegra->padctl_base +
1786 padregs->iophy_misc_pad_p1_ctl3_0);
1788 /* SATA lane also if USB3_SS port1 mapped to it */
1789 if (XUSB_DEVICE_ID_T114 != tegra->device_id &&
1790 tegra->bdata->lane_owner & BIT(0)) {
1791 reg = readl(tegra->padctl_base +
1792 padregs->iophy_misc_pad_s0_ctl3_0);
1794 reg &= ~RX_IDLE_MODE;
1795 reg |= RX_IDLE_MODE_OVRD;
1797 reg |= RX_IDLE_MODE;
1798 reg &= ~RX_IDLE_MODE_OVRD;
1800 writel(reg, tegra->padctl_base +
1801 padregs->iophy_misc_pad_s0_ctl3_0);
1806 /* Enable ss clk, host clk, falcon clk,
1807 * fs clk, dev clk, plle and refplle
1811 tegra_xusb_request_clk_rate(struct tegra_xhci_hcd *tegra,
1812 struct clk *clk_handle, u32 rate, u32 *sw_resp)
1815 enum MBOX_CMD_TYPE cmd_ack = MBOX_CMD_ACK;
1816 int fw_req_rate = rate, cur_rate;
1818 /* Do not handle clock change as needed for HS disconnect issue */
1819 if (tegra->soc_config->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2) {
1820 *sw_resp = CMD_DATA(fw_req_rate) | CMD_TYPE(MBOX_CMD_ACK);
1824 /* frequency request from firmware is in KHz.
1828 /* get current rate of clock */
1829 cur_rate = clk_get_rate(clk_handle);
1832 if (fw_req_rate == cur_rate) {
1833 cmd_ack = MBOX_CMD_ACK;
1837 if (clk_handle == tegra->ss_src_clk && fw_req_rate == 12000) {
1838 /* Change SS clock source to CLK_M at 12MHz */
1839 clk_set_parent(clk_handle, tegra->clk_m);
1840 clk_set_rate(clk_handle, fw_req_rate * 1000);
1842 /* save leakage power when SS freq is being decreased */
1843 tegra_xhci_rx_idle_mode_override(tegra, true);
1844 } else if (clk_handle == tegra->ss_src_clk &&
1845 fw_req_rate == 120000) {
1846 /* Change SS clock source to HSIC_480 at 120MHz */
1847 clk_set_rate(clk_handle, 3000 * 1000);
1848 clk_set_parent(clk_handle, tegra->pll_u_480M);
1850 /* clear ovrd bits when SS freq is being increased */
1851 tegra_xhci_rx_idle_mode_override(tegra, false);
1854 cur_rate = (clk_get_rate(clk_handle) / 1000);
1856 if (cur_rate != fw_req_rate) {
1857 xhci_err(tegra->xhci, "cur_rate=%d, fw_req_rate=%d\n",
1858 cur_rate, fw_req_rate);
1859 cmd_ack = MBOX_CMD_NACK;
1862 *sw_resp = CMD_DATA(cur_rate) | CMD_TYPE(cmd_ack);
1866 static void tegra_xusb_set_bw(struct tegra_xhci_hcd *tegra, unsigned int bw)
1868 unsigned int freq_khz;
1870 freq_khz = tegra_emc_bw_to_freq_req(bw);
1871 clk_set_rate(tegra->emc_clk, freq_khz * 1000);
1874 static void tegra_xhci_save_dfe_context(struct tegra_xhci_hcd *tegra,
1877 struct xhci_hcd *xhci = tegra->xhci;
1878 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1882 if (port > (XUSB_SS_PORT_COUNT - 1)) {
1883 pr_err("%s invalid SS port number %u\n", __func__, port);
1887 xhci_info(xhci, "saving restore DFE context for port %d\n", port);
1889 /* if port1 is mapped to SATA lane then read from SATA register */
1890 if (port == 1 && XUSB_DEVICE_ID_T114 != tegra->device_id &&
1891 tegra->bdata->lane_owner & BIT(0))
1892 offset = padregs->iophy_misc_pad_s0_ctl6_0;
1894 offset = MISC_PAD_CTL_6_0(port);
1897 * Value set to IOPHY_MISC_PAD_x_CTL_6 where x P0/P1/S0/ is from,
1898 * T114 refer PG USB3_FW_Programming_Guide_Host.doc section 14.3.10
1899 * T124 refer PG T124_USB3_FW_Programming_Guide_Host.doc section 14.3.10
1901 reg = readl(tegra->padctl_base + offset);
1902 reg &= ~MISC_OUT_SEL(~0);
1903 reg |= MISC_OUT_SEL(0x32);
1904 writel(reg, tegra->padctl_base + offset);
1906 reg = readl(tegra->padctl_base + offset);
1907 tegra->sregs.tap1_val[port] = MISC_OUT_TAP_VAL(reg);
1909 reg = readl(tegra->padctl_base + offset);
1910 reg &= ~MISC_OUT_SEL(~0);
1911 reg |= MISC_OUT_SEL(0x33);
1912 writel(reg, tegra->padctl_base + offset);
1914 reg = readl(tegra->padctl_base + offset);
1915 tegra->sregs.amp_val[port] = MISC_OUT_AMP_VAL(reg);
1917 reg = readl(tegra->padctl_base + USB3_PAD_CTL_4_0(port));
1918 reg &= ~DFE_CNTL_TAP_VAL(~0);
1919 reg |= DFE_CNTL_TAP_VAL(tegra->sregs.tap1_val[port]);
1920 writel(reg, tegra->padctl_base + USB3_PAD_CTL_4_0(port));
1922 reg = readl(tegra->padctl_base + USB3_PAD_CTL_4_0(port));
1923 reg &= ~DFE_CNTL_AMP_VAL(~0);
1924 reg |= DFE_CNTL_AMP_VAL(tegra->sregs.amp_val[port]);
1925 writel(reg, tegra->padctl_base + USB3_PAD_CTL_4_0(port));
1927 tegra->dfe_ctx_saved[port] = true;
1930 static void save_ctle_context(struct tegra_xhci_hcd *tegra,
1933 struct xhci_hcd *xhci = tegra->xhci;
1934 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
1938 if (port > (XUSB_SS_PORT_COUNT - 1)) {
1939 pr_err("%s invalid SS port number %u\n", __func__, port);
1943 xhci_info(xhci, "saving restore CTLE context for port %d\n", port);
1945 /* if port1 is mapped to SATA lane then read from SATA register */
1946 if (port == 1 && XUSB_DEVICE_ID_T114 != tegra->device_id &&
1947 tegra->bdata->lane_owner & BIT(0))
1948 offset = padregs->iophy_misc_pad_s0_ctl6_0;
1950 offset = MISC_PAD_CTL_6_0(port);
1953 * Value set to IOPHY_MISC_PAD_x_CTL_6 where x P0/P1/S0/ is from,
1954 * T114 refer PG USB3_FW_Programming_Guide_Host.doc section 14.3.10
1955 * T124 refer PG T124_USB3_FW_Programming_Guide_Host.doc section 14.3.10
1957 reg = readl(tegra->padctl_base + offset);
1958 reg &= ~MISC_OUT_SEL(~0);
1959 reg |= MISC_OUT_SEL(0xa1);
1960 writel(reg, tegra->padctl_base + offset);
1962 reg = readl(tegra->padctl_base + offset);
1963 reg &= ~MISC_OUT_SEL(~0);
1964 reg |= MISC_OUT_SEL(0x21);
1965 writel(reg, tegra->padctl_base + offset);
1967 reg = readl(tegra->padctl_base + offset);
1968 tegra->sregs.ctle_g_val[port] = MISC_OUT_G_Z_VAL(reg);
1970 reg = readl(tegra->padctl_base + offset);
1971 reg &= ~MISC_OUT_SEL(~0);
1972 reg |= MISC_OUT_SEL(0x48);
1973 writel(reg, tegra->padctl_base + offset);
1975 reg = readl(tegra->padctl_base + offset);
1976 tegra->sregs.ctle_z_val[port] = MISC_OUT_G_Z_VAL(reg);
1978 reg = readl(tegra->padctl_base + USB3_PAD_CTL_2_0(port));
1979 reg &= ~RX_EQ_Z_VAL(~0);
1980 reg |= RX_EQ_Z_VAL(tegra->sregs.ctle_z_val[port]);
1981 writel(reg, tegra->padctl_base + USB3_PAD_CTL_2_0(port));
1983 reg = readl(tegra->padctl_base + USB3_PAD_CTL_2_0(port));
1984 reg &= ~RX_EQ_G_VAL(~0);
1985 reg |= RX_EQ_G_VAL(tegra->sregs.ctle_g_val[port]);
1986 writel(reg, tegra->padctl_base + USB3_PAD_CTL_2_0(port));
1988 tegra->ctle_ctx_saved[port] = true;
1991 static void tegra_xhci_restore_dfe_context(struct tegra_xhci_hcd *tegra,
1994 struct xhci_hcd *xhci = tegra->xhci;
1997 /* don't restore if not saved */
1998 if (tegra->dfe_ctx_saved[port] == false)
2001 xhci_info(xhci, "restoring dfe context of port %d\n", port);
2003 /* restore dfe_cntl for the port */
2004 reg = readl(tegra->padctl_base + USB3_PAD_CTL_4_0(port));
2005 reg &= ~(DFE_CNTL_AMP_VAL(~0) |
2006 DFE_CNTL_TAP_VAL(~0));
2007 reg |= DFE_CNTL_AMP_VAL(tegra->sregs.amp_val[port]) |
2008 DFE_CNTL_TAP_VAL(tegra->sregs.tap1_val[port]);
2009 writel(reg, tegra->padctl_base + USB3_PAD_CTL_4_0(port));
2012 void restore_ctle_context(struct tegra_xhci_hcd *tegra,
2015 struct xhci_hcd *xhci = tegra->xhci;
2018 /* don't restore if not saved */
2019 if (tegra->ctle_ctx_saved[port] == false)
2022 xhci_info(xhci, "restoring CTLE context of port %d\n", port);
2024 /* restore ctle for the port */
2025 reg = readl(tegra->padctl_base + USB3_PAD_CTL_2_0(port));
2026 reg &= ~(RX_EQ_Z_VAL(~0) |
2028 reg |= (RX_EQ_Z_VAL(tegra->sregs.ctle_z_val[port]) |
2029 RX_EQ_G_VAL(tegra->sregs.ctle_g_val[port]));
2030 writel(reg, tegra->padctl_base + USB3_PAD_CTL_2_0(port));
2033 static void tegra_xhci_program_ulpi_pad(struct tegra_xhci_hcd *tegra,
2036 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2039 reg = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
2040 reg &= ~USB2_ULPI_PAD;
2041 reg |= USB2_ULPI_PAD_OWNER_XUSB;
2042 writel(reg, tegra->padctl_base + padregs->usb2_pad_mux_0);
2044 reg = readl(tegra->padctl_base + padregs->usb2_port_cap_0);
2045 reg &= ~USB2_ULPI_PORT_CAP;
2046 reg |= (tegra->bdata->ulpicap << 24);
2047 writel(reg, tegra->padctl_base + padregs->usb2_port_cap_0);
2048 /* FIXME: Program below when more details available
2049 * XUSB_PADCTL_ULPI_LINK_TRIM_CONTROL_0
2050 * XUSB_PADCTL_ULPI_NULL_CLK_TRIM_CONTROL_0
2054 static void tegra_xhci_program_utmip_pad(struct tegra_xhci_hcd *tegra,
2057 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2059 u32 ctl0_offset, ctl1_offset;
2061 reg = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
2062 reg &= ~USB2_OTG_PAD_PORT_MASK(port);
2063 reg |= USB2_OTG_PAD_PORT_OWNER_XUSB(port);
2064 writel(reg, tegra->padctl_base + padregs->usb2_pad_mux_0);
2066 reg = readl(tegra->padctl_base + padregs->usb2_port_cap_0);
2067 reg &= ~USB2_PORT_CAP_MASK(port);
2068 reg |= USB2_PORT_CAP_HOST(port);
2069 writel(reg, tegra->padctl_base + padregs->usb2_port_cap_0);
2072 * Modify only the bits which belongs to the port
2073 * and enable respective VBUS_PAD for the port
2075 if (tegra->bdata->uses_external_pmic == false) {
2076 reg = readl(tegra->padctl_base + padregs->oc_det_0);
2077 reg &= ~(port == 2 ? OC_DET_VBUS_ENABLE2_OC_MAP :
2078 port ? OC_DET_VBUS_ENABLE1_OC_MAP :
2079 OC_DET_VBUS_ENABLE0_OC_MAP);
2081 reg |= (port == 2) ? OC_DET_VBUS_EN2_OC_DETECTED_VBUS_PAD2 :
2082 port ? OC_DET_VBUS_EN1_OC_DETECTED_VBUS_PAD1 :
2083 OC_DET_VBUS_EN0_OC_DETECTED_VBUS_PAD0;
2084 writel(reg, tegra->padctl_base + padregs->oc_det_0);
2087 * enable respective VBUS_PAD if port is mapped to any SS port
2089 reg = readl(tegra->padctl_base + padregs->usb2_oc_map_0);
2090 reg &= ~((port == 2) ? USB2_OC_MAP_PORT2 :
2091 port ? USB2_OC_MAP_PORT1 : USB2_OC_MAP_PORT0);
2092 reg |= (0x4 | port) << (port * 3);
2093 writel(reg, tegra->padctl_base + padregs->usb2_oc_map_0);
2095 ctl0_offset = (port == 2) ? padregs->usb2_otg_pad2_ctl0_0 :
2096 port ? padregs->usb2_otg_pad1_ctl0_0 :
2097 padregs->usb2_otg_pad0_ctl0_0;
2098 ctl1_offset = (port == 2) ? padregs->usb2_otg_pad2_ctl1_0 :
2099 port ? padregs->usb2_otg_pad1_ctl1_0 :
2100 padregs->usb2_otg_pad0_ctl1_0;
2102 reg = readl(tegra->padctl_base + ctl0_offset);
2103 reg &= ~(USB2_OTG_HS_CURR_LVL | USB2_OTG_HS_SLEW |
2104 USB2_OTG_FS_SLEW | USB2_OTG_LS_RSLEW |
2105 USB2_OTG_PD | USB2_OTG_PD2 | USB2_OTG_PD_ZI);
2107 reg |= tegra->soc_config->hs_slew;
2108 reg |= (port == 2) ? tegra->soc_config->ls_rslew_pad2 :
2109 port ? tegra->soc_config->ls_rslew_pad1 :
2110 tegra->soc_config->ls_rslew_pad0;
2111 reg |= (port == 2) ? tegra->cdata->hs_curr_level_pad2 :
2112 port ? tegra->cdata->hs_curr_level_pad1 :
2113 tegra->cdata->hs_curr_level_pad0;
2114 writel(reg, tegra->padctl_base + ctl0_offset);
2116 reg = readl(tegra->padctl_base + ctl1_offset);
2117 reg &= ~(USB2_OTG_TERM_RANGE_AD | USB2_OTG_HS_IREF_CAP
2118 | USB2_OTG_PD_CHRP_FORCE_POWERUP
2119 | USB2_OTG_PD_DISC_FORCE_POWERUP
2121 reg |= (tegra->cdata->hs_iref_cap << 9) |
2122 (tegra->cdata->hs_term_range_adj << 3);
2123 writel(reg, tegra->padctl_base + ctl1_offset);
2125 /*Release OTG port if not in host mode*/
2127 if ((port == 0) && !is_otg_host(tegra))
2128 tegra_xhci_release_otg_port(true);
2131 static inline bool xusb_use_sata_lane(struct tegra_xhci_hcd *tegra)
2133 return ((XUSB_DEVICE_ID_T114 == tegra->device_id) ? false
2134 : ((tegra->bdata->portmap & TEGRA_XUSB_SS_P1)
2135 && (tegra->bdata->lane_owner & BIT(0))));
2138 static void tegra_xhci_program_ss_pad(struct tegra_xhci_hcd *tegra,
2141 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2142 u32 ctl2_offset, ctl4_offset, ctl5_offset;
2145 ctl2_offset = port ? padregs->iophy_usb3_pad1_ctl2_0 :
2146 padregs->iophy_usb3_pad0_ctl2_0;
2147 ctl4_offset = port ? padregs->iophy_usb3_pad1_ctl4_0 :
2148 padregs->iophy_usb3_pad0_ctl4_0;
2149 ctl5_offset = port ? padregs->iophy_misc_pad_p1_ctl5_0 :
2150 padregs->iophy_misc_pad_p0_ctl5_0;
2152 reg = readl(tegra->padctl_base + ctl2_offset);
2153 reg &= ~(IOPHY_USB3_RXWANDER | IOPHY_USB3_RXEQ |
2154 IOPHY_USB3_CDRCNTL);
2155 reg |= tegra->soc_config->rx_wander | tegra->soc_config->rx_eq |
2156 tegra->soc_config->cdr_cntl;
2157 writel(reg, tegra->padctl_base + ctl2_offset);
2159 reg = readl(tegra->padctl_base + ctl4_offset);
2160 reg = tegra->soc_config->dfe_cntl;
2161 writel(reg, tegra->padctl_base + ctl4_offset);
2163 reg = readl(tegra->padctl_base + ctl5_offset);
2165 writel(reg, tegra->padctl_base + ctl5_offset);
2167 reg = readl(tegra->padctl_base + MISC_PAD_CTL_2_0(port));
2168 reg &= ~SPARE_IN(~0);
2169 reg |= SPARE_IN(tegra->soc_config->spare_in);
2170 writel(reg, tegra->padctl_base + MISC_PAD_CTL_2_0(port));
2172 if (xusb_use_sata_lane(tegra)) {
2173 reg = readl(tegra->padctl_base + MISC_PAD_S0_CTL_5_0);
2175 writel(reg, tegra->padctl_base + MISC_PAD_S0_CTL_5_0);
2177 reg = readl(tegra->padctl_base + MISC_PAD_S0_CTL_2_0);
2178 reg &= ~SPARE_IN(~0);
2179 reg |= SPARE_IN(tegra->soc_config->spare_in);
2180 writel(reg, tegra->padctl_base + MISC_PAD_S0_CTL_2_0);
2183 reg = readl(tegra->padctl_base + padregs->ss_port_map_0);
2184 reg &= ~(port ? SS_PORT_MAP_P1 : SS_PORT_MAP_P0);
2185 reg |= (tegra->bdata->ss_portmap &
2186 (port ? TEGRA_XUSB_SS1_PORT_MAP : TEGRA_XUSB_SS0_PORT_MAP));
2187 writel(reg, tegra->padctl_base + padregs->ss_port_map_0);
2189 tegra_xhci_restore_dfe_context(tegra, port);
2190 tegra_xhci_restore_ctle_context(tegra, port);
2193 /* This function assigns the USB ports to the controllers,
2194 * then programs the port capabilities and pad parameters
2195 * of ports assigned to XUSB after booted to OS.
2198 tegra_xhci_padctl_portmap_and_caps(struct tegra_xhci_hcd *tegra)
2200 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2201 u32 reg, oc_bits = 0;
2204 reg = readl(tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
2205 reg &= ~(USB2_BIAS_HS_SQUELCH_LEVEL | USB2_BIAS_HS_DISCON_LEVEL);
2206 reg |= tegra->cdata->hs_squelch_level | tegra->soc_config->hs_disc_lvl;
2207 writel(reg, tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
2209 reg = readl(tegra->padctl_base + padregs->snps_oc_map_0);
2210 reg |= SNPS_OC_MAP_CTRL1 | SNPS_OC_MAP_CTRL2 | SNPS_OC_MAP_CTRL3;
2211 writel(reg, tegra->padctl_base + padregs->snps_oc_map_0);
2212 reg = readl(tegra->padctl_base + padregs->snps_oc_map_0);
2214 reg = readl(tegra->padctl_base + padregs->oc_det_0);
2215 reg |= OC_DET_VBUS_ENABLE0_OC_MAP | OC_DET_VBUS_ENABLE1_OC_MAP;
2216 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
2217 reg |= OC_DET_VBUS_ENABLE2_OC_MAP;
2218 writel(reg, tegra->padctl_base + padregs->oc_det_0);
2220 /* check if over current seen. Clear if present */
2221 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P0)
2222 oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD0;
2223 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P1)
2224 oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD1;
2225 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
2226 oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD2;
2228 reg = readl(tegra->padctl_base + padregs->oc_det_0);
2229 if (reg & oc_bits) {
2230 xhci_info(tegra->xhci, "Over current detected. Clearing...\n");
2231 writel(reg, tegra->padctl_base + padregs->oc_det_0);
2233 usleep_range(100, 200);
2235 reg = readl(tegra->padctl_base + padregs->oc_det_0);
2237 xhci_info(tegra->xhci, "Over current still present\n");
2240 reg = readl(tegra->padctl_base + padregs->usb2_oc_map_0);
2241 reg = USB2_OC_MAP_PORT0 | USB2_OC_MAP_PORT1;
2242 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
2243 reg |= USB2_OC_MAP_PORT2;
2244 writel(reg, tegra->padctl_base + padregs->usb2_oc_map_0);
2246 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P0)
2247 tegra_xhci_program_utmip_pad(tegra, 0);
2248 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P1)
2249 tegra_xhci_program_utmip_pad(tegra, 1);
2250 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
2251 tegra_xhci_program_utmip_pad(tegra, 2);
2253 for_each_enabled_hsic_pad(pad, tegra)
2254 hsic_pad_enable(tegra, pad);
2256 if (tegra->bdata->portmap & TEGRA_XUSB_ULPI_P0)
2257 tegra_xhci_program_ulpi_pad(tegra, 0);
2259 if (tegra->bdata->portmap & TEGRA_XUSB_SS_P0) {
2260 tegra_xhci_program_ss_pad(tegra, 0);
2262 /* set rx_idle_mode_ovrd for unused SS ports to save power */
2263 reg = readl(tegra->padctl_base +
2264 padregs->iophy_misc_pad_p0_ctl3_0);
2265 reg &= ~RX_IDLE_MODE;
2266 reg |= RX_IDLE_MODE_OVRD;
2267 writel(reg, tegra->padctl_base +
2268 padregs->iophy_misc_pad_p0_ctl3_0);
2271 if (tegra->bdata->portmap & TEGRA_XUSB_SS_P1) {
2272 tegra_xhci_program_ss_pad(tegra, 1);
2274 /* set rx_idle_mode_ovrd for unused SS ports to save power */
2275 reg = readl(tegra->padctl_base +
2276 padregs->iophy_misc_pad_p1_ctl3_0);
2277 reg &= ~RX_IDLE_MODE;
2278 reg |= RX_IDLE_MODE_OVRD;
2279 writel(reg, tegra->padctl_base +
2280 padregs->iophy_misc_pad_p1_ctl3_0);
2282 /* SATA lane also if USB3_SS port1 mapped to it but unused */
2283 if (XUSB_DEVICE_ID_T114 != tegra->device_id &&
2284 tegra->bdata->lane_owner & BIT(0)) {
2285 reg = readl(tegra->padctl_base +
2286 padregs->iophy_misc_pad_s0_ctl3_0);
2287 reg &= ~RX_IDLE_MODE;
2288 reg |= RX_IDLE_MODE_OVRD;
2289 writel(reg, tegra->padctl_base +
2290 padregs->iophy_misc_pad_s0_ctl3_0);
2293 if (XUSB_DEVICE_ID_T114 != tegra->device_id) {
2294 tegra_xhci_setup_gpio_for_ss_lane(tegra);
2295 usb3_phy_pad_enable(tegra->bdata->lane_owner);
2299 /* This function read XUSB registers and stores in device context */
2301 tegra_xhci_save_xusb_ctx(struct tegra_xhci_hcd *tegra)
2304 /* a. Save the IPFS registers */
2305 tegra->sregs.msi_bar_sz =
2306 readl(tegra->ipfs_base + IPFS_XUSB_HOST_MSI_BAR_SZ_0);
2308 tegra->sregs.msi_axi_barst =
2309 readl(tegra->ipfs_base + IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0);
2311 tegra->sregs.msi_fpci_barst =
2312 readl(tegra->ipfs_base + IPFS_XUSB_HOST_FPCI_BAR_ST_0);
2314 tegra->sregs.msi_vec0 =
2315 readl(tegra->ipfs_base + IPFS_XUSB_HOST_MSI_VEC0_0);
2317 tegra->sregs.msi_en_vec0 =
2318 readl(tegra->ipfs_base + IPFS_XUSB_HOST_MSI_EN_VEC0_0);
2320 tegra->sregs.fpci_error_masks =
2321 readl(tegra->ipfs_base + IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0);
2323 tegra->sregs.intr_mask =
2324 readl(tegra->ipfs_base + IPFS_XUSB_HOST_INTR_MASK_0);
2326 tegra->sregs.ipfs_intr_enable =
2327 readl(tegra->ipfs_base + IPFS_XUSB_HOST_IPFS_INTR_ENABLE_0);
2329 tegra->sregs.ufpci_config =
2330 readl(tegra->ipfs_base + IPFS_XUSB_HOST_UFPCI_CONFIG_0);
2332 tegra->sregs.clkgate_hysteresis =
2333 readl(tegra->ipfs_base + IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
2335 tegra->sregs.xusb_host_mccif_fifo_cntrl =
2336 readl(tegra->ipfs_base + IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0);
2338 /* b. Save the CFG registers */
2340 tegra->sregs.hs_pls =
2341 readl(tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HS_PLS);
2343 tegra->sregs.fs_pls =
2344 readl(tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_FS_PLS);
2346 tegra->sregs.hs_fs_speed =
2347 readl(tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HSFS_SPEED);
2349 tegra->sregs.hs_fs_pp =
2350 readl(tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HSFS_PP);
2352 tegra->sregs.cfg_aru =
2353 readl(tegra->fpci_base + XUSB_CFG_ARU_CONTEXT);
2355 tegra->sregs.cfg_order =
2356 readl(tegra->fpci_base + XUSB_CFG_FPCICFG);
2358 tegra->sregs.cfg_fladj =
2359 readl(tegra->fpci_base + XUSB_CFG_24);
2361 tegra->sregs.cfg_sid =
2362 readl(tegra->fpci_base + XUSB_CFG_16);
2365 /* This function restores XUSB registers from device context */
2367 tegra_xhci_restore_ctx(struct tegra_xhci_hcd *tegra)
2369 /* Restore Cfg registers */
2370 writel(tegra->sregs.hs_pls,
2371 tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HS_PLS);
2373 writel(tegra->sregs.fs_pls,
2374 tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_FS_PLS);
2376 writel(tegra->sregs.hs_fs_speed,
2377 tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HSFS_SPEED);
2379 writel(tegra->sregs.hs_fs_pp,
2380 tegra->fpci_base + XUSB_CFG_ARU_CONTEXT_HSFS_PP);
2382 writel(tegra->sregs.cfg_aru,
2383 tegra->fpci_base + XUSB_CFG_ARU_CONTEXT);
2385 writel(tegra->sregs.cfg_order,
2386 tegra->fpci_base + XUSB_CFG_FPCICFG);
2388 writel(tegra->sregs.cfg_fladj,
2389 tegra->fpci_base + XUSB_CFG_24);
2391 writel(tegra->sregs.cfg_sid,
2392 tegra->fpci_base + XUSB_CFG_16);
2394 /* Restore IPFS registers */
2396 writel(tegra->sregs.msi_bar_sz,
2397 tegra->ipfs_base + IPFS_XUSB_HOST_MSI_BAR_SZ_0);
2399 writel(tegra->sregs.msi_axi_barst,
2400 tegra->ipfs_base + IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0);
2402 writel(tegra->sregs.msi_fpci_barst,
2403 tegra->ipfs_base + IPFS_XUSB_HOST_FPCI_BAR_ST_0);
2405 writel(tegra->sregs.msi_vec0,
2406 tegra->ipfs_base + IPFS_XUSB_HOST_MSI_VEC0_0);
2408 writel(tegra->sregs.msi_en_vec0,
2409 tegra->ipfs_base + IPFS_XUSB_HOST_MSI_EN_VEC0_0);
2411 writel(tegra->sregs.fpci_error_masks,
2412 tegra->ipfs_base + IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0);
2414 writel(tegra->sregs.intr_mask,
2415 tegra->ipfs_base + IPFS_XUSB_HOST_INTR_MASK_0);
2417 writel(tegra->sregs.ipfs_intr_enable,
2418 tegra->ipfs_base + IPFS_XUSB_HOST_IPFS_INTR_ENABLE_0);
2420 writel(tegra->sregs.ufpci_config,
2421 tegra->fpci_base + IPFS_XUSB_HOST_UFPCI_CONFIG_0);
2423 writel(tegra->sregs.clkgate_hysteresis,
2424 tegra->ipfs_base + IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
2426 writel(tegra->sregs.xusb_host_mccif_fifo_cntrl,
2427 tegra->ipfs_base + IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0);
2430 static void tegra_xhci_enable_fw_message(struct tegra_xhci_hcd *tegra)
2432 fw_message_send(tegra, MBOX_CMD_MSG_ENABLED, 0 /* no data needed */);
2435 static int load_firmware(struct tegra_xhci_hcd *tegra, bool resetARU)
2437 struct platform_device *pdev = tegra->pdev;
2438 struct cfgtbl *cfg_tbl = (struct cfgtbl *) tegra->firmware.data;
2445 u32 usbsts, count = 0xff;
2446 struct xhci_cap_regs __iomem *cap_regs;
2447 struct xhci_op_regs __iomem *op_regs;
2449 /* enable mbox interrupt */
2450 writel(readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD) | MBOX_INT_EN,
2451 tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
2453 /* First thing, reset the ARU. By the time we get to
2454 * loading boot code below, reset would be complete.
2455 * alternatively we can busy wait on rst pending bit.
2457 /* Don't reset during ELPG/LP0 exit path */
2459 iowrite32(0x1, tegra->fpci_base + XUSB_CFG_ARU_RST);
2460 usleep_range(1000, 2000);
2463 if (csb_read(tegra, XUSB_CSB_MP_ILOAD_BASE_LO) != 0) {
2464 dev_info(&pdev->dev, "Firmware already loaded, Falcon state 0x%x\n",
2465 csb_read(tegra, XUSB_FALC_CPUCTL));
2469 /* update the phys_log_buffer and total_entries here */
2470 cfg_tbl->phys_addr_log_buffer = tegra->log.phys_addr;
2471 cfg_tbl->total_log_entries = FW_LOG_COUNT;
2473 phys_addr_lo = tegra->firmware.dma;
2474 phys_addr_lo += sizeof(struct cfgtbl);
2476 /* Program the size of DFI into ILOAD_ATTR */
2477 csb_write(tegra, XUSB_CSB_MP_ILOAD_ATTR, tegra->firmware.size);
2479 /* Boot code of the firmware reads the ILOAD_BASE_LO register
2480 * to get to the start of the dfi in system memory.
2482 csb_write(tegra, XUSB_CSB_MP_ILOAD_BASE_LO, phys_addr_lo);
2484 /* Program the ILOAD_BASE_HI with a value of MSB 32 bits */
2485 csb_write(tegra, XUSB_CSB_MP_ILOAD_BASE_HI, 0);
2487 /* Set BOOTPATH to 1 in APMAP Register. Bit 31 is APMAP_BOOTMAP */
2488 csb_write(tegra, XUSB_CSB_MP_APMAP, APMAP_BOOTPATH);
2490 /* Invalidate L2IMEM. */
2491 csb_write(tegra, XUSB_CSB_MP_L2IMEMOP_TRIG, L2IMEM_INVALIDATE_ALL);
2493 /* Initiate fetch of Bootcode from system memory into L2IMEM.
2494 * Program BootCode location and size in system memory.
2496 HwReg = ((cfg_tbl->boot_codetag / IMEM_BLOCK_SIZE) &
2497 L2IMEMOP_SIZE_SRC_OFFSET_MASK)
2498 << L2IMEMOP_SIZE_SRC_OFFSET_SHIFT;
2499 HwReg |= ((cfg_tbl->boot_codesize / IMEM_BLOCK_SIZE) &
2500 L2IMEMOP_SIZE_SRC_COUNT_MASK)
2501 << L2IMEMOP_SIZE_SRC_COUNT_SHIFT;
2502 csb_write(tegra, XUSB_CSB_MP_L2IMEMOP_SIZE, HwReg);
2504 /* Trigger L2IMEM Load operation. */
2505 csb_write(tegra, XUSB_CSB_MP_L2IMEMOP_TRIG, L2IMEM_LOAD_LOCKED_RESULT);
2507 /* Setup Falcon Auto-fill */
2508 nblocks = (cfg_tbl->boot_codesize / IMEM_BLOCK_SIZE);
2509 if ((cfg_tbl->boot_codesize % IMEM_BLOCK_SIZE) != 0)
2511 csb_write(tegra, XUSB_FALC_IMFILLCTL, nblocks);
2513 HwReg = (cfg_tbl->boot_codetag / IMEM_BLOCK_SIZE) & IMFILLRNG_TAG_MASK;
2514 HwReg |= (((cfg_tbl->boot_codetag + cfg_tbl->boot_codesize)
2515 /IMEM_BLOCK_SIZE) - 1) << IMFILLRNG1_TAG_HI_SHIFT;
2516 csb_write(tegra, XUSB_FALC_IMFILLRNG1, HwReg);
2518 csb_write(tegra, XUSB_FALC_DMACTL, 0);
2521 csb_write(tegra, XUSB_FALC_BOOTVEC, cfg_tbl->boot_codetag);
2523 /* Start Falcon CPU */
2524 csb_write(tegra, XUSB_FALC_CPUCTL, CPUCTL_STARTCPU);
2525 usleep_range(1000, 2000);
2527 fw_time = cfg_tbl->fwimg_created_time;
2528 time_to_tm(fw_time, 0, &fw_tm);
2529 dev_info(&pdev->dev,
2530 "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC, "\
2531 "Falcon state 0x%x\n", fw_tm.tm_year + 1900,
2532 fw_tm.tm_mon + 1, fw_tm.tm_mday, fw_tm.tm_hour,
2533 fw_tm.tm_min, fw_tm.tm_sec,
2534 csb_read(tegra, XUSB_FALC_CPUCTL));
2536 dev_dbg(&pdev->dev, "num_hsic_port %d\n", cfg_tbl->num_hsic_port);
2538 /* return fail if firmware status is not good */
2539 if (csb_read(tegra, XUSB_FALC_CPUCTL) == XUSB_FALC_STATE_HALTED)
2542 cap_regs = IO_ADDRESS(tegra->host_phy_base);
2543 hc_caplength = HC_LENGTH(ioread32(&cap_regs->hc_capbase));
2544 op_regs = IO_ADDRESS(tegra->host_phy_base + hc_caplength);
2546 /* wait for USBSTS_CNR to get set */
2548 usbsts = ioread32(&op_regs->status);
2549 } while ((usbsts & STS_CNR) && count--);
2551 if (!count && (usbsts & STS_CNR)) {
2552 dev_err(&pdev->dev, "Controller not ready\n");
2559 static void tegra_xhci_release_port_ownership(struct tegra_xhci_hcd *tegra,
2562 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2565 /* Issue is only applicable for T114 */
2566 if (XUSB_DEVICE_ID_T114 != tegra->device_id)
2569 reg = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
2570 reg &= ~(USB2_OTG_PAD_PORT_MASK(0) | USB2_OTG_PAD_PORT_MASK(1) |
2571 USB2_OTG_PAD_PORT_MASK(2));
2574 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P0)
2575 if (is_otg_host(tegra))
2576 reg |= USB2_OTG_PAD_PORT_OWNER_XUSB(0);
2577 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P1)
2578 reg |= USB2_OTG_PAD_PORT_OWNER_XUSB(1);
2579 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
2580 reg |= USB2_OTG_PAD_PORT_OWNER_XUSB(2);
2583 writel(reg, tegra->padctl_base + padregs->usb2_pad_mux_0);
2585 /* SS ELPG Entry initiated by fw */
2586 static int tegra_xhci_ss_elpg_entry(struct tegra_xhci_hcd *tegra)
2588 struct xhci_hcd *xhci = tegra->xhci;
2591 must_have_sync_lock(tegra);
2593 /* update maximum BW requirement to 0 */
2594 tegra_xusb_set_bw(tegra, 0);
2596 /* This is SS partition ELPG entry
2597 * STEP 0: firmware will set WOC WOD bits in PVTPORTSC2 regs.
2600 /* Step 0: Acquire mbox and send PWRGATE msg to firmware
2601 * only if it is sw initiated one
2604 /* STEP 1: xHCI firmware and xHCIPEP driver communicates
2605 * SuperSpeed partition ELPG entry via mailbox protocol
2608 /* STEP 2: xHCI PEP driver and XUSB device mode driver
2609 * enable the XUSB wakeup interrupts for the SuperSpeed
2610 * and USB2.0 ports assigned to host.Section 4.1 Step 3
2612 tegra_xhci_ss_wake_on_interrupts(tegra->bdata->portmap, true);
2614 /* STEP 3: xHCI PEP driver initiates the signal sequence
2615 * to enable the XUSB SSwake detection logic for the
2616 * SuperSpeed ports assigned to host.Section 4.1 Step 4
2618 tegra_xhci_ss_wake_signal(tegra->bdata->portmap, true);
2620 /* STEP 4: System Power Management driver asserts reset
2621 * to XUSB SuperSpeed partition then disables its clocks
2623 tegra_periph_reset_assert(tegra->ss_clk);
2624 clk_disable(tegra->ss_clk);
2626 usleep_range(100, 200);
2628 /* STEP 5: System Power Management driver disables the
2629 * XUSB SuperSpeed partition power rails.
2631 debug_print_portsc(xhci);
2633 /* tegra_powergate_partition also does partition reset assert */
2634 ret = tegra_powergate_partition(TEGRA_POWERGATE_XUSBA);
2636 xhci_err(xhci, "%s: could not powergate xusba partition\n",
2638 /* TODO: error recovery? */
2640 tegra->ss_pwr_gated = true;
2642 /* STEP 6: xHCI PEP driver initiates the signal sequence
2643 * to enable the XUSB SSwake detection logic for the
2644 * SuperSpeed ports assigned to host.Section 4.1 Step 7
2646 tegra_xhci_ss_vcore(tegra->bdata->portmap, true);
2651 /* Host ELPG Entry */
2652 static int tegra_xhci_host_elpg_entry(struct tegra_xhci_hcd *tegra)
2654 struct xhci_hcd *xhci = tegra->xhci;
2657 must_have_sync_lock(tegra);
2659 /* If ss is already powergated skip ss ctx save stuff */
2660 if (tegra->ss_pwr_gated) {
2661 xhci_info(xhci, "%s: SS partition is already powergated\n",
2664 ret = tegra_xhci_ss_elpg_entry(tegra);
2666 xhci_err(xhci, "%s: ss_elpg_entry failed %d\n",
2672 /* 1. IS INTR PENDING INT_PENDING=1 ? */
2674 /* STEP 1.1: Do a context save of XUSB and IPFS registers */
2675 tegra_xhci_save_xusb_ctx(tegra);
2677 /* calculate rctrl_val and tctrl_val */
2678 tegra_xhci_war_for_tctrl_rctrl(tegra);
2680 pmc_setup_wake_detect(tegra);
2682 tegra_xhci_hs_wake_on_interrupts(tegra->bdata->portmap, true);
2683 xhci_dbg(xhci, "%s: PMC_UTMIP_UHSIC_SLEEP_CFG_0 = %x\n", __func__,
2684 tegra_usb_pmc_reg_read(PMC_UTMIP_UHSIC_SLEEP_CFG_0));
2686 /* tegra_powergate_partition also does partition reset assert */
2687 ret = tegra_powergate_partition(TEGRA_POWERGATE_XUSBC);
2689 xhci_err(xhci, "%s: could not unpowergate xusbc partition %d\n",
2691 /* TODO: error handling? */
2694 tegra->host_pwr_gated = true;
2695 clk_disable(tegra->host_clk);
2697 if (tegra->soc_config->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
2698 clk_disable(tegra->pll_re_vco_clk);
2699 clk_disable(tegra->emc_clk);
2700 /* set port ownership to SNPS */
2701 tegra_xhci_release_port_ownership(tegra, true);
2703 xhci_dbg(xhci, "%s: PMC_UTMIP_UHSIC_SLEEP_CFG_0 = %x\n", __func__,
2704 tegra_usb_pmc_reg_read(PMC_UTMIP_UHSIC_SLEEP_CFG_0));
2706 xhci_info(xhci, "%s: elpg_entry: completed\n", __func__);
2707 xhci_dbg(xhci, "%s: HOST POWER STATUS = %d\n",
2708 __func__, tegra_powergate_is_powered(TEGRA_POWERGATE_XUSBC));
2712 /* SS ELPG Exit triggered by PADCTL irq */
2714 * tegra_xhci_ss_partition_elpg_exit - bring XUSBA partition out from elpg
2716 * This function must be called with tegra->sync_lock acquired.
2718 * @tegra: xhci controller context
2719 * @return 0 for success, or error numbers
2721 static int tegra_xhci_ss_partition_elpg_exit(struct tegra_xhci_hcd *tegra)
2723 struct xhci_hcd *xhci = tegra->xhci;
2726 must_have_sync_lock(tegra);
2728 if (tegra->ss_pwr_gated && (tegra->ss_wake_event ||
2729 tegra->hs_wake_event || tegra->host_resume_req)) {
2732 * PWR_UNGATE SS partition. XUSBA
2733 * tegra_unpowergate_partition also does partition reset
2736 ret = tegra_unpowergate_partition(TEGRA_POWERGATE_XUSBA);
2739 "%s: could not unpowergate xusba partition %d\n",
2743 if (tegra->ss_wake_event)
2744 tegra->ss_wake_event = false;
2747 xhci_info(xhci, "%s: ss already power gated\n",
2752 /* Step 3: Enable clock to ss partition */
2753 clk_enable(tegra->ss_clk);
2755 /* Step 4: Disable ss wake detection logic */
2756 tegra_xhci_ss_wake_on_interrupts(tegra->bdata->portmap, false);
2758 /* Step 4.1: Disable ss wake detection logic */
2759 tegra_xhci_ss_vcore(tegra->bdata->portmap, false);
2762 usleep_range(150, 200);
2764 /* Step 4.2: Disable ss wake detection logic */
2765 tegra_xhci_ss_wake_signal(tegra->bdata->portmap, false);
2767 /* Step 6 Deassert reset for ss clks */
2768 tegra_periph_reset_deassert(tegra->ss_clk);
2770 xhci_dbg(xhci, "%s: SS ELPG EXIT. ALL DONE\n", __func__);
2771 tegra->ss_pwr_gated = false;
2776 static void ss_partition_elpg_exit_work(struct work_struct *work)
2778 struct tegra_xhci_hcd *tegra = container_of(work, struct tegra_xhci_hcd,
2781 mutex_lock(&tegra->sync_lock);
2782 tegra_xhci_ss_partition_elpg_exit(tegra);
2783 mutex_unlock(&tegra->sync_lock);
2786 /* read pmc WAKE2_STATUS register to know if SS port caused remote wake */
2787 static void update_remote_wakeup_ports(struct tegra_xhci_hcd *tegra)
2789 struct xhci_hcd *xhci = tegra->xhci;
2793 #define PMC_WAKE2_STATUS 0x168
2794 #define PADCTL_WAKE (1 << (58 - 32)) /* PADCTL is WAKE#58 */
2796 wake2_status = tegra_usb_pmc_reg_read(PMC_WAKE2_STATUS);
2798 if (wake2_status & PADCTL_WAKE) {
2799 /* FIXME: This is customized for Dalmore, find a generic way */
2800 set_bit(0, &tegra->usb3_rh_remote_wakeup_ports);
2801 /* clear wake status */
2802 tegra_usb_pmc_reg_write(PMC_WAKE2_STATUS, PADCTL_WAKE);
2805 /* set all usb2 ports with RESUME link state as wakup ports */
2806 for (port = 0; port < xhci->num_usb2_ports; port++) {
2807 u32 portsc = xhci_readl(xhci, xhci->usb2_ports[port]);
2808 if ((portsc & PORT_PLS_MASK) == XDEV_RESUME)
2809 set_bit(port, &tegra->usb2_rh_remote_wakeup_ports);
2812 xhci_dbg(xhci, "%s: usb2 roothub remote_wakeup_ports 0x%lx\n",
2813 __func__, tegra->usb2_rh_remote_wakeup_ports);
2814 xhci_dbg(xhci, "%s: usb3 roothub remote_wakeup_ports 0x%lx\n",
2815 __func__, tegra->usb3_rh_remote_wakeup_ports);
2818 static void wait_remote_wakeup_ports(struct usb_hcd *hcd)
2820 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2821 struct tegra_xhci_hcd *tegra = hcd_to_tegra_xhci(hcd);
2822 int port, num_ports;
2823 unsigned long *remote_wakeup_ports;
2825 __le32 __iomem **port_array;
2827 unsigned int retry = 64;
2828 struct xhci_bus_state *bus_state;
2830 bus_state = &xhci->bus_state[hcd_index(hcd)];
2832 if (hcd == xhci->shared_hcd) {
2833 port_array = xhci->usb3_ports;
2834 num_ports = xhci->num_usb3_ports;
2835 remote_wakeup_ports = &tegra->usb3_rh_remote_wakeup_ports;
2836 rh = "usb3 roothub";
2838 port_array = xhci->usb2_ports;
2839 num_ports = xhci->num_usb2_ports;
2840 remote_wakeup_ports = &tegra->usb2_rh_remote_wakeup_ports;
2841 rh = "usb2 roothub";
2844 while (*remote_wakeup_ports && retry--) {
2845 for_each_set_bit(port, remote_wakeup_ports, num_ports) {
2848 portsc = xhci_readl(xhci, port_array[port]);
2850 if (!(portsc & PORT_CONNECT)) {
2851 /* nothing to do if already disconnected */
2852 clear_bit(port, remote_wakeup_ports);
2856 if (hcd == xhci->shared_hcd) {
2858 (portsc & PORT_PLS_MASK) == XDEV_U0;
2860 unsigned long flags;
2862 spin_lock_irqsave(&xhci->lock, flags);
2864 test_bit(port, &bus_state->resuming_ports);
2865 spin_unlock_irqrestore(&xhci->lock, flags);
2869 clear_bit(port, remote_wakeup_ports);
2871 xhci_dbg(xhci, "%s: %s port %d status 0x%x\n",
2872 __func__, rh, port, portsc);
2875 if (*remote_wakeup_ports)
2876 msleep(20); /* give some time, irq will direct U0 */
2879 xhci_dbg(xhci, "%s: %s remote_wakeup_ports 0x%lx\n", __func__, rh,
2880 *remote_wakeup_ports);
2883 static void tegra_xhci_war_for_tctrl_rctrl(struct tegra_xhci_hcd *tegra)
2885 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2886 u32 reg, utmip_rctrl_val, utmip_tctrl_val, pad_mux, portmux, portowner;
2888 portmux = USB2_OTG_PAD_PORT_MASK(0) | USB2_OTG_PAD_PORT_MASK(1);
2889 portowner = USB2_OTG_PAD_PORT_OWNER_XUSB(0) |
2890 USB2_OTG_PAD_PORT_OWNER_XUSB(1);
2892 if (XUSB_DEVICE_ID_T114 != tegra->device_id) {
2893 portmux |= USB2_OTG_PAD_PORT_MASK(2);
2894 portowner |= USB2_OTG_PAD_PORT_OWNER_XUSB(2);
2897 /* Use xusb padctl space only when xusb owns all UTMIP port */
2898 pad_mux = readl(tegra->padctl_base + padregs->usb2_pad_mux_0);
2899 if ((pad_mux & portmux) == portowner) {
2900 /* XUSB_PADCTL_USB2_BIAS_PAD_CTL_0_0::PD = 0 and
2901 * XUSB_PADCTL_USB2_BIAS_PAD_CTL_0_0::PD_TRK = 0
2903 reg = readl(tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
2904 reg &= ~((1 << 12) | (1 << 13));
2905 writel(reg, tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
2908 usleep_range(20, 30);
2910 /* Read XUSB_PADCTL:: XUSB_PADCTL_USB2_BIAS_PAD_CTL_1_0
2911 * :: TCTRL and RCTRL
2913 reg = readl(tegra->padctl_base + padregs->usb2_bias_pad_ctl1_0);
2914 utmip_rctrl_val = RCTRL(reg);
2915 utmip_tctrl_val = TCTRL(reg);
2918 * tctrl_val = 0x1f - (16 - ffz(utmip_tctrl_val)
2919 * rctrl_val = 0x1f - (16 - ffz(utmip_rctrl_val)
2921 utmip_rctrl_val = 0xf + ffz(utmip_rctrl_val);
2922 utmip_tctrl_val = 0xf + ffz(utmip_tctrl_val);
2923 utmi_phy_update_trking_data(utmip_tctrl_val, utmip_rctrl_val);
2924 xhci_dbg(tegra->xhci, "rctrl_val = 0x%x, tctrl_val = 0x%x\n",
2925 utmip_rctrl_val, utmip_tctrl_val);
2927 /* XUSB_PADCTL_USB2_BIAS_PAD_CTL_0_0::PD = 1 and
2928 * XUSB_PADCTL_USB2_BIAS_PAD_CTL_0_0::PD_TRK = 1
2930 reg = readl(tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
2932 writel(reg, tegra->padctl_base + padregs->usb2_bias_pad_ctl0_0);
2934 /* Program these values into PMC regiseter and program the
2937 reg = PMC_TCTRL_VAL(utmip_tctrl_val) |
2938 PMC_RCTRL_VAL(utmip_rctrl_val);
2939 tegra_usb_pmc_reg_update(PMC_UTMIP_TERM_PAD_CFG,
2941 reg = UTMIP_RCTRL_USE_PMC_P2 | UTMIP_TCTRL_USE_PMC_P2;
2942 tegra_usb_pmc_reg_update(PMC_SLEEP_CFG, reg, reg);
2944 /* Use common PMC API to use SNPS register space */
2945 utmi_phy_set_snps_trking_data();
2949 /* Host ELPG Exit triggered by PADCTL irq */
2951 * tegra_xhci_host_partition_elpg_exit - bring XUSBC partition out from elpg
2953 * This function must be called with tegra->sync_lock acquired.
2955 * @tegra: xhci controller context
2956 * @return 0 for success, or error numbers
2959 tegra_xhci_host_partition_elpg_exit(struct tegra_xhci_hcd *tegra)
2961 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
2962 struct xhci_hcd *xhci = tegra->xhci;
2965 must_have_sync_lock(tegra);
2967 if (!tegra->hc_in_elpg)
2970 clk_enable(tegra->emc_clk);
2971 if (tegra->soc_config->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
2972 clk_enable(tegra->pll_re_vco_clk);
2974 if (tegra->lp0_exit) {
2975 u32 reg, oc_bits = 0;
2977 /* Issue is only applicable for T114 */
2978 if (XUSB_DEVICE_ID_T114 == tegra->device_id)
2979 tegra_xhci_war_for_tctrl_rctrl(tegra);
2980 /* check if over current seen. Clear if present */
2981 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P0)
2982 oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD0;
2983 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P1)
2984 oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD1;
2985 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P2)
2986 oc_bits |= OC_DET_OC_DETECTED_VBUS_PAD2;
2988 reg = readl(tegra->padctl_base + padregs->oc_det_0);
2989 xhci_dbg(xhci, "%s: oc_det_0=0x%x\n", __func__, reg);
2990 if (reg & oc_bits) {
2991 xhci_info(xhci, "Over current detected. Clearing...\n");
2992 writel(reg, tegra->padctl_base + padregs->oc_det_0);
2994 usleep_range(100, 200);
2996 reg = readl(tegra->padctl_base + padregs->oc_det_0);
2998 xhci_info(xhci, "Over current still present\n");
3000 tegra_xhci_padctl_portmap_and_caps(tegra);
3001 /* release clamps post deassert */
3002 tegra->lp0_exit = false;
3005 /* Clear FLUSH_ENABLE of MC client */
3006 tegra_powergate_mc_flush_done(TEGRA_POWERGATE_XUSBC);
3008 /* set port ownership back to xusb */
3009 tegra_xhci_release_port_ownership(tegra, false);
3012 * PWR_UNGATE Host partition. XUSBC
3013 * tegra_unpowergate_partition also does partition reset deassert
3015 ret = tegra_unpowergate_partition(TEGRA_POWERGATE_XUSBC);
3017 xhci_err(xhci, "%s: could not unpowergate xusbc partition %d\n",
3021 clk_enable(tegra->host_clk);
3023 /* Step 4: Deassert reset to host partition clk */
3024 tegra_periph_reset_deassert(tegra->host_clk);
3026 /* Step 6.1: IPFS and XUSB BAR initialization */
3027 tegra_xhci_cfg(tegra);
3029 /* Step 6.2: IPFS and XUSB related restore */
3030 tegra_xhci_restore_ctx(tegra);
3032 /* Step 8: xhci spec related ctx restore
3033 * will be done in xhci_resume().Do it here.
3036 tegra_xhci_ss_partition_elpg_exit(tegra);
3038 /* Change SS clock source to HSIC_480 and set ss_src_clk at 120MHz */
3039 if (clk_get_rate(tegra->ss_src_clk) == 12000000) {
3040 clk_set_rate(tegra->ss_src_clk, 3000 * 1000);
3041 clk_set_parent(tegra->ss_src_clk, tegra->pll_u_480M);
3044 /* clear ovrd bits */
3045 tegra_xhci_rx_idle_mode_override(tegra, false);
3048 xhci_dbg(xhci, "%s: elpg_exit: loading firmware from pmc.\n"
3049 "ss (p1=0x%x, p2=0x%x, p3=0x%x), "
3050 "hs (p1=0x%x, p2=0x%x, p3=0x%x),\n"
3051 "fs (p1=0x%x, p2=0x%x, p3=0x%x)\n",
3053 csb_read(tegra, XUSB_FALC_SS_PVTPORTSC1),
3054 csb_read(tegra, XUSB_FALC_SS_PVTPORTSC2),
3055 csb_read(tegra, XUSB_FALC_SS_PVTPORTSC3),
3056 csb_read(tegra, XUSB_FALC_HS_PVTPORTSC1),
3057 csb_read(tegra, XUSB_FALC_HS_PVTPORTSC2),
3058 csb_read(tegra, XUSB_FALC_HS_PVTPORTSC3),
3059 csb_read(tegra, XUSB_FALC_FS_PVTPORTSC1),
3060 csb_read(tegra, XUSB_FALC_FS_PVTPORTSC2),
3061 csb_read(tegra, XUSB_FALC_FS_PVTPORTSC3));
3062 debug_print_portsc(xhci);
3064 ret = load_firmware(tegra, false /* EPLG exit, do not reset ARU */);
3066 xhci_err(xhci, "%s: failed to load firmware %d\n",
3071 pmc_disable_bus_ctrl(tegra);
3073 tegra->hc_in_elpg = false;
3074 ret = xhci_resume(tegra->xhci, 0);
3076 xhci_err(xhci, "%s: could not resume right %d\n",
3081 update_remote_wakeup_ports(tegra);
3083 if (tegra->hs_wake_event)
3084 tegra->hs_wake_event = false;
3086 if (tegra->host_resume_req)
3087 tegra->host_resume_req = false;
3089 xhci_info(xhci, "elpg_exit: completed: lp0/elpg time=%d msec\n",
3090 jiffies_to_msecs(jiffies - tegra->last_jiffies));
3092 tegra->host_pwr_gated = false;
3097 static void host_partition_elpg_exit_work(struct work_struct *work)
3099 struct tegra_xhci_hcd *tegra = container_of(work, struct tegra_xhci_hcd,
3100 host_elpg_exit_work);
3102 mutex_lock(&tegra->sync_lock);
3103 tegra_xhci_host_partition_elpg_exit(tegra);
3104 mutex_unlock(&tegra->sync_lock);
3107 /* Mailbox handling function. This function handles requests
3108 * from firmware and communicates with clock and powergating
3109 * module to alter clock rates and to power gate/ungate xusb
3112 * Following is the structure of mailbox messages.
3113 * bit 31:28 - msg type
3114 * bits 27:0 - mbox data
3115 * FIXME: Check if we can just call clock functions like below
3116 * or should we schedule it for calling later ?
3120 tegra_xhci_process_mbox_message(struct work_struct *work)
3122 u32 sw_resp = 0, cmd, data_in, fw_msg;
3124 struct tegra_xhci_hcd *tegra = container_of(work, struct tegra_xhci_hcd,
3126 struct xhci_hcd *xhci = tegra->xhci;
3128 unsigned long ports;
3130 mutex_lock(&tegra->mbox_lock);
3132 /* get the mbox message from firmware */
3133 fw_msg = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_DATA_OUT);
3135 data_in = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_DATA_IN);
3137 dev_warn(&tegra->pdev->dev, "%s data_in 0x%x\n",
3139 mutex_unlock(&tegra->mbox_lock);
3143 /* get cmd type and cmd data */
3144 tegra->cmd_type = (fw_msg >> CMD_TYPE_SHIFT) & CMD_TYPE_MASK;
3145 tegra->cmd_data = (fw_msg >> CMD_DATA_SHIFT) & CMD_DATA_MASK;
3147 /* decode the message and make appropriate requests to
3148 * clock or powergating module.
3151 switch (tegra->cmd_type) {
3152 case MBOX_CMD_INC_FALC_CLOCK:
3153 case MBOX_CMD_DEC_FALC_CLOCK:
3154 ret = tegra_xusb_request_clk_rate(
3160 xhci_err(xhci, "%s: could not set required falc rate\n",
3162 goto send_sw_response;
3163 case MBOX_CMD_INC_SSPI_CLOCK:
3164 case MBOX_CMD_DEC_SSPI_CLOCK:
3165 ret = tegra_xusb_request_clk_rate(
3171 xhci_err(xhci, "%s: could not set required ss rate.\n",
3173 goto send_sw_response;
3175 case MBOX_CMD_SET_BW:
3176 /* fw sends BW request in MByte/sec */
3177 mutex_lock(&tegra->sync_lock);
3178 tegra_xusb_set_bw(tegra, tegra->cmd_data << 10);
3179 mutex_unlock(&tegra->sync_lock);
3182 case MBOX_CMD_SAVE_DFE_CTLE_CTX:
3183 tegra_xhci_save_dfe_context(tegra, tegra->cmd_data);
3184 tegra_xhci_save_ctle_context(tegra, tegra->cmd_data);
3185 sw_resp = CMD_DATA(tegra->cmd_data) | CMD_TYPE(MBOX_CMD_ACK);
3186 goto send_sw_response;
3188 case MBOX_CMD_STAR_HSIC_IDLE:
3189 ports = tegra->cmd_data;
3190 for_each_set_bit(port, &ports, BITS_PER_LONG) {
3191 pad = port_to_hsic_pad(port - 1);
3192 mutex_lock(&tegra->sync_lock);
3193 ret = hsic_pad_pupd_set(tegra, pad, PUPD_IDLE);
3194 mutex_unlock(&tegra->sync_lock);
3199 sw_resp = CMD_DATA(tegra->cmd_data);
3201 sw_resp |= CMD_TYPE(MBOX_CMD_ACK);
3203 sw_resp |= CMD_TYPE(MBOX_CMD_ACK);
3205 goto send_sw_response;
3207 case MBOX_CMD_STOP_HSIC_IDLE:
3208 ports = tegra->cmd_data;
3209 for_each_set_bit(port, &ports, BITS_PER_LONG) {
3210 pad = port_to_hsic_pad(port - 1);
3211 mutex_lock(&tegra->sync_lock);
3212 ret = hsic_pad_pupd_set(tegra, pad, PUPD_DISABLE);
3213 mutex_unlock(&tegra->sync_lock);
3218 sw_resp = CMD_DATA(tegra->cmd_data);
3220 sw_resp |= CMD_TYPE(MBOX_CMD_ACK);
3222 sw_resp |= CMD_TYPE(MBOX_CMD_NACK);
3223 goto send_sw_response;
3226 xhci_dbg(xhci, "%s firmware responds with ACK\n", __func__);
3229 xhci_warn(xhci, "%s firmware responds with NACK\n", __func__);
3232 xhci_err(xhci, "%s: invalid cmdtype %d\n",
3233 __func__, tegra->cmd_type);
3236 /* clear MBOX_SMI_INT_EN bit */
3237 cmd = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
3238 cmd &= ~MBOX_SMI_INT_EN;
3239 writel(cmd, tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
3241 /* clear mailbox ownership */
3242 writel(0, tegra->fpci_base + XUSB_CFG_ARU_MBOX_OWNER);
3244 mutex_unlock(&tegra->mbox_lock);
3248 if (((sw_resp >> CMD_TYPE_SHIFT) & CMD_TYPE_MASK) == MBOX_CMD_NACK)
3249 xhci_err(xhci, "%s respond fw message 0x%x with NAK\n",
3252 writel(sw_resp, tegra->fpci_base + XUSB_CFG_ARU_MBOX_DATA_IN);
3253 cmd = readl(tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
3254 cmd |= MBOX_INT_EN | MBOX_FALC_INT_EN;
3255 writel(cmd, tegra->fpci_base + XUSB_CFG_ARU_MBOX_CMD);
3257 mutex_unlock(&tegra->mbox_lock);
3260 static irqreturn_t pmc_usb_phy_wake_isr(int irq, void *data)
3262 struct tegra_xhci_hcd *tegra = (struct tegra_xhci_hcd *) data;
3263 struct xhci_hcd *xhci = tegra->xhci;
3265 xhci_dbg(xhci, "%s irq %d", __func__, irq);
3269 static irqreturn_t tegra_xhci_padctl_irq(int irq, void *ptrdev)
3271 struct tegra_xhci_hcd *tegra = (struct tegra_xhci_hcd *) ptrdev;
3272 struct xhci_hcd *xhci = tegra->xhci;
3273 struct tegra_xusb_padctl_regs *padregs = tegra->padregs;
3274 u32 elpg_program0 = 0;
3276 spin_lock(&tegra->lock);
3278 tegra->last_jiffies = jiffies;
3280 /* Check the intr cause. Could be USB2 or HSIC or SS wake events */
3281 elpg_program0 = tegra_usb_pad_reg_read(padregs->elpg_program_0);
3283 /* Clear the interrupt cause. We already read the intr status. */
3284 tegra_xhci_ss_wake_on_interrupts(tegra->bdata->portmap, false);
3285 tegra_xhci_hs_wake_on_interrupts(tegra->bdata->portmap, false);
3287 xhci_dbg(xhci, "%s: elpg_program0 = %x\n", __func__, elpg_program0);
3288 xhci_dbg(xhci, "%s: PMC REGISTER = %x\n", __func__,
3289 tegra_usb_pmc_reg_read(PMC_UTMIP_UHSIC_SLEEP_CFG_0));
3290 xhci_dbg(xhci, "%s: OC_DET Register = %x\n",
3291 __func__, readl(tegra->padctl_base + padregs->oc_det_0));
3292 xhci_dbg(xhci, "%s: usb2_bchrg_otgpad0_ctl0_0 Register = %x\n",
3294 readl(tegra->padctl_base + padregs->usb2_bchrg_otgpad0_ctl0_0));
3295 xhci_dbg(xhci, "%s: usb2_bchrg_otgpad1_ctl0_0 Register = %x\n",
3297 readl(tegra->padctl_base + padregs->usb2_bchrg_otgpad1_ctl0_0));
3298 xhci_dbg(xhci, "%s: usb2_bchrg_bias_pad_0 Register = %x\n",
3300 readl(tegra->padctl_base + padregs->usb2_bchrg_bias_pad_0));
3302 if (elpg_program0 & (SS_PORT0_WAKEUP_EVENT | SS_PORT1_WAKEUP_EVENT))
3303 tegra->ss_wake_event = true;
3304 else if (elpg_program0 & (USB2_PORT0_WAKEUP_EVENT |
3305 USB2_PORT1_WAKEUP_EVENT |
3306 USB2_PORT2_WAKEUP_EVENT |
3307 USB2_HSIC_PORT0_WAKEUP_EVENT |
3308 USB2_HSIC_PORT1_WAKEUP_EVENT))
3309 tegra->hs_wake_event = true;
3311 if (tegra->ss_wake_event || tegra->hs_wake_event) {
3312 if (tegra->ss_pwr_gated && !tegra->host_pwr_gated) {
3313 xhci_err(xhci, "SS gated Host ungated. Should not happen\n");
3314 WARN_ON(tegra->ss_pwr_gated && tegra->host_pwr_gated);
3315 } else if (tegra->ss_pwr_gated
3316 && tegra->host_pwr_gated) {
3317 xhci_dbg(xhci, "[%s] schedule host_elpg_exit_work\n",
3319 schedule_work(&tegra->host_elpg_exit_work);
3322 xhci_err(xhci, "error: wake due to no hs/ss event\n");
3323 tegra_usb_pad_reg_write(padregs->elpg_program_0, 0xffffffff);
3325 spin_unlock(&tegra->lock);
3329 static irqreturn_t tegra_xhci_smi_irq(int irq, void *ptrdev)
3331 struct tegra_xhci_hcd *tegra = (struct tegra_xhci_hcd *) ptrdev;
3334 spin_lock(&tegra->lock);
3336 /* clear the mbox intr status 1st thing. Other
3337 * bits are W1C bits, so just write to SMI bit.
3340 temp = readl(tegra->fpci_base + XUSB_CFG_ARU_SMI_INTR);
3342 /* write 1 to clear SMI INTR en bit ( bit 3 ) */
3343 temp = MBOX_SMI_INTR_EN;
3344 writel(temp, tegra->fpci_base + XUSB_CFG_ARU_SMI_INTR);
3346 schedule_work(&tegra->mbox_work);
3348 spin_unlock(&tegra->lock);
3352 static void tegra_xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
3355 * As of now platform drivers don't provide MSI support so we ensure
3356 * here that the generic code does not try to make a pci_dev from our
3357 * dev struct in order to setup MSI
3359 xhci->quirks |= XHCI_PLAT;
3360 xhci->quirks &= ~XHCI_SPURIOUS_REBOOT;
3363 /* called during probe() after chip reset completes */
3364 static int xhci_plat_setup(struct usb_hcd *hcd)
3366 return xhci_gen_setup(hcd, tegra_xhci_plat_quirks);
3369 static int tegra_xhci_request_mem_region(struct platform_device *pdev,
3370 int num, void __iomem **region)
3372 struct resource *res;
3375 res = platform_get_resource(pdev, IORESOURCE_MEM, num);
3377 dev_err(&pdev->dev, "memory resource %d doesn't exist\n", num);
3381 mem = devm_request_and_ioremap(&pdev->dev, res);
3383 dev_err(&pdev->dev, "failed to ioremap for %d\n", num);
3391 static int tegra_xhci_request_irq(struct platform_device *pdev,
3392 int num, irq_handler_t handler, unsigned long irqflags,
3393 const char *devname, int *irq_no)
3396 struct tegra_xhci_hcd *tegra = platform_get_drvdata(pdev);
3397 struct resource *res;
3399 res = platform_get_resource(pdev, IORESOURCE_IRQ, num);
3401 dev_err(&pdev->dev, "irq resource %d doesn't exist\n", num);
3405 ret = devm_request_irq(&pdev->dev, res->start, handler, irqflags,
3409 "failed to request_irq for %s (irq %d), error = %d\n",
3410 devname, (int)res->start, ret);
3413 *irq_no = res->start;
3420 static int tegra_xhci_bus_suspend(struct usb_hcd *hcd)
3422 struct tegra_xhci_hcd *tegra = hcd_to_tegra_xhci(hcd);
3423 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3425 unsigned long flags;
3427 mutex_lock(&tegra->sync_lock);
3429 if (xhci->shared_hcd == hcd) {
3430 tegra->usb3_rh_suspend = true;
3431 xhci_dbg(xhci, "%s: usb3 root hub\n", __func__);
3432 } else if (xhci->main_hcd == hcd) {
3433 tegra->usb2_rh_suspend = true;
3434 xhci_dbg(xhci, "%s: usb2 root hub\n", __func__);
3437 WARN_ON(tegra->hc_in_elpg);
3439 /* suspend xhci bus. This will also set remote mask */
3440 err = xhci_bus_suspend(hcd);
3442 xhci_err(xhci, "%s: xhci_bus_suspend failed %d\n",
3444 goto xhci_bus_suspend_failed;
3447 if (!(tegra->usb2_rh_suspend && tegra->usb3_rh_suspend))
3448 goto done; /* one of the root hubs is still working */
3450 spin_lock_irqsave(&tegra->lock, flags);
3451 tegra->hc_in_elpg = true;
3452 spin_unlock_irqrestore(&tegra->lock, flags);
3454 WARN_ON(tegra->ss_pwr_gated && tegra->host_pwr_gated);
3456 /* save xhci spec ctx. Already done by xhci_suspend */
3457 err = xhci_suspend(tegra->xhci);
3459 xhci_err(xhci, "%s: xhci_suspend failed %d\n", __func__, err);
3460 goto xhci_suspend_failed;
3463 /* Powergate host. Include ss power gate if not already done */
3464 err = tegra_xhci_host_elpg_entry(tegra);
3466 xhci_err(xhci, "%s: unable to perform elpg entry %d\n",
3468 goto tegra_xhci_host_elpg_entry_failed;
3471 /* At this point,ensure ss/hs intr enables are always on */
3472 tegra_xhci_ss_wake_on_interrupts(tegra->bdata->portmap, true);
3473 tegra_xhci_hs_wake_on_interrupts(tegra->bdata->portmap, true);
3475 /* In ELPG, firmware log context is gone. Rewind shared log buffer. */
3476 if (fw_log_wait_empty_timeout(tegra, 100))
3477 xhci_warn(xhci, "%s still has logs\n", __func__);
3478 tegra->log.dequeue = tegra->log.virt_addr;
3482 /* pads are disabled only if usb2 root hub in xusb is idle */
3483 /* pads will actually be disabled only when all usb2 ports are idle */
3484 if (xhci->main_hcd == hcd) {
3485 utmi_phy_pad_disable();
3486 utmi_phy_iddq_override(true);
3488 mutex_unlock(&tegra->sync_lock);
3491 tegra_xhci_host_elpg_entry_failed:
3493 xhci_suspend_failed:
3494 tegra->hc_in_elpg = false;
3495 xhci_bus_suspend_failed:
3496 if (xhci->shared_hcd == hcd)
3497 tegra->usb3_rh_suspend = false;
3498 else if (xhci->main_hcd == hcd)
3499 tegra->usb2_rh_suspend = false;
3501 mutex_unlock(&tegra->sync_lock);
3505 /* First, USB2HCD and then USB3HCD resume will be called */
3506 static int tegra_xhci_bus_resume(struct usb_hcd *hcd)
3508 struct tegra_xhci_hcd *tegra = hcd_to_tegra_xhci(hcd);
3509 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3512 mutex_lock(&tegra->sync_lock);
3514 tegra->host_resume_req = true;
3516 if (xhci->shared_hcd == hcd)
3517 xhci_dbg(xhci, "%s: usb3 root hub\n", __func__);
3518 else if (xhci->main_hcd == hcd)
3519 xhci_dbg(xhci, "%s: usb2 root hub\n", __func__);
3521 /* pads are disabled only if usb2 root hub in xusb is idle */
3522 /* pads will actually be disabled only when all usb2 ports are idle */
3523 if (xhci->main_hcd == hcd && tegra->usb2_rh_suspend) {
3524 utmi_phy_pad_enable();
3525 utmi_phy_iddq_override(false);
3527 if (tegra->usb2_rh_suspend && tegra->usb3_rh_suspend) {
3528 if (tegra->ss_pwr_gated && tegra->host_pwr_gated)
3529 tegra_xhci_host_partition_elpg_exit(tegra);
3532 /* handle remote wakeup before resuming bus */
3533 wait_remote_wakeup_ports(hcd);
3535 err = xhci_bus_resume(hcd);
3537 xhci_err(xhci, "%s: xhci_bus_resume failed %d\n",
3539 goto xhci_bus_resume_failed;
3542 if (xhci->shared_hcd == hcd)
3543 tegra->usb3_rh_suspend = false;
3544 else if (xhci->main_hcd == hcd)
3545 tegra->usb2_rh_suspend = false;
3547 mutex_unlock(&tegra->sync_lock);
3550 xhci_bus_resume_failed:
3551 /* TODO: reverse elpg? */
3552 mutex_unlock(&tegra->sync_lock);
3557 static irqreturn_t tegra_xhci_irq(struct usb_hcd *hcd)
3559 struct tegra_xhci_hcd *tegra = hcd_to_tegra_xhci(hcd);
3560 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3561 irqreturn_t iret = IRQ_HANDLED;
3564 spin_lock(&tegra->lock);
3565 if (tegra->hc_in_elpg) {
3566 spin_lock(&xhci->lock);
3567 if (HCD_HW_ACCESSIBLE(hcd)) {
3568 status = xhci_readl(xhci, &xhci->op_regs->status);
3570 xhci_writel(xhci, status, &xhci->op_regs->status);
3572 xhci_dbg(xhci, "%s: schedule host_elpg_exit_work\n",
3574 schedule_work(&tegra->host_elpg_exit_work);
3575 spin_unlock(&xhci->lock);
3577 iret = xhci_irq(hcd);
3578 spin_unlock(&tegra->lock);
3580 wake_up_interruptible(&tegra->log.intr_wait);
3586 static const struct hc_driver tegra_plat_xhci_driver = {
3587 .description = "tegra-xhci",
3588 .product_desc = "Nvidia xHCI Host Controller",
3589 .hcd_priv_size = sizeof(struct xhci_hcd *),
3592 * generic hardware linkage
3594 .irq = tegra_xhci_irq,
3595 .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
3598 * basic lifecycle operations
3600 .reset = xhci_plat_setup,
3603 .shutdown = xhci_shutdown,
3606 * managing i/o requests and associated device resources
3608 .urb_enqueue = xhci_urb_enqueue,
3609 .urb_dequeue = xhci_urb_dequeue,
3610 .alloc_dev = xhci_alloc_dev,
3611 .free_dev = xhci_free_dev,
3612 .alloc_streams = xhci_alloc_streams,
3613 .free_streams = xhci_free_streams,
3614 .add_endpoint = xhci_add_endpoint,
3615 .drop_endpoint = xhci_drop_endpoint,
3616 .endpoint_reset = xhci_endpoint_reset,
3617 .check_bandwidth = xhci_check_bandwidth,
3618 .reset_bandwidth = xhci_reset_bandwidth,
3619 .address_device = xhci_address_device,
3620 .update_hub_device = xhci_update_hub_device,
3621 .reset_device = xhci_discover_or_reset_device,
3624 * scheduling support
3626 .get_frame_number = xhci_get_frame,
3628 /* Root hub support */
3629 .hub_control = xhci_hub_control,
3630 .hub_status_data = xhci_hub_status_data,
3633 .bus_suspend = tegra_xhci_bus_suspend,
3634 .bus_resume = tegra_xhci_bus_resume,
3640 tegra_xhci_suspend(struct platform_device *pdev,
3643 struct tegra_xhci_hcd *tegra = platform_get_drvdata(pdev);
3644 struct xhci_hcd *xhci = tegra->xhci;
3648 mutex_lock(&tegra->sync_lock);
3649 if (!tegra->init_done) {
3650 xhci_warn(xhci, "%s: xhci probe not done\n",
3652 mutex_unlock(&tegra->sync_lock);
3655 if (!tegra->hc_in_elpg) {
3656 xhci_warn(xhci, "%s: lp0 suspend entry while elpg not done\n",
3658 mutex_unlock(&tegra->sync_lock);
3661 mutex_unlock(&tegra->sync_lock);
3663 tegra_xhci_ss_wake_on_interrupts(tegra->bdata->portmap, false);
3664 tegra_xhci_hs_wake_on_interrupts(tegra->bdata->portmap, false);
3666 /* enable_irq_wake for ss ports */
3667 ret = enable_irq_wake(tegra->padctl_irq);
3670 "%s: Couldn't enable USB host mode wakeup, irq=%d, error=%d\n",
3671 __func__, tegra->padctl_irq, ret);
3674 /* enable_irq_wake for utmip/uhisc wakes */
3675 ret = enable_irq_wake(tegra->usb3_irq);
3678 "%s: Couldn't enable utmip/uhsic wakeup, irq=%d, error=%d\n",
3679 __func__, tegra->usb3_irq, ret);
3682 /* enable_irq_wake for utmip/uhisc wakes */
3683 ret = enable_irq_wake(tegra->usb2_irq);
3686 "%s: Couldn't enable utmip/uhsic wakeup, irq=%d, error=%d\n",
3687 __func__, tegra->usb2_irq, ret);
3690 regulator_disable(tegra->xusb_s1p8v_reg);
3691 regulator_disable(tegra->xusb_s1p05v_reg);
3692 tegra_usb2_clocks_deinit(tegra);
3698 tegra_xhci_resume(struct platform_device *pdev)
3700 struct tegra_xhci_hcd *tegra = platform_get_drvdata(pdev);
3701 struct xhci_hcd *xhci = tegra->xhci;
3703 dev_dbg(&pdev->dev, "%s\n", __func__);
3705 mutex_lock(&tegra->sync_lock);
3706 if (!tegra->init_done) {
3707 xhci_warn(xhci, "%s: xhci probe not done\n",
3709 mutex_unlock(&tegra->sync_lock);
3712 mutex_unlock(&tegra->sync_lock);
3714 tegra->last_jiffies = jiffies;
3716 disable_irq_wake(tegra->padctl_irq);
3717 disable_irq_wake(tegra->usb3_irq);
3718 disable_irq_wake(tegra->usb2_irq);
3719 tegra->lp0_exit = true;
3721 regulator_enable(tegra->xusb_s1p05v_reg);
3722 regulator_enable(tegra->xusb_s1p8v_reg);
3723 tegra_usb2_clocks_init(tegra);
3730 static int init_bootloader_firmware(struct tegra_xhci_hcd *tegra)
3732 struct platform_device *pdev = tegra->pdev;
3733 void __iomem *fw_mmio_base;
3734 phys_addr_t fw_mem_phy_addr;
3737 #ifdef CONFIG_PLATFORM_ENABLE_IOMMU
3739 DEFINE_DMA_ATTRS(attrs);
3742 /* bootloader saved firmware memory address in PMC SCRATCH34 register */
3743 fw_mem_phy_addr = tegra_usb_pmc_reg_read(PMC_SCRATCH34);
3745 fw_mmio_base = devm_ioremap_nocache(&pdev->dev,
3746 fw_mem_phy_addr, sizeof(struct cfgtbl));
3748 if (!fw_mmio_base) {
3749 dev_err(&pdev->dev, "error mapping fw memory 0x%x\n",
3750 (u32)fw_mem_phy_addr);
3754 fw_size = ioread32(fw_mmio_base + FW_SIZE_OFFSET);
3755 devm_iounmap(&pdev->dev, fw_mmio_base);
3757 fw_mmio_base = devm_ioremap_nocache(&pdev->dev,
3758 fw_mem_phy_addr, fw_size);
3759 if (!fw_mmio_base) {
3760 dev_err(&pdev->dev, "error mapping fw memory 0x%x\n",
3761 (u32)fw_mem_phy_addr);
3765 dev_info(&pdev->dev, "Firmware Memory: phy 0x%x mapped 0x%p (%d Bytes)\n",
3766 (u32) fw_mem_phy_addr, fw_mmio_base, fw_size);
3768 #ifdef CONFIG_PLATFORM_ENABLE_IOMMU
3769 dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
3770 fw_dma = dma_map_linear_attrs(&pdev->dev, fw_mem_phy_addr, fw_size,
3771 DMA_TO_DEVICE, &attrs);
3773 if (fw_dma == DMA_ERROR_CODE) {
3774 dev_err(&pdev->dev, "%s: dma_map_linear failed\n",
3780 fw_dma = fw_mem_phy_addr;
3782 dev_info(&pdev->dev, "Firmware DMA Memory: dma 0x%p (%d Bytes)\n",
3783 (void *) fw_dma, fw_size);
3785 /* all set and ready to go */
3786 tegra->firmware.data = fw_mmio_base;
3787 tegra->firmware.dma = fw_dma;
3788 tegra->firmware.size = fw_size;
3792 #ifdef CONFIG_PLATFORM_ENABLE_IOMMU
3794 devm_iounmap(&pdev->dev, fw_mmio_base);
3799 static void deinit_bootloader_firmware(struct tegra_xhci_hcd *tegra)
3801 struct platform_device *pdev = tegra->pdev;
3802 void __iomem *fw_mmio_base = tegra->firmware.data;
3804 #ifdef CONFIG_PLATFORM_ENABLE_IOMMU
3805 dma_unmap_single(&pdev->dev, tegra->firmware.dma,
3806 tegra->firmware.size, DMA_TO_DEVICE);
3808 devm_iounmap(&pdev->dev, fw_mmio_base);
3810 memset(&tegra->firmware, 0, sizeof(tegra->firmware));
3813 static int init_filesystem_firmware(struct tegra_xhci_hcd *tegra)
3815 struct platform_device *pdev = tegra->pdev;
3818 ret = request_firmware_nowait(THIS_MODULE, true, firmware_file,
3819 &pdev->dev, GFP_KERNEL, tegra, init_filesystem_firmware_done);
3821 dev_err(&pdev->dev, "request_firmware failed %d\n", ret);
3828 static void init_filesystem_firmware_done(const struct firmware *fw,
3831 struct tegra_xhci_hcd *tegra = context;
3832 struct platform_device *pdev = tegra->pdev;
3833 struct cfgtbl *fw_cfgtbl;
3839 mutex_lock(&tegra->sync_lock);
3843 "failed to init firmware from filesystem: %s\n",
3845 goto err_firmware_done;
3848 fw_cfgtbl = (struct cfgtbl *) fw->data;
3849 fw_size = fw_cfgtbl->fwimg_len;
3850 dev_info(&pdev->dev, "Firmware File: %s (%d Bytes)\n",
3851 firmware_file, fw_size);
3853 fw_data = dma_alloc_coherent(&pdev->dev, fw_size,
3854 &fw_dma, GFP_KERNEL);
3856 dev_err(&pdev->dev, "%s: dma_alloc_coherent failed\n",
3858 goto err_firmware_done;
3861 memcpy(fw_data, fw->data, fw_size);
3862 dev_info(&pdev->dev,
3863 "Firmware DMA Memory: dma 0x%p mapped 0x%p (%d Bytes)\n",
3864 (void *) fw_dma, fw_data, fw_size);
3866 /* all set and ready to go */
3867 tegra->firmware.data = fw_data;
3868 tegra->firmware.dma = fw_dma;
3869 tegra->firmware.size = fw_size;
3871 ret = tegra_xhci_probe2(tegra);
3873 dev_err(&pdev->dev, "%s: failed to probe: %d\n", __func__, ret);
3874 goto err_firmware_done;
3877 release_firmware(fw);
3878 mutex_unlock(&tegra->sync_lock);
3882 release_firmware(fw);
3883 mutex_unlock(&tegra->sync_lock);
3884 tegra_xhci_remove(pdev);
3887 static void deinit_filesystem_firmware(struct tegra_xhci_hcd *tegra)
3889 struct platform_device *pdev = tegra->pdev;
3891 if (tegra->firmware.data) {
3892 dma_free_coherent(&pdev->dev, tegra->firmware.size,
3893 tegra->firmware.data, tegra->firmware.dma);
3896 memset(&tegra->firmware, 0, sizeof(tegra->firmware));
3898 static int init_firmware(struct tegra_xhci_hcd *tegra)
3900 if (use_bootloader_firmware)
3901 return init_bootloader_firmware(tegra);
3903 return init_filesystem_firmware(tegra);
3906 static void deinit_firmware(struct tegra_xhci_hcd *tegra)
3908 if (use_bootloader_firmware)
3909 return deinit_bootloader_firmware(tegra);
3911 return deinit_filesystem_firmware(tegra);
3914 static int tegra_enable_xusb_clk(struct tegra_xhci_hcd *tegra,
3915 struct platform_device *pdev)
3918 /* enable ss clock */
3919 err = clk_enable(tegra->host_clk);
3921 dev_err(&pdev->dev, "Failed to enable host partition clk\n");
3922 goto enable_host_clk_failed;
3925 err = clk_enable(tegra->ss_clk);
3927 dev_err(&pdev->dev, "Failed to enable ss partition clk\n");
3928 goto eanble_ss_clk_failed;
3931 err = clk_enable(tegra->emc_clk);
3933 dev_err(&pdev->dev, "Failed to enable xusb.emc clk\n");
3934 goto eanble_emc_clk_failed;
3939 eanble_emc_clk_failed:
3940 clk_disable(tegra->ss_clk);
3942 eanble_ss_clk_failed:
3943 clk_disable(tegra->host_clk);
3945 enable_host_clk_failed:
3946 if (tegra->soc_config->quirks & TEGRA_XUSB_USE_HS_SRC_CLOCK2)
3947 clk_disable(tegra->pll_re_vco_clk);
3951 static struct tegra_xusb_padctl_regs t114_padregs_offset = {
3952 .boot_media_0 = 0x0,
3953 .usb2_pad_mux_0 = 0x4,
3954 .usb2_port_cap_0 = 0x8,
3955 .snps_oc_map_0 = 0xc,
3956 .usb2_oc_map_0 = 0x10,
3957 .ss_port_map_0 = 0x14,
3959 .elpg_program_0 = 0x1c,
3960 .usb2_bchrg_otgpad0_ctl0_0 = 0x20,
3961 .usb2_bchrg_otgpad0_ctl1_0 = 0xffff,
3962 .usb2_bchrg_otgpad1_ctl0_0 = 0x24,
3963 .usb2_bchrg_otgpad1_ctl1_0 = 0xffff,
3964 .usb2_bchrg_otgpad2_ctl0_0 = 0xffff,
3965 .usb2_bchrg_otgpad2_ctl1_0 = 0xffff,
3966 .usb2_bchrg_bias_pad_0 = 0x28,
3967 .usb2_bchrg_tdcd_dbnc_timer_0 = 0x2c,
3968 .iophy_pll_p0_ctl1_0 = 0x30,
3969 .iophy_pll_p0_ctl2_0 = 0x34,
3970 .iophy_pll_p0_ctl3_0 = 0x38,
3971 .iophy_pll_p0_ctl4_0 = 0x3c,
3972 .iophy_usb3_pad0_ctl1_0 = 0x40,
3973 .iophy_usb3_pad1_ctl1_0 = 0x44,
3974 .iophy_usb3_pad0_ctl2_0 = 0x48,
3975 .iophy_usb3_pad1_ctl2_0 = 0x4c,
3976 .iophy_usb3_pad0_ctl3_0 = 0x50,
3977 .iophy_usb3_pad1_ctl3_0 = 0x54,
3978 .iophy_usb3_pad0_ctl4_0 = 0x58,
3979 .iophy_usb3_pad1_ctl4_0 = 0x5c,
3980 .iophy_misc_pad_p0_ctl1_0 = 0x60,
3981 .iophy_misc_pad_p1_ctl1_0 = 0x64,
3982 .iophy_misc_pad_p0_ctl2_0 = 0x68,
3983 .iophy_misc_pad_p1_ctl2_0 = 0x6c,
3984 .iophy_misc_pad_p0_ctl3_0 = 0x70,
3985 .iophy_misc_pad_p1_ctl3_0 = 0x74,
3986 .iophy_misc_pad_p0_ctl4_0 = 0x78,
3987 .iophy_misc_pad_p1_ctl4_0 = 0x7c,
3988 .iophy_misc_pad_p0_ctl5_0 = 0x80,
3989 .iophy_misc_pad_p1_ctl5_0 = 0x84,
3990 .iophy_misc_pad_p0_ctl6_0 = 0x88,
3991 .iophy_misc_pad_p1_ctl6_0 = 0x8c,
3992 .usb2_otg_pad0_ctl0_0 = 0x90,
3993 .usb2_otg_pad1_ctl0_0 = 0x94,
3994 .usb2_otg_pad2_ctl0_0 = 0xffff,
3995 .usb2_otg_pad0_ctl1_0 = 0x98,
3996 .usb2_otg_pad1_ctl1_0 = 0x9c,
3997 .usb2_otg_pad2_ctl1_0 = 0xffff,
3998 .usb2_bias_pad_ctl0_0 = 0xa0,
3999 .usb2_bias_pad_ctl1_0 = 0xa4,
4000 .usb2_hsic_pad0_ctl0_0 = 0xa8,
4001 .usb2_hsic_pad1_ctl0_0 = 0xac,
4002 .usb2_hsic_pad0_ctl1_0 = 0xb0,
4003 .usb2_hsic_pad1_ctl1_0 = 0xb4,
4004 .usb2_hsic_pad0_ctl2_0 = 0xb8,
4005 .usb2_hsic_pad1_ctl2_0 = 0xbc,
4006 .ulpi_link_trim_ctl0 = 0xc0,
4007 .ulpi_null_clk_trim_ctl0 = 0xc4,
4008 .hsic_strb_trim_ctl0 = 0xc8,
4011 .iophy_misc_pad_p2_ctl1_0 = 0xffff,
4012 .iophy_misc_pad_p3_ctl1_0 = 0xffff,
4013 .iophy_misc_pad_p4_ctl1_0 = 0xffff,
4014 .iophy_misc_pad_p2_ctl2_0 = 0xffff,
4015 .iophy_misc_pad_p3_ctl2_0 = 0xffff,
4016 .iophy_misc_pad_p4_ctl2_0 = 0xffff,
4017 .iophy_misc_pad_p2_ctl3_0 = 0xffff,
4018 .iophy_misc_pad_p3_ctl3_0 = 0xffff,
4019 .iophy_misc_pad_p4_ctl3_0 = 0xffff,
4020 .iophy_misc_pad_p2_ctl4_0 = 0xffff,
4021 .iophy_misc_pad_p3_ctl4_0 = 0xffff,
4022 .iophy_misc_pad_p4_ctl4_0 = 0xffff,
4023 .iophy_misc_pad_p2_ctl5_0 = 0xffff,
4024 .iophy_misc_pad_p3_ctl5_0 = 0xffff,
4025 .iophy_misc_pad_p4_ctl5_0 = 0xffff,
4026 .iophy_misc_pad_p2_ctl6_0 = 0xffff,
4027 .iophy_misc_pad_p3_ctl6_0 = 0xffff,
4028 .iophy_misc_pad_p4_ctl6_0 = 0xffff,
4029 .usb3_pad_mux_0 = 0xffff,
4030 .iophy_pll_s0_ctl1_0 = 0xffff,
4031 .iophy_pll_s0_ctl2_0 = 0xffff,
4032 .iophy_pll_s0_ctl3_0 = 0xffff,
4033 .iophy_pll_s0_ctl4_0 = 0xffff,
4034 .iophy_misc_pad_s0_ctl1_0 = 0xffff,
4035 .iophy_misc_pad_s0_ctl2_0 = 0xffff,
4036 .iophy_misc_pad_s0_ctl3_0 = 0xffff,
4037 .iophy_misc_pad_s0_ctl4_0 = 0xffff,
4038 .iophy_misc_pad_s0_ctl5_0 = 0xffff,
4039 .iophy_misc_pad_s0_ctl6_0 = 0xffff,
4042 static struct tegra_xusb_padctl_regs t124_padregs_offset = {
4043 .boot_media_0 = 0x0,
4044 .usb2_pad_mux_0 = 0x4,
4045 .usb2_port_cap_0 = 0x8,
4046 .snps_oc_map_0 = 0xc,
4047 .usb2_oc_map_0 = 0x10,
4048 .ss_port_map_0 = 0x14,
4050 .elpg_program_0 = 0x1c,
4051 .usb2_bchrg_otgpad0_ctl0_0 = 0x20,
4052 .usb2_bchrg_otgpad0_ctl1_0 = 0x24,
4053 .usb2_bchrg_otgpad1_ctl0_0 = 0x28,
4054 .usb2_bchrg_otgpad1_ctl1_0 = 0x2c,
4055 .usb2_bchrg_otgpad2_ctl0_0 = 0x30,
4056 .usb2_bchrg_otgpad2_ctl1_0 = 0x34,
4057 .usb2_bchrg_bias_pad_0 = 0x38,
4058 .usb2_bchrg_tdcd_dbnc_timer_0 = 0x3c,
4059 .iophy_pll_p0_ctl1_0 = 0x40,
4060 .iophy_pll_p0_ctl2_0 = 0x44,
4061 .iophy_pll_p0_ctl3_0 = 0x48,
4062 .iophy_pll_p0_ctl4_0 = 0x4c,
4063 .iophy_usb3_pad0_ctl1_0 = 0x50,
4064 .iophy_usb3_pad1_ctl1_0 = 0x54,
4065 .iophy_usb3_pad0_ctl2_0 = 0x58,
4066 .iophy_usb3_pad1_ctl2_0 = 0x5c,
4067 .iophy_usb3_pad0_ctl3_0 = 0x60,
4068 .iophy_usb3_pad1_ctl3_0 = 0x64,
4069 .iophy_usb3_pad0_ctl4_0 = 0x68,
4070 .iophy_usb3_pad1_ctl4_0 = 0x6c,
4071 .iophy_misc_pad_p0_ctl1_0 = 0x70,
4072 .iophy_misc_pad_p1_ctl1_0 = 0x74,
4073 .iophy_misc_pad_p0_ctl2_0 = 0x78,
4074 .iophy_misc_pad_p1_ctl2_0 = 0x7c,
4075 .iophy_misc_pad_p0_ctl3_0 = 0x80,
4076 .iophy_misc_pad_p1_ctl3_0 = 0x84,
4077 .iophy_misc_pad_p0_ctl4_0 = 0x88,
4078 .iophy_misc_pad_p1_ctl4_0 = 0x8c,
4079 .iophy_misc_pad_p0_ctl5_0 = 0x90,
4080 .iophy_misc_pad_p1_ctl5_0 = 0x94,
4081 .iophy_misc_pad_p0_ctl6_0 = 0x98,
4082 .iophy_misc_pad_p1_ctl6_0 = 0x9c,
4083 .usb2_otg_pad0_ctl0_0 = 0xa0,
4084 .usb2_otg_pad1_ctl0_0 = 0xa4,
4085 .usb2_otg_pad2_ctl0_0 = 0xa8,
4086 .usb2_otg_pad0_ctl1_0 = 0xac,
4087 .usb2_otg_pad1_ctl1_0 = 0xb0,
4088 .usb2_otg_pad2_ctl1_0 = 0xb4,
4089 .usb2_bias_pad_ctl0_0 = 0xb8,
4090 .usb2_bias_pad_ctl1_0 = 0xbc,
4091 .usb2_hsic_pad0_ctl0_0 = 0xc0,
4092 .usb2_hsic_pad1_ctl0_0 = 0xc4,
4093 .usb2_hsic_pad0_ctl1_0 = 0xc8,
4094 .usb2_hsic_pad1_ctl1_0 = 0xcc,
4095 .usb2_hsic_pad0_ctl2_0 = 0xd0,
4096 .usb2_hsic_pad1_ctl2_0 = 0xd4,
4097 .ulpi_link_trim_ctl0 = 0xd8,
4098 .ulpi_null_clk_trim_ctl0 = 0xdc,
4099 .hsic_strb_trim_ctl0 = 0xe0,
4102 .iophy_misc_pad_p2_ctl1_0 = 0xec,
4103 .iophy_misc_pad_p3_ctl1_0 = 0xf0,
4104 .iophy_misc_pad_p4_ctl1_0 = 0xf4,
4105 .iophy_misc_pad_p2_ctl2_0 = 0xf8,
4106 .iophy_misc_pad_p3_ctl2_0 = 0xfc,
4107 .iophy_misc_pad_p4_ctl2_0 = 0x100,
4108 .iophy_misc_pad_p2_ctl3_0 = 0x104,
4109 .iophy_misc_pad_p3_ctl3_0 = 0x108,
4110 .iophy_misc_pad_p4_ctl3_0 = 0x10c,
4111 .iophy_misc_pad_p2_ctl4_0 = 0x110,
4112 .iophy_misc_pad_p3_ctl4_0 = 0x114,
4113 .iophy_misc_pad_p4_ctl4_0 = 0x118,
4114 .iophy_misc_pad_p2_ctl5_0 = 0x11c,
4115 .iophy_misc_pad_p3_ctl5_0 = 0x120,
4116 .iophy_misc_pad_p4_ctl5_0 = 0x124,
4117 .iophy_misc_pad_p2_ctl6_0 = 0x128,
4118 .iophy_misc_pad_p3_ctl6_0 = 0x12c,
4119 .iophy_misc_pad_p4_ctl6_0 = 0x130,
4120 .usb3_pad_mux_0 = 0x134,
4121 .iophy_pll_s0_ctl1_0 = 0x138,
4122 .iophy_pll_s0_ctl2_0 = 0x13c,
4123 .iophy_pll_s0_ctl3_0 = 0x140,
4124 .iophy_pll_s0_ctl4_0 = 0x144,
4125 .iophy_misc_pad_s0_ctl1_0 = 0x148,
4126 .iophy_misc_pad_s0_ctl2_0 = 0x14c,
4127 .iophy_misc_pad_s0_ctl3_0 = 0x150,
4128 .iophy_misc_pad_s0_ctl4_0 = 0x154,
4129 .iophy_misc_pad_s0_ctl5_0 = 0x158,
4130 .iophy_misc_pad_s0_ctl6_0 = 0x15c,
4133 /* FIXME: using notifier to transfer control to host from suspend
4134 * for otg port when xhci is in elpg. Find better alternative
4136 static int tegra_xhci_otg_notify(struct notifier_block *nb,
4137 unsigned long event, void *unused)
4139 struct tegra_xhci_hcd *tegra = container_of(nb,
4140 struct tegra_xhci_hcd, otgnb);
4142 if ((event == USB_EVENT_ID))
4143 if (tegra->hc_in_elpg) {
4144 schedule_work(&tegra->host_elpg_exit_work);
4145 tegra->host_resume_req = true;
4151 static void tegra_xusb_read_board_data(struct tegra_xhci_hcd *tegra)
4153 struct tegra_xusb_board_data *bdata = tegra->bdata;
4154 struct device_node *node = tegra->pdev->dev.of_node;
4157 bdata->uses_external_pmic = of_property_read_bool(node,
4158 "nvidia,uses_external_pmic");
4159 bdata->gpio_controls_muxed_ss_lanes = of_property_read_bool(node,
4160 "nvidia,gpio_controls_muxed_ss_lanes");
4161 ret = of_property_read_u32(node, "nvidia,gpio_ss1_sata",
4162 &bdata->gpio_ss1_sata);
4163 ret = of_property_read_u32(node, "nvidia,portmap",
4165 ret = of_property_read_u8(node, "nvidia,ss_portmap",
4166 &bdata->ss_portmap);
4167 ret = of_property_read_u8(node, "nvidia,lane_owner",
4168 &bdata->lane_owner);
4169 ret = of_property_read_u8(node, "nvidia,ulpicap",
4171 ret = of_property_read_string_index(node, "nvidia,supply_utmi_vbuses",
4172 0, &bdata->supply.utmi_vbuses[0]);
4173 ret = of_property_read_string_index(node, "nvidia,supply_utmi_vbuses",
4174 1, &bdata->supply.utmi_vbuses[1]);
4175 ret = of_property_read_string_index(node, "nvidia,supply_utmi_vbuses",
4176 2, &bdata->supply.utmi_vbuses[2]);
4177 ret = of_property_read_string(node, "nvidia,supply_s3p3v",
4178 &bdata->supply.s3p3v);
4179 ret = of_property_read_string(node, "nvidia,supply_s1p8v",
4180 &bdata->supply.s1p8v);
4181 ret = of_property_read_string(node, "nvidia,supply_vddio_hsic",
4182 &bdata->supply.vddio_hsic);
4183 ret = of_property_read_string(node, "nvidia,supply_s1p05v",
4184 &bdata->supply.s1p05v);
4185 /* TODO: add read for tegra_xusb_hsic_config
4186 * Add error conditions check
4190 static void tegra_xusb_read_calib_data(struct tegra_xhci_hcd *tegra)
4192 u32 usb_calib0 = tegra_fuse_readl(FUSE_SKU_USB_CALIB_0);
4193 struct tegra_xusb_chip_calib *cdata = tegra->cdata;
4195 pr_info("tegra_xusb_read_usb_calib: usb_calib0 = 0x%08x\n", usb_calib0);
4197 * read from usb_calib0 and pass to driver
4198 * set HS_CURR_LEVEL (PAD0) = usb_calib0[5:0]
4199 * set TERM_RANGE_ADJ = usb_calib0[10:7]
4200 * set HS_SQUELCH_LEVEL = usb_calib0[12:11]
4201 * set HS_IREF_CAP = usb_calib0[14:13]
4202 * set HS_CURR_LEVEL (PAD1) = usb_calib0[20:15]
4205 cdata->hs_curr_level_pad0 = (usb_calib0 >> 0) & 0x3f;
4206 cdata->hs_term_range_adj = (usb_calib0 >> 7) & 0xf;
4207 cdata->hs_squelch_level = (usb_calib0 >> 11) & 0x3;
4208 cdata->hs_iref_cap = (usb_calib0 >> 13) & 0x3;
4209 cdata->hs_curr_level_pad1 = (usb_calib0 >> 15) & 0x3f;
4210 cdata->hs_curr_level_pad2 = (usb_calib0 >> 15) & 0x3f;
4213 static const struct tegra_xusb_soc_config tegra114_soc_config = {
4214 .pmc_portmap = (TEGRA_XUSB_UTMIP_PMC_PORT0 << 0) |
4215 (TEGRA_XUSB_UTMIP_PMC_PORT2 << 4),
4216 .quirks = TEGRA_XUSB_USE_HS_SRC_CLOCK2,
4217 .rx_wander = (0x3 << 4),
4218 .rx_eq = (0x3928 << 8),
4219 .cdr_cntl = (0x26 << 24),
4220 .dfe_cntl = 0x002008EE,
4221 .hs_slew = (0xE << 6),
4222 .ls_rslew_pad0 = (0x3 << 14),
4223 .ls_rslew_pad1 = (0x0 << 14),
4224 .hs_disc_lvl = (0x5 << 2),
4228 static const struct tegra_xusb_soc_config tegra124_soc_config = {
4229 .pmc_portmap = (TEGRA_XUSB_UTMIP_PMC_PORT0 << 0) |
4230 (TEGRA_XUSB_UTMIP_PMC_PORT1 << 4) |
4231 (TEGRA_XUSB_UTMIP_PMC_PORT2 << 8),
4232 .rx_wander = (0xF << 4),
4233 .rx_eq = (0xF070 << 8),
4234 .cdr_cntl = (0x26 << 24),
4235 .dfe_cntl = 0x002008EE,
4236 .hs_slew = (0xE << 6),
4237 .ls_rslew_pad0 = (0x3 << 14),
4238 .ls_rslew_pad1 = (0x0 << 14),
4239 .ls_rslew_pad2 = (0x0 << 14),
4240 .hs_disc_lvl = (0x5 << 2),
4244 static struct of_device_id tegra_xhci_of_match[] = {
4245 { .compatible = "nvidia,tegra114-xhci", .data = &tegra114_soc_config },
4246 { .compatible = "nvidia,tegra124-xhci", .data = &tegra124_soc_config },
4250 /* TODO: we have to refine error handling in tegra_xhci_probe() */
4251 static int tegra_xhci_probe(struct platform_device *pdev)
4253 struct tegra_xhci_hcd *tegra;
4254 struct resource *res;
4259 const struct tegra_xusb_soc_config *soc_config;
4260 const struct of_device_id *match;
4262 BUILD_BUG_ON(sizeof(struct cfgtbl) != 256);
4267 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
4269 dev_err(&pdev->dev, "memory alloc failed\n");
4272 mutex_init(&tegra->sync_lock);
4273 spin_lock_init(&tegra->lock);
4274 mutex_init(&tegra->mbox_lock);
4276 tegra->init_done = false;
4278 tegra->bdata = devm_kzalloc(&pdev->dev, sizeof(
4279 struct tegra_xusb_board_data),
4281 if (!tegra->bdata) {
4282 dev_err(&pdev->dev, "memory alloc failed\n");
4285 tegra->cdata = devm_kzalloc(&pdev->dev, sizeof(
4286 struct tegra_xusb_chip_calib),
4288 if (!tegra->cdata) {
4289 dev_err(&pdev->dev, "memory alloc failed\n");
4292 match = of_match_device(tegra_xhci_of_match, &pdev->dev);
4294 dev_err(&pdev->dev, "Error: No device match found\n");
4297 soc_config = match->data;
4298 /* Right now device-tree probed devices don't get dma_mask set.
4299 * Since shared usb code relies on it, set it here for now.
4300 * Once we have dma capability bindings this can go away.
4302 tegra->tegra_xusb_dmamask = DMA_BIT_MASK(64);
4303 if (!pdev->dev.dma_mask)
4304 pdev->dev.dma_mask = &tegra->tegra_xusb_dmamask;
4307 tegra_xusb_read_calib_data(tegra);
4308 tegra_xusb_read_board_data(tegra);
4309 tegra->pdata = dev_get_platdata(&pdev->dev);
4310 tegra->bdata->portmap = tegra->pdata->portmap;
4311 if (tegra->bdata->portmap == NULL)
4313 tegra->bdata->lane_owner = tegra->pdata->lane_owner;
4314 tegra->soc_config = soc_config;
4315 tegra->ss_pwr_gated = false;
4316 tegra->host_pwr_gated = false;
4317 tegra->hc_in_elpg = false;
4318 tegra->hs_wake_event = false;
4319 tegra->host_resume_req = false;
4320 tegra->lp0_exit = false;
4322 /* request resource padctl base address */
4323 ret = tegra_xhci_request_mem_region(pdev, 3, &tegra->padctl_base);
4325 dev_err(&pdev->dev, "failed to map padctl\n");
4329 /* request resource fpci base address */
4330 ret = tegra_xhci_request_mem_region(pdev, 1, &tegra->fpci_base);
4332 dev_err(&pdev->dev, "failed to map fpci\n");
4336 /* request resource ipfs base address */
4337 ret = tegra_xhci_request_mem_region(pdev, 2, &tegra->ipfs_base);
4339 dev_err(&pdev->dev, "failed to map ipfs\n");
4343 ret = tegra_xusb_partitions_clk_init(tegra);
4346 "failed to initialize xusb partitions clocks\n");
4350 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P0) {
4351 tegra->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
4352 if (IS_ERR_OR_NULL(tegra->transceiver)) {
4353 dev_err(&pdev->dev, "failed to get usb phy\n");
4354 tegra->transceiver = NULL;
4358 /* Enable power rails to the PAD,VBUS
4359 * and pull-up voltage.Initialize the regulators
4361 ret = tegra_xusb_regulator_init(tegra, pdev);
4363 dev_err(&pdev->dev, "failed to initialize xusb regulator\n");
4364 goto err_deinit_xusb_partition_clk;
4367 /* Enable UTMIP, PLLU and PLLE */
4368 ret = tegra_usb2_clocks_init(tegra);
4370 dev_err(&pdev->dev, "error initializing usb2 clocks\n");
4371 goto err_deinit_tegra_xusb_regulator;
4374 /* tegra_unpowergate_partition also does partition reset deassert */
4375 ret = tegra_unpowergate_partition(TEGRA_POWERGATE_XUSBA);
4377 dev_err(&pdev->dev, "could not unpowergate xusba partition\n");
4379 /* tegra_unpowergate_partition also does partition reset deassert */
4380 ret = tegra_unpowergate_partition(TEGRA_POWERGATE_XUSBC);
4382 dev_err(&pdev->dev, "could not unpowergate xusbc partition\n");
4384 ret = tegra_enable_xusb_clk(tegra, pdev);
4386 dev_err(&pdev->dev, "could not enable partition clock\n");
4388 /* reset the pointer back to NULL. driver uses it */
4389 /* platform_set_drvdata(pdev, NULL); */
4391 /* request resource host base address */
4392 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4394 dev_err(&pdev->dev, "mem resource host doesn't exist\n");
4396 goto err_deinit_usb2_clocks;
4398 tegra->host_phy_base = res->start;
4399 tegra->host_phy_size = resource_size(res);
4401 tegra->host_phy_virt_base = devm_ioremap(&pdev->dev,
4402 res->start, resource_size(res));
4403 if (!tegra->host_phy_virt_base) {
4404 dev_err(&pdev->dev, "error mapping host phy memory\n");
4406 goto err_deinit_usb2_clocks;
4409 /* Setup IPFS access and BAR0 space */
4410 tegra_xhci_cfg(tegra);
4412 val = readl(tegra->fpci_base + XUSB_CFG_0);
4413 tegra->device_id = (val >> 16) & 0xffff;
4415 dev_info(&pdev->dev, "XUSB device id = 0x%x (%s)\n", tegra->device_id,
4416 (XUSB_DEVICE_ID_T114 == tegra->device_id) ? "T114" : "T124+");
4418 if (XUSB_DEVICE_ID_T114 == tegra->device_id) {
4419 tegra->padregs = &t114_padregs_offset;
4420 } else if (XUSB_DEVICE_ID_T124 == tegra->device_id) {
4421 tegra->padregs = &t124_padregs_offset;
4423 dev_info(&pdev->dev, "XUSB device_id neither T114 nor T124!\n");
4424 dev_info(&pdev->dev, "XUSB using T124 pad register offsets!\n");
4425 tegra->padregs = &t124_padregs_offset;
4428 /* calculate rctrl_val and tctrl_val once at boot time */
4429 /* Issue is only applicable for T114 */
4430 if (XUSB_DEVICE_ID_T114 == tegra->device_id)
4431 tegra_xhci_war_for_tctrl_rctrl(tegra);
4433 for_each_enabled_hsic_pad(pad, tegra)
4434 hsic_power_rail_enable(tegra);
4436 /* Program the XUSB pads to take ownership of ports */
4437 tegra_xhci_padctl_portmap_and_caps(tegra);
4439 /* Release XUSB wake logic state latching */
4440 tegra_xhci_ss_wake_signal(tegra->bdata->portmap, false);
4441 tegra_xhci_ss_vcore(tegra->bdata->portmap, false);
4443 /* Deassert reset to XUSB host, ss, dev clocks */
4444 tegra_periph_reset_deassert(tegra->host_clk);
4445 tegra_periph_reset_deassert(tegra->ss_clk);
4447 platform_set_drvdata(pdev, tegra);
4449 ret = init_firmware(tegra);
4451 dev_err(&pdev->dev, "failed to init firmware\n");
4453 goto err_deinit_firmware_log;
4456 if (use_bootloader_firmware) {
4457 ret = tegra_xhci_probe2(tegra);
4460 goto err_deinit_firmware;
4466 err_deinit_firmware:
4467 deinit_firmware(tegra);
4468 err_deinit_firmware_log:
4469 fw_log_deinit(tegra);
4470 err_deinit_usb2_clocks:
4471 tegra_usb2_clocks_deinit(tegra);
4472 err_deinit_tegra_xusb_regulator:
4473 tegra_xusb_regulator_deinit(tegra);
4474 err_deinit_xusb_partition_clk:
4475 if (tegra->transceiver)
4476 usb_unregister_notifier(tegra->transceiver, &tegra->otgnb);
4478 tegra_xusb_partitions_clk_deinit(tegra);
4483 static int tegra_xhci_probe2(struct tegra_xhci_hcd *tegra)
4485 struct platform_device *pdev = tegra->pdev;
4486 const struct hc_driver *driver;
4488 struct resource *res;
4490 struct xhci_hcd *xhci;
4491 struct usb_hcd *hcd;
4495 ret = load_firmware(tegra, true /* do reset ARU */);
4497 dev_err(&pdev->dev, "failed to load firmware\n");
4502 device_init_wakeup(&pdev->dev, 1);
4503 driver = &tegra_plat_xhci_driver;
4505 hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
4507 dev_err(&pdev->dev, "failed to create usb2 hcd\n");
4511 /* request resource host base address */
4512 ret = tegra_xhci_request_mem_region(pdev, 0, &hcd->regs);
4514 dev_err(&pdev->dev, "failed to map host\n");
4515 goto err_put_usb2_hcd;
4517 hcd->rsrc_start = tegra->host_phy_base;
4518 hcd->rsrc_len = tegra->host_phy_size;
4520 /* Register interrupt handler for HOST */
4521 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
4523 dev_err(&pdev->dev, "irq resource host doesn't exist\n");
4525 goto err_put_usb2_hcd;
4529 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
4531 dev_err(&pdev->dev, "failed to add usb2hcd, error = %d\n", ret);
4532 goto err_put_usb2_hcd;
4535 /* USB 2.0 roothub is stored in the platform_device now. */
4536 hcd = dev_get_drvdata(&pdev->dev);
4537 xhci = hcd_to_xhci(hcd);
4539 platform_set_drvdata(pdev, tegra);
4541 if (tegra->bdata->portmap & TEGRA_XUSB_USB2_P0) {
4542 if (!IS_ERR_OR_NULL(tegra->transceiver)) {
4543 otg_set_host(tegra->transceiver->otg, &hcd->self);
4544 tegra->otgnb.notifier_call = tegra_xhci_otg_notify;
4545 usb_register_notifier(tegra->transceiver,
4550 xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
4551 dev_name(&pdev->dev), hcd);
4552 if (!xhci->shared_hcd) {
4553 dev_err(&pdev->dev, "failed to create usb3 hcd\n");
4555 goto err_remove_usb2_hcd;
4559 * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
4560 * is called by usb_add_hcd().
4562 *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
4564 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
4566 dev_err(&pdev->dev, "failed to add usb3hcd, error = %d\n", ret);
4567 goto err_put_usb3_hcd;
4570 device_init_wakeup(&hcd->self.root_hub->dev, 1);
4571 device_init_wakeup(&xhci->shared_hcd->self.root_hub->dev, 1);
4573 /* do mailbox related initializations */
4574 tegra->mbox_owner = 0xffff;
4575 INIT_WORK(&tegra->mbox_work, tegra_xhci_process_mbox_message);
4577 tegra_xhci_enable_fw_message(tegra);
4579 /* do ss partition elpg exit related initialization */
4580 INIT_WORK(&tegra->ss_elpg_exit_work, ss_partition_elpg_exit_work);
4582 /* do host partition elpg exit related initialization */
4583 INIT_WORK(&tegra->host_elpg_exit_work, host_partition_elpg_exit_work);
4585 /* Register interrupt handler for SMI line to handle mailbox
4586 * interrupt from firmware
4589 ret = tegra_xhci_request_irq(pdev, 1, tegra_xhci_smi_irq,
4590 IRQF_SHARED, "tegra_xhci_mbox_irq", &tegra->smi_irq);
4592 goto err_remove_usb3_hcd;
4594 /* Register interrupt handler for PADCTRL line to
4595 * handle wake on connect irqs interrupt from
4598 ret = tegra_xhci_request_irq(pdev, 2, tegra_xhci_padctl_irq,
4599 IRQF_SHARED | IRQF_TRIGGER_HIGH,
4600 "tegra_xhci_padctl_irq", &tegra->padctl_irq);
4602 goto err_remove_usb3_hcd;
4604 /* Register interrupt wake handler for USB2 */
4605 ret = tegra_xhci_request_irq(pdev, 4, pmc_usb_phy_wake_isr,
4606 IRQF_SHARED | IRQF_TRIGGER_HIGH, "pmc_usb_phy_wake_isr",
4609 goto err_remove_usb3_hcd;
4611 /* Register interrupt wake handler for USB3 */
4612 ret = tegra_xhci_request_irq(pdev, 3, pmc_usb_phy_wake_isr,
4613 IRQF_SHARED | IRQF_TRIGGER_HIGH, "pmc_usb_phy_wake_isr",
4616 goto err_remove_usb3_hcd;
4618 for (port = 0; port < XUSB_SS_PORT_COUNT; port++) {
4619 tegra->ctle_ctx_saved[port] = false;
4620 tegra->dfe_ctx_saved[port] = false;
4623 hsic_pad_pretend_connect(tegra);
4625 tegra_xhci_debug_read_pads(tegra);
4626 utmi_phy_pad_enable();
4627 utmi_phy_iddq_override(false);
4629 tegra_pd_add_device(&pdev->dev);
4630 pm_runtime_enable(&pdev->dev);
4632 tegra->init_done = true;
4636 err_remove_usb3_hcd:
4637 usb_remove_hcd(xhci->shared_hcd);
4639 usb_put_hcd(xhci->shared_hcd);
4640 err_remove_usb2_hcd:
4642 usb_remove_hcd(hcd);
4649 static int tegra_xhci_remove(struct platform_device *pdev)
4651 struct tegra_xhci_hcd *tegra = platform_get_drvdata(pdev);
4657 mutex_lock(&tegra->sync_lock);
4659 for_each_enabled_hsic_pad(pad, tegra) {
4660 hsic_pad_disable(tegra, pad);
4661 hsic_power_rail_disable(tegra);
4664 if (tegra->init_done) {
4665 struct xhci_hcd *xhci = NULL;
4666 struct usb_hcd *hcd = NULL;
4669 hcd = xhci_to_hcd(xhci);
4671 devm_free_irq(&pdev->dev, tegra->usb3_irq, tegra);
4672 devm_free_irq(&pdev->dev, tegra->padctl_irq, tegra);
4673 devm_free_irq(&pdev->dev, tegra->smi_irq, tegra);
4674 usb_remove_hcd(xhci->shared_hcd);
4675 usb_put_hcd(xhci->shared_hcd);
4676 usb_remove_hcd(hcd);
4681 deinit_firmware(tegra);
4682 fw_log_deinit(tegra);
4684 tegra_xusb_regulator_deinit(tegra);
4686 if (tegra->transceiver)
4687 usb_unregister_notifier(tegra->transceiver, &tegra->otgnb);
4689 tegra_usb2_clocks_deinit(tegra);
4690 if (!tegra->hc_in_elpg)
4691 tegra_xusb_partitions_clk_deinit(tegra);
4693 utmi_phy_pad_disable();
4694 utmi_phy_iddq_override(true);
4696 tegra_pd_remove_device(&pdev->dev);
4697 platform_set_drvdata(pdev, NULL);
4699 mutex_unlock(&tegra->sync_lock);
4704 static void tegra_xhci_shutdown(struct platform_device *pdev)
4706 struct tegra_xhci_hcd *tegra = platform_get_drvdata(pdev);
4707 struct xhci_hcd *xhci = NULL;
4708 struct usb_hcd *hcd = NULL;
4713 if (tegra->hc_in_elpg) {
4714 pmc_disable_bus_ctrl(tegra);
4717 hcd = xhci_to_hcd(xhci);
4722 static struct platform_driver tegra_xhci_driver = {
4723 .probe = tegra_xhci_probe,
4724 .remove = tegra_xhci_remove,
4725 .shutdown = tegra_xhci_shutdown,
4727 .suspend = tegra_xhci_suspend,
4728 .resume = tegra_xhci_resume,
4731 .name = "tegra-xhci",
4732 .of_match_table = tegra_xhci_of_match,
4735 MODULE_ALIAS("platform:tegra-xhci");
4737 int tegra_xhci_register_plat(void)
4739 return platform_driver_register(&tegra_xhci_driver);
4742 void tegra_xhci_unregister_plat(void)
4744 platform_driver_unregister(&tegra_xhci_driver);