jedec.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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->writesize = 1;
  231. MTD->erasesize = SectorSize*(map->buswidth);
  232. // printk("MTD->erasesize is %x\n",(unsigned int)MTD->erasesize);
  233. MTD->size = priv->size;
  234. // printk("MTD->size is %x\n",(unsigned int)MTD->size);
  235. //MTD->module = THIS_MODULE; // ? Maybe this should be the low level module?
  236. MTD->erase = flash_erase;
  237. if (priv->is_banked == 1)
  238. MTD->read = jedec_read_banked;
  239. else
  240. MTD->read = jedec_read;
  241. MTD->write = flash_write;
  242. MTD->sync = jedec_sync;
  243. MTD->priv = map;
  244. map->fldrv_priv = priv;
  245. map->fldrv = &jedec_chipdrv;
  246. __module_get(THIS_MODULE);
  247. return MTD;
  248. }
  249. /* Helper for the JEDEC function, JEDEC numbers all have odd parity */
  250. static int checkparity(u_char C)
  251. {
  252. u_char parity = 0;
  253. while (C != 0)
  254. {
  255. parity ^= C & 1;
  256. C >>= 1;
  257. }
  258. return parity == 1;
  259. }
  260. /* Take an array of JEDEC numbers that represent interleved flash chips
  261. and process them. Check to make sure they are good JEDEC numbers, look
  262. them up and then add them to the chip list */
  263. static int handle_jedecs(struct map_info *map,__u8 *Mfg,__u8 *Id,unsigned Count,
  264. unsigned long base,struct jedec_private *priv)
  265. {
  266. unsigned I,J;
  267. unsigned long Size;
  268. unsigned long SectorSize;
  269. const struct JEDECTable *JEDEC;
  270. // Test #2 JEDEC numbers exhibit odd parity
  271. for (I = 0; I != Count; I++)
  272. {
  273. if (checkparity(Mfg[I]) == 0 || checkparity(Id[I]) == 0)
  274. return 0;
  275. }
  276. // Finally, just make sure all the chip sizes are the same
  277. JEDEC = jedec_idtoinf(Mfg[0],Id[0]);
  278. if (JEDEC == 0)
  279. {
  280. printk("mtd: Found JEDEC flash chip, but do not have a table entry for %x:%x\n",Mfg[0],Mfg[1]);
  281. return 0;
  282. }
  283. Size = JEDEC->size;
  284. SectorSize = JEDEC->sectorsize;
  285. for (I = 0; I != Count; I++)
  286. {
  287. JEDEC = jedec_idtoinf(Mfg[0],Id[0]);
  288. if (JEDEC == 0)
  289. {
  290. printk("mtd: Found JEDEC flash chip, but do not have a table entry for %x:%x\n",Mfg[0],Mfg[1]);
  291. return 0;
  292. }
  293. if (Size != JEDEC->size || SectorSize != JEDEC->sectorsize)
  294. {
  295. printk("mtd: Failed. Interleved flash does not have matching characteristics\n");
  296. return 0;
  297. }
  298. }
  299. // Load the Chips
  300. for (I = 0; I != MAX_JEDEC_CHIPS; I++)
  301. {
  302. if (priv->chips[I].jedec == 0)
  303. break;
  304. }
  305. if (I + Count > MAX_JEDEC_CHIPS)
  306. {
  307. printk("mtd: Device has too many chips. Increase MAX_JEDEC_CHIPS\n");
  308. return 0;
  309. }
  310. // Add them to the table
  311. for (J = 0; J != Count; J++)
  312. {
  313. unsigned long Bank;
  314. JEDEC = jedec_idtoinf(Mfg[J],Id[J]);
  315. priv->chips[I].jedec = (Mfg[J] << 8) | Id[J];
  316. priv->chips[I].size = JEDEC->size;
  317. priv->chips[I].sectorsize = JEDEC->sectorsize;
  318. priv->chips[I].base = base + J;
  319. priv->chips[I].datashift = J*8;
  320. priv->chips[I].capabilities = JEDEC->capabilities;
  321. priv->chips[I].offset = priv->size + J;
  322. // log2 n :|
  323. priv->chips[I].addrshift = 0;
  324. for (Bank = Count; Bank != 1; Bank >>= 1, priv->chips[I].addrshift++);
  325. // Determine how filled this bank is.
  326. Bank = base & (~(my_bank_size-1));
  327. if (priv->bank_fill[Bank/my_bank_size] < base +
  328. (JEDEC->size << priv->chips[I].addrshift) - Bank)
  329. priv->bank_fill[Bank/my_bank_size] = base + (JEDEC->size << priv->chips[I].addrshift) - Bank;
  330. I++;
  331. }
  332. priv->size += priv->chips[I-1].size*Count;
  333. return priv->chips[I-1].size;
  334. }
  335. /* Lookup the chip information from the JEDEC ID table. */
  336. static const struct JEDECTable *jedec_idtoinf(__u8 mfr,__u8 id)
  337. {
  338. __u16 Id = (mfr << 8) | id;
  339. unsigned long I = 0;
  340. for (I = 0; JEDEC_table[I].jedec != 0; I++)
  341. if (JEDEC_table[I].jedec == Id)
  342. return JEDEC_table + I;
  343. return NULL;
  344. }
  345. // Look for flash using an 8 bit bus interface
  346. static int jedec_probe8(struct map_info *map,unsigned long base,
  347. struct jedec_private *priv)
  348. {
  349. #define flread(x) map_read8(map,base+x)
  350. #define flwrite(v,x) map_write8(map,v,base+x)
  351. const unsigned long AutoSel1 = 0xAA;
  352. const unsigned long AutoSel2 = 0x55;
  353. const unsigned long AutoSel3 = 0x90;
  354. const unsigned long Reset = 0xF0;
  355. __u32 OldVal;
  356. __u8 Mfg[1];
  357. __u8 Id[1];
  358. unsigned I;
  359. unsigned long Size;
  360. // Wait for any write/erase operation to settle
  361. OldVal = flread(base);
  362. for (I = 0; OldVal != flread(base) && I < 10000; I++)
  363. OldVal = flread(base);
  364. // Reset the chip
  365. flwrite(Reset,0x555);
  366. // Send the sequence
  367. flwrite(AutoSel1,0x555);
  368. flwrite(AutoSel2,0x2AA);
  369. flwrite(AutoSel3,0x555);
  370. // Get the JEDEC numbers
  371. Mfg[0] = flread(0);
  372. Id[0] = flread(1);
  373. // printk("Mfg is %x, Id is %x\n",Mfg[0],Id[0]);
  374. Size = handle_jedecs(map,Mfg,Id,1,base,priv);
  375. // printk("handle_jedecs Size is %x\n",(unsigned int)Size);
  376. if (Size == 0)
  377. {
  378. flwrite(Reset,0x555);
  379. return 0;
  380. }
  381. // Reset.
  382. flwrite(Reset,0x555);
  383. return 1;
  384. #undef flread
  385. #undef flwrite
  386. }
  387. // Look for flash using a 16 bit bus interface (ie 2 8-bit chips)
  388. static int jedec_probe16(struct map_info *map,unsigned long base,
  389. struct jedec_private *priv)
  390. {
  391. return 0;
  392. }
  393. // Look for flash using a 32 bit bus interface (ie 4 8-bit chips)
  394. static int jedec_probe32(struct map_info *map,unsigned long base,
  395. struct jedec_private *priv)
  396. {
  397. #define flread(x) map_read32(map,base+((x)<<2))
  398. #define flwrite(v,x) map_write32(map,v,base+((x)<<2))
  399. const unsigned long AutoSel1 = 0xAAAAAAAA;
  400. const unsigned long AutoSel2 = 0x55555555;
  401. const unsigned long AutoSel3 = 0x90909090;
  402. const unsigned long Reset = 0xF0F0F0F0;
  403. __u32 OldVal;
  404. __u8 Mfg[4];
  405. __u8 Id[4];
  406. unsigned I;
  407. unsigned long Size;
  408. // Wait for any write/erase operation to settle
  409. OldVal = flread(base);
  410. for (I = 0; OldVal != flread(base) && I < 10000; I++)
  411. OldVal = flread(base);
  412. // Reset the chip
  413. flwrite(Reset,0x555);
  414. // Send the sequence
  415. flwrite(AutoSel1,0x555);
  416. flwrite(AutoSel2,0x2AA);
  417. flwrite(AutoSel3,0x555);
  418. // Test #1, JEDEC numbers are readable from 0x??00/0x??01
  419. if (flread(0) != flread(0x100) ||
  420. flread(1) != flread(0x101))
  421. {
  422. flwrite(Reset,0x555);
  423. return 0;
  424. }
  425. // Split up the JEDEC numbers
  426. OldVal = flread(0);
  427. for (I = 0; I != 4; I++)
  428. Mfg[I] = (OldVal >> (I*8));
  429. OldVal = flread(1);
  430. for (I = 0; I != 4; I++)
  431. Id[I] = (OldVal >> (I*8));
  432. Size = handle_jedecs(map,Mfg,Id,4,base,priv);
  433. if (Size == 0)
  434. {
  435. flwrite(Reset,0x555);
  436. return 0;
  437. }
  438. /* Check if there is address wrap around within a single bank, if this
  439. returns JEDEC numbers then we assume that it is wrap around. Notice
  440. we call this routine with the JEDEC return still enabled, if two or
  441. more flashes have a truncated address space the probe test will still
  442. work */
  443. if (base + (Size<<2)+0x555 < map->size &&
  444. base + (Size<<2)+0x555 < (base & (~(my_bank_size-1))) + my_bank_size)
  445. {
  446. if (flread(base+Size) != flread(base+Size + 0x100) ||
  447. flread(base+Size + 1) != flread(base+Size + 0x101))
  448. {
  449. jedec_probe32(map,base+Size,priv);
  450. }
  451. }
  452. // Reset.
  453. flwrite(0xF0F0F0F0,0x555);
  454. return 1;
  455. #undef flread
  456. #undef flwrite
  457. }
  458. /* Linear read. */
  459. static int jedec_read(struct mtd_info *mtd, loff_t from, size_t len,
  460. size_t *retlen, u_char *buf)
  461. {
  462. struct map_info *map = mtd->priv;
  463. map_copy_from(map, buf, from, len);
  464. *retlen = len;
  465. return 0;
  466. }
  467. /* Banked read. Take special care to jump past the holes in the bank
  468. mapping. This version assumes symetry in the holes.. */
  469. static int jedec_read_banked(struct mtd_info *mtd, loff_t from, size_t len,
  470. size_t *retlen, u_char *buf)
  471. {
  472. struct map_info *map = mtd->priv;
  473. struct jedec_private *priv = map->fldrv_priv;
  474. *retlen = 0;
  475. while (len > 0)
  476. {
  477. // Determine what bank and offset into that bank the first byte is
  478. unsigned long bank = from & (~(priv->bank_fill[0]-1));
  479. unsigned long offset = from & (priv->bank_fill[0]-1);
  480. unsigned long get = len;
  481. if (priv->bank_fill[0] - offset < len)
  482. get = priv->bank_fill[0] - offset;
  483. bank /= priv->bank_fill[0];
  484. map_copy_from(map,buf + *retlen,bank*my_bank_size + offset,get);
  485. len -= get;
  486. *retlen += get;
  487. from += get;
  488. }
  489. return 0;
  490. }
  491. /* Pass the flags value that the flash return before it re-entered read
  492. mode. */
  493. static void jedec_flash_failed(unsigned char code)
  494. {
  495. /* Bit 5 being high indicates that there was an internal device
  496. failure, erasure time limits exceeded or something */
  497. if ((code & (1 << 5)) != 0)
  498. {
  499. printk("mtd: Internal Flash failure\n");
  500. return;
  501. }
  502. printk("mtd: Programming didn't take\n");
  503. }
  504. /* This uses the erasure function described in the AMD Flash Handbook,
  505. it will work for flashes with a fixed sector size only. Flashes with
  506. a selection of sector sizes (ie the AMD Am29F800B) will need a different
  507. routine. This routine tries to parallize erasing multiple chips/sectors
  508. where possible */
  509. static int flash_erase(struct mtd_info *mtd, struct erase_info *instr)
  510. {
  511. // Does IO to the currently selected chip
  512. #define flread(x) map_read8(map,chip->base+((x)<<chip->addrshift))
  513. #define flwrite(v,x) map_write8(map,v,chip->base+((x)<<chip->addrshift))
  514. unsigned long Time = 0;
  515. unsigned long NoTime = 0;
  516. unsigned long start = instr->addr, len = instr->len;
  517. unsigned int I;
  518. struct map_info *map = mtd->priv;
  519. struct jedec_private *priv = map->fldrv_priv;
  520. // Verify the arguments..
  521. if (start + len > mtd->size ||
  522. (start % mtd->erasesize) != 0 ||
  523. (len % mtd->erasesize) != 0 ||
  524. (len/mtd->erasesize) == 0)
  525. return -EINVAL;
  526. jedec_flash_chip_scan(priv,start,len);
  527. // Start the erase sequence on each chip
  528. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  529. {
  530. unsigned long off;
  531. struct jedec_flash_chip *chip = priv->chips + I;
  532. if (chip->length == 0)
  533. continue;
  534. if (chip->start + chip->length > chip->size)
  535. {
  536. printk("DIE\n");
  537. return -EIO;
  538. }
  539. flwrite(0xF0,chip->start + 0x555);
  540. flwrite(0xAA,chip->start + 0x555);
  541. flwrite(0x55,chip->start + 0x2AA);
  542. flwrite(0x80,chip->start + 0x555);
  543. flwrite(0xAA,chip->start + 0x555);
  544. flwrite(0x55,chip->start + 0x2AA);
  545. /* Once we start selecting the erase sectors the delay between each
  546. command must not exceed 50us or it will immediately start erasing
  547. and ignore the other sectors */
  548. for (off = 0; off < len; off += chip->sectorsize)
  549. {
  550. // Check to make sure we didn't timeout
  551. flwrite(0x30,chip->start + off);
  552. if (off == 0)
  553. continue;
  554. if ((flread(chip->start + off) & (1 << 3)) != 0)
  555. {
  556. printk("mtd: Ack! We timed out the erase timer!\n");
  557. return -EIO;
  558. }
  559. }
  560. }
  561. /* We could split this into a timer routine and return early, performing
  562. background erasure.. Maybe later if the need warrents */
  563. /* Poll the flash for erasure completion, specs say this can take as long
  564. as 480 seconds to do all the sectors (for a 2 meg flash).
  565. Erasure time is dependent on chip age, temp and wear.. */
  566. /* This being a generic routine assumes a 32 bit bus. It does read32s
  567. and bundles interleved chips into the same grouping. This will work
  568. for all bus widths */
  569. Time = 0;
  570. NoTime = 0;
  571. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  572. {
  573. struct jedec_flash_chip *chip = priv->chips + I;
  574. unsigned long off = 0;
  575. unsigned todo[4] = {0,0,0,0};
  576. unsigned todo_left = 0;
  577. unsigned J;
  578. if (chip->length == 0)
  579. continue;
  580. /* Find all chips in this data line, realistically this is all
  581. or nothing up to the interleve count */
  582. for (J = 0; priv->chips[J].jedec != 0 && J < MAX_JEDEC_CHIPS; J++)
  583. {
  584. if ((priv->chips[J].base & (~((1<<chip->addrshift)-1))) ==
  585. (chip->base & (~((1<<chip->addrshift)-1))))
  586. {
  587. todo_left++;
  588. todo[priv->chips[J].base & ((1<<chip->addrshift)-1)] = 1;
  589. }
  590. }
  591. /* printk("todo: %x %x %x %x\n",(short)todo[0],(short)todo[1],
  592. (short)todo[2],(short)todo[3]);
  593. */
  594. while (1)
  595. {
  596. __u32 Last[4];
  597. unsigned long Count = 0;
  598. /* During erase bit 7 is held low and bit 6 toggles, we watch this,
  599. should it stop toggling or go high then the erase is completed,
  600. or this is not really flash ;> */
  601. switch (map->buswidth) {
  602. case 1:
  603. Last[0] = map_read8(map,(chip->base >> chip->addrshift) + chip->start + off);
  604. Last[1] = map_read8(map,(chip->base >> chip->addrshift) + chip->start + off);
  605. Last[2] = map_read8(map,(chip->base >> chip->addrshift) + chip->start + off);
  606. break;
  607. case 2:
  608. Last[0] = map_read16(map,(chip->base >> chip->addrshift) + chip->start + off);
  609. Last[1] = map_read16(map,(chip->base >> chip->addrshift) + chip->start + off);
  610. Last[2] = map_read16(map,(chip->base >> chip->addrshift) + chip->start + off);
  611. break;
  612. case 3:
  613. Last[0] = map_read32(map,(chip->base >> chip->addrshift) + chip->start + off);
  614. Last[1] = map_read32(map,(chip->base >> chip->addrshift) + chip->start + off);
  615. Last[2] = map_read32(map,(chip->base >> chip->addrshift) + chip->start + off);
  616. break;
  617. }
  618. Count = 3;
  619. while (todo_left != 0)
  620. {
  621. for (J = 0; J != 4; J++)
  622. {
  623. __u8 Byte1 = (Last[(Count-1)%4] >> (J*8)) & 0xFF;
  624. __u8 Byte2 = (Last[(Count-2)%4] >> (J*8)) & 0xFF;
  625. __u8 Byte3 = (Last[(Count-3)%4] >> (J*8)) & 0xFF;
  626. if (todo[J] == 0)
  627. continue;
  628. if ((Byte1 & (1 << 7)) == 0 && Byte1 != Byte2)
  629. {
  630. // printk("Check %x %x %x\n",(short)J,(short)Byte1,(short)Byte2);
  631. continue;
  632. }
  633. if (Byte1 == Byte2)
  634. {
  635. jedec_flash_failed(Byte3);
  636. return -EIO;
  637. }
  638. todo[J] = 0;
  639. todo_left--;
  640. }
  641. /* if (NoTime == 0)
  642. Time += HZ/10 - schedule_timeout(HZ/10);*/
  643. NoTime = 0;
  644. switch (map->buswidth) {
  645. case 1:
  646. Last[Count % 4] = map_read8(map,(chip->base >> chip->addrshift) + chip->start + off);
  647. break;
  648. case 2:
  649. Last[Count % 4] = map_read16(map,(chip->base >> chip->addrshift) + chip->start + off);
  650. break;
  651. case 4:
  652. Last[Count % 4] = map_read32(map,(chip->base >> chip->addrshift) + chip->start + off);
  653. break;
  654. }
  655. Count++;
  656. /* // Count time, max of 15s per sector (according to AMD)
  657. if (Time > 15*len/mtd->erasesize*HZ)
  658. {
  659. printk("mtd: Flash Erase Timed out\n");
  660. return -EIO;
  661. } */
  662. }
  663. // Skip to the next chip if we used chip erase
  664. if (chip->length == chip->size)
  665. off = chip->size;
  666. else
  667. off += chip->sectorsize;
  668. if (off >= chip->length)
  669. break;
  670. NoTime = 1;
  671. }
  672. for (J = 0; priv->chips[J].jedec != 0 && J < MAX_JEDEC_CHIPS; J++)
  673. {
  674. if ((priv->chips[J].base & (~((1<<chip->addrshift)-1))) ==
  675. (chip->base & (~((1<<chip->addrshift)-1))))
  676. priv->chips[J].length = 0;
  677. }
  678. }
  679. //printk("done\n");
  680. instr->state = MTD_ERASE_DONE;
  681. mtd_erase_callback(instr);
  682. return 0;
  683. #undef flread
  684. #undef flwrite
  685. }
  686. /* This is the simple flash writing function. It writes to every byte, in
  687. sequence. It takes care of how to properly address the flash if
  688. the flash is interleved. It can only be used if all the chips in the
  689. array are identical!*/
  690. static int flash_write(struct mtd_info *mtd, loff_t start, size_t len,
  691. size_t *retlen, const u_char *buf)
  692. {
  693. /* Does IO to the currently selected chip. It takes the bank addressing
  694. base (which is divisible by the chip size) adds the necessary lower bits
  695. of addrshift (interleave index) and then adds the control register index. */
  696. #define flread(x) map_read8(map,base+(off&((1<<chip->addrshift)-1))+((x)<<chip->addrshift))
  697. #define flwrite(v,x) map_write8(map,v,base+(off&((1<<chip->addrshift)-1))+((x)<<chip->addrshift))
  698. struct map_info *map = mtd->priv;
  699. struct jedec_private *priv = map->fldrv_priv;
  700. unsigned long base;
  701. unsigned long off;
  702. size_t save_len = len;
  703. if (start + len > mtd->size)
  704. return -EIO;
  705. //printk("Here");
  706. //printk("flash_write: start is %x, len is %x\n",start,(unsigned long)len);
  707. while (len != 0)
  708. {
  709. struct jedec_flash_chip *chip = priv->chips;
  710. unsigned long bank;
  711. unsigned long boffset;
  712. // Compute the base of the flash.
  713. off = ((unsigned long)start) % (chip->size << chip->addrshift);
  714. base = start - off;
  715. // Perform banked addressing translation.
  716. bank = base & (~(priv->bank_fill[0]-1));
  717. boffset = base & (priv->bank_fill[0]-1);
  718. bank = (bank/priv->bank_fill[0])*my_bank_size;
  719. base = bank + boffset;
  720. // printk("Flasing %X %X %X\n",base,chip->size,len);
  721. // printk("off is %x, compare with %x\n",off,chip->size << chip->addrshift);
  722. // Loop over this page
  723. for (; off != (chip->size << chip->addrshift) && len != 0; start++, len--, off++,buf++)
  724. {
  725. unsigned char oldbyte = map_read8(map,base+off);
  726. unsigned char Last[4];
  727. unsigned long Count = 0;
  728. if (oldbyte == *buf) {
  729. // printk("oldbyte and *buf is %x,len is %x\n",oldbyte,len);
  730. continue;
  731. }
  732. if (((~oldbyte) & *buf) != 0)
  733. printk("mtd: warn: Trying to set a 0 to a 1\n");
  734. // Write
  735. flwrite(0xAA,0x555);
  736. flwrite(0x55,0x2AA);
  737. flwrite(0xA0,0x555);
  738. map_write8(map,*buf,base + off);
  739. Last[0] = map_read8(map,base + off);
  740. Last[1] = map_read8(map,base + off);
  741. Last[2] = map_read8(map,base + off);
  742. /* Wait for the flash to finish the operation. We store the last 4
  743. status bytes that have been retrieved so we can determine why
  744. it failed. The toggle bits keep toggling when there is a
  745. failure */
  746. for (Count = 3; Last[(Count - 1) % 4] != Last[(Count - 2) % 4] &&
  747. Count < 10000; Count++)
  748. Last[Count % 4] = map_read8(map,base + off);
  749. if (Last[(Count - 1) % 4] != *buf)
  750. {
  751. jedec_flash_failed(Last[(Count - 3) % 4]);
  752. return -EIO;
  753. }
  754. }
  755. }
  756. *retlen = save_len;
  757. return 0;
  758. }
  759. /* This is used to enhance the speed of the erase routine,
  760. when things are being done to multiple chips it is possible to
  761. parallize the operations, particularly full memory erases of multi
  762. chip memories benifit */
  763. static void jedec_flash_chip_scan(struct jedec_private *priv,unsigned long start,
  764. unsigned long len)
  765. {
  766. unsigned int I;
  767. // Zero the records
  768. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  769. priv->chips[I].start = priv->chips[I].length = 0;
  770. // Intersect the region with each chip
  771. for (I = 0; priv->chips[I].jedec != 0 && I < MAX_JEDEC_CHIPS; I++)
  772. {
  773. struct jedec_flash_chip *chip = priv->chips + I;
  774. unsigned long ByteStart;
  775. unsigned long ChipEndByte = chip->offset + (chip->size << chip->addrshift);
  776. // End is before this chip or the start is after it
  777. if (start+len < chip->offset ||
  778. ChipEndByte - (1 << chip->addrshift) < start)
  779. continue;
  780. if (start < chip->offset)
  781. {
  782. ByteStart = chip->offset;
  783. chip->start = 0;
  784. }
  785. else
  786. {
  787. chip->start = (start - chip->offset + (1 << chip->addrshift)-1) >> chip->addrshift;
  788. ByteStart = start;
  789. }
  790. if (start + len >= ChipEndByte)
  791. chip->length = (ChipEndByte - ByteStart) >> chip->addrshift;
  792. else
  793. chip->length = (start + len - ByteStart + (1 << chip->addrshift)-1) >> chip->addrshift;
  794. }
  795. }
  796. int __init jedec_init(void)
  797. {
  798. register_mtd_chip_driver(&jedec_chipdrv);
  799. return 0;
  800. }
  801. static void __exit jedec_exit(void)
  802. {
  803. unregister_mtd_chip_driver(&jedec_chipdrv);
  804. }
  805. module_init(jedec_init);
  806. module_exit(jedec_exit);
  807. MODULE_LICENSE("GPL");
  808. MODULE_AUTHOR("Jason Gunthorpe <jgg@deltatee.com> et al.");
  809. MODULE_DESCRIPTION("Old MTD chip driver for JEDEC-compliant flash chips");