Blame view
drivers/ptp/ptp_ixp46x.c
6.72 KB
74ba9207e
|
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
32bd93e8f
|
2 3 4 5 |
/* * PTP 1588 clock using the IXP46X * * Copyright (C) 2010 OMICRON electronics GmbH |
32bd93e8f
|
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
*/ #include <linux/device.h> #include <linux/err.h> #include <linux/gpio.h> #include <linux/init.h> #include <linux/interrupt.h> #include <linux/io.h> #include <linux/irq.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/ptp_clock_kernel.h> #include <mach/ixp46x_ts.h> #define DRIVER "ptp_ixp46x" #define N_EXT_TS 2 #define MASTER_GPIO 8 #define MASTER_IRQ 25 #define SLAVE_GPIO 7 #define SLAVE_IRQ 24 struct ixp_clock { struct ixp46x_ts_regs *regs; struct ptp_clock *ptp_clock; struct ptp_clock_info caps; int exts0_enabled; int exts1_enabled; }; DEFINE_SPINLOCK(register_lock); /* * Register access functions */ static u64 ixp_systime_read(struct ixp46x_ts_regs *regs) { u64 ns; u32 lo, hi; lo = __raw_readl(®s->systime_lo); hi = __raw_readl(®s->systime_hi); ns = ((u64) hi) << 32; ns |= lo; ns <<= TICKS_NS_SHIFT; return ns; } static void ixp_systime_write(struct ixp46x_ts_regs *regs, u64 ns) { u32 hi, lo; ns >>= TICKS_NS_SHIFT; hi = ns >> 32; lo = ns & 0xffffffff; __raw_writel(lo, ®s->systime_lo); __raw_writel(hi, ®s->systime_hi); } /* * Interrupt service routine */ static irqreturn_t isr(int irq, void *priv) { struct ixp_clock *ixp_clock = priv; struct ixp46x_ts_regs *regs = ixp_clock->regs; struct ptp_clock_event event; u32 ack = 0, lo, hi, val; val = __raw_readl(®s->event); if (val & TSER_SNS) { ack |= TSER_SNS; if (ixp_clock->exts0_enabled) { hi = __raw_readl(®s->asms_hi); lo = __raw_readl(®s->asms_lo); event.type = PTP_CLOCK_EXTTS; event.index = 0; event.timestamp = ((u64) hi) << 32; event.timestamp |= lo; event.timestamp <<= TICKS_NS_SHIFT; ptp_clock_event(ixp_clock->ptp_clock, &event); } } if (val & TSER_SNM) { ack |= TSER_SNM; if (ixp_clock->exts1_enabled) { hi = __raw_readl(®s->amms_hi); lo = __raw_readl(®s->amms_lo); event.type = PTP_CLOCK_EXTTS; event.index = 1; event.timestamp = ((u64) hi) << 32; event.timestamp |= lo; event.timestamp <<= TICKS_NS_SHIFT; ptp_clock_event(ixp_clock->ptp_clock, &event); } } if (val & TTIPEND) ack |= TTIPEND; /* this bit seems to be always set */ if (ack) { __raw_writel(ack, ®s->event); return IRQ_HANDLED; } else return IRQ_NONE; } /* * PTP clock operations */ static int ptp_ixp_adjfreq(struct ptp_clock_info *ptp, s32 ppb) { u64 adj; u32 diff, addend; int neg_adj = 0; struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps); struct ixp46x_ts_regs *regs = ixp_clock->regs; if (ppb < 0) { neg_adj = 1; ppb = -ppb; } addend = DEFAULT_ADDEND; adj = addend; adj *= ppb; diff = div_u64(adj, 1000000000ULL); addend = neg_adj ? addend - diff : addend + diff; __raw_writel(addend, ®s->addend); return 0; } static int ptp_ixp_adjtime(struct ptp_clock_info *ptp, s64 delta) { s64 now; unsigned long flags; struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps); struct ixp46x_ts_regs *regs = ixp_clock->regs; spin_lock_irqsave(®ister_lock, flags); now = ixp_systime_read(regs); now += delta; ixp_systime_write(regs, now); spin_unlock_irqrestore(®ister_lock, flags); return 0; } |
1ca13de26
|
164 |
static int ptp_ixp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts) |
32bd93e8f
|
165 166 |
{ u64 ns; |
32bd93e8f
|
167 168 169 170 171 172 173 174 175 |
unsigned long flags; struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps); struct ixp46x_ts_regs *regs = ixp_clock->regs; spin_lock_irqsave(®ister_lock, flags); ns = ixp_systime_read(regs); spin_unlock_irqrestore(®ister_lock, flags); |
b83ef507d
|
176 |
*ts = ns_to_timespec64(ns); |
32bd93e8f
|
177 178 179 180 |
return 0; } static int ptp_ixp_settime(struct ptp_clock_info *ptp, |
1ca13de26
|
181 |
const struct timespec64 *ts) |
32bd93e8f
|
182 183 184 185 186 |
{ u64 ns; unsigned long flags; struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps); struct ixp46x_ts_regs *regs = ixp_clock->regs; |
b83ef507d
|
187 |
ns = timespec64_to_ns(ts); |
32bd93e8f
|
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
spin_lock_irqsave(®ister_lock, flags); ixp_systime_write(regs, ns); spin_unlock_irqrestore(®ister_lock, flags); return 0; } static int ptp_ixp_enable(struct ptp_clock_info *ptp, struct ptp_clock_request *rq, int on) { struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps); switch (rq->type) { case PTP_CLK_REQ_EXTTS: switch (rq->extts.index) { case 0: ixp_clock->exts0_enabled = on ? 1 : 0; break; case 1: ixp_clock->exts1_enabled = on ? 1 : 0; break; default: return -EINVAL; } return 0; default: break; } return -EOPNOTSUPP; } |
7d47e9a20
|
222 |
static const struct ptp_clock_info ptp_ixp_caps = { |
32bd93e8f
|
223 224 225 226 |
.owner = THIS_MODULE, .name = "IXP46X timer", .max_adj = 66666655, .n_ext_ts = N_EXT_TS, |
4986b4f00
|
227 |
.n_pins = 0, |
32bd93e8f
|
228 229 230 |
.pps = 0, .adjfreq = ptp_ixp_adjfreq, .adjtime = ptp_ixp_adjtime, |
1ca13de26
|
231 232 |
.gettime64 = ptp_ixp_gettime, .settime64 = ptp_ixp_settime, |
32bd93e8f
|
233 234 235 236 237 238 239 240 241 242 |
.enable = ptp_ixp_enable, }; /* module operations */ static struct ixp_clock ixp_clock; static int setup_interrupt(int gpio) { int irq; |
dc6ab07d8
|
243 |
int err; |
32bd93e8f
|
244 |
|
dc6ab07d8
|
245 246 247 248 249 250 251 |
err = gpio_request(gpio, "ixp4-ptp"); if (err) return err; err = gpio_direction_input(gpio); if (err) return err; |
32bd93e8f
|
252 253 |
irq = gpio_to_irq(gpio); |
cf86799e8
|
254 255 |
if (irq < 0) return irq; |
32bd93e8f
|
256 |
|
cf86799e8
|
257 258 |
err = irq_set_irq_type(irq, IRQF_TRIGGER_FALLING); if (err) { |
32bd93e8f
|
259 260 |
pr_err("cannot set trigger type for irq %d ", irq); |
cf86799e8
|
261 |
return err; |
32bd93e8f
|
262 |
} |
cf86799e8
|
263 264 |
err = request_irq(irq, isr, 0, DRIVER, &ixp_clock); if (err) { |
32bd93e8f
|
265 266 |
pr_err("request_irq failed for irq %d ", irq); |
cf86799e8
|
267 |
return err; |
32bd93e8f
|
268 269 270 271 272 273 274 275 276 |
} return irq; } static void __exit ptp_ixp_exit(void) { free_irq(MASTER_IRQ, &ixp_clock); free_irq(SLAVE_IRQ, &ixp_clock); |
c1e6aaf0a
|
277 |
ixp46x_phc_index = -1; |
32bd93e8f
|
278 279 280 281 282 283 284 285 286 287 288 289 |
ptp_clock_unregister(ixp_clock.ptp_clock); } static int __init ptp_ixp_init(void) { if (!cpu_is_ixp46x()) return -ENODEV; ixp_clock.regs = (struct ixp46x_ts_regs __iomem *) IXP4XX_TIMESYNC_BASE_VIRT; ixp_clock.caps = ptp_ixp_caps; |
1ef761582
|
290 |
ixp_clock.ptp_clock = ptp_clock_register(&ixp_clock.caps, NULL); |
32bd93e8f
|
291 292 293 |
if (IS_ERR(ixp_clock.ptp_clock)) return PTR_ERR(ixp_clock.ptp_clock); |
c1e6aaf0a
|
294 |
ixp46x_phc_index = ptp_clock_index(ixp_clock.ptp_clock); |
509a7c257
|
295 |
|
32bd93e8f
|
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
__raw_writel(DEFAULT_ADDEND, &ixp_clock.regs->addend); __raw_writel(1, &ixp_clock.regs->trgt_lo); __raw_writel(0, &ixp_clock.regs->trgt_hi); __raw_writel(TTIPEND, &ixp_clock.regs->event); if (MASTER_IRQ != setup_interrupt(MASTER_GPIO)) { pr_err("failed to setup gpio %d as irq ", MASTER_GPIO); goto no_master; } if (SLAVE_IRQ != setup_interrupt(SLAVE_GPIO)) { pr_err("failed to setup gpio %d as irq ", SLAVE_GPIO); goto no_slave; } return 0; no_slave: free_irq(MASTER_IRQ, &ixp_clock); no_master: ptp_clock_unregister(ixp_clock.ptp_clock); return -ENODEV; } module_init(ptp_ixp_init); module_exit(ptp_ixp_exit); |
c2ec3ff6b
|
322 |
MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>"); |
32bd93e8f
|
323 324 |
MODULE_DESCRIPTION("PTP clock using the IXP46X timer"); MODULE_LICENSE("GPL"); |