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