docg3.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  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. int max_bitflips = 0;
  757. if (buf)
  758. len = ops->len;
  759. else
  760. len = 0;
  761. if (oobbuf)
  762. ooblen = ops->ooblen;
  763. else
  764. ooblen = 0;
  765. if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
  766. oobbuf += ops->ooboffs;
  767. doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
  768. from, ops->mode, buf, len, oobbuf, ooblen);
  769. if (ooblen % DOC_LAYOUT_OOB_SIZE)
  770. return -EINVAL;
  771. if (from + len > mtd->size)
  772. return -EINVAL;
  773. ops->oobretlen = 0;
  774. ops->retlen = 0;
  775. ret = 0;
  776. skip = from % DOC_LAYOUT_PAGE_SIZE;
  777. mutex_lock(&docg3->cascade->lock);
  778. while (ret >= 0 && (len > 0 || ooblen > 0)) {
  779. calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
  780. docg3->reliable);
  781. nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
  782. nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
  783. ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
  784. if (ret < 0)
  785. goto out;
  786. ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
  787. if (ret < 0)
  788. goto err_in_read;
  789. ret = doc_read_page_getbytes(docg3, skip, NULL, 1);
  790. if (ret < skip)
  791. goto err_in_read;
  792. ret = doc_read_page_getbytes(docg3, nbdata, buf, 0);
  793. if (ret < nbdata)
  794. goto err_in_read;
  795. doc_read_page_getbytes(docg3,
  796. DOC_LAYOUT_PAGE_SIZE - nbdata - skip,
  797. NULL, 0);
  798. ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0);
  799. if (ret < nboob)
  800. goto err_in_read;
  801. doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
  802. NULL, 0);
  803. doc_get_bch_hw_ecc(docg3, hwecc);
  804. eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
  805. if (nboob >= DOC_LAYOUT_OOB_SIZE) {
  806. doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  807. oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
  808. oobbuf[4], oobbuf[5], oobbuf[6]);
  809. doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
  810. doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  811. oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
  812. oobbuf[12], oobbuf[13], oobbuf[14]);
  813. doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
  814. }
  815. doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
  816. doc_dbg("ECC HW_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  817. hwecc[0], hwecc[1], hwecc[2], hwecc[3], hwecc[4],
  818. hwecc[5], hwecc[6]);
  819. ret = -EIO;
  820. if (is_prot_seq_error(docg3))
  821. goto err_in_read;
  822. ret = 0;
  823. if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
  824. (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
  825. (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
  826. (ops->mode != MTD_OPS_RAW) &&
  827. (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
  828. ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
  829. if (ret < 0) {
  830. mtd->ecc_stats.failed++;
  831. ret = -EBADMSG;
  832. }
  833. if (ret > 0) {
  834. mtd->ecc_stats.corrected += ret;
  835. max_bitflips = max(max_bitflips, ret);
  836. ret = max_bitflips;
  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. out:
  850. mutex_unlock(&docg3->cascade->lock);
  851. return ret;
  852. err_in_read:
  853. doc_read_page_finish(docg3);
  854. goto out;
  855. }
  856. /**
  857. * doc_read - Read bytes from flash
  858. * @mtd: the device
  859. * @from: the offset from first block and first page, in bytes, aligned on page
  860. * size
  861. * @len: the number of bytes to read (must be a multiple of 4)
  862. * @retlen: the number of bytes actually read
  863. * @buf: the filled in buffer
  864. *
  865. * Reads flash memory pages. This function does not read the OOB chunk, but only
  866. * the page data.
  867. *
  868. * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
  869. */
  870. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  871. size_t *retlen, u_char *buf)
  872. {
  873. struct mtd_oob_ops ops;
  874. size_t ret;
  875. memset(&ops, 0, sizeof(ops));
  876. ops.datbuf = buf;
  877. ops.len = len;
  878. ops.mode = MTD_OPS_AUTO_OOB;
  879. ret = doc_read_oob(mtd, from, &ops);
  880. *retlen = ops.retlen;
  881. return ret;
  882. }
  883. static int doc_reload_bbt(struct docg3 *docg3)
  884. {
  885. int block = DOC_LAYOUT_BLOCK_BBT;
  886. int ret = 0, nbpages, page;
  887. u_char *buf = docg3->bbt;
  888. nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
  889. for (page = 0; !ret && (page < nbpages); page++) {
  890. ret = doc_read_page_prepare(docg3, block, block + 1,
  891. page + DOC_LAYOUT_PAGE_BBT, 0);
  892. if (!ret)
  893. ret = doc_read_page_ecc_init(docg3,
  894. DOC_LAYOUT_PAGE_SIZE);
  895. if (!ret)
  896. doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
  897. buf, 1);
  898. buf += DOC_LAYOUT_PAGE_SIZE;
  899. }
  900. doc_read_page_finish(docg3);
  901. return ret;
  902. }
  903. /**
  904. * doc_block_isbad - Checks whether a block is good or not
  905. * @mtd: the device
  906. * @from: the offset to find the correct block
  907. *
  908. * Returns 1 if block is bad, 0 if block is good
  909. */
  910. static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
  911. {
  912. struct docg3 *docg3 = mtd->priv;
  913. int block0, block1, page, ofs, is_good;
  914. calc_block_sector(from, &block0, &block1, &page, &ofs,
  915. docg3->reliable);
  916. doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
  917. from, block0, block1, page, ofs);
  918. if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
  919. return 0;
  920. if (block1 > docg3->max_block)
  921. return -EINVAL;
  922. is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
  923. return !is_good;
  924. }
  925. #if 0
  926. /**
  927. * doc_get_erase_count - Get block erase count
  928. * @docg3: the device
  929. * @from: the offset in which the block is.
  930. *
  931. * Get the number of times a block was erased. The number is the maximum of
  932. * erase times between first and second plane (which should be equal normally).
  933. *
  934. * Returns The number of erases, or -EINVAL or -EIO on error.
  935. */
  936. static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
  937. {
  938. u8 buf[DOC_LAYOUT_WEAR_SIZE];
  939. int ret, plane1_erase_count, plane2_erase_count;
  940. int block0, block1, page, ofs;
  941. doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
  942. if (from % DOC_LAYOUT_PAGE_SIZE)
  943. return -EINVAL;
  944. calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
  945. if (block1 > docg3->max_block)
  946. return -EINVAL;
  947. ret = doc_reset_seq(docg3);
  948. if (!ret)
  949. ret = doc_read_page_prepare(docg3, block0, block1, page,
  950. ofs + DOC_LAYOUT_WEAR_OFFSET);
  951. if (!ret)
  952. ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
  953. buf, 1);
  954. doc_read_page_finish(docg3);
  955. if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
  956. return -EIO;
  957. plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
  958. | ((u8)(~buf[5]) << 16);
  959. plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
  960. | ((u8)(~buf[7]) << 16);
  961. return max(plane1_erase_count, plane2_erase_count);
  962. }
  963. #endif
  964. /**
  965. * doc_get_op_status - get erase/write operation status
  966. * @docg3: the device
  967. *
  968. * Queries the status from the chip, and returns it
  969. *
  970. * Returns the status (bits DOC_PLANES_STATUS_*)
  971. */
  972. static int doc_get_op_status(struct docg3 *docg3)
  973. {
  974. u8 status;
  975. doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
  976. doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
  977. doc_delay(docg3, 5);
  978. doc_ecc_disable(docg3);
  979. doc_read_data_area(docg3, &status, 1, 1);
  980. return status;
  981. }
  982. /**
  983. * doc_write_erase_wait_status - wait for write or erase completion
  984. * @docg3: the device
  985. *
  986. * Wait for the chip to be ready again after erase or write operation, and check
  987. * erase/write status.
  988. *
  989. * Returns 0 if erase successfull, -EIO if erase/write issue, -ETIMEOUT if
  990. * timeout
  991. */
  992. static int doc_write_erase_wait_status(struct docg3 *docg3)
  993. {
  994. int i, status, ret = 0;
  995. for (i = 0; !doc_is_ready(docg3) && i < 5; i++)
  996. msleep(20);
  997. if (!doc_is_ready(docg3)) {
  998. doc_dbg("Timeout reached and the chip is still not ready\n");
  999. ret = -EAGAIN;
  1000. goto out;
  1001. }
  1002. status = doc_get_op_status(docg3);
  1003. if (status & DOC_PLANES_STATUS_FAIL) {
  1004. doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
  1005. status);
  1006. ret = -EIO;
  1007. }
  1008. out:
  1009. doc_page_finish(docg3);
  1010. return ret;
  1011. }
  1012. /**
  1013. * doc_erase_block - Erase a couple of blocks
  1014. * @docg3: the device
  1015. * @block0: the first block to erase (leftmost plane)
  1016. * @block1: the second block to erase (rightmost plane)
  1017. *
  1018. * Erase both blocks, and return operation status
  1019. *
  1020. * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
  1021. * ready for too long
  1022. */
  1023. static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
  1024. {
  1025. int ret, sector;
  1026. doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
  1027. ret = doc_reset_seq(docg3);
  1028. if (ret)
  1029. return -EIO;
  1030. doc_set_reliable_mode(docg3);
  1031. doc_flash_sequence(docg3, DOC_SEQ_ERASE);
  1032. sector = block0 << DOC_ADDR_BLOCK_SHIFT;
  1033. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  1034. doc_setup_addr_sector(docg3, sector);
  1035. sector = block1 << DOC_ADDR_BLOCK_SHIFT;
  1036. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  1037. doc_setup_addr_sector(docg3, sector);
  1038. doc_delay(docg3, 1);
  1039. doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
  1040. doc_delay(docg3, 2);
  1041. if (is_prot_seq_error(docg3)) {
  1042. doc_err("Erase blocks %d,%d error\n", block0, block1);
  1043. return -EIO;
  1044. }
  1045. return doc_write_erase_wait_status(docg3);
  1046. }
  1047. /**
  1048. * doc_erase - Erase a portion of the chip
  1049. * @mtd: the device
  1050. * @info: the erase info
  1051. *
  1052. * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
  1053. * split into 2 pages of 512 bytes on 2 contiguous blocks.
  1054. *
  1055. * Returns 0 if erase successful, -EINVAL if adressing error, -EIO if erase
  1056. * issue
  1057. */
  1058. static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
  1059. {
  1060. struct docg3 *docg3 = mtd->priv;
  1061. uint64_t len;
  1062. int block0, block1, page, ret, ofs = 0;
  1063. doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
  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. mutex_lock(&docg3->cascade->lock);
  1074. doc_set_device_id(docg3, docg3->device_id);
  1075. doc_set_reliable_mode(docg3);
  1076. for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
  1077. info->state = MTD_ERASING;
  1078. ret = doc_erase_block(docg3, block0, block1);
  1079. block0 += 2;
  1080. block1 += 2;
  1081. }
  1082. mutex_unlock(&docg3->cascade->lock);
  1083. if (ret)
  1084. goto reset_err;
  1085. info->state = MTD_ERASE_DONE;
  1086. return 0;
  1087. reset_err:
  1088. info->state = MTD_ERASE_FAILED;
  1089. return ret;
  1090. }
  1091. /**
  1092. * doc_write_page - Write a single page to the chip
  1093. * @docg3: the device
  1094. * @to: the offset from first block and first page, in bytes, aligned on page
  1095. * size
  1096. * @buf: buffer to get bytes from
  1097. * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
  1098. * written)
  1099. * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
  1100. * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
  1101. * remaining ones are filled with hardware Hamming and BCH
  1102. * computations. Its value is not meaningfull is oob == NULL.
  1103. *
  1104. * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
  1105. * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
  1106. * BCH generator if autoecc is not null.
  1107. *
  1108. * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
  1109. */
  1110. static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
  1111. const u_char *oob, int autoecc)
  1112. {
  1113. int block0, block1, page, ret, ofs = 0;
  1114. u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
  1115. doc_dbg("doc_write_page(to=%lld)\n", to);
  1116. calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
  1117. doc_set_device_id(docg3, docg3->device_id);
  1118. ret = doc_reset_seq(docg3);
  1119. if (ret)
  1120. goto err;
  1121. /* Program the flash address block and page */
  1122. ret = doc_write_seek(docg3, block0, block1, page, ofs);
  1123. if (ret)
  1124. goto err;
  1125. doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
  1126. doc_delay(docg3, 2);
  1127. doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
  1128. if (oob && autoecc) {
  1129. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
  1130. doc_delay(docg3, 2);
  1131. oob += DOC_LAYOUT_OOB_UNUSED_OFS;
  1132. hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
  1133. doc_delay(docg3, 2);
  1134. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
  1135. &hamming);
  1136. doc_delay(docg3, 2);
  1137. doc_get_bch_hw_ecc(docg3, hwecc);
  1138. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
  1139. doc_delay(docg3, 2);
  1140. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
  1141. }
  1142. if (oob && !autoecc)
  1143. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
  1144. doc_delay(docg3, 2);
  1145. doc_page_finish(docg3);
  1146. doc_delay(docg3, 2);
  1147. doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
  1148. doc_delay(docg3, 2);
  1149. /*
  1150. * The wait status will perform another doc_page_finish() call, but that
  1151. * seems to please the docg3, so leave it.
  1152. */
  1153. ret = doc_write_erase_wait_status(docg3);
  1154. return ret;
  1155. err:
  1156. doc_read_page_finish(docg3);
  1157. return ret;
  1158. }
  1159. /**
  1160. * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
  1161. * @ops: the oob operations
  1162. *
  1163. * Returns 0 or 1 if success, -EINVAL if invalid oob mode
  1164. */
  1165. static int doc_guess_autoecc(struct mtd_oob_ops *ops)
  1166. {
  1167. int autoecc;
  1168. switch (ops->mode) {
  1169. case MTD_OPS_PLACE_OOB:
  1170. case MTD_OPS_AUTO_OOB:
  1171. autoecc = 1;
  1172. break;
  1173. case MTD_OPS_RAW:
  1174. autoecc = 0;
  1175. break;
  1176. default:
  1177. autoecc = -EINVAL;
  1178. }
  1179. return autoecc;
  1180. }
  1181. /**
  1182. * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
  1183. * @dst: the target 16 bytes OOB buffer
  1184. * @oobsrc: the source 8 bytes non-ECC OOB buffer
  1185. *
  1186. */
  1187. static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
  1188. {
  1189. memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
  1190. dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
  1191. }
  1192. /**
  1193. * doc_backup_oob - Backup OOB into docg3 structure
  1194. * @docg3: the device
  1195. * @to: the page offset in the chip
  1196. * @ops: the OOB size and buffer
  1197. *
  1198. * As the docg3 should write a page with its OOB in one pass, and some userland
  1199. * applications do write_oob() to setup the OOB and then write(), store the OOB
  1200. * into a temporary storage. This is very dangerous, as 2 concurrent
  1201. * applications could store an OOB, and then write their pages (which will
  1202. * result into one having its OOB corrupted).
  1203. *
  1204. * The only reliable way would be for userland to call doc_write_oob() with both
  1205. * the page data _and_ the OOB area.
  1206. *
  1207. * Returns 0 if success, -EINVAL if ops content invalid
  1208. */
  1209. static int doc_backup_oob(struct docg3 *docg3, loff_t to,
  1210. struct mtd_oob_ops *ops)
  1211. {
  1212. int ooblen = ops->ooblen, autoecc;
  1213. if (ooblen != DOC_LAYOUT_OOB_SIZE)
  1214. return -EINVAL;
  1215. autoecc = doc_guess_autoecc(ops);
  1216. if (autoecc < 0)
  1217. return autoecc;
  1218. docg3->oob_write_ofs = to;
  1219. docg3->oob_autoecc = autoecc;
  1220. if (ops->mode == MTD_OPS_AUTO_OOB) {
  1221. doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
  1222. ops->oobretlen = 8;
  1223. } else {
  1224. memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
  1225. ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
  1226. }
  1227. return 0;
  1228. }
  1229. /**
  1230. * doc_write_oob - Write out of band bytes to flash
  1231. * @mtd: the device
  1232. * @ofs: the offset from first block and first page, in bytes, aligned on page
  1233. * size
  1234. * @ops: the mtd oob structure
  1235. *
  1236. * Either write OOB data into a temporary buffer, for the subsequent write
  1237. * page. The provided OOB should be 16 bytes long. If a data buffer is provided
  1238. * as well, issue the page write.
  1239. * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
  1240. * still be filled in if asked for).
  1241. *
  1242. * Returns 0 is successfull, EINVAL if length is not 14 bytes
  1243. */
  1244. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  1245. struct mtd_oob_ops *ops)
  1246. {
  1247. struct docg3 *docg3 = mtd->priv;
  1248. int ret, autoecc, oobdelta;
  1249. u8 *oobbuf = ops->oobbuf;
  1250. u8 *buf = ops->datbuf;
  1251. size_t len, ooblen;
  1252. u8 oob[DOC_LAYOUT_OOB_SIZE];
  1253. if (buf)
  1254. len = ops->len;
  1255. else
  1256. len = 0;
  1257. if (oobbuf)
  1258. ooblen = ops->ooblen;
  1259. else
  1260. ooblen = 0;
  1261. if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
  1262. oobbuf += ops->ooboffs;
  1263. doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
  1264. ofs, ops->mode, buf, len, oobbuf, ooblen);
  1265. switch (ops->mode) {
  1266. case MTD_OPS_PLACE_OOB:
  1267. case MTD_OPS_RAW:
  1268. oobdelta = mtd->oobsize;
  1269. break;
  1270. case MTD_OPS_AUTO_OOB:
  1271. oobdelta = mtd->ecclayout->oobavail;
  1272. break;
  1273. default:
  1274. oobdelta = 0;
  1275. }
  1276. if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
  1277. (ofs % DOC_LAYOUT_PAGE_SIZE))
  1278. return -EINVAL;
  1279. if (len && ooblen &&
  1280. (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
  1281. return -EINVAL;
  1282. if (ofs + len > mtd->size)
  1283. return -EINVAL;
  1284. ops->oobretlen = 0;
  1285. ops->retlen = 0;
  1286. ret = 0;
  1287. if (len == 0 && ooblen == 0)
  1288. return -EINVAL;
  1289. if (len == 0 && ooblen > 0)
  1290. return doc_backup_oob(docg3, ofs, ops);
  1291. autoecc = doc_guess_autoecc(ops);
  1292. if (autoecc < 0)
  1293. return autoecc;
  1294. mutex_lock(&docg3->cascade->lock);
  1295. while (!ret && len > 0) {
  1296. memset(oob, 0, sizeof(oob));
  1297. if (ofs == docg3->oob_write_ofs)
  1298. memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
  1299. else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
  1300. doc_fill_autooob(oob, oobbuf);
  1301. else if (ooblen > 0)
  1302. memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
  1303. ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
  1304. ofs += DOC_LAYOUT_PAGE_SIZE;
  1305. len -= DOC_LAYOUT_PAGE_SIZE;
  1306. buf += DOC_LAYOUT_PAGE_SIZE;
  1307. if (ooblen) {
  1308. oobbuf += oobdelta;
  1309. ooblen -= oobdelta;
  1310. ops->oobretlen += oobdelta;
  1311. }
  1312. ops->retlen += DOC_LAYOUT_PAGE_SIZE;
  1313. }
  1314. doc_set_device_id(docg3, 0);
  1315. mutex_unlock(&docg3->cascade->lock);
  1316. return ret;
  1317. }
  1318. /**
  1319. * doc_write - Write a buffer to the chip
  1320. * @mtd: the device
  1321. * @to: the offset from first block and first page, in bytes, aligned on page
  1322. * size
  1323. * @len: the number of bytes to write (must be a full page size, ie. 512)
  1324. * @retlen: the number of bytes actually written (0 or 512)
  1325. * @buf: the buffer to get bytes from
  1326. *
  1327. * Writes data to the chip.
  1328. *
  1329. * Returns 0 if write successful, -EIO if write error
  1330. */
  1331. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  1332. size_t *retlen, const u_char *buf)
  1333. {
  1334. struct docg3 *docg3 = mtd->priv;
  1335. int ret;
  1336. struct mtd_oob_ops ops;
  1337. doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
  1338. ops.datbuf = (char *)buf;
  1339. ops.len = len;
  1340. ops.mode = MTD_OPS_PLACE_OOB;
  1341. ops.oobbuf = NULL;
  1342. ops.ooblen = 0;
  1343. ops.ooboffs = 0;
  1344. ret = doc_write_oob(mtd, to, &ops);
  1345. *retlen = ops.retlen;
  1346. return ret;
  1347. }
  1348. static struct docg3 *sysfs_dev2docg3(struct device *dev,
  1349. struct device_attribute *attr)
  1350. {
  1351. int floor;
  1352. struct platform_device *pdev = to_platform_device(dev);
  1353. struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
  1354. floor = attr->attr.name[1] - '0';
  1355. if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
  1356. return NULL;
  1357. else
  1358. return docg3_floors[floor]->priv;
  1359. }
  1360. static ssize_t dps0_is_key_locked(struct device *dev,
  1361. struct device_attribute *attr, char *buf)
  1362. {
  1363. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1364. int dps0;
  1365. mutex_lock(&docg3->cascade->lock);
  1366. doc_set_device_id(docg3, docg3->device_id);
  1367. dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
  1368. doc_set_device_id(docg3, 0);
  1369. mutex_unlock(&docg3->cascade->lock);
  1370. return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
  1371. }
  1372. static ssize_t dps1_is_key_locked(struct device *dev,
  1373. struct device_attribute *attr, char *buf)
  1374. {
  1375. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1376. int dps1;
  1377. mutex_lock(&docg3->cascade->lock);
  1378. doc_set_device_id(docg3, docg3->device_id);
  1379. dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
  1380. doc_set_device_id(docg3, 0);
  1381. mutex_unlock(&docg3->cascade->lock);
  1382. return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
  1383. }
  1384. static ssize_t dps0_insert_key(struct device *dev,
  1385. struct device_attribute *attr,
  1386. const char *buf, size_t count)
  1387. {
  1388. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1389. int i;
  1390. if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
  1391. return -EINVAL;
  1392. mutex_lock(&docg3->cascade->lock);
  1393. doc_set_device_id(docg3, docg3->device_id);
  1394. for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
  1395. doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
  1396. doc_set_device_id(docg3, 0);
  1397. mutex_unlock(&docg3->cascade->lock);
  1398. return count;
  1399. }
  1400. static ssize_t dps1_insert_key(struct device *dev,
  1401. struct device_attribute *attr,
  1402. const char *buf, size_t count)
  1403. {
  1404. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1405. int i;
  1406. if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
  1407. return -EINVAL;
  1408. mutex_lock(&docg3->cascade->lock);
  1409. doc_set_device_id(docg3, docg3->device_id);
  1410. for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
  1411. doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
  1412. doc_set_device_id(docg3, 0);
  1413. mutex_unlock(&docg3->cascade->lock);
  1414. return count;
  1415. }
  1416. #define FLOOR_SYSFS(id) { \
  1417. __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
  1418. __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
  1419. __ATTR(f##id##_dps0_protection_key, S_IWUGO, NULL, dps0_insert_key), \
  1420. __ATTR(f##id##_dps1_protection_key, S_IWUGO, NULL, dps1_insert_key), \
  1421. }
  1422. static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
  1423. FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
  1424. };
  1425. static int doc_register_sysfs(struct platform_device *pdev,
  1426. struct docg3_cascade *cascade)
  1427. {
  1428. int ret = 0, floor, i = 0;
  1429. struct device *dev = &pdev->dev;
  1430. for (floor = 0; !ret && floor < DOC_MAX_NBFLOORS &&
  1431. cascade->floors[floor]; floor++)
  1432. for (i = 0; !ret && i < 4; i++)
  1433. ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
  1434. if (!ret)
  1435. return 0;
  1436. do {
  1437. while (--i >= 0)
  1438. device_remove_file(dev, &doc_sys_attrs[floor][i]);
  1439. i = 4;
  1440. } while (--floor >= 0);
  1441. return ret;
  1442. }
  1443. static void doc_unregister_sysfs(struct platform_device *pdev,
  1444. struct docg3_cascade *cascade)
  1445. {
  1446. struct device *dev = &pdev->dev;
  1447. int floor, i;
  1448. for (floor = 0; floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
  1449. floor++)
  1450. for (i = 0; i < 4; i++)
  1451. device_remove_file(dev, &doc_sys_attrs[floor][i]);
  1452. }
  1453. /*
  1454. * Debug sysfs entries
  1455. */
  1456. static int dbg_flashctrl_show(struct seq_file *s, void *p)
  1457. {
  1458. struct docg3 *docg3 = (struct docg3 *)s->private;
  1459. int pos = 0;
  1460. u8 fctrl;
  1461. mutex_lock(&docg3->cascade->lock);
  1462. fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  1463. mutex_unlock(&docg3->cascade->lock);
  1464. pos += seq_printf(s,
  1465. "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
  1466. fctrl,
  1467. fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
  1468. fctrl & DOC_CTRL_CE ? "active" : "inactive",
  1469. fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
  1470. fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
  1471. fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
  1472. return pos;
  1473. }
  1474. DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
  1475. static int dbg_asicmode_show(struct seq_file *s, void *p)
  1476. {
  1477. struct docg3 *docg3 = (struct docg3 *)s->private;
  1478. int pos = 0, pctrl, mode;
  1479. mutex_lock(&docg3->cascade->lock);
  1480. pctrl = doc_register_readb(docg3, DOC_ASICMODE);
  1481. mode = pctrl & 0x03;
  1482. mutex_unlock(&docg3->cascade->lock);
  1483. pos += seq_printf(s,
  1484. "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
  1485. pctrl,
  1486. pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
  1487. pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
  1488. pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
  1489. pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
  1490. pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
  1491. mode >> 1, mode & 0x1);
  1492. switch (mode) {
  1493. case DOC_ASICMODE_RESET:
  1494. pos += seq_printf(s, "reset");
  1495. break;
  1496. case DOC_ASICMODE_NORMAL:
  1497. pos += seq_printf(s, "normal");
  1498. break;
  1499. case DOC_ASICMODE_POWERDOWN:
  1500. pos += seq_printf(s, "powerdown");
  1501. break;
  1502. }
  1503. pos += seq_printf(s, ")\n");
  1504. return pos;
  1505. }
  1506. DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
  1507. static int dbg_device_id_show(struct seq_file *s, void *p)
  1508. {
  1509. struct docg3 *docg3 = (struct docg3 *)s->private;
  1510. int pos = 0;
  1511. int id;
  1512. mutex_lock(&docg3->cascade->lock);
  1513. id = doc_register_readb(docg3, DOC_DEVICESELECT);
  1514. mutex_unlock(&docg3->cascade->lock);
  1515. pos += seq_printf(s, "DeviceId = %d\n", id);
  1516. return pos;
  1517. }
  1518. DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
  1519. static int dbg_protection_show(struct seq_file *s, void *p)
  1520. {
  1521. struct docg3 *docg3 = (struct docg3 *)s->private;
  1522. int pos = 0;
  1523. int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
  1524. mutex_lock(&docg3->cascade->lock);
  1525. protect = doc_register_readb(docg3, DOC_PROTECTION);
  1526. dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
  1527. dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
  1528. dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
  1529. dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
  1530. dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
  1531. dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
  1532. mutex_unlock(&docg3->cascade->lock);
  1533. pos += seq_printf(s, "Protection = 0x%02x (",
  1534. protect);
  1535. if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
  1536. pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
  1537. if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
  1538. pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
  1539. if (protect & DOC_PROTECT_LOCK_INPUT)
  1540. pos += seq_printf(s, "LOCK_INPUT,");
  1541. if (protect & DOC_PROTECT_STICKY_LOCK)
  1542. pos += seq_printf(s, "STICKY_LOCK,");
  1543. if (protect & DOC_PROTECT_PROTECTION_ENABLED)
  1544. pos += seq_printf(s, "PROTECTION ON,");
  1545. if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
  1546. pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
  1547. if (protect & DOC_PROTECT_PROTECTION_ERROR)
  1548. pos += seq_printf(s, "PROTECT_ERR,");
  1549. else
  1550. pos += seq_printf(s, "NO_PROTECT_ERR");
  1551. pos += seq_printf(s, ")\n");
  1552. pos += seq_printf(s, "DPS0 = 0x%02x : "
  1553. "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
  1554. "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
  1555. dps0, dps0_low, dps0_high,
  1556. !!(dps0 & DOC_DPS_OTP_PROTECTED),
  1557. !!(dps0 & DOC_DPS_READ_PROTECTED),
  1558. !!(dps0 & DOC_DPS_WRITE_PROTECTED),
  1559. !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
  1560. !!(dps0 & DOC_DPS_KEY_OK));
  1561. pos += seq_printf(s, "DPS1 = 0x%02x : "
  1562. "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
  1563. "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
  1564. dps1, dps1_low, dps1_high,
  1565. !!(dps1 & DOC_DPS_OTP_PROTECTED),
  1566. !!(dps1 & DOC_DPS_READ_PROTECTED),
  1567. !!(dps1 & DOC_DPS_WRITE_PROTECTED),
  1568. !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
  1569. !!(dps1 & DOC_DPS_KEY_OK));
  1570. return pos;
  1571. }
  1572. DEBUGFS_RO_ATTR(protection, dbg_protection_show);
  1573. static int __init doc_dbg_register(struct docg3 *docg3)
  1574. {
  1575. struct dentry *root, *entry;
  1576. root = debugfs_create_dir("docg3", NULL);
  1577. if (!root)
  1578. return -ENOMEM;
  1579. entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
  1580. &flashcontrol_fops);
  1581. if (entry)
  1582. entry = debugfs_create_file("asic_mode", S_IRUSR, root,
  1583. docg3, &asic_mode_fops);
  1584. if (entry)
  1585. entry = debugfs_create_file("device_id", S_IRUSR, root,
  1586. docg3, &device_id_fops);
  1587. if (entry)
  1588. entry = debugfs_create_file("protection", S_IRUSR, root,
  1589. docg3, &protection_fops);
  1590. if (entry) {
  1591. docg3->debugfs_root = root;
  1592. return 0;
  1593. } else {
  1594. debugfs_remove_recursive(root);
  1595. return -ENOMEM;
  1596. }
  1597. }
  1598. static void __exit doc_dbg_unregister(struct docg3 *docg3)
  1599. {
  1600. debugfs_remove_recursive(docg3->debugfs_root);
  1601. }
  1602. /**
  1603. * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
  1604. * @chip_id: The chip ID of the supported chip
  1605. * @mtd: The structure to fill
  1606. */
  1607. static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
  1608. {
  1609. struct docg3 *docg3 = mtd->priv;
  1610. int cfg;
  1611. cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
  1612. docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
  1613. docg3->reliable = reliable_mode;
  1614. switch (chip_id) {
  1615. case DOC_CHIPID_G3:
  1616. mtd->name = kasprintf(GFP_KERNEL, "docg3.%d",
  1617. docg3->device_id);
  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. }
  1641. /**
  1642. * doc_probe_device - Check if a device is available
  1643. * @base: the io space where the device is probed
  1644. * @floor: the floor of the probed device
  1645. * @dev: the device
  1646. * @cascade: the cascade of chips this devices will belong to
  1647. *
  1648. * Checks whether a device at the specified IO range, and floor is available.
  1649. *
  1650. * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
  1651. * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
  1652. * launched.
  1653. */
  1654. static struct mtd_info * __init
  1655. doc_probe_device(struct docg3_cascade *cascade, int floor, struct device *dev)
  1656. {
  1657. int ret, bbt_nbpages;
  1658. u16 chip_id, chip_id_inv;
  1659. struct docg3 *docg3;
  1660. struct mtd_info *mtd;
  1661. ret = -ENOMEM;
  1662. docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
  1663. if (!docg3)
  1664. goto nomem1;
  1665. mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
  1666. if (!mtd)
  1667. goto nomem2;
  1668. mtd->priv = docg3;
  1669. bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
  1670. 8 * DOC_LAYOUT_PAGE_SIZE);
  1671. docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
  1672. if (!docg3->bbt)
  1673. goto nomem3;
  1674. docg3->dev = dev;
  1675. docg3->device_id = floor;
  1676. docg3->cascade = cascade;
  1677. doc_set_device_id(docg3, docg3->device_id);
  1678. if (!floor)
  1679. doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
  1680. doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
  1681. chip_id = doc_register_readw(docg3, DOC_CHIPID);
  1682. chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
  1683. ret = 0;
  1684. if (chip_id != (u16)(~chip_id_inv)) {
  1685. goto nomem3;
  1686. }
  1687. switch (chip_id) {
  1688. case DOC_CHIPID_G3:
  1689. doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
  1690. docg3->cascade->base, floor);
  1691. break;
  1692. default:
  1693. doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
  1694. goto nomem3;
  1695. }
  1696. doc_set_driver_info(chip_id, mtd);
  1697. doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
  1698. doc_reload_bbt(docg3);
  1699. return mtd;
  1700. nomem3:
  1701. kfree(mtd);
  1702. nomem2:
  1703. kfree(docg3);
  1704. nomem1:
  1705. return ERR_PTR(ret);
  1706. }
  1707. /**
  1708. * doc_release_device - Release a docg3 floor
  1709. * @mtd: the device
  1710. */
  1711. static void doc_release_device(struct mtd_info *mtd)
  1712. {
  1713. struct docg3 *docg3 = mtd->priv;
  1714. mtd_device_unregister(mtd);
  1715. kfree(docg3->bbt);
  1716. kfree(docg3);
  1717. kfree(mtd->name);
  1718. kfree(mtd);
  1719. }
  1720. /**
  1721. * docg3_resume - Awakens docg3 floor
  1722. * @pdev: platfrom device
  1723. *
  1724. * Returns 0 (always successfull)
  1725. */
  1726. static int docg3_resume(struct platform_device *pdev)
  1727. {
  1728. int i;
  1729. struct docg3_cascade *cascade;
  1730. struct mtd_info **docg3_floors, *mtd;
  1731. struct docg3 *docg3;
  1732. cascade = platform_get_drvdata(pdev);
  1733. docg3_floors = cascade->floors;
  1734. mtd = docg3_floors[0];
  1735. docg3 = mtd->priv;
  1736. doc_dbg("docg3_resume()\n");
  1737. for (i = 0; i < 12; i++)
  1738. doc_readb(docg3, DOC_IOSPACE_IPL);
  1739. return 0;
  1740. }
  1741. /**
  1742. * docg3_suspend - Put in low power mode the docg3 floor
  1743. * @pdev: platform device
  1744. * @state: power state
  1745. *
  1746. * Shuts off most of docg3 circuitery to lower power consumption.
  1747. *
  1748. * Returns 0 if suspend succeeded, -EIO if chip refused suspend
  1749. */
  1750. static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
  1751. {
  1752. int floor, i;
  1753. struct docg3_cascade *cascade;
  1754. struct mtd_info **docg3_floors, *mtd;
  1755. struct docg3 *docg3;
  1756. u8 ctrl, pwr_down;
  1757. cascade = platform_get_drvdata(pdev);
  1758. docg3_floors = cascade->floors;
  1759. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
  1760. mtd = docg3_floors[floor];
  1761. if (!mtd)
  1762. continue;
  1763. docg3 = mtd->priv;
  1764. doc_writeb(docg3, floor, DOC_DEVICESELECT);
  1765. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  1766. ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
  1767. doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
  1768. for (i = 0; i < 10; i++) {
  1769. usleep_range(3000, 4000);
  1770. pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
  1771. if (pwr_down & DOC_POWERDOWN_READY)
  1772. break;
  1773. }
  1774. if (pwr_down & DOC_POWERDOWN_READY) {
  1775. doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
  1776. floor);
  1777. } else {
  1778. doc_err("docg3_suspend(): floor %d powerdown failed\n",
  1779. floor);
  1780. return -EIO;
  1781. }
  1782. }
  1783. mtd = docg3_floors[0];
  1784. docg3 = mtd->priv;
  1785. doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
  1786. return 0;
  1787. }
  1788. /**
  1789. * doc_probe - Probe the IO space for a DiskOnChip G3 chip
  1790. * @pdev: platform device
  1791. *
  1792. * Probes for a G3 chip at the specified IO space in the platform data
  1793. * ressources. The floor 0 must be available.
  1794. *
  1795. * Returns 0 on success, -ENOMEM, -ENXIO on error
  1796. */
  1797. static int __init docg3_probe(struct platform_device *pdev)
  1798. {
  1799. struct device *dev = &pdev->dev;
  1800. struct mtd_info *mtd;
  1801. struct resource *ress;
  1802. void __iomem *base;
  1803. int ret, floor, found = 0;
  1804. struct docg3_cascade *cascade;
  1805. ret = -ENXIO;
  1806. ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1807. if (!ress) {
  1808. dev_err(dev, "No I/O memory resource defined\n");
  1809. goto noress;
  1810. }
  1811. base = ioremap(ress->start, DOC_IOSPACE_SIZE);
  1812. ret = -ENOMEM;
  1813. cascade = kzalloc(sizeof(*cascade) * DOC_MAX_NBFLOORS,
  1814. GFP_KERNEL);
  1815. if (!cascade)
  1816. goto nomem1;
  1817. cascade->base = base;
  1818. mutex_init(&cascade->lock);
  1819. cascade->bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
  1820. DOC_ECC_BCH_PRIMPOLY);
  1821. if (!cascade->bch)
  1822. goto nomem2;
  1823. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
  1824. mtd = doc_probe_device(cascade, floor, dev);
  1825. if (IS_ERR(mtd)) {
  1826. ret = PTR_ERR(mtd);
  1827. goto err_probe;
  1828. }
  1829. if (!mtd) {
  1830. if (floor == 0)
  1831. goto notfound;
  1832. else
  1833. continue;
  1834. }
  1835. cascade->floors[floor] = mtd;
  1836. ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
  1837. 0);
  1838. if (ret)
  1839. goto err_probe;
  1840. found++;
  1841. }
  1842. ret = doc_register_sysfs(pdev, cascade);
  1843. if (ret)
  1844. goto err_probe;
  1845. if (!found)
  1846. goto notfound;
  1847. platform_set_drvdata(pdev, cascade);
  1848. doc_dbg_register(cascade->floors[0]->priv);
  1849. return 0;
  1850. notfound:
  1851. ret = -ENODEV;
  1852. dev_info(dev, "No supported DiskOnChip found\n");
  1853. err_probe:
  1854. kfree(cascade->bch);
  1855. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
  1856. if (cascade->floors[floor])
  1857. doc_release_device(cascade->floors[floor]);
  1858. nomem2:
  1859. kfree(cascade);
  1860. nomem1:
  1861. iounmap(base);
  1862. noress:
  1863. return ret;
  1864. }
  1865. /**
  1866. * docg3_release - Release the driver
  1867. * @pdev: the platform device
  1868. *
  1869. * Returns 0
  1870. */
  1871. static int __exit docg3_release(struct platform_device *pdev)
  1872. {
  1873. struct docg3_cascade *cascade = platform_get_drvdata(pdev);
  1874. struct docg3 *docg3 = cascade->floors[0]->priv;
  1875. void __iomem *base = cascade->base;
  1876. int floor;
  1877. doc_unregister_sysfs(pdev, cascade);
  1878. doc_dbg_unregister(docg3);
  1879. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
  1880. if (cascade->floors[floor])
  1881. doc_release_device(cascade->floors[floor]);
  1882. free_bch(docg3->cascade->bch);
  1883. kfree(cascade);
  1884. iounmap(base);
  1885. return 0;
  1886. }
  1887. static struct platform_driver g3_driver = {
  1888. .driver = {
  1889. .name = "docg3",
  1890. .owner = THIS_MODULE,
  1891. },
  1892. .suspend = docg3_suspend,
  1893. .resume = docg3_resume,
  1894. .remove = __exit_p(docg3_release),
  1895. };
  1896. static int __init docg3_init(void)
  1897. {
  1898. return platform_driver_probe(&g3_driver, docg3_probe);
  1899. }
  1900. module_init(docg3_init);
  1901. static void __exit docg3_exit(void)
  1902. {
  1903. platform_driver_unregister(&g3_driver);
  1904. }
  1905. module_exit(docg3_exit);
  1906. MODULE_LICENSE("GPL");
  1907. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  1908. MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");