2 * Copyright 2016 Google Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/arch/timer.h>
14 DECLARE_GLOBAL_DATA_PTR
;
16 #define AST_TICK_TIMER 1
17 #define AST_TMC_RELOAD_VAL 0xffffffff
19 struct ast_timer_priv
{
20 struct ast_timer
*regs
;
21 struct ast_timer_counter
*tmc
;
24 static struct ast_timer_counter
*ast_get_timer_counter(struct ast_timer
*timer
,
28 return &timer
->timers2
[n
- 4];
30 return &timer
->timers1
[n
- 1];
33 static int ast_timer_probe(struct udevice
*dev
)
35 struct ast_timer_priv
*priv
= dev_get_priv(dev
);
36 struct timer_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
38 writel(AST_TMC_RELOAD_VAL
, &priv
->tmc
->reload_val
);
41 * Stop the timer. This will also load reload_val into
42 * the status register.
44 clrbits_le32(&priv
->regs
->ctrl1
,
45 AST_TMC_EN
<< AST_TMC_CTRL1_SHIFT(AST_TICK_TIMER
));
46 /* Start the timer from the fixed 1MHz clock. */
47 setbits_le32(&priv
->regs
->ctrl1
,
48 (AST_TMC_EN
| AST_TMC_1MHZ
) <<
49 AST_TMC_CTRL1_SHIFT(AST_TICK_TIMER
));
51 uc_priv
->clock_rate
= AST_TMC_RATE
;
56 static int ast_timer_get_count(struct udevice
*dev
, u64
*count
)
58 struct ast_timer_priv
*priv
= dev_get_priv(dev
);
60 *count
= AST_TMC_RELOAD_VAL
- readl(&priv
->tmc
->status
);
65 static int ast_timer_ofdata_to_platdata(struct udevice
*dev
)
67 struct ast_timer_priv
*priv
= dev_get_priv(dev
);
69 priv
->regs
= dev_get_addr_ptr(dev
);
70 if (IS_ERR(priv
->regs
))
71 return PTR_ERR(priv
->regs
);
73 priv
->tmc
= ast_get_timer_counter(priv
->regs
, AST_TICK_TIMER
);
78 static const struct timer_ops ast_timer_ops
= {
79 .get_count
= ast_timer_get_count
,
82 static const struct udevice_id ast_timer_ids
[] = {
83 { .compatible
= "aspeed,ast2500-timer" },
84 { .compatible
= "aspeed,ast2400-timer" },
88 U_BOOT_DRIVER(ast_timer
) = {
91 .of_match
= ast_timer_ids
,
92 .probe
= ast_timer_probe
,
93 .priv_auto_alloc_size
= sizeof(struct ast_timer_priv
),
94 .ofdata_to_platdata
= ast_timer_ofdata_to_platdata
,
95 .ops
= &ast_timer_ops
,
96 .flags
= DM_FLAG_PRE_RELOC
,