time.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. int atari_mste_set_clock_mmss (unsigned long nowtime)
  252. {
  253. short real_seconds = nowtime % 60, real_minutes = (nowtime / 60) % 60;
  254. struct MSTE_RTC val;
  255. unsigned char rtc_minutes;
  256. mste_read(&val);
  257. rtc_minutes= val.min_ones + val.min_tens * 10;
  258. if ((rtc_minutes < real_minutes
  259. ? real_minutes - rtc_minutes
  260. : rtc_minutes - real_minutes) < 30)
  261. {
  262. val.sec_ones = real_seconds % 10;
  263. val.sec_tens = real_seconds / 10;
  264. val.min_ones = real_minutes % 10;
  265. val.min_tens = real_minutes / 10;
  266. mste_write(&val);
  267. }
  268. else
  269. return -1;
  270. return 0;
  271. }
  272. int atari_tt_set_clock_mmss (unsigned long nowtime)
  273. {
  274. int retval = 0;
  275. short real_seconds = nowtime % 60, real_minutes = (nowtime / 60) % 60;
  276. unsigned char save_control, save_freq_select, rtc_minutes;
  277. save_control = RTC_READ (RTC_CONTROL); /* tell the clock it's being set */
  278. RTC_WRITE (RTC_CONTROL, save_control | RTC_SET);
  279. save_freq_select = RTC_READ (RTC_FREQ_SELECT); /* stop and reset prescaler */
  280. RTC_WRITE (RTC_FREQ_SELECT, save_freq_select | RTC_DIV_RESET2);
  281. rtc_minutes = RTC_READ (RTC_MINUTES);
  282. if (!(save_control & RTC_DM_BINARY))
  283. rtc_minutes = bcd2bin(rtc_minutes);
  284. /* Since we're only adjusting minutes and seconds, don't interfere
  285. with hour overflow. This avoids messing with unknown time zones
  286. but requires your RTC not to be off by more than 30 minutes. */
  287. if ((rtc_minutes < real_minutes
  288. ? real_minutes - rtc_minutes
  289. : rtc_minutes - real_minutes) < 30)
  290. {
  291. if (!(save_control & RTC_DM_BINARY))
  292. {
  293. real_seconds = bin2bcd(real_seconds);
  294. real_minutes = bin2bcd(real_minutes);
  295. }
  296. RTC_WRITE (RTC_SECONDS, real_seconds);
  297. RTC_WRITE (RTC_MINUTES, real_minutes);
  298. }
  299. else
  300. retval = -1;
  301. RTC_WRITE (RTC_FREQ_SELECT, save_freq_select);
  302. RTC_WRITE (RTC_CONTROL, save_control);
  303. return retval;
  304. }
  305. /*
  306. * Local variables:
  307. * c-indent-level: 4
  308. * tab-width: 8
  309. * End:
  310. */