docg4.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /*
  2. * Copyright © 2012 Mike Dunn <mikedunn@newsguy.com>
  3. *
  4. * mtd nand driver for M-Systems DiskOnChip G4
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  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 <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/init.h>
  33. #include <linux/string.h>
  34. #include <linux/sched.h>
  35. #include <linux/delay.h>
  36. #include <linux/module.h>
  37. #include <linux/export.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/io.h>
  40. #include <linux/bitops.h>
  41. #include <linux/mtd/partitions.h>
  42. #include <linux/mtd/mtd.h>
  43. #include <linux/mtd/nand.h>
  44. #include <linux/bch.h>
  45. #include <linux/bitrev.h>
  46. #include <linux/jiffies.h>
  47. /*
  48. * In "reliable mode" consecutive 2k pages are used in parallel (in some
  49. * fashion) to store the same data. The data can be read back from the
  50. * even-numbered pages in the normal manner; odd-numbered pages will appear to
  51. * contain junk. Systems that boot from the docg4 typically write the secondary
  52. * program loader (SPL) code in this mode. The SPL is loaded by the initial
  53. * program loader (IPL, stored in the docg4's 2k NOR-like region that is mapped
  54. * to the reset vector address). This module parameter enables you to use this
  55. * driver to write the SPL. When in this mode, no more than 2k of data can be
  56. * written at a time, because the addresses do not increment in the normal
  57. * manner, and the starting offset must be within an even-numbered 2k region;
  58. * i.e., invalid starting offsets are 0x800, 0xa00, 0xc00, 0xe00, 0x1800,
  59. * 0x1a00, ... Reliable mode is a special case and should not be used unless
  60. * you know what you're doing.
  61. */
  62. static bool reliable_mode;
  63. module_param(reliable_mode, bool, 0);
  64. MODULE_PARM_DESC(reliable_mode, "pages are programmed in reliable mode");
  65. /*
  66. * You'll want to ignore badblocks if you're reading a partition that contains
  67. * data written by the TrueFFS library (i.e., by PalmOS, Windows, etc), since
  68. * it does not use mtd nand's method for marking bad blocks (using oob area).
  69. * This will also skip the check of the "page written" flag.
  70. */
  71. static bool ignore_badblocks;
  72. module_param(ignore_badblocks, bool, 0);
  73. MODULE_PARM_DESC(ignore_badblocks, "no badblock checking performed");
  74. struct docg4_priv {
  75. struct mtd_info *mtd;
  76. struct device *dev;
  77. void __iomem *virtadr;
  78. int status;
  79. struct {
  80. unsigned int command;
  81. int column;
  82. int page;
  83. } last_command;
  84. uint8_t oob_buf[16];
  85. uint8_t ecc_buf[7];
  86. int oob_page;
  87. struct bch_control *bch;
  88. };
  89. /*
  90. * Defines prefixed with DOCG4 are unique to the diskonchip G4. All others are
  91. * shared with other diskonchip devices (P3, G3 at least).
  92. *
  93. * Functions with names prefixed with docg4_ are mtd / nand interface functions
  94. * (though they may also be called internally). All others are internal.
  95. */
  96. #define DOC_IOSPACE_DATA 0x0800
  97. /* register offsets */
  98. #define DOC_CHIPID 0x1000
  99. #define DOC_DEVICESELECT 0x100a
  100. #define DOC_ASICMODE 0x100c
  101. #define DOC_DATAEND 0x101e
  102. #define DOC_NOP 0x103e
  103. #define DOC_FLASHSEQUENCE 0x1032
  104. #define DOC_FLASHCOMMAND 0x1034
  105. #define DOC_FLASHADDRESS 0x1036
  106. #define DOC_FLASHCONTROL 0x1038
  107. #define DOC_ECCCONF0 0x1040
  108. #define DOC_ECCCONF1 0x1042
  109. #define DOC_HAMMINGPARITY 0x1046
  110. #define DOC_BCH_SYNDROM(idx) (0x1048 + idx)
  111. #define DOC_ASICMODECONFIRM 0x1072
  112. #define DOC_CHIPID_INV 0x1074
  113. #define DOC_POWERMODE 0x107c
  114. #define DOCG4_MYSTERY_REG 0x1050
  115. /* apparently used only to write oob bytes 6 and 7 */
  116. #define DOCG4_OOB_6_7 0x1052
  117. /* DOC_FLASHSEQUENCE register commands */
  118. #define DOC_SEQ_RESET 0x00
  119. #define DOCG4_SEQ_PAGE_READ 0x03
  120. #define DOCG4_SEQ_FLUSH 0x29
  121. #define DOCG4_SEQ_PAGEWRITE 0x16
  122. #define DOCG4_SEQ_PAGEPROG 0x1e
  123. #define DOCG4_SEQ_BLOCKERASE 0x24
  124. #define DOCG4_SEQ_SETMODE 0x45
  125. /* DOC_FLASHCOMMAND register commands */
  126. #define DOCG4_CMD_PAGE_READ 0x00
  127. #define DOC_CMD_ERASECYCLE2 0xd0
  128. #define DOCG4_CMD_FLUSH 0x70
  129. #define DOCG4_CMD_READ2 0x30
  130. #define DOC_CMD_PROG_BLOCK_ADDR 0x60
  131. #define DOCG4_CMD_PAGEWRITE 0x80
  132. #define DOC_CMD_PROG_CYCLE2 0x10
  133. #define DOCG4_CMD_FAST_MODE 0xa3 /* functionality guessed */
  134. #define DOC_CMD_RELIABLE_MODE 0x22
  135. #define DOC_CMD_RESET 0xff
  136. /* DOC_POWERMODE register bits */
  137. #define DOC_POWERDOWN_READY 0x80
  138. /* DOC_FLASHCONTROL register bits */
  139. #define DOC_CTRL_CE 0x10
  140. #define DOC_CTRL_UNKNOWN 0x40
  141. #define DOC_CTRL_FLASHREADY 0x01
  142. /* DOC_ECCCONF0 register bits */
  143. #define DOC_ECCCONF0_READ_MODE 0x8000
  144. #define DOC_ECCCONF0_UNKNOWN 0x2000
  145. #define DOC_ECCCONF0_ECC_ENABLE 0x1000
  146. #define DOC_ECCCONF0_DATA_BYTES_MASK 0x07ff
  147. /* DOC_ECCCONF1 register bits */
  148. #define DOC_ECCCONF1_BCH_SYNDROM_ERR 0x80
  149. #define DOC_ECCCONF1_ECC_ENABLE 0x07
  150. #define DOC_ECCCONF1_PAGE_IS_WRITTEN 0x20
  151. /* DOC_ASICMODE register bits */
  152. #define DOC_ASICMODE_RESET 0x00
  153. #define DOC_ASICMODE_NORMAL 0x01
  154. #define DOC_ASICMODE_POWERDOWN 0x02
  155. #define DOC_ASICMODE_MDWREN 0x04
  156. #define DOC_ASICMODE_BDETCT_RESET 0x08
  157. #define DOC_ASICMODE_RSTIN_RESET 0x10
  158. #define DOC_ASICMODE_RAM_WE 0x20
  159. /* good status values read after read/write/erase operations */
  160. #define DOCG4_PROGSTATUS_GOOD 0x51
  161. #define DOCG4_PROGSTATUS_GOOD_2 0xe0
  162. /*
  163. * On read operations (page and oob-only), the first byte read from I/O reg is a
  164. * status. On error, it reads 0x73; otherwise, it reads either 0x71 (first read
  165. * after reset only) or 0x51, so bit 1 is presumed to be an error indicator.
  166. */
  167. #define DOCG4_READ_ERROR 0x02 /* bit 1 indicates read error */
  168. /* anatomy of the device */
  169. #define DOCG4_CHIP_SIZE 0x8000000
  170. #define DOCG4_PAGE_SIZE 0x200
  171. #define DOCG4_PAGES_PER_BLOCK 0x200
  172. #define DOCG4_BLOCK_SIZE (DOCG4_PAGES_PER_BLOCK * DOCG4_PAGE_SIZE)
  173. #define DOCG4_NUMBLOCKS (DOCG4_CHIP_SIZE / DOCG4_BLOCK_SIZE)
  174. #define DOCG4_OOB_SIZE 0x10
  175. #define DOCG4_CHIP_SHIFT 27 /* log_2(DOCG4_CHIP_SIZE) */
  176. #define DOCG4_PAGE_SHIFT 9 /* log_2(DOCG4_PAGE_SIZE) */
  177. #define DOCG4_ERASE_SHIFT 18 /* log_2(DOCG4_BLOCK_SIZE) */
  178. /* all but the last byte is included in ecc calculation */
  179. #define DOCG4_BCH_SIZE (DOCG4_PAGE_SIZE + DOCG4_OOB_SIZE - 1)
  180. #define DOCG4_USERDATA_LEN 520 /* 512 byte page plus 8 oob avail to user */
  181. /* expected values from the ID registers */
  182. #define DOCG4_IDREG1_VALUE 0x0400
  183. #define DOCG4_IDREG2_VALUE 0xfbff
  184. /* primitive polynomial used to build the Galois field used by hw ecc gen */
  185. #define DOCG4_PRIMITIVE_POLY 0x4443
  186. #define DOCG4_M 14 /* Galois field is of order 2^14 */
  187. #define DOCG4_T 4 /* BCH alg corrects up to 4 bit errors */
  188. #define DOCG4_FACTORY_BBT_PAGE 16 /* page where read-only factory bbt lives */
  189. #define DOCG4_REDUNDANT_BBT_PAGE 24 /* page where redundant factory bbt lives */
  190. /*
  191. * Bytes 0, 1 are used as badblock marker.
  192. * Bytes 2 - 6 are available to the user.
  193. * Byte 7 is hamming ecc for first 7 oob bytes only.
  194. * Bytes 8 - 14 are hw-generated ecc covering entire page + oob bytes 0 - 14.
  195. * Byte 15 (the last) is used by the driver as a "page written" flag.
  196. */
  197. static struct nand_ecclayout docg4_oobinfo = {
  198. .eccbytes = 9,
  199. .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
  200. .oobavail = 5,
  201. .oobfree = { {.offset = 2, .length = 5} }
  202. };
  203. /*
  204. * The device has a nop register which M-Sys claims is for the purpose of
  205. * inserting precise delays. But beware; at least some operations fail if the
  206. * nop writes are replaced with a generic delay!
  207. */
  208. static inline void write_nop(void __iomem *docptr)
  209. {
  210. writew(0, docptr + DOC_NOP);
  211. }
  212. static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  213. {
  214. int i;
  215. struct nand_chip *nand = mtd->priv;
  216. uint16_t *p = (uint16_t *) buf;
  217. len >>= 1;
  218. for (i = 0; i < len; i++)
  219. p[i] = readw(nand->IO_ADDR_R);
  220. }
  221. static void docg4_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  222. {
  223. int i;
  224. struct nand_chip *nand = mtd->priv;
  225. uint16_t *p = (uint16_t *) buf;
  226. len >>= 1;
  227. for (i = 0; i < len; i++)
  228. writew(p[i], nand->IO_ADDR_W);
  229. }
  230. static int poll_status(struct docg4_priv *doc)
  231. {
  232. /*
  233. * Busy-wait for the FLASHREADY bit to be set in the FLASHCONTROL
  234. * register. Operations known to take a long time (e.g., block erase)
  235. * should sleep for a while before calling this.
  236. */
  237. uint16_t flash_status;
  238. unsigned long timeo;
  239. void __iomem *docptr = doc->virtadr;
  240. dev_dbg(doc->dev, "%s...\n", __func__);
  241. /* hardware quirk requires reading twice initially */
  242. flash_status = readw(docptr + DOC_FLASHCONTROL);
  243. timeo = jiffies + msecs_to_jiffies(200); /* generous timeout */
  244. do {
  245. cpu_relax();
  246. flash_status = readb(docptr + DOC_FLASHCONTROL);
  247. } while (!(flash_status & DOC_CTRL_FLASHREADY) &&
  248. time_before(jiffies, timeo));
  249. if (unlikely(!(flash_status & DOC_CTRL_FLASHREADY))) {
  250. dev_err(doc->dev, "%s: timed out!\n", __func__);
  251. return NAND_STATUS_FAIL;
  252. }
  253. return 0;
  254. }
  255. static int docg4_wait(struct mtd_info *mtd, struct nand_chip *nand)
  256. {
  257. struct docg4_priv *doc = nand->priv;
  258. int status = NAND_STATUS_WP; /* inverse logic?? */
  259. dev_dbg(doc->dev, "%s...\n", __func__);
  260. /* report any previously unreported error */
  261. if (doc->status) {
  262. status |= doc->status;
  263. doc->status = 0;
  264. return status;
  265. }
  266. status |= poll_status(doc);
  267. return status;
  268. }
  269. static void docg4_select_chip(struct mtd_info *mtd, int chip)
  270. {
  271. /*
  272. * Select among multiple cascaded chips ("floors"). Multiple floors are
  273. * not yet supported, so the only valid non-negative value is 0.
  274. */
  275. struct nand_chip *nand = mtd->priv;
  276. struct docg4_priv *doc = nand->priv;
  277. void __iomem *docptr = doc->virtadr;
  278. dev_dbg(doc->dev, "%s: chip %d\n", __func__, chip);
  279. if (chip < 0)
  280. return; /* deselected */
  281. if (chip > 0)
  282. dev_warn(doc->dev, "multiple floors currently unsupported\n");
  283. writew(0, docptr + DOC_DEVICESELECT);
  284. }
  285. static void reset(struct mtd_info *mtd)
  286. {
  287. /* full device reset */
  288. struct nand_chip *nand = mtd->priv;
  289. struct docg4_priv *doc = nand->priv;
  290. void __iomem *docptr = doc->virtadr;
  291. writew(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN,
  292. docptr + DOC_ASICMODE);
  293. writew(~(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN),
  294. docptr + DOC_ASICMODECONFIRM);
  295. write_nop(docptr);
  296. writew(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN,
  297. docptr + DOC_ASICMODE);
  298. writew(~(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN),
  299. docptr + DOC_ASICMODECONFIRM);
  300. writew(DOC_ECCCONF1_ECC_ENABLE, docptr + DOC_ECCCONF1);
  301. poll_status(doc);
  302. }
  303. static void read_hw_ecc(void __iomem *docptr, uint8_t *ecc_buf)
  304. {
  305. /* read the 7 hw-generated ecc bytes */
  306. int i;
  307. for (i = 0; i < 7; i++) { /* hw quirk; read twice */
  308. ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
  309. ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
  310. }
  311. }
  312. static int correct_data(struct mtd_info *mtd, uint8_t *buf, int page)
  313. {
  314. /*
  315. * Called after a page read when hardware reports bitflips.
  316. * Up to four bitflips can be corrected.
  317. */
  318. struct nand_chip *nand = mtd->priv;
  319. struct docg4_priv *doc = nand->priv;
  320. void __iomem *docptr = doc->virtadr;
  321. int i, numerrs, errpos[4];
  322. const uint8_t blank_read_hwecc[8] = {
  323. 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
  324. read_hw_ecc(docptr, doc->ecc_buf); /* read 7 hw-generated ecc bytes */
  325. /* check if read error is due to a blank page */
  326. if (!memcmp(doc->ecc_buf, blank_read_hwecc, 7))
  327. return 0; /* yes */
  328. /* skip additional check of "written flag" if ignore_badblocks */
  329. if (ignore_badblocks == false) {
  330. /*
  331. * If the hw ecc bytes are not those of a blank page, there's
  332. * still a chance that the page is blank, but was read with
  333. * errors. Check the "written flag" in last oob byte, which
  334. * is set to zero when a page is written. If more than half
  335. * the bits are set, assume a blank page. Unfortunately, the
  336. * bit flips(s) are not reported in stats.
  337. */
  338. if (nand->oob_poi[15]) {
  339. int bit, numsetbits = 0;
  340. unsigned long written_flag = nand->oob_poi[15];
  341. for_each_set_bit(bit, &written_flag, 8)
  342. numsetbits++;
  343. if (numsetbits > 4) { /* assume blank */
  344. dev_warn(doc->dev,
  345. "error(s) in blank page "
  346. "at offset %08x\n",
  347. page * DOCG4_PAGE_SIZE);
  348. return 0;
  349. }
  350. }
  351. }
  352. /*
  353. * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
  354. * algorithm is used to decode this. However the hw operates on page
  355. * data in a bit order that is the reverse of that of the bch alg,
  356. * requiring that the bits be reversed on the result. Thanks to Ivan
  357. * Djelic for his analysis!
  358. */
  359. for (i = 0; i < 7; i++)
  360. doc->ecc_buf[i] = bitrev8(doc->ecc_buf[i]);
  361. numerrs = decode_bch(doc->bch, NULL, DOCG4_USERDATA_LEN, NULL,
  362. doc->ecc_buf, NULL, errpos);
  363. if (numerrs == -EBADMSG) {
  364. dev_warn(doc->dev, "uncorrectable errors at offset %08x\n",
  365. page * DOCG4_PAGE_SIZE);
  366. return -EBADMSG;
  367. }
  368. BUG_ON(numerrs < 0); /* -EINVAL, or anything other than -EBADMSG */
  369. /* undo last step in BCH alg (modulo mirroring not needed) */
  370. for (i = 0; i < numerrs; i++)
  371. errpos[i] = (errpos[i] & ~7)|(7-(errpos[i] & 7));
  372. /* fix the errors */
  373. for (i = 0; i < numerrs; i++) {
  374. /* ignore if error within oob ecc bytes */
  375. if (errpos[i] > DOCG4_USERDATA_LEN * 8)
  376. continue;
  377. /* if error within oob area preceeding ecc bytes... */
  378. if (errpos[i] > DOCG4_PAGE_SIZE * 8)
  379. change_bit(errpos[i] - DOCG4_PAGE_SIZE * 8,
  380. (unsigned long *)nand->oob_poi);
  381. else /* error in page data */
  382. change_bit(errpos[i], (unsigned long *)buf);
  383. }
  384. dev_notice(doc->dev, "%d error(s) corrected at offset %08x\n",
  385. numerrs, page * DOCG4_PAGE_SIZE);
  386. return numerrs;
  387. }
  388. static uint8_t docg4_read_byte(struct mtd_info *mtd)
  389. {
  390. struct nand_chip *nand = mtd->priv;
  391. struct docg4_priv *doc = nand->priv;
  392. dev_dbg(doc->dev, "%s\n", __func__);
  393. if (doc->last_command.command == NAND_CMD_STATUS) {
  394. int status;
  395. /*
  396. * Previous nand command was status request, so nand
  397. * infrastructure code expects to read the status here. If an
  398. * error occurred in a previous operation, report it.
  399. */
  400. doc->last_command.command = 0;
  401. if (doc->status) {
  402. status = doc->status;
  403. doc->status = 0;
  404. }
  405. /* why is NAND_STATUS_WP inverse logic?? */
  406. else
  407. status = NAND_STATUS_WP | NAND_STATUS_READY;
  408. return status;
  409. }
  410. dev_warn(doc->dev, "unexpected call to read_byte()\n");
  411. return 0;
  412. }
  413. static void write_addr(struct docg4_priv *doc, uint32_t docg4_addr)
  414. {
  415. /* write the four address bytes packed in docg4_addr to the device */
  416. void __iomem *docptr = doc->virtadr;
  417. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  418. docg4_addr >>= 8;
  419. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  420. docg4_addr >>= 8;
  421. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  422. docg4_addr >>= 8;
  423. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  424. }
  425. static int read_progstatus(struct docg4_priv *doc)
  426. {
  427. /*
  428. * This apparently checks the status of programming. Done after an
  429. * erasure, and after page data is written. On error, the status is
  430. * saved, to be later retrieved by the nand infrastructure code.
  431. */
  432. void __iomem *docptr = doc->virtadr;
  433. /* status is read from the I/O reg */
  434. uint16_t status1 = readw(docptr + DOC_IOSPACE_DATA);
  435. uint16_t status2 = readw(docptr + DOC_IOSPACE_DATA);
  436. uint16_t status3 = readw(docptr + DOCG4_MYSTERY_REG);
  437. dev_dbg(doc->dev, "docg4: %s: %02x %02x %02x\n",
  438. __func__, status1, status2, status3);
  439. if (status1 != DOCG4_PROGSTATUS_GOOD
  440. || status2 != DOCG4_PROGSTATUS_GOOD_2
  441. || status3 != DOCG4_PROGSTATUS_GOOD_2) {
  442. doc->status = NAND_STATUS_FAIL;
  443. dev_warn(doc->dev, "read_progstatus failed: "
  444. "%02x, %02x, %02x\n", status1, status2, status3);
  445. return -EIO;
  446. }
  447. return 0;
  448. }
  449. static int pageprog(struct mtd_info *mtd)
  450. {
  451. /*
  452. * Final step in writing a page. Writes the contents of its
  453. * internal buffer out to the flash array, or some such.
  454. */
  455. struct nand_chip *nand = mtd->priv;
  456. struct docg4_priv *doc = nand->priv;
  457. void __iomem *docptr = doc->virtadr;
  458. int retval = 0;
  459. dev_dbg(doc->dev, "docg4: %s\n", __func__);
  460. writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
  461. writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
  462. write_nop(docptr);
  463. write_nop(docptr);
  464. /* Just busy-wait; usleep_range() slows things down noticeably. */
  465. poll_status(doc);
  466. writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
  467. writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
  468. writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
  469. write_nop(docptr);
  470. write_nop(docptr);
  471. write_nop(docptr);
  472. write_nop(docptr);
  473. write_nop(docptr);
  474. retval = read_progstatus(doc);
  475. writew(0, docptr + DOC_DATAEND);
  476. write_nop(docptr);
  477. poll_status(doc);
  478. write_nop(docptr);
  479. return retval;
  480. }
  481. static void sequence_reset(struct mtd_info *mtd)
  482. {
  483. /* common starting sequence for all operations */
  484. struct nand_chip *nand = mtd->priv;
  485. struct docg4_priv *doc = nand->priv;
  486. void __iomem *docptr = doc->virtadr;
  487. writew(DOC_CTRL_UNKNOWN | DOC_CTRL_CE, docptr + DOC_FLASHCONTROL);
  488. writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
  489. writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
  490. write_nop(docptr);
  491. write_nop(docptr);
  492. poll_status(doc);
  493. write_nop(docptr);
  494. }
  495. static void read_page_prologue(struct mtd_info *mtd, uint32_t docg4_addr)
  496. {
  497. /* first step in reading a page */
  498. struct nand_chip *nand = mtd->priv;
  499. struct docg4_priv *doc = nand->priv;
  500. void __iomem *docptr = doc->virtadr;
  501. dev_dbg(doc->dev,
  502. "docg4: %s: g4 page %08x\n", __func__, docg4_addr);
  503. sequence_reset(mtd);
  504. writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
  505. writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
  506. write_nop(docptr);
  507. write_addr(doc, docg4_addr);
  508. write_nop(docptr);
  509. writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
  510. write_nop(docptr);
  511. write_nop(docptr);
  512. poll_status(doc);
  513. }
  514. static void write_page_prologue(struct mtd_info *mtd, uint32_t docg4_addr)
  515. {
  516. /* first step in writing a page */
  517. struct nand_chip *nand = mtd->priv;
  518. struct docg4_priv *doc = nand->priv;
  519. void __iomem *docptr = doc->virtadr;
  520. dev_dbg(doc->dev,
  521. "docg4: %s: g4 addr: %x\n", __func__, docg4_addr);
  522. sequence_reset(mtd);
  523. if (unlikely(reliable_mode)) {
  524. writew(DOCG4_SEQ_SETMODE, docptr + DOC_FLASHSEQUENCE);
  525. writew(DOCG4_CMD_FAST_MODE, docptr + DOC_FLASHCOMMAND);
  526. writew(DOC_CMD_RELIABLE_MODE, docptr + DOC_FLASHCOMMAND);
  527. write_nop(docptr);
  528. }
  529. writew(DOCG4_SEQ_PAGEWRITE, docptr + DOC_FLASHSEQUENCE);
  530. writew(DOCG4_CMD_PAGEWRITE, docptr + DOC_FLASHCOMMAND);
  531. write_nop(docptr);
  532. write_addr(doc, docg4_addr);
  533. write_nop(docptr);
  534. write_nop(docptr);
  535. poll_status(doc);
  536. }
  537. static uint32_t mtd_to_docg4_address(int page, int column)
  538. {
  539. /*
  540. * Convert mtd address to format used by the device, 32 bit packed.
  541. *
  542. * Some notes on G4 addressing... The M-Sys documentation on this device
  543. * claims that pages are 2K in length, and indeed, the format of the
  544. * address used by the device reflects that. But within each page are
  545. * four 512 byte "sub-pages", each with its own oob data that is
  546. * read/written immediately after the 512 bytes of page data. This oob
  547. * data contains the ecc bytes for the preceeding 512 bytes.
  548. *
  549. * Rather than tell the mtd nand infrastructure that page size is 2k,
  550. * with four sub-pages each, we engage in a little subterfuge and tell
  551. * the infrastructure code that pages are 512 bytes in size. This is
  552. * done because during the course of reverse-engineering the device, I
  553. * never observed an instance where an entire 2K "page" was read or
  554. * written as a unit. Each "sub-page" is always addressed individually,
  555. * its data read/written, and ecc handled before the next "sub-page" is
  556. * addressed.
  557. *
  558. * This requires us to convert addresses passed by the mtd nand
  559. * infrastructure code to those used by the device.
  560. *
  561. * The address that is written to the device consists of four bytes: the
  562. * first two are the 2k page number, and the second is the index into
  563. * the page. The index is in terms of 16-bit half-words and includes
  564. * the preceeding oob data, so e.g., the index into the second
  565. * "sub-page" is 0x108, and the full device address of the start of mtd
  566. * page 0x201 is 0x00800108.
  567. */
  568. int g4_page = page / 4; /* device's 2K page */
  569. int g4_index = (page % 4) * 0x108 + column/2; /* offset into page */
  570. return (g4_page << 16) | g4_index; /* pack */
  571. }
  572. static void docg4_command(struct mtd_info *mtd, unsigned command, int column,
  573. int page_addr)
  574. {
  575. /* handle standard nand commands */
  576. struct nand_chip *nand = mtd->priv;
  577. struct docg4_priv *doc = nand->priv;
  578. uint32_t g4_addr = mtd_to_docg4_address(page_addr, column);
  579. dev_dbg(doc->dev, "%s %x, page_addr=%x, column=%x\n",
  580. __func__, command, page_addr, column);
  581. /*
  582. * Save the command and its arguments. This enables emulation of
  583. * standard flash devices, and also some optimizations.
  584. */
  585. doc->last_command.command = command;
  586. doc->last_command.column = column;
  587. doc->last_command.page = page_addr;
  588. switch (command) {
  589. case NAND_CMD_RESET:
  590. reset(mtd);
  591. break;
  592. case NAND_CMD_READ0:
  593. read_page_prologue(mtd, g4_addr);
  594. break;
  595. case NAND_CMD_STATUS:
  596. /* next call to read_byte() will expect a status */
  597. break;
  598. case NAND_CMD_SEQIN:
  599. if (unlikely(reliable_mode)) {
  600. uint16_t g4_page = g4_addr >> 16;
  601. /* writes to odd-numbered 2k pages are invalid */
  602. if (g4_page & 0x01)
  603. dev_warn(doc->dev,
  604. "invalid reliable mode address\n");
  605. }
  606. write_page_prologue(mtd, g4_addr);
  607. /* hack for deferred write of oob bytes */
  608. if (doc->oob_page == page_addr)
  609. memcpy(nand->oob_poi, doc->oob_buf, 16);
  610. break;
  611. case NAND_CMD_PAGEPROG:
  612. pageprog(mtd);
  613. break;
  614. /* we don't expect these, based on review of nand_base.c */
  615. case NAND_CMD_READOOB:
  616. case NAND_CMD_READID:
  617. case NAND_CMD_ERASE1:
  618. case NAND_CMD_ERASE2:
  619. dev_warn(doc->dev, "docg4_command: "
  620. "unexpected nand command 0x%x\n", command);
  621. break;
  622. }
  623. }
  624. static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
  625. uint8_t *buf, int page, bool use_ecc)
  626. {
  627. struct docg4_priv *doc = nand->priv;
  628. void __iomem *docptr = doc->virtadr;
  629. uint16_t status, edc_err, *buf16;
  630. int bits_corrected = 0;
  631. dev_dbg(doc->dev, "%s: page %08x\n", __func__, page);
  632. writew(DOC_ECCCONF0_READ_MODE |
  633. DOC_ECCCONF0_ECC_ENABLE |
  634. DOC_ECCCONF0_UNKNOWN |
  635. DOCG4_BCH_SIZE,
  636. docptr + DOC_ECCCONF0);
  637. write_nop(docptr);
  638. write_nop(docptr);
  639. write_nop(docptr);
  640. write_nop(docptr);
  641. write_nop(docptr);
  642. /* the 1st byte from the I/O reg is a status; the rest is page data */
  643. status = readw(docptr + DOC_IOSPACE_DATA);
  644. if (status & DOCG4_READ_ERROR) {
  645. dev_err(doc->dev,
  646. "docg4_read_page: bad status: 0x%02x\n", status);
  647. writew(0, docptr + DOC_DATAEND);
  648. return -EIO;
  649. }
  650. dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
  651. docg4_read_buf(mtd, buf, DOCG4_PAGE_SIZE); /* read the page data */
  652. /* this device always reads oob after page data */
  653. /* first 14 oob bytes read from I/O reg */
  654. docg4_read_buf(mtd, nand->oob_poi, 14);
  655. /* last 2 read from another reg */
  656. buf16 = (uint16_t *)(nand->oob_poi + 14);
  657. *buf16 = readw(docptr + DOCG4_MYSTERY_REG);
  658. write_nop(docptr);
  659. if (likely(use_ecc == true)) {
  660. /* read the register that tells us if bitflip(s) detected */
  661. edc_err = readw(docptr + DOC_ECCCONF1);
  662. edc_err = readw(docptr + DOC_ECCCONF1);
  663. dev_dbg(doc->dev, "%s: edc_err = 0x%02x\n", __func__, edc_err);
  664. /* If bitflips are reported, attempt to correct with ecc */
  665. if (edc_err & DOC_ECCCONF1_BCH_SYNDROM_ERR) {
  666. bits_corrected = correct_data(mtd, buf, page);
  667. if (bits_corrected == -EBADMSG)
  668. mtd->ecc_stats.failed++;
  669. else
  670. mtd->ecc_stats.corrected += bits_corrected;
  671. }
  672. }
  673. writew(0, docptr + DOC_DATAEND);
  674. if (bits_corrected == -EBADMSG) /* uncorrectable errors */
  675. return 0;
  676. return bits_corrected;
  677. }
  678. static int docg4_read_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
  679. uint8_t *buf, int oob_required, int page)
  680. {
  681. return read_page(mtd, nand, buf, page, false);
  682. }
  683. static int docg4_read_page(struct mtd_info *mtd, struct nand_chip *nand,
  684. uint8_t *buf, int oob_required, int page)
  685. {
  686. return read_page(mtd, nand, buf, page, true);
  687. }
  688. static int docg4_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
  689. int page)
  690. {
  691. struct docg4_priv *doc = nand->priv;
  692. void __iomem *docptr = doc->virtadr;
  693. uint16_t status;
  694. dev_dbg(doc->dev, "%s: page %x\n", __func__, page);
  695. docg4_command(mtd, NAND_CMD_READ0, nand->ecc.size, page);
  696. writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
  697. write_nop(docptr);
  698. write_nop(docptr);
  699. write_nop(docptr);
  700. write_nop(docptr);
  701. write_nop(docptr);
  702. /* the 1st byte from the I/O reg is a status; the rest is oob data */
  703. status = readw(docptr + DOC_IOSPACE_DATA);
  704. if (status & DOCG4_READ_ERROR) {
  705. dev_warn(doc->dev,
  706. "docg4_read_oob failed: status = 0x%02x\n", status);
  707. return -EIO;
  708. }
  709. dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
  710. docg4_read_buf(mtd, nand->oob_poi, 16);
  711. write_nop(docptr);
  712. write_nop(docptr);
  713. write_nop(docptr);
  714. writew(0, docptr + DOC_DATAEND);
  715. write_nop(docptr);
  716. return 0;
  717. }
  718. static int docg4_erase_block(struct mtd_info *mtd, int page)
  719. {
  720. struct nand_chip *nand = mtd->priv;
  721. struct docg4_priv *doc = nand->priv;
  722. void __iomem *docptr = doc->virtadr;
  723. uint16_t g4_page;
  724. dev_dbg(doc->dev, "%s: page %04x\n", __func__, page);
  725. sequence_reset(mtd);
  726. writew(DOCG4_SEQ_BLOCKERASE, docptr + DOC_FLASHSEQUENCE);
  727. writew(DOC_CMD_PROG_BLOCK_ADDR, docptr + DOC_FLASHCOMMAND);
  728. write_nop(docptr);
  729. /* only 2 bytes of address are written to specify erase block */
  730. g4_page = (uint16_t)(page / 4); /* to g4's 2k page addressing */
  731. writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
  732. g4_page >>= 8;
  733. writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
  734. write_nop(docptr);
  735. /* start the erasure */
  736. writew(DOC_CMD_ERASECYCLE2, docptr + DOC_FLASHCOMMAND);
  737. write_nop(docptr);
  738. write_nop(docptr);
  739. usleep_range(500, 1000); /* erasure is long; take a snooze */
  740. poll_status(doc);
  741. writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
  742. writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
  743. writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
  744. write_nop(docptr);
  745. write_nop(docptr);
  746. write_nop(docptr);
  747. write_nop(docptr);
  748. write_nop(docptr);
  749. read_progstatus(doc);
  750. writew(0, docptr + DOC_DATAEND);
  751. write_nop(docptr);
  752. poll_status(doc);
  753. write_nop(docptr);
  754. return nand->waitfunc(mtd, nand);
  755. }
  756. static int write_page(struct mtd_info *mtd, struct nand_chip *nand,
  757. const uint8_t *buf, bool use_ecc)
  758. {
  759. struct docg4_priv *doc = nand->priv;
  760. void __iomem *docptr = doc->virtadr;
  761. uint8_t ecc_buf[8];
  762. dev_dbg(doc->dev, "%s...\n", __func__);
  763. writew(DOC_ECCCONF0_ECC_ENABLE |
  764. DOC_ECCCONF0_UNKNOWN |
  765. DOCG4_BCH_SIZE,
  766. docptr + DOC_ECCCONF0);
  767. write_nop(docptr);
  768. /* write the page data */
  769. docg4_write_buf16(mtd, buf, DOCG4_PAGE_SIZE);
  770. /* oob bytes 0 through 5 are written to I/O reg */
  771. docg4_write_buf16(mtd, nand->oob_poi, 6);
  772. /* oob byte 6 written to a separate reg */
  773. writew(nand->oob_poi[6], docptr + DOCG4_OOB_6_7);
  774. write_nop(docptr);
  775. write_nop(docptr);
  776. /* write hw-generated ecc bytes to oob */
  777. if (likely(use_ecc == true)) {
  778. /* oob byte 7 is hamming code */
  779. uint8_t hamming = readb(docptr + DOC_HAMMINGPARITY);
  780. hamming = readb(docptr + DOC_HAMMINGPARITY); /* 2nd read */
  781. writew(hamming, docptr + DOCG4_OOB_6_7);
  782. write_nop(docptr);
  783. /* read the 7 bch bytes from ecc regs */
  784. read_hw_ecc(docptr, ecc_buf);
  785. ecc_buf[7] = 0; /* clear the "page written" flag */
  786. }
  787. /* write user-supplied bytes to oob */
  788. else {
  789. writew(nand->oob_poi[7], docptr + DOCG4_OOB_6_7);
  790. write_nop(docptr);
  791. memcpy(ecc_buf, &nand->oob_poi[8], 8);
  792. }
  793. docg4_write_buf16(mtd, ecc_buf, 8);
  794. write_nop(docptr);
  795. write_nop(docptr);
  796. writew(0, docptr + DOC_DATAEND);
  797. write_nop(docptr);
  798. return 0;
  799. }
  800. static int docg4_write_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
  801. const uint8_t *buf, int oob_required, int page)
  802. {
  803. return write_page(mtd, nand, buf, false);
  804. }
  805. static int docg4_write_page(struct mtd_info *mtd, struct nand_chip *nand,
  806. const uint8_t *buf, int oob_required, int page)
  807. {
  808. return write_page(mtd, nand, buf, true);
  809. }
  810. static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
  811. int page)
  812. {
  813. /*
  814. * Writing oob-only is not really supported, because MLC nand must write
  815. * oob bytes at the same time as page data. Nonetheless, we save the
  816. * oob buffer contents here, and then write it along with the page data
  817. * if the same page is subsequently written. This allows user space
  818. * utilities that write the oob data prior to the page data to work
  819. * (e.g., nandwrite). The disdvantage is that, if the intention was to
  820. * write oob only, the operation is quietly ignored. Also, oob can get
  821. * corrupted if two concurrent processes are running nandwrite.
  822. */
  823. /* note that bytes 7..14 are hw generated hamming/ecc and overwritten */
  824. struct docg4_priv *doc = nand->priv;
  825. doc->oob_page = page;
  826. memcpy(doc->oob_buf, nand->oob_poi, 16);
  827. return 0;
  828. }
  829. static int __init read_factory_bbt(struct mtd_info *mtd)
  830. {
  831. /*
  832. * The device contains a read-only factory bad block table. Read it and
  833. * update the memory-based bbt accordingly.
  834. */
  835. struct nand_chip *nand = mtd->priv;
  836. struct docg4_priv *doc = nand->priv;
  837. uint32_t g4_addr = mtd_to_docg4_address(DOCG4_FACTORY_BBT_PAGE, 0);
  838. uint8_t *buf;
  839. int i, block;
  840. __u32 eccfailed_stats = mtd->ecc_stats.failed;
  841. buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
  842. if (buf == NULL)
  843. return -ENOMEM;
  844. read_page_prologue(mtd, g4_addr);
  845. docg4_read_page(mtd, nand, buf, 0, DOCG4_FACTORY_BBT_PAGE);
  846. /*
  847. * If no memory-based bbt was created, exit. This will happen if module
  848. * parameter ignore_badblocks is set. Then why even call this function?
  849. * For an unknown reason, block erase always fails if it's the first
  850. * operation after device power-up. The above read ensures it never is.
  851. * Ugly, I know.
  852. */
  853. if (nand->bbt == NULL) /* no memory-based bbt */
  854. goto exit;
  855. if (mtd->ecc_stats.failed > eccfailed_stats) {
  856. /*
  857. * Whoops, an ecc failure ocurred reading the factory bbt.
  858. * It is stored redundantly, so we get another chance.
  859. */
  860. eccfailed_stats = mtd->ecc_stats.failed;
  861. docg4_read_page(mtd, nand, buf, 0, DOCG4_REDUNDANT_BBT_PAGE);
  862. if (mtd->ecc_stats.failed > eccfailed_stats) {
  863. dev_warn(doc->dev,
  864. "The factory bbt could not be read!\n");
  865. goto exit;
  866. }
  867. }
  868. /*
  869. * Parse factory bbt and update memory-based bbt. Factory bbt format is
  870. * simple: one bit per block, block numbers increase left to right (msb
  871. * to lsb). Bit clear means bad block.
  872. */
  873. for (i = block = 0; block < DOCG4_NUMBLOCKS; block += 8, i++) {
  874. int bitnum;
  875. unsigned long bits = ~buf[i];
  876. for_each_set_bit(bitnum, &bits, 8) {
  877. int badblock = block + 7 - bitnum;
  878. nand->bbt[badblock / 4] |=
  879. 0x03 << ((badblock % 4) * 2);
  880. mtd->ecc_stats.badblocks++;
  881. dev_notice(doc->dev, "factory-marked bad block: %d\n",
  882. badblock);
  883. }
  884. }
  885. exit:
  886. kfree(buf);
  887. return 0;
  888. }
  889. static int docg4_block_markbad(struct mtd_info *mtd, loff_t ofs)
  890. {
  891. /*
  892. * Mark a block as bad. Bad blocks are marked in the oob area of the
  893. * first page of the block. The default scan_bbt() in the nand
  894. * infrastructure code works fine for building the memory-based bbt
  895. * during initialization, as does the nand infrastructure function that
  896. * checks if a block is bad by reading the bbt. This function replaces
  897. * the nand default because writes to oob-only are not supported.
  898. */
  899. int ret, i;
  900. uint8_t *buf;
  901. struct nand_chip *nand = mtd->priv;
  902. struct docg4_priv *doc = nand->priv;
  903. struct nand_bbt_descr *bbtd = nand->badblock_pattern;
  904. int page = (int)(ofs >> nand->page_shift);
  905. uint32_t g4_addr = mtd_to_docg4_address(page, 0);
  906. dev_dbg(doc->dev, "%s: %08llx\n", __func__, ofs);
  907. if (unlikely(ofs & (DOCG4_BLOCK_SIZE - 1)))
  908. dev_warn(doc->dev, "%s: ofs %llx not start of block!\n",
  909. __func__, ofs);
  910. /* allocate blank buffer for page data */
  911. buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
  912. if (buf == NULL)
  913. return -ENOMEM;
  914. /* write bit-wise negation of pattern to oob buffer */
  915. memset(nand->oob_poi, 0xff, mtd->oobsize);
  916. for (i = 0; i < bbtd->len; i++)
  917. nand->oob_poi[bbtd->offs + i] = ~bbtd->pattern[i];
  918. /* write first page of block */
  919. write_page_prologue(mtd, g4_addr);
  920. docg4_write_page(mtd, nand, buf, 1, page);
  921. ret = pageprog(mtd);
  922. kfree(buf);
  923. return ret;
  924. }
  925. static int docg4_block_neverbad(struct mtd_info *mtd, loff_t ofs, int getchip)
  926. {
  927. /* only called when module_param ignore_badblocks is set */
  928. return 0;
  929. }
  930. static int docg4_suspend(struct platform_device *pdev, pm_message_t state)
  931. {
  932. /*
  933. * Put the device into "deep power-down" mode. Note that CE# must be
  934. * deasserted for this to take effect. The xscale, e.g., can be
  935. * configured to float this signal when the processor enters power-down,
  936. * and a suitable pull-up ensures its deassertion.
  937. */
  938. int i;
  939. uint8_t pwr_down;
  940. struct docg4_priv *doc = platform_get_drvdata(pdev);
  941. void __iomem *docptr = doc->virtadr;
  942. dev_dbg(doc->dev, "%s...\n", __func__);
  943. /* poll the register that tells us we're ready to go to sleep */
  944. for (i = 0; i < 10; i++) {
  945. pwr_down = readb(docptr + DOC_POWERMODE);
  946. if (pwr_down & DOC_POWERDOWN_READY)
  947. break;
  948. usleep_range(1000, 4000);
  949. }
  950. if (pwr_down & DOC_POWERDOWN_READY) {
  951. dev_err(doc->dev, "suspend failed; "
  952. "timeout polling DOC_POWERDOWN_READY\n");
  953. return -EIO;
  954. }
  955. writew(DOC_ASICMODE_POWERDOWN | DOC_ASICMODE_MDWREN,
  956. docptr + DOC_ASICMODE);
  957. writew(~(DOC_ASICMODE_POWERDOWN | DOC_ASICMODE_MDWREN),
  958. docptr + DOC_ASICMODECONFIRM);
  959. write_nop(docptr);
  960. return 0;
  961. }
  962. static int docg4_resume(struct platform_device *pdev)
  963. {
  964. /*
  965. * Exit power-down. Twelve consecutive reads of the address below
  966. * accomplishes this, assuming CE# has been asserted.
  967. */
  968. struct docg4_priv *doc = platform_get_drvdata(pdev);
  969. void __iomem *docptr = doc->virtadr;
  970. int i;
  971. dev_dbg(doc->dev, "%s...\n", __func__);
  972. for (i = 0; i < 12; i++)
  973. readb(docptr + 0x1fff);
  974. return 0;
  975. }
  976. static void __init init_mtd_structs(struct mtd_info *mtd)
  977. {
  978. /* initialize mtd and nand data structures */
  979. /*
  980. * Note that some of the following initializations are not usually
  981. * required within a nand driver because they are performed by the nand
  982. * infrastructure code as part of nand_scan(). In this case they need
  983. * to be initialized here because we skip call to nand_scan_ident() (the
  984. * first half of nand_scan()). The call to nand_scan_ident() is skipped
  985. * because for this device the chip id is not read in the manner of a
  986. * standard nand device. Unfortunately, nand_scan_ident() does other
  987. * things as well, such as call nand_set_defaults().
  988. */
  989. struct nand_chip *nand = mtd->priv;
  990. struct docg4_priv *doc = nand->priv;
  991. mtd->size = DOCG4_CHIP_SIZE;
  992. mtd->name = "Msys_Diskonchip_G4";
  993. mtd->writesize = DOCG4_PAGE_SIZE;
  994. mtd->erasesize = DOCG4_BLOCK_SIZE;
  995. mtd->oobsize = DOCG4_OOB_SIZE;
  996. nand->chipsize = DOCG4_CHIP_SIZE;
  997. nand->chip_shift = DOCG4_CHIP_SHIFT;
  998. nand->bbt_erase_shift = nand->phys_erase_shift = DOCG4_ERASE_SHIFT;
  999. nand->chip_delay = 20;
  1000. nand->page_shift = DOCG4_PAGE_SHIFT;
  1001. nand->pagemask = 0x3ffff;
  1002. nand->badblockpos = NAND_LARGE_BADBLOCK_POS;
  1003. nand->badblockbits = 8;
  1004. nand->ecc.layout = &docg4_oobinfo;
  1005. nand->ecc.mode = NAND_ECC_HW_SYNDROME;
  1006. nand->ecc.size = DOCG4_PAGE_SIZE;
  1007. nand->ecc.prepad = 8;
  1008. nand->ecc.bytes = 8;
  1009. nand->ecc.strength = DOCG4_T;
  1010. nand->options = NAND_BUSWIDTH_16 | NAND_NO_SUBPAGE_WRITE;
  1011. nand->IO_ADDR_R = nand->IO_ADDR_W = doc->virtadr + DOC_IOSPACE_DATA;
  1012. nand->controller = &nand->hwcontrol;
  1013. spin_lock_init(&nand->controller->lock);
  1014. init_waitqueue_head(&nand->controller->wq);
  1015. /* methods */
  1016. nand->cmdfunc = docg4_command;
  1017. nand->waitfunc = docg4_wait;
  1018. nand->select_chip = docg4_select_chip;
  1019. nand->read_byte = docg4_read_byte;
  1020. nand->block_markbad = docg4_block_markbad;
  1021. nand->read_buf = docg4_read_buf;
  1022. nand->write_buf = docg4_write_buf16;
  1023. nand->erase = docg4_erase_block;
  1024. nand->ecc.read_page = docg4_read_page;
  1025. nand->ecc.write_page = docg4_write_page;
  1026. nand->ecc.read_page_raw = docg4_read_page_raw;
  1027. nand->ecc.write_page_raw = docg4_write_page_raw;
  1028. nand->ecc.read_oob = docg4_read_oob;
  1029. nand->ecc.write_oob = docg4_write_oob;
  1030. /*
  1031. * The way the nand infrastructure code is written, a memory-based bbt
  1032. * is not created if NAND_SKIP_BBTSCAN is set. With no memory bbt,
  1033. * nand->block_bad() is used. So when ignoring bad blocks, we skip the
  1034. * scan and define a dummy block_bad() which always returns 0.
  1035. */
  1036. if (ignore_badblocks) {
  1037. nand->options |= NAND_SKIP_BBTSCAN;
  1038. nand->block_bad = docg4_block_neverbad;
  1039. }
  1040. }
  1041. static int __init read_id_reg(struct mtd_info *mtd)
  1042. {
  1043. struct nand_chip *nand = mtd->priv;
  1044. struct docg4_priv *doc = nand->priv;
  1045. void __iomem *docptr = doc->virtadr;
  1046. uint16_t id1, id2;
  1047. /* check for presence of g4 chip by reading id registers */
  1048. id1 = readw(docptr + DOC_CHIPID);
  1049. id1 = readw(docptr + DOCG4_MYSTERY_REG);
  1050. id2 = readw(docptr + DOC_CHIPID_INV);
  1051. id2 = readw(docptr + DOCG4_MYSTERY_REG);
  1052. if (id1 == DOCG4_IDREG1_VALUE && id2 == DOCG4_IDREG2_VALUE) {
  1053. dev_info(doc->dev,
  1054. "NAND device: 128MiB Diskonchip G4 detected\n");
  1055. return 0;
  1056. }
  1057. return -ENODEV;
  1058. }
  1059. static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
  1060. static int __init probe_docg4(struct platform_device *pdev)
  1061. {
  1062. struct mtd_info *mtd;
  1063. struct nand_chip *nand;
  1064. void __iomem *virtadr;
  1065. struct docg4_priv *doc;
  1066. int len, retval;
  1067. struct resource *r;
  1068. struct device *dev = &pdev->dev;
  1069. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1070. if (r == NULL) {
  1071. dev_err(dev, "no io memory resource defined!\n");
  1072. return -ENODEV;
  1073. }
  1074. virtadr = ioremap(r->start, resource_size(r));
  1075. if (!virtadr) {
  1076. dev_err(dev, "Diskonchip ioremap failed: %pR\n", r);
  1077. return -EIO;
  1078. }
  1079. len = sizeof(struct mtd_info) + sizeof(struct nand_chip) +
  1080. sizeof(struct docg4_priv);
  1081. mtd = kzalloc(len, GFP_KERNEL);
  1082. if (mtd == NULL) {
  1083. retval = -ENOMEM;
  1084. goto fail;
  1085. }
  1086. nand = (struct nand_chip *) (mtd + 1);
  1087. doc = (struct docg4_priv *) (nand + 1);
  1088. mtd->priv = nand;
  1089. nand->priv = doc;
  1090. mtd->dev.parent = &pdev->dev;
  1091. doc->virtadr = virtadr;
  1092. doc->dev = dev;
  1093. init_mtd_structs(mtd);
  1094. /* initialize kernel bch algorithm */
  1095. doc->bch = init_bch(DOCG4_M, DOCG4_T, DOCG4_PRIMITIVE_POLY);
  1096. if (doc->bch == NULL) {
  1097. retval = -EINVAL;
  1098. goto fail;
  1099. }
  1100. platform_set_drvdata(pdev, doc);
  1101. reset(mtd);
  1102. retval = read_id_reg(mtd);
  1103. if (retval == -ENODEV) {
  1104. dev_warn(dev, "No diskonchip G4 device found.\n");
  1105. goto fail;
  1106. }
  1107. retval = nand_scan_tail(mtd);
  1108. if (retval)
  1109. goto fail;
  1110. retval = read_factory_bbt(mtd);
  1111. if (retval)
  1112. goto fail;
  1113. retval = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
  1114. if (retval)
  1115. goto fail;
  1116. doc->mtd = mtd;
  1117. return 0;
  1118. fail:
  1119. iounmap(virtadr);
  1120. if (mtd) {
  1121. /* re-declarations avoid compiler warning */
  1122. struct nand_chip *nand = mtd->priv;
  1123. struct docg4_priv *doc = nand->priv;
  1124. nand_release(mtd); /* deletes partitions and mtd devices */
  1125. free_bch(doc->bch);
  1126. kfree(mtd);
  1127. }
  1128. return retval;
  1129. }
  1130. static int __exit cleanup_docg4(struct platform_device *pdev)
  1131. {
  1132. struct docg4_priv *doc = platform_get_drvdata(pdev);
  1133. nand_release(doc->mtd);
  1134. free_bch(doc->bch);
  1135. kfree(doc->mtd);
  1136. iounmap(doc->virtadr);
  1137. return 0;
  1138. }
  1139. static struct platform_driver docg4_driver = {
  1140. .driver = {
  1141. .name = "docg4",
  1142. },
  1143. .suspend = docg4_suspend,
  1144. .resume = docg4_resume,
  1145. .remove = __exit_p(cleanup_docg4),
  1146. };
  1147. module_platform_driver_probe(docg4_driver, probe_docg4);
  1148. MODULE_LICENSE("GPL");
  1149. MODULE_AUTHOR("Mike Dunn");
  1150. MODULE_DESCRIPTION("M-Systems DiskOnChip G4 device driver");