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"
149627c981SKurt Kanzenbach #include "global1.h"
152fa8d3afSBrandon Streiff #include "global2.h"
16ffc705deSAndrew Lunn #include "hwtstamp.h"
172fa8d3afSBrandon Streiff #include "ptp.h"
182fa8d3afSBrandon Streiff
1971509614SHubert Feurstein #define MV88E6XXX_MAX_ADJ_PPB 1000000
2071509614SHubert Feurstein
214dc655d8SShenghao Yang struct mv88e6xxx_cc_coeffs {
224dc655d8SShenghao Yang u32 cc_shift;
234dc655d8SShenghao Yang u32 cc_mult;
244dc655d8SShenghao Yang u32 cc_mult_num;
254dc655d8SShenghao Yang u32 cc_mult_dem;
264dc655d8SShenghao Yang };
274dc655d8SShenghao Yang
2871509614SHubert Feurstein /* Family MV88E6250:
2971509614SHubert Feurstein * Raw timestamps are in units of 10-ns clock periods.
3071509614SHubert Feurstein *
3171509614SHubert Feurstein * clkadj = scaled_ppm * 10*2^28 / (10^6 * 2^16)
3271509614SHubert Feurstein * simplifies to
3371509614SHubert Feurstein * clkadj = scaled_ppm * 2^7 / 5^5
3471509614SHubert Feurstein */
3506b1c809SShenghao Yang #define MV88E6XXX_CC_10NS_SHIFT 28
3606b1c809SShenghao Yang static const struct mv88e6xxx_cc_coeffs mv88e6xxx_cc_10ns_coeffs = {
3706b1c809SShenghao Yang .cc_shift = MV88E6XXX_CC_10NS_SHIFT,
3806b1c809SShenghao Yang .cc_mult = 10 << MV88E6XXX_CC_10NS_SHIFT,
394dc655d8SShenghao Yang .cc_mult_num = 1 << 7,
404dc655d8SShenghao Yang .cc_mult_dem = 3125ULL,
414dc655d8SShenghao Yang };
4271509614SHubert Feurstein
43*773dc610SShenghao Yang /* Other families except MV88E6393X in internal clock mode:
4471509614SHubert Feurstein * Raw timestamps are in units of 8-ns clock periods.
4571509614SHubert Feurstein *
4671509614SHubert Feurstein * clkadj = scaled_ppm * 8*2^28 / (10^6 * 2^16)
4771509614SHubert Feurstein * simplifies to
4871509614SHubert Feurstein * clkadj = scaled_ppm * 2^9 / 5^6
4971509614SHubert Feurstein */
5006b1c809SShenghao Yang #define MV88E6XXX_CC_8NS_SHIFT 28
5106b1c809SShenghao Yang static const struct mv88e6xxx_cc_coeffs mv88e6xxx_cc_8ns_coeffs = {
5206b1c809SShenghao Yang .cc_shift = MV88E6XXX_CC_8NS_SHIFT,
5306b1c809SShenghao Yang .cc_mult = 8 << MV88E6XXX_CC_8NS_SHIFT,
544dc655d8SShenghao Yang .cc_mult_num = 1 << 9,
554dc655d8SShenghao Yang .cc_mult_dem = 15625ULL
564dc655d8SShenghao Yang };
572fa8d3afSBrandon Streiff
58*773dc610SShenghao Yang /* Family MV88E6393X using internal clock:
59*773dc610SShenghao Yang * Raw timestamps are in units of 4-ns clock periods.
60*773dc610SShenghao Yang *
61*773dc610SShenghao Yang * clkadj = scaled_ppm * 4*2^28 / (10^6 * 2^16)
62*773dc610SShenghao Yang * simplifies to
63*773dc610SShenghao Yang * clkadj = scaled_ppm * 2^8 / 5^6
64*773dc610SShenghao Yang */
65*773dc610SShenghao Yang #define MV88E6XXX_CC_4NS_SHIFT 28
66*773dc610SShenghao Yang static const struct mv88e6xxx_cc_coeffs mv88e6xxx_cc_4ns_coeffs = {
67*773dc610SShenghao Yang .cc_shift = MV88E6XXX_CC_4NS_SHIFT,
68*773dc610SShenghao Yang .cc_mult = 4 << MV88E6XXX_CC_4NS_SHIFT,
69*773dc610SShenghao Yang .cc_mult_num = 1 << 8,
70*773dc610SShenghao Yang .cc_mult_dem = 15625ULL
71*773dc610SShenghao Yang };
72*773dc610SShenghao Yang
732fa8d3afSBrandon Streiff #define TAI_EVENT_WORK_INTERVAL msecs_to_jiffies(100)
742fa8d3afSBrandon Streiff
752fa8d3afSBrandon Streiff #define cc_to_chip(cc) container_of(cc, struct mv88e6xxx_chip, tstamp_cc)
762fa8d3afSBrandon Streiff #define dw_overflow_to_chip(dw) container_of(dw, struct mv88e6xxx_chip, \
772fa8d3afSBrandon Streiff overflow_work)
784eb3be29SBrandon Streiff #define dw_tai_event_to_chip(dw) container_of(dw, struct mv88e6xxx_chip, \
794eb3be29SBrandon Streiff tai_event_work)
802fa8d3afSBrandon Streiff
mv88e6xxx_tai_read(struct mv88e6xxx_chip * chip,int addr,u16 * data,int len)812fa8d3afSBrandon Streiff static int mv88e6xxx_tai_read(struct mv88e6xxx_chip *chip, int addr,
822fa8d3afSBrandon Streiff u16 *data, int len)
832fa8d3afSBrandon Streiff {
842fa8d3afSBrandon Streiff if (!chip->info->ops->avb_ops->tai_read)
852fa8d3afSBrandon Streiff return -EOPNOTSUPP;
862fa8d3afSBrandon Streiff
872fa8d3afSBrandon Streiff return chip->info->ops->avb_ops->tai_read(chip, addr, data, len);
882fa8d3afSBrandon Streiff }
892fa8d3afSBrandon Streiff
mv88e6xxx_tai_write(struct mv88e6xxx_chip * chip,int addr,u16 data)904eb3be29SBrandon Streiff static int mv88e6xxx_tai_write(struct mv88e6xxx_chip *chip, int addr, u16 data)
914eb3be29SBrandon Streiff {
924eb3be29SBrandon Streiff if (!chip->info->ops->avb_ops->tai_write)
934eb3be29SBrandon Streiff return -EOPNOTSUPP;
944eb3be29SBrandon Streiff
954eb3be29SBrandon Streiff return chip->info->ops->avb_ops->tai_write(chip, addr, data);
964eb3be29SBrandon Streiff }
974eb3be29SBrandon Streiff
984eb3be29SBrandon Streiff /* TODO: places where this are called should be using pinctrl */
mv88e6352_set_gpio_func(struct mv88e6xxx_chip * chip,int pin,int func,int input)996d2ac8eeSAndrew Lunn static int mv88e6352_set_gpio_func(struct mv88e6xxx_chip *chip, int pin,
1004eb3be29SBrandon Streiff int func, int input)
1014eb3be29SBrandon Streiff {
1024eb3be29SBrandon Streiff int err;
1034eb3be29SBrandon Streiff
1044eb3be29SBrandon Streiff if (!chip->info->ops->gpio_ops)
1054eb3be29SBrandon Streiff return -EOPNOTSUPP;
1064eb3be29SBrandon Streiff
1074eb3be29SBrandon Streiff err = chip->info->ops->gpio_ops->set_dir(chip, pin, input);
1084eb3be29SBrandon Streiff if (err)
1094eb3be29SBrandon Streiff return err;
1104eb3be29SBrandon Streiff
1114eb3be29SBrandon Streiff return chip->info->ops->gpio_ops->set_pctl(chip, pin, func);
1124eb3be29SBrandon Streiff }
1134eb3be29SBrandon Streiff
11406b1c809SShenghao Yang static const struct mv88e6xxx_cc_coeffs *
mv88e6xxx_cc_coeff_get(struct mv88e6xxx_chip * chip)11506b1c809SShenghao Yang mv88e6xxx_cc_coeff_get(struct mv88e6xxx_chip *chip)
11606b1c809SShenghao Yang {
11706b1c809SShenghao Yang u16 period_ps;
11806b1c809SShenghao Yang int err;
11906b1c809SShenghao Yang
12006b1c809SShenghao Yang err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_CLOCK_PERIOD, &period_ps, 1);
12106b1c809SShenghao Yang if (err) {
12206b1c809SShenghao Yang dev_err(chip->dev, "failed to read cycle counter period: %d\n",
12306b1c809SShenghao Yang err);
12406b1c809SShenghao Yang return ERR_PTR(err);
12506b1c809SShenghao Yang }
12606b1c809SShenghao Yang
12706b1c809SShenghao Yang switch (period_ps) {
128*773dc610SShenghao Yang case 4000:
129*773dc610SShenghao Yang return &mv88e6xxx_cc_4ns_coeffs;
13006b1c809SShenghao Yang case 8000:
13106b1c809SShenghao Yang return &mv88e6xxx_cc_8ns_coeffs;
13206b1c809SShenghao Yang case 10000:
13306b1c809SShenghao Yang return &mv88e6xxx_cc_10ns_coeffs;
13406b1c809SShenghao Yang default:
13506b1c809SShenghao Yang dev_err(chip->dev, "unexpected cycle counter period of %u ps\n",
13606b1c809SShenghao Yang period_ps);
13706b1c809SShenghao Yang return ERR_PTR(-ENODEV);
13806b1c809SShenghao Yang }
13906b1c809SShenghao Yang }
14006b1c809SShenghao Yang
mv88e6352_ptp_clock_read(const struct cyclecounter * cc)1416d2ac8eeSAndrew Lunn static u64 mv88e6352_ptp_clock_read(const struct cyclecounter *cc)
1422fa8d3afSBrandon Streiff {
1432fa8d3afSBrandon Streiff struct mv88e6xxx_chip *chip = cc_to_chip(cc);
1442fa8d3afSBrandon Streiff u16 phc_time[2];
1452fa8d3afSBrandon Streiff int err;
1462fa8d3afSBrandon Streiff
1472fa8d3afSBrandon Streiff err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_TIME_LO, phc_time,
1482fa8d3afSBrandon Streiff ARRAY_SIZE(phc_time));
1492fa8d3afSBrandon Streiff if (err)
1502fa8d3afSBrandon Streiff return 0;
1512fa8d3afSBrandon Streiff else
1522fa8d3afSBrandon Streiff return ((u32)phc_time[1] << 16) | phc_time[0];
1532fa8d3afSBrandon Streiff }
1542fa8d3afSBrandon Streiff
mv88e6165_ptp_clock_read(const struct cyclecounter * cc)155dfa54348SAndrew Lunn static u64 mv88e6165_ptp_clock_read(const struct cyclecounter *cc)
156dfa54348SAndrew Lunn {
157dfa54348SAndrew Lunn struct mv88e6xxx_chip *chip = cc_to_chip(cc);
158dfa54348SAndrew Lunn u16 phc_time[2];
159dfa54348SAndrew Lunn int err;
160dfa54348SAndrew Lunn
161dfa54348SAndrew Lunn err = mv88e6xxx_tai_read(chip, MV88E6XXX_PTP_GC_TIME_LO, phc_time,
162dfa54348SAndrew Lunn ARRAY_SIZE(phc_time));
163dfa54348SAndrew Lunn if (err)
164dfa54348SAndrew Lunn return 0;
165dfa54348SAndrew Lunn else
166dfa54348SAndrew Lunn return ((u32)phc_time[1] << 16) | phc_time[0];
167dfa54348SAndrew Lunn }
168dfa54348SAndrew Lunn
1696d2ac8eeSAndrew Lunn /* mv88e6352_config_eventcap - configure TAI event capture
1704eb3be29SBrandon Streiff * @event: PTP_CLOCK_PPS (internal) or PTP_CLOCK_EXTTS (external)
1714eb3be29SBrandon Streiff * @rising: zero for falling-edge trigger, else rising-edge trigger
1724eb3be29SBrandon Streiff *
1734eb3be29SBrandon Streiff * This will also reset the capture sequence counter.
1744eb3be29SBrandon Streiff */
mv88e6352_config_eventcap(struct mv88e6xxx_chip * chip,int event,int rising)1756d2ac8eeSAndrew Lunn static int mv88e6352_config_eventcap(struct mv88e6xxx_chip *chip, int event,
1764eb3be29SBrandon Streiff int rising)
1774eb3be29SBrandon Streiff {
1784eb3be29SBrandon Streiff u16 global_config;
1794eb3be29SBrandon Streiff u16 cap_config;
1804eb3be29SBrandon Streiff int err;
1814eb3be29SBrandon Streiff
1824eb3be29SBrandon Streiff chip->evcap_config = MV88E6XXX_TAI_CFG_CAP_OVERWRITE |
1834eb3be29SBrandon Streiff MV88E6XXX_TAI_CFG_CAP_CTR_START;
1844eb3be29SBrandon Streiff if (!rising)
1854eb3be29SBrandon Streiff chip->evcap_config |= MV88E6XXX_TAI_CFG_EVREQ_FALLING;
1864eb3be29SBrandon Streiff
1874eb3be29SBrandon Streiff global_config = (chip->evcap_config | chip->trig_config);
1884eb3be29SBrandon Streiff err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_CFG, global_config);
1894eb3be29SBrandon Streiff if (err)
1904eb3be29SBrandon Streiff return err;
1914eb3be29SBrandon Streiff
1924eb3be29SBrandon Streiff if (event == PTP_CLOCK_PPS) {
1934eb3be29SBrandon Streiff cap_config = MV88E6XXX_TAI_EVENT_STATUS_CAP_TRIG;
1944eb3be29SBrandon Streiff } else if (event == PTP_CLOCK_EXTTS) {
1954eb3be29SBrandon Streiff /* if STATUS_CAP_TRIG is unset we capture PTP_EVREQ events */
1964eb3be29SBrandon Streiff cap_config = 0;
1974eb3be29SBrandon Streiff } else {
1984eb3be29SBrandon Streiff return -EINVAL;
1994eb3be29SBrandon Streiff }
2004eb3be29SBrandon Streiff
2014eb3be29SBrandon Streiff /* Write the capture config; this also clears the capture counter */
2024eb3be29SBrandon Streiff err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_EVENT_STATUS,
2034eb3be29SBrandon Streiff cap_config);
2044eb3be29SBrandon Streiff
2054eb3be29SBrandon Streiff return err;
2064eb3be29SBrandon Streiff }
2074eb3be29SBrandon Streiff
mv88e6352_tai_event_work(struct work_struct * ugly)2086d2ac8eeSAndrew Lunn static void mv88e6352_tai_event_work(struct work_struct *ugly)
2094eb3be29SBrandon Streiff {
2104eb3be29SBrandon Streiff struct delayed_work *dw = to_delayed_work(ugly);
2114eb3be29SBrandon Streiff struct mv88e6xxx_chip *chip = dw_tai_event_to_chip(dw);
2124eb3be29SBrandon Streiff struct ptp_clock_event ev;
2134eb3be29SBrandon Streiff u16 status[4];
2144eb3be29SBrandon Streiff u32 raw_ts;
2154eb3be29SBrandon Streiff int err;
2164eb3be29SBrandon Streiff
217c9acece0SRasmus Villemoes mv88e6xxx_reg_lock(chip);
2184eb3be29SBrandon Streiff err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_EVENT_STATUS,
2194eb3be29SBrandon Streiff status, ARRAY_SIZE(status));
220c9acece0SRasmus Villemoes mv88e6xxx_reg_unlock(chip);
2214eb3be29SBrandon Streiff
2224eb3be29SBrandon Streiff if (err) {
2234eb3be29SBrandon Streiff dev_err(chip->dev, "failed to read TAI status register\n");
2244eb3be29SBrandon Streiff return;
2254eb3be29SBrandon Streiff }
2264eb3be29SBrandon Streiff if (status[0] & MV88E6XXX_TAI_EVENT_STATUS_ERROR) {
2274eb3be29SBrandon Streiff dev_warn(chip->dev, "missed event capture\n");
2284eb3be29SBrandon Streiff return;
2294eb3be29SBrandon Streiff }
2304eb3be29SBrandon Streiff if (!(status[0] & MV88E6XXX_TAI_EVENT_STATUS_VALID))
2314eb3be29SBrandon Streiff goto out;
2324eb3be29SBrandon Streiff
2334eb3be29SBrandon Streiff raw_ts = ((u32)status[2] << 16) | status[1];
2344eb3be29SBrandon Streiff
2354eb3be29SBrandon Streiff /* Clear the valid bit so the next timestamp can come in */
2364eb3be29SBrandon Streiff status[0] &= ~MV88E6XXX_TAI_EVENT_STATUS_VALID;
237c9acece0SRasmus Villemoes mv88e6xxx_reg_lock(chip);
2384eb3be29SBrandon Streiff err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_EVENT_STATUS, status[0]);
239c9acece0SRasmus Villemoes mv88e6xxx_reg_unlock(chip);
2404eb3be29SBrandon Streiff
2414eb3be29SBrandon Streiff /* This is an external timestamp */
2424eb3be29SBrandon Streiff ev.type = PTP_CLOCK_EXTTS;
2434eb3be29SBrandon Streiff
2444eb3be29SBrandon Streiff /* We only have one timestamping channel. */
2454eb3be29SBrandon Streiff ev.index = 0;
246c9acece0SRasmus Villemoes mv88e6xxx_reg_lock(chip);
2474eb3be29SBrandon Streiff ev.timestamp = timecounter_cyc2time(&chip->tstamp_tc, raw_ts);
248c9acece0SRasmus Villemoes mv88e6xxx_reg_unlock(chip);
2494eb3be29SBrandon Streiff
2504eb3be29SBrandon Streiff ptp_clock_event(chip->ptp_clock, &ev);
2514eb3be29SBrandon Streiff out:
2524eb3be29SBrandon Streiff schedule_delayed_work(&chip->tai_event_work, TAI_EVENT_WORK_INTERVAL);
2534eb3be29SBrandon Streiff }
2544eb3be29SBrandon Streiff
mv88e6xxx_ptp_adjfine(struct ptp_clock_info * ptp,long scaled_ppm)2552fa8d3afSBrandon Streiff static int mv88e6xxx_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
2562fa8d3afSBrandon Streiff {
2572fa8d3afSBrandon Streiff struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
2582fa8d3afSBrandon Streiff int neg_adj = 0;
2592fa8d3afSBrandon Streiff u32 diff, mult;
2602fa8d3afSBrandon Streiff u64 adj;
2612fa8d3afSBrandon Streiff
2622fa8d3afSBrandon Streiff if (scaled_ppm < 0) {
2632fa8d3afSBrandon Streiff neg_adj = 1;
2642fa8d3afSBrandon Streiff scaled_ppm = -scaled_ppm;
2652fa8d3afSBrandon Streiff }
26671509614SHubert Feurstein
26706b1c809SShenghao Yang mult = chip->cc_coeffs->cc_mult;
26806b1c809SShenghao Yang adj = chip->cc_coeffs->cc_mult_num;
2692fa8d3afSBrandon Streiff adj *= scaled_ppm;
27006b1c809SShenghao Yang diff = div_u64(adj, chip->cc_coeffs->cc_mult_dem);
2712fa8d3afSBrandon Streiff
272c9acece0SRasmus Villemoes mv88e6xxx_reg_lock(chip);
2732fa8d3afSBrandon Streiff
2742fa8d3afSBrandon Streiff timecounter_read(&chip->tstamp_tc);
2752fa8d3afSBrandon Streiff chip->tstamp_cc.mult = neg_adj ? mult - diff : mult + diff;
2762fa8d3afSBrandon Streiff
277c9acece0SRasmus Villemoes mv88e6xxx_reg_unlock(chip);
2782fa8d3afSBrandon Streiff
2792fa8d3afSBrandon Streiff return 0;
2802fa8d3afSBrandon Streiff }
2812fa8d3afSBrandon Streiff
mv88e6xxx_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)2822fa8d3afSBrandon Streiff static int mv88e6xxx_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2832fa8d3afSBrandon Streiff {
2842fa8d3afSBrandon Streiff struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
2852fa8d3afSBrandon Streiff
286c9acece0SRasmus Villemoes mv88e6xxx_reg_lock(chip);
2872fa8d3afSBrandon Streiff timecounter_adjtime(&chip->tstamp_tc, delta);
288c9acece0SRasmus Villemoes mv88e6xxx_reg_unlock(chip);
2892fa8d3afSBrandon Streiff
2902fa8d3afSBrandon Streiff return 0;
2912fa8d3afSBrandon Streiff }
2922fa8d3afSBrandon Streiff
mv88e6xxx_ptp_gettime(struct ptp_clock_info * ptp,struct timespec64 * ts)2932fa8d3afSBrandon Streiff static int mv88e6xxx_ptp_gettime(struct ptp_clock_info *ptp,
2942fa8d3afSBrandon Streiff struct timespec64 *ts)
2952fa8d3afSBrandon Streiff {
2962fa8d3afSBrandon Streiff struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
2972fa8d3afSBrandon Streiff u64 ns;
2982fa8d3afSBrandon Streiff
299c9acece0SRasmus Villemoes mv88e6xxx_reg_lock(chip);
3002fa8d3afSBrandon Streiff ns = timecounter_read(&chip->tstamp_tc);
301c9acece0SRasmus Villemoes mv88e6xxx_reg_unlock(chip);
3022fa8d3afSBrandon Streiff
3032fa8d3afSBrandon Streiff *ts = ns_to_timespec64(ns);
3042fa8d3afSBrandon Streiff
3052fa8d3afSBrandon Streiff return 0;
3062fa8d3afSBrandon Streiff }
3072fa8d3afSBrandon Streiff
mv88e6xxx_ptp_settime(struct ptp_clock_info * ptp,const struct timespec64 * ts)3082fa8d3afSBrandon Streiff static int mv88e6xxx_ptp_settime(struct ptp_clock_info *ptp,
3092fa8d3afSBrandon Streiff const struct timespec64 *ts)
3102fa8d3afSBrandon Streiff {
3112fa8d3afSBrandon Streiff struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
3122fa8d3afSBrandon Streiff u64 ns;
3132fa8d3afSBrandon Streiff
3142fa8d3afSBrandon Streiff ns = timespec64_to_ns(ts);
3152fa8d3afSBrandon Streiff
316c9acece0SRasmus Villemoes mv88e6xxx_reg_lock(chip);
3172fa8d3afSBrandon Streiff timecounter_init(&chip->tstamp_tc, &chip->tstamp_cc, ns);
318c9acece0SRasmus Villemoes mv88e6xxx_reg_unlock(chip);
3192fa8d3afSBrandon Streiff
3202fa8d3afSBrandon Streiff return 0;
3212fa8d3afSBrandon Streiff }
3222fa8d3afSBrandon Streiff
mv88e6352_ptp_enable_extts(struct mv88e6xxx_chip * chip,struct ptp_clock_request * rq,int on)3236d2ac8eeSAndrew Lunn static int mv88e6352_ptp_enable_extts(struct mv88e6xxx_chip *chip,
3244eb3be29SBrandon Streiff struct ptp_clock_request *rq, int on)
3254eb3be29SBrandon Streiff {
3264eb3be29SBrandon Streiff int rising = (rq->extts.flags & PTP_RISING_EDGE);
3274eb3be29SBrandon Streiff int func;
3284eb3be29SBrandon Streiff int pin;
3294eb3be29SBrandon Streiff int err;
3304eb3be29SBrandon Streiff
3317d9465ebSJacob Keller /* Reject requests with unsupported flags */
3327d9465ebSJacob Keller if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
3337d9465ebSJacob Keller PTP_RISING_EDGE |
3346138e687SRichard Cochran PTP_FALLING_EDGE |
3356138e687SRichard Cochran PTP_STRICT_FLAGS))
3367d9465ebSJacob Keller return -EOPNOTSUPP;
3377d9465ebSJacob Keller
338c019b4beSRichard Cochran /* Reject requests to enable time stamping on both edges. */
339c019b4beSRichard Cochran if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
340c019b4beSRichard Cochran (rq->extts.flags & PTP_ENABLE_FEATURE) &&
341c019b4beSRichard Cochran (rq->extts.flags & PTP_EXTTS_EDGES) == PTP_EXTTS_EDGES)
342c019b4beSRichard Cochran return -EOPNOTSUPP;
343c019b4beSRichard Cochran
3444eb3be29SBrandon Streiff pin = ptp_find_pin(chip->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
3454eb3be29SBrandon Streiff
3464eb3be29SBrandon Streiff if (pin < 0)
3474eb3be29SBrandon Streiff return -EBUSY;
3484eb3be29SBrandon Streiff
349c9acece0SRasmus Villemoes mv88e6xxx_reg_lock(chip);
3504eb3be29SBrandon Streiff
3514eb3be29SBrandon Streiff if (on) {
3524eb3be29SBrandon Streiff func = MV88E6352_G2_SCRATCH_GPIO_PCTL_EVREQ;
3534eb3be29SBrandon Streiff
3546d2ac8eeSAndrew Lunn err = mv88e6352_set_gpio_func(chip, pin, func, true);
3554eb3be29SBrandon Streiff if (err)
3564eb3be29SBrandon Streiff goto out;
3574eb3be29SBrandon Streiff
3584eb3be29SBrandon Streiff schedule_delayed_work(&chip->tai_event_work,
3594eb3be29SBrandon Streiff TAI_EVENT_WORK_INTERVAL);
3604eb3be29SBrandon Streiff
3616d2ac8eeSAndrew Lunn err = mv88e6352_config_eventcap(chip, PTP_CLOCK_EXTTS, rising);
3624eb3be29SBrandon Streiff } else {
3634eb3be29SBrandon Streiff func = MV88E6352_G2_SCRATCH_GPIO_PCTL_GPIO;
3644eb3be29SBrandon Streiff
3656d2ac8eeSAndrew Lunn err = mv88e6352_set_gpio_func(chip, pin, func, true);
3664eb3be29SBrandon Streiff
3674eb3be29SBrandon Streiff cancel_delayed_work_sync(&chip->tai_event_work);
3684eb3be29SBrandon Streiff }
3694eb3be29SBrandon Streiff
3704eb3be29SBrandon Streiff out:
371c9acece0SRasmus Villemoes mv88e6xxx_reg_unlock(chip);
3724eb3be29SBrandon Streiff
3734eb3be29SBrandon Streiff return err;
3744eb3be29SBrandon Streiff }
3754eb3be29SBrandon Streiff
mv88e6352_ptp_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)3766d2ac8eeSAndrew Lunn static int mv88e6352_ptp_enable(struct ptp_clock_info *ptp,
3772fa8d3afSBrandon Streiff struct ptp_clock_request *rq, int on)
3782fa8d3afSBrandon Streiff {
3794eb3be29SBrandon Streiff struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
3804eb3be29SBrandon Streiff
3814eb3be29SBrandon Streiff switch (rq->type) {
3824eb3be29SBrandon Streiff case PTP_CLK_REQ_EXTTS:
3836d2ac8eeSAndrew Lunn return mv88e6352_ptp_enable_extts(chip, rq, on);
3844eb3be29SBrandon Streiff default:
3852fa8d3afSBrandon Streiff return -EOPNOTSUPP;
3862fa8d3afSBrandon Streiff }
3874eb3be29SBrandon Streiff }
3882fa8d3afSBrandon Streiff
mv88e6352_ptp_verify(struct ptp_clock_info * ptp,unsigned int pin,enum ptp_pin_function func,unsigned int chan)3896d2ac8eeSAndrew Lunn static int mv88e6352_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
3902fa8d3afSBrandon Streiff enum ptp_pin_function func, unsigned int chan)
3912fa8d3afSBrandon Streiff {
3924eb3be29SBrandon Streiff switch (func) {
3934eb3be29SBrandon Streiff case PTP_PF_NONE:
3944eb3be29SBrandon Streiff case PTP_PF_EXTTS:
3954eb3be29SBrandon Streiff break;
3964eb3be29SBrandon Streiff case PTP_PF_PEROUT:
3974eb3be29SBrandon Streiff case PTP_PF_PHYSYNC:
3982fa8d3afSBrandon Streiff return -EOPNOTSUPP;
3992fa8d3afSBrandon Streiff }
4004eb3be29SBrandon Streiff return 0;
4014eb3be29SBrandon Streiff }
4022fa8d3afSBrandon Streiff
4038858ccc8SHubert Feurstein const struct mv88e6xxx_ptp_ops mv88e6165_ptp_ops = {
4048858ccc8SHubert Feurstein .clock_read = mv88e6165_ptp_clock_read,
4058858ccc8SHubert Feurstein .global_enable = mv88e6165_global_enable,
4068858ccc8SHubert Feurstein .global_disable = mv88e6165_global_disable,
4078858ccc8SHubert Feurstein .arr0_sts_reg = MV88E6165_PORT_PTP_ARR0_STS,
4088858ccc8SHubert Feurstein .arr1_sts_reg = MV88E6165_PORT_PTP_ARR1_STS,
4098858ccc8SHubert Feurstein .dep_sts_reg = MV88E6165_PORT_PTP_DEP_STS,
4108858ccc8SHubert Feurstein .rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
4118858ccc8SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
4128858ccc8SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
4138858ccc8SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
4148858ccc8SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
4158858ccc8SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
4168858ccc8SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
41771509614SHubert Feurstein };
41871509614SHubert Feurstein
41971509614SHubert Feurstein const struct mv88e6xxx_ptp_ops mv88e6250_ptp_ops = {
42071509614SHubert Feurstein .clock_read = mv88e6352_ptp_clock_read,
42171509614SHubert Feurstein .ptp_enable = mv88e6352_ptp_enable,
42271509614SHubert Feurstein .ptp_verify = mv88e6352_ptp_verify,
42371509614SHubert Feurstein .event_work = mv88e6352_tai_event_work,
42471509614SHubert Feurstein .port_enable = mv88e6352_hwtstamp_port_enable,
42571509614SHubert Feurstein .port_disable = mv88e6352_hwtstamp_port_disable,
42671509614SHubert Feurstein .n_ext_ts = 1,
42771509614SHubert Feurstein .arr0_sts_reg = MV88E6XXX_PORT_PTP_ARR0_STS,
42871509614SHubert Feurstein .arr1_sts_reg = MV88E6XXX_PORT_PTP_ARR1_STS,
42971509614SHubert Feurstein .dep_sts_reg = MV88E6XXX_PORT_PTP_DEP_STS,
43071509614SHubert Feurstein .rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
43171509614SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
43271509614SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
43371509614SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
43471509614SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
43571509614SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
43671509614SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
43771509614SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
43871509614SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
43971509614SHubert Feurstein (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
4408858ccc8SHubert Feurstein };
4418858ccc8SHubert Feurstein
4426d2ac8eeSAndrew Lunn const struct mv88e6xxx_ptp_ops mv88e6352_ptp_ops = {
4436d2ac8eeSAndrew Lunn .clock_read = mv88e6352_ptp_clock_read,
4446d2ac8eeSAndrew Lunn .ptp_enable = mv88e6352_ptp_enable,
4456d2ac8eeSAndrew Lunn .ptp_verify = mv88e6352_ptp_verify,
4466d2ac8eeSAndrew Lunn .event_work = mv88e6352_tai_event_work,
447ffc705deSAndrew Lunn .port_enable = mv88e6352_hwtstamp_port_enable,
448ffc705deSAndrew Lunn .port_disable = mv88e6352_hwtstamp_port_disable,
4496d2ac8eeSAndrew Lunn .n_ext_ts = 1,
450ffc705deSAndrew Lunn .arr0_sts_reg = MV88E6XXX_PORT_PTP_ARR0_STS,
451ffc705deSAndrew Lunn .arr1_sts_reg = MV88E6XXX_PORT_PTP_ARR1_STS,
452ffc705deSAndrew Lunn .dep_sts_reg = MV88E6XXX_PORT_PTP_DEP_STS,
45348cb5e03SAndrew Lunn .rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
45448cb5e03SAndrew Lunn (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
45548cb5e03SAndrew Lunn (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
45648cb5e03SAndrew Lunn (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
45748cb5e03SAndrew Lunn (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
45848cb5e03SAndrew Lunn (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
45948cb5e03SAndrew Lunn (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
46048cb5e03SAndrew Lunn (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
46148cb5e03SAndrew Lunn (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
46248cb5e03SAndrew Lunn (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
4636d2ac8eeSAndrew Lunn };
4646d2ac8eeSAndrew Lunn
4659627c981SKurt Kanzenbach const struct mv88e6xxx_ptp_ops mv88e6390_ptp_ops = {
4669627c981SKurt Kanzenbach .clock_read = mv88e6352_ptp_clock_read,
4679627c981SKurt Kanzenbach .ptp_enable = mv88e6352_ptp_enable,
4689627c981SKurt Kanzenbach .ptp_verify = mv88e6352_ptp_verify,
4699627c981SKurt Kanzenbach .event_work = mv88e6352_tai_event_work,
4709627c981SKurt Kanzenbach .port_enable = mv88e6352_hwtstamp_port_enable,
4719627c981SKurt Kanzenbach .port_disable = mv88e6352_hwtstamp_port_disable,
4729627c981SKurt Kanzenbach .set_ptp_cpu_port = mv88e6390_g1_set_ptp_cpu_port,
4739627c981SKurt Kanzenbach .n_ext_ts = 1,
4749627c981SKurt Kanzenbach .arr0_sts_reg = MV88E6XXX_PORT_PTP_ARR0_STS,
4759627c981SKurt Kanzenbach .arr1_sts_reg = MV88E6XXX_PORT_PTP_ARR1_STS,
4769627c981SKurt Kanzenbach .dep_sts_reg = MV88E6XXX_PORT_PTP_DEP_STS,
4779627c981SKurt Kanzenbach .rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
4789627c981SKurt Kanzenbach (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
4799627c981SKurt Kanzenbach (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
4809627c981SKurt Kanzenbach (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
4819627c981SKurt Kanzenbach (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
4829627c981SKurt Kanzenbach (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
4839627c981SKurt Kanzenbach (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
4849627c981SKurt Kanzenbach (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
4859627c981SKurt Kanzenbach (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
4869627c981SKurt Kanzenbach (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
4879627c981SKurt Kanzenbach };
4889627c981SKurt Kanzenbach
mv88e6xxx_ptp_clock_read(const struct cyclecounter * cc)4896d2ac8eeSAndrew Lunn static u64 mv88e6xxx_ptp_clock_read(const struct cyclecounter *cc)
4906d2ac8eeSAndrew Lunn {
4916d2ac8eeSAndrew Lunn struct mv88e6xxx_chip *chip = cc_to_chip(cc);
4926d2ac8eeSAndrew Lunn
4936d2ac8eeSAndrew Lunn if (chip->info->ops->ptp_ops->clock_read)
4946d2ac8eeSAndrew Lunn return chip->info->ops->ptp_ops->clock_read(cc);
4956d2ac8eeSAndrew Lunn
4966d2ac8eeSAndrew Lunn return 0;
4976d2ac8eeSAndrew Lunn }
4986d2ac8eeSAndrew Lunn
499*773dc610SShenghao Yang /* With a 250MHz input clock, the 32-bit timestamp counter overflows in ~17.2
5002fa8d3afSBrandon Streiff * seconds; this task forces periodic reads so that we don't miss any.
5012fa8d3afSBrandon Streiff */
502*773dc610SShenghao Yang #define MV88E6XXX_TAI_OVERFLOW_PERIOD (HZ * 8)
mv88e6xxx_ptp_overflow_check(struct work_struct * work)5032fa8d3afSBrandon Streiff static void mv88e6xxx_ptp_overflow_check(struct work_struct *work)
5042fa8d3afSBrandon Streiff {
5052fa8d3afSBrandon Streiff struct delayed_work *dw = to_delayed_work(work);
5062fa8d3afSBrandon Streiff struct mv88e6xxx_chip *chip = dw_overflow_to_chip(dw);
5072fa8d3afSBrandon Streiff struct timespec64 ts;
5082fa8d3afSBrandon Streiff
5092fa8d3afSBrandon Streiff mv88e6xxx_ptp_gettime(&chip->ptp_clock_info, &ts);
5102fa8d3afSBrandon Streiff
5112fa8d3afSBrandon Streiff schedule_delayed_work(&chip->overflow_work,
5122fa8d3afSBrandon Streiff MV88E6XXX_TAI_OVERFLOW_PERIOD);
5132fa8d3afSBrandon Streiff }
5142fa8d3afSBrandon Streiff
mv88e6xxx_ptp_setup(struct mv88e6xxx_chip * chip)5152fa8d3afSBrandon Streiff int mv88e6xxx_ptp_setup(struct mv88e6xxx_chip *chip)
5162fa8d3afSBrandon Streiff {
5176d2ac8eeSAndrew Lunn const struct mv88e6xxx_ptp_ops *ptp_ops = chip->info->ops->ptp_ops;
5184eb3be29SBrandon Streiff int i;
5194eb3be29SBrandon Streiff
5202fa8d3afSBrandon Streiff /* Set up the cycle counter */
52106b1c809SShenghao Yang chip->cc_coeffs = mv88e6xxx_cc_coeff_get(chip);
52206b1c809SShenghao Yang if (IS_ERR(chip->cc_coeffs))
52306b1c809SShenghao Yang return PTR_ERR(chip->cc_coeffs);
52406b1c809SShenghao Yang
5252fa8d3afSBrandon Streiff memset(&chip->tstamp_cc, 0, sizeof(chip->tstamp_cc));
5262fa8d3afSBrandon Streiff chip->tstamp_cc.read = mv88e6xxx_ptp_clock_read;
5272fa8d3afSBrandon Streiff chip->tstamp_cc.mask = CYCLECOUNTER_MASK(32);
52806b1c809SShenghao Yang chip->tstamp_cc.mult = chip->cc_coeffs->cc_mult;
52906b1c809SShenghao Yang chip->tstamp_cc.shift = chip->cc_coeffs->cc_shift;
5302fa8d3afSBrandon Streiff
5312fa8d3afSBrandon Streiff timecounter_init(&chip->tstamp_tc, &chip->tstamp_cc,
5322fa8d3afSBrandon Streiff ktime_to_ns(ktime_get_real()));
5332fa8d3afSBrandon Streiff
5342fa8d3afSBrandon Streiff INIT_DELAYED_WORK(&chip->overflow_work, mv88e6xxx_ptp_overflow_check);
5356d2ac8eeSAndrew Lunn if (ptp_ops->event_work)
5366d2ac8eeSAndrew Lunn INIT_DELAYED_WORK(&chip->tai_event_work, ptp_ops->event_work);
5372fa8d3afSBrandon Streiff
5382fa8d3afSBrandon Streiff chip->ptp_clock_info.owner = THIS_MODULE;
5392fa8d3afSBrandon Streiff snprintf(chip->ptp_clock_info.name, sizeof(chip->ptp_clock_info.name),
5403f8b8696SFlorian Fainelli "%s", dev_name(chip->dev));
5412fa8d3afSBrandon Streiff
5426d2ac8eeSAndrew Lunn chip->ptp_clock_info.n_ext_ts = ptp_ops->n_ext_ts;
5434eb3be29SBrandon Streiff chip->ptp_clock_info.n_per_out = 0;
5444eb3be29SBrandon Streiff chip->ptp_clock_info.n_pins = mv88e6xxx_num_gpio(chip);
5454eb3be29SBrandon Streiff chip->ptp_clock_info.pps = 0;
5464eb3be29SBrandon Streiff
5474eb3be29SBrandon Streiff for (i = 0; i < chip->ptp_clock_info.n_pins; ++i) {
5484eb3be29SBrandon Streiff struct ptp_pin_desc *ppd = &chip->pin_config[i];
5494eb3be29SBrandon Streiff
5504eb3be29SBrandon Streiff snprintf(ppd->name, sizeof(ppd->name), "mv88e6xxx_gpio%d", i);
5514eb3be29SBrandon Streiff ppd->index = i;
5524eb3be29SBrandon Streiff ppd->func = PTP_PF_NONE;
5534eb3be29SBrandon Streiff }
5544eb3be29SBrandon Streiff chip->ptp_clock_info.pin_config = chip->pin_config;
5554eb3be29SBrandon Streiff
55671509614SHubert Feurstein chip->ptp_clock_info.max_adj = MV88E6XXX_MAX_ADJ_PPB;
5572fa8d3afSBrandon Streiff chip->ptp_clock_info.adjfine = mv88e6xxx_ptp_adjfine;
5582fa8d3afSBrandon Streiff chip->ptp_clock_info.adjtime = mv88e6xxx_ptp_adjtime;
5592fa8d3afSBrandon Streiff chip->ptp_clock_info.gettime64 = mv88e6xxx_ptp_gettime;
5602fa8d3afSBrandon Streiff chip->ptp_clock_info.settime64 = mv88e6xxx_ptp_settime;
5616d2ac8eeSAndrew Lunn chip->ptp_clock_info.enable = ptp_ops->ptp_enable;
5626d2ac8eeSAndrew Lunn chip->ptp_clock_info.verify = ptp_ops->ptp_verify;
563c6fe0ad2SBrandon Streiff chip->ptp_clock_info.do_aux_work = mv88e6xxx_hwtstamp_work;
5642fa8d3afSBrandon Streiff
5659627c981SKurt Kanzenbach if (ptp_ops->set_ptp_cpu_port) {
5669627c981SKurt Kanzenbach struct dsa_port *dp;
5679627c981SKurt Kanzenbach int upstream = 0;
5689627c981SKurt Kanzenbach int err;
5699627c981SKurt Kanzenbach
5709627c981SKurt Kanzenbach dsa_switch_for_each_user_port(dp, chip->ds) {
5719627c981SKurt Kanzenbach upstream = dsa_upstream_port(chip->ds, dp->index);
5729627c981SKurt Kanzenbach break;
5739627c981SKurt Kanzenbach }
5749627c981SKurt Kanzenbach
5759627c981SKurt Kanzenbach err = ptp_ops->set_ptp_cpu_port(chip, upstream);
5769627c981SKurt Kanzenbach if (err) {
5779627c981SKurt Kanzenbach dev_err(chip->dev, "Failed to set PTP CPU destination port!\n");
5789627c981SKurt Kanzenbach return err;
5799627c981SKurt Kanzenbach }
5809627c981SKurt Kanzenbach }
5819627c981SKurt Kanzenbach
5822fa8d3afSBrandon Streiff chip->ptp_clock = ptp_clock_register(&chip->ptp_clock_info, chip->dev);
5832fa8d3afSBrandon Streiff if (IS_ERR(chip->ptp_clock))
5842fa8d3afSBrandon Streiff return PTR_ERR(chip->ptp_clock);
5852fa8d3afSBrandon Streiff
5862fa8d3afSBrandon Streiff schedule_delayed_work(&chip->overflow_work,
5872fa8d3afSBrandon Streiff MV88E6XXX_TAI_OVERFLOW_PERIOD);
5882fa8d3afSBrandon Streiff
5892fa8d3afSBrandon Streiff return 0;
5902fa8d3afSBrandon Streiff }
5912fa8d3afSBrandon Streiff
mv88e6xxx_ptp_free(struct mv88e6xxx_chip * chip)5922fa8d3afSBrandon Streiff void mv88e6xxx_ptp_free(struct mv88e6xxx_chip *chip)
5932fa8d3afSBrandon Streiff {
5942fa8d3afSBrandon Streiff if (chip->ptp_clock) {
5952fa8d3afSBrandon Streiff cancel_delayed_work_sync(&chip->overflow_work);
5966d2ac8eeSAndrew Lunn if (chip->info->ops->ptp_ops->event_work)
5974eb3be29SBrandon Streiff cancel_delayed_work_sync(&chip->tai_event_work);
5982fa8d3afSBrandon Streiff
5992fa8d3afSBrandon Streiff ptp_clock_unregister(chip->ptp_clock);
6002fa8d3afSBrandon Streiff chip->ptp_clock = NULL;
6012fa8d3afSBrandon Streiff }
6022fa8d3afSBrandon Streiff }
603