dp83640.c 38 KB

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