1 // SPDX-License-Identifier: GPL-2.0 2 /* TI K3 AM65x Common Platform Time Sync 3 * 4 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com 5 * 6 */ 7 8 #include <linux/clk.h> 9 #include <linux/clk-provider.h> 10 #include <linux/err.h> 11 #include <linux/if_vlan.h> 12 #include <linux/interrupt.h> 13 #include <linux/module.h> 14 #include <linux/netdevice.h> 15 #include <linux/net_tstamp.h> 16 #include <linux/of.h> 17 #include <linux/of_irq.h> 18 #include <linux/platform_device.h> 19 #include <linux/pm_runtime.h> 20 #include <linux/ptp_classify.h> 21 #include <linux/ptp_clock_kernel.h> 22 23 #include "am65-cpts.h" 24 25 struct am65_genf_regs { 26 u32 comp_lo; /* Comparison Low Value 0:31 */ 27 u32 comp_hi; /* Comparison High Value 32:63 */ 28 u32 control; /* control */ 29 u32 length; /* Length */ 30 u32 ppm_low; /* PPM Load Low Value 0:31 */ 31 u32 ppm_hi; /* PPM Load High Value 32:63 */ 32 u32 ts_nudge; /* Nudge value */ 33 } __aligned(32) __packed; 34 35 #define AM65_CPTS_GENF_MAX_NUM 9 36 #define AM65_CPTS_ESTF_MAX_NUM 8 37 38 struct am65_cpts_regs { 39 u32 idver; /* Identification and version */ 40 u32 control; /* Time sync control */ 41 u32 rftclk_sel; /* Reference Clock Select Register */ 42 u32 ts_push; /* Time stamp event push */ 43 u32 ts_load_val_lo; /* Time Stamp Load Low Value 0:31 */ 44 u32 ts_load_en; /* Time stamp load enable */ 45 u32 ts_comp_lo; /* Time Stamp Comparison Low Value 0:31 */ 46 u32 ts_comp_length; /* Time Stamp Comparison Length */ 47 u32 intstat_raw; /* Time sync interrupt status raw */ 48 u32 intstat_masked; /* Time sync interrupt status masked */ 49 u32 int_enable; /* Time sync interrupt enable */ 50 u32 ts_comp_nudge; /* Time Stamp Comparison Nudge Value */ 51 u32 event_pop; /* Event interrupt pop */ 52 u32 event_0; /* Event Time Stamp lo 0:31 */ 53 u32 event_1; /* Event Type Fields */ 54 u32 event_2; /* Event Type Fields domain */ 55 u32 event_3; /* Event Time Stamp hi 32:63 */ 56 u32 ts_load_val_hi; /* Time Stamp Load High Value 32:63 */ 57 u32 ts_comp_hi; /* Time Stamp Comparison High Value 32:63 */ 58 u32 ts_add_val; /* Time Stamp Add value */ 59 u32 ts_ppm_low; /* Time Stamp PPM Load Low Value 0:31 */ 60 u32 ts_ppm_hi; /* Time Stamp PPM Load High Value 32:63 */ 61 u32 ts_nudge; /* Time Stamp Nudge value */ 62 u32 reserv[33]; 63 struct am65_genf_regs genf[AM65_CPTS_GENF_MAX_NUM]; 64 struct am65_genf_regs estf[AM65_CPTS_ESTF_MAX_NUM]; 65 }; 66 67 /* CONTROL_REG */ 68 #define AM65_CPTS_CONTROL_EN BIT(0) 69 #define AM65_CPTS_CONTROL_INT_TEST BIT(1) 70 #define AM65_CPTS_CONTROL_TS_COMP_POLARITY BIT(2) 71 #define AM65_CPTS_CONTROL_TSTAMP_EN BIT(3) 72 #define AM65_CPTS_CONTROL_SEQUENCE_EN BIT(4) 73 #define AM65_CPTS_CONTROL_64MODE BIT(5) 74 #define AM65_CPTS_CONTROL_TS_COMP_TOG BIT(6) 75 #define AM65_CPTS_CONTROL_TS_PPM_DIR BIT(7) 76 #define AM65_CPTS_CONTROL_HW1_TS_PUSH_EN BIT(8) 77 #define AM65_CPTS_CONTROL_HW2_TS_PUSH_EN BIT(9) 78 #define AM65_CPTS_CONTROL_HW3_TS_PUSH_EN BIT(10) 79 #define AM65_CPTS_CONTROL_HW4_TS_PUSH_EN BIT(11) 80 #define AM65_CPTS_CONTROL_HW5_TS_PUSH_EN BIT(12) 81 #define AM65_CPTS_CONTROL_HW6_TS_PUSH_EN BIT(13) 82 #define AM65_CPTS_CONTROL_HW7_TS_PUSH_EN BIT(14) 83 #define AM65_CPTS_CONTROL_HW8_TS_PUSH_EN BIT(15) 84 #define AM65_CPTS_CONTROL_HW1_TS_PUSH_OFFSET (8) 85 86 #define AM65_CPTS_CONTROL_TX_GENF_CLR_EN BIT(17) 87 88 #define AM65_CPTS_CONTROL_TS_SYNC_SEL_MASK (0xF) 89 #define AM65_CPTS_CONTROL_TS_SYNC_SEL_SHIFT (28) 90 91 /* RFTCLK_SEL_REG */ 92 #define AM65_CPTS_RFTCLK_SEL_MASK (0x1F) 93 94 /* TS_PUSH_REG */ 95 #define AM65_CPTS_TS_PUSH BIT(0) 96 97 /* TS_LOAD_EN_REG */ 98 #define AM65_CPTS_TS_LOAD_EN BIT(0) 99 100 /* INTSTAT_RAW_REG */ 101 #define AM65_CPTS_INTSTAT_RAW_TS_PEND BIT(0) 102 103 /* INTSTAT_MASKED_REG */ 104 #define AM65_CPTS_INTSTAT_MASKED_TS_PEND BIT(0) 105 106 /* INT_ENABLE_REG */ 107 #define AM65_CPTS_INT_ENABLE_TS_PEND_EN BIT(0) 108 109 /* TS_COMP_NUDGE_REG */ 110 #define AM65_CPTS_TS_COMP_NUDGE_MASK (0xFF) 111 112 /* EVENT_POP_REG */ 113 #define AM65_CPTS_EVENT_POP BIT(0) 114 115 /* EVENT_1_REG */ 116 #define AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK GENMASK(15, 0) 117 118 #define AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK GENMASK(19, 16) 119 #define AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT (16) 120 121 #define AM65_CPTS_EVENT_1_EVENT_TYPE_MASK GENMASK(23, 20) 122 #define AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT (20) 123 124 #define AM65_CPTS_EVENT_1_PORT_NUMBER_MASK GENMASK(28, 24) 125 #define AM65_CPTS_EVENT_1_PORT_NUMBER_SHIFT (24) 126 127 /* EVENT_2_REG */ 128 #define AM65_CPTS_EVENT_2_REG_DOMAIN_MASK (0xFF) 129 #define AM65_CPTS_EVENT_2_REG_DOMAIN_SHIFT (0) 130 131 enum { 132 AM65_CPTS_EV_PUSH, /* Time Stamp Push Event */ 133 AM65_CPTS_EV_ROLL, /* Time Stamp Rollover Event */ 134 AM65_CPTS_EV_HALF, /* Time Stamp Half Rollover Event */ 135 AM65_CPTS_EV_HW, /* Hardware Time Stamp Push Event */ 136 AM65_CPTS_EV_RX, /* Ethernet Receive Event */ 137 AM65_CPTS_EV_TX, /* Ethernet Transmit Event */ 138 AM65_CPTS_EV_TS_COMP, /* Time Stamp Compare Event */ 139 AM65_CPTS_EV_HOST, /* Host Transmit Event */ 140 }; 141 142 struct am65_cpts_event { 143 struct list_head list; 144 unsigned long tmo; 145 u32 event1; 146 u32 event2; 147 u64 timestamp; 148 }; 149 150 #define AM65_CPTS_FIFO_DEPTH (16) 151 #define AM65_CPTS_MAX_EVENTS (32) 152 #define AM65_CPTS_EVENT_RX_TX_TIMEOUT (20) /* ms */ 153 #define AM65_CPTS_SKB_TX_WORK_TIMEOUT 1 /* jiffies */ 154 #define AM65_CPTS_MIN_PPM 0x400 155 156 struct am65_cpts { 157 struct device *dev; 158 struct am65_cpts_regs __iomem *reg; 159 struct ptp_clock_info ptp_info; 160 struct ptp_clock *ptp_clock; 161 int phc_index; 162 struct clk_hw *clk_mux_hw; 163 struct device_node *clk_mux_np; 164 struct clk *refclk; 165 u32 refclk_freq; 166 struct list_head events; 167 struct list_head pool; 168 struct am65_cpts_event pool_data[AM65_CPTS_MAX_EVENTS]; 169 spinlock_t lock; /* protects events lists*/ 170 u32 ext_ts_inputs; 171 u32 genf_num; 172 u32 ts_add_val; 173 int irq; 174 struct mutex ptp_clk_lock; /* PHC access sync */ 175 u64 timestamp; 176 u32 genf_enable; 177 u32 hw_ts_enable; 178 struct sk_buff_head txq; 179 bool pps_enabled; 180 bool pps_present; 181 u32 pps_hw_ts_idx; 182 u32 pps_genf_idx; 183 /* context save/restore */ 184 u64 sr_cpts_ns; 185 u64 sr_ktime_ns; 186 u32 sr_control; 187 u32 sr_int_enable; 188 u32 sr_rftclk_sel; 189 u32 sr_ts_ppm_hi; 190 u32 sr_ts_ppm_low; 191 struct am65_genf_regs sr_genf[AM65_CPTS_GENF_MAX_NUM]; 192 struct am65_genf_regs sr_estf[AM65_CPTS_ESTF_MAX_NUM]; 193 }; 194 195 struct am65_cpts_skb_cb_data { 196 unsigned long tmo; 197 u32 skb_mtype_seqid; 198 }; 199 200 #define am65_cpts_write32(c, v, r) writel(v, &(c)->reg->r) 201 #define am65_cpts_read32(c, r) readl(&(c)->reg->r) 202 203 static void am65_cpts_settime(struct am65_cpts *cpts, u64 start_tstamp) 204 { 205 u32 val; 206 207 val = upper_32_bits(start_tstamp); 208 am65_cpts_write32(cpts, val, ts_load_val_hi); 209 val = lower_32_bits(start_tstamp); 210 am65_cpts_write32(cpts, val, ts_load_val_lo); 211 212 am65_cpts_write32(cpts, AM65_CPTS_TS_LOAD_EN, ts_load_en); 213 } 214 215 static void am65_cpts_set_add_val(struct am65_cpts *cpts) 216 { 217 /* select coefficient according to the rate */ 218 cpts->ts_add_val = (NSEC_PER_SEC / cpts->refclk_freq - 1) & 0x7; 219 220 am65_cpts_write32(cpts, cpts->ts_add_val, ts_add_val); 221 } 222 223 static void am65_cpts_disable(struct am65_cpts *cpts) 224 { 225 am65_cpts_write32(cpts, 0, control); 226 am65_cpts_write32(cpts, 0, int_enable); 227 } 228 229 static int am65_cpts_event_get_port(struct am65_cpts_event *event) 230 { 231 return (event->event1 & AM65_CPTS_EVENT_1_PORT_NUMBER_MASK) >> 232 AM65_CPTS_EVENT_1_PORT_NUMBER_SHIFT; 233 } 234 235 static int am65_cpts_event_get_type(struct am65_cpts_event *event) 236 { 237 return (event->event1 & AM65_CPTS_EVENT_1_EVENT_TYPE_MASK) >> 238 AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT; 239 } 240 241 static int am65_cpts_cpts_purge_events(struct am65_cpts *cpts) 242 { 243 struct list_head *this, *next; 244 struct am65_cpts_event *event; 245 int removed = 0; 246 247 list_for_each_safe(this, next, &cpts->events) { 248 event = list_entry(this, struct am65_cpts_event, list); 249 if (time_after(jiffies, event->tmo)) { 250 list_del_init(&event->list); 251 list_add(&event->list, &cpts->pool); 252 ++removed; 253 } 254 } 255 256 if (removed) 257 dev_dbg(cpts->dev, "event pool cleaned up %d\n", removed); 258 return removed ? 0 : -1; 259 } 260 261 static bool am65_cpts_fifo_pop_event(struct am65_cpts *cpts, 262 struct am65_cpts_event *event) 263 { 264 u32 r = am65_cpts_read32(cpts, intstat_raw); 265 266 if (r & AM65_CPTS_INTSTAT_RAW_TS_PEND) { 267 event->timestamp = am65_cpts_read32(cpts, event_0); 268 event->event1 = am65_cpts_read32(cpts, event_1); 269 event->event2 = am65_cpts_read32(cpts, event_2); 270 event->timestamp |= (u64)am65_cpts_read32(cpts, event_3) << 32; 271 am65_cpts_write32(cpts, AM65_CPTS_EVENT_POP, event_pop); 272 return false; 273 } 274 return true; 275 } 276 277 static int am65_cpts_fifo_read(struct am65_cpts *cpts) 278 { 279 struct ptp_clock_event pevent; 280 struct am65_cpts_event *event; 281 bool schedule = false; 282 int i, type, ret = 0; 283 unsigned long flags; 284 285 spin_lock_irqsave(&cpts->lock, flags); 286 for (i = 0; i < AM65_CPTS_FIFO_DEPTH; i++) { 287 event = list_first_entry_or_null(&cpts->pool, 288 struct am65_cpts_event, list); 289 290 if (!event) { 291 if (am65_cpts_cpts_purge_events(cpts)) { 292 dev_err(cpts->dev, "cpts: event pool empty\n"); 293 ret = -1; 294 goto out; 295 } 296 continue; 297 } 298 299 if (am65_cpts_fifo_pop_event(cpts, event)) 300 break; 301 302 type = am65_cpts_event_get_type(event); 303 switch (type) { 304 case AM65_CPTS_EV_PUSH: 305 cpts->timestamp = event->timestamp; 306 dev_dbg(cpts->dev, "AM65_CPTS_EV_PUSH t:%llu\n", 307 cpts->timestamp); 308 break; 309 case AM65_CPTS_EV_RX: 310 case AM65_CPTS_EV_TX: 311 event->tmo = jiffies + 312 msecs_to_jiffies(AM65_CPTS_EVENT_RX_TX_TIMEOUT); 313 314 list_del_init(&event->list); 315 list_add_tail(&event->list, &cpts->events); 316 317 dev_dbg(cpts->dev, 318 "AM65_CPTS_EV_TX e1:%08x e2:%08x t:%lld\n", 319 event->event1, event->event2, 320 event->timestamp); 321 schedule = true; 322 break; 323 case AM65_CPTS_EV_HW: 324 pevent.index = am65_cpts_event_get_port(event) - 1; 325 pevent.timestamp = event->timestamp; 326 if (cpts->pps_enabled && pevent.index == cpts->pps_hw_ts_idx) { 327 pevent.type = PTP_CLOCK_PPSUSR; 328 pevent.pps_times.ts_real = ns_to_timespec64(pevent.timestamp); 329 } else { 330 pevent.type = PTP_CLOCK_EXTTS; 331 } 332 dev_dbg(cpts->dev, "AM65_CPTS_EV_HW:%s p:%d t:%llu\n", 333 pevent.type == PTP_CLOCK_EXTTS ? 334 "extts" : "pps", 335 pevent.index, event->timestamp); 336 337 ptp_clock_event(cpts->ptp_clock, &pevent); 338 break; 339 case AM65_CPTS_EV_HOST: 340 break; 341 case AM65_CPTS_EV_ROLL: 342 case AM65_CPTS_EV_HALF: 343 case AM65_CPTS_EV_TS_COMP: 344 dev_dbg(cpts->dev, 345 "AM65_CPTS_EVT: %d e1:%08x e2:%08x t:%lld\n", 346 type, 347 event->event1, event->event2, 348 event->timestamp); 349 break; 350 default: 351 dev_err(cpts->dev, "cpts: unknown event type\n"); 352 ret = -1; 353 goto out; 354 } 355 } 356 357 out: 358 spin_unlock_irqrestore(&cpts->lock, flags); 359 360 if (schedule) 361 ptp_schedule_worker(cpts->ptp_clock, 0); 362 363 return ret; 364 } 365 366 static u64 am65_cpts_gettime(struct am65_cpts *cpts, 367 struct ptp_system_timestamp *sts) 368 { 369 unsigned long flags; 370 u64 val = 0; 371 372 /* temporarily disable cpts interrupt to avoid intentional 373 * doubled read. Interrupt can be in-flight - it's Ok. 374 */ 375 am65_cpts_write32(cpts, 0, int_enable); 376 377 /* use spin_lock_irqsave() here as it has to run very fast */ 378 spin_lock_irqsave(&cpts->lock, flags); 379 ptp_read_system_prets(sts); 380 am65_cpts_write32(cpts, AM65_CPTS_TS_PUSH, ts_push); 381 am65_cpts_read32(cpts, ts_push); 382 ptp_read_system_postts(sts); 383 spin_unlock_irqrestore(&cpts->lock, flags); 384 385 am65_cpts_fifo_read(cpts); 386 387 am65_cpts_write32(cpts, AM65_CPTS_INT_ENABLE_TS_PEND_EN, int_enable); 388 389 val = cpts->timestamp; 390 391 return val; 392 } 393 394 static irqreturn_t am65_cpts_interrupt(int irq, void *dev_id) 395 { 396 struct am65_cpts *cpts = dev_id; 397 398 if (am65_cpts_fifo_read(cpts)) 399 dev_dbg(cpts->dev, "cpts: unable to obtain a time stamp\n"); 400 401 return IRQ_HANDLED; 402 } 403 404 /* PTP clock operations */ 405 static int am65_cpts_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 406 { 407 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 408 u32 pps_ctrl_val = 0, pps_ppm_hi = 0, pps_ppm_low = 0; 409 s32 ppb = scaled_ppm_to_ppb(scaled_ppm); 410 int pps_index = cpts->pps_genf_idx; 411 u64 adj_period, pps_adj_period; 412 u32 ctrl_val, ppm_hi, ppm_low; 413 unsigned long flags; 414 int neg_adj = 0; 415 416 if (ppb < 0) { 417 neg_adj = 1; 418 ppb = -ppb; 419 } 420 421 /* base freq = 1GHz = 1 000 000 000 422 * ppb_norm = ppb * base_freq / clock_freq; 423 * ppm_norm = ppb_norm / 1000 424 * adj_period = 1 000 000 / ppm_norm 425 * adj_period = 1 000 000 000 / ppb_norm 426 * adj_period = 1 000 000 000 / (ppb * base_freq / clock_freq) 427 * adj_period = (1 000 000 000 * clock_freq) / (ppb * base_freq) 428 * adj_period = clock_freq / ppb 429 */ 430 adj_period = div_u64(cpts->refclk_freq, ppb); 431 432 mutex_lock(&cpts->ptp_clk_lock); 433 434 ctrl_val = am65_cpts_read32(cpts, control); 435 if (neg_adj) 436 ctrl_val |= AM65_CPTS_CONTROL_TS_PPM_DIR; 437 else 438 ctrl_val &= ~AM65_CPTS_CONTROL_TS_PPM_DIR; 439 440 ppm_hi = upper_32_bits(adj_period) & 0x3FF; 441 ppm_low = lower_32_bits(adj_period); 442 443 if (cpts->pps_enabled) { 444 pps_ctrl_val = am65_cpts_read32(cpts, genf[pps_index].control); 445 if (neg_adj) 446 pps_ctrl_val &= ~BIT(1); 447 else 448 pps_ctrl_val |= BIT(1); 449 450 /* GenF PPM will do correction using cpts refclk tick which is 451 * (cpts->ts_add_val + 1) ns, so GenF length PPM adj period 452 * need to be corrected. 453 */ 454 pps_adj_period = adj_period * (cpts->ts_add_val + 1); 455 pps_ppm_hi = upper_32_bits(pps_adj_period) & 0x3FF; 456 pps_ppm_low = lower_32_bits(pps_adj_period); 457 } 458 459 spin_lock_irqsave(&cpts->lock, flags); 460 461 /* All below writes must be done extremely fast: 462 * - delay between PPM dir and PPM value changes can cause err due old 463 * PPM correction applied in wrong direction 464 * - delay between CPTS-clock PPM cfg and GenF PPM cfg can cause err 465 * due CPTS-clock PPM working with new cfg while GenF PPM cfg still 466 * with old for short period of time 467 */ 468 469 am65_cpts_write32(cpts, ctrl_val, control); 470 am65_cpts_write32(cpts, ppm_hi, ts_ppm_hi); 471 am65_cpts_write32(cpts, ppm_low, ts_ppm_low); 472 473 if (cpts->pps_enabled) { 474 am65_cpts_write32(cpts, pps_ctrl_val, genf[pps_index].control); 475 am65_cpts_write32(cpts, pps_ppm_hi, genf[pps_index].ppm_hi); 476 am65_cpts_write32(cpts, pps_ppm_low, genf[pps_index].ppm_low); 477 } 478 479 /* All GenF/EstF can be updated here the same way */ 480 spin_unlock_irqrestore(&cpts->lock, flags); 481 482 mutex_unlock(&cpts->ptp_clk_lock); 483 484 return 0; 485 } 486 487 static int am65_cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 488 { 489 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 490 s64 ns; 491 492 mutex_lock(&cpts->ptp_clk_lock); 493 ns = am65_cpts_gettime(cpts, NULL); 494 ns += delta; 495 am65_cpts_settime(cpts, ns); 496 mutex_unlock(&cpts->ptp_clk_lock); 497 498 return 0; 499 } 500 501 static int am65_cpts_ptp_gettimex(struct ptp_clock_info *ptp, 502 struct timespec64 *ts, 503 struct ptp_system_timestamp *sts) 504 { 505 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 506 u64 ns; 507 508 mutex_lock(&cpts->ptp_clk_lock); 509 ns = am65_cpts_gettime(cpts, sts); 510 mutex_unlock(&cpts->ptp_clk_lock); 511 *ts = ns_to_timespec64(ns); 512 513 return 0; 514 } 515 516 u64 am65_cpts_ns_gettime(struct am65_cpts *cpts) 517 { 518 u64 ns; 519 520 /* reuse ptp_clk_lock as it serialize ts push */ 521 mutex_lock(&cpts->ptp_clk_lock); 522 ns = am65_cpts_gettime(cpts, NULL); 523 mutex_unlock(&cpts->ptp_clk_lock); 524 525 return ns; 526 } 527 EXPORT_SYMBOL_GPL(am65_cpts_ns_gettime); 528 529 static int am65_cpts_ptp_settime(struct ptp_clock_info *ptp, 530 const struct timespec64 *ts) 531 { 532 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 533 u64 ns; 534 535 ns = timespec64_to_ns(ts); 536 mutex_lock(&cpts->ptp_clk_lock); 537 am65_cpts_settime(cpts, ns); 538 mutex_unlock(&cpts->ptp_clk_lock); 539 540 return 0; 541 } 542 543 static void am65_cpts_extts_enable_hw(struct am65_cpts *cpts, u32 index, int on) 544 { 545 u32 v; 546 547 v = am65_cpts_read32(cpts, control); 548 if (on) { 549 v |= BIT(AM65_CPTS_CONTROL_HW1_TS_PUSH_OFFSET + index); 550 cpts->hw_ts_enable |= BIT(index); 551 } else { 552 v &= ~BIT(AM65_CPTS_CONTROL_HW1_TS_PUSH_OFFSET + index); 553 cpts->hw_ts_enable &= ~BIT(index); 554 } 555 am65_cpts_write32(cpts, v, control); 556 } 557 558 static int am65_cpts_extts_enable(struct am65_cpts *cpts, u32 index, int on) 559 { 560 if (index >= cpts->ptp_info.n_ext_ts) 561 return -ENXIO; 562 563 if (cpts->pps_present && index == cpts->pps_hw_ts_idx) 564 return -EINVAL; 565 566 if (((cpts->hw_ts_enable & BIT(index)) >> index) == on) 567 return 0; 568 569 mutex_lock(&cpts->ptp_clk_lock); 570 am65_cpts_extts_enable_hw(cpts, index, on); 571 mutex_unlock(&cpts->ptp_clk_lock); 572 573 dev_dbg(cpts->dev, "%s: ExtTS:%u %s\n", 574 __func__, index, on ? "enabled" : "disabled"); 575 576 return 0; 577 } 578 579 int am65_cpts_estf_enable(struct am65_cpts *cpts, int idx, 580 struct am65_cpts_estf_cfg *cfg) 581 { 582 u64 cycles; 583 u32 val; 584 585 cycles = cfg->ns_period * cpts->refclk_freq; 586 cycles = DIV_ROUND_UP(cycles, NSEC_PER_SEC); 587 if (cycles > U32_MAX) 588 return -EINVAL; 589 590 /* according to TRM should be zeroed */ 591 am65_cpts_write32(cpts, 0, estf[idx].length); 592 593 val = upper_32_bits(cfg->ns_start); 594 am65_cpts_write32(cpts, val, estf[idx].comp_hi); 595 val = lower_32_bits(cfg->ns_start); 596 am65_cpts_write32(cpts, val, estf[idx].comp_lo); 597 val = lower_32_bits(cycles); 598 am65_cpts_write32(cpts, val, estf[idx].length); 599 600 dev_dbg(cpts->dev, "%s: ESTF:%u enabled\n", __func__, idx); 601 602 return 0; 603 } 604 EXPORT_SYMBOL_GPL(am65_cpts_estf_enable); 605 606 void am65_cpts_estf_disable(struct am65_cpts *cpts, int idx) 607 { 608 am65_cpts_write32(cpts, 0, estf[idx].length); 609 610 dev_dbg(cpts->dev, "%s: ESTF:%u disabled\n", __func__, idx); 611 } 612 EXPORT_SYMBOL_GPL(am65_cpts_estf_disable); 613 614 static void am65_cpts_perout_enable_hw(struct am65_cpts *cpts, 615 struct ptp_perout_request *req, int on) 616 { 617 u64 ns_period, ns_start, cycles; 618 struct timespec64 ts; 619 u32 val; 620 621 if (on) { 622 ts.tv_sec = req->period.sec; 623 ts.tv_nsec = req->period.nsec; 624 ns_period = timespec64_to_ns(&ts); 625 626 cycles = (ns_period * cpts->refclk_freq) / NSEC_PER_SEC; 627 628 ts.tv_sec = req->start.sec; 629 ts.tv_nsec = req->start.nsec; 630 ns_start = timespec64_to_ns(&ts); 631 632 val = upper_32_bits(ns_start); 633 am65_cpts_write32(cpts, val, genf[req->index].comp_hi); 634 val = lower_32_bits(ns_start); 635 am65_cpts_write32(cpts, val, genf[req->index].comp_lo); 636 val = lower_32_bits(cycles); 637 am65_cpts_write32(cpts, val, genf[req->index].length); 638 639 am65_cpts_write32(cpts, 0, genf[req->index].control); 640 am65_cpts_write32(cpts, 0, genf[req->index].ppm_hi); 641 am65_cpts_write32(cpts, 0, genf[req->index].ppm_low); 642 643 cpts->genf_enable |= BIT(req->index); 644 } else { 645 am65_cpts_write32(cpts, 0, genf[req->index].length); 646 647 cpts->genf_enable &= ~BIT(req->index); 648 } 649 } 650 651 static int am65_cpts_perout_enable(struct am65_cpts *cpts, 652 struct ptp_perout_request *req, int on) 653 { 654 if (req->index >= cpts->ptp_info.n_per_out) 655 return -ENXIO; 656 657 if (cpts->pps_present && req->index == cpts->pps_genf_idx) 658 return -EINVAL; 659 660 if (!!(cpts->genf_enable & BIT(req->index)) == !!on) 661 return 0; 662 663 mutex_lock(&cpts->ptp_clk_lock); 664 am65_cpts_perout_enable_hw(cpts, req, on); 665 mutex_unlock(&cpts->ptp_clk_lock); 666 667 dev_dbg(cpts->dev, "%s: GenF:%u %s\n", 668 __func__, req->index, on ? "enabled" : "disabled"); 669 670 return 0; 671 } 672 673 static int am65_cpts_pps_enable(struct am65_cpts *cpts, int on) 674 { 675 int ret = 0; 676 struct timespec64 ts; 677 struct ptp_clock_request rq; 678 u64 ns; 679 680 if (!cpts->pps_present) 681 return -EINVAL; 682 683 if (cpts->pps_enabled == !!on) 684 return 0; 685 686 mutex_lock(&cpts->ptp_clk_lock); 687 688 if (on) { 689 am65_cpts_extts_enable_hw(cpts, cpts->pps_hw_ts_idx, on); 690 691 ns = am65_cpts_gettime(cpts, NULL); 692 ts = ns_to_timespec64(ns); 693 rq.perout.period.sec = 1; 694 rq.perout.period.nsec = 0; 695 rq.perout.start.sec = ts.tv_sec + 2; 696 rq.perout.start.nsec = 0; 697 rq.perout.index = cpts->pps_genf_idx; 698 699 am65_cpts_perout_enable_hw(cpts, &rq.perout, on); 700 cpts->pps_enabled = true; 701 } else { 702 rq.perout.index = cpts->pps_genf_idx; 703 am65_cpts_perout_enable_hw(cpts, &rq.perout, on); 704 am65_cpts_extts_enable_hw(cpts, cpts->pps_hw_ts_idx, on); 705 cpts->pps_enabled = false; 706 } 707 708 mutex_unlock(&cpts->ptp_clk_lock); 709 710 dev_dbg(cpts->dev, "%s: pps: %s\n", 711 __func__, on ? "enabled" : "disabled"); 712 return ret; 713 } 714 715 static int am65_cpts_ptp_enable(struct ptp_clock_info *ptp, 716 struct ptp_clock_request *rq, int on) 717 { 718 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 719 720 switch (rq->type) { 721 case PTP_CLK_REQ_EXTTS: 722 return am65_cpts_extts_enable(cpts, rq->extts.index, on); 723 case PTP_CLK_REQ_PEROUT: 724 return am65_cpts_perout_enable(cpts, &rq->perout, on); 725 case PTP_CLK_REQ_PPS: 726 return am65_cpts_pps_enable(cpts, on); 727 default: 728 break; 729 } 730 731 return -EOPNOTSUPP; 732 } 733 734 static long am65_cpts_ts_work(struct ptp_clock_info *ptp); 735 736 static struct ptp_clock_info am65_ptp_info = { 737 .owner = THIS_MODULE, 738 .name = "CTPS timer", 739 .adjfine = am65_cpts_ptp_adjfine, 740 .adjtime = am65_cpts_ptp_adjtime, 741 .gettimex64 = am65_cpts_ptp_gettimex, 742 .settime64 = am65_cpts_ptp_settime, 743 .enable = am65_cpts_ptp_enable, 744 .do_aux_work = am65_cpts_ts_work, 745 }; 746 747 static bool am65_cpts_match_tx_ts(struct am65_cpts *cpts, 748 struct am65_cpts_event *event) 749 { 750 struct sk_buff_head txq_list; 751 struct sk_buff *skb, *tmp; 752 unsigned long flags; 753 bool found = false; 754 u32 mtype_seqid; 755 756 mtype_seqid = event->event1 & 757 (AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK | 758 AM65_CPTS_EVENT_1_EVENT_TYPE_MASK | 759 AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK); 760 761 __skb_queue_head_init(&txq_list); 762 763 spin_lock_irqsave(&cpts->txq.lock, flags); 764 skb_queue_splice_init(&cpts->txq, &txq_list); 765 spin_unlock_irqrestore(&cpts->txq.lock, flags); 766 767 /* no need to grab txq.lock as access is always done under cpts->lock */ 768 skb_queue_walk_safe(&txq_list, skb, tmp) { 769 struct skb_shared_hwtstamps ssh; 770 struct am65_cpts_skb_cb_data *skb_cb = 771 (struct am65_cpts_skb_cb_data *)skb->cb; 772 773 if (mtype_seqid == skb_cb->skb_mtype_seqid) { 774 u64 ns = event->timestamp; 775 776 memset(&ssh, 0, sizeof(ssh)); 777 ssh.hwtstamp = ns_to_ktime(ns); 778 skb_tstamp_tx(skb, &ssh); 779 found = true; 780 __skb_unlink(skb, &txq_list); 781 dev_consume_skb_any(skb); 782 dev_dbg(cpts->dev, 783 "match tx timestamp mtype_seqid %08x\n", 784 mtype_seqid); 785 break; 786 } 787 788 if (time_after(jiffies, skb_cb->tmo)) { 789 /* timeout any expired skbs over 100 ms */ 790 dev_dbg(cpts->dev, 791 "expiring tx timestamp mtype_seqid %08x\n", 792 mtype_seqid); 793 __skb_unlink(skb, &txq_list); 794 dev_consume_skb_any(skb); 795 } 796 } 797 798 spin_lock_irqsave(&cpts->txq.lock, flags); 799 skb_queue_splice(&txq_list, &cpts->txq); 800 spin_unlock_irqrestore(&cpts->txq.lock, flags); 801 802 return found; 803 } 804 805 static void am65_cpts_find_ts(struct am65_cpts *cpts) 806 { 807 struct am65_cpts_event *event; 808 struct list_head *this, *next; 809 LIST_HEAD(events_free); 810 unsigned long flags; 811 LIST_HEAD(events); 812 813 spin_lock_irqsave(&cpts->lock, flags); 814 list_splice_init(&cpts->events, &events); 815 spin_unlock_irqrestore(&cpts->lock, flags); 816 817 list_for_each_safe(this, next, &events) { 818 event = list_entry(this, struct am65_cpts_event, list); 819 if (am65_cpts_match_tx_ts(cpts, event) || 820 time_after(jiffies, event->tmo)) { 821 list_del_init(&event->list); 822 list_add(&event->list, &events_free); 823 } 824 } 825 826 spin_lock_irqsave(&cpts->lock, flags); 827 list_splice_tail(&events, &cpts->events); 828 list_splice_tail(&events_free, &cpts->pool); 829 spin_unlock_irqrestore(&cpts->lock, flags); 830 } 831 832 static long am65_cpts_ts_work(struct ptp_clock_info *ptp) 833 { 834 struct am65_cpts *cpts = container_of(ptp, struct am65_cpts, ptp_info); 835 unsigned long flags; 836 long delay = -1; 837 838 am65_cpts_find_ts(cpts); 839 840 spin_lock_irqsave(&cpts->txq.lock, flags); 841 if (!skb_queue_empty(&cpts->txq)) 842 delay = AM65_CPTS_SKB_TX_WORK_TIMEOUT; 843 spin_unlock_irqrestore(&cpts->txq.lock, flags); 844 845 return delay; 846 } 847 848 /** 849 * am65_cpts_rx_enable - enable rx timestamping 850 * @cpts: cpts handle 851 * @en: enable 852 * 853 * This functions enables rx packets timestamping. The CPTS can timestamp all 854 * rx packets. 855 */ 856 void am65_cpts_rx_enable(struct am65_cpts *cpts, bool en) 857 { 858 u32 val; 859 860 mutex_lock(&cpts->ptp_clk_lock); 861 val = am65_cpts_read32(cpts, control); 862 if (en) 863 val |= AM65_CPTS_CONTROL_TSTAMP_EN; 864 else 865 val &= ~AM65_CPTS_CONTROL_TSTAMP_EN; 866 am65_cpts_write32(cpts, val, control); 867 mutex_unlock(&cpts->ptp_clk_lock); 868 } 869 EXPORT_SYMBOL_GPL(am65_cpts_rx_enable); 870 871 static int am65_skb_get_mtype_seqid(struct sk_buff *skb, u32 *mtype_seqid) 872 { 873 unsigned int ptp_class = ptp_classify_raw(skb); 874 struct ptp_header *hdr; 875 u8 msgtype; 876 u16 seqid; 877 878 if (ptp_class == PTP_CLASS_NONE) 879 return 0; 880 881 hdr = ptp_parse_header(skb, ptp_class); 882 if (!hdr) 883 return 0; 884 885 msgtype = ptp_get_msgtype(hdr, ptp_class); 886 seqid = ntohs(hdr->sequence_id); 887 888 *mtype_seqid = (msgtype << AM65_CPTS_EVENT_1_MESSAGE_TYPE_SHIFT) & 889 AM65_CPTS_EVENT_1_MESSAGE_TYPE_MASK; 890 *mtype_seqid |= (seqid & AM65_CPTS_EVENT_1_SEQUENCE_ID_MASK); 891 892 return 1; 893 } 894 895 /** 896 * am65_cpts_tx_timestamp - save tx packet for timestamping 897 * @cpts: cpts handle 898 * @skb: packet 899 * 900 * This functions saves tx packet for timestamping if packet can be timestamped. 901 * The future processing is done in from PTP auxiliary worker. 902 */ 903 void am65_cpts_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb) 904 { 905 struct am65_cpts_skb_cb_data *skb_cb = (void *)skb->cb; 906 907 if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) 908 return; 909 910 /* add frame to queue for processing later. 911 * The periodic FIFO check will handle this. 912 */ 913 skb_get(skb); 914 /* get the timestamp for timeouts */ 915 skb_cb->tmo = jiffies + msecs_to_jiffies(100); 916 skb_queue_tail(&cpts->txq, skb); 917 ptp_schedule_worker(cpts->ptp_clock, 0); 918 } 919 EXPORT_SYMBOL_GPL(am65_cpts_tx_timestamp); 920 921 /** 922 * am65_cpts_prep_tx_timestamp - check and prepare tx packet for timestamping 923 * @cpts: cpts handle 924 * @skb: packet 925 * 926 * This functions should be called from .xmit(). 927 * It checks if packet can be timestamped, fills internal cpts data 928 * in skb-cb and marks packet as SKBTX_IN_PROGRESS. 929 */ 930 void am65_cpts_prep_tx_timestamp(struct am65_cpts *cpts, struct sk_buff *skb) 931 { 932 struct am65_cpts_skb_cb_data *skb_cb = (void *)skb->cb; 933 int ret; 934 935 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) 936 return; 937 938 ret = am65_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid); 939 if (!ret) 940 return; 941 skb_cb->skb_mtype_seqid |= (AM65_CPTS_EV_TX << 942 AM65_CPTS_EVENT_1_EVENT_TYPE_SHIFT); 943 944 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 945 } 946 EXPORT_SYMBOL_GPL(am65_cpts_prep_tx_timestamp); 947 948 int am65_cpts_phc_index(struct am65_cpts *cpts) 949 { 950 return cpts->phc_index; 951 } 952 EXPORT_SYMBOL_GPL(am65_cpts_phc_index); 953 954 static void cpts_free_clk_mux(void *data) 955 { 956 struct am65_cpts *cpts = data; 957 958 of_clk_del_provider(cpts->clk_mux_np); 959 clk_hw_unregister_mux(cpts->clk_mux_hw); 960 of_node_put(cpts->clk_mux_np); 961 } 962 963 static int cpts_of_mux_clk_setup(struct am65_cpts *cpts, 964 struct device_node *node) 965 { 966 unsigned int num_parents; 967 const char **parent_names; 968 char *clk_mux_name; 969 void __iomem *reg; 970 int ret = -EINVAL; 971 972 cpts->clk_mux_np = of_get_child_by_name(node, "refclk-mux"); 973 if (!cpts->clk_mux_np) 974 return 0; 975 976 num_parents = of_clk_get_parent_count(cpts->clk_mux_np); 977 if (num_parents < 1) { 978 dev_err(cpts->dev, "mux-clock %pOF must have parents\n", 979 cpts->clk_mux_np); 980 goto mux_fail; 981 } 982 983 parent_names = devm_kcalloc(cpts->dev, sizeof(char *), num_parents, 984 GFP_KERNEL); 985 if (!parent_names) { 986 ret = -ENOMEM; 987 goto mux_fail; 988 } 989 990 of_clk_parent_fill(cpts->clk_mux_np, parent_names, num_parents); 991 992 clk_mux_name = devm_kasprintf(cpts->dev, GFP_KERNEL, "%s.%pOFn", 993 dev_name(cpts->dev), cpts->clk_mux_np); 994 if (!clk_mux_name) { 995 ret = -ENOMEM; 996 goto mux_fail; 997 } 998 999 reg = &cpts->reg->rftclk_sel; 1000 /* dev must be NULL to avoid recursive incrementing 1001 * of module refcnt 1002 */ 1003 cpts->clk_mux_hw = clk_hw_register_mux(NULL, clk_mux_name, 1004 parent_names, num_parents, 1005 0, reg, 0, 5, 0, NULL); 1006 if (IS_ERR(cpts->clk_mux_hw)) { 1007 ret = PTR_ERR(cpts->clk_mux_hw); 1008 goto mux_fail; 1009 } 1010 1011 ret = of_clk_add_hw_provider(cpts->clk_mux_np, of_clk_hw_simple_get, 1012 cpts->clk_mux_hw); 1013 if (ret) 1014 goto clk_hw_register; 1015 1016 ret = devm_add_action_or_reset(cpts->dev, cpts_free_clk_mux, cpts); 1017 if (ret) 1018 dev_err(cpts->dev, "failed to add clkmux reset action %d", ret); 1019 1020 return ret; 1021 1022 clk_hw_register: 1023 clk_hw_unregister_mux(cpts->clk_mux_hw); 1024 mux_fail: 1025 of_node_put(cpts->clk_mux_np); 1026 return ret; 1027 } 1028 1029 static int am65_cpts_of_parse(struct am65_cpts *cpts, struct device_node *node) 1030 { 1031 u32 prop[2]; 1032 1033 if (!of_property_read_u32(node, "ti,cpts-ext-ts-inputs", &prop[0])) 1034 cpts->ext_ts_inputs = prop[0]; 1035 1036 if (!of_property_read_u32(node, "ti,cpts-periodic-outputs", &prop[0])) 1037 cpts->genf_num = prop[0]; 1038 1039 if (!of_property_read_u32_array(node, "ti,pps", prop, 2)) { 1040 cpts->pps_present = true; 1041 1042 if (prop[0] > 7) { 1043 dev_err(cpts->dev, "invalid HWx_TS_PUSH index: %u provided\n", prop[0]); 1044 cpts->pps_present = false; 1045 } 1046 if (prop[1] > 1) { 1047 dev_err(cpts->dev, "invalid GENFy index: %u provided\n", prop[1]); 1048 cpts->pps_present = false; 1049 } 1050 if (cpts->pps_present) { 1051 cpts->pps_hw_ts_idx = prop[0]; 1052 cpts->pps_genf_idx = prop[1]; 1053 } 1054 } 1055 1056 return cpts_of_mux_clk_setup(cpts, node); 1057 } 1058 1059 void am65_cpts_release(struct am65_cpts *cpts) 1060 { 1061 ptp_clock_unregister(cpts->ptp_clock); 1062 am65_cpts_disable(cpts); 1063 clk_disable_unprepare(cpts->refclk); 1064 } 1065 EXPORT_SYMBOL_GPL(am65_cpts_release); 1066 1067 struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs, 1068 struct device_node *node) 1069 { 1070 struct am65_cpts *cpts; 1071 int ret, i; 1072 1073 cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL); 1074 if (!cpts) 1075 return ERR_PTR(-ENOMEM); 1076 1077 cpts->dev = dev; 1078 cpts->reg = (struct am65_cpts_regs __iomem *)regs; 1079 1080 cpts->irq = of_irq_get_byname(node, "cpts"); 1081 if (cpts->irq <= 0) { 1082 ret = cpts->irq ?: -ENXIO; 1083 dev_err_probe(dev, ret, "Failed to get IRQ number\n"); 1084 return ERR_PTR(ret); 1085 } 1086 1087 ret = am65_cpts_of_parse(cpts, node); 1088 if (ret) 1089 return ERR_PTR(ret); 1090 1091 mutex_init(&cpts->ptp_clk_lock); 1092 INIT_LIST_HEAD(&cpts->events); 1093 INIT_LIST_HEAD(&cpts->pool); 1094 spin_lock_init(&cpts->lock); 1095 skb_queue_head_init(&cpts->txq); 1096 1097 for (i = 0; i < AM65_CPTS_MAX_EVENTS; i++) 1098 list_add(&cpts->pool_data[i].list, &cpts->pool); 1099 1100 cpts->refclk = devm_get_clk_from_child(dev, node, "cpts"); 1101 if (IS_ERR(cpts->refclk)) { 1102 ret = PTR_ERR(cpts->refclk); 1103 dev_err_probe(dev, ret, "Failed to get refclk\n"); 1104 return ERR_PTR(ret); 1105 } 1106 1107 ret = clk_prepare_enable(cpts->refclk); 1108 if (ret) { 1109 dev_err(dev, "Failed to enable refclk %d\n", ret); 1110 return ERR_PTR(ret); 1111 } 1112 1113 cpts->refclk_freq = clk_get_rate(cpts->refclk); 1114 1115 am65_ptp_info.max_adj = cpts->refclk_freq / AM65_CPTS_MIN_PPM; 1116 cpts->ptp_info = am65_ptp_info; 1117 1118 if (cpts->ext_ts_inputs) 1119 cpts->ptp_info.n_ext_ts = cpts->ext_ts_inputs; 1120 if (cpts->genf_num) 1121 cpts->ptp_info.n_per_out = cpts->genf_num; 1122 if (cpts->pps_present) 1123 cpts->ptp_info.pps = 1; 1124 1125 am65_cpts_set_add_val(cpts); 1126 1127 am65_cpts_write32(cpts, AM65_CPTS_CONTROL_EN | 1128 AM65_CPTS_CONTROL_64MODE | 1129 AM65_CPTS_CONTROL_TX_GENF_CLR_EN, 1130 control); 1131 am65_cpts_write32(cpts, AM65_CPTS_INT_ENABLE_TS_PEND_EN, int_enable); 1132 1133 /* set time to the current system time */ 1134 am65_cpts_settime(cpts, ktime_to_ns(ktime_get_real())); 1135 1136 cpts->ptp_clock = ptp_clock_register(&cpts->ptp_info, cpts->dev); 1137 if (IS_ERR_OR_NULL(cpts->ptp_clock)) { 1138 dev_err(dev, "Failed to register ptp clk %ld\n", 1139 PTR_ERR(cpts->ptp_clock)); 1140 ret = cpts->ptp_clock ? PTR_ERR(cpts->ptp_clock) : -ENODEV; 1141 goto refclk_disable; 1142 } 1143 cpts->phc_index = ptp_clock_index(cpts->ptp_clock); 1144 1145 ret = devm_request_threaded_irq(dev, cpts->irq, NULL, 1146 am65_cpts_interrupt, 1147 IRQF_ONESHOT, dev_name(dev), cpts); 1148 if (ret < 0) { 1149 dev_err(cpts->dev, "error attaching irq %d\n", ret); 1150 goto reset_ptpclk; 1151 } 1152 1153 dev_info(dev, "CPTS ver 0x%08x, freq:%u, add_val:%u pps:%d\n", 1154 am65_cpts_read32(cpts, idver), 1155 cpts->refclk_freq, cpts->ts_add_val, cpts->pps_present); 1156 1157 return cpts; 1158 1159 reset_ptpclk: 1160 am65_cpts_release(cpts); 1161 refclk_disable: 1162 clk_disable_unprepare(cpts->refclk); 1163 return ERR_PTR(ret); 1164 } 1165 EXPORT_SYMBOL_GPL(am65_cpts_create); 1166 1167 void am65_cpts_suspend(struct am65_cpts *cpts) 1168 { 1169 /* save state and disable CPTS */ 1170 cpts->sr_control = am65_cpts_read32(cpts, control); 1171 cpts->sr_int_enable = am65_cpts_read32(cpts, int_enable); 1172 cpts->sr_rftclk_sel = am65_cpts_read32(cpts, rftclk_sel); 1173 cpts->sr_ts_ppm_hi = am65_cpts_read32(cpts, ts_ppm_hi); 1174 cpts->sr_ts_ppm_low = am65_cpts_read32(cpts, ts_ppm_low); 1175 cpts->sr_cpts_ns = am65_cpts_gettime(cpts, NULL); 1176 cpts->sr_ktime_ns = ktime_to_ns(ktime_get_real()); 1177 am65_cpts_disable(cpts); 1178 clk_disable(cpts->refclk); 1179 1180 /* Save GENF state */ 1181 memcpy_fromio(&cpts->sr_genf, &cpts->reg->genf, sizeof(cpts->sr_genf)); 1182 1183 /* Save ESTF state */ 1184 memcpy_fromio(&cpts->sr_estf, &cpts->reg->estf, sizeof(cpts->sr_estf)); 1185 } 1186 EXPORT_SYMBOL_GPL(am65_cpts_suspend); 1187 1188 void am65_cpts_resume(struct am65_cpts *cpts) 1189 { 1190 int i; 1191 s64 ktime_ns; 1192 1193 /* restore state and enable CPTS */ 1194 clk_enable(cpts->refclk); 1195 am65_cpts_write32(cpts, cpts->sr_rftclk_sel, rftclk_sel); 1196 am65_cpts_set_add_val(cpts); 1197 am65_cpts_write32(cpts, cpts->sr_control, control); 1198 am65_cpts_write32(cpts, cpts->sr_int_enable, int_enable); 1199 1200 /* Restore time to saved CPTS time + time in suspend/resume */ 1201 ktime_ns = ktime_to_ns(ktime_get_real()); 1202 ktime_ns -= cpts->sr_ktime_ns; 1203 am65_cpts_settime(cpts, cpts->sr_cpts_ns + ktime_ns); 1204 1205 /* Restore compensation (PPM) */ 1206 am65_cpts_write32(cpts, cpts->sr_ts_ppm_hi, ts_ppm_hi); 1207 am65_cpts_write32(cpts, cpts->sr_ts_ppm_low, ts_ppm_low); 1208 1209 /* Restore GENF state */ 1210 for (i = 0; i < AM65_CPTS_GENF_MAX_NUM; i++) { 1211 am65_cpts_write32(cpts, 0, genf[i].length); /* TRM sequence */ 1212 am65_cpts_write32(cpts, cpts->sr_genf[i].comp_hi, genf[i].comp_hi); 1213 am65_cpts_write32(cpts, cpts->sr_genf[i].comp_lo, genf[i].comp_lo); 1214 am65_cpts_write32(cpts, cpts->sr_genf[i].length, genf[i].length); 1215 am65_cpts_write32(cpts, cpts->sr_genf[i].control, genf[i].control); 1216 am65_cpts_write32(cpts, cpts->sr_genf[i].ppm_hi, genf[i].ppm_hi); 1217 am65_cpts_write32(cpts, cpts->sr_genf[i].ppm_low, genf[i].ppm_low); 1218 } 1219 1220 /* Restore ESTTF state */ 1221 for (i = 0; i < AM65_CPTS_ESTF_MAX_NUM; i++) { 1222 am65_cpts_write32(cpts, 0, estf[i].length); /* TRM sequence */ 1223 am65_cpts_write32(cpts, cpts->sr_estf[i].comp_hi, estf[i].comp_hi); 1224 am65_cpts_write32(cpts, cpts->sr_estf[i].comp_lo, estf[i].comp_lo); 1225 am65_cpts_write32(cpts, cpts->sr_estf[i].length, estf[i].length); 1226 am65_cpts_write32(cpts, cpts->sr_estf[i].control, estf[i].control); 1227 am65_cpts_write32(cpts, cpts->sr_estf[i].ppm_hi, estf[i].ppm_hi); 1228 am65_cpts_write32(cpts, cpts->sr_estf[i].ppm_low, estf[i].ppm_low); 1229 } 1230 } 1231 EXPORT_SYMBOL_GPL(am65_cpts_resume); 1232 1233 static int am65_cpts_probe(struct platform_device *pdev) 1234 { 1235 struct device_node *node = pdev->dev.of_node; 1236 struct device *dev = &pdev->dev; 1237 struct am65_cpts *cpts; 1238 void __iomem *base; 1239 1240 base = devm_platform_ioremap_resource_byname(pdev, "cpts"); 1241 if (IS_ERR(base)) 1242 return PTR_ERR(base); 1243 1244 cpts = am65_cpts_create(dev, base, node); 1245 return PTR_ERR_OR_ZERO(cpts); 1246 } 1247 1248 static const struct of_device_id am65_cpts_of_match[] = { 1249 { .compatible = "ti,am65-cpts", }, 1250 { .compatible = "ti,j721e-cpts", }, 1251 {}, 1252 }; 1253 MODULE_DEVICE_TABLE(of, am65_cpts_of_match); 1254 1255 static struct platform_driver am65_cpts_driver = { 1256 .probe = am65_cpts_probe, 1257 .driver = { 1258 .name = "am65-cpts", 1259 .of_match_table = am65_cpts_of_match, 1260 }, 1261 }; 1262 module_platform_driver(am65_cpts_driver); 1263 1264 MODULE_LICENSE("GPL v2"); 1265 MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>"); 1266 MODULE_DESCRIPTION("TI K3 AM65 CPTS driver"); 1267