time.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * linux/arch/m68k/atari/time.c
  3. *
  4. * Atari time and real time clock stuff
  5. *
  6. * Assembled of parts of former atari/config.c 97-12-18 by Roman Hodek
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file COPYING in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/types.h>
  13. #include <linux/mc146818rtc.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/init.h>
  16. #include <linux/rtc.h>
  17. #include <linux/bcd.h>
  18. #include <linux/delay.h>
  19. #include <linux/export.h>
  20. #include <asm/atariints.h>
  21. DEFINE_SPINLOCK(rtc_lock);
  22. EXPORT_SYMBOL_GPL(rtc_lock);
  23. void __init
  24. atari_sched_init(irq_handler_t timer_routine)
  25. {
  26. /* set Timer C data Register */
  27. st_mfp.tim_dt_c = INT_TICKS;
  28. /* start timer C, div = 1:100 */
  29. st_mfp.tim_ct_cd = (st_mfp.tim_ct_cd & 15) | 0x60;
  30. /* install interrupt service routine for MFP Timer C */
  31. if (request_irq(IRQ_MFP_TIMC, timer_routine, 0, "timer", timer_routine))
  32. pr_err("Couldn't register timer interrupt\n");
  33. }
  34. /* ++andreas: gettimeoffset fixed to check for pending interrupt */
  35. #define TICK_SIZE 10000
  36. /* This is always executed with interrupts disabled. */
  37. u32 atari_gettimeoffset(void)
  38. {
  39. u32 ticks, offset = 0;
  40. /* read MFP timer C current value */
  41. ticks = st_mfp.tim_dt_c;
  42. /* The probability of underflow is less than 2% */
  43. if (ticks > INT_TICKS - INT_TICKS / 50)
  44. /* Check for pending timer interrupt */
  45. if (st_mfp.int_pn_b & (1 << 5))
  46. offset = TICK_SIZE;
  47. ticks = INT_TICKS - ticks;
  48. ticks = ticks * 10000L / INT_TICKS;
  49. return (ticks + offset) * 1000;
  50. }
  51. static void mste_read(struct MSTE_RTC *val)
  52. {
  53. #define COPY(v) val->v=(mste_rtc.v & 0xf)
  54. do {
  55. COPY(sec_ones) ; COPY(sec_tens) ; COPY(min_ones) ;
  56. COPY(min_tens) ; COPY(hr_ones) ; COPY(hr_tens) ;
  57. COPY(weekday) ; COPY(day_ones) ; COPY(day_tens) ;
  58. COPY(mon_ones) ; COPY(mon_tens) ; COPY(year_ones) ;
  59. COPY(year_tens) ;
  60. /* prevent from reading the clock while it changed */
  61. } while (val->sec_ones != (mste_rtc.sec_ones & 0xf));
  62. #undef COPY
  63. }
  64. static void mste_write(struct MSTE_RTC *val)
  65. {
  66. #define COPY(v) mste_rtc.v=val->v
  67. do {
  68. COPY(sec_ones) ; COPY(sec_tens) ; COPY(min_ones) ;
  69. COPY(min_tens) ; COPY(hr_ones) ; COPY(hr_tens) ;
  70. COPY(weekday) ; COPY(day_ones) ; COPY(day_tens) ;
  71. COPY(mon_ones) ; COPY(mon_tens) ; COPY(year_ones) ;
  72. COPY(year_tens) ;
  73. /* prevent from writing the clock while it changed */
  74. } while (val->sec_ones != (mste_rtc.sec_ones & 0xf));
  75. #undef COPY
  76. }
  77. #define RTC_READ(reg) \
  78. ({ unsigned char __val; \
  79. (void) atari_writeb(reg,&tt_rtc.regsel); \
  80. __val = tt_rtc.data; \
  81. __val; \
  82. })
  83. #define RTC_WRITE(reg,val) \
  84. do { \
  85. atari_writeb(reg,&tt_rtc.regsel); \
  86. tt_rtc.data = (val); \
  87. } while(0)
  88. #define HWCLK_POLL_INTERVAL 5
  89. int atari_mste_hwclk( int op, struct rtc_time *t )
  90. {
  91. int hour, year;
  92. int hr24=0;
  93. struct MSTE_RTC val;
  94. mste_rtc.mode=(mste_rtc.mode | 1);
  95. hr24=mste_rtc.mon_tens & 1;
  96. mste_rtc.mode=(mste_rtc.mode & ~1);
  97. if (op) {
  98. /* write: prepare values */
  99. val.sec_ones = t->tm_sec % 10;
  100. val.sec_tens = t->tm_sec / 10;
  101. val.min_ones = t->tm_min % 10;
  102. val.min_tens = t->tm_min / 10;
  103. hour = t->tm_hour;
  104. if (!hr24) {
  105. if (hour > 11)
  106. hour += 20 - 12;
  107. if (hour == 0 || hour == 20)
  108. hour += 12;
  109. }
  110. val.hr_ones = hour % 10;
  111. val.hr_tens = hour / 10;
  112. val.day_ones = t->tm_mday % 10;
  113. val.day_tens = t->tm_mday / 10;
  114. val.mon_ones = (t->tm_mon+1) % 10;
  115. val.mon_tens = (t->tm_mon+1) / 10;
  116. year = t->tm_year - 80;
  117. val.year_ones = year % 10;
  118. val.year_tens = year / 10;
  119. val.weekday = t->tm_wday;
  120. mste_write(&val);
  121. mste_rtc.mode=(mste_rtc.mode | 1);
  122. val.year_ones = (year % 4); /* leap year register */
  123. mste_rtc.mode=(mste_rtc.mode & ~1);
  124. }
  125. else {
  126. mste_read(&val);
  127. t->tm_sec = val.sec_ones + val.sec_tens * 10;
  128. t->tm_min = val.min_ones + val.min_tens * 10;
  129. hour = val.hr_ones + val.hr_tens * 10;
  130. if (!hr24) {
  131. if (hour == 12 || hour == 12 + 20)
  132. hour -= 12;
  133. if (hour >= 20)
  134. hour += 12 - 20;
  135. }
  136. t->tm_hour = hour;
  137. t->tm_mday = val.day_ones + val.day_tens * 10;
  138. t->tm_mon = val.mon_ones + val.mon_tens * 10 - 1;
  139. t->tm_year = val.year_ones + val.year_tens * 10 + 80;
  140. t->tm_wday = val.weekday;
  141. }
  142. return 0;
  143. }
  144. int atari_tt_hwclk( int op, struct rtc_time *t )
  145. {
  146. int sec=0, min=0, hour=0, day=0, mon=0, year=0, wday=0;
  147. unsigned long flags;
  148. unsigned char ctrl;
  149. int pm = 0;
  150. ctrl = RTC_READ(RTC_CONTROL); /* control registers are
  151. * independent from the UIP */
  152. if (op) {
  153. /* write: prepare values */
  154. sec = t->tm_sec;
  155. min = t->tm_min;
  156. hour = t->tm_hour;
  157. day = t->tm_mday;
  158. mon = t->tm_mon + 1;
  159. year = t->tm_year - atari_rtc_year_offset;
  160. wday = t->tm_wday + (t->tm_wday >= 0);
  161. if (!(ctrl & RTC_24H)) {
  162. if (hour > 11) {
  163. pm = 0x80;
  164. if (hour != 12)
  165. hour -= 12;
  166. }
  167. else if (hour == 0)
  168. hour = 12;
  169. }
  170. if (!(ctrl & RTC_DM_BINARY)) {
  171. sec = bin2bcd(sec);
  172. min = bin2bcd(min);
  173. hour = bin2bcd(hour);
  174. day = bin2bcd(day);
  175. mon = bin2bcd(mon);
  176. year = bin2bcd(year);
  177. if (wday >= 0)
  178. wday = bin2bcd(wday);
  179. }
  180. }
  181. /* Reading/writing the clock registers is a bit critical due to
  182. * the regular update cycle of the RTC. While an update is in
  183. * progress, registers 0..9 shouldn't be touched.
  184. * The problem is solved like that: If an update is currently in
  185. * progress (the UIP bit is set), the process sleeps for a while
  186. * (50ms). This really should be enough, since the update cycle
  187. * normally needs 2 ms.
  188. * If the UIP bit reads as 0, we have at least 244 usecs until the
  189. * update starts. This should be enough... But to be sure,
  190. * additionally the RTC_SET bit is set to prevent an update cycle.
  191. */
  192. while( RTC_READ(RTC_FREQ_SELECT) & RTC_UIP ) {
  193. if (in_atomic() || irqs_disabled())
  194. mdelay(1);
  195. else
  196. schedule_timeout_interruptible(HWCLK_POLL_INTERVAL);
  197. }
  198. local_irq_save(flags);
  199. RTC_WRITE( RTC_CONTROL, ctrl | RTC_SET );
  200. if (!op) {
  201. sec = RTC_READ( RTC_SECONDS );
  202. min = RTC_READ( RTC_MINUTES );
  203. hour = RTC_READ( RTC_HOURS );
  204. day = RTC_READ( RTC_DAY_OF_MONTH );
  205. mon = RTC_READ( RTC_MONTH );
  206. year = RTC_READ( RTC_YEAR );
  207. wday = RTC_READ( RTC_DAY_OF_WEEK );
  208. }
  209. else {
  210. RTC_WRITE( RTC_SECONDS, sec );
  211. RTC_WRITE( RTC_MINUTES, min );
  212. RTC_WRITE( RTC_HOURS, hour + pm);
  213. RTC_WRITE( RTC_DAY_OF_MONTH, day );
  214. RTC_WRITE( RTC_MONTH, mon );
  215. RTC_WRITE( RTC_YEAR, year );
  216. if (wday >= 0) RTC_WRITE( RTC_DAY_OF_WEEK, wday );
  217. }
  218. RTC_WRITE( RTC_CONTROL, ctrl & ~RTC_SET );
  219. local_irq_restore(flags);
  220. if (!op) {
  221. /* read: adjust values */
  222. if (hour & 0x80) {
  223. hour &= ~0x80;
  224. pm = 1;
  225. }
  226. if (!(ctrl & RTC_DM_BINARY)) {
  227. sec = bcd2bin(sec);
  228. min = bcd2bin(min);
  229. hour = bcd2bin(hour);
  230. day = bcd2bin(day);
  231. mon = bcd2bin(mon);
  232. year = bcd2bin(year);
  233. wday = bcd2bin(wday);
  234. }
  235. if (!(ctrl & RTC_24H)) {
  236. if (!pm && hour == 12)
  237. hour = 0;
  238. else if (pm && hour != 12)
  239. hour += 12;
  240. }
  241. t->tm_sec = sec;
  242. t->tm_min = min;
  243. t->tm_hour = hour;
  244. t->tm_mday = day;
  245. t->tm_mon = mon - 1;
  246. t->tm_year = year + atari_rtc_year_offset;
  247. t->tm_wday = wday - 1;
  248. }
  249. return( 0 );
  250. }
  251. /*
  252. * Local variables:
  253. * c-indent-level: 4
  254. * tab-width: 8
  255. * End:
  256. */