Stephan Gerhold | 057b613 | 2020-01-04 18:45:15 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2019 Stephan Gerhold <stephan@gerhold.net> |
| 4 | * |
| 5 | * Based on arch/arm/cpu/armv7/u8500/timer.c: |
| 6 | * Copyright (C) 2010 Linaro Limited |
| 7 | * John Rigby <john.rigby@linaro.org> |
| 8 | * |
| 9 | * Based on Linux kernel source and internal ST-Ericsson U-Boot source: |
| 10 | * Copyright (C) 2009 Alessandro Rubini |
| 11 | * Copyright (C) 2010 ST-Ericsson |
| 12 | * Copyright (C) 2010 Linus Walleij for ST-Ericsson |
| 13 | */ |
| 14 | |
| 15 | #include <common.h> |
| 16 | #include <dm.h> |
| 17 | #include <timer.h> |
| 18 | #include <asm/io.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame^] | 19 | #include <linux/bitops.h> |
Stephan Gerhold | 057b613 | 2020-01-04 18:45:15 +0100 | [diff] [blame] | 20 | |
| 21 | #define MTU_NUM_TIMERS 4 |
| 22 | |
| 23 | /* The timers */ |
| 24 | struct nomadik_mtu_timer_regs { |
| 25 | u32 lr; /* Load register */ |
| 26 | u32 cv; /* Current value */ |
| 27 | u32 cr; /* Control register */ |
| 28 | u32 bglr; /* Background load register */ |
| 29 | }; |
| 30 | |
| 31 | /* The MTU that contains the timers */ |
| 32 | struct nomadik_mtu_regs { |
| 33 | u32 imsc; /* Interrupt mask set/clear */ |
| 34 | u32 ris; /* Raw interrupt status */ |
| 35 | u32 mis; /* Masked interrupt status */ |
| 36 | u32 icr; /* Interrupt clear register */ |
| 37 | |
| 38 | struct nomadik_mtu_timer_regs timers[MTU_NUM_TIMERS]; |
| 39 | }; |
| 40 | |
| 41 | /* Bits for the control register */ |
| 42 | #define MTU_CR_ONESHOT BIT(0) /* if 0 = wraps reloading from BGLR */ |
| 43 | #define MTU_CR_32BITS BIT(1) /* if 0 = 16-bit counter */ |
| 44 | |
| 45 | #define MTU_CR_PRESCALE_SHIFT 2 |
| 46 | #define MTU_CR_PRESCALE_1 (0 << MTU_CR_PRESCALE_SHIFT) |
| 47 | #define MTU_CR_PRESCALE_16 (1 << MTU_CR_PRESCALE_SHIFT) |
| 48 | #define MTU_CR_PRESCALE_256 (2 << MTU_CR_PRESCALE_SHIFT) |
| 49 | |
| 50 | #define MTU_CR_PERIODIC BIT(6) /* if 0 = free-running */ |
| 51 | #define MTU_CR_ENABLE BIT(7) |
| 52 | |
| 53 | struct nomadik_mtu_priv { |
| 54 | struct nomadik_mtu_timer_regs *timer; |
| 55 | }; |
| 56 | |
| 57 | static int nomadik_mtu_get_count(struct udevice *dev, u64 *count) |
| 58 | { |
| 59 | struct nomadik_mtu_priv *priv = dev_get_priv(dev); |
| 60 | |
| 61 | /* Decrementing counter: invert the value */ |
| 62 | *count = timer_conv_64(~readl(&priv->timer->cv)); |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int nomadik_mtu_probe(struct udevice *dev) |
| 68 | { |
| 69 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 70 | struct nomadik_mtu_priv *priv = dev_get_priv(dev); |
| 71 | struct nomadik_mtu_regs *mtu; |
| 72 | fdt_addr_t addr; |
| 73 | u32 prescale; |
| 74 | |
| 75 | addr = dev_read_addr(dev); |
| 76 | if (addr == FDT_ADDR_T_NONE) |
| 77 | return -EINVAL; |
| 78 | |
| 79 | mtu = (struct nomadik_mtu_regs *)addr; |
| 80 | priv->timer = mtu->timers; /* Use first timer */ |
| 81 | |
| 82 | if (!uc_priv->clock_rate) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | /* Use divide-by-16 counter if tick rate is more than 32 MHz */ |
| 86 | if (uc_priv->clock_rate > 32000000) { |
| 87 | uc_priv->clock_rate /= 16; |
| 88 | prescale = MTU_CR_PRESCALE_16; |
| 89 | } else { |
| 90 | prescale = MTU_CR_PRESCALE_1; |
| 91 | } |
| 92 | |
| 93 | /* Configure a free-running, auto-wrap counter with selected prescale */ |
| 94 | writel(MTU_CR_ENABLE | prescale | MTU_CR_32BITS, &priv->timer->cr); |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static const struct timer_ops nomadik_mtu_ops = { |
| 100 | .get_count = nomadik_mtu_get_count, |
| 101 | }; |
| 102 | |
| 103 | static const struct udevice_id nomadik_mtu_ids[] = { |
| 104 | { .compatible = "st,nomadik-mtu" }, |
| 105 | {} |
| 106 | }; |
| 107 | |
| 108 | U_BOOT_DRIVER(nomadik_mtu) = { |
| 109 | .name = "nomadik_mtu", |
| 110 | .id = UCLASS_TIMER, |
| 111 | .of_match = nomadik_mtu_ids, |
| 112 | .priv_auto_alloc_size = sizeof(struct nomadik_mtu_priv), |
| 113 | .probe = nomadik_mtu_probe, |
| 114 | .ops = &nomadik_mtu_ops, |
| 115 | }; |