docg4.c 40 KB

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