1 /* 2 * PTP 1588 clock for Freescale QorIQ 1588 timer 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 21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 22 23 #include <linux/device.h> 24 #include <linux/hrtimer.h> 25 #include <linux/interrupt.h> 26 #include <linux/kernel.h> 27 #include <linux/module.h> 28 #include <linux/of.h> 29 #include <linux/of_platform.h> 30 #include <linux/timex.h> 31 #include <linux/slab.h> 32 33 #include <linux/fsl/ptp_qoriq.h> 34 35 /* 36 * Register access functions 37 */ 38 39 /* Caller must hold qoriq_ptp->lock. */ 40 static u64 tmr_cnt_read(struct qoriq_ptp *qoriq_ptp) 41 { 42 struct qoriq_ptp_registers *regs = &qoriq_ptp->regs; 43 u64 ns; 44 u32 lo, hi; 45 46 lo = qoriq_read(®s->ctrl_regs->tmr_cnt_l); 47 hi = qoriq_read(®s->ctrl_regs->tmr_cnt_h); 48 ns = ((u64) hi) << 32; 49 ns |= lo; 50 return ns; 51 } 52 53 /* Caller must hold qoriq_ptp->lock. */ 54 static void tmr_cnt_write(struct qoriq_ptp *qoriq_ptp, u64 ns) 55 { 56 struct qoriq_ptp_registers *regs = &qoriq_ptp->regs; 57 u32 hi = ns >> 32; 58 u32 lo = ns & 0xffffffff; 59 60 qoriq_write(®s->ctrl_regs->tmr_cnt_l, lo); 61 qoriq_write(®s->ctrl_regs->tmr_cnt_h, hi); 62 } 63 64 /* Caller must hold qoriq_ptp->lock. */ 65 static void set_alarm(struct qoriq_ptp *qoriq_ptp) 66 { 67 struct qoriq_ptp_registers *regs = &qoriq_ptp->regs; 68 u64 ns; 69 u32 lo, hi; 70 71 ns = tmr_cnt_read(qoriq_ptp) + 1500000000ULL; 72 ns = div_u64(ns, 1000000000UL) * 1000000000ULL; 73 ns -= qoriq_ptp->tclk_period; 74 hi = ns >> 32; 75 lo = ns & 0xffffffff; 76 qoriq_write(®s->alarm_regs->tmr_alarm1_l, lo); 77 qoriq_write(®s->alarm_regs->tmr_alarm1_h, hi); 78 } 79 80 /* Caller must hold qoriq_ptp->lock. */ 81 static void set_fipers(struct qoriq_ptp *qoriq_ptp) 82 { 83 struct qoriq_ptp_registers *regs = &qoriq_ptp->regs; 84 85 set_alarm(qoriq_ptp); 86 qoriq_write(®s->fiper_regs->tmr_fiper1, qoriq_ptp->tmr_fiper1); 87 qoriq_write(®s->fiper_regs->tmr_fiper2, qoriq_ptp->tmr_fiper2); 88 } 89 90 /* 91 * Interrupt service routine 92 */ 93 94 static irqreturn_t isr(int irq, void *priv) 95 { 96 struct qoriq_ptp *qoriq_ptp = priv; 97 struct qoriq_ptp_registers *regs = &qoriq_ptp->regs; 98 struct ptp_clock_event event; 99 u64 ns; 100 u32 ack = 0, lo, hi, mask, val; 101 102 val = qoriq_read(®s->ctrl_regs->tmr_tevent); 103 104 if (val & ETS1) { 105 ack |= ETS1; 106 hi = qoriq_read(®s->etts_regs->tmr_etts1_h); 107 lo = qoriq_read(®s->etts_regs->tmr_etts1_l); 108 event.type = PTP_CLOCK_EXTTS; 109 event.index = 0; 110 event.timestamp = ((u64) hi) << 32; 111 event.timestamp |= lo; 112 ptp_clock_event(qoriq_ptp->clock, &event); 113 } 114 115 if (val & ETS2) { 116 ack |= ETS2; 117 hi = qoriq_read(®s->etts_regs->tmr_etts2_h); 118 lo = qoriq_read(®s->etts_regs->tmr_etts2_l); 119 event.type = PTP_CLOCK_EXTTS; 120 event.index = 1; 121 event.timestamp = ((u64) hi) << 32; 122 event.timestamp |= lo; 123 ptp_clock_event(qoriq_ptp->clock, &event); 124 } 125 126 if (val & ALM2) { 127 ack |= ALM2; 128 if (qoriq_ptp->alarm_value) { 129 event.type = PTP_CLOCK_ALARM; 130 event.index = 0; 131 event.timestamp = qoriq_ptp->alarm_value; 132 ptp_clock_event(qoriq_ptp->clock, &event); 133 } 134 if (qoriq_ptp->alarm_interval) { 135 ns = qoriq_ptp->alarm_value + qoriq_ptp->alarm_interval; 136 hi = ns >> 32; 137 lo = ns & 0xffffffff; 138 spin_lock(&qoriq_ptp->lock); 139 qoriq_write(®s->alarm_regs->tmr_alarm2_l, lo); 140 qoriq_write(®s->alarm_regs->tmr_alarm2_h, hi); 141 spin_unlock(&qoriq_ptp->lock); 142 qoriq_ptp->alarm_value = ns; 143 } else { 144 qoriq_write(®s->ctrl_regs->tmr_tevent, ALM2); 145 spin_lock(&qoriq_ptp->lock); 146 mask = qoriq_read(®s->ctrl_regs->tmr_temask); 147 mask &= ~ALM2EN; 148 qoriq_write(®s->ctrl_regs->tmr_temask, mask); 149 spin_unlock(&qoriq_ptp->lock); 150 qoriq_ptp->alarm_value = 0; 151 qoriq_ptp->alarm_interval = 0; 152 } 153 } 154 155 if (val & PP1) { 156 ack |= PP1; 157 event.type = PTP_CLOCK_PPS; 158 ptp_clock_event(qoriq_ptp->clock, &event); 159 } 160 161 if (ack) { 162 qoriq_write(®s->ctrl_regs->tmr_tevent, ack); 163 return IRQ_HANDLED; 164 } else 165 return IRQ_NONE; 166 } 167 168 /* 169 * PTP clock operations 170 */ 171 172 static int ptp_qoriq_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 173 { 174 u64 adj, diff; 175 u32 tmr_add; 176 int neg_adj = 0; 177 struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps); 178 struct qoriq_ptp_registers *regs = &qoriq_ptp->regs; 179 180 if (scaled_ppm < 0) { 181 neg_adj = 1; 182 scaled_ppm = -scaled_ppm; 183 } 184 tmr_add = qoriq_ptp->tmr_add; 185 adj = tmr_add; 186 187 /* calculate diff as adj*(scaled_ppm/65536)/1000000 188 * and round() to the nearest integer 189 */ 190 adj *= scaled_ppm; 191 diff = div_u64(adj, 8000000); 192 diff = (diff >> 13) + ((diff >> 12) & 1); 193 194 tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff; 195 196 qoriq_write(®s->ctrl_regs->tmr_add, tmr_add); 197 198 return 0; 199 } 200 201 static int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta) 202 { 203 s64 now; 204 unsigned long flags; 205 struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps); 206 207 spin_lock_irqsave(&qoriq_ptp->lock, flags); 208 209 now = tmr_cnt_read(qoriq_ptp); 210 now += delta; 211 tmr_cnt_write(qoriq_ptp, now); 212 set_fipers(qoriq_ptp); 213 214 spin_unlock_irqrestore(&qoriq_ptp->lock, flags); 215 216 return 0; 217 } 218 219 static int ptp_qoriq_gettime(struct ptp_clock_info *ptp, 220 struct timespec64 *ts) 221 { 222 u64 ns; 223 unsigned long flags; 224 struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps); 225 226 spin_lock_irqsave(&qoriq_ptp->lock, flags); 227 228 ns = tmr_cnt_read(qoriq_ptp); 229 230 spin_unlock_irqrestore(&qoriq_ptp->lock, flags); 231 232 *ts = ns_to_timespec64(ns); 233 234 return 0; 235 } 236 237 static int ptp_qoriq_settime(struct ptp_clock_info *ptp, 238 const struct timespec64 *ts) 239 { 240 u64 ns; 241 unsigned long flags; 242 struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps); 243 244 ns = timespec64_to_ns(ts); 245 246 spin_lock_irqsave(&qoriq_ptp->lock, flags); 247 248 tmr_cnt_write(qoriq_ptp, ns); 249 set_fipers(qoriq_ptp); 250 251 spin_unlock_irqrestore(&qoriq_ptp->lock, flags); 252 253 return 0; 254 } 255 256 static int ptp_qoriq_enable(struct ptp_clock_info *ptp, 257 struct ptp_clock_request *rq, int on) 258 { 259 struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps); 260 struct qoriq_ptp_registers *regs = &qoriq_ptp->regs; 261 unsigned long flags; 262 u32 bit, mask; 263 264 switch (rq->type) { 265 case PTP_CLK_REQ_EXTTS: 266 switch (rq->extts.index) { 267 case 0: 268 bit = ETS1EN; 269 break; 270 case 1: 271 bit = ETS2EN; 272 break; 273 default: 274 return -EINVAL; 275 } 276 spin_lock_irqsave(&qoriq_ptp->lock, flags); 277 mask = qoriq_read(®s->ctrl_regs->tmr_temask); 278 if (on) 279 mask |= bit; 280 else 281 mask &= ~bit; 282 qoriq_write(®s->ctrl_regs->tmr_temask, mask); 283 spin_unlock_irqrestore(&qoriq_ptp->lock, flags); 284 return 0; 285 286 case PTP_CLK_REQ_PPS: 287 spin_lock_irqsave(&qoriq_ptp->lock, flags); 288 mask = qoriq_read(®s->ctrl_regs->tmr_temask); 289 if (on) 290 mask |= PP1EN; 291 else 292 mask &= ~PP1EN; 293 qoriq_write(®s->ctrl_regs->tmr_temask, mask); 294 spin_unlock_irqrestore(&qoriq_ptp->lock, flags); 295 return 0; 296 297 default: 298 break; 299 } 300 301 return -EOPNOTSUPP; 302 } 303 304 static const struct ptp_clock_info ptp_qoriq_caps = { 305 .owner = THIS_MODULE, 306 .name = "qoriq ptp clock", 307 .max_adj = 512000, 308 .n_alarm = 0, 309 .n_ext_ts = N_EXT_TS, 310 .n_per_out = 0, 311 .n_pins = 0, 312 .pps = 1, 313 .adjfine = ptp_qoriq_adjfine, 314 .adjtime = ptp_qoriq_adjtime, 315 .gettime64 = ptp_qoriq_gettime, 316 .settime64 = ptp_qoriq_settime, 317 .enable = ptp_qoriq_enable, 318 }; 319 320 static int qoriq_ptp_probe(struct platform_device *dev) 321 { 322 struct device_node *node = dev->dev.of_node; 323 struct qoriq_ptp *qoriq_ptp; 324 struct qoriq_ptp_registers *regs; 325 struct timespec64 now; 326 int err = -ENOMEM; 327 u32 tmr_ctrl; 328 unsigned long flags; 329 void __iomem *base; 330 331 qoriq_ptp = kzalloc(sizeof(*qoriq_ptp), GFP_KERNEL); 332 if (!qoriq_ptp) 333 goto no_memory; 334 335 err = -ENODEV; 336 337 qoriq_ptp->caps = ptp_qoriq_caps; 338 339 if (of_property_read_u32(node, "fsl,cksel", &qoriq_ptp->cksel)) 340 qoriq_ptp->cksel = DEFAULT_CKSEL; 341 342 if (of_property_read_u32(node, 343 "fsl,tclk-period", &qoriq_ptp->tclk_period) || 344 of_property_read_u32(node, 345 "fsl,tmr-prsc", &qoriq_ptp->tmr_prsc) || 346 of_property_read_u32(node, 347 "fsl,tmr-add", &qoriq_ptp->tmr_add) || 348 of_property_read_u32(node, 349 "fsl,tmr-fiper1", &qoriq_ptp->tmr_fiper1) || 350 of_property_read_u32(node, 351 "fsl,tmr-fiper2", &qoriq_ptp->tmr_fiper2) || 352 of_property_read_u32(node, 353 "fsl,max-adj", &qoriq_ptp->caps.max_adj)) { 354 pr_err("device tree node missing required elements\n"); 355 goto no_node; 356 } 357 358 qoriq_ptp->irq = platform_get_irq(dev, 0); 359 360 if (qoriq_ptp->irq < 0) { 361 pr_err("irq not in device tree\n"); 362 goto no_node; 363 } 364 if (request_irq(qoriq_ptp->irq, isr, IRQF_SHARED, DRIVER, qoriq_ptp)) { 365 pr_err("request_irq failed\n"); 366 goto no_node; 367 } 368 369 qoriq_ptp->rsrc = platform_get_resource(dev, IORESOURCE_MEM, 0); 370 if (!qoriq_ptp->rsrc) { 371 pr_err("no resource\n"); 372 goto no_resource; 373 } 374 if (request_resource(&iomem_resource, qoriq_ptp->rsrc)) { 375 pr_err("resource busy\n"); 376 goto no_resource; 377 } 378 379 spin_lock_init(&qoriq_ptp->lock); 380 381 base = ioremap(qoriq_ptp->rsrc->start, 382 resource_size(qoriq_ptp->rsrc)); 383 if (!base) { 384 pr_err("ioremap ptp registers failed\n"); 385 goto no_ioremap; 386 } 387 388 qoriq_ptp->base = base; 389 390 if (of_device_is_compatible(node, "fsl,fman-ptp-timer")) { 391 qoriq_ptp->regs.ctrl_regs = base + FMAN_CTRL_REGS_OFFSET; 392 qoriq_ptp->regs.alarm_regs = base + FMAN_ALARM_REGS_OFFSET; 393 qoriq_ptp->regs.fiper_regs = base + FMAN_FIPER_REGS_OFFSET; 394 qoriq_ptp->regs.etts_regs = base + FMAN_ETTS_REGS_OFFSET; 395 } else { 396 qoriq_ptp->regs.ctrl_regs = base + CTRL_REGS_OFFSET; 397 qoriq_ptp->regs.alarm_regs = base + ALARM_REGS_OFFSET; 398 qoriq_ptp->regs.fiper_regs = base + FIPER_REGS_OFFSET; 399 qoriq_ptp->regs.etts_regs = base + ETTS_REGS_OFFSET; 400 } 401 402 ktime_get_real_ts64(&now); 403 ptp_qoriq_settime(&qoriq_ptp->caps, &now); 404 405 tmr_ctrl = 406 (qoriq_ptp->tclk_period & TCLK_PERIOD_MASK) << TCLK_PERIOD_SHIFT | 407 (qoriq_ptp->cksel & CKSEL_MASK) << CKSEL_SHIFT; 408 409 spin_lock_irqsave(&qoriq_ptp->lock, flags); 410 411 regs = &qoriq_ptp->regs; 412 qoriq_write(®s->ctrl_regs->tmr_ctrl, tmr_ctrl); 413 qoriq_write(®s->ctrl_regs->tmr_add, qoriq_ptp->tmr_add); 414 qoriq_write(®s->ctrl_regs->tmr_prsc, qoriq_ptp->tmr_prsc); 415 qoriq_write(®s->fiper_regs->tmr_fiper1, qoriq_ptp->tmr_fiper1); 416 qoriq_write(®s->fiper_regs->tmr_fiper2, qoriq_ptp->tmr_fiper2); 417 set_alarm(qoriq_ptp); 418 qoriq_write(®s->ctrl_regs->tmr_ctrl, tmr_ctrl|FIPERST|RTPE|TE|FRD); 419 420 spin_unlock_irqrestore(&qoriq_ptp->lock, flags); 421 422 qoriq_ptp->clock = ptp_clock_register(&qoriq_ptp->caps, &dev->dev); 423 if (IS_ERR(qoriq_ptp->clock)) { 424 err = PTR_ERR(qoriq_ptp->clock); 425 goto no_clock; 426 } 427 qoriq_ptp->phc_index = ptp_clock_index(qoriq_ptp->clock); 428 429 platform_set_drvdata(dev, qoriq_ptp); 430 431 return 0; 432 433 no_clock: 434 iounmap(qoriq_ptp->base); 435 no_ioremap: 436 release_resource(qoriq_ptp->rsrc); 437 no_resource: 438 free_irq(qoriq_ptp->irq, qoriq_ptp); 439 no_node: 440 kfree(qoriq_ptp); 441 no_memory: 442 return err; 443 } 444 445 static int qoriq_ptp_remove(struct platform_device *dev) 446 { 447 struct qoriq_ptp *qoriq_ptp = platform_get_drvdata(dev); 448 struct qoriq_ptp_registers *regs = &qoriq_ptp->regs; 449 450 qoriq_write(®s->ctrl_regs->tmr_temask, 0); 451 qoriq_write(®s->ctrl_regs->tmr_ctrl, 0); 452 453 ptp_clock_unregister(qoriq_ptp->clock); 454 iounmap(qoriq_ptp->base); 455 release_resource(qoriq_ptp->rsrc); 456 free_irq(qoriq_ptp->irq, qoriq_ptp); 457 kfree(qoriq_ptp); 458 459 return 0; 460 } 461 462 static const struct of_device_id match_table[] = { 463 { .compatible = "fsl,etsec-ptp" }, 464 { .compatible = "fsl,fman-ptp-timer" }, 465 {}, 466 }; 467 MODULE_DEVICE_TABLE(of, match_table); 468 469 static struct platform_driver qoriq_ptp_driver = { 470 .driver = { 471 .name = "ptp_qoriq", 472 .of_match_table = match_table, 473 }, 474 .probe = qoriq_ptp_probe, 475 .remove = qoriq_ptp_remove, 476 }; 477 478 module_platform_driver(qoriq_ptp_driver); 479 480 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>"); 481 MODULE_DESCRIPTION("PTP clock for Freescale QorIQ 1588 timer"); 482 MODULE_LICENSE("GPL"); 483