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/profile.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/rtc.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/clockchips.h>
  35. #include <linux/clocksource.h>
  36. #include <asm/oplib.h>
  37. #include <asm/mostek.h>
  38. #include <asm/timer.h>
  39. #include <asm/irq.h>
  40. #include <asm/io.h>
  41. #include <asm/prom.h>
  42. #include <asm/of_device.h>
  43. #include <asm/starfire.h>
  44. #include <asm/smp.h>
  45. #include <asm/sections.h>
  46. #include <asm/cpudata.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/prom.h>
  49. #include <asm/irq_regs.h>
  50. DEFINE_SPINLOCK(mostek_lock);
  51. DEFINE_SPINLOCK(rtc_lock);
  52. void __iomem *mstk48t02_regs = NULL;
  53. #ifdef CONFIG_PCI
  54. unsigned long ds1287_regs = 0UL;
  55. static void __iomem *bq4802_regs;
  56. #endif
  57. static void __iomem *mstk48t08_regs;
  58. static void __iomem *mstk48t59_regs;
  59. static int set_rtc_mmss(unsigned long);
  60. #define TICK_PRIV_BIT (1UL << 63)
  61. #define TICKCMP_IRQ_BIT (1UL << 63)
  62. #ifdef CONFIG_SMP
  63. unsigned long profile_pc(struct pt_regs *regs)
  64. {
  65. unsigned long pc = instruction_pointer(regs);
  66. if (in_lock_functions(pc))
  67. return regs->u_regs[UREG_RETPC];
  68. return pc;
  69. }
  70. EXPORT_SYMBOL(profile_pc);
  71. #endif
  72. static void tick_disable_protection(void)
  73. {
  74. /* Set things up so user can access tick register for profiling
  75. * purposes. Also workaround BB_ERRATA_1 by doing a dummy
  76. * read back of %tick after writing it.
  77. */
  78. __asm__ __volatile__(
  79. " ba,pt %%xcc, 1f\n"
  80. " nop\n"
  81. " .align 64\n"
  82. "1: rd %%tick, %%g2\n"
  83. " add %%g2, 6, %%g2\n"
  84. " andn %%g2, %0, %%g2\n"
  85. " wrpr %%g2, 0, %%tick\n"
  86. " rdpr %%tick, %%g0"
  87. : /* no outputs */
  88. : "r" (TICK_PRIV_BIT)
  89. : "g2");
  90. }
  91. static void tick_disable_irq(void)
  92. {
  93. __asm__ __volatile__(
  94. " ba,pt %%xcc, 1f\n"
  95. " nop\n"
  96. " .align 64\n"
  97. "1: wr %0, 0x0, %%tick_cmpr\n"
  98. " rd %%tick_cmpr, %%g0"
  99. : /* no outputs */
  100. : "r" (TICKCMP_IRQ_BIT));
  101. }
  102. static void tick_init_tick(void)
  103. {
  104. tick_disable_protection();
  105. tick_disable_irq();
  106. }
  107. static unsigned long tick_get_tick(void)
  108. {
  109. unsigned long ret;
  110. __asm__ __volatile__("rd %%tick, %0\n\t"
  111. "mov %0, %0"
  112. : "=r" (ret));
  113. return ret & ~TICK_PRIV_BIT;
  114. }
  115. static int tick_add_compare(unsigned long adj)
  116. {
  117. unsigned long orig_tick, new_tick, new_compare;
  118. __asm__ __volatile__("rd %%tick, %0"
  119. : "=r" (orig_tick));
  120. orig_tick &= ~TICKCMP_IRQ_BIT;
  121. /* Workaround for Spitfire Errata (#54 I think??), I discovered
  122. * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
  123. * number 103640.
  124. *
  125. * On Blackbird writes to %tick_cmpr can fail, the
  126. * workaround seems to be to execute the wr instruction
  127. * at the start of an I-cache line, and perform a dummy
  128. * read back from %tick_cmpr right after writing to it. -DaveM
  129. */
  130. __asm__ __volatile__("ba,pt %%xcc, 1f\n\t"
  131. " add %1, %2, %0\n\t"
  132. ".align 64\n"
  133. "1:\n\t"
  134. "wr %0, 0, %%tick_cmpr\n\t"
  135. "rd %%tick_cmpr, %%g0\n\t"
  136. : "=r" (new_compare)
  137. : "r" (orig_tick), "r" (adj));
  138. __asm__ __volatile__("rd %%tick, %0"
  139. : "=r" (new_tick));
  140. new_tick &= ~TICKCMP_IRQ_BIT;
  141. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  142. }
  143. static unsigned long tick_add_tick(unsigned long adj)
  144. {
  145. unsigned long new_tick;
  146. /* Also need to handle Blackbird bug here too. */
  147. __asm__ __volatile__("rd %%tick, %0\n\t"
  148. "add %0, %1, %0\n\t"
  149. "wrpr %0, 0, %%tick\n\t"
  150. : "=&r" (new_tick)
  151. : "r" (adj));
  152. return new_tick;
  153. }
  154. static struct sparc64_tick_ops tick_operations __read_mostly = {
  155. .name = "tick",
  156. .init_tick = tick_init_tick,
  157. .disable_irq = tick_disable_irq,
  158. .get_tick = tick_get_tick,
  159. .add_tick = tick_add_tick,
  160. .add_compare = tick_add_compare,
  161. .softint_mask = 1UL << 0,
  162. };
  163. struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
  164. static void stick_disable_irq(void)
  165. {
  166. __asm__ __volatile__(
  167. "wr %0, 0x0, %%asr25"
  168. : /* no outputs */
  169. : "r" (TICKCMP_IRQ_BIT));
  170. }
  171. static void stick_init_tick(void)
  172. {
  173. /* Writes to the %tick and %stick register are not
  174. * allowed on sun4v. The Hypervisor controls that
  175. * bit, per-strand.
  176. */
  177. if (tlb_type != hypervisor) {
  178. tick_disable_protection();
  179. tick_disable_irq();
  180. /* Let the user get at STICK too. */
  181. __asm__ __volatile__(
  182. " rd %%asr24, %%g2\n"
  183. " andn %%g2, %0, %%g2\n"
  184. " wr %%g2, 0, %%asr24"
  185. : /* no outputs */
  186. : "r" (TICK_PRIV_BIT)
  187. : "g1", "g2");
  188. }
  189. stick_disable_irq();
  190. }
  191. static unsigned long stick_get_tick(void)
  192. {
  193. unsigned long ret;
  194. __asm__ __volatile__("rd %%asr24, %0"
  195. : "=r" (ret));
  196. return ret & ~TICK_PRIV_BIT;
  197. }
  198. static unsigned long stick_add_tick(unsigned long adj)
  199. {
  200. unsigned long new_tick;
  201. __asm__ __volatile__("rd %%asr24, %0\n\t"
  202. "add %0, %1, %0\n\t"
  203. "wr %0, 0, %%asr24\n\t"
  204. : "=&r" (new_tick)
  205. : "r" (adj));
  206. return new_tick;
  207. }
  208. static int stick_add_compare(unsigned long adj)
  209. {
  210. unsigned long orig_tick, new_tick;
  211. __asm__ __volatile__("rd %%asr24, %0"
  212. : "=r" (orig_tick));
  213. orig_tick &= ~TICKCMP_IRQ_BIT;
  214. __asm__ __volatile__("wr %0, 0, %%asr25"
  215. : /* no outputs */
  216. : "r" (orig_tick + adj));
  217. __asm__ __volatile__("rd %%asr24, %0"
  218. : "=r" (new_tick));
  219. new_tick &= ~TICKCMP_IRQ_BIT;
  220. return ((long)(new_tick - (orig_tick+adj))) > 0L;
  221. }
  222. static struct sparc64_tick_ops stick_operations __read_mostly = {
  223. .name = "stick",
  224. .init_tick = stick_init_tick,
  225. .disable_irq = stick_disable_irq,
  226. .get_tick = stick_get_tick,
  227. .add_tick = stick_add_tick,
  228. .add_compare = stick_add_compare,
  229. .softint_mask = 1UL << 16,
  230. };
  231. /* On Hummingbird the STICK/STICK_CMPR register is implemented
  232. * in I/O space. There are two 64-bit registers each, the
  233. * first holds the low 32-bits of the value and the second holds
  234. * the high 32-bits.
  235. *
  236. * Since STICK is constantly updating, we have to access it carefully.
  237. *
  238. * The sequence we use to read is:
  239. * 1) read high
  240. * 2) read low
  241. * 3) read high again, if it rolled re-read both low and high again.
  242. *
  243. * Writing STICK safely is also tricky:
  244. * 1) write low to zero
  245. * 2) write high
  246. * 3) write low
  247. */
  248. #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
  249. #define HBIRD_STICK_ADDR 0x1fe0000f070UL
  250. static unsigned long __hbird_read_stick(void)
  251. {
  252. unsigned long ret, tmp1, tmp2, tmp3;
  253. unsigned long addr = HBIRD_STICK_ADDR+8;
  254. __asm__ __volatile__("ldxa [%1] %5, %2\n"
  255. "1:\n\t"
  256. "sub %1, 0x8, %1\n\t"
  257. "ldxa [%1] %5, %3\n\t"
  258. "add %1, 0x8, %1\n\t"
  259. "ldxa [%1] %5, %4\n\t"
  260. "cmp %4, %2\n\t"
  261. "bne,a,pn %%xcc, 1b\n\t"
  262. " mov %4, %2\n\t"
  263. "sllx %4, 32, %4\n\t"
  264. "or %3, %4, %0\n\t"
  265. : "=&r" (ret), "=&r" (addr),
  266. "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
  267. : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
  268. return ret;
  269. }
  270. static void __hbird_write_stick(unsigned long val)
  271. {
  272. unsigned long low = (val & 0xffffffffUL);
  273. unsigned long high = (val >> 32UL);
  274. unsigned long addr = HBIRD_STICK_ADDR;
  275. __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
  276. "add %0, 0x8, %0\n\t"
  277. "stxa %3, [%0] %4\n\t"
  278. "sub %0, 0x8, %0\n\t"
  279. "stxa %2, [%0] %4"
  280. : "=&r" (addr)
  281. : "0" (addr), "r" (low), "r" (high),
  282. "i" (ASI_PHYS_BYPASS_EC_E));
  283. }
  284. static void __hbird_write_compare(unsigned long val)
  285. {
  286. unsigned long low = (val & 0xffffffffUL);
  287. unsigned long high = (val >> 32UL);
  288. unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
  289. __asm__ __volatile__("stxa %3, [%0] %4\n\t"
  290. "sub %0, 0x8, %0\n\t"
  291. "stxa %2, [%0] %4"
  292. : "=&r" (addr)
  293. : "0" (addr), "r" (low), "r" (high),
  294. "i" (ASI_PHYS_BYPASS_EC_E));
  295. }
  296. static void hbtick_disable_irq(void)
  297. {
  298. __hbird_write_compare(TICKCMP_IRQ_BIT);
  299. }
  300. static void hbtick_init_tick(void)
  301. {
  302. tick_disable_protection();
  303. /* XXX This seems to be necessary to 'jumpstart' Hummingbird
  304. * XXX into actually sending STICK interrupts. I think because
  305. * XXX of how we store %tick_cmpr in head.S this somehow resets the
  306. * XXX {TICK + STICK} interrupt mux. -DaveM
  307. */
  308. __hbird_write_stick(__hbird_read_stick());
  309. hbtick_disable_irq();
  310. }
  311. static unsigned long hbtick_get_tick(void)
  312. {
  313. return __hbird_read_stick() & ~TICK_PRIV_BIT;
  314. }
  315. static unsigned long hbtick_add_tick(unsigned long adj)
  316. {
  317. unsigned long val;
  318. val = __hbird_read_stick() + adj;
  319. __hbird_write_stick(val);
  320. return val;
  321. }
  322. static int hbtick_add_compare(unsigned long adj)
  323. {
  324. unsigned long val = __hbird_read_stick();
  325. unsigned long val2;
  326. val &= ~TICKCMP_IRQ_BIT;
  327. val += adj;
  328. __hbird_write_compare(val);
  329. val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
  330. return ((long)(val2 - val)) > 0L;
  331. }
  332. static struct sparc64_tick_ops hbtick_operations __read_mostly = {
  333. .name = "hbtick",
  334. .init_tick = hbtick_init_tick,
  335. .disable_irq = hbtick_disable_irq,
  336. .get_tick = hbtick_get_tick,
  337. .add_tick = hbtick_add_tick,
  338. .add_compare = hbtick_add_compare,
  339. .softint_mask = 1UL << 0,
  340. };
  341. static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
  342. int update_persistent_clock(struct timespec now)
  343. {
  344. return set_rtc_mmss(now.tv_sec);
  345. }
  346. /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
  347. static void __init kick_start_clock(void)
  348. {
  349. void __iomem *regs = mstk48t02_regs;
  350. u8 sec, tmp;
  351. int i, count;
  352. prom_printf("CLOCK: Clock was stopped. Kick start ");
  353. spin_lock_irq(&mostek_lock);
  354. /* Turn on the kick start bit to start the oscillator. */
  355. tmp = mostek_read(regs + MOSTEK_CREG);
  356. tmp |= MSTK_CREG_WRITE;
  357. mostek_write(regs + MOSTEK_CREG, tmp);
  358. tmp = mostek_read(regs + MOSTEK_SEC);
  359. tmp &= ~MSTK_STOP;
  360. mostek_write(regs + MOSTEK_SEC, tmp);
  361. tmp = mostek_read(regs + MOSTEK_HOUR);
  362. tmp |= MSTK_KICK_START;
  363. mostek_write(regs + MOSTEK_HOUR, tmp);
  364. tmp = mostek_read(regs + MOSTEK_CREG);
  365. tmp &= ~MSTK_CREG_WRITE;
  366. mostek_write(regs + MOSTEK_CREG, tmp);
  367. spin_unlock_irq(&mostek_lock);
  368. /* Delay to allow the clock oscillator to start. */
  369. sec = MSTK_REG_SEC(regs);
  370. for (i = 0; i < 3; i++) {
  371. while (sec == MSTK_REG_SEC(regs))
  372. for (count = 0; count < 100000; count++)
  373. /* nothing */ ;
  374. prom_printf(".");
  375. sec = MSTK_REG_SEC(regs);
  376. }
  377. prom_printf("\n");
  378. spin_lock_irq(&mostek_lock);
  379. /* Turn off kick start and set a "valid" time and date. */
  380. tmp = mostek_read(regs + MOSTEK_CREG);
  381. tmp |= MSTK_CREG_WRITE;
  382. mostek_write(regs + MOSTEK_CREG, tmp);
  383. tmp = mostek_read(regs + MOSTEK_HOUR);
  384. tmp &= ~MSTK_KICK_START;
  385. mostek_write(regs + MOSTEK_HOUR, tmp);
  386. MSTK_SET_REG_SEC(regs,0);
  387. MSTK_SET_REG_MIN(regs,0);
  388. MSTK_SET_REG_HOUR(regs,0);
  389. MSTK_SET_REG_DOW(regs,5);
  390. MSTK_SET_REG_DOM(regs,1);
  391. MSTK_SET_REG_MONTH(regs,8);
  392. MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
  393. tmp = mostek_read(regs + MOSTEK_CREG);
  394. tmp &= ~MSTK_CREG_WRITE;
  395. mostek_write(regs + MOSTEK_CREG, tmp);
  396. spin_unlock_irq(&mostek_lock);
  397. /* Ensure the kick start bit is off. If it isn't, turn it off. */
  398. while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
  399. prom_printf("CLOCK: Kick start still on!\n");
  400. spin_lock_irq(&mostek_lock);
  401. tmp = mostek_read(regs + MOSTEK_CREG);
  402. tmp |= MSTK_CREG_WRITE;
  403. mostek_write(regs + MOSTEK_CREG, tmp);
  404. tmp = mostek_read(regs + MOSTEK_HOUR);
  405. tmp &= ~MSTK_KICK_START;
  406. mostek_write(regs + MOSTEK_HOUR, tmp);
  407. tmp = mostek_read(regs + MOSTEK_CREG);
  408. tmp &= ~MSTK_CREG_WRITE;
  409. mostek_write(regs + MOSTEK_CREG, tmp);
  410. spin_unlock_irq(&mostek_lock);
  411. }
  412. prom_printf("CLOCK: Kick start procedure successful.\n");
  413. }
  414. /* Return nonzero if the clock chip battery is low. */
  415. static int __init has_low_battery(void)
  416. {
  417. void __iomem *regs = mstk48t02_regs;
  418. u8 data1, data2;
  419. spin_lock_irq(&mostek_lock);
  420. data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
  421. mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
  422. data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
  423. mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
  424. spin_unlock_irq(&mostek_lock);
  425. return (data1 == data2); /* Was the write blocked? */
  426. }
  427. /* Probe for the real time clock chip. */
  428. static void __init set_system_time(void)
  429. {
  430. unsigned int year, mon, day, hour, min, sec;
  431. void __iomem *mregs = mstk48t02_regs;
  432. #ifdef CONFIG_PCI
  433. unsigned long dregs = ds1287_regs;
  434. void __iomem *bregs = bq4802_regs;
  435. #else
  436. unsigned long dregs = 0UL;
  437. void __iomem *bregs = 0UL;
  438. #endif
  439. u8 tmp;
  440. if (!mregs && !dregs && !bregs) {
  441. prom_printf("Something wrong, clock regs not mapped yet.\n");
  442. prom_halt();
  443. }
  444. if (mregs) {
  445. spin_lock_irq(&mostek_lock);
  446. /* Traditional Mostek chip. */
  447. tmp = mostek_read(mregs + MOSTEK_CREG);
  448. tmp |= MSTK_CREG_READ;
  449. mostek_write(mregs + MOSTEK_CREG, tmp);
  450. sec = MSTK_REG_SEC(mregs);
  451. min = MSTK_REG_MIN(mregs);
  452. hour = MSTK_REG_HOUR(mregs);
  453. day = MSTK_REG_DOM(mregs);
  454. mon = MSTK_REG_MONTH(mregs);
  455. year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
  456. } else if (bregs) {
  457. unsigned char val = readb(bregs + 0x0e);
  458. unsigned int century;
  459. /* BQ4802 RTC chip. */
  460. writeb(val | 0x08, bregs + 0x0e);
  461. sec = readb(bregs + 0x00);
  462. min = readb(bregs + 0x02);
  463. hour = readb(bregs + 0x04);
  464. day = readb(bregs + 0x06);
  465. mon = readb(bregs + 0x09);
  466. year = readb(bregs + 0x0a);
  467. century = readb(bregs + 0x0f);
  468. writeb(val, bregs + 0x0e);
  469. BCD_TO_BIN(sec);
  470. BCD_TO_BIN(min);
  471. BCD_TO_BIN(hour);
  472. BCD_TO_BIN(day);
  473. BCD_TO_BIN(mon);
  474. BCD_TO_BIN(year);
  475. BCD_TO_BIN(century);
  476. year += (century * 100);
  477. } else {
  478. /* Dallas 12887 RTC chip. */
  479. do {
  480. sec = CMOS_READ(RTC_SECONDS);
  481. min = CMOS_READ(RTC_MINUTES);
  482. hour = CMOS_READ(RTC_HOURS);
  483. day = CMOS_READ(RTC_DAY_OF_MONTH);
  484. mon = CMOS_READ(RTC_MONTH);
  485. year = CMOS_READ(RTC_YEAR);
  486. } while (sec != CMOS_READ(RTC_SECONDS));
  487. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  488. BCD_TO_BIN(sec);
  489. BCD_TO_BIN(min);
  490. BCD_TO_BIN(hour);
  491. BCD_TO_BIN(day);
  492. BCD_TO_BIN(mon);
  493. BCD_TO_BIN(year);
  494. }
  495. if ((year += 1900) < 1970)
  496. year += 100;
  497. }
  498. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  499. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  500. set_normalized_timespec(&wall_to_monotonic,
  501. -xtime.tv_sec, -xtime.tv_nsec);
  502. if (mregs) {
  503. tmp = mostek_read(mregs + MOSTEK_CREG);
  504. tmp &= ~MSTK_CREG_READ;
  505. mostek_write(mregs + MOSTEK_CREG, tmp);
  506. spin_unlock_irq(&mostek_lock);
  507. }
  508. }
  509. /* davem suggests we keep this within the 4M locked kernel image */
  510. static u32 starfire_get_time(void)
  511. {
  512. static char obp_gettod[32];
  513. static u32 unix_tod;
  514. sprintf(obp_gettod, "h# %08x unix-gettod",
  515. (unsigned int) (long) &unix_tod);
  516. prom_feval(obp_gettod);
  517. return unix_tod;
  518. }
  519. static int starfire_set_time(u32 val)
  520. {
  521. /* Do nothing, time is set using the service processor
  522. * console on this platform.
  523. */
  524. return 0;
  525. }
  526. static u32 hypervisor_get_time(void)
  527. {
  528. unsigned long ret, time;
  529. int retries = 10000;
  530. retry:
  531. ret = sun4v_tod_get(&time);
  532. if (ret == HV_EOK)
  533. return time;
  534. if (ret == HV_EWOULDBLOCK) {
  535. if (--retries > 0) {
  536. udelay(100);
  537. goto retry;
  538. }
  539. printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
  540. return 0;
  541. }
  542. printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
  543. return 0;
  544. }
  545. static int hypervisor_set_time(u32 secs)
  546. {
  547. unsigned long ret;
  548. int retries = 10000;
  549. retry:
  550. ret = sun4v_tod_set(secs);
  551. if (ret == HV_EOK)
  552. return 0;
  553. if (ret == HV_EWOULDBLOCK) {
  554. if (--retries > 0) {
  555. udelay(100);
  556. goto retry;
  557. }
  558. printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
  559. return -EAGAIN;
  560. }
  561. printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
  562. return -EOPNOTSUPP;
  563. }
  564. static int __init clock_model_matches(const char *model)
  565. {
  566. if (strcmp(model, "mk48t02") &&
  567. strcmp(model, "mk48t08") &&
  568. strcmp(model, "mk48t59") &&
  569. strcmp(model, "m5819") &&
  570. strcmp(model, "m5819p") &&
  571. strcmp(model, "m5823") &&
  572. strcmp(model, "ds1287") &&
  573. strcmp(model, "bq4802"))
  574. return 0;
  575. return 1;
  576. }
  577. static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
  578. {
  579. struct device_node *dp = op->node;
  580. const char *model = of_get_property(dp, "model", NULL);
  581. const char *compat = of_get_property(dp, "compatible", NULL);
  582. unsigned long size, flags;
  583. void __iomem *regs;
  584. if (!model)
  585. model = compat;
  586. if (!model || !clock_model_matches(model))
  587. return -ENODEV;
  588. /* On an Enterprise system there can be multiple mostek clocks.
  589. * We should only match the one that is on the central FHC bus.
  590. */
  591. if (!strcmp(dp->parent->name, "fhc") &&
  592. strcmp(dp->parent->parent->name, "central") != 0)
  593. return -ENODEV;
  594. size = (op->resource[0].end - op->resource[0].start) + 1;
  595. regs = of_ioremap(&op->resource[0], 0, size, "clock");
  596. if (!regs)
  597. return -ENOMEM;
  598. #ifdef CONFIG_PCI
  599. if (!strcmp(model, "ds1287") ||
  600. !strcmp(model, "m5819") ||
  601. !strcmp(model, "m5819p") ||
  602. !strcmp(model, "m5823")) {
  603. ds1287_regs = (unsigned long) regs;
  604. } else if (!strcmp(model, "bq4802")) {
  605. bq4802_regs = regs;
  606. } else
  607. #endif
  608. if (model[5] == '0' && model[6] == '2') {
  609. mstk48t02_regs = regs;
  610. } else if(model[5] == '0' && model[6] == '8') {
  611. mstk48t08_regs = regs;
  612. mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
  613. } else {
  614. mstk48t59_regs = regs;
  615. mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
  616. }
  617. printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs);
  618. local_irq_save(flags);
  619. if (mstk48t02_regs != NULL) {
  620. /* Report a low battery voltage condition. */
  621. if (has_low_battery())
  622. prom_printf("NVRAM: Low battery voltage!\n");
  623. /* Kick start the clock if it is completely stopped. */
  624. if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
  625. kick_start_clock();
  626. }
  627. set_system_time();
  628. local_irq_restore(flags);
  629. return 0;
  630. }
  631. static struct of_device_id clock_match[] = {
  632. {
  633. .name = "eeprom",
  634. },
  635. {
  636. .name = "rtc",
  637. },
  638. {},
  639. };
  640. static struct of_platform_driver clock_driver = {
  641. .name = "clock",
  642. .match_table = clock_match,
  643. .probe = clock_probe,
  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);