docg4.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*
  2. * drivers/mtd/nand/docg4.c
  3. *
  4. * Copyright (C) 2013 Mike Dunn <mikedunn@newsguy.com>
  5. *
  6. * This file is released under the terms of GPL v2 and any later version.
  7. * See the file COPYING in the root directory of the source tree for details.
  8. *
  9. * mtd nand driver for M-Systems DiskOnChip G4
  10. *
  11. * Tested on the Palm Treo 680. The G4 is also present on Toshiba Portege, Asus
  12. * P526, some HTC smartphones (Wizard, Prophet, ...), O2 XDA Zinc, maybe others.
  13. * Should work on these as well. Let me know!
  14. *
  15. * TODO:
  16. *
  17. * Mechanism for management of password-protected areas
  18. *
  19. * Hamming ecc when reading oob only
  20. *
  21. * According to the M-Sys documentation, this device is also available in a
  22. * "dual-die" configuration having a 256MB capacity, but no mechanism for
  23. * detecting this variant is documented. Currently this driver assumes 128MB
  24. * capacity.
  25. *
  26. * Support for multiple cascaded devices ("floors"). Not sure which gadgets
  27. * contain multiple G4s in a cascaded configuration, if any.
  28. *
  29. */
  30. #include <common.h>
  31. #include <asm/arch/hardware.h>
  32. #include <asm/io.h>
  33. #include <asm/bitops.h>
  34. #include <asm/errno.h>
  35. #include <malloc.h>
  36. #include <nand.h>
  37. #include <linux/bch.h>
  38. #include <linux/bitrev.h>
  39. #include <linux/mtd/docg4.h>
  40. /*
  41. * The device has a nop register which M-Sys claims is for the purpose of
  42. * inserting precise delays. But beware; at least some operations fail if the
  43. * nop writes are replaced with a generic delay!
  44. */
  45. static inline void write_nop(void __iomem *docptr)
  46. {
  47. writew(0, docptr + DOC_NOP);
  48. }
  49. static int poll_status(void __iomem *docptr)
  50. {
  51. /*
  52. * Busy-wait for the FLASHREADY bit to be set in the FLASHCONTROL
  53. * register. Operations known to take a long time (e.g., block erase)
  54. * should sleep for a while before calling this.
  55. */
  56. uint8_t flash_status;
  57. /* hardware quirk requires reading twice initially */
  58. flash_status = readb(docptr + DOC_FLASHCONTROL);
  59. do {
  60. flash_status = readb(docptr + DOC_FLASHCONTROL);
  61. } while (!(flash_status & DOC_CTRL_FLASHREADY));
  62. return 0;
  63. }
  64. static void write_addr(void __iomem *docptr, uint32_t docg4_addr)
  65. {
  66. /* write the four address bytes packed in docg4_addr to the device */
  67. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  68. docg4_addr >>= 8;
  69. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  70. docg4_addr >>= 8;
  71. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  72. docg4_addr >>= 8;
  73. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  74. }
  75. /*
  76. * This is a module parameter in the linux kernel version of this driver. It is
  77. * hard-coded to 'off' for u-boot. This driver uses oob to mark bad blocks.
  78. * This can be problematic when dealing with data not intended for the mtd/nand
  79. * subsystem. For example, on boards that boot from the docg4 and use the IPL
  80. * to load an spl + u-boot image, the blocks containing the image will be
  81. * reported as "bad" because the oob of the first page of each block contains a
  82. * magic number that the IPL looks for, which causes the badblock scan to
  83. * erroneously add them to the bad block table. To erase such a block, use
  84. * u-boot's 'nand scrub'. scrub is safe for the docg4. The device does have a
  85. * factory bad block table, but it is read-only, and is used in conjunction with
  86. * oob bad block markers that are written by mtd/nand when a block is deemed to
  87. * be bad. To read data from "bad" blocks, use 'read.raw'. Unfortunately,
  88. * read.raw does not use ecc, which would still work fine on such misidentified
  89. * bad blocks. TODO: u-boot nand utilities need the ability to ignore bad
  90. * blocks.
  91. */
  92. static const int ignore_badblocks; /* remains false */
  93. struct docg4_priv {
  94. int status;
  95. struct {
  96. unsigned int command;
  97. int column;
  98. int page;
  99. } last_command;
  100. uint8_t oob_buf[16];
  101. uint8_t ecc_buf[7];
  102. int oob_page;
  103. struct bch_control *bch;
  104. };
  105. /*
  106. * Oob bytes 0 - 6 are available to the user.
  107. * Byte 7 is hamming ecc for first 7 bytes. Bytes 8 - 14 are hw-generated ecc.
  108. * Byte 15 (the last) is used by the driver as a "page written" flag.
  109. */
  110. static struct nand_ecclayout docg4_oobinfo = {
  111. .eccbytes = 9,
  112. .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
  113. .oobavail = 7,
  114. .oobfree = { {0, 7} }
  115. };
  116. static void reset(void __iomem *docptr)
  117. {
  118. /* full device reset */
  119. writew(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN, docptr + DOC_ASICMODE);
  120. writew(~(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN),
  121. docptr + DOC_ASICMODECONFIRM);
  122. write_nop(docptr);
  123. writew(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN,
  124. docptr + DOC_ASICMODE);
  125. writew(~(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN),
  126. docptr + DOC_ASICMODECONFIRM);
  127. writew(DOC_ECCCONF1_ECC_ENABLE, docptr + DOC_ECCCONF1);
  128. poll_status(docptr);
  129. }
  130. static void docg4_select_chip(struct mtd_info *mtd, int chip)
  131. {
  132. /*
  133. * Select among multiple cascaded chips ("floors"). Multiple floors are
  134. * not yet supported, so the only valid non-negative value is 0.
  135. */
  136. void __iomem *docptr = CONFIG_SYS_NAND_BASE;
  137. if (chip < 0)
  138. return; /* deselected */
  139. if (chip > 0)
  140. printf("multiple floors currently unsupported\n");
  141. writew(0, docptr + DOC_DEVICESELECT);
  142. }
  143. static void read_hw_ecc(void __iomem *docptr, uint8_t *ecc_buf)
  144. {
  145. /* read the 7 hw-generated ecc bytes */
  146. int i;
  147. for (i = 0; i < 7; i++) { /* hw quirk; read twice */
  148. ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
  149. ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
  150. }
  151. }
  152. static int correct_data(struct mtd_info *mtd, uint8_t *buf, int page)
  153. {
  154. /*
  155. * Called after a page read when hardware reports bitflips.
  156. * Up to four bitflips can be corrected.
  157. */
  158. struct nand_chip *nand = mtd->priv;
  159. struct docg4_priv *doc = nand->priv;
  160. void __iomem *docptr = CONFIG_SYS_NAND_BASE;
  161. int i, numerrs;
  162. unsigned int errpos[4];
  163. const uint8_t blank_read_hwecc[8] = {
  164. 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
  165. read_hw_ecc(docptr, doc->ecc_buf); /* read 7 hw-generated ecc bytes */
  166. /* check if read error is due to a blank page */
  167. if (!memcmp(doc->ecc_buf, blank_read_hwecc, 7))
  168. return 0; /* yes */
  169. /* skip additional check of "written flag" if ignore_badblocks */
  170. if (!ignore_badblocks) {
  171. /*
  172. * If the hw ecc bytes are not those of a blank page, there's
  173. * still a chance that the page is blank, but was read with
  174. * errors. Check the "written flag" in last oob byte, which
  175. * is set to zero when a page is written. If more than half
  176. * the bits are set, assume a blank page. Unfortunately, the
  177. * bit flips(s) are not reported in stats.
  178. */
  179. if (doc->oob_buf[15]) {
  180. int bit, numsetbits = 0;
  181. unsigned long written_flag = doc->oob_buf[15];
  182. for (bit = 0; bit < 8; bit++) {
  183. if (written_flag & 0x01)
  184. numsetbits++;
  185. written_flag >>= 1;
  186. }
  187. if (numsetbits > 4) { /* assume blank */
  188. printf("errors in blank page at offset %08x\n",
  189. page * DOCG4_PAGE_SIZE);
  190. return 0;
  191. }
  192. }
  193. }
  194. /*
  195. * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
  196. * algorithm is used to decode this. However the hw operates on page
  197. * data in a bit order that is the reverse of that of the bch alg,
  198. * requiring that the bits be reversed on the result. Thanks to Ivan
  199. * Djelic for his analysis!
  200. */
  201. for (i = 0; i < 7; i++)
  202. doc->ecc_buf[i] = bitrev8(doc->ecc_buf[i]);
  203. numerrs = decode_bch(doc->bch, NULL, DOCG4_USERDATA_LEN, NULL,
  204. doc->ecc_buf, NULL, errpos);
  205. if (numerrs == -EBADMSG) {
  206. printf("uncorrectable errors at offset %08x\n",
  207. page * DOCG4_PAGE_SIZE);
  208. return -EBADMSG;
  209. }
  210. BUG_ON(numerrs < 0); /* -EINVAL, or anything other than -EBADMSG */
  211. /* undo last step in BCH alg (modulo mirroring not needed) */
  212. for (i = 0; i < numerrs; i++)
  213. errpos[i] = (errpos[i] & ~7)|(7-(errpos[i] & 7));
  214. /* fix the errors */
  215. for (i = 0; i < numerrs; i++) {
  216. /* ignore if error within oob ecc bytes */
  217. if (errpos[i] > DOCG4_USERDATA_LEN * 8)
  218. continue;
  219. /* if error within oob area preceeding ecc bytes... */
  220. if (errpos[i] > DOCG4_PAGE_SIZE * 8)
  221. __change_bit(errpos[i] - DOCG4_PAGE_SIZE * 8,
  222. (unsigned long *)doc->oob_buf);
  223. else /* error in page data */
  224. __change_bit(errpos[i], (unsigned long *)buf);
  225. }
  226. printf("%d error(s) corrected at offset %08x\n",
  227. numerrs, page * DOCG4_PAGE_SIZE);
  228. return numerrs;
  229. }
  230. static int read_progstatus(struct docg4_priv *doc, void __iomem *docptr)
  231. {
  232. /*
  233. * This apparently checks the status of programming. Done after an
  234. * erasure, and after page data is written. On error, the status is
  235. * saved, to be later retrieved by the nand infrastructure code.
  236. */
  237. /* status is read from the I/O reg */
  238. uint16_t status1 = readw(docptr + DOC_IOSPACE_DATA);
  239. uint16_t status2 = readw(docptr + DOC_IOSPACE_DATA);
  240. uint16_t status3 = readw(docptr + DOCG4_MYSTERY_REG);
  241. MTDDEBUG(MTD_DEBUG_LEVEL3, "docg4: %s: %02x %02x %02x\n",
  242. __func__, status1, status2, status3);
  243. if (status1 != DOCG4_PROGSTATUS_GOOD ||
  244. status2 != DOCG4_PROGSTATUS_GOOD_2 ||
  245. status3 != DOCG4_PROGSTATUS_GOOD_2) {
  246. doc->status = NAND_STATUS_FAIL;
  247. printf("read_progstatus failed: %02x, %02x, %02x\n",
  248. status1, status2, status3);
  249. return -EIO;
  250. }
  251. return 0;
  252. }
  253. static int pageprog(struct mtd_info *mtd)
  254. {
  255. /*
  256. * Final step in writing a page. Writes the contents of its
  257. * internal buffer out to the flash array, or some such.
  258. */
  259. struct nand_chip *nand = mtd->priv;
  260. struct docg4_priv *doc = nand->priv;
  261. void __iomem *docptr = CONFIG_SYS_NAND_BASE;
  262. int retval = 0;
  263. MTDDEBUG(MTD_DEBUG_LEVEL3, "docg4: %s\n", __func__);
  264. writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
  265. writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
  266. write_nop(docptr);
  267. write_nop(docptr);
  268. /* Just busy-wait; usleep_range() slows things down noticeably. */
  269. poll_status(docptr);
  270. writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
  271. writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
  272. writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
  273. write_nop(docptr);
  274. write_nop(docptr);
  275. write_nop(docptr);
  276. write_nop(docptr);
  277. write_nop(docptr);
  278. retval = read_progstatus(doc, docptr);
  279. writew(0, docptr + DOC_DATAEND);
  280. write_nop(docptr);
  281. poll_status(docptr);
  282. write_nop(docptr);
  283. return retval;
  284. }
  285. static void sequence_reset(void __iomem *docptr)
  286. {
  287. /* common starting sequence for all operations */
  288. writew(DOC_CTRL_UNKNOWN | DOC_CTRL_CE, docptr + DOC_FLASHCONTROL);
  289. writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
  290. writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
  291. write_nop(docptr);
  292. write_nop(docptr);
  293. poll_status(docptr);
  294. write_nop(docptr);
  295. }
  296. static void read_page_prologue(void __iomem *docptr, uint32_t docg4_addr)
  297. {
  298. /* first step in reading a page */
  299. sequence_reset(docptr);
  300. writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
  301. writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
  302. write_nop(docptr);
  303. write_addr(docptr, docg4_addr);
  304. write_nop(docptr);
  305. writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
  306. write_nop(docptr);
  307. write_nop(docptr);
  308. poll_status(docptr);
  309. }
  310. static void write_page_prologue(void __iomem *docptr, uint32_t docg4_addr)
  311. {
  312. /* first step in writing a page */
  313. sequence_reset(docptr);
  314. writew(DOCG4_SEQ_PAGEWRITE, docptr + DOC_FLASHSEQUENCE);
  315. writew(DOCG4_CMD_PAGEWRITE, docptr + DOC_FLASHCOMMAND);
  316. write_nop(docptr);
  317. write_addr(docptr, docg4_addr);
  318. write_nop(docptr);
  319. write_nop(docptr);
  320. poll_status(docptr);
  321. }
  322. static uint32_t mtd_to_docg4_address(int page, int column)
  323. {
  324. /*
  325. * Convert mtd address to format used by the device, 32 bit packed.
  326. *
  327. * Some notes on G4 addressing... The M-Sys documentation on this device
  328. * claims that pages are 2K in length, and indeed, the format of the
  329. * address used by the device reflects that. But within each page are
  330. * four 512 byte "sub-pages", each with its own oob data that is
  331. * read/written immediately after the 512 bytes of page data. This oob
  332. * data contains the ecc bytes for the preceeding 512 bytes.
  333. *
  334. * Rather than tell the mtd nand infrastructure that page size is 2k,
  335. * with four sub-pages each, we engage in a little subterfuge and tell
  336. * the infrastructure code that pages are 512 bytes in size. This is
  337. * done because during the course of reverse-engineering the device, I
  338. * never observed an instance where an entire 2K "page" was read or
  339. * written as a unit. Each "sub-page" is always addressed individually,
  340. * its data read/written, and ecc handled before the next "sub-page" is
  341. * addressed.
  342. *
  343. * This requires us to convert addresses passed by the mtd nand
  344. * infrastructure code to those used by the device.
  345. *
  346. * The address that is written to the device consists of four bytes: the
  347. * first two are the 2k page number, and the second is the index into
  348. * the page. The index is in terms of 16-bit half-words and includes
  349. * the preceeding oob data, so e.g., the index into the second
  350. * "sub-page" is 0x108, and the full device address of the start of mtd
  351. * page 0x201 is 0x00800108.
  352. */
  353. int g4_page = page / 4; /* device's 2K page */
  354. int g4_index = (page % 4) * 0x108 + column/2; /* offset into page */
  355. return (g4_page << 16) | g4_index; /* pack */
  356. }
  357. static void docg4_command(struct mtd_info *mtd, unsigned command, int column,
  358. int page_addr)
  359. {
  360. /* handle standard nand commands */
  361. struct nand_chip *nand = mtd->priv;
  362. struct docg4_priv *doc = nand->priv;
  363. uint32_t g4_addr = mtd_to_docg4_address(page_addr, column);
  364. MTDDEBUG(MTD_DEBUG_LEVEL3, "%s %x, page_addr=%x, column=%x\n",
  365. __func__, command, page_addr, column);
  366. /*
  367. * Save the command and its arguments. This enables emulation of
  368. * standard flash devices, and also some optimizations.
  369. */
  370. doc->last_command.command = command;
  371. doc->last_command.column = column;
  372. doc->last_command.page = page_addr;
  373. switch (command) {
  374. case NAND_CMD_RESET:
  375. reset(CONFIG_SYS_NAND_BASE);
  376. break;
  377. case NAND_CMD_READ0:
  378. read_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
  379. break;
  380. case NAND_CMD_STATUS:
  381. /* next call to read_byte() will expect a status */
  382. break;
  383. case NAND_CMD_SEQIN:
  384. write_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
  385. /* hack for deferred write of oob bytes */
  386. if (doc->oob_page == page_addr)
  387. memcpy(nand->oob_poi, doc->oob_buf, 16);
  388. break;
  389. case NAND_CMD_PAGEPROG:
  390. pageprog(mtd);
  391. break;
  392. /* we don't expect these, based on review of nand_base.c */
  393. case NAND_CMD_READOOB:
  394. case NAND_CMD_READID:
  395. case NAND_CMD_ERASE1:
  396. case NAND_CMD_ERASE2:
  397. printf("docg4_command: unexpected nand command 0x%x\n",
  398. command);
  399. break;
  400. }
  401. }
  402. static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  403. {
  404. int i;
  405. struct nand_chip *nand = mtd->priv;
  406. uint16_t *p = (uint16_t *)buf;
  407. len >>= 1;
  408. for (i = 0; i < len; i++)
  409. p[i] = readw(nand->IO_ADDR_R);
  410. }
  411. static int docg4_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
  412. int page)
  413. {
  414. struct docg4_priv *doc = nand->priv;
  415. void __iomem *docptr = CONFIG_SYS_NAND_BASE;
  416. uint16_t status;
  417. MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: page %x\n", __func__, page);
  418. /*
  419. * Oob bytes are read as part of a normal page read. If the previous
  420. * nand command was a read of the page whose oob is now being read, just
  421. * copy the oob bytes that we saved in a local buffer and avoid a
  422. * separate oob read.
  423. */
  424. if (doc->last_command.command == NAND_CMD_READ0 &&
  425. doc->last_command.page == page) {
  426. memcpy(nand->oob_poi, doc->oob_buf, 16);
  427. return 0;
  428. }
  429. /*
  430. * Separate read of oob data only.
  431. */
  432. docg4_command(mtd, NAND_CMD_READ0, nand->ecc.size, page);
  433. writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
  434. write_nop(docptr);
  435. write_nop(docptr);
  436. write_nop(docptr);
  437. write_nop(docptr);
  438. write_nop(docptr);
  439. /* the 1st byte from the I/O reg is a status; the rest is oob data */
  440. status = readw(docptr + DOC_IOSPACE_DATA);
  441. if (status & DOCG4_READ_ERROR) {
  442. printf("docg4_read_oob failed: status = 0x%02x\n", status);
  443. return -EIO;
  444. }
  445. MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: status = 0x%x\n", __func__, status);
  446. docg4_read_buf(mtd, nand->oob_poi, 16);
  447. write_nop(docptr);
  448. write_nop(docptr);
  449. write_nop(docptr);
  450. writew(0, docptr + DOC_DATAEND);
  451. write_nop(docptr);
  452. return 0;
  453. }
  454. static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
  455. int page)
  456. {
  457. /*
  458. * Writing oob-only is not really supported, because MLC nand must write
  459. * oob bytes at the same time as page data. Nonetheless, we save the
  460. * oob buffer contents here, and then write it along with the page data
  461. * if the same page is subsequently written. This allows user space
  462. * utilities that write the oob data prior to the page data to work
  463. * (e.g., nandwrite). The disdvantage is that, if the intention was to
  464. * write oob only, the operation is quietly ignored. Also, oob can get
  465. * corrupted if two concurrent processes are running nandwrite.
  466. */
  467. /* note that bytes 7..14 are hw generated hamming/ecc and overwritten */
  468. struct docg4_priv *doc = nand->priv;
  469. doc->oob_page = page;
  470. memcpy(doc->oob_buf, nand->oob_poi, 16);
  471. return 0;
  472. }
  473. static int docg4_block_neverbad(struct mtd_info *mtd, loff_t ofs, int getchip)
  474. {
  475. /* only called when module_param ignore_badblocks is set */
  476. return 0;
  477. }
  478. static void docg4_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  479. {
  480. int i;
  481. struct nand_chip *nand = mtd->priv;
  482. uint16_t *p = (uint16_t *)buf;
  483. len >>= 1;
  484. for (i = 0; i < len; i++)
  485. writew(p[i], nand->IO_ADDR_W);
  486. }
  487. static int write_page(struct mtd_info *mtd, struct nand_chip *nand,
  488. const uint8_t *buf, int use_ecc)
  489. {
  490. void __iomem *docptr = CONFIG_SYS_NAND_BASE;
  491. uint8_t ecc_buf[8];
  492. writew(DOC_ECCCONF0_ECC_ENABLE |
  493. DOC_ECCCONF0_UNKNOWN |
  494. DOCG4_BCH_SIZE,
  495. docptr + DOC_ECCCONF0);
  496. write_nop(docptr);
  497. /* write the page data */
  498. docg4_write_buf16(mtd, buf, DOCG4_PAGE_SIZE);
  499. /* oob bytes 0 through 5 are written to I/O reg */
  500. docg4_write_buf16(mtd, nand->oob_poi, 6);
  501. /* oob byte 6 written to a separate reg */
  502. writew(nand->oob_poi[6], docptr + DOCG4_OOB_6_7);
  503. write_nop(docptr);
  504. write_nop(docptr);
  505. /* write hw-generated ecc bytes to oob */
  506. if (likely(use_ecc)) {
  507. /* oob byte 7 is hamming code */
  508. uint8_t hamming = readb(docptr + DOC_HAMMINGPARITY);
  509. hamming = readb(docptr + DOC_HAMMINGPARITY); /* 2nd read */
  510. writew(hamming, docptr + DOCG4_OOB_6_7);
  511. write_nop(docptr);
  512. /* read the 7 bch bytes from ecc regs */
  513. read_hw_ecc(docptr, ecc_buf);
  514. ecc_buf[7] = 0; /* clear the "page written" flag */
  515. }
  516. /* write user-supplied bytes to oob */
  517. else {
  518. writew(nand->oob_poi[7], docptr + DOCG4_OOB_6_7);
  519. write_nop(docptr);
  520. memcpy(ecc_buf, &nand->oob_poi[8], 8);
  521. }
  522. docg4_write_buf16(mtd, ecc_buf, 8);
  523. write_nop(docptr);
  524. write_nop(docptr);
  525. writew(0, docptr + DOC_DATAEND);
  526. write_nop(docptr);
  527. return 0;
  528. }
  529. static int docg4_write_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
  530. const uint8_t *buf, int oob_required)
  531. {
  532. return write_page(mtd, nand, buf, 0);
  533. }
  534. static int docg4_write_page(struct mtd_info *mtd, struct nand_chip *nand,
  535. const uint8_t *buf, int oob_required)
  536. {
  537. return write_page(mtd, nand, buf, 1);
  538. }
  539. static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
  540. uint8_t *buf, int page, int use_ecc)
  541. {
  542. struct docg4_priv *doc = nand->priv;
  543. void __iomem *docptr = CONFIG_SYS_NAND_BASE;
  544. uint16_t status, edc_err, *buf16;
  545. writew(DOC_ECCCONF0_READ_MODE |
  546. DOC_ECCCONF0_ECC_ENABLE |
  547. DOC_ECCCONF0_UNKNOWN |
  548. DOCG4_BCH_SIZE,
  549. docptr + DOC_ECCCONF0);
  550. write_nop(docptr);
  551. write_nop(docptr);
  552. write_nop(docptr);
  553. write_nop(docptr);
  554. write_nop(docptr);
  555. /* the 1st byte from the I/O reg is a status; the rest is page data */
  556. status = readw(docptr + DOC_IOSPACE_DATA);
  557. if (status & DOCG4_READ_ERROR) {
  558. printf("docg4_read_page: bad status: 0x%02x\n", status);
  559. writew(0, docptr + DOC_DATAEND);
  560. return -EIO;
  561. }
  562. docg4_read_buf(mtd, buf, DOCG4_PAGE_SIZE); /* read the page data */
  563. /* first 14 oob bytes read from I/O reg */
  564. docg4_read_buf(mtd, nand->oob_poi, 14);
  565. /* last 2 read from another reg */
  566. buf16 = (uint16_t *)(nand->oob_poi + 14);
  567. *buf16 = readw(docptr + DOCG4_MYSTERY_REG);
  568. /*
  569. * Diskonchips read oob immediately after a page read. Mtd
  570. * infrastructure issues a separate command for reading oob after the
  571. * page is read. So we save the oob bytes in a local buffer and just
  572. * copy it if the next command reads oob from the same page.
  573. */
  574. memcpy(doc->oob_buf, nand->oob_poi, 16);
  575. write_nop(docptr);
  576. if (likely(use_ecc)) {
  577. /* read the register that tells us if bitflip(s) detected */
  578. edc_err = readw(docptr + DOC_ECCCONF1);
  579. edc_err = readw(docptr + DOC_ECCCONF1);
  580. /* If bitflips are reported, attempt to correct with ecc */
  581. if (edc_err & DOC_ECCCONF1_BCH_SYNDROM_ERR) {
  582. int bits_corrected = correct_data(mtd, buf, page);
  583. if (bits_corrected == -EBADMSG)
  584. mtd->ecc_stats.failed++;
  585. else
  586. mtd->ecc_stats.corrected += bits_corrected;
  587. }
  588. }
  589. writew(0, docptr + DOC_DATAEND);
  590. return 0;
  591. }
  592. static int docg4_read_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
  593. uint8_t *buf, int oob_required, int page)
  594. {
  595. return read_page(mtd, nand, buf, page, 0);
  596. }
  597. static int docg4_read_page(struct mtd_info *mtd, struct nand_chip *nand,
  598. uint8_t *buf, int oob_required, int page)
  599. {
  600. return read_page(mtd, nand, buf, page, 1);
  601. }
  602. static void docg4_erase_block(struct mtd_info *mtd, int page)
  603. {
  604. struct nand_chip *nand = mtd->priv;
  605. struct docg4_priv *doc = nand->priv;
  606. void __iomem *docptr = CONFIG_SYS_NAND_BASE;
  607. uint16_t g4_page;
  608. MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: page %04x\n", __func__, page);
  609. sequence_reset(docptr);
  610. writew(DOCG4_SEQ_BLOCKERASE, docptr + DOC_FLASHSEQUENCE);
  611. writew(DOC_CMD_PROG_BLOCK_ADDR, docptr + DOC_FLASHCOMMAND);
  612. write_nop(docptr);
  613. /* only 2 bytes of address are written to specify erase block */
  614. g4_page = (uint16_t)(page / 4); /* to g4's 2k page addressing */
  615. writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
  616. g4_page >>= 8;
  617. writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
  618. write_nop(docptr);
  619. /* start the erasure */
  620. writew(DOC_CMD_ERASECYCLE2, docptr + DOC_FLASHCOMMAND);
  621. write_nop(docptr);
  622. write_nop(docptr);
  623. poll_status(docptr);
  624. writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
  625. writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
  626. writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
  627. write_nop(docptr);
  628. write_nop(docptr);
  629. write_nop(docptr);
  630. write_nop(docptr);
  631. write_nop(docptr);
  632. read_progstatus(doc, docptr);
  633. writew(0, docptr + DOC_DATAEND);
  634. write_nop(docptr);
  635. poll_status(docptr);
  636. write_nop(docptr);
  637. }
  638. static int read_factory_bbt(struct mtd_info *mtd)
  639. {
  640. /*
  641. * The device contains a read-only factory bad block table. Read it and
  642. * update the memory-based bbt accordingly.
  643. */
  644. struct nand_chip *nand = mtd->priv;
  645. uint32_t g4_addr = mtd_to_docg4_address(DOCG4_FACTORY_BBT_PAGE, 0);
  646. uint8_t *buf;
  647. int i, block, status;
  648. buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
  649. if (buf == NULL)
  650. return -ENOMEM;
  651. read_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
  652. status = docg4_read_page(mtd, nand, buf, 0, DOCG4_FACTORY_BBT_PAGE);
  653. if (status)
  654. goto exit;
  655. /*
  656. * If no memory-based bbt was created, exit. This will happen if module
  657. * parameter ignore_badblocks is set. Then why even call this function?
  658. * For an unknown reason, block erase always fails if it's the first
  659. * operation after device power-up. The above read ensures it never is.
  660. * Ugly, I know.
  661. */
  662. if (nand->bbt == NULL) /* no memory-based bbt */
  663. goto exit;
  664. /*
  665. * Parse factory bbt and update memory-based bbt. Factory bbt format is
  666. * simple: one bit per block, block numbers increase left to right (msb
  667. * to lsb). Bit clear means bad block.
  668. */
  669. for (i = block = 0; block < DOCG4_NUMBLOCKS; block += 8, i++) {
  670. int bitnum;
  671. uint8_t mask;
  672. for (bitnum = 0, mask = 0x80;
  673. bitnum < 8; bitnum++, mask >>= 1) {
  674. if (!(buf[i] & mask)) {
  675. int badblock = block + bitnum;
  676. nand->bbt[badblock / 4] |=
  677. 0x03 << ((badblock % 4) * 2);
  678. mtd->ecc_stats.badblocks++;
  679. printf("factory-marked bad block: %d\n",
  680. badblock);
  681. }
  682. }
  683. }
  684. exit:
  685. kfree(buf);
  686. return status;
  687. }
  688. static int docg4_block_markbad(struct mtd_info *mtd, loff_t ofs)
  689. {
  690. /*
  691. * Mark a block as bad. Bad blocks are marked in the oob area of the
  692. * first page of the block. The default scan_bbt() in the nand
  693. * infrastructure code works fine for building the memory-based bbt
  694. * during initialization, as does the nand infrastructure function that
  695. * checks if a block is bad by reading the bbt. This function replaces
  696. * the nand default because writes to oob-only are not supported.
  697. */
  698. int ret, i;
  699. uint8_t *buf;
  700. struct nand_chip *nand = mtd->priv;
  701. struct nand_bbt_descr *bbtd = nand->badblock_pattern;
  702. int block = (int)(ofs >> nand->bbt_erase_shift);
  703. int page = (int)(ofs >> nand->page_shift);
  704. uint32_t g4_addr = mtd_to_docg4_address(page, 0);
  705. MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: %08llx\n", __func__, ofs);
  706. if (unlikely(ofs & (DOCG4_BLOCK_SIZE - 1)))
  707. printf("%s: ofs %llx not start of block!\n",
  708. __func__, ofs);
  709. /* allocate blank buffer for page data */
  710. buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
  711. if (buf == NULL)
  712. return -ENOMEM;
  713. /* update bbt in memory */
  714. nand->bbt[block / 4] |= 0x01 << ((block & 0x03) * 2);
  715. /* write bit-wise negation of pattern to oob buffer */
  716. memset(nand->oob_poi, 0xff, mtd->oobsize);
  717. for (i = 0; i < bbtd->len; i++)
  718. nand->oob_poi[bbtd->offs + i] = ~bbtd->pattern[i];
  719. /* write first page of block */
  720. write_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
  721. docg4_write_page(mtd, nand, buf, 1);
  722. ret = pageprog(mtd);
  723. if (!ret)
  724. mtd->ecc_stats.badblocks++;
  725. kfree(buf);
  726. return ret;
  727. }
  728. static uint8_t docg4_read_byte(struct mtd_info *mtd)
  729. {
  730. struct nand_chip *nand = mtd->priv;
  731. struct docg4_priv *doc = nand->priv;
  732. MTDDEBUG(MTD_DEBUG_LEVEL3, "%s\n", __func__);
  733. if (doc->last_command.command == NAND_CMD_STATUS) {
  734. int status;
  735. /*
  736. * Previous nand command was status request, so nand
  737. * infrastructure code expects to read the status here. If an
  738. * error occurred in a previous operation, report it.
  739. */
  740. doc->last_command.command = 0;
  741. if (doc->status) {
  742. status = doc->status;
  743. doc->status = 0;
  744. }
  745. /* why is NAND_STATUS_WP inverse logic?? */
  746. else
  747. status = NAND_STATUS_WP | NAND_STATUS_READY;
  748. return status;
  749. }
  750. printf("unexpectd call to read_byte()\n");
  751. return 0;
  752. }
  753. static int docg4_wait(struct mtd_info *mtd, struct nand_chip *nand)
  754. {
  755. struct docg4_priv *doc = nand->priv;
  756. int status = NAND_STATUS_WP; /* inverse logic?? */
  757. MTDDEBUG(MTD_DEBUG_LEVEL3, "%s...\n", __func__);
  758. /* report any previously unreported error */
  759. if (doc->status) {
  760. status |= doc->status;
  761. doc->status = 0;
  762. return status;
  763. }
  764. status |= poll_status(CONFIG_SYS_NAND_BASE);
  765. return status;
  766. }
  767. int docg4_nand_init(struct mtd_info *mtd, struct nand_chip *nand, int devnum)
  768. {
  769. uint16_t id1, id2;
  770. struct docg4_priv *docg4;
  771. int retval;
  772. docg4 = kzalloc(sizeof(*docg4), GFP_KERNEL);
  773. if (!docg4)
  774. return -1;
  775. mtd->priv = nand;
  776. nand->priv = docg4;
  777. /* These must be initialized here because the docg4 is non-standard
  778. * and doesn't produce an id that the nand code can use to look up
  779. * these values (nand_scan_ident() not called).
  780. */
  781. mtd->size = DOCG4_CHIP_SIZE;
  782. mtd->name = "Msys_Diskonchip_G4";
  783. mtd->writesize = DOCG4_PAGE_SIZE;
  784. mtd->erasesize = DOCG4_BLOCK_SIZE;
  785. mtd->oobsize = DOCG4_OOB_SIZE;
  786. nand->IO_ADDR_R =
  787. (void __iomem *)CONFIG_SYS_NAND_BASE + DOC_IOSPACE_DATA;
  788. nand->IO_ADDR_W = nand->IO_ADDR_R;
  789. nand->chipsize = DOCG4_CHIP_SIZE;
  790. nand->chip_shift = DOCG4_CHIP_SHIFT;
  791. nand->bbt_erase_shift = DOCG4_ERASE_SHIFT;
  792. nand->phys_erase_shift = DOCG4_ERASE_SHIFT;
  793. nand->chip_delay = 20;
  794. nand->page_shift = DOCG4_PAGE_SHIFT;
  795. nand->pagemask = 0x3ffff;
  796. nand->badblockpos = NAND_LARGE_BADBLOCK_POS;
  797. nand->badblockbits = 8;
  798. nand->ecc.layout = &docg4_oobinfo;
  799. nand->ecc.mode = NAND_ECC_HW_SYNDROME;
  800. nand->ecc.size = DOCG4_PAGE_SIZE;
  801. nand->ecc.prepad = 8;
  802. nand->ecc.bytes = 8;
  803. nand->ecc.strength = DOCG4_T;
  804. nand->options = NAND_BUSWIDTH_16 | NAND_NO_SUBPAGE_WRITE;
  805. nand->controller = &nand->hwcontrol;
  806. /* methods */
  807. nand->cmdfunc = docg4_command;
  808. nand->waitfunc = docg4_wait;
  809. nand->select_chip = docg4_select_chip;
  810. nand->read_byte = docg4_read_byte;
  811. nand->block_markbad = docg4_block_markbad;
  812. nand->read_buf = docg4_read_buf;
  813. nand->write_buf = docg4_write_buf16;
  814. nand->scan_bbt = nand_default_bbt;
  815. nand->erase_cmd = docg4_erase_block;
  816. nand->ecc.read_page = docg4_read_page;
  817. nand->ecc.write_page = docg4_write_page;
  818. nand->ecc.read_page_raw = docg4_read_page_raw;
  819. nand->ecc.write_page_raw = docg4_write_page_raw;
  820. nand->ecc.read_oob = docg4_read_oob;
  821. nand->ecc.write_oob = docg4_write_oob;
  822. /*
  823. * The way the nand infrastructure code is written, a memory-based bbt
  824. * is not created if NAND_SKIP_BBTSCAN is set. With no memory bbt,
  825. * nand->block_bad() is used. So when ignoring bad blocks, we skip the
  826. * scan and define a dummy block_bad() which always returns 0.
  827. */
  828. if (ignore_badblocks) {
  829. nand->options |= NAND_SKIP_BBTSCAN;
  830. nand->block_bad = docg4_block_neverbad;
  831. }
  832. reset(CONFIG_SYS_NAND_BASE);
  833. /* check for presence of g4 chip by reading id registers */
  834. id1 = readw(CONFIG_SYS_NAND_BASE + DOC_CHIPID);
  835. id1 = readw(CONFIG_SYS_NAND_BASE + DOCG4_MYSTERY_REG);
  836. id2 = readw(CONFIG_SYS_NAND_BASE + DOC_CHIPID_INV);
  837. id2 = readw(CONFIG_SYS_NAND_BASE + DOCG4_MYSTERY_REG);
  838. if (id1 != DOCG4_IDREG1_VALUE || id2 != DOCG4_IDREG2_VALUE)
  839. return -1;
  840. /* initialize bch algorithm */
  841. docg4->bch = init_bch(DOCG4_M, DOCG4_T, DOCG4_PRIMITIVE_POLY);
  842. if (docg4->bch == NULL)
  843. return -1;
  844. retval = nand_scan_tail(mtd);
  845. if (retval)
  846. return -1;
  847. /*
  848. * Scan for bad blocks and create bbt here, then add the factory-marked
  849. * bad blocks to the bbt.
  850. */
  851. nand->scan_bbt(mtd);
  852. nand->options |= NAND_BBT_SCANNED;
  853. retval = read_factory_bbt(mtd);
  854. if (retval)
  855. return -1;
  856. retval = nand_register(devnum);
  857. if (retval)
  858. return -1;
  859. return 0;
  860. }