docg4.c 38 KB

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