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 | #define SOCFPGA_L4_MP_CLK "l4_mp_clk" |
| 27 | #define SOCFPGA_L4_SP_CLK "l4_sp_clk" |
| 28 | #define SOCFPGA_NAND_CLK "nand_clk" |
| 29 | #define SOCFPGA_NAND_X_CLK "nand_x_clk" |
| 30 | #define SOCFPGA_MMC_CLK "sdmmc_clk" |
| 31 | #define SOCFPGA_GPIO_DB_CLK_OFFSET 0xA8 |
| 32 | |
| 33 | #define div_mask(width) ((1 << (width)) - 1) |
| 34 | #define streq(a, b) (strcmp((a), (b)) == 0) |
| 35 | |
| 36 | #define to_socfpga_gate_clk(p) container_of(p, struct socfpga_gate_clk, hw.hw) |
| 37 | |
| 38 | static u8 socfpga_clk_get_parent(struct clk_hw *hwclk) |
| 39 | { |
| 40 | u32 l4_src; |
| 41 | u32 perpll_src; |
| 42 | |
| 43 | if (streq(hwclk->init->name, SOCFPGA_L4_MP_CLK)) { |
| 44 | l4_src = readl(clk_mgr_base_addr + CLKMGR_L4SRC); |
| 45 | return l4_src &= 0x1; |
| 46 | } |
| 47 | if (streq(hwclk->init->name, SOCFPGA_L4_SP_CLK)) { |
| 48 | l4_src = readl(clk_mgr_base_addr + CLKMGR_L4SRC); |
| 49 | return !!(l4_src & 2); |
| 50 | } |
| 51 | |
| 52 | perpll_src = readl(clk_mgr_base_addr + CLKMGR_PERPLL_SRC); |
| 53 | if (streq(hwclk->init->name, SOCFPGA_MMC_CLK)) |
| 54 | return perpll_src &= 0x3; |
| 55 | if (streq(hwclk->init->name, SOCFPGA_NAND_CLK) || |
| 56 | streq(hwclk->init->name, SOCFPGA_NAND_X_CLK)) |
| 57 | return (perpll_src >> 2) & 3; |
| 58 | |
| 59 | /* QSPI clock */ |
| 60 | return (perpll_src >> 4) & 3; |
| 61 | |
| 62 | } |
| 63 | |
| 64 | static int socfpga_clk_set_parent(struct clk_hw *hwclk, u8 parent) |
| 65 | { |
| 66 | u32 src_reg; |
| 67 | |
| 68 | if (streq(hwclk->init->name, SOCFPGA_L4_MP_CLK)) { |
| 69 | src_reg = readl(clk_mgr_base_addr + CLKMGR_L4SRC); |
| 70 | src_reg &= ~0x1; |
| 71 | src_reg |= parent; |
| 72 | writel(src_reg, clk_mgr_base_addr + CLKMGR_L4SRC); |
| 73 | } else if (streq(hwclk->init->name, SOCFPGA_L4_SP_CLK)) { |
| 74 | src_reg = readl(clk_mgr_base_addr + CLKMGR_L4SRC); |
| 75 | src_reg &= ~0x2; |
| 76 | src_reg |= (parent << 1); |
| 77 | writel(src_reg, clk_mgr_base_addr + CLKMGR_L4SRC); |
| 78 | } else { |
| 79 | src_reg = readl(clk_mgr_base_addr + CLKMGR_PERPLL_SRC); |
| 80 | if (streq(hwclk->init->name, SOCFPGA_MMC_CLK)) { |
| 81 | src_reg &= ~0x3; |
| 82 | src_reg |= parent; |
| 83 | } else if (streq(hwclk->init->name, SOCFPGA_NAND_CLK) || |
| 84 | streq(hwclk->init->name, SOCFPGA_NAND_X_CLK)) { |
| 85 | src_reg &= ~0xC; |
| 86 | src_reg |= (parent << 2); |
| 87 | } else {/* QSPI clock */ |
| 88 | src_reg &= ~0x30; |
| 89 | src_reg |= (parent << 4); |
| 90 | } |
| 91 | writel(src_reg, clk_mgr_base_addr + CLKMGR_PERPLL_SRC); |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static unsigned long socfpga_clk_recalc_rate(struct clk_hw *hwclk, |
| 98 | unsigned long parent_rate) |
| 99 | { |
| 100 | struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk); |
| 101 | u32 div = 1, val; |
| 102 | |
| 103 | if (socfpgaclk->fixed_div) |
| 104 | div = socfpgaclk->fixed_div; |
| 105 | else if (socfpgaclk->div_reg) { |
| 106 | val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift; |
| 107 | val &= div_mask(socfpgaclk->width); |
| 108 | /* Check for GPIO_DB_CLK by its offset */ |
| 109 | if ((int) socfpgaclk->div_reg & SOCFPGA_GPIO_DB_CLK_OFFSET) |
| 110 | div = val + 1; |
| 111 | else |
| 112 | div = (1 << val); |
| 113 | } |
| 114 | |
| 115 | return parent_rate / div; |
| 116 | } |
| 117 | |
| 118 | static struct clk_ops gateclk_ops = { |
| 119 | .recalc_rate = socfpga_clk_recalc_rate, |
| 120 | .get_parent = socfpga_clk_get_parent, |
| 121 | .set_parent = socfpga_clk_set_parent, |
| 122 | }; |
| 123 | |
| 124 | static void __init __socfpga_gate_init(struct device_node *node, |
| 125 | const struct clk_ops *ops) |
| 126 | { |
| 127 | u32 clk_gate[2]; |
| 128 | u32 div_reg[3]; |
| 129 | u32 fixed_div; |
| 130 | struct clk *clk; |
| 131 | struct socfpga_gate_clk *socfpga_clk; |
| 132 | const char *clk_name = node->name; |
| 133 | const char *parent_name[SOCFPGA_MAX_PARENTS]; |
| 134 | struct clk_init_data init; |
| 135 | int rc; |
| 136 | int i = 0; |
| 137 | |
| 138 | socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL); |
| 139 | if (WARN_ON(!socfpga_clk)) |
| 140 | return; |
| 141 | |
| 142 | rc = of_property_read_u32_array(node, "clk-gate", clk_gate, 2); |
| 143 | if (rc) |
| 144 | clk_gate[0] = 0; |
| 145 | |
| 146 | if (clk_gate[0]) { |
| 147 | socfpga_clk->hw.reg = clk_mgr_base_addr + clk_gate[0]; |
| 148 | socfpga_clk->hw.bit_idx = clk_gate[1]; |
| 149 | |
| 150 | gateclk_ops.enable = clk_gate_ops.enable; |
| 151 | gateclk_ops.disable = clk_gate_ops.disable; |
| 152 | } |
| 153 | |
| 154 | rc = of_property_read_u32(node, "fixed-divider", &fixed_div); |
| 155 | if (rc) |
| 156 | socfpga_clk->fixed_div = 0; |
| 157 | else |
| 158 | socfpga_clk->fixed_div = fixed_div; |
| 159 | |
| 160 | rc = of_property_read_u32_array(node, "div-reg", div_reg, 3); |
| 161 | if (!rc) { |
| 162 | socfpga_clk->div_reg = clk_mgr_base_addr + div_reg[0]; |
| 163 | socfpga_clk->shift = div_reg[1]; |
| 164 | socfpga_clk->width = div_reg[2]; |
| 165 | } else { |
| 166 | socfpga_clk->div_reg = 0; |
| 167 | } |
| 168 | |
| 169 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 170 | |
| 171 | init.name = clk_name; |
| 172 | init.ops = ops; |
| 173 | init.flags = 0; |
| 174 | while (i < SOCFPGA_MAX_PARENTS && (parent_name[i] = |
| 175 | of_clk_get_parent_name(node, i)) != NULL) |
| 176 | i++; |
| 177 | |
| 178 | init.parent_names = parent_name; |
| 179 | init.num_parents = i; |
| 180 | socfpga_clk->hw.hw.init = &init; |
| 181 | |
| 182 | clk = clk_register(NULL, &socfpga_clk->hw.hw); |
| 183 | if (WARN_ON(IS_ERR(clk))) { |
| 184 | kfree(socfpga_clk); |
| 185 | return; |
| 186 | } |
| 187 | rc = of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 188 | if (WARN_ON(rc)) |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | void __init socfpga_gate_init(struct device_node *node) |
| 193 | { |
| 194 | __socfpga_gate_init(node, &gateclk_ops); |
| 195 | } |