Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Microchip Corporation |
Wolfgang Denk | 0cf207e | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 4 | * Wenyou.Yang <wenyou.yang@microchip.com> |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <timer.h> |
| 11 | #include <asm/io.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 13 | |
| 14 | #define AT91_PIT_VALUE 0xfffff |
| 15 | #define AT91_PIT_PITEN BIT(24) /* Timer Enabled */ |
| 16 | |
| 17 | struct atmel_pit_regs { |
| 18 | u32 mode; |
| 19 | u32 status; |
| 20 | u32 value; |
| 21 | u32 value_image; |
| 22 | }; |
| 23 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 24 | struct atmel_pit_plat { |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 25 | struct atmel_pit_regs *regs; |
| 26 | }; |
| 27 | |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 28 | static u64 atmel_pit_get_count(struct udevice *dev) |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 29 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 30 | struct atmel_pit_plat *plat = dev_get_plat(dev); |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 31 | struct atmel_pit_regs *const regs = plat->regs; |
| 32 | u32 val = readl(®s->value_image); |
| 33 | |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 34 | return timer_conv_64(val); |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static int atmel_pit_probe(struct udevice *dev) |
| 38 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 39 | struct atmel_pit_plat *plat = dev_get_plat(dev); |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 40 | struct atmel_pit_regs *const regs = plat->regs; |
| 41 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 42 | struct clk clk; |
| 43 | ulong clk_rate; |
| 44 | int ret; |
| 45 | |
| 46 | ret = clk_get_by_index(dev, 0, &clk); |
| 47 | if (ret) |
| 48 | return -EINVAL; |
| 49 | |
| 50 | clk_rate = clk_get_rate(&clk); |
| 51 | if (!clk_rate) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | uc_priv->clock_rate = clk_rate / 16; |
| 55 | |
| 56 | writel(AT91_PIT_VALUE | AT91_PIT_PITEN, ®s->mode); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 61 | static int atmel_pit_of_to_plat(struct udevice *dev) |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 62 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 63 | struct atmel_pit_plat *plat = dev_get_plat(dev); |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 64 | |
Masahiro Yamada | 702e57e | 2020-08-04 14:14:43 +0900 | [diff] [blame] | 65 | plat->regs = dev_read_addr_ptr(dev); |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static const struct timer_ops atmel_pit_ops = { |
| 71 | .get_count = atmel_pit_get_count, |
| 72 | }; |
| 73 | |
| 74 | static const struct udevice_id atmel_pit_ids[] = { |
| 75 | { .compatible = "atmel,at91sam9260-pit" }, |
| 76 | { } |
| 77 | }; |
| 78 | |
| 79 | U_BOOT_DRIVER(atmel_pit) = { |
| 80 | .name = "atmel_pit", |
| 81 | .id = UCLASS_TIMER, |
| 82 | .of_match = atmel_pit_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 83 | .of_to_plat = atmel_pit_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 84 | .plat_auto = sizeof(struct atmel_pit_plat), |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 85 | .probe = atmel_pit_probe, |
| 86 | .ops = &atmel_pit_ops, |
Wenyou.Yang@microchip.com | 47edaea | 2017-08-15 17:40:26 +0800 | [diff] [blame] | 87 | }; |