Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ARM: socfpga: Add support to gate peripheral clocks

Add support to gate the clocks that directly feed peripherals. For clocks
with multiple parents, add the ability to determine the correct parent,
and also set parents. Also add support to calculate and set the clocks'
rate.

Signed-off-by: Dinh Nguyen <dinguyen@altera.com>
Reviewed-by: Pavel Machek <pavel@denx.de>
Acked-by: Mike Turquette <mturquette@linaro.org>
Cc: Mike Turquette <mturquette@linaro.org>
CC: Arnd Bergmann <arnd@arndb.de>
CC: Olof Johansson <olof@lixom.net>
Cc: Pavel Machek <pavel@denx.de>
CC: <linux@arm.linux.org.uk>

v4:
- Add Acked-by: Mike Turquette

v3:
- Addressed comments from Pavel

v2:
- Fix space/indent errors
- Add streq for strcmp == 0
Signed-off-by: Olof Johansson <olof@lixom.net>

authored by

Dinh Nguyen and committed by
Olof Johansson
825f0c26 ea36b022

+185 -9
+185 -9
drivers/clk/socfpga/clk.c
··· 24 24 #include <linux/of.h> 25 25 26 26 /* Clock Manager offsets */ 27 - #define CLKMGR_CTRL 0x0 28 - #define CLKMGR_BYPASS 0x4 27 + #define CLKMGR_CTRL 0x0 28 + #define CLKMGR_BYPASS 0x4 29 + #define CLKMGR_L4SRC 0x70 30 + #define CLKMGR_PERPLL_SRC 0xAC 29 31 30 32 /* Clock bypass bits */ 31 - #define MAINPLL_BYPASS (1<<0) 32 - #define SDRAMPLL_BYPASS (1<<1) 33 - #define SDRAMPLL_SRC_BYPASS (1<<2) 34 - #define PERPLL_BYPASS (1<<3) 35 - #define PERPLL_SRC_BYPASS (1<<4) 33 + #define MAINPLL_BYPASS (1<<0) 34 + #define SDRAMPLL_BYPASS (1<<1) 35 + #define SDRAMPLL_SRC_BYPASS (1<<2) 36 + #define PERPLL_BYPASS (1<<3) 37 + #define PERPLL_SRC_BYPASS (1<<4) 36 38 37 39 #define SOCFPGA_PLL_BG_PWRDWN 0 38 40 #define SOCFPGA_PLL_EXT_ENA 1 ··· 43 41 #define SOCFPGA_PLL_DIVF_SHIFT 3 44 42 #define SOCFPGA_PLL_DIVQ_MASK 0x003F0000 45 43 #define SOCFPGA_PLL_DIVQ_SHIFT 16 44 + #define SOCFGPA_MAX_PARENTS 3 45 + 46 + #define SOCFPGA_L4_MP_CLK "l4_mp_clk" 47 + #define SOCFPGA_L4_SP_CLK "l4_sp_clk" 48 + #define SOCFPGA_NAND_CLK "nand_clk" 49 + #define SOCFPGA_NAND_X_CLK "nand_x_clk" 50 + #define SOCFPGA_MMC_CLK "mmc_clk" 51 + #define SOCFPGA_DB_CLK "gpio_db_clk" 52 + 53 + #define div_mask(width) ((1 << (width)) - 1) 54 + #define streq(a, b) (strcmp((a), (b)) == 0) 46 55 47 56 extern void __iomem *clk_mgr_base_addr; 48 57 ··· 62 49 char *parent_name; 63 50 char *clk_name; 64 51 u32 fixed_div; 52 + void __iomem *div_reg; 53 + u32 width; /* only valid if div_reg != 0 */ 54 + u32 shift; /* only valid if div_reg != 0 */ 65 55 }; 66 56 #define to_socfpga_clk(p) container_of(p, struct socfpga_clk, hw.hw) 67 57 ··· 148 132 149 133 socfpga_clk->hw.hw.init = &init; 150 134 151 - if (strcmp(clk_name, "main_pll") || strcmp(clk_name, "periph_pll") || 152 - strcmp(clk_name, "sdram_pll")) { 135 + if (streq(clk_name, "main_pll") || 136 + streq(clk_name, "periph_pll") || 137 + streq(clk_name, "sdram_pll")) { 153 138 socfpga_clk->hw.bit_idx = SOCFPGA_PLL_EXT_ENA; 154 139 clk_pll_ops.enable = clk_gate_ops.enable; 155 140 clk_pll_ops.disable = clk_gate_ops.disable; ··· 165 148 return clk; 166 149 } 167 150 151 + static u8 socfpga_clk_get_parent(struct clk_hw *hwclk) 152 + { 153 + u32 l4_src; 154 + u32 perpll_src; 155 + 156 + if (streq(hwclk->init->name, SOCFPGA_L4_MP_CLK)) { 157 + l4_src = readl(clk_mgr_base_addr + CLKMGR_L4SRC); 158 + return l4_src &= 0x1; 159 + } 160 + if (streq(hwclk->init->name, SOCFPGA_L4_SP_CLK)) { 161 + l4_src = readl(clk_mgr_base_addr + CLKMGR_L4SRC); 162 + return !!(l4_src & 2); 163 + } 164 + 165 + perpll_src = readl(clk_mgr_base_addr + CLKMGR_PERPLL_SRC); 166 + if (streq(hwclk->init->name, SOCFPGA_MMC_CLK)) 167 + return perpll_src &= 0x3; 168 + if (streq(hwclk->init->name, SOCFPGA_NAND_CLK) || 169 + streq(hwclk->init->name, SOCFPGA_NAND_X_CLK)) 170 + return (perpll_src >> 2) & 3; 171 + 172 + /* QSPI clock */ 173 + return (perpll_src >> 4) & 3; 174 + 175 + } 176 + 177 + static int socfpga_clk_set_parent(struct clk_hw *hwclk, u8 parent) 178 + { 179 + u32 src_reg; 180 + 181 + if (streq(hwclk->init->name, SOCFPGA_L4_MP_CLK)) { 182 + src_reg = readl(clk_mgr_base_addr + CLKMGR_L4SRC); 183 + src_reg &= ~0x1; 184 + src_reg |= parent; 185 + writel(src_reg, clk_mgr_base_addr + CLKMGR_L4SRC); 186 + } else if (streq(hwclk->init->name, SOCFPGA_L4_SP_CLK)) { 187 + src_reg = readl(clk_mgr_base_addr + CLKMGR_L4SRC); 188 + src_reg &= ~0x2; 189 + src_reg |= (parent << 1); 190 + writel(src_reg, clk_mgr_base_addr + CLKMGR_L4SRC); 191 + } else { 192 + src_reg = readl(clk_mgr_base_addr + CLKMGR_PERPLL_SRC); 193 + if (streq(hwclk->init->name, SOCFPGA_MMC_CLK)) { 194 + src_reg &= ~0x3; 195 + src_reg |= parent; 196 + } else if (streq(hwclk->init->name, SOCFPGA_NAND_CLK) || 197 + streq(hwclk->init->name, SOCFPGA_NAND_X_CLK)) { 198 + src_reg &= ~0xC; 199 + src_reg |= (parent << 2); 200 + } else {/* QSPI clock */ 201 + src_reg &= ~0x30; 202 + src_reg |= (parent << 4); 203 + } 204 + writel(src_reg, clk_mgr_base_addr + CLKMGR_PERPLL_SRC); 205 + } 206 + 207 + return 0; 208 + } 209 + 210 + static unsigned long socfpga_clk_recalc_rate(struct clk_hw *hwclk, 211 + unsigned long parent_rate) 212 + { 213 + struct socfpga_clk *socfpgaclk = to_socfpga_clk(hwclk); 214 + u32 div = 1, val; 215 + 216 + if (socfpgaclk->fixed_div) 217 + div = socfpgaclk->fixed_div; 218 + else if (socfpgaclk->div_reg) { 219 + val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift; 220 + val &= div_mask(socfpgaclk->width); 221 + if (streq(hwclk->init->name, SOCFPGA_DB_CLK)) 222 + div = val + 1; 223 + else 224 + div = (1 << val); 225 + } 226 + 227 + return parent_rate / div; 228 + } 229 + 230 + static struct clk_ops gateclk_ops = { 231 + .recalc_rate = socfpga_clk_recalc_rate, 232 + .get_parent = socfpga_clk_get_parent, 233 + .set_parent = socfpga_clk_set_parent, 234 + }; 235 + 236 + static void __init socfpga_gate_clk_init(struct device_node *node, 237 + const struct clk_ops *ops) 238 + { 239 + u32 clk_gate[2]; 240 + u32 div_reg[3]; 241 + u32 fixed_div; 242 + struct clk *clk; 243 + struct socfpga_clk *socfpga_clk; 244 + const char *clk_name = node->name; 245 + const char *parent_name[SOCFGPA_MAX_PARENTS]; 246 + struct clk_init_data init; 247 + int rc; 248 + int i = 0; 249 + 250 + socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL); 251 + if (WARN_ON(!socfpga_clk)) 252 + return; 253 + 254 + rc = of_property_read_u32_array(node, "clk-gate", clk_gate, 2); 255 + if (rc) 256 + clk_gate[0] = 0; 257 + 258 + if (clk_gate[0]) { 259 + socfpga_clk->hw.reg = clk_mgr_base_addr + clk_gate[0]; 260 + socfpga_clk->hw.bit_idx = clk_gate[1]; 261 + 262 + gateclk_ops.enable = clk_gate_ops.enable; 263 + gateclk_ops.disable = clk_gate_ops.disable; 264 + } 265 + 266 + rc = of_property_read_u32(node, "fixed-divider", &fixed_div); 267 + if (rc) 268 + socfpga_clk->fixed_div = 0; 269 + else 270 + socfpga_clk->fixed_div = fixed_div; 271 + 272 + rc = of_property_read_u32_array(node, "div-reg", div_reg, 3); 273 + if (!rc) { 274 + socfpga_clk->div_reg = clk_mgr_base_addr + div_reg[0]; 275 + socfpga_clk->shift = div_reg[1]; 276 + socfpga_clk->width = div_reg[2]; 277 + } else { 278 + socfpga_clk->div_reg = 0; 279 + } 280 + 281 + of_property_read_string(node, "clock-output-names", &clk_name); 282 + 283 + init.name = clk_name; 284 + init.ops = ops; 285 + init.flags = 0; 286 + while (i < SOCFGPA_MAX_PARENTS && (parent_name[i] = 287 + of_clk_get_parent_name(node, i)) != NULL) 288 + i++; 289 + 290 + init.parent_names = parent_name; 291 + init.num_parents = i; 292 + socfpga_clk->hw.hw.init = &init; 293 + 294 + clk = clk_register(NULL, &socfpga_clk->hw.hw); 295 + if (WARN_ON(IS_ERR(clk))) { 296 + kfree(socfpga_clk); 297 + return; 298 + } 299 + rc = of_clk_add_provider(node, of_clk_src_simple_get, clk); 300 + if (WARN_ON(rc)) 301 + return; 302 + } 303 + 168 304 static void __init socfpga_pll_init(struct device_node *node) 169 305 { 170 306 socfpga_clk_init(node, &clk_pll_ops); ··· 329 159 socfpga_clk_init(node, &periclk_ops); 330 160 } 331 161 CLK_OF_DECLARE(socfpga_periph, "altr,socfpga-perip-clk", socfpga_periph_init); 162 + 163 + static void __init socfpga_gate_init(struct device_node *node) 164 + { 165 + socfpga_gate_clk_init(node, &gateclk_ops); 166 + } 167 + CLK_OF_DECLARE(socfpga_gate, "altr,socfpga-gate-clk", socfpga_gate_init); 332 168 333 169 void __init socfpga_init_clocks(void) 334 170 {