blob: 7e9abee0efc77df5b0a1368be9f7a83101fcd307 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Thomas Choua54915d2015-10-22 22:28:53 +08002/*
3 * (C) Copyright 2000-2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
7 * Scott McNutt <smcnutt@psyent.com>
Thomas Choua54915d2015-10-22 22:28:53 +08008 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
13#include <timer.h>
14#include <asm/io.h>
Simon Glasscd93d622020-05-10 11:40:13 -060015#include <linux/bitops.h>
Thomas Choua54915d2015-10-22 22:28:53 +080016
Thomas Chou1235e5a2015-10-31 20:54:16 +080017/* control register */
18#define ALTERA_TIMER_CONT BIT(1) /* Continuous mode */
19#define ALTERA_TIMER_START BIT(2) /* Start timer */
20#define ALTERA_TIMER_STOP BIT(3) /* Stop timer */
21
Thomas Choua54915d2015-10-22 22:28:53 +080022struct altera_timer_regs {
23 u32 status; /* Timer status reg */
24 u32 control; /* Timer control reg */
25 u32 periodl; /* Timeout period low */
26 u32 periodh; /* Timeout period high */
27 u32 snapl; /* Snapshot low */
28 u32 snaph; /* Snapshot high */
29};
30
Simon Glass8a8d24b2020-12-03 16:55:23 -070031struct altera_timer_plat {
Thomas Choua54915d2015-10-22 22:28:53 +080032 struct altera_timer_regs *regs;
Thomas Choua54915d2015-10-22 22:28:53 +080033};
34
Sean Anderson8af7bb92020-10-07 14:37:44 -040035static u64 altera_timer_get_count(struct udevice *dev)
Thomas Choua54915d2015-10-22 22:28:53 +080036{
Simon Glass8a8d24b2020-12-03 16:55:23 -070037 struct altera_timer_plat *plat = dev->plat;
Thomas Choua54915d2015-10-22 22:28:53 +080038 struct altera_timer_regs *const regs = plat->regs;
39 u32 val;
40
41 /* Trigger update */
42 writel(0x0, &regs->snapl);
43
44 /* Read timer value */
45 val = readl(&regs->snapl) & 0xffff;
46 val |= (readl(&regs->snaph) & 0xffff) << 16;
Sean Anderson8af7bb92020-10-07 14:37:44 -040047 return timer_conv_64(~val);
Thomas Choua54915d2015-10-22 22:28:53 +080048}
49
50static int altera_timer_probe(struct udevice *dev)
51{
Simon Glass8a8d24b2020-12-03 16:55:23 -070052 struct altera_timer_plat *plat = dev->plat;
Thomas Choua54915d2015-10-22 22:28:53 +080053 struct altera_timer_regs *const regs = plat->regs;
54
Thomas Choua54915d2015-10-22 22:28:53 +080055 writel(0, &regs->status);
56 writel(0, &regs->control);
57 writel(ALTERA_TIMER_STOP, &regs->control);
58
59 writel(0xffff, &regs->periodl);
60 writel(0xffff, &regs->periodh);
61 writel(ALTERA_TIMER_CONT | ALTERA_TIMER_START, &regs->control);
62
63 return 0;
64}
65
Simon Glassd1998a92020-12-03 16:55:21 -070066static int altera_timer_of_to_plat(struct udevice *dev)
Thomas Choua54915d2015-10-22 22:28:53 +080067{
Simon Glass8a8d24b2020-12-03 16:55:23 -070068 struct altera_timer_plat *plat = dev_get_plat(dev);
Thomas Choua54915d2015-10-22 22:28:53 +080069
Masahiro Yamada25484932020-07-17 14:36:48 +090070 plat->regs = map_physmem(dev_read_addr(dev),
Thomas Chou4c26ec12015-11-14 11:15:31 +080071 sizeof(struct altera_timer_regs),
72 MAP_NOCACHE);
Thomas Choua54915d2015-10-22 22:28:53 +080073
74 return 0;
75}
76
77static const struct timer_ops altera_timer_ops = {
78 .get_count = altera_timer_get_count,
79};
80
81static const struct udevice_id altera_timer_ids[] = {
Thomas Chou1235e5a2015-10-31 20:54:16 +080082 { .compatible = "altr,timer-1.0" },
83 {}
Thomas Choua54915d2015-10-22 22:28:53 +080084};
85
86U_BOOT_DRIVER(altera_timer) = {
87 .name = "altera_timer",
88 .id = UCLASS_TIMER,
89 .of_match = altera_timer_ids,
Simon Glassd1998a92020-12-03 16:55:21 -070090 .of_to_plat = altera_timer_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -070091 .plat_auto = sizeof(struct altera_timer_plat),
Thomas Choua54915d2015-10-22 22:28:53 +080092 .probe = altera_timer_probe,
93 .ops = &altera_timer_ops,
Thomas Choua54915d2015-10-22 22:28:53 +080094};