2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
6 * Scott McNutt <smcnutt@psyent.com>
8 * SPDX-License-Identifier: GPL-2.0+
17 DECLARE_GLOBAL_DATA_PTR;
19 /* control register */
20 #define ALTERA_TIMER_CONT BIT(1) /* Continuous mode */
21 #define ALTERA_TIMER_START BIT(2) /* Start timer */
22 #define ALTERA_TIMER_STOP BIT(3) /* Stop timer */
24 struct altera_timer_regs {
25 u32 status; /* Timer status reg */
26 u32 control; /* Timer control reg */
27 u32 periodl; /* Timeout period low */
28 u32 periodh; /* Timeout period high */
29 u32 snapl; /* Snapshot low */
30 u32 snaph; /* Snapshot high */
33 struct altera_timer_platdata {
34 struct altera_timer_regs *regs;
37 static int altera_timer_get_count(struct udevice *dev, u64 *count)
39 struct altera_timer_platdata *plat = dev->platdata;
40 struct altera_timer_regs *const regs = plat->regs;
44 writel(0x0, ®s->snapl);
46 /* Read timer value */
47 val = readl(®s->snapl) & 0xffff;
48 val |= (readl(®s->snaph) & 0xffff) << 16;
49 *count = timer_conv_64(~val);
54 static int altera_timer_probe(struct udevice *dev)
56 struct altera_timer_platdata *plat = dev->platdata;
57 struct altera_timer_regs *const regs = plat->regs;
59 writel(0, ®s->status);
60 writel(0, ®s->control);
61 writel(ALTERA_TIMER_STOP, ®s->control);
63 writel(0xffff, ®s->periodl);
64 writel(0xffff, ®s->periodh);
65 writel(ALTERA_TIMER_CONT | ALTERA_TIMER_START, ®s->control);
70 static int altera_timer_ofdata_to_platdata(struct udevice *dev)
72 struct altera_timer_platdata *plat = dev_get_platdata(dev);
74 plat->regs = map_physmem(devfdt_get_addr(dev),
75 sizeof(struct altera_timer_regs),
81 static const struct timer_ops altera_timer_ops = {
82 .get_count = altera_timer_get_count,
85 static const struct udevice_id altera_timer_ids[] = {
86 { .compatible = "altr,timer-1.0" },
90 U_BOOT_DRIVER(altera_timer) = {
91 .name = "altera_timer",
93 .of_match = altera_timer_ids,
94 .ofdata_to_platdata = altera_timer_ofdata_to_platdata,
95 .platdata_auto_alloc_size = sizeof(struct altera_timer_platdata),
96 .probe = altera_timer_probe,
97 .ops = &altera_timer_ops,
98 .flags = DM_FLAG_PRE_RELOC,