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