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 struct altera_timer_regs {
20 u32 status; /* Timer status reg */
21 u32 control; /* Timer control reg */
22 u32 periodl; /* Timeout period low */
23 u32 periodh; /* Timeout period high */
24 u32 snapl; /* Snapshot low */
25 u32 snaph; /* Snapshot high */
28 struct altera_timer_platdata {
29 struct altera_timer_regs *regs;
30 unsigned long clock_rate;
33 /* control register */
34 #define ALTERA_TIMER_CONT (1 << 1) /* Continuous mode */
35 #define ALTERA_TIMER_START (1 << 2) /* Start timer */
36 #define ALTERA_TIMER_STOP (1 << 3) /* Stop timer */
38 static int altera_timer_get_count(struct udevice *dev, unsigned long *count)
40 struct altera_timer_platdata *plat = dev->platdata;
41 struct altera_timer_regs *const regs = plat->regs;
45 writel(0x0, ®s->snapl);
47 /* Read timer value */
48 val = readl(®s->snapl) & 0xffff;
49 val |= (readl(®s->snaph) & 0xffff) << 16;
55 static int altera_timer_probe(struct udevice *dev)
57 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
58 struct altera_timer_platdata *plat = dev->platdata;
59 struct altera_timer_regs *const regs = plat->regs;
61 uc_priv->clock_rate = plat->clock_rate;
63 writel(0, ®s->status);
64 writel(0, ®s->control);
65 writel(ALTERA_TIMER_STOP, ®s->control);
67 writel(0xffff, ®s->periodl);
68 writel(0xffff, ®s->periodh);
69 writel(ALTERA_TIMER_CONT | ALTERA_TIMER_START, ®s->control);
74 static int altera_timer_ofdata_to_platdata(struct udevice *dev)
76 struct altera_timer_platdata *plat = dev_get_platdata(dev);
78 plat->regs = ioremap(dev_get_addr(dev),
79 sizeof(struct altera_timer_regs));
80 plat->clock_rate = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
81 "clock-frequency", 0);
86 static const struct timer_ops altera_timer_ops = {
87 .get_count = altera_timer_get_count,
90 static const struct udevice_id altera_timer_ids[] = {
91 { .compatible = "altr,timer-1.0", },
95 U_BOOT_DRIVER(altera_timer) = {
96 .name = "altera_timer",
98 .of_match = altera_timer_ids,
99 .ofdata_to_platdata = altera_timer_ofdata_to_platdata,
100 .platdata_auto_alloc_size = sizeof(struct altera_timer_platdata),
101 .probe = altera_timer_probe,
102 .ops = &altera_timer_ops,
103 .flags = DM_FLAG_PRE_RELOC,