blob: f4f6e903873979fa5e4a196196bbe885f90fd7cd [file] [log] [blame]
Mario Six2c217492018-08-06 10:23:38 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 */
6
7#include <common.h>
Mario Six2c217492018-08-06 10:23:38 +02008#include <clk.h>
9#include <dm.h>
Simon Glassc30b7ad2019-11-14 12:57:41 -070010#include <irq_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glassc3e44302019-11-14 12:57:11 -070012#include <status_led.h>
Simon Glass3a8ee3d2020-11-05 06:32:05 -070013#include <sysinfo.h>
Simon Glass036a0172019-11-14 12:57:27 -070014#include <time.h>
Mario Six2c217492018-08-06 10:23:38 +020015#include <timer.h>
16#include <watchdog.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass25a58182020-05-10 11:40:06 -060018#include <asm/ptrace.h>
Simon Glasscd93d622020-05-10 11:40:13 -060019#include <linux/bitops.h>
Mario Six2c217492018-08-06 10:23:38 +020020
21DECLARE_GLOBAL_DATA_PTR;
22
23/**
24 * struct mpc83xx_timer_priv - Private data structure for MPC83xx timer driver
25 * @decrementer_count: Value to which the decrementer register should be re-set
26 * to when a timer interrupt occurs, thus determines the
27 * interrupt frequency (value for 1e6/HZ microseconds)
28 * @timestamp: Counter for the number of timer interrupts that have
29 * occurred (i.e. can be used to trigger events
30 * periodically in the timer interrupt)
31 */
32struct mpc83xx_timer_priv {
33 uint decrementer_count;
34 ulong timestamp;
35};
36
37/*
38 * Bitmask for enabling the time base in the SPCR (System Priority
39 * Configuration Register)
40 */
41static const u32 SPCR_TBEN_MASK = BIT(31 - 9);
42
43/**
44 * get_dec() - Get the value of the decrementer register
45 *
46 * Return: The value of the decrementer register
47 */
48static inline unsigned long get_dec(void)
49{
50 unsigned long val;
51
52 asm volatile ("mfdec %0" : "=r" (val) : );
53
54 return val;
55}
56
57/**
58 * set_dec() - Set the value of the decrementer register
59 * @val: The value of the decrementer register to be set
60 */
61static inline void set_dec(unsigned long val)
62{
63 if (val)
64 asm volatile ("mtdec %0"::"r" (val));
65}
66
67/**
68 * mftbu() - Get value of TBU (upper time base) register
69 *
70 * Return: Value of the TBU register
71 */
72static inline u32 mftbu(void)
73{
74 u32 rval;
75
76 asm volatile("mftbu %0" : "=r" (rval));
77 return rval;
78}
79
80/**
81 * mftb() - Get value of TBL (lower time base) register
82 *
83 * Return: Value of the TBL register
84 */
85static inline u32 mftb(void)
86{
87 u32 rval;
88
89 asm volatile("mftb %0" : "=r" (rval));
90 return rval;
91}
92
93/*
94 * TODO(mario.six@gdsys.cc): This should really be done by timer_init, and the
95 * interrupt init should go into a interrupt driver.
96 */
97int interrupt_init(void)
98{
99 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
100 struct udevice *csb;
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700101 struct udevice *sysinfo;
Mario Six2c217492018-08-06 10:23:38 +0200102 struct udevice *timer;
103 struct mpc83xx_timer_priv *timer_priv;
104 struct clk clock;
105 int ret;
106
107 ret = uclass_first_device_err(UCLASS_TIMER, &timer);
108 if (ret) {
109 debug("%s: Could not find timer device (error: %d)",
110 __func__, ret);
111 return ret;
112 }
113
114 timer_priv = dev_get_priv(timer);
115
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700116 if (sysinfo_get(&sysinfo)) {
117 debug("%s: sysinfo device could not be fetched.\n", __func__);
Mario Six2c217492018-08-06 10:23:38 +0200118 return -ENOENT;
119 }
120
Simon Glass3a8ee3d2020-11-05 06:32:05 -0700121 ret = uclass_get_device_by_phandle(UCLASS_SIMPLE_BUS, sysinfo,
Mario Six2c217492018-08-06 10:23:38 +0200122 "csb", &csb);
123 if (ret) {
124 debug("%s: Could not retrieve CSB device (error: %d)",
125 __func__, ret);
126 return ret;
127 }
128
129 ret = clk_get_by_index(csb, 0, &clock);
130 if (ret) {
131 debug("%s: Could not retrieve clock (error: %d)",
132 __func__, ret);
133 return ret;
134 }
135
136 timer_priv->decrementer_count = (clk_get_rate(&clock) / 4)
137 / CONFIG_SYS_HZ;
138 /* Enable e300 time base */
139 setbits_be32(&immr->sysconf.spcr, SPCR_TBEN_MASK);
140
141 set_dec(timer_priv->decrementer_count);
142
143 /* Switch on interrupts */
144 set_msr(get_msr() | MSR_EE);
145
146 return 0;
147}
148
149/**
150 * timer_interrupt() - Handler for the timer interrupt
151 * @regs: Array of register values
152 */
153void timer_interrupt(struct pt_regs *regs)
154{
155 struct udevice *timer = gd->timer;
156 struct mpc83xx_timer_priv *priv;
157
158 /*
159 * During initialization, gd->timer might not be set yet, but the timer
160 * interrupt may already be enabled. In this case, wait for the
161 * initialization to complete
162 */
163 if (!timer)
164 return;
165
166 priv = dev_get_priv(timer);
167
168 /* Restore Decrementer Count */
169 set_dec(priv->decrementer_count);
170
171 priv->timestamp++;
172
173#if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
174 if ((timestamp % (CONFIG_SYS_WATCHDOG_FREQ)) == 0)
175 WATCHDOG_RESET();
176#endif /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
177
178#ifdef CONFIG_LED_STATUS
179 status_led_tick(priv->timestamp);
180#endif /* CONFIG_LED_STATUS */
Mario Six2c217492018-08-06 10:23:38 +0200181}
182
183void wait_ticks(ulong ticks)
184{
185 ulong end = get_ticks() + ticks;
186
187 while (end > get_ticks())
188 WATCHDOG_RESET();
189}
190
Sean Anderson8af7bb92020-10-07 14:37:44 -0400191static u64 mpc83xx_timer_get_count(struct udevice *dev)
Mario Six2c217492018-08-06 10:23:38 +0200192{
193 u32 tbu, tbl;
194
195 /*
196 * To make sure that no tbl overflow occurred between reading tbl and
197 * tbu, read tbu again, and compare it with the previously read tbu
198 * value: If they're different, a tbl overflow has occurred.
199 */
200 do {
201 tbu = mftbu();
202 tbl = mftb();
203 } while (tbu != mftbu());
204
Sean Anderson8af7bb92020-10-07 14:37:44 -0400205 return (tbu * 0x10000ULL) + tbl;
Mario Six2c217492018-08-06 10:23:38 +0200206}
207
208static int mpc83xx_timer_probe(struct udevice *dev)
209{
Simon Glass0fd3d912020-12-22 19:30:28 -0700210 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Mario Six2c217492018-08-06 10:23:38 +0200211 struct clk clock;
212 int ret;
213
214 ret = interrupt_init();
215 if (ret) {
216 debug("%s: interrupt_init failed (err = %d)\n",
217 dev->name, ret);
218 return ret;
219 }
220
221 ret = clk_get_by_index(dev, 0, &clock);
222 if (ret) {
223 debug("%s: Could not retrieve clock (err = %d)\n",
224 dev->name, ret);
225 return ret;
226 }
227
228 uc_priv->clock_rate = (clk_get_rate(&clock) + 3L) / 4L;
229
230 return 0;
231}
232
233static const struct timer_ops mpc83xx_timer_ops = {
234 .get_count = mpc83xx_timer_get_count,
235};
236
237static const struct udevice_id mpc83xx_timer_ids[] = {
238 { .compatible = "fsl,mpc83xx-timer" },
239 { /* sentinel */ }
240};
241
242U_BOOT_DRIVER(mpc83xx_timer) = {
243 .name = "mpc83xx_timer",
244 .id = UCLASS_TIMER,
245 .of_match = mpc83xx_timer_ids,
246 .probe = mpc83xx_timer_probe,
247 .ops = &mpc83xx_timer_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700248 .priv_auto = sizeof(struct mpc83xx_timer_priv),
Mario Six2c217492018-08-06 10:23:38 +0200249};