| 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 | */ |
| Stephen Boyd | b0af24b | 2015-06-19 15:00:46 -0700 | [diff] [blame^] | 18 | #include <linux/slab.h> |
| Steffen Trumtrar | 97259e9 | 2014-01-06 10:27:37 -0600 | [diff] [blame] | 19 | #include <linux/clk-provider.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/of.h> |
| 22 | |
| 23 | #include "clk.h" |
| 24 | |
| 25 | #define to_socfpga_periph_clk(p) container_of(p, struct socfpga_periph_clk, hw.hw) |
| 26 | |
| 27 | static unsigned long clk_periclk_recalc_rate(struct clk_hw *hwclk, |
| 28 | unsigned long parent_rate) |
| 29 | { |
| 30 | struct socfpga_periph_clk *socfpgaclk = to_socfpga_periph_clk(hwclk); |
| Dinh Nguyen | 0691bb1 | 2014-05-12 12:27:22 -0500 | [diff] [blame] | 31 | u32 div, val; |
| Steffen Trumtrar | 97259e9 | 2014-01-06 10:27:37 -0600 | [diff] [blame] | 32 | |
| Dinh Nguyen | 0691bb1 | 2014-05-12 12:27:22 -0500 | [diff] [blame] | 33 | if (socfpgaclk->fixed_div) { |
| Steffen Trumtrar | 97259e9 | 2014-01-06 10:27:37 -0600 | [diff] [blame] | 34 | div = socfpgaclk->fixed_div; |
| Dinh Nguyen | 0691bb1 | 2014-05-12 12:27:22 -0500 | [diff] [blame] | 35 | } else { |
| 36 | if (socfpgaclk->div_reg) { |
| 37 | val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift; |
| 38 | val &= div_mask(socfpgaclk->width); |
| 39 | parent_rate /= (val + 1); |
| 40 | } |
| Steffen Trumtrar | 97259e9 | 2014-01-06 10:27:37 -0600 | [diff] [blame] | 41 | div = ((readl(socfpgaclk->hw.reg) & 0x1ff) + 1); |
| Dinh Nguyen | 0691bb1 | 2014-05-12 12:27:22 -0500 | [diff] [blame] | 42 | } |
| Steffen Trumtrar | 97259e9 | 2014-01-06 10:27:37 -0600 | [diff] [blame] | 43 | |
| 44 | return parent_rate / div; |
| 45 | } |
| 46 | |
| 47 | static const struct clk_ops periclk_ops = { |
| 48 | .recalc_rate = clk_periclk_recalc_rate, |
| 49 | }; |
| 50 | |
| 51 | static __init void __socfpga_periph_init(struct device_node *node, |
| 52 | const struct clk_ops *ops) |
| 53 | { |
| 54 | u32 reg; |
| 55 | struct clk *clk; |
| 56 | struct socfpga_periph_clk *periph_clk; |
| 57 | const char *clk_name = node->name; |
| 58 | const char *parent_name; |
| 59 | struct clk_init_data init; |
| 60 | int rc; |
| 61 | u32 fixed_div; |
| Dinh Nguyen | 0691bb1 | 2014-05-12 12:27:22 -0500 | [diff] [blame] | 62 | u32 div_reg[3]; |
| Steffen Trumtrar | 97259e9 | 2014-01-06 10:27:37 -0600 | [diff] [blame] | 63 | |
| 64 | of_property_read_u32(node, "reg", ®); |
| 65 | |
| 66 | periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL); |
| 67 | if (WARN_ON(!periph_clk)) |
| 68 | return; |
| 69 | |
| 70 | periph_clk->hw.reg = clk_mgr_base_addr + reg; |
| 71 | |
| Dinh Nguyen | 0691bb1 | 2014-05-12 12:27:22 -0500 | [diff] [blame] | 72 | rc = of_property_read_u32_array(node, "div-reg", div_reg, 3); |
| 73 | if (!rc) { |
| 74 | periph_clk->div_reg = clk_mgr_base_addr + div_reg[0]; |
| 75 | periph_clk->shift = div_reg[1]; |
| 76 | periph_clk->width = div_reg[2]; |
| 77 | } else { |
| Stephen Boyd | e45310bf | 2015-05-01 13:11:31 -0700 | [diff] [blame] | 78 | periph_clk->div_reg = NULL; |
| Dinh Nguyen | 0691bb1 | 2014-05-12 12:27:22 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| Steffen Trumtrar | 97259e9 | 2014-01-06 10:27:37 -0600 | [diff] [blame] | 81 | rc = of_property_read_u32(node, "fixed-divider", &fixed_div); |
| 82 | if (rc) |
| 83 | periph_clk->fixed_div = 0; |
| 84 | else |
| 85 | periph_clk->fixed_div = fixed_div; |
| 86 | |
| 87 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 88 | |
| 89 | init.name = clk_name; |
| 90 | init.ops = ops; |
| 91 | init.flags = 0; |
| 92 | parent_name = of_clk_get_parent_name(node, 0); |
| 93 | init.parent_names = &parent_name; |
| 94 | init.num_parents = 1; |
| 95 | |
| 96 | periph_clk->hw.hw.init = &init; |
| 97 | |
| 98 | clk = clk_register(NULL, &periph_clk->hw.hw); |
| 99 | if (WARN_ON(IS_ERR(clk))) { |
| 100 | kfree(periph_clk); |
| 101 | return; |
| 102 | } |
| 103 | rc = of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 104 | } |
| 105 | |
| 106 | void __init socfpga_periph_init(struct device_node *node) |
| 107 | { |
| 108 | __socfpga_periph_init(node, &periclk_ops); |
| 109 | } |