blob: a577218d1ab716fc9367e5e4a02289268a7f8ec7 [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 Luceefc71d2018-05-25 12:40:34 +080075}
76
Yangbo Lu1e562c82019-02-12 12:23:56 +080077static int extts_clean_up(struct ptp_qoriq *ptp_qoriq, int index,
Yangbo Lu6815d8b2019-01-21 18:41:39 +080078 bool update_event)
79{
Yangbo Lu1e562c82019-02-12 12:23:56 +080080 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Lu6815d8b2019-01-21 18:41:39 +080081 struct ptp_clock_event event;
82 void __iomem *reg_etts_l;
83 void __iomem *reg_etts_h;
84 u32 valid, stat, lo, hi;
85
86 switch (index) {
87 case 0:
88 valid = ETS1_VLD;
89 reg_etts_l = &regs->etts_regs->tmr_etts1_l;
90 reg_etts_h = &regs->etts_regs->tmr_etts1_h;
91 break;
92 case 1:
93 valid = ETS2_VLD;
94 reg_etts_l = &regs->etts_regs->tmr_etts2_l;
95 reg_etts_h = &regs->etts_regs->tmr_etts2_h;
96 break;
97 default:
98 return -EINVAL;
99 }
100
101 event.type = PTP_CLOCK_EXTTS;
102 event.index = index;
103
104 do {
Yangbo Luf038ddf2019-02-12 12:23:59 +0800105 lo = ptp_qoriq->read(reg_etts_l);
106 hi = ptp_qoriq->read(reg_etts_h);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800107
108 if (update_event) {
109 event.timestamp = ((u64) hi) << 32;
110 event.timestamp |= lo;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800111 ptp_clock_event(ptp_qoriq->clock, &event);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800112 }
113
Yangbo Luf038ddf2019-02-12 12:23:59 +0800114 stat = ptp_qoriq->read(&regs->ctrl_regs->tmr_stat);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800115 } while (ptp_qoriq->extts_fifo_support && (stat & valid));
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800116
117 return 0;
118}
119
Yangbo Luceefc71d2018-05-25 12:40:34 +0800120/*
121 * Interrupt service routine
122 */
123
Yangbo Lu73356e42019-02-12 12:23:57 +0800124irqreturn_t ptp_qoriq_isr(int irq, void *priv)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800125{
Yangbo Lu1e562c82019-02-12 12:23:56 +0800126 struct ptp_qoriq *ptp_qoriq = priv;
127 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800128 struct ptp_clock_event event;
129 u64 ns;
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800130 u32 ack = 0, lo, hi, mask, val, irqs;
131
Yangbo Lu1e562c82019-02-12 12:23:56 +0800132 spin_lock(&ptp_qoriq->lock);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800133
Yangbo Luf038ddf2019-02-12 12:23:59 +0800134 val = ptp_qoriq->read(&regs->ctrl_regs->tmr_tevent);
135 mask = ptp_qoriq->read(&regs->ctrl_regs->tmr_temask);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800136
Yangbo Lu1e562c82019-02-12 12:23:56 +0800137 spin_unlock(&ptp_qoriq->lock);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800138
139 irqs = val & mask;
140
141 if (irqs & ETS1) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800142 ack |= ETS1;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800143 extts_clean_up(ptp_qoriq, 0, true);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800144 }
145
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800146 if (irqs & ETS2) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800147 ack |= ETS2;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800148 extts_clean_up(ptp_qoriq, 1, true);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800149 }
150
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800151 if (irqs & ALM2) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800152 ack |= ALM2;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800153 if (ptp_qoriq->alarm_value) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800154 event.type = PTP_CLOCK_ALARM;
155 event.index = 0;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800156 event.timestamp = ptp_qoriq->alarm_value;
157 ptp_clock_event(ptp_qoriq->clock, &event);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800158 }
Yangbo Lu1e562c82019-02-12 12:23:56 +0800159 if (ptp_qoriq->alarm_interval) {
160 ns = ptp_qoriq->alarm_value + ptp_qoriq->alarm_interval;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800161 hi = ns >> 32;
162 lo = ns & 0xffffffff;
Yangbo Luf038ddf2019-02-12 12:23:59 +0800163 ptp_qoriq->write(&regs->alarm_regs->tmr_alarm2_l, lo);
164 ptp_qoriq->write(&regs->alarm_regs->tmr_alarm2_h, hi);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800165 ptp_qoriq->alarm_value = ns;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800166 } else {
Yangbo Lu1e562c82019-02-12 12:23:56 +0800167 spin_lock(&ptp_qoriq->lock);
Yangbo Luf038ddf2019-02-12 12:23:59 +0800168 mask = ptp_qoriq->read(&regs->ctrl_regs->tmr_temask);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800169 mask &= ~ALM2EN;
Yangbo Luf038ddf2019-02-12 12:23:59 +0800170 ptp_qoriq->write(&regs->ctrl_regs->tmr_temask, mask);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800171 spin_unlock(&ptp_qoriq->lock);
172 ptp_qoriq->alarm_value = 0;
173 ptp_qoriq->alarm_interval = 0;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800174 }
175 }
176
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800177 if (irqs & PP1) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800178 ack |= PP1;
179 event.type = PTP_CLOCK_PPS;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800180 ptp_clock_event(ptp_qoriq->clock, &event);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800181 }
182
183 if (ack) {
Yangbo Luf038ddf2019-02-12 12:23:59 +0800184 ptp_qoriq->write(&regs->ctrl_regs->tmr_tevent, ack);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800185 return IRQ_HANDLED;
186 } else
187 return IRQ_NONE;
188}
Yangbo Lu73356e42019-02-12 12:23:57 +0800189EXPORT_SYMBOL_GPL(ptp_qoriq_isr);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800190
191/*
192 * PTP clock operations
193 */
194
Yangbo Lu73356e42019-02-12 12:23:57 +0800195int ptp_qoriq_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800196{
197 u64 adj, diff;
198 u32 tmr_add;
199 int neg_adj = 0;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800200 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
201 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800202
203 if (scaled_ppm < 0) {
204 neg_adj = 1;
205 scaled_ppm = -scaled_ppm;
206 }
Yangbo Lu1e562c82019-02-12 12:23:56 +0800207 tmr_add = ptp_qoriq->tmr_add;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800208 adj = tmr_add;
209
210 /* calculate diff as adj*(scaled_ppm/65536)/1000000
211 * and round() to the nearest integer
212 */
213 adj *= scaled_ppm;
214 diff = div_u64(adj, 8000000);
215 diff = (diff >> 13) + ((diff >> 12) & 1);
216
217 tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
218
Yangbo Luf038ddf2019-02-12 12:23:59 +0800219 ptp_qoriq->write(&regs->ctrl_regs->tmr_add, tmr_add);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800220
221 return 0;
222}
Yangbo Lu73356e42019-02-12 12:23:57 +0800223EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800224
Yangbo Lu73356e42019-02-12 12:23:57 +0800225int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800226{
227 s64 now;
228 unsigned long flags;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800229 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800230
Yangbo Lu1e562c82019-02-12 12:23:56 +0800231 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800232
Yangbo Lu1e562c82019-02-12 12:23:56 +0800233 now = tmr_cnt_read(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800234 now += delta;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800235 tmr_cnt_write(ptp_qoriq, now);
236 set_fipers(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800237
Yangbo Lu1e562c82019-02-12 12:23:56 +0800238 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800239
240 return 0;
241}
Yangbo Lu73356e42019-02-12 12:23:57 +0800242EXPORT_SYMBOL_GPL(ptp_qoriq_adjtime);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800243
Yangbo Lu73356e42019-02-12 12:23:57 +0800244int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800245{
246 u64 ns;
247 unsigned long flags;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800248 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800249
Yangbo Lu1e562c82019-02-12 12:23:56 +0800250 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800251
Yangbo Lu1e562c82019-02-12 12:23:56 +0800252 ns = tmr_cnt_read(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800253
Yangbo Lu1e562c82019-02-12 12:23:56 +0800254 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800255
256 *ts = ns_to_timespec64(ns);
257
258 return 0;
259}
Yangbo Lu73356e42019-02-12 12:23:57 +0800260EXPORT_SYMBOL_GPL(ptp_qoriq_gettime);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800261
Yangbo Lu73356e42019-02-12 12:23:57 +0800262int ptp_qoriq_settime(struct ptp_clock_info *ptp,
263 const struct timespec64 *ts)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800264{
265 u64 ns;
266 unsigned long flags;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800267 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800268
269 ns = timespec64_to_ns(ts);
270
Yangbo Lu1e562c82019-02-12 12:23:56 +0800271 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800272
Yangbo Lu1e562c82019-02-12 12:23:56 +0800273 tmr_cnt_write(ptp_qoriq, ns);
274 set_fipers(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800275
Yangbo Lu1e562c82019-02-12 12:23:56 +0800276 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800277
278 return 0;
279}
Yangbo Lu73356e42019-02-12 12:23:57 +0800280EXPORT_SYMBOL_GPL(ptp_qoriq_settime);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800281
Yangbo Lu73356e42019-02-12 12:23:57 +0800282int ptp_qoriq_enable(struct ptp_clock_info *ptp,
283 struct ptp_clock_request *rq, int on)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800284{
Yangbo Lu1e562c82019-02-12 12:23:56 +0800285 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
286 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800287 unsigned long flags;
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800288 u32 bit, mask = 0;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800289
290 switch (rq->type) {
291 case PTP_CLK_REQ_EXTTS:
292 switch (rq->extts.index) {
293 case 0:
294 bit = ETS1EN;
295 break;
296 case 1:
297 bit = ETS2EN;
298 break;
299 default:
300 return -EINVAL;
301 }
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800302
303 if (on)
Yangbo Lu1e562c82019-02-12 12:23:56 +0800304 extts_clean_up(ptp_qoriq, rq->extts.index, false);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800305
Yangbo Luceefc71d2018-05-25 12:40:34 +0800306 break;
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800307 case PTP_CLK_REQ_PPS:
308 bit = PP1EN;
309 break;
310 default:
311 return -EOPNOTSUPP;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800312 }
313
Yangbo Lu1e562c82019-02-12 12:23:56 +0800314 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800315
Yangbo Luf038ddf2019-02-12 12:23:59 +0800316 mask = ptp_qoriq->read(&regs->ctrl_regs->tmr_temask);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800317 if (on) {
318 mask |= bit;
Yangbo Luf038ddf2019-02-12 12:23:59 +0800319 ptp_qoriq->write(&regs->ctrl_regs->tmr_tevent, bit);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800320 } else {
321 mask &= ~bit;
322 }
323
Yangbo Luf038ddf2019-02-12 12:23:59 +0800324 ptp_qoriq->write(&regs->ctrl_regs->tmr_temask, mask);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800325
Yangbo Lu1e562c82019-02-12 12:23:56 +0800326 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800327 return 0;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800328}
Yangbo Lu73356e42019-02-12 12:23:57 +0800329EXPORT_SYMBOL_GPL(ptp_qoriq_enable);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800330
331static const struct ptp_clock_info ptp_qoriq_caps = {
332 .owner = THIS_MODULE,
333 .name = "qoriq ptp clock",
334 .max_adj = 512000,
335 .n_alarm = 0,
336 .n_ext_ts = N_EXT_TS,
337 .n_per_out = 0,
338 .n_pins = 0,
339 .pps = 1,
340 .adjfine = ptp_qoriq_adjfine,
341 .adjtime = ptp_qoriq_adjtime,
342 .gettime64 = ptp_qoriq_gettime,
343 .settime64 = ptp_qoriq_settime,
344 .enable = ptp_qoriq_enable,
345};
346
Yangbo Lu91305f22018-08-01 18:05:54 +0800347/**
Yangbo Lu1e562c82019-02-12 12:23:56 +0800348 * ptp_qoriq_nominal_freq - calculate nominal frequency according to
Yangbo Lu91305f22018-08-01 18:05:54 +0800349 * reference clock frequency
350 *
351 * @clk_src: reference clock frequency
352 *
353 * The nominal frequency is the desired clock frequency.
354 * It should be less than the reference clock frequency.
355 * It should be a factor of 1000MHz.
356 *
357 * Return the nominal frequency
358 */
Yangbo Lu1e562c82019-02-12 12:23:56 +0800359static u32 ptp_qoriq_nominal_freq(u32 clk_src)
Yangbo Lu91305f22018-08-01 18:05:54 +0800360{
361 u32 remainder = 0;
362
363 clk_src /= 1000000;
364 remainder = clk_src % 100;
365 if (remainder) {
366 clk_src -= remainder;
367 clk_src += 100;
368 }
369
370 do {
371 clk_src -= 100;
372
373 } while (1000 % clk_src);
374
375 return clk_src * 1000000;
376}
377
378/**
Yangbo Lu1e562c82019-02-12 12:23:56 +0800379 * ptp_qoriq_auto_config - calculate a set of default configurations
Yangbo Lu91305f22018-08-01 18:05:54 +0800380 *
Yangbo Lu1e562c82019-02-12 12:23:56 +0800381 * @ptp_qoriq: pointer to ptp_qoriq
Yangbo Lu91305f22018-08-01 18:05:54 +0800382 * @node: pointer to device_node
383 *
384 * If below dts properties are not provided, this function will be
385 * called to calculate a set of default configurations for them.
386 * "fsl,tclk-period"
387 * "fsl,tmr-prsc"
388 * "fsl,tmr-add"
389 * "fsl,tmr-fiper1"
390 * "fsl,tmr-fiper2"
391 * "fsl,max-adj"
392 *
393 * Return 0 if success
394 */
Yangbo Lu1e562c82019-02-12 12:23:56 +0800395static int ptp_qoriq_auto_config(struct ptp_qoriq *ptp_qoriq,
Yangbo Lu91305f22018-08-01 18:05:54 +0800396 struct device_node *node)
397{
398 struct clk *clk;
399 u64 freq_comp;
400 u64 max_adj;
401 u32 nominal_freq;
Yangbo Lu74c05a32018-08-06 12:39:11 +0800402 u32 remainder = 0;
Yangbo Lu91305f22018-08-01 18:05:54 +0800403 u32 clk_src = 0;
404
Yangbo Lu1e562c82019-02-12 12:23:56 +0800405 ptp_qoriq->cksel = DEFAULT_CKSEL;
Yangbo Lu91305f22018-08-01 18:05:54 +0800406
407 clk = of_clk_get(node, 0);
408 if (!IS_ERR(clk)) {
409 clk_src = clk_get_rate(clk);
410 clk_put(clk);
411 }
412
413 if (clk_src <= 100000000UL) {
414 pr_err("error reference clock value, or lower than 100MHz\n");
415 return -EINVAL;
416 }
417
Yangbo Lu1e562c82019-02-12 12:23:56 +0800418 nominal_freq = ptp_qoriq_nominal_freq(clk_src);
Yangbo Lu91305f22018-08-01 18:05:54 +0800419 if (!nominal_freq)
420 return -EINVAL;
421
Yangbo Lu1e562c82019-02-12 12:23:56 +0800422 ptp_qoriq->tclk_period = 1000000000UL / nominal_freq;
423 ptp_qoriq->tmr_prsc = DEFAULT_TMR_PRSC;
Yangbo Lu91305f22018-08-01 18:05:54 +0800424
425 /* Calculate initial frequency compensation value for TMR_ADD register.
426 * freq_comp = ceil(2^32 / freq_ratio)
427 * freq_ratio = reference_clock_freq / nominal_freq
428 */
429 freq_comp = ((u64)1 << 32) * nominal_freq;
Yangbo Lu74c05a32018-08-06 12:39:11 +0800430 freq_comp = div_u64_rem(freq_comp, clk_src, &remainder);
431 if (remainder)
Yangbo Lu91305f22018-08-01 18:05:54 +0800432 freq_comp++;
433
Yangbo Lu1e562c82019-02-12 12:23:56 +0800434 ptp_qoriq->tmr_add = freq_comp;
435 ptp_qoriq->tmr_fiper1 = DEFAULT_FIPER1_PERIOD - ptp_qoriq->tclk_period;
436 ptp_qoriq->tmr_fiper2 = DEFAULT_FIPER2_PERIOD - ptp_qoriq->tclk_period;
Yangbo Lu91305f22018-08-01 18:05:54 +0800437
438 /* max_adj = 1000000000 * (freq_ratio - 1.0) - 1
439 * freq_ratio = reference_clock_freq / nominal_freq
440 */
441 max_adj = 1000000000ULL * (clk_src - nominal_freq);
Yangbo Lu74c05a32018-08-06 12:39:11 +0800442 max_adj = div_u64(max_adj, nominal_freq) - 1;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800443 ptp_qoriq->caps.max_adj = max_adj;
Yangbo Lu91305f22018-08-01 18:05:54 +0800444
445 return 0;
446}
447
Yangbo Luff545712019-02-12 12:23:58 +0800448int ptp_qoriq_init(struct ptp_qoriq *ptp_qoriq, void __iomem *base,
Colin Ian King58066ac2019-02-19 14:21:20 +0000449 const struct ptp_clock_info *caps)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800450{
Yangbo Luff545712019-02-12 12:23:58 +0800451 struct device_node *node = ptp_qoriq->dev->of_node;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800452 struct ptp_qoriq_registers *regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800453 struct timespec64 now;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800454 unsigned long flags;
Yangbo Luff545712019-02-12 12:23:58 +0800455 u32 tmr_ctrl;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800456
Claudiu Manoil7f4399b2019-05-09 03:07:12 +0000457 if (!node)
458 return -ENODEV;
459
Yangbo Luff545712019-02-12 12:23:58 +0800460 ptp_qoriq->base = base;
Colin Ian King58066ac2019-02-19 14:21:20 +0000461 ptp_qoriq->caps = *caps;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800462
Yangbo Lu1e562c82019-02-12 12:23:56 +0800463 if (of_property_read_u32(node, "fsl,cksel", &ptp_qoriq->cksel))
464 ptp_qoriq->cksel = DEFAULT_CKSEL;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800465
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800466 if (of_property_read_bool(node, "fsl,extts-fifo"))
Yangbo Lu1e562c82019-02-12 12:23:56 +0800467 ptp_qoriq->extts_fifo_support = true;
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800468 else
Yangbo Lu1e562c82019-02-12 12:23:56 +0800469 ptp_qoriq->extts_fifo_support = false;
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800470
Yangbo Luceefc71d2018-05-25 12:40:34 +0800471 if (of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800472 "fsl,tclk-period", &ptp_qoriq->tclk_period) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800473 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800474 "fsl,tmr-prsc", &ptp_qoriq->tmr_prsc) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800475 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800476 "fsl,tmr-add", &ptp_qoriq->tmr_add) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800477 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800478 "fsl,tmr-fiper1", &ptp_qoriq->tmr_fiper1) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800479 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800480 "fsl,tmr-fiper2", &ptp_qoriq->tmr_fiper2) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800481 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800482 "fsl,max-adj", &ptp_qoriq->caps.max_adj)) {
Yangbo Lu91305f22018-08-01 18:05:54 +0800483 pr_warn("device tree node missing required elements, try automatic configuration\n");
484
Yangbo Lu1e562c82019-02-12 12:23:56 +0800485 if (ptp_qoriq_auto_config(ptp_qoriq, node))
Yangbo Luff545712019-02-12 12:23:58 +0800486 return -ENODEV;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800487 }
488
Yangbo Luf038ddf2019-02-12 12:23:59 +0800489 if (of_property_read_bool(node, "little-endian")) {
490 ptp_qoriq->read = qoriq_read_le;
491 ptp_qoriq->write = qoriq_write_le;
492 } else {
493 ptp_qoriq->read = qoriq_read_be;
494 ptp_qoriq->write = qoriq_write_be;
495 }
496
Yangbo Lud4e17682019-02-12 12:24:01 +0800497 /* The eTSEC uses differnt memory map with DPAA/ENETC */
498 if (of_device_is_compatible(node, "fsl,etsec-ptp")) {
499 ptp_qoriq->regs.ctrl_regs = base + ETSEC_CTRL_REGS_OFFSET;
500 ptp_qoriq->regs.alarm_regs = base + ETSEC_ALARM_REGS_OFFSET;
501 ptp_qoriq->regs.fiper_regs = base + ETSEC_FIPER_REGS_OFFSET;
502 ptp_qoriq->regs.etts_regs = base + ETSEC_ETTS_REGS_OFFSET;
Yangbo Lua8f62d02018-06-25 20:37:08 +0800503 } else {
Yangbo Lu1e562c82019-02-12 12:23:56 +0800504 ptp_qoriq->regs.ctrl_regs = base + CTRL_REGS_OFFSET;
505 ptp_qoriq->regs.alarm_regs = base + ALARM_REGS_OFFSET;
506 ptp_qoriq->regs.fiper_regs = base + FIPER_REGS_OFFSET;
507 ptp_qoriq->regs.etts_regs = base + ETTS_REGS_OFFSET;
Yangbo Lua8f62d02018-06-25 20:37:08 +0800508 }
509
Vladimir Olteandb34a472019-10-01 22:07:01 +0300510 spin_lock_init(&ptp_qoriq->lock);
511
Arnd Bergmannf696a212018-06-18 16:20:39 +0200512 ktime_get_real_ts64(&now);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800513 ptp_qoriq_settime(&ptp_qoriq->caps, &now);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800514
515 tmr_ctrl =
Yangbo Lu1e562c82019-02-12 12:23:56 +0800516 (ptp_qoriq->tclk_period & TCLK_PERIOD_MASK) << TCLK_PERIOD_SHIFT |
517 (ptp_qoriq->cksel & CKSEL_MASK) << CKSEL_SHIFT;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800518
Yangbo Lu1e562c82019-02-12 12:23:56 +0800519 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800520
Yangbo Lu1e562c82019-02-12 12:23:56 +0800521 regs = &ptp_qoriq->regs;
Yangbo Luf038ddf2019-02-12 12:23:59 +0800522 ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl, tmr_ctrl);
523 ptp_qoriq->write(&regs->ctrl_regs->tmr_add, ptp_qoriq->tmr_add);
524 ptp_qoriq->write(&regs->ctrl_regs->tmr_prsc, ptp_qoriq->tmr_prsc);
525 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper1, ptp_qoriq->tmr_fiper1);
526 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper2, ptp_qoriq->tmr_fiper2);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800527 set_alarm(ptp_qoriq);
Yangbo Luf038ddf2019-02-12 12:23:59 +0800528 ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl,
529 tmr_ctrl|FIPERST|RTPE|TE|FRD);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800530
Yangbo Lu1e562c82019-02-12 12:23:56 +0800531 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800532
Yangbo Luff545712019-02-12 12:23:58 +0800533 ptp_qoriq->clock = ptp_clock_register(&ptp_qoriq->caps, ptp_qoriq->dev);
534 if (IS_ERR(ptp_qoriq->clock))
535 return PTR_ERR(ptp_qoriq->clock);
536
Yangbo Lu1e562c82019-02-12 12:23:56 +0800537 ptp_qoriq->phc_index = ptp_clock_index(ptp_qoriq->clock);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800538 ptp_qoriq_create_debugfs(ptp_qoriq);
Yangbo Luff545712019-02-12 12:23:58 +0800539 return 0;
540}
541EXPORT_SYMBOL_GPL(ptp_qoriq_init);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800542
Yangbo Luff545712019-02-12 12:23:58 +0800543void ptp_qoriq_free(struct ptp_qoriq *ptp_qoriq)
544{
545 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
546
Yangbo Luf038ddf2019-02-12 12:23:59 +0800547 ptp_qoriq->write(&regs->ctrl_regs->tmr_temask, 0);
548 ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl, 0);
Yangbo Luff545712019-02-12 12:23:58 +0800549
550 ptp_qoriq_remove_debugfs(ptp_qoriq);
551 ptp_clock_unregister(ptp_qoriq->clock);
552 iounmap(ptp_qoriq->base);
553 free_irq(ptp_qoriq->irq, ptp_qoriq);
554}
555EXPORT_SYMBOL_GPL(ptp_qoriq_free);
556
557static int ptp_qoriq_probe(struct platform_device *dev)
558{
559 struct ptp_qoriq *ptp_qoriq;
560 int err = -ENOMEM;
561 void __iomem *base;
562
563 ptp_qoriq = kzalloc(sizeof(*ptp_qoriq), GFP_KERNEL);
564 if (!ptp_qoriq)
565 goto no_memory;
566
567 ptp_qoriq->dev = &dev->dev;
568
569 err = -ENODEV;
570
571 ptp_qoriq->irq = platform_get_irq(dev, 0);
572 if (ptp_qoriq->irq < 0) {
573 pr_err("irq not in device tree\n");
574 goto no_node;
575 }
576 if (request_irq(ptp_qoriq->irq, ptp_qoriq_isr, IRQF_SHARED,
577 DRIVER, ptp_qoriq)) {
578 pr_err("request_irq failed\n");
579 goto no_node;
580 }
581
582 ptp_qoriq->rsrc = platform_get_resource(dev, IORESOURCE_MEM, 0);
583 if (!ptp_qoriq->rsrc) {
584 pr_err("no resource\n");
585 goto no_resource;
586 }
587 if (request_resource(&iomem_resource, ptp_qoriq->rsrc)) {
588 pr_err("resource busy\n");
589 goto no_resource;
590 }
591
592 base = ioremap(ptp_qoriq->rsrc->start,
593 resource_size(ptp_qoriq->rsrc));
594 if (!base) {
595 pr_err("ioremap ptp registers failed\n");
596 goto no_ioremap;
597 }
598
Colin Ian King58066ac2019-02-19 14:21:20 +0000599 err = ptp_qoriq_init(ptp_qoriq, base, &ptp_qoriq_caps);
Yangbo Luff545712019-02-12 12:23:58 +0800600 if (err)
601 goto no_clock;
602
603 platform_set_drvdata(dev, ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800604 return 0;
605
606no_clock:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800607 iounmap(ptp_qoriq->base);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800608no_ioremap:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800609 release_resource(ptp_qoriq->rsrc);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800610no_resource:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800611 free_irq(ptp_qoriq->irq, ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800612no_node:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800613 kfree(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800614no_memory:
615 return err;
616}
617
Yangbo Lu1e562c82019-02-12 12:23:56 +0800618static int ptp_qoriq_remove(struct platform_device *dev)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800619{
Yangbo Lu1e562c82019-02-12 12:23:56 +0800620 struct ptp_qoriq *ptp_qoriq = platform_get_drvdata(dev);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800621
Yangbo Luff545712019-02-12 12:23:58 +0800622 ptp_qoriq_free(ptp_qoriq);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800623 release_resource(ptp_qoriq->rsrc);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800624 kfree(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800625 return 0;
626}
627
628static const struct of_device_id match_table[] = {
629 { .compatible = "fsl,etsec-ptp" },
Yangbo Lua8f62d02018-06-25 20:37:08 +0800630 { .compatible = "fsl,fman-ptp-timer" },
Yangbo Luceefc71d2018-05-25 12:40:34 +0800631 {},
632};
633MODULE_DEVICE_TABLE(of, match_table);
634
Yangbo Lu1e562c82019-02-12 12:23:56 +0800635static struct platform_driver ptp_qoriq_driver = {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800636 .driver = {
637 .name = "ptp_qoriq",
638 .of_match_table = match_table,
639 },
Yangbo Lu1e562c82019-02-12 12:23:56 +0800640 .probe = ptp_qoriq_probe,
641 .remove = ptp_qoriq_remove,
Yangbo Luceefc71d2018-05-25 12:40:34 +0800642};
643
Yangbo Lu1e562c82019-02-12 12:23:56 +0800644module_platform_driver(ptp_qoriq_driver);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800645
646MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
647MODULE_DESCRIPTION("PTP clock for Freescale QorIQ 1588 timer");
648MODULE_LICENSE("GPL");