docg3.c 58 KB

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