docg4.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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. /*
  47. * In "reliable mode" consecutive 2k pages are used in parallel (in some
  48. * fashion) to store the same data. The data can be read back from the
  49. * even-numbered pages in the normal manner; odd-numbered pages will appear to
  50. * contain junk. Systems that boot from the docg4 typically write the secondary
  51. * program loader (SPL) code in this mode. The SPL is loaded by the initial
  52. * program loader (IPL, stored in the docg4's 2k NOR-like region that is mapped
  53. * to the reset vector address). This module parameter enables you to use this
  54. * driver to write the SPL. When in this mode, no more than 2k of data can be
  55. * written at a time, because the addresses do not increment in the normal
  56. * manner, and the starting offset must be within an even-numbered 2k region;
  57. * i.e., invalid starting offsets are 0x800, 0xa00, 0xc00, 0xe00, 0x1800,
  58. * 0x1a00, ... Reliable mode is a special case and should not be used unless
  59. * you know what you're doing.
  60. */
  61. static bool reliable_mode;
  62. module_param(reliable_mode, bool, 0);
  63. MODULE_PARM_DESC(reliable_mode, "pages are programmed in reliable mode");
  64. /*
  65. * You'll want to ignore badblocks if you're reading a partition that contains
  66. * data written by the TrueFFS library (i.e., by PalmOS, Windows, etc), since
  67. * it does not use mtd nand's method for marking bad blocks (using oob area).
  68. * This will also skip the check of the "page written" flag.
  69. */
  70. static bool ignore_badblocks;
  71. module_param(ignore_badblocks, bool, 0);
  72. MODULE_PARM_DESC(ignore_badblocks, "no badblock checking performed");
  73. struct docg4_priv {
  74. struct mtd_info *mtd;
  75. struct device *dev;
  76. void __iomem *virtadr;
  77. int status;
  78. struct {
  79. unsigned int command;
  80. int column;
  81. int page;
  82. } last_command;
  83. uint8_t oob_buf[16];
  84. uint8_t ecc_buf[7];
  85. int oob_page;
  86. struct bch_control *bch;
  87. };
  88. /*
  89. * Defines prefixed with DOCG4 are unique to the diskonchip G4. All others are
  90. * shared with other diskonchip devices (P3, G3 at least).
  91. *
  92. * Functions with names prefixed with docg4_ are mtd / nand interface functions
  93. * (though they may also be called internally). All others are internal.
  94. */
  95. #define DOC_IOSPACE_DATA 0x0800
  96. /* register offsets */
  97. #define DOC_CHIPID 0x1000
  98. #define DOC_DEVICESELECT 0x100a
  99. #define DOC_ASICMODE 0x100c
  100. #define DOC_DATAEND 0x101e
  101. #define DOC_NOP 0x103e
  102. #define DOC_FLASHSEQUENCE 0x1032
  103. #define DOC_FLASHCOMMAND 0x1034
  104. #define DOC_FLASHADDRESS 0x1036
  105. #define DOC_FLASHCONTROL 0x1038
  106. #define DOC_ECCCONF0 0x1040
  107. #define DOC_ECCCONF1 0x1042
  108. #define DOC_HAMMINGPARITY 0x1046
  109. #define DOC_BCH_SYNDROM(idx) (0x1048 + idx)
  110. #define DOC_ASICMODECONFIRM 0x1072
  111. #define DOC_CHIPID_INV 0x1074
  112. #define DOC_POWERMODE 0x107c
  113. #define DOCG4_MYSTERY_REG 0x1050
  114. /* apparently used only to write oob bytes 6 and 7 */
  115. #define DOCG4_OOB_6_7 0x1052
  116. /* DOC_FLASHSEQUENCE register commands */
  117. #define DOC_SEQ_RESET 0x00
  118. #define DOCG4_SEQ_PAGE_READ 0x03
  119. #define DOCG4_SEQ_FLUSH 0x29
  120. #define DOCG4_SEQ_PAGEWRITE 0x16
  121. #define DOCG4_SEQ_PAGEPROG 0x1e
  122. #define DOCG4_SEQ_BLOCKERASE 0x24
  123. #define DOCG4_SEQ_SETMODE 0x45
  124. /* DOC_FLASHCOMMAND register commands */
  125. #define DOCG4_CMD_PAGE_READ 0x00
  126. #define DOC_CMD_ERASECYCLE2 0xd0
  127. #define DOCG4_CMD_FLUSH 0x70
  128. #define DOCG4_CMD_READ2 0x30
  129. #define DOC_CMD_PROG_BLOCK_ADDR 0x60
  130. #define DOCG4_CMD_PAGEWRITE 0x80
  131. #define DOC_CMD_PROG_CYCLE2 0x10
  132. #define DOCG4_CMD_FAST_MODE 0xa3 /* functionality guessed */
  133. #define DOC_CMD_RELIABLE_MODE 0x22
  134. #define DOC_CMD_RESET 0xff
  135. /* DOC_POWERMODE register bits */
  136. #define DOC_POWERDOWN_READY 0x80
  137. /* DOC_FLASHCONTROL register bits */
  138. #define DOC_CTRL_CE 0x10
  139. #define DOC_CTRL_UNKNOWN 0x40
  140. #define DOC_CTRL_FLASHREADY 0x01
  141. /* DOC_ECCCONF0 register bits */
  142. #define DOC_ECCCONF0_READ_MODE 0x8000
  143. #define DOC_ECCCONF0_UNKNOWN 0x2000
  144. #define DOC_ECCCONF0_ECC_ENABLE 0x1000
  145. #define DOC_ECCCONF0_DATA_BYTES_MASK 0x07ff
  146. /* DOC_ECCCONF1 register bits */
  147. #define DOC_ECCCONF1_BCH_SYNDROM_ERR 0x80
  148. #define DOC_ECCCONF1_ECC_ENABLE 0x07
  149. #define DOC_ECCCONF1_PAGE_IS_WRITTEN 0x20
  150. /* DOC_ASICMODE register bits */
  151. #define DOC_ASICMODE_RESET 0x00
  152. #define DOC_ASICMODE_NORMAL 0x01
  153. #define DOC_ASICMODE_POWERDOWN 0x02
  154. #define DOC_ASICMODE_MDWREN 0x04
  155. #define DOC_ASICMODE_BDETCT_RESET 0x08
  156. #define DOC_ASICMODE_RSTIN_RESET 0x10
  157. #define DOC_ASICMODE_RAM_WE 0x20
  158. /* good status values read after read/write/erase operations */
  159. #define DOCG4_PROGSTATUS_GOOD 0x51
  160. #define DOCG4_PROGSTATUS_GOOD_2 0xe0
  161. /*
  162. * On read operations (page and oob-only), the first byte read from I/O reg is a
  163. * status. On error, it reads 0x73; otherwise, it reads either 0x71 (first read
  164. * after reset only) or 0x51, so bit 1 is presumed to be an error indicator.
  165. */
  166. #define DOCG4_READ_ERROR 0x02 /* bit 1 indicates read error */
  167. /* anatomy of the device */
  168. #define DOCG4_CHIP_SIZE 0x8000000
  169. #define DOCG4_PAGE_SIZE 0x200
  170. #define DOCG4_PAGES_PER_BLOCK 0x200
  171. #define DOCG4_BLOCK_SIZE (DOCG4_PAGES_PER_BLOCK * DOCG4_PAGE_SIZE)
  172. #define DOCG4_NUMBLOCKS (DOCG4_CHIP_SIZE / DOCG4_BLOCK_SIZE)
  173. #define DOCG4_OOB_SIZE 0x10
  174. #define DOCG4_CHIP_SHIFT 27 /* log_2(DOCG4_CHIP_SIZE) */
  175. #define DOCG4_PAGE_SHIFT 9 /* log_2(DOCG4_PAGE_SIZE) */
  176. #define DOCG4_ERASE_SHIFT 18 /* log_2(DOCG4_BLOCK_SIZE) */
  177. /* all but the last byte is included in ecc calculation */
  178. #define DOCG4_BCH_SIZE (DOCG4_PAGE_SIZE + DOCG4_OOB_SIZE - 1)
  179. #define DOCG4_USERDATA_LEN 520 /* 512 byte page plus 8 oob avail to user */
  180. /* expected values from the ID registers */
  181. #define DOCG4_IDREG1_VALUE 0x0400
  182. #define DOCG4_IDREG2_VALUE 0xfbff
  183. /* primitive polynomial used to build the Galois field used by hw ecc gen */
  184. #define DOCG4_PRIMITIVE_POLY 0x4443
  185. #define DOCG4_M 14 /* Galois field is of order 2^14 */
  186. #define DOCG4_T 4 /* BCH alg corrects up to 4 bit errors */
  187. #define DOCG4_FACTORY_BBT_PAGE 16 /* page where read-only factory bbt lives */
  188. #define DOCG4_REDUNDANT_BBT_PAGE 24 /* page where redundant factory bbt lives */
  189. /*
  190. * Bytes 0, 1 are used as badblock marker.
  191. * Bytes 2 - 6 are available to the user.
  192. * Byte 7 is hamming ecc for first 7 oob bytes only.
  193. * Bytes 8 - 14 are hw-generated ecc covering entire page + oob bytes 0 - 14.
  194. * Byte 15 (the last) is used by the driver as a "page written" flag.
  195. */
  196. static struct nand_ecclayout docg4_oobinfo = {
  197. .eccbytes = 9,
  198. .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
  199. .oobavail = 5,
  200. .oobfree = { {.offset = 2, .length = 5} }
  201. };
  202. /*
  203. * The device has a nop register which M-Sys claims is for the purpose of
  204. * inserting precise delays. But beware; at least some operations fail if the
  205. * nop writes are replaced with a generic delay!
  206. */
  207. static inline void write_nop(void __iomem *docptr)
  208. {
  209. writew(0, docptr + DOC_NOP);
  210. }
  211. static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  212. {
  213. int i;
  214. struct nand_chip *nand = mtd->priv;
  215. uint16_t *p = (uint16_t *) buf;
  216. len >>= 1;
  217. for (i = 0; i < len; i++)
  218. p[i] = readw(nand->IO_ADDR_R);
  219. }
  220. static void docg4_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  221. {
  222. int i;
  223. struct nand_chip *nand = mtd->priv;
  224. uint16_t *p = (uint16_t *) buf;
  225. len >>= 1;
  226. for (i = 0; i < len; i++)
  227. writew(p[i], nand->IO_ADDR_W);
  228. }
  229. static int poll_status(struct docg4_priv *doc)
  230. {
  231. /*
  232. * Busy-wait for the FLASHREADY bit to be set in the FLASHCONTROL
  233. * register. Operations known to take a long time (e.g., block erase)
  234. * should sleep for a while before calling this.
  235. */
  236. uint16_t flash_status;
  237. unsigned int timeo;
  238. void __iomem *docptr = doc->virtadr;
  239. dev_dbg(doc->dev, "%s...\n", __func__);
  240. /* hardware quirk requires reading twice initially */
  241. flash_status = readw(docptr + DOC_FLASHCONTROL);
  242. timeo = 1000;
  243. do {
  244. cpu_relax();
  245. flash_status = readb(docptr + DOC_FLASHCONTROL);
  246. } while (!(flash_status & DOC_CTRL_FLASHREADY) && --timeo);
  247. if (!timeo) {
  248. dev_err(doc->dev, "%s: timed out!\n", __func__);
  249. return NAND_STATUS_FAIL;
  250. }
  251. if (unlikely(timeo < 50))
  252. dev_warn(doc->dev, "%s: nearly timed out; %d remaining\n",
  253. __func__, timeo);
  254. return 0;
  255. }
  256. static int docg4_wait(struct mtd_info *mtd, struct nand_chip *nand)
  257. {
  258. struct docg4_priv *doc = nand->priv;
  259. int status = NAND_STATUS_WP; /* inverse logic?? */
  260. dev_dbg(doc->dev, "%s...\n", __func__);
  261. /* report any previously unreported error */
  262. if (doc->status) {
  263. status |= doc->status;
  264. doc->status = 0;
  265. return status;
  266. }
  267. status |= poll_status(doc);
  268. return status;
  269. }
  270. static void docg4_select_chip(struct mtd_info *mtd, int chip)
  271. {
  272. /*
  273. * Select among multiple cascaded chips ("floors"). Multiple floors are
  274. * not yet supported, so the only valid non-negative value is 0.
  275. */
  276. struct nand_chip *nand = mtd->priv;
  277. struct docg4_priv *doc = nand->priv;
  278. void __iomem *docptr = doc->virtadr;
  279. dev_dbg(doc->dev, "%s: chip %d\n", __func__, chip);
  280. if (chip < 0)
  281. return; /* deselected */
  282. if (chip > 0)
  283. dev_warn(doc->dev, "multiple floors currently unsupported\n");
  284. writew(0, docptr + DOC_DEVICESELECT);
  285. }
  286. static void reset(struct mtd_info *mtd)
  287. {
  288. /* full device reset */
  289. struct nand_chip *nand = mtd->priv;
  290. struct docg4_priv *doc = nand->priv;
  291. void __iomem *docptr = doc->virtadr;
  292. writew(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN,
  293. docptr + DOC_ASICMODE);
  294. writew(~(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN),
  295. docptr + DOC_ASICMODECONFIRM);
  296. write_nop(docptr);
  297. writew(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN,
  298. docptr + DOC_ASICMODE);
  299. writew(~(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN),
  300. docptr + DOC_ASICMODECONFIRM);
  301. writew(DOC_ECCCONF1_ECC_ENABLE, docptr + DOC_ECCCONF1);
  302. poll_status(doc);
  303. }
  304. static void read_hw_ecc(void __iomem *docptr, uint8_t *ecc_buf)
  305. {
  306. /* read the 7 hw-generated ecc bytes */
  307. int i;
  308. for (i = 0; i < 7; i++) { /* hw quirk; read twice */
  309. ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
  310. ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
  311. }
  312. }
  313. static int correct_data(struct mtd_info *mtd, uint8_t *buf, int page)
  314. {
  315. /*
  316. * Called after a page read when hardware reports bitflips.
  317. * Up to four bitflips can be corrected.
  318. */
  319. struct nand_chip *nand = mtd->priv;
  320. struct docg4_priv *doc = nand->priv;
  321. void __iomem *docptr = doc->virtadr;
  322. int i, numerrs, errpos[4];
  323. const uint8_t blank_read_hwecc[8] = {
  324. 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
  325. read_hw_ecc(docptr, doc->ecc_buf); /* read 7 hw-generated ecc bytes */
  326. /* check if read error is due to a blank page */
  327. if (!memcmp(doc->ecc_buf, blank_read_hwecc, 7))
  328. return 0; /* yes */
  329. /* skip additional check of "written flag" if ignore_badblocks */
  330. if (ignore_badblocks == false) {
  331. /*
  332. * If the hw ecc bytes are not those of a blank page, there's
  333. * still a chance that the page is blank, but was read with
  334. * errors. Check the "written flag" in last oob byte, which
  335. * is set to zero when a page is written. If more than half
  336. * the bits are set, assume a blank page. Unfortunately, the
  337. * bit flips(s) are not reported in stats.
  338. */
  339. if (nand->oob_poi[15]) {
  340. int bit, numsetbits = 0;
  341. unsigned long written_flag = nand->oob_poi[15];
  342. for_each_set_bit(bit, &written_flag, 8)
  343. numsetbits++;
  344. if (numsetbits > 4) { /* assume blank */
  345. dev_warn(doc->dev,
  346. "error(s) in blank page "
  347. "at offset %08x\n",
  348. page * DOCG4_PAGE_SIZE);
  349. return 0;
  350. }
  351. }
  352. }
  353. /*
  354. * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
  355. * algorithm is used to decode this. However the hw operates on page
  356. * data in a bit order that is the reverse of that of the bch alg,
  357. * requiring that the bits be reversed on the result. Thanks to Ivan
  358. * Djelic for his analysis!
  359. */
  360. for (i = 0; i < 7; i++)
  361. doc->ecc_buf[i] = bitrev8(doc->ecc_buf[i]);
  362. numerrs = decode_bch(doc->bch, NULL, DOCG4_USERDATA_LEN, NULL,
  363. doc->ecc_buf, NULL, errpos);
  364. if (numerrs == -EBADMSG) {
  365. dev_warn(doc->dev, "uncorrectable errors at offset %08x\n",
  366. page * DOCG4_PAGE_SIZE);
  367. return -EBADMSG;
  368. }
  369. BUG_ON(numerrs < 0); /* -EINVAL, or anything other than -EBADMSG */
  370. /* undo last step in BCH alg (modulo mirroring not needed) */
  371. for (i = 0; i < numerrs; i++)
  372. errpos[i] = (errpos[i] & ~7)|(7-(errpos[i] & 7));
  373. /* fix the errors */
  374. for (i = 0; i < numerrs; i++) {
  375. /* ignore if error within oob ecc bytes */
  376. if (errpos[i] > DOCG4_USERDATA_LEN * 8)
  377. continue;
  378. /* if error within oob area preceeding ecc bytes... */
  379. if (errpos[i] > DOCG4_PAGE_SIZE * 8)
  380. change_bit(errpos[i] - DOCG4_PAGE_SIZE * 8,
  381. (unsigned long *)nand->oob_poi);
  382. else /* error in page data */
  383. change_bit(errpos[i], (unsigned long *)buf);
  384. }
  385. dev_notice(doc->dev, "%d error(s) corrected at offset %08x\n",
  386. numerrs, page * DOCG4_PAGE_SIZE);
  387. return numerrs;
  388. }
  389. static uint8_t docg4_read_byte(struct mtd_info *mtd)
  390. {
  391. struct nand_chip *nand = mtd->priv;
  392. struct docg4_priv *doc = nand->priv;
  393. dev_dbg(doc->dev, "%s\n", __func__);
  394. if (doc->last_command.command == NAND_CMD_STATUS) {
  395. int status;
  396. /*
  397. * Previous nand command was status request, so nand
  398. * infrastructure code expects to read the status here. If an
  399. * error occurred in a previous operation, report it.
  400. */
  401. doc->last_command.command = 0;
  402. if (doc->status) {
  403. status = doc->status;
  404. doc->status = 0;
  405. }
  406. /* why is NAND_STATUS_WP inverse logic?? */
  407. else
  408. status = NAND_STATUS_WP | NAND_STATUS_READY;
  409. return status;
  410. }
  411. dev_warn(doc->dev, "unexpectd call to read_byte()\n");
  412. return 0;
  413. }
  414. static void write_addr(struct docg4_priv *doc, uint32_t docg4_addr)
  415. {
  416. /* write the four address bytes packed in docg4_addr to the device */
  417. void __iomem *docptr = doc->virtadr;
  418. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  419. docg4_addr >>= 8;
  420. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  421. docg4_addr >>= 8;
  422. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  423. docg4_addr >>= 8;
  424. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  425. }
  426. static int read_progstatus(struct docg4_priv *doc)
  427. {
  428. /*
  429. * This apparently checks the status of programming. Done after an
  430. * erasure, and after page data is written. On error, the status is
  431. * saved, to be later retrieved by the nand infrastructure code.
  432. */
  433. void __iomem *docptr = doc->virtadr;
  434. /* status is read from the I/O reg */
  435. uint16_t status1 = readw(docptr + DOC_IOSPACE_DATA);
  436. uint16_t status2 = readw(docptr + DOC_IOSPACE_DATA);
  437. uint16_t status3 = readw(docptr + DOCG4_MYSTERY_REG);
  438. dev_dbg(doc->dev, "docg4: %s: %02x %02x %02x\n",
  439. __func__, status1, status2, status3);
  440. if (status1 != DOCG4_PROGSTATUS_GOOD
  441. || status2 != DOCG4_PROGSTATUS_GOOD_2
  442. || status3 != DOCG4_PROGSTATUS_GOOD_2) {
  443. doc->status = NAND_STATUS_FAIL;
  444. dev_warn(doc->dev, "read_progstatus failed: "
  445. "%02x, %02x, %02x\n", status1, status2, status3);
  446. return -EIO;
  447. }
  448. return 0;
  449. }
  450. static int pageprog(struct mtd_info *mtd)
  451. {
  452. /*
  453. * Final step in writing a page. Writes the contents of its
  454. * internal buffer out to the flash array, or some such.
  455. */
  456. struct nand_chip *nand = mtd->priv;
  457. struct docg4_priv *doc = nand->priv;
  458. void __iomem *docptr = doc->virtadr;
  459. int retval = 0;
  460. dev_dbg(doc->dev, "docg4: %s\n", __func__);
  461. writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
  462. writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
  463. write_nop(docptr);
  464. write_nop(docptr);
  465. /* Just busy-wait; usleep_range() slows things down noticeably. */
  466. poll_status(doc);
  467. writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
  468. writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
  469. writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
  470. write_nop(docptr);
  471. write_nop(docptr);
  472. write_nop(docptr);
  473. write_nop(docptr);
  474. write_nop(docptr);
  475. retval = read_progstatus(doc);
  476. writew(0, docptr + DOC_DATAEND);
  477. write_nop(docptr);
  478. poll_status(doc);
  479. write_nop(docptr);
  480. return retval;
  481. }
  482. static void sequence_reset(struct mtd_info *mtd)
  483. {
  484. /* common starting sequence for all operations */
  485. struct nand_chip *nand = mtd->priv;
  486. struct docg4_priv *doc = nand->priv;
  487. void __iomem *docptr = doc->virtadr;
  488. writew(DOC_CTRL_UNKNOWN | DOC_CTRL_CE, docptr + DOC_FLASHCONTROL);
  489. writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
  490. writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
  491. write_nop(docptr);
  492. write_nop(docptr);
  493. poll_status(doc);
  494. write_nop(docptr);
  495. }
  496. static void read_page_prologue(struct mtd_info *mtd, uint32_t docg4_addr)
  497. {
  498. /* first step in reading a page */
  499. struct nand_chip *nand = mtd->priv;
  500. struct docg4_priv *doc = nand->priv;
  501. void __iomem *docptr = doc->virtadr;
  502. dev_dbg(doc->dev,
  503. "docg4: %s: g4 page %08x\n", __func__, docg4_addr);
  504. sequence_reset(mtd);
  505. writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
  506. writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
  507. write_nop(docptr);
  508. write_addr(doc, docg4_addr);
  509. write_nop(docptr);
  510. writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
  511. write_nop(docptr);
  512. write_nop(docptr);
  513. poll_status(doc);
  514. }
  515. static void write_page_prologue(struct mtd_info *mtd, uint32_t docg4_addr)
  516. {
  517. /* first step in writing a page */
  518. struct nand_chip *nand = mtd->priv;
  519. struct docg4_priv *doc = nand->priv;
  520. void __iomem *docptr = doc->virtadr;
  521. dev_dbg(doc->dev,
  522. "docg4: %s: g4 addr: %x\n", __func__, docg4_addr);
  523. sequence_reset(mtd);
  524. if (unlikely(reliable_mode)) {
  525. writew(DOCG4_SEQ_SETMODE, docptr + DOC_FLASHSEQUENCE);
  526. writew(DOCG4_CMD_FAST_MODE, docptr + DOC_FLASHCOMMAND);
  527. writew(DOC_CMD_RELIABLE_MODE, docptr + DOC_FLASHCOMMAND);
  528. write_nop(docptr);
  529. }
  530. writew(DOCG4_SEQ_PAGEWRITE, docptr + DOC_FLASHSEQUENCE);
  531. writew(DOCG4_CMD_PAGEWRITE, docptr + DOC_FLASHCOMMAND);
  532. write_nop(docptr);
  533. write_addr(doc, docg4_addr);
  534. write_nop(docptr);
  535. write_nop(docptr);
  536. poll_status(doc);
  537. }
  538. static uint32_t mtd_to_docg4_address(int page, int column)
  539. {
  540. /*
  541. * Convert mtd address to format used by the device, 32 bit packed.
  542. *
  543. * Some notes on G4 addressing... The M-Sys documentation on this device
  544. * claims that pages are 2K in length, and indeed, the format of the
  545. * address used by the device reflects that. But within each page are
  546. * four 512 byte "sub-pages", each with its own oob data that is
  547. * read/written immediately after the 512 bytes of page data. This oob
  548. * data contains the ecc bytes for the preceeding 512 bytes.
  549. *
  550. * Rather than tell the mtd nand infrastructure that page size is 2k,
  551. * with four sub-pages each, we engage in a little subterfuge and tell
  552. * the infrastructure code that pages are 512 bytes in size. This is
  553. * done because during the course of reverse-engineering the device, I
  554. * never observed an instance where an entire 2K "page" was read or
  555. * written as a unit. Each "sub-page" is always addressed individually,
  556. * its data read/written, and ecc handled before the next "sub-page" is
  557. * addressed.
  558. *
  559. * This requires us to convert addresses passed by the mtd nand
  560. * infrastructure code to those used by the device.
  561. *
  562. * The address that is written to the device consists of four bytes: the
  563. * first two are the 2k page number, and the second is the index into
  564. * the page. The index is in terms of 16-bit half-words and includes
  565. * the preceeding oob data, so e.g., the index into the second
  566. * "sub-page" is 0x108, and the full device address of the start of mtd
  567. * page 0x201 is 0x00800108.
  568. */
  569. int g4_page = page / 4; /* device's 2K page */
  570. int g4_index = (page % 4) * 0x108 + column/2; /* offset into page */
  571. return (g4_page << 16) | g4_index; /* pack */
  572. }
  573. static void docg4_command(struct mtd_info *mtd, unsigned command, int column,
  574. int page_addr)
  575. {
  576. /* handle standard nand commands */
  577. struct nand_chip *nand = mtd->priv;
  578. struct docg4_priv *doc = nand->priv;
  579. uint32_t g4_addr = mtd_to_docg4_address(page_addr, column);
  580. dev_dbg(doc->dev, "%s %x, page_addr=%x, column=%x\n",
  581. __func__, command, page_addr, column);
  582. /*
  583. * Save the command and its arguments. This enables emulation of
  584. * standard flash devices, and also some optimizations.
  585. */
  586. doc->last_command.command = command;
  587. doc->last_command.column = column;
  588. doc->last_command.page = page_addr;
  589. switch (command) {
  590. case NAND_CMD_RESET:
  591. reset(mtd);
  592. break;
  593. case NAND_CMD_READ0:
  594. read_page_prologue(mtd, g4_addr);
  595. break;
  596. case NAND_CMD_STATUS:
  597. /* next call to read_byte() will expect a status */
  598. break;
  599. case NAND_CMD_SEQIN:
  600. if (unlikely(reliable_mode)) {
  601. uint16_t g4_page = g4_addr >> 16;
  602. /* writes to odd-numbered 2k pages are invalid */
  603. if (g4_page & 0x01)
  604. dev_warn(doc->dev,
  605. "invalid reliable mode address\n");
  606. }
  607. write_page_prologue(mtd, g4_addr);
  608. /* hack for deferred write of oob bytes */
  609. if (doc->oob_page == page_addr)
  610. memcpy(nand->oob_poi, doc->oob_buf, 16);
  611. break;
  612. case NAND_CMD_PAGEPROG:
  613. pageprog(mtd);
  614. break;
  615. /* we don't expect these, based on review of nand_base.c */
  616. case NAND_CMD_READOOB:
  617. case NAND_CMD_READID:
  618. case NAND_CMD_ERASE1:
  619. case NAND_CMD_ERASE2:
  620. dev_warn(doc->dev, "docg4_command: "
  621. "unexpected nand command 0x%x\n", command);
  622. break;
  623. }
  624. }
  625. static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
  626. uint8_t *buf, int page, bool use_ecc)
  627. {
  628. struct docg4_priv *doc = nand->priv;
  629. void __iomem *docptr = doc->virtadr;
  630. uint16_t status, edc_err, *buf16;
  631. int bits_corrected = 0;
  632. dev_dbg(doc->dev, "%s: page %08x\n", __func__, page);
  633. writew(DOC_ECCCONF0_READ_MODE |
  634. DOC_ECCCONF0_ECC_ENABLE |
  635. DOC_ECCCONF0_UNKNOWN |
  636. DOCG4_BCH_SIZE,
  637. docptr + DOC_ECCCONF0);
  638. write_nop(docptr);
  639. write_nop(docptr);
  640. write_nop(docptr);
  641. write_nop(docptr);
  642. write_nop(docptr);
  643. /* the 1st byte from the I/O reg is a status; the rest is page data */
  644. status = readw(docptr + DOC_IOSPACE_DATA);
  645. if (status & DOCG4_READ_ERROR) {
  646. dev_err(doc->dev,
  647. "docg4_read_page: bad status: 0x%02x\n", status);
  648. writew(0, docptr + DOC_DATAEND);
  649. return -EIO;
  650. }
  651. dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
  652. docg4_read_buf(mtd, buf, DOCG4_PAGE_SIZE); /* read the page data */
  653. /* this device always reads oob after page data */
  654. /* first 14 oob bytes read from I/O reg */
  655. docg4_read_buf(mtd, nand->oob_poi, 14);
  656. /* last 2 read from another reg */
  657. buf16 = (uint16_t *)(nand->oob_poi + 14);
  658. *buf16 = readw(docptr + DOCG4_MYSTERY_REG);
  659. write_nop(docptr);
  660. if (likely(use_ecc == true)) {
  661. /* read the register that tells us if bitflip(s) detected */
  662. edc_err = readw(docptr + DOC_ECCCONF1);
  663. edc_err = readw(docptr + DOC_ECCCONF1);
  664. dev_dbg(doc->dev, "%s: edc_err = 0x%02x\n", __func__, edc_err);
  665. /* If bitflips are reported, attempt to correct with ecc */
  666. if (edc_err & DOC_ECCCONF1_BCH_SYNDROM_ERR) {
  667. bits_corrected = correct_data(mtd, buf, page);
  668. if (bits_corrected == -EBADMSG)
  669. mtd->ecc_stats.failed++;
  670. else
  671. mtd->ecc_stats.corrected += bits_corrected;
  672. }
  673. }
  674. writew(0, docptr + DOC_DATAEND);
  675. if (bits_corrected == -EBADMSG) /* uncorrectable errors */
  676. return 0;
  677. return bits_corrected;
  678. }
  679. static int docg4_read_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
  680. uint8_t *buf, int oob_required, int page)
  681. {
  682. return read_page(mtd, nand, buf, page, false);
  683. }
  684. static int docg4_read_page(struct mtd_info *mtd, struct nand_chip *nand,
  685. uint8_t *buf, int oob_required, int page)
  686. {
  687. return read_page(mtd, nand, buf, page, true);
  688. }
  689. static int docg4_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
  690. int page)
  691. {
  692. struct docg4_priv *doc = nand->priv;
  693. void __iomem *docptr = doc->virtadr;
  694. uint16_t status;
  695. dev_dbg(doc->dev, "%s: page %x\n", __func__, page);
  696. docg4_command(mtd, NAND_CMD_READ0, nand->ecc.size, page);
  697. writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
  698. write_nop(docptr);
  699. write_nop(docptr);
  700. write_nop(docptr);
  701. write_nop(docptr);
  702. write_nop(docptr);
  703. /* the 1st byte from the I/O reg is a status; the rest is oob data */
  704. status = readw(docptr + DOC_IOSPACE_DATA);
  705. if (status & DOCG4_READ_ERROR) {
  706. dev_warn(doc->dev,
  707. "docg4_read_oob failed: status = 0x%02x\n", status);
  708. return -EIO;
  709. }
  710. dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
  711. docg4_read_buf(mtd, nand->oob_poi, 16);
  712. write_nop(docptr);
  713. write_nop(docptr);
  714. write_nop(docptr);
  715. writew(0, docptr + DOC_DATAEND);
  716. write_nop(docptr);
  717. return 0;
  718. }
  719. static void docg4_erase_block(struct mtd_info *mtd, int page)
  720. {
  721. struct nand_chip *nand = mtd->priv;
  722. struct docg4_priv *doc = nand->priv;
  723. void __iomem *docptr = doc->virtadr;
  724. uint16_t g4_page;
  725. dev_dbg(doc->dev, "%s: page %04x\n", __func__, page);
  726. sequence_reset(mtd);
  727. writew(DOCG4_SEQ_BLOCKERASE, docptr + DOC_FLASHSEQUENCE);
  728. writew(DOC_CMD_PROG_BLOCK_ADDR, docptr + DOC_FLASHCOMMAND);
  729. write_nop(docptr);
  730. /* only 2 bytes of address are written to specify erase block */
  731. g4_page = (uint16_t)(page / 4); /* to g4's 2k page addressing */
  732. writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
  733. g4_page >>= 8;
  734. writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
  735. write_nop(docptr);
  736. /* start the erasure */
  737. writew(DOC_CMD_ERASECYCLE2, docptr + DOC_FLASHCOMMAND);
  738. write_nop(docptr);
  739. write_nop(docptr);
  740. usleep_range(500, 1000); /* erasure is long; take a snooze */
  741. poll_status(doc);
  742. writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
  743. writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
  744. writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
  745. write_nop(docptr);
  746. write_nop(docptr);
  747. write_nop(docptr);
  748. write_nop(docptr);
  749. write_nop(docptr);
  750. read_progstatus(doc);
  751. writew(0, docptr + DOC_DATAEND);
  752. write_nop(docptr);
  753. poll_status(doc);
  754. write_nop(docptr);
  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)
  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)
  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);
  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->scan_bbt = nand_default_bbt;
  1024. nand->erase_cmd = docg4_erase_block;
  1025. nand->ecc.read_page = docg4_read_page;
  1026. nand->ecc.write_page = docg4_write_page;
  1027. nand->ecc.read_page_raw = docg4_read_page_raw;
  1028. nand->ecc.write_page_raw = docg4_write_page_raw;
  1029. nand->ecc.read_oob = docg4_read_oob;
  1030. nand->ecc.write_oob = docg4_write_oob;
  1031. /*
  1032. * The way the nand infrastructure code is written, a memory-based bbt
  1033. * is not created if NAND_SKIP_BBTSCAN is set. With no memory bbt,
  1034. * nand->block_bad() is used. So when ignoring bad blocks, we skip the
  1035. * scan and define a dummy block_bad() which always returns 0.
  1036. */
  1037. if (ignore_badblocks) {
  1038. nand->options |= NAND_SKIP_BBTSCAN;
  1039. nand->block_bad = docg4_block_neverbad;
  1040. }
  1041. }
  1042. static int __init read_id_reg(struct mtd_info *mtd)
  1043. {
  1044. struct nand_chip *nand = mtd->priv;
  1045. struct docg4_priv *doc = nand->priv;
  1046. void __iomem *docptr = doc->virtadr;
  1047. uint16_t id1, id2;
  1048. /* check for presence of g4 chip by reading id registers */
  1049. id1 = readw(docptr + DOC_CHIPID);
  1050. id1 = readw(docptr + DOCG4_MYSTERY_REG);
  1051. id2 = readw(docptr + DOC_CHIPID_INV);
  1052. id2 = readw(docptr + DOCG4_MYSTERY_REG);
  1053. if (id1 == DOCG4_IDREG1_VALUE && id2 == DOCG4_IDREG2_VALUE) {
  1054. dev_info(doc->dev,
  1055. "NAND device: 128MiB Diskonchip G4 detected\n");
  1056. return 0;
  1057. }
  1058. return -ENODEV;
  1059. }
  1060. static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
  1061. static int __init probe_docg4(struct platform_device *pdev)
  1062. {
  1063. struct mtd_info *mtd;
  1064. struct nand_chip *nand;
  1065. void __iomem *virtadr;
  1066. struct docg4_priv *doc;
  1067. int len, retval;
  1068. struct resource *r;
  1069. struct device *dev = &pdev->dev;
  1070. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1071. if (r == NULL) {
  1072. dev_err(dev, "no io memory resource defined!\n");
  1073. return -ENODEV;
  1074. }
  1075. virtadr = ioremap(r->start, resource_size(r));
  1076. if (!virtadr) {
  1077. dev_err(dev, "Diskonchip ioremap failed: %pR\n", r);
  1078. return -EIO;
  1079. }
  1080. len = sizeof(struct mtd_info) + sizeof(struct nand_chip) +
  1081. sizeof(struct docg4_priv);
  1082. mtd = kzalloc(len, GFP_KERNEL);
  1083. if (mtd == NULL) {
  1084. retval = -ENOMEM;
  1085. goto fail;
  1086. }
  1087. nand = (struct nand_chip *) (mtd + 1);
  1088. doc = (struct docg4_priv *) (nand + 1);
  1089. mtd->priv = nand;
  1090. nand->priv = doc;
  1091. mtd->owner = THIS_MODULE;
  1092. doc->virtadr = virtadr;
  1093. doc->dev = dev;
  1094. init_mtd_structs(mtd);
  1095. /* initialize kernel bch algorithm */
  1096. doc->bch = init_bch(DOCG4_M, DOCG4_T, DOCG4_PRIMITIVE_POLY);
  1097. if (doc->bch == NULL) {
  1098. retval = -EINVAL;
  1099. goto fail;
  1100. }
  1101. platform_set_drvdata(pdev, doc);
  1102. reset(mtd);
  1103. retval = read_id_reg(mtd);
  1104. if (retval == -ENODEV) {
  1105. dev_warn(dev, "No diskonchip G4 device found.\n");
  1106. goto fail;
  1107. }
  1108. retval = nand_scan_tail(mtd);
  1109. if (retval)
  1110. goto fail;
  1111. retval = read_factory_bbt(mtd);
  1112. if (retval)
  1113. goto fail;
  1114. retval = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
  1115. if (retval)
  1116. goto fail;
  1117. doc->mtd = mtd;
  1118. return 0;
  1119. fail:
  1120. iounmap(virtadr);
  1121. if (mtd) {
  1122. /* re-declarations avoid compiler warning */
  1123. struct nand_chip *nand = mtd->priv;
  1124. struct docg4_priv *doc = nand->priv;
  1125. nand_release(mtd); /* deletes partitions and mtd devices */
  1126. free_bch(doc->bch);
  1127. kfree(mtd);
  1128. }
  1129. return retval;
  1130. }
  1131. static int __exit cleanup_docg4(struct platform_device *pdev)
  1132. {
  1133. struct docg4_priv *doc = platform_get_drvdata(pdev);
  1134. nand_release(doc->mtd);
  1135. free_bch(doc->bch);
  1136. kfree(doc->mtd);
  1137. iounmap(doc->virtadr);
  1138. return 0;
  1139. }
  1140. static struct platform_driver docg4_driver = {
  1141. .driver = {
  1142. .name = "docg4",
  1143. .owner = THIS_MODULE,
  1144. },
  1145. .suspend = docg4_suspend,
  1146. .resume = docg4_resume,
  1147. .remove = __exit_p(cleanup_docg4),
  1148. };
  1149. module_platform_driver_probe(docg4_driver, probe_docg4);
  1150. MODULE_LICENSE("GPL");
  1151. MODULE_AUTHOR("Mike Dunn");
  1152. MODULE_DESCRIPTION("M-Systems DiskOnChip G4 device driver");