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