Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mugunthan V N | dadf313 | 2015-12-24 16:08:07 +0530 | [diff] [blame] | 2 | /* |
| 3 | * TI OMAP timer driver |
| 4 | * |
| 5 | * Copyright (C) 2015, Texas Instruments, Incorporated |
Mugunthan V N | dadf313 | 2015-12-24 16:08:07 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <timer.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <asm/arch/clock.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Mugunthan V N | dadf313 | 2015-12-24 16:08:07 +0530 | [diff] [blame] | 15 | |
Mugunthan V N | dadf313 | 2015-12-24 16:08:07 +0530 | [diff] [blame] | 16 | /* Timer register bits */ |
| 17 | #define TCLR_START BIT(0) /* Start=1 */ |
| 18 | #define TCLR_AUTO_RELOAD BIT(1) /* Auto reload */ |
| 19 | #define TCLR_PRE_EN BIT(5) /* Pre-scaler enable */ |
| 20 | #define TCLR_PTV_SHIFT (2) /* Pre-scaler shift value */ |
| 21 | |
| 22 | #define TIMER_CLOCK (V_SCLK / (2 << CONFIG_SYS_PTV)) |
| 23 | |
| 24 | struct omap_gptimer_regs { |
| 25 | unsigned int tidr; /* offset 0x00 */ |
| 26 | unsigned char res1[12]; |
| 27 | unsigned int tiocp_cfg; /* offset 0x10 */ |
| 28 | unsigned char res2[12]; |
| 29 | unsigned int tier; /* offset 0x20 */ |
| 30 | unsigned int tistatr; /* offset 0x24 */ |
| 31 | unsigned int tistat; /* offset 0x28 */ |
| 32 | unsigned int tisr; /* offset 0x2c */ |
| 33 | unsigned int tcicr; /* offset 0x30 */ |
| 34 | unsigned int twer; /* offset 0x34 */ |
| 35 | unsigned int tclr; /* offset 0x38 */ |
| 36 | unsigned int tcrr; /* offset 0x3c */ |
| 37 | unsigned int tldr; /* offset 0x40 */ |
| 38 | unsigned int ttgr; /* offset 0x44 */ |
| 39 | unsigned int twpc; /* offset 0x48 */ |
| 40 | unsigned int tmar; /* offset 0x4c */ |
| 41 | unsigned int tcar1; /* offset 0x50 */ |
| 42 | unsigned int tscir; /* offset 0x54 */ |
| 43 | unsigned int tcar2; /* offset 0x58 */ |
| 44 | }; |
| 45 | |
| 46 | /* Omap Timer Priv */ |
| 47 | struct omap_timer_priv { |
| 48 | struct omap_gptimer_regs *regs; |
| 49 | }; |
| 50 | |
| 51 | static int omap_timer_get_count(struct udevice *dev, u64 *count) |
| 52 | { |
| 53 | struct omap_timer_priv *priv = dev_get_priv(dev); |
| 54 | |
Lokesh Vutla | 3ee15a5 | 2018-08-16 18:26:54 +0530 | [diff] [blame] | 55 | *count = timer_conv_64(readl(&priv->regs->tcrr)); |
Mugunthan V N | dadf313 | 2015-12-24 16:08:07 +0530 | [diff] [blame] | 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int omap_timer_probe(struct udevice *dev) |
| 61 | { |
| 62 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 63 | struct omap_timer_priv *priv = dev_get_priv(dev); |
| 64 | |
Lokesh Vutla | 84b4221 | 2018-08-16 18:26:55 +0530 | [diff] [blame] | 65 | if (!uc_priv->clock_rate) |
| 66 | uc_priv->clock_rate = TIMER_CLOCK; |
Mugunthan V N | dadf313 | 2015-12-24 16:08:07 +0530 | [diff] [blame] | 67 | |
| 68 | /* start the counter ticking up, reload value on overflow */ |
| 69 | writel(0, &priv->regs->tldr); |
Lokesh Vutla | 3ee15a5 | 2018-08-16 18:26:54 +0530 | [diff] [blame] | 70 | writel(0, &priv->regs->tcrr); |
Mugunthan V N | dadf313 | 2015-12-24 16:08:07 +0530 | [diff] [blame] | 71 | /* enable timer */ |
| 72 | writel((CONFIG_SYS_PTV << 2) | TCLR_PRE_EN | TCLR_AUTO_RELOAD | |
| 73 | TCLR_START, &priv->regs->tclr); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int omap_timer_ofdata_to_platdata(struct udevice *dev) |
| 79 | { |
| 80 | struct omap_timer_priv *priv = dev_get_priv(dev); |
| 81 | |
Tom Rini | 7208396 | 2020-07-24 08:42:06 -0400 | [diff] [blame^] | 82 | priv->regs = map_physmem(devfdt_get_addr(dev), |
Lokesh Vutla | 871ca26 | 2016-03-05 16:40:32 +0530 | [diff] [blame] | 83 | sizeof(struct omap_gptimer_regs), MAP_NOCACHE); |
Mugunthan V N | dadf313 | 2015-12-24 16:08:07 +0530 | [diff] [blame] | 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | static const struct timer_ops omap_timer_ops = { |
| 90 | .get_count = omap_timer_get_count, |
| 91 | }; |
| 92 | |
| 93 | static const struct udevice_id omap_timer_ids[] = { |
| 94 | { .compatible = "ti,am335x-timer" }, |
| 95 | { .compatible = "ti,am4372-timer" }, |
| 96 | { .compatible = "ti,omap5430-timer" }, |
| 97 | {} |
| 98 | }; |
| 99 | |
| 100 | U_BOOT_DRIVER(omap_timer) = { |
| 101 | .name = "omap_timer", |
| 102 | .id = UCLASS_TIMER, |
| 103 | .of_match = omap_timer_ids, |
| 104 | .ofdata_to_platdata = omap_timer_ofdata_to_platdata, |
| 105 | .priv_auto_alloc_size = sizeof(struct omap_timer_priv), |
| 106 | .probe = omap_timer_probe, |
| 107 | .ops = &omap_timer_ops, |
Mugunthan V N | dadf313 | 2015-12-24 16:08:07 +0530 | [diff] [blame] | 108 | }; |