xref: /rk3399_ARM-atf/drivers/delay_timer/delay_timer.c (revision 0bcedb2212bd2436834117cd956d7b6e16b11673)
19055c7d1SRyan Harkin /*
29055c7d1SRyan Harkin  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
39055c7d1SRyan Harkin  *
49055c7d1SRyan Harkin  * Redistribution and use in source and binary forms, with or without
59055c7d1SRyan Harkin  * modification, are permitted provided that the following conditions are met:
69055c7d1SRyan Harkin  *
79055c7d1SRyan Harkin  * Redistributions of source code must retain the above copyright notice, this
89055c7d1SRyan Harkin  * list of conditions and the following disclaimer.
99055c7d1SRyan Harkin  *
109055c7d1SRyan Harkin  * Redistributions in binary form must reproduce the above copyright notice,
119055c7d1SRyan Harkin  * this list of conditions and the following disclaimer in the documentation
129055c7d1SRyan Harkin  * and/or other materials provided with the distribution.
139055c7d1SRyan Harkin  *
149055c7d1SRyan Harkin  * Neither the name of ARM nor the names of its contributors may be used
159055c7d1SRyan Harkin  * to endorse or promote products derived from this software without specific
169055c7d1SRyan Harkin  * prior written permission.
179055c7d1SRyan Harkin  *
189055c7d1SRyan Harkin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
199055c7d1SRyan Harkin  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209055c7d1SRyan Harkin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219055c7d1SRyan Harkin  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
229055c7d1SRyan Harkin  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
239055c7d1SRyan Harkin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
249055c7d1SRyan Harkin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
259055c7d1SRyan Harkin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
269055c7d1SRyan Harkin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
279055c7d1SRyan Harkin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
289055c7d1SRyan Harkin  * POSSIBILITY OF SUCH DAMAGE.
299055c7d1SRyan Harkin  */
309055c7d1SRyan Harkin 
319055c7d1SRyan Harkin #include <assert.h>
329055c7d1SRyan Harkin #include <delay_timer.h>
339055c7d1SRyan Harkin #include <platform_def.h>
349055c7d1SRyan Harkin 
359055c7d1SRyan Harkin /***********************************************************
369055c7d1SRyan Harkin  * The delay timer implementation
379055c7d1SRyan Harkin  ***********************************************************/
389055c7d1SRyan Harkin static const timer_ops_t *ops;
399055c7d1SRyan Harkin 
409055c7d1SRyan Harkin /***********************************************************
419055c7d1SRyan Harkin  * Delay for the given number of microseconds. The driver must
429055c7d1SRyan Harkin  * be initialized before calling this function.
439055c7d1SRyan Harkin  ***********************************************************/
449055c7d1SRyan Harkin void udelay(uint32_t usec)
459055c7d1SRyan Harkin {
469055c7d1SRyan Harkin 	assert(ops != 0 &&
479055c7d1SRyan Harkin 		(ops->clk_mult != 0) &&
489055c7d1SRyan Harkin 		(ops->clk_div != 0) &&
499055c7d1SRyan Harkin 		(ops->get_timer_value != 0));
509055c7d1SRyan Harkin 
51*0bcedb22SAntonio Nino Diaz 	uint32_t start, delta, total_delta;
529055c7d1SRyan Harkin 
53*0bcedb22SAntonio Nino Diaz 	assert(usec < UINT32_MAX / ops->clk_div);
54*0bcedb22SAntonio Nino Diaz 
559055c7d1SRyan Harkin 	start = ops->get_timer_value();
56*0bcedb22SAntonio Nino Diaz 
57*0bcedb22SAntonio Nino Diaz 	total_delta = (usec * ops->clk_div) / ops->clk_mult;
58*0bcedb22SAntonio Nino Diaz 
599055c7d1SRyan Harkin 	do {
60*0bcedb22SAntonio Nino Diaz 		/*
61*0bcedb22SAntonio Nino Diaz 		 * If the timer value wraps around, the subtraction will
62*0bcedb22SAntonio Nino Diaz 		 * overflow and it will still give the correct result.
63*0bcedb22SAntonio Nino Diaz 		 */
64*0bcedb22SAntonio Nino Diaz 		delta = start - ops->get_timer_value(); /* Decreasing counter */
65*0bcedb22SAntonio Nino Diaz 
66*0bcedb22SAntonio Nino Diaz 	} while (delta < total_delta);
679055c7d1SRyan Harkin }
689055c7d1SRyan Harkin 
699055c7d1SRyan Harkin /***********************************************************
709055c7d1SRyan Harkin  * Delay for the given number of milliseconds. The driver must
719055c7d1SRyan Harkin  * be initialized before calling this function.
729055c7d1SRyan Harkin  ***********************************************************/
739055c7d1SRyan Harkin void mdelay(uint32_t msec)
749055c7d1SRyan Harkin {
759055c7d1SRyan Harkin 	udelay(msec*1000);
769055c7d1SRyan Harkin }
779055c7d1SRyan Harkin 
789055c7d1SRyan Harkin /***********************************************************
799055c7d1SRyan Harkin  * Initialize the timer. The fields in the provided timer
809055c7d1SRyan Harkin  * ops pointer must be valid.
819055c7d1SRyan Harkin  ***********************************************************/
829055c7d1SRyan Harkin void timer_init(const timer_ops_t *ops_ptr)
839055c7d1SRyan Harkin {
849055c7d1SRyan Harkin 	assert(ops_ptr != 0  &&
859055c7d1SRyan Harkin 		(ops_ptr->clk_mult != 0) &&
869055c7d1SRyan Harkin 		(ops_ptr->clk_div != 0) &&
879055c7d1SRyan Harkin 		(ops_ptr->get_timer_value != 0));
889055c7d1SRyan Harkin 
899055c7d1SRyan Harkin 	ops = ops_ptr;
909055c7d1SRyan Harkin }
91