ocelot_ptp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /* Microsemi Ocelot PTP clock driver
  3. *
  4. * Copyright (c) 2017 Microsemi Corporation
  5. * Copyright 2020 NXP
  6. */
  7. #include <linux/time64.h>
  8. #include <linux/dsa/ocelot.h>
  9. #include <linux/ptp_classify.h>
  10. #include <soc/mscc/ocelot_ptp.h>
  11. #include <soc/mscc/ocelot_sys.h>
  12. #include <soc/mscc/ocelot_vcap.h>
  13. #include <soc/mscc/ocelot.h>
  14. #include "ocelot.h"
  15. int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
  16. {
  17. struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
  18. unsigned long flags;
  19. time64_t s;
  20. u32 val;
  21. s64 ns;
  22. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  23. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  24. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
  25. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
  26. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  27. s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
  28. s <<= 32;
  29. s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
  30. ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
  31. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  32. /* Deal with negative values */
  33. if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
  34. s--;
  35. ns &= 0xf;
  36. ns += 999999984;
  37. }
  38. set_normalized_timespec64(ts, s, ns);
  39. return 0;
  40. }
  41. EXPORT_SYMBOL(ocelot_ptp_gettime64);
  42. int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
  43. const struct timespec64 *ts)
  44. {
  45. struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
  46. unsigned long flags;
  47. u32 val;
  48. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  49. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  50. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
  51. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
  52. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  53. ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
  54. TOD_ACC_PIN);
  55. ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
  56. TOD_ACC_PIN);
  57. ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
  58. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  59. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
  60. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
  61. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  62. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  63. if (ocelot->ops->tas_clock_adjust)
  64. ocelot->ops->tas_clock_adjust(ocelot);
  65. return 0;
  66. }
  67. EXPORT_SYMBOL(ocelot_ptp_settime64);
  68. int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  69. {
  70. if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
  71. struct ocelot *ocelot = container_of(ptp, struct ocelot,
  72. ptp_info);
  73. unsigned long flags;
  74. u32 val;
  75. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  76. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  77. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK |
  78. PTP_PIN_CFG_DOM);
  79. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
  80. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  81. ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
  82. ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
  83. ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
  84. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  85. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK |
  86. PTP_PIN_CFG_DOM);
  87. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
  88. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  89. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  90. if (ocelot->ops->tas_clock_adjust)
  91. ocelot->ops->tas_clock_adjust(ocelot);
  92. } else {
  93. /* Fall back using ocelot_ptp_settime64 which is not exact. */
  94. struct timespec64 ts;
  95. u64 now;
  96. ocelot_ptp_gettime64(ptp, &ts);
  97. now = ktime_to_ns(timespec64_to_ktime(ts));
  98. ts = ns_to_timespec64(now + delta);
  99. ocelot_ptp_settime64(ptp, &ts);
  100. }
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(ocelot_ptp_adjtime);
  104. int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  105. {
  106. struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
  107. u32 unit = 0, direction = 0;
  108. unsigned long flags;
  109. u64 adj = 0;
  110. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  111. if (!scaled_ppm)
  112. goto disable_adj;
  113. if (scaled_ppm < 0) {
  114. direction = PTP_CFG_CLK_ADJ_CFG_DIR;
  115. scaled_ppm = -scaled_ppm;
  116. }
  117. adj = PSEC_PER_SEC << 16;
  118. do_div(adj, scaled_ppm);
  119. do_div(adj, 1000);
  120. /* If the adjustment value is too large, use ns instead */
  121. if (adj >= (1L << 30)) {
  122. unit = PTP_CFG_CLK_ADJ_FREQ_NS;
  123. do_div(adj, 1000);
  124. }
  125. /* Still too big */
  126. if (adj >= (1L << 30))
  127. goto disable_adj;
  128. ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
  129. ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
  130. PTP_CLK_CFG_ADJ_CFG);
  131. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  132. return 0;
  133. disable_adj:
  134. ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
  135. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  136. return 0;
  137. }
  138. EXPORT_SYMBOL(ocelot_ptp_adjfine);
  139. int ocelot_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
  140. enum ptp_pin_function func, unsigned int chan)
  141. {
  142. switch (func) {
  143. case PTP_PF_NONE:
  144. case PTP_PF_PEROUT:
  145. break;
  146. case PTP_PF_EXTTS:
  147. case PTP_PF_PHYSYNC:
  148. return -1;
  149. }
  150. return 0;
  151. }
  152. EXPORT_SYMBOL(ocelot_ptp_verify);
  153. int ocelot_ptp_enable(struct ptp_clock_info *ptp,
  154. struct ptp_clock_request *rq, int on)
  155. {
  156. struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
  157. struct timespec64 ts_phase, ts_period;
  158. enum ocelot_ptp_pins ptp_pin;
  159. unsigned long flags;
  160. bool pps = false;
  161. int pin = -1;
  162. s64 wf_high;
  163. s64 wf_low;
  164. u32 val;
  165. switch (rq->type) {
  166. case PTP_CLK_REQ_PEROUT:
  167. /* Reject requests with unsupported flags */
  168. if (rq->perout.flags & ~(PTP_PEROUT_DUTY_CYCLE |
  169. PTP_PEROUT_PHASE))
  170. return -EOPNOTSUPP;
  171. pin = ptp_find_pin(ocelot->ptp_clock, PTP_PF_PEROUT,
  172. rq->perout.index);
  173. if (pin == 0)
  174. ptp_pin = PTP_PIN_0;
  175. else if (pin == 1)
  176. ptp_pin = PTP_PIN_1;
  177. else if (pin == 2)
  178. ptp_pin = PTP_PIN_2;
  179. else if (pin == 3)
  180. ptp_pin = PTP_PIN_3;
  181. else
  182. return -EBUSY;
  183. ts_period.tv_sec = rq->perout.period.sec;
  184. ts_period.tv_nsec = rq->perout.period.nsec;
  185. if (ts_period.tv_sec == 1 && ts_period.tv_nsec == 0)
  186. pps = true;
  187. /* Handle turning off */
  188. if (!on) {
  189. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  190. val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
  191. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
  192. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  193. break;
  194. }
  195. if (rq->perout.flags & PTP_PEROUT_PHASE) {
  196. ts_phase.tv_sec = rq->perout.phase.sec;
  197. ts_phase.tv_nsec = rq->perout.phase.nsec;
  198. } else {
  199. /* Compatibility */
  200. ts_phase.tv_sec = rq->perout.start.sec;
  201. ts_phase.tv_nsec = rq->perout.start.nsec;
  202. }
  203. if (ts_phase.tv_sec || (ts_phase.tv_nsec && !pps)) {
  204. dev_warn(ocelot->dev,
  205. "Absolute start time not supported!\n");
  206. dev_warn(ocelot->dev,
  207. "Accept nsec for PPS phase adjustment, otherwise start time should be 0 0.\n");
  208. return -EINVAL;
  209. }
  210. /* Calculate waveform high and low times */
  211. if (rq->perout.flags & PTP_PEROUT_DUTY_CYCLE) {
  212. struct timespec64 ts_on;
  213. ts_on.tv_sec = rq->perout.on.sec;
  214. ts_on.tv_nsec = rq->perout.on.nsec;
  215. wf_high = timespec64_to_ns(&ts_on);
  216. } else {
  217. if (pps) {
  218. wf_high = 1000;
  219. } else {
  220. wf_high = timespec64_to_ns(&ts_period);
  221. wf_high = div_s64(wf_high, 2);
  222. }
  223. }
  224. wf_low = timespec64_to_ns(&ts_period);
  225. wf_low -= wf_high;
  226. /* Handle PPS request */
  227. if (pps) {
  228. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  229. ocelot_write_rix(ocelot, ts_phase.tv_nsec,
  230. PTP_PIN_WF_LOW_PERIOD, ptp_pin);
  231. ocelot_write_rix(ocelot, wf_high,
  232. PTP_PIN_WF_HIGH_PERIOD, ptp_pin);
  233. val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_CLOCK);
  234. val |= PTP_PIN_CFG_SYNC;
  235. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
  236. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  237. break;
  238. }
  239. /* Handle periodic clock */
  240. if (wf_high > 0x3fffffff || wf_high <= 0x6)
  241. return -EINVAL;
  242. if (wf_low > 0x3fffffff || wf_low <= 0x6)
  243. return -EINVAL;
  244. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  245. ocelot_write_rix(ocelot, wf_low, PTP_PIN_WF_LOW_PERIOD,
  246. ptp_pin);
  247. ocelot_write_rix(ocelot, wf_high, PTP_PIN_WF_HIGH_PERIOD,
  248. ptp_pin);
  249. val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_CLOCK);
  250. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
  251. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  252. break;
  253. default:
  254. return -EOPNOTSUPP;
  255. }
  256. return 0;
  257. }
  258. EXPORT_SYMBOL(ocelot_ptp_enable);
  259. static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap)
  260. {
  261. trap->key_type = OCELOT_VCAP_KEY_ETYPE;
  262. *(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588);
  263. *(__be16 *)trap->key.etype.etype.mask = htons(0xffff);
  264. }
  265. static void
  266. ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
  267. {
  268. trap->key_type = OCELOT_VCAP_KEY_IPV4;
  269. trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
  270. trap->key.ipv4.proto.mask[0] = 0xff;
  271. trap->key.ipv4.dport.value = PTP_EV_PORT;
  272. trap->key.ipv4.dport.mask = 0xffff;
  273. }
  274. static void
  275. ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
  276. {
  277. trap->key_type = OCELOT_VCAP_KEY_IPV6;
  278. trap->key.ipv6.proto.value[0] = IPPROTO_UDP;
  279. trap->key.ipv6.proto.mask[0] = 0xff;
  280. trap->key.ipv6.dport.value = PTP_EV_PORT;
  281. trap->key.ipv6.dport.mask = 0xffff;
  282. }
  283. static void
  284. ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
  285. {
  286. trap->key_type = OCELOT_VCAP_KEY_IPV4;
  287. trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
  288. trap->key.ipv4.proto.mask[0] = 0xff;
  289. trap->key.ipv4.dport.value = PTP_GEN_PORT;
  290. trap->key.ipv4.dport.mask = 0xffff;
  291. }
  292. static void
  293. ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
  294. {
  295. trap->key_type = OCELOT_VCAP_KEY_IPV6;
  296. trap->key.ipv6.proto.value[0] = IPPROTO_UDP;
  297. trap->key.ipv6.proto.mask[0] = 0xff;
  298. trap->key.ipv6.dport.value = PTP_GEN_PORT;
  299. trap->key.ipv6.dport.mask = 0xffff;
  300. }
  301. static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port)
  302. {
  303. unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
  304. return ocelot_trap_add(ocelot, port, l2_cookie, true,
  305. ocelot_populate_l2_ptp_trap_key);
  306. }
  307. static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port)
  308. {
  309. unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
  310. return ocelot_trap_del(ocelot, port, l2_cookie);
  311. }
  312. static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port)
  313. {
  314. unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot);
  315. unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot);
  316. int err;
  317. err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie, true,
  318. ocelot_populate_ipv4_ptp_event_trap_key);
  319. if (err)
  320. return err;
  321. err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie, false,
  322. ocelot_populate_ipv4_ptp_general_trap_key);
  323. if (err)
  324. ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
  325. return err;
  326. }
  327. static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port)
  328. {
  329. unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot);
  330. unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot);
  331. int err;
  332. err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
  333. err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie);
  334. return err;
  335. }
  336. static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port)
  337. {
  338. unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot);
  339. unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot);
  340. int err;
  341. err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie, true,
  342. ocelot_populate_ipv6_ptp_event_trap_key);
  343. if (err)
  344. return err;
  345. err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie, false,
  346. ocelot_populate_ipv6_ptp_general_trap_key);
  347. if (err)
  348. ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
  349. return err;
  350. }
  351. static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port)
  352. {
  353. unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot);
  354. unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot);
  355. int err;
  356. err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
  357. err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie);
  358. return err;
  359. }
  360. static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port,
  361. bool l2, bool l4)
  362. {
  363. struct ocelot_port *ocelot_port = ocelot->ports[port];
  364. int err;
  365. ocelot_port->trap_proto &= ~(OCELOT_PROTO_PTP_L2 |
  366. OCELOT_PROTO_PTP_L4);
  367. if (l2)
  368. err = ocelot_l2_ptp_trap_add(ocelot, port);
  369. else
  370. err = ocelot_l2_ptp_trap_del(ocelot, port);
  371. if (err)
  372. return err;
  373. if (l4) {
  374. err = ocelot_ipv4_ptp_trap_add(ocelot, port);
  375. if (err)
  376. goto err_ipv4;
  377. err = ocelot_ipv6_ptp_trap_add(ocelot, port);
  378. if (err)
  379. goto err_ipv6;
  380. } else {
  381. err = ocelot_ipv4_ptp_trap_del(ocelot, port);
  382. err |= ocelot_ipv6_ptp_trap_del(ocelot, port);
  383. }
  384. if (err)
  385. return err;
  386. if (l2)
  387. ocelot_port->trap_proto |= OCELOT_PROTO_PTP_L2;
  388. if (l4)
  389. ocelot_port->trap_proto |= OCELOT_PROTO_PTP_L4;
  390. return 0;
  391. err_ipv6:
  392. ocelot_ipv4_ptp_trap_del(ocelot, port);
  393. err_ipv4:
  394. if (l2)
  395. ocelot_l2_ptp_trap_del(ocelot, port);
  396. return err;
  397. }
  398. static int ocelot_traps_to_ptp_rx_filter(unsigned int proto)
  399. {
  400. if ((proto & OCELOT_PROTO_PTP_L2) && (proto & OCELOT_PROTO_PTP_L4))
  401. return HWTSTAMP_FILTER_PTP_V2_EVENT;
  402. else if (proto & OCELOT_PROTO_PTP_L2)
  403. return HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  404. else if (proto & OCELOT_PROTO_PTP_L4)
  405. return HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  406. return HWTSTAMP_FILTER_NONE;
  407. }
  408. int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
  409. {
  410. struct ocelot_port *ocelot_port = ocelot->ports[port];
  411. struct hwtstamp_config cfg = {};
  412. switch (ocelot_port->ptp_cmd) {
  413. case IFH_REW_OP_TWO_STEP_PTP:
  414. cfg.tx_type = HWTSTAMP_TX_ON;
  415. break;
  416. case IFH_REW_OP_ORIGIN_PTP:
  417. cfg.tx_type = HWTSTAMP_TX_ONESTEP_SYNC;
  418. break;
  419. default:
  420. cfg.tx_type = HWTSTAMP_TX_OFF;
  421. break;
  422. }
  423. cfg.rx_filter = ocelot_traps_to_ptp_rx_filter(ocelot_port->trap_proto);
  424. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  425. }
  426. EXPORT_SYMBOL(ocelot_hwstamp_get);
  427. int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
  428. {
  429. struct ocelot_port *ocelot_port = ocelot->ports[port];
  430. bool l2 = false, l4 = false;
  431. struct hwtstamp_config cfg;
  432. int err;
  433. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  434. return -EFAULT;
  435. /* Tx type sanity check */
  436. switch (cfg.tx_type) {
  437. case HWTSTAMP_TX_ON:
  438. ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
  439. break;
  440. case HWTSTAMP_TX_ONESTEP_SYNC:
  441. /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
  442. * need to update the origin time.
  443. */
  444. ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
  445. break;
  446. case HWTSTAMP_TX_OFF:
  447. ocelot_port->ptp_cmd = 0;
  448. break;
  449. default:
  450. return -ERANGE;
  451. }
  452. switch (cfg.rx_filter) {
  453. case HWTSTAMP_FILTER_NONE:
  454. break;
  455. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  456. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  457. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  458. l4 = true;
  459. break;
  460. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  461. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  462. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  463. l2 = true;
  464. break;
  465. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  466. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  467. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  468. l2 = true;
  469. l4 = true;
  470. break;
  471. default:
  472. return -ERANGE;
  473. }
  474. err = ocelot_setup_ptp_traps(ocelot, port, l2, l4);
  475. if (err)
  476. return err;
  477. cfg.rx_filter = ocelot_traps_to_ptp_rx_filter(ocelot_port->trap_proto);
  478. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  479. }
  480. EXPORT_SYMBOL(ocelot_hwstamp_set);
  481. int ocelot_get_ts_info(struct ocelot *ocelot, int port,
  482. struct ethtool_ts_info *info)
  483. {
  484. info->phc_index = ocelot->ptp_clock ?
  485. ptp_clock_index(ocelot->ptp_clock) : -1;
  486. if (info->phc_index == -1) {
  487. info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
  488. SOF_TIMESTAMPING_RX_SOFTWARE |
  489. SOF_TIMESTAMPING_SOFTWARE;
  490. return 0;
  491. }
  492. info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
  493. SOF_TIMESTAMPING_RX_SOFTWARE |
  494. SOF_TIMESTAMPING_SOFTWARE |
  495. SOF_TIMESTAMPING_TX_HARDWARE |
  496. SOF_TIMESTAMPING_RX_HARDWARE |
  497. SOF_TIMESTAMPING_RAW_HARDWARE;
  498. info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
  499. BIT(HWTSTAMP_TX_ONESTEP_SYNC);
  500. info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
  501. BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
  502. BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
  503. BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
  504. return 0;
  505. }
  506. EXPORT_SYMBOL(ocelot_get_ts_info);
  507. static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
  508. struct sk_buff *clone)
  509. {
  510. struct ocelot_port *ocelot_port = ocelot->ports[port];
  511. unsigned long flags;
  512. spin_lock_irqsave(&ocelot->ts_id_lock, flags);
  513. if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
  514. ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
  515. spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
  516. return -EBUSY;
  517. }
  518. skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
  519. /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
  520. OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
  521. ocelot_port->ts_id++;
  522. if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
  523. ocelot_port->ts_id = 0;
  524. ocelot_port->ptp_skbs_in_flight++;
  525. ocelot->ptp_skbs_in_flight++;
  526. skb_queue_tail(&ocelot_port->tx_skbs, clone);
  527. spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
  528. return 0;
  529. }
  530. static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
  531. unsigned int ptp_class)
  532. {
  533. struct ptp_header *hdr;
  534. u8 msgtype, twostep;
  535. hdr = ptp_parse_header(skb, ptp_class);
  536. if (!hdr)
  537. return false;
  538. msgtype = ptp_get_msgtype(hdr, ptp_class);
  539. twostep = hdr->flag_field[0] & 0x2;
  540. if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
  541. return true;
  542. return false;
  543. }
  544. int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
  545. struct sk_buff *skb,
  546. struct sk_buff **clone)
  547. {
  548. struct ocelot_port *ocelot_port = ocelot->ports[port];
  549. u8 ptp_cmd = ocelot_port->ptp_cmd;
  550. unsigned int ptp_class;
  551. int err;
  552. /* Don't do anything if PTP timestamping not enabled */
  553. if (!ptp_cmd)
  554. return 0;
  555. ptp_class = ptp_classify_raw(skb);
  556. if (ptp_class == PTP_CLASS_NONE)
  557. return -EINVAL;
  558. /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
  559. if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
  560. if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
  561. OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
  562. return 0;
  563. }
  564. /* Fall back to two-step timestamping */
  565. ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
  566. }
  567. if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
  568. *clone = skb_clone_sk(skb);
  569. if (!(*clone))
  570. return -ENOMEM;
  571. err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
  572. if (err)
  573. return err;
  574. OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
  575. OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
  576. }
  577. return 0;
  578. }
  579. EXPORT_SYMBOL(ocelot_port_txtstamp_request);
  580. static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
  581. struct timespec64 *ts)
  582. {
  583. unsigned long flags;
  584. u32 val;
  585. spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
  586. /* Read current PTP time to get seconds */
  587. val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
  588. val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
  589. val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
  590. ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
  591. ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
  592. /* Read packet HW timestamp from FIFO */
  593. val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
  594. ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
  595. /* Sec has incremented since the ts was registered */
  596. if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
  597. ts->tv_sec--;
  598. spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
  599. }
  600. static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
  601. {
  602. struct ptp_header *hdr;
  603. hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
  604. if (WARN_ON(!hdr))
  605. return false;
  606. return seqid == ntohs(hdr->sequence_id);
  607. }
  608. void ocelot_get_txtstamp(struct ocelot *ocelot)
  609. {
  610. int budget = OCELOT_PTP_QUEUE_SZ;
  611. while (budget--) {
  612. struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
  613. struct skb_shared_hwtstamps shhwtstamps;
  614. u32 val, id, seqid, txport;
  615. struct ocelot_port *port;
  616. struct timespec64 ts;
  617. unsigned long flags;
  618. val = ocelot_read(ocelot, SYS_PTP_STATUS);
  619. /* Check if a timestamp can be retrieved */
  620. if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
  621. break;
  622. WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
  623. /* Retrieve the ts ID and Tx port */
  624. id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
  625. txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
  626. seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
  627. port = ocelot->ports[txport];
  628. spin_lock(&ocelot->ts_id_lock);
  629. port->ptp_skbs_in_flight--;
  630. ocelot->ptp_skbs_in_flight--;
  631. spin_unlock(&ocelot->ts_id_lock);
  632. /* Retrieve its associated skb */
  633. try_again:
  634. spin_lock_irqsave(&port->tx_skbs.lock, flags);
  635. skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
  636. if (OCELOT_SKB_CB(skb)->ts_id != id)
  637. continue;
  638. __skb_unlink(skb, &port->tx_skbs);
  639. skb_match = skb;
  640. break;
  641. }
  642. spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
  643. if (WARN_ON(!skb_match))
  644. continue;
  645. if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
  646. dev_err_ratelimited(ocelot->dev,
  647. "port %d received stale TX timestamp for seqid %d, discarding\n",
  648. txport, seqid);
  649. dev_kfree_skb_any(skb);
  650. goto try_again;
  651. }
  652. /* Get the h/w timestamp */
  653. ocelot_get_hwtimestamp(ocelot, &ts);
  654. /* Set the timestamp into the skb */
  655. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  656. shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
  657. skb_complete_tx_timestamp(skb_match, &shhwtstamps);
  658. /* Next ts */
  659. ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
  660. }
  661. }
  662. EXPORT_SYMBOL(ocelot_get_txtstamp);
  663. int ocelot_init_timestamp(struct ocelot *ocelot,
  664. const struct ptp_clock_info *info)
  665. {
  666. struct ptp_clock *ptp_clock;
  667. int i;
  668. ocelot->ptp_info = *info;
  669. for (i = 0; i < OCELOT_PTP_PINS_NUM; i++) {
  670. struct ptp_pin_desc *p = &ocelot->ptp_pins[i];
  671. snprintf(p->name, sizeof(p->name), "switch_1588_dat%d", i);
  672. p->index = i;
  673. p->func = PTP_PF_NONE;
  674. }
  675. ocelot->ptp_info.pin_config = &ocelot->ptp_pins[0];
  676. ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
  677. if (IS_ERR(ptp_clock))
  678. return PTR_ERR(ptp_clock);
  679. /* Check if PHC support is missing at the configuration level */
  680. if (!ptp_clock)
  681. return 0;
  682. ocelot->ptp_clock = ptp_clock;
  683. ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
  684. ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
  685. ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
  686. ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
  687. return 0;
  688. }
  689. EXPORT_SYMBOL(ocelot_init_timestamp);
  690. int ocelot_deinit_timestamp(struct ocelot *ocelot)
  691. {
  692. if (ocelot->ptp_clock)
  693. ptp_clock_unregister(ocelot->ptp_clock);
  694. return 0;
  695. }
  696. EXPORT_SYMBOL(ocelot_deinit_timestamp);