Marek Vasut | 4d0732b | 2019-05-04 17:30:58 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Renesas RZ/A1 R7S72100 OSTM Timer driver |
| 4 | * |
| 5 | * Copyright (C) 2019 Marek Vasut <marek.vasut@gmail.com> |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Marek Vasut | 4d0732b | 2019-05-04 17:30:58 +0200 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <dm.h> |
| 12 | #include <clk.h> |
| 13 | #include <timer.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Marek Vasut | 4d0732b | 2019-05-04 17:30:58 +0200 | [diff] [blame] | 15 | |
| 16 | #define OSTM_CMP 0x00 |
| 17 | #define OSTM_CNT 0x04 |
| 18 | #define OSTM_TE 0x10 |
| 19 | #define OSTM_TS 0x14 |
| 20 | #define OSTM_TT 0x18 |
| 21 | #define OSTM_CTL 0x20 |
| 22 | #define OSTM_CTL_D BIT(1) |
| 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
| 26 | struct ostm_priv { |
| 27 | fdt_addr_t regs; |
| 28 | }; |
| 29 | |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 30 | static u64 ostm_get_count(struct udevice *dev) |
Marek Vasut | 4d0732b | 2019-05-04 17:30:58 +0200 | [diff] [blame] | 31 | { |
| 32 | struct ostm_priv *priv = dev_get_priv(dev); |
| 33 | |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 34 | return timer_conv_64(readl(priv->regs + OSTM_CNT)); |
Marek Vasut | 4d0732b | 2019-05-04 17:30:58 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static int ostm_probe(struct udevice *dev) |
| 38 | { |
| 39 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 40 | struct ostm_priv *priv = dev_get_priv(dev); |
| 41 | #if CONFIG_IS_ENABLED(CLK) |
| 42 | struct clk clk; |
| 43 | int ret; |
| 44 | |
| 45 | ret = clk_get_by_index(dev, 0, &clk); |
| 46 | if (ret) |
| 47 | return ret; |
| 48 | |
| 49 | uc_priv->clock_rate = clk_get_rate(&clk); |
| 50 | |
| 51 | clk_free(&clk); |
| 52 | #else |
| 53 | uc_priv->clock_rate = CONFIG_SYS_CLK_FREQ / 2; |
| 54 | #endif |
| 55 | |
| 56 | readb(priv->regs + OSTM_CTL); |
| 57 | writeb(OSTM_CTL_D, priv->regs + OSTM_CTL); |
| 58 | |
| 59 | setbits_8(priv->regs + OSTM_TT, BIT(0)); |
| 60 | writel(0xffffffff, priv->regs + OSTM_CMP); |
| 61 | setbits_8(priv->regs + OSTM_TS, BIT(0)); |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 66 | static int ostm_of_to_plat(struct udevice *dev) |
Marek Vasut | 4d0732b | 2019-05-04 17:30:58 +0200 | [diff] [blame] | 67 | { |
| 68 | struct ostm_priv *priv = dev_get_priv(dev); |
| 69 | |
| 70 | priv->regs = dev_read_addr(dev); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static const struct timer_ops ostm_ops = { |
| 76 | .get_count = ostm_get_count, |
| 77 | }; |
| 78 | |
| 79 | static const struct udevice_id ostm_ids[] = { |
| 80 | { .compatible = "renesas,ostm" }, |
| 81 | {} |
| 82 | }; |
| 83 | |
| 84 | U_BOOT_DRIVER(ostm_timer) = { |
| 85 | .name = "ostm-timer", |
| 86 | .id = UCLASS_TIMER, |
| 87 | .ops = &ostm_ops, |
| 88 | .probe = ostm_probe, |
| 89 | .of_match = ostm_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 90 | .of_to_plat = ostm_of_to_plat, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 91 | .priv_auto = sizeof(struct ostm_priv), |
Marek Vasut | 4d0732b | 2019-05-04 17:30:58 +0200 | [diff] [blame] | 92 | }; |