time.c 9.6 KB

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