Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Patrice Chotard | fdfefdc | 2018-02-07 10:44:45 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018, STMicroelectronics - All Rights Reserved |
Patrice Chotard | 5d9950d | 2020-12-02 18:47:30 +0100 | [diff] [blame] | 4 | * Author(s): Patrice Chotard, <patrice.chotard@foss.st.com> for STMicroelectronics. |
Patrice Chotard | fdfefdc | 2018-02-07 10:44:45 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <timer.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Patrice Chotard | fdfefdc | 2018-02-07 10:44:45 +0100 | [diff] [blame] | 14 | |
| 15 | #include <asm/io.h> |
| 16 | |
| 17 | /* Timer control1 register */ |
| 18 | #define CR1_CEN BIT(0) |
| 19 | #define CR1_ARPE BIT(7) |
| 20 | |
| 21 | /* Event Generation Register register */ |
| 22 | #define EGR_UG BIT(0) |
| 23 | |
| 24 | /* Auto reload register for free running config */ |
| 25 | #define GPT_FREE_RUNNING 0xFFFFFFFF |
| 26 | |
| 27 | struct stm32_timer_regs { |
| 28 | u32 cr1; |
| 29 | u32 cr2; |
| 30 | u32 smcr; |
| 31 | u32 dier; |
| 32 | u32 sr; |
| 33 | u32 egr; |
| 34 | u32 ccmr1; |
| 35 | u32 ccmr2; |
| 36 | u32 ccer; |
| 37 | u32 cnt; |
| 38 | u32 psc; |
| 39 | u32 arr; |
| 40 | u32 reserved; |
| 41 | u32 ccr1; |
| 42 | u32 ccr2; |
| 43 | u32 ccr3; |
| 44 | u32 ccr4; |
| 45 | u32 reserved1; |
| 46 | u32 dcr; |
| 47 | u32 dmar; |
| 48 | u32 tim2_5_or; |
| 49 | }; |
| 50 | |
| 51 | struct stm32_timer_priv { |
| 52 | struct stm32_timer_regs *base; |
| 53 | }; |
| 54 | |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 55 | static u64 stm32_timer_get_count(struct udevice *dev) |
Patrice Chotard | fdfefdc | 2018-02-07 10:44:45 +0100 | [diff] [blame] | 56 | { |
| 57 | struct stm32_timer_priv *priv = dev_get_priv(dev); |
| 58 | struct stm32_timer_regs *regs = priv->base; |
| 59 | |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 60 | return readl(®s->cnt); |
Patrice Chotard | fdfefdc | 2018-02-07 10:44:45 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static int stm32_timer_probe(struct udevice *dev) |
| 64 | { |
| 65 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 66 | struct stm32_timer_priv *priv = dev_get_priv(dev); |
| 67 | struct stm32_timer_regs *regs; |
| 68 | struct clk clk; |
| 69 | fdt_addr_t addr; |
| 70 | int ret; |
| 71 | u32 rate, psc; |
| 72 | |
| 73 | addr = dev_read_addr(dev); |
| 74 | if (addr == FDT_ADDR_T_NONE) |
| 75 | return -EINVAL; |
| 76 | |
| 77 | priv->base = (struct stm32_timer_regs *)addr; |
| 78 | |
| 79 | ret = clk_get_by_index(dev, 0, &clk); |
| 80 | if (ret < 0) |
| 81 | return ret; |
| 82 | |
| 83 | ret = clk_enable(&clk); |
| 84 | if (ret) { |
| 85 | dev_err(dev, "failed to enable clock\n"); |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | regs = priv->base; |
| 90 | |
| 91 | /* Stop the timer */ |
| 92 | clrbits_le32(®s->cr1, CR1_CEN); |
| 93 | |
| 94 | /* get timer clock */ |
| 95 | rate = clk_get_rate(&clk); |
| 96 | |
| 97 | /* we set timer prescaler to obtain a 1MHz timer counter frequency */ |
| 98 | psc = (rate / CONFIG_SYS_HZ_CLOCK) - 1; |
| 99 | writel(psc, ®s->psc); |
| 100 | |
| 101 | /* Set timer frequency to 1MHz */ |
| 102 | uc_priv->clock_rate = CONFIG_SYS_HZ_CLOCK; |
| 103 | |
| 104 | /* Configure timer for auto-reload */ |
| 105 | setbits_le32(®s->cr1, CR1_ARPE); |
| 106 | |
| 107 | /* load value for auto reload */ |
| 108 | writel(GPT_FREE_RUNNING, ®s->arr); |
| 109 | |
| 110 | /* start timer */ |
| 111 | setbits_le32(®s->cr1, CR1_CEN); |
| 112 | |
| 113 | /* Update generation */ |
| 114 | setbits_le32(®s->egr, EGR_UG); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static const struct timer_ops stm32_timer_ops = { |
| 120 | .get_count = stm32_timer_get_count, |
| 121 | }; |
| 122 | |
| 123 | static const struct udevice_id stm32_timer_ids[] = { |
| 124 | { .compatible = "st,stm32-timer" }, |
| 125 | {} |
| 126 | }; |
| 127 | |
| 128 | U_BOOT_DRIVER(stm32_timer) = { |
| 129 | .name = "stm32_timer", |
| 130 | .id = UCLASS_TIMER, |
| 131 | .of_match = stm32_timer_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 132 | .priv_auto = sizeof(struct stm32_timer_priv), |
Patrice Chotard | fdfefdc | 2018-02-07 10:44:45 +0100 | [diff] [blame] | 133 | .probe = stm32_timer_probe, |
| 134 | .ops = &stm32_timer_ops, |
Patrice Chotard | fdfefdc | 2018-02-07 10:44:45 +0100 | [diff] [blame] | 135 | }; |
| 136 | |