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 */ |
| 158 | if (drv->self_auth) |
| 159 | writel_relaxed(drv->start_addr, drv->rmb_base + RMB_MBA_IMAGE); |
| 160 | else |
| 161 | writel_relaxed((drv->start_addr >> 4) & 0x0FFFFFF0, |
| 162 | drv->reg_base + QDSP6SS_RST_EVB); |
| 163 | |
| 164 | ret = pil_q6v5_reset(pil); |
| 165 | if (ret) |
| 166 | goto err_q6v5_reset; |
| 167 | |
| 168 | /* Wait for MBA to start. Check for PBL and MBA errors while waiting. */ |
| 169 | if (drv->self_auth) { |
| 170 | ret = wait_for_mba_ready(pil->dev); |
| 171 | if (ret) |
| 172 | goto err_auth; |
| 173 | } |
| 174 | |
| 175 | drv->is_booted = true; |
| 176 | |
| 177 | return 0; |
| 178 | |
| 179 | err_auth: |
| 180 | pil_q6v5_shutdown(pil); |
| 181 | err_q6v5_reset: |
| 182 | pil_q6v5_disable_clks(pil); |
| 183 | err_clks: |
| 184 | pil_mss_power_down(pil->dev); |
| 185 | err_power: |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static struct pil_reset_ops pil_mss_ops = { |
| 190 | .init_image = pil_q6v5_init_image, |
| 191 | .proxy_vote = pil_q6v5_make_proxy_votes, |
| 192 | .proxy_unvote = pil_q6v5_remove_proxy_votes, |
| 193 | .auth_and_reset = pil_mss_reset, |
| 194 | .shutdown = pil_mss_shutdown, |
| 195 | }; |
| 196 | |
| 197 | static int __devinit pil_mss_driver_probe(struct platform_device *pdev) |
| 198 | { |
| 199 | struct q6v5_data *drv; |
| 200 | struct pil_desc *desc; |
| 201 | struct resource *res; |
| 202 | int ret; |
| 203 | |
| 204 | desc = pil_q6v5_init(pdev); |
| 205 | if (IS_ERR(desc)) |
| 206 | return PTR_ERR(desc); |
| 207 | drv = platform_get_drvdata(pdev); |
| 208 | if (drv == NULL) |
| 209 | return -ENODEV; |
| 210 | |
| 211 | desc->ops = &pil_mss_ops; |
| 212 | desc->owner = THIS_MODULE; |
| 213 | desc->proxy_timeout = PROXY_TIMEOUT_MS; |
| 214 | |
| 215 | of_property_read_u32(pdev->dev.of_node, "qcom,pil-self-auth", |
| 216 | &drv->self_auth); |
| 217 | if (drv->self_auth) { |
| 218 | res = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
| 219 | drv->rmb_base = devm_ioremap(&pdev->dev, res->start, |
| 220 | resource_size(res)); |
| 221 | if (!drv->rmb_base) |
| 222 | return -ENOMEM; |
| 223 | } |
| 224 | |
| 225 | res = platform_get_resource(pdev, IORESOURCE_MEM, 3); |
| 226 | drv->restart_reg = devm_ioremap(&pdev->dev, res->start, |
| 227 | resource_size(res)); |
| 228 | if (!drv->restart_reg) |
| 229 | return -ENOMEM; |
| 230 | |
| 231 | drv->vreg = devm_regulator_get(&pdev->dev, "vdd_mss"); |
| 232 | if (IS_ERR(drv->vreg)) |
| 233 | return PTR_ERR(drv->vreg); |
| 234 | |
| 235 | ret = regulator_set_voltage(drv->vreg, 1150000, 1150000); |
| 236 | if (ret) |
| 237 | dev_err(&pdev->dev, "Failed to set regulator's voltage.\n"); |
| 238 | |
| 239 | ret = regulator_set_optimum_mode(drv->vreg, 100000); |
| 240 | if (ret < 0) { |
| 241 | dev_err(&pdev->dev, "Failed to set regulator's mode.\n"); |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | drv->mem_clk = devm_clk_get(&pdev->dev, "mem_clk"); |
| 246 | if (IS_ERR(drv->mem_clk)) |
| 247 | return PTR_ERR(drv->mem_clk); |
| 248 | |
| 249 | drv->pil = msm_pil_register(desc); |
| 250 | if (IS_ERR(drv->pil)) |
| 251 | return PTR_ERR(drv->pil); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int __devexit pil_mss_driver_exit(struct platform_device *pdev) |
| 257 | { |
| 258 | struct q6v5_data *drv = platform_get_drvdata(pdev); |
| 259 | msm_pil_unregister(drv->pil); |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static struct of_device_id mss_match_table[] = { |
| 264 | { .compatible = "qcom,pil-q6v5-mss" }, |
| 265 | {} |
| 266 | }; |
| 267 | |
| 268 | static struct platform_driver pil_mss_driver = { |
| 269 | .probe = pil_mss_driver_probe, |
| 270 | .remove = __devexit_p(pil_mss_driver_exit), |
| 271 | .driver = { |
| 272 | .name = "pil-q6v5-mss", |
| 273 | .of_match_table = mss_match_table, |
| 274 | .owner = THIS_MODULE, |
| 275 | }, |
| 276 | }; |
| 277 | |
| 278 | static int __init pil_mss_init(void) |
| 279 | { |
| 280 | return platform_driver_register(&pil_mss_driver); |
| 281 | } |
| 282 | module_init(pil_mss_init); |
| 283 | |
| 284 | static void __exit pil_mss_exit(void) |
| 285 | { |
| 286 | platform_driver_unregister(&pil_mss_driver); |
| 287 | } |
| 288 | module_exit(pil_mss_exit); |
| 289 | |
| 290 | MODULE_DESCRIPTION("Support for booting modem subsystems with QDSP6v5 Hexagon processors"); |
| 291 | MODULE_LICENSE("GPL v2"); |