dp83640.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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/ethtool.h>
  22. #include <linux/kernel.h>
  23. #include <linux/list.h>
  24. #include <linux/mii.h>
  25. #include <linux/module.h>
  26. #include <linux/net_tstamp.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/phy.h>
  30. #include <linux/ptp_classify.h>
  31. #include <linux/ptp_clock_kernel.h>
  32. #include "dp83640_reg.h"
  33. #define DP83640_PHY_ID 0x20005ce1
  34. #define PAGESEL 0x13
  35. #define LAYER4 0x02
  36. #define LAYER2 0x01
  37. #define MAX_RXTS 64
  38. #define N_EXT_TS 6
  39. #define N_PER_OUT 7
  40. #define PSF_PTPVER 2
  41. #define PSF_EVNT 0x4000
  42. #define PSF_RX 0x2000
  43. #define PSF_TX 0x1000
  44. #define EXT_EVENT 1
  45. #define CAL_EVENT 7
  46. #define CAL_TRIGGER 7
  47. #define DP83640_N_PINS 12
  48. #define MII_DP83640_MICR 0x11
  49. #define MII_DP83640_MISR 0x12
  50. #define MII_DP83640_MICR_OE 0x1
  51. #define MII_DP83640_MICR_IE 0x2
  52. #define MII_DP83640_MISR_RHF_INT_EN 0x01
  53. #define MII_DP83640_MISR_FHF_INT_EN 0x02
  54. #define MII_DP83640_MISR_ANC_INT_EN 0x04
  55. #define MII_DP83640_MISR_DUP_INT_EN 0x08
  56. #define MII_DP83640_MISR_SPD_INT_EN 0x10
  57. #define MII_DP83640_MISR_LINK_INT_EN 0x20
  58. #define MII_DP83640_MISR_ED_INT_EN 0x40
  59. #define MII_DP83640_MISR_LQ_INT_EN 0x80
  60. /* phyter seems to miss the mark by 16 ns */
  61. #define ADJTIME_FIX 16
  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 work_struct 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 timespec *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 + 2;
  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 timespec ts;
  346. int err;
  347. delta += ADJTIME_FIX;
  348. ts = ns_to_timespec(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, struct timespec *ts)
  355. {
  356. struct dp83640_clock *clock =
  357. container_of(ptp, struct dp83640_clock, caps);
  358. struct phy_device *phydev = clock->chosen->phydev;
  359. unsigned int val[4];
  360. mutex_lock(&clock->extreg_lock);
  361. ext_write(0, phydev, PAGE4, PTP_CTL, PTP_RD_CLK);
  362. val[0] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[15:0] */
  363. val[1] = ext_read(phydev, PAGE4, PTP_TDR); /* ns[31:16] */
  364. val[2] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[15:0] */
  365. val[3] = ext_read(phydev, PAGE4, PTP_TDR); /* sec[31:16] */
  366. mutex_unlock(&clock->extreg_lock);
  367. ts->tv_nsec = val[0] | (val[1] << 16);
  368. ts->tv_sec = val[2] | (val[3] << 16);
  369. return 0;
  370. }
  371. static int ptp_dp83640_settime(struct ptp_clock_info *ptp,
  372. const struct timespec *ts)
  373. {
  374. struct dp83640_clock *clock =
  375. container_of(ptp, struct dp83640_clock, caps);
  376. struct phy_device *phydev = clock->chosen->phydev;
  377. int err;
  378. mutex_lock(&clock->extreg_lock);
  379. err = tdr_write(1, phydev, ts, PTP_LOAD_CLK);
  380. mutex_unlock(&clock->extreg_lock);
  381. return err;
  382. }
  383. static int ptp_dp83640_enable(struct ptp_clock_info *ptp,
  384. struct ptp_clock_request *rq, int on)
  385. {
  386. struct dp83640_clock *clock =
  387. container_of(ptp, struct dp83640_clock, caps);
  388. struct phy_device *phydev = clock->chosen->phydev;
  389. unsigned int index;
  390. u16 evnt, event_num, gpio_num;
  391. switch (rq->type) {
  392. case PTP_CLK_REQ_EXTTS:
  393. index = rq->extts.index;
  394. if (index >= N_EXT_TS)
  395. return -EINVAL;
  396. event_num = EXT_EVENT + index;
  397. evnt = EVNT_WR | (event_num & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
  398. if (on) {
  399. gpio_num = 1 + ptp_find_pin(clock->ptp_clock,
  400. PTP_PF_EXTTS, index);
  401. if (gpio_num < 1)
  402. return -EINVAL;
  403. evnt |= (gpio_num & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
  404. if (rq->extts.flags & PTP_FALLING_EDGE)
  405. evnt |= EVNT_FALL;
  406. else
  407. evnt |= EVNT_RISE;
  408. }
  409. ext_write(0, phydev, PAGE5, PTP_EVNT, evnt);
  410. return 0;
  411. case PTP_CLK_REQ_PEROUT:
  412. if (rq->perout.index >= N_PER_OUT)
  413. return -EINVAL;
  414. return periodic_output(clock, rq, on, rq->perout.index);
  415. default:
  416. break;
  417. }
  418. return -EOPNOTSUPP;
  419. }
  420. static int ptp_dp83640_verify(struct ptp_clock_info *ptp, unsigned int pin,
  421. enum ptp_pin_function func, unsigned int chan)
  422. {
  423. struct dp83640_clock *clock =
  424. container_of(ptp, struct dp83640_clock, caps);
  425. if (clock->caps.pin_config[pin].func == PTP_PF_PHYSYNC &&
  426. !list_empty(&clock->phylist))
  427. return 1;
  428. if (func == PTP_PF_PHYSYNC)
  429. return 1;
  430. return 0;
  431. }
  432. static u8 status_frame_dst[6] = { 0x01, 0x1B, 0x19, 0x00, 0x00, 0x00 };
  433. static u8 status_frame_src[6] = { 0x08, 0x00, 0x17, 0x0B, 0x6B, 0x0F };
  434. static void enable_status_frames(struct phy_device *phydev, bool on)
  435. {
  436. u16 cfg0 = 0, ver;
  437. if (on)
  438. cfg0 = PSF_EVNT_EN | PSF_RXTS_EN | PSF_TXTS_EN | ENDIAN_FLAG;
  439. ver = (PSF_PTPVER & VERSIONPTP_MASK) << VERSIONPTP_SHIFT;
  440. ext_write(0, phydev, PAGE5, PSF_CFG0, cfg0);
  441. ext_write(0, phydev, PAGE6, PSF_CFG1, ver);
  442. if (!phydev->attached_dev) {
  443. pr_warn("expected to find an attached netdevice\n");
  444. return;
  445. }
  446. if (on) {
  447. if (dev_mc_add(phydev->attached_dev, status_frame_dst))
  448. pr_warn("failed to add mc address\n");
  449. } else {
  450. if (dev_mc_del(phydev->attached_dev, status_frame_dst))
  451. pr_warn("failed to delete mc address\n");
  452. }
  453. }
  454. static bool is_status_frame(struct sk_buff *skb, int type)
  455. {
  456. struct ethhdr *h = eth_hdr(skb);
  457. if (PTP_CLASS_V2_L2 == type &&
  458. !memcmp(h->h_source, status_frame_src, sizeof(status_frame_src)))
  459. return true;
  460. else
  461. return false;
  462. }
  463. static int expired(struct rxts *rxts)
  464. {
  465. return time_after(jiffies, rxts->tmo);
  466. }
  467. /* Caller must hold rx_lock. */
  468. static void prune_rx_ts(struct dp83640_private *dp83640)
  469. {
  470. struct list_head *this, *next;
  471. struct rxts *rxts;
  472. list_for_each_safe(this, next, &dp83640->rxts) {
  473. rxts = list_entry(this, struct rxts, list);
  474. if (expired(rxts)) {
  475. list_del_init(&rxts->list);
  476. list_add(&rxts->list, &dp83640->rxpool);
  477. }
  478. }
  479. }
  480. /* synchronize the phyters so they act as one clock */
  481. static void enable_broadcast(struct phy_device *phydev, int init_page, int on)
  482. {
  483. int val;
  484. phy_write(phydev, PAGESEL, 0);
  485. val = phy_read(phydev, PHYCR2);
  486. if (on)
  487. val |= BC_WRITE;
  488. else
  489. val &= ~BC_WRITE;
  490. phy_write(phydev, PHYCR2, val);
  491. phy_write(phydev, PAGESEL, init_page);
  492. }
  493. static void recalibrate(struct dp83640_clock *clock)
  494. {
  495. s64 now, diff;
  496. struct phy_txts event_ts;
  497. struct timespec ts;
  498. struct list_head *this;
  499. struct dp83640_private *tmp;
  500. struct phy_device *master = clock->chosen->phydev;
  501. u16 cal_gpio, cfg0, evnt, ptp_trig, trigger, val;
  502. trigger = CAL_TRIGGER;
  503. cal_gpio = 1 + ptp_find_pin(clock->ptp_clock, PTP_PF_PHYSYNC, 0);
  504. if (cal_gpio < 1) {
  505. pr_err("PHY calibration pin not avaible - PHY is not calibrated.");
  506. return;
  507. }
  508. mutex_lock(&clock->extreg_lock);
  509. /*
  510. * enable broadcast, disable status frames, enable ptp clock
  511. */
  512. list_for_each(this, &clock->phylist) {
  513. tmp = list_entry(this, struct dp83640_private, list);
  514. enable_broadcast(tmp->phydev, clock->page, 1);
  515. tmp->cfg0 = ext_read(tmp->phydev, PAGE5, PSF_CFG0);
  516. ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, 0);
  517. ext_write(0, tmp->phydev, PAGE4, PTP_CTL, PTP_ENABLE);
  518. }
  519. enable_broadcast(master, clock->page, 1);
  520. cfg0 = ext_read(master, PAGE5, PSF_CFG0);
  521. ext_write(0, master, PAGE5, PSF_CFG0, 0);
  522. ext_write(0, master, PAGE4, PTP_CTL, PTP_ENABLE);
  523. /*
  524. * enable an event timestamp
  525. */
  526. evnt = EVNT_WR | EVNT_RISE | EVNT_SINGLE;
  527. evnt |= (CAL_EVENT & EVNT_SEL_MASK) << EVNT_SEL_SHIFT;
  528. evnt |= (cal_gpio & EVNT_GPIO_MASK) << EVNT_GPIO_SHIFT;
  529. list_for_each(this, &clock->phylist) {
  530. tmp = list_entry(this, struct dp83640_private, list);
  531. ext_write(0, tmp->phydev, PAGE5, PTP_EVNT, evnt);
  532. }
  533. ext_write(0, master, PAGE5, PTP_EVNT, evnt);
  534. /*
  535. * configure a trigger
  536. */
  537. ptp_trig = TRIG_WR | TRIG_IF_LATE | TRIG_PULSE;
  538. ptp_trig |= (trigger & TRIG_CSEL_MASK) << TRIG_CSEL_SHIFT;
  539. ptp_trig |= (cal_gpio & TRIG_GPIO_MASK) << TRIG_GPIO_SHIFT;
  540. ext_write(0, master, PAGE5, PTP_TRIG, ptp_trig);
  541. /* load trigger */
  542. val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
  543. val |= TRIG_LOAD;
  544. ext_write(0, master, PAGE4, PTP_CTL, val);
  545. /* enable trigger */
  546. val &= ~TRIG_LOAD;
  547. val |= TRIG_EN;
  548. ext_write(0, master, PAGE4, PTP_CTL, val);
  549. /* disable trigger */
  550. val = (trigger & TRIG_SEL_MASK) << TRIG_SEL_SHIFT;
  551. val |= TRIG_DIS;
  552. ext_write(0, master, PAGE4, PTP_CTL, val);
  553. /*
  554. * read out and correct offsets
  555. */
  556. val = ext_read(master, PAGE4, PTP_STS);
  557. pr_info("master PTP_STS 0x%04hx\n", val);
  558. val = ext_read(master, PAGE4, PTP_ESTS);
  559. pr_info("master PTP_ESTS 0x%04hx\n", val);
  560. event_ts.ns_lo = ext_read(master, PAGE4, PTP_EDATA);
  561. event_ts.ns_hi = ext_read(master, PAGE4, PTP_EDATA);
  562. event_ts.sec_lo = ext_read(master, PAGE4, PTP_EDATA);
  563. event_ts.sec_hi = ext_read(master, PAGE4, PTP_EDATA);
  564. now = phy2txts(&event_ts);
  565. list_for_each(this, &clock->phylist) {
  566. tmp = list_entry(this, struct dp83640_private, list);
  567. val = ext_read(tmp->phydev, PAGE4, PTP_STS);
  568. pr_info("slave PTP_STS 0x%04hx\n", val);
  569. val = ext_read(tmp->phydev, PAGE4, PTP_ESTS);
  570. pr_info("slave PTP_ESTS 0x%04hx\n", val);
  571. event_ts.ns_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
  572. event_ts.ns_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
  573. event_ts.sec_lo = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
  574. event_ts.sec_hi = ext_read(tmp->phydev, PAGE4, PTP_EDATA);
  575. diff = now - (s64) phy2txts(&event_ts);
  576. pr_info("slave offset %lld nanoseconds\n", diff);
  577. diff += ADJTIME_FIX;
  578. ts = ns_to_timespec(diff);
  579. tdr_write(0, tmp->phydev, &ts, PTP_STEP_CLK);
  580. }
  581. /*
  582. * restore status frames
  583. */
  584. list_for_each(this, &clock->phylist) {
  585. tmp = list_entry(this, struct dp83640_private, list);
  586. ext_write(0, tmp->phydev, PAGE5, PSF_CFG0, tmp->cfg0);
  587. }
  588. ext_write(0, master, PAGE5, PSF_CFG0, cfg0);
  589. mutex_unlock(&clock->extreg_lock);
  590. }
  591. /* time stamping methods */
  592. static inline u16 exts_chan_to_edata(int ch)
  593. {
  594. return 1 << ((ch + EXT_EVENT) * 2);
  595. }
  596. static int decode_evnt(struct dp83640_private *dp83640,
  597. void *data, u16 ests)
  598. {
  599. struct phy_txts *phy_txts;
  600. struct ptp_clock_event event;
  601. int i, parsed;
  602. int words = (ests >> EVNT_TS_LEN_SHIFT) & EVNT_TS_LEN_MASK;
  603. u16 ext_status = 0;
  604. if (ests & MULT_EVNT) {
  605. ext_status = *(u16 *) data;
  606. data += sizeof(ext_status);
  607. }
  608. phy_txts = data;
  609. switch (words) { /* fall through in every case */
  610. case 3:
  611. dp83640->edata.sec_hi = phy_txts->sec_hi;
  612. case 2:
  613. dp83640->edata.sec_lo = phy_txts->sec_lo;
  614. case 1:
  615. dp83640->edata.ns_hi = phy_txts->ns_hi;
  616. case 0:
  617. dp83640->edata.ns_lo = phy_txts->ns_lo;
  618. }
  619. if (ext_status) {
  620. parsed = words + 2;
  621. } else {
  622. parsed = words + 1;
  623. i = ((ests >> EVNT_NUM_SHIFT) & EVNT_NUM_MASK) - EXT_EVENT;
  624. ext_status = exts_chan_to_edata(i);
  625. }
  626. event.type = PTP_CLOCK_EXTTS;
  627. event.timestamp = phy2txts(&dp83640->edata);
  628. /* Compensate for input path and synchronization delays */
  629. event.timestamp -= 35;
  630. for (i = 0; i < N_EXT_TS; i++) {
  631. if (ext_status & exts_chan_to_edata(i)) {
  632. event.index = i;
  633. ptp_clock_event(dp83640->clock->ptp_clock, &event);
  634. }
  635. }
  636. return parsed * sizeof(u16);
  637. }
  638. static int match(struct sk_buff *skb, unsigned int type, struct rxts *rxts)
  639. {
  640. u16 *seqid;
  641. unsigned int offset = 0;
  642. u8 *msgtype, *data = skb_mac_header(skb);
  643. /* check sequenceID, messageType, 12 bit hash of offset 20-29 */
  644. if (type & PTP_CLASS_VLAN)
  645. offset += VLAN_HLEN;
  646. switch (type & PTP_CLASS_PMASK) {
  647. case PTP_CLASS_IPV4:
  648. offset += ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
  649. break;
  650. case PTP_CLASS_IPV6:
  651. offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
  652. break;
  653. case PTP_CLASS_L2:
  654. offset += ETH_HLEN;
  655. break;
  656. default:
  657. return 0;
  658. }
  659. if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
  660. return 0;
  661. if (unlikely(type & PTP_CLASS_V1))
  662. msgtype = data + offset + OFF_PTP_CONTROL;
  663. else
  664. msgtype = data + offset;
  665. seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
  666. return rxts->msgtype == (*msgtype & 0xf) &&
  667. rxts->seqid == ntohs(*seqid);
  668. }
  669. static void decode_rxts(struct dp83640_private *dp83640,
  670. struct phy_rxts *phy_rxts)
  671. {
  672. struct rxts *rxts;
  673. struct skb_shared_hwtstamps *shhwtstamps = NULL;
  674. struct sk_buff *skb;
  675. unsigned long flags;
  676. spin_lock_irqsave(&dp83640->rx_lock, flags);
  677. prune_rx_ts(dp83640);
  678. if (list_empty(&dp83640->rxpool)) {
  679. pr_debug("rx timestamp pool is empty\n");
  680. goto out;
  681. }
  682. rxts = list_first_entry(&dp83640->rxpool, struct rxts, list);
  683. list_del_init(&rxts->list);
  684. phy2rxts(phy_rxts, rxts);
  685. spin_lock_irqsave(&dp83640->rx_queue.lock, flags);
  686. skb_queue_walk(&dp83640->rx_queue, skb) {
  687. struct dp83640_skb_info *skb_info;
  688. skb_info = (struct dp83640_skb_info *)skb->cb;
  689. if (match(skb, skb_info->ptp_type, rxts)) {
  690. __skb_unlink(skb, &dp83640->rx_queue);
  691. shhwtstamps = skb_hwtstamps(skb);
  692. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  693. shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
  694. netif_rx_ni(skb);
  695. list_add(&rxts->list, &dp83640->rxpool);
  696. break;
  697. }
  698. }
  699. spin_unlock_irqrestore(&dp83640->rx_queue.lock, flags);
  700. if (!shhwtstamps)
  701. list_add_tail(&rxts->list, &dp83640->rxts);
  702. out:
  703. spin_unlock_irqrestore(&dp83640->rx_lock, flags);
  704. }
  705. static void decode_txts(struct dp83640_private *dp83640,
  706. struct phy_txts *phy_txts)
  707. {
  708. struct skb_shared_hwtstamps shhwtstamps;
  709. struct sk_buff *skb;
  710. u64 ns;
  711. /* We must already have the skb that triggered this. */
  712. skb = skb_dequeue(&dp83640->tx_queue);
  713. if (!skb) {
  714. pr_debug("have timestamp but tx_queue empty\n");
  715. return;
  716. }
  717. ns = phy2txts(phy_txts);
  718. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  719. shhwtstamps.hwtstamp = ns_to_ktime(ns);
  720. skb_complete_tx_timestamp(skb, &shhwtstamps);
  721. }
  722. static void decode_status_frame(struct dp83640_private *dp83640,
  723. struct sk_buff *skb)
  724. {
  725. struct phy_rxts *phy_rxts;
  726. struct phy_txts *phy_txts;
  727. u8 *ptr;
  728. int len, size;
  729. u16 ests, type;
  730. ptr = skb->data + 2;
  731. for (len = skb_headlen(skb) - 2; len > sizeof(type); len -= size) {
  732. type = *(u16 *)ptr;
  733. ests = type & 0x0fff;
  734. type = type & 0xf000;
  735. len -= sizeof(type);
  736. ptr += sizeof(type);
  737. if (PSF_RX == type && len >= sizeof(*phy_rxts)) {
  738. phy_rxts = (struct phy_rxts *) ptr;
  739. decode_rxts(dp83640, phy_rxts);
  740. size = sizeof(*phy_rxts);
  741. } else if (PSF_TX == type && len >= sizeof(*phy_txts)) {
  742. phy_txts = (struct phy_txts *) ptr;
  743. decode_txts(dp83640, phy_txts);
  744. size = sizeof(*phy_txts);
  745. } else if (PSF_EVNT == type && len >= sizeof(*phy_txts)) {
  746. size = decode_evnt(dp83640, ptr, ests);
  747. } else {
  748. size = 0;
  749. break;
  750. }
  751. ptr += size;
  752. }
  753. }
  754. static int is_sync(struct sk_buff *skb, int type)
  755. {
  756. u8 *data = skb->data, *msgtype;
  757. unsigned int offset = 0;
  758. if (type & PTP_CLASS_VLAN)
  759. offset += VLAN_HLEN;
  760. switch (type & PTP_CLASS_PMASK) {
  761. case PTP_CLASS_IPV4:
  762. offset += ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
  763. break;
  764. case PTP_CLASS_IPV6:
  765. offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
  766. break;
  767. case PTP_CLASS_L2:
  768. offset += ETH_HLEN;
  769. break;
  770. default:
  771. return 0;
  772. }
  773. if (type & PTP_CLASS_V1)
  774. offset += OFF_PTP_CONTROL;
  775. if (skb->len < offset + 1)
  776. return 0;
  777. msgtype = data + offset;
  778. return (*msgtype & 0xf) == 0;
  779. }
  780. static void dp83640_free_clocks(void)
  781. {
  782. struct dp83640_clock *clock;
  783. struct list_head *this, *next;
  784. mutex_lock(&phyter_clocks_lock);
  785. list_for_each_safe(this, next, &phyter_clocks) {
  786. clock = list_entry(this, struct dp83640_clock, list);
  787. if (!list_empty(&clock->phylist)) {
  788. pr_warn("phy list non-empty while unloading\n");
  789. BUG();
  790. }
  791. list_del(&clock->list);
  792. mutex_destroy(&clock->extreg_lock);
  793. mutex_destroy(&clock->clock_lock);
  794. put_device(&clock->bus->dev);
  795. kfree(clock->caps.pin_config);
  796. kfree(clock);
  797. }
  798. mutex_unlock(&phyter_clocks_lock);
  799. }
  800. static void dp83640_clock_init(struct dp83640_clock *clock, struct mii_bus *bus)
  801. {
  802. INIT_LIST_HEAD(&clock->list);
  803. clock->bus = bus;
  804. mutex_init(&clock->extreg_lock);
  805. mutex_init(&clock->clock_lock);
  806. INIT_LIST_HEAD(&clock->phylist);
  807. clock->caps.owner = THIS_MODULE;
  808. sprintf(clock->caps.name, "dp83640 timer");
  809. clock->caps.max_adj = 1953124;
  810. clock->caps.n_alarm = 0;
  811. clock->caps.n_ext_ts = N_EXT_TS;
  812. clock->caps.n_per_out = N_PER_OUT;
  813. clock->caps.n_pins = DP83640_N_PINS;
  814. clock->caps.pps = 0;
  815. clock->caps.adjfreq = ptp_dp83640_adjfreq;
  816. clock->caps.adjtime = ptp_dp83640_adjtime;
  817. clock->caps.gettime = ptp_dp83640_gettime;
  818. clock->caps.settime = ptp_dp83640_settime;
  819. clock->caps.enable = ptp_dp83640_enable;
  820. clock->caps.verify = ptp_dp83640_verify;
  821. /*
  822. * Convert the module param defaults into a dynamic pin configuration.
  823. */
  824. dp83640_gpio_defaults(clock->caps.pin_config);
  825. /*
  826. * Get a reference to this bus instance.
  827. */
  828. get_device(&bus->dev);
  829. }
  830. static int choose_this_phy(struct dp83640_clock *clock,
  831. struct phy_device *phydev)
  832. {
  833. if (chosen_phy == -1 && !clock->chosen)
  834. return 1;
  835. if (chosen_phy == phydev->addr)
  836. return 1;
  837. return 0;
  838. }
  839. static struct dp83640_clock *dp83640_clock_get(struct dp83640_clock *clock)
  840. {
  841. if (clock)
  842. mutex_lock(&clock->clock_lock);
  843. return clock;
  844. }
  845. /*
  846. * Look up and lock a clock by bus instance.
  847. * If there is no clock for this bus, then create it first.
  848. */
  849. static struct dp83640_clock *dp83640_clock_get_bus(struct mii_bus *bus)
  850. {
  851. struct dp83640_clock *clock = NULL, *tmp;
  852. struct list_head *this;
  853. mutex_lock(&phyter_clocks_lock);
  854. list_for_each(this, &phyter_clocks) {
  855. tmp = list_entry(this, struct dp83640_clock, list);
  856. if (tmp->bus == bus) {
  857. clock = tmp;
  858. break;
  859. }
  860. }
  861. if (clock)
  862. goto out;
  863. clock = kzalloc(sizeof(struct dp83640_clock), GFP_KERNEL);
  864. if (!clock)
  865. goto out;
  866. clock->caps.pin_config = kzalloc(sizeof(struct ptp_pin_desc) *
  867. DP83640_N_PINS, GFP_KERNEL);
  868. if (!clock->caps.pin_config) {
  869. kfree(clock);
  870. clock = NULL;
  871. goto out;
  872. }
  873. dp83640_clock_init(clock, bus);
  874. list_add_tail(&phyter_clocks, &clock->list);
  875. out:
  876. mutex_unlock(&phyter_clocks_lock);
  877. return dp83640_clock_get(clock);
  878. }
  879. static void dp83640_clock_put(struct dp83640_clock *clock)
  880. {
  881. mutex_unlock(&clock->clock_lock);
  882. }
  883. static int dp83640_probe(struct phy_device *phydev)
  884. {
  885. struct dp83640_clock *clock;
  886. struct dp83640_private *dp83640;
  887. int err = -ENOMEM, i;
  888. if (phydev->addr == BROADCAST_ADDR)
  889. return 0;
  890. clock = dp83640_clock_get_bus(phydev->bus);
  891. if (!clock)
  892. goto no_clock;
  893. dp83640 = kzalloc(sizeof(struct dp83640_private), GFP_KERNEL);
  894. if (!dp83640)
  895. goto no_memory;
  896. dp83640->phydev = phydev;
  897. INIT_WORK(&dp83640->ts_work, rx_timestamp_work);
  898. INIT_LIST_HEAD(&dp83640->rxts);
  899. INIT_LIST_HEAD(&dp83640->rxpool);
  900. for (i = 0; i < MAX_RXTS; i++)
  901. list_add(&dp83640->rx_pool_data[i].list, &dp83640->rxpool);
  902. phydev->priv = dp83640;
  903. spin_lock_init(&dp83640->rx_lock);
  904. skb_queue_head_init(&dp83640->rx_queue);
  905. skb_queue_head_init(&dp83640->tx_queue);
  906. dp83640->clock = clock;
  907. if (choose_this_phy(clock, phydev)) {
  908. clock->chosen = dp83640;
  909. clock->ptp_clock = ptp_clock_register(&clock->caps, &phydev->dev);
  910. if (IS_ERR(clock->ptp_clock)) {
  911. err = PTR_ERR(clock->ptp_clock);
  912. goto no_register;
  913. }
  914. } else
  915. list_add_tail(&dp83640->list, &clock->phylist);
  916. dp83640_clock_put(clock);
  917. return 0;
  918. no_register:
  919. clock->chosen = NULL;
  920. kfree(dp83640);
  921. no_memory:
  922. dp83640_clock_put(clock);
  923. no_clock:
  924. return err;
  925. }
  926. static void dp83640_remove(struct phy_device *phydev)
  927. {
  928. struct dp83640_clock *clock;
  929. struct list_head *this, *next;
  930. struct dp83640_private *tmp, *dp83640 = phydev->priv;
  931. struct sk_buff *skb;
  932. if (phydev->addr == BROADCAST_ADDR)
  933. return;
  934. enable_status_frames(phydev, false);
  935. cancel_work_sync(&dp83640->ts_work);
  936. while ((skb = skb_dequeue(&dp83640->rx_queue)) != NULL)
  937. kfree_skb(skb);
  938. while ((skb = skb_dequeue(&dp83640->tx_queue)) != NULL)
  939. skb_complete_tx_timestamp(skb, NULL);
  940. clock = dp83640_clock_get(dp83640->clock);
  941. if (dp83640 == clock->chosen) {
  942. ptp_clock_unregister(clock->ptp_clock);
  943. clock->chosen = NULL;
  944. } else {
  945. list_for_each_safe(this, next, &clock->phylist) {
  946. tmp = list_entry(this, struct dp83640_private, list);
  947. if (tmp == dp83640) {
  948. list_del_init(&tmp->list);
  949. break;
  950. }
  951. }
  952. }
  953. dp83640_clock_put(clock);
  954. kfree(dp83640);
  955. }
  956. static int dp83640_config_init(struct phy_device *phydev)
  957. {
  958. struct dp83640_private *dp83640 = phydev->priv;
  959. struct dp83640_clock *clock = dp83640->clock;
  960. if (clock->chosen && !list_empty(&clock->phylist))
  961. recalibrate(clock);
  962. else
  963. enable_broadcast(phydev, clock->page, 1);
  964. enable_status_frames(phydev, true);
  965. ext_write(0, phydev, PAGE4, PTP_CTL, PTP_ENABLE);
  966. return 0;
  967. }
  968. static int dp83640_ack_interrupt(struct phy_device *phydev)
  969. {
  970. int err = phy_read(phydev, MII_DP83640_MISR);
  971. if (err < 0)
  972. return err;
  973. return 0;
  974. }
  975. static int dp83640_config_intr(struct phy_device *phydev)
  976. {
  977. int micr;
  978. int misr;
  979. int err;
  980. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  981. misr = phy_read(phydev, MII_DP83640_MISR);
  982. if (misr < 0)
  983. return misr;
  984. misr |=
  985. (MII_DP83640_MISR_ANC_INT_EN |
  986. MII_DP83640_MISR_DUP_INT_EN |
  987. MII_DP83640_MISR_SPD_INT_EN |
  988. MII_DP83640_MISR_LINK_INT_EN);
  989. err = phy_write(phydev, MII_DP83640_MISR, misr);
  990. if (err < 0)
  991. return err;
  992. micr = phy_read(phydev, MII_DP83640_MICR);
  993. if (micr < 0)
  994. return micr;
  995. micr |=
  996. (MII_DP83640_MICR_OE |
  997. MII_DP83640_MICR_IE);
  998. return phy_write(phydev, MII_DP83640_MICR, micr);
  999. } else {
  1000. micr = phy_read(phydev, MII_DP83640_MICR);
  1001. if (micr < 0)
  1002. return micr;
  1003. micr &=
  1004. ~(MII_DP83640_MICR_OE |
  1005. MII_DP83640_MICR_IE);
  1006. err = phy_write(phydev, MII_DP83640_MICR, micr);
  1007. if (err < 0)
  1008. return err;
  1009. misr = phy_read(phydev, MII_DP83640_MISR);
  1010. if (misr < 0)
  1011. return misr;
  1012. misr &=
  1013. ~(MII_DP83640_MISR_ANC_INT_EN |
  1014. MII_DP83640_MISR_DUP_INT_EN |
  1015. MII_DP83640_MISR_SPD_INT_EN |
  1016. MII_DP83640_MISR_LINK_INT_EN);
  1017. return phy_write(phydev, MII_DP83640_MISR, misr);
  1018. }
  1019. }
  1020. static int dp83640_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
  1021. {
  1022. struct dp83640_private *dp83640 = phydev->priv;
  1023. struct hwtstamp_config cfg;
  1024. u16 txcfg0, rxcfg0;
  1025. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  1026. return -EFAULT;
  1027. if (cfg.flags) /* reserved for future extensions */
  1028. return -EINVAL;
  1029. if (cfg.tx_type < 0 || cfg.tx_type > HWTSTAMP_TX_ONESTEP_SYNC)
  1030. return -ERANGE;
  1031. dp83640->hwts_tx_en = cfg.tx_type;
  1032. switch (cfg.rx_filter) {
  1033. case HWTSTAMP_FILTER_NONE:
  1034. dp83640->hwts_rx_en = 0;
  1035. dp83640->layer = 0;
  1036. dp83640->version = 0;
  1037. break;
  1038. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1039. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1040. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1041. dp83640->hwts_rx_en = 1;
  1042. dp83640->layer = LAYER4;
  1043. dp83640->version = 1;
  1044. break;
  1045. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1046. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1047. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1048. dp83640->hwts_rx_en = 1;
  1049. dp83640->layer = LAYER4;
  1050. dp83640->version = 2;
  1051. break;
  1052. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1053. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1054. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1055. dp83640->hwts_rx_en = 1;
  1056. dp83640->layer = LAYER2;
  1057. dp83640->version = 2;
  1058. break;
  1059. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1060. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1061. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1062. dp83640->hwts_rx_en = 1;
  1063. dp83640->layer = LAYER4|LAYER2;
  1064. dp83640->version = 2;
  1065. break;
  1066. default:
  1067. return -ERANGE;
  1068. }
  1069. txcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
  1070. rxcfg0 = (dp83640->version & TX_PTP_VER_MASK) << TX_PTP_VER_SHIFT;
  1071. if (dp83640->layer & LAYER2) {
  1072. txcfg0 |= TX_L2_EN;
  1073. rxcfg0 |= RX_L2_EN;
  1074. }
  1075. if (dp83640->layer & LAYER4) {
  1076. txcfg0 |= TX_IPV6_EN | TX_IPV4_EN;
  1077. rxcfg0 |= RX_IPV6_EN | RX_IPV4_EN;
  1078. }
  1079. if (dp83640->hwts_tx_en)
  1080. txcfg0 |= TX_TS_EN;
  1081. if (dp83640->hwts_tx_en == HWTSTAMP_TX_ONESTEP_SYNC)
  1082. txcfg0 |= SYNC_1STEP | CHK_1STEP;
  1083. if (dp83640->hwts_rx_en)
  1084. rxcfg0 |= RX_TS_EN;
  1085. mutex_lock(&dp83640->clock->extreg_lock);
  1086. ext_write(0, phydev, PAGE5, PTP_TXCFG0, txcfg0);
  1087. ext_write(0, phydev, PAGE5, PTP_RXCFG0, rxcfg0);
  1088. mutex_unlock(&dp83640->clock->extreg_lock);
  1089. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1090. }
  1091. static void rx_timestamp_work(struct work_struct *work)
  1092. {
  1093. struct dp83640_private *dp83640 =
  1094. container_of(work, struct dp83640_private, ts_work);
  1095. struct sk_buff *skb;
  1096. /* Deliver expired packets. */
  1097. while ((skb = skb_dequeue(&dp83640->rx_queue))) {
  1098. struct dp83640_skb_info *skb_info;
  1099. skb_info = (struct dp83640_skb_info *)skb->cb;
  1100. if (!time_after(jiffies, skb_info->tmo)) {
  1101. skb_queue_head(&dp83640->rx_queue, skb);
  1102. break;
  1103. }
  1104. netif_rx_ni(skb);
  1105. }
  1106. if (!skb_queue_empty(&dp83640->rx_queue))
  1107. schedule_work(&dp83640->ts_work);
  1108. }
  1109. static bool dp83640_rxtstamp(struct phy_device *phydev,
  1110. struct sk_buff *skb, int type)
  1111. {
  1112. struct dp83640_private *dp83640 = phydev->priv;
  1113. struct dp83640_skb_info *skb_info = (struct dp83640_skb_info *)skb->cb;
  1114. struct list_head *this, *next;
  1115. struct rxts *rxts;
  1116. struct skb_shared_hwtstamps *shhwtstamps = NULL;
  1117. unsigned long flags;
  1118. if (is_status_frame(skb, type)) {
  1119. decode_status_frame(dp83640, skb);
  1120. kfree_skb(skb);
  1121. return true;
  1122. }
  1123. if (!dp83640->hwts_rx_en)
  1124. return false;
  1125. spin_lock_irqsave(&dp83640->rx_lock, flags);
  1126. list_for_each_safe(this, next, &dp83640->rxts) {
  1127. rxts = list_entry(this, struct rxts, list);
  1128. if (match(skb, type, rxts)) {
  1129. shhwtstamps = skb_hwtstamps(skb);
  1130. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  1131. shhwtstamps->hwtstamp = ns_to_ktime(rxts->ns);
  1132. netif_rx_ni(skb);
  1133. list_del_init(&rxts->list);
  1134. list_add(&rxts->list, &dp83640->rxpool);
  1135. break;
  1136. }
  1137. }
  1138. spin_unlock_irqrestore(&dp83640->rx_lock, flags);
  1139. if (!shhwtstamps) {
  1140. skb_info->ptp_type = type;
  1141. skb_info->tmo = jiffies + 2;
  1142. skb_queue_tail(&dp83640->rx_queue, skb);
  1143. schedule_work(&dp83640->ts_work);
  1144. }
  1145. return true;
  1146. }
  1147. static void dp83640_txtstamp(struct phy_device *phydev,
  1148. struct sk_buff *skb, int type)
  1149. {
  1150. struct dp83640_private *dp83640 = phydev->priv;
  1151. switch (dp83640->hwts_tx_en) {
  1152. case HWTSTAMP_TX_ONESTEP_SYNC:
  1153. if (is_sync(skb, type)) {
  1154. skb_complete_tx_timestamp(skb, NULL);
  1155. return;
  1156. }
  1157. /* fall through */
  1158. case HWTSTAMP_TX_ON:
  1159. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1160. skb_queue_tail(&dp83640->tx_queue, skb);
  1161. break;
  1162. case HWTSTAMP_TX_OFF:
  1163. default:
  1164. skb_complete_tx_timestamp(skb, NULL);
  1165. break;
  1166. }
  1167. }
  1168. static int dp83640_ts_info(struct phy_device *dev, struct ethtool_ts_info *info)
  1169. {
  1170. struct dp83640_private *dp83640 = dev->priv;
  1171. info->so_timestamping =
  1172. SOF_TIMESTAMPING_TX_HARDWARE |
  1173. SOF_TIMESTAMPING_RX_HARDWARE |
  1174. SOF_TIMESTAMPING_RAW_HARDWARE;
  1175. info->phc_index = ptp_clock_index(dp83640->clock->ptp_clock);
  1176. info->tx_types =
  1177. (1 << HWTSTAMP_TX_OFF) |
  1178. (1 << HWTSTAMP_TX_ON) |
  1179. (1 << HWTSTAMP_TX_ONESTEP_SYNC);
  1180. info->rx_filters =
  1181. (1 << HWTSTAMP_FILTER_NONE) |
  1182. (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
  1183. (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
  1184. (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
  1185. (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
  1186. (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
  1187. (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
  1188. (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
  1189. (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
  1190. (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
  1191. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
  1192. (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
  1193. (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ);
  1194. return 0;
  1195. }
  1196. static struct phy_driver dp83640_driver = {
  1197. .phy_id = DP83640_PHY_ID,
  1198. .phy_id_mask = 0xfffffff0,
  1199. .name = "NatSemi DP83640",
  1200. .features = PHY_BASIC_FEATURES,
  1201. .flags = PHY_HAS_INTERRUPT,
  1202. .probe = dp83640_probe,
  1203. .remove = dp83640_remove,
  1204. .config_init = dp83640_config_init,
  1205. .config_aneg = genphy_config_aneg,
  1206. .read_status = genphy_read_status,
  1207. .ack_interrupt = dp83640_ack_interrupt,
  1208. .config_intr = dp83640_config_intr,
  1209. .ts_info = dp83640_ts_info,
  1210. .hwtstamp = dp83640_hwtstamp,
  1211. .rxtstamp = dp83640_rxtstamp,
  1212. .txtstamp = dp83640_txtstamp,
  1213. .driver = {.owner = THIS_MODULE,}
  1214. };
  1215. static int __init dp83640_init(void)
  1216. {
  1217. return phy_driver_register(&dp83640_driver);
  1218. }
  1219. static void __exit dp83640_exit(void)
  1220. {
  1221. dp83640_free_clocks();
  1222. phy_driver_unregister(&dp83640_driver);
  1223. }
  1224. MODULE_DESCRIPTION("National Semiconductor DP83640 PHY driver");
  1225. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
  1226. MODULE_LICENSE("GPL");
  1227. module_init(dp83640_init);
  1228. module_exit(dp83640_exit);
  1229. static struct mdio_device_id __maybe_unused dp83640_tbl[] = {
  1230. { DP83640_PHY_ID, 0xfffffff0 },
  1231. { }
  1232. };
  1233. MODULE_DEVICE_TABLE(mdio, dp83640_tbl);