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