1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 297259e99SSteffen Trumtrar /* 397259e99SSteffen Trumtrar * Copyright 2011-2012 Calxeda, Inc. 497259e99SSteffen Trumtrar * Copyright (C) 2012-2013 Altera Corporation <www.altera.com> 597259e99SSteffen Trumtrar * 697259e99SSteffen Trumtrar * Based from clk-highbank.c 797259e99SSteffen Trumtrar */ 8b0af24b5SStephen Boyd #include <linux/slab.h> 997259e99SSteffen Trumtrar #include <linux/clk-provider.h> 1097259e99SSteffen Trumtrar #include <linux/io.h> 1197259e99SSteffen Trumtrar #include <linux/of.h> 1297259e99SSteffen Trumtrar 1397259e99SSteffen Trumtrar #include "clk.h" 1497259e99SSteffen Trumtrar 1597259e99SSteffen Trumtrar #define to_socfpga_periph_clk(p) container_of(p, struct socfpga_periph_clk, hw.hw) 1697259e99SSteffen Trumtrar 1797259e99SSteffen Trumtrar static unsigned long clk_periclk_recalc_rate(struct clk_hw *hwclk, 1897259e99SSteffen Trumtrar unsigned long parent_rate) 1997259e99SSteffen Trumtrar { 2097259e99SSteffen Trumtrar struct socfpga_periph_clk *socfpgaclk = to_socfpga_periph_clk(hwclk); 210691bb1bSDinh Nguyen u32 div, val; 2297259e99SSteffen Trumtrar 230691bb1bSDinh Nguyen if (socfpgaclk->fixed_div) { 2497259e99SSteffen Trumtrar div = socfpgaclk->fixed_div; 250691bb1bSDinh Nguyen } else { 260691bb1bSDinh Nguyen if (socfpgaclk->div_reg) { 270691bb1bSDinh Nguyen val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift; 2825d4d341SAndy Shevchenko val &= GENMASK(socfpgaclk->width - 1, 0); 290691bb1bSDinh Nguyen parent_rate /= (val + 1); 300691bb1bSDinh Nguyen } 3197259e99SSteffen Trumtrar div = ((readl(socfpgaclk->hw.reg) & 0x1ff) + 1); 320691bb1bSDinh Nguyen } 3397259e99SSteffen Trumtrar 3497259e99SSteffen Trumtrar return parent_rate / div; 3597259e99SSteffen Trumtrar } 3697259e99SSteffen Trumtrar 3734d5003bSDinh Nguyen static u8 clk_periclk_get_parent(struct clk_hw *hwclk) 3834d5003bSDinh Nguyen { 3934d5003bSDinh Nguyen u32 clk_src; 4034d5003bSDinh Nguyen 4134d5003bSDinh Nguyen clk_src = readl(clk_mgr_base_addr + CLKMGR_DBCTRL); 4234d5003bSDinh Nguyen return clk_src & 0x1; 4334d5003bSDinh Nguyen } 4434d5003bSDinh Nguyen 4597259e99SSteffen Trumtrar static const struct clk_ops periclk_ops = { 4697259e99SSteffen Trumtrar .recalc_rate = clk_periclk_recalc_rate, 4734d5003bSDinh Nguyen .get_parent = clk_periclk_get_parent, 4897259e99SSteffen Trumtrar }; 4997259e99SSteffen Trumtrar 50*48801895SMarco Pagani static void __init __socfpga_periph_init(struct device_node *node, 5197259e99SSteffen Trumtrar const struct clk_ops *ops) 5297259e99SSteffen Trumtrar { 5397259e99SSteffen Trumtrar u32 reg; 542c2b9c60SDinh Nguyen struct clk_hw *hw_clk; 5597259e99SSteffen Trumtrar struct socfpga_periph_clk *periph_clk; 5697259e99SSteffen Trumtrar const char *clk_name = node->name; 5734d5003bSDinh Nguyen const char *parent_name[SOCFPGA_MAX_PARENTS]; 5897259e99SSteffen Trumtrar struct clk_init_data init; 5997259e99SSteffen Trumtrar int rc; 6097259e99SSteffen Trumtrar u32 fixed_div; 610691bb1bSDinh Nguyen u32 div_reg[3]; 6297259e99SSteffen Trumtrar 6397259e99SSteffen Trumtrar of_property_read_u32(node, "reg", ®); 6497259e99SSteffen Trumtrar 6597259e99SSteffen Trumtrar periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL); 6697259e99SSteffen Trumtrar if (WARN_ON(!periph_clk)) 6797259e99SSteffen Trumtrar return; 6897259e99SSteffen Trumtrar 6997259e99SSteffen Trumtrar periph_clk->hw.reg = clk_mgr_base_addr + reg; 7097259e99SSteffen Trumtrar 710691bb1bSDinh Nguyen rc = of_property_read_u32_array(node, "div-reg", div_reg, 3); 720691bb1bSDinh Nguyen if (!rc) { 730691bb1bSDinh Nguyen periph_clk->div_reg = clk_mgr_base_addr + div_reg[0]; 740691bb1bSDinh Nguyen periph_clk->shift = div_reg[1]; 750691bb1bSDinh Nguyen periph_clk->width = div_reg[2]; 760691bb1bSDinh Nguyen } else { 77e45310bfSStephen Boyd periph_clk->div_reg = NULL; 780691bb1bSDinh Nguyen } 790691bb1bSDinh Nguyen 8097259e99SSteffen Trumtrar rc = of_property_read_u32(node, "fixed-divider", &fixed_div); 8197259e99SSteffen Trumtrar if (rc) 8297259e99SSteffen Trumtrar periph_clk->fixed_div = 0; 8397259e99SSteffen Trumtrar else 8497259e99SSteffen Trumtrar periph_clk->fixed_div = fixed_div; 8597259e99SSteffen Trumtrar 8697259e99SSteffen Trumtrar of_property_read_string(node, "clock-output-names", &clk_name); 8797259e99SSteffen Trumtrar 8897259e99SSteffen Trumtrar init.name = clk_name; 8997259e99SSteffen Trumtrar init.ops = ops; 9097259e99SSteffen Trumtrar init.flags = 0; 9134d5003bSDinh Nguyen 9234d5003bSDinh Nguyen init.num_parents = of_clk_parent_fill(node, parent_name, 9334d5003bSDinh Nguyen SOCFPGA_MAX_PARENTS); 9434d5003bSDinh Nguyen init.parent_names = parent_name; 9597259e99SSteffen Trumtrar 9697259e99SSteffen Trumtrar periph_clk->hw.hw.init = &init; 972c2b9c60SDinh Nguyen hw_clk = &periph_clk->hw.hw; 9897259e99SSteffen Trumtrar 99*48801895SMarco Pagani rc = clk_hw_register(NULL, hw_clk); 100*48801895SMarco Pagani if (rc) { 101*48801895SMarco Pagani pr_err("Could not register clock:%s\n", clk_name); 102*48801895SMarco Pagani goto err_clk_hw_register; 10397259e99SSteffen Trumtrar } 104*48801895SMarco Pagani 105*48801895SMarco Pagani rc = of_clk_add_hw_provider(node, of_clk_hw_simple_get, hw_clk); 106*48801895SMarco Pagani if (rc) { 107*48801895SMarco Pagani pr_err("Could not register clock provider for node:%s\n", 108*48801895SMarco Pagani clk_name); 109*48801895SMarco Pagani goto err_of_clk_add_hw_provider; 110*48801895SMarco Pagani } 111*48801895SMarco Pagani 112*48801895SMarco Pagani return; 113*48801895SMarco Pagani 114*48801895SMarco Pagani err_of_clk_add_hw_provider: 115*48801895SMarco Pagani clk_hw_unregister(hw_clk); 116*48801895SMarco Pagani err_clk_hw_register: 117*48801895SMarco Pagani kfree(periph_clk); 11897259e99SSteffen Trumtrar } 11997259e99SSteffen Trumtrar 12097259e99SSteffen Trumtrar void __init socfpga_periph_init(struct device_node *node) 12197259e99SSteffen Trumtrar { 12297259e99SSteffen Trumtrar __socfpga_periph_init(node, &periclk_ops); 12397259e99SSteffen Trumtrar } 124