Main Page   Modules   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

/projects/cubeos/src_current/drivers/flash/flash.c

Go to the documentation of this file.
00001 /*  src_experimental/drivers/i2c/i2cd.c
00002    CubeOS Version 0.4.91 experimental
00003    Copyright (C) 1999,2000 Holger Kenn
00004 
00005    CubeOS is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or any later version.
00009 
00010    CubeOS is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015  */
00016 #include <config.h>
00017 #include <mc68332.h>
00018 #include <cubeos.h>
00019 #include <stdio.h>
00020 #include <flash.h>
00021 
00022 #define WAIT 100000
00023 
00024 
00029 unsigned short FLASH_MID;
00030 unsigned short FLASH_DID;
00031 
00037 int FLASH_init()
00038 {
00039 #ifdef CUBE1
00040 return -1;
00041 #else
00042   int ok=0;
00043   unsigned int csba;
00044   unsigned int csbf;
00045 
00046   printf("EPROM_BASE = %x\n",EPROM_BASE);
00047   printf("checking CSBOOT...\n");
00048   csba = readshort(SIM_CSBARBT);
00049   csbf = readshort(SIM_CSORBT);
00050   printf("CSBARBT = %x,CSORBT=%x\n",csba,csbf);
00051 
00052   FLASH_CSInfo(csba,csbf);
00053 
00054   if (((csba & 0xfff8)<<8)!=EPROM_BASE){
00055         printf("EPROM_BASE!=CSBOOT setting!\n");
00056         return(-1);
00057   }
00058   if (!(csbf&0x1000)) {
00059         printf("setting R/W mode on CSBOOT\n");
00060         writeshort(SIM_CSORBT,csbf|0x1000);
00061         csbf|=0x1000;
00062   } else {
00063         printf("CSBOOT already R/W\n");
00064   }
00065 
00066   printf("reading flash ID\n");
00067   fflush(stdout);
00068 
00069   writeshort(EPROM_BASE+(0x555<<1),0xaa);
00070   writeshort(EPROM_BASE+(0x2aa<<1),0x55);
00071   writeshort(EPROM_BASE+(0x555<<1),0x90); /* Autoselect mode */
00072   FLASH_MID=readshort(EPROM_BASE);
00073   FLASH_DID=readshort(EPROM_BASE+2);
00074   writeshort(EPROM_BASE,0xF0);          /* reset */
00075   printf("Manufacturer ID is %x\n",FLASH_MID);
00076   printf("DEVICE ID is %x\n",FLASH_DID);
00077   switch (FLASH_MID) {
00078         case 0x01:
00079                 printf (" AMD");
00080                 switch(FLASH_DID) {
00081                         case 0x22d6:
00082                                 printf(" AM29F800B Top");
00083                                 ok=1;
00084                                 break;
00085                         case 0x2258:
00086                                 printf(" AM29F800B Bot");
00087                                 ok=1;
00088                                 break;
00089                         default:
00090                                 printf(" UNKNOWN");
00091                 }
00092                 break;
00093         default:
00094                 printf (" UNKNOWN");
00095   }
00096   printf("\n");
00097 
00098 return (ok==1?0:-1);
00099 #endif
00100 }
00101 
00108 int FLASH_bcopy(void * s1, void * s2, size_t n)
00109 {
00110   unsigned long size,addr,j;
00111   unsigned short int tmp1, tmp2;
00112   char ok,i;
00113   return -1;
00114 
00115   LED_ON();
00116   for(j=0;j<WAIT;j++);
00117   LED_OFF();
00118   for(j=0;j<WAIT;j++);
00119 
00120   /* erase chip */
00121   writeshort(EPROM_BASE+(0x555<<1),0xaa);
00122   writeshort(EPROM_BASE+(0x2aa<<1),0x55);
00123   writeshort(EPROM_BASE+(0x555<<1),0x80);
00124   writeshort(EPROM_BASE+(0x555<<1),0xaa);
00125   writeshort(EPROM_BASE+(0x2aa<<1),0x55);
00126   writeshort(EPROM_BASE+(0x555<<1),0x10);
00127   while(~readshort(EPROM_BASE))
00128         {
00129         LED_ON();
00130         LED_OFF();
00131         }
00132 
00133   LED_ON();
00134   for(j=0;j<WAIT;j++);
00135   LED_OFF();
00136   for(j=0;j<WAIT;j++);
00137 
00138   size = n;
00139   addr = 0;
00140 
00141   /* program */
00142   while(addr < size) {
00143     LED_ON();
00144     tmp1 = readshort(s1+addr);
00145     writeshort(EPROM_BASE+(0x555<<1),0xaa);
00146     writeshort(EPROM_BASE+(0x2aa<<1),0x55);
00147     writeshort(EPROM_BASE+(0x555<<1),0xa0);
00148     writeshort(EPROM_BASE+s2+addr,tmp1);
00149     LED_OFF();
00150     ok = 0;
00151     while(!ok) {
00152       tmp2 = readshort(EPROM_BASE+addr);
00153       if ((tmp1 ^ tmp2) & 0x80) {   //not finished
00154         if (tmp2 & 0x20) {          //time out ?
00155           tmp2 = readshort(EPROM_BASE+addr);
00156           if ((tmp1 ^ tmp2) & 0x80) {   //yes!
00157             /*
00158             LED_ON();
00159             while(1);
00160             */
00161             return -1;
00162           }
00163           ok = 1;
00164         }
00165       }
00166       else ok = 1;
00167     }
00168     addr += 2;
00169   }
00170 
00171   LED_ON();
00172   for(j=0;j<WAIT;j++);
00173   LED_OFF();
00174   for(j=0;j<WAIT;j++);
00175 
00176   /* verify */
00177   addr = 0;
00178 
00179   while(addr < size) {
00180     LED_ON();
00181     tmp1 = readshort(s1+addr);
00182     tmp2 = readshort(EPROM_BASE+s2+addr);
00183     LED_OFF();
00184     if(tmp1 ^ tmp2) {  //verify failed
00185       /*LED_ON();
00186       while(1);
00187         */
00188         return (-1);
00189     }
00190     addr += 2;
00191   }
00192 
00193   /* success */
00194   LED_ON();
00195   for(j=0;j<WAIT;j++);
00196   LED_OFF();
00197   for(j=0;j<WAIT;j++);
00198 
00199   return 0;
00200 
00201 }
00202 
00209 int FLASH_CSInfo(unsigned short br,unsigned short or)
00210 {
00211   printf("CS addr = %x ",(br & 0xfff8)<<8);
00212   printf("CS blk = %x : ",(br & 0x0007));
00213   switch(br & 0x0007) {
00214         case 0:
00215                 printf("2k\n");
00216                 break;
00217         case 1:
00218                 printf("8k\n");
00219                 break;
00220         case 2:
00221                 printf("16k\n");
00222                 break;
00223         case 3:
00224                 printf("64k\n");
00225                 break;
00226         case 4:
00227                 printf("128k\n");
00228                 break;
00229         case 5:
00230                 printf("256k\n");
00231                 break;
00232         case 6:
00233                 printf("512k\n");
00234                 break;
00235         case 7:
00236                 printf("1024k\n");
00237                 break;
00238         default:
00239                 printf("???\n");
00240   }
00241 
00242   printf("MODE: %s ",(or&0x8000?"SYNC":"ASYNC"));
00243   printf("BYTE: ");
00244   switch (or&0x6000) {
00245         case 0x0000:
00246                 printf("off ");
00247         case 0x2000:
00248                 printf("low ");
00249         case 0x4000:
00250                 printf("high ");
00251         case 0x6000:
00252                 printf("both ");
00253   }
00254   printf("R/W: ");
00255   switch (or&0x1800) {
00256         case 0x0000:
00257                 printf("rsvd ");
00258                 break;
00259         case 0x0800:
00260                 printf("read ");
00261                 break;
00262         case 0x1000:
00263                 printf("write ");
00264                 break;
00265         case 0x1800:
00266                 printf("both ");
00267                 break;
00268   }
00269   printf("\n");
00270   printf("STRB: %s ",((or&0x0400)?"DS":"AS"));
00271   printf("DSACK: %d ",((or&0x0300)>>6));
00272   printf("SPACE: ");
00273   switch (or&0x0030) {
00274         case 0x0000:
00275                 printf("CPU ");
00276                 break;
00277         case 0x0010:
00278                 printf("USER ");
00279                 break;
00280         case 0x0020:
00281                 printf("SUPV ");
00282                 break;
00283         case 0x0030:
00284                 printf("both ");
00285   }
00286   printf("\n");
00287  return 0;
00288 
00289 }

Generated on Thu Feb 20 15:38:43 2003 for cubeOS by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002