blob: 6848c597d779be283e75e346a2181b5c69d9c6b5 [file] [log] [blame]
Stephen Boyd12332572011-12-06 16:00:51 -08001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Stephen Boyd322a9922011-09-20 01:05:54 -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/kernel.h>
14#include <linux/err.h>
15#include <linux/io.h>
16#include <linux/elf.h>
17#include <linux/delay.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/platform_device.h>
21
22#include <mach/msm_iomap.h>
Matt Wagantall04b7cc72011-12-09 18:52:26 -080023#include <mach/msm_xo.h>
Stephen Boyd322a9922011-09-20 01:05:54 -070024
25#include "peripheral-loader.h"
26#include "scm-pas.h"
27
Matt Wagantall04b7cc72011-12-09 18:52:26 -080028#define PROXY_VOTE_TIMEOUT 10000
29
Stephen Boyd322a9922011-09-20 01:05:54 -070030#define RIVA_PMU_A2XB_CFG 0xB8
31#define RIVA_PMU_A2XB_CFG_EN BIT(0)
32
33#define RIVA_PMU_CFG 0x28
34#define RIVA_PMU_CFG_WARM_BOOT BIT(0)
35#define RIVA_PMU_CFG_IRIS_XO_MODE 0x6
36#define RIVA_PMU_CFG_IRIS_XO_MODE_48 (3 << 1)
37
Stephen Boyd12332572011-12-06 16:00:51 -080038#define RIVA_PMU_OVRD_EN 0x2C
39#define RIVA_PMU_OVRD_EN_CCPU_RESET BIT(0)
40#define RIVA_PMU_OVRD_EN_CCPU_CLK BIT(1)
41
Stephen Boyd322a9922011-09-20 01:05:54 -070042#define RIVA_PMU_OVRD_VAL 0x30
43#define RIVA_PMU_OVRD_VAL_CCPU_RESET BIT(0)
44#define RIVA_PMU_OVRD_VAL_CCPU_CLK BIT(1)
45
46#define RIVA_PMU_CCPU_CTL 0x9C
47#define RIVA_PMU_CCPU_CTL_HIGH_IVT BIT(0)
48#define RIVA_PMU_CCPU_CTL_REMAP_EN BIT(2)
49
50#define RIVA_PMU_CCPU_BOOT_REMAP_ADDR 0xA0
51
52#define RIVA_PLL_MODE (MSM_CLK_CTL_BASE + 0x31A0)
53#define PLL_MODE_OUTCTRL BIT(0)
54#define PLL_MODE_BYPASSNL BIT(1)
55#define PLL_MODE_RESET_N BIT(2)
56#define PLL_MODE_REF_XO_SEL 0x30
57#define PLL_MODE_REF_XO_SEL_CXO (2 << 4)
58#define PLL_MODE_REF_XO_SEL_RF (3 << 4)
59#define RIVA_PLL_L_VAL (MSM_CLK_CTL_BASE + 0x31A4)
60#define RIVA_PLL_M_VAL (MSM_CLK_CTL_BASE + 0x31A8)
61#define RIVA_PLL_N_VAL (MSM_CLK_CTL_BASE + 0x31Ac)
62#define RIVA_PLL_CONFIG (MSM_CLK_CTL_BASE + 0x31B4)
63#define RIVA_PLL_STATUS (MSM_CLK_CTL_BASE + 0x31B8)
Stephen Boyd12332572011-12-06 16:00:51 -080064#define RIVA_RESET (MSM_CLK_CTL_BASE + 0x35E0)
Stephen Boyd322a9922011-09-20 01:05:54 -070065
66#define RIVA_PMU_ROOT_CLK_SEL 0xC8
67#define RIVA_PMU_ROOT_CLK_SEL_3 BIT(2)
68
69#define RIVA_PMU_CLK_ROOT3 0x78
70#define RIVA_PMU_CLK_ROOT3_ENA BIT(0)
71#define RIVA_PMU_CLK_ROOT3_SRC0_DIV 0x3C
72#define RIVA_PMU_CLK_ROOT3_SRC0_DIV_2 (1 << 2)
73#define RIVA_PMU_CLK_ROOT3_SRC0_SEL 0x1C0
74#define RIVA_PMU_CLK_ROOT3_SRC0_SEL_RIVA (1 << 6)
75#define RIVA_PMU_CLK_ROOT3_SRC1_DIV 0x1E00
76#define RIVA_PMU_CLK_ROOT3_SRC1_DIV_2 (1 << 9)
77#define RIVA_PMU_CLK_ROOT3_SRC1_SEL 0xE000
78#define RIVA_PMU_CLK_ROOT3_SRC1_SEL_RIVA (1 << 13)
79
80struct riva_data {
81 void __iomem *base;
82 unsigned long start_addr;
Matt Wagantall04b7cc72011-12-09 18:52:26 -080083 struct msm_xo_voter *xo;
84 struct timer_list xo_timer;
Stephen Boyd322a9922011-09-20 01:05:54 -070085};
86
Matt Wagantall04b7cc72011-12-09 18:52:26 -080087static void pil_riva_make_xo_proxy_votes(struct device *dev)
88{
89 struct riva_data *drv = dev_get_drvdata(dev);
90
91 msm_xo_mode_vote(drv->xo, MSM_XO_MODE_ON);
92 mod_timer(&drv->xo_timer, jiffies+msecs_to_jiffies(PROXY_VOTE_TIMEOUT));
93}
94
95static void pil_riva_remove_xo_proxy_votes(unsigned long data)
96{
97 struct riva_data *drv = (struct riva_data *)data;
98
99 msm_xo_mode_vote(drv->xo, MSM_XO_MODE_OFF);
100}
101
102static void pil_riva_remove_xo_proxy_votes_now(struct device *dev)
103{
104 struct riva_data *drv = dev_get_drvdata(dev);
105
106 if (del_timer(&drv->xo_timer))
107 pil_riva_remove_xo_proxy_votes((unsigned long)drv);
108}
109
110static bool cxo_is_needed(struct riva_data *drv)
111{
112 u32 reg = readl_relaxed(drv->base + RIVA_PMU_CFG);
113 return (reg & RIVA_PMU_CFG_IRIS_XO_MODE)
114 != RIVA_PMU_CFG_IRIS_XO_MODE_48;
115}
116
Stephen Boyd322a9922011-09-20 01:05:54 -0700117static int nop_verify_blob(struct pil_desc *pil, u32 phy_addr, size_t size)
118{
119 return 0;
120}
121
122static int pil_riva_init_image(struct pil_desc *pil, const u8 *metadata,
123 size_t size)
124{
125 const struct elf32_hdr *ehdr = (struct elf32_hdr *)metadata;
126 struct riva_data *drv = dev_get_drvdata(pil->dev);
127 drv->start_addr = ehdr->e_entry;
128 return 0;
129}
130
131static int pil_riva_reset(struct pil_desc *pil)
132{
133 u32 reg, sel;
Matt Wagantall04b7cc72011-12-09 18:52:26 -0800134 bool use_cxo;
Stephen Boyd322a9922011-09-20 01:05:54 -0700135 struct riva_data *drv = dev_get_drvdata(pil->dev);
136 void __iomem *base = drv->base;
137 unsigned long start_addr = drv->start_addr;
138
139 /* Enable A2XB bridge */
140 reg = readl_relaxed(base + RIVA_PMU_A2XB_CFG);
141 reg |= RIVA_PMU_A2XB_CFG_EN;
142 writel_relaxed(reg, base + RIVA_PMU_A2XB_CFG);
143
Matt Wagantall04b7cc72011-12-09 18:52:26 -0800144 /* Proxy-vote for CXO if it's needed */
145 use_cxo = cxo_is_needed(drv);
146 if (use_cxo)
147 pil_riva_make_xo_proxy_votes(pil->dev);
Stephen Boyd322a9922011-09-20 01:05:54 -0700148
149 /* Program PLL 13 to 960 MHz */
150 reg = readl_relaxed(RIVA_PLL_MODE);
151 reg &= ~(PLL_MODE_BYPASSNL | PLL_MODE_OUTCTRL | PLL_MODE_RESET_N);
152 writel_relaxed(reg, RIVA_PLL_MODE);
153
Matt Wagantall04b7cc72011-12-09 18:52:26 -0800154 if (use_cxo)
Stephen Boyd322a9922011-09-20 01:05:54 -0700155 writel_relaxed(0x40000C00 | 50, RIVA_PLL_L_VAL);
Matt Wagantall04b7cc72011-12-09 18:52:26 -0800156 else
157 writel_relaxed(0x40000C00 | 40, RIVA_PLL_L_VAL);
Stephen Boyd322a9922011-09-20 01:05:54 -0700158 writel_relaxed(0, RIVA_PLL_M_VAL);
159 writel_relaxed(1, RIVA_PLL_N_VAL);
160 writel_relaxed(0x01495227, RIVA_PLL_CONFIG);
161
162 reg = readl_relaxed(RIVA_PLL_MODE);
163 reg &= ~(PLL_MODE_REF_XO_SEL);
Matt Wagantall04b7cc72011-12-09 18:52:26 -0800164 reg |= use_cxo ? PLL_MODE_REF_XO_SEL_CXO : PLL_MODE_REF_XO_SEL_RF;
Stephen Boyd322a9922011-09-20 01:05:54 -0700165 writel_relaxed(reg, RIVA_PLL_MODE);
166
167 /* Enable PLL 13 */
168 reg |= PLL_MODE_BYPASSNL;
169 writel_relaxed(reg, RIVA_PLL_MODE);
170
171 /*
172 * H/W requires a 5us delay between disabling the bypass and
173 * de-asserting the reset. Delay 10us just to be safe.
174 */
175 mb();
176 usleep_range(10, 20);
177
178 reg |= PLL_MODE_RESET_N;
179 writel_relaxed(reg, RIVA_PLL_MODE);
180 reg |= PLL_MODE_OUTCTRL;
181 writel_relaxed(reg, RIVA_PLL_MODE);
182
183 /* Wait for PLL to settle */
184 mb();
185 usleep_range(50, 100);
186
187 /* Configure cCPU for 240 MHz */
188 sel = readl_relaxed(base + RIVA_PMU_ROOT_CLK_SEL);
189 reg = readl_relaxed(base + RIVA_PMU_CLK_ROOT3);
190 if (sel & RIVA_PMU_ROOT_CLK_SEL_3) {
191 reg &= ~(RIVA_PMU_CLK_ROOT3_SRC0_SEL |
192 RIVA_PMU_CLK_ROOT3_SRC0_DIV);
193 reg |= RIVA_PMU_CLK_ROOT3_SRC0_SEL_RIVA |
194 RIVA_PMU_CLK_ROOT3_SRC0_DIV_2;
195 } else {
196 reg &= ~(RIVA_PMU_CLK_ROOT3_SRC1_SEL |
197 RIVA_PMU_CLK_ROOT3_SRC1_DIV);
198 reg |= RIVA_PMU_CLK_ROOT3_SRC1_SEL_RIVA |
199 RIVA_PMU_CLK_ROOT3_SRC1_DIV_2;
200 }
201 writel_relaxed(reg, base + RIVA_PMU_CLK_ROOT3);
202 reg |= RIVA_PMU_CLK_ROOT3_ENA;
203 writel_relaxed(reg, base + RIVA_PMU_CLK_ROOT3);
204 reg = readl_relaxed(base + RIVA_PMU_ROOT_CLK_SEL);
205 reg ^= RIVA_PMU_ROOT_CLK_SEL_3;
206 writel_relaxed(reg, base + RIVA_PMU_ROOT_CLK_SEL);
207
208 /* Use the high vector table */
209 reg = readl_relaxed(base + RIVA_PMU_CCPU_CTL);
210 reg |= RIVA_PMU_CCPU_CTL_HIGH_IVT | RIVA_PMU_CCPU_CTL_REMAP_EN;
211 writel_relaxed(reg, base + RIVA_PMU_CCPU_CTL);
212
213 /* Set base memory address */
214 writel_relaxed(start_addr >> 16, base + RIVA_PMU_CCPU_BOOT_REMAP_ADDR);
215
216 /* Clear warmboot bit indicating this is a cold boot */
217 reg = readl_relaxed(base + RIVA_PMU_CFG);
218 reg &= ~(RIVA_PMU_CFG_WARM_BOOT);
219 writel_relaxed(reg, base + RIVA_PMU_CFG);
220
221 /* Enable the cCPU clock */
222 reg = readl_relaxed(base + RIVA_PMU_OVRD_VAL);
223 reg |= RIVA_PMU_OVRD_VAL_CCPU_CLK;
224 writel_relaxed(reg, base + RIVA_PMU_OVRD_VAL);
225
226 /* Take cCPU out of reset */
227 reg |= RIVA_PMU_OVRD_VAL_CCPU_RESET;
228 writel_relaxed(reg, base + RIVA_PMU_OVRD_VAL);
229
230 return 0;
231}
232
233static int pil_riva_shutdown(struct pil_desc *pil)
234{
235 struct riva_data *drv = dev_get_drvdata(pil->dev);
236 u32 reg;
237
Stephen Boyd12332572011-12-06 16:00:51 -0800238 /* Put cCPU and cCPU clock into reset */
Stephen Boyd322a9922011-09-20 01:05:54 -0700239 reg = readl_relaxed(drv->base + RIVA_PMU_OVRD_VAL);
240 reg &= ~(RIVA_PMU_OVRD_VAL_CCPU_RESET | RIVA_PMU_OVRD_VAL_CCPU_CLK);
241 writel_relaxed(reg, drv->base + RIVA_PMU_OVRD_VAL);
Stephen Boyd12332572011-12-06 16:00:51 -0800242 reg = readl_relaxed(drv->base + RIVA_PMU_OVRD_EN);
243 reg |= RIVA_PMU_OVRD_EN_CCPU_RESET | RIVA_PMU_OVRD_EN_CCPU_CLK;
244 writel_relaxed(reg, drv->base + RIVA_PMU_OVRD_EN);
245 mb();
246
247 /* Assert reset to Riva */
248 writel_relaxed(1, RIVA_RESET);
249 mb();
250 usleep_range(1000, 2000);
251
252 /* Deassert reset to Riva */
253 writel_relaxed(0, RIVA_RESET);
254 mb();
Stephen Boyd322a9922011-09-20 01:05:54 -0700255
Matt Wagantall04b7cc72011-12-09 18:52:26 -0800256 pil_riva_remove_xo_proxy_votes_now(pil->dev);
257
Stephen Boyd322a9922011-09-20 01:05:54 -0700258 return 0;
259}
260
261static struct pil_reset_ops pil_riva_ops = {
262 .init_image = pil_riva_init_image,
263 .verify_blob = nop_verify_blob,
264 .auth_and_reset = pil_riva_reset,
265 .shutdown = pil_riva_shutdown,
266};
267
268static int pil_riva_init_image_trusted(struct pil_desc *pil,
269 const u8 *metadata, size_t size)
270{
271 return pas_init_image(PAS_RIVA, metadata, size);
272}
273
274static int pil_riva_reset_trusted(struct pil_desc *pil)
275{
Matt Wagantall04b7cc72011-12-09 18:52:26 -0800276 struct riva_data *drv = dev_get_drvdata(pil->dev);
277
278 /* Proxy-vote for CXO if it's needed */
279 if (cxo_is_needed(drv))
280 pil_riva_make_xo_proxy_votes(pil->dev);
281
Stephen Boyd322a9922011-09-20 01:05:54 -0700282 return pas_auth_and_reset(PAS_RIVA);
283}
284
285static int pil_riva_shutdown_trusted(struct pil_desc *pil)
286{
Matt Wagantall04b7cc72011-12-09 18:52:26 -0800287 int ret = pas_shutdown(PAS_RIVA);
288
289 pil_riva_remove_xo_proxy_votes_now(pil->dev);
290
291 return ret;
Stephen Boyd322a9922011-09-20 01:05:54 -0700292}
293
294static struct pil_reset_ops pil_riva_ops_trusted = {
295 .init_image = pil_riva_init_image_trusted,
296 .verify_blob = nop_verify_blob,
297 .auth_and_reset = pil_riva_reset_trusted,
298 .shutdown = pil_riva_shutdown_trusted,
299};
300
301static int __devinit pil_riva_probe(struct platform_device *pdev)
302{
303 struct riva_data *drv;
304 struct resource *res;
305 struct pil_desc *desc;
306
307 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
308 if (!res)
309 return -EINVAL;
310
311 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
312 if (!drv)
313 return -ENOMEM;
314 platform_set_drvdata(pdev, drv);
315
316 drv->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
317 if (!drv->base)
318 return -ENOMEM;
319
320 desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
321 if (!desc)
322 return -ENOMEM;
323
324 desc->name = "wcnss";
325 desc->dev = &pdev->dev;
326
327 if (pas_supported(PAS_RIVA) > 0) {
328 desc->ops = &pil_riva_ops_trusted;
329 dev_info(&pdev->dev, "using secure boot\n");
330 } else {
331 desc->ops = &pil_riva_ops;
332 dev_info(&pdev->dev, "using non-secure boot\n");
333 }
Matt Wagantall04b7cc72011-12-09 18:52:26 -0800334
335 setup_timer(&drv->xo_timer, pil_riva_remove_xo_proxy_votes,
336 (unsigned long)drv);
337 drv->xo = msm_xo_get(MSM_XO_CXO, desc->name);
338 if (IS_ERR(drv->xo))
339 return PTR_ERR(drv->xo);
340
Stephen Boyd322a9922011-09-20 01:05:54 -0700341 return msm_pil_register(desc);
342}
343
344static int __devexit pil_riva_remove(struct platform_device *pdev)
345{
346 return 0;
347}
348
349static struct platform_driver pil_riva_driver = {
350 .probe = pil_riva_probe,
351 .remove = __devexit_p(pil_riva_remove),
352 .driver = {
353 .name = "pil_riva",
354 .owner = THIS_MODULE,
355 },
356};
357
358static int __init pil_riva_init(void)
359{
360 return platform_driver_register(&pil_riva_driver);
361}
362module_init(pil_riva_init);
363
364static void __exit pil_riva_exit(void)
365{
366 platform_driver_unregister(&pil_riva_driver);
367}
368module_exit(pil_riva_exit);
369
370MODULE_DESCRIPTION("Support for booting RIVA (WCNSS) processors");
371MODULE_LICENSE("GPL v2");