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