docg3.c 59 KB

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