18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * linux/drivers/usb/gadget/pxa27x_udc.h
48c2ecf20Sopenharmony_ci * Intel PXA27x on-chip full speed USB device controller
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Inspired by original driver by Frank Becker, David Brownell, and others.
78c2ecf20Sopenharmony_ci * Copyright (C) 2008 Robert Jarzmik
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef __LINUX_USB_GADGET_PXA27X_H
118c2ecf20Sopenharmony_ci#define __LINUX_USB_GADGET_PXA27X_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
158c2ecf20Sopenharmony_ci#include <linux/io.h>
168c2ecf20Sopenharmony_ci#include <linux/usb/otg.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci * Register definitions
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci/* Offsets */
228c2ecf20Sopenharmony_ci#define UDCCR		0x0000		/* UDC Control Register */
238c2ecf20Sopenharmony_ci#define UDCICR0		0x0004		/* UDC Interrupt Control Register0 */
248c2ecf20Sopenharmony_ci#define UDCICR1		0x0008		/* UDC Interrupt Control Register1 */
258c2ecf20Sopenharmony_ci#define UDCISR0		0x000C		/* UDC Interrupt Status Register 0 */
268c2ecf20Sopenharmony_ci#define UDCISR1		0x0010		/* UDC Interrupt Status Register 1 */
278c2ecf20Sopenharmony_ci#define UDCFNR		0x0014		/* UDC Frame Number Register */
288c2ecf20Sopenharmony_ci#define UDCOTGICR	0x0018		/* UDC On-The-Go interrupt control */
298c2ecf20Sopenharmony_ci#define UP2OCR		0x0020		/* USB Port 2 Output Control register */
308c2ecf20Sopenharmony_ci#define UP3OCR		0x0024		/* USB Port 3 Output Control register */
318c2ecf20Sopenharmony_ci#define UDCCSRn(x)	(0x0100 + ((x)<<2)) /* UDC Control/Status register */
328c2ecf20Sopenharmony_ci#define UDCBCRn(x)	(0x0200 + ((x)<<2)) /* UDC Byte Count Register */
338c2ecf20Sopenharmony_ci#define UDCDRn(x)	(0x0300 + ((x)<<2)) /* UDC Data Register  */
348c2ecf20Sopenharmony_ci#define UDCCRn(x)	(0x0400 + ((x)<<2)) /* UDC Control Register */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define UDCCR_OEN	(1 << 31)	/* On-the-Go Enable */
378c2ecf20Sopenharmony_ci#define UDCCR_AALTHNP	(1 << 30)	/* A-device Alternate Host Negotiation
388c2ecf20Sopenharmony_ci					   Protocol Port Support */
398c2ecf20Sopenharmony_ci#define UDCCR_AHNP	(1 << 29)	/* A-device Host Negotiation Protocol
408c2ecf20Sopenharmony_ci					   Support */
418c2ecf20Sopenharmony_ci#define UDCCR_BHNP	(1 << 28)	/* B-device Host Negotiation Protocol
428c2ecf20Sopenharmony_ci					   Enable */
438c2ecf20Sopenharmony_ci#define UDCCR_DWRE	(1 << 16)	/* Device Remote Wake-up Enable */
448c2ecf20Sopenharmony_ci#define UDCCR_ACN	(0x03 << 11)	/* Active UDC configuration Number */
458c2ecf20Sopenharmony_ci#define UDCCR_ACN_S	11
468c2ecf20Sopenharmony_ci#define UDCCR_AIN	(0x07 << 8)	/* Active UDC interface Number */
478c2ecf20Sopenharmony_ci#define UDCCR_AIN_S	8
488c2ecf20Sopenharmony_ci#define UDCCR_AAISN	(0x07 << 5)	/* Active UDC Alternate Interface
498c2ecf20Sopenharmony_ci					   Setting Number */
508c2ecf20Sopenharmony_ci#define UDCCR_AAISN_S	5
518c2ecf20Sopenharmony_ci#define UDCCR_SMAC	(1 << 4)	/* Switch Endpoint Memory to Active
528c2ecf20Sopenharmony_ci					   Configuration */
538c2ecf20Sopenharmony_ci#define UDCCR_EMCE	(1 << 3)	/* Endpoint Memory Configuration
548c2ecf20Sopenharmony_ci					   Error */
558c2ecf20Sopenharmony_ci#define UDCCR_UDR	(1 << 2)	/* UDC Resume */
568c2ecf20Sopenharmony_ci#define UDCCR_UDA	(1 << 1)	/* UDC Active */
578c2ecf20Sopenharmony_ci#define UDCCR_UDE	(1 << 0)	/* UDC Enable */
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define UDCICR_INT(n, intr) (((intr) & 0x03) << (((n) & 0x0F) * 2))
608c2ecf20Sopenharmony_ci#define UDCICR1_IECC	(1 << 31)	/* IntEn - Configuration Change */
618c2ecf20Sopenharmony_ci#define UDCICR1_IESOF	(1 << 30)	/* IntEn - Start of Frame */
628c2ecf20Sopenharmony_ci#define UDCICR1_IERU	(1 << 29)	/* IntEn - Resume */
638c2ecf20Sopenharmony_ci#define UDCICR1_IESU	(1 << 28)	/* IntEn - Suspend */
648c2ecf20Sopenharmony_ci#define UDCICR1_IERS	(1 << 27)	/* IntEn - Reset */
658c2ecf20Sopenharmony_ci#define UDCICR_FIFOERR	(1 << 1)	/* FIFO Error interrupt for EP */
668c2ecf20Sopenharmony_ci#define UDCICR_PKTCOMPL	(1 << 0)	/* Packet Complete interrupt for EP */
678c2ecf20Sopenharmony_ci#define UDCICR_INT_MASK	(UDCICR_FIFOERR | UDCICR_PKTCOMPL)
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define UDCISR_INT(n, intr) (((intr) & 0x03) << (((n) & 0x0F) * 2))
708c2ecf20Sopenharmony_ci#define UDCISR1_IRCC	(1 << 31)	/* IntReq - Configuration Change */
718c2ecf20Sopenharmony_ci#define UDCISR1_IRSOF	(1 << 30)	/* IntReq - Start of Frame */
728c2ecf20Sopenharmony_ci#define UDCISR1_IRRU	(1 << 29)	/* IntReq - Resume */
738c2ecf20Sopenharmony_ci#define UDCISR1_IRSU	(1 << 28)	/* IntReq - Suspend */
748c2ecf20Sopenharmony_ci#define UDCISR1_IRRS	(1 << 27)	/* IntReq - Reset */
758c2ecf20Sopenharmony_ci#define UDCISR_INT_MASK	(UDCICR_FIFOERR | UDCICR_PKTCOMPL)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define UDCOTGICR_IESF	(1 << 24)	/* OTG SET_FEATURE command recvd */
788c2ecf20Sopenharmony_ci#define UDCOTGICR_IEXR	(1 << 17)	/* Extra Transceiver Interrupt
798c2ecf20Sopenharmony_ci					   Rising Edge Interrupt Enable */
808c2ecf20Sopenharmony_ci#define UDCOTGICR_IEXF	(1 << 16)	/* Extra Transceiver Interrupt
818c2ecf20Sopenharmony_ci					   Falling Edge Interrupt Enable */
828c2ecf20Sopenharmony_ci#define UDCOTGICR_IEVV40R (1 << 9)	/* OTG Vbus Valid 4.0V Rising Edge
838c2ecf20Sopenharmony_ci					   Interrupt Enable */
848c2ecf20Sopenharmony_ci#define UDCOTGICR_IEVV40F (1 << 8)	/* OTG Vbus Valid 4.0V Falling Edge
858c2ecf20Sopenharmony_ci					   Interrupt Enable */
868c2ecf20Sopenharmony_ci#define UDCOTGICR_IEVV44R (1 << 7)	/* OTG Vbus Valid 4.4V Rising Edge
878c2ecf20Sopenharmony_ci					   Interrupt Enable */
888c2ecf20Sopenharmony_ci#define UDCOTGICR_IEVV44F (1 << 6)	/* OTG Vbus Valid 4.4V Falling Edge
898c2ecf20Sopenharmony_ci					   Interrupt Enable */
908c2ecf20Sopenharmony_ci#define UDCOTGICR_IESVR	(1 << 5)	/* OTG Session Valid Rising Edge
918c2ecf20Sopenharmony_ci					   Interrupt Enable */
928c2ecf20Sopenharmony_ci#define UDCOTGICR_IESVF	(1 << 4)	/* OTG Session Valid Falling Edge
938c2ecf20Sopenharmony_ci					   Interrupt Enable */
948c2ecf20Sopenharmony_ci#define UDCOTGICR_IESDR	(1 << 3)	/* OTG A-Device SRP Detect Rising
958c2ecf20Sopenharmony_ci					   Edge Interrupt Enable */
968c2ecf20Sopenharmony_ci#define UDCOTGICR_IESDF	(1 << 2)	/* OTG A-Device SRP Detect Falling
978c2ecf20Sopenharmony_ci					   Edge Interrupt Enable */
988c2ecf20Sopenharmony_ci#define UDCOTGICR_IEIDR	(1 << 1)	/* OTG ID Change Rising Edge
998c2ecf20Sopenharmony_ci					   Interrupt Enable */
1008c2ecf20Sopenharmony_ci#define UDCOTGICR_IEIDF	(1 << 0)	/* OTG ID Change Falling Edge
1018c2ecf20Sopenharmony_ci					   Interrupt Enable */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* Host Port 2 field bits */
1048c2ecf20Sopenharmony_ci#define UP2OCR_CPVEN	(1 << 0)	/* Charge Pump Vbus Enable */
1058c2ecf20Sopenharmony_ci#define UP2OCR_CPVPE	(1 << 1)	/* Charge Pump Vbus Pulse Enable */
1068c2ecf20Sopenharmony_ci					/* Transceiver enablers */
1078c2ecf20Sopenharmony_ci#define UP2OCR_DPPDE	(1 << 2)	/*   D+ Pull Down Enable */
1088c2ecf20Sopenharmony_ci#define UP2OCR_DMPDE	(1 << 3)	/*   D- Pull Down Enable */
1098c2ecf20Sopenharmony_ci#define UP2OCR_DPPUE	(1 << 4)	/*   D+ Pull Up Enable */
1108c2ecf20Sopenharmony_ci#define UP2OCR_DMPUE	(1 << 5)	/*   D- Pull Up Enable */
1118c2ecf20Sopenharmony_ci#define UP2OCR_DPPUBE	(1 << 6)	/*   D+ Pull Up Bypass Enable */
1128c2ecf20Sopenharmony_ci#define UP2OCR_DMPUBE	(1 << 7)	/*   D- Pull Up Bypass Enable */
1138c2ecf20Sopenharmony_ci#define UP2OCR_EXSP	(1 << 8)	/* External Transceiver Speed Control */
1148c2ecf20Sopenharmony_ci#define UP2OCR_EXSUS	(1 << 9)	/* External Transceiver Speed Enable */
1158c2ecf20Sopenharmony_ci#define UP2OCR_IDON	(1 << 10)	/* OTG ID Read Enable */
1168c2ecf20Sopenharmony_ci#define UP2OCR_HXS	(1 << 16)	/* Transceiver Output Select */
1178c2ecf20Sopenharmony_ci#define UP2OCR_HXOE	(1 << 17)	/* Transceiver Output Enable */
1188c2ecf20Sopenharmony_ci#define UP2OCR_SEOS	(1 << 24)	/* Single-Ended Output Select */
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define UDCCSR0_ACM	(1 << 9)	/* Ack Control Mode */
1218c2ecf20Sopenharmony_ci#define UDCCSR0_AREN	(1 << 8)	/* Ack Response Enable */
1228c2ecf20Sopenharmony_ci#define UDCCSR0_SA	(1 << 7)	/* Setup Active */
1238c2ecf20Sopenharmony_ci#define UDCCSR0_RNE	(1 << 6)	/* Receive FIFO Not Empty */
1248c2ecf20Sopenharmony_ci#define UDCCSR0_FST	(1 << 5)	/* Force Stall */
1258c2ecf20Sopenharmony_ci#define UDCCSR0_SST	(1 << 4)	/* Sent Stall */
1268c2ecf20Sopenharmony_ci#define UDCCSR0_DME	(1 << 3)	/* DMA Enable */
1278c2ecf20Sopenharmony_ci#define UDCCSR0_FTF	(1 << 2)	/* Flush Transmit FIFO */
1288c2ecf20Sopenharmony_ci#define UDCCSR0_IPR	(1 << 1)	/* IN Packet Ready */
1298c2ecf20Sopenharmony_ci#define UDCCSR0_OPC	(1 << 0)	/* OUT Packet Complete */
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci#define UDCCSR_DPE	(1 << 9)	/* Data Packet Error */
1328c2ecf20Sopenharmony_ci#define UDCCSR_FEF	(1 << 8)	/* Flush Endpoint FIFO */
1338c2ecf20Sopenharmony_ci#define UDCCSR_SP	(1 << 7)	/* Short Packet Control/Status */
1348c2ecf20Sopenharmony_ci#define UDCCSR_BNE	(1 << 6)	/* Buffer Not Empty (IN endpoints) */
1358c2ecf20Sopenharmony_ci#define UDCCSR_BNF	(1 << 6)	/* Buffer Not Full (OUT endpoints) */
1368c2ecf20Sopenharmony_ci#define UDCCSR_FST	(1 << 5)	/* Force STALL */
1378c2ecf20Sopenharmony_ci#define UDCCSR_SST	(1 << 4)	/* Sent STALL */
1388c2ecf20Sopenharmony_ci#define UDCCSR_DME	(1 << 3)	/* DMA Enable */
1398c2ecf20Sopenharmony_ci#define UDCCSR_TRN	(1 << 2)	/* Tx/Rx NAK */
1408c2ecf20Sopenharmony_ci#define UDCCSR_PC	(1 << 1)	/* Packet Complete */
1418c2ecf20Sopenharmony_ci#define UDCCSR_FS	(1 << 0)	/* FIFO needs service */
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#define UDCCONR_CN	(0x03 << 25)	/* Configuration Number */
1448c2ecf20Sopenharmony_ci#define UDCCONR_CN_S	25
1458c2ecf20Sopenharmony_ci#define UDCCONR_IN	(0x07 << 22)	/* Interface Number */
1468c2ecf20Sopenharmony_ci#define UDCCONR_IN_S	22
1478c2ecf20Sopenharmony_ci#define UDCCONR_AISN	(0x07 << 19)	/* Alternate Interface Number */
1488c2ecf20Sopenharmony_ci#define UDCCONR_AISN_S	19
1498c2ecf20Sopenharmony_ci#define UDCCONR_EN	(0x0f << 15)	/* Endpoint Number */
1508c2ecf20Sopenharmony_ci#define UDCCONR_EN_S	15
1518c2ecf20Sopenharmony_ci#define UDCCONR_ET	(0x03 << 13)	/* Endpoint Type: */
1528c2ecf20Sopenharmony_ci#define UDCCONR_ET_S	13
1538c2ecf20Sopenharmony_ci#define UDCCONR_ET_INT	(0x03 << 13)	/*   Interrupt */
1548c2ecf20Sopenharmony_ci#define UDCCONR_ET_BULK	(0x02 << 13)	/*   Bulk */
1558c2ecf20Sopenharmony_ci#define UDCCONR_ET_ISO	(0x01 << 13)	/*   Isochronous */
1568c2ecf20Sopenharmony_ci#define UDCCONR_ET_NU	(0x00 << 13)	/*   Not used */
1578c2ecf20Sopenharmony_ci#define UDCCONR_ED	(1 << 12)	/* Endpoint Direction */
1588c2ecf20Sopenharmony_ci#define UDCCONR_MPS	(0x3ff << 2)	/* Maximum Packet Size */
1598c2ecf20Sopenharmony_ci#define UDCCONR_MPS_S	2
1608c2ecf20Sopenharmony_ci#define UDCCONR_DE	(1 << 1)	/* Double Buffering Enable */
1618c2ecf20Sopenharmony_ci#define UDCCONR_EE	(1 << 0)	/* Endpoint Enable */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci#define UDCCR_MASK_BITS (UDCCR_OEN | UDCCR_SMAC | UDCCR_UDR | UDCCR_UDE)
1648c2ecf20Sopenharmony_ci#define UDCCSR_WR_MASK	(UDCCSR_DME | UDCCSR_FST)
1658c2ecf20Sopenharmony_ci#define UDC_FNR_MASK	(0x7ff)
1668c2ecf20Sopenharmony_ci#define UDC_BCR_MASK	(0x3ff)
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/*
1698c2ecf20Sopenharmony_ci * UDCCR = UDC Endpoint Configuration Registers
1708c2ecf20Sopenharmony_ci * UDCCSR = UDC Control/Status Register for this EP
1718c2ecf20Sopenharmony_ci * UDCBCR = UDC Byte Count Remaining (contents of OUT fifo)
1728c2ecf20Sopenharmony_ci * UDCDR = UDC Endpoint Data Register (the fifo)
1738c2ecf20Sopenharmony_ci */
1748c2ecf20Sopenharmony_ci#define ofs_UDCCR(ep)	(UDCCRn(ep->idx))
1758c2ecf20Sopenharmony_ci#define ofs_UDCCSR(ep)	(UDCCSRn(ep->idx))
1768c2ecf20Sopenharmony_ci#define ofs_UDCBCR(ep)	(UDCBCRn(ep->idx))
1778c2ecf20Sopenharmony_ci#define ofs_UDCDR(ep)	(UDCDRn(ep->idx))
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/* Register access macros */
1808c2ecf20Sopenharmony_ci#define udc_ep_readl(ep, reg)	\
1818c2ecf20Sopenharmony_ci	__raw_readl((ep)->dev->regs + ofs_##reg(ep))
1828c2ecf20Sopenharmony_ci#define udc_ep_writel(ep, reg, value)	\
1838c2ecf20Sopenharmony_ci	__raw_writel((value), ep->dev->regs + ofs_##reg(ep))
1848c2ecf20Sopenharmony_ci#define udc_ep_readb(ep, reg)	\
1858c2ecf20Sopenharmony_ci	__raw_readb((ep)->dev->regs + ofs_##reg(ep))
1868c2ecf20Sopenharmony_ci#define udc_ep_writeb(ep, reg, value)	\
1878c2ecf20Sopenharmony_ci	__raw_writeb((value), ep->dev->regs + ofs_##reg(ep))
1888c2ecf20Sopenharmony_ci#define udc_readl(dev, reg)	\
1898c2ecf20Sopenharmony_ci	__raw_readl((dev)->regs + (reg))
1908c2ecf20Sopenharmony_ci#define udc_writel(udc, reg, value)	\
1918c2ecf20Sopenharmony_ci	__raw_writel((value), (udc)->regs + (reg))
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci#define UDCCSR_MASK		(UDCCSR_FST | UDCCSR_DME)
1948c2ecf20Sopenharmony_ci#define UDCCISR0_EP_MASK	~0
1958c2ecf20Sopenharmony_ci#define UDCCISR1_EP_MASK	0xffff
1968c2ecf20Sopenharmony_ci#define UDCCSR0_CTRL_REQ_MASK	(UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE)
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci#define EPIDX(ep)	(ep->idx)
1998c2ecf20Sopenharmony_ci#define EPADDR(ep)	(ep->addr)
2008c2ecf20Sopenharmony_ci#define EPXFERTYPE(ep)	(ep->type)
2018c2ecf20Sopenharmony_ci#define EPNAME(ep)	(ep->name)
2028c2ecf20Sopenharmony_ci#define is_ep0(ep)	(!ep->idx)
2038c2ecf20Sopenharmony_ci#define EPXFERTYPE_is_ISO(ep) (EPXFERTYPE(ep) == USB_ENDPOINT_XFER_ISOC)
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci/*
2068c2ecf20Sopenharmony_ci * Endpoint definitions
2078c2ecf20Sopenharmony_ci *
2088c2ecf20Sopenharmony_ci * Once enabled, pxa endpoint configuration is freezed, and cannot change
2098c2ecf20Sopenharmony_ci * unless a reset happens or the udc is disabled.
2108c2ecf20Sopenharmony_ci * Therefore, we must define all pxa potential endpoint definitions needed for
2118c2ecf20Sopenharmony_ci * all gadget and set them up before the udc is enabled.
2128c2ecf20Sopenharmony_ci *
2138c2ecf20Sopenharmony_ci * As the architecture chosen is fully static, meaning the pxa endpoint
2148c2ecf20Sopenharmony_ci * configurations are set up once and for all, we must provide a way to match
2158c2ecf20Sopenharmony_ci * one usb endpoint (usb_ep) to several pxa endpoints. The reason is that gadget
2168c2ecf20Sopenharmony_ci * layer autoconf doesn't choose the usb_ep endpoint on (config, interface, alt)
2178c2ecf20Sopenharmony_ci * criteria, while the pxa architecture requires that.
2188c2ecf20Sopenharmony_ci *
2198c2ecf20Sopenharmony_ci * The solution is to define several pxa endpoints matching one usb_ep. Ex:
2208c2ecf20Sopenharmony_ci *   - "ep1-in" matches pxa endpoint EPA (which is an IN ep at addr 1, when
2218c2ecf20Sopenharmony_ci *     the udc talks on (config=3, interface=0, alt=0)
2228c2ecf20Sopenharmony_ci *   - "ep1-in" matches pxa endpoint EPB (which is an IN ep at addr 1, when
2238c2ecf20Sopenharmony_ci *     the udc talks on (config=3, interface=0, alt=1)
2248c2ecf20Sopenharmony_ci *   - "ep1-in" matches pxa endpoint EPC (which is an IN ep at addr 1, when
2258c2ecf20Sopenharmony_ci *     the udc talks on (config=2, interface=0, alt=0)
2268c2ecf20Sopenharmony_ci *
2278c2ecf20Sopenharmony_ci * We'll define the pxa endpoint by its index (EPA => idx=1, EPB => idx=2, ...)
2288c2ecf20Sopenharmony_ci */
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci/*
2318c2ecf20Sopenharmony_ci * Endpoint definition helpers
2328c2ecf20Sopenharmony_ci */
2338c2ecf20Sopenharmony_ci#define USB_EP_DEF(addr, bname, dir, type, maxpkt, ctype, cdir) \
2348c2ecf20Sopenharmony_ci{ .usb_ep = {	.name = bname, .ops = &pxa_ep_ops, .maxpacket = maxpkt, \
2358c2ecf20Sopenharmony_ci		.caps = USB_EP_CAPS(ctype, cdir), }, \
2368c2ecf20Sopenharmony_ci  .desc = {	.bEndpointAddress = addr | (dir ? USB_DIR_IN : 0), \
2378c2ecf20Sopenharmony_ci		.bmAttributes = USB_ENDPOINT_XFER_ ## type, \
2388c2ecf20Sopenharmony_ci		.wMaxPacketSize = maxpkt, }, \
2398c2ecf20Sopenharmony_ci  .dev = &memory \
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci#define USB_EP_BULK(addr, bname, dir, cdir) \
2428c2ecf20Sopenharmony_ci	USB_EP_DEF(addr, bname, dir, BULK, BULK_FIFO_SIZE, \
2438c2ecf20Sopenharmony_ci		USB_EP_CAPS_TYPE_BULK, cdir)
2448c2ecf20Sopenharmony_ci#define USB_EP_ISO(addr, bname, dir, cdir) \
2458c2ecf20Sopenharmony_ci	USB_EP_DEF(addr, bname, dir, ISOC, ISO_FIFO_SIZE, \
2468c2ecf20Sopenharmony_ci		USB_EP_CAPS_TYPE_ISO, cdir)
2478c2ecf20Sopenharmony_ci#define USB_EP_INT(addr, bname, dir, cdir) \
2488c2ecf20Sopenharmony_ci	USB_EP_DEF(addr, bname, dir, INT, INT_FIFO_SIZE, \
2498c2ecf20Sopenharmony_ci		USB_EP_CAPS_TYPE_INT, cdir)
2508c2ecf20Sopenharmony_ci#define USB_EP_IN_BULK(n)	USB_EP_BULK(n, "ep" #n "in-bulk", 1, \
2518c2ecf20Sopenharmony_ci					USB_EP_CAPS_DIR_IN)
2528c2ecf20Sopenharmony_ci#define USB_EP_OUT_BULK(n)	USB_EP_BULK(n, "ep" #n "out-bulk", 0, \
2538c2ecf20Sopenharmony_ci					USB_EP_CAPS_DIR_OUT)
2548c2ecf20Sopenharmony_ci#define USB_EP_IN_ISO(n)	USB_EP_ISO(n,  "ep" #n "in-iso", 1, \
2558c2ecf20Sopenharmony_ci					USB_EP_CAPS_DIR_IN)
2568c2ecf20Sopenharmony_ci#define USB_EP_OUT_ISO(n)	USB_EP_ISO(n,  "ep" #n "out-iso", 0, \
2578c2ecf20Sopenharmony_ci					USB_EP_CAPS_DIR_OUT)
2588c2ecf20Sopenharmony_ci#define USB_EP_IN_INT(n)	USB_EP_INT(n,  "ep" #n "in-int", 1, \
2598c2ecf20Sopenharmony_ci					USB_EP_CAPS_DIR_IN)
2608c2ecf20Sopenharmony_ci#define USB_EP_CTRL	USB_EP_DEF(0,  "ep0", 0, CONTROL, EP0_FIFO_SIZE, \
2618c2ecf20Sopenharmony_ci				USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci#define PXA_EP_DEF(_idx, _addr, dir, _type, maxpkt, _config, iface, altset) \
2648c2ecf20Sopenharmony_ci{ \
2658c2ecf20Sopenharmony_ci	.dev = &memory, \
2668c2ecf20Sopenharmony_ci	.name = "ep" #_idx, \
2678c2ecf20Sopenharmony_ci	.idx = _idx, .enabled = 0, \
2688c2ecf20Sopenharmony_ci	.dir_in = dir, .addr = _addr, \
2698c2ecf20Sopenharmony_ci	.config = _config, .interface = iface, .alternate = altset, \
2708c2ecf20Sopenharmony_ci	.type = _type, .fifo_size = maxpkt, \
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci#define PXA_EP_BULK(_idx, addr, dir, config, iface, alt) \
2738c2ecf20Sopenharmony_ci  PXA_EP_DEF(_idx, addr, dir, USB_ENDPOINT_XFER_BULK, BULK_FIFO_SIZE, \
2748c2ecf20Sopenharmony_ci		config, iface, alt)
2758c2ecf20Sopenharmony_ci#define PXA_EP_ISO(_idx, addr, dir, config, iface, alt) \
2768c2ecf20Sopenharmony_ci  PXA_EP_DEF(_idx, addr, dir, USB_ENDPOINT_XFER_ISOC, ISO_FIFO_SIZE, \
2778c2ecf20Sopenharmony_ci		config, iface, alt)
2788c2ecf20Sopenharmony_ci#define PXA_EP_INT(_idx, addr, dir, config, iface, alt) \
2798c2ecf20Sopenharmony_ci  PXA_EP_DEF(_idx, addr, dir, USB_ENDPOINT_XFER_INT, INT_FIFO_SIZE, \
2808c2ecf20Sopenharmony_ci		config, iface, alt)
2818c2ecf20Sopenharmony_ci#define PXA_EP_IN_BULK(i, adr, c, f, a)		PXA_EP_BULK(i, adr, 1, c, f, a)
2828c2ecf20Sopenharmony_ci#define PXA_EP_OUT_BULK(i, adr, c, f, a)	PXA_EP_BULK(i, adr, 0, c, f, a)
2838c2ecf20Sopenharmony_ci#define PXA_EP_IN_ISO(i, adr, c, f, a)		PXA_EP_ISO(i, adr, 1, c, f, a)
2848c2ecf20Sopenharmony_ci#define PXA_EP_OUT_ISO(i, adr, c, f, a)		PXA_EP_ISO(i, adr, 0, c, f, a)
2858c2ecf20Sopenharmony_ci#define PXA_EP_IN_INT(i, adr, c, f, a)		PXA_EP_INT(i, adr, 1, c, f, a)
2868c2ecf20Sopenharmony_ci#define PXA_EP_CTRL	PXA_EP_DEF(0, 0, 0, 0, EP0_FIFO_SIZE, 0, 0, 0)
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistruct pxa27x_udc;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistruct stats {
2918c2ecf20Sopenharmony_ci	unsigned long in_ops;
2928c2ecf20Sopenharmony_ci	unsigned long out_ops;
2938c2ecf20Sopenharmony_ci	unsigned long in_bytes;
2948c2ecf20Sopenharmony_ci	unsigned long out_bytes;
2958c2ecf20Sopenharmony_ci	unsigned long irqs;
2968c2ecf20Sopenharmony_ci};
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci/**
2998c2ecf20Sopenharmony_ci * struct udc_usb_ep - container of each usb_ep structure
3008c2ecf20Sopenharmony_ci * @usb_ep: usb endpoint
3018c2ecf20Sopenharmony_ci * @desc: usb descriptor, especially type and address
3028c2ecf20Sopenharmony_ci * @dev: udc managing this endpoint
3038c2ecf20Sopenharmony_ci * @pxa_ep: matching pxa_ep (cache of find_pxa_ep() call)
3048c2ecf20Sopenharmony_ci */
3058c2ecf20Sopenharmony_cistruct udc_usb_ep {
3068c2ecf20Sopenharmony_ci	struct usb_ep usb_ep;
3078c2ecf20Sopenharmony_ci	struct usb_endpoint_descriptor desc;
3088c2ecf20Sopenharmony_ci	struct pxa_udc *dev;
3098c2ecf20Sopenharmony_ci	struct pxa_ep *pxa_ep;
3108c2ecf20Sopenharmony_ci};
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci/**
3138c2ecf20Sopenharmony_ci * struct pxa_ep - pxa endpoint
3148c2ecf20Sopenharmony_ci * @dev: udc device
3158c2ecf20Sopenharmony_ci * @queue: requests queue
3168c2ecf20Sopenharmony_ci * @lock: lock to pxa_ep data (queues and stats)
3178c2ecf20Sopenharmony_ci * @enabled: true when endpoint enabled (not stopped by gadget layer)
3188c2ecf20Sopenharmony_ci * @in_handle_ep: number of recursions of handle_ep() function
3198c2ecf20Sopenharmony_ci * 	Prevents deadlocks or infinite recursions of types :
3208c2ecf20Sopenharmony_ci *	  irq->handle_ep()->req_done()->req.complete()->pxa_ep_queue()->handle_ep()
3218c2ecf20Sopenharmony_ci *      or
3228c2ecf20Sopenharmony_ci *        pxa_ep_queue()->handle_ep()->req_done()->req.complete()->pxa_ep_queue()
3238c2ecf20Sopenharmony_ci * @idx: endpoint index (1 => epA, 2 => epB, ..., 24 => epX)
3248c2ecf20Sopenharmony_ci * @name: endpoint name (for trace/debug purpose)
3258c2ecf20Sopenharmony_ci * @dir_in: 1 if IN endpoint, 0 if OUT endpoint
3268c2ecf20Sopenharmony_ci * @addr: usb endpoint number
3278c2ecf20Sopenharmony_ci * @config: configuration in which this endpoint is active
3288c2ecf20Sopenharmony_ci * @interface: interface in which this endpoint is active
3298c2ecf20Sopenharmony_ci * @alternate: altsetting in which this endpoitn is active
3308c2ecf20Sopenharmony_ci * @fifo_size: max packet size in the endpoint fifo
3318c2ecf20Sopenharmony_ci * @type: endpoint type (bulk, iso, int, ...)
3328c2ecf20Sopenharmony_ci * @udccsr_value: save register of UDCCSR0 for suspend/resume
3338c2ecf20Sopenharmony_ci * @udccr_value: save register of UDCCR for suspend/resume
3348c2ecf20Sopenharmony_ci * @stats: endpoint statistics
3358c2ecf20Sopenharmony_ci *
3368c2ecf20Sopenharmony_ci * The *PROBLEM* is that pxa's endpoint configuration scheme is both misdesigned
3378c2ecf20Sopenharmony_ci * (cares about config/interface/altsetting, thus placing needless limits on
3388c2ecf20Sopenharmony_ci * device capability) and full of implementation bugs forcing it to be set up
3398c2ecf20Sopenharmony_ci * for use more or less like a pxa255.
3408c2ecf20Sopenharmony_ci *
3418c2ecf20Sopenharmony_ci * As we define the pxa_ep statically, we must guess all needed pxa_ep for all
3428c2ecf20Sopenharmony_ci * gadget which may work with this udc driver.
3438c2ecf20Sopenharmony_ci */
3448c2ecf20Sopenharmony_cistruct pxa_ep {
3458c2ecf20Sopenharmony_ci	struct pxa_udc		*dev;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	struct list_head	queue;
3488c2ecf20Sopenharmony_ci	spinlock_t		lock;		/* Protects this structure */
3498c2ecf20Sopenharmony_ci						/* (queues, stats) */
3508c2ecf20Sopenharmony_ci	unsigned		enabled:1;
3518c2ecf20Sopenharmony_ci	unsigned		in_handle_ep:1;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	unsigned		idx:5;
3548c2ecf20Sopenharmony_ci	char			*name;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	/*
3578c2ecf20Sopenharmony_ci	 * Specific pxa endpoint data, needed for hardware initialization
3588c2ecf20Sopenharmony_ci	 */
3598c2ecf20Sopenharmony_ci	unsigned		dir_in:1;
3608c2ecf20Sopenharmony_ci	unsigned		addr:4;
3618c2ecf20Sopenharmony_ci	unsigned		config:2;
3628c2ecf20Sopenharmony_ci	unsigned		interface:3;
3638c2ecf20Sopenharmony_ci	unsigned		alternate:3;
3648c2ecf20Sopenharmony_ci	unsigned		fifo_size;
3658c2ecf20Sopenharmony_ci	unsigned		type;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
3688c2ecf20Sopenharmony_ci	u32			udccsr_value;
3698c2ecf20Sopenharmony_ci	u32			udccr_value;
3708c2ecf20Sopenharmony_ci#endif
3718c2ecf20Sopenharmony_ci	struct stats		stats;
3728c2ecf20Sopenharmony_ci};
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci/**
3758c2ecf20Sopenharmony_ci * struct pxa27x_request - container of each usb_request structure
3768c2ecf20Sopenharmony_ci * @req: usb request
3778c2ecf20Sopenharmony_ci * @udc_usb_ep: usb endpoint the request was submitted on
3788c2ecf20Sopenharmony_ci * @in_use: sanity check if request already queued on an pxa_ep
3798c2ecf20Sopenharmony_ci * @queue: linked list of requests, linked on pxa_ep->queue
3808c2ecf20Sopenharmony_ci */
3818c2ecf20Sopenharmony_cistruct pxa27x_request {
3828c2ecf20Sopenharmony_ci	struct usb_request			req;
3838c2ecf20Sopenharmony_ci	struct udc_usb_ep			*udc_usb_ep;
3848c2ecf20Sopenharmony_ci	unsigned				in_use:1;
3858c2ecf20Sopenharmony_ci	struct list_head			queue;
3868c2ecf20Sopenharmony_ci};
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cienum ep0_state {
3898c2ecf20Sopenharmony_ci	WAIT_FOR_SETUP,
3908c2ecf20Sopenharmony_ci	SETUP_STAGE,
3918c2ecf20Sopenharmony_ci	IN_DATA_STAGE,
3928c2ecf20Sopenharmony_ci	OUT_DATA_STAGE,
3938c2ecf20Sopenharmony_ci	IN_STATUS_STAGE,
3948c2ecf20Sopenharmony_ci	OUT_STATUS_STAGE,
3958c2ecf20Sopenharmony_ci	STALL,
3968c2ecf20Sopenharmony_ci	WAIT_ACK_SET_CONF_INTERF
3978c2ecf20Sopenharmony_ci};
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_cistatic char *ep0_state_name[] = {
4008c2ecf20Sopenharmony_ci	"WAIT_FOR_SETUP", "SETUP_STAGE", "IN_DATA_STAGE", "OUT_DATA_STAGE",
4018c2ecf20Sopenharmony_ci	"IN_STATUS_STAGE", "OUT_STATUS_STAGE", "STALL",
4028c2ecf20Sopenharmony_ci	"WAIT_ACK_SET_CONF_INTERF"
4038c2ecf20Sopenharmony_ci};
4048c2ecf20Sopenharmony_ci#define EP0_STNAME(udc) ep0_state_name[(udc)->ep0state]
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci#define EP0_FIFO_SIZE	16U
4078c2ecf20Sopenharmony_ci#define BULK_FIFO_SIZE	64U
4088c2ecf20Sopenharmony_ci#define ISO_FIFO_SIZE	256U
4098c2ecf20Sopenharmony_ci#define INT_FIFO_SIZE	16U
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistruct udc_stats {
4128c2ecf20Sopenharmony_ci	unsigned long	irqs_reset;
4138c2ecf20Sopenharmony_ci	unsigned long	irqs_suspend;
4148c2ecf20Sopenharmony_ci	unsigned long	irqs_resume;
4158c2ecf20Sopenharmony_ci	unsigned long	irqs_reconfig;
4168c2ecf20Sopenharmony_ci};
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci#define NR_USB_ENDPOINTS (1 + 5)	/* ep0 + ep1in-bulk + .. + ep3in-iso */
4198c2ecf20Sopenharmony_ci#define NR_PXA_ENDPOINTS (1 + 14)	/* ep0 + epA + epB + .. + epX */
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci/**
4228c2ecf20Sopenharmony_ci * struct pxa_udc - udc structure
4238c2ecf20Sopenharmony_ci * @regs: mapped IO space
4248c2ecf20Sopenharmony_ci * @irq: udc irq
4258c2ecf20Sopenharmony_ci * @clk: udc clock
4268c2ecf20Sopenharmony_ci * @usb_gadget: udc gadget structure
4278c2ecf20Sopenharmony_ci * @driver: bound gadget (zero, g_ether, g_mass_storage, ...)
4288c2ecf20Sopenharmony_ci * @dev: device
4298c2ecf20Sopenharmony_ci * @udc_command: machine specific function to activate D+ pullup
4308c2ecf20Sopenharmony_ci * @gpiod: gpio descriptor of gpio for D+ pullup (or NULL if none)
4318c2ecf20Sopenharmony_ci * @transceiver: external transceiver to handle vbus sense and D+ pullup
4328c2ecf20Sopenharmony_ci * @ep0state: control endpoint state machine state
4338c2ecf20Sopenharmony_ci * @stats: statistics on udc usage
4348c2ecf20Sopenharmony_ci * @udc_usb_ep: array of usb endpoints offered by the gadget
4358c2ecf20Sopenharmony_ci * @pxa_ep: array of pxa available endpoints
4368c2ecf20Sopenharmony_ci * @enabled: UDC was enabled by a previous udc_enable()
4378c2ecf20Sopenharmony_ci * @pullup_on: if pullup resistor connected to D+ pin
4388c2ecf20Sopenharmony_ci * @pullup_resume: if pullup resistor should be connected to D+ pin on resume
4398c2ecf20Sopenharmony_ci * @config: UDC active configuration
4408c2ecf20Sopenharmony_ci * @last_interface: UDC interface of the last SET_INTERFACE host request
4418c2ecf20Sopenharmony_ci * @last_alternate: UDC altsetting of the last SET_INTERFACE host request
4428c2ecf20Sopenharmony_ci * @udccsr0: save of udccsr0 in case of suspend
4438c2ecf20Sopenharmony_ci * @debugfs_root: root entry of debug filesystem
4448c2ecf20Sopenharmony_ci * @debugfs_state: debugfs entry for "udcstate"
4458c2ecf20Sopenharmony_ci * @debugfs_queues: debugfs entry for "queues"
4468c2ecf20Sopenharmony_ci * @debugfs_eps: debugfs entry for "epstate"
4478c2ecf20Sopenharmony_ci */
4488c2ecf20Sopenharmony_cistruct pxa_udc {
4498c2ecf20Sopenharmony_ci	void __iomem				*regs;
4508c2ecf20Sopenharmony_ci	int					irq;
4518c2ecf20Sopenharmony_ci	struct clk				*clk;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	struct usb_gadget			gadget;
4548c2ecf20Sopenharmony_ci	struct usb_gadget_driver		*driver;
4558c2ecf20Sopenharmony_ci	struct device				*dev;
4568c2ecf20Sopenharmony_ci	void					(*udc_command)(int);
4578c2ecf20Sopenharmony_ci	struct gpio_desc			*gpiod;
4588c2ecf20Sopenharmony_ci	struct usb_phy				*transceiver;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	enum ep0_state				ep0state;
4618c2ecf20Sopenharmony_ci	struct udc_stats			stats;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	struct udc_usb_ep			udc_usb_ep[NR_USB_ENDPOINTS];
4648c2ecf20Sopenharmony_ci	struct pxa_ep				pxa_ep[NR_PXA_ENDPOINTS];
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	unsigned				enabled:1;
4678c2ecf20Sopenharmony_ci	unsigned				pullup_on:1;
4688c2ecf20Sopenharmony_ci	unsigned				pullup_resume:1;
4698c2ecf20Sopenharmony_ci	unsigned				vbus_sensed:1;
4708c2ecf20Sopenharmony_ci	unsigned				config:2;
4718c2ecf20Sopenharmony_ci	unsigned				last_interface:3;
4728c2ecf20Sopenharmony_ci	unsigned				last_alternate:3;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
4758c2ecf20Sopenharmony_ci	unsigned				udccsr0;
4768c2ecf20Sopenharmony_ci#endif
4778c2ecf20Sopenharmony_ci#ifdef CONFIG_USB_GADGET_DEBUG_FS
4788c2ecf20Sopenharmony_ci	struct dentry				*debugfs_root;
4798c2ecf20Sopenharmony_ci#endif
4808c2ecf20Sopenharmony_ci};
4818c2ecf20Sopenharmony_ci#define to_pxa(g)	(container_of((g), struct pxa_udc, gadget))
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_cistatic inline struct pxa_udc *to_gadget_udc(struct usb_gadget *gadget)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	return container_of(gadget, struct pxa_udc, gadget);
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci/*
4898c2ecf20Sopenharmony_ci * Debugging/message support
4908c2ecf20Sopenharmony_ci */
4918c2ecf20Sopenharmony_ci#define ep_dbg(ep, fmt, arg...) \
4928c2ecf20Sopenharmony_ci	dev_dbg(ep->dev->dev, "%s:%s: " fmt, EPNAME(ep), __func__, ## arg)
4938c2ecf20Sopenharmony_ci#define ep_vdbg(ep, fmt, arg...) \
4948c2ecf20Sopenharmony_ci	dev_vdbg(ep->dev->dev, "%s:%s: " fmt, EPNAME(ep), __func__, ## arg)
4958c2ecf20Sopenharmony_ci#define ep_err(ep, fmt, arg...) \
4968c2ecf20Sopenharmony_ci	dev_err(ep->dev->dev, "%s:%s: " fmt, EPNAME(ep), __func__, ## arg)
4978c2ecf20Sopenharmony_ci#define ep_info(ep, fmt, arg...) \
4988c2ecf20Sopenharmony_ci	dev_info(ep->dev->dev, "%s:%s: " fmt, EPNAME(ep), __func__, ## arg)
4998c2ecf20Sopenharmony_ci#define ep_warn(ep, fmt, arg...) \
5008c2ecf20Sopenharmony_ci	dev_warn(ep->dev->dev, "%s:%s:" fmt, EPNAME(ep), __func__, ## arg)
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci#endif /* __LINUX_USB_GADGET_PXA27X_H */
503