jedec.c 27 KB

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