docg4.c 40 KB

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