Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the National Semiconductor DP83640 PHYTER |
| 3 | * |
| 4 | * Copyright (C) 2010 OMICRON electronics GmbH |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 20 | |
| 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 22 | |
Stefan Sørensen | 539e44d | 2015-11-03 09:34:04 +0100 | [diff] [blame] | 23 | #include <linux/crc32.h> |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 24 | #include <linux/ethtool.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/mii.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/net_tstamp.h> |
| 30 | #include <linux/netdevice.h> |
Daniel Borkmann | 408eccc | 2014-04-01 16:20:23 +0200 | [diff] [blame] | 31 | #include <linux/if_vlan.h> |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 32 | #include <linux/phy.h> |
| 33 | #include <linux/ptp_classify.h> |
| 34 | #include <linux/ptp_clock_kernel.h> |
| 35 | |
| 36 | #include "dp83640_reg.h" |
| 37 | |
| 38 | #define DP83640_PHY_ID 0x20005ce1 |
| 39 | #define PAGESEL 0x13 |
Richard Cochran | 8028837 | 2011-08-06 21:03:04 +0000 | [diff] [blame] | 40 | #define MAX_RXTS 64 |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 41 | #define N_EXT_TS 6 |
Stefan Sørensen | ad01577 | 2014-06-27 12:05:30 +0200 | [diff] [blame] | 42 | #define N_PER_OUT 7 |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 43 | #define PSF_PTPVER 2 |
| 44 | #define PSF_EVNT 0x4000 |
| 45 | #define PSF_RX 0x2000 |
| 46 | #define PSF_TX 0x1000 |
| 47 | #define EXT_EVENT 1 |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 48 | #define CAL_EVENT 7 |
Richard Cochran | 397a253 | 2015-05-25 11:55:43 +0200 | [diff] [blame] | 49 | #define CAL_TRIGGER 1 |
Richard Cochran | 86dd361 | 2014-03-20 22:21:58 +0100 | [diff] [blame] | 50 | #define DP83640_N_PINS 12 |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 51 | |
Stephan Gatzka | 1642182 | 2012-12-04 10:21:38 +0000 | [diff] [blame] | 52 | #define MII_DP83640_MICR 0x11 |
| 53 | #define MII_DP83640_MISR 0x12 |
| 54 | |
| 55 | #define MII_DP83640_MICR_OE 0x1 |
| 56 | #define MII_DP83640_MICR_IE 0x2 |
| 57 | |
| 58 | #define MII_DP83640_MISR_RHF_INT_EN 0x01 |
| 59 | #define MII_DP83640_MISR_FHF_INT_EN 0x02 |
| 60 | #define MII_DP83640_MISR_ANC_INT_EN 0x04 |
| 61 | #define MII_DP83640_MISR_DUP_INT_EN 0x08 |
| 62 | #define MII_DP83640_MISR_SPD_INT_EN 0x10 |
| 63 | #define MII_DP83640_MISR_LINK_INT_EN 0x20 |
| 64 | #define MII_DP83640_MISR_ED_INT_EN 0x40 |
| 65 | #define MII_DP83640_MISR_LQ_INT_EN 0x80 |
| 66 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 67 | /* phyter seems to miss the mark by 16 ns */ |
| 68 | #define ADJTIME_FIX 16 |
| 69 | |
Stefan Sørensen | 4b06325 | 2015-11-03 09:34:05 +0100 | [diff] [blame] | 70 | #define SKB_TIMESTAMP_TIMEOUT 2 /* jiffies */ |
| 71 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 72 | #if defined(__BIG_ENDIAN) |
| 73 | #define ENDIAN_FLAG 0 |
| 74 | #elif defined(__LITTLE_ENDIAN) |
| 75 | #define ENDIAN_FLAG PSF_ENDIAN |
| 76 | #endif |
| 77 | |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 78 | struct dp83640_skb_info { |
| 79 | int ptp_type; |
| 80 | unsigned long tmo; |
| 81 | }; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 82 | |
| 83 | struct phy_rxts { |
| 84 | u16 ns_lo; /* ns[15:0] */ |
| 85 | u16 ns_hi; /* overflow[1:0], ns[29:16] */ |
| 86 | u16 sec_lo; /* sec[15:0] */ |
| 87 | u16 sec_hi; /* sec[31:16] */ |
| 88 | u16 seqid; /* sequenceId[15:0] */ |
| 89 | u16 msgtype; /* messageType[3:0], hash[11:0] */ |
| 90 | }; |
| 91 | |
| 92 | struct phy_txts { |
| 93 | u16 ns_lo; /* ns[15:0] */ |
| 94 | u16 ns_hi; /* overflow[1:0], ns[29:16] */ |
| 95 | u16 sec_lo; /* sec[15:0] */ |
| 96 | u16 sec_hi; /* sec[31:16] */ |
| 97 | }; |
| 98 | |
| 99 | struct rxts { |
| 100 | struct list_head list; |
| 101 | unsigned long tmo; |
| 102 | u64 ns; |
| 103 | u16 seqid; |
| 104 | u8 msgtype; |
| 105 | u16 hash; |
| 106 | }; |
| 107 | |
| 108 | struct dp83640_clock; |
| 109 | |
| 110 | struct dp83640_private { |
| 111 | struct list_head list; |
| 112 | struct dp83640_clock *clock; |
| 113 | struct phy_device *phydev; |
Stefan Sørensen | 4b06325 | 2015-11-03 09:34:05 +0100 | [diff] [blame] | 114 | struct delayed_work ts_work; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 115 | int hwts_tx_en; |
| 116 | int hwts_rx_en; |
| 117 | int layer; |
| 118 | int version; |
| 119 | /* remember state of cfg0 during calibration */ |
| 120 | int cfg0; |
| 121 | /* remember the last event time stamp */ |
| 122 | struct phy_txts edata; |
| 123 | /* list of rx timestamps */ |
| 124 | struct list_head rxts; |
| 125 | struct list_head rxpool; |
| 126 | struct rxts rx_pool_data[MAX_RXTS]; |
| 127 | /* protects above three fields from concurrent access */ |
| 128 | spinlock_t rx_lock; |
| 129 | /* queues of incoming and outgoing packets */ |
| 130 | struct sk_buff_head rx_queue; |
| 131 | struct sk_buff_head tx_queue; |
| 132 | }; |
| 133 | |
| 134 | struct dp83640_clock { |
| 135 | /* keeps the instance in the 'phyter_clocks' list */ |
| 136 | struct list_head list; |
| 137 | /* we create one clock instance per MII bus */ |
| 138 | struct mii_bus *bus; |
| 139 | /* protects extended registers from concurrent access */ |
| 140 | struct mutex extreg_lock; |
| 141 | /* remembers which page was last selected */ |
| 142 | int page; |
| 143 | /* our advertised capabilities */ |
| 144 | struct ptp_clock_info caps; |
| 145 | /* protects the three fields below from concurrent access */ |
| 146 | struct mutex clock_lock; |
| 147 | /* the one phyter from which we shall read */ |
| 148 | struct dp83640_private *chosen; |
| 149 | /* list of the other attached phyters, not chosen */ |
| 150 | struct list_head phylist; |
| 151 | /* reference to our PTP hardware clock */ |
| 152 | struct ptp_clock *ptp_clock; |
| 153 | }; |
| 154 | |
| 155 | /* globals */ |
| 156 | |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 157 | enum { |
| 158 | CALIBRATE_GPIO, |
| 159 | PEROUT_GPIO, |
| 160 | EXTTS0_GPIO, |
| 161 | EXTTS1_GPIO, |
| 162 | EXTTS2_GPIO, |
| 163 | EXTTS3_GPIO, |
| 164 | EXTTS4_GPIO, |
| 165 | EXTTS5_GPIO, |
| 166 | GPIO_TABLE_SIZE |
| 167 | }; |
| 168 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 169 | static int chosen_phy = -1; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 170 | static ushort gpio_tab[GPIO_TABLE_SIZE] = { |
| 171 | 1, 2, 3, 4, 8, 9, 10, 11 |
| 172 | }; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 173 | |
| 174 | module_param(chosen_phy, int, 0444); |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 175 | module_param_array(gpio_tab, ushort, NULL, 0444); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 176 | |
| 177 | MODULE_PARM_DESC(chosen_phy, \ |
| 178 | "The address of the PHY to use for the ancillary clock features"); |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 179 | MODULE_PARM_DESC(gpio_tab, \ |
| 180 | "Which GPIO line to use for which purpose: cal,perout,extts1,...,extts6"); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 181 | |
Richard Cochran | 86dd361 | 2014-03-20 22:21:58 +0100 | [diff] [blame] | 182 | static void dp83640_gpio_defaults(struct ptp_pin_desc *pd) |
| 183 | { |
| 184 | int i, index; |
| 185 | |
| 186 | for (i = 0; i < DP83640_N_PINS; i++) { |
| 187 | snprintf(pd[i].name, sizeof(pd[i].name), "GPIO%d", 1 + i); |
| 188 | pd[i].index = i; |
| 189 | } |
| 190 | |
| 191 | for (i = 0; i < GPIO_TABLE_SIZE; i++) { |
| 192 | if (gpio_tab[i] < 1 || gpio_tab[i] > DP83640_N_PINS) { |
| 193 | pr_err("gpio_tab[%d]=%hu out of range", i, gpio_tab[i]); |
| 194 | return; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | index = gpio_tab[CALIBRATE_GPIO] - 1; |
| 199 | pd[index].func = PTP_PF_PHYSYNC; |
| 200 | pd[index].chan = 0; |
| 201 | |
| 202 | index = gpio_tab[PEROUT_GPIO] - 1; |
| 203 | pd[index].func = PTP_PF_PEROUT; |
| 204 | pd[index].chan = 0; |
| 205 | |
| 206 | for (i = EXTTS0_GPIO; i < GPIO_TABLE_SIZE; i++) { |
| 207 | index = gpio_tab[i] - 1; |
| 208 | pd[index].func = PTP_PF_EXTTS; |
| 209 | pd[index].chan = i - EXTTS0_GPIO; |
| 210 | } |
| 211 | } |
| 212 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 213 | /* a list of clocks and a mutex to protect it */ |
| 214 | static LIST_HEAD(phyter_clocks); |
| 215 | static DEFINE_MUTEX(phyter_clocks_lock); |
| 216 | |
| 217 | static void rx_timestamp_work(struct work_struct *work); |
| 218 | |
| 219 | /* extended register access functions */ |
| 220 | |
| 221 | #define BROADCAST_ADDR 31 |
| 222 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 223 | static inline int broadcast_write(struct phy_device *phydev, u32 regnum, |
| 224 | u16 val) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 225 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 226 | return mdiobus_write(phydev->mdio.bus, BROADCAST_ADDR, regnum, val); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* Caller must hold extreg_lock. */ |
| 230 | static int ext_read(struct phy_device *phydev, int page, u32 regnum) |
| 231 | { |
| 232 | struct dp83640_private *dp83640 = phydev->priv; |
| 233 | int val; |
| 234 | |
| 235 | if (dp83640->clock->page != page) { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 236 | broadcast_write(phydev, PAGESEL, page); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 237 | dp83640->clock->page = page; |
| 238 | } |
| 239 | val = phy_read(phydev, regnum); |
| 240 | |
| 241 | return val; |
| 242 | } |
| 243 | |
| 244 | /* Caller must hold extreg_lock. */ |
| 245 | static void ext_write(int broadcast, struct phy_device *phydev, |
| 246 | int page, u32 regnum, u16 val) |
| 247 | { |
| 248 | struct dp83640_private *dp83640 = phydev->priv; |
| 249 | |
| 250 | if (dp83640->clock->page != page) { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 251 | broadcast_write(phydev, PAGESEL, page); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 252 | dp83640->clock->page = page; |
| 253 | } |
| 254 | if (broadcast) |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 255 | broadcast_write(phydev, regnum, val); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 256 | else |
| 257 | phy_write(phydev, regnum, val); |
| 258 | } |
| 259 | |
| 260 | /* Caller must hold extreg_lock. */ |
| 261 | static int tdr_write(int bc, struct phy_device *dev, |
Richard Cochran | 41c2c18 | 2015-03-29 23:12:10 +0200 | [diff] [blame] | 262 | const struct timespec64 *ts, u16 cmd) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 263 | { |
| 264 | ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec & 0xffff);/* ns[15:0] */ |
| 265 | ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec >> 16); /* ns[31:16] */ |
| 266 | ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec & 0xffff); /* sec[15:0] */ |
| 267 | ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec >> 16); /* sec[31:16]*/ |
| 268 | |
| 269 | ext_write(bc, dev, PAGE4, PTP_CTL, cmd); |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | /* convert phy timestamps into driver timestamps */ |
| 275 | |
| 276 | static void phy2rxts(struct phy_rxts *p, struct rxts *rxts) |
| 277 | { |
| 278 | u32 sec; |
| 279 | |
| 280 | sec = p->sec_lo; |
| 281 | sec |= p->sec_hi << 16; |
| 282 | |
| 283 | rxts->ns = p->ns_lo; |
| 284 | rxts->ns |= (p->ns_hi & 0x3fff) << 16; |
| 285 | rxts->ns += ((u64)sec) * 1000000000ULL; |
| 286 | rxts->seqid = p->seqid; |
| 287 | rxts->msgtype = (p->msgtype >> 12) & 0xf; |
| 288 | rxts->hash = p->msgtype & 0x0fff; |
Stefan Sørensen | 4b06325 | 2015-11-03 09:34:05 +0100 | [diff] [blame] | 289 | rxts->tmo = jiffies + SKB_TIMESTAMP_TIMEOUT; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static u64 phy2txts(struct phy_txts *p) |
| 293 | { |
| 294 | u64 ns; |
| 295 | u32 sec; |
| 296 | |
| 297 | sec = p->sec_lo; |
| 298 | sec |= p->sec_hi << 16; |
| 299 | |
| 300 | ns = p->ns_lo; |
| 301 | ns |= (p->ns_hi & 0x3fff) << 16; |
| 302 | ns += ((u64)sec) * 1000000000ULL; |
| 303 | |
| 304 | return ns; |
| 305 | } |
| 306 | |
Richard Cochran | 621bdec | 2014-03-20 22:22:00 +0100 | [diff] [blame] | 307 | static int periodic_output(struct dp83640_clock *clock, |
Stefan Sørensen | ad01577 | 2014-06-27 12:05:30 +0200 | [diff] [blame] | 308 | struct ptp_clock_request *clkreq, bool on, |
| 309 | int trigger) |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 310 | { |
| 311 | struct dp83640_private *dp83640 = clock->chosen; |
| 312 | struct phy_device *phydev = dp83640->phydev; |
Richard Cochran | 564ca56 | 2014-03-20 22:21:57 +0100 | [diff] [blame] | 313 | u32 sec, nsec, pwidth; |
Stefan Sørensen | ad01577 | 2014-06-27 12:05:30 +0200 | [diff] [blame] | 314 | u16 gpio, ptp_trig, val; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 315 | |
Richard Cochran | 621bdec | 2014-03-20 22:22:00 +0100 | [diff] [blame] | 316 | if (on) { |
Stefan Sørensen | ad01577 | 2014-06-27 12:05:30 +0200 | [diff] [blame] | 317 | gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PEROUT, |
| 318 | trigger); |
Richard Cochran | 621bdec | 2014-03-20 22:22:00 +0100 | [diff] [blame] | 319 | if (gpio < 1) |
| 320 | return -EINVAL; |
| 321 | } else { |
| 322 | gpio = 0; |
| 323 | } |
| 324 | |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 325 | ptp_trig = TRIG_WR | |
| 326 | (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT | |
| 327 | (gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT | |
| 328 | TRIG_PER | |
| 329 | TRIG_PULSE; |
| 330 | |
| 331 | val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT; |
| 332 | |
| 333 | if (!on) { |
| 334 | val |= TRIG_DIS; |
| 335 | mutex_lock(&clock->extreg_lock); |
| 336 | ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig); |
| 337 | ext_write(0, phydev, PAGE4, PTP_CTL, val); |
| 338 | mutex_unlock(&clock->extreg_lock); |
Richard Cochran | 621bdec | 2014-03-20 22:22:00 +0100 | [diff] [blame] | 339 | return 0; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | sec = clkreq->perout.start.sec; |
| 343 | nsec = clkreq->perout.start.nsec; |
Richard Cochran | 564ca56 | 2014-03-20 22:21:57 +0100 | [diff] [blame] | 344 | pwidth = clkreq->perout.period.sec * 1000000000UL; |
| 345 | pwidth += clkreq->perout.period.nsec; |
| 346 | pwidth /= 2; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 347 | |
| 348 | mutex_lock(&clock->extreg_lock); |
| 349 | |
| 350 | ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig); |
| 351 | |
| 352 | /*load trigger*/ |
| 353 | val |= TRIG_LOAD; |
| 354 | ext_write(0, phydev, PAGE4, PTP_CTL, val); |
| 355 | ext_write(0, phydev, PAGE4, PTP_TDR, nsec & 0xffff); /* ns[15:0] */ |
| 356 | ext_write(0, phydev, PAGE4, PTP_TDR, nsec >> 16); /* ns[31:16] */ |
| 357 | ext_write(0, phydev, PAGE4, PTP_TDR, sec & 0xffff); /* sec[15:0] */ |
| 358 | ext_write(0, phydev, PAGE4, PTP_TDR, sec >> 16); /* sec[31:16] */ |
Richard Cochran | 564ca56 | 2014-03-20 22:21:57 +0100 | [diff] [blame] | 359 | ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff); /* ns[15:0] */ |
| 360 | ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16); /* ns[31:16] */ |
Stefan Sørensen | 35e872a | 2014-06-27 12:05:29 +0200 | [diff] [blame] | 361 | /* Triggers 0 and 1 has programmable pulsewidth2 */ |
| 362 | if (trigger < 2) { |
| 363 | ext_write(0, phydev, PAGE4, PTP_TDR, pwidth & 0xffff); |
| 364 | ext_write(0, phydev, PAGE4, PTP_TDR, pwidth >> 16); |
| 365 | } |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 366 | |
| 367 | /*enable trigger*/ |
| 368 | val &= ~TRIG_LOAD; |
| 369 | val |= TRIG_EN; |
| 370 | ext_write(0, phydev, PAGE4, PTP_CTL, val); |
| 371 | |
| 372 | mutex_unlock(&clock->extreg_lock); |
Richard Cochran | 621bdec | 2014-03-20 22:22:00 +0100 | [diff] [blame] | 373 | return 0; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 376 | /* ptp clock methods */ |
| 377 | |
Richard Cochran | e4788b8 | 2016-11-08 22:49:18 +0100 | [diff] [blame] | 378 | static int ptp_dp83640_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 379 | { |
| 380 | struct dp83640_clock *clock = |
| 381 | container_of(ptp, struct dp83640_clock, caps); |
| 382 | struct phy_device *phydev = clock->chosen->phydev; |
| 383 | u64 rate; |
| 384 | int neg_adj = 0; |
| 385 | u16 hi, lo; |
| 386 | |
Richard Cochran | e4788b8 | 2016-11-08 22:49:18 +0100 | [diff] [blame] | 387 | if (scaled_ppm < 0) { |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 388 | neg_adj = 1; |
Richard Cochran | e4788b8 | 2016-11-08 22:49:18 +0100 | [diff] [blame] | 389 | scaled_ppm = -scaled_ppm; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 390 | } |
Richard Cochran | e4788b8 | 2016-11-08 22:49:18 +0100 | [diff] [blame] | 391 | rate = scaled_ppm; |
| 392 | rate <<= 13; |
| 393 | rate = div_u64(rate, 15625); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 394 | |
| 395 | hi = (rate >> 16) & PTP_RATE_HI_MASK; |
| 396 | if (neg_adj) |
| 397 | hi |= PTP_RATE_DIR; |
| 398 | |
| 399 | lo = rate & 0xffff; |
| 400 | |
| 401 | mutex_lock(&clock->extreg_lock); |
| 402 | |
| 403 | ext_write(1, phydev, PAGE4, PTP_RATEH, hi); |
| 404 | ext_write(1, phydev, PAGE4, PTP_RATEL, lo); |
| 405 | |
| 406 | mutex_unlock(&clock->extreg_lock); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int ptp_dp83640_adjtime(struct ptp_clock_info *ptp, s64 delta) |
| 412 | { |
| 413 | struct dp83640_clock *clock = |
| 414 | container_of(ptp, struct dp83640_clock, caps); |
| 415 | struct phy_device *phydev = clock->chosen->phydev; |
Richard Cochran | 41c2c18 | 2015-03-29 23:12:10 +0200 | [diff] [blame] | 416 | struct timespec64 ts; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 417 | int err; |
| 418 | |
| 419 | delta += ADJTIME_FIX; |
| 420 | |
Richard Cochran | 41c2c18 | 2015-03-29 23:12:10 +0200 | [diff] [blame] | 421 | ts = ns_to_timespec64(delta); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 422 | |
| 423 | mutex_lock(&clock->extreg_lock); |
| 424 | |
| 425 | err = tdr_write(1, phydev, &ts, PTP_STEP_CLK); |
| 426 | |
| 427 | mutex_unlock(&clock->extreg_lock); |
| 428 | |
| 429 | return err; |
| 430 | } |
| 431 | |
Richard Cochran | 41c2c18 | 2015-03-29 23:12:10 +0200 | [diff] [blame] | 432 | static int ptp_dp83640_gettime(struct ptp_clock_info *ptp, |
| 433 | struct timespec64 *ts) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 434 | { |
| 435 | struct dp83640_clock *clock = |
| 436 | container_of(ptp, struct dp83640_clock, caps); |
| 437 | struct phy_device *phydev = clock->chosen->phydev; |
| 438 | unsigned int val[4]; |
| 439 | |
| 440 | mutex_lock(&clock->extreg_lock); |
| 441 | |
| 442 | ext_write(0, phydev, PAGE4, PTP_CTL, PTP_RD_CLK); |
| 443 | |
| 444 | val[0] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[15:0] */ |
| 445 | val[1] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[31:16] */ |
| 446 | val[2] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[15:0] */ |
| 447 | val[3] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[31:16] */ |
| 448 | |
| 449 | mutex_unlock(&clock->extreg_lock); |
| 450 | |
| 451 | ts->tv_nsec = val[0] | (val[1] << 16); |
| 452 | ts->tv_sec = val[2] | (val[3] << 16); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static int ptp_dp83640_settime(struct ptp_clock_info *ptp, |
Richard Cochran | 41c2c18 | 2015-03-29 23:12:10 +0200 | [diff] [blame] | 458 | const struct timespec64 *ts) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 459 | { |
| 460 | struct dp83640_clock *clock = |
| 461 | container_of(ptp, struct dp83640_clock, caps); |
| 462 | struct phy_device *phydev = clock->chosen->phydev; |
| 463 | int err; |
| 464 | |
| 465 | mutex_lock(&clock->extreg_lock); |
| 466 | |
| 467 | err = tdr_write(1, phydev, ts, PTP_LOAD_CLK); |
| 468 | |
| 469 | mutex_unlock(&clock->extreg_lock); |
| 470 | |
| 471 | return err; |
| 472 | } |
| 473 | |
| 474 | static int ptp_dp83640_enable(struct ptp_clock_info *ptp, |
| 475 | struct ptp_clock_request *rq, int on) |
| 476 | { |
| 477 | struct dp83640_clock *clock = |
| 478 | container_of(ptp, struct dp83640_clock, caps); |
| 479 | struct phy_device *phydev = clock->chosen->phydev; |
Richard Cochran | fbf4b93 | 2014-03-20 22:21:56 +0100 | [diff] [blame] | 480 | unsigned int index; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 481 | u16 evnt, event_num, gpio_num; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 482 | |
| 483 | switch (rq->type) { |
| 484 | case PTP_CLK_REQ_EXTTS: |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 485 | index = rq->extts.index; |
Richard Cochran | fbf4b93 | 2014-03-20 22:21:56 +0100 | [diff] [blame] | 486 | if (index >= N_EXT_TS) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 487 | return -EINVAL; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 488 | event_num = EXT_EVENT + index; |
| 489 | evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 490 | if (on) { |
Richard Cochran | faa8971 | 2014-03-20 22:21:59 +0100 | [diff] [blame] | 491 | gpio_num = 1 + ptp_find_pin(clock->ptp_clock, |
| 492 | PTP_PF_EXTTS, index); |
| 493 | if (gpio_num < 1) |
| 494 | return -EINVAL; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 495 | evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT; |
Stefan Sørensen | 80671bd | 2014-02-03 15:36:50 +0100 | [diff] [blame] | 496 | if (rq->extts.flags & PTP_FALLING_EDGE) |
| 497 | evnt |= EVNT_FALL; |
| 498 | else |
| 499 | evnt |= EVNT_RISE; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 500 | } |
Richard Cochran | a935865 | 2015-05-25 11:55:44 +0200 | [diff] [blame] | 501 | mutex_lock(&clock->extreg_lock); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 502 | ext_write(0, phydev, PAGE5, PTP_EVNT, evnt); |
Richard Cochran | a935865 | 2015-05-25 11:55:44 +0200 | [diff] [blame] | 503 | mutex_unlock(&clock->extreg_lock); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 504 | return 0; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 505 | |
| 506 | case PTP_CLK_REQ_PEROUT: |
Stefan Sørensen | ad01577 | 2014-06-27 12:05:30 +0200 | [diff] [blame] | 507 | if (rq->perout.index >= N_PER_OUT) |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 508 | return -EINVAL; |
Stefan Sørensen | ad01577 | 2014-06-27 12:05:30 +0200 | [diff] [blame] | 509 | return periodic_output(clock, rq, on, rq->perout.index); |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 510 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 511 | default: |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | return -EOPNOTSUPP; |
| 516 | } |
| 517 | |
Richard Cochran | 86dd361 | 2014-03-20 22:21:58 +0100 | [diff] [blame] | 518 | static int ptp_dp83640_verify(struct ptp_clock_info *ptp, unsigned int pin, |
| 519 | enum ptp_pin_function func, unsigned int chan) |
| 520 | { |
Stefan Sørensen | 6f39eb8 | 2014-06-27 12:05:31 +0200 | [diff] [blame] | 521 | struct dp83640_clock *clock = |
| 522 | container_of(ptp, struct dp83640_clock, caps); |
| 523 | |
| 524 | if (clock->caps.pin_config[pin].func == PTP_PF_PHYSYNC && |
| 525 | !list_empty(&clock->phylist)) |
| 526 | return 1; |
| 527 | |
| 528 | if (func == PTP_PF_PHYSYNC) |
| 529 | return 1; |
| 530 | |
Richard Cochran | 86dd361 | 2014-03-20 22:21:58 +0100 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 534 | static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 }; |
| 535 | static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F }; |
| 536 | |
| 537 | static void enable_status_frames(struct phy_device *phydev, bool on) |
| 538 | { |
Richard Cochran | a935865 | 2015-05-25 11:55:44 +0200 | [diff] [blame] | 539 | struct dp83640_private *dp83640 = phydev->priv; |
| 540 | struct dp83640_clock *clock = dp83640->clock; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 541 | u16 cfg0 = 0, ver; |
| 542 | |
| 543 | if (on) |
| 544 | cfg0 = PSF_EVNT_EN | PSF_RXTS_EN | PSF_TXTS_EN | ENDIAN_FLAG; |
| 545 | |
| 546 | ver = (PSF_PTPVER & VERSIONPTP_MASK) << VERSIONPTP_SHIFT; |
| 547 | |
Richard Cochran | a935865 | 2015-05-25 11:55:44 +0200 | [diff] [blame] | 548 | mutex_lock(&clock->extreg_lock); |
| 549 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 550 | ext_write(0, phydev, PAGE5, PSF_CFG0, cfg0); |
| 551 | ext_write(0, phydev, PAGE6, PSF_CFG1, ver); |
| 552 | |
Richard Cochran | a935865 | 2015-05-25 11:55:44 +0200 | [diff] [blame] | 553 | mutex_unlock(&clock->extreg_lock); |
| 554 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 555 | if (!phydev->attached_dev) { |
Andrew Lunn | ab2a605 | 2018-09-29 23:04:10 +0200 | [diff] [blame^] | 556 | phydev_warn(phydev, |
| 557 | "expected to find an attached netdevice\n"); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 558 | return; |
| 559 | } |
| 560 | |
| 561 | if (on) { |
| 562 | if (dev_mc_add(phydev->attached_dev, status_frame_dst)) |
Andrew Lunn | ab2a605 | 2018-09-29 23:04:10 +0200 | [diff] [blame^] | 563 | phydev_warn(phydev, "failed to add mc address\n"); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 564 | } else { |
| 565 | if (dev_mc_del(phydev->attached_dev, status_frame_dst)) |
Andrew Lunn | ab2a605 | 2018-09-29 23:04:10 +0200 | [diff] [blame^] | 566 | phydev_warn(phydev, "failed to delete mc address\n"); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
| 570 | static bool is_status_frame(struct sk_buff *skb, int type) |
| 571 | { |
| 572 | struct ethhdr *h = eth_hdr(skb); |
| 573 | |
| 574 | if (PTP_CLASS_V2_L2 == type && |
| 575 | !memcmp(h->h_source, status_frame_src, sizeof(status_frame_src))) |
| 576 | return true; |
| 577 | else |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | static int expired(struct rxts *rxts) |
| 582 | { |
| 583 | return time_after(jiffies, rxts->tmo); |
| 584 | } |
| 585 | |
| 586 | /* Caller must hold rx_lock. */ |
| 587 | static void prune_rx_ts(struct dp83640_private *dp83640) |
| 588 | { |
| 589 | struct list_head *this, *next; |
| 590 | struct rxts *rxts; |
| 591 | |
| 592 | list_for_each_safe(this, next, &dp83640->rxts) { |
| 593 | rxts = list_entry(this, struct rxts, list); |
| 594 | if (expired(rxts)) { |
| 595 | list_del_init(&rxts->list); |
| 596 | list_add(&rxts->list, &dp83640->rxpool); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* synchronize the phyters so they act as one clock */ |
| 602 | |
| 603 | static void enable_broadcast(struct phy_device *phydev, int init_page, int on) |
| 604 | { |
| 605 | int val; |
| 606 | phy_write(phydev, PAGESEL, 0); |
| 607 | val = phy_read(phydev, PHYCR2); |
| 608 | if (on) |
| 609 | val |= BC_WRITE; |
| 610 | else |
| 611 | val &= ~BC_WRITE; |
| 612 | phy_write(phydev, PHYCR2, val); |
| 613 | phy_write(phydev, PAGESEL, init_page); |
| 614 | } |
| 615 | |
| 616 | static void recalibrate(struct dp83640_clock *clock) |
| 617 | { |
| 618 | s64 now, diff; |
| 619 | struct phy_txts event_ts; |
Richard Cochran | 41c2c18 | 2015-03-29 23:12:10 +0200 | [diff] [blame] | 620 | struct timespec64 ts; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 621 | struct list_head *this; |
| 622 | struct dp83640_private *tmp; |
| 623 | struct phy_device *master = clock->chosen->phydev; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 624 | u16 cal_gpio, cfg0, evnt, ptp_trig, trigger, val; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 625 | |
| 626 | trigger = CAL_TRIGGER; |
Stefan Sørensen | e015595 | 2014-06-27 12:05:32 +0200 | [diff] [blame] | 627 | cal_gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PHYSYNC, 0); |
| 628 | if (cal_gpio < 1) { |
Masanari Iida | f42cf8d | 2015-02-24 23:11:26 +0900 | [diff] [blame] | 629 | pr_err("PHY calibration pin not available - PHY is not calibrated."); |
Stefan Sørensen | e015595 | 2014-06-27 12:05:32 +0200 | [diff] [blame] | 630 | return; |
| 631 | } |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 632 | |
| 633 | mutex_lock(&clock->extreg_lock); |
| 634 | |
| 635 | /* |
| 636 | * enable broadcast, disable status frames, enable ptp clock |
| 637 | */ |
| 638 | list_for_each(this, &clock->phylist) { |
| 639 | tmp = list_entry(this, struct dp83640_private, list); |
| 640 | enable_broadcast(tmp->phydev, clock->page, 1); |
| 641 | tmp->cfg0 = ext_read(tmp->phydev, PAGE5, PSF_CFG0); |
| 642 | ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, 0); |
| 643 | ext_write(0, tmp->phydev, PAGE4, PTP_CTL, PTP_ENABLE); |
| 644 | } |
| 645 | enable_broadcast(master, clock->page, 1); |
| 646 | cfg0 = ext_read(master, PAGE5, PSF_CFG0); |
| 647 | ext_write(0, master, PAGE5, PSF_CFG0, 0); |
| 648 | ext_write(0, master, PAGE4, PTP_CTL, PTP_ENABLE); |
| 649 | |
| 650 | /* |
| 651 | * enable an event timestamp |
| 652 | */ |
| 653 | evnt = EVNT_WR | EVNT_RISE | EVNT_SINGLE; |
| 654 | evnt |= (CAL_EVENT & EVNT_SEL_MASK) << EVNT_SEL_SHIFT; |
| 655 | evnt |= (cal_gpio & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT; |
| 656 | |
| 657 | list_for_each(this, &clock->phylist) { |
| 658 | tmp = list_entry(this, struct dp83640_private, list); |
| 659 | ext_write(0, tmp->phydev, PAGE5, PTP_EVNT, evnt); |
| 660 | } |
| 661 | ext_write(0, master, PAGE5, PTP_EVNT, evnt); |
| 662 | |
| 663 | /* |
| 664 | * configure a trigger |
| 665 | */ |
| 666 | ptp_trig = TRIG_WR | TRIG_IF_LATE | TRIG_PULSE; |
| 667 | ptp_trig |= (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT; |
| 668 | ptp_trig |= (cal_gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT; |
| 669 | ext_write(0, master, PAGE5, PTP_TRIG, ptp_trig); |
| 670 | |
| 671 | /* load trigger */ |
| 672 | val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT; |
| 673 | val |= TRIG_LOAD; |
| 674 | ext_write(0, master, PAGE4, PTP_CTL, val); |
| 675 | |
| 676 | /* enable trigger */ |
| 677 | val &= ~TRIG_LOAD; |
| 678 | val |= TRIG_EN; |
| 679 | ext_write(0, master, PAGE4, PTP_CTL, val); |
| 680 | |
| 681 | /* disable trigger */ |
| 682 | val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT; |
| 683 | val |= TRIG_DIS; |
| 684 | ext_write(0, master, PAGE4, PTP_CTL, val); |
| 685 | |
| 686 | /* |
| 687 | * read out and correct offsets |
| 688 | */ |
| 689 | val = ext_read(master, PAGE4, PTP_STS); |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 690 | pr_info("master PTP_STS 0x%04hx\n", val); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 691 | val = ext_read(master, PAGE4, PTP_ESTS); |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 692 | pr_info("master PTP_ESTS 0x%04hx\n", val); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 693 | event_ts.ns_lo = ext_read(master, PAGE4, PTP_EDATA); |
| 694 | event_ts.ns_hi = ext_read(master, PAGE4, PTP_EDATA); |
| 695 | event_ts.sec_lo = ext_read(master, PAGE4, PTP_EDATA); |
| 696 | event_ts.sec_hi = ext_read(master, PAGE4, PTP_EDATA); |
| 697 | now = phy2txts(&event_ts); |
| 698 | |
| 699 | list_for_each(this, &clock->phylist) { |
| 700 | tmp = list_entry(this, struct dp83640_private, list); |
| 701 | val = ext_read(tmp->phydev, PAGE4, PTP_STS); |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 702 | pr_info("slave PTP_STS 0x%04hx\n", val); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 703 | val = ext_read(tmp->phydev, PAGE4, PTP_ESTS); |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 704 | pr_info("slave PTP_ESTS 0x%04hx\n", val); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 705 | event_ts.ns_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA); |
| 706 | event_ts.ns_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA); |
| 707 | event_ts.sec_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA); |
| 708 | event_ts.sec_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA); |
| 709 | diff = now - (s64) phy2txts(&event_ts); |
| 710 | pr_info("slave offset %lld nanoseconds\n", diff); |
| 711 | diff += ADJTIME_FIX; |
Richard Cochran | 41c2c18 | 2015-03-29 23:12:10 +0200 | [diff] [blame] | 712 | ts = ns_to_timespec64(diff); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 713 | tdr_write(0, tmp->phydev, &ts, PTP_STEP_CLK); |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * restore status frames |
| 718 | */ |
| 719 | list_for_each(this, &clock->phylist) { |
| 720 | tmp = list_entry(this, struct dp83640_private, list); |
| 721 | ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, tmp->cfg0); |
| 722 | } |
| 723 | ext_write(0, master, PAGE5, PSF_CFG0, cfg0); |
| 724 | |
| 725 | mutex_unlock(&clock->extreg_lock); |
| 726 | } |
| 727 | |
| 728 | /* time stamping methods */ |
| 729 | |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 730 | static inline u16 exts_chan_to_edata(int ch) |
| 731 | { |
| 732 | return 1 << ((ch + EXT_EVENT) * 2); |
| 733 | } |
| 734 | |
Richard Cochran | 2331038 | 2011-06-14 23:55:19 +0000 | [diff] [blame] | 735 | static int decode_evnt(struct dp83640_private *dp83640, |
Christian Riesch | 13322f2 | 2014-08-21 15:17:04 +0200 | [diff] [blame] | 736 | void *data, int len, u16 ests) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 737 | { |
Richard Cochran | 2331038 | 2011-06-14 23:55:19 +0000 | [diff] [blame] | 738 | struct phy_txts *phy_txts; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 739 | struct ptp_clock_event event; |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 740 | int i, parsed; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 741 | int words = (ests >> EVNT_TS_LEN_SHIFT) & EVNT_TS_LEN_MASK; |
Richard Cochran | 2331038 | 2011-06-14 23:55:19 +0000 | [diff] [blame] | 742 | u16 ext_status = 0; |
| 743 | |
Christian Riesch | 13322f2 | 2014-08-21 15:17:04 +0200 | [diff] [blame] | 744 | /* calculate length of the event timestamp status message */ |
| 745 | if (ests & MULT_EVNT) |
| 746 | parsed = (words + 2) * sizeof(u16); |
| 747 | else |
| 748 | parsed = (words + 1) * sizeof(u16); |
| 749 | |
| 750 | /* check if enough data is available */ |
| 751 | if (len < parsed) |
| 752 | return len; |
| 753 | |
Richard Cochran | 2331038 | 2011-06-14 23:55:19 +0000 | [diff] [blame] | 754 | if (ests & MULT_EVNT) { |
| 755 | ext_status = *(u16 *) data; |
| 756 | data += sizeof(ext_status); |
| 757 | } |
| 758 | |
| 759 | phy_txts = data; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 760 | |
Gustavo A. R. Silva | d331e75 | 2018-08-09 10:08:24 -0500 | [diff] [blame] | 761 | switch (words) { |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 762 | case 3: |
| 763 | dp83640->edata.sec_hi = phy_txts->sec_hi; |
Gustavo A. R. Silva | d331e75 | 2018-08-09 10:08:24 -0500 | [diff] [blame] | 764 | /* fall through */ |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 765 | case 2: |
| 766 | dp83640->edata.sec_lo = phy_txts->sec_lo; |
Gustavo A. R. Silva | d331e75 | 2018-08-09 10:08:24 -0500 | [diff] [blame] | 767 | /* fall through */ |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 768 | case 1: |
| 769 | dp83640->edata.ns_hi = phy_txts->ns_hi; |
Gustavo A. R. Silva | d331e75 | 2018-08-09 10:08:24 -0500 | [diff] [blame] | 770 | /* fall through */ |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 771 | case 0: |
| 772 | dp83640->edata.ns_lo = phy_txts->ns_lo; |
| 773 | } |
| 774 | |
Christian Riesch | 13322f2 | 2014-08-21 15:17:04 +0200 | [diff] [blame] | 775 | if (!ext_status) { |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 776 | i = ((ests >> EVNT_NUM_SHIFT) & EVNT_NUM_MASK) - EXT_EVENT; |
| 777 | ext_status = exts_chan_to_edata(i); |
| 778 | } |
| 779 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 780 | event.type = PTP_CLOCK_EXTTS; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 781 | event.timestamp = phy2txts(&dp83640->edata); |
| 782 | |
Stefan Sørensen | a0077a9 | 2014-07-11 08:18:26 +0200 | [diff] [blame] | 783 | /* Compensate for input path and synchronization delays */ |
| 784 | event.timestamp -= 35; |
| 785 | |
Richard Cochran | 49b3fd4 | 2011-09-20 01:43:14 +0000 | [diff] [blame] | 786 | for (i = 0; i < N_EXT_TS; i++) { |
| 787 | if (ext_status & exts_chan_to_edata(i)) { |
| 788 | event.index = i; |
| 789 | ptp_clock_event(dp83640->clock->ptp_clock, &event); |
| 790 | } |
| 791 | } |
Richard Cochran | 2331038 | 2011-06-14 23:55:19 +0000 | [diff] [blame] | 792 | |
Christian Riesch | 13322f2 | 2014-08-21 15:17:04 +0200 | [diff] [blame] | 793 | return parsed; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 794 | } |
| 795 | |
Stefan Sørensen | 539e44d | 2015-11-03 09:34:04 +0100 | [diff] [blame] | 796 | #define DP83640_PACKET_HASH_OFFSET 20 |
| 797 | #define DP83640_PACKET_HASH_LEN 10 |
| 798 | |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 799 | static int match(struct sk_buff *skb, unsigned int type, struct rxts *rxts) |
| 800 | { |
Stefan Sørensen | 539e44d | 2015-11-03 09:34:04 +0100 | [diff] [blame] | 801 | u16 *seqid, hash; |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 802 | unsigned int offset = 0; |
| 803 | u8 *msgtype, *data = skb_mac_header(skb); |
| 804 | |
| 805 | /* check sequenceID, messageType, 12 bit hash of offset 20-29 */ |
| 806 | |
| 807 | if (type & PTP_CLASS_VLAN) |
| 808 | offset += VLAN_HLEN; |
| 809 | |
| 810 | switch (type & PTP_CLASS_PMASK) { |
| 811 | case PTP_CLASS_IPV4: |
Richard Cochran | cca04b2 | 2014-11-12 11:33:52 +0100 | [diff] [blame] | 812 | offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN; |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 813 | break; |
| 814 | case PTP_CLASS_IPV6: |
| 815 | offset += ETH_HLEN + IP6_HLEN + UDP_HLEN; |
| 816 | break; |
| 817 | case PTP_CLASS_L2: |
| 818 | offset += ETH_HLEN; |
| 819 | break; |
| 820 | default: |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid)) |
| 825 | return 0; |
| 826 | |
| 827 | if (unlikely(type & PTP_CLASS_V1)) |
| 828 | msgtype = data + offset + OFF_PTP_CONTROL; |
| 829 | else |
| 830 | msgtype = data + offset; |
Stefan Sørensen | 539e44d | 2015-11-03 09:34:04 +0100 | [diff] [blame] | 831 | if (rxts->msgtype != (*msgtype & 0xf)) |
| 832 | return 0; |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 833 | |
| 834 | seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID); |
Stefan Sørensen | 539e44d | 2015-11-03 09:34:04 +0100 | [diff] [blame] | 835 | if (rxts->seqid != ntohs(*seqid)) |
| 836 | return 0; |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 837 | |
Stefan Sørensen | 539e44d | 2015-11-03 09:34:04 +0100 | [diff] [blame] | 838 | hash = ether_crc(DP83640_PACKET_HASH_LEN, |
| 839 | data + offset + DP83640_PACKET_HASH_OFFSET) >> 20; |
| 840 | if (rxts->hash != hash) |
| 841 | return 0; |
| 842 | |
| 843 | return 1; |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 846 | static void decode_rxts(struct dp83640_private *dp83640, |
| 847 | struct phy_rxts *phy_rxts) |
| 848 | { |
| 849 | struct rxts *rxts; |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 850 | struct skb_shared_hwtstamps *shhwtstamps = NULL; |
| 851 | struct sk_buff *skb; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 852 | unsigned long flags; |
Manfred Rudigier | 81e8f2e | 2016-01-20 11:22:28 +0100 | [diff] [blame] | 853 | u8 overflow; |
| 854 | |
| 855 | overflow = (phy_rxts->ns_hi >> 14) & 0x3; |
| 856 | if (overflow) |
| 857 | pr_debug("rx timestamp queue overflow, count %d\n", overflow); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 858 | |
| 859 | spin_lock_irqsave(&dp83640->rx_lock, flags); |
| 860 | |
| 861 | prune_rx_ts(dp83640); |
| 862 | |
| 863 | if (list_empty(&dp83640->rxpool)) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 864 | pr_debug("rx timestamp pool is empty\n"); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 865 | goto out; |
| 866 | } |
| 867 | rxts = list_first_entry(&dp83640->rxpool, struct rxts, list); |
| 868 | list_del_init(&rxts->list); |
| 869 | phy2rxts(phy_rxts, rxts); |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 870 | |
Richard Cochran | adbe088 | 2015-05-25 11:55:45 +0200 | [diff] [blame] | 871 | spin_lock(&dp83640->rx_queue.lock); |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 872 | skb_queue_walk(&dp83640->rx_queue, skb) { |
| 873 | struct dp83640_skb_info *skb_info; |
| 874 | |
| 875 | skb_info = (struct dp83640_skb_info *)skb->cb; |
| 876 | if (match(skb, skb_info->ptp_type, rxts)) { |
| 877 | __skb_unlink(skb, &dp83640->rx_queue); |
| 878 | shhwtstamps = skb_hwtstamps(skb); |
| 879 | memset(shhwtstamps, 0, sizeof(*shhwtstamps)); |
| 880 | shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns); |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 881 | list_add(&rxts->list, &dp83640->rxpool); |
| 882 | break; |
| 883 | } |
| 884 | } |
Richard Cochran | adbe088 | 2015-05-25 11:55:45 +0200 | [diff] [blame] | 885 | spin_unlock(&dp83640->rx_queue.lock); |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 886 | |
| 887 | if (!shhwtstamps) |
| 888 | list_add_tail(&rxts->list, &dp83640->rxts); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 889 | out: |
| 890 | spin_unlock_irqrestore(&dp83640->rx_lock, flags); |
Stefan Sørensen | d36b82b | 2017-08-30 08:58:47 +0200 | [diff] [blame] | 891 | |
| 892 | if (shhwtstamps) |
| 893 | netif_rx_ni(skb); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | static void decode_txts(struct dp83640_private *dp83640, |
| 897 | struct phy_txts *phy_txts) |
| 898 | { |
| 899 | struct skb_shared_hwtstamps shhwtstamps; |
| 900 | struct sk_buff *skb; |
| 901 | u64 ns; |
Manfred Rudigier | 81e8f2e | 2016-01-20 11:22:28 +0100 | [diff] [blame] | 902 | u8 overflow; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 903 | |
| 904 | /* We must already have the skb that triggered this. */ |
| 905 | |
| 906 | skb = skb_dequeue(&dp83640->tx_queue); |
| 907 | |
| 908 | if (!skb) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 909 | pr_debug("have timestamp but tx_queue empty\n"); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 910 | return; |
| 911 | } |
Manfred Rudigier | 81e8f2e | 2016-01-20 11:22:28 +0100 | [diff] [blame] | 912 | |
| 913 | overflow = (phy_txts->ns_hi >> 14) & 0x3; |
| 914 | if (overflow) { |
| 915 | pr_debug("tx timestamp queue overflow, count %d\n", overflow); |
| 916 | while (skb) { |
Richard Cochran | db9d8b2 | 2017-06-23 17:51:31 +0200 | [diff] [blame] | 917 | kfree_skb(skb); |
Manfred Rudigier | 81e8f2e | 2016-01-20 11:22:28 +0100 | [diff] [blame] | 918 | skb = skb_dequeue(&dp83640->tx_queue); |
| 919 | } |
| 920 | return; |
| 921 | } |
| 922 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 923 | ns = phy2txts(phy_txts); |
| 924 | memset(&shhwtstamps, 0, sizeof(shhwtstamps)); |
| 925 | shhwtstamps.hwtstamp = ns_to_ktime(ns); |
| 926 | skb_complete_tx_timestamp(skb, &shhwtstamps); |
| 927 | } |
| 928 | |
| 929 | static void decode_status_frame(struct dp83640_private *dp83640, |
| 930 | struct sk_buff *skb) |
| 931 | { |
| 932 | struct phy_rxts *phy_rxts; |
| 933 | struct phy_txts *phy_txts; |
| 934 | u8 *ptr; |
| 935 | int len, size; |
| 936 | u16 ests, type; |
| 937 | |
| 938 | ptr = skb->data + 2; |
| 939 | |
| 940 | for (len = skb_headlen(skb) - 2; len > sizeof(type); len -= size) { |
| 941 | |
| 942 | type = *(u16 *)ptr; |
| 943 | ests = type & 0x0fff; |
| 944 | type = type & 0xf000; |
| 945 | len -= sizeof(type); |
| 946 | ptr += sizeof(type); |
| 947 | |
| 948 | if (PSF_RX == type && len >= sizeof(*phy_rxts)) { |
| 949 | |
| 950 | phy_rxts = (struct phy_rxts *) ptr; |
| 951 | decode_rxts(dp83640, phy_rxts); |
| 952 | size = sizeof(*phy_rxts); |
| 953 | |
| 954 | } else if (PSF_TX == type && len >= sizeof(*phy_txts)) { |
| 955 | |
| 956 | phy_txts = (struct phy_txts *) ptr; |
| 957 | decode_txts(dp83640, phy_txts); |
| 958 | size = sizeof(*phy_txts); |
| 959 | |
Christian Riesch | 13322f2 | 2014-08-21 15:17:04 +0200 | [diff] [blame] | 960 | } else if (PSF_EVNT == type) { |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 961 | |
Christian Riesch | 13322f2 | 2014-08-21 15:17:04 +0200 | [diff] [blame] | 962 | size = decode_evnt(dp83640, ptr, len, ests); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 963 | |
| 964 | } else { |
| 965 | size = 0; |
| 966 | break; |
| 967 | } |
| 968 | ptr += size; |
| 969 | } |
| 970 | } |
| 971 | |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 972 | static int is_sync(struct sk_buff *skb, int type) |
| 973 | { |
| 974 | u8 *data = skb->data, *msgtype; |
| 975 | unsigned int offset = 0; |
| 976 | |
Stefan Sørensen | ae5c6c6 | 2014-06-27 11:59:10 +0200 | [diff] [blame] | 977 | if (type & PTP_CLASS_VLAN) |
| 978 | offset += VLAN_HLEN; |
| 979 | |
| 980 | switch (type & PTP_CLASS_PMASK) { |
| 981 | case PTP_CLASS_IPV4: |
Richard Cochran | cca04b2 | 2014-11-12 11:33:52 +0100 | [diff] [blame] | 982 | offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN; |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 983 | break; |
Stefan Sørensen | ae5c6c6 | 2014-06-27 11:59:10 +0200 | [diff] [blame] | 984 | case PTP_CLASS_IPV6: |
| 985 | offset += ETH_HLEN + IP6_HLEN + UDP_HLEN; |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 986 | break; |
Stefan Sørensen | ae5c6c6 | 2014-06-27 11:59:10 +0200 | [diff] [blame] | 987 | case PTP_CLASS_L2: |
| 988 | offset += ETH_HLEN; |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 989 | break; |
| 990 | default: |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | if (type & PTP_CLASS_V1) |
| 995 | offset += OFF_PTP_CONTROL; |
| 996 | |
| 997 | if (skb->len < offset + 1) |
| 998 | return 0; |
| 999 | |
| 1000 | msgtype = data + offset; |
| 1001 | |
| 1002 | return (*msgtype & 0xf) == 0; |
| 1003 | } |
| 1004 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1005 | static void dp83640_free_clocks(void) |
| 1006 | { |
| 1007 | struct dp83640_clock *clock; |
| 1008 | struct list_head *this, *next; |
| 1009 | |
| 1010 | mutex_lock(&phyter_clocks_lock); |
| 1011 | |
| 1012 | list_for_each_safe(this, next, &phyter_clocks) { |
| 1013 | clock = list_entry(this, struct dp83640_clock, list); |
| 1014 | if (!list_empty(&clock->phylist)) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 1015 | pr_warn("phy list non-empty while unloading\n"); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1016 | BUG(); |
| 1017 | } |
| 1018 | list_del(&clock->list); |
| 1019 | mutex_destroy(&clock->extreg_lock); |
| 1020 | mutex_destroy(&clock->clock_lock); |
| 1021 | put_device(&clock->bus->dev); |
Richard Cochran | 86dd361 | 2014-03-20 22:21:58 +0100 | [diff] [blame] | 1022 | kfree(clock->caps.pin_config); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1023 | kfree(clock); |
| 1024 | } |
| 1025 | |
| 1026 | mutex_unlock(&phyter_clocks_lock); |
| 1027 | } |
| 1028 | |
| 1029 | static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus) |
| 1030 | { |
| 1031 | INIT_LIST_HEAD(&clock->list); |
| 1032 | clock->bus = bus; |
| 1033 | mutex_init(&clock->extreg_lock); |
| 1034 | mutex_init(&clock->clock_lock); |
| 1035 | INIT_LIST_HEAD(&clock->phylist); |
| 1036 | clock->caps.owner = THIS_MODULE; |
| 1037 | sprintf(clock->caps.name, "dp83640 timer"); |
| 1038 | clock->caps.max_adj = 1953124; |
| 1039 | clock->caps.n_alarm = 0; |
| 1040 | clock->caps.n_ext_ts = N_EXT_TS; |
Stefan Sørensen | ad01577 | 2014-06-27 12:05:30 +0200 | [diff] [blame] | 1041 | clock->caps.n_per_out = N_PER_OUT; |
Richard Cochran | 86dd361 | 2014-03-20 22:21:58 +0100 | [diff] [blame] | 1042 | clock->caps.n_pins = DP83640_N_PINS; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1043 | clock->caps.pps = 0; |
Richard Cochran | e4788b8 | 2016-11-08 22:49:18 +0100 | [diff] [blame] | 1044 | clock->caps.adjfine = ptp_dp83640_adjfine; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1045 | clock->caps.adjtime = ptp_dp83640_adjtime; |
Richard Cochran | 41c2c18 | 2015-03-29 23:12:10 +0200 | [diff] [blame] | 1046 | clock->caps.gettime64 = ptp_dp83640_gettime; |
| 1047 | clock->caps.settime64 = ptp_dp83640_settime; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1048 | clock->caps.enable = ptp_dp83640_enable; |
Richard Cochran | 86dd361 | 2014-03-20 22:21:58 +0100 | [diff] [blame] | 1049 | clock->caps.verify = ptp_dp83640_verify; |
| 1050 | /* |
| 1051 | * Convert the module param defaults into a dynamic pin configuration. |
| 1052 | */ |
| 1053 | dp83640_gpio_defaults(clock->caps.pin_config); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1054 | /* |
| 1055 | * Get a reference to this bus instance. |
| 1056 | */ |
| 1057 | get_device(&bus->dev); |
| 1058 | } |
| 1059 | |
| 1060 | static int choose_this_phy(struct dp83640_clock *clock, |
| 1061 | struct phy_device *phydev) |
| 1062 | { |
| 1063 | if (chosen_phy == -1 && !clock->chosen) |
| 1064 | return 1; |
| 1065 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1066 | if (chosen_phy == phydev->mdio.addr) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1067 | return 1; |
| 1068 | |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | static struct dp83640_clock *dp83640_clock_get(struct dp83640_clock *clock) |
| 1073 | { |
| 1074 | if (clock) |
| 1075 | mutex_lock(&clock->clock_lock); |
| 1076 | return clock; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * Look up and lock a clock by bus instance. |
| 1081 | * If there is no clock for this bus, then create it first. |
| 1082 | */ |
| 1083 | static struct dp83640_clock *dp83640_clock_get_bus(struct mii_bus *bus) |
| 1084 | { |
| 1085 | struct dp83640_clock *clock = NULL, *tmp; |
| 1086 | struct list_head *this; |
| 1087 | |
| 1088 | mutex_lock(&phyter_clocks_lock); |
| 1089 | |
| 1090 | list_for_each(this, &phyter_clocks) { |
| 1091 | tmp = list_entry(this, struct dp83640_clock, list); |
| 1092 | if (tmp->bus == bus) { |
| 1093 | clock = tmp; |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | if (clock) |
| 1098 | goto out; |
| 1099 | |
| 1100 | clock = kzalloc(sizeof(struct dp83640_clock), GFP_KERNEL); |
| 1101 | if (!clock) |
| 1102 | goto out; |
| 1103 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1104 | clock->caps.pin_config = kcalloc(DP83640_N_PINS, |
| 1105 | sizeof(struct ptp_pin_desc), |
| 1106 | GFP_KERNEL); |
Richard Cochran | 86dd361 | 2014-03-20 22:21:58 +0100 | [diff] [blame] | 1107 | if (!clock->caps.pin_config) { |
| 1108 | kfree(clock); |
| 1109 | clock = NULL; |
| 1110 | goto out; |
| 1111 | } |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1112 | dp83640_clock_init(clock, bus); |
| 1113 | list_add_tail(&phyter_clocks, &clock->list); |
| 1114 | out: |
| 1115 | mutex_unlock(&phyter_clocks_lock); |
| 1116 | |
| 1117 | return dp83640_clock_get(clock); |
| 1118 | } |
| 1119 | |
| 1120 | static void dp83640_clock_put(struct dp83640_clock *clock) |
| 1121 | { |
| 1122 | mutex_unlock(&clock->clock_lock); |
| 1123 | } |
| 1124 | |
| 1125 | static int dp83640_probe(struct phy_device *phydev) |
| 1126 | { |
| 1127 | struct dp83640_clock *clock; |
| 1128 | struct dp83640_private *dp83640; |
| 1129 | int err = -ENOMEM, i; |
| 1130 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1131 | if (phydev->mdio.addr == BROADCAST_ADDR) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1132 | return 0; |
| 1133 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1134 | clock = dp83640_clock_get_bus(phydev->mdio.bus); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1135 | if (!clock) |
| 1136 | goto no_clock; |
| 1137 | |
| 1138 | dp83640 = kzalloc(sizeof(struct dp83640_private), GFP_KERNEL); |
| 1139 | if (!dp83640) |
| 1140 | goto no_memory; |
| 1141 | |
| 1142 | dp83640->phydev = phydev; |
Stefan Sørensen | 4b06325 | 2015-11-03 09:34:05 +0100 | [diff] [blame] | 1143 | INIT_DELAYED_WORK(&dp83640->ts_work, rx_timestamp_work); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1144 | |
| 1145 | INIT_LIST_HEAD(&dp83640->rxts); |
| 1146 | INIT_LIST_HEAD(&dp83640->rxpool); |
| 1147 | for (i = 0; i < MAX_RXTS; i++) |
| 1148 | list_add(&dp83640->rx_pool_data[i].list, &dp83640->rxpool); |
| 1149 | |
| 1150 | phydev->priv = dp83640; |
| 1151 | |
| 1152 | spin_lock_init(&dp83640->rx_lock); |
| 1153 | skb_queue_head_init(&dp83640->rx_queue); |
| 1154 | skb_queue_head_init(&dp83640->tx_queue); |
| 1155 | |
| 1156 | dp83640->clock = clock; |
| 1157 | |
| 1158 | if (choose_this_phy(clock, phydev)) { |
| 1159 | clock->chosen = dp83640; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1160 | clock->ptp_clock = ptp_clock_register(&clock->caps, |
| 1161 | &phydev->mdio.dev); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1162 | if (IS_ERR(clock->ptp_clock)) { |
| 1163 | err = PTR_ERR(clock->ptp_clock); |
| 1164 | goto no_register; |
| 1165 | } |
| 1166 | } else |
| 1167 | list_add_tail(&dp83640->list, &clock->phylist); |
| 1168 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1169 | dp83640_clock_put(clock); |
| 1170 | return 0; |
| 1171 | |
| 1172 | no_register: |
| 1173 | clock->chosen = NULL; |
| 1174 | kfree(dp83640); |
| 1175 | no_memory: |
| 1176 | dp83640_clock_put(clock); |
| 1177 | no_clock: |
| 1178 | return err; |
| 1179 | } |
| 1180 | |
| 1181 | static void dp83640_remove(struct phy_device *phydev) |
| 1182 | { |
| 1183 | struct dp83640_clock *clock; |
| 1184 | struct list_head *this, *next; |
| 1185 | struct dp83640_private *tmp, *dp83640 = phydev->priv; |
| 1186 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1187 | if (phydev->mdio.addr == BROADCAST_ADDR) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1188 | return; |
| 1189 | |
| 1190 | enable_status_frames(phydev, false); |
Stefan Sørensen | 4b06325 | 2015-11-03 09:34:05 +0100 | [diff] [blame] | 1191 | cancel_delayed_work_sync(&dp83640->ts_work); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1192 | |
Alexander Duyck | db91b72 | 2014-09-08 11:25:34 -0400 | [diff] [blame] | 1193 | skb_queue_purge(&dp83640->rx_queue); |
| 1194 | skb_queue_purge(&dp83640->tx_queue); |
Richard Cochran | 8b3408f | 2011-10-21 00:49:17 +0000 | [diff] [blame] | 1195 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1196 | clock = dp83640_clock_get(dp83640->clock); |
| 1197 | |
| 1198 | if (dp83640 == clock->chosen) { |
| 1199 | ptp_clock_unregister(clock->ptp_clock); |
| 1200 | clock->chosen = NULL; |
| 1201 | } else { |
| 1202 | list_for_each_safe(this, next, &clock->phylist) { |
| 1203 | tmp = list_entry(this, struct dp83640_private, list); |
| 1204 | if (tmp == dp83640) { |
| 1205 | list_del_init(&tmp->list); |
| 1206 | break; |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | dp83640_clock_put(clock); |
| 1212 | kfree(dp83640); |
| 1213 | } |
| 1214 | |
Esben Haabendal | 76327a3 | 2018-04-08 22:17:01 +0200 | [diff] [blame] | 1215 | static int dp83640_soft_reset(struct phy_device *phydev) |
| 1216 | { |
| 1217 | int ret; |
| 1218 | |
| 1219 | ret = genphy_soft_reset(phydev); |
| 1220 | if (ret < 0) |
| 1221 | return ret; |
| 1222 | |
| 1223 | /* From DP83640 datasheet: "Software driver code must wait 3 us |
| 1224 | * following a software reset before allowing further serial MII |
| 1225 | * operations with the DP83640." |
| 1226 | */ |
| 1227 | udelay(10); /* Taking udelay inaccuracy into account */ |
| 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
Stefan Sørensen | 62ad968 | 2014-02-03 15:36:58 +0100 | [diff] [blame] | 1232 | static int dp83640_config_init(struct phy_device *phydev) |
| 1233 | { |
Stefan Sørensen | 602b109 | 2014-02-13 15:26:57 +0100 | [diff] [blame] | 1234 | struct dp83640_private *dp83640 = phydev->priv; |
| 1235 | struct dp83640_clock *clock = dp83640->clock; |
| 1236 | |
| 1237 | if (clock->chosen && !list_empty(&clock->phylist)) |
| 1238 | recalibrate(clock); |
Richard Cochran | a935865 | 2015-05-25 11:55:44 +0200 | [diff] [blame] | 1239 | else { |
| 1240 | mutex_lock(&clock->extreg_lock); |
Stefan Sørensen | 602b109 | 2014-02-13 15:26:57 +0100 | [diff] [blame] | 1241 | enable_broadcast(phydev, clock->page, 1); |
Richard Cochran | a935865 | 2015-05-25 11:55:44 +0200 | [diff] [blame] | 1242 | mutex_unlock(&clock->extreg_lock); |
| 1243 | } |
Stefan Sørensen | 602b109 | 2014-02-13 15:26:57 +0100 | [diff] [blame] | 1244 | |
Stefan Sørensen | 62ad968 | 2014-02-03 15:36:58 +0100 | [diff] [blame] | 1245 | enable_status_frames(phydev, true); |
Richard Cochran | a935865 | 2015-05-25 11:55:44 +0200 | [diff] [blame] | 1246 | |
| 1247 | mutex_lock(&clock->extreg_lock); |
Stefan Sørensen | 62ad968 | 2014-02-03 15:36:58 +0100 | [diff] [blame] | 1248 | ext_write(0, phydev, PAGE4, PTP_CTL, PTP_ENABLE); |
Richard Cochran | a935865 | 2015-05-25 11:55:44 +0200 | [diff] [blame] | 1249 | mutex_unlock(&clock->extreg_lock); |
| 1250 | |
Stefan Sørensen | 62ad968 | 2014-02-03 15:36:58 +0100 | [diff] [blame] | 1251 | return 0; |
| 1252 | } |
| 1253 | |
Stephan Gatzka | 1642182 | 2012-12-04 10:21:38 +0000 | [diff] [blame] | 1254 | static int dp83640_ack_interrupt(struct phy_device *phydev) |
| 1255 | { |
| 1256 | int err = phy_read(phydev, MII_DP83640_MISR); |
| 1257 | |
| 1258 | if (err < 0) |
| 1259 | return err; |
| 1260 | |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | static int dp83640_config_intr(struct phy_device *phydev) |
| 1265 | { |
| 1266 | int micr; |
| 1267 | int misr; |
| 1268 | int err; |
| 1269 | |
| 1270 | if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { |
| 1271 | misr = phy_read(phydev, MII_DP83640_MISR); |
| 1272 | if (misr < 0) |
| 1273 | return misr; |
| 1274 | misr |= |
| 1275 | (MII_DP83640_MISR_ANC_INT_EN | |
| 1276 | MII_DP83640_MISR_DUP_INT_EN | |
| 1277 | MII_DP83640_MISR_SPD_INT_EN | |
| 1278 | MII_DP83640_MISR_LINK_INT_EN); |
| 1279 | err = phy_write(phydev, MII_DP83640_MISR, misr); |
| 1280 | if (err < 0) |
| 1281 | return err; |
| 1282 | |
| 1283 | micr = phy_read(phydev, MII_DP83640_MICR); |
| 1284 | if (micr < 0) |
| 1285 | return micr; |
| 1286 | micr |= |
| 1287 | (MII_DP83640_MICR_OE | |
| 1288 | MII_DP83640_MICR_IE); |
| 1289 | return phy_write(phydev, MII_DP83640_MICR, micr); |
| 1290 | } else { |
| 1291 | micr = phy_read(phydev, MII_DP83640_MICR); |
| 1292 | if (micr < 0) |
| 1293 | return micr; |
| 1294 | micr &= |
| 1295 | ~(MII_DP83640_MICR_OE | |
| 1296 | MII_DP83640_MICR_IE); |
| 1297 | err = phy_write(phydev, MII_DP83640_MICR, micr); |
| 1298 | if (err < 0) |
| 1299 | return err; |
| 1300 | |
| 1301 | misr = phy_read(phydev, MII_DP83640_MISR); |
| 1302 | if (misr < 0) |
| 1303 | return misr; |
| 1304 | misr &= |
| 1305 | ~(MII_DP83640_MISR_ANC_INT_EN | |
| 1306 | MII_DP83640_MISR_DUP_INT_EN | |
| 1307 | MII_DP83640_MISR_SPD_INT_EN | |
| 1308 | MII_DP83640_MISR_LINK_INT_EN); |
| 1309 | return phy_write(phydev, MII_DP83640_MISR, misr); |
| 1310 | } |
| 1311 | } |
| 1312 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1313 | static int dp83640_hwtstamp(struct phy_device *phydev, struct ifreq *ifr) |
| 1314 | { |
| 1315 | struct dp83640_private *dp83640 = phydev->priv; |
| 1316 | struct hwtstamp_config cfg; |
| 1317 | u16 txcfg0, rxcfg0; |
| 1318 | |
| 1319 | if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) |
| 1320 | return -EFAULT; |
| 1321 | |
| 1322 | if (cfg.flags) /* reserved for future extensions */ |
| 1323 | return -EINVAL; |
| 1324 | |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 1325 | if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC) |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1326 | return -ERANGE; |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 1327 | |
| 1328 | dp83640->hwts_tx_en = cfg.tx_type; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1329 | |
| 1330 | switch (cfg.rx_filter) { |
| 1331 | case HWTSTAMP_FILTER_NONE: |
| 1332 | dp83640->hwts_rx_en = 0; |
| 1333 | dp83640->layer = 0; |
| 1334 | dp83640->version = 0; |
| 1335 | break; |
| 1336 | case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: |
| 1337 | case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: |
| 1338 | case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: |
| 1339 | dp83640->hwts_rx_en = 1; |
Stefan Sørensen | a1f8723 | 2015-11-03 09:34:08 +0100 | [diff] [blame] | 1340 | dp83640->layer = PTP_CLASS_L4; |
| 1341 | dp83640->version = PTP_CLASS_V1; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1342 | break; |
| 1343 | case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: |
| 1344 | case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: |
| 1345 | case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: |
| 1346 | dp83640->hwts_rx_en = 1; |
Stefan Sørensen | a1f8723 | 2015-11-03 09:34:08 +0100 | [diff] [blame] | 1347 | dp83640->layer = PTP_CLASS_L4; |
| 1348 | dp83640->version = PTP_CLASS_V2; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1349 | break; |
| 1350 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: |
| 1351 | case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: |
| 1352 | case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: |
| 1353 | dp83640->hwts_rx_en = 1; |
Stefan Sørensen | a1f8723 | 2015-11-03 09:34:08 +0100 | [diff] [blame] | 1354 | dp83640->layer = PTP_CLASS_L2; |
| 1355 | dp83640->version = PTP_CLASS_V2; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1356 | break; |
| 1357 | case HWTSTAMP_FILTER_PTP_V2_EVENT: |
| 1358 | case HWTSTAMP_FILTER_PTP_V2_SYNC: |
| 1359 | case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: |
| 1360 | dp83640->hwts_rx_en = 1; |
Stefan Sørensen | a1f8723 | 2015-11-03 09:34:08 +0100 | [diff] [blame] | 1361 | dp83640->layer = PTP_CLASS_L4 | PTP_CLASS_L2; |
| 1362 | dp83640->version = PTP_CLASS_V2; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1363 | break; |
| 1364 | default: |
| 1365 | return -ERANGE; |
| 1366 | } |
| 1367 | |
| 1368 | txcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT; |
| 1369 | rxcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT; |
| 1370 | |
Stefan Sørensen | a1f8723 | 2015-11-03 09:34:08 +0100 | [diff] [blame] | 1371 | if (dp83640->layer & PTP_CLASS_L2) { |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1372 | txcfg0 |= TX_L2_EN; |
| 1373 | rxcfg0 |= RX_L2_EN; |
| 1374 | } |
Stefan Sørensen | a1f8723 | 2015-11-03 09:34:08 +0100 | [diff] [blame] | 1375 | if (dp83640->layer & PTP_CLASS_L4) { |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1376 | txcfg0 |= TX_IPV6_EN | TX_IPV4_EN; |
| 1377 | rxcfg0 |= RX_IPV6_EN | RX_IPV4_EN; |
| 1378 | } |
| 1379 | |
| 1380 | if (dp83640->hwts_tx_en) |
| 1381 | txcfg0 |= TX_TS_EN; |
| 1382 | |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 1383 | if (dp83640->hwts_tx_en == HWTSTAMP_TX_ONESTEP_SYNC) |
| 1384 | txcfg0 |= SYNC_1STEP | CHK_1STEP; |
| 1385 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1386 | if (dp83640->hwts_rx_en) |
| 1387 | rxcfg0 |= RX_TS_EN; |
| 1388 | |
| 1389 | mutex_lock(&dp83640->clock->extreg_lock); |
| 1390 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1391 | ext_write(0, phydev, PAGE5, PTP_TXCFG0, txcfg0); |
| 1392 | ext_write(0, phydev, PAGE5, PTP_RXCFG0, rxcfg0); |
| 1393 | |
| 1394 | mutex_unlock(&dp83640->clock->extreg_lock); |
| 1395 | |
| 1396 | return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; |
| 1397 | } |
| 1398 | |
| 1399 | static void rx_timestamp_work(struct work_struct *work) |
| 1400 | { |
| 1401 | struct dp83640_private *dp83640 = |
Stefan Sørensen | 4b06325 | 2015-11-03 09:34:05 +0100 | [diff] [blame] | 1402 | container_of(work, struct dp83640_private, ts_work.work); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1403 | struct sk_buff *skb; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1404 | |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 1405 | /* Deliver expired packets. */ |
| 1406 | while ((skb = skb_dequeue(&dp83640->rx_queue))) { |
| 1407 | struct dp83640_skb_info *skb_info; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1408 | |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 1409 | skb_info = (struct dp83640_skb_info *)skb->cb; |
| 1410 | if (!time_after(jiffies, skb_info->tmo)) { |
| 1411 | skb_queue_head(&dp83640->rx_queue, skb); |
| 1412 | break; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1413 | } |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 1414 | |
Manfred Rudigier | 72092cc | 2012-01-09 23:52:15 +0000 | [diff] [blame] | 1415 | netif_rx_ni(skb); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1416 | } |
| 1417 | |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 1418 | if (!skb_queue_empty(&dp83640->rx_queue)) |
Stefan Sørensen | 4b06325 | 2015-11-03 09:34:05 +0100 | [diff] [blame] | 1419 | schedule_delayed_work(&dp83640->ts_work, SKB_TIMESTAMP_TIMEOUT); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | static bool dp83640_rxtstamp(struct phy_device *phydev, |
| 1423 | struct sk_buff *skb, int type) |
| 1424 | { |
| 1425 | struct dp83640_private *dp83640 = phydev->priv; |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 1426 | struct dp83640_skb_info *skb_info = (struct dp83640_skb_info *)skb->cb; |
| 1427 | struct list_head *this, *next; |
| 1428 | struct rxts *rxts; |
| 1429 | struct skb_shared_hwtstamps *shhwtstamps = NULL; |
| 1430 | unsigned long flags; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1431 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1432 | if (is_status_frame(skb, type)) { |
| 1433 | decode_status_frame(dp83640, skb); |
Richard Cochran | ae6e86b | 2011-06-14 23:55:20 +0000 | [diff] [blame] | 1434 | kfree_skb(skb); |
| 1435 | return true; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1436 | } |
| 1437 | |
Stefan Sørensen | a12f78c | 2014-06-25 14:40:16 +0200 | [diff] [blame] | 1438 | if (!dp83640->hwts_rx_en) |
| 1439 | return false; |
| 1440 | |
Stefan Sørensen | a1f8723 | 2015-11-03 09:34:08 +0100 | [diff] [blame] | 1441 | if ((type & dp83640->version) == 0 || (type & dp83640->layer) == 0) |
| 1442 | return false; |
| 1443 | |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 1444 | spin_lock_irqsave(&dp83640->rx_lock, flags); |
Stefan Sørensen | ccf6ee9 | 2015-11-03 09:34:06 +0100 | [diff] [blame] | 1445 | prune_rx_ts(dp83640); |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 1446 | list_for_each_safe(this, next, &dp83640->rxts) { |
| 1447 | rxts = list_entry(this, struct rxts, list); |
| 1448 | if (match(skb, type, rxts)) { |
| 1449 | shhwtstamps = skb_hwtstamps(skb); |
| 1450 | memset(shhwtstamps, 0, sizeof(*shhwtstamps)); |
| 1451 | shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns); |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 1452 | list_del_init(&rxts->list); |
| 1453 | list_add(&rxts->list, &dp83640->rxpool); |
| 1454 | break; |
| 1455 | } |
| 1456 | } |
| 1457 | spin_unlock_irqrestore(&dp83640->rx_lock, flags); |
| 1458 | |
| 1459 | if (!shhwtstamps) { |
| 1460 | skb_info->ptp_type = type; |
Stefan Sørensen | 4b06325 | 2015-11-03 09:34:05 +0100 | [diff] [blame] | 1461 | skb_info->tmo = jiffies + SKB_TIMESTAMP_TIMEOUT; |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 1462 | skb_queue_tail(&dp83640->rx_queue, skb); |
Stefan Sørensen | 4b06325 | 2015-11-03 09:34:05 +0100 | [diff] [blame] | 1463 | schedule_delayed_work(&dp83640->ts_work, SKB_TIMESTAMP_TIMEOUT); |
Stefan Sørensen | d36b82b | 2017-08-30 08:58:47 +0200 | [diff] [blame] | 1464 | } else { |
| 1465 | netif_rx_ni(skb); |
Stefan Sørensen | 63502b8 | 2014-07-22 15:20:45 +0200 | [diff] [blame] | 1466 | } |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1467 | |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
| 1471 | static void dp83640_txtstamp(struct phy_device *phydev, |
| 1472 | struct sk_buff *skb, int type) |
| 1473 | { |
| 1474 | struct dp83640_private *dp83640 = phydev->priv; |
| 1475 | |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 1476 | switch (dp83640->hwts_tx_en) { |
| 1477 | |
| 1478 | case HWTSTAMP_TX_ONESTEP_SYNC: |
| 1479 | if (is_sync(skb, type)) { |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 1480 | kfree_skb(skb); |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 1481 | return; |
| 1482 | } |
| 1483 | /* fall through */ |
| 1484 | case HWTSTAMP_TX_ON: |
Stefan Sørensen | e2e2f51 | 2014-02-03 15:36:35 +0100 | [diff] [blame] | 1485 | skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 1486 | skb_queue_tail(&dp83640->tx_queue, skb); |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 1487 | break; |
| 1488 | |
| 1489 | case HWTSTAMP_TX_OFF: |
| 1490 | default: |
Alexander Duyck | 62bccb8 | 2014-09-04 13:31:35 -0400 | [diff] [blame] | 1491 | kfree_skb(skb); |
Richard Cochran | dccaa9e | 2011-09-20 01:43:16 +0000 | [diff] [blame] | 1492 | break; |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1493 | } |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Richard Cochran | 7dff349 | 2012-04-03 22:59:18 +0000 | [diff] [blame] | 1496 | static int dp83640_ts_info(struct phy_device *dev, struct ethtool_ts_info *info) |
| 1497 | { |
| 1498 | struct dp83640_private *dp83640 = dev->priv; |
| 1499 | |
| 1500 | info->so_timestamping = |
| 1501 | SOF_TIMESTAMPING_TX_HARDWARE | |
| 1502 | SOF_TIMESTAMPING_RX_HARDWARE | |
| 1503 | SOF_TIMESTAMPING_RAW_HARDWARE; |
| 1504 | info->phc_index = ptp_clock_index(dp83640->clock->ptp_clock); |
| 1505 | info->tx_types = |
| 1506 | (1 << HWTSTAMP_TX_OFF) | |
| 1507 | (1 << HWTSTAMP_TX_ON) | |
| 1508 | (1 << HWTSTAMP_TX_ONESTEP_SYNC); |
| 1509 | info->rx_filters = |
| 1510 | (1 << HWTSTAMP_FILTER_NONE) | |
| 1511 | (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) | |
Richard Cochran | 7dff349 | 2012-04-03 22:59:18 +0000 | [diff] [blame] | 1512 | (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) | |
Richard Cochran | 7dff349 | 2012-04-03 22:59:18 +0000 | [diff] [blame] | 1513 | (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) | |
Jacob Keller | 11b1544 | 2015-04-22 14:40:37 -0700 | [diff] [blame] | 1514 | (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); |
Richard Cochran | 7dff349 | 2012-04-03 22:59:18 +0000 | [diff] [blame] | 1515 | return 0; |
| 1516 | } |
| 1517 | |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1518 | static struct phy_driver dp83640_driver = { |
| 1519 | .phy_id = DP83640_PHY_ID, |
| 1520 | .phy_id_mask = 0xfffffff0, |
| 1521 | .name = "NatSemi DP83640", |
| 1522 | .features = PHY_BASIC_FEATURES, |
Stephan Gatzka | 1642182 | 2012-12-04 10:21:38 +0000 | [diff] [blame] | 1523 | .flags = PHY_HAS_INTERRUPT, |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1524 | .probe = dp83640_probe, |
| 1525 | .remove = dp83640_remove, |
Esben Haabendal | 76327a3 | 2018-04-08 22:17:01 +0200 | [diff] [blame] | 1526 | .soft_reset = dp83640_soft_reset, |
Stefan Sørensen | 62ad968 | 2014-02-03 15:36:58 +0100 | [diff] [blame] | 1527 | .config_init = dp83640_config_init, |
Stephan Gatzka | 1642182 | 2012-12-04 10:21:38 +0000 | [diff] [blame] | 1528 | .ack_interrupt = dp83640_ack_interrupt, |
| 1529 | .config_intr = dp83640_config_intr, |
Richard Cochran | 7dff349 | 2012-04-03 22:59:18 +0000 | [diff] [blame] | 1530 | .ts_info = dp83640_ts_info, |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1531 | .hwtstamp = dp83640_hwtstamp, |
| 1532 | .rxtstamp = dp83640_rxtstamp, |
| 1533 | .txtstamp = dp83640_txtstamp, |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1534 | }; |
| 1535 | |
| 1536 | static int __init dp83640_init(void) |
| 1537 | { |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 1538 | return phy_driver_register(&dp83640_driver, THIS_MODULE); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | static void __exit dp83640_exit(void) |
| 1542 | { |
| 1543 | dp83640_free_clocks(); |
| 1544 | phy_driver_unregister(&dp83640_driver); |
| 1545 | } |
| 1546 | |
| 1547 | MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver"); |
Richard Cochran | fbf4b93 | 2014-03-20 22:21:56 +0100 | [diff] [blame] | 1548 | MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>"); |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1549 | MODULE_LICENSE("GPL"); |
| 1550 | |
| 1551 | module_init(dp83640_init); |
| 1552 | module_exit(dp83640_exit); |
| 1553 | |
John Stultz | 86ff9baa | 2011-05-23 13:32:11 -0700 | [diff] [blame] | 1554 | static struct mdio_device_id __maybe_unused dp83640_tbl[] = { |
Richard Cochran | cb646e2 | 2011-04-22 12:04:55 +0200 | [diff] [blame] | 1555 | { DP83640_PHY_ID, 0xfffffff0 }, |
| 1556 | { } |
| 1557 | }; |
| 1558 | |
| 1559 | MODULE_DEVICE_TABLE(mdio, dp83640_tbl); |