docg4.c 29 KB

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