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> |
| 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 | |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 63 | struct q6v3_data { |
| 64 | void __iomem *base; |
| 65 | unsigned long start_addr; |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 66 | struct pil_device *pil; |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 67 | struct clk *pll; |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 70 | static int pil_q6v3_init_image(struct pil_desc *pil, const u8 *metadata, |
| 71 | size_t size) |
| 72 | { |
| 73 | const struct elf32_hdr *ehdr = (struct elf32_hdr *)metadata; |
| 74 | struct q6v3_data *drv = dev_get_drvdata(pil->dev); |
| 75 | drv->start_addr = ehdr->e_entry; |
| 76 | return 0; |
| 77 | } |
| 78 | |
Stephen Boyd | 32f7f2e | 2012-03-22 10:59:22 -0700 | [diff] [blame] | 79 | static void pil_q6v3_remove_proxy_votes(struct pil_desc *pil) |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 80 | { |
Stephen Boyd | 32f7f2e | 2012-03-22 10:59:22 -0700 | [diff] [blame] | 81 | struct q6v3_data *drv = dev_get_drvdata(pil->dev); |
Stephen Boyd | 4f8b7e2 | 2012-01-24 13:31:29 -0800 | [diff] [blame] | 82 | clk_disable_unprepare(drv->pll); |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Stephen Boyd | 32f7f2e | 2012-03-22 10:59:22 -0700 | [diff] [blame] | 85 | static int pil_q6v3_make_proxy_votes(struct pil_desc *pil) |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 86 | { |
| 87 | int ret; |
Stephen Boyd | 32f7f2e | 2012-03-22 10:59:22 -0700 | [diff] [blame] | 88 | struct q6v3_data *drv = dev_get_drvdata(pil->dev); |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 89 | |
Stephen Boyd | 4f8b7e2 | 2012-01-24 13:31:29 -0800 | [diff] [blame] | 90 | ret = clk_prepare_enable(drv->pll); |
| 91 | if (ret) { |
Stephen Boyd | 32f7f2e | 2012-03-22 10:59:22 -0700 | [diff] [blame] | 92 | dev_err(pil->dev, "Failed to enable PLL\n"); |
Stephen Boyd | 4f8b7e2 | 2012-01-24 13:31:29 -0800 | [diff] [blame] | 93 | return ret; |
| 94 | } |
Stephen Boyd | 4f8b7e2 | 2012-01-24 13:31:29 -0800 | [diff] [blame] | 95 | return 0; |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 98 | static int pil_q6v3_reset(struct pil_desc *pil) |
| 99 | { |
| 100 | u32 reg; |
| 101 | struct q6v3_data *drv = dev_get_drvdata(pil->dev); |
| 102 | |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 103 | /* Put Q6 into reset */ |
| 104 | reg = readl_relaxed(LCC_Q6_FUNC); |
| 105 | reg |= Q6SS_SS_ARES | Q6SS_ISDB_ARES | Q6SS_ETM_ARES | STOP_CORE | |
| 106 | CORE_ARES; |
| 107 | reg &= ~CORE_GFM4_CLK_EN; |
| 108 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 109 | |
| 110 | /* Wait 8 AHB cycles for Q6 to be fully reset (AHB = 1.5Mhz) */ |
| 111 | usleep_range(20, 30); |
| 112 | |
| 113 | /* Turn on Q6 memory */ |
| 114 | reg |= CORE_GFM4_CLK_EN | CORE_L1_MEM_CORE_EN | CORE_TCM_MEM_CORE_EN | |
| 115 | CORE_TCM_MEM_PERPH_EN; |
| 116 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 117 | |
| 118 | /* Turn on Q6 core clocks and take core out of reset */ |
| 119 | reg &= ~(CLAMP_IO | Q6SS_SS_ARES | Q6SS_ISDB_ARES | Q6SS_ETM_ARES | |
| 120 | CORE_ARES); |
| 121 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 122 | |
| 123 | /* Wait for clocks to be enabled */ |
| 124 | mb(); |
| 125 | /* Program boot address */ |
| 126 | writel_relaxed((drv->start_addr >> 12) & 0xFFFFF, |
| 127 | drv->base + QDSP6SS_RST_EVB); |
| 128 | |
| 129 | writel_relaxed(Q6_STRAP_TCM_CONFIG | Q6_STRAP_TCM_BASE, |
| 130 | drv->base + QDSP6SS_STRAP_TCM); |
| 131 | writel_relaxed(Q6_STRAP_AHB_UPPER | Q6_STRAP_AHB_LOWER, |
| 132 | drv->base + QDSP6SS_STRAP_AHB); |
| 133 | |
| 134 | /* Wait for addresses to be programmed before starting Q6 */ |
| 135 | mb(); |
| 136 | |
| 137 | /* Start Q6 instruction execution */ |
| 138 | reg &= ~STOP_CORE; |
| 139 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int pil_q6v3_shutdown(struct pil_desc *pil) |
| 145 | { |
| 146 | u32 reg; |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 147 | |
| 148 | /* Put Q6 into reset */ |
| 149 | reg = readl_relaxed(LCC_Q6_FUNC); |
| 150 | reg |= Q6SS_SS_ARES | Q6SS_ISDB_ARES | Q6SS_ETM_ARES | STOP_CORE | |
| 151 | CORE_ARES; |
| 152 | reg &= ~CORE_GFM4_CLK_EN; |
| 153 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 154 | |
| 155 | /* Wait 8 AHB cycles for Q6 to be fully reset (AHB = 1.5Mhz) */ |
| 156 | usleep_range(20, 30); |
| 157 | |
| 158 | /* Turn off Q6 memory */ |
| 159 | reg &= ~(CORE_L1_MEM_CORE_EN | CORE_TCM_MEM_CORE_EN | |
| 160 | CORE_TCM_MEM_PERPH_EN); |
| 161 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 162 | |
| 163 | reg |= CLAMP_IO; |
| 164 | writel_relaxed(reg, LCC_Q6_FUNC); |
| 165 | |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static struct pil_reset_ops pil_q6v3_ops = { |
| 170 | .init_image = pil_q6v3_init_image, |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 171 | .auth_and_reset = pil_q6v3_reset, |
| 172 | .shutdown = pil_q6v3_shutdown, |
Stephen Boyd | 32f7f2e | 2012-03-22 10:59:22 -0700 | [diff] [blame] | 173 | .proxy_vote = pil_q6v3_make_proxy_votes, |
| 174 | .proxy_unvote = pil_q6v3_remove_proxy_votes, |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static int pil_q6v3_init_image_trusted(struct pil_desc *pil, |
| 178 | const u8 *metadata, size_t size) |
| 179 | { |
| 180 | return pas_init_image(PAS_Q6, metadata, size); |
| 181 | } |
| 182 | |
| 183 | static int pil_q6v3_reset_trusted(struct pil_desc *pil) |
| 184 | { |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 185 | return pas_auth_and_reset(PAS_Q6); |
| 186 | } |
| 187 | |
| 188 | static int pil_q6v3_shutdown_trusted(struct pil_desc *pil) |
| 189 | { |
Stephen Boyd | 32f7f2e | 2012-03-22 10:59:22 -0700 | [diff] [blame] | 190 | return pas_shutdown(PAS_Q6); |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static struct pil_reset_ops pil_q6v3_ops_trusted = { |
| 194 | .init_image = pil_q6v3_init_image_trusted, |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 195 | .auth_and_reset = pil_q6v3_reset_trusted, |
| 196 | .shutdown = pil_q6v3_shutdown_trusted, |
Stephen Boyd | 32f7f2e | 2012-03-22 10:59:22 -0700 | [diff] [blame] | 197 | .proxy_vote = pil_q6v3_make_proxy_votes, |
| 198 | .proxy_unvote = pil_q6v3_remove_proxy_votes, |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | static int __devinit pil_q6v3_driver_probe(struct platform_device *pdev) |
| 202 | { |
| 203 | struct q6v3_data *drv; |
| 204 | struct resource *res; |
| 205 | struct pil_desc *desc; |
| 206 | |
| 207 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 208 | if (!res) |
| 209 | return -EINVAL; |
| 210 | |
| 211 | drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL); |
| 212 | if (!drv) |
| 213 | return -ENOMEM; |
| 214 | platform_set_drvdata(pdev, drv); |
| 215 | |
| 216 | drv->base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 217 | if (!drv->base) |
| 218 | return -ENOMEM; |
| 219 | |
| 220 | desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL); |
| 221 | if (!drv) |
| 222 | return -ENOMEM; |
| 223 | |
Stephen Boyd | f11bfb5 | 2012-03-23 15:30:48 -0700 | [diff] [blame] | 224 | drv->pll = devm_clk_get(&pdev->dev, "pll4"); |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 225 | if (IS_ERR(drv->pll)) |
| 226 | return PTR_ERR(drv->pll); |
| 227 | |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 228 | desc->name = "q6"; |
| 229 | desc->dev = &pdev->dev; |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 230 | desc->owner = THIS_MODULE; |
Stephen Boyd | 32f7f2e | 2012-03-22 10:59:22 -0700 | [diff] [blame] | 231 | desc->proxy_timeout = 10000; |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 232 | |
| 233 | if (pas_supported(PAS_Q6) > 0) { |
| 234 | desc->ops = &pil_q6v3_ops_trusted; |
| 235 | dev_info(&pdev->dev, "using secure boot\n"); |
| 236 | } else { |
| 237 | desc->ops = &pil_q6v3_ops; |
| 238 | dev_info(&pdev->dev, "using non-secure boot\n"); |
| 239 | } |
| 240 | |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 241 | drv->pil = msm_pil_register(desc); |
| 242 | if (IS_ERR(drv->pil)) { |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 243 | return PTR_ERR(drv->pil); |
Stephen Boyd | 4f8b7e2 | 2012-01-24 13:31:29 -0800 | [diff] [blame] | 244 | } |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int __devexit pil_q6v3_driver_exit(struct platform_device *pdev) |
| 249 | { |
| 250 | struct q6v3_data *drv = platform_get_drvdata(pdev); |
Stephen Boyd | 6d67d25 | 2011-09-27 11:50:05 -0700 | [diff] [blame] | 251 | msm_pil_unregister(drv->pil); |
Stephen Boyd | 3acc9e4 | 2011-09-28 16:46:40 -0700 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static struct platform_driver pil_q6v3_driver = { |
| 256 | .probe = pil_q6v3_driver_probe, |
| 257 | .remove = __devexit_p(pil_q6v3_driver_exit), |
| 258 | .driver = { |
| 259 | .name = "pil_qdsp6v3", |
| 260 | .owner = THIS_MODULE, |
| 261 | }, |
| 262 | }; |
| 263 | |
| 264 | static int __init pil_q6v3_init(void) |
| 265 | { |
| 266 | return platform_driver_register(&pil_q6v3_driver); |
| 267 | } |
| 268 | module_init(pil_q6v3_init); |
| 269 | |
| 270 | static void __exit pil_q6v3_exit(void) |
| 271 | { |
| 272 | platform_driver_unregister(&pil_q6v3_driver); |
| 273 | } |
| 274 | module_exit(pil_q6v3_exit); |
| 275 | |
| 276 | MODULE_DESCRIPTION("Support for booting QDSP6v3 (Hexagon) processors"); |
| 277 | MODULE_LICENSE("GPL v2"); |