1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the NXP ISP1761 device controller
5 * Copyright 2014 Ideas on Board Oy
8 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/timer.h>
18 #include <linux/usb.h>
20 #include "isp1760-core.h"
21 #include "isp1760-regs.h"
22 #include "isp1760-udc.h"
24 #define ISP1760_VBUS_POLL_INTERVAL msecs_to_jiffies(500)
26 struct isp1760_request {
27 struct usb_request req;
28 struct list_head queue;
29 struct isp1760_ep *ep;
30 unsigned int packet_size;
33 static inline struct isp1760_udc *gadget_to_udc(struct usb_gadget *gadget)
35 return container_of(gadget, struct isp1760_udc, gadget);
38 static inline struct isp1760_ep *ep_to_udc_ep(struct usb_ep *ep)
40 return container_of(ep, struct isp1760_ep, ep);
43 static inline struct isp1760_request *req_to_udc_req(struct usb_request *req)
45 return container_of(req, struct isp1760_request, req);
48 static inline u32 isp1760_udc_read(struct isp1760_udc *udc, u16 reg)
50 return isp1760_read32(udc->regs, reg);
53 static inline void isp1760_udc_write(struct isp1760_udc *udc, u16 reg, u32 val)
55 isp1760_write32(udc->regs, reg, val);
58 /* -----------------------------------------------------------------------------
62 static struct isp1760_ep *isp1760_udc_find_ep(struct isp1760_udc *udc,
70 for (i = 1; i < ARRAY_SIZE(udc->ep); ++i) {
71 if (udc->ep[i].addr == index)
72 return udc->ep[i].desc ? &udc->ep[i] : NULL;
78 static void __isp1760_udc_select_ep(struct isp1760_ep *ep, int dir)
80 isp1760_udc_write(ep->udc, DC_EPINDEX,
81 DC_ENDPIDX(ep->addr & USB_ENDPOINT_NUMBER_MASK) |
82 (dir == USB_DIR_IN ? DC_EPDIR : 0));
86 * isp1760_udc_select_ep - Select an endpoint for register access
89 * The ISP1761 endpoint registers are banked. This function selects the target
90 * endpoint for banked register access. The selection remains valid until the
91 * next call to this function, the next direct access to the EPINDEX register
92 * or the next reset, whichever comes first.
94 * Called with the UDC spinlock held.
96 static void isp1760_udc_select_ep(struct isp1760_ep *ep)
98 __isp1760_udc_select_ep(ep, ep->addr & USB_ENDPOINT_DIR_MASK);
101 /* Called with the UDC spinlock held. */
102 static void isp1760_udc_ctrl_send_status(struct isp1760_ep *ep, int dir)
104 struct isp1760_udc *udc = ep->udc;
107 * Proceed to the status stage. The status stage data packet flows in
108 * the direction opposite to the data stage data packets, we thus need
109 * to select the OUT/IN endpoint for IN/OUT transfers.
111 isp1760_udc_write(udc, DC_EPINDEX, DC_ENDPIDX(0) |
112 (dir == USB_DIR_IN ? 0 : DC_EPDIR));
113 isp1760_udc_write(udc, DC_CTRLFUNC, DC_STATUS);
116 * The hardware will terminate the request automatically and go back to
117 * the setup stage without notifying us.
119 udc->ep0_state = ISP1760_CTRL_SETUP;
122 /* Called without the UDC spinlock held. */
123 static void isp1760_udc_request_complete(struct isp1760_ep *ep,
124 struct isp1760_request *req,
127 struct isp1760_udc *udc = ep->udc;
130 dev_dbg(ep->udc->isp->dev, "completing request %p with status %d\n",
134 req->req.status = status;
135 req->req.complete(&ep->ep, &req->req);
137 spin_lock_irqsave(&udc->lock, flags);
140 * When completing control OUT requests, move to the status stage after
141 * calling the request complete callback. This gives the gadget an
142 * opportunity to stall the control transfer if needed.
144 if (status == 0 && ep->addr == 0 && udc->ep0_dir == USB_DIR_OUT)
145 isp1760_udc_ctrl_send_status(ep, USB_DIR_OUT);
147 spin_unlock_irqrestore(&udc->lock, flags);
150 static void isp1760_udc_ctrl_send_stall(struct isp1760_ep *ep)
152 struct isp1760_udc *udc = ep->udc;
155 dev_dbg(ep->udc->isp->dev, "%s(ep%02x)\n", __func__, ep->addr);
157 spin_lock_irqsave(&udc->lock, flags);
159 /* Stall both the IN and OUT endpoints. */
160 __isp1760_udc_select_ep(ep, USB_DIR_OUT);
161 isp1760_udc_write(udc, DC_CTRLFUNC, DC_STALL);
162 __isp1760_udc_select_ep(ep, USB_DIR_IN);
163 isp1760_udc_write(udc, DC_CTRLFUNC, DC_STALL);
165 /* A protocol stall completes the control transaction. */
166 udc->ep0_state = ISP1760_CTRL_SETUP;
168 spin_unlock_irqrestore(&udc->lock, flags);
171 /* -----------------------------------------------------------------------------
175 /* Called with the UDC spinlock held. */
176 static bool isp1760_udc_receive(struct isp1760_ep *ep,
177 struct isp1760_request *req)
179 struct isp1760_udc *udc = ep->udc;
184 isp1760_udc_select_ep(ep);
185 len = isp1760_udc_read(udc, DC_BUFLEN) & DC_DATACOUNT_MASK;
187 dev_dbg(udc->isp->dev, "%s: received %u bytes (%u/%u done)\n",
188 __func__, len, req->req.actual, req->req.length);
190 len = min(len, req->req.length - req->req.actual);
194 * There's no data to be read from the FIFO, acknowledge the RX
195 * interrupt by clearing the buffer.
197 * TODO: What if another packet arrives in the meantime ? The
198 * datasheet doesn't clearly document how this should be
201 isp1760_udc_write(udc, DC_CTRLFUNC, DC_CLBUF);
205 buf = req->req.buf + req->req.actual;
208 * Make sure not to read more than one extra byte, otherwise data from
209 * the next packet might be removed from the FIFO.
211 for (i = len; i > 2; i -= 4, ++buf)
212 *buf = le32_to_cpu(isp1760_udc_read(udc, DC_DATAPORT));
214 *(u16 *)buf = le16_to_cpu(readw(udc->regs + DC_DATAPORT));
216 req->req.actual += len;
219 * TODO: The short_not_ok flag isn't supported yet, but isn't used by
220 * any gadget driver either.
223 dev_dbg(udc->isp->dev,
224 "%s: req %p actual/length %u/%u maxpacket %u packet size %u\n",
225 __func__, req, req->req.actual, req->req.length, ep->maxpacket,
228 ep->rx_pending = false;
231 * Complete the request if all data has been received or if a short
232 * packet has been received.
234 if (req->req.actual == req->req.length || len < ep->maxpacket) {
235 list_del(&req->queue);
242 static void isp1760_udc_transmit(struct isp1760_ep *ep,
243 struct isp1760_request *req)
245 struct isp1760_udc *udc = ep->udc;
246 u32 *buf = req->req.buf + req->req.actual;
249 req->packet_size = min(req->req.length - req->req.actual,
252 dev_dbg(udc->isp->dev, "%s: transferring %u bytes (%u/%u done)\n",
253 __func__, req->packet_size, req->req.actual,
256 __isp1760_udc_select_ep(ep, USB_DIR_IN);
258 if (req->packet_size)
259 isp1760_udc_write(udc, DC_BUFLEN, req->packet_size);
262 * Make sure not to write more than one extra byte, otherwise extra data
263 * will stay in the FIFO and will be transmitted during the next control
264 * request. The endpoint control CLBUF bit is supposed to allow flushing
265 * the FIFO for this kind of conditions, but doesn't seem to work.
267 for (i = req->packet_size; i > 2; i -= 4, ++buf)
268 isp1760_udc_write(udc, DC_DATAPORT, cpu_to_le32(*buf));
270 writew(cpu_to_le16(*(u16 *)buf), udc->regs + DC_DATAPORT);
273 isp1760_udc_write(udc, DC_CTRLFUNC, DC_DSEN);
274 if (!req->packet_size)
275 isp1760_udc_write(udc, DC_CTRLFUNC, DC_VENDP);
278 static void isp1760_ep_rx_ready(struct isp1760_ep *ep)
280 struct isp1760_udc *udc = ep->udc;
281 struct isp1760_request *req;
284 spin_lock(&udc->lock);
286 if (ep->addr == 0 && udc->ep0_state != ISP1760_CTRL_DATA_OUT) {
287 spin_unlock(&udc->lock);
288 dev_dbg(udc->isp->dev, "%s: invalid ep0 state %u\n", __func__,
293 if (ep->addr != 0 && !ep->desc) {
294 spin_unlock(&udc->lock);
295 dev_dbg(udc->isp->dev, "%s: ep%02x is disabled\n", __func__,
300 if (list_empty(&ep->queue)) {
301 ep->rx_pending = true;
302 spin_unlock(&udc->lock);
303 dev_dbg(udc->isp->dev, "%s: ep%02x (%p) has no request queued\n",
304 __func__, ep->addr, ep);
308 req = list_first_entry(&ep->queue, struct isp1760_request,
310 complete = isp1760_udc_receive(ep, req);
312 spin_unlock(&udc->lock);
315 isp1760_udc_request_complete(ep, req, 0);
318 static void isp1760_ep_tx_complete(struct isp1760_ep *ep)
320 struct isp1760_udc *udc = ep->udc;
321 struct isp1760_request *complete = NULL;
322 struct isp1760_request *req;
325 spin_lock(&udc->lock);
327 if (ep->addr == 0 && udc->ep0_state != ISP1760_CTRL_DATA_IN) {
328 spin_unlock(&udc->lock);
329 dev_dbg(udc->isp->dev, "TX IRQ: invalid endpoint state %u\n",
334 if (list_empty(&ep->queue)) {
336 * This can happen for the control endpoint when the reply to
337 * the GET_STATUS IN control request is sent directly by the
338 * setup IRQ handler. Just proceed to the status stage.
341 isp1760_udc_ctrl_send_status(ep, USB_DIR_IN);
342 spin_unlock(&udc->lock);
346 spin_unlock(&udc->lock);
347 dev_dbg(udc->isp->dev, "%s: ep%02x has no request queued\n",
352 req = list_first_entry(&ep->queue, struct isp1760_request,
354 req->req.actual += req->packet_size;
356 need_zlp = req->req.actual == req->req.length &&
357 !(req->req.length % ep->maxpacket) &&
358 req->packet_size && req->req.zero;
360 dev_dbg(udc->isp->dev,
361 "TX IRQ: req %p actual/length %u/%u maxpacket %u packet size %u zero %u need zlp %u\n",
362 req, req->req.actual, req->req.length, ep->maxpacket,
363 req->packet_size, req->req.zero, need_zlp);
366 * Complete the request if all data has been sent and we don't need to
367 * transmit a zero length packet.
369 if (req->req.actual == req->req.length && !need_zlp) {
371 list_del(&req->queue);
374 isp1760_udc_ctrl_send_status(ep, USB_DIR_IN);
376 if (!list_empty(&ep->queue))
377 req = list_first_entry(&ep->queue,
378 struct isp1760_request, queue);
384 * Transmit the next packet or start the next request, if any.
386 * TODO: If the endpoint is stalled the next request shouldn't be
387 * started, but what about the next packet ?
390 isp1760_udc_transmit(ep, req);
392 spin_unlock(&udc->lock);
395 isp1760_udc_request_complete(ep, complete, 0);
398 static int __isp1760_udc_set_halt(struct isp1760_ep *ep, bool halt)
400 struct isp1760_udc *udc = ep->udc;
402 dev_dbg(udc->isp->dev, "%s: %s halt on ep%02x\n", __func__,
403 halt ? "set" : "clear", ep->addr);
405 if (ep->desc && usb_endpoint_xfer_isoc(ep->desc)) {
406 dev_dbg(udc->isp->dev, "%s: ep%02x is isochronous\n", __func__,
411 isp1760_udc_select_ep(ep);
412 isp1760_udc_write(udc, DC_CTRLFUNC, halt ? DC_STALL : 0);
415 /* When halting the control endpoint, stall both IN and OUT. */
416 __isp1760_udc_select_ep(ep, USB_DIR_IN);
417 isp1760_udc_write(udc, DC_CTRLFUNC, halt ? DC_STALL : 0);
419 /* Reset the data PID by cycling the endpoint enable bit. */
420 u16 eptype = isp1760_udc_read(udc, DC_EPTYPE);
422 isp1760_udc_write(udc, DC_EPTYPE, eptype & ~DC_EPENABLE);
423 isp1760_udc_write(udc, DC_EPTYPE, eptype);
426 * Disabling the endpoint emptied the transmit FIFO, fill it
427 * again if a request is pending.
429 * TODO: Does the gadget framework require synchronizatino with
430 * the TX IRQ handler ?
432 if ((ep->addr & USB_DIR_IN) && !list_empty(&ep->queue)) {
433 struct isp1760_request *req;
435 req = list_first_entry(&ep->queue,
436 struct isp1760_request, queue);
437 isp1760_udc_transmit(ep, req);
446 /* -----------------------------------------------------------------------------
450 static int isp1760_udc_get_status(struct isp1760_udc *udc,
451 const struct usb_ctrlrequest *req)
453 struct isp1760_ep *ep;
456 if (req->wLength != cpu_to_le16(2) || req->wValue != cpu_to_le16(0))
459 switch (req->bRequestType) {
460 case USB_DIR_IN | USB_RECIP_DEVICE:
461 status = udc->devstatus;
464 case USB_DIR_IN | USB_RECIP_INTERFACE:
468 case USB_DIR_IN | USB_RECIP_ENDPOINT:
469 ep = isp1760_udc_find_ep(udc, le16_to_cpu(req->wIndex));
475 status |= 1 << USB_ENDPOINT_HALT;
482 isp1760_udc_write(udc, DC_EPINDEX, DC_ENDPIDX(0) | DC_EPDIR);
483 isp1760_udc_write(udc, DC_BUFLEN, 2);
485 writew(cpu_to_le16(status), udc->regs + DC_DATAPORT);
487 isp1760_udc_write(udc, DC_CTRLFUNC, DC_DSEN);
489 dev_dbg(udc->isp->dev, "%s: status 0x%04x\n", __func__, status);
494 static int isp1760_udc_set_address(struct isp1760_udc *udc, u16 addr)
497 dev_dbg(udc->isp->dev, "invalid device address %u\n", addr);
501 if (udc->gadget.state != USB_STATE_DEFAULT &&
502 udc->gadget.state != USB_STATE_ADDRESS) {
503 dev_dbg(udc->isp->dev, "can't set address in state %u\n",
508 usb_gadget_set_state(&udc->gadget, addr ? USB_STATE_ADDRESS :
511 isp1760_udc_write(udc, DC_ADDRESS, DC_DEVEN | addr);
513 spin_lock(&udc->lock);
514 isp1760_udc_ctrl_send_status(&udc->ep[0], USB_DIR_OUT);
515 spin_unlock(&udc->lock);
520 static bool isp1760_ep0_setup_standard(struct isp1760_udc *udc,
521 struct usb_ctrlrequest *req)
525 switch (req->bRequest) {
526 case USB_REQ_GET_STATUS:
527 return isp1760_udc_get_status(udc, req);
529 case USB_REQ_CLEAR_FEATURE:
530 switch (req->bRequestType) {
531 case USB_DIR_OUT | USB_RECIP_DEVICE: {
532 /* TODO: Handle remote wakeup feature. */
536 case USB_DIR_OUT | USB_RECIP_ENDPOINT: {
537 u16 index = le16_to_cpu(req->wIndex);
538 struct isp1760_ep *ep;
540 if (req->wLength != cpu_to_le16(0) ||
541 req->wValue != cpu_to_le16(USB_ENDPOINT_HALT))
544 ep = isp1760_udc_find_ep(udc, index);
548 spin_lock(&udc->lock);
551 * If the endpoint is wedged only the gadget can clear
552 * the halt feature. Pretend success in that case, but
553 * keep the endpoint halted.
556 stall = __isp1760_udc_set_halt(ep, false);
561 isp1760_udc_ctrl_send_status(&udc->ep[0],
564 spin_unlock(&udc->lock);
573 case USB_REQ_SET_FEATURE:
574 switch (req->bRequestType) {
575 case USB_DIR_OUT | USB_RECIP_DEVICE: {
576 /* TODO: Handle remote wakeup and test mode features */
580 case USB_DIR_OUT | USB_RECIP_ENDPOINT: {
581 u16 index = le16_to_cpu(req->wIndex);
582 struct isp1760_ep *ep;
584 if (req->wLength != cpu_to_le16(0) ||
585 req->wValue != cpu_to_le16(USB_ENDPOINT_HALT))
588 ep = isp1760_udc_find_ep(udc, index);
592 spin_lock(&udc->lock);
594 stall = __isp1760_udc_set_halt(ep, true);
596 isp1760_udc_ctrl_send_status(&udc->ep[0],
599 spin_unlock(&udc->lock);
608 case USB_REQ_SET_ADDRESS:
609 if (req->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
612 return isp1760_udc_set_address(udc, le16_to_cpu(req->wValue));
614 case USB_REQ_SET_CONFIGURATION:
615 if (req->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
618 if (udc->gadget.state != USB_STATE_ADDRESS &&
619 udc->gadget.state != USB_STATE_CONFIGURED)
622 stall = udc->driver->setup(&udc->gadget, req) < 0;
626 usb_gadget_set_state(&udc->gadget, req->wValue ?
627 USB_STATE_CONFIGURED : USB_STATE_ADDRESS);
630 * SET_CONFIGURATION (and SET_INTERFACE) must reset the halt
631 * feature on all endpoints. There is however no need to do so
632 * explicitly here as the gadget driver will disable and
633 * reenable endpoints, clearing the halt feature.
638 return udc->driver->setup(&udc->gadget, req) < 0;
642 static void isp1760_ep0_setup(struct isp1760_udc *udc)
645 struct usb_ctrlrequest r;
651 spin_lock(&udc->lock);
653 isp1760_udc_write(udc, DC_EPINDEX, DC_EP0SETUP);
655 count = isp1760_udc_read(udc, DC_BUFLEN) & DC_DATACOUNT_MASK;
656 if (count != sizeof(req)) {
657 spin_unlock(&udc->lock);
659 dev_err(udc->isp->dev, "invalid length %u for setup packet\n",
662 isp1760_udc_ctrl_send_stall(&udc->ep[0]);
666 req.data[0] = isp1760_udc_read(udc, DC_DATAPORT);
667 req.data[1] = isp1760_udc_read(udc, DC_DATAPORT);
669 if (udc->ep0_state != ISP1760_CTRL_SETUP) {
670 spin_unlock(&udc->lock);
671 dev_dbg(udc->isp->dev, "unexpected SETUP packet\n");
675 /* Move to the data stage. */
677 udc->ep0_state = ISP1760_CTRL_STATUS;
678 else if (req.r.bRequestType & USB_DIR_IN)
679 udc->ep0_state = ISP1760_CTRL_DATA_IN;
681 udc->ep0_state = ISP1760_CTRL_DATA_OUT;
683 udc->ep0_dir = req.r.bRequestType & USB_DIR_IN;
684 udc->ep0_length = le16_to_cpu(req.r.wLength);
686 spin_unlock(&udc->lock);
688 dev_dbg(udc->isp->dev,
689 "%s: bRequestType 0x%02x bRequest 0x%02x wValue 0x%04x wIndex 0x%04x wLength 0x%04x\n",
690 __func__, req.r.bRequestType, req.r.bRequest,
691 le16_to_cpu(req.r.wValue), le16_to_cpu(req.r.wIndex),
692 le16_to_cpu(req.r.wLength));
694 if ((req.r.bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
695 stall = isp1760_ep0_setup_standard(udc, &req.r);
697 stall = udc->driver->setup(&udc->gadget, &req.r) < 0;
700 isp1760_udc_ctrl_send_stall(&udc->ep[0]);
703 /* -----------------------------------------------------------------------------
704 * Gadget Endpoint Operations
707 static int isp1760_ep_enable(struct usb_ep *ep,
708 const struct usb_endpoint_descriptor *desc)
710 struct isp1760_ep *uep = ep_to_udc_ep(ep);
711 struct isp1760_udc *udc = uep->udc;
715 dev_dbg(uep->udc->isp->dev, "%s\n", __func__);
718 * Validate the descriptor. The control endpoint can't be enabled
721 if (desc->bDescriptorType != USB_DT_ENDPOINT ||
722 desc->bEndpointAddress == 0 ||
723 desc->bEndpointAddress != uep->addr ||
724 le16_to_cpu(desc->wMaxPacketSize) > ep->maxpacket) {
725 dev_dbg(udc->isp->dev,
726 "%s: invalid descriptor type %u addr %02x ep addr %02x max packet size %u/%u\n",
727 __func__, desc->bDescriptorType,
728 desc->bEndpointAddress, uep->addr,
729 le16_to_cpu(desc->wMaxPacketSize), ep->maxpacket);
733 switch (usb_endpoint_type(desc)) {
734 case USB_ENDPOINT_XFER_ISOC:
735 type = DC_ENDPTYP_ISOC;
737 case USB_ENDPOINT_XFER_BULK:
738 type = DC_ENDPTYP_BULK;
740 case USB_ENDPOINT_XFER_INT:
741 type = DC_ENDPTYP_INTERRUPT;
743 case USB_ENDPOINT_XFER_CONTROL:
745 dev_dbg(udc->isp->dev, "%s: control endpoints unsupported\n",
750 spin_lock_irqsave(&udc->lock, flags);
753 uep->maxpacket = le16_to_cpu(desc->wMaxPacketSize);
754 uep->rx_pending = false;
758 isp1760_udc_select_ep(uep);
759 isp1760_udc_write(udc, DC_EPMAXPKTSZ, uep->maxpacket);
760 isp1760_udc_write(udc, DC_BUFLEN, uep->maxpacket);
761 isp1760_udc_write(udc, DC_EPTYPE, DC_EPENABLE | type);
763 spin_unlock_irqrestore(&udc->lock, flags);
768 static int isp1760_ep_disable(struct usb_ep *ep)
770 struct isp1760_ep *uep = ep_to_udc_ep(ep);
771 struct isp1760_udc *udc = uep->udc;
772 struct isp1760_request *req, *nreq;
776 dev_dbg(udc->isp->dev, "%s\n", __func__);
778 spin_lock_irqsave(&udc->lock, flags);
781 dev_dbg(udc->isp->dev, "%s: endpoint not enabled\n", __func__);
782 spin_unlock_irqrestore(&udc->lock, flags);
789 isp1760_udc_select_ep(uep);
790 isp1760_udc_write(udc, DC_EPTYPE, 0);
792 /* TODO Synchronize with the IRQ handler */
794 list_splice_init(&uep->queue, &req_list);
796 spin_unlock_irqrestore(&udc->lock, flags);
798 list_for_each_entry_safe(req, nreq, &req_list, queue) {
799 list_del(&req->queue);
800 isp1760_udc_request_complete(uep, req, -ESHUTDOWN);
806 static struct usb_request *isp1760_ep_alloc_request(struct usb_ep *ep,
809 struct isp1760_request *req;
811 req = kzalloc(sizeof(*req), gfp_flags);
818 static void isp1760_ep_free_request(struct usb_ep *ep, struct usb_request *_req)
820 struct isp1760_request *req = req_to_udc_req(_req);
825 static int isp1760_ep_queue(struct usb_ep *ep, struct usb_request *_req,
828 struct isp1760_request *req = req_to_udc_req(_req);
829 struct isp1760_ep *uep = ep_to_udc_ep(ep);
830 struct isp1760_udc *udc = uep->udc;
831 bool complete = false;
835 _req->status = -EINPROGRESS;
838 spin_lock_irqsave(&udc->lock, flags);
840 dev_dbg(udc->isp->dev,
841 "%s: req %p (%u bytes%s) ep %p(0x%02x)\n", __func__, _req,
842 _req->length, _req->zero ? " (zlp)" : "", uep, uep->addr);
846 if (uep->addr == 0) {
847 if (_req->length != udc->ep0_length &&
848 udc->ep0_state != ISP1760_CTRL_DATA_IN) {
849 dev_dbg(udc->isp->dev,
850 "%s: invalid length %u for req %p\n",
851 __func__, _req->length, req);
856 switch (udc->ep0_state) {
857 case ISP1760_CTRL_DATA_IN:
858 dev_dbg(udc->isp->dev, "%s: transmitting req %p\n",
861 list_add_tail(&req->queue, &uep->queue);
862 isp1760_udc_transmit(uep, req);
865 case ISP1760_CTRL_DATA_OUT:
866 list_add_tail(&req->queue, &uep->queue);
867 __isp1760_udc_select_ep(uep, USB_DIR_OUT);
868 isp1760_udc_write(udc, DC_CTRLFUNC, DC_DSEN);
871 case ISP1760_CTRL_STATUS:
876 dev_dbg(udc->isp->dev, "%s: invalid ep0 state\n",
881 } else if (uep->desc) {
882 bool empty = list_empty(&uep->queue);
884 list_add_tail(&req->queue, &uep->queue);
885 if ((uep->addr & USB_DIR_IN) && !uep->halted && empty)
886 isp1760_udc_transmit(uep, req);
887 else if (!(uep->addr & USB_DIR_IN) && uep->rx_pending)
888 complete = isp1760_udc_receive(uep, req);
890 dev_dbg(udc->isp->dev,
891 "%s: can't queue request to disabled ep%02x\n",
892 __func__, uep->addr);
900 spin_unlock_irqrestore(&udc->lock, flags);
903 isp1760_udc_request_complete(uep, req, 0);
908 static int isp1760_ep_dequeue(struct usb_ep *ep, struct usb_request *_req)
910 struct isp1760_request *req = req_to_udc_req(_req);
911 struct isp1760_ep *uep = ep_to_udc_ep(ep);
912 struct isp1760_udc *udc = uep->udc;
915 dev_dbg(uep->udc->isp->dev, "%s(ep%02x)\n", __func__, uep->addr);
917 spin_lock_irqsave(&udc->lock, flags);
922 list_del(&req->queue);
924 spin_unlock_irqrestore(&udc->lock, flags);
929 isp1760_udc_request_complete(uep, req, -ECONNRESET);
933 static int __isp1760_ep_set_halt(struct isp1760_ep *uep, bool stall, bool wedge)
935 struct isp1760_udc *udc = uep->udc;
940 * Halting the control endpoint is only valid as a delayed error
941 * response to a SETUP packet. Make sure EP0 is in the right
942 * stage and that the gadget isn't trying to clear the halt
945 if (WARN_ON(udc->ep0_state == ISP1760_CTRL_SETUP || !stall ||
951 if (uep->addr && !uep->desc) {
952 dev_dbg(udc->isp->dev, "%s: ep%02x is disabled\n", __func__,
957 if (uep->addr & USB_DIR_IN) {
958 /* Refuse to halt IN endpoints with active transfers. */
959 if (!list_empty(&uep->queue)) {
960 dev_dbg(udc->isp->dev,
961 "%s: ep%02x has request pending\n", __func__,
967 ret = __isp1760_udc_set_halt(uep, stall);
973 * Stalling EP0 completes the control transaction, move back to
976 udc->ep0_state = ISP1760_CTRL_SETUP;
988 static int isp1760_ep_set_halt(struct usb_ep *ep, int value)
990 struct isp1760_ep *uep = ep_to_udc_ep(ep);
994 dev_dbg(uep->udc->isp->dev, "%s: %s halt on ep%02x\n", __func__,
995 value ? "set" : "clear", uep->addr);
997 spin_lock_irqsave(&uep->udc->lock, flags);
998 ret = __isp1760_ep_set_halt(uep, value, false);
999 spin_unlock_irqrestore(&uep->udc->lock, flags);
1004 static int isp1760_ep_set_wedge(struct usb_ep *ep)
1006 struct isp1760_ep *uep = ep_to_udc_ep(ep);
1007 unsigned long flags;
1010 dev_dbg(uep->udc->isp->dev, "%s: set wedge on ep%02x)\n", __func__,
1013 spin_lock_irqsave(&uep->udc->lock, flags);
1014 ret = __isp1760_ep_set_halt(uep, true, true);
1015 spin_unlock_irqrestore(&uep->udc->lock, flags);
1020 static void isp1760_ep_fifo_flush(struct usb_ep *ep)
1022 struct isp1760_ep *uep = ep_to_udc_ep(ep);
1023 struct isp1760_udc *udc = uep->udc;
1024 unsigned long flags;
1026 spin_lock_irqsave(&udc->lock, flags);
1028 isp1760_udc_select_ep(uep);
1031 * Set the CLBUF bit twice to flush both buffers in case double
1032 * buffering is enabled.
1034 isp1760_udc_write(udc, DC_CTRLFUNC, DC_CLBUF);
1035 isp1760_udc_write(udc, DC_CTRLFUNC, DC_CLBUF);
1037 spin_unlock_irqrestore(&udc->lock, flags);
1040 static const struct usb_ep_ops isp1760_ep_ops = {
1041 .enable = isp1760_ep_enable,
1042 .disable = isp1760_ep_disable,
1043 .alloc_request = isp1760_ep_alloc_request,
1044 .free_request = isp1760_ep_free_request,
1045 .queue = isp1760_ep_queue,
1046 .dequeue = isp1760_ep_dequeue,
1047 .set_halt = isp1760_ep_set_halt,
1048 .set_wedge = isp1760_ep_set_wedge,
1049 .fifo_flush = isp1760_ep_fifo_flush,
1052 /* -----------------------------------------------------------------------------
1056 /* Called with the UDC spinlock held. */
1057 static void isp1760_udc_connect(struct isp1760_udc *udc)
1059 usb_gadget_set_state(&udc->gadget, USB_STATE_POWERED);
1060 mod_timer(&udc->vbus_timer, jiffies + ISP1760_VBUS_POLL_INTERVAL);
1063 /* Called with the UDC spinlock held. */
1064 static void isp1760_udc_disconnect(struct isp1760_udc *udc)
1066 if (udc->gadget.state < USB_STATE_POWERED)
1069 dev_dbg(udc->isp->dev, "Device disconnected in state %u\n",
1072 udc->gadget.speed = USB_SPEED_UNKNOWN;
1073 usb_gadget_set_state(&udc->gadget, USB_STATE_ATTACHED);
1075 if (udc->driver->disconnect)
1076 udc->driver->disconnect(&udc->gadget);
1078 del_timer(&udc->vbus_timer);
1080 /* TODO Reset all endpoints ? */
1083 static void isp1760_udc_init_hw(struct isp1760_udc *udc)
1086 * The device controller currently shares its interrupt with the host
1087 * controller, the DC_IRQ polarity and signaling mode are ignored. Set
1088 * the to active-low level-triggered.
1090 * Configure the control, in and out pipes to generate interrupts on
1091 * ACK tokens only (and NYET for the out pipe). The default
1092 * configuration also generates an interrupt on the first NACK token.
1094 isp1760_udc_write(udc, DC_INTCONF, DC_CDBGMOD_ACK | DC_DDBGMODIN_ACK |
1095 DC_DDBGMODOUT_ACK_NYET);
1097 isp1760_udc_write(udc, DC_INTENABLE, DC_IEPRXTX(7) | DC_IEPRXTX(6) |
1098 DC_IEPRXTX(5) | DC_IEPRXTX(4) | DC_IEPRXTX(3) |
1099 DC_IEPRXTX(2) | DC_IEPRXTX(1) | DC_IEPRXTX(0) |
1100 DC_IEP0SETUP | DC_IEVBUS | DC_IERESM | DC_IESUSP |
1101 DC_IEHS_STA | DC_IEBRST);
1104 isp1760_set_pullup(udc->isp, true);
1106 isp1760_udc_write(udc, DC_ADDRESS, DC_DEVEN);
1109 static void isp1760_udc_reset(struct isp1760_udc *udc)
1111 unsigned long flags;
1113 spin_lock_irqsave(&udc->lock, flags);
1116 * The bus reset has reset most registers to their default value,
1117 * reinitialize the UDC hardware.
1119 isp1760_udc_init_hw(udc);
1121 udc->ep0_state = ISP1760_CTRL_SETUP;
1122 udc->gadget.speed = USB_SPEED_FULL;
1124 usb_gadget_udc_reset(&udc->gadget, udc->driver);
1126 spin_unlock_irqrestore(&udc->lock, flags);
1129 static void isp1760_udc_suspend(struct isp1760_udc *udc)
1131 if (udc->gadget.state < USB_STATE_DEFAULT)
1134 if (udc->driver->suspend)
1135 udc->driver->suspend(&udc->gadget);
1138 static void isp1760_udc_resume(struct isp1760_udc *udc)
1140 if (udc->gadget.state < USB_STATE_DEFAULT)
1143 if (udc->driver->resume)
1144 udc->driver->resume(&udc->gadget);
1147 /* -----------------------------------------------------------------------------
1151 static int isp1760_udc_get_frame(struct usb_gadget *gadget)
1153 struct isp1760_udc *udc = gadget_to_udc(gadget);
1155 return isp1760_udc_read(udc, DC_FRAMENUM) & ((1 << 11) - 1);
1158 static int isp1760_udc_wakeup(struct usb_gadget *gadget)
1160 struct isp1760_udc *udc = gadget_to_udc(gadget);
1162 dev_dbg(udc->isp->dev, "%s\n", __func__);
1166 static int isp1760_udc_set_selfpowered(struct usb_gadget *gadget,
1169 struct isp1760_udc *udc = gadget_to_udc(gadget);
1172 udc->devstatus |= 1 << USB_DEVICE_SELF_POWERED;
1174 udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
1179 static int isp1760_udc_pullup(struct usb_gadget *gadget, int is_on)
1181 struct isp1760_udc *udc = gadget_to_udc(gadget);
1183 isp1760_set_pullup(udc->isp, is_on);
1184 udc->connected = is_on;
1189 static int isp1760_udc_start(struct usb_gadget *gadget,
1190 struct usb_gadget_driver *driver)
1192 struct isp1760_udc *udc = gadget_to_udc(gadget);
1193 unsigned long flags;
1195 /* The hardware doesn't support low speed. */
1196 if (driver->max_speed < USB_SPEED_FULL) {
1197 dev_err(udc->isp->dev, "Invalid gadget driver\n");
1201 spin_lock_irqsave(&udc->lock, flags);
1204 dev_err(udc->isp->dev, "UDC already has a gadget driver\n");
1205 spin_unlock_irqrestore(&udc->lock, flags);
1209 udc->driver = driver;
1211 spin_unlock_irqrestore(&udc->lock, flags);
1213 dev_dbg(udc->isp->dev, "starting UDC with driver %s\n",
1217 udc->connected = true;
1219 usb_gadget_set_state(&udc->gadget, USB_STATE_ATTACHED);
1221 /* DMA isn't supported yet, don't enable the DMA clock. */
1222 isp1760_udc_write(udc, DC_MODE, DC_GLINTENA);
1224 isp1760_udc_init_hw(udc);
1226 dev_dbg(udc->isp->dev, "UDC started with driver %s\n",
1232 static int isp1760_udc_stop(struct usb_gadget *gadget)
1234 struct isp1760_udc *udc = gadget_to_udc(gadget);
1235 unsigned long flags;
1237 dev_dbg(udc->isp->dev, "%s\n", __func__);
1239 del_timer_sync(&udc->vbus_timer);
1241 isp1760_udc_write(udc, DC_MODE, 0);
1243 spin_lock_irqsave(&udc->lock, flags);
1245 spin_unlock_irqrestore(&udc->lock, flags);
1250 static const struct usb_gadget_ops isp1760_udc_ops = {
1251 .get_frame = isp1760_udc_get_frame,
1252 .wakeup = isp1760_udc_wakeup,
1253 .set_selfpowered = isp1760_udc_set_selfpowered,
1254 .pullup = isp1760_udc_pullup,
1255 .udc_start = isp1760_udc_start,
1256 .udc_stop = isp1760_udc_stop,
1259 /* -----------------------------------------------------------------------------
1260 * Interrupt Handling
1263 static irqreturn_t isp1760_udc_irq(int irq, void *dev)
1265 struct isp1760_udc *udc = dev;
1269 status = isp1760_udc_read(udc, DC_INTERRUPT)
1270 & isp1760_udc_read(udc, DC_INTENABLE);
1271 isp1760_udc_write(udc, DC_INTERRUPT, status);
1273 if (status & DC_IEVBUS) {
1274 dev_dbg(udc->isp->dev, "%s(VBUS)\n", __func__);
1275 /* The VBUS interrupt is only triggered when VBUS appears. */
1276 spin_lock(&udc->lock);
1277 isp1760_udc_connect(udc);
1278 spin_unlock(&udc->lock);
1281 if (status & DC_IEBRST) {
1282 dev_dbg(udc->isp->dev, "%s(BRST)\n", __func__);
1284 isp1760_udc_reset(udc);
1287 for (i = 0; i <= 7; ++i) {
1288 struct isp1760_ep *ep = &udc->ep[i*2];
1290 if (status & DC_IEPTX(i)) {
1291 dev_dbg(udc->isp->dev, "%s(EPTX%u)\n", __func__, i);
1292 isp1760_ep_tx_complete(ep);
1295 if (status & DC_IEPRX(i)) {
1296 dev_dbg(udc->isp->dev, "%s(EPRX%u)\n", __func__, i);
1297 isp1760_ep_rx_ready(i ? ep - 1 : ep);
1301 if (status & DC_IEP0SETUP) {
1302 dev_dbg(udc->isp->dev, "%s(EP0SETUP)\n", __func__);
1304 isp1760_ep0_setup(udc);
1307 if (status & DC_IERESM) {
1308 dev_dbg(udc->isp->dev, "%s(RESM)\n", __func__);
1309 isp1760_udc_resume(udc);
1312 if (status & DC_IESUSP) {
1313 dev_dbg(udc->isp->dev, "%s(SUSP)\n", __func__);
1315 spin_lock(&udc->lock);
1316 if (!(isp1760_udc_read(udc, DC_MODE) & DC_VBUSSTAT))
1317 isp1760_udc_disconnect(udc);
1319 isp1760_udc_suspend(udc);
1320 spin_unlock(&udc->lock);
1323 if (status & DC_IEHS_STA) {
1324 dev_dbg(udc->isp->dev, "%s(HS_STA)\n", __func__);
1325 udc->gadget.speed = USB_SPEED_HIGH;
1328 return status ? IRQ_HANDLED : IRQ_NONE;
1331 static void isp1760_udc_vbus_poll(struct timer_list *t)
1333 struct isp1760_udc *udc = from_timer(udc, t, vbus_timer);
1334 unsigned long flags;
1336 spin_lock_irqsave(&udc->lock, flags);
1338 if (!(isp1760_udc_read(udc, DC_MODE) & DC_VBUSSTAT))
1339 isp1760_udc_disconnect(udc);
1340 else if (udc->gadget.state >= USB_STATE_POWERED)
1341 mod_timer(&udc->vbus_timer,
1342 jiffies + ISP1760_VBUS_POLL_INTERVAL);
1344 spin_unlock_irqrestore(&udc->lock, flags);
1347 /* -----------------------------------------------------------------------------
1351 static void isp1760_udc_init_eps(struct isp1760_udc *udc)
1355 INIT_LIST_HEAD(&udc->gadget.ep_list);
1357 for (i = 0; i < ARRAY_SIZE(udc->ep); ++i) {
1358 struct isp1760_ep *ep = &udc->ep[i];
1359 unsigned int ep_num = (i + 1) / 2;
1360 bool is_in = !(i & 1);
1364 INIT_LIST_HEAD(&ep->queue);
1366 ep->addr = (ep_num && is_in ? USB_DIR_IN : USB_DIR_OUT)
1370 sprintf(ep->name, "ep%u%s", ep_num,
1371 ep_num ? (is_in ? "in" : "out") : "");
1373 ep->ep.ops = &isp1760_ep_ops;
1374 ep->ep.name = ep->name;
1377 * Hardcode the maximum packet sizes for now, to 64 bytes for
1378 * the control endpoint and 512 bytes for all other endpoints.
1379 * This fits in the 8kB FIFO without double-buffering.
1382 usb_ep_set_maxpacket_limit(&ep->ep, 64);
1383 ep->ep.caps.type_control = true;
1384 ep->ep.caps.dir_in = true;
1385 ep->ep.caps.dir_out = true;
1387 udc->gadget.ep0 = &ep->ep;
1389 usb_ep_set_maxpacket_limit(&ep->ep, 512);
1390 ep->ep.caps.type_iso = true;
1391 ep->ep.caps.type_bulk = true;
1392 ep->ep.caps.type_int = true;
1394 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
1398 ep->ep.caps.dir_in = true;
1400 ep->ep.caps.dir_out = true;
1404 static int isp1760_udc_init(struct isp1760_udc *udc)
1410 * Check that the controller is present by writing to the scratch
1411 * register, modifying the bus pattern by reading from the chip ID
1412 * register, and reading the scratch register value back. The chip ID
1413 * and scratch register contents must match the expected values.
1415 isp1760_udc_write(udc, DC_SCRATCH, 0xbabe);
1416 chipid = isp1760_udc_read(udc, DC_CHIPID);
1417 scratch = isp1760_udc_read(udc, DC_SCRATCH);
1419 if (scratch != 0xbabe) {
1420 dev_err(udc->isp->dev,
1421 "udc: scratch test failed (0x%04x/0x%08x)\n",
1426 if (chipid != 0x00011582 && chipid != 0x00158210) {
1427 dev_err(udc->isp->dev, "udc: invalid chip ID 0x%08x\n", chipid);
1431 /* Reset the device controller. */
1432 isp1760_udc_write(udc, DC_MODE, DC_SFRESET);
1433 usleep_range(10000, 11000);
1434 isp1760_udc_write(udc, DC_MODE, 0);
1435 usleep_range(10000, 11000);
1440 int isp1760_udc_register(struct isp1760_device *isp, int irq,
1441 unsigned long irqflags)
1443 struct isp1760_udc *udc = &isp->udc;
1448 udc->regs = isp->regs;
1450 spin_lock_init(&udc->lock);
1451 timer_setup(&udc->vbus_timer, isp1760_udc_vbus_poll, 0);
1453 ret = isp1760_udc_init(udc);
1457 udc->irqname = kasprintf(GFP_KERNEL, "%s (udc)", dev_name(isp->dev));
1461 ret = request_irq(irq, isp1760_udc_irq, IRQF_SHARED | irqflags,
1469 * Initialize the gadget static fields and register its device. Gadget
1470 * fields that vary during the life time of the gadget are initialized
1473 udc->gadget.ops = &isp1760_udc_ops;
1474 udc->gadget.speed = USB_SPEED_UNKNOWN;
1475 udc->gadget.max_speed = USB_SPEED_HIGH;
1476 udc->gadget.name = "isp1761_udc";
1478 isp1760_udc_init_eps(udc);
1480 ret = usb_add_gadget_udc(isp->dev, &udc->gadget);
1488 free_irq(udc->irq, udc);
1489 kfree(udc->irqname);
1494 void isp1760_udc_unregister(struct isp1760_device *isp)
1496 struct isp1760_udc *udc = &isp->udc;
1501 usb_del_gadget_udc(&udc->gadget);
1503 free_irq(udc->irq, udc);
1504 kfree(udc->irqname);