Skip to content

Commit 91305f2

Browse files
yangbolu1991davem330
authored andcommitted
ptp_qoriq: support automatic configuration for ptp timer
This patch is to support automatic configuration for ptp timer. If required ptp dts properties are not provided, driver could try to calculate a set of default configurations to initialize the ptp timer. This makes the driver work for many boards which don't have the required ptp dts properties in current kernel. Also the users could set dts properties by themselves according to their requirement. Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a16b5da commit 91305f2

2 files changed

Lines changed: 113 additions & 4 deletions

File tree

‎drivers/ptp/ptp_qoriq.c‎

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/of_platform.h>
3030
#include <linux/timex.h>
3131
#include <linux/slab.h>
32+
#include <linux/clk.h>
3233

3334
#include <linux/fsl/ptp_qoriq.h>
3435

@@ -317,6 +318,105 @@ static const struct ptp_clock_info ptp_qoriq_caps = {
317318
.enable = ptp_qoriq_enable,
318319
};
319320

321+
/**
322+
* qoriq_ptp_nominal_freq - calculate nominal frequency according to
323+
* reference clock frequency
324+
*
325+
* @clk_src: reference clock frequency
326+
*
327+
* The nominal frequency is the desired clock frequency.
328+
* It should be less than the reference clock frequency.
329+
* It should be a factor of 1000MHz.
330+
*
331+
* Return the nominal frequency
332+
*/
333+
static u32 qoriq_ptp_nominal_freq(u32 clk_src)
334+
{
335+
u32 remainder = 0;
336+
337+
clk_src /= 1000000;
338+
remainder = clk_src % 100;
339+
if (remainder) {
340+
clk_src -= remainder;
341+
clk_src += 100;
342+
}
343+
344+
do {
345+
clk_src -= 100;
346+
347+
} while (1000 % clk_src);
348+
349+
return clk_src * 1000000;
350+
}
351+
352+
/**
353+
* qoriq_ptp_auto_config - calculate a set of default configurations
354+
*
355+
* @qoriq_ptp: pointer to qoriq_ptp
356+
* @node: pointer to device_node
357+
*
358+
* If below dts properties are not provided, this function will be
359+
* called to calculate a set of default configurations for them.
360+
* "fsl,tclk-period"
361+
* "fsl,tmr-prsc"
362+
* "fsl,tmr-add"
363+
* "fsl,tmr-fiper1"
364+
* "fsl,tmr-fiper2"
365+
* "fsl,max-adj"
366+
*
367+
* Return 0 if success
368+
*/
369+
static int qoriq_ptp_auto_config(struct qoriq_ptp *qoriq_ptp,
370+
struct device_node *node)
371+
{
372+
struct clk *clk;
373+
u64 freq_comp;
374+
u64 max_adj;
375+
u32 nominal_freq;
376+
u32 clk_src = 0;
377+
378+
qoriq_ptp->cksel = DEFAULT_CKSEL;
379+
380+
clk = of_clk_get(node, 0);
381+
if (!IS_ERR(clk)) {
382+
clk_src = clk_get_rate(clk);
383+
clk_put(clk);
384+
}
385+
386+
if (clk_src <= 100000000UL) {
387+
pr_err("error reference clock value, or lower than 100MHz\n");
388+
return -EINVAL;
389+
}
390+
391+
nominal_freq = qoriq_ptp_nominal_freq(clk_src);
392+
if (!nominal_freq)
393+
return -EINVAL;
394+
395+
qoriq_ptp->tclk_period = 1000000000UL / nominal_freq;
396+
qoriq_ptp->tmr_prsc = DEFAULT_TMR_PRSC;
397+
398+
/* Calculate initial frequency compensation value for TMR_ADD register.
399+
* freq_comp = ceil(2^32 / freq_ratio)
400+
* freq_ratio = reference_clock_freq / nominal_freq
401+
*/
402+
freq_comp = ((u64)1 << 32) * nominal_freq;
403+
if (do_div(freq_comp, clk_src))
404+
freq_comp++;
405+
406+
qoriq_ptp->tmr_add = freq_comp;
407+
qoriq_ptp->tmr_fiper1 = DEFAULT_FIPER1_PERIOD - qoriq_ptp->tclk_period;
408+
qoriq_ptp->tmr_fiper2 = DEFAULT_FIPER2_PERIOD - qoriq_ptp->tclk_period;
409+
410+
/* max_adj = 1000000000 * (freq_ratio - 1.0) - 1
411+
* freq_ratio = reference_clock_freq / nominal_freq
412+
*/
413+
max_adj = 1000000000ULL * (clk_src - nominal_freq);
414+
max_adj = max_adj / nominal_freq - 1;
415+
qoriq_ptp->caps.max_adj = max_adj;
416+
417+
return 0;
418+
}
419+
320420
static int qoriq_ptp_probe(struct platform_device *dev)
321421
{
322422
struct device_node *node = dev->dev.of_node;
@@ -332,7 +432,7 @@ static int qoriq_ptp_probe(struct platform_device *dev)
332432
if (!qoriq_ptp)
333433
goto no_memory;
334434

335-
err = -ENODEV;
435+
err = -EINVAL;
336436

337437
qoriq_ptp->caps = ptp_qoriq_caps;
338438

@@ -351,10 +451,14 @@ static int qoriq_ptp_probe(struct platform_device *dev)
351451
"fsl,tmr-fiper2", &qoriq_ptp->tmr_fiper2) ||
352452
of_property_read_u32(node,
353453
"fsl,max-adj", &qoriq_ptp->caps.max_adj)) {
354-
pr_err("device tree node missing required elements\n");
355-
goto no_node;
454+
pr_warn("device tree node missing required elements, try automatic configuration\n");
455+
456+
if (qoriq_ptp_auto_config(qoriq_ptp, node))
457+
goto no_config;
356458
}
357459

460+
err = -ENODEV;
461+
358462
qoriq_ptp->irq = platform_get_irq(dev, 0);
359463

360464
if (qoriq_ptp->irq < 0) {
@@ -436,6 +540,7 @@ static int qoriq_ptp_probe(struct platform_device *dev)
436540
release_resource(qoriq_ptp->rsrc);
437541
no_resource:
438542
free_irq(qoriq_ptp->irq, qoriq_ptp);
543+
no_config:
439544
no_node:
440545
kfree(qoriq_ptp);
441546
no_memory:

‎include/linux/fsl/ptp_qoriq.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,13 @@ struct qoriq_ptp_registers {
127127

128128

129129
#define DRIVER "ptp_qoriq"
130-
#define DEFAULT_CKSEL 1
131130
#define N_EXT_TS 2
132131

132+
#define DEFAULT_CKSEL 1
133+
#define DEFAULT_TMR_PRSC 2
134+
#define DEFAULT_FIPER1_PERIOD 1000000000
135+
#define DEFAULT_FIPER2_PERIOD 100000
136+
133137
struct qoriq_ptp {
134138
void __iomem *base;
135139
struct qoriq_ptp_registers regs;

0 commit comments

Comments
 (0)