tgt_machdep.c
来自「国产CPU-龙芯(loongson)BIOS源代码」· C语言 代码 · 共 2,203 行 · 第 1/4 页
C
2,203 行
tm.tm_year = CMOS_READ(DS_REG_YEAR); if(tm.tm_year < 50)tm.tm_year += 100; CMOS_WRITE(ctrlbsave & ~DS_CTLB_SET, DS_REG_CTLB); tm.tm_isdst = tm.tm_gmtoff = 0; t = gmmktime(&tm); } else { t = 957960000; /* Wed May 10 14:00:00 2000 :-) */ } return(t);} /* * Set the current time if a TOD clock is present */voidtgt_settime(time_t t){ struct tm *tm; int ctrlbsave; if(!clk_invalid) { tm = gmtime(&t); ctrlbsave = CMOS_READ(DS_REG_CTLB); CMOS_WRITE(ctrlbsave | DS_CTLB_SET, DS_REG_CTLB); CMOS_WRITE(tm->tm_year % 100, DS_REG_YEAR); CMOS_WRITE(tm->tm_mon + 1, DS_REG_MONTH); CMOS_WRITE(tm->tm_mday, DS_REG_DATE); CMOS_WRITE(tm->tm_wday, DS_REG_WDAY); CMOS_WRITE(tm->tm_hour, DS_REG_HOUR); CMOS_WRITE(tm->tm_min, DS_REG_MIN); CMOS_WRITE(tm->tm_sec, DS_REG_SEC); CMOS_WRITE(ctrlbsave & ~DS_CTLB_SET, DS_REG_CTLB); }}/* * Print out any target specific memory information */voidtgt_memprint(){ printf("Primary Instruction cache size %dkb (%d line, %d way)\n", CpuPrimaryInstCacheSize / 1024, CpuPrimaryInstCacheLSize, CpuNWayCache); printf("Primary Data cache size %dkb (%d line, %d way)\n", CpuPrimaryDataCacheSize / 1024, CpuPrimaryDataCacheLSize, CpuNWayCache); if(CpuSecondaryCacheSize != 0) { printf("Secondary cache size %dkb\n", CpuSecondaryCacheSize / 1024); } if(CpuTertiaryCacheSize != 0) { printf("Tertiary cache size %dkb\n", CpuTertiaryCacheSize / 1024); }}voidtgt_machprint(){ printf("Copyright 2000-2002, Opsycon AB, Sweden.\n"); printf("Copyright 2003, Michael and Pine, ICT CAS.\n"); printf("CPU %s @", md_cpuname());} /* * Return a suitable address for the client stack. * Usually top of RAM memory. */register_ttgt_clienttos(){ return((register_t)(int)PHYS_TO_UNCACHED(memorysize & ~7) - 64);}#ifdef HAVE_FLASH/* * Flash programming support code. *//* * Table of flash devices on target. See pflash_tgt.h. */struct fl_map tgt_fl_map_boot16[]={ TARGET_FLASH_DEVICES_16};struct fl_map *tgt_flashmap(){ /*if((GT_READ(GT_BOOT_PAR) & 0x00300000) == 0) { return tgt_fl_map_boot8; } else { return tgt_fl_map_boot32; }*/ return tgt_fl_map_boot16;} voidtgt_flashwrite_disable(){}inttgt_flashwrite_enable(){ return(1);}voidtgt_flashinfo(void *p, size_t *t){ struct fl_map *map; map = fl_find_map(p); if(map) { *t = map->fl_map_size; } else { *t = 0; }}voidtgt_flashprogram(void *p, int size, void *s, int endian){ printf("Programming flash %x:%x into %x\n", s, size, p); if(fl_erase_device(p, size, TRUE)) { printf("Erase failed!\n"); return; } if(fl_program_device(p, s, size, TRUE)) { printf("Programming failed!\n"); } fl_verify_device(p, s, size, TRUE);}#endif /* PFLASH *//* * Network stuff. */voidtgt_netinit(){}inttgt_ethaddr(char *p){ bcopy((void *)&hwethadr, p, 6); return(0);}voidtgt_netreset(){}/*************************************************************************//* * Target dependent Non volatile memory support code * ================================================= * * * On this target a part of the boot flash memory is used to store * environment. See EV64260.h for mapping details. (offset and size). *//* * Read in environment from NV-ram and set. */voidtgt_mapenv(int (*func) __P((char *, char *))){ char *ep; char env[512]; char *nvram; int i; /* * Check integrity of the NVRAM env area. If not in order * initialize it to empty. */ printf("in envinit\n");#ifdef NVRAM_IN_FLASH nvram = (char *)(tgt_flashmap())->fl_map_base; printf("%08x\n",(unsigned int)nvram); if(fl_devident(nvram, NULL) == 0 || cksum(nvram + NVRAM_OFFS, NVRAM_SIZE, 0) != 0) {#else nvram = (char *)malloc(512); nvram_get(nvram); if(cksum(nvram, NVRAM_SIZE, 0) != 0) {#endif printf("NVRAM is invalid!\n"); nvram_invalid = 1; } else { nvram += NVRAM_OFFS; ep = nvram+2;; while(*ep != 0) { char *val = 0, *p = env; i = 0; while((*p++ = *ep++) && (ep <= nvram + NVRAM_SIZE - 1) && i++ < 255) { if((*(p - 1) == '=') && (val == NULL)) { *(p - 1) = '\0'; val = p; } } if(ep <= nvram + NVRAM_SIZE - 1 && i < 255) { (*func)(env, val); } else { nvram_invalid = 2; break; } } } printf("NVRAM@%x\n",(u_int32_t)nvram); /* * Ethernet address for Galileo ethernet is stored in the last * six bytes of nvram storage. Set environment to it. */ bcopy(&nvram[ETHER_OFFS], hwethadr, 6); sprintf(env, "%02x:%02x:%02x:%02x:%02x:%02x", hwethadr[0], hwethadr[1], hwethadr[2], hwethadr[3], hwethadr[4], hwethadr[5]); (*func)("ethaddr", env);#ifndef NVRAM_IN_FLASH free(nvram);#endif#ifdef no_thank_you (*func)("vxWorks", env);#endif sprintf(env, "%d", memorysize / (1024 * 1024)); (*func)("memsize", env); sprintf(env, "%d", md_pipefreq); (*func)("cpuclock", env); sprintf(env, "%d", md_cpufreq); (*func)("busclock", env); (*func)("systype", SYSTYPE); }inttgt_unsetenv(char *name){ char *ep, *np, *sp; char *nvram; char *nvrambuf; char *nvramsecbuf; int status; if(nvram_invalid) { return(0); } /* Use first defined flash device (we probably have only one) */#ifdef NVRAM_IN_FLASH nvram = (char *)(tgt_flashmap())->fl_map_base; /* Map. Deal with an entire sector even if we only use part of it */ nvram += NVRAM_OFFS & ~(NVRAM_SECSIZE - 1); nvramsecbuf = (char *)malloc(NVRAM_SECSIZE); if(nvramsecbuf == 0) { printf("Warning! Unable to malloc nvrambuffer!\n"); return(-1); } memcpy(nvramsecbuf, nvram, NVRAM_SECSIZE); nvrambuf = nvramsecbuf + (NVRAM_OFFS & (NVRAM_SECSIZE - 1));#else nvramsecbuf = nvrambuf = nvram = (char *)malloc(512); nvram_get(nvram);#endif ep = nvrambuf + 2; status = 0; while((*ep != '\0') && (ep <= nvrambuf + NVRAM_SIZE)) { np = name; sp = ep; while((*ep == *np) && (*ep != '=') && (*np != '\0')) { ep++; np++; } if((*np == '\0') && ((*ep == '\0') || (*ep == '='))) { while(*ep++); while(ep <= nvrambuf + NVRAM_SIZE) { *sp++ = *ep++; } if(nvrambuf[2] == '\0') { nvrambuf[3] = '\0'; } cksum(nvrambuf, NVRAM_SIZE, 1);#ifdef NVRAM_IN_FLASH if(fl_erase_device(nvram, NVRAM_SECSIZE, FALSE)) { status = -1; break; } if(fl_program_device(nvram, nvramsecbuf, NVRAM_SECSIZE, FALSE)) { status = -1; break; }#else nvram_put(nvram);#endif status = 1; break; } else if(*ep != '\0') { while(*ep++ != '\0'); } } free(nvramsecbuf); return(status);}inttgt_setenv(char *name, char *value){ char *ep; int envlen; char *nvrambuf; char *nvramsecbuf;#ifdef NVRAM_IN_FLASH char *nvram;#endif /* Non permanent vars. */ if(strcmp(EXPERT, name) == 0) { return(1); } /* Calculate total env mem size requiered */ envlen = strlen(name); if(envlen == 0) { return(0); } if(value != NULL) { envlen += strlen(value); } envlen += 2; /* '=' + null byte */ if(envlen > 255) { return(0); /* Are you crazy!? */ } /* Use first defined flash device (we probably have only one) */#ifdef NVRAM_IN_FLASH nvram = (char *)(tgt_flashmap())->fl_map_base; /* Deal with an entire sector even if we only use part of it */ nvram += NVRAM_OFFS & ~(NVRAM_SECSIZE - 1);#endif /* If NVRAM is found to be uninitialized, reinit it. */ if(nvram_invalid) { nvramsecbuf = (char *)malloc(NVRAM_SECSIZE); if(nvramsecbuf == 0) { printf("Warning! Unable to malloc nvrambuffer!\n"); return(-1); }#ifdef NVRAM_IN_FLASH memcpy(nvramsecbuf, nvram, NVRAM_SECSIZE);#endif nvrambuf = nvramsecbuf + (NVRAM_OFFS & (NVRAM_SECSIZE - 1)); memset(nvrambuf, -1, NVRAM_SIZE); nvrambuf[2] = '\0'; nvrambuf[3] = '\0'; cksum((void *)nvrambuf, NVRAM_SIZE, 1); printf("Warning! NVRAM checksum fail. Reset!\n");#ifdef NVRAM_IN_FLASH if(fl_erase_device(nvram, NVRAM_SECSIZE, FALSE)) { printf("Error! Nvram erase failed!\n"); free(nvramsecbuf); return(-1); } if(fl_program_device(nvram, nvramsecbuf, NVRAM_SECSIZE, FALSE)) { printf("Error! Nvram init failed!\n"); free(nvramsecbuf); return(-1); }#else nvram_put(nvramsecbuf);#endif nvram_invalid = 0; free(nvramsecbuf); } /* Remove any current setting */ tgt_unsetenv(name); /* Find end of evironment strings */ nvramsecbuf = (char *)malloc(NVRAM_SECSIZE); if(nvramsecbuf == 0) { printf("Warning! Unable to malloc nvrambuffer!\n"); return(-1); }#ifndef NVRAM_IN_FLASH nvram_get(nvramsecbuf);#else memcpy(nvramsecbuf, nvram, NVRAM_SECSIZE);#endif nvrambuf = nvramsecbuf + (NVRAM_OFFS & (NVRAM_SECSIZE - 1)); /* Etheraddr is special case to save space */ if (strcmp("ethaddr", name) == 0) { char *s = value; int i; int32_t v; for(i = 0; i < 6; i++) { gethex(&v, s, 2); hwethadr[i] = v; s += 3; /* Don't get to fancy here :-) */ } } else { ep = nvrambuf+2; if(*ep != '\0') { do { while(*ep++ != '\0'); } while(*ep++ != '\0'); ep--; } if(((int)ep + NVRAM_SIZE - (int)ep) < (envlen + 1)) { free(nvramsecbuf); return(0); /* Bummer! */ } /* * Special case heaptop must always be first since it * can change how memory allocation works. */ if(strcmp("heaptop", name) == 0) { bcopy(nvrambuf+2, nvrambuf+2 + envlen, ep - nvrambuf+1); ep = nvrambuf+2; while(*name != '\0') { *ep++ = *name++; } if(value != NULL) { *ep++ = '='; while((*ep++ = *value++) != '\0'); } else { *ep++ = '\0'; } } else { while(*name != '\0') { *ep++ = *name++; } if(value != NULL) { *ep++ = '='; while((*ep++ = *value++) != '\0'); } else { *ep++ = '\0'; } *ep++ = '\0'; /* End of env strings */ } } cksum(nvrambuf, NVRAM_SIZE, 1); bcopy(hwethadr, &nvramsecbuf[ETHER_OFFS], 6);#ifdef NVRAM_IN_FLASH if(fl_erase_device(nvram, NVRAM_SECSIZE, FALSE)) { printf("Error! Nvram erase failed!\n"); free(nvramsecbuf); return(0); } if(fl_program_device(nvram, nvramsecbuf, NVRAM_SECSIZE, FALSE)) { printf("Error! Nvram program failed!\n"); free(nvramsecbuf); return(0); }#else nvram_put(nvramsecbuf);#endif free(nvramsecbuf); return(1);}/* * Calculate checksum. If 'set' checksum is calculated and set. */static intcksum(void *p, size_t s, int set){ u_int16_t sum = 0; u_int8_t *sp = p; int sz = s / 2; if(set) { *sp = 0; /* Clear checksum */ *(sp+1) = 0; /* Clear checksum */ } while(sz--) { sum += (*sp++) << 8; sum += *sp++; } if(set) { sum = -sum; *(u_int8_t *)p = sum >> 8; *((u_int8_t *)p+1) = sum; } return(sum);}#ifndef NVRAM_IN_FLASH/* * Read and write data into non volatile memory in clock chip. */voidnvram_get(char *buffer){ int i; for(i = 0; i < 114; i++) { linux_outb(i + RTC_NVRAM_BASE, RTC_INDEX_REG); /* Address */ buffer[i] = linux_inb(RTC_DATA_REG); }}voidnvram_put(char *buffer){ int i; for(i = 0; i < 114; i++) { linux_outb(i+RTC_NVRAM_BASE, RTC_INDEX_REG); /* Address */ linux_outb(buffer[i],RTC_DATA_REG); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?