[RFC PATCH 34/40] phy: ti: Add a new SERDES driver for TI's AM654x SoC

2018-09-21 Thread Kishon Vijay Abraham I
Add a new SERDES driver for TI's AM654x SoC which configures
the SERDES only for PCIe. Support fo USB3 will be added later.

SERDES in am654x has three input clocks (left input, externel reference
clock and right input) and two output clocks (left output and right
output) in addition to a PLL mux clock which the SERDES uses for Clock
Multiplier Unit (CMU refclock).

The PLL mux clock can select from one of the three input clocks.
The right output can select between left input and external reference
clock while the left output can select between the right input and
external reference clock.

The driver has support to select PLL mux and left/right output mux as
specified in device tree.

Signed-off-by: Kishon Vijay Abraham I 
---
 drivers/phy/ti/Kconfig|  11 +
 drivers/phy/ti/Makefile   |   1 +
 drivers/phy/ti/phy-am654-serdes.c | 513 ++
 3 files changed, 525 insertions(+)
 create mode 100644 drivers/phy/ti/phy-am654-serdes.c

diff --git a/drivers/phy/ti/Kconfig b/drivers/phy/ti/Kconfig
index 20503562666c..8a556649de68 100644
--- a/drivers/phy/ti/Kconfig
+++ b/drivers/phy/ti/Kconfig
@@ -20,6 +20,17 @@ config PHY_DM816X_USB
help
  Enable this for dm816x USB to work.
 
+config PHY_AM654_SERDES
+   tristate "TI AM654 SERDES support"
+   depends on OF && ARCH_K3 || COMPILE_TEST
+   select GENERIC_PHY
+   select MULTIPLEXER
+   select REGMAP_MMIO
+   select MUX_MMIO
+   help
+ This option enables support for TI AM654 SerDes PHY used for
+ PCIe.
+
 config OMAP_CONTROL_PHY
tristate "OMAP CONTROL PHY Driver"
depends on ARCH_OMAP2PLUS || COMPILE_TEST
diff --git a/drivers/phy/ti/Makefile b/drivers/phy/ti/Makefile
index 9f361756eaf2..0df18acbbb60 100644
--- a/drivers/phy/ti/Makefile
+++ b/drivers/phy/ti/Makefile
@@ -6,3 +6,4 @@ obj-$(CONFIG_OMAP_USB2) += phy-omap-usb2.o
 obj-$(CONFIG_TI_PIPE3) += phy-ti-pipe3.o
 obj-$(CONFIG_PHY_TUSB1210) += phy-tusb1210.o
 obj-$(CONFIG_TWL4030_USB)  += phy-twl4030-usb.o
+obj-$(CONFIG_PHY_AM654_SERDES) += phy-am654-serdes.o
diff --git a/drivers/phy/ti/phy-am654-serdes.c 
b/drivers/phy/ti/phy-am654-serdes.c
new file mode 100644
index ..1a6216e7c69e
--- /dev/null
+++ b/drivers/phy/ti/phy-am654-serdes.c
@@ -0,0 +1,513 @@
+// SPDX-License-Identifier: GPL-2.0
+/**
+ * PCIe SERDES driver for AM654x SoC
+ *
+ * Copyright (C) 2018 Texas Instruments
+ * Author: Kishon Vijay Abraham I 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define CMU_R07C   0x7c
+#define CMU_MASTER_CDN_O   BIT(24)
+
+#define COMLANE_R138   0xb38
+#define CONFIG_VERSION_REG_MASKGENMASK(23, 16)
+#define CONFIG_VERSION_REG_SHIFT 16
+#define VERSION0x70
+
+#define COMLANE_R190   0xb90
+#define L1_MASTER_CDN_OBIT(9)
+
+#define COMLANE_R194   0xb94
+#define CMU_OK_I_0 BIT(19)
+
+#define SERDES_CTRL0x1fd0
+#define POR_EN BIT(29)
+
+#define WIZ_LANEXCTL_STS   0x1fe0
+#define TX0_ENABLE_OVL BIT(31)
+#define TX0_ENABLE_MASKGENMASK(30, 29)
+#define TX0_ENABLE_SHIFT   29
+#define TX0_DISABLE_STATE  0x0
+#define TX0_SLEEP_STATE0x1
+#define TX0_SNOOZE_STATE   0x2
+#define TX0_ENABLE_STATE   0x3
+#define RX0_ENABLE_OVL BIT(15)
+#define RX0_ENABLE_MASKGENMASK(14, 13)
+#define RX0_ENABLE_SHIFT   13
+#define RX0_DISABLE_STATE  0x0
+#define RX0_SLEEP_STATE0x1
+#define RX0_SNOOZE_STATE   0x2
+#define RX0_ENABLE_STATE   0x3
+
+#define WIZ_PLL_CTRL   0x1ff4
+#define PLL_ENABLE_OVL BIT(31)
+#define PLL_ENABLE_MASKGENMASK(30, 29)
+#define PLL_ENABLE_SHIFT   29
+#define PLL_DISABLE_STATE  0x0
+#define PLL_SLEEP_STATE0x1
+#define PLL_SNOOZE_STATE   0x2
+#define PLL_ENABLE_STATE   0x3
+#define PLL_OK BIT(28)
+
+#define PLL_LOCK_TIME  10  /* in microseconds */
+#define SLEEP_TIME 100 /* in microseconds */
+
+#define LANE_USB3  0x0
+#define LANE_PCIE0_LANE0   0x1
+
+#define LANE_PCIE1_LANE0   0x0
+#define LANE_PCIE0_LANE1   0x1
+
+#define SERDES_NUM_CLOCKS  3
+
+struct serdes_am654_clk_mux {
+   struct clk_hw   hw;
+   struct regmap   *regmap;
+   unsigned intreg;
+   int *table;
+   u32 mask;
+   u8  shift;
+};
+
+#define to_serdes_am654_clk_mux(_hw)   \
+   container_of(_hw, struct serdes_am654_clk_mux, hw)
+
+static struct regmap_config serdes_am654_regmap_config = {
+   .reg_bits = 32,
+   .val_bits = 32,
+   .reg_stride = 4,
+   .fast_io = true,
+};
+

[RFC PATCH 34/40] phy: ti: Add a new SERDES driver for TI's AM654x SoC

2018-09-21 Thread Kishon Vijay Abraham I
Add a new SERDES driver for TI's AM654x SoC which configures
the SERDES only for PCIe. Support fo USB3 will be added later.

SERDES in am654x has three input clocks (left input, externel reference
clock and right input) and two output clocks (left output and right
output) in addition to a PLL mux clock which the SERDES uses for Clock
Multiplier Unit (CMU refclock).

The PLL mux clock can select from one of the three input clocks.
The right output can select between left input and external reference
clock while the left output can select between the right input and
external reference clock.

The driver has support to select PLL mux and left/right output mux as
specified in device tree.

Signed-off-by: Kishon Vijay Abraham I 
---
 drivers/phy/ti/Kconfig|  11 +
 drivers/phy/ti/Makefile   |   1 +
 drivers/phy/ti/phy-am654-serdes.c | 513 ++
 3 files changed, 525 insertions(+)
 create mode 100644 drivers/phy/ti/phy-am654-serdes.c

diff --git a/drivers/phy/ti/Kconfig b/drivers/phy/ti/Kconfig
index 20503562666c..8a556649de68 100644
--- a/drivers/phy/ti/Kconfig
+++ b/drivers/phy/ti/Kconfig
@@ -20,6 +20,17 @@ config PHY_DM816X_USB
help
  Enable this for dm816x USB to work.
 
+config PHY_AM654_SERDES
+   tristate "TI AM654 SERDES support"
+   depends on OF && ARCH_K3 || COMPILE_TEST
+   select GENERIC_PHY
+   select MULTIPLEXER
+   select REGMAP_MMIO
+   select MUX_MMIO
+   help
+ This option enables support for TI AM654 SerDes PHY used for
+ PCIe.
+
 config OMAP_CONTROL_PHY
tristate "OMAP CONTROL PHY Driver"
depends on ARCH_OMAP2PLUS || COMPILE_TEST
diff --git a/drivers/phy/ti/Makefile b/drivers/phy/ti/Makefile
index 9f361756eaf2..0df18acbbb60 100644
--- a/drivers/phy/ti/Makefile
+++ b/drivers/phy/ti/Makefile
@@ -6,3 +6,4 @@ obj-$(CONFIG_OMAP_USB2) += phy-omap-usb2.o
 obj-$(CONFIG_TI_PIPE3) += phy-ti-pipe3.o
 obj-$(CONFIG_PHY_TUSB1210) += phy-tusb1210.o
 obj-$(CONFIG_TWL4030_USB)  += phy-twl4030-usb.o
+obj-$(CONFIG_PHY_AM654_SERDES) += phy-am654-serdes.o
diff --git a/drivers/phy/ti/phy-am654-serdes.c 
b/drivers/phy/ti/phy-am654-serdes.c
new file mode 100644
index ..1a6216e7c69e
--- /dev/null
+++ b/drivers/phy/ti/phy-am654-serdes.c
@@ -0,0 +1,513 @@
+// SPDX-License-Identifier: GPL-2.0
+/**
+ * PCIe SERDES driver for AM654x SoC
+ *
+ * Copyright (C) 2018 Texas Instruments
+ * Author: Kishon Vijay Abraham I 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define CMU_R07C   0x7c
+#define CMU_MASTER_CDN_O   BIT(24)
+
+#define COMLANE_R138   0xb38
+#define CONFIG_VERSION_REG_MASKGENMASK(23, 16)
+#define CONFIG_VERSION_REG_SHIFT 16
+#define VERSION0x70
+
+#define COMLANE_R190   0xb90
+#define L1_MASTER_CDN_OBIT(9)
+
+#define COMLANE_R194   0xb94
+#define CMU_OK_I_0 BIT(19)
+
+#define SERDES_CTRL0x1fd0
+#define POR_EN BIT(29)
+
+#define WIZ_LANEXCTL_STS   0x1fe0
+#define TX0_ENABLE_OVL BIT(31)
+#define TX0_ENABLE_MASKGENMASK(30, 29)
+#define TX0_ENABLE_SHIFT   29
+#define TX0_DISABLE_STATE  0x0
+#define TX0_SLEEP_STATE0x1
+#define TX0_SNOOZE_STATE   0x2
+#define TX0_ENABLE_STATE   0x3
+#define RX0_ENABLE_OVL BIT(15)
+#define RX0_ENABLE_MASKGENMASK(14, 13)
+#define RX0_ENABLE_SHIFT   13
+#define RX0_DISABLE_STATE  0x0
+#define RX0_SLEEP_STATE0x1
+#define RX0_SNOOZE_STATE   0x2
+#define RX0_ENABLE_STATE   0x3
+
+#define WIZ_PLL_CTRL   0x1ff4
+#define PLL_ENABLE_OVL BIT(31)
+#define PLL_ENABLE_MASKGENMASK(30, 29)
+#define PLL_ENABLE_SHIFT   29
+#define PLL_DISABLE_STATE  0x0
+#define PLL_SLEEP_STATE0x1
+#define PLL_SNOOZE_STATE   0x2
+#define PLL_ENABLE_STATE   0x3
+#define PLL_OK BIT(28)
+
+#define PLL_LOCK_TIME  10  /* in microseconds */
+#define SLEEP_TIME 100 /* in microseconds */
+
+#define LANE_USB3  0x0
+#define LANE_PCIE0_LANE0   0x1
+
+#define LANE_PCIE1_LANE0   0x0
+#define LANE_PCIE0_LANE1   0x1
+
+#define SERDES_NUM_CLOCKS  3
+
+struct serdes_am654_clk_mux {
+   struct clk_hw   hw;
+   struct regmap   *regmap;
+   unsigned intreg;
+   int *table;
+   u32 mask;
+   u8  shift;
+};
+
+#define to_serdes_am654_clk_mux(_hw)   \
+   container_of(_hw, struct serdes_am654_clk_mux, hw)
+
+static struct regmap_config serdes_am654_regmap_config = {
+   .reg_bits = 32,
+   .val_bits = 32,
+   .reg_stride = 4,
+   .fast_io = true,
+};
+