Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2016 Google Inc. |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <errno.h> |
| 8 | #include <timer.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <asm/arch/timer.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 11 | #include <linux/err.h> |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 12 | |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 13 | #define AST_TICK_TIMER 1 |
| 14 | #define AST_TMC_RELOAD_VAL 0xffffffff |
| 15 | |
| 16 | struct ast_timer_priv { |
| 17 | struct ast_timer *regs; |
| 18 | struct ast_timer_counter *tmc; |
| 19 | }; |
| 20 | |
| 21 | static struct ast_timer_counter *ast_get_timer_counter(struct ast_timer *timer, |
| 22 | int n) |
| 23 | { |
| 24 | if (n > 3) |
| 25 | return &timer->timers2[n - 4]; |
| 26 | else |
| 27 | return &timer->timers1[n - 1]; |
| 28 | } |
| 29 | |
| 30 | static int ast_timer_probe(struct udevice *dev) |
| 31 | { |
| 32 | struct ast_timer_priv *priv = dev_get_priv(dev); |
| 33 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 34 | |
| 35 | writel(AST_TMC_RELOAD_VAL, &priv->tmc->reload_val); |
| 36 | |
| 37 | /* |
| 38 | * Stop the timer. This will also load reload_val into |
| 39 | * the status register. |
| 40 | */ |
| 41 | clrbits_le32(&priv->regs->ctrl1, |
| 42 | AST_TMC_EN << AST_TMC_CTRL1_SHIFT(AST_TICK_TIMER)); |
| 43 | /* Start the timer from the fixed 1MHz clock. */ |
| 44 | setbits_le32(&priv->regs->ctrl1, |
| 45 | (AST_TMC_EN | AST_TMC_1MHZ) << |
| 46 | AST_TMC_CTRL1_SHIFT(AST_TICK_TIMER)); |
| 47 | |
| 48 | uc_priv->clock_rate = AST_TMC_RATE; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 53 | static u64 ast_timer_get_count(struct udevice *dev) |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 54 | { |
| 55 | struct ast_timer_priv *priv = dev_get_priv(dev); |
| 56 | |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 57 | return AST_TMC_RELOAD_VAL - readl(&priv->tmc->status); |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 60 | static int ast_timer_of_to_plat(struct udevice *dev) |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 61 | { |
| 62 | struct ast_timer_priv *priv = dev_get_priv(dev); |
| 63 | |
Masahiro Yamada | 32822d0 | 2020-08-04 14:14:43 +0900 | [diff] [blame] | 64 | priv->regs = dev_read_addr_ptr(dev); |
Ovidiu Panait | a633f00 | 2020-08-03 22:17:35 +0300 | [diff] [blame] | 65 | if (!priv->regs) |
| 66 | return -EINVAL; |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 67 | |
| 68 | priv->tmc = ast_get_timer_counter(priv->regs, AST_TICK_TIMER); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static const struct timer_ops ast_timer_ops = { |
| 74 | .get_count = ast_timer_get_count, |
| 75 | }; |
| 76 | |
| 77 | static const struct udevice_id ast_timer_ids[] = { |
| 78 | { .compatible = "aspeed,ast2500-timer" }, |
| 79 | { .compatible = "aspeed,ast2400-timer" }, |
| 80 | { } |
| 81 | }; |
| 82 | |
| 83 | U_BOOT_DRIVER(ast_timer) = { |
| 84 | .name = "ast_timer", |
| 85 | .id = UCLASS_TIMER, |
| 86 | .of_match = ast_timer_ids, |
| 87 | .probe = ast_timer_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 88 | .priv_auto = sizeof(struct ast_timer_priv), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 89 | .of_to_plat = ast_timer_of_to_plat, |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 90 | .ops = &ast_timer_ops, |
maxims@google.com | f57bd00 | 2017-01-18 13:44:55 -0800 | [diff] [blame] | 91 | }; |