Re: [RFC/PATCH v4] usb: dwc3: Introduce OTG driver for dwc3

2012-08-09 Thread Felipe Balbi
Hi,

On Mon, Aug 06, 2012 at 03:31:17PM +0300, Ido Shayevitz wrote:
 This is first release of otg driver for the dwc3 Synopsys USB3 core.
 The otg driver implements the otg final state machine and control the
 activation of the device controller or host controller.
 
 In this first implementation, only simple DRD mode is implemented,
 determine if A or B device according to the ID pin as reflected in the
 OSTS.ConIDSts field.
 
 Signed-off-by: Ido Shayevitz i...@codeaurora.org
 
 ---
  drivers/usb/dwc3/Makefile|2 +
  drivers/usb/dwc3/core.c  |   15 +-
  drivers/usb/dwc3/core.h  |   55 -
  drivers/usb/dwc3/dwc3_otg.c  |  537 
 ++
  drivers/usb/dwc3/dwc3_otg.h  |   59 +
  drivers/usb/dwc3/gadget.c|   63 +
  drivers/usb/host/xhci-plat.c |   21 ++
  drivers/usb/host/xhci.c  |   13 +-
  8 files changed, 755 insertions(+), 10 deletions(-)
  create mode 100644 drivers/usb/dwc3/dwc3_otg.c
  create mode 100644 drivers/usb/dwc3/dwc3_otg.h
 
 diff --git a/drivers/usb/dwc3/Makefile b/drivers/usb/dwc3/Makefile
 index d441fe4..ffb3f55 100644
 --- a/drivers/usb/dwc3/Makefile
 +++ b/drivers/usb/dwc3/Makefile
 @@ -1,11 +1,13 @@
  ccflags-$(CONFIG_USB_DWC3_DEBUG) := -DDEBUG
  ccflags-$(CONFIG_USB_DWC3_VERBOSE)   += -DVERBOSE_DEBUG
 +ccflags-y += -Idrivers/usb/host

this is wrong.

  obj-$(CONFIG_USB_DWC3)   += dwc3.o
  
  dwc3-y   := core.o
  dwc3-y   += host.o
  dwc3-y   += gadget.o ep0.o
 +dwc3-y   += dwc3_otg.o
  
  ifneq ($(CONFIG_DEBUG_FS),)
   dwc3-y  += debugfs.o
 diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
 index c34452a..5343e39 100644
 --- a/drivers/usb/dwc3/core.c
 +++ b/drivers/usb/dwc3/core.c
 @@ -517,15 +517,24 @@ static int __devinit dwc3_probe(struct platform_device 
 *pdev)
   break;
   case DWC3_MODE_DRD:
   dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
 + ret = dwc3_otg_init(dwc);
 + if (ret) {
 + dev_err(dev, failed to initialize otg\n);
 + goto err1;
 + }
 +
   ret = dwc3_host_init(dwc);
   if (ret) {
   dev_err(dev, failed to initialize host\n);
 + dwc3_otg_exit(dwc);
   goto err1;
   }
  
   ret = dwc3_gadget_init(dwc);
   if (ret) {
   dev_err(dev, failed to initialize gadget\n);
 + dwc3_host_exit(dwc);
 + dwc3_otg_exit(dwc);
   goto err1;
   }
   break;
 @@ -554,8 +563,9 @@ err2:
   dwc3_host_exit(dwc);
   break;
   case DWC3_MODE_DRD:
 - dwc3_host_exit(dwc);
   dwc3_gadget_exit(dwc);
 + dwc3_host_exit(dwc);

inverting gadget and host is fixing something ? If it is, it should be a
separate patch with a proper reasoning.

 + dwc3_otg_exit(dwc);
   break;
   default:
   /* do nothing */
 @@ -588,8 +598,9 @@ static int __devexit dwc3_remove(struct platform_device 
 *pdev)
   dwc3_host_exit(dwc);
   break;
   case DWC3_MODE_DRD:
 - dwc3_host_exit(dwc);
   dwc3_gadget_exit(dwc);
 + dwc3_host_exit(dwc);

likewise.

 + dwc3_otg_exit(dwc);
   break;
   default:
   /* do nothing */
 diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
 index c611d80..c2521ba 100644
 --- a/drivers/usb/dwc3/core.h
 +++ b/drivers/usb/dwc3/core.h
 @@ -50,6 +50,8 @@
  #include linux/usb/ch9.h
  #include linux/usb/gadget.h
  
 +#include dwc3_otg.h
 +
  /* Global constants */
  #define DWC3_EP0_BOUNCE_SIZE 512
  #define DWC3_ENDPOINTS_NUM   32
 @@ -152,8 +154,9 @@
  /* OTG Registers */
  #define DWC3_OCFG0xcc00
  #define DWC3_OCTL0xcc04
 -#define DWC3_OEVTEN  0xcc08
 -#define DWC3_OSTS0xcc0C
 +#define DWC3_OEVT0xcc08
 +#define DWC3_OEVTEN  0xcc0c
 +#define DWC3_OSTS0xcc10

this should be a separate patch fixing the macro definitions.

  
  /* Bit fields */
  
 @@ -203,6 +206,9 @@
  #define DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(n) (((n)  (0x0f  13))  13)
  #define DWC3_MAX_HIBER_SCRATCHBUFS   15
  
 +/* Global HWPARAMS6 Register */
 +#define DWC3_GHWPARAMS6_SRP_SUPPORT  (1  10)
 +
  /* Device Configuration Register */
  #define DWC3_DCFG_LPM_CAP(1  22)
  #define DWC3_DCFG_DEVADDR(addr)  ((addr)  3)
 @@ -358,6 +364,38 @@
  #define DWC3_DEPCMD_TYPE_BULK2
  #define DWC3_DEPCMD_TYPE_INTR3
  
 +/* OTG Events Register */
 +#define DWC3_OEVT_DEVICEMODE (1  31)
 +#define 

[RFC/PATCH v4] usb: dwc3: Introduce OTG driver for dwc3

2012-08-06 Thread Ido Shayevitz
This is first release of otg driver for the dwc3 Synopsys USB3 core.
The otg driver implements the otg final state machine and control the
activation of the device controller or host controller.

In this first implementation, only simple DRD mode is implemented,
determine if A or B device according to the ID pin as reflected in the
OSTS.ConIDSts field.

Signed-off-by: Ido Shayevitz i...@codeaurora.org

---
 drivers/usb/dwc3/Makefile|2 +
 drivers/usb/dwc3/core.c  |   15 +-
 drivers/usb/dwc3/core.h  |   55 -
 drivers/usb/dwc3/dwc3_otg.c  |  537 ++
 drivers/usb/dwc3/dwc3_otg.h  |   59 +
 drivers/usb/dwc3/gadget.c|   63 +
 drivers/usb/host/xhci-plat.c |   21 ++
 drivers/usb/host/xhci.c  |   13 +-
 8 files changed, 755 insertions(+), 10 deletions(-)
 create mode 100644 drivers/usb/dwc3/dwc3_otg.c
 create mode 100644 drivers/usb/dwc3/dwc3_otg.h

diff --git a/drivers/usb/dwc3/Makefile b/drivers/usb/dwc3/Makefile
index d441fe4..ffb3f55 100644
--- a/drivers/usb/dwc3/Makefile
+++ b/drivers/usb/dwc3/Makefile
@@ -1,11 +1,13 @@
 ccflags-$(CONFIG_USB_DWC3_DEBUG)   := -DDEBUG
 ccflags-$(CONFIG_USB_DWC3_VERBOSE) += -DVERBOSE_DEBUG
+ccflags-y += -Idrivers/usb/host
 
 obj-$(CONFIG_USB_DWC3) += dwc3.o
 
 dwc3-y := core.o
 dwc3-y += host.o
 dwc3-y += gadget.o ep0.o
+dwc3-y += dwc3_otg.o
 
 ifneq ($(CONFIG_DEBUG_FS),)
dwc3-y  += debugfs.o
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index c34452a..5343e39 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -517,15 +517,24 @@ static int __devinit dwc3_probe(struct platform_device 
*pdev)
break;
case DWC3_MODE_DRD:
dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
+   ret = dwc3_otg_init(dwc);
+   if (ret) {
+   dev_err(dev, failed to initialize otg\n);
+   goto err1;
+   }
+
ret = dwc3_host_init(dwc);
if (ret) {
dev_err(dev, failed to initialize host\n);
+   dwc3_otg_exit(dwc);
goto err1;
}
 
ret = dwc3_gadget_init(dwc);
if (ret) {
dev_err(dev, failed to initialize gadget\n);
+   dwc3_host_exit(dwc);
+   dwc3_otg_exit(dwc);
goto err1;
}
break;
@@ -554,8 +563,9 @@ err2:
dwc3_host_exit(dwc);
break;
case DWC3_MODE_DRD:
-   dwc3_host_exit(dwc);
dwc3_gadget_exit(dwc);
+   dwc3_host_exit(dwc);
+   dwc3_otg_exit(dwc);
break;
default:
/* do nothing */
@@ -588,8 +598,9 @@ static int __devexit dwc3_remove(struct platform_device 
*pdev)
dwc3_host_exit(dwc);
break;
case DWC3_MODE_DRD:
-   dwc3_host_exit(dwc);
dwc3_gadget_exit(dwc);
+   dwc3_host_exit(dwc);
+   dwc3_otg_exit(dwc);
break;
default:
/* do nothing */
diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index c611d80..c2521ba 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -50,6 +50,8 @@
 #include linux/usb/ch9.h
 #include linux/usb/gadget.h
 
+#include dwc3_otg.h
+
 /* Global constants */
 #define DWC3_EP0_BOUNCE_SIZE   512
 #define DWC3_ENDPOINTS_NUM 32
@@ -152,8 +154,9 @@
 /* OTG Registers */
 #define DWC3_OCFG  0xcc00
 #define DWC3_OCTL  0xcc04
-#define DWC3_OEVTEN0xcc08
-#define DWC3_OSTS  0xcc0C
+#define DWC3_OEVT  0xcc08
+#define DWC3_OEVTEN0xcc0c
+#define DWC3_OSTS  0xcc10
 
 /* Bit fields */
 
@@ -203,6 +206,9 @@
 #define DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(n)   (((n)  (0x0f  13))  13)
 #define DWC3_MAX_HIBER_SCRATCHBUFS 15
 
+/* Global HWPARAMS6 Register */
+#define DWC3_GHWPARAMS6_SRP_SUPPORT(1  10)
+
 /* Device Configuration Register */
 #define DWC3_DCFG_LPM_CAP  (1  22)
 #define DWC3_DCFG_DEVADDR(addr)((addr)  3)
@@ -358,6 +364,38 @@
 #define DWC3_DEPCMD_TYPE_BULK  2
 #define DWC3_DEPCMD_TYPE_INTR  3
 
+/* OTG Events Register */
+#define DWC3_OEVT_DEVICEMODE   (1  31)
+#define DWC3_OEVT_CLEAR_ALL(~DWC3_OEVT_DEVICEMODE)
+#define DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT(1  24)
+#define DWC3_OEVTEN_OTGADEVBHOSTENDEVNT(1  20)
+#define DWC3_OEVTEN_OTGADEVHOSTEVNT(1  19)
+#define DWC3_OEVTEN_OTGADEVHNPCHNGEVNT (1  18)
+#define DWC3_OEVTEN_OTGADEVSRPDETEVNT