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