cmd_doc.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. /*
  2. * Driver for Disk-On-Chip 2000 and Millennium
  3. * (c) 1999 Machine Vision Holdings, Inc.
  4. * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
  5. *
  6. * $Id: doc2000.c,v 1.46 2001/10/02 15:05:13 dwmw2 Exp $
  7. */
  8. #include <common.h>
  9. #include <config.h>
  10. #include <command.h>
  11. #include <malloc.h>
  12. #include <asm/io.h>
  13. #include <linux/mtd/nftl.h>
  14. #include <linux/mtd/doc2000.h>
  15. #error This code is broken and will be removed outright in the next release.
  16. #error If you need diskonchip support, please update the Linux driver in
  17. #error drivers/mtd/nand/diskonchip.c to work with u-boot.
  18. /*
  19. * ! BROKEN !
  20. *
  21. * TODO: must be implemented and tested by someone with HW
  22. */
  23. #if 0
  24. #ifdef CONFIG_SYS_DOC_SUPPORT_2000
  25. #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
  26. #else
  27. #define DoC_is_2000(doc) (0)
  28. #endif
  29. #ifdef CONFIG_SYS_DOC_SUPPORT_MILLENNIUM
  30. #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
  31. #else
  32. #define DoC_is_Millennium(doc) (0)
  33. #endif
  34. /* CONFIG_SYS_DOC_PASSIVE_PROBE:
  35. In order to ensure that the BIOS checksum is correct at boot time, and
  36. hence that the onboard BIOS extension gets executed, the DiskOnChip
  37. goes into reset mode when it is read sequentially: all registers
  38. return 0xff until the chip is woken up again by writing to the
  39. DOCControl register.
  40. Unfortunately, this means that the probe for the DiskOnChip is unsafe,
  41. because one of the first things it does is write to where it thinks
  42. the DOCControl register should be - which may well be shared memory
  43. for another device. I've had machines which lock up when this is
  44. attempted. Hence the possibility to do a passive probe, which will fail
  45. to detect a chip in reset mode, but is at least guaranteed not to lock
  46. the machine.
  47. If you have this problem, uncomment the following line:
  48. #define CONFIG_SYS_DOC_PASSIVE_PROBE
  49. */
  50. #undef DOC_DEBUG
  51. #undef ECC_DEBUG
  52. #undef PSYCHO_DEBUG
  53. #undef NFTL_DEBUG
  54. static struct DiskOnChip doc_dev_desc[CONFIG_SYS_MAX_DOC_DEVICE];
  55. /* Current DOC Device */
  56. static int curr_device = -1;
  57. /* Supported NAND flash devices */
  58. static struct nand_flash_dev nand_flash_ids[] = {
  59. {"Toshiba TC5816BDC", NAND_MFR_TOSHIBA, 0x64, 21, 1, 2, 0x1000, 0},
  60. {"Toshiba TC5832DC", NAND_MFR_TOSHIBA, 0x6b, 22, 0, 2, 0x2000, 0},
  61. {"Toshiba TH58V128DC", NAND_MFR_TOSHIBA, 0x73, 24, 0, 2, 0x4000, 0},
  62. {"Toshiba TC58256FT/DC", NAND_MFR_TOSHIBA, 0x75, 25, 0, 2, 0x4000, 0},
  63. {"Toshiba TH58512FT", NAND_MFR_TOSHIBA, 0x76, 26, 0, 3, 0x4000, 0},
  64. {"Toshiba TC58V32DC", NAND_MFR_TOSHIBA, 0xe5, 22, 0, 2, 0x2000, 0},
  65. {"Toshiba TC58V64AFT/DC", NAND_MFR_TOSHIBA, 0xe6, 23, 0, 2, 0x2000, 0},
  66. {"Toshiba TC58V16BDC", NAND_MFR_TOSHIBA, 0xea, 21, 1, 2, 0x1000, 0},
  67. {"Toshiba TH58100FT", NAND_MFR_TOSHIBA, 0x79, 27, 0, 3, 0x4000, 0},
  68. {"Samsung KM29N16000", NAND_MFR_SAMSUNG, 0x64, 21, 1, 2, 0x1000, 0},
  69. {"Samsung unknown 4Mb", NAND_MFR_SAMSUNG, 0x6b, 22, 0, 2, 0x2000, 0},
  70. {"Samsung KM29U128T", NAND_MFR_SAMSUNG, 0x73, 24, 0, 2, 0x4000, 0},
  71. {"Samsung KM29U256T", NAND_MFR_SAMSUNG, 0x75, 25, 0, 2, 0x4000, 0},
  72. {"Samsung unknown 64Mb", NAND_MFR_SAMSUNG, 0x76, 26, 0, 3, 0x4000, 0},
  73. {"Samsung KM29W32000", NAND_MFR_SAMSUNG, 0xe3, 22, 0, 2, 0x2000, 0},
  74. {"Samsung unknown 4Mb", NAND_MFR_SAMSUNG, 0xe5, 22, 0, 2, 0x2000, 0},
  75. {"Samsung KM29U64000", NAND_MFR_SAMSUNG, 0xe6, 23, 0, 2, 0x2000, 0},
  76. {"Samsung KM29W16000", NAND_MFR_SAMSUNG, 0xea, 21, 1, 2, 0x1000, 0},
  77. {"Samsung K9F5616Q0C", NAND_MFR_SAMSUNG, 0x45, 25, 0, 2, 0x4000, 1},
  78. {"Samsung K9K1216Q0C", NAND_MFR_SAMSUNG, 0x46, 26, 0, 3, 0x4000, 1},
  79. {"Samsung K9F1G08U0M", NAND_MFR_SAMSUNG, 0xf1, 27, 0, 2, 0, 0},
  80. {NULL,}
  81. };
  82. /* ------------------------------------------------------------------------- */
  83. int do_doc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  84. {
  85. int rcode = 0;
  86. switch (argc) {
  87. case 0:
  88. case 1:
  89. cmd_usage(cmdtp);
  90. return 1;
  91. case 2:
  92. if (strcmp(argv[1],"info") == 0) {
  93. int i;
  94. putc ('\n');
  95. for (i=0; i<CONFIG_SYS_MAX_DOC_DEVICE; ++i) {
  96. if(doc_dev_desc[i].ChipID == DOC_ChipID_UNKNOWN)
  97. continue; /* list only known devices */
  98. printf ("Device %d: ", i);
  99. doc_print(&doc_dev_desc[i]);
  100. }
  101. return 0;
  102. } else if (strcmp(argv[1],"device") == 0) {
  103. if ((curr_device < 0) || (curr_device >= CONFIG_SYS_MAX_DOC_DEVICE)) {
  104. puts ("\nno devices available\n");
  105. return 1;
  106. }
  107. printf ("\nDevice %d: ", curr_device);
  108. doc_print(&doc_dev_desc[curr_device]);
  109. return 0;
  110. }
  111. cmd_usage(cmdtp);
  112. return 1;
  113. case 3:
  114. if (strcmp(argv[1],"device") == 0) {
  115. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  116. printf ("\nDevice %d: ", dev);
  117. if (dev >= CONFIG_SYS_MAX_DOC_DEVICE) {
  118. puts ("unknown device\n");
  119. return 1;
  120. }
  121. doc_print(&doc_dev_desc[dev]);
  122. /*doc_print (dev);*/
  123. if (doc_dev_desc[dev].ChipID == DOC_ChipID_UNKNOWN) {
  124. return 1;
  125. }
  126. curr_device = dev;
  127. puts ("... is now current device\n");
  128. return 0;
  129. }
  130. cmd_usage(cmdtp);
  131. return 1;
  132. default:
  133. /* at least 4 args */
  134. if (strcmp(argv[1],"read") == 0 || strcmp(argv[1],"write") == 0) {
  135. ulong addr = simple_strtoul(argv[2], NULL, 16);
  136. ulong off = simple_strtoul(argv[3], NULL, 16);
  137. ulong size = simple_strtoul(argv[4], NULL, 16);
  138. int cmd = (strcmp(argv[1],"read") == 0);
  139. int ret, total;
  140. printf ("\nDOC %s: device %d offset %ld, size %ld ... ",
  141. cmd ? "read" : "write", curr_device, off, size);
  142. ret = doc_rw(doc_dev_desc + curr_device, cmd, off, size,
  143. (size_t *)&total, (u_char*)addr);
  144. printf ("%d bytes %s: %s\n", total, cmd ? "read" : "write",
  145. ret ? "ERROR" : "OK");
  146. return ret;
  147. } else if (strcmp(argv[1],"erase") == 0) {
  148. ulong off = simple_strtoul(argv[2], NULL, 16);
  149. ulong size = simple_strtoul(argv[3], NULL, 16);
  150. int ret;
  151. printf ("\nDOC erase: device %d offset %ld, size %ld ... ",
  152. curr_device, off, size);
  153. ret = doc_erase (doc_dev_desc + curr_device, off, size);
  154. printf("%s\n", ret ? "ERROR" : "OK");
  155. return ret;
  156. } else {
  157. cmd_usage(cmdtp);
  158. rcode = 1;
  159. }
  160. return rcode;
  161. }
  162. }
  163. U_BOOT_CMD(
  164. doc, 5, 1, do_doc,
  165. "Disk-On-Chip sub-system",
  166. "info - show available DOC devices\n"
  167. "doc device [dev] - show or set current device\n"
  168. "doc read addr off size\n"
  169. "doc write addr off size - read/write `size'"
  170. " bytes starting at offset `off'\n"
  171. " to/from memory address `addr'\n"
  172. "doc erase off size - erase `size' bytes of DOC from offset `off'"
  173. );
  174. int do_docboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  175. {
  176. char *boot_device = NULL;
  177. char *ep;
  178. int dev;
  179. ulong cnt;
  180. ulong addr;
  181. ulong offset = 0;
  182. image_header_t *hdr;
  183. int rcode = 0;
  184. #if defined(CONFIG_FIT)
  185. const void *fit_hdr = NULL;
  186. #endif
  187. show_boot_progress (34);
  188. switch (argc) {
  189. case 1:
  190. addr = CONFIG_SYS_LOAD_ADDR;
  191. boot_device = getenv ("bootdevice");
  192. break;
  193. case 2:
  194. addr = simple_strtoul(argv[1], NULL, 16);
  195. boot_device = getenv ("bootdevice");
  196. break;
  197. case 3:
  198. addr = simple_strtoul(argv[1], NULL, 16);
  199. boot_device = argv[2];
  200. break;
  201. case 4:
  202. addr = simple_strtoul(argv[1], NULL, 16);
  203. boot_device = argv[2];
  204. offset = simple_strtoul(argv[3], NULL, 16);
  205. break;
  206. default:
  207. cmd_usage(cmdtp);
  208. show_boot_progress (-35);
  209. return 1;
  210. }
  211. show_boot_progress (35);
  212. if (!boot_device) {
  213. puts ("\n** No boot device **\n");
  214. show_boot_progress (-36);
  215. return 1;
  216. }
  217. show_boot_progress (36);
  218. dev = simple_strtoul(boot_device, &ep, 16);
  219. if ((dev >= CONFIG_SYS_MAX_DOC_DEVICE) ||
  220. (doc_dev_desc[dev].ChipID == DOC_ChipID_UNKNOWN)) {
  221. printf ("\n** Device %d not available\n", dev);
  222. show_boot_progress (-37);
  223. return 1;
  224. }
  225. show_boot_progress (37);
  226. printf ("\nLoading from device %d: %s at 0x%lX (offset 0x%lX)\n",
  227. dev, doc_dev_desc[dev].name, doc_dev_desc[dev].physadr,
  228. offset);
  229. if (doc_rw (doc_dev_desc + dev, 1, offset,
  230. SECTORSIZE, NULL, (u_char *)addr)) {
  231. printf ("** Read error on %d\n", dev);
  232. show_boot_progress (-38);
  233. return 1;
  234. }
  235. show_boot_progress (38);
  236. switch (genimg_get_format ((void *)addr)) {
  237. case IMAGE_FORMAT_LEGACY:
  238. hdr = (image_header_t *)addr;
  239. image_print_contents (hdr);
  240. cnt = image_get_image_size (hdr);
  241. break;
  242. #if defined(CONFIG_FIT)
  243. case IMAGE_FORMAT_FIT:
  244. fit_hdr = (const void *)addr;
  245. puts ("Fit image detected...\n");
  246. cnt = fit_get_size (fit_hdr);
  247. break;
  248. #endif
  249. default:
  250. show_boot_progress (-39);
  251. puts ("** Unknown image type\n");
  252. return 1;
  253. }
  254. show_boot_progress (39);
  255. cnt -= SECTORSIZE;
  256. if (doc_rw (doc_dev_desc + dev, 1, offset + SECTORSIZE, cnt,
  257. NULL, (u_char *)(addr+SECTORSIZE))) {
  258. printf ("** Read error on %d\n", dev);
  259. show_boot_progress (-40);
  260. return 1;
  261. }
  262. show_boot_progress (40);
  263. #if defined(CONFIG_FIT)
  264. /* This cannot be done earlier, we need complete FIT image in RAM first */
  265. if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
  266. if (!fit_check_format (fit_hdr)) {
  267. show_boot_progress (-130);
  268. puts ("** Bad FIT image format\n");
  269. return 1;
  270. }
  271. show_boot_progress (131);
  272. fit_print_contents (fit_hdr);
  273. }
  274. #endif
  275. /* Loading ok, update default load address */
  276. load_addr = addr;
  277. /* Check if we should attempt an auto-start */
  278. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
  279. char *local_args[2];
  280. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  281. local_args[0] = argv[0];
  282. local_args[1] = NULL;
  283. printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
  284. do_bootm (cmdtp, 0, 1, local_args);
  285. rcode = 1;
  286. }
  287. return rcode;
  288. }
  289. U_BOOT_CMD(
  290. docboot, 4, 1, do_docboot,
  291. "boot from DOC device",
  292. "loadAddr dev"
  293. );
  294. int doc_rw (struct DiskOnChip* this, int cmd,
  295. loff_t from, size_t len,
  296. size_t * retlen, u_char * buf)
  297. {
  298. int noecc, ret = 0, n, total = 0;
  299. char eccbuf[6];
  300. while(len) {
  301. /* The ECC will not be calculated correctly if
  302. less than 512 is written or read */
  303. noecc = (from != (from | 0x1ff) + 1) || (len < 0x200);
  304. if (cmd)
  305. ret = doc_read_ecc(this, from, len,
  306. (size_t *)&n, (u_char*)buf,
  307. noecc ? (uchar *)NULL : (uchar *)eccbuf);
  308. else
  309. ret = doc_write_ecc(this, from, len,
  310. (size_t *)&n, (u_char*)buf,
  311. noecc ? (uchar *)NULL : (uchar *)eccbuf);
  312. if (ret)
  313. break;
  314. from += n;
  315. buf += n;
  316. total += n;
  317. len -= n;
  318. }
  319. if (retlen)
  320. *retlen = total;
  321. return ret;
  322. }
  323. void doc_print(struct DiskOnChip *this) {
  324. printf("%s at 0x%lX,\n"
  325. "\t %d chip%s %s, size %d MB, \n"
  326. "\t total size %ld MB, sector size %ld kB\n",
  327. this->name, this->physadr, this->numchips,
  328. this->numchips>1 ? "s" : "", this->chips_name,
  329. 1 << (this->chipshift - 20),
  330. this->totlen >> 20, this->erasesize >> 10);
  331. if (this->nftl_found) {
  332. struct NFTLrecord *nftl = &this->nftl;
  333. unsigned long bin_size, flash_size;
  334. bin_size = nftl->nb_boot_blocks * this->erasesize;
  335. flash_size = (nftl->nb_blocks - nftl->nb_boot_blocks) * this->erasesize;
  336. printf("\t NFTL boot record:\n"
  337. "\t Binary partition: size %ld%s\n"
  338. "\t Flash disk partition: size %ld%s, offset 0x%lx\n",
  339. bin_size > (1 << 20) ? bin_size >> 20 : bin_size >> 10,
  340. bin_size > (1 << 20) ? "MB" : "kB",
  341. flash_size > (1 << 20) ? flash_size >> 20 : flash_size >> 10,
  342. flash_size > (1 << 20) ? "MB" : "kB", bin_size);
  343. } else {
  344. puts ("\t No NFTL boot record found.\n");
  345. }
  346. }
  347. /* ------------------------------------------------------------------------- */
  348. /* This function is needed to avoid calls of the __ashrdi3 function. */
  349. static int shr(int val, int shift) {
  350. return val >> shift;
  351. }
  352. /* Perform the required delay cycles by reading from the appropriate register */
  353. static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
  354. {
  355. volatile char dummy;
  356. int i;
  357. for (i = 0; i < cycles; i++) {
  358. if (DoC_is_Millennium(doc))
  359. dummy = ReadDOC(doc->virtadr, NOP);
  360. else
  361. dummy = ReadDOC(doc->virtadr, DOCStatus);
  362. }
  363. }
  364. /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
  365. static int _DoC_WaitReady(struct DiskOnChip *doc)
  366. {
  367. unsigned long docptr = doc->virtadr;
  368. unsigned long start = get_timer(0);
  369. #ifdef PSYCHO_DEBUG
  370. puts ("_DoC_WaitReady called for out-of-line wait\n");
  371. #endif
  372. /* Out-of-line routine to wait for chip response */
  373. while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
  374. #ifdef CONFIG_SYS_DOC_SHORT_TIMEOUT
  375. /* it seems that after a certain time the DoC deasserts
  376. * the CDSN_CTRL_FR_B although it is not ready...
  377. * using a short timout solve this (timer increments every ms) */
  378. if (get_timer(start) > 10) {
  379. return DOC_ETIMEOUT;
  380. }
  381. #else
  382. if (get_timer(start) > 10 * 1000) {
  383. puts ("_DoC_WaitReady timed out.\n");
  384. return DOC_ETIMEOUT;
  385. }
  386. #endif
  387. udelay(1);
  388. }
  389. return 0;
  390. }
  391. static int DoC_WaitReady(struct DiskOnChip *doc)
  392. {
  393. unsigned long docptr = doc->virtadr;
  394. /* This is inline, to optimise the common case, where it's ready instantly */
  395. int ret = 0;
  396. /* 4 read form NOP register should be issued in prior to the read from CDSNControl
  397. see Software Requirement 11.4 item 2. */
  398. DoC_Delay(doc, 4);
  399. if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
  400. /* Call the out-of-line routine to wait */
  401. ret = _DoC_WaitReady(doc);
  402. /* issue 2 read from NOP register after reading from CDSNControl register
  403. see Software Requirement 11.4 item 2. */
  404. DoC_Delay(doc, 2);
  405. return ret;
  406. }
  407. /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
  408. bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
  409. required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
  410. static inline int DoC_Command(struct DiskOnChip *doc, unsigned char command,
  411. unsigned char xtraflags)
  412. {
  413. unsigned long docptr = doc->virtadr;
  414. if (DoC_is_2000(doc))
  415. xtraflags |= CDSN_CTRL_FLASH_IO;
  416. /* Assert the CLE (Command Latch Enable) line to the flash chip */
  417. WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
  418. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  419. if (DoC_is_Millennium(doc))
  420. WriteDOC(command, docptr, CDSNSlowIO);
  421. /* Send the command */
  422. WriteDOC_(command, docptr, doc->ioreg);
  423. /* Lower the CLE line */
  424. WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
  425. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  426. /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
  427. return DoC_WaitReady(doc);
  428. }
  429. /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
  430. bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
  431. required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
  432. static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
  433. unsigned char xtraflags1, unsigned char xtraflags2)
  434. {
  435. unsigned long docptr;
  436. int i;
  437. docptr = doc->virtadr;
  438. if (DoC_is_2000(doc))
  439. xtraflags1 |= CDSN_CTRL_FLASH_IO;
  440. /* Assert the ALE (Address Latch Enable) line to the flash chip */
  441. WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
  442. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  443. /* Send the address */
  444. /* Devices with 256-byte page are addressed as:
  445. Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
  446. * there is no device on the market with page256
  447. and more than 24 bits.
  448. Devices with 512-byte page are addressed as:
  449. Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
  450. * 25-31 is sent only if the chip support it.
  451. * bit 8 changes the read command to be sent
  452. (NAND_CMD_READ0 or NAND_CMD_READ1).
  453. */
  454. if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
  455. if (DoC_is_Millennium(doc))
  456. WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
  457. WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
  458. }
  459. if (doc->page256) {
  460. ofs = ofs >> 8;
  461. } else {
  462. ofs = ofs >> 9;
  463. }
  464. if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
  465. for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
  466. if (DoC_is_Millennium(doc))
  467. WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
  468. WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
  469. }
  470. }
  471. DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
  472. /* FIXME: The SlowIO's for millennium could be replaced by
  473. a single WritePipeTerm here. mf. */
  474. /* Lower the ALE line */
  475. WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
  476. CDSNControl);
  477. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  478. /* Wait for the chip to respond - Software requirement 11.4.1 */
  479. return DoC_WaitReady(doc);
  480. }
  481. /* Read a buffer from DoC, taking care of Millennium oddities */
  482. static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
  483. {
  484. volatile int dummy;
  485. int modulus = 0xffff;
  486. unsigned long docptr;
  487. int i;
  488. docptr = doc->virtadr;
  489. if (len <= 0)
  490. return;
  491. if (DoC_is_Millennium(doc)) {
  492. /* Read the data via the internal pipeline through CDSN IO register,
  493. see Pipelined Read Operations 11.3 */
  494. dummy = ReadDOC(docptr, ReadPipeInit);
  495. /* Millennium should use the LastDataRead register - Pipeline Reads */
  496. len--;
  497. /* This is needed for correctly ECC calculation */
  498. modulus = 0xff;
  499. }
  500. for (i = 0; i < len; i++)
  501. buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
  502. if (DoC_is_Millennium(doc)) {
  503. buf[i] = ReadDOC(docptr, LastDataRead);
  504. }
  505. }
  506. /* Write a buffer to DoC, taking care of Millennium oddities */
  507. static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
  508. {
  509. unsigned long docptr;
  510. int i;
  511. docptr = doc->virtadr;
  512. if (len <= 0)
  513. return;
  514. for (i = 0; i < len; i++)
  515. WriteDOC_(buf[i], docptr, doc->ioreg + i);
  516. if (DoC_is_Millennium(doc)) {
  517. WriteDOC(0x00, docptr, WritePipeTerm);
  518. }
  519. }
  520. /* DoC_SelectChip: Select a given flash chip within the current floor */
  521. static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
  522. {
  523. unsigned long docptr = doc->virtadr;
  524. /* Software requirement 11.4.4 before writing DeviceSelect */
  525. /* Deassert the CE line to eliminate glitches on the FCE# outputs */
  526. WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
  527. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  528. /* Select the individual flash chip requested */
  529. WriteDOC(chip, docptr, CDSNDeviceSelect);
  530. DoC_Delay(doc, 4);
  531. /* Reassert the CE line */
  532. WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
  533. CDSNControl);
  534. DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
  535. /* Wait for it to be ready */
  536. return DoC_WaitReady(doc);
  537. }
  538. /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
  539. static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
  540. {
  541. unsigned long docptr = doc->virtadr;
  542. /* Select the floor (bank) of chips required */
  543. WriteDOC(floor, docptr, FloorSelect);
  544. /* Wait for the chip to be ready */
  545. return DoC_WaitReady(doc);
  546. }
  547. /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
  548. static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
  549. {
  550. int mfr, id, i;
  551. volatile char dummy;
  552. /* Page in the required floor/chip */
  553. DoC_SelectFloor(doc, floor);
  554. DoC_SelectChip(doc, chip);
  555. /* Reset the chip */
  556. if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
  557. #ifdef DOC_DEBUG
  558. printf("DoC_Command (reset) for %d,%d returned true\n",
  559. floor, chip);
  560. #endif
  561. return 0;
  562. }
  563. /* Read the NAND chip ID: 1. Send ReadID command */
  564. if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
  565. #ifdef DOC_DEBUG
  566. printf("DoC_Command (ReadID) for %d,%d returned true\n",
  567. floor, chip);
  568. #endif
  569. return 0;
  570. }
  571. /* Read the NAND chip ID: 2. Send address byte zero */
  572. DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
  573. /* Read the manufacturer and device id codes from the device */
  574. /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
  575. dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
  576. DoC_Delay(doc, 2);
  577. mfr = ReadDOC_(doc->virtadr, doc->ioreg);
  578. /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
  579. dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
  580. DoC_Delay(doc, 2);
  581. id = ReadDOC_(doc->virtadr, doc->ioreg);
  582. /* No response - return failure */
  583. if (mfr == 0xff || mfr == 0)
  584. return 0;
  585. /* Check it's the same as the first chip we identified.
  586. * M-Systems say that any given DiskOnChip device should only
  587. * contain _one_ type of flash part, although that's not a
  588. * hardware restriction. */
  589. if (doc->mfr) {
  590. if (doc->mfr == mfr && doc->id == id)
  591. return 1; /* This is another the same the first */
  592. else
  593. printf("Flash chip at floor %d, chip %d is different:\n",
  594. floor, chip);
  595. }
  596. /* Print and store the manufacturer and ID codes. */
  597. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  598. if (mfr == nand_flash_ids[i].manufacture_id &&
  599. id == nand_flash_ids[i].model_id) {
  600. #ifdef DOC_DEBUG
  601. printf("Flash chip found: Manufacturer ID: %2.2X, "
  602. "Chip ID: %2.2X (%s)\n", mfr, id,
  603. nand_flash_ids[i].name);
  604. #endif
  605. if (!doc->mfr) {
  606. doc->mfr = mfr;
  607. doc->id = id;
  608. doc->chipshift =
  609. nand_flash_ids[i].chipshift;
  610. doc->page256 = nand_flash_ids[i].page256;
  611. doc->pageadrlen =
  612. nand_flash_ids[i].pageadrlen;
  613. doc->erasesize =
  614. nand_flash_ids[i].erasesize;
  615. doc->chips_name =
  616. nand_flash_ids[i].name;
  617. return 1;
  618. }
  619. return 0;
  620. }
  621. }
  622. #ifdef DOC_DEBUG
  623. /* We haven't fully identified the chip. Print as much as we know. */
  624. printf("Unknown flash chip found: %2.2X %2.2X\n",
  625. id, mfr);
  626. #endif
  627. return 0;
  628. }
  629. /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
  630. static void DoC_ScanChips(struct DiskOnChip *this)
  631. {
  632. int floor, chip;
  633. int numchips[MAX_FLOORS];
  634. int maxchips = MAX_CHIPS;
  635. int ret = 1;
  636. this->numchips = 0;
  637. this->mfr = 0;
  638. this->id = 0;
  639. if (DoC_is_Millennium(this))
  640. maxchips = MAX_CHIPS_MIL;
  641. /* For each floor, find the number of valid chips it contains */
  642. for (floor = 0; floor < MAX_FLOORS; floor++) {
  643. ret = 1;
  644. numchips[floor] = 0;
  645. for (chip = 0; chip < maxchips && ret != 0; chip++) {
  646. ret = DoC_IdentChip(this, floor, chip);
  647. if (ret) {
  648. numchips[floor]++;
  649. this->numchips++;
  650. }
  651. }
  652. }
  653. /* If there are none at all that we recognise, bail */
  654. if (!this->numchips) {
  655. puts ("No flash chips recognised.\n");
  656. return;
  657. }
  658. /* Allocate an array to hold the information for each chip */
  659. this->chips = malloc(sizeof(struct Nand) * this->numchips);
  660. if (!this->chips) {
  661. puts ("No memory for allocating chip info structures\n");
  662. return;
  663. }
  664. ret = 0;
  665. /* Fill out the chip array with {floor, chipno} for each
  666. * detected chip in the device. */
  667. for (floor = 0; floor < MAX_FLOORS; floor++) {
  668. for (chip = 0; chip < numchips[floor]; chip++) {
  669. this->chips[ret].floor = floor;
  670. this->chips[ret].chip = chip;
  671. this->chips[ret].curadr = 0;
  672. this->chips[ret].curmode = 0x50;
  673. ret++;
  674. }
  675. }
  676. /* Calculate and print the total size of the device */
  677. this->totlen = this->numchips * (1 << this->chipshift);
  678. #ifdef DOC_DEBUG
  679. printf("%d flash chips found. Total DiskOnChip size: %ld MB\n",
  680. this->numchips, this->totlen >> 20);
  681. #endif
  682. }
  683. /* find_boot_record: Find the NFTL Media Header and its Spare copy which contains the
  684. * various device information of the NFTL partition and Bad Unit Table. Update
  685. * the ReplUnitTable[] table accroding to the Bad Unit Table. ReplUnitTable[]
  686. * is used for management of Erase Unit in other routines in nftl.c and nftlmount.c
  687. */
  688. static int find_boot_record(struct NFTLrecord *nftl)
  689. {
  690. struct nftl_uci1 h1;
  691. struct nftl_oob oob;
  692. unsigned int block, boot_record_count = 0;
  693. int retlen;
  694. u8 buf[SECTORSIZE];
  695. struct NFTLMediaHeader *mh = &nftl->MediaHdr;
  696. unsigned int i;
  697. nftl->MediaUnit = BLOCK_NIL;
  698. nftl->SpareMediaUnit = BLOCK_NIL;
  699. /* search for a valid boot record */
  700. for (block = 0; block < nftl->nb_blocks; block++) {
  701. int ret;
  702. /* Check for ANAND header first. Then can whinge if it's found but later
  703. checks fail */
  704. if ((ret = doc_read_ecc(nftl->mtd, block * nftl->EraseSize, SECTORSIZE,
  705. (size_t *)&retlen, buf, NULL))) {
  706. static int warncount = 5;
  707. if (warncount) {
  708. printf("Block read at 0x%x failed\n", block * nftl->EraseSize);
  709. if (!--warncount)
  710. puts ("Further failures for this block will not be printed\n");
  711. }
  712. continue;
  713. }
  714. if (retlen < 6 || memcmp(buf, "ANAND", 6)) {
  715. /* ANAND\0 not found. Continue */
  716. #ifdef PSYCHO_DEBUG
  717. printf("ANAND header not found at 0x%x\n", block * nftl->EraseSize);
  718. #endif
  719. continue;
  720. }
  721. #ifdef NFTL_DEBUG
  722. printf("ANAND header found at 0x%x\n", block * nftl->EraseSize);
  723. #endif
  724. /* To be safer with BIOS, also use erase mark as discriminant */
  725. if ((ret = doc_read_oob(nftl->mtd, block * nftl->EraseSize + SECTORSIZE + 8,
  726. 8, (size_t *)&retlen, (uchar *)&h1) < 0)) {
  727. #ifdef NFTL_DEBUG
  728. printf("ANAND header found at 0x%x, but OOB data read failed\n",
  729. block * nftl->EraseSize);
  730. #endif
  731. continue;
  732. }
  733. /* OK, we like it. */
  734. if (boot_record_count) {
  735. /* We've already processed one. So we just check if
  736. this one is the same as the first one we found */
  737. if (memcmp(mh, buf, sizeof(struct NFTLMediaHeader))) {
  738. #ifdef NFTL_DEBUG
  739. printf("NFTL Media Headers at 0x%x and 0x%x disagree.\n",
  740. nftl->MediaUnit * nftl->EraseSize, block * nftl->EraseSize);
  741. #endif
  742. /* if (debug) Print both side by side */
  743. return -1;
  744. }
  745. if (boot_record_count == 1)
  746. nftl->SpareMediaUnit = block;
  747. boot_record_count++;
  748. continue;
  749. }
  750. /* This is the first we've seen. Copy the media header structure into place */
  751. memcpy(mh, buf, sizeof(struct NFTLMediaHeader));
  752. /* Do some sanity checks on it */
  753. if (mh->UnitSizeFactor == 0) {
  754. #ifdef NFTL_DEBUG
  755. puts ("UnitSizeFactor 0x00 detected.\n"
  756. "This violates the spec but we think we know what it means...\n");
  757. #endif
  758. } else if (mh->UnitSizeFactor != 0xff) {
  759. printf ("Sorry, we don't support UnitSizeFactor "
  760. "of != 1 yet.\n");
  761. return -1;
  762. }
  763. nftl->nb_boot_blocks = le16_to_cpu(mh->FirstPhysicalEUN);
  764. if ((nftl->nb_boot_blocks + 2) >= nftl->nb_blocks) {
  765. printf ("NFTL Media Header sanity check failed:\n"
  766. "nb_boot_blocks (%d) + 2 > nb_blocks (%d)\n",
  767. nftl->nb_boot_blocks, nftl->nb_blocks);
  768. return -1;
  769. }
  770. nftl->numvunits = le32_to_cpu(mh->FormattedSize) / nftl->EraseSize;
  771. if (nftl->numvunits > (nftl->nb_blocks - nftl->nb_boot_blocks - 2)) {
  772. printf ("NFTL Media Header sanity check failed:\n"
  773. "numvunits (%d) > nb_blocks (%d) - nb_boot_blocks(%d) - 2\n",
  774. nftl->numvunits,
  775. nftl->nb_blocks,
  776. nftl->nb_boot_blocks);
  777. return -1;
  778. }
  779. nftl->nr_sects = nftl->numvunits * (nftl->EraseSize / SECTORSIZE);
  780. /* If we're not using the last sectors in the device for some reason,
  781. reduce nb_blocks accordingly so we forget they're there */
  782. nftl->nb_blocks = le16_to_cpu(mh->NumEraseUnits) + le16_to_cpu(mh->FirstPhysicalEUN);
  783. /* read the Bad Erase Unit Table and modify ReplUnitTable[] accordingly */
  784. for (i = 0; i < nftl->nb_blocks; i++) {
  785. if ((i & (SECTORSIZE - 1)) == 0) {
  786. /* read one sector for every SECTORSIZE of blocks */
  787. if ((ret = doc_read_ecc(nftl->mtd, block * nftl->EraseSize +
  788. i + SECTORSIZE, SECTORSIZE,
  789. (size_t *)&retlen, buf, (uchar *)&oob)) < 0) {
  790. puts ("Read of bad sector table failed\n");
  791. return -1;
  792. }
  793. }
  794. /* mark the Bad Erase Unit as RESERVED in ReplUnitTable */
  795. if (buf[i & (SECTORSIZE - 1)] != 0xff)
  796. nftl->ReplUnitTable[i] = BLOCK_RESERVED;
  797. }
  798. nftl->MediaUnit = block;
  799. boot_record_count++;
  800. } /* foreach (block) */
  801. return boot_record_count?0:-1;
  802. }
  803. /* This routine is made available to other mtd code via
  804. * inter_module_register. It must only be accessed through
  805. * inter_module_get which will bump the use count of this module. The
  806. * addresses passed back in mtd are valid as long as the use count of
  807. * this module is non-zero, i.e. between inter_module_get and
  808. * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
  809. */
  810. static void DoC2k_init(struct DiskOnChip* this)
  811. {
  812. struct NFTLrecord *nftl;
  813. switch (this->ChipID) {
  814. case DOC_ChipID_Doc2k:
  815. this->name = "DiskOnChip 2000";
  816. this->ioreg = DoC_2k_CDSN_IO;
  817. break;
  818. case DOC_ChipID_DocMil:
  819. this->name = "DiskOnChip Millennium";
  820. this->ioreg = DoC_Mil_CDSN_IO;
  821. break;
  822. }
  823. #ifdef DOC_DEBUG
  824. printf("%s found at address 0x%lX\n", this->name,
  825. this->physadr);
  826. #endif
  827. this->totlen = 0;
  828. this->numchips = 0;
  829. this->curfloor = -1;
  830. this->curchip = -1;
  831. /* Ident all the chips present. */
  832. DoC_ScanChips(this);
  833. if ((!this->numchips) || (!this->chips))
  834. return;
  835. nftl = &this->nftl;
  836. /* Get physical parameters */
  837. nftl->EraseSize = this->erasesize;
  838. nftl->nb_blocks = this->totlen / this->erasesize;
  839. nftl->mtd = this;
  840. if (find_boot_record(nftl) != 0)
  841. this->nftl_found = 0;
  842. else
  843. this->nftl_found = 1;
  844. printf("%s @ 0x%lX, %ld MB\n", this->name, this->physadr, this->totlen >> 20);
  845. }
  846. int doc_read_ecc(struct DiskOnChip* this, loff_t from, size_t len,
  847. size_t * retlen, u_char * buf, u_char * eccbuf)
  848. {
  849. unsigned long docptr;
  850. struct Nand *mychip;
  851. unsigned char syndrome[6];
  852. volatile char dummy;
  853. int i, len256 = 0, ret=0;
  854. docptr = this->virtadr;
  855. /* Don't allow read past end of device */
  856. if (from >= this->totlen) {
  857. puts ("Out of flash\n");
  858. return DOC_EINVAL;
  859. }
  860. /* Don't allow a single read to cross a 512-byte block boundary */
  861. if (from + len > ((from | 0x1ff) + 1))
  862. len = ((from | 0x1ff) + 1) - from;
  863. /* The ECC will not be calculated correctly if less than 512 is read */
  864. if (len != 0x200 && eccbuf)
  865. printf("ECC needs a full sector read (adr: %lx size %lx)\n",
  866. (long) from, (long) len);
  867. #ifdef PSYCHO_DEBUG
  868. printf("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len);
  869. #endif
  870. /* Find the chip which is to be used and select it */
  871. mychip = &this->chips[shr(from, this->chipshift)];
  872. if (this->curfloor != mychip->floor) {
  873. DoC_SelectFloor(this, mychip->floor);
  874. DoC_SelectChip(this, mychip->chip);
  875. } else if (this->curchip != mychip->chip) {
  876. DoC_SelectChip(this, mychip->chip);
  877. }
  878. this->curfloor = mychip->floor;
  879. this->curchip = mychip->chip;
  880. DoC_Command(this,
  881. (!this->page256
  882. && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
  883. CDSN_CTRL_WP);
  884. DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
  885. CDSN_CTRL_ECC_IO);
  886. if (eccbuf) {
  887. /* Prime the ECC engine */
  888. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  889. WriteDOC(DOC_ECC_EN, docptr, ECCConf);
  890. } else {
  891. /* disable the ECC engine */
  892. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  893. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  894. }
  895. /* treat crossing 256-byte sector for 2M x 8bits devices */
  896. if (this->page256 && from + len > (from | 0xff) + 1) {
  897. len256 = (from | 0xff) + 1 - from;
  898. DoC_ReadBuf(this, buf, len256);
  899. DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
  900. DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
  901. CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
  902. }
  903. DoC_ReadBuf(this, &buf[len256], len - len256);
  904. /* Let the caller know we completed it */
  905. *retlen = len;
  906. if (eccbuf) {
  907. /* Read the ECC data through the DiskOnChip ECC logic */
  908. /* Note: this will work even with 2M x 8bit devices as */
  909. /* they have 8 bytes of OOB per 256 page. mf. */
  910. DoC_ReadBuf(this, eccbuf, 6);
  911. /* Flush the pipeline */
  912. if (DoC_is_Millennium(this)) {
  913. dummy = ReadDOC(docptr, ECCConf);
  914. dummy = ReadDOC(docptr, ECCConf);
  915. i = ReadDOC(docptr, ECCConf);
  916. } else {
  917. dummy = ReadDOC(docptr, 2k_ECCStatus);
  918. dummy = ReadDOC(docptr, 2k_ECCStatus);
  919. i = ReadDOC(docptr, 2k_ECCStatus);
  920. }
  921. /* Check the ECC Status */
  922. if (i & 0x80) {
  923. int nb_errors;
  924. /* There was an ECC error */
  925. #ifdef ECC_DEBUG
  926. printf("DiskOnChip ECC Error: Read at %lx\n", (long)from);
  927. #endif
  928. /* Read the ECC syndrom through the DiskOnChip ECC logic.
  929. These syndrome will be all ZERO when there is no error */
  930. for (i = 0; i < 6; i++) {
  931. syndrome[i] =
  932. ReadDOC(docptr, ECCSyndrome0 + i);
  933. }
  934. nb_errors = doc_decode_ecc(buf, syndrome);
  935. #ifdef ECC_DEBUG
  936. printf("Errors corrected: %x\n", nb_errors);
  937. #endif
  938. if (nb_errors < 0) {
  939. /* We return error, but have actually done the read. Not that
  940. this can be told to user-space, via sys_read(), but at least
  941. MTD-aware stuff can know about it by checking *retlen */
  942. printf("ECC Errors at %lx\n", (long)from);
  943. ret = DOC_EECC;
  944. }
  945. }
  946. #ifdef PSYCHO_DEBUG
  947. printf("ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  948. (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
  949. eccbuf[3], eccbuf[4], eccbuf[5]);
  950. #endif
  951. /* disable the ECC engine */
  952. WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
  953. }
  954. /* according to 11.4.1, we need to wait for the busy line
  955. * drop if we read to the end of the page. */
  956. if(0 == ((from + *retlen) & 0x1ff))
  957. {
  958. DoC_WaitReady(this);
  959. }
  960. return ret;
  961. }
  962. int doc_write_ecc(struct DiskOnChip* this, loff_t to, size_t len,
  963. size_t * retlen, const u_char * buf,
  964. u_char * eccbuf)
  965. {
  966. int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
  967. unsigned long docptr;
  968. volatile char dummy;
  969. int len256 = 0;
  970. struct Nand *mychip;
  971. docptr = this->virtadr;
  972. /* Don't allow write past end of device */
  973. if (to >= this->totlen) {
  974. puts ("Out of flash\n");
  975. return DOC_EINVAL;
  976. }
  977. /* Don't allow a single write to cross a 512-byte block boundary */
  978. if (to + len > ((to | 0x1ff) + 1))
  979. len = ((to | 0x1ff) + 1) - to;
  980. /* The ECC will not be calculated correctly if less than 512 is written */
  981. if (len != 0x200 && eccbuf)
  982. printf("ECC needs a full sector write (adr: %lx size %lx)\n",
  983. (long) to, (long) len);
  984. /* printf("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
  985. /* Find the chip which is to be used and select it */
  986. mychip = &this->chips[shr(to, this->chipshift)];
  987. if (this->curfloor != mychip->floor) {
  988. DoC_SelectFloor(this, mychip->floor);
  989. DoC_SelectChip(this, mychip->chip);
  990. } else if (this->curchip != mychip->chip) {
  991. DoC_SelectChip(this, mychip->chip);
  992. }
  993. this->curfloor = mychip->floor;
  994. this->curchip = mychip->chip;
  995. /* Set device to main plane of flash */
  996. DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
  997. DoC_Command(this,
  998. (!this->page256
  999. && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
  1000. CDSN_CTRL_WP);
  1001. DoC_Command(this, NAND_CMD_SEQIN, 0);
  1002. DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
  1003. if (eccbuf) {
  1004. /* Prime the ECC engine */
  1005. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  1006. WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
  1007. } else {
  1008. /* disable the ECC engine */
  1009. WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
  1010. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  1011. }
  1012. /* treat crossing 256-byte sector for 2M x 8bits devices */
  1013. if (this->page256 && to + len > (to | 0xff) + 1) {
  1014. len256 = (to | 0xff) + 1 - to;
  1015. DoC_WriteBuf(this, buf, len256);
  1016. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  1017. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  1018. /* There's an implicit DoC_WaitReady() in DoC_Command */
  1019. dummy = ReadDOC(docptr, CDSNSlowIO);
  1020. DoC_Delay(this, 2);
  1021. if (ReadDOC_(docptr, this->ioreg) & 1) {
  1022. puts ("Error programming flash\n");
  1023. /* Error in programming */
  1024. *retlen = 0;
  1025. return DOC_EIO;
  1026. }
  1027. DoC_Command(this, NAND_CMD_SEQIN, 0);
  1028. DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
  1029. CDSN_CTRL_ECC_IO);
  1030. }
  1031. DoC_WriteBuf(this, &buf[len256], len - len256);
  1032. if (eccbuf) {
  1033. WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
  1034. CDSNControl);
  1035. if (DoC_is_Millennium(this)) {
  1036. WriteDOC(0, docptr, NOP);
  1037. WriteDOC(0, docptr, NOP);
  1038. WriteDOC(0, docptr, NOP);
  1039. } else {
  1040. WriteDOC_(0, docptr, this->ioreg);
  1041. WriteDOC_(0, docptr, this->ioreg);
  1042. WriteDOC_(0, docptr, this->ioreg);
  1043. }
  1044. /* Read the ECC data through the DiskOnChip ECC logic */
  1045. for (di = 0; di < 6; di++) {
  1046. eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
  1047. }
  1048. /* Reset the ECC engine */
  1049. WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
  1050. #ifdef PSYCHO_DEBUG
  1051. printf
  1052. ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
  1053. (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
  1054. eccbuf[4], eccbuf[5]);
  1055. #endif
  1056. }
  1057. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  1058. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  1059. /* There's an implicit DoC_WaitReady() in DoC_Command */
  1060. dummy = ReadDOC(docptr, CDSNSlowIO);
  1061. DoC_Delay(this, 2);
  1062. if (ReadDOC_(docptr, this->ioreg) & 1) {
  1063. puts ("Error programming flash\n");
  1064. /* Error in programming */
  1065. *retlen = 0;
  1066. return DOC_EIO;
  1067. }
  1068. /* Let the caller know we completed it */
  1069. *retlen = len;
  1070. if (eccbuf) {
  1071. unsigned char x[8];
  1072. size_t dummy;
  1073. int ret;
  1074. /* Write the ECC data to flash */
  1075. for (di=0; di<6; di++)
  1076. x[di] = eccbuf[di];
  1077. x[6]=0x55;
  1078. x[7]=0x55;
  1079. ret = doc_write_oob(this, to, 8, &dummy, x);
  1080. return ret;
  1081. }
  1082. return 0;
  1083. }
  1084. int doc_read_oob(struct DiskOnChip* this, loff_t ofs, size_t len,
  1085. size_t * retlen, u_char * buf)
  1086. {
  1087. int len256 = 0, ret;
  1088. unsigned long docptr;
  1089. struct Nand *mychip;
  1090. docptr = this->virtadr;
  1091. mychip = &this->chips[shr(ofs, this->chipshift)];
  1092. if (this->curfloor != mychip->floor) {
  1093. DoC_SelectFloor(this, mychip->floor);
  1094. DoC_SelectChip(this, mychip->chip);
  1095. } else if (this->curchip != mychip->chip) {
  1096. DoC_SelectChip(this, mychip->chip);
  1097. }
  1098. this->curfloor = mychip->floor;
  1099. this->curchip = mychip->chip;
  1100. /* update address for 2M x 8bit devices. OOB starts on the second */
  1101. /* page to maintain compatibility with doc_read_ecc. */
  1102. if (this->page256) {
  1103. if (!(ofs & 0x8))
  1104. ofs += 0x100;
  1105. else
  1106. ofs -= 0x8;
  1107. }
  1108. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  1109. DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
  1110. /* treat crossing 8-byte OOB data for 2M x 8bit devices */
  1111. /* Note: datasheet says it should automaticaly wrap to the */
  1112. /* next OOB block, but it didn't work here. mf. */
  1113. if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
  1114. len256 = (ofs | 0x7) + 1 - ofs;
  1115. DoC_ReadBuf(this, buf, len256);
  1116. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  1117. DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
  1118. CDSN_CTRL_WP, 0);
  1119. }
  1120. DoC_ReadBuf(this, &buf[len256], len - len256);
  1121. *retlen = len;
  1122. /* Reading the full OOB data drops us off of the end of the page,
  1123. * causing the flash device to go into busy mode, so we need
  1124. * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
  1125. ret = DoC_WaitReady(this);
  1126. return ret;
  1127. }
  1128. int doc_write_oob(struct DiskOnChip* this, loff_t ofs, size_t len,
  1129. size_t * retlen, const u_char * buf)
  1130. {
  1131. int len256 = 0;
  1132. unsigned long docptr = this->virtadr;
  1133. struct Nand *mychip = &this->chips[shr(ofs, this->chipshift)];
  1134. volatile int dummy;
  1135. #ifdef PSYCHO_DEBUG
  1136. printf("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",
  1137. (long)ofs, len, buf[0], buf[1], buf[2], buf[3],
  1138. buf[8], buf[9], buf[14],buf[15]);
  1139. #endif
  1140. /* Find the chip which is to be used and select it */
  1141. if (this->curfloor != mychip->floor) {
  1142. DoC_SelectFloor(this, mychip->floor);
  1143. DoC_SelectChip(this, mychip->chip);
  1144. } else if (this->curchip != mychip->chip) {
  1145. DoC_SelectChip(this, mychip->chip);
  1146. }
  1147. this->curfloor = mychip->floor;
  1148. this->curchip = mychip->chip;
  1149. /* disable the ECC engine */
  1150. WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
  1151. WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
  1152. /* Reset the chip, see Software Requirement 11.4 item 1. */
  1153. DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
  1154. /* issue the Read2 command to set the pointer to the Spare Data Area. */
  1155. DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
  1156. /* update address for 2M x 8bit devices. OOB starts on the second */
  1157. /* page to maintain compatibility with doc_read_ecc. */
  1158. if (this->page256) {
  1159. if (!(ofs & 0x8))
  1160. ofs += 0x100;
  1161. else
  1162. ofs -= 0x8;
  1163. }
  1164. /* issue the Serial Data In command to initial the Page Program process */
  1165. DoC_Command(this, NAND_CMD_SEQIN, 0);
  1166. DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
  1167. /* treat crossing 8-byte OOB data for 2M x 8bit devices */
  1168. /* Note: datasheet says it should automaticaly wrap to the */
  1169. /* next OOB block, but it didn't work here. mf. */
  1170. if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
  1171. len256 = (ofs | 0x7) + 1 - ofs;
  1172. DoC_WriteBuf(this, buf, len256);
  1173. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  1174. DoC_Command(this, NAND_CMD_STATUS, 0);
  1175. /* DoC_WaitReady() is implicit in DoC_Command */
  1176. dummy = ReadDOC(docptr, CDSNSlowIO);
  1177. DoC_Delay(this, 2);
  1178. if (ReadDOC_(docptr, this->ioreg) & 1) {
  1179. puts ("Error programming oob data\n");
  1180. /* There was an error */
  1181. *retlen = 0;
  1182. return DOC_EIO;
  1183. }
  1184. DoC_Command(this, NAND_CMD_SEQIN, 0);
  1185. DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
  1186. }
  1187. DoC_WriteBuf(this, &buf[len256], len - len256);
  1188. DoC_Command(this, NAND_CMD_PAGEPROG, 0);
  1189. DoC_Command(this, NAND_CMD_STATUS, 0);
  1190. /* DoC_WaitReady() is implicit in DoC_Command */
  1191. dummy = ReadDOC(docptr, CDSNSlowIO);
  1192. DoC_Delay(this, 2);
  1193. if (ReadDOC_(docptr, this->ioreg) & 1) {
  1194. puts ("Error programming oob data\n");
  1195. /* There was an error */
  1196. *retlen = 0;
  1197. return DOC_EIO;
  1198. }
  1199. *retlen = len;
  1200. return 0;
  1201. }
  1202. int doc_erase(struct DiskOnChip* this, loff_t ofs, size_t len)
  1203. {
  1204. volatile int dummy;
  1205. unsigned long docptr;
  1206. struct Nand *mychip;
  1207. if (ofs & (this->erasesize-1) || len & (this->erasesize-1)) {
  1208. puts ("Offset and size must be sector aligned\n");
  1209. return DOC_EINVAL;
  1210. }
  1211. docptr = this->virtadr;
  1212. /* FIXME: Do this in the background. Use timers or schedule_task() */
  1213. while(len) {
  1214. mychip = &this->chips[shr(ofs, this->chipshift)];
  1215. if (this->curfloor != mychip->floor) {
  1216. DoC_SelectFloor(this, mychip->floor);
  1217. DoC_SelectChip(this, mychip->chip);
  1218. } else if (this->curchip != mychip->chip) {
  1219. DoC_SelectChip(this, mychip->chip);
  1220. }
  1221. this->curfloor = mychip->floor;
  1222. this->curchip = mychip->chip;
  1223. DoC_Command(this, NAND_CMD_ERASE1, 0);
  1224. DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
  1225. DoC_Command(this, NAND_CMD_ERASE2, 0);
  1226. DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
  1227. dummy = ReadDOC(docptr, CDSNSlowIO);
  1228. DoC_Delay(this, 2);
  1229. if (ReadDOC_(docptr, this->ioreg) & 1) {
  1230. printf("Error erasing at 0x%lx\n", (long)ofs);
  1231. /* There was an error */
  1232. goto callback;
  1233. }
  1234. ofs += this->erasesize;
  1235. len -= this->erasesize;
  1236. }
  1237. callback:
  1238. return 0;
  1239. }
  1240. static inline int doccheck(unsigned long potential, unsigned long physadr)
  1241. {
  1242. unsigned long window=potential;
  1243. unsigned char tmp, ChipID;
  1244. #ifndef DOC_PASSIVE_PROBE
  1245. unsigned char tmp2;
  1246. #endif
  1247. /* Routine copied from the Linux DOC driver */
  1248. #ifdef CONFIG_SYS_DOCPROBE_55AA
  1249. /* Check for 0x55 0xAA signature at beginning of window,
  1250. this is no longer true once we remove the IPL (for Millennium */
  1251. if (ReadDOC(window, Sig1) != 0x55 || ReadDOC(window, Sig2) != 0xaa)
  1252. return 0;
  1253. #endif /* CONFIG_SYS_DOCPROBE_55AA */
  1254. #ifndef DOC_PASSIVE_PROBE
  1255. /* It's not possible to cleanly detect the DiskOnChip - the
  1256. * bootup procedure will put the device into reset mode, and
  1257. * it's not possible to talk to it without actually writing
  1258. * to the DOCControl register. So we store the current contents
  1259. * of the DOCControl register's location, in case we later decide
  1260. * that it's not a DiskOnChip, and want to put it back how we
  1261. * found it.
  1262. */
  1263. tmp2 = ReadDOC(window, DOCControl);
  1264. /* Reset the DiskOnChip ASIC */
  1265. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
  1266. window, DOCControl);
  1267. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
  1268. window, DOCControl);
  1269. /* Enable the DiskOnChip ASIC */
  1270. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
  1271. window, DOCControl);
  1272. WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
  1273. window, DOCControl);
  1274. #endif /* !DOC_PASSIVE_PROBE */
  1275. ChipID = ReadDOC(window, ChipID);
  1276. switch (ChipID) {
  1277. case DOC_ChipID_Doc2k:
  1278. /* Check the TOGGLE bit in the ECC register */
  1279. tmp = ReadDOC(window, 2k_ECCStatus) & DOC_TOGGLE_BIT;
  1280. if ((ReadDOC(window, 2k_ECCStatus) & DOC_TOGGLE_BIT) != tmp)
  1281. return ChipID;
  1282. break;
  1283. case DOC_ChipID_DocMil:
  1284. /* Check the TOGGLE bit in the ECC register */
  1285. tmp = ReadDOC(window, ECCConf) & DOC_TOGGLE_BIT;
  1286. if ((ReadDOC(window, ECCConf) & DOC_TOGGLE_BIT) != tmp)
  1287. return ChipID;
  1288. break;
  1289. default:
  1290. #ifndef CONFIG_SYS_DOCPROBE_55AA
  1291. /*
  1292. * if the ID isn't the DoC2000 or DoCMillenium ID, so we can assume
  1293. * the DOC is missing
  1294. */
  1295. # if 0
  1296. printf("Possible DiskOnChip with unknown ChipID %2.2X found at 0x%lx\n",
  1297. ChipID, physadr);
  1298. # endif
  1299. #endif
  1300. #ifndef DOC_PASSIVE_PROBE
  1301. /* Put back the contents of the DOCControl register, in case it's not
  1302. * actually a DiskOnChip.
  1303. */
  1304. WriteDOC(tmp2, window, DOCControl);
  1305. #endif
  1306. return 0;
  1307. }
  1308. puts ("DiskOnChip failed TOGGLE test, dropping.\n");
  1309. #ifndef DOC_PASSIVE_PROBE
  1310. /* Put back the contents of the DOCControl register: it's not a DiskOnChip */
  1311. WriteDOC(tmp2, window, DOCControl);
  1312. #endif
  1313. return 0;
  1314. }
  1315. void doc_probe(unsigned long physadr)
  1316. {
  1317. struct DiskOnChip *this = NULL;
  1318. int i=0, ChipID;
  1319. if ((ChipID = doccheck(physadr, physadr))) {
  1320. for (i=0; i<CONFIG_SYS_MAX_DOC_DEVICE; i++) {
  1321. if (doc_dev_desc[i].ChipID == DOC_ChipID_UNKNOWN) {
  1322. this = doc_dev_desc + i;
  1323. break;
  1324. }
  1325. }
  1326. if (!this) {
  1327. puts ("Cannot allocate memory for data structures.\n");
  1328. return;
  1329. }
  1330. if (curr_device == -1)
  1331. curr_device = i;
  1332. memset((char *)this, 0, sizeof(struct DiskOnChip));
  1333. this->virtadr = physadr;
  1334. this->physadr = physadr;
  1335. this->ChipID = ChipID;
  1336. DoC2k_init(this);
  1337. } else {
  1338. puts ("No DiskOnChip found\n");
  1339. }
  1340. }
  1341. #else
  1342. void doc_probe(unsigned long physadr) {}
  1343. #endif