blob: 28b9deed97ad799844f1b36d5f07230e01fb5ec2 [file] [log] [blame]
Stephen Boyd4f8b7e22012-01-24 13:31:29 -08001/* Copyright (c) 2010-2012, Code Aurora Forum. All rights reserved.
Stephen Boyd3acc9e42011-09-28 16:46:40 -07002 *
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 Boyd3acc9e42011-09-28 16:46:40 -070063struct q6v3_data {
64 void __iomem *base;
65 unsigned long start_addr;
Stephen Boyd6d67d252011-09-27 11:50:05 -070066 struct pil_device *pil;
Stephen Boyd3acc9e42011-09-28 16:46:40 -070067 struct clk *pll;
Stephen Boyd3acc9e42011-09-28 16:46:40 -070068};
69
Stephen Boyd3acc9e42011-09-28 16:46:40 -070070static 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 Boyd32f7f2e2012-03-22 10:59:22 -070079static void pil_q6v3_remove_proxy_votes(struct pil_desc *pil)
Stephen Boyd3acc9e42011-09-28 16:46:40 -070080{
Stephen Boyd32f7f2e2012-03-22 10:59:22 -070081 struct q6v3_data *drv = dev_get_drvdata(pil->dev);
Stephen Boyd4f8b7e22012-01-24 13:31:29 -080082 clk_disable_unprepare(drv->pll);
Stephen Boyd3acc9e42011-09-28 16:46:40 -070083}
84
Stephen Boyd32f7f2e2012-03-22 10:59:22 -070085static int pil_q6v3_make_proxy_votes(struct pil_desc *pil)
Stephen Boyd3acc9e42011-09-28 16:46:40 -070086{
87 int ret;
Stephen Boyd32f7f2e2012-03-22 10:59:22 -070088 struct q6v3_data *drv = dev_get_drvdata(pil->dev);
Stephen Boyd3acc9e42011-09-28 16:46:40 -070089
Stephen Boyd4f8b7e22012-01-24 13:31:29 -080090 ret = clk_prepare_enable(drv->pll);
91 if (ret) {
Stephen Boyd32f7f2e2012-03-22 10:59:22 -070092 dev_err(pil->dev, "Failed to enable PLL\n");
Stephen Boyd4f8b7e22012-01-24 13:31:29 -080093 return ret;
94 }
Stephen Boyd4f8b7e22012-01-24 13:31:29 -080095 return 0;
Stephen Boyd3acc9e42011-09-28 16:46:40 -070096}
97
Stephen Boyd3acc9e42011-09-28 16:46:40 -070098static int pil_q6v3_reset(struct pil_desc *pil)
99{
100 u32 reg;
101 struct q6v3_data *drv = dev_get_drvdata(pil->dev);
102
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700103 /* 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
144static int pil_q6v3_shutdown(struct pil_desc *pil)
145{
146 u32 reg;
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700147
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 Boyd3acc9e42011-09-28 16:46:40 -0700166 return 0;
167}
168
169static struct pil_reset_ops pil_q6v3_ops = {
170 .init_image = pil_q6v3_init_image,
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700171 .auth_and_reset = pil_q6v3_reset,
172 .shutdown = pil_q6v3_shutdown,
Stephen Boyd32f7f2e2012-03-22 10:59:22 -0700173 .proxy_vote = pil_q6v3_make_proxy_votes,
174 .proxy_unvote = pil_q6v3_remove_proxy_votes,
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700175};
176
177static 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
183static int pil_q6v3_reset_trusted(struct pil_desc *pil)
184{
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700185 return pas_auth_and_reset(PAS_Q6);
186}
187
188static int pil_q6v3_shutdown_trusted(struct pil_desc *pil)
189{
Stephen Boyd32f7f2e2012-03-22 10:59:22 -0700190 return pas_shutdown(PAS_Q6);
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700191}
192
193static struct pil_reset_ops pil_q6v3_ops_trusted = {
194 .init_image = pil_q6v3_init_image_trusted,
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700195 .auth_and_reset = pil_q6v3_reset_trusted,
196 .shutdown = pil_q6v3_shutdown_trusted,
Stephen Boyd32f7f2e2012-03-22 10:59:22 -0700197 .proxy_vote = pil_q6v3_make_proxy_votes,
198 .proxy_unvote = pil_q6v3_remove_proxy_votes,
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700199};
200
201static 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 Boydf11bfb52012-03-23 15:30:48 -0700224 drv->pll = devm_clk_get(&pdev->dev, "pll4");
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700225 if (IS_ERR(drv->pll))
226 return PTR_ERR(drv->pll);
227
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700228 desc->name = "q6";
229 desc->dev = &pdev->dev;
Stephen Boyd6d67d252011-09-27 11:50:05 -0700230 desc->owner = THIS_MODULE;
Stephen Boyd32f7f2e2012-03-22 10:59:22 -0700231 desc->proxy_timeout = 10000;
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700232
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 Boyd6d67d252011-09-27 11:50:05 -0700241 drv->pil = msm_pil_register(desc);
242 if (IS_ERR(drv->pil)) {
Stephen Boyd6d67d252011-09-27 11:50:05 -0700243 return PTR_ERR(drv->pil);
Stephen Boyd4f8b7e22012-01-24 13:31:29 -0800244 }
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700245 return 0;
246}
247
248static int __devexit pil_q6v3_driver_exit(struct platform_device *pdev)
249{
250 struct q6v3_data *drv = platform_get_drvdata(pdev);
Stephen Boyd6d67d252011-09-27 11:50:05 -0700251 msm_pil_unregister(drv->pil);
Stephen Boyd3acc9e42011-09-28 16:46:40 -0700252 return 0;
253}
254
255static 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
264static int __init pil_q6v3_init(void)
265{
266 return platform_driver_register(&pil_q6v3_driver);
267}
268module_init(pil_q6v3_init);
269
270static void __exit pil_q6v3_exit(void)
271{
272 platform_driver_unregister(&pil_q6v3_driver);
273}
274module_exit(pil_q6v3_exit);
275
276MODULE_DESCRIPTION("Support for booting QDSP6v3 (Hexagon) processors");
277MODULE_LICENSE("GPL v2");