ptp_fc3.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PTP hardware clock driver for the FemtoClock3 family of timing and
  4. * synchronization devices.
  5. *
  6. * Copyright (C) 2023 Integrated Device Technology, Inc., a Renesas Company.
  7. */
  8. #include <linux/firmware.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/module.h>
  11. #include <linux/ptp_clock_kernel.h>
  12. #include <linux/delay.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/kernel.h>
  15. #include <linux/timekeeping.h>
  16. #include <linux/string.h>
  17. #include <linux/of.h>
  18. #include <linux/bitfield.h>
  19. #include <linux/mfd/rsmu.h>
  20. #include <linux/mfd/idtRC38xxx_reg.h>
  21. #include <linux/unaligned.h>
  22. #include "ptp_private.h"
  23. #include "ptp_fc3.h"
  24. MODULE_DESCRIPTION("Driver for IDT FemtoClock3(TM) family");
  25. MODULE_AUTHOR("IDT support-1588 <IDT-support-1588@lm.renesas.com>");
  26. MODULE_VERSION("1.0");
  27. MODULE_LICENSE("GPL");
  28. /*
  29. * The name of the firmware file to be loaded
  30. * over-rides any automatic selection
  31. */
  32. static char *firmware;
  33. module_param(firmware, charp, 0);
  34. static s64 ns2counters(struct idtfc3 *idtfc3, s64 nsec, u32 *sub_ns)
  35. {
  36. s64 sync;
  37. s32 rem;
  38. if (likely(nsec >= 0)) {
  39. sync = div_u64_rem(nsec, idtfc3->ns_per_sync, &rem);
  40. *sub_ns = rem;
  41. } else {
  42. sync = -div_u64_rem(-nsec - 1, idtfc3->ns_per_sync, &rem) - 1;
  43. *sub_ns = idtfc3->ns_per_sync - rem - 1;
  44. }
  45. return sync * idtfc3->ns_per_sync;
  46. }
  47. static s64 tdc_meas2offset(struct idtfc3 *idtfc3, u64 meas_read)
  48. {
  49. s64 coarse, fine;
  50. fine = sign_extend64(FIELD_GET(FINE_MEAS_MASK, meas_read), 12);
  51. coarse = sign_extend64(FIELD_GET(COARSE_MEAS_MASK, meas_read), (39 - 13));
  52. fine = div64_s64(fine * NSEC_PER_SEC, idtfc3->tdc_apll_freq * 62LL);
  53. coarse = div64_s64(coarse * NSEC_PER_SEC, idtfc3->time_ref_freq);
  54. return coarse + fine;
  55. }
  56. static s64 tdc_offset2phase(struct idtfc3 *idtfc3, s64 offset_ns)
  57. {
  58. if (offset_ns > idtfc3->ns_per_sync / 2)
  59. offset_ns -= idtfc3->ns_per_sync;
  60. return offset_ns * idtfc3->tdc_offset_sign;
  61. }
  62. static int idtfc3_set_lpf_mode(struct idtfc3 *idtfc3, u8 mode)
  63. {
  64. int err;
  65. if (mode >= LPF_INVALID)
  66. return -EINVAL;
  67. if (idtfc3->lpf_mode == mode)
  68. return 0;
  69. err = regmap_bulk_write(idtfc3->regmap, LPF_MODE_CNFG, &mode, sizeof(mode));
  70. if (err)
  71. return err;
  72. idtfc3->lpf_mode = mode;
  73. return 0;
  74. }
  75. static int idtfc3_enable_lpf(struct idtfc3 *idtfc3, bool enable)
  76. {
  77. u8 val;
  78. int err;
  79. err = regmap_bulk_read(idtfc3->regmap, LPF_CTRL, &val, sizeof(val));
  80. if (err)
  81. return err;
  82. if (enable == true)
  83. val |= LPF_EN;
  84. else
  85. val &= ~LPF_EN;
  86. return regmap_bulk_write(idtfc3->regmap, LPF_CTRL, &val, sizeof(val));
  87. }
  88. static int idtfc3_get_time_ref_freq(struct idtfc3 *idtfc3)
  89. {
  90. int err;
  91. u8 buf[4];
  92. u8 time_ref_div;
  93. u8 time_clk_div;
  94. err = regmap_bulk_read(idtfc3->regmap, TIME_CLOCK_MEAS_DIV_CNFG, buf, sizeof(buf));
  95. if (err)
  96. return err;
  97. time_ref_div = FIELD_GET(TIME_REF_DIV_MASK, get_unaligned_le32(buf)) + 1;
  98. err = regmap_bulk_read(idtfc3->regmap, TIME_CLOCK_COUNT, buf, 1);
  99. if (err)
  100. return err;
  101. time_clk_div = (buf[0] & TIME_CLOCK_COUNT_MASK) + 1;
  102. idtfc3->time_ref_freq = idtfc3->hw_param.time_clk_freq *
  103. time_clk_div / time_ref_div;
  104. return 0;
  105. }
  106. static int idtfc3_get_tdc_offset_sign(struct idtfc3 *idtfc3)
  107. {
  108. int err;
  109. u8 buf[4];
  110. u32 val;
  111. u8 sig1, sig2;
  112. err = regmap_bulk_read(idtfc3->regmap, TIME_CLOCK_TDC_FANOUT_CNFG, buf, sizeof(buf));
  113. if (err)
  114. return err;
  115. val = get_unaligned_le32(buf);
  116. if ((val & TIME_SYNC_TO_TDC_EN) != TIME_SYNC_TO_TDC_EN) {
  117. dev_err(idtfc3->dev, "TIME_SYNC_TO_TDC_EN is off !!!");
  118. return -EINVAL;
  119. }
  120. sig1 = FIELD_GET(SIG1_MUX_SEL_MASK, val);
  121. sig2 = FIELD_GET(SIG2_MUX_SEL_MASK, val);
  122. if ((sig1 == sig2) || ((sig1 != TIME_SYNC) && (sig2 != TIME_SYNC))) {
  123. dev_err(idtfc3->dev, "Invalid tdc_mux_sel sig1=%d sig2=%d", sig1, sig2);
  124. return -EINVAL;
  125. } else if (sig1 == TIME_SYNC) {
  126. idtfc3->tdc_offset_sign = 1;
  127. } else if (sig2 == TIME_SYNC) {
  128. idtfc3->tdc_offset_sign = -1;
  129. }
  130. return 0;
  131. }
  132. static int idtfc3_lpf_bw(struct idtfc3 *idtfc3, u8 shift, u8 mult)
  133. {
  134. u8 val = FIELD_PREP(LPF_BW_SHIFT, shift) | FIELD_PREP(LPF_BW_MULT, mult);
  135. return regmap_bulk_write(idtfc3->regmap, LPF_BW_CNFG, &val, sizeof(val));
  136. }
  137. static int idtfc3_enable_tdc(struct idtfc3 *idtfc3, bool enable, u8 meas_mode)
  138. {
  139. int err;
  140. u8 val = 0;
  141. /* Disable TDC first */
  142. err = regmap_bulk_write(idtfc3->regmap, TIME_CLOCK_MEAS_CTRL, &val, sizeof(val));
  143. if (err)
  144. return err;
  145. if (enable == false)
  146. return idtfc3_lpf_bw(idtfc3, LPF_BW_SHIFT_DEFAULT, LPF_BW_MULT_DEFAULT);
  147. if (meas_mode >= MEAS_MODE_INVALID)
  148. return -EINVAL;
  149. /* Change TDC meas mode */
  150. err = regmap_bulk_write(idtfc3->regmap, TIME_CLOCK_MEAS_CNFG,
  151. &meas_mode, sizeof(meas_mode));
  152. if (err)
  153. return err;
  154. /* Enable TDC */
  155. val = TDC_MEAS_EN;
  156. if (meas_mode == CONTINUOUS)
  157. val |= TDC_MEAS_START;
  158. err = regmap_bulk_write(idtfc3->regmap, TIME_CLOCK_MEAS_CTRL, &val, sizeof(val));
  159. if (err)
  160. return err;
  161. return idtfc3_lpf_bw(idtfc3, LPF_BW_SHIFT_1PPS, LPF_BW_MULT_DEFAULT);
  162. }
  163. static bool get_tdc_meas(struct idtfc3 *idtfc3, s64 *offset_ns)
  164. {
  165. bool valid = false;
  166. u8 buf[9];
  167. u8 val;
  168. int err;
  169. while (true) {
  170. err = regmap_bulk_read(idtfc3->regmap, TDC_FIFO_STS,
  171. &val, sizeof(val));
  172. if (err)
  173. return false;
  174. if (val & FIFO_EMPTY)
  175. break;
  176. err = regmap_bulk_read(idtfc3->regmap, TDC_FIFO_READ_REQ,
  177. &buf, sizeof(buf));
  178. if (err)
  179. return false;
  180. valid = true;
  181. }
  182. if (valid)
  183. *offset_ns = tdc_meas2offset(idtfc3, get_unaligned_le64(&buf[1]));
  184. return valid;
  185. }
  186. static int check_tdc_fifo_overrun(struct idtfc3 *idtfc3)
  187. {
  188. u8 val;
  189. int err;
  190. /* Check if FIFO is overrun */
  191. err = regmap_bulk_read(idtfc3->regmap, TDC_FIFO_STS, &val, sizeof(val));
  192. if (err)
  193. return err;
  194. if (!(val & FIFO_FULL))
  195. return 0;
  196. dev_warn(idtfc3->dev, "TDC FIFO overrun !!!");
  197. err = idtfc3_enable_tdc(idtfc3, true, CONTINUOUS);
  198. if (err)
  199. return err;
  200. return 0;
  201. }
  202. static int get_tdc_meas_continuous(struct idtfc3 *idtfc3)
  203. {
  204. int err;
  205. s64 offset_ns;
  206. struct ptp_clock_event event;
  207. err = check_tdc_fifo_overrun(idtfc3);
  208. if (err)
  209. return err;
  210. if (get_tdc_meas(idtfc3, &offset_ns) && offset_ns >= 0) {
  211. event.index = 0;
  212. event.offset = tdc_offset2phase(idtfc3, offset_ns);
  213. event.type = PTP_CLOCK_EXTOFF;
  214. ptp_clock_event(idtfc3->ptp_clock, &event);
  215. }
  216. return 0;
  217. }
  218. static int idtfc3_read_subcounter(struct idtfc3 *idtfc3)
  219. {
  220. u8 buf[5] = {0};
  221. int err;
  222. err = regmap_bulk_read(idtfc3->regmap, TOD_COUNTER_READ_REQ,
  223. &buf, sizeof(buf));
  224. if (err)
  225. return err;
  226. /* sync_counter_value is [31:82] and sub_sync_counter_value is [0:30] */
  227. return get_unaligned_le32(&buf[1]) & SUB_SYNC_COUNTER_MASK;
  228. }
  229. static int idtfc3_tod_update_is_done(struct idtfc3 *idtfc3)
  230. {
  231. int err;
  232. u8 req;
  233. err = read_poll_timeout_atomic(regmap_bulk_read, err, !req, USEC_PER_MSEC,
  234. idtfc3->tc_write_timeout, true, idtfc3->regmap,
  235. TOD_SYNC_LOAD_REQ_CTRL, &req, 1);
  236. if (err)
  237. dev_err(idtfc3->dev, "TOD counter write timeout !!!");
  238. return err;
  239. }
  240. static int idtfc3_write_subcounter(struct idtfc3 *idtfc3, u32 counter)
  241. {
  242. u8 buf[18] = {0};
  243. int err;
  244. /* sync_counter_value is [31:82] and sub_sync_counter_value is [0:30] */
  245. put_unaligned_le32(counter & SUB_SYNC_COUNTER_MASK, &buf[0]);
  246. buf[16] = SUB_SYNC_LOAD_ENABLE | SYNC_LOAD_ENABLE;
  247. buf[17] = SYNC_LOAD_REQ;
  248. err = regmap_bulk_write(idtfc3->regmap, TOD_SYNC_LOAD_VAL_CTRL,
  249. &buf, sizeof(buf));
  250. if (err)
  251. return err;
  252. return idtfc3_tod_update_is_done(idtfc3);
  253. }
  254. static int idtfc3_timecounter_update(struct idtfc3 *idtfc3, u32 counter, s64 ns)
  255. {
  256. int err;
  257. err = idtfc3_write_subcounter(idtfc3, counter);
  258. if (err)
  259. return err;
  260. /* Update time counter */
  261. idtfc3->ns = ns;
  262. idtfc3->last_counter = counter;
  263. return 0;
  264. }
  265. static int idtfc3_timecounter_read(struct idtfc3 *idtfc3)
  266. {
  267. int now, delta;
  268. now = idtfc3_read_subcounter(idtfc3);
  269. if (now < 0)
  270. return now;
  271. /* calculate the delta since the last idtfc3_timecounter_read(): */
  272. if (now >= idtfc3->last_counter)
  273. delta = now - idtfc3->last_counter;
  274. else
  275. delta = idtfc3->sub_sync_count - idtfc3->last_counter + now;
  276. /* Update time counter */
  277. idtfc3->ns += delta * idtfc3->ns_per_counter;
  278. idtfc3->last_counter = now;
  279. return 0;
  280. }
  281. static int _idtfc3_gettime(struct idtfc3 *idtfc3, struct timespec64 *ts)
  282. {
  283. int err;
  284. err = idtfc3_timecounter_read(idtfc3);
  285. if (err)
  286. return err;
  287. *ts = ns_to_timespec64(idtfc3->ns);
  288. return 0;
  289. }
  290. static int idtfc3_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  291. {
  292. struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
  293. int err;
  294. mutex_lock(idtfc3->lock);
  295. err = _idtfc3_gettime(idtfc3, ts);
  296. mutex_unlock(idtfc3->lock);
  297. return err;
  298. }
  299. static int _idtfc3_settime(struct idtfc3 *idtfc3, const struct timespec64 *ts)
  300. {
  301. s64 offset_ns, now_ns;
  302. u32 counter, sub_ns;
  303. int now;
  304. if (timespec64_valid(ts) == false) {
  305. dev_err(idtfc3->dev, "%s: invalid timespec", __func__);
  306. return -EINVAL;
  307. }
  308. now = idtfc3_read_subcounter(idtfc3);
  309. if (now < 0)
  310. return now;
  311. offset_ns = (idtfc3->sub_sync_count - now) * idtfc3->ns_per_counter;
  312. now_ns = timespec64_to_ns(ts);
  313. (void)ns2counters(idtfc3, offset_ns + now_ns, &sub_ns);
  314. counter = sub_ns / idtfc3->ns_per_counter;
  315. return idtfc3_timecounter_update(idtfc3, counter, now_ns);
  316. }
  317. static int idtfc3_settime(struct ptp_clock_info *ptp, const struct timespec64 *ts)
  318. {
  319. struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
  320. int err;
  321. mutex_lock(idtfc3->lock);
  322. err = _idtfc3_settime(idtfc3, ts);
  323. mutex_unlock(idtfc3->lock);
  324. return err;
  325. }
  326. static int _idtfc3_adjtime(struct idtfc3 *idtfc3, s64 delta)
  327. {
  328. /*
  329. * The TOD counter can be synchronously loaded with any value,
  330. * to be loaded on the next Time Sync pulse
  331. */
  332. s64 sync_ns;
  333. u32 sub_ns;
  334. u32 counter;
  335. if (idtfc3->ns + delta < 0) {
  336. dev_err(idtfc3->dev, "%lld ns adj is too large", delta);
  337. return -EINVAL;
  338. }
  339. sync_ns = ns2counters(idtfc3, delta + idtfc3->ns_per_sync, &sub_ns);
  340. counter = sub_ns / idtfc3->ns_per_counter;
  341. return idtfc3_timecounter_update(idtfc3, counter, idtfc3->ns + sync_ns +
  342. counter * idtfc3->ns_per_counter);
  343. }
  344. static int idtfc3_adjtime(struct ptp_clock_info *ptp, s64 delta)
  345. {
  346. struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
  347. int err;
  348. mutex_lock(idtfc3->lock);
  349. err = _idtfc3_adjtime(idtfc3, delta);
  350. mutex_unlock(idtfc3->lock);
  351. return err;
  352. }
  353. static int _idtfc3_adjphase(struct idtfc3 *idtfc3, s32 delta)
  354. {
  355. u8 buf[8] = {0};
  356. int err;
  357. s64 pcw;
  358. err = idtfc3_set_lpf_mode(idtfc3, LPF_WP);
  359. if (err)
  360. return err;
  361. /*
  362. * Phase Control Word unit is: 10^9 / (TDC_APLL_FREQ * 124)
  363. *
  364. * delta * TDC_APLL_FREQ * 124
  365. * PCW = ---------------------------
  366. * 10^9
  367. *
  368. */
  369. pcw = div_s64((s64)delta * idtfc3->tdc_apll_freq * 124, NSEC_PER_SEC);
  370. put_unaligned_le64(pcw, buf);
  371. return regmap_bulk_write(idtfc3->regmap, LPF_WR_PHASE_CTRL, buf, sizeof(buf));
  372. }
  373. static int idtfc3_adjphase(struct ptp_clock_info *ptp, s32 delta)
  374. {
  375. struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
  376. int err;
  377. mutex_lock(idtfc3->lock);
  378. err = _idtfc3_adjphase(idtfc3, delta);
  379. mutex_unlock(idtfc3->lock);
  380. return err;
  381. }
  382. static int _idtfc3_adjfine(struct idtfc3 *idtfc3, long scaled_ppm)
  383. {
  384. u8 buf[8] = {0};
  385. int err;
  386. s64 fcw;
  387. err = idtfc3_set_lpf_mode(idtfc3, LPF_WF);
  388. if (err)
  389. return err;
  390. /*
  391. * Frequency Control Word unit is: 2^-44 * 10^6 ppm
  392. *
  393. * adjfreq:
  394. * ppb * 2^44
  395. * FCW = ----------
  396. * 10^9
  397. *
  398. * adjfine:
  399. * ppm_16 * 2^28
  400. * FCW = -------------
  401. * 10^6
  402. */
  403. fcw = scaled_ppm * BIT(28);
  404. fcw = div_s64(fcw, 1000000);
  405. put_unaligned_le64(fcw, buf);
  406. return regmap_bulk_write(idtfc3->regmap, LPF_WR_FREQ_CTRL, buf, sizeof(buf));
  407. }
  408. static int idtfc3_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  409. {
  410. struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
  411. int err;
  412. mutex_lock(idtfc3->lock);
  413. err = _idtfc3_adjfine(idtfc3, scaled_ppm);
  414. mutex_unlock(idtfc3->lock);
  415. return err;
  416. }
  417. static int idtfc3_enable(struct ptp_clock_info *ptp,
  418. struct ptp_clock_request *rq, int on)
  419. {
  420. struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
  421. int err = -EOPNOTSUPP;
  422. mutex_lock(idtfc3->lock);
  423. switch (rq->type) {
  424. case PTP_CLK_REQ_PEROUT:
  425. if (!on)
  426. err = 0;
  427. /* Only accept a 1-PPS aligned to the second. */
  428. else if (rq->perout.start.nsec || rq->perout.period.sec != 1 ||
  429. rq->perout.period.nsec)
  430. err = -ERANGE;
  431. else
  432. err = 0;
  433. break;
  434. case PTP_CLK_REQ_EXTTS:
  435. if (on) {
  436. /* Only accept requests for external phase offset */
  437. if ((rq->extts.flags & PTP_EXT_OFFSET) != (PTP_EXT_OFFSET))
  438. err = -EOPNOTSUPP;
  439. else
  440. err = idtfc3_enable_tdc(idtfc3, true, CONTINUOUS);
  441. } else {
  442. err = idtfc3_enable_tdc(idtfc3, false, MEAS_MODE_INVALID);
  443. }
  444. break;
  445. default:
  446. break;
  447. }
  448. mutex_unlock(idtfc3->lock);
  449. if (err)
  450. dev_err(idtfc3->dev, "Failed in %s with err %d!", __func__, err);
  451. return err;
  452. }
  453. static long idtfc3_aux_work(struct ptp_clock_info *ptp)
  454. {
  455. struct idtfc3 *idtfc3 = container_of(ptp, struct idtfc3, caps);
  456. static int tdc_get;
  457. mutex_lock(idtfc3->lock);
  458. tdc_get %= TDC_GET_PERIOD;
  459. if ((tdc_get == 0) || (tdc_get == TDC_GET_PERIOD / 2))
  460. idtfc3_timecounter_read(idtfc3);
  461. get_tdc_meas_continuous(idtfc3);
  462. tdc_get++;
  463. mutex_unlock(idtfc3->lock);
  464. return idtfc3->tc_update_period;
  465. }
  466. static const struct ptp_clock_info idtfc3_caps = {
  467. .owner = THIS_MODULE,
  468. .max_adj = MAX_FFO_PPB,
  469. .n_per_out = 1,
  470. .n_ext_ts = 1,
  471. .adjphase = &idtfc3_adjphase,
  472. .adjfine = &idtfc3_adjfine,
  473. .adjtime = &idtfc3_adjtime,
  474. .gettime64 = &idtfc3_gettime,
  475. .settime64 = &idtfc3_settime,
  476. .enable = &idtfc3_enable,
  477. .do_aux_work = &idtfc3_aux_work,
  478. };
  479. static int idtfc3_hw_calibrate(struct idtfc3 *idtfc3)
  480. {
  481. int err = 0;
  482. u8 val;
  483. mdelay(10);
  484. /*
  485. * Toggle TDC_DAC_RECAL_REQ:
  486. * (1) set tdc_en to 1
  487. * (2) set tdc_dac_recal_req to 0
  488. * (3) set tdc_dac_recal_req to 1
  489. */
  490. val = TDC_EN;
  491. err = regmap_bulk_write(idtfc3->regmap, TDC_CTRL,
  492. &val, sizeof(val));
  493. if (err)
  494. return err;
  495. val = TDC_EN | TDC_DAC_RECAL_REQ;
  496. err = regmap_bulk_write(idtfc3->regmap, TDC_CTRL,
  497. &val, sizeof(val));
  498. if (err)
  499. return err;
  500. mdelay(10);
  501. /*
  502. * Toggle APLL_REINIT:
  503. * (1) set apll_reinit to 0
  504. * (2) set apll_reinit to 1
  505. */
  506. val = 0;
  507. err = regmap_bulk_write(idtfc3->regmap, SOFT_RESET_CTRL,
  508. &val, sizeof(val));
  509. if (err)
  510. return err;
  511. val = APLL_REINIT;
  512. err = regmap_bulk_write(idtfc3->regmap, SOFT_RESET_CTRL,
  513. &val, sizeof(val));
  514. if (err)
  515. return err;
  516. mdelay(10);
  517. return err;
  518. }
  519. static int idtfc3_init_timecounter(struct idtfc3 *idtfc3)
  520. {
  521. int err;
  522. u32 period_ms;
  523. period_ms = idtfc3->sub_sync_count * MSEC_PER_SEC /
  524. idtfc3->hw_param.time_clk_freq;
  525. idtfc3->tc_update_period = msecs_to_jiffies(period_ms / TDC_GET_PERIOD);
  526. idtfc3->tc_write_timeout = period_ms * USEC_PER_MSEC;
  527. err = idtfc3_timecounter_update(idtfc3, 0, 0);
  528. if (err)
  529. return err;
  530. err = idtfc3_timecounter_read(idtfc3);
  531. if (err)
  532. return err;
  533. ptp_schedule_worker(idtfc3->ptp_clock, idtfc3->tc_update_period);
  534. return 0;
  535. }
  536. static int idtfc3_get_tdc_apll_freq(struct idtfc3 *idtfc3)
  537. {
  538. int err;
  539. u8 tdc_fb_div_int;
  540. u8 tdc_ref_div;
  541. struct idtfc3_hw_param *param = &idtfc3->hw_param;
  542. err = regmap_bulk_read(idtfc3->regmap, TDC_REF_DIV_CNFG,
  543. &tdc_ref_div, sizeof(tdc_ref_div));
  544. if (err)
  545. return err;
  546. err = regmap_bulk_read(idtfc3->regmap, TDC_FB_DIV_INT_CNFG,
  547. &tdc_fb_div_int, sizeof(tdc_fb_div_int));
  548. if (err)
  549. return err;
  550. tdc_fb_div_int &= TDC_FB_DIV_INT_MASK;
  551. tdc_ref_div &= TDC_REF_DIV_CONFIG_MASK;
  552. idtfc3->tdc_apll_freq = div_u64(param->xtal_freq * (u64)tdc_fb_div_int,
  553. 1 << tdc_ref_div);
  554. return 0;
  555. }
  556. static int idtfc3_get_fod(struct idtfc3 *idtfc3)
  557. {
  558. int err;
  559. u8 fod;
  560. err = regmap_bulk_read(idtfc3->regmap, TIME_CLOCK_SRC, &fod, sizeof(fod));
  561. if (err)
  562. return err;
  563. switch (fod) {
  564. case 0:
  565. idtfc3->fod_n = FOD_0;
  566. break;
  567. case 1:
  568. idtfc3->fod_n = FOD_1;
  569. break;
  570. case 2:
  571. idtfc3->fod_n = FOD_2;
  572. break;
  573. default:
  574. return -EINVAL;
  575. }
  576. return 0;
  577. }
  578. static int idtfc3_get_sync_count(struct idtfc3 *idtfc3)
  579. {
  580. int err;
  581. u8 buf[4];
  582. err = regmap_bulk_read(idtfc3->regmap, SUB_SYNC_GEN_CNFG, buf, sizeof(buf));
  583. if (err)
  584. return err;
  585. idtfc3->sub_sync_count = (get_unaligned_le32(buf) & SUB_SYNC_COUNTER_MASK) + 1;
  586. idtfc3->ns_per_counter = NSEC_PER_SEC / idtfc3->hw_param.time_clk_freq;
  587. idtfc3->ns_per_sync = idtfc3->sub_sync_count * idtfc3->ns_per_counter;
  588. return 0;
  589. }
  590. static int idtfc3_setup_hw_param(struct idtfc3 *idtfc3)
  591. {
  592. int err;
  593. err = idtfc3_get_fod(idtfc3);
  594. if (err)
  595. return err;
  596. err = idtfc3_get_sync_count(idtfc3);
  597. if (err)
  598. return err;
  599. err = idtfc3_get_time_ref_freq(idtfc3);
  600. if (err)
  601. return err;
  602. return idtfc3_get_tdc_apll_freq(idtfc3);
  603. }
  604. static int idtfc3_configure_hw(struct idtfc3 *idtfc3)
  605. {
  606. int err = 0;
  607. err = idtfc3_hw_calibrate(idtfc3);
  608. if (err)
  609. return err;
  610. err = idtfc3_enable_lpf(idtfc3, true);
  611. if (err)
  612. return err;
  613. err = idtfc3_enable_tdc(idtfc3, false, MEAS_MODE_INVALID);
  614. if (err)
  615. return err;
  616. err = idtfc3_get_tdc_offset_sign(idtfc3);
  617. if (err)
  618. return err;
  619. return idtfc3_setup_hw_param(idtfc3);
  620. }
  621. static int idtfc3_set_overhead(struct idtfc3 *idtfc3)
  622. {
  623. s64 current_ns = 0;
  624. s64 lowest_ns = 0;
  625. int err;
  626. u8 i;
  627. ktime_t start;
  628. ktime_t stop;
  629. ktime_t diff;
  630. char buf[18] = {0};
  631. for (i = 0; i < 5; i++) {
  632. start = ktime_get_raw();
  633. err = regmap_bulk_write(idtfc3->regmap, TOD_SYNC_LOAD_VAL_CTRL,
  634. &buf, sizeof(buf));
  635. if (err)
  636. return err;
  637. stop = ktime_get_raw();
  638. diff = ktime_sub(stop, start);
  639. current_ns = ktime_to_ns(diff);
  640. if (i == 0) {
  641. lowest_ns = current_ns;
  642. } else {
  643. if (current_ns < lowest_ns)
  644. lowest_ns = current_ns;
  645. }
  646. }
  647. idtfc3->tod_write_overhead = lowest_ns;
  648. return err;
  649. }
  650. static int idtfc3_enable_ptp(struct idtfc3 *idtfc3)
  651. {
  652. int err;
  653. idtfc3->caps = idtfc3_caps;
  654. snprintf(idtfc3->caps.name, sizeof(idtfc3->caps.name), "IDT FC3W");
  655. idtfc3->ptp_clock = ptp_clock_register(&idtfc3->caps, NULL);
  656. if (IS_ERR(idtfc3->ptp_clock)) {
  657. err = PTR_ERR(idtfc3->ptp_clock);
  658. idtfc3->ptp_clock = NULL;
  659. return err;
  660. }
  661. err = idtfc3_set_overhead(idtfc3);
  662. if (err)
  663. return err;
  664. err = idtfc3_init_timecounter(idtfc3);
  665. if (err)
  666. return err;
  667. dev_info(idtfc3->dev, "TIME_SYNC_CHANNEL registered as ptp%d",
  668. idtfc3->ptp_clock->index);
  669. return 0;
  670. }
  671. static int idtfc3_load_firmware(struct idtfc3 *idtfc3)
  672. {
  673. char fname[128] = FW_FILENAME;
  674. const struct firmware *fw;
  675. struct idtfc3_fwrc *rec;
  676. u16 addr;
  677. u8 val;
  678. int err;
  679. s32 len;
  680. idtfc3_default_hw_param(&idtfc3->hw_param);
  681. if (firmware) /* module parameter */
  682. snprintf(fname, sizeof(fname), "%s", firmware);
  683. dev_info(idtfc3->dev, "requesting firmware '%s'\n", fname);
  684. err = request_firmware(&fw, fname, idtfc3->dev);
  685. if (err) {
  686. dev_err(idtfc3->dev,
  687. "requesting firmware failed with err %d!\n", err);
  688. return err;
  689. }
  690. dev_dbg(idtfc3->dev, "firmware size %zu bytes\n", fw->size);
  691. rec = (struct idtfc3_fwrc *)fw->data;
  692. for (len = fw->size; len > 0; len -= sizeof(*rec)) {
  693. if (rec->reserved) {
  694. dev_err(idtfc3->dev,
  695. "bad firmware, reserved field non-zero\n");
  696. err = -EINVAL;
  697. } else {
  698. val = rec->value;
  699. addr = rec->hiaddr << 8 | rec->loaddr;
  700. rec++;
  701. err = idtfc3_set_hw_param(&idtfc3->hw_param, addr, val);
  702. }
  703. if (err != -EINVAL) {
  704. err = 0;
  705. /* Max register */
  706. if (addr >= 0xE88)
  707. continue;
  708. err = regmap_bulk_write(idtfc3->regmap, addr,
  709. &val, sizeof(val));
  710. }
  711. if (err)
  712. goto out;
  713. }
  714. err = idtfc3_configure_hw(idtfc3);
  715. out:
  716. release_firmware(fw);
  717. return err;
  718. }
  719. static int idtfc3_read_device_id(struct idtfc3 *idtfc3, u16 *device_id)
  720. {
  721. int err;
  722. u8 buf[2] = {0};
  723. err = regmap_bulk_read(idtfc3->regmap, DEVICE_ID,
  724. &buf, sizeof(buf));
  725. if (err) {
  726. dev_err(idtfc3->dev, "%s failed with %d", __func__, err);
  727. return err;
  728. }
  729. *device_id = get_unaligned_le16(buf);
  730. return 0;
  731. }
  732. static int idtfc3_check_device_compatibility(struct idtfc3 *idtfc3)
  733. {
  734. int err;
  735. u16 device_id;
  736. err = idtfc3_read_device_id(idtfc3, &device_id);
  737. if (err)
  738. return err;
  739. if ((device_id & DEVICE_ID_MASK) == 0) {
  740. dev_err(idtfc3->dev, "invalid device");
  741. return -EINVAL;
  742. }
  743. return 0;
  744. }
  745. static int idtfc3_probe(struct platform_device *pdev)
  746. {
  747. struct rsmu_ddata *ddata = dev_get_drvdata(pdev->dev.parent);
  748. struct idtfc3 *idtfc3;
  749. int err;
  750. idtfc3 = devm_kzalloc(&pdev->dev, sizeof(struct idtfc3), GFP_KERNEL);
  751. if (!idtfc3)
  752. return -ENOMEM;
  753. idtfc3->dev = &pdev->dev;
  754. idtfc3->mfd = pdev->dev.parent;
  755. idtfc3->lock = &ddata->lock;
  756. idtfc3->regmap = ddata->regmap;
  757. mutex_lock(idtfc3->lock);
  758. err = idtfc3_check_device_compatibility(idtfc3);
  759. if (err) {
  760. mutex_unlock(idtfc3->lock);
  761. return err;
  762. }
  763. err = idtfc3_load_firmware(idtfc3);
  764. if (err) {
  765. if (err == -ENOENT) {
  766. mutex_unlock(idtfc3->lock);
  767. return -EPROBE_DEFER;
  768. }
  769. dev_warn(idtfc3->dev, "loading firmware failed with %d", err);
  770. }
  771. err = idtfc3_enable_ptp(idtfc3);
  772. if (err) {
  773. dev_err(idtfc3->dev, "idtfc3_enable_ptp failed with %d", err);
  774. mutex_unlock(idtfc3->lock);
  775. return err;
  776. }
  777. mutex_unlock(idtfc3->lock);
  778. if (err) {
  779. ptp_clock_unregister(idtfc3->ptp_clock);
  780. return err;
  781. }
  782. platform_set_drvdata(pdev, idtfc3);
  783. return 0;
  784. }
  785. static void idtfc3_remove(struct platform_device *pdev)
  786. {
  787. struct idtfc3 *idtfc3 = platform_get_drvdata(pdev);
  788. ptp_clock_unregister(idtfc3->ptp_clock);
  789. }
  790. static struct platform_driver idtfc3_driver = {
  791. .driver = {
  792. .name = "rc38xxx-phc",
  793. },
  794. .probe = idtfc3_probe,
  795. .remove_new = idtfc3_remove,
  796. };
  797. module_platform_driver(idtfc3_driver);