time.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /* $Id: time.c,v 1.60 2002/01/23 14:33:55 davem Exp $
  2. * linux/arch/sparc/kernel/time.c
  3. *
  4. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
  6. *
  7. * Chris Davis (cdavis@cois.on.ca) 03/27/1998
  8. * Added support for the intersil on the sun4/4200
  9. *
  10. * Gleb Raiko (rajko@mech.math.msu.su) 08/18/1998
  11. * Support for MicroSPARC-IIep, PCI CPU.
  12. *
  13. * This file handles the Sparc specific time handling details.
  14. *
  15. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  16. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  17. */
  18. #include <linux/config.h>
  19. #include <linux/errno.h>
  20. #include <linux/module.h>
  21. #include <linux/sched.h>
  22. #include <linux/kernel.h>
  23. #include <linux/param.h>
  24. #include <linux/string.h>
  25. #include <linux/mm.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/time.h>
  28. #include <linux/timex.h>
  29. #include <linux/init.h>
  30. #include <linux/pci.h>
  31. #include <linux/ioport.h>
  32. #include <linux/profile.h>
  33. #include <asm/oplib.h>
  34. #include <asm/timer.h>
  35. #include <asm/mostek.h>
  36. #include <asm/system.h>
  37. #include <asm/irq.h>
  38. #include <asm/io.h>
  39. #include <asm/idprom.h>
  40. #include <asm/machines.h>
  41. #include <asm/sun4paddr.h>
  42. #include <asm/page.h>
  43. #include <asm/pcic.h>
  44. extern unsigned long wall_jiffies;
  45. u64 jiffies_64 = INITIAL_JIFFIES;
  46. EXPORT_SYMBOL(jiffies_64);
  47. DEFINE_SPINLOCK(rtc_lock);
  48. enum sparc_clock_type sp_clock_typ;
  49. DEFINE_SPINLOCK(mostek_lock);
  50. void __iomem *mstk48t02_regs = NULL;
  51. static struct mostek48t08 *mstk48t08_regs = NULL;
  52. static int set_rtc_mmss(unsigned long);
  53. static int sbus_do_settimeofday(struct timespec *tv);
  54. #ifdef CONFIG_SUN4
  55. struct intersil *intersil_clock;
  56. #define intersil_cmd(intersil_reg, intsil_cmd) intersil_reg->int_cmd_reg = \
  57. (intsil_cmd)
  58. #define intersil_intr(intersil_reg, intsil_cmd) intersil_reg->int_intr_reg = \
  59. (intsil_cmd)
  60. #define intersil_start(intersil_reg) intersil_cmd(intersil_reg, \
  61. ( INTERSIL_START | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
  62. INTERSIL_INTR_ENABLE))
  63. #define intersil_stop(intersil_reg) intersil_cmd(intersil_reg, \
  64. ( INTERSIL_STOP | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
  65. INTERSIL_INTR_ENABLE))
  66. #define intersil_read_intr(intersil_reg, towhere) towhere = \
  67. intersil_reg->int_intr_reg
  68. #endif
  69. unsigned long profile_pc(struct pt_regs *regs)
  70. {
  71. extern char __copy_user_begin[], __copy_user_end[];
  72. extern char __atomic_begin[], __atomic_end[];
  73. extern char __bzero_begin[], __bzero_end[];
  74. extern char __bitops_begin[], __bitops_end[];
  75. unsigned long pc = regs->pc;
  76. if (in_lock_functions(pc) ||
  77. (pc >= (unsigned long) __copy_user_begin &&
  78. pc < (unsigned long) __copy_user_end) ||
  79. (pc >= (unsigned long) __atomic_begin &&
  80. pc < (unsigned long) __atomic_end) ||
  81. (pc >= (unsigned long) __bzero_begin &&
  82. pc < (unsigned long) __bzero_end) ||
  83. (pc >= (unsigned long) __bitops_begin &&
  84. pc < (unsigned long) __bitops_end))
  85. pc = regs->u_regs[UREG_RETPC];
  86. return pc;
  87. }
  88. __volatile__ unsigned int *master_l10_counter;
  89. __volatile__ unsigned int *master_l10_limit;
  90. /*
  91. * timer_interrupt() needs to keep up the real-time clock,
  92. * as well as call the "do_timer()" routine every clocktick
  93. */
  94. #define TICK_SIZE (tick_nsec / 1000)
  95. irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  96. {
  97. /* last time the cmos clock got updated */
  98. static long last_rtc_update;
  99. #ifndef CONFIG_SMP
  100. profile_tick(CPU_PROFILING, regs);
  101. #endif
  102. /* Protect counter clear so that do_gettimeoffset works */
  103. write_seqlock(&xtime_lock);
  104. #ifdef CONFIG_SUN4
  105. if((idprom->id_machtype == (SM_SUN4 | SM_4_260)) ||
  106. (idprom->id_machtype == (SM_SUN4 | SM_4_110))) {
  107. int temp;
  108. intersil_read_intr(intersil_clock, temp);
  109. /* re-enable the irq */
  110. enable_pil_irq(10);
  111. }
  112. #endif
  113. clear_clock_irq();
  114. do_timer(regs);
  115. #ifndef CONFIG_SMP
  116. update_process_times(user_mode(regs));
  117. #endif
  118. /* Determine when to update the Mostek clock. */
  119. if (ntp_synced() &&
  120. xtime.tv_sec > last_rtc_update + 660 &&
  121. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  122. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  123. if (set_rtc_mmss(xtime.tv_sec) == 0)
  124. last_rtc_update = xtime.tv_sec;
  125. else
  126. last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
  127. }
  128. write_sequnlock(&xtime_lock);
  129. return IRQ_HANDLED;
  130. }
  131. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  132. static void __init kick_start_clock(void)
  133. {
  134. struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
  135. unsigned char sec;
  136. int i, count;
  137. prom_printf("CLOCK: Clock was stopped. Kick start ");
  138. spin_lock_irq(&mostek_lock);
  139. /* Turn on the kick start bit to start the oscillator. */
  140. regs->creg |= MSTK_CREG_WRITE;
  141. regs->sec &= ~MSTK_STOP;
  142. regs->hour |= MSTK_KICK_START;
  143. regs->creg &= ~MSTK_CREG_WRITE;
  144. spin_unlock_irq(&mostek_lock);
  145. /* Delay to allow the clock oscillator to start. */
  146. sec = MSTK_REG_SEC(regs);
  147. for (i = 0; i < 3; i++) {
  148. while (sec == MSTK_REG_SEC(regs))
  149. for (count = 0; count < 100000; count++)
  150. /* nothing */ ;
  151. prom_printf(".");
  152. sec = regs->sec;
  153. }
  154. prom_printf("\n");
  155. spin_lock_irq(&mostek_lock);
  156. /* Turn off kick start and set a "valid" time and date. */
  157. regs->creg |= MSTK_CREG_WRITE;
  158. regs->hour &= ~MSTK_KICK_START;
  159. MSTK_SET_REG_SEC(regs,0);
  160. MSTK_SET_REG_MIN(regs,0);
  161. MSTK_SET_REG_HOUR(regs,0);
  162. MSTK_SET_REG_DOW(regs,5);
  163. MSTK_SET_REG_DOM(regs,1);
  164. MSTK_SET_REG_MONTH(regs,8);
  165. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  166. regs->creg &= ~MSTK_CREG_WRITE;
  167. spin_unlock_irq(&mostek_lock);
  168. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  169. while (regs->hour & MSTK_KICK_START) {
  170. prom_printf("CLOCK: Kick start still on!\n");
  171. spin_lock_irq(&mostek_lock);
  172. regs->creg |= MSTK_CREG_WRITE;
  173. regs->hour &= ~MSTK_KICK_START;
  174. regs->creg &= ~MSTK_CREG_WRITE;
  175. spin_unlock_irq(&mostek_lock);
  176. }
  177. prom_printf("CLOCK: Kick start procedure successful.\n");
  178. }
  179. /* Return nonzero if the clock chip battery is low. */
  180. static __inline__ int has_low_battery(void)
  181. {
  182. struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
  183. unsigned char data1, data2;
  184. spin_lock_irq(&mostek_lock);
  185. data1 = regs->eeprom[0]; /* Read some data. */
  186. regs->eeprom[0] = ~data1; /* Write back the complement. */
  187. data2 = regs->eeprom[0]; /* Read back the complement. */
  188. regs->eeprom[0] = data1; /* Restore the original value. */
  189. spin_unlock_irq(&mostek_lock);
  190. return (data1 == data2); /* Was the write blocked? */
  191. }
  192. /* Probe for the real time clock chip on Sun4 */
  193. static __inline__ void sun4_clock_probe(void)
  194. {
  195. #ifdef CONFIG_SUN4
  196. int temp;
  197. struct resource r;
  198. memset(&r, 0, sizeof(r));
  199. if( idprom->id_machtype == (SM_SUN4 | SM_4_330) ) {
  200. sp_clock_typ = MSTK48T02;
  201. r.start = sun4_clock_physaddr;
  202. mstk48t02_regs = sbus_ioremap(&r, 0,
  203. sizeof(struct mostek48t02), NULL);
  204. mstk48t08_regs = NULL; /* To catch weirdness */
  205. intersil_clock = NULL; /* just in case */
  206. /* Kick start the clock if it is completely stopped. */
  207. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  208. kick_start_clock();
  209. } else if( idprom->id_machtype == (SM_SUN4 | SM_4_260)) {
  210. /* intersil setup code */
  211. printk("Clock: INTERSIL at %8x ",sun4_clock_physaddr);
  212. sp_clock_typ = INTERSIL;
  213. r.start = sun4_clock_physaddr;
  214. intersil_clock = (struct intersil *)
  215. sbus_ioremap(&r, 0, sizeof(*intersil_clock), "intersil");
  216. mstk48t02_regs = 0; /* just be sure */
  217. mstk48t08_regs = NULL; /* ditto */
  218. /* initialise the clock */
  219. intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
  220. intersil_start(intersil_clock);
  221. intersil_read_intr(intersil_clock, temp);
  222. while (!(temp & 0x80))
  223. intersil_read_intr(intersil_clock, temp);
  224. intersil_read_intr(intersil_clock, temp);
  225. while (!(temp & 0x80))
  226. intersil_read_intr(intersil_clock, temp);
  227. intersil_stop(intersil_clock);
  228. }
  229. #endif
  230. }
  231. /* Probe for the mostek real time clock chip. */
  232. static __inline__ void clock_probe(void)
  233. {
  234. struct linux_prom_registers clk_reg[2];
  235. char model[128];
  236. register int node, cpuunit, bootbus;
  237. struct resource r;
  238. cpuunit = bootbus = 0;
  239. memset(&r, 0, sizeof(r));
  240. /* Determine the correct starting PROM node for the probe. */
  241. node = prom_getchild(prom_root_node);
  242. switch (sparc_cpu_model) {
  243. case sun4c:
  244. break;
  245. case sun4m:
  246. node = prom_getchild(prom_searchsiblings(node, "obio"));
  247. break;
  248. case sun4d:
  249. node = prom_getchild(bootbus = prom_searchsiblings(prom_getchild(cpuunit = prom_searchsiblings(node, "cpu-unit")), "bootbus"));
  250. break;
  251. default:
  252. prom_printf("CLOCK: Unsupported architecture!\n");
  253. prom_halt();
  254. }
  255. /* Find the PROM node describing the real time clock. */
  256. sp_clock_typ = MSTK_INVALID;
  257. node = prom_searchsiblings(node,"eeprom");
  258. if (!node) {
  259. prom_printf("CLOCK: No clock found!\n");
  260. prom_halt();
  261. }
  262. /* Get the model name and setup everything up. */
  263. model[0] = '\0';
  264. prom_getstring(node, "model", model, sizeof(model));
  265. if (strcmp(model, "mk48t02") == 0) {
  266. sp_clock_typ = MSTK48T02;
  267. if (prom_getproperty(node, "reg", (char *) clk_reg, sizeof(clk_reg)) == -1) {
  268. prom_printf("clock_probe: FAILED!\n");
  269. prom_halt();
  270. }
  271. if (sparc_cpu_model == sun4d)
  272. prom_apply_generic_ranges (bootbus, cpuunit, clk_reg, 1);
  273. else
  274. prom_apply_obio_ranges(clk_reg, 1);
  275. /* Map the clock register io area read-only */
  276. r.flags = clk_reg[0].which_io;
  277. r.start = clk_reg[0].phys_addr;
  278. mstk48t02_regs = sbus_ioremap(&r, 0,
  279. sizeof(struct mostek48t02), "mk48t02");
  280. mstk48t08_regs = NULL; /* To catch weirdness */
  281. } else if (strcmp(model, "mk48t08") == 0) {
  282. sp_clock_typ = MSTK48T08;
  283. if(prom_getproperty(node, "reg", (char *) clk_reg,
  284. sizeof(clk_reg)) == -1) {
  285. prom_printf("clock_probe: FAILED!\n");
  286. prom_halt();
  287. }
  288. if (sparc_cpu_model == sun4d)
  289. prom_apply_generic_ranges (bootbus, cpuunit, clk_reg, 1);
  290. else
  291. prom_apply_obio_ranges(clk_reg, 1);
  292. /* Map the clock register io area read-only */
  293. /* XXX r/o attribute is somewhere in r.flags */
  294. r.flags = clk_reg[0].which_io;
  295. r.start = clk_reg[0].phys_addr;
  296. mstk48t08_regs = (struct mostek48t08 *) sbus_ioremap(&r, 0,
  297. sizeof(struct mostek48t08), "mk48t08");
  298. mstk48t02_regs = &mstk48t08_regs->regs;
  299. } else {
  300. prom_printf("CLOCK: Unknown model name '%s'\n",model);
  301. prom_halt();
  302. }
  303. /* Report a low battery voltage condition. */
  304. if (has_low_battery())
  305. printk(KERN_CRIT "NVRAM: Low battery voltage!\n");
  306. /* Kick start the clock if it is completely stopped. */
  307. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  308. kick_start_clock();
  309. }
  310. void __init sbus_time_init(void)
  311. {
  312. unsigned int year, mon, day, hour, min, sec;
  313. struct mostek48t02 *mregs;
  314. #ifdef CONFIG_SUN4
  315. int temp;
  316. struct intersil *iregs;
  317. #endif
  318. BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM);
  319. btfixup();
  320. if (ARCH_SUN4)
  321. sun4_clock_probe();
  322. else
  323. clock_probe();
  324. sparc_init_timers(timer_interrupt);
  325. #ifdef CONFIG_SUN4
  326. if(idprom->id_machtype == (SM_SUN4 | SM_4_330)) {
  327. #endif
  328. mregs = (struct mostek48t02 *)mstk48t02_regs;
  329. if(!mregs) {
  330. prom_printf("Something wrong, clock regs not mapped yet.\n");
  331. prom_halt();
  332. }
  333. spin_lock_irq(&mostek_lock);
  334. mregs->creg |= MSTK_CREG_READ;
  335. sec = MSTK_REG_SEC(mregs);
  336. min = MSTK_REG_MIN(mregs);
  337. hour = MSTK_REG_HOUR(mregs);
  338. day = MSTK_REG_DOM(mregs);
  339. mon = MSTK_REG_MONTH(mregs);
  340. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  341. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  342. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  343. set_normalized_timespec(&wall_to_monotonic,
  344. -xtime.tv_sec, -xtime.tv_nsec);
  345. mregs->creg &= ~MSTK_CREG_READ;
  346. spin_unlock_irq(&mostek_lock);
  347. #ifdef CONFIG_SUN4
  348. } else if(idprom->id_machtype == (SM_SUN4 | SM_4_260) ) {
  349. /* initialise the intersil on sun4 */
  350. iregs=intersil_clock;
  351. if(!iregs) {
  352. prom_printf("Something wrong, clock regs not mapped yet.\n");
  353. prom_halt();
  354. }
  355. intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
  356. disable_pil_irq(10);
  357. intersil_stop(iregs);
  358. intersil_read_intr(intersil_clock, temp);
  359. temp = iregs->clk.int_csec;
  360. sec = iregs->clk.int_sec;
  361. min = iregs->clk.int_min;
  362. hour = iregs->clk.int_hour;
  363. day = iregs->clk.int_day;
  364. mon = iregs->clk.int_month;
  365. year = MSTK_CVT_YEAR(iregs->clk.int_year);
  366. enable_pil_irq(10);
  367. intersil_start(iregs);
  368. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  369. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  370. set_normalized_timespec(&wall_to_monotonic,
  371. -xtime.tv_sec, -xtime.tv_nsec);
  372. printk("%u/%u/%u %u:%u:%u\n",day,mon,year,hour,min,sec);
  373. }
  374. #endif
  375. /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */
  376. local_irq_enable();
  377. }
  378. void __init time_init(void)
  379. {
  380. #ifdef CONFIG_PCI
  381. extern void pci_time_init(void);
  382. if (pcic_present()) {
  383. pci_time_init();
  384. return;
  385. }
  386. #endif
  387. sbus_time_init();
  388. }
  389. extern __inline__ unsigned long do_gettimeoffset(void)
  390. {
  391. return (*master_l10_counter >> 10) & 0x1fffff;
  392. }
  393. /*
  394. * Returns nanoseconds
  395. * XXX This is a suboptimal implementation.
  396. */
  397. unsigned long long sched_clock(void)
  398. {
  399. return (unsigned long long)jiffies * (1000000000 / HZ);
  400. }
  401. /* Ok, my cute asm atomicity trick doesn't work anymore.
  402. * There are just too many variables that need to be protected
  403. * now (both members of xtime, wall_jiffies, et al.)
  404. */
  405. void do_gettimeofday(struct timeval *tv)
  406. {
  407. unsigned long flags;
  408. unsigned long seq;
  409. unsigned long usec, sec;
  410. unsigned long max_ntp_tick = tick_usec - tickadj;
  411. do {
  412. unsigned long lost;
  413. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  414. usec = do_gettimeoffset();
  415. lost = jiffies - wall_jiffies;
  416. /*
  417. * If time_adjust is negative then NTP is slowing the clock
  418. * so make sure not to go into next possible interval.
  419. * Better to lose some accuracy than have time go backwards..
  420. */
  421. if (unlikely(time_adjust < 0)) {
  422. usec = min(usec, max_ntp_tick);
  423. if (lost)
  424. usec += lost * max_ntp_tick;
  425. }
  426. else if (unlikely(lost))
  427. usec += lost * tick_usec;
  428. sec = xtime.tv_sec;
  429. usec += (xtime.tv_nsec / 1000);
  430. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  431. while (usec >= 1000000) {
  432. usec -= 1000000;
  433. sec++;
  434. }
  435. tv->tv_sec = sec;
  436. tv->tv_usec = usec;
  437. }
  438. EXPORT_SYMBOL(do_gettimeofday);
  439. int do_settimeofday(struct timespec *tv)
  440. {
  441. int ret;
  442. write_seqlock_irq(&xtime_lock);
  443. ret = bus_do_settimeofday(tv);
  444. write_sequnlock_irq(&xtime_lock);
  445. clock_was_set();
  446. return ret;
  447. }
  448. EXPORT_SYMBOL(do_settimeofday);
  449. static int sbus_do_settimeofday(struct timespec *tv)
  450. {
  451. time_t wtm_sec, sec = tv->tv_sec;
  452. long wtm_nsec, nsec = tv->tv_nsec;
  453. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  454. return -EINVAL;
  455. /*
  456. * This is revolting. We need to set "xtime" correctly. However, the
  457. * value in this location is the value at the most recent update of
  458. * wall time. Discover what correction gettimeofday() would have
  459. * made, and then undo it!
  460. */
  461. nsec -= 1000 * (do_gettimeoffset() +
  462. (jiffies - wall_jiffies) * (USEC_PER_SEC / HZ));
  463. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  464. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  465. set_normalized_timespec(&xtime, sec, nsec);
  466. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  467. ntp_clear();
  468. return 0;
  469. }
  470. /*
  471. * BUG: This routine does not handle hour overflow properly; it just
  472. * sets the minutes. Usually you won't notice until after reboot!
  473. */
  474. static int set_rtc_mmss(unsigned long nowtime)
  475. {
  476. int real_seconds, real_minutes, mostek_minutes;
  477. struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
  478. unsigned long flags;
  479. #ifdef CONFIG_SUN4
  480. struct intersil *iregs = intersil_clock;
  481. int temp;
  482. #endif
  483. /* Not having a register set can lead to trouble. */
  484. if (!regs) {
  485. #ifdef CONFIG_SUN4
  486. if(!iregs)
  487. return -1;
  488. else {
  489. temp = iregs->clk.int_csec;
  490. mostek_minutes = iregs->clk.int_min;
  491. real_seconds = nowtime % 60;
  492. real_minutes = nowtime / 60;
  493. if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
  494. real_minutes += 30; /* correct for half hour time zone */
  495. real_minutes %= 60;
  496. if (abs(real_minutes - mostek_minutes) < 30) {
  497. intersil_stop(iregs);
  498. iregs->clk.int_sec=real_seconds;
  499. iregs->clk.int_min=real_minutes;
  500. intersil_start(iregs);
  501. } else {
  502. printk(KERN_WARNING
  503. "set_rtc_mmss: can't update from %d to %d\n",
  504. mostek_minutes, real_minutes);
  505. return -1;
  506. }
  507. return 0;
  508. }
  509. #endif
  510. }
  511. spin_lock_irqsave(&mostek_lock, flags);
  512. /* Read the current RTC minutes. */
  513. regs->creg |= MSTK_CREG_READ;
  514. mostek_minutes = MSTK_REG_MIN(regs);
  515. regs->creg &= ~MSTK_CREG_READ;
  516. /*
  517. * since we're only adjusting minutes and seconds,
  518. * don't interfere with hour overflow. This avoids
  519. * messing with unknown time zones but requires your
  520. * RTC not to be off by more than 15 minutes
  521. */
  522. real_seconds = nowtime % 60;
  523. real_minutes = nowtime / 60;
  524. if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
  525. real_minutes += 30; /* correct for half hour time zone */
  526. real_minutes %= 60;
  527. if (abs(real_minutes - mostek_minutes) < 30) {
  528. regs->creg |= MSTK_CREG_WRITE;
  529. MSTK_SET_REG_SEC(regs,real_seconds);
  530. MSTK_SET_REG_MIN(regs,real_minutes);
  531. regs->creg &= ~MSTK_CREG_WRITE;
  532. spin_unlock_irqrestore(&mostek_lock, flags);
  533. return 0;
  534. } else {
  535. spin_unlock_irqrestore(&mostek_lock, flags);
  536. return -1;
  537. }
  538. }