time.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  3. *
  4. * Adapted for PowerPC (PReP) by Gary Thomas
  5. * Modified by Cort Dougan (cort@cs.nmt.edu).
  6. * Copied and modified from arch/i386/kernel/time.c
  7. *
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/sched.h>
  11. #include <linux/kernel.h>
  12. #include <linux/param.h>
  13. #include <linux/string.h>
  14. #include <linux/mm.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/time.h>
  17. #include <linux/timex.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/mc146818rtc.h>
  20. #include <linux/init.h>
  21. #include <linux/bcd.h>
  22. #include <linux/ioport.h>
  23. #include <asm/io.h>
  24. #include <asm/nvram.h>
  25. #include <asm/prom.h>
  26. #include <asm/sections.h>
  27. #include <asm/time.h>
  28. #include <platforms/chrp/chrp.h>
  29. extern spinlock_t rtc_lock;
  30. #define NVRAM_AS0 0x74
  31. #define NVRAM_AS1 0x75
  32. #define NVRAM_DATA 0x77
  33. static int nvram_as1 = NVRAM_AS1;
  34. static int nvram_as0 = NVRAM_AS0;
  35. static int nvram_data = NVRAM_DATA;
  36. long __init chrp_time_init(void)
  37. {
  38. struct device_node *rtcs;
  39. struct resource r;
  40. int base;
  41. rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
  42. if (rtcs == NULL)
  43. rtcs = of_find_compatible_node(NULL, "rtc", "ds1385-rtc");
  44. if (rtcs == NULL)
  45. return 0;
  46. if (of_address_to_resource(rtcs, 0, &r)) {
  47. of_node_put(rtcs);
  48. return 0;
  49. }
  50. of_node_put(rtcs);
  51. base = r.start;
  52. nvram_as1 = 0;
  53. nvram_as0 = base;
  54. nvram_data = base + 1;
  55. return 0;
  56. }
  57. static int chrp_cmos_clock_read(int addr)
  58. {
  59. if (nvram_as1 != 0)
  60. outb(addr>>8, nvram_as1);
  61. outb(addr, nvram_as0);
  62. return (inb(nvram_data));
  63. }
  64. static void chrp_cmos_clock_write(unsigned long val, int addr)
  65. {
  66. if (nvram_as1 != 0)
  67. outb(addr>>8, nvram_as1);
  68. outb(addr, nvram_as0);
  69. outb(val, nvram_data);
  70. return;
  71. }
  72. /*
  73. * Set the hardware clock. -- Cort
  74. */
  75. int chrp_set_rtc_time(struct rtc_time *tmarg)
  76. {
  77. unsigned char save_control, save_freq_select;
  78. struct rtc_time tm = *tmarg;
  79. spin_lock(&rtc_lock);
  80. save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  81. chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
  82. save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  83. chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  84. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  85. tm.tm_sec = bin2bcd(tm.tm_sec);
  86. tm.tm_min = bin2bcd(tm.tm_min);
  87. tm.tm_hour = bin2bcd(tm.tm_hour);
  88. tm.tm_mon = bin2bcd(tm.tm_mon);
  89. tm.tm_mday = bin2bcd(tm.tm_mday);
  90. tm.tm_year = bin2bcd(tm.tm_year);
  91. }
  92. chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
  93. chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
  94. chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
  95. chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
  96. chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
  97. chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
  98. /* The following flags have to be released exactly in this order,
  99. * otherwise the DS12887 (popular MC146818A clone with integrated
  100. * battery and quartz) will not reset the oscillator and will not
  101. * update precisely 500 ms later. You won't find this mentioned in
  102. * the Dallas Semiconductor data sheets, but who believes data
  103. * sheets anyway ... -- Markus Kuhn
  104. */
  105. chrp_cmos_clock_write(save_control, RTC_CONTROL);
  106. chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
  107. spin_unlock(&rtc_lock);
  108. return 0;
  109. }
  110. void chrp_get_rtc_time(struct rtc_time *tm)
  111. {
  112. unsigned int year, mon, day, hour, min, sec;
  113. do {
  114. sec = chrp_cmos_clock_read(RTC_SECONDS);
  115. min = chrp_cmos_clock_read(RTC_MINUTES);
  116. hour = chrp_cmos_clock_read(RTC_HOURS);
  117. day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
  118. mon = chrp_cmos_clock_read(RTC_MONTH);
  119. year = chrp_cmos_clock_read(RTC_YEAR);
  120. } while (sec != chrp_cmos_clock_read(RTC_SECONDS));
  121. if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  122. sec = bcd2bin(sec);
  123. min = bcd2bin(min);
  124. hour = bcd2bin(hour);
  125. day = bcd2bin(day);
  126. mon = bcd2bin(mon);
  127. year = bcd2bin(year);
  128. }
  129. if (year < 70)
  130. year += 100;
  131. tm->tm_sec = sec;
  132. tm->tm_min = min;
  133. tm->tm_hour = hour;
  134. tm->tm_mday = day;
  135. tm->tm_mon = mon;
  136. tm->tm_year = year;
  137. }