Steffen Trumtrar | 97259e9 | 2014-01-06 10:27:37 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2011-2012 Calxeda, Inc. |
| 3 | * Copyright (C) 2012-2013 Altera Corporation <www.altera.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * Based from clk-highbank.c |
| 16 | * |
| 17 | */ |
| 18 | #include <linux/clk.h> |
| 19 | #include <linux/clkdev.h> |
| 20 | #include <linux/clk-provider.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/of.h> |
| 23 | |
| 24 | #include "clk.h" |
| 25 | |
| 26 | /* Clock bypass bits */ |
| 27 | #define MAINPLL_BYPASS (1<<0) |
| 28 | #define SDRAMPLL_BYPASS (1<<1) |
| 29 | #define SDRAMPLL_SRC_BYPASS (1<<2) |
| 30 | #define PERPLL_BYPASS (1<<3) |
| 31 | #define PERPLL_SRC_BYPASS (1<<4) |
| 32 | |
| 33 | #define SOCFPGA_PLL_BG_PWRDWN 0 |
| 34 | #define SOCFPGA_PLL_EXT_ENA 1 |
| 35 | #define SOCFPGA_PLL_PWR_DOWN 2 |
| 36 | #define SOCFPGA_PLL_DIVF_MASK 0x0000FFF8 |
| 37 | #define SOCFPGA_PLL_DIVF_SHIFT 3 |
| 38 | #define SOCFPGA_PLL_DIVQ_MASK 0x003F0000 |
| 39 | #define SOCFPGA_PLL_DIVQ_SHIFT 16 |
| 40 | |
| 41 | #define to_socfpga_clk(p) container_of(p, struct socfpga_pll, hw.hw) |
| 42 | |
| 43 | static unsigned long clk_pll_recalc_rate(struct clk_hw *hwclk, |
| 44 | unsigned long parent_rate) |
| 45 | { |
| 46 | struct socfpga_pll *socfpgaclk = to_socfpga_clk(hwclk); |
| 47 | unsigned long divf, divq, vco_freq, reg; |
| 48 | unsigned long bypass; |
| 49 | |
| 50 | reg = readl(socfpgaclk->hw.reg); |
| 51 | bypass = readl(clk_mgr_base_addr + CLKMGR_BYPASS); |
| 52 | if (bypass & MAINPLL_BYPASS) |
| 53 | return parent_rate; |
| 54 | |
| 55 | divf = (reg & SOCFPGA_PLL_DIVF_MASK) >> SOCFPGA_PLL_DIVF_SHIFT; |
| 56 | divq = (reg & SOCFPGA_PLL_DIVQ_MASK) >> SOCFPGA_PLL_DIVQ_SHIFT; |
| 57 | vco_freq = parent_rate * (divf + 1); |
| 58 | return vco_freq / (1 + divq); |
| 59 | } |
| 60 | |
| 61 | static struct clk_ops clk_pll_ops = { |
| 62 | .recalc_rate = clk_pll_recalc_rate, |
| 63 | }; |
| 64 | |
| 65 | static __init struct clk *__socfpga_pll_init(struct device_node *node, |
| 66 | const struct clk_ops *ops) |
| 67 | { |
| 68 | u32 reg; |
| 69 | struct clk *clk; |
| 70 | struct socfpga_pll *pll_clk; |
| 71 | const char *clk_name = node->name; |
| 72 | const char *parent_name; |
| 73 | struct clk_init_data init; |
| 74 | int rc; |
| 75 | |
| 76 | of_property_read_u32(node, "reg", ®); |
| 77 | |
| 78 | pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL); |
| 79 | if (WARN_ON(!pll_clk)) |
| 80 | return NULL; |
| 81 | |
| 82 | pll_clk->hw.reg = clk_mgr_base_addr + reg; |
| 83 | |
| 84 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 85 | |
| 86 | init.name = clk_name; |
| 87 | init.ops = ops; |
| 88 | init.flags = 0; |
| 89 | parent_name = of_clk_get_parent_name(node, 0); |
| 90 | init.parent_names = parent_name ? &parent_name : NULL; |
| 91 | init.num_parents = parent_name ? 1 : 0; |
| 92 | |
| 93 | pll_clk->hw.hw.init = &init; |
| 94 | |
| 95 | pll_clk->hw.bit_idx = SOCFPGA_PLL_EXT_ENA; |
| 96 | clk_pll_ops.enable = clk_gate_ops.enable; |
| 97 | clk_pll_ops.disable = clk_gate_ops.disable; |
| 98 | |
| 99 | clk = clk_register(NULL, &pll_clk->hw.hw); |
| 100 | if (WARN_ON(IS_ERR(clk))) { |
| 101 | kfree(pll_clk); |
| 102 | return NULL; |
| 103 | } |
| 104 | rc = of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 105 | return clk; |
| 106 | } |
| 107 | |
| 108 | void __init socfpga_pll_init(struct device_node *node) |
| 109 | { |
| 110 | __socfpga_pll_init(node, &clk_pll_ops); |
| 111 | } |