1 1.1 uwe /* $NetBSD: delay.c,v 1.1 2006/09/01 21:26:18 uwe Exp $ */ 2 1.1 uwe 3 1.1 uwe /*- 4 1.1 uwe * Copyright (c) 2005 NONAKA Kimihiro 5 1.1 uwe * All rights reserved. 6 1.1 uwe * 7 1.1 uwe * Redistribution and use in source and binary forms, with or without 8 1.1 uwe * modification, are permitted provided that the following conditions 9 1.1 uwe * are met: 10 1.1 uwe * 1. Redistributions of source code must retain the above copyright 11 1.1 uwe * notice, this list of conditions and the following disclaimer. 12 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 uwe * notice, this list of conditions and the following disclaimer in the 14 1.1 uwe * documentation and/or other materials provided with the distribution. 15 1.1 uwe * 16 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 1.1 uwe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 uwe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 uwe * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 1.1 uwe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 uwe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 uwe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 uwe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 uwe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 uwe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 uwe * SUCH DAMAGE. 27 1.1 uwe */ 28 1.1 uwe 29 1.1 uwe #include <sys/types.h> 30 1.1 uwe 31 1.1 uwe #include <lib/libsa/stand.h> 32 1.1 uwe 33 1.1 uwe #include <sh3/tmureg.h> 34 1.1 uwe 35 1.1 uwe #include "boot.h" 36 1.1 uwe 37 1.1 uwe #ifndef TICK_CH 38 1.1 uwe #define TICK_CH 0 39 1.1 uwe #endif 40 1.1 uwe #if TICK_CH == 0 41 1.1 uwe #define TSTR SH4_TSTR 42 1.1 uwe #define TCOR SH4_TCOR0 43 1.1 uwe #define TCNT SH4_TCNT0 44 1.1 uwe #define TCR SH4_TCR0 45 1.1 uwe #define TSTR_CH TSTR_STR0 46 1.1 uwe #elif TICK_CH == 1 47 1.1 uwe #define TSTR SH4_TSTR 48 1.1 uwe #define TCOR SH4_TCOR1 49 1.1 uwe #define TCNT SH4_TCNT1 50 1.1 uwe #define TCR SH4_TCR1 51 1.1 uwe #define TSTR_CH TSTR_STR1 52 1.1 uwe #elif TICK_CH == 2 53 1.1 uwe #define TSTR SH4_TSTR 54 1.1 uwe #define TCOR SH4_TCOR2 55 1.1 uwe #define TCNT SH4_TCNT2 56 1.1 uwe #define TCR SH4_TCR2 57 1.1 uwe #define TSTR_CH TSTR_STR2 58 1.1 uwe #elif TICK_CH == 3 59 1.1 uwe #define TSTR SH4_TSTR2 60 1.1 uwe #define TCOR SH4_TCOR3 61 1.1 uwe #define TCNT SH4_TCNT3 62 1.1 uwe #define TCR SH4_TCR3 63 1.1 uwe #define TSTR_CH SH4_TSTR2_STR3 64 1.1 uwe #elif TICK_CH == 4 65 1.1 uwe #define TSTR SH4_TSTR2 66 1.1 uwe #define TCOR SH4_TCOR4 67 1.1 uwe #define TCNT SH4_TCNT4 68 1.1 uwe #define TCR SH4_TCR4 69 1.1 uwe #define TSTR_CH SH4_TSTR2_STR4 70 1.1 uwe #else 71 1.1 uwe #error TICK_CH != [01234] 72 1.1 uwe #endif 73 1.1 uwe 74 1.1 uwe #ifndef TICK_PRESC 75 1.1 uwe #define TICK_PRESC 1024 76 1.1 uwe #endif 77 1.1 uwe #if TICK_PRESC == 4 78 1.1 uwe #define TCR_TPSC TCR_TPSC_P4 79 1.1 uwe #elif TICK_PRESC == 16 80 1.1 uwe #define TCR_TPSC TCR_TPSC_P16 81 1.1 uwe #elif TICK_PRESC == 64 82 1.1 uwe #define TCR_TPSC TCR_TPSC_P64 83 1.1 uwe #elif TICK_PRESC == 256 84 1.1 uwe #define TCR_TPSC TCR_TPSC_P256 85 1.1 uwe #elif TICK_PRESC == 1024 86 1.1 uwe #define TCR_TPSC SH4_TCR_TPSC_P1024 87 1.1 uwe #else 88 1.1 uwe #error TICK_PRESC != 4, 16, 64, 256, 1024 89 1.1 uwe #endif 90 1.1 uwe 91 1.1 uwe #define TICKS_PER_SEC (PCLOCK / TICK_PRESC) 92 1.1 uwe #define MS_PER_TICK (1000000 / TICKS_PER_SEC) 93 1.1 uwe 94 1.1 uwe int 95 1.1 uwe tick_init(void) 96 1.1 uwe { 97 1.1 uwe 98 1.1 uwe _reg_bclr_1(TSTR, TSTR_CH); 99 1.1 uwe _reg_write_2(TCR, TCR_TPSC); 100 1.1 uwe _reg_write_4(TCOR, 0xffffffff); 101 1.1 uwe _reg_write_4(TCNT, 0xffffffff); 102 1.1 uwe _reg_bset_1(TSTR, TSTR_CH); 103 1.1 uwe 104 1.1 uwe return 0; 105 1.1 uwe } 106 1.1 uwe 107 1.1 uwe void 108 1.1 uwe tick_stop(void) 109 1.1 uwe { 110 1.1 uwe 111 1.1 uwe _reg_bclr_1(TSTR, TSTR_CH); 112 1.1 uwe } 113 1.1 uwe 114 1.1 uwe uint32_t 115 1.1 uwe gettick(void) 116 1.1 uwe { 117 1.1 uwe 118 1.1 uwe return ~(_reg_read_4(TCNT)); 119 1.1 uwe } 120 1.1 uwe 121 1.1 uwe void 122 1.1 uwe delay(int ms) 123 1.1 uwe { 124 1.1 uwe uint32_t base, now; 125 1.1 uwe 126 1.1 uwe base = gettick(); 127 1.1 uwe for (;;) { 128 1.1 uwe now = gettick(); 129 1.1 uwe if (((now - base) / MS_PER_TICK) > ms) { 130 1.1 uwe break; 131 1.1 uwe } 132 1.1 uwe } 133 1.1 uwe } 134