summaryrefslogtreecommitdiffstats
path: root/drivers/clocksource/digic.c
blob: 1ecd83916000d9e246e4d371389155fa7f72ae44 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (C) 2013, 2014 Antony Pavlov <antonynpavlov@gmail.com>
 *
 * This file is part of barebox.
 * See file CREDITS for list of people who contributed to this project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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.
 *
 */

#include <common.h>
#include <io.h>
#include <init.h>
#include <clock.h>
#include <linux/err.h>

#define DIGIC_TIMER_CLOCK 1000000

#define DIGIC_TIMER_CONTROL 0x00
#define DIGIC_TIMER_VALUE 0x0c

static void __iomem *timer_base;

static uint64_t digic_cs_read(void)
{
	return (uint64_t)(0xffff - readl(timer_base + DIGIC_TIMER_VALUE));
}

static struct clocksource digic_cs = {
	.read	= digic_cs_read,
	.mask   = CLOCKSOURCE_MASK(16),
};

static int digic_timer_probe(struct device_d *dev)
{
	struct resource *iores;
	/* use only one timer */
	if (timer_base)
		return -EBUSY;

	iores = dev_request_mem_resource(dev, 0);
	if (IS_ERR(iores)) {
		dev_err(dev, "could not get memory region\n");
		return PTR_ERR(iores);
	}
	timer_base = IOMEM(iores->start);

	clocks_calc_mult_shift(&digic_cs.mult, &digic_cs.shift,
		DIGIC_TIMER_CLOCK, NSEC_PER_SEC, 1);

	/* disable timer */
	writel(0x80000000, timer_base + DIGIC_TIMER_CONTROL);

	/* magic values... divider? */
	writel(0x00000002, timer_base + 0x04);
	writel(0x00000003, timer_base + 0x14);

	/* max counter value */
	writel(0x0000ffff, timer_base + 0x08);

	init_clock(&digic_cs);

	/* enable timer */
	writel(0x00000001, timer_base + DIGIC_TIMER_CONTROL);
	/* start timer */
	writel(0x00000001, timer_base + 0x10);

	return 0;
}

static __maybe_unused struct of_device_id digic_timer_dt_ids[] = {
	{
		.compatible = "canon,digic-timer",
	}, {
		/* sentinel */
	}
};

static struct driver_d digic_timer_driver = {
	.probe	= digic_timer_probe,
	.name	= "digic-timer",
	.of_compatible = DRV_OF_COMPAT(digic_timer_dt_ids),
};

static int digic_timer_init(void)
{
	return platform_driver_register(&digic_timer_driver);
}
coredevice_initcall(digic_timer_init);