1a47a12beSStefan Roese /* 2a47a12beSStefan Roese * (C) Copyright 2000, 2001 3a47a12beSStefan Roese * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4a47a12beSStefan Roese * 51a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 6a47a12beSStefan Roese */ 7a47a12beSStefan Roese 8a47a12beSStefan Roese #include <common.h> 9*ba3da734SChristophe Leroy #include <asm/io.h> 10a47a12beSStefan Roese 11a47a12beSStefan Roese /* ------------------------------------------------------------------------- */ 12a47a12beSStefan Roese 13a47a12beSStefan Roese /* 14a47a12beSStefan Roese * This function is intended for SHORT delays only. 15a47a12beSStefan Roese * It will overflow at around 10 seconds @ 400MHz, 16a47a12beSStefan Roese * or 20 seconds @ 200MHz. 17a47a12beSStefan Roese */ 18a47a12beSStefan Roese unsigned long usec2ticks(unsigned long usec) 19a47a12beSStefan Roese { 20a47a12beSStefan Roese ulong ticks; 21a47a12beSStefan Roese 22a47a12beSStefan Roese if (usec < 1000) { 23a47a12beSStefan Roese ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000; 24a47a12beSStefan Roese } else { 25a47a12beSStefan Roese ticks = ((usec / 10) * (get_tbclk() / 100000)); 26a47a12beSStefan Roese } 27a47a12beSStefan Roese 28a47a12beSStefan Roese return (ticks); 29a47a12beSStefan Roese } 30a47a12beSStefan Roese 31a47a12beSStefan Roese /* ------------------------------------------------------------------------- */ 32a47a12beSStefan Roese 33a47a12beSStefan Roese /* 34a47a12beSStefan Roese * We implement the delay by converting the delay (the number of 35a47a12beSStefan Roese * microseconds to wait) into a number of time base ticks; then we 36a47a12beSStefan Roese * watch the time base until it has incremented by that amount. 37a47a12beSStefan Roese */ 38a47a12beSStefan Roese void __udelay(unsigned long usec) 39a47a12beSStefan Roese { 40a47a12beSStefan Roese ulong ticks = usec2ticks (usec); 41a47a12beSStefan Roese wait_ticks (ticks); 42a47a12beSStefan Roese } 43a47a12beSStefan Roese 44a47a12beSStefan Roese /* ------------------------------------------------------------------------- */ 45a47a12beSStefan Roese #ifndef CONFIG_NAND_SPL 46a47a12beSStefan Roese unsigned long ticks2usec(unsigned long ticks) 47a47a12beSStefan Roese { 48a47a12beSStefan Roese ulong tbclk = get_tbclk(); 49a47a12beSStefan Roese 50a47a12beSStefan Roese /* usec = ticks * 1000000 / tbclk 51a47a12beSStefan Roese * Multiplication would overflow at ~4.2e3 ticks, 52a47a12beSStefan Roese * so we break it up into 53a47a12beSStefan Roese * usec = ( ( ticks * 1000) / tbclk ) * 1000; 54a47a12beSStefan Roese */ 55a47a12beSStefan Roese ticks *= 1000L; 56a47a12beSStefan Roese ticks /= tbclk; 57a47a12beSStefan Roese ticks *= 1000L; 58a47a12beSStefan Roese 59a47a12beSStefan Roese return ((ulong)ticks); 60a47a12beSStefan Roese } 61a47a12beSStefan Roese #endif 62a47a12beSStefan Roese /* ------------------------------------------------------------------------- */ 63a47a12beSStefan Roese 6470e2aaf3SSimon Glass int timer_init(void) 65a47a12beSStefan Roese { 6696805a52STimur Tabi unsigned long temp; 6796805a52STimur Tabi 68907208c4SChristophe Leroy #if defined(CONFIG_8xx) 69*ba3da734SChristophe Leroy immap_t __iomem *immap = (immap_t __iomem *)CONFIG_SYS_IMMR; 70907208c4SChristophe Leroy 71907208c4SChristophe Leroy /* unlock */ 72*ba3da734SChristophe Leroy out_be32(&immap->im_sitk.sitk_tbk, KAPWR_KEY); 73907208c4SChristophe Leroy #endif 74907208c4SChristophe Leroy 75a47a12beSStefan Roese /* reset */ 7696805a52STimur Tabi asm volatile("li %0,0 ; mttbu %0 ; mttbl %0;" 7796805a52STimur Tabi : "=&r"(temp) ); 78a47a12beSStefan Roese 79907208c4SChristophe Leroy #if defined(CONFIG_8xx) 80907208c4SChristophe Leroy /* enable */ 81*ba3da734SChristophe Leroy setbits_be16(&immap->im_sit.sit_tbscr, TBSCR_TBE); 82907208c4SChristophe Leroy #endif 83a47a12beSStefan Roese return (0); 84a47a12beSStefan Roese } 85a47a12beSStefan Roese /* ------------------------------------------------------------------------- */ 86