todc_time.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * arch/ppc/syslib/todc_time.c
  3. *
  4. * Time of Day Clock support for the M48T35, M48T37, M48T59, and MC146818
  5. * Real Time Clocks/Timekeepers.
  6. *
  7. * Author: Mark A. Greer
  8. * mgreer@mvista.com
  9. *
  10. * 2001-2004 (c) MontaVista, Software, Inc. This file is licensed under
  11. * the terms of the GNU General Public License version 2. This program
  12. * is licensed "as is" without any warranty of any kind, whether express
  13. * or implied.
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/time.h>
  19. #include <linux/timex.h>
  20. #include <linux/bcd.h>
  21. #include <linux/mc146818rtc.h>
  22. #include <asm/machdep.h>
  23. #include <asm/io.h>
  24. #include <asm/time.h>
  25. #include <asm/todc.h>
  26. /*
  27. * Depending on the hardware on your board and your board design, the
  28. * RTC/NVRAM may be accessed either directly (like normal memory) or via
  29. * address/data registers. If your board uses the direct method, set
  30. * 'nvram_data' to the base address of your nvram and leave 'nvram_as0' and
  31. * 'nvram_as1' NULL. If your board uses address/data regs to access nvram,
  32. * set 'nvram_as0' to the address of the lower byte, set 'nvram_as1' to the
  33. * address of the upper byte (leave NULL if using mc146818), and set
  34. * 'nvram_data' to the address of the 8-bit data register.
  35. *
  36. * In order to break the assumption that the RTC and NVRAM are accessed by
  37. * the same mechanism, you need to explicitly set 'ppc_md.rtc_read_val' and
  38. * 'ppc_md.rtc_write_val', otherwise the values of 'ppc_md.rtc_read_val'
  39. * and 'ppc_md.rtc_write_val' will be used.
  40. *
  41. * Note: Even though the documentation for the various RTC chips say that it
  42. * take up to a second before it starts updating once the 'R' bit is
  43. * cleared, they always seem to update even though we bang on it many
  44. * times a second. This is true, except for the Dallas Semi 1746/1747
  45. * (possibly others). Those chips seem to have a real problem whenever
  46. * we set the 'R' bit before reading them, they basically stop counting.
  47. * --MAG
  48. */
  49. /*
  50. * 'todc_info' should be initialized in your *_setup.c file to
  51. * point to a fully initialized 'todc_info_t' structure.
  52. * This structure holds all the register offsets for your particular
  53. * TODC/RTC chip.
  54. * TODC_ALLOC()/TODC_INIT() will allocate and initialize this table for you.
  55. */
  56. #ifdef RTC_FREQ_SELECT
  57. #undef RTC_FREQ_SELECT
  58. #define RTC_FREQ_SELECT control_b /* Register A */
  59. #endif
  60. #ifdef RTC_CONTROL
  61. #undef RTC_CONTROL
  62. #define RTC_CONTROL control_a /* Register B */
  63. #endif
  64. #ifdef RTC_INTR_FLAGS
  65. #undef RTC_INTR_FLAGS
  66. #define RTC_INTR_FLAGS watchdog /* Register C */
  67. #endif
  68. #ifdef RTC_VALID
  69. #undef RTC_VALID
  70. #define RTC_VALID interrupts /* Register D */
  71. #endif
  72. /* Access routines when RTC accessed directly (like normal memory) */
  73. u_char
  74. todc_direct_read_val(int addr)
  75. {
  76. return readb((void __iomem *)(todc_info->nvram_data + addr));
  77. }
  78. void
  79. todc_direct_write_val(int addr, unsigned char val)
  80. {
  81. writeb(val, (void __iomem *)(todc_info->nvram_data + addr));
  82. return;
  83. }
  84. /* Access routines for accessing m48txx type chips via addr/data regs */
  85. u_char
  86. todc_m48txx_read_val(int addr)
  87. {
  88. outb(addr, todc_info->nvram_as0);
  89. outb(addr>>todc_info->as0_bits, todc_info->nvram_as1);
  90. return inb(todc_info->nvram_data);
  91. }
  92. void
  93. todc_m48txx_write_val(int addr, unsigned char val)
  94. {
  95. outb(addr, todc_info->nvram_as0);
  96. outb(addr>>todc_info->as0_bits, todc_info->nvram_as1);
  97. outb(val, todc_info->nvram_data);
  98. return;
  99. }
  100. /* Access routines for accessing mc146818 type chips via addr/data regs */
  101. u_char
  102. todc_mc146818_read_val(int addr)
  103. {
  104. outb_p(addr, todc_info->nvram_as0);
  105. return inb_p(todc_info->nvram_data);
  106. }
  107. void
  108. todc_mc146818_write_val(int addr, unsigned char val)
  109. {
  110. outb_p(addr, todc_info->nvram_as0);
  111. outb_p(val, todc_info->nvram_data);
  112. }
  113. /*
  114. * Routines to make RTC chips with NVRAM buried behind an addr/data pair
  115. * have the NVRAM and clock regs appear at the same level.
  116. * The NVRAM will appear to start at addr 0 and the clock regs will appear
  117. * to start immediately after the NVRAM (actually, start at offset
  118. * todc_info->nvram_size).
  119. */
  120. static inline u_char
  121. todc_read_val(int addr)
  122. {
  123. u_char val;
  124. if (todc_info->sw_flags & TODC_FLAG_2_LEVEL_NVRAM) {
  125. if (addr < todc_info->nvram_size) { /* NVRAM */
  126. ppc_md.rtc_write_val(todc_info->nvram_addr_reg, addr);
  127. val = ppc_md.rtc_read_val(todc_info->nvram_data_reg);
  128. }
  129. else { /* Clock Reg */
  130. addr -= todc_info->nvram_size;
  131. val = ppc_md.rtc_read_val(addr);
  132. }
  133. }
  134. else {
  135. val = ppc_md.rtc_read_val(addr);
  136. }
  137. return val;
  138. }
  139. static inline void
  140. todc_write_val(int addr, u_char val)
  141. {
  142. if (todc_info->sw_flags & TODC_FLAG_2_LEVEL_NVRAM) {
  143. if (addr < todc_info->nvram_size) { /* NVRAM */
  144. ppc_md.rtc_write_val(todc_info->nvram_addr_reg, addr);
  145. ppc_md.rtc_write_val(todc_info->nvram_data_reg, val);
  146. }
  147. else { /* Clock Reg */
  148. addr -= todc_info->nvram_size;
  149. ppc_md.rtc_write_val(addr, val);
  150. }
  151. }
  152. else {
  153. ppc_md.rtc_write_val(addr, val);
  154. }
  155. }
  156. /*
  157. * TODC routines
  158. *
  159. * There is some ugly stuff in that there are assumptions for the mc146818.
  160. *
  161. * Assumptions:
  162. * - todc_info->control_a has the offset as mc146818 Register B reg
  163. * - todc_info->control_b has the offset as mc146818 Register A reg
  164. * - m48txx control reg's write enable or 'W' bit is same as
  165. * mc146818 Register B 'SET' bit (i.e., 0x80)
  166. *
  167. * These assumptions were made to make the code simpler.
  168. */
  169. long __init
  170. todc_time_init(void)
  171. {
  172. u_char cntl_b;
  173. if (!ppc_md.rtc_read_val)
  174. ppc_md.rtc_read_val = ppc_md.nvram_read_val;
  175. if (!ppc_md.rtc_write_val)
  176. ppc_md.rtc_write_val = ppc_md.nvram_write_val;
  177. cntl_b = todc_read_val(todc_info->control_b);
  178. if (todc_info->rtc_type == TODC_TYPE_MC146818) {
  179. if ((cntl_b & 0x70) != 0x20) {
  180. printk(KERN_INFO "TODC %s %s\n",
  181. "real-time-clock was stopped.",
  182. "Now starting...");
  183. cntl_b &= ~0x70;
  184. cntl_b |= 0x20;
  185. }
  186. todc_write_val(todc_info->control_b, cntl_b);
  187. } else if (todc_info->rtc_type == TODC_TYPE_DS17285) {
  188. u_char mode;
  189. mode = todc_read_val(TODC_TYPE_DS17285_CNTL_A);
  190. /* Make sure countdown clear is not set */
  191. mode &= ~0x40;
  192. /* Enable oscillator, extended register set */
  193. mode |= 0x30;
  194. todc_write_val(TODC_TYPE_DS17285_CNTL_A, mode);
  195. } else if (todc_info->rtc_type == TODC_TYPE_DS1501) {
  196. u_char month;
  197. todc_info->enable_read = TODC_DS1501_CNTL_B_TE;
  198. todc_info->enable_write = TODC_DS1501_CNTL_B_TE;
  199. month = todc_read_val(todc_info->month);
  200. if ((month & 0x80) == 0x80) {
  201. printk(KERN_INFO "TODC %s %s\n",
  202. "real-time-clock was stopped.",
  203. "Now starting...");
  204. month &= ~0x80;
  205. todc_write_val(todc_info->month, month);
  206. }
  207. cntl_b &= ~TODC_DS1501_CNTL_B_TE;
  208. todc_write_val(todc_info->control_b, cntl_b);
  209. } else { /* must be a m48txx type */
  210. u_char cntl_a;
  211. todc_info->enable_read = TODC_MK48TXX_CNTL_A_R;
  212. todc_info->enable_write = TODC_MK48TXX_CNTL_A_W;
  213. cntl_a = todc_read_val(todc_info->control_a);
  214. /* Check & clear STOP bit in control B register */
  215. if (cntl_b & TODC_MK48TXX_DAY_CB) {
  216. printk(KERN_INFO "TODC %s %s\n",
  217. "real-time-clock was stopped.",
  218. "Now starting...");
  219. cntl_a |= todc_info->enable_write;
  220. cntl_b &= ~TODC_MK48TXX_DAY_CB;/* Start Oscil */
  221. todc_write_val(todc_info->control_a, cntl_a);
  222. todc_write_val(todc_info->control_b, cntl_b);
  223. }
  224. /* Make sure READ & WRITE bits are cleared. */
  225. cntl_a &= ~(todc_info->enable_write |
  226. todc_info->enable_read);
  227. todc_write_val(todc_info->control_a, cntl_a);
  228. }
  229. return 0;
  230. }
  231. /*
  232. * There is some ugly stuff in that there are assumptions that for a mc146818,
  233. * the todc_info->control_a has the offset of the mc146818 Register B reg and
  234. * that the register'ss 'SET' bit is the same as the m48txx's write enable
  235. * bit in the control register of the m48txx (i.e., 0x80).
  236. *
  237. * It was done to make the code look simpler.
  238. */
  239. ulong
  240. todc_get_rtc_time(void)
  241. {
  242. uint year = 0, mon = 0, day = 0, hour = 0, min = 0, sec = 0;
  243. uint limit, i;
  244. u_char save_control, uip = 0;
  245. spin_lock(&rtc_lock);
  246. save_control = todc_read_val(todc_info->control_a);
  247. if (todc_info->rtc_type != TODC_TYPE_MC146818) {
  248. limit = 1;
  249. switch (todc_info->rtc_type) {
  250. case TODC_TYPE_DS1553:
  251. case TODC_TYPE_DS1557:
  252. case TODC_TYPE_DS1743:
  253. case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
  254. case TODC_TYPE_DS1747:
  255. case TODC_TYPE_DS17285:
  256. break;
  257. default:
  258. todc_write_val(todc_info->control_a,
  259. (save_control | todc_info->enable_read));
  260. }
  261. }
  262. else {
  263. limit = 100000000;
  264. }
  265. for (i=0; i<limit; i++) {
  266. if (todc_info->rtc_type == TODC_TYPE_MC146818) {
  267. uip = todc_read_val(todc_info->RTC_FREQ_SELECT);
  268. }
  269. sec = todc_read_val(todc_info->seconds) & 0x7f;
  270. min = todc_read_val(todc_info->minutes) & 0x7f;
  271. hour = todc_read_val(todc_info->hours) & 0x3f;
  272. day = todc_read_val(todc_info->day_of_month) & 0x3f;
  273. mon = todc_read_val(todc_info->month) & 0x1f;
  274. year = todc_read_val(todc_info->year) & 0xff;
  275. if (todc_info->rtc_type == TODC_TYPE_MC146818) {
  276. uip |= todc_read_val(todc_info->RTC_FREQ_SELECT);
  277. if ((uip & RTC_UIP) == 0) break;
  278. }
  279. }
  280. if (todc_info->rtc_type != TODC_TYPE_MC146818) {
  281. switch (todc_info->rtc_type) {
  282. case TODC_TYPE_DS1553:
  283. case TODC_TYPE_DS1557:
  284. case TODC_TYPE_DS1743:
  285. case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
  286. case TODC_TYPE_DS1747:
  287. case TODC_TYPE_DS17285:
  288. break;
  289. default:
  290. save_control &= ~(todc_info->enable_read);
  291. todc_write_val(todc_info->control_a,
  292. save_control);
  293. }
  294. }
  295. spin_unlock(&rtc_lock);
  296. if ((todc_info->rtc_type != TODC_TYPE_MC146818) ||
  297. ((save_control & RTC_DM_BINARY) == 0) ||
  298. RTC_ALWAYS_BCD) {
  299. BCD_TO_BIN(sec);
  300. BCD_TO_BIN(min);
  301. BCD_TO_BIN(hour);
  302. BCD_TO_BIN(day);
  303. BCD_TO_BIN(mon);
  304. BCD_TO_BIN(year);
  305. }
  306. year = year + 1900;
  307. if (year < 1970) {
  308. year += 100;
  309. }
  310. return mktime(year, mon, day, hour, min, sec);
  311. }
  312. int
  313. todc_set_rtc_time(unsigned long nowtime)
  314. {
  315. struct rtc_time tm;
  316. u_char save_control, save_freq_select = 0;
  317. spin_lock(&rtc_lock);
  318. to_tm(nowtime, &tm);
  319. save_control = todc_read_val(todc_info->control_a);
  320. /* Assuming MK48T59_RTC_CA_WRITE & RTC_SET are equal */
  321. todc_write_val(todc_info->control_a,
  322. (save_control | todc_info->enable_write));
  323. save_control &= ~(todc_info->enable_write); /* in case it was set */
  324. if (todc_info->rtc_type == TODC_TYPE_MC146818) {
  325. save_freq_select = todc_read_val(todc_info->RTC_FREQ_SELECT);
  326. todc_write_val(todc_info->RTC_FREQ_SELECT,
  327. save_freq_select | RTC_DIV_RESET2);
  328. }
  329. tm.tm_year = (tm.tm_year - 1900) % 100;
  330. if ((todc_info->rtc_type != TODC_TYPE_MC146818) ||
  331. ((save_control & RTC_DM_BINARY) == 0) ||
  332. RTC_ALWAYS_BCD) {
  333. BIN_TO_BCD(tm.tm_sec);
  334. BIN_TO_BCD(tm.tm_min);
  335. BIN_TO_BCD(tm.tm_hour);
  336. BIN_TO_BCD(tm.tm_mon);
  337. BIN_TO_BCD(tm.tm_mday);
  338. BIN_TO_BCD(tm.tm_year);
  339. }
  340. todc_write_val(todc_info->seconds, tm.tm_sec);
  341. todc_write_val(todc_info->minutes, tm.tm_min);
  342. todc_write_val(todc_info->hours, tm.tm_hour);
  343. todc_write_val(todc_info->month, tm.tm_mon);
  344. todc_write_val(todc_info->day_of_month, tm.tm_mday);
  345. todc_write_val(todc_info->year, tm.tm_year);
  346. todc_write_val(todc_info->control_a, save_control);
  347. if (todc_info->rtc_type == TODC_TYPE_MC146818) {
  348. todc_write_val(todc_info->RTC_FREQ_SELECT, save_freq_select);
  349. }
  350. spin_unlock(&rtc_lock);
  351. return 0;
  352. }
  353. /*
  354. * Manipulates read bit to reliably read seconds at a high rate.
  355. */
  356. static unsigned char __init todc_read_timereg(int addr)
  357. {
  358. unsigned char save_control = 0, val;
  359. switch (todc_info->rtc_type) {
  360. case TODC_TYPE_DS1553:
  361. case TODC_TYPE_DS1557:
  362. case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
  363. case TODC_TYPE_DS1747:
  364. case TODC_TYPE_DS17285:
  365. case TODC_TYPE_MC146818:
  366. break;
  367. default:
  368. save_control = todc_read_val(todc_info->control_a);
  369. todc_write_val(todc_info->control_a,
  370. (save_control | todc_info->enable_read));
  371. }
  372. val = todc_read_val(addr);
  373. switch (todc_info->rtc_type) {
  374. case TODC_TYPE_DS1553:
  375. case TODC_TYPE_DS1557:
  376. case TODC_TYPE_DS1746: /* XXXX BAD HACK -> FIX */
  377. case TODC_TYPE_DS1747:
  378. case TODC_TYPE_DS17285:
  379. case TODC_TYPE_MC146818:
  380. break;
  381. default:
  382. save_control &= ~(todc_info->enable_read);
  383. todc_write_val(todc_info->control_a, save_control);
  384. }
  385. return val;
  386. }
  387. /*
  388. * This was taken from prep_setup.c
  389. * Use the NVRAM RTC to time a second to calibrate the decrementer.
  390. */
  391. void __init
  392. todc_calibrate_decr(void)
  393. {
  394. ulong freq;
  395. ulong tbl, tbu;
  396. long i, loop_count;
  397. u_char sec;
  398. todc_time_init();
  399. /*
  400. * Actually this is bad for precision, we should have a loop in
  401. * which we only read the seconds counter. todc_read_val writes
  402. * the address bytes on every call and this takes a lot of time.
  403. * Perhaps an nvram_wait_change method returning a time
  404. * stamp with a loop count as parameter would be the solution.
  405. */
  406. /*
  407. * Need to make sure the tbl doesn't roll over so if tbu increments
  408. * during this test, we need to do it again.
  409. */
  410. loop_count = 0;
  411. sec = todc_read_timereg(todc_info->seconds) & 0x7f;
  412. do {
  413. tbu = get_tbu();
  414. for (i = 0 ; i < 10000000 ; i++) {/* may take up to 1 second */
  415. tbl = get_tbl();
  416. if ((todc_read_timereg(todc_info->seconds) & 0x7f) != sec) {
  417. break;
  418. }
  419. }
  420. sec = todc_read_timereg(todc_info->seconds) & 0x7f;
  421. for (i = 0 ; i < 10000000 ; i++) { /* Should take 1 second */
  422. freq = get_tbl();
  423. if ((todc_read_timereg(todc_info->seconds) & 0x7f) != sec) {
  424. break;
  425. }
  426. }
  427. freq -= tbl;
  428. } while ((get_tbu() != tbu) && (++loop_count < 2));
  429. printk("time_init: decrementer frequency = %lu.%.6lu MHz\n",
  430. freq/1000000, freq%1000000);
  431. tb_ticks_per_jiffy = freq / HZ;
  432. tb_to_us = mulhwu_scale_factor(freq, 1000000);
  433. return;
  434. }