1 /* $NetBSD: rtc.c,v 1.18 2014/11/20 16:34:25 christos Exp $ */ 2 /* 3 * Copyright (c) 1998 Darrin Jewell 4 * Copyright (c) 1997 Rolf Grossmann 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Rolf Grossmann. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* These haven't been tested to see how they interact with NeXTstep's 34 * interpretation of the rtc. 35 */ 36 37 /* Now using this in the kernel. This should be turned into a device 38 * Darrin B Jewell <jewell@mit.edu> Tue Jan 27 20:59:25 1998 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.18 2014/11/20 16:34:25 christos Exp $"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> /* for panic */ 46 47 #include <machine/bus.h> 48 #include <machine/cpu.h> 49 50 #include <dev/clock_subr.h> 51 52 #include <next68k/next68k/rtc.h> 53 54 #include <next68k/dev/clockreg.h> 55 #include <next68k/dev/intiovar.h> 56 57 /* #define RTC_DEBUG */ 58 59 u_char new_clock; 60 volatile u_int *scr2 = (u_int *)NEXT_P_SCR2; /* will get memory mapped in rtc_init */ 61 62 static int gettime_old(todr_chip_handle_t, struct clock_ymdhms *); 63 static int settime_old(todr_chip_handle_t, struct clock_ymdhms *); 64 static int gettime_new(todr_chip_handle_t, struct timeval *); 65 static int settime_new(todr_chip_handle_t, struct timeval *); 66 67 /* 68 * NB: This code should probably be converted to a _true_ device, then this 69 * initialization could happen in attach. The printf could get fixed then, 70 * too. 71 */ 72 void 73 rtc_init(void) 74 { 75 static struct todr_chip_handle tch; 76 u_char val; 77 78 scr2 = (u_int *)IIOV(NEXT_P_SCR2); 79 val = rtc_read(RTC_STATUS); 80 new_clock = (val & RTC_NEW_CLOCK) ? 1 : 0; 81 82 printf("Looks like a %s clock chip.\n", 83 (new_clock? 84 "MCS1850 (new style)": 85 "MC68HC68T1 (old style)")); 86 87 #ifdef RTC_DEBUG 88 rtc_print(); 89 #endif 90 91 if (new_clock) { 92 tch.todr_gettime = gettime_new; 93 tch.todr_settime = settime_new; 94 tch.todr_gettime_ymdhms = NULL; 95 tch.todr_settime_ymdhms = NULL; 96 } else { 97 tch.todr_gettime_ymdhms = gettime_old; 98 tch.todr_settime_ymdhms = settime_old; 99 tch.todr_gettime = NULL; 100 tch.todr_settime = NULL; 101 } 102 tch.todr_setwen = NULL; 103 104 todr_attach(&tch); 105 } 106 107 void 108 rtc_print(void) 109 { 110 111 #define RTC_PRINT(x) printf("\t%16s= 0x%02x\n",#x, rtc_read(x)) 112 113 if (new_clock) { 114 RTC_PRINT(RTC_RAM); 115 RTC_PRINT(RTC_CNTR0); 116 RTC_PRINT(RTC_CNTR1); 117 RTC_PRINT(RTC_CNTR2); 118 RTC_PRINT(RTC_CNTR3); 119 RTC_PRINT(RTC_ALARM0); 120 RTC_PRINT(RTC_ALARM1); 121 RTC_PRINT(RTC_ALARM2); 122 RTC_PRINT(RTC_ALARM3); 123 RTC_PRINT(RTC_STATUS); 124 RTC_PRINT(RTC_CONTROL); 125 } else { 126 RTC_PRINT(RTC_RAM); 127 RTC_PRINT(RTC_SEC); 128 RTC_PRINT(RTC_MIN); 129 RTC_PRINT(RTC_HRS); 130 RTC_PRINT(RTC_DAY); 131 RTC_PRINT(RTC_DATE); 132 RTC_PRINT(RTC_MON); 133 RTC_PRINT(RTC_YR); 134 RTC_PRINT(RTC_ALARM_SEC); 135 RTC_PRINT(RTC_ALARM_MIN); 136 RTC_PRINT(RTC_ALARM_HR); 137 RTC_PRINT(RTC_STATUS); 138 RTC_PRINT(RTC_CONTROL); 139 RTC_PRINT(RTC_INTRCTL); 140 } 141 } 142 143 144 u_char 145 rtc_read(u_char reg) 146 { 147 int i; 148 u_int tmp; 149 u_char val; 150 151 *scr2 = (*scr2 & ~(SCR2_RTDATA | SCR2_RTCLK)) | SCR2_RTCE; 152 DELAY(1); 153 154 val = reg; 155 for (i=0; i<8; i++) { 156 tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK); 157 if (val & 0x80) 158 tmp |= SCR2_RTDATA; 159 160 *scr2 = tmp; 161 DELAY(1); 162 *scr2 = tmp | SCR2_RTCLK; 163 DELAY(1); 164 *scr2 = tmp; 165 DELAY(1); 166 167 val <<= 1; 168 } 169 170 val = 0; /* should be anyway */ 171 for (i=0; i<8; i++) { 172 val <<= 1; 173 174 tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK); 175 176 *scr2 = tmp | SCR2_RTCLK; 177 DELAY(1); 178 *scr2 = tmp; 179 DELAY(1); 180 181 if (*scr2 & SCR2_RTDATA) 182 val |= 1; 183 } 184 185 *scr2 &= ~(SCR2_RTDATA|SCR2_RTCLK|SCR2_RTCE); 186 DELAY(1); 187 188 return val; 189 } 190 191 void 192 rtc_write(u_char reg, u_char v) 193 { 194 int i; 195 u_int tmp; 196 u_char val; 197 198 *scr2 = (*scr2 & ~(SCR2_RTDATA | SCR2_RTCLK)) | SCR2_RTCE; 199 DELAY(1); 200 201 val = reg|RTC_WRITE; 202 203 for (i=0; i<8; i++) { 204 tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK); 205 if (val & 0x80) 206 tmp |= SCR2_RTDATA; 207 208 *scr2 = tmp; 209 DELAY(1); 210 *scr2 = tmp | SCR2_RTCLK; 211 DELAY(1); 212 *scr2 = tmp; 213 DELAY(1); 214 215 val <<= 1; 216 } 217 218 DELAY(1); 219 220 for (i=0; i<8; i++) { 221 tmp = *scr2 & ~(SCR2_RTDATA | SCR2_RTCLK); 222 if (v & 0x80) 223 tmp |= SCR2_RTDATA; 224 225 *scr2 = tmp; 226 DELAY(1); 227 *scr2 = tmp | SCR2_RTCLK; 228 DELAY(1); 229 *scr2 = tmp; 230 DELAY(1); 231 232 v <<= 1; 233 } 234 235 *scr2 &= ~(SCR2_RTDATA|SCR2_RTCLK|SCR2_RTCE); 236 DELAY(1); 237 } 238 239 void 240 poweroff(void) 241 { 242 int reg, t; 243 244 if(new_clock) { 245 reg = RTC_CNTR3; 246 } else { 247 reg = RTC_CNTR0; 248 } 249 250 t = rtc_read(reg); /* seconds */ 251 /* wait for clock to tick */ 252 while(t == rtc_read(reg)); 253 254 DELAY(850000); /* hardware bug workaround ? */ 255 256 if(new_clock) { 257 reg = RTC_CONTROL; 258 } else { 259 reg = RTC_INTRCTL; 260 } 261 262 rtc_write(reg, rtc_read(reg)|(RTC_PDOWN)); 263 264 printf("....................."); /* @@@ work around some sort of bug. */ 265 266 panic("Failed to poweroff!"); 267 } 268 269 270 int 271 gettime_old(todr_chip_handle_t tch, struct clock_ymdhms *dt) 272 { 273 u_char h, y; 274 275 y = bcdtobin(rtc_read(RTC_YR)); 276 if (y >= 69) { 277 dt->dt_year = 1900+y; 278 } else { 279 dt->dt_year = 2000+y; 280 } 281 282 dt->dt_mon = bcdtobin(rtc_read(RTC_MON)&0x1f); 283 dt->dt_day = bcdtobin(rtc_read(RTC_DATE)&0x3f); 284 dt->dt_wday = bcdtobin(rtc_read(RTC_DAY)&0x7); 285 286 h = rtc_read(RTC_HRS); 287 if (h & 0x80) { /* time is am/pm format */ 288 dt->dt_hour = bcdtobin(h&0x1f); 289 if (h & 0x20) { /* pm */ 290 if (dt->dt_hour < 12) dt->dt_hour += 12; 291 } else { /* am */ 292 if (dt->dt_hour == 12) dt->dt_hour = 0; 293 } 294 #ifdef notdef 295 } else { /* time is 24 hour format */ 296 struct clock_ymdhms val; 297 val.dt_hour = bcdtobin(h & 0x3f); 298 #endif 299 } 300 301 dt->dt_min = bcdtobin(rtc_read(RTC_MIN)&0x7f); 302 dt->dt_sec = bcdtobin(rtc_read(RTC_SEC)&0x7f); 303 304 return 0; 305 } 306 307 int 308 settime_old(todr_chip_handle_t tcr, struct clock_ymdhms *dt) 309 { 310 u_char h; 311 312 /* Stop the clock */ 313 rtc_write(RTC_CONTROL,rtc_read(RTC_CONTROL) & ~RTC_START); 314 315 #ifdef RTC_DEBUG 316 printf("Setting RTC to 0x%08x. Regs before:\n",secs); 317 rtc_print(); 318 #endif 319 320 rtc_write(RTC_SEC,bintobcd(dt->dt_sec)); 321 rtc_write(RTC_MIN,bintobcd(dt->dt_min)); 322 h = rtc_read(RTC_HRS); 323 if (h & 0x80) { /* time is am/pm format */ 324 if (dt->dt_hour == 0) { 325 rtc_write(RTC_HRS,bintobcd(12)|0x80); 326 } else if (dt->dt_hour < 12) { /* am */ 327 rtc_write(RTC_HRS,bintobcd(dt->dt_hour)|0x80); 328 } else if (dt->dt_hour == 12) { 329 rtc_write(RTC_HRS,bintobcd(12)|0x80|0x20); 330 } else /* pm */ 331 rtc_write(RTC_HRS,bintobcd(dt->dt_hour-12)|0x80|0x20); 332 } else { /* time is 24 hour format */ 333 rtc_write(RTC_HRS,bintobcd(dt->dt_hour)); 334 } 335 rtc_write(RTC_DAY,bintobcd(dt->dt_wday)); 336 rtc_write(RTC_DATE,bintobcd(dt->dt_day)); 337 rtc_write(RTC_MON,bintobcd(dt->dt_mon)); 338 rtc_write(RTC_YR,bintobcd(dt->dt_year%100)); 339 340 #ifdef RTC_DEBUG 341 printf("Regs after:\n",secs); 342 rtc_print(); 343 #endif 344 345 /* restart the clock */ 346 rtc_write(RTC_CONTROL,rtc_read(RTC_CONTROL) | RTC_START); 347 return 0; 348 } 349 350 int 351 gettime_new(todr_chip_handle_t tch, struct timeval *tvp) 352 { 353 tvp->tv_sec = rtc_read(RTC_CNTR0) << 24 | 354 rtc_read(RTC_CNTR1) << 16 | 355 rtc_read(RTC_CNTR2) << 8 | 356 rtc_read(RTC_CNTR3); 357 return 0; 358 } 359 360 int 361 settime_new(todr_chip_handle_t tch, struct timeval *tvp) 362 { 363 364 /* Stop the clock */ 365 rtc_write(RTC_CONTROL,rtc_read(RTC_CONTROL) & ~RTC_START); 366 367 #ifdef RTC_DEBUG 368 printf("Setting RTC to 0x%08x. Regs before:\n",tvp->tv_sec); 369 rtc_print(); 370 #endif 371 372 rtc_write(RTC_CNTR0, (tvp->tv_sec >> 24) & 0xff); 373 rtc_write(RTC_CNTR1, (tvp->tv_sec >> 16) & 0xff); 374 rtc_write(RTC_CNTR2, (tvp->tv_sec >> 8) & 0xff); 375 rtc_write(RTC_CNTR3, (tvp->tv_sec) & 0xff); 376 377 #ifdef RTC_DEBUG 378 printf("Regs after:\n",secs); 379 rtc_print(); 380 #endif 381 382 /* restart the clock */ 383 rtc_write(RTC_CONTROL,rtc_read(RTC_CONTROL) | RTC_START); 384 385 return 0; 386 } 387