Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 2 | /* |
Stefan Herbrechtsmeier | c7abb82 | 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 | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 6 | * Copyright (C) 2012 Michal Simek <monstr@monstr.eu> |
Michal Simek | 3e1b61d | 2018-01-17 07:37:47 +0100 | [diff] [blame] | 7 | * Copyright (C) 2011-2017 Xilinx, Inc. All rights reserved. |
Michal Simek | 38b343d | 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 | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Stefan Herbrechtsmeier | c7abb82 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 30 | #include <clk.h> |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 31 | #include <common.h> |
| 32 | #include <div64.h> |
Stefan Herbrechtsmeier | c7abb82 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 33 | #include <dm.h> |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 34 | #include <asm/io.h> |
Michal Simek | 4b21284 | 2013-04-12 16:21:26 +0200 | [diff] [blame] | 35 | #include <asm/arch/hardware.h> |
Soren Brinkmann | 614c272 | 2013-11-21 13:38:57 -0800 | [diff] [blame] | 36 | #include <asm/arch/clk.h> |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 37 | |
| 38 | DECLARE_GLOBAL_DATA_PTR; |
| 39 | |
| 40 | struct scu_timer { |
| 41 | u32 load; /* Timer Load Register */ |
| 42 | u32 counter; /* Timer Counter Register */ |
| 43 | u32 control; /* Timer Control Register */ |
| 44 | }; |
| 45 | |
| 46 | static struct scu_timer *timer_base = |
Michal Simek | 4b21284 | 2013-04-12 16:21:26 +0200 | [diff] [blame] | 47 | (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR; |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 48 | |
| 49 | #define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */ |
| 50 | #define SCUTIMER_CONTROL_PRESCALER_SHIFT 8 |
| 51 | #define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */ |
| 52 | #define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */ |
| 53 | |
| 54 | #define TIMER_LOAD_VAL 0xFFFFFFFF |
| 55 | #define TIMER_PRESCALE 255 |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 56 | |
| 57 | int timer_init(void) |
| 58 | { |
| 59 | const u32 emask = SCUTIMER_CONTROL_AUTO_RELOAD_MASK | |
| 60 | (TIMER_PRESCALE << SCUTIMER_CONTROL_PRESCALER_SHIFT) | |
| 61 | SCUTIMER_CONTROL_ENABLE_MASK; |
| 62 | |
Stefan Herbrechtsmeier | c7abb82 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 63 | struct udevice *dev; |
| 64 | struct clk clk; |
| 65 | int ret; |
| 66 | |
| 67 | ret = uclass_get_device_by_driver(UCLASS_CLK, |
| 68 | DM_GET_DRIVER(zynq_clk), &dev); |
| 69 | if (ret) |
| 70 | return ret; |
| 71 | |
| 72 | clk.id = cpu_6or4x_clk; |
| 73 | ret = clk_request(dev, &clk); |
| 74 | if (ret < 0) |
| 75 | return ret; |
| 76 | |
| 77 | gd->cpu_clk = clk_get_rate(&clk); |
| 78 | |
| 79 | clk_free(&clk); |
Stefan Herbrechtsmeier | c7abb82 | 2017-01-17 16:27:26 +0100 | [diff] [blame] | 80 | |
Michal Simek | 2826fd3 | 2013-11-22 15:29:38 +0100 | [diff] [blame] | 81 | gd->arch.timer_rate_hz = (gd->cpu_clk / 2) / (TIMER_PRESCALE + 1); |
Soren Brinkmann | 614c272 | 2013-11-21 13:38:57 -0800 | [diff] [blame] | 82 | |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 83 | /* Load the timer counter register */ |
Michal Simek | 7ba69b7 | 2013-08-28 07:36:31 +0200 | [diff] [blame] | 84 | writel(0xFFFFFFFF, &timer_base->load); |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * Start the A9Timer device |
| 88 | * Enable Auto reload mode, Clear prescaler control bits |
| 89 | * Set prescaler value, Enable the decrementer |
| 90 | */ |
| 91 | clrsetbits_le32(&timer_base->control, SCUTIMER_CONTROL_PRESCALER_MASK, |
| 92 | emask); |
| 93 | |
| 94 | /* Reset time */ |
Simon Glass | 582601d | 2012-12-13 20:48:35 +0000 | [diff] [blame] | 95 | gd->arch.lastinc = readl(&timer_base->counter) / |
Soren Brinkmann | 614c272 | 2013-11-21 13:38:57 -0800 | [diff] [blame] | 96 | (gd->arch.timer_rate_hz / CONFIG_SYS_HZ); |
Simon Glass | 66ee692 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 97 | gd->arch.tbl = 0; |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 103 | * This function is derived from PowerPC code (timebase clock frequency). |
| 104 | * On ARM it returns the number of timer ticks per second. |
| 105 | */ |
| 106 | ulong get_tbclk(void) |
| 107 | { |
Michal Simek | a2ec7fb | 2015-04-20 12:56:24 +0200 | [diff] [blame] | 108 | return gd->arch.timer_rate_hz; |
Michal Simek | 38b343d | 2012-09-13 20:23:35 +0000 | [diff] [blame] | 109 | } |