docg4.c 40 KB

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