[PATCH v3 2/3] Broadcom USB DRD Phy driver for Northstar2

2017-01-30 Thread Raviteja Garimella
This is driver for USB DRD Phy used in Broadcom's Northstar2
SoC. The phy can be configured to be in Device mode or Host
mode based on the type of cable connected to the port. The
driver registers to  extcon framework to get appropriate
connect events for Host/Device cables connect/disconnect
states based on VBUS and ID interrupts.

Signed-off-by: Raviteja Garimella 
---
 drivers/phy/Kconfig  |  13 +
 drivers/phy/Makefile |   1 +
 drivers/phy/phy-bcm-ns2-usbdrd.c | 576 +++
 3 files changed, 590 insertions(+)
 create mode 100644 drivers/phy/phy-bcm-ns2-usbdrd.c

diff --git a/drivers/phy/Kconfig b/drivers/phy/Kconfig
index e8eb7f2..1b3de42 100644
--- a/drivers/phy/Kconfig
+++ b/drivers/phy/Kconfig
@@ -463,6 +463,19 @@ config PHY_CYGNUS_PCIE
  Enable this to support the Broadcom Cygnus PCIe PHY.
  If unsure, say N.
 
+config PHY_NS2_USB_DRD
+   tristate "Broadcom Northstar2 USB DRD PHY support"
+   depends on OF && (ARCH_BCM_IPROC || COMPILE_TEST)
+   select GENERIC_PHY
+   select EXTCON
+   default ARCH_BCM_IPROC
+   help
+ Enable this to support the Broadcom Northstar2 USB DRD PHY.
+ This driver initializes the PHY in either HOST or DEVICE mode.
+ The host or device configuration is read from device tree.
+
+ If unsure, say N.
+
 source "drivers/phy/tegra/Kconfig"
 
 config PHY_NS2_PCIE
diff --git a/drivers/phy/Makefile b/drivers/phy/Makefile
index 65eb2f4..cfbdd9a 100644
--- a/drivers/phy/Makefile
+++ b/drivers/phy/Makefile
@@ -56,6 +56,7 @@ obj-$(CONFIG_PHY_TUSB1210)+= phy-tusb1210.o
 obj-$(CONFIG_PHY_BRCM_SATA)+= phy-brcm-sata.o
 obj-$(CONFIG_PHY_PISTACHIO_USB)+= phy-pistachio-usb.o
 obj-$(CONFIG_PHY_CYGNUS_PCIE)  += phy-bcm-cygnus-pcie.o
+obj-$(CONFIG_PHY_NS2_USB_DRD)  += phy-bcm-ns2-usbdrd.o
 obj-$(CONFIG_ARCH_TEGRA) += tegra/
 obj-$(CONFIG_PHY_NS2_PCIE) += phy-bcm-ns2-pcie.o
 obj-$(CONFIG_PHY_MESON8B_USB2) += phy-meson8b-usb2.o
diff --git a/drivers/phy/phy-bcm-ns2-usbdrd.c b/drivers/phy/phy-bcm-ns2-usbdrd.c
new file mode 100644
index 000..6436225
--- /dev/null
+++ b/drivers/phy/phy-bcm-ns2-usbdrd.c
@@ -0,0 +1,576 @@
+/*
+ * Copyright (C) 2016 Broadcom
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation version 2.
+ *
+ * This program is distributed "as is" WITHOUT ANY WARRANTY of any
+ * kind, whether express or implied; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define ICFG_DRD_AFE   0x0
+#define ICFG_MISC_STAT 0x18
+#define ICFG_DRD_P0CTL 0x1C
+#define ICFG_STRAP_CTRL0x20
+#define ICFG_FSM_CTRL  0x24
+
+#define IDM_RST_BITBIT(0)
+#define AFE_CORERDY_VDDC   BIT(18)
+#define PHY_PLL_RESETB BIT(15)
+#define PHY_RESETB BIT(14)
+#define PHY_PLL_LOCK   BIT(0)
+
+#define DRD_DEV_MODE   BIT(20)
+#define OHCI_OVRCUR_POLBIT(11)
+#define ICFG_OFF_MODE  BIT(6)
+#define PLL_LOCK_RETRY 1000
+
+#define EVT_DEVICE 0
+#define EVT_HOST   1
+#define EVT_IDLE   2
+
+#define DRD_HOST_MODE  (BIT(2) | BIT(3))
+#define DRD_DEVICE_MODE(BIT(4) | BIT(5))
+#define DRD_HOST_VAL   0x803
+#define DRD_DEV_VAL0x807
+#define GPIO_DELAY 20
+#define PHY_WQ_DELAY   msecs_to_jiffies(600)
+
+struct ns2_phy_data;
+struct ns2_phy_driver {
+   void __iomem *icfgdrd_regs;
+   void __iomem *idmdrd_rst_ctrl;
+   void __iomem *crmu_usb2_ctrl;
+   void __iomem *usb2h_strap_reg;
+   spinlock_t lock; /* spin lock for phy driver */
+   bool host_mode;
+   struct ns2_phy_data *data;
+   struct extcon_specific_cable_nb extcon_dev;
+   struct extcon_specific_cable_nb extcon_host;
+   struct notifier_block host_nb;
+   struct notifier_block dev_nb;
+   struct delayed_work conn_work;
+   struct extcon_dev *edev;
+   struct gpio_desc *vbus_gpiod;
+   struct gpio_desc *id_gpiod;
+   int id_irq;
+   int vbus_irq;
+   unsigned long debounce_jiffies;
+   struct delayed_work wq_extcon;
+};
+
+struct ns2_phy_data {
+   struct ns2_phy_driver *driver;
+   struct phy *phy;
+   int new_state;
+};
+
+static const unsigned int usb_extcon_cable[] = {
+   EXTCON_USB,
+   EXTCON_USB_HOST,
+   EXTCON_NONE,
+};
+
+static inline int pll_lock_stat(u32 usb_reg, int reg_mask,
+  

[PATCH v3 2/3] Broadcom USB DRD Phy driver for Northstar2

2017-01-30 Thread Raviteja Garimella
This is driver for USB DRD Phy used in Broadcom's Northstar2
SoC. The phy can be configured to be in Device mode or Host
mode based on the type of cable connected to the port. The
driver registers to  extcon framework to get appropriate
connect events for Host/Device cables connect/disconnect
states based on VBUS and ID interrupts.

Signed-off-by: Raviteja Garimella 
---
 drivers/phy/Kconfig  |  13 +
 drivers/phy/Makefile |   1 +
 drivers/phy/phy-bcm-ns2-usbdrd.c | 576 +++
 3 files changed, 590 insertions(+)
 create mode 100644 drivers/phy/phy-bcm-ns2-usbdrd.c

diff --git a/drivers/phy/Kconfig b/drivers/phy/Kconfig
index e8eb7f2..1b3de42 100644
--- a/drivers/phy/Kconfig
+++ b/drivers/phy/Kconfig
@@ -463,6 +463,19 @@ config PHY_CYGNUS_PCIE
  Enable this to support the Broadcom Cygnus PCIe PHY.
  If unsure, say N.
 
+config PHY_NS2_USB_DRD
+   tristate "Broadcom Northstar2 USB DRD PHY support"
+   depends on OF && (ARCH_BCM_IPROC || COMPILE_TEST)
+   select GENERIC_PHY
+   select EXTCON
+   default ARCH_BCM_IPROC
+   help
+ Enable this to support the Broadcom Northstar2 USB DRD PHY.
+ This driver initializes the PHY in either HOST or DEVICE mode.
+ The host or device configuration is read from device tree.
+
+ If unsure, say N.
+
 source "drivers/phy/tegra/Kconfig"
 
 config PHY_NS2_PCIE
diff --git a/drivers/phy/Makefile b/drivers/phy/Makefile
index 65eb2f4..cfbdd9a 100644
--- a/drivers/phy/Makefile
+++ b/drivers/phy/Makefile
@@ -56,6 +56,7 @@ obj-$(CONFIG_PHY_TUSB1210)+= phy-tusb1210.o
 obj-$(CONFIG_PHY_BRCM_SATA)+= phy-brcm-sata.o
 obj-$(CONFIG_PHY_PISTACHIO_USB)+= phy-pistachio-usb.o
 obj-$(CONFIG_PHY_CYGNUS_PCIE)  += phy-bcm-cygnus-pcie.o
+obj-$(CONFIG_PHY_NS2_USB_DRD)  += phy-bcm-ns2-usbdrd.o
 obj-$(CONFIG_ARCH_TEGRA) += tegra/
 obj-$(CONFIG_PHY_NS2_PCIE) += phy-bcm-ns2-pcie.o
 obj-$(CONFIG_PHY_MESON8B_USB2) += phy-meson8b-usb2.o
diff --git a/drivers/phy/phy-bcm-ns2-usbdrd.c b/drivers/phy/phy-bcm-ns2-usbdrd.c
new file mode 100644
index 000..6436225
--- /dev/null
+++ b/drivers/phy/phy-bcm-ns2-usbdrd.c
@@ -0,0 +1,576 @@
+/*
+ * Copyright (C) 2016 Broadcom
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation version 2.
+ *
+ * This program is distributed "as is" WITHOUT ANY WARRANTY of any
+ * kind, whether express or implied; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define ICFG_DRD_AFE   0x0
+#define ICFG_MISC_STAT 0x18
+#define ICFG_DRD_P0CTL 0x1C
+#define ICFG_STRAP_CTRL0x20
+#define ICFG_FSM_CTRL  0x24
+
+#define IDM_RST_BITBIT(0)
+#define AFE_CORERDY_VDDC   BIT(18)
+#define PHY_PLL_RESETB BIT(15)
+#define PHY_RESETB BIT(14)
+#define PHY_PLL_LOCK   BIT(0)
+
+#define DRD_DEV_MODE   BIT(20)
+#define OHCI_OVRCUR_POLBIT(11)
+#define ICFG_OFF_MODE  BIT(6)
+#define PLL_LOCK_RETRY 1000
+
+#define EVT_DEVICE 0
+#define EVT_HOST   1
+#define EVT_IDLE   2
+
+#define DRD_HOST_MODE  (BIT(2) | BIT(3))
+#define DRD_DEVICE_MODE(BIT(4) | BIT(5))
+#define DRD_HOST_VAL   0x803
+#define DRD_DEV_VAL0x807
+#define GPIO_DELAY 20
+#define PHY_WQ_DELAY   msecs_to_jiffies(600)
+
+struct ns2_phy_data;
+struct ns2_phy_driver {
+   void __iomem *icfgdrd_regs;
+   void __iomem *idmdrd_rst_ctrl;
+   void __iomem *crmu_usb2_ctrl;
+   void __iomem *usb2h_strap_reg;
+   spinlock_t lock; /* spin lock for phy driver */
+   bool host_mode;
+   struct ns2_phy_data *data;
+   struct extcon_specific_cable_nb extcon_dev;
+   struct extcon_specific_cable_nb extcon_host;
+   struct notifier_block host_nb;
+   struct notifier_block dev_nb;
+   struct delayed_work conn_work;
+   struct extcon_dev *edev;
+   struct gpio_desc *vbus_gpiod;
+   struct gpio_desc *id_gpiod;
+   int id_irq;
+   int vbus_irq;
+   unsigned long debounce_jiffies;
+   struct delayed_work wq_extcon;
+};
+
+struct ns2_phy_data {
+   struct ns2_phy_driver *driver;
+   struct phy *phy;
+   int new_state;
+};
+
+static const unsigned int usb_extcon_cable[] = {
+   EXTCON_USB,
+   EXTCON_USB_HOST,
+   EXTCON_NONE,
+};
+
+static inline int pll_lock_stat(u32 usb_reg, int reg_mask,
+