ptp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Marvell 88E6xxx Switch PTP support
  3. *
  4. * Copyright (c) 2008 Marvell Semiconductor
  5. *
  6. * Copyright (c) 2017 National Instruments
  7. * Erik Hons <erik.hons@ni.com>
  8. * Brandon Streiff <brandon.streiff@ni.com>
  9. * Dane Wagner <dane.wagner@ni.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include "chip.h"
  17. #include "global2.h"
  18. #include "hwtstamp.h"
  19. #include "ptp.h"
  20. /* Raw timestamps are in units of 8-ns clock periods. */
  21. #define CC_SHIFT 28
  22. #define CC_MULT (8 << CC_SHIFT)
  23. #define CC_MULT_NUM (1 << 9)
  24. #define CC_MULT_DEM 15625ULL
  25. #define TAI_EVENT_WORK_INTERVAL msecs_to_jiffies(100)
  26. #define cc_to_chip(cc) container_of(cc, struct mv88e6xxx_chip, tstamp_cc)
  27. #define dw_overflow_to_chip(dw) container_of(dw, struct mv88e6xxx_chip, \
  28. overflow_work)
  29. #define dw_tai_event_to_chip(dw) container_of(dw, struct mv88e6xxx_chip, \
  30. tai_event_work)
  31. static int mv88e6xxx_tai_read(struct mv88e6xxx_chip *chip, int addr,
  32. u16 *data, int len)
  33. {
  34. if (!chip->info->ops->avb_ops->tai_read)
  35. return -EOPNOTSUPP;
  36. return chip->info->ops->avb_ops->tai_read(chip, addr, data, len);
  37. }
  38. static int mv88e6xxx_tai_write(struct mv88e6xxx_chip *chip, int addr, u16 data)
  39. {
  40. if (!chip->info->ops->avb_ops->tai_write)
  41. return -EOPNOTSUPP;
  42. return chip->info->ops->avb_ops->tai_write(chip, addr, data);
  43. }
  44. /* TODO: places where this are called should be using pinctrl */
  45. static int mv88e6352_set_gpio_func(struct mv88e6xxx_chip *chip, int pin,
  46. int func, int input)
  47. {
  48. int err;
  49. if (!chip->info->ops->gpio_ops)
  50. return -EOPNOTSUPP;
  51. err = chip->info->ops->gpio_ops->set_dir(chip, pin, input);
  52. if (err)
  53. return err;
  54. return chip->info->ops->gpio_ops->set_pctl(chip, pin, func);
  55. }
  56. static u64 mv88e6352_ptp_clock_read(const struct cyclecounter *cc)
  57. {
  58. struct mv88e6xxx_chip *chip = cc_to_chip(cc);
  59. u16 phc_time[2];
  60. int err;
  61. err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_TIME_LO, phc_time,
  62. ARRAY_SIZE(phc_time));
  63. if (err)
  64. return 0;
  65. else
  66. return ((u32)phc_time[1] << 16) | phc_time[0];
  67. }
  68. static u64 mv88e6165_ptp_clock_read(const struct cyclecounter *cc)
  69. {
  70. struct mv88e6xxx_chip *chip = cc_to_chip(cc);
  71. u16 phc_time[2];
  72. int err;
  73. err = mv88e6xxx_tai_read(chip, MV88E6XXX_PTP_GC_TIME_LO, phc_time,
  74. ARRAY_SIZE(phc_time));
  75. if (err)
  76. return 0;
  77. else
  78. return ((u32)phc_time[1] << 16) | phc_time[0];
  79. }
  80. /* mv88e6352_config_eventcap - configure TAI event capture
  81. * @event: PTP_CLOCK_PPS (internal) or PTP_CLOCK_EXTTS (external)
  82. * @rising: zero for falling-edge trigger, else rising-edge trigger
  83. *
  84. * This will also reset the capture sequence counter.
  85. */
  86. static int mv88e6352_config_eventcap(struct mv88e6xxx_chip *chip, int event,
  87. int rising)
  88. {
  89. u16 global_config;
  90. u16 cap_config;
  91. int err;
  92. chip->evcap_config = MV88E6XXX_TAI_CFG_CAP_OVERWRITE |
  93. MV88E6XXX_TAI_CFG_CAP_CTR_START;
  94. if (!rising)
  95. chip->evcap_config |= MV88E6XXX_TAI_CFG_EVREQ_FALLING;
  96. global_config = (chip->evcap_config | chip->trig_config);
  97. err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_CFG, global_config);
  98. if (err)
  99. return err;
  100. if (event == PTP_CLOCK_PPS) {
  101. cap_config = MV88E6XXX_TAI_EVENT_STATUS_CAP_TRIG;
  102. } else if (event == PTP_CLOCK_EXTTS) {
  103. /* if STATUS_CAP_TRIG is unset we capture PTP_EVREQ events */
  104. cap_config = 0;
  105. } else {
  106. return -EINVAL;
  107. }
  108. /* Write the capture config; this also clears the capture counter */
  109. err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_EVENT_STATUS,
  110. cap_config);
  111. return err;
  112. }
  113. static void mv88e6352_tai_event_work(struct work_struct *ugly)
  114. {
  115. struct delayed_work *dw = to_delayed_work(ugly);
  116. struct mv88e6xxx_chip *chip = dw_tai_event_to_chip(dw);
  117. struct ptp_clock_event ev;
  118. u16 status[4];
  119. u32 raw_ts;
  120. int err;
  121. mutex_lock(&chip->reg_lock);
  122. err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_EVENT_STATUS,
  123. status, ARRAY_SIZE(status));
  124. mutex_unlock(&chip->reg_lock);
  125. if (err) {
  126. dev_err(chip->dev, "failed to read TAI status register\n");
  127. return;
  128. }
  129. if (status[0] & MV88E6XXX_TAI_EVENT_STATUS_ERROR) {
  130. dev_warn(chip->dev, "missed event capture\n");
  131. return;
  132. }
  133. if (!(status[0] & MV88E6XXX_TAI_EVENT_STATUS_VALID))
  134. goto out;
  135. raw_ts = ((u32)status[2] << 16) | status[1];
  136. /* Clear the valid bit so the next timestamp can come in */
  137. status[0] &= ~MV88E6XXX_TAI_EVENT_STATUS_VALID;
  138. mutex_lock(&chip->reg_lock);
  139. err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_EVENT_STATUS, status[0]);
  140. mutex_unlock(&chip->reg_lock);
  141. /* This is an external timestamp */
  142. ev.type = PTP_CLOCK_EXTTS;
  143. /* We only have one timestamping channel. */
  144. ev.index = 0;
  145. mutex_lock(&chip->reg_lock);
  146. ev.timestamp = timecounter_cyc2time(&chip->tstamp_tc, raw_ts);
  147. mutex_unlock(&chip->reg_lock);
  148. ptp_clock_event(chip->ptp_clock, &ev);
  149. out:
  150. schedule_delayed_work(&chip->tai_event_work, TAI_EVENT_WORK_INTERVAL);
  151. }
  152. static int mv88e6xxx_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  153. {
  154. struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
  155. int neg_adj = 0;
  156. u32 diff, mult;
  157. u64 adj;
  158. if (scaled_ppm < 0) {
  159. neg_adj = 1;
  160. scaled_ppm = -scaled_ppm;
  161. }
  162. mult = CC_MULT;
  163. adj = CC_MULT_NUM;
  164. adj *= scaled_ppm;
  165. diff = div_u64(adj, CC_MULT_DEM);
  166. mutex_lock(&chip->reg_lock);
  167. timecounter_read(&chip->tstamp_tc);
  168. chip->tstamp_cc.mult = neg_adj ? mult - diff : mult + diff;
  169. mutex_unlock(&chip->reg_lock);
  170. return 0;
  171. }
  172. static int mv88e6xxx_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  173. {
  174. struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
  175. mutex_lock(&chip->reg_lock);
  176. timecounter_adjtime(&chip->tstamp_tc, delta);
  177. mutex_unlock(&chip->reg_lock);
  178. return 0;
  179. }
  180. static int mv88e6xxx_ptp_gettime(struct ptp_clock_info *ptp,
  181. struct timespec64 *ts)
  182. {
  183. struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
  184. u64 ns;
  185. mutex_lock(&chip->reg_lock);
  186. ns = timecounter_read(&chip->tstamp_tc);
  187. mutex_unlock(&chip->reg_lock);
  188. *ts = ns_to_timespec64(ns);
  189. return 0;
  190. }
  191. static int mv88e6xxx_ptp_settime(struct ptp_clock_info *ptp,
  192. const struct timespec64 *ts)
  193. {
  194. struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
  195. u64 ns;
  196. ns = timespec64_to_ns(ts);
  197. mutex_lock(&chip->reg_lock);
  198. timecounter_init(&chip->tstamp_tc, &chip->tstamp_cc, ns);
  199. mutex_unlock(&chip->reg_lock);
  200. return 0;
  201. }
  202. static int mv88e6352_ptp_enable_extts(struct mv88e6xxx_chip *chip,
  203. struct ptp_clock_request *rq, int on)
  204. {
  205. int rising = (rq->extts.flags & PTP_RISING_EDGE);
  206. int func;
  207. int pin;
  208. int err;
  209. pin = ptp_find_pin(chip->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
  210. if (pin < 0)
  211. return -EBUSY;
  212. mutex_lock(&chip->reg_lock);
  213. if (on) {
  214. func = MV88E6352_G2_SCRATCH_GPIO_PCTL_EVREQ;
  215. err = mv88e6352_set_gpio_func(chip, pin, func, true);
  216. if (err)
  217. goto out;
  218. schedule_delayed_work(&chip->tai_event_work,
  219. TAI_EVENT_WORK_INTERVAL);
  220. err = mv88e6352_config_eventcap(chip, PTP_CLOCK_EXTTS, rising);
  221. } else {
  222. func = MV88E6352_G2_SCRATCH_GPIO_PCTL_GPIO;
  223. err = mv88e6352_set_gpio_func(chip, pin, func, true);
  224. cancel_delayed_work_sync(&chip->tai_event_work);
  225. }
  226. out:
  227. mutex_unlock(&chip->reg_lock);
  228. return err;
  229. }
  230. static int mv88e6352_ptp_enable(struct ptp_clock_info *ptp,
  231. struct ptp_clock_request *rq, int on)
  232. {
  233. struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
  234. switch (rq->type) {
  235. case PTP_CLK_REQ_EXTTS:
  236. return mv88e6352_ptp_enable_extts(chip, rq, on);
  237. default:
  238. return -EOPNOTSUPP;
  239. }
  240. }
  241. static int mv88e6352_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
  242. enum ptp_pin_function func, unsigned int chan)
  243. {
  244. switch (func) {
  245. case PTP_PF_NONE:
  246. case PTP_PF_EXTTS:
  247. break;
  248. case PTP_PF_PEROUT:
  249. case PTP_PF_PHYSYNC:
  250. return -EOPNOTSUPP;
  251. }
  252. return 0;
  253. }
  254. const struct mv88e6xxx_ptp_ops mv88e6352_ptp_ops = {
  255. .clock_read = mv88e6352_ptp_clock_read,
  256. .ptp_enable = mv88e6352_ptp_enable,
  257. .ptp_verify = mv88e6352_ptp_verify,
  258. .event_work = mv88e6352_tai_event_work,
  259. .port_enable = mv88e6352_hwtstamp_port_enable,
  260. .port_disable = mv88e6352_hwtstamp_port_disable,
  261. .n_ext_ts = 1,
  262. .arr0_sts_reg = MV88E6XXX_PORT_PTP_ARR0_STS,
  263. .arr1_sts_reg = MV88E6XXX_PORT_PTP_ARR1_STS,
  264. .dep_sts_reg = MV88E6XXX_PORT_PTP_DEP_STS,
  265. .rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  266. (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
  267. (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
  268. (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
  269. (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
  270. (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
  271. (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
  272. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
  273. (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
  274. (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
  275. };
  276. const struct mv88e6xxx_ptp_ops mv88e6165_ptp_ops = {
  277. .clock_read = mv88e6165_ptp_clock_read,
  278. .global_enable = mv88e6165_global_enable,
  279. .global_disable = mv88e6165_global_disable,
  280. .arr0_sts_reg = MV88E6165_PORT_PTP_ARR0_STS,
  281. .arr1_sts_reg = MV88E6165_PORT_PTP_ARR1_STS,
  282. .dep_sts_reg = MV88E6165_PORT_PTP_DEP_STS,
  283. .rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  284. (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
  285. (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
  286. (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
  287. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
  288. (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
  289. (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
  290. };
  291. static u64 mv88e6xxx_ptp_clock_read(const struct cyclecounter *cc)
  292. {
  293. struct mv88e6xxx_chip *chip = cc_to_chip(cc);
  294. if (chip->info->ops->ptp_ops->clock_read)
  295. return chip->info->ops->ptp_ops->clock_read(cc);
  296. return 0;
  297. }
  298. /* With a 125MHz input clock, the 32-bit timestamp counter overflows in ~34.3
  299. * seconds; this task forces periodic reads so that we don't miss any.
  300. */
  301. #define MV88E6XXX_TAI_OVERFLOW_PERIOD (HZ * 16)
  302. static void mv88e6xxx_ptp_overflow_check(struct work_struct *work)
  303. {
  304. struct delayed_work *dw = to_delayed_work(work);
  305. struct mv88e6xxx_chip *chip = dw_overflow_to_chip(dw);
  306. struct timespec64 ts;
  307. mv88e6xxx_ptp_gettime(&chip->ptp_clock_info, &ts);
  308. schedule_delayed_work(&chip->overflow_work,
  309. MV88E6XXX_TAI_OVERFLOW_PERIOD);
  310. }
  311. int mv88e6xxx_ptp_setup(struct mv88e6xxx_chip *chip)
  312. {
  313. const struct mv88e6xxx_ptp_ops *ptp_ops = chip->info->ops->ptp_ops;
  314. int i;
  315. /* Set up the cycle counter */
  316. memset(&chip->tstamp_cc, 0, sizeof(chip->tstamp_cc));
  317. chip->tstamp_cc.read = mv88e6xxx_ptp_clock_read;
  318. chip->tstamp_cc.mask = CYCLECOUNTER_MASK(32);
  319. chip->tstamp_cc.mult = CC_MULT;
  320. chip->tstamp_cc.shift = CC_SHIFT;
  321. timecounter_init(&chip->tstamp_tc, &chip->tstamp_cc,
  322. ktime_to_ns(ktime_get_real()));
  323. INIT_DELAYED_WORK(&chip->overflow_work, mv88e6xxx_ptp_overflow_check);
  324. if (ptp_ops->event_work)
  325. INIT_DELAYED_WORK(&chip->tai_event_work, ptp_ops->event_work);
  326. chip->ptp_clock_info.owner = THIS_MODULE;
  327. snprintf(chip->ptp_clock_info.name, sizeof(chip->ptp_clock_info.name),
  328. dev_name(chip->dev));
  329. chip->ptp_clock_info.max_adj = 1000000;
  330. chip->ptp_clock_info.n_ext_ts = ptp_ops->n_ext_ts;
  331. chip->ptp_clock_info.n_per_out = 0;
  332. chip->ptp_clock_info.n_pins = mv88e6xxx_num_gpio(chip);
  333. chip->ptp_clock_info.pps = 0;
  334. for (i = 0; i < chip->ptp_clock_info.n_pins; ++i) {
  335. struct ptp_pin_desc *ppd = &chip->pin_config[i];
  336. snprintf(ppd->name, sizeof(ppd->name), "mv88e6xxx_gpio%d", i);
  337. ppd->index = i;
  338. ppd->func = PTP_PF_NONE;
  339. }
  340. chip->ptp_clock_info.pin_config = chip->pin_config;
  341. chip->ptp_clock_info.adjfine = mv88e6xxx_ptp_adjfine;
  342. chip->ptp_clock_info.adjtime = mv88e6xxx_ptp_adjtime;
  343. chip->ptp_clock_info.gettime64 = mv88e6xxx_ptp_gettime;
  344. chip->ptp_clock_info.settime64 = mv88e6xxx_ptp_settime;
  345. chip->ptp_clock_info.enable = ptp_ops->ptp_enable;
  346. chip->ptp_clock_info.verify = ptp_ops->ptp_verify;
  347. chip->ptp_clock_info.do_aux_work = mv88e6xxx_hwtstamp_work;
  348. chip->ptp_clock = ptp_clock_register(&chip->ptp_clock_info, chip->dev);
  349. if (IS_ERR(chip->ptp_clock))
  350. return PTR_ERR(chip->ptp_clock);
  351. schedule_delayed_work(&chip->overflow_work,
  352. MV88E6XXX_TAI_OVERFLOW_PERIOD);
  353. return 0;
  354. }
  355. void mv88e6xxx_ptp_free(struct mv88e6xxx_chip *chip)
  356. {
  357. if (chip->ptp_clock) {
  358. cancel_delayed_work_sync(&chip->overflow_work);
  359. if (chip->info->ops->ptp_ops->event_work)
  360. cancel_delayed_work_sync(&chip->tai_event_work);
  361. ptp_clock_unregister(chip->ptp_clock);
  362. chip->ptp_clock = NULL;
  363. }
  364. }