Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2017, Linaro Ltd.  All rights reserved.
0004  *
0005  * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
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  * timer_of_irq_exit - Release the interrupt
0018  * @of_irq: an of_timer_irq structure pointer
0019  *
0020  * Free the irq resource
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  * timer_of_irq_init - Request the interrupt
0036  * @np: a device tree node pointer
0037  * @of_irq: an of_timer_irq structure pointer
0038  *
0039  * Get the interrupt number from the DT from its definition and
0040  * request it. The interrupt is gotten by falling back the following way:
0041  *
0042  * - Get interrupt number by name
0043  * - Get interrupt number by index
0044  *
0045  * When the interrupt is per CPU, 'request_percpu_irq()' is called,
0046  * otherwise 'request_irq()' is used.
0047  *
0048  * Returns 0 on success, < 0 otherwise
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  * timer_of_clk_exit - Release the clock resources
0090  * @of_clk: a of_timer_clk structure pointer
0091  *
0092  * Disables and releases the refcount on the clk
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  * timer_of_clk_init - Initialize the clock resources
0103  * @np: a device tree node pointer
0104  * @of_clk: a of_timer_clk structure pointer
0105  *
0106  * Get the clock by name or by index, enable it and get the rate
0107  *
0108  * Returns 0 on success, < 0 otherwise
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  * timer_of_cleanup - release timer_of resources
0215  * @to: timer_of structure
0216  *
0217  * Release the resources that has been used in timer_of_init().
0218  * This function should be called in init error cases
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 }