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