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