cmd_doc.c 44 KB

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