1 // SPDX-License-Identifier: GPL-2.0+
3 * NVIDIA Tegra XUSB device mode controller
5 * Copyright (c) 2013-2019, NVIDIA CORPORATION. All rights reserved.
6 * Copyright (c) 2015, Google Inc.
10 #include <linux/completion.h>
11 #include <linux/delay.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmapool.h>
14 #include <linux/interrupt.h>
15 #include <linux/iopoll.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/phy/phy.h>
21 #include <linux/phy/tegra/xusb.h>
22 #include <linux/pm_domain.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/reset.h>
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/gadget.h>
29 #include <linux/usb/role.h>
30 #include <linux/workqueue.h>
32 /* XUSB_DEV registers */
34 #define SPARAM_ERSTMAX_MASK GENMASK(20, 16)
35 #define SPARAM_ERSTMAX(x) (((x) << 16) & SPARAM_ERSTMAX_MASK)
37 #define DB_TARGET_MASK GENMASK(15, 8)
38 #define DB_TARGET(x) (((x) << 8) & DB_TARGET_MASK)
39 #define DB_STREAMID_MASK GENMASK(31, 16)
40 #define DB_STREAMID(x) (((x) << 16) & DB_STREAMID_MASK)
42 #define ERSTSZ_ERSTXSZ_SHIFT(x) ((x) * 16)
43 #define ERSTSZ_ERSTXSZ_MASK GENMASK(15, 0)
44 #define ERSTXBALO(x) (0x010 + 8 * (x))
45 #define ERSTXBAHI(x) (0x014 + 8 * (x))
47 #define ERDPLO_EHB BIT(3)
50 #define EREPLO_ECS BIT(0)
51 #define EREPLO_SEGI BIT(1)
54 #define CTRL_RUN BIT(0)
55 #define CTRL_LSE BIT(1)
56 #define CTRL_IE BIT(4)
57 #define CTRL_SMI_EVT BIT(5)
58 #define CTRL_SMI_DSE BIT(6)
59 #define CTRL_EWE BIT(7)
60 #define CTRL_DEVADDR_MASK GENMASK(30, 24)
61 #define CTRL_DEVADDR(x) (((x) << 24) & CTRL_DEVADDR_MASK)
62 #define CTRL_ENABLE BIT(31)
67 #define RT_IMOD_IMODI_MASK GENMASK(15, 0)
68 #define RT_IMOD_IMODI(x) ((x) & RT_IMOD_IMODI_MASK)
69 #define RT_IMOD_IMODC_MASK GENMASK(31, 16)
70 #define RT_IMOD_IMODC(x) (((x) << 16) & RT_IMOD_IMODC_MASK)
72 #define PORTSC_CCS BIT(0)
73 #define PORTSC_PED BIT(1)
74 #define PORTSC_PR BIT(4)
75 #define PORTSC_PLS_SHIFT 5
76 #define PORTSC_PLS_MASK GENMASK(8, 5)
77 #define PORTSC_PLS_U0 0x0
78 #define PORTSC_PLS_U2 0x2
79 #define PORTSC_PLS_U3 0x3
80 #define PORTSC_PLS_DISABLED 0x4
81 #define PORTSC_PLS_RXDETECT 0x5
82 #define PORTSC_PLS_INACTIVE 0x6
83 #define PORTSC_PLS_RESUME 0xf
84 #define PORTSC_PLS(x) (((x) << PORTSC_PLS_SHIFT) & PORTSC_PLS_MASK)
85 #define PORTSC_PS_SHIFT 10
86 #define PORTSC_PS_MASK GENMASK(13, 10)
87 #define PORTSC_PS_UNDEFINED 0x0
88 #define PORTSC_PS_FS 0x1
89 #define PORTSC_PS_LS 0x2
90 #define PORTSC_PS_HS 0x3
91 #define PORTSC_PS_SS 0x4
92 #define PORTSC_LWS BIT(16)
93 #define PORTSC_CSC BIT(17)
94 #define PORTSC_WRC BIT(19)
95 #define PORTSC_PRC BIT(21)
96 #define PORTSC_PLC BIT(22)
97 #define PORTSC_CEC BIT(23)
98 #define PORTSC_WPR BIT(30)
99 #define PORTSC_CHANGE_MASK (PORTSC_CSC | PORTSC_WRC | PORTSC_PRC | \
100 PORTSC_PLC | PORTSC_CEC)
103 #define MFINDEX 0x048
104 #define MFINDEX_FRAME_SHIFT 3
105 #define MFINDEX_FRAME_MASK GENMASK(13, 3)
107 #define PORTPM_L1S_MASK GENMASK(1, 0)
108 #define PORTPM_L1S_DROP 0x0
109 #define PORTPM_L1S_ACCEPT 0x1
110 #define PORTPM_L1S_NYET 0x2
111 #define PORTPM_L1S_STALL 0x3
112 #define PORTPM_L1S(x) ((x) & PORTPM_L1S_MASK)
113 #define PORTPM_RWE BIT(3)
114 #define PORTPM_U2TIMEOUT_MASK GENMASK(15, 8)
115 #define PORTPM_U1TIMEOUT_MASK GENMASK(23, 16)
116 #define PORTPM_FLA BIT(24)
117 #define PORTPM_VBA BIT(25)
118 #define PORTPM_WOC BIT(26)
119 #define PORTPM_WOD BIT(27)
120 #define PORTPM_U1E BIT(28)
121 #define PORTPM_U2E BIT(29)
122 #define PORTPM_FRWE BIT(30)
123 #define PORTPM_PNG_CYA BIT(31)
124 #define EP_HALT 0x050
125 #define EP_PAUSE 0x054
126 #define EP_RELOAD 0x058
127 #define EP_STCHG 0x05c
128 #define DEVNOTIF_LO 0x064
129 #define DEVNOTIF_LO_TRIG BIT(0)
130 #define DEVNOTIF_LO_TYPE_MASK GENMASK(7, 4)
131 #define DEVNOTIF_LO_TYPE(x) (((x) << 4) & DEVNOTIF_LO_TYPE_MASK)
132 #define DEVNOTIF_LO_TYPE_FUNCTION_WAKE 0x1
133 #define DEVNOTIF_HI 0x068
134 #define PORTHALT 0x06c
135 #define PORTHALT_HALT_LTSSM BIT(0)
136 #define PORTHALT_HALT_REJECT BIT(1)
137 #define PORTHALT_STCHG_REQ BIT(20)
138 #define PORTHALT_STCHG_INTR_EN BIT(24)
139 #define PORT_TM 0x070
140 #define EP_THREAD_ACTIVE 0x074
141 #define EP_STOPPED 0x078
142 #define HSFSPI_COUNT0 0x100
143 #define HSFSPI_COUNT13 0x134
144 #define HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK GENMASK(29, 0)
145 #define HSFSPI_COUNT13_U2_RESUME_K_DURATION(x) ((x) & \
146 HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK)
148 #define SSPX_CORE_CNT0 0x610
149 #define SSPX_CORE_CNT0_PING_TBURST_MASK GENMASK(7, 0)
150 #define SSPX_CORE_CNT0_PING_TBURST(x) ((x) & SSPX_CORE_CNT0_PING_TBURST_MASK)
151 #define SSPX_CORE_CNT30 0x688
152 #define SSPX_CORE_CNT30_LMPITP_TIMER_MASK GENMASK(19, 0)
153 #define SSPX_CORE_CNT30_LMPITP_TIMER(x) ((x) & \
154 SSPX_CORE_CNT30_LMPITP_TIMER_MASK)
155 #define SSPX_CORE_CNT32 0x690
156 #define SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK GENMASK(7, 0)
157 #define SSPX_CORE_CNT32_POLL_TBURST_MAX(x) ((x) & \
158 SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK)
159 #define SSPX_CORE_PADCTL4 0x750
160 #define SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK GENMASK(19, 0)
161 #define SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3(x) ((x) & \
162 SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK)
163 #define BLCG_DFPCI BIT(0)
164 #define BLCG_UFPCI BIT(1)
165 #define BLCG_FE BIT(2)
166 #define BLCG_COREPLL_PWRDN BIT(8)
167 #define BLCG_IOPLL_0_PWRDN BIT(9)
168 #define BLCG_IOPLL_1_PWRDN BIT(10)
169 #define BLCG_IOPLL_2_PWRDN BIT(11)
170 #define BLCG_ALL 0x1ff
171 #define CFG_DEV_SSPI_XFER 0x858
172 #define CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK GENMASK(31, 0)
173 #define CFG_DEV_SSPI_XFER_ACKTIMEOUT(x) ((x) & \
174 CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK)
175 #define CFG_DEV_FE 0x85c
176 #define CFG_DEV_FE_PORTREGSEL_MASK GENMASK(1, 0)
177 #define CFG_DEV_FE_PORTREGSEL_SS_PI 1
178 #define CFG_DEV_FE_PORTREGSEL_HSFS_PI 2
179 #define CFG_DEV_FE_PORTREGSEL(x) ((x) & CFG_DEV_FE_PORTREGSEL_MASK)
180 #define CFG_DEV_FE_INFINITE_SS_RETRY BIT(29)
183 #define XUSB_DEV_CFG_1 0x004
184 #define XUSB_DEV_CFG_1_IO_SPACE_EN BIT(0)
185 #define XUSB_DEV_CFG_1_MEMORY_SPACE_EN BIT(1)
186 #define XUSB_DEV_CFG_1_BUS_MASTER_EN BIT(2)
187 #define XUSB_DEV_CFG_4 0x010
188 #define XUSB_DEV_CFG_4_BASE_ADDR_MASK GENMASK(31, 15)
189 #define XUSB_DEV_CFG_5 0x014
192 #define XUSB_DEV_CONFIGURATION_0 0x180
193 #define XUSB_DEV_CONFIGURATION_0_EN_FPCI BIT(0)
194 #define XUSB_DEV_INTR_MASK_0 0x188
195 #define XUSB_DEV_INTR_MASK_0_IP_INT_MASK BIT(16)
197 struct tegra_xudc_ep_context {
206 #define EP_STATE_DISABLED 0
207 #define EP_STATE_RUNNING 1
208 #define EP_STATE_HALTED 2
209 #define EP_STATE_STOPPED 3
210 #define EP_STATE_ERROR 4
212 #define EP_TYPE_INVALID 0
213 #define EP_TYPE_ISOCH_OUT 1
214 #define EP_TYPE_BULK_OUT 2
215 #define EP_TYPE_INTERRUPT_OUT 3
216 #define EP_TYPE_CONTROL 4
217 #define EP_TYPE_ISCOH_IN 5
218 #define EP_TYPE_BULK_IN 6
219 #define EP_TYPE_INTERRUPT_IN 7
221 #define BUILD_EP_CONTEXT_RW(name, member, shift, mask) \
222 static inline u32 ep_ctx_read_##name(struct tegra_xudc_ep_context *ctx) \
224 return (le32_to_cpu(ctx->member) >> (shift)) & (mask); \
227 ep_ctx_write_##name(struct tegra_xudc_ep_context *ctx, u32 val) \
231 tmp = le32_to_cpu(ctx->member) & ~((mask) << (shift)); \
232 tmp |= (val & (mask)) << (shift); \
233 ctx->member = cpu_to_le32(tmp); \
236 BUILD_EP_CONTEXT_RW(state, info0, 0, 0x7)
237 BUILD_EP_CONTEXT_RW(mult, info0, 8, 0x3)
238 BUILD_EP_CONTEXT_RW(max_pstreams, info0, 10, 0x1f)
239 BUILD_EP_CONTEXT_RW(lsa, info0, 15, 0x1)
240 BUILD_EP_CONTEXT_RW(interval, info0, 16, 0xff)
241 BUILD_EP_CONTEXT_RW(cerr, info1, 1, 0x3)
242 BUILD_EP_CONTEXT_RW(type, info1, 3, 0x7)
243 BUILD_EP_CONTEXT_RW(hid, info1, 7, 0x1)
244 BUILD_EP_CONTEXT_RW(max_burst_size, info1, 8, 0xff)
245 BUILD_EP_CONTEXT_RW(max_packet_size, info1, 16, 0xffff)
246 BUILD_EP_CONTEXT_RW(dcs, deq_lo, 0, 0x1)
247 BUILD_EP_CONTEXT_RW(deq_lo, deq_lo, 4, 0xfffffff)
248 BUILD_EP_CONTEXT_RW(deq_hi, deq_hi, 0, 0xffffffff)
249 BUILD_EP_CONTEXT_RW(avg_trb_len, tx_info, 0, 0xffff)
250 BUILD_EP_CONTEXT_RW(max_esit_payload, tx_info, 16, 0xffff)
251 BUILD_EP_CONTEXT_RW(edtla, rsvd[0], 0, 0xffffff)
252 BUILD_EP_CONTEXT_RW(seq_num, rsvd[0], 24, 0xff)
253 BUILD_EP_CONTEXT_RW(partial_td, rsvd[0], 25, 0x1)
254 BUILD_EP_CONTEXT_RW(cerrcnt, rsvd[1], 18, 0x3)
255 BUILD_EP_CONTEXT_RW(data_offset, rsvd[2], 0, 0x1ffff)
256 BUILD_EP_CONTEXT_RW(numtrbs, rsvd[2], 22, 0x1f)
257 BUILD_EP_CONTEXT_RW(devaddr, rsvd[6], 0, 0x7f)
259 static inline u64 ep_ctx_read_deq_ptr(struct tegra_xudc_ep_context *ctx)
261 return ((u64)ep_ctx_read_deq_hi(ctx) << 32) |
262 (ep_ctx_read_deq_lo(ctx) << 4);
266 ep_ctx_write_deq_ptr(struct tegra_xudc_ep_context *ctx, u64 addr)
268 ep_ctx_write_deq_lo(ctx, lower_32_bits(addr) >> 4);
269 ep_ctx_write_deq_hi(ctx, upper_32_bits(addr));
272 struct tegra_xudc_trb {
279 #define TRB_TYPE_RSVD 0
280 #define TRB_TYPE_NORMAL 1
281 #define TRB_TYPE_SETUP_STAGE 2
282 #define TRB_TYPE_DATA_STAGE 3
283 #define TRB_TYPE_STATUS_STAGE 4
284 #define TRB_TYPE_ISOCH 5
285 #define TRB_TYPE_LINK 6
286 #define TRB_TYPE_TRANSFER_EVENT 32
287 #define TRB_TYPE_PORT_STATUS_CHANGE_EVENT 34
288 #define TRB_TYPE_STREAM 48
289 #define TRB_TYPE_SETUP_PACKET_EVENT 63
291 #define TRB_CMPL_CODE_INVALID 0
292 #define TRB_CMPL_CODE_SUCCESS 1
293 #define TRB_CMPL_CODE_DATA_BUFFER_ERR 2
294 #define TRB_CMPL_CODE_BABBLE_DETECTED_ERR 3
295 #define TRB_CMPL_CODE_USB_TRANS_ERR 4
296 #define TRB_CMPL_CODE_TRB_ERR 5
297 #define TRB_CMPL_CODE_STALL 6
298 #define TRB_CMPL_CODE_INVALID_STREAM_TYPE_ERR 10
299 #define TRB_CMPL_CODE_SHORT_PACKET 13
300 #define TRB_CMPL_CODE_RING_UNDERRUN 14
301 #define TRB_CMPL_CODE_RING_OVERRUN 15
302 #define TRB_CMPL_CODE_EVENT_RING_FULL_ERR 21
303 #define TRB_CMPL_CODE_STOPPED 26
304 #define TRB_CMPL_CODE_ISOCH_BUFFER_OVERRUN 31
305 #define TRB_CMPL_CODE_STREAM_NUMP_ERROR 219
306 #define TRB_CMPL_CODE_PRIME_PIPE_RECEIVED 220
307 #define TRB_CMPL_CODE_HOST_REJECTED 221
308 #define TRB_CMPL_CODE_CTRL_DIR_ERR 222
309 #define TRB_CMPL_CODE_CTRL_SEQNUM_ERR 223
311 #define BUILD_TRB_RW(name, member, shift, mask) \
312 static inline u32 trb_read_##name(struct tegra_xudc_trb *trb) \
314 return (le32_to_cpu(trb->member) >> (shift)) & (mask); \
317 trb_write_##name(struct tegra_xudc_trb *trb, u32 val) \
321 tmp = le32_to_cpu(trb->member) & ~((mask) << (shift)); \
322 tmp |= (val & (mask)) << (shift); \
323 trb->member = cpu_to_le32(tmp); \
326 BUILD_TRB_RW(data_lo, data_lo, 0, 0xffffffff)
327 BUILD_TRB_RW(data_hi, data_hi, 0, 0xffffffff)
328 BUILD_TRB_RW(seq_num, status, 0, 0xffff)
329 BUILD_TRB_RW(transfer_len, status, 0, 0xffffff)
330 BUILD_TRB_RW(td_size, status, 17, 0x1f)
331 BUILD_TRB_RW(cmpl_code, status, 24, 0xff)
332 BUILD_TRB_RW(cycle, control, 0, 0x1)
333 BUILD_TRB_RW(toggle_cycle, control, 1, 0x1)
334 BUILD_TRB_RW(isp, control, 2, 0x1)
335 BUILD_TRB_RW(chain, control, 4, 0x1)
336 BUILD_TRB_RW(ioc, control, 5, 0x1)
337 BUILD_TRB_RW(type, control, 10, 0x3f)
338 BUILD_TRB_RW(stream_id, control, 16, 0xffff)
339 BUILD_TRB_RW(endpoint_id, control, 16, 0x1f)
340 BUILD_TRB_RW(tlbpc, control, 16, 0xf)
341 BUILD_TRB_RW(data_stage_dir, control, 16, 0x1)
342 BUILD_TRB_RW(frame_id, control, 20, 0x7ff)
343 BUILD_TRB_RW(sia, control, 31, 0x1)
345 static inline u64 trb_read_data_ptr(struct tegra_xudc_trb *trb)
347 return ((u64)trb_read_data_hi(trb) << 32) |
348 trb_read_data_lo(trb);
351 static inline void trb_write_data_ptr(struct tegra_xudc_trb *trb, u64 addr)
353 trb_write_data_lo(trb, lower_32_bits(addr));
354 trb_write_data_hi(trb, upper_32_bits(addr));
357 struct tegra_xudc_request {
358 struct usb_request usb_req;
361 unsigned int trbs_queued;
362 unsigned int trbs_needed;
365 struct tegra_xudc_trb *first_trb;
366 struct tegra_xudc_trb *last_trb;
368 struct list_head list;
371 struct tegra_xudc_ep {
372 struct tegra_xudc *xudc;
373 struct usb_ep usb_ep;
377 struct tegra_xudc_ep_context *context;
379 #define XUDC_TRANSFER_RING_SIZE 64
380 struct tegra_xudc_trb *transfer_ring;
381 dma_addr_t transfer_ring_phys;
383 unsigned int enq_ptr;
384 unsigned int deq_ptr;
387 bool stream_rejected;
389 struct list_head queue;
390 const struct usb_endpoint_descriptor *desc;
391 const struct usb_ss_ep_comp_descriptor *comp_desc;
394 struct tegra_xudc_sel_timing {
401 enum tegra_xudc_setup_state {
409 struct tegra_xudc_setup_packet {
410 struct usb_ctrlrequest ctrl_req;
411 unsigned int seq_num;
414 struct tegra_xudc_save_regs {
421 const struct tegra_xudc_soc *soc;
422 struct tegra_xusb_padctl *padctl;
426 struct usb_gadget gadget;
427 struct usb_gadget_driver *driver;
429 #define XUDC_NR_EVENT_RINGS 2
430 #define XUDC_EVENT_RING_SIZE 4096
431 struct tegra_xudc_trb *event_ring[XUDC_NR_EVENT_RINGS];
432 dma_addr_t event_ring_phys[XUDC_NR_EVENT_RINGS];
433 unsigned int event_ring_index;
434 unsigned int event_ring_deq_ptr;
437 #define XUDC_NR_EPS 32
438 struct tegra_xudc_ep ep[XUDC_NR_EPS];
439 struct tegra_xudc_ep_context *ep_context;
440 dma_addr_t ep_context_phys;
442 struct device *genpd_dev_device;
443 struct device *genpd_dev_ss;
444 struct device_link *genpd_dl_device;
445 struct device_link *genpd_dl_ss;
447 struct dma_pool *transfer_ring_pool;
449 bool queued_setup_packet;
450 struct tegra_xudc_setup_packet setup_packet;
451 enum tegra_xudc_setup_state setup_state;
456 struct tegra_xudc_sel_timing sel_timing;
457 u8 test_mode_pattern;
459 struct tegra_xudc_request *ep0_req;
463 unsigned int nr_enabled_eps;
464 unsigned int nr_isoch_eps;
466 unsigned int device_state;
467 unsigned int resume_state;
472 resource_size_t phys_base;
476 struct regulator_bulk_data *supplies;
478 struct clk_bulk_data *clks;
481 struct work_struct usb_role_sw_work;
483 struct phy *usb3_phy;
484 struct phy *utmi_phy;
486 struct tegra_xudc_save_regs saved_regs;
490 struct completion disconnect_complete;
494 #define TOGGLE_VBUS_WAIT_MS 100
495 struct delayed_work plc_reset_work;
498 struct delayed_work port_reset_war_work;
499 bool wait_for_sec_prc;
502 #define XUDC_TRB_MAX_BUFFER_SIZE 65536
503 #define XUDC_MAX_ISOCH_EPS 4
504 #define XUDC_INTERRUPT_MODERATION_US 0
506 static struct usb_endpoint_descriptor tegra_xudc_ep0_desc = {
507 .bLength = USB_DT_ENDPOINT_SIZE,
508 .bDescriptorType = USB_DT_ENDPOINT,
509 .bEndpointAddress = 0,
510 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
511 .wMaxPacketSize = cpu_to_le16(64),
514 struct tegra_xudc_soc {
515 const char * const *supply_names;
516 unsigned int num_supplies;
517 const char * const *clock_names;
518 unsigned int num_clks;
522 bool invalid_seq_num;
524 bool port_reset_quirk;
528 static inline u32 fpci_readl(struct tegra_xudc *xudc, unsigned int offset)
530 return readl(xudc->fpci + offset);
533 static inline void fpci_writel(struct tegra_xudc *xudc, u32 val,
536 writel(val, xudc->fpci + offset);
539 static inline u32 ipfs_readl(struct tegra_xudc *xudc, unsigned int offset)
541 return readl(xudc->ipfs + offset);
544 static inline void ipfs_writel(struct tegra_xudc *xudc, u32 val,
547 writel(val, xudc->ipfs + offset);
550 static inline u32 xudc_readl(struct tegra_xudc *xudc, unsigned int offset)
552 return readl(xudc->base + offset);
555 static inline void xudc_writel(struct tegra_xudc *xudc, u32 val,
558 writel(val, xudc->base + offset);
561 static inline int xudc_readl_poll(struct tegra_xudc *xudc,
562 unsigned int offset, u32 mask, u32 val)
566 return readl_poll_timeout_atomic(xudc->base + offset, regval,
567 (regval & mask) == val, 1, 100);
570 static inline struct tegra_xudc *to_xudc(struct usb_gadget *gadget)
572 return container_of(gadget, struct tegra_xudc, gadget);
575 static inline struct tegra_xudc_ep *to_xudc_ep(struct usb_ep *ep)
577 return container_of(ep, struct tegra_xudc_ep, usb_ep);
580 static inline struct tegra_xudc_request *to_xudc_req(struct usb_request *req)
582 return container_of(req, struct tegra_xudc_request, usb_req);
585 static inline void dump_trb(struct tegra_xudc *xudc, const char *type,
586 struct tegra_xudc_trb *trb)
589 "%s: %p, lo = %#x, hi = %#x, status = %#x, control = %#x\n",
590 type, trb, trb->data_lo, trb->data_hi, trb->status,
594 static void tegra_xudc_device_mode_on(struct tegra_xudc *xudc)
598 pm_runtime_get_sync(xudc->dev);
600 err = phy_power_on(xudc->utmi_phy);
602 dev_err(xudc->dev, "utmi power on failed %d\n", err);
604 err = phy_power_on(xudc->usb3_phy);
606 dev_err(xudc->dev, "usb3 phy power on failed %d\n", err);
608 dev_dbg(xudc->dev, "device mode on\n");
610 tegra_xusb_padctl_set_vbus_override(xudc->padctl, true);
613 static void tegra_xudc_device_mode_off(struct tegra_xudc *xudc)
615 bool connected = false;
619 dev_dbg(xudc->dev, "device mode off\n");
621 connected = !!(xudc_readl(xudc, PORTSC) & PORTSC_CCS);
623 reinit_completion(&xudc->disconnect_complete);
625 tegra_xusb_padctl_set_vbus_override(xudc->padctl, false);
627 pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
630 /* Direct link to U0 if disconnected in RESUME or U2. */
631 if (xudc->soc->pls_quirk && xudc->gadget.speed == USB_SPEED_SUPER &&
632 (pls == PORTSC_PLS_RESUME || pls == PORTSC_PLS_U2)) {
633 val = xudc_readl(xudc, PORTPM);
635 xudc_writel(xudc, val, PORTPM);
637 val = xudc_readl(xudc, PORTSC);
638 val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
639 val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_U0);
640 xudc_writel(xudc, val, PORTSC);
643 /* Wait for disconnect event. */
645 wait_for_completion(&xudc->disconnect_complete);
647 /* Make sure interrupt handler has completed before powergating. */
648 synchronize_irq(xudc->irq);
650 err = phy_power_off(xudc->utmi_phy);
652 dev_err(xudc->dev, "utmi_phy power off failed %d\n", err);
654 err = phy_power_off(xudc->usb3_phy);
656 dev_err(xudc->dev, "usb3_phy power off failed %d\n", err);
658 pm_runtime_put(xudc->dev);
661 static void tegra_xudc_usb_role_sw_work(struct work_struct *work)
663 struct tegra_xudc *xudc = container_of(work, struct tegra_xudc,
666 if (xudc->device_mode)
667 tegra_xudc_device_mode_on(xudc);
669 tegra_xudc_device_mode_off(xudc);
672 static void tegra_xudc_plc_reset_work(struct work_struct *work)
674 struct delayed_work *dwork = to_delayed_work(work);
675 struct tegra_xudc *xudc = container_of(dwork, struct tegra_xudc,
679 spin_lock_irqsave(&xudc->lock, flags);
681 if (xudc->wait_csc) {
682 u32 pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
685 if (pls == PORTSC_PLS_INACTIVE) {
686 dev_info(xudc->dev, "PLS = Inactive. Toggle VBUS\n");
687 tegra_xusb_padctl_set_vbus_override(xudc->padctl,
689 tegra_xusb_padctl_set_vbus_override(xudc->padctl, true);
690 xudc->wait_csc = false;
694 spin_unlock_irqrestore(&xudc->lock, flags);
697 static void tegra_xudc_port_reset_war_work(struct work_struct *work)
699 struct delayed_work *dwork = to_delayed_work(work);
700 struct tegra_xudc *xudc =
701 container_of(dwork, struct tegra_xudc, port_reset_war_work);
706 spin_lock_irqsave(&xudc->lock, flags);
708 if (xudc->device_mode && xudc->wait_for_sec_prc) {
709 pls = (xudc_readl(xudc, PORTSC) & PORTSC_PLS_MASK) >>
711 dev_dbg(xudc->dev, "pls = %x\n", pls);
713 if (pls == PORTSC_PLS_DISABLED) {
714 dev_dbg(xudc->dev, "toggle vbus\n");
715 /* PRC doesn't complete in 100ms, toggle the vbus */
716 ret = tegra_phy_xusb_utmi_port_reset(xudc->utmi_phy);
718 xudc->wait_for_sec_prc = 0;
722 spin_unlock_irqrestore(&xudc->lock, flags);
725 static dma_addr_t trb_virt_to_phys(struct tegra_xudc_ep *ep,
726 struct tegra_xudc_trb *trb)
730 index = trb - ep->transfer_ring;
732 if (WARN_ON(index >= XUDC_TRANSFER_RING_SIZE))
735 return (ep->transfer_ring_phys + index * sizeof(*trb));
738 static struct tegra_xudc_trb *trb_phys_to_virt(struct tegra_xudc_ep *ep,
741 struct tegra_xudc_trb *trb;
744 index = (addr - ep->transfer_ring_phys) / sizeof(*trb);
746 if (WARN_ON(index >= XUDC_TRANSFER_RING_SIZE))
749 trb = &ep->transfer_ring[index];
754 static void ep_reload(struct tegra_xudc *xudc, unsigned int ep)
756 xudc_writel(xudc, BIT(ep), EP_RELOAD);
757 xudc_readl_poll(xudc, EP_RELOAD, BIT(ep), 0);
760 static void ep_pause(struct tegra_xudc *xudc, unsigned int ep)
764 val = xudc_readl(xudc, EP_PAUSE);
769 xudc_writel(xudc, val, EP_PAUSE);
771 xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
773 xudc_writel(xudc, BIT(ep), EP_STCHG);
776 static void ep_unpause(struct tegra_xudc *xudc, unsigned int ep)
780 val = xudc_readl(xudc, EP_PAUSE);
781 if (!(val & BIT(ep)))
785 xudc_writel(xudc, val, EP_PAUSE);
787 xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
789 xudc_writel(xudc, BIT(ep), EP_STCHG);
792 static void ep_unpause_all(struct tegra_xudc *xudc)
796 val = xudc_readl(xudc, EP_PAUSE);
798 xudc_writel(xudc, 0, EP_PAUSE);
800 xudc_readl_poll(xudc, EP_STCHG, val, val);
802 xudc_writel(xudc, val, EP_STCHG);
805 static void ep_halt(struct tegra_xudc *xudc, unsigned int ep)
809 val = xudc_readl(xudc, EP_HALT);
813 xudc_writel(xudc, val, EP_HALT);
815 xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
817 xudc_writel(xudc, BIT(ep), EP_STCHG);
820 static void ep_unhalt(struct tegra_xudc *xudc, unsigned int ep)
824 val = xudc_readl(xudc, EP_HALT);
825 if (!(val & BIT(ep)))
828 xudc_writel(xudc, val, EP_HALT);
830 xudc_readl_poll(xudc, EP_STCHG, BIT(ep), BIT(ep));
832 xudc_writel(xudc, BIT(ep), EP_STCHG);
835 static void ep_unhalt_all(struct tegra_xudc *xudc)
839 val = xudc_readl(xudc, EP_HALT);
842 xudc_writel(xudc, 0, EP_HALT);
844 xudc_readl_poll(xudc, EP_STCHG, val, val);
846 xudc_writel(xudc, val, EP_STCHG);
849 static void ep_wait_for_stopped(struct tegra_xudc *xudc, unsigned int ep)
851 xudc_readl_poll(xudc, EP_STOPPED, BIT(ep), BIT(ep));
852 xudc_writel(xudc, BIT(ep), EP_STOPPED);
855 static void ep_wait_for_inactive(struct tegra_xudc *xudc, unsigned int ep)
857 xudc_readl_poll(xudc, EP_THREAD_ACTIVE, BIT(ep), 0);
860 static void tegra_xudc_req_done(struct tegra_xudc_ep *ep,
861 struct tegra_xudc_request *req, int status)
863 struct tegra_xudc *xudc = ep->xudc;
865 dev_dbg(xudc->dev, "completing request %p on EP %u with status %d\n",
866 req, ep->index, status);
868 if (likely(req->usb_req.status == -EINPROGRESS))
869 req->usb_req.status = status;
871 list_del_init(&req->list);
873 if (usb_endpoint_xfer_control(ep->desc)) {
874 usb_gadget_unmap_request(&xudc->gadget, &req->usb_req,
875 (xudc->setup_state ==
878 usb_gadget_unmap_request(&xudc->gadget, &req->usb_req,
879 usb_endpoint_dir_in(ep->desc));
882 spin_unlock(&xudc->lock);
883 usb_gadget_giveback_request(&ep->usb_ep, &req->usb_req);
884 spin_lock(&xudc->lock);
887 static void tegra_xudc_ep_nuke(struct tegra_xudc_ep *ep, int status)
889 struct tegra_xudc_request *req;
891 while (!list_empty(&ep->queue)) {
892 req = list_first_entry(&ep->queue, struct tegra_xudc_request,
894 tegra_xudc_req_done(ep, req, status);
898 static unsigned int ep_available_trbs(struct tegra_xudc_ep *ep)
903 if (ep->deq_ptr > ep->enq_ptr)
904 return ep->deq_ptr - ep->enq_ptr - 1;
906 return XUDC_TRANSFER_RING_SIZE - (ep->enq_ptr - ep->deq_ptr) - 2;
909 static void tegra_xudc_queue_one_trb(struct tegra_xudc_ep *ep,
910 struct tegra_xudc_request *req,
911 struct tegra_xudc_trb *trb,
914 struct tegra_xudc *xudc = ep->xudc;
918 len = min_t(size_t, XUDC_TRB_MAX_BUFFER_SIZE, req->usb_req.length -
921 buf_addr = req->usb_req.dma + req->buf_queued;
925 trb_write_data_ptr(trb, buf_addr);
927 trb_write_transfer_len(trb, len);
928 trb_write_td_size(trb, req->trbs_needed - req->trbs_queued - 1);
930 if (req->trbs_queued == req->trbs_needed - 1 ||
931 (req->need_zlp && req->trbs_queued == req->trbs_needed - 2))
932 trb_write_chain(trb, 0);
934 trb_write_chain(trb, 1);
936 trb_write_ioc(trb, ioc);
938 if (usb_endpoint_dir_out(ep->desc) ||
939 (usb_endpoint_xfer_control(ep->desc) &&
940 (xudc->setup_state == DATA_STAGE_RECV)))
941 trb_write_isp(trb, 1);
943 trb_write_isp(trb, 0);
945 if (usb_endpoint_xfer_control(ep->desc)) {
946 if (xudc->setup_state == DATA_STAGE_XFER ||
947 xudc->setup_state == DATA_STAGE_RECV)
948 trb_write_type(trb, TRB_TYPE_DATA_STAGE);
950 trb_write_type(trb, TRB_TYPE_STATUS_STAGE);
952 if (xudc->setup_state == DATA_STAGE_XFER ||
953 xudc->setup_state == STATUS_STAGE_XFER)
954 trb_write_data_stage_dir(trb, 1);
956 trb_write_data_stage_dir(trb, 0);
957 } else if (usb_endpoint_xfer_isoc(ep->desc)) {
958 trb_write_type(trb, TRB_TYPE_ISOCH);
959 trb_write_sia(trb, 1);
960 trb_write_frame_id(trb, 0);
961 trb_write_tlbpc(trb, 0);
962 } else if (usb_ss_max_streams(ep->comp_desc)) {
963 trb_write_type(trb, TRB_TYPE_STREAM);
964 trb_write_stream_id(trb, req->usb_req.stream_id);
966 trb_write_type(trb, TRB_TYPE_NORMAL);
967 trb_write_stream_id(trb, 0);
970 trb_write_cycle(trb, ep->pcs);
973 req->buf_queued += len;
975 dump_trb(xudc, "TRANSFER", trb);
978 static unsigned int tegra_xudc_queue_trbs(struct tegra_xudc_ep *ep,
979 struct tegra_xudc_request *req)
981 unsigned int i, count, available;
982 bool wait_td = false;
984 available = ep_available_trbs(ep);
985 count = req->trbs_needed - req->trbs_queued;
986 if (available < count) {
988 ep->ring_full = true;
992 * To generate zero-length packet on USB bus, SW needs schedule a
993 * standalone zero-length TD. According to HW's behavior, SW needs
994 * to schedule TDs in different ways for different endpoint types.
996 * For control endpoint:
997 * - Data stage TD (IOC = 1, CH = 0)
998 * - Ring doorbell and wait transfer event
999 * - Data stage TD for ZLP (IOC = 1, CH = 0)
1002 * For bulk and interrupt endpoints:
1003 * - Normal transfer TD (IOC = 0, CH = 0)
1004 * - Normal transfer TD for ZLP (IOC = 1, CH = 0)
1008 if (req->need_zlp && usb_endpoint_xfer_control(ep->desc) && count > 1)
1011 if (!req->first_trb)
1012 req->first_trb = &ep->transfer_ring[ep->enq_ptr];
1014 for (i = 0; i < count; i++) {
1015 struct tegra_xudc_trb *trb = &ep->transfer_ring[ep->enq_ptr];
1018 if ((i == count - 1) || (wait_td && i == count - 2))
1021 tegra_xudc_queue_one_trb(ep, req, trb, ioc);
1022 req->last_trb = trb;
1025 if (ep->enq_ptr == XUDC_TRANSFER_RING_SIZE - 1) {
1026 trb = &ep->transfer_ring[ep->enq_ptr];
1027 trb_write_cycle(trb, ep->pcs);
1039 static void tegra_xudc_ep_ring_doorbell(struct tegra_xudc_ep *ep)
1041 struct tegra_xudc *xudc = ep->xudc;
1044 if (list_empty(&ep->queue))
1047 val = DB_TARGET(ep->index);
1048 if (usb_endpoint_xfer_control(ep->desc)) {
1049 val |= DB_STREAMID(xudc->setup_seq_num);
1050 } else if (usb_ss_max_streams(ep->comp_desc) > 0) {
1051 struct tegra_xudc_request *req;
1053 /* Don't ring doorbell if the stream has been rejected. */
1054 if (ep->stream_rejected)
1057 req = list_first_entry(&ep->queue, struct tegra_xudc_request,
1059 val |= DB_STREAMID(req->usb_req.stream_id);
1062 dev_dbg(xudc->dev, "ring doorbell: %#x\n", val);
1063 xudc_writel(xudc, val, DB);
1066 static void tegra_xudc_ep_kick_queue(struct tegra_xudc_ep *ep)
1068 struct tegra_xudc_request *req;
1069 bool trbs_queued = false;
1071 list_for_each_entry(req, &ep->queue, list) {
1075 if (tegra_xudc_queue_trbs(ep, req) > 0)
1080 tegra_xudc_ep_ring_doorbell(ep);
1084 __tegra_xudc_ep_queue(struct tegra_xudc_ep *ep, struct tegra_xudc_request *req)
1086 struct tegra_xudc *xudc = ep->xudc;
1089 if (usb_endpoint_xfer_control(ep->desc) && !list_empty(&ep->queue)) {
1090 dev_err(xudc->dev, "control EP has pending transfers\n");
1094 if (usb_endpoint_xfer_control(ep->desc)) {
1095 err = usb_gadget_map_request(&xudc->gadget, &req->usb_req,
1096 (xudc->setup_state ==
1099 err = usb_gadget_map_request(&xudc->gadget, &req->usb_req,
1100 usb_endpoint_dir_in(ep->desc));
1104 dev_err(xudc->dev, "failed to map request: %d\n", err);
1108 req->first_trb = NULL;
1109 req->last_trb = NULL;
1110 req->buf_queued = 0;
1111 req->trbs_queued = 0;
1112 req->need_zlp = false;
1113 req->trbs_needed = DIV_ROUND_UP(req->usb_req.length,
1114 XUDC_TRB_MAX_BUFFER_SIZE);
1115 if (req->usb_req.length == 0)
1118 if (!usb_endpoint_xfer_isoc(ep->desc) &&
1119 req->usb_req.zero && req->usb_req.length &&
1120 ((req->usb_req.length % ep->usb_ep.maxpacket) == 0)) {
1122 req->need_zlp = true;
1125 req->usb_req.status = -EINPROGRESS;
1126 req->usb_req.actual = 0;
1128 list_add_tail(&req->list, &ep->queue);
1130 tegra_xudc_ep_kick_queue(ep);
1136 tegra_xudc_ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
1139 struct tegra_xudc_request *req;
1140 struct tegra_xudc_ep *ep;
1141 struct tegra_xudc *xudc;
1142 unsigned long flags;
1145 if (!usb_ep || !usb_req)
1148 ep = to_xudc_ep(usb_ep);
1149 req = to_xudc_req(usb_req);
1152 spin_lock_irqsave(&xudc->lock, flags);
1153 if (xudc->powergated || !ep->desc) {
1158 ret = __tegra_xudc_ep_queue(ep, req);
1160 spin_unlock_irqrestore(&xudc->lock, flags);
1165 static void squeeze_transfer_ring(struct tegra_xudc_ep *ep,
1166 struct tegra_xudc_request *req)
1168 struct tegra_xudc_trb *trb = req->first_trb;
1169 bool pcs_enq = trb_read_cycle(trb);
1173 * Clear out all the TRBs part of or after the cancelled request,
1174 * and must correct trb cycle bit to the last un-enqueued state.
1176 while (trb != &ep->transfer_ring[ep->enq_ptr]) {
1177 pcs = trb_read_cycle(trb);
1178 memset(trb, 0, sizeof(*trb));
1179 trb_write_cycle(trb, !pcs);
1182 if (trb_read_type(trb) == TRB_TYPE_LINK)
1183 trb = ep->transfer_ring;
1186 /* Requests will be re-queued at the start of the cancelled request. */
1187 ep->enq_ptr = req->first_trb - ep->transfer_ring;
1189 * Retrieve the correct cycle bit state from the first trb of
1190 * the cancelled request.
1193 ep->ring_full = false;
1194 list_for_each_entry_continue(req, &ep->queue, list) {
1195 req->usb_req.status = -EINPROGRESS;
1196 req->usb_req.actual = 0;
1198 req->first_trb = NULL;
1199 req->last_trb = NULL;
1200 req->buf_queued = 0;
1201 req->trbs_queued = 0;
1206 * Determine if the given TRB is in the range [first trb, last trb] for the
1209 static bool trb_in_request(struct tegra_xudc_ep *ep,
1210 struct tegra_xudc_request *req,
1211 struct tegra_xudc_trb *trb)
1213 dev_dbg(ep->xudc->dev, "%s: request %p -> %p; trb %p\n", __func__,
1214 req->first_trb, req->last_trb, trb);
1216 if (trb >= req->first_trb && (trb <= req->last_trb ||
1217 req->last_trb < req->first_trb))
1220 if (trb < req->first_trb && trb <= req->last_trb &&
1221 req->last_trb < req->first_trb)
1228 * Determine if the given TRB is in the range [EP enqueue pointer, first TRB)
1229 * for the given endpoint and request.
1231 static bool trb_before_request(struct tegra_xudc_ep *ep,
1232 struct tegra_xudc_request *req,
1233 struct tegra_xudc_trb *trb)
1235 struct tegra_xudc_trb *enq_trb = &ep->transfer_ring[ep->enq_ptr];
1237 dev_dbg(ep->xudc->dev, "%s: request %p -> %p; enq ptr: %p; trb %p\n",
1238 __func__, req->first_trb, req->last_trb, enq_trb, trb);
1240 if (trb < req->first_trb && (enq_trb <= trb ||
1241 req->first_trb < enq_trb))
1244 if (trb > req->first_trb && req->first_trb < enq_trb && enq_trb <= trb)
1251 __tegra_xudc_ep_dequeue(struct tegra_xudc_ep *ep,
1252 struct tegra_xudc_request *req)
1254 struct tegra_xudc *xudc = ep->xudc;
1255 struct tegra_xudc_request *r;
1256 struct tegra_xudc_trb *deq_trb;
1257 bool busy, kick_queue = false;
1260 /* Make sure the request is actually queued to this endpoint. */
1261 list_for_each_entry(r, &ep->queue, list) {
1269 /* Request hasn't been queued in the transfer ring yet. */
1270 if (!req->trbs_queued) {
1271 tegra_xudc_req_done(ep, req, -ECONNRESET);
1275 /* Halt DMA for this endpiont. */
1276 if (ep_ctx_read_state(ep->context) == EP_STATE_RUNNING) {
1277 ep_pause(xudc, ep->index);
1278 ep_wait_for_inactive(xudc, ep->index);
1281 deq_trb = trb_phys_to_virt(ep, ep_ctx_read_deq_ptr(ep->context));
1282 /* Is the hardware processing the TRB at the dequeue pointer? */
1283 busy = (trb_read_cycle(deq_trb) == ep_ctx_read_dcs(ep->context));
1285 if (trb_in_request(ep, req, deq_trb) && busy) {
1287 * Request has been partially completed or it hasn't
1288 * started processing yet.
1292 squeeze_transfer_ring(ep, req);
1294 req->usb_req.actual = ep_ctx_read_edtla(ep->context);
1295 tegra_xudc_req_done(ep, req, -ECONNRESET);
1298 /* EDTLA is > 0: request has been partially completed */
1299 if (req->usb_req.actual > 0) {
1301 * Abort the pending transfer and update the dequeue
1304 ep_ctx_write_edtla(ep->context, 0);
1305 ep_ctx_write_partial_td(ep->context, 0);
1306 ep_ctx_write_data_offset(ep->context, 0);
1308 deq_ptr = trb_virt_to_phys(ep,
1309 &ep->transfer_ring[ep->enq_ptr]);
1311 if (dma_mapping_error(xudc->dev, deq_ptr)) {
1314 ep_ctx_write_deq_ptr(ep->context, deq_ptr);
1315 ep_ctx_write_dcs(ep->context, ep->pcs);
1316 ep_reload(xudc, ep->index);
1319 } else if (trb_before_request(ep, req, deq_trb) && busy) {
1320 /* Request hasn't started processing yet. */
1321 squeeze_transfer_ring(ep, req);
1323 tegra_xudc_req_done(ep, req, -ECONNRESET);
1327 * Request has completed, but we haven't processed the
1328 * completion event yet.
1330 tegra_xudc_req_done(ep, req, -ECONNRESET);
1334 /* Resume the endpoint. */
1335 ep_unpause(xudc, ep->index);
1338 tegra_xudc_ep_kick_queue(ep);
1344 tegra_xudc_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
1346 struct tegra_xudc_request *req;
1347 struct tegra_xudc_ep *ep;
1348 struct tegra_xudc *xudc;
1349 unsigned long flags;
1352 if (!usb_ep || !usb_req)
1355 ep = to_xudc_ep(usb_ep);
1356 req = to_xudc_req(usb_req);
1359 spin_lock_irqsave(&xudc->lock, flags);
1361 if (xudc->powergated || !ep->desc) {
1366 ret = __tegra_xudc_ep_dequeue(ep, req);
1368 spin_unlock_irqrestore(&xudc->lock, flags);
1373 static int __tegra_xudc_ep_set_halt(struct tegra_xudc_ep *ep, bool halt)
1375 struct tegra_xudc *xudc = ep->xudc;
1380 if (usb_endpoint_xfer_isoc(ep->desc)) {
1381 dev_err(xudc->dev, "can't halt isoc EP\n");
1385 if (!!(xudc_readl(xudc, EP_HALT) & BIT(ep->index)) == halt) {
1386 dev_dbg(xudc->dev, "EP %u already %s\n", ep->index,
1387 halt ? "halted" : "not halted");
1392 ep_halt(xudc, ep->index);
1394 ep_ctx_write_state(ep->context, EP_STATE_DISABLED);
1396 ep_reload(xudc, ep->index);
1398 ep_ctx_write_state(ep->context, EP_STATE_RUNNING);
1399 ep_ctx_write_seq_num(ep->context, 0);
1401 ep_reload(xudc, ep->index);
1402 ep_unpause(xudc, ep->index);
1403 ep_unhalt(xudc, ep->index);
1405 tegra_xudc_ep_ring_doorbell(ep);
1411 static int tegra_xudc_ep_set_halt(struct usb_ep *usb_ep, int value)
1413 struct tegra_xudc_ep *ep;
1414 struct tegra_xudc *xudc;
1415 unsigned long flags;
1421 ep = to_xudc_ep(usb_ep);
1424 spin_lock_irqsave(&xudc->lock, flags);
1425 if (xudc->powergated) {
1430 if (value && usb_endpoint_dir_in(ep->desc) &&
1431 !list_empty(&ep->queue)) {
1432 dev_err(xudc->dev, "can't halt EP with requests pending\n");
1437 ret = __tegra_xudc_ep_set_halt(ep, value);
1439 spin_unlock_irqrestore(&xudc->lock, flags);
1444 static void tegra_xudc_ep_context_setup(struct tegra_xudc_ep *ep)
1446 const struct usb_endpoint_descriptor *desc = ep->desc;
1447 const struct usb_ss_ep_comp_descriptor *comp_desc = ep->comp_desc;
1448 struct tegra_xudc *xudc = ep->xudc;
1449 u16 maxpacket, maxburst = 0, esit = 0;
1452 maxpacket = usb_endpoint_maxp(desc) & 0x7ff;
1453 if (xudc->gadget.speed == USB_SPEED_SUPER) {
1454 if (!usb_endpoint_xfer_control(desc))
1455 maxburst = comp_desc->bMaxBurst;
1457 if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc))
1458 esit = le16_to_cpu(comp_desc->wBytesPerInterval);
1459 } else if ((xudc->gadget.speed < USB_SPEED_SUPER) &&
1460 (usb_endpoint_xfer_int(desc) ||
1461 usb_endpoint_xfer_isoc(desc))) {
1462 if (xudc->gadget.speed == USB_SPEED_HIGH) {
1463 maxburst = (usb_endpoint_maxp(desc) >> 11) & 0x3;
1464 if (maxburst == 0x3) {
1466 "invalid endpoint maxburst\n");
1470 esit = maxpacket * (maxburst + 1);
1473 memset(ep->context, 0, sizeof(*ep->context));
1475 ep_ctx_write_state(ep->context, EP_STATE_RUNNING);
1476 ep_ctx_write_interval(ep->context, desc->bInterval);
1477 if (xudc->gadget.speed == USB_SPEED_SUPER) {
1478 if (usb_endpoint_xfer_isoc(desc)) {
1479 ep_ctx_write_mult(ep->context,
1480 comp_desc->bmAttributes & 0x3);
1483 if (usb_endpoint_xfer_bulk(desc)) {
1484 ep_ctx_write_max_pstreams(ep->context,
1485 comp_desc->bmAttributes &
1487 ep_ctx_write_lsa(ep->context, 1);
1491 if (!usb_endpoint_xfer_control(desc) && usb_endpoint_dir_out(desc))
1492 val = usb_endpoint_type(desc);
1494 val = usb_endpoint_type(desc) + EP_TYPE_CONTROL;
1496 ep_ctx_write_type(ep->context, val);
1497 ep_ctx_write_cerr(ep->context, 0x3);
1498 ep_ctx_write_max_packet_size(ep->context, maxpacket);
1499 ep_ctx_write_max_burst_size(ep->context, maxburst);
1501 ep_ctx_write_deq_ptr(ep->context, ep->transfer_ring_phys);
1502 ep_ctx_write_dcs(ep->context, ep->pcs);
1504 /* Select a reasonable average TRB length based on endpoint type. */
1505 switch (usb_endpoint_type(desc)) {
1506 case USB_ENDPOINT_XFER_CONTROL:
1509 case USB_ENDPOINT_XFER_INT:
1512 case USB_ENDPOINT_XFER_BULK:
1513 case USB_ENDPOINT_XFER_ISOC:
1519 ep_ctx_write_avg_trb_len(ep->context, val);
1520 ep_ctx_write_max_esit_payload(ep->context, esit);
1522 ep_ctx_write_cerrcnt(ep->context, 0x3);
1525 static void setup_link_trb(struct tegra_xudc_ep *ep,
1526 struct tegra_xudc_trb *trb)
1528 trb_write_data_ptr(trb, ep->transfer_ring_phys);
1529 trb_write_type(trb, TRB_TYPE_LINK);
1530 trb_write_toggle_cycle(trb, 1);
1533 static int __tegra_xudc_ep_disable(struct tegra_xudc_ep *ep)
1535 struct tegra_xudc *xudc = ep->xudc;
1537 if (ep_ctx_read_state(ep->context) == EP_STATE_DISABLED) {
1538 dev_err(xudc->dev, "endpoint %u already disabled\n",
1543 ep_ctx_write_state(ep->context, EP_STATE_DISABLED);
1545 ep_reload(xudc, ep->index);
1547 tegra_xudc_ep_nuke(ep, -ESHUTDOWN);
1549 xudc->nr_enabled_eps--;
1550 if (usb_endpoint_xfer_isoc(ep->desc))
1551 xudc->nr_isoch_eps--;
1554 ep->comp_desc = NULL;
1556 memset(ep->context, 0, sizeof(*ep->context));
1558 ep_unpause(xudc, ep->index);
1559 ep_unhalt(xudc, ep->index);
1560 if (xudc_readl(xudc, EP_STOPPED) & BIT(ep->index))
1561 xudc_writel(xudc, BIT(ep->index), EP_STOPPED);
1564 * If this is the last endpoint disabled in a de-configure request,
1565 * switch back to address state.
1567 if ((xudc->device_state == USB_STATE_CONFIGURED) &&
1568 (xudc->nr_enabled_eps == 1)) {
1571 xudc->device_state = USB_STATE_ADDRESS;
1572 usb_gadget_set_state(&xudc->gadget, xudc->device_state);
1574 val = xudc_readl(xudc, CTRL);
1576 xudc_writel(xudc, val, CTRL);
1579 dev_info(xudc->dev, "ep %u disabled\n", ep->index);
1584 static int tegra_xudc_ep_disable(struct usb_ep *usb_ep)
1586 struct tegra_xudc_ep *ep;
1587 struct tegra_xudc *xudc;
1588 unsigned long flags;
1594 ep = to_xudc_ep(usb_ep);
1597 spin_lock_irqsave(&xudc->lock, flags);
1598 if (xudc->powergated) {
1603 ret = __tegra_xudc_ep_disable(ep);
1605 spin_unlock_irqrestore(&xudc->lock, flags);
1610 static int __tegra_xudc_ep_enable(struct tegra_xudc_ep *ep,
1611 const struct usb_endpoint_descriptor *desc)
1613 struct tegra_xudc *xudc = ep->xudc;
1617 if (xudc->gadget.speed == USB_SPEED_SUPER &&
1618 !usb_endpoint_xfer_control(desc) && !ep->usb_ep.comp_desc)
1621 /* Disable the EP if it is not disabled */
1622 if (ep_ctx_read_state(ep->context) != EP_STATE_DISABLED)
1623 __tegra_xudc_ep_disable(ep);
1626 ep->comp_desc = ep->usb_ep.comp_desc;
1628 if (usb_endpoint_xfer_isoc(desc)) {
1629 if (xudc->nr_isoch_eps > XUDC_MAX_ISOCH_EPS) {
1630 dev_err(xudc->dev, "too many isoch endpoints\n");
1633 xudc->nr_isoch_eps++;
1636 memset(ep->transfer_ring, 0, XUDC_TRANSFER_RING_SIZE *
1637 sizeof(*ep->transfer_ring));
1638 setup_link_trb(ep, &ep->transfer_ring[XUDC_TRANSFER_RING_SIZE - 1]);
1643 ep->ring_full = false;
1644 xudc->nr_enabled_eps++;
1646 tegra_xudc_ep_context_setup(ep);
1649 * No need to reload and un-halt EP0. This will be done automatically
1650 * once a valid SETUP packet is received.
1652 if (usb_endpoint_xfer_control(desc))
1656 * Transition to configured state once the first non-control
1657 * endpoint is enabled.
1659 if (xudc->device_state == USB_STATE_ADDRESS) {
1660 val = xudc_readl(xudc, CTRL);
1662 xudc_writel(xudc, val, CTRL);
1664 xudc->device_state = USB_STATE_CONFIGURED;
1665 usb_gadget_set_state(&xudc->gadget, xudc->device_state);
1668 if (usb_endpoint_xfer_isoc(desc)) {
1670 * Pause all bulk endpoints when enabling an isoch endpoint
1671 * to ensure the isoch endpoint is allocated enough bandwidth.
1673 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
1674 if (xudc->ep[i].desc &&
1675 usb_endpoint_xfer_bulk(xudc->ep[i].desc))
1680 ep_reload(xudc, ep->index);
1681 ep_unpause(xudc, ep->index);
1682 ep_unhalt(xudc, ep->index);
1684 if (usb_endpoint_xfer_isoc(desc)) {
1685 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
1686 if (xudc->ep[i].desc &&
1687 usb_endpoint_xfer_bulk(xudc->ep[i].desc))
1688 ep_unpause(xudc, i);
1693 dev_info(xudc->dev, "EP %u (type: %s, dir: %s) enabled\n", ep->index,
1694 usb_ep_type_string(usb_endpoint_type(ep->desc)),
1695 usb_endpoint_dir_in(ep->desc) ? "in" : "out");
1700 static int tegra_xudc_ep_enable(struct usb_ep *usb_ep,
1701 const struct usb_endpoint_descriptor *desc)
1703 struct tegra_xudc_ep *ep;
1704 struct tegra_xudc *xudc;
1705 unsigned long flags;
1708 if (!usb_ep || !desc || (desc->bDescriptorType != USB_DT_ENDPOINT))
1711 ep = to_xudc_ep(usb_ep);
1714 spin_lock_irqsave(&xudc->lock, flags);
1715 if (xudc->powergated) {
1720 ret = __tegra_xudc_ep_enable(ep, desc);
1722 spin_unlock_irqrestore(&xudc->lock, flags);
1727 static struct usb_request *
1728 tegra_xudc_ep_alloc_request(struct usb_ep *usb_ep, gfp_t gfp)
1730 struct tegra_xudc_request *req;
1732 req = kzalloc(sizeof(*req), gfp);
1736 INIT_LIST_HEAD(&req->list);
1738 return &req->usb_req;
1741 static void tegra_xudc_ep_free_request(struct usb_ep *usb_ep,
1742 struct usb_request *usb_req)
1744 struct tegra_xudc_request *req = to_xudc_req(usb_req);
1749 static struct usb_ep_ops tegra_xudc_ep_ops = {
1750 .enable = tegra_xudc_ep_enable,
1751 .disable = tegra_xudc_ep_disable,
1752 .alloc_request = tegra_xudc_ep_alloc_request,
1753 .free_request = tegra_xudc_ep_free_request,
1754 .queue = tegra_xudc_ep_queue,
1755 .dequeue = tegra_xudc_ep_dequeue,
1756 .set_halt = tegra_xudc_ep_set_halt,
1759 static int tegra_xudc_ep0_enable(struct usb_ep *usb_ep,
1760 const struct usb_endpoint_descriptor *desc)
1765 static int tegra_xudc_ep0_disable(struct usb_ep *usb_ep)
1770 static struct usb_ep_ops tegra_xudc_ep0_ops = {
1771 .enable = tegra_xudc_ep0_enable,
1772 .disable = tegra_xudc_ep0_disable,
1773 .alloc_request = tegra_xudc_ep_alloc_request,
1774 .free_request = tegra_xudc_ep_free_request,
1775 .queue = tegra_xudc_ep_queue,
1776 .dequeue = tegra_xudc_ep_dequeue,
1777 .set_halt = tegra_xudc_ep_set_halt,
1780 static int tegra_xudc_gadget_get_frame(struct usb_gadget *gadget)
1782 struct tegra_xudc *xudc = to_xudc(gadget);
1783 unsigned long flags;
1786 spin_lock_irqsave(&xudc->lock, flags);
1787 if (xudc->powergated) {
1792 ret = (xudc_readl(xudc, MFINDEX) & MFINDEX_FRAME_MASK) >>
1793 MFINDEX_FRAME_SHIFT;
1795 spin_unlock_irqrestore(&xudc->lock, flags);
1800 static void tegra_xudc_resume_device_state(struct tegra_xudc *xudc)
1805 ep_unpause_all(xudc);
1807 /* Direct link to U0. */
1808 val = xudc_readl(xudc, PORTSC);
1809 if (((val & PORTSC_PLS_MASK) >> PORTSC_PLS_SHIFT) != PORTSC_PLS_U0) {
1810 val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
1811 val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_U0);
1812 xudc_writel(xudc, val, PORTSC);
1815 if (xudc->device_state == USB_STATE_SUSPENDED) {
1816 xudc->device_state = xudc->resume_state;
1817 usb_gadget_set_state(&xudc->gadget, xudc->device_state);
1818 xudc->resume_state = 0;
1822 * Doorbells may be dropped if they are sent too soon (< ~200ns)
1823 * after unpausing the endpoint. Wait for 500ns just to be safe.
1826 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
1827 tegra_xudc_ep_ring_doorbell(&xudc->ep[i]);
1830 static int tegra_xudc_gadget_wakeup(struct usb_gadget *gadget)
1832 struct tegra_xudc *xudc = to_xudc(gadget);
1833 unsigned long flags;
1837 spin_lock_irqsave(&xudc->lock, flags);
1839 if (xudc->powergated) {
1843 val = xudc_readl(xudc, PORTPM);
1844 dev_dbg(xudc->dev, "%s: PORTPM=%#x, speed=%x\n", __func__,
1845 val, gadget->speed);
1847 if (((xudc->gadget.speed <= USB_SPEED_HIGH) &&
1848 (val & PORTPM_RWE)) ||
1849 ((xudc->gadget.speed == USB_SPEED_SUPER) &&
1850 (val & PORTPM_FRWE))) {
1851 tegra_xudc_resume_device_state(xudc);
1853 /* Send Device Notification packet. */
1854 if (xudc->gadget.speed == USB_SPEED_SUPER) {
1855 val = DEVNOTIF_LO_TYPE(DEVNOTIF_LO_TYPE_FUNCTION_WAKE)
1857 xudc_writel(xudc, 0, DEVNOTIF_HI);
1858 xudc_writel(xudc, val, DEVNOTIF_LO);
1863 dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret);
1864 spin_unlock_irqrestore(&xudc->lock, flags);
1869 static int tegra_xudc_gadget_pullup(struct usb_gadget *gadget, int is_on)
1871 struct tegra_xudc *xudc = to_xudc(gadget);
1872 unsigned long flags;
1875 pm_runtime_get_sync(xudc->dev);
1877 spin_lock_irqsave(&xudc->lock, flags);
1879 if (is_on != xudc->pullup) {
1880 val = xudc_readl(xudc, CTRL);
1884 val &= ~CTRL_ENABLE;
1885 xudc_writel(xudc, val, CTRL);
1888 xudc->pullup = is_on;
1889 dev_dbg(xudc->dev, "%s: pullup:%d", __func__, is_on);
1891 spin_unlock_irqrestore(&xudc->lock, flags);
1893 pm_runtime_put(xudc->dev);
1898 static int tegra_xudc_gadget_start(struct usb_gadget *gadget,
1899 struct usb_gadget_driver *driver)
1901 struct tegra_xudc *xudc = to_xudc(gadget);
1902 unsigned long flags;
1909 pm_runtime_get_sync(xudc->dev);
1911 spin_lock_irqsave(&xudc->lock, flags);
1918 xudc->setup_state = WAIT_FOR_SETUP;
1919 xudc->device_state = USB_STATE_DEFAULT;
1920 usb_gadget_set_state(&xudc->gadget, xudc->device_state);
1922 ret = __tegra_xudc_ep_enable(&xudc->ep[0], &tegra_xudc_ep0_desc);
1926 val = xudc_readl(xudc, CTRL);
1927 val |= CTRL_IE | CTRL_LSE;
1928 xudc_writel(xudc, val, CTRL);
1930 val = xudc_readl(xudc, PORTHALT);
1931 val |= PORTHALT_STCHG_INTR_EN;
1932 xudc_writel(xudc, val, PORTHALT);
1935 val = xudc_readl(xudc, CTRL);
1937 xudc_writel(xudc, val, CTRL);
1940 xudc->driver = driver;
1942 dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret);
1943 spin_unlock_irqrestore(&xudc->lock, flags);
1945 pm_runtime_put(xudc->dev);
1950 static int tegra_xudc_gadget_stop(struct usb_gadget *gadget)
1952 struct tegra_xudc *xudc = to_xudc(gadget);
1953 unsigned long flags;
1956 pm_runtime_get_sync(xudc->dev);
1958 spin_lock_irqsave(&xudc->lock, flags);
1960 val = xudc_readl(xudc, CTRL);
1961 val &= ~(CTRL_IE | CTRL_ENABLE);
1962 xudc_writel(xudc, val, CTRL);
1964 __tegra_xudc_ep_disable(&xudc->ep[0]);
1966 xudc->driver = NULL;
1967 dev_dbg(xudc->dev, "Gadget stopped");
1969 spin_unlock_irqrestore(&xudc->lock, flags);
1971 pm_runtime_put(xudc->dev);
1976 static int tegra_xudc_set_selfpowered(struct usb_gadget *gadget, int is_on)
1978 struct tegra_xudc *xudc = to_xudc(gadget);
1980 dev_dbg(xudc->dev, "%s: %d\n", __func__, is_on);
1981 xudc->selfpowered = !!is_on;
1986 static struct usb_gadget_ops tegra_xudc_gadget_ops = {
1987 .get_frame = tegra_xudc_gadget_get_frame,
1988 .wakeup = tegra_xudc_gadget_wakeup,
1989 .pullup = tegra_xudc_gadget_pullup,
1990 .udc_start = tegra_xudc_gadget_start,
1991 .udc_stop = tegra_xudc_gadget_stop,
1992 .set_selfpowered = tegra_xudc_set_selfpowered,
1995 static void no_op_complete(struct usb_ep *ep, struct usb_request *req)
2000 tegra_xudc_ep0_queue_status(struct tegra_xudc *xudc,
2001 void (*cmpl)(struct usb_ep *, struct usb_request *))
2003 xudc->ep0_req->usb_req.buf = NULL;
2004 xudc->ep0_req->usb_req.dma = 0;
2005 xudc->ep0_req->usb_req.length = 0;
2006 xudc->ep0_req->usb_req.complete = cmpl;
2007 xudc->ep0_req->usb_req.context = xudc;
2009 return __tegra_xudc_ep_queue(&xudc->ep[0], xudc->ep0_req);
2013 tegra_xudc_ep0_queue_data(struct tegra_xudc *xudc, void *buf, size_t len,
2014 void (*cmpl)(struct usb_ep *, struct usb_request *))
2016 xudc->ep0_req->usb_req.buf = buf;
2017 xudc->ep0_req->usb_req.length = len;
2018 xudc->ep0_req->usb_req.complete = cmpl;
2019 xudc->ep0_req->usb_req.context = xudc;
2021 return __tegra_xudc_ep_queue(&xudc->ep[0], xudc->ep0_req);
2024 static void tegra_xudc_ep0_req_done(struct tegra_xudc *xudc)
2026 switch (xudc->setup_state) {
2027 case DATA_STAGE_XFER:
2028 xudc->setup_state = STATUS_STAGE_RECV;
2029 tegra_xudc_ep0_queue_status(xudc, no_op_complete);
2031 case DATA_STAGE_RECV:
2032 xudc->setup_state = STATUS_STAGE_XFER;
2033 tegra_xudc_ep0_queue_status(xudc, no_op_complete);
2036 xudc->setup_state = WAIT_FOR_SETUP;
2041 static int tegra_xudc_ep0_delegate_req(struct tegra_xudc *xudc,
2042 struct usb_ctrlrequest *ctrl)
2046 spin_unlock(&xudc->lock);
2047 ret = xudc->driver->setup(&xudc->gadget, ctrl);
2048 spin_lock(&xudc->lock);
2053 static void set_feature_complete(struct usb_ep *ep, struct usb_request *req)
2055 struct tegra_xudc *xudc = req->context;
2057 if (xudc->test_mode_pattern) {
2058 xudc_writel(xudc, xudc->test_mode_pattern, PORT_TM);
2059 xudc->test_mode_pattern = 0;
2063 static int tegra_xudc_ep0_set_feature(struct tegra_xudc *xudc,
2064 struct usb_ctrlrequest *ctrl)
2066 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
2067 u32 feature = le16_to_cpu(ctrl->wValue);
2068 u32 index = le16_to_cpu(ctrl->wIndex);
2072 if (le16_to_cpu(ctrl->wLength) != 0)
2075 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2076 case USB_RECIP_DEVICE:
2078 case USB_DEVICE_REMOTE_WAKEUP:
2079 if ((xudc->gadget.speed == USB_SPEED_SUPER) ||
2080 (xudc->device_state == USB_STATE_DEFAULT))
2083 val = xudc_readl(xudc, PORTPM);
2089 xudc_writel(xudc, val, PORTPM);
2091 case USB_DEVICE_U1_ENABLE:
2092 case USB_DEVICE_U2_ENABLE:
2093 if ((xudc->device_state != USB_STATE_CONFIGURED) ||
2094 (xudc->gadget.speed != USB_SPEED_SUPER))
2097 val = xudc_readl(xudc, PORTPM);
2098 if ((feature == USB_DEVICE_U1_ENABLE) &&
2099 xudc->soc->u1_enable) {
2106 if ((feature == USB_DEVICE_U2_ENABLE) &&
2107 xudc->soc->u2_enable) {
2114 xudc_writel(xudc, val, PORTPM);
2116 case USB_DEVICE_TEST_MODE:
2117 if (xudc->gadget.speed != USB_SPEED_HIGH)
2123 xudc->test_mode_pattern = index >> 8;
2130 case USB_RECIP_INTERFACE:
2131 if (xudc->device_state != USB_STATE_CONFIGURED)
2135 case USB_INTRF_FUNC_SUSPEND:
2137 val = xudc_readl(xudc, PORTPM);
2139 if (index & USB_INTRF_FUNC_SUSPEND_RW)
2142 val &= ~PORTPM_FRWE;
2144 xudc_writel(xudc, val, PORTPM);
2147 return tegra_xudc_ep0_delegate_req(xudc, ctrl);
2153 case USB_RECIP_ENDPOINT:
2154 ep = (index & USB_ENDPOINT_NUMBER_MASK) * 2 +
2155 ((index & USB_DIR_IN) ? 1 : 0);
2157 if ((xudc->device_state == USB_STATE_DEFAULT) ||
2158 ((xudc->device_state == USB_STATE_ADDRESS) &&
2162 ret = __tegra_xudc_ep_set_halt(&xudc->ep[ep], set);
2170 return tegra_xudc_ep0_queue_status(xudc, set_feature_complete);
2173 static int tegra_xudc_ep0_get_status(struct tegra_xudc *xudc,
2174 struct usb_ctrlrequest *ctrl)
2176 struct tegra_xudc_ep_context *ep_ctx;
2177 u32 val, ep, index = le16_to_cpu(ctrl->wIndex);
2180 if (!(ctrl->bRequestType & USB_DIR_IN))
2183 if ((le16_to_cpu(ctrl->wValue) != 0) ||
2184 (le16_to_cpu(ctrl->wLength) != 2))
2187 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2188 case USB_RECIP_DEVICE:
2189 val = xudc_readl(xudc, PORTPM);
2191 if (xudc->selfpowered)
2192 status |= BIT(USB_DEVICE_SELF_POWERED);
2194 if ((xudc->gadget.speed < USB_SPEED_SUPER) &&
2196 status |= BIT(USB_DEVICE_REMOTE_WAKEUP);
2198 if (xudc->gadget.speed == USB_SPEED_SUPER) {
2199 if (val & PORTPM_U1E)
2200 status |= BIT(USB_DEV_STAT_U1_ENABLED);
2201 if (val & PORTPM_U2E)
2202 status |= BIT(USB_DEV_STAT_U2_ENABLED);
2205 case USB_RECIP_INTERFACE:
2206 if (xudc->gadget.speed == USB_SPEED_SUPER) {
2207 status |= USB_INTRF_STAT_FUNC_RW_CAP;
2208 val = xudc_readl(xudc, PORTPM);
2209 if (val & PORTPM_FRWE)
2210 status |= USB_INTRF_STAT_FUNC_RW;
2213 case USB_RECIP_ENDPOINT:
2214 ep = (index & USB_ENDPOINT_NUMBER_MASK) * 2 +
2215 ((index & USB_DIR_IN) ? 1 : 0);
2216 ep_ctx = &xudc->ep_context[ep];
2218 if ((xudc->device_state != USB_STATE_CONFIGURED) &&
2219 ((xudc->device_state != USB_STATE_ADDRESS) || (ep != 0)))
2222 if (ep_ctx_read_state(ep_ctx) == EP_STATE_DISABLED)
2225 if (xudc_readl(xudc, EP_HALT) & BIT(ep))
2226 status |= BIT(USB_ENDPOINT_HALT);
2232 xudc->status_buf = cpu_to_le16(status);
2233 return tegra_xudc_ep0_queue_data(xudc, &xudc->status_buf,
2234 sizeof(xudc->status_buf),
2238 static void set_sel_complete(struct usb_ep *ep, struct usb_request *req)
2240 /* Nothing to do with SEL values */
2243 static int tegra_xudc_ep0_set_sel(struct tegra_xudc *xudc,
2244 struct usb_ctrlrequest *ctrl)
2246 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
2250 if (xudc->device_state == USB_STATE_DEFAULT)
2253 if ((le16_to_cpu(ctrl->wIndex) != 0) ||
2254 (le16_to_cpu(ctrl->wValue) != 0) ||
2255 (le16_to_cpu(ctrl->wLength) != 6))
2258 return tegra_xudc_ep0_queue_data(xudc, &xudc->sel_timing,
2259 sizeof(xudc->sel_timing),
2263 static void set_isoch_delay_complete(struct usb_ep *ep, struct usb_request *req)
2265 /* Nothing to do with isoch delay */
2268 static int tegra_xudc_ep0_set_isoch_delay(struct tegra_xudc *xudc,
2269 struct usb_ctrlrequest *ctrl)
2271 u32 delay = le16_to_cpu(ctrl->wValue);
2273 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
2277 if ((delay > 65535) || (le16_to_cpu(ctrl->wIndex) != 0) ||
2278 (le16_to_cpu(ctrl->wLength) != 0))
2281 xudc->isoch_delay = delay;
2283 return tegra_xudc_ep0_queue_status(xudc, set_isoch_delay_complete);
2286 static void set_address_complete(struct usb_ep *ep, struct usb_request *req)
2288 struct tegra_xudc *xudc = req->context;
2290 if ((xudc->device_state == USB_STATE_DEFAULT) &&
2291 (xudc->dev_addr != 0)) {
2292 xudc->device_state = USB_STATE_ADDRESS;
2293 usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2294 } else if ((xudc->device_state == USB_STATE_ADDRESS) &&
2295 (xudc->dev_addr == 0)) {
2296 xudc->device_state = USB_STATE_DEFAULT;
2297 usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2301 static int tegra_xudc_ep0_set_address(struct tegra_xudc *xudc,
2302 struct usb_ctrlrequest *ctrl)
2304 struct tegra_xudc_ep *ep0 = &xudc->ep[0];
2305 u32 val, addr = le16_to_cpu(ctrl->wValue);
2307 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE |
2311 if ((addr > 127) || (le16_to_cpu(ctrl->wIndex) != 0) ||
2312 (le16_to_cpu(ctrl->wLength) != 0))
2315 if (xudc->device_state == USB_STATE_CONFIGURED)
2318 dev_dbg(xudc->dev, "set address: %u\n", addr);
2320 xudc->dev_addr = addr;
2321 val = xudc_readl(xudc, CTRL);
2322 val &= ~(CTRL_DEVADDR_MASK);
2323 val |= CTRL_DEVADDR(addr);
2324 xudc_writel(xudc, val, CTRL);
2326 ep_ctx_write_devaddr(ep0->context, addr);
2328 return tegra_xudc_ep0_queue_status(xudc, set_address_complete);
2331 static int tegra_xudc_ep0_standard_req(struct tegra_xudc *xudc,
2332 struct usb_ctrlrequest *ctrl)
2336 switch (ctrl->bRequest) {
2337 case USB_REQ_GET_STATUS:
2338 dev_dbg(xudc->dev, "USB_REQ_GET_STATUS\n");
2339 ret = tegra_xudc_ep0_get_status(xudc, ctrl);
2341 case USB_REQ_SET_ADDRESS:
2342 dev_dbg(xudc->dev, "USB_REQ_SET_ADDRESS\n");
2343 ret = tegra_xudc_ep0_set_address(xudc, ctrl);
2345 case USB_REQ_SET_SEL:
2346 dev_dbg(xudc->dev, "USB_REQ_SET_SEL\n");
2347 ret = tegra_xudc_ep0_set_sel(xudc, ctrl);
2349 case USB_REQ_SET_ISOCH_DELAY:
2350 dev_dbg(xudc->dev, "USB_REQ_SET_ISOCH_DELAY\n");
2351 ret = tegra_xudc_ep0_set_isoch_delay(xudc, ctrl);
2353 case USB_REQ_CLEAR_FEATURE:
2354 case USB_REQ_SET_FEATURE:
2355 dev_dbg(xudc->dev, "USB_REQ_CLEAR/SET_FEATURE\n");
2356 ret = tegra_xudc_ep0_set_feature(xudc, ctrl);
2358 case USB_REQ_SET_CONFIGURATION:
2359 dev_dbg(xudc->dev, "USB_REQ_SET_CONFIGURATION\n");
2361 * In theory we need to clear RUN bit before status stage of
2362 * deconfig request sent, but this seems to be causing problems.
2363 * Clear RUN once all endpoints are disabled instead.
2367 ret = tegra_xudc_ep0_delegate_req(xudc, ctrl);
2374 static void tegra_xudc_handle_ep0_setup_packet(struct tegra_xudc *xudc,
2375 struct usb_ctrlrequest *ctrl,
2380 xudc->setup_seq_num = seq_num;
2382 /* Ensure EP0 is unhalted. */
2386 * On Tegra210, setup packets with sequence numbers 0xfffe or 0xffff
2387 * are invalid. Halt EP0 until we get a valid packet.
2389 if (xudc->soc->invalid_seq_num &&
2390 (seq_num == 0xfffe || seq_num == 0xffff)) {
2391 dev_warn(xudc->dev, "invalid sequence number detected\n");
2397 xudc->setup_state = (ctrl->bRequestType & USB_DIR_IN) ?
2398 DATA_STAGE_XFER : DATA_STAGE_RECV;
2400 xudc->setup_state = STATUS_STAGE_XFER;
2402 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
2403 ret = tegra_xudc_ep0_standard_req(xudc, ctrl);
2405 ret = tegra_xudc_ep0_delegate_req(xudc, ctrl);
2408 dev_warn(xudc->dev, "setup request failed: %d\n", ret);
2409 xudc->setup_state = WAIT_FOR_SETUP;
2414 static void tegra_xudc_handle_ep0_event(struct tegra_xudc *xudc,
2415 struct tegra_xudc_trb *event)
2417 struct usb_ctrlrequest *ctrl = (struct usb_ctrlrequest *)event;
2418 u16 seq_num = trb_read_seq_num(event);
2420 if (xudc->setup_state != WAIT_FOR_SETUP) {
2422 * The controller is in the process of handling another
2423 * setup request. Queue subsequent requests and handle
2424 * the last one once the controller reports a sequence
2427 memcpy(&xudc->setup_packet.ctrl_req, ctrl, sizeof(*ctrl));
2428 xudc->setup_packet.seq_num = seq_num;
2429 xudc->queued_setup_packet = true;
2431 tegra_xudc_handle_ep0_setup_packet(xudc, ctrl, seq_num);
2435 static struct tegra_xudc_request *
2436 trb_to_request(struct tegra_xudc_ep *ep, struct tegra_xudc_trb *trb)
2438 struct tegra_xudc_request *req;
2440 list_for_each_entry(req, &ep->queue, list) {
2441 if (!req->trbs_queued)
2444 if (trb_in_request(ep, req, trb))
2451 static void tegra_xudc_handle_transfer_completion(struct tegra_xudc *xudc,
2452 struct tegra_xudc_ep *ep,
2453 struct tegra_xudc_trb *event)
2455 struct tegra_xudc_request *req;
2456 struct tegra_xudc_trb *trb;
2459 short_packet = (trb_read_cmpl_code(event) ==
2460 TRB_CMPL_CODE_SHORT_PACKET);
2462 trb = trb_phys_to_virt(ep, trb_read_data_ptr(event));
2463 req = trb_to_request(ep, trb);
2466 * TDs are complete on short packet or when the completed TRB is the
2467 * last TRB in the TD (the CHAIN bit is unset).
2469 if (req && (short_packet || (!trb_read_chain(trb) &&
2470 (req->trbs_needed == req->trbs_queued)))) {
2471 struct tegra_xudc_trb *last = req->last_trb;
2472 unsigned int residual;
2474 residual = trb_read_transfer_len(event);
2475 req->usb_req.actual = req->usb_req.length - residual;
2477 dev_dbg(xudc->dev, "bytes transferred %u / %u\n",
2478 req->usb_req.actual, req->usb_req.length);
2480 tegra_xudc_req_done(ep, req, 0);
2482 if (ep->desc && usb_endpoint_xfer_control(ep->desc))
2483 tegra_xudc_ep0_req_done(xudc);
2486 * Advance the dequeue pointer past the end of the current TD
2487 * on short packet completion.
2490 ep->deq_ptr = (last - ep->transfer_ring) + 1;
2491 if (ep->deq_ptr == XUDC_TRANSFER_RING_SIZE - 1)
2495 dev_warn(xudc->dev, "transfer event on dequeued request\n");
2499 tegra_xudc_ep_kick_queue(ep);
2502 static void tegra_xudc_handle_transfer_event(struct tegra_xudc *xudc,
2503 struct tegra_xudc_trb *event)
2505 unsigned int ep_index = trb_read_endpoint_id(event);
2506 struct tegra_xudc_ep *ep = &xudc->ep[ep_index];
2507 struct tegra_xudc_trb *trb;
2510 if (ep_ctx_read_state(ep->context) == EP_STATE_DISABLED) {
2511 dev_warn(xudc->dev, "transfer event on disabled EP %u\n",
2516 /* Update transfer ring dequeue pointer. */
2517 trb = trb_phys_to_virt(ep, trb_read_data_ptr(event));
2518 comp_code = trb_read_cmpl_code(event);
2519 if (comp_code != TRB_CMPL_CODE_BABBLE_DETECTED_ERR) {
2520 ep->deq_ptr = (trb - ep->transfer_ring) + 1;
2522 if (ep->deq_ptr == XUDC_TRANSFER_RING_SIZE - 1)
2524 ep->ring_full = false;
2527 switch (comp_code) {
2528 case TRB_CMPL_CODE_SUCCESS:
2529 case TRB_CMPL_CODE_SHORT_PACKET:
2530 tegra_xudc_handle_transfer_completion(xudc, ep, event);
2532 case TRB_CMPL_CODE_HOST_REJECTED:
2533 dev_info(xudc->dev, "stream rejected on EP %u\n", ep_index);
2535 ep->stream_rejected = true;
2537 case TRB_CMPL_CODE_PRIME_PIPE_RECEIVED:
2538 dev_info(xudc->dev, "prime pipe received on EP %u\n", ep_index);
2540 if (ep->stream_rejected) {
2541 ep->stream_rejected = false;
2543 * An EP is stopped when a stream is rejected. Wait
2544 * for the EP to report that it is stopped and then
2547 ep_wait_for_stopped(xudc, ep_index);
2549 tegra_xudc_ep_ring_doorbell(ep);
2551 case TRB_CMPL_CODE_BABBLE_DETECTED_ERR:
2553 * Wait for the EP to be stopped so the controller stops
2554 * processing doorbells.
2556 ep_wait_for_stopped(xudc, ep_index);
2557 ep->enq_ptr = ep->deq_ptr;
2558 tegra_xudc_ep_nuke(ep, -EIO);
2560 case TRB_CMPL_CODE_STREAM_NUMP_ERROR:
2561 case TRB_CMPL_CODE_CTRL_DIR_ERR:
2562 case TRB_CMPL_CODE_INVALID_STREAM_TYPE_ERR:
2563 case TRB_CMPL_CODE_RING_UNDERRUN:
2564 case TRB_CMPL_CODE_RING_OVERRUN:
2565 case TRB_CMPL_CODE_ISOCH_BUFFER_OVERRUN:
2566 case TRB_CMPL_CODE_USB_TRANS_ERR:
2567 case TRB_CMPL_CODE_TRB_ERR:
2568 dev_err(xudc->dev, "completion error %#x on EP %u\n",
2569 comp_code, ep_index);
2571 ep_halt(xudc, ep_index);
2573 case TRB_CMPL_CODE_CTRL_SEQNUM_ERR:
2574 dev_info(xudc->dev, "sequence number error\n");
2577 * Kill any queued control request and skip to the last
2578 * setup packet we received.
2580 tegra_xudc_ep_nuke(ep, -EINVAL);
2581 xudc->setup_state = WAIT_FOR_SETUP;
2582 if (!xudc->queued_setup_packet)
2585 tegra_xudc_handle_ep0_setup_packet(xudc,
2586 &xudc->setup_packet.ctrl_req,
2587 xudc->setup_packet.seq_num);
2588 xudc->queued_setup_packet = false;
2590 case TRB_CMPL_CODE_STOPPED:
2591 dev_dbg(xudc->dev, "stop completion code on EP %u\n",
2595 tegra_xudc_ep_nuke(ep, -ECONNREFUSED);
2598 dev_dbg(xudc->dev, "completion event %#x on EP %u\n",
2599 comp_code, ep_index);
2604 static void tegra_xudc_reset(struct tegra_xudc *xudc)
2606 struct tegra_xudc_ep *ep0 = &xudc->ep[0];
2610 xudc->setup_state = WAIT_FOR_SETUP;
2611 xudc->device_state = USB_STATE_DEFAULT;
2612 usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2614 ep_unpause_all(xudc);
2616 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
2617 tegra_xudc_ep_nuke(&xudc->ep[i], -ESHUTDOWN);
2620 * Reset sequence number and dequeue pointer to flush the transfer
2623 ep0->deq_ptr = ep0->enq_ptr;
2624 ep0->ring_full = false;
2626 xudc->setup_seq_num = 0;
2627 xudc->queued_setup_packet = false;
2629 ep_ctx_write_seq_num(ep0->context, xudc->setup_seq_num);
2631 deq_ptr = trb_virt_to_phys(ep0, &ep0->transfer_ring[ep0->deq_ptr]);
2633 if (!dma_mapping_error(xudc->dev, deq_ptr)) {
2634 ep_ctx_write_deq_ptr(ep0->context, deq_ptr);
2635 ep_ctx_write_dcs(ep0->context, ep0->pcs);
2638 ep_unhalt_all(xudc);
2640 ep_unpause(xudc, 0);
2643 static void tegra_xudc_port_connect(struct tegra_xudc *xudc)
2645 struct tegra_xudc_ep *ep0 = &xudc->ep[0];
2649 val = (xudc_readl(xudc, PORTSC) & PORTSC_PS_MASK) >> PORTSC_PS_SHIFT;
2652 xudc->gadget.speed = USB_SPEED_LOW;
2655 xudc->gadget.speed = USB_SPEED_FULL;
2658 xudc->gadget.speed = USB_SPEED_HIGH;
2661 xudc->gadget.speed = USB_SPEED_SUPER;
2664 xudc->gadget.speed = USB_SPEED_UNKNOWN;
2668 xudc->device_state = USB_STATE_DEFAULT;
2669 usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2671 xudc->setup_state = WAIT_FOR_SETUP;
2673 if (xudc->gadget.speed == USB_SPEED_SUPER)
2678 ep_ctx_write_max_packet_size(ep0->context, maxpacket);
2679 tegra_xudc_ep0_desc.wMaxPacketSize = cpu_to_le16(maxpacket);
2680 usb_ep_set_maxpacket_limit(&ep0->usb_ep, maxpacket);
2682 if (!xudc->soc->u1_enable) {
2683 val = xudc_readl(xudc, PORTPM);
2684 val &= ~(PORTPM_U1TIMEOUT_MASK);
2685 xudc_writel(xudc, val, PORTPM);
2688 if (!xudc->soc->u2_enable) {
2689 val = xudc_readl(xudc, PORTPM);
2690 val &= ~(PORTPM_U2TIMEOUT_MASK);
2691 xudc_writel(xudc, val, PORTPM);
2694 if (xudc->gadget.speed <= USB_SPEED_HIGH) {
2695 val = xudc_readl(xudc, PORTPM);
2696 val &= ~(PORTPM_L1S_MASK);
2697 if (xudc->soc->lpm_enable)
2698 val |= PORTPM_L1S(PORTPM_L1S_ACCEPT);
2700 val |= PORTPM_L1S(PORTPM_L1S_NYET);
2701 xudc_writel(xudc, val, PORTPM);
2704 val = xudc_readl(xudc, ST);
2706 xudc_writel(xudc, ST_RC, ST);
2709 static void tegra_xudc_port_disconnect(struct tegra_xudc *xudc)
2711 tegra_xudc_reset(xudc);
2713 if (xudc->driver && xudc->driver->disconnect) {
2714 spin_unlock(&xudc->lock);
2715 xudc->driver->disconnect(&xudc->gadget);
2716 spin_lock(&xudc->lock);
2719 xudc->device_state = USB_STATE_NOTATTACHED;
2720 usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2722 complete(&xudc->disconnect_complete);
2725 static void tegra_xudc_port_reset(struct tegra_xudc *xudc)
2727 tegra_xudc_reset(xudc);
2730 spin_unlock(&xudc->lock);
2731 usb_gadget_udc_reset(&xudc->gadget, xudc->driver);
2732 spin_lock(&xudc->lock);
2735 tegra_xudc_port_connect(xudc);
2738 static void tegra_xudc_port_suspend(struct tegra_xudc *xudc)
2740 dev_dbg(xudc->dev, "port suspend\n");
2742 xudc->resume_state = xudc->device_state;
2743 xudc->device_state = USB_STATE_SUSPENDED;
2744 usb_gadget_set_state(&xudc->gadget, xudc->device_state);
2746 if (xudc->driver->suspend) {
2747 spin_unlock(&xudc->lock);
2748 xudc->driver->suspend(&xudc->gadget);
2749 spin_lock(&xudc->lock);
2753 static void tegra_xudc_port_resume(struct tegra_xudc *xudc)
2755 dev_dbg(xudc->dev, "port resume\n");
2757 tegra_xudc_resume_device_state(xudc);
2759 if (xudc->driver->resume) {
2760 spin_unlock(&xudc->lock);
2761 xudc->driver->resume(&xudc->gadget);
2762 spin_lock(&xudc->lock);
2766 static inline void clear_port_change(struct tegra_xudc *xudc, u32 flag)
2770 val = xudc_readl(xudc, PORTSC);
2771 val &= ~PORTSC_CHANGE_MASK;
2773 xudc_writel(xudc, val, PORTSC);
2776 static void __tegra_xudc_handle_port_status(struct tegra_xudc *xudc)
2778 u32 portsc, porthalt;
2780 porthalt = xudc_readl(xudc, PORTHALT);
2781 if ((porthalt & PORTHALT_STCHG_REQ) &&
2782 (porthalt & PORTHALT_HALT_LTSSM)) {
2783 dev_dbg(xudc->dev, "STCHG_REQ, PORTHALT = %#x\n", porthalt);
2784 porthalt &= ~PORTHALT_HALT_LTSSM;
2785 xudc_writel(xudc, porthalt, PORTHALT);
2788 portsc = xudc_readl(xudc, PORTSC);
2789 if ((portsc & PORTSC_PRC) && (portsc & PORTSC_PR)) {
2790 dev_dbg(xudc->dev, "PRC, PR, PORTSC = %#x\n", portsc);
2791 clear_port_change(xudc, PORTSC_PRC | PORTSC_PED);
2792 #define TOGGLE_VBUS_WAIT_MS 100
2793 if (xudc->soc->port_reset_quirk) {
2794 schedule_delayed_work(&xudc->port_reset_war_work,
2795 msecs_to_jiffies(TOGGLE_VBUS_WAIT_MS));
2796 xudc->wait_for_sec_prc = 1;
2800 if ((portsc & PORTSC_PRC) && !(portsc & PORTSC_PR)) {
2801 dev_dbg(xudc->dev, "PRC, Not PR, PORTSC = %#x\n", portsc);
2802 clear_port_change(xudc, PORTSC_PRC | PORTSC_PED);
2803 tegra_xudc_port_reset(xudc);
2804 cancel_delayed_work(&xudc->port_reset_war_work);
2805 xudc->wait_for_sec_prc = 0;
2808 portsc = xudc_readl(xudc, PORTSC);
2809 if (portsc & PORTSC_WRC) {
2810 dev_dbg(xudc->dev, "WRC, PORTSC = %#x\n", portsc);
2811 clear_port_change(xudc, PORTSC_WRC | PORTSC_PED);
2812 if (!(xudc_readl(xudc, PORTSC) & PORTSC_WPR))
2813 tegra_xudc_port_reset(xudc);
2816 portsc = xudc_readl(xudc, PORTSC);
2817 if (portsc & PORTSC_CSC) {
2818 dev_dbg(xudc->dev, "CSC, PORTSC = %#x\n", portsc);
2819 clear_port_change(xudc, PORTSC_CSC);
2821 if (portsc & PORTSC_CCS)
2822 tegra_xudc_port_connect(xudc);
2824 tegra_xudc_port_disconnect(xudc);
2826 if (xudc->wait_csc) {
2827 cancel_delayed_work(&xudc->plc_reset_work);
2828 xudc->wait_csc = false;
2832 portsc = xudc_readl(xudc, PORTSC);
2833 if (portsc & PORTSC_PLC) {
2834 u32 pls = (portsc & PORTSC_PLS_MASK) >> PORTSC_PLS_SHIFT;
2836 dev_dbg(xudc->dev, "PLC, PORTSC = %#x\n", portsc);
2837 clear_port_change(xudc, PORTSC_PLC);
2840 tegra_xudc_port_suspend(xudc);
2843 if (xudc->gadget.speed < USB_SPEED_SUPER)
2844 tegra_xudc_port_resume(xudc);
2846 case PORTSC_PLS_RESUME:
2847 if (xudc->gadget.speed == USB_SPEED_SUPER)
2848 tegra_xudc_port_resume(xudc);
2850 case PORTSC_PLS_INACTIVE:
2851 schedule_delayed_work(&xudc->plc_reset_work,
2852 msecs_to_jiffies(TOGGLE_VBUS_WAIT_MS));
2853 xudc->wait_csc = true;
2860 if (portsc & PORTSC_CEC) {
2861 dev_warn(xudc->dev, "CEC, PORTSC = %#x\n", portsc);
2862 clear_port_change(xudc, PORTSC_CEC);
2865 dev_dbg(xudc->dev, "PORTSC = %#x\n", xudc_readl(xudc, PORTSC));
2868 static void tegra_xudc_handle_port_status(struct tegra_xudc *xudc)
2870 while ((xudc_readl(xudc, PORTSC) & PORTSC_CHANGE_MASK) ||
2871 (xudc_readl(xudc, PORTHALT) & PORTHALT_STCHG_REQ))
2872 __tegra_xudc_handle_port_status(xudc);
2875 static void tegra_xudc_handle_event(struct tegra_xudc *xudc,
2876 struct tegra_xudc_trb *event)
2878 u32 type = trb_read_type(event);
2880 dump_trb(xudc, "EVENT", event);
2883 case TRB_TYPE_PORT_STATUS_CHANGE_EVENT:
2884 tegra_xudc_handle_port_status(xudc);
2886 case TRB_TYPE_TRANSFER_EVENT:
2887 tegra_xudc_handle_transfer_event(xudc, event);
2889 case TRB_TYPE_SETUP_PACKET_EVENT:
2890 tegra_xudc_handle_ep0_event(xudc, event);
2893 dev_info(xudc->dev, "Unrecognized TRB type = %#x\n", type);
2898 static void tegra_xudc_process_event_ring(struct tegra_xudc *xudc)
2900 struct tegra_xudc_trb *event;
2904 event = xudc->event_ring[xudc->event_ring_index] +
2905 xudc->event_ring_deq_ptr;
2907 if (trb_read_cycle(event) != xudc->ccs)
2910 tegra_xudc_handle_event(xudc, event);
2912 xudc->event_ring_deq_ptr++;
2913 if (xudc->event_ring_deq_ptr == XUDC_EVENT_RING_SIZE) {
2914 xudc->event_ring_deq_ptr = 0;
2915 xudc->event_ring_index++;
2918 if (xudc->event_ring_index == XUDC_NR_EVENT_RINGS) {
2919 xudc->event_ring_index = 0;
2920 xudc->ccs = !xudc->ccs;
2924 erdp = xudc->event_ring_phys[xudc->event_ring_index] +
2925 xudc->event_ring_deq_ptr * sizeof(*event);
2927 xudc_writel(xudc, upper_32_bits(erdp), ERDPHI);
2928 xudc_writel(xudc, lower_32_bits(erdp) | ERDPLO_EHB, ERDPLO);
2931 static irqreturn_t tegra_xudc_irq(int irq, void *data)
2933 struct tegra_xudc *xudc = data;
2934 unsigned long flags;
2937 val = xudc_readl(xudc, ST);
2940 xudc_writel(xudc, ST_IP, ST);
2942 spin_lock_irqsave(&xudc->lock, flags);
2943 tegra_xudc_process_event_ring(xudc);
2944 spin_unlock_irqrestore(&xudc->lock, flags);
2949 static int tegra_xudc_alloc_ep(struct tegra_xudc *xudc, unsigned int index)
2951 struct tegra_xudc_ep *ep = &xudc->ep[index];
2955 ep->context = &xudc->ep_context[index];
2956 INIT_LIST_HEAD(&ep->queue);
2959 * EP1 would be the input endpoint corresponding to EP0, but since
2960 * EP0 is bi-directional, EP1 is unused.
2965 ep->transfer_ring = dma_pool_alloc(xudc->transfer_ring_pool,
2967 &ep->transfer_ring_phys);
2968 if (!ep->transfer_ring)
2972 snprintf(ep->name, sizeof(ep->name), "ep%u%s", index / 2,
2973 (index % 2 == 0) ? "out" : "in");
2974 ep->usb_ep.name = ep->name;
2975 usb_ep_set_maxpacket_limit(&ep->usb_ep, 1024);
2976 ep->usb_ep.max_streams = 16;
2977 ep->usb_ep.ops = &tegra_xudc_ep_ops;
2978 ep->usb_ep.caps.type_bulk = true;
2979 ep->usb_ep.caps.type_int = true;
2981 ep->usb_ep.caps.dir_in = true;
2983 ep->usb_ep.caps.dir_out = true;
2984 list_add_tail(&ep->usb_ep.ep_list, &xudc->gadget.ep_list);
2986 strscpy(ep->name, "ep0", 3);
2987 ep->usb_ep.name = ep->name;
2988 usb_ep_set_maxpacket_limit(&ep->usb_ep, 512);
2989 ep->usb_ep.ops = &tegra_xudc_ep0_ops;
2990 ep->usb_ep.caps.type_control = true;
2991 ep->usb_ep.caps.dir_in = true;
2992 ep->usb_ep.caps.dir_out = true;
2998 static void tegra_xudc_free_ep(struct tegra_xudc *xudc, unsigned int index)
3000 struct tegra_xudc_ep *ep = &xudc->ep[index];
3003 * EP1 would be the input endpoint corresponding to EP0, but since
3004 * EP0 is bi-directional, EP1 is unused.
3009 dma_pool_free(xudc->transfer_ring_pool, ep->transfer_ring,
3010 ep->transfer_ring_phys);
3013 static int tegra_xudc_alloc_eps(struct tegra_xudc *xudc)
3015 struct usb_request *req;
3020 dma_alloc_coherent(xudc->dev, XUDC_NR_EPS *
3021 sizeof(*xudc->ep_context),
3022 &xudc->ep_context_phys, GFP_KERNEL);
3023 if (!xudc->ep_context)
3026 xudc->transfer_ring_pool =
3027 dmam_pool_create(dev_name(xudc->dev), xudc->dev,
3028 XUDC_TRANSFER_RING_SIZE *
3029 sizeof(struct tegra_xudc_trb),
3030 sizeof(struct tegra_xudc_trb), 0);
3031 if (!xudc->transfer_ring_pool) {
3033 goto free_ep_context;
3036 INIT_LIST_HEAD(&xudc->gadget.ep_list);
3037 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) {
3038 err = tegra_xudc_alloc_ep(xudc, i);
3043 req = tegra_xudc_ep_alloc_request(&xudc->ep[0].usb_ep, GFP_KERNEL);
3048 xudc->ep0_req = to_xudc_req(req);
3054 tegra_xudc_free_ep(xudc, i - 1);
3056 dma_free_coherent(xudc->dev, XUDC_NR_EPS * sizeof(*xudc->ep_context),
3057 xudc->ep_context, xudc->ep_context_phys);
3061 static void tegra_xudc_init_eps(struct tegra_xudc *xudc)
3063 xudc_writel(xudc, lower_32_bits(xudc->ep_context_phys), ECPLO);
3064 xudc_writel(xudc, upper_32_bits(xudc->ep_context_phys), ECPHI);
3067 static void tegra_xudc_free_eps(struct tegra_xudc *xudc)
3071 tegra_xudc_ep_free_request(&xudc->ep[0].usb_ep,
3072 &xudc->ep0_req->usb_req);
3074 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++)
3075 tegra_xudc_free_ep(xudc, i);
3077 dma_free_coherent(xudc->dev, XUDC_NR_EPS * sizeof(*xudc->ep_context),
3078 xudc->ep_context, xudc->ep_context_phys);
3081 static int tegra_xudc_alloc_event_ring(struct tegra_xudc *xudc)
3085 for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
3086 xudc->event_ring[i] =
3087 dma_alloc_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
3088 sizeof(*xudc->event_ring[i]),
3089 &xudc->event_ring_phys[i],
3091 if (!xudc->event_ring[i])
3098 for (; i > 0; i--) {
3099 dma_free_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
3100 sizeof(*xudc->event_ring[i - 1]),
3101 xudc->event_ring[i - 1],
3102 xudc->event_ring_phys[i - 1]);
3107 static void tegra_xudc_init_event_ring(struct tegra_xudc *xudc)
3112 val = xudc_readl(xudc, SPARAM);
3113 val &= ~(SPARAM_ERSTMAX_MASK);
3114 val |= SPARAM_ERSTMAX(XUDC_NR_EVENT_RINGS);
3115 xudc_writel(xudc, val, SPARAM);
3117 for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
3118 memset(xudc->event_ring[i], 0, XUDC_EVENT_RING_SIZE *
3119 sizeof(*xudc->event_ring[i]));
3121 val = xudc_readl(xudc, ERSTSZ);
3122 val &= ~(ERSTSZ_ERSTXSZ_MASK << ERSTSZ_ERSTXSZ_SHIFT(i));
3123 val |= XUDC_EVENT_RING_SIZE << ERSTSZ_ERSTXSZ_SHIFT(i);
3124 xudc_writel(xudc, val, ERSTSZ);
3126 xudc_writel(xudc, lower_32_bits(xudc->event_ring_phys[i]),
3128 xudc_writel(xudc, upper_32_bits(xudc->event_ring_phys[i]),
3132 val = lower_32_bits(xudc->event_ring_phys[0]);
3133 xudc_writel(xudc, val, ERDPLO);
3135 xudc_writel(xudc, val, EREPLO);
3137 val = upper_32_bits(xudc->event_ring_phys[0]);
3138 xudc_writel(xudc, val, ERDPHI);
3139 xudc_writel(xudc, val, EREPHI);
3142 xudc->event_ring_index = 0;
3143 xudc->event_ring_deq_ptr = 0;
3146 static void tegra_xudc_free_event_ring(struct tegra_xudc *xudc)
3150 for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) {
3151 dma_free_coherent(xudc->dev, XUDC_EVENT_RING_SIZE *
3152 sizeof(*xudc->event_ring[i]),
3153 xudc->event_ring[i],
3154 xudc->event_ring_phys[i]);
3158 static void tegra_xudc_fpci_ipfs_init(struct tegra_xudc *xudc)
3162 if (xudc->soc->has_ipfs) {
3163 val = ipfs_readl(xudc, XUSB_DEV_CONFIGURATION_0);
3164 val |= XUSB_DEV_CONFIGURATION_0_EN_FPCI;
3165 ipfs_writel(xudc, val, XUSB_DEV_CONFIGURATION_0);
3166 usleep_range(10, 15);
3169 /* Enable bus master */
3170 val = XUSB_DEV_CFG_1_IO_SPACE_EN | XUSB_DEV_CFG_1_MEMORY_SPACE_EN |
3171 XUSB_DEV_CFG_1_BUS_MASTER_EN;
3172 fpci_writel(xudc, val, XUSB_DEV_CFG_1);
3174 /* Program BAR0 space */
3175 val = fpci_readl(xudc, XUSB_DEV_CFG_4);
3176 val &= ~(XUSB_DEV_CFG_4_BASE_ADDR_MASK);
3177 val |= xudc->phys_base & (XUSB_DEV_CFG_4_BASE_ADDR_MASK);
3179 fpci_writel(xudc, val, XUSB_DEV_CFG_4);
3180 fpci_writel(xudc, upper_32_bits(xudc->phys_base), XUSB_DEV_CFG_5);
3182 usleep_range(100, 200);
3184 if (xudc->soc->has_ipfs) {
3185 /* Enable interrupt assertion */
3186 val = ipfs_readl(xudc, XUSB_DEV_INTR_MASK_0);
3187 val |= XUSB_DEV_INTR_MASK_0_IP_INT_MASK;
3188 ipfs_writel(xudc, val, XUSB_DEV_INTR_MASK_0);
3192 static void tegra_xudc_device_params_init(struct tegra_xudc *xudc)
3196 if (xudc->soc->has_ipfs) {
3197 val = xudc_readl(xudc, BLCG);
3199 val &= ~(BLCG_DFPCI | BLCG_UFPCI | BLCG_FE |
3200 BLCG_COREPLL_PWRDN);
3201 val |= BLCG_IOPLL_0_PWRDN;
3202 val |= BLCG_IOPLL_1_PWRDN;
3203 val |= BLCG_IOPLL_2_PWRDN;
3205 xudc_writel(xudc, val, BLCG);
3208 /* Set a reasonable U3 exit timer value. */
3209 val = xudc_readl(xudc, SSPX_CORE_PADCTL4);
3210 val &= ~(SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3_MASK);
3211 val |= SSPX_CORE_PADCTL4_RXDAT_VLD_TIMEOUT_U3(0x5dc0);
3212 xudc_writel(xudc, val, SSPX_CORE_PADCTL4);
3214 /* Default ping LFPS tBurst is too large. */
3215 val = xudc_readl(xudc, SSPX_CORE_CNT0);
3216 val &= ~(SSPX_CORE_CNT0_PING_TBURST_MASK);
3217 val |= SSPX_CORE_CNT0_PING_TBURST(0xa);
3218 xudc_writel(xudc, val, SSPX_CORE_CNT0);
3220 /* Default tPortConfiguration timeout is too small. */
3221 val = xudc_readl(xudc, SSPX_CORE_CNT30);
3222 val &= ~(SSPX_CORE_CNT30_LMPITP_TIMER_MASK);
3223 val |= SSPX_CORE_CNT30_LMPITP_TIMER(0x978);
3224 xudc_writel(xudc, val, SSPX_CORE_CNT30);
3226 if (xudc->soc->lpm_enable) {
3227 /* Set L1 resume duration to 95 us. */
3228 val = xudc_readl(xudc, HSFSPI_COUNT13);
3229 val &= ~(HSFSPI_COUNT13_U2_RESUME_K_DURATION_MASK);
3230 val |= HSFSPI_COUNT13_U2_RESUME_K_DURATION(0x2c88);
3231 xudc_writel(xudc, val, HSFSPI_COUNT13);
3235 * Compliacne suite appears to be violating polling LFPS tBurst max
3236 * of 1.4us. Send 1.45us instead.
3238 val = xudc_readl(xudc, SSPX_CORE_CNT32);
3239 val &= ~(SSPX_CORE_CNT32_POLL_TBURST_MAX_MASK);
3240 val |= SSPX_CORE_CNT32_POLL_TBURST_MAX(0xb0);
3241 xudc_writel(xudc, val, SSPX_CORE_CNT32);
3243 /* Direct HS/FS port instance to RxDetect. */
3244 val = xudc_readl(xudc, CFG_DEV_FE);
3245 val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
3246 val |= CFG_DEV_FE_PORTREGSEL(CFG_DEV_FE_PORTREGSEL_HSFS_PI);
3247 xudc_writel(xudc, val, CFG_DEV_FE);
3249 val = xudc_readl(xudc, PORTSC);
3250 val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
3251 val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_RXDETECT);
3252 xudc_writel(xudc, val, PORTSC);
3254 /* Direct SS port instance to RxDetect. */
3255 val = xudc_readl(xudc, CFG_DEV_FE);
3256 val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
3257 val |= CFG_DEV_FE_PORTREGSEL_SS_PI & CFG_DEV_FE_PORTREGSEL_MASK;
3258 xudc_writel(xudc, val, CFG_DEV_FE);
3260 val = xudc_readl(xudc, PORTSC);
3261 val &= ~(PORTSC_CHANGE_MASK | PORTSC_PLS_MASK);
3262 val |= PORTSC_LWS | PORTSC_PLS(PORTSC_PLS_RXDETECT);
3263 xudc_writel(xudc, val, PORTSC);
3265 /* Restore port instance. */
3266 val = xudc_readl(xudc, CFG_DEV_FE);
3267 val &= ~(CFG_DEV_FE_PORTREGSEL_MASK);
3268 xudc_writel(xudc, val, CFG_DEV_FE);
3271 * Enable INFINITE_SS_RETRY to prevent device from entering
3272 * Disabled.Error when attached to buggy SuperSpeed hubs.
3274 val = xudc_readl(xudc, CFG_DEV_FE);
3275 val |= CFG_DEV_FE_INFINITE_SS_RETRY;
3276 xudc_writel(xudc, val, CFG_DEV_FE);
3278 /* Set interrupt moderation. */
3279 imod = XUDC_INTERRUPT_MODERATION_US * 4;
3280 val = xudc_readl(xudc, RT_IMOD);
3281 val &= ~((RT_IMOD_IMODI_MASK) | (RT_IMOD_IMODC_MASK));
3282 val |= (RT_IMOD_IMODI(imod) | RT_IMOD_IMODC(imod));
3283 xudc_writel(xudc, val, RT_IMOD);
3285 /* increase SSPI transaction timeout from 32us to 512us */
3286 val = xudc_readl(xudc, CFG_DEV_SSPI_XFER);
3287 val &= ~(CFG_DEV_SSPI_XFER_ACKTIMEOUT_MASK);
3288 val |= CFG_DEV_SSPI_XFER_ACKTIMEOUT(0xf000);
3289 xudc_writel(xudc, val, CFG_DEV_SSPI_XFER);
3292 static int tegra_xudc_phy_init(struct tegra_xudc *xudc)
3296 err = phy_init(xudc->utmi_phy);
3298 dev_err(xudc->dev, "utmi phy init failed: %d\n", err);
3302 err = phy_init(xudc->usb3_phy);
3304 dev_err(xudc->dev, "usb3 phy init failed: %d\n", err);
3311 phy_exit(xudc->utmi_phy);
3315 static void tegra_xudc_phy_exit(struct tegra_xudc *xudc)
3317 phy_exit(xudc->usb3_phy);
3318 phy_exit(xudc->utmi_phy);
3321 static const char * const tegra210_xudc_supply_names[] = {
3326 static const char * const tegra210_xudc_clock_names[] = {
3334 static const char * const tegra186_xudc_clock_names[] = {
3341 static struct tegra_xudc_soc tegra210_xudc_soc_data = {
3342 .supply_names = tegra210_xudc_supply_names,
3343 .num_supplies = ARRAY_SIZE(tegra210_xudc_supply_names),
3344 .clock_names = tegra210_xudc_clock_names,
3345 .num_clks = ARRAY_SIZE(tegra210_xudc_clock_names),
3348 .lpm_enable = false,
3349 .invalid_seq_num = true,
3351 .port_reset_quirk = true,
3355 static struct tegra_xudc_soc tegra186_xudc_soc_data = {
3356 .clock_names = tegra186_xudc_clock_names,
3357 .num_clks = ARRAY_SIZE(tegra186_xudc_clock_names),
3360 .lpm_enable = false,
3361 .invalid_seq_num = false,
3363 .port_reset_quirk = false,
3367 static const struct of_device_id tegra_xudc_of_match[] = {
3369 .compatible = "nvidia,tegra210-xudc",
3370 .data = &tegra210_xudc_soc_data
3373 .compatible = "nvidia,tegra186-xudc",
3374 .data = &tegra186_xudc_soc_data
3378 MODULE_DEVICE_TABLE(of, tegra_xudc_of_match);
3380 static void tegra_xudc_powerdomain_remove(struct tegra_xudc *xudc)
3382 if (xudc->genpd_dl_ss)
3383 device_link_del(xudc->genpd_dl_ss);
3384 if (xudc->genpd_dl_device)
3385 device_link_del(xudc->genpd_dl_device);
3386 if (xudc->genpd_dev_ss)
3387 dev_pm_domain_detach(xudc->genpd_dev_ss, true);
3388 if (xudc->genpd_dev_device)
3389 dev_pm_domain_detach(xudc->genpd_dev_device, true);
3392 static int tegra_xudc_powerdomain_init(struct tegra_xudc *xudc)
3394 struct device *dev = xudc->dev;
3397 xudc->genpd_dev_device = dev_pm_domain_attach_by_name(dev,
3399 if (IS_ERR(xudc->genpd_dev_device)) {
3400 err = PTR_ERR(xudc->genpd_dev_device);
3401 dev_err(dev, "failed to get dev pm-domain: %d\n", err);
3405 xudc->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "ss");
3406 if (IS_ERR(xudc->genpd_dev_ss)) {
3407 err = PTR_ERR(xudc->genpd_dev_ss);
3408 dev_err(dev, "failed to get superspeed pm-domain: %d\n", err);
3412 xudc->genpd_dl_device = device_link_add(dev, xudc->genpd_dev_device,
3413 DL_FLAG_PM_RUNTIME |
3415 if (!xudc->genpd_dl_device) {
3416 dev_err(dev, "adding usb device device link failed!\n");
3420 xudc->genpd_dl_ss = device_link_add(dev, xudc->genpd_dev_ss,
3421 DL_FLAG_PM_RUNTIME |
3423 if (!xudc->genpd_dl_ss) {
3424 dev_err(dev, "adding superspeed device link failed!\n");
3431 static int tegra_xudc_probe(struct platform_device *pdev)
3433 struct tegra_xudc *xudc;
3434 struct resource *res;
3438 xudc = devm_kzalloc(&pdev->dev, sizeof(*xudc), GFP_ATOMIC);
3442 xudc->dev = &pdev->dev;
3443 platform_set_drvdata(pdev, xudc);
3445 xudc->soc = of_device_get_match_data(&pdev->dev);
3449 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
3450 xudc->base = devm_ioremap_resource(&pdev->dev, res);
3451 if (IS_ERR(xudc->base))
3452 return PTR_ERR(xudc->base);
3453 xudc->phys_base = res->start;
3455 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fpci");
3456 xudc->fpci = devm_ioremap_resource(&pdev->dev, res);
3457 if (IS_ERR(xudc->fpci))
3458 return PTR_ERR(xudc->fpci);
3460 if (xudc->soc->has_ipfs) {
3461 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
3463 xudc->ipfs = devm_ioremap_resource(&pdev->dev, res);
3464 if (IS_ERR(xudc->ipfs))
3465 return PTR_ERR(xudc->ipfs);
3468 xudc->irq = platform_get_irq(pdev, 0);
3472 err = devm_request_irq(&pdev->dev, xudc->irq, tegra_xudc_irq, 0,
3473 dev_name(&pdev->dev), xudc);
3475 dev_err(xudc->dev, "failed to claim IRQ#%u: %d\n", xudc->irq,
3480 xudc->clks = devm_kcalloc(&pdev->dev, xudc->soc->num_clks,
3481 sizeof(*xudc->clks), GFP_KERNEL);
3485 for (i = 0; i < xudc->soc->num_clks; i++)
3486 xudc->clks[i].id = xudc->soc->clock_names[i];
3488 err = devm_clk_bulk_get(&pdev->dev, xudc->soc->num_clks,
3491 dev_err(xudc->dev, "failed to request clks %d\n", err);
3495 xudc->supplies = devm_kcalloc(&pdev->dev, xudc->soc->num_supplies,
3496 sizeof(*xudc->supplies), GFP_KERNEL);
3497 if (!xudc->supplies)
3500 for (i = 0; i < xudc->soc->num_supplies; i++)
3501 xudc->supplies[i].supply = xudc->soc->supply_names[i];
3503 err = devm_regulator_bulk_get(&pdev->dev, xudc->soc->num_supplies,
3506 dev_err(xudc->dev, "failed to request regulators %d\n", err);
3510 xudc->padctl = tegra_xusb_padctl_get(&pdev->dev);
3511 if (IS_ERR(xudc->padctl))
3512 return PTR_ERR(xudc->padctl);
3514 err = regulator_bulk_enable(xudc->soc->num_supplies, xudc->supplies);
3516 dev_err(xudc->dev, "failed to enable regulators %d\n", err);
3520 xudc->usb3_phy = devm_phy_optional_get(&pdev->dev, "usb3");
3521 if (IS_ERR(xudc->usb3_phy)) {
3522 err = PTR_ERR(xudc->usb3_phy);
3523 dev_err(xudc->dev, "failed to get usb3 phy: %d\n", err);
3524 goto disable_regulator;
3527 xudc->utmi_phy = devm_phy_optional_get(&pdev->dev, "usb2");
3528 if (IS_ERR(xudc->utmi_phy)) {
3529 err = PTR_ERR(xudc->utmi_phy);
3530 dev_err(xudc->dev, "failed to get usb2 phy: %d\n", err);
3531 goto disable_regulator;
3534 err = tegra_xudc_powerdomain_init(xudc);
3536 goto put_powerdomains;
3538 err = tegra_xudc_phy_init(xudc);
3540 goto put_powerdomains;
3542 err = tegra_xudc_alloc_event_ring(xudc);
3546 err = tegra_xudc_alloc_eps(xudc);
3548 goto free_event_ring;
3550 spin_lock_init(&xudc->lock);
3552 init_completion(&xudc->disconnect_complete);
3554 INIT_WORK(&xudc->usb_role_sw_work, tegra_xudc_usb_role_sw_work);
3556 INIT_DELAYED_WORK(&xudc->plc_reset_work, tegra_xudc_plc_reset_work);
3558 INIT_DELAYED_WORK(&xudc->port_reset_war_work,
3559 tegra_xudc_port_reset_war_work);
3561 /* Set the mode as device mode and this keeps phy always ON */
3562 xudc->device_mode = true;
3563 schedule_work(&xudc->usb_role_sw_work);
3565 pm_runtime_enable(&pdev->dev);
3567 xudc->gadget.ops = &tegra_xudc_gadget_ops;
3568 xudc->gadget.ep0 = &xudc->ep[0].usb_ep;
3569 xudc->gadget.name = "tegra-xudc";
3570 xudc->gadget.max_speed = USB_SPEED_SUPER;
3572 err = usb_add_gadget_udc(&pdev->dev, &xudc->gadget);
3574 dev_err(&pdev->dev, "failed to add USB gadget: %d\n", err);
3581 tegra_xudc_free_eps(xudc);
3583 tegra_xudc_free_event_ring(xudc);
3585 tegra_xudc_phy_exit(xudc);
3587 tegra_xudc_powerdomain_remove(xudc);
3589 regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
3591 tegra_xusb_padctl_put(xudc->padctl);
3596 static int tegra_xudc_remove(struct platform_device *pdev)
3598 struct tegra_xudc *xudc = platform_get_drvdata(pdev);
3600 pm_runtime_get_sync(xudc->dev);
3602 cancel_delayed_work(&xudc->plc_reset_work);
3603 cancel_work_sync(&xudc->usb_role_sw_work);
3605 usb_del_gadget_udc(&xudc->gadget);
3607 tegra_xudc_free_eps(xudc);
3608 tegra_xudc_free_event_ring(xudc);
3610 tegra_xudc_powerdomain_remove(xudc);
3612 regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
3614 phy_power_off(xudc->utmi_phy);
3615 phy_power_off(xudc->usb3_phy);
3617 tegra_xudc_phy_exit(xudc);
3619 pm_runtime_disable(xudc->dev);
3620 pm_runtime_put(xudc->dev);
3622 tegra_xusb_padctl_put(xudc->padctl);
3627 static int __maybe_unused tegra_xudc_powergate(struct tegra_xudc *xudc)
3629 unsigned long flags;
3631 dev_dbg(xudc->dev, "entering ELPG\n");
3633 spin_lock_irqsave(&xudc->lock, flags);
3635 xudc->powergated = true;
3636 xudc->saved_regs.ctrl = xudc_readl(xudc, CTRL);
3637 xudc->saved_regs.portpm = xudc_readl(xudc, PORTPM);
3638 xudc_writel(xudc, 0, CTRL);
3640 spin_unlock_irqrestore(&xudc->lock, flags);
3642 clk_bulk_disable_unprepare(xudc->soc->num_clks, xudc->clks);
3644 regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies);
3646 dev_dbg(xudc->dev, "entering ELPG done\n");
3650 static int __maybe_unused tegra_xudc_unpowergate(struct tegra_xudc *xudc)
3652 unsigned long flags;
3655 dev_dbg(xudc->dev, "exiting ELPG\n");
3657 err = regulator_bulk_enable(xudc->soc->num_supplies,
3662 err = clk_bulk_prepare_enable(xudc->soc->num_clks, xudc->clks);
3666 tegra_xudc_fpci_ipfs_init(xudc);
3668 tegra_xudc_device_params_init(xudc);
3670 tegra_xudc_init_event_ring(xudc);
3672 tegra_xudc_init_eps(xudc);
3674 xudc_writel(xudc, xudc->saved_regs.portpm, PORTPM);
3675 xudc_writel(xudc, xudc->saved_regs.ctrl, CTRL);
3677 spin_lock_irqsave(&xudc->lock, flags);
3678 xudc->powergated = false;
3679 spin_unlock_irqrestore(&xudc->lock, flags);
3681 dev_dbg(xudc->dev, "exiting ELPG done\n");
3685 static int __maybe_unused tegra_xudc_suspend(struct device *dev)
3687 struct tegra_xudc *xudc = dev_get_drvdata(dev);
3688 unsigned long flags;
3690 spin_lock_irqsave(&xudc->lock, flags);
3691 xudc->suspended = true;
3692 spin_unlock_irqrestore(&xudc->lock, flags);
3694 flush_work(&xudc->usb_role_sw_work);
3696 /* Forcibly disconnect before powergating. */
3697 tegra_xudc_device_mode_off(xudc);
3699 if (!pm_runtime_status_suspended(dev))
3700 tegra_xudc_powergate(xudc);
3702 pm_runtime_disable(dev);
3707 static int __maybe_unused tegra_xudc_resume(struct device *dev)
3709 struct tegra_xudc *xudc = dev_get_drvdata(dev);
3710 unsigned long flags;
3713 err = tegra_xudc_unpowergate(xudc);
3717 spin_lock_irqsave(&xudc->lock, flags);
3718 xudc->suspended = false;
3719 spin_unlock_irqrestore(&xudc->lock, flags);
3721 schedule_work(&xudc->usb_role_sw_work);
3723 pm_runtime_enable(dev);
3728 static int __maybe_unused tegra_xudc_runtime_suspend(struct device *dev)
3730 struct tegra_xudc *xudc = dev_get_drvdata(dev);
3732 return tegra_xudc_powergate(xudc);
3735 static int __maybe_unused tegra_xudc_runtime_resume(struct device *dev)
3737 struct tegra_xudc *xudc = dev_get_drvdata(dev);
3739 return tegra_xudc_unpowergate(xudc);
3742 static const struct dev_pm_ops tegra_xudc_pm_ops = {
3743 SET_SYSTEM_SLEEP_PM_OPS(tegra_xudc_suspend, tegra_xudc_resume)
3744 SET_RUNTIME_PM_OPS(tegra_xudc_runtime_suspend,
3745 tegra_xudc_runtime_resume, NULL)
3748 static struct platform_driver tegra_xudc_driver = {
3749 .probe = tegra_xudc_probe,
3750 .remove = tegra_xudc_remove,
3752 .name = "tegra-xudc",
3753 .pm = &tegra_xudc_pm_ops,
3754 .of_match_table = tegra_xudc_of_match,
3757 module_platform_driver(tegra_xudc_driver);
3759 MODULE_DESCRIPTION("NVIDIA Tegra XUSB Device Controller");
3760 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
3761 MODULE_AUTHOR("Hui Fu <hfu@nvidia.com>");
3762 MODULE_AUTHOR("Nagarjuna Kristam <nkristam@nvidia.com>");
3763 MODULE_LICENSE("GPL v2");