Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Theobroma Systems Design und Consulting GmbH |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 52f2423 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 7 | #include <bootstage.h> |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 691d719 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 9 | #include <init.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Philipp Tomsich | cc7ce94 | 2017-09-11 22:04:16 +0200 | [diff] [blame] | 12 | #include <dm/ofnode.h> |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 13 | #include <mapmem.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 14 | #include <asm/arch-rockchip/timer.h> |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 15 | #include <dt-structs.h> |
| 16 | #include <timer.h> |
| 17 | #include <asm/io.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 22 | struct rockchip_timer_plat { |
| 23 | struct dtd_rockchip_rk3368_timer dtd; |
| 24 | }; |
| 25 | #endif |
| 26 | |
| 27 | /* Driver private data. Contains timer id. Could be either 0 or 1. */ |
| 28 | struct rockchip_timer_priv { |
| 29 | struct rk_timer *timer; |
| 30 | }; |
| 31 | |
Philipp Tomsich | cc7ce94 | 2017-09-11 22:04:16 +0200 | [diff] [blame] | 32 | static inline int64_t rockchip_timer_get_curr_value(struct rk_timer *timer) |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 33 | { |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 34 | uint64_t timebase_h, timebase_l; |
| 35 | uint64_t cntr; |
| 36 | |
Philipp Tomsich | cc7ce94 | 2017-09-11 22:04:16 +0200 | [diff] [blame] | 37 | timebase_l = readl(&timer->timer_curr_value0); |
| 38 | timebase_h = readl(&timer->timer_curr_value1); |
| 39 | |
| 40 | cntr = timebase_h << 32 | timebase_l; |
| 41 | return cntr; |
| 42 | } |
| 43 | |
| 44 | #if CONFIG_IS_ENABLED(BOOTSTAGE) |
| 45 | ulong timer_get_boot_us(void) |
| 46 | { |
| 47 | uint64_t ticks = 0; |
| 48 | uint32_t rate; |
| 49 | uint64_t us; |
| 50 | int ret; |
| 51 | |
| 52 | ret = dm_timer_init(); |
| 53 | |
| 54 | if (!ret) { |
| 55 | /* The timer is available */ |
| 56 | rate = timer_get_rate(gd->timer); |
| 57 | timer_get_count(gd->timer, &ticks); |
Simon Glass | dcfc42b | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 58 | } else if (CONFIG_IS_ENABLED(OF_REAL) && ret == -EAGAIN) { |
Philipp Tomsich | cc7ce94 | 2017-09-11 22:04:16 +0200 | [diff] [blame] | 59 | /* We have been called so early that the DM is not ready,... */ |
| 60 | ofnode node = offset_to_ofnode(-1); |
| 61 | struct rk_timer *timer = NULL; |
| 62 | |
| 63 | /* |
| 64 | * ... so we try to access the raw timer, if it is specified |
| 65 | * via the tick-timer property in /chosen. |
| 66 | */ |
| 67 | node = ofnode_get_chosen_node("tick-timer"); |
| 68 | if (!ofnode_valid(node)) { |
| 69 | debug("%s: no /chosen/tick-timer\n", __func__); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | timer = (struct rk_timer *)ofnode_get_addr(node); |
| 74 | |
| 75 | /* This timer is down-counting */ |
| 76 | ticks = ~0uLL - rockchip_timer_get_curr_value(timer); |
| 77 | if (ofnode_read_u32(node, "clock-frequency", &rate)) { |
| 78 | debug("%s: could not read clock-frequency\n", __func__); |
| 79 | return 0; |
| 80 | } |
Philipp Tomsich | cc7ce94 | 2017-09-11 22:04:16 +0200 | [diff] [blame] | 81 | } else { |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | us = (ticks * 1000) / rate; |
| 86 | return us; |
| 87 | } |
| 88 | #endif |
| 89 | |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 90 | static u64 rockchip_timer_get_count(struct udevice *dev) |
Philipp Tomsich | cc7ce94 | 2017-09-11 22:04:16 +0200 | [diff] [blame] | 91 | { |
| 92 | struct rockchip_timer_priv *priv = dev_get_priv(dev); |
| 93 | uint64_t cntr = rockchip_timer_get_curr_value(priv->timer); |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 94 | |
| 95 | /* timers are down-counting */ |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 96 | return ~0ull - cntr; |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 99 | static int rockchip_clk_of_to_plat(struct udevice *dev) |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 100 | { |
Simon Glass | dcfc42b | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 101 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
| 102 | struct rockchip_timer_priv *priv = dev_get_priv(dev); |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 103 | |
Simon Glass | dcfc42b | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 104 | priv->timer = dev_read_addr_ptr(dev); |
| 105 | if (!priv->timer) |
| 106 | return -ENOENT; |
| 107 | } |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int rockchip_timer_start(struct udevice *dev) |
| 113 | { |
| 114 | struct rockchip_timer_priv *priv = dev_get_priv(dev); |
| 115 | const uint64_t reload_val = ~0uLL; |
| 116 | const uint32_t reload_val_l = reload_val & 0xffffffff; |
| 117 | const uint32_t reload_val_h = reload_val >> 32; |
| 118 | |
Philipp Tomsich | cc7ce94 | 2017-09-11 22:04:16 +0200 | [diff] [blame] | 119 | /* don't reinit, if the timer is already running and set up */ |
| 120 | if ((readl(&priv->timer->timer_ctrl_reg) & 1) == 1 && |
| 121 | (readl(&priv->timer->timer_load_count0) == reload_val_l) && |
| 122 | (readl(&priv->timer->timer_load_count1) == reload_val_h)) |
| 123 | return 0; |
| 124 | |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 125 | /* disable timer and reset all control */ |
| 126 | writel(0, &priv->timer->timer_ctrl_reg); |
| 127 | /* write reload value */ |
| 128 | writel(reload_val_l, &priv->timer->timer_load_count0); |
| 129 | writel(reload_val_h, &priv->timer->timer_load_count1); |
| 130 | /* enable timer */ |
| 131 | writel(1, &priv->timer->timer_ctrl_reg); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int rockchip_timer_probe(struct udevice *dev) |
| 137 | { |
| 138 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 139 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 140 | struct rockchip_timer_priv *priv = dev_get_priv(dev); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 141 | struct rockchip_timer_plat *plat = dev_get_plat(dev); |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 142 | |
Philipp Tomsich | 8158a84 | 2017-08-14 19:05:31 +0200 | [diff] [blame] | 143 | priv->timer = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]); |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 144 | uc_priv->clock_rate = plat->dtd.clock_frequency; |
| 145 | #endif |
| 146 | |
| 147 | return rockchip_timer_start(dev); |
| 148 | } |
| 149 | |
| 150 | static const struct timer_ops rockchip_timer_ops = { |
| 151 | .get_count = rockchip_timer_get_count, |
| 152 | }; |
| 153 | |
| 154 | static const struct udevice_id rockchip_timer_ids[] = { |
Philipp Tomsich | e0e1d3f | 2018-04-25 14:07:06 +0200 | [diff] [blame] | 155 | { .compatible = "rockchip,rk3188-timer" }, |
| 156 | { .compatible = "rockchip,rk3288-timer" }, |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 157 | { .compatible = "rockchip,rk3368-timer" }, |
| 158 | {} |
| 159 | }; |
| 160 | |
Philipp Tomsich | 5798d50 | 2017-08-25 13:22:00 +0200 | [diff] [blame] | 161 | U_BOOT_DRIVER(rockchip_rk3368_timer) = { |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 162 | .name = "rockchip_rk3368_timer", |
| 163 | .id = UCLASS_TIMER, |
| 164 | .of_match = rockchip_timer_ids, |
| 165 | .probe = rockchip_timer_probe, |
| 166 | .ops = &rockchip_timer_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 167 | .priv_auto = sizeof(struct rockchip_timer_priv), |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 168 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 169 | .plat_auto = sizeof(struct rockchip_timer_plat), |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 170 | #endif |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 171 | .of_to_plat = rockchip_clk_of_to_plat, |
Philipp Tomsich | 1168d2d | 2017-07-28 17:43:19 +0200 | [diff] [blame] | 172 | }; |