time.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/clocksource.h>
  19. #include <linux/delay.h>
  20. #include <linux/export.h>
  21. #include <asm/atariints.h>
  22. #include <asm/machdep.h>
  23. #include "atari.h"
  24. DEFINE_SPINLOCK(rtc_lock);
  25. EXPORT_SYMBOL_GPL(rtc_lock);
  26. static u64 atari_read_clk(struct clocksource *cs);
  27. static struct clocksource atari_clk = {
  28. .name = "mfp",
  29. .rating = 100,
  30. .read = atari_read_clk,
  31. .mask = CLOCKSOURCE_MASK(32),
  32. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  33. };
  34. static u32 clk_total;
  35. static u8 last_timer_count;
  36. static irqreturn_t mfp_timer_c_handler(int irq, void *dev_id)
  37. {
  38. unsigned long flags;
  39. local_irq_save(flags);
  40. do {
  41. last_timer_count = st_mfp.tim_dt_c;
  42. } while (last_timer_count == 1);
  43. clk_total += INT_TICKS;
  44. legacy_timer_tick(1);
  45. timer_heartbeat();
  46. local_irq_restore(flags);
  47. return IRQ_HANDLED;
  48. }
  49. void __init
  50. atari_sched_init(void)
  51. {
  52. /* set Timer C data Register */
  53. st_mfp.tim_dt_c = INT_TICKS;
  54. /* start timer C, div = 1:100 */
  55. st_mfp.tim_ct_cd = (st_mfp.tim_ct_cd & 15) | 0x60;
  56. /* install interrupt service routine for MFP Timer C */
  57. if (request_irq(IRQ_MFP_TIMC, mfp_timer_c_handler, IRQF_TIMER, "timer",
  58. NULL))
  59. pr_err("Couldn't register timer interrupt\n");
  60. clocksource_register_hz(&atari_clk, INT_CLK);
  61. }
  62. /* ++andreas: gettimeoffset fixed to check for pending interrupt */
  63. static u64 atari_read_clk(struct clocksource *cs)
  64. {
  65. unsigned long flags;
  66. u8 count;
  67. u32 ticks;
  68. local_irq_save(flags);
  69. /* Ensure that the count is monotonically decreasing, even though
  70. * the result may briefly stop changing after counter wrap-around.
  71. */
  72. count = min(st_mfp.tim_dt_c, last_timer_count);
  73. last_timer_count = count;
  74. ticks = INT_TICKS - count;
  75. ticks += clk_total;
  76. local_irq_restore(flags);
  77. return ticks;
  78. }
  79. static void mste_read(struct MSTE_RTC *val)
  80. {
  81. #define COPY(v) val->v=(mste_rtc.v & 0xf)
  82. do {
  83. COPY(sec_ones) ; COPY(sec_tens) ; COPY(min_ones) ;
  84. COPY(min_tens) ; COPY(hr_ones) ; COPY(hr_tens) ;
  85. COPY(weekday) ; COPY(day_ones) ; COPY(day_tens) ;
  86. COPY(mon_ones) ; COPY(mon_tens) ; COPY(year_ones) ;
  87. COPY(year_tens) ;
  88. /* prevent from reading the clock while it changed */
  89. } while (val->sec_ones != (mste_rtc.sec_ones & 0xf));
  90. #undef COPY
  91. }
  92. static void mste_write(struct MSTE_RTC *val)
  93. {
  94. #define COPY(v) mste_rtc.v=val->v
  95. do {
  96. COPY(sec_ones) ; COPY(sec_tens) ; COPY(min_ones) ;
  97. COPY(min_tens) ; COPY(hr_ones) ; COPY(hr_tens) ;
  98. COPY(weekday) ; COPY(day_ones) ; COPY(day_tens) ;
  99. COPY(mon_ones) ; COPY(mon_tens) ; COPY(year_ones) ;
  100. COPY(year_tens) ;
  101. /* prevent from writing the clock while it changed */
  102. } while (val->sec_ones != (mste_rtc.sec_ones & 0xf));
  103. #undef COPY
  104. }
  105. #define RTC_READ(reg) \
  106. ({ unsigned char __val; \
  107. (void) atari_writeb(reg,&tt_rtc.regsel); \
  108. __val = tt_rtc.data; \
  109. __val; \
  110. })
  111. #define RTC_WRITE(reg,val) \
  112. do { \
  113. atari_writeb(reg,&tt_rtc.regsel); \
  114. tt_rtc.data = (val); \
  115. } while(0)
  116. #define HWCLK_POLL_INTERVAL 5
  117. int atari_mste_hwclk( int op, struct rtc_time *t )
  118. {
  119. int hour, year;
  120. int hr24=0;
  121. struct MSTE_RTC val;
  122. mste_rtc.mode=(mste_rtc.mode | 1);
  123. hr24=mste_rtc.mon_tens & 1;
  124. mste_rtc.mode=(mste_rtc.mode & ~1);
  125. if (op) {
  126. /* write: prepare values */
  127. val.sec_ones = t->tm_sec % 10;
  128. val.sec_tens = t->tm_sec / 10;
  129. val.min_ones = t->tm_min % 10;
  130. val.min_tens = t->tm_min / 10;
  131. hour = t->tm_hour;
  132. if (!hr24) {
  133. if (hour > 11)
  134. hour += 20 - 12;
  135. if (hour == 0 || hour == 20)
  136. hour += 12;
  137. }
  138. val.hr_ones = hour % 10;
  139. val.hr_tens = hour / 10;
  140. val.day_ones = t->tm_mday % 10;
  141. val.day_tens = t->tm_mday / 10;
  142. val.mon_ones = (t->tm_mon+1) % 10;
  143. val.mon_tens = (t->tm_mon+1) / 10;
  144. year = t->tm_year - 80;
  145. val.year_ones = year % 10;
  146. val.year_tens = year / 10;
  147. val.weekday = t->tm_wday;
  148. mste_write(&val);
  149. mste_rtc.mode=(mste_rtc.mode | 1);
  150. val.year_ones = (year % 4); /* leap year register */
  151. mste_rtc.mode=(mste_rtc.mode & ~1);
  152. }
  153. else {
  154. mste_read(&val);
  155. t->tm_sec = val.sec_ones + val.sec_tens * 10;
  156. t->tm_min = val.min_ones + val.min_tens * 10;
  157. hour = val.hr_ones + val.hr_tens * 10;
  158. if (!hr24) {
  159. if (hour == 12 || hour == 12 + 20)
  160. hour -= 12;
  161. if (hour >= 20)
  162. hour += 12 - 20;
  163. }
  164. t->tm_hour = hour;
  165. t->tm_mday = val.day_ones + val.day_tens * 10;
  166. t->tm_mon = val.mon_ones + val.mon_tens * 10 - 1;
  167. t->tm_year = val.year_ones + val.year_tens * 10 + 80;
  168. t->tm_wday = val.weekday;
  169. }
  170. return 0;
  171. }
  172. int atari_tt_hwclk( int op, struct rtc_time *t )
  173. {
  174. int sec=0, min=0, hour=0, day=0, mon=0, year=0, wday=0;
  175. unsigned long flags;
  176. unsigned char ctrl;
  177. int pm = 0;
  178. ctrl = RTC_READ(RTC_CONTROL); /* control registers are
  179. * independent from the UIP */
  180. if (op) {
  181. /* write: prepare values */
  182. sec = t->tm_sec;
  183. min = t->tm_min;
  184. hour = t->tm_hour;
  185. day = t->tm_mday;
  186. mon = t->tm_mon + 1;
  187. year = t->tm_year - atari_rtc_year_offset;
  188. wday = t->tm_wday + (t->tm_wday >= 0);
  189. if (!(ctrl & RTC_24H)) {
  190. if (hour > 11) {
  191. pm = 0x80;
  192. if (hour != 12)
  193. hour -= 12;
  194. }
  195. else if (hour == 0)
  196. hour = 12;
  197. }
  198. if (!(ctrl & RTC_DM_BINARY)) {
  199. sec = bin2bcd(sec);
  200. min = bin2bcd(min);
  201. hour = bin2bcd(hour);
  202. day = bin2bcd(day);
  203. mon = bin2bcd(mon);
  204. year = bin2bcd(year);
  205. if (wday >= 0)
  206. wday = bin2bcd(wday);
  207. }
  208. }
  209. /* Reading/writing the clock registers is a bit critical due to
  210. * the regular update cycle of the RTC. While an update is in
  211. * progress, registers 0..9 shouldn't be touched.
  212. * The problem is solved like that: If an update is currently in
  213. * progress (the UIP bit is set), the process sleeps for a while
  214. * (50ms). This really should be enough, since the update cycle
  215. * normally needs 2 ms.
  216. * If the UIP bit reads as 0, we have at least 244 usecs until the
  217. * update starts. This should be enough... But to be sure,
  218. * additionally the RTC_SET bit is set to prevent an update cycle.
  219. */
  220. while( RTC_READ(RTC_FREQ_SELECT) & RTC_UIP ) {
  221. if (in_atomic() || irqs_disabled())
  222. mdelay(1);
  223. else
  224. schedule_timeout_interruptible(HWCLK_POLL_INTERVAL);
  225. }
  226. local_irq_save(flags);
  227. RTC_WRITE( RTC_CONTROL, ctrl | RTC_SET );
  228. if (!op) {
  229. sec = RTC_READ( RTC_SECONDS );
  230. min = RTC_READ( RTC_MINUTES );
  231. hour = RTC_READ( RTC_HOURS );
  232. day = RTC_READ( RTC_DAY_OF_MONTH );
  233. mon = RTC_READ( RTC_MONTH );
  234. year = RTC_READ( RTC_YEAR );
  235. wday = RTC_READ( RTC_DAY_OF_WEEK );
  236. }
  237. else {
  238. RTC_WRITE( RTC_SECONDS, sec );
  239. RTC_WRITE( RTC_MINUTES, min );
  240. RTC_WRITE( RTC_HOURS, hour + pm);
  241. RTC_WRITE( RTC_DAY_OF_MONTH, day );
  242. RTC_WRITE( RTC_MONTH, mon );
  243. RTC_WRITE( RTC_YEAR, year );
  244. if (wday >= 0) RTC_WRITE( RTC_DAY_OF_WEEK, wday );
  245. }
  246. RTC_WRITE( RTC_CONTROL, ctrl & ~RTC_SET );
  247. local_irq_restore(flags);
  248. if (!op) {
  249. /* read: adjust values */
  250. if (hour & 0x80) {
  251. hour &= ~0x80;
  252. pm = 1;
  253. }
  254. if (!(ctrl & RTC_DM_BINARY)) {
  255. sec = bcd2bin(sec);
  256. min = bcd2bin(min);
  257. hour = bcd2bin(hour);
  258. day = bcd2bin(day);
  259. mon = bcd2bin(mon);
  260. year = bcd2bin(year);
  261. wday = bcd2bin(wday);
  262. }
  263. if (!(ctrl & RTC_24H)) {
  264. if (!pm && hour == 12)
  265. hour = 0;
  266. else if (pm && hour != 12)
  267. hour += 12;
  268. }
  269. t->tm_sec = sec;
  270. t->tm_min = min;
  271. t->tm_hour = hour;
  272. t->tm_mday = day;
  273. t->tm_mon = mon - 1;
  274. t->tm_year = year + atari_rtc_year_offset;
  275. t->tm_wday = wday - 1;
  276. }
  277. return( 0 );
  278. }