xref: /linux/drivers/usb/host/xhci-tegra.c (revision 68a052239fc4b351e961f698b824f7654a346091)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVIDIA Tegra xHCI host controller driver
4  *
5  * Copyright (c) 2014-2020, NVIDIA CORPORATION. All rights reserved.
6  * Copyright (C) 2014 Google, Inc.
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/firmware.h>
13 #include <linux/interrupt.h>
14 #include <linux/iopoll.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/of_irq.h>
19 #include <linux/phy/phy.h>
20 #include <linux/phy/tegra/xusb.h>
21 #include <linux/platform_device.h>
22 #include <linux/usb/ch9.h>
23 #include <linux/pm.h>
24 #include <linux/pm_domain.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/reset.h>
28 #include <linux/slab.h>
29 #include <linux/string_choices.h>
30 #include <linux/usb/otg.h>
31 #include <linux/usb/phy.h>
32 #include <linux/usb/role.h>
33 #include <soc/tegra/pmc.h>
34 
35 #include "xhci.h"
36 
37 #define TEGRA_XHCI_SS_HIGH_SPEED 120000000
38 #define TEGRA_XHCI_SS_LOW_SPEED   12000000
39 
40 /* FPCI CFG registers */
41 #define XUSB_CFG_1				0x004
42 #define  XUSB_IO_SPACE_EN			BIT(0)
43 #define  XUSB_MEM_SPACE_EN			BIT(1)
44 #define  XUSB_BUS_MASTER_EN			BIT(2)
45 #define XUSB_CFG_4				0x010
46 #define  XUSB_BASE_ADDR_SHIFT			15
47 #define  XUSB_BASE_ADDR_MASK			0x1ffff
48 #define XUSB_CFG_7				0x01c
49 #define  XUSB_BASE2_ADDR_SHIFT			16
50 #define  XUSB_BASE2_ADDR_MASK			0xffff
51 #define XUSB_CFG_16				0x040
52 #define XUSB_CFG_24				0x060
53 #define XUSB_CFG_AXI_CFG			0x0f8
54 #define XUSB_CFG_ARU_C11_CSBRANGE		0x41c
55 #define XUSB_CFG_ARU_CONTEXT			0x43c
56 #define XUSB_CFG_ARU_CONTEXT_HS_PLS		0x478
57 #define XUSB_CFG_ARU_CONTEXT_FS_PLS		0x47c
58 #define XUSB_CFG_ARU_CONTEXT_HSFS_SPEED		0x480
59 #define XUSB_CFG_ARU_CONTEXT_HSFS_PP		0x484
60 #define XUSB_CFG_CSB_BASE_ADDR			0x800
61 
62 /* FPCI mailbox registers */
63 /* XUSB_CFG_ARU_MBOX_CMD */
64 #define  MBOX_DEST_FALC				BIT(27)
65 #define  MBOX_DEST_PME				BIT(28)
66 #define  MBOX_DEST_SMI				BIT(29)
67 #define  MBOX_DEST_XHCI				BIT(30)
68 #define  MBOX_INT_EN				BIT(31)
69 /* XUSB_CFG_ARU_MBOX_DATA_IN and XUSB_CFG_ARU_MBOX_DATA_OUT */
70 #define  CMD_DATA_SHIFT				0
71 #define  CMD_DATA_MASK				0xffffff
72 #define  CMD_TYPE_SHIFT				24
73 #define  CMD_TYPE_MASK				0xff
74 /* XUSB_CFG_ARU_MBOX_OWNER */
75 #define  MBOX_OWNER_NONE			0
76 #define  MBOX_OWNER_FW				1
77 #define  MBOX_OWNER_SW				2
78 #define XUSB_CFG_ARU_SMI_INTR			0x428
79 #define  MBOX_SMI_INTR_FW_HANG			BIT(1)
80 #define  MBOX_SMI_INTR_EN			BIT(3)
81 
82 /* BAR2 registers */
83 #define XUSB_BAR2_ARU_MBOX_CMD			0x004
84 #define XUSB_BAR2_ARU_MBOX_DATA_IN		0x008
85 #define XUSB_BAR2_ARU_MBOX_DATA_OUT		0x00c
86 #define XUSB_BAR2_ARU_MBOX_OWNER		0x010
87 #define XUSB_BAR2_ARU_SMI_INTR			0x014
88 #define XUSB_BAR2_ARU_SMI_ARU_FW_SCRATCH_DATA0	0x01c
89 #define XUSB_BAR2_ARU_IFRDMA_CFG0		0x0e0
90 #define XUSB_BAR2_ARU_IFRDMA_CFG1		0x0e4
91 #define XUSB_BAR2_ARU_IFRDMA_STREAMID_FIELD	0x0e8
92 #define XUSB_BAR2_ARU_C11_CSBRANGE		0x9c
93 #define XUSB_BAR2_ARU_FW_SCRATCH		0x1000
94 #define XUSB_BAR2_CSB_BASE_ADDR			0x2000
95 
96 /* IPFS registers */
97 #define IPFS_XUSB_HOST_MSI_BAR_SZ_0		0x0c0
98 #define IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0		0x0c4
99 #define IPFS_XUSB_HOST_MSI_FPCI_BAR_ST_0	0x0c8
100 #define IPFS_XUSB_HOST_MSI_VEC0_0		0x100
101 #define IPFS_XUSB_HOST_MSI_EN_VEC0_0		0x140
102 #define IPFS_XUSB_HOST_CONFIGURATION_0		0x180
103 #define  IPFS_EN_FPCI				BIT(0)
104 #define IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0	0x184
105 #define IPFS_XUSB_HOST_INTR_MASK_0		0x188
106 #define  IPFS_IP_INT_MASK			BIT(16)
107 #define IPFS_XUSB_HOST_INTR_ENABLE_0		0x198
108 #define IPFS_XUSB_HOST_UFPCI_CONFIG_0		0x19c
109 #define IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0	0x1bc
110 #define IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0		0x1dc
111 
112 #define CSB_PAGE_SELECT_MASK			0x7fffff
113 #define CSB_PAGE_SELECT_SHIFT			9
114 #define CSB_PAGE_OFFSET_MASK			0x1ff
115 #define CSB_PAGE_SELECT(addr)	((addr) >> (CSB_PAGE_SELECT_SHIFT) &	\
116 				 CSB_PAGE_SELECT_MASK)
117 #define CSB_PAGE_OFFSET(addr)	((addr) & CSB_PAGE_OFFSET_MASK)
118 
119 /* Falcon CSB registers */
120 #define XUSB_FALC_CPUCTL			0x100
121 #define  CPUCTL_STARTCPU			BIT(1)
122 #define  CPUCTL_STATE_HALTED			BIT(4)
123 #define  CPUCTL_STATE_STOPPED			BIT(5)
124 #define XUSB_FALC_BOOTVEC			0x104
125 #define XUSB_FALC_DMACTL			0x10c
126 #define XUSB_FALC_IMFILLRNG1			0x154
127 #define  IMFILLRNG1_TAG_MASK			0xffff
128 #define  IMFILLRNG1_TAG_LO_SHIFT		0
129 #define  IMFILLRNG1_TAG_HI_SHIFT		16
130 #define XUSB_FALC_IMFILLCTL			0x158
131 
132 /* CSB ARU registers */
133 #define XUSB_CSB_ARU_SCRATCH0			0x100100
134 
135 /* MP CSB registers */
136 #define XUSB_CSB_MP_ILOAD_ATTR			0x101a00
137 #define XUSB_CSB_MP_ILOAD_BASE_LO		0x101a04
138 #define XUSB_CSB_MP_ILOAD_BASE_HI		0x101a08
139 #define XUSB_CSB_MP_L2IMEMOP_SIZE		0x101a10
140 #define  L2IMEMOP_SIZE_SRC_OFFSET_SHIFT		8
141 #define  L2IMEMOP_SIZE_SRC_OFFSET_MASK		0x3ff
142 #define  L2IMEMOP_SIZE_SRC_COUNT_SHIFT		24
143 #define  L2IMEMOP_SIZE_SRC_COUNT_MASK		0xff
144 #define XUSB_CSB_MP_L2IMEMOP_TRIG		0x101a14
145 #define  L2IMEMOP_ACTION_SHIFT			24
146 #define  L2IMEMOP_INVALIDATE_ALL		(0x40 << L2IMEMOP_ACTION_SHIFT)
147 #define  L2IMEMOP_LOAD_LOCKED_RESULT		(0x11 << L2IMEMOP_ACTION_SHIFT)
148 #define XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT	0x101a18
149 #define  L2IMEMOP_RESULT_VLD			BIT(31)
150 #define XUSB_CSB_MP_APMAP			0x10181c
151 #define  APMAP_BOOTPATH				BIT(31)
152 
153 #define IMEM_BLOCK_SIZE				256
154 
155 #define FW_IOCTL_TYPE_SHIFT			24
156 #define FW_IOCTL_CFGTBL_READ		17
157 
158 #define WAKE_IRQ_START_INDEX			2
159 
160 struct tegra_xusb_fw_header {
161 	__le32 boot_loadaddr_in_imem;
162 	__le32 boot_codedfi_offset;
163 	__le32 boot_codetag;
164 	__le32 boot_codesize;
165 	__le32 phys_memaddr;
166 	__le16 reqphys_memsize;
167 	__le16 alloc_phys_memsize;
168 	__le32 rodata_img_offset;
169 	__le32 rodata_section_start;
170 	__le32 rodata_section_end;
171 	__le32 main_fnaddr;
172 	__le32 fwimg_cksum;
173 	__le32 fwimg_created_time;
174 	__le32 imem_resident_start;
175 	__le32 imem_resident_end;
176 	__le32 idirect_start;
177 	__le32 idirect_end;
178 	__le32 l2_imem_start;
179 	__le32 l2_imem_end;
180 	__le32 version_id;
181 	u8 init_ddirect;
182 	u8 reserved[3];
183 	__le32 phys_addr_log_buffer;
184 	__le32 total_log_entries;
185 	__le32 dequeue_ptr;
186 	__le32 dummy_var[2];
187 	__le32 fwimg_len;
188 	u8 magic[8];
189 	__le32 ss_low_power_entry_timeout;
190 	u8 num_hsic_port;
191 	u8 padding[139]; /* Pad to 256 bytes */
192 };
193 
194 struct tegra_xusb_phy_type {
195 	const char *name;
196 	unsigned int num;
197 };
198 
199 struct tegra_xusb_mbox_regs {
200 	u16 cmd;
201 	u16 data_in;
202 	u16 data_out;
203 	u16 owner;
204 	u16 smi_intr;
205 };
206 
207 struct tegra_xusb_context_soc {
208 	struct {
209 		const unsigned int *offsets;
210 		unsigned int num_offsets;
211 	} ipfs;
212 
213 	struct {
214 		const unsigned int *offsets;
215 		unsigned int num_offsets;
216 	} fpci;
217 };
218 
219 struct tegra_xusb;
220 struct tegra_xusb_soc_ops {
221 	u32 (*mbox_reg_readl)(struct tegra_xusb *tegra, unsigned int offset);
222 	void (*mbox_reg_writel)(struct tegra_xusb *tegra, u32 value, unsigned int offset);
223 	u32 (*csb_reg_readl)(struct tegra_xusb *tegra, unsigned int offset);
224 	void (*csb_reg_writel)(struct tegra_xusb *tegra, u32 value, unsigned int offset);
225 };
226 
227 struct tegra_xusb_soc {
228 	const char *firmware;
229 	const char * const *supply_names;
230 	unsigned int num_supplies;
231 	const struct tegra_xusb_phy_type *phy_types;
232 	unsigned int num_types;
233 	unsigned int max_num_wakes;
234 	const struct tegra_xusb_context_soc *context;
235 
236 	struct {
237 		struct {
238 			unsigned int offset;
239 			unsigned int count;
240 		} usb2, ulpi, hsic, usb3;
241 	} ports;
242 
243 	struct tegra_xusb_mbox_regs mbox;
244 	const struct tegra_xusb_soc_ops *ops;
245 
246 	bool scale_ss_clock;
247 	bool has_ipfs;
248 	bool lpm_support;
249 	bool otg_reset_sspi;
250 
251 	bool has_bar2;
252 };
253 
254 struct tegra_xusb_context {
255 	u32 *ipfs;
256 	u32 *fpci;
257 };
258 
259 struct tegra_xusb {
260 	struct device *dev;
261 	void __iomem *regs;
262 	struct usb_hcd *hcd;
263 
264 	struct mutex lock;
265 
266 	int xhci_irq;
267 	int mbox_irq;
268 	int padctl_irq;
269 	int *wake_irqs;
270 
271 	void __iomem *ipfs_base;
272 	void __iomem *fpci_base;
273 	void __iomem *bar2_base;
274 	struct resource *bar2;
275 
276 	const struct tegra_xusb_soc *soc;
277 
278 	struct regulator_bulk_data *supplies;
279 
280 	struct tegra_xusb_padctl *padctl;
281 
282 	struct clk *host_clk;
283 	struct clk *falcon_clk;
284 	struct clk *ss_clk;
285 	struct clk *ss_src_clk;
286 	struct clk *hs_src_clk;
287 	struct clk *fs_src_clk;
288 	struct clk *pll_u_480m;
289 	struct clk *clk_m;
290 	struct clk *pll_e;
291 
292 	struct reset_control *host_rst;
293 	struct reset_control *ss_rst;
294 
295 	struct device *genpd_dev_host;
296 	struct device *genpd_dev_ss;
297 	bool use_genpd;
298 
299 	struct phy **phys;
300 	unsigned int num_phys;
301 
302 	struct usb_phy **usbphy;
303 	unsigned int num_usb_phys;
304 	int otg_usb2_port;
305 	int otg_usb3_port;
306 	bool host_mode;
307 	struct notifier_block id_nb;
308 	struct work_struct id_work;
309 
310 	/* Firmware loading related */
311 	struct {
312 		size_t size;
313 		void *virt;
314 		dma_addr_t phys;
315 	} fw;
316 
317 	bool suspended;
318 	struct tegra_xusb_context context;
319 	u8 lp0_utmi_pad_mask;
320 	int num_wakes;
321 };
322 
323 static struct hc_driver __read_mostly tegra_xhci_hc_driver;
324 
325 static inline u32 fpci_readl(struct tegra_xusb *tegra, unsigned int offset)
326 {
327 	return readl(tegra->fpci_base + offset);
328 }
329 
330 static inline void fpci_writel(struct tegra_xusb *tegra, u32 value,
331 			       unsigned int offset)
332 {
333 	writel(value, tegra->fpci_base + offset);
334 }
335 
336 static inline u32 ipfs_readl(struct tegra_xusb *tegra, unsigned int offset)
337 {
338 	return readl(tegra->ipfs_base + offset);
339 }
340 
341 static inline void ipfs_writel(struct tegra_xusb *tegra, u32 value,
342 			       unsigned int offset)
343 {
344 	writel(value, tegra->ipfs_base + offset);
345 }
346 
347 static inline u32 bar2_readl(struct tegra_xusb *tegra, unsigned int offset)
348 {
349 	return readl(tegra->bar2_base + offset);
350 }
351 
352 static inline void bar2_writel(struct tegra_xusb *tegra, u32 value,
353 			       unsigned int offset)
354 {
355 	writel(value, tegra->bar2_base + offset);
356 }
357 
358 static u32 csb_readl(struct tegra_xusb *tegra, unsigned int offset)
359 {
360 	const struct tegra_xusb_soc_ops *ops = tegra->soc->ops;
361 
362 	return ops->csb_reg_readl(tegra, offset);
363 }
364 
365 static void csb_writel(struct tegra_xusb *tegra, u32 value,
366 		       unsigned int offset)
367 {
368 	const struct tegra_xusb_soc_ops *ops = tegra->soc->ops;
369 
370 	ops->csb_reg_writel(tegra, value, offset);
371 }
372 
373 static u32 fpci_csb_readl(struct tegra_xusb *tegra, unsigned int offset)
374 {
375 	u32 page = CSB_PAGE_SELECT(offset);
376 	u32 ofs = CSB_PAGE_OFFSET(offset);
377 
378 	fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE);
379 
380 	return fpci_readl(tegra, XUSB_CFG_CSB_BASE_ADDR + ofs);
381 }
382 
383 static void fpci_csb_writel(struct tegra_xusb *tegra, u32 value,
384 			    unsigned int offset)
385 {
386 	u32 page = CSB_PAGE_SELECT(offset);
387 	u32 ofs = CSB_PAGE_OFFSET(offset);
388 
389 	fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE);
390 	fpci_writel(tegra, value, XUSB_CFG_CSB_BASE_ADDR + ofs);
391 }
392 
393 static u32 bar2_csb_readl(struct tegra_xusb *tegra, unsigned int offset)
394 {
395 	u32 page = CSB_PAGE_SELECT(offset);
396 	u32 ofs = CSB_PAGE_OFFSET(offset);
397 
398 	bar2_writel(tegra, page, XUSB_BAR2_ARU_C11_CSBRANGE);
399 
400 	return bar2_readl(tegra, XUSB_BAR2_CSB_BASE_ADDR + ofs);
401 }
402 
403 static void bar2_csb_writel(struct tegra_xusb *tegra, u32 value,
404 			    unsigned int offset)
405 {
406 	u32 page = CSB_PAGE_SELECT(offset);
407 	u32 ofs = CSB_PAGE_OFFSET(offset);
408 
409 	bar2_writel(tegra, page, XUSB_BAR2_ARU_C11_CSBRANGE);
410 	bar2_writel(tegra, value, XUSB_BAR2_CSB_BASE_ADDR + ofs);
411 }
412 
413 static int tegra_xusb_set_ss_clk(struct tegra_xusb *tegra,
414 				 unsigned long rate)
415 {
416 	unsigned long new_parent_rate, old_parent_rate;
417 	struct clk *clk = tegra->ss_src_clk;
418 	unsigned int div;
419 	int err;
420 
421 	if (clk_get_rate(clk) == rate)
422 		return 0;
423 
424 	switch (rate) {
425 	case TEGRA_XHCI_SS_HIGH_SPEED:
426 		/*
427 		 * Reparent to PLLU_480M. Set divider first to avoid
428 		 * overclocking.
429 		 */
430 		old_parent_rate = clk_get_rate(clk_get_parent(clk));
431 		new_parent_rate = clk_get_rate(tegra->pll_u_480m);
432 		div = new_parent_rate / rate;
433 
434 		err = clk_set_rate(clk, old_parent_rate / div);
435 		if (err)
436 			return err;
437 
438 		err = clk_set_parent(clk, tegra->pll_u_480m);
439 		if (err)
440 			return err;
441 
442 		/*
443 		 * The rate should already be correct, but set it again just
444 		 * to be sure.
445 		 */
446 		err = clk_set_rate(clk, rate);
447 		if (err)
448 			return err;
449 
450 		break;
451 
452 	case TEGRA_XHCI_SS_LOW_SPEED:
453 		/* Reparent to CLK_M */
454 		err = clk_set_parent(clk, tegra->clk_m);
455 		if (err)
456 			return err;
457 
458 		err = clk_set_rate(clk, rate);
459 		if (err)
460 			return err;
461 
462 		break;
463 
464 	default:
465 		dev_err(tegra->dev, "Invalid SS rate: %lu Hz\n", rate);
466 		return -EINVAL;
467 	}
468 
469 	if (clk_get_rate(clk) != rate) {
470 		dev_err(tegra->dev, "SS clock doesn't match requested rate\n");
471 		return -EINVAL;
472 	}
473 
474 	return 0;
475 }
476 
477 static unsigned long extract_field(u32 value, unsigned int start,
478 				   unsigned int count)
479 {
480 	return (value >> start) & ((1 << count) - 1);
481 }
482 
483 /* Command requests from the firmware */
484 enum tegra_xusb_mbox_cmd {
485 	MBOX_CMD_MSG_ENABLED = 1,
486 	MBOX_CMD_INC_FALC_CLOCK,
487 	MBOX_CMD_DEC_FALC_CLOCK,
488 	MBOX_CMD_INC_SSPI_CLOCK,
489 	MBOX_CMD_DEC_SSPI_CLOCK,
490 	MBOX_CMD_SET_BW, /* no ACK/NAK required */
491 	MBOX_CMD_SET_SS_PWR_GATING,
492 	MBOX_CMD_SET_SS_PWR_UNGATING,
493 	MBOX_CMD_SAVE_DFE_CTLE_CTX,
494 	MBOX_CMD_AIRPLANE_MODE_ENABLED, /* unused */
495 	MBOX_CMD_AIRPLANE_MODE_DISABLED, /* unused */
496 	MBOX_CMD_START_HSIC_IDLE,
497 	MBOX_CMD_STOP_HSIC_IDLE,
498 	MBOX_CMD_DBC_WAKE_STACK, /* unused */
499 	MBOX_CMD_HSIC_PRETEND_CONNECT,
500 	MBOX_CMD_RESET_SSPI,
501 	MBOX_CMD_DISABLE_SS_LFPS_DETECTION,
502 	MBOX_CMD_ENABLE_SS_LFPS_DETECTION,
503 
504 	MBOX_CMD_MAX,
505 
506 	/* Response message to above commands */
507 	MBOX_CMD_ACK = 128,
508 	MBOX_CMD_NAK
509 };
510 
511 struct tegra_xusb_mbox_msg {
512 	u32 cmd;
513 	u32 data;
514 };
515 
516 static inline u32 tegra_xusb_mbox_pack(const struct tegra_xusb_mbox_msg *msg)
517 {
518 	return (msg->cmd & CMD_TYPE_MASK) << CMD_TYPE_SHIFT |
519 	       (msg->data & CMD_DATA_MASK) << CMD_DATA_SHIFT;
520 }
521 static inline void tegra_xusb_mbox_unpack(struct tegra_xusb_mbox_msg *msg,
522 					  u32 value)
523 {
524 	msg->cmd = (value >> CMD_TYPE_SHIFT) & CMD_TYPE_MASK;
525 	msg->data = (value >> CMD_DATA_SHIFT) & CMD_DATA_MASK;
526 }
527 
528 static bool tegra_xusb_mbox_cmd_requires_ack(enum tegra_xusb_mbox_cmd cmd)
529 {
530 	switch (cmd) {
531 	case MBOX_CMD_SET_BW:
532 	case MBOX_CMD_ACK:
533 	case MBOX_CMD_NAK:
534 		return false;
535 
536 	default:
537 		return true;
538 	}
539 }
540 
541 static int tegra_xusb_mbox_send(struct tegra_xusb *tegra,
542 				const struct tegra_xusb_mbox_msg *msg)
543 {
544 	const struct tegra_xusb_soc_ops *ops = tegra->soc->ops;
545 	bool wait_for_idle = false;
546 	u32 value;
547 
548 	/*
549 	 * Acquire the mailbox. The firmware still owns the mailbox for
550 	 * ACK/NAK messages.
551 	 */
552 	if (!(msg->cmd == MBOX_CMD_ACK || msg->cmd == MBOX_CMD_NAK)) {
553 		value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner);
554 		if (value != MBOX_OWNER_NONE) {
555 			dev_err(tegra->dev, "mailbox is busy\n");
556 			return -EBUSY;
557 		}
558 
559 		ops->mbox_reg_writel(tegra, MBOX_OWNER_SW, tegra->soc->mbox.owner);
560 
561 		value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner);
562 		if (value != MBOX_OWNER_SW) {
563 			dev_err(tegra->dev, "failed to acquire mailbox\n");
564 			return -EBUSY;
565 		}
566 
567 		wait_for_idle = true;
568 	}
569 
570 	value = tegra_xusb_mbox_pack(msg);
571 	ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.data_in);
572 
573 	value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.cmd);
574 	value |= MBOX_INT_EN | MBOX_DEST_FALC;
575 	ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.cmd);
576 
577 	if (wait_for_idle) {
578 		unsigned long timeout = jiffies + msecs_to_jiffies(250);
579 
580 		while (time_before(jiffies, timeout)) {
581 			value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner);
582 			if (value == MBOX_OWNER_NONE)
583 				break;
584 
585 			usleep_range(10, 20);
586 		}
587 
588 		if (time_after(jiffies, timeout))
589 			value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner);
590 
591 		if (value != MBOX_OWNER_NONE)
592 			return -ETIMEDOUT;
593 	}
594 
595 	return 0;
596 }
597 
598 static irqreturn_t tegra_xusb_mbox_irq(int irq, void *data)
599 {
600 	struct tegra_xusb *tegra = data;
601 	const struct tegra_xusb_soc_ops *ops = tegra->soc->ops;
602 	u32 value;
603 
604 	/* clear mailbox interrupts */
605 	value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.smi_intr);
606 	ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.smi_intr);
607 
608 	if (value & MBOX_SMI_INTR_FW_HANG)
609 		dev_err(tegra->dev, "controller firmware hang\n");
610 
611 	return IRQ_WAKE_THREAD;
612 }
613 
614 static void tegra_xusb_mbox_handle(struct tegra_xusb *tegra,
615 				   const struct tegra_xusb_mbox_msg *msg)
616 {
617 	struct tegra_xusb_padctl *padctl = tegra->padctl;
618 	const struct tegra_xusb_soc *soc = tegra->soc;
619 	struct device *dev = tegra->dev;
620 	struct tegra_xusb_mbox_msg rsp;
621 	unsigned long mask;
622 	unsigned int port;
623 	bool idle, enable;
624 	int err = 0;
625 
626 	memset(&rsp, 0, sizeof(rsp));
627 
628 	switch (msg->cmd) {
629 	case MBOX_CMD_INC_FALC_CLOCK:
630 	case MBOX_CMD_DEC_FALC_CLOCK:
631 		rsp.data = clk_get_rate(tegra->falcon_clk) / 1000;
632 		if (rsp.data != msg->data)
633 			rsp.cmd = MBOX_CMD_NAK;
634 		else
635 			rsp.cmd = MBOX_CMD_ACK;
636 
637 		break;
638 
639 	case MBOX_CMD_INC_SSPI_CLOCK:
640 	case MBOX_CMD_DEC_SSPI_CLOCK:
641 		if (tegra->soc->scale_ss_clock) {
642 			err = tegra_xusb_set_ss_clk(tegra, msg->data * 1000);
643 			if (err < 0)
644 				rsp.cmd = MBOX_CMD_NAK;
645 			else
646 				rsp.cmd = MBOX_CMD_ACK;
647 
648 			rsp.data = clk_get_rate(tegra->ss_src_clk) / 1000;
649 		} else {
650 			rsp.cmd = MBOX_CMD_ACK;
651 			rsp.data = msg->data;
652 		}
653 
654 		break;
655 
656 	case MBOX_CMD_SET_BW:
657 		/*
658 		 * TODO: Request bandwidth once EMC scaling is supported.
659 		 * Ignore for now since ACK/NAK is not required for SET_BW
660 		 * messages.
661 		 */
662 		break;
663 
664 	case MBOX_CMD_SAVE_DFE_CTLE_CTX:
665 		err = tegra_xusb_padctl_usb3_save_context(padctl, msg->data);
666 		if (err < 0) {
667 			dev_err(dev, "failed to save context for USB3#%u: %d\n",
668 				msg->data, err);
669 			rsp.cmd = MBOX_CMD_NAK;
670 		} else {
671 			rsp.cmd = MBOX_CMD_ACK;
672 		}
673 
674 		rsp.data = msg->data;
675 		break;
676 
677 	case MBOX_CMD_START_HSIC_IDLE:
678 	case MBOX_CMD_STOP_HSIC_IDLE:
679 		if (msg->cmd == MBOX_CMD_STOP_HSIC_IDLE)
680 			idle = false;
681 		else
682 			idle = true;
683 
684 		mask = extract_field(msg->data, 1 + soc->ports.hsic.offset,
685 				     soc->ports.hsic.count);
686 
687 		for_each_set_bit(port, &mask, 32) {
688 			err = tegra_xusb_padctl_hsic_set_idle(padctl, port,
689 							      idle);
690 			if (err < 0)
691 				break;
692 		}
693 
694 		if (err < 0) {
695 			dev_err(dev, "failed to set HSIC#%u %s: %d\n", port,
696 				idle ? "idle" : "busy", err);
697 			rsp.cmd = MBOX_CMD_NAK;
698 		} else {
699 			rsp.cmd = MBOX_CMD_ACK;
700 		}
701 
702 		rsp.data = msg->data;
703 		break;
704 
705 	case MBOX_CMD_DISABLE_SS_LFPS_DETECTION:
706 	case MBOX_CMD_ENABLE_SS_LFPS_DETECTION:
707 		if (msg->cmd == MBOX_CMD_DISABLE_SS_LFPS_DETECTION)
708 			enable = false;
709 		else
710 			enable = true;
711 
712 		mask = extract_field(msg->data, 1 + soc->ports.usb3.offset,
713 				     soc->ports.usb3.count);
714 
715 		for_each_set_bit(port, &mask, soc->ports.usb3.count) {
716 			err = tegra_xusb_padctl_usb3_set_lfps_detect(padctl,
717 								     port,
718 								     enable);
719 			if (err < 0)
720 				break;
721 
722 			/*
723 			 * wait 500us for LFPS detector to be disabled before
724 			 * sending ACK
725 			 */
726 			if (!enable)
727 				usleep_range(500, 1000);
728 		}
729 
730 		if (err < 0) {
731 			dev_err(dev,
732 				"failed to %s LFPS detection on USB3#%u: %d\n",
733 				str_enable_disable(enable), port, err);
734 			rsp.cmd = MBOX_CMD_NAK;
735 		} else {
736 			rsp.cmd = MBOX_CMD_ACK;
737 		}
738 
739 		rsp.data = msg->data;
740 		break;
741 
742 	default:
743 		dev_warn(dev, "unknown message: %#x\n", msg->cmd);
744 		break;
745 	}
746 
747 	if (rsp.cmd) {
748 		const char *cmd = (rsp.cmd == MBOX_CMD_ACK) ? "ACK" : "NAK";
749 
750 		err = tegra_xusb_mbox_send(tegra, &rsp);
751 		if (err < 0)
752 			dev_err(dev, "failed to send %s: %d\n", cmd, err);
753 	}
754 }
755 
756 static irqreturn_t tegra_xusb_mbox_thread(int irq, void *data)
757 {
758 	struct tegra_xusb *tegra = data;
759 	const struct tegra_xusb_soc_ops *ops = tegra->soc->ops;
760 	struct tegra_xusb_mbox_msg msg;
761 	u32 value;
762 
763 	mutex_lock(&tegra->lock);
764 
765 	if (pm_runtime_suspended(tegra->dev) || tegra->suspended)
766 		goto out;
767 
768 	value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.data_out);
769 	tegra_xusb_mbox_unpack(&msg, value);
770 
771 	value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.cmd);
772 	value &= ~MBOX_DEST_SMI;
773 	ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.cmd);
774 
775 	/* clear mailbox owner if no ACK/NAK is required */
776 	if (!tegra_xusb_mbox_cmd_requires_ack(msg.cmd))
777 		ops->mbox_reg_writel(tegra, MBOX_OWNER_NONE, tegra->soc->mbox.owner);
778 
779 	tegra_xusb_mbox_handle(tegra, &msg);
780 
781 out:
782 	mutex_unlock(&tegra->lock);
783 	return IRQ_HANDLED;
784 }
785 
786 static void tegra_xusb_config(struct tegra_xusb *tegra)
787 {
788 	u32 regs = tegra->hcd->rsrc_start;
789 	u32 value;
790 
791 	if (tegra->soc->has_ipfs) {
792 		value = ipfs_readl(tegra, IPFS_XUSB_HOST_CONFIGURATION_0);
793 		value |= IPFS_EN_FPCI;
794 		ipfs_writel(tegra, value, IPFS_XUSB_HOST_CONFIGURATION_0);
795 
796 		usleep_range(10, 20);
797 	}
798 
799 	/* Program BAR0 space */
800 	value = fpci_readl(tegra, XUSB_CFG_4);
801 	value &= ~(XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT);
802 	value |= regs & (XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT);
803 	fpci_writel(tegra, value, XUSB_CFG_4);
804 
805 	/* Program BAR2 space */
806 	if (tegra->bar2) {
807 		value = fpci_readl(tegra, XUSB_CFG_7);
808 		value &= ~(XUSB_BASE2_ADDR_MASK << XUSB_BASE2_ADDR_SHIFT);
809 		value |= tegra->bar2->start &
810 			(XUSB_BASE2_ADDR_MASK << XUSB_BASE2_ADDR_SHIFT);
811 		fpci_writel(tegra, value, XUSB_CFG_7);
812 	}
813 
814 	usleep_range(100, 200);
815 
816 	/* Enable bus master */
817 	value = fpci_readl(tegra, XUSB_CFG_1);
818 	value |= XUSB_IO_SPACE_EN | XUSB_MEM_SPACE_EN | XUSB_BUS_MASTER_EN;
819 	fpci_writel(tegra, value, XUSB_CFG_1);
820 
821 	if (tegra->soc->has_ipfs) {
822 		/* Enable interrupt assertion */
823 		value = ipfs_readl(tegra, IPFS_XUSB_HOST_INTR_MASK_0);
824 		value |= IPFS_IP_INT_MASK;
825 		ipfs_writel(tegra, value, IPFS_XUSB_HOST_INTR_MASK_0);
826 
827 		/* Set hysteresis */
828 		ipfs_writel(tegra, 0x80, IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
829 	}
830 }
831 
832 static int tegra_xusb_clk_enable(struct tegra_xusb *tegra)
833 {
834 	int err;
835 
836 	err = clk_prepare_enable(tegra->pll_e);
837 	if (err < 0)
838 		return err;
839 
840 	err = clk_prepare_enable(tegra->host_clk);
841 	if (err < 0)
842 		goto disable_plle;
843 
844 	err = clk_prepare_enable(tegra->ss_clk);
845 	if (err < 0)
846 		goto disable_host;
847 
848 	err = clk_prepare_enable(tegra->falcon_clk);
849 	if (err < 0)
850 		goto disable_ss;
851 
852 	err = clk_prepare_enable(tegra->fs_src_clk);
853 	if (err < 0)
854 		goto disable_falc;
855 
856 	err = clk_prepare_enable(tegra->hs_src_clk);
857 	if (err < 0)
858 		goto disable_fs_src;
859 
860 	if (tegra->soc->scale_ss_clock) {
861 		err = tegra_xusb_set_ss_clk(tegra, TEGRA_XHCI_SS_HIGH_SPEED);
862 		if (err < 0)
863 			goto disable_hs_src;
864 	}
865 
866 	return 0;
867 
868 disable_hs_src:
869 	clk_disable_unprepare(tegra->hs_src_clk);
870 disable_fs_src:
871 	clk_disable_unprepare(tegra->fs_src_clk);
872 disable_falc:
873 	clk_disable_unprepare(tegra->falcon_clk);
874 disable_ss:
875 	clk_disable_unprepare(tegra->ss_clk);
876 disable_host:
877 	clk_disable_unprepare(tegra->host_clk);
878 disable_plle:
879 	clk_disable_unprepare(tegra->pll_e);
880 	return err;
881 }
882 
883 static void tegra_xusb_clk_disable(struct tegra_xusb *tegra)
884 {
885 	clk_disable_unprepare(tegra->pll_e);
886 	clk_disable_unprepare(tegra->host_clk);
887 	clk_disable_unprepare(tegra->ss_clk);
888 	clk_disable_unprepare(tegra->falcon_clk);
889 	clk_disable_unprepare(tegra->fs_src_clk);
890 	clk_disable_unprepare(tegra->hs_src_clk);
891 }
892 
893 static int tegra_xusb_phy_enable(struct tegra_xusb *tegra)
894 {
895 	unsigned int i;
896 	int err;
897 
898 	for (i = 0; i < tegra->num_phys; i++) {
899 		err = phy_init(tegra->phys[i]);
900 		if (err)
901 			goto disable_phy;
902 
903 		err = phy_power_on(tegra->phys[i]);
904 		if (err) {
905 			phy_exit(tegra->phys[i]);
906 			goto disable_phy;
907 		}
908 	}
909 
910 	return 0;
911 
912 disable_phy:
913 	while (i--) {
914 		phy_power_off(tegra->phys[i]);
915 		phy_exit(tegra->phys[i]);
916 	}
917 
918 	return err;
919 }
920 
921 static void tegra_xusb_phy_disable(struct tegra_xusb *tegra)
922 {
923 	unsigned int i;
924 
925 	for (i = 0; i < tegra->num_phys; i++) {
926 		phy_power_off(tegra->phys[i]);
927 		phy_exit(tegra->phys[i]);
928 	}
929 }
930 
931 #ifdef CONFIG_PM_SLEEP
932 static int tegra_xusb_init_context(struct tegra_xusb *tegra)
933 {
934 	const struct tegra_xusb_context_soc *soc = tegra->soc->context;
935 
936 	tegra->context.ipfs = devm_kcalloc(tegra->dev, soc->ipfs.num_offsets,
937 					   sizeof(u32), GFP_KERNEL);
938 	if (!tegra->context.ipfs)
939 		return -ENOMEM;
940 
941 	tegra->context.fpci = devm_kcalloc(tegra->dev, soc->fpci.num_offsets,
942 					   sizeof(u32), GFP_KERNEL);
943 	if (!tegra->context.fpci)
944 		return -ENOMEM;
945 
946 	return 0;
947 }
948 #else
949 static inline int tegra_xusb_init_context(struct tegra_xusb *tegra)
950 {
951 	return 0;
952 }
953 #endif
954 
955 static int tegra_xusb_request_firmware(struct tegra_xusb *tegra)
956 {
957 	struct tegra_xusb_fw_header *header;
958 	const struct firmware *fw;
959 	int err;
960 
961 	err = request_firmware(&fw, tegra->soc->firmware, tegra->dev);
962 	if (err < 0) {
963 		dev_err(tegra->dev, "failed to request firmware: %d\n", err);
964 		return err;
965 	}
966 
967 	/* Load Falcon controller with its firmware. */
968 	header = (struct tegra_xusb_fw_header *)fw->data;
969 	tegra->fw.size = le32_to_cpu(header->fwimg_len);
970 
971 	tegra->fw.virt = dma_alloc_coherent(tegra->dev, tegra->fw.size,
972 					    &tegra->fw.phys, GFP_KERNEL);
973 	if (!tegra->fw.virt) {
974 		dev_err(tegra->dev, "failed to allocate memory for firmware\n");
975 		release_firmware(fw);
976 		return -ENOMEM;
977 	}
978 
979 	header = (struct tegra_xusb_fw_header *)tegra->fw.virt;
980 	memcpy(tegra->fw.virt, fw->data, tegra->fw.size);
981 	release_firmware(fw);
982 
983 	return 0;
984 }
985 
986 static int tegra_xusb_wait_for_falcon(struct tegra_xusb *tegra)
987 {
988 	struct xhci_cap_regs __iomem *cap_regs;
989 	struct xhci_op_regs __iomem *op_regs;
990 	int ret;
991 	u32 value;
992 
993 	cap_regs = tegra->regs;
994 	op_regs = tegra->regs + HC_LENGTH(readl(&cap_regs->hc_capbase));
995 
996 	ret = readl_poll_timeout(&op_regs->status, value, !(value & STS_CNR), 1000, 200000);
997 
998 	if (ret)
999 		dev_err(tegra->dev, "XHCI Controller not ready. Falcon state: 0x%x\n",
1000 			csb_readl(tegra, XUSB_FALC_CPUCTL));
1001 
1002 	return ret;
1003 }
1004 
1005 static int tegra_xusb_load_firmware_rom(struct tegra_xusb *tegra)
1006 {
1007 	unsigned int code_tag_blocks, code_size_blocks, code_blocks;
1008 	struct tegra_xusb_fw_header *header;
1009 	struct device *dev = tegra->dev;
1010 	time64_t timestamp;
1011 	u64 address;
1012 	u32 value;
1013 	int err;
1014 
1015 	header = (struct tegra_xusb_fw_header *)tegra->fw.virt;
1016 
1017 	if (csb_readl(tegra, XUSB_CSB_MP_ILOAD_BASE_LO) != 0) {
1018 		dev_info(dev, "Firmware already loaded, Falcon state %#x\n",
1019 			 csb_readl(tegra, XUSB_FALC_CPUCTL));
1020 		return 0;
1021 	}
1022 
1023 	/* Program the size of DFI into ILOAD_ATTR. */
1024 	csb_writel(tegra, tegra->fw.size, XUSB_CSB_MP_ILOAD_ATTR);
1025 
1026 	/*
1027 	 * Boot code of the firmware reads the ILOAD_BASE registers
1028 	 * to get to the start of the DFI in system memory.
1029 	 */
1030 	address = tegra->fw.phys + sizeof(*header);
1031 	csb_writel(tegra, address >> 32, XUSB_CSB_MP_ILOAD_BASE_HI);
1032 	csb_writel(tegra, address, XUSB_CSB_MP_ILOAD_BASE_LO);
1033 
1034 	/* Set BOOTPATH to 1 in APMAP. */
1035 	csb_writel(tegra, APMAP_BOOTPATH, XUSB_CSB_MP_APMAP);
1036 
1037 	/* Invalidate L2IMEM. */
1038 	csb_writel(tegra, L2IMEMOP_INVALIDATE_ALL, XUSB_CSB_MP_L2IMEMOP_TRIG);
1039 
1040 	/*
1041 	 * Initiate fetch of bootcode from system memory into L2IMEM.
1042 	 * Program bootcode location and size in system memory.
1043 	 */
1044 	code_tag_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codetag),
1045 				       IMEM_BLOCK_SIZE);
1046 	code_size_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codesize),
1047 					IMEM_BLOCK_SIZE);
1048 	code_blocks = code_tag_blocks + code_size_blocks;
1049 
1050 	value = ((code_tag_blocks & L2IMEMOP_SIZE_SRC_OFFSET_MASK) <<
1051 			L2IMEMOP_SIZE_SRC_OFFSET_SHIFT) |
1052 		((code_size_blocks & L2IMEMOP_SIZE_SRC_COUNT_MASK) <<
1053 			L2IMEMOP_SIZE_SRC_COUNT_SHIFT);
1054 	csb_writel(tegra, value, XUSB_CSB_MP_L2IMEMOP_SIZE);
1055 
1056 	/* Trigger L2IMEM load operation. */
1057 	csb_writel(tegra, L2IMEMOP_LOAD_LOCKED_RESULT,
1058 		   XUSB_CSB_MP_L2IMEMOP_TRIG);
1059 
1060 	/* Setup Falcon auto-fill. */
1061 	csb_writel(tegra, code_size_blocks, XUSB_FALC_IMFILLCTL);
1062 
1063 	value = ((code_tag_blocks & IMFILLRNG1_TAG_MASK) <<
1064 			IMFILLRNG1_TAG_LO_SHIFT) |
1065 		((code_blocks & IMFILLRNG1_TAG_MASK) <<
1066 			IMFILLRNG1_TAG_HI_SHIFT);
1067 	csb_writel(tegra, value, XUSB_FALC_IMFILLRNG1);
1068 
1069 	csb_writel(tegra, 0, XUSB_FALC_DMACTL);
1070 
1071 	/* wait for RESULT_VLD to get set */
1072 #define tegra_csb_readl(offset) csb_readl(tegra, offset)
1073 	err = readx_poll_timeout(tegra_csb_readl,
1074 				 XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT, value,
1075 				 value & L2IMEMOP_RESULT_VLD, 100, 10000);
1076 	if (err < 0) {
1077 		dev_err(dev, "DMA controller not ready %#010x\n", value);
1078 		return err;
1079 	}
1080 #undef tegra_csb_readl
1081 
1082 	csb_writel(tegra, le32_to_cpu(header->boot_codetag),
1083 		   XUSB_FALC_BOOTVEC);
1084 
1085 	/* Boot Falcon CPU and wait for USBSTS_CNR to get cleared. */
1086 	csb_writel(tegra, CPUCTL_STARTCPU, XUSB_FALC_CPUCTL);
1087 
1088 	if (tegra_xusb_wait_for_falcon(tegra))
1089 		return -EIO;
1090 
1091 	timestamp = le32_to_cpu(header->fwimg_created_time);
1092 
1093 	dev_info(dev, "Firmware timestamp: %ptTs UTC\n", &timestamp);
1094 
1095 	return 0;
1096 }
1097 
1098 static u32 tegra_xusb_read_firmware_header(struct tegra_xusb *tegra, u32 offset)
1099 {
1100 	/*
1101 	 * We only accept reading the firmware config table
1102 	 * The offset should not exceed the fw header structure
1103 	 */
1104 	if (offset >= sizeof(struct tegra_xusb_fw_header))
1105 		return 0;
1106 
1107 	bar2_writel(tegra, (FW_IOCTL_CFGTBL_READ << FW_IOCTL_TYPE_SHIFT) | offset,
1108 		    XUSB_BAR2_ARU_FW_SCRATCH);
1109 	return bar2_readl(tegra, XUSB_BAR2_ARU_SMI_ARU_FW_SCRATCH_DATA0);
1110 }
1111 
1112 static int tegra_xusb_init_ifr_firmware(struct tegra_xusb *tegra)
1113 {
1114 	time64_t timestamp;
1115 
1116 	if (tegra_xusb_wait_for_falcon(tegra))
1117 		return -EIO;
1118 
1119 #define offsetof_32(X, Y) ((u8)(offsetof(X, Y) / sizeof(__le32)))
1120 	timestamp = tegra_xusb_read_firmware_header(tegra, offsetof_32(struct tegra_xusb_fw_header,
1121 								       fwimg_created_time) << 2);
1122 
1123 	dev_info(tegra->dev, "Firmware timestamp: %ptTs UTC\n", &timestamp);
1124 
1125 	return 0;
1126 }
1127 
1128 static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
1129 {
1130 	if (!tegra->soc->firmware)
1131 		return tegra_xusb_init_ifr_firmware(tegra);
1132 	else
1133 		return tegra_xusb_load_firmware_rom(tegra);
1134 }
1135 
1136 static void tegra_xusb_powerdomain_remove(struct device *dev,
1137 					  struct tegra_xusb *tegra)
1138 {
1139 	if (!tegra->use_genpd)
1140 		return;
1141 
1142 	if (!IS_ERR_OR_NULL(tegra->genpd_dev_ss))
1143 		dev_pm_domain_detach(tegra->genpd_dev_ss, true);
1144 	if (!IS_ERR_OR_NULL(tegra->genpd_dev_host))
1145 		dev_pm_domain_detach(tegra->genpd_dev_host, true);
1146 }
1147 
1148 static int tegra_xusb_powerdomain_init(struct device *dev,
1149 				       struct tegra_xusb *tegra)
1150 {
1151 	int err;
1152 
1153 	tegra->genpd_dev_host = dev_pm_domain_attach_by_name(dev, "xusb_host");
1154 	if (IS_ERR(tegra->genpd_dev_host)) {
1155 		err = PTR_ERR(tegra->genpd_dev_host);
1156 		dev_err(dev, "failed to get host pm-domain: %d\n", err);
1157 		return err;
1158 	}
1159 
1160 	tegra->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "xusb_ss");
1161 	if (IS_ERR(tegra->genpd_dev_ss)) {
1162 		err = PTR_ERR(tegra->genpd_dev_ss);
1163 		dev_err(dev, "failed to get superspeed pm-domain: %d\n", err);
1164 		return err;
1165 	}
1166 
1167 	tegra->use_genpd = true;
1168 
1169 	return 0;
1170 }
1171 
1172 static int tegra_xusb_unpowergate_partitions(struct tegra_xusb *tegra)
1173 {
1174 	struct device *dev = tegra->dev;
1175 	int rc;
1176 
1177 	if (tegra->use_genpd) {
1178 		rc = pm_runtime_resume_and_get(tegra->genpd_dev_ss);
1179 		if (rc < 0) {
1180 			dev_err(dev, "failed to enable XUSB SS partition\n");
1181 			return rc;
1182 		}
1183 
1184 		rc = pm_runtime_resume_and_get(tegra->genpd_dev_host);
1185 		if (rc < 0) {
1186 			dev_err(dev, "failed to enable XUSB Host partition\n");
1187 			pm_runtime_put_sync(tegra->genpd_dev_ss);
1188 			return rc;
1189 		}
1190 	} else {
1191 		rc = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBA,
1192 							tegra->ss_clk,
1193 							tegra->ss_rst);
1194 		if (rc < 0) {
1195 			dev_err(dev, "failed to enable XUSB SS partition\n");
1196 			return rc;
1197 		}
1198 
1199 		rc = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBC,
1200 							tegra->host_clk,
1201 							tegra->host_rst);
1202 		if (rc < 0) {
1203 			dev_err(dev, "failed to enable XUSB Host partition\n");
1204 			tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
1205 			return rc;
1206 		}
1207 	}
1208 
1209 	return 0;
1210 }
1211 
1212 static int tegra_xusb_powergate_partitions(struct tegra_xusb *tegra)
1213 {
1214 	struct device *dev = tegra->dev;
1215 	int rc;
1216 
1217 	if (tegra->use_genpd) {
1218 		rc = pm_runtime_put_sync(tegra->genpd_dev_host);
1219 		if (rc < 0) {
1220 			dev_err(dev, "failed to disable XUSB Host partition\n");
1221 			return rc;
1222 		}
1223 
1224 		rc = pm_runtime_put_sync(tegra->genpd_dev_ss);
1225 		if (rc < 0) {
1226 			dev_err(dev, "failed to disable XUSB SS partition\n");
1227 			pm_runtime_get_sync(tegra->genpd_dev_host);
1228 			return rc;
1229 		}
1230 	} else {
1231 		rc = tegra_powergate_power_off(TEGRA_POWERGATE_XUSBC);
1232 		if (rc < 0) {
1233 			dev_err(dev, "failed to disable XUSB Host partition\n");
1234 			return rc;
1235 		}
1236 
1237 		rc = tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
1238 		if (rc < 0) {
1239 			dev_err(dev, "failed to disable XUSB SS partition\n");
1240 			tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBC,
1241 							  tegra->host_clk,
1242 							  tegra->host_rst);
1243 			return rc;
1244 		}
1245 	}
1246 
1247 	return 0;
1248 }
1249 
1250 static int __tegra_xusb_enable_firmware_messages(struct tegra_xusb *tegra)
1251 {
1252 	struct tegra_xusb_mbox_msg msg;
1253 	int err;
1254 
1255 	/* Enable firmware messages from controller. */
1256 	msg.cmd = MBOX_CMD_MSG_ENABLED;
1257 	msg.data = 0;
1258 
1259 	err = tegra_xusb_mbox_send(tegra, &msg);
1260 	if (err < 0)
1261 		dev_err(tegra->dev, "failed to enable messages: %d\n", err);
1262 
1263 	return err;
1264 }
1265 
1266 static irqreturn_t tegra_xusb_padctl_irq(int irq, void *data)
1267 {
1268 	struct tegra_xusb *tegra = data;
1269 
1270 	mutex_lock(&tegra->lock);
1271 
1272 	if (tegra->suspended) {
1273 		mutex_unlock(&tegra->lock);
1274 		return IRQ_HANDLED;
1275 	}
1276 
1277 	mutex_unlock(&tegra->lock);
1278 
1279 	pm_runtime_resume(tegra->dev);
1280 
1281 	return IRQ_HANDLED;
1282 }
1283 
1284 static int tegra_xusb_enable_firmware_messages(struct tegra_xusb *tegra)
1285 {
1286 	int err;
1287 
1288 	mutex_lock(&tegra->lock);
1289 	err = __tegra_xusb_enable_firmware_messages(tegra);
1290 	mutex_unlock(&tegra->lock);
1291 
1292 	return err;
1293 }
1294 
1295 static void tegra_xhci_set_port_power(struct tegra_xusb *tegra, bool main,
1296 						 bool set)
1297 {
1298 	struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
1299 	struct usb_hcd *hcd = main ?  xhci->main_hcd : xhci->shared_hcd;
1300 	unsigned int wait = (!main && !set) ? 1000 : 10;
1301 	u16 typeReq = set ? SetPortFeature : ClearPortFeature;
1302 	u16 wIndex = main ? tegra->otg_usb2_port + 1 : tegra->otg_usb3_port + 1;
1303 	u32 status;
1304 	u32 stat_power = main ? USB_PORT_STAT_POWER : USB_SS_PORT_STAT_POWER;
1305 	u32 status_val = set ? stat_power : 0;
1306 
1307 	dev_dbg(tegra->dev, "%s():%s %s port power\n", __func__,
1308 		set ? "set" : "clear", main ? "HS" : "SS");
1309 
1310 	hcd->driver->hub_control(hcd, typeReq, USB_PORT_FEAT_POWER, wIndex,
1311 				 NULL, 0);
1312 
1313 	do {
1314 		tegra_xhci_hc_driver.hub_control(hcd, GetPortStatus, 0, wIndex,
1315 					(char *) &status, sizeof(status));
1316 		if (status_val == (status & stat_power))
1317 			break;
1318 
1319 		if (!main && !set)
1320 			usleep_range(600, 700);
1321 		else
1322 			usleep_range(10, 20);
1323 	} while (--wait > 0);
1324 
1325 	if (status_val != (status & stat_power))
1326 		dev_info(tegra->dev, "failed to %s %s PP %d\n",
1327 						set ? "set" : "clear",
1328 						main ? "HS" : "SS", status);
1329 }
1330 
1331 static struct phy *tegra_xusb_get_phy(struct tegra_xusb *tegra, char *name,
1332 								int port)
1333 {
1334 	unsigned int i, phy_count = 0;
1335 
1336 	for (i = 0; i < tegra->soc->num_types; i++) {
1337 		if (!strncmp(tegra->soc->phy_types[i].name, name,
1338 							    strlen(name)))
1339 			return tegra->phys[phy_count+port];
1340 
1341 		phy_count += tegra->soc->phy_types[i].num;
1342 	}
1343 
1344 	return NULL;
1345 }
1346 
1347 static void tegra_xhci_id_work(struct work_struct *work)
1348 {
1349 	struct tegra_xusb *tegra = container_of(work, struct tegra_xusb,
1350 						id_work);
1351 	struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
1352 	struct tegra_xusb_mbox_msg msg;
1353 	struct phy *phy = tegra_xusb_get_phy(tegra, "usb2",
1354 						    tegra->otg_usb2_port);
1355 	u32 status;
1356 	int ret;
1357 
1358 	dev_dbg(tegra->dev, "host mode %s\n", str_on_off(tegra->host_mode));
1359 
1360 	mutex_lock(&tegra->lock);
1361 
1362 	if (tegra->host_mode)
1363 		phy_set_mode_ext(phy, PHY_MODE_USB_OTG, USB_ROLE_HOST);
1364 	else
1365 		phy_set_mode_ext(phy, PHY_MODE_USB_OTG, USB_ROLE_NONE);
1366 
1367 	mutex_unlock(&tegra->lock);
1368 
1369 	tegra->otg_usb3_port = tegra_xusb_padctl_get_usb3_companion(tegra->padctl,
1370 								    tegra->otg_usb2_port);
1371 
1372 	pm_runtime_get_sync(tegra->dev);
1373 	if (tegra->host_mode) {
1374 		/* switch to host mode */
1375 		if (tegra->otg_usb3_port >= 0) {
1376 			if (tegra->soc->otg_reset_sspi) {
1377 				/* set PP=0 */
1378 				tegra_xhci_hc_driver.hub_control(
1379 					xhci->shared_hcd, GetPortStatus,
1380 					0, tegra->otg_usb3_port+1,
1381 					(char *) &status, sizeof(status));
1382 				if (status & USB_SS_PORT_STAT_POWER)
1383 					tegra_xhci_set_port_power(tegra, false,
1384 								  false);
1385 
1386 				/* reset OTG port SSPI */
1387 				msg.cmd = MBOX_CMD_RESET_SSPI;
1388 				msg.data = tegra->otg_usb3_port+1;
1389 
1390 				ret = tegra_xusb_mbox_send(tegra, &msg);
1391 				if (ret < 0) {
1392 					dev_info(tegra->dev,
1393 						"failed to RESET_SSPI %d\n",
1394 						ret);
1395 				}
1396 			}
1397 
1398 			tegra_xhci_set_port_power(tegra, false, true);
1399 		}
1400 
1401 		tegra_xhci_set_port_power(tegra, true, true);
1402 		pm_runtime_mark_last_busy(tegra->dev);
1403 
1404 	} else {
1405 		if (tegra->otg_usb3_port >= 0)
1406 			tegra_xhci_set_port_power(tegra, false, false);
1407 
1408 		tegra_xhci_set_port_power(tegra, true, false);
1409 	}
1410 	pm_runtime_put_autosuspend(tegra->dev);
1411 }
1412 
1413 #if IS_ENABLED(CONFIG_PM) || IS_ENABLED(CONFIG_PM_SLEEP)
1414 static bool is_usb2_otg_phy(struct tegra_xusb *tegra, unsigned int index)
1415 {
1416 	return (tegra->usbphy[index] != NULL);
1417 }
1418 
1419 static bool is_usb3_otg_phy(struct tegra_xusb *tegra, unsigned int index)
1420 {
1421 	struct tegra_xusb_padctl *padctl = tegra->padctl;
1422 	unsigned int i;
1423 	int port;
1424 
1425 	for (i = 0; i < tegra->num_usb_phys; i++) {
1426 		if (is_usb2_otg_phy(tegra, i)) {
1427 			port = tegra_xusb_padctl_get_usb3_companion(padctl, i);
1428 			if ((port >= 0) && (index == (unsigned int)port))
1429 				return true;
1430 		}
1431 	}
1432 
1433 	return false;
1434 }
1435 
1436 static bool is_host_mode_phy(struct tegra_xusb *tegra, unsigned int phy_type, unsigned int index)
1437 {
1438 	if (strcmp(tegra->soc->phy_types[phy_type].name, "hsic") == 0)
1439 		return true;
1440 
1441 	if (strcmp(tegra->soc->phy_types[phy_type].name, "usb2") == 0) {
1442 		if (is_usb2_otg_phy(tegra, index))
1443 			return ((index == tegra->otg_usb2_port) && tegra->host_mode);
1444 		else
1445 			return true;
1446 	}
1447 
1448 	if (strcmp(tegra->soc->phy_types[phy_type].name, "usb3") == 0) {
1449 		if (is_usb3_otg_phy(tegra, index))
1450 			return ((index == tegra->otg_usb3_port) && tegra->host_mode);
1451 		else
1452 			return true;
1453 	}
1454 
1455 	return false;
1456 }
1457 #endif
1458 
1459 static int tegra_xusb_get_usb2_port(struct tegra_xusb *tegra,
1460 					      struct usb_phy *usbphy)
1461 {
1462 	unsigned int i;
1463 
1464 	for (i = 0; i < tegra->num_usb_phys; i++) {
1465 		if (tegra->usbphy[i] && usbphy == tegra->usbphy[i])
1466 			return i;
1467 	}
1468 
1469 	return -1;
1470 }
1471 
1472 static int tegra_xhci_id_notify(struct notifier_block *nb,
1473 					 unsigned long action, void *data)
1474 {
1475 	struct tegra_xusb *tegra = container_of(nb, struct tegra_xusb,
1476 						    id_nb);
1477 	struct usb_phy *usbphy = (struct usb_phy *)data;
1478 
1479 	dev_dbg(tegra->dev, "%s(): action is %d", __func__, usbphy->last_event);
1480 
1481 	if ((tegra->host_mode && usbphy->last_event == USB_EVENT_ID) ||
1482 		(!tegra->host_mode && usbphy->last_event != USB_EVENT_ID)) {
1483 		dev_dbg(tegra->dev, "Same role(%d) received. Ignore",
1484 			tegra->host_mode);
1485 		return NOTIFY_OK;
1486 	}
1487 
1488 	tegra->otg_usb2_port = tegra_xusb_get_usb2_port(tegra, usbphy);
1489 
1490 	tegra->host_mode = usbphy->last_event == USB_EVENT_ID;
1491 
1492 	schedule_work(&tegra->id_work);
1493 
1494 	return NOTIFY_OK;
1495 }
1496 
1497 static int tegra_xusb_init_usb_phy(struct tegra_xusb *tegra)
1498 {
1499 	unsigned int i;
1500 
1501 	tegra->usbphy = devm_kcalloc(tegra->dev, tegra->num_usb_phys,
1502 				   sizeof(*tegra->usbphy), GFP_KERNEL);
1503 	if (!tegra->usbphy)
1504 		return -ENOMEM;
1505 
1506 	INIT_WORK(&tegra->id_work, tegra_xhci_id_work);
1507 	tegra->id_nb.notifier_call = tegra_xhci_id_notify;
1508 	tegra->otg_usb2_port = -EINVAL;
1509 	tegra->otg_usb3_port = -EINVAL;
1510 
1511 	for (i = 0; i < tegra->num_usb_phys; i++) {
1512 		struct phy *phy = tegra_xusb_get_phy(tegra, "usb2", i);
1513 
1514 		if (!phy)
1515 			continue;
1516 
1517 		tegra->usbphy[i] = devm_usb_get_phy_by_node(tegra->dev,
1518 							phy->dev.of_node,
1519 							&tegra->id_nb);
1520 		if (!IS_ERR(tegra->usbphy[i])) {
1521 			dev_dbg(tegra->dev, "usbphy-%d registered", i);
1522 			otg_set_host(tegra->usbphy[i]->otg, &tegra->hcd->self);
1523 		} else {
1524 			/*
1525 			 * usb-phy is optional, continue if its not available.
1526 			 */
1527 			tegra->usbphy[i] = NULL;
1528 		}
1529 	}
1530 
1531 	return 0;
1532 }
1533 
1534 static void tegra_xusb_deinit_usb_phy(struct tegra_xusb *tegra)
1535 {
1536 	unsigned int i;
1537 
1538 	cancel_work_sync(&tegra->id_work);
1539 
1540 	for (i = 0; i < tegra->num_usb_phys; i++)
1541 		if (tegra->usbphy[i])
1542 			otg_set_host(tegra->usbphy[i]->otg, NULL);
1543 }
1544 
1545 static int tegra_xusb_setup_wakeup(struct platform_device *pdev, struct tegra_xusb *tegra)
1546 {
1547 	unsigned int i;
1548 
1549 	if (tegra->soc->max_num_wakes == 0)
1550 		return 0;
1551 
1552 	tegra->wake_irqs = devm_kcalloc(tegra->dev,
1553 					tegra->soc->max_num_wakes,
1554 					sizeof(*tegra->wake_irqs), GFP_KERNEL);
1555 	if (!tegra->wake_irqs)
1556 		return -ENOMEM;
1557 
1558 	/*
1559 	 * USB wake events are independent of each other, so it is not necessary for a platform
1560 	 * to utilize all wake-up events supported for a given device. The USB host can operate
1561 	 * even if wake-up events are not defined or fail to be configured. Therefore, we only
1562 	 * return critical errors, such as -ENOMEM.
1563 	 */
1564 	for (i = 0; i < tegra->soc->max_num_wakes; i++) {
1565 		struct irq_data *data;
1566 
1567 		tegra->wake_irqs[i] = platform_get_irq(pdev, i + WAKE_IRQ_START_INDEX);
1568 		if (tegra->wake_irqs[i] < 0)
1569 			break;
1570 
1571 		data = irq_get_irq_data(tegra->wake_irqs[i]);
1572 		if (!data) {
1573 			dev_warn(tegra->dev, "get wake event %d irq data fail\n", i);
1574 			irq_dispose_mapping(tegra->wake_irqs[i]);
1575 			break;
1576 		}
1577 
1578 		irq_set_irq_type(tegra->wake_irqs[i], irqd_get_trigger_type(data));
1579 	}
1580 
1581 	tegra->num_wakes = i;
1582 	dev_dbg(tegra->dev, "setup %d wake events\n", tegra->num_wakes);
1583 
1584 	return 0;
1585 }
1586 
1587 static void tegra_xusb_dispose_wake(struct tegra_xusb *tegra)
1588 {
1589 	unsigned int i;
1590 
1591 	for (i = 0; i < tegra->num_wakes; i++)
1592 		irq_dispose_mapping(tegra->wake_irqs[i]);
1593 
1594 	tegra->num_wakes = 0;
1595 }
1596 
1597 static int tegra_xusb_probe(struct platform_device *pdev)
1598 {
1599 	struct tegra_xusb *tegra;
1600 	struct device_node *np;
1601 	struct resource *regs;
1602 	struct xhci_hcd *xhci;
1603 	unsigned int i, j, k;
1604 	struct phy *phy;
1605 	int err;
1606 
1607 	BUILD_BUG_ON(sizeof(struct tegra_xusb_fw_header) != 256);
1608 
1609 	tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
1610 	if (!tegra)
1611 		return -ENOMEM;
1612 
1613 	tegra->soc = of_device_get_match_data(&pdev->dev);
1614 	mutex_init(&tegra->lock);
1615 	tegra->dev = &pdev->dev;
1616 
1617 	err = tegra_xusb_init_context(tegra);
1618 	if (err < 0)
1619 		return err;
1620 
1621 	tegra->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &regs);
1622 	if (IS_ERR(tegra->regs))
1623 		return PTR_ERR(tegra->regs);
1624 
1625 	tegra->fpci_base = devm_platform_ioremap_resource(pdev, 1);
1626 	if (IS_ERR(tegra->fpci_base))
1627 		return PTR_ERR(tegra->fpci_base);
1628 
1629 	if (tegra->soc->has_ipfs) {
1630 		tegra->ipfs_base = devm_platform_ioremap_resource(pdev, 2);
1631 		if (IS_ERR(tegra->ipfs_base))
1632 			return PTR_ERR(tegra->ipfs_base);
1633 	} else if (tegra->soc->has_bar2) {
1634 		tegra->bar2_base = devm_platform_get_and_ioremap_resource(pdev, 2, &tegra->bar2);
1635 		if (IS_ERR(tegra->bar2_base))
1636 			return PTR_ERR(tegra->bar2_base);
1637 	}
1638 
1639 	tegra->xhci_irq = platform_get_irq(pdev, 0);
1640 	if (tegra->xhci_irq < 0)
1641 		return tegra->xhci_irq;
1642 
1643 	tegra->mbox_irq = platform_get_irq(pdev, 1);
1644 	if (tegra->mbox_irq < 0)
1645 		return tegra->mbox_irq;
1646 
1647 	err = tegra_xusb_setup_wakeup(pdev, tegra);
1648 	if (err)
1649 		return err;
1650 
1651 	tegra->padctl = tegra_xusb_padctl_get(&pdev->dev);
1652 	if (IS_ERR(tegra->padctl)) {
1653 		err = PTR_ERR(tegra->padctl);
1654 		goto dispose_wake;
1655 	}
1656 
1657 	np = of_parse_phandle(pdev->dev.of_node, "nvidia,xusb-padctl", 0);
1658 	if (!np) {
1659 		err = -ENODEV;
1660 		goto put_padctl;
1661 	}
1662 
1663 	tegra->padctl_irq = of_irq_get(np, 0);
1664 	if (tegra->padctl_irq == -EPROBE_DEFER) {
1665 		err = tegra->padctl_irq;
1666 		goto put_padctl;
1667 	} else if (tegra->padctl_irq <= 0) {
1668 		/* Older device-trees don't have padctrl interrupt */
1669 		tegra->padctl_irq = 0;
1670 		dev_dbg(&pdev->dev,
1671 			"%pOF is missing an interrupt, disabling PM support\n", np);
1672 	}
1673 
1674 	tegra->host_clk = devm_clk_get(&pdev->dev, "xusb_host");
1675 	if (IS_ERR(tegra->host_clk)) {
1676 		err = PTR_ERR(tegra->host_clk);
1677 		dev_err(&pdev->dev, "failed to get xusb_host: %d\n", err);
1678 		goto put_padctl;
1679 	}
1680 
1681 	tegra->falcon_clk = devm_clk_get(&pdev->dev, "xusb_falcon_src");
1682 	if (IS_ERR(tegra->falcon_clk)) {
1683 		err = PTR_ERR(tegra->falcon_clk);
1684 		dev_err(&pdev->dev, "failed to get xusb_falcon_src: %d\n", err);
1685 		goto put_padctl;
1686 	}
1687 
1688 	tegra->ss_clk = devm_clk_get(&pdev->dev, "xusb_ss");
1689 	if (IS_ERR(tegra->ss_clk)) {
1690 		err = PTR_ERR(tegra->ss_clk);
1691 		dev_err(&pdev->dev, "failed to get xusb_ss: %d\n", err);
1692 		goto put_padctl;
1693 	}
1694 
1695 	tegra->ss_src_clk = devm_clk_get(&pdev->dev, "xusb_ss_src");
1696 	if (IS_ERR(tegra->ss_src_clk)) {
1697 		err = PTR_ERR(tegra->ss_src_clk);
1698 		dev_err(&pdev->dev, "failed to get xusb_ss_src: %d\n", err);
1699 		goto put_padctl;
1700 	}
1701 
1702 	tegra->hs_src_clk = devm_clk_get(&pdev->dev, "xusb_hs_src");
1703 	if (IS_ERR(tegra->hs_src_clk)) {
1704 		err = PTR_ERR(tegra->hs_src_clk);
1705 		dev_err(&pdev->dev, "failed to get xusb_hs_src: %d\n", err);
1706 		goto put_padctl;
1707 	}
1708 
1709 	tegra->fs_src_clk = devm_clk_get(&pdev->dev, "xusb_fs_src");
1710 	if (IS_ERR(tegra->fs_src_clk)) {
1711 		err = PTR_ERR(tegra->fs_src_clk);
1712 		dev_err(&pdev->dev, "failed to get xusb_fs_src: %d\n", err);
1713 		goto put_padctl;
1714 	}
1715 
1716 	tegra->pll_u_480m = devm_clk_get(&pdev->dev, "pll_u_480m");
1717 	if (IS_ERR(tegra->pll_u_480m)) {
1718 		err = PTR_ERR(tegra->pll_u_480m);
1719 		dev_err(&pdev->dev, "failed to get pll_u_480m: %d\n", err);
1720 		goto put_padctl;
1721 	}
1722 
1723 	tegra->clk_m = devm_clk_get(&pdev->dev, "clk_m");
1724 	if (IS_ERR(tegra->clk_m)) {
1725 		err = PTR_ERR(tegra->clk_m);
1726 		dev_err(&pdev->dev, "failed to get clk_m: %d\n", err);
1727 		goto put_padctl;
1728 	}
1729 
1730 	tegra->pll_e = devm_clk_get(&pdev->dev, "pll_e");
1731 	if (IS_ERR(tegra->pll_e)) {
1732 		err = PTR_ERR(tegra->pll_e);
1733 		dev_err(&pdev->dev, "failed to get pll_e: %d\n", err);
1734 		goto put_padctl;
1735 	}
1736 
1737 	if (!of_property_present(pdev->dev.of_node, "power-domains")) {
1738 		tegra->host_rst = devm_reset_control_get(&pdev->dev,
1739 							 "xusb_host");
1740 		if (IS_ERR(tegra->host_rst)) {
1741 			err = PTR_ERR(tegra->host_rst);
1742 			dev_err(&pdev->dev,
1743 				"failed to get xusb_host reset: %d\n", err);
1744 			goto put_padctl;
1745 		}
1746 
1747 		tegra->ss_rst = devm_reset_control_get(&pdev->dev, "xusb_ss");
1748 		if (IS_ERR(tegra->ss_rst)) {
1749 			err = PTR_ERR(tegra->ss_rst);
1750 			dev_err(&pdev->dev, "failed to get xusb_ss reset: %d\n",
1751 				err);
1752 			goto put_padctl;
1753 		}
1754 	} else {
1755 		err = tegra_xusb_powerdomain_init(&pdev->dev, tegra);
1756 		if (err)
1757 			goto put_powerdomains;
1758 	}
1759 
1760 	tegra->supplies = devm_kcalloc(&pdev->dev, tegra->soc->num_supplies,
1761 				       sizeof(*tegra->supplies), GFP_KERNEL);
1762 	if (!tegra->supplies) {
1763 		err = -ENOMEM;
1764 		goto put_powerdomains;
1765 	}
1766 
1767 	regulator_bulk_set_supply_names(tegra->supplies,
1768 					tegra->soc->supply_names,
1769 					tegra->soc->num_supplies);
1770 
1771 	err = devm_regulator_bulk_get(&pdev->dev, tegra->soc->num_supplies,
1772 				      tegra->supplies);
1773 	if (err) {
1774 		dev_err(&pdev->dev, "failed to get regulators: %d\n", err);
1775 		goto put_powerdomains;
1776 	}
1777 
1778 	for (i = 0; i < tegra->soc->num_types; i++) {
1779 		if (!strncmp(tegra->soc->phy_types[i].name, "usb2", 4))
1780 			tegra->num_usb_phys = tegra->soc->phy_types[i].num;
1781 		tegra->num_phys += tegra->soc->phy_types[i].num;
1782 	}
1783 
1784 	tegra->phys = devm_kcalloc(&pdev->dev, tegra->num_phys,
1785 				   sizeof(*tegra->phys), GFP_KERNEL);
1786 	if (!tegra->phys) {
1787 		err = -ENOMEM;
1788 		goto put_powerdomains;
1789 	}
1790 
1791 	for (i = 0, k = 0; i < tegra->soc->num_types; i++) {
1792 		char prop[8];
1793 
1794 		for (j = 0; j < tegra->soc->phy_types[i].num; j++) {
1795 			snprintf(prop, sizeof(prop), "%s-%d",
1796 				 tegra->soc->phy_types[i].name, j);
1797 
1798 			phy = devm_phy_optional_get(&pdev->dev, prop);
1799 			if (IS_ERR(phy)) {
1800 				dev_err(&pdev->dev,
1801 					"failed to get PHY %s: %ld\n", prop,
1802 					PTR_ERR(phy));
1803 				err = PTR_ERR(phy);
1804 				goto put_powerdomains;
1805 			}
1806 
1807 			tegra->phys[k++] = phy;
1808 		}
1809 	}
1810 
1811 	tegra->hcd = usb_create_hcd(&tegra_xhci_hc_driver, &pdev->dev,
1812 				    dev_name(&pdev->dev));
1813 	if (!tegra->hcd) {
1814 		err = -ENOMEM;
1815 		goto put_powerdomains;
1816 	}
1817 
1818 	tegra->hcd->skip_phy_initialization = 1;
1819 	tegra->hcd->regs = tegra->regs;
1820 	tegra->hcd->rsrc_start = regs->start;
1821 	tegra->hcd->rsrc_len = resource_size(regs);
1822 
1823 	/*
1824 	 * This must happen after usb_create_hcd(), because usb_create_hcd()
1825 	 * will overwrite the drvdata of the device with the hcd it creates.
1826 	 */
1827 	platform_set_drvdata(pdev, tegra);
1828 
1829 	err = tegra_xusb_clk_enable(tegra);
1830 	if (err) {
1831 		dev_err(tegra->dev, "failed to enable clocks: %d\n", err);
1832 		goto put_hcd;
1833 	}
1834 
1835 	err = regulator_bulk_enable(tegra->soc->num_supplies, tegra->supplies);
1836 	if (err) {
1837 		dev_err(tegra->dev, "failed to enable regulators: %d\n", err);
1838 		goto disable_clk;
1839 	}
1840 
1841 	err = tegra_xusb_phy_enable(tegra);
1842 	if (err < 0) {
1843 		dev_err(&pdev->dev, "failed to enable PHYs: %d\n", err);
1844 		goto disable_regulator;
1845 	}
1846 
1847 	/*
1848 	 * The XUSB Falcon microcontroller can only address 40 bits, so set
1849 	 * the DMA mask accordingly.
1850 	 */
1851 	err = dma_set_mask_and_coherent(tegra->dev, DMA_BIT_MASK(40));
1852 	if (err < 0) {
1853 		dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
1854 		goto disable_phy;
1855 	}
1856 
1857 	if (tegra->soc->firmware) {
1858 		err = tegra_xusb_request_firmware(tegra);
1859 		if (err < 0) {
1860 			dev_err(&pdev->dev,
1861 				"failed to request firmware: %d\n", err);
1862 			goto disable_phy;
1863 		}
1864 	}
1865 
1866 	err = tegra_xusb_unpowergate_partitions(tegra);
1867 	if (err)
1868 		goto free_firmware;
1869 
1870 	tegra_xusb_config(tegra);
1871 
1872 	err = tegra_xusb_load_firmware(tegra);
1873 	if (err < 0) {
1874 		dev_err(&pdev->dev, "failed to load firmware: %d\n", err);
1875 		goto powergate;
1876 	}
1877 
1878 	err = usb_add_hcd(tegra->hcd, tegra->xhci_irq, IRQF_SHARED);
1879 	if (err < 0) {
1880 		dev_err(&pdev->dev, "failed to add USB HCD: %d\n", err);
1881 		goto powergate;
1882 	}
1883 
1884 	device_wakeup_enable(tegra->hcd->self.controller);
1885 
1886 	xhci = hcd_to_xhci(tegra->hcd);
1887 
1888 	xhci->shared_hcd = usb_create_shared_hcd(&tegra_xhci_hc_driver,
1889 						 &pdev->dev,
1890 						 dev_name(&pdev->dev),
1891 						 tegra->hcd);
1892 	if (!xhci->shared_hcd) {
1893 		dev_err(&pdev->dev, "failed to create shared HCD\n");
1894 		err = -ENOMEM;
1895 		goto remove_usb2;
1896 	}
1897 
1898 	if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
1899 		xhci->shared_hcd->can_do_streams = 1;
1900 
1901 	err = usb_add_hcd(xhci->shared_hcd, tegra->xhci_irq, IRQF_SHARED);
1902 	if (err < 0) {
1903 		dev_err(&pdev->dev, "failed to add shared HCD: %d\n", err);
1904 		goto put_usb3;
1905 	}
1906 
1907 	err = devm_request_threaded_irq(&pdev->dev, tegra->mbox_irq,
1908 					tegra_xusb_mbox_irq,
1909 					tegra_xusb_mbox_thread, 0,
1910 					dev_name(&pdev->dev), tegra);
1911 	if (err < 0) {
1912 		dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1913 		goto remove_usb3;
1914 	}
1915 
1916 	if (tegra->padctl_irq) {
1917 		err = devm_request_threaded_irq(&pdev->dev, tegra->padctl_irq,
1918 						NULL, tegra_xusb_padctl_irq,
1919 						IRQF_ONESHOT, dev_name(&pdev->dev),
1920 						tegra);
1921 		if (err < 0) {
1922 			dev_err(&pdev->dev, "failed to request padctl IRQ: %d\n", err);
1923 			goto remove_usb3;
1924 		}
1925 	}
1926 
1927 	err = tegra_xusb_enable_firmware_messages(tegra);
1928 	if (err < 0) {
1929 		dev_err(&pdev->dev, "failed to enable messages: %d\n", err);
1930 		goto remove_usb3;
1931 	}
1932 
1933 	err = tegra_xusb_init_usb_phy(tegra);
1934 	if (err < 0) {
1935 		dev_err(&pdev->dev, "failed to init USB PHY: %d\n", err);
1936 		goto remove_usb3;
1937 	}
1938 
1939 	/* Enable wake for both USB 2.0 and USB 3.0 roothubs */
1940 	device_init_wakeup(&tegra->hcd->self.root_hub->dev, true);
1941 	device_init_wakeup(&xhci->shared_hcd->self.root_hub->dev, true);
1942 
1943 	pm_runtime_use_autosuspend(tegra->dev);
1944 	pm_runtime_set_autosuspend_delay(tegra->dev, 2000);
1945 	pm_runtime_mark_last_busy(tegra->dev);
1946 	pm_runtime_set_active(tegra->dev);
1947 
1948 	if (tegra->padctl_irq) {
1949 		device_init_wakeup(tegra->dev, true);
1950 		pm_runtime_enable(tegra->dev);
1951 	}
1952 
1953 	return 0;
1954 
1955 remove_usb3:
1956 	usb_remove_hcd(xhci->shared_hcd);
1957 put_usb3:
1958 	usb_put_hcd(xhci->shared_hcd);
1959 remove_usb2:
1960 	usb_remove_hcd(tegra->hcd);
1961 powergate:
1962 	tegra_xusb_powergate_partitions(tegra);
1963 free_firmware:
1964 	dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt,
1965 			  tegra->fw.phys);
1966 disable_phy:
1967 	tegra_xusb_phy_disable(tegra);
1968 disable_regulator:
1969 	regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies);
1970 disable_clk:
1971 	tegra_xusb_clk_disable(tegra);
1972 put_hcd:
1973 	usb_put_hcd(tegra->hcd);
1974 put_powerdomains:
1975 	tegra_xusb_powerdomain_remove(&pdev->dev, tegra);
1976 put_padctl:
1977 	of_node_put(np);
1978 	tegra_xusb_padctl_put(tegra->padctl);
1979 dispose_wake:
1980 	tegra_xusb_dispose_wake(tegra);
1981 	return err;
1982 }
1983 
1984 static void tegra_xusb_disable(struct tegra_xusb *tegra)
1985 {
1986 	tegra_xusb_powergate_partitions(tegra);
1987 	tegra_xusb_powerdomain_remove(tegra->dev, tegra);
1988 	tegra_xusb_phy_disable(tegra);
1989 	tegra_xusb_clk_disable(tegra);
1990 	regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies);
1991 }
1992 
1993 static void tegra_xusb_remove(struct platform_device *pdev)
1994 {
1995 	struct tegra_xusb *tegra = platform_get_drvdata(pdev);
1996 	struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
1997 
1998 	tegra_xusb_deinit_usb_phy(tegra);
1999 
2000 	pm_runtime_get_sync(&pdev->dev);
2001 	usb_remove_hcd(xhci->shared_hcd);
2002 	usb_put_hcd(xhci->shared_hcd);
2003 	xhci->shared_hcd = NULL;
2004 	usb_remove_hcd(tegra->hcd);
2005 	usb_put_hcd(tegra->hcd);
2006 
2007 	dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt,
2008 			  tegra->fw.phys);
2009 
2010 	if (tegra->padctl_irq)
2011 		pm_runtime_disable(&pdev->dev);
2012 
2013 	tegra_xusb_dispose_wake(tegra);
2014 
2015 	pm_runtime_put(&pdev->dev);
2016 
2017 	tegra_xusb_disable(tegra);
2018 	tegra_xusb_padctl_put(tegra->padctl);
2019 }
2020 
2021 static void tegra_xusb_shutdown(struct platform_device *pdev)
2022 {
2023 	struct tegra_xusb *tegra = platform_get_drvdata(pdev);
2024 
2025 	pm_runtime_get_sync(&pdev->dev);
2026 	disable_irq(tegra->xhci_irq);
2027 	xhci_shutdown(tegra->hcd);
2028 	tegra_xusb_disable(tegra);
2029 }
2030 
2031 static bool xhci_hub_ports_suspended(struct xhci_hub *hub)
2032 {
2033 	struct device *dev = hub->hcd->self.controller;
2034 	bool status = true;
2035 	unsigned int i;
2036 	u32 value;
2037 
2038 	for (i = 0; i < hub->num_ports; i++) {
2039 		value = readl(hub->ports[i]->addr);
2040 		if ((value & PORT_PE) == 0)
2041 			continue;
2042 
2043 		if ((value & PORT_PLS_MASK) != XDEV_U3) {
2044 			dev_info(dev, "%u-%u isn't suspended: %#010x\n",
2045 				 hub->hcd->self.busnum, i + 1, value);
2046 			status = false;
2047 		}
2048 	}
2049 
2050 	return status;
2051 }
2052 
2053 static int tegra_xusb_check_ports(struct tegra_xusb *tegra)
2054 {
2055 	struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
2056 	struct xhci_bus_state *bus_state = &xhci->usb2_rhub.bus_state;
2057 	unsigned long flags;
2058 	int err = 0;
2059 
2060 	if (bus_state->bus_suspended) {
2061 		/* xusb_hub_suspend() has just directed one or more USB2 port(s)
2062 		 * to U3 state, it takes 3ms to enter U3.
2063 		 */
2064 		usleep_range(3000, 4000);
2065 	}
2066 
2067 	spin_lock_irqsave(&xhci->lock, flags);
2068 
2069 	if (!xhci_hub_ports_suspended(&xhci->usb2_rhub) ||
2070 	    !xhci_hub_ports_suspended(&xhci->usb3_rhub))
2071 		err = -EBUSY;
2072 
2073 	spin_unlock_irqrestore(&xhci->lock, flags);
2074 
2075 	return err;
2076 }
2077 
2078 static void tegra_xusb_save_context(struct tegra_xusb *tegra)
2079 {
2080 	const struct tegra_xusb_context_soc *soc = tegra->soc->context;
2081 	struct tegra_xusb_context *ctx = &tegra->context;
2082 	unsigned int i;
2083 
2084 	if (soc->ipfs.num_offsets > 0) {
2085 		for (i = 0; i < soc->ipfs.num_offsets; i++)
2086 			ctx->ipfs[i] = ipfs_readl(tegra, soc->ipfs.offsets[i]);
2087 	}
2088 
2089 	if (soc->fpci.num_offsets > 0) {
2090 		for (i = 0; i < soc->fpci.num_offsets; i++)
2091 			ctx->fpci[i] = fpci_readl(tegra, soc->fpci.offsets[i]);
2092 	}
2093 }
2094 
2095 static void tegra_xusb_restore_context(struct tegra_xusb *tegra)
2096 {
2097 	const struct tegra_xusb_context_soc *soc = tegra->soc->context;
2098 	struct tegra_xusb_context *ctx = &tegra->context;
2099 	unsigned int i;
2100 
2101 	if (soc->fpci.num_offsets > 0) {
2102 		for (i = 0; i < soc->fpci.num_offsets; i++)
2103 			fpci_writel(tegra, ctx->fpci[i], soc->fpci.offsets[i]);
2104 	}
2105 
2106 	if (soc->ipfs.num_offsets > 0) {
2107 		for (i = 0; i < soc->ipfs.num_offsets; i++)
2108 			ipfs_writel(tegra, ctx->ipfs[i], soc->ipfs.offsets[i]);
2109 	}
2110 }
2111 
2112 static enum usb_device_speed tegra_xhci_portsc_to_speed(struct tegra_xusb *tegra, u32 portsc)
2113 {
2114 	if (DEV_LOWSPEED(portsc))
2115 		return USB_SPEED_LOW;
2116 
2117 	if (DEV_HIGHSPEED(portsc))
2118 		return USB_SPEED_HIGH;
2119 
2120 	if (DEV_FULLSPEED(portsc))
2121 		return USB_SPEED_FULL;
2122 
2123 	if (DEV_SUPERSPEED_ANY(portsc))
2124 		return USB_SPEED_SUPER;
2125 
2126 	return USB_SPEED_UNKNOWN;
2127 }
2128 
2129 static void tegra_xhci_enable_phy_sleepwalk_wake(struct tegra_xusb *tegra)
2130 {
2131 	struct tegra_xusb_padctl *padctl = tegra->padctl;
2132 	struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
2133 	enum usb_device_speed speed;
2134 	struct phy *phy;
2135 	unsigned int index, offset;
2136 	unsigned int i, j, k;
2137 	struct xhci_hub *rhub;
2138 	u32 portsc;
2139 
2140 	for (i = 0, k = 0; i < tegra->soc->num_types; i++) {
2141 		if (strcmp(tegra->soc->phy_types[i].name, "usb3") == 0)
2142 			rhub = &xhci->usb3_rhub;
2143 		else
2144 			rhub = &xhci->usb2_rhub;
2145 
2146 		if (strcmp(tegra->soc->phy_types[i].name, "hsic") == 0)
2147 			offset = tegra->soc->ports.usb2.count;
2148 		else
2149 			offset = 0;
2150 
2151 		for (j = 0; j < tegra->soc->phy_types[i].num; j++) {
2152 			phy = tegra->phys[k++];
2153 
2154 			if (!phy)
2155 				continue;
2156 
2157 			index = j + offset;
2158 
2159 			if (index >= rhub->num_ports)
2160 				continue;
2161 
2162 			if (!is_host_mode_phy(tegra, i, j))
2163 				continue;
2164 
2165 			portsc = readl(rhub->ports[index]->addr);
2166 			speed = tegra_xhci_portsc_to_speed(tegra, portsc);
2167 			tegra_xusb_padctl_enable_phy_sleepwalk(padctl, phy, speed);
2168 			tegra_xusb_padctl_enable_phy_wake(padctl, phy);
2169 		}
2170 	}
2171 }
2172 
2173 static void tegra_xhci_disable_phy_wake(struct tegra_xusb *tegra)
2174 {
2175 	struct tegra_xusb_padctl *padctl = tegra->padctl;
2176 	unsigned int i;
2177 
2178 	for (i = 0; i < tegra->num_usb_phys; i++) {
2179 		struct phy *phy = tegra_xusb_get_phy(tegra, "usb2", i);
2180 
2181 		if (!phy)
2182 			continue;
2183 
2184 		if (tegra_xusb_padctl_remote_wake_detected(padctl, phy))
2185 			tegra_phy_xusb_utmi_pad_power_on(phy);
2186 	}
2187 
2188 	for (i = 0; i < tegra->num_phys; i++) {
2189 		if (!tegra->phys[i])
2190 			continue;
2191 
2192 		if (tegra_xusb_padctl_remote_wake_detected(padctl, tegra->phys[i]))
2193 			dev_dbg(tegra->dev, "%pOF remote wake detected\n",
2194 				tegra->phys[i]->dev.of_node);
2195 
2196 		tegra_xusb_padctl_disable_phy_wake(padctl, tegra->phys[i]);
2197 	}
2198 }
2199 
2200 static void tegra_xhci_disable_phy_sleepwalk(struct tegra_xusb *tegra)
2201 {
2202 	struct tegra_xusb_padctl *padctl = tegra->padctl;
2203 	unsigned int i;
2204 
2205 	for (i = 0; i < tegra->num_phys; i++) {
2206 		if (!tegra->phys[i])
2207 			continue;
2208 
2209 		tegra_xusb_padctl_disable_phy_sleepwalk(padctl, tegra->phys[i]);
2210 	}
2211 }
2212 
2213 static void tegra_xhci_program_utmi_power_lp0_exit(struct tegra_xusb *tegra)
2214 {
2215 	unsigned int i, index_to_usb2;
2216 	struct phy *phy;
2217 
2218 	for (i = 0; i < tegra->soc->num_types; i++) {
2219 		if (strcmp(tegra->soc->phy_types[i].name, "usb2") == 0)
2220 			index_to_usb2 = i;
2221 	}
2222 
2223 	for (i = 0; i < tegra->num_usb_phys; i++) {
2224 		if (!is_host_mode_phy(tegra, index_to_usb2, i))
2225 			continue;
2226 
2227 		phy = tegra_xusb_get_phy(tegra, "usb2", i);
2228 		if (tegra->lp0_utmi_pad_mask & BIT(i))
2229 			tegra_phy_xusb_utmi_pad_power_on(phy);
2230 		else
2231 			tegra_phy_xusb_utmi_pad_power_down(phy);
2232 	}
2233 }
2234 
2235 static int tegra_xusb_enter_elpg(struct tegra_xusb *tegra, bool is_auto_resume)
2236 {
2237 	struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
2238 	struct device *dev = tegra->dev;
2239 	bool wakeup = is_auto_resume ? true : device_may_wakeup(dev);
2240 	unsigned int i;
2241 	int err;
2242 	u32 usbcmd;
2243 	u32 portsc;
2244 
2245 	dev_dbg(dev, "entering ELPG\n");
2246 
2247 	usbcmd = readl(&xhci->op_regs->command);
2248 	usbcmd &= ~CMD_EIE;
2249 	writel(usbcmd, &xhci->op_regs->command);
2250 
2251 	err = tegra_xusb_check_ports(tegra);
2252 	if (err < 0) {
2253 		dev_err(tegra->dev, "not all ports suspended: %d\n", err);
2254 		goto out;
2255 	}
2256 
2257 	for (i = 0; i < xhci->usb2_rhub.num_ports; i++) {
2258 		if (!xhci->usb2_rhub.ports[i])
2259 			continue;
2260 		portsc = readl(xhci->usb2_rhub.ports[i]->addr);
2261 		tegra->lp0_utmi_pad_mask &= ~BIT(i);
2262 		if (((portsc & PORT_PLS_MASK) == XDEV_U3) || ((portsc & DEV_SPEED_MASK) == XDEV_FS))
2263 			tegra->lp0_utmi_pad_mask |= BIT(i);
2264 	}
2265 
2266 	err = xhci_suspend(xhci, wakeup);
2267 	if (err < 0) {
2268 		dev_err(tegra->dev, "failed to suspend XHCI: %d\n", err);
2269 		goto out;
2270 	}
2271 
2272 	tegra_xusb_save_context(tegra);
2273 
2274 	if (wakeup)
2275 		tegra_xhci_enable_phy_sleepwalk_wake(tegra);
2276 
2277 	tegra_xusb_powergate_partitions(tegra);
2278 
2279 	for (i = 0; i < tegra->num_phys; i++) {
2280 		if (!tegra->phys[i])
2281 			continue;
2282 
2283 		phy_power_off(tegra->phys[i]);
2284 		if (!wakeup)
2285 			phy_exit(tegra->phys[i]);
2286 	}
2287 
2288 	tegra_xusb_clk_disable(tegra);
2289 
2290 out:
2291 	if (!err)
2292 		dev_dbg(tegra->dev, "entering ELPG done\n");
2293 	else {
2294 		usbcmd = readl(&xhci->op_regs->command);
2295 		usbcmd |= CMD_EIE;
2296 		writel(usbcmd, &xhci->op_regs->command);
2297 
2298 		dev_dbg(tegra->dev, "entering ELPG failed\n");
2299 		pm_runtime_mark_last_busy(tegra->dev);
2300 	}
2301 
2302 	return err;
2303 }
2304 
2305 static int tegra_xusb_exit_elpg(struct tegra_xusb *tegra, bool is_auto_resume)
2306 {
2307 	struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
2308 	struct device *dev = tegra->dev;
2309 	bool wakeup = is_auto_resume ? true : device_may_wakeup(dev);
2310 	unsigned int i;
2311 	u32 usbcmd;
2312 	int err;
2313 
2314 	dev_dbg(dev, "exiting ELPG\n");
2315 	pm_runtime_mark_last_busy(tegra->dev);
2316 
2317 	err = tegra_xusb_clk_enable(tegra);
2318 	if (err < 0) {
2319 		dev_err(tegra->dev, "failed to enable clocks: %d\n", err);
2320 		goto out;
2321 	}
2322 
2323 	err = tegra_xusb_unpowergate_partitions(tegra);
2324 	if (err)
2325 		goto disable_clks;
2326 
2327 	if (wakeup)
2328 		tegra_xhci_disable_phy_wake(tegra);
2329 
2330 	for (i = 0; i < tegra->num_phys; i++) {
2331 		if (!tegra->phys[i])
2332 			continue;
2333 
2334 		if (!wakeup)
2335 			phy_init(tegra->phys[i]);
2336 
2337 		phy_power_on(tegra->phys[i]);
2338 	}
2339 	if (tegra->suspended)
2340 		tegra_xhci_program_utmi_power_lp0_exit(tegra);
2341 
2342 	tegra_xusb_config(tegra);
2343 	tegra_xusb_restore_context(tegra);
2344 
2345 	err = tegra_xusb_load_firmware(tegra);
2346 	if (err < 0) {
2347 		dev_err(tegra->dev, "failed to load firmware: %d\n", err);
2348 		goto disable_phy;
2349 	}
2350 
2351 	err = __tegra_xusb_enable_firmware_messages(tegra);
2352 	if (err < 0) {
2353 		dev_err(tegra->dev, "failed to enable messages: %d\n", err);
2354 		goto disable_phy;
2355 	}
2356 
2357 	if (wakeup)
2358 		tegra_xhci_disable_phy_sleepwalk(tegra);
2359 
2360 	err = xhci_resume(xhci, false, is_auto_resume);
2361 	if (err < 0) {
2362 		dev_err(tegra->dev, "failed to resume XHCI: %d\n", err);
2363 		goto disable_phy;
2364 	}
2365 
2366 	usbcmd = readl(&xhci->op_regs->command);
2367 	usbcmd |= CMD_EIE;
2368 	writel(usbcmd, &xhci->op_regs->command);
2369 
2370 	goto out;
2371 
2372 disable_phy:
2373 	for (i = 0; i < tegra->num_phys; i++) {
2374 		if (!tegra->phys[i])
2375 			continue;
2376 
2377 		phy_power_off(tegra->phys[i]);
2378 		if (!wakeup)
2379 			phy_exit(tegra->phys[i]);
2380 	}
2381 	tegra_xusb_powergate_partitions(tegra);
2382 disable_clks:
2383 	tegra_xusb_clk_disable(tegra);
2384 out:
2385 	if (!err)
2386 		dev_dbg(dev, "exiting ELPG done\n");
2387 	else
2388 		dev_dbg(dev, "exiting ELPG failed\n");
2389 
2390 	return err;
2391 }
2392 
2393 static __maybe_unused int tegra_xusb_suspend(struct device *dev)
2394 {
2395 	struct tegra_xusb *tegra = dev_get_drvdata(dev);
2396 	int err;
2397 
2398 	synchronize_irq(tegra->mbox_irq);
2399 
2400 	mutex_lock(&tegra->lock);
2401 
2402 	if (pm_runtime_suspended(dev)) {
2403 		err = tegra_xusb_exit_elpg(tegra, true);
2404 		if (err < 0)
2405 			goto out;
2406 	}
2407 
2408 	err = tegra_xusb_enter_elpg(tegra, false);
2409 	if (err < 0) {
2410 		if (pm_runtime_suspended(dev)) {
2411 			pm_runtime_disable(dev);
2412 			pm_runtime_set_active(dev);
2413 			pm_runtime_enable(dev);
2414 		}
2415 
2416 		goto out;
2417 	}
2418 
2419 out:
2420 	if (!err) {
2421 		tegra->suspended = true;
2422 		pm_runtime_disable(dev);
2423 
2424 		if (device_may_wakeup(dev)) {
2425 			unsigned int i;
2426 
2427 			if (enable_irq_wake(tegra->padctl_irq))
2428 				dev_err(dev, "failed to enable padctl wakes\n");
2429 
2430 			for (i = 0; i < tegra->num_wakes; i++)
2431 				enable_irq_wake(tegra->wake_irqs[i]);
2432 		}
2433 	}
2434 
2435 	mutex_unlock(&tegra->lock);
2436 
2437 	return err;
2438 }
2439 
2440 static __maybe_unused int tegra_xusb_resume(struct device *dev)
2441 {
2442 	struct tegra_xusb *tegra = dev_get_drvdata(dev);
2443 	int err;
2444 
2445 	mutex_lock(&tegra->lock);
2446 
2447 	if (!tegra->suspended) {
2448 		mutex_unlock(&tegra->lock);
2449 		return 0;
2450 	}
2451 
2452 	err = tegra_xusb_exit_elpg(tegra, false);
2453 	if (err < 0) {
2454 		mutex_unlock(&tegra->lock);
2455 		return err;
2456 	}
2457 
2458 	if (device_may_wakeup(dev)) {
2459 		unsigned int i;
2460 
2461 		if (disable_irq_wake(tegra->padctl_irq))
2462 			dev_err(dev, "failed to disable padctl wakes\n");
2463 
2464 		for (i = 0; i < tegra->num_wakes; i++)
2465 			disable_irq_wake(tegra->wake_irqs[i]);
2466 	}
2467 	tegra->suspended = false;
2468 	mutex_unlock(&tegra->lock);
2469 
2470 	pm_runtime_set_active(dev);
2471 	pm_runtime_enable(dev);
2472 
2473 	return 0;
2474 }
2475 
2476 static __maybe_unused int tegra_xusb_runtime_suspend(struct device *dev)
2477 {
2478 	struct tegra_xusb *tegra = dev_get_drvdata(dev);
2479 	int ret;
2480 
2481 	synchronize_irq(tegra->mbox_irq);
2482 	mutex_lock(&tegra->lock);
2483 	ret = tegra_xusb_enter_elpg(tegra, true);
2484 	mutex_unlock(&tegra->lock);
2485 
2486 	return ret;
2487 }
2488 
2489 static __maybe_unused int tegra_xusb_runtime_resume(struct device *dev)
2490 {
2491 	struct tegra_xusb *tegra = dev_get_drvdata(dev);
2492 	int err;
2493 
2494 	mutex_lock(&tegra->lock);
2495 	err = tegra_xusb_exit_elpg(tegra, true);
2496 	mutex_unlock(&tegra->lock);
2497 
2498 	return err;
2499 }
2500 
2501 static const struct dev_pm_ops tegra_xusb_pm_ops = {
2502 	SET_RUNTIME_PM_OPS(tegra_xusb_runtime_suspend,
2503 			   tegra_xusb_runtime_resume, NULL)
2504 	SET_SYSTEM_SLEEP_PM_OPS(tegra_xusb_suspend, tegra_xusb_resume)
2505 };
2506 
2507 static const char * const tegra124_supply_names[] = {
2508 	"avddio-pex",
2509 	"dvddio-pex",
2510 	"avdd-usb",
2511 	"hvdd-usb-ss",
2512 };
2513 
2514 static const struct tegra_xusb_phy_type tegra124_phy_types[] = {
2515 	{ .name = "usb3", .num = 2, },
2516 	{ .name = "usb2", .num = 3, },
2517 	{ .name = "hsic", .num = 2, },
2518 };
2519 
2520 static const unsigned int tegra124_xusb_context_ipfs[] = {
2521 	IPFS_XUSB_HOST_MSI_BAR_SZ_0,
2522 	IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0,
2523 	IPFS_XUSB_HOST_MSI_FPCI_BAR_ST_0,
2524 	IPFS_XUSB_HOST_MSI_VEC0_0,
2525 	IPFS_XUSB_HOST_MSI_EN_VEC0_0,
2526 	IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0,
2527 	IPFS_XUSB_HOST_INTR_MASK_0,
2528 	IPFS_XUSB_HOST_INTR_ENABLE_0,
2529 	IPFS_XUSB_HOST_UFPCI_CONFIG_0,
2530 	IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0,
2531 	IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0,
2532 };
2533 
2534 static const unsigned int tegra124_xusb_context_fpci[] = {
2535 	XUSB_CFG_ARU_CONTEXT_HS_PLS,
2536 	XUSB_CFG_ARU_CONTEXT_FS_PLS,
2537 	XUSB_CFG_ARU_CONTEXT_HSFS_SPEED,
2538 	XUSB_CFG_ARU_CONTEXT_HSFS_PP,
2539 	XUSB_CFG_ARU_CONTEXT,
2540 	XUSB_CFG_AXI_CFG,
2541 	XUSB_CFG_24,
2542 	XUSB_CFG_16,
2543 };
2544 
2545 static const struct tegra_xusb_context_soc tegra124_xusb_context = {
2546 	.ipfs = {
2547 		.num_offsets = ARRAY_SIZE(tegra124_xusb_context_ipfs),
2548 		.offsets = tegra124_xusb_context_ipfs,
2549 	},
2550 	.fpci = {
2551 		.num_offsets = ARRAY_SIZE(tegra124_xusb_context_fpci),
2552 		.offsets = tegra124_xusb_context_fpci,
2553 	},
2554 };
2555 
2556 static const struct tegra_xusb_soc_ops tegra124_ops = {
2557 	.mbox_reg_readl = &fpci_readl,
2558 	.mbox_reg_writel = &fpci_writel,
2559 	.csb_reg_readl = &fpci_csb_readl,
2560 	.csb_reg_writel = &fpci_csb_writel,
2561 };
2562 
2563 static const struct tegra_xusb_soc tegra124_soc = {
2564 	.firmware = "nvidia/tegra124/xusb.bin",
2565 	.supply_names = tegra124_supply_names,
2566 	.num_supplies = ARRAY_SIZE(tegra124_supply_names),
2567 	.phy_types = tegra124_phy_types,
2568 	.num_types = ARRAY_SIZE(tegra124_phy_types),
2569 	.context = &tegra124_xusb_context,
2570 	.ports = {
2571 		.usb2 = { .offset = 4, .count = 4, },
2572 		.hsic = { .offset = 6, .count = 2, },
2573 		.usb3 = { .offset = 0, .count = 2, },
2574 	},
2575 	.scale_ss_clock = true,
2576 	.has_ipfs = true,
2577 	.otg_reset_sspi = false,
2578 	.ops = &tegra124_ops,
2579 	.mbox = {
2580 		.cmd = 0xe4,
2581 		.data_in = 0xe8,
2582 		.data_out = 0xec,
2583 		.owner = 0xf0,
2584 		.smi_intr = XUSB_CFG_ARU_SMI_INTR,
2585 	},
2586 };
2587 MODULE_FIRMWARE("nvidia/tegra124/xusb.bin");
2588 
2589 static const char * const tegra210_supply_names[] = {
2590 	"dvddio-pex",
2591 	"hvddio-pex",
2592 	"avdd-usb",
2593 };
2594 
2595 static const struct tegra_xusb_phy_type tegra210_phy_types[] = {
2596 	{ .name = "usb3", .num = 4, },
2597 	{ .name = "usb2", .num = 4, },
2598 	{ .name = "hsic", .num = 1, },
2599 };
2600 
2601 static const struct tegra_xusb_soc tegra210_soc = {
2602 	.firmware = "nvidia/tegra210/xusb.bin",
2603 	.supply_names = tegra210_supply_names,
2604 	.num_supplies = ARRAY_SIZE(tegra210_supply_names),
2605 	.phy_types = tegra210_phy_types,
2606 	.num_types = ARRAY_SIZE(tegra210_phy_types),
2607 	.context = &tegra124_xusb_context,
2608 	.ports = {
2609 		.usb2 = { .offset = 4, .count = 4, },
2610 		.hsic = { .offset = 8, .count = 1, },
2611 		.usb3 = { .offset = 0, .count = 4, },
2612 	},
2613 	.scale_ss_clock = false,
2614 	.has_ipfs = true,
2615 	.otg_reset_sspi = true,
2616 	.ops = &tegra124_ops,
2617 	.mbox = {
2618 		.cmd = 0xe4,
2619 		.data_in = 0xe8,
2620 		.data_out = 0xec,
2621 		.owner = 0xf0,
2622 		.smi_intr = XUSB_CFG_ARU_SMI_INTR,
2623 	},
2624 };
2625 MODULE_FIRMWARE("nvidia/tegra210/xusb.bin");
2626 
2627 static const char * const tegra186_supply_names[] = {
2628 };
2629 MODULE_FIRMWARE("nvidia/tegra186/xusb.bin");
2630 
2631 static const struct tegra_xusb_phy_type tegra186_phy_types[] = {
2632 	{ .name = "usb3", .num = 3, },
2633 	{ .name = "usb2", .num = 3, },
2634 	{ .name = "hsic", .num = 1, },
2635 };
2636 
2637 static const struct tegra_xusb_context_soc tegra186_xusb_context = {
2638 	.fpci = {
2639 		.num_offsets = ARRAY_SIZE(tegra124_xusb_context_fpci),
2640 		.offsets = tegra124_xusb_context_fpci,
2641 	},
2642 };
2643 
2644 static const struct tegra_xusb_soc tegra186_soc = {
2645 	.firmware = "nvidia/tegra186/xusb.bin",
2646 	.supply_names = tegra186_supply_names,
2647 	.num_supplies = ARRAY_SIZE(tegra186_supply_names),
2648 	.phy_types = tegra186_phy_types,
2649 	.num_types = ARRAY_SIZE(tegra186_phy_types),
2650 	.context = &tegra186_xusb_context,
2651 	.ports = {
2652 		.usb3 = { .offset = 0, .count = 3, },
2653 		.usb2 = { .offset = 3, .count = 3, },
2654 		.hsic = { .offset = 6, .count = 1, },
2655 	},
2656 	.scale_ss_clock = false,
2657 	.has_ipfs = false,
2658 	.otg_reset_sspi = false,
2659 	.ops = &tegra124_ops,
2660 	.mbox = {
2661 		.cmd = 0xe4,
2662 		.data_in = 0xe8,
2663 		.data_out = 0xec,
2664 		.owner = 0xf0,
2665 		.smi_intr = XUSB_CFG_ARU_SMI_INTR,
2666 	},
2667 	.lpm_support = true,
2668 };
2669 
2670 static const char * const tegra194_supply_names[] = {
2671 };
2672 
2673 static const struct tegra_xusb_phy_type tegra194_phy_types[] = {
2674 	{ .name = "usb3", .num = 4, },
2675 	{ .name = "usb2", .num = 4, },
2676 };
2677 
2678 static const struct tegra_xusb_soc tegra194_soc = {
2679 	.firmware = "nvidia/tegra194/xusb.bin",
2680 	.supply_names = tegra194_supply_names,
2681 	.num_supplies = ARRAY_SIZE(tegra194_supply_names),
2682 	.phy_types = tegra194_phy_types,
2683 	.num_types = ARRAY_SIZE(tegra194_phy_types),
2684 	.context = &tegra186_xusb_context,
2685 	.ports = {
2686 		.usb3 = { .offset = 0, .count = 4, },
2687 		.usb2 = { .offset = 4, .count = 4, },
2688 	},
2689 	.scale_ss_clock = false,
2690 	.has_ipfs = false,
2691 	.otg_reset_sspi = false,
2692 	.ops = &tegra124_ops,
2693 	.mbox = {
2694 		.cmd = 0x68,
2695 		.data_in = 0x6c,
2696 		.data_out = 0x70,
2697 		.owner = 0x74,
2698 		.smi_intr = XUSB_CFG_ARU_SMI_INTR,
2699 	},
2700 	.lpm_support = true,
2701 };
2702 MODULE_FIRMWARE("nvidia/tegra194/xusb.bin");
2703 
2704 static const struct tegra_xusb_soc_ops tegra234_ops = {
2705 	.mbox_reg_readl = &bar2_readl,
2706 	.mbox_reg_writel = &bar2_writel,
2707 	.csb_reg_readl = &bar2_csb_readl,
2708 	.csb_reg_writel = &bar2_csb_writel,
2709 };
2710 
2711 static const struct tegra_xusb_soc tegra234_soc = {
2712 	.supply_names = tegra194_supply_names,
2713 	.num_supplies = ARRAY_SIZE(tegra194_supply_names),
2714 	.phy_types = tegra194_phy_types,
2715 	.num_types = ARRAY_SIZE(tegra194_phy_types),
2716 	.max_num_wakes = 7,
2717 	.context = &tegra186_xusb_context,
2718 	.ports = {
2719 		.usb3 = { .offset = 0, .count = 4, },
2720 		.usb2 = { .offset = 4, .count = 4, },
2721 	},
2722 	.scale_ss_clock = false,
2723 	.has_ipfs = false,
2724 	.otg_reset_sspi = false,
2725 	.ops = &tegra234_ops,
2726 	.mbox = {
2727 		.cmd = XUSB_BAR2_ARU_MBOX_CMD,
2728 		.data_in = XUSB_BAR2_ARU_MBOX_DATA_IN,
2729 		.data_out = XUSB_BAR2_ARU_MBOX_DATA_OUT,
2730 		.owner = XUSB_BAR2_ARU_MBOX_OWNER,
2731 		.smi_intr = XUSB_BAR2_ARU_SMI_INTR,
2732 	},
2733 	.lpm_support = true,
2734 	.has_bar2 = true,
2735 };
2736 
2737 static const struct of_device_id tegra_xusb_of_match[] = {
2738 	{ .compatible = "nvidia,tegra124-xusb", .data = &tegra124_soc },
2739 	{ .compatible = "nvidia,tegra210-xusb", .data = &tegra210_soc },
2740 	{ .compatible = "nvidia,tegra186-xusb", .data = &tegra186_soc },
2741 	{ .compatible = "nvidia,tegra194-xusb", .data = &tegra194_soc },
2742 	{ .compatible = "nvidia,tegra234-xusb", .data = &tegra234_soc },
2743 	{ },
2744 };
2745 MODULE_DEVICE_TABLE(of, tegra_xusb_of_match);
2746 
2747 static struct platform_driver tegra_xusb_driver = {
2748 	.probe = tegra_xusb_probe,
2749 	.remove = tegra_xusb_remove,
2750 	.shutdown = tegra_xusb_shutdown,
2751 	.driver = {
2752 		.name = "tegra-xusb",
2753 		.pm = &tegra_xusb_pm_ops,
2754 		.of_match_table = tegra_xusb_of_match,
2755 	},
2756 };
2757 
2758 static void tegra_xhci_quirks(struct device *dev, struct xhci_hcd *xhci)
2759 {
2760 	struct tegra_xusb *tegra = dev_get_drvdata(dev);
2761 
2762 	if (tegra && tegra->soc->lpm_support)
2763 		xhci->quirks |= XHCI_LPM_SUPPORT;
2764 }
2765 
2766 static int tegra_xhci_setup(struct usb_hcd *hcd)
2767 {
2768 	return xhci_gen_setup(hcd, tegra_xhci_quirks);
2769 }
2770 
2771 static int tegra_xhci_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
2772 				  char *buf, u16 length)
2773 {
2774 	struct tegra_xusb *tegra = dev_get_drvdata(hcd->self.controller);
2775 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2776 	struct xhci_hub *rhub;
2777 	struct xhci_bus_state *bus_state;
2778 	int port = (index & 0xff) - 1;
2779 	unsigned int i;
2780 	struct xhci_port **ports;
2781 	u32 portsc;
2782 	int ret;
2783 	struct phy *phy;
2784 
2785 	rhub = &xhci->usb2_rhub;
2786 	bus_state = &rhub->bus_state;
2787 	if (bus_state->resuming_ports && hcd->speed == HCD_USB2) {
2788 		ports = rhub->ports;
2789 		i = rhub->num_ports;
2790 		while (i--) {
2791 			if (!test_bit(i, &bus_state->resuming_ports))
2792 				continue;
2793 			portsc = readl(ports[i]->addr);
2794 			if ((portsc & PORT_PLS_MASK) == XDEV_RESUME)
2795 				tegra_phy_xusb_utmi_pad_power_on(
2796 					tegra_xusb_get_phy(tegra, "usb2", (int) i));
2797 		}
2798 	}
2799 
2800 	if (hcd->speed == HCD_USB2) {
2801 		phy = tegra_xusb_get_phy(tegra, "usb2", port);
2802 		if ((type_req == ClearPortFeature) && (value == USB_PORT_FEAT_SUSPEND)) {
2803 			if (!index || index > rhub->num_ports)
2804 				return -EPIPE;
2805 			tegra_phy_xusb_utmi_pad_power_on(phy);
2806 		}
2807 		if ((type_req == SetPortFeature) && (value == USB_PORT_FEAT_RESET)) {
2808 			if (!index || index > rhub->num_ports)
2809 				return -EPIPE;
2810 			ports = rhub->ports;
2811 			portsc = readl(ports[port]->addr);
2812 			if (portsc & PORT_CONNECT)
2813 				tegra_phy_xusb_utmi_pad_power_on(phy);
2814 		}
2815 	}
2816 
2817 	ret = xhci_hub_control(hcd, type_req, value, index, buf, length);
2818 	if (ret < 0)
2819 		return ret;
2820 
2821 	if (hcd->speed == HCD_USB2) {
2822 		/* Use phy where we set previously */
2823 		if ((type_req == SetPortFeature) && (value == USB_PORT_FEAT_SUSPEND))
2824 			/* We don't suspend the PAD while HNP role swap happens on the OTG port */
2825 			if (!((hcd->self.otg_port == (port + 1)) && hcd->self.b_hnp_enable))
2826 				tegra_phy_xusb_utmi_pad_power_down(phy);
2827 
2828 		if ((type_req == ClearPortFeature) && (value == USB_PORT_FEAT_C_CONNECTION)) {
2829 			ports = rhub->ports;
2830 			portsc = readl(ports[port]->addr);
2831 			if (!(portsc & PORT_CONNECT)) {
2832 				/* We don't suspend the PAD while HNP role swap happens on the OTG
2833 				 * port
2834 				 */
2835 				if (!((hcd->self.otg_port == (port + 1)) && hcd->self.b_hnp_enable))
2836 					tegra_phy_xusb_utmi_pad_power_down(phy);
2837 			}
2838 		}
2839 		if ((type_req == SetPortFeature) && (value == USB_PORT_FEAT_TEST))
2840 			tegra_phy_xusb_utmi_pad_power_on(phy);
2841 	}
2842 
2843 	return ret;
2844 }
2845 
2846 static const struct xhci_driver_overrides tegra_xhci_overrides __initconst = {
2847 	.reset = tegra_xhci_setup,
2848 	.hub_control = tegra_xhci_hub_control,
2849 };
2850 
2851 static int __init tegra_xusb_init(void)
2852 {
2853 	xhci_init_driver(&tegra_xhci_hc_driver, &tegra_xhci_overrides);
2854 
2855 	return platform_driver_register(&tegra_xusb_driver);
2856 }
2857 module_init(tegra_xusb_init);
2858 
2859 static void __exit tegra_xusb_exit(void)
2860 {
2861 	platform_driver_unregister(&tegra_xusb_driver);
2862 }
2863 module_exit(tegra_xusb_exit);
2864 
2865 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
2866 MODULE_DESCRIPTION("NVIDIA Tegra XUSB xHCI host-controller driver");
2867 MODULE_LICENSE("GPL v2");
2868