Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 1 | /* |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2 | * Copyright (c) 2013-2016, Linux Foundation. All rights reserved. |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #ifndef UFS_QCOM_PHY_I_H_ |
| 16 | #define UFS_QCOM_PHY_I_H_ |
| 17 | |
Yaniv Gardi | 39e794b | 2015-01-15 16:32:36 +0200 | [diff] [blame] | 18 | #include <linux/module.h> |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 19 | #include <linux/clk.h> |
Yaniv Gardi | 39e794b | 2015-01-15 16:32:36 +0200 | [diff] [blame] | 20 | #include <linux/regulator/consumer.h> |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 21 | #include <linux/slab.h> |
Yaniv Gardi | 39e794b | 2015-01-15 16:32:36 +0200 | [diff] [blame] | 22 | #include <linux/phy/phy-qcom-ufs.h> |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <linux/delay.h> |
| 26 | |
Yaniv Gardi | 39e794b | 2015-01-15 16:32:36 +0200 | [diff] [blame] | 27 | #define readl_poll_timeout(addr, val, cond, sleep_us, timeout_us) \ |
| 28 | ({ \ |
| 29 | ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \ |
| 30 | might_sleep_if(timeout_us); \ |
| 31 | for (;;) { \ |
| 32 | (val) = readl(addr); \ |
| 33 | if (cond) \ |
| 34 | break; \ |
| 35 | if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \ |
| 36 | (val) = readl(addr); \ |
| 37 | break; \ |
| 38 | } \ |
| 39 | if (sleep_us) \ |
| 40 | usleep_range(DIV_ROUND_UP(sleep_us, 4), sleep_us); \ |
| 41 | } \ |
| 42 | (cond) ? 0 : -ETIMEDOUT; \ |
| 43 | }) |
| 44 | |
| 45 | #define UFS_QCOM_PHY_CAL_ENTRY(reg, val) \ |
| 46 | { \ |
| 47 | .reg_offset = reg, \ |
| 48 | .cfg_value = val, \ |
| 49 | } |
| 50 | |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 51 | #define UFS_QCOM_PHY_NAME_LEN 30 |
| 52 | |
Yaniv Gardi | 39e794b | 2015-01-15 16:32:36 +0200 | [diff] [blame] | 53 | enum { |
| 54 | MASK_SERDES_START = 0x1, |
| 55 | MASK_PCS_READY = 0x1, |
| 56 | }; |
| 57 | |
| 58 | enum { |
| 59 | OFFSET_SERDES_START = 0x0, |
| 60 | }; |
| 61 | |
| 62 | struct ufs_qcom_phy_stored_attributes { |
| 63 | u32 att; |
| 64 | u32 value; |
| 65 | }; |
| 66 | |
| 67 | |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 68 | struct ufs_qcom_phy_calibration { |
| 69 | u32 reg_offset; |
| 70 | u32 cfg_value; |
| 71 | }; |
| 72 | |
| 73 | struct ufs_qcom_phy_vreg { |
| 74 | const char *name; |
| 75 | struct regulator *reg; |
| 76 | int max_uA; |
| 77 | int min_uV; |
| 78 | int max_uV; |
| 79 | bool enabled; |
| 80 | bool is_always_on; |
| 81 | }; |
| 82 | |
| 83 | struct ufs_qcom_phy { |
| 84 | struct list_head list; |
| 85 | struct device *dev; |
| 86 | void __iomem *mmio; |
| 87 | void __iomem *dev_ref_clk_ctrl_mmio; |
| 88 | struct clk *tx_iface_clk; |
| 89 | struct clk *rx_iface_clk; |
| 90 | bool is_iface_clk_enabled; |
| 91 | struct clk *ref_clk_src; |
| 92 | struct clk *ref_clk_parent; |
| 93 | struct clk *ref_clk; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 94 | struct clk *ref_aux_clk; |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 95 | bool is_ref_clk_enabled; |
| 96 | bool is_dev_ref_clk_enabled; |
| 97 | struct ufs_qcom_phy_vreg vdda_pll; |
| 98 | struct ufs_qcom_phy_vreg vdda_phy; |
| 99 | struct ufs_qcom_phy_vreg vddp_ref_clk; |
Subhash Jadavani | 8bfeb63 | 2017-04-03 17:14:30 -0700 | [diff] [blame] | 100 | |
| 101 | /* Number of lanes available (1 or 2) for Rx/Tx */ |
| 102 | u32 lanes_per_direction; |
| 103 | |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 104 | unsigned int quirks; |
| 105 | |
| 106 | /* |
| 107 | * If UFS link is put into Hibern8 and if UFS PHY analog hardware is |
| 108 | * power collapsed (by clearing UFS_PHY_POWER_DOWN_CONTROL), Hibern8 |
| 109 | * exit might fail even after powering on UFS PHY analog hardware. |
| 110 | * Enabling this quirk will help to solve above issue by doing |
| 111 | * custom PHY settings just before PHY analog power collapse. |
| 112 | */ |
| 113 | #define UFS_QCOM_PHY_QUIRK_HIBERN8_EXIT_AFTER_PHY_PWR_COLLAPSE BIT(0) |
| 114 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 115 | /* |
| 116 | * On some UFS PHY HW revisions, UFS PHY power up calibration sequence |
| 117 | * cannot have SVS mode configuration otherwise calibration result |
| 118 | * cannot be used in HS-G3. So there are additional register writes must |
| 119 | * be done after the PHY is initialized but before the controller |
| 120 | * requests hibernate exit. |
| 121 | */ |
| 122 | #define UFS_QCOM_PHY_QUIRK_SVS_MODE BIT(1) |
| 123 | |
| 124 | /* |
| 125 | * On some UFS PHY HW revisions, UFS PHY power up calibration sequence |
| 126 | * requires manual VCO tuning code and its better to rely on the VCO |
| 127 | * tuning code programmed by boot loader. Enable this quirk to enable |
| 128 | * programming the manually tuned VCO code. |
| 129 | */ |
| 130 | #define UFS_QCOM_PHY_QUIRK_VCO_MANUAL_TUNING BIT(2) |
| 131 | |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 132 | u8 host_ctrl_rev_major; |
| 133 | u16 host_ctrl_rev_minor; |
| 134 | u16 host_ctrl_rev_step; |
| 135 | |
| 136 | char name[UFS_QCOM_PHY_NAME_LEN]; |
| 137 | struct ufs_qcom_phy_calibration *cached_regs; |
| 138 | int cached_regs_table_size; |
| 139 | bool is_powered_on; |
| 140 | struct ufs_qcom_phy_specific_ops *phy_spec_ops; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 141 | u32 vco_tune1_mode1; |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | /** |
| 145 | * struct ufs_qcom_phy_specific_ops - set of pointers to functions which have a |
| 146 | * specific implementation per phy. Each UFS phy, should implement |
| 147 | * those functions according to its spec and requirements |
| 148 | * @calibrate_phy: pointer to a function that calibrate the phy |
| 149 | * @start_serdes: pointer to a function that starts the serdes |
| 150 | * @is_physical_coding_sublayer_ready: pointer to a function that |
| 151 | * checks pcs readiness. returns 0 for success and non-zero for error. |
| 152 | * @set_tx_lane_enable: pointer to a function that enable tx lanes |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 153 | * @ctrl_rx_linecfg: pointer to a function that controls the Host Rx LineCfg |
| 154 | * state. |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 155 | * @power_control: pointer to a function that controls analog rail of phy |
| 156 | * and writes to QSERDES_RX_SIGDET_CNTRL attribute |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 157 | * @configure_lpm: pointer to a function that configures the phy |
| 158 | * for low power mode. |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 159 | * @dbg_register_dump: pointer to a function that dumps phy registers for debug. |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 160 | */ |
| 161 | struct ufs_qcom_phy_specific_ops { |
| 162 | int (*calibrate_phy)(struct ufs_qcom_phy *phy, bool is_rate_B); |
| 163 | void (*start_serdes)(struct ufs_qcom_phy *phy); |
| 164 | int (*is_physical_coding_sublayer_ready)(struct ufs_qcom_phy *phy); |
| 165 | void (*set_tx_lane_enable)(struct ufs_qcom_phy *phy, u32 val); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 166 | void (*ctrl_rx_linecfg)(struct ufs_qcom_phy *phy, bool ctrl); |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 167 | void (*power_control)(struct ufs_qcom_phy *phy, bool val); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 168 | int (*configure_lpm)(struct ufs_qcom_phy *phy, bool enable); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 169 | void (*dbg_register_dump)(struct ufs_qcom_phy *phy); |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | struct ufs_qcom_phy *get_ufs_qcom_phy(struct phy *generic_phy); |
| 173 | int ufs_qcom_phy_power_on(struct phy *generic_phy); |
| 174 | int ufs_qcom_phy_power_off(struct phy *generic_phy); |
| 175 | int ufs_qcom_phy_exit(struct phy *generic_phy); |
| 176 | int ufs_qcom_phy_init_clks(struct phy *generic_phy, |
| 177 | struct ufs_qcom_phy *phy_common); |
| 178 | int ufs_qcom_phy_init_vregulators(struct phy *generic_phy, |
| 179 | struct ufs_qcom_phy *phy_common); |
| 180 | int ufs_qcom_phy_remove(struct phy *generic_phy, |
| 181 | struct ufs_qcom_phy *ufs_qcom_phy); |
| 182 | struct phy *ufs_qcom_phy_generic_probe(struct platform_device *pdev, |
| 183 | struct ufs_qcom_phy *common_cfg, |
Axel Lin | 4a9e5ca | 2015-07-15 15:33:51 +0800 | [diff] [blame] | 184 | const struct phy_ops *ufs_qcom_phy_gen_ops, |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 185 | struct ufs_qcom_phy_specific_ops *phy_spec_ops); |
| 186 | int ufs_qcom_phy_calibrate(struct ufs_qcom_phy *ufs_qcom_phy, |
| 187 | struct ufs_qcom_phy_calibration *tbl_A, int tbl_size_A, |
| 188 | struct ufs_qcom_phy_calibration *tbl_B, int tbl_size_B, |
| 189 | bool is_rate_B); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 190 | void ufs_qcom_phy_write_tbl(struct ufs_qcom_phy *ufs_qcom_phy, |
| 191 | struct ufs_qcom_phy_calibration *tbl, |
| 192 | int tbl_size); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 193 | void ufs_qcom_phy_dump_regs(struct ufs_qcom_phy *phy, |
| 194 | int offset, int len, char *prefix); |
Yaniv Gardi | adaafaa | 2015-01-15 16:32:35 +0200 | [diff] [blame] | 195 | #endif |