time.c 41 KB

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