blob: 0a367a5a7f4ae8e2d191d8d4fcbc8f066ed63812 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +08002/*
3 * Copyright (C) 2017 Microchip Corporation
Wolfgang Denk62fb2b42021-09-27 17:42:39 +02004 * Wenyou.Yang <wenyou.yang@microchip.com>
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +08005 */
6
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +08007#include <clk.h>
8#include <dm.h>
9#include <timer.h>
10#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080012
13#define AT91_PIT_VALUE 0xfffff
14#define AT91_PIT_PITEN BIT(24) /* Timer Enabled */
15
16struct atmel_pit_regs {
17 u32 mode;
18 u32 status;
19 u32 value;
20 u32 value_image;
21};
22
Simon Glassb75b15b2020-12-03 16:55:23 -070023struct atmel_pit_plat {
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080024 struct atmel_pit_regs *regs;
25};
26
Sean Anderson947fc2d2020-10-07 14:37:44 -040027static u64 atmel_pit_get_count(struct udevice *dev)
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080028{
Simon Glassb75b15b2020-12-03 16:55:23 -070029 struct atmel_pit_plat *plat = dev_get_plat(dev);
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080030 struct atmel_pit_regs *const regs = plat->regs;
31 u32 val = readl(&regs->value_image);
32
Sean Anderson947fc2d2020-10-07 14:37:44 -040033 return timer_conv_64(val);
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080034}
35
36static int atmel_pit_probe(struct udevice *dev)
37{
Simon Glassb75b15b2020-12-03 16:55:23 -070038 struct atmel_pit_plat *plat = dev_get_plat(dev);
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080039 struct atmel_pit_regs *const regs = plat->regs;
40 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
41 struct clk clk;
42 ulong clk_rate;
43 int ret;
44
45 ret = clk_get_by_index(dev, 0, &clk);
46 if (ret)
47 return -EINVAL;
48
49 clk_rate = clk_get_rate(&clk);
50 if (!clk_rate)
51 return -EINVAL;
52
53 uc_priv->clock_rate = clk_rate / 16;
54
55 writel(AT91_PIT_VALUE | AT91_PIT_PITEN, &regs->mode);
56
57 return 0;
58}
59
Simon Glassaad29ae2020-12-03 16:55:21 -070060static int atmel_pit_of_to_plat(struct udevice *dev)
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080061{
Simon Glassb75b15b2020-12-03 16:55:23 -070062 struct atmel_pit_plat *plat = dev_get_plat(dev);
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080063
Masahiro Yamada32822d02020-08-04 14:14:43 +090064 plat->regs = dev_read_addr_ptr(dev);
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080065
66 return 0;
67}
68
69static const struct timer_ops atmel_pit_ops = {
70 .get_count = atmel_pit_get_count,
71};
72
73static const struct udevice_id atmel_pit_ids[] = {
74 { .compatible = "atmel,at91sam9260-pit" },
75 { }
76};
77
78U_BOOT_DRIVER(atmel_pit) = {
79 .name = "atmel_pit",
80 .id = UCLASS_TIMER,
81 .of_match = atmel_pit_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -070082 .of_to_plat = atmel_pit_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -070083 .plat_auto = sizeof(struct atmel_pit_plat),
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080084 .probe = atmel_pit_probe,
85 .ops = &atmel_pit_ops,
Wenyou.Yang@microchip.comce8a36f2017-08-15 17:40:26 +080086};