1 /* $OpenBSD: clock.c,v 1.8 2002/01/25 03:36:25 jason Exp $ */ 2 /* $NetBSD: clock.c,v 1.41 2001/07/24 19:29:25 eeh Exp $ */ 3 4 /* 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * Copyright (c) 1994 Gordon W. Ross 8 * Copyright (c) 1993 Adam Glass 9 * Copyright (c) 1996 Paul Kranenburg 10 * Copyright (c) 1996 11 * The President and Fellows of Harvard College. All rights reserved. 12 * 13 * This software was developed by the Computer Systems Engineering group 14 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 15 * contributed to Berkeley. 16 * 17 * All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Harvard University. 20 * This product includes software developed by the University of 21 * California, Lawrence Berkeley Laboratory. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 27 * 1. Redistributions of source code must retain the above copyright 28 * notice, this list of conditions and the following disclaimer. 29 * 2. Redistributions in binary form must reproduce the above copyright 30 * notice, this list of conditions and the following disclaimer in the 31 * documentation and/or other materials provided with the distribution. 32 * 3. All advertising materials mentioning features or use of this software 33 * must display the following acknowledgement: 34 * This product includes software developed by the University of 35 * California, Berkeley and its contributors. 36 * This product includes software developed by Paul Kranenburg. 37 * This product includes software developed by Harvard University. 38 * 4. Neither the name of the University nor the names of its contributors 39 * may be used to endorse or promote products derived from this software 40 * without specific prior written permission. 41 * 42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * 54 * @(#)clock.c 8.1 (Berkeley) 6/11/93 55 * 56 */ 57 58 /* 59 * Clock driver. This is the id prom and eeprom driver as well 60 * and includes the timer register functions too. 61 */ 62 63 /* Define this for a 1/4s clock to ease debugging */ 64 /* #define INTR_DEBUG */ 65 66 #include <sys/param.h> 67 #include <sys/kernel.h> 68 #include <sys/device.h> 69 #include <sys/proc.h> 70 #include <sys/resourcevar.h> 71 #include <sys/malloc.h> 72 #include <sys/systm.h> 73 #ifdef GPROF 74 #include <sys/gmon.h> 75 #endif 76 #include <sys/sched.h> 77 78 #include <uvm/uvm_extern.h> 79 80 #include <machine/bus.h> 81 #include <machine/autoconf.h> 82 #include <machine/eeprom.h> 83 #include <machine/cpu.h> 84 #include <machine/idprom.h> 85 86 #include <dev/clock_subr.h> 87 #include <dev/ic/mk48txxreg.h> 88 #include <dev/ic/mc146818reg.h> 89 90 #include <sparc64/sparc64/intreg.h> 91 #include <sparc64/sparc64/timerreg.h> 92 #include <sparc64/dev/iommureg.h> 93 #include <sparc64/dev/sbusreg.h> 94 #include <dev/sbus/sbusvar.h> 95 #include <sparc64/dev/ebusreg.h> 96 #include <sparc64/dev/ebusvar.h> 97 98 extern u_int64_t cpu_clockrate; 99 100 struct rtc_info { 101 bus_space_tag_t rtc_bt; /* bus tag & handle */ 102 bus_space_handle_t rtc_bh; /* */ 103 }; 104 105 struct cfdriver clock_cd = { 106 NULL, "clock", DV_DULL 107 }; 108 109 /* 110 * Statistics clock interval and variance, in usec. Variance must be a 111 * power of two. Since this gives us an even number, not an odd number, 112 * we discard one case and compensate. That is, a variance of 1024 would 113 * give us offsets in [0..1023]. Instead, we take offsets in [1..1023]. 114 * This is symmetric about the point 512, or statvar/2, and thus averages 115 * to that value (assuming uniform random numbers). 116 */ 117 /* XXX fix comment to match value */ 118 int statvar = 8192; 119 int statmin; /* statclock interval - 1/2*variance */ 120 int timerok; 121 122 static long tick_increment; 123 int schedintr __P((void *)); 124 125 static struct intrhand level10 = { clockintr }; 126 static struct intrhand level0 = { tickintr }; 127 static struct intrhand level14 = { statintr }; 128 static struct intrhand schedint = { schedintr }; 129 130 /* 131 * clock (eeprom) attaches at the sbus or the ebus (PCI) 132 */ 133 static int clockmatch_sbus __P((struct device *, void *, void *)); 134 static void clockattach_sbus __P((struct device *, struct device *, void *)); 135 static int clockmatch_ebus __P((struct device *, void *, void *)); 136 static void clockattach_ebus __P((struct device *, struct device *, void *)); 137 static int clockmatch_rtc __P((struct device *, void *, void *)); 138 static void clockattach_rtc __P((struct device *, struct device *, void *)); 139 static void clockattach __P((int, bus_space_tag_t, bus_space_handle_t)); 140 141 struct cfattach clock_sbus_ca = { 142 sizeof(struct device), clockmatch_sbus, clockattach_sbus 143 }; 144 145 struct cfattach clock_ebus_ca = { 146 sizeof(struct device), clockmatch_ebus, clockattach_ebus 147 }; 148 149 struct cfattach rtc_ebus_ca = { 150 sizeof(struct device), clockmatch_rtc, clockattach_rtc 151 }; 152 153 struct cfdriver rtc_cd = { 154 NULL, "rtc", DV_DULL 155 }; 156 157 /* Global TOD clock handle & idprom pointer */ 158 static todr_chip_handle_t todr_handle = NULL; 159 static struct idprom *idprom; 160 161 static int timermatch __P((struct device *, void *, void *)); 162 static void timerattach __P((struct device *, struct device *, void *)); 163 164 struct timerreg_4u timerreg_4u; /* XXX - need more cleanup */ 165 166 struct cfattach timer_ca = { 167 sizeof(struct device), timermatch, timerattach 168 }; 169 170 struct cfdriver timer_cd = { 171 NULL, "timer", DV_DULL 172 }; 173 174 int sbus_wenable __P((struct todr_chip_handle *, int)); 175 int ebus_wenable __P((struct todr_chip_handle *, int)); 176 struct chiptime; 177 void myetheraddr __P((u_char *)); 178 int chiptotime __P((int, int, int, int, int, int)); 179 void timetochip __P((struct chiptime *)); 180 void stopcounter __P((struct timer_4u *)); 181 182 int timerblurb = 10; /* Guess a value; used before clock is attached */ 183 184 u_int8_t rtc_read_reg(bus_space_tag_t, bus_space_handle_t, int); 185 void rtc_write_reg(bus_space_tag_t, bus_space_handle_t, int, u_int8_t); 186 int rtc_gettime(todr_chip_handle_t, struct timeval *); 187 int rtc_settime(todr_chip_handle_t, struct timeval *); 188 int rtc_getcal(todr_chip_handle_t, int *); 189 int rtc_setcal(todr_chip_handle_t, int); 190 191 int rtc_auto_century_adjust = 1; 192 193 /* 194 * The OPENPROM calls the clock the "eeprom", so we have to have our 195 * own special match function to call it the "clock". 196 */ 197 static int 198 clockmatch_sbus(parent, cf, aux) 199 struct device *parent; 200 void *cf; 201 void *aux; 202 { 203 struct sbus_attach_args *sa = aux; 204 205 return (strcmp("eeprom", sa->sa_name) == 0); 206 } 207 208 static int 209 clockmatch_ebus(parent, cf, aux) 210 struct device *parent; 211 void *cf; 212 void *aux; 213 { 214 struct ebus_attach_args *ea = aux; 215 216 return (strcmp("eeprom", ea->ea_name) == 0); 217 } 218 219 static int 220 clockmatch_rtc(parent, cf, aux) 221 struct device *parent; 222 void *cf; 223 void *aux; 224 { 225 struct ebus_attach_args *ea = aux; 226 227 return (strcmp("rtc", ea->ea_name) == 0); 228 } 229 230 /* 231 * Attach a clock (really `eeprom') to the sbus or ebus. 232 * 233 * We ignore any existing virtual address as we need to map 234 * this read-only and make it read-write only temporarily, 235 * whenever we read or write the clock chip. The clock also 236 * contains the ID ``PROM'', and I have already had the pleasure 237 * of reloading the cpu type, Ethernet address, etc, by hand from 238 * the console FORTH interpreter. I intend not to enjoy it again. 239 * 240 * the MK48T02 is 2K. the MK48T08 is 8K, and the MK48T59 is 241 * supposed to be identical to it. 242 * 243 * This is *UGLY*! We probably have multiple mappings. But I do 244 * know that this all fits inside an 8K page, so I'll just map in 245 * once. 246 * 247 * What we really need is some way to record the bus attach args 248 * so we can call *_bus_map() later with BUS_SPACE_MAP_READONLY 249 * or not to write enable/disable the device registers. This is 250 * a non-trivial operation. 251 */ 252 253 /* Somewhere to keep info that sbus_wenable() needs */ 254 struct sbus_info { 255 bus_space_tag_t si_bt; 256 bus_space_handle_t si_bh; 257 struct sbus_reg si_reg; 258 }; 259 260 /* ARGSUSED */ 261 static void 262 clockattach_sbus(parent, self, aux) 263 struct device *parent, *self; 264 void *aux; 265 { 266 struct sbus_attach_args *sa = aux; 267 bus_space_tag_t bt = sa->sa_bustag; 268 int sz; 269 static struct sbus_info sbi; 270 271 /* use sa->sa_regs[0].size? */ 272 sz = 8192; 273 274 if (sbus_bus_map(bt, 275 sa->sa_slot, 276 (sa->sa_offset & ~NBPG), 277 sz, 278 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_READONLY, 279 0, 280 &sbi.si_bh) != 0) { 281 printf("%s: can't map register\n", self->dv_xname); 282 return; 283 } 284 clockattach(sa->sa_node, bt, sbi.si_bh); 285 286 /* Save info for the clock wenable call. */ 287 sbi.si_bt = bt; 288 sbi.si_reg = sa->sa_reg[0]; 289 todr_handle->bus_cookie = &sbi; 290 todr_handle->todr_setwen = sbus_wenable; 291 } 292 293 /* 294 * Write en/dis-able clock registers. We coordinate so that several 295 * writers can run simultaneously. 296 */ 297 int 298 sbus_wenable(handle, onoff) 299 struct todr_chip_handle *handle; 300 int onoff; 301 { 302 register int s, err = 0; 303 register int prot;/* nonzero => change prot */ 304 static int writers; 305 306 s = splhigh(); 307 if (onoff) 308 prot = writers++ == 0 ? BUS_SPACE_MAP_LINEAR : 0; 309 else 310 prot = --writers == 0 ? 311 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_READONLY : 0; 312 splx(s); 313 if (prot) { 314 struct sbus_info *sbi = (struct sbus_info *)handle->bus_cookie; 315 bus_space_handle_t newaddr; 316 317 err = sbus_bus_map(sbi->si_bt, sbi->si_reg.sbr_slot, 318 (sbi->si_reg.sbr_offset & ~NBPG), 319 8192, prot, (vaddr_t)sbi->si_bh, &newaddr); 320 /* We can panic now or take a datafault later... */ 321 if (sbi->si_bh != newaddr) 322 panic("sbus_wenable: address %p changed to %p\n", 323 (void *)(u_long)sbi->si_bh, 324 (void *)(u_long)newaddr); 325 } 326 return (err); 327 } 328 329 330 struct ebus_info { 331 bus_space_tag_t ei_bt; 332 bus_space_handle_t ei_bh; 333 struct ebus_regs ei_reg; 334 }; 335 336 /* ARGSUSED */ 337 static void 338 clockattach_ebus(parent, self, aux) 339 struct device *parent, *self; 340 void *aux; 341 { 342 struct ebus_attach_args *ea = aux; 343 bus_space_tag_t bt = ea->ea_bustag; 344 int sz; 345 static struct ebus_info ebi; 346 347 /* hard code to 8K? */ 348 sz = ea->ea_regs[0].size; 349 350 if (ebus_bus_map(bt, 351 0, 352 EBUS_PADDR_FROM_REG(&ea->ea_regs[0]), 353 sz, 354 BUS_SPACE_MAP_LINEAR, 355 0, 356 &ebi.ei_bh) != 0) { 357 printf("%s: can't map register\n", self->dv_xname); 358 return; 359 } 360 clockattach(ea->ea_node, bt, ebi.ei_bh); 361 362 /* Save info for the clock wenable call. */ 363 ebi.ei_bt = bt; 364 ebi.ei_reg = ea->ea_regs[0]; 365 todr_handle->bus_cookie = &ebi; 366 todr_handle->todr_setwen = ebus_wenable; 367 } 368 369 /* 370 * Write en/dis-able clock registers. We coordinate so that several 371 * writers can run simultaneously. 372 */ 373 int 374 ebus_wenable(handle, onoff) 375 struct todr_chip_handle *handle; 376 int onoff; 377 { 378 register int s, err = 0; 379 register int prot;/* nonzero => change prot */ 380 static int writers; 381 382 s = splhigh(); 383 if (onoff) 384 prot = writers++ == 0 ? BUS_SPACE_MAP_LINEAR : 0; 385 else 386 prot = --writers == 0 ? 387 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_READONLY : 0; 388 splx(s); 389 if (prot) { 390 struct ebus_info *ebi = (struct ebus_info *)handle->bus_cookie; 391 bus_space_handle_t newaddr; 392 393 err = sbus_bus_map(ebi->ei_bt, 0, 394 EBUS_PADDR_FROM_REG(&ebi->ei_reg), 8192, prot, 395 (vaddr_t)ebi->ei_bh, &newaddr); 396 /* We can panic now or take a datafault later... */ 397 if (ebi->ei_bh != newaddr) 398 panic("ebus_wenable: address %p changed to %p\n", 399 (void *)(u_long)ebi->ei_bh, 400 (void *)(u_long)newaddr); 401 } 402 return (err); 403 } 404 405 406 static void 407 clockattach(node, bt, bh) 408 int node; 409 bus_space_tag_t bt; 410 bus_space_handle_t bh; 411 { 412 char *model; 413 struct idprom *idp; 414 int h; 415 416 model = getpropstring(node, "model"); 417 418 #ifdef DIAGNOSTIC 419 if (model == NULL) 420 panic("clockattach: no model property"); 421 #endif 422 423 /* Our TOD clock year 0 is 1968 */ 424 if ((todr_handle = mk48txx_attach(bt, bh, model, 1968)) == NULL) 425 panic("Can't attach %s tod clock", model); 426 427 #define IDPROM_OFFSET (8*1024 - 40) /* XXX - get nvram sz from driver */ 428 idp = (struct idprom *)((u_long)bh + IDPROM_OFFSET); 429 430 h = idp->id_machine << 24; 431 h |= idp->id_hostid[0] << 16; 432 h |= idp->id_hostid[1] << 8; 433 h |= idp->id_hostid[2]; 434 hostid = h; 435 printf(": hostid %x\n", (u_int)hostid); 436 437 idprom = idp; 438 } 439 440 /* 441 * `rtc' is a ds1287 on an ebus (actually an isa bus, but we use the 442 * ebus driver for isa.) So we can use ebus_wenable() but need to do 443 * different attach work and use different todr routines. It does not 444 * incorporate an IDPROM. 445 */ 446 447 /* 448 * XXX the stupid ds1287 is not mapped directly but uses an address 449 * and a data reg so we cannot access the stuuupid thing w/o having 450 * write access to the registers. 451 * 452 * XXXX We really need to mutex register access! 453 */ 454 #define RTC_ADDR 0 455 #define RTC_DATA 1 456 u_int8_t 457 rtc_read_reg(bus_space_tag_t bt, bus_space_handle_t bh, int reg) 458 { 459 bus_space_write_1(bt, bh, RTC_ADDR, reg); 460 return (bus_space_read_1(bt, bh, RTC_DATA)); 461 } 462 void 463 rtc_write_reg(bus_space_tag_t bt, bus_space_handle_t bh, int reg, u_int8_t val) 464 { 465 bus_space_write_1(bt, bh, RTC_ADDR, reg); 466 bus_space_write_1(bt, bh, RTC_DATA, val); 467 } 468 469 /* ARGSUSED */ 470 static void 471 clockattach_rtc(parent, self, aux) 472 struct device *parent, *self; 473 void *aux; 474 { 475 struct ebus_attach_args *ea = aux; 476 bus_space_tag_t bt = ea->ea_bustag; 477 todr_chip_handle_t handle; 478 struct rtc_info *rtc; 479 char *model; 480 int sz; 481 static struct ebus_info ebi; 482 483 /* hard code to 8K? */ 484 sz = ea->ea_regs[0].size; 485 486 if (ebus_bus_map(bt, 487 0, 488 EBUS_PADDR_FROM_REG(&ea->ea_regs[0]), 489 sz, 490 BUS_SPACE_MAP_LINEAR, 491 0, 492 &ebi.ei_bh) != 0) { 493 printf("%s: can't map register\n", self->dv_xname); 494 return; 495 } 496 497 model = getpropstring(ea->ea_node, "model"); 498 #ifdef DIAGNOSTIC 499 if (model == NULL) 500 panic("clockattach_rtc: no model property"); 501 #endif 502 printf(": %s\n", model); 503 504 /* 505 * Turn interrupts off, just in case. (Although they shouldn't 506 * be wired to an interrupt controller on sparcs). 507 */ 508 rtc_write_reg(bt, ebi.ei_bh, 509 MC_REGB, MC_REGB_BINARY | MC_REGB_24HR); 510 511 /* Setup our todr_handle */ 512 sz = ALIGN(sizeof(struct todr_chip_handle)) + sizeof(struct rtc_info); 513 handle = malloc(sz, M_DEVBUF, M_NOWAIT); 514 if (handle == NULL) 515 panic("clockattach_rtc"); 516 rtc = (struct rtc_info*)((u_long)handle + 517 ALIGN(sizeof(struct todr_chip_handle))); 518 handle->cookie = rtc; 519 handle->todr_gettime = rtc_gettime; 520 handle->todr_settime = rtc_settime; 521 handle->todr_getcal = rtc_getcal; 522 handle->todr_setcal = rtc_setcal; 523 handle->todr_setwen = NULL; 524 rtc->rtc_bt = bt; 525 rtc->rtc_bh = ebi.ei_bh; 526 527 /* Save info for the clock wenable call. */ 528 ebi.ei_bt = bt; 529 ebi.ei_reg = ea->ea_regs[0]; 530 handle->bus_cookie = &ebi; 531 handle->todr_setwen = ebus_wenable; 532 todr_handle = handle; 533 } 534 535 /* 536 * The sun4u OPENPROMs call the timer the "counter-timer", except for 537 * the lame UltraSPARC IIi PCI machines that don't have them. 538 */ 539 static int 540 timermatch(parent, cf, aux) 541 struct device *parent; 542 void *cf; 543 void *aux; 544 { 545 struct mainbus_attach_args *ma = aux; 546 547 return (strcmp("counter-timer", ma->ma_name) == 0); 548 } 549 550 static void 551 timerattach(parent, self, aux) 552 struct device *parent, *self; 553 void *aux; 554 { 555 struct mainbus_attach_args *ma = aux; 556 u_int *va = ma->ma_address; 557 #if 0 558 volatile int64_t *cnt = NULL, *lim = NULL; 559 #endif 560 561 /* 562 * What we should have are 3 sets of registers that reside on 563 * different parts of SYSIO or PSYCHO. We'll use the prom 564 * mappings cause we can't get rid of them and set up appropriate 565 * pointers on the timerreg_4u structure. 566 */ 567 timerreg_4u.t_timer = (struct timer_4u *)(u_long)va[0]; 568 timerreg_4u.t_clrintr = (int64_t *)(u_long)va[1]; 569 timerreg_4u.t_mapintr = (int64_t *)(u_long)va[2]; 570 571 /* Install the appropriate interrupt vector here */ 572 level10.ih_number = ma->ma_interrupts[0]; 573 level10.ih_clr = (void*)&timerreg_4u.t_clrintr[0]; 574 intr_establish(10, &level10); 575 level14.ih_number = ma->ma_interrupts[1]; 576 level14.ih_clr = (void*)&timerreg_4u.t_clrintr[1]; 577 578 intr_establish(14, &level14); 579 printf(" irq vectors %lx and %lx", 580 (u_long)level10.ih_number, 581 (u_long)level14.ih_number); 582 583 #if 0 584 cnt = &(timerreg_4u.t_timer[0].t_count); 585 lim = &(timerreg_4u.t_timer[0].t_limit); 586 587 /* 588 * Calibrate delay() by tweaking the magic constant 589 * until a delay(100) actually reads (at least) 100 us 590 * on the clock. Since we're using the %tick register 591 * which should be running at exactly the CPU clock rate, it 592 * has a period of somewhere between 7ns and 3ns. 593 */ 594 595 #ifdef DEBUG 596 printf("Delay calibrarion....\n"); 597 #endif 598 for (timerblurb = 1; timerblurb > 0; timerblurb++) { 599 volatile int discard; 600 register int t0, t1; 601 602 /* Reset counter register by writing some large limit value */ 603 discard = *lim; 604 *lim = tmr_ustolim(TMR_MASK-1); 605 606 t0 = *cnt; 607 delay(100); 608 t1 = *cnt; 609 610 if (t1 & TMR_LIMIT) 611 panic("delay calibration"); 612 613 t0 = (t0 >> TMR_SHIFT) & TMR_MASK; 614 t1 = (t1 >> TMR_SHIFT) & TMR_MASK; 615 616 if (t1 >= t0 + 100) 617 break; 618 } 619 620 printf(" delay constant %d\n", timerblurb); 621 #endif 622 printf("\n"); 623 timerok = 1; 624 } 625 626 void 627 stopcounter(creg) 628 struct timer_4u *creg; 629 { 630 /* Stop the clock */ 631 volatile int discard; 632 discard = creg->t_limit; 633 creg->t_limit = 0; 634 } 635 636 /* 637 * XXX this belongs elsewhere 638 */ 639 void 640 myetheraddr(cp) 641 u_char *cp; 642 { 643 struct idprom *idp; 644 645 if ((idp = idprom) == NULL) { 646 int node, n; 647 648 node = findroot(); 649 if (getprop(node, "idprom", sizeof *idp, &n, (void **)&idp) || 650 n != 1) { 651 printf("\nmyetheraddr: clock not setup yet, " 652 "and no idprom property in /\n"); 653 return; 654 } 655 } 656 657 cp[0] = idp->id_ether[0]; 658 cp[1] = idp->id_ether[1]; 659 cp[2] = idp->id_ether[2]; 660 cp[3] = idp->id_ether[3]; 661 cp[4] = idp->id_ether[4]; 662 cp[5] = idp->id_ether[5]; 663 if (idprom == NULL) 664 free(idp, M_DEVBUF); 665 } 666 667 /* 668 * Set up the real-time and statistics clocks. Leave stathz 0 only if 669 * no alternative timer is available. 670 * 671 * The frequencies of these clocks must be an even number of microseconds. 672 */ 673 void 674 cpu_initclocks() 675 { 676 int statint, minint; 677 static u_int64_t start_time; 678 #ifdef DEBUG 679 extern int intrdebug; 680 #endif 681 682 #ifdef DEBUG 683 /* Set a 1s clock */ 684 if (intrdebug) { 685 hz = 1; 686 tick = 1000000 / hz; 687 printf("intrdebug set: 1Hz clock\n"); 688 } 689 #endif 690 691 if (1000000 % hz) { 692 printf("cannot get %d Hz clock; using 100 Hz\n", hz); 693 hz = 100; 694 tick = 1000000 / hz; 695 } 696 697 /* Make sure we have a sane cpu_clockrate -- we'll need it */ 698 if (!cpu_clockrate) 699 /* Default to 200MHz clock XXXXX */ 700 cpu_clockrate = 200000000; 701 702 /* 703 * Calculate the starting %tick value. We set that to the same 704 * as time, scaled for the CPU clockrate. This gets nasty, but 705 * we can handle it. time.tv_usec is in microseconds. 706 * cpu_clockrate is in MHz. 707 */ 708 start_time = time.tv_sec * cpu_clockrate; 709 /* Now fine tune the usecs */ 710 start_time += cpu_clockrate / 1000000 * time.tv_usec; 711 712 /* Initialize the %tick register */ 713 #ifdef __arch64__ 714 __asm __volatile("wrpr %0, 0, %%tick" : : "r" (start_time)); 715 #else 716 { 717 int start_hi = (start_time>>32), start_lo = start_time; 718 __asm __volatile("sllx %1,32,%0; or %0,%2,%0; wrpr %0, 0, %%tick" 719 : "=&r" (start_hi) /* scratch register */ 720 : "r" ((int)(start_hi)), "r" ((int)(start_lo))); 721 } 722 #endif 723 724 725 /* 726 * Now handle machines w/o counter-timers. 727 */ 728 729 if (!timerreg_4u.t_timer || !timerreg_4u.t_clrintr) { 730 731 printf("No counter-timer -- using %%tick at %ldMHz as system clock.\n", 732 (long)(cpu_clockrate/1000000)); 733 /* We don't have a counter-timer -- use %tick */ 734 level0.ih_clr = 0; 735 /* 736 * Establish a level 10 interrupt handler 737 * 738 * We will have a conflict with the softint handler, 739 * so we set the ih_number to 1. 740 */ 741 level0.ih_number = 1; 742 intr_establish(10, &level0); 743 /* We only have one timer so we have no statclock */ 744 stathz = 0; 745 746 /* set the next interrupt time */ 747 tick_increment = cpu_clockrate / hz; 748 #ifdef DEBUG 749 printf("Using %%tick -- intr in %ld cycles...", tick_increment); 750 #endif 751 next_tick(tick_increment); 752 #ifdef DEBUG 753 printf("done.\n"); 754 #endif 755 return; 756 } 757 758 if (stathz == 0) 759 stathz = hz; 760 if (1000000 % stathz) { 761 printf("cannot get %d Hz statclock; using 100 Hz\n", stathz); 762 stathz = 100; 763 } 764 765 profhz = stathz; /* always */ 766 767 statint = 1000000 / stathz; 768 minint = statint / 2 + 100; 769 while (statvar > minint) 770 statvar >>= 1; 771 772 /* 773 * Establish scheduler softint. 774 */ 775 schedint.ih_pil = PIL_SCHED; 776 schedint.ih_clr = NULL; 777 schedint.ih_arg = 0; 778 schedint.ih_pending = 0; 779 schedhz = stathz/4; 780 781 /* 782 * Enable timers 783 * 784 * Also need to map the interrupts cause we're not a child of the sbus. 785 * N.B. By default timer[0] is disabled and timer[1] is enabled. 786 */ 787 stxa((vaddr_t)&timerreg_4u.t_timer[0].t_limit, ASI_NUCLEUS, 788 tmr_ustolim(tick)|TMR_LIM_IEN|TMR_LIM_PERIODIC|TMR_LIM_RELOAD); 789 stxa((vaddr_t)&timerreg_4u.t_mapintr[0], ASI_NUCLEUS, 790 timerreg_4u.t_mapintr[0]|INTMAP_V); 791 792 #ifdef DEBUG 793 if (intrdebug) 794 /* Neglect to enable timer */ 795 stxa((vaddr_t)&timerreg_4u.t_timer[1].t_limit, ASI_NUCLEUS, 796 tmr_ustolim(statint)|TMR_LIM_RELOAD); 797 else 798 #endif 799 stxa((vaddr_t)&timerreg_4u.t_timer[1].t_limit, ASI_NUCLEUS, 800 tmr_ustolim(statint)|TMR_LIM_IEN|TMR_LIM_RELOAD); 801 stxa((vaddr_t)&timerreg_4u.t_mapintr[1], ASI_NUCLEUS, 802 timerreg_4u.t_mapintr[1]|INTMAP_V); 803 804 statmin = statint - (statvar >> 1); 805 806 } 807 808 /* 809 * Dummy setstatclockrate(), since we know profhz==hz. 810 */ 811 /* ARGSUSED */ 812 void 813 setstatclockrate(newhz) 814 int newhz; 815 { 816 /* nothing */ 817 } 818 819 /* 820 * Level 10 (clock) interrupts. If we are using the FORTH PROM for 821 * console input, we need to check for that here as well, and generate 822 * a software interrupt to read it. 823 */ 824 #ifdef DEBUG 825 static int clockcheck = 0; 826 #endif 827 int 828 clockintr(cap) 829 void *cap; 830 { 831 #ifdef DEBUG 832 static int64_t tick_base = 0; 833 int64_t t = (u_int64_t)tick(); 834 835 if (!tick_base) { 836 tick_base = (time.tv_sec * 1000000LL + time.tv_usec) 837 * 1000000LL / cpu_clockrate; 838 tick_base -= t; 839 } else if (clockcheck) { 840 int64_t tk = t; 841 int64_t clk = (time.tv_sec * 1000000LL + time.tv_usec); 842 t -= tick_base; 843 t = t * 1000000LL / cpu_clockrate; 844 if (t - clk > hz) { 845 printf("Clock lost an interrupt!\n"); 846 printf("Actual: %llx Expected: %llx tick %llx tick_base %llx\n", 847 (long long)t, (long long)clk, (long long)tk, (long long)tick_base); 848 #ifdef DDB 849 Debugger(); 850 #endif 851 tick_base = 0; 852 } 853 } 854 #endif 855 /* Let locore.s clear the interrupt for us. */ 856 hardclock((struct clockframe *)cap); 857 return (1); 858 } 859 860 int poll_console = 0; 861 862 /* 863 * Level 10 (clock) interrupts. If we are using the FORTH PROM for 864 * console input, we need to check for that here as well, and generate 865 * a software interrupt to read it. 866 * 867 * %tick is really a level-14 interrupt. We need to remap this in 868 * locore.s to a level 10. 869 */ 870 int 871 tickintr(cap) 872 void *cap; 873 { 874 int s; 875 876 hardclock((struct clockframe *)cap); 877 if (poll_console) 878 setsoftint(); 879 880 s = splhigh(); 881 /* Reset the interrupt */ 882 next_tick(tick_increment); 883 splx(s); 884 885 return (1); 886 } 887 888 /* 889 * Level 14 (stat clock) interrupt handler. 890 */ 891 int 892 statintr(cap) 893 void *cap; 894 { 895 register u_long newint, r, var; 896 struct cpu_info *ci = curcpu(); 897 898 #ifdef NOT_DEBUG 899 printf("statclock: count %x:%x, limit %x:%x\n", 900 timerreg_4u.t_timer[1].t_count, timerreg_4u.t_timer[1].t_limit); 901 #endif 902 #ifdef NOT_DEBUG 903 prom_printf("!"); 904 #endif 905 statclock((struct clockframe *)cap); 906 #ifdef NOTDEF_DEBUG 907 /* Don't re-schedule the IRQ */ 908 return 1; 909 #endif 910 /* 911 * Compute new randomized interval. The intervals are uniformly 912 * distributed on [statint - statvar / 2, statint + statvar / 2], 913 * and therefore have mean statint, giving a stathz frequency clock. 914 */ 915 var = statvar; 916 do { 917 r = random() & (var - 1); 918 } while (r == 0); 919 newint = statmin + r; 920 921 if (schedhz) 922 if ((++ci->ci_schedstate.spc_schedticks & 3) == 0) 923 send_softint(-1, PIL_SCHED, &schedint); 924 stxa((vaddr_t)&timerreg_4u.t_timer[1].t_limit, ASI_NUCLEUS, 925 tmr_ustolim(newint)|TMR_LIM_IEN|TMR_LIM_RELOAD); 926 return (1); 927 } 928 929 int 930 schedintr(arg) 931 void *arg; 932 { 933 if (curproc) 934 schedclock(curproc); 935 return (1); 936 } 937 938 939 /* 940 * `sparc_clock_time_is_ok' is used in cpu_reboot() to determine 941 * whether it is appropriate to call resettodr() to consolidate 942 * pending time adjustments. 943 */ 944 int sparc_clock_time_is_ok; 945 946 /* 947 * Set up the system's time, given a `reasonable' time value. 948 */ 949 void 950 inittodr(base) 951 time_t base; 952 { 953 int badbase = 0, waszero = base == 0; 954 955 if (base < 5 * SECYR) { 956 /* 957 * If base is 0, assume filesystem time is just unknown 958 * in stead of preposterous. Don't bark. 959 */ 960 if (base != 0) 961 printf("WARNING: preposterous time in file system\n"); 962 /* not going to use it anyway, if the chip is readable */ 963 base = 21*SECYR + 186*SECDAY + SECDAY/2; 964 badbase = 1; 965 } 966 967 if (todr_handle && 968 (todr_gettime(todr_handle, (struct timeval *)&time) != 0 || 969 time.tv_sec == 0)) { 970 printf("WARNING: bad date in battery clock"); 971 /* 972 * Believe the time in the file system for lack of 973 * anything better, resetting the clock. 974 */ 975 time.tv_sec = base; 976 if (!badbase) 977 resettodr(); 978 } else { 979 int deltat = time.tv_sec - base; 980 981 sparc_clock_time_is_ok = 1; 982 983 if (deltat < 0) 984 deltat = -deltat; 985 if (waszero || deltat < 2 * SECDAY) 986 return; 987 printf("WARNING: clock %s %d days", 988 time.tv_sec < base ? "lost" : "gained", deltat / SECDAY); 989 } 990 printf(" -- CHECK AND RESET THE DATE!\n"); 991 } 992 993 /* 994 * Reset the clock based on the current time. 995 * Used when the current clock is preposterous, when the time is changed, 996 * and when rebooting. Do nothing if the time is not yet known, e.g., 997 * when crashing during autoconfig. 998 */ 999 void 1000 resettodr() 1001 { 1002 1003 if (time.tv_sec == 0) 1004 return; 1005 1006 sparc_clock_time_is_ok = 1; 1007 if (todr_handle == 0 || 1008 todr_settime(todr_handle, (struct timeval *)&time) != 0) 1009 printf("Cannot set time in time-of-day clock\n"); 1010 } 1011 1012 /* 1013 * XXX: these may actually belong somewhere else, but since the 1014 * EEPROM is so closely tied to the clock on some models, perhaps 1015 * it needs to stay here... 1016 */ 1017 int 1018 eeprom_uio(uio) 1019 struct uio *uio; 1020 { 1021 return (ENODEV); 1022 } 1023 1024 1025 /* 1026 * RTC todr routines. 1027 */ 1028 1029 /* 1030 * Get time-of-day and convert to a `struct timeval' 1031 * Return 0 on success; an error number otherwise. 1032 */ 1033 int 1034 rtc_gettime(handle, tv) 1035 todr_chip_handle_t handle; 1036 struct timeval *tv; 1037 { 1038 struct rtc_info *rtc = handle->cookie; 1039 bus_space_tag_t bt = rtc->rtc_bt; 1040 bus_space_handle_t bh = rtc->rtc_bh; 1041 struct clock_ymdhms dt; 1042 int year; 1043 u_int8_t csr; 1044 1045 todr_wenable(handle, 1); 1046 1047 /* Stop updates. */ 1048 csr = rtc_read_reg(bt, bh, MC_REGB); 1049 csr |= MC_REGB_SET; 1050 rtc_write_reg(bt, bh, MC_REGB, csr); 1051 1052 /* Read time */ 1053 dt.dt_sec = rtc_read_reg(bt, bh, MC_SEC); 1054 dt.dt_min = rtc_read_reg(bt, bh, MC_MIN); 1055 dt.dt_hour = rtc_read_reg(bt, bh, MC_HOUR); 1056 dt.dt_day = rtc_read_reg(bt, bh, MC_DOM); 1057 dt.dt_wday = rtc_read_reg(bt, bh, MC_DOW); 1058 dt.dt_mon = rtc_read_reg(bt, bh, MC_MONTH); 1059 year = rtc_read_reg(bt, bh, MC_YEAR); 1060 1061 if ((year += 1900) < POSIX_BASE_YEAR) 1062 year += 100; 1063 1064 dt.dt_year = year; 1065 1066 /* time wears on */ 1067 csr = rtc_read_reg(bt, bh, MC_REGB); 1068 csr &= ~MC_REGB_SET; 1069 rtc_write_reg(bt, bh, MC_REGB, csr); 1070 todr_wenable(handle, 0); 1071 1072 /* simple sanity checks */ 1073 if (dt.dt_mon > 12 || dt.dt_day > 31 || 1074 dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60) 1075 return (1); 1076 1077 tv->tv_sec = clock_ymdhms_to_secs(&dt); 1078 tv->tv_usec = 0; 1079 return (0); 1080 } 1081 1082 /* 1083 * Set the time-of-day clock based on the value of the `struct timeval' arg. 1084 * Return 0 on success; an error number otherwise. 1085 */ 1086 int 1087 rtc_settime(handle, tv) 1088 todr_chip_handle_t handle; 1089 struct timeval *tv; 1090 { 1091 struct rtc_info *rtc = handle->cookie; 1092 bus_space_tag_t bt = rtc->rtc_bt; 1093 bus_space_handle_t bh = rtc->rtc_bh; 1094 struct clock_ymdhms dt; 1095 u_int8_t csr; 1096 int year; 1097 1098 /* Note: we ignore `tv_usec' */ 1099 clock_secs_to_ymdhms(tv->tv_sec, &dt); 1100 1101 year = dt.dt_year % 100; 1102 1103 todr_wenable(handle, 1); 1104 /* enable write */ 1105 csr = rtc_read_reg(bt, bh, MC_REGB); 1106 csr |= MC_REGB_SET; 1107 rtc_write_reg(bt, bh, MC_REGB, csr); 1108 1109 rtc_write_reg(bt, bh, MC_SEC, dt.dt_sec); 1110 rtc_write_reg(bt, bh, MC_MIN, dt.dt_min); 1111 rtc_write_reg(bt, bh, MC_HOUR, dt.dt_hour); 1112 rtc_write_reg(bt, bh, MC_DOW, dt.dt_wday); 1113 rtc_write_reg(bt, bh, MC_DOM, dt.dt_day); 1114 rtc_write_reg(bt, bh, MC_MONTH, dt.dt_mon); 1115 rtc_write_reg(bt, bh, MC_YEAR, year); 1116 1117 /* load them up */ 1118 csr = rtc_read_reg(bt, bh, MC_REGB); 1119 csr &= ~MC_REGB_SET; 1120 rtc_write_reg(bt, bh, MC_REGB, csr); 1121 todr_wenable(handle, 0); 1122 return (0); 1123 } 1124 1125 int 1126 rtc_getcal(handle, vp) 1127 todr_chip_handle_t handle; 1128 int *vp; 1129 { 1130 return (EOPNOTSUPP); 1131 } 1132 1133 int 1134 rtc_setcal(handle, v) 1135 todr_chip_handle_t handle; 1136 int v; 1137 { 1138 return (EOPNOTSUPP); 1139 } 1140 1141