blob: beb5f74944cdf95f4d083e57bab7699f7c20014d [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Yangbo Luceefc71d2018-05-25 12:40:34 +08002/*
3 * PTP 1588 clock for Freescale QorIQ 1588 timer
4 *
5 * Copyright (C) 2010 OMICRON electronics GmbH
Yangbo Luceefc71d2018-05-25 12:40:34 +08006 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/device.h>
11#include <linux/hrtimer.h>
Yangbo Luceefc71d2018-05-25 12:40:34 +080012#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/of_platform.h>
16#include <linux/timex.h>
Yangbo Luceefc71d2018-05-25 12:40:34 +080017#include <linux/slab.h>
Yangbo Lu91305f22018-08-01 18:05:54 +080018#include <linux/clk.h>
Yangbo Luceefc71d2018-05-25 12:40:34 +080019
Yangbo Lu6c50c1e2018-05-25 12:40:35 +080020#include <linux/fsl/ptp_qoriq.h>
Yangbo Luceefc71d2018-05-25 12:40:34 +080021
22/*
23 * Register access functions
24 */
25
Yangbo Lu1e562c82019-02-12 12:23:56 +080026/* Caller must hold ptp_qoriq->lock. */
27static u64 tmr_cnt_read(struct ptp_qoriq *ptp_qoriq)
Yangbo Luceefc71d2018-05-25 12:40:34 +080028{
Yangbo Lu1e562c82019-02-12 12:23:56 +080029 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +080030 u64 ns;
31 u32 lo, hi;
32
Yangbo Luf038ddf2019-02-12 12:23:59 +080033 lo = ptp_qoriq->read(&regs->ctrl_regs->tmr_cnt_l);
34 hi = ptp_qoriq->read(&regs->ctrl_regs->tmr_cnt_h);
Yangbo Luceefc71d2018-05-25 12:40:34 +080035 ns = ((u64) hi) << 32;
36 ns |= lo;
37 return ns;
38}
39
Yangbo Lu1e562c82019-02-12 12:23:56 +080040/* Caller must hold ptp_qoriq->lock. */
41static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns)
Yangbo Luceefc71d2018-05-25 12:40:34 +080042{
Yangbo Lu1e562c82019-02-12 12:23:56 +080043 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +080044 u32 hi = ns >> 32;
45 u32 lo = ns & 0xffffffff;
46
Yangbo Luf038ddf2019-02-12 12:23:59 +080047 ptp_qoriq->write(&regs->ctrl_regs->tmr_cnt_l, lo);
48 ptp_qoriq->write(&regs->ctrl_regs->tmr_cnt_h, hi);
Yangbo Luceefc71d2018-05-25 12:40:34 +080049}
50
Yangbo Lu1e562c82019-02-12 12:23:56 +080051/* Caller must hold ptp_qoriq->lock. */
52static void set_alarm(struct ptp_qoriq *ptp_qoriq)
Yangbo Luceefc71d2018-05-25 12:40:34 +080053{
Yangbo Lu1e562c82019-02-12 12:23:56 +080054 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +080055 u64 ns;
56 u32 lo, hi;
57
Yangbo Lu1e562c82019-02-12 12:23:56 +080058 ns = tmr_cnt_read(ptp_qoriq) + 1500000000ULL;
Yangbo Luceefc71d2018-05-25 12:40:34 +080059 ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
Yangbo Lu1e562c82019-02-12 12:23:56 +080060 ns -= ptp_qoriq->tclk_period;
Yangbo Luceefc71d2018-05-25 12:40:34 +080061 hi = ns >> 32;
62 lo = ns & 0xffffffff;
Yangbo Luf038ddf2019-02-12 12:23:59 +080063 ptp_qoriq->write(&regs->alarm_regs->tmr_alarm1_l, lo);
64 ptp_qoriq->write(&regs->alarm_regs->tmr_alarm1_h, hi);
Yangbo Luceefc71d2018-05-25 12:40:34 +080065}
66
Yangbo Lu1e562c82019-02-12 12:23:56 +080067/* Caller must hold ptp_qoriq->lock. */
68static void set_fipers(struct ptp_qoriq *ptp_qoriq)
Yangbo Luceefc71d2018-05-25 12:40:34 +080069{
Yangbo Lu1e562c82019-02-12 12:23:56 +080070 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Lua8f62d02018-06-25 20:37:08 +080071
Yangbo Lu1e562c82019-02-12 12:23:56 +080072 set_alarm(ptp_qoriq);
Yangbo Luf038ddf2019-02-12 12:23:59 +080073 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper1, ptp_qoriq->tmr_fiper1);
74 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper2, ptp_qoriq->tmr_fiper2);
Yangbo Lu6d23d832020-09-18 17:48:01 +080075
76 if (ptp_qoriq->fiper3_support)
77 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper3,
78 ptp_qoriq->tmr_fiper3);
Yangbo Luceefc71d2018-05-25 12:40:34 +080079}
80
Yangbo Lu94294392019-12-12 18:08:05 +080081int extts_clean_up(struct ptp_qoriq *ptp_qoriq, int index, bool update_event)
Yangbo Lu6815d8b2019-01-21 18:41:39 +080082{
Yangbo Lu1e562c82019-02-12 12:23:56 +080083 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Lu6815d8b2019-01-21 18:41:39 +080084 struct ptp_clock_event event;
85 void __iomem *reg_etts_l;
86 void __iomem *reg_etts_h;
Yangbo Lu10bc8772019-12-12 18:08:04 +080087 u32 valid, lo, hi;
Yangbo Lu6815d8b2019-01-21 18:41:39 +080088
89 switch (index) {
90 case 0:
91 valid = ETS1_VLD;
92 reg_etts_l = &regs->etts_regs->tmr_etts1_l;
93 reg_etts_h = &regs->etts_regs->tmr_etts1_h;
94 break;
95 case 1:
96 valid = ETS2_VLD;
97 reg_etts_l = &regs->etts_regs->tmr_etts2_l;
98 reg_etts_h = &regs->etts_regs->tmr_etts2_h;
99 break;
100 default:
101 return -EINVAL;
102 }
103
104 event.type = PTP_CLOCK_EXTTS;
105 event.index = index;
106
Yangbo Lu10bc8772019-12-12 18:08:04 +0800107 if (ptp_qoriq->extts_fifo_support)
108 if (!(ptp_qoriq->read(&regs->ctrl_regs->tmr_stat) & valid))
109 return 0;
110
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800111 do {
Yangbo Luf038ddf2019-02-12 12:23:59 +0800112 lo = ptp_qoriq->read(reg_etts_l);
113 hi = ptp_qoriq->read(reg_etts_h);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800114
115 if (update_event) {
116 event.timestamp = ((u64) hi) << 32;
117 event.timestamp |= lo;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800118 ptp_clock_event(ptp_qoriq->clock, &event);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800119 }
120
Yangbo Lu10bc8772019-12-12 18:08:04 +0800121 if (!ptp_qoriq->extts_fifo_support)
122 break;
123 } while (ptp_qoriq->read(&regs->ctrl_regs->tmr_stat) & valid);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800124
125 return 0;
126}
Yangbo Lu94294392019-12-12 18:08:05 +0800127EXPORT_SYMBOL_GPL(extts_clean_up);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800128
Yangbo Luceefc71d2018-05-25 12:40:34 +0800129/*
130 * Interrupt service routine
131 */
132
Yangbo Lu73356e42019-02-12 12:23:57 +0800133irqreturn_t ptp_qoriq_isr(int irq, void *priv)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800134{
Yangbo Lu1e562c82019-02-12 12:23:56 +0800135 struct ptp_qoriq *ptp_qoriq = priv;
136 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800137 struct ptp_clock_event event;
Yangbo Lud71151a2020-02-11 12:52:49 +0800138 u32 ack = 0, mask, val, irqs;
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800139
Yangbo Lu1e562c82019-02-12 12:23:56 +0800140 spin_lock(&ptp_qoriq->lock);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800141
Yangbo Luf038ddf2019-02-12 12:23:59 +0800142 val = ptp_qoriq->read(&regs->ctrl_regs->tmr_tevent);
143 mask = ptp_qoriq->read(&regs->ctrl_regs->tmr_temask);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800144
Yangbo Lu1e562c82019-02-12 12:23:56 +0800145 spin_unlock(&ptp_qoriq->lock);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800146
147 irqs = val & mask;
148
149 if (irqs & ETS1) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800150 ack |= ETS1;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800151 extts_clean_up(ptp_qoriq, 0, true);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800152 }
153
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800154 if (irqs & ETS2) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800155 ack |= ETS2;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800156 extts_clean_up(ptp_qoriq, 1, true);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800157 }
158
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800159 if (irqs & PP1) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800160 ack |= PP1;
161 event.type = PTP_CLOCK_PPS;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800162 ptp_clock_event(ptp_qoriq->clock, &event);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800163 }
164
165 if (ack) {
Yangbo Luf038ddf2019-02-12 12:23:59 +0800166 ptp_qoriq->write(&regs->ctrl_regs->tmr_tevent, ack);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800167 return IRQ_HANDLED;
168 } else
169 return IRQ_NONE;
170}
Yangbo Lu73356e42019-02-12 12:23:57 +0800171EXPORT_SYMBOL_GPL(ptp_qoriq_isr);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800172
173/*
174 * PTP clock operations
175 */
176
Yangbo Lu73356e42019-02-12 12:23:57 +0800177int ptp_qoriq_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800178{
179 u64 adj, diff;
180 u32 tmr_add;
181 int neg_adj = 0;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800182 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
183 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800184
185 if (scaled_ppm < 0) {
186 neg_adj = 1;
187 scaled_ppm = -scaled_ppm;
188 }
Yangbo Lu1e562c82019-02-12 12:23:56 +0800189 tmr_add = ptp_qoriq->tmr_add;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800190 adj = tmr_add;
191
192 /* calculate diff as adj*(scaled_ppm/65536)/1000000
193 * and round() to the nearest integer
194 */
195 adj *= scaled_ppm;
196 diff = div_u64(adj, 8000000);
197 diff = (diff >> 13) + ((diff >> 12) & 1);
198
199 tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
200
Yangbo Luf038ddf2019-02-12 12:23:59 +0800201 ptp_qoriq->write(&regs->ctrl_regs->tmr_add, tmr_add);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800202
203 return 0;
204}
Yangbo Lu73356e42019-02-12 12:23:57 +0800205EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800206
Yangbo Lu73356e42019-02-12 12:23:57 +0800207int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800208{
209 s64 now;
210 unsigned long flags;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800211 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800212
Yangbo Lu1e562c82019-02-12 12:23:56 +0800213 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800214
Yangbo Lu1e562c82019-02-12 12:23:56 +0800215 now = tmr_cnt_read(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800216 now += delta;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800217 tmr_cnt_write(ptp_qoriq, now);
218 set_fipers(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800219
Yangbo Lu1e562c82019-02-12 12:23:56 +0800220 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800221
222 return 0;
223}
Yangbo Lu73356e42019-02-12 12:23:57 +0800224EXPORT_SYMBOL_GPL(ptp_qoriq_adjtime);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800225
Yangbo Lu73356e42019-02-12 12:23:57 +0800226int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800227{
228 u64 ns;
229 unsigned long flags;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800230 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800231
Yangbo Lu1e562c82019-02-12 12:23:56 +0800232 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800233
Yangbo Lu1e562c82019-02-12 12:23:56 +0800234 ns = tmr_cnt_read(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800235
Yangbo Lu1e562c82019-02-12 12:23:56 +0800236 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800237
238 *ts = ns_to_timespec64(ns);
239
240 return 0;
241}
Yangbo Lu73356e42019-02-12 12:23:57 +0800242EXPORT_SYMBOL_GPL(ptp_qoriq_gettime);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800243
Yangbo Lu73356e42019-02-12 12:23:57 +0800244int ptp_qoriq_settime(struct ptp_clock_info *ptp,
245 const struct timespec64 *ts)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800246{
247 u64 ns;
248 unsigned long flags;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800249 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800250
251 ns = timespec64_to_ns(ts);
252
Yangbo Lu1e562c82019-02-12 12:23:56 +0800253 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800254
Yangbo Lu1e562c82019-02-12 12:23:56 +0800255 tmr_cnt_write(ptp_qoriq, ns);
256 set_fipers(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800257
Yangbo Lu1e562c82019-02-12 12:23:56 +0800258 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800259
260 return 0;
261}
Yangbo Lu73356e42019-02-12 12:23:57 +0800262EXPORT_SYMBOL_GPL(ptp_qoriq_settime);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800263
Yangbo Lu73356e42019-02-12 12:23:57 +0800264int ptp_qoriq_enable(struct ptp_clock_info *ptp,
265 struct ptp_clock_request *rq, int on)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800266{
Yangbo Lu1e562c82019-02-12 12:23:56 +0800267 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
268 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800269 unsigned long flags;
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800270 u32 bit, mask = 0;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800271
272 switch (rq->type) {
273 case PTP_CLK_REQ_EXTTS:
274 switch (rq->extts.index) {
275 case 0:
276 bit = ETS1EN;
277 break;
278 case 1:
279 bit = ETS2EN;
280 break;
281 default:
282 return -EINVAL;
283 }
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800284
285 if (on)
Yangbo Lu1e562c82019-02-12 12:23:56 +0800286 extts_clean_up(ptp_qoriq, rq->extts.index, false);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800287
Yangbo Luceefc71d2018-05-25 12:40:34 +0800288 break;
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800289 case PTP_CLK_REQ_PPS:
290 bit = PP1EN;
291 break;
292 default:
293 return -EOPNOTSUPP;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800294 }
295
Yangbo Lu1e562c82019-02-12 12:23:56 +0800296 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800297
Yangbo Luf038ddf2019-02-12 12:23:59 +0800298 mask = ptp_qoriq->read(&regs->ctrl_regs->tmr_temask);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800299 if (on) {
300 mask |= bit;
Yangbo Luf038ddf2019-02-12 12:23:59 +0800301 ptp_qoriq->write(&regs->ctrl_regs->tmr_tevent, bit);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800302 } else {
303 mask &= ~bit;
304 }
305
Yangbo Luf038ddf2019-02-12 12:23:59 +0800306 ptp_qoriq->write(&regs->ctrl_regs->tmr_temask, mask);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800307
Yangbo Lu1e562c82019-02-12 12:23:56 +0800308 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800309 return 0;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800310}
Yangbo Lu73356e42019-02-12 12:23:57 +0800311EXPORT_SYMBOL_GPL(ptp_qoriq_enable);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800312
313static const struct ptp_clock_info ptp_qoriq_caps = {
314 .owner = THIS_MODULE,
315 .name = "qoriq ptp clock",
316 .max_adj = 512000,
317 .n_alarm = 0,
318 .n_ext_ts = N_EXT_TS,
319 .n_per_out = 0,
320 .n_pins = 0,
321 .pps = 1,
322 .adjfine = ptp_qoriq_adjfine,
323 .adjtime = ptp_qoriq_adjtime,
324 .gettime64 = ptp_qoriq_gettime,
325 .settime64 = ptp_qoriq_settime,
326 .enable = ptp_qoriq_enable,
327};
328
Yangbo Lu91305f22018-08-01 18:05:54 +0800329/**
Yangbo Lu1e562c82019-02-12 12:23:56 +0800330 * ptp_qoriq_nominal_freq - calculate nominal frequency according to
Yangbo Lu91305f22018-08-01 18:05:54 +0800331 * reference clock frequency
332 *
333 * @clk_src: reference clock frequency
334 *
335 * The nominal frequency is the desired clock frequency.
336 * It should be less than the reference clock frequency.
337 * It should be a factor of 1000MHz.
338 *
339 * Return the nominal frequency
340 */
Yangbo Lu1e562c82019-02-12 12:23:56 +0800341static u32 ptp_qoriq_nominal_freq(u32 clk_src)
Yangbo Lu91305f22018-08-01 18:05:54 +0800342{
343 u32 remainder = 0;
344
345 clk_src /= 1000000;
346 remainder = clk_src % 100;
347 if (remainder) {
348 clk_src -= remainder;
349 clk_src += 100;
350 }
351
352 do {
353 clk_src -= 100;
354
355 } while (1000 % clk_src);
356
357 return clk_src * 1000000;
358}
359
360/**
Yangbo Lu1e562c82019-02-12 12:23:56 +0800361 * ptp_qoriq_auto_config - calculate a set of default configurations
Yangbo Lu91305f22018-08-01 18:05:54 +0800362 *
Yangbo Lu1e562c82019-02-12 12:23:56 +0800363 * @ptp_qoriq: pointer to ptp_qoriq
Yangbo Lu91305f22018-08-01 18:05:54 +0800364 * @node: pointer to device_node
365 *
366 * If below dts properties are not provided, this function will be
367 * called to calculate a set of default configurations for them.
368 * "fsl,tclk-period"
369 * "fsl,tmr-prsc"
370 * "fsl,tmr-add"
371 * "fsl,tmr-fiper1"
372 * "fsl,tmr-fiper2"
Yangbo Lu6d23d832020-09-18 17:48:01 +0800373 * "fsl,tmr-fiper3" (required only for DPAA2 and ENETC hardware)
Yangbo Lu91305f22018-08-01 18:05:54 +0800374 * "fsl,max-adj"
375 *
376 * Return 0 if success
377 */
Yangbo Lu1e562c82019-02-12 12:23:56 +0800378static int ptp_qoriq_auto_config(struct ptp_qoriq *ptp_qoriq,
Yangbo Lu91305f22018-08-01 18:05:54 +0800379 struct device_node *node)
380{
381 struct clk *clk;
382 u64 freq_comp;
383 u64 max_adj;
384 u32 nominal_freq;
Yangbo Lu74c05a32018-08-06 12:39:11 +0800385 u32 remainder = 0;
Yangbo Lu91305f22018-08-01 18:05:54 +0800386 u32 clk_src = 0;
387
Yangbo Lu1e562c82019-02-12 12:23:56 +0800388 ptp_qoriq->cksel = DEFAULT_CKSEL;
Yangbo Lu91305f22018-08-01 18:05:54 +0800389
390 clk = of_clk_get(node, 0);
391 if (!IS_ERR(clk)) {
392 clk_src = clk_get_rate(clk);
393 clk_put(clk);
394 }
395
396 if (clk_src <= 100000000UL) {
397 pr_err("error reference clock value, or lower than 100MHz\n");
398 return -EINVAL;
399 }
400
Yangbo Lu1e562c82019-02-12 12:23:56 +0800401 nominal_freq = ptp_qoriq_nominal_freq(clk_src);
Yangbo Lu91305f22018-08-01 18:05:54 +0800402 if (!nominal_freq)
403 return -EINVAL;
404
Yangbo Lu1e562c82019-02-12 12:23:56 +0800405 ptp_qoriq->tclk_period = 1000000000UL / nominal_freq;
406 ptp_qoriq->tmr_prsc = DEFAULT_TMR_PRSC;
Yangbo Lu91305f22018-08-01 18:05:54 +0800407
408 /* Calculate initial frequency compensation value for TMR_ADD register.
409 * freq_comp = ceil(2^32 / freq_ratio)
410 * freq_ratio = reference_clock_freq / nominal_freq
411 */
412 freq_comp = ((u64)1 << 32) * nominal_freq;
Yangbo Lu74c05a32018-08-06 12:39:11 +0800413 freq_comp = div_u64_rem(freq_comp, clk_src, &remainder);
414 if (remainder)
Yangbo Lu91305f22018-08-01 18:05:54 +0800415 freq_comp++;
416
Yangbo Lu1e562c82019-02-12 12:23:56 +0800417 ptp_qoriq->tmr_add = freq_comp;
418 ptp_qoriq->tmr_fiper1 = DEFAULT_FIPER1_PERIOD - ptp_qoriq->tclk_period;
419 ptp_qoriq->tmr_fiper2 = DEFAULT_FIPER2_PERIOD - ptp_qoriq->tclk_period;
Yangbo Lu6d23d832020-09-18 17:48:01 +0800420 ptp_qoriq->tmr_fiper3 = DEFAULT_FIPER3_PERIOD - ptp_qoriq->tclk_period;
Yangbo Lu91305f22018-08-01 18:05:54 +0800421
422 /* max_adj = 1000000000 * (freq_ratio - 1.0) - 1
423 * freq_ratio = reference_clock_freq / nominal_freq
424 */
425 max_adj = 1000000000ULL * (clk_src - nominal_freq);
Yangbo Lu74c05a32018-08-06 12:39:11 +0800426 max_adj = div_u64(max_adj, nominal_freq) - 1;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800427 ptp_qoriq->caps.max_adj = max_adj;
Yangbo Lu91305f22018-08-01 18:05:54 +0800428
429 return 0;
430}
431
Yangbo Luff545712019-02-12 12:23:58 +0800432int ptp_qoriq_init(struct ptp_qoriq *ptp_qoriq, void __iomem *base,
Colin Ian King58066ac2019-02-19 14:21:20 +0000433 const struct ptp_clock_info *caps)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800434{
Yangbo Luff545712019-02-12 12:23:58 +0800435 struct device_node *node = ptp_qoriq->dev->of_node;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800436 struct ptp_qoriq_registers *regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800437 struct timespec64 now;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800438 unsigned long flags;
Yangbo Luff545712019-02-12 12:23:58 +0800439 u32 tmr_ctrl;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800440
Claudiu Manoil7f4399b2019-05-09 03:07:12 +0000441 if (!node)
442 return -ENODEV;
443
Yangbo Luff545712019-02-12 12:23:58 +0800444 ptp_qoriq->base = base;
Colin Ian King58066ac2019-02-19 14:21:20 +0000445 ptp_qoriq->caps = *caps;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800446
Yangbo Lu1e562c82019-02-12 12:23:56 +0800447 if (of_property_read_u32(node, "fsl,cksel", &ptp_qoriq->cksel))
448 ptp_qoriq->cksel = DEFAULT_CKSEL;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800449
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800450 if (of_property_read_bool(node, "fsl,extts-fifo"))
Yangbo Lu1e562c82019-02-12 12:23:56 +0800451 ptp_qoriq->extts_fifo_support = true;
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800452 else
Yangbo Lu1e562c82019-02-12 12:23:56 +0800453 ptp_qoriq->extts_fifo_support = false;
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800454
Yangbo Lu6d23d832020-09-18 17:48:01 +0800455 if (of_device_is_compatible(node, "fsl,dpaa2-ptp") ||
456 of_device_is_compatible(node, "fsl,enetc-ptp"))
457 ptp_qoriq->fiper3_support = true;
458
Yangbo Luceefc71d2018-05-25 12:40:34 +0800459 if (of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800460 "fsl,tclk-period", &ptp_qoriq->tclk_period) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800461 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800462 "fsl,tmr-prsc", &ptp_qoriq->tmr_prsc) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800463 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800464 "fsl,tmr-add", &ptp_qoriq->tmr_add) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800465 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800466 "fsl,tmr-fiper1", &ptp_qoriq->tmr_fiper1) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800467 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800468 "fsl,tmr-fiper2", &ptp_qoriq->tmr_fiper2) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800469 of_property_read_u32(node,
Yangbo Lu6d23d832020-09-18 17:48:01 +0800470 "fsl,max-adj", &ptp_qoriq->caps.max_adj) ||
471 (ptp_qoriq->fiper3_support &&
472 of_property_read_u32(node, "fsl,tmr-fiper3",
473 &ptp_qoriq->tmr_fiper3))) {
Yangbo Lu91305f22018-08-01 18:05:54 +0800474 pr_warn("device tree node missing required elements, try automatic configuration\n");
475
Yangbo Lu1e562c82019-02-12 12:23:56 +0800476 if (ptp_qoriq_auto_config(ptp_qoriq, node))
Yangbo Luff545712019-02-12 12:23:58 +0800477 return -ENODEV;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800478 }
479
Yangbo Luf038ddf2019-02-12 12:23:59 +0800480 if (of_property_read_bool(node, "little-endian")) {
481 ptp_qoriq->read = qoriq_read_le;
482 ptp_qoriq->write = qoriq_write_le;
483 } else {
484 ptp_qoriq->read = qoriq_read_be;
485 ptp_qoriq->write = qoriq_write_be;
486 }
487
Yangbo Lud4e17682019-02-12 12:24:01 +0800488 /* The eTSEC uses differnt memory map with DPAA/ENETC */
489 if (of_device_is_compatible(node, "fsl,etsec-ptp")) {
490 ptp_qoriq->regs.ctrl_regs = base + ETSEC_CTRL_REGS_OFFSET;
491 ptp_qoriq->regs.alarm_regs = base + ETSEC_ALARM_REGS_OFFSET;
492 ptp_qoriq->regs.fiper_regs = base + ETSEC_FIPER_REGS_OFFSET;
493 ptp_qoriq->regs.etts_regs = base + ETSEC_ETTS_REGS_OFFSET;
Yangbo Lua8f62d02018-06-25 20:37:08 +0800494 } else {
Yangbo Lu1e562c82019-02-12 12:23:56 +0800495 ptp_qoriq->regs.ctrl_regs = base + CTRL_REGS_OFFSET;
496 ptp_qoriq->regs.alarm_regs = base + ALARM_REGS_OFFSET;
497 ptp_qoriq->regs.fiper_regs = base + FIPER_REGS_OFFSET;
498 ptp_qoriq->regs.etts_regs = base + ETTS_REGS_OFFSET;
Yangbo Lua8f62d02018-06-25 20:37:08 +0800499 }
500
Vladimir Olteandb34a472019-10-01 22:07:01 +0300501 spin_lock_init(&ptp_qoriq->lock);
502
Arnd Bergmannf696a212018-06-18 16:20:39 +0200503 ktime_get_real_ts64(&now);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800504 ptp_qoriq_settime(&ptp_qoriq->caps, &now);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800505
506 tmr_ctrl =
Yangbo Lu1e562c82019-02-12 12:23:56 +0800507 (ptp_qoriq->tclk_period & TCLK_PERIOD_MASK) << TCLK_PERIOD_SHIFT |
508 (ptp_qoriq->cksel & CKSEL_MASK) << CKSEL_SHIFT;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800509
Yangbo Lu1e562c82019-02-12 12:23:56 +0800510 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800511
Yangbo Lu1e562c82019-02-12 12:23:56 +0800512 regs = &ptp_qoriq->regs;
Yangbo Luf038ddf2019-02-12 12:23:59 +0800513 ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl, tmr_ctrl);
514 ptp_qoriq->write(&regs->ctrl_regs->tmr_add, ptp_qoriq->tmr_add);
515 ptp_qoriq->write(&regs->ctrl_regs->tmr_prsc, ptp_qoriq->tmr_prsc);
516 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper1, ptp_qoriq->tmr_fiper1);
517 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper2, ptp_qoriq->tmr_fiper2);
Yangbo Lu6d23d832020-09-18 17:48:01 +0800518
519 if (ptp_qoriq->fiper3_support)
520 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper3,
521 ptp_qoriq->tmr_fiper3);
522
Yangbo Lu1e562c82019-02-12 12:23:56 +0800523 set_alarm(ptp_qoriq);
Yangbo Luf038ddf2019-02-12 12:23:59 +0800524 ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl,
525 tmr_ctrl|FIPERST|RTPE|TE|FRD);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800526
Yangbo Lu1e562c82019-02-12 12:23:56 +0800527 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800528
Yangbo Luff545712019-02-12 12:23:58 +0800529 ptp_qoriq->clock = ptp_clock_register(&ptp_qoriq->caps, ptp_qoriq->dev);
530 if (IS_ERR(ptp_qoriq->clock))
531 return PTR_ERR(ptp_qoriq->clock);
532
Yangbo Lu1e562c82019-02-12 12:23:56 +0800533 ptp_qoriq->phc_index = ptp_clock_index(ptp_qoriq->clock);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800534 ptp_qoriq_create_debugfs(ptp_qoriq);
Yangbo Luff545712019-02-12 12:23:58 +0800535 return 0;
536}
537EXPORT_SYMBOL_GPL(ptp_qoriq_init);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800538
Yangbo Luff545712019-02-12 12:23:58 +0800539void ptp_qoriq_free(struct ptp_qoriq *ptp_qoriq)
540{
541 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
542
Yangbo Luf038ddf2019-02-12 12:23:59 +0800543 ptp_qoriq->write(&regs->ctrl_regs->tmr_temask, 0);
544 ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl, 0);
Yangbo Luff545712019-02-12 12:23:58 +0800545
546 ptp_qoriq_remove_debugfs(ptp_qoriq);
547 ptp_clock_unregister(ptp_qoriq->clock);
548 iounmap(ptp_qoriq->base);
549 free_irq(ptp_qoriq->irq, ptp_qoriq);
550}
551EXPORT_SYMBOL_GPL(ptp_qoriq_free);
552
553static int ptp_qoriq_probe(struct platform_device *dev)
554{
555 struct ptp_qoriq *ptp_qoriq;
556 int err = -ENOMEM;
557 void __iomem *base;
558
559 ptp_qoriq = kzalloc(sizeof(*ptp_qoriq), GFP_KERNEL);
560 if (!ptp_qoriq)
561 goto no_memory;
562
563 ptp_qoriq->dev = &dev->dev;
564
565 err = -ENODEV;
566
567 ptp_qoriq->irq = platform_get_irq(dev, 0);
568 if (ptp_qoriq->irq < 0) {
569 pr_err("irq not in device tree\n");
570 goto no_node;
571 }
572 if (request_irq(ptp_qoriq->irq, ptp_qoriq_isr, IRQF_SHARED,
573 DRIVER, ptp_qoriq)) {
574 pr_err("request_irq failed\n");
575 goto no_node;
576 }
577
578 ptp_qoriq->rsrc = platform_get_resource(dev, IORESOURCE_MEM, 0);
579 if (!ptp_qoriq->rsrc) {
580 pr_err("no resource\n");
581 goto no_resource;
582 }
583 if (request_resource(&iomem_resource, ptp_qoriq->rsrc)) {
584 pr_err("resource busy\n");
585 goto no_resource;
586 }
587
588 base = ioremap(ptp_qoriq->rsrc->start,
589 resource_size(ptp_qoriq->rsrc));
590 if (!base) {
591 pr_err("ioremap ptp registers failed\n");
592 goto no_ioremap;
593 }
594
Colin Ian King58066ac2019-02-19 14:21:20 +0000595 err = ptp_qoriq_init(ptp_qoriq, base, &ptp_qoriq_caps);
Yangbo Luff545712019-02-12 12:23:58 +0800596 if (err)
597 goto no_clock;
598
599 platform_set_drvdata(dev, ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800600 return 0;
601
602no_clock:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800603 iounmap(ptp_qoriq->base);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800604no_ioremap:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800605 release_resource(ptp_qoriq->rsrc);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800606no_resource:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800607 free_irq(ptp_qoriq->irq, ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800608no_node:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800609 kfree(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800610no_memory:
611 return err;
612}
613
Yangbo Lu1e562c82019-02-12 12:23:56 +0800614static int ptp_qoriq_remove(struct platform_device *dev)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800615{
Yangbo Lu1e562c82019-02-12 12:23:56 +0800616 struct ptp_qoriq *ptp_qoriq = platform_get_drvdata(dev);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800617
Yangbo Luff545712019-02-12 12:23:58 +0800618 ptp_qoriq_free(ptp_qoriq);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800619 release_resource(ptp_qoriq->rsrc);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800620 kfree(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800621 return 0;
622}
623
624static const struct of_device_id match_table[] = {
625 { .compatible = "fsl,etsec-ptp" },
Yangbo Lua8f62d02018-06-25 20:37:08 +0800626 { .compatible = "fsl,fman-ptp-timer" },
Yangbo Luceefc71d2018-05-25 12:40:34 +0800627 {},
628};
629MODULE_DEVICE_TABLE(of, match_table);
630
Yangbo Lu1e562c82019-02-12 12:23:56 +0800631static struct platform_driver ptp_qoriq_driver = {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800632 .driver = {
633 .name = "ptp_qoriq",
634 .of_match_table = match_table,
635 },
Yangbo Lu1e562c82019-02-12 12:23:56 +0800636 .probe = ptp_qoriq_probe,
637 .remove = ptp_qoriq_remove,
Yangbo Luceefc71d2018-05-25 12:40:34 +0800638};
639
Yangbo Lu1e562c82019-02-12 12:23:56 +0800640module_platform_driver(ptp_qoriq_driver);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800641
642MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
643MODULE_DESCRIPTION("PTP clock for Freescale QorIQ 1588 timer");
644MODULE_LICENSE("GPL");