Re: [PATCH 2/2] USB: pxa: Add USB client support for Marvell PXA9xx/PXA168 chips

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Nov 22, 2010 at 5:32 PM,  <chao.xie@xxxxxxxxxxx> wrote:
> From: cxie4 <cxie4@xxxxxxxxxxx>
>
> This patch add USB client support Marvell PXA9xx/PXA168 chips. The USB
> controller in PXA9xx/PXA168 is a High-Speed OTG controller. The available
> endpoints is different between PXA9xx and PXA168.
>
> NOTE:
> It is the first version of Marvell PXA9xx/PXA168 USB controller driver.
> The support for OTG mode will be added in later patch.
> PXA9xx and PXA168 has integrated UTMI PHY in the chips. The initialization
> for the PHY is a little different between PXA9xx and PXA168.
>
> Signed-off-by: Chao Xie <chao.xie@xxxxxxxxxxx>

This is indeed a big driver. I believe the code can be simplified a bit
further, and the mv_udc_phy_init() can be merged into the driver itself,
the difference can be decided by looking up its 'struct platform_device_id'.
(e.g. drivers/i2c/busses/i2c-pxa.c)

The code quality looks acceptable to me, so

Acked-by: Eric Miao <eric.y.miao@xxxxxxxxx>

> ---
> Âdrivers/usb/gadget/Kconfig    |  13 +
> Âdrivers/usb/gadget/Makefile   Â|  Â2 +
> Âdrivers/usb/gadget/mv_udc.h   Â| Â293 ++++++
> Âdrivers/usb/gadget/mv_udc_core.c | 2122 ++++++++++++++++++++++++++++++++++++++
> Âdrivers/usb/gadget/mv_udc_phy.c Â| Â212 ++++
> Â5 files changed, 2642 insertions(+), 0 deletions(-)
> Âcreate mode 100644 drivers/usb/gadget/mv_udc.h
> Âcreate mode 100644 drivers/usb/gadget/mv_udc_core.c
> Âcreate mode 100644 drivers/usb/gadget/mv_udc_phy.c
>
> diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
> index 607d0db..3dd2573 100644
> --- a/drivers/usb/gadget/Kconfig
> +++ b/drivers/usb/gadget/Kconfig
> @@ -338,6 +338,19 @@ config USB_S3C2410_DEBUG
> Â Â Â Âboolean "S3C2410 udc debug messages"
> Â Â Â Âdepends on USB_GADGET_S3C2410
>
> +config USB_GADGET_PXA_U2O
> + Â Â Â boolean "PXA9xx Processor USB2.0 controller"
> + Â Â Â select USB_GADGET_DUALSPEED
> + Â Â Â help
> + Â Â Â Â PXA9xx Processor series include a high speed USB2.0 device
> + Â Â Â Â controller, which support high speed and full speed USB peripheral.
> +
> +config USB_PXA_U2O
> + Â Â Â tristate
> + Â Â Â depends on USB_GADGET_PXA_U2O
> + Â Â Â default USB_GADGET
> + Â Â Â select USB_GADGET_SELECTED
> +
> Â#
> Â# Controllers available in both integrated and discrete versions
> Â#
> diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
> index 5780db4..e2ad3fa 100644
> --- a/drivers/usb/gadget/Makefile
> +++ b/drivers/usb/gadget/Makefile
> @@ -24,6 +24,8 @@ obj-$(CONFIG_USB_FSL_QE) Â Â Â+= fsl_qe_udc.o
> Âobj-$(CONFIG_USB_CI13XXX) Â Â Â+= ci13xxx_udc.o
> Âobj-$(CONFIG_USB_S3C_HSOTG) Â Â+= s3c-hsotg.o
> Âobj-$(CONFIG_USB_LANGWELL) Â Â += langwell_udc.o
> +obj-$(CONFIG_USB_PXA_U2O) Â Â Â+= mv_udc.o
> +mv_udc-y            := mv_udc_core.o mv_udc_phy.o
>
> Â#
> Â# USB gadget drivers
> diff --git a/drivers/usb/gadget/mv_udc.h b/drivers/usb/gadget/mv_udc.h
> new file mode 100644
> index 0000000..43417f1
> --- /dev/null
> +++ b/drivers/usb/gadget/mv_udc.h
> @@ -0,0 +1,293 @@
> +
> +#ifndef __MV_UDC_H
> +#define __MV_UDC_H
> +
> +#define VUSBHS_MAX_PORTS Â Â Â 8
> +
> +#define DQH_ALIGNMENT Â Â Â Â Â2048
> +#define DTD_ALIGNMENT Â Â Â Â Â64
> +#define DMA_BOUNDARY Â Â Â Â Â 4096
> +
> +#define EP_DIR_IN Â Â Â1
> +#define EP_DIR_OUT Â Â 0
> +
> +#define DMA_ADDR_INVALID Â Â Â (~(dma_addr_t)0)
> +
> +#define EP0_MAX_PKT_SIZE Â Â Â 64
> +/* ep0 transfer state */
> +#define WAIT_FOR_SETUP Â Â Â Â 0
> +#define DATA_STATE_XMIT Â Â Â Â Â Â Â Â1
> +#define DATA_STATE_NEED_ZLP Â Â2
> +#define WAIT_FOR_OUT_STATUS Â Â3
> +#define DATA_STATE_RECV Â Â Â Â Â Â Â Â4
> +
> +#define CAPLENGTH_MASK Â Â Â Â (0xff)
> +#define DCCPARAMS_DEN_MASK Â Â (0x1f)
> +
> +#define HCSPARAMS_PPC Â Â Â Â Â(0x10)
> +
> +/* Frame Index Register Bit Masks */
> +#define USB_FRINDEX_MASKS Â Â Â0x3fff
> +
> +/* Command Register Bit Masks */
> +#define USBCMD_RUN_STOP Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â(0x00000001)
> +#define USBCMD_CTRL_RESET Â Â Â Â Â Â Â Â Â Â Â(0x00000002)
> +#define USBCMD_SETUP_TRIPWIRE_SET Â Â Â Â Â Â Â(0x00002000)
> +#define USBCMD_SETUP_TRIPWIRE_CLEAR Â Â Â Â Â Â(~USBCMD_SETUP_TRIPWIRE_SET)
> +
> +#define USBCMD_ATDTW_TRIPWIRE_SET Â Â Â Â Â Â Â(0x00004000)
> +#define USBCMD_ATDTW_TRIPWIRE_CLEAR Â Â Â Â Â Â(~USBCMD_ATDTW_TRIPWIRE_SET)
> +
> +/*bit 15,3,2 are for frame list size */
> +#define USBCMD_FRAME_SIZE_1024 Â Â Â Â Â Â Â Â (0x00000000) /* 000 */
> +#define USBCMD_FRAME_SIZE_512 Â Â Â Â Â Â Â Â Â(0x00000004) /* 001 */
> +#define USBCMD_FRAME_SIZE_256 Â Â Â Â Â Â Â Â Â(0x00000008) /* 010 */
> +#define USBCMD_FRAME_SIZE_128 Â Â Â Â Â Â Â Â Â(0x0000000C) /* 011 */
> +#define USBCMD_FRAME_SIZE_64 Â Â Â Â Â Â Â Â Â (0x00008000) /* 100 */
> +#define USBCMD_FRAME_SIZE_32 Â Â Â Â Â Â Â Â Â (0x00008004) /* 101 */
> +#define USBCMD_FRAME_SIZE_16 Â Â Â Â Â Â Â Â Â (0x00008008) /* 110 */
> +#define USBCMD_FRAME_SIZE_8 Â Â Â Â Â Â Â Â Â Â(0x0000800C) /* 111 */
> +
> +#define EPCTRL_TX_ALL_MASK Â Â Â Â Â Â Â Â Â Â (0xFFFF0000)
> +#define EPCTRL_RX_ALL_MASK Â Â Â Â Â Â Â Â Â Â (0x0000FFFF)
> +
> +#define EPCTRL_TX_DATA_TOGGLE_RST Â Â Â Â Â Â Â(0x00400000)
> +#define EPCTRL_TX_EP_STALL Â Â Â Â Â Â Â Â Â Â (0x00010000)
> +#define EPCTRL_RX_EP_STALL Â Â Â Â Â Â Â Â Â Â (0x00000001)
> +#define EPCTRL_RX_DATA_TOGGLE_RST Â Â Â Â Â Â Â(0x00000040)
> +#define EPCTRL_RX_ENABLE Â Â Â Â Â Â Â Â Â Â Â (0x00000080)
> +#define EPCTRL_TX_ENABLE Â Â Â Â Â Â Â Â Â Â Â (0x00800000)
> +#define EPCTRL_CONTROL Â Â Â Â Â Â Â Â Â Â Â Â (0x00000000)
> +#define EPCTRL_ISOCHRONOUS Â Â Â Â Â Â Â Â Â Â (0x00040000)
> +#define EPCTRL_BULK Â Â Â Â Â Â Â Â Â Â Â Â Â Â(0x00080000)
> +#define EPCTRL_INT Â Â Â Â Â Â Â Â Â Â Â Â Â Â (0x000C0000)
> +#define EPCTRL_TX_TYPE Â Â Â Â Â Â Â Â Â Â Â Â (0x000C0000)
> +#define EPCTRL_RX_TYPE Â Â Â Â Â Â Â Â Â Â Â Â (0x0000000C)
> +#define EPCTRL_DATA_TOGGLE_INHIBIT Â Â Â Â Â Â (0x00000020)
> +#define EPCTRL_TX_EP_TYPE_SHIFT Â Â Â Â Â Â Â Â Â Â Â Â(18)
> +#define EPCTRL_RX_EP_TYPE_SHIFT Â Â Â Â Â Â Â Â Â Â Â Â(2)
> +
> +#define EPCOMPLETE_MAX_ENDPOINTS Â Â Â Â Â Â Â (16)
> +
> +/* endpoint list address bit masks */
> +#define USB_EP_LIST_ADDRESS_MASK Â Â Â Â Â Â Â0xfffff800
> +
> +#define PORTSCX_W1C_BITS Â Â Â Â Â Â Â Â Â Â Â 0x2a
> +#define PORTSCX_PORT_RESET Â Â Â Â Â Â Â Â Â Â 0x00000100
> +#define PORTSCX_PORT_POWER Â Â Â Â Â Â Â Â Â Â 0x00001000
> +#define PORTSCX_FORCE_FULL_SPEED_CONNECT Â Â Â 0x01000000
> +#define PORTSCX_PAR_XCVR_SELECT Â Â Â Â Â Â Â Â Â Â Â Â0xC0000000
> +#define PORTSCX_PORT_FORCE_RESUME Â Â Â Â Â Â Â0x00000040
> +#define PORTSCX_PORT_SUSPEND Â Â Â Â Â Â Â Â Â 0x00000080
> +#define PORTSCX_PORT_SPEED_FULL Â Â Â Â Â Â Â Â Â Â Â Â0x00000000
> +#define PORTSCX_PORT_SPEED_LOW Â Â Â Â Â Â Â Â 0x04000000
> +#define PORTSCX_PORT_SPEED_HIGH Â Â Â Â Â Â Â Â Â Â Â Â0x08000000
> +#define PORTSCX_PORT_SPEED_MASK Â Â Â Â Â Â Â Â Â Â Â Â0x0C000000
> +
> +/* USB MODE Register Bit Masks */
> +#define USBMODE_CTRL_MODE_IDLE Â Â Â Â Â Â Â Â 0x00000000
> +#define USBMODE_CTRL_MODE_DEVICE Â Â Â Â Â Â Â 0x00000002
> +#define USBMODE_CTRL_MODE_HOST Â Â Â Â Â Â Â Â 0x00000003
> +#define USBMODE_CTRL_MODE_RSV Â Â Â Â Â Â Â Â Â0x00000001
> +#define USBMODE_SETUP_LOCK_OFF Â Â Â Â Â Â Â Â 0x00000008
> +#define USBMODE_STREAM_DISABLE Â Â Â Â Â Â Â Â 0x00000010
> +
> +/* USB STS Register Bit Masks */
> +#define USBSTS_INT Â Â Â Â Â Â Â Â Â Â 0x00000001
> +#define USBSTS_ERR Â Â Â Â Â Â Â Â Â Â 0x00000002
> +#define USBSTS_PORT_CHANGE Â Â Â Â Â Â 0x00000004
> +#define USBSTS_FRM_LST_ROLL Â Â Â Â Â Â0x00000008
> +#define USBSTS_SYS_ERR Â Â Â Â Â Â Â Â 0x00000010
> +#define USBSTS_IAA Â Â Â Â Â Â Â Â Â Â 0x00000020
> +#define USBSTS_RESET Â Â Â Â Â Â Â Â Â 0x00000040
> +#define USBSTS_SOF Â Â Â Â Â Â Â Â Â Â 0x00000080
> +#define USBSTS_SUSPEND Â Â Â Â Â Â Â Â 0x00000100
> +#define USBSTS_HC_HALTED Â Â Â Â Â Â Â 0x00001000
> +#define USBSTS_RCL Â Â Â Â Â Â Â Â Â Â 0x00002000
> +#define USBSTS_PERIODIC_SCHEDULE Â Â Â 0x00004000
> +#define USBSTS_ASYNC_SCHEDULE Â Â Â Â Â0x00008000
> +
> +
> +/* Interrupt Enable Register Bit Masks */
> +#define USBINTR_INT_EN Â Â Â Â Â Â Â Â Â Â Â Â Â(0x00000001)
> +#define USBINTR_ERR_INT_EN Â Â Â Â Â Â Â Â Â Â Â(0x00000002)
> +#define USBINTR_PORT_CHANGE_DETECT_EN Â Â Â Â Â (0x00000004)
> +
> +#define USBINTR_ASYNC_ADV_AAE Â Â Â Â Â Â Â Â Â (0x00000020)
> +#define USBINTR_ASYNC_ADV_AAE_ENABLE Â Â Â Â Â Â(0x00000020)
> +#define USBINTR_ASYNC_ADV_AAE_DISABLE Â Â Â Â Â (0xFFFFFFDF)
> +
> +#define USBINTR_RESET_EN Â Â Â Â Â Â Â Â Â Â Â Â(0x00000040)
> +#define USBINTR_SOF_UFRAME_EN Â Â Â Â Â Â Â Â Â (0x00000080)
> +#define USBINTR_DEVICE_SUSPEND Â Â Â Â Â Â Â Â Â(0x00000100)
> +
> +#define USB_DEVICE_ADDRESS_MASK Â Â Â Â Â Â Â Â Â Â Â Â(0xfe000000)
> +#define USB_DEVICE_ADDRESS_BIT_SHIFT Â Â Â Â Â (25)
> +
> +struct mv_cap_regs {
> + Â Â Â u32 Â Â caplength_hciversion;
> + Â Â Â u32 Â Â hcsparams; Â Â Â/* HC structural parameters */
> + Â Â Â u32 Â Â hccparams; Â Â Â/* HC Capability Parameters*/
> + Â Â Â u32 Â Â reserved[5];
> + Â Â Â u32 Â Â dciversion; Â Â /* DC version number and reserved 16 bits */
> + Â Â Â u32 Â Â dccparams; Â Â Â/* DC Capability Parameters */
> +};
> +
> +struct mv_op_regs {
> + Â Â Â u32 Â Â usbcmd; Â Â Â Â /* Command register */
> + Â Â Â u32 Â Â usbsts; Â Â Â Â /* Status register */
> + Â Â Â u32 Â Â usbintr; Â Â Â Â/* Interrupt enable */
> + Â Â Â u32 Â Â frindex; Â Â Â Â/* Frame index */
> + Â Â Â u32 Â Â reserved1[1];
> + Â Â Â u32 Â Â deviceaddr; Â Â /* Device Address */
> + Â Â Â u32 Â Â eplistaddr; Â Â /* Endpoint List Address */
> + Â Â Â u32 Â Â ttctrl; Â Â Â Â /* HOST TT status and control */
> + Â Â Â u32 Â Â burstsize; Â Â Â/* Programmable Burst Size */
> + Â Â Â u32 Â Â txfilltuning; Â /* Host Transmit Pre-Buffer Packet Tuning */
> + Â Â Â u32 Â Â reserved[4];
> + Â Â Â u32 Â Â epnak; Â Â Â Â Â/* Endpoint NAK */
> + Â Â Â u32 Â Â epnaken; Â Â Â Â/* Endpoint NAK Enable */
> + Â Â Â u32 Â Â configflag; Â Â /* Configured Flag register */
> + Â Â Â u32 Â Â portsc[VUSBHS_MAX_PORTS]; /* Port Status/Control x, x = 1..8 */
> + Â Â Â u32 Â Â otgsc;
> + Â Â Â u32 Â Â usbmode; Â Â Â Â/* USB Host/Device mode */
> + Â Â Â u32 Â Â epsetupstat; Â Â/* Endpoint Setup Status */
> + Â Â Â u32 Â Â epprime; Â Â Â Â/* Endpoint Initialize */
> + Â Â Â u32 Â Â epflush; Â Â Â Â/* Endpoint De-initialize */
> + Â Â Â u32 Â Â epstatus; Â Â Â /* Endpoint Status */
> + Â Â Â u32 Â Â epcomplete; Â Â /* Endpoint Interrupt On Complete */
> + Â Â Â u32 Â Â epctrlx[16]; Â Â/* Endpoint Control, where x = 0.. 15 */
> + Â Â Â u32 Â Â mcr; Â Â Â Â Â Â/* Mux Control */
> + Â Â Â u32 Â Â isr; Â Â Â Â Â Â/* Interrupt Status */
> + Â Â Â u32 Â Â ier; Â Â Â Â Â Â/* Interrupt Enable */
> +};
> +
> +struct mv_udc {
> +    struct usb_gadget        gadget;
> +    struct usb_gadget_driver    Â*driver;
> +    spinlock_t           Âlock;
> +    struct completion        *done;
> +    struct platform_device     Â*dev;
> +    int               irq;
> +
> +    struct mv_cap_regs __iomem   Â*cap_regs;
> +    struct mv_op_regs __iomem    *op_regs;
> +    unsigned int          Âphy_regs;
> +    unsigned int          Âmax_eps;
> +    struct mv_dqh          *ep_dqh;
> +    size_t             Âep_dqh_size;
> +    dma_addr_t           Âep_dqh_dma;
> +
> +    struct dma_pool         *dtd_pool;
> +    struct mv_ep          Â*eps;
> +
> +    struct mv_dtd          *dtd_head;
> +    struct mv_dtd          *dtd_tail;
> +    unsigned int          Âdtd_entries;
> +
> +    struct mv_req          *status_req;
> +    struct usb_ctrlrequest     Âlocal_setup_buff;
> +
> +    unsigned int      Âresume_state;  /* USB state to resume */
> +    unsigned int      Âusb_state;   Â/* USB current state */
> +    unsigned int      Âep0_state;   Â/* Endpoint zero state */
> +    unsigned int      Âep0_dir;
> +
> +    unsigned int      Âdev_addr;
> +
> +    int           errors;
> +    unsigned        Âsoftconnect:1,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â vbus_active:1,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â remote_wakeup:1,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â softconnected:1,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â force_fs:1;
> +    struct clk       Â*clk;
> +};
> +
> +/* endpoint data structure */
> +struct mv_ep {
> +    struct usb_ep      ep;
> +    struct mv_udc      *udc;
> +    struct list_head    Âqueue;
> +    struct mv_dqh      *dqh;
> +    const struct usb_endpoint_descriptor  Â*desc;
> + Â Â Â u32 Â Â Â Â Â Â Â Â Â Â direction;
> +    char          Âname[14];
> +    unsigned        Âstopped:1,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ep_type:2,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ep_num:8;
> +};
> +
> +/* request data structure */
> +struct mv_req {
> +    struct usb_request   Âreq;
> +    struct mv_dtd      *dtd, *head, *tail;
> +    struct mv_ep      Â*ep;
> +    struct list_head    Âqueue;
> +    unsigned        Âdtd_count;
> +    unsigned        Âmapped:1;
> +};
> +
> +#define EP_QUEUE_HEAD_MULT_POS Â Â Â Â Â Â Â Â 30
> +#define EP_QUEUE_HEAD_ZLT_SEL Â Â Â Â Â Â Â Â Â0x20000000
> +#define EP_QUEUE_HEAD_MAX_PKT_LEN_POS Â Â Â Â Â16
> +#define EP_QUEUE_HEAD_MAX_PKT_LEN(ep_info) Â Â (((ep_info)>>16)&0x07ff)
> +#define EP_QUEUE_HEAD_IOS Â Â Â Â Â Â Â Â Â Â Â0x00008000
> +#define EP_QUEUE_HEAD_NEXT_TERMINATE Â Â Â Â Â 0x00000001
> +#define EP_QUEUE_HEAD_IOC Â Â Â Â Â Â Â Â Â Â Â0x00008000
> +#define EP_QUEUE_HEAD_MULTO Â Â Â Â Â Â Â Â Â Â0x00000C00
> +#define EP_QUEUE_HEAD_STATUS_HALT Â Â Â Â Â Â Â0x00000040
> +#define EP_QUEUE_HEAD_STATUS_ACTIVE Â Â Â Â Â Â0x00000080
> +#define EP_QUEUE_CURRENT_OFFSET_MASK Â Â Â Â Â 0x00000FFF
> +#define EP_QUEUE_HEAD_NEXT_POINTER_MASK Â Â Â Â Â Â Â Â0xFFFFFFE0
> +#define EP_QUEUE_FRINDEX_MASK Â Â Â Â Â Â Â Â Â0x000007FF
> +#define EP_MAX_LENGTH_TRANSFER Â Â Â Â Â Â Â Â 0x4000
> +
> +struct mv_dqh {
> + Â Â Â /* Bits 16..26 Bit 15 is Interrupt On Setup */
> + Â Â Â u32 Â Â max_packet_length;
> + Â Â Â u32 Â Â curr_dtd_ptr; Â Â Â Â Â /* Current dTD Pointer */
> + Â Â Â u32 Â Â next_dtd_ptr; Â Â Â Â Â /* Next dTD Pointer */
> + Â Â Â /* Total bytes (16..30), IOC (15), INT (8), STS (0-7) */
> + Â Â Â u32 Â Â size_ioc_int_sts;
> + Â Â Â u32 Â Â buff_ptr0; Â Â Â Â Â Â Â/* Buffer pointer Page 0 (12-31) */
> + Â Â Â u32 Â Â buff_ptr1; Â Â Â Â Â Â Â/* Buffer pointer Page 1 (12-31) */
> + Â Â Â u32 Â Â buff_ptr2; Â Â Â Â Â Â Â/* Buffer pointer Page 2 (12-31) */
> + Â Â Â u32 Â Â buff_ptr3; Â Â Â Â Â Â Â/* Buffer pointer Page 3 (12-31) */
> + Â Â Â u32 Â Â buff_ptr4; Â Â Â Â Â Â Â/* Buffer pointer Page 4 (12-31) */
> + Â Â Â u32 Â Â reserved1;
> + Â Â Â /* 8 bytes of setup data that follows the Setup PID */
> + Â Â Â u8 Â Â Âsetup_buffer[8];
> + Â Â Â u32 Â Â reserved2[4];
> +};
> +
> +
> +#define DTD_NEXT_TERMINATE Â Â Â Â Â Â (0x00000001)
> +#define DTD_IOC Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â(0x00008000)
> +#define DTD_STATUS_ACTIVE Â Â Â Â Â Â Â(0x00000080)
> +#define DTD_STATUS_HALTED Â Â Â Â Â Â Â(0x00000040)
> +#define DTD_STATUS_DATA_BUFF_ERR Â Â Â (0x00000020)
> +#define DTD_STATUS_TRANSACTION_ERR Â Â (0x00000008)
> +#define DTD_RESERVED_FIELDS Â Â Â Â Â Â(0x00007F00)
> +#define DTD_ERROR_MASK Â Â Â Â Â Â Â Â (0x68)
> +#define DTD_ADDR_MASK Â Â Â Â Â Â Â Â Â(0xFFFFFFE0)
> +#define DTD_PACKET_SIZE Â Â Â Â Â Â Â Â Â Â Â Â0x7FFF0000
> +#define DTD_LENGTH_BIT_POS Â Â Â Â Â Â (16)
> +
> +struct mv_dtd {
> + Â Â Â u32 Â Â dtd_next;
> + Â Â Â u32 Â Â size_ioc_sts;
> + Â Â Â u32 Â Â buff_ptr0; Â Â Â Â Â Â Â/* Buffer pointer Page 0 */
> + Â Â Â u32 Â Â buff_ptr1; Â Â Â Â Â Â Â/* Buffer pointer Page 1 */
> + Â Â Â u32 Â Â buff_ptr2; Â Â Â Â Â Â Â/* Buffer pointer Page 2 */
> + Â Â Â u32 Â Â buff_ptr3; Â Â Â Â Â Â Â/* Buffer pointer Page 3 */
> + Â Â Â u32 Â Â buff_ptr4; Â Â Â Â Â Â Â/* Buffer pointer Page 4 */
> + Â Â Â u32 Â Â scratch_ptr;
> + Â Â Â /* 32 bytes */
> + Â Â Â dma_addr_t td_dma; Â Â Â Â Â Â Â/* dma address for this td */
> + Â Â Â struct mv_dtd *next_dtd_virt;
> +};
> +
> +extern int mv_udc_phy_init(unsigned int base);
> +
> +#endif
> diff --git a/drivers/usb/gadget/mv_udc_core.c b/drivers/usb/gadget/mv_udc_core.c
> new file mode 100644
> index 0000000..4b296dd
> --- /dev/null
> +++ b/drivers/usb/gadget/mv_udc_core.c
> @@ -0,0 +1,2122 @@
> +#include <linux/module.h>
> +#include <linux/pci.h>
> +#include <linux/dma-mapping.h>
> +#include <linux/dmapool.h>
> +#include <linux/kernel.h>
> +#include <linux/delay.h>
> +#include <linux/ioport.h>
> +#include <linux/sched.h>
> +#include <linux/slab.h>
> +#include <linux/errno.h>
> +#include <linux/init.h>
> +#include <linux/timer.h>
> +#include <linux/list.h>
> +#include <linux/interrupt.h>
> +#include <linux/moduleparam.h>
> +#include <linux/device.h>
> +#include <linux/usb/ch9.h>
> +#include <linux/usb/gadget.h>
> +#include <linux/usb/otg.h>
> +#include <linux/pm.h>
> +#include <linux/io.h>
> +#include <linux/irq.h>
> +#include <linux/platform_device.h>
> +#include <linux/clk.h>
> +#include <asm/system.h>
> +#include <asm/unaligned.h>
> +
> +#include "mv_udc.h"
> +
> +#define DRIVER_DESC Â Â Â Â Â Â"Marvell PXA USB Device Controller driver"
> +#define DRIVER_VERSION Â Â Â Â "8 Nov 2010"
> +
> +#define ep_dir(ep) Â Â (((ep)->ep_num == 0) ? \
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ((ep)->udc->ep0_dir) : ((ep)->direction))
> +
> +/* timeout value -- usec */
> +#define RESET_TIMEOUT Â Â Â Â Â10000
> +#define FLUSH_TIMEOUT Â Â Â Â Â10000
> +#define EPSTATUS_TIMEOUT Â Â Â 10000
> +#define PRIME_TIMEOUT Â Â Â Â Â10000
> +#define READSAFE_TIMEOUT Â Â Â 1000
> +#define DTD_TIMEOUT Â Â Â Â Â Â1000
> +
> +#define LOOPS_USEC_SHIFT Â Â Â 4
> +#define LOOPS_USEC Â Â Â Â Â Â (1 << LOOPS_USEC_SHIFT)
> +#define LOOPS(timeout) Â Â Â Â ((timeout) >> LOOPS_USEC_SHIFT)
> +
> +static const char driver_name[] = "mv_udc";
> +static const char driver_desc[] = DRIVER_DESC;
> +
> +/* controller device global variable */
> +static struct mv_udc  *the_controller;
> +int mv_usb_otgsc;
> +
> +static void nuke(struct mv_ep *ep, int status);
> +
> +/* for endpoint 0 operations */
> +static const struct usb_endpoint_descriptor mv_ep0_desc = {
> + Â Â Â .bLength = Â Â Â Â Â Â ÂUSB_DT_ENDPOINT_SIZE,
> + Â Â Â .bDescriptorType = Â Â ÂUSB_DT_ENDPOINT,
> + Â Â Â .bEndpointAddress = Â Â 0,
> + Â Â Â .bmAttributes = Â Â Â Â USB_ENDPOINT_XFER_CONTROL,
> + Â Â Â .wMaxPacketSize = Â Â Â EP0_MAX_PKT_SIZE,
> +};
> +
> +static void ep0_reset(struct mv_udc *udc)
> +{
> + Â Â Â struct mv_ep *ep;
> + Â Â Â u32 epctrlx;
> + Â Â Â int i = 0;
> +
> + Â Â Â /* ep0 in and out */
> + Â Â Â for (i = 0; i < 2; i++) {
> + Â Â Â Â Â Â Â ep = &udc->eps[i];
> + Â Â Â Â Â Â Â ep->udc = udc;
> +
> + Â Â Â Â Â Â Â /* ep0 dQH */
> + Â Â Â Â Â Â Â ep->dqh = &udc->ep_dqh[i];
> +
> + Â Â Â Â Â Â Â /* configure ep0 endpoint capabilities in dQH */
> + Â Â Â Â Â Â Â ep->dqh->max_packet_length =
> + Â Â Â Â Â Â Â Â Â Â Â (EP0_MAX_PKT_SIZE << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
> + Â Â Â Â Â Â Â Â Â Â Â | EP_QUEUE_HEAD_IOS;
> +
> + Â Â Â Â Â Â Â epctrlx = readl(&udc->op_regs->epctrlx[0]);
> + Â Â Â Â Â Â Â if (i) { Â Â Â Â/* TX */
> + Â Â Â Â Â Â Â Â Â Â Â epctrlx |= EPCTRL_TX_ENABLE | EPCTRL_TX_DATA_TOGGLE_RST
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | (USB_ENDPOINT_XFER_CONTROL
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â << EPCTRL_TX_EP_TYPE_SHIFT);
> +
> + Â Â Â Â Â Â Â } else { Â Â Â Â/* RX */
> + Â Â Â Â Â Â Â Â Â Â Â epctrlx |= EPCTRL_RX_ENABLE | EPCTRL_RX_DATA_TOGGLE_RST
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | (USB_ENDPOINT_XFER_CONTROL
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â << EPCTRL_RX_EP_TYPE_SHIFT);
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â writel(epctrlx, &udc->op_regs->epctrlx[0]);
> + Â Â Â }
> +}
> +
> +/* protocol ep0 stall, will automatically be cleared on new transaction */
> +static void ep0_stall(struct mv_udc *udc)
> +{
> + Â Â Â u32 Â Â epctrlx;
> +
> + Â Â Â /* set TX and RX to stall */
> + Â Â Â epctrlx = readl(&udc->op_regs->epctrlx[0]);
> + Â Â Â epctrlx |= EPCTRL_RX_EP_STALL | EPCTRL_TX_EP_STALL;
> + Â Â Â writel(epctrlx, &udc->op_regs->epctrlx[0]);
> +
> + Â Â Â /* update ep0 state */
> + Â Â Â udc->ep0_state = WAIT_FOR_SETUP;
> + Â Â Â udc->ep0_dir = EP_DIR_OUT;
> +}
> +
> +static int process_ep_req(struct mv_udc *udc, int index,
> + Â Â Â struct mv_req *curr_req)
> +{
> +    struct mv_dtd  *curr_dtd;
> +    struct mv_dqh  *curr_dqh;
> + Â Â Â int td_complete, actual, remaining_length;
> + Â Â Â int i, direction;
> + Â Â Â int retval = 0;
> + Â Â Â u32 errors;
> +
> + Â Â Â curr_dqh = &udc->ep_dqh[index];
> + Â Â Â direction = index % 2;
> +
> + Â Â Â curr_dtd = curr_req->head;
> + Â Â Â td_complete = 0;
> + Â Â Â actual = curr_req->req.length;
> +
> + Â Â Â for (i = 0; i < curr_req->dtd_count; i++) {
> + Â Â Â Â Â Â Â if (curr_dtd->size_ioc_sts & DTD_STATUS_ACTIVE) {
> + Â Â Â Â Â Â Â Â Â Â Â dev_dbg(&udc->dev->dev, "%s, dTD not completed\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â udc->eps[index].name);
> + Â Â Â Â Â Â Â Â Â Â Â return 1;
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â errors = curr_dtd->size_ioc_sts & DTD_ERROR_MASK;
> + Â Â Â Â Â Â Â if (!errors) {
> + Â Â Â Â Â Â Â Â Â Â Â remaining_length +=
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (curr_dtd->size_ioc_sts & DTD_PACKET_SIZE)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â >> DTD_LENGTH_BIT_POS;
> + Â Â Â Â Â Â Â Â Â Â Â actual -= remaining_length;
> + Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â dev_info(&udc->dev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "complete_tr error: ep=%d %s: error = 0x%x\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â index >> 1, direction ? "SEND" : "RECV",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â errors);
> + Â Â Â Â Â Â Â Â Â Â Â if (errors & DTD_STATUS_HALTED) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* Clear the errors and Halt condition */
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â curr_dqh->size_ioc_int_sts &= ~errors;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â retval = -EPIPE;
> + Â Â Â Â Â Â Â Â Â Â Â } else if (errors & DTD_STATUS_DATA_BUFF_ERR) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â retval = -EPROTO;
> + Â Â Â Â Â Â Â Â Â Â Â } else if (errors & DTD_STATUS_TRANSACTION_ERR) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â retval = -EILSEQ;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â if (i != curr_req->dtd_count - 1)
> + Â Â Â Â Â Â Â Â Â Â Â curr_dtd = (struct mv_dtd *)curr_dtd->next_dtd_virt;
> + Â Â Â }
> + Â Â Â if (retval)
> + Â Â Â Â Â Â Â return retval;
> +
> + Â Â Â curr_req->req.actual = actual;
> +
> + Â Â Â return 0;
> +}
> +
> +/*-----------------------------------------------------------------
> + * done() - retire a request; caller blocked irqs
> + * @status : request status to be set, only works when
> + * Â Â request is still in progress.
> + *--------------------------------------------------------------*/
> +static void done(struct mv_ep *ep, struct mv_req *req, int status)
> +{
> + Â Â Â struct mv_udc *udc = NULL;
> + Â Â Â unsigned char stopped = ep->stopped;
> + Â Â Â struct mv_dtd *curr_td, *next_td;
> + Â Â Â int j;
> +
> + Â Â Â udc = (struct mv_udc *)ep->udc;
> + Â Â Â /* Removed the req from fsl_ep->queue */
> + Â Â Â list_del_init(&req->queue);
> +
> + Â Â Â /* req.status should be set as -EINPROGRESS in ep_queue() */
> + Â Â Â if (req->req.status == -EINPROGRESS)
> + Â Â Â Â Â Â Â req->req.status = status;
> + Â Â Â else
> + Â Â Â Â Â Â Â status = req->req.status;
> +
> + Â Â Â /* Free dtd for the request */
> + Â Â Â next_td = req->head;
> + Â Â Â for (j = 0; j < req->dtd_count; j++) {
> + Â Â Â Â Â Â Â curr_td = next_td;
> + Â Â Â Â Â Â Â if (j != req->dtd_count - 1)
> + Â Â Â Â Â Â Â Â Â Â Â next_td = curr_td->next_dtd_virt;
> + Â Â Â Â Â Â Â dma_pool_free(udc->dtd_pool, curr_td, curr_td->td_dma);
> + Â Â Â }
> +
> + Â Â Â if (req->mapped) {
> + Â Â Â Â Â Â Â dma_unmap_single(ep->udc->gadget.dev.parent,
> + Â Â Â Â Â Â Â Â Â Â Â req->req.dma, req->req.length,
> + Â Â Â Â Â Â Â Â Â Â Â ((ep_dir(ep) == EP_DIR_IN) ?
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â DMA_TO_DEVICE : DMA_FROM_DEVICE));
> + Â Â Â Â Â Â Â req->req.dma = DMA_ADDR_INVALID;
> + Â Â Â Â Â Â Â req->mapped = 0;
> + Â Â Â } else
> + Â Â Â Â Â Â Â dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
> + Â Â Â Â Â Â Â Â Â Â Â req->req.dma, req->req.length,
> + Â Â Â Â Â Â Â Â Â Â Â ((ep_dir(ep) == EP_DIR_IN) ?
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â DMA_TO_DEVICE : DMA_FROM_DEVICE));
> +
> + Â Â Â if (status && (status != -ESHUTDOWN))
> + Â Â Â Â Â Â Â dev_info(&udc->dev->dev, "complete %s req %p stat %d len %u/%u",
> + Â Â Â Â Â Â Â Â Â Â Â ep->ep.name, &req->req, status,
> + Â Â Â Â Â Â Â Â Â Â Â req->req.actual, req->req.length);
> +
> + Â Â Â ep->stopped = 1;
> +
> + Â Â Â spin_unlock(&ep->udc->lock);
> + Â Â Â /* complete() is from gadget layer,
> + Â Â Â Â* eg fsg->bulk_in_complete() */
> + Â Â Â if (req->req.complete)
> + Â Â Â Â Â Â Â req->req.complete(&ep->ep, &req->req);
> +
> + Â Â Â spin_lock(&ep->udc->lock);
> + Â Â Â ep->stopped = stopped;
> +}
> +
> +static int queue_dtd(struct mv_ep *ep, struct mv_req *req)
> +{
> + Â Â Â u32 tmp, epstatus, bit_pos, direction;
> + Â Â Â struct mv_udc *udc;
> + Â Â Â struct mv_dqh *dqh;
> + Â Â Â unsigned int loops;
> + Â Â Â int readsafe, retval = 0;
> +
> + Â Â Â udc = ep->udc;
> + Â Â Â direction = ep_dir(ep);
> + Â Â Â dqh = &(udc->ep_dqh[ep->ep_num * 2 + direction]);
> + Â Â Â bit_pos = 1 << (((direction == EP_DIR_OUT) ? 0 : 16) + ep->ep_num);
> +
> + Â Â Â /* check if the pipe is empty */
> + Â Â Â if (!(list_empty(&ep->queue))) {
> + Â Â Â Â Â Â Â struct mv_req *lastreq;
> + Â Â Â Â Â Â Â lastreq = list_entry(ep->queue.prev, struct mv_req, queue);
> + Â Â Â Â Â Â Â lastreq->tail->dtd_next =
> + Â Â Â Â Â Â Â Â Â Â Â req->head->td_dma & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
> + Â Â Â Â Â Â Â if (readl(&udc->op_regs->epprime) & bit_pos) {
> + Â Â Â Â Â Â Â Â Â Â Â loops = LOOPS(PRIME_TIMEOUT);
> + Â Â Â Â Â Â Â Â Â Â Â while (readl(&udc->op_regs->epprime) & bit_pos) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (loops == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â retval = -ETIME;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto done;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â udelay(LOOPS_USEC);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â loops--;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â if (readl(&udc->op_regs->epstatus) & bit_pos)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto done;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â readsafe = 0;
> + Â Â Â Â Â Â Â loops = LOOPS(READSAFE_TIMEOUT);
> + Â Â Â Â Â Â Â while (readsafe == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â if (loops == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â retval = -ETIME;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto done;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â /* start with setting the semaphores */
> + Â Â Â Â Â Â Â Â Â Â Â tmp = readl(&udc->op_regs->usbcmd);
> + Â Â Â Â Â Â Â Â Â Â Â tmp |= USBCMD_ATDTW_TRIPWIRE_SET;
> + Â Â Â Â Â Â Â Â Â Â Â writel(tmp, &udc->op_regs->usbcmd);
> +
> + Â Â Â Â Â Â Â Â Â Â Â /* read the endpoint status */
> + Â Â Â Â Â Â Â Â Â Â Â epstatus = readl(&udc->op_regs->epstatus) & bit_pos;
> +
> + Â Â Â Â Â Â Â Â Â Â Â /* Reread the ATDTW semaphore bit to check if it is
> + Â Â Â Â Â Â Â Â Â Â Â Â* cleared. When hardware see a hazard, it will clear
> + Â Â Â Â Â Â Â Â Â Â Â Â* the bit or else we remain set to 1 and we can
> + Â Â Â Â Â Â Â Â Â Â Â Â* proceed with priming of endpoint if not already
> + Â Â Â Â Â Â Â Â Â Â Â Â* primed.
> + Â Â Â Â Â Â Â Â Â Â Â Â*/
> + Â Â Â Â Â Â Â Â Â Â Â if (readl(&udc->op_regs->usbcmd)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â & USBCMD_ATDTW_TRIPWIRE_SET) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â readsafe = 1;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â loops--;
> + Â Â Â Â Â Â Â Â Â Â Â udelay(LOOPS_USEC);
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â /* Clear the semaphore */
> + Â Â Â Â Â Â Â tmp = readl(&udc->op_regs->usbcmd);
> + Â Â Â Â Â Â Â tmp &= USBCMD_ATDTW_TRIPWIRE_CLEAR;
> + Â Â Â Â Â Â Â writel(tmp, &udc->op_regs->usbcmd);
> +
> + Â Â Â Â Â Â Â /* If endpoint is not active, we activate it now. */
> + Â Â Â Â Â Â Â if (!epstatus) {
> + Â Â Â Â Â Â Â Â Â Â Â if (direction == EP_DIR_IN) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct mv_dtd *curr_dtd = dma_to_virt(
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &udc->dev->dev, dqh->curr_dtd_ptr);
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â loops = LOOPS(DTD_TIMEOUT);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â while (curr_dtd->size_ioc_sts
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â & DTD_STATUS_ACTIVE) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (loops == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â retval = -ETIME;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto done;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â loops--;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â udelay(LOOPS_USEC);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â /* No other transfers on the queue */
> +
> + Â Â Â Â Â Â Â Â Â Â Â /* Write dQH next pointer and terminate bit to 0 */
> + Â Â Â Â Â Â Â Â Â Â Â dqh->next_dtd_ptr = req->head->td_dma
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â & EP_QUEUE_HEAD_NEXT_POINTER_MASK;
> + Â Â Â Â Â Â Â Â Â Â Â dqh->size_ioc_int_sts = 0;
> +
> + Â Â Â Â Â Â Â Â Â Â Â /* Ensure that updates to the QH will
> + Â Â Â Â Â Â Â Â Â Â Â Â* occure before priming.
> + Â Â Â Â Â Â Â Â Â Â Â Â*/
> + Â Â Â Â Â Â Â Â Â Â Â wmb();
> +
> + Â Â Â Â Â Â Â Â Â Â Â /* Prime the Endpoint */
> + Â Â Â Â Â Â Â Â Â Â Â writel(bit_pos, &udc->op_regs->epprime);
> + Â Â Â Â Â Â Â }
> + Â Â Â } else {
> + Â Â Â Â Â Â Â /* Write dQH next pointer and terminate bit to 0 */
> + Â Â Â Â Â Â Â dqh->next_dtd_ptr = req->head->td_dma
> + Â Â Â Â Â Â Â Â Â Â Â & EP_QUEUE_HEAD_NEXT_POINTER_MASK;;
> + Â Â Â Â Â Â Â dqh->size_ioc_int_sts = 0;
> +
> + Â Â Â Â Â Â Â /* Ensure that updates to the QH will occure before priming. */
> + Â Â Â Â Â Â Â wmb();
> +
> + Â Â Â Â Â Â Â /* Prime the Endpoint */
> + Â Â Â Â Â Â Â writel(bit_pos, &udc->op_regs->epprime);
> +
> + Â Â Â Â Â Â Â if (direction == EP_DIR_IN) {
> + Â Â Â Â Â Â Â Â Â Â Â /* FIXME add status check after prime the IN ep */
> + Â Â Â Â Â Â Â Â Â Â Â int prime_again;
> + Â Â Â Â Â Â Â Â Â Â Â u32 curr_dtd_ptr = dqh->curr_dtd_ptr;
> +
> + Â Â Â Â Â Â Â Â Â Â Â loops = LOOPS(DTD_TIMEOUT);
> + Â Â Â Â Â Â Â Â Â Â Â prime_again = 0;
> + Â Â Â Â Â Â Â Â Â Â Â while ((curr_dtd_ptr != req->head->td_dma)) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â curr_dtd_ptr = dqh->curr_dtd_ptr;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (loops == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dev_err(&udc->dev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "failed to prime %s\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ep->name);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â retval = -ETIME;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto done;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â loops--;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â udelay(LOOPS_USEC);
> +
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (loops == (LOOPS(DTD_TIMEOUT) >> 2)) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (prime_again)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto done;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dev_info(&udc->dev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "prime again\n");
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â writel(bit_pos,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &udc->op_regs->epprime);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â prime_again = 1;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +done:
> + Â Â Â return retval;;
> +}
> +
> +static struct mv_dtd *build_dtd(struct mv_req *req, unsigned *length,
> + Â Â Â Â Â Â Â dma_addr_t *dma, int *is_last)
> +{
> + Â Â Â u32 temp;
> + Â Â Â struct mv_dtd *dtd;
> + Â Â Â struct mv_udc *udc;
> +
> + Â Â Â /* how big will this transfer be? */
> + Â Â Â *length = min(req->req.length - req->req.actual,
> + Â Â Â Â Â Â Â Â Â Â Â (unsigned)EP_MAX_LENGTH_TRANSFER);
> +
> + Â Â Â udc = req->ep->udc;
> +
> + Â Â Â /* Be careful that no _GFP_HIGHMEM is set,
> + Â Â Â Â* or we can not use dma_to_virt
> + Â Â Â Â*/
> + Â Â Â dtd = dma_pool_alloc(udc->dtd_pool, GFP_KERNEL, dma);
> + Â Â Â if (dtd == NULL)
> + Â Â Â Â Â Â Â return dtd;
> +
> + Â Â Â dtd->td_dma = *dma;
> + Â Â Â /* initialize buffer page pointers */
> + Â Â Â temp = (u32)(req->req.dma + req->req.actual);
> + Â Â Â dtd->buff_ptr0 = cpu_to_le32(temp);
> + Â Â Â temp &= ~0xFFF;
> + Â Â Â dtd->buff_ptr1 = cpu_to_le32(temp + 0x1000);
> + Â Â Â dtd->buff_ptr2 = cpu_to_le32(temp + 0x2000);
> + Â Â Â dtd->buff_ptr3 = cpu_to_le32(temp + 0x3000);
> + Â Â Â dtd->buff_ptr4 = cpu_to_le32(temp + 0x4000);
> +
> + Â Â Â req->req.actual += *length;
> +
> + Â Â Â /* zlp is needed if req->req.zero is set */
> + Â Â Â if (req->req.zero) {
> + Â Â Â Â Â Â Â if (*length == 0 || (*length % req->ep->ep.maxpacket) != 0)
> + Â Â Â Â Â Â Â Â Â Â Â *is_last = 1;
> + Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â Â *is_last = 0;
> + Â Â Â } else if (req->req.length == req->req.actual)
> + Â Â Â Â Â Â Â *is_last = 1;
> + Â Â Â else
> + Â Â Â Â Â Â Â *is_last = 0;
> +
> + Â Â Â /* Fill in the transfer size; set active bit */
> + Â Â Â temp = ((*length << DTD_LENGTH_BIT_POS) | DTD_STATUS_ACTIVE);
> +
> + Â Â Â /* Enable interrupt for the last dtd of a request */
> + Â Â Â if (*is_last && !req->req.no_interrupt)
> + Â Â Â Â Â Â Â temp |= DTD_IOC;
> +
> + Â Â Â dtd->size_ioc_sts = temp;
> +
> + Â Â Â mb();
> +
> + Â Â Â return dtd;
> +}
> +
> +/* generate dTD linked list for a request */
> +static int req_to_dtd(struct mv_req *req)
> +{
> + Â Â Â unsigned count;
> + Â Â Â int is_last, is_first = 1;
> + Â Â Â struct mv_dtd *dtd, *last_dtd = NULL;
> + Â Â Â struct mv_udc *udc;
> + Â Â Â dma_addr_t dma;
> +
> + Â Â Â udc = req->ep->udc;
> +
> + Â Â Â do {
> + Â Â Â Â Â Â Â dtd = build_dtd(req, &count, &dma, &is_last);
> + Â Â Â Â Â Â Â if (dtd == NULL)
> + Â Â Â Â Â Â Â Â Â Â Â return -ENOMEM;
> +
> + Â Â Â Â Â Â Â if (is_first) {
> + Â Â Â Â Â Â Â Â Â Â Â is_first = 0;
> + Â Â Â Â Â Â Â Â Â Â Â req->head = dtd;
> + Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â last_dtd->dtd_next = dma;
> + Â Â Â Â Â Â Â Â Â Â Â last_dtd->next_dtd_virt = dtd;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â last_dtd = dtd;
> + Â Â Â Â Â Â Â req->dtd_count++;
> + Â Â Â } while (!is_last);
> +
> + Â Â Â /* set terminate bit to 1 for the last dTD */
> + Â Â Â dtd->dtd_next = DTD_NEXT_TERMINATE;
> +
> + Â Â Â req->tail = dtd;
> +
> + Â Â Â return 0;
> +}
> +
> +static int mv_ep_enable(struct usb_ep *_ep,
> + Â Â Â Â Â Â Â const struct usb_endpoint_descriptor *desc)
> +{
> + Â Â Â struct mv_udc *udc;
> + Â Â Â struct mv_ep *ep;
> + Â Â Â struct mv_dqh *dqh;
> + Â Â Â u16 max = 0;
> + Â Â Â u32 bit_pos, epctrlx, direction;
> + Â Â Â unsigned char zlt = 0, ios = 0, mult = 0;
> +
> + Â Â Â ep = container_of(_ep, struct mv_ep, ep);
> + Â Â Â udc = ep->udc;
> +
> + Â Â Â if (!_ep || !desc || ep->desc
> + Â Â Â Â Â Â Â Â Â Â Â || desc->bDescriptorType != USB_DT_ENDPOINT)
> + Â Â Â Â Â Â Â return -EINVAL;
> +
> + Â Â Â if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
> + Â Â Â Â Â Â Â return -ESHUTDOWN;
> +
> + Â Â Â direction = ep_dir(ep);
> + Â Â Â max = le16_to_cpu(desc->wMaxPacketSize);
> +
> + Â Â Â /*
> + Â Â Â Â* disable HW zero length termination select
> + Â Â Â Â* driver handles zero length packet through req->req.zero
> + Â Â Â Â*/
> + Â Â Â zlt = 1;
> +
> + Â Â Â /* Get the endpoint queue head address */
> + Â Â Â dqh = (struct mv_dqh *)ep->dqh;
> +
> + Â Â Â bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
> +
> + Â Â Â /* Check if the Endpoint is Primed */
> + Â Â Â if ((readl(&udc->op_regs->epprime) & bit_pos)
> + Â Â Â Â Â Â Â || (readl(&udc->op_regs->epstatus) & bit_pos)) {
> + Â Â Â Â Â Â Â dev_info(&udc->dev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â "ep=%d %s: Init ERROR: ENDPTPRIME=0x%x,"
> + Â Â Â Â Â Â Â Â Â Â Â " ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
> + Â Â Â Â Â Â Â Â Â Â Â (unsigned)ep->ep_num, direction ? "SEND" : "RECV",
> + Â Â Â Â Â Â Â Â Â Â Â (unsigned)readl(&udc->op_regs->epprime),
> + Â Â Â Â Â Â Â Â Â Â Â (unsigned)readl(&udc->op_regs->epstatus),
> + Â Â Â Â Â Â Â Â Â Â Â (unsigned)bit_pos);
> + Â Â Â Â Â Â Â goto en_done;
> + Â Â Â }
> + Â Â Â /* Set the max packet length, interrupt on Setup and Mult fields */
> + Â Â Â switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
> + Â Â Â case USB_ENDPOINT_XFER_BULK:
> + Â Â Â Â Â Â Â zlt = 1;
> + Â Â Â Â Â Â Â mult = 0;
> + Â Â Â Â Â Â Â break;
> + Â Â Â case USB_ENDPOINT_XFER_CONTROL:
> + Â Â Â Â Â Â Â ios = 1;
> + Â Â Â case USB_ENDPOINT_XFER_INT:
> + Â Â Â Â Â Â Â mult = 0;
> + Â Â Â Â Â Â Â break;
> + Â Â Â case USB_ENDPOINT_XFER_ISOC:
> + Â Â Â Â Â Â Â /* Calculate transactions needed for high bandwidth iso */
> + Â Â Â Â Â Â Â mult = (unsigned char)(1 + ((max >> 11) & 0x03));
> + Â Â Â Â Â Â Â max = max & 0x8ff; Â Â Â/* bit 0~10 */
> + Â Â Â Â Â Â Â /* 3 transactions at most */
> + Â Â Â Â Â Â Â if (mult > 3)
> + Â Â Â Â Â Â Â Â Â Â Â goto en_done;
> + Â Â Â Â Â Â Â break;
> + Â Â Â default:
> + Â Â Â Â Â Â Â goto en_done;
> + Â Â Â }
> + Â Â Â dqh->max_packet_length = (max << EP_QUEUE_HEAD_MAX_PKT_LEN_POS)
> + Â Â Â Â Â Â Â | (mult << EP_QUEUE_HEAD_MULT_POS)
> + Â Â Â Â Â Â Â | (zlt ? EP_QUEUE_HEAD_ZLT_SEL : 0)
> + Â Â Â Â Â Â Â | (ios ? EP_QUEUE_HEAD_IOS : 0);
> + Â Â Â dqh->next_dtd_ptr = 1;
> + Â Â Â dqh->size_ioc_int_sts = 0;
> +
> + Â Â Â ep->ep.maxpacket = max;
> + Â Â Â ep->desc = desc;
> + Â Â Â ep->stopped = 0;
> +
> + Â Â Â /* Enable the endpoint for Rx or Tx and set the endpoint type */
> + Â Â Â epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
> + Â Â Â if (direction == EP_DIR_IN) {
> + Â Â Â Â Â Â Â epctrlx &= ~EPCTRL_TX_ALL_MASK;
> + Â Â Â Â Â Â Â epctrlx |= EPCTRL_TX_ENABLE | EPCTRL_TX_DATA_TOGGLE_RST
> + Â Â Â Â Â Â Â Â Â Â Â | ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â << EPCTRL_TX_EP_TYPE_SHIFT);
> + Â Â Â } else {
> + Â Â Â Â Â Â Â epctrlx &= ~EPCTRL_RX_ALL_MASK;
> + Â Â Â Â Â Â Â epctrlx |= EPCTRL_RX_ENABLE | EPCTRL_RX_DATA_TOGGLE_RST
> + Â Â Â Â Â Â Â Â Â Â Â | ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â << EPCTRL_RX_EP_TYPE_SHIFT);
> + Â Â Â }
> + Â Â Â writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
> +
> + Â Â Â /* Implement Guideline (GL# USB-7) The unused endpoint type must
> + Â Â Â Â* be programmed to bulk.
> + Â Â Â Â*/
> + Â Â Â epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
> + Â Â Â if ((epctrlx & EPCTRL_RX_ENABLE) == 0) {
> + Â Â Â Â Â Â Â epctrlx |= ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â << EPCTRL_RX_EP_TYPE_SHIFT);
> + Â Â Â Â Â Â Â writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
> + Â Â Â }
> +
> + Â Â Â epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
> + Â Â Â if ((epctrlx & EPCTRL_TX_ENABLE) == 0) {
> + Â Â Â Â Â Â Â epctrlx |= ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â << EPCTRL_TX_EP_TYPE_SHIFT);
> + Â Â Â Â Â Â Â writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
> + Â Â Â }
> +
> + Â Â Â return 0;
> +en_done:
> + Â Â Â return -EINVAL;
> +}
> +
> +static int Âmv_ep_disable(struct usb_ep *_ep)
> +{
> + Â Â Â struct mv_udc *udc;
> + Â Â Â struct mv_ep *ep;
> + Â Â Â struct mv_dqh *dqh;
> + Â Â Â u32 bit_pos, epctrlx, direction;
> +
> + Â Â Â ep = container_of(_ep, struct mv_ep, ep);
> + Â Â Â if ((_ep == NULL) || !ep->desc)
> + Â Â Â Â Â Â Â return -EINVAL;
> +
> + Â Â Â udc = ep->udc;
> +
> + Â Â Â /* Get the endpoint queue head address */
> + Â Â Â dqh = ep->dqh;
> +
> + Â Â Â direction = ep_dir(ep);
> + Â Â Â bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
> +
> + Â Â Â /* Reset the max packet length and the interrupt on Setup */
> + Â Â Â dqh->max_packet_length = 0;
> +
> + Â Â Â /* Disable the endpoint for Rx or Tx and reset the endpoint type */
> + Â Â Â epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
> + Â Â Â epctrlx &= ~((direction == EP_DIR_IN)
> + Â Â Â Â Â Â Â Â Â Â Â ? (EPCTRL_TX_ENABLE | EPCTRL_TX_TYPE)
> + Â Â Â Â Â Â Â Â Â Â Â : (EPCTRL_RX_ENABLE | EPCTRL_RX_TYPE));
> + Â Â Â writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
> +
> + Â Â Â /* nuke all pending requests (does flush) */
> + Â Â Â nuke(ep, -ESHUTDOWN);
> +
> + Â Â Â ep->desc = NULL;
> + Â Â Â ep->stopped = 1;
> + Â Â Â return 0;
> +}
> +
> +static struct usb_request *
> +mv_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
> +{
> + Â Â Â struct mv_req *req = NULL;
> +
> + Â Â Â req = kzalloc(sizeof *req, gfp_flags);
> + Â Â Â if (!req)
> + Â Â Â Â Â Â Â return NULL;
> +
> + Â Â Â req->req.dma = DMA_ADDR_INVALID;
> + Â Â Â INIT_LIST_HEAD(&req->queue);
> +
> + Â Â Â return &req->req;
> +}
> +
> +static void mv_free_request(struct usb_ep *_ep, struct usb_request *_req)
> +{
> + Â Â Â struct mv_req *req = NULL;
> +
> + Â Â Â req = container_of(_req, struct mv_req, req);
> +
> + Â Â Â if (_req)
> + Â Â Â Â Â Â Â kfree(req);
> +}
> +
> +static void mv_ep_fifo_flush(struct usb_ep *_ep)
> +{
> + Â Â Â struct mv_udc *udc;
> + Â Â Â u32 bit_pos, direction;
> + Â Â Â struct mv_ep *ep = container_of(_ep, struct mv_ep, ep);
> + Â Â Â unsigned int loops;
> +
> + Â Â Â udc = ep->udc;
> + Â Â Â direction = ep_dir(ep);
> + Â Â Â bit_pos = 1 << ((direction == EP_DIR_OUT ? 0 : 16) + ep->ep_num);
> + Â Â Â /* Flushing will halt the pipe */
> + Â Â Â /* Write 1 to the Flush register */
> + Â Â Â writel(bit_pos, &udc->op_regs->epflush);
> +
> + Â Â Â /* Wait until flushing completed */
> + Â Â Â loops = LOOPS(FLUSH_TIMEOUT);
> + Â Â Â while (readl(&udc->op_regs->epflush) & bit_pos) {
> + Â Â Â Â Â Â Â /* ENDPTFLUSH bit should be cleared to indicate this
> + Â Â Â Â Â Â Â Â* operation is complete
> + Â Â Â Â Â Â Â Â*/
> + Â Â Â Â Â Â Â if (loops == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â dev_err(&udc->dev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "TIMEOUT for ENDPTFLUSH=0x%x, bit_pos=0x%x\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (unsigned)readl(&udc->op_regs->epflush),
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (unsigned)bit_pos);
> + Â Â Â Â Â Â Â Â Â Â Â return;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â loops--;
> + Â Â Â Â Â Â Â udelay(LOOPS_USEC);
> + Â Â Â }
> + Â Â Â loops = LOOPS(EPSTATUS_TIMEOUT);
> + Â Â Â while (readl(&udc->op_regs->epstatus) & bit_pos) {
> + Â Â Â Â Â Â Â unsigned int inter_loops;
> +
> + Â Â Â Â Â Â Â if (loops == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â dev_err(&udc->dev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "TIMEOUT for ENDPTSTATUS=0x%x, bit_pos=0x%x\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (unsigned)readl(&udc->op_regs->epstatus),
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (unsigned)bit_pos);
> + Â Â Â Â Â Â Â Â Â Â Â return;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â /* Write 1 to the Flush register */
> + Â Â Â Â Â Â Â writel(bit_pos, &udc->op_regs->epflush);
> +
> + Â Â Â Â Â Â Â /* Wait until flushing completed */
> + Â Â Â Â Â Â Â inter_loops = LOOPS(FLUSH_TIMEOUT);
> + Â Â Â Â Â Â Â while (readl(&udc->op_regs->epflush) & bit_pos) {
> + Â Â Â Â Â Â Â Â Â Â Â /* ENDPTFLUSH bit should be cleared to indicate this
> + Â Â Â Â Â Â Â Â Â Â Â Â* operation is complete
> + Â Â Â Â Â Â Â Â Â Â Â Â*/
> + Â Â Â Â Â Â Â Â Â Â Â if (inter_loops == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dev_err(&udc->dev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "TIMEOUT for ENDPTFLUSH=0x%x,"
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "bit_pos=0x%x\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (unsigned)readl(&udc->op_regs->epflush),
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (unsigned)bit_pos);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return;
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â Â Â Â Â inter_loops--;
> + Â Â Â Â Â Â Â Â Â Â Â udelay(LOOPS_USEC);
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â loops--;
> + Â Â Â }
> +}
> +
> +/* queues (submits) an I/O request to an endpoint */
> +static int
> +mv_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
> +{
> + Â Â Â struct mv_ep *ep = container_of(_ep, struct mv_ep, ep);
> + Â Â Â struct mv_req *req = container_of(_req, struct mv_req, req);
> + Â Â Â struct mv_udc *udc = ep->udc;
> + Â Â Â unsigned long flags;
> +
> + Â Â Â /* catch various bogus parameters */
> + Â Â Â if (!_req || !req->req.complete || !req->req.buf
> + Â Â Â Â Â Â Â Â Â Â Â || !list_empty(&req->queue)) {
> + Â Â Â Â Â Â Â dev_err(&udc->dev->dev, "%s, bad params", __func__);
> + Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â }
> + Â Â Â if (unlikely(!_ep || !ep->desc)) {
> + Â Â Â Â Â Â Â dev_err(&udc->dev->dev, "%s, bad ep", __func__);
> + Â Â Â Â Â Â Â return -EINVAL;
> + Â Â Â }
> + Â Â Â if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
> + Â Â Â Â Â Â Â if (req->req.length > ep->ep.maxpacket)
> + Â Â Â Â Â Â Â Â Â Â Â return -EMSGSIZE;
> + Â Â Â }
> +
> + Â Â Â udc = ep->udc;
> + Â Â Â if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
> + Â Â Â Â Â Â Â return -ESHUTDOWN;
> +
> + Â Â Â req->ep = ep;
> +
> + Â Â Â /* map virtual address to hardware */
> + Â Â Â if (req->req.dma == DMA_ADDR_INVALID) {
> + Â Â Â Â Â Â Â req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â req->req.buf,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â req->req.length, ep_dir(ep)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ? DMA_TO_DEVICE
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â : DMA_FROM_DEVICE);
> + Â Â Â Â Â Â Â req->mapped = 1;
> + Â Â Â } else {
> + Â Â Â Â Â Â Â dma_sync_single_for_device(ep->udc->gadget.dev.parent,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â req->req.dma, req->req.length,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ep_dir(ep)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ? DMA_TO_DEVICE
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â : DMA_FROM_DEVICE);
> + Â Â Â Â Â Â Â req->mapped = 0;
> + Â Â Â }
> +
> + Â Â Â req->req.status = -EINPROGRESS;
> + Â Â Â req->req.actual = 0;
> + Â Â Â req->dtd_count = 0;
> +
> + Â Â Â spin_lock_irqsave(&udc->lock, flags);
> +
> + Â Â Â /* build dtds and push them to device queue */
> + Â Â Â if (!req_to_dtd(req)) {
> + Â Â Â Â Â Â Â int retval;
> + Â Â Â Â Â Â Â retval = queue_dtd(ep, req);
> + Â Â Â Â Â Â Â if (retval) {
> + Â Â Â Â Â Â Â Â Â Â Â spin_unlock_irqrestore(&udc->lock, flags);
> + Â Â Â Â Â Â Â Â Â Â Â return retval;
> + Â Â Â Â Â Â Â }
> + Â Â Â } else {
> + Â Â Â Â Â Â Â spin_unlock_irqrestore(&udc->lock, flags);
> + Â Â Â Â Â Â Â return -ENOMEM;
> + Â Â Â }
> +
> + Â Â Â /* Update ep0 state */
> + Â Â Â if (ep->ep_num == 0)
> + Â Â Â Â Â Â Â udc->ep0_state = DATA_STATE_XMIT;
> +
> + Â Â Â /* irq handler advances the queue */
> + Â Â Â if (req != NULL)
> + Â Â Â Â Â Â Â list_add_tail(&req->queue, &ep->queue);
> + Â Â Â spin_unlock_irqrestore(&udc->lock, flags);
> +
> + Â Â Â return 0;
> +}
> +
> +/* dequeues (cancels, unlinks) an I/O request from an endpoint */
> +static int mv_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
> +{
> + Â Â Â struct mv_ep *ep = container_of(_ep, struct mv_ep, ep);
> + Â Â Â struct mv_req *req;
> + Â Â Â struct mv_udc *udc = ep->udc;
> + Â Â Â unsigned long flags;
> + Â Â Â int stopped, ret = 0;
> + Â Â Â u32 epctrlx;
> +
> + Â Â Â if (!_ep || !_req)
> + Â Â Â Â Â Â Â return -EINVAL;
> +
> + Â Â Â spin_lock_irqsave(&ep->udc->lock, flags);
> + Â Â Â stopped = ep->stopped;
> +
> + Â Â Â /* Stop the ep before we deal with the queue */
> + Â Â Â ep->stopped = 1;
> + Â Â Â epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
> + Â Â Â if (ep_dir(ep) == EP_DIR_IN)
> + Â Â Â Â Â Â Â epctrlx &= ~EPCTRL_TX_ENABLE;
> + Â Â Â else
> + Â Â Â Â Â Â Â epctrlx &= ~EPCTRL_RX_ENABLE;
> + Â Â Â writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
> +
> + Â Â Â /* make sure it's actually queued on this endpoint */
> + Â Â Â list_for_each_entry(req, &ep->queue, queue) {
> + Â Â Â Â Â Â Â if (&req->req == _req)
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â }
> + Â Â Â if (&req->req != _req) {
> + Â Â Â Â Â Â Â ret = -EINVAL;
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â /* The request is in progress, or completed but not dequeued */
> + Â Â Â if (ep->queue.next == &req->queue) {
> + Â Â Â Â Â Â Â _req->status = -ECONNRESET;
> + Â Â Â Â Â Â Â mv_ep_fifo_flush(_ep); Â/* flush current transfer */
> +
> + Â Â Â Â Â Â Â /* The request isn't the last request in this ep queue */
> + Â Â Â Â Â Â Â if (req->queue.next != &ep->queue) {
> + Â Â Â Â Â Â Â Â Â Â Â struct mv_dqh *qh;
> + Â Â Â Â Â Â Â Â Â Â Â struct mv_req *next_req;
> +
> + Â Â Â Â Â Â Â Â Â Â Â qh = ep->dqh;
> + Â Â Â Â Â Â Â Â Â Â Â next_req = list_entry(req->queue.next, struct mv_req,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â queue);
> +
> + Â Â Â Â Â Â Â Â Â Â Â /* Point the QH to the first TD of next request */
> + Â Â Â Â Â Â Â Â Â Â Â writel((u32) next_req->head, &qh->curr_dtd_ptr);
> + Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â struct mv_dqh *qh;
> +
> + Â Â Â Â Â Â Â Â Â Â Â qh = ep->dqh;
> + Â Â Â Â Â Â Â Â Â Â Â qh->next_dtd_ptr = 1;
> + Â Â Â Â Â Â Â Â Â Â Â qh->size_ioc_int_sts = 0;
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â /* The request hasn't been processed, patch up the TD chain */
> + Â Â Â } else {
> + Â Â Â Â Â Â Â struct mv_req *prev_req;
> +
> + Â Â Â Â Â Â Â prev_req = list_entry(req->queue.prev, struct mv_req, queue);
> + Â Â Â Â Â Â Â writel(readl(&req->tail->dtd_next),
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &prev_req->tail->dtd_next);
> +
> + Â Â Â }
> +
> + Â Â Â done(ep, req, -ECONNRESET);
> +
> + Â Â Â /* Enable EP */
> +out:
> + Â Â Â epctrlx = readl(&udc->op_regs->epctrlx[ep->ep_num]);
> + Â Â Â if (ep_dir(ep) == EP_DIR_IN)
> + Â Â Â Â Â Â Â epctrlx |= EPCTRL_TX_ENABLE;
> + Â Â Â else
> + Â Â Â Â Â Â Â epctrlx |= EPCTRL_RX_ENABLE;
> + Â Â Â writel(epctrlx, &udc->op_regs->epctrlx[ep->ep_num]);
> + Â Â Â ep->stopped = stopped;
> +
> + Â Â Â spin_unlock_irqrestore(&ep->udc->lock, flags);
> + Â Â Â return ret;
> +}
> +
> +static void ep_set_stall(struct mv_udc *udc, u8 ep_num, u8 direction, int stall)
> +{
> + Â Â Â u32 epctrlx;
> +
> + Â Â Â epctrlx = readl(&udc->op_regs->epctrlx[ep_num]);
> +
> + Â Â Â if (stall) {
> + Â Â Â Â Â Â Â if (direction == EP_DIR_IN)
> + Â Â Â Â Â Â Â Â Â Â Â epctrlx |= EPCTRL_TX_EP_STALL;
> + Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â Â epctrlx |= EPCTRL_RX_EP_STALL;
> + Â Â Â } else {
> + Â Â Â Â Â Â Â if (direction == EP_DIR_IN) {
> + Â Â Â Â Â Â Â Â Â Â Â epctrlx &= ~EPCTRL_TX_EP_STALL;
> + Â Â Â Â Â Â Â Â Â Â Â epctrlx |= EPCTRL_TX_DATA_TOGGLE_RST;
> + Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â epctrlx &= ~EPCTRL_RX_EP_STALL;
> + Â Â Â Â Â Â Â Â Â Â Â epctrlx |= EPCTRL_RX_DATA_TOGGLE_RST;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> + Â Â Â writel(epctrlx, &udc->op_regs->epctrlx[ep_num]);
> +}
> +
> +static int ep_is_stall(struct mv_udc *udc, u8 ep_num, u8 direction)
> +{
> + Â Â Â u32 epctrlx;
> +
> + Â Â Â epctrlx = readl(&udc->op_regs->epctrlx[ep_num]);
> +
> + Â Â Â if (direction == EP_DIR_OUT)
> + Â Â Â Â Â Â Â return (epctrlx & EPCTRL_RX_EP_STALL) ? 1 : 0;
> + Â Â Â else
> + Â Â Â Â Â Â Â return (epctrlx & EPCTRL_TX_EP_STALL) ? 1 : 0;
> +}
> +
> +static int mv_ep_set_halt(struct usb_ep *_ep, int halt)
> +{
> + Â Â Â struct mv_ep *ep;
> + Â Â Â unsigned long flags = 0;
> + Â Â Â int status = 0;
> + Â Â Â struct mv_udc *udc;
> +
> + Â Â Â ep = container_of(_ep, struct mv_ep, ep);
> + Â Â Â udc = ep->udc;
> + Â Â Â if (!_ep || !ep->desc) {
> + Â Â Â Â Â Â Â status = -EINVAL;
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â if (ep->desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
> + Â Â Â Â Â Â Â status = -EOPNOTSUPP;
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â /* Attempt to halt IN ep will fail if any transfer requests
> + Â Â Â Â* are still queue */
> + Â Â Â if (halt && (ep_dir(ep) == EP_DIR_IN) && !list_empty(&ep->queue)) {
> + Â Â Â Â Â Â Â status = -EAGAIN;
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â spin_lock_irqsave(&ep->udc->lock, flags);
> + Â Â Â ep_set_stall(udc, ep->ep_num, ep_dir(ep), halt);
> + Â Â Â spin_unlock_irqrestore(&ep->udc->lock, flags);
> +
> + Â Â Â if (ep->ep_num == 0) {
> + Â Â Â Â Â Â Â udc->ep0_state = WAIT_FOR_SETUP;
> + Â Â Â Â Â Â Â udc->ep0_dir = EP_DIR_OUT;
> + Â Â Â }
> +out:
> + Â Â Â return status;
> +}
> +
> +static struct usb_ep_ops mv_ep_ops = {
> +    .enable     = mv_ep_enable,
> +    .disable    Â= mv_ep_disable,
> +
> + Â Â Â .alloc_request Â= mv_alloc_request,
> +    .free_request  = mv_free_request,
> +
> +    .queue     Â= mv_ep_queue,
> +    .dequeue    Â= mv_ep_dequeue,
> +
> +    .set_halt    = mv_ep_set_halt,
> +    .fifo_flush   = mv_ep_fifo_flush,   /* flush fifo */
> +};
> +
> +static void udc_stop(struct mv_udc *udc)
> +{
> + Â Â Â u32 tmp;
> +
> + Â Â Â /* Disable interrupts */
> + Â Â Â tmp = readl(&udc->op_regs->usbintr);
> + Â Â Â tmp &= ~(USBINTR_INT_EN | USBINTR_ERR_INT_EN |
> + Â Â Â Â Â Â Â USBINTR_PORT_CHANGE_DETECT_EN | USBINTR_RESET_EN);
> + Â Â Â writel(tmp, &udc->op_regs->usbintr);
> +
> + Â Â Â /* Reset the Run the bit in the command register to stop VUSB */
> + Â Â Â tmp = readl(&udc->op_regs->usbcmd);
> + Â Â Â tmp &= ~USBCMD_RUN_STOP;
> + Â Â Â writel(tmp, &udc->op_regs->usbcmd);
> +}
> +
> +static void udc_start(struct mv_udc *udc)
> +{
> + Â Â Â u32 usbintr;
> +
> + Â Â Â usbintr = USBINTR_INT_EN | USBINTR_ERR_INT_EN
> + Â Â Â Â Â Â Â | USBINTR_PORT_CHANGE_DETECT_EN
> + Â Â Â Â Â Â Â | USBINTR_RESET_EN | USBINTR_DEVICE_SUSPEND;
> + Â Â Â /* Enable interrupts */
> + Â Â Â writel(usbintr, &udc->op_regs->usbintr);
> +
> + Â Â Â /* Set the Run bit in the command register */
> + Â Â Â writel(USBCMD_RUN_STOP, &udc->op_regs->usbcmd);
> +}
> +
> +static int udc_reset(struct mv_udc *udc)
> +{
> + Â Â Â unsigned int loops;
> + Â Â Â u32 tmp, portsc;
> +
> + Â Â Â /* Stop the controller */
> + Â Â Â tmp = readl(&udc->op_regs->usbcmd);
> + Â Â Â tmp &= ~USBCMD_RUN_STOP;
> + Â Â Â writel(tmp, &udc->op_regs->usbcmd);
> +
> + Â Â Â /* Reset the controller to get default values */
> + Â Â Â writel(USBCMD_CTRL_RESET, &udc->op_regs->usbcmd);
> +
> + Â Â Â /* wait for reset to complete */
> + Â Â Â loops = LOOPS(RESET_TIMEOUT);
> + Â Â Â while (readl(&udc->op_regs->usbcmd) & USBCMD_CTRL_RESET) {
> + Â Â Â Â Â Â Â if (loops == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â dev_err(&udc->dev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Wait for RESET completed TIMEOUT\n");
> + Â Â Â Â Â Â Â Â Â Â Â return -ETIMEDOUT;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â loops--;
> + Â Â Â Â Â Â Â udelay(LOOPS_USEC);
> + Â Â Â }
> +
> + Â Â Â /* set controller to device mode */
> + Â Â Â tmp = readl(&udc->op_regs->usbmode);
> + Â Â Â tmp |= USBMODE_CTRL_MODE_DEVICE;
> +
> + Â Â Â /* turn setup lockout off, require setup tripwire in usbcmd */
> + Â Â Â tmp |= USBMODE_SETUP_LOCK_OFF | USBMODE_STREAM_DISABLE;
> +
> + Â Â Â writel(tmp, &udc->op_regs->usbmode);
> +
> + Â Â Â writel(0x0, &udc->op_regs->epsetupstat);
> +
> + Â Â Â /* Configure the Endpoint List Address */
> + Â Â Â writel(udc->ep_dqh_dma & USB_EP_LIST_ADDRESS_MASK,
> + Â Â Â Â Â Â Â &udc->op_regs->eplistaddr);
> +
> + Â Â Â portsc = readl(&udc->op_regs->portsc[0]);
> + Â Â Â if (readl(&udc->cap_regs->hcsparams) & HCSPARAMS_PPC)
> + Â Â Â Â Â Â Â portsc &= (~PORTSCX_W1C_BITS | ~PORTSCX_PORT_POWER);
> +
> + Â Â Â if (udc->force_fs)
> + Â Â Â Â Â Â Â portsc |= PORTSCX_FORCE_FULL_SPEED_CONNECT;
> + Â Â Â else
> + Â Â Â Â Â Â Â portsc &= (~PORTSCX_FORCE_FULL_SPEED_CONNECT);
> +
> + Â Â Â writel(portsc, &udc->op_regs->portsc[0]);
> +
> + Â Â Â tmp = readl(&udc->op_regs->epctrlx[0]);
> + Â Â Â tmp &= ~(EPCTRL_TX_EP_STALL | EPCTRL_RX_EP_STALL);
> + Â Â Â writel(tmp, &udc->op_regs->epctrlx[0]);
> +
> + Â Â Â return 0;
> +}
> +
> +static int mv_udc_get_frame(struct usb_gadget *gadget)
> +{
> + Â Â Â struct mv_udc *udc;
> + Â Â Â u16 Â Â retval;
> +
> + Â Â Â if (!gadget)
> + Â Â Â Â Â Â Â return -ENODEV;
> +
> + Â Â Â udc = container_of(gadget, struct mv_udc, gadget);
> +
> + Â Â Â retval = readl(udc->op_regs->frindex) & USB_FRINDEX_MASKS;
> +
> + Â Â Â return retval;
> +}
> +
> +/*-----------------------------------------------------------------------
> + * Tries to wake up the host connected to this gadget
> + -----------------------------------------------------------------------*/
> +static int mv_udc_wakeup(struct usb_gadget *gadget)
> +{
> + Â Â Â struct mv_udc *udc = container_of(gadget, struct mv_udc, gadget);
> + Â Â Â u32 portsc;
> +
> + Â Â Â /* Remote wakeup feature not enabled by host */
> + Â Â Â if (!udc->remote_wakeup)
> + Â Â Â Â Â Â Â return -ENOTSUPP;
> +
> + Â Â Â portsc = readl(&udc->op_regs->portsc);
> + Â Â Â /* not suspended? */
> + Â Â Â if (!(portsc & PORTSCX_PORT_SUSPEND))
> + Â Â Â Â Â Â Â return 0;
> + Â Â Â /* trigger force resume */
> + Â Â Â portsc |= PORTSCX_PORT_FORCE_RESUME;
> + Â Â Â writel(portsc, &udc->op_regs->portsc[0]);
> + Â Â Â return 0;
> +}
> +
> +static int mv_udc_pullup(struct usb_gadget *gadget, int is_on)
> +{
> + Â Â Â struct mv_udc *udc;
> + Â Â Â unsigned long flags;
> +
> + Â Â Â udc = container_of(gadget, struct mv_udc, gadget);
> + Â Â Â spin_lock_irqsave(&udc->lock, flags);
> +
> + Â Â Â udc->softconnect = (is_on != 0);
> + Â Â Â if (udc->driver && udc->softconnect)
> + Â Â Â Â Â Â Â udc_start(udc);
> + Â Â Â else
> + Â Â Â Â Â Â Â udc_stop(udc);
> +
> + Â Â Â spin_unlock_irqrestore(&udc->lock, flags);
> + Â Â Â return 0;
> +}
> +
> +/* device controller usb_gadget_ops structure */
> +static const struct usb_gadget_ops mv_ops = {
> +
> + Â Â Â /* returns the current frame number */
> +    .get_frame   Â= mv_udc_get_frame,
> +
> + Â Â Â /* tries to wake up the host connected to this gadget */
> +    .wakeup     = mv_udc_wakeup,
> +
> + Â Â Â /* D+ pullup, software-controlled connect/disconnect to USB host */
> +    .pullup     = mv_udc_pullup,
> +};
> +
> +static void mv_udc_testmode(struct mv_udc *udc, u16 index, bool enter)
> +{
> + Â Â Â dev_info(&udc->dev->dev, "Test Mode is not support yet\n");
> +}
> +
> +static int eps_init(struct mv_udc *udc)
> +{
> +    struct mv_ep  Â*ep;
> + Â Â Â char name[14];
> + Â Â Â int i;
> +
> + Â Â Â /* initialize ep0 */
> + Â Â Â ep = &udc->eps[0];
> + Â Â Â ep->udc = udc;
> + Â Â Â strncpy(ep->name, "ep0", sizeof(ep->name));
> + Â Â Â ep->ep.name = ep->name;
> + Â Â Â ep->ep.ops = &mv_ep_ops;
> + Â Â Â ep->stopped = 0;
> + Â Â Â ep->ep.maxpacket = EP0_MAX_PKT_SIZE;
> + Â Â Â ep->ep_num = 0;
> + Â Â Â ep->desc = &mv_ep0_desc;
> + Â Â Â INIT_LIST_HEAD(&ep->queue);
> +
> + Â Â Â ep->ep_type = USB_ENDPOINT_XFER_CONTROL;
> +
> + Â Â Â /* initialize other endpoints */
> + Â Â Â for (i = 2; i < udc->max_eps * 2; i++) {
> + Â Â Â Â Â Â Â ep = &udc->eps[i];
> + Â Â Â Â Â Â Â if (i % 2) {
> + Â Â Â Â Â Â Â Â Â Â Â snprintf(name, sizeof(name), "ep%din", i / 2);
> + Â Â Â Â Â Â Â Â Â Â Â ep->direction = EP_DIR_IN;
> + Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â snprintf(name, sizeof(name), "ep%dout", i / 2);
> + Â Â Â Â Â Â Â Â Â Â Â ep->direction = EP_DIR_OUT;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â ep->udc = udc;
> + Â Â Â Â Â Â Â strncpy(ep->name, name, sizeof(ep->name));
> + Â Â Â Â Â Â Â ep->ep.name = ep->name;
> +
> + Â Â Â Â Â Â Â ep->ep.ops = &mv_ep_ops;
> + Â Â Â Â Â Â Â ep->stopped = 0;
> + Â Â Â Â Â Â Â ep->ep.maxpacket = (unsigned short) ~0;
> + Â Â Â Â Â Â Â ep->ep_num = i / 2;
> +
> + Â Â Â Â Â Â Â INIT_LIST_HEAD(&ep->queue);
> + Â Â Â Â Â Â Â list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
> +
> + Â Â Â Â Â Â Â ep->dqh = &udc->ep_dqh[i];
> + Â Â Â }
> +
> + Â Â Â return 0;
> +}
> +
> +/* delete all endpoint requests, called with spinlock held */
> +static void nuke(struct mv_ep *ep, int status)
> +{
> + Â Â Â /* called with spinlock held */
> + Â Â Â ep->stopped = 1;
> +
> + Â Â Â /* endpoint fifo flush */
> + Â Â Â mv_ep_fifo_flush(&ep->ep);
> +
> + Â Â Â while (!list_empty(&ep->queue)) {
> + Â Â Â Â Â Â Â struct mv_req *req = NULL;
> + Â Â Â Â Â Â Â req = list_entry(ep->queue.next, struct mv_req, queue);
> + Â Â Â Â Â Â Â done(ep, req, status);
> + Â Â Â }
> +}
> +
> +/* stop all USB activities */
> +static void stop_activity(struct mv_udc *udc, struct usb_gadget_driver *driver)
> +{
> +    struct mv_ep  Â*ep;
> +
> + Â Â Â nuke(&udc->eps[0], -ESHUTDOWN);
> +
> + Â Â Â list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
> + Â Â Â Â Â Â Â nuke(ep, -ESHUTDOWN);
> + Â Â Â }
> +
> + Â Â Â /* report disconnect; the driver is already quiesced */
> + Â Â Â if (driver) {
> + Â Â Â Â Â Â Â spin_unlock(&udc->lock);
> + Â Â Â Â Â Â Â driver->disconnect(&udc->gadget);
> + Â Â Â Â Â Â Â spin_lock(&udc->lock);
> + Â Â Â }
> +}
> +
> +int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
> + Â Â Â Â Â Â Â int (*bind)(struct usb_gadget *))
> +{
> + Â Â Â struct mv_udc *udc = the_controller;
> + Â Â Â int retval = 0;
> + Â Â Â unsigned long flags;
> +
> + Â Â Â if (!udc)
> + Â Â Â Â Â Â Â return -ENODEV;
> +
> + Â Â Â if (udc->driver)
> + Â Â Â Â Â Â Â return -EBUSY;
> +
> + Â Â Â spin_lock_irqsave(&udc->lock, flags);
> +
> + Â Â Â /* hook up the driver ... */
> + Â Â Â driver->driver.bus = NULL;
> + Â Â Â udc->driver = driver;
> + Â Â Â udc->gadget.dev.driver = &driver->driver;
> +
> + Â Â Â udc->usb_state = USB_STATE_ATTACHED;
> + Â Â Â udc->ep0_state = WAIT_FOR_SETUP;
> + Â Â Â udc->ep0_dir = USB_DIR_OUT;
> +
> + Â Â Â spin_unlock_irqrestore(&udc->lock, flags);
> +
> + Â Â Â retval = bind(&udc->gadget);
> + Â Â Â if (retval) {
> + Â Â Â Â Â Â Â dev_err(&udc->dev->dev, "bind to driver %s --> %d\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â driver->driver.name, retval);
> + Â Â Â Â Â Â Â udc->driver = NULL;
> + Â Â Â Â Â Â Â udc->gadget.dev.driver = NULL;
> + Â Â Â Â Â Â Â return retval;
> + Â Â Â }
> + Â Â Â udc_reset(udc);
> + Â Â Â ep0_reset(udc);
> + Â Â Â udc_start(udc);
> +
> + Â Â Â return 0;
> +}
> +EXPORT_SYMBOL(usb_gadget_probe_driver);
> +
> +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
> +{
> + Â Â Â struct mv_udc *udc = the_controller;
> + Â Â Â unsigned long flags;
> +
> + Â Â Â if (!udc)
> + Â Â Â Â Â Â Â return -ENODEV;
> +
> + Â Â Â udc_stop(udc);
> +
> + Â Â Â spin_lock_irqsave(&udc->lock, flags);
> +
> + Â Â Â /* stop all usb activities */
> + Â Â Â udc->gadget.speed = USB_SPEED_UNKNOWN;
> + Â Â Â stop_activity(udc, driver);
> + Â Â Â spin_unlock_irqrestore(&udc->lock, flags);
> +
> + Â Â Â /* unbind gadget driver */
> + Â Â Â driver->unbind(&udc->gadget);
> + Â Â Â udc->gadget.dev.driver = NULL;
> + Â Â Â udc->driver = NULL;
> +
> + Â Â Â return 0;
> +}
> +EXPORT_SYMBOL(usb_gadget_unregister_driver);
> +
> +static int
> +udc_prime_status(struct mv_udc *udc, u8 direction, u16 status, bool empty)
> +{
> + Â Â Â int retval = 0;
> + Â Â Â struct mv_req *req;
> + Â Â Â struct mv_ep *ep;
> +
> + Â Â Â ep = &udc->eps[0];
> + Â Â Â udc->ep0_dir = direction;
> +
> + Â Â Â req = udc->status_req;
> +
> + Â Â Â /* fill in the reqest structure */
> + Â Â Â if (empty == false) {
> + Â Â Â Â Â Â Â *((u16 *) req->req.buf) = cpu_to_le16(status);
> + Â Â Â Â Â Â Â req->req.length = 2;
> + Â Â Â } else
> + Â Â Â Â Â Â Â req->req.length = 0;
> +
> + Â Â Â req->ep = ep;
> + Â Â Â req->req.status = -EINPROGRESS;
> + Â Â Â req->req.actual = 0;
> + Â Â Â req->req.complete = NULL;
> + Â Â Â req->dtd_count = 0;
> +
> + Â Â Â /* prime the data phase */
> + Â Â Â if (!req_to_dtd(req))
> + Â Â Â Â Â Â Â retval = queue_dtd(ep, req);
> + Â Â Â else{ Â /* no mem */
> + Â Â Â Â Â Â Â retval = -ENOMEM;
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â if (retval) {
> + Â Â Â Â Â Â Â dev_err(&udc->dev->dev, "response error on GET_STATUS request\n");
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â list_add_tail(&req->queue, &ep->queue);
> +
> + Â Â Â return 0;
> +out:
> + Â Â Â return retval;
> +}
> +
> +static void ch9setaddress(struct mv_udc *udc, struct usb_ctrlrequest *setup)
> +{
> + Â Â Â udc->dev_addr = (u8)setup->wValue;
> +
> + Â Â Â /* update usb state */
> + Â Â Â udc->usb_state = USB_STATE_ADDRESS;
> +
> + Â Â Â if (udc_prime_status(udc, EP_DIR_IN, 0, true))
> + Â Â Â Â Â Â Â ep0_stall(udc);
> +}
> +
> +static void ch9getstatus(struct mv_udc *udc, u8 ep_num,
> + Â Â Â struct usb_ctrlrequest *setup)
> +{
> + Â Â Â u16 status;
> + Â Â Â int retval;
> +
> + Â Â Â if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
> + Â Â Â Â Â Â Â != (USB_DIR_IN | USB_TYPE_STANDARD))
> + Â Â Â Â Â Â Â return;
> +
> + Â Â Â if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
> + Â Â Â Â Â Â Â status = 1 << USB_DEVICE_SELF_POWERED;
> + Â Â Â Â Â Â Â status |= udc->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP;
> + Â Â Â } else if ((setup->bRequestType & USB_RECIP_MASK)
> + Â Â Â Â Â Â Â Â Â Â Â == USB_RECIP_INTERFACE) {
> + Â Â Â Â Â Â Â /* get interface status */
> + Â Â Â Â Â Â Â status = 0;
> + Â Â Â } else if ((setup->bRequestType & USB_RECIP_MASK)
> + Â Â Â Â Â Â Â Â Â Â Â == USB_RECIP_ENDPOINT) {
> + Â Â Â Â Â Â Â u8 ep_num, direction;
> +
> + Â Â Â Â Â Â Â ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
> + Â Â Â Â Â Â Â direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ? EP_DIR_IN : EP_DIR_OUT;
> + Â Â Â Â Â Â Â status = ep_is_stall(udc, ep_num, direction)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â << USB_ENDPOINT_HALT;
> + Â Â Â }
> +
> + Â Â Â retval = udc_prime_status(udc, EP_DIR_IN, status, false);
> + Â Â Â if (retval)
> + Â Â Â Â Â Â Â ep0_stall(udc);
> +}
> +
> +static void ch9clearfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
> +{
> + Â Â Â u8 ep_num;
> + Â Â Â u8 direction;
> +
> + Â Â Â if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
> + Â Â Â Â Â Â Â == ((USB_TYPE_STANDARD | USB_RECIP_DEVICE))) {
> + Â Â Â Â Â Â Â switch (setup->wValue) {
> + Â Â Â Â Â Â Â case USB_DEVICE_REMOTE_WAKEUP:
> + Â Â Â Â Â Â Â Â Â Â Â udc->remote_wakeup = 0;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â case USB_DEVICE_TEST_MODE:
> + Â Â Â Â Â Â Â Â Â Â Â mv_udc_testmode(udc, 0, false);
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â default:
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> + Â Â Â } else if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
> + Â Â Â Â Â Â Â == ((USB_TYPE_STANDARD | USB_RECIP_ENDPOINT))) {
> + Â Â Â Â Â Â Â switch (setup->wValue) {
> + Â Â Â Â Â Â Â case USB_ENDPOINT_HALT:
> + Â Â Â Â Â Â Â Â Â Â Â ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
> + Â Â Â Â Â Â Â Â Â Â Â direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ? EP_DIR_IN : EP_DIR_OUT;
> + Â Â Â Â Â Â Â Â Â Â Â if (setup->wValue != 0 || setup->wLength != 0
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â || ep_num > udc->max_eps)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â Â Â Â Â spin_unlock(&udc->lock);
> + Â Â Â Â Â Â Â Â Â Â Â ep_set_stall(udc, ep_num, direction, 0);
> + Â Â Â Â Â Â Â Â Â Â Â spin_lock(&udc->lock);
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â default:
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> + Â Â Â } else
> + Â Â Â Â Â Â Â goto out;
> +
> + Â Â Â if (udc_prime_status(udc, EP_DIR_IN, 0, true))
> + Â Â Â Â Â Â Â ep0_stall(udc);
> + Â Â Â else
> + Â Â Â Â Â Â Â udc->ep0_state = DATA_STATE_XMIT;
> +out:
> + Â Â Â return;
> +}
> +
> +static void ch9setfeature(struct mv_udc *udc, struct usb_ctrlrequest *setup)
> +{
> + Â Â Â u8 ep_num;
> + Â Â Â u8 direction;
> +
> + Â Â Â if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
> + Â Â Â Â Â Â Â == ((USB_TYPE_STANDARD | USB_RECIP_DEVICE))) {
> + Â Â Â Â Â Â Â switch (setup->wValue) {
> + Â Â Â Â Â Â Â case USB_DEVICE_REMOTE_WAKEUP:
> + Â Â Â Â Â Â Â Â Â Â Â udc->remote_wakeup = 1;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â case USB_DEVICE_TEST_MODE:
> + Â Â Â Â Â Â Â Â Â Â Â if (setup->wIndex & 0xFF
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â && udc->gadget.speed != USB_SPEED_HIGH)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â Â Â Â Â if (udc->usb_state == USB_STATE_CONFIGURED
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â || udc->usb_state == USB_STATE_ADDRESS
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â || udc->usb_state == USB_STATE_DEFAULT)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â mv_udc_testmode(udc,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â setup->wIndex & 0xFF00, true);
> + Â Â Â Â Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â default:
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> + Â Â Â } else if ((setup->bRequestType & (USB_TYPE_MASK | USB_RECIP_MASK))
> + Â Â Â Â Â Â Â == ((USB_TYPE_STANDARD | USB_RECIP_ENDPOINT))) {
> + Â Â Â Â Â Â Â switch (setup->wValue) {
> + Â Â Â Â Â Â Â case USB_ENDPOINT_HALT:
> + Â Â Â Â Â Â Â Â Â Â Â ep_num = setup->wIndex & USB_ENDPOINT_NUMBER_MASK;
> + Â Â Â Â Â Â Â Â Â Â Â direction = (setup->wIndex & USB_ENDPOINT_DIR_MASK)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ? EP_DIR_IN : EP_DIR_OUT;
> + Â Â Â Â Â Â Â Â Â Â Â if (setup->wValue != 0 || setup->wLength != 0
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â || ep_num > udc->max_eps)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â Â Â Â Â spin_unlock(&udc->lock);
> + Â Â Â Â Â Â Â Â Â Â Â ep_set_stall(udc, ep_num, direction, 1);
> + Â Â Â Â Â Â Â Â Â Â Â spin_lock(&udc->lock);
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â default:
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> + Â Â Â } else
> + Â Â Â Â Â Â Â goto out;
> +
> + Â Â Â if (udc_prime_status(udc, EP_DIR_IN, 0, true))
> + Â Â Â Â Â Â Â ep0_stall(udc);
> +out:
> + Â Â Â return;
> +}
> +
> +static void handle_setup_packet(struct mv_udc *udc, u8 ep_num,
> + Â Â Â struct usb_ctrlrequest *setup)
> +{
> + Â Â Â bool delegate = false;
> +
> + Â Â Â nuke(&udc->eps[ep_num * 2 + EP_DIR_OUT], -ESHUTDOWN);
> +
> + Â Â Â dev_dbg(&udc->dev->dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
> + Â Â Â Â Â Â Â Â Â Â Â setup->bRequestType, setup->bRequest,
> + Â Â Â Â Â Â Â Â Â Â Â setup->wValue, setup->wIndex, setup->wLength);
> + Â Â Â /* We process some stardard setup requests here */
> + Â Â Â if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
> + Â Â Â Â Â Â Â switch (setup->bRequest) {
> + Â Â Â Â Â Â Â case USB_REQ_GET_STATUS:
> + Â Â Â Â Â Â Â Â Â Â Â ch9getstatus(udc, ep_num, setup);
> + Â Â Â Â Â Â Â Â Â Â Â break;
> +
> + Â Â Â Â Â Â Â case USB_REQ_SET_ADDRESS:
> + Â Â Â Â Â Â Â Â Â Â Â ch9setaddress(udc, setup);
> + Â Â Â Â Â Â Â Â Â Â Â break;
> +
> + Â Â Â Â Â Â Â case USB_REQ_CLEAR_FEATURE:
> + Â Â Â Â Â Â Â Â Â Â Â ch9clearfeature(udc, setup);
> + Â Â Â Â Â Â Â Â Â Â Â break;
> +
> + Â Â Â Â Â Â Â case USB_REQ_SET_FEATURE:
> + Â Â Â Â Â Â Â Â Â Â Â ch9setfeature(udc, setup);
> + Â Â Â Â Â Â Â Â Â Â Â break;
> +
> + Â Â Â Â Â Â Â default:
> + Â Â Â Â Â Â Â Â Â Â Â delegate = true;
> + Â Â Â Â Â Â Â }
> + Â Â Â } else
> + Â Â Â Â Â Â Â delegate = true;
> +
> + Â Â Â /* delegate USB standard requests to the gadget driver */
> + Â Â Â if (delegate == true) {
> + Â Â Â Â Â Â Â /* USB requests handled by gadget */
> + Â Â Â Â Â Â Â if (setup->wLength) {
> + Â Â Â Â Â Â Â Â Â Â Â /* DATA phase from gadget, STATUS phase from udc */
> + Â Â Â Â Â Â Â Â Â Â Â udc->ep0_dir = (setup->bRequestType & USB_DIR_IN)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ? ÂEP_DIR_IN : EP_DIR_OUT;
> + Â Â Â Â Â Â Â Â Â Â Â spin_unlock(&udc->lock);
> + Â Â Â Â Â Â Â Â Â Â Â if (udc->driver->setup(&udc->gadget,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &udc->local_setup_buff) < 0)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ep0_stall(udc);
> + Â Â Â Â Â Â Â Â Â Â Â spin_lock(&udc->lock);
> + Â Â Â Â Â Â Â Â Â Â Â udc->ep0_state = (setup->bRequestType & USB_DIR_IN)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ? ÂDATA_STATE_XMIT : DATA_STATE_RECV;
> + Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â /* no DATA phase, IN STATUS phase from gadget */
> + Â Â Â Â Â Â Â Â Â Â Â udc->ep0_dir = EP_DIR_IN;
> + Â Â Â Â Â Â Â Â Â Â Â spin_unlock(&udc->lock);
> + Â Â Â Â Â Â Â Â Â Â Â if (udc->driver->setup(&udc->gadget,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &udc->local_setup_buff) < 0)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ep0_stall(udc);
> + Â Â Â Â Â Â Â Â Â Â Â spin_lock(&udc->lock);
> + Â Â Â Â Â Â Â Â Â Â Â udc->ep0_state = WAIT_FOR_OUT_STATUS;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +}
> +
> +/* complete DATA or STATUS phase of ep0 prime status phase if needed */
> +static void ep0_req_complete(struct mv_udc *udc,
> + Â Â Â struct mv_ep *ep0, struct mv_req *req)
> +{
> + Â Â Â u32 new_addr;
> +
> + Â Â Â if (udc->usb_state == USB_STATE_ADDRESS) {
> + Â Â Â Â Â Â Â /* set the new address */
> + Â Â Â Â Â Â Â new_addr = (u32)udc->dev_addr;
> + Â Â Â Â Â Â Â writel(new_addr << USB_DEVICE_ADDRESS_BIT_SHIFT,
> + Â Â Â Â Â Â Â Â Â Â Â &udc->op_regs->deviceaddr);
> + Â Â Â }
> +
> + Â Â Â done(ep0, req, 0);
> +
> + Â Â Â switch (udc->ep0_state) {
> + Â Â Â case DATA_STATE_XMIT:
> + Â Â Â Â Â Â Â /* receive status phase */
> + Â Â Â Â Â Â Â if (udc_prime_status(udc, EP_DIR_OUT, 0, true))
> + Â Â Â Â Â Â Â Â Â Â Â ep0_stall(udc);
> + Â Â Â Â Â Â Â break;
> + Â Â Â case DATA_STATE_RECV:
> + Â Â Â Â Â Â Â /* send status phase */
> + Â Â Â Â Â Â Â if (udc_prime_status(udc, EP_DIR_IN, 0 , true))
> + Â Â Â Â Â Â Â Â Â Â Â ep0_stall(udc);
> + Â Â Â Â Â Â Â break;
> + Â Â Â case WAIT_FOR_OUT_STATUS:
> + Â Â Â Â Â Â Â udc->ep0_state = WAIT_FOR_SETUP;
> + Â Â Â Â Â Â Â break;
> + Â Â Â case WAIT_FOR_SETUP:
> + Â Â Â Â Â Â Â dev_err(&udc->dev->dev, "unexpect ep0 packets\n");
> + Â Â Â Â Â Â Â break;
> + Â Â Â default:
> + Â Â Â Â Â Â Â ep0_stall(udc);
> + Â Â Â Â Â Â Â break;
> + Â Â Â }
> +}
> +
> +static void get_setup_data(struct mv_udc *udc, u8 ep_num, u8 *buffer_ptr)
> +{
> + Â Â Â u32 temp;
> + Â Â Â struct mv_dqh *dqh;
> +
> + Â Â Â dqh = &udc->ep_dqh[ep_num * 2 + EP_DIR_OUT];
> +
> + Â Â Â /* Clear bit in ENDPTSETUPSTAT */
> + Â Â Â temp = readl(&udc->op_regs->epsetupstat);
> + Â Â Â writel(temp | (1 << ep_num), &udc->op_regs->epsetupstat);
> +
> + Â Â Â /* while a hazard exists when setup package arrives */
> + Â Â Â do {
> + Â Â Â Â Â Â Â /* Set Setup Tripwire */
> + Â Â Â Â Â Â Â temp = readl(&udc->op_regs->usbcmd);
> + Â Â Â Â Â Â Â writel(temp | USBCMD_SETUP_TRIPWIRE_SET, &udc->op_regs->usbcmd);
> +
> + Â Â Â Â Â Â Â /* Copy the setup packet to local buffer */
> + Â Â Â Â Â Â Â memcpy(buffer_ptr, (u8 *) dqh->setup_buffer, 8);
> + Â Â Â } while (!(readl(&udc->op_regs->usbcmd) & USBCMD_SETUP_TRIPWIRE_SET));
> +
> + Â Â Â /* Clear Setup Tripwire */
> + Â Â Â temp = readl(&udc->op_regs->usbcmd);
> + Â Â Â writel(temp & ~USBCMD_SETUP_TRIPWIRE_SET, &udc->op_regs->usbcmd);
> +}
> +
> +static void irq_process_tr_complete(struct mv_udc *udc)
> +{
> + Â Â Â u32 tmp, bit_pos;
> + Â Â Â int i, ep_num = 0, direction = 0;
> +    struct mv_ep  Â*curr_ep;
> + Â Â Â struct mv_req *curr_req, *temp_req;
> + Â Â Â int status;
> +
> + Â Â Â /* We use separate loops for ENDPTSETUPSTAT and ENDPTCOMPLETE
> + Â Â Â Â* because the setup packets are to be read ASAP
> + Â Â Â Â*/
> +
> + Â Â Â /* Process all Setup packet received interrupts */
> + Â Â Â tmp = readl(&udc->op_regs->epsetupstat);
> +
> + Â Â Â if (tmp) {
> + Â Â Â Â Â Â Â for (i = 0; i < udc->max_eps; i++) {
> + Â Â Â Â Â Â Â Â Â Â Â if (tmp & (1 << i)) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â get_setup_data(udc, i,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (u8 *)(&udc->local_setup_buff));
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â handle_setup_packet(udc, i,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &udc->local_setup_buff);
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +
> + Â Â Â /* Don't clear the endpoint setup status register here.
> + Â Â Â Â* It is cleared as a setup packet is read out of the buffer
> + Â Â Â Â*/
> +
> + Â Â Â /* Process non-setup transaction complete interrupts */
> + Â Â Â tmp = readl(&udc->op_regs->epcomplete);
> +
> + Â Â Â if (!tmp)
> + Â Â Â Â Â Â Â return;
> +
> + Â Â Â writel(tmp, &udc->op_regs->epcomplete);
> +
> + Â Â Â for (i = 0; i < udc->max_eps * 2; i++) {
> + Â Â Â Â Â Â Â ep_num = i >> 1;
> + Â Â Â Â Â Â Â direction = i % 2;
> +
> + Â Â Â Â Â Â Â bit_pos = 1 << (ep_num + 16 * direction);
> +
> + Â Â Â Â Â Â Â if (!(bit_pos & tmp))
> + Â Â Â Â Â Â Â Â Â Â Â continue;
> +
> + Â Â Â Â Â Â Â if (i == 1)
> + Â Â Â Â Â Â Â Â Â Â Â curr_ep = &udc->eps[0];
> + Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â Â curr_ep = &udc->eps[i];
> + Â Â Â Â Â Â Â /* process the req queue until an uncomplete request */
> + Â Â Â Â Â Â Â list_for_each_entry_safe(curr_req, temp_req,
> + Â Â Â Â Â Â Â Â Â Â Â &curr_ep->queue, queue) {
> + Â Â Â Â Â Â Â Â Â Â Â status = process_ep_req(udc, i, curr_req);
> + Â Â Â Â Â Â Â Â Â Â Â if (status)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break;
> +
> + Â Â Â Â Â Â Â Â Â Â Â /* write back status to req */
> + Â Â Â Â Â Â Â Â Â Â Â curr_req->req.status = status;
> +
> + Â Â Â Â Â Â Â Â Â Â Â /* ep0 request completion */
> + Â Â Â Â Â Â Â Â Â Â Â if (ep_num == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ep0_req_complete(udc, curr_ep, curr_req);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â done(curr_ep, curr_req, status);
> + Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +}
> +
> +void irq_process_reset(struct mv_udc *udc)
> +{
> + Â Â Â u32 tmp;
> + Â Â Â unsigned int loops;
> +
> + Â Â Â udc->ep0_dir = EP_DIR_OUT;
> + Â Â Â udc->ep0_state = WAIT_FOR_SETUP;
> + Â Â Â udc->remote_wakeup = 0; Â Â Â Â /* default to 0 on reset */
> +
> + Â Â Â /* The address bits are past bit 25-31. Set the address */
> + Â Â Â tmp = readl(&udc->op_regs->deviceaddr);
> + Â Â Â tmp &= ~(USB_DEVICE_ADDRESS_MASK);
> + Â Â Â writel(tmp, &udc->op_regs->deviceaddr);
> +
> + Â Â Â /* Clear all the setup token semaphores */
> + Â Â Â tmp = readl(&udc->op_regs->epsetupstat);
> + Â Â Â writel(tmp, &udc->op_regs->epsetupstat);
> +
> + Â Â Â /* Clear all the endpoint complete status bits */
> + Â Â Â tmp = readl(&udc->op_regs->epcomplete);
> + Â Â Â writel(tmp, &udc->op_regs->epcomplete);
> +
> + Â Â Â /* wait until all endptprime bits cleared */
> + Â Â Â loops = LOOPS(PRIME_TIMEOUT);
> + Â Â Â while (readl(&udc->op_regs->epprime) & 0xFFFFFFFF) {
> + Â Â Â Â Â Â Â if (loops == 0) {
> + Â Â Â Â Â Â Â Â Â Â Â dev_err(&udc->dev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Timeout for ENDPTPRIME = 0x%x\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â readl(&udc->op_regs->epprime));
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â loops--;
> + Â Â Â Â Â Â Â udelay(LOOPS_USEC);
> + Â Â Â }
> +
> + Â Â Â /* Write 1s to the Flush register */
> + Â Â Â writel((u32)~0, &udc->op_regs->epflush);
> +
> + Â Â Â if (readl(&udc->op_regs->portsc[0]) & PORTSCX_PORT_RESET) {
> + Â Â Â Â Â Â Â dev_info(&udc->dev->dev, "usb bus reset\n");
> + Â Â Â Â Â Â Â udc->usb_state = USB_STATE_DEFAULT;
> + Â Â Â Â Â Â Â /* reset all the queues, stop all USB activities */
> + Â Â Â Â Â Â Â stop_activity(udc, udc->driver);
> + Â Â Â } else {
> + Â Â Â Â Â Â Â dev_info(&udc->dev->dev, "USB reset portsc 0x%x\n",
> + Â Â Â Â Â Â Â Â Â Â Â readl(&udc->op_regs->portsc));
> +
> + Â Â Â Â Â Â Â /* re-initialize */
> + Â Â Â Â Â Â Â /* controller reset */
> + Â Â Â Â Â Â Â udc_reset(udc);
> +
> + Â Â Â Â Â Â Â /* reset all the queues, stop all USB activities */
> + Â Â Â Â Â Â Â stop_activity(udc, udc->driver);
> +
> + Â Â Â Â Â Â Â /* reset ep0 dQH and endptctrl */
> + Â Â Â Â Â Â Â ep0_reset(udc);
> +
> + Â Â Â Â Â Â Â /* enable interrupt and set controller to run state */
> + Â Â Â Â Â Â Â udc_start(udc);
> +
> + Â Â Â Â Â Â Â udc->usb_state = USB_STATE_ATTACHED;
> + Â Â Â }
> +}
> +
> +static void handle_bus_resume(struct mv_udc *udc)
> +{
> + Â Â Â udc->usb_state = udc->resume_state;
> + Â Â Â udc->resume_state = 0;
> +
> + Â Â Â /* report resume to the driver */
> + Â Â Â if (udc->driver) {
> + Â Â Â Â Â Â Â if (udc->driver->resume) {
> + Â Â Â Â Â Â Â Â Â Â Â spin_unlock(&udc->lock);
> + Â Â Â Â Â Â Â Â Â Â Â udc->driver->resume(&udc->gadget);
> + Â Â Â Â Â Â Â Â Â Â Â spin_lock(&udc->lock);
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +}
> +
> +static void irq_process_suspend(struct mv_udc *udc)
> +{
> + Â Â Â udc->resume_state = udc->usb_state;
> + Â Â Â udc->usb_state = USB_STATE_SUSPENDED;
> +
> + Â Â Â if (udc->driver->suspend) {
> + Â Â Â Â Â Â Â spin_unlock(&udc->lock);
> + Â Â Â Â Â Â Â udc->driver->suspend(&udc->gadget);
> + Â Â Â Â Â Â Â spin_lock(&udc->lock);
> + Â Â Â }
> +}
> +
> +static void irq_process_port_change(struct mv_udc *udc)
> +{
> + Â Â Â u32 portsc;
> +
> + Â Â Â portsc = readl(&udc->op_regs->portsc[0]);
> + Â Â Â if (!(portsc & PORTSCX_PORT_RESET)) {
> + Â Â Â Â Â Â Â /* Get the speed */
> + Â Â Â Â Â Â Â u32 speed = portsc & PORTSCX_PORT_SPEED_MASK;
> + Â Â Â Â Â Â Â switch (speed) {
> + Â Â Â Â Â Â Â case PORTSCX_PORT_SPEED_HIGH:
> + Â Â Â Â Â Â Â Â Â Â Â udc->gadget.speed = USB_SPEED_HIGH;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â case PORTSCX_PORT_SPEED_FULL:
> + Â Â Â Â Â Â Â Â Â Â Â udc->gadget.speed = USB_SPEED_FULL;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â case PORTSCX_PORT_SPEED_LOW:
> + Â Â Â Â Â Â Â Â Â Â Â udc->gadget.speed = USB_SPEED_LOW;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â default:
> + Â Â Â Â Â Â Â Â Â Â Â udc->gadget.speed = USB_SPEED_UNKNOWN;
> + Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +
> + Â Â Â if (portsc & PORTSCX_PORT_SUSPEND) {
> + Â Â Â Â Â Â Â udc->resume_state = udc->usb_state;
> + Â Â Â Â Â Â Â udc->usb_state = USB_STATE_SUSPENDED;
> + Â Â Â Â Â Â Â if (udc->driver->suspend) {
> + Â Â Â Â Â Â Â Â Â Â Â spin_unlock(&udc->lock);
> + Â Â Â Â Â Â Â Â Â Â Â udc->driver->suspend(&udc->gadget);
> + Â Â Â Â Â Â Â Â Â Â Â spin_lock(&udc->lock);
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +
> + Â Â Â if (!(portsc & PORTSCX_PORT_SUSPEND)
> + Â Â Â Â Â Â Â && udc->usb_state == USB_STATE_SUSPENDED) {
> + Â Â Â Â Â Â Â handle_bus_resume(udc);
> + Â Â Â }
> +
> + Â Â Â if (!udc->resume_state)
> + Â Â Â Â Â Â Â udc->usb_state = USB_STATE_DEFAULT;
> +}
> +
> +static void irq_process_error(struct mv_udc *udc)
> +{
> + Â Â Â /* Increment the error count */
> + Â Â Â udc->errors++;
> +}
> +
> +static irqreturn_t mv_udc_irq(int irq, void *dev)
> +{
> + Â Â Â struct mv_udc *udc = (struct mv_udc *)dev;
> + Â Â Â u32 status, intr;
> +
> + Â Â Â spin_lock(&udc->lock);
> +
> + Â Â Â status = readl(&udc->op_regs->usbsts);
> + Â Â Â intr = readl(&udc->op_regs->usbintr);
> + Â Â Â status &= intr;
> +
> + Â Â Â if (status == 0) {
> + Â Â Â Â Â Â Â spin_unlock(&udc->lock);
> + Â Â Â Â Â Â Â return IRQ_NONE;
> + Â Â Â }
> +
> + Â Â Â /* Clear all the interrupts occured */
> + Â Â Â writel(status, &udc->op_regs->usbsts);
> +
> + Â Â Â if (status & USBSTS_ERR)
> + Â Â Â Â Â Â Â irq_process_error(udc);
> +
> + Â Â Â if (status & USBSTS_RESET)
> + Â Â Â Â Â Â Â irq_process_reset(udc);
> +
> + Â Â Â if (status & USBSTS_PORT_CHANGE)
> + Â Â Â Â Â Â Â irq_process_port_change(udc);
> +
> + Â Â Â if (status & USBSTS_INT)
> + Â Â Â Â Â Â Â irq_process_tr_complete(udc);
> +
> + Â Â Â if (status & USBSTS_SUSPEND)
> + Â Â Â Â Â Â Â irq_process_suspend(udc);
> +
> + Â Â Â /* resume from suspend */
> + Â Â Â /*portsc = readl(&udc->op_regs->portsc[0]);
> + Â Â Â if (udc->usb_state == USB_STATE_SUSPENDED)
> + Â Â Â Â Â Â Â if (!(portsc & PORTSCX_PORT_SUSPEND))
> + Â Â Â Â Â Â Â handle_bus_resume(dev);*/
> +
> + Â Â Â spin_unlock(&udc->lock);
> +
> + Â Â Â return IRQ_HANDLED;
> +}
> +
> +/* release device structure */
> +static void gadget_release(struct device *_dev)
> +{
> + Â Â Â struct mv_udc *udc = the_controller;
> +
> + Â Â Â complete(udc->done);
> + Â Â Â kfree(udc);
> +}
> +
> +static int mv_udc_remove(struct platform_device *dev)
> +{
> + Â Â Â struct mv_udc *udc = the_controller;
> +
> + Â Â Â DECLARE_COMPLETION(done);
> +
> + Â Â Â udc->done = &done;
> +
> + Â Â Â /* free memory allocated in probe */
> + Â Â Â if (udc->dtd_pool)
> + Â Â Â Â Â Â Â dma_pool_destroy(udc->dtd_pool);
> +
> + Â Â Â if (udc->ep_dqh)
> + Â Â Â Â Â Â Â dma_free_coherent(&dev->dev, udc->ep_dqh_size,
> + Â Â Â Â Â Â Â Â Â Â Â udc->ep_dqh, udc->ep_dqh_dma);
> +
> + Â Â Â kfree(udc->eps);
> +
> + Â Â Â if (udc->irq)
> + Â Â Â Â Â Â Â free_irq(udc->irq, &dev->dev);
> +
> + Â Â Â if (udc->cap_regs)
> + Â Â Â Â Â Â Â iounmap(udc->cap_regs);
> + Â Â Â udc->cap_regs = NULL;
> +
> + Â Â Â if (udc->phy_regs)
> + Â Â Â Â Â Â Â iounmap((void *)udc->phy_regs);
> + Â Â Â udc->phy_regs = 0;
> +
> + Â Â Â if (udc->status_req) {
> + Â Â Â Â Â Â Â kfree(udc->status_req->req.buf);
> + Â Â Â Â Â Â Â kfree(udc->status_req);
> + Â Â Â }
> +
> + Â Â Â device_unregister(&udc->gadget.dev);
> +
> + Â Â Â /* free dev, wait for the release() finished */
> + Â Â Â wait_for_completion(&done);
> +
> + Â Â Â the_controller = NULL;
> +
> + Â Â Â return 0;
> +}
> +
> +int mv_udc_probe(struct platform_device *dev)
> +{
> + Â Â Â struct mv_udc *udc;
> + Â Â Â int retval = 0;
> + Â Â Â struct resource *r;
> + Â Â Â size_t size;
> +
> + Â Â Â udc = kzalloc(sizeof *udc, GFP_KERNEL);
> + Â Â Â if (udc == NULL) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "failed to allocate memory for udc\n");
> + Â Â Â Â Â Â Â retval = -ENOMEM;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> +
> + Â Â Â spin_lock_init(&udc->lock);
> +
> + Â Â Â udc->dev = dev;
> +
> + Â Â Â udc->clk = clk_get(&dev->dev, "U2OCLK");
> + Â Â Â if (IS_ERR(udc->clk)) {
> + Â Â Â Â Â Â Â retval = PTR_ERR(udc->clk);
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> +
> + Â Â Â r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "u2o");
> + Â Â Â if (r == NULL) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "no I/O memory resource defined\n");
> + Â Â Â Â Â Â Â retval = -ENODEV;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> +
> + Â Â Â udc->cap_regs = (struct mv_cap_regs __iomem *)
> + Â Â Â Â Â Â Â ioremap(r->start, resource_size(r));
> + Â Â Â if (udc->cap_regs == NULL) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "failed to map I/O memory\n");
> + Â Â Â Â Â Â Â retval = -EBUSY;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> +
> + Â Â Â r = platform_get_resource_byname(udc->dev, IORESOURCE_MEM, "u2ophy");
> + Â Â Â if (r == NULL) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "no phy I/O memory resource defined\n");
> + Â Â Â Â Â Â Â retval = -ENODEV;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> +
> + Â Â Â udc->phy_regs = (unsigned int)ioremap(r->start, resource_size(r));
> + Â Â Â if (udc->phy_regs == 0) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "failed to map phy I/O memory\n");
> + Â Â Â Â Â Â Â retval = -EBUSY;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> +
> + Â Â Â /* we will acces controller register, so enable the clk */
> + Â Â Â clk_enable(udc->clk);
> + Â Â Â retval = mv_udc_phy_init(udc->phy_regs);
> + Â Â Â if (retval) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "phy initialization error %d\n", retval);
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> +
> + Â Â Â udc->op_regs = (struct mv_op_regs __iomem *)((u32)udc->cap_regs
> + Â Â Â Â Â Â Â + (readl(&udc->cap_regs->caplength_hciversion)
> + Â Â Â Â Â Â Â Â Â Â Â & CAPLENGTH_MASK));
> + Â Â Â udc->max_eps = readl(&udc->cap_regs->dccparams) & DCCPARAMS_DEN_MASK;
> +
> + Â Â Â size = udc->max_eps * sizeof(struct mv_dqh) *2;
> + Â Â Â size = (size + DQH_ALIGNMENT - 1) & ~(DQH_ALIGNMENT - 1);
> + Â Â Â udc->ep_dqh = dma_alloc_coherent(&dev->dev, size,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &udc->ep_dqh_dma, GFP_KERNEL);
> +
> + Â Â Â if (udc->ep_dqh == NULL) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "allocate dQH memory failed\n");
> + Â Â Â Â Â Â Â retval = -ENOMEM;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> + Â Â Â udc->ep_dqh_size = size;
> +
> + Â Â Â /* create dTD dma_pool resource */
> + Â Â Â udc->dtd_pool = dma_pool_create("mv_dtd",
> + Â Â Â Â Â Â Â Â Â Â Â &dev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â sizeof(struct mv_dtd),
> + Â Â Â Â Â Â Â Â Â Â Â DTD_ALIGNMENT,
> + Â Â Â Â Â Â Â Â Â Â Â DMA_BOUNDARY);
> +
> + Â Â Â if (!udc->dtd_pool) {
> + Â Â Â Â Â Â Â retval = -ENOMEM;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> +
> + Â Â Â size = udc->max_eps * sizeof(struct mv_ep) *2;
> + Â Â Â udc->eps = kzalloc(size, GFP_KERNEL);
> + Â Â Â if (udc->eps == NULL) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "allocate ep memory failed\n");
> + Â Â Â Â Â Â Â retval = -ENOMEM;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> +
> + Â Â Â /* initialize ep0 status request structure */
> + Â Â Â udc->status_req = kzalloc(sizeof(struct mv_req), GFP_KERNEL);
> + Â Â Â if (!udc->status_req) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "allocate status_req memory failed\n");
> + Â Â Â Â Â Â Â retval = -ENOMEM;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> + Â Â Â INIT_LIST_HEAD(&udc->status_req->queue);
> +
> + Â Â Â /* allocate a small amount of memory to get valid address */
> + Â Â Â udc->status_req->req.buf = kzalloc(8, GFP_KERNEL);
> + Â Â Â udc->status_req->req.dma = virt_to_phys(udc->status_req->req.buf);
> +
> + Â Â Â udc->resume_state = USB_STATE_NOTATTACHED;
> + Â Â Â udc->usb_state = USB_STATE_POWERED;
> + Â Â Â udc->ep0_dir = EP_DIR_OUT;
> + Â Â Â udc->remote_wakeup = 0;
> +
> + Â Â Â r = platform_get_resource(udc->dev, IORESOURCE_IRQ, 0);
> + Â Â Â if (r == NULL) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "no IRQ resource defined\n");
> + Â Â Â Â Â Â Â retval = -ENODEV;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> + Â Â Â udc->irq = r->start;
> + Â Â Â if (request_irq(udc->irq, mv_udc_irq,
> + Â Â Â Â Â Â Â IRQF_DISABLED | IRQF_SHARED, driver_name, udc)) {
> + Â Â Â Â Â Â Â dev_err(&dev->dev, "Request irq %d for UDC failed\n",
> + Â Â Â Â Â Â Â Â Â Â Â udc->irq);
> + Â Â Â Â Â Â Â retval = -ENODEV;
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> +
> + Â Â Â /* initialize gadget structure */
> + Â Â Â udc->gadget.ops = &mv_ops; Â Â Â/* usb_gadget_ops */
> + Â Â Â udc->gadget.ep0 = &udc->eps[0].ep; Â Â Â/* gadget ep0 */
> + Â Â Â INIT_LIST_HEAD(&udc->gadget.ep_list); Â /* ep_list */
> + Â Â Â udc->gadget.speed = USB_SPEED_UNKNOWN; Â/* speed */
> + Â Â Â udc->gadget.is_dualspeed = 1; Â Â Â Â Â /* support dual speed */
> +
> + Â Â Â /* the "gadget" abstracts/virtualizes the controller */
> + Â Â Â dev_set_name(&udc->gadget.dev, "gadget");
> + Â Â Â udc->gadget.dev.parent = &dev->dev;
> + Â Â Â udc->gadget.dev.dma_mask = dev->dev.dma_mask;
> + Â Â Â udc->gadget.dev.release = gadget_release;
> + Â Â Â udc->gadget.name = driver_name; Â Â Â Â /* gadget name */
> +
> + Â Â Â retval = device_register(&udc->gadget.dev);
> + Â Â Â if (retval)
> + Â Â Â Â Â Â Â goto error;
> +
> + Â Â Â eps_init(udc);
> +
> + Â Â Â the_controller = udc;
> +
> + Â Â Â goto out;
> +error:
> + Â Â Â if (udc)
> + Â Â Â Â Â Â Â mv_udc_remove(udc->dev);
> +out:
> + Â Â Â return retval;
> +}
> +
> +#ifdef CONFIG_PM
> +static int mv_udc_suspend(struct platform_device *_dev, pm_message_t state)
> +{
> + Â Â Â struct mv_udc *udc = the_controller;
> +
> + Â Â Â udc_stop(udc);
> +
> + Â Â Â return 0;
> +}
> +
> +static int mv_udc_resume(struct platform_device *_dev)
> +{
> + Â Â Â struct mv_udc *udc = the_controller;
> + Â Â Â int retval;
> +
> + Â Â Â retval = mv_udc_phy_init(udc->phy_regs);
> + Â Â Â if (retval) {
> + Â Â Â Â Â Â Â dev_err(_dev, "phy initialization error %d\n", retval);
> + Â Â Â Â Â Â Â goto error;
> + Â Â Â }
> + Â Â Â udc_reset(udc);
> + Â Â Â ep0_reset(udc);
> + Â Â Â udc_start(udc);
> +
> + Â Â Â return 0;
> +}
> +
> +static const struct dev_pm_ops mv_udc_pm_ops = {
> +    .suspend    Â= mv_udc_suspend,
> +    .resume     = mv_udc_resume,
> +};
> +#endif
> +
> +static struct platform_driver udc_driver = {
> +    .probe     Â= mv_udc_probe,
> +    .remove     = __exit_p(mv_udc_remove),
> +    .driver     = {
> + Â Â Â Â Â Â Â .owner Â= THIS_MODULE,
> +        .name  = "pxa-u2o",
> +#ifdef CONFIG_PM
> +        .pm   = mv_udc_pm_ops,
> +#endif
> + Â Â Â },
> +};
> +
> +
> +MODULE_DESCRIPTION(DRIVER_DESC);
> +MODULE_AUTHOR("Chao Xie <chao.xie@xxxxxxxxxxx>");
> +MODULE_VERSION(DRIVER_VERSION);
> +MODULE_LICENSE("GPL");
> +
> +
> +static int __init init(void)
> +{
> + Â Â Â return platform_driver_register(&udc_driver);
> +}
> +module_init(init);
> +
> +
> +static void __exit cleanup(void)
> +{
> + Â Â Â platform_driver_unregister(&udc_driver);
> +}
> +module_exit(cleanup);
> +
> diff --git a/drivers/usb/gadget/mv_udc_phy.c b/drivers/usb/gadget/mv_udc_phy.c
> new file mode 100644
> index 0000000..b828bc1
> --- /dev/null
> +++ b/drivers/usb/gadget/mv_udc_phy.c
> @@ -0,0 +1,212 @@
> +#include <linux/delay.h>
> +#include <linux/timer.h>
> +#include <linux/io.h>
> +#include <linux/errno.h>
> +
> +#include <mach/cputype.h>
> +
> +#ifdef CONFIG_ARCH_MMP
> +
> +#define UTMI_REVISION Â Â Â Â Â0x0
> +#define UTMI_CTRL Â Â Â Â Â Â Â0x4
> +#define UTMI_PLL Â Â Â Â Â Â Â 0x8
> +#define UTMI_TX Â Â Â Â Â Â Â Â Â Â Â Â0xc
> +#define UTMI_RX Â Â Â Â Â Â Â Â Â Â Â Â0x10
> +#define UTMI_IVREF Â Â Â Â Â Â 0x14
> +#define UTMI_T0 Â Â Â Â Â Â Â Â Â Â Â Â0x18
> +#define UTMI_T1 Â Â Â Â Â Â Â Â Â Â Â Â0x1c
> +#define UTMI_T2 Â Â Â Â Â Â Â Â Â Â Â Â0x20
> +#define UTMI_T3 Â Â Â Â Â Â Â Â Â Â Â Â0x24
> +#define UTMI_T4 Â Â Â Â Â Â Â Â Â Â Â Â0x28
> +#define UTMI_T5 Â Â Â Â Â Â Â Â Â Â Â Â0x2c
> +#define UTMI_RESERVE Â Â Â Â Â 0x30
> +#define UTMI_USB_INT Â Â Â Â Â 0x34
> +#define UTMI_DBG_CTL Â Â Â Â Â 0x38
> +#define UTMI_OTG_ADDON Â Â Â Â 0x3c
> +
> +/* For UTMICTRL Register */
> +#define UTMI_CTRL_USB_CLK_EN Â Â Â Â Â Â Â Â Â (1 << 31)
> +/* pxa168 */
> +#define UTMI_CTRL_SUSPEND_SET1 Â Â Â Â Â Â Â Â (1 << 30)
> +#define UTMI_CTRL_SUSPEND_SET2 Â Â Â Â Â Â Â Â (1 << 29)
> +#define UTMI_CTRL_RXBUF_PDWN Â Â Â Â Â Â Â Â Â (1 << 24)
> +#define UTMI_CTRL_TXBUF_PDWN Â Â Â Â Â Â Â Â Â (1 << 11)
> +
> +#define UTMI_CTRL_INPKT_DELAY_SHIFT Â Â Â Â Â Â30
> +#define UTMI_CTRL_INPKT_DELAY_SOF_SHIFT Â Â Â Â Â Â Â Â28
> +#define UTMI_CTRL_PU_REF_SHIFT Â Â Â Â Â Â Â Â 20
> +#define UTMI_CTRL_ARC_PULLDN_SHIFT Â Â Â Â Â Â 12
> +#define UTMI_CTRL_PLL_PWR_UP_SHIFT Â Â Â Â Â Â 1
> +#define UTMI_CTRL_PWR_UP_SHIFT Â Â Â Â Â Â Â Â 0
> +/* For UTMI_PLL Register */
> +#define UTMI_PLL_CLK_BLK_EN_SHIFT Â Â Â Â Â Â Â24
> +#define UTMI_PLL_FBDIV_SHIFT Â Â Â Â Â Â Â Â Â 4
> +#define UTMI_PLL_REFDIV_SHIFT Â Â Â Â Â Â Â Â Â0
> +#define UTMI_PLL_FBDIV_MASK Â Â Â Â Â Â Â Â Â Â0x00000FF0
> +#define UTMI_PLL_REFDIV_MASK Â Â Â Â Â Â Â Â Â 0x0000000F
> +#define UTMI_PLL_ICP_MASK Â Â Â Â Â Â Â Â Â Â Â0x00007000
> +#define UTMI_PLL_KVCO_MASK Â Â Â Â Â Â Â Â Â Â 0x00031000
> +#define UTMI_PLL_PLLCALI12_SHIFT Â Â Â Â Â Â Â 29
> +#define UTMI_PLL_PLLCALI12_MASK Â Â Â Â Â Â Â Â Â Â Â Â(0x3 << 29)
> +#define UTMI_PLL_PLLVDD18_SHIFT Â Â Â Â Â Â Â Â Â Â Â Â27
> +#define UTMI_PLL_PLLVDD18_MASK Â Â Â Â Â Â Â Â (0x3 << 27)
> +#define UTMI_PLL_PLLVDD12_SHIFT Â Â Â Â Â Â Â Â Â Â Â Â25
> +#define UTMI_PLL_PLLVDD12_MASK Â Â Â Â Â Â Â Â (0x3 << 25)
> +#define UTMI_PLL_KVCO_SHIFT Â Â Â Â Â Â Â Â Â Â15
> +#define UTMI_PLL_ICP_SHIFT Â Â Â Â Â Â Â Â Â Â 12
> +/* For UTMI_TX Register */
> +#define UTMI_TX_REG_EXT_FS_RCAL_SHIFT Â Â Â Â Â27
> +#define UTMI_TX_REG_EXT_FS_RCAL_MASK Â Â Â Â Â (0xf << 27)
> +#define UTMI_TX_REG_EXT_FS_RCAL_EN_MASK Â Â Â Â Â Â Â Â26
> +#define UTMI_TX_REG_EXT_FS_RCAL_EN Â Â Â Â Â Â (0x1 << 26)
> +#define UTMI_TX_LOW_VDD_EN_SHIFT Â Â Â Â Â Â Â 11
> +#define UTMI_TX_IMPCAL_VTH_SHIFT Â Â Â Â Â Â Â 14
> +#define UTMI_TX_IMPCAL_VTH_MASK Â Â Â Â Â Â Â Â Â Â Â Â(0x7 << 14)
> +#define UTMI_TX_CK60_PHSEL_SHIFT Â Â Â Â Â Â Â 17
> +#define UTMI_TX_CK60_PHSEL_MASK Â Â Â Â Â Â Â Â Â Â Â Â(0xf << 17)
> +#define UTMI_TX_TXVDD12_SHIFT Â Â Â Â Â Â Â Â Â 22
> +#define UTMI_TX_TXVDD12_MASK Â Â Â Â Â Â Â Â Â (0x3 << 22)
> +#define UTMI_TX_AMP_SHIFT Â Â Â Â Â Â Â Â Â Â Â0
> +#define UTMI_TX_AMP_MASK Â Â Â Â Â Â Â Â Â Â Â (0x7 << 0)
> +/* For UTMI_RX Register */
> +#define UTMI_RX_SQ_THRESH_SHIFT Â Â Â Â Â Â Â Â Â Â Â Â4
> +#define UTMI_RX_SQ_THRESH_MASK Â Â Â Â Â Â Â Â (0xf << 4)
> +#define UTMI_REG_SQ_LENGTH_SHIFT Â Â Â Â Â Â Â 15
> +#define UTMI_REG_SQ_LENGTH_MASK Â Â Â Â Â Â Â Â Â Â Â Â(0x3 << 15)
> +
> +#define REG_RCAL_START Â Â Â Â Â Â Â Â Â Â Â Â 0x00001000
> +#define VCOCAL_START Â Â Â Â Â Â Â Â Â Â Â Â Â 0x00200000
> +#define KVCO_EXT Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0x00400000
> +#define PLL_READY Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â0x00800000
> +#define CLK_BLK_EN Â Â Â Â Â Â Â Â Â Â Â Â Â Â 0x01000000
> +#endif
> +
> +static unsigned int u2o_read(unsigned int base, unsigned int offset)
> +{
> + Â Â Â return readl(base + offset);
> +}
> +
> +static void u2o_set(unsigned int base, unsigned int offset, unsigned int value)
> +{
> + Â Â Â unsigned int reg;
> +
> + Â Â Â reg = readl(base + offset);
> + Â Â Â reg |= value;
> + Â Â Â writel(reg, base + offset);
> + Â Â Â readl(base + offset);
> +}
> +
> +static void u2o_clear(unsigned int base, unsigned int offset,
> + Â Â Â unsigned int value)
> +{
> + Â Â Â unsigned int reg;
> +
> + Â Â Â reg = readl(base + offset);
> + Â Â Â reg &= ~value;
> + Â Â Â writel(reg, base + offset);
> + Â Â Â readl(base + offset);
> +}
> +
> +static void u2o_write(unsigned int base, unsigned int offset,
> + Â Â Â unsigned int value)
> +{
> + Â Â Â writel(value, base + offset);
> + Â Â Â readl(base + offset);
> +}
> +
> +#ifdef CONFIG_ARCH_MMP
> +int mv_udc_phy_init(unsigned int base)
> +{
> + Â Â Â unsigned long timeout;
> +
> + Â Â Â /* Initialize the USB PHY power */
> + Â Â Â if (cpu_is_pxa910()) {
> + Â Â Â Â Â Â Â u2o_set(base, UTMI_CTRL, (1 << UTMI_CTRL_INPKT_DELAY_SOF_SHIFT)
> + Â Â Â Â Â Â Â Â Â Â Â | (1 << UTMI_CTRL_PU_REF_SHIFT));
> + Â Â Â }
> +
> + Â Â Â u2o_set(base, UTMI_CTRL, 1 << UTMI_CTRL_PLL_PWR_UP_SHIFT);
> + Â Â Â u2o_set(base, UTMI_CTRL, 1 << UTMI_CTRL_PWR_UP_SHIFT);
> +
> + Â Â Â /* UTMI_PLL settings */
> + Â Â Â u2o_clear(base, UTMI_PLL, UTMI_PLL_PLLVDD18_MASK
> + Â Â Â Â Â Â Â | UTMI_PLL_PLLVDD12_MASK | UTMI_PLL_PLLCALI12_MASK
> + Â Â Â Â Â Â Â | UTMI_PLL_FBDIV_MASK | UTMI_PLL_REFDIV_MASK
> + Â Â Â Â Â Â Â | UTMI_PLL_ICP_MASK | UTMI_PLL_KVCO_MASK);
> +
> + Â Â Â u2o_set(base, UTMI_PLL, (0xee << UTMI_PLL_FBDIV_SHIFT)
> + Â Â Â Â Â Â Â | (0xb << UTMI_PLL_REFDIV_SHIFT)
> + Â Â Â Â Â Â Â | (3 << UTMI_PLL_PLLVDD18_SHIFT)
> + Â Â Â Â Â Â Â | (3 << UTMI_PLL_PLLVDD12_SHIFT)
> + Â Â Â Â Â Â Â | (3 << UTMI_PLL_PLLCALI12_SHIFT)
> + Â Â Â Â Â Â Â | (1 << UTMI_PLL_ICP_SHIFT) | (3 << UTMI_PLL_KVCO_SHIFT));
> +
> + Â Â Â /* UTMI_TX */
> + Â Â Â u2o_clear(base, UTMI_TX, UTMI_TX_REG_EXT_FS_RCAL_EN_MASK
> + Â Â Â Â Â Â Â | UTMI_TX_TXVDD12_MASK
> + Â Â Â Â Â Â Â | UTMI_TX_CK60_PHSEL_MASK | UTMI_TX_IMPCAL_VTH_MASK
> + Â Â Â Â Â Â Â | UTMI_TX_REG_EXT_FS_RCAL_MASK | UTMI_TX_AMP_MASK);
> + Â Â Â u2o_set(base, UTMI_TX, (3 << UTMI_TX_TXVDD12_SHIFT)
> + Â Â Â Â Â Â Â | (4 << UTMI_TX_CK60_PHSEL_SHIFT)
> + Â Â Â Â Â Â Â | (4 << UTMI_TX_IMPCAL_VTH_SHIFT)
> + Â Â Â Â Â Â Â | (8 << UTMI_TX_REG_EXT_FS_RCAL_SHIFT)
> + Â Â Â Â Â Â Â | (3 << UTMI_TX_AMP_SHIFT));
> +
> + Â Â Â /* UTMI_RX */
> + Â Â Â u2o_clear(base, UTMI_RX, UTMI_RX_SQ_THRESH_MASK
> + Â Â Â Â Â Â Â | UTMI_REG_SQ_LENGTH_MASK);
> + Â Â Â if (cpu_is_pxa168())
> + Â Â Â Â Â Â Â u2o_set(base, UTMI_RX, (7 << UTMI_RX_SQ_THRESH_SHIFT)
> + Â Â Â Â Â Â Â Â Â Â Â | (2 << UTMI_REG_SQ_LENGTH_SHIFT));
> + Â Â Â else
> + Â Â Â Â Â Â Â u2o_set(base, UTMI_RX, (0x7 << UTMI_RX_SQ_THRESH_SHIFT)
> + Â Â Â Â Â Â Â Â Â Â Â | (2 << UTMI_REG_SQ_LENGTH_SHIFT));
> +
> + Â Â Â /* UTMI_IVREF */
> + Â Â Â if (cpu_is_pxa168())
> + Â Â Â Â Â Â Â /* fixing Microsoft Altair board interface with NEC hub issue -
> + Â Â Â Â Â Â Â Â* Set UTMI_IVREF from 0x4a3 to 0x4bf */
> + Â Â Â Â Â Â Â u2o_write(base, UTMI_IVREF, 0x4bf);
> +
> + Â Â Â /* calibrate */
> + Â Â Â timeout = jiffies + 100;
> + Â Â Â while ((u2o_read(base, UTMI_PLL) & PLL_READY) == 0) {
> + Â Â Â Â Â Â Â if (time_after(jiffies, timeout))
> + Â Â Â Â Â Â Â Â Â Â Â return -ETIME;
> + Â Â Â Â Â Â Â cpu_relax();
> + Â Â Â }
> +
> + Â Â Â /* toggle VCOCAL_START bit of UTMI_PLL */
> + Â Â Â udelay(200);
> + Â Â Â u2o_set(base, UTMI_PLL, VCOCAL_START);
> + Â Â Â udelay(40);
> + Â Â Â u2o_clear(base, UTMI_PLL, VCOCAL_START);
> +
> + Â Â Â /* toggle REG_RCAL_START bit of UTMI_TX */
> + Â Â Â udelay(200);
> + Â Â Â u2o_set(base, UTMI_TX, REG_RCAL_START);
> + Â Â Â udelay(40);
> + Â Â Â u2o_clear(base, UTMI_TX, REG_RCAL_START);
> + Â Â Â udelay(200);
> +
> + Â Â Â /* make sure phy is ready */
> + Â Â Â timeout = jiffies + 100;
> + Â Â Â while ((u2o_read(base, UTMI_PLL) & PLL_READY) == 0) {
> + Â Â Â Â Â Â Â if (time_after(jiffies, timeout))
> + Â Â Â Â Â Â Â Â Â Â Â return -ETIME;
> + Â Â Â Â Â Â Â cpu_relax();
> + Â Â Â }
> +
> + Â Â Â if (cpu_is_pxa168()) {
> + Â Â Â Â Â Â Â u2o_set(base, UTMI_RESERVE, 1 << 5);
> + Â Â Â Â Â Â Â /* Turn on UTMI PHY OTG extension */
> + Â Â Â Â Â Â Â u2o_write(base, UTMI_OTG_ADDON, 1);
> + Â Â Â }
> + Â Â Â return 0;
> +}
> +#else
> +int mv_udc_phy_init(unsigned int base)
> +{
> + Â Â Â return 0;
> +}
> +#endif
> --
> 1.6.3.3
>
>
ÿô.nlj·Ÿ®‰­†+%ŠË±é¥Šwÿº{.nlj·¥Š{±þë)íèjg¬±¨¶‰šŽŠÝjÿ¾«þG«é¸¢·¦j:+v‰¨Šwèm¶Ÿÿþø®w¥þŠà£¢·hšâÿ†Ù



[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux