| 1 | |
| 2 | |
| 3 | * Ralink System Tick Counter driver present on RT3352 and MT7620 SoCs.
|
| 4 | *
|
| 5 | * Copyright (C) 2013 by John Crispin <[email protected]>
|
| 6 |
|
| 7 |
|
| 8 | #include <linux/clockchips.h>
|
| 9 | #include <linux/clocksource.h>
|
| 10 | #include <linux/interrupt.h>
|
| 11 | #include <linux/reset.h>
|
| 12 | #include <linux/init.h>
|
| 13 | #include <linux/time.h>
|
| 14 | #include <linux/of.h>
|
| 15 | #include <linux/of_irq.h>
|
| 16 | #include <linux/of_address.h>
|
| 17 |
|
| 18 | #define SYSTICK_FREQ (50 * 1000)
|
| 19 |
|
| 20 | #define SYSTICK_CONFIG 0x00
|
| 21 | #define SYSTICK_COMPARE 0x04
|
| 22 | #define SYSTICK_COUNT 0x08
|
| 23 |
|
| 24 |
|
| 25 | #define CFG_EXT_STK_EN 0x2
|
| 26 |
|
| 27 | #define CFG_CNT_EN 0x1
|
| 28 |
|
| 29 | struct systick_device {
|
| 30 | void __iomem *membase;
|
| 31 | struct clock_event_device dev;
|
| 32 | int irq_requested;
|
| 33 | int freq_scale;
|
| 34 | };
|
| 35 |
|
| 36 | static int systick_set_oneshot(struct clock_event_device *evt);
|
| 37 | static int systick_shutdown(struct clock_event_device *evt);
|
| 38 |
|
| 39 | static int systick_next_event(unsigned long delta,
|
| 40 | struct clock_event_device *evt)
|
| 41 | {
|
| 42 | struct systick_device *sdev;
|
| 43 | u32 count;
|
| 44 |
|
| 45 | sdev = container_of(evt, struct systick_device, dev);
|
| 46 | count = ioread32(sdev->membase + SYSTICK_COUNT);
|
| 47 | count = (count + delta) % SYSTICK_FREQ;
|
| 48 | iowrite32(count, sdev->membase + SYSTICK_COMPARE);
|
| 49 |
|
| 50 | return 0;
|
| 51 | }
|
| 52 |
|
| 53 | static void systick_event_handler(struct clock_event_device *dev)
|
| 54 | {
|
| 55 |
|
| 56 | }
|
| 57 |
|
| 58 | static irqreturn_t systick_interrupt(int irq, void *dev_id)
|
| 59 | {
|
| 60 | struct clock_event_device *dev = (struct clock_event_device *)dev_id;
|
| 61 |
|
| 62 | dev->event_handler(dev);
|
| 63 |
|
| 64 | return IRQ_HANDLED;
|
| 65 | }
|
| 66 |
|
| 67 | static struct systick_device systick = {
|
| 68 | .dev = {
|
| 69 | |
| 70 | * cevt-r4k uses 300, make sure systick
|
| 71 | * gets used if available
|
| 72 |
|
| 73 | .rating = 310,
|
| 74 | .features = CLOCK_EVT_FEAT_ONESHOT,
|
| 75 | .set_next_event = systick_next_event,
|
| 76 | .set_state_shutdown = systick_shutdown,
|
| 77 | .set_state_oneshot = systick_set_oneshot,
|
| 78 | .event_handler = systick_event_handler,
|
| 79 | },
|
| 80 | };
|
| 81 |
|
| 82 | static int systick_shutdown(struct clock_event_device *evt)
|
| 83 | {
|
| 84 | struct systick_device *sdev;
|
| 85 |
|
| 86 | sdev = container_of(evt, struct systick_device, dev);
|
| 87 |
|
| 88 | if (sdev->irq_requested)
|
| 89 | free_irq(systick.dev.irq, &systick.dev);
|
| 90 | sdev->irq_requested = 0;
|
| 91 | iowrite32(0, systick.membase + SYSTICK_CONFIG);
|
| 92 |
|
| 93 | return 0;
|
| 94 | }
|
| 95 |
|
| 96 | static int systick_set_oneshot(struct clock_event_device *evt)
|
| 97 | {
|
| 98 | const char *name = systick.dev.name;
|
| 99 | struct systick_device *sdev;
|
| 100 | int irq = systick.dev.irq;
|
| 101 |
|
| 102 | sdev = container_of(evt, struct systick_device, dev);
|
| 103 |
|
| 104 | if (!sdev->irq_requested) {
|
| 105 | if (request_irq(irq, systick_interrupt,
|
| 106 | IRQF_PERCPU | IRQF_TIMER, name, &systick.dev))
|
| 107 | pr_err("Failed to request irq %d (%s)\n", irq, name);
|
| 108 | }
|
| 109 | sdev->irq_requested = 1;
|
| 110 | iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN,
|
| 111 | systick.membase + SYSTICK_CONFIG);
|
| 112 |
|
| 113 | return 0;
|
| 114 | }
|
| 115 |
|
| 116 | static int __init ralink_systick_init(struct device_node *np)
|
| 117 | {
|
| 118 | int ret;
|
| 119 |
|
| 120 | systick.membase = of_iomap(np, 0);
|
| 121 | if (!systick.membase)
|
| 122 | return -ENXIO;
|
| 123 |
|
| 124 | systick.dev.name = np->name;
|
| 125 | clockevents_calc_mult_shift(&systick.dev, SYSTICK_FREQ, 60);
|
| 126 | systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
|
| 127 | systick.dev.max_delta_ticks = 0x7fff;
|
| 128 | systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
|
| 129 | systick.dev.min_delta_ticks = 0x3;
|
| 130 | systick.dev.irq = irq_of_parse_and_map(np, 0);
|
| 131 | if (!systick.dev.irq) {
|
| 132 | pr_err("%pOFn: request_irq failed", np);
|
| 133 | ret = -EINVAL;
|
| 134 | goto err_iounmap;
|
| 135 | }
|
| 136 |
|
| 137 | ret = clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
|
| 138 | SYSTICK_FREQ, 301, 16,
|
| 139 | clocksource_mmio_readl_up);
|
| 140 | if (ret)
|
| 141 | goto err_free_irq;
|
| 142 |
|
| 143 | clockevents_register_device(&systick.dev);
|
| 144 |
|
| 145 | pr_info("%pOFn: running - mult: %d, shift: %d\n",
|
| 146 | np, systick.dev.mult, systick.dev.shift);
|
| 147 |
|
| 148 | return 0;
|
| 149 |
|
| 150 | err_free_irq:
|
| 151 | irq_dispose_mapping(systick.dev.irq);
|
| 152 | err_iounmap:
|
| 153 | iounmap(systick.membase);
|
| 154 | return ret;
|
| 155 | }
|
| 156 |
|
| 157 | TIMER_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);
|