time.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 <asm/atariints.h>
  20. void __init
  21. atari_sched_init(irq_handler_t timer_routine)
  22. {
  23. /* set Timer C data Register */
  24. mfp.tim_dt_c = INT_TICKS;
  25. /* start timer C, div = 1:100 */
  26. mfp.tim_ct_cd = (mfp.tim_ct_cd & 15) | 0x60;
  27. /* install interrupt service routine for MFP Timer C */
  28. request_irq(IRQ_MFP_TIMC, timer_routine, IRQ_TYPE_SLOW,
  29. "timer", timer_routine);
  30. }
  31. /* ++andreas: gettimeoffset fixed to check for pending interrupt */
  32. #define TICK_SIZE 10000
  33. /* This is always executed with interrupts disabled. */
  34. unsigned long atari_gettimeoffset (void)
  35. {
  36. unsigned long ticks, offset = 0;
  37. /* read MFP timer C current value */
  38. ticks = mfp.tim_dt_c;
  39. /* The probability of underflow is less than 2% */
  40. if (ticks > INT_TICKS - INT_TICKS / 50)
  41. /* Check for pending timer interrupt */
  42. if (mfp.int_pn_b & (1 << 5))
  43. offset = TICK_SIZE;
  44. ticks = INT_TICKS - ticks;
  45. ticks = ticks * 10000L / INT_TICKS;
  46. return ticks + offset;
  47. }
  48. static void mste_read(struct MSTE_RTC *val)
  49. {
  50. #define COPY(v) val->v=(mste_rtc.v & 0xf)
  51. do {
  52. COPY(sec_ones) ; COPY(sec_tens) ; COPY(min_ones) ;
  53. COPY(min_tens) ; COPY(hr_ones) ; COPY(hr_tens) ;
  54. COPY(weekday) ; COPY(day_ones) ; COPY(day_tens) ;
  55. COPY(mon_ones) ; COPY(mon_tens) ; COPY(year_ones) ;
  56. COPY(year_tens) ;
  57. /* prevent from reading the clock while it changed */
  58. } while (val->sec_ones != (mste_rtc.sec_ones & 0xf));
  59. #undef COPY
  60. }
  61. static void mste_write(struct MSTE_RTC *val)
  62. {
  63. #define COPY(v) mste_rtc.v=val->v
  64. do {
  65. COPY(sec_ones) ; COPY(sec_tens) ; COPY(min_ones) ;
  66. COPY(min_tens) ; COPY(hr_ones) ; COPY(hr_tens) ;
  67. COPY(weekday) ; COPY(day_ones) ; COPY(day_tens) ;
  68. COPY(mon_ones) ; COPY(mon_tens) ; COPY(year_ones) ;
  69. COPY(year_tens) ;
  70. /* prevent from writing the clock while it changed */
  71. } while (val->sec_ones != (mste_rtc.sec_ones & 0xf));
  72. #undef COPY
  73. }
  74. #define RTC_READ(reg) \
  75. ({ unsigned char __val; \
  76. (void) atari_writeb(reg,&tt_rtc.regsel); \
  77. __val = tt_rtc.data; \
  78. __val; \
  79. })
  80. #define RTC_WRITE(reg,val) \
  81. do { \
  82. atari_writeb(reg,&tt_rtc.regsel); \
  83. tt_rtc.data = (val); \
  84. } while(0)
  85. #define HWCLK_POLL_INTERVAL 5
  86. int atari_mste_hwclk( int op, struct rtc_time *t )
  87. {
  88. int hour, year;
  89. int hr24=0;
  90. struct MSTE_RTC val;
  91. mste_rtc.mode=(mste_rtc.mode | 1);
  92. hr24=mste_rtc.mon_tens & 1;
  93. mste_rtc.mode=(mste_rtc.mode & ~1);
  94. if (op) {
  95. /* write: prepare values */
  96. val.sec_ones = t->tm_sec % 10;
  97. val.sec_tens = t->tm_sec / 10;
  98. val.min_ones = t->tm_min % 10;
  99. val.min_tens = t->tm_min / 10;
  100. hour = t->tm_hour;
  101. if (!hr24) {
  102. if (hour > 11)
  103. hour += 20 - 12;
  104. if (hour == 0 || hour == 20)
  105. hour += 12;
  106. }
  107. val.hr_ones = hour % 10;
  108. val.hr_tens = hour / 10;
  109. val.day_ones = t->tm_mday % 10;
  110. val.day_tens = t->tm_mday / 10;
  111. val.mon_ones = (t->tm_mon+1) % 10;
  112. val.mon_tens = (t->tm_mon+1) / 10;
  113. year = t->tm_year - 80;
  114. val.year_ones = year % 10;
  115. val.year_tens = year / 10;
  116. val.weekday = t->tm_wday;
  117. mste_write(&val);
  118. mste_rtc.mode=(mste_rtc.mode | 1);
  119. val.year_ones = (year % 4); /* leap year register */
  120. mste_rtc.mode=(mste_rtc.mode & ~1);
  121. }
  122. else {
  123. mste_read(&val);
  124. t->tm_sec = val.sec_ones + val.sec_tens * 10;
  125. t->tm_min = val.min_ones + val.min_tens * 10;
  126. hour = val.hr_ones + val.hr_tens * 10;
  127. if (!hr24) {
  128. if (hour == 12 || hour == 12 + 20)
  129. hour -= 12;
  130. if (hour >= 20)
  131. hour += 12 - 20;
  132. }
  133. t->tm_hour = hour;
  134. t->tm_mday = val.day_ones + val.day_tens * 10;
  135. t->tm_mon = val.mon_ones + val.mon_tens * 10 - 1;
  136. t->tm_year = val.year_ones + val.year_tens * 10 + 80;
  137. t->tm_wday = val.weekday;
  138. }
  139. return 0;
  140. }
  141. int atari_tt_hwclk( int op, struct rtc_time *t )
  142. {
  143. int sec=0, min=0, hour=0, day=0, mon=0, year=0, wday=0;
  144. unsigned long flags;
  145. unsigned char ctrl;
  146. int pm = 0;
  147. ctrl = RTC_READ(RTC_CONTROL); /* control registers are
  148. * independent from the UIP */
  149. if (op) {
  150. /* write: prepare values */
  151. sec = t->tm_sec;
  152. min = t->tm_min;
  153. hour = t->tm_hour;
  154. day = t->tm_mday;
  155. mon = t->tm_mon + 1;
  156. year = t->tm_year - atari_rtc_year_offset;
  157. wday = t->tm_wday + (t->tm_wday >= 0);
  158. if (!(ctrl & RTC_24H)) {
  159. if (hour > 11) {
  160. pm = 0x80;
  161. if (hour != 12)
  162. hour -= 12;
  163. }
  164. else if (hour == 0)
  165. hour = 12;
  166. }
  167. if (!(ctrl & RTC_DM_BINARY)) {
  168. BIN_TO_BCD(sec);
  169. BIN_TO_BCD(min);
  170. BIN_TO_BCD(hour);
  171. BIN_TO_BCD(day);
  172. BIN_TO_BCD(mon);
  173. BIN_TO_BCD(year);
  174. if (wday >= 0) BIN_TO_BCD(wday);
  175. }
  176. }
  177. /* Reading/writing the clock registers is a bit critical due to
  178. * the regular update cycle of the RTC. While an update is in
  179. * progress, registers 0..9 shouldn't be touched.
  180. * The problem is solved like that: If an update is currently in
  181. * progress (the UIP bit is set), the process sleeps for a while
  182. * (50ms). This really should be enough, since the update cycle
  183. * normally needs 2 ms.
  184. * If the UIP bit reads as 0, we have at least 244 usecs until the
  185. * update starts. This should be enough... But to be sure,
  186. * additionally the RTC_SET bit is set to prevent an update cycle.
  187. */
  188. while( RTC_READ(RTC_FREQ_SELECT) & RTC_UIP ) {
  189. if (in_atomic() || irqs_disabled())
  190. mdelay(1);
  191. else
  192. schedule_timeout_interruptible(HWCLK_POLL_INTERVAL);
  193. }
  194. local_irq_save(flags);
  195. RTC_WRITE( RTC_CONTROL, ctrl | RTC_SET );
  196. if (!op) {
  197. sec = RTC_READ( RTC_SECONDS );
  198. min = RTC_READ( RTC_MINUTES );
  199. hour = RTC_READ( RTC_HOURS );
  200. day = RTC_READ( RTC_DAY_OF_MONTH );
  201. mon = RTC_READ( RTC_MONTH );
  202. year = RTC_READ( RTC_YEAR );
  203. wday = RTC_READ( RTC_DAY_OF_WEEK );
  204. }
  205. else {
  206. RTC_WRITE( RTC_SECONDS, sec );
  207. RTC_WRITE( RTC_MINUTES, min );
  208. RTC_WRITE( RTC_HOURS, hour + pm);
  209. RTC_WRITE( RTC_DAY_OF_MONTH, day );
  210. RTC_WRITE( RTC_MONTH, mon );
  211. RTC_WRITE( RTC_YEAR, year );
  212. if (wday >= 0) RTC_WRITE( RTC_DAY_OF_WEEK, wday );
  213. }
  214. RTC_WRITE( RTC_CONTROL, ctrl & ~RTC_SET );
  215. local_irq_restore(flags);
  216. if (!op) {
  217. /* read: adjust values */
  218. if (hour & 0x80) {
  219. hour &= ~0x80;
  220. pm = 1;
  221. }
  222. if (!(ctrl & RTC_DM_BINARY)) {
  223. BCD_TO_BIN(sec);
  224. BCD_TO_BIN(min);
  225. BCD_TO_BIN(hour);
  226. BCD_TO_BIN(day);
  227. BCD_TO_BIN(mon);
  228. BCD_TO_BIN(year);
  229. BCD_TO_BIN(wday);
  230. }
  231. if (!(ctrl & RTC_24H)) {
  232. if (!pm && hour == 12)
  233. hour = 0;
  234. else if (pm && hour != 12)
  235. hour += 12;
  236. }
  237. t->tm_sec = sec;
  238. t->tm_min = min;
  239. t->tm_hour = hour;
  240. t->tm_mday = day;
  241. t->tm_mon = mon - 1;
  242. t->tm_year = year + atari_rtc_year_offset;
  243. t->tm_wday = wday - 1;
  244. }
  245. return( 0 );
  246. }
  247. int atari_mste_set_clock_mmss (unsigned long nowtime)
  248. {
  249. short real_seconds = nowtime % 60, real_minutes = (nowtime / 60) % 60;
  250. struct MSTE_RTC val;
  251. unsigned char rtc_minutes;
  252. mste_read(&val);
  253. rtc_minutes= val.min_ones + val.min_tens * 10;
  254. if ((rtc_minutes < real_minutes
  255. ? real_minutes - rtc_minutes
  256. : rtc_minutes - real_minutes) < 30)
  257. {
  258. val.sec_ones = real_seconds % 10;
  259. val.sec_tens = real_seconds / 10;
  260. val.min_ones = real_minutes % 10;
  261. val.min_tens = real_minutes / 10;
  262. mste_write(&val);
  263. }
  264. else
  265. return -1;
  266. return 0;
  267. }
  268. int atari_tt_set_clock_mmss (unsigned long nowtime)
  269. {
  270. int retval = 0;
  271. short real_seconds = nowtime % 60, real_minutes = (nowtime / 60) % 60;
  272. unsigned char save_control, save_freq_select, rtc_minutes;
  273. save_control = RTC_READ (RTC_CONTROL); /* tell the clock it's being set */
  274. RTC_WRITE (RTC_CONTROL, save_control | RTC_SET);
  275. save_freq_select = RTC_READ (RTC_FREQ_SELECT); /* stop and reset prescaler */
  276. RTC_WRITE (RTC_FREQ_SELECT, save_freq_select | RTC_DIV_RESET2);
  277. rtc_minutes = RTC_READ (RTC_MINUTES);
  278. if (!(save_control & RTC_DM_BINARY))
  279. BCD_TO_BIN (rtc_minutes);
  280. /* Since we're only adjusting minutes and seconds, don't interfere
  281. with hour overflow. This avoids messing with unknown time zones
  282. but requires your RTC not to be off by more than 30 minutes. */
  283. if ((rtc_minutes < real_minutes
  284. ? real_minutes - rtc_minutes
  285. : rtc_minutes - real_minutes) < 30)
  286. {
  287. if (!(save_control & RTC_DM_BINARY))
  288. {
  289. BIN_TO_BCD (real_seconds);
  290. BIN_TO_BCD (real_minutes);
  291. }
  292. RTC_WRITE (RTC_SECONDS, real_seconds);
  293. RTC_WRITE (RTC_MINUTES, real_minutes);
  294. }
  295. else
  296. retval = -1;
  297. RTC_WRITE (RTC_FREQ_SELECT, save_freq_select);
  298. RTC_WRITE (RTC_CONTROL, save_control);
  299. return retval;
  300. }
  301. /*
  302. * Local variables:
  303. * c-indent-level: 4
  304. * tab-width: 8
  305. * End:
  306. */