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> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 37 | #include <asm/global_data.h> |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 38 | #include <asm/io.h> |
Michal Simek | ad2e2b7 | 2013-04-12 16:21:26 +0200 | [diff] [blame] | 39 | #include <asm/arch/hardware.h> |
Soren Brinkmann | 15fff9b | 2013-11-21 13:38:57 -0800 | [diff] [blame] | 40 | #include <asm/arch/clk.h> |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 41 | |
| 42 | DECLARE_GLOBAL_DATA_PTR; |
| 43 | |
| 44 | struct scu_timer { |
| 45 | u32 load; /* Timer Load Register */ |
| 46 | u32 counter; /* Timer Counter Register */ |
| 47 | u32 control; /* Timer Control Register */ |
| 48 | }; |
| 49 | |
| 50 | static struct scu_timer *timer_base = |
Michal Simek | ad2e2b7 | 2013-04-12 16:21:26 +0200 | [diff] [blame] | 51 | (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR; |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 52 | |
| 53 | #define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */ |
| 54 | #define SCUTIMER_CONTROL_PRESCALER_SHIFT 8 |
| 55 | #define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */ |
| 56 | #define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */ |
| 57 | |
| 58 | #define TIMER_LOAD_VAL 0xFFFFFFFF |
| 59 | #define TIMER_PRESCALE 255 |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 60 | |
| 61 | int timer_init(void) |
| 62 | { |
| 63 | const u32 emask = SCUTIMER_CONTROL_AUTO_RELOAD_MASK | |
| 64 | (TIMER_PRESCALE << SCUTIMER_CONTROL_PRESCALER_SHIFT) | |
| 65 | SCUTIMER_CONTROL_ENABLE_MASK; |
| 66 | |
Stefan Herbrechtsmeier | 3576380 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 67 | struct udevice *dev; |
| 68 | struct clk clk; |
| 69 | int ret; |
| 70 | |
| 71 | ret = uclass_get_device_by_driver(UCLASS_CLK, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 72 | DM_DRIVER_GET(zynq_clk), &dev); |
Stefan Herbrechtsmeier | 3576380 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 73 | if (ret) |
| 74 | return ret; |
| 75 | |
| 76 | clk.id = cpu_6or4x_clk; |
| 77 | ret = clk_request(dev, &clk); |
| 78 | if (ret < 0) |
| 79 | return ret; |
| 80 | |
| 81 | gd->cpu_clk = clk_get_rate(&clk); |
| 82 | |
| 83 | clk_free(&clk); |
Stefan Herbrechtsmeier | 3576380 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 84 | |
Michal Simek | 3924012 | 2013-11-22 15:29:38 +0100 | [diff] [blame] | 85 | gd->arch.timer_rate_hz = (gd->cpu_clk / 2) / (TIMER_PRESCALE + 1); |
Soren Brinkmann | 15fff9b | 2013-11-21 13:38:57 -0800 | [diff] [blame] | 86 | |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 87 | /* Load the timer counter register */ |
Michal Simek | 38003bc | 2013-08-28 07:36:31 +0200 | [diff] [blame] | 88 | writel(0xFFFFFFFF, &timer_base->load); |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Start the A9Timer device |
| 92 | * Enable Auto reload mode, Clear prescaler control bits |
| 93 | * Set prescaler value, Enable the decrementer |
| 94 | */ |
| 95 | clrsetbits_le32(&timer_base->control, SCUTIMER_CONTROL_PRESCALER_MASK, |
| 96 | emask); |
| 97 | |
| 98 | /* Reset time */ |
Simon Glass | a848da5 | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 99 | gd->arch.lastinc = readl(&timer_base->counter) / |
Soren Brinkmann | 15fff9b | 2013-11-21 13:38:57 -0800 | [diff] [blame] | 100 | (gd->arch.timer_rate_hz / CONFIG_SYS_HZ); |
Simon Glass | 2655ee1 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 101 | gd->arch.tbl = 0; |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 107 | * This function is derived from PowerPC code (timebase clock frequency). |
| 108 | * On ARM it returns the number of timer ticks per second. |
| 109 | */ |
| 110 | ulong get_tbclk(void) |
| 111 | { |
Michal Simek | 40bcb86 | 2015-04-20 12:56:24 +0200 | [diff] [blame] | 112 | return gd->arch.timer_rate_hz; |
Michal Simek | dea68a7 | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 113 | } |