Skip to content

Commit 86232bf

Browse files
alexandrebelloniDaniel Lezcano
authored andcommitted
clocksource/drivers/tcb_clksrc: Stop depending on atmel_tclib
atmel_tclib is probed too late in the boot process to be able to use the TCB as the boot clocksource. This is an issue for SoCs without the PIT (sams70, samv70 and samv71 families) as they simply currently can't boot. Get rid of the atmel_tclib dependency and probe everything on our own using the correct device tree binding. This also allows getting rid of ATMEL_TCB_CLKSRC_BLOCK and makes the driver a bit more flexible as the TCB is not hardcoded in the kernel anymore. Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
1 parent c2c9136 commit 86232bf

2 files changed

Lines changed: 70 additions & 52 deletions

File tree

‎drivers/clocksource/tcb_clksrc.c‎

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include <linux/err.h>
1010
#include <linux/ioport.h>
1111
#include <linux/io.h>
12-
#include <linux/platform_device.h>
12+
#include <linux/of_address.h>
13+
#include <linux/of_irq.h>
1314
#include <linux/syscore_ops.h>
1415
#include <soc/at91/atmel_tcb.h>
1516

@@ -28,13 +29,6 @@
2829
* source, used in either periodic or oneshot mode. This runs
2930
* at 32 KiHZ, and can handle delays of up to two seconds.
3031
*
31-
* A boot clocksource and clockevent source are also currently needed,
32-
* unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
33-
* this code can be used when init_timers() is called, well before most
34-
* devices are set up. (Some low end AT91 parts, which can run uClinux,
35-
* have only the timers in one TC block... they currently don't support
36-
* the tclib code, because of that initialization issue.)
37-
*
3832
* REVISIT behavior during system suspend states... we should disable
3933
* all clocks and save the power. Easily done for clockevent devices,
4034
* but clocksources won't necessarily get the needed notifications.
@@ -112,7 +106,6 @@ void tc_clksrc_resume(struct clocksource *cs)
112106
}
113107

114108
static struct clocksource clksrc = {
115-
.name = "tcb_clksrc",
116109
.rating = 200,
117110
.read = tc_get_cycles,
118111
.mask = CLOCKSOURCE_MASK(32),
@@ -214,7 +207,6 @@ static int tc_next_event(unsigned long delta, struct clock_event_device *d)
214207

215208
static struct tc_clkevt_device clkevt = {
216209
.clkevt = {
217-
.name = "tc_clkevt",
218210
.features = CLOCK_EVT_FEAT_PERIODIC |
219211
CLOCK_EVT_FEAT_ONESHOT,
220212
/* Should be lower than at91rm9200's system timer */
@@ -330,39 +322,73 @@ static void __init tcb_setup_single_chan(struct atmel_tc *tc, int mck_divisor_id
330322
writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
331323
}
332324

333-
static int __init tcb_clksrc_init(void)
334-
{
335-
static char bootinfo[] __initdata
336-
= KERN_DEBUG "%s: tc%d at %d.%03d MHz\n";
325+
static const u8 atmel_tcb_divisors[5] = { 2, 8, 32, 128, 0, };
326+
327+
static const struct of_device_id atmel_tcb_of_match[] = {
328+
{ .compatible = "atmel,at91rm9200-tcb", .data = (void *)16, },
329+
{ .compatible = "atmel,at91sam9x5-tcb", .data = (void *)32, },
330+
{ /* sentinel */ }
331+
};
337332

338-
struct platform_device *pdev;
339-
struct atmel_tc *tc;
333+
static int __init tcb_clksrc_init(struct device_node *node)
334+
{
335+
struct atmel_tc tc;
340336
struct clk *t0_clk;
337+
const struct of_device_id *match;
341338
u32 rate, divided_rate = 0;
342339
int best_divisor_idx = -1;
343340
int clk32k_divisor_idx = -1;
341+
int bits;
344342
int i;
345343
int ret;
346344

347-
tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK);
348-
if (!tc) {
349-
pr_debug("can't alloc TC for clocksource\n");
350-
return -ENODEV;
345+
/* Protect against multiple calls */
346+
if (tcaddr)
347+
return 0;
348+
349+
tc.regs = of_iomap(node->parent, 0);
350+
if (!tc.regs)
351+
return -ENXIO;
352+
353+
t0_clk = of_clk_get_by_name(node->parent, "t0_clk");
354+
if (IS_ERR(t0_clk))
355+
return PTR_ERR(t0_clk);
356+
357+
tc.slow_clk = of_clk_get_by_name(node->parent, "slow_clk");
358+
if (IS_ERR(tc.slow_clk))
359+
return PTR_ERR(tc.slow_clk);
360+
361+
tc.clk[0] = t0_clk;
362+
tc.clk[1] = of_clk_get_by_name(node->parent, "t1_clk");
363+
if (IS_ERR(tc.clk[1]))
364+
tc.clk[1] = t0_clk;
365+
tc.clk[2] = of_clk_get_by_name(node->parent, "t2_clk");
366+
if (IS_ERR(tc.clk[2]))
367+
tc.clk[2] = t0_clk;
368+
369+
tc.irq[2] = of_irq_get(node->parent, 2);
370+
if (tc.irq[2] <= 0) {
371+
tc.irq[2] = of_irq_get(node->parent, 0);
372+
if (tc.irq[2] <= 0)
373+
return -EINVAL;
351374
}
352-
tcaddr = tc->regs;
353-
pdev = tc->pdev;
354375

355-
t0_clk = tc->clk[0];
376+
match = of_match_node(atmel_tcb_of_match, node->parent);
377+
bits = (uintptr_t)match->data;
378+
379+
for (i = 0; i < ARRAY_SIZE(tc.irq); i++)
380+
writel(ATMEL_TC_ALL_IRQ, tc.regs + ATMEL_TC_REG(i, IDR));
381+
356382
ret = clk_prepare_enable(t0_clk);
357383
if (ret) {
358384
pr_debug("can't enable T0 clk\n");
359-
goto err_free_tc;
385+
return ret;
360386
}
361387

362388
/* How fast will we be counting? Pick something over 5 MHz. */
363389
rate = (u32) clk_get_rate(t0_clk);
364-
for (i = 0; i < 5; i++) {
365-
unsigned divisor = atmel_tc_divisors[i];
390+
for (i = 0; i < ARRAY_SIZE(atmel_tcb_divisors); i++) {
391+
unsigned divisor = atmel_tcb_divisors[i];
366392
unsigned tmp;
367393

368394
/* remember 32 KiHz clock for later */
@@ -381,27 +407,29 @@ static int __init tcb_clksrc_init(void)
381407
best_divisor_idx = i;
382408
}
383409

384-
385-
printk(bootinfo, clksrc.name, CONFIG_ATMEL_TCB_CLKSRC_BLOCK,
386-
divided_rate / 1000000,
410+
clksrc.name = kbasename(node->parent->full_name);
411+
clkevt.clkevt.name = kbasename(node->parent->full_name);
412+
pr_debug("%s at %d.%03d MHz\n", clksrc.name, divided_rate / 1000000,
387413
((divided_rate % 1000000) + 500) / 1000);
388414

389-
if (tc->tcb_config && tc->tcb_config->counter_width == 32) {
415+
tcaddr = tc.regs;
416+
417+
if (bits == 32) {
390418
/* use apropriate function to read 32 bit counter */
391419
clksrc.read = tc_get_cycles32;
392420
/* setup ony channel 0 */
393-
tcb_setup_single_chan(tc, best_divisor_idx);
421+
tcb_setup_single_chan(&tc, best_divisor_idx);
394422
} else {
395-
/* tclib will give us three clocks no matter what the
423+
/* we have three clocks no matter what the
396424
* underlying platform supports.
397425
*/
398-
ret = clk_prepare_enable(tc->clk[1]);
426+
ret = clk_prepare_enable(tc.clk[1]);
399427
if (ret) {
400428
pr_debug("can't enable T1 clk\n");
401429
goto err_disable_t0;
402430
}
403431
/* setup both channel 0 & 1 */
404-
tcb_setup_dual_chan(tc, best_divisor_idx);
432+
tcb_setup_dual_chan(&tc, best_divisor_idx);
405433
}
406434

407435
/* and away we go! */
@@ -410,7 +438,7 @@ static int __init tcb_clksrc_init(void)
410438
goto err_disable_t1;
411439

412440
/* channel 2: periodic and oneshot timer support */
413-
ret = setup_clkevents(tc, clk32k_divisor_idx);
441+
ret = setup_clkevents(&tc, clk32k_divisor_idx);
414442
if (ret)
415443
goto err_unregister_clksrc;
416444

@@ -420,14 +448,14 @@ static int __init tcb_clksrc_init(void)
420448
clocksource_unregister(&clksrc);
421449

422450
err_disable_t1:
423-
if (!tc->tcb_config || tc->tcb_config->counter_width != 32)
424-
clk_disable_unprepare(tc->clk[1]);
451+
if (bits != 32)
452+
clk_disable_unprepare(tc.clk[1]);
425453

426454
err_disable_t0:
427455
clk_disable_unprepare(t0_clk);
428456

429-
err_free_tc:
430-
atmel_tc_free(tc);
457+
tcaddr = NULL;
458+
431459
return ret;
432460
}
433-
arch_initcall(tcb_clksrc_init);
461+
TIMER_OF_DECLARE(atmel_tcb_clksrc, "atmel,tcb-timer", tcb_clksrc_init);

‎drivers/misc/Kconfig‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ config ATMEL_TCLIB
6161

6262
config ATMEL_TCB_CLKSRC
6363
bool "TC Block Clocksource"
64-
depends on ATMEL_TCLIB
64+
depends on ARCH_AT91
65+
select TIMER_OF if OF
6566
default y
6667
help
6768
Select this to get a high precision clocksource based on a
@@ -72,17 +73,6 @@ config ATMEL_TCB_CLKSRC
7273
may be used as a clock event device supporting oneshot mode
7374
(delays of up to two seconds) based on the 32 KiHz clock.
7475

75-
config ATMEL_TCB_CLKSRC_BLOCK
76-
int
77-
depends on ATMEL_TCB_CLKSRC
78-
default 0
79-
range 0 1
80-
help
81-
Some chips provide more than one TC block, so you have the
82-
choice of which one to use for the clock framework. The other
83-
TC can be used for other purposes, such as PWM generation and
84-
interval timing.
85-
8676
config DUMMY_IRQ
8777
tristate "Dummy IRQ handler"
8878
default n

0 commit comments

Comments
 (0)