Lines Matching +full:ignore +full:- +full:power +full:- +full:on +full:- +full:sel

1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2013-2022, NVIDIA CORPORATION. All rights reserved.
12 #include <linux/dma-mapping.h>
247 return (le32_to_cpu(ctx->member) >> (shift)) & (mask); \
254 tmp = le32_to_cpu(ctx->member) & ~((mask) << (shift)); \
256 ctx->member = cpu_to_le32(tmp); \
339 return (le32_to_cpu(trb->member) >> (shift)) & (mask); \
346 tmp = le32_to_cpu(trb->member) & ~((mask) << (shift)); \
348 trb->member = cpu_to_le32(tmp); \
563 return readl(xudc->fpci + offset); in fpci_readl()
569 writel(val, xudc->fpci + offset); in fpci_writel()
574 return readl(xudc->ipfs + offset); in ipfs_readl()
580 writel(val, xudc->ipfs + offset); in ipfs_writel()
585 return readl(xudc->base + offset); in xudc_readl()
591 writel(val, xudc->base + offset); in xudc_writel()
599 return readl_poll_timeout_atomic(xudc->base + offset, regval, in xudc_readl_poll()
621 dev_dbg(xudc->dev, in dump_trb()
623 type, trb, trb->data_lo, trb->data_hi, trb->status, in dump_trb()
624 trb->control); in dump_trb()
703 pm_runtime_get_sync(xudc->dev); in tegra_xudc_device_mode_on()
705 tegra_phy_xusb_utmi_pad_power_on(xudc->curr_utmi_phy); in tegra_xudc_device_mode_on()
707 err = phy_power_on(xudc->curr_utmi_phy); in tegra_xudc_device_mode_on()
709 dev_err(xudc->dev, "UTMI power on failed: %d\n", err); in tegra_xudc_device_mode_on()
711 err = phy_power_on(xudc->curr_usb3_phy); in tegra_xudc_device_mode_on()
713 dev_err(xudc->dev, "USB3 PHY power on failed: %d\n", err); in tegra_xudc_device_mode_on()
715 dev_dbg(xudc->dev, "device mode on\n"); in tegra_xudc_device_mode_on()
717 phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG, in tegra_xudc_device_mode_on()
727 dev_dbg(xudc->dev, "device mode off\n"); in tegra_xudc_device_mode_off()
731 reinit_completion(&xudc->disconnect_complete); in tegra_xudc_device_mode_off()
733 if (xudc->soc->port_speed_quirk) in tegra_xudc_device_mode_off()
736 phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG, USB_ROLE_NONE); in tegra_xudc_device_mode_off()
742 if (xudc->soc->pls_quirk && xudc->gadget.speed == USB_SPEED_SUPER && in tegra_xudc_device_mode_off()
756 wait_for_completion(&xudc->disconnect_complete); in tegra_xudc_device_mode_off()
759 synchronize_irq(xudc->irq); in tegra_xudc_device_mode_off()
761 tegra_phy_xusb_utmi_pad_power_down(xudc->curr_utmi_phy); in tegra_xudc_device_mode_off()
763 err = phy_power_off(xudc->curr_utmi_phy); in tegra_xudc_device_mode_off()
765 dev_err(xudc->dev, "UTMI PHY power off failed: %d\n", err); in tegra_xudc_device_mode_off()
767 err = phy_power_off(xudc->curr_usb3_phy); in tegra_xudc_device_mode_off()
769 dev_err(xudc->dev, "USB3 PHY power off failed: %d\n", err); in tegra_xudc_device_mode_off()
771 pm_runtime_put(xudc->dev); in tegra_xudc_device_mode_off()
779 if (xudc->device_mode) in tegra_xudc_usb_role_sw_work()
790 for (i = 0; i < xudc->soc->num_phys; i++) { in tegra_xudc_get_phy_index()
791 if (xudc->usbphy[i] && usbphy == xudc->usbphy[i]) in tegra_xudc_get_phy_index()
795 dev_info(xudc->dev, "phy index could not be found for shared USB PHY"); in tegra_xudc_get_phy_index()
796 return -1; in tegra_xudc_get_phy_index()
807 dev_dbg(xudc->dev, "%s(): event is %d\n", __func__, usbphy->last_event); in tegra_xudc_vbus_notify()
809 if ((xudc->device_mode && usbphy->last_event == USB_EVENT_VBUS) || in tegra_xudc_vbus_notify()
810 (!xudc->device_mode && usbphy->last_event != USB_EVENT_VBUS)) { in tegra_xudc_vbus_notify()
811 dev_dbg(xudc->dev, "Same role(%d) received. Ignore", in tegra_xudc_vbus_notify()
812 xudc->device_mode); in tegra_xudc_vbus_notify()
816 xudc->device_mode = (usbphy->last_event == USB_EVENT_VBUS) ? true : in tegra_xudc_vbus_notify()
820 dev_dbg(xudc->dev, "%s(): current phy index is %d\n", __func__, in tegra_xudc_vbus_notify()
823 if (!xudc->suspended && phy_index != -1) { in tegra_xudc_vbus_notify()
824 xudc->curr_utmi_phy = xudc->utmi_phy[phy_index]; in tegra_xudc_vbus_notify()
825 xudc->curr_usb3_phy = xudc->usb3_phy[phy_index]; in tegra_xudc_vbus_notify()
826 xudc->curr_usbphy = usbphy; in tegra_xudc_vbus_notify()
827 schedule_work(&xudc->usb_role_sw_work); in tegra_xudc_vbus_notify()
840 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_plc_reset_work()
842 if (xudc->wait_csc) { in tegra_xudc_plc_reset_work()
847 dev_info(xudc->dev, "PLS = Inactive. Toggle VBUS\n"); in tegra_xudc_plc_reset_work()
848 phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG, in tegra_xudc_plc_reset_work()
850 phy_set_mode_ext(xudc->curr_utmi_phy, PHY_MODE_USB_OTG, in tegra_xudc_plc_reset_work()
853 xudc->wait_csc = false; in tegra_xudc_plc_reset_work()
857 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_plc_reset_work()
869 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_port_reset_war_work()
871 if (xudc->device_mode && xudc->wait_for_sec_prc) { in tegra_xudc_port_reset_war_work()
874 dev_dbg(xudc->dev, "pls = %x\n", pls); in tegra_xudc_port_reset_war_work()
877 dev_dbg(xudc->dev, "toggle vbus\n"); in tegra_xudc_port_reset_war_work()
880 xudc->curr_utmi_phy); in tegra_xudc_port_reset_war_work()
882 xudc->wait_for_sec_prc = 0; in tegra_xudc_port_reset_war_work()
886 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_port_reset_war_work()
894 index = trb - ep->transfer_ring; in trb_virt_to_phys()
899 return (ep->transfer_ring_phys + index * sizeof(*trb)); in trb_virt_to_phys()
908 index = (addr - ep->transfer_ring_phys) / sizeof(*trb); in trb_phys_to_virt()
913 trb = &ep->transfer_ring[index]; in trb_phys_to_virt()
1027 struct tegra_xudc *xudc = ep->xudc; in tegra_xudc_req_done()
1029 dev_dbg(xudc->dev, "completing request %p on EP %u with status %d\n", in tegra_xudc_req_done()
1030 req, ep->index, status); in tegra_xudc_req_done()
1032 if (likely(req->usb_req.status == -EINPROGRESS)) in tegra_xudc_req_done()
1033 req->usb_req.status = status; in tegra_xudc_req_done()
1035 list_del_init(&req->list); in tegra_xudc_req_done()
1037 if (usb_endpoint_xfer_control(ep->desc)) { in tegra_xudc_req_done()
1038 usb_gadget_unmap_request(&xudc->gadget, &req->usb_req, in tegra_xudc_req_done()
1039 (xudc->setup_state == in tegra_xudc_req_done()
1042 usb_gadget_unmap_request(&xudc->gadget, &req->usb_req, in tegra_xudc_req_done()
1043 usb_endpoint_dir_in(ep->desc)); in tegra_xudc_req_done()
1046 spin_unlock(&xudc->lock); in tegra_xudc_req_done()
1047 usb_gadget_giveback_request(&ep->usb_ep, &req->usb_req); in tegra_xudc_req_done()
1048 spin_lock(&xudc->lock); in tegra_xudc_req_done()
1055 while (!list_empty(&ep->queue)) { in tegra_xudc_ep_nuke()
1056 req = list_first_entry(&ep->queue, struct tegra_xudc_request, in tegra_xudc_ep_nuke()
1064 if (ep->ring_full) in ep_available_trbs()
1067 if (ep->deq_ptr > ep->enq_ptr) in ep_available_trbs()
1068 return ep->deq_ptr - ep->enq_ptr - 1; in ep_available_trbs()
1070 return XUDC_TRANSFER_RING_SIZE - (ep->enq_ptr - ep->deq_ptr) - 2; in ep_available_trbs()
1078 struct tegra_xudc *xudc = ep->xudc; in tegra_xudc_queue_one_trb()
1082 len = min_t(size_t, XUDC_TRB_MAX_BUFFER_SIZE, req->usb_req.length - in tegra_xudc_queue_one_trb()
1083 req->buf_queued); in tegra_xudc_queue_one_trb()
1085 buf_addr = req->usb_req.dma + req->buf_queued; in tegra_xudc_queue_one_trb()
1092 trb_write_td_size(trb, req->trbs_needed - req->trbs_queued - 1); in tegra_xudc_queue_one_trb()
1094 if (req->trbs_queued == req->trbs_needed - 1 || in tegra_xudc_queue_one_trb()
1095 (req->need_zlp && req->trbs_queued == req->trbs_needed - 2)) in tegra_xudc_queue_one_trb()
1102 if (usb_endpoint_dir_out(ep->desc) || in tegra_xudc_queue_one_trb()
1103 (usb_endpoint_xfer_control(ep->desc) && in tegra_xudc_queue_one_trb()
1104 (xudc->setup_state == DATA_STAGE_RECV))) in tegra_xudc_queue_one_trb()
1109 if (usb_endpoint_xfer_control(ep->desc)) { in tegra_xudc_queue_one_trb()
1110 if (xudc->setup_state == DATA_STAGE_XFER || in tegra_xudc_queue_one_trb()
1111 xudc->setup_state == DATA_STAGE_RECV) in tegra_xudc_queue_one_trb()
1116 if (xudc->setup_state == DATA_STAGE_XFER || in tegra_xudc_queue_one_trb()
1117 xudc->setup_state == STATUS_STAGE_XFER) in tegra_xudc_queue_one_trb()
1121 } else if (usb_endpoint_xfer_isoc(ep->desc)) { in tegra_xudc_queue_one_trb()
1126 } else if (usb_ss_max_streams(ep->comp_desc)) { in tegra_xudc_queue_one_trb()
1128 trb_write_stream_id(trb, req->usb_req.stream_id); in tegra_xudc_queue_one_trb()
1134 trb_write_cycle(trb, ep->pcs); in tegra_xudc_queue_one_trb()
1136 req->trbs_queued++; in tegra_xudc_queue_one_trb()
1137 req->buf_queued += len; in tegra_xudc_queue_one_trb()
1149 count = req->trbs_needed - req->trbs_queued; in tegra_xudc_queue_trbs()
1152 ep->ring_full = true; in tegra_xudc_queue_trbs()
1156 * To generate zero-length packet on USB bus, SW needs schedule a in tegra_xudc_queue_trbs()
1157 * standalone zero-length TD. According to HW's behavior, SW needs in tegra_xudc_queue_trbs()
1161 * - Data stage TD (IOC = 1, CH = 0) in tegra_xudc_queue_trbs()
1162 * - Ring doorbell and wait transfer event in tegra_xudc_queue_trbs()
1163 * - Data stage TD for ZLP (IOC = 1, CH = 0) in tegra_xudc_queue_trbs()
1164 * - Ring doorbell in tegra_xudc_queue_trbs()
1167 * - Normal transfer TD (IOC = 0, CH = 0) in tegra_xudc_queue_trbs()
1168 * - Normal transfer TD for ZLP (IOC = 1, CH = 0) in tegra_xudc_queue_trbs()
1169 * - Ring doorbell in tegra_xudc_queue_trbs()
1172 if (req->need_zlp && usb_endpoint_xfer_control(ep->desc) && count > 1) in tegra_xudc_queue_trbs()
1175 if (!req->first_trb) in tegra_xudc_queue_trbs()
1176 req->first_trb = &ep->transfer_ring[ep->enq_ptr]; in tegra_xudc_queue_trbs()
1179 struct tegra_xudc_trb *trb = &ep->transfer_ring[ep->enq_ptr]; in tegra_xudc_queue_trbs()
1182 if ((i == count - 1) || (wait_td && i == count - 2)) in tegra_xudc_queue_trbs()
1186 req->last_trb = trb; in tegra_xudc_queue_trbs()
1188 ep->enq_ptr++; in tegra_xudc_queue_trbs()
1189 if (ep->enq_ptr == XUDC_TRANSFER_RING_SIZE - 1) { in tegra_xudc_queue_trbs()
1190 trb = &ep->transfer_ring[ep->enq_ptr]; in tegra_xudc_queue_trbs()
1191 trb_write_cycle(trb, ep->pcs); in tegra_xudc_queue_trbs()
1192 ep->pcs = !ep->pcs; in tegra_xudc_queue_trbs()
1193 ep->enq_ptr = 0; in tegra_xudc_queue_trbs()
1205 struct tegra_xudc *xudc = ep->xudc; in tegra_xudc_ep_ring_doorbell()
1208 if (list_empty(&ep->queue)) in tegra_xudc_ep_ring_doorbell()
1211 val = DB_TARGET(ep->index); in tegra_xudc_ep_ring_doorbell()
1212 if (usb_endpoint_xfer_control(ep->desc)) { in tegra_xudc_ep_ring_doorbell()
1213 val |= DB_STREAMID(xudc->setup_seq_num); in tegra_xudc_ep_ring_doorbell()
1214 } else if (usb_ss_max_streams(ep->comp_desc) > 0) { in tegra_xudc_ep_ring_doorbell()
1218 if (ep->stream_rejected) in tegra_xudc_ep_ring_doorbell()
1221 req = list_first_entry(&ep->queue, struct tegra_xudc_request, in tegra_xudc_ep_ring_doorbell()
1223 val |= DB_STREAMID(req->usb_req.stream_id); in tegra_xudc_ep_ring_doorbell()
1226 dev_dbg(xudc->dev, "ring doorbell: %#x\n", val); in tegra_xudc_ep_ring_doorbell()
1235 list_for_each_entry(req, &ep->queue, list) { in tegra_xudc_ep_kick_queue()
1236 if (ep->ring_full) in tegra_xudc_ep_kick_queue()
1250 struct tegra_xudc *xudc = ep->xudc; in __tegra_xudc_ep_queue()
1253 if (usb_endpoint_xfer_control(ep->desc) && !list_empty(&ep->queue)) { in __tegra_xudc_ep_queue()
1254 dev_err(xudc->dev, "control EP has pending transfers\n"); in __tegra_xudc_ep_queue()
1255 return -EINVAL; in __tegra_xudc_ep_queue()
1258 if (usb_endpoint_xfer_control(ep->desc)) { in __tegra_xudc_ep_queue()
1259 err = usb_gadget_map_request(&xudc->gadget, &req->usb_req, in __tegra_xudc_ep_queue()
1260 (xudc->setup_state == in __tegra_xudc_ep_queue()
1263 err = usb_gadget_map_request(&xudc->gadget, &req->usb_req, in __tegra_xudc_ep_queue()
1264 usb_endpoint_dir_in(ep->desc)); in __tegra_xudc_ep_queue()
1268 dev_err(xudc->dev, "failed to map request: %d\n", err); in __tegra_xudc_ep_queue()
1272 req->first_trb = NULL; in __tegra_xudc_ep_queue()
1273 req->last_trb = NULL; in __tegra_xudc_ep_queue()
1274 req->buf_queued = 0; in __tegra_xudc_ep_queue()
1275 req->trbs_queued = 0; in __tegra_xudc_ep_queue()
1276 req->need_zlp = false; in __tegra_xudc_ep_queue()
1277 req->trbs_needed = DIV_ROUND_UP(req->usb_req.length, in __tegra_xudc_ep_queue()
1279 if (req->usb_req.length == 0) in __tegra_xudc_ep_queue()
1280 req->trbs_needed++; in __tegra_xudc_ep_queue()
1282 if (!usb_endpoint_xfer_isoc(ep->desc) && in __tegra_xudc_ep_queue()
1283 req->usb_req.zero && req->usb_req.length && in __tegra_xudc_ep_queue()
1284 ((req->usb_req.length % ep->usb_ep.maxpacket) == 0)) { in __tegra_xudc_ep_queue()
1285 req->trbs_needed++; in __tegra_xudc_ep_queue()
1286 req->need_zlp = true; in __tegra_xudc_ep_queue()
1289 req->usb_req.status = -EINPROGRESS; in __tegra_xudc_ep_queue()
1290 req->usb_req.actual = 0; in __tegra_xudc_ep_queue()
1292 list_add_tail(&req->list, &ep->queue); in __tegra_xudc_ep_queue()
1310 return -EINVAL; in tegra_xudc_ep_queue()
1314 xudc = ep->xudc; in tegra_xudc_ep_queue()
1316 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_ep_queue()
1317 if (xudc->powergated || !ep->desc) { in tegra_xudc_ep_queue()
1318 ret = -ESHUTDOWN; in tegra_xudc_ep_queue()
1324 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_ep_queue()
1332 struct tegra_xudc_trb *trb = req->first_trb; in squeeze_transfer_ring()
1338 * and must correct trb cycle bit to the last un-enqueued state. in squeeze_transfer_ring()
1340 while (trb != &ep->transfer_ring[ep->enq_ptr]) { in squeeze_transfer_ring()
1347 trb = ep->transfer_ring; in squeeze_transfer_ring()
1350 /* Requests will be re-queued at the start of the cancelled request. */ in squeeze_transfer_ring()
1351 ep->enq_ptr = req->first_trb - ep->transfer_ring; in squeeze_transfer_ring()
1356 ep->pcs = pcs_enq; in squeeze_transfer_ring()
1357 ep->ring_full = false; in squeeze_transfer_ring()
1358 list_for_each_entry_continue(req, &ep->queue, list) { in squeeze_transfer_ring()
1359 req->usb_req.status = -EINPROGRESS; in squeeze_transfer_ring()
1360 req->usb_req.actual = 0; in squeeze_transfer_ring()
1362 req->first_trb = NULL; in squeeze_transfer_ring()
1363 req->last_trb = NULL; in squeeze_transfer_ring()
1364 req->buf_queued = 0; in squeeze_transfer_ring()
1365 req->trbs_queued = 0; in squeeze_transfer_ring()
1377 dev_dbg(ep->xudc->dev, "%s: request %p -> %p; trb %p\n", __func__, in trb_in_request()
1378 req->first_trb, req->last_trb, trb); in trb_in_request()
1380 if (trb >= req->first_trb && (trb <= req->last_trb || in trb_in_request()
1381 req->last_trb < req->first_trb)) in trb_in_request()
1384 if (trb < req->first_trb && trb <= req->last_trb && in trb_in_request()
1385 req->last_trb < req->first_trb) in trb_in_request()
1399 struct tegra_xudc_trb *enq_trb = &ep->transfer_ring[ep->enq_ptr]; in trb_before_request()
1401 dev_dbg(ep->xudc->dev, "%s: request %p -> %p; enq ptr: %p; trb %p\n", in trb_before_request()
1402 __func__, req->first_trb, req->last_trb, enq_trb, trb); in trb_before_request()
1404 if (trb < req->first_trb && (enq_trb <= trb || in trb_before_request()
1405 req->first_trb < enq_trb)) in trb_before_request()
1408 if (trb > req->first_trb && req->first_trb < enq_trb && enq_trb <= trb) in trb_before_request()
1418 struct tegra_xudc *xudc = ep->xudc; in __tegra_xudc_ep_dequeue()
1425 list_for_each_entry(iter, &ep->queue, list) { in __tegra_xudc_ep_dequeue()
1433 return -EINVAL; in __tegra_xudc_ep_dequeue()
1436 if (!req->trbs_queued) { in __tegra_xudc_ep_dequeue()
1437 tegra_xudc_req_done(ep, req, -ECONNRESET); in __tegra_xudc_ep_dequeue()
1442 if (ep_ctx_read_state(ep->context) == EP_STATE_RUNNING) { in __tegra_xudc_ep_dequeue()
1443 ep_pause(xudc, ep->index); in __tegra_xudc_ep_dequeue()
1444 ep_wait_for_inactive(xudc, ep->index); in __tegra_xudc_ep_dequeue()
1447 deq_trb = trb_phys_to_virt(ep, ep_ctx_read_deq_ptr(ep->context)); in __tegra_xudc_ep_dequeue()
1449 busy = (trb_read_cycle(deq_trb) == ep_ctx_read_dcs(ep->context)); in __tegra_xudc_ep_dequeue()
1460 req->usb_req.actual = ep_ctx_read_edtla(ep->context); in __tegra_xudc_ep_dequeue()
1461 tegra_xudc_req_done(ep, req, -ECONNRESET); in __tegra_xudc_ep_dequeue()
1465 if (req->usb_req.actual > 0) { in __tegra_xudc_ep_dequeue()
1470 ep_ctx_write_edtla(ep->context, 0); in __tegra_xudc_ep_dequeue()
1471 ep_ctx_write_partial_td(ep->context, 0); in __tegra_xudc_ep_dequeue()
1472 ep_ctx_write_data_offset(ep->context, 0); in __tegra_xudc_ep_dequeue()
1475 &ep->transfer_ring[ep->enq_ptr]); in __tegra_xudc_ep_dequeue()
1477 if (dma_mapping_error(xudc->dev, deq_ptr)) { in __tegra_xudc_ep_dequeue()
1478 ret = -EINVAL; in __tegra_xudc_ep_dequeue()
1480 ep_ctx_write_deq_ptr(ep->context, deq_ptr); in __tegra_xudc_ep_dequeue()
1481 ep_ctx_write_dcs(ep->context, ep->pcs); in __tegra_xudc_ep_dequeue()
1482 ep_reload(xudc, ep->index); in __tegra_xudc_ep_dequeue()
1489 tegra_xudc_req_done(ep, req, -ECONNRESET); in __tegra_xudc_ep_dequeue()
1496 tegra_xudc_req_done(ep, req, -ECONNRESET); in __tegra_xudc_ep_dequeue()
1497 ret = -EINVAL; in __tegra_xudc_ep_dequeue()
1501 ep_unpause(xudc, ep->index); in __tegra_xudc_ep_dequeue()
1519 return -EINVAL; in tegra_xudc_ep_dequeue()
1523 xudc = ep->xudc; in tegra_xudc_ep_dequeue()
1525 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_ep_dequeue()
1527 if (xudc->powergated || !ep->desc) { in tegra_xudc_ep_dequeue()
1528 ret = -ESHUTDOWN; in tegra_xudc_ep_dequeue()
1534 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_ep_dequeue()
1541 struct tegra_xudc *xudc = ep->xudc; in __tegra_xudc_ep_set_halt()
1543 if (!ep->desc) in __tegra_xudc_ep_set_halt()
1544 return -EINVAL; in __tegra_xudc_ep_set_halt()
1546 if (usb_endpoint_xfer_isoc(ep->desc)) { in __tegra_xudc_ep_set_halt()
1547 dev_err(xudc->dev, "can't halt isochronous EP\n"); in __tegra_xudc_ep_set_halt()
1548 return -ENOTSUPP; in __tegra_xudc_ep_set_halt()
1551 if (!!(xudc_readl(xudc, EP_HALT) & BIT(ep->index)) == halt) { in __tegra_xudc_ep_set_halt()
1552 dev_dbg(xudc->dev, "EP %u already %s\n", ep->index, in __tegra_xudc_ep_set_halt()
1558 ep_halt(xudc, ep->index); in __tegra_xudc_ep_set_halt()
1560 ep_ctx_write_state(ep->context, EP_STATE_DISABLED); in __tegra_xudc_ep_set_halt()
1562 ep_reload(xudc, ep->index); in __tegra_xudc_ep_set_halt()
1564 ep_ctx_write_state(ep->context, EP_STATE_RUNNING); in __tegra_xudc_ep_set_halt()
1565 ep_ctx_write_rsvd(ep->context, 0); in __tegra_xudc_ep_set_halt()
1566 ep_ctx_write_partial_td(ep->context, 0); in __tegra_xudc_ep_set_halt()
1567 ep_ctx_write_splitxstate(ep->context, 0); in __tegra_xudc_ep_set_halt()
1568 ep_ctx_write_seq_num(ep->context, 0); in __tegra_xudc_ep_set_halt()
1570 ep_reload(xudc, ep->index); in __tegra_xudc_ep_set_halt()
1571 ep_unpause(xudc, ep->index); in __tegra_xudc_ep_set_halt()
1572 ep_unhalt(xudc, ep->index); in __tegra_xudc_ep_set_halt()
1588 return -EINVAL; in tegra_xudc_ep_set_halt()
1591 xudc = ep->xudc; in tegra_xudc_ep_set_halt()
1593 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_ep_set_halt()
1594 if (xudc->powergated) { in tegra_xudc_ep_set_halt()
1595 ret = -ESHUTDOWN; in tegra_xudc_ep_set_halt()
1599 if (value && usb_endpoint_dir_in(ep->desc) && in tegra_xudc_ep_set_halt()
1600 !list_empty(&ep->queue)) { in tegra_xudc_ep_set_halt()
1601 dev_err(xudc->dev, "can't halt EP with requests pending\n"); in tegra_xudc_ep_set_halt()
1602 ret = -EAGAIN; in tegra_xudc_ep_set_halt()
1608 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_ep_set_halt()
1615 const struct usb_endpoint_descriptor *desc = ep->desc; in tegra_xudc_ep_context_setup()
1616 const struct usb_ss_ep_comp_descriptor *comp_desc = ep->comp_desc; in tegra_xudc_ep_context_setup()
1617 struct tegra_xudc *xudc = ep->xudc; in tegra_xudc_ep_context_setup()
1622 if (xudc->gadget.speed == USB_SPEED_SUPER) { in tegra_xudc_ep_context_setup()
1624 maxburst = comp_desc->bMaxBurst; in tegra_xudc_ep_context_setup()
1627 esit = le16_to_cpu(comp_desc->wBytesPerInterval); in tegra_xudc_ep_context_setup()
1628 } else if ((xudc->gadget.speed < USB_SPEED_SUPER) && in tegra_xudc_ep_context_setup()
1631 if (xudc->gadget.speed == USB_SPEED_HIGH) { in tegra_xudc_ep_context_setup()
1632 maxburst = usb_endpoint_maxp_mult(desc) - 1; in tegra_xudc_ep_context_setup()
1634 dev_warn(xudc->dev, in tegra_xudc_ep_context_setup()
1642 memset(ep->context, 0, sizeof(*ep->context)); in tegra_xudc_ep_context_setup()
1644 ep_ctx_write_state(ep->context, EP_STATE_RUNNING); in tegra_xudc_ep_context_setup()
1645 ep_ctx_write_interval(ep->context, desc->bInterval); in tegra_xudc_ep_context_setup()
1646 if (xudc->gadget.speed == USB_SPEED_SUPER) { in tegra_xudc_ep_context_setup()
1648 ep_ctx_write_mult(ep->context, in tegra_xudc_ep_context_setup()
1649 comp_desc->bmAttributes & 0x3); in tegra_xudc_ep_context_setup()
1653 ep_ctx_write_max_pstreams(ep->context, in tegra_xudc_ep_context_setup()
1654 comp_desc->bmAttributes & in tegra_xudc_ep_context_setup()
1656 ep_ctx_write_lsa(ep->context, 1); in tegra_xudc_ep_context_setup()
1665 ep_ctx_write_type(ep->context, val); in tegra_xudc_ep_context_setup()
1666 ep_ctx_write_cerr(ep->context, 0x3); in tegra_xudc_ep_context_setup()
1667 ep_ctx_write_max_packet_size(ep->context, maxpacket); in tegra_xudc_ep_context_setup()
1668 ep_ctx_write_max_burst_size(ep->context, maxburst); in tegra_xudc_ep_context_setup()
1670 ep_ctx_write_deq_ptr(ep->context, ep->transfer_ring_phys); in tegra_xudc_ep_context_setup()
1671 ep_ctx_write_dcs(ep->context, ep->pcs); in tegra_xudc_ep_context_setup()
1673 /* Select a reasonable average TRB length based on endpoint type. */ in tegra_xudc_ep_context_setup()
1688 ep_ctx_write_avg_trb_len(ep->context, val); in tegra_xudc_ep_context_setup()
1689 ep_ctx_write_max_esit_payload(ep->context, esit); in tegra_xudc_ep_context_setup()
1691 ep_ctx_write_cerrcnt(ep->context, 0x3); in tegra_xudc_ep_context_setup()
1697 trb_write_data_ptr(trb, ep->transfer_ring_phys); in setup_link_trb()
1704 struct tegra_xudc *xudc = ep->xudc; in __tegra_xudc_ep_disable()
1706 if (ep_ctx_read_state(ep->context) == EP_STATE_DISABLED) { in __tegra_xudc_ep_disable()
1707 dev_err(xudc->dev, "endpoint %u already disabled\n", in __tegra_xudc_ep_disable()
1708 ep->index); in __tegra_xudc_ep_disable()
1709 return -EINVAL; in __tegra_xudc_ep_disable()
1712 ep_ctx_write_state(ep->context, EP_STATE_DISABLED); in __tegra_xudc_ep_disable()
1714 ep_reload(xudc, ep->index); in __tegra_xudc_ep_disable()
1716 tegra_xudc_ep_nuke(ep, -ESHUTDOWN); in __tegra_xudc_ep_disable()
1718 xudc->nr_enabled_eps--; in __tegra_xudc_ep_disable()
1719 if (usb_endpoint_xfer_isoc(ep->desc)) in __tegra_xudc_ep_disable()
1720 xudc->nr_isoch_eps--; in __tegra_xudc_ep_disable()
1722 ep->desc = NULL; in __tegra_xudc_ep_disable()
1723 ep->comp_desc = NULL; in __tegra_xudc_ep_disable()
1725 memset(ep->context, 0, sizeof(*ep->context)); in __tegra_xudc_ep_disable()
1727 ep_unpause(xudc, ep->index); in __tegra_xudc_ep_disable()
1728 ep_unhalt(xudc, ep->index); in __tegra_xudc_ep_disable()
1729 if (xudc_readl(xudc, EP_STOPPED) & BIT(ep->index)) in __tegra_xudc_ep_disable()
1730 xudc_writel(xudc, BIT(ep->index), EP_STOPPED); in __tegra_xudc_ep_disable()
1733 * If this is the last endpoint disabled in a de-configure request, in __tegra_xudc_ep_disable()
1736 if ((xudc->device_state == USB_STATE_CONFIGURED) && in __tegra_xudc_ep_disable()
1737 (xudc->nr_enabled_eps == 1)) { in __tegra_xudc_ep_disable()
1740 xudc->device_state = USB_STATE_ADDRESS; in __tegra_xudc_ep_disable()
1741 usb_gadget_set_state(&xudc->gadget, xudc->device_state); in __tegra_xudc_ep_disable()
1748 dev_info(xudc->dev, "ep %u disabled\n", ep->index); in __tegra_xudc_ep_disable()
1761 return -EINVAL; in tegra_xudc_ep_disable()
1764 xudc = ep->xudc; in tegra_xudc_ep_disable()
1766 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_ep_disable()
1767 if (xudc->powergated) { in tegra_xudc_ep_disable()
1768 ret = -ESHUTDOWN; in tegra_xudc_ep_disable()
1774 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_ep_disable()
1782 struct tegra_xudc *xudc = ep->xudc; in __tegra_xudc_ep_enable()
1786 if (xudc->gadget.speed == USB_SPEED_SUPER && in __tegra_xudc_ep_enable()
1787 !usb_endpoint_xfer_control(desc) && !ep->usb_ep.comp_desc) in __tegra_xudc_ep_enable()
1788 return -EINVAL; in __tegra_xudc_ep_enable()
1791 if (ep_ctx_read_state(ep->context) != EP_STATE_DISABLED) in __tegra_xudc_ep_enable()
1794 ep->desc = desc; in __tegra_xudc_ep_enable()
1795 ep->comp_desc = ep->usb_ep.comp_desc; in __tegra_xudc_ep_enable()
1798 if (xudc->nr_isoch_eps > XUDC_MAX_ISOCH_EPS) { in __tegra_xudc_ep_enable()
1799 dev_err(xudc->dev, "too many isochronous endpoints\n"); in __tegra_xudc_ep_enable()
1800 return -EBUSY; in __tegra_xudc_ep_enable()
1802 xudc->nr_isoch_eps++; in __tegra_xudc_ep_enable()
1805 memset(ep->transfer_ring, 0, XUDC_TRANSFER_RING_SIZE * in __tegra_xudc_ep_enable()
1806 sizeof(*ep->transfer_ring)); in __tegra_xudc_ep_enable()
1807 setup_link_trb(ep, &ep->transfer_ring[XUDC_TRANSFER_RING_SIZE - 1]); in __tegra_xudc_ep_enable()
1809 ep->enq_ptr = 0; in __tegra_xudc_ep_enable()
1810 ep->deq_ptr = 0; in __tegra_xudc_ep_enable()
1811 ep->pcs = true; in __tegra_xudc_ep_enable()
1812 ep->ring_full = false; in __tegra_xudc_ep_enable()
1813 xudc->nr_enabled_eps++; in __tegra_xudc_ep_enable()
1818 * No need to reload and un-halt EP0. This will be done automatically in __tegra_xudc_ep_enable()
1825 * Transition to configured state once the first non-control in __tegra_xudc_ep_enable()
1828 if (xudc->device_state == USB_STATE_ADDRESS) { in __tegra_xudc_ep_enable()
1833 xudc->device_state = USB_STATE_CONFIGURED; in __tegra_xudc_ep_enable()
1834 usb_gadget_set_state(&xudc->gadget, xudc->device_state); in __tegra_xudc_ep_enable()
1842 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) { in __tegra_xudc_ep_enable()
1843 if (xudc->ep[i].desc && in __tegra_xudc_ep_enable()
1844 usb_endpoint_xfer_bulk(xudc->ep[i].desc)) in __tegra_xudc_ep_enable()
1849 ep_reload(xudc, ep->index); in __tegra_xudc_ep_enable()
1850 ep_unpause(xudc, ep->index); in __tegra_xudc_ep_enable()
1851 ep_unhalt(xudc, ep->index); in __tegra_xudc_ep_enable()
1854 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) { in __tegra_xudc_ep_enable()
1855 if (xudc->ep[i].desc && in __tegra_xudc_ep_enable()
1856 usb_endpoint_xfer_bulk(xudc->ep[i].desc)) in __tegra_xudc_ep_enable()
1862 dev_info(xudc->dev, "EP %u (type: %s, dir: %s) enabled\n", ep->index, in __tegra_xudc_ep_enable()
1863 usb_ep_type_string(usb_endpoint_type(ep->desc)), in __tegra_xudc_ep_enable()
1864 usb_endpoint_dir_in(ep->desc) ? "in" : "out"); in __tegra_xudc_ep_enable()
1877 if (!usb_ep || !desc || (desc->bDescriptorType != USB_DT_ENDPOINT)) in tegra_xudc_ep_enable()
1878 return -EINVAL; in tegra_xudc_ep_enable()
1881 xudc = ep->xudc; in tegra_xudc_ep_enable()
1883 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_ep_enable()
1884 if (xudc->powergated) { in tegra_xudc_ep_enable()
1885 ret = -ESHUTDOWN; in tegra_xudc_ep_enable()
1891 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_ep_enable()
1905 INIT_LIST_HEAD(&req->list); in tegra_xudc_ep_alloc_request()
1907 return &req->usb_req; in tegra_xudc_ep_alloc_request()
1931 return -EBUSY; in tegra_xudc_ep0_enable()
1936 return -EBUSY; in tegra_xudc_ep0_disable()
1955 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_gadget_get_frame()
1956 if (xudc->powergated) { in tegra_xudc_gadget_get_frame()
1957 ret = -ESHUTDOWN; in tegra_xudc_gadget_get_frame()
1964 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_gadget_get_frame()
1984 if (xudc->device_state == USB_STATE_SUSPENDED) { in tegra_xudc_resume_device_state()
1985 xudc->device_state = xudc->resume_state; in tegra_xudc_resume_device_state()
1986 usb_gadget_set_state(&xudc->gadget, xudc->device_state); in tegra_xudc_resume_device_state()
1987 xudc->resume_state = 0; in tegra_xudc_resume_device_state()
1995 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) in tegra_xudc_resume_device_state()
1996 tegra_xudc_ep_ring_doorbell(&xudc->ep[i]); in tegra_xudc_resume_device_state()
2006 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_gadget_wakeup()
2008 if (xudc->powergated) { in tegra_xudc_gadget_wakeup()
2009 ret = -ESHUTDOWN; in tegra_xudc_gadget_wakeup()
2013 dev_dbg(xudc->dev, "%s: PORTPM=%#x, speed=%x\n", __func__, in tegra_xudc_gadget_wakeup()
2014 val, gadget->speed); in tegra_xudc_gadget_wakeup()
2016 if (((xudc->gadget.speed <= USB_SPEED_HIGH) && in tegra_xudc_gadget_wakeup()
2018 ((xudc->gadget.speed == USB_SPEED_SUPER) && in tegra_xudc_gadget_wakeup()
2023 if (xudc->gadget.speed == USB_SPEED_SUPER) { in tegra_xudc_gadget_wakeup()
2032 dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret); in tegra_xudc_gadget_wakeup()
2033 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_gadget_wakeup()
2044 pm_runtime_get_sync(xudc->dev); in tegra_xudc_gadget_pullup()
2046 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_gadget_pullup()
2048 if (is_on != xudc->pullup) { in tegra_xudc_gadget_pullup()
2057 xudc->pullup = is_on; in tegra_xudc_gadget_pullup()
2058 dev_dbg(xudc->dev, "%s: pullup:%d", __func__, is_on); in tegra_xudc_gadget_pullup()
2060 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_gadget_pullup()
2062 pm_runtime_put(xudc->dev); in tegra_xudc_gadget_pullup()
2077 return -EINVAL; in tegra_xudc_gadget_start()
2079 pm_runtime_get_sync(xudc->dev); in tegra_xudc_gadget_start()
2081 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_gadget_start()
2083 if (xudc->driver) { in tegra_xudc_gadget_start()
2084 ret = -EBUSY; in tegra_xudc_gadget_start()
2088 xudc->setup_state = WAIT_FOR_SETUP; in tegra_xudc_gadget_start()
2089 xudc->device_state = USB_STATE_DEFAULT; in tegra_xudc_gadget_start()
2090 usb_gadget_set_state(&xudc->gadget, xudc->device_state); in tegra_xudc_gadget_start()
2092 ret = __tegra_xudc_ep_enable(&xudc->ep[0], &tegra_xudc_ep0_desc); in tegra_xudc_gadget_start()
2104 if (xudc->pullup) { in tegra_xudc_gadget_start()
2110 for (i = 0; i < xudc->soc->num_phys; i++) in tegra_xudc_gadget_start()
2111 if (xudc->usbphy[i]) in tegra_xudc_gadget_start()
2112 otg_set_peripheral(xudc->usbphy[i]->otg, gadget); in tegra_xudc_gadget_start()
2114 xudc->driver = driver; in tegra_xudc_gadget_start()
2116 dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret); in tegra_xudc_gadget_start()
2117 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_gadget_start()
2119 pm_runtime_put(xudc->dev); in tegra_xudc_gadget_start()
2131 pm_runtime_get_sync(xudc->dev); in tegra_xudc_gadget_stop()
2133 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_gadget_stop()
2135 for (i = 0; i < xudc->soc->num_phys; i++) in tegra_xudc_gadget_stop()
2136 if (xudc->usbphy[i]) in tegra_xudc_gadget_stop()
2137 otg_set_peripheral(xudc->usbphy[i]->otg, NULL); in tegra_xudc_gadget_stop()
2143 __tegra_xudc_ep_disable(&xudc->ep[0]); in tegra_xudc_gadget_stop()
2145 xudc->driver = NULL; in tegra_xudc_gadget_stop()
2146 dev_dbg(xudc->dev, "Gadget stopped"); in tegra_xudc_gadget_stop()
2148 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_gadget_stop()
2150 pm_runtime_put(xudc->dev); in tegra_xudc_gadget_stop()
2161 dev_dbg(xudc->dev, "%s: %u mA\n", __func__, m_a); in tegra_xudc_gadget_vbus_draw()
2163 if (xudc->curr_usbphy->chg_type == SDP_TYPE) in tegra_xudc_gadget_vbus_draw()
2164 ret = usb_phy_set_power(xudc->curr_usbphy, m_a); in tegra_xudc_gadget_vbus_draw()
2173 dev_dbg(xudc->dev, "%s: %d\n", __func__, is_on); in tegra_xudc_set_selfpowered()
2174 xudc->selfpowered = !!is_on; in tegra_xudc_set_selfpowered()
2197 xudc->ep0_req->usb_req.buf = NULL; in tegra_xudc_ep0_queue_status()
2198 xudc->ep0_req->usb_req.dma = 0; in tegra_xudc_ep0_queue_status()
2199 xudc->ep0_req->usb_req.length = 0; in tegra_xudc_ep0_queue_status()
2200 xudc->ep0_req->usb_req.complete = cmpl; in tegra_xudc_ep0_queue_status()
2201 xudc->ep0_req->usb_req.context = xudc; in tegra_xudc_ep0_queue_status()
2203 return __tegra_xudc_ep_queue(&xudc->ep[0], xudc->ep0_req); in tegra_xudc_ep0_queue_status()
2210 xudc->ep0_req->usb_req.buf = buf; in tegra_xudc_ep0_queue_data()
2211 xudc->ep0_req->usb_req.length = len; in tegra_xudc_ep0_queue_data()
2212 xudc->ep0_req->usb_req.complete = cmpl; in tegra_xudc_ep0_queue_data()
2213 xudc->ep0_req->usb_req.context = xudc; in tegra_xudc_ep0_queue_data()
2215 return __tegra_xudc_ep_queue(&xudc->ep[0], xudc->ep0_req); in tegra_xudc_ep0_queue_data()
2220 switch (xudc->setup_state) { in tegra_xudc_ep0_req_done()
2222 xudc->setup_state = STATUS_STAGE_RECV; in tegra_xudc_ep0_req_done()
2226 xudc->setup_state = STATUS_STAGE_XFER; in tegra_xudc_ep0_req_done()
2230 xudc->setup_state = WAIT_FOR_SETUP; in tegra_xudc_ep0_req_done()
2240 spin_unlock(&xudc->lock); in tegra_xudc_ep0_delegate_req()
2241 ret = xudc->driver->setup(&xudc->gadget, ctrl); in tegra_xudc_ep0_delegate_req()
2242 spin_lock(&xudc->lock); in tegra_xudc_ep0_delegate_req()
2249 struct tegra_xudc *xudc = req->context; in set_feature_complete()
2251 if (xudc->test_mode_pattern) { in set_feature_complete()
2252 xudc_writel(xudc, xudc->test_mode_pattern, PORT_TM); in set_feature_complete()
2253 xudc->test_mode_pattern = 0; in set_feature_complete()
2260 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE); in tegra_xudc_ep0_set_feature()
2261 u32 feature = le16_to_cpu(ctrl->wValue); in tegra_xudc_ep0_set_feature()
2262 u32 index = le16_to_cpu(ctrl->wIndex); in tegra_xudc_ep0_set_feature()
2266 if (le16_to_cpu(ctrl->wLength) != 0) in tegra_xudc_ep0_set_feature()
2267 return -EINVAL; in tegra_xudc_ep0_set_feature()
2269 switch (ctrl->bRequestType & USB_RECIP_MASK) { in tegra_xudc_ep0_set_feature()
2273 if ((xudc->gadget.speed == USB_SPEED_SUPER) || in tegra_xudc_ep0_set_feature()
2274 (xudc->device_state == USB_STATE_DEFAULT)) in tegra_xudc_ep0_set_feature()
2275 return -EINVAL; in tegra_xudc_ep0_set_feature()
2287 if ((xudc->device_state != USB_STATE_CONFIGURED) || in tegra_xudc_ep0_set_feature()
2288 (xudc->gadget.speed != USB_SPEED_SUPER)) in tegra_xudc_ep0_set_feature()
2289 return -EINVAL; in tegra_xudc_ep0_set_feature()
2293 xudc->soc->u1_enable) { in tegra_xudc_ep0_set_feature()
2301 xudc->soc->u2_enable) { in tegra_xudc_ep0_set_feature()
2311 if (xudc->gadget.speed != USB_SPEED_HIGH) in tegra_xudc_ep0_set_feature()
2312 return -EINVAL; in tegra_xudc_ep0_set_feature()
2315 return -EINVAL; in tegra_xudc_ep0_set_feature()
2317 xudc->test_mode_pattern = index >> 8; in tegra_xudc_ep0_set_feature()
2320 return -EINVAL; in tegra_xudc_ep0_set_feature()
2325 if (xudc->device_state != USB_STATE_CONFIGURED) in tegra_xudc_ep0_set_feature()
2326 return -EINVAL; in tegra_xudc_ep0_set_feature()
2343 return -EINVAL; in tegra_xudc_ep0_set_feature()
2351 if ((xudc->device_state == USB_STATE_DEFAULT) || in tegra_xudc_ep0_set_feature()
2352 ((xudc->device_state == USB_STATE_ADDRESS) && in tegra_xudc_ep0_set_feature()
2354 return -EINVAL; in tegra_xudc_ep0_set_feature()
2356 ret = __tegra_xudc_ep_set_halt(&xudc->ep[ep], set); in tegra_xudc_ep0_set_feature()
2361 return -EINVAL; in tegra_xudc_ep0_set_feature()
2371 u32 val, ep, index = le16_to_cpu(ctrl->wIndex); in tegra_xudc_ep0_get_status()
2374 if (!(ctrl->bRequestType & USB_DIR_IN)) in tegra_xudc_ep0_get_status()
2375 return -EINVAL; in tegra_xudc_ep0_get_status()
2377 if ((le16_to_cpu(ctrl->wValue) != 0) || in tegra_xudc_ep0_get_status()
2378 (le16_to_cpu(ctrl->wLength) != 2)) in tegra_xudc_ep0_get_status()
2379 return -EINVAL; in tegra_xudc_ep0_get_status()
2381 switch (ctrl->bRequestType & USB_RECIP_MASK) { in tegra_xudc_ep0_get_status()
2385 if (xudc->selfpowered) in tegra_xudc_ep0_get_status()
2388 if ((xudc->gadget.speed < USB_SPEED_SUPER) && in tegra_xudc_ep0_get_status()
2392 if (xudc->gadget.speed == USB_SPEED_SUPER) { in tegra_xudc_ep0_get_status()
2400 if (xudc->gadget.speed == USB_SPEED_SUPER) { in tegra_xudc_ep0_get_status()
2410 ep_ctx = &xudc->ep_context[ep]; in tegra_xudc_ep0_get_status()
2412 if ((xudc->device_state != USB_STATE_CONFIGURED) && in tegra_xudc_ep0_get_status()
2413 ((xudc->device_state != USB_STATE_ADDRESS) || (ep != 0))) in tegra_xudc_ep0_get_status()
2414 return -EINVAL; in tegra_xudc_ep0_get_status()
2417 return -EINVAL; in tegra_xudc_ep0_get_status()
2423 return -EINVAL; in tegra_xudc_ep0_get_status()
2426 xudc->status_buf = cpu_to_le16(status); in tegra_xudc_ep0_get_status()
2427 return tegra_xudc_ep0_queue_data(xudc, &xudc->status_buf, in tegra_xudc_ep0_get_status()
2428 sizeof(xudc->status_buf), in tegra_xudc_ep0_get_status()
2434 /* Nothing to do with SEL values */ in set_sel_complete()
2440 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE | in tegra_xudc_ep0_set_sel()
2442 return -EINVAL; in tegra_xudc_ep0_set_sel()
2444 if (xudc->device_state == USB_STATE_DEFAULT) in tegra_xudc_ep0_set_sel()
2445 return -EINVAL; in tegra_xudc_ep0_set_sel()
2447 if ((le16_to_cpu(ctrl->wIndex) != 0) || in tegra_xudc_ep0_set_sel()
2448 (le16_to_cpu(ctrl->wValue) != 0) || in tegra_xudc_ep0_set_sel()
2449 (le16_to_cpu(ctrl->wLength) != 6)) in tegra_xudc_ep0_set_sel()
2450 return -EINVAL; in tegra_xudc_ep0_set_sel()
2452 return tegra_xudc_ep0_queue_data(xudc, &xudc->sel_timing, in tegra_xudc_ep0_set_sel()
2453 sizeof(xudc->sel_timing), in tegra_xudc_ep0_set_sel()
2465 u32 delay = le16_to_cpu(ctrl->wValue); in tegra_xudc_ep0_set_isoch_delay()
2467 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE | in tegra_xudc_ep0_set_isoch_delay()
2469 return -EINVAL; in tegra_xudc_ep0_set_isoch_delay()
2471 if ((delay > 65535) || (le16_to_cpu(ctrl->wIndex) != 0) || in tegra_xudc_ep0_set_isoch_delay()
2472 (le16_to_cpu(ctrl->wLength) != 0)) in tegra_xudc_ep0_set_isoch_delay()
2473 return -EINVAL; in tegra_xudc_ep0_set_isoch_delay()
2475 xudc->isoch_delay = delay; in tegra_xudc_ep0_set_isoch_delay()
2482 struct tegra_xudc *xudc = req->context; in set_address_complete()
2484 if ((xudc->device_state == USB_STATE_DEFAULT) && in set_address_complete()
2485 (xudc->dev_addr != 0)) { in set_address_complete()
2486 xudc->device_state = USB_STATE_ADDRESS; in set_address_complete()
2487 usb_gadget_set_state(&xudc->gadget, xudc->device_state); in set_address_complete()
2488 } else if ((xudc->device_state == USB_STATE_ADDRESS) && in set_address_complete()
2489 (xudc->dev_addr == 0)) { in set_address_complete()
2490 xudc->device_state = USB_STATE_DEFAULT; in set_address_complete()
2491 usb_gadget_set_state(&xudc->gadget, xudc->device_state); in set_address_complete()
2498 struct tegra_xudc_ep *ep0 = &xudc->ep[0]; in tegra_xudc_ep0_set_address()
2499 u32 val, addr = le16_to_cpu(ctrl->wValue); in tegra_xudc_ep0_set_address()
2501 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE | in tegra_xudc_ep0_set_address()
2503 return -EINVAL; in tegra_xudc_ep0_set_address()
2505 if ((addr > 127) || (le16_to_cpu(ctrl->wIndex) != 0) || in tegra_xudc_ep0_set_address()
2506 (le16_to_cpu(ctrl->wLength) != 0)) in tegra_xudc_ep0_set_address()
2507 return -EINVAL; in tegra_xudc_ep0_set_address()
2509 if (xudc->device_state == USB_STATE_CONFIGURED) in tegra_xudc_ep0_set_address()
2510 return -EINVAL; in tegra_xudc_ep0_set_address()
2512 dev_dbg(xudc->dev, "set address: %u\n", addr); in tegra_xudc_ep0_set_address()
2514 xudc->dev_addr = addr; in tegra_xudc_ep0_set_address()
2520 ep_ctx_write_devaddr(ep0->context, addr); in tegra_xudc_ep0_set_address()
2530 switch (ctrl->bRequest) { in tegra_xudc_ep0_standard_req()
2532 dev_dbg(xudc->dev, "USB_REQ_GET_STATUS\n"); in tegra_xudc_ep0_standard_req()
2536 dev_dbg(xudc->dev, "USB_REQ_SET_ADDRESS\n"); in tegra_xudc_ep0_standard_req()
2540 dev_dbg(xudc->dev, "USB_REQ_SET_SEL\n"); in tegra_xudc_ep0_standard_req()
2544 dev_dbg(xudc->dev, "USB_REQ_SET_ISOCH_DELAY\n"); in tegra_xudc_ep0_standard_req()
2549 dev_dbg(xudc->dev, "USB_REQ_CLEAR/SET_FEATURE\n"); in tegra_xudc_ep0_standard_req()
2553 dev_dbg(xudc->dev, "USB_REQ_SET_CONFIGURATION\n"); in tegra_xudc_ep0_standard_req()
2574 xudc->setup_seq_num = seq_num; in tegra_xudc_handle_ep0_setup_packet()
2580 * On Tegra210, setup packets with sequence numbers 0xfffe or 0xffff in tegra_xudc_handle_ep0_setup_packet()
2583 if (xudc->soc->invalid_seq_num && in tegra_xudc_handle_ep0_setup_packet()
2585 dev_warn(xudc->dev, "invalid sequence number detected\n"); in tegra_xudc_handle_ep0_setup_packet()
2590 if (ctrl->wLength) in tegra_xudc_handle_ep0_setup_packet()
2591 xudc->setup_state = (ctrl->bRequestType & USB_DIR_IN) ? in tegra_xudc_handle_ep0_setup_packet()
2594 xudc->setup_state = STATUS_STAGE_XFER; in tegra_xudc_handle_ep0_setup_packet()
2596 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) in tegra_xudc_handle_ep0_setup_packet()
2602 dev_warn(xudc->dev, "setup request failed: %d\n", ret); in tegra_xudc_handle_ep0_setup_packet()
2603 xudc->setup_state = WAIT_FOR_SETUP; in tegra_xudc_handle_ep0_setup_packet()
2614 if (xudc->setup_state != WAIT_FOR_SETUP) { in tegra_xudc_handle_ep0_event()
2621 memcpy(&xudc->setup_packet.ctrl_req, ctrl, sizeof(*ctrl)); in tegra_xudc_handle_ep0_event()
2622 xudc->setup_packet.seq_num = seq_num; in tegra_xudc_handle_ep0_event()
2623 xudc->queued_setup_packet = true; in tegra_xudc_handle_ep0_event()
2634 list_for_each_entry(req, &ep->queue, list) { in trb_to_request()
2635 if (!req->trbs_queued) in trb_to_request()
2660 * TDs are complete on short packet or when the completed TRB is the in tegra_xudc_handle_transfer_completion()
2664 (req->trbs_needed == req->trbs_queued)))) { in tegra_xudc_handle_transfer_completion()
2665 struct tegra_xudc_trb *last = req->last_trb; in tegra_xudc_handle_transfer_completion()
2669 req->usb_req.actual = req->usb_req.length - residual; in tegra_xudc_handle_transfer_completion()
2671 dev_dbg(xudc->dev, "bytes transferred %u / %u\n", in tegra_xudc_handle_transfer_completion()
2672 req->usb_req.actual, req->usb_req.length); in tegra_xudc_handle_transfer_completion()
2676 if (ep->desc && usb_endpoint_xfer_control(ep->desc)) in tegra_xudc_handle_transfer_completion()
2681 * on short packet completion. in tegra_xudc_handle_transfer_completion()
2684 ep->deq_ptr = (last - ep->transfer_ring) + 1; in tegra_xudc_handle_transfer_completion()
2685 if (ep->deq_ptr == XUDC_TRANSFER_RING_SIZE - 1) in tegra_xudc_handle_transfer_completion()
2686 ep->deq_ptr = 0; in tegra_xudc_handle_transfer_completion()
2689 dev_warn(xudc->dev, "transfer event on dequeued request\n"); in tegra_xudc_handle_transfer_completion()
2692 if (ep->desc) in tegra_xudc_handle_transfer_completion()
2700 struct tegra_xudc_ep *ep = &xudc->ep[ep_index]; in tegra_xudc_handle_transfer_event()
2704 if (ep_ctx_read_state(ep->context) == EP_STATE_DISABLED) { in tegra_xudc_handle_transfer_event()
2705 dev_warn(xudc->dev, "transfer event on disabled EP %u\n", in tegra_xudc_handle_transfer_event()
2714 ep->deq_ptr = (trb - ep->transfer_ring) + 1; in tegra_xudc_handle_transfer_event()
2716 if (ep->deq_ptr == XUDC_TRANSFER_RING_SIZE - 1) in tegra_xudc_handle_transfer_event()
2717 ep->deq_ptr = 0; in tegra_xudc_handle_transfer_event()
2718 ep->ring_full = false; in tegra_xudc_handle_transfer_event()
2727 dev_info(xudc->dev, "stream rejected on EP %u\n", ep_index); in tegra_xudc_handle_transfer_event()
2729 ep->stream_rejected = true; in tegra_xudc_handle_transfer_event()
2732 dev_info(xudc->dev, "prime pipe received on EP %u\n", ep_index); in tegra_xudc_handle_transfer_event()
2734 if (ep->stream_rejected) { in tegra_xudc_handle_transfer_event()
2735 ep->stream_rejected = false; in tegra_xudc_handle_transfer_event()
2739 * un-stop it. in tegra_xudc_handle_transfer_event()
2751 ep->enq_ptr = ep->deq_ptr; in tegra_xudc_handle_transfer_event()
2752 tegra_xudc_ep_nuke(ep, -EIO); in tegra_xudc_handle_transfer_event()
2762 dev_err(xudc->dev, "completion error %#x on EP %u\n", in tegra_xudc_handle_transfer_event()
2768 dev_info(xudc->dev, "sequence number error\n"); in tegra_xudc_handle_transfer_event()
2774 tegra_xudc_ep_nuke(ep, -EINVAL); in tegra_xudc_handle_transfer_event()
2775 xudc->setup_state = WAIT_FOR_SETUP; in tegra_xudc_handle_transfer_event()
2776 if (!xudc->queued_setup_packet) in tegra_xudc_handle_transfer_event()
2780 &xudc->setup_packet.ctrl_req, in tegra_xudc_handle_transfer_event()
2781 xudc->setup_packet.seq_num); in tegra_xudc_handle_transfer_event()
2782 xudc->queued_setup_packet = false; in tegra_xudc_handle_transfer_event()
2785 dev_dbg(xudc->dev, "stop completion code on EP %u\n", in tegra_xudc_handle_transfer_event()
2789 tegra_xudc_ep_nuke(ep, -ECONNREFUSED); in tegra_xudc_handle_transfer_event()
2792 dev_dbg(xudc->dev, "completion event %#x on EP %u\n", in tegra_xudc_handle_transfer_event()
2800 struct tegra_xudc_ep *ep0 = &xudc->ep[0]; in tegra_xudc_reset()
2804 xudc->setup_state = WAIT_FOR_SETUP; in tegra_xudc_reset()
2805 xudc->device_state = USB_STATE_DEFAULT; in tegra_xudc_reset()
2806 usb_gadget_set_state(&xudc->gadget, xudc->device_state); in tegra_xudc_reset()
2810 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) in tegra_xudc_reset()
2811 tegra_xudc_ep_nuke(&xudc->ep[i], -ESHUTDOWN); in tegra_xudc_reset()
2817 ep0->deq_ptr = ep0->enq_ptr; in tegra_xudc_reset()
2818 ep0->ring_full = false; in tegra_xudc_reset()
2820 xudc->setup_seq_num = 0; in tegra_xudc_reset()
2821 xudc->queued_setup_packet = false; in tegra_xudc_reset()
2823 ep_ctx_write_rsvd(ep0->context, 0); in tegra_xudc_reset()
2824 ep_ctx_write_partial_td(ep0->context, 0); in tegra_xudc_reset()
2825 ep_ctx_write_splitxstate(ep0->context, 0); in tegra_xudc_reset()
2826 ep_ctx_write_seq_num(ep0->context, 0); in tegra_xudc_reset()
2828 deq_ptr = trb_virt_to_phys(ep0, &ep0->transfer_ring[ep0->deq_ptr]); in tegra_xudc_reset()
2830 if (!dma_mapping_error(xudc->dev, deq_ptr)) { in tegra_xudc_reset()
2831 ep_ctx_write_deq_ptr(ep0->context, deq_ptr); in tegra_xudc_reset()
2832 ep_ctx_write_dcs(ep0->context, ep0->pcs); in tegra_xudc_reset()
2842 struct tegra_xudc_ep *ep0 = &xudc->ep[0]; in tegra_xudc_port_connect()
2849 xudc->gadget.speed = USB_SPEED_LOW; in tegra_xudc_port_connect()
2852 xudc->gadget.speed = USB_SPEED_FULL; in tegra_xudc_port_connect()
2855 xudc->gadget.speed = USB_SPEED_HIGH; in tegra_xudc_port_connect()
2858 xudc->gadget.speed = USB_SPEED_SUPER; in tegra_xudc_port_connect()
2861 xudc->gadget.speed = USB_SPEED_UNKNOWN; in tegra_xudc_port_connect()
2865 xudc->device_state = USB_STATE_DEFAULT; in tegra_xudc_port_connect()
2866 usb_gadget_set_state(&xudc->gadget, xudc->device_state); in tegra_xudc_port_connect()
2868 xudc->setup_state = WAIT_FOR_SETUP; in tegra_xudc_port_connect()
2870 if (xudc->gadget.speed == USB_SPEED_SUPER) in tegra_xudc_port_connect()
2875 ep_ctx_write_max_packet_size(ep0->context, maxpacket); in tegra_xudc_port_connect()
2877 usb_ep_set_maxpacket_limit(&ep0->usb_ep, maxpacket); in tegra_xudc_port_connect()
2879 if (!xudc->soc->u1_enable) { in tegra_xudc_port_connect()
2885 if (!xudc->soc->u2_enable) { in tegra_xudc_port_connect()
2891 if (xudc->gadget.speed <= USB_SPEED_HIGH) { in tegra_xudc_port_connect()
2894 if (xudc->soc->lpm_enable) in tegra_xudc_port_connect()
2910 if (xudc->driver && xudc->driver->disconnect) { in tegra_xudc_port_disconnect()
2911 spin_unlock(&xudc->lock); in tegra_xudc_port_disconnect()
2912 xudc->driver->disconnect(&xudc->gadget); in tegra_xudc_port_disconnect()
2913 spin_lock(&xudc->lock); in tegra_xudc_port_disconnect()
2916 xudc->device_state = USB_STATE_NOTATTACHED; in tegra_xudc_port_disconnect()
2917 usb_gadget_set_state(&xudc->gadget, xudc->device_state); in tegra_xudc_port_disconnect()
2919 complete(&xudc->disconnect_complete); in tegra_xudc_port_disconnect()
2926 if (xudc->driver) { in tegra_xudc_port_reset()
2927 spin_unlock(&xudc->lock); in tegra_xudc_port_reset()
2928 usb_gadget_udc_reset(&xudc->gadget, xudc->driver); in tegra_xudc_port_reset()
2929 spin_lock(&xudc->lock); in tegra_xudc_port_reset()
2937 dev_dbg(xudc->dev, "port suspend\n"); in tegra_xudc_port_suspend()
2939 xudc->resume_state = xudc->device_state; in tegra_xudc_port_suspend()
2940 xudc->device_state = USB_STATE_SUSPENDED; in tegra_xudc_port_suspend()
2941 usb_gadget_set_state(&xudc->gadget, xudc->device_state); in tegra_xudc_port_suspend()
2943 if (xudc->driver->suspend) { in tegra_xudc_port_suspend()
2944 spin_unlock(&xudc->lock); in tegra_xudc_port_suspend()
2945 xudc->driver->suspend(&xudc->gadget); in tegra_xudc_port_suspend()
2946 spin_lock(&xudc->lock); in tegra_xudc_port_suspend()
2952 dev_dbg(xudc->dev, "port resume\n"); in tegra_xudc_port_resume()
2956 if (xudc->driver->resume) { in tegra_xudc_port_resume()
2957 spin_unlock(&xudc->lock); in tegra_xudc_port_resume()
2958 xudc->driver->resume(&xudc->gadget); in tegra_xudc_port_resume()
2959 spin_lock(&xudc->lock); in tegra_xudc_port_resume()
2980 dev_dbg(xudc->dev, "STCHG_REQ, PORTHALT = %#x\n", porthalt); in __tegra_xudc_handle_port_status()
2987 dev_dbg(xudc->dev, "PRC, PR, PORTSC = %#x\n", portsc); in __tegra_xudc_handle_port_status()
2990 if (xudc->soc->port_reset_quirk) { in __tegra_xudc_handle_port_status()
2991 schedule_delayed_work(&xudc->port_reset_war_work, in __tegra_xudc_handle_port_status()
2993 xudc->wait_for_sec_prc = 1; in __tegra_xudc_handle_port_status()
2998 dev_dbg(xudc->dev, "PRC, Not PR, PORTSC = %#x\n", portsc); in __tegra_xudc_handle_port_status()
3001 cancel_delayed_work(&xudc->port_reset_war_work); in __tegra_xudc_handle_port_status()
3002 xudc->wait_for_sec_prc = 0; in __tegra_xudc_handle_port_status()
3007 dev_dbg(xudc->dev, "WRC, PORTSC = %#x\n", portsc); in __tegra_xudc_handle_port_status()
3015 dev_dbg(xudc->dev, "CSC, PORTSC = %#x\n", portsc); in __tegra_xudc_handle_port_status()
3023 if (xudc->wait_csc) { in __tegra_xudc_handle_port_status()
3024 cancel_delayed_work(&xudc->plc_reset_work); in __tegra_xudc_handle_port_status()
3025 xudc->wait_csc = false; in __tegra_xudc_handle_port_status()
3033 dev_dbg(xudc->dev, "PLC, PORTSC = %#x\n", portsc); in __tegra_xudc_handle_port_status()
3040 if (xudc->gadget.speed < USB_SPEED_SUPER) in __tegra_xudc_handle_port_status()
3044 if (xudc->gadget.speed == USB_SPEED_SUPER) in __tegra_xudc_handle_port_status()
3048 schedule_delayed_work(&xudc->plc_reset_work, in __tegra_xudc_handle_port_status()
3050 xudc->wait_csc = true; in __tegra_xudc_handle_port_status()
3058 dev_warn(xudc->dev, "CEC, PORTSC = %#x\n", portsc); in __tegra_xudc_handle_port_status()
3062 dev_dbg(xudc->dev, "PORTSC = %#x\n", xudc_readl(xudc, PORTSC)); in __tegra_xudc_handle_port_status()
3090 dev_info(xudc->dev, "Unrecognized TRB type = %#x\n", type); in tegra_xudc_handle_event()
3101 event = xudc->event_ring[xudc->event_ring_index] + in tegra_xudc_process_event_ring()
3102 xudc->event_ring_deq_ptr; in tegra_xudc_process_event_ring()
3104 if (trb_read_cycle(event) != xudc->ccs) in tegra_xudc_process_event_ring()
3109 xudc->event_ring_deq_ptr++; in tegra_xudc_process_event_ring()
3110 if (xudc->event_ring_deq_ptr == XUDC_EVENT_RING_SIZE) { in tegra_xudc_process_event_ring()
3111 xudc->event_ring_deq_ptr = 0; in tegra_xudc_process_event_ring()
3112 xudc->event_ring_index++; in tegra_xudc_process_event_ring()
3115 if (xudc->event_ring_index == XUDC_NR_EVENT_RINGS) { in tegra_xudc_process_event_ring()
3116 xudc->event_ring_index = 0; in tegra_xudc_process_event_ring()
3117 xudc->ccs = !xudc->ccs; in tegra_xudc_process_event_ring()
3121 erdp = xudc->event_ring_phys[xudc->event_ring_index] + in tegra_xudc_process_event_ring()
3122 xudc->event_ring_deq_ptr * sizeof(*event); in tegra_xudc_process_event_ring()
3139 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_irq()
3141 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_irq()
3148 struct tegra_xudc_ep *ep = &xudc->ep[index]; in tegra_xudc_alloc_ep()
3150 ep->xudc = xudc; in tegra_xudc_alloc_ep()
3151 ep->index = index; in tegra_xudc_alloc_ep()
3152 ep->context = &xudc->ep_context[index]; in tegra_xudc_alloc_ep()
3153 INIT_LIST_HEAD(&ep->queue); in tegra_xudc_alloc_ep()
3157 * EP0 is bi-directional, EP1 is unused. in tegra_xudc_alloc_ep()
3162 ep->transfer_ring = dma_pool_alloc(xudc->transfer_ring_pool, in tegra_xudc_alloc_ep()
3164 &ep->transfer_ring_phys); in tegra_xudc_alloc_ep()
3165 if (!ep->transfer_ring) in tegra_xudc_alloc_ep()
3166 return -ENOMEM; in tegra_xudc_alloc_ep()
3169 snprintf(ep->name, sizeof(ep->name), "ep%u%s", index / 2, in tegra_xudc_alloc_ep()
3171 ep->usb_ep.name = ep->name; in tegra_xudc_alloc_ep()
3172 usb_ep_set_maxpacket_limit(&ep->usb_ep, 1024); in tegra_xudc_alloc_ep()
3173 ep->usb_ep.max_streams = 16; in tegra_xudc_alloc_ep()
3174 ep->usb_ep.ops = &tegra_xudc_ep_ops; in tegra_xudc_alloc_ep()
3175 ep->usb_ep.caps.type_bulk = true; in tegra_xudc_alloc_ep()
3176 ep->usb_ep.caps.type_int = true; in tegra_xudc_alloc_ep()
3178 ep->usb_ep.caps.dir_in = true; in tegra_xudc_alloc_ep()
3180 ep->usb_ep.caps.dir_out = true; in tegra_xudc_alloc_ep()
3181 list_add_tail(&ep->usb_ep.ep_list, &xudc->gadget.ep_list); in tegra_xudc_alloc_ep()
3183 strscpy(ep->name, "ep0", 3); in tegra_xudc_alloc_ep()
3184 ep->usb_ep.name = ep->name; in tegra_xudc_alloc_ep()
3185 usb_ep_set_maxpacket_limit(&ep->usb_ep, 512); in tegra_xudc_alloc_ep()
3186 ep->usb_ep.ops = &tegra_xudc_ep0_ops; in tegra_xudc_alloc_ep()
3187 ep->usb_ep.caps.type_control = true; in tegra_xudc_alloc_ep()
3188 ep->usb_ep.caps.dir_in = true; in tegra_xudc_alloc_ep()
3189 ep->usb_ep.caps.dir_out = true; in tegra_xudc_alloc_ep()
3197 struct tegra_xudc_ep *ep = &xudc->ep[index]; in tegra_xudc_free_ep()
3201 * EP0 is bi-directional, EP1 is unused. in tegra_xudc_free_ep()
3206 dma_pool_free(xudc->transfer_ring_pool, ep->transfer_ring, in tegra_xudc_free_ep()
3207 ep->transfer_ring_phys); in tegra_xudc_free_ep()
3216 xudc->ep_context = in tegra_xudc_alloc_eps()
3217 dma_alloc_coherent(xudc->dev, XUDC_NR_EPS * in tegra_xudc_alloc_eps()
3218 sizeof(*xudc->ep_context), in tegra_xudc_alloc_eps()
3219 &xudc->ep_context_phys, GFP_KERNEL); in tegra_xudc_alloc_eps()
3220 if (!xudc->ep_context) in tegra_xudc_alloc_eps()
3221 return -ENOMEM; in tegra_xudc_alloc_eps()
3223 xudc->transfer_ring_pool = in tegra_xudc_alloc_eps()
3224 dmam_pool_create(dev_name(xudc->dev), xudc->dev, in tegra_xudc_alloc_eps()
3228 if (!xudc->transfer_ring_pool) { in tegra_xudc_alloc_eps()
3229 err = -ENOMEM; in tegra_xudc_alloc_eps()
3233 INIT_LIST_HEAD(&xudc->gadget.ep_list); in tegra_xudc_alloc_eps()
3234 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) { in tegra_xudc_alloc_eps()
3240 req = tegra_xudc_ep_alloc_request(&xudc->ep[0].usb_ep, GFP_KERNEL); in tegra_xudc_alloc_eps()
3242 err = -ENOMEM; in tegra_xudc_alloc_eps()
3245 xudc->ep0_req = to_xudc_req(req); in tegra_xudc_alloc_eps()
3250 for (; i > 0; i--) in tegra_xudc_alloc_eps()
3251 tegra_xudc_free_ep(xudc, i - 1); in tegra_xudc_alloc_eps()
3253 dma_free_coherent(xudc->dev, XUDC_NR_EPS * sizeof(*xudc->ep_context), in tegra_xudc_alloc_eps()
3254 xudc->ep_context, xudc->ep_context_phys); in tegra_xudc_alloc_eps()
3260 xudc_writel(xudc, lower_32_bits(xudc->ep_context_phys), ECPLO); in tegra_xudc_init_eps()
3261 xudc_writel(xudc, upper_32_bits(xudc->ep_context_phys), ECPHI); in tegra_xudc_init_eps()
3268 tegra_xudc_ep_free_request(&xudc->ep[0].usb_ep, in tegra_xudc_free_eps()
3269 &xudc->ep0_req->usb_req); in tegra_xudc_free_eps()
3271 for (i = 0; i < ARRAY_SIZE(xudc->ep); i++) in tegra_xudc_free_eps()
3274 dma_free_coherent(xudc->dev, XUDC_NR_EPS * sizeof(*xudc->ep_context), in tegra_xudc_free_eps()
3275 xudc->ep_context, xudc->ep_context_phys); in tegra_xudc_free_eps()
3282 for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) { in tegra_xudc_alloc_event_ring()
3283 xudc->event_ring[i] = in tegra_xudc_alloc_event_ring()
3284 dma_alloc_coherent(xudc->dev, XUDC_EVENT_RING_SIZE * in tegra_xudc_alloc_event_ring()
3285 sizeof(*xudc->event_ring[i]), in tegra_xudc_alloc_event_ring()
3286 &xudc->event_ring_phys[i], in tegra_xudc_alloc_event_ring()
3288 if (!xudc->event_ring[i]) in tegra_xudc_alloc_event_ring()
3295 for (; i > 0; i--) { in tegra_xudc_alloc_event_ring()
3296 dma_free_coherent(xudc->dev, XUDC_EVENT_RING_SIZE * in tegra_xudc_alloc_event_ring()
3297 sizeof(*xudc->event_ring[i - 1]), in tegra_xudc_alloc_event_ring()
3298 xudc->event_ring[i - 1], in tegra_xudc_alloc_event_ring()
3299 xudc->event_ring_phys[i - 1]); in tegra_xudc_alloc_event_ring()
3301 return -ENOMEM; in tegra_xudc_alloc_event_ring()
3309 for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) { in tegra_xudc_init_event_ring()
3310 memset(xudc->event_ring[i], 0, XUDC_EVENT_RING_SIZE * in tegra_xudc_init_event_ring()
3311 sizeof(*xudc->event_ring[i])); in tegra_xudc_init_event_ring()
3318 xudc_writel(xudc, lower_32_bits(xudc->event_ring_phys[i]), in tegra_xudc_init_event_ring()
3320 xudc_writel(xudc, upper_32_bits(xudc->event_ring_phys[i]), in tegra_xudc_init_event_ring()
3324 val = lower_32_bits(xudc->event_ring_phys[0]); in tegra_xudc_init_event_ring()
3329 val = upper_32_bits(xudc->event_ring_phys[0]); in tegra_xudc_init_event_ring()
3333 xudc->ccs = true; in tegra_xudc_init_event_ring()
3334 xudc->event_ring_index = 0; in tegra_xudc_init_event_ring()
3335 xudc->event_ring_deq_ptr = 0; in tegra_xudc_init_event_ring()
3342 for (i = 0; i < ARRAY_SIZE(xudc->event_ring); i++) { in tegra_xudc_free_event_ring()
3343 dma_free_coherent(xudc->dev, XUDC_EVENT_RING_SIZE * in tegra_xudc_free_event_ring()
3344 sizeof(*xudc->event_ring[i]), in tegra_xudc_free_event_ring()
3345 xudc->event_ring[i], in tegra_xudc_free_event_ring()
3346 xudc->event_ring_phys[i]); in tegra_xudc_free_event_ring()
3354 if (xudc->soc->has_ipfs) { in tegra_xudc_fpci_ipfs_init()
3369 val |= xudc->phys_base & (XUSB_DEV_CFG_4_BASE_ADDR_MASK); in tegra_xudc_fpci_ipfs_init()
3372 fpci_writel(xudc, upper_32_bits(xudc->phys_base), XUSB_DEV_CFG_5); in tegra_xudc_fpci_ipfs_init()
3376 if (xudc->soc->has_ipfs) { in tegra_xudc_fpci_ipfs_init()
3388 if (xudc->soc->has_ipfs) { in tegra_xudc_device_params_init()
3400 if (xudc->soc->port_speed_quirk) in tegra_xudc_device_params_init()
3421 if (xudc->soc->lpm_enable) { in tegra_xudc_device_params_init()
3492 xudc->utmi_phy = devm_kcalloc(xudc->dev, xudc->soc->num_phys, in tegra_xudc_phy_get()
3493 sizeof(*xudc->utmi_phy), GFP_KERNEL); in tegra_xudc_phy_get()
3494 if (!xudc->utmi_phy) in tegra_xudc_phy_get()
3495 return -ENOMEM; in tegra_xudc_phy_get()
3497 xudc->usb3_phy = devm_kcalloc(xudc->dev, xudc->soc->num_phys, in tegra_xudc_phy_get()
3498 sizeof(*xudc->usb3_phy), GFP_KERNEL); in tegra_xudc_phy_get()
3499 if (!xudc->usb3_phy) in tegra_xudc_phy_get()
3500 return -ENOMEM; in tegra_xudc_phy_get()
3502 xudc->usbphy = devm_kcalloc(xudc->dev, xudc->soc->num_phys, in tegra_xudc_phy_get()
3503 sizeof(*xudc->usbphy), GFP_KERNEL); in tegra_xudc_phy_get()
3504 if (!xudc->usbphy) in tegra_xudc_phy_get()
3505 return -ENOMEM; in tegra_xudc_phy_get()
3507 xudc->vbus_nb.notifier_call = tegra_xudc_vbus_notify; in tegra_xudc_phy_get()
3509 for (i = 0; i < xudc->soc->num_phys; i++) { in tegra_xudc_phy_get()
3510 char phy_name[] = "usb.-."; in tegra_xudc_phy_get()
3513 snprintf(phy_name, sizeof(phy_name), "usb2-%d", i); in tegra_xudc_phy_get()
3514 xudc->utmi_phy[i] = devm_phy_optional_get(xudc->dev, phy_name); in tegra_xudc_phy_get()
3515 if (IS_ERR(xudc->utmi_phy[i])) { in tegra_xudc_phy_get()
3516 err = PTR_ERR(xudc->utmi_phy[i]); in tegra_xudc_phy_get()
3517 dev_err_probe(xudc->dev, err, in tegra_xudc_phy_get()
3518 "failed to get usb2-%d PHY\n", i); in tegra_xudc_phy_get()
3520 } else if (xudc->utmi_phy[i]) { in tegra_xudc_phy_get()
3521 /* Get usb-phy, if utmi phy is available */ in tegra_xudc_phy_get()
3522 xudc->usbphy[i] = devm_usb_get_phy_by_node(xudc->dev, in tegra_xudc_phy_get()
3523 xudc->utmi_phy[i]->dev.of_node, in tegra_xudc_phy_get()
3524 &xudc->vbus_nb); in tegra_xudc_phy_get()
3525 if (IS_ERR(xudc->usbphy[i])) { in tegra_xudc_phy_get()
3526 err = PTR_ERR(xudc->usbphy[i]); in tegra_xudc_phy_get()
3527 dev_err_probe(xudc->dev, err, in tegra_xudc_phy_get()
3528 "failed to get usbphy-%d\n", i); in tegra_xudc_phy_get()
3531 } else if (!xudc->utmi_phy[i]) { in tegra_xudc_phy_get()
3532 /* if utmi phy is not available, ignore USB3 phy get */ in tegra_xudc_phy_get()
3537 usb3 = tegra_xusb_padctl_get_usb3_companion(xudc->padctl, i); in tegra_xudc_phy_get()
3541 snprintf(phy_name, sizeof(phy_name), "usb3-%d", usb3); in tegra_xudc_phy_get()
3542 xudc->usb3_phy[i] = devm_phy_optional_get(xudc->dev, phy_name); in tegra_xudc_phy_get()
3543 if (IS_ERR(xudc->usb3_phy[i])) { in tegra_xudc_phy_get()
3544 err = PTR_ERR(xudc->usb3_phy[i]); in tegra_xudc_phy_get()
3545 dev_err_probe(xudc->dev, err, in tegra_xudc_phy_get()
3546 "failed to get usb3-%d PHY\n", usb3); in tegra_xudc_phy_get()
3548 } else if (xudc->usb3_phy[i]) in tegra_xudc_phy_get()
3549 dev_dbg(xudc->dev, "usb3-%d PHY registered", usb3); in tegra_xudc_phy_get()
3555 for (i = 0; i < xudc->soc->num_phys; i++) { in tegra_xudc_phy_get()
3556 xudc->usb3_phy[i] = NULL; in tegra_xudc_phy_get()
3557 xudc->utmi_phy[i] = NULL; in tegra_xudc_phy_get()
3558 xudc->usbphy[i] = NULL; in tegra_xudc_phy_get()
3568 for (i = 0; i < xudc->soc->num_phys; i++) { in tegra_xudc_phy_exit()
3569 phy_exit(xudc->usb3_phy[i]); in tegra_xudc_phy_exit()
3570 phy_exit(xudc->utmi_phy[i]); in tegra_xudc_phy_exit()
3579 for (i = 0; i < xudc->soc->num_phys; i++) { in tegra_xudc_phy_init()
3580 err = phy_init(xudc->utmi_phy[i]); in tegra_xudc_phy_init()
3582 dev_err(xudc->dev, "UTMI PHY #%u initialization failed: %d\n", i, err); in tegra_xudc_phy_init()
3586 err = phy_init(xudc->usb3_phy[i]); in tegra_xudc_phy_init()
3588 dev_err(xudc->dev, "USB3 PHY #%u initialization failed: %d\n", i, err); in tegra_xudc_phy_init()
3600 "hvdd-usb",
3601 "avddio-usb",
3665 .compatible = "nvidia,tegra210-xudc",
3669 .compatible = "nvidia,tegra186-xudc",
3673 .compatible = "nvidia,tegra194-xudc",
3682 if (xudc->genpd_dl_ss) in tegra_xudc_powerdomain_remove()
3683 device_link_del(xudc->genpd_dl_ss); in tegra_xudc_powerdomain_remove()
3684 if (xudc->genpd_dl_device) in tegra_xudc_powerdomain_remove()
3685 device_link_del(xudc->genpd_dl_device); in tegra_xudc_powerdomain_remove()
3686 if (xudc->genpd_dev_ss) in tegra_xudc_powerdomain_remove()
3687 dev_pm_domain_detach(xudc->genpd_dev_ss, true); in tegra_xudc_powerdomain_remove()
3688 if (xudc->genpd_dev_device) in tegra_xudc_powerdomain_remove()
3689 dev_pm_domain_detach(xudc->genpd_dev_device, true); in tegra_xudc_powerdomain_remove()
3694 struct device *dev = xudc->dev; in tegra_xudc_powerdomain_init()
3697 xudc->genpd_dev_device = dev_pm_domain_attach_by_name(dev, "dev"); in tegra_xudc_powerdomain_init()
3698 if (IS_ERR_OR_NULL(xudc->genpd_dev_device)) { in tegra_xudc_powerdomain_init()
3699 err = PTR_ERR(xudc->genpd_dev_device) ? : -ENODATA; in tegra_xudc_powerdomain_init()
3700 dev_err(dev, "failed to get device power domain: %d\n", err); in tegra_xudc_powerdomain_init()
3704 xudc->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "ss"); in tegra_xudc_powerdomain_init()
3705 if (IS_ERR_OR_NULL(xudc->genpd_dev_ss)) { in tegra_xudc_powerdomain_init()
3706 err = PTR_ERR(xudc->genpd_dev_ss) ? : -ENODATA; in tegra_xudc_powerdomain_init()
3707 dev_err(dev, "failed to get SuperSpeed power domain: %d\n", err); in tegra_xudc_powerdomain_init()
3711 xudc->genpd_dl_device = device_link_add(dev, xudc->genpd_dev_device, in tegra_xudc_powerdomain_init()
3714 if (!xudc->genpd_dl_device) { in tegra_xudc_powerdomain_init()
3716 return -ENODEV; in tegra_xudc_powerdomain_init()
3719 xudc->genpd_dl_ss = device_link_add(dev, xudc->genpd_dev_ss, in tegra_xudc_powerdomain_init()
3722 if (!xudc->genpd_dl_ss) { in tegra_xudc_powerdomain_init()
3724 return -ENODEV; in tegra_xudc_powerdomain_init()
3737 xudc = devm_kzalloc(&pdev->dev, sizeof(*xudc), GFP_KERNEL); in tegra_xudc_probe()
3739 return -ENOMEM; in tegra_xudc_probe()
3741 xudc->dev = &pdev->dev; in tegra_xudc_probe()
3744 xudc->soc = of_device_get_match_data(&pdev->dev); in tegra_xudc_probe()
3745 if (!xudc->soc) in tegra_xudc_probe()
3746 return -ENODEV; in tegra_xudc_probe()
3749 xudc->base = devm_ioremap_resource(&pdev->dev, res); in tegra_xudc_probe()
3750 if (IS_ERR(xudc->base)) in tegra_xudc_probe()
3751 return PTR_ERR(xudc->base); in tegra_xudc_probe()
3752 xudc->phys_base = res->start; in tegra_xudc_probe()
3754 xudc->fpci = devm_platform_ioremap_resource_byname(pdev, "fpci"); in tegra_xudc_probe()
3755 if (IS_ERR(xudc->fpci)) in tegra_xudc_probe()
3756 return PTR_ERR(xudc->fpci); in tegra_xudc_probe()
3758 if (xudc->soc->has_ipfs) { in tegra_xudc_probe()
3759 xudc->ipfs = devm_platform_ioremap_resource_byname(pdev, "ipfs"); in tegra_xudc_probe()
3760 if (IS_ERR(xudc->ipfs)) in tegra_xudc_probe()
3761 return PTR_ERR(xudc->ipfs); in tegra_xudc_probe()
3764 xudc->irq = platform_get_irq(pdev, 0); in tegra_xudc_probe()
3765 if (xudc->irq < 0) in tegra_xudc_probe()
3766 return xudc->irq; in tegra_xudc_probe()
3768 err = devm_request_irq(&pdev->dev, xudc->irq, tegra_xudc_irq, 0, in tegra_xudc_probe()
3769 dev_name(&pdev->dev), xudc); in tegra_xudc_probe()
3771 dev_err(xudc->dev, "failed to claim IRQ#%u: %d\n", xudc->irq, in tegra_xudc_probe()
3776 xudc->clks = devm_kcalloc(&pdev->dev, xudc->soc->num_clks, sizeof(*xudc->clks), in tegra_xudc_probe()
3778 if (!xudc->clks) in tegra_xudc_probe()
3779 return -ENOMEM; in tegra_xudc_probe()
3781 for (i = 0; i < xudc->soc->num_clks; i++) in tegra_xudc_probe()
3782 xudc->clks[i].id = xudc->soc->clock_names[i]; in tegra_xudc_probe()
3784 err = devm_clk_bulk_get(&pdev->dev, xudc->soc->num_clks, xudc->clks); in tegra_xudc_probe()
3786 dev_err_probe(xudc->dev, err, "failed to request clocks\n"); in tegra_xudc_probe()
3790 xudc->supplies = devm_kcalloc(&pdev->dev, xudc->soc->num_supplies, in tegra_xudc_probe()
3791 sizeof(*xudc->supplies), GFP_KERNEL); in tegra_xudc_probe()
3792 if (!xudc->supplies) in tegra_xudc_probe()
3793 return -ENOMEM; in tegra_xudc_probe()
3795 for (i = 0; i < xudc->soc->num_supplies; i++) in tegra_xudc_probe()
3796 xudc->supplies[i].supply = xudc->soc->supply_names[i]; in tegra_xudc_probe()
3798 err = devm_regulator_bulk_get(&pdev->dev, xudc->soc->num_supplies, in tegra_xudc_probe()
3799 xudc->supplies); in tegra_xudc_probe()
3801 dev_err_probe(xudc->dev, err, "failed to request regulators\n"); in tegra_xudc_probe()
3805 xudc->padctl = tegra_xusb_padctl_get(&pdev->dev); in tegra_xudc_probe()
3806 if (IS_ERR(xudc->padctl)) in tegra_xudc_probe()
3807 return PTR_ERR(xudc->padctl); in tegra_xudc_probe()
3809 err = regulator_bulk_enable(xudc->soc->num_supplies, xudc->supplies); in tegra_xudc_probe()
3811 dev_err(xudc->dev, "failed to enable regulators: %d\n", err); in tegra_xudc_probe()
3835 spin_lock_init(&xudc->lock); in tegra_xudc_probe()
3837 init_completion(&xudc->disconnect_complete); in tegra_xudc_probe()
3839 INIT_WORK(&xudc->usb_role_sw_work, tegra_xudc_usb_role_sw_work); in tegra_xudc_probe()
3841 INIT_DELAYED_WORK(&xudc->plc_reset_work, tegra_xudc_plc_reset_work); in tegra_xudc_probe()
3843 INIT_DELAYED_WORK(&xudc->port_reset_war_work, in tegra_xudc_probe()
3846 pm_runtime_enable(&pdev->dev); in tegra_xudc_probe()
3848 xudc->gadget.ops = &tegra_xudc_gadget_ops; in tegra_xudc_probe()
3849 xudc->gadget.ep0 = &xudc->ep[0].usb_ep; in tegra_xudc_probe()
3850 xudc->gadget.name = "tegra-xudc"; in tegra_xudc_probe()
3851 xudc->gadget.max_speed = USB_SPEED_SUPER; in tegra_xudc_probe()
3853 err = usb_add_gadget_udc(&pdev->dev, &xudc->gadget); in tegra_xudc_probe()
3855 dev_err(&pdev->dev, "failed to add USB gadget: %d\n", err); in tegra_xudc_probe()
3862 pm_runtime_disable(&pdev->dev); in tegra_xudc_probe()
3871 regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies); in tegra_xudc_probe()
3873 tegra_xusb_padctl_put(xudc->padctl); in tegra_xudc_probe()
3883 pm_runtime_get_sync(xudc->dev); in tegra_xudc_remove()
3885 cancel_delayed_work_sync(&xudc->plc_reset_work); in tegra_xudc_remove()
3886 cancel_work_sync(&xudc->usb_role_sw_work); in tegra_xudc_remove()
3888 usb_del_gadget_udc(&xudc->gadget); in tegra_xudc_remove()
3895 regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies); in tegra_xudc_remove()
3897 for (i = 0; i < xudc->soc->num_phys; i++) { in tegra_xudc_remove()
3898 phy_power_off(xudc->utmi_phy[i]); in tegra_xudc_remove()
3899 phy_power_off(xudc->usb3_phy[i]); in tegra_xudc_remove()
3904 pm_runtime_disable(xudc->dev); in tegra_xudc_remove()
3905 pm_runtime_put(xudc->dev); in tegra_xudc_remove()
3907 tegra_xusb_padctl_put(xudc->padctl); in tegra_xudc_remove()
3916 dev_dbg(xudc->dev, "entering ELPG\n"); in tegra_xudc_powergate()
3918 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_powergate()
3920 xudc->powergated = true; in tegra_xudc_powergate()
3921 xudc->saved_regs.ctrl = xudc_readl(xudc, CTRL); in tegra_xudc_powergate()
3922 xudc->saved_regs.portpm = xudc_readl(xudc, PORTPM); in tegra_xudc_powergate()
3925 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_powergate()
3927 clk_bulk_disable_unprepare(xudc->soc->num_clks, xudc->clks); in tegra_xudc_powergate()
3929 regulator_bulk_disable(xudc->soc->num_supplies, xudc->supplies); in tegra_xudc_powergate()
3931 dev_dbg(xudc->dev, "entering ELPG done\n"); in tegra_xudc_powergate()
3940 dev_dbg(xudc->dev, "exiting ELPG\n"); in tegra_xudc_unpowergate()
3942 err = regulator_bulk_enable(xudc->soc->num_supplies, in tegra_xudc_unpowergate()
3943 xudc->supplies); in tegra_xudc_unpowergate()
3947 err = clk_bulk_prepare_enable(xudc->soc->num_clks, xudc->clks); in tegra_xudc_unpowergate()
3959 xudc_writel(xudc, xudc->saved_regs.portpm, PORTPM); in tegra_xudc_unpowergate()
3960 xudc_writel(xudc, xudc->saved_regs.ctrl, CTRL); in tegra_xudc_unpowergate()
3962 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_unpowergate()
3963 xudc->powergated = false; in tegra_xudc_unpowergate()
3964 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_unpowergate()
3966 dev_dbg(xudc->dev, "exiting ELPG done\n"); in tegra_xudc_unpowergate()
3975 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_suspend()
3976 xudc->suspended = true; in tegra_xudc_suspend()
3977 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_suspend()
3979 flush_work(&xudc->usb_role_sw_work); in tegra_xudc_suspend()
4002 spin_lock_irqsave(&xudc->lock, flags); in tegra_xudc_resume()
4003 xudc->suspended = false; in tegra_xudc_resume()
4004 spin_unlock_irqrestore(&xudc->lock, flags); in tegra_xudc_resume()
4006 schedule_work(&xudc->usb_role_sw_work); in tegra_xudc_resume()
4037 .name = "tegra-xudc",