blob: 70a12defbf86f27b44e667ea1df566903d90d0bc [file] [log] [blame]
Matt Wagantallb3fe8992011-12-07 19:26:55 -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/init.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/io.h>
Matt Wagantallb7747992012-05-11 19:37:51 -070018#include <linux/iopoll.h>
Matt Wagantallb3fe8992011-12-07 19:26:55 -080019#include <linux/elf.h>
20#include <linux/err.h>
21#include <linux/of.h>
22#include <linux/clk.h>
Matt Wagantalld41ce772012-05-10 23:16:41 -070023#include <mach/clk.h>
Matt Wagantallb3fe8992011-12-07 19:26:55 -080024#include "peripheral-loader.h"
25#include "pil-q6v5.h"
26
Matt Wagantallb7747992012-05-11 19:37:51 -070027/* QDSP6SS Register Offsets */
Matt Wagantallb3fe8992011-12-07 19:26:55 -080028#define QDSP6SS_RESET 0x014
29#define QDSP6SS_GFMUX_CTL 0x020
30#define QDSP6SS_PWR_CTL 0x030
Matt Wagantallb76f1902012-08-09 14:51:28 -070031#define QDSP6SS_CGC_OVERRIDE 0x034
Matt Wagantallb3fe8992011-12-07 19:26:55 -080032
Matt Wagantallb7747992012-05-11 19:37:51 -070033/* AXI Halt Register Offsets */
34#define AXI_HALTREQ 0x0
35#define AXI_HALTACK 0x4
36#define AXI_IDLE 0x8
37
38#define HALT_ACK_TIMEOUT_US 100000
39
Matt Wagantallb3fe8992011-12-07 19:26:55 -080040/* QDSP6SS_RESET */
Matt Wagantall11c07e22012-08-09 16:14:07 -070041#define Q6SS_STOP_CORE BIT(0)
Matt Wagantallb3fe8992011-12-07 19:26:55 -080042#define Q6SS_CORE_ARES BIT(1)
Matt Wagantall11c07e22012-08-09 16:14:07 -070043#define Q6SS_BUS_ARES_ENA BIT(2)
Matt Wagantallb3fe8992011-12-07 19:26:55 -080044
45/* QDSP6SS_GFMUX_CTL */
46#define Q6SS_CLK_ENA BIT(1)
47
48/* QDSP6SS_PWR_CTL */
Matt Wagantall11c07e22012-08-09 16:14:07 -070049#define Q6SS_L2DATA_SLP_NRET_N (BIT(0)|BIT(1)|BIT(2))
Matt Wagantallb3fe8992011-12-07 19:26:55 -080050#define Q6SS_L2TAG_SLP_NRET_N BIT(16)
51#define Q6SS_ETB_SLP_NRET_N BIT(17)
52#define Q6SS_L2DATA_STBY_N BIT(18)
53#define Q6SS_SLP_RET_N BIT(19)
54#define Q6SS_CLAMP_IO BIT(20)
55#define QDSS_BHS_ON BIT(21)
56
Matt Wagantallb76f1902012-08-09 14:51:28 -070057/* QDSP6SS_CGC_OVERRIDE */
58#define Q6SS_CORE_CLK_EN BIT(0)
59#define Q6SS_CORE_RCLK_EN BIT(1)
60
Matt Wagantallb3fe8992011-12-07 19:26:55 -080061int pil_q6v5_make_proxy_votes(struct pil_desc *pil)
62{
63 int ret;
Stephen Boyd3826cd42012-07-05 17:37:53 -070064 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
Matt Wagantallb3fe8992011-12-07 19:26:55 -080065
66 ret = clk_prepare_enable(drv->xo);
67 if (ret) {
68 dev_err(pil->dev, "Failed to enable XO\n");
69 return ret;
70 }
71 return 0;
72}
73EXPORT_SYMBOL(pil_q6v5_make_proxy_votes);
74
75void pil_q6v5_remove_proxy_votes(struct pil_desc *pil)
76{
Stephen Boyd3826cd42012-07-05 17:37:53 -070077 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
Matt Wagantallb3fe8992011-12-07 19:26:55 -080078 clk_disable_unprepare(drv->xo);
79}
80EXPORT_SYMBOL(pil_q6v5_remove_proxy_votes);
81
Matt Wagantallb7747992012-05-11 19:37:51 -070082void pil_q6v5_halt_axi_port(struct pil_desc *pil, void __iomem *halt_base)
83{
84 int ret;
85 u32 status;
86
87 /* Assert halt request */
88 writel_relaxed(1, halt_base + AXI_HALTREQ);
89
90 /* Wait for halt */
91 ret = readl_poll_timeout(halt_base + AXI_HALTACK,
92 status, status != 0, 50, HALT_ACK_TIMEOUT_US);
93 if (ret)
94 dev_warn(pil->dev, "Port %p halt timeout\n", halt_base);
95 else if (!readl_relaxed(halt_base + AXI_IDLE))
96 dev_warn(pil->dev, "Port %p halt failed\n", halt_base);
97
98 /* Clear halt request (port will remain halted until reset) */
99 writel_relaxed(0, halt_base + AXI_HALTREQ);
100}
101EXPORT_SYMBOL(pil_q6v5_halt_axi_port);
102
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800103int pil_q6v5_init_image(struct pil_desc *pil, const u8 *metadata,
104 size_t size)
105{
106 const struct elf32_hdr *ehdr = (struct elf32_hdr *)metadata;
Stephen Boyd3826cd42012-07-05 17:37:53 -0700107 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800108 drv->start_addr = ehdr->e_entry;
109 return 0;
110}
111EXPORT_SYMBOL(pil_q6v5_init_image);
112
113void pil_q6v5_shutdown(struct pil_desc *pil)
114{
115 u32 val;
Stephen Boyd3826cd42012-07-05 17:37:53 -0700116 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800117
118 /* Turn off core clock */
119 val = readl_relaxed(drv->reg_base + QDSP6SS_GFMUX_CTL);
120 val &= ~Q6SS_CLK_ENA;
121 writel_relaxed(val, drv->reg_base + QDSP6SS_GFMUX_CTL);
122
123 /* Clamp IO */
124 val = readl_relaxed(drv->reg_base + QDSP6SS_PWR_CTL);
125 val |= Q6SS_CLAMP_IO;
126 writel_relaxed(val, drv->reg_base + QDSP6SS_PWR_CTL);
127
128 /* Turn off Q6 memories */
129 val &= ~(Q6SS_L2DATA_SLP_NRET_N | Q6SS_SLP_RET_N |
130 Q6SS_L2TAG_SLP_NRET_N | Q6SS_ETB_SLP_NRET_N |
131 Q6SS_L2DATA_STBY_N);
132 writel_relaxed(Q6SS_CLAMP_IO, drv->reg_base + QDSP6SS_PWR_CTL);
133
134 /* Assert Q6 resets */
135 val = readl_relaxed(drv->reg_base + QDSP6SS_RESET);
Matt Wagantall11c07e22012-08-09 16:14:07 -0700136 val = (Q6SS_CORE_ARES | Q6SS_BUS_ARES_ENA);
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800137 writel_relaxed(val, drv->reg_base + QDSP6SS_RESET);
138
139 /* Kill power at block headswitch (affects LPASS only) */
140 val = readl_relaxed(drv->reg_base + QDSP6SS_PWR_CTL);
141 val &= ~QDSS_BHS_ON;
142 writel_relaxed(val, drv->reg_base + QDSP6SS_PWR_CTL);
143}
144EXPORT_SYMBOL(pil_q6v5_shutdown);
145
146int pil_q6v5_reset(struct pil_desc *pil)
147{
Stephen Boyd3826cd42012-07-05 17:37:53 -0700148 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800149 u32 val;
150
151 /* Assert resets, stop core */
152 val = readl_relaxed(drv->reg_base + QDSP6SS_RESET);
Matt Wagantall11c07e22012-08-09 16:14:07 -0700153 val |= (Q6SS_CORE_ARES | Q6SS_BUS_ARES_ENA | Q6SS_STOP_CORE);
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800154 writel_relaxed(val, drv->reg_base + QDSP6SS_RESET);
155
156 /* Enable power block headswitch (only affects LPASS) */
157 val = readl_relaxed(drv->reg_base + QDSP6SS_PWR_CTL);
158 val |= QDSS_BHS_ON;
159 writel_relaxed(val, drv->reg_base + QDSP6SS_PWR_CTL);
160
161 /* Turn on memories */
162 val = readl_relaxed(drv->reg_base + QDSP6SS_PWR_CTL);
163 val |= Q6SS_L2DATA_SLP_NRET_N | Q6SS_SLP_RET_N |
164 Q6SS_L2TAG_SLP_NRET_N | Q6SS_ETB_SLP_NRET_N |
165 Q6SS_L2DATA_STBY_N;
166 writel_relaxed(val, drv->reg_base + QDSP6SS_PWR_CTL);
167
168 /* Remove IO clamp */
169 val &= ~Q6SS_CLAMP_IO;
170 writel_relaxed(val, drv->reg_base + QDSP6SS_PWR_CTL);
171
172 /* Bring core out of reset */
Matt Wagantall11c07e22012-08-09 16:14:07 -0700173 val = readl_relaxed(drv->reg_base + QDSP6SS_RESET);
174 val &= ~Q6SS_CORE_ARES;
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800175 writel_relaxed(val, drv->reg_base + QDSP6SS_RESET);
176
Matt Wagantallb76f1902012-08-09 14:51:28 -0700177 /* Disable clock gating for core and rclk */
178 val = readl_relaxed(drv->reg_base + QDSP6SS_CGC_OVERRIDE);
179 val |= Q6SS_CORE_RCLK_EN | Q6SS_CORE_CLK_EN;
180 writel_relaxed(val, drv->reg_base + QDSP6SS_CGC_OVERRIDE);
181
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800182 /* Turn on core clock */
183 val = readl_relaxed(drv->reg_base + QDSP6SS_GFMUX_CTL);
184 val |= Q6SS_CLK_ENA;
185 writel_relaxed(val, drv->reg_base + QDSP6SS_GFMUX_CTL);
186
187 /* Start core execution */
188 val = readl_relaxed(drv->reg_base + QDSP6SS_RESET);
189 val &= ~Q6SS_STOP_CORE;
190 writel_relaxed(val, drv->reg_base + QDSP6SS_RESET);
191
192 return 0;
193}
194EXPORT_SYMBOL(pil_q6v5_reset);
195
Stephen Boyd3826cd42012-07-05 17:37:53 -0700196struct q6v5_data __devinit *pil_q6v5_init(struct platform_device *pdev)
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800197{
198 struct q6v5_data *drv;
199 struct resource *res;
200 struct pil_desc *desc;
201 int ret;
202
203 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
204 if (!drv)
205 return ERR_PTR(-ENOMEM);
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800206
Matt Wagantall1f168152012-09-25 13:26:47 -0700207 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qdsp6_base");
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800208 if (!res)
209 return ERR_PTR(-EINVAL);
210 drv->reg_base = devm_ioremap(&pdev->dev, res->start,
211 resource_size(res));
212 if (!drv->reg_base)
213 return ERR_PTR(-ENOMEM);
Matt Wagantall1f168152012-09-25 13:26:47 -0700214 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "halt_base");
Matt Wagantallb7747992012-05-11 19:37:51 -0700215 drv->axi_halt_base = devm_ioremap(&pdev->dev, res->start,
216 resource_size(res));
217 if (!drv->axi_halt_base)
218 return ERR_PTR(-ENOMEM);
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800219
Stephen Boyd633eb622012-06-13 12:05:35 -0700220 desc = &drv->desc;
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800221 ret = of_property_read_string(pdev->dev.of_node, "qcom,firmware-name",
222 &desc->name);
223 if (ret)
224 return ERR_PTR(ret);
225
226 drv->xo = devm_clk_get(&pdev->dev, "xo");
227 if (IS_ERR(drv->xo))
228 return ERR_CAST(drv->xo);
229
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800230 desc->dev = &pdev->dev;
231
Stephen Boyd3826cd42012-07-05 17:37:53 -0700232 return drv;
Matt Wagantallb3fe8992011-12-07 19:26:55 -0800233}
234EXPORT_SYMBOL(pil_q6v5_init);