1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2002
6 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
14 #include <linux/bitops.h>
16 /* control register */
17 #define ALTERA_TIMER_CONT BIT(1) /* Continuous mode */
18 #define ALTERA_TIMER_START BIT(2) /* Start timer */
19 #define ALTERA_TIMER_STOP BIT(3) /* Stop timer */
21 struct altera_timer_regs {
22 u32 status; /* Timer status reg */
23 u32 control; /* Timer control reg */
24 u32 periodl; /* Timeout period low */
25 u32 periodh; /* Timeout period high */
26 u32 snapl; /* Snapshot low */
27 u32 snaph; /* Snapshot high */
30 struct altera_timer_plat {
31 struct altera_timer_regs *regs;
34 static u64 altera_timer_get_count(struct udevice *dev)
36 struct altera_timer_plat *plat = dev_get_plat(dev);
37 struct altera_timer_regs *const regs = plat->regs;
41 writel(0x0, ®s->snapl);
43 /* Read timer value */
44 val = readl(®s->snapl) & 0xffff;
45 val |= (readl(®s->snaph) & 0xffff) << 16;
46 return timer_conv_64(~val);
49 static int altera_timer_probe(struct udevice *dev)
51 struct altera_timer_plat *plat = dev_get_plat(dev);
52 struct altera_timer_regs *const regs = plat->regs;
54 writel(0, ®s->status);
55 writel(0, ®s->control);
56 writel(ALTERA_TIMER_STOP, ®s->control);
58 writel(0xffff, ®s->periodl);
59 writel(0xffff, ®s->periodh);
60 writel(ALTERA_TIMER_CONT | ALTERA_TIMER_START, ®s->control);
65 static int altera_timer_of_to_plat(struct udevice *dev)
67 struct altera_timer_plat *plat = dev_get_plat(dev);
69 plat->regs = map_physmem(dev_read_addr(dev),
70 sizeof(struct altera_timer_regs),
76 static const struct timer_ops altera_timer_ops = {
77 .get_count = altera_timer_get_count,
80 static const struct udevice_id altera_timer_ids[] = {
81 { .compatible = "altr,timer-1.0" },
85 U_BOOT_DRIVER(altera_timer) = {
86 .name = "altera_timer",
88 .of_match = altera_timer_ids,
89 .of_to_plat = altera_timer_of_to_plat,
90 .plat_auto = sizeof(struct altera_timer_plat),
91 .probe = altera_timer_probe,
92 .ops = &altera_timer_ops,