1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 Regents of the University of California 4 */ 5 6 #include <linux/delay.h> 7 #include <linux/math.h> 8 #include <linux/param.h> 9 #include <linux/types.h> 10 #include <linux/export.h> 11 12 #include <asm/processor.h> 13 14 /* 15 * This is copies from arch/arm/include/asm/delay.h 16 * 17 * Loop (or tick) based delay: 18 * 19 * loops = loops_per_jiffy * jiffies_per_sec * delay_us / us_per_sec 20 * 21 * where: 22 * 23 * jiffies_per_sec = HZ 24 * us_per_sec = 1000000 25 * 26 * Therefore the constant part is HZ / 1000000 which is a small 27 * fractional number. To make this usable with integer math, we 28 * scale up this constant by 2^31, perform the actual multiplication, 29 * and scale the result back down by 2^31 with a simple shift: 30 * 31 * loops = (loops_per_jiffy * delay_us * UDELAY_MULT) >> 31 32 * 33 * where: 34 * 35 * UDELAY_MULT = 2^31 * HZ / 1000000 36 * = (2^31 / 1000000) * HZ 37 * = 2147.483648 * HZ 38 * = 2147 * HZ + 483648 * HZ / 1000000 39 * 40 * 31 is the biggest scale shift value that won't overflow 32 bits for 41 * delay_us * UDELAY_MULT assuming HZ <= 1000 and delay_us <= 2000. 42 */ 43 #define MAX_UDELAY_US 2000 44 #define MAX_UDELAY_HZ 1000 45 #define UDELAY_MULT (2147UL * HZ + 483648UL * HZ / 1000000UL) 46 #define UDELAY_SHIFT 31 47 48 #if HZ > MAX_UDELAY_HZ 49 #error "HZ > MAX_UDELAY_HZ" 50 #endif 51 52 /* 53 * RISC-V supports both UDELAY and NDELAY. This is largely the same as above, 54 * but with different constants. I added 10 bits to the shift to get this, but 55 * the result is that I need a 64-bit multiply, which is slow on 32-bit 56 * platforms. 57 * 58 * NDELAY_MULT = 2^41 * HZ / 1000000000 59 * = (2^41 / 1000000000) * HZ 60 * = 2199.02325555 * HZ 61 * = 2199 * HZ + 23255550 * HZ / 1000000000 62 * 63 * The maximum here is to avoid 64-bit overflow, but it isn't checked as it 64 * won't happen. 65 */ 66 #define MAX_NDELAY_NS (1ULL << 42) 67 #define MAX_NDELAY_HZ MAX_UDELAY_HZ 68 #define NDELAY_MULT ((unsigned long long)(2199ULL * HZ + 23255550ULL * HZ / 1000000000ULL)) 69 #define NDELAY_SHIFT 41 70 71 #if HZ > MAX_NDELAY_HZ 72 #error "HZ > MAX_NDELAY_HZ" 73 #endif 74 75 void __delay(unsigned long cycles) 76 { 77 u64 t0 = get_cycles(); 78 79 while ((unsigned long)(get_cycles() - t0) < cycles) 80 cpu_relax(); 81 } 82 EXPORT_SYMBOL(__delay); 83 84 void udelay(unsigned long usecs) 85 { 86 u64 ucycles = (u64)usecs * lpj_fine * UDELAY_MULT; 87 u64 n; 88 89 if (unlikely(usecs > MAX_UDELAY_US)) { 90 n = (u64)usecs * riscv_timebase; 91 do_div(n, 1000000); 92 93 __delay(n); 94 return; 95 } 96 97 __delay(ucycles >> UDELAY_SHIFT); 98 } 99 EXPORT_SYMBOL(udelay); 100 101 void ndelay(unsigned long nsecs) 102 { 103 /* 104 * This doesn't bother checking for overflow, as it won't happen (it's 105 * an hour) of delay. 106 */ 107 unsigned long long ncycles = nsecs * lpj_fine * NDELAY_MULT; 108 __delay(ncycles >> NDELAY_SHIFT); 109 } 110 EXPORT_SYMBOL(ndelay); 111 112 bool delay_read_timer(unsigned long *timer_val) 113 { 114 *timer_val = get_cycles(); 115 return true; 116 } 117