summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-at91/early_udelay.c
blob: 632e797bebe9fbd69aa27b8a6e240471d0f0476e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-License-Identifier: BSD-1-Clause
/*
 * Copyright (c) 2012, Atmel Corporation
 */

#include <mach/hardware.h>
#include <asm/io.h>
#include <mach/at91_pmc_ll.h>
#include <mach/at91_pit.h>
#include <mach/early_udelay.h>

static unsigned int master_clock;
static void __iomem *pmc, *pit;
static bool has_h32mxdiv;

/* Because the below statement is used in the function:
 *	((MASTER_CLOCK >> 10) * usec) is used,
 * to our 32-bit system. the argu "usec" maximum value is:
 * supposed "MASTER_CLOCK" is 132M.
 *	132000000 / 1024 = 128906
 *	(0xffffffff) / 128906 = 33318.
 * So the maximum delay time is 33318 us.
 */
/* requires PIT to be initialized, but not the clocksource framework */
void early_udelay(unsigned int usec)
{
	unsigned int delay;
	unsigned int current;
	unsigned int base = readl(pit + AT91_PIT_PIIR);

	if (has_h32mxdiv)
		master_clock /= 2;

	delay = ((master_clock >> 10) * usec) >> 14;

	do {
		current = readl(pit + AT91_PIT_PIIR);
		current -= base;
	} while (current < delay);
}

void early_udelay_init(void __iomem *pmc_base,
		       void __iomem *pit_base,
		       unsigned int clock,
		       unsigned int master_clock_rate,
		       unsigned int flags)
{
	master_clock = master_clock_rate;
	pmc = pmc_base;
	pit = pit_base;
	has_h32mxdiv = at91_pmc_check_mck_h32mxdiv(pmc, flags);

	writel(AT91_PIT_PIV | AT91_PIT_PITEN, pit + AT91_PIT_MR);

	at91_pmc_enable_periph_clock(pmc_base, clock);
}