162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Faraday FOTG210 USB OTG controller 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2013 Faraday Technology Corporation 662306a36Sopenharmony_ci * Author: Yuan-Hsin Chen <yhchen@faraday-tech.com> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/kernel.h> 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#define FOTG210_MAX_NUM_EP 5 /* ep0...ep4 */ 1262306a36Sopenharmony_ci#define FOTG210_MAX_FIFO_NUM 4 /* fifo0...fifo4 */ 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci/* Global Mask of HC/OTG/DEV interrupt Register(0xC4) */ 1562306a36Sopenharmony_ci#define FOTG210_GMIR 0xC4 1662306a36Sopenharmony_ci#define GMIR_INT_POLARITY 0x8 /*Active High*/ 1762306a36Sopenharmony_ci#define GMIR_MHC_INT 0x4 1862306a36Sopenharmony_ci#define GMIR_MOTG_INT 0x2 1962306a36Sopenharmony_ci#define GMIR_MDEV_INT 0x1 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci/* Device Main Control Register(0x100) */ 2262306a36Sopenharmony_ci#define FOTG210_DMCR 0x100 2362306a36Sopenharmony_ci#define DMCR_HS_EN (1 << 6) 2462306a36Sopenharmony_ci#define DMCR_CHIP_EN (1 << 5) 2562306a36Sopenharmony_ci#define DMCR_SFRST (1 << 4) 2662306a36Sopenharmony_ci#define DMCR_GOSUSP (1 << 3) 2762306a36Sopenharmony_ci#define DMCR_GLINT_EN (1 << 2) 2862306a36Sopenharmony_ci#define DMCR_HALF_SPEED (1 << 1) 2962306a36Sopenharmony_ci#define DMCR_CAP_RMWAKUP (1 << 0) 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci/* Device Address Register(0x104) */ 3262306a36Sopenharmony_ci#define FOTG210_DAR 0x104 3362306a36Sopenharmony_ci#define DAR_AFT_CONF (1 << 7) 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci/* Device Test Register(0x108) */ 3662306a36Sopenharmony_ci#define FOTG210_DTR 0x108 3762306a36Sopenharmony_ci#define DTR_TST_CLRFF (1 << 0) 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci/* PHY Test Mode Selector register(0x114) */ 4062306a36Sopenharmony_ci#define FOTG210_PHYTMSR 0x114 4162306a36Sopenharmony_ci#define PHYTMSR_TST_PKT (1 << 4) 4262306a36Sopenharmony_ci#define PHYTMSR_TST_SE0NAK (1 << 3) 4362306a36Sopenharmony_ci#define PHYTMSR_TST_KSTA (1 << 2) 4462306a36Sopenharmony_ci#define PHYTMSR_TST_JSTA (1 << 1) 4562306a36Sopenharmony_ci#define PHYTMSR_UNPLUG (1 << 0) 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci/* Cx configuration and FIFO Empty Status register(0x120) */ 4862306a36Sopenharmony_ci#define FOTG210_DCFESR 0x120 4962306a36Sopenharmony_ci#define DCFESR_FIFO_EMPTY(fifo) (1 << 8 << (fifo)) 5062306a36Sopenharmony_ci#define DCFESR_CX_EMP (1 << 5) 5162306a36Sopenharmony_ci#define DCFESR_CX_CLR (1 << 3) 5262306a36Sopenharmony_ci#define DCFESR_CX_STL (1 << 2) 5362306a36Sopenharmony_ci#define DCFESR_TST_PKDONE (1 << 1) 5462306a36Sopenharmony_ci#define DCFESR_CX_DONE (1 << 0) 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci/* Device IDLE Counter Register(0x124) */ 5762306a36Sopenharmony_ci#define FOTG210_DICR 0x124 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci/* Device Mask of Interrupt Group Register (0x130) */ 6062306a36Sopenharmony_ci#define FOTG210_DMIGR 0x130 6162306a36Sopenharmony_ci#define DMIGR_MINT_G2 (1 << 2) 6262306a36Sopenharmony_ci#define DMIGR_MINT_G1 (1 << 1) 6362306a36Sopenharmony_ci#define DMIGR_MINT_G0 (1 << 0) 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci/* Device Mask of Interrupt Source Group 0(0x134) */ 6662306a36Sopenharmony_ci#define FOTG210_DMISGR0 0x134 6762306a36Sopenharmony_ci#define DMISGR0_MCX_COMEND (1 << 3) 6862306a36Sopenharmony_ci#define DMISGR0_MCX_OUT_INT (1 << 2) 6962306a36Sopenharmony_ci#define DMISGR0_MCX_IN_INT (1 << 1) 7062306a36Sopenharmony_ci#define DMISGR0_MCX_SETUP_INT (1 << 0) 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci/* Device Mask of Interrupt Source Group 1 Register(0x138)*/ 7362306a36Sopenharmony_ci#define FOTG210_DMISGR1 0x138 7462306a36Sopenharmony_ci#define DMISGR1_MF3_IN_INT (1 << 19) 7562306a36Sopenharmony_ci#define DMISGR1_MF2_IN_INT (1 << 18) 7662306a36Sopenharmony_ci#define DMISGR1_MF1_IN_INT (1 << 17) 7762306a36Sopenharmony_ci#define DMISGR1_MF0_IN_INT (1 << 16) 7862306a36Sopenharmony_ci#define DMISGR1_MF_IN_INT(fifo) (1 << (16 + (fifo))) 7962306a36Sopenharmony_ci#define DMISGR1_MF3_SPK_INT (1 << 7) 8062306a36Sopenharmony_ci#define DMISGR1_MF3_OUT_INT (1 << 6) 8162306a36Sopenharmony_ci#define DMISGR1_MF2_SPK_INT (1 << 5) 8262306a36Sopenharmony_ci#define DMISGR1_MF2_OUT_INT (1 << 4) 8362306a36Sopenharmony_ci#define DMISGR1_MF1_SPK_INT (1 << 3) 8462306a36Sopenharmony_ci#define DMISGR1_MF1_OUT_INT (1 << 2) 8562306a36Sopenharmony_ci#define DMISGR1_MF0_SPK_INT (1 << 1) 8662306a36Sopenharmony_ci#define DMISGR1_MF0_OUT_INT (1 << 0) 8762306a36Sopenharmony_ci#define DMISGR1_MF_OUTSPK_INT(fifo) (0x3 << (fifo) * 2) 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci/* Device Mask of Interrupt Source Group 2 Register (0x13C) */ 9062306a36Sopenharmony_ci#define FOTG210_DMISGR2 0x13C 9162306a36Sopenharmony_ci#define DMISGR2_MDMA_ERROR (1 << 8) 9262306a36Sopenharmony_ci#define DMISGR2_MDMA_CMPLT (1 << 7) 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci/* Device Interrupt group Register (0x140) */ 9562306a36Sopenharmony_ci#define FOTG210_DIGR 0x140 9662306a36Sopenharmony_ci#define DIGR_INT_G2 (1 << 2) 9762306a36Sopenharmony_ci#define DIGR_INT_G1 (1 << 1) 9862306a36Sopenharmony_ci#define DIGR_INT_G0 (1 << 0) 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci/* Device Interrupt Source Group 0 Register (0x144) */ 10162306a36Sopenharmony_ci#define FOTG210_DISGR0 0x144 10262306a36Sopenharmony_ci#define DISGR0_CX_COMABT_INT (1 << 5) 10362306a36Sopenharmony_ci#define DISGR0_CX_COMFAIL_INT (1 << 4) 10462306a36Sopenharmony_ci#define DISGR0_CX_COMEND_INT (1 << 3) 10562306a36Sopenharmony_ci#define DISGR0_CX_OUT_INT (1 << 2) 10662306a36Sopenharmony_ci#define DISGR0_CX_IN_INT (1 << 1) 10762306a36Sopenharmony_ci#define DISGR0_CX_SETUP_INT (1 << 0) 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci/* Device Interrupt Source Group 1 Register (0x148) */ 11062306a36Sopenharmony_ci#define FOTG210_DISGR1 0x148 11162306a36Sopenharmony_ci#define DISGR1_OUT_INT(fifo) (1 << ((fifo) * 2)) 11262306a36Sopenharmony_ci#define DISGR1_SPK_INT(fifo) (1 << 1 << ((fifo) * 2)) 11362306a36Sopenharmony_ci#define DISGR1_IN_INT(fifo) (1 << 16 << (fifo)) 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci/* Device Interrupt Source Group 2 Register (0x14C) */ 11662306a36Sopenharmony_ci#define FOTG210_DISGR2 0x14C 11762306a36Sopenharmony_ci#define DISGR2_DMA_ERROR (1 << 8) 11862306a36Sopenharmony_ci#define DISGR2_DMA_CMPLT (1 << 7) 11962306a36Sopenharmony_ci#define DISGR2_RX0BYTE_INT (1 << 6) 12062306a36Sopenharmony_ci#define DISGR2_TX0BYTE_INT (1 << 5) 12162306a36Sopenharmony_ci#define DISGR2_ISO_SEQ_ABORT_INT (1 << 4) 12262306a36Sopenharmony_ci#define DISGR2_ISO_SEQ_ERR_INT (1 << 3) 12362306a36Sopenharmony_ci#define DISGR2_RESM_INT (1 << 2) 12462306a36Sopenharmony_ci#define DISGR2_SUSP_INT (1 << 1) 12562306a36Sopenharmony_ci#define DISGR2_USBRST_INT (1 << 0) 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci/* Device Receive Zero-Length Data Packet Register (0x150)*/ 12862306a36Sopenharmony_ci#define FOTG210_RX0BYTE 0x150 12962306a36Sopenharmony_ci#define RX0BYTE_EP8 (1 << 7) 13062306a36Sopenharmony_ci#define RX0BYTE_EP7 (1 << 6) 13162306a36Sopenharmony_ci#define RX0BYTE_EP6 (1 << 5) 13262306a36Sopenharmony_ci#define RX0BYTE_EP5 (1 << 4) 13362306a36Sopenharmony_ci#define RX0BYTE_EP4 (1 << 3) 13462306a36Sopenharmony_ci#define RX0BYTE_EP3 (1 << 2) 13562306a36Sopenharmony_ci#define RX0BYTE_EP2 (1 << 1) 13662306a36Sopenharmony_ci#define RX0BYTE_EP1 (1 << 0) 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci/* Device Transfer Zero-Length Data Packet Register (0x154)*/ 13962306a36Sopenharmony_ci#define FOTG210_TX0BYTE 0x154 14062306a36Sopenharmony_ci#define TX0BYTE_EP8 (1 << 7) 14162306a36Sopenharmony_ci#define TX0BYTE_EP7 (1 << 6) 14262306a36Sopenharmony_ci#define TX0BYTE_EP6 (1 << 5) 14362306a36Sopenharmony_ci#define TX0BYTE_EP5 (1 << 4) 14462306a36Sopenharmony_ci#define TX0BYTE_EP4 (1 << 3) 14562306a36Sopenharmony_ci#define TX0BYTE_EP3 (1 << 2) 14662306a36Sopenharmony_ci#define TX0BYTE_EP2 (1 << 1) 14762306a36Sopenharmony_ci#define TX0BYTE_EP1 (1 << 0) 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci/* Device IN Endpoint x MaxPacketSize Register(0x160+4*(x-1)) */ 15062306a36Sopenharmony_ci#define FOTG210_INEPMPSR(ep) (0x160 + 4 * ((ep) - 1)) 15162306a36Sopenharmony_ci#define INOUTEPMPSR_MPS(mps) ((mps) & 0x2FF) 15262306a36Sopenharmony_ci#define INOUTEPMPSR_STL_EP (1 << 11) 15362306a36Sopenharmony_ci#define INOUTEPMPSR_RESET_TSEQ (1 << 12) 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci/* Device OUT Endpoint x MaxPacketSize Register(0x180+4*(x-1)) */ 15662306a36Sopenharmony_ci#define FOTG210_OUTEPMPSR(ep) (0x180 + 4 * ((ep) - 1)) 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci/* Device Endpoint 1~4 Map Register (0x1A0) */ 15962306a36Sopenharmony_ci#define FOTG210_EPMAP 0x1A0 16062306a36Sopenharmony_ci#define EPMAP_FIFONO(ep, dir) \ 16162306a36Sopenharmony_ci ((((ep) - 1) << ((ep) - 1) * 8) << ((dir) ? 0 : 4)) 16262306a36Sopenharmony_ci#define EPMAP_FIFONOMSK(ep, dir) \ 16362306a36Sopenharmony_ci ((3 << ((ep) - 1) * 8) << ((dir) ? 0 : 4)) 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci/* Device FIFO Map Register (0x1A8) */ 16662306a36Sopenharmony_ci#define FOTG210_FIFOMAP 0x1A8 16762306a36Sopenharmony_ci#define FIFOMAP_DIROUT(fifo) (0x0 << 4 << (fifo) * 8) 16862306a36Sopenharmony_ci#define FIFOMAP_DIRIN(fifo) (0x1 << 4 << (fifo) * 8) 16962306a36Sopenharmony_ci#define FIFOMAP_BIDIR(fifo) (0x2 << 4 << (fifo) * 8) 17062306a36Sopenharmony_ci#define FIFOMAP_NA(fifo) (0x3 << 4 << (fifo) * 8) 17162306a36Sopenharmony_ci#define FIFOMAP_EPNO(ep) ((ep) << ((ep) - 1) * 8) 17262306a36Sopenharmony_ci#define FIFOMAP_EPNOMSK(ep) (0xF << ((ep) - 1) * 8) 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci/* Device FIFO Confuguration Register (0x1AC) */ 17562306a36Sopenharmony_ci#define FOTG210_FIFOCF 0x1AC 17662306a36Sopenharmony_ci#define FIFOCF_TYPE(type, fifo) ((type) << (fifo) * 8) 17762306a36Sopenharmony_ci#define FIFOCF_BLK_SIN(fifo) (0x0 << (fifo) * 8 << 2) 17862306a36Sopenharmony_ci#define FIFOCF_BLK_DUB(fifo) (0x1 << (fifo) * 8 << 2) 17962306a36Sopenharmony_ci#define FIFOCF_BLK_TRI(fifo) (0x2 << (fifo) * 8 << 2) 18062306a36Sopenharmony_ci#define FIFOCF_BLKSZ_512(fifo) (0x0 << (fifo) * 8 << 4) 18162306a36Sopenharmony_ci#define FIFOCF_BLKSZ_1024(fifo) (0x1 << (fifo) * 8 << 4) 18262306a36Sopenharmony_ci#define FIFOCF_FIFO_EN(fifo) (0x1 << (fifo) * 8 << 5) 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci/* Device FIFO n Instruction and Byte Count Register (0x1B0+4*n) */ 18562306a36Sopenharmony_ci#define FOTG210_FIBCR(fifo) (0x1B0 + (fifo) * 4) 18662306a36Sopenharmony_ci#define FIBCR_BCFX 0x7FF 18762306a36Sopenharmony_ci#define FIBCR_FFRST (1 << 12) 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci/* Device DMA Target FIFO Number Register (0x1C0) */ 19062306a36Sopenharmony_ci#define FOTG210_DMATFNR 0x1C0 19162306a36Sopenharmony_ci#define DMATFNR_ACC_CXF (1 << 4) 19262306a36Sopenharmony_ci#define DMATFNR_ACC_F3 (1 << 3) 19362306a36Sopenharmony_ci#define DMATFNR_ACC_F2 (1 << 2) 19462306a36Sopenharmony_ci#define DMATFNR_ACC_F1 (1 << 1) 19562306a36Sopenharmony_ci#define DMATFNR_ACC_F0 (1 << 0) 19662306a36Sopenharmony_ci#define DMATFNR_ACC_FN(fifo) (1 << (fifo)) 19762306a36Sopenharmony_ci#define DMATFNR_DISDMA 0 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci/* Device DMA Controller Parameter setting 1 Register (0x1C8) */ 20062306a36Sopenharmony_ci#define FOTG210_DMACPSR1 0x1C8 20162306a36Sopenharmony_ci#define DMACPSR1_DMA_LEN(len) (((len) & 0xFFFF) << 8) 20262306a36Sopenharmony_ci#define DMACPSR1_DMA_ABORT (1 << 3) 20362306a36Sopenharmony_ci#define DMACPSR1_DMA_TYPE(dir_in) (((dir_in) ? 1 : 0) << 1) 20462306a36Sopenharmony_ci#define DMACPSR1_DMA_START (1 << 0) 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci/* Device DMA Controller Parameter setting 2 Register (0x1CC) */ 20762306a36Sopenharmony_ci#define FOTG210_DMACPSR2 0x1CC 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci/* Device DMA Controller Parameter setting 3 Register (0x1CC) */ 21062306a36Sopenharmony_ci#define FOTG210_CXPORT 0x1D0 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_cistruct fotg210_request { 21362306a36Sopenharmony_ci struct usb_request req; 21462306a36Sopenharmony_ci struct list_head queue; 21562306a36Sopenharmony_ci}; 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_cistruct fotg210_ep { 21862306a36Sopenharmony_ci struct usb_ep ep; 21962306a36Sopenharmony_ci struct fotg210_udc *fotg210; 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci struct list_head queue; 22262306a36Sopenharmony_ci unsigned stall:1; 22362306a36Sopenharmony_ci unsigned wedged:1; 22462306a36Sopenharmony_ci unsigned use_dma:1; 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci unsigned char epnum; 22762306a36Sopenharmony_ci unsigned char type; 22862306a36Sopenharmony_ci unsigned char dir_in; 22962306a36Sopenharmony_ci unsigned int maxp; 23062306a36Sopenharmony_ci const struct usb_endpoint_descriptor *desc; 23162306a36Sopenharmony_ci}; 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_cistruct fotg210_udc { 23462306a36Sopenharmony_ci spinlock_t lock; /* protect the struct */ 23562306a36Sopenharmony_ci void __iomem *reg; 23662306a36Sopenharmony_ci 23762306a36Sopenharmony_ci unsigned long irq_trigger; 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci struct device *dev; 24062306a36Sopenharmony_ci struct fotg210 *fotg; 24162306a36Sopenharmony_ci struct usb_phy *phy; 24262306a36Sopenharmony_ci struct usb_gadget gadget; 24362306a36Sopenharmony_ci struct usb_gadget_driver *driver; 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci struct fotg210_ep *ep[FOTG210_MAX_NUM_EP]; 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci struct usb_request *ep0_req; /* for internal request */ 24862306a36Sopenharmony_ci __le16 ep0_data; 24962306a36Sopenharmony_ci u8 ep0_dir; /* 0/0x80 out/in */ 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ci u8 reenum; /* if re-enumeration */ 25262306a36Sopenharmony_ci}; 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ci#define gadget_to_fotg210(g) container_of((g), struct fotg210_udc, gadget) 255