jedec.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /* JEDEC Flash Interface.
  2. * This is an older type of interface for self programming flash. It is
  3. * commonly use in older AMD chips and is obsolete compared with CFI.
  4. * It is called JEDEC because the JEDEC association distributes the ID codes
  5. * for the chips.
  6. *
  7. * See the AMD flash databook for information on how to operate the interface.
  8. *
  9. * This code does not support anything wider than 8 bit flash chips, I am
  10. * not going to guess how to send commands to them, plus I expect they will
  11. * all speak CFI..
  12. *
  13. * $Id: jedec.c,v 1.22 2005/01/05 18:05:11 dwmw2 Exp $
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mtd/jedec.h>
  19. #include <linux/mtd/map.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/compatmac.h>
  22. static struct mtd_info *jedec_probe(struct map_info *);
  23. static int jedec_probe8(struct map_info *map,unsigned long base,
  24. struct jedec_private *priv);
  25. static int jedec_probe16(struct map_info *map,unsigned long base,
  26. struct jedec_private *priv);
  27. static int jedec_probe32(struct map_info *map,unsigned long base,
  28. struct jedec_private *priv);
  29. static void jedec_flash_chip_scan(struct jedec_private *priv,unsigned long start,
  30. unsigned long len);
  31. static int flash_erase(struct mtd_info *mtd, struct erase_info *instr);
  32. static int flash_write(struct mtd_info *mtd, loff_t start, size_t len,
  33. size_t *retlen, const u_char *buf);
  34. static unsigned long my_bank_size;
  35. /* Listing of parts and sizes. We need this table to learn the sector
  36. size of the chip and the total length */
  37. static const struct JEDECTable JEDEC_table[] = {
  38. {
  39. .jedec = 0x013D,
  40. .name = "AMD Am29F017D",
  41. .size = 2*1024*1024,
  42. .sectorsize = 64*1024,
  43. .capabilities = MTD_CAP_NORFLASH
  44. },
  45. {
  46. .jedec = 0x01AD,
  47. .name = "AMD Am29F016",
  48. .size = 2*1024*1024,
  49. .sectorsize = 64*1024,
  50. .capabilities = MTD_CAP_NORFLASH
  51. },
  52. {
  53. .jedec = 0x01D5,
  54. .name = "AMD Am29F080",
  55. .size = 1*1024*1024,
  56. .sectorsize = 64*1024,
  57. .capabilities = MTD_CAP_NORFLASH
  58. },
  59. {
  60. .jedec = 0x01A4,
  61. .name = "AMD Am29F040",
  62. .size = 512*1024,
  63. .sectorsize = 64*1024,
  64. .capabilities = MTD_CAP_NORFLASH
  65. },
  66. {
  67. .jedec = 0x20E3,
  68. .name = "AMD Am29W040B",
  69. .size = 512*1024,
  70. .sectorsize = 64*1024,
  71. .capabilities = MTD_CAP_NORFLASH
  72. },
  73. {
  74. .jedec = 0xC2AD,
  75. .name = "Macronix MX29F016",
  76. .size = 2*1024*1024,
  77. .sectorsize = 64*1024,
  78. .capabilities = MTD_CAP_NORFLASH
  79. },
  80. { .jedec = 0x0 }
  81. };
  82. static const struct JEDECTable *jedec_idtoinf(__u8 mfr,__u8 id);
  83. static void jedec_sync(struct mtd_info *mtd) {};
  84. static int jedec_read(struct mtd_info *mtd, loff_t from, size_t len,
  85. size_t *retlen, u_char *buf);
  86. static int jedec_read_banked(struct mtd_info *mtd, loff_t from, size_t len,
  87. size_t *retlen, u_char *buf);
  88. static struct mtd_info *jedec_probe(struct map_info *map);
  89. static struct mtd_chip_driver jedec_chipdrv = {
  90. .probe = jedec_probe,
  91. .name = "jedec",
  92. .module = THIS_MODULE
  93. };
  94. /* Probe entry point */
  95. static struct mtd_info *jedec_probe(struct map_info *map)
  96. {
  97. struct mtd_info *MTD;
  98. struct jedec_private *priv;
  99. unsigned long Base;
  100. unsigned long SectorSize;
  101. unsigned count;
  102. unsigned I,Uniq;
  103. char Part[200];
  104. memset(&priv,0,sizeof(priv));
  105. MTD = kmalloc(sizeof(struct mtd_info) + sizeof(struct jedec_private), GFP_KERNEL);
  106. if (!MTD)
  107. return NULL;
  108. memset(MTD, 0, sizeof(struct mtd_info) + sizeof(struct jedec_private));
  109. priv = (struct jedec_private *)&MTD[1];
  110. my_bank_size = map->size;
  111. if (map->size/my_bank_size > MAX_JEDEC_CHIPS)
  112. {
  113. printk("mtd: Increase MAX_JEDEC_CHIPS, too many banks.\n");
  114. kfree(MTD);
  115. return NULL;
  116. }
  117. for (Base = 0; Base < map->size; Base += my_bank_size)
  118. {
  119. // Perhaps zero could designate all tests?
  120. if (map->buswidth == 0)
  121. map->buswidth = 1;
  122. if (map->buswidth == 1){
  123. if (jedec_probe8(map,Base,priv) == 0) {
  124. printk("did recognize jedec chip\n");
  125. kfree(MTD);
  126. return NULL;
  127. }
  128. }
  129. if (map->buswidth == 2)
  130. jedec_probe16(map,Base,priv);
  131. if (map->buswidth == 4)
  132. jedec_probe32(map,Base,priv);
  133. }
  134. // Get the biggest sector size
  135. SectorSize = 0;
  136. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  137. {
  138. // printk("priv->chips[%d].jedec is %x\n",I,priv->chips[I].jedec);
  139. // printk("priv->chips[%d].sectorsize is %lx\n",I,priv->chips[I].sectorsize);
  140. if (priv->chips[I].sectorsize > SectorSize)
  141. SectorSize = priv->chips[I].sectorsize;
  142. }
  143. // Quickly ensure that the other sector sizes are factors of the largest
  144. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  145. {
  146. if ((SectorSize/priv->chips[I].sectorsize)*priv->chips[I].sectorsize != SectorSize)
  147. {
  148. printk("mtd: Failed. Device has incompatible mixed sector sizes\n");
  149. kfree(MTD);
  150. return NULL;
  151. }
  152. }
  153. /* Generate a part name that includes the number of different chips and
  154. other configuration information */
  155. count = 1;
  156. strlcpy(Part,map->name,sizeof(Part)-10);
  157. strcat(Part," ");
  158. Uniq = 0;
  159. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  160. {
  161. const struct JEDECTable *JEDEC;
  162. if (priv->chips[I+1].jedec == priv->chips[I].jedec)
  163. {
  164. count++;
  165. continue;
  166. }
  167. // Locate the chip in the jedec table
  168. JEDEC = jedec_idtoinf(priv->chips[I].jedec >> 8,priv->chips[I].jedec);
  169. if (JEDEC == 0)
  170. {
  171. printk("mtd: Internal Error, JEDEC not set\n");
  172. kfree(MTD);
  173. return NULL;
  174. }
  175. if (Uniq != 0)
  176. strcat(Part,",");
  177. Uniq++;
  178. if (count != 1)
  179. sprintf(Part+strlen(Part),"%x*[%s]",count,JEDEC->name);
  180. else
  181. sprintf(Part+strlen(Part),"%s",JEDEC->name);
  182. if (strlen(Part) > sizeof(Part)*2/3)
  183. break;
  184. count = 1;
  185. }
  186. /* Determine if the chips are organized in a linear fashion, or if there
  187. are empty banks. Note, the last bank does not count here, only the
  188. first banks are important. Holes on non-bank boundaries can not exist
  189. due to the way the detection algorithm works. */
  190. if (priv->size < my_bank_size)
  191. my_bank_size = priv->size;
  192. priv->is_banked = 0;
  193. //printk("priv->size is %x, my_bank_size is %x\n",priv->size,my_bank_size);
  194. //printk("priv->bank_fill[0] is %x\n",priv->bank_fill[0]);
  195. if (!priv->size) {
  196. printk("priv->size is zero\n");
  197. kfree(MTD);
  198. return NULL;
  199. }
  200. if (priv->size/my_bank_size) {
  201. if (priv->size/my_bank_size == 1) {
  202. priv->size = my_bank_size;
  203. }
  204. else {
  205. for (I = 0; I != priv->size/my_bank_size - 1; I++)
  206. {
  207. if (priv->bank_fill[I] != my_bank_size)
  208. priv->is_banked = 1;
  209. /* This even could be eliminated, but new de-optimized read/write
  210. functions have to be written */
  211. printk("priv->bank_fill[%d] is %lx, priv->bank_fill[0] is %lx\n",I,priv->bank_fill[I],priv->bank_fill[0]);
  212. if (priv->bank_fill[I] != priv->bank_fill[0])
  213. {
  214. printk("mtd: Failed. Cannot handle unsymmetric banking\n");
  215. kfree(MTD);
  216. return NULL;
  217. }
  218. }
  219. }
  220. }
  221. if (priv->is_banked == 1)
  222. strcat(Part,", banked");
  223. // printk("Part: '%s'\n",Part);
  224. memset(MTD,0,sizeof(*MTD));
  225. // strlcpy(MTD->name,Part,sizeof(MTD->name));
  226. MTD->name = map->name;
  227. MTD->type = MTD_NORFLASH;
  228. MTD->flags = MTD_CAP_NORFLASH;
  229. MTD->erasesize = SectorSize*(map->buswidth);
  230. // printk("MTD->erasesize is %x\n",(unsigned int)MTD->erasesize);
  231. MTD->size = priv->size;
  232. // printk("MTD->size is %x\n",(unsigned int)MTD->size);
  233. //MTD->module = THIS_MODULE; // ? Maybe this should be the low level module?
  234. MTD->erase = flash_erase;
  235. if (priv->is_banked == 1)
  236. MTD->read = jedec_read_banked;
  237. else
  238. MTD->read = jedec_read;
  239. MTD->write = flash_write;
  240. MTD->sync = jedec_sync;
  241. MTD->priv = map;
  242. map->fldrv_priv = priv;
  243. map->fldrv = &jedec_chipdrv;
  244. __module_get(THIS_MODULE);
  245. return MTD;
  246. }
  247. /* Helper for the JEDEC function, JEDEC numbers all have odd parity */
  248. static int checkparity(u_char C)
  249. {
  250. u_char parity = 0;
  251. while (C != 0)
  252. {
  253. parity ^= C & 1;
  254. C >>= 1;
  255. }
  256. return parity == 1;
  257. }
  258. /* Take an array of JEDEC numbers that represent interleved flash chips
  259. and process them. Check to make sure they are good JEDEC numbers, look
  260. them up and then add them to the chip list */
  261. static int handle_jedecs(struct map_info *map,__u8 *Mfg,__u8 *Id,unsigned Count,
  262. unsigned long base,struct jedec_private *priv)
  263. {
  264. unsigned I,J;
  265. unsigned long Size;
  266. unsigned long SectorSize;
  267. const struct JEDECTable *JEDEC;
  268. // Test #2 JEDEC numbers exhibit odd parity
  269. for (I = 0; I != Count; I++)
  270. {
  271. if (checkparity(Mfg[I]) == 0 || checkparity(Id[I]) == 0)
  272. return 0;
  273. }
  274. // Finally, just make sure all the chip sizes are the same
  275. JEDEC = jedec_idtoinf(Mfg[0],Id[0]);
  276. if (JEDEC == 0)
  277. {
  278. printk("mtd: Found JEDEC flash chip, but do not have a table entry for %x:%x\n",Mfg[0],Mfg[1]);
  279. return 0;
  280. }
  281. Size = JEDEC->size;
  282. SectorSize = JEDEC->sectorsize;
  283. for (I = 0; I != Count; I++)
  284. {
  285. JEDEC = jedec_idtoinf(Mfg[0],Id[0]);
  286. if (JEDEC == 0)
  287. {
  288. printk("mtd: Found JEDEC flash chip, but do not have a table entry for %x:%x\n",Mfg[0],Mfg[1]);
  289. return 0;
  290. }
  291. if (Size != JEDEC->size || SectorSize != JEDEC->sectorsize)
  292. {
  293. printk("mtd: Failed. Interleved flash does not have matching characteristics\n");
  294. return 0;
  295. }
  296. }
  297. // Load the Chips
  298. for (I = 0; I != MAX_JEDEC_CHIPS; I++)
  299. {
  300. if (priv->chips[I].jedec == 0)
  301. break;
  302. }
  303. if (I + Count > MAX_JEDEC_CHIPS)
  304. {
  305. printk("mtd: Device has too many chips. Increase MAX_JEDEC_CHIPS\n");
  306. return 0;
  307. }
  308. // Add them to the table
  309. for (J = 0; J != Count; J++)
  310. {
  311. unsigned long Bank;
  312. JEDEC = jedec_idtoinf(Mfg[J],Id[J]);
  313. priv->chips[I].jedec = (Mfg[J] << 8) | Id[J];
  314. priv->chips[I].size = JEDEC->size;
  315. priv->chips[I].sectorsize = JEDEC->sectorsize;
  316. priv->chips[I].base = base + J;
  317. priv->chips[I].datashift = J*8;
  318. priv->chips[I].capabilities = JEDEC->capabilities;
  319. priv->chips[I].offset = priv->size + J;
  320. // log2 n :|
  321. priv->chips[I].addrshift = 0;
  322. for (Bank = Count; Bank != 1; Bank >>= 1, priv->chips[I].addrshift++);
  323. // Determine how filled this bank is.
  324. Bank = base & (~(my_bank_size-1));
  325. if (priv->bank_fill[Bank/my_bank_size] < base +
  326. (JEDEC->size << priv->chips[I].addrshift) - Bank)
  327. priv->bank_fill[Bank/my_bank_size] = base + (JEDEC->size << priv->chips[I].addrshift) - Bank;
  328. I++;
  329. }
  330. priv->size += priv->chips[I-1].size*Count;
  331. return priv->chips[I-1].size;
  332. }
  333. /* Lookup the chip information from the JEDEC ID table. */
  334. static const struct JEDECTable *jedec_idtoinf(__u8 mfr,__u8 id)
  335. {
  336. __u16 Id = (mfr << 8) | id;
  337. unsigned long I = 0;
  338. for (I = 0; JEDEC_table[I].jedec != 0; I++)
  339. if (JEDEC_table[I].jedec == Id)
  340. return JEDEC_table + I;
  341. return NULL;
  342. }
  343. // Look for flash using an 8 bit bus interface
  344. static int jedec_probe8(struct map_info *map,unsigned long base,
  345. struct jedec_private *priv)
  346. {
  347. #define flread(x) map_read8(map,base+x)
  348. #define flwrite(v,x) map_write8(map,v,base+x)
  349. const unsigned long AutoSel1 = 0xAA;
  350. const unsigned long AutoSel2 = 0x55;
  351. const unsigned long AutoSel3 = 0x90;
  352. const unsigned long Reset = 0xF0;
  353. __u32 OldVal;
  354. __u8 Mfg[1];
  355. __u8 Id[1];
  356. unsigned I;
  357. unsigned long Size;
  358. // Wait for any write/erase operation to settle
  359. OldVal = flread(base);
  360. for (I = 0; OldVal != flread(base) && I < 10000; I++)
  361. OldVal = flread(base);
  362. // Reset the chip
  363. flwrite(Reset,0x555);
  364. // Send the sequence
  365. flwrite(AutoSel1,0x555);
  366. flwrite(AutoSel2,0x2AA);
  367. flwrite(AutoSel3,0x555);
  368. // Get the JEDEC numbers
  369. Mfg[0] = flread(0);
  370. Id[0] = flread(1);
  371. // printk("Mfg is %x, Id is %x\n",Mfg[0],Id[0]);
  372. Size = handle_jedecs(map,Mfg,Id,1,base,priv);
  373. // printk("handle_jedecs Size is %x\n",(unsigned int)Size);
  374. if (Size == 0)
  375. {
  376. flwrite(Reset,0x555);
  377. return 0;
  378. }
  379. // Reset.
  380. flwrite(Reset,0x555);
  381. return 1;
  382. #undef flread
  383. #undef flwrite
  384. }
  385. // Look for flash using a 16 bit bus interface (ie 2 8-bit chips)
  386. static int jedec_probe16(struct map_info *map,unsigned long base,
  387. struct jedec_private *priv)
  388. {
  389. return 0;
  390. }
  391. // Look for flash using a 32 bit bus interface (ie 4 8-bit chips)
  392. static int jedec_probe32(struct map_info *map,unsigned long base,
  393. struct jedec_private *priv)
  394. {
  395. #define flread(x) map_read32(map,base+((x)<<2))
  396. #define flwrite(v,x) map_write32(map,v,base+((x)<<2))
  397. const unsigned long AutoSel1 = 0xAAAAAAAA;
  398. const unsigned long AutoSel2 = 0x55555555;
  399. const unsigned long AutoSel3 = 0x90909090;
  400. const unsigned long Reset = 0xF0F0F0F0;
  401. __u32 OldVal;
  402. __u8 Mfg[4];
  403. __u8 Id[4];
  404. unsigned I;
  405. unsigned long Size;
  406. // Wait for any write/erase operation to settle
  407. OldVal = flread(base);
  408. for (I = 0; OldVal != flread(base) && I < 10000; I++)
  409. OldVal = flread(base);
  410. // Reset the chip
  411. flwrite(Reset,0x555);
  412. // Send the sequence
  413. flwrite(AutoSel1,0x555);
  414. flwrite(AutoSel2,0x2AA);
  415. flwrite(AutoSel3,0x555);
  416. // Test #1, JEDEC numbers are readable from 0x??00/0x??01
  417. if (flread(0) != flread(0x100) ||
  418. flread(1) != flread(0x101))
  419. {
  420. flwrite(Reset,0x555);
  421. return 0;
  422. }
  423. // Split up the JEDEC numbers
  424. OldVal = flread(0);
  425. for (I = 0; I != 4; I++)
  426. Mfg[I] = (OldVal >> (I*8));
  427. OldVal = flread(1);
  428. for (I = 0; I != 4; I++)
  429. Id[I] = (OldVal >> (I*8));
  430. Size = handle_jedecs(map,Mfg,Id,4,base,priv);
  431. if (Size == 0)
  432. {
  433. flwrite(Reset,0x555);
  434. return 0;
  435. }
  436. /* Check if there is address wrap around within a single bank, if this
  437. returns JEDEC numbers then we assume that it is wrap around. Notice
  438. we call this routine with the JEDEC return still enabled, if two or
  439. more flashes have a truncated address space the probe test will still
  440. work */
  441. if (base + (Size<<2)+0x555 < map->size &&
  442. base + (Size<<2)+0x555 < (base & (~(my_bank_size-1))) + my_bank_size)
  443. {
  444. if (flread(base+Size) != flread(base+Size + 0x100) ||
  445. flread(base+Size + 1) != flread(base+Size + 0x101))
  446. {
  447. jedec_probe32(map,base+Size,priv);
  448. }
  449. }
  450. // Reset.
  451. flwrite(0xF0F0F0F0,0x555);
  452. return 1;
  453. #undef flread
  454. #undef flwrite
  455. }
  456. /* Linear read. */
  457. static int jedec_read(struct mtd_info *mtd, loff_t from, size_t len,
  458. size_t *retlen, u_char *buf)
  459. {
  460. struct map_info *map = mtd->priv;
  461. map_copy_from(map, buf, from, len);
  462. *retlen = len;
  463. return 0;
  464. }
  465. /* Banked read. Take special care to jump past the holes in the bank
  466. mapping. This version assumes symetry in the holes.. */
  467. static int jedec_read_banked(struct mtd_info *mtd, loff_t from, size_t len,
  468. size_t *retlen, u_char *buf)
  469. {
  470. struct map_info *map = mtd->priv;
  471. struct jedec_private *priv = map->fldrv_priv;
  472. *retlen = 0;
  473. while (len > 0)
  474. {
  475. // Determine what bank and offset into that bank the first byte is
  476. unsigned long bank = from & (~(priv->bank_fill[0]-1));
  477. unsigned long offset = from & (priv->bank_fill[0]-1);
  478. unsigned long get = len;
  479. if (priv->bank_fill[0] - offset < len)
  480. get = priv->bank_fill[0] - offset;
  481. bank /= priv->bank_fill[0];
  482. map_copy_from(map,buf + *retlen,bank*my_bank_size + offset,get);
  483. len -= get;
  484. *retlen += get;
  485. from += get;
  486. }
  487. return 0;
  488. }
  489. /* Pass the flags value that the flash return before it re-entered read
  490. mode. */
  491. static void jedec_flash_failed(unsigned char code)
  492. {
  493. /* Bit 5 being high indicates that there was an internal device
  494. failure, erasure time limits exceeded or something */
  495. if ((code & (1 << 5)) != 0)
  496. {
  497. printk("mtd: Internal Flash failure\n");
  498. return;
  499. }
  500. printk("mtd: Programming didn't take\n");
  501. }
  502. /* This uses the erasure function described in the AMD Flash Handbook,
  503. it will work for flashes with a fixed sector size only. Flashes with
  504. a selection of sector sizes (ie the AMD Am29F800B) will need a different
  505. routine. This routine tries to parallize erasing multiple chips/sectors
  506. where possible */
  507. static int flash_erase(struct mtd_info *mtd, struct erase_info *instr)
  508. {
  509. // Does IO to the currently selected chip
  510. #define flread(x) map_read8(map,chip->base+((x)<<chip->addrshift))
  511. #define flwrite(v,x) map_write8(map,v,chip->base+((x)<<chip->addrshift))
  512. unsigned long Time = 0;
  513. unsigned long NoTime = 0;
  514. unsigned long start = instr->addr, len = instr->len;
  515. unsigned int I;
  516. struct map_info *map = mtd->priv;
  517. struct jedec_private *priv = map->fldrv_priv;
  518. // Verify the arguments..
  519. if (start + len > mtd->size ||
  520. (start % mtd->erasesize) != 0 ||
  521. (len % mtd->erasesize) != 0 ||
  522. (len/mtd->erasesize) == 0)
  523. return -EINVAL;
  524. jedec_flash_chip_scan(priv,start,len);
  525. // Start the erase sequence on each chip
  526. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  527. {
  528. unsigned long off;
  529. struct jedec_flash_chip *chip = priv->chips + I;
  530. if (chip->length == 0)
  531. continue;
  532. if (chip->start + chip->length > chip->size)
  533. {
  534. printk("DIE\n");
  535. return -EIO;
  536. }
  537. flwrite(0xF0,chip->start + 0x555);
  538. flwrite(0xAA,chip->start + 0x555);
  539. flwrite(0x55,chip->start + 0x2AA);
  540. flwrite(0x80,chip->start + 0x555);
  541. flwrite(0xAA,chip->start + 0x555);
  542. flwrite(0x55,chip->start + 0x2AA);
  543. /* Once we start selecting the erase sectors the delay between each
  544. command must not exceed 50us or it will immediately start erasing
  545. and ignore the other sectors */
  546. for (off = 0; off < len; off += chip->sectorsize)
  547. {
  548. // Check to make sure we didn't timeout
  549. flwrite(0x30,chip->start + off);
  550. if (off == 0)
  551. continue;
  552. if ((flread(chip->start + off) & (1 << 3)) != 0)
  553. {
  554. printk("mtd: Ack! We timed out the erase timer!\n");
  555. return -EIO;
  556. }
  557. }
  558. }
  559. /* We could split this into a timer routine and return early, performing
  560. background erasure.. Maybe later if the need warrents */
  561. /* Poll the flash for erasure completion, specs say this can take as long
  562. as 480 seconds to do all the sectors (for a 2 meg flash).
  563. Erasure time is dependent on chip age, temp and wear.. */
  564. /* This being a generic routine assumes a 32 bit bus. It does read32s
  565. and bundles interleved chips into the same grouping. This will work
  566. for all bus widths */
  567. Time = 0;
  568. NoTime = 0;
  569. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  570. {
  571. struct jedec_flash_chip *chip = priv->chips + I;
  572. unsigned long off = 0;
  573. unsigned todo[4] = {0,0,0,0};
  574. unsigned todo_left = 0;
  575. unsigned J;
  576. if (chip->length == 0)
  577. continue;
  578. /* Find all chips in this data line, realistically this is all
  579. or nothing up to the interleve count */
  580. for (J = 0; priv->chips[J].jedec != 0 && J < MAX_JEDEC_CHIPS; J++)
  581. {
  582. if ((priv->chips[J].base & (~((1<<chip->addrshift)-1))) ==
  583. (chip->base & (~((1<<chip->addrshift)-1))))
  584. {
  585. todo_left++;
  586. todo[priv->chips[J].base & ((1<<chip->addrshift)-1)] = 1;
  587. }
  588. }
  589. /* printk("todo: %x %x %x %x\n",(short)todo[0],(short)todo[1],
  590. (short)todo[2],(short)todo[3]);
  591. */
  592. while (1)
  593. {
  594. __u32 Last[4];
  595. unsigned long Count = 0;
  596. /* During erase bit 7 is held low and bit 6 toggles, we watch this,
  597. should it stop toggling or go high then the erase is completed,
  598. or this is not really flash ;> */
  599. switch (map->buswidth) {
  600. case 1:
  601. Last[0] = map_read8(map,(chip->base >> chip->addrshift) + chip->start + off);
  602. Last[1] = map_read8(map,(chip->base >> chip->addrshift) + chip->start + off);
  603. Last[2] = map_read8(map,(chip->base >> chip->addrshift) + chip->start + off);
  604. break;
  605. case 2:
  606. Last[0] = map_read16(map,(chip->base >> chip->addrshift) + chip->start + off);
  607. Last[1] = map_read16(map,(chip->base >> chip->addrshift) + chip->start + off);
  608. Last[2] = map_read16(map,(chip->base >> chip->addrshift) + chip->start + off);
  609. break;
  610. case 3:
  611. Last[0] = map_read32(map,(chip->base >> chip->addrshift) + chip->start + off);
  612. Last[1] = map_read32(map,(chip->base >> chip->addrshift) + chip->start + off);
  613. Last[2] = map_read32(map,(chip->base >> chip->addrshift) + chip->start + off);
  614. break;
  615. }
  616. Count = 3;
  617. while (todo_left != 0)
  618. {
  619. for (J = 0; J != 4; J++)
  620. {
  621. __u8 Byte1 = (Last[(Count-1)%4] >> (J*8)) & 0xFF;
  622. __u8 Byte2 = (Last[(Count-2)%4] >> (J*8)) & 0xFF;
  623. __u8 Byte3 = (Last[(Count-3)%4] >> (J*8)) & 0xFF;
  624. if (todo[J] == 0)
  625. continue;
  626. if ((Byte1 & (1 << 7)) == 0 && Byte1 != Byte2)
  627. {
  628. // printk("Check %x %x %x\n",(short)J,(short)Byte1,(short)Byte2);
  629. continue;
  630. }
  631. if (Byte1 == Byte2)
  632. {
  633. jedec_flash_failed(Byte3);
  634. return -EIO;
  635. }
  636. todo[J] = 0;
  637. todo_left--;
  638. }
  639. /* if (NoTime == 0)
  640. Time += HZ/10 - schedule_timeout(HZ/10);*/
  641. NoTime = 0;
  642. switch (map->buswidth) {
  643. case 1:
  644. Last[Count % 4] = map_read8(map,(chip->base >> chip->addrshift) + chip->start + off);
  645. break;
  646. case 2:
  647. Last[Count % 4] = map_read16(map,(chip->base >> chip->addrshift) + chip->start + off);
  648. break;
  649. case 4:
  650. Last[Count % 4] = map_read32(map,(chip->base >> chip->addrshift) + chip->start + off);
  651. break;
  652. }
  653. Count++;
  654. /* // Count time, max of 15s per sector (according to AMD)
  655. if (Time > 15*len/mtd->erasesize*HZ)
  656. {
  657. printk("mtd: Flash Erase Timed out\n");
  658. return -EIO;
  659. } */
  660. }
  661. // Skip to the next chip if we used chip erase
  662. if (chip->length == chip->size)
  663. off = chip->size;
  664. else
  665. off += chip->sectorsize;
  666. if (off >= chip->length)
  667. break;
  668. NoTime = 1;
  669. }
  670. for (J = 0; priv->chips[J].jedec != 0 && J < MAX_JEDEC_CHIPS; J++)
  671. {
  672. if ((priv->chips[J].base & (~((1<<chip->addrshift)-1))) ==
  673. (chip->base & (~((1<<chip->addrshift)-1))))
  674. priv->chips[J].length = 0;
  675. }
  676. }
  677. //printk("done\n");
  678. instr->state = MTD_ERASE_DONE;
  679. mtd_erase_callback(instr);
  680. return 0;
  681. #undef flread
  682. #undef flwrite
  683. }
  684. /* This is the simple flash writing function. It writes to every byte, in
  685. sequence. It takes care of how to properly address the flash if
  686. the flash is interleved. It can only be used if all the chips in the
  687. array are identical!*/
  688. static int flash_write(struct mtd_info *mtd, loff_t start, size_t len,
  689. size_t *retlen, const u_char *buf)
  690. {
  691. /* Does IO to the currently selected chip. It takes the bank addressing
  692. base (which is divisible by the chip size) adds the necessary lower bits
  693. of addrshift (interleave index) and then adds the control register index. */
  694. #define flread(x) map_read8(map,base+(off&((1<<chip->addrshift)-1))+((x)<<chip->addrshift))
  695. #define flwrite(v,x) map_write8(map,v,base+(off&((1<<chip->addrshift)-1))+((x)<<chip->addrshift))
  696. struct map_info *map = mtd->priv;
  697. struct jedec_private *priv = map->fldrv_priv;
  698. unsigned long base;
  699. unsigned long off;
  700. size_t save_len = len;
  701. if (start + len > mtd->size)
  702. return -EIO;
  703. //printk("Here");
  704. //printk("flash_write: start is %x, len is %x\n",start,(unsigned long)len);
  705. while (len != 0)
  706. {
  707. struct jedec_flash_chip *chip = priv->chips;
  708. unsigned long bank;
  709. unsigned long boffset;
  710. // Compute the base of the flash.
  711. off = ((unsigned long)start) % (chip->size << chip->addrshift);
  712. base = start - off;
  713. // Perform banked addressing translation.
  714. bank = base & (~(priv->bank_fill[0]-1));
  715. boffset = base & (priv->bank_fill[0]-1);
  716. bank = (bank/priv->bank_fill[0])*my_bank_size;
  717. base = bank + boffset;
  718. // printk("Flasing %X %X %X\n",base,chip->size,len);
  719. // printk("off is %x, compare with %x\n",off,chip->size << chip->addrshift);
  720. // Loop over this page
  721. for (; off != (chip->size << chip->addrshift) && len != 0; start++, len--, off++,buf++)
  722. {
  723. unsigned char oldbyte = map_read8(map,base+off);
  724. unsigned char Last[4];
  725. unsigned long Count = 0;
  726. if (oldbyte == *buf) {
  727. // printk("oldbyte and *buf is %x,len is %x\n",oldbyte,len);
  728. continue;
  729. }
  730. if (((~oldbyte) & *buf) != 0)
  731. printk("mtd: warn: Trying to set a 0 to a 1\n");
  732. // Write
  733. flwrite(0xAA,0x555);
  734. flwrite(0x55,0x2AA);
  735. flwrite(0xA0,0x555);
  736. map_write8(map,*buf,base + off);
  737. Last[0] = map_read8(map,base + off);
  738. Last[1] = map_read8(map,base + off);
  739. Last[2] = map_read8(map,base + off);
  740. /* Wait for the flash to finish the operation. We store the last 4
  741. status bytes that have been retrieved so we can determine why
  742. it failed. The toggle bits keep toggling when there is a
  743. failure */
  744. for (Count = 3; Last[(Count - 1) % 4] != Last[(Count - 2) % 4] &&
  745. Count < 10000; Count++)
  746. Last[Count % 4] = map_read8(map,base + off);
  747. if (Last[(Count - 1) % 4] != *buf)
  748. {
  749. jedec_flash_failed(Last[(Count - 3) % 4]);
  750. return -EIO;
  751. }
  752. }
  753. }
  754. *retlen = save_len;
  755. return 0;
  756. }
  757. /* This is used to enhance the speed of the erase routine,
  758. when things are being done to multiple chips it is possible to
  759. parallize the operations, particularly full memory erases of multi
  760. chip memories benifit */
  761. static void jedec_flash_chip_scan(struct jedec_private *priv,unsigned long start,
  762. unsigned long len)
  763. {
  764. unsigned int I;
  765. // Zero the records
  766. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  767. priv->chips[I].start = priv->chips[I].length = 0;
  768. // Intersect the region with each chip
  769. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  770. {
  771. struct jedec_flash_chip *chip = priv->chips + I;
  772. unsigned long ByteStart;
  773. unsigned long ChipEndByte = chip->offset + (chip->size << chip->addrshift);
  774. // End is before this chip or the start is after it
  775. if (start+len < chip->offset ||
  776. ChipEndByte - (1 << chip->addrshift) < start)
  777. continue;
  778. if (start < chip->offset)
  779. {
  780. ByteStart = chip->offset;
  781. chip->start = 0;
  782. }
  783. else
  784. {
  785. chip->start = (start - chip->offset + (1 << chip->addrshift)-1) >> chip->addrshift;
  786. ByteStart = start;
  787. }
  788. if (start + len >= ChipEndByte)
  789. chip->length = (ChipEndByte - ByteStart) >> chip->addrshift;
  790. else
  791. chip->length = (start + len - ByteStart + (1 << chip->addrshift)-1) >> chip->addrshift;
  792. }
  793. }
  794. int __init jedec_init(void)
  795. {
  796. register_mtd_chip_driver(&jedec_chipdrv);
  797. return 0;
  798. }
  799. static void __exit jedec_exit(void)
  800. {
  801. unregister_mtd_chip_driver(&jedec_chipdrv);
  802. }
  803. module_init(jedec_init);
  804. module_exit(jedec_exit);
  805. MODULE_LICENSE("GPL");
  806. MODULE_AUTHOR("Jason Gunthorpe <jgg@deltatee.com> et al.");
  807. MODULE_DESCRIPTION("Old MTD chip driver for JEDEC-compliant flash chips");