Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Copyright (C) 2013 ARM Limited
0005  */
0006 
0007 #include <linux/amba/sp810.h>
0008 #include <linux/slab.h>
0009 #include <linux/clk.h>
0010 #include <linux/clk-provider.h>
0011 #include <linux/err.h>
0012 #include <linux/io.h>
0013 #include <linux/of.h>
0014 #include <linux/of_address.h>
0015 
0016 #define to_clk_sp810_timerclken(_hw) \
0017         container_of(_hw, struct clk_sp810_timerclken, hw)
0018 
0019 struct clk_sp810;
0020 
0021 struct clk_sp810_timerclken {
0022     struct clk_hw hw;
0023     struct clk *clk;
0024     struct clk_sp810 *sp810;
0025     int channel;
0026 };
0027 
0028 struct clk_sp810 {
0029     struct device_node *node;
0030     void __iomem *base;
0031     spinlock_t lock;
0032     struct clk_sp810_timerclken timerclken[4];
0033 };
0034 
0035 static u8 clk_sp810_timerclken_get_parent(struct clk_hw *hw)
0036 {
0037     struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw);
0038     u32 val = readl(timerclken->sp810->base + SCCTRL);
0039 
0040     return !!(val & (1 << SCCTRL_TIMERENnSEL_SHIFT(timerclken->channel)));
0041 }
0042 
0043 static int clk_sp810_timerclken_set_parent(struct clk_hw *hw, u8 index)
0044 {
0045     struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw);
0046     struct clk_sp810 *sp810 = timerclken->sp810;
0047     u32 val, shift = SCCTRL_TIMERENnSEL_SHIFT(timerclken->channel);
0048     unsigned long flags = 0;
0049 
0050     if (WARN_ON(index > 1))
0051         return -EINVAL;
0052 
0053     spin_lock_irqsave(&sp810->lock, flags);
0054 
0055     val = readl(sp810->base + SCCTRL);
0056     val &= ~(1 << shift);
0057     val |= index << shift;
0058     writel(val, sp810->base + SCCTRL);
0059 
0060     spin_unlock_irqrestore(&sp810->lock, flags);
0061 
0062     return 0;
0063 }
0064 
0065 static const struct clk_ops clk_sp810_timerclken_ops = {
0066     .get_parent = clk_sp810_timerclken_get_parent,
0067     .set_parent = clk_sp810_timerclken_set_parent,
0068 };
0069 
0070 static struct clk *clk_sp810_timerclken_of_get(struct of_phandle_args *clkspec,
0071         void *data)
0072 {
0073     struct clk_sp810 *sp810 = data;
0074 
0075     if (WARN_ON(clkspec->args_count != 1 ||
0076             clkspec->args[0] >= ARRAY_SIZE(sp810->timerclken)))
0077         return NULL;
0078 
0079     return sp810->timerclken[clkspec->args[0]].clk;
0080 }
0081 
0082 static void __init clk_sp810_of_setup(struct device_node *node)
0083 {
0084     struct clk_sp810 *sp810 = kzalloc(sizeof(*sp810), GFP_KERNEL);
0085     const char *parent_names[2];
0086     int num = ARRAY_SIZE(parent_names);
0087     char name[12];
0088     struct clk_init_data init;
0089     static int instance;
0090     int i;
0091     bool deprecated;
0092 
0093     if (!sp810)
0094         return;
0095 
0096     if (of_clk_parent_fill(node, parent_names, num) != num) {
0097         pr_warn("Failed to obtain parent clocks for SP810!\n");
0098         kfree(sp810);
0099         return;
0100     }
0101 
0102     sp810->node = node;
0103     sp810->base = of_iomap(node, 0);
0104     spin_lock_init(&sp810->lock);
0105 
0106     init.name = name;
0107     init.ops = &clk_sp810_timerclken_ops;
0108     init.flags = 0;
0109     init.parent_names = parent_names;
0110     init.num_parents = num;
0111 
0112     deprecated = !of_find_property(node, "assigned-clock-parents", NULL);
0113 
0114     for (i = 0; i < ARRAY_SIZE(sp810->timerclken); i++) {
0115         snprintf(name, sizeof(name), "sp810_%d_%d", instance, i);
0116 
0117         sp810->timerclken[i].sp810 = sp810;
0118         sp810->timerclken[i].channel = i;
0119         sp810->timerclken[i].hw.init = &init;
0120 
0121         /*
0122          * If DT isn't setting the parent, force it to be
0123          * the 1 MHz clock without going through the framework.
0124          * We do this before clk_register() so that it can determine
0125          * the parent and setup the tree properly.
0126          */
0127         if (deprecated)
0128             init.ops->set_parent(&sp810->timerclken[i].hw, 1);
0129 
0130         sp810->timerclken[i].clk = clk_register(NULL,
0131                 &sp810->timerclken[i].hw);
0132         WARN_ON(IS_ERR(sp810->timerclken[i].clk));
0133     }
0134 
0135     of_clk_add_provider(node, clk_sp810_timerclken_of_get, sp810);
0136     instance++;
0137 }
0138 CLK_OF_DECLARE(sp810, "arm,sp810", clk_sp810_of_setup);