gianfar_ptp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * PTP 1588 clock using the eTSEC
  3. *
  4. * Copyright (C) 2010 OMICRON electronics GmbH
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/device.h>
  22. #include <linux/hrtimer.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/timex.h>
  29. #include <linux/io.h>
  30. #include <linux/ptp_clock_kernel.h>
  31. #include "gianfar.h"
  32. /*
  33. * gianfar ptp registers
  34. * Generated by regen.tcl on Thu May 13 01:38:57 PM CEST 2010
  35. */
  36. struct gianfar_ptp_registers {
  37. u32 tmr_ctrl; /* Timer control register */
  38. u32 tmr_tevent; /* Timestamp event register */
  39. u32 tmr_temask; /* Timer event mask register */
  40. u32 tmr_pevent; /* Timestamp event register */
  41. u32 tmr_pemask; /* Timer event mask register */
  42. u32 tmr_stat; /* Timestamp status register */
  43. u32 tmr_cnt_h; /* Timer counter high register */
  44. u32 tmr_cnt_l; /* Timer counter low register */
  45. u32 tmr_add; /* Timer drift compensation addend register */
  46. u32 tmr_acc; /* Timer accumulator register */
  47. u32 tmr_prsc; /* Timer prescale */
  48. u8 res1[4];
  49. u32 tmroff_h; /* Timer offset high */
  50. u32 tmroff_l; /* Timer offset low */
  51. u8 res2[8];
  52. u32 tmr_alarm1_h; /* Timer alarm 1 high register */
  53. u32 tmr_alarm1_l; /* Timer alarm 1 high register */
  54. u32 tmr_alarm2_h; /* Timer alarm 2 high register */
  55. u32 tmr_alarm2_l; /* Timer alarm 2 high register */
  56. u8 res3[48];
  57. u32 tmr_fiper1; /* Timer fixed period interval */
  58. u32 tmr_fiper2; /* Timer fixed period interval */
  59. u32 tmr_fiper3; /* Timer fixed period interval */
  60. u8 res4[20];
  61. u32 tmr_etts1_h; /* Timestamp of general purpose external trigger */
  62. u32 tmr_etts1_l; /* Timestamp of general purpose external trigger */
  63. u32 tmr_etts2_h; /* Timestamp of general purpose external trigger */
  64. u32 tmr_etts2_l; /* Timestamp of general purpose external trigger */
  65. };
  66. /* Bit definitions for the TMR_CTRL register */
  67. #define ALM1P (1<<31) /* Alarm1 output polarity */
  68. #define ALM2P (1<<30) /* Alarm2 output polarity */
  69. #define FS (1<<28) /* FIPER start indication */
  70. #define PP1L (1<<27) /* Fiper1 pulse loopback mode enabled. */
  71. #define PP2L (1<<26) /* Fiper2 pulse loopback mode enabled. */
  72. #define TCLK_PERIOD_SHIFT (16) /* 1588 timer reference clock period. */
  73. #define TCLK_PERIOD_MASK (0x3ff)
  74. #define RTPE (1<<15) /* Record Tx Timestamp to PAL Enable. */
  75. #define FRD (1<<14) /* FIPER Realignment Disable */
  76. #define ESFDP (1<<11) /* External Tx/Rx SFD Polarity. */
  77. #define ESFDE (1<<10) /* External Tx/Rx SFD Enable. */
  78. #define ETEP2 (1<<9) /* External trigger 2 edge polarity */
  79. #define ETEP1 (1<<8) /* External trigger 1 edge polarity */
  80. #define COPH (1<<7) /* Generated clock output phase. */
  81. #define CIPH (1<<6) /* External oscillator input clock phase */
  82. #define TMSR (1<<5) /* Timer soft reset. */
  83. #define BYP (1<<3) /* Bypass drift compensated clock */
  84. #define TE (1<<2) /* 1588 timer enable. */
  85. #define CKSEL_SHIFT (0) /* 1588 Timer reference clock source */
  86. #define CKSEL_MASK (0x3)
  87. /* Bit definitions for the TMR_TEVENT register */
  88. #define ETS2 (1<<25) /* External trigger 2 timestamp sampled */
  89. #define ETS1 (1<<24) /* External trigger 1 timestamp sampled */
  90. #define ALM2 (1<<17) /* Current time = alarm time register 2 */
  91. #define ALM1 (1<<16) /* Current time = alarm time register 1 */
  92. #define PP1 (1<<7) /* periodic pulse generated on FIPER1 */
  93. #define PP2 (1<<6) /* periodic pulse generated on FIPER2 */
  94. #define PP3 (1<<5) /* periodic pulse generated on FIPER3 */
  95. /* Bit definitions for the TMR_TEMASK register */
  96. #define ETS2EN (1<<25) /* External trigger 2 timestamp enable */
  97. #define ETS1EN (1<<24) /* External trigger 1 timestamp enable */
  98. #define ALM2EN (1<<17) /* Timer ALM2 event enable */
  99. #define ALM1EN (1<<16) /* Timer ALM1 event enable */
  100. #define PP1EN (1<<7) /* Periodic pulse event 1 enable */
  101. #define PP2EN (1<<6) /* Periodic pulse event 2 enable */
  102. /* Bit definitions for the TMR_PEVENT register */
  103. #define TXP2 (1<<9) /* PTP transmitted timestamp im TXTS2 */
  104. #define TXP1 (1<<8) /* PTP transmitted timestamp in TXTS1 */
  105. #define RXP (1<<0) /* PTP frame has been received */
  106. /* Bit definitions for the TMR_PEMASK register */
  107. #define TXP2EN (1<<9) /* Transmit PTP packet event 2 enable */
  108. #define TXP1EN (1<<8) /* Transmit PTP packet event 1 enable */
  109. #define RXPEN (1<<0) /* Receive PTP packet event enable */
  110. /* Bit definitions for the TMR_STAT register */
  111. #define STAT_VEC_SHIFT (0) /* Timer general purpose status vector */
  112. #define STAT_VEC_MASK (0x3f)
  113. /* Bit definitions for the TMR_PRSC register */
  114. #define PRSC_OCK_SHIFT (0) /* Output clock division/prescale factor. */
  115. #define PRSC_OCK_MASK (0xffff)
  116. #define DRIVER "gianfar_ptp"
  117. #define DEFAULT_CKSEL 1
  118. #define N_EXT_TS 2
  119. #define REG_SIZE sizeof(struct gianfar_ptp_registers)
  120. struct etsects {
  121. struct gianfar_ptp_registers __iomem *regs;
  122. spinlock_t lock; /* protects regs */
  123. struct ptp_clock *clock;
  124. struct ptp_clock_info caps;
  125. struct resource *rsrc;
  126. int irq;
  127. u64 alarm_interval; /* for periodic alarm */
  128. u64 alarm_value;
  129. u32 tclk_period; /* nanoseconds */
  130. u32 tmr_prsc;
  131. u32 tmr_add;
  132. u32 cksel;
  133. u32 tmr_fiper1;
  134. u32 tmr_fiper2;
  135. };
  136. /*
  137. * Register access functions
  138. */
  139. /* Caller must hold etsects->lock. */
  140. static u64 tmr_cnt_read(struct etsects *etsects)
  141. {
  142. u64 ns;
  143. u32 lo, hi;
  144. lo = gfar_read(&etsects->regs->tmr_cnt_l);
  145. hi = gfar_read(&etsects->regs->tmr_cnt_h);
  146. ns = ((u64) hi) << 32;
  147. ns |= lo;
  148. return ns;
  149. }
  150. /* Caller must hold etsects->lock. */
  151. static void tmr_cnt_write(struct etsects *etsects, u64 ns)
  152. {
  153. u32 hi = ns >> 32;
  154. u32 lo = ns & 0xffffffff;
  155. gfar_write(&etsects->regs->tmr_cnt_l, lo);
  156. gfar_write(&etsects->regs->tmr_cnt_h, hi);
  157. }
  158. /* Caller must hold etsects->lock. */
  159. static void set_alarm(struct etsects *etsects)
  160. {
  161. u64 ns;
  162. u32 lo, hi;
  163. ns = tmr_cnt_read(etsects) + 1500000000ULL;
  164. ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
  165. ns -= etsects->tclk_period;
  166. hi = ns >> 32;
  167. lo = ns & 0xffffffff;
  168. gfar_write(&etsects->regs->tmr_alarm1_l, lo);
  169. gfar_write(&etsects->regs->tmr_alarm1_h, hi);
  170. }
  171. /* Caller must hold etsects->lock. */
  172. static void set_fipers(struct etsects *etsects)
  173. {
  174. set_alarm(etsects);
  175. gfar_write(&etsects->regs->tmr_fiper1, etsects->tmr_fiper1);
  176. gfar_write(&etsects->regs->tmr_fiper2, etsects->tmr_fiper2);
  177. }
  178. /*
  179. * Interrupt service routine
  180. */
  181. static irqreturn_t isr(int irq, void *priv)
  182. {
  183. struct etsects *etsects = priv;
  184. struct ptp_clock_event event;
  185. u64 ns;
  186. u32 ack = 0, lo, hi, mask, val;
  187. val = gfar_read(&etsects->regs->tmr_tevent);
  188. if (val & ETS1) {
  189. ack |= ETS1;
  190. hi = gfar_read(&etsects->regs->tmr_etts1_h);
  191. lo = gfar_read(&etsects->regs->tmr_etts1_l);
  192. event.type = PTP_CLOCK_EXTTS;
  193. event.index = 0;
  194. event.timestamp = ((u64) hi) << 32;
  195. event.timestamp |= lo;
  196. ptp_clock_event(etsects->clock, &event);
  197. }
  198. if (val & ETS2) {
  199. ack |= ETS2;
  200. hi = gfar_read(&etsects->regs->tmr_etts2_h);
  201. lo = gfar_read(&etsects->regs->tmr_etts2_l);
  202. event.type = PTP_CLOCK_EXTTS;
  203. event.index = 1;
  204. event.timestamp = ((u64) hi) << 32;
  205. event.timestamp |= lo;
  206. ptp_clock_event(etsects->clock, &event);
  207. }
  208. if (val & ALM2) {
  209. ack |= ALM2;
  210. if (etsects->alarm_value) {
  211. event.type = PTP_CLOCK_ALARM;
  212. event.index = 0;
  213. event.timestamp = etsects->alarm_value;
  214. ptp_clock_event(etsects->clock, &event);
  215. }
  216. if (etsects->alarm_interval) {
  217. ns = etsects->alarm_value + etsects->alarm_interval;
  218. hi = ns >> 32;
  219. lo = ns & 0xffffffff;
  220. spin_lock(&etsects->lock);
  221. gfar_write(&etsects->regs->tmr_alarm2_l, lo);
  222. gfar_write(&etsects->regs->tmr_alarm2_h, hi);
  223. spin_unlock(&etsects->lock);
  224. etsects->alarm_value = ns;
  225. } else {
  226. gfar_write(&etsects->regs->tmr_tevent, ALM2);
  227. spin_lock(&etsects->lock);
  228. mask = gfar_read(&etsects->regs->tmr_temask);
  229. mask &= ~ALM2EN;
  230. gfar_write(&etsects->regs->tmr_temask, mask);
  231. spin_unlock(&etsects->lock);
  232. etsects->alarm_value = 0;
  233. etsects->alarm_interval = 0;
  234. }
  235. }
  236. if (val & PP1) {
  237. ack |= PP1;
  238. event.type = PTP_CLOCK_PPS;
  239. ptp_clock_event(etsects->clock, &event);
  240. }
  241. if (ack) {
  242. gfar_write(&etsects->regs->tmr_tevent, ack);
  243. return IRQ_HANDLED;
  244. } else
  245. return IRQ_NONE;
  246. }
  247. /*
  248. * PTP clock operations
  249. */
  250. static int ptp_gianfar_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  251. {
  252. u64 adj;
  253. u32 diff, tmr_add;
  254. int neg_adj = 0;
  255. struct etsects *etsects = container_of(ptp, struct etsects, caps);
  256. if (ppb < 0) {
  257. neg_adj = 1;
  258. ppb = -ppb;
  259. }
  260. tmr_add = etsects->tmr_add;
  261. adj = tmr_add;
  262. adj *= ppb;
  263. diff = div_u64(adj, 1000000000ULL);
  264. tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
  265. gfar_write(&etsects->regs->tmr_add, tmr_add);
  266. return 0;
  267. }
  268. static int ptp_gianfar_adjtime(struct ptp_clock_info *ptp, s64 delta)
  269. {
  270. s64 now;
  271. unsigned long flags;
  272. struct etsects *etsects = container_of(ptp, struct etsects, caps);
  273. spin_lock_irqsave(&etsects->lock, flags);
  274. now = tmr_cnt_read(etsects);
  275. now += delta;
  276. tmr_cnt_write(etsects, now);
  277. set_fipers(etsects);
  278. spin_unlock_irqrestore(&etsects->lock, flags);
  279. return 0;
  280. }
  281. static int ptp_gianfar_gettime(struct ptp_clock_info *ptp,
  282. struct timespec64 *ts)
  283. {
  284. u64 ns;
  285. unsigned long flags;
  286. struct etsects *etsects = container_of(ptp, struct etsects, caps);
  287. spin_lock_irqsave(&etsects->lock, flags);
  288. ns = tmr_cnt_read(etsects);
  289. spin_unlock_irqrestore(&etsects->lock, flags);
  290. *ts = ns_to_timespec64(ns);
  291. return 0;
  292. }
  293. static int ptp_gianfar_settime(struct ptp_clock_info *ptp,
  294. const struct timespec64 *ts)
  295. {
  296. u64 ns;
  297. unsigned long flags;
  298. struct etsects *etsects = container_of(ptp, struct etsects, caps);
  299. ns = timespec64_to_ns(ts);
  300. spin_lock_irqsave(&etsects->lock, flags);
  301. tmr_cnt_write(etsects, ns);
  302. set_fipers(etsects);
  303. spin_unlock_irqrestore(&etsects->lock, flags);
  304. return 0;
  305. }
  306. static int ptp_gianfar_enable(struct ptp_clock_info *ptp,
  307. struct ptp_clock_request *rq, int on)
  308. {
  309. struct etsects *etsects = container_of(ptp, struct etsects, caps);
  310. unsigned long flags;
  311. u32 bit, mask;
  312. switch (rq->type) {
  313. case PTP_CLK_REQ_EXTTS:
  314. switch (rq->extts.index) {
  315. case 0:
  316. bit = ETS1EN;
  317. break;
  318. case 1:
  319. bit = ETS2EN;
  320. break;
  321. default:
  322. return -EINVAL;
  323. }
  324. spin_lock_irqsave(&etsects->lock, flags);
  325. mask = gfar_read(&etsects->regs->tmr_temask);
  326. if (on)
  327. mask |= bit;
  328. else
  329. mask &= ~bit;
  330. gfar_write(&etsects->regs->tmr_temask, mask);
  331. spin_unlock_irqrestore(&etsects->lock, flags);
  332. return 0;
  333. case PTP_CLK_REQ_PPS:
  334. spin_lock_irqsave(&etsects->lock, flags);
  335. mask = gfar_read(&etsects->regs->tmr_temask);
  336. if (on)
  337. mask |= PP1EN;
  338. else
  339. mask &= ~PP1EN;
  340. gfar_write(&etsects->regs->tmr_temask, mask);
  341. spin_unlock_irqrestore(&etsects->lock, flags);
  342. return 0;
  343. default:
  344. break;
  345. }
  346. return -EOPNOTSUPP;
  347. }
  348. static struct ptp_clock_info ptp_gianfar_caps = {
  349. .owner = THIS_MODULE,
  350. .name = "gianfar clock",
  351. .max_adj = 512000,
  352. .n_alarm = 0,
  353. .n_ext_ts = N_EXT_TS,
  354. .n_per_out = 0,
  355. .n_pins = 0,
  356. .pps = 1,
  357. .adjfreq = ptp_gianfar_adjfreq,
  358. .adjtime = ptp_gianfar_adjtime,
  359. .gettime64 = ptp_gianfar_gettime,
  360. .settime64 = ptp_gianfar_settime,
  361. .enable = ptp_gianfar_enable,
  362. };
  363. /* OF device tree */
  364. static int get_of_u32(struct device_node *node, char *str, u32 *val)
  365. {
  366. int plen;
  367. const u32 *prop = of_get_property(node, str, &plen);
  368. if (!prop || plen != sizeof(*prop))
  369. return -1;
  370. *val = *prop;
  371. return 0;
  372. }
  373. static int gianfar_ptp_probe(struct platform_device *dev)
  374. {
  375. struct device_node *node = dev->dev.of_node;
  376. struct etsects *etsects;
  377. struct timespec64 now;
  378. int err = -ENOMEM;
  379. u32 tmr_ctrl;
  380. unsigned long flags;
  381. etsects = kzalloc(sizeof(*etsects), GFP_KERNEL);
  382. if (!etsects)
  383. goto no_memory;
  384. err = -ENODEV;
  385. etsects->caps = ptp_gianfar_caps;
  386. if (get_of_u32(node, "fsl,cksel", &etsects->cksel))
  387. etsects->cksel = DEFAULT_CKSEL;
  388. if (get_of_u32(node, "fsl,tclk-period", &etsects->tclk_period) ||
  389. get_of_u32(node, "fsl,tmr-prsc", &etsects->tmr_prsc) ||
  390. get_of_u32(node, "fsl,tmr-add", &etsects->tmr_add) ||
  391. get_of_u32(node, "fsl,tmr-fiper1", &etsects->tmr_fiper1) ||
  392. get_of_u32(node, "fsl,tmr-fiper2", &etsects->tmr_fiper2) ||
  393. get_of_u32(node, "fsl,max-adj", &etsects->caps.max_adj)) {
  394. pr_err("device tree node missing required elements\n");
  395. goto no_node;
  396. }
  397. etsects->irq = platform_get_irq(dev, 0);
  398. if (etsects->irq < 0) {
  399. pr_err("irq not in device tree\n");
  400. goto no_node;
  401. }
  402. if (request_irq(etsects->irq, isr, 0, DRIVER, etsects)) {
  403. pr_err("request_irq failed\n");
  404. goto no_node;
  405. }
  406. etsects->rsrc = platform_get_resource(dev, IORESOURCE_MEM, 0);
  407. if (!etsects->rsrc) {
  408. pr_err("no resource\n");
  409. goto no_resource;
  410. }
  411. if (request_resource(&iomem_resource, etsects->rsrc)) {
  412. pr_err("resource busy\n");
  413. goto no_resource;
  414. }
  415. spin_lock_init(&etsects->lock);
  416. etsects->regs = ioremap(etsects->rsrc->start,
  417. resource_size(etsects->rsrc));
  418. if (!etsects->regs) {
  419. pr_err("ioremap ptp registers failed\n");
  420. goto no_ioremap;
  421. }
  422. getnstimeofday64(&now);
  423. ptp_gianfar_settime(&etsects->caps, &now);
  424. tmr_ctrl =
  425. (etsects->tclk_period & TCLK_PERIOD_MASK) << TCLK_PERIOD_SHIFT |
  426. (etsects->cksel & CKSEL_MASK) << CKSEL_SHIFT;
  427. spin_lock_irqsave(&etsects->lock, flags);
  428. gfar_write(&etsects->regs->tmr_ctrl, tmr_ctrl);
  429. gfar_write(&etsects->regs->tmr_add, etsects->tmr_add);
  430. gfar_write(&etsects->regs->tmr_prsc, etsects->tmr_prsc);
  431. gfar_write(&etsects->regs->tmr_fiper1, etsects->tmr_fiper1);
  432. gfar_write(&etsects->regs->tmr_fiper2, etsects->tmr_fiper2);
  433. set_alarm(etsects);
  434. gfar_write(&etsects->regs->tmr_ctrl, tmr_ctrl|FS|RTPE|TE|FRD);
  435. spin_unlock_irqrestore(&etsects->lock, flags);
  436. etsects->clock = ptp_clock_register(&etsects->caps, &dev->dev);
  437. if (IS_ERR(etsects->clock)) {
  438. err = PTR_ERR(etsects->clock);
  439. goto no_clock;
  440. }
  441. gfar_phc_index = ptp_clock_index(etsects->clock);
  442. platform_set_drvdata(dev, etsects);
  443. return 0;
  444. no_clock:
  445. iounmap(etsects->regs);
  446. no_ioremap:
  447. release_resource(etsects->rsrc);
  448. no_resource:
  449. free_irq(etsects->irq, etsects);
  450. no_node:
  451. kfree(etsects);
  452. no_memory:
  453. return err;
  454. }
  455. static int gianfar_ptp_remove(struct platform_device *dev)
  456. {
  457. struct etsects *etsects = platform_get_drvdata(dev);
  458. gfar_write(&etsects->regs->tmr_temask, 0);
  459. gfar_write(&etsects->regs->tmr_ctrl, 0);
  460. gfar_phc_index = -1;
  461. ptp_clock_unregister(etsects->clock);
  462. iounmap(etsects->regs);
  463. release_resource(etsects->rsrc);
  464. free_irq(etsects->irq, etsects);
  465. kfree(etsects);
  466. return 0;
  467. }
  468. static const struct of_device_id match_table[] = {
  469. { .compatible = "fsl,etsec-ptp" },
  470. {},
  471. };
  472. MODULE_DEVICE_TABLE(of, match_table);
  473. static struct platform_driver gianfar_ptp_driver = {
  474. .driver = {
  475. .name = "gianfar_ptp",
  476. .of_match_table = match_table,
  477. },
  478. .probe = gianfar_ptp_probe,
  479. .remove = gianfar_ptp_remove,
  480. };
  481. module_platform_driver(gianfar_ptp_driver);
  482. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
  483. MODULE_DESCRIPTION("PTP clock using the eTSEC");
  484. MODULE_LICENSE("GPL");