Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 2 | /* |
| 3 | * Cirrus Logic CLPS711X clocksource driver |
| 4 | * |
| 5 | * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru> |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/clockchips.h> |
| 10 | #include <linux/clocksource.h> |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/of_address.h> |
| 14 | #include <linux/of_irq.h> |
| 15 | #include <linux/sched_clock.h> |
| 16 | #include <linux/slab.h> |
| 17 | |
| 18 | enum { |
| 19 | CLPS711X_CLKSRC_CLOCKSOURCE, |
| 20 | CLPS711X_CLKSRC_CLOCKEVENT, |
| 21 | }; |
| 22 | |
| 23 | static void __iomem *tcd; |
| 24 | |
| 25 | static u64 notrace clps711x_sched_clock_read(void) |
| 26 | { |
| 27 | return ~readw(tcd); |
| 28 | } |
| 29 | |
Alexander Shiyan | 2a6a8e2 | 2018-12-20 14:16:26 +0300 | [diff] [blame] | 30 | static void __init clps711x_clksrc_init(struct clk *clock, void __iomem *base) |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 31 | { |
Alexander Shiyan | 2a6a8e2 | 2018-12-20 14:16:26 +0300 | [diff] [blame] | 32 | unsigned long rate = clk_get_rate(clock); |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 33 | |
| 34 | tcd = base; |
| 35 | |
| 36 | clocksource_mmio_init(tcd, "clps711x-clocksource", rate, 300, 16, |
| 37 | clocksource_mmio_readw_down); |
| 38 | |
| 39 | sched_clock_register(clps711x_sched_clock_read, 16, rate); |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static irqreturn_t clps711x_timer_interrupt(int irq, void *dev_id) |
| 43 | { |
| 44 | struct clock_event_device *evt = dev_id; |
| 45 | |
| 46 | evt->event_handler(evt); |
| 47 | |
| 48 | return IRQ_HANDLED; |
| 49 | } |
| 50 | |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 51 | static int __init _clps711x_clkevt_init(struct clk *clock, void __iomem *base, |
| 52 | unsigned int irq) |
| 53 | { |
| 54 | struct clock_event_device *clkevt; |
| 55 | unsigned long rate; |
| 56 | |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 57 | clkevt = kzalloc(sizeof(*clkevt), GFP_KERNEL); |
| 58 | if (!clkevt) |
| 59 | return -ENOMEM; |
| 60 | |
| 61 | rate = clk_get_rate(clock); |
| 62 | |
| 63 | /* Set Timer prescaler */ |
| 64 | writew(DIV_ROUND_CLOSEST(rate, HZ), base); |
| 65 | |
| 66 | clkevt->name = "clps711x-clockevent"; |
| 67 | clkevt->rating = 300; |
| 68 | clkevt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_C3STOP; |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 69 | clkevt->cpumask = cpumask_of(0); |
| 70 | clockevents_config_and_register(clkevt, HZ, 0, 0); |
| 71 | |
| 72 | return request_irq(irq, clps711x_timer_interrupt, IRQF_TIMER, |
| 73 | "clps711x-timer", clkevt); |
| 74 | } |
| 75 | |
Daniel Lezcano | 04410ef | 2016-06-02 20:07:35 +0200 | [diff] [blame] | 76 | static int __init clps711x_timer_init(struct device_node *np) |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 77 | { |
| 78 | unsigned int irq = irq_of_parse_and_map(np, 0); |
| 79 | struct clk *clock = of_clk_get(np, 0); |
| 80 | void __iomem *base = of_iomap(np, 0); |
| 81 | |
Alexander Shiyan | 2a6a8e2 | 2018-12-20 14:16:26 +0300 | [diff] [blame] | 82 | if (!base) |
| 83 | return -ENOMEM; |
| 84 | if (!irq) |
| 85 | return -EINVAL; |
| 86 | if (IS_ERR(clock)) |
| 87 | return PTR_ERR(clock); |
| 88 | |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 89 | switch (of_alias_get_id(np, "timer")) { |
| 90 | case CLPS711X_CLKSRC_CLOCKSOURCE: |
Alexander Shiyan | 2a6a8e2 | 2018-12-20 14:16:26 +0300 | [diff] [blame] | 91 | clps711x_clksrc_init(clock, base); |
| 92 | break; |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 93 | case CLPS711X_CLKSRC_CLOCKEVENT: |
Daniel Lezcano | 04410ef | 2016-06-02 20:07:35 +0200 | [diff] [blame] | 94 | return _clps711x_clkevt_init(clock, base, irq); |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 95 | default: |
Daniel Lezcano | 04410ef | 2016-06-02 20:07:35 +0200 | [diff] [blame] | 96 | return -EINVAL; |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 97 | } |
Alexander Shiyan | 2a6a8e2 | 2018-12-20 14:16:26 +0300 | [diff] [blame] | 98 | |
| 99 | return 0; |
Alexander Shiyan | f0b7fab | 2014-07-13 08:45:13 +0400 | [diff] [blame] | 100 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 101 | TIMER_OF_DECLARE(clps711x, "cirrus,ep7209-timer", clps711x_timer_init); |