Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 1 | /* Copyright (c) 2010, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/platform_device.h> |
| 10 | #include <linux/pm_runtime.h> |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 11 | #include <linux/usb/chipidea.h> |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 12 | #include <linux/clk.h> |
| 13 | #include <linux/reset.h> |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 14 | #include <linux/mfd/syscon.h> |
| 15 | #include <linux/regmap.h> |
| 16 | #include <linux/io.h> |
Stephen Boyd | 1b8fc5a | 2016-12-28 14:57:05 -0800 | [diff] [blame] | 17 | #include <linux/reset-controller.h> |
Stephen Boyd | 47654a1 | 2016-12-28 14:57:03 -0800 | [diff] [blame] | 18 | #include <linux/extcon.h> |
| 19 | #include <linux/of.h> |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 20 | |
| 21 | #include "ci.h" |
| 22 | |
Stephen Boyd | ee33f6e | 2016-12-28 14:57:00 -0800 | [diff] [blame] | 23 | #define HS_PHY_AHB_MODE 0x0098 |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 24 | |
Stephen Boyd | 47654a1 | 2016-12-28 14:57:03 -0800 | [diff] [blame] | 25 | #define HS_PHY_GENCONFIG 0x009c |
| 26 | #define HS_PHY_TXFIFO_IDLE_FORCE_DIS BIT(4) |
| 27 | |
| 28 | #define HS_PHY_GENCONFIG_2 0x00a0 |
| 29 | #define HS_PHY_SESS_VLD_CTRL_EN BIT(7) |
| 30 | #define HS_PHY_ULPI_TX_PKT_EN_CLR_FIX BIT(19) |
| 31 | |
| 32 | #define HSPHY_SESS_VLD_CTRL BIT(25) |
| 33 | |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 34 | /* Vendor base starts at 0x200 beyond CI base */ |
Stephen Boyd | 1b8fc5a | 2016-12-28 14:57:05 -0800 | [diff] [blame] | 35 | #define HS_PHY_CTRL 0x0040 |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 36 | #define HS_PHY_SEC_CTRL 0x0078 |
| 37 | #define HS_PHY_DIG_CLAMP_N BIT(16) |
Stephen Boyd | 1b8fc5a | 2016-12-28 14:57:05 -0800 | [diff] [blame] | 38 | #define HS_PHY_POR_ASSERT BIT(0) |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 39 | |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 40 | struct ci_hdrc_msm { |
| 41 | struct platform_device *ci; |
| 42 | struct clk *core_clk; |
| 43 | struct clk *iface_clk; |
| 44 | struct clk *fs_clk; |
Stephen Boyd | 26f8e3a | 2016-12-28 14:57:04 -0800 | [diff] [blame] | 45 | struct ci_hdrc_platform_data pdata; |
Stephen Boyd | 1b8fc5a | 2016-12-28 14:57:05 -0800 | [diff] [blame] | 46 | struct reset_controller_dev rcdev; |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 47 | bool secondary_phy; |
Stephen Boyd | 47654a1 | 2016-12-28 14:57:03 -0800 | [diff] [blame] | 48 | bool hsic; |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 49 | void __iomem *base; |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
Stephen Boyd | 1b8fc5a | 2016-12-28 14:57:05 -0800 | [diff] [blame] | 52 | static int |
| 53 | ci_hdrc_msm_por_reset(struct reset_controller_dev *r, unsigned long id) |
| 54 | { |
| 55 | struct ci_hdrc_msm *ci_msm = container_of(r, struct ci_hdrc_msm, rcdev); |
| 56 | void __iomem *addr = ci_msm->base; |
| 57 | u32 val; |
| 58 | |
| 59 | if (id) |
| 60 | addr += HS_PHY_SEC_CTRL; |
| 61 | else |
| 62 | addr += HS_PHY_CTRL; |
| 63 | |
| 64 | val = readl_relaxed(addr); |
| 65 | val |= HS_PHY_POR_ASSERT; |
| 66 | writel(val, addr); |
| 67 | /* |
| 68 | * wait for minimum 10 microseconds as suggested by manual. |
| 69 | * Use a slightly larger value since the exact value didn't |
| 70 | * work 100% of the time. |
| 71 | */ |
| 72 | udelay(12); |
| 73 | val &= ~HS_PHY_POR_ASSERT; |
| 74 | writel(val, addr); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static const struct reset_control_ops ci_hdrc_msm_reset_ops = { |
| 80 | .reset = ci_hdrc_msm_por_reset, |
| 81 | }; |
| 82 | |
Stephen Boyd | 11893da | 2016-12-28 14:57:06 -0800 | [diff] [blame] | 83 | static int ci_hdrc_msm_notify_event(struct ci_hdrc *ci, unsigned event) |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 84 | { |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 85 | struct device *dev = ci->dev->parent; |
| 86 | struct ci_hdrc_msm *msm_ci = dev_get_drvdata(dev); |
Stephen Boyd | 11893da | 2016-12-28 14:57:06 -0800 | [diff] [blame] | 87 | int ret; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 88 | |
| 89 | switch (event) { |
| 90 | case CI_HDRC_CONTROLLER_RESET_EVENT: |
| 91 | dev_dbg(dev, "CI_HDRC_CONTROLLER_RESET_EVENT received\n"); |
Stephen Boyd | 11893da | 2016-12-28 14:57:06 -0800 | [diff] [blame] | 92 | |
| 93 | hw_phymode_configure(ci); |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 94 | if (msm_ci->secondary_phy) { |
| 95 | u32 val = readl_relaxed(msm_ci->base + HS_PHY_SEC_CTRL); |
| 96 | val |= HS_PHY_DIG_CLAMP_N; |
| 97 | writel_relaxed(val, msm_ci->base + HS_PHY_SEC_CTRL); |
| 98 | } |
| 99 | |
Stephen Boyd | 11893da | 2016-12-28 14:57:06 -0800 | [diff] [blame] | 100 | ret = phy_init(ci->phy); |
| 101 | if (ret) |
| 102 | return ret; |
| 103 | |
| 104 | ret = phy_power_on(ci->phy); |
| 105 | if (ret) { |
| 106 | phy_exit(ci->phy); |
| 107 | return ret; |
| 108 | } |
| 109 | |
Andy Gross | 5ce7d27 | 2015-11-06 00:04:06 -0600 | [diff] [blame] | 110 | /* use AHB transactor, allow posted data writes */ |
Stephen Boyd | ee33f6e | 2016-12-28 14:57:00 -0800 | [diff] [blame] | 111 | hw_write_id_reg(ci, HS_PHY_AHB_MODE, 0xffffffff, 0x8); |
Stephen Boyd | 47654a1 | 2016-12-28 14:57:03 -0800 | [diff] [blame] | 112 | |
| 113 | /* workaround for rx buffer collision issue */ |
| 114 | hw_write_id_reg(ci, HS_PHY_GENCONFIG, |
| 115 | HS_PHY_TXFIFO_IDLE_FORCE_DIS, 0); |
| 116 | |
| 117 | if (!msm_ci->hsic) |
| 118 | hw_write_id_reg(ci, HS_PHY_GENCONFIG_2, |
| 119 | HS_PHY_ULPI_TX_PKT_EN_CLR_FIX, 0); |
| 120 | |
| 121 | if (!IS_ERR(ci->platdata->vbus_extcon.edev)) { |
| 122 | hw_write_id_reg(ci, HS_PHY_GENCONFIG_2, |
| 123 | HS_PHY_SESS_VLD_CTRL_EN, |
| 124 | HS_PHY_SESS_VLD_CTRL_EN); |
| 125 | hw_write(ci, OP_USBCMD, HSPHY_SESS_VLD_CTRL, |
| 126 | HSPHY_SESS_VLD_CTRL); |
| 127 | |
| 128 | } |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 129 | break; |
| 130 | case CI_HDRC_CONTROLLER_STOPPED_EVENT: |
| 131 | dev_dbg(dev, "CI_HDRC_CONTROLLER_STOPPED_EVENT received\n"); |
Stephen Boyd | 11893da | 2016-12-28 14:57:06 -0800 | [diff] [blame] | 132 | phy_power_off(ci->phy); |
| 133 | phy_exit(ci->phy); |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 134 | break; |
| 135 | default: |
| 136 | dev_dbg(dev, "unknown ci_hdrc event\n"); |
| 137 | break; |
| 138 | } |
Stephen Boyd | 11893da | 2016-12-28 14:57:06 -0800 | [diff] [blame] | 139 | |
| 140 | return 0; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 141 | } |
| 142 | |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 143 | static int ci_hdrc_msm_mux_phy(struct ci_hdrc_msm *ci, |
| 144 | struct platform_device *pdev) |
| 145 | { |
| 146 | struct regmap *regmap; |
| 147 | struct device *dev = &pdev->dev; |
| 148 | struct of_phandle_args args; |
| 149 | u32 val; |
| 150 | int ret; |
| 151 | |
| 152 | ret = of_parse_phandle_with_fixed_args(dev->of_node, "phy-select", 2, 0, |
| 153 | &args); |
| 154 | if (ret) |
| 155 | return 0; |
| 156 | |
| 157 | regmap = syscon_node_to_regmap(args.np); |
| 158 | of_node_put(args.np); |
| 159 | if (IS_ERR(regmap)) |
| 160 | return PTR_ERR(regmap); |
| 161 | |
| 162 | ret = regmap_write(regmap, args.args[0], args.args[1]); |
| 163 | if (ret) |
| 164 | return ret; |
| 165 | |
| 166 | ci->secondary_phy = !!args.args[1]; |
| 167 | if (ci->secondary_phy) { |
| 168 | val = readl_relaxed(ci->base + HS_PHY_SEC_CTRL); |
| 169 | val |= HS_PHY_DIG_CLAMP_N; |
| 170 | writel_relaxed(val, ci->base + HS_PHY_SEC_CTRL); |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 176 | static int ci_hdrc_msm_probe(struct platform_device *pdev) |
| 177 | { |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 178 | struct ci_hdrc_msm *ci; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 179 | struct platform_device *plat_ci; |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 180 | struct clk *clk; |
| 181 | struct reset_control *reset; |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 182 | struct resource *res; |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 183 | int ret; |
Stephen Boyd | 47654a1 | 2016-12-28 14:57:03 -0800 | [diff] [blame] | 184 | struct device_node *ulpi_node, *phy_node; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 185 | |
| 186 | dev_dbg(&pdev->dev, "ci_hdrc_msm_probe\n"); |
| 187 | |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 188 | ci = devm_kzalloc(&pdev->dev, sizeof(*ci), GFP_KERNEL); |
| 189 | if (!ci) |
| 190 | return -ENOMEM; |
| 191 | platform_set_drvdata(pdev, ci); |
| 192 | |
Stephen Boyd | 26f8e3a | 2016-12-28 14:57:04 -0800 | [diff] [blame] | 193 | ci->pdata.name = "ci_hdrc_msm"; |
| 194 | ci->pdata.capoffset = DEF_CAPOFFSET; |
| 195 | ci->pdata.flags = CI_HDRC_REGS_SHARED | CI_HDRC_DISABLE_STREAMING | |
Stephen Boyd | 11893da | 2016-12-28 14:57:06 -0800 | [diff] [blame] | 196 | CI_HDRC_OVERRIDE_AHB_BURST | |
| 197 | CI_HDRC_OVERRIDE_PHY_CONTROL; |
Stephen Boyd | 26f8e3a | 2016-12-28 14:57:04 -0800 | [diff] [blame] | 198 | ci->pdata.notify_event = ci_hdrc_msm_notify_event; |
Ivan T. Ivanov | 2629b10 | 2014-05-04 09:24:41 +0800 | [diff] [blame] | 199 | |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 200 | reset = devm_reset_control_get(&pdev->dev, "core"); |
| 201 | if (IS_ERR(reset)) |
| 202 | return PTR_ERR(reset); |
| 203 | |
| 204 | ci->core_clk = clk = devm_clk_get(&pdev->dev, "core"); |
| 205 | if (IS_ERR(clk)) |
| 206 | return PTR_ERR(clk); |
| 207 | |
| 208 | ci->iface_clk = clk = devm_clk_get(&pdev->dev, "iface"); |
| 209 | if (IS_ERR(clk)) |
| 210 | return PTR_ERR(clk); |
| 211 | |
| 212 | ci->fs_clk = clk = devm_clk_get(&pdev->dev, "fs"); |
| 213 | if (IS_ERR(clk)) { |
| 214 | if (PTR_ERR(clk) == -EPROBE_DEFER) |
| 215 | return -EPROBE_DEFER; |
| 216 | ci->fs_clk = NULL; |
| 217 | } |
| 218 | |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 219 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 220 | ci->base = devm_ioremap_resource(&pdev->dev, res); |
Wei Yongjun | 753dfd2 | 2017-01-25 14:02:48 +0000 | [diff] [blame] | 221 | if (IS_ERR(ci->base)) |
| 222 | return PTR_ERR(ci->base); |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 223 | |
Stephen Boyd | 1b8fc5a | 2016-12-28 14:57:05 -0800 | [diff] [blame] | 224 | ci->rcdev.owner = THIS_MODULE; |
| 225 | ci->rcdev.ops = &ci_hdrc_msm_reset_ops; |
| 226 | ci->rcdev.of_node = pdev->dev.of_node; |
| 227 | ci->rcdev.nr_resets = 2; |
| 228 | ret = reset_controller_register(&ci->rcdev); |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 229 | if (ret) |
| 230 | return ret; |
| 231 | |
Stephen Boyd | 1b8fc5a | 2016-12-28 14:57:05 -0800 | [diff] [blame] | 232 | ret = clk_prepare_enable(ci->fs_clk); |
| 233 | if (ret) |
| 234 | goto err_fs; |
| 235 | |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 236 | reset_control_assert(reset); |
| 237 | usleep_range(10000, 12000); |
| 238 | reset_control_deassert(reset); |
| 239 | |
| 240 | clk_disable_unprepare(ci->fs_clk); |
| 241 | |
| 242 | ret = clk_prepare_enable(ci->core_clk); |
| 243 | if (ret) |
Stephen Boyd | 1b8fc5a | 2016-12-28 14:57:05 -0800 | [diff] [blame] | 244 | goto err_fs; |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 245 | |
| 246 | ret = clk_prepare_enable(ci->iface_clk); |
| 247 | if (ret) |
| 248 | goto err_iface; |
| 249 | |
Stephen Boyd | 2fc305b | 2016-12-28 14:57:02 -0800 | [diff] [blame] | 250 | ret = ci_hdrc_msm_mux_phy(ci, pdev); |
| 251 | if (ret) |
| 252 | goto err_mux; |
| 253 | |
Stephen Boyd | 47654a1 | 2016-12-28 14:57:03 -0800 | [diff] [blame] | 254 | ulpi_node = of_find_node_by_name(pdev->dev.of_node, "ulpi"); |
| 255 | if (ulpi_node) { |
| 256 | phy_node = of_get_next_available_child(ulpi_node, NULL); |
| 257 | ci->hsic = of_device_is_compatible(phy_node, "qcom,usb-hsic-phy"); |
| 258 | of_node_put(phy_node); |
| 259 | } |
| 260 | of_node_put(ulpi_node); |
| 261 | |
Stephen Boyd | 26f8e3a | 2016-12-28 14:57:04 -0800 | [diff] [blame] | 262 | plat_ci = ci_hdrc_add_device(&pdev->dev, pdev->resource, |
| 263 | pdev->num_resources, &ci->pdata); |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 264 | if (IS_ERR(plat_ci)) { |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 265 | ret = PTR_ERR(plat_ci); |
Stephen Boyd | ed04f19 | 2016-12-28 14:57:07 -0800 | [diff] [blame] | 266 | if (ret != -EPROBE_DEFER) |
| 267 | dev_err(&pdev->dev, "ci_hdrc_add_device failed!\n"); |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 268 | goto err_mux; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 269 | } |
| 270 | |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 271 | ci->ci = plat_ci; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 272 | |
Stephen Boyd | 2c8ea46 | 2016-12-28 14:56:58 -0800 | [diff] [blame] | 273 | pm_runtime_set_active(&pdev->dev); |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 274 | pm_runtime_no_callbacks(&pdev->dev); |
| 275 | pm_runtime_enable(&pdev->dev); |
| 276 | |
| 277 | return 0; |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 278 | |
| 279 | err_mux: |
| 280 | clk_disable_unprepare(ci->iface_clk); |
| 281 | err_iface: |
| 282 | clk_disable_unprepare(ci->core_clk); |
Stephen Boyd | 1b8fc5a | 2016-12-28 14:57:05 -0800 | [diff] [blame] | 283 | err_fs: |
| 284 | reset_controller_unregister(&ci->rcdev); |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 285 | return ret; |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static int ci_hdrc_msm_remove(struct platform_device *pdev) |
| 289 | { |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 290 | struct ci_hdrc_msm *ci = platform_get_drvdata(pdev); |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 291 | |
| 292 | pm_runtime_disable(&pdev->dev); |
Stephen Boyd | e9f15a7 | 2016-12-28 14:57:01 -0800 | [diff] [blame] | 293 | ci_hdrc_remove_device(ci->ci); |
| 294 | clk_disable_unprepare(ci->iface_clk); |
| 295 | clk_disable_unprepare(ci->core_clk); |
Stephen Boyd | 1b8fc5a | 2016-12-28 14:57:05 -0800 | [diff] [blame] | 296 | reset_controller_unregister(&ci->rcdev); |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Ivan T. Ivanov | 2629b10 | 2014-05-04 09:24:41 +0800 | [diff] [blame] | 301 | static const struct of_device_id msm_ci_dt_match[] = { |
| 302 | { .compatible = "qcom,ci-hdrc", }, |
| 303 | { } |
| 304 | }; |
| 305 | MODULE_DEVICE_TABLE(of, msm_ci_dt_match); |
| 306 | |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 307 | static struct platform_driver ci_hdrc_msm_driver = { |
| 308 | .probe = ci_hdrc_msm_probe, |
| 309 | .remove = ci_hdrc_msm_remove, |
Ivan T. Ivanov | 2629b10 | 2014-05-04 09:24:41 +0800 | [diff] [blame] | 310 | .driver = { |
| 311 | .name = "msm_hsusb", |
| 312 | .of_match_table = msm_ci_dt_match, |
| 313 | }, |
Alexander Shishkin | 8e22978 | 2013-06-24 14:46:36 +0300 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | module_platform_driver(ci_hdrc_msm_driver); |
| 317 | |
| 318 | MODULE_ALIAS("platform:msm_hsusb"); |
| 319 | MODULE_ALIAS("platform:ci13xxx_msm"); |
| 320 | MODULE_LICENSE("GPL v2"); |