xref: /openbmc/linux/drivers/usb/gadget/udc/tegra-xudc.c (revision a88520bfc0ec829973f92b7a1ab0f64eb9a20724)
149db4272SNagarjuna Kristam // SPDX-License-Identifier: GPL-2.0+
249db4272SNagarjuna Kristam /*
349db4272SNagarjuna Kristam  * NVIDIA Tegra XUSB device mode controller
449db4272SNagarjuna Kristam  *
5*a88520bfSJim Lin  * Copyright (c) 2013-2022, NVIDIA CORPORATION.  All rights reserved.
649db4272SNagarjuna Kristam  * Copyright (c) 2015, Google Inc.
749db4272SNagarjuna Kristam  */
849db4272SNagarjuna Kristam 
949db4272SNagarjuna Kristam #include <linux/clk.h>
1049db4272SNagarjuna Kristam #include <linux/completion.h>
1149db4272SNagarjuna Kristam #include <linux/delay.h>
1249db4272SNagarjuna Kristam #include <linux/dma-mapping.h>
1349db4272SNagarjuna Kristam #include <linux/dmapool.h>
1449db4272SNagarjuna Kristam #include <linux/interrupt.h>
1549db4272SNagarjuna Kristam #include <linux/iopoll.h>
1649db4272SNagarjuna Kristam #include <linux/kernel.h>
1749db4272SNagarjuna Kristam #include <linux/module.h>
1849db4272SNagarjuna Kristam #include <linux/of.h>
1949db4272SNagarjuna Kristam #include <linux/of_device.h>
2049db4272SNagarjuna Kristam #include <linux/phy/phy.h>
2149db4272SNagarjuna Kristam #include <linux/phy/tegra/xusb.h>
2249db4272SNagarjuna Kristam #include <linux/pm_domain.h>
2349db4272SNagarjuna Kristam #include <linux/platform_device.h>
2449db4272SNagarjuna Kristam #include <linux/pm_runtime.h>
2549db4272SNagarjuna Kristam #include <linux/regulator/consumer.h>
2649db4272SNagarjuna Kristam #include <linux/reset.h>
2749db4272SNagarjuna Kristam #include <linux/usb/ch9.h>
2849db4272SNagarjuna Kristam #include <linux/usb/gadget.h>
29b77f2ffeSNagarjuna Kristam #include <linux/usb/otg.h>
3049db4272SNagarjuna Kristam #include <linux/usb/role.h>
31b77f2ffeSNagarjuna Kristam #include <linux/usb/phy.h>
3249db4272SNagarjuna Kristam #include <linux/workqueue.h>
3349db4272SNagarjuna Kristam 
3449db4272SNagarjuna Kristam /* XUSB_DEV registers */
3549db4272SNagarjuna Kristam #define DB 0x004
3649db4272SNagarjuna Kristam #define  DB_TARGET_MASK GENMASK(15, 8)
3749db4272SNagarjuna Kristam #define  DB_TARGET(x) (((x) << 8) & DB_TARGET_MASK)
3849db4272SNagarjuna Kristam #define  DB_STREAMID_MASK GENMASK(31, 16)
3949db4272SNagarjuna Kristam #define  DB_STREAMID(x) (((x) << 16) & DB_STREAMID_MASK)
4049db4272SNagarjuna Kristam #define ERSTSZ 0x008
4149db4272SNagarjuna Kristam #define  ERSTSZ_ERSTXSZ_SHIFT(x) ((x) * 16)
4249db4272SNagarjuna Kristam #define  ERSTSZ_ERSTXSZ_MASK GENMASK(15, 0)
4349db4272SNagarjuna Kristam #define ERSTXBALO(x) (0x010 + 8 * (x))
4449db4272SNagarjuna Kristam #define ERSTXBAHI(x) (0x014 + 8 * (x))
4549db4272SNagarjuna Kristam #define ERDPLO 0x020
4649db4272SNagarjuna Kristam #define  ERDPLO_EHB BIT(3)
4749db4272SNagarjuna Kristam #define ERDPHI 0x024
4849db4272SNagarjuna Kristam #define EREPLO 0x028
4949db4272SNagarjuna Kristam #define  EREPLO_ECS BIT(0)
5049db4272SNagarjuna Kristam #define  EREPLO_SEGI BIT(1)
5149db4272SNagarjuna Kristam #define EREPHI 0x02c
5249db4272SNagarjuna Kristam #define CTRL 0x030
5349db4272SNagarjuna Kristam #define  CTRL_RUN BIT(0)
5449db4272SNagarjuna Kristam #define  CTRL_LSE BIT(1)
5549db4272SNagarjuna Kristam #define  CTRL_IE BIT(4)
5649db4272SNagarjuna Kristam #define  CTRL_SMI_EVT BIT(5)
5749db4272SNagarjuna Kristam #define  CTRL_SMI_DSE BIT(6)
5849db4272SNagarjuna Kristam #define  CTRL_EWE BIT(7)
5949db4272SNagarjuna Kristam #define  CTRL_DEVADDR_MASK GENMASK(30, 24)
6049db4272SNagarjuna Kristam #define  CTRL_DEVADDR(x) (((x) << 24) & CTRL_DEVADDR_MASK)
6149db4272SNagarjuna Kristam #define  CTRL_ENABLE BIT(31)
6249db4272SNagarjuna Kristam #define ST 0x034
6349db4272SNagarjuna Kristam #define  ST_RC BIT(0)
6449db4272SNagarjuna Kristam #define  ST_IP BIT(4)
6549db4272SNagarjuna Kristam #define RT_IMOD	0x038
6649db4272SNagarjuna Kristam #define  RT_IMOD_IMODI_MASK GENMASK(15, 0)
6749db4272SNagarjuna Kristam #define  RT_IMOD_IMODI(x) ((x) & RT_IMOD_IMODI_MASK)
6849db4272SNagarjuna Kristam #define  RT_IMOD_IMODC_MASK GENMASK(31, 16)
6949db4272SNagarjuna Kristam #define  RT_IMOD_IMODC(x) (((x) << 16) & RT_IMOD_IMODC_MASK)
7049db4272SNagarjuna Kristam #define PORTSC 0x03c
7149db4272SNagarjuna Kristam #define  PORTSC_CCS BIT(0)
7249db4272SNagarjuna Kristam #define  PORTSC_PED BIT(1)
7349db4272SNagarjuna Kristam #define  PORTSC_PR BIT(4)
7449db4272SNagarjuna Kristam #define  PORTSC_PLS_SHIFT 5
7549db4272SNagarjuna Kristam #define  PORTSC_PLS_MASK GENMASK(8, 5)
7649db4272SNagarjuna Kristam #define  PORTSC_PLS_U0 0x0
7749db4272SNagarjuna Kristam #define  PORTSC_PLS_U2 0x2
7849db4272SNagarjuna Kristam #define  PORTSC_PLS_U3 0x3
7949db4272SNagarjuna Kristam #define  PORTSC_PLS_DISABLED 0x4
8049db4272SNagarjuna Kristam #define  PORTSC_PLS_RXDETECT 0x5
8149db4272SNagarjuna Kristam #define  PORTSC_PLS_INACTIVE 0x6
8249db4272SNagarjuna Kristam #define  PORTSC_PLS_RESUME 0xf
8349db4272SNagarjuna Kristam #define  PORTSC_PLS(x) (((x) << PORTSC_PLS_SHIFT) & PORTSC_PLS_MASK)
8449db4272SNagarjuna Kristam #define  PORTSC_PS_SHIFT 10
8549db4272SNagarjuna Kristam #define  PORTSC_PS_MASK GENMASK(13, 10)
8649db4272SNagarjuna Kristam #define  PORTSC_PS_UNDEFINED 0x0
8749db4272SNagarjuna Kristam #define  PORTSC_PS_FS 0x1
8849db4272SNagarjuna Kristam #define  PORTSC_PS_LS 0x2
8949db4272SNagarjuna Kristam #define  PORTSC_PS_HS 0x3
9049db4272SNagarjuna Kristam #define  PORTSC_PS_SS 0x4
9149db4272SNagarjuna Kristam #define  PORTSC_LWS BIT(16)
9249db4272SNagarjuna Kristam #define  PORTSC_CSC BIT(17)
9349db4272SNagarjuna Kristam #define  PORTSC_WRC BIT(19)
9449db4272SNagarjuna Kristam #define  PORTSC_PRC BIT(21)
9549db4272SNagarjuna Kristam #define  PORTSC_PLC BIT(22)
9649db4272SNagarjuna Kristam #define  PORTSC_CEC BIT(23)
9749db4272SNagarjuna Kristam #define  PORTSC_WPR BIT(30)
9849db4272SNagarjuna Kristam #define  PORTSC_CHANGE_MASK (PORTSC_CSC | PORTSC_WRC | PORTSC_PRC | \
9949db4272SNagarjuna Kristam 			     PORTSC_PLC | PORTSC_CEC)
10049db4272SNagarjuna Kristam #define ECPLO 0x040
10149db4272SNagarjuna Kristam #define ECPHI 0x044
10249db4272SNagarjuna Kristam #define MFINDEX 0x048
10349db4272SNagarjuna Kristam #define  MFINDEX_FRAME_SHIFT 3
10449db4272SNagarjuna Kristam #define  MFINDEX_FRAME_MASK GENMASK(13, 3)
10549db4272SNagarjuna Kristam #define PORTPM 0x04c
10649db4272SNagarjuna Kristam #define  PORTPM_L1S_MASK GENMASK(1, 0)
10749db4272SNagarjuna Kristam #define  PORTPM_L1S_DROP 0x0
10849db4272SNagarjuna Kristam #define  PORTPM_L1S_ACCEPT 0x1
10949db4272SNagarjuna Kristam #define  PORTPM_L1S_NYET 0x2
11049db4272SNagarjuna Kristam #define  PORTPM_L1S_STALL 0x3
11149db4272SNagarjuna Kristam #define  PORTPM_L1S(x) ((x) & PORTPM_L1S_MASK)
11249db4272SNagarjuna Kristam #define  PORTPM_RWE BIT(3)
11349db4272SNagarjuna Kristam #define  PORTPM_U2TIMEOUT_MASK GENMASK(15, 8)
11449db4272SNagarjuna Kristam #define  PORTPM_U1TIMEOUT_MASK GENMASK(23, 16)
11549db4272SNagarjuna Kristam #define  PORTPM_FLA BIT(24)
11649db4272SNagarjuna Kristam #define  PORTPM_VBA BIT(25)
11749db4272SNagarjuna Kristam #define  PORTPM_WOC BIT(26)
11849db4272SNagarjuna Kristam #define  PORTPM_WOD BIT(27)
11949db4272SNagarjuna Kristam #define  PORTPM_U1E BIT(28)
12049db4272SNagarjuna Kristam #define  PORTPM_U2E BIT(29)
12149db4272SNagarjuna Kristam #define  PORTPM_FRWE BIT(30)
12249db4272SNagarjuna Kristam #define  PORTPM_PNG_CYA BIT(31)
12349db4272SNagarjuna Kristam #define EP_HALT 0x050
12449db4272SNagarjuna Kristam #define EP_PAUSE 0x054
12549db4272SNagarjuna Kristam #define EP_RELOAD 0x058
12649db4272SNagarjuna Kristam #define EP_STCHG 0x05c
12749db4272SNagarjuna Kristam #define DEVNOTIF_LO 0x064
12849db4272SNagarjuna Kristam #define  DEVNOTIF_LO_TRIG BIT(0)
12949db4272SNagarjuna Kristam #define  DEVNOTIF_LO_TYPE_MASK GENMASK(7, 4)
13049db4272SNagarjuna Kristam #define  DEVNOTIF_LO_TYPE(x) (((x) << 4)  & DEVNOTIF_LO_TYPE_MASK)
13149db4272SNagarjuna Kristam #define  DEVNOTIF_LO_TYPE_FUNCTION_WAKE 0x1
13249db4272SNagarjuna Kristam #define DEVNOTIF_HI 0x068
13349db4272SNagarjuna Kristam #define PORTHALT 0x06c
13449db4272SNagarjuna Kristam #define  PORTHALT_HALT_LTSSM BIT(0)
13549db4272SNagarjuna Kristam #define  PORTHALT_HALT_REJECT BIT(1)
13649db4272SNagarjuna Kristam #define  PORTHALT_STCHG_REQ BIT(20)
13749db4272SNagarjuna Kristam #define  PORTHALT_STCHG_INTR_EN BIT(24)
13849db4272SNagarjuna Kristam #define PORT_TM	0x070
13949db4272SNagarjuna Kristam #define EP_THREAD_ACTIVE 0x074
14049db4272SNagarjuna Kristam #define EP_STOPPED 0x078
14149db4272SNagarjuna Kristam #define HSFSPI_COUNT0 0x100
14249db4272SNagarjuna Kristam #define HSFSPI_COUNT13 0x134
14349db4272SNagarjuna Kristam #define  HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK GENMASK(29, 0)
14449db4272SNagarjuna Kristam #define  HSFSPI_COUNT13_U2_RESUME_K_DURATION(x) ((x) & \
14549db4272SNagarjuna Kristam 				HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK)
14649db4272SNagarjuna Kristam #define BLCG 0x840
14749db4272SNagarjuna Kristam #define SSPX_CORE_CNT0 0x610
14849db4272SNagarjuna Kristam #define  SSPX_CORE_CNT0_PING_TBURST_MASK GENMASK(7, 0)
14949db4272SNagarjuna Kristam #define  SSPX_CORE_CNT0_PING_TBURST(x) ((x) & SSPX_CORE_CNT0_PING_TBURST_MASK)
15049db4272SNagarjuna Kristam #define SSPX_CORE_CNT30 0x688
15149db4272SNagarjuna Kristam #define  SSPX_CORE_CNT30_LMPITP_TIMER_MASK GENMASK(19, 0)
15249db4272SNagarjuna Kristam #define  SSPX_CORE_CNT30_LMPITP_TIMER(x) ((x) & \
15349db4272SNagarjuna Kristam 					SSPX_CORE_CNT30_LMPITP_TIMER_MASK)
15449db4272SNagarjuna Kristam #define SSPX_CORE_CNT32 0x690
15549db4272SNagarjuna Kristam #define  SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK GENMASK(7, 0)
15649db4272SNagarjuna Kristam #define  SSPX_CORE_CNT32_POLL_TBURST_MAX(x) ((x) & \
15749db4272SNagarjuna Kristam 					SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK)
15888607a82SNagarjuna Kristam #define SSPX_CORE_CNT56 0x6fc
15988607a82SNagarjuna Kristam #define  SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK GENMASK(19, 0)
16088607a82SNagarjuna Kristam #define  SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX(x) ((x) & \
16188607a82SNagarjuna Kristam 				SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK)
16288607a82SNagarjuna Kristam #define SSPX_CORE_CNT57 0x700
16388607a82SNagarjuna Kristam #define  SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK GENMASK(19, 0)
16488607a82SNagarjuna Kristam #define  SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX(x) ((x) & \
16588607a82SNagarjuna Kristam 				SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK)
16688607a82SNagarjuna Kristam #define SSPX_CORE_CNT65 0x720
16788607a82SNagarjuna Kristam #define  SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK GENMASK(19, 0)
16888607a82SNagarjuna Kristam #define  SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID(x) ((x) & \
16988607a82SNagarjuna Kristam 				SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK)
17088607a82SNagarjuna Kristam #define SSPX_CORE_CNT66 0x724
17188607a82SNagarjuna Kristam #define  SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK GENMASK(19, 0)
17288607a82SNagarjuna Kristam #define  SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID(x) ((x) & \
17388607a82SNagarjuna Kristam 				SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK)
17488607a82SNagarjuna Kristam #define SSPX_CORE_CNT67 0x728
17588607a82SNagarjuna Kristam #define  SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK GENMASK(19, 0)
17688607a82SNagarjuna Kristam #define  SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID(x) ((x) & \
17788607a82SNagarjuna Kristam 				SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK)
17888607a82SNagarjuna Kristam #define SSPX_CORE_CNT72 0x73c
17988607a82SNagarjuna Kristam #define  SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK GENMASK(19, 0)
18088607a82SNagarjuna Kristam #define  SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT(x) ((x) & \
18188607a82SNagarjuna Kristam 				SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK)
18249db4272SNagarjuna Kristam #define SSPX_CORE_PADCTL4 0x750
18349db4272SNagarjuna Kristam #define  SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK GENMASK(19, 0)
18449db4272SNagarjuna Kristam #define  SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3(x) ((x) & \
18549db4272SNagarjuna Kristam 				SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK)
18649db4272SNagarjuna Kristam #define  BLCG_DFPCI BIT(0)
18749db4272SNagarjuna Kristam #define  BLCG_UFPCI BIT(1)
18849db4272SNagarjuna Kristam #define  BLCG_FE BIT(2)
18949db4272SNagarjuna Kristam #define  BLCG_COREPLL_PWRDN BIT(8)
19049db4272SNagarjuna Kristam #define  BLCG_IOPLL_0_PWRDN BIT(9)
19149db4272SNagarjuna Kristam #define  BLCG_IOPLL_1_PWRDN BIT(10)
19249db4272SNagarjuna Kristam #define  BLCG_IOPLL_2_PWRDN BIT(11)
19349db4272SNagarjuna Kristam #define  BLCG_ALL 0x1ff
19449db4272SNagarjuna Kristam #define CFG_DEV_SSPI_XFER 0x858
19549db4272SNagarjuna Kristam #define  CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK GENMASK(31, 0)
19649db4272SNagarjuna Kristam #define  CFG_DEV_SSPI_XFER_ACKTIMEOUT(x) ((x) & \
19749db4272SNagarjuna Kristam 					CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK)
19849db4272SNagarjuna Kristam #define CFG_DEV_FE 0x85c
19949db4272SNagarjuna Kristam #define  CFG_DEV_FE_PORTREGSEL_MASK GENMASK(1, 0)
20049db4272SNagarjuna Kristam #define  CFG_DEV_FE_PORTREGSEL_SS_PI 1
20149db4272SNagarjuna Kristam #define  CFG_DEV_FE_PORTREGSEL_HSFS_PI 2
20249db4272SNagarjuna Kristam #define  CFG_DEV_FE_PORTREGSEL(x) ((x) & CFG_DEV_FE_PORTREGSEL_MASK)
20349db4272SNagarjuna Kristam #define  CFG_DEV_FE_INFINITE_SS_RETRY BIT(29)
20449db4272SNagarjuna Kristam 
20549db4272SNagarjuna Kristam /* FPCI registers */
20649db4272SNagarjuna Kristam #define XUSB_DEV_CFG_1 0x004
20749db4272SNagarjuna Kristam #define  XUSB_DEV_CFG_1_IO_SPACE_EN BIT(0)
20849db4272SNagarjuna Kristam #define  XUSB_DEV_CFG_1_MEMORY_SPACE_EN BIT(1)
20949db4272SNagarjuna Kristam #define  XUSB_DEV_CFG_1_BUS_MASTER_EN BIT(2)
21049db4272SNagarjuna Kristam #define XUSB_DEV_CFG_4 0x010
21149db4272SNagarjuna Kristam #define  XUSB_DEV_CFG_4_BASE_ADDR_MASK GENMASK(31, 15)
21249db4272SNagarjuna Kristam #define XUSB_DEV_CFG_5 0x014
21349db4272SNagarjuna Kristam 
21449db4272SNagarjuna Kristam /* IPFS registers */
21549db4272SNagarjuna Kristam #define XUSB_DEV_CONFIGURATION_0 0x180
21649db4272SNagarjuna Kristam #define  XUSB_DEV_CONFIGURATION_0_EN_FPCI BIT(0)
21749db4272SNagarjuna Kristam #define XUSB_DEV_INTR_MASK_0 0x188
21849db4272SNagarjuna Kristam #define  XUSB_DEV_INTR_MASK_0_IP_INT_MASK BIT(16)
21949db4272SNagarjuna Kristam 
22049db4272SNagarjuna Kristam struct tegra_xudc_ep_context {
22149db4272SNagarjuna Kristam 	__le32 info0;
22249db4272SNagarjuna Kristam 	__le32 info1;
22349db4272SNagarjuna Kristam 	__le32 deq_lo;
22449db4272SNagarjuna Kristam 	__le32 deq_hi;
22549db4272SNagarjuna Kristam 	__le32 tx_info;
22649db4272SNagarjuna Kristam 	__le32 rsvd[11];
22749db4272SNagarjuna Kristam };
22849db4272SNagarjuna Kristam 
22949db4272SNagarjuna Kristam #define EP_STATE_DISABLED 0
23049db4272SNagarjuna Kristam #define EP_STATE_RUNNING 1
23149db4272SNagarjuna Kristam #define EP_STATE_HALTED 2
23249db4272SNagarjuna Kristam #define EP_STATE_STOPPED 3
23349db4272SNagarjuna Kristam #define EP_STATE_ERROR 4
23449db4272SNagarjuna Kristam 
23549db4272SNagarjuna Kristam #define EP_TYPE_INVALID 0
23649db4272SNagarjuna Kristam #define EP_TYPE_ISOCH_OUT 1
23749db4272SNagarjuna Kristam #define EP_TYPE_BULK_OUT 2
23849db4272SNagarjuna Kristam #define EP_TYPE_INTERRUPT_OUT 3
23949db4272SNagarjuna Kristam #define EP_TYPE_CONTROL 4
24049db4272SNagarjuna Kristam #define EP_TYPE_ISCOH_IN 5
24149db4272SNagarjuna Kristam #define EP_TYPE_BULK_IN 6
24249db4272SNagarjuna Kristam #define EP_TYPE_INTERRUPT_IN 7
24349db4272SNagarjuna Kristam 
24449db4272SNagarjuna Kristam #define BUILD_EP_CONTEXT_RW(name, member, shift, mask)			\
24549db4272SNagarjuna Kristam static inline u32 ep_ctx_read_##name(struct tegra_xudc_ep_context *ctx)	\
24649db4272SNagarjuna Kristam {									\
24749db4272SNagarjuna Kristam 	return (le32_to_cpu(ctx->member) >> (shift)) & (mask);		\
24849db4272SNagarjuna Kristam }									\
24949db4272SNagarjuna Kristam static inline void							\
25049db4272SNagarjuna Kristam ep_ctx_write_##name(struct tegra_xudc_ep_context *ctx, u32 val)		\
25149db4272SNagarjuna Kristam {									\
25249db4272SNagarjuna Kristam 	u32 tmp;							\
25349db4272SNagarjuna Kristam 									\
25449db4272SNagarjuna Kristam 	tmp = le32_to_cpu(ctx->member) & ~((mask) << (shift));		\
25549db4272SNagarjuna Kristam 	tmp |= (val & (mask)) << (shift);				\
25649db4272SNagarjuna Kristam 	ctx->member = cpu_to_le32(tmp);					\
25749db4272SNagarjuna Kristam }
25849db4272SNagarjuna Kristam 
25949db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(state, info0, 0, 0x7)
26049db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(mult, info0, 8, 0x3)
26149db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(max_pstreams, info0, 10, 0x1f)
26249db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(lsa, info0, 15, 0x1)
26349db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(interval, info0, 16, 0xff)
26449db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(cerr, info1, 1, 0x3)
26549db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(type, info1, 3, 0x7)
26649db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(hid, info1, 7, 0x1)
26749db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(max_burst_size, info1, 8, 0xff)
26849db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(max_packet_size, info1, 16, 0xffff)
26949db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(dcs, deq_lo, 0, 0x1)
27049db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(deq_lo, deq_lo, 4, 0xfffffff)
27149db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(deq_hi, deq_hi, 0, 0xffffffff)
27249db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(avg_trb_len, tx_info, 0, 0xffff)
27349db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(max_esit_payload, tx_info, 16, 0xffff)
27449db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(edtla, rsvd[0], 0, 0xffffff)
2757bd42fb9SWayne Chang BUILD_EP_CONTEXT_RW(rsvd, rsvd[0], 24, 0x1)
27649db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(partial_td, rsvd[0], 25, 0x1)
2777bd42fb9SWayne Chang BUILD_EP_CONTEXT_RW(splitxstate, rsvd[0], 26, 0x1)
2787bd42fb9SWayne Chang BUILD_EP_CONTEXT_RW(seq_num, rsvd[0], 27, 0x1f)
27949db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(cerrcnt, rsvd[1], 18, 0x3)
28049db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(data_offset, rsvd[2], 0, 0x1ffff)
28149db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(numtrbs, rsvd[2], 22, 0x1f)
28249db4272SNagarjuna Kristam BUILD_EP_CONTEXT_RW(devaddr, rsvd[6], 0, 0x7f)
28349db4272SNagarjuna Kristam 
28449db4272SNagarjuna Kristam static inline u64 ep_ctx_read_deq_ptr(struct tegra_xudc_ep_context *ctx)
28549db4272SNagarjuna Kristam {
28649db4272SNagarjuna Kristam 	return ((u64)ep_ctx_read_deq_hi(ctx) << 32) |
28749db4272SNagarjuna Kristam 		(ep_ctx_read_deq_lo(ctx) << 4);
28849db4272SNagarjuna Kristam }
28949db4272SNagarjuna Kristam 
29049db4272SNagarjuna Kristam static inline void
29149db4272SNagarjuna Kristam ep_ctx_write_deq_ptr(struct tegra_xudc_ep_context *ctx, u64 addr)
29249db4272SNagarjuna Kristam {
29349db4272SNagarjuna Kristam 	ep_ctx_write_deq_lo(ctx, lower_32_bits(addr) >> 4);
29449db4272SNagarjuna Kristam 	ep_ctx_write_deq_hi(ctx, upper_32_bits(addr));
29549db4272SNagarjuna Kristam }
29649db4272SNagarjuna Kristam 
29749db4272SNagarjuna Kristam struct tegra_xudc_trb {
29849db4272SNagarjuna Kristam 	__le32 data_lo;
29949db4272SNagarjuna Kristam 	__le32 data_hi;
30049db4272SNagarjuna Kristam 	__le32 status;
30149db4272SNagarjuna Kristam 	__le32 control;
30249db4272SNagarjuna Kristam };
30349db4272SNagarjuna Kristam 
30449db4272SNagarjuna Kristam #define TRB_TYPE_RSVD 0
30549db4272SNagarjuna Kristam #define TRB_TYPE_NORMAL 1
30649db4272SNagarjuna Kristam #define TRB_TYPE_SETUP_STAGE 2
30749db4272SNagarjuna Kristam #define TRB_TYPE_DATA_STAGE 3
30849db4272SNagarjuna Kristam #define TRB_TYPE_STATUS_STAGE 4
30949db4272SNagarjuna Kristam #define TRB_TYPE_ISOCH 5
31049db4272SNagarjuna Kristam #define TRB_TYPE_LINK 6
31149db4272SNagarjuna Kristam #define TRB_TYPE_TRANSFER_EVENT 32
31249db4272SNagarjuna Kristam #define TRB_TYPE_PORT_STATUS_CHANGE_EVENT 34
31349db4272SNagarjuna Kristam #define TRB_TYPE_STREAM 48
31449db4272SNagarjuna Kristam #define TRB_TYPE_SETUP_PACKET_EVENT 63
31549db4272SNagarjuna Kristam 
31649db4272SNagarjuna Kristam #define TRB_CMPL_CODE_INVALID 0
31749db4272SNagarjuna Kristam #define TRB_CMPL_CODE_SUCCESS 1
31849db4272SNagarjuna Kristam #define TRB_CMPL_CODE_DATA_BUFFER_ERR 2
31949db4272SNagarjuna Kristam #define TRB_CMPL_CODE_BABBLE_DETECTED_ERR 3
32049db4272SNagarjuna Kristam #define TRB_CMPL_CODE_USB_TRANS_ERR 4
32149db4272SNagarjuna Kristam #define TRB_CMPL_CODE_TRB_ERR 5
32249db4272SNagarjuna Kristam #define TRB_CMPL_CODE_STALL 6
32349db4272SNagarjuna Kristam #define TRB_CMPL_CODE_INVALID_STREAM_TYPE_ERR 10
32449db4272SNagarjuna Kristam #define TRB_CMPL_CODE_SHORT_PACKET 13
32549db4272SNagarjuna Kristam #define TRB_CMPL_CODE_RING_UNDERRUN 14
32649db4272SNagarjuna Kristam #define TRB_CMPL_CODE_RING_OVERRUN 15
32749db4272SNagarjuna Kristam #define TRB_CMPL_CODE_EVENT_RING_FULL_ERR 21
32849db4272SNagarjuna Kristam #define TRB_CMPL_CODE_STOPPED 26
32949db4272SNagarjuna Kristam #define TRB_CMPL_CODE_ISOCH_BUFFER_OVERRUN 31
33049db4272SNagarjuna Kristam #define TRB_CMPL_CODE_STREAM_NUMP_ERROR 219
33149db4272SNagarjuna Kristam #define TRB_CMPL_CODE_PRIME_PIPE_RECEIVED 220
33249db4272SNagarjuna Kristam #define TRB_CMPL_CODE_HOST_REJECTED 221
33349db4272SNagarjuna Kristam #define TRB_CMPL_CODE_CTRL_DIR_ERR 222
33449db4272SNagarjuna Kristam #define TRB_CMPL_CODE_CTRL_SEQNUM_ERR 223
33549db4272SNagarjuna Kristam 
33649db4272SNagarjuna Kristam #define BUILD_TRB_RW(name, member, shift, mask)				\
33749db4272SNagarjuna Kristam static inline u32 trb_read_##name(struct tegra_xudc_trb *trb)		\
33849db4272SNagarjuna Kristam {									\
33949db4272SNagarjuna Kristam 	return (le32_to_cpu(trb->member) >> (shift)) & (mask);		\
34049db4272SNagarjuna Kristam }									\
34149db4272SNagarjuna Kristam static inline void							\
34249db4272SNagarjuna Kristam trb_write_##name(struct tegra_xudc_trb *trb, u32 val)			\
34349db4272SNagarjuna Kristam {									\
34449db4272SNagarjuna Kristam 	u32 tmp;							\
34549db4272SNagarjuna Kristam 									\
34649db4272SNagarjuna Kristam 	tmp = le32_to_cpu(trb->member) & ~((mask) << (shift));		\
34749db4272SNagarjuna Kristam 	tmp |= (val & (mask)) << (shift);				\
34849db4272SNagarjuna Kristam 	trb->member = cpu_to_le32(tmp);					\
34949db4272SNagarjuna Kristam }
35049db4272SNagarjuna Kristam 
35149db4272SNagarjuna Kristam BUILD_TRB_RW(data_lo, data_lo, 0, 0xffffffff)
35249db4272SNagarjuna Kristam BUILD_TRB_RW(data_hi, data_hi, 0, 0xffffffff)
35349db4272SNagarjuna Kristam BUILD_TRB_RW(seq_num, status, 0, 0xffff)
35449db4272SNagarjuna Kristam BUILD_TRB_RW(transfer_len, status, 0, 0xffffff)
35549db4272SNagarjuna Kristam BUILD_TRB_RW(td_size, status, 17, 0x1f)
35649db4272SNagarjuna Kristam BUILD_TRB_RW(cmpl_code, status, 24, 0xff)
35749db4272SNagarjuna Kristam BUILD_TRB_RW(cycle, control, 0, 0x1)
35849db4272SNagarjuna Kristam BUILD_TRB_RW(toggle_cycle, control, 1, 0x1)
35949db4272SNagarjuna Kristam BUILD_TRB_RW(isp, control, 2, 0x1)
36049db4272SNagarjuna Kristam BUILD_TRB_RW(chain, control, 4, 0x1)
36149db4272SNagarjuna Kristam BUILD_TRB_RW(ioc, control, 5, 0x1)
36249db4272SNagarjuna Kristam BUILD_TRB_RW(type, control, 10, 0x3f)
36349db4272SNagarjuna Kristam BUILD_TRB_RW(stream_id, control, 16, 0xffff)
36449db4272SNagarjuna Kristam BUILD_TRB_RW(endpoint_id, control, 16, 0x1f)
36549db4272SNagarjuna Kristam BUILD_TRB_RW(tlbpc, control, 16, 0xf)
36649db4272SNagarjuna Kristam BUILD_TRB_RW(data_stage_dir, control, 16, 0x1)
36749db4272SNagarjuna Kristam BUILD_TRB_RW(frame_id, control, 20, 0x7ff)
36849db4272SNagarjuna Kristam BUILD_TRB_RW(sia, control, 31, 0x1)
36949db4272SNagarjuna Kristam 
37049db4272SNagarjuna Kristam static inline u64 trb_read_data_ptr(struct tegra_xudc_trb *trb)
37149db4272SNagarjuna Kristam {
37249db4272SNagarjuna Kristam 	return ((u64)trb_read_data_hi(trb) << 32) |
37349db4272SNagarjuna Kristam 		trb_read_data_lo(trb);
37449db4272SNagarjuna Kristam }
37549db4272SNagarjuna Kristam 
37649db4272SNagarjuna Kristam static inline void trb_write_data_ptr(struct tegra_xudc_trb *trb, u64 addr)
37749db4272SNagarjuna Kristam {
37849db4272SNagarjuna Kristam 	trb_write_data_lo(trb, lower_32_bits(addr));
37949db4272SNagarjuna Kristam 	trb_write_data_hi(trb, upper_32_bits(addr));
38049db4272SNagarjuna Kristam }
38149db4272SNagarjuna Kristam 
38249db4272SNagarjuna Kristam struct tegra_xudc_request {
38349db4272SNagarjuna Kristam 	struct usb_request usb_req;
38449db4272SNagarjuna Kristam 
38549db4272SNagarjuna Kristam 	size_t buf_queued;
38649db4272SNagarjuna Kristam 	unsigned int trbs_queued;
38749db4272SNagarjuna Kristam 	unsigned int trbs_needed;
38849db4272SNagarjuna Kristam 	bool need_zlp;
38949db4272SNagarjuna Kristam 
39049db4272SNagarjuna Kristam 	struct tegra_xudc_trb *first_trb;
39149db4272SNagarjuna Kristam 	struct tegra_xudc_trb *last_trb;
39249db4272SNagarjuna Kristam 
39349db4272SNagarjuna Kristam 	struct list_head list;
39449db4272SNagarjuna Kristam };
39549db4272SNagarjuna Kristam 
39649db4272SNagarjuna Kristam struct tegra_xudc_ep {
39749db4272SNagarjuna Kristam 	struct tegra_xudc *xudc;
39849db4272SNagarjuna Kristam 	struct usb_ep usb_ep;
39949db4272SNagarjuna Kristam 	unsigned int index;
40049db4272SNagarjuna Kristam 	char name[8];
40149db4272SNagarjuna Kristam 
40249db4272SNagarjuna Kristam 	struct tegra_xudc_ep_context *context;
40349db4272SNagarjuna Kristam 
40449db4272SNagarjuna Kristam #define XUDC_TRANSFER_RING_SIZE 64
40549db4272SNagarjuna Kristam 	struct tegra_xudc_trb *transfer_ring;
40649db4272SNagarjuna Kristam 	dma_addr_t transfer_ring_phys;
40749db4272SNagarjuna Kristam 
40849db4272SNagarjuna Kristam 	unsigned int enq_ptr;
40949db4272SNagarjuna Kristam 	unsigned int deq_ptr;
41049db4272SNagarjuna Kristam 	bool pcs;
41149db4272SNagarjuna Kristam 	bool ring_full;
41249db4272SNagarjuna Kristam 	bool stream_rejected;
41349db4272SNagarjuna Kristam 
41449db4272SNagarjuna Kristam 	struct list_head queue;
41549db4272SNagarjuna Kristam 	const struct usb_endpoint_descriptor *desc;
41649db4272SNagarjuna Kristam 	const struct usb_ss_ep_comp_descriptor *comp_desc;
41749db4272SNagarjuna Kristam };
41849db4272SNagarjuna Kristam 
41949db4272SNagarjuna Kristam struct tegra_xudc_sel_timing {
42049db4272SNagarjuna Kristam 	__u8 u1sel;
42149db4272SNagarjuna Kristam 	__u8 u1pel;
42249db4272SNagarjuna Kristam 	__le16 u2sel;
42349db4272SNagarjuna Kristam 	__le16 u2pel;
42449db4272SNagarjuna Kristam };
42549db4272SNagarjuna Kristam 
42649db4272SNagarjuna Kristam enum tegra_xudc_setup_state {
42749db4272SNagarjuna Kristam 	WAIT_FOR_SETUP,
42849db4272SNagarjuna Kristam 	DATA_STAGE_XFER,
42949db4272SNagarjuna Kristam 	DATA_STAGE_RECV,
43049db4272SNagarjuna Kristam 	STATUS_STAGE_XFER,
43149db4272SNagarjuna Kristam 	STATUS_STAGE_RECV,
43249db4272SNagarjuna Kristam };
43349db4272SNagarjuna Kristam 
43449db4272SNagarjuna Kristam struct tegra_xudc_setup_packet {
43549db4272SNagarjuna Kristam 	struct usb_ctrlrequest ctrl_req;
43649db4272SNagarjuna Kristam 	unsigned int seq_num;
43749db4272SNagarjuna Kristam };
43849db4272SNagarjuna Kristam 
43949db4272SNagarjuna Kristam struct tegra_xudc_save_regs {
44049db4272SNagarjuna Kristam 	u32 ctrl;
44149db4272SNagarjuna Kristam 	u32 portpm;
44249db4272SNagarjuna Kristam };
44349db4272SNagarjuna Kristam 
44449db4272SNagarjuna Kristam struct tegra_xudc {
44549db4272SNagarjuna Kristam 	struct device *dev;
44649db4272SNagarjuna Kristam 	const struct tegra_xudc_soc *soc;
44749db4272SNagarjuna Kristam 	struct tegra_xusb_padctl *padctl;
44849db4272SNagarjuna Kristam 
44949db4272SNagarjuna Kristam 	spinlock_t lock;
45049db4272SNagarjuna Kristam 
45149db4272SNagarjuna Kristam 	struct usb_gadget gadget;
45249db4272SNagarjuna Kristam 	struct usb_gadget_driver *driver;
45349db4272SNagarjuna Kristam 
45449db4272SNagarjuna Kristam #define XUDC_NR_EVENT_RINGS 2
45549db4272SNagarjuna Kristam #define XUDC_EVENT_RING_SIZE 4096
45649db4272SNagarjuna Kristam 	struct tegra_xudc_trb *event_ring[XUDC_NR_EVENT_RINGS];
45749db4272SNagarjuna Kristam 	dma_addr_t event_ring_phys[XUDC_NR_EVENT_RINGS];
45849db4272SNagarjuna Kristam 	unsigned int event_ring_index;
45949db4272SNagarjuna Kristam 	unsigned int event_ring_deq_ptr;
46049db4272SNagarjuna Kristam 	bool ccs;
46149db4272SNagarjuna Kristam 
46249db4272SNagarjuna Kristam #define XUDC_NR_EPS 32
46349db4272SNagarjuna Kristam 	struct tegra_xudc_ep ep[XUDC_NR_EPS];
46449db4272SNagarjuna Kristam 	struct tegra_xudc_ep_context *ep_context;
46549db4272SNagarjuna Kristam 	dma_addr_t ep_context_phys;
46649db4272SNagarjuna Kristam 
46749db4272SNagarjuna Kristam 	struct device *genpd_dev_device;
46849db4272SNagarjuna Kristam 	struct device *genpd_dev_ss;
46949db4272SNagarjuna Kristam 	struct device_link *genpd_dl_device;
47049db4272SNagarjuna Kristam 	struct device_link *genpd_dl_ss;
47149db4272SNagarjuna Kristam 
47249db4272SNagarjuna Kristam 	struct dma_pool *transfer_ring_pool;
47349db4272SNagarjuna Kristam 
47449db4272SNagarjuna Kristam 	bool queued_setup_packet;
47549db4272SNagarjuna Kristam 	struct tegra_xudc_setup_packet setup_packet;
47649db4272SNagarjuna Kristam 	enum tegra_xudc_setup_state setup_state;
47749db4272SNagarjuna Kristam 	u16 setup_seq_num;
47849db4272SNagarjuna Kristam 
47949db4272SNagarjuna Kristam 	u16 dev_addr;
48049db4272SNagarjuna Kristam 	u16 isoch_delay;
48149db4272SNagarjuna Kristam 	struct tegra_xudc_sel_timing sel_timing;
48249db4272SNagarjuna Kristam 	u8 test_mode_pattern;
48349db4272SNagarjuna Kristam 	u16 status_buf;
48449db4272SNagarjuna Kristam 	struct tegra_xudc_request *ep0_req;
48549db4272SNagarjuna Kristam 
48649db4272SNagarjuna Kristam 	bool pullup;
48749db4272SNagarjuna Kristam 
48849db4272SNagarjuna Kristam 	unsigned int nr_enabled_eps;
48949db4272SNagarjuna Kristam 	unsigned int nr_isoch_eps;
49049db4272SNagarjuna Kristam 
49149db4272SNagarjuna Kristam 	unsigned int device_state;
49249db4272SNagarjuna Kristam 	unsigned int resume_state;
49349db4272SNagarjuna Kristam 
49449db4272SNagarjuna Kristam 	int irq;
49549db4272SNagarjuna Kristam 
49649db4272SNagarjuna Kristam 	void __iomem *base;
49749db4272SNagarjuna Kristam 	resource_size_t phys_base;
49849db4272SNagarjuna Kristam 	void __iomem *ipfs;
49949db4272SNagarjuna Kristam 	void __iomem *fpci;
50049db4272SNagarjuna Kristam 
50149db4272SNagarjuna Kristam 	struct regulator_bulk_data *supplies;
50249db4272SNagarjuna Kristam 
50349db4272SNagarjuna Kristam 	struct clk_bulk_data *clks;
50449db4272SNagarjuna Kristam 
5059ce0a14bSNagarjuna Kristam 	bool device_mode;
50649db4272SNagarjuna Kristam 	struct work_struct usb_role_sw_work;
50749db4272SNagarjuna Kristam 
508b4e19931SNagarjuna Kristam 	struct phy **usb3_phy;
509b4e19931SNagarjuna Kristam 	struct phy *curr_usb3_phy;
510b4e19931SNagarjuna Kristam 	struct phy **utmi_phy;
511b4e19931SNagarjuna Kristam 	struct phy *curr_utmi_phy;
51249db4272SNagarjuna Kristam 
51349db4272SNagarjuna Kristam 	struct tegra_xudc_save_regs saved_regs;
51449db4272SNagarjuna Kristam 	bool suspended;
51549db4272SNagarjuna Kristam 	bool powergated;
51649db4272SNagarjuna Kristam 
517b4e19931SNagarjuna Kristam 	struct usb_phy **usbphy;
518ac82b56bSNagarjuna Kristam 	struct usb_phy *curr_usbphy;
519b77f2ffeSNagarjuna Kristam 	struct notifier_block vbus_nb;
520b77f2ffeSNagarjuna Kristam 
52149db4272SNagarjuna Kristam 	struct completion disconnect_complete;
52249db4272SNagarjuna Kristam 
52349db4272SNagarjuna Kristam 	bool selfpowered;
52449db4272SNagarjuna Kristam 
52549db4272SNagarjuna Kristam #define TOGGLE_VBUS_WAIT_MS 100
52649db4272SNagarjuna Kristam 	struct delayed_work plc_reset_work;
52749db4272SNagarjuna Kristam 	bool wait_csc;
52849db4272SNagarjuna Kristam 
52949db4272SNagarjuna Kristam 	struct delayed_work port_reset_war_work;
53049db4272SNagarjuna Kristam 	bool wait_for_sec_prc;
53149db4272SNagarjuna Kristam };
53249db4272SNagarjuna Kristam 
53349db4272SNagarjuna Kristam #define XUDC_TRB_MAX_BUFFER_SIZE 65536
53449db4272SNagarjuna Kristam #define XUDC_MAX_ISOCH_EPS 4
53549db4272SNagarjuna Kristam #define XUDC_INTERRUPT_MODERATION_US 0
53649db4272SNagarjuna Kristam 
53749db4272SNagarjuna Kristam static struct usb_endpoint_descriptor tegra_xudc_ep0_desc = {
53849db4272SNagarjuna Kristam 	.bLength = USB_DT_ENDPOINT_SIZE,
53949db4272SNagarjuna Kristam 	.bDescriptorType = USB_DT_ENDPOINT,
54049db4272SNagarjuna Kristam 	.bEndpointAddress = 0,
54149db4272SNagarjuna Kristam 	.bmAttributes = USB_ENDPOINT_XFER_CONTROL,
54249db4272SNagarjuna Kristam 	.wMaxPacketSize = cpu_to_le16(64),
54349db4272SNagarjuna Kristam };
54449db4272SNagarjuna Kristam 
54549db4272SNagarjuna Kristam struct tegra_xudc_soc {
54649db4272SNagarjuna Kristam 	const char * const *supply_names;
54749db4272SNagarjuna Kristam 	unsigned int num_supplies;
54849db4272SNagarjuna Kristam 	const char * const *clock_names;
54949db4272SNagarjuna Kristam 	unsigned int num_clks;
550b4e19931SNagarjuna Kristam 	unsigned int num_phys;
55149db4272SNagarjuna Kristam 	bool u1_enable;
55249db4272SNagarjuna Kristam 	bool u2_enable;
55349db4272SNagarjuna Kristam 	bool lpm_enable;
55449db4272SNagarjuna Kristam 	bool invalid_seq_num;
55549db4272SNagarjuna Kristam 	bool pls_quirk;
55649db4272SNagarjuna Kristam 	bool port_reset_quirk;
55788607a82SNagarjuna Kristam 	bool port_speed_quirk;
55849db4272SNagarjuna Kristam 	bool has_ipfs;
55949db4272SNagarjuna Kristam };
56049db4272SNagarjuna Kristam 
56149db4272SNagarjuna Kristam static inline u32 fpci_readl(struct tegra_xudc *xudc, unsigned int offset)
56249db4272SNagarjuna Kristam {
56349db4272SNagarjuna Kristam 	return readl(xudc->fpci + offset);
56449db4272SNagarjuna Kristam }
56549db4272SNagarjuna Kristam 
56649db4272SNagarjuna Kristam static inline void fpci_writel(struct tegra_xudc *xudc, u32 val,
56749db4272SNagarjuna Kristam 			       unsigned int offset)
56849db4272SNagarjuna Kristam {
56949db4272SNagarjuna Kristam 	writel(val, xudc->fpci + offset);
57049db4272SNagarjuna Kristam }
57149db4272SNagarjuna Kristam 
57249db4272SNagarjuna Kristam static inline u32 ipfs_readl(struct tegra_xudc *xudc, unsigned int offset)
57349db4272SNagarjuna Kristam {
57449db4272SNagarjuna Kristam 	return readl(xudc->ipfs + offset);
57549db4272SNagarjuna Kristam }
57649db4272SNagarjuna Kristam 
57749db4272SNagarjuna Kristam static inline void ipfs_writel(struct tegra_xudc *xudc, u32 val,
57849db4272SNagarjuna Kristam 			       unsigned int offset)
57949db4272SNagarjuna Kristam {
58049db4272SNagarjuna Kristam 	writel(val, xudc->ipfs + offset);
58149db4272SNagarjuna Kristam }
58249db4272SNagarjuna Kristam 
58349db4272SNagarjuna Kristam static inline u32 xudc_readl(struct tegra_xudc *xudc, unsigned int offset)
58449db4272SNagarjuna Kristam {
58549db4272SNagarjuna Kristam 	return readl(xudc->base + offset);
58649db4272SNagarjuna Kristam }
58749db4272SNagarjuna Kristam 
58849db4272SNagarjuna Kristam static inline void xudc_writel(struct tegra_xudc *xudc, u32 val,
58949db4272SNagarjuna Kristam 			       unsigned int offset)
59049db4272SNagarjuna Kristam {
59149db4272SNagarjuna Kristam 	writel(val, xudc->base + offset);
59249db4272SNagarjuna Kristam }
59349db4272SNagarjuna Kristam 
59449db4272SNagarjuna Kristam static inline int xudc_readl_poll(struct tegra_xudc *xudc,
59549db4272SNagarjuna Kristam 				  unsigned int offset, u32 mask, u32 val)
59649db4272SNagarjuna Kristam {
59749db4272SNagarjuna Kristam 	u32 regval;
59849db4272SNagarjuna Kristam 
59949db4272SNagarjuna Kristam 	return readl_poll_timeout_atomic(xudc->base + offset, regval,
60049db4272SNagarjuna Kristam 					 (regval & mask) == val, 1, 100);
60149db4272SNagarjuna Kristam }
60249db4272SNagarjuna Kristam 
60349db4272SNagarjuna Kristam static inline struct tegra_xudc *to_xudc(struct usb_gadget *gadget)
60449db4272SNagarjuna Kristam {
60549db4272SNagarjuna Kristam 	return container_of(gadget, struct tegra_xudc, gadget);
60649db4272SNagarjuna Kristam }
60749db4272SNagarjuna Kristam 
60849db4272SNagarjuna Kristam static inline struct tegra_xudc_ep *to_xudc_ep(struct usb_ep *ep)
60949db4272SNagarjuna Kristam {
61049db4272SNagarjuna Kristam 	return container_of(ep, struct tegra_xudc_ep, usb_ep);
61149db4272SNagarjuna Kristam }
61249db4272SNagarjuna Kristam 
61349db4272SNagarjuna Kristam static inline struct tegra_xudc_request *to_xudc_req(struct usb_request *req)
61449db4272SNagarjuna Kristam {
61549db4272SNagarjuna Kristam 	return container_of(req, struct tegra_xudc_request, usb_req);
61649db4272SNagarjuna Kristam }
61749db4272SNagarjuna Kristam 
61849db4272SNagarjuna Kristam static inline void dump_trb(struct tegra_xudc *xudc, const char *type,
61949db4272SNagarjuna Kristam 			    struct tegra_xudc_trb *trb)
62049db4272SNagarjuna Kristam {
62149db4272SNagarjuna Kristam 	dev_dbg(xudc->dev,
62249db4272SNagarjuna Kristam 		"%s: %p, lo = %#x, hi = %#x, status = %#x, control = %#x\n",
62349db4272SNagarjuna Kristam 		type, trb, trb->data_lo, trb->data_hi, trb->status,
62449db4272SNagarjuna Kristam 		trb->control);
62549db4272SNagarjuna Kristam }
62649db4272SNagarjuna Kristam 
62788607a82SNagarjuna Kristam static void tegra_xudc_limit_port_speed(struct tegra_xudc *xudc)
62888607a82SNagarjuna Kristam {
62988607a82SNagarjuna Kristam 	u32 val;
63088607a82SNagarjuna Kristam 
63188607a82SNagarjuna Kristam 	/* limit port speed to gen 1 */
63288607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT56);
63388607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK);
63488607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX(0x260);
63588607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT56);
63688607a82SNagarjuna Kristam 
63788607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT57);
63888607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK);
63988607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX(0x6D6);
64088607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT57);
64188607a82SNagarjuna Kristam 
64288607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT65);
64388607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK);
64488607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID(0x4B0);
64588607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT66);
64688607a82SNagarjuna Kristam 
64788607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT66);
64888607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK);
64988607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID(0x4B0);
65088607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT66);
65188607a82SNagarjuna Kristam 
65288607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT67);
65388607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK);
65488607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID(0x4B0);
65588607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT67);
65688607a82SNagarjuna Kristam 
65788607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT72);
65888607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK);
65988607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT(0x10);
66088607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT72);
66188607a82SNagarjuna Kristam }
66288607a82SNagarjuna Kristam 
66388607a82SNagarjuna Kristam static void tegra_xudc_restore_port_speed(struct tegra_xudc *xudc)
66488607a82SNagarjuna Kristam {
66588607a82SNagarjuna Kristam 	u32 val;
66688607a82SNagarjuna Kristam 
66788607a82SNagarjuna Kristam 	/* restore port speed to gen2 */
66888607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT56);
66988607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX_MASK);
67088607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT56_SCD_BIT0_TRPT_MAX(0x438);
67188607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT56);
67288607a82SNagarjuna Kristam 
67388607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT57);
67488607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX_MASK);
67588607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT57_SCD_BIT1_TRPT_MAX(0x528);
67688607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT57);
67788607a82SNagarjuna Kristam 
67888607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT65);
67988607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID_MASK);
68088607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT65_TX_SCD_END_TRPT_MID(0xE10);
68188607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT66);
68288607a82SNagarjuna Kristam 
68388607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT66);
68488607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID_MASK);
68588607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT66_TX_SCD_BIT0_TRPT_MID(0x348);
68688607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT66);
68788607a82SNagarjuna Kristam 
68888607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT67);
68988607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID_MASK);
69088607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT67_TX_SCD_BIT1_TRPT_MID(0x5a0);
69188607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT67);
69288607a82SNagarjuna Kristam 
69388607a82SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT72);
69488607a82SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT_MASK);
69588607a82SNagarjuna Kristam 	val |= SSPX_CORE_CNT72_SCD_LFPS_TIMEOUT(0x1c21);
69688607a82SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT72);
69788607a82SNagarjuna Kristam }
69888607a82SNagarjuna Kristam 
69949db4272SNagarjuna Kristam static void tegra_xudc_device_mode_on(struct tegra_xudc *xudc)
70049db4272SNagarjuna Kristam {
70149db4272SNagarjuna Kristam 	int err;
70249db4272SNagarjuna Kristam 
70349db4272SNagarjuna Kristam 	pm_runtime_get_sync(xudc->dev);
70449db4272SNagarjuna Kristam 
705*a88520bfSJim Lin 	tegra_phy_xusb_utmi_pad_power_on(xudc->curr_utmi_phy);
706*a88520bfSJim Lin 
707b4e19931SNagarjuna Kristam 	err = phy_power_on(xudc->curr_utmi_phy);
70849db4272SNagarjuna Kristam 	if (err < 0)
709de21e728SThierry Reding 		dev_err(xudc->dev, "UTMI power on failed: %d\n", err);
71049db4272SNagarjuna Kristam 
711b4e19931SNagarjuna Kristam 	err = phy_power_on(xudc->curr_usb3_phy);
71249db4272SNagarjuna Kristam 	if (err < 0)
713de21e728SThierry Reding 		dev_err(xudc->dev, "USB3 PHY power on failed: %d\n", err);
71449db4272SNagarjuna Kristam 
71549db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "device mode on\n");
71649db4272SNagarjuna Kristam 
717b4e19931SNagarjuna Kristam 	phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG,
718b4e19931SNagarjuna Kristam 			 USB_ROLE_DEVICE);
71949db4272SNagarjuna Kristam }
72049db4272SNagarjuna Kristam 
72149db4272SNagarjuna Kristam static void tegra_xudc_device_mode_off(struct tegra_xudc *xudc)
72249db4272SNagarjuna Kristam {
72349db4272SNagarjuna Kristam 	bool connected = false;
72449db4272SNagarjuna Kristam 	u32 pls, val;
72549db4272SNagarjuna Kristam 	int err;
72649db4272SNagarjuna Kristam 
72749db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "device mode off\n");
72849db4272SNagarjuna Kristam 
72949db4272SNagarjuna Kristam 	connected = !!(xudc_readl(xudc, PORTSC) & PORTSC_CCS);
73049db4272SNagarjuna Kristam 
73149db4272SNagarjuna Kristam 	reinit_completion(&xudc->disconnect_complete);
73249db4272SNagarjuna Kristam 
73388607a82SNagarjuna Kristam 	if (xudc->soc->port_speed_quirk)
73488607a82SNagarjuna Kristam 		tegra_xudc_restore_port_speed(xudc);
73588607a82SNagarjuna Kristam 
736b4e19931SNagarjuna Kristam 	phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG, USB_ROLE_NONE);
73749db4272SNagarjuna Kristam 
73849db4272SNagarjuna Kristam 	pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
73949db4272SNagarjuna Kristam 		PORTSC_PLS_SHIFT;
74049db4272SNagarjuna Kristam 
74149db4272SNagarjuna Kristam 	/* Direct link to U0 if disconnected in RESUME or U2. */
74249db4272SNagarjuna Kristam 	if (xudc->soc->pls_quirk && xudc->gadget.speed == USB_SPEED_SUPER &&
74349db4272SNagarjuna Kristam 	    (pls == PORTSC_PLS_RESUME || pls == PORTSC_PLS_U2)) {
74449db4272SNagarjuna Kristam 		val = xudc_readl(xudc, PORTPM);
74549db4272SNagarjuna Kristam 		val |= PORTPM_FRWE;
74649db4272SNagarjuna Kristam 		xudc_writel(xudc, val, PORTPM);
74749db4272SNagarjuna Kristam 
74849db4272SNagarjuna Kristam 		val = xudc_readl(xudc, PORTSC);
74949db4272SNagarjuna Kristam 		val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
75049db4272SNagarjuna Kristam 		val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_U0);
75149db4272SNagarjuna Kristam 		xudc_writel(xudc, val, PORTSC);
75249db4272SNagarjuna Kristam 	}
75349db4272SNagarjuna Kristam 
75449db4272SNagarjuna Kristam 	/* Wait for disconnect event. */
75549db4272SNagarjuna Kristam 	if (connected)
75649db4272SNagarjuna Kristam 		wait_for_completion(&xudc->disconnect_complete);
75749db4272SNagarjuna Kristam 
75849db4272SNagarjuna Kristam 	/* Make sure interrupt handler has completed before powergating. */
75949db4272SNagarjuna Kristam 	synchronize_irq(xudc->irq);
76049db4272SNagarjuna Kristam 
761*a88520bfSJim Lin 	tegra_phy_xusb_utmi_pad_power_down(xudc->curr_utmi_phy);
762*a88520bfSJim Lin 
763b4e19931SNagarjuna Kristam 	err = phy_power_off(xudc->curr_utmi_phy);
76449db4272SNagarjuna Kristam 	if (err < 0)
765de21e728SThierry Reding 		dev_err(xudc->dev, "UTMI PHY power off failed: %d\n", err);
76649db4272SNagarjuna Kristam 
767b4e19931SNagarjuna Kristam 	err = phy_power_off(xudc->curr_usb3_phy);
76849db4272SNagarjuna Kristam 	if (err < 0)
769de21e728SThierry Reding 		dev_err(xudc->dev, "USB3 PHY power off failed: %d\n", err);
77049db4272SNagarjuna Kristam 
77149db4272SNagarjuna Kristam 	pm_runtime_put(xudc->dev);
77249db4272SNagarjuna Kristam }
77349db4272SNagarjuna Kristam 
77449db4272SNagarjuna Kristam static void tegra_xudc_usb_role_sw_work(struct work_struct *work)
77549db4272SNagarjuna Kristam {
77649db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = container_of(work, struct tegra_xudc,
77749db4272SNagarjuna Kristam 					       usb_role_sw_work);
77849db4272SNagarjuna Kristam 
7799ce0a14bSNagarjuna Kristam 	if (xudc->device_mode)
78049db4272SNagarjuna Kristam 		tegra_xudc_device_mode_on(xudc);
78149db4272SNagarjuna Kristam 	else
78249db4272SNagarjuna Kristam 		tegra_xudc_device_mode_off(xudc);
78349db4272SNagarjuna Kristam }
78449db4272SNagarjuna Kristam 
785b4e19931SNagarjuna Kristam static int tegra_xudc_get_phy_index(struct tegra_xudc *xudc,
786b4e19931SNagarjuna Kristam 					      struct usb_phy *usbphy)
787b4e19931SNagarjuna Kristam {
788b4e19931SNagarjuna Kristam 	unsigned int i;
789b4e19931SNagarjuna Kristam 
790b4e19931SNagarjuna Kristam 	for (i = 0; i < xudc->soc->num_phys; i++) {
791b4e19931SNagarjuna Kristam 		if (xudc->usbphy[i] && usbphy == xudc->usbphy[i])
792b4e19931SNagarjuna Kristam 			return i;
793b4e19931SNagarjuna Kristam 	}
794b4e19931SNagarjuna Kristam 
795b4e19931SNagarjuna Kristam 	dev_info(xudc->dev, "phy index could not be found for shared USB PHY");
796b4e19931SNagarjuna Kristam 	return -1;
797b4e19931SNagarjuna Kristam }
798b4e19931SNagarjuna Kristam 
799b77f2ffeSNagarjuna Kristam static int tegra_xudc_vbus_notify(struct notifier_block *nb,
800b77f2ffeSNagarjuna Kristam 					 unsigned long action, void *data)
801b77f2ffeSNagarjuna Kristam {
802b77f2ffeSNagarjuna Kristam 	struct tegra_xudc *xudc = container_of(nb, struct tegra_xudc,
803b77f2ffeSNagarjuna Kristam 					       vbus_nb);
804b77f2ffeSNagarjuna Kristam 	struct usb_phy *usbphy = (struct usb_phy *)data;
805b4e19931SNagarjuna Kristam 	int phy_index;
806b77f2ffeSNagarjuna Kristam 
807b77f2ffeSNagarjuna Kristam 	dev_dbg(xudc->dev, "%s(): event is %d\n", __func__, usbphy->last_event);
808b77f2ffeSNagarjuna Kristam 
809b77f2ffeSNagarjuna Kristam 	if ((xudc->device_mode && usbphy->last_event == USB_EVENT_VBUS) ||
810b77f2ffeSNagarjuna Kristam 	    (!xudc->device_mode && usbphy->last_event != USB_EVENT_VBUS)) {
811b77f2ffeSNagarjuna Kristam 		dev_dbg(xudc->dev, "Same role(%d) received. Ignore",
812b77f2ffeSNagarjuna Kristam 			xudc->device_mode);
813b77f2ffeSNagarjuna Kristam 		return NOTIFY_OK;
814b77f2ffeSNagarjuna Kristam 	}
815b77f2ffeSNagarjuna Kristam 
816b77f2ffeSNagarjuna Kristam 	xudc->device_mode = (usbphy->last_event == USB_EVENT_VBUS) ? true :
817b77f2ffeSNagarjuna Kristam 								     false;
818b77f2ffeSNagarjuna Kristam 
819b4e19931SNagarjuna Kristam 	phy_index = tegra_xudc_get_phy_index(xudc, usbphy);
820b4e19931SNagarjuna Kristam 	dev_dbg(xudc->dev, "%s(): current phy index is %d\n", __func__,
821b4e19931SNagarjuna Kristam 		phy_index);
822b4e19931SNagarjuna Kristam 
823b4e19931SNagarjuna Kristam 	if (!xudc->suspended && phy_index != -1) {
824b4e19931SNagarjuna Kristam 		xudc->curr_utmi_phy = xudc->utmi_phy[phy_index];
825b4e19931SNagarjuna Kristam 		xudc->curr_usb3_phy = xudc->usb3_phy[phy_index];
826ac82b56bSNagarjuna Kristam 		xudc->curr_usbphy = usbphy;
827b77f2ffeSNagarjuna Kristam 		schedule_work(&xudc->usb_role_sw_work);
828b4e19931SNagarjuna Kristam 	}
829b77f2ffeSNagarjuna Kristam 
830b77f2ffeSNagarjuna Kristam 	return NOTIFY_OK;
831b77f2ffeSNagarjuna Kristam }
832b77f2ffeSNagarjuna Kristam 
83349db4272SNagarjuna Kristam static void tegra_xudc_plc_reset_work(struct work_struct *work)
83449db4272SNagarjuna Kristam {
83549db4272SNagarjuna Kristam 	struct delayed_work *dwork = to_delayed_work(work);
83649db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = container_of(dwork, struct tegra_xudc,
83749db4272SNagarjuna Kristam 					       plc_reset_work);
83849db4272SNagarjuna Kristam 	unsigned long flags;
83949db4272SNagarjuna Kristam 
84049db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
84149db4272SNagarjuna Kristam 
84249db4272SNagarjuna Kristam 	if (xudc->wait_csc) {
84349db4272SNagarjuna Kristam 		u32 pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
84449db4272SNagarjuna Kristam 			PORTSC_PLS_SHIFT;
84549db4272SNagarjuna Kristam 
84649db4272SNagarjuna Kristam 		if (pls == PORTSC_PLS_INACTIVE) {
84749db4272SNagarjuna Kristam 			dev_info(xudc->dev, "PLS = Inactive. Toggle VBUS\n");
848b4e19931SNagarjuna Kristam 			phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG,
849b9c9fd4aSNagarjuna Kristam 					 USB_ROLE_NONE);
850b4e19931SNagarjuna Kristam 			phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG,
851b9c9fd4aSNagarjuna Kristam 					 USB_ROLE_DEVICE);
852b9c9fd4aSNagarjuna Kristam 
85349db4272SNagarjuna Kristam 			xudc->wait_csc = false;
85449db4272SNagarjuna Kristam 		}
85549db4272SNagarjuna Kristam 	}
85649db4272SNagarjuna Kristam 
85749db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
85849db4272SNagarjuna Kristam }
85949db4272SNagarjuna Kristam 
86049db4272SNagarjuna Kristam static void tegra_xudc_port_reset_war_work(struct work_struct *work)
86149db4272SNagarjuna Kristam {
86249db4272SNagarjuna Kristam 	struct delayed_work *dwork = to_delayed_work(work);
86349db4272SNagarjuna Kristam 	struct tegra_xudc *xudc =
86449db4272SNagarjuna Kristam 		container_of(dwork, struct tegra_xudc, port_reset_war_work);
86549db4272SNagarjuna Kristam 	unsigned long flags;
86649db4272SNagarjuna Kristam 	u32 pls;
86749db4272SNagarjuna Kristam 	int ret;
86849db4272SNagarjuna Kristam 
86949db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
87049db4272SNagarjuna Kristam 
8719ce0a14bSNagarjuna Kristam 	if (xudc->device_mode && xudc->wait_for_sec_prc) {
87249db4272SNagarjuna Kristam 		pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
87349db4272SNagarjuna Kristam 			PORTSC_PLS_SHIFT;
87449db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "pls = %x\n", pls);
87549db4272SNagarjuna Kristam 
87649db4272SNagarjuna Kristam 		if (pls == PORTSC_PLS_DISABLED) {
87749db4272SNagarjuna Kristam 			dev_dbg(xudc->dev, "toggle vbus\n");
87849db4272SNagarjuna Kristam 			/* PRC doesn't complete in 100ms, toggle the vbus */
879b4e19931SNagarjuna Kristam 			ret = tegra_phy_xusb_utmi_port_reset(
880b4e19931SNagarjuna Kristam 				xudc->curr_utmi_phy);
88149db4272SNagarjuna Kristam 			if (ret == 1)
88249db4272SNagarjuna Kristam 				xudc->wait_for_sec_prc = 0;
88349db4272SNagarjuna Kristam 		}
88449db4272SNagarjuna Kristam 	}
88549db4272SNagarjuna Kristam 
88649db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
88749db4272SNagarjuna Kristam }
88849db4272SNagarjuna Kristam 
88949db4272SNagarjuna Kristam static dma_addr_t trb_virt_to_phys(struct tegra_xudc_ep *ep,
89049db4272SNagarjuna Kristam 				   struct tegra_xudc_trb *trb)
89149db4272SNagarjuna Kristam {
89249db4272SNagarjuna Kristam 	unsigned int index;
89349db4272SNagarjuna Kristam 
89449db4272SNagarjuna Kristam 	index = trb - ep->transfer_ring;
89549db4272SNagarjuna Kristam 
89649db4272SNagarjuna Kristam 	if (WARN_ON(index >= XUDC_TRANSFER_RING_SIZE))
89749db4272SNagarjuna Kristam 		return 0;
89849db4272SNagarjuna Kristam 
89949db4272SNagarjuna Kristam 	return (ep->transfer_ring_phys + index * sizeof(*trb));
90049db4272SNagarjuna Kristam }
90149db4272SNagarjuna Kristam 
90249db4272SNagarjuna Kristam static struct tegra_xudc_trb *trb_phys_to_virt(struct tegra_xudc_ep *ep,
90349db4272SNagarjuna Kristam 					       dma_addr_t addr)
90449db4272SNagarjuna Kristam {
90549db4272SNagarjuna Kristam 	struct tegra_xudc_trb *trb;
90649db4272SNagarjuna Kristam 	unsigned int index;
90749db4272SNagarjuna Kristam 
90849db4272SNagarjuna Kristam 	index = (addr - ep->transfer_ring_phys) / sizeof(*trb);
90949db4272SNagarjuna Kristam 
91049db4272SNagarjuna Kristam 	if (WARN_ON(index >= XUDC_TRANSFER_RING_SIZE))
91149db4272SNagarjuna Kristam 		return NULL;
91249db4272SNagarjuna Kristam 
91349db4272SNagarjuna Kristam 	trb = &ep->transfer_ring[index];
91449db4272SNagarjuna Kristam 
91549db4272SNagarjuna Kristam 	return trb;
91649db4272SNagarjuna Kristam }
91749db4272SNagarjuna Kristam 
91849db4272SNagarjuna Kristam static void ep_reload(struct tegra_xudc *xudc, unsigned int ep)
91949db4272SNagarjuna Kristam {
92049db4272SNagarjuna Kristam 	xudc_writel(xudc, BIT(ep), EP_RELOAD);
92149db4272SNagarjuna Kristam 	xudc_readl_poll(xudc, EP_RELOAD, BIT(ep), 0);
92249db4272SNagarjuna Kristam }
92349db4272SNagarjuna Kristam 
92449db4272SNagarjuna Kristam static void ep_pause(struct tegra_xudc *xudc, unsigned int ep)
92549db4272SNagarjuna Kristam {
92649db4272SNagarjuna Kristam 	u32 val;
92749db4272SNagarjuna Kristam 
92849db4272SNagarjuna Kristam 	val = xudc_readl(xudc, EP_PAUSE);
92949db4272SNagarjuna Kristam 	if (val & BIT(ep))
93049db4272SNagarjuna Kristam 		return;
93149db4272SNagarjuna Kristam 	val |= BIT(ep);
93249db4272SNagarjuna Kristam 
93349db4272SNagarjuna Kristam 	xudc_writel(xudc, val, EP_PAUSE);
93449db4272SNagarjuna Kristam 
93549db4272SNagarjuna Kristam 	xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
93649db4272SNagarjuna Kristam 
93749db4272SNagarjuna Kristam 	xudc_writel(xudc, BIT(ep), EP_STCHG);
93849db4272SNagarjuna Kristam }
93949db4272SNagarjuna Kristam 
94049db4272SNagarjuna Kristam static void ep_unpause(struct tegra_xudc *xudc, unsigned int ep)
94149db4272SNagarjuna Kristam {
94249db4272SNagarjuna Kristam 	u32 val;
94349db4272SNagarjuna Kristam 
94449db4272SNagarjuna Kristam 	val = xudc_readl(xudc, EP_PAUSE);
94549db4272SNagarjuna Kristam 	if (!(val & BIT(ep)))
94649db4272SNagarjuna Kristam 		return;
94749db4272SNagarjuna Kristam 	val &= ~BIT(ep);
94849db4272SNagarjuna Kristam 
94949db4272SNagarjuna Kristam 	xudc_writel(xudc, val, EP_PAUSE);
95049db4272SNagarjuna Kristam 
95149db4272SNagarjuna Kristam 	xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
95249db4272SNagarjuna Kristam 
95349db4272SNagarjuna Kristam 	xudc_writel(xudc, BIT(ep), EP_STCHG);
95449db4272SNagarjuna Kristam }
95549db4272SNagarjuna Kristam 
95649db4272SNagarjuna Kristam static void ep_unpause_all(struct tegra_xudc *xudc)
95749db4272SNagarjuna Kristam {
95849db4272SNagarjuna Kristam 	u32 val;
95949db4272SNagarjuna Kristam 
96049db4272SNagarjuna Kristam 	val = xudc_readl(xudc, EP_PAUSE);
96149db4272SNagarjuna Kristam 
96249db4272SNagarjuna Kristam 	xudc_writel(xudc, 0, EP_PAUSE);
96349db4272SNagarjuna Kristam 
96449db4272SNagarjuna Kristam 	xudc_readl_poll(xudc, EP_STCHG, val, val);
96549db4272SNagarjuna Kristam 
96649db4272SNagarjuna Kristam 	xudc_writel(xudc, val, EP_STCHG);
96749db4272SNagarjuna Kristam }
96849db4272SNagarjuna Kristam 
96949db4272SNagarjuna Kristam static void ep_halt(struct tegra_xudc *xudc, unsigned int ep)
97049db4272SNagarjuna Kristam {
97149db4272SNagarjuna Kristam 	u32 val;
97249db4272SNagarjuna Kristam 
97349db4272SNagarjuna Kristam 	val = xudc_readl(xudc, EP_HALT);
97449db4272SNagarjuna Kristam 	if (val & BIT(ep))
97549db4272SNagarjuna Kristam 		return;
97649db4272SNagarjuna Kristam 	val |= BIT(ep);
97749db4272SNagarjuna Kristam 	xudc_writel(xudc, val, EP_HALT);
97849db4272SNagarjuna Kristam 
97949db4272SNagarjuna Kristam 	xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
98049db4272SNagarjuna Kristam 
98149db4272SNagarjuna Kristam 	xudc_writel(xudc, BIT(ep), EP_STCHG);
98249db4272SNagarjuna Kristam }
98349db4272SNagarjuna Kristam 
98449db4272SNagarjuna Kristam static void ep_unhalt(struct tegra_xudc *xudc, unsigned int ep)
98549db4272SNagarjuna Kristam {
98649db4272SNagarjuna Kristam 	u32 val;
98749db4272SNagarjuna Kristam 
98849db4272SNagarjuna Kristam 	val = xudc_readl(xudc, EP_HALT);
98949db4272SNagarjuna Kristam 	if (!(val & BIT(ep)))
99049db4272SNagarjuna Kristam 		return;
99149db4272SNagarjuna Kristam 	val &= ~BIT(ep);
99249db4272SNagarjuna Kristam 	xudc_writel(xudc, val, EP_HALT);
99349db4272SNagarjuna Kristam 
99449db4272SNagarjuna Kristam 	xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
99549db4272SNagarjuna Kristam 
99649db4272SNagarjuna Kristam 	xudc_writel(xudc, BIT(ep), EP_STCHG);
99749db4272SNagarjuna Kristam }
99849db4272SNagarjuna Kristam 
99949db4272SNagarjuna Kristam static void ep_unhalt_all(struct tegra_xudc *xudc)
100049db4272SNagarjuna Kristam {
100149db4272SNagarjuna Kristam 	u32 val;
100249db4272SNagarjuna Kristam 
100349db4272SNagarjuna Kristam 	val = xudc_readl(xudc, EP_HALT);
100449db4272SNagarjuna Kristam 	if (!val)
100549db4272SNagarjuna Kristam 		return;
100649db4272SNagarjuna Kristam 	xudc_writel(xudc, 0, EP_HALT);
100749db4272SNagarjuna Kristam 
100849db4272SNagarjuna Kristam 	xudc_readl_poll(xudc, EP_STCHG, val, val);
100949db4272SNagarjuna Kristam 
101049db4272SNagarjuna Kristam 	xudc_writel(xudc, val, EP_STCHG);
101149db4272SNagarjuna Kristam }
101249db4272SNagarjuna Kristam 
101349db4272SNagarjuna Kristam static void ep_wait_for_stopped(struct tegra_xudc *xudc, unsigned int ep)
101449db4272SNagarjuna Kristam {
101549db4272SNagarjuna Kristam 	xudc_readl_poll(xudc, EP_STOPPED, BIT(ep), BIT(ep));
101649db4272SNagarjuna Kristam 	xudc_writel(xudc, BIT(ep), EP_STOPPED);
101749db4272SNagarjuna Kristam }
101849db4272SNagarjuna Kristam 
101949db4272SNagarjuna Kristam static void ep_wait_for_inactive(struct tegra_xudc *xudc, unsigned int ep)
102049db4272SNagarjuna Kristam {
102149db4272SNagarjuna Kristam 	xudc_readl_poll(xudc, EP_THREAD_ACTIVE, BIT(ep), 0);
102249db4272SNagarjuna Kristam }
102349db4272SNagarjuna Kristam 
102449db4272SNagarjuna Kristam static void tegra_xudc_req_done(struct tegra_xudc_ep *ep,
102549db4272SNagarjuna Kristam 				struct tegra_xudc_request *req, int status)
102649db4272SNagarjuna Kristam {
102749db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = ep->xudc;
102849db4272SNagarjuna Kristam 
102949db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "completing request %p on EP %u with status %d\n",
103049db4272SNagarjuna Kristam 		 req, ep->index, status);
103149db4272SNagarjuna Kristam 
103249db4272SNagarjuna Kristam 	if (likely(req->usb_req.status == -EINPROGRESS))
103349db4272SNagarjuna Kristam 		req->usb_req.status = status;
103449db4272SNagarjuna Kristam 
103549db4272SNagarjuna Kristam 	list_del_init(&req->list);
103649db4272SNagarjuna Kristam 
103749db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_control(ep->desc)) {
103849db4272SNagarjuna Kristam 		usb_gadget_unmap_request(&xudc->gadget, &req->usb_req,
103949db4272SNagarjuna Kristam 					 (xudc->setup_state ==
104049db4272SNagarjuna Kristam 					  DATA_STAGE_XFER));
104149db4272SNagarjuna Kristam 	} else {
104249db4272SNagarjuna Kristam 		usb_gadget_unmap_request(&xudc->gadget, &req->usb_req,
104349db4272SNagarjuna Kristam 					 usb_endpoint_dir_in(ep->desc));
104449db4272SNagarjuna Kristam 	}
104549db4272SNagarjuna Kristam 
104649db4272SNagarjuna Kristam 	spin_unlock(&xudc->lock);
104749db4272SNagarjuna Kristam 	usb_gadget_giveback_request(&ep->usb_ep, &req->usb_req);
104849db4272SNagarjuna Kristam 	spin_lock(&xudc->lock);
104949db4272SNagarjuna Kristam }
105049db4272SNagarjuna Kristam 
105149db4272SNagarjuna Kristam static void tegra_xudc_ep_nuke(struct tegra_xudc_ep *ep, int status)
105249db4272SNagarjuna Kristam {
105349db4272SNagarjuna Kristam 	struct tegra_xudc_request *req;
105449db4272SNagarjuna Kristam 
105549db4272SNagarjuna Kristam 	while (!list_empty(&ep->queue)) {
105649db4272SNagarjuna Kristam 		req = list_first_entry(&ep->queue, struct tegra_xudc_request,
105749db4272SNagarjuna Kristam 				       list);
105849db4272SNagarjuna Kristam 		tegra_xudc_req_done(ep, req, status);
105949db4272SNagarjuna Kristam 	}
106049db4272SNagarjuna Kristam }
106149db4272SNagarjuna Kristam 
106249db4272SNagarjuna Kristam static unsigned int ep_available_trbs(struct tegra_xudc_ep *ep)
106349db4272SNagarjuna Kristam {
106449db4272SNagarjuna Kristam 	if (ep->ring_full)
106549db4272SNagarjuna Kristam 		return 0;
106649db4272SNagarjuna Kristam 
106749db4272SNagarjuna Kristam 	if (ep->deq_ptr > ep->enq_ptr)
106849db4272SNagarjuna Kristam 		return ep->deq_ptr - ep->enq_ptr - 1;
106949db4272SNagarjuna Kristam 
107049db4272SNagarjuna Kristam 	return XUDC_TRANSFER_RING_SIZE - (ep->enq_ptr - ep->deq_ptr) - 2;
107149db4272SNagarjuna Kristam }
107249db4272SNagarjuna Kristam 
107349db4272SNagarjuna Kristam static void tegra_xudc_queue_one_trb(struct tegra_xudc_ep *ep,
107449db4272SNagarjuna Kristam 				     struct tegra_xudc_request *req,
107549db4272SNagarjuna Kristam 				     struct tegra_xudc_trb *trb,
107649db4272SNagarjuna Kristam 				     bool ioc)
107749db4272SNagarjuna Kristam {
107849db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = ep->xudc;
107949db4272SNagarjuna Kristam 	dma_addr_t buf_addr;
108049db4272SNagarjuna Kristam 	size_t len;
108149db4272SNagarjuna Kristam 
108249db4272SNagarjuna Kristam 	len = min_t(size_t, XUDC_TRB_MAX_BUFFER_SIZE, req->usb_req.length -
108349db4272SNagarjuna Kristam 		    req->buf_queued);
108449db4272SNagarjuna Kristam 	if (len > 0)
108549db4272SNagarjuna Kristam 		buf_addr = req->usb_req.dma + req->buf_queued;
108649db4272SNagarjuna Kristam 	else
108749db4272SNagarjuna Kristam 		buf_addr = 0;
108849db4272SNagarjuna Kristam 
108949db4272SNagarjuna Kristam 	trb_write_data_ptr(trb, buf_addr);
109049db4272SNagarjuna Kristam 
109149db4272SNagarjuna Kristam 	trb_write_transfer_len(trb, len);
109249db4272SNagarjuna Kristam 	trb_write_td_size(trb, req->trbs_needed - req->trbs_queued - 1);
109349db4272SNagarjuna Kristam 
109449db4272SNagarjuna Kristam 	if (req->trbs_queued == req->trbs_needed - 1 ||
109549db4272SNagarjuna Kristam 		(req->need_zlp && req->trbs_queued == req->trbs_needed - 2))
109649db4272SNagarjuna Kristam 		trb_write_chain(trb, 0);
109749db4272SNagarjuna Kristam 	else
109849db4272SNagarjuna Kristam 		trb_write_chain(trb, 1);
109949db4272SNagarjuna Kristam 
110049db4272SNagarjuna Kristam 	trb_write_ioc(trb, ioc);
110149db4272SNagarjuna Kristam 
110249db4272SNagarjuna Kristam 	if (usb_endpoint_dir_out(ep->desc) ||
110349db4272SNagarjuna Kristam 	    (usb_endpoint_xfer_control(ep->desc) &&
110449db4272SNagarjuna Kristam 	     (xudc->setup_state == DATA_STAGE_RECV)))
110549db4272SNagarjuna Kristam 		trb_write_isp(trb, 1);
110649db4272SNagarjuna Kristam 	else
110749db4272SNagarjuna Kristam 		trb_write_isp(trb, 0);
110849db4272SNagarjuna Kristam 
110949db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_control(ep->desc)) {
111049db4272SNagarjuna Kristam 		if (xudc->setup_state == DATA_STAGE_XFER ||
111149db4272SNagarjuna Kristam 		    xudc->setup_state == DATA_STAGE_RECV)
111249db4272SNagarjuna Kristam 			trb_write_type(trb, TRB_TYPE_DATA_STAGE);
111349db4272SNagarjuna Kristam 		else
111449db4272SNagarjuna Kristam 			trb_write_type(trb, TRB_TYPE_STATUS_STAGE);
111549db4272SNagarjuna Kristam 
111649db4272SNagarjuna Kristam 		if (xudc->setup_state == DATA_STAGE_XFER ||
111749db4272SNagarjuna Kristam 		    xudc->setup_state == STATUS_STAGE_XFER)
111849db4272SNagarjuna Kristam 			trb_write_data_stage_dir(trb, 1);
111949db4272SNagarjuna Kristam 		else
112049db4272SNagarjuna Kristam 			trb_write_data_stage_dir(trb, 0);
112149db4272SNagarjuna Kristam 	} else if (usb_endpoint_xfer_isoc(ep->desc)) {
112249db4272SNagarjuna Kristam 		trb_write_type(trb, TRB_TYPE_ISOCH);
112349db4272SNagarjuna Kristam 		trb_write_sia(trb, 1);
112449db4272SNagarjuna Kristam 		trb_write_frame_id(trb, 0);
112549db4272SNagarjuna Kristam 		trb_write_tlbpc(trb, 0);
112649db4272SNagarjuna Kristam 	} else if (usb_ss_max_streams(ep->comp_desc)) {
112749db4272SNagarjuna Kristam 		trb_write_type(trb, TRB_TYPE_STREAM);
112849db4272SNagarjuna Kristam 		trb_write_stream_id(trb, req->usb_req.stream_id);
112949db4272SNagarjuna Kristam 	} else {
113049db4272SNagarjuna Kristam 		trb_write_type(trb, TRB_TYPE_NORMAL);
113149db4272SNagarjuna Kristam 		trb_write_stream_id(trb, 0);
113249db4272SNagarjuna Kristam 	}
113349db4272SNagarjuna Kristam 
113449db4272SNagarjuna Kristam 	trb_write_cycle(trb, ep->pcs);
113549db4272SNagarjuna Kristam 
113649db4272SNagarjuna Kristam 	req->trbs_queued++;
113749db4272SNagarjuna Kristam 	req->buf_queued += len;
113849db4272SNagarjuna Kristam 
113949db4272SNagarjuna Kristam 	dump_trb(xudc, "TRANSFER", trb);
114049db4272SNagarjuna Kristam }
114149db4272SNagarjuna Kristam 
114249db4272SNagarjuna Kristam static unsigned int tegra_xudc_queue_trbs(struct tegra_xudc_ep *ep,
114349db4272SNagarjuna Kristam 					  struct tegra_xudc_request *req)
114449db4272SNagarjuna Kristam {
114549db4272SNagarjuna Kristam 	unsigned int i, count, available;
114649db4272SNagarjuna Kristam 	bool wait_td = false;
114749db4272SNagarjuna Kristam 
114849db4272SNagarjuna Kristam 	available = ep_available_trbs(ep);
114949db4272SNagarjuna Kristam 	count = req->trbs_needed - req->trbs_queued;
115049db4272SNagarjuna Kristam 	if (available < count) {
115149db4272SNagarjuna Kristam 		count = available;
115249db4272SNagarjuna Kristam 		ep->ring_full = true;
115349db4272SNagarjuna Kristam 	}
115449db4272SNagarjuna Kristam 
115549db4272SNagarjuna Kristam 	/*
115649db4272SNagarjuna Kristam 	 * To generate zero-length packet on USB bus, SW needs schedule a
115749db4272SNagarjuna Kristam 	 * standalone zero-length TD. According to HW's behavior, SW needs
115849db4272SNagarjuna Kristam 	 * to schedule TDs in different ways for different endpoint types.
115949db4272SNagarjuna Kristam 	 *
116049db4272SNagarjuna Kristam 	 * For control endpoint:
116149db4272SNagarjuna Kristam 	 * - Data stage TD (IOC = 1, CH = 0)
116249db4272SNagarjuna Kristam 	 * - Ring doorbell and wait transfer event
116349db4272SNagarjuna Kristam 	 * - Data stage TD for ZLP (IOC = 1, CH = 0)
116449db4272SNagarjuna Kristam 	 * - Ring doorbell
116549db4272SNagarjuna Kristam 	 *
116649db4272SNagarjuna Kristam 	 * For bulk and interrupt endpoints:
116749db4272SNagarjuna Kristam 	 * - Normal transfer TD (IOC = 0, CH = 0)
116849db4272SNagarjuna Kristam 	 * - Normal transfer TD for ZLP (IOC = 1, CH = 0)
116949db4272SNagarjuna Kristam 	 * - Ring doorbell
117049db4272SNagarjuna Kristam 	 */
117149db4272SNagarjuna Kristam 
117249db4272SNagarjuna Kristam 	if (req->need_zlp && usb_endpoint_xfer_control(ep->desc) && count > 1)
117349db4272SNagarjuna Kristam 		wait_td = true;
117449db4272SNagarjuna Kristam 
117549db4272SNagarjuna Kristam 	if (!req->first_trb)
117649db4272SNagarjuna Kristam 		req->first_trb = &ep->transfer_ring[ep->enq_ptr];
117749db4272SNagarjuna Kristam 
117849db4272SNagarjuna Kristam 	for (i = 0; i < count; i++) {
117949db4272SNagarjuna Kristam 		struct tegra_xudc_trb *trb = &ep->transfer_ring[ep->enq_ptr];
118049db4272SNagarjuna Kristam 		bool ioc = false;
118149db4272SNagarjuna Kristam 
118249db4272SNagarjuna Kristam 		if ((i == count - 1) || (wait_td && i == count - 2))
118349db4272SNagarjuna Kristam 			ioc = true;
118449db4272SNagarjuna Kristam 
118549db4272SNagarjuna Kristam 		tegra_xudc_queue_one_trb(ep, req, trb, ioc);
118649db4272SNagarjuna Kristam 		req->last_trb = trb;
118749db4272SNagarjuna Kristam 
118849db4272SNagarjuna Kristam 		ep->enq_ptr++;
118949db4272SNagarjuna Kristam 		if (ep->enq_ptr == XUDC_TRANSFER_RING_SIZE - 1) {
119049db4272SNagarjuna Kristam 			trb = &ep->transfer_ring[ep->enq_ptr];
119149db4272SNagarjuna Kristam 			trb_write_cycle(trb, ep->pcs);
119249db4272SNagarjuna Kristam 			ep->pcs = !ep->pcs;
119349db4272SNagarjuna Kristam 			ep->enq_ptr = 0;
119449db4272SNagarjuna Kristam 		}
119549db4272SNagarjuna Kristam 
119649db4272SNagarjuna Kristam 		if (ioc)
119749db4272SNagarjuna Kristam 			break;
119849db4272SNagarjuna Kristam 	}
119949db4272SNagarjuna Kristam 
120049db4272SNagarjuna Kristam 	return count;
120149db4272SNagarjuna Kristam }
120249db4272SNagarjuna Kristam 
120349db4272SNagarjuna Kristam static void tegra_xudc_ep_ring_doorbell(struct tegra_xudc_ep *ep)
120449db4272SNagarjuna Kristam {
120549db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = ep->xudc;
120649db4272SNagarjuna Kristam 	u32 val;
120749db4272SNagarjuna Kristam 
120849db4272SNagarjuna Kristam 	if (list_empty(&ep->queue))
120949db4272SNagarjuna Kristam 		return;
121049db4272SNagarjuna Kristam 
121149db4272SNagarjuna Kristam 	val = DB_TARGET(ep->index);
121249db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_control(ep->desc)) {
121349db4272SNagarjuna Kristam 		val |= DB_STREAMID(xudc->setup_seq_num);
121449db4272SNagarjuna Kristam 	} else if (usb_ss_max_streams(ep->comp_desc) > 0) {
121549db4272SNagarjuna Kristam 		struct tegra_xudc_request *req;
121649db4272SNagarjuna Kristam 
121749db4272SNagarjuna Kristam 		/* Don't ring doorbell if the stream has been rejected. */
121849db4272SNagarjuna Kristam 		if (ep->stream_rejected)
121949db4272SNagarjuna Kristam 			return;
122049db4272SNagarjuna Kristam 
122149db4272SNagarjuna Kristam 		req = list_first_entry(&ep->queue, struct tegra_xudc_request,
122249db4272SNagarjuna Kristam 				       list);
122349db4272SNagarjuna Kristam 		val |= DB_STREAMID(req->usb_req.stream_id);
122449db4272SNagarjuna Kristam 	}
122549db4272SNagarjuna Kristam 
122649db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "ring doorbell: %#x\n", val);
122749db4272SNagarjuna Kristam 	xudc_writel(xudc, val, DB);
122849db4272SNagarjuna Kristam }
122949db4272SNagarjuna Kristam 
123049db4272SNagarjuna Kristam static void tegra_xudc_ep_kick_queue(struct tegra_xudc_ep *ep)
123149db4272SNagarjuna Kristam {
123249db4272SNagarjuna Kristam 	struct tegra_xudc_request *req;
123349db4272SNagarjuna Kristam 	bool trbs_queued = false;
123449db4272SNagarjuna Kristam 
123549db4272SNagarjuna Kristam 	list_for_each_entry(req, &ep->queue, list) {
123649db4272SNagarjuna Kristam 		if (ep->ring_full)
123749db4272SNagarjuna Kristam 			break;
123849db4272SNagarjuna Kristam 
123949db4272SNagarjuna Kristam 		if (tegra_xudc_queue_trbs(ep, req) > 0)
124049db4272SNagarjuna Kristam 			trbs_queued = true;
124149db4272SNagarjuna Kristam 	}
124249db4272SNagarjuna Kristam 
124349db4272SNagarjuna Kristam 	if (trbs_queued)
124449db4272SNagarjuna Kristam 		tegra_xudc_ep_ring_doorbell(ep);
124549db4272SNagarjuna Kristam }
124649db4272SNagarjuna Kristam 
124749db4272SNagarjuna Kristam static int
124849db4272SNagarjuna Kristam __tegra_xudc_ep_queue(struct tegra_xudc_ep *ep, struct tegra_xudc_request *req)
124949db4272SNagarjuna Kristam {
125049db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = ep->xudc;
125149db4272SNagarjuna Kristam 	int err;
125249db4272SNagarjuna Kristam 
125349db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_control(ep->desc) && !list_empty(&ep->queue)) {
125449db4272SNagarjuna Kristam 		dev_err(xudc->dev, "control EP has pending transfers\n");
125549db4272SNagarjuna Kristam 		return -EINVAL;
125649db4272SNagarjuna Kristam 	}
125749db4272SNagarjuna Kristam 
125849db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_control(ep->desc)) {
125949db4272SNagarjuna Kristam 		err = usb_gadget_map_request(&xudc->gadget, &req->usb_req,
126049db4272SNagarjuna Kristam 					     (xudc->setup_state ==
126149db4272SNagarjuna Kristam 					      DATA_STAGE_XFER));
126249db4272SNagarjuna Kristam 	} else {
126349db4272SNagarjuna Kristam 		err = usb_gadget_map_request(&xudc->gadget, &req->usb_req,
126449db4272SNagarjuna Kristam 					     usb_endpoint_dir_in(ep->desc));
126549db4272SNagarjuna Kristam 	}
126649db4272SNagarjuna Kristam 
126749db4272SNagarjuna Kristam 	if (err < 0) {
126849db4272SNagarjuna Kristam 		dev_err(xudc->dev, "failed to map request: %d\n", err);
126949db4272SNagarjuna Kristam 		return err;
127049db4272SNagarjuna Kristam 	}
127149db4272SNagarjuna Kristam 
127249db4272SNagarjuna Kristam 	req->first_trb = NULL;
127349db4272SNagarjuna Kristam 	req->last_trb = NULL;
127449db4272SNagarjuna Kristam 	req->buf_queued = 0;
127549db4272SNagarjuna Kristam 	req->trbs_queued = 0;
127649db4272SNagarjuna Kristam 	req->need_zlp = false;
127749db4272SNagarjuna Kristam 	req->trbs_needed = DIV_ROUND_UP(req->usb_req.length,
127849db4272SNagarjuna Kristam 					XUDC_TRB_MAX_BUFFER_SIZE);
127949db4272SNagarjuna Kristam 	if (req->usb_req.length == 0)
128049db4272SNagarjuna Kristam 		req->trbs_needed++;
128149db4272SNagarjuna Kristam 
128249db4272SNagarjuna Kristam 	if (!usb_endpoint_xfer_isoc(ep->desc) &&
128349db4272SNagarjuna Kristam 	    req->usb_req.zero && req->usb_req.length &&
128449db4272SNagarjuna Kristam 	    ((req->usb_req.length % ep->usb_ep.maxpacket) == 0)) {
128549db4272SNagarjuna Kristam 		req->trbs_needed++;
128649db4272SNagarjuna Kristam 		req->need_zlp = true;
128749db4272SNagarjuna Kristam 	}
128849db4272SNagarjuna Kristam 
128949db4272SNagarjuna Kristam 	req->usb_req.status = -EINPROGRESS;
129049db4272SNagarjuna Kristam 	req->usb_req.actual = 0;
129149db4272SNagarjuna Kristam 
129249db4272SNagarjuna Kristam 	list_add_tail(&req->list, &ep->queue);
129349db4272SNagarjuna Kristam 
129449db4272SNagarjuna Kristam 	tegra_xudc_ep_kick_queue(ep);
129549db4272SNagarjuna Kristam 
129649db4272SNagarjuna Kristam 	return 0;
129749db4272SNagarjuna Kristam }
129849db4272SNagarjuna Kristam 
129949db4272SNagarjuna Kristam static int
130049db4272SNagarjuna Kristam tegra_xudc_ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
130149db4272SNagarjuna Kristam 		    gfp_t gfp)
130249db4272SNagarjuna Kristam {
130349db4272SNagarjuna Kristam 	struct tegra_xudc_request *req;
130449db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep;
130549db4272SNagarjuna Kristam 	struct tegra_xudc *xudc;
130649db4272SNagarjuna Kristam 	unsigned long flags;
130749db4272SNagarjuna Kristam 	int ret;
130849db4272SNagarjuna Kristam 
130949db4272SNagarjuna Kristam 	if (!usb_ep || !usb_req)
131049db4272SNagarjuna Kristam 		return -EINVAL;
131149db4272SNagarjuna Kristam 
131249db4272SNagarjuna Kristam 	ep = to_xudc_ep(usb_ep);
131349db4272SNagarjuna Kristam 	req = to_xudc_req(usb_req);
131449db4272SNagarjuna Kristam 	xudc = ep->xudc;
131549db4272SNagarjuna Kristam 
131649db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
131749db4272SNagarjuna Kristam 	if (xudc->powergated || !ep->desc) {
131849db4272SNagarjuna Kristam 		ret = -ESHUTDOWN;
131949db4272SNagarjuna Kristam 		goto unlock;
132049db4272SNagarjuna Kristam 	}
132149db4272SNagarjuna Kristam 
132249db4272SNagarjuna Kristam 	ret = __tegra_xudc_ep_queue(ep, req);
132349db4272SNagarjuna Kristam unlock:
132449db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
132549db4272SNagarjuna Kristam 
132649db4272SNagarjuna Kristam 	return ret;
132749db4272SNagarjuna Kristam }
132849db4272SNagarjuna Kristam 
132949db4272SNagarjuna Kristam static void squeeze_transfer_ring(struct tegra_xudc_ep *ep,
133049db4272SNagarjuna Kristam 				  struct tegra_xudc_request *req)
133149db4272SNagarjuna Kristam {
133249db4272SNagarjuna Kristam 	struct tegra_xudc_trb *trb = req->first_trb;
133349db4272SNagarjuna Kristam 	bool pcs_enq = trb_read_cycle(trb);
133449db4272SNagarjuna Kristam 	bool pcs;
133549db4272SNagarjuna Kristam 
133649db4272SNagarjuna Kristam 	/*
133749db4272SNagarjuna Kristam 	 * Clear out all the TRBs part of or after the cancelled request,
133849db4272SNagarjuna Kristam 	 * and must correct trb cycle bit to the last un-enqueued state.
133949db4272SNagarjuna Kristam 	 */
134049db4272SNagarjuna Kristam 	while (trb != &ep->transfer_ring[ep->enq_ptr]) {
134149db4272SNagarjuna Kristam 		pcs = trb_read_cycle(trb);
134249db4272SNagarjuna Kristam 		memset(trb, 0, sizeof(*trb));
134349db4272SNagarjuna Kristam 		trb_write_cycle(trb, !pcs);
134449db4272SNagarjuna Kristam 		trb++;
134549db4272SNagarjuna Kristam 
134649db4272SNagarjuna Kristam 		if (trb_read_type(trb) == TRB_TYPE_LINK)
134749db4272SNagarjuna Kristam 			trb = ep->transfer_ring;
134849db4272SNagarjuna Kristam 	}
134949db4272SNagarjuna Kristam 
135049db4272SNagarjuna Kristam 	/* Requests will be re-queued at the start of the cancelled request. */
135149db4272SNagarjuna Kristam 	ep->enq_ptr = req->first_trb - ep->transfer_ring;
135249db4272SNagarjuna Kristam 	/*
135349db4272SNagarjuna Kristam 	 * Retrieve the correct cycle bit state from the first trb of
135449db4272SNagarjuna Kristam 	 * the cancelled request.
135549db4272SNagarjuna Kristam 	 */
135649db4272SNagarjuna Kristam 	ep->pcs = pcs_enq;
135749db4272SNagarjuna Kristam 	ep->ring_full = false;
135849db4272SNagarjuna Kristam 	list_for_each_entry_continue(req, &ep->queue, list) {
135949db4272SNagarjuna Kristam 		req->usb_req.status = -EINPROGRESS;
136049db4272SNagarjuna Kristam 		req->usb_req.actual = 0;
136149db4272SNagarjuna Kristam 
136249db4272SNagarjuna Kristam 		req->first_trb = NULL;
136349db4272SNagarjuna Kristam 		req->last_trb = NULL;
136449db4272SNagarjuna Kristam 		req->buf_queued = 0;
136549db4272SNagarjuna Kristam 		req->trbs_queued = 0;
136649db4272SNagarjuna Kristam 	}
136749db4272SNagarjuna Kristam }
136849db4272SNagarjuna Kristam 
136949db4272SNagarjuna Kristam /*
137049db4272SNagarjuna Kristam  * Determine if the given TRB is in the range [first trb, last trb] for the
137149db4272SNagarjuna Kristam  * given request.
137249db4272SNagarjuna Kristam  */
137349db4272SNagarjuna Kristam static bool trb_in_request(struct tegra_xudc_ep *ep,
137449db4272SNagarjuna Kristam 			   struct tegra_xudc_request *req,
137549db4272SNagarjuna Kristam 			   struct tegra_xudc_trb *trb)
137649db4272SNagarjuna Kristam {
137749db4272SNagarjuna Kristam 	dev_dbg(ep->xudc->dev, "%s: request %p -> %p; trb %p\n", __func__,
137849db4272SNagarjuna Kristam 		req->first_trb, req->last_trb, trb);
137949db4272SNagarjuna Kristam 
138049db4272SNagarjuna Kristam 	if (trb >= req->first_trb && (trb <= req->last_trb ||
138149db4272SNagarjuna Kristam 				      req->last_trb < req->first_trb))
138249db4272SNagarjuna Kristam 		return true;
138349db4272SNagarjuna Kristam 
138449db4272SNagarjuna Kristam 	if (trb < req->first_trb && trb <= req->last_trb &&
138549db4272SNagarjuna Kristam 	    req->last_trb < req->first_trb)
138649db4272SNagarjuna Kristam 		return true;
138749db4272SNagarjuna Kristam 
138849db4272SNagarjuna Kristam 	return false;
138949db4272SNagarjuna Kristam }
139049db4272SNagarjuna Kristam 
139149db4272SNagarjuna Kristam /*
139249db4272SNagarjuna Kristam  * Determine if the given TRB is in the range [EP enqueue pointer, first TRB)
139349db4272SNagarjuna Kristam  * for the given endpoint and request.
139449db4272SNagarjuna Kristam  */
139549db4272SNagarjuna Kristam static bool trb_before_request(struct tegra_xudc_ep *ep,
139649db4272SNagarjuna Kristam 			       struct tegra_xudc_request *req,
139749db4272SNagarjuna Kristam 			       struct tegra_xudc_trb *trb)
139849db4272SNagarjuna Kristam {
139949db4272SNagarjuna Kristam 	struct tegra_xudc_trb *enq_trb = &ep->transfer_ring[ep->enq_ptr];
140049db4272SNagarjuna Kristam 
140149db4272SNagarjuna Kristam 	dev_dbg(ep->xudc->dev, "%s: request %p -> %p; enq ptr: %p; trb %p\n",
140249db4272SNagarjuna Kristam 		__func__, req->first_trb, req->last_trb, enq_trb, trb);
140349db4272SNagarjuna Kristam 
140449db4272SNagarjuna Kristam 	if (trb < req->first_trb && (enq_trb <= trb ||
140549db4272SNagarjuna Kristam 				     req->first_trb < enq_trb))
140649db4272SNagarjuna Kristam 		return true;
140749db4272SNagarjuna Kristam 
140849db4272SNagarjuna Kristam 	if (trb > req->first_trb && req->first_trb < enq_trb && enq_trb <= trb)
140949db4272SNagarjuna Kristam 		return true;
141049db4272SNagarjuna Kristam 
141149db4272SNagarjuna Kristam 	return false;
141249db4272SNagarjuna Kristam }
141349db4272SNagarjuna Kristam 
141449db4272SNagarjuna Kristam static int
141549db4272SNagarjuna Kristam __tegra_xudc_ep_dequeue(struct tegra_xudc_ep *ep,
141649db4272SNagarjuna Kristam 			struct tegra_xudc_request *req)
141749db4272SNagarjuna Kristam {
141849db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = ep->xudc;
141941894774SJakob Koschel 	struct tegra_xudc_request *r = NULL, *iter;
142049db4272SNagarjuna Kristam 	struct tegra_xudc_trb *deq_trb;
142149db4272SNagarjuna Kristam 	bool busy, kick_queue = false;
142249db4272SNagarjuna Kristam 	int ret = 0;
142349db4272SNagarjuna Kristam 
142449db4272SNagarjuna Kristam 	/* Make sure the request is actually queued to this endpoint. */
142541894774SJakob Koschel 	list_for_each_entry(iter, &ep->queue, list) {
142641894774SJakob Koschel 		if (iter != req)
142741894774SJakob Koschel 			continue;
142841894774SJakob Koschel 		r = iter;
142949db4272SNagarjuna Kristam 		break;
143049db4272SNagarjuna Kristam 	}
143149db4272SNagarjuna Kristam 
143241894774SJakob Koschel 	if (!r)
143349db4272SNagarjuna Kristam 		return -EINVAL;
143449db4272SNagarjuna Kristam 
143549db4272SNagarjuna Kristam 	/* Request hasn't been queued in the transfer ring yet. */
143649db4272SNagarjuna Kristam 	if (!req->trbs_queued) {
143749db4272SNagarjuna Kristam 		tegra_xudc_req_done(ep, req, -ECONNRESET);
143849db4272SNagarjuna Kristam 		return 0;
143949db4272SNagarjuna Kristam 	}
144049db4272SNagarjuna Kristam 
144153b0c69fSTom Rix 	/* Halt DMA for this endpoint. */
144249db4272SNagarjuna Kristam 	if (ep_ctx_read_state(ep->context) == EP_STATE_RUNNING) {
144349db4272SNagarjuna Kristam 		ep_pause(xudc, ep->index);
144449db4272SNagarjuna Kristam 		ep_wait_for_inactive(xudc, ep->index);
144549db4272SNagarjuna Kristam 	}
144649db4272SNagarjuna Kristam 
144749db4272SNagarjuna Kristam 	deq_trb = trb_phys_to_virt(ep, ep_ctx_read_deq_ptr(ep->context));
144849db4272SNagarjuna Kristam 	/* Is the hardware processing the TRB at the dequeue pointer? */
144949db4272SNagarjuna Kristam 	busy = (trb_read_cycle(deq_trb) == ep_ctx_read_dcs(ep->context));
145049db4272SNagarjuna Kristam 
145149db4272SNagarjuna Kristam 	if (trb_in_request(ep, req, deq_trb) && busy) {
145249db4272SNagarjuna Kristam 		/*
145349db4272SNagarjuna Kristam 		 * Request has been partially completed or it hasn't
145449db4272SNagarjuna Kristam 		 * started processing yet.
145549db4272SNagarjuna Kristam 		 */
145649db4272SNagarjuna Kristam 		dma_addr_t deq_ptr;
145749db4272SNagarjuna Kristam 
145849db4272SNagarjuna Kristam 		squeeze_transfer_ring(ep, req);
145949db4272SNagarjuna Kristam 
146049db4272SNagarjuna Kristam 		req->usb_req.actual = ep_ctx_read_edtla(ep->context);
146149db4272SNagarjuna Kristam 		tegra_xudc_req_done(ep, req, -ECONNRESET);
146249db4272SNagarjuna Kristam 		kick_queue = true;
146349db4272SNagarjuna Kristam 
146449db4272SNagarjuna Kristam 		/* EDTLA is > 0: request has been partially completed */
146549db4272SNagarjuna Kristam 		if (req->usb_req.actual > 0) {
146649db4272SNagarjuna Kristam 			/*
146749db4272SNagarjuna Kristam 			 * Abort the pending transfer and update the dequeue
146849db4272SNagarjuna Kristam 			 * pointer
146949db4272SNagarjuna Kristam 			 */
147049db4272SNagarjuna Kristam 			ep_ctx_write_edtla(ep->context, 0);
147149db4272SNagarjuna Kristam 			ep_ctx_write_partial_td(ep->context, 0);
147249db4272SNagarjuna Kristam 			ep_ctx_write_data_offset(ep->context, 0);
147349db4272SNagarjuna Kristam 
147449db4272SNagarjuna Kristam 			deq_ptr = trb_virt_to_phys(ep,
147549db4272SNagarjuna Kristam 					&ep->transfer_ring[ep->enq_ptr]);
147649db4272SNagarjuna Kristam 
147749db4272SNagarjuna Kristam 			if (dma_mapping_error(xudc->dev, deq_ptr)) {
147849db4272SNagarjuna Kristam 				ret = -EINVAL;
147949db4272SNagarjuna Kristam 			} else {
148049db4272SNagarjuna Kristam 				ep_ctx_write_deq_ptr(ep->context, deq_ptr);
148149db4272SNagarjuna Kristam 				ep_ctx_write_dcs(ep->context, ep->pcs);
148249db4272SNagarjuna Kristam 				ep_reload(xudc, ep->index);
148349db4272SNagarjuna Kristam 			}
148449db4272SNagarjuna Kristam 		}
148549db4272SNagarjuna Kristam 	} else if (trb_before_request(ep, req, deq_trb) && busy) {
148649db4272SNagarjuna Kristam 		/* Request hasn't started processing yet. */
148749db4272SNagarjuna Kristam 		squeeze_transfer_ring(ep, req);
148849db4272SNagarjuna Kristam 
148949db4272SNagarjuna Kristam 		tegra_xudc_req_done(ep, req, -ECONNRESET);
149049db4272SNagarjuna Kristam 		kick_queue = true;
149149db4272SNagarjuna Kristam 	} else {
149249db4272SNagarjuna Kristam 		/*
149349db4272SNagarjuna Kristam 		 * Request has completed, but we haven't processed the
149449db4272SNagarjuna Kristam 		 * completion event yet.
149549db4272SNagarjuna Kristam 		 */
149649db4272SNagarjuna Kristam 		tegra_xudc_req_done(ep, req, -ECONNRESET);
149749db4272SNagarjuna Kristam 		ret = -EINVAL;
149849db4272SNagarjuna Kristam 	}
149949db4272SNagarjuna Kristam 
150049db4272SNagarjuna Kristam 	/* Resume the endpoint. */
150149db4272SNagarjuna Kristam 	ep_unpause(xudc, ep->index);
150249db4272SNagarjuna Kristam 
150349db4272SNagarjuna Kristam 	if (kick_queue)
150449db4272SNagarjuna Kristam 		tegra_xudc_ep_kick_queue(ep);
150549db4272SNagarjuna Kristam 
150649db4272SNagarjuna Kristam 	return ret;
150749db4272SNagarjuna Kristam }
150849db4272SNagarjuna Kristam 
150949db4272SNagarjuna Kristam static int
151049db4272SNagarjuna Kristam tegra_xudc_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
151149db4272SNagarjuna Kristam {
151249db4272SNagarjuna Kristam 	struct tegra_xudc_request *req;
151349db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep;
151449db4272SNagarjuna Kristam 	struct tegra_xudc *xudc;
151549db4272SNagarjuna Kristam 	unsigned long flags;
151649db4272SNagarjuna Kristam 	int ret;
151749db4272SNagarjuna Kristam 
151849db4272SNagarjuna Kristam 	if (!usb_ep || !usb_req)
151949db4272SNagarjuna Kristam 		return -EINVAL;
152049db4272SNagarjuna Kristam 
152149db4272SNagarjuna Kristam 	ep = to_xudc_ep(usb_ep);
152249db4272SNagarjuna Kristam 	req = to_xudc_req(usb_req);
152349db4272SNagarjuna Kristam 	xudc = ep->xudc;
152449db4272SNagarjuna Kristam 
152549db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
152649db4272SNagarjuna Kristam 
152749db4272SNagarjuna Kristam 	if (xudc->powergated || !ep->desc) {
152849db4272SNagarjuna Kristam 		ret = -ESHUTDOWN;
152949db4272SNagarjuna Kristam 		goto unlock;
153049db4272SNagarjuna Kristam 	}
153149db4272SNagarjuna Kristam 
153249db4272SNagarjuna Kristam 	ret = __tegra_xudc_ep_dequeue(ep, req);
153349db4272SNagarjuna Kristam unlock:
153449db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
153549db4272SNagarjuna Kristam 
153649db4272SNagarjuna Kristam 	return ret;
153749db4272SNagarjuna Kristam }
153849db4272SNagarjuna Kristam 
153949db4272SNagarjuna Kristam static int __tegra_xudc_ep_set_halt(struct tegra_xudc_ep *ep, bool halt)
154049db4272SNagarjuna Kristam {
154149db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = ep->xudc;
154249db4272SNagarjuna Kristam 
154349db4272SNagarjuna Kristam 	if (!ep->desc)
154449db4272SNagarjuna Kristam 		return -EINVAL;
154549db4272SNagarjuna Kristam 
154649db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_isoc(ep->desc)) {
1547de21e728SThierry Reding 		dev_err(xudc->dev, "can't halt isochronous EP\n");
154849db4272SNagarjuna Kristam 		return -ENOTSUPP;
154949db4272SNagarjuna Kristam 	}
155049db4272SNagarjuna Kristam 
155149db4272SNagarjuna Kristam 	if (!!(xudc_readl(xudc, EP_HALT) & BIT(ep->index)) == halt) {
155249db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "EP %u already %s\n", ep->index,
155349db4272SNagarjuna Kristam 			halt ? "halted" : "not halted");
155449db4272SNagarjuna Kristam 		return 0;
155549db4272SNagarjuna Kristam 	}
155649db4272SNagarjuna Kristam 
155749db4272SNagarjuna Kristam 	if (halt) {
155849db4272SNagarjuna Kristam 		ep_halt(xudc, ep->index);
155949db4272SNagarjuna Kristam 	} else {
156049db4272SNagarjuna Kristam 		ep_ctx_write_state(ep->context, EP_STATE_DISABLED);
156149db4272SNagarjuna Kristam 
156249db4272SNagarjuna Kristam 		ep_reload(xudc, ep->index);
156349db4272SNagarjuna Kristam 
156449db4272SNagarjuna Kristam 		ep_ctx_write_state(ep->context, EP_STATE_RUNNING);
15657bd42fb9SWayne Chang 		ep_ctx_write_rsvd(ep->context, 0);
15667bd42fb9SWayne Chang 		ep_ctx_write_partial_td(ep->context, 0);
15677bd42fb9SWayne Chang 		ep_ctx_write_splitxstate(ep->context, 0);
156849db4272SNagarjuna Kristam 		ep_ctx_write_seq_num(ep->context, 0);
156949db4272SNagarjuna Kristam 
157049db4272SNagarjuna Kristam 		ep_reload(xudc, ep->index);
157149db4272SNagarjuna Kristam 		ep_unpause(xudc, ep->index);
157249db4272SNagarjuna Kristam 		ep_unhalt(xudc, ep->index);
157349db4272SNagarjuna Kristam 
157449db4272SNagarjuna Kristam 		tegra_xudc_ep_ring_doorbell(ep);
157549db4272SNagarjuna Kristam 	}
157649db4272SNagarjuna Kristam 
157749db4272SNagarjuna Kristam 	return 0;
157849db4272SNagarjuna Kristam }
157949db4272SNagarjuna Kristam 
158049db4272SNagarjuna Kristam static int tegra_xudc_ep_set_halt(struct usb_ep *usb_ep, int value)
158149db4272SNagarjuna Kristam {
158249db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep;
158349db4272SNagarjuna Kristam 	struct tegra_xudc *xudc;
158449db4272SNagarjuna Kristam 	unsigned long flags;
158549db4272SNagarjuna Kristam 	int ret;
158649db4272SNagarjuna Kristam 
158749db4272SNagarjuna Kristam 	if (!usb_ep)
158849db4272SNagarjuna Kristam 		return -EINVAL;
158949db4272SNagarjuna Kristam 
159049db4272SNagarjuna Kristam 	ep = to_xudc_ep(usb_ep);
159149db4272SNagarjuna Kristam 	xudc = ep->xudc;
159249db4272SNagarjuna Kristam 
159349db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
159449db4272SNagarjuna Kristam 	if (xudc->powergated) {
159549db4272SNagarjuna Kristam 		ret = -ESHUTDOWN;
159649db4272SNagarjuna Kristam 		goto unlock;
159749db4272SNagarjuna Kristam 	}
159849db4272SNagarjuna Kristam 
159949db4272SNagarjuna Kristam 	if (value && usb_endpoint_dir_in(ep->desc) &&
160049db4272SNagarjuna Kristam 	    !list_empty(&ep->queue)) {
160149db4272SNagarjuna Kristam 		dev_err(xudc->dev, "can't halt EP with requests pending\n");
160249db4272SNagarjuna Kristam 		ret = -EAGAIN;
160349db4272SNagarjuna Kristam 		goto unlock;
160449db4272SNagarjuna Kristam 	}
160549db4272SNagarjuna Kristam 
160649db4272SNagarjuna Kristam 	ret = __tegra_xudc_ep_set_halt(ep, value);
160749db4272SNagarjuna Kristam unlock:
160849db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
160949db4272SNagarjuna Kristam 
161049db4272SNagarjuna Kristam 	return ret;
161149db4272SNagarjuna Kristam }
161249db4272SNagarjuna Kristam 
161349db4272SNagarjuna Kristam static void tegra_xudc_ep_context_setup(struct tegra_xudc_ep *ep)
161449db4272SNagarjuna Kristam {
161549db4272SNagarjuna Kristam 	const struct usb_endpoint_descriptor *desc = ep->desc;
161649db4272SNagarjuna Kristam 	const struct usb_ss_ep_comp_descriptor *comp_desc = ep->comp_desc;
161749db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = ep->xudc;
161849db4272SNagarjuna Kristam 	u16 maxpacket, maxburst = 0, esit = 0;
161949db4272SNagarjuna Kristam 	u32 val;
162049db4272SNagarjuna Kristam 
1621eeb0cfb6SChunfeng Yun 	maxpacket = usb_endpoint_maxp(desc);
162249db4272SNagarjuna Kristam 	if (xudc->gadget.speed == USB_SPEED_SUPER) {
162349db4272SNagarjuna Kristam 		if (!usb_endpoint_xfer_control(desc))
162449db4272SNagarjuna Kristam 			maxburst = comp_desc->bMaxBurst;
162549db4272SNagarjuna Kristam 
162649db4272SNagarjuna Kristam 		if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc))
162749db4272SNagarjuna Kristam 			esit = le16_to_cpu(comp_desc->wBytesPerInterval);
162849db4272SNagarjuna Kristam 	} else if ((xudc->gadget.speed < USB_SPEED_SUPER) &&
162949db4272SNagarjuna Kristam 		   (usb_endpoint_xfer_int(desc) ||
163049db4272SNagarjuna Kristam 		    usb_endpoint_xfer_isoc(desc))) {
163149db4272SNagarjuna Kristam 		if (xudc->gadget.speed == USB_SPEED_HIGH) {
1632eeb0cfb6SChunfeng Yun 			maxburst = usb_endpoint_maxp_mult(desc) - 1;
163349db4272SNagarjuna Kristam 			if (maxburst == 0x3) {
163449db4272SNagarjuna Kristam 				dev_warn(xudc->dev,
163549db4272SNagarjuna Kristam 					 "invalid endpoint maxburst\n");
163649db4272SNagarjuna Kristam 				maxburst = 0x2;
163749db4272SNagarjuna Kristam 			}
163849db4272SNagarjuna Kristam 		}
163949db4272SNagarjuna Kristam 		esit = maxpacket * (maxburst + 1);
164049db4272SNagarjuna Kristam 	}
164149db4272SNagarjuna Kristam 
164249db4272SNagarjuna Kristam 	memset(ep->context, 0, sizeof(*ep->context));
164349db4272SNagarjuna Kristam 
164449db4272SNagarjuna Kristam 	ep_ctx_write_state(ep->context, EP_STATE_RUNNING);
164549db4272SNagarjuna Kristam 	ep_ctx_write_interval(ep->context, desc->bInterval);
164649db4272SNagarjuna Kristam 	if (xudc->gadget.speed == USB_SPEED_SUPER) {
164749db4272SNagarjuna Kristam 		if (usb_endpoint_xfer_isoc(desc)) {
164849db4272SNagarjuna Kristam 			ep_ctx_write_mult(ep->context,
164949db4272SNagarjuna Kristam 					  comp_desc->bmAttributes & 0x3);
165049db4272SNagarjuna Kristam 		}
165149db4272SNagarjuna Kristam 
165249db4272SNagarjuna Kristam 		if (usb_endpoint_xfer_bulk(desc)) {
165349db4272SNagarjuna Kristam 			ep_ctx_write_max_pstreams(ep->context,
165449db4272SNagarjuna Kristam 						  comp_desc->bmAttributes &
165549db4272SNagarjuna Kristam 						  0x1f);
165649db4272SNagarjuna Kristam 			ep_ctx_write_lsa(ep->context, 1);
165749db4272SNagarjuna Kristam 		}
165849db4272SNagarjuna Kristam 	}
165949db4272SNagarjuna Kristam 
166049db4272SNagarjuna Kristam 	if (!usb_endpoint_xfer_control(desc) && usb_endpoint_dir_out(desc))
166149db4272SNagarjuna Kristam 		val = usb_endpoint_type(desc);
166249db4272SNagarjuna Kristam 	else
166349db4272SNagarjuna Kristam 		val = usb_endpoint_type(desc) + EP_TYPE_CONTROL;
166449db4272SNagarjuna Kristam 
166549db4272SNagarjuna Kristam 	ep_ctx_write_type(ep->context, val);
166649db4272SNagarjuna Kristam 	ep_ctx_write_cerr(ep->context, 0x3);
166749db4272SNagarjuna Kristam 	ep_ctx_write_max_packet_size(ep->context, maxpacket);
166849db4272SNagarjuna Kristam 	ep_ctx_write_max_burst_size(ep->context, maxburst);
166949db4272SNagarjuna Kristam 
167049db4272SNagarjuna Kristam 	ep_ctx_write_deq_ptr(ep->context, ep->transfer_ring_phys);
167149db4272SNagarjuna Kristam 	ep_ctx_write_dcs(ep->context, ep->pcs);
167249db4272SNagarjuna Kristam 
167349db4272SNagarjuna Kristam 	/* Select a reasonable average TRB length based on endpoint type. */
167449db4272SNagarjuna Kristam 	switch (usb_endpoint_type(desc)) {
167549db4272SNagarjuna Kristam 	case USB_ENDPOINT_XFER_CONTROL:
167649db4272SNagarjuna Kristam 		val = 8;
167749db4272SNagarjuna Kristam 		break;
167849db4272SNagarjuna Kristam 	case USB_ENDPOINT_XFER_INT:
167949db4272SNagarjuna Kristam 		val = 1024;
168049db4272SNagarjuna Kristam 		break;
168149db4272SNagarjuna Kristam 	case USB_ENDPOINT_XFER_BULK:
168249db4272SNagarjuna Kristam 	case USB_ENDPOINT_XFER_ISOC:
168349db4272SNagarjuna Kristam 	default:
168449db4272SNagarjuna Kristam 		val = 3072;
168549db4272SNagarjuna Kristam 		break;
168649db4272SNagarjuna Kristam 	}
168749db4272SNagarjuna Kristam 
168849db4272SNagarjuna Kristam 	ep_ctx_write_avg_trb_len(ep->context, val);
168949db4272SNagarjuna Kristam 	ep_ctx_write_max_esit_payload(ep->context, esit);
169049db4272SNagarjuna Kristam 
169149db4272SNagarjuna Kristam 	ep_ctx_write_cerrcnt(ep->context, 0x3);
169249db4272SNagarjuna Kristam }
169349db4272SNagarjuna Kristam 
169449db4272SNagarjuna Kristam static void setup_link_trb(struct tegra_xudc_ep *ep,
169549db4272SNagarjuna Kristam 			   struct tegra_xudc_trb *trb)
169649db4272SNagarjuna Kristam {
169749db4272SNagarjuna Kristam 	trb_write_data_ptr(trb, ep->transfer_ring_phys);
169849db4272SNagarjuna Kristam 	trb_write_type(trb, TRB_TYPE_LINK);
169949db4272SNagarjuna Kristam 	trb_write_toggle_cycle(trb, 1);
170049db4272SNagarjuna Kristam }
170149db4272SNagarjuna Kristam 
170249db4272SNagarjuna Kristam static int __tegra_xudc_ep_disable(struct tegra_xudc_ep *ep)
170349db4272SNagarjuna Kristam {
170449db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = ep->xudc;
170549db4272SNagarjuna Kristam 
170649db4272SNagarjuna Kristam 	if (ep_ctx_read_state(ep->context) == EP_STATE_DISABLED) {
170749db4272SNagarjuna Kristam 		dev_err(xudc->dev, "endpoint %u already disabled\n",
170849db4272SNagarjuna Kristam 			ep->index);
170949db4272SNagarjuna Kristam 		return -EINVAL;
171049db4272SNagarjuna Kristam 	}
171149db4272SNagarjuna Kristam 
171249db4272SNagarjuna Kristam 	ep_ctx_write_state(ep->context, EP_STATE_DISABLED);
171349db4272SNagarjuna Kristam 
171449db4272SNagarjuna Kristam 	ep_reload(xudc, ep->index);
171549db4272SNagarjuna Kristam 
171649db4272SNagarjuna Kristam 	tegra_xudc_ep_nuke(ep, -ESHUTDOWN);
171749db4272SNagarjuna Kristam 
171849db4272SNagarjuna Kristam 	xudc->nr_enabled_eps--;
171949db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_isoc(ep->desc))
172049db4272SNagarjuna Kristam 		xudc->nr_isoch_eps--;
172149db4272SNagarjuna Kristam 
172249db4272SNagarjuna Kristam 	ep->desc = NULL;
172349db4272SNagarjuna Kristam 	ep->comp_desc = NULL;
172449db4272SNagarjuna Kristam 
172549db4272SNagarjuna Kristam 	memset(ep->context, 0, sizeof(*ep->context));
172649db4272SNagarjuna Kristam 
172749db4272SNagarjuna Kristam 	ep_unpause(xudc, ep->index);
172849db4272SNagarjuna Kristam 	ep_unhalt(xudc, ep->index);
172949db4272SNagarjuna Kristam 	if (xudc_readl(xudc, EP_STOPPED) & BIT(ep->index))
173049db4272SNagarjuna Kristam 		xudc_writel(xudc, BIT(ep->index), EP_STOPPED);
173149db4272SNagarjuna Kristam 
173249db4272SNagarjuna Kristam 	/*
173349db4272SNagarjuna Kristam 	 * If this is the last endpoint disabled in a de-configure request,
173449db4272SNagarjuna Kristam 	 * switch back to address state.
173549db4272SNagarjuna Kristam 	 */
173649db4272SNagarjuna Kristam 	if ((xudc->device_state == USB_STATE_CONFIGURED) &&
173749db4272SNagarjuna Kristam 	    (xudc->nr_enabled_eps == 1)) {
173849db4272SNagarjuna Kristam 		u32 val;
173949db4272SNagarjuna Kristam 
174049db4272SNagarjuna Kristam 		xudc->device_state = USB_STATE_ADDRESS;
174149db4272SNagarjuna Kristam 		usb_gadget_set_state(&xudc->gadget, xudc->device_state);
174249db4272SNagarjuna Kristam 
174349db4272SNagarjuna Kristam 		val = xudc_readl(xudc, CTRL);
174449db4272SNagarjuna Kristam 		val &= ~CTRL_RUN;
174549db4272SNagarjuna Kristam 		xudc_writel(xudc, val, CTRL);
174649db4272SNagarjuna Kristam 	}
174749db4272SNagarjuna Kristam 
174849db4272SNagarjuna Kristam 	dev_info(xudc->dev, "ep %u disabled\n", ep->index);
174949db4272SNagarjuna Kristam 
175049db4272SNagarjuna Kristam 	return 0;
175149db4272SNagarjuna Kristam }
175249db4272SNagarjuna Kristam 
175349db4272SNagarjuna Kristam static int tegra_xudc_ep_disable(struct usb_ep *usb_ep)
175449db4272SNagarjuna Kristam {
175549db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep;
175649db4272SNagarjuna Kristam 	struct tegra_xudc *xudc;
175749db4272SNagarjuna Kristam 	unsigned long flags;
175849db4272SNagarjuna Kristam 	int ret;
175949db4272SNagarjuna Kristam 
176049db4272SNagarjuna Kristam 	if (!usb_ep)
176149db4272SNagarjuna Kristam 		return -EINVAL;
176249db4272SNagarjuna Kristam 
176349db4272SNagarjuna Kristam 	ep = to_xudc_ep(usb_ep);
176449db4272SNagarjuna Kristam 	xudc = ep->xudc;
176549db4272SNagarjuna Kristam 
176649db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
176749db4272SNagarjuna Kristam 	if (xudc->powergated) {
176849db4272SNagarjuna Kristam 		ret = -ESHUTDOWN;
176949db4272SNagarjuna Kristam 		goto unlock;
177049db4272SNagarjuna Kristam 	}
177149db4272SNagarjuna Kristam 
177249db4272SNagarjuna Kristam 	ret = __tegra_xudc_ep_disable(ep);
177349db4272SNagarjuna Kristam unlock:
177449db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
177549db4272SNagarjuna Kristam 
177649db4272SNagarjuna Kristam 	return ret;
177749db4272SNagarjuna Kristam }
177849db4272SNagarjuna Kristam 
177949db4272SNagarjuna Kristam static int __tegra_xudc_ep_enable(struct tegra_xudc_ep *ep,
178049db4272SNagarjuna Kristam 				  const struct usb_endpoint_descriptor *desc)
178149db4272SNagarjuna Kristam {
178249db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = ep->xudc;
178349db4272SNagarjuna Kristam 	unsigned int i;
178449db4272SNagarjuna Kristam 	u32 val;
178549db4272SNagarjuna Kristam 
178649db4272SNagarjuna Kristam 	if (xudc->gadget.speed == USB_SPEED_SUPER &&
178749db4272SNagarjuna Kristam 		!usb_endpoint_xfer_control(desc) && !ep->usb_ep.comp_desc)
178849db4272SNagarjuna Kristam 		return -EINVAL;
178949db4272SNagarjuna Kristam 
179049db4272SNagarjuna Kristam 	/* Disable the EP if it is not disabled */
179149db4272SNagarjuna Kristam 	if (ep_ctx_read_state(ep->context) != EP_STATE_DISABLED)
179249db4272SNagarjuna Kristam 		__tegra_xudc_ep_disable(ep);
179349db4272SNagarjuna Kristam 
179449db4272SNagarjuna Kristam 	ep->desc = desc;
179549db4272SNagarjuna Kristam 	ep->comp_desc = ep->usb_ep.comp_desc;
179649db4272SNagarjuna Kristam 
179749db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_isoc(desc)) {
179849db4272SNagarjuna Kristam 		if (xudc->nr_isoch_eps > XUDC_MAX_ISOCH_EPS) {
1799de21e728SThierry Reding 			dev_err(xudc->dev, "too many isochronous endpoints\n");
180049db4272SNagarjuna Kristam 			return -EBUSY;
180149db4272SNagarjuna Kristam 		}
180249db4272SNagarjuna Kristam 		xudc->nr_isoch_eps++;
180349db4272SNagarjuna Kristam 	}
180449db4272SNagarjuna Kristam 
180549db4272SNagarjuna Kristam 	memset(ep->transfer_ring, 0, XUDC_TRANSFER_RING_SIZE *
180649db4272SNagarjuna Kristam 	       sizeof(*ep->transfer_ring));
180749db4272SNagarjuna Kristam 	setup_link_trb(ep, &ep->transfer_ring[XUDC_TRANSFER_RING_SIZE - 1]);
180849db4272SNagarjuna Kristam 
180949db4272SNagarjuna Kristam 	ep->enq_ptr = 0;
181049db4272SNagarjuna Kristam 	ep->deq_ptr = 0;
181149db4272SNagarjuna Kristam 	ep->pcs = true;
181249db4272SNagarjuna Kristam 	ep->ring_full = false;
181349db4272SNagarjuna Kristam 	xudc->nr_enabled_eps++;
181449db4272SNagarjuna Kristam 
181549db4272SNagarjuna Kristam 	tegra_xudc_ep_context_setup(ep);
181649db4272SNagarjuna Kristam 
181749db4272SNagarjuna Kristam 	/*
181849db4272SNagarjuna Kristam 	 * No need to reload and un-halt EP0.  This will be done automatically
181949db4272SNagarjuna Kristam 	 * once a valid SETUP packet is received.
182049db4272SNagarjuna Kristam 	 */
182149db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_control(desc))
182249db4272SNagarjuna Kristam 		goto out;
182349db4272SNagarjuna Kristam 
182449db4272SNagarjuna Kristam 	/*
182549db4272SNagarjuna Kristam 	 * Transition to configured state once the first non-control
182649db4272SNagarjuna Kristam 	 * endpoint is enabled.
182749db4272SNagarjuna Kristam 	 */
182849db4272SNagarjuna Kristam 	if (xudc->device_state == USB_STATE_ADDRESS) {
182949db4272SNagarjuna Kristam 		val = xudc_readl(xudc, CTRL);
183049db4272SNagarjuna Kristam 		val |= CTRL_RUN;
183149db4272SNagarjuna Kristam 		xudc_writel(xudc, val, CTRL);
183249db4272SNagarjuna Kristam 
183349db4272SNagarjuna Kristam 		xudc->device_state = USB_STATE_CONFIGURED;
183449db4272SNagarjuna Kristam 		usb_gadget_set_state(&xudc->gadget, xudc->device_state);
183549db4272SNagarjuna Kristam 	}
183649db4272SNagarjuna Kristam 
183749db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_isoc(desc)) {
183849db4272SNagarjuna Kristam 		/*
183949db4272SNagarjuna Kristam 		 * Pause all bulk endpoints when enabling an isoch endpoint
184049db4272SNagarjuna Kristam 		 * to ensure the isoch endpoint is allocated enough bandwidth.
184149db4272SNagarjuna Kristam 		 */
184249db4272SNagarjuna Kristam 		for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
184349db4272SNagarjuna Kristam 			if (xudc->ep[i].desc &&
184449db4272SNagarjuna Kristam 			    usb_endpoint_xfer_bulk(xudc->ep[i].desc))
184549db4272SNagarjuna Kristam 				ep_pause(xudc, i);
184649db4272SNagarjuna Kristam 		}
184749db4272SNagarjuna Kristam 	}
184849db4272SNagarjuna Kristam 
184949db4272SNagarjuna Kristam 	ep_reload(xudc, ep->index);
185049db4272SNagarjuna Kristam 	ep_unpause(xudc, ep->index);
185149db4272SNagarjuna Kristam 	ep_unhalt(xudc, ep->index);
185249db4272SNagarjuna Kristam 
185349db4272SNagarjuna Kristam 	if (usb_endpoint_xfer_isoc(desc)) {
185449db4272SNagarjuna Kristam 		for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
185549db4272SNagarjuna Kristam 			if (xudc->ep[i].desc &&
185649db4272SNagarjuna Kristam 			    usb_endpoint_xfer_bulk(xudc->ep[i].desc))
185749db4272SNagarjuna Kristam 				ep_unpause(xudc, i);
185849db4272SNagarjuna Kristam 		}
185949db4272SNagarjuna Kristam 	}
186049db4272SNagarjuna Kristam 
186149db4272SNagarjuna Kristam out:
186249db4272SNagarjuna Kristam 	dev_info(xudc->dev, "EP %u (type: %s, dir: %s) enabled\n", ep->index,
186349db4272SNagarjuna Kristam 		 usb_ep_type_string(usb_endpoint_type(ep->desc)),
186449db4272SNagarjuna Kristam 		 usb_endpoint_dir_in(ep->desc) ? "in" : "out");
186549db4272SNagarjuna Kristam 
186649db4272SNagarjuna Kristam 	return 0;
186749db4272SNagarjuna Kristam }
186849db4272SNagarjuna Kristam 
186949db4272SNagarjuna Kristam static int tegra_xudc_ep_enable(struct usb_ep *usb_ep,
187049db4272SNagarjuna Kristam 				const struct usb_endpoint_descriptor *desc)
187149db4272SNagarjuna Kristam {
187249db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep;
187349db4272SNagarjuna Kristam 	struct tegra_xudc *xudc;
187449db4272SNagarjuna Kristam 	unsigned long flags;
187549db4272SNagarjuna Kristam 	int ret;
187649db4272SNagarjuna Kristam 
187749db4272SNagarjuna Kristam 	if  (!usb_ep || !desc || (desc->bDescriptorType != USB_DT_ENDPOINT))
187849db4272SNagarjuna Kristam 		return -EINVAL;
187949db4272SNagarjuna Kristam 
188049db4272SNagarjuna Kristam 	ep = to_xudc_ep(usb_ep);
188149db4272SNagarjuna Kristam 	xudc = ep->xudc;
188249db4272SNagarjuna Kristam 
188349db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
188449db4272SNagarjuna Kristam 	if (xudc->powergated) {
188549db4272SNagarjuna Kristam 		ret = -ESHUTDOWN;
188649db4272SNagarjuna Kristam 		goto unlock;
188749db4272SNagarjuna Kristam 	}
188849db4272SNagarjuna Kristam 
188949db4272SNagarjuna Kristam 	ret = __tegra_xudc_ep_enable(ep, desc);
189049db4272SNagarjuna Kristam unlock:
189149db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
189249db4272SNagarjuna Kristam 
189349db4272SNagarjuna Kristam 	return ret;
189449db4272SNagarjuna Kristam }
189549db4272SNagarjuna Kristam 
189649db4272SNagarjuna Kristam static struct usb_request *
189749db4272SNagarjuna Kristam tegra_xudc_ep_alloc_request(struct usb_ep *usb_ep, gfp_t gfp)
189849db4272SNagarjuna Kristam {
189949db4272SNagarjuna Kristam 	struct tegra_xudc_request *req;
190049db4272SNagarjuna Kristam 
190149db4272SNagarjuna Kristam 	req = kzalloc(sizeof(*req), gfp);
190249db4272SNagarjuna Kristam 	if (!req)
190349db4272SNagarjuna Kristam 		return NULL;
190449db4272SNagarjuna Kristam 
190549db4272SNagarjuna Kristam 	INIT_LIST_HEAD(&req->list);
190649db4272SNagarjuna Kristam 
190749db4272SNagarjuna Kristam 	return &req->usb_req;
190849db4272SNagarjuna Kristam }
190949db4272SNagarjuna Kristam 
191049db4272SNagarjuna Kristam static void tegra_xudc_ep_free_request(struct usb_ep *usb_ep,
191149db4272SNagarjuna Kristam 				       struct usb_request *usb_req)
191249db4272SNagarjuna Kristam {
191349db4272SNagarjuna Kristam 	struct tegra_xudc_request *req = to_xudc_req(usb_req);
191449db4272SNagarjuna Kristam 
191549db4272SNagarjuna Kristam 	kfree(req);
191649db4272SNagarjuna Kristam }
191749db4272SNagarjuna Kristam 
191853ad92fdSRikard Falkeborn static const struct usb_ep_ops tegra_xudc_ep_ops = {
191949db4272SNagarjuna Kristam 	.enable = tegra_xudc_ep_enable,
192049db4272SNagarjuna Kristam 	.disable = tegra_xudc_ep_disable,
192149db4272SNagarjuna Kristam 	.alloc_request = tegra_xudc_ep_alloc_request,
192249db4272SNagarjuna Kristam 	.free_request = tegra_xudc_ep_free_request,
192349db4272SNagarjuna Kristam 	.queue = tegra_xudc_ep_queue,
192449db4272SNagarjuna Kristam 	.dequeue = tegra_xudc_ep_dequeue,
192549db4272SNagarjuna Kristam 	.set_halt = tegra_xudc_ep_set_halt,
192649db4272SNagarjuna Kristam };
192749db4272SNagarjuna Kristam 
192849db4272SNagarjuna Kristam static int tegra_xudc_ep0_enable(struct usb_ep *usb_ep,
192949db4272SNagarjuna Kristam 				 const struct usb_endpoint_descriptor *desc)
193049db4272SNagarjuna Kristam {
193149db4272SNagarjuna Kristam 	return -EBUSY;
193249db4272SNagarjuna Kristam }
193349db4272SNagarjuna Kristam 
193449db4272SNagarjuna Kristam static int tegra_xudc_ep0_disable(struct usb_ep *usb_ep)
193549db4272SNagarjuna Kristam {
193649db4272SNagarjuna Kristam 	return -EBUSY;
193749db4272SNagarjuna Kristam }
193849db4272SNagarjuna Kristam 
193953ad92fdSRikard Falkeborn static const struct usb_ep_ops tegra_xudc_ep0_ops = {
194049db4272SNagarjuna Kristam 	.enable = tegra_xudc_ep0_enable,
194149db4272SNagarjuna Kristam 	.disable = tegra_xudc_ep0_disable,
194249db4272SNagarjuna Kristam 	.alloc_request = tegra_xudc_ep_alloc_request,
194349db4272SNagarjuna Kristam 	.free_request = tegra_xudc_ep_free_request,
194449db4272SNagarjuna Kristam 	.queue = tegra_xudc_ep_queue,
194549db4272SNagarjuna Kristam 	.dequeue = tegra_xudc_ep_dequeue,
194649db4272SNagarjuna Kristam 	.set_halt = tegra_xudc_ep_set_halt,
194749db4272SNagarjuna Kristam };
194849db4272SNagarjuna Kristam 
194949db4272SNagarjuna Kristam static int tegra_xudc_gadget_get_frame(struct usb_gadget *gadget)
195049db4272SNagarjuna Kristam {
195149db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = to_xudc(gadget);
195249db4272SNagarjuna Kristam 	unsigned long flags;
195349db4272SNagarjuna Kristam 	int ret;
195449db4272SNagarjuna Kristam 
195549db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
195649db4272SNagarjuna Kristam 	if (xudc->powergated) {
195749db4272SNagarjuna Kristam 		ret = -ESHUTDOWN;
195849db4272SNagarjuna Kristam 		goto unlock;
195949db4272SNagarjuna Kristam 	}
196049db4272SNagarjuna Kristam 
196149db4272SNagarjuna Kristam 	ret = (xudc_readl(xudc, MFINDEX) & MFINDEX_FRAME_MASK) >>
196249db4272SNagarjuna Kristam 		MFINDEX_FRAME_SHIFT;
196349db4272SNagarjuna Kristam unlock:
196449db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
196549db4272SNagarjuna Kristam 
196649db4272SNagarjuna Kristam 	return ret;
196749db4272SNagarjuna Kristam }
196849db4272SNagarjuna Kristam 
196949db4272SNagarjuna Kristam static void tegra_xudc_resume_device_state(struct tegra_xudc *xudc)
197049db4272SNagarjuna Kristam {
197149db4272SNagarjuna Kristam 	unsigned int i;
197249db4272SNagarjuna Kristam 	u32 val;
197349db4272SNagarjuna Kristam 
197449db4272SNagarjuna Kristam 	ep_unpause_all(xudc);
197549db4272SNagarjuna Kristam 
197649db4272SNagarjuna Kristam 	/* Direct link to U0. */
197749db4272SNagarjuna Kristam 	val = xudc_readl(xudc, PORTSC);
197849db4272SNagarjuna Kristam 	if (((val & PORTSC_PLS_MASK) >> PORTSC_PLS_SHIFT) != PORTSC_PLS_U0) {
197949db4272SNagarjuna Kristam 		val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
198049db4272SNagarjuna Kristam 		val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_U0);
198149db4272SNagarjuna Kristam 		xudc_writel(xudc, val, PORTSC);
198249db4272SNagarjuna Kristam 	}
198349db4272SNagarjuna Kristam 
198449db4272SNagarjuna Kristam 	if (xudc->device_state == USB_STATE_SUSPENDED) {
198549db4272SNagarjuna Kristam 		xudc->device_state = xudc->resume_state;
198649db4272SNagarjuna Kristam 		usb_gadget_set_state(&xudc->gadget, xudc->device_state);
198749db4272SNagarjuna Kristam 		xudc->resume_state = 0;
198849db4272SNagarjuna Kristam 	}
198949db4272SNagarjuna Kristam 
199049db4272SNagarjuna Kristam 	/*
199149db4272SNagarjuna Kristam 	 * Doorbells may be dropped if they are sent too soon (< ~200ns)
199249db4272SNagarjuna Kristam 	 * after unpausing the endpoint.  Wait for 500ns just to be safe.
199349db4272SNagarjuna Kristam 	 */
199449db4272SNagarjuna Kristam 	ndelay(500);
199549db4272SNagarjuna Kristam 	for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
199649db4272SNagarjuna Kristam 		tegra_xudc_ep_ring_doorbell(&xudc->ep[i]);
199749db4272SNagarjuna Kristam }
199849db4272SNagarjuna Kristam 
199949db4272SNagarjuna Kristam static int tegra_xudc_gadget_wakeup(struct usb_gadget *gadget)
200049db4272SNagarjuna Kristam {
200149db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = to_xudc(gadget);
200249db4272SNagarjuna Kristam 	unsigned long flags;
200349db4272SNagarjuna Kristam 	int ret = 0;
200449db4272SNagarjuna Kristam 	u32 val;
200549db4272SNagarjuna Kristam 
200649db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
200749db4272SNagarjuna Kristam 
200849db4272SNagarjuna Kristam 	if (xudc->powergated) {
200949db4272SNagarjuna Kristam 		ret = -ESHUTDOWN;
201049db4272SNagarjuna Kristam 		goto unlock;
201149db4272SNagarjuna Kristam 	}
201249db4272SNagarjuna Kristam 	val = xudc_readl(xudc, PORTPM);
201349db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "%s: PORTPM=%#x, speed=%x\n", __func__,
201449db4272SNagarjuna Kristam 			val, gadget->speed);
201549db4272SNagarjuna Kristam 
201649db4272SNagarjuna Kristam 	if (((xudc->gadget.speed <= USB_SPEED_HIGH) &&
201749db4272SNagarjuna Kristam 	     (val & PORTPM_RWE)) ||
201849db4272SNagarjuna Kristam 	    ((xudc->gadget.speed == USB_SPEED_SUPER) &&
201949db4272SNagarjuna Kristam 	     (val & PORTPM_FRWE))) {
202049db4272SNagarjuna Kristam 		tegra_xudc_resume_device_state(xudc);
202149db4272SNagarjuna Kristam 
202249db4272SNagarjuna Kristam 		/* Send Device Notification packet. */
202349db4272SNagarjuna Kristam 		if (xudc->gadget.speed == USB_SPEED_SUPER) {
202449db4272SNagarjuna Kristam 			val = DEVNOTIF_LO_TYPE(DEVNOTIF_LO_TYPE_FUNCTION_WAKE)
202549db4272SNagarjuna Kristam 					     | DEVNOTIF_LO_TRIG;
202649db4272SNagarjuna Kristam 			xudc_writel(xudc, 0, DEVNOTIF_HI);
202749db4272SNagarjuna Kristam 			xudc_writel(xudc, val, DEVNOTIF_LO);
202849db4272SNagarjuna Kristam 		}
202949db4272SNagarjuna Kristam 	}
203049db4272SNagarjuna Kristam 
203149db4272SNagarjuna Kristam unlock:
203249db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret);
203349db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
203449db4272SNagarjuna Kristam 
203549db4272SNagarjuna Kristam 	return ret;
203649db4272SNagarjuna Kristam }
203749db4272SNagarjuna Kristam 
203849db4272SNagarjuna Kristam static int tegra_xudc_gadget_pullup(struct usb_gadget *gadget, int is_on)
203949db4272SNagarjuna Kristam {
204049db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = to_xudc(gadget);
204149db4272SNagarjuna Kristam 	unsigned long flags;
204249db4272SNagarjuna Kristam 	u32 val;
204349db4272SNagarjuna Kristam 
204449db4272SNagarjuna Kristam 	pm_runtime_get_sync(xudc->dev);
204549db4272SNagarjuna Kristam 
204649db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
204749db4272SNagarjuna Kristam 
204849db4272SNagarjuna Kristam 	if (is_on != xudc->pullup) {
204949db4272SNagarjuna Kristam 		val = xudc_readl(xudc, CTRL);
205049db4272SNagarjuna Kristam 		if (is_on)
205149db4272SNagarjuna Kristam 			val |= CTRL_ENABLE;
205249db4272SNagarjuna Kristam 		else
205349db4272SNagarjuna Kristam 			val &= ~CTRL_ENABLE;
205449db4272SNagarjuna Kristam 		xudc_writel(xudc, val, CTRL);
205549db4272SNagarjuna Kristam 	}
205649db4272SNagarjuna Kristam 
205749db4272SNagarjuna Kristam 	xudc->pullup = is_on;
205849db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "%s: pullup:%d", __func__, is_on);
205949db4272SNagarjuna Kristam 
206049db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
206149db4272SNagarjuna Kristam 
206249db4272SNagarjuna Kristam 	pm_runtime_put(xudc->dev);
206349db4272SNagarjuna Kristam 
206449db4272SNagarjuna Kristam 	return 0;
206549db4272SNagarjuna Kristam }
206649db4272SNagarjuna Kristam 
206749db4272SNagarjuna Kristam static int tegra_xudc_gadget_start(struct usb_gadget *gadget,
206849db4272SNagarjuna Kristam 				   struct usb_gadget_driver *driver)
206949db4272SNagarjuna Kristam {
207049db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = to_xudc(gadget);
207149db4272SNagarjuna Kristam 	unsigned long flags;
207249db4272SNagarjuna Kristam 	u32 val;
207349db4272SNagarjuna Kristam 	int ret;
2074b4e19931SNagarjuna Kristam 	unsigned int i;
207549db4272SNagarjuna Kristam 
207649db4272SNagarjuna Kristam 	if (!driver)
207749db4272SNagarjuna Kristam 		return -EINVAL;
207849db4272SNagarjuna Kristam 
207949db4272SNagarjuna Kristam 	pm_runtime_get_sync(xudc->dev);
208049db4272SNagarjuna Kristam 
208149db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
208249db4272SNagarjuna Kristam 
208349db4272SNagarjuna Kristam 	if (xudc->driver) {
208449db4272SNagarjuna Kristam 		ret = -EBUSY;
208549db4272SNagarjuna Kristam 		goto unlock;
208649db4272SNagarjuna Kristam 	}
208749db4272SNagarjuna Kristam 
208849db4272SNagarjuna Kristam 	xudc->setup_state = WAIT_FOR_SETUP;
208949db4272SNagarjuna Kristam 	xudc->device_state = USB_STATE_DEFAULT;
209049db4272SNagarjuna Kristam 	usb_gadget_set_state(&xudc->gadget, xudc->device_state);
209149db4272SNagarjuna Kristam 
209249db4272SNagarjuna Kristam 	ret = __tegra_xudc_ep_enable(&xudc->ep[0], &tegra_xudc_ep0_desc);
209349db4272SNagarjuna Kristam 	if (ret < 0)
209449db4272SNagarjuna Kristam 		goto unlock;
209549db4272SNagarjuna Kristam 
209649db4272SNagarjuna Kristam 	val = xudc_readl(xudc, CTRL);
209749db4272SNagarjuna Kristam 	val |= CTRL_IE | CTRL_LSE;
209849db4272SNagarjuna Kristam 	xudc_writel(xudc, val, CTRL);
209949db4272SNagarjuna Kristam 
210049db4272SNagarjuna Kristam 	val = xudc_readl(xudc, PORTHALT);
210149db4272SNagarjuna Kristam 	val |= PORTHALT_STCHG_INTR_EN;
210249db4272SNagarjuna Kristam 	xudc_writel(xudc, val, PORTHALT);
210349db4272SNagarjuna Kristam 
210449db4272SNagarjuna Kristam 	if (xudc->pullup) {
210549db4272SNagarjuna Kristam 		val = xudc_readl(xudc, CTRL);
210649db4272SNagarjuna Kristam 		val |= CTRL_ENABLE;
210749db4272SNagarjuna Kristam 		xudc_writel(xudc, val, CTRL);
210849db4272SNagarjuna Kristam 	}
210949db4272SNagarjuna Kristam 
2110b4e19931SNagarjuna Kristam 	for (i = 0; i < xudc->soc->num_phys; i++)
2111b4e19931SNagarjuna Kristam 		if (xudc->usbphy[i])
2112b4e19931SNagarjuna Kristam 			otg_set_peripheral(xudc->usbphy[i]->otg, gadget);
2113b77f2ffeSNagarjuna Kristam 
211449db4272SNagarjuna Kristam 	xudc->driver = driver;
211549db4272SNagarjuna Kristam unlock:
211649db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret);
211749db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
211849db4272SNagarjuna Kristam 
211949db4272SNagarjuna Kristam 	pm_runtime_put(xudc->dev);
212049db4272SNagarjuna Kristam 
212149db4272SNagarjuna Kristam 	return ret;
212249db4272SNagarjuna Kristam }
212349db4272SNagarjuna Kristam 
212449db4272SNagarjuna Kristam static int tegra_xudc_gadget_stop(struct usb_gadget *gadget)
212549db4272SNagarjuna Kristam {
212649db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = to_xudc(gadget);
212749db4272SNagarjuna Kristam 	unsigned long flags;
212849db4272SNagarjuna Kristam 	u32 val;
2129b4e19931SNagarjuna Kristam 	unsigned int i;
213049db4272SNagarjuna Kristam 
213149db4272SNagarjuna Kristam 	pm_runtime_get_sync(xudc->dev);
213249db4272SNagarjuna Kristam 
213349db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
213449db4272SNagarjuna Kristam 
2135b4e19931SNagarjuna Kristam 	for (i = 0; i < xudc->soc->num_phys; i++)
2136b4e19931SNagarjuna Kristam 		if (xudc->usbphy[i])
2137b4e19931SNagarjuna Kristam 			otg_set_peripheral(xudc->usbphy[i]->otg, NULL);
2138b77f2ffeSNagarjuna Kristam 
213949db4272SNagarjuna Kristam 	val = xudc_readl(xudc, CTRL);
214049db4272SNagarjuna Kristam 	val &= ~(CTRL_IE | CTRL_ENABLE);
214149db4272SNagarjuna Kristam 	xudc_writel(xudc, val, CTRL);
214249db4272SNagarjuna Kristam 
214349db4272SNagarjuna Kristam 	__tegra_xudc_ep_disable(&xudc->ep[0]);
214449db4272SNagarjuna Kristam 
214549db4272SNagarjuna Kristam 	xudc->driver = NULL;
214649db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "Gadget stopped");
214749db4272SNagarjuna Kristam 
214849db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
214949db4272SNagarjuna Kristam 
215049db4272SNagarjuna Kristam 	pm_runtime_put(xudc->dev);
215149db4272SNagarjuna Kristam 
215249db4272SNagarjuna Kristam 	return 0;
215349db4272SNagarjuna Kristam }
215449db4272SNagarjuna Kristam 
2155ac82b56bSNagarjuna Kristam static int tegra_xudc_gadget_vbus_draw(struct usb_gadget *gadget,
2156ac82b56bSNagarjuna Kristam 						unsigned int m_a)
2157ac82b56bSNagarjuna Kristam {
2158ac82b56bSNagarjuna Kristam 	int ret = 0;
2159ac82b56bSNagarjuna Kristam 	struct tegra_xudc *xudc = to_xudc(gadget);
2160ac82b56bSNagarjuna Kristam 
2161ac82b56bSNagarjuna Kristam 	dev_dbg(xudc->dev, "%s: %u mA\n", __func__, m_a);
2162ac82b56bSNagarjuna Kristam 
2163ac82b56bSNagarjuna Kristam 	if (xudc->curr_usbphy->chg_type == SDP_TYPE)
2164ac82b56bSNagarjuna Kristam 		ret = usb_phy_set_power(xudc->curr_usbphy, m_a);
2165ac82b56bSNagarjuna Kristam 
2166ac82b56bSNagarjuna Kristam 	return ret;
2167ac82b56bSNagarjuna Kristam }
2168ac82b56bSNagarjuna Kristam 
216949db4272SNagarjuna Kristam static int tegra_xudc_set_selfpowered(struct usb_gadget *gadget, int is_on)
217049db4272SNagarjuna Kristam {
217149db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = to_xudc(gadget);
217249db4272SNagarjuna Kristam 
217349db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "%s: %d\n", __func__, is_on);
217449db4272SNagarjuna Kristam 	xudc->selfpowered = !!is_on;
217549db4272SNagarjuna Kristam 
217649db4272SNagarjuna Kristam 	return 0;
217749db4272SNagarjuna Kristam }
217849db4272SNagarjuna Kristam 
217953ad92fdSRikard Falkeborn static const struct usb_gadget_ops tegra_xudc_gadget_ops = {
218049db4272SNagarjuna Kristam 	.get_frame = tegra_xudc_gadget_get_frame,
218149db4272SNagarjuna Kristam 	.wakeup = tegra_xudc_gadget_wakeup,
218249db4272SNagarjuna Kristam 	.pullup = tegra_xudc_gadget_pullup,
218349db4272SNagarjuna Kristam 	.udc_start = tegra_xudc_gadget_start,
218449db4272SNagarjuna Kristam 	.udc_stop = tegra_xudc_gadget_stop,
2185ac82b56bSNagarjuna Kristam 	.vbus_draw = tegra_xudc_gadget_vbus_draw,
218649db4272SNagarjuna Kristam 	.set_selfpowered = tegra_xudc_set_selfpowered,
218749db4272SNagarjuna Kristam };
218849db4272SNagarjuna Kristam 
218949db4272SNagarjuna Kristam static void no_op_complete(struct usb_ep *ep, struct usb_request *req)
219049db4272SNagarjuna Kristam {
219149db4272SNagarjuna Kristam }
219249db4272SNagarjuna Kristam 
219349db4272SNagarjuna Kristam static int
219449db4272SNagarjuna Kristam tegra_xudc_ep0_queue_status(struct tegra_xudc *xudc,
219549db4272SNagarjuna Kristam 		void (*cmpl)(struct usb_ep *, struct usb_request *))
219649db4272SNagarjuna Kristam {
219749db4272SNagarjuna Kristam 	xudc->ep0_req->usb_req.buf = NULL;
219849db4272SNagarjuna Kristam 	xudc->ep0_req->usb_req.dma = 0;
219949db4272SNagarjuna Kristam 	xudc->ep0_req->usb_req.length = 0;
220049db4272SNagarjuna Kristam 	xudc->ep0_req->usb_req.complete = cmpl;
220149db4272SNagarjuna Kristam 	xudc->ep0_req->usb_req.context = xudc;
220249db4272SNagarjuna Kristam 
220349db4272SNagarjuna Kristam 	return __tegra_xudc_ep_queue(&xudc->ep[0], xudc->ep0_req);
220449db4272SNagarjuna Kristam }
220549db4272SNagarjuna Kristam 
220649db4272SNagarjuna Kristam static int
220749db4272SNagarjuna Kristam tegra_xudc_ep0_queue_data(struct tegra_xudc *xudc, void *buf, size_t len,
220849db4272SNagarjuna Kristam 		void (*cmpl)(struct usb_ep *, struct usb_request *))
220949db4272SNagarjuna Kristam {
221049db4272SNagarjuna Kristam 	xudc->ep0_req->usb_req.buf = buf;
221149db4272SNagarjuna Kristam 	xudc->ep0_req->usb_req.length = len;
221249db4272SNagarjuna Kristam 	xudc->ep0_req->usb_req.complete = cmpl;
221349db4272SNagarjuna Kristam 	xudc->ep0_req->usb_req.context = xudc;
221449db4272SNagarjuna Kristam 
221549db4272SNagarjuna Kristam 	return __tegra_xudc_ep_queue(&xudc->ep[0], xudc->ep0_req);
221649db4272SNagarjuna Kristam }
221749db4272SNagarjuna Kristam 
221849db4272SNagarjuna Kristam static void tegra_xudc_ep0_req_done(struct tegra_xudc *xudc)
221949db4272SNagarjuna Kristam {
222049db4272SNagarjuna Kristam 	switch (xudc->setup_state) {
222149db4272SNagarjuna Kristam 	case DATA_STAGE_XFER:
222249db4272SNagarjuna Kristam 		xudc->setup_state = STATUS_STAGE_RECV;
222349db4272SNagarjuna Kristam 		tegra_xudc_ep0_queue_status(xudc, no_op_complete);
222449db4272SNagarjuna Kristam 		break;
222549db4272SNagarjuna Kristam 	case DATA_STAGE_RECV:
222649db4272SNagarjuna Kristam 		xudc->setup_state = STATUS_STAGE_XFER;
222749db4272SNagarjuna Kristam 		tegra_xudc_ep0_queue_status(xudc, no_op_complete);
222849db4272SNagarjuna Kristam 		break;
222949db4272SNagarjuna Kristam 	default:
223049db4272SNagarjuna Kristam 		xudc->setup_state = WAIT_FOR_SETUP;
223149db4272SNagarjuna Kristam 		break;
223249db4272SNagarjuna Kristam 	}
223349db4272SNagarjuna Kristam }
223449db4272SNagarjuna Kristam 
223549db4272SNagarjuna Kristam static int tegra_xudc_ep0_delegate_req(struct tegra_xudc *xudc,
223649db4272SNagarjuna Kristam 				       struct usb_ctrlrequest *ctrl)
223749db4272SNagarjuna Kristam {
223849db4272SNagarjuna Kristam 	int ret;
223949db4272SNagarjuna Kristam 
224049db4272SNagarjuna Kristam 	spin_unlock(&xudc->lock);
224149db4272SNagarjuna Kristam 	ret = xudc->driver->setup(&xudc->gadget, ctrl);
224249db4272SNagarjuna Kristam 	spin_lock(&xudc->lock);
224349db4272SNagarjuna Kristam 
224449db4272SNagarjuna Kristam 	return ret;
224549db4272SNagarjuna Kristam }
224649db4272SNagarjuna Kristam 
224749db4272SNagarjuna Kristam static void set_feature_complete(struct usb_ep *ep, struct usb_request *req)
224849db4272SNagarjuna Kristam {
224949db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = req->context;
225049db4272SNagarjuna Kristam 
225149db4272SNagarjuna Kristam 	if (xudc->test_mode_pattern) {
225249db4272SNagarjuna Kristam 		xudc_writel(xudc, xudc->test_mode_pattern, PORT_TM);
225349db4272SNagarjuna Kristam 		xudc->test_mode_pattern = 0;
225449db4272SNagarjuna Kristam 	}
225549db4272SNagarjuna Kristam }
225649db4272SNagarjuna Kristam 
225749db4272SNagarjuna Kristam static int tegra_xudc_ep0_set_feature(struct tegra_xudc *xudc,
225849db4272SNagarjuna Kristam 				      struct usb_ctrlrequest *ctrl)
225949db4272SNagarjuna Kristam {
226049db4272SNagarjuna Kristam 	bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
226149db4272SNagarjuna Kristam 	u32 feature = le16_to_cpu(ctrl->wValue);
226249db4272SNagarjuna Kristam 	u32 index = le16_to_cpu(ctrl->wIndex);
226349db4272SNagarjuna Kristam 	u32 val, ep;
226449db4272SNagarjuna Kristam 	int ret;
226549db4272SNagarjuna Kristam 
226649db4272SNagarjuna Kristam 	if (le16_to_cpu(ctrl->wLength) != 0)
226749db4272SNagarjuna Kristam 		return -EINVAL;
226849db4272SNagarjuna Kristam 
226949db4272SNagarjuna Kristam 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
227049db4272SNagarjuna Kristam 	case USB_RECIP_DEVICE:
227149db4272SNagarjuna Kristam 		switch (feature) {
227249db4272SNagarjuna Kristam 		case USB_DEVICE_REMOTE_WAKEUP:
227349db4272SNagarjuna Kristam 			if ((xudc->gadget.speed == USB_SPEED_SUPER) ||
227449db4272SNagarjuna Kristam 			    (xudc->device_state == USB_STATE_DEFAULT))
227549db4272SNagarjuna Kristam 				return -EINVAL;
227649db4272SNagarjuna Kristam 
227749db4272SNagarjuna Kristam 			val = xudc_readl(xudc, PORTPM);
227849db4272SNagarjuna Kristam 			if (set)
227949db4272SNagarjuna Kristam 				val |= PORTPM_RWE;
228049db4272SNagarjuna Kristam 			else
228149db4272SNagarjuna Kristam 				val &= ~PORTPM_RWE;
228249db4272SNagarjuna Kristam 
228349db4272SNagarjuna Kristam 			xudc_writel(xudc, val, PORTPM);
228449db4272SNagarjuna Kristam 			break;
228549db4272SNagarjuna Kristam 		case USB_DEVICE_U1_ENABLE:
228649db4272SNagarjuna Kristam 		case USB_DEVICE_U2_ENABLE:
228749db4272SNagarjuna Kristam 			if ((xudc->device_state != USB_STATE_CONFIGURED) ||
228849db4272SNagarjuna Kristam 			    (xudc->gadget.speed != USB_SPEED_SUPER))
228949db4272SNagarjuna Kristam 				return -EINVAL;
229049db4272SNagarjuna Kristam 
229149db4272SNagarjuna Kristam 			val = xudc_readl(xudc, PORTPM);
229249db4272SNagarjuna Kristam 			if ((feature == USB_DEVICE_U1_ENABLE) &&
229349db4272SNagarjuna Kristam 			     xudc->soc->u1_enable) {
229449db4272SNagarjuna Kristam 				if (set)
229549db4272SNagarjuna Kristam 					val |= PORTPM_U1E;
229649db4272SNagarjuna Kristam 				else
229749db4272SNagarjuna Kristam 					val &= ~PORTPM_U1E;
229849db4272SNagarjuna Kristam 			}
229949db4272SNagarjuna Kristam 
230049db4272SNagarjuna Kristam 			if ((feature == USB_DEVICE_U2_ENABLE) &&
230149db4272SNagarjuna Kristam 			     xudc->soc->u2_enable) {
230249db4272SNagarjuna Kristam 				if (set)
230349db4272SNagarjuna Kristam 					val |= PORTPM_U2E;
230449db4272SNagarjuna Kristam 				else
230549db4272SNagarjuna Kristam 					val &= ~PORTPM_U2E;
230649db4272SNagarjuna Kristam 			}
230749db4272SNagarjuna Kristam 
230849db4272SNagarjuna Kristam 			xudc_writel(xudc, val, PORTPM);
230949db4272SNagarjuna Kristam 			break;
231049db4272SNagarjuna Kristam 		case USB_DEVICE_TEST_MODE:
231149db4272SNagarjuna Kristam 			if (xudc->gadget.speed != USB_SPEED_HIGH)
231249db4272SNagarjuna Kristam 				return -EINVAL;
231349db4272SNagarjuna Kristam 
231449db4272SNagarjuna Kristam 			if (!set)
231549db4272SNagarjuna Kristam 				return -EINVAL;
231649db4272SNagarjuna Kristam 
231749db4272SNagarjuna Kristam 			xudc->test_mode_pattern = index >> 8;
231849db4272SNagarjuna Kristam 			break;
231949db4272SNagarjuna Kristam 		default:
232049db4272SNagarjuna Kristam 			return -EINVAL;
232149db4272SNagarjuna Kristam 		}
232249db4272SNagarjuna Kristam 
232349db4272SNagarjuna Kristam 		break;
232449db4272SNagarjuna Kristam 	case USB_RECIP_INTERFACE:
232549db4272SNagarjuna Kristam 		if (xudc->device_state != USB_STATE_CONFIGURED)
232649db4272SNagarjuna Kristam 			return -EINVAL;
232749db4272SNagarjuna Kristam 
232849db4272SNagarjuna Kristam 		switch (feature) {
232949db4272SNagarjuna Kristam 		case USB_INTRF_FUNC_SUSPEND:
233049db4272SNagarjuna Kristam 			if (set) {
233149db4272SNagarjuna Kristam 				val = xudc_readl(xudc, PORTPM);
233249db4272SNagarjuna Kristam 
233349db4272SNagarjuna Kristam 				if (index & USB_INTRF_FUNC_SUSPEND_RW)
233449db4272SNagarjuna Kristam 					val |= PORTPM_FRWE;
233549db4272SNagarjuna Kristam 				else
233649db4272SNagarjuna Kristam 					val &= ~PORTPM_FRWE;
233749db4272SNagarjuna Kristam 
233849db4272SNagarjuna Kristam 				xudc_writel(xudc, val, PORTPM);
233949db4272SNagarjuna Kristam 			}
234049db4272SNagarjuna Kristam 
234149db4272SNagarjuna Kristam 			return tegra_xudc_ep0_delegate_req(xudc, ctrl);
234249db4272SNagarjuna Kristam 		default:
234349db4272SNagarjuna Kristam 			return -EINVAL;
234449db4272SNagarjuna Kristam 		}
234549db4272SNagarjuna Kristam 
234649db4272SNagarjuna Kristam 		break;
234749db4272SNagarjuna Kristam 	case USB_RECIP_ENDPOINT:
234849db4272SNagarjuna Kristam 		ep = (index & USB_ENDPOINT_NUMBER_MASK) * 2 +
234949db4272SNagarjuna Kristam 			((index & USB_DIR_IN) ? 1 : 0);
235049db4272SNagarjuna Kristam 
235149db4272SNagarjuna Kristam 		if ((xudc->device_state == USB_STATE_DEFAULT) ||
235249db4272SNagarjuna Kristam 		    ((xudc->device_state == USB_STATE_ADDRESS) &&
235349db4272SNagarjuna Kristam 		     (index != 0)))
235449db4272SNagarjuna Kristam 			return -EINVAL;
235549db4272SNagarjuna Kristam 
235649db4272SNagarjuna Kristam 		ret = __tegra_xudc_ep_set_halt(&xudc->ep[ep], set);
235749db4272SNagarjuna Kristam 		if (ret < 0)
235849db4272SNagarjuna Kristam 			return ret;
235949db4272SNagarjuna Kristam 		break;
236049db4272SNagarjuna Kristam 	default:
236149db4272SNagarjuna Kristam 		return -EINVAL;
236249db4272SNagarjuna Kristam 	}
236349db4272SNagarjuna Kristam 
236449db4272SNagarjuna Kristam 	return tegra_xudc_ep0_queue_status(xudc, set_feature_complete);
236549db4272SNagarjuna Kristam }
236649db4272SNagarjuna Kristam 
236749db4272SNagarjuna Kristam static int tegra_xudc_ep0_get_status(struct tegra_xudc *xudc,
236849db4272SNagarjuna Kristam 				     struct usb_ctrlrequest *ctrl)
236949db4272SNagarjuna Kristam {
237049db4272SNagarjuna Kristam 	struct tegra_xudc_ep_context *ep_ctx;
237149db4272SNagarjuna Kristam 	u32 val, ep, index = le16_to_cpu(ctrl->wIndex);
237249db4272SNagarjuna Kristam 	u16 status = 0;
237349db4272SNagarjuna Kristam 
237449db4272SNagarjuna Kristam 	if (!(ctrl->bRequestType & USB_DIR_IN))
237549db4272SNagarjuna Kristam 		return -EINVAL;
237649db4272SNagarjuna Kristam 
237749db4272SNagarjuna Kristam 	if ((le16_to_cpu(ctrl->wValue) != 0) ||
237849db4272SNagarjuna Kristam 	    (le16_to_cpu(ctrl->wLength) != 2))
237949db4272SNagarjuna Kristam 		return -EINVAL;
238049db4272SNagarjuna Kristam 
238149db4272SNagarjuna Kristam 	switch (ctrl->bRequestType & USB_RECIP_MASK) {
238249db4272SNagarjuna Kristam 	case USB_RECIP_DEVICE:
238349db4272SNagarjuna Kristam 		val = xudc_readl(xudc, PORTPM);
238449db4272SNagarjuna Kristam 
238549db4272SNagarjuna Kristam 		if (xudc->selfpowered)
238649db4272SNagarjuna Kristam 			status |= BIT(USB_DEVICE_SELF_POWERED);
238749db4272SNagarjuna Kristam 
238849db4272SNagarjuna Kristam 		if ((xudc->gadget.speed < USB_SPEED_SUPER) &&
238949db4272SNagarjuna Kristam 		    (val & PORTPM_RWE))
239049db4272SNagarjuna Kristam 			status |= BIT(USB_DEVICE_REMOTE_WAKEUP);
239149db4272SNagarjuna Kristam 
239249db4272SNagarjuna Kristam 		if (xudc->gadget.speed == USB_SPEED_SUPER) {
239349db4272SNagarjuna Kristam 			if (val & PORTPM_U1E)
239449db4272SNagarjuna Kristam 				status |= BIT(USB_DEV_STAT_U1_ENABLED);
239549db4272SNagarjuna Kristam 			if (val & PORTPM_U2E)
239649db4272SNagarjuna Kristam 				status |= BIT(USB_DEV_STAT_U2_ENABLED);
239749db4272SNagarjuna Kristam 		}
239849db4272SNagarjuna Kristam 		break;
239949db4272SNagarjuna Kristam 	case USB_RECIP_INTERFACE:
240049db4272SNagarjuna Kristam 		if (xudc->gadget.speed == USB_SPEED_SUPER) {
240149db4272SNagarjuna Kristam 			status |= USB_INTRF_STAT_FUNC_RW_CAP;
240249db4272SNagarjuna Kristam 			val = xudc_readl(xudc, PORTPM);
240349db4272SNagarjuna Kristam 			if (val & PORTPM_FRWE)
240449db4272SNagarjuna Kristam 				status |= USB_INTRF_STAT_FUNC_RW;
240549db4272SNagarjuna Kristam 		}
240649db4272SNagarjuna Kristam 		break;
240749db4272SNagarjuna Kristam 	case USB_RECIP_ENDPOINT:
240849db4272SNagarjuna Kristam 		ep = (index & USB_ENDPOINT_NUMBER_MASK) * 2 +
240949db4272SNagarjuna Kristam 			((index & USB_DIR_IN) ? 1 : 0);
241049db4272SNagarjuna Kristam 		ep_ctx = &xudc->ep_context[ep];
241149db4272SNagarjuna Kristam 
241249db4272SNagarjuna Kristam 		if ((xudc->device_state != USB_STATE_CONFIGURED) &&
241349db4272SNagarjuna Kristam 		    ((xudc->device_state != USB_STATE_ADDRESS) || (ep != 0)))
241449db4272SNagarjuna Kristam 			return -EINVAL;
241549db4272SNagarjuna Kristam 
241649db4272SNagarjuna Kristam 		if (ep_ctx_read_state(ep_ctx) == EP_STATE_DISABLED)
241749db4272SNagarjuna Kristam 			return -EINVAL;
241849db4272SNagarjuna Kristam 
241949db4272SNagarjuna Kristam 		if (xudc_readl(xudc, EP_HALT) & BIT(ep))
242049db4272SNagarjuna Kristam 			status |= BIT(USB_ENDPOINT_HALT);
242149db4272SNagarjuna Kristam 		break;
242249db4272SNagarjuna Kristam 	default:
242349db4272SNagarjuna Kristam 		return -EINVAL;
242449db4272SNagarjuna Kristam 	}
242549db4272SNagarjuna Kristam 
242649db4272SNagarjuna Kristam 	xudc->status_buf = cpu_to_le16(status);
242749db4272SNagarjuna Kristam 	return tegra_xudc_ep0_queue_data(xudc, &xudc->status_buf,
242849db4272SNagarjuna Kristam 					 sizeof(xudc->status_buf),
242949db4272SNagarjuna Kristam 					 no_op_complete);
243049db4272SNagarjuna Kristam }
243149db4272SNagarjuna Kristam 
243249db4272SNagarjuna Kristam static void set_sel_complete(struct usb_ep *ep, struct usb_request *req)
243349db4272SNagarjuna Kristam {
243449db4272SNagarjuna Kristam 	/* Nothing to do with SEL values */
243549db4272SNagarjuna Kristam }
243649db4272SNagarjuna Kristam 
243749db4272SNagarjuna Kristam static int tegra_xudc_ep0_set_sel(struct tegra_xudc *xudc,
243849db4272SNagarjuna Kristam 				  struct usb_ctrlrequest *ctrl)
243949db4272SNagarjuna Kristam {
244049db4272SNagarjuna Kristam 	if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
244149db4272SNagarjuna Kristam 				     USB_TYPE_STANDARD))
244249db4272SNagarjuna Kristam 		return -EINVAL;
244349db4272SNagarjuna Kristam 
244449db4272SNagarjuna Kristam 	if (xudc->device_state == USB_STATE_DEFAULT)
244549db4272SNagarjuna Kristam 		return -EINVAL;
244649db4272SNagarjuna Kristam 
244749db4272SNagarjuna Kristam 	if ((le16_to_cpu(ctrl->wIndex) != 0) ||
244849db4272SNagarjuna Kristam 	    (le16_to_cpu(ctrl->wValue) != 0) ||
244949db4272SNagarjuna Kristam 	    (le16_to_cpu(ctrl->wLength) != 6))
245049db4272SNagarjuna Kristam 		return -EINVAL;
245149db4272SNagarjuna Kristam 
245249db4272SNagarjuna Kristam 	return tegra_xudc_ep0_queue_data(xudc, &xudc->sel_timing,
245349db4272SNagarjuna Kristam 					 sizeof(xudc->sel_timing),
245449db4272SNagarjuna Kristam 					 set_sel_complete);
245549db4272SNagarjuna Kristam }
245649db4272SNagarjuna Kristam 
245749db4272SNagarjuna Kristam static void set_isoch_delay_complete(struct usb_ep *ep, struct usb_request *req)
245849db4272SNagarjuna Kristam {
245949db4272SNagarjuna Kristam 	/* Nothing to do with isoch delay */
246049db4272SNagarjuna Kristam }
246149db4272SNagarjuna Kristam 
246249db4272SNagarjuna Kristam static int tegra_xudc_ep0_set_isoch_delay(struct tegra_xudc *xudc,
246349db4272SNagarjuna Kristam 					  struct usb_ctrlrequest *ctrl)
246449db4272SNagarjuna Kristam {
246549db4272SNagarjuna Kristam 	u32 delay = le16_to_cpu(ctrl->wValue);
246649db4272SNagarjuna Kristam 
246749db4272SNagarjuna Kristam 	if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
246849db4272SNagarjuna Kristam 				   USB_TYPE_STANDARD))
246949db4272SNagarjuna Kristam 		return -EINVAL;
247049db4272SNagarjuna Kristam 
247149db4272SNagarjuna Kristam 	if ((delay > 65535) || (le16_to_cpu(ctrl->wIndex) != 0) ||
247249db4272SNagarjuna Kristam 	    (le16_to_cpu(ctrl->wLength) != 0))
247349db4272SNagarjuna Kristam 		return -EINVAL;
247449db4272SNagarjuna Kristam 
247549db4272SNagarjuna Kristam 	xudc->isoch_delay = delay;
247649db4272SNagarjuna Kristam 
247749db4272SNagarjuna Kristam 	return tegra_xudc_ep0_queue_status(xudc, set_isoch_delay_complete);
247849db4272SNagarjuna Kristam }
247949db4272SNagarjuna Kristam 
248049db4272SNagarjuna Kristam static void set_address_complete(struct usb_ep *ep, struct usb_request *req)
248149db4272SNagarjuna Kristam {
248249db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = req->context;
248349db4272SNagarjuna Kristam 
248449db4272SNagarjuna Kristam 	if ((xudc->device_state == USB_STATE_DEFAULT) &&
248549db4272SNagarjuna Kristam 	    (xudc->dev_addr != 0)) {
248649db4272SNagarjuna Kristam 		xudc->device_state = USB_STATE_ADDRESS;
248749db4272SNagarjuna Kristam 		usb_gadget_set_state(&xudc->gadget, xudc->device_state);
248849db4272SNagarjuna Kristam 	} else if ((xudc->device_state == USB_STATE_ADDRESS) &&
248949db4272SNagarjuna Kristam 		   (xudc->dev_addr == 0)) {
249049db4272SNagarjuna Kristam 		xudc->device_state = USB_STATE_DEFAULT;
249149db4272SNagarjuna Kristam 		usb_gadget_set_state(&xudc->gadget, xudc->device_state);
249249db4272SNagarjuna Kristam 	}
249349db4272SNagarjuna Kristam }
249449db4272SNagarjuna Kristam 
249549db4272SNagarjuna Kristam static int tegra_xudc_ep0_set_address(struct tegra_xudc *xudc,
249649db4272SNagarjuna Kristam 				      struct usb_ctrlrequest *ctrl)
249749db4272SNagarjuna Kristam {
249849db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep0 = &xudc->ep[0];
249949db4272SNagarjuna Kristam 	u32 val, addr = le16_to_cpu(ctrl->wValue);
250049db4272SNagarjuna Kristam 
250149db4272SNagarjuna Kristam 	if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
250249db4272SNagarjuna Kristam 				     USB_TYPE_STANDARD))
250349db4272SNagarjuna Kristam 		return -EINVAL;
250449db4272SNagarjuna Kristam 
250549db4272SNagarjuna Kristam 	if ((addr > 127) || (le16_to_cpu(ctrl->wIndex) != 0) ||
250649db4272SNagarjuna Kristam 	    (le16_to_cpu(ctrl->wLength) != 0))
250749db4272SNagarjuna Kristam 		return -EINVAL;
250849db4272SNagarjuna Kristam 
250949db4272SNagarjuna Kristam 	if (xudc->device_state == USB_STATE_CONFIGURED)
251049db4272SNagarjuna Kristam 		return -EINVAL;
251149db4272SNagarjuna Kristam 
251249db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "set address: %u\n", addr);
251349db4272SNagarjuna Kristam 
251449db4272SNagarjuna Kristam 	xudc->dev_addr = addr;
251549db4272SNagarjuna Kristam 	val = xudc_readl(xudc, CTRL);
251649db4272SNagarjuna Kristam 	val &= ~(CTRL_DEVADDR_MASK);
251749db4272SNagarjuna Kristam 	val |= CTRL_DEVADDR(addr);
251849db4272SNagarjuna Kristam 	xudc_writel(xudc, val, CTRL);
251949db4272SNagarjuna Kristam 
252049db4272SNagarjuna Kristam 	ep_ctx_write_devaddr(ep0->context, addr);
252149db4272SNagarjuna Kristam 
252249db4272SNagarjuna Kristam 	return tegra_xudc_ep0_queue_status(xudc, set_address_complete);
252349db4272SNagarjuna Kristam }
252449db4272SNagarjuna Kristam 
252549db4272SNagarjuna Kristam static int tegra_xudc_ep0_standard_req(struct tegra_xudc *xudc,
252649db4272SNagarjuna Kristam 				      struct usb_ctrlrequest *ctrl)
252749db4272SNagarjuna Kristam {
252849db4272SNagarjuna Kristam 	int ret;
252949db4272SNagarjuna Kristam 
253049db4272SNagarjuna Kristam 	switch (ctrl->bRequest) {
253149db4272SNagarjuna Kristam 	case USB_REQ_GET_STATUS:
253249db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "USB_REQ_GET_STATUS\n");
253349db4272SNagarjuna Kristam 		ret = tegra_xudc_ep0_get_status(xudc, ctrl);
253449db4272SNagarjuna Kristam 		break;
253549db4272SNagarjuna Kristam 	case USB_REQ_SET_ADDRESS:
253649db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "USB_REQ_SET_ADDRESS\n");
253749db4272SNagarjuna Kristam 		ret = tegra_xudc_ep0_set_address(xudc, ctrl);
253849db4272SNagarjuna Kristam 		break;
253949db4272SNagarjuna Kristam 	case USB_REQ_SET_SEL:
254049db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "USB_REQ_SET_SEL\n");
254149db4272SNagarjuna Kristam 		ret = tegra_xudc_ep0_set_sel(xudc, ctrl);
254249db4272SNagarjuna Kristam 		break;
254349db4272SNagarjuna Kristam 	case USB_REQ_SET_ISOCH_DELAY:
254449db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
254549db4272SNagarjuna Kristam 		ret = tegra_xudc_ep0_set_isoch_delay(xudc, ctrl);
254649db4272SNagarjuna Kristam 		break;
254749db4272SNagarjuna Kristam 	case USB_REQ_CLEAR_FEATURE:
254849db4272SNagarjuna Kristam 	case USB_REQ_SET_FEATURE:
254949db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "USB_REQ_CLEAR/SET_FEATURE\n");
255049db4272SNagarjuna Kristam 		ret = tegra_xudc_ep0_set_feature(xudc, ctrl);
255149db4272SNagarjuna Kristam 		break;
255249db4272SNagarjuna Kristam 	case USB_REQ_SET_CONFIGURATION:
255349db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "USB_REQ_SET_CONFIGURATION\n");
255449db4272SNagarjuna Kristam 		/*
255549db4272SNagarjuna Kristam 		 * In theory we need to clear RUN bit before status stage of
255649db4272SNagarjuna Kristam 		 * deconfig request sent, but this seems to be causing problems.
255749db4272SNagarjuna Kristam 		 * Clear RUN once all endpoints are disabled instead.
255849db4272SNagarjuna Kristam 		 */
255949db4272SNagarjuna Kristam 		fallthrough;
256049db4272SNagarjuna Kristam 	default:
256149db4272SNagarjuna Kristam 		ret = tegra_xudc_ep0_delegate_req(xudc, ctrl);
256249db4272SNagarjuna Kristam 		break;
256349db4272SNagarjuna Kristam 	}
256449db4272SNagarjuna Kristam 
256549db4272SNagarjuna Kristam 	return ret;
256649db4272SNagarjuna Kristam }
256749db4272SNagarjuna Kristam 
256849db4272SNagarjuna Kristam static void tegra_xudc_handle_ep0_setup_packet(struct tegra_xudc *xudc,
256949db4272SNagarjuna Kristam 					       struct usb_ctrlrequest *ctrl,
257049db4272SNagarjuna Kristam 					       u16 seq_num)
257149db4272SNagarjuna Kristam {
257249db4272SNagarjuna Kristam 	int ret;
257349db4272SNagarjuna Kristam 
257449db4272SNagarjuna Kristam 	xudc->setup_seq_num = seq_num;
257549db4272SNagarjuna Kristam 
257649db4272SNagarjuna Kristam 	/* Ensure EP0 is unhalted. */
257749db4272SNagarjuna Kristam 	ep_unhalt(xudc, 0);
257849db4272SNagarjuna Kristam 
257949db4272SNagarjuna Kristam 	/*
258049db4272SNagarjuna Kristam 	 * On Tegra210, setup packets with sequence numbers 0xfffe or 0xffff
258149db4272SNagarjuna Kristam 	 * are invalid.  Halt EP0 until we get a valid packet.
258249db4272SNagarjuna Kristam 	 */
258349db4272SNagarjuna Kristam 	if (xudc->soc->invalid_seq_num &&
258449db4272SNagarjuna Kristam 	    (seq_num == 0xfffe || seq_num == 0xffff)) {
258549db4272SNagarjuna Kristam 		dev_warn(xudc->dev, "invalid sequence number detected\n");
258649db4272SNagarjuna Kristam 		ep_halt(xudc, 0);
258749db4272SNagarjuna Kristam 		return;
258849db4272SNagarjuna Kristam 	}
258949db4272SNagarjuna Kristam 
259049db4272SNagarjuna Kristam 	if (ctrl->wLength)
259149db4272SNagarjuna Kristam 		xudc->setup_state = (ctrl->bRequestType & USB_DIR_IN) ?
259249db4272SNagarjuna Kristam 			DATA_STAGE_XFER :  DATA_STAGE_RECV;
259349db4272SNagarjuna Kristam 	else
259449db4272SNagarjuna Kristam 		xudc->setup_state = STATUS_STAGE_XFER;
259549db4272SNagarjuna Kristam 
259649db4272SNagarjuna Kristam 	if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
259749db4272SNagarjuna Kristam 		ret = tegra_xudc_ep0_standard_req(xudc, ctrl);
259849db4272SNagarjuna Kristam 	else
259949db4272SNagarjuna Kristam 		ret = tegra_xudc_ep0_delegate_req(xudc, ctrl);
260049db4272SNagarjuna Kristam 
260149db4272SNagarjuna Kristam 	if (ret < 0) {
260249db4272SNagarjuna Kristam 		dev_warn(xudc->dev, "setup request failed: %d\n", ret);
260349db4272SNagarjuna Kristam 		xudc->setup_state = WAIT_FOR_SETUP;
260449db4272SNagarjuna Kristam 		ep_halt(xudc, 0);
260549db4272SNagarjuna Kristam 	}
260649db4272SNagarjuna Kristam }
260749db4272SNagarjuna Kristam 
260849db4272SNagarjuna Kristam static void tegra_xudc_handle_ep0_event(struct tegra_xudc *xudc,
260949db4272SNagarjuna Kristam 					struct tegra_xudc_trb *event)
261049db4272SNagarjuna Kristam {
261149db4272SNagarjuna Kristam 	struct usb_ctrlrequest *ctrl = (struct usb_ctrlrequest *)event;
261249db4272SNagarjuna Kristam 	u16 seq_num = trb_read_seq_num(event);
261349db4272SNagarjuna Kristam 
261449db4272SNagarjuna Kristam 	if (xudc->setup_state != WAIT_FOR_SETUP) {
261549db4272SNagarjuna Kristam 		/*
261649db4272SNagarjuna Kristam 		 * The controller is in the process of handling another
261749db4272SNagarjuna Kristam 		 * setup request.  Queue subsequent requests and handle
261849db4272SNagarjuna Kristam 		 * the last one once the controller reports a sequence
261949db4272SNagarjuna Kristam 		 * number error.
262049db4272SNagarjuna Kristam 		 */
262149db4272SNagarjuna Kristam 		memcpy(&xudc->setup_packet.ctrl_req, ctrl, sizeof(*ctrl));
262249db4272SNagarjuna Kristam 		xudc->setup_packet.seq_num = seq_num;
262349db4272SNagarjuna Kristam 		xudc->queued_setup_packet = true;
262449db4272SNagarjuna Kristam 	} else {
262549db4272SNagarjuna Kristam 		tegra_xudc_handle_ep0_setup_packet(xudc, ctrl, seq_num);
262649db4272SNagarjuna Kristam 	}
262749db4272SNagarjuna Kristam }
262849db4272SNagarjuna Kristam 
262949db4272SNagarjuna Kristam static struct tegra_xudc_request *
263049db4272SNagarjuna Kristam trb_to_request(struct tegra_xudc_ep *ep, struct tegra_xudc_trb *trb)
263149db4272SNagarjuna Kristam {
263249db4272SNagarjuna Kristam 	struct tegra_xudc_request *req;
263349db4272SNagarjuna Kristam 
263449db4272SNagarjuna Kristam 	list_for_each_entry(req, &ep->queue, list) {
263549db4272SNagarjuna Kristam 		if (!req->trbs_queued)
263649db4272SNagarjuna Kristam 			break;
263749db4272SNagarjuna Kristam 
263849db4272SNagarjuna Kristam 		if (trb_in_request(ep, req, trb))
263949db4272SNagarjuna Kristam 			return req;
264049db4272SNagarjuna Kristam 	}
264149db4272SNagarjuna Kristam 
264249db4272SNagarjuna Kristam 	return NULL;
264349db4272SNagarjuna Kristam }
264449db4272SNagarjuna Kristam 
264549db4272SNagarjuna Kristam static void tegra_xudc_handle_transfer_completion(struct tegra_xudc *xudc,
264649db4272SNagarjuna Kristam 						  struct tegra_xudc_ep *ep,
264749db4272SNagarjuna Kristam 						  struct tegra_xudc_trb *event)
264849db4272SNagarjuna Kristam {
264949db4272SNagarjuna Kristam 	struct tegra_xudc_request *req;
265049db4272SNagarjuna Kristam 	struct tegra_xudc_trb *trb;
265149db4272SNagarjuna Kristam 	bool short_packet;
265249db4272SNagarjuna Kristam 
265349db4272SNagarjuna Kristam 	short_packet = (trb_read_cmpl_code(event) ==
265449db4272SNagarjuna Kristam 			TRB_CMPL_CODE_SHORT_PACKET);
265549db4272SNagarjuna Kristam 
265649db4272SNagarjuna Kristam 	trb = trb_phys_to_virt(ep, trb_read_data_ptr(event));
265749db4272SNagarjuna Kristam 	req = trb_to_request(ep, trb);
265849db4272SNagarjuna Kristam 
265949db4272SNagarjuna Kristam 	/*
266049db4272SNagarjuna Kristam 	 * TDs are complete on short packet or when the completed TRB is the
266149db4272SNagarjuna Kristam 	 * last TRB in the TD (the CHAIN bit is unset).
266249db4272SNagarjuna Kristam 	 */
266349db4272SNagarjuna Kristam 	if (req && (short_packet || (!trb_read_chain(trb) &&
266449db4272SNagarjuna Kristam 		(req->trbs_needed == req->trbs_queued)))) {
266549db4272SNagarjuna Kristam 		struct tegra_xudc_trb *last = req->last_trb;
266649db4272SNagarjuna Kristam 		unsigned int residual;
266749db4272SNagarjuna Kristam 
266849db4272SNagarjuna Kristam 		residual = trb_read_transfer_len(event);
266949db4272SNagarjuna Kristam 		req->usb_req.actual = req->usb_req.length - residual;
267049db4272SNagarjuna Kristam 
267149db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "bytes transferred %u / %u\n",
267249db4272SNagarjuna Kristam 			req->usb_req.actual, req->usb_req.length);
267349db4272SNagarjuna Kristam 
267449db4272SNagarjuna Kristam 		tegra_xudc_req_done(ep, req, 0);
267549db4272SNagarjuna Kristam 
267649db4272SNagarjuna Kristam 		if (ep->desc && usb_endpoint_xfer_control(ep->desc))
267749db4272SNagarjuna Kristam 			tegra_xudc_ep0_req_done(xudc);
267849db4272SNagarjuna Kristam 
267949db4272SNagarjuna Kristam 		/*
268049db4272SNagarjuna Kristam 		 * Advance the dequeue pointer past the end of the current TD
268149db4272SNagarjuna Kristam 		 * on short packet completion.
268249db4272SNagarjuna Kristam 		 */
268349db4272SNagarjuna Kristam 		if (short_packet) {
268449db4272SNagarjuna Kristam 			ep->deq_ptr = (last - ep->transfer_ring) + 1;
268549db4272SNagarjuna Kristam 			if (ep->deq_ptr == XUDC_TRANSFER_RING_SIZE - 1)
268649db4272SNagarjuna Kristam 				ep->deq_ptr = 0;
268749db4272SNagarjuna Kristam 		}
268849db4272SNagarjuna Kristam 	} else if (!req) {
268949db4272SNagarjuna Kristam 		dev_warn(xudc->dev, "transfer event on dequeued request\n");
269049db4272SNagarjuna Kristam 	}
269149db4272SNagarjuna Kristam 
269249db4272SNagarjuna Kristam 	if (ep->desc)
269349db4272SNagarjuna Kristam 		tegra_xudc_ep_kick_queue(ep);
269449db4272SNagarjuna Kristam }
269549db4272SNagarjuna Kristam 
269649db4272SNagarjuna Kristam static void tegra_xudc_handle_transfer_event(struct tegra_xudc *xudc,
269749db4272SNagarjuna Kristam 					     struct tegra_xudc_trb *event)
269849db4272SNagarjuna Kristam {
269949db4272SNagarjuna Kristam 	unsigned int ep_index = trb_read_endpoint_id(event);
270049db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep = &xudc->ep[ep_index];
270149db4272SNagarjuna Kristam 	struct tegra_xudc_trb *trb;
270249db4272SNagarjuna Kristam 	u16 comp_code;
270349db4272SNagarjuna Kristam 
270449db4272SNagarjuna Kristam 	if (ep_ctx_read_state(ep->context) == EP_STATE_DISABLED) {
270549db4272SNagarjuna Kristam 		dev_warn(xudc->dev, "transfer event on disabled EP %u\n",
270649db4272SNagarjuna Kristam 			 ep_index);
270749db4272SNagarjuna Kristam 		return;
270849db4272SNagarjuna Kristam 	}
270949db4272SNagarjuna Kristam 
271049db4272SNagarjuna Kristam 	/* Update transfer ring dequeue pointer. */
271149db4272SNagarjuna Kristam 	trb = trb_phys_to_virt(ep, trb_read_data_ptr(event));
271249db4272SNagarjuna Kristam 	comp_code = trb_read_cmpl_code(event);
271349db4272SNagarjuna Kristam 	if (comp_code != TRB_CMPL_CODE_BABBLE_DETECTED_ERR) {
271449db4272SNagarjuna Kristam 		ep->deq_ptr = (trb - ep->transfer_ring) + 1;
271549db4272SNagarjuna Kristam 
271649db4272SNagarjuna Kristam 		if (ep->deq_ptr == XUDC_TRANSFER_RING_SIZE - 1)
271749db4272SNagarjuna Kristam 			ep->deq_ptr = 0;
271849db4272SNagarjuna Kristam 		ep->ring_full = false;
271949db4272SNagarjuna Kristam 	}
272049db4272SNagarjuna Kristam 
272149db4272SNagarjuna Kristam 	switch (comp_code) {
272249db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_SUCCESS:
272349db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_SHORT_PACKET:
272449db4272SNagarjuna Kristam 		tegra_xudc_handle_transfer_completion(xudc, ep, event);
272549db4272SNagarjuna Kristam 		break;
272649db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_HOST_REJECTED:
272749db4272SNagarjuna Kristam 		dev_info(xudc->dev, "stream rejected on EP %u\n", ep_index);
272849db4272SNagarjuna Kristam 
272949db4272SNagarjuna Kristam 		ep->stream_rejected = true;
273049db4272SNagarjuna Kristam 		break;
273149db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_PRIME_PIPE_RECEIVED:
273249db4272SNagarjuna Kristam 		dev_info(xudc->dev, "prime pipe received on EP %u\n", ep_index);
273349db4272SNagarjuna Kristam 
273449db4272SNagarjuna Kristam 		if (ep->stream_rejected) {
273549db4272SNagarjuna Kristam 			ep->stream_rejected = false;
273649db4272SNagarjuna Kristam 			/*
273749db4272SNagarjuna Kristam 			 * An EP is stopped when a stream is rejected.  Wait
273849db4272SNagarjuna Kristam 			 * for the EP to report that it is stopped and then
273949db4272SNagarjuna Kristam 			 * un-stop it.
274049db4272SNagarjuna Kristam 			 */
274149db4272SNagarjuna Kristam 			ep_wait_for_stopped(xudc, ep_index);
274249db4272SNagarjuna Kristam 		}
274349db4272SNagarjuna Kristam 		tegra_xudc_ep_ring_doorbell(ep);
274449db4272SNagarjuna Kristam 		break;
274549db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_BABBLE_DETECTED_ERR:
274649db4272SNagarjuna Kristam 		/*
274749db4272SNagarjuna Kristam 		 * Wait for the EP to be stopped so the controller stops
274849db4272SNagarjuna Kristam 		 * processing doorbells.
274949db4272SNagarjuna Kristam 		 */
275049db4272SNagarjuna Kristam 		ep_wait_for_stopped(xudc, ep_index);
275149db4272SNagarjuna Kristam 		ep->enq_ptr = ep->deq_ptr;
275249db4272SNagarjuna Kristam 		tegra_xudc_ep_nuke(ep, -EIO);
2753a74005abSGustavo A. R. Silva 		fallthrough;
275449db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_STREAM_NUMP_ERROR:
275549db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_CTRL_DIR_ERR:
275649db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_INVALID_STREAM_TYPE_ERR:
275749db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_RING_UNDERRUN:
275849db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_RING_OVERRUN:
275949db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_ISOCH_BUFFER_OVERRUN:
276049db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_USB_TRANS_ERR:
276149db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_TRB_ERR:
276249db4272SNagarjuna Kristam 		dev_err(xudc->dev, "completion error %#x on EP %u\n",
276349db4272SNagarjuna Kristam 			comp_code, ep_index);
276449db4272SNagarjuna Kristam 
276549db4272SNagarjuna Kristam 		ep_halt(xudc, ep_index);
276649db4272SNagarjuna Kristam 		break;
276749db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_CTRL_SEQNUM_ERR:
276849db4272SNagarjuna Kristam 		dev_info(xudc->dev, "sequence number error\n");
276949db4272SNagarjuna Kristam 
277049db4272SNagarjuna Kristam 		/*
277149db4272SNagarjuna Kristam 		 * Kill any queued control request and skip to the last
277249db4272SNagarjuna Kristam 		 * setup packet we received.
277349db4272SNagarjuna Kristam 		 */
277449db4272SNagarjuna Kristam 		tegra_xudc_ep_nuke(ep, -EINVAL);
277549db4272SNagarjuna Kristam 		xudc->setup_state = WAIT_FOR_SETUP;
277649db4272SNagarjuna Kristam 		if (!xudc->queued_setup_packet)
277749db4272SNagarjuna Kristam 			break;
277849db4272SNagarjuna Kristam 
277949db4272SNagarjuna Kristam 		tegra_xudc_handle_ep0_setup_packet(xudc,
278049db4272SNagarjuna Kristam 						   &xudc->setup_packet.ctrl_req,
278149db4272SNagarjuna Kristam 						   xudc->setup_packet.seq_num);
278249db4272SNagarjuna Kristam 		xudc->queued_setup_packet = false;
278349db4272SNagarjuna Kristam 		break;
278449db4272SNagarjuna Kristam 	case TRB_CMPL_CODE_STOPPED:
278549db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "stop completion code on EP %u\n",
278649db4272SNagarjuna Kristam 			ep_index);
278749db4272SNagarjuna Kristam 
278849db4272SNagarjuna Kristam 		/* Disconnected. */
278949db4272SNagarjuna Kristam 		tegra_xudc_ep_nuke(ep, -ECONNREFUSED);
279049db4272SNagarjuna Kristam 		break;
279149db4272SNagarjuna Kristam 	default:
279249db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "completion event %#x on EP %u\n",
279349db4272SNagarjuna Kristam 			comp_code, ep_index);
279449db4272SNagarjuna Kristam 		break;
279549db4272SNagarjuna Kristam 	}
279649db4272SNagarjuna Kristam }
279749db4272SNagarjuna Kristam 
279849db4272SNagarjuna Kristam static void tegra_xudc_reset(struct tegra_xudc *xudc)
279949db4272SNagarjuna Kristam {
280049db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep0 = &xudc->ep[0];
280149db4272SNagarjuna Kristam 	dma_addr_t deq_ptr;
280249db4272SNagarjuna Kristam 	unsigned int i;
280349db4272SNagarjuna Kristam 
280449db4272SNagarjuna Kristam 	xudc->setup_state = WAIT_FOR_SETUP;
280549db4272SNagarjuna Kristam 	xudc->device_state = USB_STATE_DEFAULT;
280649db4272SNagarjuna Kristam 	usb_gadget_set_state(&xudc->gadget, xudc->device_state);
280749db4272SNagarjuna Kristam 
280849db4272SNagarjuna Kristam 	ep_unpause_all(xudc);
280949db4272SNagarjuna Kristam 
281049db4272SNagarjuna Kristam 	for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
281149db4272SNagarjuna Kristam 		tegra_xudc_ep_nuke(&xudc->ep[i], -ESHUTDOWN);
281249db4272SNagarjuna Kristam 
281349db4272SNagarjuna Kristam 	/*
281449db4272SNagarjuna Kristam 	 * Reset sequence number and dequeue pointer to flush the transfer
281549db4272SNagarjuna Kristam 	 * ring.
281649db4272SNagarjuna Kristam 	 */
281749db4272SNagarjuna Kristam 	ep0->deq_ptr = ep0->enq_ptr;
281849db4272SNagarjuna Kristam 	ep0->ring_full = false;
281949db4272SNagarjuna Kristam 
282049db4272SNagarjuna Kristam 	xudc->setup_seq_num = 0;
282149db4272SNagarjuna Kristam 	xudc->queued_setup_packet = false;
282249db4272SNagarjuna Kristam 
28237bd42fb9SWayne Chang 	ep_ctx_write_rsvd(ep0->context, 0);
28247bd42fb9SWayne Chang 	ep_ctx_write_partial_td(ep0->context, 0);
28257bd42fb9SWayne Chang 	ep_ctx_write_splitxstate(ep0->context, 0);
28267bd42fb9SWayne Chang 	ep_ctx_write_seq_num(ep0->context, 0);
282749db4272SNagarjuna Kristam 
282849db4272SNagarjuna Kristam 	deq_ptr = trb_virt_to_phys(ep0, &ep0->transfer_ring[ep0->deq_ptr]);
282949db4272SNagarjuna Kristam 
283049db4272SNagarjuna Kristam 	if (!dma_mapping_error(xudc->dev, deq_ptr)) {
283149db4272SNagarjuna Kristam 		ep_ctx_write_deq_ptr(ep0->context, deq_ptr);
283249db4272SNagarjuna Kristam 		ep_ctx_write_dcs(ep0->context, ep0->pcs);
283349db4272SNagarjuna Kristam 	}
283449db4272SNagarjuna Kristam 
283549db4272SNagarjuna Kristam 	ep_unhalt_all(xudc);
283649db4272SNagarjuna Kristam 	ep_reload(xudc, 0);
283749db4272SNagarjuna Kristam 	ep_unpause(xudc, 0);
283849db4272SNagarjuna Kristam }
283949db4272SNagarjuna Kristam 
284049db4272SNagarjuna Kristam static void tegra_xudc_port_connect(struct tegra_xudc *xudc)
284149db4272SNagarjuna Kristam {
284249db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep0 = &xudc->ep[0];
284349db4272SNagarjuna Kristam 	u16 maxpacket;
284449db4272SNagarjuna Kristam 	u32 val;
284549db4272SNagarjuna Kristam 
284649db4272SNagarjuna Kristam 	val = (xudc_readl(xudc, PORTSC) & PORTSC_PS_MASK) >> PORTSC_PS_SHIFT;
284749db4272SNagarjuna Kristam 	switch (val) {
284849db4272SNagarjuna Kristam 	case PORTSC_PS_LS:
284949db4272SNagarjuna Kristam 		xudc->gadget.speed = USB_SPEED_LOW;
285049db4272SNagarjuna Kristam 		break;
285149db4272SNagarjuna Kristam 	case PORTSC_PS_FS:
285249db4272SNagarjuna Kristam 		xudc->gadget.speed = USB_SPEED_FULL;
285349db4272SNagarjuna Kristam 		break;
285449db4272SNagarjuna Kristam 	case PORTSC_PS_HS:
285549db4272SNagarjuna Kristam 		xudc->gadget.speed = USB_SPEED_HIGH;
285649db4272SNagarjuna Kristam 		break;
285749db4272SNagarjuna Kristam 	case PORTSC_PS_SS:
285849db4272SNagarjuna Kristam 		xudc->gadget.speed = USB_SPEED_SUPER;
285949db4272SNagarjuna Kristam 		break;
286049db4272SNagarjuna Kristam 	default:
286149db4272SNagarjuna Kristam 		xudc->gadget.speed = USB_SPEED_UNKNOWN;
286249db4272SNagarjuna Kristam 		break;
286349db4272SNagarjuna Kristam 	}
286449db4272SNagarjuna Kristam 
286549db4272SNagarjuna Kristam 	xudc->device_state = USB_STATE_DEFAULT;
286649db4272SNagarjuna Kristam 	usb_gadget_set_state(&xudc->gadget, xudc->device_state);
286749db4272SNagarjuna Kristam 
286849db4272SNagarjuna Kristam 	xudc->setup_state = WAIT_FOR_SETUP;
286949db4272SNagarjuna Kristam 
287049db4272SNagarjuna Kristam 	if (xudc->gadget.speed == USB_SPEED_SUPER)
287149db4272SNagarjuna Kristam 		maxpacket = 512;
287249db4272SNagarjuna Kristam 	else
287349db4272SNagarjuna Kristam 		maxpacket = 64;
287449db4272SNagarjuna Kristam 
287549db4272SNagarjuna Kristam 	ep_ctx_write_max_packet_size(ep0->context, maxpacket);
287649db4272SNagarjuna Kristam 	tegra_xudc_ep0_desc.wMaxPacketSize = cpu_to_le16(maxpacket);
287749db4272SNagarjuna Kristam 	usb_ep_set_maxpacket_limit(&ep0->usb_ep, maxpacket);
287849db4272SNagarjuna Kristam 
287949db4272SNagarjuna Kristam 	if (!xudc->soc->u1_enable) {
288049db4272SNagarjuna Kristam 		val = xudc_readl(xudc, PORTPM);
288149db4272SNagarjuna Kristam 		val &= ~(PORTPM_U1TIMEOUT_MASK);
288249db4272SNagarjuna Kristam 		xudc_writel(xudc, val, PORTPM);
288349db4272SNagarjuna Kristam 	}
288449db4272SNagarjuna Kristam 
288549db4272SNagarjuna Kristam 	if (!xudc->soc->u2_enable) {
288649db4272SNagarjuna Kristam 		val = xudc_readl(xudc, PORTPM);
288749db4272SNagarjuna Kristam 		val &= ~(PORTPM_U2TIMEOUT_MASK);
288849db4272SNagarjuna Kristam 		xudc_writel(xudc, val, PORTPM);
288949db4272SNagarjuna Kristam 	}
289049db4272SNagarjuna Kristam 
289149db4272SNagarjuna Kristam 	if (xudc->gadget.speed <= USB_SPEED_HIGH) {
289249db4272SNagarjuna Kristam 		val = xudc_readl(xudc, PORTPM);
289349db4272SNagarjuna Kristam 		val &= ~(PORTPM_L1S_MASK);
289449db4272SNagarjuna Kristam 		if (xudc->soc->lpm_enable)
289549db4272SNagarjuna Kristam 			val |= PORTPM_L1S(PORTPM_L1S_ACCEPT);
289649db4272SNagarjuna Kristam 		else
289749db4272SNagarjuna Kristam 			val |= PORTPM_L1S(PORTPM_L1S_NYET);
289849db4272SNagarjuna Kristam 		xudc_writel(xudc, val, PORTPM);
289949db4272SNagarjuna Kristam 	}
290049db4272SNagarjuna Kristam 
290149db4272SNagarjuna Kristam 	val = xudc_readl(xudc, ST);
290249db4272SNagarjuna Kristam 	if (val & ST_RC)
290349db4272SNagarjuna Kristam 		xudc_writel(xudc, ST_RC, ST);
290449db4272SNagarjuna Kristam }
290549db4272SNagarjuna Kristam 
290649db4272SNagarjuna Kristam static void tegra_xudc_port_disconnect(struct tegra_xudc *xudc)
290749db4272SNagarjuna Kristam {
290849db4272SNagarjuna Kristam 	tegra_xudc_reset(xudc);
290949db4272SNagarjuna Kristam 
291049db4272SNagarjuna Kristam 	if (xudc->driver && xudc->driver->disconnect) {
291149db4272SNagarjuna Kristam 		spin_unlock(&xudc->lock);
291249db4272SNagarjuna Kristam 		xudc->driver->disconnect(&xudc->gadget);
291349db4272SNagarjuna Kristam 		spin_lock(&xudc->lock);
291449db4272SNagarjuna Kristam 	}
291549db4272SNagarjuna Kristam 
291649db4272SNagarjuna Kristam 	xudc->device_state = USB_STATE_NOTATTACHED;
291749db4272SNagarjuna Kristam 	usb_gadget_set_state(&xudc->gadget, xudc->device_state);
291849db4272SNagarjuna Kristam 
291949db4272SNagarjuna Kristam 	complete(&xudc->disconnect_complete);
292049db4272SNagarjuna Kristam }
292149db4272SNagarjuna Kristam 
292249db4272SNagarjuna Kristam static void tegra_xudc_port_reset(struct tegra_xudc *xudc)
292349db4272SNagarjuna Kristam {
292449db4272SNagarjuna Kristam 	tegra_xudc_reset(xudc);
292549db4272SNagarjuna Kristam 
292649db4272SNagarjuna Kristam 	if (xudc->driver) {
292749db4272SNagarjuna Kristam 		spin_unlock(&xudc->lock);
292849db4272SNagarjuna Kristam 		usb_gadget_udc_reset(&xudc->gadget, xudc->driver);
292949db4272SNagarjuna Kristam 		spin_lock(&xudc->lock);
293049db4272SNagarjuna Kristam 	}
293149db4272SNagarjuna Kristam 
293249db4272SNagarjuna Kristam 	tegra_xudc_port_connect(xudc);
293349db4272SNagarjuna Kristam }
293449db4272SNagarjuna Kristam 
293549db4272SNagarjuna Kristam static void tegra_xudc_port_suspend(struct tegra_xudc *xudc)
293649db4272SNagarjuna Kristam {
293749db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "port suspend\n");
293849db4272SNagarjuna Kristam 
293949db4272SNagarjuna Kristam 	xudc->resume_state = xudc->device_state;
294049db4272SNagarjuna Kristam 	xudc->device_state = USB_STATE_SUSPENDED;
294149db4272SNagarjuna Kristam 	usb_gadget_set_state(&xudc->gadget, xudc->device_state);
294249db4272SNagarjuna Kristam 
294349db4272SNagarjuna Kristam 	if (xudc->driver->suspend) {
294449db4272SNagarjuna Kristam 		spin_unlock(&xudc->lock);
294549db4272SNagarjuna Kristam 		xudc->driver->suspend(&xudc->gadget);
294649db4272SNagarjuna Kristam 		spin_lock(&xudc->lock);
294749db4272SNagarjuna Kristam 	}
294849db4272SNagarjuna Kristam }
294949db4272SNagarjuna Kristam 
295049db4272SNagarjuna Kristam static void tegra_xudc_port_resume(struct tegra_xudc *xudc)
295149db4272SNagarjuna Kristam {
295249db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "port resume\n");
295349db4272SNagarjuna Kristam 
295449db4272SNagarjuna Kristam 	tegra_xudc_resume_device_state(xudc);
295549db4272SNagarjuna Kristam 
295649db4272SNagarjuna Kristam 	if (xudc->driver->resume) {
295749db4272SNagarjuna Kristam 		spin_unlock(&xudc->lock);
295849db4272SNagarjuna Kristam 		xudc->driver->resume(&xudc->gadget);
295949db4272SNagarjuna Kristam 		spin_lock(&xudc->lock);
296049db4272SNagarjuna Kristam 	}
296149db4272SNagarjuna Kristam }
296249db4272SNagarjuna Kristam 
296349db4272SNagarjuna Kristam static inline void clear_port_change(struct tegra_xudc *xudc, u32 flag)
296449db4272SNagarjuna Kristam {
296549db4272SNagarjuna Kristam 	u32 val;
296649db4272SNagarjuna Kristam 
296749db4272SNagarjuna Kristam 	val = xudc_readl(xudc, PORTSC);
296849db4272SNagarjuna Kristam 	val &= ~PORTSC_CHANGE_MASK;
296949db4272SNagarjuna Kristam 	val |= flag;
297049db4272SNagarjuna Kristam 	xudc_writel(xudc, val, PORTSC);
297149db4272SNagarjuna Kristam }
297249db4272SNagarjuna Kristam 
297349db4272SNagarjuna Kristam static void __tegra_xudc_handle_port_status(struct tegra_xudc *xudc)
297449db4272SNagarjuna Kristam {
297549db4272SNagarjuna Kristam 	u32 portsc, porthalt;
297649db4272SNagarjuna Kristam 
297749db4272SNagarjuna Kristam 	porthalt = xudc_readl(xudc, PORTHALT);
297849db4272SNagarjuna Kristam 	if ((porthalt & PORTHALT_STCHG_REQ) &&
297949db4272SNagarjuna Kristam 	    (porthalt & PORTHALT_HALT_LTSSM)) {
298049db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "STCHG_REQ, PORTHALT = %#x\n", porthalt);
298149db4272SNagarjuna Kristam 		porthalt &= ~PORTHALT_HALT_LTSSM;
298249db4272SNagarjuna Kristam 		xudc_writel(xudc, porthalt, PORTHALT);
298349db4272SNagarjuna Kristam 	}
298449db4272SNagarjuna Kristam 
298549db4272SNagarjuna Kristam 	portsc = xudc_readl(xudc, PORTSC);
298649db4272SNagarjuna Kristam 	if ((portsc & PORTSC_PRC) && (portsc & PORTSC_PR)) {
298749db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "PRC, PR, PORTSC = %#x\n", portsc);
298849db4272SNagarjuna Kristam 		clear_port_change(xudc, PORTSC_PRC | PORTSC_PED);
298949db4272SNagarjuna Kristam #define TOGGLE_VBUS_WAIT_MS 100
299049db4272SNagarjuna Kristam 		if (xudc->soc->port_reset_quirk) {
299149db4272SNagarjuna Kristam 			schedule_delayed_work(&xudc->port_reset_war_work,
299249db4272SNagarjuna Kristam 				msecs_to_jiffies(TOGGLE_VBUS_WAIT_MS));
299349db4272SNagarjuna Kristam 			xudc->wait_for_sec_prc = 1;
299449db4272SNagarjuna Kristam 		}
299549db4272SNagarjuna Kristam 	}
299649db4272SNagarjuna Kristam 
299749db4272SNagarjuna Kristam 	if ((portsc & PORTSC_PRC) && !(portsc & PORTSC_PR)) {
299849db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "PRC, Not PR, PORTSC = %#x\n", portsc);
299949db4272SNagarjuna Kristam 		clear_port_change(xudc, PORTSC_PRC | PORTSC_PED);
300049db4272SNagarjuna Kristam 		tegra_xudc_port_reset(xudc);
300149db4272SNagarjuna Kristam 		cancel_delayed_work(&xudc->port_reset_war_work);
300249db4272SNagarjuna Kristam 		xudc->wait_for_sec_prc = 0;
300349db4272SNagarjuna Kristam 	}
300449db4272SNagarjuna Kristam 
300549db4272SNagarjuna Kristam 	portsc = xudc_readl(xudc, PORTSC);
300649db4272SNagarjuna Kristam 	if (portsc & PORTSC_WRC) {
300749db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "WRC, PORTSC = %#x\n", portsc);
300849db4272SNagarjuna Kristam 		clear_port_change(xudc, PORTSC_WRC | PORTSC_PED);
300949db4272SNagarjuna Kristam 		if (!(xudc_readl(xudc, PORTSC) & PORTSC_WPR))
301049db4272SNagarjuna Kristam 			tegra_xudc_port_reset(xudc);
301149db4272SNagarjuna Kristam 	}
301249db4272SNagarjuna Kristam 
301349db4272SNagarjuna Kristam 	portsc = xudc_readl(xudc, PORTSC);
301449db4272SNagarjuna Kristam 	if (portsc & PORTSC_CSC) {
301549db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "CSC, PORTSC = %#x\n", portsc);
301649db4272SNagarjuna Kristam 		clear_port_change(xudc, PORTSC_CSC);
301749db4272SNagarjuna Kristam 
301849db4272SNagarjuna Kristam 		if (portsc & PORTSC_CCS)
301949db4272SNagarjuna Kristam 			tegra_xudc_port_connect(xudc);
302049db4272SNagarjuna Kristam 		else
302149db4272SNagarjuna Kristam 			tegra_xudc_port_disconnect(xudc);
302249db4272SNagarjuna Kristam 
302349db4272SNagarjuna Kristam 		if (xudc->wait_csc) {
302449db4272SNagarjuna Kristam 			cancel_delayed_work(&xudc->plc_reset_work);
302549db4272SNagarjuna Kristam 			xudc->wait_csc = false;
302649db4272SNagarjuna Kristam 		}
302749db4272SNagarjuna Kristam 	}
302849db4272SNagarjuna Kristam 
302949db4272SNagarjuna Kristam 	portsc = xudc_readl(xudc, PORTSC);
303049db4272SNagarjuna Kristam 	if (portsc & PORTSC_PLC) {
303149db4272SNagarjuna Kristam 		u32 pls = (portsc & PORTSC_PLS_MASK) >> PORTSC_PLS_SHIFT;
303249db4272SNagarjuna Kristam 
303349db4272SNagarjuna Kristam 		dev_dbg(xudc->dev, "PLC, PORTSC = %#x\n", portsc);
303449db4272SNagarjuna Kristam 		clear_port_change(xudc, PORTSC_PLC);
303549db4272SNagarjuna Kristam 		switch (pls) {
303649db4272SNagarjuna Kristam 		case PORTSC_PLS_U3:
303749db4272SNagarjuna Kristam 			tegra_xudc_port_suspend(xudc);
303849db4272SNagarjuna Kristam 			break;
303949db4272SNagarjuna Kristam 		case PORTSC_PLS_U0:
304049db4272SNagarjuna Kristam 			if (xudc->gadget.speed < USB_SPEED_SUPER)
304149db4272SNagarjuna Kristam 				tegra_xudc_port_resume(xudc);
304249db4272SNagarjuna Kristam 			break;
304349db4272SNagarjuna Kristam 		case PORTSC_PLS_RESUME:
304449db4272SNagarjuna Kristam 			if (xudc->gadget.speed == USB_SPEED_SUPER)
304549db4272SNagarjuna Kristam 				tegra_xudc_port_resume(xudc);
304649db4272SNagarjuna Kristam 			break;
304749db4272SNagarjuna Kristam 		case PORTSC_PLS_INACTIVE:
304849db4272SNagarjuna Kristam 			schedule_delayed_work(&xudc->plc_reset_work,
304949db4272SNagarjuna Kristam 					msecs_to_jiffies(TOGGLE_VBUS_WAIT_MS));
305049db4272SNagarjuna Kristam 			xudc->wait_csc = true;
305149db4272SNagarjuna Kristam 			break;
305249db4272SNagarjuna Kristam 		default:
305349db4272SNagarjuna Kristam 			break;
305449db4272SNagarjuna Kristam 		}
305549db4272SNagarjuna Kristam 	}
305649db4272SNagarjuna Kristam 
305749db4272SNagarjuna Kristam 	if (portsc & PORTSC_CEC) {
305849db4272SNagarjuna Kristam 		dev_warn(xudc->dev, "CEC, PORTSC = %#x\n", portsc);
305949db4272SNagarjuna Kristam 		clear_port_change(xudc, PORTSC_CEC);
306049db4272SNagarjuna Kristam 	}
306149db4272SNagarjuna Kristam 
306249db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "PORTSC = %#x\n", xudc_readl(xudc, PORTSC));
306349db4272SNagarjuna Kristam }
306449db4272SNagarjuna Kristam 
306549db4272SNagarjuna Kristam static void tegra_xudc_handle_port_status(struct tegra_xudc *xudc)
306649db4272SNagarjuna Kristam {
306749db4272SNagarjuna Kristam 	while ((xudc_readl(xudc, PORTSC) & PORTSC_CHANGE_MASK) ||
306849db4272SNagarjuna Kristam 	       (xudc_readl(xudc, PORTHALT) & PORTHALT_STCHG_REQ))
306949db4272SNagarjuna Kristam 		__tegra_xudc_handle_port_status(xudc);
307049db4272SNagarjuna Kristam }
307149db4272SNagarjuna Kristam 
307249db4272SNagarjuna Kristam static void tegra_xudc_handle_event(struct tegra_xudc *xudc,
307349db4272SNagarjuna Kristam 				    struct tegra_xudc_trb *event)
307449db4272SNagarjuna Kristam {
307549db4272SNagarjuna Kristam 	u32 type = trb_read_type(event);
307649db4272SNagarjuna Kristam 
307749db4272SNagarjuna Kristam 	dump_trb(xudc, "EVENT", event);
307849db4272SNagarjuna Kristam 
307949db4272SNagarjuna Kristam 	switch (type) {
308049db4272SNagarjuna Kristam 	case TRB_TYPE_PORT_STATUS_CHANGE_EVENT:
308149db4272SNagarjuna Kristam 		tegra_xudc_handle_port_status(xudc);
308249db4272SNagarjuna Kristam 		break;
308349db4272SNagarjuna Kristam 	case TRB_TYPE_TRANSFER_EVENT:
308449db4272SNagarjuna Kristam 		tegra_xudc_handle_transfer_event(xudc, event);
308549db4272SNagarjuna Kristam 		break;
308649db4272SNagarjuna Kristam 	case TRB_TYPE_SETUP_PACKET_EVENT:
308749db4272SNagarjuna Kristam 		tegra_xudc_handle_ep0_event(xudc, event);
308849db4272SNagarjuna Kristam 		break;
308949db4272SNagarjuna Kristam 	default:
309049db4272SNagarjuna Kristam 		dev_info(xudc->dev, "Unrecognized TRB type = %#x\n", type);
309149db4272SNagarjuna Kristam 		break;
309249db4272SNagarjuna Kristam 	}
309349db4272SNagarjuna Kristam }
309449db4272SNagarjuna Kristam 
309549db4272SNagarjuna Kristam static void tegra_xudc_process_event_ring(struct tegra_xudc *xudc)
309649db4272SNagarjuna Kristam {
309749db4272SNagarjuna Kristam 	struct tegra_xudc_trb *event;
309849db4272SNagarjuna Kristam 	dma_addr_t erdp;
309949db4272SNagarjuna Kristam 
310049db4272SNagarjuna Kristam 	while (true) {
310149db4272SNagarjuna Kristam 		event = xudc->event_ring[xudc->event_ring_index] +
310249db4272SNagarjuna Kristam 			xudc->event_ring_deq_ptr;
310349db4272SNagarjuna Kristam 
310449db4272SNagarjuna Kristam 		if (trb_read_cycle(event) != xudc->ccs)
310549db4272SNagarjuna Kristam 			break;
310649db4272SNagarjuna Kristam 
310749db4272SNagarjuna Kristam 		tegra_xudc_handle_event(xudc, event);
310849db4272SNagarjuna Kristam 
310949db4272SNagarjuna Kristam 		xudc->event_ring_deq_ptr++;
311049db4272SNagarjuna Kristam 		if (xudc->event_ring_deq_ptr == XUDC_EVENT_RING_SIZE) {
311149db4272SNagarjuna Kristam 			xudc->event_ring_deq_ptr = 0;
311249db4272SNagarjuna Kristam 			xudc->event_ring_index++;
311349db4272SNagarjuna Kristam 		}
311449db4272SNagarjuna Kristam 
311549db4272SNagarjuna Kristam 		if (xudc->event_ring_index == XUDC_NR_EVENT_RINGS) {
311649db4272SNagarjuna Kristam 			xudc->event_ring_index = 0;
311749db4272SNagarjuna Kristam 			xudc->ccs = !xudc->ccs;
311849db4272SNagarjuna Kristam 		}
311949db4272SNagarjuna Kristam 	}
312049db4272SNagarjuna Kristam 
312149db4272SNagarjuna Kristam 	erdp = xudc->event_ring_phys[xudc->event_ring_index] +
312249db4272SNagarjuna Kristam 		xudc->event_ring_deq_ptr * sizeof(*event);
312349db4272SNagarjuna Kristam 
312449db4272SNagarjuna Kristam 	xudc_writel(xudc, upper_32_bits(erdp), ERDPHI);
312549db4272SNagarjuna Kristam 	xudc_writel(xudc, lower_32_bits(erdp) | ERDPLO_EHB, ERDPLO);
312649db4272SNagarjuna Kristam }
312749db4272SNagarjuna Kristam 
312849db4272SNagarjuna Kristam static irqreturn_t tegra_xudc_irq(int irq, void *data)
312949db4272SNagarjuna Kristam {
313049db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = data;
313149db4272SNagarjuna Kristam 	unsigned long flags;
313249db4272SNagarjuna Kristam 	u32 val;
313349db4272SNagarjuna Kristam 
313449db4272SNagarjuna Kristam 	val = xudc_readl(xudc, ST);
313549db4272SNagarjuna Kristam 	if (!(val & ST_IP))
313649db4272SNagarjuna Kristam 		return IRQ_NONE;
313749db4272SNagarjuna Kristam 	xudc_writel(xudc, ST_IP, ST);
313849db4272SNagarjuna Kristam 
313949db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
314049db4272SNagarjuna Kristam 	tegra_xudc_process_event_ring(xudc);
314149db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
314249db4272SNagarjuna Kristam 
314349db4272SNagarjuna Kristam 	return IRQ_HANDLED;
314449db4272SNagarjuna Kristam }
314549db4272SNagarjuna Kristam 
314649db4272SNagarjuna Kristam static int tegra_xudc_alloc_ep(struct tegra_xudc *xudc, unsigned int index)
314749db4272SNagarjuna Kristam {
314849db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep = &xudc->ep[index];
314949db4272SNagarjuna Kristam 
315049db4272SNagarjuna Kristam 	ep->xudc = xudc;
315149db4272SNagarjuna Kristam 	ep->index = index;
315249db4272SNagarjuna Kristam 	ep->context = &xudc->ep_context[index];
315349db4272SNagarjuna Kristam 	INIT_LIST_HEAD(&ep->queue);
315449db4272SNagarjuna Kristam 
315549db4272SNagarjuna Kristam 	/*
315649db4272SNagarjuna Kristam 	 * EP1 would be the input endpoint corresponding to EP0, but since
315749db4272SNagarjuna Kristam 	 * EP0 is bi-directional, EP1 is unused.
315849db4272SNagarjuna Kristam 	 */
315949db4272SNagarjuna Kristam 	if (index == 1)
316049db4272SNagarjuna Kristam 		return 0;
316149db4272SNagarjuna Kristam 
316249db4272SNagarjuna Kristam 	ep->transfer_ring = dma_pool_alloc(xudc->transfer_ring_pool,
316349db4272SNagarjuna Kristam 					   GFP_KERNEL,
316449db4272SNagarjuna Kristam 					   &ep->transfer_ring_phys);
316549db4272SNagarjuna Kristam 	if (!ep->transfer_ring)
316649db4272SNagarjuna Kristam 		return -ENOMEM;
316749db4272SNagarjuna Kristam 
316849db4272SNagarjuna Kristam 	if (index) {
316949db4272SNagarjuna Kristam 		snprintf(ep->name, sizeof(ep->name), "ep%u%s", index / 2,
317049db4272SNagarjuna Kristam 			 (index % 2 == 0) ? "out" : "in");
317149db4272SNagarjuna Kristam 		ep->usb_ep.name = ep->name;
317249db4272SNagarjuna Kristam 		usb_ep_set_maxpacket_limit(&ep->usb_ep, 1024);
317349db4272SNagarjuna Kristam 		ep->usb_ep.max_streams = 16;
317449db4272SNagarjuna Kristam 		ep->usb_ep.ops = &tegra_xudc_ep_ops;
317549db4272SNagarjuna Kristam 		ep->usb_ep.caps.type_bulk = true;
317649db4272SNagarjuna Kristam 		ep->usb_ep.caps.type_int = true;
317749db4272SNagarjuna Kristam 		if (index & 1)
317849db4272SNagarjuna Kristam 			ep->usb_ep.caps.dir_in = true;
317949db4272SNagarjuna Kristam 		else
318049db4272SNagarjuna Kristam 			ep->usb_ep.caps.dir_out = true;
318149db4272SNagarjuna Kristam 		list_add_tail(&ep->usb_ep.ep_list, &xudc->gadget.ep_list);
318249db4272SNagarjuna Kristam 	} else {
318349db4272SNagarjuna Kristam 		strscpy(ep->name, "ep0", 3);
318449db4272SNagarjuna Kristam 		ep->usb_ep.name = ep->name;
318549db4272SNagarjuna Kristam 		usb_ep_set_maxpacket_limit(&ep->usb_ep, 512);
318649db4272SNagarjuna Kristam 		ep->usb_ep.ops = &tegra_xudc_ep0_ops;
318749db4272SNagarjuna Kristam 		ep->usb_ep.caps.type_control = true;
318849db4272SNagarjuna Kristam 		ep->usb_ep.caps.dir_in = true;
318949db4272SNagarjuna Kristam 		ep->usb_ep.caps.dir_out = true;
319049db4272SNagarjuna Kristam 	}
319149db4272SNagarjuna Kristam 
319249db4272SNagarjuna Kristam 	return 0;
319349db4272SNagarjuna Kristam }
319449db4272SNagarjuna Kristam 
319549db4272SNagarjuna Kristam static void tegra_xudc_free_ep(struct tegra_xudc *xudc, unsigned int index)
319649db4272SNagarjuna Kristam {
319749db4272SNagarjuna Kristam 	struct tegra_xudc_ep *ep = &xudc->ep[index];
319849db4272SNagarjuna Kristam 
319949db4272SNagarjuna Kristam 	/*
320049db4272SNagarjuna Kristam 	 * EP1 would be the input endpoint corresponding to EP0, but since
320149db4272SNagarjuna Kristam 	 * EP0 is bi-directional, EP1 is unused.
320249db4272SNagarjuna Kristam 	 */
320349db4272SNagarjuna Kristam 	if (index == 1)
320449db4272SNagarjuna Kristam 		return;
320549db4272SNagarjuna Kristam 
320649db4272SNagarjuna Kristam 	dma_pool_free(xudc->transfer_ring_pool, ep->transfer_ring,
320749db4272SNagarjuna Kristam 		      ep->transfer_ring_phys);
320849db4272SNagarjuna Kristam }
320949db4272SNagarjuna Kristam 
321049db4272SNagarjuna Kristam static int tegra_xudc_alloc_eps(struct tegra_xudc *xudc)
321149db4272SNagarjuna Kristam {
321249db4272SNagarjuna Kristam 	struct usb_request *req;
321349db4272SNagarjuna Kristam 	unsigned int i;
321449db4272SNagarjuna Kristam 	int err;
321549db4272SNagarjuna Kristam 
321649db4272SNagarjuna Kristam 	xudc->ep_context =
321749db4272SNagarjuna Kristam 		dma_alloc_coherent(xudc->dev, XUDC_NR_EPS *
321849db4272SNagarjuna Kristam 				    sizeof(*xudc->ep_context),
321949db4272SNagarjuna Kristam 				    &xudc->ep_context_phys, GFP_KERNEL);
322049db4272SNagarjuna Kristam 	if (!xudc->ep_context)
322149db4272SNagarjuna Kristam 		return -ENOMEM;
322249db4272SNagarjuna Kristam 
322349db4272SNagarjuna Kristam 	xudc->transfer_ring_pool =
322449db4272SNagarjuna Kristam 		dmam_pool_create(dev_name(xudc->dev), xudc->dev,
322549db4272SNagarjuna Kristam 				 XUDC_TRANSFER_RING_SIZE *
322649db4272SNagarjuna Kristam 				 sizeof(struct tegra_xudc_trb),
322749db4272SNagarjuna Kristam 				 sizeof(struct tegra_xudc_trb), 0);
322849db4272SNagarjuna Kristam 	if (!xudc->transfer_ring_pool) {
322949db4272SNagarjuna Kristam 		err = -ENOMEM;
323049db4272SNagarjuna Kristam 		goto free_ep_context;
323149db4272SNagarjuna Kristam 	}
323249db4272SNagarjuna Kristam 
323349db4272SNagarjuna Kristam 	INIT_LIST_HEAD(&xudc->gadget.ep_list);
323449db4272SNagarjuna Kristam 	for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
323549db4272SNagarjuna Kristam 		err = tegra_xudc_alloc_ep(xudc, i);
323649db4272SNagarjuna Kristam 		if (err < 0)
323749db4272SNagarjuna Kristam 			goto free_eps;
323849db4272SNagarjuna Kristam 	}
323949db4272SNagarjuna Kristam 
324049db4272SNagarjuna Kristam 	req = tegra_xudc_ep_alloc_request(&xudc->ep[0].usb_ep, GFP_KERNEL);
324149db4272SNagarjuna Kristam 	if (!req) {
324249db4272SNagarjuna Kristam 		err = -ENOMEM;
324349db4272SNagarjuna Kristam 		goto free_eps;
324449db4272SNagarjuna Kristam 	}
324549db4272SNagarjuna Kristam 	xudc->ep0_req = to_xudc_req(req);
324649db4272SNagarjuna Kristam 
324749db4272SNagarjuna Kristam 	return 0;
324849db4272SNagarjuna Kristam 
324949db4272SNagarjuna Kristam free_eps:
325049db4272SNagarjuna Kristam 	for (; i > 0; i--)
325149db4272SNagarjuna Kristam 		tegra_xudc_free_ep(xudc, i - 1);
325249db4272SNagarjuna Kristam free_ep_context:
325349db4272SNagarjuna Kristam 	dma_free_coherent(xudc->dev, XUDC_NR_EPS * sizeof(*xudc->ep_context),
325449db4272SNagarjuna Kristam 			  xudc->ep_context, xudc->ep_context_phys);
325549db4272SNagarjuna Kristam 	return err;
325649db4272SNagarjuna Kristam }
325749db4272SNagarjuna Kristam 
325849db4272SNagarjuna Kristam static void tegra_xudc_init_eps(struct tegra_xudc *xudc)
325949db4272SNagarjuna Kristam {
326049db4272SNagarjuna Kristam 	xudc_writel(xudc, lower_32_bits(xudc->ep_context_phys), ECPLO);
326149db4272SNagarjuna Kristam 	xudc_writel(xudc, upper_32_bits(xudc->ep_context_phys), ECPHI);
326249db4272SNagarjuna Kristam }
326349db4272SNagarjuna Kristam 
326449db4272SNagarjuna Kristam static void tegra_xudc_free_eps(struct tegra_xudc *xudc)
326549db4272SNagarjuna Kristam {
326649db4272SNagarjuna Kristam 	unsigned int i;
326749db4272SNagarjuna Kristam 
326849db4272SNagarjuna Kristam 	tegra_xudc_ep_free_request(&xudc->ep[0].usb_ep,
326949db4272SNagarjuna Kristam 				   &xudc->ep0_req->usb_req);
327049db4272SNagarjuna Kristam 
327149db4272SNagarjuna Kristam 	for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
327249db4272SNagarjuna Kristam 		tegra_xudc_free_ep(xudc, i);
327349db4272SNagarjuna Kristam 
327449db4272SNagarjuna Kristam 	dma_free_coherent(xudc->dev, XUDC_NR_EPS * sizeof(*xudc->ep_context),
327549db4272SNagarjuna Kristam 			  xudc->ep_context, xudc->ep_context_phys);
327649db4272SNagarjuna Kristam }
327749db4272SNagarjuna Kristam 
327849db4272SNagarjuna Kristam static int tegra_xudc_alloc_event_ring(struct tegra_xudc *xudc)
327949db4272SNagarjuna Kristam {
328049db4272SNagarjuna Kristam 	unsigned int i;
328149db4272SNagarjuna Kristam 
328249db4272SNagarjuna Kristam 	for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
328349db4272SNagarjuna Kristam 		xudc->event_ring[i] =
328449db4272SNagarjuna Kristam 			dma_alloc_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
328549db4272SNagarjuna Kristam 					   sizeof(*xudc->event_ring[i]),
328649db4272SNagarjuna Kristam 					   &xudc->event_ring_phys[i],
328749db4272SNagarjuna Kristam 					   GFP_KERNEL);
328849db4272SNagarjuna Kristam 		if (!xudc->event_ring[i])
328949db4272SNagarjuna Kristam 			goto free_dma;
329049db4272SNagarjuna Kristam 	}
329149db4272SNagarjuna Kristam 
329249db4272SNagarjuna Kristam 	return 0;
329349db4272SNagarjuna Kristam 
329449db4272SNagarjuna Kristam free_dma:
329549db4272SNagarjuna Kristam 	for (; i > 0; i--) {
329649db4272SNagarjuna Kristam 		dma_free_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
329749db4272SNagarjuna Kristam 				  sizeof(*xudc->event_ring[i - 1]),
329849db4272SNagarjuna Kristam 				  xudc->event_ring[i - 1],
329949db4272SNagarjuna Kristam 				  xudc->event_ring_phys[i - 1]);
330049db4272SNagarjuna Kristam 	}
330149db4272SNagarjuna Kristam 	return -ENOMEM;
330249db4272SNagarjuna Kristam }
330349db4272SNagarjuna Kristam 
330449db4272SNagarjuna Kristam static void tegra_xudc_init_event_ring(struct tegra_xudc *xudc)
330549db4272SNagarjuna Kristam {
330649db4272SNagarjuna Kristam 	unsigned int i;
330749db4272SNagarjuna Kristam 	u32 val;
330849db4272SNagarjuna Kristam 
330949db4272SNagarjuna Kristam 	for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
331049db4272SNagarjuna Kristam 		memset(xudc->event_ring[i], 0, XUDC_EVENT_RING_SIZE *
331149db4272SNagarjuna Kristam 		       sizeof(*xudc->event_ring[i]));
331249db4272SNagarjuna Kristam 
331349db4272SNagarjuna Kristam 		val = xudc_readl(xudc, ERSTSZ);
331449db4272SNagarjuna Kristam 		val &= ~(ERSTSZ_ERSTXSZ_MASK << ERSTSZ_ERSTXSZ_SHIFT(i));
331549db4272SNagarjuna Kristam 		val |= XUDC_EVENT_RING_SIZE << ERSTSZ_ERSTXSZ_SHIFT(i);
331649db4272SNagarjuna Kristam 		xudc_writel(xudc, val, ERSTSZ);
331749db4272SNagarjuna Kristam 
331849db4272SNagarjuna Kristam 		xudc_writel(xudc, lower_32_bits(xudc->event_ring_phys[i]),
331949db4272SNagarjuna Kristam 			    ERSTXBALO(i));
332049db4272SNagarjuna Kristam 		xudc_writel(xudc, upper_32_bits(xudc->event_ring_phys[i]),
332149db4272SNagarjuna Kristam 			    ERSTXBAHI(i));
332249db4272SNagarjuna Kristam 	}
332349db4272SNagarjuna Kristam 
332449db4272SNagarjuna Kristam 	val = lower_32_bits(xudc->event_ring_phys[0]);
332549db4272SNagarjuna Kristam 	xudc_writel(xudc, val, ERDPLO);
332649db4272SNagarjuna Kristam 	val |= EREPLO_ECS;
332749db4272SNagarjuna Kristam 	xudc_writel(xudc, val, EREPLO);
332849db4272SNagarjuna Kristam 
332949db4272SNagarjuna Kristam 	val = upper_32_bits(xudc->event_ring_phys[0]);
333049db4272SNagarjuna Kristam 	xudc_writel(xudc, val, ERDPHI);
333149db4272SNagarjuna Kristam 	xudc_writel(xudc, val, EREPHI);
333249db4272SNagarjuna Kristam 
333349db4272SNagarjuna Kristam 	xudc->ccs = true;
333449db4272SNagarjuna Kristam 	xudc->event_ring_index = 0;
333549db4272SNagarjuna Kristam 	xudc->event_ring_deq_ptr = 0;
333649db4272SNagarjuna Kristam }
333749db4272SNagarjuna Kristam 
333849db4272SNagarjuna Kristam static void tegra_xudc_free_event_ring(struct tegra_xudc *xudc)
333949db4272SNagarjuna Kristam {
334049db4272SNagarjuna Kristam 	unsigned int i;
334149db4272SNagarjuna Kristam 
334249db4272SNagarjuna Kristam 	for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
334349db4272SNagarjuna Kristam 		dma_free_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
334449db4272SNagarjuna Kristam 				  sizeof(*xudc->event_ring[i]),
334549db4272SNagarjuna Kristam 				  xudc->event_ring[i],
334649db4272SNagarjuna Kristam 				  xudc->event_ring_phys[i]);
334749db4272SNagarjuna Kristam 	}
334849db4272SNagarjuna Kristam }
334949db4272SNagarjuna Kristam 
335049db4272SNagarjuna Kristam static void tegra_xudc_fpci_ipfs_init(struct tegra_xudc *xudc)
335149db4272SNagarjuna Kristam {
335249db4272SNagarjuna Kristam 	u32 val;
335349db4272SNagarjuna Kristam 
335449db4272SNagarjuna Kristam 	if (xudc->soc->has_ipfs) {
335549db4272SNagarjuna Kristam 		val = ipfs_readl(xudc, XUSB_DEV_CONFIGURATION_0);
335649db4272SNagarjuna Kristam 		val |= XUSB_DEV_CONFIGURATION_0_EN_FPCI;
335749db4272SNagarjuna Kristam 		ipfs_writel(xudc, val, XUSB_DEV_CONFIGURATION_0);
335849db4272SNagarjuna Kristam 		usleep_range(10, 15);
335949db4272SNagarjuna Kristam 	}
336049db4272SNagarjuna Kristam 
336149db4272SNagarjuna Kristam 	/* Enable bus master */
336249db4272SNagarjuna Kristam 	val = XUSB_DEV_CFG_1_IO_SPACE_EN | XUSB_DEV_CFG_1_MEMORY_SPACE_EN |
336349db4272SNagarjuna Kristam 		XUSB_DEV_CFG_1_BUS_MASTER_EN;
336449db4272SNagarjuna Kristam 	fpci_writel(xudc, val, XUSB_DEV_CFG_1);
336549db4272SNagarjuna Kristam 
336649db4272SNagarjuna Kristam 	/* Program BAR0 space */
336749db4272SNagarjuna Kristam 	val = fpci_readl(xudc, XUSB_DEV_CFG_4);
336849db4272SNagarjuna Kristam 	val &= ~(XUSB_DEV_CFG_4_BASE_ADDR_MASK);
336949db4272SNagarjuna Kristam 	val |= xudc->phys_base & (XUSB_DEV_CFG_4_BASE_ADDR_MASK);
337049db4272SNagarjuna Kristam 
337149db4272SNagarjuna Kristam 	fpci_writel(xudc, val, XUSB_DEV_CFG_4);
337249db4272SNagarjuna Kristam 	fpci_writel(xudc, upper_32_bits(xudc->phys_base), XUSB_DEV_CFG_5);
337349db4272SNagarjuna Kristam 
337449db4272SNagarjuna Kristam 	usleep_range(100, 200);
337549db4272SNagarjuna Kristam 
337649db4272SNagarjuna Kristam 	if (xudc->soc->has_ipfs) {
337749db4272SNagarjuna Kristam 		/* Enable interrupt assertion */
337849db4272SNagarjuna Kristam 		val = ipfs_readl(xudc, XUSB_DEV_INTR_MASK_0);
337949db4272SNagarjuna Kristam 		val |= XUSB_DEV_INTR_MASK_0_IP_INT_MASK;
338049db4272SNagarjuna Kristam 		ipfs_writel(xudc, val, XUSB_DEV_INTR_MASK_0);
338149db4272SNagarjuna Kristam 	}
338249db4272SNagarjuna Kristam }
338349db4272SNagarjuna Kristam 
338449db4272SNagarjuna Kristam static void tegra_xudc_device_params_init(struct tegra_xudc *xudc)
338549db4272SNagarjuna Kristam {
338649db4272SNagarjuna Kristam 	u32 val, imod;
338749db4272SNagarjuna Kristam 
338849db4272SNagarjuna Kristam 	if (xudc->soc->has_ipfs) {
338949db4272SNagarjuna Kristam 		val = xudc_readl(xudc, BLCG);
339049db4272SNagarjuna Kristam 		val |= BLCG_ALL;
339149db4272SNagarjuna Kristam 		val &= ~(BLCG_DFPCI | BLCG_UFPCI | BLCG_FE |
339249db4272SNagarjuna Kristam 				BLCG_COREPLL_PWRDN);
339349db4272SNagarjuna Kristam 		val |= BLCG_IOPLL_0_PWRDN;
339449db4272SNagarjuna Kristam 		val |= BLCG_IOPLL_1_PWRDN;
339549db4272SNagarjuna Kristam 		val |= BLCG_IOPLL_2_PWRDN;
339649db4272SNagarjuna Kristam 
339749db4272SNagarjuna Kristam 		xudc_writel(xudc, val, BLCG);
339849db4272SNagarjuna Kristam 	}
339949db4272SNagarjuna Kristam 
340088607a82SNagarjuna Kristam 	if (xudc->soc->port_speed_quirk)
340188607a82SNagarjuna Kristam 		tegra_xudc_limit_port_speed(xudc);
340288607a82SNagarjuna Kristam 
340349db4272SNagarjuna Kristam 	/* Set a reasonable U3 exit timer value. */
340449db4272SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_PADCTL4);
340549db4272SNagarjuna Kristam 	val &= ~(SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK);
340649db4272SNagarjuna Kristam 	val |= SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3(0x5dc0);
340749db4272SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_PADCTL4);
340849db4272SNagarjuna Kristam 
340949db4272SNagarjuna Kristam 	/* Default ping LFPS tBurst is too large. */
341049db4272SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT0);
341149db4272SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT0_PING_TBURST_MASK);
341249db4272SNagarjuna Kristam 	val |= SSPX_CORE_CNT0_PING_TBURST(0xa);
341349db4272SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT0);
341449db4272SNagarjuna Kristam 
341549db4272SNagarjuna Kristam 	/* Default tPortConfiguration timeout is too small. */
341649db4272SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT30);
341749db4272SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT30_LMPITP_TIMER_MASK);
341849db4272SNagarjuna Kristam 	val |= SSPX_CORE_CNT30_LMPITP_TIMER(0x978);
341949db4272SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT30);
342049db4272SNagarjuna Kristam 
342149db4272SNagarjuna Kristam 	if (xudc->soc->lpm_enable) {
342249db4272SNagarjuna Kristam 		/* Set L1 resume duration to 95 us. */
342349db4272SNagarjuna Kristam 		val = xudc_readl(xudc, HSFSPI_COUNT13);
342449db4272SNagarjuna Kristam 		val &= ~(HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK);
342549db4272SNagarjuna Kristam 		val |= HSFSPI_COUNT13_U2_RESUME_K_DURATION(0x2c88);
342649db4272SNagarjuna Kristam 		xudc_writel(xudc, val, HSFSPI_COUNT13);
342749db4272SNagarjuna Kristam 	}
342849db4272SNagarjuna Kristam 
342949db4272SNagarjuna Kristam 	/*
343053b0c69fSTom Rix 	 * Compliance suite appears to be violating polling LFPS tBurst max
343149db4272SNagarjuna Kristam 	 * of 1.4us.  Send 1.45us instead.
343249db4272SNagarjuna Kristam 	 */
343349db4272SNagarjuna Kristam 	val = xudc_readl(xudc, SSPX_CORE_CNT32);
343449db4272SNagarjuna Kristam 	val &= ~(SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK);
343549db4272SNagarjuna Kristam 	val |= SSPX_CORE_CNT32_POLL_TBURST_MAX(0xb0);
343649db4272SNagarjuna Kristam 	xudc_writel(xudc, val, SSPX_CORE_CNT32);
343749db4272SNagarjuna Kristam 
343849db4272SNagarjuna Kristam 	/* Direct HS/FS port instance to RxDetect. */
343949db4272SNagarjuna Kristam 	val = xudc_readl(xudc, CFG_DEV_FE);
344049db4272SNagarjuna Kristam 	val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
344149db4272SNagarjuna Kristam 	val |= CFG_DEV_FE_PORTREGSEL(CFG_DEV_FE_PORTREGSEL_HSFS_PI);
344249db4272SNagarjuna Kristam 	xudc_writel(xudc, val, CFG_DEV_FE);
344349db4272SNagarjuna Kristam 
344449db4272SNagarjuna Kristam 	val = xudc_readl(xudc, PORTSC);
344549db4272SNagarjuna Kristam 	val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
344649db4272SNagarjuna Kristam 	val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_RXDETECT);
344749db4272SNagarjuna Kristam 	xudc_writel(xudc, val, PORTSC);
344849db4272SNagarjuna Kristam 
344949db4272SNagarjuna Kristam 	/* Direct SS port instance to RxDetect. */
345049db4272SNagarjuna Kristam 	val = xudc_readl(xudc, CFG_DEV_FE);
345149db4272SNagarjuna Kristam 	val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
345249db4272SNagarjuna Kristam 	val |= CFG_DEV_FE_PORTREGSEL_SS_PI & CFG_DEV_FE_PORTREGSEL_MASK;
345349db4272SNagarjuna Kristam 	xudc_writel(xudc, val, CFG_DEV_FE);
345449db4272SNagarjuna Kristam 
345549db4272SNagarjuna Kristam 	val = xudc_readl(xudc, PORTSC);
345649db4272SNagarjuna Kristam 	val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
345749db4272SNagarjuna Kristam 	val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_RXDETECT);
345849db4272SNagarjuna Kristam 	xudc_writel(xudc, val, PORTSC);
345949db4272SNagarjuna Kristam 
346049db4272SNagarjuna Kristam 	/* Restore port instance. */
346149db4272SNagarjuna Kristam 	val = xudc_readl(xudc, CFG_DEV_FE);
346249db4272SNagarjuna Kristam 	val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
346349db4272SNagarjuna Kristam 	xudc_writel(xudc, val, CFG_DEV_FE);
346449db4272SNagarjuna Kristam 
346549db4272SNagarjuna Kristam 	/*
346649db4272SNagarjuna Kristam 	 * Enable INFINITE_SS_RETRY to prevent device from entering
346749db4272SNagarjuna Kristam 	 * Disabled.Error when attached to buggy SuperSpeed hubs.
346849db4272SNagarjuna Kristam 	 */
346949db4272SNagarjuna Kristam 	val = xudc_readl(xudc, CFG_DEV_FE);
347049db4272SNagarjuna Kristam 	val |= CFG_DEV_FE_INFINITE_SS_RETRY;
347149db4272SNagarjuna Kristam 	xudc_writel(xudc, val, CFG_DEV_FE);
347249db4272SNagarjuna Kristam 
347349db4272SNagarjuna Kristam 	/* Set interrupt moderation. */
347449db4272SNagarjuna Kristam 	imod = XUDC_INTERRUPT_MODERATION_US * 4;
347549db4272SNagarjuna Kristam 	val = xudc_readl(xudc, RT_IMOD);
347649db4272SNagarjuna Kristam 	val &= ~((RT_IMOD_IMODI_MASK) | (RT_IMOD_IMODC_MASK));
347749db4272SNagarjuna Kristam 	val |= (RT_IMOD_IMODI(imod) | RT_IMOD_IMODC(imod));
347849db4272SNagarjuna Kristam 	xudc_writel(xudc, val, RT_IMOD);
347949db4272SNagarjuna Kristam 
348049db4272SNagarjuna Kristam 	/* increase SSPI transaction timeout from 32us to 512us */
348149db4272SNagarjuna Kristam 	val = xudc_readl(xudc, CFG_DEV_SSPI_XFER);
348249db4272SNagarjuna Kristam 	val &= ~(CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK);
348349db4272SNagarjuna Kristam 	val |= CFG_DEV_SSPI_XFER_ACKTIMEOUT(0xf000);
348449db4272SNagarjuna Kristam 	xudc_writel(xudc, val, CFG_DEV_SSPI_XFER);
348549db4272SNagarjuna Kristam }
348649db4272SNagarjuna Kristam 
3487b4e19931SNagarjuna Kristam static int tegra_xudc_phy_get(struct tegra_xudc *xudc)
348849db4272SNagarjuna Kristam {
3489b4e19931SNagarjuna Kristam 	int err = 0, usb3;
3490b4e19931SNagarjuna Kristam 	unsigned int i;
349149db4272SNagarjuna Kristam 
3492b4e19931SNagarjuna Kristam 	xudc->utmi_phy = devm_kcalloc(xudc->dev, xudc->soc->num_phys,
3493b4e19931SNagarjuna Kristam 					   sizeof(*xudc->utmi_phy), GFP_KERNEL);
3494b4e19931SNagarjuna Kristam 	if (!xudc->utmi_phy)
3495b4e19931SNagarjuna Kristam 		return -ENOMEM;
3496b4e19931SNagarjuna Kristam 
3497b4e19931SNagarjuna Kristam 	xudc->usb3_phy = devm_kcalloc(xudc->dev, xudc->soc->num_phys,
3498b4e19931SNagarjuna Kristam 					   sizeof(*xudc->usb3_phy), GFP_KERNEL);
3499b4e19931SNagarjuna Kristam 	if (!xudc->usb3_phy)
3500b4e19931SNagarjuna Kristam 		return -ENOMEM;
3501b4e19931SNagarjuna Kristam 
3502b4e19931SNagarjuna Kristam 	xudc->usbphy = devm_kcalloc(xudc->dev, xudc->soc->num_phys,
3503b4e19931SNagarjuna Kristam 					   sizeof(*xudc->usbphy), GFP_KERNEL);
3504b4e19931SNagarjuna Kristam 	if (!xudc->usbphy)
3505b4e19931SNagarjuna Kristam 		return -ENOMEM;
3506b4e19931SNagarjuna Kristam 
3507b4e19931SNagarjuna Kristam 	xudc->vbus_nb.notifier_call = tegra_xudc_vbus_notify;
3508b4e19931SNagarjuna Kristam 
3509b4e19931SNagarjuna Kristam 	for (i = 0; i < xudc->soc->num_phys; i++) {
3510b4e19931SNagarjuna Kristam 		char phy_name[] = "usb.-.";
3511b4e19931SNagarjuna Kristam 
3512b4e19931SNagarjuna Kristam 		/* Get USB2 phy */
3513b4e19931SNagarjuna Kristam 		snprintf(phy_name, sizeof(phy_name), "usb2-%d", i);
3514b4e19931SNagarjuna Kristam 		xudc->utmi_phy[i] = devm_phy_optional_get(xudc->dev, phy_name);
3515b4e19931SNagarjuna Kristam 		if (IS_ERR(xudc->utmi_phy[i])) {
3516b4e19931SNagarjuna Kristam 			err = PTR_ERR(xudc->utmi_phy[i]);
351777b57218SJon Hunter 			dev_err_probe(xudc->dev, err,
351877b57218SJon Hunter 				      "failed to get usb2-%d PHY\n", i);
3519b4e19931SNagarjuna Kristam 			goto clean_up;
3520b4e19931SNagarjuna Kristam 		} else if (xudc->utmi_phy[i]) {
3521b4e19931SNagarjuna Kristam 			/* Get usb-phy, if utmi phy is available */
3522b4e19931SNagarjuna Kristam 			xudc->usbphy[i] = devm_usb_get_phy_by_node(xudc->dev,
3523b4e19931SNagarjuna Kristam 						xudc->utmi_phy[i]->dev.of_node,
3524b4e19931SNagarjuna Kristam 						&xudc->vbus_nb);
3525b4e19931SNagarjuna Kristam 			if (IS_ERR(xudc->usbphy[i])) {
3526b4e19931SNagarjuna Kristam 				err = PTR_ERR(xudc->usbphy[i]);
352780a3c7f7SJon Hunter 				dev_err_probe(xudc->dev, err,
352880a3c7f7SJon Hunter 					      "failed to get usbphy-%d\n", i);
3529b4e19931SNagarjuna Kristam 				goto clean_up;
3530b4e19931SNagarjuna Kristam 			}
3531b4e19931SNagarjuna Kristam 		} else if (!xudc->utmi_phy[i]) {
3532b4e19931SNagarjuna Kristam 			/* if utmi phy is not available, ignore USB3 phy get */
3533b4e19931SNagarjuna Kristam 			continue;
3534b4e19931SNagarjuna Kristam 		}
3535b4e19931SNagarjuna Kristam 
3536b4e19931SNagarjuna Kristam 		/* Get USB3 phy */
3537b4e19931SNagarjuna Kristam 		usb3 = tegra_xusb_padctl_get_usb3_companion(xudc->padctl, i);
3538b4e19931SNagarjuna Kristam 		if (usb3 < 0)
3539b4e19931SNagarjuna Kristam 			continue;
3540b4e19931SNagarjuna Kristam 
3541b4e19931SNagarjuna Kristam 		snprintf(phy_name, sizeof(phy_name), "usb3-%d", usb3);
3542b4e19931SNagarjuna Kristam 		xudc->usb3_phy[i] = devm_phy_optional_get(xudc->dev, phy_name);
3543b4e19931SNagarjuna Kristam 		if (IS_ERR(xudc->usb3_phy[i])) {
3544b4e19931SNagarjuna Kristam 			err = PTR_ERR(xudc->usb3_phy[i]);
354577b57218SJon Hunter 			dev_err_probe(xudc->dev, err,
354677b57218SJon Hunter 				      "failed to get usb3-%d PHY\n", usb3);
3547b4e19931SNagarjuna Kristam 			goto clean_up;
3548b4e19931SNagarjuna Kristam 		} else if (xudc->usb3_phy[i])
3549de21e728SThierry Reding 			dev_dbg(xudc->dev, "usb3-%d PHY registered", usb3);
3550b4e19931SNagarjuna Kristam 	}
3551b4e19931SNagarjuna Kristam 
355249db4272SNagarjuna Kristam 	return err;
3553b4e19931SNagarjuna Kristam 
3554b4e19931SNagarjuna Kristam clean_up:
3555b4e19931SNagarjuna Kristam 	for (i = 0; i < xudc->soc->num_phys; i++) {
3556b4e19931SNagarjuna Kristam 		xudc->usb3_phy[i] = NULL;
3557b4e19931SNagarjuna Kristam 		xudc->utmi_phy[i] = NULL;
3558b4e19931SNagarjuna Kristam 		xudc->usbphy[i] = NULL;
355949db4272SNagarjuna Kristam 	}
356049db4272SNagarjuna Kristam 
356149db4272SNagarjuna Kristam 	return err;
356249db4272SNagarjuna Kristam }
356349db4272SNagarjuna Kristam 
356449db4272SNagarjuna Kristam static void tegra_xudc_phy_exit(struct tegra_xudc *xudc)
356549db4272SNagarjuna Kristam {
3566b4e19931SNagarjuna Kristam 	unsigned int i;
3567b4e19931SNagarjuna Kristam 
3568b4e19931SNagarjuna Kristam 	for (i = 0; i < xudc->soc->num_phys; i++) {
3569b4e19931SNagarjuna Kristam 		phy_exit(xudc->usb3_phy[i]);
3570b4e19931SNagarjuna Kristam 		phy_exit(xudc->utmi_phy[i]);
3571b4e19931SNagarjuna Kristam 	}
3572b4e19931SNagarjuna Kristam }
3573b4e19931SNagarjuna Kristam 
3574b4e19931SNagarjuna Kristam static int tegra_xudc_phy_init(struct tegra_xudc *xudc)
3575b4e19931SNagarjuna Kristam {
3576b4e19931SNagarjuna Kristam 	int err;
3577b4e19931SNagarjuna Kristam 	unsigned int i;
3578b4e19931SNagarjuna Kristam 
3579b4e19931SNagarjuna Kristam 	for (i = 0; i < xudc->soc->num_phys; i++) {
3580b4e19931SNagarjuna Kristam 		err = phy_init(xudc->utmi_phy[i]);
3581b4e19931SNagarjuna Kristam 		if (err < 0) {
3582de21e728SThierry Reding 			dev_err(xudc->dev, "UTMI PHY #%u initialization failed: %d\n", i, err);
3583b4e19931SNagarjuna Kristam 			goto exit_phy;
3584b4e19931SNagarjuna Kristam 		}
3585b4e19931SNagarjuna Kristam 
3586b4e19931SNagarjuna Kristam 		err = phy_init(xudc->usb3_phy[i]);
3587b4e19931SNagarjuna Kristam 		if (err < 0) {
3588de21e728SThierry Reding 			dev_err(xudc->dev, "USB3 PHY #%u initialization failed: %d\n", i, err);
3589b4e19931SNagarjuna Kristam 			goto exit_phy;
3590b4e19931SNagarjuna Kristam 		}
3591b4e19931SNagarjuna Kristam 	}
3592b4e19931SNagarjuna Kristam 	return 0;
3593b4e19931SNagarjuna Kristam 
3594b4e19931SNagarjuna Kristam exit_phy:
3595b4e19931SNagarjuna Kristam 	tegra_xudc_phy_exit(xudc);
3596b4e19931SNagarjuna Kristam 	return err;
359749db4272SNagarjuna Kristam }
359849db4272SNagarjuna Kristam 
359949db4272SNagarjuna Kristam static const char * const tegra210_xudc_supply_names[] = {
360049db4272SNagarjuna Kristam 	"hvdd-usb",
360149db4272SNagarjuna Kristam 	"avddio-usb",
360249db4272SNagarjuna Kristam };
360349db4272SNagarjuna Kristam 
360449db4272SNagarjuna Kristam static const char * const tegra210_xudc_clock_names[] = {
360549db4272SNagarjuna Kristam 	"dev",
360649db4272SNagarjuna Kristam 	"ss",
360749db4272SNagarjuna Kristam 	"ss_src",
360849db4272SNagarjuna Kristam 	"hs_src",
360949db4272SNagarjuna Kristam 	"fs_src",
361049db4272SNagarjuna Kristam };
361149db4272SNagarjuna Kristam 
361249db4272SNagarjuna Kristam static const char * const tegra186_xudc_clock_names[] = {
361349db4272SNagarjuna Kristam 	"dev",
361449db4272SNagarjuna Kristam 	"ss",
361549db4272SNagarjuna Kristam 	"ss_src",
361649db4272SNagarjuna Kristam 	"fs_src",
361749db4272SNagarjuna Kristam };
361849db4272SNagarjuna Kristam 
361949db4272SNagarjuna Kristam static struct tegra_xudc_soc tegra210_xudc_soc_data = {
362049db4272SNagarjuna Kristam 	.supply_names = tegra210_xudc_supply_names,
362149db4272SNagarjuna Kristam 	.num_supplies = ARRAY_SIZE(tegra210_xudc_supply_names),
362249db4272SNagarjuna Kristam 	.clock_names = tegra210_xudc_clock_names,
362349db4272SNagarjuna Kristam 	.num_clks = ARRAY_SIZE(tegra210_xudc_clock_names),
3624b4e19931SNagarjuna Kristam 	.num_phys = 4,
362549db4272SNagarjuna Kristam 	.u1_enable = false,
362649db4272SNagarjuna Kristam 	.u2_enable = true,
362749db4272SNagarjuna Kristam 	.lpm_enable = false,
362849db4272SNagarjuna Kristam 	.invalid_seq_num = true,
362949db4272SNagarjuna Kristam 	.pls_quirk = true,
363049db4272SNagarjuna Kristam 	.port_reset_quirk = true,
363188607a82SNagarjuna Kristam 	.port_speed_quirk = false,
363249db4272SNagarjuna Kristam 	.has_ipfs = true,
363349db4272SNagarjuna Kristam };
363449db4272SNagarjuna Kristam 
363549db4272SNagarjuna Kristam static struct tegra_xudc_soc tegra186_xudc_soc_data = {
363649db4272SNagarjuna Kristam 	.clock_names = tegra186_xudc_clock_names,
363749db4272SNagarjuna Kristam 	.num_clks = ARRAY_SIZE(tegra186_xudc_clock_names),
3638b4e19931SNagarjuna Kristam 	.num_phys = 4,
363949db4272SNagarjuna Kristam 	.u1_enable = true,
364049db4272SNagarjuna Kristam 	.u2_enable = true,
364149db4272SNagarjuna Kristam 	.lpm_enable = false,
364249db4272SNagarjuna Kristam 	.invalid_seq_num = false,
364349db4272SNagarjuna Kristam 	.pls_quirk = false,
364449db4272SNagarjuna Kristam 	.port_reset_quirk = false,
364588607a82SNagarjuna Kristam 	.port_speed_quirk = false,
364649db4272SNagarjuna Kristam 	.has_ipfs = false,
364749db4272SNagarjuna Kristam };
364849db4272SNagarjuna Kristam 
36499584a60aSNagarjuna Kristam static struct tegra_xudc_soc tegra194_xudc_soc_data = {
36509584a60aSNagarjuna Kristam 	.clock_names = tegra186_xudc_clock_names,
36519584a60aSNagarjuna Kristam 	.num_clks = ARRAY_SIZE(tegra186_xudc_clock_names),
36529584a60aSNagarjuna Kristam 	.num_phys = 4,
36539584a60aSNagarjuna Kristam 	.u1_enable = true,
36549584a60aSNagarjuna Kristam 	.u2_enable = true,
36559584a60aSNagarjuna Kristam 	.lpm_enable = true,
36569584a60aSNagarjuna Kristam 	.invalid_seq_num = false,
36579584a60aSNagarjuna Kristam 	.pls_quirk = false,
36589584a60aSNagarjuna Kristam 	.port_reset_quirk = false,
365988607a82SNagarjuna Kristam 	.port_speed_quirk = true,
366049db4272SNagarjuna Kristam 	.has_ipfs = false,
366149db4272SNagarjuna Kristam };
366249db4272SNagarjuna Kristam 
366349db4272SNagarjuna Kristam static const struct of_device_id tegra_xudc_of_match[] = {
366449db4272SNagarjuna Kristam 	{
366549db4272SNagarjuna Kristam 		.compatible = "nvidia,tegra210-xudc",
366649db4272SNagarjuna Kristam 		.data = &tegra210_xudc_soc_data
366749db4272SNagarjuna Kristam 	},
366849db4272SNagarjuna Kristam 	{
366949db4272SNagarjuna Kristam 		.compatible = "nvidia,tegra186-xudc",
367049db4272SNagarjuna Kristam 		.data = &tegra186_xudc_soc_data
367149db4272SNagarjuna Kristam 	},
36729584a60aSNagarjuna Kristam 	{
36739584a60aSNagarjuna Kristam 		.compatible = "nvidia,tegra194-xudc",
36749584a60aSNagarjuna Kristam 		.data = &tegra194_xudc_soc_data
36759584a60aSNagarjuna Kristam 	},
367649db4272SNagarjuna Kristam 	{ }
367749db4272SNagarjuna Kristam };
367849db4272SNagarjuna Kristam MODULE_DEVICE_TABLE(of, tegra_xudc_of_match);
367949db4272SNagarjuna Kristam 
368049db4272SNagarjuna Kristam static void tegra_xudc_powerdomain_remove(struct tegra_xudc *xudc)
368149db4272SNagarjuna Kristam {
368249db4272SNagarjuna Kristam 	if (xudc->genpd_dl_ss)
368349db4272SNagarjuna Kristam 		device_link_del(xudc->genpd_dl_ss);
368449db4272SNagarjuna Kristam 	if (xudc->genpd_dl_device)
368549db4272SNagarjuna Kristam 		device_link_del(xudc->genpd_dl_device);
368649db4272SNagarjuna Kristam 	if (xudc->genpd_dev_ss)
368749db4272SNagarjuna Kristam 		dev_pm_domain_detach(xudc->genpd_dev_ss, true);
368849db4272SNagarjuna Kristam 	if (xudc->genpd_dev_device)
368949db4272SNagarjuna Kristam 		dev_pm_domain_detach(xudc->genpd_dev_device, true);
369049db4272SNagarjuna Kristam }
369149db4272SNagarjuna Kristam 
369249db4272SNagarjuna Kristam static int tegra_xudc_powerdomain_init(struct tegra_xudc *xudc)
369349db4272SNagarjuna Kristam {
369449db4272SNagarjuna Kristam 	struct device *dev = xudc->dev;
369549db4272SNagarjuna Kristam 	int err;
369649db4272SNagarjuna Kristam 
3697230c1aa3SThierry Reding 	xudc->genpd_dev_device = dev_pm_domain_attach_by_name(dev, "dev");
3698f08aa7c8STang Bin 	if (IS_ERR_OR_NULL(xudc->genpd_dev_device)) {
3699f08aa7c8STang Bin 		err = PTR_ERR(xudc->genpd_dev_device) ? : -ENODATA;
3700de21e728SThierry Reding 		dev_err(dev, "failed to get device power domain: %d\n", err);
370149db4272SNagarjuna Kristam 		return err;
370249db4272SNagarjuna Kristam 	}
370349db4272SNagarjuna Kristam 
370449db4272SNagarjuna Kristam 	xudc->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "ss");
3705f08aa7c8STang Bin 	if (IS_ERR_OR_NULL(xudc->genpd_dev_ss)) {
3706f08aa7c8STang Bin 		err = PTR_ERR(xudc->genpd_dev_ss) ? : -ENODATA;
3707de21e728SThierry Reding 		dev_err(dev, "failed to get SuperSpeed power domain: %d\n", err);
370849db4272SNagarjuna Kristam 		return err;
370949db4272SNagarjuna Kristam 	}
371049db4272SNagarjuna Kristam 
371149db4272SNagarjuna Kristam 	xudc->genpd_dl_device = device_link_add(dev, xudc->genpd_dev_device,
371249db4272SNagarjuna Kristam 						DL_FLAG_PM_RUNTIME |
371349db4272SNagarjuna Kristam 						DL_FLAG_STATELESS);
371449db4272SNagarjuna Kristam 	if (!xudc->genpd_dl_device) {
3715de21e728SThierry Reding 		dev_err(dev, "failed to add USB device link\n");
371649db4272SNagarjuna Kristam 		return -ENODEV;
371749db4272SNagarjuna Kristam 	}
371849db4272SNagarjuna Kristam 
371949db4272SNagarjuna Kristam 	xudc->genpd_dl_ss = device_link_add(dev, xudc->genpd_dev_ss,
372049db4272SNagarjuna Kristam 					    DL_FLAG_PM_RUNTIME |
372149db4272SNagarjuna Kristam 					    DL_FLAG_STATELESS);
372249db4272SNagarjuna Kristam 	if (!xudc->genpd_dl_ss) {
3723de21e728SThierry Reding 		dev_err(dev, "failed to add SuperSpeed device link\n");
372449db4272SNagarjuna Kristam 		return -ENODEV;
372549db4272SNagarjuna Kristam 	}
372649db4272SNagarjuna Kristam 
372749db4272SNagarjuna Kristam 	return 0;
372849db4272SNagarjuna Kristam }
372949db4272SNagarjuna Kristam 
373049db4272SNagarjuna Kristam static int tegra_xudc_probe(struct platform_device *pdev)
373149db4272SNagarjuna Kristam {
373249db4272SNagarjuna Kristam 	struct tegra_xudc *xudc;
373349db4272SNagarjuna Kristam 	struct resource *res;
373449db4272SNagarjuna Kristam 	unsigned int i;
373549db4272SNagarjuna Kristam 	int err;
373649db4272SNagarjuna Kristam 
37376c2a754aSChristophe JAILLET 	xudc = devm_kzalloc(&pdev->dev, sizeof(*xudc), GFP_KERNEL);
373849db4272SNagarjuna Kristam 	if (!xudc)
373949db4272SNagarjuna Kristam 		return -ENOMEM;
374049db4272SNagarjuna Kristam 
374149db4272SNagarjuna Kristam 	xudc->dev = &pdev->dev;
374249db4272SNagarjuna Kristam 	platform_set_drvdata(pdev, xudc);
374349db4272SNagarjuna Kristam 
374449db4272SNagarjuna Kristam 	xudc->soc = of_device_get_match_data(&pdev->dev);
374549db4272SNagarjuna Kristam 	if (!xudc->soc)
374649db4272SNagarjuna Kristam 		return -ENODEV;
374749db4272SNagarjuna Kristam 
374849db4272SNagarjuna Kristam 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
374949db4272SNagarjuna Kristam 	xudc->base = devm_ioremap_resource(&pdev->dev, res);
375049db4272SNagarjuna Kristam 	if (IS_ERR(xudc->base))
375149db4272SNagarjuna Kristam 		return PTR_ERR(xudc->base);
375249db4272SNagarjuna Kristam 	xudc->phys_base = res->start;
375349db4272SNagarjuna Kristam 
37549d4ee5bdSChunfeng Yun 	xudc->fpci = devm_platform_ioremap_resource_byname(pdev, "fpci");
375549db4272SNagarjuna Kristam 	if (IS_ERR(xudc->fpci))
375649db4272SNagarjuna Kristam 		return PTR_ERR(xudc->fpci);
375749db4272SNagarjuna Kristam 
375849db4272SNagarjuna Kristam 	if (xudc->soc->has_ipfs) {
37599d4ee5bdSChunfeng Yun 		xudc->ipfs = devm_platform_ioremap_resource_byname(pdev, "ipfs");
376049db4272SNagarjuna Kristam 		if (IS_ERR(xudc->ipfs))
376149db4272SNagarjuna Kristam 			return PTR_ERR(xudc->ipfs);
376249db4272SNagarjuna Kristam 	}
376349db4272SNagarjuna Kristam 
376449db4272SNagarjuna Kristam 	xudc->irq = platform_get_irq(pdev, 0);
376549f1997aSYueHaibing 	if (xudc->irq < 0)
376649db4272SNagarjuna Kristam 		return xudc->irq;
376749db4272SNagarjuna Kristam 
376849db4272SNagarjuna Kristam 	err = devm_request_irq(&pdev->dev, xudc->irq, tegra_xudc_irq, 0,
376949db4272SNagarjuna Kristam 			       dev_name(&pdev->dev), xudc);
377049db4272SNagarjuna Kristam 	if (err < 0) {
377149db4272SNagarjuna Kristam 		dev_err(xudc->dev, "failed to claim IRQ#%u: %d\n", xudc->irq,
377249db4272SNagarjuna Kristam 			err);
377349db4272SNagarjuna Kristam 		return err;
377449db4272SNagarjuna Kristam 	}
377549db4272SNagarjuna Kristam 
3776230c1aa3SThierry Reding 	xudc->clks = devm_kcalloc(&pdev->dev, xudc->soc->num_clks, sizeof(*xudc->clks),
3777230c1aa3SThierry Reding 				  GFP_KERNEL);
377849db4272SNagarjuna Kristam 	if (!xudc->clks)
377949db4272SNagarjuna Kristam 		return -ENOMEM;
378049db4272SNagarjuna Kristam 
378149db4272SNagarjuna Kristam 	for (i = 0; i < xudc->soc->num_clks; i++)
378249db4272SNagarjuna Kristam 		xudc->clks[i].id = xudc->soc->clock_names[i];
378349db4272SNagarjuna Kristam 
3784230c1aa3SThierry Reding 	err = devm_clk_bulk_get(&pdev->dev, xudc->soc->num_clks, xudc->clks);
378549db4272SNagarjuna Kristam 	if (err) {
378677b57218SJon Hunter 		dev_err_probe(xudc->dev, err, "failed to request clocks\n");
378749db4272SNagarjuna Kristam 		return err;
378849db4272SNagarjuna Kristam 	}
378949db4272SNagarjuna Kristam 
379049db4272SNagarjuna Kristam 	xudc->supplies = devm_kcalloc(&pdev->dev, xudc->soc->num_supplies,
379149db4272SNagarjuna Kristam 				      sizeof(*xudc->supplies), GFP_KERNEL);
379249db4272SNagarjuna Kristam 	if (!xudc->supplies)
379349db4272SNagarjuna Kristam 		return -ENOMEM;
379449db4272SNagarjuna Kristam 
379549db4272SNagarjuna Kristam 	for (i = 0; i < xudc->soc->num_supplies; i++)
379649db4272SNagarjuna Kristam 		xudc->supplies[i].supply = xudc->soc->supply_names[i];
379749db4272SNagarjuna Kristam 
379849db4272SNagarjuna Kristam 	err = devm_regulator_bulk_get(&pdev->dev, xudc->soc->num_supplies,
379949db4272SNagarjuna Kristam 				      xudc->supplies);
380049db4272SNagarjuna Kristam 	if (err) {
380177b57218SJon Hunter 		dev_err_probe(xudc->dev, err, "failed to request regulators\n");
380249db4272SNagarjuna Kristam 		return err;
380349db4272SNagarjuna Kristam 	}
380449db4272SNagarjuna Kristam 
380549db4272SNagarjuna Kristam 	xudc->padctl = tegra_xusb_padctl_get(&pdev->dev);
380649db4272SNagarjuna Kristam 	if (IS_ERR(xudc->padctl))
380749db4272SNagarjuna Kristam 		return PTR_ERR(xudc->padctl);
380849db4272SNagarjuna Kristam 
380949db4272SNagarjuna Kristam 	err = regulator_bulk_enable(xudc->soc->num_supplies, xudc->supplies);
381049db4272SNagarjuna Kristam 	if (err) {
3811de21e728SThierry Reding 		dev_err(xudc->dev, "failed to enable regulators: %d\n", err);
381249db4272SNagarjuna Kristam 		goto put_padctl;
381349db4272SNagarjuna Kristam 	}
381449db4272SNagarjuna Kristam 
3815b4e19931SNagarjuna Kristam 	err = tegra_xudc_phy_get(xudc);
3816b4e19931SNagarjuna Kristam 	if (err)
381749db4272SNagarjuna Kristam 		goto disable_regulator;
381849db4272SNagarjuna Kristam 
381949db4272SNagarjuna Kristam 	err = tegra_xudc_powerdomain_init(xudc);
382049db4272SNagarjuna Kristam 	if (err)
382149db4272SNagarjuna Kristam 		goto put_powerdomains;
382249db4272SNagarjuna Kristam 
382349db4272SNagarjuna Kristam 	err = tegra_xudc_phy_init(xudc);
382449db4272SNagarjuna Kristam 	if (err)
382549db4272SNagarjuna Kristam 		goto put_powerdomains;
382649db4272SNagarjuna Kristam 
382749db4272SNagarjuna Kristam 	err = tegra_xudc_alloc_event_ring(xudc);
382849db4272SNagarjuna Kristam 	if (err)
382949db4272SNagarjuna Kristam 		goto disable_phy;
383049db4272SNagarjuna Kristam 
383149db4272SNagarjuna Kristam 	err = tegra_xudc_alloc_eps(xudc);
383249db4272SNagarjuna Kristam 	if (err)
383349db4272SNagarjuna Kristam 		goto free_event_ring;
383449db4272SNagarjuna Kristam 
383549db4272SNagarjuna Kristam 	spin_lock_init(&xudc->lock);
383649db4272SNagarjuna Kristam 
383749db4272SNagarjuna Kristam 	init_completion(&xudc->disconnect_complete);
383849db4272SNagarjuna Kristam 
383949db4272SNagarjuna Kristam 	INIT_WORK(&xudc->usb_role_sw_work, tegra_xudc_usb_role_sw_work);
384049db4272SNagarjuna Kristam 
384149db4272SNagarjuna Kristam 	INIT_DELAYED_WORK(&xudc->plc_reset_work, tegra_xudc_plc_reset_work);
384249db4272SNagarjuna Kristam 
384349db4272SNagarjuna Kristam 	INIT_DELAYED_WORK(&xudc->port_reset_war_work,
384449db4272SNagarjuna Kristam 				tegra_xudc_port_reset_war_work);
384549db4272SNagarjuna Kristam 
384649db4272SNagarjuna Kristam 	pm_runtime_enable(&pdev->dev);
384749db4272SNagarjuna Kristam 
384849db4272SNagarjuna Kristam 	xudc->gadget.ops = &tegra_xudc_gadget_ops;
384949db4272SNagarjuna Kristam 	xudc->gadget.ep0 = &xudc->ep[0].usb_ep;
385049db4272SNagarjuna Kristam 	xudc->gadget.name = "tegra-xudc";
385149db4272SNagarjuna Kristam 	xudc->gadget.max_speed = USB_SPEED_SUPER;
385249db4272SNagarjuna Kristam 
385349db4272SNagarjuna Kristam 	err = usb_add_gadget_udc(&pdev->dev, &xudc->gadget);
385449db4272SNagarjuna Kristam 	if (err) {
385549db4272SNagarjuna Kristam 		dev_err(&pdev->dev, "failed to add USB gadget: %d\n", err);
385649db4272SNagarjuna Kristam 		goto free_eps;
385749db4272SNagarjuna Kristam 	}
385849db4272SNagarjuna Kristam 
385949db4272SNagarjuna Kristam 	return 0;
386049db4272SNagarjuna Kristam 
386149db4272SNagarjuna Kristam free_eps:
38625b012481SZhang Qilong 	pm_runtime_disable(&pdev->dev);
386349db4272SNagarjuna Kristam 	tegra_xudc_free_eps(xudc);
386449db4272SNagarjuna Kristam free_event_ring:
386549db4272SNagarjuna Kristam 	tegra_xudc_free_event_ring(xudc);
386649db4272SNagarjuna Kristam disable_phy:
386749db4272SNagarjuna Kristam 	tegra_xudc_phy_exit(xudc);
386849db4272SNagarjuna Kristam put_powerdomains:
386949db4272SNagarjuna Kristam 	tegra_xudc_powerdomain_remove(xudc);
387049db4272SNagarjuna Kristam disable_regulator:
387149db4272SNagarjuna Kristam 	regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
387249db4272SNagarjuna Kristam put_padctl:
387349db4272SNagarjuna Kristam 	tegra_xusb_padctl_put(xudc->padctl);
387449db4272SNagarjuna Kristam 
387549db4272SNagarjuna Kristam 	return err;
387649db4272SNagarjuna Kristam }
387749db4272SNagarjuna Kristam 
387849db4272SNagarjuna Kristam static int tegra_xudc_remove(struct platform_device *pdev)
387949db4272SNagarjuna Kristam {
388049db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = platform_get_drvdata(pdev);
3881b4e19931SNagarjuna Kristam 	unsigned int i;
388249db4272SNagarjuna Kristam 
388349db4272SNagarjuna Kristam 	pm_runtime_get_sync(xudc->dev);
388449db4272SNagarjuna Kristam 
3885a932ee40SYang Yingliang 	cancel_delayed_work_sync(&xudc->plc_reset_work);
388649db4272SNagarjuna Kristam 	cancel_work_sync(&xudc->usb_role_sw_work);
388749db4272SNagarjuna Kristam 
388849db4272SNagarjuna Kristam 	usb_del_gadget_udc(&xudc->gadget);
388949db4272SNagarjuna Kristam 
389049db4272SNagarjuna Kristam 	tegra_xudc_free_eps(xudc);
389149db4272SNagarjuna Kristam 	tegra_xudc_free_event_ring(xudc);
389249db4272SNagarjuna Kristam 
389349db4272SNagarjuna Kristam 	tegra_xudc_powerdomain_remove(xudc);
389449db4272SNagarjuna Kristam 
389549db4272SNagarjuna Kristam 	regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
389649db4272SNagarjuna Kristam 
3897b4e19931SNagarjuna Kristam 	for (i = 0; i < xudc->soc->num_phys; i++) {
3898b4e19931SNagarjuna Kristam 		phy_power_off(xudc->utmi_phy[i]);
3899b4e19931SNagarjuna Kristam 		phy_power_off(xudc->usb3_phy[i]);
3900b4e19931SNagarjuna Kristam 	}
390149db4272SNagarjuna Kristam 
390249db4272SNagarjuna Kristam 	tegra_xudc_phy_exit(xudc);
390349db4272SNagarjuna Kristam 
390449db4272SNagarjuna Kristam 	pm_runtime_disable(xudc->dev);
390549db4272SNagarjuna Kristam 	pm_runtime_put(xudc->dev);
390649db4272SNagarjuna Kristam 
390749db4272SNagarjuna Kristam 	tegra_xusb_padctl_put(xudc->padctl);
390849db4272SNagarjuna Kristam 
390949db4272SNagarjuna Kristam 	return 0;
391049db4272SNagarjuna Kristam }
391149db4272SNagarjuna Kristam 
391249db4272SNagarjuna Kristam static int __maybe_unused tegra_xudc_powergate(struct tegra_xudc *xudc)
391349db4272SNagarjuna Kristam {
391449db4272SNagarjuna Kristam 	unsigned long flags;
391549db4272SNagarjuna Kristam 
391649db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "entering ELPG\n");
391749db4272SNagarjuna Kristam 
391849db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
391949db4272SNagarjuna Kristam 
392049db4272SNagarjuna Kristam 	xudc->powergated = true;
392149db4272SNagarjuna Kristam 	xudc->saved_regs.ctrl = xudc_readl(xudc, CTRL);
392249db4272SNagarjuna Kristam 	xudc->saved_regs.portpm = xudc_readl(xudc, PORTPM);
392349db4272SNagarjuna Kristam 	xudc_writel(xudc, 0, CTRL);
392449db4272SNagarjuna Kristam 
392549db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
392649db4272SNagarjuna Kristam 
392749db4272SNagarjuna Kristam 	clk_bulk_disable_unprepare(xudc->soc->num_clks, xudc->clks);
392849db4272SNagarjuna Kristam 
392949db4272SNagarjuna Kristam 	regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
393049db4272SNagarjuna Kristam 
393149db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "entering ELPG done\n");
393249db4272SNagarjuna Kristam 	return 0;
393349db4272SNagarjuna Kristam }
393449db4272SNagarjuna Kristam 
393549db4272SNagarjuna Kristam static int __maybe_unused tegra_xudc_unpowergate(struct tegra_xudc *xudc)
393649db4272SNagarjuna Kristam {
393749db4272SNagarjuna Kristam 	unsigned long flags;
393849db4272SNagarjuna Kristam 	int err;
393949db4272SNagarjuna Kristam 
394049db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "exiting ELPG\n");
394149db4272SNagarjuna Kristam 
394249db4272SNagarjuna Kristam 	err = regulator_bulk_enable(xudc->soc->num_supplies,
394349db4272SNagarjuna Kristam 			xudc->supplies);
394449db4272SNagarjuna Kristam 	if (err < 0)
394549db4272SNagarjuna Kristam 		return err;
394649db4272SNagarjuna Kristam 
394749db4272SNagarjuna Kristam 	err = clk_bulk_prepare_enable(xudc->soc->num_clks, xudc->clks);
394849db4272SNagarjuna Kristam 	if (err < 0)
394949db4272SNagarjuna Kristam 		return err;
395049db4272SNagarjuna Kristam 
395149db4272SNagarjuna Kristam 	tegra_xudc_fpci_ipfs_init(xudc);
395249db4272SNagarjuna Kristam 
395349db4272SNagarjuna Kristam 	tegra_xudc_device_params_init(xudc);
395449db4272SNagarjuna Kristam 
395549db4272SNagarjuna Kristam 	tegra_xudc_init_event_ring(xudc);
395649db4272SNagarjuna Kristam 
395749db4272SNagarjuna Kristam 	tegra_xudc_init_eps(xudc);
395849db4272SNagarjuna Kristam 
395949db4272SNagarjuna Kristam 	xudc_writel(xudc, xudc->saved_regs.portpm, PORTPM);
396049db4272SNagarjuna Kristam 	xudc_writel(xudc, xudc->saved_regs.ctrl, CTRL);
396149db4272SNagarjuna Kristam 
396249db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
396349db4272SNagarjuna Kristam 	xudc->powergated = false;
396449db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
396549db4272SNagarjuna Kristam 
396649db4272SNagarjuna Kristam 	dev_dbg(xudc->dev, "exiting ELPG done\n");
396749db4272SNagarjuna Kristam 	return 0;
396849db4272SNagarjuna Kristam }
396949db4272SNagarjuna Kristam 
397049db4272SNagarjuna Kristam static int __maybe_unused tegra_xudc_suspend(struct device *dev)
397149db4272SNagarjuna Kristam {
397249db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = dev_get_drvdata(dev);
397349db4272SNagarjuna Kristam 	unsigned long flags;
397449db4272SNagarjuna Kristam 
397549db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
397649db4272SNagarjuna Kristam 	xudc->suspended = true;
397749db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
397849db4272SNagarjuna Kristam 
397949db4272SNagarjuna Kristam 	flush_work(&xudc->usb_role_sw_work);
398049db4272SNagarjuna Kristam 
39810534d401SThierry Reding 	if (!pm_runtime_status_suspended(dev)) {
398249db4272SNagarjuna Kristam 		/* Forcibly disconnect before powergating. */
398349db4272SNagarjuna Kristam 		tegra_xudc_device_mode_off(xudc);
398449db4272SNagarjuna Kristam 		tegra_xudc_powergate(xudc);
39850534d401SThierry Reding 	}
398649db4272SNagarjuna Kristam 
398749db4272SNagarjuna Kristam 	pm_runtime_disable(dev);
398849db4272SNagarjuna Kristam 
398949db4272SNagarjuna Kristam 	return 0;
399049db4272SNagarjuna Kristam }
399149db4272SNagarjuna Kristam 
399249db4272SNagarjuna Kristam static int __maybe_unused tegra_xudc_resume(struct device *dev)
399349db4272SNagarjuna Kristam {
399449db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = dev_get_drvdata(dev);
399549db4272SNagarjuna Kristam 	unsigned long flags;
399649db4272SNagarjuna Kristam 	int err;
399749db4272SNagarjuna Kristam 
399849db4272SNagarjuna Kristam 	err = tegra_xudc_unpowergate(xudc);
399949db4272SNagarjuna Kristam 	if (err < 0)
400049db4272SNagarjuna Kristam 		return err;
400149db4272SNagarjuna Kristam 
400249db4272SNagarjuna Kristam 	spin_lock_irqsave(&xudc->lock, flags);
400349db4272SNagarjuna Kristam 	xudc->suspended = false;
400449db4272SNagarjuna Kristam 	spin_unlock_irqrestore(&xudc->lock, flags);
400549db4272SNagarjuna Kristam 
400649db4272SNagarjuna Kristam 	schedule_work(&xudc->usb_role_sw_work);
400749db4272SNagarjuna Kristam 
400849db4272SNagarjuna Kristam 	pm_runtime_enable(dev);
400949db4272SNagarjuna Kristam 
401049db4272SNagarjuna Kristam 	return 0;
401149db4272SNagarjuna Kristam }
401249db4272SNagarjuna Kristam 
401349db4272SNagarjuna Kristam static int __maybe_unused tegra_xudc_runtime_suspend(struct device *dev)
401449db4272SNagarjuna Kristam {
401549db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = dev_get_drvdata(dev);
401649db4272SNagarjuna Kristam 
401749db4272SNagarjuna Kristam 	return tegra_xudc_powergate(xudc);
401849db4272SNagarjuna Kristam }
401949db4272SNagarjuna Kristam 
402049db4272SNagarjuna Kristam static int __maybe_unused tegra_xudc_runtime_resume(struct device *dev)
402149db4272SNagarjuna Kristam {
402249db4272SNagarjuna Kristam 	struct tegra_xudc *xudc = dev_get_drvdata(dev);
402349db4272SNagarjuna Kristam 
402449db4272SNagarjuna Kristam 	return tegra_xudc_unpowergate(xudc);
402549db4272SNagarjuna Kristam }
402649db4272SNagarjuna Kristam 
402749db4272SNagarjuna Kristam static const struct dev_pm_ops tegra_xudc_pm_ops = {
402849db4272SNagarjuna Kristam 	SET_SYSTEM_SLEEP_PM_OPS(tegra_xudc_suspend, tegra_xudc_resume)
402949db4272SNagarjuna Kristam 	SET_RUNTIME_PM_OPS(tegra_xudc_runtime_suspend,
403049db4272SNagarjuna Kristam 			   tegra_xudc_runtime_resume, NULL)
403149db4272SNagarjuna Kristam };
403249db4272SNagarjuna Kristam 
403349db4272SNagarjuna Kristam static struct platform_driver tegra_xudc_driver = {
403449db4272SNagarjuna Kristam 	.probe = tegra_xudc_probe,
403549db4272SNagarjuna Kristam 	.remove = tegra_xudc_remove,
403649db4272SNagarjuna Kristam 	.driver = {
403749db4272SNagarjuna Kristam 		.name = "tegra-xudc",
403849db4272SNagarjuna Kristam 		.pm = &tegra_xudc_pm_ops,
403949db4272SNagarjuna Kristam 		.of_match_table = tegra_xudc_of_match,
404049db4272SNagarjuna Kristam 	},
404149db4272SNagarjuna Kristam };
404249db4272SNagarjuna Kristam module_platform_driver(tegra_xudc_driver);
404349db4272SNagarjuna Kristam 
404449db4272SNagarjuna Kristam MODULE_DESCRIPTION("NVIDIA Tegra XUSB Device Controller");
404549db4272SNagarjuna Kristam MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
404649db4272SNagarjuna Kristam MODULE_AUTHOR("Hui Fu <hfu@nvidia.com>");
404749db4272SNagarjuna Kristam MODULE_AUTHOR("Nagarjuna Kristam <nkristam@nvidia.com>");
404849db4272SNagarjuna Kristam MODULE_LICENSE("GPL v2");
4049