dp83640.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. */
  20. #include <linux/ethtool.h>
  21. #include <linux/kernel.h>
  22. #include <linux/list.h>
  23. #include <linux/mii.h>
  24. #include <linux/module.h>
  25. #include <linux/net_tstamp.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/phy.h>
  28. #include <linux/ptp_classify.h>
  29. #include <linux/ptp_clock_kernel.h>
  30. #include "dp83640_reg.h"
  31. #define DP83640_PHY_ID 0x20005ce1
  32. #define PAGESEL 0x13
  33. #define LAYER4 0x02
  34. #define LAYER2 0x01
  35. #define MAX_RXTS 64
  36. #define N_EXT_TS 6
  37. #define PSF_PTPVER 2
  38. #define PSF_EVNT 0x4000
  39. #define PSF_RX 0x2000
  40. #define PSF_TX 0x1000
  41. #define EXT_EVENT 1
  42. #define CAL_EVENT 7
  43. #define CAL_TRIGGER 7
  44. #define PER_TRIGGER 6
  45. /* phyter seems to miss the mark by 16 ns */
  46. #define ADJTIME_FIX 16
  47. #if defined(__BIG_ENDIAN)
  48. #define ENDIAN_FLAG 0
  49. #elif defined(__LITTLE_ENDIAN)
  50. #define ENDIAN_FLAG PSF_ENDIAN
  51. #endif
  52. #define SKB_PTP_TYPE(__skb) (*(unsigned int *)((__skb)->cb))
  53. struct phy_rxts {
  54. u16 ns_lo; /* ns[15:0] */
  55. u16 ns_hi; /* overflow[1:0], ns[29:16] */
  56. u16 sec_lo; /* sec[15:0] */
  57. u16 sec_hi; /* sec[31:16] */
  58. u16 seqid; /* sequenceId[15:0] */
  59. u16 msgtype; /* messageType[3:0], hash[11:0] */
  60. };
  61. struct phy_txts {
  62. u16 ns_lo; /* ns[15:0] */
  63. u16 ns_hi; /* overflow[1:0], ns[29:16] */
  64. u16 sec_lo; /* sec[15:0] */
  65. u16 sec_hi; /* sec[31:16] */
  66. };
  67. struct rxts {
  68. struct list_head list;
  69. unsigned long tmo;
  70. u64 ns;
  71. u16 seqid;
  72. u8 msgtype;
  73. u16 hash;
  74. };
  75. struct dp83640_clock;
  76. struct dp83640_private {
  77. struct list_head list;
  78. struct dp83640_clock *clock;
  79. struct phy_device *phydev;
  80. struct work_struct ts_work;
  81. int hwts_tx_en;
  82. int hwts_rx_en;
  83. int layer;
  84. int version;
  85. /* remember state of cfg0 during calibration */
  86. int cfg0;
  87. /* remember the last event time stamp */
  88. struct phy_txts edata;
  89. /* list of rx timestamps */
  90. struct list_head rxts;
  91. struct list_head rxpool;
  92. struct rxts rx_pool_data[MAX_RXTS];
  93. /* protects above three fields from concurrent access */
  94. spinlock_t rx_lock;
  95. /* queues of incoming and outgoing packets */
  96. struct sk_buff_head rx_queue;
  97. struct sk_buff_head tx_queue;
  98. };
  99. struct dp83640_clock {
  100. /* keeps the instance in the 'phyter_clocks' list */
  101. struct list_head list;
  102. /* we create one clock instance per MII bus */
  103. struct mii_bus *bus;
  104. /* protects extended registers from concurrent access */
  105. struct mutex extreg_lock;
  106. /* remembers which page was last selected */
  107. int page;
  108. /* our advertised capabilities */
  109. struct ptp_clock_info caps;
  110. /* protects the three fields below from concurrent access */
  111. struct mutex clock_lock;
  112. /* the one phyter from which we shall read */
  113. struct dp83640_private *chosen;
  114. /* list of the other attached phyters, not chosen */
  115. struct list_head phylist;
  116. /* reference to our PTP hardware clock */
  117. struct ptp_clock *ptp_clock;
  118. };
  119. /* globals */
  120. enum {
  121. CALIBRATE_GPIO,
  122. PEROUT_GPIO,
  123. EXTTS0_GPIO,
  124. EXTTS1_GPIO,
  125. EXTTS2_GPIO,
  126. EXTTS3_GPIO,
  127. EXTTS4_GPIO,
  128. EXTTS5_GPIO,
  129. GPIO_TABLE_SIZE
  130. };
  131. static int chosen_phy = -1;
  132. static ushort gpio_tab[GPIO_TABLE_SIZE] = {
  133. 1, 2, 3, 4, 8, 9, 10, 11
  134. };
  135. module_param(chosen_phy, int, 0444);
  136. module_param_array(gpio_tab, ushort, NULL, 0444);
  137. MODULE_PARM_DESC(chosen_phy, \
  138. "The address of the PHY to use for the ancillary clock features");
  139. MODULE_PARM_DESC(gpio_tab, \
  140. "Which GPIO line to use for which purpose: cal,perout,extts1,...,extts6");
  141. /* a list of clocks and a mutex to protect it */
  142. static LIST_HEAD(phyter_clocks);
  143. static DEFINE_MUTEX(phyter_clocks_lock);
  144. static void rx_timestamp_work(struct work_struct *work);
  145. /* extended register access functions */
  146. #define BROADCAST_ADDR 31
  147. static inline int broadcast_write(struct mii_bus *bus, u32 regnum, u16 val)
  148. {
  149. return mdiobus_write(bus, BROADCAST_ADDR, regnum, val);
  150. }
  151. /* Caller must hold extreg_lock. */
  152. static int ext_read(struct phy_device *phydev, int page, u32 regnum)
  153. {
  154. struct dp83640_private *dp83640 = phydev->priv;
  155. int val;
  156. if (dp83640->clock->page != page) {
  157. broadcast_write(phydev->bus, PAGESEL, page);
  158. dp83640->clock->page = page;
  159. }
  160. val = phy_read(phydev, regnum);
  161. return val;
  162. }
  163. /* Caller must hold extreg_lock. */
  164. static void ext_write(int broadcast, struct phy_device *phydev,
  165. int page, u32 regnum, u16 val)
  166. {
  167. struct dp83640_private *dp83640 = phydev->priv;
  168. if (dp83640->clock->page != page) {
  169. broadcast_write(phydev->bus, PAGESEL, page);
  170. dp83640->clock->page = page;
  171. }
  172. if (broadcast)
  173. broadcast_write(phydev->bus, regnum, val);
  174. else
  175. phy_write(phydev, regnum, val);
  176. }
  177. /* Caller must hold extreg_lock. */
  178. static int tdr_write(int bc, struct phy_device *dev,
  179. const struct timespec *ts, u16 cmd)
  180. {
  181. ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec & 0xffff);/* ns[15:0] */
  182. ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_nsec >> 16); /* ns[31:16] */
  183. ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec & 0xffff); /* sec[15:0] */
  184. ext_write(bc, dev, PAGE4, PTP_TDR, ts->tv_sec >> 16); /* sec[31:16]*/
  185. ext_write(bc, dev, PAGE4, PTP_CTL, cmd);
  186. return 0;
  187. }
  188. /* convert phy timestamps into driver timestamps */
  189. static void phy2rxts(struct phy_rxts *p, struct rxts *rxts)
  190. {
  191. u32 sec;
  192. sec = p->sec_lo;
  193. sec |= p->sec_hi << 16;
  194. rxts->ns = p->ns_lo;
  195. rxts->ns |= (p->ns_hi & 0x3fff) << 16;
  196. rxts->ns += ((u64)sec) * 1000000000ULL;
  197. rxts->seqid = p->seqid;
  198. rxts->msgtype = (p->msgtype >> 12) & 0xf;
  199. rxts->hash = p->msgtype & 0x0fff;
  200. rxts->tmo = jiffies + 2;
  201. }
  202. static u64 phy2txts(struct phy_txts *p)
  203. {
  204. u64 ns;
  205. u32 sec;
  206. sec = p->sec_lo;
  207. sec |= p->sec_hi << 16;
  208. ns = p->ns_lo;
  209. ns |= (p->ns_hi & 0x3fff) << 16;
  210. ns += ((u64)sec) * 1000000000ULL;
  211. return ns;
  212. }
  213. static void periodic_output(struct dp83640_clock *clock,
  214. struct ptp_clock_request *clkreq, bool on)
  215. {
  216. struct dp83640_private *dp83640 = clock->chosen;
  217. struct phy_device *phydev = dp83640->phydev;
  218. u32 sec, nsec, period;
  219. u16 gpio, ptp_trig, trigger, val;
  220. gpio = on ? gpio_tab[PEROUT_GPIO] : 0;
  221. trigger = PER_TRIGGER;
  222. ptp_trig = TRIG_WR |
  223. (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT |
  224. (gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT |
  225. TRIG_PER |
  226. TRIG_PULSE;
  227. val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
  228. if (!on) {
  229. val |= TRIG_DIS;
  230. mutex_lock(&clock->extreg_lock);
  231. ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
  232. ext_write(0, phydev, PAGE4, PTP_CTL, val);
  233. mutex_unlock(&clock->extreg_lock);
  234. return;
  235. }
  236. sec = clkreq->perout.start.sec;
  237. nsec = clkreq->perout.start.nsec;
  238. period = clkreq->perout.period.sec * 1000000000UL;
  239. period += clkreq->perout.period.nsec;
  240. mutex_lock(&clock->extreg_lock);
  241. ext_write(0, phydev, PAGE5, PTP_TRIG, ptp_trig);
  242. /*load trigger*/
  243. val |= TRIG_LOAD;
  244. ext_write(0, phydev, PAGE4, PTP_CTL, val);
  245. ext_write(0, phydev, PAGE4, PTP_TDR, nsec & 0xffff); /* ns[15:0] */
  246. ext_write(0, phydev, PAGE4, PTP_TDR, nsec >> 16); /* ns[31:16] */
  247. ext_write(0, phydev, PAGE4, PTP_TDR, sec & 0xffff); /* sec[15:0] */
  248. ext_write(0, phydev, PAGE4, PTP_TDR, sec >> 16); /* sec[31:16] */
  249. ext_write(0, phydev, PAGE4, PTP_TDR, period & 0xffff); /* ns[15:0] */
  250. ext_write(0, phydev, PAGE4, PTP_TDR, period >> 16); /* ns[31:16] */
  251. /*enable trigger*/
  252. val &= ~TRIG_LOAD;
  253. val |= TRIG_EN;
  254. ext_write(0, phydev, PAGE4, PTP_CTL, val);
  255. mutex_unlock(&clock->extreg_lock);
  256. }
  257. /* ptp clock methods */
  258. static int ptp_dp83640_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  259. {
  260. struct dp83640_clock *clock =
  261. container_of(ptp, struct dp83640_clock, caps);
  262. struct phy_device *phydev = clock->chosen->phydev;
  263. u64 rate;
  264. int neg_adj = 0;
  265. u16 hi, lo;
  266. if (ppb < 0) {
  267. neg_adj = 1;
  268. ppb = -ppb;
  269. }
  270. rate = ppb;
  271. rate <<= 26;
  272. rate = div_u64(rate, 1953125);
  273. hi = (rate >> 16) & PTP_RATE_HI_MASK;
  274. if (neg_adj)
  275. hi |= PTP_RATE_DIR;
  276. lo = rate & 0xffff;
  277. mutex_lock(&clock->extreg_lock);
  278. ext_write(1, phydev, PAGE4, PTP_RATEH, hi);
  279. ext_write(1, phydev, PAGE4, PTP_RATEL, lo);
  280. mutex_unlock(&clock->extreg_lock);
  281. return 0;
  282. }
  283. static int ptp_dp83640_adjtime(struct ptp_clock_info *ptp, s64 delta)
  284. {
  285. struct dp83640_clock *clock =
  286. container_of(ptp, struct dp83640_clock, caps);
  287. struct phy_device *phydev = clock->chosen->phydev;
  288. struct timespec ts;
  289. int err;
  290. delta += ADJTIME_FIX;
  291. ts = ns_to_timespec(delta);
  292. mutex_lock(&clock->extreg_lock);
  293. err = tdr_write(1, phydev, &ts, PTP_STEP_CLK);
  294. mutex_unlock(&clock->extreg_lock);
  295. return err;
  296. }
  297. static int ptp_dp83640_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  298. {
  299. struct dp83640_clock *clock =
  300. container_of(ptp, struct dp83640_clock, caps);
  301. struct phy_device *phydev = clock->chosen->phydev;
  302. unsigned int val[4];
  303. mutex_lock(&clock->extreg_lock);
  304. ext_write(0, phydev, PAGE4, PTP_CTL, PTP_RD_CLK);
  305. val[0] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[15:0] */
  306. val[1] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[31:16] */
  307. val[2] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[15:0] */
  308. val[3] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[31:16] */
  309. mutex_unlock(&clock->extreg_lock);
  310. ts->tv_nsec = val[0] | (val[1] << 16);
  311. ts->tv_sec = val[2] | (val[3] << 16);
  312. return 0;
  313. }
  314. static int ptp_dp83640_settime(struct ptp_clock_info *ptp,
  315. const struct timespec *ts)
  316. {
  317. struct dp83640_clock *clock =
  318. container_of(ptp, struct dp83640_clock, caps);
  319. struct phy_device *phydev = clock->chosen->phydev;
  320. int err;
  321. mutex_lock(&clock->extreg_lock);
  322. err = tdr_write(1, phydev, ts, PTP_LOAD_CLK);
  323. mutex_unlock(&clock->extreg_lock);
  324. return err;
  325. }
  326. static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
  327. struct ptp_clock_request *rq, int on)
  328. {
  329. struct dp83640_clock *clock =
  330. container_of(ptp, struct dp83640_clock, caps);
  331. struct phy_device *phydev = clock->chosen->phydev;
  332. int index;
  333. u16 evnt, event_num, gpio_num;
  334. switch (rq->type) {
  335. case PTP_CLK_REQ_EXTTS:
  336. index = rq->extts.index;
  337. if (index < 0 || index >= N_EXT_TS)
  338. return -EINVAL;
  339. event_num = EXT_EVENT + index;
  340. evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
  341. if (on) {
  342. gpio_num = gpio_tab[EXTTS0_GPIO + index];
  343. evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
  344. evnt |= EVNT_RISE;
  345. }
  346. ext_write(0, phydev, PAGE5, PTP_EVNT, evnt);
  347. return 0;
  348. case PTP_CLK_REQ_PEROUT:
  349. if (rq->perout.index != 0)
  350. return -EINVAL;
  351. periodic_output(clock, rq, on);
  352. return 0;
  353. default:
  354. break;
  355. }
  356. return -EOPNOTSUPP;
  357. }
  358. static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
  359. static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
  360. static void enable_status_frames(struct phy_device *phydev, bool on)
  361. {
  362. u16 cfg0 = 0, ver;
  363. if (on)
  364. cfg0 = PSF_EVNT_EN | PSF_RXTS_EN | PSF_TXTS_EN | ENDIAN_FLAG;
  365. ver = (PSF_PTPVER & VERSIONPTP_MASK) << VERSIONPTP_SHIFT;
  366. ext_write(0, phydev, PAGE5, PSF_CFG0, cfg0);
  367. ext_write(0, phydev, PAGE6, PSF_CFG1, ver);
  368. if (!phydev->attached_dev) {
  369. pr_warning("dp83640: expected to find an attached netdevice\n");
  370. return;
  371. }
  372. if (on) {
  373. if (dev_mc_add(phydev->attached_dev, status_frame_dst))
  374. pr_warning("dp83640: failed to add mc address\n");
  375. } else {
  376. if (dev_mc_del(phydev->attached_dev, status_frame_dst))
  377. pr_warning("dp83640: failed to delete mc address\n");
  378. }
  379. }
  380. static bool is_status_frame(struct sk_buff *skb, int type)
  381. {
  382. struct ethhdr *h = eth_hdr(skb);
  383. if (PTP_CLASS_V2_L2 == type &&
  384. !memcmp(h->h_source, status_frame_src, sizeof(status_frame_src)))
  385. return true;
  386. else
  387. return false;
  388. }
  389. static int expired(struct rxts *rxts)
  390. {
  391. return time_after(jiffies, rxts->tmo);
  392. }
  393. /* Caller must hold rx_lock. */
  394. static void prune_rx_ts(struct dp83640_private *dp83640)
  395. {
  396. struct list_head *this, *next;
  397. struct rxts *rxts;
  398. list_for_each_safe(this, next, &dp83640->rxts) {
  399. rxts = list_entry(this, struct rxts, list);
  400. if (expired(rxts)) {
  401. list_del_init(&rxts->list);
  402. list_add(&rxts->list, &dp83640->rxpool);
  403. }
  404. }
  405. }
  406. /* synchronize the phyters so they act as one clock */
  407. static void enable_broadcast(struct phy_device *phydev, int init_page, int on)
  408. {
  409. int val;
  410. phy_write(phydev, PAGESEL, 0);
  411. val = phy_read(phydev, PHYCR2);
  412. if (on)
  413. val |= BC_WRITE;
  414. else
  415. val &= ~BC_WRITE;
  416. phy_write(phydev, PHYCR2, val);
  417. phy_write(phydev, PAGESEL, init_page);
  418. }
  419. static void recalibrate(struct dp83640_clock *clock)
  420. {
  421. s64 now, diff;
  422. struct phy_txts event_ts;
  423. struct timespec ts;
  424. struct list_head *this;
  425. struct dp83640_private *tmp;
  426. struct phy_device *master = clock->chosen->phydev;
  427. u16 cal_gpio, cfg0, evnt, ptp_trig, trigger, val;
  428. trigger = CAL_TRIGGER;
  429. cal_gpio = gpio_tab[CALIBRATE_GPIO];
  430. mutex_lock(&clock->extreg_lock);
  431. /*
  432. * enable broadcast, disable status frames, enable ptp clock
  433. */
  434. list_for_each(this, &clock->phylist) {
  435. tmp = list_entry(this, struct dp83640_private, list);
  436. enable_broadcast(tmp->phydev, clock->page, 1);
  437. tmp->cfg0 = ext_read(tmp->phydev, PAGE5, PSF_CFG0);
  438. ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, 0);
  439. ext_write(0, tmp->phydev, PAGE4, PTP_CTL, PTP_ENABLE);
  440. }
  441. enable_broadcast(master, clock->page, 1);
  442. cfg0 = ext_read(master, PAGE5, PSF_CFG0);
  443. ext_write(0, master, PAGE5, PSF_CFG0, 0);
  444. ext_write(0, master, PAGE4, PTP_CTL, PTP_ENABLE);
  445. /*
  446. * enable an event timestamp
  447. */
  448. evnt = EVNT_WR | EVNT_RISE | EVNT_SINGLE;
  449. evnt |= (CAL_EVENT & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
  450. evnt |= (cal_gpio & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
  451. list_for_each(this, &clock->phylist) {
  452. tmp = list_entry(this, struct dp83640_private, list);
  453. ext_write(0, tmp->phydev, PAGE5, PTP_EVNT, evnt);
  454. }
  455. ext_write(0, master, PAGE5, PTP_EVNT, evnt);
  456. /*
  457. * configure a trigger
  458. */
  459. ptp_trig = TRIG_WR | TRIG_IF_LATE | TRIG_PULSE;
  460. ptp_trig |= (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT;
  461. ptp_trig |= (cal_gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT;
  462. ext_write(0, master, PAGE5, PTP_TRIG, ptp_trig);
  463. /* load trigger */
  464. val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
  465. val |= TRIG_LOAD;
  466. ext_write(0, master, PAGE4, PTP_CTL, val);
  467. /* enable trigger */
  468. val &= ~TRIG_LOAD;
  469. val |= TRIG_EN;
  470. ext_write(0, master, PAGE4, PTP_CTL, val);
  471. /* disable trigger */
  472. val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
  473. val |= TRIG_DIS;
  474. ext_write(0, master, PAGE4, PTP_CTL, val);
  475. /*
  476. * read out and correct offsets
  477. */
  478. val = ext_read(master, PAGE4, PTP_STS);
  479. pr_info("master PTP_STS 0x%04hx", val);
  480. val = ext_read(master, PAGE4, PTP_ESTS);
  481. pr_info("master PTP_ESTS 0x%04hx", val);
  482. event_ts.ns_lo = ext_read(master, PAGE4, PTP_EDATA);
  483. event_ts.ns_hi = ext_read(master, PAGE4, PTP_EDATA);
  484. event_ts.sec_lo = ext_read(master, PAGE4, PTP_EDATA);
  485. event_ts.sec_hi = ext_read(master, PAGE4, PTP_EDATA);
  486. now = phy2txts(&event_ts);
  487. list_for_each(this, &clock->phylist) {
  488. tmp = list_entry(this, struct dp83640_private, list);
  489. val = ext_read(tmp->phydev, PAGE4, PTP_STS);
  490. pr_info("slave PTP_STS 0x%04hx", val);
  491. val = ext_read(tmp->phydev, PAGE4, PTP_ESTS);
  492. pr_info("slave PTP_ESTS 0x%04hx", val);
  493. event_ts.ns_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
  494. event_ts.ns_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
  495. event_ts.sec_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
  496. event_ts.sec_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
  497. diff = now - (s64) phy2txts(&event_ts);
  498. pr_info("slave offset %lld nanoseconds\n", diff);
  499. diff += ADJTIME_FIX;
  500. ts = ns_to_timespec(diff);
  501. tdr_write(0, tmp->phydev, &ts, PTP_STEP_CLK);
  502. }
  503. /*
  504. * restore status frames
  505. */
  506. list_for_each(this, &clock->phylist) {
  507. tmp = list_entry(this, struct dp83640_private, list);
  508. ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, tmp->cfg0);
  509. }
  510. ext_write(0, master, PAGE5, PSF_CFG0, cfg0);
  511. mutex_unlock(&clock->extreg_lock);
  512. }
  513. /* time stamping methods */
  514. static inline u16 exts_chan_to_edata(int ch)
  515. {
  516. return 1 << ((ch + EXT_EVENT) * 2);
  517. }
  518. static int decode_evnt(struct dp83640_private *dp83640,
  519. void *data, u16 ests)
  520. {
  521. struct phy_txts *phy_txts;
  522. struct ptp_clock_event event;
  523. int i, parsed;
  524. int words = (ests >> EVNT_TS_LEN_SHIFT) & EVNT_TS_LEN_MASK;
  525. u16 ext_status = 0;
  526. if (ests & MULT_EVNT) {
  527. ext_status = *(u16 *) data;
  528. data += sizeof(ext_status);
  529. }
  530. phy_txts = data;
  531. switch (words) { /* fall through in every case */
  532. case 3:
  533. dp83640->edata.sec_hi = phy_txts->sec_hi;
  534. case 2:
  535. dp83640->edata.sec_lo = phy_txts->sec_lo;
  536. case 1:
  537. dp83640->edata.ns_hi = phy_txts->ns_hi;
  538. case 0:
  539. dp83640->edata.ns_lo = phy_txts->ns_lo;
  540. }
  541. if (ext_status) {
  542. parsed = words + 2;
  543. } else {
  544. parsed = words + 1;
  545. i = ((ests >> EVNT_NUM_SHIFT) & EVNT_NUM_MASK) - EXT_EVENT;
  546. ext_status = exts_chan_to_edata(i);
  547. }
  548. event.type = PTP_CLOCK_EXTTS;
  549. event.timestamp = phy2txts(&dp83640->edata);
  550. for (i = 0; i < N_EXT_TS; i++) {
  551. if (ext_status & exts_chan_to_edata(i)) {
  552. event.index = i;
  553. ptp_clock_event(dp83640->clock->ptp_clock, &event);
  554. }
  555. }
  556. return parsed * sizeof(u16);
  557. }
  558. static void decode_rxts(struct dp83640_private *dp83640,
  559. struct phy_rxts *phy_rxts)
  560. {
  561. struct rxts *rxts;
  562. unsigned long flags;
  563. spin_lock_irqsave(&dp83640->rx_lock, flags);
  564. prune_rx_ts(dp83640);
  565. if (list_empty(&dp83640->rxpool)) {
  566. pr_debug("dp83640: rx timestamp pool is empty\n");
  567. goto out;
  568. }
  569. rxts = list_first_entry(&dp83640->rxpool, struct rxts, list);
  570. list_del_init(&rxts->list);
  571. phy2rxts(phy_rxts, rxts);
  572. list_add_tail(&rxts->list, &dp83640->rxts);
  573. out:
  574. spin_unlock_irqrestore(&dp83640->rx_lock, flags);
  575. }
  576. static void decode_txts(struct dp83640_private *dp83640,
  577. struct phy_txts *phy_txts)
  578. {
  579. struct skb_shared_hwtstamps shhwtstamps;
  580. struct sk_buff *skb;
  581. u64 ns;
  582. /* We must already have the skb that triggered this. */
  583. skb = skb_dequeue(&dp83640->tx_queue);
  584. if (!skb) {
  585. pr_debug("dp83640: have timestamp but tx_queue empty\n");
  586. return;
  587. }
  588. ns = phy2txts(phy_txts);
  589. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  590. shhwtstamps.hwtstamp = ns_to_ktime(ns);
  591. skb_complete_tx_timestamp(skb, &shhwtstamps);
  592. }
  593. static void decode_status_frame(struct dp83640_private *dp83640,
  594. struct sk_buff *skb)
  595. {
  596. struct phy_rxts *phy_rxts;
  597. struct phy_txts *phy_txts;
  598. u8 *ptr;
  599. int len, size;
  600. u16 ests, type;
  601. ptr = skb->data + 2;
  602. for (len = skb_headlen(skb) - 2; len > sizeof(type); len -= size) {
  603. type = *(u16 *)ptr;
  604. ests = type & 0x0fff;
  605. type = type & 0xf000;
  606. len -= sizeof(type);
  607. ptr += sizeof(type);
  608. if (PSF_RX == type && len >= sizeof(*phy_rxts)) {
  609. phy_rxts = (struct phy_rxts *) ptr;
  610. decode_rxts(dp83640, phy_rxts);
  611. size = sizeof(*phy_rxts);
  612. } else if (PSF_TX == type && len >= sizeof(*phy_txts)) {
  613. phy_txts = (struct phy_txts *) ptr;
  614. decode_txts(dp83640, phy_txts);
  615. size = sizeof(*phy_txts);
  616. } else if (PSF_EVNT == type && len >= sizeof(*phy_txts)) {
  617. size = decode_evnt(dp83640, ptr, ests);
  618. } else {
  619. size = 0;
  620. break;
  621. }
  622. ptr += size;
  623. }
  624. }
  625. static int is_sync(struct sk_buff *skb, int type)
  626. {
  627. u8 *data = skb->data, *msgtype;
  628. unsigned int offset = 0;
  629. switch (type) {
  630. case PTP_CLASS_V1_IPV4:
  631. case PTP_CLASS_V2_IPV4:
  632. offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
  633. break;
  634. case PTP_CLASS_V1_IPV6:
  635. case PTP_CLASS_V2_IPV6:
  636. offset = OFF_PTP6;
  637. break;
  638. case PTP_CLASS_V2_L2:
  639. offset = ETH_HLEN;
  640. break;
  641. case PTP_CLASS_V2_VLAN:
  642. offset = ETH_HLEN + VLAN_HLEN;
  643. break;
  644. default:
  645. return 0;
  646. }
  647. if (type & PTP_CLASS_V1)
  648. offset += OFF_PTP_CONTROL;
  649. if (skb->len < offset + 1)
  650. return 0;
  651. msgtype = data + offset;
  652. return (*msgtype & 0xf) == 0;
  653. }
  654. static int match(struct sk_buff *skb, unsigned int type, struct rxts *rxts)
  655. {
  656. u16 *seqid;
  657. unsigned int offset;
  658. u8 *msgtype, *data = skb_mac_header(skb);
  659. /* check sequenceID, messageType, 12 bit hash of offset 20-29 */
  660. switch (type) {
  661. case PTP_CLASS_V1_IPV4:
  662. case PTP_CLASS_V2_IPV4:
  663. offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
  664. break;
  665. case PTP_CLASS_V1_IPV6:
  666. case PTP_CLASS_V2_IPV6:
  667. offset = OFF_PTP6;
  668. break;
  669. case PTP_CLASS_V2_L2:
  670. offset = ETH_HLEN;
  671. break;
  672. case PTP_CLASS_V2_VLAN:
  673. offset = ETH_HLEN + VLAN_HLEN;
  674. break;
  675. default:
  676. return 0;
  677. }
  678. if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
  679. return 0;
  680. if (unlikely(type & PTP_CLASS_V1))
  681. msgtype = data + offset + OFF_PTP_CONTROL;
  682. else
  683. msgtype = data + offset;
  684. seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
  685. return (rxts->msgtype == (*msgtype & 0xf) &&
  686. rxts->seqid == ntohs(*seqid));
  687. }
  688. static void dp83640_free_clocks(void)
  689. {
  690. struct dp83640_clock *clock;
  691. struct list_head *this, *next;
  692. mutex_lock(&phyter_clocks_lock);
  693. list_for_each_safe(this, next, &phyter_clocks) {
  694. clock = list_entry(this, struct dp83640_clock, list);
  695. if (!list_empty(&clock->phylist)) {
  696. pr_warning("phy list non-empty while unloading");
  697. BUG();
  698. }
  699. list_del(&clock->list);
  700. mutex_destroy(&clock->extreg_lock);
  701. mutex_destroy(&clock->clock_lock);
  702. put_device(&clock->bus->dev);
  703. kfree(clock);
  704. }
  705. mutex_unlock(&phyter_clocks_lock);
  706. }
  707. static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus)
  708. {
  709. INIT_LIST_HEAD(&clock->list);
  710. clock->bus = bus;
  711. mutex_init(&clock->extreg_lock);
  712. mutex_init(&clock->clock_lock);
  713. INIT_LIST_HEAD(&clock->phylist);
  714. clock->caps.owner = THIS_MODULE;
  715. sprintf(clock->caps.name, "dp83640 timer");
  716. clock->caps.max_adj = 1953124;
  717. clock->caps.n_alarm = 0;
  718. clock->caps.n_ext_ts = N_EXT_TS;
  719. clock->caps.n_per_out = 1;
  720. clock->caps.pps = 0;
  721. clock->caps.adjfreq = ptp_dp83640_adjfreq;
  722. clock->caps.adjtime = ptp_dp83640_adjtime;
  723. clock->caps.gettime = ptp_dp83640_gettime;
  724. clock->caps.settime = ptp_dp83640_settime;
  725. clock->caps.enable = ptp_dp83640_enable;
  726. /*
  727. * Get a reference to this bus instance.
  728. */
  729. get_device(&bus->dev);
  730. }
  731. static int choose_this_phy(struct dp83640_clock *clock,
  732. struct phy_device *phydev)
  733. {
  734. if (chosen_phy == -1 && !clock->chosen)
  735. return 1;
  736. if (chosen_phy == phydev->addr)
  737. return 1;
  738. return 0;
  739. }
  740. static struct dp83640_clock *dp83640_clock_get(struct dp83640_clock *clock)
  741. {
  742. if (clock)
  743. mutex_lock(&clock->clock_lock);
  744. return clock;
  745. }
  746. /*
  747. * Look up and lock a clock by bus instance.
  748. * If there is no clock for this bus, then create it first.
  749. */
  750. static struct dp83640_clock *dp83640_clock_get_bus(struct mii_bus *bus)
  751. {
  752. struct dp83640_clock *clock = NULL, *tmp;
  753. struct list_head *this;
  754. mutex_lock(&phyter_clocks_lock);
  755. list_for_each(this, &phyter_clocks) {
  756. tmp = list_entry(this, struct dp83640_clock, list);
  757. if (tmp->bus == bus) {
  758. clock = tmp;
  759. break;
  760. }
  761. }
  762. if (clock)
  763. goto out;
  764. clock = kzalloc(sizeof(struct dp83640_clock), GFP_KERNEL);
  765. if (!clock)
  766. goto out;
  767. dp83640_clock_init(clock, bus);
  768. list_add_tail(&phyter_clocks, &clock->list);
  769. out:
  770. mutex_unlock(&phyter_clocks_lock);
  771. return dp83640_clock_get(clock);
  772. }
  773. static void dp83640_clock_put(struct dp83640_clock *clock)
  774. {
  775. mutex_unlock(&clock->clock_lock);
  776. }
  777. static int dp83640_probe(struct phy_device *phydev)
  778. {
  779. struct dp83640_clock *clock;
  780. struct dp83640_private *dp83640;
  781. int err = -ENOMEM, i;
  782. if (phydev->addr == BROADCAST_ADDR)
  783. return 0;
  784. clock = dp83640_clock_get_bus(phydev->bus);
  785. if (!clock)
  786. goto no_clock;
  787. dp83640 = kzalloc(sizeof(struct dp83640_private), GFP_KERNEL);
  788. if (!dp83640)
  789. goto no_memory;
  790. dp83640->phydev = phydev;
  791. INIT_WORK(&dp83640->ts_work, rx_timestamp_work);
  792. INIT_LIST_HEAD(&dp83640->rxts);
  793. INIT_LIST_HEAD(&dp83640->rxpool);
  794. for (i = 0; i < MAX_RXTS; i++)
  795. list_add(&dp83640->rx_pool_data[i].list, &dp83640->rxpool);
  796. phydev->priv = dp83640;
  797. spin_lock_init(&dp83640->rx_lock);
  798. skb_queue_head_init(&dp83640->rx_queue);
  799. skb_queue_head_init(&dp83640->tx_queue);
  800. dp83640->clock = clock;
  801. if (choose_this_phy(clock, phydev)) {
  802. clock->chosen = dp83640;
  803. clock->ptp_clock = ptp_clock_register(&clock->caps);
  804. if (IS_ERR(clock->ptp_clock)) {
  805. err = PTR_ERR(clock->ptp_clock);
  806. goto no_register;
  807. }
  808. } else
  809. list_add_tail(&dp83640->list, &clock->phylist);
  810. if (clock->chosen && !list_empty(&clock->phylist))
  811. recalibrate(clock);
  812. else
  813. enable_broadcast(dp83640->phydev, clock->page, 1);
  814. dp83640_clock_put(clock);
  815. return 0;
  816. no_register:
  817. clock->chosen = NULL;
  818. kfree(dp83640);
  819. no_memory:
  820. dp83640_clock_put(clock);
  821. no_clock:
  822. return err;
  823. }
  824. static void dp83640_remove(struct phy_device *phydev)
  825. {
  826. struct dp83640_clock *clock;
  827. struct list_head *this, *next;
  828. struct dp83640_private *tmp, *dp83640 = phydev->priv;
  829. struct sk_buff *skb;
  830. if (phydev->addr == BROADCAST_ADDR)
  831. return;
  832. enable_status_frames(phydev, false);
  833. cancel_work_sync(&dp83640->ts_work);
  834. while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL)
  835. kfree_skb(skb);
  836. while ((skb = skb_dequeue(&dp83640->tx_queue)) != NULL)
  837. skb_complete_tx_timestamp(skb, NULL);
  838. clock = dp83640_clock_get(dp83640->clock);
  839. if (dp83640 == clock->chosen) {
  840. ptp_clock_unregister(clock->ptp_clock);
  841. clock->chosen = NULL;
  842. } else {
  843. list_for_each_safe(this, next, &clock->phylist) {
  844. tmp = list_entry(this, struct dp83640_private, list);
  845. if (tmp == dp83640) {
  846. list_del_init(&tmp->list);
  847. break;
  848. }
  849. }
  850. }
  851. dp83640_clock_put(clock);
  852. kfree(dp83640);
  853. }
  854. static int dp83640_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
  855. {
  856. struct dp83640_private *dp83640 = phydev->priv;
  857. struct hwtstamp_config cfg;
  858. u16 txcfg0, rxcfg0;
  859. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  860. return -EFAULT;
  861. if (cfg.flags) /* reserved for future extensions */
  862. return -EINVAL;
  863. if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
  864. return -ERANGE;
  865. dp83640->hwts_tx_en = cfg.tx_type;
  866. switch (cfg.rx_filter) {
  867. case HWTSTAMP_FILTER_NONE:
  868. dp83640->hwts_rx_en = 0;
  869. dp83640->layer = 0;
  870. dp83640->version = 0;
  871. break;
  872. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  873. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  874. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  875. dp83640->hwts_rx_en = 1;
  876. dp83640->layer = LAYER4;
  877. dp83640->version = 1;
  878. break;
  879. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  880. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  881. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  882. dp83640->hwts_rx_en = 1;
  883. dp83640->layer = LAYER4;
  884. dp83640->version = 2;
  885. break;
  886. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  887. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  888. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  889. dp83640->hwts_rx_en = 1;
  890. dp83640->layer = LAYER2;
  891. dp83640->version = 2;
  892. break;
  893. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  894. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  895. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  896. dp83640->hwts_rx_en = 1;
  897. dp83640->layer = LAYER4|LAYER2;
  898. dp83640->version = 2;
  899. break;
  900. default:
  901. return -ERANGE;
  902. }
  903. txcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
  904. rxcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
  905. if (dp83640->layer & LAYER2) {
  906. txcfg0 |= TX_L2_EN;
  907. rxcfg0 |= RX_L2_EN;
  908. }
  909. if (dp83640->layer & LAYER4) {
  910. txcfg0 |= TX_IPV6_EN | TX_IPV4_EN;
  911. rxcfg0 |= RX_IPV6_EN | RX_IPV4_EN;
  912. }
  913. if (dp83640->hwts_tx_en)
  914. txcfg0 |= TX_TS_EN;
  915. if (dp83640->hwts_tx_en == HWTSTAMP_TX_ONESTEP_SYNC)
  916. txcfg0 |= SYNC_1STEP | CHK_1STEP;
  917. if (dp83640->hwts_rx_en)
  918. rxcfg0 |= RX_TS_EN;
  919. mutex_lock(&dp83640->clock->extreg_lock);
  920. if (dp83640->hwts_tx_en || dp83640->hwts_rx_en) {
  921. enable_status_frames(phydev, true);
  922. ext_write(0, phydev, PAGE4, PTP_CTL, PTP_ENABLE);
  923. }
  924. ext_write(0, phydev, PAGE5, PTP_TXCFG0, txcfg0);
  925. ext_write(0, phydev, PAGE5, PTP_RXCFG0, rxcfg0);
  926. mutex_unlock(&dp83640->clock->extreg_lock);
  927. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  928. }
  929. static void rx_timestamp_work(struct work_struct *work)
  930. {
  931. struct dp83640_private *dp83640 =
  932. container_of(work, struct dp83640_private, ts_work);
  933. struct list_head *this, *next;
  934. struct rxts *rxts;
  935. struct skb_shared_hwtstamps *shhwtstamps;
  936. struct sk_buff *skb;
  937. unsigned int type;
  938. unsigned long flags;
  939. /* Deliver each deferred packet, with or without a time stamp. */
  940. while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL) {
  941. type = SKB_PTP_TYPE(skb);
  942. spin_lock_irqsave(&dp83640->rx_lock, flags);
  943. list_for_each_safe(this, next, &dp83640->rxts) {
  944. rxts = list_entry(this, struct rxts, list);
  945. if (match(skb, type, rxts)) {
  946. shhwtstamps = skb_hwtstamps(skb);
  947. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  948. shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
  949. list_del_init(&rxts->list);
  950. list_add(&rxts->list, &dp83640->rxpool);
  951. break;
  952. }
  953. }
  954. spin_unlock_irqrestore(&dp83640->rx_lock, flags);
  955. netif_rx_ni(skb);
  956. }
  957. /* Clear out expired time stamps. */
  958. spin_lock_irqsave(&dp83640->rx_lock, flags);
  959. prune_rx_ts(dp83640);
  960. spin_unlock_irqrestore(&dp83640->rx_lock, flags);
  961. }
  962. static bool dp83640_rxtstamp(struct phy_device *phydev,
  963. struct sk_buff *skb, int type)
  964. {
  965. struct dp83640_private *dp83640 = phydev->priv;
  966. if (!dp83640->hwts_rx_en)
  967. return false;
  968. if (is_status_frame(skb, type)) {
  969. decode_status_frame(dp83640, skb);
  970. kfree_skb(skb);
  971. return true;
  972. }
  973. SKB_PTP_TYPE(skb) = type;
  974. skb_queue_tail(&dp83640->rx_queue, skb);
  975. schedule_work(&dp83640->ts_work);
  976. return true;
  977. }
  978. static void dp83640_txtstamp(struct phy_device *phydev,
  979. struct sk_buff *skb, int type)
  980. {
  981. struct dp83640_private *dp83640 = phydev->priv;
  982. switch (dp83640->hwts_tx_en) {
  983. case HWTSTAMP_TX_ONESTEP_SYNC:
  984. if (is_sync(skb, type)) {
  985. skb_complete_tx_timestamp(skb, NULL);
  986. return;
  987. }
  988. /* fall through */
  989. case HWTSTAMP_TX_ON:
  990. skb_queue_tail(&dp83640->tx_queue, skb);
  991. schedule_work(&dp83640->ts_work);
  992. break;
  993. case HWTSTAMP_TX_OFF:
  994. default:
  995. skb_complete_tx_timestamp(skb, NULL);
  996. break;
  997. }
  998. }
  999. static struct phy_driver dp83640_driver = {
  1000. .phy_id = DP83640_PHY_ID,
  1001. .phy_id_mask = 0xfffffff0,
  1002. .name = "NatSemi DP83640",
  1003. .features = PHY_BASIC_FEATURES,
  1004. .flags = 0,
  1005. .probe = dp83640_probe,
  1006. .remove = dp83640_remove,
  1007. .config_aneg = genphy_config_aneg,
  1008. .read_status = genphy_read_status,
  1009. .hwtstamp = dp83640_hwtstamp,
  1010. .rxtstamp = dp83640_rxtstamp,
  1011. .txtstamp = dp83640_txtstamp,
  1012. .driver = {.owner = THIS_MODULE,}
  1013. };
  1014. static int __init dp83640_init(void)
  1015. {
  1016. return phy_driver_register(&dp83640_driver);
  1017. }
  1018. static void __exit dp83640_exit(void)
  1019. {
  1020. dp83640_free_clocks();
  1021. phy_driver_unregister(&dp83640_driver);
  1022. }
  1023. MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver");
  1024. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.at>");
  1025. MODULE_LICENSE("GPL");
  1026. module_init(dp83640_init);
  1027. module_exit(dp83640_exit);
  1028. static struct mdio_device_id __maybe_unused dp83640_tbl[] = {
  1029. { DP83640_PHY_ID, 0xfffffff0 },
  1030. { }
  1031. };
  1032. MODULE_DEVICE_TABLE(mdio, dp83640_tbl);