Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 2 | /* |
Stefan Herbrechtsmeier | 3576380 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 3 | * Copyright (C) 2017 Weidmüller Interface GmbH & Co. KG |
| 4 | * Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com> |
| 5 | * |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 6 | * Copyright (C) 2012 Michal Simek <monstr@monstr.eu> |
Michal Simek | 98d0f1f | 2018-01-17 07:37:47 +0100 | [diff] [blame] | 7 | * Copyright (C) 2011-2017 Xilinx, Inc. All rights reserved. |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 8 | * |
| 9 | * (C) Copyright 2008 |
| 10 | * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de> |
| 11 | * |
| 12 | * (C) Copyright 2004 |
| 13 | * Philippe Robin, ARM Ltd. <philippe.robin@arm.com> |
| 14 | * |
| 15 | * (C) Copyright 2002-2004 |
| 16 | * Gary Jennejohn, DENX Software Engineering, <gj@denx.de> |
| 17 | * |
| 18 | * (C) Copyright 2003 |
| 19 | * Texas Instruments <www.ti.com> |
| 20 | * |
| 21 | * (C) Copyright 2002 |
| 22 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 23 | * Marius Groeger <mgroeger@sysgo.de> |
| 24 | * |
| 25 | * (C) Copyright 2002 |
| 26 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 27 | * Alex Zuepke <azu@sysgo.de> |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Stefan Herbrechtsmeier | 3576380 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 30 | #include <clk.h> |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 31 | #include <common.h> |
| 32 | #include <div64.h> |
Stefan Herbrechtsmeier | 3576380 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 33 | #include <dm.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 34 | #include <init.h> |
Simon Glass | a9dc068 | 2019-12-28 10:44:59 -0700 | [diff] [blame] | 35 | #include <time.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 36 | #include <malloc.h> |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 37 | #include <asm/io.h> |
Michal Simek | ad2e2b7 | 2013-04-12 16:21:26 +0200 | [diff] [blame] | 38 | #include <asm/arch/hardware.h> |
Soren Brinkmann | 15fff9b | 2013-11-21 13:38:57 -0800 | [diff] [blame] | 39 | #include <asm/arch/clk.h> |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 40 | |
| 41 | DECLARE_GLOBAL_DATA_PTR; |
| 42 | |
| 43 | struct scu_timer { |
| 44 | u32 load; /* Timer Load Register */ |
| 45 | u32 counter; /* Timer Counter Register */ |
| 46 | u32 control; /* Timer Control Register */ |
| 47 | }; |
| 48 | |
| 49 | static struct scu_timer *timer_base = |
Michal Simek | ad2e2b7 | 2013-04-12 16:21:26 +0200 | [diff] [blame] | 50 | (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR; |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 51 | |
| 52 | #define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */ |
| 53 | #define SCUTIMER_CONTROL_PRESCALER_SHIFT 8 |
| 54 | #define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */ |
| 55 | #define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */ |
| 56 | |
| 57 | #define TIMER_LOAD_VAL 0xFFFFFFFF |
| 58 | #define TIMER_PRESCALE 255 |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 59 | |
| 60 | int timer_init(void) |
| 61 | { |
| 62 | const u32 emask = SCUTIMER_CONTROL_AUTO_RELOAD_MASK | |
| 63 | (TIMER_PRESCALE << SCUTIMER_CONTROL_PRESCALER_SHIFT) | |
| 64 | SCUTIMER_CONTROL_ENABLE_MASK; |
| 65 | |
Stefan Herbrechtsmeier | 3576380 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 66 | struct udevice *dev; |
| 67 | struct clk clk; |
| 68 | int ret; |
| 69 | |
| 70 | ret = uclass_get_device_by_driver(UCLASS_CLK, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 71 | DM_DRIVER_GET(zynq_clk), &dev); |
Stefan Herbrechtsmeier | 3576380 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 72 | if (ret) |
| 73 | return ret; |
| 74 | |
| 75 | clk.id = cpu_6or4x_clk; |
| 76 | ret = clk_request(dev, &clk); |
| 77 | if (ret < 0) |
| 78 | return ret; |
| 79 | |
| 80 | gd->cpu_clk = clk_get_rate(&clk); |
| 81 | |
| 82 | clk_free(&clk); |
Stefan Herbrechtsmeier | 3576380 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 83 | |
Michal Simek | 3924012 | 2013-11-22 15:29:38 +0100 | [diff] [blame] | 84 | gd->arch.timer_rate_hz = (gd->cpu_clk / 2) / (TIMER_PRESCALE + 1); |
Soren Brinkmann | 15fff9b | 2013-11-21 13:38:57 -0800 | [diff] [blame] | 85 | |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 86 | /* Load the timer counter register */ |
Michal Simek | 38003bc | 2013-08-28 07:36:31 +0200 | [diff] [blame] | 87 | writel(0xFFFFFFFF, &timer_base->load); |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | * Start the A9Timer device |
| 91 | * Enable Auto reload mode, Clear prescaler control bits |
| 92 | * Set prescaler value, Enable the decrementer |
| 93 | */ |
| 94 | clrsetbits_le32(&timer_base->control, SCUTIMER_CONTROL_PRESCALER_MASK, |
| 95 | emask); |
| 96 | |
| 97 | /* Reset time */ |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 98 | gd->arch.lastinc = readl(&timer_base->counter) / |
Soren Brinkmann | 15fff9b | 2013-11-21 13:38:57 -0800 | [diff] [blame] | 99 | (gd->arch.timer_rate_hz / CONFIG_SYS_HZ); |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 100 | gd->arch.tbl = 0; |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 106 | * This function is derived from PowerPC code (timebase clock frequency). |
| 107 | * On ARM it returns the number of timer ticks per second. |
| 108 | */ |
| 109 | ulong get_tbclk(void) |
| 110 | { |
Michal Simek | 40bcb86 | 2015-04-20 12:56:24 +0200 | [diff] [blame] | 111 | return gd->arch.timer_rate_hz; |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 112 | } |