docg4.c 41 KB

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