time.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Carsten Langgaard, carstenl@mips.com
  3. * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
  4. *
  5. * Copyright (C) 2003 MontaVista Software Inc.
  6. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  7. *
  8. * ########################################################################
  9. *
  10. * This program is free software; you can distribute it and/or modify it
  11. * under the terms of the GNU General Public License (Version 2) as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * ########################################################################
  24. *
  25. * Setting up the clock on the MIPS boards.
  26. */
  27. #include <linux/init.h>
  28. #include <linux/kernel_stat.h>
  29. #include <linux/sched.h>
  30. #include <linux/time.h>
  31. #include <linux/spinlock.h>
  32. #include <asm/time.h>
  33. #include <asm/mipsregs.h>
  34. #include <asm/ptrace.h>
  35. #include <asm/it8172/it8172.h>
  36. #include <asm/it8172/it8172_int.h>
  37. #include <asm/debug.h>
  38. #define IT8172_RTC_ADR_REG (IT8172_PCI_IO_BASE + IT_RTC_BASE)
  39. #define IT8172_RTC_DAT_REG (IT8172_RTC_ADR_REG + 1)
  40. #define IT8172_RTC_CENTURY_REG (IT8172_PCI_IO_BASE + IT_RTC_CENTURY)
  41. static volatile char *rtc_adr_reg = (char*)KSEG1ADDR(IT8172_RTC_ADR_REG);
  42. static volatile char *rtc_dat_reg = (char*)KSEG1ADDR(IT8172_RTC_DAT_REG);
  43. static volatile char *rtc_century_reg = (char*)KSEG1ADDR(IT8172_RTC_CENTURY_REG);
  44. unsigned char it8172_rtc_read_data(unsigned long addr)
  45. {
  46. unsigned char retval;
  47. *rtc_adr_reg = addr;
  48. retval = *rtc_dat_reg;
  49. return retval;
  50. }
  51. void it8172_rtc_write_data(unsigned char data, unsigned long addr)
  52. {
  53. *rtc_adr_reg = addr;
  54. *rtc_dat_reg = data;
  55. }
  56. #undef CMOS_READ
  57. #undef CMOS_WRITE
  58. #define CMOS_READ(addr) it8172_rtc_read_data(addr)
  59. #define CMOS_WRITE(data, addr) it8172_rtc_write_data(data, addr)
  60. static unsigned char saved_control; /* remember rtc control reg */
  61. static inline int rtc_24h(void) { return saved_control & RTC_24H; }
  62. static inline int rtc_dm_binary(void) { return saved_control & RTC_DM_BINARY; }
  63. static inline unsigned char
  64. bin_to_hw(unsigned char c)
  65. {
  66. if (rtc_dm_binary())
  67. return c;
  68. else
  69. return ((c/10) << 4) + (c%10);
  70. }
  71. static inline unsigned char
  72. hw_to_bin(unsigned char c)
  73. {
  74. if (rtc_dm_binary())
  75. return c;
  76. else
  77. return (c>>4)*10 + (c &0xf);
  78. }
  79. /* 0x80 bit indicates pm in 12-hour format */
  80. static inline unsigned char
  81. hour_bin_to_hw(unsigned char c)
  82. {
  83. if (rtc_24h())
  84. return bin_to_hw(c);
  85. if (c >= 12)
  86. return 0x80 | bin_to_hw((c==12)?12:c-12); /* 12 is 12pm */
  87. else
  88. return bin_to_hw((c==0)?12:c); /* 0 is 12 AM, not 0 am */
  89. }
  90. static inline unsigned char
  91. hour_hw_to_bin(unsigned char c)
  92. {
  93. unsigned char tmp = hw_to_bin(c&0x3f);
  94. if (rtc_24h())
  95. return tmp;
  96. if (c & 0x80)
  97. return (tmp==12)?12:tmp+12; /* 12pm is 12, not 24 */
  98. else
  99. return (tmp==12)?0:tmp; /* 12am is 0 */
  100. }
  101. static unsigned long r4k_offset; /* Amount to increment compare reg each time */
  102. static unsigned long r4k_cur; /* What counter should be at next timer irq */
  103. extern unsigned int mips_hpt_frequency;
  104. /*
  105. * Figure out the r4k offset, the amount to increment the compare
  106. * register for each time tick.
  107. * Use the RTC to calculate offset.
  108. */
  109. static unsigned long __init cal_r4koff(void)
  110. {
  111. unsigned int flags;
  112. local_irq_save(flags);
  113. /* Start counter exactly on falling edge of update flag */
  114. while (CMOS_READ(RTC_REG_A) & RTC_UIP);
  115. while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
  116. /* Start r4k counter. */
  117. write_c0_count(0);
  118. /* Read counter exactly on falling edge of update flag */
  119. while (CMOS_READ(RTC_REG_A) & RTC_UIP);
  120. while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
  121. mips_hpt_frequency = read_c0_count();
  122. /* restore interrupts */
  123. local_irq_restore(flags);
  124. return (mips_hpt_frequency / HZ);
  125. }
  126. static unsigned long
  127. it8172_rtc_get_time(void)
  128. {
  129. unsigned int year, mon, day, hour, min, sec;
  130. unsigned int flags;
  131. /* avoid update-in-progress. */
  132. for (;;) {
  133. local_irq_save(flags);
  134. if (! (CMOS_READ(RTC_REG_A) & RTC_UIP))
  135. break;
  136. /* don't hold intr closed all the time */
  137. local_irq_restore(flags);
  138. }
  139. /* Read regs. */
  140. sec = hw_to_bin(CMOS_READ(RTC_SECONDS));
  141. min = hw_to_bin(CMOS_READ(RTC_MINUTES));
  142. hour = hour_hw_to_bin(CMOS_READ(RTC_HOURS));
  143. day = hw_to_bin(CMOS_READ(RTC_DAY_OF_MONTH));
  144. mon = hw_to_bin(CMOS_READ(RTC_MONTH));
  145. year = hw_to_bin(CMOS_READ(RTC_YEAR)) +
  146. hw_to_bin(*rtc_century_reg) * 100;
  147. /* restore interrupts */
  148. local_irq_restore(flags);
  149. return mktime(year, mon, day, hour, min, sec);
  150. }
  151. static int
  152. it8172_rtc_set_time(unsigned long t)
  153. {
  154. struct rtc_time tm;
  155. unsigned int flags;
  156. /* convert */
  157. to_tm(t, &tm);
  158. /* avoid update-in-progress. */
  159. for (;;) {
  160. local_irq_save(flags);
  161. if (! (CMOS_READ(RTC_REG_A) & RTC_UIP))
  162. break;
  163. /* don't hold intr closed all the time */
  164. local_irq_restore(flags);
  165. }
  166. *rtc_century_reg = bin_to_hw(tm.tm_year/100);
  167. CMOS_WRITE(bin_to_hw(tm.tm_sec), RTC_SECONDS);
  168. CMOS_WRITE(bin_to_hw(tm.tm_min), RTC_MINUTES);
  169. CMOS_WRITE(hour_bin_to_hw(tm.tm_hour), RTC_HOURS);
  170. CMOS_WRITE(bin_to_hw(tm.tm_mday), RTC_DAY_OF_MONTH);
  171. CMOS_WRITE(bin_to_hw(tm.tm_mon+1), RTC_MONTH); /* tm_mon starts from 0 */
  172. CMOS_WRITE(bin_to_hw(tm.tm_year%100), RTC_YEAR);
  173. /* restore interrupts */
  174. local_irq_restore(flags);
  175. return 0;
  176. }
  177. void __init it8172_time_init(void)
  178. {
  179. unsigned int est_freq, flags;
  180. local_irq_save(flags);
  181. saved_control = CMOS_READ(RTC_CONTROL);
  182. printk("calculating r4koff... ");
  183. r4k_offset = cal_r4koff();
  184. printk("%08lx(%d)\n", r4k_offset, (int) r4k_offset);
  185. est_freq = 2*r4k_offset*HZ;
  186. est_freq += 5000; /* round */
  187. est_freq -= est_freq%10000;
  188. printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
  189. (est_freq%1000000)*100/1000000);
  190. local_irq_restore(flags);
  191. rtc_get_time = it8172_rtc_get_time;
  192. rtc_set_time = it8172_rtc_set_time;
  193. }
  194. #define ALLINTS (IE_IRQ0 | IE_IRQ1 | IE_IRQ2 | IE_IRQ3 | IE_IRQ4 | IE_IRQ5)
  195. void __init it8172_timer_setup(struct irqaction *irq)
  196. {
  197. puts("timer_setup\n");
  198. put32(NR_IRQS);
  199. puts("");
  200. /* we are using the cpu counter for timer interrupts */
  201. setup_irq(MIPS_CPU_TIMER_IRQ, irq);
  202. /* to generate the first timer interrupt */
  203. r4k_cur = (read_c0_count() + r4k_offset);
  204. write_c0_compare(r4k_cur);
  205. set_c0_status(ALLINTS);
  206. }