162306a36Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0 OR MIT) */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * DSA driver for:
462306a36Sopenharmony_ci * Hirschmann Hellcreek TSN switch.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 2019,2020 Hochschule Offenburg
762306a36Sopenharmony_ci * Copyright (C) 2019,2020 Linutronix GmbH
862306a36Sopenharmony_ci * Authors: Kurt Kanzenbach <kurt@linutronix.de>
962306a36Sopenharmony_ci *	    Kamil Alkhouri <kamil.alkhouri@hs-offenburg.de>
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#ifndef _HELLCREEK_PTP_H_
1362306a36Sopenharmony_ci#define _HELLCREEK_PTP_H_
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <linux/bitops.h>
1662306a36Sopenharmony_ci#include <linux/ptp_clock_kernel.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#include "hellcreek.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* Every jump in time is 7 ns */
2162306a36Sopenharmony_ci#define MAX_NS_PER_STEP			7L
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/* Correct offset at every clock cycle */
2462306a36Sopenharmony_ci#define MIN_CLK_CYCLES_BETWEEN_STEPS	0
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/* Maximum available slow offset resources */
2762306a36Sopenharmony_ci#define MAX_SLOW_OFFSET_ADJ					\
2862306a36Sopenharmony_ci	((unsigned long long)((1 << 30) - 1) * MAX_NS_PER_STEP)
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/* four times a second overflow check */
3162306a36Sopenharmony_ci#define HELLCREEK_OVERFLOW_PERIOD	(HZ / 4)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/* PTP Register */
3462306a36Sopenharmony_ci#define PR_SETTINGS_C			(0x09 * 2)
3562306a36Sopenharmony_ci#define PR_SETTINGS_C_RES3TS		BIT(4)
3662306a36Sopenharmony_ci#define PR_SETTINGS_C_TS_SRC_TK_SHIFT	8
3762306a36Sopenharmony_ci#define PR_SETTINGS_C_TS_SRC_TK_MASK	GENMASK(9, 8)
3862306a36Sopenharmony_ci#define PR_COMMAND_C			(0x0a * 2)
3962306a36Sopenharmony_ci#define PR_COMMAND_C_SS			BIT(0)
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define PR_CLOCK_STATUS_C		(0x0c * 2)
4262306a36Sopenharmony_ci#define PR_CLOCK_STATUS_C_ENA_DRIFT	BIT(12)
4362306a36Sopenharmony_ci#define PR_CLOCK_STATUS_C_OFS_ACT	BIT(13)
4462306a36Sopenharmony_ci#define PR_CLOCK_STATUS_C_ENA_OFS	BIT(14)
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define PR_CLOCK_READ_C			(0x0d * 2)
4762306a36Sopenharmony_ci#define PR_CLOCK_WRITE_C		(0x0e * 2)
4862306a36Sopenharmony_ci#define PR_CLOCK_OFFSET_C		(0x0f * 2)
4962306a36Sopenharmony_ci#define PR_CLOCK_DRIFT_C		(0x10 * 2)
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci#define PR_SS_FREE_DATA_C		(0x12 * 2)
5262306a36Sopenharmony_ci#define PR_SS_SYNT_DATA_C		(0x14 * 2)
5362306a36Sopenharmony_ci#define PR_SS_SYNC_DATA_C		(0x16 * 2)
5462306a36Sopenharmony_ci#define PR_SS_DRAC_DATA_C		(0x18 * 2)
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#define STATUS_OUT			(0x60 * 2)
5762306a36Sopenharmony_ci#define STATUS_OUT_SYNC_GOOD		BIT(0)
5862306a36Sopenharmony_ci#define STATUS_OUT_IS_GM		BIT(1)
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciint hellcreek_ptp_setup(struct hellcreek *hellcreek);
6162306a36Sopenharmony_civoid hellcreek_ptp_free(struct hellcreek *hellcreek);
6262306a36Sopenharmony_ciu16 hellcreek_ptp_read(struct hellcreek *hellcreek, unsigned int offset);
6362306a36Sopenharmony_civoid hellcreek_ptp_write(struct hellcreek *hellcreek, u16 data,
6462306a36Sopenharmony_ci			 unsigned int offset);
6562306a36Sopenharmony_ciu64 hellcreek_ptp_gettime_seconds(struct hellcreek *hellcreek, u64 ns);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#define ptp_to_hellcreek(ptp)					\
6862306a36Sopenharmony_ci	container_of(ptp, struct hellcreek, ptp_clock_info)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#define dw_overflow_to_hellcreek(dw)				\
7162306a36Sopenharmony_ci	container_of(dw, struct hellcreek, overflow_work)
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci#define led_to_hellcreek(ldev, led)				\
7462306a36Sopenharmony_ci	container_of(ldev, struct hellcreek, led)
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci#endif /* _HELLCREEK_PTP_H_ */
77