time.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. /* $Id: time.c,v 1.42 2002/01/23 14:33:55 davem Exp $
  2. * time.c: UltraSparc timer and TOD clock support.
  3. *
  4. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
  6. *
  7. * Based largely on code which is:
  8. *
  9. * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/param.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/time.h>
  20. #include <linux/timex.h>
  21. #include <linux/init.h>
  22. #include <linux/ioport.h>
  23. #include <linux/mc146818rtc.h>
  24. #include <linux/delay.h>
  25. #include <linux/profile.h>
  26. #include <linux/bcd.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/percpu.h>
  30. #include <linux/profile.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/rtc.h>
  33. #include <asm/oplib.h>
  34. #include <asm/mostek.h>
  35. #include <asm/timer.h>
  36. #include <asm/irq.h>
  37. #include <asm/io.h>
  38. #include <asm/prom.h>
  39. #include <asm/of_device.h>
  40. #include <asm/starfire.h>
  41. #include <asm/smp.h>
  42. #include <asm/sections.h>
  43. #include <asm/cpudata.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/prom.h>
  46. #include <asm/irq_regs.h>
  47. DEFINE_SPINLOCK(mostek_lock);
  48. DEFINE_SPINLOCK(rtc_lock);
  49. void __iomem *mstk48t02_regs = NULL;
  50. #ifdef CONFIG_PCI
  51. unsigned long ds1287_regs = 0UL;
  52. #endif
  53. static void __iomem *mstk48t08_regs;
  54. static void __iomem *mstk48t59_regs;
  55. static int set_rtc_mmss(unsigned long);
  56. #define TICK_PRIV_BIT (1UL << 63)
  57. #ifdef CONFIG_SMP
  58. unsigned long profile_pc(struct pt_regs *regs)
  59. {
  60. unsigned long pc = instruction_pointer(regs);
  61. if (in_lock_functions(pc))
  62. return regs->u_regs[UREG_RETPC];
  63. return pc;
  64. }
  65. EXPORT_SYMBOL(profile_pc);
  66. #endif
  67. static void tick_disable_protection(void)
  68. {
  69. /* Set things up so user can access tick register for profiling
  70. * purposes. Also workaround BB_ERRATA_1 by doing a dummy
  71. * read back of %tick after writing it.
  72. */
  73. __asm__ __volatile__(
  74. " ba,pt %%xcc, 1f\n"
  75. " nop\n"
  76. " .align 64\n"
  77. "1: rd %%tick, %%g2\n"
  78. " add %%g2, 6, %%g2\n"
  79. " andn %%g2, %0, %%g2\n"
  80. " wrpr %%g2, 0, %%tick\n"
  81. " rdpr %%tick, %%g0"
  82. : /* no outputs */
  83. : "r" (TICK_PRIV_BIT)
  84. : "g2");
  85. }
  86. static void tick_init_tick(unsigned long offset)
  87. {
  88. tick_disable_protection();
  89. __asm__ __volatile__(
  90. " rd %%tick, %%g1\n"
  91. " andn %%g1, %1, %%g1\n"
  92. " ba,pt %%xcc, 1f\n"
  93. " add %%g1, %0, %%g1\n"
  94. " .align 64\n"
  95. "1: wr %%g1, 0x0, %%tick_cmpr\n"
  96. " rd %%tick_cmpr, %%g0"
  97. : /* no outputs */
  98. : "r" (offset), "r" (TICK_PRIV_BIT)
  99. : "g1");
  100. }
  101. static unsigned long tick_get_tick(void)
  102. {
  103. unsigned long ret;
  104. __asm__ __volatile__("rd %%tick, %0\n\t"
  105. "mov %0, %0"
  106. : "=r" (ret));
  107. return ret & ~TICK_PRIV_BIT;
  108. }
  109. static unsigned long tick_get_compare(void)
  110. {
  111. unsigned long ret;
  112. __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
  113. "mov %0, %0"
  114. : "=r" (ret));
  115. return ret;
  116. }
  117. static unsigned long tick_add_compare(unsigned long adj)
  118. {
  119. unsigned long new_compare;
  120. /* Workaround for Spitfire Errata (#54 I think??), I discovered
  121. * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
  122. * number 103640.
  123. *
  124. * On Blackbird writes to %tick_cmpr can fail, the
  125. * workaround seems to be to execute the wr instruction
  126. * at the start of an I-cache line, and perform a dummy
  127. * read back from %tick_cmpr right after writing to it. -DaveM
  128. */
  129. __asm__ __volatile__("rd %%tick_cmpr, %0\n\t"
  130. "ba,pt %%xcc, 1f\n\t"
  131. " add %0, %1, %0\n\t"
  132. ".align 64\n"
  133. "1:\n\t"
  134. "wr %0, 0, %%tick_cmpr\n\t"
  135. "rd %%tick_cmpr, %%g0"
  136. : "=&r" (new_compare)
  137. : "r" (adj));
  138. return new_compare;
  139. }
  140. static unsigned long tick_add_tick(unsigned long adj, unsigned long offset)
  141. {
  142. unsigned long new_tick, tmp;
  143. /* Also need to handle Blackbird bug here too. */
  144. __asm__ __volatile__("rd %%tick, %0\n\t"
  145. "add %0, %2, %0\n\t"
  146. "wrpr %0, 0, %%tick\n\t"
  147. "andn %0, %4, %1\n\t"
  148. "ba,pt %%xcc, 1f\n\t"
  149. " add %1, %3, %1\n\t"
  150. ".align 64\n"
  151. "1:\n\t"
  152. "wr %1, 0, %%tick_cmpr\n\t"
  153. "rd %%tick_cmpr, %%g0"
  154. : "=&r" (new_tick), "=&r" (tmp)
  155. : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
  156. return new_tick;
  157. }
  158. static struct sparc64_tick_ops tick_operations __read_mostly = {
  159. .init_tick = tick_init_tick,
  160. .get_tick = tick_get_tick,
  161. .get_compare = tick_get_compare,
  162. .add_tick = tick_add_tick,
  163. .add_compare = tick_add_compare,
  164. .softint_mask = 1UL << 0,
  165. };
  166. struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
  167. static void stick_init_tick(unsigned long offset)
  168. {
  169. /* Writes to the %tick and %stick register are not
  170. * allowed on sun4v. The Hypervisor controls that
  171. * bit, per-strand.
  172. */
  173. if (tlb_type != hypervisor) {
  174. tick_disable_protection();
  175. /* Let the user get at STICK too. */
  176. __asm__ __volatile__(
  177. " rd %%asr24, %%g2\n"
  178. " andn %%g2, %0, %%g2\n"
  179. " wr %%g2, 0, %%asr24"
  180. : /* no outputs */
  181. : "r" (TICK_PRIV_BIT)
  182. : "g1", "g2");
  183. }
  184. __asm__ __volatile__(
  185. " rd %%asr24, %%g1\n"
  186. " andn %%g1, %1, %%g1\n"
  187. " add %%g1, %0, %%g1\n"
  188. " wr %%g1, 0x0, %%asr25"
  189. : /* no outputs */
  190. : "r" (offset), "r" (TICK_PRIV_BIT)
  191. : "g1");
  192. }
  193. static unsigned long stick_get_tick(void)
  194. {
  195. unsigned long ret;
  196. __asm__ __volatile__("rd %%asr24, %0"
  197. : "=r" (ret));
  198. return ret & ~TICK_PRIV_BIT;
  199. }
  200. static unsigned long stick_get_compare(void)
  201. {
  202. unsigned long ret;
  203. __asm__ __volatile__("rd %%asr25, %0"
  204. : "=r" (ret));
  205. return ret;
  206. }
  207. static unsigned long stick_add_tick(unsigned long adj, unsigned long offset)
  208. {
  209. unsigned long new_tick, tmp;
  210. __asm__ __volatile__("rd %%asr24, %0\n\t"
  211. "add %0, %2, %0\n\t"
  212. "wr %0, 0, %%asr24\n\t"
  213. "andn %0, %4, %1\n\t"
  214. "add %1, %3, %1\n\t"
  215. "wr %1, 0, %%asr25"
  216. : "=&r" (new_tick), "=&r" (tmp)
  217. : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
  218. return new_tick;
  219. }
  220. static unsigned long stick_add_compare(unsigned long adj)
  221. {
  222. unsigned long new_compare;
  223. __asm__ __volatile__("rd %%asr25, %0\n\t"
  224. "add %0, %1, %0\n\t"
  225. "wr %0, 0, %%asr25"
  226. : "=&r" (new_compare)
  227. : "r" (adj));
  228. return new_compare;
  229. }
  230. static struct sparc64_tick_ops stick_operations __read_mostly = {
  231. .init_tick = stick_init_tick,
  232. .get_tick = stick_get_tick,
  233. .get_compare = stick_get_compare,
  234. .add_tick = stick_add_tick,
  235. .add_compare = stick_add_compare,
  236. .softint_mask = 1UL << 16,
  237. };
  238. /* On Hummingbird the STICK/STICK_CMPR register is implemented
  239. * in I/O space. There are two 64-bit registers each, the
  240. * first holds the low 32-bits of the value and the second holds
  241. * the high 32-bits.
  242. *
  243. * Since STICK is constantly updating, we have to access it carefully.
  244. *
  245. * The sequence we use to read is:
  246. * 1) read high
  247. * 2) read low
  248. * 3) read high again, if it rolled re-read both low and high again.
  249. *
  250. * Writing STICK safely is also tricky:
  251. * 1) write low to zero
  252. * 2) write high
  253. * 3) write low
  254. */
  255. #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
  256. #define HBIRD_STICK_ADDR 0x1fe0000f070UL
  257. static unsigned long __hbird_read_stick(void)
  258. {
  259. unsigned long ret, tmp1, tmp2, tmp3;
  260. unsigned long addr = HBIRD_STICK_ADDR+8;
  261. __asm__ __volatile__("ldxa [%1] %5, %2\n"
  262. "1:\n\t"
  263. "sub %1, 0x8, %1\n\t"
  264. "ldxa [%1] %5, %3\n\t"
  265. "add %1, 0x8, %1\n\t"
  266. "ldxa [%1] %5, %4\n\t"
  267. "cmp %4, %2\n\t"
  268. "bne,a,pn %%xcc, 1b\n\t"
  269. " mov %4, %2\n\t"
  270. "sllx %4, 32, %4\n\t"
  271. "or %3, %4, %0\n\t"
  272. : "=&r" (ret), "=&r" (addr),
  273. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  274. : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
  275. return ret;
  276. }
  277. static unsigned long __hbird_read_compare(void)
  278. {
  279. unsigned long low, high;
  280. unsigned long addr = HBIRD_STICKCMP_ADDR;
  281. __asm__ __volatile__("ldxa [%2] %3, %0\n\t"
  282. "add %2, 0x8, %2\n\t"
  283. "ldxa [%2] %3, %1"
  284. : "=&r" (low), "=&r" (high), "=&r" (addr)
  285. : "i" (ASI_PHYS_BYPASS_EC_E), "2" (addr));
  286. return (high << 32UL) | low;
  287. }
  288. static void __hbird_write_stick(unsigned long val)
  289. {
  290. unsigned long low = (val & 0xffffffffUL);
  291. unsigned long high = (val >> 32UL);
  292. unsigned long addr = HBIRD_STICK_ADDR;
  293. __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
  294. "add %0, 0x8, %0\n\t"
  295. "stxa %3, [%0] %4\n\t"
  296. "sub %0, 0x8, %0\n\t"
  297. "stxa %2, [%0] %4"
  298. : "=&r" (addr)
  299. : "0" (addr), "r" (low), "r" (high),
  300. "i" (ASI_PHYS_BYPASS_EC_E));
  301. }
  302. static void __hbird_write_compare(unsigned long val)
  303. {
  304. unsigned long low = (val & 0xffffffffUL);
  305. unsigned long high = (val >> 32UL);
  306. unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
  307. __asm__ __volatile__("stxa %3, [%0] %4\n\t"
  308. "sub %0, 0x8, %0\n\t"
  309. "stxa %2, [%0] %4"
  310. : "=&r" (addr)
  311. : "0" (addr), "r" (low), "r" (high),
  312. "i" (ASI_PHYS_BYPASS_EC_E));
  313. }
  314. static void hbtick_init_tick(unsigned long offset)
  315. {
  316. unsigned long val;
  317. tick_disable_protection();
  318. /* XXX This seems to be necessary to 'jumpstart' Hummingbird
  319. * XXX into actually sending STICK interrupts. I think because
  320. * XXX of how we store %tick_cmpr in head.S this somehow resets the
  321. * XXX {TICK + STICK} interrupt mux. -DaveM
  322. */
  323. __hbird_write_stick(__hbird_read_stick());
  324. val = __hbird_read_stick() & ~TICK_PRIV_BIT;
  325. __hbird_write_compare(val + offset);
  326. }
  327. static unsigned long hbtick_get_tick(void)
  328. {
  329. return __hbird_read_stick() & ~TICK_PRIV_BIT;
  330. }
  331. static unsigned long hbtick_get_compare(void)
  332. {
  333. return __hbird_read_compare();
  334. }
  335. static unsigned long hbtick_add_tick(unsigned long adj, unsigned long offset)
  336. {
  337. unsigned long val;
  338. val = __hbird_read_stick() + adj;
  339. __hbird_write_stick(val);
  340. val &= ~TICK_PRIV_BIT;
  341. __hbird_write_compare(val + offset);
  342. return val;
  343. }
  344. static unsigned long hbtick_add_compare(unsigned long adj)
  345. {
  346. unsigned long val = __hbird_read_compare() + adj;
  347. val &= ~TICK_PRIV_BIT;
  348. __hbird_write_compare(val);
  349. return val;
  350. }
  351. static struct sparc64_tick_ops hbtick_operations __read_mostly = {
  352. .init_tick = hbtick_init_tick,
  353. .get_tick = hbtick_get_tick,
  354. .get_compare = hbtick_get_compare,
  355. .add_tick = hbtick_add_tick,
  356. .add_compare = hbtick_add_compare,
  357. .softint_mask = 1UL << 0,
  358. };
  359. /* timer_interrupt() needs to keep up the real-time clock,
  360. * as well as call the "do_timer()" routine every clocktick
  361. *
  362. * NOTE: On SUN5 systems the ticker interrupt comes in using 2
  363. * interrupts, one at level14 and one with softint bit 0.
  364. */
  365. unsigned long timer_tick_offset __read_mostly;
  366. static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
  367. #define TICK_SIZE (tick_nsec / 1000)
  368. static inline void timer_check_rtc(void)
  369. {
  370. /* last time the cmos clock got updated */
  371. static long last_rtc_update;
  372. /* Determine when to update the Mostek clock. */
  373. if (ntp_synced() &&
  374. xtime.tv_sec > last_rtc_update + 660 &&
  375. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  376. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  377. if (set_rtc_mmss(xtime.tv_sec) == 0)
  378. last_rtc_update = xtime.tv_sec;
  379. else
  380. last_rtc_update = xtime.tv_sec - 600;
  381. /* do it again in 60 s */
  382. }
  383. }
  384. irqreturn_t timer_interrupt(int irq, void *dev_id)
  385. {
  386. unsigned long ticks, compare, pstate;
  387. write_seqlock(&xtime_lock);
  388. do {
  389. #ifndef CONFIG_SMP
  390. profile_tick(CPU_PROFILING);
  391. update_process_times(user_mode(get_irq_regs()));
  392. #endif
  393. do_timer(1);
  394. /* Guarantee that the following sequences execute
  395. * uninterrupted.
  396. */
  397. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  398. "wrpr %0, %1, %%pstate"
  399. : "=r" (pstate)
  400. : "i" (PSTATE_IE));
  401. compare = tick_ops->add_compare(timer_tick_offset);
  402. ticks = tick_ops->get_tick();
  403. /* Restore PSTATE_IE. */
  404. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  405. : /* no outputs */
  406. : "r" (pstate));
  407. } while (time_after_eq(ticks, compare));
  408. timer_check_rtc();
  409. write_sequnlock(&xtime_lock);
  410. return IRQ_HANDLED;
  411. }
  412. #ifdef CONFIG_SMP
  413. void timer_tick_interrupt(struct pt_regs *regs)
  414. {
  415. write_seqlock(&xtime_lock);
  416. do_timer(1);
  417. timer_check_rtc();
  418. write_sequnlock(&xtime_lock);
  419. }
  420. #endif
  421. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  422. static void __init kick_start_clock(void)
  423. {
  424. void __iomem *regs = mstk48t02_regs;
  425. u8 sec, tmp;
  426. int i, count;
  427. prom_printf("CLOCK: Clock was stopped. Kick start ");
  428. spin_lock_irq(&mostek_lock);
  429. /* Turn on the kick start bit to start the oscillator. */
  430. tmp = mostek_read(regs + MOSTEK_CREG);
  431. tmp |= MSTK_CREG_WRITE;
  432. mostek_write(regs + MOSTEK_CREG, tmp);
  433. tmp = mostek_read(regs + MOSTEK_SEC);
  434. tmp &= ~MSTK_STOP;
  435. mostek_write(regs + MOSTEK_SEC, tmp);
  436. tmp = mostek_read(regs + MOSTEK_HOUR);
  437. tmp |= MSTK_KICK_START;
  438. mostek_write(regs + MOSTEK_HOUR, tmp);
  439. tmp = mostek_read(regs + MOSTEK_CREG);
  440. tmp &= ~MSTK_CREG_WRITE;
  441. mostek_write(regs + MOSTEK_CREG, tmp);
  442. spin_unlock_irq(&mostek_lock);
  443. /* Delay to allow the clock oscillator to start. */
  444. sec = MSTK_REG_SEC(regs);
  445. for (i = 0; i < 3; i++) {
  446. while (sec == MSTK_REG_SEC(regs))
  447. for (count = 0; count < 100000; count++)
  448. /* nothing */ ;
  449. prom_printf(".");
  450. sec = MSTK_REG_SEC(regs);
  451. }
  452. prom_printf("\n");
  453. spin_lock_irq(&mostek_lock);
  454. /* Turn off kick start and set a "valid" time and date. */
  455. tmp = mostek_read(regs + MOSTEK_CREG);
  456. tmp |= MSTK_CREG_WRITE;
  457. mostek_write(regs + MOSTEK_CREG, tmp);
  458. tmp = mostek_read(regs + MOSTEK_HOUR);
  459. tmp &= ~MSTK_KICK_START;
  460. mostek_write(regs + MOSTEK_HOUR, tmp);
  461. MSTK_SET_REG_SEC(regs,0);
  462. MSTK_SET_REG_MIN(regs,0);
  463. MSTK_SET_REG_HOUR(regs,0);
  464. MSTK_SET_REG_DOW(regs,5);
  465. MSTK_SET_REG_DOM(regs,1);
  466. MSTK_SET_REG_MONTH(regs,8);
  467. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  468. tmp = mostek_read(regs + MOSTEK_CREG);
  469. tmp &= ~MSTK_CREG_WRITE;
  470. mostek_write(regs + MOSTEK_CREG, tmp);
  471. spin_unlock_irq(&mostek_lock);
  472. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  473. while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
  474. prom_printf("CLOCK: Kick start still on!\n");
  475. spin_lock_irq(&mostek_lock);
  476. tmp = mostek_read(regs + MOSTEK_CREG);
  477. tmp |= MSTK_CREG_WRITE;
  478. mostek_write(regs + MOSTEK_CREG, tmp);
  479. tmp = mostek_read(regs + MOSTEK_HOUR);
  480. tmp &= ~MSTK_KICK_START;
  481. mostek_write(regs + MOSTEK_HOUR, tmp);
  482. tmp = mostek_read(regs + MOSTEK_CREG);
  483. tmp &= ~MSTK_CREG_WRITE;
  484. mostek_write(regs + MOSTEK_CREG, tmp);
  485. spin_unlock_irq(&mostek_lock);
  486. }
  487. prom_printf("CLOCK: Kick start procedure successful.\n");
  488. }
  489. /* Return nonzero if the clock chip battery is low. */
  490. static int __init has_low_battery(void)
  491. {
  492. void __iomem *regs = mstk48t02_regs;
  493. u8 data1, data2;
  494. spin_lock_irq(&mostek_lock);
  495. data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
  496. mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
  497. data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
  498. mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
  499. spin_unlock_irq(&mostek_lock);
  500. return (data1 == data2); /* Was the write blocked? */
  501. }
  502. /* Probe for the real time clock chip. */
  503. static void __init set_system_time(void)
  504. {
  505. unsigned int year, mon, day, hour, min, sec;
  506. void __iomem *mregs = mstk48t02_regs;
  507. #ifdef CONFIG_PCI
  508. unsigned long dregs = ds1287_regs;
  509. #else
  510. unsigned long dregs = 0UL;
  511. #endif
  512. u8 tmp;
  513. if (!mregs && !dregs) {
  514. prom_printf("Something wrong, clock regs not mapped yet.\n");
  515. prom_halt();
  516. }
  517. if (mregs) {
  518. spin_lock_irq(&mostek_lock);
  519. /* Traditional Mostek chip. */
  520. tmp = mostek_read(mregs + MOSTEK_CREG);
  521. tmp |= MSTK_CREG_READ;
  522. mostek_write(mregs + MOSTEK_CREG, tmp);
  523. sec = MSTK_REG_SEC(mregs);
  524. min = MSTK_REG_MIN(mregs);
  525. hour = MSTK_REG_HOUR(mregs);
  526. day = MSTK_REG_DOM(mregs);
  527. mon = MSTK_REG_MONTH(mregs);
  528. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  529. } else {
  530. /* Dallas 12887 RTC chip. */
  531. do {
  532. sec = CMOS_READ(RTC_SECONDS);
  533. min = CMOS_READ(RTC_MINUTES);
  534. hour = CMOS_READ(RTC_HOURS);
  535. day = CMOS_READ(RTC_DAY_OF_MONTH);
  536. mon = CMOS_READ(RTC_MONTH);
  537. year = CMOS_READ(RTC_YEAR);
  538. } while (sec != CMOS_READ(RTC_SECONDS));
  539. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  540. BCD_TO_BIN(sec);
  541. BCD_TO_BIN(min);
  542. BCD_TO_BIN(hour);
  543. BCD_TO_BIN(day);
  544. BCD_TO_BIN(mon);
  545. BCD_TO_BIN(year);
  546. }
  547. if ((year += 1900) < 1970)
  548. year += 100;
  549. }
  550. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  551. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  552. set_normalized_timespec(&wall_to_monotonic,
  553. -xtime.tv_sec, -xtime.tv_nsec);
  554. if (mregs) {
  555. tmp = mostek_read(mregs + MOSTEK_CREG);
  556. tmp &= ~MSTK_CREG_READ;
  557. mostek_write(mregs + MOSTEK_CREG, tmp);
  558. spin_unlock_irq(&mostek_lock);
  559. }
  560. }
  561. /* davem suggests we keep this within the 4M locked kernel image */
  562. static u32 starfire_get_time(void)
  563. {
  564. static char obp_gettod[32];
  565. static u32 unix_tod;
  566. sprintf(obp_gettod, "h# %08x unix-gettod",
  567. (unsigned int) (long) &unix_tod);
  568. prom_feval(obp_gettod);
  569. return unix_tod;
  570. }
  571. static int starfire_set_time(u32 val)
  572. {
  573. /* Do nothing, time is set using the service processor
  574. * console on this platform.
  575. */
  576. return 0;
  577. }
  578. static u32 hypervisor_get_time(void)
  579. {
  580. register unsigned long func asm("%o5");
  581. register unsigned long arg0 asm("%o0");
  582. register unsigned long arg1 asm("%o1");
  583. int retries = 10000;
  584. retry:
  585. func = HV_FAST_TOD_GET;
  586. arg0 = 0;
  587. arg1 = 0;
  588. __asm__ __volatile__("ta %6"
  589. : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
  590. : "0" (func), "1" (arg0), "2" (arg1),
  591. "i" (HV_FAST_TRAP));
  592. if (arg0 == HV_EOK)
  593. return arg1;
  594. if (arg0 == HV_EWOULDBLOCK) {
  595. if (--retries > 0) {
  596. udelay(100);
  597. goto retry;
  598. }
  599. printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
  600. return 0;
  601. }
  602. printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
  603. return 0;
  604. }
  605. static int hypervisor_set_time(u32 secs)
  606. {
  607. register unsigned long func asm("%o5");
  608. register unsigned long arg0 asm("%o0");
  609. int retries = 10000;
  610. retry:
  611. func = HV_FAST_TOD_SET;
  612. arg0 = secs;
  613. __asm__ __volatile__("ta %4"
  614. : "=&r" (func), "=&r" (arg0)
  615. : "0" (func), "1" (arg0),
  616. "i" (HV_FAST_TRAP));
  617. if (arg0 == HV_EOK)
  618. return 0;
  619. if (arg0 == HV_EWOULDBLOCK) {
  620. if (--retries > 0) {
  621. udelay(100);
  622. goto retry;
  623. }
  624. printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
  625. return -EAGAIN;
  626. }
  627. printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
  628. return -EOPNOTSUPP;
  629. }
  630. static int __init clock_model_matches(char *model)
  631. {
  632. if (strcmp(model, "mk48t02") &&
  633. strcmp(model, "mk48t08") &&
  634. strcmp(model, "mk48t59") &&
  635. strcmp(model, "m5819") &&
  636. strcmp(model, "m5819p") &&
  637. strcmp(model, "m5823") &&
  638. strcmp(model, "ds1287"))
  639. return 0;
  640. return 1;
  641. }
  642. static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
  643. {
  644. struct device_node *dp = op->node;
  645. char *model = of_get_property(dp, "model", NULL);
  646. unsigned long size, flags;
  647. void __iomem *regs;
  648. if (!model || !clock_model_matches(model))
  649. return -ENODEV;
  650. /* On an Enterprise system there can be multiple mostek clocks.
  651. * We should only match the one that is on the central FHC bus.
  652. */
  653. if (!strcmp(dp->parent->name, "fhc") &&
  654. strcmp(dp->parent->parent->name, "central") != 0)
  655. return -ENODEV;
  656. size = (op->resource[0].end - op->resource[0].start) + 1;
  657. regs = of_ioremap(&op->resource[0], 0, size, "clock");
  658. if (!regs)
  659. return -ENOMEM;
  660. #ifdef CONFIG_PCI
  661. if (!strcmp(model, "ds1287") ||
  662. !strcmp(model, "m5819") ||
  663. !strcmp(model, "m5819p") ||
  664. !strcmp(model, "m5823")) {
  665. ds1287_regs = (unsigned long) regs;
  666. } else
  667. #endif
  668. if (model[5] == '0' && model[6] == '2') {
  669. mstk48t02_regs = regs;
  670. } else if(model[5] == '0' && model[6] == '8') {
  671. mstk48t08_regs = regs;
  672. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  673. } else {
  674. mstk48t59_regs = regs;
  675. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  676. }
  677. printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs);
  678. local_irq_save(flags);
  679. if (mstk48t02_regs != NULL) {
  680. /* Report a low battery voltage condition. */
  681. if (has_low_battery())
  682. prom_printf("NVRAM: Low battery voltage!\n");
  683. /* Kick start the clock if it is completely stopped. */
  684. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  685. kick_start_clock();
  686. }
  687. set_system_time();
  688. local_irq_restore(flags);
  689. return 0;
  690. }
  691. static struct of_device_id clock_match[] = {
  692. {
  693. .name = "eeprom",
  694. },
  695. {
  696. .name = "rtc",
  697. },
  698. {},
  699. };
  700. static struct of_platform_driver clock_driver = {
  701. .name = "clock",
  702. .match_table = clock_match,
  703. .probe = clock_probe,
  704. };
  705. static int __init clock_init(void)
  706. {
  707. if (this_is_starfire) {
  708. xtime.tv_sec = starfire_get_time();
  709. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  710. set_normalized_timespec(&wall_to_monotonic,
  711. -xtime.tv_sec, -xtime.tv_nsec);
  712. return 0;
  713. }
  714. if (tlb_type == hypervisor) {
  715. xtime.tv_sec = hypervisor_get_time();
  716. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  717. set_normalized_timespec(&wall_to_monotonic,
  718. -xtime.tv_sec, -xtime.tv_nsec);
  719. return 0;
  720. }
  721. return of_register_driver(&clock_driver, &of_bus_type);
  722. }
  723. /* Must be after subsys_initcall() so that busses are probed. Must
  724. * be before device_initcall() because things like the RTC driver
  725. * need to see the clock registers.
  726. */
  727. fs_initcall(clock_init);
  728. /* This is gets the master TICK_INT timer going. */
  729. static unsigned long sparc64_init_timers(void)
  730. {
  731. struct device_node *dp;
  732. struct property *prop;
  733. unsigned long clock;
  734. #ifdef CONFIG_SMP
  735. extern void smp_tick_init(void);
  736. #endif
  737. dp = of_find_node_by_path("/");
  738. if (tlb_type == spitfire) {
  739. unsigned long ver, manuf, impl;
  740. __asm__ __volatile__ ("rdpr %%ver, %0"
  741. : "=&r" (ver));
  742. manuf = ((ver >> 48) & 0xffff);
  743. impl = ((ver >> 32) & 0xffff);
  744. if (manuf == 0x17 && impl == 0x13) {
  745. /* Hummingbird, aka Ultra-IIe */
  746. tick_ops = &hbtick_operations;
  747. prop = of_find_property(dp, "stick-frequency", NULL);
  748. } else {
  749. tick_ops = &tick_operations;
  750. cpu_find_by_instance(0, &dp, NULL);
  751. prop = of_find_property(dp, "clock-frequency", NULL);
  752. }
  753. } else {
  754. tick_ops = &stick_operations;
  755. prop = of_find_property(dp, "stick-frequency", NULL);
  756. }
  757. clock = *(unsigned int *) prop->value;
  758. timer_tick_offset = clock / HZ;
  759. #ifdef CONFIG_SMP
  760. smp_tick_init();
  761. #endif
  762. return clock;
  763. }
  764. static void sparc64_start_timers(void)
  765. {
  766. unsigned long pstate;
  767. /* Guarantee that the following sequences execute
  768. * uninterrupted.
  769. */
  770. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  771. "wrpr %0, %1, %%pstate"
  772. : "=r" (pstate)
  773. : "i" (PSTATE_IE));
  774. tick_ops->init_tick(timer_tick_offset);
  775. /* Restore PSTATE_IE. */
  776. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  777. : /* no outputs */
  778. : "r" (pstate));
  779. }
  780. struct freq_table {
  781. unsigned long clock_tick_ref;
  782. unsigned int ref_freq;
  783. };
  784. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  785. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  786. {
  787. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  788. if (ft->clock_tick_ref)
  789. return ft->clock_tick_ref;
  790. return cpu_data(cpu).clock_tick;
  791. }
  792. #ifdef CONFIG_CPU_FREQ
  793. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  794. void *data)
  795. {
  796. struct cpufreq_freqs *freq = data;
  797. unsigned int cpu = freq->cpu;
  798. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  799. if (!ft->ref_freq) {
  800. ft->ref_freq = freq->old;
  801. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  802. }
  803. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  804. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  805. (val == CPUFREQ_RESUMECHANGE)) {
  806. cpu_data(cpu).clock_tick =
  807. cpufreq_scale(ft->clock_tick_ref,
  808. ft->ref_freq,
  809. freq->new);
  810. }
  811. return 0;
  812. }
  813. static struct notifier_block sparc64_cpufreq_notifier_block = {
  814. .notifier_call = sparc64_cpufreq_notifier
  815. };
  816. #endif /* CONFIG_CPU_FREQ */
  817. static struct time_interpolator sparc64_cpu_interpolator = {
  818. .source = TIME_SOURCE_CPU,
  819. .shift = 16,
  820. .mask = 0xffffffffffffffffLL
  821. };
  822. /* The quotient formula is taken from the IA64 port. */
  823. #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
  824. void __init time_init(void)
  825. {
  826. unsigned long clock = sparc64_init_timers();
  827. sparc64_cpu_interpolator.frequency = clock;
  828. register_time_interpolator(&sparc64_cpu_interpolator);
  829. /* Now that the interpolator is registered, it is
  830. * safe to start the timer ticking.
  831. */
  832. sparc64_start_timers();
  833. timer_ticks_per_nsec_quotient =
  834. (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
  835. (clock / 2)) / clock);
  836. #ifdef CONFIG_CPU_FREQ
  837. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  838. CPUFREQ_TRANSITION_NOTIFIER);
  839. #endif
  840. }
  841. unsigned long long sched_clock(void)
  842. {
  843. unsigned long ticks = tick_ops->get_tick();
  844. return (ticks * timer_ticks_per_nsec_quotient)
  845. >> SPARC64_NSEC_PER_CYC_SHIFT;
  846. }
  847. static int set_rtc_mmss(unsigned long nowtime)
  848. {
  849. int real_seconds, real_minutes, chip_minutes;
  850. void __iomem *mregs = mstk48t02_regs;
  851. #ifdef CONFIG_PCI
  852. unsigned long dregs = ds1287_regs;
  853. #else
  854. unsigned long dregs = 0UL;
  855. #endif
  856. unsigned long flags;
  857. u8 tmp;
  858. /*
  859. * Not having a register set can lead to trouble.
  860. * Also starfire doesn't have a tod clock.
  861. */
  862. if (!mregs && !dregs)
  863. return -1;
  864. if (mregs) {
  865. spin_lock_irqsave(&mostek_lock, flags);
  866. /* Read the current RTC minutes. */
  867. tmp = mostek_read(mregs + MOSTEK_CREG);
  868. tmp |= MSTK_CREG_READ;
  869. mostek_write(mregs + MOSTEK_CREG, tmp);
  870. chip_minutes = MSTK_REG_MIN(mregs);
  871. tmp = mostek_read(mregs + MOSTEK_CREG);
  872. tmp &= ~MSTK_CREG_READ;
  873. mostek_write(mregs + MOSTEK_CREG, tmp);
  874. /*
  875. * since we're only adjusting minutes and seconds,
  876. * don't interfere with hour overflow. This avoids
  877. * messing with unknown time zones but requires your
  878. * RTC not to be off by more than 15 minutes
  879. */
  880. real_seconds = nowtime % 60;
  881. real_minutes = nowtime / 60;
  882. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  883. real_minutes += 30; /* correct for half hour time zone */
  884. real_minutes %= 60;
  885. if (abs(real_minutes - chip_minutes) < 30) {
  886. tmp = mostek_read(mregs + MOSTEK_CREG);
  887. tmp |= MSTK_CREG_WRITE;
  888. mostek_write(mregs + MOSTEK_CREG, tmp);
  889. MSTK_SET_REG_SEC(mregs,real_seconds);
  890. MSTK_SET_REG_MIN(mregs,real_minutes);
  891. tmp = mostek_read(mregs + MOSTEK_CREG);
  892. tmp &= ~MSTK_CREG_WRITE;
  893. mostek_write(mregs + MOSTEK_CREG, tmp);
  894. spin_unlock_irqrestore(&mostek_lock, flags);
  895. return 0;
  896. } else {
  897. spin_unlock_irqrestore(&mostek_lock, flags);
  898. return -1;
  899. }
  900. } else {
  901. int retval = 0;
  902. unsigned char save_control, save_freq_select;
  903. /* Stolen from arch/i386/kernel/time.c, see there for
  904. * credits and descriptive comments.
  905. */
  906. spin_lock_irqsave(&rtc_lock, flags);
  907. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  908. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  909. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  910. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  911. chip_minutes = CMOS_READ(RTC_MINUTES);
  912. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  913. BCD_TO_BIN(chip_minutes);
  914. real_seconds = nowtime % 60;
  915. real_minutes = nowtime / 60;
  916. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  917. real_minutes += 30;
  918. real_minutes %= 60;
  919. if (abs(real_minutes - chip_minutes) < 30) {
  920. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  921. BIN_TO_BCD(real_seconds);
  922. BIN_TO_BCD(real_minutes);
  923. }
  924. CMOS_WRITE(real_seconds,RTC_SECONDS);
  925. CMOS_WRITE(real_minutes,RTC_MINUTES);
  926. } else {
  927. printk(KERN_WARNING
  928. "set_rtc_mmss: can't update from %d to %d\n",
  929. chip_minutes, real_minutes);
  930. retval = -1;
  931. }
  932. CMOS_WRITE(save_control, RTC_CONTROL);
  933. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  934. spin_unlock_irqrestore(&rtc_lock, flags);
  935. return retval;
  936. }
  937. }
  938. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  939. static unsigned char mini_rtc_status; /* bitmapped status byte. */
  940. /* months start at 0 now */
  941. static unsigned char days_in_mo[] =
  942. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  943. #define FEBRUARY 2
  944. #define STARTOFTIME 1970
  945. #define SECDAY 86400L
  946. #define SECYR (SECDAY * 365)
  947. #define leapyear(year) ((year) % 4 == 0 && \
  948. ((year) % 100 != 0 || (year) % 400 == 0))
  949. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  950. #define days_in_month(a) (month_days[(a) - 1])
  951. static int month_days[12] = {
  952. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  953. };
  954. /*
  955. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  956. */
  957. static void GregorianDay(struct rtc_time * tm)
  958. {
  959. int leapsToDate;
  960. int lastYear;
  961. int day;
  962. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  963. lastYear = tm->tm_year - 1;
  964. /*
  965. * Number of leap corrections to apply up to end of last year
  966. */
  967. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  968. /*
  969. * This year is a leap year if it is divisible by 4 except when it is
  970. * divisible by 100 unless it is divisible by 400
  971. *
  972. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  973. */
  974. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  975. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  976. tm->tm_mday;
  977. tm->tm_wday = day % 7;
  978. }
  979. static void to_tm(int tim, struct rtc_time *tm)
  980. {
  981. register int i;
  982. register long hms, day;
  983. day = tim / SECDAY;
  984. hms = tim % SECDAY;
  985. /* Hours, minutes, seconds are easy */
  986. tm->tm_hour = hms / 3600;
  987. tm->tm_min = (hms % 3600) / 60;
  988. tm->tm_sec = (hms % 3600) % 60;
  989. /* Number of years in days */
  990. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  991. day -= days_in_year(i);
  992. tm->tm_year = i;
  993. /* Number of months in days left */
  994. if (leapyear(tm->tm_year))
  995. days_in_month(FEBRUARY) = 29;
  996. for (i = 1; day >= days_in_month(i); i++)
  997. day -= days_in_month(i);
  998. days_in_month(FEBRUARY) = 28;
  999. tm->tm_mon = i;
  1000. /* Days are what is left over (+1) from all that. */
  1001. tm->tm_mday = day + 1;
  1002. /*
  1003. * Determine the day of week
  1004. */
  1005. GregorianDay(tm);
  1006. }
  1007. /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
  1008. * aka Unix time. So we have to convert to/from rtc_time.
  1009. */
  1010. static inline void mini_get_rtc_time(struct rtc_time *time)
  1011. {
  1012. unsigned long flags;
  1013. u32 seconds;
  1014. spin_lock_irqsave(&rtc_lock, flags);
  1015. seconds = 0;
  1016. if (this_is_starfire)
  1017. seconds = starfire_get_time();
  1018. else if (tlb_type == hypervisor)
  1019. seconds = hypervisor_get_time();
  1020. spin_unlock_irqrestore(&rtc_lock, flags);
  1021. to_tm(seconds, time);
  1022. time->tm_year -= 1900;
  1023. time->tm_mon -= 1;
  1024. }
  1025. static inline int mini_set_rtc_time(struct rtc_time *time)
  1026. {
  1027. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  1028. time->tm_mday, time->tm_hour,
  1029. time->tm_min, time->tm_sec);
  1030. unsigned long flags;
  1031. int err;
  1032. spin_lock_irqsave(&rtc_lock, flags);
  1033. err = -ENODEV;
  1034. if (this_is_starfire)
  1035. err = starfire_set_time(seconds);
  1036. else if (tlb_type == hypervisor)
  1037. err = hypervisor_set_time(seconds);
  1038. spin_unlock_irqrestore(&rtc_lock, flags);
  1039. return err;
  1040. }
  1041. static int mini_rtc_ioctl(struct inode *inode, struct file *file,
  1042. unsigned int cmd, unsigned long arg)
  1043. {
  1044. struct rtc_time wtime;
  1045. void __user *argp = (void __user *)arg;
  1046. switch (cmd) {
  1047. case RTC_PLL_GET:
  1048. return -EINVAL;
  1049. case RTC_PLL_SET:
  1050. return -EINVAL;
  1051. case RTC_UIE_OFF: /* disable ints from RTC updates. */
  1052. return 0;
  1053. case RTC_UIE_ON: /* enable ints for RTC updates. */
  1054. return -EINVAL;
  1055. case RTC_RD_TIME: /* Read the time/date from RTC */
  1056. /* this doesn't get week-day, who cares */
  1057. memset(&wtime, 0, sizeof(wtime));
  1058. mini_get_rtc_time(&wtime);
  1059. return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
  1060. case RTC_SET_TIME: /* Set the RTC */
  1061. {
  1062. int year;
  1063. unsigned char leap_yr;
  1064. if (!capable(CAP_SYS_TIME))
  1065. return -EACCES;
  1066. if (copy_from_user(&wtime, argp, sizeof(wtime)))
  1067. return -EFAULT;
  1068. year = wtime.tm_year + 1900;
  1069. leap_yr = ((!(year % 4) && (year % 100)) ||
  1070. !(year % 400));
  1071. if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
  1072. return -EINVAL;
  1073. if (wtime.tm_mday < 0 || wtime.tm_mday >
  1074. (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
  1075. return -EINVAL;
  1076. if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
  1077. wtime.tm_min < 0 || wtime.tm_min >= 60 ||
  1078. wtime.tm_sec < 0 || wtime.tm_sec >= 60)
  1079. return -EINVAL;
  1080. return mini_set_rtc_time(&wtime);
  1081. }
  1082. }
  1083. return -EINVAL;
  1084. }
  1085. static int mini_rtc_open(struct inode *inode, struct file *file)
  1086. {
  1087. if (mini_rtc_status & RTC_IS_OPEN)
  1088. return -EBUSY;
  1089. mini_rtc_status |= RTC_IS_OPEN;
  1090. return 0;
  1091. }
  1092. static int mini_rtc_release(struct inode *inode, struct file *file)
  1093. {
  1094. mini_rtc_status &= ~RTC_IS_OPEN;
  1095. return 0;
  1096. }
  1097. static struct file_operations mini_rtc_fops = {
  1098. .owner = THIS_MODULE,
  1099. .ioctl = mini_rtc_ioctl,
  1100. .open = mini_rtc_open,
  1101. .release = mini_rtc_release,
  1102. };
  1103. static struct miscdevice rtc_mini_dev =
  1104. {
  1105. .minor = RTC_MINOR,
  1106. .name = "rtc",
  1107. .fops = &mini_rtc_fops,
  1108. };
  1109. static int __init rtc_mini_init(void)
  1110. {
  1111. int retval;
  1112. if (tlb_type != hypervisor && !this_is_starfire)
  1113. return -ENODEV;
  1114. printk(KERN_INFO "Mini RTC Driver\n");
  1115. retval = misc_register(&rtc_mini_dev);
  1116. if (retval < 0)
  1117. return retval;
  1118. return 0;
  1119. }
  1120. static void __exit rtc_mini_exit(void)
  1121. {
  1122. misc_deregister(&rtc_mini_dev);
  1123. }
  1124. module_init(rtc_mini_init);
  1125. module_exit(rtc_mini_exit);