blob: f6b2b4541f313c40ba7309238079de212eeaaebc [file] [log] [blame]
Pankaj Kumar3912c982011-12-07 16:59:03 +05301/*
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
15#include <linux/kernel.h>
16#include <linux/delay.h>
17#include <linux/err.h>
18#include <linux/remote_spinlock.h>
19
20#include <mach/socinfo.h>
21#include <mach/msm_iomap.h>
22
23#include "clock.h"
24#include "clock-pll.h"
25#include "smd_private.h"
26
27struct pll_rate {
28 unsigned int lvalue;
29 unsigned long rate;
30};
31
32static struct pll_rate pll_l_rate[] = {
33 {10, 196000000},
34 {12, 245760000},
35 {30, 589820000},
36 {38, 737280000},
37 {41, 800000000},
38 {50, 960000000},
39 {52, 1008000000},
40 {62, 1200000000},
41 {0, 0},
42};
43
44#define PLL_BASE 7
45
46struct shared_pll_control {
47 uint32_t version;
48 struct {
49 /*
50 * Denotes if the PLL is ON. Technically, this can be read
51 * directly from the PLL registers, but this feild is here,
52 * so let's use it.
53 */
54 uint32_t on;
55 /*
56 * One bit for each processor core. The application processor
57 * is allocated bit position 1. All other bits should be
58 * considered as votes from other processors.
59 */
60 uint32_t votes;
61 } pll[PLL_BASE + PLL_END];
62};
63
64static remote_spinlock_t pll_lock;
65static struct shared_pll_control *pll_control;
66
67void __init msm_shared_pll_control_init(void)
68{
69#define PLL_REMOTE_SPINLOCK_ID "S:7"
70 unsigned smem_size;
71
72 remote_spin_lock_init(&pll_lock, PLL_REMOTE_SPINLOCK_ID);
73
74 pll_control = smem_get_entry(SMEM_CLKREGIM_SOURCES, &smem_size);
75 if (!pll_control) {
76 pr_err("Can't find shared PLL control data structure!\n");
77 BUG();
78 /*
79 * There might be more PLLs than what the application processor knows
80 * about. But the index used for each PLL is guaranteed to remain the
81 * same.
82 */
83 } else if (smem_size < sizeof(struct shared_pll_control)) {
84 pr_err("Shared PLL control data"
85 "structure too small!\n");
86 BUG();
87 } else if (pll_control->version != 0xCCEE0001) {
88 pr_err("Shared PLL control version mismatch!\n");
89 BUG();
90 } else {
91 pr_info("Shared PLL control available.\n");
92 return;
93 }
94
95}
96
97static void pll_enable(void __iomem *addr, unsigned on)
98{
99 if (on) {
100 writel_relaxed(2, addr);
101 mb();
102 udelay(5);
103 writel_relaxed(6, addr);
104 mb();
105 udelay(50);
106 writel_relaxed(7, addr);
107 } else {
108 writel_relaxed(0, addr);
109 }
110}
111
112static int pll_clk_enable(struct clk *clk)
113{
114 struct pll_shared_clk *pll = to_pll_shared_clk(clk);
115 unsigned int pll_id = pll->id;
116
117 remote_spin_lock(&pll_lock);
118
119 pll_control->pll[PLL_BASE + pll_id].votes |= BIT(1);
120 if (!pll_control->pll[PLL_BASE + pll_id].on) {
121 pll_enable(pll->mode_reg, 1);
122 pll_control->pll[PLL_BASE + pll_id].on = 1;
123 }
124
125 remote_spin_unlock(&pll_lock);
126 return 0;
127}
128
129static void pll_clk_disable(struct clk *clk)
130{
131 struct pll_shared_clk *pll = to_pll_shared_clk(clk);
132 unsigned int pll_id = pll->id;
133
134 remote_spin_lock(&pll_lock);
135
136 pll_control->pll[PLL_BASE + pll_id].votes &= ~BIT(1);
137 if (pll_control->pll[PLL_BASE + pll_id].on
138 && !pll_control->pll[PLL_BASE + pll_id].votes) {
139 pll_enable(pll->mode_reg, 0);
140 pll_control->pll[PLL_BASE + pll_id].on = 0;
141 }
142
143 remote_spin_unlock(&pll_lock);
144}
145
146static int pll_clk_is_enabled(struct clk *clk)
147{
148 struct pll_shared_clk *pll = to_pll_shared_clk(clk);
149
150 return readl_relaxed(pll->mode_reg) & BIT(0);
151}
152
153static bool pll_clk_is_local(struct clk *clk)
154{
155 return true;
156}
157
158static int pll_clk_handoff(struct clk *clk)
159{
160 struct pll_shared_clk *pll = to_pll_shared_clk(clk);
161 unsigned int pll_lval;
162 struct pll_rate *l;
163
164 /*
165 * Wait for the PLLs to be initialized and then read their frequency.
166 */
167 do {
168 pll_lval = readl_relaxed(pll->mode_reg + 4) & 0x3ff;
169 cpu_relax();
170 udelay(50);
171 } while (pll_lval == 0);
172
173 /* Convert PLL L values to PLL Output rate */
174 for (l = pll_l_rate; l->rate != 0; l++) {
175 if (l->lvalue == pll_lval) {
176 clk->rate = l->rate;
177 break;
178 }
179 }
180
181 if (!clk->rate) {
182 pr_crit("Unknown PLL's L value!\n");
183 BUG();
184 }
185
186 return 0;
187}
188
189struct clk_ops clk_pll_ops = {
190 .enable = pll_clk_enable,
191 .disable = pll_clk_disable,
192 .handoff = pll_clk_handoff,
193 .is_local = pll_clk_is_local,
194 .is_enabled = pll_clk_is_enabled,
195};