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