Ryan Harkin | 32539fc | 2015-03-17 14:50:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <assert.h> |
| 32 | #include <delay_timer.h> |
| 33 | #include <platform_def.h> |
| 34 | |
| 35 | /*********************************************************** |
| 36 | * The delay timer implementation |
| 37 | ***********************************************************/ |
| 38 | static const timer_ops_t *ops; |
| 39 | |
| 40 | /*********************************************************** |
| 41 | * Delay for the given number of microseconds. The driver must |
| 42 | * be initialized before calling this function. |
| 43 | ***********************************************************/ |
| 44 | void udelay(uint32_t usec) |
| 45 | { |
| 46 | assert(ops != 0 && |
| 47 | (ops->clk_mult != 0) && |
| 48 | (ops->clk_div != 0) && |
| 49 | (ops->get_timer_value != 0)); |
| 50 | |
Antonio Nino Diaz | bcd79b2 | 2016-05-18 10:37:25 +0100 | [diff] [blame] | 51 | uint32_t start, delta, total_delta; |
Ryan Harkin | 32539fc | 2015-03-17 14:50:05 +0000 | [diff] [blame] | 52 | |
Antonio Nino Diaz | bcd79b2 | 2016-05-18 10:37:25 +0100 | [diff] [blame] | 53 | assert(usec < UINT32_MAX / ops->clk_div); |
| 54 | |
Ryan Harkin | 32539fc | 2015-03-17 14:50:05 +0000 | [diff] [blame] | 55 | start = ops->get_timer_value(); |
Antonio Nino Diaz | bcd79b2 | 2016-05-18 10:37:25 +0100 | [diff] [blame] | 56 | |
| 57 | total_delta = (usec * ops->clk_div) / ops->clk_mult; |
| 58 | |
Ryan Harkin | 32539fc | 2015-03-17 14:50:05 +0000 | [diff] [blame] | 59 | do { |
Antonio Nino Diaz | bcd79b2 | 2016-05-18 10:37:25 +0100 | [diff] [blame] | 60 | /* |
| 61 | * If the timer value wraps around, the subtraction will |
| 62 | * overflow and it will still give the correct result. |
| 63 | */ |
| 64 | delta = start - ops->get_timer_value(); /* Decreasing counter */ |
| 65 | |
| 66 | } while (delta < total_delta); |
Ryan Harkin | 32539fc | 2015-03-17 14:50:05 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /*********************************************************** |
| 70 | * Delay for the given number of milliseconds. The driver must |
| 71 | * be initialized before calling this function. |
| 72 | ***********************************************************/ |
| 73 | void mdelay(uint32_t msec) |
| 74 | { |
| 75 | udelay(msec*1000); |
| 76 | } |
| 77 | |
| 78 | /*********************************************************** |
| 79 | * Initialize the timer. The fields in the provided timer |
| 80 | * ops pointer must be valid. |
| 81 | ***********************************************************/ |
| 82 | void timer_init(const timer_ops_t *ops_ptr) |
| 83 | { |
| 84 | assert(ops_ptr != 0 && |
| 85 | (ops_ptr->clk_mult != 0) && |
| 86 | (ops_ptr->clk_div != 0) && |
| 87 | (ops_ptr->get_timer_value != 0)); |
| 88 | |
| 89 | ops = ops_ptr; |
| 90 | } |