docg3.c 59 KB

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