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 | |
| 41 | /* RMB Status Register Values */ |
| 42 | #define STATUS_PBL_SUCCESS 0x1 |
| 43 | #define STATUS_XPU_UNLOCKED 0x1 |
| 44 | #define STATUS_XPU_UNLOCKED_SCRIBBLED 0x2 |
| 45 | |
| 46 | /* PBL/MBA interface registers */ |
| 47 | #define RMB_MBA_IMAGE 0x00 |
| 48 | #define RMB_PBL_STATUS 0x04 |
| 49 | #define RMB_MBA_STATUS 0x0C |
| 50 | |
| 51 | #define PBL_MBA_WAIT_TIMEOUT_US 100000 |
| 52 | #define PROXY_TIMEOUT_MS 10000 |
| 53 | #define POLL_INTERVAL_US 50 |
| 54 | |
| 55 | static int pil_mss_power_up(struct device *dev) |
| 56 | { |
| 57 | int ret; |
| 58 | struct q6v5_data *drv = dev_get_drvdata(dev); |
| 59 | |
| 60 | ret = regulator_enable(drv->vreg); |
| 61 | if (ret) |
| 62 | dev_err(dev, "Failed to enable regulator.\n"); |
| 63 | |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | static int pil_mss_power_down(struct device *dev) |
| 68 | { |
| 69 | struct q6v5_data *drv = dev_get_drvdata(dev); |
| 70 | |
| 71 | return regulator_disable(drv->vreg); |
| 72 | } |
| 73 | |
| 74 | static int wait_for_mba_ready(struct device *dev) |
| 75 | { |
| 76 | struct q6v5_data *drv = dev_get_drvdata(dev); |
| 77 | int ret; |
| 78 | u32 status; |
| 79 | |
| 80 | /* Wait for PBL completion. */ |
| 81 | ret = readl_poll_timeout(drv->rmb_base + RMB_PBL_STATUS, status, |
| 82 | status != 0, POLL_INTERVAL_US, PBL_MBA_WAIT_TIMEOUT_US); |
| 83 | if (ret) { |
| 84 | dev_err(dev, "PBL boot timed out\n"); |
| 85 | return ret; |
| 86 | } |
| 87 | if (status != STATUS_PBL_SUCCESS) { |
| 88 | dev_err(dev, "PBL returned unexpected status %d\n", status); |
| 89 | return -EINVAL; |
| 90 | } |
| 91 | |
| 92 | /* Wait for MBA completion. */ |
| 93 | ret = readl_poll_timeout(drv->rmb_base + RMB_MBA_STATUS, status, |
| 94 | status != 0, POLL_INTERVAL_US, PBL_MBA_WAIT_TIMEOUT_US); |
| 95 | if (ret) { |
| 96 | dev_err(dev, "MBA boot timed out\n"); |
| 97 | return ret; |
| 98 | } |
| 99 | if (status != STATUS_XPU_UNLOCKED && |
| 100 | status != STATUS_XPU_UNLOCKED_SCRIBBLED) { |
| 101 | dev_err(dev, "MBA returned unexpected status %d\n", status); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int pil_mss_shutdown(struct pil_desc *pil) |
| 109 | { |
| 110 | struct q6v5_data *drv = dev_get_drvdata(pil->dev); |
| 111 | |
| 112 | pil_q6v5_halt_axi_port(pil, drv->axi_halt_base + MSS_Q6_HALT_BASE); |
| 113 | pil_q6v5_halt_axi_port(pil, drv->axi_halt_base + MSS_MODEM_HALT_BASE); |
| 114 | pil_q6v5_halt_axi_port(pil, drv->axi_halt_base + MSS_NC_HALT_BASE); |
| 115 | |
| 116 | /* |
| 117 | * If the shutdown function is called before the reset function, clocks |
| 118 | * and power will not be enabled yet. Enable them here so that register |
| 119 | * writes performed during the shutdown succeed. |
| 120 | */ |
| 121 | if (drv->is_booted == false) { |
| 122 | pil_mss_power_up(pil->dev); |
| 123 | pil_q6v5_enable_clks(pil); |
| 124 | } |
| 125 | pil_q6v5_shutdown(pil); |
| 126 | |
| 127 | pil_q6v5_disable_clks(pil); |
| 128 | pil_mss_power_down(pil->dev); |
| 129 | |
| 130 | writel_relaxed(1, drv->restart_reg); |
| 131 | |
| 132 | drv->is_booted = false; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int pil_mss_reset(struct pil_desc *pil) |
| 138 | { |
| 139 | struct q6v5_data *drv = dev_get_drvdata(pil->dev); |
| 140 | int ret; |
| 141 | |
| 142 | writel_relaxed(0, drv->restart_reg); |
| 143 | mb(); |
| 144 | |
| 145 | /* |
| 146 | * Bring subsystem out of reset and enable required |
| 147 | * regulators and clocks. |
| 148 | */ |
| 149 | ret = pil_mss_power_up(pil->dev); |
| 150 | if (ret) |
| 151 | goto err_power; |
| 152 | |
| 153 | ret = pil_q6v5_enable_clks(pil); |
| 154 | if (ret) |
| 155 | goto err_clks; |
| 156 | |
| 157 | /* Program Image Address */ |
Matt Wagantall | f11928a | 2012-07-27 15:47:59 -0700 | [diff] [blame] | 158 | if (drv->self_auth) { |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 159 | writel_relaxed(drv->start_addr, drv->rmb_base + RMB_MBA_IMAGE); |
Matt Wagantall | f11928a | 2012-07-27 15:47:59 -0700 | [diff] [blame] | 160 | /* Ensure write to RMB base occurs before reset is released. */ |
| 161 | mb(); |
| 162 | } else { |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 163 | writel_relaxed((drv->start_addr >> 4) & 0x0FFFFFF0, |
| 164 | drv->reg_base + QDSP6SS_RST_EVB); |
Matt Wagantall | f11928a | 2012-07-27 15:47:59 -0700 | [diff] [blame] | 165 | } |
Matt Wagantall | 4e2599e | 2012-03-21 22:31:35 -0700 | [diff] [blame] | 166 | |
| 167 | ret = pil_q6v5_reset(pil); |
| 168 | if (ret) |
| 169 | goto err_q6v5_reset; |
| 170 | |
| 171 | /* Wait for MBA to start. Check for PBL and MBA errors while waiting. */ |
| 172 | if (drv->self_auth) { |
| 173 | ret = wait_for_mba_ready(pil->dev); |
| 174 | if (ret) |
| 175 | goto err_auth; |
| 176 | } |
| 177 | |
| 178 | drv->is_booted = true; |
| 179 | |
| 180 | return 0; |
| 181 | |
| 182 | err_auth: |
| 183 | pil_q6v5_shutdown(pil); |
| 184 | err_q6v5_reset: |
| 185 | pil_q6v5_disable_clks(pil); |
| 186 | err_clks: |
| 187 | pil_mss_power_down(pil->dev); |
| 188 | err_power: |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | static struct pil_reset_ops pil_mss_ops = { |
| 193 | .init_image = pil_q6v5_init_image, |
| 194 | .proxy_vote = pil_q6v5_make_proxy_votes, |
| 195 | .proxy_unvote = pil_q6v5_remove_proxy_votes, |
| 196 | .auth_and_reset = pil_mss_reset, |
| 197 | .shutdown = pil_mss_shutdown, |
| 198 | }; |
| 199 | |
| 200 | static int __devinit pil_mss_driver_probe(struct platform_device *pdev) |
| 201 | { |
| 202 | struct q6v5_data *drv; |
| 203 | struct pil_desc *desc; |
| 204 | struct resource *res; |
| 205 | int ret; |
| 206 | |
| 207 | desc = pil_q6v5_init(pdev); |
| 208 | if (IS_ERR(desc)) |
| 209 | return PTR_ERR(desc); |
| 210 | drv = platform_get_drvdata(pdev); |
| 211 | if (drv == NULL) |
| 212 | return -ENODEV; |
| 213 | |
| 214 | desc->ops = &pil_mss_ops; |
| 215 | desc->owner = THIS_MODULE; |
| 216 | desc->proxy_timeout = PROXY_TIMEOUT_MS; |
| 217 | |
| 218 | of_property_read_u32(pdev->dev.of_node, "qcom,pil-self-auth", |
| 219 | &drv->self_auth); |
| 220 | if (drv->self_auth) { |
| 221 | res = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
| 222 | drv->rmb_base = devm_ioremap(&pdev->dev, res->start, |
| 223 | resource_size(res)); |
| 224 | if (!drv->rmb_base) |
| 225 | return -ENOMEM; |
| 226 | } |
| 227 | |
| 228 | res = platform_get_resource(pdev, IORESOURCE_MEM, 3); |
| 229 | drv->restart_reg = devm_ioremap(&pdev->dev, res->start, |
| 230 | resource_size(res)); |
| 231 | if (!drv->restart_reg) |
| 232 | return -ENOMEM; |
| 233 | |
| 234 | drv->vreg = devm_regulator_get(&pdev->dev, "vdd_mss"); |
| 235 | if (IS_ERR(drv->vreg)) |
| 236 | return PTR_ERR(drv->vreg); |
| 237 | |
| 238 | ret = regulator_set_voltage(drv->vreg, 1150000, 1150000); |
| 239 | if (ret) |
| 240 | dev_err(&pdev->dev, "Failed to set regulator's voltage.\n"); |
| 241 | |
| 242 | ret = regulator_set_optimum_mode(drv->vreg, 100000); |
| 243 | if (ret < 0) { |
| 244 | dev_err(&pdev->dev, "Failed to set regulator's mode.\n"); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | drv->mem_clk = devm_clk_get(&pdev->dev, "mem_clk"); |
| 249 | if (IS_ERR(drv->mem_clk)) |
| 250 | return PTR_ERR(drv->mem_clk); |
| 251 | |
| 252 | drv->pil = msm_pil_register(desc); |
| 253 | if (IS_ERR(drv->pil)) |
| 254 | return PTR_ERR(drv->pil); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int __devexit pil_mss_driver_exit(struct platform_device *pdev) |
| 260 | { |
| 261 | struct q6v5_data *drv = platform_get_drvdata(pdev); |
| 262 | msm_pil_unregister(drv->pil); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static struct of_device_id mss_match_table[] = { |
| 267 | { .compatible = "qcom,pil-q6v5-mss" }, |
| 268 | {} |
| 269 | }; |
| 270 | |
| 271 | static struct platform_driver pil_mss_driver = { |
| 272 | .probe = pil_mss_driver_probe, |
| 273 | .remove = __devexit_p(pil_mss_driver_exit), |
| 274 | .driver = { |
| 275 | .name = "pil-q6v5-mss", |
| 276 | .of_match_table = mss_match_table, |
| 277 | .owner = THIS_MODULE, |
| 278 | }, |
| 279 | }; |
| 280 | |
| 281 | static int __init pil_mss_init(void) |
| 282 | { |
| 283 | return platform_driver_register(&pil_mss_driver); |
| 284 | } |
| 285 | module_init(pil_mss_init); |
| 286 | |
| 287 | static void __exit pil_mss_exit(void) |
| 288 | { |
| 289 | platform_driver_unregister(&pil_mss_driver); |
| 290 | } |
| 291 | module_exit(pil_mss_exit); |
| 292 | |
| 293 | MODULE_DESCRIPTION("Support for booting modem subsystems with QDSP6v5 Hexagon processors"); |
| 294 | MODULE_LICENSE("GPL v2"); |