Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Designware APB Timer driver |
| 4 | * |
| 5 | * Copyright (C) 2018 Marek Vasut <marex@denx.de> |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <clk.h> |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 11 | #include <dt-structs.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Goldschmidt | caaaf62 | 2019-10-23 22:23:12 +0200 | [diff] [blame] | 13 | #include <reset.h> |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 14 | #include <timer.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 16 | |
| 17 | #include <asm/io.h> |
| 18 | #include <asm/arch/timer.h> |
| 19 | |
| 20 | #define DW_APB_LOAD_VAL 0x0 |
| 21 | #define DW_APB_CURR_VAL 0x4 |
| 22 | #define DW_APB_CTRL 0x8 |
| 23 | |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 24 | struct dw_apb_timer_priv { |
Johan Jonker | e9eddb7 | 2023-03-13 01:29:47 +0100 | [diff] [blame] | 25 | uintptr_t regs; |
Simon Goldschmidt | caaaf62 | 2019-10-23 22:23:12 +0200 | [diff] [blame] | 26 | struct reset_ctl_bulk resets; |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 29 | struct dw_apb_timer_plat { |
| 30 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 31 | struct dtd_snps_dw_apb_timer dtplat; |
| 32 | #endif |
| 33 | }; |
| 34 | |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 35 | static u64 dw_apb_timer_get_count(struct udevice *dev) |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 36 | { |
| 37 | struct dw_apb_timer_priv *priv = dev_get_priv(dev); |
| 38 | |
| 39 | /* |
| 40 | * The DW APB counter counts down, but this function |
| 41 | * requires the count to be incrementing. Invert the |
| 42 | * result. |
| 43 | */ |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 44 | return timer_conv_64(~readl(priv->regs + DW_APB_CURR_VAL)); |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static int dw_apb_timer_probe(struct udevice *dev) |
| 48 | { |
| 49 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 50 | struct dw_apb_timer_priv *priv = dev_get_priv(dev); |
| 51 | struct clk clk; |
| 52 | int ret; |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 53 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 54 | struct dw_apb_timer_plat *plat = dev_get_plat(dev); |
| 55 | struct dtd_snps_dw_apb_timer *dtplat = &plat->dtplat; |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 56 | |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 57 | priv->regs = dtplat->reg[0]; |
Simon Goldschmidt | caaaf62 | 2019-10-23 22:23:12 +0200 | [diff] [blame] | 58 | |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 59 | ret = clk_get_by_phandle(dev, &dtplat->clocks[0], &clk); |
| 60 | if (ret < 0) |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 61 | return ret; |
| 62 | |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 63 | uc_priv->clock_rate = dtplat->clock_frequency; |
| 64 | #endif |
| 65 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
| 66 | ret = reset_get_bulk(dev, &priv->resets); |
| 67 | if (ret) |
| 68 | dev_warn(dev, "Can't get reset: %d\n", ret); |
| 69 | else |
| 70 | reset_deassert_bulk(&priv->resets); |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 71 | |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 72 | ret = clk_get_by_index(dev, 0, &clk); |
| 73 | if (ret) |
| 74 | return ret; |
| 75 | |
| 76 | uc_priv->clock_rate = clk_get_rate(&clk); |
| 77 | |
| 78 | clk_free(&clk); |
| 79 | } |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 80 | |
| 81 | /* init timer */ |
| 82 | writel(0xffffffff, priv->regs + DW_APB_LOAD_VAL); |
| 83 | writel(0xffffffff, priv->regs + DW_APB_CURR_VAL); |
| 84 | setbits_le32(priv->regs + DW_APB_CTRL, 0x3); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 89 | static int dw_apb_timer_of_to_plat(struct udevice *dev) |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 90 | { |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 91 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
| 92 | struct dw_apb_timer_priv *priv = dev_get_priv(dev); |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 93 | |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 94 | priv->regs = dev_read_addr(dev); |
| 95 | } |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Simon Goldschmidt | caaaf62 | 2019-10-23 22:23:12 +0200 | [diff] [blame] | 100 | static int dw_apb_timer_remove(struct udevice *dev) |
| 101 | { |
| 102 | struct dw_apb_timer_priv *priv = dev_get_priv(dev); |
| 103 | |
| 104 | return reset_release_bulk(&priv->resets); |
| 105 | } |
| 106 | |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 107 | static const struct timer_ops dw_apb_timer_ops = { |
| 108 | .get_count = dw_apb_timer_get_count, |
| 109 | }; |
| 110 | |
| 111 | static const struct udevice_id dw_apb_timer_ids[] = { |
| 112 | { .compatible = "snps,dw-apb-timer" }, |
| 113 | {} |
| 114 | }; |
| 115 | |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 116 | U_BOOT_DRIVER(snps_dw_apb_timer) = { |
| 117 | .name = "snps_dw_apb_timer", |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 118 | .id = UCLASS_TIMER, |
| 119 | .ops = &dw_apb_timer_ops, |
| 120 | .probe = dw_apb_timer_probe, |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 121 | .of_match = dw_apb_timer_ids, |
Johan Jonker | 3930209 | 2022-04-09 18:55:07 +0200 | [diff] [blame] | 122 | .of_to_plat = dw_apb_timer_of_to_plat, |
Simon Goldschmidt | caaaf62 | 2019-10-23 22:23:12 +0200 | [diff] [blame] | 123 | .remove = dw_apb_timer_remove, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 124 | .priv_auto = sizeof(struct dw_apb_timer_priv), |
Johan Jonker | 516e216 | 2022-04-09 18:55:06 +0200 | [diff] [blame] | 125 | .plat_auto = sizeof(struct dw_apb_timer_plat), |
Marek Vasut | 66011a0 | 2018-08-18 15:58:32 +0200 | [diff] [blame] | 126 | }; |