⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btflash.c

📁 strongarm的bootloader
💻 C
📖 第 1 页 / 共 3 页
字号:
      return -1;   }      flashword[0x555] = doubleword(0xAA);   flashword[0x2AA] = doubleword(0x55);   flashword[0x555] = doubleword(0x80);   flashword[0x555] = doubleword(0xAA);   flashword[0x2AA] = doubleword(0x55);   flashword[flashWordOffset] = doubleword(0x30);   while (((flashContents = flashword[flashWordOffset]) != 0xFFFFFFFFL) &&          (timeout > 0)) {      unsigned long hiword = (flashContents >> 16) & 0xFFFFl;      unsigned long loword = (flashContents >> 0) & 0xFFFFl;      if (0 && (hiword != hivalue) && (hiword & (1 << 5))) {         /* programming upper bank of flash timed out */         putstr("Upper bank of flash timed out!!");         break;      }      if (0 && (loword != lovalue) && (loword & (1 << 5))) {         /* programming upper bank of flash timed out */         putstr("Lower bank of flash timed out!!");         break;      }      oldFlashContents = flashContents; /* to check for toggle bits */      timeout--;   }   if (timeout <= 0) {      putstr("eraseFlashSector timeout\r\n");      putLabeledWord("  flashp=", (dword)(&flashword[flashWordOffset]));      putLabeledWord("  flashAddress=", sectorAddress);      putLabeledWord("  flashContents=", flashContents);      putLabeledWord("  oldFlashContents=", oldFlashContents);      putLabeledWord("  timeout=", timeout);      return(-1);   }   return 0;}static int amdFlashEraseRange(unsigned long startAddress, unsigned long len){  unsigned long lastSectorAddress = flashSectors[0];  unsigned long limitAddress = startAddress + len;  unsigned long sectorAddress, i;  startAddress &= flash_address_mask;  limitAddress &= flash_address_mask;  for (i=0;i<nsectors;i++) {     sectorAddress = flashSectors[i+1]; /* actually nsectors entries in this array -- last is fictitious guard sector */    if ((lastSectorAddress <= startAddress) && (sectorAddress > startAddress)) {      putLabeledWord("Erasing sector ",lastSectorAddress);      if (eraseFlashSector(lastSectorAddress))	return -1;      len -= (sectorAddress - startAddress);      startAddress = sectorAddress;      if (startAddress >= limitAddress)	break;    }    lastSectorAddress = sectorAddress;  }  return 0;}static int amdFlashProtectRange(unsigned long startAddress, unsigned long len, int protect){   putstr("amdFlashProtectRange: unimplemented!!\r\n");   return 0;}#endif CONFIG_AMD_FLASH#ifdef CONFIG_INTEL_FLASHint intelFlashReset (){   /* send flash the reset command */   /* address does not matter -- only the data */   flashword[0x55] = doubleword(0xFF);   return 0;}static int intelFlashReadStatus(){   /* address does not matter */   flashword[0x55] = doubleword(0x70);   return flashword[0x55];}static int intelFlashClearStatus(){   /* address does not matter */   flashword[0x55] = doubleword(0x50);}/* * Programs value at flashAddress * Sectors must be erased before they can be programmed. */static int intelFlashProgramWord(unsigned long flashAddress, unsigned long value){   unsigned long flashWordOffset = (flashAddress&flash_address_mask) >> 2;   long timeout = FLASH_TIMEOUT;   unsigned long flashContents = flashword[flashWordOffset];   unsigned long status = 0;   /* see if we can program the value without erasing */   if ((flashContents & value) != value) {      putstr("the flash sector needs to be erased first!\r\n");      putLabeledWord("  flashAddress=", flashAddress);      putLabeledWord("  flashWordOffset=", flashWordOffset);      putLabeledWord("  &flashword[flashWordOffset]=", 		     (dword)&flashword[flashWordOffset]);      putLabeledWord("  flashContents=", flashContents);      return -1;   }      /* send flash the program word command */   flashword[0x55] = doubleword(0x40);   flashword[flashWordOffset] = value;   /* now wait for it to be programmed */   while (timeout > 0) {      status = flashword[flashWordOffset];      if ((status & doubleword(0x80)) == doubleword(0x80))         break;      timeout--;   }   intelFlashClearStatus();      if ((timeout <= 0) || (status & doubleword(0x7f))) {      putstr("programFlashWord error\r\n");      putLabeledWord("  flashAddress=", flashAddress);      putLabeledWord("  value=", value);      putLabeledWord("  flashContents=", flashContents);      putLabeledWord("  status=", status);      return(-1);   }   return 0;}/* each flash chip has 32B block, 64B block for chip array */static int intelFlashProgramBlock(unsigned long flashAddress, unsigned long *values, int nbytes){   unsigned long flashWordOffset = (flashAddress&flash_address_mask) >> 2;   unsigned long blockOffset = (flashWordOffset & 0xFFFFFFC0);   int nwords = nbytes >> 2;   int result = 0;   long timeout = FLASH_TIMEOUT;   unsigned long status = 0;   int i;   if (0) putLabeledWord("intelFlashProgramFlashBlock\r\n", flashAddress);   /* send the "write to buffer" command */   flashword[flashWordOffset] = doubleword(0xE8);    /* read the extended status register */   do {      status = flashword[flashWordOffset];      if (0) putLabeledWord("  XSR=", status);   } while ((status & 0x00800080) != 0x00800080);   /* write word count at block start address */   flashword[blockOffset] = doubleword(nwords-1); /* length minus one */   /* send the data */   for (i = 0; i < nwords; i++) {      flashword[flashWordOffset+i] = values[i];      if (0) putLabeledWord(" sr=", flashword[blockOffset]);   }   /* send the confirmation to program */   flashword[blockOffset] = doubleword(0xD0);   /* now wait for it to be programmed */   timeout = FLASH_TIMEOUT;   while (timeout > 0) {      status = flashword[blockOffset];      if (0) putLabeledWord("  status=", status);      if ((status & doubleword(0x80)) == doubleword(0x80))         break;      timeout--;   }   status = intelFlashReadStatus();   if (0) putLabeledWord("final status=", status);   intelFlashClearStatus();      if ((timeout <= 0) || (status & doubleword(0x7f))) {      putstr("programFlashBlock error\r\n");      putLabeledWord("  flashAddress=", flashAddress);      putLabeledWord("  status=", status);      return(-1);   }   return 0;}int intelFlashEraseChip (){   int i;   long timeout = FLASH_TIMEOUT;   unsigned long flashWordOffset = 0;   unsigned long flashContents;      putstr("intelFlashEraseChip unimplemented\r\n");   return 0;}/* sectorAddress must be a valid start of sector address. sectors   must be erased before they can be programmed! */static int intelFlashEraseSector (unsigned long sectorAddress){   int i;   long timeout = FLASH_TIMEOUT;   unsigned long flashWordOffset = (sectorAddress&flash_address_mask) >> 2;   unsigned long flashContents;   unsigned long status;   for (i = 0; i < nsectors; i++) {      if (flashSectors[i] == sectorAddress)         break;   }   if (i >= nsectors) {      putLabeledWord("eraseFlashSector: sectorAddress must be start of a sector! address=", sectorAddress);      putLabeledWord("nsectors=", nsectors);      return -1;   }   /* send flash the erase sector command */   flashword[0x55] = doubleword(0x20);   flashword[flashWordOffset] = doubleword(0xD0);   /* now wait for it to be programmed */   while (timeout > 0) {      status = flashword[flashWordOffset];      if ((status & doubleword(0x80)) == doubleword(0x80))         break;      timeout--;   }   intelFlashClearStatus();   flashContents = flashword[flashWordOffset];   if ((timeout <= 0) || (status & doubleword(0x7f))) {      putstr("eraseSector error\r\n");      putLabeledWord("  sectorAddress=", sectorAddress);      putLabeledWord("  flashContents=", flashContents);      putLabeledWord("  status=", status);      return(-1);   }      return 0;}static int intelFlashEraseRange(unsigned long startAddress, unsigned long len){  unsigned long lastSectorAddress = flashSectors[0];  unsigned long limitAddress = startAddress + len;  unsigned long sectorAddress, i;  startAddress &= flash_address_mask;  limitAddress &= flash_address_mask;  for (i=0;i<nsectors;i++) {     sectorAddress = flashSectors[i+1]; /* actually nsectors entries in this array -- last is fictitious guard sector */    if ((lastSectorAddress <= startAddress) && (sectorAddress > startAddress)) {      putLabeledWord("Erasing sector ",lastSectorAddress);      putLabeledWord("  len=", len);      if (eraseFlashSector(lastSectorAddress))	return -1;      len -= (sectorAddress - startAddress);      startAddress = sectorAddress;      if (startAddress >= limitAddress)	break;    }    lastSectorAddress = sectorAddress;  }  return 0;}static int intelFlashProtectRange(unsigned long startAddress, unsigned long len, int protect){  unsigned long lastSectorAddress = flashSectors[0];  unsigned long limitAddress = startAddress + len;  unsigned long sectorAddress, i;  int result = 0;  startAddress &= flash_address_mask;  limitAddress &= flash_address_mask;  if (protect) {    for (i=0;i<nsectors;i++) {      sectorAddress = flashSectors[i+1]; /* actually nsectors entries in this array -- last is fictitious guard sector */      if ((lastSectorAddress <= startAddress) && (sectorAddress > startAddress)) {	int status;	putLabeledWord("Protecting sector ",lastSectorAddress);	flashword[0] = doubleword(0x60);	flashword[sectorAddress >> 2] = doubleword(0x01);	status = intelFlashReadStatus();	intelFlashClearStatus();	if (status != doubleword(0x80)) {	  return -1;	}	len -= (sectorAddress - startAddress);	startAddress = sectorAddress;	if (startAddress >= limitAddress)	  break;      }      lastSectorAddress = sectorAddress;    }  } else {    /* unprotects whole chip */    flashword[0] = doubleword(0x60);    flashword[0] = doubleword(0xd0);    if (intelFlashReadStatus() != doubleword(0x80))      result = -1;    intelFlashClearStatus();  }  return result;}#endif /* CONFIG_INTEL_FLASH */void btflash_print_types(){  int i;  putstr("Flash types supported:\r\n");  for (i = 0; flashDescriptors[i] != NULL; i++) {    FlashDescriptor *fd = flashDescriptors[i];    putstr("  "); putstr(fd->deviceName); putstr("\r\n");  }  putstr("Current flash type is "); putstr(flashDescriptor->deviceName); putstr("\r\n");}void btflash_set_type(const char *devicename){   int i;   for (i = 0; flashDescriptors[i] != NULL; i++) {      FlashDescriptor *fd = flashDescriptors[i];      if (strcmp(fd->deviceName, devicename) == 0) {         putstr("setting flash type="); putstr(flashDescriptor->deviceName); putstr("\r\n");         flashDescriptor = fd;         nsectors = flashDescriptor->nsectors;         flashSectors = flashDescriptor->sectors;         flash_size = flashSectors[nsectors];         flash_address_mask = flash_size-1;         return;      }   }   putstr("  Unknown flash device type: "); putstr(devicename); putstr("\r\n");}void btflash_init(){   int algorithm = updateFlashAlgorithm();   int mfrid = queryFlashID(0x00);   int devid = queryFlashID(0x01);   int protected = queryFlashID(0x02);   FlashDescriptor **fd = &flashDescriptors[0];   putLabeledWord("btflash_init: mfrid=", mfrid);   putLabeledWord("  devid=", devid);   if ((devid&0xFFFF) != ((devid>>16)&0xFFFF)) {#ifndef linux      struct bootblk_param *noeraseParam = get_param("noerase");      putLabeledWord("\r\n\r\n@@@@@@@@ mismatched flash parts @@@@@@@@\r\n\r\n", devid);      if (noeraseParam != NULL)         noeraseParam->value = 1;#endif      flashDescriptor = fd[0];   }   putstr("walking flash descriptors\r\n");   for (flashDescriptor = *fd;         (flashDescriptor != NULL);         fd++, flashDescriptor = *fd) {      if (doubleword(flashDescriptor->manufacturerID) == mfrid          && doubleword(flashDescriptor->deviceID) == devid) {         putstr("btflash_init: found flash "); putstr(flashDescriptor->deviceName); putstr("\r\n");         nsectors = flashDescriptor->nsectors;         flashSectors = flashDescriptor->sectors;         flash_size = flashSectors[nsectors];         flash_address_mask = flash_size-1;         putLabeledWord("  flashDescriptor=", (long)flashDescriptor);         putLabeledWord("  flashSectors=", (long)flashSectors);         putLabeledWord("  nsectors=", nsectors);         putLabeledWord("  flash_size=", flash_size);         putLabeledWord("  flash_address_mask=", flash_address_mask);         break;      }   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -