Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame^] | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/ioport.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/elf.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/clk.h> |
| 22 | |
| 23 | #include <mach/msm_iomap.h> |
| 24 | |
| 25 | #include "peripheral-loader.h" |
| 26 | #include "scm-pas.h" |
| 27 | |
| 28 | #define QDSP6SS_RST_EVB 0x0000 |
| 29 | #define QDSP6SS_STRAP_TCM 0x001C |
| 30 | #define QDSP6SS_STRAP_AHB 0x0020 |
| 31 | |
| 32 | #define LCC_Q6_FUNC (MSM_LPASS_CLK_CTL_BASE + 0x001C) |
| 33 | #define LV_EN BIT(27) |
| 34 | #define STOP_CORE BIT(26) |
| 35 | #define CLAMP_IO BIT(25) |
| 36 | #define Q6SS_PRIV_ARES BIT(24) |
| 37 | #define Q6SS_SS_ARES BIT(23) |
| 38 | #define Q6SS_ISDB_ARES BIT(22) |
| 39 | #define Q6SS_ETM_ARES BIT(21) |
| 40 | #define Q6_JTAG_CRC_EN BIT(20) |
| 41 | #define Q6_JTAG_INV_EN BIT(19) |
| 42 | #define Q6_JTAG_CXC_EN BIT(18) |
| 43 | #define Q6_PXO_CRC_EN BIT(17) |
| 44 | #define Q6_PXO_INV_EN BIT(16) |
| 45 | #define Q6_PXO_CXC_EN BIT(15) |
| 46 | #define Q6_PXO_SLEEP_EN BIT(14) |
| 47 | #define Q6_SLP_CRC_EN BIT(13) |
| 48 | #define Q6_SLP_INV_EN BIT(12) |
| 49 | #define Q6_SLP_CXC_EN BIT(11) |
| 50 | #define CORE_ARES BIT(10) |
| 51 | #define CORE_L1_MEM_CORE_EN BIT(9) |
| 52 | #define CORE_TCM_MEM_CORE_EN BIT(8) |
| 53 | #define CORE_TCM_MEM_PERPH_EN BIT(7) |
| 54 | #define CORE_GFM4_CLK_EN BIT(2) |
| 55 | #define CORE_GFM4_RES BIT(1) |
| 56 | #define RAMP_PLL_SRC_SEL BIT(0) |
| 57 | |
| 58 | #define Q6_STRAP_AHB_UPPER (0x290 << 12) |
| 59 | #define Q6_STRAP_AHB_LOWER 0x280 |
| 60 | #define Q6_STRAP_TCM_BASE (0x28C << 15) |
| 61 | #define Q6_STRAP_TCM_CONFIG 0x28B |
| 62 | |
| 63 | #define PROXY_VOTE_TIMEOUT 10000 |
| 64 | |
| 65 | struct q6v3_data { |
| 66 | void __iomem *base; |
| 67 | unsigned long start_addr; |
| 68 | struct clk *pll; |
| 69 | struct timer_list timer; |
| 70 | }; |
| 71 | |
| 72 | static int nop_verify_blob(struct pil_desc *pil, u32 phy_addr, size_t size) |
| 73 | { |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int pil_q6v3_init_image(struct pil_desc *pil, const u8 *metadata, |
| 78 | size_t size) |
| 79 | { |
| 80 | const struct elf32_hdr *ehdr = (struct elf32_hdr *)metadata; |
| 81 | struct q6v3_data *drv = dev_get_drvdata(pil->dev); |
| 82 | drv->start_addr = ehdr->e_entry; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static void q6v3_remove_proxy_votes(unsigned long data) |
| 87 | { |
| 88 | struct q6v3_data *drv = (struct q6v3_data *)data; |
| 89 | clk_disable(drv->pll); |
| 90 | } |
| 91 | |
| 92 | static void q6v3_make_proxy_votes(struct device *dev) |
| 93 | { |
| 94 | int ret; |
| 95 | struct q6v3_data *drv = dev_get_drvdata(dev); |
| 96 | |
| 97 | ret = clk_enable(drv->pll); |
| 98 | if (ret) |
| 99 | dev_err(dev, "Failed to enable PLL\n"); |
| 100 | mod_timer(&drv->timer, jiffies + msecs_to_jiffies(PROXY_VOTE_TIMEOUT)); |
| 101 | } |
| 102 | |
| 103 | static void q6v3_remove_proxy_votes_now(struct q6v3_data *drv) |
| 104 | { |
| 105 | /* If the proxy vote hasn't been removed yet, remove it immediately. */ |
| 106 | if (del_timer(&drv->timer)) |
| 107 | q6v3_remove_proxy_votes((unsigned long)drv); |
| 108 | } |
| 109 | |
| 110 | static int pil_q6v3_reset(struct pil_desc *pil) |
| 111 | { |
| 112 | u32 reg; |
| 113 | struct q6v3_data *drv = dev_get_drvdata(pil->dev); |
| 114 | |
| 115 | q6v3_make_proxy_votes(pil->dev); |
| 116 | |
| 117 | /* Put Q6 into reset */ |
| 118 | reg = readl_relaxed(LCC_Q6_FUNC); |
| 119 | reg |= Q6SS_SS_ARES | Q6SS_ISDB_ARES | Q6SS_ETM_ARES | STOP_CORE | |
| 120 | CORE_ARES; |
| 121 | reg &= ~CORE_GFM4_CLK_EN; |
| 122 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 123 | |
| 124 | /* Wait 8 AHB cycles for Q6 to be fully reset (AHB = 1.5Mhz) */ |
| 125 | usleep_range(20, 30); |
| 126 | |
| 127 | /* Turn on Q6 memory */ |
| 128 | reg |= CORE_GFM4_CLK_EN | CORE_L1_MEM_CORE_EN | CORE_TCM_MEM_CORE_EN | |
| 129 | CORE_TCM_MEM_PERPH_EN; |
| 130 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 131 | |
| 132 | /* Turn on Q6 core clocks and take core out of reset */ |
| 133 | reg &= ~(CLAMP_IO | Q6SS_SS_ARES | Q6SS_ISDB_ARES | Q6SS_ETM_ARES | |
| 134 | CORE_ARES); |
| 135 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 136 | |
| 137 | /* Wait for clocks to be enabled */ |
| 138 | mb(); |
| 139 | /* Program boot address */ |
| 140 | writel_relaxed((drv->start_addr >> 12) & 0xFFFFF, |
| 141 | drv->base + QDSP6SS_RST_EVB); |
| 142 | |
| 143 | writel_relaxed(Q6_STRAP_TCM_CONFIG | Q6_STRAP_TCM_BASE, |
| 144 | drv->base + QDSP6SS_STRAP_TCM); |
| 145 | writel_relaxed(Q6_STRAP_AHB_UPPER | Q6_STRAP_AHB_LOWER, |
| 146 | drv->base + QDSP6SS_STRAP_AHB); |
| 147 | |
| 148 | /* Wait for addresses to be programmed before starting Q6 */ |
| 149 | mb(); |
| 150 | |
| 151 | /* Start Q6 instruction execution */ |
| 152 | reg &= ~STOP_CORE; |
| 153 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int pil_q6v3_shutdown(struct pil_desc *pil) |
| 159 | { |
| 160 | u32 reg; |
| 161 | struct q6v3_data *drv = dev_get_drvdata(pil->dev); |
| 162 | |
| 163 | /* Put Q6 into reset */ |
| 164 | reg = readl_relaxed(LCC_Q6_FUNC); |
| 165 | reg |= Q6SS_SS_ARES | Q6SS_ISDB_ARES | Q6SS_ETM_ARES | STOP_CORE | |
| 166 | CORE_ARES; |
| 167 | reg &= ~CORE_GFM4_CLK_EN; |
| 168 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 169 | |
| 170 | /* Wait 8 AHB cycles for Q6 to be fully reset (AHB = 1.5Mhz) */ |
| 171 | usleep_range(20, 30); |
| 172 | |
| 173 | /* Turn off Q6 memory */ |
| 174 | reg &= ~(CORE_L1_MEM_CORE_EN | CORE_TCM_MEM_CORE_EN | |
| 175 | CORE_TCM_MEM_PERPH_EN); |
| 176 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 177 | |
| 178 | reg |= CLAMP_IO; |
| 179 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 180 | |
| 181 | q6v3_remove_proxy_votes_now(drv); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static struct pil_reset_ops pil_q6v3_ops = { |
| 187 | .init_image = pil_q6v3_init_image, |
| 188 | .verify_blob = nop_verify_blob, |
| 189 | .auth_and_reset = pil_q6v3_reset, |
| 190 | .shutdown = pil_q6v3_shutdown, |
| 191 | }; |
| 192 | |
| 193 | static int pil_q6v3_init_image_trusted(struct pil_desc *pil, |
| 194 | const u8 *metadata, size_t size) |
| 195 | { |
| 196 | return pas_init_image(PAS_Q6, metadata, size); |
| 197 | } |
| 198 | |
| 199 | static int pil_q6v3_reset_trusted(struct pil_desc *pil) |
| 200 | { |
| 201 | q6v3_make_proxy_votes(pil->dev); |
| 202 | return pas_auth_and_reset(PAS_Q6); |
| 203 | } |
| 204 | |
| 205 | static int pil_q6v3_shutdown_trusted(struct pil_desc *pil) |
| 206 | { |
| 207 | int ret; |
| 208 | struct q6v3_data *drv = dev_get_drvdata(pil->dev); |
| 209 | |
| 210 | ret = pas_shutdown(PAS_Q6); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | |
| 214 | q6v3_remove_proxy_votes_now(drv); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static struct pil_reset_ops pil_q6v3_ops_trusted = { |
| 220 | .init_image = pil_q6v3_init_image_trusted, |
| 221 | .verify_blob = nop_verify_blob, |
| 222 | .auth_and_reset = pil_q6v3_reset_trusted, |
| 223 | .shutdown = pil_q6v3_shutdown_trusted, |
| 224 | }; |
| 225 | |
| 226 | static int __devinit pil_q6v3_driver_probe(struct platform_device *pdev) |
| 227 | { |
| 228 | struct q6v3_data *drv; |
| 229 | struct resource *res; |
| 230 | struct pil_desc *desc; |
| 231 | |
| 232 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 233 | if (!res) |
| 234 | return -EINVAL; |
| 235 | |
| 236 | drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL); |
| 237 | if (!drv) |
| 238 | return -ENOMEM; |
| 239 | platform_set_drvdata(pdev, drv); |
| 240 | |
| 241 | drv->base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 242 | if (!drv->base) |
| 243 | return -ENOMEM; |
| 244 | |
| 245 | desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL); |
| 246 | if (!drv) |
| 247 | return -ENOMEM; |
| 248 | |
| 249 | drv->pll = clk_get(&pdev->dev, "pll4"); |
| 250 | if (IS_ERR(drv->pll)) |
| 251 | return PTR_ERR(drv->pll); |
| 252 | |
| 253 | setup_timer(&drv->timer, q6v3_remove_proxy_votes, (unsigned long)drv); |
| 254 | desc->name = "q6"; |
| 255 | desc->dev = &pdev->dev; |
| 256 | |
| 257 | if (pas_supported(PAS_Q6) > 0) { |
| 258 | desc->ops = &pil_q6v3_ops_trusted; |
| 259 | dev_info(&pdev->dev, "using secure boot\n"); |
| 260 | } else { |
| 261 | desc->ops = &pil_q6v3_ops; |
| 262 | dev_info(&pdev->dev, "using non-secure boot\n"); |
| 263 | } |
| 264 | |
| 265 | if (msm_pil_register(desc)) |
| 266 | return -EINVAL; |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int __devexit pil_q6v3_driver_exit(struct platform_device *pdev) |
| 271 | { |
| 272 | struct q6v3_data *drv = platform_get_drvdata(pdev); |
| 273 | del_timer_sync(&drv->timer); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static struct platform_driver pil_q6v3_driver = { |
| 278 | .probe = pil_q6v3_driver_probe, |
| 279 | .remove = __devexit_p(pil_q6v3_driver_exit), |
| 280 | .driver = { |
| 281 | .name = "pil_qdsp6v3", |
| 282 | .owner = THIS_MODULE, |
| 283 | }, |
| 284 | }; |
| 285 | |
| 286 | static int __init pil_q6v3_init(void) |
| 287 | { |
| 288 | return platform_driver_register(&pil_q6v3_driver); |
| 289 | } |
| 290 | module_init(pil_q6v3_init); |
| 291 | |
| 292 | static void __exit pil_q6v3_exit(void) |
| 293 | { |
| 294 | platform_driver_unregister(&pil_q6v3_driver); |
| 295 | } |
| 296 | module_exit(pil_q6v3_exit); |
| 297 | |
| 298 | MODULE_DESCRIPTION("Support for booting QDSP6v3 (Hexagon) processors"); |
| 299 | MODULE_LICENSE("GPL v2"); |