docg4.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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. dev_dbg(doc->dev, "%s: page %08x\n", __func__, page);
  595. writew(DOC_ECCCONF0_READ_MODE |
  596. DOC_ECCCONF0_ECC_ENABLE |
  597. DOC_ECCCONF0_UNKNOWN |
  598. DOCG4_BCH_SIZE,
  599. docptr + DOC_ECCCONF0);
  600. write_nop(docptr);
  601. write_nop(docptr);
  602. write_nop(docptr);
  603. write_nop(docptr);
  604. write_nop(docptr);
  605. /* the 1st byte from the I/O reg is a status; the rest is page data */
  606. status = readw(docptr + DOC_IOSPACE_DATA);
  607. if (status & DOCG4_READ_ERROR) {
  608. dev_err(doc->dev,
  609. "docg4_read_page: bad status: 0x%02x\n", status);
  610. writew(0, docptr + DOC_DATAEND);
  611. return -EIO;
  612. }
  613. dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
  614. docg4_read_buf(mtd, buf, DOCG4_PAGE_SIZE); /* read the page data */
  615. /*
  616. * Diskonchips read oob immediately after a page read. Mtd
  617. * infrastructure issues a separate command for reading oob after the
  618. * page is read. So we save the oob bytes in a local buffer and just
  619. * copy it if the next command reads oob from the same page.
  620. */
  621. /* first 14 oob bytes read from I/O reg */
  622. docg4_read_buf(mtd, doc->oob_buf, 14);
  623. /* last 2 read from another reg */
  624. buf16 = (uint16_t *)(doc->oob_buf + 14);
  625. *buf16 = readw(docptr + DOCG4_MYSTERY_REG);
  626. write_nop(docptr);
  627. if (likely(use_ecc == true)) {
  628. /* read the register that tells us if bitflip(s) detected */
  629. edc_err = readw(docptr + DOC_ECCCONF1);
  630. edc_err = readw(docptr + DOC_ECCCONF1);
  631. dev_dbg(doc->dev, "%s: edc_err = 0x%02x\n", __func__, edc_err);
  632. /* If bitflips are reported, attempt to correct with ecc */
  633. if (edc_err & DOC_ECCCONF1_BCH_SYNDROM_ERR) {
  634. int bits_corrected = correct_data(mtd, buf, page);
  635. if (bits_corrected == -EBADMSG)
  636. mtd->ecc_stats.failed++;
  637. else
  638. mtd->ecc_stats.corrected += bits_corrected;
  639. }
  640. }
  641. writew(0, docptr + DOC_DATAEND);
  642. return 0;
  643. }
  644. static int docg4_read_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
  645. uint8_t *buf, int page)
  646. {
  647. return read_page(mtd, nand, buf, page, false);
  648. }
  649. static int docg4_read_page(struct mtd_info *mtd, struct nand_chip *nand,
  650. uint8_t *buf, int page)
  651. {
  652. return read_page(mtd, nand, buf, page, true);
  653. }
  654. static int docg4_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
  655. int page, int sndcmd)
  656. {
  657. struct docg4_priv *doc = nand->priv;
  658. void __iomem *docptr = doc->virtadr;
  659. uint16_t status;
  660. dev_dbg(doc->dev, "%s: page %x\n", __func__, page);
  661. /*
  662. * Oob bytes are read as part of a normal page read. If the previous
  663. * nand command was a read of the page whose oob is now being read, just
  664. * copy the oob bytes that we saved in a local buffer and avoid a
  665. * separate oob read.
  666. */
  667. if (doc->last_command.command == NAND_CMD_READ0 &&
  668. doc->last_command.page == page) {
  669. memcpy(nand->oob_poi, doc->oob_buf, 16);
  670. return 0;
  671. }
  672. /*
  673. * Separate read of oob data only.
  674. */
  675. docg4_command(mtd, NAND_CMD_READ0, nand->ecc.size, page);
  676. writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
  677. write_nop(docptr);
  678. write_nop(docptr);
  679. write_nop(docptr);
  680. write_nop(docptr);
  681. write_nop(docptr);
  682. /* the 1st byte from the I/O reg is a status; the rest is oob data */
  683. status = readw(docptr + DOC_IOSPACE_DATA);
  684. if (status & DOCG4_READ_ERROR) {
  685. dev_warn(doc->dev,
  686. "docg4_read_oob failed: status = 0x%02x\n", status);
  687. return -EIO;
  688. }
  689. dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
  690. docg4_read_buf(mtd, nand->oob_poi, 16);
  691. write_nop(docptr);
  692. write_nop(docptr);
  693. write_nop(docptr);
  694. writew(0, docptr + DOC_DATAEND);
  695. write_nop(docptr);
  696. return 0;
  697. }
  698. static void docg4_erase_block(struct mtd_info *mtd, int page)
  699. {
  700. struct nand_chip *nand = mtd->priv;
  701. struct docg4_priv *doc = nand->priv;
  702. void __iomem *docptr = doc->virtadr;
  703. uint16_t g4_page;
  704. dev_dbg(doc->dev, "%s: page %04x\n", __func__, page);
  705. sequence_reset(mtd);
  706. writew(DOCG4_SEQ_BLOCKERASE, docptr + DOC_FLASHSEQUENCE);
  707. writew(DOC_CMD_PROG_BLOCK_ADDR, docptr + DOC_FLASHCOMMAND);
  708. write_nop(docptr);
  709. /* only 2 bytes of address are written to specify erase block */
  710. g4_page = (uint16_t)(page / 4); /* to g4's 2k page addressing */
  711. writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
  712. g4_page >>= 8;
  713. writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
  714. write_nop(docptr);
  715. /* start the erasure */
  716. writew(DOC_CMD_ERASECYCLE2, docptr + DOC_FLASHCOMMAND);
  717. write_nop(docptr);
  718. write_nop(docptr);
  719. usleep_range(500, 1000); /* erasure is long; take a snooze */
  720. poll_status(doc);
  721. writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
  722. writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
  723. writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
  724. write_nop(docptr);
  725. write_nop(docptr);
  726. write_nop(docptr);
  727. write_nop(docptr);
  728. write_nop(docptr);
  729. read_progstatus(doc);
  730. writew(0, docptr + DOC_DATAEND);
  731. write_nop(docptr);
  732. poll_status(doc);
  733. write_nop(docptr);
  734. }
  735. static void write_page(struct mtd_info *mtd, struct nand_chip *nand,
  736. const uint8_t *buf, bool use_ecc)
  737. {
  738. struct docg4_priv *doc = nand->priv;
  739. void __iomem *docptr = doc->virtadr;
  740. uint8_t ecc_buf[8];
  741. dev_dbg(doc->dev, "%s...\n", __func__);
  742. writew(DOC_ECCCONF0_ECC_ENABLE |
  743. DOC_ECCCONF0_UNKNOWN |
  744. DOCG4_BCH_SIZE,
  745. docptr + DOC_ECCCONF0);
  746. write_nop(docptr);
  747. /* write the page data */
  748. docg4_write_buf16(mtd, buf, DOCG4_PAGE_SIZE);
  749. /* oob bytes 0 through 5 are written to I/O reg */
  750. docg4_write_buf16(mtd, nand->oob_poi, 6);
  751. /* oob byte 6 written to a separate reg */
  752. writew(nand->oob_poi[6], docptr + DOCG4_OOB_6_7);
  753. write_nop(docptr);
  754. write_nop(docptr);
  755. /* write hw-generated ecc bytes to oob */
  756. if (likely(use_ecc == true)) {
  757. /* oob byte 7 is hamming code */
  758. uint8_t hamming = readb(docptr + DOC_HAMMINGPARITY);
  759. hamming = readb(docptr + DOC_HAMMINGPARITY); /* 2nd read */
  760. writew(hamming, docptr + DOCG4_OOB_6_7);
  761. write_nop(docptr);
  762. /* read the 7 bch bytes from ecc regs */
  763. read_hw_ecc(docptr, ecc_buf);
  764. ecc_buf[7] = 0; /* clear the "page written" flag */
  765. }
  766. /* write user-supplied bytes to oob */
  767. else {
  768. writew(nand->oob_poi[7], docptr + DOCG4_OOB_6_7);
  769. write_nop(docptr);
  770. memcpy(ecc_buf, &nand->oob_poi[8], 8);
  771. }
  772. docg4_write_buf16(mtd, ecc_buf, 8);
  773. write_nop(docptr);
  774. write_nop(docptr);
  775. writew(0, docptr + DOC_DATAEND);
  776. write_nop(docptr);
  777. }
  778. static void docg4_write_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
  779. const uint8_t *buf)
  780. {
  781. return write_page(mtd, nand, buf, false);
  782. }
  783. static void docg4_write_page(struct mtd_info *mtd, struct nand_chip *nand,
  784. const uint8_t *buf)
  785. {
  786. return write_page(mtd, nand, buf, true);
  787. }
  788. static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
  789. int page)
  790. {
  791. /*
  792. * Writing oob-only is not really supported, because MLC nand must write
  793. * oob bytes at the same time as page data. Nonetheless, we save the
  794. * oob buffer contents here, and then write it along with the page data
  795. * if the same page is subsequently written. This allows user space
  796. * utilities that write the oob data prior to the page data to work
  797. * (e.g., nandwrite). The disdvantage is that, if the intention was to
  798. * write oob only, the operation is quietly ignored. Also, oob can get
  799. * corrupted if two concurrent processes are running nandwrite.
  800. */
  801. /* note that bytes 7..14 are hw generated hamming/ecc and overwritten */
  802. struct docg4_priv *doc = nand->priv;
  803. doc->oob_page = page;
  804. memcpy(doc->oob_buf, nand->oob_poi, 16);
  805. return 0;
  806. }
  807. static int __init read_factory_bbt(struct mtd_info *mtd)
  808. {
  809. /*
  810. * The device contains a read-only factory bad block table. Read it and
  811. * update the memory-based bbt accordingly.
  812. */
  813. struct nand_chip *nand = mtd->priv;
  814. struct docg4_priv *doc = nand->priv;
  815. uint32_t g4_addr = mtd_to_docg4_address(DOCG4_FACTORY_BBT_PAGE, 0);
  816. uint8_t *buf;
  817. int i, block, status;
  818. buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
  819. if (buf == NULL)
  820. return -ENOMEM;
  821. read_page_prologue(mtd, g4_addr);
  822. status = docg4_read_page(mtd, nand, buf, DOCG4_FACTORY_BBT_PAGE);
  823. if (status)
  824. goto exit;
  825. /*
  826. * If no memory-based bbt was created, exit. This will happen if module
  827. * parameter ignore_badblocks is set. Then why even call this function?
  828. * For an unknown reason, block erase always fails if it's the first
  829. * operation after device power-up. The above read ensures it never is.
  830. * Ugly, I know.
  831. */
  832. if (nand->bbt == NULL) /* no memory-based bbt */
  833. goto exit;
  834. /*
  835. * Parse factory bbt and update memory-based bbt. Factory bbt format is
  836. * simple: one bit per block, block numbers increase left to right (msb
  837. * to lsb). Bit clear means bad block.
  838. */
  839. for (i = block = 0; block < DOCG4_NUMBLOCKS; block += 8, i++) {
  840. int bitnum;
  841. unsigned long bits = ~buf[i];
  842. for_each_set_bit(bitnum, &bits, 8) {
  843. int badblock = block + 7 - bitnum;
  844. nand->bbt[badblock / 4] |=
  845. 0x03 << ((badblock % 4) * 2);
  846. mtd->ecc_stats.badblocks++;
  847. dev_notice(doc->dev, "factory-marked bad block: %d\n",
  848. badblock);
  849. }
  850. }
  851. exit:
  852. kfree(buf);
  853. return status;
  854. }
  855. static int docg4_block_markbad(struct mtd_info *mtd, loff_t ofs)
  856. {
  857. /*
  858. * Mark a block as bad. Bad blocks are marked in the oob area of the
  859. * first page of the block. The default scan_bbt() in the nand
  860. * infrastructure code works fine for building the memory-based bbt
  861. * during initialization, as does the nand infrastructure function that
  862. * checks if a block is bad by reading the bbt. This function replaces
  863. * the nand default because writes to oob-only are not supported.
  864. */
  865. int ret, i;
  866. uint8_t *buf;
  867. struct nand_chip *nand = mtd->priv;
  868. struct docg4_priv *doc = nand->priv;
  869. struct nand_bbt_descr *bbtd = nand->badblock_pattern;
  870. int block = (int)(ofs >> nand->bbt_erase_shift);
  871. int page = (int)(ofs >> nand->page_shift);
  872. uint32_t g4_addr = mtd_to_docg4_address(page, 0);
  873. dev_dbg(doc->dev, "%s: %08llx\n", __func__, ofs);
  874. if (unlikely(ofs & (DOCG4_BLOCK_SIZE - 1)))
  875. dev_warn(doc->dev, "%s: ofs %llx not start of block!\n",
  876. __func__, ofs);
  877. /* allocate blank buffer for page data */
  878. buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
  879. if (buf == NULL)
  880. return -ENOMEM;
  881. /* update bbt in memory */
  882. nand->bbt[block / 4] |= 0x01 << ((block & 0x03) * 2);
  883. /* write bit-wise negation of pattern to oob buffer */
  884. memset(nand->oob_poi, 0xff, mtd->oobsize);
  885. for (i = 0; i < bbtd->len; i++)
  886. nand->oob_poi[bbtd->offs + i] = ~bbtd->pattern[i];
  887. /* write first page of block */
  888. write_page_prologue(mtd, g4_addr);
  889. docg4_write_page(mtd, nand, buf);
  890. ret = pageprog(mtd);
  891. if (!ret)
  892. mtd->ecc_stats.badblocks++;
  893. kfree(buf);
  894. return ret;
  895. }
  896. static int docg4_block_neverbad(struct mtd_info *mtd, loff_t ofs, int getchip)
  897. {
  898. /* only called when module_param ignore_badblocks is set */
  899. return 0;
  900. }
  901. static int docg4_suspend(struct platform_device *pdev, pm_message_t state)
  902. {
  903. /*
  904. * Put the device into "deep power-down" mode. Note that CE# must be
  905. * deasserted for this to take effect. The xscale, e.g., can be
  906. * configured to float this signal when the processor enters power-down,
  907. * and a suitable pull-up ensures its deassertion.
  908. */
  909. int i;
  910. uint8_t pwr_down;
  911. struct docg4_priv *doc = platform_get_drvdata(pdev);
  912. void __iomem *docptr = doc->virtadr;
  913. dev_dbg(doc->dev, "%s...\n", __func__);
  914. /* poll the register that tells us we're ready to go to sleep */
  915. for (i = 0; i < 10; i++) {
  916. pwr_down = readb(docptr + DOC_POWERMODE);
  917. if (pwr_down & DOC_POWERDOWN_READY)
  918. break;
  919. usleep_range(1000, 4000);
  920. }
  921. if (pwr_down & DOC_POWERDOWN_READY) {
  922. dev_err(doc->dev, "suspend failed; "
  923. "timeout polling DOC_POWERDOWN_READY\n");
  924. return -EIO;
  925. }
  926. writew(DOC_ASICMODE_POWERDOWN | DOC_ASICMODE_MDWREN,
  927. docptr + DOC_ASICMODE);
  928. writew(~(DOC_ASICMODE_POWERDOWN | DOC_ASICMODE_MDWREN),
  929. docptr + DOC_ASICMODECONFIRM);
  930. write_nop(docptr);
  931. return 0;
  932. }
  933. static int docg4_resume(struct platform_device *pdev)
  934. {
  935. /*
  936. * Exit power-down. Twelve consecutive reads of the address below
  937. * accomplishes this, assuming CE# has been asserted.
  938. */
  939. struct docg4_priv *doc = platform_get_drvdata(pdev);
  940. void __iomem *docptr = doc->virtadr;
  941. int i;
  942. dev_dbg(doc->dev, "%s...\n", __func__);
  943. for (i = 0; i < 12; i++)
  944. readb(docptr + 0x1fff);
  945. return 0;
  946. }
  947. static void __init init_mtd_structs(struct mtd_info *mtd)
  948. {
  949. /* initialize mtd and nand data structures */
  950. /*
  951. * Note that some of the following initializations are not usually
  952. * required within a nand driver because they are performed by the nand
  953. * infrastructure code as part of nand_scan(). In this case they need
  954. * to be initialized here because we skip call to nand_scan_ident() (the
  955. * first half of nand_scan()). The call to nand_scan_ident() is skipped
  956. * because for this device the chip id is not read in the manner of a
  957. * standard nand device. Unfortunately, nand_scan_ident() does other
  958. * things as well, such as call nand_set_defaults().
  959. */
  960. struct nand_chip *nand = mtd->priv;
  961. struct docg4_priv *doc = nand->priv;
  962. mtd->size = DOCG4_CHIP_SIZE;
  963. mtd->name = "Msys_Diskonchip_G4";
  964. mtd->writesize = DOCG4_PAGE_SIZE;
  965. mtd->erasesize = DOCG4_BLOCK_SIZE;
  966. mtd->oobsize = DOCG4_OOB_SIZE;
  967. nand->chipsize = DOCG4_CHIP_SIZE;
  968. nand->chip_shift = DOCG4_CHIP_SHIFT;
  969. nand->bbt_erase_shift = nand->phys_erase_shift = DOCG4_ERASE_SHIFT;
  970. nand->chip_delay = 20;
  971. nand->page_shift = DOCG4_PAGE_SHIFT;
  972. nand->pagemask = 0x3ffff;
  973. nand->badblockpos = NAND_LARGE_BADBLOCK_POS;
  974. nand->badblockbits = 8;
  975. nand->ecc.layout = &docg4_oobinfo;
  976. nand->ecc.mode = NAND_ECC_HW_SYNDROME;
  977. nand->ecc.size = DOCG4_PAGE_SIZE;
  978. nand->ecc.prepad = 8;
  979. nand->ecc.bytes = 8;
  980. nand->ecc.strength = DOCG4_T;
  981. nand->options =
  982. NAND_BUSWIDTH_16 | NAND_NO_SUBPAGE_WRITE | NAND_NO_AUTOINCR;
  983. nand->IO_ADDR_R = nand->IO_ADDR_W = doc->virtadr + DOC_IOSPACE_DATA;
  984. nand->controller = &nand->hwcontrol;
  985. spin_lock_init(&nand->controller->lock);
  986. init_waitqueue_head(&nand->controller->wq);
  987. /* methods */
  988. nand->cmdfunc = docg4_command;
  989. nand->waitfunc = docg4_wait;
  990. nand->select_chip = docg4_select_chip;
  991. nand->read_byte = docg4_read_byte;
  992. nand->block_markbad = docg4_block_markbad;
  993. nand->read_buf = docg4_read_buf;
  994. nand->write_buf = docg4_write_buf16;
  995. nand->scan_bbt = nand_default_bbt;
  996. nand->erase_cmd = docg4_erase_block;
  997. nand->ecc.read_page = docg4_read_page;
  998. nand->ecc.write_page = docg4_write_page;
  999. nand->ecc.read_page_raw = docg4_read_page_raw;
  1000. nand->ecc.write_page_raw = docg4_write_page_raw;
  1001. nand->ecc.read_oob = docg4_read_oob;
  1002. nand->ecc.write_oob = docg4_write_oob;
  1003. /*
  1004. * The way the nand infrastructure code is written, a memory-based bbt
  1005. * is not created if NAND_SKIP_BBTSCAN is set. With no memory bbt,
  1006. * nand->block_bad() is used. So when ignoring bad blocks, we skip the
  1007. * scan and define a dummy block_bad() which always returns 0.
  1008. */
  1009. if (ignore_badblocks) {
  1010. nand->options |= NAND_SKIP_BBTSCAN;
  1011. nand->block_bad = docg4_block_neverbad;
  1012. }
  1013. }
  1014. static int __init read_id_reg(struct mtd_info *mtd)
  1015. {
  1016. struct nand_chip *nand = mtd->priv;
  1017. struct docg4_priv *doc = nand->priv;
  1018. void __iomem *docptr = doc->virtadr;
  1019. uint16_t id1, id2;
  1020. /* check for presence of g4 chip by reading id registers */
  1021. id1 = readw(docptr + DOC_CHIPID);
  1022. id1 = readw(docptr + DOCG4_MYSTERY_REG);
  1023. id2 = readw(docptr + DOC_CHIPID_INV);
  1024. id2 = readw(docptr + DOCG4_MYSTERY_REG);
  1025. if (id1 == DOCG4_IDREG1_VALUE && id2 == DOCG4_IDREG2_VALUE) {
  1026. dev_info(doc->dev,
  1027. "NAND device: 128MiB Diskonchip G4 detected\n");
  1028. return 0;
  1029. }
  1030. return -ENODEV;
  1031. }
  1032. static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
  1033. static int __init probe_docg4(struct platform_device *pdev)
  1034. {
  1035. struct mtd_info *mtd;
  1036. struct nand_chip *nand;
  1037. void __iomem *virtadr;
  1038. struct docg4_priv *doc;
  1039. int len, retval;
  1040. struct resource *r;
  1041. struct device *dev = &pdev->dev;
  1042. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1043. if (r == NULL) {
  1044. dev_err(dev, "no io memory resource defined!\n");
  1045. return -ENODEV;
  1046. }
  1047. virtadr = ioremap(r->start, resource_size(r));
  1048. if (!virtadr) {
  1049. dev_err(dev, "Diskonchip ioremap failed: %pR\n", r);
  1050. return -EIO;
  1051. }
  1052. len = sizeof(struct mtd_info) + sizeof(struct nand_chip) +
  1053. sizeof(struct docg4_priv);
  1054. mtd = kzalloc(len, GFP_KERNEL);
  1055. if (mtd == NULL) {
  1056. retval = -ENOMEM;
  1057. goto fail;
  1058. }
  1059. nand = (struct nand_chip *) (mtd + 1);
  1060. doc = (struct docg4_priv *) (nand + 1);
  1061. mtd->priv = nand;
  1062. nand->priv = doc;
  1063. mtd->owner = THIS_MODULE;
  1064. doc->virtadr = virtadr;
  1065. doc->dev = dev;
  1066. init_mtd_structs(mtd);
  1067. /* initialize kernel bch algorithm */
  1068. doc->bch = init_bch(DOCG4_M, DOCG4_T, DOCG4_PRIMITIVE_POLY);
  1069. if (doc->bch == NULL) {
  1070. retval = -EINVAL;
  1071. goto fail;
  1072. }
  1073. platform_set_drvdata(pdev, doc);
  1074. reset(mtd);
  1075. retval = read_id_reg(mtd);
  1076. if (retval == -ENODEV) {
  1077. dev_warn(dev, "No diskonchip G4 device found.\n");
  1078. goto fail;
  1079. }
  1080. retval = nand_scan_tail(mtd);
  1081. if (retval)
  1082. goto fail;
  1083. retval = read_factory_bbt(mtd);
  1084. if (retval)
  1085. goto fail;
  1086. retval = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
  1087. if (retval)
  1088. goto fail;
  1089. doc->mtd = mtd;
  1090. return 0;
  1091. fail:
  1092. iounmap(virtadr);
  1093. if (mtd) {
  1094. /* re-declarations avoid compiler warning */
  1095. struct nand_chip *nand = mtd->priv;
  1096. struct docg4_priv *doc = nand->priv;
  1097. nand_release(mtd); /* deletes partitions and mtd devices */
  1098. platform_set_drvdata(pdev, NULL);
  1099. free_bch(doc->bch);
  1100. kfree(mtd);
  1101. }
  1102. return retval;
  1103. }
  1104. static int __exit cleanup_docg4(struct platform_device *pdev)
  1105. {
  1106. struct docg4_priv *doc = platform_get_drvdata(pdev);
  1107. nand_release(doc->mtd);
  1108. platform_set_drvdata(pdev, NULL);
  1109. free_bch(doc->bch);
  1110. kfree(doc->mtd);
  1111. iounmap(doc->virtadr);
  1112. return 0;
  1113. }
  1114. static struct platform_driver docg4_driver = {
  1115. .driver = {
  1116. .name = "docg4",
  1117. .owner = THIS_MODULE,
  1118. },
  1119. .suspend = docg4_suspend,
  1120. .resume = docg4_resume,
  1121. .remove = __exit_p(cleanup_docg4),
  1122. };
  1123. static int __init docg4_init(void)
  1124. {
  1125. return platform_driver_probe(&docg4_driver, probe_docg4);
  1126. }
  1127. static void __exit docg4_exit(void)
  1128. {
  1129. platform_driver_unregister(&docg4_driver);
  1130. }
  1131. module_init(docg4_init);
  1132. module_exit(docg4_exit);
  1133. MODULE_LICENSE("GPL");
  1134. MODULE_AUTHOR("Mike Dunn");
  1135. MODULE_DESCRIPTION("M-Systems DiskOnChip G4 device driver");