bcm-phy-ptp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2022 Meta Platforms Inc.
  4. * Copyright (C) 2022 Jonathan Lemon <[email protected]>
  5. */
  6. #include <asm/unaligned.h>
  7. #include <linux/mii.h>
  8. #include <linux/phy.h>
  9. #include <linux/ptp_classify.h>
  10. #include <linux/ptp_clock_kernel.h>
  11. #include <linux/net_tstamp.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/workqueue.h>
  14. #include "bcm-phy-lib.h"
  15. /* IEEE 1588 Expansion registers */
  16. #define SLICE_CTRL 0x0810
  17. #define SLICE_TX_EN BIT(0)
  18. #define SLICE_RX_EN BIT(8)
  19. #define TX_EVENT_MODE 0x0811
  20. #define MODE_TX_UPDATE_CF BIT(0)
  21. #define MODE_TX_REPLACE_TS_CF BIT(1)
  22. #define MODE_TX_REPLACE_TS GENMASK(1, 0)
  23. #define RX_EVENT_MODE 0x0819
  24. #define MODE_RX_UPDATE_CF BIT(0)
  25. #define MODE_RX_INSERT_TS_48 BIT(1)
  26. #define MODE_RX_INSERT_TS_64 GENMASK(1, 0)
  27. #define MODE_EVT_SHIFT_SYNC 0
  28. #define MODE_EVT_SHIFT_DELAY_REQ 2
  29. #define MODE_EVT_SHIFT_PDELAY_REQ 4
  30. #define MODE_EVT_SHIFT_PDELAY_RESP 6
  31. #define MODE_SEL_SHIFT_PORT 0
  32. #define MODE_SEL_SHIFT_CPU 8
  33. #define RX_MODE_SEL(sel, evt, act) \
  34. (((MODE_RX_##act) << (MODE_EVT_SHIFT_##evt)) << (MODE_SEL_SHIFT_##sel))
  35. #define TX_MODE_SEL(sel, evt, act) \
  36. (((MODE_TX_##act) << (MODE_EVT_SHIFT_##evt)) << (MODE_SEL_SHIFT_##sel))
  37. /* needs global TS capture first */
  38. #define TX_TS_CAPTURE 0x0821
  39. #define TX_TS_CAP_EN BIT(0)
  40. #define RX_TS_CAPTURE 0x0822
  41. #define RX_TS_CAP_EN BIT(0)
  42. #define TIME_CODE_0 0x0854
  43. #define TIME_CODE_1 0x0855
  44. #define TIME_CODE_2 0x0856
  45. #define TIME_CODE_3 0x0857
  46. #define TIME_CODE_4 0x0858
  47. #define DPLL_SELECT 0x085b
  48. #define DPLL_HB_MODE2 BIT(6)
  49. #define SHADOW_CTRL 0x085c
  50. #define SHADOW_LOAD 0x085d
  51. #define TIME_CODE_LOAD BIT(10)
  52. #define SYNC_OUT_LOAD BIT(9)
  53. #define NCO_TIME_LOAD BIT(7)
  54. #define FREQ_LOAD BIT(6)
  55. #define INTR_MASK 0x085e
  56. #define INTR_STATUS 0x085f
  57. #define INTC_FSYNC BIT(0)
  58. #define INTC_SOP BIT(1)
  59. #define NCO_FREQ_LSB 0x0873
  60. #define NCO_FREQ_MSB 0x0874
  61. #define NCO_TIME_0 0x0875
  62. #define NCO_TIME_1 0x0876
  63. #define NCO_TIME_2_CTRL 0x0877
  64. #define FREQ_MDIO_SEL BIT(14)
  65. #define SYNC_OUT_0 0x0878
  66. #define SYNC_OUT_1 0x0879
  67. #define SYNC_OUT_2 0x087a
  68. #define SYNC_IN_DIVIDER 0x087b
  69. #define SYNOUT_TS_0 0x087c
  70. #define SYNOUT_TS_1 0x087d
  71. #define SYNOUT_TS_2 0x087e
  72. #define NSE_CTRL 0x087f
  73. #define NSE_GMODE_EN GENMASK(15, 14)
  74. #define NSE_CAPTURE_EN BIT(13)
  75. #define NSE_INIT BIT(12)
  76. #define NSE_CPU_FRAMESYNC BIT(5)
  77. #define NSE_SYNC1_FRAMESYNC BIT(3)
  78. #define NSE_FRAMESYNC_MASK GENMASK(5, 2)
  79. #define NSE_PEROUT_EN BIT(1)
  80. #define NSE_ONESHOT_EN BIT(0)
  81. #define NSE_SYNC_OUT_MASK GENMASK(1, 0)
  82. #define TS_READ_CTRL 0x0885
  83. #define TS_READ_START BIT(0)
  84. #define TS_READ_END BIT(1)
  85. #define HB_REG_0 0x0886
  86. #define HB_REG_1 0x0887
  87. #define HB_REG_2 0x0888
  88. #define HB_REG_3 0x08ec
  89. #define HB_REG_4 0x08ed
  90. #define HB_STAT_CTRL 0x088e
  91. #define HB_READ_START BIT(10)
  92. #define HB_READ_END BIT(11)
  93. #define HB_READ_MASK GENMASK(11, 10)
  94. #define TS_REG_0 0x0889
  95. #define TS_REG_1 0x088a
  96. #define TS_REG_2 0x088b
  97. #define TS_REG_3 0x08c4
  98. #define TS_INFO_0 0x088c
  99. #define TS_INFO_1 0x088d
  100. #define TIMECODE_CTRL 0x08c3
  101. #define TX_TIMECODE_SEL GENMASK(7, 0)
  102. #define RX_TIMECODE_SEL GENMASK(15, 8)
  103. #define TIME_SYNC 0x0ff5
  104. #define TIME_SYNC_EN BIT(0)
  105. struct bcm_ptp_private {
  106. struct phy_device *phydev;
  107. struct mii_timestamper mii_ts;
  108. struct ptp_clock *ptp_clock;
  109. struct ptp_clock_info ptp_info;
  110. struct ptp_pin_desc pin;
  111. struct mutex mutex;
  112. struct sk_buff_head tx_queue;
  113. int tx_type;
  114. bool hwts_rx;
  115. u16 nse_ctrl;
  116. bool pin_active;
  117. struct delayed_work pin_work;
  118. };
  119. struct bcm_ptp_skb_cb {
  120. unsigned long timeout;
  121. u16 seq_id;
  122. u8 msgtype;
  123. bool discard;
  124. };
  125. struct bcm_ptp_capture {
  126. ktime_t hwtstamp;
  127. u16 seq_id;
  128. u8 msgtype;
  129. bool tx_dir;
  130. };
  131. #define BCM_SKB_CB(skb) ((struct bcm_ptp_skb_cb *)(skb)->cb)
  132. #define SKB_TS_TIMEOUT 10 /* jiffies */
  133. #define BCM_MAX_PULSE_8NS ((1U << 9) - 1)
  134. #define BCM_MAX_PERIOD_8NS ((1U << 30) - 1)
  135. #define BRCM_PHY_MODEL(phydev) \
  136. ((phydev)->drv->phy_id & (phydev)->drv->phy_id_mask)
  137. static struct bcm_ptp_private *mii2priv(struct mii_timestamper *mii_ts)
  138. {
  139. return container_of(mii_ts, struct bcm_ptp_private, mii_ts);
  140. }
  141. static struct bcm_ptp_private *ptp2priv(struct ptp_clock_info *info)
  142. {
  143. return container_of(info, struct bcm_ptp_private, ptp_info);
  144. }
  145. static void bcm_ptp_get_framesync_ts(struct phy_device *phydev,
  146. struct timespec64 *ts)
  147. {
  148. u16 hb[4];
  149. bcm_phy_write_exp(phydev, HB_STAT_CTRL, HB_READ_START);
  150. hb[0] = bcm_phy_read_exp(phydev, HB_REG_0);
  151. hb[1] = bcm_phy_read_exp(phydev, HB_REG_1);
  152. hb[2] = bcm_phy_read_exp(phydev, HB_REG_2);
  153. hb[3] = bcm_phy_read_exp(phydev, HB_REG_3);
  154. bcm_phy_write_exp(phydev, HB_STAT_CTRL, HB_READ_END);
  155. bcm_phy_write_exp(phydev, HB_STAT_CTRL, 0);
  156. ts->tv_sec = (hb[3] << 16) | hb[2];
  157. ts->tv_nsec = (hb[1] << 16) | hb[0];
  158. }
  159. static u16 bcm_ptp_framesync_disable(struct phy_device *phydev, u16 orig_ctrl)
  160. {
  161. u16 ctrl = orig_ctrl & ~(NSE_FRAMESYNC_MASK | NSE_CAPTURE_EN);
  162. bcm_phy_write_exp(phydev, NSE_CTRL, ctrl);
  163. return ctrl;
  164. }
  165. static void bcm_ptp_framesync_restore(struct phy_device *phydev, u16 orig_ctrl)
  166. {
  167. if (orig_ctrl & NSE_FRAMESYNC_MASK)
  168. bcm_phy_write_exp(phydev, NSE_CTRL, orig_ctrl);
  169. }
  170. static void bcm_ptp_framesync(struct phy_device *phydev, u16 ctrl)
  171. {
  172. /* trigger framesync - must have 0->1 transition. */
  173. bcm_phy_write_exp(phydev, NSE_CTRL, ctrl | NSE_CPU_FRAMESYNC);
  174. }
  175. static int bcm_ptp_framesync_ts(struct phy_device *phydev,
  176. struct ptp_system_timestamp *sts,
  177. struct timespec64 *ts,
  178. u16 orig_ctrl)
  179. {
  180. u16 ctrl, reg;
  181. int i;
  182. ctrl = bcm_ptp_framesync_disable(phydev, orig_ctrl);
  183. ptp_read_system_prets(sts);
  184. /* trigger framesync + capture */
  185. bcm_ptp_framesync(phydev, ctrl | NSE_CAPTURE_EN);
  186. ptp_read_system_postts(sts);
  187. /* poll for FSYNC interrupt from TS capture */
  188. for (i = 0; i < 10; i++) {
  189. reg = bcm_phy_read_exp(phydev, INTR_STATUS);
  190. if (reg & INTC_FSYNC) {
  191. bcm_ptp_get_framesync_ts(phydev, ts);
  192. break;
  193. }
  194. }
  195. bcm_ptp_framesync_restore(phydev, orig_ctrl);
  196. return reg & INTC_FSYNC ? 0 : -ETIMEDOUT;
  197. }
  198. static int bcm_ptp_gettimex(struct ptp_clock_info *info,
  199. struct timespec64 *ts,
  200. struct ptp_system_timestamp *sts)
  201. {
  202. struct bcm_ptp_private *priv = ptp2priv(info);
  203. int err;
  204. mutex_lock(&priv->mutex);
  205. err = bcm_ptp_framesync_ts(priv->phydev, sts, ts, priv->nse_ctrl);
  206. mutex_unlock(&priv->mutex);
  207. return err;
  208. }
  209. static int bcm_ptp_settime_locked(struct bcm_ptp_private *priv,
  210. const struct timespec64 *ts)
  211. {
  212. struct phy_device *phydev = priv->phydev;
  213. u16 ctrl;
  214. u64 ns;
  215. ctrl = bcm_ptp_framesync_disable(phydev, priv->nse_ctrl);
  216. /* set up time code */
  217. bcm_phy_write_exp(phydev, TIME_CODE_0, ts->tv_nsec);
  218. bcm_phy_write_exp(phydev, TIME_CODE_1, ts->tv_nsec >> 16);
  219. bcm_phy_write_exp(phydev, TIME_CODE_2, ts->tv_sec);
  220. bcm_phy_write_exp(phydev, TIME_CODE_3, ts->tv_sec >> 16);
  221. bcm_phy_write_exp(phydev, TIME_CODE_4, ts->tv_sec >> 32);
  222. /* set NCO counter to match */
  223. ns = timespec64_to_ns(ts);
  224. bcm_phy_write_exp(phydev, NCO_TIME_0, ns >> 4);
  225. bcm_phy_write_exp(phydev, NCO_TIME_1, ns >> 20);
  226. bcm_phy_write_exp(phydev, NCO_TIME_2_CTRL, (ns >> 36) & 0xfff);
  227. /* set up load on next frame sync (auto-clears due to NSE_INIT) */
  228. bcm_phy_write_exp(phydev, SHADOW_LOAD, TIME_CODE_LOAD | NCO_TIME_LOAD);
  229. /* must have NSE_INIT in order to write time code */
  230. bcm_ptp_framesync(phydev, ctrl | NSE_INIT);
  231. bcm_ptp_framesync_restore(phydev, priv->nse_ctrl);
  232. return 0;
  233. }
  234. static int bcm_ptp_settime(struct ptp_clock_info *info,
  235. const struct timespec64 *ts)
  236. {
  237. struct bcm_ptp_private *priv = ptp2priv(info);
  238. int err;
  239. mutex_lock(&priv->mutex);
  240. err = bcm_ptp_settime_locked(priv, ts);
  241. mutex_unlock(&priv->mutex);
  242. return err;
  243. }
  244. static int bcm_ptp_adjtime_locked(struct bcm_ptp_private *priv,
  245. s64 delta_ns)
  246. {
  247. struct timespec64 ts;
  248. int err;
  249. s64 ns;
  250. err = bcm_ptp_framesync_ts(priv->phydev, NULL, &ts, priv->nse_ctrl);
  251. if (!err) {
  252. ns = timespec64_to_ns(&ts) + delta_ns;
  253. ts = ns_to_timespec64(ns);
  254. err = bcm_ptp_settime_locked(priv, &ts);
  255. }
  256. return err;
  257. }
  258. static int bcm_ptp_adjtime(struct ptp_clock_info *info, s64 delta_ns)
  259. {
  260. struct bcm_ptp_private *priv = ptp2priv(info);
  261. int err;
  262. mutex_lock(&priv->mutex);
  263. err = bcm_ptp_adjtime_locked(priv, delta_ns);
  264. mutex_unlock(&priv->mutex);
  265. return err;
  266. }
  267. /* A 125Mhz clock should adjust 8ns per pulse.
  268. * The frequency adjustment base is 0x8000 0000, or 8*2^28.
  269. *
  270. * Frequency adjustment is
  271. * adj = scaled_ppm * 8*2^28 / (10^6 * 2^16)
  272. * which simplifies to:
  273. * adj = scaled_ppm * 2^9 / 5^6
  274. */
  275. static int bcm_ptp_adjfine(struct ptp_clock_info *info, long scaled_ppm)
  276. {
  277. struct bcm_ptp_private *priv = ptp2priv(info);
  278. int neg_adj = 0;
  279. u32 diff, freq;
  280. u16 ctrl;
  281. u64 adj;
  282. if (scaled_ppm < 0) {
  283. neg_adj = 1;
  284. scaled_ppm = -scaled_ppm;
  285. }
  286. adj = scaled_ppm << 9;
  287. diff = div_u64(adj, 15625);
  288. freq = (8 << 28) + (neg_adj ? -diff : diff);
  289. mutex_lock(&priv->mutex);
  290. ctrl = bcm_ptp_framesync_disable(priv->phydev, priv->nse_ctrl);
  291. bcm_phy_write_exp(priv->phydev, NCO_FREQ_LSB, freq);
  292. bcm_phy_write_exp(priv->phydev, NCO_FREQ_MSB, freq >> 16);
  293. bcm_phy_write_exp(priv->phydev, NCO_TIME_2_CTRL, FREQ_MDIO_SEL);
  294. /* load on next framesync */
  295. bcm_phy_write_exp(priv->phydev, SHADOW_LOAD, FREQ_LOAD);
  296. bcm_ptp_framesync(priv->phydev, ctrl);
  297. /* clear load */
  298. bcm_phy_write_exp(priv->phydev, SHADOW_LOAD, 0);
  299. bcm_ptp_framesync_restore(priv->phydev, priv->nse_ctrl);
  300. mutex_unlock(&priv->mutex);
  301. return 0;
  302. }
  303. static bool bcm_ptp_rxtstamp(struct mii_timestamper *mii_ts,
  304. struct sk_buff *skb, int type)
  305. {
  306. struct bcm_ptp_private *priv = mii2priv(mii_ts);
  307. struct skb_shared_hwtstamps *hwts;
  308. struct ptp_header *header;
  309. u32 sec, nsec;
  310. u8 *data;
  311. int off;
  312. if (!priv->hwts_rx)
  313. return false;
  314. header = ptp_parse_header(skb, type);
  315. if (!header)
  316. return false;
  317. data = (u8 *)(header + 1);
  318. sec = get_unaligned_be32(data);
  319. nsec = get_unaligned_be32(data + 4);
  320. hwts = skb_hwtstamps(skb);
  321. hwts->hwtstamp = ktime_set(sec, nsec);
  322. off = data - skb->data + 8;
  323. if (off < skb->len) {
  324. memmove(data, data + 8, skb->len - off);
  325. __pskb_trim(skb, skb->len - 8);
  326. }
  327. return false;
  328. }
  329. static bool bcm_ptp_get_tstamp(struct bcm_ptp_private *priv,
  330. struct bcm_ptp_capture *capts)
  331. {
  332. struct phy_device *phydev = priv->phydev;
  333. u16 ts[4], reg;
  334. u32 sec, nsec;
  335. mutex_lock(&priv->mutex);
  336. reg = bcm_phy_read_exp(phydev, INTR_STATUS);
  337. if ((reg & INTC_SOP) == 0) {
  338. mutex_unlock(&priv->mutex);
  339. return false;
  340. }
  341. bcm_phy_write_exp(phydev, TS_READ_CTRL, TS_READ_START);
  342. ts[0] = bcm_phy_read_exp(phydev, TS_REG_0);
  343. ts[1] = bcm_phy_read_exp(phydev, TS_REG_1);
  344. ts[2] = bcm_phy_read_exp(phydev, TS_REG_2);
  345. ts[3] = bcm_phy_read_exp(phydev, TS_REG_3);
  346. /* not in be32 format for some reason */
  347. capts->seq_id = bcm_phy_read_exp(priv->phydev, TS_INFO_0);
  348. reg = bcm_phy_read_exp(phydev, TS_INFO_1);
  349. capts->msgtype = reg >> 12;
  350. capts->tx_dir = !!(reg & BIT(11));
  351. bcm_phy_write_exp(phydev, TS_READ_CTRL, TS_READ_END);
  352. bcm_phy_write_exp(phydev, TS_READ_CTRL, 0);
  353. mutex_unlock(&priv->mutex);
  354. sec = (ts[3] << 16) | ts[2];
  355. nsec = (ts[1] << 16) | ts[0];
  356. capts->hwtstamp = ktime_set(sec, nsec);
  357. return true;
  358. }
  359. static void bcm_ptp_match_tstamp(struct bcm_ptp_private *priv,
  360. struct bcm_ptp_capture *capts)
  361. {
  362. struct skb_shared_hwtstamps hwts;
  363. struct sk_buff *skb, *ts_skb;
  364. unsigned long flags;
  365. bool first = false;
  366. ts_skb = NULL;
  367. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  368. skb_queue_walk(&priv->tx_queue, skb) {
  369. if (BCM_SKB_CB(skb)->seq_id == capts->seq_id &&
  370. BCM_SKB_CB(skb)->msgtype == capts->msgtype) {
  371. first = skb_queue_is_first(&priv->tx_queue, skb);
  372. __skb_unlink(skb, &priv->tx_queue);
  373. ts_skb = skb;
  374. break;
  375. }
  376. }
  377. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  378. /* TX captures one-step packets, discard them if needed. */
  379. if (ts_skb) {
  380. if (BCM_SKB_CB(ts_skb)->discard) {
  381. kfree_skb(ts_skb);
  382. } else {
  383. memset(&hwts, 0, sizeof(hwts));
  384. hwts.hwtstamp = capts->hwtstamp;
  385. skb_complete_tx_timestamp(ts_skb, &hwts);
  386. }
  387. }
  388. /* not first match, try and expire entries */
  389. if (!first) {
  390. while ((skb = skb_dequeue(&priv->tx_queue))) {
  391. if (!time_after(jiffies, BCM_SKB_CB(skb)->timeout)) {
  392. skb_queue_head(&priv->tx_queue, skb);
  393. break;
  394. }
  395. kfree_skb(skb);
  396. }
  397. }
  398. }
  399. static long bcm_ptp_do_aux_work(struct ptp_clock_info *info)
  400. {
  401. struct bcm_ptp_private *priv = ptp2priv(info);
  402. struct bcm_ptp_capture capts;
  403. bool reschedule = false;
  404. while (!skb_queue_empty_lockless(&priv->tx_queue)) {
  405. if (!bcm_ptp_get_tstamp(priv, &capts)) {
  406. reschedule = true;
  407. break;
  408. }
  409. bcm_ptp_match_tstamp(priv, &capts);
  410. }
  411. return reschedule ? 1 : -1;
  412. }
  413. static int bcm_ptp_cancel_func(struct bcm_ptp_private *priv)
  414. {
  415. if (!priv->pin_active)
  416. return 0;
  417. priv->pin_active = false;
  418. priv->nse_ctrl &= ~(NSE_SYNC_OUT_MASK | NSE_SYNC1_FRAMESYNC |
  419. NSE_CAPTURE_EN);
  420. bcm_phy_write_exp(priv->phydev, NSE_CTRL, priv->nse_ctrl);
  421. cancel_delayed_work_sync(&priv->pin_work);
  422. return 0;
  423. }
  424. static void bcm_ptp_perout_work(struct work_struct *pin_work)
  425. {
  426. struct bcm_ptp_private *priv =
  427. container_of(pin_work, struct bcm_ptp_private, pin_work.work);
  428. struct phy_device *phydev = priv->phydev;
  429. struct timespec64 ts;
  430. u64 ns, next;
  431. u16 ctrl;
  432. mutex_lock(&priv->mutex);
  433. /* no longer running */
  434. if (!priv->pin_active) {
  435. mutex_unlock(&priv->mutex);
  436. return;
  437. }
  438. bcm_ptp_framesync_ts(phydev, NULL, &ts, priv->nse_ctrl);
  439. /* this is 1PPS only */
  440. next = NSEC_PER_SEC - ts.tv_nsec;
  441. ts.tv_sec += next < NSEC_PER_MSEC ? 2 : 1;
  442. ts.tv_nsec = 0;
  443. ns = timespec64_to_ns(&ts);
  444. /* force 0->1 transition for ONESHOT */
  445. ctrl = bcm_ptp_framesync_disable(phydev,
  446. priv->nse_ctrl & ~NSE_ONESHOT_EN);
  447. bcm_phy_write_exp(phydev, SYNOUT_TS_0, ns & 0xfff0);
  448. bcm_phy_write_exp(phydev, SYNOUT_TS_1, ns >> 16);
  449. bcm_phy_write_exp(phydev, SYNOUT_TS_2, ns >> 32);
  450. /* load values on next framesync */
  451. bcm_phy_write_exp(phydev, SHADOW_LOAD, SYNC_OUT_LOAD);
  452. bcm_ptp_framesync(phydev, ctrl | NSE_ONESHOT_EN | NSE_INIT);
  453. priv->nse_ctrl |= NSE_ONESHOT_EN;
  454. bcm_ptp_framesync_restore(phydev, priv->nse_ctrl);
  455. mutex_unlock(&priv->mutex);
  456. next = next + NSEC_PER_MSEC;
  457. schedule_delayed_work(&priv->pin_work, nsecs_to_jiffies(next));
  458. }
  459. static int bcm_ptp_perout_locked(struct bcm_ptp_private *priv,
  460. struct ptp_perout_request *req, int on)
  461. {
  462. struct phy_device *phydev = priv->phydev;
  463. u64 period, pulse;
  464. u16 val;
  465. if (!on)
  466. return bcm_ptp_cancel_func(priv);
  467. /* 1PPS */
  468. if (req->period.sec != 1 || req->period.nsec != 0)
  469. return -EINVAL;
  470. period = BCM_MAX_PERIOD_8NS; /* write nonzero value */
  471. if (req->flags & PTP_PEROUT_PHASE)
  472. return -EOPNOTSUPP;
  473. if (req->flags & PTP_PEROUT_DUTY_CYCLE)
  474. pulse = ktime_to_ns(ktime_set(req->on.sec, req->on.nsec));
  475. else
  476. pulse = (u64)BCM_MAX_PULSE_8NS << 3;
  477. /* convert to 8ns units */
  478. pulse >>= 3;
  479. if (!pulse || pulse > period || pulse > BCM_MAX_PULSE_8NS)
  480. return -EINVAL;
  481. bcm_phy_write_exp(phydev, SYNC_OUT_0, period);
  482. val = ((pulse & 0x3) << 14) | ((period >> 16) & 0x3fff);
  483. bcm_phy_write_exp(phydev, SYNC_OUT_1, val);
  484. val = ((pulse >> 2) & 0x7f) | (pulse << 7);
  485. bcm_phy_write_exp(phydev, SYNC_OUT_2, val);
  486. if (priv->pin_active)
  487. cancel_delayed_work_sync(&priv->pin_work);
  488. priv->pin_active = true;
  489. INIT_DELAYED_WORK(&priv->pin_work, bcm_ptp_perout_work);
  490. schedule_delayed_work(&priv->pin_work, 0);
  491. return 0;
  492. }
  493. static void bcm_ptp_extts_work(struct work_struct *pin_work)
  494. {
  495. struct bcm_ptp_private *priv =
  496. container_of(pin_work, struct bcm_ptp_private, pin_work.work);
  497. struct phy_device *phydev = priv->phydev;
  498. struct ptp_clock_event event;
  499. struct timespec64 ts;
  500. u16 reg;
  501. mutex_lock(&priv->mutex);
  502. /* no longer running */
  503. if (!priv->pin_active) {
  504. mutex_unlock(&priv->mutex);
  505. return;
  506. }
  507. reg = bcm_phy_read_exp(phydev, INTR_STATUS);
  508. if ((reg & INTC_FSYNC) == 0)
  509. goto out;
  510. bcm_ptp_get_framesync_ts(phydev, &ts);
  511. event.index = 0;
  512. event.type = PTP_CLOCK_EXTTS;
  513. event.timestamp = timespec64_to_ns(&ts);
  514. ptp_clock_event(priv->ptp_clock, &event);
  515. out:
  516. mutex_unlock(&priv->mutex);
  517. schedule_delayed_work(&priv->pin_work, HZ / 4);
  518. }
  519. static int bcm_ptp_extts_locked(struct bcm_ptp_private *priv, int on)
  520. {
  521. struct phy_device *phydev = priv->phydev;
  522. if (!on)
  523. return bcm_ptp_cancel_func(priv);
  524. if (priv->pin_active)
  525. cancel_delayed_work_sync(&priv->pin_work);
  526. bcm_ptp_framesync_disable(phydev, priv->nse_ctrl);
  527. priv->nse_ctrl |= NSE_SYNC1_FRAMESYNC | NSE_CAPTURE_EN;
  528. bcm_ptp_framesync_restore(phydev, priv->nse_ctrl);
  529. priv->pin_active = true;
  530. INIT_DELAYED_WORK(&priv->pin_work, bcm_ptp_extts_work);
  531. schedule_delayed_work(&priv->pin_work, 0);
  532. return 0;
  533. }
  534. static int bcm_ptp_enable(struct ptp_clock_info *info,
  535. struct ptp_clock_request *rq, int on)
  536. {
  537. struct bcm_ptp_private *priv = ptp2priv(info);
  538. int err = -EBUSY;
  539. mutex_lock(&priv->mutex);
  540. switch (rq->type) {
  541. case PTP_CLK_REQ_PEROUT:
  542. if (priv->pin.func == PTP_PF_PEROUT)
  543. err = bcm_ptp_perout_locked(priv, &rq->perout, on);
  544. break;
  545. case PTP_CLK_REQ_EXTTS:
  546. if (priv->pin.func == PTP_PF_EXTTS)
  547. err = bcm_ptp_extts_locked(priv, on);
  548. break;
  549. default:
  550. err = -EOPNOTSUPP;
  551. break;
  552. }
  553. mutex_unlock(&priv->mutex);
  554. return err;
  555. }
  556. static int bcm_ptp_verify(struct ptp_clock_info *info, unsigned int pin,
  557. enum ptp_pin_function func, unsigned int chan)
  558. {
  559. switch (func) {
  560. case PTP_PF_NONE:
  561. case PTP_PF_EXTTS:
  562. case PTP_PF_PEROUT:
  563. break;
  564. default:
  565. return -EOPNOTSUPP;
  566. }
  567. return 0;
  568. }
  569. static const struct ptp_clock_info bcm_ptp_clock_info = {
  570. .owner = THIS_MODULE,
  571. .name = KBUILD_MODNAME,
  572. .max_adj = 100000000,
  573. .gettimex64 = bcm_ptp_gettimex,
  574. .settime64 = bcm_ptp_settime,
  575. .adjtime = bcm_ptp_adjtime,
  576. .adjfine = bcm_ptp_adjfine,
  577. .enable = bcm_ptp_enable,
  578. .verify = bcm_ptp_verify,
  579. .do_aux_work = bcm_ptp_do_aux_work,
  580. .n_pins = 1,
  581. .n_per_out = 1,
  582. .n_ext_ts = 1,
  583. };
  584. static void bcm_ptp_txtstamp(struct mii_timestamper *mii_ts,
  585. struct sk_buff *skb, int type)
  586. {
  587. struct bcm_ptp_private *priv = mii2priv(mii_ts);
  588. struct ptp_header *hdr;
  589. bool discard = false;
  590. int msgtype;
  591. hdr = ptp_parse_header(skb, type);
  592. if (!hdr)
  593. goto out;
  594. msgtype = ptp_get_msgtype(hdr, type);
  595. switch (priv->tx_type) {
  596. case HWTSTAMP_TX_ONESTEP_P2P:
  597. if (msgtype == PTP_MSGTYPE_PDELAY_RESP)
  598. discard = true;
  599. fallthrough;
  600. case HWTSTAMP_TX_ONESTEP_SYNC:
  601. if (msgtype == PTP_MSGTYPE_SYNC)
  602. discard = true;
  603. fallthrough;
  604. case HWTSTAMP_TX_ON:
  605. BCM_SKB_CB(skb)->timeout = jiffies + SKB_TS_TIMEOUT;
  606. BCM_SKB_CB(skb)->seq_id = be16_to_cpu(hdr->sequence_id);
  607. BCM_SKB_CB(skb)->msgtype = msgtype;
  608. BCM_SKB_CB(skb)->discard = discard;
  609. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  610. skb_queue_tail(&priv->tx_queue, skb);
  611. ptp_schedule_worker(priv->ptp_clock, 0);
  612. return;
  613. default:
  614. break;
  615. }
  616. out:
  617. kfree_skb(skb);
  618. }
  619. static int bcm_ptp_hwtstamp(struct mii_timestamper *mii_ts,
  620. struct ifreq *ifr)
  621. {
  622. struct bcm_ptp_private *priv = mii2priv(mii_ts);
  623. struct hwtstamp_config cfg;
  624. u16 mode, ctrl;
  625. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  626. return -EFAULT;
  627. switch (cfg.rx_filter) {
  628. case HWTSTAMP_FILTER_NONE:
  629. priv->hwts_rx = false;
  630. break;
  631. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  632. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  633. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  634. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  635. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  636. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  637. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  638. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  639. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  640. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  641. priv->hwts_rx = true;
  642. break;
  643. default:
  644. return -ERANGE;
  645. }
  646. priv->tx_type = cfg.tx_type;
  647. ctrl = priv->hwts_rx ? SLICE_RX_EN : 0;
  648. ctrl |= priv->tx_type != HWTSTAMP_TX_OFF ? SLICE_TX_EN : 0;
  649. mode = TX_MODE_SEL(PORT, SYNC, REPLACE_TS) |
  650. TX_MODE_SEL(PORT, DELAY_REQ, REPLACE_TS) |
  651. TX_MODE_SEL(PORT, PDELAY_REQ, REPLACE_TS) |
  652. TX_MODE_SEL(PORT, PDELAY_RESP, REPLACE_TS);
  653. bcm_phy_write_exp(priv->phydev, TX_EVENT_MODE, mode);
  654. mode = RX_MODE_SEL(PORT, SYNC, INSERT_TS_64) |
  655. RX_MODE_SEL(PORT, DELAY_REQ, INSERT_TS_64) |
  656. RX_MODE_SEL(PORT, PDELAY_REQ, INSERT_TS_64) |
  657. RX_MODE_SEL(PORT, PDELAY_RESP, INSERT_TS_64);
  658. bcm_phy_write_exp(priv->phydev, RX_EVENT_MODE, mode);
  659. bcm_phy_write_exp(priv->phydev, SLICE_CTRL, ctrl);
  660. if (ctrl & SLICE_TX_EN)
  661. bcm_phy_write_exp(priv->phydev, TX_TS_CAPTURE, TX_TS_CAP_EN);
  662. else
  663. ptp_cancel_worker_sync(priv->ptp_clock);
  664. /* purge existing data */
  665. skb_queue_purge(&priv->tx_queue);
  666. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  667. }
  668. static int bcm_ptp_ts_info(struct mii_timestamper *mii_ts,
  669. struct ethtool_ts_info *ts_info)
  670. {
  671. struct bcm_ptp_private *priv = mii2priv(mii_ts);
  672. ts_info->phc_index = ptp_clock_index(priv->ptp_clock);
  673. ts_info->so_timestamping =
  674. SOF_TIMESTAMPING_TX_HARDWARE |
  675. SOF_TIMESTAMPING_RX_HARDWARE |
  676. SOF_TIMESTAMPING_RAW_HARDWARE;
  677. ts_info->tx_types =
  678. BIT(HWTSTAMP_TX_ON) |
  679. BIT(HWTSTAMP_TX_OFF) |
  680. BIT(HWTSTAMP_TX_ONESTEP_SYNC) |
  681. BIT(HWTSTAMP_TX_ONESTEP_P2P);
  682. ts_info->rx_filters =
  683. BIT(HWTSTAMP_FILTER_NONE) |
  684. BIT(HWTSTAMP_FILTER_PTP_V2_EVENT);
  685. return 0;
  686. }
  687. void bcm_ptp_stop(struct bcm_ptp_private *priv)
  688. {
  689. ptp_cancel_worker_sync(priv->ptp_clock);
  690. bcm_ptp_cancel_func(priv);
  691. }
  692. EXPORT_SYMBOL_GPL(bcm_ptp_stop);
  693. void bcm_ptp_config_init(struct phy_device *phydev)
  694. {
  695. /* init network sync engine */
  696. bcm_phy_write_exp(phydev, NSE_CTRL, NSE_GMODE_EN | NSE_INIT);
  697. /* enable time sync (TX/RX SOP capture) */
  698. bcm_phy_write_exp(phydev, TIME_SYNC, TIME_SYNC_EN);
  699. /* use sec.nsec heartbeat capture */
  700. bcm_phy_write_exp(phydev, DPLL_SELECT, DPLL_HB_MODE2);
  701. /* use 64 bit timecode for TX */
  702. bcm_phy_write_exp(phydev, TIMECODE_CTRL, TX_TIMECODE_SEL);
  703. /* always allow FREQ_LOAD on framesync */
  704. bcm_phy_write_exp(phydev, SHADOW_CTRL, FREQ_LOAD);
  705. bcm_phy_write_exp(phydev, SYNC_IN_DIVIDER, 1);
  706. }
  707. EXPORT_SYMBOL_GPL(bcm_ptp_config_init);
  708. static void bcm_ptp_init(struct bcm_ptp_private *priv)
  709. {
  710. priv->nse_ctrl = NSE_GMODE_EN;
  711. mutex_init(&priv->mutex);
  712. skb_queue_head_init(&priv->tx_queue);
  713. priv->mii_ts.rxtstamp = bcm_ptp_rxtstamp;
  714. priv->mii_ts.txtstamp = bcm_ptp_txtstamp;
  715. priv->mii_ts.hwtstamp = bcm_ptp_hwtstamp;
  716. priv->mii_ts.ts_info = bcm_ptp_ts_info;
  717. priv->phydev->mii_ts = &priv->mii_ts;
  718. }
  719. struct bcm_ptp_private *bcm_ptp_probe(struct phy_device *phydev)
  720. {
  721. struct bcm_ptp_private *priv;
  722. struct ptp_clock *clock;
  723. switch (BRCM_PHY_MODEL(phydev)) {
  724. case PHY_ID_BCM54210E:
  725. break;
  726. default:
  727. return NULL;
  728. }
  729. priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
  730. if (!priv)
  731. return ERR_PTR(-ENOMEM);
  732. priv->ptp_info = bcm_ptp_clock_info;
  733. snprintf(priv->pin.name, sizeof(priv->pin.name), "SYNC_OUT");
  734. priv->ptp_info.pin_config = &priv->pin;
  735. clock = ptp_clock_register(&priv->ptp_info, &phydev->mdio.dev);
  736. if (IS_ERR(clock))
  737. return ERR_CAST(clock);
  738. priv->ptp_clock = clock;
  739. priv->phydev = phydev;
  740. bcm_ptp_init(priv);
  741. return priv;
  742. }
  743. EXPORT_SYMBOL_GPL(bcm_ptp_probe);
  744. MODULE_LICENSE("GPL");