0001
0002
0003
0004
0005
0006
0007 #include <linux/clk.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/of.h>
0010 #include <linux/of_address.h>
0011 #include <linux/of_irq.h>
0012 #include <linux/slab.h>
0013
0014 #include "timer-of.h"
0015
0016
0017
0018
0019
0020
0021
0022 static __init void timer_of_irq_exit(struct of_timer_irq *of_irq)
0023 {
0024 struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
0025
0026 struct clock_event_device *clkevt = &to->clkevt;
0027
0028 if (of_irq->percpu)
0029 free_percpu_irq(of_irq->irq, clkevt);
0030 else
0031 free_irq(of_irq->irq, clkevt);
0032 }
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 static __init int timer_of_irq_init(struct device_node *np,
0051 struct of_timer_irq *of_irq)
0052 {
0053 int ret;
0054 struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
0055 struct clock_event_device *clkevt = &to->clkevt;
0056
0057 if (of_irq->name) {
0058 of_irq->irq = ret = of_irq_get_byname(np, of_irq->name);
0059 if (ret < 0) {
0060 pr_err("Failed to get interrupt %s for %pOF\n",
0061 of_irq->name, np);
0062 return ret;
0063 }
0064 } else {
0065 of_irq->irq = irq_of_parse_and_map(np, of_irq->index);
0066 }
0067 if (!of_irq->irq) {
0068 pr_err("Failed to map interrupt for %pOF\n", np);
0069 return -EINVAL;
0070 }
0071
0072 ret = of_irq->percpu ?
0073 request_percpu_irq(of_irq->irq, of_irq->handler,
0074 np->full_name, clkevt) :
0075 request_irq(of_irq->irq, of_irq->handler,
0076 of_irq->flags ? of_irq->flags : IRQF_TIMER,
0077 np->full_name, clkevt);
0078 if (ret) {
0079 pr_err("Failed to request irq %d for %pOF\n", of_irq->irq, np);
0080 return ret;
0081 }
0082
0083 clkevt->irq = of_irq->irq;
0084
0085 return 0;
0086 }
0087
0088
0089
0090
0091
0092
0093
0094 static __init void timer_of_clk_exit(struct of_timer_clk *of_clk)
0095 {
0096 of_clk->rate = 0;
0097 clk_disable_unprepare(of_clk->clk);
0098 clk_put(of_clk->clk);
0099 }
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110 static __init int timer_of_clk_init(struct device_node *np,
0111 struct of_timer_clk *of_clk)
0112 {
0113 int ret;
0114
0115 of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) :
0116 of_clk_get(np, of_clk->index);
0117 if (IS_ERR(of_clk->clk)) {
0118 ret = PTR_ERR(of_clk->clk);
0119 if (ret != -EPROBE_DEFER)
0120 pr_err("Failed to get clock for %pOF\n", np);
0121 goto out;
0122 }
0123
0124 ret = clk_prepare_enable(of_clk->clk);
0125 if (ret) {
0126 pr_err("Failed for enable clock for %pOF\n", np);
0127 goto out_clk_put;
0128 }
0129
0130 of_clk->rate = clk_get_rate(of_clk->clk);
0131 if (!of_clk->rate) {
0132 ret = -EINVAL;
0133 pr_err("Failed to get clock rate for %pOF\n", np);
0134 goto out_clk_disable;
0135 }
0136
0137 of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ);
0138 out:
0139 return ret;
0140
0141 out_clk_disable:
0142 clk_disable_unprepare(of_clk->clk);
0143 out_clk_put:
0144 clk_put(of_clk->clk);
0145
0146 goto out;
0147 }
0148
0149 static __init void timer_of_base_exit(struct of_timer_base *of_base)
0150 {
0151 iounmap(of_base->base);
0152 }
0153
0154 static __init int timer_of_base_init(struct device_node *np,
0155 struct of_timer_base *of_base)
0156 {
0157 of_base->base = of_base->name ?
0158 of_io_request_and_map(np, of_base->index, of_base->name) :
0159 of_iomap(np, of_base->index);
0160 if (IS_ERR_OR_NULL(of_base->base)) {
0161 pr_err("Failed to iomap (%s:%s)\n", np->name, of_base->name);
0162 return of_base->base ? PTR_ERR(of_base->base) : -ENOMEM;
0163 }
0164
0165 return 0;
0166 }
0167
0168 int __init timer_of_init(struct device_node *np, struct timer_of *to)
0169 {
0170 int ret = -EINVAL;
0171 int flags = 0;
0172
0173 if (to->flags & TIMER_OF_BASE) {
0174 ret = timer_of_base_init(np, &to->of_base);
0175 if (ret)
0176 goto out_fail;
0177 flags |= TIMER_OF_BASE;
0178 }
0179
0180 if (to->flags & TIMER_OF_CLOCK) {
0181 ret = timer_of_clk_init(np, &to->of_clk);
0182 if (ret)
0183 goto out_fail;
0184 flags |= TIMER_OF_CLOCK;
0185 }
0186
0187 if (to->flags & TIMER_OF_IRQ) {
0188 ret = timer_of_irq_init(np, &to->of_irq);
0189 if (ret)
0190 goto out_fail;
0191 flags |= TIMER_OF_IRQ;
0192 }
0193
0194 if (!to->clkevt.name)
0195 to->clkevt.name = np->full_name;
0196
0197 to->np = np;
0198
0199 return ret;
0200
0201 out_fail:
0202 if (flags & TIMER_OF_IRQ)
0203 timer_of_irq_exit(&to->of_irq);
0204
0205 if (flags & TIMER_OF_CLOCK)
0206 timer_of_clk_exit(&to->of_clk);
0207
0208 if (flags & TIMER_OF_BASE)
0209 timer_of_base_exit(&to->of_base);
0210 return ret;
0211 }
0212
0213
0214
0215
0216
0217
0218
0219
0220 void __init timer_of_cleanup(struct timer_of *to)
0221 {
0222 if (to->flags & TIMER_OF_IRQ)
0223 timer_of_irq_exit(&to->of_irq);
0224
0225 if (to->flags & TIMER_OF_CLOCK)
0226 timer_of_clk_exit(&to->of_clk);
0227
0228 if (to->flags & TIMER_OF_BASE)
0229 timer_of_base_exit(&to->of_base);
0230 }