blob: e10642403b25890e3402d07b8bb49b06a9e79c7a [file] [log] [blame]
Yangbo Luceefc71d2018-05-25 12:40:34 +08001/*
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>
Yangbo Luceefc71d2018-05-25 12:40:34 +080025#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/of.h>
28#include <linux/of_platform.h>
29#include <linux/timex.h>
Yangbo Luceefc71d2018-05-25 12:40:34 +080030#include <linux/slab.h>
Yangbo Lu91305f22018-08-01 18:05:54 +080031#include <linux/clk.h>
Yangbo Luceefc71d2018-05-25 12:40:34 +080032
Yangbo Lu6c50c1e2018-05-25 12:40:35 +080033#include <linux/fsl/ptp_qoriq.h>
Yangbo Luceefc71d2018-05-25 12:40:34 +080034
35/*
36 * Register access functions
37 */
38
Yangbo Lu1e562c82019-02-12 12:23:56 +080039/* Caller must hold ptp_qoriq->lock. */
40static u64 tmr_cnt_read(struct ptp_qoriq *ptp_qoriq)
Yangbo Luceefc71d2018-05-25 12:40:34 +080041{
Yangbo Lu1e562c82019-02-12 12:23:56 +080042 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +080043 u64 ns;
44 u32 lo, hi;
45
Yangbo Luf038ddf2019-02-12 12:23:59 +080046 lo = ptp_qoriq->read(&regs->ctrl_regs->tmr_cnt_l);
47 hi = ptp_qoriq->read(&regs->ctrl_regs->tmr_cnt_h);
Yangbo Luceefc71d2018-05-25 12:40:34 +080048 ns = ((u64) hi) << 32;
49 ns |= lo;
50 return ns;
51}
52
Yangbo Lu1e562c82019-02-12 12:23:56 +080053/* Caller must hold ptp_qoriq->lock. */
54static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns)
Yangbo Luceefc71d2018-05-25 12:40:34 +080055{
Yangbo Lu1e562c82019-02-12 12:23:56 +080056 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +080057 u32 hi = ns >> 32;
58 u32 lo = ns & 0xffffffff;
59
Yangbo Luf038ddf2019-02-12 12:23:59 +080060 ptp_qoriq->write(&regs->ctrl_regs->tmr_cnt_l, lo);
61 ptp_qoriq->write(&regs->ctrl_regs->tmr_cnt_h, hi);
Yangbo Luceefc71d2018-05-25 12:40:34 +080062}
63
Yangbo Lu1e562c82019-02-12 12:23:56 +080064/* Caller must hold ptp_qoriq->lock. */
65static void set_alarm(struct ptp_qoriq *ptp_qoriq)
Yangbo Luceefc71d2018-05-25 12:40:34 +080066{
Yangbo Lu1e562c82019-02-12 12:23:56 +080067 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +080068 u64 ns;
69 u32 lo, hi;
70
Yangbo Lu1e562c82019-02-12 12:23:56 +080071 ns = tmr_cnt_read(ptp_qoriq) + 1500000000ULL;
Yangbo Luceefc71d2018-05-25 12:40:34 +080072 ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
Yangbo Lu1e562c82019-02-12 12:23:56 +080073 ns -= ptp_qoriq->tclk_period;
Yangbo Luceefc71d2018-05-25 12:40:34 +080074 hi = ns >> 32;
75 lo = ns & 0xffffffff;
Yangbo Luf038ddf2019-02-12 12:23:59 +080076 ptp_qoriq->write(&regs->alarm_regs->tmr_alarm1_l, lo);
77 ptp_qoriq->write(&regs->alarm_regs->tmr_alarm1_h, hi);
Yangbo Luceefc71d2018-05-25 12:40:34 +080078}
79
Yangbo Lu1e562c82019-02-12 12:23:56 +080080/* Caller must hold ptp_qoriq->lock. */
81static void set_fipers(struct ptp_qoriq *ptp_qoriq)
Yangbo Luceefc71d2018-05-25 12:40:34 +080082{
Yangbo Lu1e562c82019-02-12 12:23:56 +080083 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Lua8f62d02018-06-25 20:37:08 +080084
Yangbo Lu1e562c82019-02-12 12:23:56 +080085 set_alarm(ptp_qoriq);
Yangbo Luf038ddf2019-02-12 12:23:59 +080086 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper1, ptp_qoriq->tmr_fiper1);
87 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper2, ptp_qoriq->tmr_fiper2);
Yangbo Luceefc71d2018-05-25 12:40:34 +080088}
89
Yangbo Lu1e562c82019-02-12 12:23:56 +080090static int extts_clean_up(struct ptp_qoriq *ptp_qoriq, int index,
Yangbo Lu6815d8b2019-01-21 18:41:39 +080091 bool update_event)
92{
Yangbo Lu1e562c82019-02-12 12:23:56 +080093 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Lu6815d8b2019-01-21 18:41:39 +080094 struct ptp_clock_event event;
95 void __iomem *reg_etts_l;
96 void __iomem *reg_etts_h;
97 u32 valid, stat, lo, hi;
98
99 switch (index) {
100 case 0:
101 valid = ETS1_VLD;
102 reg_etts_l = &regs->etts_regs->tmr_etts1_l;
103 reg_etts_h = &regs->etts_regs->tmr_etts1_h;
104 break;
105 case 1:
106 valid = ETS2_VLD;
107 reg_etts_l = &regs->etts_regs->tmr_etts2_l;
108 reg_etts_h = &regs->etts_regs->tmr_etts2_h;
109 break;
110 default:
111 return -EINVAL;
112 }
113
114 event.type = PTP_CLOCK_EXTTS;
115 event.index = index;
116
117 do {
Yangbo Luf038ddf2019-02-12 12:23:59 +0800118 lo = ptp_qoriq->read(reg_etts_l);
119 hi = ptp_qoriq->read(reg_etts_h);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800120
121 if (update_event) {
122 event.timestamp = ((u64) hi) << 32;
123 event.timestamp |= lo;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800124 ptp_clock_event(ptp_qoriq->clock, &event);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800125 }
126
Yangbo Luf038ddf2019-02-12 12:23:59 +0800127 stat = ptp_qoriq->read(&regs->ctrl_regs->tmr_stat);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800128 } while (ptp_qoriq->extts_fifo_support && (stat & valid));
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800129
130 return 0;
131}
132
Yangbo Luceefc71d2018-05-25 12:40:34 +0800133/*
134 * Interrupt service routine
135 */
136
Yangbo Lu73356e42019-02-12 12:23:57 +0800137irqreturn_t ptp_qoriq_isr(int irq, void *priv)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800138{
Yangbo Lu1e562c82019-02-12 12:23:56 +0800139 struct ptp_qoriq *ptp_qoriq = priv;
140 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800141 struct ptp_clock_event event;
142 u64 ns;
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800143 u32 ack = 0, lo, hi, mask, val, irqs;
144
Yangbo Lu1e562c82019-02-12 12:23:56 +0800145 spin_lock(&ptp_qoriq->lock);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800146
Yangbo Luf038ddf2019-02-12 12:23:59 +0800147 val = ptp_qoriq->read(&regs->ctrl_regs->tmr_tevent);
148 mask = ptp_qoriq->read(&regs->ctrl_regs->tmr_temask);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800149
Yangbo Lu1e562c82019-02-12 12:23:56 +0800150 spin_unlock(&ptp_qoriq->lock);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800151
152 irqs = val & mask;
153
154 if (irqs & ETS1) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800155 ack |= ETS1;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800156 extts_clean_up(ptp_qoriq, 0, true);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800157 }
158
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800159 if (irqs & ETS2) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800160 ack |= ETS2;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800161 extts_clean_up(ptp_qoriq, 1, true);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800162 }
163
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800164 if (irqs & ALM2) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800165 ack |= ALM2;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800166 if (ptp_qoriq->alarm_value) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800167 event.type = PTP_CLOCK_ALARM;
168 event.index = 0;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800169 event.timestamp = ptp_qoriq->alarm_value;
170 ptp_clock_event(ptp_qoriq->clock, &event);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800171 }
Yangbo Lu1e562c82019-02-12 12:23:56 +0800172 if (ptp_qoriq->alarm_interval) {
173 ns = ptp_qoriq->alarm_value + ptp_qoriq->alarm_interval;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800174 hi = ns >> 32;
175 lo = ns & 0xffffffff;
Yangbo Luf038ddf2019-02-12 12:23:59 +0800176 ptp_qoriq->write(&regs->alarm_regs->tmr_alarm2_l, lo);
177 ptp_qoriq->write(&regs->alarm_regs->tmr_alarm2_h, hi);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800178 ptp_qoriq->alarm_value = ns;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800179 } else {
Yangbo Lu1e562c82019-02-12 12:23:56 +0800180 spin_lock(&ptp_qoriq->lock);
Yangbo Luf038ddf2019-02-12 12:23:59 +0800181 mask = ptp_qoriq->read(&regs->ctrl_regs->tmr_temask);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800182 mask &= ~ALM2EN;
Yangbo Luf038ddf2019-02-12 12:23:59 +0800183 ptp_qoriq->write(&regs->ctrl_regs->tmr_temask, mask);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800184 spin_unlock(&ptp_qoriq->lock);
185 ptp_qoriq->alarm_value = 0;
186 ptp_qoriq->alarm_interval = 0;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800187 }
188 }
189
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800190 if (irqs & PP1) {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800191 ack |= PP1;
192 event.type = PTP_CLOCK_PPS;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800193 ptp_clock_event(ptp_qoriq->clock, &event);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800194 }
195
196 if (ack) {
Yangbo Luf038ddf2019-02-12 12:23:59 +0800197 ptp_qoriq->write(&regs->ctrl_regs->tmr_tevent, ack);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800198 return IRQ_HANDLED;
199 } else
200 return IRQ_NONE;
201}
Yangbo Lu73356e42019-02-12 12:23:57 +0800202EXPORT_SYMBOL_GPL(ptp_qoriq_isr);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800203
204/*
205 * PTP clock operations
206 */
207
Yangbo Lu73356e42019-02-12 12:23:57 +0800208int ptp_qoriq_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800209{
210 u64 adj, diff;
211 u32 tmr_add;
212 int neg_adj = 0;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800213 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
214 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800215
216 if (scaled_ppm < 0) {
217 neg_adj = 1;
218 scaled_ppm = -scaled_ppm;
219 }
Yangbo Lu1e562c82019-02-12 12:23:56 +0800220 tmr_add = ptp_qoriq->tmr_add;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800221 adj = tmr_add;
222
223 /* calculate diff as adj*(scaled_ppm/65536)/1000000
224 * and round() to the nearest integer
225 */
226 adj *= scaled_ppm;
227 diff = div_u64(adj, 8000000);
228 diff = (diff >> 13) + ((diff >> 12) & 1);
229
230 tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
231
Yangbo Luf038ddf2019-02-12 12:23:59 +0800232 ptp_qoriq->write(&regs->ctrl_regs->tmr_add, tmr_add);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800233
234 return 0;
235}
Yangbo Lu73356e42019-02-12 12:23:57 +0800236EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800237
Yangbo Lu73356e42019-02-12 12:23:57 +0800238int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800239{
240 s64 now;
241 unsigned long flags;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800242 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800243
Yangbo Lu1e562c82019-02-12 12:23:56 +0800244 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800245
Yangbo Lu1e562c82019-02-12 12:23:56 +0800246 now = tmr_cnt_read(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800247 now += delta;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800248 tmr_cnt_write(ptp_qoriq, now);
249 set_fipers(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800250
Yangbo Lu1e562c82019-02-12 12:23:56 +0800251 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800252
253 return 0;
254}
Yangbo Lu73356e42019-02-12 12:23:57 +0800255EXPORT_SYMBOL_GPL(ptp_qoriq_adjtime);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800256
Yangbo Lu73356e42019-02-12 12:23:57 +0800257int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800258{
259 u64 ns;
260 unsigned long flags;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800261 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800262
Yangbo Lu1e562c82019-02-12 12:23:56 +0800263 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800264
Yangbo Lu1e562c82019-02-12 12:23:56 +0800265 ns = tmr_cnt_read(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800266
Yangbo Lu1e562c82019-02-12 12:23:56 +0800267 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800268
269 *ts = ns_to_timespec64(ns);
270
271 return 0;
272}
Yangbo Lu73356e42019-02-12 12:23:57 +0800273EXPORT_SYMBOL_GPL(ptp_qoriq_gettime);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800274
Yangbo Lu73356e42019-02-12 12:23:57 +0800275int ptp_qoriq_settime(struct ptp_clock_info *ptp,
276 const struct timespec64 *ts)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800277{
278 u64 ns;
279 unsigned long flags;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800280 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800281
282 ns = timespec64_to_ns(ts);
283
Yangbo Lu1e562c82019-02-12 12:23:56 +0800284 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800285
Yangbo Lu1e562c82019-02-12 12:23:56 +0800286 tmr_cnt_write(ptp_qoriq, ns);
287 set_fipers(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800288
Yangbo Lu1e562c82019-02-12 12:23:56 +0800289 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800290
291 return 0;
292}
Yangbo Lu73356e42019-02-12 12:23:57 +0800293EXPORT_SYMBOL_GPL(ptp_qoriq_settime);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800294
Yangbo Lu73356e42019-02-12 12:23:57 +0800295int ptp_qoriq_enable(struct ptp_clock_info *ptp,
296 struct ptp_clock_request *rq, int on)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800297{
Yangbo Lu1e562c82019-02-12 12:23:56 +0800298 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
299 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800300 unsigned long flags;
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800301 u32 bit, mask = 0;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800302
303 switch (rq->type) {
304 case PTP_CLK_REQ_EXTTS:
305 switch (rq->extts.index) {
306 case 0:
307 bit = ETS1EN;
308 break;
309 case 1:
310 bit = ETS2EN;
311 break;
312 default:
313 return -EINVAL;
314 }
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800315
316 if (on)
Yangbo Lu1e562c82019-02-12 12:23:56 +0800317 extts_clean_up(ptp_qoriq, rq->extts.index, false);
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800318
Yangbo Luceefc71d2018-05-25 12:40:34 +0800319 break;
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800320 case PTP_CLK_REQ_PPS:
321 bit = PP1EN;
322 break;
323 default:
324 return -EOPNOTSUPP;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800325 }
326
Yangbo Lu1e562c82019-02-12 12:23:56 +0800327 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800328
Yangbo Luf038ddf2019-02-12 12:23:59 +0800329 mask = ptp_qoriq->read(&regs->ctrl_regs->tmr_temask);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800330 if (on) {
331 mask |= bit;
Yangbo Luf038ddf2019-02-12 12:23:59 +0800332 ptp_qoriq->write(&regs->ctrl_regs->tmr_tevent, bit);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800333 } else {
334 mask &= ~bit;
335 }
336
Yangbo Luf038ddf2019-02-12 12:23:59 +0800337 ptp_qoriq->write(&regs->ctrl_regs->tmr_temask, mask);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800338
Yangbo Lu1e562c82019-02-12 12:23:56 +0800339 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Lub0bc10c2019-01-21 18:41:38 +0800340 return 0;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800341}
Yangbo Lu73356e42019-02-12 12:23:57 +0800342EXPORT_SYMBOL_GPL(ptp_qoriq_enable);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800343
344static const struct ptp_clock_info ptp_qoriq_caps = {
345 .owner = THIS_MODULE,
346 .name = "qoriq ptp clock",
347 .max_adj = 512000,
348 .n_alarm = 0,
349 .n_ext_ts = N_EXT_TS,
350 .n_per_out = 0,
351 .n_pins = 0,
352 .pps = 1,
353 .adjfine = ptp_qoriq_adjfine,
354 .adjtime = ptp_qoriq_adjtime,
355 .gettime64 = ptp_qoriq_gettime,
356 .settime64 = ptp_qoriq_settime,
357 .enable = ptp_qoriq_enable,
358};
359
Yangbo Lu91305f22018-08-01 18:05:54 +0800360/**
Yangbo Lu1e562c82019-02-12 12:23:56 +0800361 * ptp_qoriq_nominal_freq - calculate nominal frequency according to
Yangbo Lu91305f22018-08-01 18:05:54 +0800362 * reference clock frequency
363 *
364 * @clk_src: reference clock frequency
365 *
366 * The nominal frequency is the desired clock frequency.
367 * It should be less than the reference clock frequency.
368 * It should be a factor of 1000MHz.
369 *
370 * Return the nominal frequency
371 */
Yangbo Lu1e562c82019-02-12 12:23:56 +0800372static u32 ptp_qoriq_nominal_freq(u32 clk_src)
Yangbo Lu91305f22018-08-01 18:05:54 +0800373{
374 u32 remainder = 0;
375
376 clk_src /= 1000000;
377 remainder = clk_src % 100;
378 if (remainder) {
379 clk_src -= remainder;
380 clk_src += 100;
381 }
382
383 do {
384 clk_src -= 100;
385
386 } while (1000 % clk_src);
387
388 return clk_src * 1000000;
389}
390
391/**
Yangbo Lu1e562c82019-02-12 12:23:56 +0800392 * ptp_qoriq_auto_config - calculate a set of default configurations
Yangbo Lu91305f22018-08-01 18:05:54 +0800393 *
Yangbo Lu1e562c82019-02-12 12:23:56 +0800394 * @ptp_qoriq: pointer to ptp_qoriq
Yangbo Lu91305f22018-08-01 18:05:54 +0800395 * @node: pointer to device_node
396 *
397 * If below dts properties are not provided, this function will be
398 * called to calculate a set of default configurations for them.
399 * "fsl,tclk-period"
400 * "fsl,tmr-prsc"
401 * "fsl,tmr-add"
402 * "fsl,tmr-fiper1"
403 * "fsl,tmr-fiper2"
404 * "fsl,max-adj"
405 *
406 * Return 0 if success
407 */
Yangbo Lu1e562c82019-02-12 12:23:56 +0800408static int ptp_qoriq_auto_config(struct ptp_qoriq *ptp_qoriq,
Yangbo Lu91305f22018-08-01 18:05:54 +0800409 struct device_node *node)
410{
411 struct clk *clk;
412 u64 freq_comp;
413 u64 max_adj;
414 u32 nominal_freq;
Yangbo Lu74c05a32018-08-06 12:39:11 +0800415 u32 remainder = 0;
Yangbo Lu91305f22018-08-01 18:05:54 +0800416 u32 clk_src = 0;
417
Yangbo Lu1e562c82019-02-12 12:23:56 +0800418 ptp_qoriq->cksel = DEFAULT_CKSEL;
Yangbo Lu91305f22018-08-01 18:05:54 +0800419
420 clk = of_clk_get(node, 0);
421 if (!IS_ERR(clk)) {
422 clk_src = clk_get_rate(clk);
423 clk_put(clk);
424 }
425
426 if (clk_src <= 100000000UL) {
427 pr_err("error reference clock value, or lower than 100MHz\n");
428 return -EINVAL;
429 }
430
Yangbo Lu1e562c82019-02-12 12:23:56 +0800431 nominal_freq = ptp_qoriq_nominal_freq(clk_src);
Yangbo Lu91305f22018-08-01 18:05:54 +0800432 if (!nominal_freq)
433 return -EINVAL;
434
Yangbo Lu1e562c82019-02-12 12:23:56 +0800435 ptp_qoriq->tclk_period = 1000000000UL / nominal_freq;
436 ptp_qoriq->tmr_prsc = DEFAULT_TMR_PRSC;
Yangbo Lu91305f22018-08-01 18:05:54 +0800437
438 /* Calculate initial frequency compensation value for TMR_ADD register.
439 * freq_comp = ceil(2^32 / freq_ratio)
440 * freq_ratio = reference_clock_freq / nominal_freq
441 */
442 freq_comp = ((u64)1 << 32) * nominal_freq;
Yangbo Lu74c05a32018-08-06 12:39:11 +0800443 freq_comp = div_u64_rem(freq_comp, clk_src, &remainder);
444 if (remainder)
Yangbo Lu91305f22018-08-01 18:05:54 +0800445 freq_comp++;
446
Yangbo Lu1e562c82019-02-12 12:23:56 +0800447 ptp_qoriq->tmr_add = freq_comp;
448 ptp_qoriq->tmr_fiper1 = DEFAULT_FIPER1_PERIOD - ptp_qoriq->tclk_period;
449 ptp_qoriq->tmr_fiper2 = DEFAULT_FIPER2_PERIOD - ptp_qoriq->tclk_period;
Yangbo Lu91305f22018-08-01 18:05:54 +0800450
451 /* max_adj = 1000000000 * (freq_ratio - 1.0) - 1
452 * freq_ratio = reference_clock_freq / nominal_freq
453 */
454 max_adj = 1000000000ULL * (clk_src - nominal_freq);
Yangbo Lu74c05a32018-08-06 12:39:11 +0800455 max_adj = div_u64(max_adj, nominal_freq) - 1;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800456 ptp_qoriq->caps.max_adj = max_adj;
Yangbo Lu91305f22018-08-01 18:05:54 +0800457
458 return 0;
459}
460
Yangbo Luff545712019-02-12 12:23:58 +0800461int ptp_qoriq_init(struct ptp_qoriq *ptp_qoriq, void __iomem *base,
Colin Ian King58066ac2019-02-19 14:21:20 +0000462 const struct ptp_clock_info *caps)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800463{
Yangbo Luff545712019-02-12 12:23:58 +0800464 struct device_node *node = ptp_qoriq->dev->of_node;
Yangbo Lu1e562c82019-02-12 12:23:56 +0800465 struct ptp_qoriq_registers *regs;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800466 struct timespec64 now;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800467 unsigned long flags;
Yangbo Luff545712019-02-12 12:23:58 +0800468 u32 tmr_ctrl;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800469
Claudiu Manoil7f4399b2019-05-09 03:07:12 +0000470 if (!node)
471 return -ENODEV;
472
Yangbo Luff545712019-02-12 12:23:58 +0800473 ptp_qoriq->base = base;
Colin Ian King58066ac2019-02-19 14:21:20 +0000474 ptp_qoriq->caps = *caps;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800475
Yangbo Lu1e562c82019-02-12 12:23:56 +0800476 if (of_property_read_u32(node, "fsl,cksel", &ptp_qoriq->cksel))
477 ptp_qoriq->cksel = DEFAULT_CKSEL;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800478
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800479 if (of_property_read_bool(node, "fsl,extts-fifo"))
Yangbo Lu1e562c82019-02-12 12:23:56 +0800480 ptp_qoriq->extts_fifo_support = true;
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800481 else
Yangbo Lu1e562c82019-02-12 12:23:56 +0800482 ptp_qoriq->extts_fifo_support = false;
Yangbo Lu6815d8b2019-01-21 18:41:39 +0800483
Yangbo Luceefc71d2018-05-25 12:40:34 +0800484 if (of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800485 "fsl,tclk-period", &ptp_qoriq->tclk_period) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800486 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800487 "fsl,tmr-prsc", &ptp_qoriq->tmr_prsc) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800488 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800489 "fsl,tmr-add", &ptp_qoriq->tmr_add) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800490 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800491 "fsl,tmr-fiper1", &ptp_qoriq->tmr_fiper1) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800492 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800493 "fsl,tmr-fiper2", &ptp_qoriq->tmr_fiper2) ||
Yangbo Luceefc71d2018-05-25 12:40:34 +0800494 of_property_read_u32(node,
Yangbo Lu1e562c82019-02-12 12:23:56 +0800495 "fsl,max-adj", &ptp_qoriq->caps.max_adj)) {
Yangbo Lu91305f22018-08-01 18:05:54 +0800496 pr_warn("device tree node missing required elements, try automatic configuration\n");
497
Yangbo Lu1e562c82019-02-12 12:23:56 +0800498 if (ptp_qoriq_auto_config(ptp_qoriq, node))
Yangbo Luff545712019-02-12 12:23:58 +0800499 return -ENODEV;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800500 }
501
Yangbo Luf038ddf2019-02-12 12:23:59 +0800502 if (of_property_read_bool(node, "little-endian")) {
503 ptp_qoriq->read = qoriq_read_le;
504 ptp_qoriq->write = qoriq_write_le;
505 } else {
506 ptp_qoriq->read = qoriq_read_be;
507 ptp_qoriq->write = qoriq_write_be;
508 }
509
Yangbo Lud4e17682019-02-12 12:24:01 +0800510 /* The eTSEC uses differnt memory map with DPAA/ENETC */
511 if (of_device_is_compatible(node, "fsl,etsec-ptp")) {
512 ptp_qoriq->regs.ctrl_regs = base + ETSEC_CTRL_REGS_OFFSET;
513 ptp_qoriq->regs.alarm_regs = base + ETSEC_ALARM_REGS_OFFSET;
514 ptp_qoriq->regs.fiper_regs = base + ETSEC_FIPER_REGS_OFFSET;
515 ptp_qoriq->regs.etts_regs = base + ETSEC_ETTS_REGS_OFFSET;
Yangbo Lua8f62d02018-06-25 20:37:08 +0800516 } else {
Yangbo Lu1e562c82019-02-12 12:23:56 +0800517 ptp_qoriq->regs.ctrl_regs = base + CTRL_REGS_OFFSET;
518 ptp_qoriq->regs.alarm_regs = base + ALARM_REGS_OFFSET;
519 ptp_qoriq->regs.fiper_regs = base + FIPER_REGS_OFFSET;
520 ptp_qoriq->regs.etts_regs = base + ETTS_REGS_OFFSET;
Yangbo Lua8f62d02018-06-25 20:37:08 +0800521 }
522
Arnd Bergmannf696a212018-06-18 16:20:39 +0200523 ktime_get_real_ts64(&now);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800524 ptp_qoriq_settime(&ptp_qoriq->caps, &now);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800525
526 tmr_ctrl =
Yangbo Lu1e562c82019-02-12 12:23:56 +0800527 (ptp_qoriq->tclk_period & TCLK_PERIOD_MASK) << TCLK_PERIOD_SHIFT |
528 (ptp_qoriq->cksel & CKSEL_MASK) << CKSEL_SHIFT;
Yangbo Luceefc71d2018-05-25 12:40:34 +0800529
Yangbo Luff545712019-02-12 12:23:58 +0800530 spin_lock_init(&ptp_qoriq->lock);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800531 spin_lock_irqsave(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800532
Yangbo Lu1e562c82019-02-12 12:23:56 +0800533 regs = &ptp_qoriq->regs;
Yangbo Luf038ddf2019-02-12 12:23:59 +0800534 ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl, tmr_ctrl);
535 ptp_qoriq->write(&regs->ctrl_regs->tmr_add, ptp_qoriq->tmr_add);
536 ptp_qoriq->write(&regs->ctrl_regs->tmr_prsc, ptp_qoriq->tmr_prsc);
537 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper1, ptp_qoriq->tmr_fiper1);
538 ptp_qoriq->write(&regs->fiper_regs->tmr_fiper2, ptp_qoriq->tmr_fiper2);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800539 set_alarm(ptp_qoriq);
Yangbo Luf038ddf2019-02-12 12:23:59 +0800540 ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl,
541 tmr_ctrl|FIPERST|RTPE|TE|FRD);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800542
Yangbo Lu1e562c82019-02-12 12:23:56 +0800543 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800544
Yangbo Luff545712019-02-12 12:23:58 +0800545 ptp_qoriq->clock = ptp_clock_register(&ptp_qoriq->caps, ptp_qoriq->dev);
546 if (IS_ERR(ptp_qoriq->clock))
547 return PTR_ERR(ptp_qoriq->clock);
548
Yangbo Lu1e562c82019-02-12 12:23:56 +0800549 ptp_qoriq->phc_index = ptp_clock_index(ptp_qoriq->clock);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800550 ptp_qoriq_create_debugfs(ptp_qoriq);
Yangbo Luff545712019-02-12 12:23:58 +0800551 return 0;
552}
553EXPORT_SYMBOL_GPL(ptp_qoriq_init);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800554
Yangbo Luff545712019-02-12 12:23:58 +0800555void ptp_qoriq_free(struct ptp_qoriq *ptp_qoriq)
556{
557 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
558
Yangbo Luf038ddf2019-02-12 12:23:59 +0800559 ptp_qoriq->write(&regs->ctrl_regs->tmr_temask, 0);
560 ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl, 0);
Yangbo Luff545712019-02-12 12:23:58 +0800561
562 ptp_qoriq_remove_debugfs(ptp_qoriq);
563 ptp_clock_unregister(ptp_qoriq->clock);
564 iounmap(ptp_qoriq->base);
565 free_irq(ptp_qoriq->irq, ptp_qoriq);
566}
567EXPORT_SYMBOL_GPL(ptp_qoriq_free);
568
569static int ptp_qoriq_probe(struct platform_device *dev)
570{
571 struct ptp_qoriq *ptp_qoriq;
572 int err = -ENOMEM;
573 void __iomem *base;
574
575 ptp_qoriq = kzalloc(sizeof(*ptp_qoriq), GFP_KERNEL);
576 if (!ptp_qoriq)
577 goto no_memory;
578
579 ptp_qoriq->dev = &dev->dev;
580
581 err = -ENODEV;
582
583 ptp_qoriq->irq = platform_get_irq(dev, 0);
584 if (ptp_qoriq->irq < 0) {
585 pr_err("irq not in device tree\n");
586 goto no_node;
587 }
588 if (request_irq(ptp_qoriq->irq, ptp_qoriq_isr, IRQF_SHARED,
589 DRIVER, ptp_qoriq)) {
590 pr_err("request_irq failed\n");
591 goto no_node;
592 }
593
594 ptp_qoriq->rsrc = platform_get_resource(dev, IORESOURCE_MEM, 0);
595 if (!ptp_qoriq->rsrc) {
596 pr_err("no resource\n");
597 goto no_resource;
598 }
599 if (request_resource(&iomem_resource, ptp_qoriq->rsrc)) {
600 pr_err("resource busy\n");
601 goto no_resource;
602 }
603
604 base = ioremap(ptp_qoriq->rsrc->start,
605 resource_size(ptp_qoriq->rsrc));
606 if (!base) {
607 pr_err("ioremap ptp registers failed\n");
608 goto no_ioremap;
609 }
610
Colin Ian King58066ac2019-02-19 14:21:20 +0000611 err = ptp_qoriq_init(ptp_qoriq, base, &ptp_qoriq_caps);
Yangbo Luff545712019-02-12 12:23:58 +0800612 if (err)
613 goto no_clock;
614
615 platform_set_drvdata(dev, ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800616 return 0;
617
618no_clock:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800619 iounmap(ptp_qoriq->base);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800620no_ioremap:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800621 release_resource(ptp_qoriq->rsrc);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800622no_resource:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800623 free_irq(ptp_qoriq->irq, ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800624no_node:
Yangbo Lu1e562c82019-02-12 12:23:56 +0800625 kfree(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800626no_memory:
627 return err;
628}
629
Yangbo Lu1e562c82019-02-12 12:23:56 +0800630static int ptp_qoriq_remove(struct platform_device *dev)
Yangbo Luceefc71d2018-05-25 12:40:34 +0800631{
Yangbo Lu1e562c82019-02-12 12:23:56 +0800632 struct ptp_qoriq *ptp_qoriq = platform_get_drvdata(dev);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800633
Yangbo Luff545712019-02-12 12:23:58 +0800634 ptp_qoriq_free(ptp_qoriq);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800635 release_resource(ptp_qoriq->rsrc);
Yangbo Lu1e562c82019-02-12 12:23:56 +0800636 kfree(ptp_qoriq);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800637 return 0;
638}
639
640static const struct of_device_id match_table[] = {
641 { .compatible = "fsl,etsec-ptp" },
Yangbo Lua8f62d02018-06-25 20:37:08 +0800642 { .compatible = "fsl,fman-ptp-timer" },
Yangbo Luceefc71d2018-05-25 12:40:34 +0800643 {},
644};
645MODULE_DEVICE_TABLE(of, match_table);
646
Yangbo Lu1e562c82019-02-12 12:23:56 +0800647static struct platform_driver ptp_qoriq_driver = {
Yangbo Luceefc71d2018-05-25 12:40:34 +0800648 .driver = {
649 .name = "ptp_qoriq",
650 .of_match_table = match_table,
651 },
Yangbo Lu1e562c82019-02-12 12:23:56 +0800652 .probe = ptp_qoriq_probe,
653 .remove = ptp_qoriq_remove,
Yangbo Luceefc71d2018-05-25 12:40:34 +0800654};
655
Yangbo Lu1e562c82019-02-12 12:23:56 +0800656module_platform_driver(ptp_qoriq_driver);
Yangbo Luceefc71d2018-05-25 12:40:34 +0800657
658MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
659MODULE_DESCRIPTION("PTP clock for Freescale QorIQ 1588 timer");
660MODULE_LICENSE("GPL");