time.c 8.3 KB

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