1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * linux/drivers/usb/gadget/pxa27x_udc.h
4*4882a593Smuzhiyun * Intel PXA27x on-chip full speed USB device controller
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Inspired by original driver by Frank Becker, David Brownell, and others.
7*4882a593Smuzhiyun * Copyright (C) 2008 Robert Jarzmik
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #ifndef __LINUX_USB_GADGET_PXA27X_H
11*4882a593Smuzhiyun #define __LINUX_USB_GADGET_PXA27X_H
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/types.h>
14*4882a593Smuzhiyun #include <linux/spinlock.h>
15*4882a593Smuzhiyun #include <linux/io.h>
16*4882a593Smuzhiyun #include <linux/usb/otg.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun * Register definitions
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun /* Offsets */
22*4882a593Smuzhiyun #define UDCCR 0x0000 /* UDC Control Register */
23*4882a593Smuzhiyun #define UDCICR0 0x0004 /* UDC Interrupt Control Register0 */
24*4882a593Smuzhiyun #define UDCICR1 0x0008 /* UDC Interrupt Control Register1 */
25*4882a593Smuzhiyun #define UDCISR0 0x000C /* UDC Interrupt Status Register 0 */
26*4882a593Smuzhiyun #define UDCISR1 0x0010 /* UDC Interrupt Status Register 1 */
27*4882a593Smuzhiyun #define UDCFNR 0x0014 /* UDC Frame Number Register */
28*4882a593Smuzhiyun #define UDCOTGICR 0x0018 /* UDC On-The-Go interrupt control */
29*4882a593Smuzhiyun #define UP2OCR 0x0020 /* USB Port 2 Output Control register */
30*4882a593Smuzhiyun #define UP3OCR 0x0024 /* USB Port 3 Output Control register */
31*4882a593Smuzhiyun #define UDCCSRn(x) (0x0100 + ((x)<<2)) /* UDC Control/Status register */
32*4882a593Smuzhiyun #define UDCBCRn(x) (0x0200 + ((x)<<2)) /* UDC Byte Count Register */
33*4882a593Smuzhiyun #define UDCDRn(x) (0x0300 + ((x)<<2)) /* UDC Data Register */
34*4882a593Smuzhiyun #define UDCCRn(x) (0x0400 + ((x)<<2)) /* UDC Control Register */
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #define UDCCR_OEN (1 << 31) /* On-the-Go Enable */
37*4882a593Smuzhiyun #define UDCCR_AALTHNP (1 << 30) /* A-device Alternate Host Negotiation
38*4882a593Smuzhiyun Protocol Port Support */
39*4882a593Smuzhiyun #define UDCCR_AHNP (1 << 29) /* A-device Host Negotiation Protocol
40*4882a593Smuzhiyun Support */
41*4882a593Smuzhiyun #define UDCCR_BHNP (1 << 28) /* B-device Host Negotiation Protocol
42*4882a593Smuzhiyun Enable */
43*4882a593Smuzhiyun #define UDCCR_DWRE (1 << 16) /* Device Remote Wake-up Enable */
44*4882a593Smuzhiyun #define UDCCR_ACN (0x03 << 11) /* Active UDC configuration Number */
45*4882a593Smuzhiyun #define UDCCR_ACN_S 11
46*4882a593Smuzhiyun #define UDCCR_AIN (0x07 << 8) /* Active UDC interface Number */
47*4882a593Smuzhiyun #define UDCCR_AIN_S 8
48*4882a593Smuzhiyun #define UDCCR_AAISN (0x07 << 5) /* Active UDC Alternate Interface
49*4882a593Smuzhiyun Setting Number */
50*4882a593Smuzhiyun #define UDCCR_AAISN_S 5
51*4882a593Smuzhiyun #define UDCCR_SMAC (1 << 4) /* Switch Endpoint Memory to Active
52*4882a593Smuzhiyun Configuration */
53*4882a593Smuzhiyun #define UDCCR_EMCE (1 << 3) /* Endpoint Memory Configuration
54*4882a593Smuzhiyun Error */
55*4882a593Smuzhiyun #define UDCCR_UDR (1 << 2) /* UDC Resume */
56*4882a593Smuzhiyun #define UDCCR_UDA (1 << 1) /* UDC Active */
57*4882a593Smuzhiyun #define UDCCR_UDE (1 << 0) /* UDC Enable */
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun #define UDCICR_INT(n, intr) (((intr) & 0x03) << (((n) & 0x0F) * 2))
60*4882a593Smuzhiyun #define UDCICR1_IECC (1 << 31) /* IntEn - Configuration Change */
61*4882a593Smuzhiyun #define UDCICR1_IESOF (1 << 30) /* IntEn - Start of Frame */
62*4882a593Smuzhiyun #define UDCICR1_IERU (1 << 29) /* IntEn - Resume */
63*4882a593Smuzhiyun #define UDCICR1_IESU (1 << 28) /* IntEn - Suspend */
64*4882a593Smuzhiyun #define UDCICR1_IERS (1 << 27) /* IntEn - Reset */
65*4882a593Smuzhiyun #define UDCICR_FIFOERR (1 << 1) /* FIFO Error interrupt for EP */
66*4882a593Smuzhiyun #define UDCICR_PKTCOMPL (1 << 0) /* Packet Complete interrupt for EP */
67*4882a593Smuzhiyun #define UDCICR_INT_MASK (UDCICR_FIFOERR | UDCICR_PKTCOMPL)
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define UDCISR_INT(n, intr) (((intr) & 0x03) << (((n) & 0x0F) * 2))
70*4882a593Smuzhiyun #define UDCISR1_IRCC (1 << 31) /* IntReq - Configuration Change */
71*4882a593Smuzhiyun #define UDCISR1_IRSOF (1 << 30) /* IntReq - Start of Frame */
72*4882a593Smuzhiyun #define UDCISR1_IRRU (1 << 29) /* IntReq - Resume */
73*4882a593Smuzhiyun #define UDCISR1_IRSU (1 << 28) /* IntReq - Suspend */
74*4882a593Smuzhiyun #define UDCISR1_IRRS (1 << 27) /* IntReq - Reset */
75*4882a593Smuzhiyun #define UDCISR_INT_MASK (UDCICR_FIFOERR | UDCICR_PKTCOMPL)
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun #define UDCOTGICR_IESF (1 << 24) /* OTG SET_FEATURE command recvd */
78*4882a593Smuzhiyun #define UDCOTGICR_IEXR (1 << 17) /* Extra Transceiver Interrupt
79*4882a593Smuzhiyun Rising Edge Interrupt Enable */
80*4882a593Smuzhiyun #define UDCOTGICR_IEXF (1 << 16) /* Extra Transceiver Interrupt
81*4882a593Smuzhiyun Falling Edge Interrupt Enable */
82*4882a593Smuzhiyun #define UDCOTGICR_IEVV40R (1 << 9) /* OTG Vbus Valid 4.0V Rising Edge
83*4882a593Smuzhiyun Interrupt Enable */
84*4882a593Smuzhiyun #define UDCOTGICR_IEVV40F (1 << 8) /* OTG Vbus Valid 4.0V Falling Edge
85*4882a593Smuzhiyun Interrupt Enable */
86*4882a593Smuzhiyun #define UDCOTGICR_IEVV44R (1 << 7) /* OTG Vbus Valid 4.4V Rising Edge
87*4882a593Smuzhiyun Interrupt Enable */
88*4882a593Smuzhiyun #define UDCOTGICR_IEVV44F (1 << 6) /* OTG Vbus Valid 4.4V Falling Edge
89*4882a593Smuzhiyun Interrupt Enable */
90*4882a593Smuzhiyun #define UDCOTGICR_IESVR (1 << 5) /* OTG Session Valid Rising Edge
91*4882a593Smuzhiyun Interrupt Enable */
92*4882a593Smuzhiyun #define UDCOTGICR_IESVF (1 << 4) /* OTG Session Valid Falling Edge
93*4882a593Smuzhiyun Interrupt Enable */
94*4882a593Smuzhiyun #define UDCOTGICR_IESDR (1 << 3) /* OTG A-Device SRP Detect Rising
95*4882a593Smuzhiyun Edge Interrupt Enable */
96*4882a593Smuzhiyun #define UDCOTGICR_IESDF (1 << 2) /* OTG A-Device SRP Detect Falling
97*4882a593Smuzhiyun Edge Interrupt Enable */
98*4882a593Smuzhiyun #define UDCOTGICR_IEIDR (1 << 1) /* OTG ID Change Rising Edge
99*4882a593Smuzhiyun Interrupt Enable */
100*4882a593Smuzhiyun #define UDCOTGICR_IEIDF (1 << 0) /* OTG ID Change Falling Edge
101*4882a593Smuzhiyun Interrupt Enable */
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /* Host Port 2 field bits */
104*4882a593Smuzhiyun #define UP2OCR_CPVEN (1 << 0) /* Charge Pump Vbus Enable */
105*4882a593Smuzhiyun #define UP2OCR_CPVPE (1 << 1) /* Charge Pump Vbus Pulse Enable */
106*4882a593Smuzhiyun /* Transceiver enablers */
107*4882a593Smuzhiyun #define UP2OCR_DPPDE (1 << 2) /* D+ Pull Down Enable */
108*4882a593Smuzhiyun #define UP2OCR_DMPDE (1 << 3) /* D- Pull Down Enable */
109*4882a593Smuzhiyun #define UP2OCR_DPPUE (1 << 4) /* D+ Pull Up Enable */
110*4882a593Smuzhiyun #define UP2OCR_DMPUE (1 << 5) /* D- Pull Up Enable */
111*4882a593Smuzhiyun #define UP2OCR_DPPUBE (1 << 6) /* D+ Pull Up Bypass Enable */
112*4882a593Smuzhiyun #define UP2OCR_DMPUBE (1 << 7) /* D- Pull Up Bypass Enable */
113*4882a593Smuzhiyun #define UP2OCR_EXSP (1 << 8) /* External Transceiver Speed Control */
114*4882a593Smuzhiyun #define UP2OCR_EXSUS (1 << 9) /* External Transceiver Speed Enable */
115*4882a593Smuzhiyun #define UP2OCR_IDON (1 << 10) /* OTG ID Read Enable */
116*4882a593Smuzhiyun #define UP2OCR_HXS (1 << 16) /* Transceiver Output Select */
117*4882a593Smuzhiyun #define UP2OCR_HXOE (1 << 17) /* Transceiver Output Enable */
118*4882a593Smuzhiyun #define UP2OCR_SEOS (1 << 24) /* Single-Ended Output Select */
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun #define UDCCSR0_ACM (1 << 9) /* Ack Control Mode */
121*4882a593Smuzhiyun #define UDCCSR0_AREN (1 << 8) /* Ack Response Enable */
122*4882a593Smuzhiyun #define UDCCSR0_SA (1 << 7) /* Setup Active */
123*4882a593Smuzhiyun #define UDCCSR0_RNE (1 << 6) /* Receive FIFO Not Empty */
124*4882a593Smuzhiyun #define UDCCSR0_FST (1 << 5) /* Force Stall */
125*4882a593Smuzhiyun #define UDCCSR0_SST (1 << 4) /* Sent Stall */
126*4882a593Smuzhiyun #define UDCCSR0_DME (1 << 3) /* DMA Enable */
127*4882a593Smuzhiyun #define UDCCSR0_FTF (1 << 2) /* Flush Transmit FIFO */
128*4882a593Smuzhiyun #define UDCCSR0_IPR (1 << 1) /* IN Packet Ready */
129*4882a593Smuzhiyun #define UDCCSR0_OPC (1 << 0) /* OUT Packet Complete */
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun #define UDCCSR_DPE (1 << 9) /* Data Packet Error */
132*4882a593Smuzhiyun #define UDCCSR_FEF (1 << 8) /* Flush Endpoint FIFO */
133*4882a593Smuzhiyun #define UDCCSR_SP (1 << 7) /* Short Packet Control/Status */
134*4882a593Smuzhiyun #define UDCCSR_BNE (1 << 6) /* Buffer Not Empty (IN endpoints) */
135*4882a593Smuzhiyun #define UDCCSR_BNF (1 << 6) /* Buffer Not Full (OUT endpoints) */
136*4882a593Smuzhiyun #define UDCCSR_FST (1 << 5) /* Force STALL */
137*4882a593Smuzhiyun #define UDCCSR_SST (1 << 4) /* Sent STALL */
138*4882a593Smuzhiyun #define UDCCSR_DME (1 << 3) /* DMA Enable */
139*4882a593Smuzhiyun #define UDCCSR_TRN (1 << 2) /* Tx/Rx NAK */
140*4882a593Smuzhiyun #define UDCCSR_PC (1 << 1) /* Packet Complete */
141*4882a593Smuzhiyun #define UDCCSR_FS (1 << 0) /* FIFO needs service */
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun #define UDCCONR_CN (0x03 << 25) /* Configuration Number */
144*4882a593Smuzhiyun #define UDCCONR_CN_S 25
145*4882a593Smuzhiyun #define UDCCONR_IN (0x07 << 22) /* Interface Number */
146*4882a593Smuzhiyun #define UDCCONR_IN_S 22
147*4882a593Smuzhiyun #define UDCCONR_AISN (0x07 << 19) /* Alternate Interface Number */
148*4882a593Smuzhiyun #define UDCCONR_AISN_S 19
149*4882a593Smuzhiyun #define UDCCONR_EN (0x0f << 15) /* Endpoint Number */
150*4882a593Smuzhiyun #define UDCCONR_EN_S 15
151*4882a593Smuzhiyun #define UDCCONR_ET (0x03 << 13) /* Endpoint Type: */
152*4882a593Smuzhiyun #define UDCCONR_ET_S 13
153*4882a593Smuzhiyun #define UDCCONR_ET_INT (0x03 << 13) /* Interrupt */
154*4882a593Smuzhiyun #define UDCCONR_ET_BULK (0x02 << 13) /* Bulk */
155*4882a593Smuzhiyun #define UDCCONR_ET_ISO (0x01 << 13) /* Isochronous */
156*4882a593Smuzhiyun #define UDCCONR_ET_NU (0x00 << 13) /* Not used */
157*4882a593Smuzhiyun #define UDCCONR_ED (1 << 12) /* Endpoint Direction */
158*4882a593Smuzhiyun #define UDCCONR_MPS (0x3ff << 2) /* Maximum Packet Size */
159*4882a593Smuzhiyun #define UDCCONR_MPS_S 2
160*4882a593Smuzhiyun #define UDCCONR_DE (1 << 1) /* Double Buffering Enable */
161*4882a593Smuzhiyun #define UDCCONR_EE (1 << 0) /* Endpoint Enable */
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun #define UDCCR_MASK_BITS (UDCCR_OEN | UDCCR_SMAC | UDCCR_UDR | UDCCR_UDE)
164*4882a593Smuzhiyun #define UDCCSR_WR_MASK (UDCCSR_DME | UDCCSR_FST)
165*4882a593Smuzhiyun #define UDC_FNR_MASK (0x7ff)
166*4882a593Smuzhiyun #define UDC_BCR_MASK (0x3ff)
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /*
169*4882a593Smuzhiyun * UDCCR = UDC Endpoint Configuration Registers
170*4882a593Smuzhiyun * UDCCSR = UDC Control/Status Register for this EP
171*4882a593Smuzhiyun * UDCBCR = UDC Byte Count Remaining (contents of OUT fifo)
172*4882a593Smuzhiyun * UDCDR = UDC Endpoint Data Register (the fifo)
173*4882a593Smuzhiyun */
174*4882a593Smuzhiyun #define ofs_UDCCR(ep) (UDCCRn(ep->idx))
175*4882a593Smuzhiyun #define ofs_UDCCSR(ep) (UDCCSRn(ep->idx))
176*4882a593Smuzhiyun #define ofs_UDCBCR(ep) (UDCBCRn(ep->idx))
177*4882a593Smuzhiyun #define ofs_UDCDR(ep) (UDCDRn(ep->idx))
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /* Register access macros */
180*4882a593Smuzhiyun #define udc_ep_readl(ep, reg) \
181*4882a593Smuzhiyun __raw_readl((ep)->dev->regs + ofs_##reg(ep))
182*4882a593Smuzhiyun #define udc_ep_writel(ep, reg, value) \
183*4882a593Smuzhiyun __raw_writel((value), ep->dev->regs + ofs_##reg(ep))
184*4882a593Smuzhiyun #define udc_ep_readb(ep, reg) \
185*4882a593Smuzhiyun __raw_readb((ep)->dev->regs + ofs_##reg(ep))
186*4882a593Smuzhiyun #define udc_ep_writeb(ep, reg, value) \
187*4882a593Smuzhiyun __raw_writeb((value), ep->dev->regs + ofs_##reg(ep))
188*4882a593Smuzhiyun #define udc_readl(dev, reg) \
189*4882a593Smuzhiyun __raw_readl((dev)->regs + (reg))
190*4882a593Smuzhiyun #define udc_writel(udc, reg, value) \
191*4882a593Smuzhiyun __raw_writel((value), (udc)->regs + (reg))
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun #define UDCCSR_MASK (UDCCSR_FST | UDCCSR_DME)
194*4882a593Smuzhiyun #define UDCCISR0_EP_MASK ~0
195*4882a593Smuzhiyun #define UDCCISR1_EP_MASK 0xffff
196*4882a593Smuzhiyun #define UDCCSR0_CTRL_REQ_MASK (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE)
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun #define EPIDX(ep) (ep->idx)
199*4882a593Smuzhiyun #define EPADDR(ep) (ep->addr)
200*4882a593Smuzhiyun #define EPXFERTYPE(ep) (ep->type)
201*4882a593Smuzhiyun #define EPNAME(ep) (ep->name)
202*4882a593Smuzhiyun #define is_ep0(ep) (!ep->idx)
203*4882a593Smuzhiyun #define EPXFERTYPE_is_ISO(ep) (EPXFERTYPE(ep) == USB_ENDPOINT_XFER_ISOC)
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /*
206*4882a593Smuzhiyun * Endpoint definitions
207*4882a593Smuzhiyun *
208*4882a593Smuzhiyun * Once enabled, pxa endpoint configuration is freezed, and cannot change
209*4882a593Smuzhiyun * unless a reset happens or the udc is disabled.
210*4882a593Smuzhiyun * Therefore, we must define all pxa potential endpoint definitions needed for
211*4882a593Smuzhiyun * all gadget and set them up before the udc is enabled.
212*4882a593Smuzhiyun *
213*4882a593Smuzhiyun * As the architecture chosen is fully static, meaning the pxa endpoint
214*4882a593Smuzhiyun * configurations are set up once and for all, we must provide a way to match
215*4882a593Smuzhiyun * one usb endpoint (usb_ep) to several pxa endpoints. The reason is that gadget
216*4882a593Smuzhiyun * layer autoconf doesn't choose the usb_ep endpoint on (config, interface, alt)
217*4882a593Smuzhiyun * criteria, while the pxa architecture requires that.
218*4882a593Smuzhiyun *
219*4882a593Smuzhiyun * The solution is to define several pxa endpoints matching one usb_ep. Ex:
220*4882a593Smuzhiyun * - "ep1-in" matches pxa endpoint EPA (which is an IN ep at addr 1, when
221*4882a593Smuzhiyun * the udc talks on (config=3, interface=0, alt=0)
222*4882a593Smuzhiyun * - "ep1-in" matches pxa endpoint EPB (which is an IN ep at addr 1, when
223*4882a593Smuzhiyun * the udc talks on (config=3, interface=0, alt=1)
224*4882a593Smuzhiyun * - "ep1-in" matches pxa endpoint EPC (which is an IN ep at addr 1, when
225*4882a593Smuzhiyun * the udc talks on (config=2, interface=0, alt=0)
226*4882a593Smuzhiyun *
227*4882a593Smuzhiyun * We'll define the pxa endpoint by its index (EPA => idx=1, EPB => idx=2, ...)
228*4882a593Smuzhiyun */
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /*
231*4882a593Smuzhiyun * Endpoint definition helpers
232*4882a593Smuzhiyun */
233*4882a593Smuzhiyun #define USB_EP_DEF(addr, bname, dir, type, maxpkt, ctype, cdir) \
234*4882a593Smuzhiyun { .usb_ep = { .name = bname, .ops = &pxa_ep_ops, .maxpacket = maxpkt, \
235*4882a593Smuzhiyun .caps = USB_EP_CAPS(ctype, cdir), }, \
236*4882a593Smuzhiyun .desc = { .bEndpointAddress = addr | (dir ? USB_DIR_IN : 0), \
237*4882a593Smuzhiyun .bmAttributes = USB_ENDPOINT_XFER_ ## type, \
238*4882a593Smuzhiyun .wMaxPacketSize = maxpkt, }, \
239*4882a593Smuzhiyun .dev = &memory \
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun #define USB_EP_BULK(addr, bname, dir, cdir) \
242*4882a593Smuzhiyun USB_EP_DEF(addr, bname, dir, BULK, BULK_FIFO_SIZE, \
243*4882a593Smuzhiyun USB_EP_CAPS_TYPE_BULK, cdir)
244*4882a593Smuzhiyun #define USB_EP_ISO(addr, bname, dir, cdir) \
245*4882a593Smuzhiyun USB_EP_DEF(addr, bname, dir, ISOC, ISO_FIFO_SIZE, \
246*4882a593Smuzhiyun USB_EP_CAPS_TYPE_ISO, cdir)
247*4882a593Smuzhiyun #define USB_EP_INT(addr, bname, dir, cdir) \
248*4882a593Smuzhiyun USB_EP_DEF(addr, bname, dir, INT, INT_FIFO_SIZE, \
249*4882a593Smuzhiyun USB_EP_CAPS_TYPE_INT, cdir)
250*4882a593Smuzhiyun #define USB_EP_IN_BULK(n) USB_EP_BULK(n, "ep" #n "in-bulk", 1, \
251*4882a593Smuzhiyun USB_EP_CAPS_DIR_IN)
252*4882a593Smuzhiyun #define USB_EP_OUT_BULK(n) USB_EP_BULK(n, "ep" #n "out-bulk", 0, \
253*4882a593Smuzhiyun USB_EP_CAPS_DIR_OUT)
254*4882a593Smuzhiyun #define USB_EP_IN_ISO(n) USB_EP_ISO(n, "ep" #n "in-iso", 1, \
255*4882a593Smuzhiyun USB_EP_CAPS_DIR_IN)
256*4882a593Smuzhiyun #define USB_EP_OUT_ISO(n) USB_EP_ISO(n, "ep" #n "out-iso", 0, \
257*4882a593Smuzhiyun USB_EP_CAPS_DIR_OUT)
258*4882a593Smuzhiyun #define USB_EP_IN_INT(n) USB_EP_INT(n, "ep" #n "in-int", 1, \
259*4882a593Smuzhiyun USB_EP_CAPS_DIR_IN)
260*4882a593Smuzhiyun #define USB_EP_CTRL USB_EP_DEF(0, "ep0", 0, CONTROL, EP0_FIFO_SIZE, \
261*4882a593Smuzhiyun USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun #define PXA_EP_DEF(_idx, _addr, dir, _type, maxpkt, _config, iface, altset) \
264*4882a593Smuzhiyun { \
265*4882a593Smuzhiyun .dev = &memory, \
266*4882a593Smuzhiyun .name = "ep" #_idx, \
267*4882a593Smuzhiyun .idx = _idx, .enabled = 0, \
268*4882a593Smuzhiyun .dir_in = dir, .addr = _addr, \
269*4882a593Smuzhiyun .config = _config, .interface = iface, .alternate = altset, \
270*4882a593Smuzhiyun .type = _type, .fifo_size = maxpkt, \
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun #define PXA_EP_BULK(_idx, addr, dir, config, iface, alt) \
273*4882a593Smuzhiyun PXA_EP_DEF(_idx, addr, dir, USB_ENDPOINT_XFER_BULK, BULK_FIFO_SIZE, \
274*4882a593Smuzhiyun config, iface, alt)
275*4882a593Smuzhiyun #define PXA_EP_ISO(_idx, addr, dir, config, iface, alt) \
276*4882a593Smuzhiyun PXA_EP_DEF(_idx, addr, dir, USB_ENDPOINT_XFER_ISOC, ISO_FIFO_SIZE, \
277*4882a593Smuzhiyun config, iface, alt)
278*4882a593Smuzhiyun #define PXA_EP_INT(_idx, addr, dir, config, iface, alt) \
279*4882a593Smuzhiyun PXA_EP_DEF(_idx, addr, dir, USB_ENDPOINT_XFER_INT, INT_FIFO_SIZE, \
280*4882a593Smuzhiyun config, iface, alt)
281*4882a593Smuzhiyun #define PXA_EP_IN_BULK(i, adr, c, f, a) PXA_EP_BULK(i, adr, 1, c, f, a)
282*4882a593Smuzhiyun #define PXA_EP_OUT_BULK(i, adr, c, f, a) PXA_EP_BULK(i, adr, 0, c, f, a)
283*4882a593Smuzhiyun #define PXA_EP_IN_ISO(i, adr, c, f, a) PXA_EP_ISO(i, adr, 1, c, f, a)
284*4882a593Smuzhiyun #define PXA_EP_OUT_ISO(i, adr, c, f, a) PXA_EP_ISO(i, adr, 0, c, f, a)
285*4882a593Smuzhiyun #define PXA_EP_IN_INT(i, adr, c, f, a) PXA_EP_INT(i, adr, 1, c, f, a)
286*4882a593Smuzhiyun #define PXA_EP_CTRL PXA_EP_DEF(0, 0, 0, 0, EP0_FIFO_SIZE, 0, 0, 0)
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun struct pxa27x_udc;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun struct stats {
291*4882a593Smuzhiyun unsigned long in_ops;
292*4882a593Smuzhiyun unsigned long out_ops;
293*4882a593Smuzhiyun unsigned long in_bytes;
294*4882a593Smuzhiyun unsigned long out_bytes;
295*4882a593Smuzhiyun unsigned long irqs;
296*4882a593Smuzhiyun };
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /**
299*4882a593Smuzhiyun * struct udc_usb_ep - container of each usb_ep structure
300*4882a593Smuzhiyun * @usb_ep: usb endpoint
301*4882a593Smuzhiyun * @desc: usb descriptor, especially type and address
302*4882a593Smuzhiyun * @dev: udc managing this endpoint
303*4882a593Smuzhiyun * @pxa_ep: matching pxa_ep (cache of find_pxa_ep() call)
304*4882a593Smuzhiyun */
305*4882a593Smuzhiyun struct udc_usb_ep {
306*4882a593Smuzhiyun struct usb_ep usb_ep;
307*4882a593Smuzhiyun struct usb_endpoint_descriptor desc;
308*4882a593Smuzhiyun struct pxa_udc *dev;
309*4882a593Smuzhiyun struct pxa_ep *pxa_ep;
310*4882a593Smuzhiyun };
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /**
313*4882a593Smuzhiyun * struct pxa_ep - pxa endpoint
314*4882a593Smuzhiyun * @dev: udc device
315*4882a593Smuzhiyun * @queue: requests queue
316*4882a593Smuzhiyun * @lock: lock to pxa_ep data (queues and stats)
317*4882a593Smuzhiyun * @enabled: true when endpoint enabled (not stopped by gadget layer)
318*4882a593Smuzhiyun * @in_handle_ep: number of recursions of handle_ep() function
319*4882a593Smuzhiyun * Prevents deadlocks or infinite recursions of types :
320*4882a593Smuzhiyun * irq->handle_ep()->req_done()->req.complete()->pxa_ep_queue()->handle_ep()
321*4882a593Smuzhiyun * or
322*4882a593Smuzhiyun * pxa_ep_queue()->handle_ep()->req_done()->req.complete()->pxa_ep_queue()
323*4882a593Smuzhiyun * @idx: endpoint index (1 => epA, 2 => epB, ..., 24 => epX)
324*4882a593Smuzhiyun * @name: endpoint name (for trace/debug purpose)
325*4882a593Smuzhiyun * @dir_in: 1 if IN endpoint, 0 if OUT endpoint
326*4882a593Smuzhiyun * @addr: usb endpoint number
327*4882a593Smuzhiyun * @config: configuration in which this endpoint is active
328*4882a593Smuzhiyun * @interface: interface in which this endpoint is active
329*4882a593Smuzhiyun * @alternate: altsetting in which this endpoitn is active
330*4882a593Smuzhiyun * @fifo_size: max packet size in the endpoint fifo
331*4882a593Smuzhiyun * @type: endpoint type (bulk, iso, int, ...)
332*4882a593Smuzhiyun * @udccsr_value: save register of UDCCSR0 for suspend/resume
333*4882a593Smuzhiyun * @udccr_value: save register of UDCCR for suspend/resume
334*4882a593Smuzhiyun * @stats: endpoint statistics
335*4882a593Smuzhiyun *
336*4882a593Smuzhiyun * The *PROBLEM* is that pxa's endpoint configuration scheme is both misdesigned
337*4882a593Smuzhiyun * (cares about config/interface/altsetting, thus placing needless limits on
338*4882a593Smuzhiyun * device capability) and full of implementation bugs forcing it to be set up
339*4882a593Smuzhiyun * for use more or less like a pxa255.
340*4882a593Smuzhiyun *
341*4882a593Smuzhiyun * As we define the pxa_ep statically, we must guess all needed pxa_ep for all
342*4882a593Smuzhiyun * gadget which may work with this udc driver.
343*4882a593Smuzhiyun */
344*4882a593Smuzhiyun struct pxa_ep {
345*4882a593Smuzhiyun struct pxa_udc *dev;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun struct list_head queue;
348*4882a593Smuzhiyun spinlock_t lock; /* Protects this structure */
349*4882a593Smuzhiyun /* (queues, stats) */
350*4882a593Smuzhiyun unsigned enabled:1;
351*4882a593Smuzhiyun unsigned in_handle_ep:1;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun unsigned idx:5;
354*4882a593Smuzhiyun char *name;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun /*
357*4882a593Smuzhiyun * Specific pxa endpoint data, needed for hardware initialization
358*4882a593Smuzhiyun */
359*4882a593Smuzhiyun unsigned dir_in:1;
360*4882a593Smuzhiyun unsigned addr:4;
361*4882a593Smuzhiyun unsigned config:2;
362*4882a593Smuzhiyun unsigned interface:3;
363*4882a593Smuzhiyun unsigned alternate:3;
364*4882a593Smuzhiyun unsigned fifo_size;
365*4882a593Smuzhiyun unsigned type;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun #ifdef CONFIG_PM
368*4882a593Smuzhiyun u32 udccsr_value;
369*4882a593Smuzhiyun u32 udccr_value;
370*4882a593Smuzhiyun #endif
371*4882a593Smuzhiyun struct stats stats;
372*4882a593Smuzhiyun };
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun /**
375*4882a593Smuzhiyun * struct pxa27x_request - container of each usb_request structure
376*4882a593Smuzhiyun * @req: usb request
377*4882a593Smuzhiyun * @udc_usb_ep: usb endpoint the request was submitted on
378*4882a593Smuzhiyun * @in_use: sanity check if request already queued on an pxa_ep
379*4882a593Smuzhiyun * @queue: linked list of requests, linked on pxa_ep->queue
380*4882a593Smuzhiyun */
381*4882a593Smuzhiyun struct pxa27x_request {
382*4882a593Smuzhiyun struct usb_request req;
383*4882a593Smuzhiyun struct udc_usb_ep *udc_usb_ep;
384*4882a593Smuzhiyun unsigned in_use:1;
385*4882a593Smuzhiyun struct list_head queue;
386*4882a593Smuzhiyun };
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun enum ep0_state {
389*4882a593Smuzhiyun WAIT_FOR_SETUP,
390*4882a593Smuzhiyun SETUP_STAGE,
391*4882a593Smuzhiyun IN_DATA_STAGE,
392*4882a593Smuzhiyun OUT_DATA_STAGE,
393*4882a593Smuzhiyun IN_STATUS_STAGE,
394*4882a593Smuzhiyun OUT_STATUS_STAGE,
395*4882a593Smuzhiyun STALL,
396*4882a593Smuzhiyun WAIT_ACK_SET_CONF_INTERF
397*4882a593Smuzhiyun };
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun static char *ep0_state_name[] = {
400*4882a593Smuzhiyun "WAIT_FOR_SETUP", "SETUP_STAGE", "IN_DATA_STAGE", "OUT_DATA_STAGE",
401*4882a593Smuzhiyun "IN_STATUS_STAGE", "OUT_STATUS_STAGE", "STALL",
402*4882a593Smuzhiyun "WAIT_ACK_SET_CONF_INTERF"
403*4882a593Smuzhiyun };
404*4882a593Smuzhiyun #define EP0_STNAME(udc) ep0_state_name[(udc)->ep0state]
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun #define EP0_FIFO_SIZE 16U
407*4882a593Smuzhiyun #define BULK_FIFO_SIZE 64U
408*4882a593Smuzhiyun #define ISO_FIFO_SIZE 256U
409*4882a593Smuzhiyun #define INT_FIFO_SIZE 16U
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun struct udc_stats {
412*4882a593Smuzhiyun unsigned long irqs_reset;
413*4882a593Smuzhiyun unsigned long irqs_suspend;
414*4882a593Smuzhiyun unsigned long irqs_resume;
415*4882a593Smuzhiyun unsigned long irqs_reconfig;
416*4882a593Smuzhiyun };
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun #define NR_USB_ENDPOINTS (1 + 5) /* ep0 + ep1in-bulk + .. + ep3in-iso */
419*4882a593Smuzhiyun #define NR_PXA_ENDPOINTS (1 + 14) /* ep0 + epA + epB + .. + epX */
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun /**
422*4882a593Smuzhiyun * struct pxa_udc - udc structure
423*4882a593Smuzhiyun * @regs: mapped IO space
424*4882a593Smuzhiyun * @irq: udc irq
425*4882a593Smuzhiyun * @clk: udc clock
426*4882a593Smuzhiyun * @usb_gadget: udc gadget structure
427*4882a593Smuzhiyun * @driver: bound gadget (zero, g_ether, g_mass_storage, ...)
428*4882a593Smuzhiyun * @dev: device
429*4882a593Smuzhiyun * @udc_command: machine specific function to activate D+ pullup
430*4882a593Smuzhiyun * @gpiod: gpio descriptor of gpio for D+ pullup (or NULL if none)
431*4882a593Smuzhiyun * @transceiver: external transceiver to handle vbus sense and D+ pullup
432*4882a593Smuzhiyun * @ep0state: control endpoint state machine state
433*4882a593Smuzhiyun * @stats: statistics on udc usage
434*4882a593Smuzhiyun * @udc_usb_ep: array of usb endpoints offered by the gadget
435*4882a593Smuzhiyun * @pxa_ep: array of pxa available endpoints
436*4882a593Smuzhiyun * @enabled: UDC was enabled by a previous udc_enable()
437*4882a593Smuzhiyun * @pullup_on: if pullup resistor connected to D+ pin
438*4882a593Smuzhiyun * @pullup_resume: if pullup resistor should be connected to D+ pin on resume
439*4882a593Smuzhiyun * @config: UDC active configuration
440*4882a593Smuzhiyun * @last_interface: UDC interface of the last SET_INTERFACE host request
441*4882a593Smuzhiyun * @last_alternate: UDC altsetting of the last SET_INTERFACE host request
442*4882a593Smuzhiyun * @udccsr0: save of udccsr0 in case of suspend
443*4882a593Smuzhiyun * @debugfs_root: root entry of debug filesystem
444*4882a593Smuzhiyun * @debugfs_state: debugfs entry for "udcstate"
445*4882a593Smuzhiyun * @debugfs_queues: debugfs entry for "queues"
446*4882a593Smuzhiyun * @debugfs_eps: debugfs entry for "epstate"
447*4882a593Smuzhiyun */
448*4882a593Smuzhiyun struct pxa_udc {
449*4882a593Smuzhiyun void __iomem *regs;
450*4882a593Smuzhiyun int irq;
451*4882a593Smuzhiyun struct clk *clk;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun struct usb_gadget gadget;
454*4882a593Smuzhiyun struct usb_gadget_driver *driver;
455*4882a593Smuzhiyun struct device *dev;
456*4882a593Smuzhiyun void (*udc_command)(int);
457*4882a593Smuzhiyun struct gpio_desc *gpiod;
458*4882a593Smuzhiyun struct usb_phy *transceiver;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun enum ep0_state ep0state;
461*4882a593Smuzhiyun struct udc_stats stats;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun struct udc_usb_ep udc_usb_ep[NR_USB_ENDPOINTS];
464*4882a593Smuzhiyun struct pxa_ep pxa_ep[NR_PXA_ENDPOINTS];
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun unsigned enabled:1;
467*4882a593Smuzhiyun unsigned pullup_on:1;
468*4882a593Smuzhiyun unsigned pullup_resume:1;
469*4882a593Smuzhiyun unsigned vbus_sensed:1;
470*4882a593Smuzhiyun unsigned config:2;
471*4882a593Smuzhiyun unsigned last_interface:3;
472*4882a593Smuzhiyun unsigned last_alternate:3;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun #ifdef CONFIG_PM
475*4882a593Smuzhiyun unsigned udccsr0;
476*4882a593Smuzhiyun #endif
477*4882a593Smuzhiyun #ifdef CONFIG_USB_GADGET_DEBUG_FS
478*4882a593Smuzhiyun struct dentry *debugfs_root;
479*4882a593Smuzhiyun #endif
480*4882a593Smuzhiyun };
481*4882a593Smuzhiyun #define to_pxa(g) (container_of((g), struct pxa_udc, gadget))
482*4882a593Smuzhiyun
to_gadget_udc(struct usb_gadget * gadget)483*4882a593Smuzhiyun static inline struct pxa_udc *to_gadget_udc(struct usb_gadget *gadget)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun return container_of(gadget, struct pxa_udc, gadget);
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun /*
489*4882a593Smuzhiyun * Debugging/message support
490*4882a593Smuzhiyun */
491*4882a593Smuzhiyun #define ep_dbg(ep, fmt, arg...) \
492*4882a593Smuzhiyun dev_dbg(ep->dev->dev, "%s:%s: " fmt, EPNAME(ep), __func__, ## arg)
493*4882a593Smuzhiyun #define ep_vdbg(ep, fmt, arg...) \
494*4882a593Smuzhiyun dev_vdbg(ep->dev->dev, "%s:%s: " fmt, EPNAME(ep), __func__, ## arg)
495*4882a593Smuzhiyun #define ep_err(ep, fmt, arg...) \
496*4882a593Smuzhiyun dev_err(ep->dev->dev, "%s:%s: " fmt, EPNAME(ep), __func__, ## arg)
497*4882a593Smuzhiyun #define ep_info(ep, fmt, arg...) \
498*4882a593Smuzhiyun dev_info(ep->dev->dev, "%s:%s: " fmt, EPNAME(ep), __func__, ## arg)
499*4882a593Smuzhiyun #define ep_warn(ep, fmt, arg...) \
500*4882a593Smuzhiyun dev_warn(ep->dev->dev, "%s:%s:" fmt, EPNAME(ep), __func__, ## arg)
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun #endif /* __LINUX_USB_GADGET_PXA27X_H */
503