blob: ea2a9cd9b15984a18065e8c4cdb52d962ae4ae91 [file] [log] [blame]
Michal Simek216f0342009-03-27 14:25:27 +01001/*
2 * include/asm-microblaze/delay.h
3 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details.
7 *
8 * Copyright (C) 2008 Michal Simek
9 * Copyright (C) 2007 John Williams
10 * Copyright (C) 2006 Atmark Techno, Inc.
11 */
12
13#ifndef _ASM_MICROBLAZE_DELAY_H
14#define _ASM_MICROBLAZE_DELAY_H
15
Michal Simekac1566e2014-01-30 15:25:37 +010016#include <linux/param.h>
17
Michal Simekb6db0a52014-12-18 15:51:30 +010018static inline void __delay(unsigned long loops)
Michal Simek216f0342009-03-27 14:25:27 +010019{
20 asm volatile ("# __delay \n\t" \
21 "1: addi %0, %0, -1\t\n" \
22 "bneid %0, 1b \t\n" \
23 "nop \t\n"
24 : "=r" (loops)
25 : "0" (loops));
26}
27
28/*
29 * Note that 19 * 226 == 4294 ==~ 2^32 / 10^6, so
30 * loops = (4294 * usecs * loops_per_jiffy * HZ) / 2^32.
31 *
32 * The mul instruction gives us loops = (a * b) / 2^32.
33 * We choose a = usecs * 19 * HZ and b = loops_per_jiffy * 226
34 * because this lets us support a wide range of HZ and
35 * loops_per_jiffy values without either a or b overflowing 2^32.
36 * Thus we need usecs * HZ <= (2^32 - 1) / 19 = 226050910 and
37 * loops_per_jiffy <= (2^32 - 1) / 226 = 19004280
38 * (which corresponds to ~3800 bogomips at HZ = 100).
39 * -- paulus
40 */
41#define __MAX_UDELAY (226050910UL/HZ) /* maximum udelay argument */
42#define __MAX_NDELAY (4294967295UL/HZ) /* maximum ndelay argument */
43
44extern unsigned long loops_per_jiffy;
45
Michal Simekb6db0a52014-12-18 15:51:30 +010046static inline void __udelay(unsigned int x)
Michal Simek216f0342009-03-27 14:25:27 +010047{
48
49 unsigned long long tmp =
50 (unsigned long long)x * (unsigned long long)loops_per_jiffy \
51 * 226LL;
52 unsigned loops = tmp >> 32;
53
54/*
55 __asm__("mulxuu %0,%1,%2" : "=r" (loops) :
56 "r" (x), "r" (loops_per_jiffy * 226));
57*/
58 __delay(loops);
59}
60
61extern void __bad_udelay(void); /* deliberately undefined */
62extern void __bad_ndelay(void); /* deliberately undefined */
63
Michal Simekad1d4352014-07-09 13:23:55 +020064#define udelay(n) \
65 ({ \
66 if (__builtin_constant_p(n)) { \
67 if ((n) / __MAX_UDELAY >= 1) \
68 __bad_udelay(); \
69 else \
70 __udelay((n) * (19 * HZ)); \
71 } else { \
72 __udelay((n) * (19 * HZ)); \
73 } \
74 })
Michal Simek216f0342009-03-27 14:25:27 +010075
Michal Simekad1d4352014-07-09 13:23:55 +020076#define ndelay(n) \
77 ({ \
78 if (__builtin_constant_p(n)) { \
79 if ((n) / __MAX_NDELAY >= 1) \
80 __bad_ndelay(); \
81 else \
82 __udelay((n) * HZ); \
83 } else { \
84 __udelay((n) * HZ); \
85 } \
86 })
Michal Simek216f0342009-03-27 14:25:27 +010087
88#define muldiv(a, b, c) (((a)*(b))/(c))
89
90#endif /* _ASM_MICROBLAZE_DELAY_H */