2011-04-22 14:03:54 +04:00
|
|
|
/*
|
2018-05-25 07:40:34 +03:00
|
|
|
* PTP 1588 clock for Freescale QorIQ 1588 timer
|
2011-04-22 14:03:54 +04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 OMICRON electronics GmbH
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
2013-04-13 23:03:18 +04:00
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2011-04-22 14:03:54 +04:00
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/hrtimer.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_platform.h>
|
|
|
|
#include <linux/timex.h>
|
2018-05-25 07:40:34 +03:00
|
|
|
#include <linux/slab.h>
|
2018-08-01 13:05:54 +03:00
|
|
|
#include <linux/clk.h>
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:35 +03:00
|
|
|
#include <linux/fsl/ptp_qoriq.h>
|
2018-05-25 07:40:34 +03:00
|
|
|
|
2011-04-22 14:03:54 +04:00
|
|
|
/*
|
|
|
|
* Register access functions
|
|
|
|
*/
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
/* Caller must hold qoriq_ptp->lock. */
|
|
|
|
static u64 tmr_cnt_read(struct qoriq_ptp *qoriq_ptp)
|
2011-04-22 14:03:54 +04:00
|
|
|
{
|
2018-06-25 15:37:08 +03:00
|
|
|
struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
|
2011-04-22 14:03:54 +04:00
|
|
|
u64 ns;
|
|
|
|
u32 lo, hi;
|
|
|
|
|
2018-06-25 15:37:08 +03:00
|
|
|
lo = qoriq_read(®s->ctrl_regs->tmr_cnt_l);
|
|
|
|
hi = qoriq_read(®s->ctrl_regs->tmr_cnt_h);
|
2011-04-22 14:03:54 +04:00
|
|
|
ns = ((u64) hi) << 32;
|
|
|
|
ns |= lo;
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
/* Caller must hold qoriq_ptp->lock. */
|
|
|
|
static void tmr_cnt_write(struct qoriq_ptp *qoriq_ptp, u64 ns)
|
2011-04-22 14:03:54 +04:00
|
|
|
{
|
2018-06-25 15:37:08 +03:00
|
|
|
struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
|
2011-04-22 14:03:54 +04:00
|
|
|
u32 hi = ns >> 32;
|
|
|
|
u32 lo = ns & 0xffffffff;
|
|
|
|
|
2018-06-25 15:37:08 +03:00
|
|
|
qoriq_write(®s->ctrl_regs->tmr_cnt_l, lo);
|
|
|
|
qoriq_write(®s->ctrl_regs->tmr_cnt_h, hi);
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
/* Caller must hold qoriq_ptp->lock. */
|
|
|
|
static void set_alarm(struct qoriq_ptp *qoriq_ptp)
|
2011-04-22 14:03:54 +04:00
|
|
|
{
|
2018-06-25 15:37:08 +03:00
|
|
|
struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
|
2011-04-22 14:03:54 +04:00
|
|
|
u64 ns;
|
|
|
|
u32 lo, hi;
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
ns = tmr_cnt_read(qoriq_ptp) + 1500000000ULL;
|
2011-04-22 14:03:54 +04:00
|
|
|
ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
|
2018-05-25 07:40:34 +03:00
|
|
|
ns -= qoriq_ptp->tclk_period;
|
2011-04-22 14:03:54 +04:00
|
|
|
hi = ns >> 32;
|
|
|
|
lo = ns & 0xffffffff;
|
2018-06-25 15:37:08 +03:00
|
|
|
qoriq_write(®s->alarm_regs->tmr_alarm1_l, lo);
|
|
|
|
qoriq_write(®s->alarm_regs->tmr_alarm1_h, hi);
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
/* Caller must hold qoriq_ptp->lock. */
|
|
|
|
static void set_fipers(struct qoriq_ptp *qoriq_ptp)
|
2011-04-22 14:03:54 +04:00
|
|
|
{
|
2018-06-25 15:37:08 +03:00
|
|
|
struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
set_alarm(qoriq_ptp);
|
2018-06-25 15:37:08 +03:00
|
|
|
qoriq_write(®s->fiper_regs->tmr_fiper1, qoriq_ptp->tmr_fiper1);
|
|
|
|
qoriq_write(®s->fiper_regs->tmr_fiper2, qoriq_ptp->tmr_fiper2);
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:41:39 +03:00
|
|
|
static int extts_clean_up(struct qoriq_ptp *qoriq_ptp, int index,
|
|
|
|
bool update_event)
|
|
|
|
{
|
|
|
|
struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
|
|
|
|
struct ptp_clock_event event;
|
|
|
|
void __iomem *reg_etts_l;
|
|
|
|
void __iomem *reg_etts_h;
|
|
|
|
u32 valid, stat, lo, hi;
|
|
|
|
|
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
valid = ETS1_VLD;
|
|
|
|
reg_etts_l = ®s->etts_regs->tmr_etts1_l;
|
|
|
|
reg_etts_h = ®s->etts_regs->tmr_etts1_h;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
valid = ETS2_VLD;
|
|
|
|
reg_etts_l = ®s->etts_regs->tmr_etts2_l;
|
|
|
|
reg_etts_h = ®s->etts_regs->tmr_etts2_h;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.type = PTP_CLOCK_EXTTS;
|
|
|
|
event.index = index;
|
|
|
|
|
|
|
|
do {
|
|
|
|
lo = qoriq_read(reg_etts_l);
|
|
|
|
hi = qoriq_read(reg_etts_h);
|
|
|
|
|
|
|
|
if (update_event) {
|
|
|
|
event.timestamp = ((u64) hi) << 32;
|
|
|
|
event.timestamp |= lo;
|
|
|
|
ptp_clock_event(qoriq_ptp->clock, &event);
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = qoriq_read(®s->ctrl_regs->tmr_stat);
|
|
|
|
} while (qoriq_ptp->extts_fifo_support && (stat & valid));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-22 14:03:54 +04:00
|
|
|
/*
|
|
|
|
* Interrupt service routine
|
|
|
|
*/
|
|
|
|
|
|
|
|
static irqreturn_t isr(int irq, void *priv)
|
|
|
|
{
|
2018-05-25 07:40:34 +03:00
|
|
|
struct qoriq_ptp *qoriq_ptp = priv;
|
2018-06-25 15:37:08 +03:00
|
|
|
struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
|
2011-04-22 14:03:54 +04:00
|
|
|
struct ptp_clock_event event;
|
|
|
|
u64 ns;
|
2019-01-21 13:41:38 +03:00
|
|
|
u32 ack = 0, lo, hi, mask, val, irqs;
|
|
|
|
|
|
|
|
spin_lock(&qoriq_ptp->lock);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-06-25 15:37:08 +03:00
|
|
|
val = qoriq_read(®s->ctrl_regs->tmr_tevent);
|
2019-01-21 13:41:38 +03:00
|
|
|
mask = qoriq_read(®s->ctrl_regs->tmr_temask);
|
|
|
|
|
|
|
|
spin_unlock(&qoriq_ptp->lock);
|
|
|
|
|
|
|
|
irqs = val & mask;
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2019-01-21 13:41:38 +03:00
|
|
|
if (irqs & ETS1) {
|
2011-04-22 14:03:54 +04:00
|
|
|
ack |= ETS1;
|
2019-01-21 13:41:39 +03:00
|
|
|
extts_clean_up(qoriq_ptp, 0, true);
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:41:38 +03:00
|
|
|
if (irqs & ETS2) {
|
2011-04-22 14:03:54 +04:00
|
|
|
ack |= ETS2;
|
2019-01-21 13:41:39 +03:00
|
|
|
extts_clean_up(qoriq_ptp, 1, true);
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:41:38 +03:00
|
|
|
if (irqs & ALM2) {
|
2011-04-22 14:03:54 +04:00
|
|
|
ack |= ALM2;
|
2018-05-25 07:40:34 +03:00
|
|
|
if (qoriq_ptp->alarm_value) {
|
2011-04-22 14:03:54 +04:00
|
|
|
event.type = PTP_CLOCK_ALARM;
|
|
|
|
event.index = 0;
|
2018-05-25 07:40:34 +03:00
|
|
|
event.timestamp = qoriq_ptp->alarm_value;
|
|
|
|
ptp_clock_event(qoriq_ptp->clock, &event);
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
2018-05-25 07:40:34 +03:00
|
|
|
if (qoriq_ptp->alarm_interval) {
|
|
|
|
ns = qoriq_ptp->alarm_value + qoriq_ptp->alarm_interval;
|
2011-04-22 14:03:54 +04:00
|
|
|
hi = ns >> 32;
|
|
|
|
lo = ns & 0xffffffff;
|
2018-06-25 15:37:08 +03:00
|
|
|
qoriq_write(®s->alarm_regs->tmr_alarm2_l, lo);
|
|
|
|
qoriq_write(®s->alarm_regs->tmr_alarm2_h, hi);
|
2018-05-25 07:40:34 +03:00
|
|
|
qoriq_ptp->alarm_value = ns;
|
2011-04-22 14:03:54 +04:00
|
|
|
} else {
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_lock(&qoriq_ptp->lock);
|
2018-06-25 15:37:08 +03:00
|
|
|
mask = qoriq_read(®s->ctrl_regs->tmr_temask);
|
2011-04-22 14:03:54 +04:00
|
|
|
mask &= ~ALM2EN;
|
2018-06-25 15:37:08 +03:00
|
|
|
qoriq_write(®s->ctrl_regs->tmr_temask, mask);
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_unlock(&qoriq_ptp->lock);
|
|
|
|
qoriq_ptp->alarm_value = 0;
|
|
|
|
qoriq_ptp->alarm_interval = 0;
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 13:41:38 +03:00
|
|
|
if (irqs & PP1) {
|
2011-04-22 14:03:54 +04:00
|
|
|
ack |= PP1;
|
|
|
|
event.type = PTP_CLOCK_PPS;
|
2018-05-25 07:40:34 +03:00
|
|
|
ptp_clock_event(qoriq_ptp->clock, &event);
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ack) {
|
2018-06-25 15:37:08 +03:00
|
|
|
qoriq_write(®s->ctrl_regs->tmr_tevent, ack);
|
2011-04-22 14:03:54 +04:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
} else
|
|
|
|
return IRQ_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PTP clock operations
|
|
|
|
*/
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
static int ptp_qoriq_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
|
2011-04-22 14:03:54 +04:00
|
|
|
{
|
2016-11-23 23:11:04 +03:00
|
|
|
u64 adj, diff;
|
|
|
|
u32 tmr_add;
|
2011-04-22 14:03:54 +04:00
|
|
|
int neg_adj = 0;
|
2018-05-25 07:40:34 +03:00
|
|
|
struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps);
|
2018-06-25 15:37:08 +03:00
|
|
|
struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2016-11-23 23:11:04 +03:00
|
|
|
if (scaled_ppm < 0) {
|
2011-04-22 14:03:54 +04:00
|
|
|
neg_adj = 1;
|
2016-11-23 23:11:04 +03:00
|
|
|
scaled_ppm = -scaled_ppm;
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
2018-05-25 07:40:34 +03:00
|
|
|
tmr_add = qoriq_ptp->tmr_add;
|
2011-04-22 14:03:54 +04:00
|
|
|
adj = tmr_add;
|
2016-11-23 23:11:04 +03:00
|
|
|
|
|
|
|
/* calculate diff as adj*(scaled_ppm/65536)/1000000
|
|
|
|
* and round() to the nearest integer
|
|
|
|
*/
|
|
|
|
adj *= scaled_ppm;
|
|
|
|
diff = div_u64(adj, 8000000);
|
|
|
|
diff = (diff >> 13) + ((diff >> 12) & 1);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
|
|
|
tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
|
|
|
|
|
2018-06-25 15:37:08 +03:00
|
|
|
qoriq_write(®s->ctrl_regs->tmr_add, tmr_add);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
static int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta)
|
2011-04-22 14:03:54 +04:00
|
|
|
{
|
|
|
|
s64 now;
|
|
|
|
unsigned long flags;
|
2018-05-25 07:40:34 +03:00
|
|
|
struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_lock_irqsave(&qoriq_ptp->lock, flags);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
now = tmr_cnt_read(qoriq_ptp);
|
2011-04-22 14:03:54 +04:00
|
|
|
now += delta;
|
2018-05-25 07:40:34 +03:00
|
|
|
tmr_cnt_write(qoriq_ptp, now);
|
|
|
|
set_fipers(qoriq_ptp);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
static int ptp_qoriq_gettime(struct ptp_clock_info *ptp,
|
2015-03-30 00:11:59 +03:00
|
|
|
struct timespec64 *ts)
|
2011-04-22 14:03:54 +04:00
|
|
|
{
|
|
|
|
u64 ns;
|
|
|
|
unsigned long flags;
|
2018-05-25 07:40:34 +03:00
|
|
|
struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_lock_irqsave(&qoriq_ptp->lock, flags);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
ns = tmr_cnt_read(qoriq_ptp);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2015-04-01 00:08:10 +03:00
|
|
|
*ts = ns_to_timespec64(ns);
|
|
|
|
|
2011-04-22 14:03:54 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
static int ptp_qoriq_settime(struct ptp_clock_info *ptp,
|
2015-03-30 00:11:59 +03:00
|
|
|
const struct timespec64 *ts)
|
2011-04-22 14:03:54 +04:00
|
|
|
{
|
|
|
|
u64 ns;
|
|
|
|
unsigned long flags;
|
2018-05-25 07:40:34 +03:00
|
|
|
struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2015-04-01 00:08:10 +03:00
|
|
|
ns = timespec64_to_ns(ts);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_lock_irqsave(&qoriq_ptp->lock, flags);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
tmr_cnt_write(qoriq_ptp, ns);
|
|
|
|
set_fipers(qoriq_ptp);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
static int ptp_qoriq_enable(struct ptp_clock_info *ptp,
|
2011-04-22 14:03:54 +04:00
|
|
|
struct ptp_clock_request *rq, int on)
|
|
|
|
{
|
2018-05-25 07:40:34 +03:00
|
|
|
struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps);
|
2018-06-25 15:37:08 +03:00
|
|
|
struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
|
2011-04-22 14:03:54 +04:00
|
|
|
unsigned long flags;
|
2019-01-21 13:41:38 +03:00
|
|
|
u32 bit, mask = 0;
|
2011-04-22 14:03:54 +04:00
|
|
|
|
|
|
|
switch (rq->type) {
|
|
|
|
case PTP_CLK_REQ_EXTTS:
|
|
|
|
switch (rq->extts.index) {
|
|
|
|
case 0:
|
|
|
|
bit = ETS1EN;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
bit = ETS2EN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2019-01-21 13:41:39 +03:00
|
|
|
|
|
|
|
if (on)
|
|
|
|
extts_clean_up(qoriq_ptp, rq->extts.index, false);
|
|
|
|
|
2019-01-21 13:41:38 +03:00
|
|
|
break;
|
2011-04-22 14:03:54 +04:00
|
|
|
case PTP_CLK_REQ_PPS:
|
2019-01-21 13:41:38 +03:00
|
|
|
bit = PP1EN;
|
2011-04-22 14:03:54 +04:00
|
|
|
break;
|
2019-01-21 13:41:38 +03:00
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_irqsave(&qoriq_ptp->lock, flags);
|
|
|
|
|
|
|
|
mask = qoriq_read(®s->ctrl_regs->tmr_temask);
|
|
|
|
if (on) {
|
|
|
|
mask |= bit;
|
|
|
|
qoriq_write(®s->ctrl_regs->tmr_tevent, bit);
|
|
|
|
} else {
|
|
|
|
mask &= ~bit;
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
|
|
|
|
2019-01-21 13:41:38 +03:00
|
|
|
qoriq_write(®s->ctrl_regs->tmr_temask, mask);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
|
|
|
|
return 0;
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
static const struct ptp_clock_info ptp_qoriq_caps = {
|
2011-04-22 14:03:54 +04:00
|
|
|
.owner = THIS_MODULE,
|
2018-05-25 07:40:34 +03:00
|
|
|
.name = "qoriq ptp clock",
|
2011-04-22 14:03:54 +04:00
|
|
|
.max_adj = 512000,
|
2013-04-22 23:42:16 +04:00
|
|
|
.n_alarm = 0,
|
2011-04-22 14:03:54 +04:00
|
|
|
.n_ext_ts = N_EXT_TS,
|
|
|
|
.n_per_out = 0,
|
2014-03-21 01:21:55 +04:00
|
|
|
.n_pins = 0,
|
2011-04-22 14:03:54 +04:00
|
|
|
.pps = 1,
|
2018-05-25 07:40:34 +03:00
|
|
|
.adjfine = ptp_qoriq_adjfine,
|
|
|
|
.adjtime = ptp_qoriq_adjtime,
|
|
|
|
.gettime64 = ptp_qoriq_gettime,
|
|
|
|
.settime64 = ptp_qoriq_settime,
|
|
|
|
.enable = ptp_qoriq_enable,
|
2011-04-22 14:03:54 +04:00
|
|
|
};
|
|
|
|
|
2018-08-01 13:05:54 +03:00
|
|
|
/**
|
|
|
|
* qoriq_ptp_nominal_freq - calculate nominal frequency according to
|
|
|
|
* reference clock frequency
|
|
|
|
*
|
|
|
|
* @clk_src: reference clock frequency
|
|
|
|
*
|
|
|
|
* The nominal frequency is the desired clock frequency.
|
|
|
|
* It should be less than the reference clock frequency.
|
|
|
|
* It should be a factor of 1000MHz.
|
|
|
|
*
|
|
|
|
* Return the nominal frequency
|
|
|
|
*/
|
|
|
|
static u32 qoriq_ptp_nominal_freq(u32 clk_src)
|
|
|
|
{
|
|
|
|
u32 remainder = 0;
|
|
|
|
|
|
|
|
clk_src /= 1000000;
|
|
|
|
remainder = clk_src % 100;
|
|
|
|
if (remainder) {
|
|
|
|
clk_src -= remainder;
|
|
|
|
clk_src += 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
clk_src -= 100;
|
|
|
|
|
|
|
|
} while (1000 % clk_src);
|
|
|
|
|
|
|
|
return clk_src * 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* qoriq_ptp_auto_config - calculate a set of default configurations
|
|
|
|
*
|
|
|
|
* @qoriq_ptp: pointer to qoriq_ptp
|
|
|
|
* @node: pointer to device_node
|
|
|
|
*
|
|
|
|
* If below dts properties are not provided, this function will be
|
|
|
|
* called to calculate a set of default configurations for them.
|
|
|
|
* "fsl,tclk-period"
|
|
|
|
* "fsl,tmr-prsc"
|
|
|
|
* "fsl,tmr-add"
|
|
|
|
* "fsl,tmr-fiper1"
|
|
|
|
* "fsl,tmr-fiper2"
|
|
|
|
* "fsl,max-adj"
|
|
|
|
*
|
|
|
|
* Return 0 if success
|
|
|
|
*/
|
|
|
|
static int qoriq_ptp_auto_config(struct qoriq_ptp *qoriq_ptp,
|
|
|
|
struct device_node *node)
|
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
u64 freq_comp;
|
|
|
|
u64 max_adj;
|
|
|
|
u32 nominal_freq;
|
2018-08-06 07:39:11 +03:00
|
|
|
u32 remainder = 0;
|
2018-08-01 13:05:54 +03:00
|
|
|
u32 clk_src = 0;
|
|
|
|
|
|
|
|
qoriq_ptp->cksel = DEFAULT_CKSEL;
|
|
|
|
|
|
|
|
clk = of_clk_get(node, 0);
|
|
|
|
if (!IS_ERR(clk)) {
|
|
|
|
clk_src = clk_get_rate(clk);
|
|
|
|
clk_put(clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clk_src <= 100000000UL) {
|
|
|
|
pr_err("error reference clock value, or lower than 100MHz\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nominal_freq = qoriq_ptp_nominal_freq(clk_src);
|
|
|
|
if (!nominal_freq)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
qoriq_ptp->tclk_period = 1000000000UL / nominal_freq;
|
|
|
|
qoriq_ptp->tmr_prsc = DEFAULT_TMR_PRSC;
|
|
|
|
|
|
|
|
/* Calculate initial frequency compensation value for TMR_ADD register.
|
|
|
|
* freq_comp = ceil(2^32 / freq_ratio)
|
|
|
|
* freq_ratio = reference_clock_freq / nominal_freq
|
|
|
|
*/
|
|
|
|
freq_comp = ((u64)1 << 32) * nominal_freq;
|
2018-08-06 07:39:11 +03:00
|
|
|
freq_comp = div_u64_rem(freq_comp, clk_src, &remainder);
|
|
|
|
if (remainder)
|
2018-08-01 13:05:54 +03:00
|
|
|
freq_comp++;
|
|
|
|
|
|
|
|
qoriq_ptp->tmr_add = freq_comp;
|
|
|
|
qoriq_ptp->tmr_fiper1 = DEFAULT_FIPER1_PERIOD - qoriq_ptp->tclk_period;
|
|
|
|
qoriq_ptp->tmr_fiper2 = DEFAULT_FIPER2_PERIOD - qoriq_ptp->tclk_period;
|
|
|
|
|
|
|
|
/* max_adj = 1000000000 * (freq_ratio - 1.0) - 1
|
|
|
|
* freq_ratio = reference_clock_freq / nominal_freq
|
|
|
|
*/
|
|
|
|
max_adj = 1000000000ULL * (clk_src - nominal_freq);
|
2018-08-06 07:39:11 +03:00
|
|
|
max_adj = div_u64(max_adj, nominal_freq) - 1;
|
2018-08-01 13:05:54 +03:00
|
|
|
qoriq_ptp->caps.max_adj = max_adj;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
static int qoriq_ptp_probe(struct platform_device *dev)
|
2011-04-22 14:03:54 +04:00
|
|
|
{
|
|
|
|
struct device_node *node = dev->dev.of_node;
|
2018-05-25 07:40:34 +03:00
|
|
|
struct qoriq_ptp *qoriq_ptp;
|
2018-06-25 15:37:08 +03:00
|
|
|
struct qoriq_ptp_registers *regs;
|
2015-03-30 00:11:59 +03:00
|
|
|
struct timespec64 now;
|
2011-04-22 14:03:54 +04:00
|
|
|
int err = -ENOMEM;
|
|
|
|
u32 tmr_ctrl;
|
|
|
|
unsigned long flags;
|
2018-06-25 15:37:08 +03:00
|
|
|
void __iomem *base;
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
qoriq_ptp = kzalloc(sizeof(*qoriq_ptp), GFP_KERNEL);
|
|
|
|
if (!qoriq_ptp)
|
2011-04-22 14:03:54 +04:00
|
|
|
goto no_memory;
|
|
|
|
|
2018-08-01 13:05:54 +03:00
|
|
|
err = -EINVAL;
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2019-01-21 13:41:42 +03:00
|
|
|
qoriq_ptp->dev = &dev->dev;
|
2018-05-25 07:40:34 +03:00
|
|
|
qoriq_ptp->caps = ptp_qoriq_caps;
|
2013-09-27 17:40:27 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
if (of_property_read_u32(node, "fsl,cksel", &qoriq_ptp->cksel))
|
|
|
|
qoriq_ptp->cksel = DEFAULT_CKSEL;
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2019-01-21 13:41:39 +03:00
|
|
|
if (of_property_read_bool(node, "fsl,extts-fifo"))
|
|
|
|
qoriq_ptp->extts_fifo_support = true;
|
|
|
|
else
|
|
|
|
qoriq_ptp->extts_fifo_support = false;
|
|
|
|
|
2016-02-24 12:26:55 +03:00
|
|
|
if (of_property_read_u32(node,
|
2018-05-25 07:40:34 +03:00
|
|
|
"fsl,tclk-period", &qoriq_ptp->tclk_period) ||
|
2016-02-24 12:26:55 +03:00
|
|
|
of_property_read_u32(node,
|
2018-05-25 07:40:34 +03:00
|
|
|
"fsl,tmr-prsc", &qoriq_ptp->tmr_prsc) ||
|
2016-02-24 12:26:55 +03:00
|
|
|
of_property_read_u32(node,
|
2018-05-25 07:40:34 +03:00
|
|
|
"fsl,tmr-add", &qoriq_ptp->tmr_add) ||
|
2016-02-24 12:26:55 +03:00
|
|
|
of_property_read_u32(node,
|
2018-05-25 07:40:34 +03:00
|
|
|
"fsl,tmr-fiper1", &qoriq_ptp->tmr_fiper1) ||
|
2016-02-24 12:26:55 +03:00
|
|
|
of_property_read_u32(node,
|
2018-05-25 07:40:34 +03:00
|
|
|
"fsl,tmr-fiper2", &qoriq_ptp->tmr_fiper2) ||
|
2016-02-24 12:26:55 +03:00
|
|
|
of_property_read_u32(node,
|
2018-05-25 07:40:34 +03:00
|
|
|
"fsl,max-adj", &qoriq_ptp->caps.max_adj)) {
|
2018-08-01 13:05:54 +03:00
|
|
|
pr_warn("device tree node missing required elements, try automatic configuration\n");
|
|
|
|
|
|
|
|
if (qoriq_ptp_auto_config(qoriq_ptp, node))
|
|
|
|
goto no_config;
|
2011-04-22 14:03:54 +04:00
|
|
|
}
|
|
|
|
|
2018-08-01 13:05:54 +03:00
|
|
|
err = -ENODEV;
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
qoriq_ptp->irq = platform_get_irq(dev, 0);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
if (qoriq_ptp->irq < 0) {
|
2011-04-22 14:03:54 +04:00
|
|
|
pr_err("irq not in device tree\n");
|
|
|
|
goto no_node;
|
|
|
|
}
|
2018-06-25 15:37:08 +03:00
|
|
|
if (request_irq(qoriq_ptp->irq, isr, IRQF_SHARED, DRIVER, qoriq_ptp)) {
|
2011-04-22 14:03:54 +04:00
|
|
|
pr_err("request_irq failed\n");
|
|
|
|
goto no_node;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
qoriq_ptp->rsrc = platform_get_resource(dev, IORESOURCE_MEM, 0);
|
|
|
|
if (!qoriq_ptp->rsrc) {
|
2011-04-22 14:03:54 +04:00
|
|
|
pr_err("no resource\n");
|
|
|
|
goto no_resource;
|
|
|
|
}
|
2018-05-25 07:40:34 +03:00
|
|
|
if (request_resource(&iomem_resource, qoriq_ptp->rsrc)) {
|
2011-04-22 14:03:54 +04:00
|
|
|
pr_err("resource busy\n");
|
|
|
|
goto no_resource;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_lock_init(&qoriq_ptp->lock);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-06-25 15:37:08 +03:00
|
|
|
base = ioremap(qoriq_ptp->rsrc->start,
|
|
|
|
resource_size(qoriq_ptp->rsrc));
|
|
|
|
if (!base) {
|
2011-04-22 14:03:54 +04:00
|
|
|
pr_err("ioremap ptp registers failed\n");
|
|
|
|
goto no_ioremap;
|
|
|
|
}
|
2018-06-25 15:37:08 +03:00
|
|
|
|
|
|
|
qoriq_ptp->base = base;
|
|
|
|
|
|
|
|
if (of_device_is_compatible(node, "fsl,fman-ptp-timer")) {
|
|
|
|
qoriq_ptp->regs.ctrl_regs = base + FMAN_CTRL_REGS_OFFSET;
|
|
|
|
qoriq_ptp->regs.alarm_regs = base + FMAN_ALARM_REGS_OFFSET;
|
|
|
|
qoriq_ptp->regs.fiper_regs = base + FMAN_FIPER_REGS_OFFSET;
|
|
|
|
qoriq_ptp->regs.etts_regs = base + FMAN_ETTS_REGS_OFFSET;
|
|
|
|
} else {
|
|
|
|
qoriq_ptp->regs.ctrl_regs = base + CTRL_REGS_OFFSET;
|
|
|
|
qoriq_ptp->regs.alarm_regs = base + ALARM_REGS_OFFSET;
|
|
|
|
qoriq_ptp->regs.fiper_regs = base + FIPER_REGS_OFFSET;
|
|
|
|
qoriq_ptp->regs.etts_regs = base + ETTS_REGS_OFFSET;
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:20:39 +03:00
|
|
|
ktime_get_real_ts64(&now);
|
2018-05-25 07:40:34 +03:00
|
|
|
ptp_qoriq_settime(&qoriq_ptp->caps, &now);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
|
|
|
tmr_ctrl =
|
2018-05-25 07:40:34 +03:00
|
|
|
(qoriq_ptp->tclk_period & TCLK_PERIOD_MASK) << TCLK_PERIOD_SHIFT |
|
|
|
|
(qoriq_ptp->cksel & CKSEL_MASK) << CKSEL_SHIFT;
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_lock_irqsave(&qoriq_ptp->lock, flags);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-06-25 15:37:08 +03:00
|
|
|
regs = &qoriq_ptp->regs;
|
|
|
|
qoriq_write(®s->ctrl_regs->tmr_ctrl, tmr_ctrl);
|
|
|
|
qoriq_write(®s->ctrl_regs->tmr_add, qoriq_ptp->tmr_add);
|
|
|
|
qoriq_write(®s->ctrl_regs->tmr_prsc, qoriq_ptp->tmr_prsc);
|
|
|
|
qoriq_write(®s->fiper_regs->tmr_fiper1, qoriq_ptp->tmr_fiper1);
|
|
|
|
qoriq_write(®s->fiper_regs->tmr_fiper2, qoriq_ptp->tmr_fiper2);
|
2018-05-25 07:40:34 +03:00
|
|
|
set_alarm(qoriq_ptp);
|
2018-06-25 15:37:08 +03:00
|
|
|
qoriq_write(®s->ctrl_regs->tmr_ctrl, tmr_ctrl|FIPERST|RTPE|TE|FRD);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
qoriq_ptp->clock = ptp_clock_register(&qoriq_ptp->caps, &dev->dev);
|
|
|
|
if (IS_ERR(qoriq_ptp->clock)) {
|
|
|
|
err = PTR_ERR(qoriq_ptp->clock);
|
2011-04-22 14:03:54 +04:00
|
|
|
goto no_clock;
|
|
|
|
}
|
2018-05-25 07:40:34 +03:00
|
|
|
qoriq_ptp->phc_index = ptp_clock_index(qoriq_ptp->clock);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2019-01-21 13:41:42 +03:00
|
|
|
ptp_qoriq_create_debugfs(qoriq_ptp);
|
2018-05-25 07:40:34 +03:00
|
|
|
platform_set_drvdata(dev, qoriq_ptp);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
no_clock:
|
2018-06-25 15:37:08 +03:00
|
|
|
iounmap(qoriq_ptp->base);
|
2011-04-22 14:03:54 +04:00
|
|
|
no_ioremap:
|
2018-05-25 07:40:34 +03:00
|
|
|
release_resource(qoriq_ptp->rsrc);
|
2011-04-22 14:03:54 +04:00
|
|
|
no_resource:
|
2018-05-25 07:40:34 +03:00
|
|
|
free_irq(qoriq_ptp->irq, qoriq_ptp);
|
2018-08-01 13:05:54 +03:00
|
|
|
no_config:
|
2011-04-22 14:03:54 +04:00
|
|
|
no_node:
|
2018-05-25 07:40:34 +03:00
|
|
|
kfree(qoriq_ptp);
|
2011-04-22 14:03:54 +04:00
|
|
|
no_memory:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
static int qoriq_ptp_remove(struct platform_device *dev)
|
2011-04-22 14:03:54 +04:00
|
|
|
{
|
2018-05-25 07:40:34 +03:00
|
|
|
struct qoriq_ptp *qoriq_ptp = platform_get_drvdata(dev);
|
2018-06-25 15:37:08 +03:00
|
|
|
struct qoriq_ptp_registers *regs = &qoriq_ptp->regs;
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-06-25 15:37:08 +03:00
|
|
|
qoriq_write(®s->ctrl_regs->tmr_temask, 0);
|
|
|
|
qoriq_write(®s->ctrl_regs->tmr_ctrl, 0);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2019-01-21 13:41:42 +03:00
|
|
|
ptp_qoriq_remove_debugfs(qoriq_ptp);
|
2018-05-25 07:40:34 +03:00
|
|
|
ptp_clock_unregister(qoriq_ptp->clock);
|
2018-06-25 15:37:08 +03:00
|
|
|
iounmap(qoriq_ptp->base);
|
2018-05-25 07:40:34 +03:00
|
|
|
release_resource(qoriq_ptp->rsrc);
|
|
|
|
free_irq(qoriq_ptp->irq, qoriq_ptp);
|
|
|
|
kfree(qoriq_ptp);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:37:34 +03:00
|
|
|
static const struct of_device_id match_table[] = {
|
2011-04-22 14:03:54 +04:00
|
|
|
{ .compatible = "fsl,etsec-ptp" },
|
2018-06-25 15:37:08 +03:00
|
|
|
{ .compatible = "fsl,fman-ptp-timer" },
|
2011-04-22 14:03:54 +04:00
|
|
|
{},
|
|
|
|
};
|
2015-09-18 18:55:27 +03:00
|
|
|
MODULE_DEVICE_TABLE(of, match_table);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
static struct platform_driver qoriq_ptp_driver = {
|
2011-04-22 14:03:54 +04:00
|
|
|
.driver = {
|
2018-05-25 07:40:34 +03:00
|
|
|
.name = "ptp_qoriq",
|
2011-04-22 14:03:54 +04:00
|
|
|
.of_match_table = match_table,
|
|
|
|
},
|
2018-05-25 07:40:34 +03:00
|
|
|
.probe = qoriq_ptp_probe,
|
|
|
|
.remove = qoriq_ptp_remove,
|
2011-04-22 14:03:54 +04:00
|
|
|
};
|
|
|
|
|
2018-05-25 07:40:34 +03:00
|
|
|
module_platform_driver(qoriq_ptp_driver);
|
2011-04-22 14:03:54 +04:00
|
|
|
|
2012-03-17 02:39:29 +04:00
|
|
|
MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
|
2018-05-25 07:40:34 +03:00
|
|
|
MODULE_DESCRIPTION("PTP clock for Freescale QorIQ 1588 timer");
|
2011-04-22 14:03:54 +04:00
|
|
|
MODULE_LICENSE("GPL");
|