time.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* linux/arch/sparc/kernel/time.c
  2. *
  3. * Copyright (C) 1995 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
  5. *
  6. * Chris Davis (cdavis@cois.on.ca) 03/27/1998
  7. * Added support for the intersil on the sun4/4200
  8. *
  9. * Gleb Raiko (rajko@mech.math.msu.su) 08/18/1998
  10. * Support for MicroSPARC-IIep, PCI CPU.
  11. *
  12. * This file handles the Sparc specific time handling details.
  13. *
  14. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  15. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/module.h>
  19. #include <linux/sched.h>
  20. #include <linux/kernel.h>
  21. #include <linux/param.h>
  22. #include <linux/string.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/time.h>
  26. #include <linux/timex.h>
  27. #include <linux/init.h>
  28. #include <linux/pci.h>
  29. #include <linux/ioport.h>
  30. #include <linux/profile.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.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/page.h>
  42. #include <asm/pcic.h>
  43. #include <asm/irq_regs.h>
  44. #include "irq.h"
  45. DEFINE_SPINLOCK(rtc_lock);
  46. static enum sparc_clock_type sp_clock_typ;
  47. DEFINE_SPINLOCK(mostek_lock);
  48. void __iomem *mstk48t02_regs = NULL;
  49. static struct mostek48t08 __iomem *mstk48t08_regs = NULL;
  50. static int set_rtc_mmss(unsigned long);
  51. static int sbus_do_settimeofday(struct timespec *tv);
  52. unsigned long profile_pc(struct pt_regs *regs)
  53. {
  54. extern char __copy_user_begin[], __copy_user_end[];
  55. extern char __atomic_begin[], __atomic_end[];
  56. extern char __bzero_begin[], __bzero_end[];
  57. unsigned long pc = regs->pc;
  58. if (in_lock_functions(pc) ||
  59. (pc >= (unsigned long) __copy_user_begin &&
  60. pc < (unsigned long) __copy_user_end) ||
  61. (pc >= (unsigned long) __atomic_begin &&
  62. pc < (unsigned long) __atomic_end) ||
  63. (pc >= (unsigned long) __bzero_begin &&
  64. pc < (unsigned long) __bzero_end))
  65. pc = regs->u_regs[UREG_RETPC];
  66. return pc;
  67. }
  68. EXPORT_SYMBOL(profile_pc);
  69. __volatile__ unsigned int *master_l10_counter;
  70. __volatile__ unsigned int *master_l10_limit;
  71. /*
  72. * timer_interrupt() needs to keep up the real-time clock,
  73. * as well as call the "do_timer()" routine every clocktick
  74. */
  75. #define TICK_SIZE (tick_nsec / 1000)
  76. static irqreturn_t timer_interrupt(int dummy, void *dev_id)
  77. {
  78. /* last time the cmos clock got updated */
  79. static long last_rtc_update;
  80. #ifndef CONFIG_SMP
  81. profile_tick(CPU_PROFILING);
  82. #endif
  83. /* Protect counter clear so that do_gettimeoffset works */
  84. write_seqlock(&xtime_lock);
  85. clear_clock_irq();
  86. do_timer(1);
  87. /* Determine when to update the Mostek clock. */
  88. if (ntp_synced() &&
  89. xtime.tv_sec > last_rtc_update + 660 &&
  90. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  91. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  92. if (set_rtc_mmss(xtime.tv_sec) == 0)
  93. last_rtc_update = xtime.tv_sec;
  94. else
  95. last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
  96. }
  97. write_sequnlock(&xtime_lock);
  98. #ifndef CONFIG_SMP
  99. update_process_times(user_mode(get_irq_regs()));
  100. #endif
  101. return IRQ_HANDLED;
  102. }
  103. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  104. static void __devinit kick_start_clock(void)
  105. {
  106. struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
  107. unsigned char sec;
  108. int i, count;
  109. prom_printf("CLOCK: Clock was stopped. Kick start ");
  110. spin_lock_irq(&mostek_lock);
  111. /* Turn on the kick start bit to start the oscillator. */
  112. regs->creg |= MSTK_CREG_WRITE;
  113. regs->sec &= ~MSTK_STOP;
  114. regs->hour |= MSTK_KICK_START;
  115. regs->creg &= ~MSTK_CREG_WRITE;
  116. spin_unlock_irq(&mostek_lock);
  117. /* Delay to allow the clock oscillator to start. */
  118. sec = MSTK_REG_SEC(regs);
  119. for (i = 0; i < 3; i++) {
  120. while (sec == MSTK_REG_SEC(regs))
  121. for (count = 0; count < 100000; count++)
  122. /* nothing */ ;
  123. prom_printf(".");
  124. sec = regs->sec;
  125. }
  126. prom_printf("\n");
  127. spin_lock_irq(&mostek_lock);
  128. /* Turn off kick start and set a "valid" time and date. */
  129. regs->creg |= MSTK_CREG_WRITE;
  130. regs->hour &= ~MSTK_KICK_START;
  131. MSTK_SET_REG_SEC(regs,0);
  132. MSTK_SET_REG_MIN(regs,0);
  133. MSTK_SET_REG_HOUR(regs,0);
  134. MSTK_SET_REG_DOW(regs,5);
  135. MSTK_SET_REG_DOM(regs,1);
  136. MSTK_SET_REG_MONTH(regs,8);
  137. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  138. regs->creg &= ~MSTK_CREG_WRITE;
  139. spin_unlock_irq(&mostek_lock);
  140. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  141. while (regs->hour & MSTK_KICK_START) {
  142. prom_printf("CLOCK: Kick start still on!\n");
  143. spin_lock_irq(&mostek_lock);
  144. regs->creg |= MSTK_CREG_WRITE;
  145. regs->hour &= ~MSTK_KICK_START;
  146. regs->creg &= ~MSTK_CREG_WRITE;
  147. spin_unlock_irq(&mostek_lock);
  148. }
  149. prom_printf("CLOCK: Kick start procedure successful.\n");
  150. }
  151. /* Return nonzero if the clock chip battery is low. */
  152. static inline int has_low_battery(void)
  153. {
  154. struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
  155. unsigned char data1, data2;
  156. spin_lock_irq(&mostek_lock);
  157. data1 = regs->eeprom[0]; /* Read some data. */
  158. regs->eeprom[0] = ~data1; /* Write back the complement. */
  159. data2 = regs->eeprom[0]; /* Read back the complement. */
  160. regs->eeprom[0] = data1; /* Restore the original value. */
  161. spin_unlock_irq(&mostek_lock);
  162. return (data1 == data2); /* Was the write blocked? */
  163. }
  164. static void __devinit mostek_set_system_time(void)
  165. {
  166. unsigned int year, mon, day, hour, min, sec;
  167. struct mostek48t02 *mregs;
  168. mregs = (struct mostek48t02 *)mstk48t02_regs;
  169. if(!mregs) {
  170. prom_printf("Something wrong, clock regs not mapped yet.\n");
  171. prom_halt();
  172. }
  173. spin_lock_irq(&mostek_lock);
  174. mregs->creg |= MSTK_CREG_READ;
  175. sec = MSTK_REG_SEC(mregs);
  176. min = MSTK_REG_MIN(mregs);
  177. hour = MSTK_REG_HOUR(mregs);
  178. day = MSTK_REG_DOM(mregs);
  179. mon = MSTK_REG_MONTH(mregs);
  180. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  181. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  182. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  183. set_normalized_timespec(&wall_to_monotonic,
  184. -xtime.tv_sec, -xtime.tv_nsec);
  185. mregs->creg &= ~MSTK_CREG_READ;
  186. spin_unlock_irq(&mostek_lock);
  187. }
  188. static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
  189. {
  190. struct device_node *dp = op->node;
  191. const char *model = of_get_property(dp, "model", NULL);
  192. if (!model)
  193. return -ENODEV;
  194. if (!strcmp(model, "mk48t02")) {
  195. sp_clock_typ = MSTK48T02;
  196. /* Map the clock register io area read-only */
  197. mstk48t02_regs = of_ioremap(&op->resource[0], 0,
  198. sizeof(struct mostek48t02),
  199. "mk48t02");
  200. mstk48t08_regs = NULL; /* To catch weirdness */
  201. } else if (!strcmp(model, "mk48t08")) {
  202. sp_clock_typ = MSTK48T08;
  203. mstk48t08_regs = of_ioremap(&op->resource[0], 0,
  204. sizeof(struct mostek48t08),
  205. "mk48t08");
  206. mstk48t02_regs = &mstk48t08_regs->regs;
  207. } else
  208. return -ENODEV;
  209. /* Report a low battery voltage condition. */
  210. if (has_low_battery())
  211. printk(KERN_CRIT "NVRAM: Low battery voltage!\n");
  212. /* Kick start the clock if it is completely stopped. */
  213. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  214. kick_start_clock();
  215. mostek_set_system_time();
  216. return 0;
  217. }
  218. static struct of_device_id __initdata clock_match[] = {
  219. {
  220. .name = "eeprom",
  221. },
  222. {},
  223. };
  224. static struct of_platform_driver clock_driver = {
  225. .match_table = clock_match,
  226. .probe = clock_probe,
  227. .driver = {
  228. .name = "clock",
  229. },
  230. };
  231. /* Probe for the mostek real time clock chip. */
  232. static int __init clock_init(void)
  233. {
  234. return of_register_driver(&clock_driver, &of_platform_bus_type);
  235. }
  236. /* Must be after subsys_initcall() so that busses are probed. Must
  237. * be before device_initcall() because things like the RTC driver
  238. * need to see the clock registers.
  239. */
  240. fs_initcall(clock_init);
  241. static void __init sbus_time_init(void)
  242. {
  243. BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM);
  244. btfixup();
  245. sparc_init_timers(timer_interrupt);
  246. /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */
  247. local_irq_enable();
  248. }
  249. void __init time_init(void)
  250. {
  251. #ifdef CONFIG_PCI
  252. extern void pci_time_init(void);
  253. if (pcic_present()) {
  254. pci_time_init();
  255. return;
  256. }
  257. #endif
  258. sbus_time_init();
  259. }
  260. static inline unsigned long do_gettimeoffset(void)
  261. {
  262. unsigned long val = *master_l10_counter;
  263. unsigned long usec = (val >> 10) & 0x1fffff;
  264. /* Limit hit? */
  265. if (val & 0x80000000)
  266. usec += 1000000 / HZ;
  267. return usec;
  268. }
  269. /* Ok, my cute asm atomicity trick doesn't work anymore.
  270. * There are just too many variables that need to be protected
  271. * now (both members of xtime, et al.)
  272. */
  273. void do_gettimeofday(struct timeval *tv)
  274. {
  275. unsigned long flags;
  276. unsigned long seq;
  277. unsigned long usec, sec;
  278. unsigned long max_ntp_tick = tick_usec - tickadj;
  279. do {
  280. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  281. usec = do_gettimeoffset();
  282. /*
  283. * If time_adjust is negative then NTP is slowing the clock
  284. * so make sure not to go into next possible interval.
  285. * Better to lose some accuracy than have time go backwards..
  286. */
  287. if (unlikely(time_adjust < 0))
  288. usec = min(usec, max_ntp_tick);
  289. sec = xtime.tv_sec;
  290. usec += (xtime.tv_nsec / 1000);
  291. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  292. while (usec >= 1000000) {
  293. usec -= 1000000;
  294. sec++;
  295. }
  296. tv->tv_sec = sec;
  297. tv->tv_usec = usec;
  298. }
  299. EXPORT_SYMBOL(do_gettimeofday);
  300. int do_settimeofday(struct timespec *tv)
  301. {
  302. int ret;
  303. write_seqlock_irq(&xtime_lock);
  304. ret = bus_do_settimeofday(tv);
  305. write_sequnlock_irq(&xtime_lock);
  306. clock_was_set();
  307. return ret;
  308. }
  309. EXPORT_SYMBOL(do_settimeofday);
  310. static int sbus_do_settimeofday(struct timespec *tv)
  311. {
  312. time_t wtm_sec, sec = tv->tv_sec;
  313. long wtm_nsec, nsec = tv->tv_nsec;
  314. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  315. return -EINVAL;
  316. /*
  317. * This is revolting. We need to set "xtime" correctly. However, the
  318. * value in this location is the value at the most recent update of
  319. * wall time. Discover what correction gettimeofday() would have
  320. * made, and then undo it!
  321. */
  322. nsec -= 1000 * do_gettimeoffset();
  323. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  324. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  325. set_normalized_timespec(&xtime, sec, nsec);
  326. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  327. ntp_clear();
  328. return 0;
  329. }
  330. /*
  331. * BUG: This routine does not handle hour overflow properly; it just
  332. * sets the minutes. Usually you won't notice until after reboot!
  333. */
  334. static int set_rtc_mmss(unsigned long nowtime)
  335. {
  336. int real_seconds, real_minutes, mostek_minutes;
  337. struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
  338. unsigned long flags;
  339. spin_lock_irqsave(&mostek_lock, flags);
  340. /* Read the current RTC minutes. */
  341. regs->creg |= MSTK_CREG_READ;
  342. mostek_minutes = MSTK_REG_MIN(regs);
  343. regs->creg &= ~MSTK_CREG_READ;
  344. /*
  345. * since we're only adjusting minutes and seconds,
  346. * don't interfere with hour overflow. This avoids
  347. * messing with unknown time zones but requires your
  348. * RTC not to be off by more than 15 minutes
  349. */
  350. real_seconds = nowtime % 60;
  351. real_minutes = nowtime / 60;
  352. if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
  353. real_minutes += 30; /* correct for half hour time zone */
  354. real_minutes %= 60;
  355. if (abs(real_minutes - mostek_minutes) < 30) {
  356. regs->creg |= MSTK_CREG_WRITE;
  357. MSTK_SET_REG_SEC(regs,real_seconds);
  358. MSTK_SET_REG_MIN(regs,real_minutes);
  359. regs->creg &= ~MSTK_CREG_WRITE;
  360. spin_unlock_irqrestore(&mostek_lock, flags);
  361. return 0;
  362. } else {
  363. spin_unlock_irqrestore(&mostek_lock, flags);
  364. return -1;
  365. }
  366. }