ip22-time.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * This file is subject to the terms and conditions of the GNU General Public
  3.  * License.  See the file "COPYING" in the main directory of this archive
  4.  * for more details.
  5.  *
  6.  * Time operations for IP22 machines. Original code may come from
  7.  * Ralf Baechle or David S. Miller (sorry guys, i'm really not sure)
  8.  *
  9.  * Copyright (C) 2001 by Ladislav Michl
  10.  */
  11. #include <linux/kernel.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/time.h>
  15. #include <asm/cpu.h>
  16. #include <asm/mipsregs.h>
  17. #include <asm/io.h>
  18. #include <asm/irq.h>
  19. #include <asm/ds1286.h>
  20. #include <asm/sgialib.h>
  21. #include <asm/sgi/sgint23.h>
  22. #include <asm/sgi/sgigio.h>
  23. #include <asm/time.h>
  24. /*
  25.  * note that mktime uses month from 1 to 12 while to_tm
  26.  * uses 0 to 11.
  27.  */
  28. static unsigned long indy_rtc_get_time(void)
  29. {
  30. unsigned char yrs, mon, day, hrs, min, sec;
  31. unsigned char save_control;
  32. save_control = CMOS_READ(RTC_CMD);
  33. CMOS_WRITE((save_control|RTC_TE), RTC_CMD);
  34. sec = CMOS_READ(RTC_SECONDS);
  35. min = CMOS_READ(RTC_MINUTES);
  36. hrs = CMOS_READ(RTC_HOURS) & 0x1f;
  37. day = CMOS_READ(RTC_DATE);
  38. mon = CMOS_READ(RTC_MONTH) & 0x1f;
  39. yrs = CMOS_READ(RTC_YEAR);
  40. CMOS_WRITE(save_control, RTC_CMD);
  41. BCD_TO_BIN(sec);
  42. BCD_TO_BIN(min);
  43. BCD_TO_BIN(hrs);
  44. BCD_TO_BIN(day);
  45. BCD_TO_BIN(mon);
  46. BCD_TO_BIN(yrs);
  47. if (yrs < 45)
  48. yrs += 30;
  49. if ((yrs += 40) < 70)
  50. yrs += 100;
  51. return mktime((int)yrs + 1900, mon, day, hrs, min, sec);
  52. }
  53. static int indy_rtc_set_time(unsigned long tim)
  54. {
  55. struct rtc_time tm;
  56. unsigned char save_control;
  57. to_tm(tim, &tm);
  58. tm.tm_mon += 1; /* tm_mon starts at zero */
  59. tm.tm_year -= 1940;
  60. if (tm.tm_year >= 100)
  61. tm.tm_year -= 100;
  62. BIN_TO_BCD(tm.tm_sec);
  63. BIN_TO_BCD(tm.tm_min);
  64. BIN_TO_BCD(tm.tm_hour);
  65. BIN_TO_BCD(tm.tm_mday);
  66. BIN_TO_BCD(tm.tm_mon);
  67. BIN_TO_BCD(tm.tm_year);
  68. save_control = CMOS_READ(RTC_CMD);
  69. CMOS_WRITE((save_control|RTC_TE), RTC_CMD);
  70. CMOS_WRITE(tm.tm_year, RTC_YEAR);
  71. CMOS_WRITE(tm.tm_mon, RTC_MONTH);
  72. CMOS_WRITE(tm.tm_mday, RTC_DATE);
  73. CMOS_WRITE(tm.tm_hour, RTC_HOURS);
  74. CMOS_WRITE(tm.tm_min, RTC_MINUTES);
  75. CMOS_WRITE(tm.tm_sec, RTC_SECONDS);
  76. CMOS_WRITE(0, RTC_HUNDREDTH_SECOND);
  77. CMOS_WRITE(save_control, RTC_CMD);
  78. return 0;
  79. }
  80. static unsigned long dosample(volatile unsigned char *tcwp,
  81.       volatile unsigned char *tc2p)
  82. {
  83. u32 ct0, ct1;
  84. volatile u8 msb, lsb;
  85. /* Start the counter. */
  86. *tcwp = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL | SGINT_TCWORD_MRGEN);
  87. *tc2p = (SGINT_TCSAMP_COUNTER & 0xff);
  88. *tc2p = (SGINT_TCSAMP_COUNTER >> 8);
  89. /* Get initial counter invariant */
  90. ct0 = read_32bit_cp0_register(CP0_COUNT);
  91. /* Latch and spin until top byte of counter2 is zero */
  92. do {
  93. *tcwp = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CLAT);
  94. lsb = *tc2p;
  95. msb = *tc2p;
  96. ct1 = read_32bit_cp0_register(CP0_COUNT);
  97. } while(msb);
  98. /* Stop the counter. */
  99. *tcwp = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL | SGINT_TCWORD_MSWST);
  100. /*
  101.  * Return the difference, this is how far the r4k counter increments
  102.  * for every 1/HZ seconds. We round off the nearest 1 MHz of master
  103.  * clock (= 1000000 / 100 / 2 = 5000 count).
  104.  */
  105. return ((ct1 - ct0) / 5000) * 5000;
  106. }
  107. /*
  108.  * Here we need to calibrate the cycle counter to at least be close.
  109.  */
  110. void indy_time_init(void)
  111. {
  112. struct sgi_ioc_timers *p;
  113. volatile unsigned char *tcwp, *tc2p;
  114. unsigned long r4k_ticks[3];
  115. unsigned long r4k_tick;
  116. /* Figure out the r4k offset, the algorithm is very simple
  117.  * and works in _all_ cases as long as the 8254 counter
  118.  * register itself works ok (as an interrupt driving timer
  119.  * it does not because of bug, this is why we are using
  120.  * the onchip r4k counter/compare register to serve this
  121.  * purpose, but for r4k_offset calculation it will work
  122.  * ok for us).  There are other very complicated ways
  123.  * of performing this calculation but this one works just
  124.  * fine so I am not going to futz around. ;-)
  125.  */
  126. p = ioc_timers;
  127. tcwp = &p->tcword;
  128. tc2p = &p->tcnt2;
  129. printk(KERN_INFO "Calibrating system timer... ");
  130. dosample(tcwp, tc2p);                   /* Prime cache. */
  131. dosample(tcwp, tc2p);                   /* Prime cache. */
  132. /* Zero is NOT an option. */
  133. do {
  134. r4k_ticks[0] = dosample (tcwp, tc2p);
  135. } while (!r4k_ticks[0]);
  136. do {
  137. r4k_ticks[1] = dosample (tcwp, tc2p);
  138. } while (!r4k_ticks[1]);
  139. if (r4k_ticks[0] != r4k_ticks[1]) {
  140. printk ("warning: timer counts differ, retrying...");
  141. r4k_ticks[2] = dosample (tcwp, tc2p);
  142. if (r4k_ticks[2] == r4k_ticks[0]
  143.     || r4k_ticks[2] == r4k_ticks[1])
  144. r4k_tick = r4k_ticks[2];
  145. else {
  146. printk ("disagreement, using average...");
  147. r4k_tick = (r4k_ticks[0] + r4k_ticks[1]
  148.    + r4k_ticks[2]) / 3;
  149. }
  150. } else
  151. r4k_tick = r4k_ticks[0];
  152. printk("%d [%d.%02d MHz CPU]n", (int) r4k_tick,
  153. (int) (r4k_tick / 5000), (int) (r4k_tick % 5000) / 50);
  154. mips_counter_frequency = r4k_tick * HZ;
  155. /* HACK ALERT! This get's called after traps initialization
  156.  * We piggyback the initialization of GIO bus here even though
  157.  * it is technically not related with the timer in any way.
  158.  * Doing it from ip22_setup wouldn't work since traps aren't
  159.  * initialized yet.
  160.  */
  161. sgigio_init();
  162. }
  163. /* Generic SGI handler for (spurious) 8254 interrupts */
  164. void indy_8254timer_irq(struct pt_regs *regs)
  165. {
  166. int cpu = smp_processor_id();
  167. int irq = SGI_8254_0_IRQ;
  168. long cnt;
  169. char c;
  170. irq_enter(cpu, irq);
  171. kstat.irqs[cpu][irq]++;
  172. printk("indy_8254timer_irq: Whoops, should not have gotten this IRQn");
  173. ArcRead(0, &c, 1, &cnt);
  174. ArcEnterInteractiveMode();
  175. irq_exit(cpu, irq);
  176. }
  177. void indy_r4k_timer_interrupt(struct pt_regs *regs)
  178. {
  179. int cpu = smp_processor_id();
  180. int irq = SGI_TIMER_IRQ;
  181. irq_enter(cpu, irq);
  182. kstat.irqs[cpu][irq]++;
  183. timer_interrupt(irq, NULL, regs);
  184. irq_exit(cpu, irq);
  185. if (softirq_pending(cpu))
  186. do_softirq();
  187. }
  188. extern int setup_irq(unsigned int irq, struct irqaction *irqaction);
  189. static void indy_timer_setup(struct irqaction *irq)
  190. {
  191. unsigned long count;
  192. /* over-write the handler, we use our own way */
  193. irq->handler = no_action;
  194. /* set time for first interrupt */
  195. count = read_32bit_cp0_register(CP0_COUNT);
  196. count += mips_counter_frequency / HZ;
  197. write_32bit_cp0_register(CP0_COMPARE, count);
  198. /* setup irqaction */
  199. setup_irq(SGI_TIMER_IRQ, irq);
  200. }
  201. void sgitime_init(void)
  202. {
  203. /* setup hookup functions */
  204. rtc_get_time = indy_rtc_get_time;
  205. rtc_set_time = indy_rtc_set_time;
  206. board_time_init = indy_time_init;
  207. board_timer_setup = indy_timer_setup;
  208. }