Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [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/kernel.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/elf.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/workqueue.h> |
| 23 | #include <linux/clk.h> |
Matt Wagantall | 19ac4fd | 2012-02-03 20:18:23 -0800 | [diff] [blame] | 24 | #include <linux/smp.h> |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 25 | |
| 26 | #include <mach/msm_iomap.h> |
| 27 | #include <mach/msm_xo.h> |
Matt Wagantall | 556d3f7 | 2012-02-16 17:36:29 -0800 | [diff] [blame] | 28 | #include <mach/socinfo.h> |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 29 | |
| 30 | #include "peripheral-loader.h" |
| 31 | #include "scm-pas.h" |
| 32 | |
| 33 | #define GSS_CSR_AHB_CLK_SEL 0x0 |
| 34 | #define GSS_CSR_RESET 0x4 |
| 35 | #define GSS_CSR_CLK_BLK_CONFIG 0x8 |
| 36 | #define GSS_CSR_CLK_ENABLE 0xC |
| 37 | #define GSS_CSR_BOOT_REMAP 0x14 |
| 38 | #define GSS_CSR_POWER_UP_DOWN 0x18 |
Matt Wagantall | 19ac4fd | 2012-02-03 20:18:23 -0800 | [diff] [blame] | 39 | #define GSS_CSR_CFG_HID 0x2C |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 40 | |
| 41 | #define GSS_SLP_CLK_CTL (MSM_CLK_CTL_BASE + 0x2C60) |
| 42 | #define GSS_RESET (MSM_CLK_CTL_BASE + 0x2C64) |
| 43 | #define GSS_CLAMP_ENA (MSM_CLK_CTL_BASE + 0x2C68) |
| 44 | #define GSS_CXO_SRC_CTL (MSM_CLK_CTL_BASE + 0x2C74) |
| 45 | |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 46 | #define PLL5_STATUS (MSM_CLK_CTL_BASE + 0x30F8) |
| 47 | #define PLL_ENA_GSS (MSM_CLK_CTL_BASE + 0x3480) |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 48 | |
| 49 | #define PLL5_VOTE BIT(5) |
| 50 | #define PLL_STATUS BIT(16) |
| 51 | #define REMAP_ENABLE BIT(16) |
| 52 | #define A5_POWER_STATUS BIT(4) |
| 53 | #define A5_POWER_ENA BIT(0) |
| 54 | #define NAV_POWER_ENA BIT(1) |
| 55 | #define XO_CLK_BRANCH_ENA BIT(0) |
| 56 | #define SLP_CLK_BRANCH_ENA BIT(4) |
| 57 | #define A5_RESET BIT(0) |
| 58 | |
| 59 | #define PROXY_VOTE_TIMEOUT 10000 |
| 60 | |
| 61 | struct gss_data { |
| 62 | void __iomem *base; |
Matt Wagantall | 19ac4fd | 2012-02-03 20:18:23 -0800 | [diff] [blame] | 63 | void __iomem *qgic2_base; |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 64 | unsigned long start_addr; |
| 65 | struct delayed_work work; |
| 66 | struct clk *xo; |
| 67 | }; |
| 68 | |
| 69 | static int nop_verify_blob(struct pil_desc *pil, u32 phy_addr, size_t size) |
| 70 | { |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int pil_gss_init_image(struct pil_desc *pil, const u8 *metadata, |
| 75 | size_t size) |
| 76 | { |
| 77 | const struct elf32_hdr *ehdr = (struct elf32_hdr *)metadata; |
| 78 | struct gss_data *drv = dev_get_drvdata(pil->dev); |
| 79 | drv->start_addr = ehdr->e_entry; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int make_gss_proxy_votes(struct device *dev) |
| 84 | { |
| 85 | int ret; |
| 86 | struct gss_data *drv = dev_get_drvdata(dev); |
| 87 | |
| 88 | ret = clk_prepare_enable(drv->xo); |
| 89 | if (ret) { |
| 90 | dev_err(dev, "Failed to enable XO\n"); |
| 91 | return ret; |
| 92 | } |
| 93 | schedule_delayed_work(&drv->work, msecs_to_jiffies(PROXY_VOTE_TIMEOUT)); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static void remove_gss_proxy_votes(struct work_struct *work) |
| 98 | { |
| 99 | struct gss_data *drv = container_of(work, struct gss_data, work.work); |
| 100 | clk_disable_unprepare(drv->xo); |
| 101 | } |
| 102 | |
| 103 | static void remove_gss_proxy_votes_now(struct gss_data *drv) |
| 104 | { |
| 105 | flush_delayed_work(&drv->work); |
| 106 | } |
| 107 | |
| 108 | static void gss_init(struct gss_data *drv) |
| 109 | { |
| 110 | void __iomem *base = drv->base; |
| 111 | |
| 112 | /* Supply clocks to GSS. */ |
| 113 | writel_relaxed(XO_CLK_BRANCH_ENA, GSS_CXO_SRC_CTL); |
| 114 | writel_relaxed(SLP_CLK_BRANCH_ENA, GSS_SLP_CLK_CTL); |
| 115 | |
| 116 | /* Deassert GSS reset and clamps. */ |
| 117 | writel_relaxed(0x0, GSS_RESET); |
| 118 | writel_relaxed(0x0, GSS_CLAMP_ENA); |
| 119 | mb(); |
| 120 | |
| 121 | /* |
| 122 | * Configure clock source and dividers for 288MHz core, 144MHz AXI and |
| 123 | * 72MHz AHB, all derived from the 288MHz PLL. |
| 124 | */ |
| 125 | writel_relaxed(0x341, base + GSS_CSR_CLK_BLK_CONFIG); |
| 126 | writel_relaxed(0x1, base + GSS_CSR_AHB_CLK_SEL); |
| 127 | |
| 128 | /* Assert all GSS resets. */ |
| 129 | writel_relaxed(0x7F, base + GSS_CSR_RESET); |
| 130 | |
| 131 | /* Enable all bus clocks and wait for resets to propagate. */ |
| 132 | writel_relaxed(0x1F, base + GSS_CSR_CLK_ENABLE); |
| 133 | mb(); |
| 134 | udelay(1); |
| 135 | |
| 136 | /* Release subsystem from reset, but leave A5 in reset. */ |
| 137 | writel_relaxed(A5_RESET, base + GSS_CSR_RESET); |
| 138 | } |
| 139 | |
Matt Wagantall | 556d3f7 | 2012-02-16 17:36:29 -0800 | [diff] [blame] | 140 | static void cfg_qgic2_bus_access(void *data) |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 141 | { |
Matt Wagantall | 19ac4fd | 2012-02-03 20:18:23 -0800 | [diff] [blame] | 142 | struct gss_data *drv = data; |
Matt Wagantall | 19ac4fd | 2012-02-03 20:18:23 -0800 | [diff] [blame] | 143 | int i; |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 144 | |
Matt Wagantall | 556d3f7 | 2012-02-16 17:36:29 -0800 | [diff] [blame] | 145 | /* |
| 146 | * Apply a 8064 v1.0 workaround to configure QGIC bus access. |
| 147 | * This must be done from Krait 0 to configure the Master ID |
| 148 | * correctly. |
| 149 | */ |
| 150 | writel_relaxed(0x2, drv->base + GSS_CSR_CFG_HID); |
Matt Wagantall | 19ac4fd | 2012-02-03 20:18:23 -0800 | [diff] [blame] | 151 | for (i = 0; i <= 3; i++) |
| 152 | readl_relaxed(drv->qgic2_base); |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int pil_gss_shutdown(struct pil_desc *pil) |
| 156 | { |
| 157 | struct gss_data *drv = dev_get_drvdata(pil->dev); |
| 158 | void __iomem *base = drv->base; |
| 159 | u32 regval; |
| 160 | int ret; |
| 161 | |
| 162 | ret = clk_prepare_enable(drv->xo); |
| 163 | if (ret) { |
| 164 | dev_err(pil->dev, "Failed to enable XO\n"); |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Vote PLL on in GSS's voting register and wait for it to enable. |
| 170 | * The PLL must be enable to switch the GFMUX to a low-power source. |
| 171 | */ |
| 172 | writel_relaxed(PLL5_VOTE, PLL_ENA_GSS); |
| 173 | while ((readl_relaxed(PLL5_STATUS) & PLL_STATUS) == 0) |
| 174 | cpu_relax(); |
| 175 | |
| 176 | /* Perform one-time GSS initialization. */ |
| 177 | gss_init(drv); |
| 178 | |
| 179 | /* Assert A5 reset. */ |
| 180 | regval = readl_relaxed(base + GSS_CSR_RESET); |
| 181 | regval |= A5_RESET; |
| 182 | writel_relaxed(regval, base + GSS_CSR_RESET); |
| 183 | |
| 184 | /* Power down A5 and NAV. */ |
| 185 | regval = readl_relaxed(base + GSS_CSR_POWER_UP_DOWN); |
| 186 | regval &= ~(A5_POWER_ENA|NAV_POWER_ENA); |
| 187 | writel_relaxed(regval, base + GSS_CSR_POWER_UP_DOWN); |
| 188 | |
| 189 | /* Select XO clock source and increase dividers to save power. */ |
| 190 | regval = readl_relaxed(base + GSS_CSR_CLK_BLK_CONFIG); |
| 191 | regval |= 0x3FF; |
| 192 | writel_relaxed(regval, base + GSS_CSR_CLK_BLK_CONFIG); |
| 193 | |
| 194 | /* Disable bus clocks. */ |
| 195 | writel_relaxed(0x1F, base + GSS_CSR_CLK_ENABLE); |
| 196 | |
| 197 | /* Clear GSS PLL votes. */ |
| 198 | writel_relaxed(0, PLL_ENA_GSS); |
| 199 | mb(); |
| 200 | |
| 201 | clk_disable_unprepare(drv->xo); |
| 202 | remove_gss_proxy_votes_now(drv); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Matt Wagantall | 19ac4fd | 2012-02-03 20:18:23 -0800 | [diff] [blame] | 207 | static int pil_gss_reset(struct pil_desc *pil) |
| 208 | { |
| 209 | struct gss_data *drv = dev_get_drvdata(pil->dev); |
| 210 | void __iomem *base = drv->base; |
| 211 | unsigned long start_addr = drv->start_addr; |
| 212 | int ret; |
| 213 | |
| 214 | ret = make_gss_proxy_votes(pil->dev); |
| 215 | if (ret) |
| 216 | return ret; |
| 217 | |
| 218 | /* Vote PLL on in GSS's voting register and wait for it to enable. */ |
| 219 | writel_relaxed(PLL5_VOTE, PLL_ENA_GSS); |
| 220 | while ((readl_relaxed(PLL5_STATUS) & PLL_STATUS) == 0) |
| 221 | cpu_relax(); |
| 222 | |
| 223 | /* Perform GSS initialization. */ |
| 224 | gss_init(drv); |
| 225 | |
| 226 | /* Configure boot address and enable remap. */ |
| 227 | writel_relaxed(REMAP_ENABLE | (start_addr >> 16), |
| 228 | base + GSS_CSR_BOOT_REMAP); |
| 229 | |
| 230 | /* Power up A5 core. */ |
| 231 | writel_relaxed(A5_POWER_ENA, base + GSS_CSR_POWER_UP_DOWN); |
| 232 | while (!(readl_relaxed(base + GSS_CSR_POWER_UP_DOWN) & A5_POWER_STATUS)) |
| 233 | cpu_relax(); |
| 234 | |
Matt Wagantall | 556d3f7 | 2012-02-16 17:36:29 -0800 | [diff] [blame] | 235 | if (cpu_is_apq8064() && |
| 236 | ((SOCINFO_VERSION_MAJOR(socinfo_get_version()) == 1) && |
| 237 | (SOCINFO_VERSION_MINOR(socinfo_get_version()) == 0))) { |
| 238 | ret = smp_call_function_single(0, cfg_qgic2_bus_access, drv, 1); |
| 239 | if (ret) { |
| 240 | pr_err("Failed to configure QGIC2 bus access\n"); |
| 241 | pil_gss_shutdown(pil); |
| 242 | return ret; |
| 243 | } |
Matt Wagantall | 19ac4fd | 2012-02-03 20:18:23 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* Release A5 from reset. */ |
| 247 | writel_relaxed(0x0, base + GSS_CSR_RESET); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 252 | static struct pil_reset_ops pil_gss_ops = { |
| 253 | .init_image = pil_gss_init_image, |
| 254 | .verify_blob = nop_verify_blob, |
| 255 | .auth_and_reset = pil_gss_reset, |
| 256 | .shutdown = pil_gss_shutdown, |
| 257 | }; |
| 258 | |
Matt Wagantall | 11afeee | 2012-02-07 18:38:59 -0800 | [diff] [blame] | 259 | static int pil_gss_init_image_trusted(struct pil_desc *pil, |
| 260 | const u8 *metadata, size_t size) |
| 261 | { |
| 262 | return pas_init_image(PAS_GSS, metadata, size); |
| 263 | } |
| 264 | |
Matt Wagantall | 556d3f7 | 2012-02-16 17:36:29 -0800 | [diff] [blame] | 265 | static int pil_gss_shutdown_trusted(struct pil_desc *pil) |
| 266 | { |
| 267 | struct gss_data *drv = dev_get_drvdata(pil->dev); |
| 268 | int ret; |
| 269 | |
Stephen Boyd | 7663ccc | 2012-02-21 19:34:55 -0800 | [diff] [blame] | 270 | /* |
| 271 | * CXO is used in the secure shutdown code to configure the processor |
| 272 | * for low power mode. |
| 273 | */ |
| 274 | ret = clk_prepare_enable(drv->xo); |
| 275 | if (ret) { |
| 276 | dev_err(pil->dev, "Failed to enable XO\n"); |
Matt Wagantall | 556d3f7 | 2012-02-16 17:36:29 -0800 | [diff] [blame] | 277 | return ret; |
Stephen Boyd | 7663ccc | 2012-02-21 19:34:55 -0800 | [diff] [blame] | 278 | } |
Matt Wagantall | 556d3f7 | 2012-02-16 17:36:29 -0800 | [diff] [blame] | 279 | |
Stephen Boyd | 7663ccc | 2012-02-21 19:34:55 -0800 | [diff] [blame] | 280 | ret = pas_shutdown(PAS_GSS); |
| 281 | clk_disable_unprepare(drv->xo); |
Matt Wagantall | 556d3f7 | 2012-02-16 17:36:29 -0800 | [diff] [blame] | 282 | remove_gss_proxy_votes_now(drv); |
| 283 | |
| 284 | return ret; |
| 285 | } |
| 286 | |
Matt Wagantall | 11afeee | 2012-02-07 18:38:59 -0800 | [diff] [blame] | 287 | static int pil_gss_reset_trusted(struct pil_desc *pil) |
| 288 | { |
| 289 | struct gss_data *drv = dev_get_drvdata(pil->dev); |
| 290 | int err; |
| 291 | |
| 292 | err = make_gss_proxy_votes(pil->dev); |
| 293 | if (err) |
| 294 | return err; |
| 295 | |
| 296 | err = pas_auth_and_reset(PAS_GSS); |
| 297 | if (err) |
| 298 | remove_gss_proxy_votes_now(drv); |
| 299 | |
Matt Wagantall | 556d3f7 | 2012-02-16 17:36:29 -0800 | [diff] [blame] | 300 | if (cpu_is_apq8064() && |
| 301 | ((SOCINFO_VERSION_MAJOR(socinfo_get_version()) == 1) && |
| 302 | (SOCINFO_VERSION_MINOR(socinfo_get_version()) == 0))) { |
| 303 | err = smp_call_function_single(0, cfg_qgic2_bus_access, drv, 1); |
| 304 | if (err) { |
| 305 | pr_err("Failed to configure QGIC2 bus access\n"); |
| 306 | pil_gss_shutdown_trusted(pil); |
| 307 | return err; |
| 308 | } |
| 309 | /* |
| 310 | * On 8064v1.0, pas_auth_and_reset() will not release the A5 |
| 311 | * from reset. Linux must do this after cfg_qgic2_bus_access() |
| 312 | * is called on CPU0. |
| 313 | */ |
| 314 | writel_relaxed(0x0, drv->base + GSS_CSR_RESET); |
| 315 | } |
| 316 | |
Matt Wagantall | 11afeee | 2012-02-07 18:38:59 -0800 | [diff] [blame] | 317 | return err; |
| 318 | } |
| 319 | |
Matt Wagantall | 11afeee | 2012-02-07 18:38:59 -0800 | [diff] [blame] | 320 | static struct pil_reset_ops pil_gss_ops_trusted = { |
| 321 | .init_image = pil_gss_init_image_trusted, |
| 322 | .verify_blob = nop_verify_blob, |
| 323 | .auth_and_reset = pil_gss_reset_trusted, |
| 324 | .shutdown = pil_gss_shutdown_trusted, |
| 325 | }; |
| 326 | |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 327 | static int __devinit pil_gss_probe(struct platform_device *pdev) |
| 328 | { |
| 329 | struct gss_data *drv; |
| 330 | struct resource *res; |
| 331 | struct pil_desc *desc; |
| 332 | int ret; |
| 333 | |
| 334 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 335 | if (!res) |
| 336 | return -EINVAL; |
| 337 | |
| 338 | drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL); |
| 339 | if (!drv) |
| 340 | return -ENOMEM; |
| 341 | platform_set_drvdata(pdev, drv); |
| 342 | |
| 343 | drv->base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 344 | if (!drv->base) |
| 345 | return -ENOMEM; |
| 346 | |
| 347 | desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL); |
| 348 | if (!desc) |
| 349 | return -ENOMEM; |
| 350 | |
Matt Wagantall | 19ac4fd | 2012-02-03 20:18:23 -0800 | [diff] [blame] | 351 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 352 | if (!res) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | drv->qgic2_base = devm_ioremap(&pdev->dev, res->start, |
| 356 | resource_size(res)); |
| 357 | if (!drv->qgic2_base) |
| 358 | return -ENOMEM; |
| 359 | |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 360 | drv->xo = clk_get(&pdev->dev, "xo"); |
| 361 | if (IS_ERR(drv->xo)) |
| 362 | return PTR_ERR(drv->xo); |
| 363 | |
| 364 | desc->name = "gss"; |
| 365 | desc->dev = &pdev->dev; |
| 366 | |
Matt Wagantall | 11afeee | 2012-02-07 18:38:59 -0800 | [diff] [blame] | 367 | if (pas_supported(PAS_GSS) > 0) { |
| 368 | desc->ops = &pil_gss_ops_trusted; |
| 369 | dev_info(&pdev->dev, "using secure boot\n"); |
| 370 | } else { |
| 371 | desc->ops = &pil_gss_ops; |
| 372 | dev_info(&pdev->dev, "using non-secure boot\n"); |
| 373 | } |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 374 | |
| 375 | INIT_DELAYED_WORK(&drv->work, remove_gss_proxy_votes); |
| 376 | |
Matt Wagantall | 292aace | 2012-01-26 19:12:34 -0800 | [diff] [blame] | 377 | ret = msm_pil_register(desc); |
| 378 | if (ret) { |
| 379 | flush_delayed_work_sync(&drv->work); |
| 380 | clk_put(drv->xo); |
| 381 | } |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | static int __devexit pil_gss_remove(struct platform_device *pdev) |
| 386 | { |
| 387 | struct gss_data *drv = platform_get_drvdata(pdev); |
| 388 | flush_delayed_work_sync(&drv->work); |
| 389 | clk_put(drv->xo); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static struct platform_driver pil_gss_driver = { |
| 394 | .probe = pil_gss_probe, |
| 395 | .remove = __devexit_p(pil_gss_remove), |
| 396 | .driver = { |
| 397 | .name = "pil_gss", |
| 398 | .owner = THIS_MODULE, |
| 399 | }, |
| 400 | }; |
| 401 | |
| 402 | static int __init pil_gss_init(void) |
| 403 | { |
| 404 | return platform_driver_register(&pil_gss_driver); |
| 405 | } |
| 406 | module_init(pil_gss_init); |
| 407 | |
| 408 | static void __exit pil_gss_exit(void) |
| 409 | { |
| 410 | platform_driver_unregister(&pil_gss_driver); |
| 411 | } |
| 412 | module_exit(pil_gss_exit); |
| 413 | |
| 414 | MODULE_DESCRIPTION("Support for booting the GSS processor"); |
| 415 | MODULE_LICENSE("GPL v2"); |