xref: /openbmc/u-boot/drivers/timer/ast_timer.c (revision 3559028c)
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <timer.h>
11 #include <asm/io.h>
12 #include <asm/arch/timer.h>
13 
14 #define AST_TICK_TIMER  1
15 #define AST_TMC_RELOAD_VAL  0xffffffff
16 
17 struct ast_timer_priv {
18 	struct ast_timer *regs;
19 	struct ast_timer_counter *tmc;
20 };
21 
22 static struct ast_timer_counter *ast_get_timer_counter(struct ast_timer *timer,
23 						       int n)
24 {
25 	if (n > 3)
26 		return &timer->timers2[n - 4];
27 	else
28 		return &timer->timers1[n - 1];
29 }
30 
31 static int ast_timer_probe(struct udevice *dev)
32 {
33 	struct ast_timer_priv *priv = dev_get_priv(dev);
34 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
35 
36 	writel(AST_TMC_RELOAD_VAL, &priv->tmc->reload_val);
37 
38 	/*
39 	 * Stop the timer. This will also load reload_val into
40 	 * the status register.
41 	 */
42 	clrbits_le32(&priv->regs->ctrl1,
43 		     AST_TMC_EN << AST_TMC_CTRL1_SHIFT(AST_TICK_TIMER));
44 	/* Start the timer from the fixed 1MHz clock. */
45 	setbits_le32(&priv->regs->ctrl1,
46 		     (AST_TMC_EN | AST_TMC_1MHZ) <<
47 		     AST_TMC_CTRL1_SHIFT(AST_TICK_TIMER));
48 
49 	uc_priv->clock_rate = AST_TMC_RATE;
50 
51 	return 0;
52 }
53 
54 static int ast_timer_get_count(struct udevice *dev, u64 *count)
55 {
56 	struct ast_timer_priv *priv = dev_get_priv(dev);
57 
58 	*count = AST_TMC_RELOAD_VAL - readl(&priv->tmc->status);
59 
60 	return 0;
61 }
62 
63 static int ast_timer_ofdata_to_platdata(struct udevice *dev)
64 {
65 	struct ast_timer_priv *priv = dev_get_priv(dev);
66 
67 	priv->regs = devfdt_get_addr_ptr(dev);
68 	if (IS_ERR(priv->regs))
69 		return PTR_ERR(priv->regs);
70 
71 	priv->tmc = ast_get_timer_counter(priv->regs, AST_TICK_TIMER);
72 
73 	return 0;
74 }
75 
76 static const struct timer_ops ast_timer_ops = {
77 	.get_count = ast_timer_get_count,
78 };
79 
80 static const struct udevice_id ast_timer_ids[] = {
81 	{ .compatible = "aspeed,ast2500-timer" },
82 	{ .compatible = "aspeed,ast2400-timer" },
83 	{ }
84 };
85 
86 U_BOOT_DRIVER(ast_timer) = {
87 	.name = "ast_timer",
88 	.id = UCLASS_TIMER,
89 	.of_match = ast_timer_ids,
90 	.probe = ast_timer_probe,
91 	.priv_auto_alloc_size = sizeof(struct ast_timer_priv),
92 	.ofdata_to_platdata = ast_timer_ofdata_to_platdata,
93 	.ops = &ast_timer_ops,
94 	.flags = DM_FLAG_PRE_RELOC,
95 };
96