2 * Driver for the NXP ISP1761 device controller
4 * Copyright 2014 Ideas on Board Oy
7 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
14 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/timer.h>
21 #include <linux/usb.h>
23 #include "isp1760-core.h"
24 #include "isp1760-regs.h"
25 #include "isp1760-udc.h"
27 #define ISP1760_VBUS_POLL_INTERVAL msecs_to_jiffies(500)
29 struct isp1760_request
{
30 struct usb_request req
;
31 struct list_head queue
;
32 struct isp1760_ep
*ep
;
33 unsigned int packet_size
;
36 static inline struct isp1760_udc
*gadget_to_udc(struct usb_gadget
*gadget
)
38 return container_of(gadget
, struct isp1760_udc
, gadget
);
41 static inline struct isp1760_ep
*ep_to_udc_ep(struct usb_ep
*ep
)
43 return container_of(ep
, struct isp1760_ep
, ep
);
46 static inline struct isp1760_request
*req_to_udc_req(struct usb_request
*req
)
48 return container_of(req
, struct isp1760_request
, req
);
51 static inline u32
isp1760_udc_read(struct isp1760_udc
*udc
, u16 reg
)
53 return isp1760_read32(udc
->regs
, reg
);
56 static inline void isp1760_udc_write(struct isp1760_udc
*udc
, u16 reg
, u32 val
)
58 isp1760_write32(udc
->regs
, reg
, val
);
61 /* -----------------------------------------------------------------------------
65 static struct isp1760_ep
*isp1760_udc_find_ep(struct isp1760_udc
*udc
,
73 for (i
= 1; i
< ARRAY_SIZE(udc
->ep
); ++i
) {
74 if (udc
->ep
[i
].addr
== index
)
75 return udc
->ep
[i
].desc
? &udc
->ep
[i
] : NULL
;
81 static void __isp1760_udc_select_ep(struct isp1760_ep
*ep
, int dir
)
83 isp1760_udc_write(ep
->udc
, DC_EPINDEX
,
84 DC_ENDPIDX(ep
->addr
& USB_ENDPOINT_NUMBER_MASK
) |
85 (dir
== USB_DIR_IN
? DC_EPDIR
: 0));
89 * isp1760_udc_select_ep - Select an endpoint for register access
92 * The ISP1761 endpoint registers are banked. This function selects the target
93 * endpoint for banked register access. The selection remains valid until the
94 * next call to this function, the next direct access to the EPINDEX register
95 * or the next reset, whichever comes first.
97 * Called with the UDC spinlock held.
99 static void isp1760_udc_select_ep(struct isp1760_ep
*ep
)
101 __isp1760_udc_select_ep(ep
, ep
->addr
& USB_ENDPOINT_DIR_MASK
);
104 /* Called with the UDC spinlock held. */
105 static void isp1760_udc_ctrl_send_status(struct isp1760_ep
*ep
, int dir
)
107 struct isp1760_udc
*udc
= ep
->udc
;
110 * Proceed to the status stage. The status stage data packet flows in
111 * the direction opposite to the data stage data packets, we thus need
112 * to select the OUT/IN endpoint for IN/OUT transfers.
114 isp1760_udc_write(udc
, DC_EPINDEX
, DC_ENDPIDX(0) |
115 (dir
== USB_DIR_IN
? 0 : DC_EPDIR
));
116 isp1760_udc_write(udc
, DC_CTRLFUNC
, DC_STATUS
);
119 * The hardware will terminate the request automatically and go back to
120 * the setup stage without notifying us.
122 udc
->ep0_state
= ISP1760_CTRL_SETUP
;
125 /* Called without the UDC spinlock held. */
126 static void isp1760_udc_request_complete(struct isp1760_ep
*ep
,
127 struct isp1760_request
*req
,
130 struct isp1760_udc
*udc
= ep
->udc
;
133 dev_dbg(ep
->udc
->isp
->dev
, "completing request %p with status %d\n",
137 req
->req
.status
= status
;
138 req
->req
.complete(&ep
->ep
, &req
->req
);
140 spin_lock_irqsave(&udc
->lock
, flags
);
143 * When completing control OUT requests, move to the status stage after
144 * calling the request complete callback. This gives the gadget an
145 * opportunity to stall the control transfer if needed.
147 if (status
== 0 && ep
->addr
== 0 && udc
->ep0_dir
== USB_DIR_OUT
)
148 isp1760_udc_ctrl_send_status(ep
, USB_DIR_OUT
);
150 spin_unlock_irqrestore(&udc
->lock
, flags
);
153 static void isp1760_udc_ctrl_send_stall(struct isp1760_ep
*ep
)
155 struct isp1760_udc
*udc
= ep
->udc
;
158 dev_dbg(ep
->udc
->isp
->dev
, "%s(ep%02x)\n", __func__
, ep
->addr
);
160 spin_lock_irqsave(&udc
->lock
, flags
);
162 /* Stall both the IN and OUT endpoints. */
163 __isp1760_udc_select_ep(ep
, USB_DIR_OUT
);
164 isp1760_udc_write(udc
, DC_CTRLFUNC
, DC_STALL
);
165 __isp1760_udc_select_ep(ep
, USB_DIR_IN
);
166 isp1760_udc_write(udc
, DC_CTRLFUNC
, DC_STALL
);
168 /* A protocol stall completes the control transaction. */
169 udc
->ep0_state
= ISP1760_CTRL_SETUP
;
171 spin_unlock_irqrestore(&udc
->lock
, flags
);
174 /* -----------------------------------------------------------------------------
178 /* Called with the UDC spinlock held. */
179 static bool isp1760_udc_receive(struct isp1760_ep
*ep
,
180 struct isp1760_request
*req
)
182 struct isp1760_udc
*udc
= ep
->udc
;
187 isp1760_udc_select_ep(ep
);
188 len
= isp1760_udc_read(udc
, DC_BUFLEN
) & DC_DATACOUNT_MASK
;
190 dev_dbg(udc
->isp
->dev
, "%s: received %u bytes (%u/%u done)\n",
191 __func__
, len
, req
->req
.actual
, req
->req
.length
);
193 len
= min(len
, req
->req
.length
- req
->req
.actual
);
197 * There's no data to be read from the FIFO, acknowledge the RX
198 * interrupt by clearing the buffer.
200 * TODO: What if another packet arrives in the meantime ? The
201 * datasheet doesn't clearly document how this should be
204 isp1760_udc_write(udc
, DC_CTRLFUNC
, DC_CLBUF
);
208 buf
= req
->req
.buf
+ req
->req
.actual
;
211 * Make sure not to read more than one extra byte, otherwise data from
212 * the next packet might be removed from the FIFO.
214 for (i
= len
; i
> 2; i
-= 4, ++buf
)
215 *buf
= le32_to_cpu(isp1760_udc_read(udc
, DC_DATAPORT
));
217 *(u16
*)buf
= le16_to_cpu(readw(udc
->regs
+ DC_DATAPORT
));
219 req
->req
.actual
+= len
;
222 * TODO: The short_not_ok flag isn't supported yet, but isn't used by
223 * any gadget driver either.
226 dev_dbg(udc
->isp
->dev
,
227 "%s: req %p actual/length %u/%u maxpacket %u packet size %u\n",
228 __func__
, req
, req
->req
.actual
, req
->req
.length
, ep
->maxpacket
,
231 ep
->rx_pending
= false;
234 * Complete the request if all data has been received or if a short
235 * packet has been received.
237 if (req
->req
.actual
== req
->req
.length
|| len
< ep
->maxpacket
) {
238 list_del(&req
->queue
);
245 static void isp1760_udc_transmit(struct isp1760_ep
*ep
,
246 struct isp1760_request
*req
)
248 struct isp1760_udc
*udc
= ep
->udc
;
249 u32
*buf
= req
->req
.buf
+ req
->req
.actual
;
252 req
->packet_size
= min(req
->req
.length
- req
->req
.actual
,
255 dev_dbg(udc
->isp
->dev
, "%s: transferring %u bytes (%u/%u done)\n",
256 __func__
, req
->packet_size
, req
->req
.actual
,
259 __isp1760_udc_select_ep(ep
, USB_DIR_IN
);
261 if (req
->packet_size
)
262 isp1760_udc_write(udc
, DC_BUFLEN
, req
->packet_size
);
265 * Make sure not to write more than one extra byte, otherwise extra data
266 * will stay in the FIFO and will be transmitted during the next control
267 * request. The endpoint control CLBUF bit is supposed to allow flushing
268 * the FIFO for this kind of conditions, but doesn't seem to work.
270 for (i
= req
->packet_size
; i
> 2; i
-= 4, ++buf
)
271 isp1760_udc_write(udc
, DC_DATAPORT
, cpu_to_le32(*buf
));
273 writew(cpu_to_le16(*(u16
*)buf
), udc
->regs
+ DC_DATAPORT
);
276 isp1760_udc_write(udc
, DC_CTRLFUNC
, DC_DSEN
);
277 if (!req
->packet_size
)
278 isp1760_udc_write(udc
, DC_CTRLFUNC
, DC_VENDP
);
281 static void isp1760_ep_rx_ready(struct isp1760_ep
*ep
)
283 struct isp1760_udc
*udc
= ep
->udc
;
284 struct isp1760_request
*req
;
287 spin_lock(&udc
->lock
);
289 if (ep
->addr
== 0 && udc
->ep0_state
!= ISP1760_CTRL_DATA_OUT
) {
290 spin_unlock(&udc
->lock
);
291 dev_dbg(udc
->isp
->dev
, "%s: invalid ep0 state %u\n", __func__
,
296 if (ep
->addr
!= 0 && !ep
->desc
) {
297 spin_unlock(&udc
->lock
);
298 dev_dbg(udc
->isp
->dev
, "%s: ep%02x is disabled\n", __func__
,
303 if (list_empty(&ep
->queue
)) {
304 ep
->rx_pending
= true;
305 spin_unlock(&udc
->lock
);
306 dev_dbg(udc
->isp
->dev
, "%s: ep%02x (%p) has no request queued\n",
307 __func__
, ep
->addr
, ep
);
311 req
= list_first_entry(&ep
->queue
, struct isp1760_request
,
313 complete
= isp1760_udc_receive(ep
, req
);
315 spin_unlock(&udc
->lock
);
318 isp1760_udc_request_complete(ep
, req
, 0);
321 static void isp1760_ep_tx_complete(struct isp1760_ep
*ep
)
323 struct isp1760_udc
*udc
= ep
->udc
;
324 struct isp1760_request
*complete
= NULL
;
325 struct isp1760_request
*req
;
328 spin_lock(&udc
->lock
);
330 if (ep
->addr
== 0 && udc
->ep0_state
!= ISP1760_CTRL_DATA_IN
) {
331 spin_unlock(&udc
->lock
);
332 dev_dbg(udc
->isp
->dev
, "TX IRQ: invalid endpoint state %u\n",
337 if (list_empty(&ep
->queue
)) {
339 * This can happen for the control endpoint when the reply to
340 * the GET_STATUS IN control request is sent directly by the
341 * setup IRQ handler. Just proceed to the status stage.
344 isp1760_udc_ctrl_send_status(ep
, USB_DIR_IN
);
345 spin_unlock(&udc
->lock
);
349 spin_unlock(&udc
->lock
);
350 dev_dbg(udc
->isp
->dev
, "%s: ep%02x has no request queued\n",
355 req
= list_first_entry(&ep
->queue
, struct isp1760_request
,
357 req
->req
.actual
+= req
->packet_size
;
359 need_zlp
= req
->req
.actual
== req
->req
.length
&&
360 !(req
->req
.length
% ep
->maxpacket
) &&
361 req
->packet_size
&& req
->req
.zero
;
363 dev_dbg(udc
->isp
->dev
,
364 "TX IRQ: req %p actual/length %u/%u maxpacket %u packet size %u zero %u need zlp %u\n",
365 req
, req
->req
.actual
, req
->req
.length
, ep
->maxpacket
,
366 req
->packet_size
, req
->req
.zero
, need_zlp
);
369 * Complete the request if all data has been sent and we don't need to
370 * transmit a zero length packet.
372 if (req
->req
.actual
== req
->req
.length
&& !need_zlp
) {
374 list_del(&req
->queue
);
377 isp1760_udc_ctrl_send_status(ep
, USB_DIR_IN
);
379 if (!list_empty(&ep
->queue
))
380 req
= list_first_entry(&ep
->queue
,
381 struct isp1760_request
, queue
);
387 * Transmit the next packet or start the next request, if any.
389 * TODO: If the endpoint is stalled the next request shouldn't be
390 * started, but what about the next packet ?
393 isp1760_udc_transmit(ep
, req
);
395 spin_unlock(&udc
->lock
);
398 isp1760_udc_request_complete(ep
, complete
, 0);
401 static int __isp1760_udc_set_halt(struct isp1760_ep
*ep
, bool halt
)
403 struct isp1760_udc
*udc
= ep
->udc
;
405 dev_dbg(udc
->isp
->dev
, "%s: %s halt on ep%02x\n", __func__
,
406 halt
? "set" : "clear", ep
->addr
);
408 if (ep
->desc
&& usb_endpoint_xfer_isoc(ep
->desc
)) {
409 dev_dbg(udc
->isp
->dev
, "%s: ep%02x is isochronous\n", __func__
,
414 isp1760_udc_select_ep(ep
);
415 isp1760_udc_write(udc
, DC_CTRLFUNC
, halt
? DC_STALL
: 0);
418 /* When halting the control endpoint, stall both IN and OUT. */
419 __isp1760_udc_select_ep(ep
, USB_DIR_IN
);
420 isp1760_udc_write(udc
, DC_CTRLFUNC
, halt
? DC_STALL
: 0);
422 /* Reset the data PID by cycling the endpoint enable bit. */
423 u16 eptype
= isp1760_udc_read(udc
, DC_EPTYPE
);
425 isp1760_udc_write(udc
, DC_EPTYPE
, eptype
& ~DC_EPENABLE
);
426 isp1760_udc_write(udc
, DC_EPTYPE
, eptype
);
429 * Disabling the endpoint emptied the transmit FIFO, fill it
430 * again if a request is pending.
432 * TODO: Does the gadget framework require synchronizatino with
433 * the TX IRQ handler ?
435 if ((ep
->addr
& USB_DIR_IN
) && !list_empty(&ep
->queue
)) {
436 struct isp1760_request
*req
;
438 req
= list_first_entry(&ep
->queue
,
439 struct isp1760_request
, queue
);
440 isp1760_udc_transmit(ep
, req
);
449 /* -----------------------------------------------------------------------------
453 static int isp1760_udc_get_status(struct isp1760_udc
*udc
,
454 const struct usb_ctrlrequest
*req
)
456 struct isp1760_ep
*ep
;
459 if (req
->wLength
!= cpu_to_le16(2) || req
->wValue
!= cpu_to_le16(0))
462 switch (req
->bRequestType
) {
463 case USB_DIR_IN
| USB_RECIP_DEVICE
:
464 status
= udc
->devstatus
;
467 case USB_DIR_IN
| USB_RECIP_INTERFACE
:
471 case USB_DIR_IN
| USB_RECIP_ENDPOINT
:
472 ep
= isp1760_udc_find_ep(udc
, le16_to_cpu(req
->wIndex
));
478 status
|= 1 << USB_ENDPOINT_HALT
;
485 isp1760_udc_write(udc
, DC_EPINDEX
, DC_ENDPIDX(0) | DC_EPDIR
);
486 isp1760_udc_write(udc
, DC_BUFLEN
, 2);
488 writew(cpu_to_le16(status
), udc
->regs
+ DC_DATAPORT
);
490 isp1760_udc_write(udc
, DC_CTRLFUNC
, DC_DSEN
);
492 dev_dbg(udc
->isp
->dev
, "%s: status 0x%04x\n", __func__
, status
);
497 static int isp1760_udc_set_address(struct isp1760_udc
*udc
, u16 addr
)
500 dev_dbg(udc
->isp
->dev
, "invalid device address %u\n", addr
);
504 if (udc
->gadget
.state
!= USB_STATE_DEFAULT
&&
505 udc
->gadget
.state
!= USB_STATE_ADDRESS
) {
506 dev_dbg(udc
->isp
->dev
, "can't set address in state %u\n",
511 usb_gadget_set_state(&udc
->gadget
, addr
? USB_STATE_ADDRESS
:
514 isp1760_udc_write(udc
, DC_ADDRESS
, DC_DEVEN
| addr
);
516 spin_lock(&udc
->lock
);
517 isp1760_udc_ctrl_send_status(&udc
->ep
[0], USB_DIR_OUT
);
518 spin_unlock(&udc
->lock
);
523 static bool isp1760_ep0_setup_standard(struct isp1760_udc
*udc
,
524 struct usb_ctrlrequest
*req
)
528 switch (req
->bRequest
) {
529 case USB_REQ_GET_STATUS
:
530 return isp1760_udc_get_status(udc
, req
);
532 case USB_REQ_CLEAR_FEATURE
:
533 switch (req
->bRequestType
) {
534 case USB_DIR_OUT
| USB_RECIP_DEVICE
: {
535 /* TODO: Handle remote wakeup feature. */
539 case USB_DIR_OUT
| USB_RECIP_ENDPOINT
: {
540 u16 index
= le16_to_cpu(req
->wIndex
);
541 struct isp1760_ep
*ep
;
543 if (req
->wLength
!= cpu_to_le16(0) ||
544 req
->wValue
!= cpu_to_le16(USB_ENDPOINT_HALT
))
547 ep
= isp1760_udc_find_ep(udc
, index
);
551 spin_lock(&udc
->lock
);
554 * If the endpoint is wedged only the gadget can clear
555 * the halt feature. Pretend success in that case, but
556 * keep the endpoint halted.
559 stall
= __isp1760_udc_set_halt(ep
, false);
564 isp1760_udc_ctrl_send_status(&udc
->ep
[0],
567 spin_unlock(&udc
->lock
);
576 case USB_REQ_SET_FEATURE
:
577 switch (req
->bRequestType
) {
578 case USB_DIR_OUT
| USB_RECIP_DEVICE
: {
579 /* TODO: Handle remote wakeup and test mode features */
583 case USB_DIR_OUT
| USB_RECIP_ENDPOINT
: {
584 u16 index
= le16_to_cpu(req
->wIndex
);
585 struct isp1760_ep
*ep
;
587 if (req
->wLength
!= cpu_to_le16(0) ||
588 req
->wValue
!= cpu_to_le16(USB_ENDPOINT_HALT
))
591 ep
= isp1760_udc_find_ep(udc
, index
);
595 spin_lock(&udc
->lock
);
597 stall
= __isp1760_udc_set_halt(ep
, true);
599 isp1760_udc_ctrl_send_status(&udc
->ep
[0],
602 spin_unlock(&udc
->lock
);
611 case USB_REQ_SET_ADDRESS
:
612 if (req
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_DEVICE
))
615 return isp1760_udc_set_address(udc
, le16_to_cpu(req
->wValue
));
617 case USB_REQ_SET_CONFIGURATION
:
618 if (req
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_DEVICE
))
621 if (udc
->gadget
.state
!= USB_STATE_ADDRESS
&&
622 udc
->gadget
.state
!= USB_STATE_CONFIGURED
)
625 stall
= udc
->driver
->setup(&udc
->gadget
, req
) < 0;
629 usb_gadget_set_state(&udc
->gadget
, req
->wValue
?
630 USB_STATE_CONFIGURED
: USB_STATE_ADDRESS
);
633 * SET_CONFIGURATION (and SET_INTERFACE) must reset the halt
634 * feature on all endpoints. There is however no need to do so
635 * explicitly here as the gadget driver will disable and
636 * reenable endpoints, clearing the halt feature.
641 return udc
->driver
->setup(&udc
->gadget
, req
) < 0;
645 static void isp1760_ep0_setup(struct isp1760_udc
*udc
)
648 struct usb_ctrlrequest r
;
654 spin_lock(&udc
->lock
);
656 isp1760_udc_write(udc
, DC_EPINDEX
, DC_EP0SETUP
);
658 count
= isp1760_udc_read(udc
, DC_BUFLEN
) & DC_DATACOUNT_MASK
;
659 if (count
!= sizeof(req
)) {
660 spin_unlock(&udc
->lock
);
662 dev_err(udc
->isp
->dev
, "invalid length %u for setup packet\n",
665 isp1760_udc_ctrl_send_stall(&udc
->ep
[0]);
669 req
.data
[0] = isp1760_udc_read(udc
, DC_DATAPORT
);
670 req
.data
[1] = isp1760_udc_read(udc
, DC_DATAPORT
);
672 if (udc
->ep0_state
!= ISP1760_CTRL_SETUP
) {
673 spin_unlock(&udc
->lock
);
674 dev_dbg(udc
->isp
->dev
, "unexpected SETUP packet\n");
678 /* Move to the data stage. */
680 udc
->ep0_state
= ISP1760_CTRL_STATUS
;
681 else if (req
.r
.bRequestType
& USB_DIR_IN
)
682 udc
->ep0_state
= ISP1760_CTRL_DATA_IN
;
684 udc
->ep0_state
= ISP1760_CTRL_DATA_OUT
;
686 udc
->ep0_dir
= req
.r
.bRequestType
& USB_DIR_IN
;
687 udc
->ep0_length
= le16_to_cpu(req
.r
.wLength
);
689 spin_unlock(&udc
->lock
);
691 dev_dbg(udc
->isp
->dev
,
692 "%s: bRequestType 0x%02x bRequest 0x%02x wValue 0x%04x wIndex 0x%04x wLength 0x%04x\n",
693 __func__
, req
.r
.bRequestType
, req
.r
.bRequest
,
694 le16_to_cpu(req
.r
.wValue
), le16_to_cpu(req
.r
.wIndex
),
695 le16_to_cpu(req
.r
.wLength
));
697 if ((req
.r
.bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
)
698 stall
= isp1760_ep0_setup_standard(udc
, &req
.r
);
700 stall
= udc
->driver
->setup(&udc
->gadget
, &req
.r
) < 0;
703 isp1760_udc_ctrl_send_stall(&udc
->ep
[0]);
706 /* -----------------------------------------------------------------------------
707 * Gadget Endpoint Operations
710 static int isp1760_ep_enable(struct usb_ep
*ep
,
711 const struct usb_endpoint_descriptor
*desc
)
713 struct isp1760_ep
*uep
= ep_to_udc_ep(ep
);
714 struct isp1760_udc
*udc
= uep
->udc
;
718 dev_dbg(uep
->udc
->isp
->dev
, "%s\n", __func__
);
721 * Validate the descriptor. The control endpoint can't be enabled
724 if (desc
->bDescriptorType
!= USB_DT_ENDPOINT
||
725 desc
->bEndpointAddress
== 0 ||
726 desc
->bEndpointAddress
!= uep
->addr
||
727 le16_to_cpu(desc
->wMaxPacketSize
) > ep
->maxpacket
) {
728 dev_dbg(udc
->isp
->dev
,
729 "%s: invalid descriptor type %u addr %02x ep addr %02x max packet size %u/%u\n",
730 __func__
, desc
->bDescriptorType
,
731 desc
->bEndpointAddress
, uep
->addr
,
732 le16_to_cpu(desc
->wMaxPacketSize
), ep
->maxpacket
);
736 switch (usb_endpoint_type(desc
)) {
737 case USB_ENDPOINT_XFER_ISOC
:
738 type
= DC_ENDPTYP_ISOC
;
740 case USB_ENDPOINT_XFER_BULK
:
741 type
= DC_ENDPTYP_BULK
;
743 case USB_ENDPOINT_XFER_INT
:
744 type
= DC_ENDPTYP_INTERRUPT
;
746 case USB_ENDPOINT_XFER_CONTROL
:
748 dev_dbg(udc
->isp
->dev
, "%s: control endpoints unsupported\n",
753 spin_lock_irqsave(&udc
->lock
, flags
);
756 uep
->maxpacket
= le16_to_cpu(desc
->wMaxPacketSize
);
757 uep
->rx_pending
= false;
761 isp1760_udc_select_ep(uep
);
762 isp1760_udc_write(udc
, DC_EPMAXPKTSZ
, uep
->maxpacket
);
763 isp1760_udc_write(udc
, DC_BUFLEN
, uep
->maxpacket
);
764 isp1760_udc_write(udc
, DC_EPTYPE
, DC_EPENABLE
| type
);
766 spin_unlock_irqrestore(&udc
->lock
, flags
);
771 static int isp1760_ep_disable(struct usb_ep
*ep
)
773 struct isp1760_ep
*uep
= ep_to_udc_ep(ep
);
774 struct isp1760_udc
*udc
= uep
->udc
;
775 struct isp1760_request
*req
, *nreq
;
779 dev_dbg(udc
->isp
->dev
, "%s\n", __func__
);
781 spin_lock_irqsave(&udc
->lock
, flags
);
784 dev_dbg(udc
->isp
->dev
, "%s: endpoint not enabled\n", __func__
);
785 spin_unlock_irqrestore(&udc
->lock
, flags
);
792 isp1760_udc_select_ep(uep
);
793 isp1760_udc_write(udc
, DC_EPTYPE
, 0);
795 /* TODO Synchronize with the IRQ handler */
797 list_splice_init(&uep
->queue
, &req_list
);
799 spin_unlock_irqrestore(&udc
->lock
, flags
);
801 list_for_each_entry_safe(req
, nreq
, &req_list
, queue
) {
802 list_del(&req
->queue
);
803 isp1760_udc_request_complete(uep
, req
, -ESHUTDOWN
);
809 static struct usb_request
*isp1760_ep_alloc_request(struct usb_ep
*ep
,
812 struct isp1760_request
*req
;
814 req
= kzalloc(sizeof(*req
), gfp_flags
);
821 static void isp1760_ep_free_request(struct usb_ep
*ep
, struct usb_request
*_req
)
823 struct isp1760_request
*req
= req_to_udc_req(_req
);
828 static int isp1760_ep_queue(struct usb_ep
*ep
, struct usb_request
*_req
,
831 struct isp1760_request
*req
= req_to_udc_req(_req
);
832 struct isp1760_ep
*uep
= ep_to_udc_ep(ep
);
833 struct isp1760_udc
*udc
= uep
->udc
;
834 bool complete
= false;
838 _req
->status
= -EINPROGRESS
;
841 spin_lock_irqsave(&udc
->lock
, flags
);
843 dev_dbg(udc
->isp
->dev
,
844 "%s: req %p (%u bytes%s) ep %p(0x%02x)\n", __func__
, _req
,
845 _req
->length
, _req
->zero
? " (zlp)" : "", uep
, uep
->addr
);
849 if (uep
->addr
== 0) {
850 if (_req
->length
!= udc
->ep0_length
&&
851 udc
->ep0_state
!= ISP1760_CTRL_DATA_IN
) {
852 dev_dbg(udc
->isp
->dev
,
853 "%s: invalid length %u for req %p\n",
854 __func__
, _req
->length
, req
);
859 switch (udc
->ep0_state
) {
860 case ISP1760_CTRL_DATA_IN
:
861 dev_dbg(udc
->isp
->dev
, "%s: transmitting req %p\n",
864 list_add_tail(&req
->queue
, &uep
->queue
);
865 isp1760_udc_transmit(uep
, req
);
868 case ISP1760_CTRL_DATA_OUT
:
869 list_add_tail(&req
->queue
, &uep
->queue
);
870 __isp1760_udc_select_ep(uep
, USB_DIR_OUT
);
871 isp1760_udc_write(udc
, DC_CTRLFUNC
, DC_DSEN
);
874 case ISP1760_CTRL_STATUS
:
879 dev_dbg(udc
->isp
->dev
, "%s: invalid ep0 state\n",
884 } else if (uep
->desc
) {
885 bool empty
= list_empty(&uep
->queue
);
887 list_add_tail(&req
->queue
, &uep
->queue
);
888 if ((uep
->addr
& USB_DIR_IN
) && !uep
->halted
&& empty
)
889 isp1760_udc_transmit(uep
, req
);
890 else if (!(uep
->addr
& USB_DIR_IN
) && uep
->rx_pending
)
891 complete
= isp1760_udc_receive(uep
, req
);
893 dev_dbg(udc
->isp
->dev
,
894 "%s: can't queue request to disabled ep%02x\n",
895 __func__
, uep
->addr
);
903 spin_unlock_irqrestore(&udc
->lock
, flags
);
906 isp1760_udc_request_complete(uep
, req
, 0);
911 static int isp1760_ep_dequeue(struct usb_ep
*ep
, struct usb_request
*_req
)
913 struct isp1760_request
*req
= req_to_udc_req(_req
);
914 struct isp1760_ep
*uep
= ep_to_udc_ep(ep
);
915 struct isp1760_udc
*udc
= uep
->udc
;
918 dev_dbg(uep
->udc
->isp
->dev
, "%s(ep%02x)\n", __func__
, uep
->addr
);
920 spin_lock_irqsave(&udc
->lock
, flags
);
925 list_del(&req
->queue
);
927 spin_unlock_irqrestore(&udc
->lock
, flags
);
932 isp1760_udc_request_complete(uep
, req
, -ECONNRESET
);
936 static int __isp1760_ep_set_halt(struct isp1760_ep
*uep
, bool stall
, bool wedge
)
938 struct isp1760_udc
*udc
= uep
->udc
;
943 * Halting the control endpoint is only valid as a delayed error
944 * response to a SETUP packet. Make sure EP0 is in the right
945 * stage and that the gadget isn't trying to clear the halt
948 if (WARN_ON(udc
->ep0_state
== ISP1760_CTRL_SETUP
|| !stall
||
954 if (uep
->addr
&& !uep
->desc
) {
955 dev_dbg(udc
->isp
->dev
, "%s: ep%02x is disabled\n", __func__
,
960 if (uep
->addr
& USB_DIR_IN
) {
961 /* Refuse to halt IN endpoints with active transfers. */
962 if (!list_empty(&uep
->queue
)) {
963 dev_dbg(udc
->isp
->dev
,
964 "%s: ep%02x has request pending\n", __func__
,
970 ret
= __isp1760_udc_set_halt(uep
, stall
);
976 * Stalling EP0 completes the control transaction, move back to
979 udc
->ep0_state
= ISP1760_CTRL_SETUP
;
991 static int isp1760_ep_set_halt(struct usb_ep
*ep
, int value
)
993 struct isp1760_ep
*uep
= ep_to_udc_ep(ep
);
997 dev_dbg(uep
->udc
->isp
->dev
, "%s: %s halt on ep%02x\n", __func__
,
998 value
? "set" : "clear", uep
->addr
);
1000 spin_lock_irqsave(&uep
->udc
->lock
, flags
);
1001 ret
= __isp1760_ep_set_halt(uep
, value
, false);
1002 spin_unlock_irqrestore(&uep
->udc
->lock
, flags
);
1007 static int isp1760_ep_set_wedge(struct usb_ep
*ep
)
1009 struct isp1760_ep
*uep
= ep_to_udc_ep(ep
);
1010 unsigned long flags
;
1013 dev_dbg(uep
->udc
->isp
->dev
, "%s: set wedge on ep%02x)\n", __func__
,
1016 spin_lock_irqsave(&uep
->udc
->lock
, flags
);
1017 ret
= __isp1760_ep_set_halt(uep
, true, true);
1018 spin_unlock_irqrestore(&uep
->udc
->lock
, flags
);
1023 static void isp1760_ep_fifo_flush(struct usb_ep
*ep
)
1025 struct isp1760_ep
*uep
= ep_to_udc_ep(ep
);
1026 struct isp1760_udc
*udc
= uep
->udc
;
1027 unsigned long flags
;
1029 spin_lock_irqsave(&udc
->lock
, flags
);
1031 isp1760_udc_select_ep(uep
);
1034 * Set the CLBUF bit twice to flush both buffers in case double
1035 * buffering is enabled.
1037 isp1760_udc_write(udc
, DC_CTRLFUNC
, DC_CLBUF
);
1038 isp1760_udc_write(udc
, DC_CTRLFUNC
, DC_CLBUF
);
1040 spin_unlock_irqrestore(&udc
->lock
, flags
);
1043 static const struct usb_ep_ops isp1760_ep_ops
= {
1044 .enable
= isp1760_ep_enable
,
1045 .disable
= isp1760_ep_disable
,
1046 .alloc_request
= isp1760_ep_alloc_request
,
1047 .free_request
= isp1760_ep_free_request
,
1048 .queue
= isp1760_ep_queue
,
1049 .dequeue
= isp1760_ep_dequeue
,
1050 .set_halt
= isp1760_ep_set_halt
,
1051 .set_wedge
= isp1760_ep_set_wedge
,
1052 .fifo_flush
= isp1760_ep_fifo_flush
,
1055 /* -----------------------------------------------------------------------------
1059 /* Called with the UDC spinlock held. */
1060 static void isp1760_udc_connect(struct isp1760_udc
*udc
)
1062 usb_gadget_set_state(&udc
->gadget
, USB_STATE_POWERED
);
1063 mod_timer(&udc
->vbus_timer
, jiffies
+ ISP1760_VBUS_POLL_INTERVAL
);
1066 /* Called with the UDC spinlock held. */
1067 static void isp1760_udc_disconnect(struct isp1760_udc
*udc
)
1069 if (udc
->gadget
.state
< USB_STATE_POWERED
)
1072 dev_dbg(udc
->isp
->dev
, "Device disconnected in state %u\n",
1075 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1076 usb_gadget_set_state(&udc
->gadget
, USB_STATE_ATTACHED
);
1078 if (udc
->driver
->disconnect
)
1079 udc
->driver
->disconnect(&udc
->gadget
);
1081 del_timer(&udc
->vbus_timer
);
1083 /* TODO Reset all endpoints ? */
1086 static void isp1760_udc_init_hw(struct isp1760_udc
*udc
)
1089 * The device controller currently shares its interrupt with the host
1090 * controller, the DC_IRQ polarity and signaling mode are ignored. Set
1091 * the to active-low level-triggered.
1093 * Configure the control, in and out pipes to generate interrupts on
1094 * ACK tokens only (and NYET for the out pipe). The default
1095 * configuration also generates an interrupt on the first NACK token.
1097 isp1760_udc_write(udc
, DC_INTCONF
, DC_CDBGMOD_ACK
| DC_DDBGMODIN_ACK
|
1098 DC_DDBGMODOUT_ACK_NYET
);
1100 isp1760_udc_write(udc
, DC_INTENABLE
, DC_IEPRXTX(7) | DC_IEPRXTX(6) |
1101 DC_IEPRXTX(5) | DC_IEPRXTX(4) | DC_IEPRXTX(3) |
1102 DC_IEPRXTX(2) | DC_IEPRXTX(1) | DC_IEPRXTX(0) |
1103 DC_IEP0SETUP
| DC_IEVBUS
| DC_IERESM
| DC_IESUSP
|
1104 DC_IEHS_STA
| DC_IEBRST
);
1107 isp1760_set_pullup(udc
->isp
, true);
1109 isp1760_udc_write(udc
, DC_ADDRESS
, DC_DEVEN
);
1112 static void isp1760_udc_reset(struct isp1760_udc
*udc
)
1114 unsigned long flags
;
1116 spin_lock_irqsave(&udc
->lock
, flags
);
1119 * The bus reset has reset most registers to their default value,
1120 * reinitialize the UDC hardware.
1122 isp1760_udc_init_hw(udc
);
1124 udc
->ep0_state
= ISP1760_CTRL_SETUP
;
1125 udc
->gadget
.speed
= USB_SPEED_FULL
;
1127 usb_gadget_udc_reset(&udc
->gadget
, udc
->driver
);
1129 spin_unlock_irqrestore(&udc
->lock
, flags
);
1132 static void isp1760_udc_suspend(struct isp1760_udc
*udc
)
1134 if (udc
->gadget
.state
< USB_STATE_DEFAULT
)
1137 if (udc
->driver
->suspend
)
1138 udc
->driver
->suspend(&udc
->gadget
);
1141 static void isp1760_udc_resume(struct isp1760_udc
*udc
)
1143 if (udc
->gadget
.state
< USB_STATE_DEFAULT
)
1146 if (udc
->driver
->resume
)
1147 udc
->driver
->resume(&udc
->gadget
);
1150 /* -----------------------------------------------------------------------------
1154 static int isp1760_udc_get_frame(struct usb_gadget
*gadget
)
1156 struct isp1760_udc
*udc
= gadget_to_udc(gadget
);
1158 return isp1760_udc_read(udc
, DC_FRAMENUM
) & ((1 << 11) - 1);
1161 static int isp1760_udc_wakeup(struct usb_gadget
*gadget
)
1163 struct isp1760_udc
*udc
= gadget_to_udc(gadget
);
1165 dev_dbg(udc
->isp
->dev
, "%s\n", __func__
);
1169 static int isp1760_udc_set_selfpowered(struct usb_gadget
*gadget
,
1172 struct isp1760_udc
*udc
= gadget_to_udc(gadget
);
1175 udc
->devstatus
|= 1 << USB_DEVICE_SELF_POWERED
;
1177 udc
->devstatus
&= ~(1 << USB_DEVICE_SELF_POWERED
);
1182 static int isp1760_udc_pullup(struct usb_gadget
*gadget
, int is_on
)
1184 struct isp1760_udc
*udc
= gadget_to_udc(gadget
);
1186 isp1760_set_pullup(udc
->isp
, is_on
);
1187 udc
->connected
= is_on
;
1192 static int isp1760_udc_start(struct usb_gadget
*gadget
,
1193 struct usb_gadget_driver
*driver
)
1195 struct isp1760_udc
*udc
= gadget_to_udc(gadget
);
1196 unsigned long flags
;
1198 /* The hardware doesn't support low speed. */
1199 if (driver
->max_speed
< USB_SPEED_FULL
) {
1200 dev_err(udc
->isp
->dev
, "Invalid gadget driver\n");
1204 spin_lock_irqsave(&udc
->lock
, flags
);
1207 dev_err(udc
->isp
->dev
, "UDC already has a gadget driver\n");
1208 spin_unlock_irqrestore(&udc
->lock
, flags
);
1212 udc
->driver
= driver
;
1214 spin_unlock_irqrestore(&udc
->lock
, flags
);
1216 dev_dbg(udc
->isp
->dev
, "starting UDC with driver %s\n",
1220 udc
->connected
= true;
1222 usb_gadget_set_state(&udc
->gadget
, USB_STATE_ATTACHED
);
1224 /* DMA isn't supported yet, don't enable the DMA clock. */
1225 isp1760_udc_write(udc
, DC_MODE
, DC_GLINTENA
);
1227 isp1760_udc_init_hw(udc
);
1229 dev_dbg(udc
->isp
->dev
, "UDC started with driver %s\n",
1235 static int isp1760_udc_stop(struct usb_gadget
*gadget
)
1237 struct isp1760_udc
*udc
= gadget_to_udc(gadget
);
1238 unsigned long flags
;
1240 dev_dbg(udc
->isp
->dev
, "%s\n", __func__
);
1242 del_timer_sync(&udc
->vbus_timer
);
1244 isp1760_udc_write(udc
, DC_MODE
, 0);
1246 spin_lock_irqsave(&udc
->lock
, flags
);
1248 spin_unlock_irqrestore(&udc
->lock
, flags
);
1253 static struct usb_gadget_ops isp1760_udc_ops
= {
1254 .get_frame
= isp1760_udc_get_frame
,
1255 .wakeup
= isp1760_udc_wakeup
,
1256 .set_selfpowered
= isp1760_udc_set_selfpowered
,
1257 .pullup
= isp1760_udc_pullup
,
1258 .udc_start
= isp1760_udc_start
,
1259 .udc_stop
= isp1760_udc_stop
,
1262 /* -----------------------------------------------------------------------------
1263 * Interrupt Handling
1266 static irqreturn_t
isp1760_udc_irq(int irq
, void *dev
)
1268 struct isp1760_udc
*udc
= dev
;
1272 status
= isp1760_udc_read(udc
, DC_INTERRUPT
)
1273 & isp1760_udc_read(udc
, DC_INTENABLE
);
1274 isp1760_udc_write(udc
, DC_INTERRUPT
, status
);
1276 if (status
& DC_IEVBUS
) {
1277 dev_dbg(udc
->isp
->dev
, "%s(VBUS)\n", __func__
);
1278 /* The VBUS interrupt is only triggered when VBUS appears. */
1279 spin_lock(&udc
->lock
);
1280 isp1760_udc_connect(udc
);
1281 spin_unlock(&udc
->lock
);
1284 if (status
& DC_IEBRST
) {
1285 dev_dbg(udc
->isp
->dev
, "%s(BRST)\n", __func__
);
1287 isp1760_udc_reset(udc
);
1290 for (i
= 0; i
<= 7; ++i
) {
1291 struct isp1760_ep
*ep
= &udc
->ep
[i
*2];
1293 if (status
& DC_IEPTX(i
)) {
1294 dev_dbg(udc
->isp
->dev
, "%s(EPTX%u)\n", __func__
, i
);
1295 isp1760_ep_tx_complete(ep
);
1298 if (status
& DC_IEPRX(i
)) {
1299 dev_dbg(udc
->isp
->dev
, "%s(EPRX%u)\n", __func__
, i
);
1300 isp1760_ep_rx_ready(i
? ep
- 1 : ep
);
1304 if (status
& DC_IEP0SETUP
) {
1305 dev_dbg(udc
->isp
->dev
, "%s(EP0SETUP)\n", __func__
);
1307 isp1760_ep0_setup(udc
);
1310 if (status
& DC_IERESM
) {
1311 dev_dbg(udc
->isp
->dev
, "%s(RESM)\n", __func__
);
1312 isp1760_udc_resume(udc
);
1315 if (status
& DC_IESUSP
) {
1316 dev_dbg(udc
->isp
->dev
, "%s(SUSP)\n", __func__
);
1318 spin_lock(&udc
->lock
);
1319 if (!(isp1760_udc_read(udc
, DC_MODE
) & DC_VBUSSTAT
))
1320 isp1760_udc_disconnect(udc
);
1322 isp1760_udc_suspend(udc
);
1323 spin_unlock(&udc
->lock
);
1326 if (status
& DC_IEHS_STA
) {
1327 dev_dbg(udc
->isp
->dev
, "%s(HS_STA)\n", __func__
);
1328 udc
->gadget
.speed
= USB_SPEED_HIGH
;
1331 return status
? IRQ_HANDLED
: IRQ_NONE
;
1334 static void isp1760_udc_vbus_poll(unsigned long data
)
1336 struct isp1760_udc
*udc
= (struct isp1760_udc
*)data
;
1337 unsigned long flags
;
1339 spin_lock_irqsave(&udc
->lock
, flags
);
1341 if (!(isp1760_udc_read(udc
, DC_MODE
) & DC_VBUSSTAT
))
1342 isp1760_udc_disconnect(udc
);
1343 else if (udc
->gadget
.state
>= USB_STATE_POWERED
)
1344 mod_timer(&udc
->vbus_timer
,
1345 jiffies
+ ISP1760_VBUS_POLL_INTERVAL
);
1347 spin_unlock_irqrestore(&udc
->lock
, flags
);
1350 /* -----------------------------------------------------------------------------
1354 static void isp1760_udc_init_eps(struct isp1760_udc
*udc
)
1358 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
1360 for (i
= 0; i
< ARRAY_SIZE(udc
->ep
); ++i
) {
1361 struct isp1760_ep
*ep
= &udc
->ep
[i
];
1362 unsigned int ep_num
= (i
+ 1) / 2;
1363 bool is_in
= !(i
& 1);
1367 INIT_LIST_HEAD(&ep
->queue
);
1369 ep
->addr
= (ep_num
&& is_in
? USB_DIR_IN
: USB_DIR_OUT
)
1373 sprintf(ep
->name
, "ep%u%s", ep_num
,
1374 ep_num
? (is_in
? "in" : "out") : "");
1376 ep
->ep
.ops
= &isp1760_ep_ops
;
1377 ep
->ep
.name
= ep
->name
;
1380 * Hardcode the maximum packet sizes for now, to 64 bytes for
1381 * the control endpoint and 512 bytes for all other endpoints.
1382 * This fits in the 8kB FIFO without double-buffering.
1385 usb_ep_set_maxpacket_limit(&ep
->ep
, 64);
1386 ep
->ep
.caps
.type_control
= true;
1387 ep
->ep
.caps
.dir_in
= true;
1388 ep
->ep
.caps
.dir_out
= true;
1390 udc
->gadget
.ep0
= &ep
->ep
;
1392 usb_ep_set_maxpacket_limit(&ep
->ep
, 512);
1393 ep
->ep
.caps
.type_iso
= true;
1394 ep
->ep
.caps
.type_bulk
= true;
1395 ep
->ep
.caps
.type_int
= true;
1397 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
1401 ep
->ep
.caps
.dir_in
= true;
1403 ep
->ep
.caps
.dir_out
= true;
1407 static int isp1760_udc_init(struct isp1760_udc
*udc
)
1413 * Check that the controller is present by writing to the scratch
1414 * register, modifying the bus pattern by reading from the chip ID
1415 * register, and reading the scratch register value back. The chip ID
1416 * and scratch register contents must match the expected values.
1418 isp1760_udc_write(udc
, DC_SCRATCH
, 0xbabe);
1419 chipid
= isp1760_udc_read(udc
, DC_CHIPID
);
1420 scratch
= isp1760_udc_read(udc
, DC_SCRATCH
);
1422 if (scratch
!= 0xbabe) {
1423 dev_err(udc
->isp
->dev
,
1424 "udc: scratch test failed (0x%04x/0x%08x)\n",
1429 if (chipid
!= 0x00011582 && chipid
!= 0x00158210) {
1430 dev_err(udc
->isp
->dev
, "udc: invalid chip ID 0x%08x\n", chipid
);
1434 /* Reset the device controller. */
1435 isp1760_udc_write(udc
, DC_MODE
, DC_SFRESET
);
1436 usleep_range(10000, 11000);
1437 isp1760_udc_write(udc
, DC_MODE
, 0);
1438 usleep_range(10000, 11000);
1443 int isp1760_udc_register(struct isp1760_device
*isp
, int irq
,
1444 unsigned long irqflags
)
1446 struct isp1760_udc
*udc
= &isp
->udc
;
1447 const char *devname
;
1452 udc
->regs
= isp
->regs
;
1454 spin_lock_init(&udc
->lock
);
1455 setup_timer(&udc
->vbus_timer
, isp1760_udc_vbus_poll
,
1456 (unsigned long)udc
);
1458 ret
= isp1760_udc_init(udc
);
1462 devname
= dev_name(isp
->dev
);
1463 udc
->irqname
= kmalloc(strlen(devname
) + 7, GFP_KERNEL
);
1467 sprintf(udc
->irqname
, "%s (udc)", devname
);
1469 ret
= request_irq(irq
, isp1760_udc_irq
, IRQF_SHARED
| irqflags
,
1477 * Initialize the gadget static fields and register its device. Gadget
1478 * fields that vary during the life time of the gadget are initialized
1481 udc
->gadget
.ops
= &isp1760_udc_ops
;
1482 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1483 udc
->gadget
.max_speed
= USB_SPEED_HIGH
;
1484 udc
->gadget
.name
= "isp1761_udc";
1486 isp1760_udc_init_eps(udc
);
1488 ret
= usb_add_gadget_udc(isp
->dev
, &udc
->gadget
);
1496 free_irq(udc
->irq
, udc
);
1497 kfree(udc
->irqname
);
1502 void isp1760_udc_unregister(struct isp1760_device
*isp
)
1504 struct isp1760_udc
*udc
= &isp
->udc
;
1509 usb_del_gadget_udc(&udc
->gadget
);
1511 free_irq(udc
->irq
, udc
);
1512 kfree(udc
->irqname
);