blob: 2b4dae1c48f4178f77eeef6c03e13f9ebf4b8701 [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
46#define PLL5_MODE (MSM_CLK_CTL_BASE + 0x30E0)
47#define PLL5_L_VAL (MSM_CLK_CTL_BASE + 0x30E4)
48#define PLL5_M_VAL (MSM_CLK_CTL_BASE + 0x30E8)
49#define PLL5_N_VAL (MSM_CLK_CTL_BASE + 0x30EC)
50#define PLL5_CONFIG (MSM_CLK_CTL_BASE + 0x30F4)
51#define PLL5_STATUS (MSM_CLK_CTL_BASE + 0x30F8)
52#define PLL_ENA_GSS (MSM_CLK_CTL_BASE + 0x3480)
53#define PLL_ENA_RPM (MSM_CLK_CTL_BASE + 0x34A0)
54
55#define PLL5_VOTE BIT(5)
56#define PLL_STATUS BIT(16)
57#define REMAP_ENABLE BIT(16)
58#define A5_POWER_STATUS BIT(4)
59#define A5_POWER_ENA BIT(0)
60#define NAV_POWER_ENA BIT(1)
61#define XO_CLK_BRANCH_ENA BIT(0)
62#define SLP_CLK_BRANCH_ENA BIT(4)
63#define A5_RESET BIT(0)
64
65#define PROXY_VOTE_TIMEOUT 10000
66
67struct gss_data {
68 void __iomem *base;
Matt Wagantall19ac4fd2012-02-03 20:18:23 -080069 void __iomem *qgic2_base;
Matt Wagantall292aace2012-01-26 19:12:34 -080070 unsigned long start_addr;
71 struct delayed_work work;
72 struct clk *xo;
73};
74
75static int nop_verify_blob(struct pil_desc *pil, u32 phy_addr, size_t size)
76{
77 return 0;
78}
79
80static int pil_gss_init_image(struct pil_desc *pil, const u8 *metadata,
81 size_t size)
82{
83 const struct elf32_hdr *ehdr = (struct elf32_hdr *)metadata;
84 struct gss_data *drv = dev_get_drvdata(pil->dev);
85 drv->start_addr = ehdr->e_entry;
86 return 0;
87}
88
89static int make_gss_proxy_votes(struct device *dev)
90{
91 int ret;
92 struct gss_data *drv = dev_get_drvdata(dev);
93
94 ret = clk_prepare_enable(drv->xo);
95 if (ret) {
96 dev_err(dev, "Failed to enable XO\n");
97 return ret;
98 }
99 schedule_delayed_work(&drv->work, msecs_to_jiffies(PROXY_VOTE_TIMEOUT));
100 return 0;
101}
102
103static void remove_gss_proxy_votes(struct work_struct *work)
104{
105 struct gss_data *drv = container_of(work, struct gss_data, work.work);
106 clk_disable_unprepare(drv->xo);
107}
108
109static void remove_gss_proxy_votes_now(struct gss_data *drv)
110{
111 flush_delayed_work(&drv->work);
112}
113
114static void gss_init(struct gss_data *drv)
115{
116 void __iomem *base = drv->base;
117
118 /* Supply clocks to GSS. */
119 writel_relaxed(XO_CLK_BRANCH_ENA, GSS_CXO_SRC_CTL);
120 writel_relaxed(SLP_CLK_BRANCH_ENA, GSS_SLP_CLK_CTL);
121
122 /* Deassert GSS reset and clamps. */
123 writel_relaxed(0x0, GSS_RESET);
124 writel_relaxed(0x0, GSS_CLAMP_ENA);
125 mb();
126
127 /*
128 * Configure clock source and dividers for 288MHz core, 144MHz AXI and
129 * 72MHz AHB, all derived from the 288MHz PLL.
130 */
131 writel_relaxed(0x341, base + GSS_CSR_CLK_BLK_CONFIG);
132 writel_relaxed(0x1, base + GSS_CSR_AHB_CLK_SEL);
133
134 /* Assert all GSS resets. */
135 writel_relaxed(0x7F, base + GSS_CSR_RESET);
136
137 /* Enable all bus clocks and wait for resets to propagate. */
138 writel_relaxed(0x1F, base + GSS_CSR_CLK_ENABLE);
139 mb();
140 udelay(1);
141
142 /* Release subsystem from reset, but leave A5 in reset. */
143 writel_relaxed(A5_RESET, base + GSS_CSR_RESET);
144}
145
Matt Wagantall556d3f72012-02-16 17:36:29 -0800146static void cfg_qgic2_bus_access(void *data)
Matt Wagantall292aace2012-01-26 19:12:34 -0800147{
Matt Wagantall19ac4fd2012-02-03 20:18:23 -0800148 struct gss_data *drv = data;
Matt Wagantall19ac4fd2012-02-03 20:18:23 -0800149 int i;
Matt Wagantall292aace2012-01-26 19:12:34 -0800150
Matt Wagantall556d3f72012-02-16 17:36:29 -0800151 /*
152 * Apply a 8064 v1.0 workaround to configure QGIC bus access.
153 * This must be done from Krait 0 to configure the Master ID
154 * correctly.
155 */
156 writel_relaxed(0x2, drv->base + GSS_CSR_CFG_HID);
Matt Wagantall19ac4fd2012-02-03 20:18:23 -0800157 for (i = 0; i <= 3; i++)
158 readl_relaxed(drv->qgic2_base);
Matt Wagantall292aace2012-01-26 19:12:34 -0800159}
160
161static int pil_gss_shutdown(struct pil_desc *pil)
162{
163 struct gss_data *drv = dev_get_drvdata(pil->dev);
164 void __iomem *base = drv->base;
165 u32 regval;
166 int ret;
167
168 ret = clk_prepare_enable(drv->xo);
169 if (ret) {
170 dev_err(pil->dev, "Failed to enable XO\n");
171 return ret;
172 }
173
174 /*
175 * Vote PLL on in GSS's voting register and wait for it to enable.
176 * The PLL must be enable to switch the GFMUX to a low-power source.
177 */
178 writel_relaxed(PLL5_VOTE, PLL_ENA_GSS);
179 while ((readl_relaxed(PLL5_STATUS) & PLL_STATUS) == 0)
180 cpu_relax();
181
182 /* Perform one-time GSS initialization. */
183 gss_init(drv);
184
185 /* Assert A5 reset. */
186 regval = readl_relaxed(base + GSS_CSR_RESET);
187 regval |= A5_RESET;
188 writel_relaxed(regval, base + GSS_CSR_RESET);
189
190 /* Power down A5 and NAV. */
191 regval = readl_relaxed(base + GSS_CSR_POWER_UP_DOWN);
192 regval &= ~(A5_POWER_ENA|NAV_POWER_ENA);
193 writel_relaxed(regval, base + GSS_CSR_POWER_UP_DOWN);
194
195 /* Select XO clock source and increase dividers to save power. */
196 regval = readl_relaxed(base + GSS_CSR_CLK_BLK_CONFIG);
197 regval |= 0x3FF;
198 writel_relaxed(regval, base + GSS_CSR_CLK_BLK_CONFIG);
199
200 /* Disable bus clocks. */
201 writel_relaxed(0x1F, base + GSS_CSR_CLK_ENABLE);
202
203 /* Clear GSS PLL votes. */
204 writel_relaxed(0, PLL_ENA_GSS);
205 mb();
206
207 clk_disable_unprepare(drv->xo);
208 remove_gss_proxy_votes_now(drv);
209
210 return 0;
211}
212
Matt Wagantall19ac4fd2012-02-03 20:18:23 -0800213static int pil_gss_reset(struct pil_desc *pil)
214{
215 struct gss_data *drv = dev_get_drvdata(pil->dev);
216 void __iomem *base = drv->base;
217 unsigned long start_addr = drv->start_addr;
218 int ret;
219
220 ret = make_gss_proxy_votes(pil->dev);
221 if (ret)
222 return ret;
223
224 /* Vote PLL on in GSS's voting register and wait for it to enable. */
225 writel_relaxed(PLL5_VOTE, PLL_ENA_GSS);
226 while ((readl_relaxed(PLL5_STATUS) & PLL_STATUS) == 0)
227 cpu_relax();
228
229 /* Perform GSS initialization. */
230 gss_init(drv);
231
232 /* Configure boot address and enable remap. */
233 writel_relaxed(REMAP_ENABLE | (start_addr >> 16),
234 base + GSS_CSR_BOOT_REMAP);
235
236 /* Power up A5 core. */
237 writel_relaxed(A5_POWER_ENA, base + GSS_CSR_POWER_UP_DOWN);
238 while (!(readl_relaxed(base + GSS_CSR_POWER_UP_DOWN) & A5_POWER_STATUS))
239 cpu_relax();
240
Matt Wagantall556d3f72012-02-16 17:36:29 -0800241 if (cpu_is_apq8064() &&
242 ((SOCINFO_VERSION_MAJOR(socinfo_get_version()) == 1) &&
243 (SOCINFO_VERSION_MINOR(socinfo_get_version()) == 0))) {
244 ret = smp_call_function_single(0, cfg_qgic2_bus_access, drv, 1);
245 if (ret) {
246 pr_err("Failed to configure QGIC2 bus access\n");
247 pil_gss_shutdown(pil);
248 return ret;
249 }
Matt Wagantall19ac4fd2012-02-03 20:18:23 -0800250 }
251
252 /* Release A5 from reset. */
253 writel_relaxed(0x0, base + GSS_CSR_RESET);
254
255 return 0;
256}
257
Matt Wagantall292aace2012-01-26 19:12:34 -0800258static struct pil_reset_ops pil_gss_ops = {
259 .init_image = pil_gss_init_image,
260 .verify_blob = nop_verify_blob,
261 .auth_and_reset = pil_gss_reset,
262 .shutdown = pil_gss_shutdown,
263};
264
Matt Wagantall11afeee2012-02-07 18:38:59 -0800265static int pil_gss_init_image_trusted(struct pil_desc *pil,
266 const u8 *metadata, size_t size)
267{
268 return pas_init_image(PAS_GSS, metadata, size);
269}
270
Matt Wagantall556d3f72012-02-16 17:36:29 -0800271static int pil_gss_shutdown_trusted(struct pil_desc *pil)
272{
273 struct gss_data *drv = dev_get_drvdata(pil->dev);
274 int ret;
275
276 ret = pas_shutdown(PAS_GSS);
277 if (ret)
278 return ret;
279
280 remove_gss_proxy_votes_now(drv);
281
282 return ret;
283}
284
Matt Wagantall11afeee2012-02-07 18:38:59 -0800285static int pil_gss_reset_trusted(struct pil_desc *pil)
286{
287 struct gss_data *drv = dev_get_drvdata(pil->dev);
288 int err;
289
290 err = make_gss_proxy_votes(pil->dev);
291 if (err)
292 return err;
293
294 err = pas_auth_and_reset(PAS_GSS);
295 if (err)
296 remove_gss_proxy_votes_now(drv);
297
Matt Wagantall556d3f72012-02-16 17:36:29 -0800298 if (cpu_is_apq8064() &&
299 ((SOCINFO_VERSION_MAJOR(socinfo_get_version()) == 1) &&
300 (SOCINFO_VERSION_MINOR(socinfo_get_version()) == 0))) {
301 err = smp_call_function_single(0, cfg_qgic2_bus_access, drv, 1);
302 if (err) {
303 pr_err("Failed to configure QGIC2 bus access\n");
304 pil_gss_shutdown_trusted(pil);
305 return err;
306 }
307 /*
308 * On 8064v1.0, pas_auth_and_reset() will not release the A5
309 * from reset. Linux must do this after cfg_qgic2_bus_access()
310 * is called on CPU0.
311 */
312 writel_relaxed(0x0, drv->base + GSS_CSR_RESET);
313 }
314
Matt Wagantall11afeee2012-02-07 18:38:59 -0800315 return err;
316}
317
Matt Wagantall11afeee2012-02-07 18:38:59 -0800318static struct pil_reset_ops pil_gss_ops_trusted = {
319 .init_image = pil_gss_init_image_trusted,
320 .verify_blob = nop_verify_blob,
321 .auth_and_reset = pil_gss_reset_trusted,
322 .shutdown = pil_gss_shutdown_trusted,
323};
324
Matt Wagantall292aace2012-01-26 19:12:34 -0800325static void configure_gss_pll(struct gss_data *drv)
326{
327 u32 regval, is_pll_enabled;
328
329 /* Check if PLL5 is enabled by FSM. */
330 is_pll_enabled = readl_relaxed(PLL5_STATUS) & PLL_STATUS;
331 if (!is_pll_enabled) {
332 /* Enable XO reference for PLL5 */
333 clk_prepare_enable(drv->xo);
334
335 /*
336 * Assert a vote to hold PLL5 on in RPM register until other
337 * voters are in place.
338 */
339 regval = readl_relaxed(PLL_ENA_RPM);
340 regval |= PLL5_VOTE;
341 writel_relaxed(regval, PLL_ENA_RPM);
342
343 /* Ref clk = 27MHz and program pll5 to 288MHz */
344 writel_relaxed(0xF, PLL5_L_VAL);
345 writel_relaxed(0x0, PLL5_M_VAL);
346 writel_relaxed(0x1, PLL5_N_VAL);
347
348 regval = readl_relaxed(PLL5_CONFIG);
349 /* Disable the MN accumulator and enable the main output. */
350 regval &= ~BIT(22);
351 regval |= BIT(23);
352
353 /* Set pre-divider and post-divider values to 1 and 1 */
354 regval &= ~BIT(19);
355 regval &= ~(BIT(21)|BIT(20));
356
357 /* Set VCO frequency */
358 regval &= ~(BIT(17)|BIT(16));
359 writel_relaxed(regval, PLL5_CONFIG);
360
361 regval = readl_relaxed(PLL5_MODE);
362 /* De-assert reset to FSM */
363 regval &= ~BIT(21);
364 writel_relaxed(regval, PLL5_MODE);
365
366 /* Program bias count */
367 regval &= ~(0x3F << 14);
368 regval |= (0x1 << 14);
369 writel_relaxed(regval, PLL5_MODE);
370
371 /* Program lock count */
372 regval &= ~(0x3F << 8);
373 regval |= (0x8 << 8);
374 writel_relaxed(regval, PLL5_MODE);
375
376 /* Enable PLL FSM voting */
377 regval |= BIT(20);
378 writel_relaxed(regval, PLL5_MODE);
379 }
380}
381
382static int __devinit pil_gss_probe(struct platform_device *pdev)
383{
384 struct gss_data *drv;
385 struct resource *res;
386 struct pil_desc *desc;
387 int ret;
388
389 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
390 if (!res)
391 return -EINVAL;
392
393 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
394 if (!drv)
395 return -ENOMEM;
396 platform_set_drvdata(pdev, drv);
397
398 drv->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
399 if (!drv->base)
400 return -ENOMEM;
401
402 desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
403 if (!desc)
404 return -ENOMEM;
405
Matt Wagantall19ac4fd2012-02-03 20:18:23 -0800406 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
407 if (!res)
408 return -EINVAL;
409
410 drv->qgic2_base = devm_ioremap(&pdev->dev, res->start,
411 resource_size(res));
412 if (!drv->qgic2_base)
413 return -ENOMEM;
414
Matt Wagantall292aace2012-01-26 19:12:34 -0800415 drv->xo = clk_get(&pdev->dev, "xo");
416 if (IS_ERR(drv->xo))
417 return PTR_ERR(drv->xo);
418
419 desc->name = "gss";
420 desc->dev = &pdev->dev;
421
Matt Wagantall11afeee2012-02-07 18:38:59 -0800422 if (pas_supported(PAS_GSS) > 0) {
423 desc->ops = &pil_gss_ops_trusted;
424 dev_info(&pdev->dev, "using secure boot\n");
425 } else {
426 desc->ops = &pil_gss_ops;
427 dev_info(&pdev->dev, "using non-secure boot\n");
428 }
Matt Wagantall292aace2012-01-26 19:12:34 -0800429
430 INIT_DELAYED_WORK(&drv->work, remove_gss_proxy_votes);
431
432 /* FIXME: Remove when PLL is configured by bootloaders. */
433 configure_gss_pll(drv);
434
435 ret = msm_pil_register(desc);
436 if (ret) {
437 flush_delayed_work_sync(&drv->work);
438 clk_put(drv->xo);
439 }
440 return ret;
441}
442
443static int __devexit pil_gss_remove(struct platform_device *pdev)
444{
445 struct gss_data *drv = platform_get_drvdata(pdev);
446 flush_delayed_work_sync(&drv->work);
447 clk_put(drv->xo);
448 return 0;
449}
450
451static struct platform_driver pil_gss_driver = {
452 .probe = pil_gss_probe,
453 .remove = __devexit_p(pil_gss_remove),
454 .driver = {
455 .name = "pil_gss",
456 .owner = THIS_MODULE,
457 },
458};
459
460static int __init pil_gss_init(void)
461{
462 return platform_driver_register(&pil_gss_driver);
463}
464module_init(pil_gss_init);
465
466static void __exit pil_gss_exit(void)
467{
468 platform_driver_unregister(&pil_gss_driver);
469}
470module_exit(pil_gss_exit);
471
472MODULE_DESCRIPTION("Support for booting the GSS processor");
473MODULE_LICENSE("GPL v2");