docg3.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  1. /*
  2. * Handles the M-Systems DiskOnChip G3 chip
  3. *
  4. * Copyright (C) 2011 Robert Jarzmik
  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. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/string.h>
  26. #include <linux/slab.h>
  27. #include <linux/io.h>
  28. #include <linux/delay.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/bitmap.h>
  32. #include <linux/bitrev.h>
  33. #include <linux/bch.h>
  34. #include <linux/debugfs.h>
  35. #include <linux/seq_file.h>
  36. #define CREATE_TRACE_POINTS
  37. #include "docg3.h"
  38. /*
  39. * This driver handles the DiskOnChip G3 flash memory.
  40. *
  41. * As no specification is available from M-Systems/Sandisk, this drivers lacks
  42. * several functions available on the chip, as :
  43. * - IPL write
  44. *
  45. * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
  46. * the driver assumes a 16bits data bus.
  47. *
  48. * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
  49. * - a 1 byte Hamming code stored in the OOB for each page
  50. * - a 7 bytes BCH code stored in the OOB for each page
  51. * The BCH ECC is :
  52. * - BCH is in GF(2^14)
  53. * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
  54. * + 1 hamming byte)
  55. * - BCH can correct up to 4 bits (t = 4)
  56. * - BCH syndroms are calculated in hardware, and checked in hardware as well
  57. *
  58. */
  59. static unsigned int reliable_mode;
  60. module_param(reliable_mode, uint, 0);
  61. MODULE_PARM_DESC(reliable_mode, "Set the docg3 mode (0=normal MLC, 1=fast, "
  62. "2=reliable) : MLC normal operations are in normal mode");
  63. /**
  64. * struct docg3_oobinfo - DiskOnChip G3 OOB layout
  65. * @eccbytes: 8 bytes are used (1 for Hamming ECC, 7 for BCH ECC)
  66. * @eccpos: ecc positions (byte 7 is Hamming ECC, byte 8-14 are BCH ECC)
  67. * @oobfree: free pageinfo bytes (byte 0 until byte 6, byte 15
  68. * @oobavail: 8 available bytes remaining after ECC toll
  69. */
  70. static struct nand_ecclayout docg3_oobinfo = {
  71. .eccbytes = 8,
  72. .eccpos = {7, 8, 9, 10, 11, 12, 13, 14},
  73. .oobfree = {{0, 7}, {15, 1} },
  74. .oobavail = 8,
  75. };
  76. /**
  77. * struct docg3_bch - BCH engine
  78. */
  79. static struct bch_control *docg3_bch;
  80. static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
  81. {
  82. u8 val = readb(docg3->base + reg);
  83. trace_docg3_io(0, 8, reg, (int)val);
  84. return val;
  85. }
  86. static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
  87. {
  88. u16 val = readw(docg3->base + reg);
  89. trace_docg3_io(0, 16, reg, (int)val);
  90. return val;
  91. }
  92. static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
  93. {
  94. writeb(val, docg3->base + reg);
  95. trace_docg3_io(1, 8, reg, val);
  96. }
  97. static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
  98. {
  99. writew(val, docg3->base + reg);
  100. trace_docg3_io(1, 16, reg, val);
  101. }
  102. static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
  103. {
  104. doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
  105. }
  106. static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
  107. {
  108. doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
  109. }
  110. static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
  111. {
  112. doc_writeb(docg3, addr, DOC_FLASHADDRESS);
  113. }
  114. static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
  115. static int doc_register_readb(struct docg3 *docg3, int reg)
  116. {
  117. u8 val;
  118. doc_writew(docg3, reg, DOC_READADDRESS);
  119. val = doc_readb(docg3, reg);
  120. doc_vdbg("Read register %04x : %02x\n", reg, val);
  121. return val;
  122. }
  123. static int doc_register_readw(struct docg3 *docg3, int reg)
  124. {
  125. u16 val;
  126. doc_writew(docg3, reg, DOC_READADDRESS);
  127. val = doc_readw(docg3, reg);
  128. doc_vdbg("Read register %04x : %04x\n", reg, val);
  129. return val;
  130. }
  131. /**
  132. * doc_delay - delay docg3 operations
  133. * @docg3: the device
  134. * @nbNOPs: the number of NOPs to issue
  135. *
  136. * As no specification is available, the right timings between chip commands are
  137. * unknown. The only available piece of information are the observed nops on a
  138. * working docg3 chip.
  139. * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
  140. * friendlier msleep() functions or blocking mdelay().
  141. */
  142. static void doc_delay(struct docg3 *docg3, int nbNOPs)
  143. {
  144. int i;
  145. doc_vdbg("NOP x %d\n", nbNOPs);
  146. for (i = 0; i < nbNOPs; i++)
  147. doc_writeb(docg3, 0, DOC_NOP);
  148. }
  149. static int is_prot_seq_error(struct docg3 *docg3)
  150. {
  151. int ctrl;
  152. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  153. return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
  154. }
  155. static int doc_is_ready(struct docg3 *docg3)
  156. {
  157. int ctrl;
  158. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  159. return ctrl & DOC_CTRL_FLASHREADY;
  160. }
  161. static int doc_wait_ready(struct docg3 *docg3)
  162. {
  163. int maxWaitCycles = 100;
  164. do {
  165. doc_delay(docg3, 4);
  166. cpu_relax();
  167. } while (!doc_is_ready(docg3) && maxWaitCycles--);
  168. doc_delay(docg3, 2);
  169. if (maxWaitCycles > 0)
  170. return 0;
  171. else
  172. return -EIO;
  173. }
  174. static int doc_reset_seq(struct docg3 *docg3)
  175. {
  176. int ret;
  177. doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
  178. doc_flash_sequence(docg3, DOC_SEQ_RESET);
  179. doc_flash_command(docg3, DOC_CMD_RESET);
  180. doc_delay(docg3, 2);
  181. ret = doc_wait_ready(docg3);
  182. doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
  183. return ret;
  184. }
  185. /**
  186. * doc_read_data_area - Read data from data area
  187. * @docg3: the device
  188. * @buf: the buffer to fill in (might be NULL is dummy reads)
  189. * @len: the length to read
  190. * @first: first time read, DOC_READADDRESS should be set
  191. *
  192. * Reads bytes from flash data. Handles the single byte / even bytes reads.
  193. */
  194. static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
  195. int first)
  196. {
  197. int i, cdr, len4;
  198. u16 data16, *dst16;
  199. u8 data8, *dst8;
  200. doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
  201. cdr = len & 0x3;
  202. len4 = len - cdr;
  203. if (first)
  204. doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
  205. dst16 = buf;
  206. for (i = 0; i < len4; i += 2) {
  207. data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
  208. if (dst16) {
  209. *dst16 = data16;
  210. dst16++;
  211. }
  212. }
  213. if (cdr) {
  214. doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
  215. DOC_READADDRESS);
  216. doc_delay(docg3, 1);
  217. dst8 = (u8 *)dst16;
  218. for (i = 0; i < cdr; i++) {
  219. data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
  220. if (dst8) {
  221. *dst8 = data8;
  222. dst8++;
  223. }
  224. }
  225. }
  226. }
  227. /**
  228. * doc_write_data_area - Write data into data area
  229. * @docg3: the device
  230. * @buf: the buffer to get input bytes from
  231. * @len: the length to write
  232. *
  233. * Writes bytes into flash data. Handles the single byte / even bytes writes.
  234. */
  235. static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
  236. {
  237. int i, cdr, len4;
  238. u16 *src16;
  239. u8 *src8;
  240. doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
  241. cdr = len & 0x3;
  242. len4 = len - cdr;
  243. doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
  244. src16 = (u16 *)buf;
  245. for (i = 0; i < len4; i += 2) {
  246. doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
  247. src16++;
  248. }
  249. src8 = (u8 *)src16;
  250. for (i = 0; i < cdr; i++) {
  251. doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
  252. DOC_READADDRESS);
  253. doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
  254. src8++;
  255. }
  256. }
  257. /**
  258. * doc_set_data_mode - Sets the flash to normal or reliable data mode
  259. * @docg3: the device
  260. *
  261. * The reliable data mode is a bit slower than the fast mode, but less errors
  262. * occur. Entering the reliable mode cannot be done without entering the fast
  263. * mode first.
  264. *
  265. * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks
  266. * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading
  267. * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same
  268. * result, which is a logical and between bytes from page 0 and page 1 (which is
  269. * consistent with the fact that writing to a page is _clearing_ bits of that
  270. * page).
  271. */
  272. static void doc_set_reliable_mode(struct docg3 *docg3)
  273. {
  274. static char *strmode[] = { "normal", "fast", "reliable", "invalid" };
  275. doc_dbg("doc_set_reliable_mode(%s)\n", strmode[docg3->reliable]);
  276. switch (docg3->reliable) {
  277. case 0:
  278. break;
  279. case 1:
  280. doc_flash_sequence(docg3, DOC_SEQ_SET_FASTMODE);
  281. doc_flash_command(docg3, DOC_CMD_FAST_MODE);
  282. break;
  283. case 2:
  284. doc_flash_sequence(docg3, DOC_SEQ_SET_RELIABLEMODE);
  285. doc_flash_command(docg3, DOC_CMD_FAST_MODE);
  286. doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
  287. break;
  288. default:
  289. doc_err("doc_set_reliable_mode(): invalid mode\n");
  290. break;
  291. }
  292. doc_delay(docg3, 2);
  293. }
  294. /**
  295. * doc_set_asic_mode - Set the ASIC mode
  296. * @docg3: the device
  297. * @mode: the mode
  298. *
  299. * The ASIC can work in 3 modes :
  300. * - RESET: all registers are zeroed
  301. * - NORMAL: receives and handles commands
  302. * - POWERDOWN: minimal poweruse, flash parts shut off
  303. */
  304. static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
  305. {
  306. int i;
  307. for (i = 0; i < 12; i++)
  308. doc_readb(docg3, DOC_IOSPACE_IPL);
  309. mode |= DOC_ASICMODE_MDWREN;
  310. doc_dbg("doc_set_asic_mode(%02x)\n", mode);
  311. doc_writeb(docg3, mode, DOC_ASICMODE);
  312. doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
  313. doc_delay(docg3, 1);
  314. }
  315. /**
  316. * doc_set_device_id - Sets the devices id for cascaded G3 chips
  317. * @docg3: the device
  318. * @id: the chip to select (amongst 0, 1, 2, 3)
  319. *
  320. * There can be 4 cascaded G3 chips. This function selects the one which will
  321. * should be the active one.
  322. */
  323. static void doc_set_device_id(struct docg3 *docg3, int id)
  324. {
  325. u8 ctrl;
  326. doc_dbg("doc_set_device_id(%d)\n", id);
  327. doc_writeb(docg3, id, DOC_DEVICESELECT);
  328. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  329. ctrl &= ~DOC_CTRL_VIOLATION;
  330. ctrl |= DOC_CTRL_CE;
  331. doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
  332. }
  333. /**
  334. * doc_set_extra_page_mode - Change flash page layout
  335. * @docg3: the device
  336. *
  337. * Normally, the flash page is split into the data (512 bytes) and the out of
  338. * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
  339. * leveling counters are stored. To access this last area of 4 bytes, a special
  340. * mode must be input to the flash ASIC.
  341. *
  342. * Returns 0 if no error occured, -EIO else.
  343. */
  344. static int doc_set_extra_page_mode(struct docg3 *docg3)
  345. {
  346. int fctrl;
  347. doc_dbg("doc_set_extra_page_mode()\n");
  348. doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
  349. doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
  350. doc_delay(docg3, 2);
  351. fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  352. if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
  353. return -EIO;
  354. else
  355. return 0;
  356. }
  357. /**
  358. * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
  359. * @docg3: the device
  360. * @sector: the sector
  361. */
  362. static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
  363. {
  364. doc_delay(docg3, 1);
  365. doc_flash_address(docg3, sector & 0xff);
  366. doc_flash_address(docg3, (sector >> 8) & 0xff);
  367. doc_flash_address(docg3, (sector >> 16) & 0xff);
  368. doc_delay(docg3, 1);
  369. }
  370. /**
  371. * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
  372. * @docg3: the device
  373. * @sector: the sector
  374. * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
  375. */
  376. static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
  377. {
  378. ofs = ofs >> 2;
  379. doc_delay(docg3, 1);
  380. doc_flash_address(docg3, ofs & 0xff);
  381. doc_flash_address(docg3, sector & 0xff);
  382. doc_flash_address(docg3, (sector >> 8) & 0xff);
  383. doc_flash_address(docg3, (sector >> 16) & 0xff);
  384. doc_delay(docg3, 1);
  385. }
  386. /**
  387. * doc_seek - Set both flash planes to the specified block, page for reading
  388. * @docg3: the device
  389. * @block0: the first plane block index
  390. * @block1: the second plane block index
  391. * @page: the page index within the block
  392. * @wear: if true, read will occur on the 4 extra bytes of the wear area
  393. * @ofs: offset in page to read
  394. *
  395. * Programs the flash even and odd planes to the specific block and page.
  396. * Alternatively, programs the flash to the wear area of the specified page.
  397. */
  398. static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
  399. int wear, int ofs)
  400. {
  401. int sector, ret = 0;
  402. doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
  403. block0, block1, page, ofs, wear);
  404. if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
  405. doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
  406. doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
  407. doc_delay(docg3, 2);
  408. } else {
  409. doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
  410. doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
  411. doc_delay(docg3, 2);
  412. }
  413. doc_set_reliable_mode(docg3);
  414. if (wear)
  415. ret = doc_set_extra_page_mode(docg3);
  416. if (ret)
  417. goto out;
  418. doc_flash_sequence(docg3, DOC_SEQ_READ);
  419. sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
  420. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  421. doc_setup_addr_sector(docg3, sector);
  422. sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
  423. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  424. doc_setup_addr_sector(docg3, sector);
  425. doc_delay(docg3, 1);
  426. out:
  427. return ret;
  428. }
  429. /**
  430. * doc_write_seek - Set both flash planes to the specified block, page for writing
  431. * @docg3: the device
  432. * @block0: the first plane block index
  433. * @block1: the second plane block index
  434. * @page: the page index within the block
  435. * @ofs: offset in page to write
  436. *
  437. * Programs the flash even and odd planes to the specific block and page.
  438. * Alternatively, programs the flash to the wear area of the specified page.
  439. */
  440. static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
  441. int ofs)
  442. {
  443. int ret = 0, sector;
  444. doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
  445. block0, block1, page, ofs);
  446. doc_set_reliable_mode(docg3);
  447. if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
  448. doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
  449. doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
  450. doc_delay(docg3, 2);
  451. } else {
  452. doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
  453. doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
  454. doc_delay(docg3, 2);
  455. }
  456. doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
  457. doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
  458. sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
  459. doc_setup_writeaddr_sector(docg3, sector, ofs);
  460. doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
  461. doc_delay(docg3, 2);
  462. ret = doc_wait_ready(docg3);
  463. if (ret)
  464. goto out;
  465. doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
  466. sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
  467. doc_setup_writeaddr_sector(docg3, sector, ofs);
  468. doc_delay(docg3, 1);
  469. out:
  470. return ret;
  471. }
  472. /**
  473. * doc_read_page_ecc_init - Initialize hardware ECC engine
  474. * @docg3: the device
  475. * @len: the number of bytes covered by the ECC (BCH covered)
  476. *
  477. * The function does initialize the hardware ECC engine to compute the Hamming
  478. * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
  479. *
  480. * Return 0 if succeeded, -EIO on error
  481. */
  482. static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
  483. {
  484. doc_writew(docg3, DOC_ECCCONF0_READ_MODE
  485. | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
  486. | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
  487. DOC_ECCCONF0);
  488. doc_delay(docg3, 4);
  489. doc_register_readb(docg3, DOC_FLASHCONTROL);
  490. return doc_wait_ready(docg3);
  491. }
  492. /**
  493. * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
  494. * @docg3: the device
  495. * @len: the number of bytes covered by the ECC (BCH covered)
  496. *
  497. * The function does initialize the hardware ECC engine to compute the Hamming
  498. * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
  499. *
  500. * Return 0 if succeeded, -EIO on error
  501. */
  502. static int doc_write_page_ecc_init(struct docg3 *docg3, int len)
  503. {
  504. doc_writew(docg3, DOC_ECCCONF0_WRITE_MODE
  505. | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
  506. | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
  507. DOC_ECCCONF0);
  508. doc_delay(docg3, 4);
  509. doc_register_readb(docg3, DOC_FLASHCONTROL);
  510. return doc_wait_ready(docg3);
  511. }
  512. /**
  513. * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
  514. * @docg3: the device
  515. *
  516. * Disables the hardware ECC generator and checker, for unchecked reads (as when
  517. * reading OOB only or write status byte).
  518. */
  519. static void doc_ecc_disable(struct docg3 *docg3)
  520. {
  521. doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
  522. doc_delay(docg3, 4);
  523. }
  524. /**
  525. * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
  526. * @docg3: the device
  527. * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
  528. *
  529. * This function programs the ECC hardware to compute the hamming code on the
  530. * last provided N bytes to the hardware generator.
  531. */
  532. static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
  533. {
  534. u8 ecc_conf1;
  535. ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
  536. ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
  537. ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
  538. doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
  539. }
  540. /**
  541. * doc_ecc_bch_fix_data - Fix if need be read data from flash
  542. * @docg3: the device
  543. * @buf: the buffer of read data (512 + 7 + 1 bytes)
  544. * @hwecc: the hardware calculated ECC.
  545. * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
  546. * area data, and calc_ecc the ECC calculated by the hardware generator.
  547. *
  548. * Checks if the received data matches the ECC, and if an error is detected,
  549. * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
  550. * understands the (data, ecc, syndroms) in an inverted order in comparison to
  551. * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
  552. * bit6 and bit 1, ...) for all ECC data.
  553. *
  554. * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
  555. * algorithm is used to decode this. However the hw operates on page
  556. * data in a bit order that is the reverse of that of the bch alg,
  557. * requiring that the bits be reversed on the result. Thanks to Ivan
  558. * Djelic for his analysis.
  559. *
  560. * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
  561. * errors were detected and cannot be fixed.
  562. */
  563. static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
  564. {
  565. u8 ecc[DOC_ECC_BCH_SIZE];
  566. int errorpos[DOC_ECC_BCH_T], i, numerrs;
  567. for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
  568. ecc[i] = bitrev8(hwecc[i]);
  569. numerrs = decode_bch(docg3_bch, NULL, DOC_ECC_BCH_COVERED_BYTES,
  570. NULL, ecc, NULL, errorpos);
  571. BUG_ON(numerrs == -EINVAL);
  572. if (numerrs < 0)
  573. goto out;
  574. for (i = 0; i < numerrs; i++)
  575. errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
  576. for (i = 0; i < numerrs; i++)
  577. if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
  578. /* error is located in data, correct it */
  579. change_bit(errorpos[i], buf);
  580. out:
  581. doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
  582. return numerrs;
  583. }
  584. /**
  585. * doc_read_page_prepare - Prepares reading data from a flash page
  586. * @docg3: the device
  587. * @block0: the first plane block index on flash memory
  588. * @block1: the second plane block index on flash memory
  589. * @page: the page index in the block
  590. * @offset: the offset in the page (must be a multiple of 4)
  591. *
  592. * Prepares the page to be read in the flash memory :
  593. * - tell ASIC to map the flash pages
  594. * - tell ASIC to be in read mode
  595. *
  596. * After a call to this method, a call to doc_read_page_finish is mandatory,
  597. * to end the read cycle of the flash.
  598. *
  599. * Read data from a flash page. The length to be read must be between 0 and
  600. * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
  601. * the extra bytes reading is not implemented).
  602. *
  603. * As pages are grouped by 2 (in 2 planes), reading from a page must be done
  604. * in two steps:
  605. * - one read of 512 bytes at offset 0
  606. * - one read of 512 bytes at offset 512 + 16
  607. *
  608. * Returns 0 if successful, -EIO if a read error occured.
  609. */
  610. static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
  611. int page, int offset)
  612. {
  613. int wear_area = 0, ret = 0;
  614. doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
  615. block0, block1, page, offset);
  616. if (offset >= DOC_LAYOUT_WEAR_OFFSET)
  617. wear_area = 1;
  618. if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
  619. return -EINVAL;
  620. doc_set_device_id(docg3, docg3->device_id);
  621. ret = doc_reset_seq(docg3);
  622. if (ret)
  623. goto err;
  624. /* Program the flash address block and page */
  625. ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
  626. if (ret)
  627. goto err;
  628. doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
  629. doc_delay(docg3, 2);
  630. doc_wait_ready(docg3);
  631. doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
  632. doc_delay(docg3, 1);
  633. if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
  634. offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
  635. doc_flash_address(docg3, offset >> 2);
  636. doc_delay(docg3, 1);
  637. doc_wait_ready(docg3);
  638. doc_flash_command(docg3, DOC_CMD_READ_FLASH);
  639. return 0;
  640. err:
  641. doc_writeb(docg3, 0, DOC_DATAEND);
  642. doc_delay(docg3, 2);
  643. return -EIO;
  644. }
  645. /**
  646. * doc_read_page_getbytes - Reads bytes from a prepared page
  647. * @docg3: the device
  648. * @len: the number of bytes to be read (must be a multiple of 4)
  649. * @buf: the buffer to be filled in (or NULL is forget bytes)
  650. * @first: 1 if first time read, DOC_READADDRESS should be set
  651. *
  652. */
  653. static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
  654. int first)
  655. {
  656. doc_read_data_area(docg3, buf, len, first);
  657. doc_delay(docg3, 2);
  658. return len;
  659. }
  660. /**
  661. * doc_write_page_putbytes - Writes bytes into a prepared page
  662. * @docg3: the device
  663. * @len: the number of bytes to be written
  664. * @buf: the buffer of input bytes
  665. *
  666. */
  667. static void doc_write_page_putbytes(struct docg3 *docg3, int len,
  668. const u_char *buf)
  669. {
  670. doc_write_data_area(docg3, buf, len);
  671. doc_delay(docg3, 2);
  672. }
  673. /**
  674. * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
  675. * @docg3: the device
  676. * @hwecc: the array of 7 integers where the hardware ecc will be stored
  677. */
  678. static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc)
  679. {
  680. int i;
  681. for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
  682. hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i));
  683. }
  684. /**
  685. * doc_page_finish - Ends reading/writing of a flash page
  686. * @docg3: the device
  687. */
  688. static void doc_page_finish(struct docg3 *docg3)
  689. {
  690. doc_writeb(docg3, 0, DOC_DATAEND);
  691. doc_delay(docg3, 2);
  692. }
  693. /**
  694. * doc_read_page_finish - Ends reading of a flash page
  695. * @docg3: the device
  696. *
  697. * As a side effect, resets the chip selector to 0. This ensures that after each
  698. * read operation, the floor 0 is selected. Therefore, if the systems halts, the
  699. * reboot will boot on floor 0, where the IPL is.
  700. */
  701. static void doc_read_page_finish(struct docg3 *docg3)
  702. {
  703. doc_page_finish(docg3);
  704. doc_set_device_id(docg3, 0);
  705. }
  706. /**
  707. * calc_block_sector - Calculate blocks, pages and ofs.
  708. * @from: offset in flash
  709. * @block0: first plane block index calculated
  710. * @block1: second plane block index calculated
  711. * @page: page calculated
  712. * @ofs: offset in page
  713. * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
  714. * reliable mode.
  715. *
  716. * The calculation is based on the reliable/normal mode. In normal mode, the 64
  717. * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
  718. * clones, only 32 pages per block are available.
  719. */
  720. static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
  721. int *ofs, int reliable)
  722. {
  723. uint sector, pages_biblock;
  724. pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES;
  725. if (reliable == 1 || reliable == 2)
  726. pages_biblock /= 2;
  727. sector = from / DOC_LAYOUT_PAGE_SIZE;
  728. *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES;
  729. *block1 = *block0 + 1;
  730. *page = sector % pages_biblock;
  731. *page /= DOC_LAYOUT_NBPLANES;
  732. if (reliable == 1 || reliable == 2)
  733. *page *= 2;
  734. if (sector % 2)
  735. *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
  736. else
  737. *ofs = 0;
  738. }
  739. /**
  740. * doc_read_oob - Read out of band bytes from flash
  741. * @mtd: the device
  742. * @from: the offset from first block and first page, in bytes, aligned on page
  743. * size
  744. * @ops: the mtd oob structure
  745. *
  746. * Reads flash memory OOB area of pages.
  747. *
  748. * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
  749. */
  750. static int doc_read_oob(struct mtd_info *mtd, loff_t from,
  751. struct mtd_oob_ops *ops)
  752. {
  753. struct docg3 *docg3 = mtd->priv;
  754. int block0, block1, page, ret, skip, ofs = 0;
  755. u8 *oobbuf = ops->oobbuf;
  756. u8 *buf = ops->datbuf;
  757. size_t len, ooblen, nbdata, nboob;
  758. u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
  759. if (buf)
  760. len = ops->len;
  761. else
  762. len = 0;
  763. if (oobbuf)
  764. ooblen = ops->ooblen;
  765. else
  766. ooblen = 0;
  767. if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
  768. oobbuf += ops->ooboffs;
  769. doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
  770. from, ops->mode, buf, len, oobbuf, ooblen);
  771. if (ooblen % DOC_LAYOUT_OOB_SIZE)
  772. return -EINVAL;
  773. if (from + len > mtd->size)
  774. return -EINVAL;
  775. ops->oobretlen = 0;
  776. ops->retlen = 0;
  777. ret = 0;
  778. skip = from % DOC_LAYOUT_PAGE_SIZE;
  779. while (!ret && (len > 0 || ooblen > 0)) {
  780. calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
  781. docg3->reliable);
  782. nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
  783. nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
  784. ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
  785. if (ret < 0)
  786. goto err;
  787. ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
  788. if (ret < 0)
  789. goto err_in_read;
  790. ret = doc_read_page_getbytes(docg3, skip, NULL, 1);
  791. if (ret < skip)
  792. goto err_in_read;
  793. ret = doc_read_page_getbytes(docg3, nbdata, buf, 0);
  794. if (ret < nbdata)
  795. goto err_in_read;
  796. doc_read_page_getbytes(docg3,
  797. DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
  798. NULL, 0);
  799. ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0);
  800. if (ret < nboob)
  801. goto err_in_read;
  802. doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
  803. NULL, 0);
  804. doc_get_bch_hw_ecc(docg3, hwecc);
  805. eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
  806. if (nboob >= DOC_LAYOUT_OOB_SIZE) {
  807. doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  808. oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
  809. oobbuf[4], oobbuf[5], oobbuf[6]);
  810. doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
  811. doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  812. oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
  813. oobbuf[12], oobbuf[13], oobbuf[14]);
  814. doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
  815. }
  816. doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
  817. doc_dbg("ECC HW_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  818. hwecc[0], hwecc[1], hwecc[2], hwecc[3], hwecc[4],
  819. hwecc[5], hwecc[6]);
  820. ret = -EIO;
  821. if (is_prot_seq_error(docg3))
  822. goto err_in_read;
  823. ret = 0;
  824. if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
  825. (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
  826. (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
  827. (ops->mode != MTD_OPS_RAW) &&
  828. (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
  829. ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
  830. if (ret < 0) {
  831. mtd->ecc_stats.failed++;
  832. ret = -EBADMSG;
  833. }
  834. if (ret > 0) {
  835. mtd->ecc_stats.corrected += ret;
  836. ret = -EUCLEAN;
  837. }
  838. }
  839. doc_read_page_finish(docg3);
  840. ops->retlen += nbdata;
  841. ops->oobretlen += nboob;
  842. buf += nbdata;
  843. oobbuf += nboob;
  844. len -= nbdata;
  845. ooblen -= nboob;
  846. from += DOC_LAYOUT_PAGE_SIZE;
  847. skip = 0;
  848. }
  849. return ret;
  850. err_in_read:
  851. doc_read_page_finish(docg3);
  852. err:
  853. return ret;
  854. }
  855. /**
  856. * doc_read - Read bytes from flash
  857. * @mtd: the device
  858. * @from: the offset from first block and first page, in bytes, aligned on page
  859. * size
  860. * @len: the number of bytes to read (must be a multiple of 4)
  861. * @retlen: the number of bytes actually read
  862. * @buf: the filled in buffer
  863. *
  864. * Reads flash memory pages. This function does not read the OOB chunk, but only
  865. * the page data.
  866. *
  867. * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
  868. */
  869. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  870. size_t *retlen, u_char *buf)
  871. {
  872. struct mtd_oob_ops ops;
  873. size_t ret;
  874. memset(&ops, 0, sizeof(ops));
  875. ops.datbuf = buf;
  876. ops.len = len;
  877. ops.mode = MTD_OPS_AUTO_OOB;
  878. ret = doc_read_oob(mtd, from, &ops);
  879. *retlen = ops.retlen;
  880. return ret;
  881. }
  882. static int doc_reload_bbt(struct docg3 *docg3)
  883. {
  884. int block = DOC_LAYOUT_BLOCK_BBT;
  885. int ret = 0, nbpages, page;
  886. u_char *buf = docg3->bbt;
  887. nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
  888. for (page = 0; !ret && (page < nbpages); page++) {
  889. ret = doc_read_page_prepare(docg3, block, block + 1,
  890. page + DOC_LAYOUT_PAGE_BBT, 0);
  891. if (!ret)
  892. ret = doc_read_page_ecc_init(docg3,
  893. DOC_LAYOUT_PAGE_SIZE);
  894. if (!ret)
  895. doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
  896. buf, 1);
  897. buf += DOC_LAYOUT_PAGE_SIZE;
  898. }
  899. doc_read_page_finish(docg3);
  900. return ret;
  901. }
  902. /**
  903. * doc_block_isbad - Checks whether a block is good or not
  904. * @mtd: the device
  905. * @from: the offset to find the correct block
  906. *
  907. * Returns 1 if block is bad, 0 if block is good
  908. */
  909. static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
  910. {
  911. struct docg3 *docg3 = mtd->priv;
  912. int block0, block1, page, ofs, is_good;
  913. calc_block_sector(from, &block0, &block1, &page, &ofs,
  914. docg3->reliable);
  915. doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
  916. from, block0, block1, page, ofs);
  917. if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
  918. return 0;
  919. if (block1 > docg3->max_block)
  920. return -EINVAL;
  921. is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
  922. return !is_good;
  923. }
  924. #if 0
  925. /**
  926. * doc_get_erase_count - Get block erase count
  927. * @docg3: the device
  928. * @from: the offset in which the block is.
  929. *
  930. * Get the number of times a block was erased. The number is the maximum of
  931. * erase times between first and second plane (which should be equal normally).
  932. *
  933. * Returns The number of erases, or -EINVAL or -EIO on error.
  934. */
  935. static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
  936. {
  937. u8 buf[DOC_LAYOUT_WEAR_SIZE];
  938. int ret, plane1_erase_count, plane2_erase_count;
  939. int block0, block1, page, ofs;
  940. doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
  941. if (from % DOC_LAYOUT_PAGE_SIZE)
  942. return -EINVAL;
  943. calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
  944. if (block1 > docg3->max_block)
  945. return -EINVAL;
  946. ret = doc_reset_seq(docg3);
  947. if (!ret)
  948. ret = doc_read_page_prepare(docg3, block0, block1, page,
  949. ofs + DOC_LAYOUT_WEAR_OFFSET);
  950. if (!ret)
  951. ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
  952. buf, 1);
  953. doc_read_page_finish(docg3);
  954. if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
  955. return -EIO;
  956. plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
  957. | ((u8)(~buf[5]) << 16);
  958. plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
  959. | ((u8)(~buf[7]) << 16);
  960. return max(plane1_erase_count, plane2_erase_count);
  961. }
  962. #endif
  963. /**
  964. * doc_get_op_status - get erase/write operation status
  965. * @docg3: the device
  966. *
  967. * Queries the status from the chip, and returns it
  968. *
  969. * Returns the status (bits DOC_PLANES_STATUS_*)
  970. */
  971. static int doc_get_op_status(struct docg3 *docg3)
  972. {
  973. u8 status;
  974. doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
  975. doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
  976. doc_delay(docg3, 5);
  977. doc_ecc_disable(docg3);
  978. doc_read_data_area(docg3, &status, 1, 1);
  979. return status;
  980. }
  981. /**
  982. * doc_write_erase_wait_status - wait for write or erase completion
  983. * @docg3: the device
  984. *
  985. * Wait for the chip to be ready again after erase or write operation, and check
  986. * erase/write status.
  987. *
  988. * Returns 0 if erase successfull, -EIO if erase/write issue, -ETIMEOUT if
  989. * timeout
  990. */
  991. static int doc_write_erase_wait_status(struct docg3 *docg3)
  992. {
  993. int status, ret = 0;
  994. if (!doc_is_ready(docg3))
  995. usleep_range(3000, 3000);
  996. if (!doc_is_ready(docg3)) {
  997. doc_dbg("Timeout reached and the chip is still not ready\n");
  998. ret = -EAGAIN;
  999. goto out;
  1000. }
  1001. status = doc_get_op_status(docg3);
  1002. if (status & DOC_PLANES_STATUS_FAIL) {
  1003. doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
  1004. status);
  1005. ret = -EIO;
  1006. }
  1007. out:
  1008. doc_page_finish(docg3);
  1009. return ret;
  1010. }
  1011. /**
  1012. * doc_erase_block - Erase a couple of blocks
  1013. * @docg3: the device
  1014. * @block0: the first block to erase (leftmost plane)
  1015. * @block1: the second block to erase (rightmost plane)
  1016. *
  1017. * Erase both blocks, and return operation status
  1018. *
  1019. * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
  1020. * ready for too long
  1021. */
  1022. static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
  1023. {
  1024. int ret, sector;
  1025. doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
  1026. ret = doc_reset_seq(docg3);
  1027. if (ret)
  1028. return -EIO;
  1029. doc_set_reliable_mode(docg3);
  1030. doc_flash_sequence(docg3, DOC_SEQ_ERASE);
  1031. sector = block0 << DOC_ADDR_BLOCK_SHIFT;
  1032. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  1033. doc_setup_addr_sector(docg3, sector);
  1034. sector = block1 << DOC_ADDR_BLOCK_SHIFT;
  1035. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  1036. doc_setup_addr_sector(docg3, sector);
  1037. doc_delay(docg3, 1);
  1038. doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
  1039. doc_delay(docg3, 2);
  1040. if (is_prot_seq_error(docg3)) {
  1041. doc_err("Erase blocks %d,%d error\n", block0, block1);
  1042. return -EIO;
  1043. }
  1044. return doc_write_erase_wait_status(docg3);
  1045. }
  1046. /**
  1047. * doc_erase - Erase a portion of the chip
  1048. * @mtd: the device
  1049. * @info: the erase info
  1050. *
  1051. * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
  1052. * split into 2 pages of 512 bytes on 2 contiguous blocks.
  1053. *
  1054. * Returns 0 if erase successful, -EINVAL if adressing error, -EIO if erase
  1055. * issue
  1056. */
  1057. static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
  1058. {
  1059. struct docg3 *docg3 = mtd->priv;
  1060. uint64_t len;
  1061. int block0, block1, page, ret, ofs = 0;
  1062. doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
  1063. doc_set_device_id(docg3, docg3->device_id);
  1064. info->state = MTD_ERASE_PENDING;
  1065. calc_block_sector(info->addr + info->len, &block0, &block1, &page,
  1066. &ofs, docg3->reliable);
  1067. ret = -EINVAL;
  1068. if (info->addr + info->len > mtd->size || page || ofs)
  1069. goto reset_err;
  1070. ret = 0;
  1071. calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
  1072. docg3->reliable);
  1073. doc_set_reliable_mode(docg3);
  1074. for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
  1075. info->state = MTD_ERASING;
  1076. ret = doc_erase_block(docg3, block0, block1);
  1077. block0 += 2;
  1078. block1 += 2;
  1079. }
  1080. if (ret)
  1081. goto reset_err;
  1082. info->state = MTD_ERASE_DONE;
  1083. return 0;
  1084. reset_err:
  1085. info->state = MTD_ERASE_FAILED;
  1086. return ret;
  1087. }
  1088. /**
  1089. * doc_write_page - Write a single page to the chip
  1090. * @docg3: the device
  1091. * @to: the offset from first block and first page, in bytes, aligned on page
  1092. * size
  1093. * @buf: buffer to get bytes from
  1094. * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
  1095. * written)
  1096. * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
  1097. * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
  1098. * remaining ones are filled with hardware Hamming and BCH
  1099. * computations. Its value is not meaningfull is oob == NULL.
  1100. *
  1101. * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
  1102. * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
  1103. * BCH generator if autoecc is not null.
  1104. *
  1105. * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
  1106. */
  1107. static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
  1108. const u_char *oob, int autoecc)
  1109. {
  1110. int block0, block1, page, ret, ofs = 0;
  1111. u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
  1112. doc_dbg("doc_write_page(to=%lld)\n", to);
  1113. calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
  1114. doc_set_device_id(docg3, docg3->device_id);
  1115. ret = doc_reset_seq(docg3);
  1116. if (ret)
  1117. goto err;
  1118. /* Program the flash address block and page */
  1119. ret = doc_write_seek(docg3, block0, block1, page, ofs);
  1120. if (ret)
  1121. goto err;
  1122. doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
  1123. doc_delay(docg3, 2);
  1124. doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
  1125. if (oob && autoecc) {
  1126. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
  1127. doc_delay(docg3, 2);
  1128. oob += DOC_LAYOUT_OOB_UNUSED_OFS;
  1129. hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
  1130. doc_delay(docg3, 2);
  1131. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
  1132. &hamming);
  1133. doc_delay(docg3, 2);
  1134. doc_get_bch_hw_ecc(docg3, hwecc);
  1135. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
  1136. doc_delay(docg3, 2);
  1137. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
  1138. }
  1139. if (oob && !autoecc)
  1140. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
  1141. doc_delay(docg3, 2);
  1142. doc_page_finish(docg3);
  1143. doc_delay(docg3, 2);
  1144. doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
  1145. doc_delay(docg3, 2);
  1146. /*
  1147. * The wait status will perform another doc_page_finish() call, but that
  1148. * seems to please the docg3, so leave it.
  1149. */
  1150. ret = doc_write_erase_wait_status(docg3);
  1151. return ret;
  1152. err:
  1153. doc_read_page_finish(docg3);
  1154. return ret;
  1155. }
  1156. /**
  1157. * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
  1158. * @ops: the oob operations
  1159. *
  1160. * Returns 0 or 1 if success, -EINVAL if invalid oob mode
  1161. */
  1162. static int doc_guess_autoecc(struct mtd_oob_ops *ops)
  1163. {
  1164. int autoecc;
  1165. switch (ops->mode) {
  1166. case MTD_OPS_PLACE_OOB:
  1167. case MTD_OPS_AUTO_OOB:
  1168. autoecc = 1;
  1169. break;
  1170. case MTD_OPS_RAW:
  1171. autoecc = 0;
  1172. break;
  1173. default:
  1174. autoecc = -EINVAL;
  1175. }
  1176. return autoecc;
  1177. }
  1178. /**
  1179. * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
  1180. * @dst: the target 16 bytes OOB buffer
  1181. * @oobsrc: the source 8 bytes non-ECC OOB buffer
  1182. *
  1183. */
  1184. static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
  1185. {
  1186. memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
  1187. dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
  1188. }
  1189. /**
  1190. * doc_backup_oob - Backup OOB into docg3 structure
  1191. * @docg3: the device
  1192. * @to: the page offset in the chip
  1193. * @ops: the OOB size and buffer
  1194. *
  1195. * As the docg3 should write a page with its OOB in one pass, and some userland
  1196. * applications do write_oob() to setup the OOB and then write(), store the OOB
  1197. * into a temporary storage. This is very dangerous, as 2 concurrent
  1198. * applications could store an OOB, and then write their pages (which will
  1199. * result into one having its OOB corrupted).
  1200. *
  1201. * The only reliable way would be for userland to call doc_write_oob() with both
  1202. * the page data _and_ the OOB area.
  1203. *
  1204. * Returns 0 if success, -EINVAL if ops content invalid
  1205. */
  1206. static int doc_backup_oob(struct docg3 *docg3, loff_t to,
  1207. struct mtd_oob_ops *ops)
  1208. {
  1209. int ooblen = ops->ooblen, autoecc;
  1210. if (ooblen != DOC_LAYOUT_OOB_SIZE)
  1211. return -EINVAL;
  1212. autoecc = doc_guess_autoecc(ops);
  1213. if (autoecc < 0)
  1214. return autoecc;
  1215. docg3->oob_write_ofs = to;
  1216. docg3->oob_autoecc = autoecc;
  1217. if (ops->mode == MTD_OPS_AUTO_OOB) {
  1218. doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
  1219. ops->oobretlen = 8;
  1220. } else {
  1221. memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
  1222. ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
  1223. }
  1224. return 0;
  1225. }
  1226. /**
  1227. * doc_write_oob - Write out of band bytes to flash
  1228. * @mtd: the device
  1229. * @ofs: the offset from first block and first page, in bytes, aligned on page
  1230. * size
  1231. * @ops: the mtd oob structure
  1232. *
  1233. * Either write OOB data into a temporary buffer, for the subsequent write
  1234. * page. The provided OOB should be 16 bytes long. If a data buffer is provided
  1235. * as well, issue the page write.
  1236. * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
  1237. * still be filled in if asked for).
  1238. *
  1239. * Returns 0 is successfull, EINVAL if length is not 14 bytes
  1240. */
  1241. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  1242. struct mtd_oob_ops *ops)
  1243. {
  1244. struct docg3 *docg3 = mtd->priv;
  1245. int block0, block1, page, ret, pofs = 0, autoecc, oobdelta;
  1246. u8 *oobbuf = ops->oobbuf;
  1247. u8 *buf = ops->datbuf;
  1248. size_t len, ooblen;
  1249. u8 oob[DOC_LAYOUT_OOB_SIZE];
  1250. if (buf)
  1251. len = ops->len;
  1252. else
  1253. len = 0;
  1254. if (oobbuf)
  1255. ooblen = ops->ooblen;
  1256. else
  1257. ooblen = 0;
  1258. if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
  1259. oobbuf += ops->ooboffs;
  1260. doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
  1261. ofs, ops->mode, buf, len, oobbuf, ooblen);
  1262. switch (ops->mode) {
  1263. case MTD_OPS_PLACE_OOB:
  1264. case MTD_OPS_RAW:
  1265. oobdelta = mtd->oobsize;
  1266. break;
  1267. case MTD_OPS_AUTO_OOB:
  1268. oobdelta = mtd->ecclayout->oobavail;
  1269. break;
  1270. default:
  1271. oobdelta = 0;
  1272. }
  1273. if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
  1274. (ofs % DOC_LAYOUT_PAGE_SIZE))
  1275. return -EINVAL;
  1276. if (len && ooblen &&
  1277. (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
  1278. return -EINVAL;
  1279. if (ofs + len > mtd->size)
  1280. return -EINVAL;
  1281. ops->oobretlen = 0;
  1282. ops->retlen = 0;
  1283. ret = 0;
  1284. if (len == 0 && ooblen == 0)
  1285. return -EINVAL;
  1286. if (len == 0 && ooblen > 0)
  1287. return doc_backup_oob(docg3, ofs, ops);
  1288. autoecc = doc_guess_autoecc(ops);
  1289. if (autoecc < 0)
  1290. return autoecc;
  1291. while (!ret && len > 0) {
  1292. memset(oob, 0, sizeof(oob));
  1293. if (ofs == docg3->oob_write_ofs)
  1294. memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
  1295. else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
  1296. doc_fill_autooob(oob, oobbuf);
  1297. else if (ooblen > 0)
  1298. memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
  1299. ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
  1300. ofs += DOC_LAYOUT_PAGE_SIZE;
  1301. len -= DOC_LAYOUT_PAGE_SIZE;
  1302. buf += DOC_LAYOUT_PAGE_SIZE;
  1303. if (ooblen) {
  1304. oobbuf += oobdelta;
  1305. ooblen -= oobdelta;
  1306. ops->oobretlen += oobdelta;
  1307. }
  1308. ops->retlen += DOC_LAYOUT_PAGE_SIZE;
  1309. }
  1310. err:
  1311. doc_set_device_id(docg3, 0);
  1312. return ret;
  1313. }
  1314. /**
  1315. * doc_write - Write a buffer to the chip
  1316. * @mtd: the device
  1317. * @to: the offset from first block and first page, in bytes, aligned on page
  1318. * size
  1319. * @len: the number of bytes to write (must be a full page size, ie. 512)
  1320. * @retlen: the number of bytes actually written (0 or 512)
  1321. * @buf: the buffer to get bytes from
  1322. *
  1323. * Writes data to the chip.
  1324. *
  1325. * Returns 0 if write successful, -EIO if write error
  1326. */
  1327. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  1328. size_t *retlen, const u_char *buf)
  1329. {
  1330. struct docg3 *docg3 = mtd->priv;
  1331. int ret;
  1332. struct mtd_oob_ops ops;
  1333. doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
  1334. ops.datbuf = (char *)buf;
  1335. ops.len = len;
  1336. ops.mode = MTD_OPS_PLACE_OOB;
  1337. ops.oobbuf = NULL;
  1338. ops.ooblen = 0;
  1339. ops.ooboffs = 0;
  1340. ret = doc_write_oob(mtd, to, &ops);
  1341. *retlen = ops.retlen;
  1342. return ret;
  1343. }
  1344. static struct docg3 *sysfs_dev2docg3(struct device *dev,
  1345. struct device_attribute *attr)
  1346. {
  1347. int floor;
  1348. struct platform_device *pdev = to_platform_device(dev);
  1349. struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
  1350. floor = attr->attr.name[1] - '0';
  1351. if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
  1352. return NULL;
  1353. else
  1354. return docg3_floors[floor]->priv;
  1355. }
  1356. static ssize_t dps0_is_key_locked(struct device *dev,
  1357. struct device_attribute *attr, char *buf)
  1358. {
  1359. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1360. int dps0;
  1361. doc_set_device_id(docg3, docg3->device_id);
  1362. dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
  1363. doc_set_device_id(docg3, 0);
  1364. return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
  1365. }
  1366. static ssize_t dps1_is_key_locked(struct device *dev,
  1367. struct device_attribute *attr, char *buf)
  1368. {
  1369. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1370. int dps1;
  1371. doc_set_device_id(docg3, docg3->device_id);
  1372. dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
  1373. doc_set_device_id(docg3, 0);
  1374. return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
  1375. }
  1376. static ssize_t dps0_insert_key(struct device *dev,
  1377. struct device_attribute *attr,
  1378. const char *buf, size_t count)
  1379. {
  1380. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1381. int i;
  1382. if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
  1383. return -EINVAL;
  1384. doc_set_device_id(docg3, docg3->device_id);
  1385. for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
  1386. doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
  1387. doc_set_device_id(docg3, 0);
  1388. return count;
  1389. }
  1390. static ssize_t dps1_insert_key(struct device *dev,
  1391. struct device_attribute *attr,
  1392. const char *buf, size_t count)
  1393. {
  1394. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1395. int i;
  1396. if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
  1397. return -EINVAL;
  1398. doc_set_device_id(docg3, docg3->device_id);
  1399. for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
  1400. doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
  1401. doc_set_device_id(docg3, 0);
  1402. return count;
  1403. }
  1404. #define FLOOR_SYSFS(id) { \
  1405. __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
  1406. __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
  1407. __ATTR(f##id##_dps0_protection_key, S_IWUGO, NULL, dps0_insert_key), \
  1408. __ATTR(f##id##_dps1_protection_key, S_IWUGO, NULL, dps1_insert_key), \
  1409. }
  1410. static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
  1411. FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
  1412. };
  1413. static int doc_register_sysfs(struct platform_device *pdev,
  1414. struct mtd_info **floors)
  1415. {
  1416. int ret = 0, floor, i = 0;
  1417. struct device *dev = &pdev->dev;
  1418. for (floor = 0; !ret && floor < DOC_MAX_NBFLOORS && floors[floor];
  1419. floor++)
  1420. for (i = 0; !ret && i < 4; i++)
  1421. ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
  1422. if (!ret)
  1423. return 0;
  1424. do {
  1425. while (--i >= 0)
  1426. device_remove_file(dev, &doc_sys_attrs[floor][i]);
  1427. i = 4;
  1428. } while (--floor >= 0);
  1429. return ret;
  1430. }
  1431. static void doc_unregister_sysfs(struct platform_device *pdev,
  1432. struct mtd_info **floors)
  1433. {
  1434. struct device *dev = &pdev->dev;
  1435. int floor, i;
  1436. for (floor = 0; floor < DOC_MAX_NBFLOORS && floors[floor];
  1437. floor++)
  1438. for (i = 0; i < 4; i++)
  1439. device_remove_file(dev, &doc_sys_attrs[floor][i]);
  1440. }
  1441. /*
  1442. * Debug sysfs entries
  1443. */
  1444. static int dbg_flashctrl_show(struct seq_file *s, void *p)
  1445. {
  1446. struct docg3 *docg3 = (struct docg3 *)s->private;
  1447. int pos = 0;
  1448. u8 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  1449. pos += seq_printf(s,
  1450. "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
  1451. fctrl,
  1452. fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
  1453. fctrl & DOC_CTRL_CE ? "active" : "inactive",
  1454. fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
  1455. fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
  1456. fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
  1457. return pos;
  1458. }
  1459. DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
  1460. static int dbg_asicmode_show(struct seq_file *s, void *p)
  1461. {
  1462. struct docg3 *docg3 = (struct docg3 *)s->private;
  1463. int pos = 0;
  1464. int pctrl = doc_register_readb(docg3, DOC_ASICMODE);
  1465. int mode = pctrl & 0x03;
  1466. pos += seq_printf(s,
  1467. "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
  1468. pctrl,
  1469. pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
  1470. pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
  1471. pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
  1472. pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
  1473. pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
  1474. mode >> 1, mode & 0x1);
  1475. switch (mode) {
  1476. case DOC_ASICMODE_RESET:
  1477. pos += seq_printf(s, "reset");
  1478. break;
  1479. case DOC_ASICMODE_NORMAL:
  1480. pos += seq_printf(s, "normal");
  1481. break;
  1482. case DOC_ASICMODE_POWERDOWN:
  1483. pos += seq_printf(s, "powerdown");
  1484. break;
  1485. }
  1486. pos += seq_printf(s, ")\n");
  1487. return pos;
  1488. }
  1489. DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
  1490. static int dbg_device_id_show(struct seq_file *s, void *p)
  1491. {
  1492. struct docg3 *docg3 = (struct docg3 *)s->private;
  1493. int pos = 0;
  1494. int id = doc_register_readb(docg3, DOC_DEVICESELECT);
  1495. pos += seq_printf(s, "DeviceId = %d\n", id);
  1496. return pos;
  1497. }
  1498. DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
  1499. static int dbg_protection_show(struct seq_file *s, void *p)
  1500. {
  1501. struct docg3 *docg3 = (struct docg3 *)s->private;
  1502. int pos = 0;
  1503. int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
  1504. protect = doc_register_readb(docg3, DOC_PROTECTION);
  1505. dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
  1506. dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
  1507. dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
  1508. dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
  1509. dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
  1510. dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
  1511. pos += seq_printf(s, "Protection = 0x%02x (",
  1512. protect);
  1513. if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
  1514. pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
  1515. if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
  1516. pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
  1517. if (protect & DOC_PROTECT_LOCK_INPUT)
  1518. pos += seq_printf(s, "LOCK_INPUT,");
  1519. if (protect & DOC_PROTECT_STICKY_LOCK)
  1520. pos += seq_printf(s, "STICKY_LOCK,");
  1521. if (protect & DOC_PROTECT_PROTECTION_ENABLED)
  1522. pos += seq_printf(s, "PROTECTION ON,");
  1523. if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
  1524. pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
  1525. if (protect & DOC_PROTECT_PROTECTION_ERROR)
  1526. pos += seq_printf(s, "PROTECT_ERR,");
  1527. else
  1528. pos += seq_printf(s, "NO_PROTECT_ERR");
  1529. pos += seq_printf(s, ")\n");
  1530. pos += seq_printf(s, "DPS0 = 0x%02x : "
  1531. "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
  1532. "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
  1533. dps0, dps0_low, dps0_high,
  1534. !!(dps0 & DOC_DPS_OTP_PROTECTED),
  1535. !!(dps0 & DOC_DPS_READ_PROTECTED),
  1536. !!(dps0 & DOC_DPS_WRITE_PROTECTED),
  1537. !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
  1538. !!(dps0 & DOC_DPS_KEY_OK));
  1539. pos += seq_printf(s, "DPS1 = 0x%02x : "
  1540. "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
  1541. "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
  1542. dps1, dps1_low, dps1_high,
  1543. !!(dps1 & DOC_DPS_OTP_PROTECTED),
  1544. !!(dps1 & DOC_DPS_READ_PROTECTED),
  1545. !!(dps1 & DOC_DPS_WRITE_PROTECTED),
  1546. !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
  1547. !!(dps1 & DOC_DPS_KEY_OK));
  1548. return pos;
  1549. }
  1550. DEBUGFS_RO_ATTR(protection, dbg_protection_show);
  1551. static int __init doc_dbg_register(struct docg3 *docg3)
  1552. {
  1553. struct dentry *root, *entry;
  1554. root = debugfs_create_dir("docg3", NULL);
  1555. if (!root)
  1556. return -ENOMEM;
  1557. entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
  1558. &flashcontrol_fops);
  1559. if (entry)
  1560. entry = debugfs_create_file("asic_mode", S_IRUSR, root,
  1561. docg3, &asic_mode_fops);
  1562. if (entry)
  1563. entry = debugfs_create_file("device_id", S_IRUSR, root,
  1564. docg3, &device_id_fops);
  1565. if (entry)
  1566. entry = debugfs_create_file("protection", S_IRUSR, root,
  1567. docg3, &protection_fops);
  1568. if (entry) {
  1569. docg3->debugfs_root = root;
  1570. return 0;
  1571. } else {
  1572. debugfs_remove_recursive(root);
  1573. return -ENOMEM;
  1574. }
  1575. }
  1576. static void __exit doc_dbg_unregister(struct docg3 *docg3)
  1577. {
  1578. debugfs_remove_recursive(docg3->debugfs_root);
  1579. }
  1580. /**
  1581. * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
  1582. * @chip_id: The chip ID of the supported chip
  1583. * @mtd: The structure to fill
  1584. */
  1585. static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
  1586. {
  1587. struct docg3 *docg3 = mtd->priv;
  1588. int cfg;
  1589. cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
  1590. docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
  1591. docg3->reliable = reliable_mode;
  1592. switch (chip_id) {
  1593. case DOC_CHIPID_G3:
  1594. mtd->name = kasprintf(GFP_KERNEL, "docg3.%d",
  1595. docg3->device_id);
  1596. docg3->max_block = 2047;
  1597. break;
  1598. }
  1599. mtd->type = MTD_NANDFLASH;
  1600. mtd->flags = MTD_CAP_NANDFLASH;
  1601. mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
  1602. if (docg3->reliable == 2)
  1603. mtd->size /= 2;
  1604. mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
  1605. if (docg3->reliable == 2)
  1606. mtd->erasesize /= 2;
  1607. mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
  1608. mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
  1609. mtd->owner = THIS_MODULE;
  1610. mtd->_erase = doc_erase;
  1611. mtd->_read = doc_read;
  1612. mtd->_write = doc_write;
  1613. mtd->_read_oob = doc_read_oob;
  1614. mtd->_write_oob = doc_write_oob;
  1615. mtd->_block_isbad = doc_block_isbad;
  1616. mtd->ecclayout = &docg3_oobinfo;
  1617. mtd->ecc_strength = DOC_ECC_BCH_T;
  1618. }
  1619. /**
  1620. * doc_probe_device - Check if a device is available
  1621. * @base: the io space where the device is probed
  1622. * @floor: the floor of the probed device
  1623. * @dev: the device
  1624. *
  1625. * Checks whether a device at the specified IO range, and floor is available.
  1626. *
  1627. * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
  1628. * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
  1629. * launched.
  1630. */
  1631. static struct mtd_info * __init
  1632. doc_probe_device(void __iomem *base, int floor, struct device *dev)
  1633. {
  1634. int ret, bbt_nbpages;
  1635. u16 chip_id, chip_id_inv;
  1636. struct docg3 *docg3;
  1637. struct mtd_info *mtd;
  1638. ret = -ENOMEM;
  1639. docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
  1640. if (!docg3)
  1641. goto nomem1;
  1642. mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
  1643. if (!mtd)
  1644. goto nomem2;
  1645. mtd->priv = docg3;
  1646. bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
  1647. 8 * DOC_LAYOUT_PAGE_SIZE);
  1648. docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
  1649. if (!docg3->bbt)
  1650. goto nomem3;
  1651. docg3->dev = dev;
  1652. docg3->device_id = floor;
  1653. docg3->base = base;
  1654. doc_set_device_id(docg3, docg3->device_id);
  1655. if (!floor)
  1656. doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
  1657. doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
  1658. chip_id = doc_register_readw(docg3, DOC_CHIPID);
  1659. chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
  1660. ret = 0;
  1661. if (chip_id != (u16)(~chip_id_inv)) {
  1662. goto nomem3;
  1663. }
  1664. switch (chip_id) {
  1665. case DOC_CHIPID_G3:
  1666. doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
  1667. base, floor);
  1668. break;
  1669. default:
  1670. doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
  1671. goto nomem3;
  1672. }
  1673. doc_set_driver_info(chip_id, mtd);
  1674. doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
  1675. doc_reload_bbt(docg3);
  1676. return mtd;
  1677. nomem3:
  1678. kfree(mtd);
  1679. nomem2:
  1680. kfree(docg3);
  1681. nomem1:
  1682. return ERR_PTR(ret);
  1683. }
  1684. /**
  1685. * doc_release_device - Release a docg3 floor
  1686. * @mtd: the device
  1687. */
  1688. static void doc_release_device(struct mtd_info *mtd)
  1689. {
  1690. struct docg3 *docg3 = mtd->priv;
  1691. mtd_device_unregister(mtd);
  1692. kfree(docg3->bbt);
  1693. kfree(docg3);
  1694. kfree(mtd->name);
  1695. kfree(mtd);
  1696. }
  1697. /**
  1698. * docg3_resume - Awakens docg3 floor
  1699. * @pdev: platfrom device
  1700. *
  1701. * Returns 0 (always successfull)
  1702. */
  1703. static int docg3_resume(struct platform_device *pdev)
  1704. {
  1705. int i;
  1706. struct mtd_info **docg3_floors, *mtd;
  1707. struct docg3 *docg3;
  1708. docg3_floors = platform_get_drvdata(pdev);
  1709. mtd = docg3_floors[0];
  1710. docg3 = mtd->priv;
  1711. doc_dbg("docg3_resume()\n");
  1712. for (i = 0; i < 12; i++)
  1713. doc_readb(docg3, DOC_IOSPACE_IPL);
  1714. return 0;
  1715. }
  1716. /**
  1717. * docg3_suspend - Put in low power mode the docg3 floor
  1718. * @pdev: platform device
  1719. * @state: power state
  1720. *
  1721. * Shuts off most of docg3 circuitery to lower power consumption.
  1722. *
  1723. * Returns 0 if suspend succeeded, -EIO if chip refused suspend
  1724. */
  1725. static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
  1726. {
  1727. int floor, i;
  1728. struct mtd_info **docg3_floors, *mtd;
  1729. struct docg3 *docg3;
  1730. u8 ctrl, pwr_down;
  1731. docg3_floors = platform_get_drvdata(pdev);
  1732. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
  1733. mtd = docg3_floors[floor];
  1734. if (!mtd)
  1735. continue;
  1736. docg3 = mtd->priv;
  1737. doc_writeb(docg3, floor, DOC_DEVICESELECT);
  1738. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  1739. ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
  1740. doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
  1741. for (i = 0; i < 10; i++) {
  1742. usleep_range(3000, 4000);
  1743. pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
  1744. if (pwr_down & DOC_POWERDOWN_READY)
  1745. break;
  1746. }
  1747. if (pwr_down & DOC_POWERDOWN_READY) {
  1748. doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
  1749. floor);
  1750. } else {
  1751. doc_err("docg3_suspend(): floor %d powerdown failed\n",
  1752. floor);
  1753. return -EIO;
  1754. }
  1755. }
  1756. mtd = docg3_floors[0];
  1757. docg3 = mtd->priv;
  1758. doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
  1759. return 0;
  1760. }
  1761. /**
  1762. * doc_probe - Probe the IO space for a DiskOnChip G3 chip
  1763. * @pdev: platform device
  1764. *
  1765. * Probes for a G3 chip at the specified IO space in the platform data
  1766. * ressources. The floor 0 must be available.
  1767. *
  1768. * Returns 0 on success, -ENOMEM, -ENXIO on error
  1769. */
  1770. static int __init docg3_probe(struct platform_device *pdev)
  1771. {
  1772. struct device *dev = &pdev->dev;
  1773. struct mtd_info *mtd;
  1774. struct resource *ress;
  1775. void __iomem *base;
  1776. int ret, floor, found = 0;
  1777. struct mtd_info **docg3_floors;
  1778. ret = -ENXIO;
  1779. ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1780. if (!ress) {
  1781. dev_err(dev, "No I/O memory resource defined\n");
  1782. goto noress;
  1783. }
  1784. base = ioremap(ress->start, DOC_IOSPACE_SIZE);
  1785. ret = -ENOMEM;
  1786. docg3_floors = kzalloc(sizeof(*docg3_floors) * DOC_MAX_NBFLOORS,
  1787. GFP_KERNEL);
  1788. if (!docg3_floors)
  1789. goto nomem1;
  1790. docg3_bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
  1791. DOC_ECC_BCH_PRIMPOLY);
  1792. if (!docg3_bch)
  1793. goto nomem2;
  1794. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
  1795. mtd = doc_probe_device(base, floor, dev);
  1796. if (IS_ERR(mtd)) {
  1797. ret = PTR_ERR(mtd);
  1798. goto err_probe;
  1799. }
  1800. if (!mtd) {
  1801. if (floor == 0)
  1802. goto notfound;
  1803. else
  1804. continue;
  1805. }
  1806. docg3_floors[floor] = mtd;
  1807. ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
  1808. 0);
  1809. if (ret)
  1810. goto err_probe;
  1811. found++;
  1812. }
  1813. ret = doc_register_sysfs(pdev, docg3_floors);
  1814. if (ret)
  1815. goto err_probe;
  1816. if (!found)
  1817. goto notfound;
  1818. platform_set_drvdata(pdev, docg3_floors);
  1819. doc_dbg_register(docg3_floors[0]->priv);
  1820. return 0;
  1821. notfound:
  1822. ret = -ENODEV;
  1823. dev_info(dev, "No supported DiskOnChip found\n");
  1824. err_probe:
  1825. free_bch(docg3_bch);
  1826. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
  1827. if (docg3_floors[floor])
  1828. doc_release_device(docg3_floors[floor]);
  1829. nomem2:
  1830. kfree(docg3_floors);
  1831. nomem1:
  1832. iounmap(base);
  1833. noress:
  1834. return ret;
  1835. }
  1836. /**
  1837. * docg3_release - Release the driver
  1838. * @pdev: the platform device
  1839. *
  1840. * Returns 0
  1841. */
  1842. static int __exit docg3_release(struct platform_device *pdev)
  1843. {
  1844. struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
  1845. struct docg3 *docg3 = docg3_floors[0]->priv;
  1846. void __iomem *base = docg3->base;
  1847. int floor;
  1848. doc_unregister_sysfs(pdev, docg3_floors);
  1849. doc_dbg_unregister(docg3);
  1850. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
  1851. if (docg3_floors[floor])
  1852. doc_release_device(docg3_floors[floor]);
  1853. kfree(docg3_floors);
  1854. free_bch(docg3_bch);
  1855. iounmap(base);
  1856. return 0;
  1857. }
  1858. static struct platform_driver g3_driver = {
  1859. .driver = {
  1860. .name = "docg3",
  1861. .owner = THIS_MODULE,
  1862. },
  1863. .suspend = docg3_suspend,
  1864. .resume = docg3_resume,
  1865. .remove = __exit_p(docg3_release),
  1866. };
  1867. static int __init docg3_init(void)
  1868. {
  1869. return platform_driver_probe(&g3_driver, docg3_probe);
  1870. }
  1871. module_init(docg3_init);
  1872. static void __exit docg3_exit(void)
  1873. {
  1874. platform_driver_unregister(&g3_driver);
  1875. }
  1876. module_exit(docg3_exit);
  1877. MODULE_LICENSE("GPL");
  1878. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  1879. MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");