xref: /openbmc/linux/drivers/net/dsa/mv88e6xxx/ptp.c (revision 6138e687c7b679da08c0feb55a88f448f7890c07)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22fa8d3afSBrandon Streiff /*
32fa8d3afSBrandon Streiff  * Marvell 88E6xxx Switch PTP support
42fa8d3afSBrandon Streiff  *
52fa8d3afSBrandon Streiff  * Copyright (c) 2008 Marvell Semiconductor
62fa8d3afSBrandon Streiff  *
72fa8d3afSBrandon Streiff  * Copyright (c) 2017 National Instruments
82fa8d3afSBrandon Streiff  *      Erik Hons <erik.hons@ni.com>
92fa8d3afSBrandon Streiff  *      Brandon Streiff <brandon.streiff@ni.com>
102fa8d3afSBrandon Streiff  *      Dane Wagner <dane.wagner@ni.com>
112fa8d3afSBrandon Streiff  */
122fa8d3afSBrandon Streiff 
132fa8d3afSBrandon Streiff #include "chip.h"
142fa8d3afSBrandon Streiff #include "global2.h"
15ffc705deSAndrew Lunn #include "hwtstamp.h"
162fa8d3afSBrandon Streiff #include "ptp.h"
172fa8d3afSBrandon Streiff 
1871509614SHubert Feurstein #define MV88E6XXX_MAX_ADJ_PPB	1000000
1971509614SHubert Feurstein 
2071509614SHubert Feurstein /* Family MV88E6250:
2171509614SHubert Feurstein  * Raw timestamps are in units of 10-ns clock periods.
2271509614SHubert Feurstein  *
2371509614SHubert Feurstein  * clkadj = scaled_ppm * 10*2^28 / (10^6 * 2^16)
2471509614SHubert Feurstein  * simplifies to
2571509614SHubert Feurstein  * clkadj = scaled_ppm * 2^7 / 5^5
2671509614SHubert Feurstein  */
2771509614SHubert Feurstein #define MV88E6250_CC_SHIFT	28
2871509614SHubert Feurstein #define MV88E6250_CC_MULT	(10 << MV88E6250_CC_SHIFT)
2971509614SHubert Feurstein #define MV88E6250_CC_MULT_NUM	(1 << 7)
3071509614SHubert Feurstein #define MV88E6250_CC_MULT_DEM	3125ULL
3171509614SHubert Feurstein 
3271509614SHubert Feurstein /* Other families:
3371509614SHubert Feurstein  * Raw timestamps are in units of 8-ns clock periods.
3471509614SHubert Feurstein  *
3571509614SHubert Feurstein  * clkadj = scaled_ppm * 8*2^28 / (10^6 * 2^16)
3671509614SHubert Feurstein  * simplifies to
3771509614SHubert Feurstein  * clkadj = scaled_ppm * 2^9 / 5^6
3871509614SHubert Feurstein  */
3971509614SHubert Feurstein #define MV88E6XXX_CC_SHIFT	28
4071509614SHubert Feurstein #define MV88E6XXX_CC_MULT	(8 << MV88E6XXX_CC_SHIFT)
4171509614SHubert Feurstein #define MV88E6XXX_CC_MULT_NUM	(1 << 9)
4271509614SHubert Feurstein #define MV88E6XXX_CC_MULT_DEM	15625ULL
432fa8d3afSBrandon Streiff 
442fa8d3afSBrandon Streiff #define TAI_EVENT_WORK_INTERVAL msecs_to_jiffies(100)
452fa8d3afSBrandon Streiff 
462fa8d3afSBrandon Streiff #define cc_to_chip(cc) container_of(cc, struct mv88e6xxx_chip, tstamp_cc)
472fa8d3afSBrandon Streiff #define dw_overflow_to_chip(dw) container_of(dw, struct mv88e6xxx_chip, \
482fa8d3afSBrandon Streiff 					     overflow_work)
494eb3be29SBrandon Streiff #define dw_tai_event_to_chip(dw) container_of(dw, struct mv88e6xxx_chip, \
504eb3be29SBrandon Streiff 					      tai_event_work)
512fa8d3afSBrandon Streiff 
522fa8d3afSBrandon Streiff static int mv88e6xxx_tai_read(struct mv88e6xxx_chip *chip, int addr,
532fa8d3afSBrandon Streiff 			      u16 *data, int len)
542fa8d3afSBrandon Streiff {
552fa8d3afSBrandon Streiff 	if (!chip->info->ops->avb_ops->tai_read)
562fa8d3afSBrandon Streiff 		return -EOPNOTSUPP;
572fa8d3afSBrandon Streiff 
582fa8d3afSBrandon Streiff 	return chip->info->ops->avb_ops->tai_read(chip, addr, data, len);
592fa8d3afSBrandon Streiff }
602fa8d3afSBrandon Streiff 
614eb3be29SBrandon Streiff static int mv88e6xxx_tai_write(struct mv88e6xxx_chip *chip, int addr, u16 data)
624eb3be29SBrandon Streiff {
634eb3be29SBrandon Streiff 	if (!chip->info->ops->avb_ops->tai_write)
644eb3be29SBrandon Streiff 		return -EOPNOTSUPP;
654eb3be29SBrandon Streiff 
664eb3be29SBrandon Streiff 	return chip->info->ops->avb_ops->tai_write(chip, addr, data);
674eb3be29SBrandon Streiff }
684eb3be29SBrandon Streiff 
694eb3be29SBrandon Streiff /* TODO: places where this are called should be using pinctrl */
706d2ac8eeSAndrew Lunn static int mv88e6352_set_gpio_func(struct mv88e6xxx_chip *chip, int pin,
714eb3be29SBrandon Streiff 				   int func, int input)
724eb3be29SBrandon Streiff {
734eb3be29SBrandon Streiff 	int err;
744eb3be29SBrandon Streiff 
754eb3be29SBrandon Streiff 	if (!chip->info->ops->gpio_ops)
764eb3be29SBrandon Streiff 		return -EOPNOTSUPP;
774eb3be29SBrandon Streiff 
784eb3be29SBrandon Streiff 	err = chip->info->ops->gpio_ops->set_dir(chip, pin, input);
794eb3be29SBrandon Streiff 	if (err)
804eb3be29SBrandon Streiff 		return err;
814eb3be29SBrandon Streiff 
824eb3be29SBrandon Streiff 	return chip->info->ops->gpio_ops->set_pctl(chip, pin, func);
834eb3be29SBrandon Streiff }
844eb3be29SBrandon Streiff 
856d2ac8eeSAndrew Lunn static u64 mv88e6352_ptp_clock_read(const struct cyclecounter *cc)
862fa8d3afSBrandon Streiff {
872fa8d3afSBrandon Streiff 	struct mv88e6xxx_chip *chip = cc_to_chip(cc);
882fa8d3afSBrandon Streiff 	u16 phc_time[2];
892fa8d3afSBrandon Streiff 	int err;
902fa8d3afSBrandon Streiff 
912fa8d3afSBrandon Streiff 	err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_TIME_LO, phc_time,
922fa8d3afSBrandon Streiff 				 ARRAY_SIZE(phc_time));
932fa8d3afSBrandon Streiff 	if (err)
942fa8d3afSBrandon Streiff 		return 0;
952fa8d3afSBrandon Streiff 	else
962fa8d3afSBrandon Streiff 		return ((u32)phc_time[1] << 16) | phc_time[0];
972fa8d3afSBrandon Streiff }
982fa8d3afSBrandon Streiff 
99dfa54348SAndrew Lunn static u64 mv88e6165_ptp_clock_read(const struct cyclecounter *cc)
100dfa54348SAndrew Lunn {
101dfa54348SAndrew Lunn 	struct mv88e6xxx_chip *chip = cc_to_chip(cc);
102dfa54348SAndrew Lunn 	u16 phc_time[2];
103dfa54348SAndrew Lunn 	int err;
104dfa54348SAndrew Lunn 
105dfa54348SAndrew Lunn 	err = mv88e6xxx_tai_read(chip, MV88E6XXX_PTP_GC_TIME_LO, phc_time,
106dfa54348SAndrew Lunn 				 ARRAY_SIZE(phc_time));
107dfa54348SAndrew Lunn 	if (err)
108dfa54348SAndrew Lunn 		return 0;
109dfa54348SAndrew Lunn 	else
110dfa54348SAndrew Lunn 		return ((u32)phc_time[1] << 16) | phc_time[0];
111dfa54348SAndrew Lunn }
112dfa54348SAndrew Lunn 
1136d2ac8eeSAndrew Lunn /* mv88e6352_config_eventcap - configure TAI event capture
1144eb3be29SBrandon Streiff  * @event: PTP_CLOCK_PPS (internal) or PTP_CLOCK_EXTTS (external)
1154eb3be29SBrandon Streiff  * @rising: zero for falling-edge trigger, else rising-edge trigger
1164eb3be29SBrandon Streiff  *
1174eb3be29SBrandon Streiff  * This will also reset the capture sequence counter.
1184eb3be29SBrandon Streiff  */
1196d2ac8eeSAndrew Lunn static int mv88e6352_config_eventcap(struct mv88e6xxx_chip *chip, int event,
1204eb3be29SBrandon Streiff 				     int rising)
1214eb3be29SBrandon Streiff {
1224eb3be29SBrandon Streiff 	u16 global_config;
1234eb3be29SBrandon Streiff 	u16 cap_config;
1244eb3be29SBrandon Streiff 	int err;
1254eb3be29SBrandon Streiff 
1264eb3be29SBrandon Streiff 	chip->evcap_config = MV88E6XXX_TAI_CFG_CAP_OVERWRITE |
1274eb3be29SBrandon Streiff 			     MV88E6XXX_TAI_CFG_CAP_CTR_START;
1284eb3be29SBrandon Streiff 	if (!rising)
1294eb3be29SBrandon Streiff 		chip->evcap_config |= MV88E6XXX_TAI_CFG_EVREQ_FALLING;
1304eb3be29SBrandon Streiff 
1314eb3be29SBrandon Streiff 	global_config = (chip->evcap_config | chip->trig_config);
1324eb3be29SBrandon Streiff 	err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_CFG, global_config);
1334eb3be29SBrandon Streiff 	if (err)
1344eb3be29SBrandon Streiff 		return err;
1354eb3be29SBrandon Streiff 
1364eb3be29SBrandon Streiff 	if (event == PTP_CLOCK_PPS) {
1374eb3be29SBrandon Streiff 		cap_config = MV88E6XXX_TAI_EVENT_STATUS_CAP_TRIG;
1384eb3be29SBrandon Streiff 	} else if (event == PTP_CLOCK_EXTTS) {
1394eb3be29SBrandon Streiff 		/* if STATUS_CAP_TRIG is unset we capture PTP_EVREQ events */
1404eb3be29SBrandon Streiff 		cap_config = 0;
1414eb3be29SBrandon Streiff 	} else {
1424eb3be29SBrandon Streiff 		return -EINVAL;
1434eb3be29SBrandon Streiff 	}
1444eb3be29SBrandon Streiff 
1454eb3be29SBrandon Streiff 	/* Write the capture config; this also clears the capture counter */
1464eb3be29SBrandon Streiff 	err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_EVENT_STATUS,
1474eb3be29SBrandon Streiff 				  cap_config);
1484eb3be29SBrandon Streiff 
1494eb3be29SBrandon Streiff 	return err;
1504eb3be29SBrandon Streiff }
1514eb3be29SBrandon Streiff 
1526d2ac8eeSAndrew Lunn static void mv88e6352_tai_event_work(struct work_struct *ugly)
1534eb3be29SBrandon Streiff {
1544eb3be29SBrandon Streiff 	struct delayed_work *dw = to_delayed_work(ugly);
1554eb3be29SBrandon Streiff 	struct mv88e6xxx_chip *chip = dw_tai_event_to_chip(dw);
1564eb3be29SBrandon Streiff 	struct ptp_clock_event ev;
1574eb3be29SBrandon Streiff 	u16 status[4];
1584eb3be29SBrandon Streiff 	u32 raw_ts;
1594eb3be29SBrandon Streiff 	int err;
1604eb3be29SBrandon Streiff 
161c9acece0SRasmus Villemoes 	mv88e6xxx_reg_lock(chip);
1624eb3be29SBrandon Streiff 	err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_EVENT_STATUS,
1634eb3be29SBrandon Streiff 				 status, ARRAY_SIZE(status));
164c9acece0SRasmus Villemoes 	mv88e6xxx_reg_unlock(chip);
1654eb3be29SBrandon Streiff 
1664eb3be29SBrandon Streiff 	if (err) {
1674eb3be29SBrandon Streiff 		dev_err(chip->dev, "failed to read TAI status register\n");
1684eb3be29SBrandon Streiff 		return;
1694eb3be29SBrandon Streiff 	}
1704eb3be29SBrandon Streiff 	if (status[0] & MV88E6XXX_TAI_EVENT_STATUS_ERROR) {
1714eb3be29SBrandon Streiff 		dev_warn(chip->dev, "missed event capture\n");
1724eb3be29SBrandon Streiff 		return;
1734eb3be29SBrandon Streiff 	}
1744eb3be29SBrandon Streiff 	if (!(status[0] & MV88E6XXX_TAI_EVENT_STATUS_VALID))
1754eb3be29SBrandon Streiff 		goto out;
1764eb3be29SBrandon Streiff 
1774eb3be29SBrandon Streiff 	raw_ts = ((u32)status[2] << 16) | status[1];
1784eb3be29SBrandon Streiff 
1794eb3be29SBrandon Streiff 	/* Clear the valid bit so the next timestamp can come in */
1804eb3be29SBrandon Streiff 	status[0] &= ~MV88E6XXX_TAI_EVENT_STATUS_VALID;
181c9acece0SRasmus Villemoes 	mv88e6xxx_reg_lock(chip);
1824eb3be29SBrandon Streiff 	err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_EVENT_STATUS, status[0]);
183c9acece0SRasmus Villemoes 	mv88e6xxx_reg_unlock(chip);
1844eb3be29SBrandon Streiff 
1854eb3be29SBrandon Streiff 	/* This is an external timestamp */
1864eb3be29SBrandon Streiff 	ev.type = PTP_CLOCK_EXTTS;
1874eb3be29SBrandon Streiff 
1884eb3be29SBrandon Streiff 	/* We only have one timestamping channel. */
1894eb3be29SBrandon Streiff 	ev.index = 0;
190c9acece0SRasmus Villemoes 	mv88e6xxx_reg_lock(chip);
1914eb3be29SBrandon Streiff 	ev.timestamp = timecounter_cyc2time(&chip->tstamp_tc, raw_ts);
192c9acece0SRasmus Villemoes 	mv88e6xxx_reg_unlock(chip);
1934eb3be29SBrandon Streiff 
1944eb3be29SBrandon Streiff 	ptp_clock_event(chip->ptp_clock, &ev);
1954eb3be29SBrandon Streiff out:
1964eb3be29SBrandon Streiff 	schedule_delayed_work(&chip->tai_event_work, TAI_EVENT_WORK_INTERVAL);
1974eb3be29SBrandon Streiff }
1984eb3be29SBrandon Streiff 
1992fa8d3afSBrandon Streiff static int mv88e6xxx_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
2002fa8d3afSBrandon Streiff {
2012fa8d3afSBrandon Streiff 	struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
20271509614SHubert Feurstein 	const struct mv88e6xxx_ptp_ops *ptp_ops = chip->info->ops->ptp_ops;
2032fa8d3afSBrandon Streiff 	int neg_adj = 0;
2042fa8d3afSBrandon Streiff 	u32 diff, mult;
2052fa8d3afSBrandon Streiff 	u64 adj;
2062fa8d3afSBrandon Streiff 
2072fa8d3afSBrandon Streiff 	if (scaled_ppm < 0) {
2082fa8d3afSBrandon Streiff 		neg_adj = 1;
2092fa8d3afSBrandon Streiff 		scaled_ppm = -scaled_ppm;
2102fa8d3afSBrandon Streiff 	}
21171509614SHubert Feurstein 
21271509614SHubert Feurstein 	mult = ptp_ops->cc_mult;
21371509614SHubert Feurstein 	adj = ptp_ops->cc_mult_num;
2142fa8d3afSBrandon Streiff 	adj *= scaled_ppm;
21571509614SHubert Feurstein 	diff = div_u64(adj, ptp_ops->cc_mult_dem);
2162fa8d3afSBrandon Streiff 
217c9acece0SRasmus Villemoes 	mv88e6xxx_reg_lock(chip);
2182fa8d3afSBrandon Streiff 
2192fa8d3afSBrandon Streiff 	timecounter_read(&chip->tstamp_tc);
2202fa8d3afSBrandon Streiff 	chip->tstamp_cc.mult = neg_adj ? mult - diff : mult + diff;
2212fa8d3afSBrandon Streiff 
222c9acece0SRasmus Villemoes 	mv88e6xxx_reg_unlock(chip);
2232fa8d3afSBrandon Streiff 
2242fa8d3afSBrandon Streiff 	return 0;
2252fa8d3afSBrandon Streiff }
2262fa8d3afSBrandon Streiff 
2272fa8d3afSBrandon Streiff static int mv88e6xxx_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2282fa8d3afSBrandon Streiff {
2292fa8d3afSBrandon Streiff 	struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
2302fa8d3afSBrandon Streiff 
231c9acece0SRasmus Villemoes 	mv88e6xxx_reg_lock(chip);
2322fa8d3afSBrandon Streiff 	timecounter_adjtime(&chip->tstamp_tc, delta);
233c9acece0SRasmus Villemoes 	mv88e6xxx_reg_unlock(chip);
2342fa8d3afSBrandon Streiff 
2352fa8d3afSBrandon Streiff 	return 0;
2362fa8d3afSBrandon Streiff }
2372fa8d3afSBrandon Streiff 
2382fa8d3afSBrandon Streiff static int mv88e6xxx_ptp_gettime(struct ptp_clock_info *ptp,
2392fa8d3afSBrandon Streiff 				 struct timespec64 *ts)
2402fa8d3afSBrandon Streiff {
2412fa8d3afSBrandon Streiff 	struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
2422fa8d3afSBrandon Streiff 	u64 ns;
2432fa8d3afSBrandon Streiff 
244c9acece0SRasmus Villemoes 	mv88e6xxx_reg_lock(chip);
2452fa8d3afSBrandon Streiff 	ns = timecounter_read(&chip->tstamp_tc);
246c9acece0SRasmus Villemoes 	mv88e6xxx_reg_unlock(chip);
2472fa8d3afSBrandon Streiff 
2482fa8d3afSBrandon Streiff 	*ts = ns_to_timespec64(ns);
2492fa8d3afSBrandon Streiff 
2502fa8d3afSBrandon Streiff 	return 0;
2512fa8d3afSBrandon Streiff }
2522fa8d3afSBrandon Streiff 
2532fa8d3afSBrandon Streiff static int mv88e6xxx_ptp_settime(struct ptp_clock_info *ptp,
2542fa8d3afSBrandon Streiff 				 const struct timespec64 *ts)
2552fa8d3afSBrandon Streiff {
2562fa8d3afSBrandon Streiff 	struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
2572fa8d3afSBrandon Streiff 	u64 ns;
2582fa8d3afSBrandon Streiff 
2592fa8d3afSBrandon Streiff 	ns = timespec64_to_ns(ts);
2602fa8d3afSBrandon Streiff 
261c9acece0SRasmus Villemoes 	mv88e6xxx_reg_lock(chip);
2622fa8d3afSBrandon Streiff 	timecounter_init(&chip->tstamp_tc, &chip->tstamp_cc, ns);
263c9acece0SRasmus Villemoes 	mv88e6xxx_reg_unlock(chip);
2642fa8d3afSBrandon Streiff 
2652fa8d3afSBrandon Streiff 	return 0;
2662fa8d3afSBrandon Streiff }
2672fa8d3afSBrandon Streiff 
2686d2ac8eeSAndrew Lunn static int mv88e6352_ptp_enable_extts(struct mv88e6xxx_chip *chip,
2694eb3be29SBrandon Streiff 				      struct ptp_clock_request *rq, int on)
2704eb3be29SBrandon Streiff {
2714eb3be29SBrandon Streiff 	int rising = (rq->extts.flags & PTP_RISING_EDGE);
2724eb3be29SBrandon Streiff 	int func;
2734eb3be29SBrandon Streiff 	int pin;
2744eb3be29SBrandon Streiff 	int err;
2754eb3be29SBrandon Streiff 
2767d9465ebSJacob Keller 	/* Reject requests with unsupported flags */
2777d9465ebSJacob Keller 	if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
2787d9465ebSJacob Keller 				PTP_RISING_EDGE |
279*6138e687SRichard Cochran 				PTP_FALLING_EDGE |
280*6138e687SRichard Cochran 				PTP_STRICT_FLAGS))
2817d9465ebSJacob Keller 		return -EOPNOTSUPP;
2827d9465ebSJacob Keller 
2834eb3be29SBrandon Streiff 	pin = ptp_find_pin(chip->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
2844eb3be29SBrandon Streiff 
2854eb3be29SBrandon Streiff 	if (pin < 0)
2864eb3be29SBrandon Streiff 		return -EBUSY;
2874eb3be29SBrandon Streiff 
288c9acece0SRasmus Villemoes 	mv88e6xxx_reg_lock(chip);
2894eb3be29SBrandon Streiff 
2904eb3be29SBrandon Streiff 	if (on) {
2914eb3be29SBrandon Streiff 		func = MV88E6352_G2_SCRATCH_GPIO_PCTL_EVREQ;
2924eb3be29SBrandon Streiff 
2936d2ac8eeSAndrew Lunn 		err = mv88e6352_set_gpio_func(chip, pin, func, true);
2944eb3be29SBrandon Streiff 		if (err)
2954eb3be29SBrandon Streiff 			goto out;
2964eb3be29SBrandon Streiff 
2974eb3be29SBrandon Streiff 		schedule_delayed_work(&chip->tai_event_work,
2984eb3be29SBrandon Streiff 				      TAI_EVENT_WORK_INTERVAL);
2994eb3be29SBrandon Streiff 
3006d2ac8eeSAndrew Lunn 		err = mv88e6352_config_eventcap(chip, PTP_CLOCK_EXTTS, rising);
3014eb3be29SBrandon Streiff 	} else {
3024eb3be29SBrandon Streiff 		func = MV88E6352_G2_SCRATCH_GPIO_PCTL_GPIO;
3034eb3be29SBrandon Streiff 
3046d2ac8eeSAndrew Lunn 		err = mv88e6352_set_gpio_func(chip, pin, func, true);
3054eb3be29SBrandon Streiff 
3064eb3be29SBrandon Streiff 		cancel_delayed_work_sync(&chip->tai_event_work);
3074eb3be29SBrandon Streiff 	}
3084eb3be29SBrandon Streiff 
3094eb3be29SBrandon Streiff out:
310c9acece0SRasmus Villemoes 	mv88e6xxx_reg_unlock(chip);
3114eb3be29SBrandon Streiff 
3124eb3be29SBrandon Streiff 	return err;
3134eb3be29SBrandon Streiff }
3144eb3be29SBrandon Streiff 
3156d2ac8eeSAndrew Lunn static int mv88e6352_ptp_enable(struct ptp_clock_info *ptp,
3162fa8d3afSBrandon Streiff 				struct ptp_clock_request *rq, int on)
3172fa8d3afSBrandon Streiff {
3184eb3be29SBrandon Streiff 	struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
3194eb3be29SBrandon Streiff 
3204eb3be29SBrandon Streiff 	switch (rq->type) {
3214eb3be29SBrandon Streiff 	case PTP_CLK_REQ_EXTTS:
3226d2ac8eeSAndrew Lunn 		return mv88e6352_ptp_enable_extts(chip, rq, on);
3234eb3be29SBrandon Streiff 	default:
3242fa8d3afSBrandon Streiff 		return -EOPNOTSUPP;
3252fa8d3afSBrandon Streiff 	}
3264eb3be29SBrandon Streiff }
3272fa8d3afSBrandon Streiff 
3286d2ac8eeSAndrew Lunn static int mv88e6352_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
3292fa8d3afSBrandon Streiff 				enum ptp_pin_function func, unsigned int chan)
3302fa8d3afSBrandon Streiff {
3314eb3be29SBrandon Streiff 	switch (func) {
3324eb3be29SBrandon Streiff 	case PTP_PF_NONE:
3334eb3be29SBrandon Streiff 	case PTP_PF_EXTTS:
3344eb3be29SBrandon Streiff 		break;
3354eb3be29SBrandon Streiff 	case PTP_PF_PEROUT:
3364eb3be29SBrandon Streiff 	case PTP_PF_PHYSYNC:
3372fa8d3afSBrandon Streiff 		return -EOPNOTSUPP;
3382fa8d3afSBrandon Streiff 	}
3394eb3be29SBrandon Streiff 	return 0;
3404eb3be29SBrandon Streiff }
3412fa8d3afSBrandon Streiff 
3428858ccc8SHubert Feurstein const struct mv88e6xxx_ptp_ops mv88e6165_ptp_ops = {
3438858ccc8SHubert Feurstein 	.clock_read = mv88e6165_ptp_clock_read,
3448858ccc8SHubert Feurstein 	.global_enable = mv88e6165_global_enable,
3458858ccc8SHubert Feurstein 	.global_disable = mv88e6165_global_disable,
3468858ccc8SHubert Feurstein 	.arr0_sts_reg = MV88E6165_PORT_PTP_ARR0_STS,
3478858ccc8SHubert Feurstein 	.arr1_sts_reg = MV88E6165_PORT_PTP_ARR1_STS,
3488858ccc8SHubert Feurstein 	.dep_sts_reg = MV88E6165_PORT_PTP_DEP_STS,
3498858ccc8SHubert Feurstein 	.rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
3508858ccc8SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
3518858ccc8SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
3528858ccc8SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
3538858ccc8SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
3548858ccc8SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
3558858ccc8SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
35671509614SHubert Feurstein 	.cc_shift = MV88E6XXX_CC_SHIFT,
35771509614SHubert Feurstein 	.cc_mult = MV88E6XXX_CC_MULT,
35871509614SHubert Feurstein 	.cc_mult_num = MV88E6XXX_CC_MULT_NUM,
35971509614SHubert Feurstein 	.cc_mult_dem = MV88E6XXX_CC_MULT_DEM,
36071509614SHubert Feurstein };
36171509614SHubert Feurstein 
36271509614SHubert Feurstein const struct mv88e6xxx_ptp_ops mv88e6250_ptp_ops = {
36371509614SHubert Feurstein 	.clock_read = mv88e6352_ptp_clock_read,
36471509614SHubert Feurstein 	.ptp_enable = mv88e6352_ptp_enable,
36571509614SHubert Feurstein 	.ptp_verify = mv88e6352_ptp_verify,
36671509614SHubert Feurstein 	.event_work = mv88e6352_tai_event_work,
36771509614SHubert Feurstein 	.port_enable = mv88e6352_hwtstamp_port_enable,
36871509614SHubert Feurstein 	.port_disable = mv88e6352_hwtstamp_port_disable,
36971509614SHubert Feurstein 	.n_ext_ts = 1,
37071509614SHubert Feurstein 	.arr0_sts_reg = MV88E6XXX_PORT_PTP_ARR0_STS,
37171509614SHubert Feurstein 	.arr1_sts_reg = MV88E6XXX_PORT_PTP_ARR1_STS,
37271509614SHubert Feurstein 	.dep_sts_reg = MV88E6XXX_PORT_PTP_DEP_STS,
37371509614SHubert Feurstein 	.rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
37471509614SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
37571509614SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
37671509614SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
37771509614SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
37871509614SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
37971509614SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
38071509614SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
38171509614SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
38271509614SHubert Feurstein 		(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
38371509614SHubert Feurstein 	.cc_shift = MV88E6250_CC_SHIFT,
38471509614SHubert Feurstein 	.cc_mult = MV88E6250_CC_MULT,
38571509614SHubert Feurstein 	.cc_mult_num = MV88E6250_CC_MULT_NUM,
38671509614SHubert Feurstein 	.cc_mult_dem = MV88E6250_CC_MULT_DEM,
3878858ccc8SHubert Feurstein };
3888858ccc8SHubert Feurstein 
3896d2ac8eeSAndrew Lunn const struct mv88e6xxx_ptp_ops mv88e6352_ptp_ops = {
3906d2ac8eeSAndrew Lunn 	.clock_read = mv88e6352_ptp_clock_read,
3916d2ac8eeSAndrew Lunn 	.ptp_enable = mv88e6352_ptp_enable,
3926d2ac8eeSAndrew Lunn 	.ptp_verify = mv88e6352_ptp_verify,
3936d2ac8eeSAndrew Lunn 	.event_work = mv88e6352_tai_event_work,
394ffc705deSAndrew Lunn 	.port_enable = mv88e6352_hwtstamp_port_enable,
395ffc705deSAndrew Lunn 	.port_disable = mv88e6352_hwtstamp_port_disable,
3966d2ac8eeSAndrew Lunn 	.n_ext_ts = 1,
397ffc705deSAndrew Lunn 	.arr0_sts_reg = MV88E6XXX_PORT_PTP_ARR0_STS,
398ffc705deSAndrew Lunn 	.arr1_sts_reg = MV88E6XXX_PORT_PTP_ARR1_STS,
399ffc705deSAndrew Lunn 	.dep_sts_reg = MV88E6XXX_PORT_PTP_DEP_STS,
40048cb5e03SAndrew Lunn 	.rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
40148cb5e03SAndrew Lunn 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
40248cb5e03SAndrew Lunn 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
40348cb5e03SAndrew Lunn 		(1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
40448cb5e03SAndrew Lunn 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
40548cb5e03SAndrew Lunn 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
40648cb5e03SAndrew Lunn 		(1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
40748cb5e03SAndrew Lunn 		(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
40848cb5e03SAndrew Lunn 		(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
40948cb5e03SAndrew Lunn 		(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
41071509614SHubert Feurstein 	.cc_shift = MV88E6XXX_CC_SHIFT,
41171509614SHubert Feurstein 	.cc_mult = MV88E6XXX_CC_MULT,
41271509614SHubert Feurstein 	.cc_mult_num = MV88E6XXX_CC_MULT_NUM,
41371509614SHubert Feurstein 	.cc_mult_dem = MV88E6XXX_CC_MULT_DEM,
4146d2ac8eeSAndrew Lunn };
4156d2ac8eeSAndrew Lunn 
4166d2ac8eeSAndrew Lunn static u64 mv88e6xxx_ptp_clock_read(const struct cyclecounter *cc)
4176d2ac8eeSAndrew Lunn {
4186d2ac8eeSAndrew Lunn 	struct mv88e6xxx_chip *chip = cc_to_chip(cc);
4196d2ac8eeSAndrew Lunn 
4206d2ac8eeSAndrew Lunn 	if (chip->info->ops->ptp_ops->clock_read)
4216d2ac8eeSAndrew Lunn 		return chip->info->ops->ptp_ops->clock_read(cc);
4226d2ac8eeSAndrew Lunn 
4236d2ac8eeSAndrew Lunn 	return 0;
4246d2ac8eeSAndrew Lunn }
4256d2ac8eeSAndrew Lunn 
4262fa8d3afSBrandon Streiff /* With a 125MHz input clock, the 32-bit timestamp counter overflows in ~34.3
4272fa8d3afSBrandon Streiff  * seconds; this task forces periodic reads so that we don't miss any.
4282fa8d3afSBrandon Streiff  */
4292fa8d3afSBrandon Streiff #define MV88E6XXX_TAI_OVERFLOW_PERIOD (HZ * 16)
4302fa8d3afSBrandon Streiff static void mv88e6xxx_ptp_overflow_check(struct work_struct *work)
4312fa8d3afSBrandon Streiff {
4322fa8d3afSBrandon Streiff 	struct delayed_work *dw = to_delayed_work(work);
4332fa8d3afSBrandon Streiff 	struct mv88e6xxx_chip *chip = dw_overflow_to_chip(dw);
4342fa8d3afSBrandon Streiff 	struct timespec64 ts;
4352fa8d3afSBrandon Streiff 
4362fa8d3afSBrandon Streiff 	mv88e6xxx_ptp_gettime(&chip->ptp_clock_info, &ts);
4372fa8d3afSBrandon Streiff 
4382fa8d3afSBrandon Streiff 	schedule_delayed_work(&chip->overflow_work,
4392fa8d3afSBrandon Streiff 			      MV88E6XXX_TAI_OVERFLOW_PERIOD);
4402fa8d3afSBrandon Streiff }
4412fa8d3afSBrandon Streiff 
4422fa8d3afSBrandon Streiff int mv88e6xxx_ptp_setup(struct mv88e6xxx_chip *chip)
4432fa8d3afSBrandon Streiff {
4446d2ac8eeSAndrew Lunn 	const struct mv88e6xxx_ptp_ops *ptp_ops = chip->info->ops->ptp_ops;
4454eb3be29SBrandon Streiff 	int i;
4464eb3be29SBrandon Streiff 
4472fa8d3afSBrandon Streiff 	/* Set up the cycle counter */
4482fa8d3afSBrandon Streiff 	memset(&chip->tstamp_cc, 0, sizeof(chip->tstamp_cc));
4492fa8d3afSBrandon Streiff 	chip->tstamp_cc.read	= mv88e6xxx_ptp_clock_read;
4502fa8d3afSBrandon Streiff 	chip->tstamp_cc.mask	= CYCLECOUNTER_MASK(32);
45171509614SHubert Feurstein 	chip->tstamp_cc.mult	= ptp_ops->cc_mult;
45271509614SHubert Feurstein 	chip->tstamp_cc.shift	= ptp_ops->cc_shift;
4532fa8d3afSBrandon Streiff 
4542fa8d3afSBrandon Streiff 	timecounter_init(&chip->tstamp_tc, &chip->tstamp_cc,
4552fa8d3afSBrandon Streiff 			 ktime_to_ns(ktime_get_real()));
4562fa8d3afSBrandon Streiff 
4572fa8d3afSBrandon Streiff 	INIT_DELAYED_WORK(&chip->overflow_work, mv88e6xxx_ptp_overflow_check);
4586d2ac8eeSAndrew Lunn 	if (ptp_ops->event_work)
4596d2ac8eeSAndrew Lunn 		INIT_DELAYED_WORK(&chip->tai_event_work, ptp_ops->event_work);
4602fa8d3afSBrandon Streiff 
4612fa8d3afSBrandon Streiff 	chip->ptp_clock_info.owner = THIS_MODULE;
4622fa8d3afSBrandon Streiff 	snprintf(chip->ptp_clock_info.name, sizeof(chip->ptp_clock_info.name),
4633f8b8696SFlorian Fainelli 		 "%s", dev_name(chip->dev));
4642fa8d3afSBrandon Streiff 
4656d2ac8eeSAndrew Lunn 	chip->ptp_clock_info.n_ext_ts	= ptp_ops->n_ext_ts;
4664eb3be29SBrandon Streiff 	chip->ptp_clock_info.n_per_out	= 0;
4674eb3be29SBrandon Streiff 	chip->ptp_clock_info.n_pins	= mv88e6xxx_num_gpio(chip);
4684eb3be29SBrandon Streiff 	chip->ptp_clock_info.pps	= 0;
4694eb3be29SBrandon Streiff 
4704eb3be29SBrandon Streiff 	for (i = 0; i < chip->ptp_clock_info.n_pins; ++i) {
4714eb3be29SBrandon Streiff 		struct ptp_pin_desc *ppd = &chip->pin_config[i];
4724eb3be29SBrandon Streiff 
4734eb3be29SBrandon Streiff 		snprintf(ppd->name, sizeof(ppd->name), "mv88e6xxx_gpio%d", i);
4744eb3be29SBrandon Streiff 		ppd->index = i;
4754eb3be29SBrandon Streiff 		ppd->func = PTP_PF_NONE;
4764eb3be29SBrandon Streiff 	}
4774eb3be29SBrandon Streiff 	chip->ptp_clock_info.pin_config = chip->pin_config;
4784eb3be29SBrandon Streiff 
47971509614SHubert Feurstein 	chip->ptp_clock_info.max_adj    = MV88E6XXX_MAX_ADJ_PPB;
4802fa8d3afSBrandon Streiff 	chip->ptp_clock_info.adjfine	= mv88e6xxx_ptp_adjfine;
4812fa8d3afSBrandon Streiff 	chip->ptp_clock_info.adjtime	= mv88e6xxx_ptp_adjtime;
4822fa8d3afSBrandon Streiff 	chip->ptp_clock_info.gettime64	= mv88e6xxx_ptp_gettime;
4832fa8d3afSBrandon Streiff 	chip->ptp_clock_info.settime64	= mv88e6xxx_ptp_settime;
4846d2ac8eeSAndrew Lunn 	chip->ptp_clock_info.enable	= ptp_ops->ptp_enable;
4856d2ac8eeSAndrew Lunn 	chip->ptp_clock_info.verify	= ptp_ops->ptp_verify;
486c6fe0ad2SBrandon Streiff 	chip->ptp_clock_info.do_aux_work = mv88e6xxx_hwtstamp_work;
4872fa8d3afSBrandon Streiff 
4882fa8d3afSBrandon Streiff 	chip->ptp_clock = ptp_clock_register(&chip->ptp_clock_info, chip->dev);
4892fa8d3afSBrandon Streiff 	if (IS_ERR(chip->ptp_clock))
4902fa8d3afSBrandon Streiff 		return PTR_ERR(chip->ptp_clock);
4912fa8d3afSBrandon Streiff 
4922fa8d3afSBrandon Streiff 	schedule_delayed_work(&chip->overflow_work,
4932fa8d3afSBrandon Streiff 			      MV88E6XXX_TAI_OVERFLOW_PERIOD);
4942fa8d3afSBrandon Streiff 
4952fa8d3afSBrandon Streiff 	return 0;
4962fa8d3afSBrandon Streiff }
4972fa8d3afSBrandon Streiff 
4982fa8d3afSBrandon Streiff void mv88e6xxx_ptp_free(struct mv88e6xxx_chip *chip)
4992fa8d3afSBrandon Streiff {
5002fa8d3afSBrandon Streiff 	if (chip->ptp_clock) {
5012fa8d3afSBrandon Streiff 		cancel_delayed_work_sync(&chip->overflow_work);
5026d2ac8eeSAndrew Lunn 		if (chip->info->ops->ptp_ops->event_work)
5034eb3be29SBrandon Streiff 			cancel_delayed_work_sync(&chip->tai_event_work);
5042fa8d3afSBrandon Streiff 
5052fa8d3afSBrandon Streiff 		ptp_clock_unregister(chip->ptp_clock);
5062fa8d3afSBrandon Streiff 		chip->ptp_clock = NULL;
5072fa8d3afSBrandon Streiff 	}
5082fa8d3afSBrandon Streiff }
509