blob: c4477ff2089ed11dee10db78501c79b54e4d3341 [file] [log] [blame]
Matt Wagantall292aace2012-01-26 19:12:34 -08001/*
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 Wagantall19ac4fd2012-02-03 20:18:23 -080024#include <linux/smp.h>
Matt Wagantall292aace2012-01-26 19:12:34 -080025
26#include <mach/msm_iomap.h>
27#include <mach/msm_xo.h>
Matt Wagantall556d3f72012-02-16 17:36:29 -080028#include <mach/socinfo.h>
Matt Wagantall292aace2012-01-26 19:12:34 -080029
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 Wagantall19ac4fd2012-02-03 20:18:23 -080039#define GSS_CSR_CFG_HID 0x2C
Matt Wagantall292aace2012-01-26 19:12:34 -080040
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 Wagantall292aace2012-01-26 19:12:34 -080046#define PLL5_STATUS (MSM_CLK_CTL_BASE + 0x30F8)
47#define PLL_ENA_GSS (MSM_CLK_CTL_BASE + 0x3480)
Matt Wagantall292aace2012-01-26 19:12:34 -080048
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
61struct gss_data {
62 void __iomem *base;
Matt Wagantall19ac4fd2012-02-03 20:18:23 -080063 void __iomem *qgic2_base;
Matt Wagantall292aace2012-01-26 19:12:34 -080064 unsigned long start_addr;
65 struct delayed_work work;
66 struct clk *xo;
Stephen Boyd6d67d252011-09-27 11:50:05 -070067 struct pil_device *pil;
Matt Wagantall292aace2012-01-26 19:12:34 -080068};
69
70static int nop_verify_blob(struct pil_desc *pil, u32 phy_addr, size_t size)
71{
72 return 0;
73}
74
75static int pil_gss_init_image(struct pil_desc *pil, const u8 *metadata,
76 size_t size)
77{
78 const struct elf32_hdr *ehdr = (struct elf32_hdr *)metadata;
79 struct gss_data *drv = dev_get_drvdata(pil->dev);
80 drv->start_addr = ehdr->e_entry;
81 return 0;
82}
83
84static int make_gss_proxy_votes(struct device *dev)
85{
86 int ret;
87 struct gss_data *drv = dev_get_drvdata(dev);
88
89 ret = clk_prepare_enable(drv->xo);
90 if (ret) {
91 dev_err(dev, "Failed to enable XO\n");
92 return ret;
93 }
94 schedule_delayed_work(&drv->work, msecs_to_jiffies(PROXY_VOTE_TIMEOUT));
95 return 0;
96}
97
98static void remove_gss_proxy_votes(struct work_struct *work)
99{
100 struct gss_data *drv = container_of(work, struct gss_data, work.work);
101 clk_disable_unprepare(drv->xo);
102}
103
104static void remove_gss_proxy_votes_now(struct gss_data *drv)
105{
106 flush_delayed_work(&drv->work);
107}
108
109static void gss_init(struct gss_data *drv)
110{
111 void __iomem *base = drv->base;
112
113 /* Supply clocks to GSS. */
114 writel_relaxed(XO_CLK_BRANCH_ENA, GSS_CXO_SRC_CTL);
115 writel_relaxed(SLP_CLK_BRANCH_ENA, GSS_SLP_CLK_CTL);
116
117 /* Deassert GSS reset and clamps. */
118 writel_relaxed(0x0, GSS_RESET);
119 writel_relaxed(0x0, GSS_CLAMP_ENA);
120 mb();
121
122 /*
123 * Configure clock source and dividers for 288MHz core, 144MHz AXI and
124 * 72MHz AHB, all derived from the 288MHz PLL.
125 */
126 writel_relaxed(0x341, base + GSS_CSR_CLK_BLK_CONFIG);
127 writel_relaxed(0x1, base + GSS_CSR_AHB_CLK_SEL);
128
129 /* Assert all GSS resets. */
130 writel_relaxed(0x7F, base + GSS_CSR_RESET);
131
132 /* Enable all bus clocks and wait for resets to propagate. */
133 writel_relaxed(0x1F, base + GSS_CSR_CLK_ENABLE);
134 mb();
135 udelay(1);
136
137 /* Release subsystem from reset, but leave A5 in reset. */
138 writel_relaxed(A5_RESET, base + GSS_CSR_RESET);
139}
140
Matt Wagantall556d3f72012-02-16 17:36:29 -0800141static void cfg_qgic2_bus_access(void *data)
Matt Wagantall292aace2012-01-26 19:12:34 -0800142{
Matt Wagantall19ac4fd2012-02-03 20:18:23 -0800143 struct gss_data *drv = data;
Matt Wagantall19ac4fd2012-02-03 20:18:23 -0800144 int i;
Matt Wagantall292aace2012-01-26 19:12:34 -0800145
Matt Wagantall556d3f72012-02-16 17:36:29 -0800146 /*
147 * Apply a 8064 v1.0 workaround to configure QGIC bus access.
148 * This must be done from Krait 0 to configure the Master ID
149 * correctly.
150 */
151 writel_relaxed(0x2, drv->base + GSS_CSR_CFG_HID);
Matt Wagantall19ac4fd2012-02-03 20:18:23 -0800152 for (i = 0; i <= 3; i++)
153 readl_relaxed(drv->qgic2_base);
Matt Wagantall292aace2012-01-26 19:12:34 -0800154}
155
156static 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 Wagantall19ac4fd2012-02-03 20:18:23 -0800208static 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
Matt Wagantall556d3f72012-02-16 17:36:29 -0800236 if (cpu_is_apq8064() &&
237 ((SOCINFO_VERSION_MAJOR(socinfo_get_version()) == 1) &&
238 (SOCINFO_VERSION_MINOR(socinfo_get_version()) == 0))) {
239 ret = smp_call_function_single(0, cfg_qgic2_bus_access, drv, 1);
240 if (ret) {
241 pr_err("Failed to configure QGIC2 bus access\n");
242 pil_gss_shutdown(pil);
243 return ret;
244 }
Matt Wagantall19ac4fd2012-02-03 20:18:23 -0800245 }
246
247 /* Release A5 from reset. */
248 writel_relaxed(0x0, base + GSS_CSR_RESET);
249
250 return 0;
251}
252
Matt Wagantall292aace2012-01-26 19:12:34 -0800253static 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
Matt Wagantall11afeee2012-02-07 18:38:59 -0800260static int pil_gss_init_image_trusted(struct pil_desc *pil,
261 const u8 *metadata, size_t size)
262{
263 return pas_init_image(PAS_GSS, metadata, size);
264}
265
Matt Wagantall556d3f72012-02-16 17:36:29 -0800266static int pil_gss_shutdown_trusted(struct pil_desc *pil)
267{
268 struct gss_data *drv = dev_get_drvdata(pil->dev);
269 int ret;
270
Stephen Boyd7663ccc2012-02-21 19:34:55 -0800271 /*
272 * CXO is used in the secure shutdown code to configure the processor
273 * for low power mode.
274 */
275 ret = clk_prepare_enable(drv->xo);
276 if (ret) {
277 dev_err(pil->dev, "Failed to enable XO\n");
Matt Wagantall556d3f72012-02-16 17:36:29 -0800278 return ret;
Stephen Boyd7663ccc2012-02-21 19:34:55 -0800279 }
Matt Wagantall556d3f72012-02-16 17:36:29 -0800280
Stephen Boyd7663ccc2012-02-21 19:34:55 -0800281 ret = pas_shutdown(PAS_GSS);
282 clk_disable_unprepare(drv->xo);
Matt Wagantall556d3f72012-02-16 17:36:29 -0800283 remove_gss_proxy_votes_now(drv);
284
285 return ret;
286}
287
Matt Wagantall11afeee2012-02-07 18:38:59 -0800288static int pil_gss_reset_trusted(struct pil_desc *pil)
289{
290 struct gss_data *drv = dev_get_drvdata(pil->dev);
291 int err;
292
293 err = make_gss_proxy_votes(pil->dev);
294 if (err)
295 return err;
296
297 err = pas_auth_and_reset(PAS_GSS);
298 if (err)
299 remove_gss_proxy_votes_now(drv);
300
Matt Wagantall556d3f72012-02-16 17:36:29 -0800301 if (cpu_is_apq8064() &&
302 ((SOCINFO_VERSION_MAJOR(socinfo_get_version()) == 1) &&
303 (SOCINFO_VERSION_MINOR(socinfo_get_version()) == 0))) {
304 err = smp_call_function_single(0, cfg_qgic2_bus_access, drv, 1);
305 if (err) {
306 pr_err("Failed to configure QGIC2 bus access\n");
307 pil_gss_shutdown_trusted(pil);
308 return err;
309 }
310 /*
311 * On 8064v1.0, pas_auth_and_reset() will not release the A5
312 * from reset. Linux must do this after cfg_qgic2_bus_access()
313 * is called on CPU0.
314 */
315 writel_relaxed(0x0, drv->base + GSS_CSR_RESET);
316 }
317
Matt Wagantall11afeee2012-02-07 18:38:59 -0800318 return err;
319}
320
Matt Wagantall11afeee2012-02-07 18:38:59 -0800321static struct pil_reset_ops pil_gss_ops_trusted = {
322 .init_image = pil_gss_init_image_trusted,
323 .verify_blob = nop_verify_blob,
324 .auth_and_reset = pil_gss_reset_trusted,
325 .shutdown = pil_gss_shutdown_trusted,
326};
327
Matt Wagantall292aace2012-01-26 19:12:34 -0800328static int __devinit pil_gss_probe(struct platform_device *pdev)
329{
330 struct gss_data *drv;
331 struct resource *res;
332 struct pil_desc *desc;
Matt Wagantall292aace2012-01-26 19:12:34 -0800333
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 Wagantall19ac4fd2012-02-03 20:18:23 -0800351 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 Wagantall292aace2012-01-26 19:12:34 -0800360 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;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700366 desc->owner = THIS_MODULE;
Matt Wagantall292aace2012-01-26 19:12:34 -0800367
Matt Wagantall11afeee2012-02-07 18:38:59 -0800368 if (pas_supported(PAS_GSS) > 0) {
369 desc->ops = &pil_gss_ops_trusted;
370 dev_info(&pdev->dev, "using secure boot\n");
371 } else {
372 desc->ops = &pil_gss_ops;
373 dev_info(&pdev->dev, "using non-secure boot\n");
374 }
Matt Wagantall292aace2012-01-26 19:12:34 -0800375
376 INIT_DELAYED_WORK(&drv->work, remove_gss_proxy_votes);
377
Stephen Boyd6d67d252011-09-27 11:50:05 -0700378 drv->pil = msm_pil_register(desc);
379 if (IS_ERR(drv->pil)) {
Matt Wagantall292aace2012-01-26 19:12:34 -0800380 flush_delayed_work_sync(&drv->work);
381 clk_put(drv->xo);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700382 return PTR_ERR(drv->pil);
Matt Wagantall292aace2012-01-26 19:12:34 -0800383 }
Stephen Boyd6d67d252011-09-27 11:50:05 -0700384 return 0;
Matt Wagantall292aace2012-01-26 19:12:34 -0800385}
386
387static int __devexit pil_gss_remove(struct platform_device *pdev)
388{
389 struct gss_data *drv = platform_get_drvdata(pdev);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700390 msm_pil_unregister(drv->pil);
Matt Wagantall292aace2012-01-26 19:12:34 -0800391 flush_delayed_work_sync(&drv->work);
392 clk_put(drv->xo);
393 return 0;
394}
395
396static struct platform_driver pil_gss_driver = {
397 .probe = pil_gss_probe,
398 .remove = __devexit_p(pil_gss_remove),
399 .driver = {
400 .name = "pil_gss",
401 .owner = THIS_MODULE,
402 },
403};
404
405static int __init pil_gss_init(void)
406{
407 return platform_driver_register(&pil_gss_driver);
408}
409module_init(pil_gss_init);
410
411static void __exit pil_gss_exit(void)
412{
413 platform_driver_unregister(&pil_gss_driver);
414}
415module_exit(pil_gss_exit);
416
417MODULE_DESCRIPTION("Support for booting the GSS processor");
418MODULE_LICENSE("GPL v2");