Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * phy-ti-pipe3 - PIPE3 PHY driver.
0004  *
0005  * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
0006  * Author: Kishon Vijay Abraham I <kishon@ti.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/slab.h>
0012 #include <linux/phy/phy.h>
0013 #include <linux/of.h>
0014 #include <linux/clk.h>
0015 #include <linux/err.h>
0016 #include <linux/io.h>
0017 #include <linux/pm_runtime.h>
0018 #include <linux/delay.h>
0019 #include <linux/phy/omap_control_phy.h>
0020 #include <linux/of_platform.h>
0021 #include <linux/mfd/syscon.h>
0022 #include <linux/regmap.h>
0023 
0024 #define PLL_STATUS      0x00000004
0025 #define PLL_GO          0x00000008
0026 #define PLL_CONFIGURATION1  0x0000000C
0027 #define PLL_CONFIGURATION2  0x00000010
0028 #define PLL_CONFIGURATION3  0x00000014
0029 #define PLL_CONFIGURATION4  0x00000020
0030 
0031 #define PLL_REGM_MASK       0x001FFE00
0032 #define PLL_REGM_SHIFT      0x9
0033 #define PLL_REGM_F_MASK     0x0003FFFF
0034 #define PLL_REGM_F_SHIFT    0x0
0035 #define PLL_REGN_MASK       0x000001FE
0036 #define PLL_REGN_SHIFT      0x1
0037 #define PLL_SELFREQDCO_MASK 0x0000000E
0038 #define PLL_SELFREQDCO_SHIFT    0x1
0039 #define PLL_SD_MASK     0x0003FC00
0040 #define PLL_SD_SHIFT        10
0041 #define SET_PLL_GO      0x1
0042 #define PLL_LDOPWDN     BIT(15)
0043 #define PLL_TICOPWDN        BIT(16)
0044 #define PLL_LOCK        0x2
0045 #define PLL_IDLE        0x1
0046 
0047 #define SATA_PLL_SOFT_RESET BIT(18)
0048 
0049 #define PIPE3_PHY_PWRCTL_CLK_CMD_MASK   GENMASK(21, 14)
0050 #define PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT  14
0051 
0052 #define PIPE3_PHY_PWRCTL_CLK_FREQ_MASK  GENMASK(31, 22)
0053 #define PIPE3_PHY_PWRCTL_CLK_FREQ_SHIFT 22
0054 
0055 #define PIPE3_PHY_RX_POWERON       (0x1 << PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT)
0056 #define PIPE3_PHY_TX_POWERON       (0x2 << PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT)
0057 
0058 #define PCIE_PCS_MASK           0xFF0000
0059 #define PCIE_PCS_DELAY_COUNT_SHIFT  0x10
0060 
0061 #define PIPE3_PHY_RX_ANA_PROGRAMMABILITY    0x0000000C
0062 #define INTERFACE_MASK          GENMASK(31, 27)
0063 #define INTERFACE_SHIFT         27
0064 #define INTERFACE_MODE_USBSS        BIT(4)
0065 #define INTERFACE_MODE_SATA_1P5     BIT(3)
0066 #define INTERFACE_MODE_SATA_3P0     BIT(2)
0067 #define INTERFACE_MODE_PCIE     BIT(0)
0068 
0069 #define LOSD_MASK           GENMASK(17, 14)
0070 #define LOSD_SHIFT          14
0071 #define MEM_PLLDIV          GENMASK(6, 5)
0072 
0073 #define PIPE3_PHY_RX_TRIM       0x0000001C
0074 #define MEM_DLL_TRIM_SEL_MASK       GENMASK(31, 30)
0075 #define MEM_DLL_TRIM_SHIFT      30
0076 
0077 #define PIPE3_PHY_RX_DLL        0x00000024
0078 #define MEM_DLL_PHINT_RATE_MASK     GENMASK(31, 30)
0079 #define MEM_DLL_PHINT_RATE_SHIFT    30
0080 
0081 #define PIPE3_PHY_RX_DIGITAL_MODES      0x00000028
0082 #define MEM_HS_RATE_MASK        GENMASK(28, 27)
0083 #define MEM_HS_RATE_SHIFT       27
0084 #define MEM_OVRD_HS_RATE        BIT(26)
0085 #define MEM_OVRD_HS_RATE_SHIFT      26
0086 #define MEM_CDR_FASTLOCK        BIT(23)
0087 #define MEM_CDR_FASTLOCK_SHIFT      23
0088 #define MEM_CDR_LBW_MASK        GENMASK(22, 21)
0089 #define MEM_CDR_LBW_SHIFT       21
0090 #define MEM_CDR_STEPCNT_MASK        GENMASK(20, 19)
0091 #define MEM_CDR_STEPCNT_SHIFT       19
0092 #define MEM_CDR_STL_MASK        GENMASK(18, 16)
0093 #define MEM_CDR_STL_SHIFT       16
0094 #define MEM_CDR_THR_MASK        GENMASK(15, 13)
0095 #define MEM_CDR_THR_SHIFT       13
0096 #define MEM_CDR_THR_MODE        BIT(12)
0097 #define MEM_CDR_THR_MODE_SHIFT      12
0098 #define MEM_CDR_2NDO_SDM_MODE       BIT(11)
0099 #define MEM_CDR_2NDO_SDM_MODE_SHIFT 11
0100 
0101 #define PIPE3_PHY_RX_EQUALIZER      0x00000038
0102 #define MEM_EQLEV_MASK          GENMASK(31, 16)
0103 #define MEM_EQLEV_SHIFT         16
0104 #define MEM_EQFTC_MASK          GENMASK(15, 11)
0105 #define MEM_EQFTC_SHIFT         11
0106 #define MEM_EQCTL_MASK          GENMASK(10, 7)
0107 #define MEM_EQCTL_SHIFT         7
0108 #define MEM_OVRD_EQLEV          BIT(2)
0109 #define MEM_OVRD_EQLEV_SHIFT        2
0110 #define MEM_OVRD_EQFTC          BIT(1)
0111 #define MEM_OVRD_EQFTC_SHIFT        1
0112 
0113 #define SATA_PHY_RX_IO_AND_A2D_OVERRIDES    0x44
0114 #define MEM_CDR_LOS_SOURCE_MASK     GENMASK(10, 9)
0115 #define MEM_CDR_LOS_SOURCE_SHIFT    9
0116 
0117 /*
0118  * This is an Empirical value that works, need to confirm the actual
0119  * value required for the PIPE3PHY_PLL_CONFIGURATION2.PLL_IDLE status
0120  * to be correctly reflected in the PIPE3PHY_PLL_STATUS register.
0121  */
0122 #define PLL_IDLE_TIME   100 /* in milliseconds */
0123 #define PLL_LOCK_TIME   100 /* in milliseconds */
0124 
0125 enum pipe3_mode { PIPE3_MODE_PCIE = 1,
0126           PIPE3_MODE_SATA,
0127           PIPE3_MODE_USBSS };
0128 
0129 struct pipe3_dpll_params {
0130     u16 m;
0131     u8  n;
0132     u8  freq:3;
0133     u8  sd;
0134     u32 mf;
0135 };
0136 
0137 struct pipe3_dpll_map {
0138     unsigned long rate;
0139     struct pipe3_dpll_params params;
0140 };
0141 
0142 struct pipe3_settings {
0143     u8 ana_interface;
0144     u8 ana_losd;
0145     u8 dig_fastlock;
0146     u8 dig_lbw;
0147     u8 dig_stepcnt;
0148     u8 dig_stl;
0149     u8 dig_thr;
0150     u8 dig_thr_mode;
0151     u8 dig_2ndo_sdm_mode;
0152     u8 dig_hs_rate;
0153     u8 dig_ovrd_hs_rate;
0154     u8 dll_trim_sel;
0155     u8 dll_phint_rate;
0156     u8 eq_lev;
0157     u8 eq_ftc;
0158     u8 eq_ctl;
0159     u8 eq_ovrd_lev;
0160     u8 eq_ovrd_ftc;
0161 };
0162 
0163 struct ti_pipe3 {
0164     void __iomem        *pll_ctrl_base;
0165     void __iomem        *phy_rx;
0166     void __iomem        *phy_tx;
0167     struct device       *dev;
0168     struct device       *control_dev;
0169     struct clk      *wkupclk;
0170     struct clk      *sys_clk;
0171     struct clk      *refclk;
0172     struct clk      *div_clk;
0173     struct pipe3_dpll_map   *dpll_map;
0174     struct regmap       *phy_power_syscon; /* ctrl. reg. acces */
0175     struct regmap       *pcs_syscon; /* ctrl. reg. acces */
0176     struct regmap       *dpll_reset_syscon; /* ctrl. reg. acces */
0177     unsigned int        dpll_reset_reg; /* reg. index within syscon */
0178     unsigned int        power_reg; /* power reg. index within syscon */
0179     unsigned int        pcie_pcs_reg; /* pcs reg. index in syscon */
0180     bool            sata_refclk_enabled;
0181     enum pipe3_mode     mode;
0182     struct pipe3_settings   settings;
0183 };
0184 
0185 static struct pipe3_dpll_map dpll_map_usb[] = {
0186     {12000000, {1250, 5, 4, 20, 0} },   /* 12 MHz */
0187     {16800000, {3125, 20, 4, 20, 0} },  /* 16.8 MHz */
0188     {19200000, {1172, 8, 4, 20, 65537} },   /* 19.2 MHz */
0189     {20000000, {1000, 7, 4, 10, 0} },   /* 20 MHz */
0190     {26000000, {1250, 12, 4, 20, 0} },  /* 26 MHz */
0191     {38400000, {3125, 47, 4, 20, 92843} },  /* 38.4 MHz */
0192     { },                    /* Terminator */
0193 };
0194 
0195 static struct pipe3_dpll_map dpll_map_sata[] = {
0196     {12000000, {625, 4, 4, 6, 0} }, /* 12 MHz */
0197     {16800000, {625, 6, 4, 7, 0} },     /* 16.8 MHz */
0198     {19200000, {625, 7, 4, 6, 0} },     /* 19.2 MHz */
0199     {20000000, {750, 9, 4, 6, 0} },     /* 20 MHz */
0200     {26000000, {750, 12, 4, 6, 0} },    /* 26 MHz */
0201     {38400000, {625, 15, 4, 6, 0} },    /* 38.4 MHz */
0202     { },                    /* Terminator */
0203 };
0204 
0205 struct pipe3_data {
0206     enum pipe3_mode mode;
0207     struct pipe3_dpll_map *dpll_map;
0208     struct pipe3_settings settings;
0209 };
0210 
0211 static struct pipe3_data data_usb = {
0212     .mode = PIPE3_MODE_USBSS,
0213     .dpll_map = dpll_map_usb,
0214     .settings = {
0215     /* DRA75x TRM Table 26-17 Preferred USB3_PHY_RX SCP Register Settings */
0216         .ana_interface = INTERFACE_MODE_USBSS,
0217         .ana_losd = 0xa,
0218         .dig_fastlock = 1,
0219         .dig_lbw = 3,
0220         .dig_stepcnt = 0,
0221         .dig_stl = 0x3,
0222         .dig_thr = 1,
0223         .dig_thr_mode = 1,
0224         .dig_2ndo_sdm_mode = 0,
0225         .dig_hs_rate = 0,
0226         .dig_ovrd_hs_rate = 1,
0227         .dll_trim_sel = 0x2,
0228         .dll_phint_rate = 0x3,
0229         .eq_lev = 0,
0230         .eq_ftc = 0,
0231         .eq_ctl = 0x9,
0232         .eq_ovrd_lev = 0,
0233         .eq_ovrd_ftc = 0,
0234     },
0235 };
0236 
0237 static struct pipe3_data data_sata = {
0238     .mode = PIPE3_MODE_SATA,
0239     .dpll_map = dpll_map_sata,
0240     .settings = {
0241     /* DRA75x TRM Table 26-9 Preferred SATA_PHY_RX SCP Register Settings */
0242         .ana_interface = INTERFACE_MODE_SATA_3P0,
0243         .ana_losd = 0x5,
0244         .dig_fastlock = 1,
0245         .dig_lbw = 3,
0246         .dig_stepcnt = 0,
0247         .dig_stl = 0x3,
0248         .dig_thr = 1,
0249         .dig_thr_mode = 1,
0250         .dig_2ndo_sdm_mode = 0,
0251         .dig_hs_rate = 0,   /* Not in TRM preferred settings */
0252         .dig_ovrd_hs_rate = 0,  /* Not in TRM preferred settings */
0253         .dll_trim_sel = 0x1,
0254         .dll_phint_rate = 0x2,  /* for 1.5 GHz DPLL clock */
0255         .eq_lev = 0,
0256         .eq_ftc = 0x1f,
0257         .eq_ctl = 0,
0258         .eq_ovrd_lev = 1,
0259         .eq_ovrd_ftc = 1,
0260     },
0261 };
0262 
0263 static struct pipe3_data data_pcie = {
0264     .mode = PIPE3_MODE_PCIE,
0265     .settings = {
0266     /* DRA75x TRM Table 26-62 Preferred PCIe_PHY_RX SCP Register Settings */
0267         .ana_interface = INTERFACE_MODE_PCIE,
0268         .ana_losd = 0xa,
0269         .dig_fastlock = 1,
0270         .dig_lbw = 3,
0271         .dig_stepcnt = 0,
0272         .dig_stl = 0x3,
0273         .dig_thr = 1,
0274         .dig_thr_mode = 1,
0275         .dig_2ndo_sdm_mode = 0,
0276         .dig_hs_rate = 0,
0277         .dig_ovrd_hs_rate = 0,
0278         .dll_trim_sel = 0x2,
0279         .dll_phint_rate = 0x3,
0280         .eq_lev = 0,
0281         .eq_ftc = 0x1f,
0282         .eq_ctl = 1,
0283         .eq_ovrd_lev = 0,
0284         .eq_ovrd_ftc = 0,
0285     },
0286 };
0287 
0288 static inline u32 ti_pipe3_readl(void __iomem *addr, unsigned offset)
0289 {
0290     return __raw_readl(addr + offset);
0291 }
0292 
0293 static inline void ti_pipe3_writel(void __iomem *addr, unsigned offset,
0294     u32 data)
0295 {
0296     __raw_writel(data, addr + offset);
0297 }
0298 
0299 static struct pipe3_dpll_params *ti_pipe3_get_dpll_params(struct ti_pipe3 *phy)
0300 {
0301     unsigned long rate;
0302     struct pipe3_dpll_map *dpll_map = phy->dpll_map;
0303 
0304     rate = clk_get_rate(phy->sys_clk);
0305 
0306     for (; dpll_map->rate; dpll_map++) {
0307         if (rate == dpll_map->rate)
0308             return &dpll_map->params;
0309     }
0310 
0311     dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate);
0312 
0313     return NULL;
0314 }
0315 
0316 static int ti_pipe3_enable_clocks(struct ti_pipe3 *phy);
0317 static void ti_pipe3_disable_clocks(struct ti_pipe3 *phy);
0318 
0319 static int ti_pipe3_power_off(struct phy *x)
0320 {
0321     int ret;
0322     struct ti_pipe3 *phy = phy_get_drvdata(x);
0323 
0324     if (!phy->phy_power_syscon) {
0325         omap_control_phy_power(phy->control_dev, 0);
0326         return 0;
0327     }
0328 
0329     ret = regmap_update_bits(phy->phy_power_syscon, phy->power_reg,
0330                  PIPE3_PHY_PWRCTL_CLK_CMD_MASK, 0);
0331     return ret;
0332 }
0333 
0334 static void ti_pipe3_calibrate(struct ti_pipe3 *phy);
0335 
0336 static int ti_pipe3_power_on(struct phy *x)
0337 {
0338     u32 val;
0339     u32 mask;
0340     unsigned long rate;
0341     struct ti_pipe3 *phy = phy_get_drvdata(x);
0342     bool rx_pending = false;
0343 
0344     if (!phy->phy_power_syscon) {
0345         omap_control_phy_power(phy->control_dev, 1);
0346         return 0;
0347     }
0348 
0349     rate = clk_get_rate(phy->sys_clk);
0350     if (!rate) {
0351         dev_err(phy->dev, "Invalid clock rate\n");
0352         return -EINVAL;
0353     }
0354     rate = rate / 1000000;
0355     mask = OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_MASK;
0356     val = rate << OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_SHIFT;
0357     regmap_update_bits(phy->phy_power_syscon, phy->power_reg,
0358                mask, val);
0359     /*
0360      * For PCIe, TX and RX must be powered on simultaneously.
0361      * For USB and SATA, TX must be powered on before RX
0362      */
0363     mask = OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK;
0364     if (phy->mode == PIPE3_MODE_SATA || phy->mode == PIPE3_MODE_USBSS) {
0365         val = PIPE3_PHY_TX_POWERON;
0366         rx_pending = true;
0367     } else {
0368         val = PIPE3_PHY_TX_POWERON | PIPE3_PHY_RX_POWERON;
0369     }
0370 
0371     regmap_update_bits(phy->phy_power_syscon, phy->power_reg,
0372                mask, val);
0373 
0374     if (rx_pending) {
0375         val = PIPE3_PHY_TX_POWERON | PIPE3_PHY_RX_POWERON;
0376         regmap_update_bits(phy->phy_power_syscon, phy->power_reg,
0377                    mask, val);
0378     }
0379 
0380     if (phy->mode == PIPE3_MODE_PCIE)
0381         ti_pipe3_calibrate(phy);
0382 
0383     return 0;
0384 }
0385 
0386 static int ti_pipe3_dpll_wait_lock(struct ti_pipe3 *phy)
0387 {
0388     u32     val;
0389     unsigned long   timeout;
0390 
0391     timeout = jiffies + msecs_to_jiffies(PLL_LOCK_TIME);
0392     do {
0393         cpu_relax();
0394         val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
0395         if (val & PLL_LOCK)
0396             return 0;
0397     } while (!time_after(jiffies, timeout));
0398 
0399     dev_err(phy->dev, "DPLL failed to lock\n");
0400     return -EBUSY;
0401 }
0402 
0403 static int ti_pipe3_dpll_program(struct ti_pipe3 *phy)
0404 {
0405     u32         val;
0406     struct pipe3_dpll_params *dpll_params;
0407 
0408     dpll_params = ti_pipe3_get_dpll_params(phy);
0409     if (!dpll_params)
0410         return -EINVAL;
0411 
0412     val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
0413     val &= ~PLL_REGN_MASK;
0414     val |= dpll_params->n << PLL_REGN_SHIFT;
0415     ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
0416 
0417     val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
0418     val &= ~PLL_SELFREQDCO_MASK;
0419     val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
0420     ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
0421 
0422     val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
0423     val &= ~PLL_REGM_MASK;
0424     val |= dpll_params->m << PLL_REGM_SHIFT;
0425     ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
0426 
0427     val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
0428     val &= ~PLL_REGM_F_MASK;
0429     val |= dpll_params->mf << PLL_REGM_F_SHIFT;
0430     ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
0431 
0432     val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
0433     val &= ~PLL_SD_MASK;
0434     val |= dpll_params->sd << PLL_SD_SHIFT;
0435     ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
0436 
0437     ti_pipe3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
0438 
0439     return ti_pipe3_dpll_wait_lock(phy);
0440 }
0441 
0442 static void ti_pipe3_calibrate(struct ti_pipe3 *phy)
0443 {
0444     u32 val;
0445     struct pipe3_settings *s = &phy->settings;
0446 
0447     val = ti_pipe3_readl(phy->phy_rx, PIPE3_PHY_RX_ANA_PROGRAMMABILITY);
0448     val &= ~(INTERFACE_MASK | LOSD_MASK | MEM_PLLDIV);
0449     val |= (s->ana_interface << INTERFACE_SHIFT | s->ana_losd << LOSD_SHIFT);
0450     ti_pipe3_writel(phy->phy_rx, PIPE3_PHY_RX_ANA_PROGRAMMABILITY, val);
0451 
0452     val = ti_pipe3_readl(phy->phy_rx, PIPE3_PHY_RX_DIGITAL_MODES);
0453     val &= ~(MEM_HS_RATE_MASK | MEM_OVRD_HS_RATE | MEM_CDR_FASTLOCK |
0454          MEM_CDR_LBW_MASK | MEM_CDR_STEPCNT_MASK | MEM_CDR_STL_MASK |
0455          MEM_CDR_THR_MASK | MEM_CDR_THR_MODE | MEM_CDR_2NDO_SDM_MODE);
0456     val |= s->dig_hs_rate << MEM_HS_RATE_SHIFT |
0457         s->dig_ovrd_hs_rate << MEM_OVRD_HS_RATE_SHIFT |
0458         s->dig_fastlock << MEM_CDR_FASTLOCK_SHIFT |
0459         s->dig_lbw << MEM_CDR_LBW_SHIFT |
0460         s->dig_stepcnt << MEM_CDR_STEPCNT_SHIFT |
0461         s->dig_stl << MEM_CDR_STL_SHIFT |
0462         s->dig_thr << MEM_CDR_THR_SHIFT |
0463         s->dig_thr_mode << MEM_CDR_THR_MODE_SHIFT |
0464         s->dig_2ndo_sdm_mode << MEM_CDR_2NDO_SDM_MODE_SHIFT;
0465     ti_pipe3_writel(phy->phy_rx, PIPE3_PHY_RX_DIGITAL_MODES, val);
0466 
0467     val = ti_pipe3_readl(phy->phy_rx, PIPE3_PHY_RX_TRIM);
0468     val &= ~MEM_DLL_TRIM_SEL_MASK;
0469     val |= s->dll_trim_sel << MEM_DLL_TRIM_SHIFT;
0470     ti_pipe3_writel(phy->phy_rx, PIPE3_PHY_RX_TRIM, val);
0471 
0472     val = ti_pipe3_readl(phy->phy_rx, PIPE3_PHY_RX_DLL);
0473     val &= ~MEM_DLL_PHINT_RATE_MASK;
0474     val |= s->dll_phint_rate << MEM_DLL_PHINT_RATE_SHIFT;
0475     ti_pipe3_writel(phy->phy_rx, PIPE3_PHY_RX_DLL, val);
0476 
0477     val = ti_pipe3_readl(phy->phy_rx, PIPE3_PHY_RX_EQUALIZER);
0478     val &= ~(MEM_EQLEV_MASK | MEM_EQFTC_MASK | MEM_EQCTL_MASK |
0479          MEM_OVRD_EQLEV | MEM_OVRD_EQFTC);
0480     val |= s->eq_lev << MEM_EQLEV_SHIFT |
0481         s->eq_ftc << MEM_EQFTC_SHIFT |
0482         s->eq_ctl << MEM_EQCTL_SHIFT |
0483         s->eq_ovrd_lev << MEM_OVRD_EQLEV_SHIFT |
0484         s->eq_ovrd_ftc << MEM_OVRD_EQFTC_SHIFT;
0485     ti_pipe3_writel(phy->phy_rx, PIPE3_PHY_RX_EQUALIZER, val);
0486 
0487     if (phy->mode == PIPE3_MODE_SATA) {
0488         val = ti_pipe3_readl(phy->phy_rx,
0489                      SATA_PHY_RX_IO_AND_A2D_OVERRIDES);
0490         val &= ~MEM_CDR_LOS_SOURCE_MASK;
0491         ti_pipe3_writel(phy->phy_rx, SATA_PHY_RX_IO_AND_A2D_OVERRIDES,
0492                 val);
0493     }
0494 }
0495 
0496 static int ti_pipe3_init(struct phy *x)
0497 {
0498     struct ti_pipe3 *phy = phy_get_drvdata(x);
0499     u32 val;
0500     int ret = 0;
0501 
0502     ti_pipe3_enable_clocks(phy);
0503     /*
0504      * Set pcie_pcs register to 0x96 for proper functioning of phy
0505      * as recommended in AM572x TRM SPRUHZ6, section 18.5.2.2, table
0506      * 18-1804.
0507      */
0508     if (phy->mode == PIPE3_MODE_PCIE) {
0509         if (!phy->pcs_syscon) {
0510             omap_control_pcie_pcs(phy->control_dev, 0x96);
0511             return 0;
0512         }
0513 
0514         val = 0x96 << OMAP_CTRL_PCIE_PCS_DELAY_COUNT_SHIFT;
0515         ret = regmap_update_bits(phy->pcs_syscon, phy->pcie_pcs_reg,
0516                      PCIE_PCS_MASK, val);
0517         return ret;
0518     }
0519 
0520     /* Bring it out of IDLE if it is IDLE */
0521     val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
0522     if (val & PLL_IDLE) {
0523         val &= ~PLL_IDLE;
0524         ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
0525         ret = ti_pipe3_dpll_wait_lock(phy);
0526     }
0527 
0528     /* SATA has issues if re-programmed when locked */
0529     val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
0530     if ((val & PLL_LOCK) && phy->mode == PIPE3_MODE_SATA)
0531         return ret;
0532 
0533     /* Program the DPLL */
0534     ret = ti_pipe3_dpll_program(phy);
0535     if (ret) {
0536         ti_pipe3_disable_clocks(phy);
0537         return -EINVAL;
0538     }
0539 
0540     ti_pipe3_calibrate(phy);
0541 
0542     return ret;
0543 }
0544 
0545 static int ti_pipe3_exit(struct phy *x)
0546 {
0547     struct ti_pipe3 *phy = phy_get_drvdata(x);
0548     u32 val;
0549     unsigned long timeout;
0550 
0551     /* If dpll_reset_syscon is not present we wont power down SATA DPLL
0552      * due to Errata i783
0553      */
0554     if (phy->mode == PIPE3_MODE_SATA && !phy->dpll_reset_syscon)
0555         return 0;
0556 
0557     /* PCIe doesn't have internal DPLL */
0558     if (phy->mode != PIPE3_MODE_PCIE) {
0559         /* Put DPLL in IDLE mode */
0560         val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
0561         val |= PLL_IDLE;
0562         ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
0563 
0564         /* wait for LDO and Oscillator to power down */
0565         timeout = jiffies + msecs_to_jiffies(PLL_IDLE_TIME);
0566         do {
0567             cpu_relax();
0568             val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
0569             if ((val & PLL_TICOPWDN) && (val & PLL_LDOPWDN))
0570                 break;
0571         } while (!time_after(jiffies, timeout));
0572 
0573         if (!(val & PLL_TICOPWDN) || !(val & PLL_LDOPWDN)) {
0574             dev_err(phy->dev, "Failed to power down: PLL_STATUS 0x%x\n",
0575                 val);
0576             return -EBUSY;
0577         }
0578     }
0579 
0580     /* i783: SATA needs control bit toggle after PLL unlock */
0581     if (phy->mode == PIPE3_MODE_SATA) {
0582         regmap_update_bits(phy->dpll_reset_syscon, phy->dpll_reset_reg,
0583                    SATA_PLL_SOFT_RESET, SATA_PLL_SOFT_RESET);
0584         regmap_update_bits(phy->dpll_reset_syscon, phy->dpll_reset_reg,
0585                    SATA_PLL_SOFT_RESET, 0);
0586     }
0587 
0588     ti_pipe3_disable_clocks(phy);
0589 
0590     return 0;
0591 }
0592 static const struct phy_ops ops = {
0593     .init       = ti_pipe3_init,
0594     .exit       = ti_pipe3_exit,
0595     .power_on   = ti_pipe3_power_on,
0596     .power_off  = ti_pipe3_power_off,
0597     .owner      = THIS_MODULE,
0598 };
0599 
0600 static const struct of_device_id ti_pipe3_id_table[];
0601 
0602 static int ti_pipe3_get_clk(struct ti_pipe3 *phy)
0603 {
0604     struct clk *clk;
0605     struct device *dev = phy->dev;
0606 
0607     phy->refclk = devm_clk_get(dev, "refclk");
0608     if (IS_ERR(phy->refclk)) {
0609         dev_err(dev, "unable to get refclk\n");
0610         /* older DTBs have missing refclk in SATA PHY
0611          * so don't bail out in case of SATA PHY.
0612          */
0613         if (phy->mode != PIPE3_MODE_SATA)
0614             return PTR_ERR(phy->refclk);
0615     }
0616 
0617     if (phy->mode != PIPE3_MODE_SATA) {
0618         phy->wkupclk = devm_clk_get(dev, "wkupclk");
0619         if (IS_ERR(phy->wkupclk)) {
0620             dev_err(dev, "unable to get wkupclk\n");
0621             return PTR_ERR(phy->wkupclk);
0622         }
0623     } else {
0624         phy->wkupclk = ERR_PTR(-ENODEV);
0625     }
0626 
0627     if (phy->mode != PIPE3_MODE_PCIE || phy->phy_power_syscon) {
0628         phy->sys_clk = devm_clk_get(dev, "sysclk");
0629         if (IS_ERR(phy->sys_clk)) {
0630             dev_err(dev, "unable to get sysclk\n");
0631             return -EINVAL;
0632         }
0633     }
0634 
0635     if (phy->mode == PIPE3_MODE_PCIE) {
0636         clk = devm_clk_get(dev, "dpll_ref");
0637         if (IS_ERR(clk)) {
0638             dev_err(dev, "unable to get dpll ref clk\n");
0639             return PTR_ERR(clk);
0640         }
0641         clk_set_rate(clk, 1500000000);
0642 
0643         clk = devm_clk_get(dev, "dpll_ref_m2");
0644         if (IS_ERR(clk)) {
0645             dev_err(dev, "unable to get dpll ref m2 clk\n");
0646             return PTR_ERR(clk);
0647         }
0648         clk_set_rate(clk, 100000000);
0649 
0650         clk = devm_clk_get(dev, "phy-div");
0651         if (IS_ERR(clk)) {
0652             dev_err(dev, "unable to get phy-div clk\n");
0653             return PTR_ERR(clk);
0654         }
0655         clk_set_rate(clk, 100000000);
0656 
0657         phy->div_clk = devm_clk_get(dev, "div-clk");
0658         if (IS_ERR(phy->div_clk)) {
0659             dev_err(dev, "unable to get div-clk\n");
0660             return PTR_ERR(phy->div_clk);
0661         }
0662     } else {
0663         phy->div_clk = ERR_PTR(-ENODEV);
0664     }
0665 
0666     return 0;
0667 }
0668 
0669 static int ti_pipe3_get_sysctrl(struct ti_pipe3 *phy)
0670 {
0671     struct device *dev = phy->dev;
0672     struct device_node *node = dev->of_node;
0673     struct device_node *control_node;
0674     struct platform_device *control_pdev;
0675 
0676     phy->phy_power_syscon = syscon_regmap_lookup_by_phandle(node,
0677                             "syscon-phy-power");
0678     if (IS_ERR(phy->phy_power_syscon)) {
0679         dev_dbg(dev,
0680             "can't get syscon-phy-power, using control device\n");
0681         phy->phy_power_syscon = NULL;
0682     } else {
0683         if (of_property_read_u32_index(node,
0684                            "syscon-phy-power", 1,
0685                            &phy->power_reg)) {
0686             dev_err(dev, "couldn't get power reg. offset\n");
0687             return -EINVAL;
0688         }
0689     }
0690 
0691     if (!phy->phy_power_syscon) {
0692         control_node = of_parse_phandle(node, "ctrl-module", 0);
0693         if (!control_node) {
0694             dev_err(dev, "Failed to get control device phandle\n");
0695             return -EINVAL;
0696         }
0697 
0698         control_pdev = of_find_device_by_node(control_node);
0699         of_node_put(control_node);
0700         if (!control_pdev) {
0701             dev_err(dev, "Failed to get control device\n");
0702             return -EINVAL;
0703         }
0704 
0705         phy->control_dev = &control_pdev->dev;
0706     }
0707 
0708     if (phy->mode == PIPE3_MODE_PCIE) {
0709         phy->pcs_syscon = syscon_regmap_lookup_by_phandle(node,
0710                                   "syscon-pcs");
0711         if (IS_ERR(phy->pcs_syscon)) {
0712             dev_dbg(dev,
0713                 "can't get syscon-pcs, using omap control\n");
0714             phy->pcs_syscon = NULL;
0715         } else {
0716             if (of_property_read_u32_index(node,
0717                                "syscon-pcs", 1,
0718                                &phy->pcie_pcs_reg)) {
0719                 dev_err(dev,
0720                     "couldn't get pcie pcs reg. offset\n");
0721                 return -EINVAL;
0722             }
0723         }
0724     }
0725 
0726     if (phy->mode == PIPE3_MODE_SATA) {
0727         phy->dpll_reset_syscon = syscon_regmap_lookup_by_phandle(node,
0728                             "syscon-pllreset");
0729         if (IS_ERR(phy->dpll_reset_syscon)) {
0730             dev_info(dev,
0731                  "can't get syscon-pllreset, sata dpll won't idle\n");
0732             phy->dpll_reset_syscon = NULL;
0733         } else {
0734             if (of_property_read_u32_index(node,
0735                                "syscon-pllreset", 1,
0736                                &phy->dpll_reset_reg)) {
0737                 dev_err(dev,
0738                     "couldn't get pllreset reg. offset\n");
0739                 return -EINVAL;
0740             }
0741         }
0742     }
0743 
0744     return 0;
0745 }
0746 
0747 static int ti_pipe3_get_tx_rx_base(struct ti_pipe3 *phy)
0748 {
0749     struct device *dev = phy->dev;
0750     struct platform_device *pdev = to_platform_device(dev);
0751 
0752     phy->phy_rx = devm_platform_ioremap_resource_byname(pdev, "phy_rx");
0753     if (IS_ERR(phy->phy_rx))
0754         return PTR_ERR(phy->phy_rx);
0755 
0756     phy->phy_tx = devm_platform_ioremap_resource_byname(pdev, "phy_tx");
0757 
0758     return PTR_ERR_OR_ZERO(phy->phy_tx);
0759 }
0760 
0761 static int ti_pipe3_get_pll_base(struct ti_pipe3 *phy)
0762 {
0763     struct device *dev = phy->dev;
0764     struct platform_device *pdev = to_platform_device(dev);
0765 
0766     if (phy->mode == PIPE3_MODE_PCIE)
0767         return 0;
0768 
0769     phy->pll_ctrl_base =
0770         devm_platform_ioremap_resource_byname(pdev, "pll_ctrl");
0771     return PTR_ERR_OR_ZERO(phy->pll_ctrl_base);
0772 }
0773 
0774 static int ti_pipe3_probe(struct platform_device *pdev)
0775 {
0776     struct ti_pipe3 *phy;
0777     struct phy *generic_phy;
0778     struct phy_provider *phy_provider;
0779     struct device *dev = &pdev->dev;
0780     int ret;
0781     const struct of_device_id *match;
0782     struct pipe3_data *data;
0783 
0784     phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
0785     if (!phy)
0786         return -ENOMEM;
0787 
0788     match = of_match_device(ti_pipe3_id_table, dev);
0789     if (!match)
0790         return -EINVAL;
0791 
0792     data = (struct pipe3_data *)match->data;
0793     if (!data) {
0794         dev_err(dev, "no driver data\n");
0795         return -EINVAL;
0796     }
0797 
0798     phy->dev = dev;
0799     phy->mode = data->mode;
0800     phy->dpll_map = data->dpll_map;
0801     phy->settings = data->settings;
0802 
0803     ret = ti_pipe3_get_pll_base(phy);
0804     if (ret)
0805         return ret;
0806 
0807     ret = ti_pipe3_get_tx_rx_base(phy);
0808     if (ret)
0809         return ret;
0810 
0811     ret = ti_pipe3_get_sysctrl(phy);
0812     if (ret)
0813         return ret;
0814 
0815     ret = ti_pipe3_get_clk(phy);
0816     if (ret)
0817         return ret;
0818 
0819     platform_set_drvdata(pdev, phy);
0820     pm_runtime_enable(dev);
0821 
0822     /*
0823      * Prevent auto-disable of refclk for SATA PHY due to Errata i783
0824      */
0825     if (phy->mode == PIPE3_MODE_SATA) {
0826         if (!IS_ERR(phy->refclk)) {
0827             clk_prepare_enable(phy->refclk);
0828             phy->sata_refclk_enabled = true;
0829         }
0830     }
0831 
0832     generic_phy = devm_phy_create(dev, NULL, &ops);
0833     if (IS_ERR(generic_phy))
0834         return PTR_ERR(generic_phy);
0835 
0836     phy_set_drvdata(generic_phy, phy);
0837 
0838     ti_pipe3_power_off(generic_phy);
0839 
0840     phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
0841     return PTR_ERR_OR_ZERO(phy_provider);
0842 }
0843 
0844 static int ti_pipe3_remove(struct platform_device *pdev)
0845 {
0846     struct ti_pipe3 *phy = platform_get_drvdata(pdev);
0847 
0848     if (phy->mode == PIPE3_MODE_SATA) {
0849         clk_disable_unprepare(phy->refclk);
0850         phy->sata_refclk_enabled = false;
0851     }
0852     pm_runtime_disable(&pdev->dev);
0853 
0854     return 0;
0855 }
0856 
0857 static int ti_pipe3_enable_clocks(struct ti_pipe3 *phy)
0858 {
0859     int ret = 0;
0860 
0861     if (!IS_ERR(phy->refclk)) {
0862         ret = clk_prepare_enable(phy->refclk);
0863         if (ret) {
0864             dev_err(phy->dev, "Failed to enable refclk %d\n", ret);
0865             return ret;
0866         }
0867     }
0868 
0869     if (!IS_ERR(phy->wkupclk)) {
0870         ret = clk_prepare_enable(phy->wkupclk);
0871         if (ret) {
0872             dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
0873             goto disable_refclk;
0874         }
0875     }
0876 
0877     if (!IS_ERR(phy->div_clk)) {
0878         ret = clk_prepare_enable(phy->div_clk);
0879         if (ret) {
0880             dev_err(phy->dev, "Failed to enable div_clk %d\n", ret);
0881             goto disable_wkupclk;
0882         }
0883     }
0884 
0885     return 0;
0886 
0887 disable_wkupclk:
0888     if (!IS_ERR(phy->wkupclk))
0889         clk_disable_unprepare(phy->wkupclk);
0890 
0891 disable_refclk:
0892     if (!IS_ERR(phy->refclk))
0893         clk_disable_unprepare(phy->refclk);
0894 
0895     return ret;
0896 }
0897 
0898 static void ti_pipe3_disable_clocks(struct ti_pipe3 *phy)
0899 {
0900     if (!IS_ERR(phy->wkupclk))
0901         clk_disable_unprepare(phy->wkupclk);
0902     if (!IS_ERR(phy->refclk))
0903         clk_disable_unprepare(phy->refclk);
0904     if (!IS_ERR(phy->div_clk))
0905         clk_disable_unprepare(phy->div_clk);
0906 }
0907 
0908 static const struct of_device_id ti_pipe3_id_table[] = {
0909     {
0910         .compatible = "ti,phy-usb3",
0911         .data = &data_usb,
0912     },
0913     {
0914         .compatible = "ti,omap-usb3",
0915         .data = &data_usb,
0916     },
0917     {
0918         .compatible = "ti,phy-pipe3-sata",
0919         .data = &data_sata,
0920     },
0921     {
0922         .compatible = "ti,phy-pipe3-pcie",
0923         .data = &data_pcie,
0924     },
0925     {}
0926 };
0927 MODULE_DEVICE_TABLE(of, ti_pipe3_id_table);
0928 
0929 static struct platform_driver ti_pipe3_driver = {
0930     .probe      = ti_pipe3_probe,
0931     .remove     = ti_pipe3_remove,
0932     .driver     = {
0933         .name   = "ti-pipe3",
0934         .of_match_table = ti_pipe3_id_table,
0935     },
0936 };
0937 
0938 module_platform_driver(ti_pipe3_driver);
0939 
0940 MODULE_ALIAS("platform:ti_pipe3");
0941 MODULE_AUTHOR("Texas Instruments Inc.");
0942 MODULE_DESCRIPTION("TI PIPE3 phy driver");
0943 MODULE_LICENSE("GPL v2");