docg4.c 40 KB

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