Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 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 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/iopoll.h> |
| 19 | #include <linux/ioport.h> |
| 20 | #include <linux/elf.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/clk.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/of.h> |
| 26 | #include <linux/regulator/consumer.h> |
| 27 | |
| 28 | #include <mach/clk.h> |
| 29 | |
| 30 | #include "peripheral-loader.h" |
| 31 | #include "pil-q6v5.h" |
| 32 | |
| 33 | /* Q6 Register Offsets */ |
| 34 | #define QDSP6SS_RST_EVB 0x010 |
| 35 | |
| 36 | /* AXI Halting Registers */ |
| 37 | #define MSS_Q6_HALT_BASE 0x180 |
| 38 | #define MSS_MODEM_HALT_BASE 0x200 |
| 39 | #define MSS_NC_HALT_BASE 0x280 |
| 40 | |
Matt Wagantall | 16bc5cc | 2012-08-09 21:33:23 -0700 | [diff] [blame] | 41 | /* MSS_CLAMP_IO Register Value */ |
| 42 | #define MSS_IO_UNCLAMP_ALL 0x40 |
| 43 | |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 44 | /* RMB Status Register Values */ |
| 45 | #define STATUS_PBL_SUCCESS 0x1 |
| 46 | #define STATUS_XPU_UNLOCKED 0x1 |
| 47 | #define STATUS_XPU_UNLOCKED_SCRIBBLED 0x2 |
| 48 | |
| 49 | /* PBL/MBA interface registers */ |
| 50 | #define RMB_MBA_IMAGE 0x00 |
| 51 | #define RMB_PBL_STATUS 0x04 |
| 52 | #define RMB_MBA_STATUS 0x0C |
| 53 | |
| 54 | #define PBL_MBA_WAIT_TIMEOUT_US 100000 |
| 55 | #define PROXY_TIMEOUT_MS 10000 |
| 56 | #define POLL_INTERVAL_US 50 |
| 57 | |
| 58 | static int pil_mss_power_up(struct device *dev) |
| 59 | { |
| 60 | int ret; |
| 61 | struct q6v5_data *drv = dev_get_drvdata(dev); |
| 62 | |
| 63 | ret = regulator_enable(drv->vreg); |
| 64 | if (ret) |
| 65 | dev_err(dev, "Failed to enable regulator.\n"); |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | static int pil_mss_power_down(struct device *dev) |
| 71 | { |
| 72 | struct q6v5_data *drv = dev_get_drvdata(dev); |
| 73 | |
| 74 | return regulator_disable(drv->vreg); |
| 75 | } |
| 76 | |
Matt Wagantall | 8c2246d | 2012-08-12 17:08:04 -0700 | [diff] [blame] | 77 | static int pil_mss_enable_clks(struct q6v5_data *drv) |
| 78 | { |
| 79 | int ret; |
| 80 | |
| 81 | ret = clk_prepare_enable(drv->ahb_clk); |
| 82 | if (ret) |
| 83 | goto err_ahb_clk; |
| 84 | ret = clk_reset(drv->core_clk, CLK_RESET_DEASSERT); |
| 85 | if (ret) |
| 86 | goto err_reset; |
| 87 | ret = clk_prepare_enable(drv->core_clk); |
| 88 | if (ret) |
| 89 | goto err_core_clk; |
| 90 | ret = clk_prepare_enable(drv->axi_clk); |
| 91 | if (ret) |
| 92 | goto err_axi_clk; |
| 93 | ret = clk_prepare_enable(drv->reg_clk); |
| 94 | if (ret) |
| 95 | goto err_reg_clk; |
| 96 | ret = clk_prepare_enable(drv->rom_clk); |
| 97 | if (ret) |
| 98 | goto err_rom_clk; |
| 99 | |
| 100 | return 0; |
| 101 | |
| 102 | err_rom_clk: |
| 103 | clk_disable_unprepare(drv->reg_clk); |
| 104 | err_reg_clk: |
| 105 | clk_disable_unprepare(drv->axi_clk); |
| 106 | err_axi_clk: |
| 107 | clk_disable_unprepare(drv->core_clk); |
| 108 | err_core_clk: |
| 109 | clk_reset(drv->core_clk, CLK_RESET_ASSERT); |
| 110 | err_reset: |
| 111 | clk_disable_unprepare(drv->ahb_clk); |
| 112 | err_ahb_clk: |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | static void pil_mss_disable_clks(struct q6v5_data *drv) |
| 117 | { |
| 118 | clk_disable_unprepare(drv->rom_clk); |
| 119 | clk_disable_unprepare(drv->reg_clk); |
| 120 | clk_disable_unprepare(drv->axi_clk); |
| 121 | clk_disable_unprepare(drv->core_clk); |
| 122 | clk_reset(drv->core_clk, CLK_RESET_ASSERT); |
| 123 | clk_disable_unprepare(drv->ahb_clk); |
| 124 | } |
| 125 | |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 126 | static int wait_for_mba_ready(struct device *dev) |
| 127 | { |
| 128 | struct q6v5_data *drv = dev_get_drvdata(dev); |
| 129 | int ret; |
| 130 | u32 status; |
| 131 | |
| 132 | /* Wait for PBL completion. */ |
| 133 | ret = readl_poll_timeout(drv->rmb_base + RMB_PBL_STATUS, status, |
| 134 | status != 0, POLL_INTERVAL_US, PBL_MBA_WAIT_TIMEOUT_US); |
| 135 | if (ret) { |
| 136 | dev_err(dev, "PBL boot timed out\n"); |
| 137 | return ret; |
| 138 | } |
| 139 | if (status != STATUS_PBL_SUCCESS) { |
| 140 | dev_err(dev, "PBL returned unexpected status %d\n", status); |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | /* Wait for MBA completion. */ |
| 145 | ret = readl_poll_timeout(drv->rmb_base + RMB_MBA_STATUS, status, |
| 146 | status != 0, POLL_INTERVAL_US, PBL_MBA_WAIT_TIMEOUT_US); |
| 147 | if (ret) { |
| 148 | dev_err(dev, "MBA boot timed out\n"); |
| 149 | return ret; |
| 150 | } |
| 151 | if (status != STATUS_XPU_UNLOCKED && |
| 152 | status != STATUS_XPU_UNLOCKED_SCRIBBLED) { |
| 153 | dev_err(dev, "MBA returned unexpected status %d\n", status); |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int pil_mss_shutdown(struct pil_desc *pil) |
| 161 | { |
| 162 | struct q6v5_data *drv = dev_get_drvdata(pil->dev); |
| 163 | |
| 164 | pil_q6v5_halt_axi_port(pil, drv->axi_halt_base + MSS_Q6_HALT_BASE); |
| 165 | pil_q6v5_halt_axi_port(pil, drv->axi_halt_base + MSS_MODEM_HALT_BASE); |
| 166 | pil_q6v5_halt_axi_port(pil, drv->axi_halt_base + MSS_NC_HALT_BASE); |
| 167 | |
| 168 | /* |
| 169 | * If the shutdown function is called before the reset function, clocks |
| 170 | * and power will not be enabled yet. Enable them here so that register |
| 171 | * writes performed during the shutdown succeed. |
| 172 | */ |
| 173 | if (drv->is_booted == false) { |
| 174 | pil_mss_power_up(pil->dev); |
Matt Wagantall | 8c2246d | 2012-08-12 17:08:04 -0700 | [diff] [blame] | 175 | pil_mss_enable_clks(drv); |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 176 | } |
| 177 | pil_q6v5_shutdown(pil); |
| 178 | |
Matt Wagantall | 8c2246d | 2012-08-12 17:08:04 -0700 | [diff] [blame] | 179 | pil_mss_disable_clks(drv); |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 180 | pil_mss_power_down(pil->dev); |
| 181 | |
| 182 | writel_relaxed(1, drv->restart_reg); |
| 183 | |
| 184 | drv->is_booted = false; |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int pil_mss_reset(struct pil_desc *pil) |
| 190 | { |
| 191 | struct q6v5_data *drv = dev_get_drvdata(pil->dev); |
| 192 | int ret; |
| 193 | |
Matt Wagantall | 33c2ec7 | 2012-07-26 20:26:57 -0700 | [diff] [blame] | 194 | /* Deassert reset to subsystem and wait for propagation */ |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 195 | writel_relaxed(0, drv->restart_reg); |
| 196 | mb(); |
Matt Wagantall | 33c2ec7 | 2012-07-26 20:26:57 -0700 | [diff] [blame] | 197 | udelay(2); |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Bring subsystem out of reset and enable required |
| 201 | * regulators and clocks. |
| 202 | */ |
| 203 | ret = pil_mss_power_up(pil->dev); |
| 204 | if (ret) |
| 205 | goto err_power; |
| 206 | |
Matt Wagantall | 8c2246d | 2012-08-12 17:08:04 -0700 | [diff] [blame] | 207 | ret = pil_mss_enable_clks(drv); |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 208 | if (ret) |
| 209 | goto err_clks; |
| 210 | |
| 211 | /* Program Image Address */ |
Matt Wagantall | f11928a | 2012-07-27 15:47:59 -0700 | [diff] [blame] | 212 | if (drv->self_auth) { |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 213 | writel_relaxed(drv->start_addr, drv->rmb_base + RMB_MBA_IMAGE); |
Matt Wagantall | f11928a | 2012-07-27 15:47:59 -0700 | [diff] [blame] | 214 | /* Ensure write to RMB base occurs before reset is released. */ |
| 215 | mb(); |
| 216 | } else { |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 217 | writel_relaxed((drv->start_addr >> 4) & 0x0FFFFFF0, |
| 218 | drv->reg_base + QDSP6SS_RST_EVB); |
Matt Wagantall | f11928a | 2012-07-27 15:47:59 -0700 | [diff] [blame] | 219 | } |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 220 | |
Matt Wagantall | 16bc5cc | 2012-08-09 21:33:23 -0700 | [diff] [blame] | 221 | /* De-assert MSS IO clamps */ |
| 222 | writel_relaxed(MSS_IO_UNCLAMP_ALL, drv->io_clamp_reg); |
| 223 | |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 224 | ret = pil_q6v5_reset(pil); |
| 225 | if (ret) |
| 226 | goto err_q6v5_reset; |
| 227 | |
| 228 | /* Wait for MBA to start. Check for PBL and MBA errors while waiting. */ |
| 229 | if (drv->self_auth) { |
| 230 | ret = wait_for_mba_ready(pil->dev); |
| 231 | if (ret) |
| 232 | goto err_auth; |
| 233 | } |
| 234 | |
| 235 | drv->is_booted = true; |
| 236 | |
| 237 | return 0; |
| 238 | |
| 239 | err_auth: |
| 240 | pil_q6v5_shutdown(pil); |
| 241 | err_q6v5_reset: |
Matt Wagantall | 8c2246d | 2012-08-12 17:08:04 -0700 | [diff] [blame] | 242 | pil_mss_disable_clks(drv); |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 243 | err_clks: |
| 244 | pil_mss_power_down(pil->dev); |
| 245 | err_power: |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | static struct pil_reset_ops pil_mss_ops = { |
| 250 | .init_image = pil_q6v5_init_image, |
| 251 | .proxy_vote = pil_q6v5_make_proxy_votes, |
| 252 | .proxy_unvote = pil_q6v5_remove_proxy_votes, |
| 253 | .auth_and_reset = pil_mss_reset, |
| 254 | .shutdown = pil_mss_shutdown, |
| 255 | }; |
| 256 | |
| 257 | static int __devinit pil_mss_driver_probe(struct platform_device *pdev) |
| 258 | { |
| 259 | struct q6v5_data *drv; |
| 260 | struct pil_desc *desc; |
| 261 | struct resource *res; |
| 262 | int ret; |
| 263 | |
| 264 | desc = pil_q6v5_init(pdev); |
| 265 | if (IS_ERR(desc)) |
| 266 | return PTR_ERR(desc); |
| 267 | drv = platform_get_drvdata(pdev); |
| 268 | if (drv == NULL) |
| 269 | return -ENODEV; |
| 270 | |
| 271 | desc->ops = &pil_mss_ops; |
| 272 | desc->owner = THIS_MODULE; |
| 273 | desc->proxy_timeout = PROXY_TIMEOUT_MS; |
| 274 | |
| 275 | of_property_read_u32(pdev->dev.of_node, "qcom,pil-self-auth", |
| 276 | &drv->self_auth); |
| 277 | if (drv->self_auth) { |
| 278 | res = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
| 279 | drv->rmb_base = devm_ioremap(&pdev->dev, res->start, |
| 280 | resource_size(res)); |
| 281 | if (!drv->rmb_base) |
| 282 | return -ENOMEM; |
| 283 | } |
| 284 | |
| 285 | res = platform_get_resource(pdev, IORESOURCE_MEM, 3); |
| 286 | drv->restart_reg = devm_ioremap(&pdev->dev, res->start, |
| 287 | resource_size(res)); |
| 288 | if (!drv->restart_reg) |
| 289 | return -ENOMEM; |
| 290 | |
Matt Wagantall | 16bc5cc | 2012-08-09 21:33:23 -0700 | [diff] [blame] | 291 | res = platform_get_resource(pdev, IORESOURCE_MEM, 4); |
| 292 | drv->io_clamp_reg = devm_ioremap(&pdev->dev, res->start, |
| 293 | resource_size(res)); |
| 294 | if (!drv->io_clamp_reg) |
| 295 | return -ENOMEM; |
| 296 | |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 297 | drv->vreg = devm_regulator_get(&pdev->dev, "vdd_mss"); |
| 298 | if (IS_ERR(drv->vreg)) |
| 299 | return PTR_ERR(drv->vreg); |
| 300 | |
Matt Wagantall | 5d929a8 | 2012-08-02 11:36:15 -0700 | [diff] [blame] | 301 | ret = regulator_set_voltage(drv->vreg, 1050000, 1050000); |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 302 | if (ret) |
| 303 | dev_err(&pdev->dev, "Failed to set regulator's voltage.\n"); |
| 304 | |
| 305 | ret = regulator_set_optimum_mode(drv->vreg, 100000); |
| 306 | if (ret < 0) { |
| 307 | dev_err(&pdev->dev, "Failed to set regulator's mode.\n"); |
| 308 | return ret; |
| 309 | } |
| 310 | |
Matt Wagantall | 8c2246d | 2012-08-12 17:08:04 -0700 | [diff] [blame] | 311 | drv->ahb_clk = devm_clk_get(&pdev->dev, "iface_clk"); |
| 312 | if (IS_ERR(drv->ahb_clk)) |
| 313 | return PTR_ERR(drv->ahb_clk); |
| 314 | |
| 315 | drv->core_clk = devm_clk_get(&pdev->dev, "core_clk"); |
| 316 | if (IS_ERR(drv->core_clk)) |
| 317 | return PTR_ERR(drv->core_clk); |
| 318 | |
| 319 | drv->axi_clk = devm_clk_get(&pdev->dev, "bus_clk"); |
| 320 | if (IS_ERR(drv->axi_clk)) |
| 321 | return PTR_ERR(drv->axi_clk); |
| 322 | |
| 323 | drv->reg_clk = devm_clk_get(&pdev->dev, "reg_clk"); |
| 324 | if (IS_ERR(drv->reg_clk)) |
| 325 | return PTR_ERR(drv->reg_clk); |
| 326 | |
| 327 | drv->rom_clk = devm_clk_get(&pdev->dev, "mem_clk"); |
| 328 | if (IS_ERR(drv->rom_clk)) |
| 329 | return PTR_ERR(drv->rom_clk); |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 330 | |
| 331 | drv->pil = msm_pil_register(desc); |
| 332 | if (IS_ERR(drv->pil)) |
| 333 | return PTR_ERR(drv->pil); |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int __devexit pil_mss_driver_exit(struct platform_device *pdev) |
| 339 | { |
| 340 | struct q6v5_data *drv = platform_get_drvdata(pdev); |
| 341 | msm_pil_unregister(drv->pil); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static struct of_device_id mss_match_table[] = { |
| 346 | { .compatible = "qcom,pil-q6v5-mss" }, |
| 347 | {} |
| 348 | }; |
| 349 | |
| 350 | static struct platform_driver pil_mss_driver = { |
| 351 | .probe = pil_mss_driver_probe, |
| 352 | .remove = __devexit_p(pil_mss_driver_exit), |
| 353 | .driver = { |
| 354 | .name = "pil-q6v5-mss", |
| 355 | .of_match_table = mss_match_table, |
| 356 | .owner = THIS_MODULE, |
| 357 | }, |
| 358 | }; |
| 359 | |
| 360 | static int __init pil_mss_init(void) |
| 361 | { |
| 362 | return platform_driver_register(&pil_mss_driver); |
| 363 | } |
| 364 | module_init(pil_mss_init); |
| 365 | |
| 366 | static void __exit pil_mss_exit(void) |
| 367 | { |
| 368 | platform_driver_unregister(&pil_mss_driver); |
| 369 | } |
| 370 | module_exit(pil_mss_exit); |
| 371 | |
| 372 | MODULE_DESCRIPTION("Support for booting modem subsystems with QDSP6v5 Hexagon processors"); |
| 373 | MODULE_LICENSE("GPL v2"); |