Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1 | /* |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 2 | * Copyright (c) 2013-2016, Linux Foundation. All rights reserved. |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +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 | #include <linux/time.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/phy/phy.h> |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 19 | #include <linux/phy/phy-qcom-ufs.h> |
Yaniv Gardi | 4b9ad0b | 2016-03-10 17:37:19 +0200 | [diff] [blame] | 20 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 21 | #include "ufshcd.h" |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 22 | #include "ufshcd-pltfrm.h" |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 23 | #include "unipro.h" |
| 24 | #include "ufs-qcom.h" |
| 25 | #include "ufshci.h" |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 26 | #define UFS_QCOM_DEFAULT_DBG_PRINT_EN \ |
| 27 | (UFS_QCOM_DBG_PRINT_REGS_EN | UFS_QCOM_DBG_PRINT_TEST_BUS_EN) |
| 28 | |
| 29 | enum { |
| 30 | TSTBUS_UAWM, |
| 31 | TSTBUS_UARM, |
| 32 | TSTBUS_TXUC, |
| 33 | TSTBUS_RXUC, |
| 34 | TSTBUS_DFC, |
| 35 | TSTBUS_TRLUT, |
| 36 | TSTBUS_TMRLUT, |
| 37 | TSTBUS_OCSC, |
| 38 | TSTBUS_UTP_HCI, |
| 39 | TSTBUS_COMBINED, |
| 40 | TSTBUS_WRAPPER, |
| 41 | TSTBUS_UNIPRO, |
| 42 | TSTBUS_MAX, |
| 43 | }; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 44 | |
| 45 | static struct ufs_qcom_host *ufs_qcom_hosts[MAX_UFS_QCOM_HOSTS]; |
| 46 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 47 | static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote); |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 48 | static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host); |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 49 | static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba *hba, |
| 50 | u32 clk_cycles); |
| 51 | |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 52 | static void ufs_qcom_dump_regs(struct ufs_hba *hba, int offset, int len, |
| 53 | char *prefix) |
| 54 | { |
| 55 | print_hex_dump(KERN_ERR, prefix, |
| 56 | len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE, |
| 57 | 16, 4, (void __force *)hba->mmio_base + offset, |
| 58 | len * 4, false); |
| 59 | } |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 60 | |
Yaniv Gardi | eba5ed3 | 2016-03-10 17:37:21 +0200 | [diff] [blame] | 61 | static void ufs_qcom_dump_regs_wrapper(struct ufs_hba *hba, int offset, int len, |
| 62 | char *prefix, void *priv) |
| 63 | { |
| 64 | ufs_qcom_dump_regs(hba, offset, len, prefix); |
| 65 | } |
| 66 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 67 | static int ufs_qcom_get_connected_tx_lanes(struct ufs_hba *hba, u32 *tx_lanes) |
| 68 | { |
| 69 | int err = 0; |
| 70 | |
| 71 | err = ufshcd_dme_get(hba, |
| 72 | UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), tx_lanes); |
| 73 | if (err) |
| 74 | dev_err(hba->dev, "%s: couldn't read PA_CONNECTEDTXDATALANES %d\n", |
| 75 | __func__, err); |
| 76 | |
| 77 | return err; |
| 78 | } |
| 79 | |
| 80 | static int ufs_qcom_host_clk_get(struct device *dev, |
| 81 | const char *name, struct clk **clk_out) |
| 82 | { |
| 83 | struct clk *clk; |
| 84 | int err = 0; |
| 85 | |
| 86 | clk = devm_clk_get(dev, name); |
| 87 | if (IS_ERR(clk)) { |
| 88 | err = PTR_ERR(clk); |
| 89 | dev_err(dev, "%s: failed to get %s err %d", |
| 90 | __func__, name, err); |
| 91 | } else { |
| 92 | *clk_out = clk; |
| 93 | } |
| 94 | |
| 95 | return err; |
| 96 | } |
| 97 | |
| 98 | static int ufs_qcom_host_clk_enable(struct device *dev, |
| 99 | const char *name, struct clk *clk) |
| 100 | { |
| 101 | int err = 0; |
| 102 | |
| 103 | err = clk_prepare_enable(clk); |
| 104 | if (err) |
| 105 | dev_err(dev, "%s: %s enable failed %d\n", __func__, name, err); |
| 106 | |
| 107 | return err; |
| 108 | } |
| 109 | |
| 110 | static void ufs_qcom_disable_lane_clks(struct ufs_qcom_host *host) |
| 111 | { |
| 112 | if (!host->is_lane_clks_enabled) |
| 113 | return; |
| 114 | |
Yaniv Gardi | 4b9ad0b | 2016-03-10 17:37:19 +0200 | [diff] [blame] | 115 | if (host->hba->lanes_per_direction > 1) |
| 116 | clk_disable_unprepare(host->tx_l1_sync_clk); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 117 | clk_disable_unprepare(host->tx_l0_sync_clk); |
Yaniv Gardi | 4b9ad0b | 2016-03-10 17:37:19 +0200 | [diff] [blame] | 118 | if (host->hba->lanes_per_direction > 1) |
| 119 | clk_disable_unprepare(host->rx_l1_sync_clk); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 120 | clk_disable_unprepare(host->rx_l0_sync_clk); |
| 121 | |
| 122 | host->is_lane_clks_enabled = false; |
| 123 | } |
| 124 | |
| 125 | static int ufs_qcom_enable_lane_clks(struct ufs_qcom_host *host) |
| 126 | { |
| 127 | int err = 0; |
| 128 | struct device *dev = host->hba->dev; |
| 129 | |
| 130 | if (host->is_lane_clks_enabled) |
| 131 | return 0; |
| 132 | |
| 133 | err = ufs_qcom_host_clk_enable(dev, "rx_lane0_sync_clk", |
| 134 | host->rx_l0_sync_clk); |
| 135 | if (err) |
| 136 | goto out; |
| 137 | |
| 138 | err = ufs_qcom_host_clk_enable(dev, "tx_lane0_sync_clk", |
| 139 | host->tx_l0_sync_clk); |
| 140 | if (err) |
| 141 | goto disable_rx_l0; |
| 142 | |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 143 | if (host->hba->lanes_per_direction > 1) { |
| 144 | err = ufs_qcom_host_clk_enable(dev, "rx_lane1_sync_clk", |
| 145 | host->rx_l1_sync_clk); |
| 146 | if (err) |
| 147 | goto disable_tx_l0; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 148 | |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 149 | err = ufs_qcom_host_clk_enable(dev, "tx_lane1_sync_clk", |
| 150 | host->tx_l1_sync_clk); |
| 151 | if (err) |
| 152 | goto disable_rx_l1; |
| 153 | } |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 154 | |
| 155 | host->is_lane_clks_enabled = true; |
| 156 | goto out; |
| 157 | |
| 158 | disable_rx_l1: |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 159 | if (host->hba->lanes_per_direction > 1) |
| 160 | clk_disable_unprepare(host->rx_l1_sync_clk); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 161 | disable_tx_l0: |
| 162 | clk_disable_unprepare(host->tx_l0_sync_clk); |
| 163 | disable_rx_l0: |
| 164 | clk_disable_unprepare(host->rx_l0_sync_clk); |
| 165 | out: |
| 166 | return err; |
| 167 | } |
| 168 | |
| 169 | static int ufs_qcom_init_lane_clks(struct ufs_qcom_host *host) |
| 170 | { |
| 171 | int err = 0; |
| 172 | struct device *dev = host->hba->dev; |
| 173 | |
| 174 | err = ufs_qcom_host_clk_get(dev, |
| 175 | "rx_lane0_sync_clk", &host->rx_l0_sync_clk); |
| 176 | if (err) |
| 177 | goto out; |
| 178 | |
| 179 | err = ufs_qcom_host_clk_get(dev, |
| 180 | "tx_lane0_sync_clk", &host->tx_l0_sync_clk); |
| 181 | if (err) |
| 182 | goto out; |
| 183 | |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 184 | /* In case of single lane per direction, don't read lane1 clocks */ |
| 185 | if (host->hba->lanes_per_direction > 1) { |
| 186 | err = ufs_qcom_host_clk_get(dev, "rx_lane1_sync_clk", |
| 187 | &host->rx_l1_sync_clk); |
| 188 | if (err) |
| 189 | goto out; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 190 | |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 191 | err = ufs_qcom_host_clk_get(dev, "tx_lane1_sync_clk", |
| 192 | &host->tx_l1_sync_clk); |
| 193 | } |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 194 | out: |
| 195 | return err; |
| 196 | } |
| 197 | |
| 198 | static int ufs_qcom_link_startup_post_change(struct ufs_hba *hba) |
| 199 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 200 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 201 | struct phy *phy = host->generic_phy; |
| 202 | u32 tx_lanes; |
| 203 | int err = 0; |
| 204 | |
| 205 | err = ufs_qcom_get_connected_tx_lanes(hba, &tx_lanes); |
| 206 | if (err) |
| 207 | goto out; |
| 208 | |
| 209 | err = ufs_qcom_phy_set_tx_lane_enable(phy, tx_lanes); |
| 210 | if (err) |
| 211 | dev_err(hba->dev, "%s: ufs_qcom_phy_set_tx_lane_enable failed\n", |
| 212 | __func__); |
| 213 | |
| 214 | out: |
| 215 | return err; |
| 216 | } |
| 217 | |
| 218 | static int ufs_qcom_check_hibern8(struct ufs_hba *hba) |
| 219 | { |
| 220 | int err; |
| 221 | u32 tx_fsm_val = 0; |
| 222 | unsigned long timeout = jiffies + msecs_to_jiffies(HBRN8_POLL_TOUT_MS); |
| 223 | |
| 224 | do { |
| 225 | err = ufshcd_dme_get(hba, |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 226 | UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE, |
| 227 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 228 | &tx_fsm_val); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 229 | if (err || tx_fsm_val == TX_FSM_HIBERN8) |
| 230 | break; |
| 231 | |
| 232 | /* sleep for max. 200us */ |
| 233 | usleep_range(100, 200); |
| 234 | } while (time_before(jiffies, timeout)); |
| 235 | |
| 236 | /* |
| 237 | * we might have scheduled out for long during polling so |
| 238 | * check the state again. |
| 239 | */ |
| 240 | if (time_after(jiffies, timeout)) |
| 241 | err = ufshcd_dme_get(hba, |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 242 | UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE, |
| 243 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 244 | &tx_fsm_val); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 245 | |
| 246 | if (err) { |
| 247 | dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n", |
| 248 | __func__, err); |
| 249 | } else if (tx_fsm_val != TX_FSM_HIBERN8) { |
| 250 | err = tx_fsm_val; |
| 251 | dev_err(hba->dev, "%s: invalid TX_FSM_STATE = %d\n", |
| 252 | __func__, err); |
| 253 | } |
| 254 | |
| 255 | return err; |
| 256 | } |
| 257 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 258 | static void ufs_qcom_select_unipro_mode(struct ufs_qcom_host *host) |
| 259 | { |
| 260 | ufshcd_rmwl(host->hba, QUNIPRO_SEL, |
| 261 | ufs_qcom_cap_qunipro(host) ? QUNIPRO_SEL : 0, |
| 262 | REG_UFS_CFG1); |
| 263 | /* make sure above configuration is applied before we return */ |
| 264 | mb(); |
| 265 | } |
| 266 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 267 | static int ufs_qcom_power_up_sequence(struct ufs_hba *hba) |
| 268 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 269 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 270 | struct phy *phy = host->generic_phy; |
| 271 | int ret = 0; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 272 | bool is_rate_B = (UFS_QCOM_LIMIT_HS_RATE == PA_HS_MODE_B) |
| 273 | ? true : false; |
| 274 | |
| 275 | /* Assert PHY reset and apply PHY calibration values */ |
| 276 | ufs_qcom_assert_reset(hba); |
| 277 | /* provide 1ms delay to let the reset pulse propagate */ |
| 278 | usleep_range(1000, 1100); |
| 279 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 280 | ret = ufs_qcom_phy_calibrate_phy(phy, is_rate_B); |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 281 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 282 | if (ret) { |
Yaniv Gardi | 4b9ad0b | 2016-03-10 17:37:19 +0200 | [diff] [blame] | 283 | dev_err(hba->dev, "%s: ufs_qcom_phy_calibrate_phy() failed, ret = %d\n", |
| 284 | __func__, ret); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 285 | goto out; |
| 286 | } |
| 287 | |
| 288 | /* De-assert PHY reset and start serdes */ |
| 289 | ufs_qcom_deassert_reset(hba); |
| 290 | |
| 291 | /* |
| 292 | * after reset deassertion, phy will need all ref clocks, |
| 293 | * voltage, current to settle down before starting serdes. |
| 294 | */ |
| 295 | usleep_range(1000, 1100); |
| 296 | ret = ufs_qcom_phy_start_serdes(phy); |
| 297 | if (ret) { |
| 298 | dev_err(hba->dev, "%s: ufs_qcom_phy_start_serdes() failed, ret = %d\n", |
| 299 | __func__, ret); |
| 300 | goto out; |
| 301 | } |
| 302 | |
| 303 | ret = ufs_qcom_phy_is_pcs_ready(phy); |
| 304 | if (ret) |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 305 | dev_err(hba->dev, |
| 306 | "%s: is_physical_coding_sublayer_ready() failed, ret = %d\n", |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 307 | __func__, ret); |
| 308 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 309 | ufs_qcom_select_unipro_mode(host); |
| 310 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 311 | out: |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * The UTP controller has a number of internal clock gating cells (CGCs). |
| 317 | * Internal hardware sub-modules within the UTP controller control the CGCs. |
| 318 | * Hardware CGCs disable the clock to inactivate UTP sub-modules not involved |
| 319 | * in a specific operation, UTP controller CGCs are by default disabled and |
| 320 | * this function enables them (after every UFS link startup) to save some power |
| 321 | * leakage. |
| 322 | */ |
| 323 | static void ufs_qcom_enable_hw_clk_gating(struct ufs_hba *hba) |
| 324 | { |
| 325 | ufshcd_writel(hba, |
| 326 | ufshcd_readl(hba, REG_UFS_CFG2) | REG_UFS_CFG2_CGC_EN_ALL, |
| 327 | REG_UFS_CFG2); |
| 328 | |
| 329 | /* Ensure that HW clock gating is enabled before next operations */ |
| 330 | mb(); |
| 331 | } |
| 332 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 333 | static int ufs_qcom_hce_enable_notify(struct ufs_hba *hba, |
| 334 | enum ufs_notify_change_status status) |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 335 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 336 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 337 | int err = 0; |
| 338 | |
| 339 | switch (status) { |
| 340 | case PRE_CHANGE: |
| 341 | ufs_qcom_power_up_sequence(hba); |
| 342 | /* |
| 343 | * The PHY PLL output is the source of tx/rx lane symbol |
| 344 | * clocks, hence, enable the lane clocks only after PHY |
| 345 | * is initialized. |
| 346 | */ |
| 347 | err = ufs_qcom_enable_lane_clks(host); |
| 348 | break; |
| 349 | case POST_CHANGE: |
| 350 | /* check if UFS PHY moved from DISABLED to HIBERN8 */ |
| 351 | err = ufs_qcom_check_hibern8(hba); |
| 352 | ufs_qcom_enable_hw_clk_gating(hba); |
| 353 | |
| 354 | break; |
| 355 | default: |
| 356 | dev_err(hba->dev, "%s: invalid status %d\n", __func__, status); |
| 357 | err = -EINVAL; |
| 358 | break; |
| 359 | } |
| 360 | return err; |
| 361 | } |
| 362 | |
| 363 | /** |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 364 | * Returns zero for success and non-zero in case of a failure |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 365 | */ |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 366 | static int ufs_qcom_cfg_timers(struct ufs_hba *hba, u32 gear, |
| 367 | u32 hs, u32 rate, bool update_link_startup_timer) |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 368 | { |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 369 | int ret = 0; |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 370 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 371 | struct ufs_clk_info *clki; |
| 372 | u32 core_clk_period_in_ns; |
| 373 | u32 tx_clk_cycles_per_us = 0; |
| 374 | unsigned long core_clk_rate = 0; |
| 375 | u32 core_clk_cycles_per_us = 0; |
| 376 | |
| 377 | static u32 pwm_fr_table[][2] = { |
| 378 | {UFS_PWM_G1, 0x1}, |
| 379 | {UFS_PWM_G2, 0x1}, |
| 380 | {UFS_PWM_G3, 0x1}, |
| 381 | {UFS_PWM_G4, 0x1}, |
| 382 | }; |
| 383 | |
| 384 | static u32 hs_fr_table_rA[][2] = { |
| 385 | {UFS_HS_G1, 0x1F}, |
| 386 | {UFS_HS_G2, 0x3e}, |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 387 | {UFS_HS_G3, 0x7D}, |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 388 | }; |
| 389 | |
| 390 | static u32 hs_fr_table_rB[][2] = { |
| 391 | {UFS_HS_G1, 0x24}, |
| 392 | {UFS_HS_G2, 0x49}, |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 393 | {UFS_HS_G3, 0x92}, |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 394 | }; |
| 395 | |
Yaniv Gardi | 81c7e06 | 2015-05-17 18:54:58 +0300 | [diff] [blame] | 396 | /* |
| 397 | * The Qunipro controller does not use following registers: |
| 398 | * SYS1CLK_1US_REG, TX_SYMBOL_CLK_1US_REG, CLK_NS_REG & |
| 399 | * UFS_REG_PA_LINK_STARTUP_TIMER |
| 400 | * But UTP controller uses SYS1CLK_1US_REG register for Interrupt |
| 401 | * Aggregation logic. |
| 402 | */ |
| 403 | if (ufs_qcom_cap_qunipro(host) && !ufshcd_is_intr_aggr_allowed(hba)) |
| 404 | goto out; |
| 405 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 406 | if (gear == 0) { |
| 407 | dev_err(hba->dev, "%s: invalid gear = %d\n", __func__, gear); |
| 408 | goto out_error; |
| 409 | } |
| 410 | |
| 411 | list_for_each_entry(clki, &hba->clk_list_head, list) { |
| 412 | if (!strcmp(clki->name, "core_clk")) |
| 413 | core_clk_rate = clk_get_rate(clki->clk); |
| 414 | } |
| 415 | |
| 416 | /* If frequency is smaller than 1MHz, set to 1MHz */ |
| 417 | if (core_clk_rate < DEFAULT_CLK_RATE_HZ) |
| 418 | core_clk_rate = DEFAULT_CLK_RATE_HZ; |
| 419 | |
| 420 | core_clk_cycles_per_us = core_clk_rate / USEC_PER_SEC; |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 421 | if (ufshcd_readl(hba, REG_UFS_SYS1CLK_1US) != core_clk_cycles_per_us) { |
| 422 | ufshcd_writel(hba, core_clk_cycles_per_us, REG_UFS_SYS1CLK_1US); |
| 423 | /* |
| 424 | * make sure above write gets applied before we return from |
| 425 | * this function. |
| 426 | */ |
| 427 | mb(); |
| 428 | } |
| 429 | |
| 430 | if (ufs_qcom_cap_qunipro(host)) |
| 431 | goto out; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 432 | |
| 433 | core_clk_period_in_ns = NSEC_PER_SEC / core_clk_rate; |
| 434 | core_clk_period_in_ns <<= OFFSET_CLK_NS_REG; |
| 435 | core_clk_period_in_ns &= MASK_CLK_NS_REG; |
| 436 | |
| 437 | switch (hs) { |
| 438 | case FASTAUTO_MODE: |
| 439 | case FAST_MODE: |
| 440 | if (rate == PA_HS_MODE_A) { |
| 441 | if (gear > ARRAY_SIZE(hs_fr_table_rA)) { |
| 442 | dev_err(hba->dev, |
| 443 | "%s: index %d exceeds table size %zu\n", |
| 444 | __func__, gear, |
| 445 | ARRAY_SIZE(hs_fr_table_rA)); |
| 446 | goto out_error; |
| 447 | } |
| 448 | tx_clk_cycles_per_us = hs_fr_table_rA[gear-1][1]; |
| 449 | } else if (rate == PA_HS_MODE_B) { |
| 450 | if (gear > ARRAY_SIZE(hs_fr_table_rB)) { |
| 451 | dev_err(hba->dev, |
| 452 | "%s: index %d exceeds table size %zu\n", |
| 453 | __func__, gear, |
| 454 | ARRAY_SIZE(hs_fr_table_rB)); |
| 455 | goto out_error; |
| 456 | } |
| 457 | tx_clk_cycles_per_us = hs_fr_table_rB[gear-1][1]; |
| 458 | } else { |
| 459 | dev_err(hba->dev, "%s: invalid rate = %d\n", |
| 460 | __func__, rate); |
| 461 | goto out_error; |
| 462 | } |
| 463 | break; |
| 464 | case SLOWAUTO_MODE: |
| 465 | case SLOW_MODE: |
| 466 | if (gear > ARRAY_SIZE(pwm_fr_table)) { |
| 467 | dev_err(hba->dev, |
| 468 | "%s: index %d exceeds table size %zu\n", |
| 469 | __func__, gear, |
| 470 | ARRAY_SIZE(pwm_fr_table)); |
| 471 | goto out_error; |
| 472 | } |
| 473 | tx_clk_cycles_per_us = pwm_fr_table[gear-1][1]; |
| 474 | break; |
| 475 | case UNCHANGED: |
| 476 | default: |
| 477 | dev_err(hba->dev, "%s: invalid mode = %d\n", __func__, hs); |
| 478 | goto out_error; |
| 479 | } |
| 480 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 481 | if (ufshcd_readl(hba, REG_UFS_TX_SYMBOL_CLK_NS_US) != |
| 482 | (core_clk_period_in_ns | tx_clk_cycles_per_us)) { |
| 483 | /* this register 2 fields shall be written at once */ |
| 484 | ufshcd_writel(hba, core_clk_period_in_ns | tx_clk_cycles_per_us, |
| 485 | REG_UFS_TX_SYMBOL_CLK_NS_US); |
| 486 | /* |
| 487 | * make sure above write gets applied before we return from |
| 488 | * this function. |
| 489 | */ |
| 490 | mb(); |
| 491 | } |
| 492 | |
| 493 | if (update_link_startup_timer) { |
| 494 | ufshcd_writel(hba, ((core_clk_rate / MSEC_PER_SEC) * 100), |
| 495 | REG_UFS_PA_LINK_STARTUP_TIMER); |
| 496 | /* |
| 497 | * make sure that this configuration is applied before |
| 498 | * we return |
| 499 | */ |
| 500 | mb(); |
| 501 | } |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 502 | goto out; |
| 503 | |
| 504 | out_error: |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 505 | ret = -EINVAL; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 506 | out: |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 507 | return ret; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 510 | static int ufs_qcom_link_startup_notify(struct ufs_hba *hba, |
| 511 | enum ufs_notify_change_status status) |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 512 | { |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 513 | int err = 0; |
| 514 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 515 | |
| 516 | switch (status) { |
| 517 | case PRE_CHANGE: |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 518 | if (ufs_qcom_cfg_timers(hba, UFS_PWM_G1, SLOWAUTO_MODE, |
| 519 | 0, true)) { |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 520 | dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n", |
| 521 | __func__); |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 522 | err = -EINVAL; |
| 523 | goto out; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 524 | } |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 525 | |
| 526 | if (ufs_qcom_cap_qunipro(host)) |
| 527 | /* |
| 528 | * set unipro core clock cycles to 150 & clear clock |
| 529 | * divider |
| 530 | */ |
| 531 | err = ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, |
| 532 | 150); |
| 533 | |
Yaniv Gardi | 4b9ad0b | 2016-03-10 17:37:19 +0200 | [diff] [blame] | 534 | /* |
| 535 | * Some UFS devices (and may be host) have issues if LCC is |
| 536 | * enabled. So we are setting PA_Local_TX_LCC_Enable to 0 |
| 537 | * before link startup which will make sure that both host |
| 538 | * and device TX LCC are disabled once link startup is |
| 539 | * completed. |
| 540 | */ |
| 541 | if (ufshcd_get_local_unipro_ver(hba) != UFS_UNIPRO_VER_1_41) |
| 542 | err = ufshcd_dme_set(hba, |
| 543 | UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE), |
| 544 | 0); |
| 545 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 546 | break; |
| 547 | case POST_CHANGE: |
| 548 | ufs_qcom_link_startup_post_change(hba); |
| 549 | break; |
| 550 | default: |
| 551 | break; |
| 552 | } |
| 553 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 554 | out: |
| 555 | return err; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static int ufs_qcom_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 559 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 560 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 561 | struct phy *phy = host->generic_phy; |
| 562 | int ret = 0; |
| 563 | |
| 564 | if (ufs_qcom_is_link_off(hba)) { |
| 565 | /* |
| 566 | * Disable the tx/rx lane symbol clocks before PHY is |
| 567 | * powered down as the PLL source should be disabled |
| 568 | * after downstream clocks are disabled. |
| 569 | */ |
| 570 | ufs_qcom_disable_lane_clks(host); |
| 571 | phy_power_off(phy); |
| 572 | |
| 573 | /* Assert PHY soft reset */ |
| 574 | ufs_qcom_assert_reset(hba); |
| 575 | goto out; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * If UniPro link is not active, PHY ref_clk, main PHY analog power |
| 580 | * rail and low noise analog power rail for PLL can be switched off. |
| 581 | */ |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 582 | if (!ufs_qcom_is_link_active(hba)) { |
| 583 | ufs_qcom_disable_lane_clks(host); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 584 | phy_power_off(phy); |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 585 | } |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 586 | |
| 587 | out: |
| 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | static int ufs_qcom_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 592 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 593 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 594 | struct phy *phy = host->generic_phy; |
| 595 | int err; |
| 596 | |
| 597 | err = phy_power_on(phy); |
| 598 | if (err) { |
| 599 | dev_err(hba->dev, "%s: failed enabling regs, err = %d\n", |
| 600 | __func__, err); |
| 601 | goto out; |
| 602 | } |
| 603 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 604 | err = ufs_qcom_enable_lane_clks(host); |
| 605 | if (err) |
| 606 | goto out; |
| 607 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 608 | hba->is_sys_suspended = false; |
| 609 | |
| 610 | out: |
| 611 | return err; |
| 612 | } |
| 613 | |
| 614 | struct ufs_qcom_dev_params { |
| 615 | u32 pwm_rx_gear; /* pwm rx gear to work in */ |
| 616 | u32 pwm_tx_gear; /* pwm tx gear to work in */ |
| 617 | u32 hs_rx_gear; /* hs rx gear to work in */ |
| 618 | u32 hs_tx_gear; /* hs tx gear to work in */ |
| 619 | u32 rx_lanes; /* number of rx lanes */ |
| 620 | u32 tx_lanes; /* number of tx lanes */ |
| 621 | u32 rx_pwr_pwm; /* rx pwm working pwr */ |
| 622 | u32 tx_pwr_pwm; /* tx pwm working pwr */ |
| 623 | u32 rx_pwr_hs; /* rx hs working pwr */ |
| 624 | u32 tx_pwr_hs; /* tx hs working pwr */ |
| 625 | u32 hs_rate; /* rate A/B to work in HS */ |
| 626 | u32 desired_working_mode; |
| 627 | }; |
| 628 | |
| 629 | static int ufs_qcom_get_pwr_dev_param(struct ufs_qcom_dev_params *qcom_param, |
| 630 | struct ufs_pa_layer_attr *dev_max, |
| 631 | struct ufs_pa_layer_attr *agreed_pwr) |
| 632 | { |
| 633 | int min_qcom_gear; |
| 634 | int min_dev_gear; |
| 635 | bool is_dev_sup_hs = false; |
| 636 | bool is_qcom_max_hs = false; |
| 637 | |
| 638 | if (dev_max->pwr_rx == FAST_MODE) |
| 639 | is_dev_sup_hs = true; |
| 640 | |
| 641 | if (qcom_param->desired_working_mode == FAST) { |
| 642 | is_qcom_max_hs = true; |
| 643 | min_qcom_gear = min_t(u32, qcom_param->hs_rx_gear, |
| 644 | qcom_param->hs_tx_gear); |
| 645 | } else { |
| 646 | min_qcom_gear = min_t(u32, qcom_param->pwm_rx_gear, |
| 647 | qcom_param->pwm_tx_gear); |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * device doesn't support HS but qcom_param->desired_working_mode is |
| 652 | * HS, thus device and qcom_param don't agree |
| 653 | */ |
| 654 | if (!is_dev_sup_hs && is_qcom_max_hs) { |
| 655 | pr_err("%s: failed to agree on power mode (device doesn't support HS but requested power is HS)\n", |
| 656 | __func__); |
| 657 | return -ENOTSUPP; |
| 658 | } else if (is_dev_sup_hs && is_qcom_max_hs) { |
| 659 | /* |
| 660 | * since device supports HS, it supports FAST_MODE. |
| 661 | * since qcom_param->desired_working_mode is also HS |
| 662 | * then final decision (FAST/FASTAUTO) is done according |
| 663 | * to qcom_params as it is the restricting factor |
| 664 | */ |
| 665 | agreed_pwr->pwr_rx = agreed_pwr->pwr_tx = |
| 666 | qcom_param->rx_pwr_hs; |
| 667 | } else { |
| 668 | /* |
| 669 | * here qcom_param->desired_working_mode is PWM. |
| 670 | * it doesn't matter whether device supports HS or PWM, |
| 671 | * in both cases qcom_param->desired_working_mode will |
| 672 | * determine the mode |
| 673 | */ |
| 674 | agreed_pwr->pwr_rx = agreed_pwr->pwr_tx = |
| 675 | qcom_param->rx_pwr_pwm; |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * we would like tx to work in the minimum number of lanes |
| 680 | * between device capability and vendor preferences. |
| 681 | * the same decision will be made for rx |
| 682 | */ |
| 683 | agreed_pwr->lane_tx = min_t(u32, dev_max->lane_tx, |
| 684 | qcom_param->tx_lanes); |
| 685 | agreed_pwr->lane_rx = min_t(u32, dev_max->lane_rx, |
| 686 | qcom_param->rx_lanes); |
| 687 | |
| 688 | /* device maximum gear is the minimum between device rx and tx gears */ |
| 689 | min_dev_gear = min_t(u32, dev_max->gear_rx, dev_max->gear_tx); |
| 690 | |
| 691 | /* |
| 692 | * if both device capabilities and vendor pre-defined preferences are |
| 693 | * both HS or both PWM then set the minimum gear to be the chosen |
| 694 | * working gear. |
| 695 | * if one is PWM and one is HS then the one that is PWM get to decide |
| 696 | * what is the gear, as it is the one that also decided previously what |
| 697 | * pwr the device will be configured to. |
| 698 | */ |
| 699 | if ((is_dev_sup_hs && is_qcom_max_hs) || |
| 700 | (!is_dev_sup_hs && !is_qcom_max_hs)) |
| 701 | agreed_pwr->gear_rx = agreed_pwr->gear_tx = |
| 702 | min_t(u32, min_dev_gear, min_qcom_gear); |
| 703 | else if (!is_dev_sup_hs) |
| 704 | agreed_pwr->gear_rx = agreed_pwr->gear_tx = min_dev_gear; |
| 705 | else |
| 706 | agreed_pwr->gear_rx = agreed_pwr->gear_tx = min_qcom_gear; |
| 707 | |
| 708 | agreed_pwr->hs_rate = qcom_param->hs_rate; |
| 709 | return 0; |
| 710 | } |
| 711 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 712 | #ifdef CONFIG_MSM_BUS_SCALING |
| 713 | static int ufs_qcom_get_bus_vote(struct ufs_qcom_host *host, |
| 714 | const char *speed_mode) |
| 715 | { |
| 716 | struct device *dev = host->hba->dev; |
| 717 | struct device_node *np = dev->of_node; |
| 718 | int err; |
| 719 | const char *key = "qcom,bus-vector-names"; |
| 720 | |
| 721 | if (!speed_mode) { |
| 722 | err = -EINVAL; |
| 723 | goto out; |
| 724 | } |
| 725 | |
| 726 | if (host->bus_vote.is_max_bw_needed && !!strcmp(speed_mode, "MIN")) |
| 727 | err = of_property_match_string(np, key, "MAX"); |
| 728 | else |
| 729 | err = of_property_match_string(np, key, speed_mode); |
| 730 | |
| 731 | out: |
| 732 | if (err < 0) |
| 733 | dev_err(dev, "%s: Invalid %s mode %d\n", |
| 734 | __func__, speed_mode, err); |
| 735 | return err; |
| 736 | } |
| 737 | |
| 738 | static void ufs_qcom_get_speed_mode(struct ufs_pa_layer_attr *p, char *result) |
| 739 | { |
| 740 | int gear = max_t(u32, p->gear_rx, p->gear_tx); |
| 741 | int lanes = max_t(u32, p->lane_rx, p->lane_tx); |
| 742 | int pwr; |
| 743 | |
| 744 | /* default to PWM Gear 1, Lane 1 if power mode is not initialized */ |
| 745 | if (!gear) |
| 746 | gear = 1; |
| 747 | |
| 748 | if (!lanes) |
| 749 | lanes = 1; |
| 750 | |
| 751 | if (!p->pwr_rx && !p->pwr_tx) { |
| 752 | pwr = SLOWAUTO_MODE; |
| 753 | snprintf(result, BUS_VECTOR_NAME_LEN, "MIN"); |
| 754 | } else if (p->pwr_rx == FAST_MODE || p->pwr_rx == FASTAUTO_MODE || |
| 755 | p->pwr_tx == FAST_MODE || p->pwr_tx == FASTAUTO_MODE) { |
| 756 | pwr = FAST_MODE; |
| 757 | snprintf(result, BUS_VECTOR_NAME_LEN, "%s_R%s_G%d_L%d", "HS", |
| 758 | p->hs_rate == PA_HS_MODE_B ? "B" : "A", gear, lanes); |
| 759 | } else { |
| 760 | pwr = SLOW_MODE; |
| 761 | snprintf(result, BUS_VECTOR_NAME_LEN, "%s_G%d_L%d", |
| 762 | "PWM", gear, lanes); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote) |
| 767 | { |
| 768 | int err = 0; |
| 769 | |
| 770 | if (vote != host->bus_vote.curr_vote) { |
| 771 | err = msm_bus_scale_client_update_request( |
| 772 | host->bus_vote.client_handle, vote); |
| 773 | if (err) { |
| 774 | dev_err(host->hba->dev, |
| 775 | "%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n", |
| 776 | __func__, host->bus_vote.client_handle, |
| 777 | vote, err); |
| 778 | goto out; |
| 779 | } |
| 780 | |
| 781 | host->bus_vote.curr_vote = vote; |
| 782 | } |
| 783 | out: |
| 784 | return err; |
| 785 | } |
| 786 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 787 | static int ufs_qcom_update_bus_bw_vote(struct ufs_qcom_host *host) |
| 788 | { |
| 789 | int vote; |
| 790 | int err = 0; |
| 791 | char mode[BUS_VECTOR_NAME_LEN]; |
| 792 | |
| 793 | ufs_qcom_get_speed_mode(&host->dev_req_params, mode); |
| 794 | |
| 795 | vote = ufs_qcom_get_bus_vote(host, mode); |
| 796 | if (vote >= 0) |
| 797 | err = ufs_qcom_set_bus_vote(host, vote); |
| 798 | else |
| 799 | err = vote; |
| 800 | |
| 801 | if (err) |
| 802 | dev_err(host->hba->dev, "%s: failed %d\n", __func__, err); |
| 803 | else |
| 804 | host->bus_vote.saved_vote = vote; |
| 805 | return err; |
| 806 | } |
| 807 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 808 | static ssize_t |
| 809 | show_ufs_to_mem_max_bus_bw(struct device *dev, struct device_attribute *attr, |
| 810 | char *buf) |
| 811 | { |
| 812 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 813 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
| 814 | |
| 815 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 816 | host->bus_vote.is_max_bw_needed); |
| 817 | } |
| 818 | |
| 819 | static ssize_t |
| 820 | store_ufs_to_mem_max_bus_bw(struct device *dev, struct device_attribute *attr, |
| 821 | const char *buf, size_t count) |
| 822 | { |
| 823 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 824 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
| 825 | uint32_t value; |
| 826 | |
| 827 | if (!kstrtou32(buf, 0, &value)) { |
| 828 | host->bus_vote.is_max_bw_needed = !!value; |
| 829 | ufs_qcom_update_bus_bw_vote(host); |
| 830 | } |
| 831 | |
| 832 | return count; |
| 833 | } |
| 834 | |
| 835 | static int ufs_qcom_bus_register(struct ufs_qcom_host *host) |
| 836 | { |
| 837 | int err; |
| 838 | struct msm_bus_scale_pdata *bus_pdata; |
| 839 | struct device *dev = host->hba->dev; |
| 840 | struct platform_device *pdev = to_platform_device(dev); |
| 841 | struct device_node *np = dev->of_node; |
| 842 | |
| 843 | bus_pdata = msm_bus_cl_get_pdata(pdev); |
| 844 | if (!bus_pdata) { |
| 845 | dev_err(dev, "%s: failed to get bus vectors\n", __func__); |
| 846 | err = -ENODATA; |
| 847 | goto out; |
| 848 | } |
| 849 | |
| 850 | err = of_property_count_strings(np, "qcom,bus-vector-names"); |
| 851 | if (err < 0 || err != bus_pdata->num_usecases) { |
| 852 | dev_err(dev, "%s: qcom,bus-vector-names not specified correctly %d\n", |
| 853 | __func__, err); |
| 854 | goto out; |
| 855 | } |
| 856 | |
| 857 | host->bus_vote.client_handle = msm_bus_scale_register_client(bus_pdata); |
| 858 | if (!host->bus_vote.client_handle) { |
| 859 | dev_err(dev, "%s: msm_bus_scale_register_client failed\n", |
| 860 | __func__); |
| 861 | err = -EFAULT; |
| 862 | goto out; |
| 863 | } |
| 864 | |
| 865 | /* cache the vote index for minimum and maximum bandwidth */ |
| 866 | host->bus_vote.min_bw_vote = ufs_qcom_get_bus_vote(host, "MIN"); |
| 867 | host->bus_vote.max_bw_vote = ufs_qcom_get_bus_vote(host, "MAX"); |
| 868 | |
| 869 | host->bus_vote.max_bus_bw.show = show_ufs_to_mem_max_bus_bw; |
| 870 | host->bus_vote.max_bus_bw.store = store_ufs_to_mem_max_bus_bw; |
| 871 | sysfs_attr_init(&host->bus_vote.max_bus_bw.attr); |
| 872 | host->bus_vote.max_bus_bw.attr.name = "max_bus_bw"; |
| 873 | host->bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR; |
| 874 | err = device_create_file(dev, &host->bus_vote.max_bus_bw); |
| 875 | out: |
| 876 | return err; |
| 877 | } |
| 878 | #else /* CONFIG_MSM_BUS_SCALING */ |
| 879 | static int ufs_qcom_update_bus_bw_vote(struct ufs_qcom_host *host) |
| 880 | { |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static int ufs_qcom_set_bus_vote(struct ufs_qcom_host *host, int vote) |
| 885 | { |
| 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | static int ufs_qcom_bus_register(struct ufs_qcom_host *host) |
| 890 | { |
| 891 | return 0; |
| 892 | } |
| 893 | #endif /* CONFIG_MSM_BUS_SCALING */ |
| 894 | |
| 895 | static void ufs_qcom_dev_ref_clk_ctrl(struct ufs_qcom_host *host, bool enable) |
| 896 | { |
| 897 | if (host->dev_ref_clk_ctrl_mmio && |
| 898 | (enable ^ host->is_dev_ref_clk_enabled)) { |
| 899 | u32 temp = readl_relaxed(host->dev_ref_clk_ctrl_mmio); |
| 900 | |
| 901 | if (enable) |
| 902 | temp |= host->dev_ref_clk_en_mask; |
| 903 | else |
| 904 | temp &= ~host->dev_ref_clk_en_mask; |
| 905 | |
| 906 | /* |
| 907 | * If we are here to disable this clock it might be immediately |
| 908 | * after entering into hibern8 in which case we need to make |
| 909 | * sure that device ref_clk is active at least 1us after the |
| 910 | * hibern8 enter. |
| 911 | */ |
| 912 | if (!enable) |
| 913 | udelay(1); |
| 914 | |
| 915 | writel_relaxed(temp, host->dev_ref_clk_ctrl_mmio); |
| 916 | |
| 917 | /* ensure that ref_clk is enabled/disabled before we return */ |
| 918 | wmb(); |
| 919 | |
| 920 | /* |
| 921 | * If we call hibern8 exit after this, we need to make sure that |
| 922 | * device ref_clk is stable for at least 1us before the hibern8 |
| 923 | * exit command. |
| 924 | */ |
| 925 | if (enable) |
| 926 | udelay(1); |
| 927 | |
| 928 | host->is_dev_ref_clk_enabled = enable; |
| 929 | } |
| 930 | } |
| 931 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 932 | static int ufs_qcom_pwr_change_notify(struct ufs_hba *hba, |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 933 | enum ufs_notify_change_status status, |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 934 | struct ufs_pa_layer_attr *dev_max_params, |
| 935 | struct ufs_pa_layer_attr *dev_req_params) |
| 936 | { |
| 937 | u32 val; |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 938 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 939 | struct phy *phy = host->generic_phy; |
| 940 | struct ufs_qcom_dev_params ufs_qcom_cap; |
| 941 | int ret = 0; |
| 942 | int res = 0; |
| 943 | |
| 944 | if (!dev_req_params) { |
| 945 | pr_err("%s: incoming dev_req_params is NULL\n", __func__); |
| 946 | ret = -EINVAL; |
| 947 | goto out; |
| 948 | } |
| 949 | |
| 950 | switch (status) { |
| 951 | case PRE_CHANGE: |
| 952 | ufs_qcom_cap.tx_lanes = UFS_QCOM_LIMIT_NUM_LANES_TX; |
| 953 | ufs_qcom_cap.rx_lanes = UFS_QCOM_LIMIT_NUM_LANES_RX; |
| 954 | ufs_qcom_cap.hs_rx_gear = UFS_QCOM_LIMIT_HSGEAR_RX; |
| 955 | ufs_qcom_cap.hs_tx_gear = UFS_QCOM_LIMIT_HSGEAR_TX; |
| 956 | ufs_qcom_cap.pwm_rx_gear = UFS_QCOM_LIMIT_PWMGEAR_RX; |
| 957 | ufs_qcom_cap.pwm_tx_gear = UFS_QCOM_LIMIT_PWMGEAR_TX; |
| 958 | ufs_qcom_cap.rx_pwr_pwm = UFS_QCOM_LIMIT_RX_PWR_PWM; |
| 959 | ufs_qcom_cap.tx_pwr_pwm = UFS_QCOM_LIMIT_TX_PWR_PWM; |
| 960 | ufs_qcom_cap.rx_pwr_hs = UFS_QCOM_LIMIT_RX_PWR_HS; |
| 961 | ufs_qcom_cap.tx_pwr_hs = UFS_QCOM_LIMIT_TX_PWR_HS; |
| 962 | ufs_qcom_cap.hs_rate = UFS_QCOM_LIMIT_HS_RATE; |
| 963 | ufs_qcom_cap.desired_working_mode = |
| 964 | UFS_QCOM_LIMIT_DESIRED_MODE; |
| 965 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 966 | if (host->hw_ver.major == 0x1) { |
| 967 | /* |
| 968 | * HS-G3 operations may not reliably work on legacy QCOM |
| 969 | * UFS host controller hardware even though capability |
| 970 | * exchange during link startup phase may end up |
| 971 | * negotiating maximum supported gear as G3. |
| 972 | * Hence downgrade the maximum supported gear to HS-G2. |
| 973 | */ |
| 974 | if (ufs_qcom_cap.hs_tx_gear > UFS_HS_G2) |
| 975 | ufs_qcom_cap.hs_tx_gear = UFS_HS_G2; |
| 976 | if (ufs_qcom_cap.hs_rx_gear > UFS_HS_G2) |
| 977 | ufs_qcom_cap.hs_rx_gear = UFS_HS_G2; |
| 978 | } |
| 979 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 980 | ret = ufs_qcom_get_pwr_dev_param(&ufs_qcom_cap, |
| 981 | dev_max_params, |
| 982 | dev_req_params); |
| 983 | if (ret) { |
| 984 | pr_err("%s: failed to determine capabilities\n", |
| 985 | __func__); |
| 986 | goto out; |
| 987 | } |
| 988 | |
Yaniv Gardi | f37aabc | 2016-03-10 17:37:20 +0200 | [diff] [blame] | 989 | /* enable the device ref clock before changing to HS mode */ |
| 990 | if (!ufshcd_is_hs_mode(&hba->pwr_info) && |
| 991 | ufshcd_is_hs_mode(dev_req_params)) |
| 992 | ufs_qcom_dev_ref_clk_ctrl(host, true); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 993 | break; |
| 994 | case POST_CHANGE: |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 995 | if (ufs_qcom_cfg_timers(hba, dev_req_params->gear_rx, |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 996 | dev_req_params->pwr_rx, |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 997 | dev_req_params->hs_rate, false)) { |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 998 | dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n", |
| 999 | __func__); |
| 1000 | /* |
| 1001 | * we return error code at the end of the routine, |
| 1002 | * but continue to configure UFS_PHY_TX_LANE_ENABLE |
| 1003 | * and bus voting as usual |
| 1004 | */ |
| 1005 | ret = -EINVAL; |
| 1006 | } |
| 1007 | |
| 1008 | val = ~(MAX_U32 << dev_req_params->lane_tx); |
| 1009 | res = ufs_qcom_phy_set_tx_lane_enable(phy, val); |
| 1010 | if (res) { |
| 1011 | dev_err(hba->dev, "%s: ufs_qcom_phy_set_tx_lane_enable() failed res = %d\n", |
| 1012 | __func__, res); |
| 1013 | ret = res; |
| 1014 | } |
| 1015 | |
| 1016 | /* cache the power mode parameters to use internally */ |
| 1017 | memcpy(&host->dev_req_params, |
| 1018 | dev_req_params, sizeof(*dev_req_params)); |
| 1019 | ufs_qcom_update_bus_bw_vote(host); |
Yaniv Gardi | f37aabc | 2016-03-10 17:37:20 +0200 | [diff] [blame] | 1020 | |
| 1021 | /* disable the device ref clock if entered PWM mode */ |
| 1022 | if (ufshcd_is_hs_mode(&hba->pwr_info) && |
| 1023 | !ufshcd_is_hs_mode(dev_req_params)) |
| 1024 | ufs_qcom_dev_ref_clk_ctrl(host, false); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1025 | break; |
| 1026 | default: |
| 1027 | ret = -EINVAL; |
| 1028 | break; |
| 1029 | } |
| 1030 | out: |
| 1031 | return ret; |
| 1032 | } |
| 1033 | |
Yaniv Gardi | ae97758 | 2015-05-17 18:55:06 +0300 | [diff] [blame] | 1034 | static u32 ufs_qcom_get_ufs_hci_version(struct ufs_hba *hba) |
| 1035 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 1036 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | ae97758 | 2015-05-17 18:55:06 +0300 | [diff] [blame] | 1037 | |
| 1038 | if (host->hw_ver.major == 0x1) |
| 1039 | return UFSHCI_VERSION_11; |
| 1040 | else |
| 1041 | return UFSHCI_VERSION_20; |
| 1042 | } |
| 1043 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1044 | /** |
| 1045 | * ufs_qcom_advertise_quirks - advertise the known QCOM UFS controller quirks |
| 1046 | * @hba: host controller instance |
| 1047 | * |
| 1048 | * QCOM UFS host controller might have some non standard behaviours (quirks) |
| 1049 | * than what is specified by UFSHCI specification. Advertise all such |
| 1050 | * quirks to standard UFS host controller driver so standard takes them into |
| 1051 | * account. |
| 1052 | */ |
| 1053 | static void ufs_qcom_advertise_quirks(struct ufs_hba *hba) |
| 1054 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 1055 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1056 | |
Yaniv Gardi | 81c7e06 | 2015-05-17 18:54:58 +0300 | [diff] [blame] | 1057 | if (host->hw_ver.major == 0x01) { |
Yaniv Gardi | 8163743 | 2015-05-17 18:55:02 +0300 | [diff] [blame] | 1058 | hba->quirks |= UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS |
Yaniv Gardi | 2c0cc2e | 2015-05-17 18:55:04 +0300 | [diff] [blame] | 1059 | | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP |
| 1060 | | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1061 | |
Yaniv Gardi | 81c7e06 | 2015-05-17 18:54:58 +0300 | [diff] [blame] | 1062 | if (host->hw_ver.minor == 0x0001 && host->hw_ver.step == 0x0001) |
| 1063 | hba->quirks |= UFSHCD_QUIRK_BROKEN_INTR_AGGR; |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1064 | |
| 1065 | hba->quirks |= UFSHCD_QUIRK_BROKEN_LCC; |
Yaniv Gardi | 81c7e06 | 2015-05-17 18:54:58 +0300 | [diff] [blame] | 1066 | } |
| 1067 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 1068 | if (host->hw_ver.major >= 0x2) { |
Yaniv Gardi | ae97758 | 2015-05-17 18:55:06 +0300 | [diff] [blame] | 1069 | hba->quirks |= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION; |
Yaniv Gardi | 2f01837 | 2015-05-17 18:55:00 +0300 | [diff] [blame] | 1070 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 1071 | if (!ufs_qcom_cap_qunipro(host)) |
| 1072 | /* Legacy UniPro mode still need following quirks */ |
Yaniv Gardi | 8163743 | 2015-05-17 18:55:02 +0300 | [diff] [blame] | 1073 | hba->quirks |= (UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS |
Yaniv Gardi | 2c0cc2e | 2015-05-17 18:55:04 +0300 | [diff] [blame] | 1074 | | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE |
Yaniv Gardi | 8163743 | 2015-05-17 18:55:02 +0300 | [diff] [blame] | 1075 | | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | static void ufs_qcom_set_caps(struct ufs_hba *hba) |
| 1080 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 1081 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 1082 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1083 | hba->caps |= UFSHCD_CAP_CLK_GATING | UFSHCD_CAP_HIBERN8_WITH_CLK_GATING; |
| 1084 | hba->caps |= UFSHCD_CAP_CLK_SCALING; |
| 1085 | hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1086 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1087 | if (host->hw_ver.major >= 0x2) { |
| 1088 | host->caps = UFS_QCOM_CAP_QUNIPRO | |
| 1089 | UFS_QCOM_CAP_RETAIN_SEC_CFG_AFTER_PWR_COLLAPSE; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1093 | /** |
| 1094 | * ufs_qcom_setup_clocks - enables/disable clocks |
| 1095 | * @hba: host controller instance |
| 1096 | * @on: If true, enable clocks else disable them. |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 1097 | * @status: PRE_CHANGE or POST_CHANGE notify |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1098 | * |
| 1099 | * Returns 0 on success, non-zero on failure. |
| 1100 | */ |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 1101 | static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on, |
| 1102 | enum ufs_notify_change_status status) |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1103 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 1104 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1105 | int err; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1106 | int vote = 0; |
| 1107 | |
| 1108 | /* |
| 1109 | * In case ufs_qcom_init() is not yet done, simply ignore. |
| 1110 | * This ufs_qcom_setup_clocks() shall be called from |
| 1111 | * ufs_qcom_init() after init is done. |
| 1112 | */ |
| 1113 | if (!host) |
| 1114 | return 0; |
| 1115 | |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 1116 | if (on && (status == POST_CHANGE)) { |
Vivek Gautam | feb3d79 | 2016-11-08 15:37:48 +0530 | [diff] [blame] | 1117 | phy_power_on(host->generic_phy); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1118 | |
Yaniv Gardi | f37aabc | 2016-03-10 17:37:20 +0200 | [diff] [blame] | 1119 | /* enable the device ref clock for HS mode*/ |
| 1120 | if (ufshcd_is_hs_mode(&hba->pwr_info)) |
| 1121 | ufs_qcom_dev_ref_clk_ctrl(host, true); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1122 | vote = host->bus_vote.saved_vote; |
| 1123 | if (vote == host->bus_vote.min_bw_vote) |
| 1124 | ufs_qcom_update_bus_bw_vote(host); |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1125 | |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 1126 | } else if (!on && (status == PRE_CHANGE)) { |
Vivek Gautam | feb3d79 | 2016-11-08 15:37:48 +0530 | [diff] [blame] | 1127 | if (!ufs_qcom_is_link_active(hba)) { |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1128 | /* disable device ref_clk */ |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1129 | ufs_qcom_dev_ref_clk_ctrl(host, false); |
| 1130 | |
Vivek Gautam | feb3d79 | 2016-11-08 15:37:48 +0530 | [diff] [blame] | 1131 | /* powering off PHY during aggressive clk gating */ |
| 1132 | phy_power_off(host->generic_phy); |
| 1133 | } |
| 1134 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1135 | vote = host->bus_vote.min_bw_vote; |
| 1136 | } |
| 1137 | |
| 1138 | err = ufs_qcom_set_bus_vote(host, vote); |
| 1139 | if (err) |
| 1140 | dev_err(hba->dev, "%s: set bus vote failed %d\n", |
| 1141 | __func__, err); |
| 1142 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1143 | return err; |
| 1144 | } |
| 1145 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1146 | #define ANDROID_BOOT_DEV_MAX 30 |
| 1147 | static char android_boot_dev[ANDROID_BOOT_DEV_MAX]; |
Yaniv Gardi | fb819ee | 2015-10-28 13:15:45 +0200 | [diff] [blame] | 1148 | |
| 1149 | #ifndef MODULE |
| 1150 | static int __init get_android_boot_dev(char *str) |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1151 | { |
| 1152 | strlcpy(android_boot_dev, str, ANDROID_BOOT_DEV_MAX); |
| 1153 | return 1; |
| 1154 | } |
| 1155 | __setup("androidboot.bootdevice=", get_android_boot_dev); |
Yaniv Gardi | fb819ee | 2015-10-28 13:15:45 +0200 | [diff] [blame] | 1156 | #endif |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1157 | |
| 1158 | /** |
| 1159 | * ufs_qcom_init - bind phy with controller |
| 1160 | * @hba: host controller instance |
| 1161 | * |
| 1162 | * Binds PHY with controller and powers up PHY enabling clocks |
| 1163 | * and regulators. |
| 1164 | * |
| 1165 | * Returns -EPROBE_DEFER if binding fails, returns negative error |
| 1166 | * on phy power up failure and returns zero on success. |
| 1167 | */ |
| 1168 | static int ufs_qcom_init(struct ufs_hba *hba) |
| 1169 | { |
| 1170 | int err; |
| 1171 | struct device *dev = hba->dev; |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1172 | struct platform_device *pdev = to_platform_device(dev); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1173 | struct ufs_qcom_host *host; |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1174 | struct resource *res; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1175 | |
| 1176 | if (strlen(android_boot_dev) && strcmp(android_boot_dev, dev_name(dev))) |
| 1177 | return -ENODEV; |
| 1178 | |
| 1179 | host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL); |
| 1180 | if (!host) { |
| 1181 | err = -ENOMEM; |
| 1182 | dev_err(dev, "%s: no memory for qcom ufs host\n", __func__); |
| 1183 | goto out; |
| 1184 | } |
| 1185 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1186 | /* Make a two way bind between the qcom host and the hba */ |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1187 | host->hba = hba; |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 1188 | ufshcd_set_variant(hba, host); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1189 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1190 | /* |
| 1191 | * voting/devoting device ref_clk source is time consuming hence |
| 1192 | * skip devoting it during aggressive clock gating. This clock |
| 1193 | * will still be gated off during runtime suspend. |
| 1194 | */ |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1195 | host->generic_phy = devm_phy_get(dev, "ufsphy"); |
| 1196 | |
| 1197 | if (IS_ERR(host->generic_phy)) { |
| 1198 | err = PTR_ERR(host->generic_phy); |
| 1199 | dev_err(dev, "%s: PHY get failed %d\n", __func__, err); |
Bjorn Andersson | a6854df | 2016-11-19 22:34:51 -0800 | [diff] [blame] | 1200 | goto out_variant_clear; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | err = ufs_qcom_bus_register(host); |
| 1204 | if (err) |
Bjorn Andersson | a6854df | 2016-11-19 22:34:51 -0800 | [diff] [blame] | 1205 | goto out_variant_clear; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1206 | |
Yaniv Gardi | bfdbe8b | 2015-03-31 17:37:13 +0300 | [diff] [blame] | 1207 | ufs_qcom_get_controller_revision(hba, &host->hw_ver.major, |
| 1208 | &host->hw_ver.minor, &host->hw_ver.step); |
| 1209 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1210 | /* |
| 1211 | * for newer controllers, device reference clock control bit has |
| 1212 | * moved inside UFS controller register address space itself. |
| 1213 | */ |
| 1214 | if (host->hw_ver.major >= 0x02) { |
| 1215 | host->dev_ref_clk_ctrl_mmio = hba->mmio_base + REG_UFS_CFG1; |
| 1216 | host->dev_ref_clk_en_mask = BIT(26); |
| 1217 | } else { |
| 1218 | /* "dev_ref_clk_ctrl_mem" is optional resource */ |
| 1219 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1220 | if (res) { |
| 1221 | host->dev_ref_clk_ctrl_mmio = |
| 1222 | devm_ioremap_resource(dev, res); |
| 1223 | if (IS_ERR(host->dev_ref_clk_ctrl_mmio)) { |
| 1224 | dev_warn(dev, |
| 1225 | "%s: could not map dev_ref_clk_ctrl_mmio, err %ld\n", |
| 1226 | __func__, |
| 1227 | PTR_ERR(host->dev_ref_clk_ctrl_mmio)); |
| 1228 | host->dev_ref_clk_ctrl_mmio = NULL; |
| 1229 | } |
| 1230 | host->dev_ref_clk_en_mask = BIT(5); |
| 1231 | } |
| 1232 | } |
| 1233 | |
Yaniv Gardi | bfdbe8b | 2015-03-31 17:37:13 +0300 | [diff] [blame] | 1234 | /* update phy revision information before calling phy_init() */ |
| 1235 | ufs_qcom_phy_save_controller_version(host->generic_phy, |
| 1236 | host->hw_ver.major, host->hw_ver.minor, host->hw_ver.step); |
| 1237 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1238 | phy_init(host->generic_phy); |
| 1239 | err = phy_power_on(host->generic_phy); |
| 1240 | if (err) |
| 1241 | goto out_unregister_bus; |
| 1242 | |
| 1243 | err = ufs_qcom_init_lane_clks(host); |
| 1244 | if (err) |
| 1245 | goto out_disable_phy; |
| 1246 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 1247 | ufs_qcom_set_caps(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1248 | ufs_qcom_advertise_quirks(hba); |
| 1249 | |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 1250 | ufs_qcom_setup_clocks(hba, true, POST_CHANGE); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1251 | |
| 1252 | if (hba->dev->id < MAX_UFS_QCOM_HOSTS) |
| 1253 | ufs_qcom_hosts[hba->dev->id] = host; |
| 1254 | |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1255 | host->dbg_print_en |= UFS_QCOM_DEFAULT_DBG_PRINT_EN; |
| 1256 | ufs_qcom_get_default_testbus_cfg(host); |
| 1257 | err = ufs_qcom_testbus_config(host); |
| 1258 | if (err) { |
| 1259 | dev_warn(dev, "%s: failed to configure the testbus %d\n", |
| 1260 | __func__, err); |
| 1261 | err = 0; |
| 1262 | } |
| 1263 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1264 | goto out; |
| 1265 | |
| 1266 | out_disable_phy: |
| 1267 | phy_power_off(host->generic_phy); |
| 1268 | out_unregister_bus: |
| 1269 | phy_exit(host->generic_phy); |
Bjorn Andersson | a6854df | 2016-11-19 22:34:51 -0800 | [diff] [blame] | 1270 | out_variant_clear: |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 1271 | ufshcd_set_variant(hba, NULL); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1272 | out: |
| 1273 | return err; |
| 1274 | } |
| 1275 | |
| 1276 | static void ufs_qcom_exit(struct ufs_hba *hba) |
| 1277 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 1278 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1279 | |
| 1280 | ufs_qcom_disable_lane_clks(host); |
| 1281 | phy_power_off(host->generic_phy); |
Vivek Gautam | d7fe6b6 | 2016-11-08 15:37:50 +0530 | [diff] [blame] | 1282 | phy_exit(host->generic_phy); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1283 | } |
| 1284 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1285 | static int ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(struct ufs_hba *hba, |
| 1286 | u32 clk_cycles) |
| 1287 | { |
| 1288 | int err; |
| 1289 | u32 core_clk_ctrl_reg; |
| 1290 | |
| 1291 | if (clk_cycles > DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK) |
| 1292 | return -EINVAL; |
| 1293 | |
| 1294 | err = ufshcd_dme_get(hba, |
| 1295 | UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL), |
| 1296 | &core_clk_ctrl_reg); |
| 1297 | if (err) |
| 1298 | goto out; |
| 1299 | |
| 1300 | core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_MAX_CORE_CLK_1US_CYCLES_MASK; |
| 1301 | core_clk_ctrl_reg |= clk_cycles; |
| 1302 | |
| 1303 | /* Clear CORE_CLK_DIV_EN */ |
| 1304 | core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT; |
| 1305 | |
| 1306 | err = ufshcd_dme_set(hba, |
| 1307 | UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL), |
| 1308 | core_clk_ctrl_reg); |
| 1309 | out: |
| 1310 | return err; |
| 1311 | } |
| 1312 | |
| 1313 | static int ufs_qcom_clk_scale_up_pre_change(struct ufs_hba *hba) |
| 1314 | { |
| 1315 | /* nothing to do as of now */ |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | static int ufs_qcom_clk_scale_up_post_change(struct ufs_hba *hba) |
| 1320 | { |
| 1321 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
| 1322 | |
| 1323 | if (!ufs_qcom_cap_qunipro(host)) |
| 1324 | return 0; |
| 1325 | |
| 1326 | /* set unipro core clock cycles to 150 and clear clock divider */ |
| 1327 | return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, 150); |
| 1328 | } |
| 1329 | |
| 1330 | static int ufs_qcom_clk_scale_down_pre_change(struct ufs_hba *hba) |
| 1331 | { |
| 1332 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
| 1333 | int err; |
| 1334 | u32 core_clk_ctrl_reg; |
| 1335 | |
| 1336 | if (!ufs_qcom_cap_qunipro(host)) |
| 1337 | return 0; |
| 1338 | |
| 1339 | err = ufshcd_dme_get(hba, |
| 1340 | UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL), |
| 1341 | &core_clk_ctrl_reg); |
| 1342 | |
| 1343 | /* make sure CORE_CLK_DIV_EN is cleared */ |
| 1344 | if (!err && |
| 1345 | (core_clk_ctrl_reg & DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT)) { |
| 1346 | core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT; |
| 1347 | err = ufshcd_dme_set(hba, |
| 1348 | UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL), |
| 1349 | core_clk_ctrl_reg); |
| 1350 | } |
| 1351 | |
| 1352 | return err; |
| 1353 | } |
| 1354 | |
| 1355 | static int ufs_qcom_clk_scale_down_post_change(struct ufs_hba *hba) |
| 1356 | { |
| 1357 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
| 1358 | |
| 1359 | if (!ufs_qcom_cap_qunipro(host)) |
| 1360 | return 0; |
| 1361 | |
| 1362 | /* set unipro core clock cycles to 75 and clear clock divider */ |
| 1363 | return ufs_qcom_set_dme_vs_core_clk_ctrl_clear_div(hba, 75); |
| 1364 | } |
| 1365 | |
| 1366 | static int ufs_qcom_clk_scale_notify(struct ufs_hba *hba, |
| 1367 | bool scale_up, enum ufs_notify_change_status status) |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1368 | { |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 1369 | struct ufs_qcom_host *host = ufshcd_get_variant(hba); |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1370 | struct ufs_pa_layer_attr *dev_req_params = &host->dev_req_params; |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1371 | int err = 0; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1372 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1373 | if (status == PRE_CHANGE) { |
| 1374 | if (scale_up) |
| 1375 | err = ufs_qcom_clk_scale_up_pre_change(hba); |
| 1376 | else |
| 1377 | err = ufs_qcom_clk_scale_down_pre_change(hba); |
| 1378 | } else { |
| 1379 | if (scale_up) |
| 1380 | err = ufs_qcom_clk_scale_up_post_change(hba); |
| 1381 | else |
| 1382 | err = ufs_qcom_clk_scale_down_post_change(hba); |
| 1383 | |
| 1384 | if (err || !dev_req_params) |
| 1385 | goto out; |
| 1386 | |
| 1387 | ufs_qcom_cfg_timers(hba, |
| 1388 | dev_req_params->gear_rx, |
| 1389 | dev_req_params->pwr_rx, |
| 1390 | dev_req_params->hs_rate, |
| 1391 | false); |
| 1392 | ufs_qcom_update_bus_bw_vote(host); |
| 1393 | } |
| 1394 | |
| 1395 | out: |
| 1396 | return err; |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1397 | } |
| 1398 | |
Yaniv Gardi | eba5ed3 | 2016-03-10 17:37:21 +0200 | [diff] [blame] | 1399 | static void ufs_qcom_print_hw_debug_reg_all(struct ufs_hba *hba, |
| 1400 | void *priv, void (*print_fn)(struct ufs_hba *hba, |
| 1401 | int offset, int num_regs, char *str, void *priv)) |
| 1402 | { |
| 1403 | u32 reg; |
| 1404 | struct ufs_qcom_host *host; |
| 1405 | |
| 1406 | if (unlikely(!hba)) { |
| 1407 | pr_err("%s: hba is NULL\n", __func__); |
| 1408 | return; |
| 1409 | } |
| 1410 | if (unlikely(!print_fn)) { |
| 1411 | dev_err(hba->dev, "%s: print_fn is NULL\n", __func__); |
| 1412 | return; |
| 1413 | } |
| 1414 | |
| 1415 | host = ufshcd_get_variant(hba); |
| 1416 | if (!(host->dbg_print_en & UFS_QCOM_DBG_PRINT_REGS_EN)) |
| 1417 | return; |
| 1418 | |
| 1419 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_REG_OCSC); |
| 1420 | print_fn(hba, reg, 44, "UFS_UFS_DBG_RD_REG_OCSC ", priv); |
| 1421 | |
| 1422 | reg = ufshcd_readl(hba, REG_UFS_CFG1); |
| 1423 | reg |= UFS_BIT(17); |
| 1424 | ufshcd_writel(hba, reg, REG_UFS_CFG1); |
| 1425 | |
| 1426 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_EDTL_RAM); |
| 1427 | print_fn(hba, reg, 32, "UFS_UFS_DBG_RD_EDTL_RAM ", priv); |
| 1428 | |
| 1429 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_DESC_RAM); |
| 1430 | print_fn(hba, reg, 128, "UFS_UFS_DBG_RD_DESC_RAM ", priv); |
| 1431 | |
| 1432 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_PRDT_RAM); |
| 1433 | print_fn(hba, reg, 64, "UFS_UFS_DBG_RD_PRDT_RAM ", priv); |
| 1434 | |
| 1435 | ufshcd_writel(hba, (reg & ~UFS_BIT(17)), REG_UFS_CFG1); |
| 1436 | |
| 1437 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UAWM); |
| 1438 | print_fn(hba, reg, 4, "UFS_DBG_RD_REG_UAWM ", priv); |
| 1439 | |
| 1440 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UARM); |
| 1441 | print_fn(hba, reg, 4, "UFS_DBG_RD_REG_UARM ", priv); |
| 1442 | |
| 1443 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TXUC); |
| 1444 | print_fn(hba, reg, 48, "UFS_DBG_RD_REG_TXUC ", priv); |
| 1445 | |
| 1446 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_RXUC); |
| 1447 | print_fn(hba, reg, 27, "UFS_DBG_RD_REG_RXUC ", priv); |
| 1448 | |
| 1449 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_DFC); |
| 1450 | print_fn(hba, reg, 19, "UFS_DBG_RD_REG_DFC ", priv); |
| 1451 | |
| 1452 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TRLUT); |
| 1453 | print_fn(hba, reg, 34, "UFS_DBG_RD_REG_TRLUT ", priv); |
| 1454 | |
| 1455 | reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TMRLUT); |
| 1456 | print_fn(hba, reg, 9, "UFS_DBG_RD_REG_TMRLUT ", priv); |
| 1457 | } |
| 1458 | |
| 1459 | static void ufs_qcom_enable_test_bus(struct ufs_qcom_host *host) |
| 1460 | { |
| 1461 | if (host->dbg_print_en & UFS_QCOM_DBG_PRINT_TEST_BUS_EN) |
| 1462 | ufshcd_rmwl(host->hba, TEST_BUS_EN, TEST_BUS_EN, REG_UFS_CFG1); |
| 1463 | else |
| 1464 | ufshcd_rmwl(host->hba, TEST_BUS_EN, 0, REG_UFS_CFG1); |
| 1465 | } |
| 1466 | |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1467 | static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host) |
| 1468 | { |
| 1469 | /* provide a legal default configuration */ |
| 1470 | host->testbus.select_major = TSTBUS_UAWM; |
| 1471 | host->testbus.select_minor = 1; |
| 1472 | } |
| 1473 | |
| 1474 | static bool ufs_qcom_testbus_cfg_is_ok(struct ufs_qcom_host *host) |
| 1475 | { |
| 1476 | if (host->testbus.select_major >= TSTBUS_MAX) { |
| 1477 | dev_err(host->hba->dev, |
| 1478 | "%s: UFS_CFG1[TEST_BUS_SEL} may not equal 0x%05X\n", |
| 1479 | __func__, host->testbus.select_major); |
| 1480 | return false; |
| 1481 | } |
| 1482 | |
| 1483 | /* |
| 1484 | * Not performing check for each individual select_major |
| 1485 | * mappings of select_minor, since there is no harm in |
| 1486 | * configuring a non-existent select_minor |
| 1487 | */ |
| 1488 | if (host->testbus.select_minor > 0x1F) { |
| 1489 | dev_err(host->hba->dev, |
| 1490 | "%s: 0x%05X is not a legal testbus option\n", |
| 1491 | __func__, host->testbus.select_minor); |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
| 1495 | return true; |
| 1496 | } |
| 1497 | |
| 1498 | int ufs_qcom_testbus_config(struct ufs_qcom_host *host) |
| 1499 | { |
| 1500 | int reg; |
| 1501 | int offset; |
| 1502 | u32 mask = TEST_BUS_SUB_SEL_MASK; |
| 1503 | |
| 1504 | if (!host) |
| 1505 | return -EINVAL; |
| 1506 | |
| 1507 | if (!ufs_qcom_testbus_cfg_is_ok(host)) |
| 1508 | return -EPERM; |
| 1509 | |
| 1510 | switch (host->testbus.select_major) { |
| 1511 | case TSTBUS_UAWM: |
| 1512 | reg = UFS_TEST_BUS_CTRL_0; |
| 1513 | offset = 24; |
| 1514 | break; |
| 1515 | case TSTBUS_UARM: |
| 1516 | reg = UFS_TEST_BUS_CTRL_0; |
| 1517 | offset = 16; |
| 1518 | break; |
| 1519 | case TSTBUS_TXUC: |
| 1520 | reg = UFS_TEST_BUS_CTRL_0; |
| 1521 | offset = 8; |
| 1522 | break; |
| 1523 | case TSTBUS_RXUC: |
| 1524 | reg = UFS_TEST_BUS_CTRL_0; |
| 1525 | offset = 0; |
| 1526 | break; |
| 1527 | case TSTBUS_DFC: |
| 1528 | reg = UFS_TEST_BUS_CTRL_1; |
| 1529 | offset = 24; |
| 1530 | break; |
| 1531 | case TSTBUS_TRLUT: |
| 1532 | reg = UFS_TEST_BUS_CTRL_1; |
| 1533 | offset = 16; |
| 1534 | break; |
| 1535 | case TSTBUS_TMRLUT: |
| 1536 | reg = UFS_TEST_BUS_CTRL_1; |
| 1537 | offset = 8; |
| 1538 | break; |
| 1539 | case TSTBUS_OCSC: |
| 1540 | reg = UFS_TEST_BUS_CTRL_1; |
| 1541 | offset = 0; |
| 1542 | break; |
| 1543 | case TSTBUS_WRAPPER: |
| 1544 | reg = UFS_TEST_BUS_CTRL_2; |
| 1545 | offset = 16; |
| 1546 | break; |
| 1547 | case TSTBUS_COMBINED: |
| 1548 | reg = UFS_TEST_BUS_CTRL_2; |
| 1549 | offset = 8; |
| 1550 | break; |
| 1551 | case TSTBUS_UTP_HCI: |
| 1552 | reg = UFS_TEST_BUS_CTRL_2; |
| 1553 | offset = 0; |
| 1554 | break; |
| 1555 | case TSTBUS_UNIPRO: |
| 1556 | reg = UFS_UNIPRO_CFG; |
| 1557 | offset = 1; |
| 1558 | break; |
| 1559 | /* |
| 1560 | * No need for a default case, since |
| 1561 | * ufs_qcom_testbus_cfg_is_ok() checks that the configuration |
| 1562 | * is legal |
| 1563 | */ |
| 1564 | } |
| 1565 | mask <<= offset; |
| 1566 | |
| 1567 | pm_runtime_get_sync(host->hba->dev); |
| 1568 | ufshcd_hold(host->hba, false); |
| 1569 | ufshcd_rmwl(host->hba, TEST_BUS_SEL, |
| 1570 | (u32)host->testbus.select_major << 19, |
| 1571 | REG_UFS_CFG1); |
| 1572 | ufshcd_rmwl(host->hba, mask, |
| 1573 | (u32)host->testbus.select_minor << offset, |
| 1574 | reg); |
Yaniv Gardi | eba5ed3 | 2016-03-10 17:37:21 +0200 | [diff] [blame] | 1575 | ufs_qcom_enable_test_bus(host); |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1576 | ufshcd_release(host->hba); |
| 1577 | pm_runtime_put_sync(host->hba->dev); |
| 1578 | |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | static void ufs_qcom_testbus_read(struct ufs_hba *hba) |
| 1583 | { |
| 1584 | ufs_qcom_dump_regs(hba, UFS_TEST_BUS, 1, "UFS_TEST_BUS "); |
| 1585 | } |
| 1586 | |
| 1587 | static void ufs_qcom_dump_dbg_regs(struct ufs_hba *hba) |
| 1588 | { |
| 1589 | ufs_qcom_dump_regs(hba, REG_UFS_SYS1CLK_1US, 16, |
| 1590 | "HCI Vendor Specific Registers "); |
| 1591 | |
Yaniv Gardi | eba5ed3 | 2016-03-10 17:37:21 +0200 | [diff] [blame] | 1592 | ufs_qcom_print_hw_debug_reg_all(hba, NULL, ufs_qcom_dump_regs_wrapper); |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1593 | ufs_qcom_testbus_read(hba); |
| 1594 | } |
Yaniv Gardi | eba5ed3 | 2016-03-10 17:37:21 +0200 | [diff] [blame] | 1595 | |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1596 | /** |
| 1597 | * struct ufs_hba_qcom_vops - UFS QCOM specific variant operations |
| 1598 | * |
| 1599 | * The variant operations configure the necessary controller and PHY |
| 1600 | * handshake during initialization. |
| 1601 | */ |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 1602 | static struct ufs_hba_variant_ops ufs_hba_qcom_vops = { |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1603 | .name = "qcom", |
| 1604 | .init = ufs_qcom_init, |
| 1605 | .exit = ufs_qcom_exit, |
Yaniv Gardi | ae97758 | 2015-05-17 18:55:06 +0300 | [diff] [blame] | 1606 | .get_ufs_hci_version = ufs_qcom_get_ufs_hci_version, |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1607 | .clk_scale_notify = ufs_qcom_clk_scale_notify, |
| 1608 | .setup_clocks = ufs_qcom_setup_clocks, |
| 1609 | .hce_enable_notify = ufs_qcom_hce_enable_notify, |
| 1610 | .link_startup_notify = ufs_qcom_link_startup_notify, |
| 1611 | .pwr_change_notify = ufs_qcom_pwr_change_notify, |
| 1612 | .suspend = ufs_qcom_suspend, |
| 1613 | .resume = ufs_qcom_resume, |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1614 | .dbg_register_dump = ufs_qcom_dump_dbg_regs, |
Yaniv Gardi | 81c0fc5 | 2015-01-15 16:32:37 +0200 | [diff] [blame] | 1615 | }; |
Yaniv Gardi | fb819ee | 2015-10-28 13:15:45 +0200 | [diff] [blame] | 1616 | |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 1617 | /** |
| 1618 | * ufs_qcom_probe - probe routine of the driver |
| 1619 | * @pdev: pointer to Platform device handle |
| 1620 | * |
| 1621 | * Return zero for success and non-zero for failure |
| 1622 | */ |
| 1623 | static int ufs_qcom_probe(struct platform_device *pdev) |
| 1624 | { |
| 1625 | int err; |
| 1626 | struct device *dev = &pdev->dev; |
| 1627 | |
| 1628 | /* Perform generic probe */ |
| 1629 | err = ufshcd_pltfrm_init(pdev, &ufs_hba_qcom_vops); |
| 1630 | if (err) |
| 1631 | dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err); |
| 1632 | |
| 1633 | return err; |
| 1634 | } |
| 1635 | |
| 1636 | /** |
| 1637 | * ufs_qcom_remove - set driver_data of the device to NULL |
| 1638 | * @pdev: pointer to platform device handle |
| 1639 | * |
Yaniv Gardi | 4b9ad0b | 2016-03-10 17:37:19 +0200 | [diff] [blame] | 1640 | * Always returns 0 |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 1641 | */ |
| 1642 | static int ufs_qcom_remove(struct platform_device *pdev) |
| 1643 | { |
| 1644 | struct ufs_hba *hba = platform_get_drvdata(pdev); |
| 1645 | |
| 1646 | pm_runtime_get_sync(&(pdev)->dev); |
| 1647 | ufshcd_remove(hba); |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
| 1651 | static const struct of_device_id ufs_qcom_of_match[] = { |
| 1652 | { .compatible = "qcom,ufshc"}, |
| 1653 | {}, |
| 1654 | }; |
| 1655 | |
| 1656 | static const struct dev_pm_ops ufs_qcom_pm_ops = { |
| 1657 | .suspend = ufshcd_pltfrm_suspend, |
| 1658 | .resume = ufshcd_pltfrm_resume, |
| 1659 | .runtime_suspend = ufshcd_pltfrm_runtime_suspend, |
| 1660 | .runtime_resume = ufshcd_pltfrm_runtime_resume, |
| 1661 | .runtime_idle = ufshcd_pltfrm_runtime_idle, |
| 1662 | }; |
| 1663 | |
| 1664 | static struct platform_driver ufs_qcom_pltform = { |
| 1665 | .probe = ufs_qcom_probe, |
| 1666 | .remove = ufs_qcom_remove, |
| 1667 | .shutdown = ufshcd_pltfrm_shutdown, |
| 1668 | .driver = { |
| 1669 | .name = "ufshcd-qcom", |
| 1670 | .pm = &ufs_qcom_pm_ops, |
| 1671 | .of_match_table = of_match_ptr(ufs_qcom_of_match), |
| 1672 | }, |
| 1673 | }; |
| 1674 | module_platform_driver(ufs_qcom_pltform); |
| 1675 | |
Yaniv Gardi | fb819ee | 2015-10-28 13:15:45 +0200 | [diff] [blame] | 1676 | MODULE_LICENSE("GPL v2"); |