time.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  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/miscdevice.h>
  31. #include <linux/rtc.h>
  32. #include <linux/kernel_stat.h>
  33. #include <linux/clockchips.h>
  34. #include <linux/clocksource.h>
  35. #include <asm/oplib.h>
  36. #include <asm/mostek.h>
  37. #include <asm/timer.h>
  38. #include <asm/irq.h>
  39. #include <asm/io.h>
  40. #include <asm/prom.h>
  41. #include <asm/of_device.h>
  42. #include <asm/starfire.h>
  43. #include <asm/smp.h>
  44. #include <asm/sections.h>
  45. #include <asm/cpudata.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/irq_regs.h>
  48. DEFINE_SPINLOCK(mostek_lock);
  49. DEFINE_SPINLOCK(rtc_lock);
  50. void __iomem *mstk48t02_regs = NULL;
  51. #ifdef CONFIG_PCI
  52. unsigned long ds1287_regs = 0UL;
  53. static void __iomem *bq4802_regs;
  54. #endif
  55. static void __iomem *mstk48t08_regs;
  56. static void __iomem *mstk48t59_regs;
  57. static int set_rtc_mmss(unsigned long);
  58. #define TICK_PRIV_BIT (1UL << 63)
  59. #define TICKCMP_IRQ_BIT (1UL << 63)
  60. #ifdef CONFIG_SMP
  61. unsigned long profile_pc(struct pt_regs *regs)
  62. {
  63. unsigned long pc = instruction_pointer(regs);
  64. if (in_lock_functions(pc))
  65. return regs->u_regs[UREG_RETPC];
  66. return pc;
  67. }
  68. EXPORT_SYMBOL(profile_pc);
  69. #endif
  70. static void tick_disable_protection(void)
  71. {
  72. /* Set things up so user can access tick register for profiling
  73. * purposes. Also workaround BB_ERRATA_1 by doing a dummy
  74. * read back of %tick after writing it.
  75. */
  76. __asm__ __volatile__(
  77. " ba,pt %%xcc, 1f\n"
  78. " nop\n"
  79. " .align 64\n"
  80. "1: rd %%tick, %%g2\n"
  81. " add %%g2, 6, %%g2\n"
  82. " andn %%g2, %0, %%g2\n"
  83. " wrpr %%g2, 0, %%tick\n"
  84. " rdpr %%tick, %%g0"
  85. : /* no outputs */
  86. : "r" (TICK_PRIV_BIT)
  87. : "g2");
  88. }
  89. static void tick_disable_irq(void)
  90. {
  91. __asm__ __volatile__(
  92. " ba,pt %%xcc, 1f\n"
  93. " nop\n"
  94. " .align 64\n"
  95. "1: wr %0, 0x0, %%tick_cmpr\n"
  96. " rd %%tick_cmpr, %%g0"
  97. : /* no outputs */
  98. : "r" (TICKCMP_IRQ_BIT));
  99. }
  100. static void tick_init_tick(void)
  101. {
  102. tick_disable_protection();
  103. tick_disable_irq();
  104. }
  105. static unsigned long tick_get_tick(void)
  106. {
  107. unsigned long ret;
  108. __asm__ __volatile__("rd %%tick, %0\n\t"
  109. "mov %0, %0"
  110. : "=r" (ret));
  111. return ret & ~TICK_PRIV_BIT;
  112. }
  113. static int tick_add_compare(unsigned long adj)
  114. {
  115. unsigned long orig_tick, new_tick, new_compare;
  116. __asm__ __volatile__("rd %%tick, %0"
  117. : "=r" (orig_tick));
  118. orig_tick &= ~TICKCMP_IRQ_BIT;
  119. /* Workaround for Spitfire Errata (#54 I think??), I discovered
  120. * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
  121. * number 103640.
  122. *
  123. * On Blackbird writes to %tick_cmpr can fail, the
  124. * workaround seems to be to execute the wr instruction
  125. * at the start of an I-cache line, and perform a dummy
  126. * read back from %tick_cmpr right after writing to it. -DaveM
  127. */
  128. __asm__ __volatile__("ba,pt %%xcc, 1f\n\t"
  129. " add %1, %2, %0\n\t"
  130. ".align 64\n"
  131. "1:\n\t"
  132. "wr %0, 0, %%tick_cmpr\n\t"
  133. "rd %%tick_cmpr, %%g0\n\t"
  134. : "=r" (new_compare)
  135. : "r" (orig_tick), "r" (adj));
  136. __asm__ __volatile__("rd %%tick, %0"
  137. : "=r" (new_tick));
  138. new_tick &= ~TICKCMP_IRQ_BIT;
  139. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  140. }
  141. static unsigned long tick_add_tick(unsigned long adj)
  142. {
  143. unsigned long new_tick;
  144. /* Also need to handle Blackbird bug here too. */
  145. __asm__ __volatile__("rd %%tick, %0\n\t"
  146. "add %0, %1, %0\n\t"
  147. "wrpr %0, 0, %%tick\n\t"
  148. : "=&r" (new_tick)
  149. : "r" (adj));
  150. return new_tick;
  151. }
  152. static struct sparc64_tick_ops tick_operations __read_mostly = {
  153. .name = "tick",
  154. .init_tick = tick_init_tick,
  155. .disable_irq = tick_disable_irq,
  156. .get_tick = tick_get_tick,
  157. .add_tick = tick_add_tick,
  158. .add_compare = tick_add_compare,
  159. .softint_mask = 1UL << 0,
  160. };
  161. struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
  162. static void stick_disable_irq(void)
  163. {
  164. __asm__ __volatile__(
  165. "wr %0, 0x0, %%asr25"
  166. : /* no outputs */
  167. : "r" (TICKCMP_IRQ_BIT));
  168. }
  169. static void stick_init_tick(void)
  170. {
  171. /* Writes to the %tick and %stick register are not
  172. * allowed on sun4v. The Hypervisor controls that
  173. * bit, per-strand.
  174. */
  175. if (tlb_type != hypervisor) {
  176. tick_disable_protection();
  177. tick_disable_irq();
  178. /* Let the user get at STICK too. */
  179. __asm__ __volatile__(
  180. " rd %%asr24, %%g2\n"
  181. " andn %%g2, %0, %%g2\n"
  182. " wr %%g2, 0, %%asr24"
  183. : /* no outputs */
  184. : "r" (TICK_PRIV_BIT)
  185. : "g1", "g2");
  186. }
  187. stick_disable_irq();
  188. }
  189. static unsigned long stick_get_tick(void)
  190. {
  191. unsigned long ret;
  192. __asm__ __volatile__("rd %%asr24, %0"
  193. : "=r" (ret));
  194. return ret & ~TICK_PRIV_BIT;
  195. }
  196. static unsigned long stick_add_tick(unsigned long adj)
  197. {
  198. unsigned long new_tick;
  199. __asm__ __volatile__("rd %%asr24, %0\n\t"
  200. "add %0, %1, %0\n\t"
  201. "wr %0, 0, %%asr24\n\t"
  202. : "=&r" (new_tick)
  203. : "r" (adj));
  204. return new_tick;
  205. }
  206. static int stick_add_compare(unsigned long adj)
  207. {
  208. unsigned long orig_tick, new_tick;
  209. __asm__ __volatile__("rd %%asr24, %0"
  210. : "=r" (orig_tick));
  211. orig_tick &= ~TICKCMP_IRQ_BIT;
  212. __asm__ __volatile__("wr %0, 0, %%asr25"
  213. : /* no outputs */
  214. : "r" (orig_tick + adj));
  215. __asm__ __volatile__("rd %%asr24, %0"
  216. : "=r" (new_tick));
  217. new_tick &= ~TICKCMP_IRQ_BIT;
  218. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  219. }
  220. static struct sparc64_tick_ops stick_operations __read_mostly = {
  221. .name = "stick",
  222. .init_tick = stick_init_tick,
  223. .disable_irq = stick_disable_irq,
  224. .get_tick = stick_get_tick,
  225. .add_tick = stick_add_tick,
  226. .add_compare = stick_add_compare,
  227. .softint_mask = 1UL << 16,
  228. };
  229. /* On Hummingbird the STICK/STICK_CMPR register is implemented
  230. * in I/O space. There are two 64-bit registers each, the
  231. * first holds the low 32-bits of the value and the second holds
  232. * the high 32-bits.
  233. *
  234. * Since STICK is constantly updating, we have to access it carefully.
  235. *
  236. * The sequence we use to read is:
  237. * 1) read high
  238. * 2) read low
  239. * 3) read high again, if it rolled re-read both low and high again.
  240. *
  241. * Writing STICK safely is also tricky:
  242. * 1) write low to zero
  243. * 2) write high
  244. * 3) write low
  245. */
  246. #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
  247. #define HBIRD_STICK_ADDR 0x1fe0000f070UL
  248. static unsigned long __hbird_read_stick(void)
  249. {
  250. unsigned long ret, tmp1, tmp2, tmp3;
  251. unsigned long addr = HBIRD_STICK_ADDR+8;
  252. __asm__ __volatile__("ldxa [%1] %5, %2\n"
  253. "1:\n\t"
  254. "sub %1, 0x8, %1\n\t"
  255. "ldxa [%1] %5, %3\n\t"
  256. "add %1, 0x8, %1\n\t"
  257. "ldxa [%1] %5, %4\n\t"
  258. "cmp %4, %2\n\t"
  259. "bne,a,pn %%xcc, 1b\n\t"
  260. " mov %4, %2\n\t"
  261. "sllx %4, 32, %4\n\t"
  262. "or %3, %4, %0\n\t"
  263. : "=&r" (ret), "=&r" (addr),
  264. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  265. : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
  266. return ret;
  267. }
  268. static void __hbird_write_stick(unsigned long val)
  269. {
  270. unsigned long low = (val & 0xffffffffUL);
  271. unsigned long high = (val >> 32UL);
  272. unsigned long addr = HBIRD_STICK_ADDR;
  273. __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
  274. "add %0, 0x8, %0\n\t"
  275. "stxa %3, [%0] %4\n\t"
  276. "sub %0, 0x8, %0\n\t"
  277. "stxa %2, [%0] %4"
  278. : "=&r" (addr)
  279. : "0" (addr), "r" (low), "r" (high),
  280. "i" (ASI_PHYS_BYPASS_EC_E));
  281. }
  282. static void __hbird_write_compare(unsigned long val)
  283. {
  284. unsigned long low = (val & 0xffffffffUL);
  285. unsigned long high = (val >> 32UL);
  286. unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
  287. __asm__ __volatile__("stxa %3, [%0] %4\n\t"
  288. "sub %0, 0x8, %0\n\t"
  289. "stxa %2, [%0] %4"
  290. : "=&r" (addr)
  291. : "0" (addr), "r" (low), "r" (high),
  292. "i" (ASI_PHYS_BYPASS_EC_E));
  293. }
  294. static void hbtick_disable_irq(void)
  295. {
  296. __hbird_write_compare(TICKCMP_IRQ_BIT);
  297. }
  298. static void hbtick_init_tick(void)
  299. {
  300. tick_disable_protection();
  301. /* XXX This seems to be necessary to 'jumpstart' Hummingbird
  302. * XXX into actually sending STICK interrupts. I think because
  303. * XXX of how we store %tick_cmpr in head.S this somehow resets the
  304. * XXX {TICK + STICK} interrupt mux. -DaveM
  305. */
  306. __hbird_write_stick(__hbird_read_stick());
  307. hbtick_disable_irq();
  308. }
  309. static unsigned long hbtick_get_tick(void)
  310. {
  311. return __hbird_read_stick() & ~TICK_PRIV_BIT;
  312. }
  313. static unsigned long hbtick_add_tick(unsigned long adj)
  314. {
  315. unsigned long val;
  316. val = __hbird_read_stick() + adj;
  317. __hbird_write_stick(val);
  318. return val;
  319. }
  320. static int hbtick_add_compare(unsigned long adj)
  321. {
  322. unsigned long val = __hbird_read_stick();
  323. unsigned long val2;
  324. val &= ~TICKCMP_IRQ_BIT;
  325. val += adj;
  326. __hbird_write_compare(val);
  327. val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
  328. return ((long)(val2 - val)) > 0L;
  329. }
  330. static struct sparc64_tick_ops hbtick_operations __read_mostly = {
  331. .name = "hbtick",
  332. .init_tick = hbtick_init_tick,
  333. .disable_irq = hbtick_disable_irq,
  334. .get_tick = hbtick_get_tick,
  335. .add_tick = hbtick_add_tick,
  336. .add_compare = hbtick_add_compare,
  337. .softint_mask = 1UL << 0,
  338. };
  339. static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
  340. int update_persistent_clock(struct timespec now)
  341. {
  342. return set_rtc_mmss(now.tv_sec);
  343. }
  344. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  345. static void __init kick_start_clock(void)
  346. {
  347. void __iomem *regs = mstk48t02_regs;
  348. u8 sec, tmp;
  349. int i, count;
  350. prom_printf("CLOCK: Clock was stopped. Kick start ");
  351. spin_lock_irq(&mostek_lock);
  352. /* Turn on the kick start bit to start the oscillator. */
  353. tmp = mostek_read(regs + MOSTEK_CREG);
  354. tmp |= MSTK_CREG_WRITE;
  355. mostek_write(regs + MOSTEK_CREG, tmp);
  356. tmp = mostek_read(regs + MOSTEK_SEC);
  357. tmp &= ~MSTK_STOP;
  358. mostek_write(regs + MOSTEK_SEC, tmp);
  359. tmp = mostek_read(regs + MOSTEK_HOUR);
  360. tmp |= MSTK_KICK_START;
  361. mostek_write(regs + MOSTEK_HOUR, tmp);
  362. tmp = mostek_read(regs + MOSTEK_CREG);
  363. tmp &= ~MSTK_CREG_WRITE;
  364. mostek_write(regs + MOSTEK_CREG, tmp);
  365. spin_unlock_irq(&mostek_lock);
  366. /* Delay to allow the clock oscillator to start. */
  367. sec = MSTK_REG_SEC(regs);
  368. for (i = 0; i < 3; i++) {
  369. while (sec == MSTK_REG_SEC(regs))
  370. for (count = 0; count < 100000; count++)
  371. /* nothing */ ;
  372. prom_printf(".");
  373. sec = MSTK_REG_SEC(regs);
  374. }
  375. prom_printf("\n");
  376. spin_lock_irq(&mostek_lock);
  377. /* Turn off kick start and set a "valid" time and date. */
  378. tmp = mostek_read(regs + MOSTEK_CREG);
  379. tmp |= MSTK_CREG_WRITE;
  380. mostek_write(regs + MOSTEK_CREG, tmp);
  381. tmp = mostek_read(regs + MOSTEK_HOUR);
  382. tmp &= ~MSTK_KICK_START;
  383. mostek_write(regs + MOSTEK_HOUR, tmp);
  384. MSTK_SET_REG_SEC(regs,0);
  385. MSTK_SET_REG_MIN(regs,0);
  386. MSTK_SET_REG_HOUR(regs,0);
  387. MSTK_SET_REG_DOW(regs,5);
  388. MSTK_SET_REG_DOM(regs,1);
  389. MSTK_SET_REG_MONTH(regs,8);
  390. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  391. tmp = mostek_read(regs + MOSTEK_CREG);
  392. tmp &= ~MSTK_CREG_WRITE;
  393. mostek_write(regs + MOSTEK_CREG, tmp);
  394. spin_unlock_irq(&mostek_lock);
  395. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  396. while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
  397. prom_printf("CLOCK: Kick start still on!\n");
  398. spin_lock_irq(&mostek_lock);
  399. tmp = mostek_read(regs + MOSTEK_CREG);
  400. tmp |= MSTK_CREG_WRITE;
  401. mostek_write(regs + MOSTEK_CREG, tmp);
  402. tmp = mostek_read(regs + MOSTEK_HOUR);
  403. tmp &= ~MSTK_KICK_START;
  404. mostek_write(regs + MOSTEK_HOUR, tmp);
  405. tmp = mostek_read(regs + MOSTEK_CREG);
  406. tmp &= ~MSTK_CREG_WRITE;
  407. mostek_write(regs + MOSTEK_CREG, tmp);
  408. spin_unlock_irq(&mostek_lock);
  409. }
  410. prom_printf("CLOCK: Kick start procedure successful.\n");
  411. }
  412. /* Return nonzero if the clock chip battery is low. */
  413. static int __init has_low_battery(void)
  414. {
  415. void __iomem *regs = mstk48t02_regs;
  416. u8 data1, data2;
  417. spin_lock_irq(&mostek_lock);
  418. data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
  419. mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
  420. data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
  421. mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
  422. spin_unlock_irq(&mostek_lock);
  423. return (data1 == data2); /* Was the write blocked? */
  424. }
  425. /* Probe for the real time clock chip. */
  426. static void __init set_system_time(void)
  427. {
  428. unsigned int year, mon, day, hour, min, sec;
  429. void __iomem *mregs = mstk48t02_regs;
  430. #ifdef CONFIG_PCI
  431. unsigned long dregs = ds1287_regs;
  432. void __iomem *bregs = bq4802_regs;
  433. #else
  434. unsigned long dregs = 0UL;
  435. void __iomem *bregs = 0UL;
  436. #endif
  437. u8 tmp;
  438. if (!mregs && !dregs && !bregs) {
  439. prom_printf("Something wrong, clock regs not mapped yet.\n");
  440. prom_halt();
  441. }
  442. if (mregs) {
  443. spin_lock_irq(&mostek_lock);
  444. /* Traditional Mostek chip. */
  445. tmp = mostek_read(mregs + MOSTEK_CREG);
  446. tmp |= MSTK_CREG_READ;
  447. mostek_write(mregs + MOSTEK_CREG, tmp);
  448. sec = MSTK_REG_SEC(mregs);
  449. min = MSTK_REG_MIN(mregs);
  450. hour = MSTK_REG_HOUR(mregs);
  451. day = MSTK_REG_DOM(mregs);
  452. mon = MSTK_REG_MONTH(mregs);
  453. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  454. } else if (bregs) {
  455. unsigned char val = readb(bregs + 0x0e);
  456. unsigned int century;
  457. /* BQ4802 RTC chip. */
  458. writeb(val | 0x08, bregs + 0x0e);
  459. sec = readb(bregs + 0x00);
  460. min = readb(bregs + 0x02);
  461. hour = readb(bregs + 0x04);
  462. day = readb(bregs + 0x06);
  463. mon = readb(bregs + 0x09);
  464. year = readb(bregs + 0x0a);
  465. century = readb(bregs + 0x0f);
  466. writeb(val, bregs + 0x0e);
  467. BCD_TO_BIN(sec);
  468. BCD_TO_BIN(min);
  469. BCD_TO_BIN(hour);
  470. BCD_TO_BIN(day);
  471. BCD_TO_BIN(mon);
  472. BCD_TO_BIN(year);
  473. BCD_TO_BIN(century);
  474. year += (century * 100);
  475. } else {
  476. /* Dallas 12887 RTC chip. */
  477. do {
  478. sec = CMOS_READ(RTC_SECONDS);
  479. min = CMOS_READ(RTC_MINUTES);
  480. hour = CMOS_READ(RTC_HOURS);
  481. day = CMOS_READ(RTC_DAY_OF_MONTH);
  482. mon = CMOS_READ(RTC_MONTH);
  483. year = CMOS_READ(RTC_YEAR);
  484. } while (sec != CMOS_READ(RTC_SECONDS));
  485. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  486. BCD_TO_BIN(sec);
  487. BCD_TO_BIN(min);
  488. BCD_TO_BIN(hour);
  489. BCD_TO_BIN(day);
  490. BCD_TO_BIN(mon);
  491. BCD_TO_BIN(year);
  492. }
  493. if ((year += 1900) < 1970)
  494. year += 100;
  495. }
  496. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  497. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  498. set_normalized_timespec(&wall_to_monotonic,
  499. -xtime.tv_sec, -xtime.tv_nsec);
  500. if (mregs) {
  501. tmp = mostek_read(mregs + MOSTEK_CREG);
  502. tmp &= ~MSTK_CREG_READ;
  503. mostek_write(mregs + MOSTEK_CREG, tmp);
  504. spin_unlock_irq(&mostek_lock);
  505. }
  506. }
  507. /* davem suggests we keep this within the 4M locked kernel image */
  508. static u32 starfire_get_time(void)
  509. {
  510. static char obp_gettod[32];
  511. static u32 unix_tod;
  512. sprintf(obp_gettod, "h# %08x unix-gettod",
  513. (unsigned int) (long) &unix_tod);
  514. prom_feval(obp_gettod);
  515. return unix_tod;
  516. }
  517. static int starfire_set_time(u32 val)
  518. {
  519. /* Do nothing, time is set using the service processor
  520. * console on this platform.
  521. */
  522. return 0;
  523. }
  524. static u32 hypervisor_get_time(void)
  525. {
  526. unsigned long ret, time;
  527. int retries = 10000;
  528. retry:
  529. ret = sun4v_tod_get(&time);
  530. if (ret == HV_EOK)
  531. return time;
  532. if (ret == HV_EWOULDBLOCK) {
  533. if (--retries > 0) {
  534. udelay(100);
  535. goto retry;
  536. }
  537. printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
  538. return 0;
  539. }
  540. printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
  541. return 0;
  542. }
  543. static int hypervisor_set_time(u32 secs)
  544. {
  545. unsigned long ret;
  546. int retries = 10000;
  547. retry:
  548. ret = sun4v_tod_set(secs);
  549. if (ret == HV_EOK)
  550. return 0;
  551. if (ret == HV_EWOULDBLOCK) {
  552. if (--retries > 0) {
  553. udelay(100);
  554. goto retry;
  555. }
  556. printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
  557. return -EAGAIN;
  558. }
  559. printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
  560. return -EOPNOTSUPP;
  561. }
  562. static int __init clock_model_matches(const char *model)
  563. {
  564. if (strcmp(model, "mk48t02") &&
  565. strcmp(model, "mk48t08") &&
  566. strcmp(model, "mk48t59") &&
  567. strcmp(model, "m5819") &&
  568. strcmp(model, "m5819p") &&
  569. strcmp(model, "m5823") &&
  570. strcmp(model, "ds1287") &&
  571. strcmp(model, "bq4802"))
  572. return 0;
  573. return 1;
  574. }
  575. static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
  576. {
  577. struct device_node *dp = op->node;
  578. const char *model = of_get_property(dp, "model", NULL);
  579. const char *compat = of_get_property(dp, "compatible", NULL);
  580. unsigned long size, flags;
  581. void __iomem *regs;
  582. if (!model)
  583. model = compat;
  584. if (!model || !clock_model_matches(model))
  585. return -ENODEV;
  586. /* On an Enterprise system there can be multiple mostek clocks.
  587. * We should only match the one that is on the central FHC bus.
  588. */
  589. if (!strcmp(dp->parent->name, "fhc") &&
  590. strcmp(dp->parent->parent->name, "central") != 0)
  591. return -ENODEV;
  592. size = (op->resource[0].end - op->resource[0].start) + 1;
  593. regs = of_ioremap(&op->resource[0], 0, size, "clock");
  594. if (!regs)
  595. return -ENOMEM;
  596. #ifdef CONFIG_PCI
  597. if (!strcmp(model, "ds1287") ||
  598. !strcmp(model, "m5819") ||
  599. !strcmp(model, "m5819p") ||
  600. !strcmp(model, "m5823")) {
  601. ds1287_regs = (unsigned long) regs;
  602. } else if (!strcmp(model, "bq4802")) {
  603. bq4802_regs = regs;
  604. } else
  605. #endif
  606. if (model[5] == '0' && model[6] == '2') {
  607. mstk48t02_regs = regs;
  608. } else if(model[5] == '0' && model[6] == '8') {
  609. mstk48t08_regs = regs;
  610. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  611. } else {
  612. mstk48t59_regs = regs;
  613. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  614. }
  615. printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs);
  616. local_irq_save(flags);
  617. if (mstk48t02_regs != NULL) {
  618. /* Report a low battery voltage condition. */
  619. if (has_low_battery())
  620. prom_printf("NVRAM: Low battery voltage!\n");
  621. /* Kick start the clock if it is completely stopped. */
  622. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  623. kick_start_clock();
  624. }
  625. set_system_time();
  626. local_irq_restore(flags);
  627. return 0;
  628. }
  629. static struct of_device_id clock_match[] = {
  630. {
  631. .name = "eeprom",
  632. },
  633. {
  634. .name = "rtc",
  635. },
  636. {},
  637. };
  638. static struct of_platform_driver clock_driver = {
  639. .match_table = clock_match,
  640. .probe = clock_probe,
  641. .driver = {
  642. .name = "clock",
  643. },
  644. };
  645. static int __init clock_init(void)
  646. {
  647. if (this_is_starfire) {
  648. xtime.tv_sec = starfire_get_time();
  649. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  650. set_normalized_timespec(&wall_to_monotonic,
  651. -xtime.tv_sec, -xtime.tv_nsec);
  652. return 0;
  653. }
  654. if (tlb_type == hypervisor) {
  655. xtime.tv_sec = hypervisor_get_time();
  656. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  657. set_normalized_timespec(&wall_to_monotonic,
  658. -xtime.tv_sec, -xtime.tv_nsec);
  659. return 0;
  660. }
  661. return of_register_driver(&clock_driver, &of_platform_bus_type);
  662. }
  663. /* Must be after subsys_initcall() so that busses are probed. Must
  664. * be before device_initcall() because things like the RTC driver
  665. * need to see the clock registers.
  666. */
  667. fs_initcall(clock_init);
  668. /* This is gets the master TICK_INT timer going. */
  669. static unsigned long sparc64_init_timers(void)
  670. {
  671. struct device_node *dp;
  672. unsigned long clock;
  673. dp = of_find_node_by_path("/");
  674. if (tlb_type == spitfire) {
  675. unsigned long ver, manuf, impl;
  676. __asm__ __volatile__ ("rdpr %%ver, %0"
  677. : "=&r" (ver));
  678. manuf = ((ver >> 48) & 0xffff);
  679. impl = ((ver >> 32) & 0xffff);
  680. if (manuf == 0x17 && impl == 0x13) {
  681. /* Hummingbird, aka Ultra-IIe */
  682. tick_ops = &hbtick_operations;
  683. clock = of_getintprop_default(dp, "stick-frequency", 0);
  684. } else {
  685. tick_ops = &tick_operations;
  686. clock = local_cpu_data().clock_tick;
  687. }
  688. } else {
  689. tick_ops = &stick_operations;
  690. clock = of_getintprop_default(dp, "stick-frequency", 0);
  691. }
  692. return clock;
  693. }
  694. struct freq_table {
  695. unsigned long clock_tick_ref;
  696. unsigned int ref_freq;
  697. };
  698. static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
  699. unsigned long sparc64_get_clock_tick(unsigned int cpu)
  700. {
  701. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  702. if (ft->clock_tick_ref)
  703. return ft->clock_tick_ref;
  704. return cpu_data(cpu).clock_tick;
  705. }
  706. #ifdef CONFIG_CPU_FREQ
  707. static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  708. void *data)
  709. {
  710. struct cpufreq_freqs *freq = data;
  711. unsigned int cpu = freq->cpu;
  712. struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
  713. if (!ft->ref_freq) {
  714. ft->ref_freq = freq->old;
  715. ft->clock_tick_ref = cpu_data(cpu).clock_tick;
  716. }
  717. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  718. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  719. (val == CPUFREQ_RESUMECHANGE)) {
  720. cpu_data(cpu).clock_tick =
  721. cpufreq_scale(ft->clock_tick_ref,
  722. ft->ref_freq,
  723. freq->new);
  724. }
  725. return 0;
  726. }
  727. static struct notifier_block sparc64_cpufreq_notifier_block = {
  728. .notifier_call = sparc64_cpufreq_notifier
  729. };
  730. #endif /* CONFIG_CPU_FREQ */
  731. static int sparc64_next_event(unsigned long delta,
  732. struct clock_event_device *evt)
  733. {
  734. return tick_ops->add_compare(delta) ? -ETIME : 0;
  735. }
  736. static void sparc64_timer_setup(enum clock_event_mode mode,
  737. struct clock_event_device *evt)
  738. {
  739. switch (mode) {
  740. case CLOCK_EVT_MODE_ONESHOT:
  741. case CLOCK_EVT_MODE_RESUME:
  742. break;
  743. case CLOCK_EVT_MODE_SHUTDOWN:
  744. tick_ops->disable_irq();
  745. break;
  746. case CLOCK_EVT_MODE_PERIODIC:
  747. case CLOCK_EVT_MODE_UNUSED:
  748. WARN_ON(1);
  749. break;
  750. };
  751. }
  752. static struct clock_event_device sparc64_clockevent = {
  753. .features = CLOCK_EVT_FEAT_ONESHOT,
  754. .set_mode = sparc64_timer_setup,
  755. .set_next_event = sparc64_next_event,
  756. .rating = 100,
  757. .shift = 30,
  758. .irq = -1,
  759. };
  760. static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
  761. void timer_interrupt(int irq, struct pt_regs *regs)
  762. {
  763. struct pt_regs *old_regs = set_irq_regs(regs);
  764. unsigned long tick_mask = tick_ops->softint_mask;
  765. int cpu = smp_processor_id();
  766. struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
  767. clear_softint(tick_mask);
  768. irq_enter();
  769. kstat_this_cpu.irqs[0]++;
  770. if (unlikely(!evt->event_handler)) {
  771. printk(KERN_WARNING
  772. "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
  773. } else
  774. evt->event_handler(evt);
  775. irq_exit();
  776. set_irq_regs(old_regs);
  777. }
  778. void __devinit setup_sparc64_timer(void)
  779. {
  780. struct clock_event_device *sevt;
  781. unsigned long pstate;
  782. /* Guarantee that the following sequences execute
  783. * uninterrupted.
  784. */
  785. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  786. "wrpr %0, %1, %%pstate"
  787. : "=r" (pstate)
  788. : "i" (PSTATE_IE));
  789. tick_ops->init_tick();
  790. /* Restore PSTATE_IE. */
  791. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  792. : /* no outputs */
  793. : "r" (pstate));
  794. sevt = &__get_cpu_var(sparc64_events);
  795. memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
  796. sevt->cpumask = cpumask_of_cpu(smp_processor_id());
  797. clockevents_register_device(sevt);
  798. }
  799. #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
  800. static struct clocksource clocksource_tick = {
  801. .rating = 100,
  802. .mask = CLOCKSOURCE_MASK(64),
  803. .shift = 16,
  804. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  805. };
  806. static void __init setup_clockevent_multiplier(unsigned long hz)
  807. {
  808. unsigned long mult, shift = 32;
  809. while (1) {
  810. mult = div_sc(hz, NSEC_PER_SEC, shift);
  811. if (mult && (mult >> 32UL) == 0UL)
  812. break;
  813. shift--;
  814. }
  815. sparc64_clockevent.shift = shift;
  816. sparc64_clockevent.mult = mult;
  817. }
  818. static unsigned long tb_ticks_per_usec __read_mostly;
  819. void __delay(unsigned long loops)
  820. {
  821. unsigned long bclock, now;
  822. bclock = tick_ops->get_tick();
  823. do {
  824. now = tick_ops->get_tick();
  825. } while ((now-bclock) < loops);
  826. }
  827. EXPORT_SYMBOL(__delay);
  828. void udelay(unsigned long usecs)
  829. {
  830. __delay(tb_ticks_per_usec * usecs);
  831. }
  832. EXPORT_SYMBOL(udelay);
  833. void __init time_init(void)
  834. {
  835. unsigned long clock = sparc64_init_timers();
  836. tb_ticks_per_usec = clock / USEC_PER_SEC;
  837. timer_ticks_per_nsec_quotient =
  838. clocksource_hz2mult(clock, SPARC64_NSEC_PER_CYC_SHIFT);
  839. clocksource_tick.name = tick_ops->name;
  840. clocksource_tick.mult =
  841. clocksource_hz2mult(clock,
  842. clocksource_tick.shift);
  843. clocksource_tick.read = tick_ops->get_tick;
  844. printk("clocksource: mult[%x] shift[%d]\n",
  845. clocksource_tick.mult, clocksource_tick.shift);
  846. clocksource_register(&clocksource_tick);
  847. sparc64_clockevent.name = tick_ops->name;
  848. setup_clockevent_multiplier(clock);
  849. sparc64_clockevent.max_delta_ns =
  850. clockevent_delta2ns(0x7fffffffffffffff, &sparc64_clockevent);
  851. sparc64_clockevent.min_delta_ns =
  852. clockevent_delta2ns(0xF, &sparc64_clockevent);
  853. printk("clockevent: mult[%lx] shift[%d]\n",
  854. sparc64_clockevent.mult, sparc64_clockevent.shift);
  855. setup_sparc64_timer();
  856. #ifdef CONFIG_CPU_FREQ
  857. cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
  858. CPUFREQ_TRANSITION_NOTIFIER);
  859. #endif
  860. }
  861. unsigned long long sched_clock(void)
  862. {
  863. unsigned long ticks = tick_ops->get_tick();
  864. return (ticks * timer_ticks_per_nsec_quotient)
  865. >> SPARC64_NSEC_PER_CYC_SHIFT;
  866. }
  867. static int set_rtc_mmss(unsigned long nowtime)
  868. {
  869. int real_seconds, real_minutes, chip_minutes;
  870. void __iomem *mregs = mstk48t02_regs;
  871. #ifdef CONFIG_PCI
  872. unsigned long dregs = ds1287_regs;
  873. void __iomem *bregs = bq4802_regs;
  874. #else
  875. unsigned long dregs = 0UL;
  876. void __iomem *bregs = 0UL;
  877. #endif
  878. unsigned long flags;
  879. u8 tmp;
  880. /*
  881. * Not having a register set can lead to trouble.
  882. * Also starfire doesn't have a tod clock.
  883. */
  884. if (!mregs && !dregs && !bregs)
  885. return -1;
  886. if (mregs) {
  887. spin_lock_irqsave(&mostek_lock, flags);
  888. /* Read the current RTC minutes. */
  889. tmp = mostek_read(mregs + MOSTEK_CREG);
  890. tmp |= MSTK_CREG_READ;
  891. mostek_write(mregs + MOSTEK_CREG, tmp);
  892. chip_minutes = MSTK_REG_MIN(mregs);
  893. tmp = mostek_read(mregs + MOSTEK_CREG);
  894. tmp &= ~MSTK_CREG_READ;
  895. mostek_write(mregs + MOSTEK_CREG, tmp);
  896. /*
  897. * since we're only adjusting minutes and seconds,
  898. * don't interfere with hour overflow. This avoids
  899. * messing with unknown time zones but requires your
  900. * RTC not to be off by more than 15 minutes
  901. */
  902. real_seconds = nowtime % 60;
  903. real_minutes = nowtime / 60;
  904. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  905. real_minutes += 30; /* correct for half hour time zone */
  906. real_minutes %= 60;
  907. if (abs(real_minutes - chip_minutes) < 30) {
  908. tmp = mostek_read(mregs + MOSTEK_CREG);
  909. tmp |= MSTK_CREG_WRITE;
  910. mostek_write(mregs + MOSTEK_CREG, tmp);
  911. MSTK_SET_REG_SEC(mregs,real_seconds);
  912. MSTK_SET_REG_MIN(mregs,real_minutes);
  913. tmp = mostek_read(mregs + MOSTEK_CREG);
  914. tmp &= ~MSTK_CREG_WRITE;
  915. mostek_write(mregs + MOSTEK_CREG, tmp);
  916. spin_unlock_irqrestore(&mostek_lock, flags);
  917. return 0;
  918. } else {
  919. spin_unlock_irqrestore(&mostek_lock, flags);
  920. return -1;
  921. }
  922. } else if (bregs) {
  923. int retval = 0;
  924. unsigned char val = readb(bregs + 0x0e);
  925. /* BQ4802 RTC chip. */
  926. writeb(val | 0x08, bregs + 0x0e);
  927. chip_minutes = readb(bregs + 0x02);
  928. BCD_TO_BIN(chip_minutes);
  929. real_seconds = nowtime % 60;
  930. real_minutes = nowtime / 60;
  931. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  932. real_minutes += 30;
  933. real_minutes %= 60;
  934. if (abs(real_minutes - chip_minutes) < 30) {
  935. BIN_TO_BCD(real_seconds);
  936. BIN_TO_BCD(real_minutes);
  937. writeb(real_seconds, bregs + 0x00);
  938. writeb(real_minutes, bregs + 0x02);
  939. } else {
  940. printk(KERN_WARNING
  941. "set_rtc_mmss: can't update from %d to %d\n",
  942. chip_minutes, real_minutes);
  943. retval = -1;
  944. }
  945. writeb(val, bregs + 0x0e);
  946. return retval;
  947. } else {
  948. int retval = 0;
  949. unsigned char save_control, save_freq_select;
  950. /* Stolen from arch/i386/kernel/time.c, see there for
  951. * credits and descriptive comments.
  952. */
  953. spin_lock_irqsave(&rtc_lock, flags);
  954. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  955. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  956. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  957. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  958. chip_minutes = CMOS_READ(RTC_MINUTES);
  959. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  960. BCD_TO_BIN(chip_minutes);
  961. real_seconds = nowtime % 60;
  962. real_minutes = nowtime / 60;
  963. if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
  964. real_minutes += 30;
  965. real_minutes %= 60;
  966. if (abs(real_minutes - chip_minutes) < 30) {
  967. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  968. BIN_TO_BCD(real_seconds);
  969. BIN_TO_BCD(real_minutes);
  970. }
  971. CMOS_WRITE(real_seconds,RTC_SECONDS);
  972. CMOS_WRITE(real_minutes,RTC_MINUTES);
  973. } else {
  974. printk(KERN_WARNING
  975. "set_rtc_mmss: can't update from %d to %d\n",
  976. chip_minutes, real_minutes);
  977. retval = -1;
  978. }
  979. CMOS_WRITE(save_control, RTC_CONTROL);
  980. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  981. spin_unlock_irqrestore(&rtc_lock, flags);
  982. return retval;
  983. }
  984. }
  985. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  986. static unsigned char mini_rtc_status; /* bitmapped status byte. */
  987. #define FEBRUARY 2
  988. #define STARTOFTIME 1970
  989. #define SECDAY 86400L
  990. #define SECYR (SECDAY * 365)
  991. #define leapyear(year) ((year) % 4 == 0 && \
  992. ((year) % 100 != 0 || (year) % 400 == 0))
  993. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  994. #define days_in_month(a) (month_days[(a) - 1])
  995. static int month_days[12] = {
  996. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  997. };
  998. /*
  999. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  1000. */
  1001. static void GregorianDay(struct rtc_time * tm)
  1002. {
  1003. int leapsToDate;
  1004. int lastYear;
  1005. int day;
  1006. int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  1007. lastYear = tm->tm_year - 1;
  1008. /*
  1009. * Number of leap corrections to apply up to end of last year
  1010. */
  1011. leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
  1012. /*
  1013. * This year is a leap year if it is divisible by 4 except when it is
  1014. * divisible by 100 unless it is divisible by 400
  1015. *
  1016. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
  1017. */
  1018. day = tm->tm_mon > 2 && leapyear(tm->tm_year);
  1019. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
  1020. tm->tm_mday;
  1021. tm->tm_wday = day % 7;
  1022. }
  1023. static void to_tm(int tim, struct rtc_time *tm)
  1024. {
  1025. register int i;
  1026. register long hms, day;
  1027. day = tim / SECDAY;
  1028. hms = tim % SECDAY;
  1029. /* Hours, minutes, seconds are easy */
  1030. tm->tm_hour = hms / 3600;
  1031. tm->tm_min = (hms % 3600) / 60;
  1032. tm->tm_sec = (hms % 3600) % 60;
  1033. /* Number of years in days */
  1034. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  1035. day -= days_in_year(i);
  1036. tm->tm_year = i;
  1037. /* Number of months in days left */
  1038. if (leapyear(tm->tm_year))
  1039. days_in_month(FEBRUARY) = 29;
  1040. for (i = 1; day >= days_in_month(i); i++)
  1041. day -= days_in_month(i);
  1042. days_in_month(FEBRUARY) = 28;
  1043. tm->tm_mon = i;
  1044. /* Days are what is left over (+1) from all that. */
  1045. tm->tm_mday = day + 1;
  1046. /*
  1047. * Determine the day of week
  1048. */
  1049. GregorianDay(tm);
  1050. }
  1051. /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
  1052. * aka Unix time. So we have to convert to/from rtc_time.
  1053. */
  1054. static void starfire_get_rtc_time(struct rtc_time *time)
  1055. {
  1056. u32 seconds = starfire_get_time();
  1057. to_tm(seconds, time);
  1058. time->tm_year -= 1900;
  1059. time->tm_mon -= 1;
  1060. }
  1061. static int starfire_set_rtc_time(struct rtc_time *time)
  1062. {
  1063. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  1064. time->tm_mday, time->tm_hour,
  1065. time->tm_min, time->tm_sec);
  1066. return starfire_set_time(seconds);
  1067. }
  1068. static void hypervisor_get_rtc_time(struct rtc_time *time)
  1069. {
  1070. u32 seconds = hypervisor_get_time();
  1071. to_tm(seconds, time);
  1072. time->tm_year -= 1900;
  1073. time->tm_mon -= 1;
  1074. }
  1075. static int hypervisor_set_rtc_time(struct rtc_time *time)
  1076. {
  1077. u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
  1078. time->tm_mday, time->tm_hour,
  1079. time->tm_min, time->tm_sec);
  1080. return hypervisor_set_time(seconds);
  1081. }
  1082. #ifdef CONFIG_PCI
  1083. static void bq4802_get_rtc_time(struct rtc_time *time)
  1084. {
  1085. unsigned char val = readb(bq4802_regs + 0x0e);
  1086. unsigned int century;
  1087. writeb(val | 0x08, bq4802_regs + 0x0e);
  1088. time->tm_sec = readb(bq4802_regs + 0x00);
  1089. time->tm_min = readb(bq4802_regs + 0x02);
  1090. time->tm_hour = readb(bq4802_regs + 0x04);
  1091. time->tm_mday = readb(bq4802_regs + 0x06);
  1092. time->tm_mon = readb(bq4802_regs + 0x09);
  1093. time->tm_year = readb(bq4802_regs + 0x0a);
  1094. time->tm_wday = readb(bq4802_regs + 0x08);
  1095. century = readb(bq4802_regs + 0x0f);
  1096. writeb(val, bq4802_regs + 0x0e);
  1097. BCD_TO_BIN(time->tm_sec);
  1098. BCD_TO_BIN(time->tm_min);
  1099. BCD_TO_BIN(time->tm_hour);
  1100. BCD_TO_BIN(time->tm_mday);
  1101. BCD_TO_BIN(time->tm_mon);
  1102. BCD_TO_BIN(time->tm_year);
  1103. BCD_TO_BIN(time->tm_wday);
  1104. BCD_TO_BIN(century);
  1105. time->tm_year += (century * 100);
  1106. time->tm_year -= 1900;
  1107. time->tm_mon--;
  1108. }
  1109. static int bq4802_set_rtc_time(struct rtc_time *time)
  1110. {
  1111. unsigned char val = readb(bq4802_regs + 0x0e);
  1112. unsigned char sec, min, hrs, day, mon, yrs, century;
  1113. unsigned int year;
  1114. year = time->tm_year + 1900;
  1115. century = year / 100;
  1116. yrs = year % 100;
  1117. mon = time->tm_mon + 1; /* tm_mon starts at zero */
  1118. day = time->tm_mday;
  1119. hrs = time->tm_hour;
  1120. min = time->tm_min;
  1121. sec = time->tm_sec;
  1122. BIN_TO_BCD(sec);
  1123. BIN_TO_BCD(min);
  1124. BIN_TO_BCD(hrs);
  1125. BIN_TO_BCD(day);
  1126. BIN_TO_BCD(mon);
  1127. BIN_TO_BCD(yrs);
  1128. BIN_TO_BCD(century);
  1129. writeb(val | 0x08, bq4802_regs + 0x0e);
  1130. writeb(sec, bq4802_regs + 0x00);
  1131. writeb(min, bq4802_regs + 0x02);
  1132. writeb(hrs, bq4802_regs + 0x04);
  1133. writeb(day, bq4802_regs + 0x06);
  1134. writeb(mon, bq4802_regs + 0x09);
  1135. writeb(yrs, bq4802_regs + 0x0a);
  1136. writeb(century, bq4802_regs + 0x0f);
  1137. writeb(val, bq4802_regs + 0x0e);
  1138. return 0;
  1139. }
  1140. static void cmos_get_rtc_time(struct rtc_time *rtc_tm)
  1141. {
  1142. unsigned char ctrl;
  1143. rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
  1144. rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
  1145. rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
  1146. rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  1147. rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
  1148. rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
  1149. rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
  1150. ctrl = CMOS_READ(RTC_CONTROL);
  1151. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  1152. BCD_TO_BIN(rtc_tm->tm_sec);
  1153. BCD_TO_BIN(rtc_tm->tm_min);
  1154. BCD_TO_BIN(rtc_tm->tm_hour);
  1155. BCD_TO_BIN(rtc_tm->tm_mday);
  1156. BCD_TO_BIN(rtc_tm->tm_mon);
  1157. BCD_TO_BIN(rtc_tm->tm_year);
  1158. BCD_TO_BIN(rtc_tm->tm_wday);
  1159. }
  1160. if (rtc_tm->tm_year <= 69)
  1161. rtc_tm->tm_year += 100;
  1162. rtc_tm->tm_mon--;
  1163. }
  1164. static int cmos_set_rtc_time(struct rtc_time *rtc_tm)
  1165. {
  1166. unsigned char mon, day, hrs, min, sec;
  1167. unsigned char save_control, save_freq_select;
  1168. unsigned int yrs;
  1169. yrs = rtc_tm->tm_year;
  1170. mon = rtc_tm->tm_mon + 1;
  1171. day = rtc_tm->tm_mday;
  1172. hrs = rtc_tm->tm_hour;
  1173. min = rtc_tm->tm_min;
  1174. sec = rtc_tm->tm_sec;
  1175. if (yrs >= 100)
  1176. yrs -= 100;
  1177. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  1178. BIN_TO_BCD(sec);
  1179. BIN_TO_BCD(min);
  1180. BIN_TO_BCD(hrs);
  1181. BIN_TO_BCD(day);
  1182. BIN_TO_BCD(mon);
  1183. BIN_TO_BCD(yrs);
  1184. }
  1185. save_control = CMOS_READ(RTC_CONTROL);
  1186. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  1187. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  1188. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  1189. CMOS_WRITE(yrs, RTC_YEAR);
  1190. CMOS_WRITE(mon, RTC_MONTH);
  1191. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  1192. CMOS_WRITE(hrs, RTC_HOURS);
  1193. CMOS_WRITE(min, RTC_MINUTES);
  1194. CMOS_WRITE(sec, RTC_SECONDS);
  1195. CMOS_WRITE(save_control, RTC_CONTROL);
  1196. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  1197. return 0;
  1198. }
  1199. #endif /* CONFIG_PCI */
  1200. static void mostek_get_rtc_time(struct rtc_time *rtc_tm)
  1201. {
  1202. void __iomem *regs = mstk48t02_regs;
  1203. u8 tmp;
  1204. spin_lock_irq(&mostek_lock);
  1205. tmp = mostek_read(regs + MOSTEK_CREG);
  1206. tmp |= MSTK_CREG_READ;
  1207. mostek_write(regs + MOSTEK_CREG, tmp);
  1208. rtc_tm->tm_sec = MSTK_REG_SEC(regs);
  1209. rtc_tm->tm_min = MSTK_REG_MIN(regs);
  1210. rtc_tm->tm_hour = MSTK_REG_HOUR(regs);
  1211. rtc_tm->tm_mday = MSTK_REG_DOM(regs);
  1212. rtc_tm->tm_mon = MSTK_REG_MONTH(regs);
  1213. rtc_tm->tm_year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
  1214. rtc_tm->tm_wday = MSTK_REG_DOW(regs);
  1215. tmp = mostek_read(regs + MOSTEK_CREG);
  1216. tmp &= ~MSTK_CREG_READ;
  1217. mostek_write(regs + MOSTEK_CREG, tmp);
  1218. spin_unlock_irq(&mostek_lock);
  1219. rtc_tm->tm_mon--;
  1220. rtc_tm->tm_wday--;
  1221. rtc_tm->tm_year -= 1900;
  1222. }
  1223. static int mostek_set_rtc_time(struct rtc_time *rtc_tm)
  1224. {
  1225. unsigned char mon, day, hrs, min, sec, wday;
  1226. void __iomem *regs = mstk48t02_regs;
  1227. unsigned int yrs;
  1228. u8 tmp;
  1229. yrs = rtc_tm->tm_year + 1900;
  1230. mon = rtc_tm->tm_mon + 1;
  1231. day = rtc_tm->tm_mday;
  1232. wday = rtc_tm->tm_wday + 1;
  1233. hrs = rtc_tm->tm_hour;
  1234. min = rtc_tm->tm_min;
  1235. sec = rtc_tm->tm_sec;
  1236. spin_lock_irq(&mostek_lock);
  1237. tmp = mostek_read(regs + MOSTEK_CREG);
  1238. tmp |= MSTK_CREG_WRITE;
  1239. mostek_write(regs + MOSTEK_CREG, tmp);
  1240. MSTK_SET_REG_SEC(regs, sec);
  1241. MSTK_SET_REG_MIN(regs, min);
  1242. MSTK_SET_REG_HOUR(regs, hrs);
  1243. MSTK_SET_REG_DOW(regs, wday);
  1244. MSTK_SET_REG_DOM(regs, day);
  1245. MSTK_SET_REG_MONTH(regs, mon);
  1246. MSTK_SET_REG_YEAR(regs, yrs - MSTK_YEAR_ZERO);
  1247. tmp = mostek_read(regs + MOSTEK_CREG);
  1248. tmp &= ~MSTK_CREG_WRITE;
  1249. mostek_write(regs + MOSTEK_CREG, tmp);
  1250. spin_unlock_irq(&mostek_lock);
  1251. return 0;
  1252. }
  1253. struct mini_rtc_ops {
  1254. void (*get_rtc_time)(struct rtc_time *);
  1255. int (*set_rtc_time)(struct rtc_time *);
  1256. };
  1257. static struct mini_rtc_ops starfire_rtc_ops = {
  1258. .get_rtc_time = starfire_get_rtc_time,
  1259. .set_rtc_time = starfire_set_rtc_time,
  1260. };
  1261. static struct mini_rtc_ops hypervisor_rtc_ops = {
  1262. .get_rtc_time = hypervisor_get_rtc_time,
  1263. .set_rtc_time = hypervisor_set_rtc_time,
  1264. };
  1265. #ifdef CONFIG_PCI
  1266. static struct mini_rtc_ops bq4802_rtc_ops = {
  1267. .get_rtc_time = bq4802_get_rtc_time,
  1268. .set_rtc_time = bq4802_set_rtc_time,
  1269. };
  1270. static struct mini_rtc_ops cmos_rtc_ops = {
  1271. .get_rtc_time = cmos_get_rtc_time,
  1272. .set_rtc_time = cmos_set_rtc_time,
  1273. };
  1274. #endif /* CONFIG_PCI */
  1275. static struct mini_rtc_ops mostek_rtc_ops = {
  1276. .get_rtc_time = mostek_get_rtc_time,
  1277. .set_rtc_time = mostek_set_rtc_time,
  1278. };
  1279. static struct mini_rtc_ops *mini_rtc_ops;
  1280. static inline void mini_get_rtc_time(struct rtc_time *time)
  1281. {
  1282. unsigned long flags;
  1283. spin_lock_irqsave(&rtc_lock, flags);
  1284. mini_rtc_ops->get_rtc_time(time);
  1285. spin_unlock_irqrestore(&rtc_lock, flags);
  1286. }
  1287. static inline int mini_set_rtc_time(struct rtc_time *time)
  1288. {
  1289. unsigned long flags;
  1290. int err;
  1291. spin_lock_irqsave(&rtc_lock, flags);
  1292. err = mini_rtc_ops->set_rtc_time(time);
  1293. spin_unlock_irqrestore(&rtc_lock, flags);
  1294. return err;
  1295. }
  1296. static int mini_rtc_ioctl(struct inode *inode, struct file *file,
  1297. unsigned int cmd, unsigned long arg)
  1298. {
  1299. struct rtc_time wtime;
  1300. void __user *argp = (void __user *)arg;
  1301. switch (cmd) {
  1302. case RTC_PLL_GET:
  1303. return -EINVAL;
  1304. case RTC_PLL_SET:
  1305. return -EINVAL;
  1306. case RTC_UIE_OFF: /* disable ints from RTC updates. */
  1307. return 0;
  1308. case RTC_UIE_ON: /* enable ints for RTC updates. */
  1309. return -EINVAL;
  1310. case RTC_RD_TIME: /* Read the time/date from RTC */
  1311. /* this doesn't get week-day, who cares */
  1312. memset(&wtime, 0, sizeof(wtime));
  1313. mini_get_rtc_time(&wtime);
  1314. return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
  1315. case RTC_SET_TIME: /* Set the RTC */
  1316. {
  1317. int year, days;
  1318. if (!capable(CAP_SYS_TIME))
  1319. return -EACCES;
  1320. if (copy_from_user(&wtime, argp, sizeof(wtime)))
  1321. return -EFAULT;
  1322. year = wtime.tm_year + 1900;
  1323. days = month_days[wtime.tm_mon] +
  1324. ((wtime.tm_mon == 1) && leapyear(year));
  1325. if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) ||
  1326. (wtime.tm_mday < 1))
  1327. return -EINVAL;
  1328. if (wtime.tm_mday < 0 || wtime.tm_mday > days)
  1329. return -EINVAL;
  1330. if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
  1331. wtime.tm_min < 0 || wtime.tm_min >= 60 ||
  1332. wtime.tm_sec < 0 || wtime.tm_sec >= 60)
  1333. return -EINVAL;
  1334. return mini_set_rtc_time(&wtime);
  1335. }
  1336. }
  1337. return -EINVAL;
  1338. }
  1339. static int mini_rtc_open(struct inode *inode, struct file *file)
  1340. {
  1341. if (mini_rtc_status & RTC_IS_OPEN)
  1342. return -EBUSY;
  1343. mini_rtc_status |= RTC_IS_OPEN;
  1344. return 0;
  1345. }
  1346. static int mini_rtc_release(struct inode *inode, struct file *file)
  1347. {
  1348. mini_rtc_status &= ~RTC_IS_OPEN;
  1349. return 0;
  1350. }
  1351. static const struct file_operations mini_rtc_fops = {
  1352. .owner = THIS_MODULE,
  1353. .ioctl = mini_rtc_ioctl,
  1354. .open = mini_rtc_open,
  1355. .release = mini_rtc_release,
  1356. };
  1357. static struct miscdevice rtc_mini_dev =
  1358. {
  1359. .minor = RTC_MINOR,
  1360. .name = "rtc",
  1361. .fops = &mini_rtc_fops,
  1362. };
  1363. static int __init rtc_mini_init(void)
  1364. {
  1365. int retval;
  1366. if (tlb_type == hypervisor)
  1367. mini_rtc_ops = &hypervisor_rtc_ops;
  1368. else if (this_is_starfire)
  1369. mini_rtc_ops = &starfire_rtc_ops;
  1370. #ifdef CONFIG_PCI
  1371. else if (bq4802_regs)
  1372. mini_rtc_ops = &bq4802_rtc_ops;
  1373. else if (ds1287_regs)
  1374. mini_rtc_ops = &cmos_rtc_ops;
  1375. #endif /* CONFIG_PCI */
  1376. else if (mstk48t02_regs)
  1377. mini_rtc_ops = &mostek_rtc_ops;
  1378. else
  1379. return -ENODEV;
  1380. printk(KERN_INFO "Mini RTC Driver\n");
  1381. retval = misc_register(&rtc_mini_dev);
  1382. if (retval < 0)
  1383. return retval;
  1384. return 0;
  1385. }
  1386. static void __exit rtc_mini_exit(void)
  1387. {
  1388. misc_deregister(&rtc_mini_dev);
  1389. }
  1390. module_init(rtc_mini_init);
  1391. module_exit(rtc_mini_exit);