1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
4 * Hirschmann Hellcreek TSN switch.
6 * Copyright (C) 2019,2020 Hochschule Offenburg
7 * Copyright (C) 2019,2020 Linutronix GmbH
8 * Authors: Kamil Alkhouri <kamil.alkhouri@hs-offenburg.de>
9 * Kurt Kanzenbach <kurt@linutronix.de>
13 #include <linux/ptp_clock_kernel.h>
14 #include "hellcreek.h"
15 #include "hellcreek_ptp.h"
16 #include "hellcreek_hwtstamp.h"
18 u16 hellcreek_ptp_read(struct hellcreek *hellcreek, unsigned int offset)
20 return readw(hellcreek->ptp_base + offset);
23 void hellcreek_ptp_write(struct hellcreek *hellcreek, u16 data,
26 writew(data, hellcreek->ptp_base + offset);
29 /* Get nanoseconds from PTP clock */
30 static u64 hellcreek_ptp_clock_read(struct hellcreek *hellcreek)
35 hellcreek_ptp_write(hellcreek, PR_COMMAND_C_SS, PR_COMMAND_C);
37 /* The time of the day is saved as 96 bits. However, due to hardware
38 * limitations the seconds are not or only partly kept in the PTP
39 * core. Currently only three bits for the seconds are available. That's
40 * why only the nanoseconds are used and the seconds are tracked in
41 * software. Anyway due to internal locking all five registers should be
44 nsh = hellcreek_ptp_read(hellcreek, PR_SS_SYNC_DATA_C);
45 nsh = hellcreek_ptp_read(hellcreek, PR_SS_SYNC_DATA_C);
46 nsh = hellcreek_ptp_read(hellcreek, PR_SS_SYNC_DATA_C);
47 nsh = hellcreek_ptp_read(hellcreek, PR_SS_SYNC_DATA_C);
48 nsl = hellcreek_ptp_read(hellcreek, PR_SS_SYNC_DATA_C);
50 return (u64)nsl | ((u64)nsh << 16);
53 static u64 __hellcreek_ptp_gettime(struct hellcreek *hellcreek)
57 ns = hellcreek_ptp_clock_read(hellcreek);
58 if (ns < hellcreek->last_ts)
60 hellcreek->last_ts = ns;
61 ns += hellcreek->seconds * NSEC_PER_SEC;
66 /* Retrieve the seconds parts in nanoseconds for a packet timestamped with @ns.
67 * There has to be a check whether an overflow occurred between the packet
68 * arrival and now. If so use the correct seconds (-1) for calculating the
69 * packet arrival time.
71 u64 hellcreek_ptp_gettime_seconds(struct hellcreek *hellcreek, u64 ns)
75 __hellcreek_ptp_gettime(hellcreek);
76 if (hellcreek->last_ts > ns)
77 s = hellcreek->seconds * NSEC_PER_SEC;
79 s = (hellcreek->seconds - 1) * NSEC_PER_SEC;
84 static int hellcreek_ptp_gettime(struct ptp_clock_info *ptp,
85 struct timespec64 *ts)
87 struct hellcreek *hellcreek = ptp_to_hellcreek(ptp);
90 mutex_lock(&hellcreek->ptp_lock);
91 ns = __hellcreek_ptp_gettime(hellcreek);
92 mutex_unlock(&hellcreek->ptp_lock);
94 *ts = ns_to_timespec64(ns);
99 static int hellcreek_ptp_settime(struct ptp_clock_info *ptp,
100 const struct timespec64 *ts)
102 struct hellcreek *hellcreek = ptp_to_hellcreek(ptp);
105 secl = ts->tv_sec & 0xffff;
106 nsh = ((u32)ts->tv_nsec & 0xffff0000) >> 16;
107 nsl = ts->tv_nsec & 0xffff;
109 mutex_lock(&hellcreek->ptp_lock);
111 /* Update overflow data structure */
112 hellcreek->seconds = ts->tv_sec;
113 hellcreek->last_ts = ts->tv_nsec;
115 /* Set time in clock */
116 hellcreek_ptp_write(hellcreek, 0x00, PR_CLOCK_WRITE_C);
117 hellcreek_ptp_write(hellcreek, 0x00, PR_CLOCK_WRITE_C);
118 hellcreek_ptp_write(hellcreek, secl, PR_CLOCK_WRITE_C);
119 hellcreek_ptp_write(hellcreek, nsh, PR_CLOCK_WRITE_C);
120 hellcreek_ptp_write(hellcreek, nsl, PR_CLOCK_WRITE_C);
122 mutex_unlock(&hellcreek->ptp_lock);
127 static int hellcreek_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
129 struct hellcreek *hellcreek = ptp_to_hellcreek(ptp);
130 u16 negative = 0, addendh, addendl;
134 if (scaled_ppm < 0) {
136 scaled_ppm = -scaled_ppm;
139 /* IP-Core adjusts the nominal frequency by adding or subtracting 1 ns
140 * from the 8 ns (period of the oscillator) every time the accumulator
141 * register overflows. The value stored in the addend register is added
142 * to the accumulator register every 8 ns.
144 * addend value = (2^30 * accumulator_overflow_rate) /
145 * oscillator_frequency
148 * oscillator_frequency = 125 MHz
149 * accumulator_overflow_rate = 125 MHz * scaled_ppm * 2^-16 * 10^-6 * 8
153 addend = (u32)div_u64(adj, 15625);
155 addendh = (addend & 0xffff0000) >> 16;
156 addendl = addend & 0xffff;
158 negative = (negative << 15) & 0x8000;
160 mutex_lock(&hellcreek->ptp_lock);
162 /* Set drift register */
163 hellcreek_ptp_write(hellcreek, negative, PR_CLOCK_DRIFT_C);
164 hellcreek_ptp_write(hellcreek, 0x00, PR_CLOCK_DRIFT_C);
165 hellcreek_ptp_write(hellcreek, 0x00, PR_CLOCK_DRIFT_C);
166 hellcreek_ptp_write(hellcreek, addendh, PR_CLOCK_DRIFT_C);
167 hellcreek_ptp_write(hellcreek, addendl, PR_CLOCK_DRIFT_C);
169 mutex_unlock(&hellcreek->ptp_lock);
174 static int hellcreek_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
176 struct hellcreek *hellcreek = ptp_to_hellcreek(ptp);
177 u16 negative = 0, counth, countl;
180 /* If the offset is larger than IP-Core slow offset resources. Don't
181 * consider slow adjustment. Rather, add the offset directly to the
184 if (abs(delta) > MAX_SLOW_OFFSET_ADJ) {
185 struct timespec64 now, then = ns_to_timespec64(delta);
187 hellcreek_ptp_gettime(ptp, &now);
188 now = timespec64_add(now, then);
189 hellcreek_ptp_settime(ptp, &now);
199 /* 'count_val' does not exceed the maximum register size (2^30) */
200 count_val = div_s64(delta, MAX_NS_PER_STEP);
202 counth = (count_val & 0xffff0000) >> 16;
203 countl = count_val & 0xffff;
205 negative = (negative << 15) & 0x8000;
207 mutex_lock(&hellcreek->ptp_lock);
209 /* Set offset write register */
210 hellcreek_ptp_write(hellcreek, negative, PR_CLOCK_OFFSET_C);
211 hellcreek_ptp_write(hellcreek, MAX_NS_PER_STEP, PR_CLOCK_OFFSET_C);
212 hellcreek_ptp_write(hellcreek, MIN_CLK_CYCLES_BETWEEN_STEPS,
214 hellcreek_ptp_write(hellcreek, countl, PR_CLOCK_OFFSET_C);
215 hellcreek_ptp_write(hellcreek, counth, PR_CLOCK_OFFSET_C);
217 mutex_unlock(&hellcreek->ptp_lock);
222 static int hellcreek_ptp_enable(struct ptp_clock_info *ptp,
223 struct ptp_clock_request *rq, int on)
228 static void hellcreek_ptp_overflow_check(struct work_struct *work)
230 struct delayed_work *dw = to_delayed_work(work);
231 struct hellcreek *hellcreek;
233 hellcreek = dw_overflow_to_hellcreek(dw);
235 mutex_lock(&hellcreek->ptp_lock);
236 __hellcreek_ptp_gettime(hellcreek);
237 mutex_unlock(&hellcreek->ptp_lock);
239 schedule_delayed_work(&hellcreek->overflow_work,
240 HELLCREEK_OVERFLOW_PERIOD);
243 static enum led_brightness hellcreek_get_brightness(struct hellcreek *hellcreek,
246 return (hellcreek->status_out & led) ? 1 : 0;
249 static void hellcreek_set_brightness(struct hellcreek *hellcreek, int led,
250 enum led_brightness b)
252 mutex_lock(&hellcreek->ptp_lock);
255 hellcreek->status_out |= led;
257 hellcreek->status_out &= ~led;
259 hellcreek_ptp_write(hellcreek, hellcreek->status_out, STATUS_OUT);
261 mutex_unlock(&hellcreek->ptp_lock);
264 static void hellcreek_led_sync_good_set(struct led_classdev *ldev,
265 enum led_brightness b)
267 struct hellcreek *hellcreek = led_to_hellcreek(ldev, led_sync_good);
269 hellcreek_set_brightness(hellcreek, STATUS_OUT_SYNC_GOOD, b);
272 static enum led_brightness hellcreek_led_sync_good_get(struct led_classdev *ldev)
274 struct hellcreek *hellcreek = led_to_hellcreek(ldev, led_sync_good);
276 return hellcreek_get_brightness(hellcreek, STATUS_OUT_SYNC_GOOD);
279 static void hellcreek_led_is_gm_set(struct led_classdev *ldev,
280 enum led_brightness b)
282 struct hellcreek *hellcreek = led_to_hellcreek(ldev, led_is_gm);
284 hellcreek_set_brightness(hellcreek, STATUS_OUT_IS_GM, b);
287 static enum led_brightness hellcreek_led_is_gm_get(struct led_classdev *ldev)
289 struct hellcreek *hellcreek = led_to_hellcreek(ldev, led_is_gm);
291 return hellcreek_get_brightness(hellcreek, STATUS_OUT_IS_GM);
294 /* There two available LEDs internally called sync_good and is_gm. However, the
295 * user might want to use a different label and specify the default state. Take
296 * those properties from device tree.
298 static int hellcreek_led_setup(struct hellcreek *hellcreek)
300 struct device_node *leds, *led = NULL;
301 enum led_default_state state;
305 of_node_get(hellcreek->dev->of_node);
306 leds = of_find_node_by_name(hellcreek->dev->of_node, "leds");
308 dev_err(hellcreek->dev, "No LEDs specified in device tree!\n");
312 hellcreek->status_out = 0;
314 led = of_get_next_available_child(leds, led);
316 dev_err(hellcreek->dev, "First LED not specified!\n");
320 ret = of_property_read_string(led, "label", &label);
321 hellcreek->led_sync_good.name = ret ? "sync_good" : label;
323 state = led_init_default_state_get(of_fwnode_handle(led));
325 case LEDS_DEFSTATE_ON:
326 hellcreek->led_sync_good.brightness = 1;
328 case LEDS_DEFSTATE_KEEP:
329 hellcreek->led_sync_good.brightness =
330 hellcreek_get_brightness(hellcreek, STATUS_OUT_SYNC_GOOD);
333 hellcreek->led_sync_good.brightness = 0;
336 hellcreek->led_sync_good.max_brightness = 1;
337 hellcreek->led_sync_good.brightness_set = hellcreek_led_sync_good_set;
338 hellcreek->led_sync_good.brightness_get = hellcreek_led_sync_good_get;
340 led = of_get_next_available_child(leds, led);
342 dev_err(hellcreek->dev, "Second LED not specified!\n");
347 ret = of_property_read_string(led, "label", &label);
348 hellcreek->led_is_gm.name = ret ? "is_gm" : label;
350 state = led_init_default_state_get(of_fwnode_handle(led));
352 case LEDS_DEFSTATE_ON:
353 hellcreek->led_is_gm.brightness = 1;
355 case LEDS_DEFSTATE_KEEP:
356 hellcreek->led_is_gm.brightness =
357 hellcreek_get_brightness(hellcreek, STATUS_OUT_IS_GM);
360 hellcreek->led_is_gm.brightness = 0;
363 hellcreek->led_is_gm.max_brightness = 1;
364 hellcreek->led_is_gm.brightness_set = hellcreek_led_is_gm_set;
365 hellcreek->led_is_gm.brightness_get = hellcreek_led_is_gm_get;
367 /* Set initial state */
368 if (hellcreek->led_sync_good.brightness == 1)
369 hellcreek_set_brightness(hellcreek, STATUS_OUT_SYNC_GOOD, 1);
370 if (hellcreek->led_is_gm.brightness == 1)
371 hellcreek_set_brightness(hellcreek, STATUS_OUT_IS_GM, 1);
373 /* Register both leds */
374 led_classdev_register(hellcreek->dev, &hellcreek->led_sync_good);
375 led_classdev_register(hellcreek->dev, &hellcreek->led_is_gm);
385 int hellcreek_ptp_setup(struct hellcreek *hellcreek)
390 /* Set up the overflow work */
391 INIT_DELAYED_WORK(&hellcreek->overflow_work,
392 hellcreek_ptp_overflow_check);
394 /* Setup PTP clock */
395 hellcreek->ptp_clock_info.owner = THIS_MODULE;
396 snprintf(hellcreek->ptp_clock_info.name,
397 sizeof(hellcreek->ptp_clock_info.name),
398 dev_name(hellcreek->dev));
400 /* IP-Core can add up to 0.5 ns per 8 ns cycle, which means
401 * accumulator_overflow_rate shall not exceed 62.5 MHz (which adjusts
402 * the nominal frequency by 6.25%)
404 hellcreek->ptp_clock_info.max_adj = 62500000;
405 hellcreek->ptp_clock_info.n_alarm = 0;
406 hellcreek->ptp_clock_info.n_pins = 0;
407 hellcreek->ptp_clock_info.n_ext_ts = 0;
408 hellcreek->ptp_clock_info.n_per_out = 0;
409 hellcreek->ptp_clock_info.pps = 0;
410 hellcreek->ptp_clock_info.adjfine = hellcreek_ptp_adjfine;
411 hellcreek->ptp_clock_info.adjtime = hellcreek_ptp_adjtime;
412 hellcreek->ptp_clock_info.gettime64 = hellcreek_ptp_gettime;
413 hellcreek->ptp_clock_info.settime64 = hellcreek_ptp_settime;
414 hellcreek->ptp_clock_info.enable = hellcreek_ptp_enable;
415 hellcreek->ptp_clock_info.do_aux_work = hellcreek_hwtstamp_work;
417 hellcreek->ptp_clock = ptp_clock_register(&hellcreek->ptp_clock_info,
419 if (IS_ERR(hellcreek->ptp_clock))
420 return PTR_ERR(hellcreek->ptp_clock);
422 /* Enable the offset correction process, if no offset correction is
423 * already taking place
425 status = hellcreek_ptp_read(hellcreek, PR_CLOCK_STATUS_C);
426 if (!(status & PR_CLOCK_STATUS_C_OFS_ACT))
427 hellcreek_ptp_write(hellcreek,
428 status | PR_CLOCK_STATUS_C_ENA_OFS,
431 /* Enable the drift correction process */
432 hellcreek_ptp_write(hellcreek, status | PR_CLOCK_STATUS_C_ENA_DRIFT,
436 ret = hellcreek_led_setup(hellcreek);
438 if (hellcreek->ptp_clock)
439 ptp_clock_unregister(hellcreek->ptp_clock);
443 schedule_delayed_work(&hellcreek->overflow_work,
444 HELLCREEK_OVERFLOW_PERIOD);
449 void hellcreek_ptp_free(struct hellcreek *hellcreek)
451 led_classdev_unregister(&hellcreek->led_is_gm);
452 led_classdev_unregister(&hellcreek->led_sync_good);
453 cancel_delayed_work_sync(&hellcreek->overflow_work);
454 if (hellcreek->ptp_clock)
455 ptp_clock_unregister(hellcreek->ptp_clock);
456 hellcreek->ptp_clock = NULL;