Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2002 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
| 6 | * (C) Copyright 2004, Psyent Corporation <www.psyent.com> |
| 7 | * Scott McNutt <smcnutt@psyent.com> |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 8 | */ |
| 9 | |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <errno.h> |
| 12 | #include <timer.h> |
| 13 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 15 | |
Thomas Chou | 90b1d79 | 2015-10-31 20:54:16 +0800 | [diff] [blame] | 16 | /* control register */ |
| 17 | #define ALTERA_TIMER_CONT BIT(1) /* Continuous mode */ |
| 18 | #define ALTERA_TIMER_START BIT(2) /* Start timer */ |
| 19 | #define ALTERA_TIMER_STOP BIT(3) /* Stop timer */ |
| 20 | |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 21 | struct altera_timer_regs { |
| 22 | u32 status; /* Timer status reg */ |
| 23 | u32 control; /* Timer control reg */ |
| 24 | u32 periodl; /* Timeout period low */ |
| 25 | u32 periodh; /* Timeout period high */ |
| 26 | u32 snapl; /* Snapshot low */ |
| 27 | u32 snaph; /* Snapshot high */ |
| 28 | }; |
| 29 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 30 | struct altera_timer_plat { |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 31 | struct altera_timer_regs *regs; |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 32 | }; |
| 33 | |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 34 | static u64 altera_timer_get_count(struct udevice *dev) |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 35 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 36 | struct altera_timer_plat *plat = dev_get_plat(dev); |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 37 | struct altera_timer_regs *const regs = plat->regs; |
| 38 | u32 val; |
| 39 | |
| 40 | /* Trigger update */ |
| 41 | writel(0x0, ®s->snapl); |
| 42 | |
| 43 | /* Read timer value */ |
| 44 | val = readl(®s->snapl) & 0xffff; |
| 45 | val |= (readl(®s->snaph) & 0xffff) << 16; |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 46 | return timer_conv_64(~val); |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int altera_timer_probe(struct udevice *dev) |
| 50 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 51 | struct altera_timer_plat *plat = dev_get_plat(dev); |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 52 | struct altera_timer_regs *const regs = plat->regs; |
| 53 | |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 54 | writel(0, ®s->status); |
| 55 | writel(0, ®s->control); |
| 56 | writel(ALTERA_TIMER_STOP, ®s->control); |
| 57 | |
| 58 | writel(0xffff, ®s->periodl); |
| 59 | writel(0xffff, ®s->periodh); |
| 60 | writel(ALTERA_TIMER_CONT | ALTERA_TIMER_START, ®s->control); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 65 | static int altera_timer_of_to_plat(struct udevice *dev) |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 66 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 67 | struct altera_timer_plat *plat = dev_get_plat(dev); |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 68 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 69 | plat->regs = map_physmem(dev_read_addr(dev), |
Thomas Chou | d82a4d3 | 2015-11-14 11:15:31 +0800 | [diff] [blame] | 70 | sizeof(struct altera_timer_regs), |
| 71 | MAP_NOCACHE); |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static const struct timer_ops altera_timer_ops = { |
| 77 | .get_count = altera_timer_get_count, |
| 78 | }; |
| 79 | |
| 80 | static const struct udevice_id altera_timer_ids[] = { |
Thomas Chou | 90b1d79 | 2015-10-31 20:54:16 +0800 | [diff] [blame] | 81 | { .compatible = "altr,timer-1.0" }, |
| 82 | {} |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | U_BOOT_DRIVER(altera_timer) = { |
| 86 | .name = "altera_timer", |
| 87 | .id = UCLASS_TIMER, |
| 88 | .of_match = altera_timer_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 89 | .of_to_plat = altera_timer_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 90 | .plat_auto = sizeof(struct altera_timer_plat), |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 91 | .probe = altera_timer_probe, |
| 92 | .ops = &altera_timer_ops, |
Thomas Chou | 221d2ac | 2015-10-22 22:28:53 +0800 | [diff] [blame] | 93 | }; |