xref: /openbmc/u-boot/arch/powerpc/lib/ticks.S (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini/* SPDX-License-Identifier: GPL-2.0+ */
2a47a12beSStefan Roese/*
3a47a12beSStefan Roese * (C) Copyright 2000, 2001
4a47a12beSStefan Roese * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com.
5a47a12beSStefan Roese *  base on code by
6a47a12beSStefan Roese * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7a47a12beSStefan Roese */
8a47a12beSStefan Roese
9a47a12beSStefan Roese#include <ppc_asm.tmpl>
10a47a12beSStefan Roese#include <ppc_defs.h>
11a47a12beSStefan Roese#include <config.h>
12a47a12beSStefan Roese#include <watchdog.h>
13a47a12beSStefan Roese
14a47a12beSStefan Roese/*
15a47a12beSStefan Roese * unsigned long long get_ticks(void);
16a47a12beSStefan Roese *
17a47a12beSStefan Roese * read timebase as "long long"
18a47a12beSStefan Roese */
19a47a12beSStefan Roese	.globl	get_ticks
20a47a12beSStefan Roeseget_ticks:
21a47a12beSStefan Roese1:	mftbu	r3
22a47a12beSStefan Roese	mftb	r4
23a47a12beSStefan Roese	mftbu	r5
24a47a12beSStefan Roese	cmp	0,r3,r5
25a47a12beSStefan Roese	bne	1b
26a47a12beSStefan Roese	blr
27a47a12beSStefan Roese
28a47a12beSStefan Roese/*
29a47a12beSStefan Roese * Delay for a number of ticks
30a47a12beSStefan Roese */
31a47a12beSStefan Roese	.globl	wait_ticks
32a47a12beSStefan Roesewait_ticks:
3383f83d19SJoakim Tjernlund	stwu	r1, -16(r1)
3483f83d19SJoakim Tjernlund	mflr	r0		/* save link register */
3583f83d19SJoakim Tjernlund	stw	r0, 20(r1)	/* Use r0 or GDB will be unhappy */
36fac150e8SMats Kärrman	stw	r14, 12(r1)	/* save used registers */
37fac150e8SMats Kärrman	stw	r15, 8(r1)
38fac150e8SMats Kärrman	mr	r14, r3		/* save tick count */
39a47a12beSStefan Roese	bl	get_ticks	/* Get start time */
40a47a12beSStefan Roese
41a47a12beSStefan Roese	/* Calculate end time */
42fac150e8SMats Kärrman	addc    r14, r4, r14	/* Compute end time lower */
43fac150e8SMats Kärrman	addze	r15, r3		/*     and end time upper */
44a47a12beSStefan Roese
45a47a12beSStefan Roese	WATCHDOG_RESET		/* Trigger watchdog, if needed */
46a47a12beSStefan Roese1:	bl	get_ticks	/* Get current time */
47fac150e8SMats Kärrman	subfc	r4, r4, r14	/* Subtract current time from end time */
48fac150e8SMats Kärrman	subfe.	r3, r3, r15
49a47a12beSStefan Roese	bge	1b		/* Loop until time expired */
50a47a12beSStefan Roese
51fac150e8SMats Kärrman	lwz     r15, 8(r1)	/* restore saved registers */
52fac150e8SMats Kärrman	lwz     r14, 12(r1)
53fac150e8SMats Kärrman	lwz     r0, 20(r1)
5483f83d19SJoakim Tjernlund	addi	r1,r1,16
55fac150e8SMats Kärrman	mtlr	r0
56a47a12beSStefan Roese	blr
57