docg3.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  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: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  819. oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
  820. oobbuf[4], oobbuf[5], oobbuf[6]);
  821. doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
  822. doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  823. oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
  824. oobbuf[12], oobbuf[13], oobbuf[14]);
  825. doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
  826. }
  827. doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
  828. doc_dbg("ECC HW_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  829. hwecc[0], hwecc[1], hwecc[2], hwecc[3], hwecc[4],
  830. hwecc[5], hwecc[6]);
  831. ret = -EIO;
  832. if (is_prot_seq_error(docg3))
  833. goto err_in_read;
  834. ret = 0;
  835. if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
  836. (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
  837. (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
  838. (ops->mode != MTD_OPS_RAW) &&
  839. (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
  840. ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
  841. if (ret < 0) {
  842. mtd->ecc_stats.failed++;
  843. ret = -EBADMSG;
  844. }
  845. if (ret > 0) {
  846. mtd->ecc_stats.corrected += ret;
  847. max_bitflips = max(max_bitflips, ret);
  848. ret = max_bitflips;
  849. }
  850. }
  851. doc_read_page_finish(docg3);
  852. ops->retlen += nbdata;
  853. ops->oobretlen += nboob;
  854. buf += nbdata;
  855. oobbuf += nboob;
  856. len -= nbdata;
  857. ooblen -= nboob;
  858. from += DOC_LAYOUT_PAGE_SIZE;
  859. skip = 0;
  860. }
  861. out:
  862. mutex_unlock(&docg3->cascade->lock);
  863. return ret;
  864. err_in_read:
  865. doc_read_page_finish(docg3);
  866. goto out;
  867. }
  868. /**
  869. * doc_read - Read bytes from flash
  870. * @mtd: the device
  871. * @from: the offset from first block and first page, in bytes, aligned on page
  872. * size
  873. * @len: the number of bytes to read (must be a multiple of 4)
  874. * @retlen: the number of bytes actually read
  875. * @buf: the filled in buffer
  876. *
  877. * Reads flash memory pages. This function does not read the OOB chunk, but only
  878. * the page data.
  879. *
  880. * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
  881. */
  882. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  883. size_t *retlen, u_char *buf)
  884. {
  885. struct mtd_oob_ops ops;
  886. size_t ret;
  887. memset(&ops, 0, sizeof(ops));
  888. ops.datbuf = buf;
  889. ops.len = len;
  890. ops.mode = MTD_OPS_AUTO_OOB;
  891. ret = doc_read_oob(mtd, from, &ops);
  892. *retlen = ops.retlen;
  893. return ret;
  894. }
  895. static int doc_reload_bbt(struct docg3 *docg3)
  896. {
  897. int block = DOC_LAYOUT_BLOCK_BBT;
  898. int ret = 0, nbpages, page;
  899. u_char *buf = docg3->bbt;
  900. nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
  901. for (page = 0; !ret && (page < nbpages); page++) {
  902. ret = doc_read_page_prepare(docg3, block, block + 1,
  903. page + DOC_LAYOUT_PAGE_BBT, 0);
  904. if (!ret)
  905. ret = doc_read_page_ecc_init(docg3,
  906. DOC_LAYOUT_PAGE_SIZE);
  907. if (!ret)
  908. doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
  909. buf, 1, 0);
  910. buf += DOC_LAYOUT_PAGE_SIZE;
  911. }
  912. doc_read_page_finish(docg3);
  913. return ret;
  914. }
  915. /**
  916. * doc_block_isbad - Checks whether a block is good or not
  917. * @mtd: the device
  918. * @from: the offset to find the correct block
  919. *
  920. * Returns 1 if block is bad, 0 if block is good
  921. */
  922. static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
  923. {
  924. struct docg3 *docg3 = mtd->priv;
  925. int block0, block1, page, ofs, is_good;
  926. calc_block_sector(from, &block0, &block1, &page, &ofs,
  927. docg3->reliable);
  928. doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
  929. from, block0, block1, page, ofs);
  930. if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
  931. return 0;
  932. if (block1 > docg3->max_block)
  933. return -EINVAL;
  934. is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
  935. return !is_good;
  936. }
  937. #if 0
  938. /**
  939. * doc_get_erase_count - Get block erase count
  940. * @docg3: the device
  941. * @from: the offset in which the block is.
  942. *
  943. * Get the number of times a block was erased. The number is the maximum of
  944. * erase times between first and second plane (which should be equal normally).
  945. *
  946. * Returns The number of erases, or -EINVAL or -EIO on error.
  947. */
  948. static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
  949. {
  950. u8 buf[DOC_LAYOUT_WEAR_SIZE];
  951. int ret, plane1_erase_count, plane2_erase_count;
  952. int block0, block1, page, ofs;
  953. doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
  954. if (from % DOC_LAYOUT_PAGE_SIZE)
  955. return -EINVAL;
  956. calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
  957. if (block1 > docg3->max_block)
  958. return -EINVAL;
  959. ret = doc_reset_seq(docg3);
  960. if (!ret)
  961. ret = doc_read_page_prepare(docg3, block0, block1, page,
  962. ofs + DOC_LAYOUT_WEAR_OFFSET, 0);
  963. if (!ret)
  964. ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
  965. buf, 1, 0);
  966. doc_read_page_finish(docg3);
  967. if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
  968. return -EIO;
  969. plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
  970. | ((u8)(~buf[5]) << 16);
  971. plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
  972. | ((u8)(~buf[7]) << 16);
  973. return max(plane1_erase_count, plane2_erase_count);
  974. }
  975. #endif
  976. /**
  977. * doc_get_op_status - get erase/write operation status
  978. * @docg3: the device
  979. *
  980. * Queries the status from the chip, and returns it
  981. *
  982. * Returns the status (bits DOC_PLANES_STATUS_*)
  983. */
  984. static int doc_get_op_status(struct docg3 *docg3)
  985. {
  986. u8 status;
  987. doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
  988. doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
  989. doc_delay(docg3, 5);
  990. doc_ecc_disable(docg3);
  991. doc_read_data_area(docg3, &status, 1, 1);
  992. return status;
  993. }
  994. /**
  995. * doc_write_erase_wait_status - wait for write or erase completion
  996. * @docg3: the device
  997. *
  998. * Wait for the chip to be ready again after erase or write operation, and check
  999. * erase/write status.
  1000. *
  1001. * Returns 0 if erase successful, -EIO if erase/write issue, -ETIMEOUT if
  1002. * timeout
  1003. */
  1004. static int doc_write_erase_wait_status(struct docg3 *docg3)
  1005. {
  1006. int i, status, ret = 0;
  1007. for (i = 0; !doc_is_ready(docg3) && i < 5; i++)
  1008. msleep(20);
  1009. if (!doc_is_ready(docg3)) {
  1010. doc_dbg("Timeout reached and the chip is still not ready\n");
  1011. ret = -EAGAIN;
  1012. goto out;
  1013. }
  1014. status = doc_get_op_status(docg3);
  1015. if (status & DOC_PLANES_STATUS_FAIL) {
  1016. doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
  1017. status);
  1018. ret = -EIO;
  1019. }
  1020. out:
  1021. doc_page_finish(docg3);
  1022. return ret;
  1023. }
  1024. /**
  1025. * doc_erase_block - Erase a couple of blocks
  1026. * @docg3: the device
  1027. * @block0: the first block to erase (leftmost plane)
  1028. * @block1: the second block to erase (rightmost plane)
  1029. *
  1030. * Erase both blocks, and return operation status
  1031. *
  1032. * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
  1033. * ready for too long
  1034. */
  1035. static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
  1036. {
  1037. int ret, sector;
  1038. doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
  1039. ret = doc_reset_seq(docg3);
  1040. if (ret)
  1041. return -EIO;
  1042. doc_set_reliable_mode(docg3);
  1043. doc_flash_sequence(docg3, DOC_SEQ_ERASE);
  1044. sector = block0 << DOC_ADDR_BLOCK_SHIFT;
  1045. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  1046. doc_setup_addr_sector(docg3, sector);
  1047. sector = block1 << DOC_ADDR_BLOCK_SHIFT;
  1048. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  1049. doc_setup_addr_sector(docg3, sector);
  1050. doc_delay(docg3, 1);
  1051. doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
  1052. doc_delay(docg3, 2);
  1053. if (is_prot_seq_error(docg3)) {
  1054. doc_err("Erase blocks %d,%d error\n", block0, block1);
  1055. return -EIO;
  1056. }
  1057. return doc_write_erase_wait_status(docg3);
  1058. }
  1059. /**
  1060. * doc_erase - Erase a portion of the chip
  1061. * @mtd: the device
  1062. * @info: the erase info
  1063. *
  1064. * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
  1065. * split into 2 pages of 512 bytes on 2 contiguous blocks.
  1066. *
  1067. * Returns 0 if erase successful, -EINVAL if addressing error, -EIO if erase
  1068. * issue
  1069. */
  1070. static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
  1071. {
  1072. struct docg3 *docg3 = mtd->priv;
  1073. uint64_t len;
  1074. int block0, block1, page, ret, ofs = 0;
  1075. doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
  1076. info->state = MTD_ERASE_PENDING;
  1077. calc_block_sector(info->addr + info->len, &block0, &block1, &page,
  1078. &ofs, docg3->reliable);
  1079. ret = -EINVAL;
  1080. if (info->addr + info->len > mtd->size || page || ofs)
  1081. goto reset_err;
  1082. ret = 0;
  1083. calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
  1084. docg3->reliable);
  1085. mutex_lock(&docg3->cascade->lock);
  1086. doc_set_device_id(docg3, docg3->device_id);
  1087. doc_set_reliable_mode(docg3);
  1088. for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
  1089. info->state = MTD_ERASING;
  1090. ret = doc_erase_block(docg3, block0, block1);
  1091. block0 += 2;
  1092. block1 += 2;
  1093. }
  1094. mutex_unlock(&docg3->cascade->lock);
  1095. if (ret)
  1096. goto reset_err;
  1097. info->state = MTD_ERASE_DONE;
  1098. return 0;
  1099. reset_err:
  1100. info->state = MTD_ERASE_FAILED;
  1101. return ret;
  1102. }
  1103. /**
  1104. * doc_write_page - Write a single page to the chip
  1105. * @docg3: the device
  1106. * @to: the offset from first block and first page, in bytes, aligned on page
  1107. * size
  1108. * @buf: buffer to get bytes from
  1109. * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
  1110. * written)
  1111. * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
  1112. * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
  1113. * remaining ones are filled with hardware Hamming and BCH
  1114. * computations. Its value is not meaningfull is oob == NULL.
  1115. *
  1116. * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
  1117. * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
  1118. * BCH generator if autoecc is not null.
  1119. *
  1120. * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
  1121. */
  1122. static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
  1123. const u_char *oob, int autoecc)
  1124. {
  1125. int block0, block1, page, ret, ofs = 0;
  1126. u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
  1127. doc_dbg("doc_write_page(to=%lld)\n", to);
  1128. calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
  1129. doc_set_device_id(docg3, docg3->device_id);
  1130. ret = doc_reset_seq(docg3);
  1131. if (ret)
  1132. goto err;
  1133. /* Program the flash address block and page */
  1134. ret = doc_write_seek(docg3, block0, block1, page, ofs);
  1135. if (ret)
  1136. goto err;
  1137. doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
  1138. doc_delay(docg3, 2);
  1139. doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
  1140. if (oob && autoecc) {
  1141. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
  1142. doc_delay(docg3, 2);
  1143. oob += DOC_LAYOUT_OOB_UNUSED_OFS;
  1144. hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
  1145. doc_delay(docg3, 2);
  1146. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
  1147. &hamming);
  1148. doc_delay(docg3, 2);
  1149. doc_get_bch_hw_ecc(docg3, hwecc);
  1150. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
  1151. doc_delay(docg3, 2);
  1152. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
  1153. }
  1154. if (oob && !autoecc)
  1155. doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
  1156. doc_delay(docg3, 2);
  1157. doc_page_finish(docg3);
  1158. doc_delay(docg3, 2);
  1159. doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
  1160. doc_delay(docg3, 2);
  1161. /*
  1162. * The wait status will perform another doc_page_finish() call, but that
  1163. * seems to please the docg3, so leave it.
  1164. */
  1165. ret = doc_write_erase_wait_status(docg3);
  1166. return ret;
  1167. err:
  1168. doc_read_page_finish(docg3);
  1169. return ret;
  1170. }
  1171. /**
  1172. * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
  1173. * @ops: the oob operations
  1174. *
  1175. * Returns 0 or 1 if success, -EINVAL if invalid oob mode
  1176. */
  1177. static int doc_guess_autoecc(struct mtd_oob_ops *ops)
  1178. {
  1179. int autoecc;
  1180. switch (ops->mode) {
  1181. case MTD_OPS_PLACE_OOB:
  1182. case MTD_OPS_AUTO_OOB:
  1183. autoecc = 1;
  1184. break;
  1185. case MTD_OPS_RAW:
  1186. autoecc = 0;
  1187. break;
  1188. default:
  1189. autoecc = -EINVAL;
  1190. }
  1191. return autoecc;
  1192. }
  1193. /**
  1194. * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
  1195. * @dst: the target 16 bytes OOB buffer
  1196. * @oobsrc: the source 8 bytes non-ECC OOB buffer
  1197. *
  1198. */
  1199. static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
  1200. {
  1201. memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
  1202. dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
  1203. }
  1204. /**
  1205. * doc_backup_oob - Backup OOB into docg3 structure
  1206. * @docg3: the device
  1207. * @to: the page offset in the chip
  1208. * @ops: the OOB size and buffer
  1209. *
  1210. * As the docg3 should write a page with its OOB in one pass, and some userland
  1211. * applications do write_oob() to setup the OOB and then write(), store the OOB
  1212. * into a temporary storage. This is very dangerous, as 2 concurrent
  1213. * applications could store an OOB, and then write their pages (which will
  1214. * result into one having its OOB corrupted).
  1215. *
  1216. * The only reliable way would be for userland to call doc_write_oob() with both
  1217. * the page data _and_ the OOB area.
  1218. *
  1219. * Returns 0 if success, -EINVAL if ops content invalid
  1220. */
  1221. static int doc_backup_oob(struct docg3 *docg3, loff_t to,
  1222. struct mtd_oob_ops *ops)
  1223. {
  1224. int ooblen = ops->ooblen, autoecc;
  1225. if (ooblen != DOC_LAYOUT_OOB_SIZE)
  1226. return -EINVAL;
  1227. autoecc = doc_guess_autoecc(ops);
  1228. if (autoecc < 0)
  1229. return autoecc;
  1230. docg3->oob_write_ofs = to;
  1231. docg3->oob_autoecc = autoecc;
  1232. if (ops->mode == MTD_OPS_AUTO_OOB) {
  1233. doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
  1234. ops->oobretlen = 8;
  1235. } else {
  1236. memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
  1237. ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
  1238. }
  1239. return 0;
  1240. }
  1241. /**
  1242. * doc_write_oob - Write out of band bytes to flash
  1243. * @mtd: the device
  1244. * @ofs: the offset from first block and first page, in bytes, aligned on page
  1245. * size
  1246. * @ops: the mtd oob structure
  1247. *
  1248. * Either write OOB data into a temporary buffer, for the subsequent write
  1249. * page. The provided OOB should be 16 bytes long. If a data buffer is provided
  1250. * as well, issue the page write.
  1251. * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
  1252. * still be filled in if asked for).
  1253. *
  1254. * Returns 0 is successful, EINVAL if length is not 14 bytes
  1255. */
  1256. static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
  1257. struct mtd_oob_ops *ops)
  1258. {
  1259. struct docg3 *docg3 = mtd->priv;
  1260. int ret, autoecc, oobdelta;
  1261. u8 *oobbuf = ops->oobbuf;
  1262. u8 *buf = ops->datbuf;
  1263. size_t len, ooblen;
  1264. u8 oob[DOC_LAYOUT_OOB_SIZE];
  1265. if (buf)
  1266. len = ops->len;
  1267. else
  1268. len = 0;
  1269. if (oobbuf)
  1270. ooblen = ops->ooblen;
  1271. else
  1272. ooblen = 0;
  1273. if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
  1274. oobbuf += ops->ooboffs;
  1275. doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
  1276. ofs, ops->mode, buf, len, oobbuf, ooblen);
  1277. switch (ops->mode) {
  1278. case MTD_OPS_PLACE_OOB:
  1279. case MTD_OPS_RAW:
  1280. oobdelta = mtd->oobsize;
  1281. break;
  1282. case MTD_OPS_AUTO_OOB:
  1283. oobdelta = mtd->ecclayout->oobavail;
  1284. break;
  1285. default:
  1286. oobdelta = 0;
  1287. }
  1288. if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
  1289. (ofs % DOC_LAYOUT_PAGE_SIZE))
  1290. return -EINVAL;
  1291. if (len && ooblen &&
  1292. (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
  1293. return -EINVAL;
  1294. if (ofs + len > mtd->size)
  1295. return -EINVAL;
  1296. ops->oobretlen = 0;
  1297. ops->retlen = 0;
  1298. ret = 0;
  1299. if (len == 0 && ooblen == 0)
  1300. return -EINVAL;
  1301. if (len == 0 && ooblen > 0)
  1302. return doc_backup_oob(docg3, ofs, ops);
  1303. autoecc = doc_guess_autoecc(ops);
  1304. if (autoecc < 0)
  1305. return autoecc;
  1306. mutex_lock(&docg3->cascade->lock);
  1307. while (!ret && len > 0) {
  1308. memset(oob, 0, sizeof(oob));
  1309. if (ofs == docg3->oob_write_ofs)
  1310. memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
  1311. else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
  1312. doc_fill_autooob(oob, oobbuf);
  1313. else if (ooblen > 0)
  1314. memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
  1315. ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
  1316. ofs += DOC_LAYOUT_PAGE_SIZE;
  1317. len -= DOC_LAYOUT_PAGE_SIZE;
  1318. buf += DOC_LAYOUT_PAGE_SIZE;
  1319. if (ooblen) {
  1320. oobbuf += oobdelta;
  1321. ooblen -= oobdelta;
  1322. ops->oobretlen += oobdelta;
  1323. }
  1324. ops->retlen += DOC_LAYOUT_PAGE_SIZE;
  1325. }
  1326. doc_set_device_id(docg3, 0);
  1327. mutex_unlock(&docg3->cascade->lock);
  1328. return ret;
  1329. }
  1330. /**
  1331. * doc_write - Write a buffer to the chip
  1332. * @mtd: the device
  1333. * @to: the offset from first block and first page, in bytes, aligned on page
  1334. * size
  1335. * @len: the number of bytes to write (must be a full page size, ie. 512)
  1336. * @retlen: the number of bytes actually written (0 or 512)
  1337. * @buf: the buffer to get bytes from
  1338. *
  1339. * Writes data to the chip.
  1340. *
  1341. * Returns 0 if write successful, -EIO if write error
  1342. */
  1343. static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
  1344. size_t *retlen, const u_char *buf)
  1345. {
  1346. struct docg3 *docg3 = mtd->priv;
  1347. int ret;
  1348. struct mtd_oob_ops ops;
  1349. doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
  1350. ops.datbuf = (char *)buf;
  1351. ops.len = len;
  1352. ops.mode = MTD_OPS_PLACE_OOB;
  1353. ops.oobbuf = NULL;
  1354. ops.ooblen = 0;
  1355. ops.ooboffs = 0;
  1356. ret = doc_write_oob(mtd, to, &ops);
  1357. *retlen = ops.retlen;
  1358. return ret;
  1359. }
  1360. static struct docg3 *sysfs_dev2docg3(struct device *dev,
  1361. struct device_attribute *attr)
  1362. {
  1363. int floor;
  1364. struct platform_device *pdev = to_platform_device(dev);
  1365. struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
  1366. floor = attr->attr.name[1] - '0';
  1367. if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
  1368. return NULL;
  1369. else
  1370. return docg3_floors[floor]->priv;
  1371. }
  1372. static ssize_t dps0_is_key_locked(struct device *dev,
  1373. struct device_attribute *attr, char *buf)
  1374. {
  1375. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1376. int dps0;
  1377. mutex_lock(&docg3->cascade->lock);
  1378. doc_set_device_id(docg3, docg3->device_id);
  1379. dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
  1380. doc_set_device_id(docg3, 0);
  1381. mutex_unlock(&docg3->cascade->lock);
  1382. return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
  1383. }
  1384. static ssize_t dps1_is_key_locked(struct device *dev,
  1385. struct device_attribute *attr, char *buf)
  1386. {
  1387. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1388. int dps1;
  1389. mutex_lock(&docg3->cascade->lock);
  1390. doc_set_device_id(docg3, docg3->device_id);
  1391. dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
  1392. doc_set_device_id(docg3, 0);
  1393. mutex_unlock(&docg3->cascade->lock);
  1394. return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
  1395. }
  1396. static ssize_t dps0_insert_key(struct device *dev,
  1397. struct device_attribute *attr,
  1398. const char *buf, size_t count)
  1399. {
  1400. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1401. int i;
  1402. if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
  1403. return -EINVAL;
  1404. mutex_lock(&docg3->cascade->lock);
  1405. doc_set_device_id(docg3, docg3->device_id);
  1406. for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
  1407. doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
  1408. doc_set_device_id(docg3, 0);
  1409. mutex_unlock(&docg3->cascade->lock);
  1410. return count;
  1411. }
  1412. static ssize_t dps1_insert_key(struct device *dev,
  1413. struct device_attribute *attr,
  1414. const char *buf, size_t count)
  1415. {
  1416. struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
  1417. int i;
  1418. if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
  1419. return -EINVAL;
  1420. mutex_lock(&docg3->cascade->lock);
  1421. doc_set_device_id(docg3, docg3->device_id);
  1422. for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
  1423. doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
  1424. doc_set_device_id(docg3, 0);
  1425. mutex_unlock(&docg3->cascade->lock);
  1426. return count;
  1427. }
  1428. #define FLOOR_SYSFS(id) { \
  1429. __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
  1430. __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
  1431. __ATTR(f##id##_dps0_protection_key, S_IWUGO, NULL, dps0_insert_key), \
  1432. __ATTR(f##id##_dps1_protection_key, S_IWUGO, NULL, dps1_insert_key), \
  1433. }
  1434. static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
  1435. FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
  1436. };
  1437. static int doc_register_sysfs(struct platform_device *pdev,
  1438. struct docg3_cascade *cascade)
  1439. {
  1440. int ret = 0, floor, i = 0;
  1441. struct device *dev = &pdev->dev;
  1442. for (floor = 0; !ret && floor < DOC_MAX_NBFLOORS &&
  1443. cascade->floors[floor]; floor++)
  1444. for (i = 0; !ret && i < 4; i++)
  1445. ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
  1446. if (!ret)
  1447. return 0;
  1448. do {
  1449. while (--i >= 0)
  1450. device_remove_file(dev, &doc_sys_attrs[floor][i]);
  1451. i = 4;
  1452. } while (--floor >= 0);
  1453. return ret;
  1454. }
  1455. static void doc_unregister_sysfs(struct platform_device *pdev,
  1456. struct docg3_cascade *cascade)
  1457. {
  1458. struct device *dev = &pdev->dev;
  1459. int floor, i;
  1460. for (floor = 0; floor < DOC_MAX_NBFLOORS && cascade->floors[floor];
  1461. floor++)
  1462. for (i = 0; i < 4; i++)
  1463. device_remove_file(dev, &doc_sys_attrs[floor][i]);
  1464. }
  1465. /*
  1466. * Debug sysfs entries
  1467. */
  1468. static int dbg_flashctrl_show(struct seq_file *s, void *p)
  1469. {
  1470. struct docg3 *docg3 = (struct docg3 *)s->private;
  1471. int pos = 0;
  1472. u8 fctrl;
  1473. mutex_lock(&docg3->cascade->lock);
  1474. fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  1475. mutex_unlock(&docg3->cascade->lock);
  1476. pos += seq_printf(s,
  1477. "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 pos;
  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 pos = 0, 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. pos += 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. pos += seq_printf(s, "reset");
  1507. break;
  1508. case DOC_ASICMODE_NORMAL:
  1509. pos += seq_printf(s, "normal");
  1510. break;
  1511. case DOC_ASICMODE_POWERDOWN:
  1512. pos += seq_printf(s, "powerdown");
  1513. break;
  1514. }
  1515. pos += seq_printf(s, ")\n");
  1516. return pos;
  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 pos = 0;
  1523. int id;
  1524. mutex_lock(&docg3->cascade->lock);
  1525. id = doc_register_readb(docg3, DOC_DEVICESELECT);
  1526. mutex_unlock(&docg3->cascade->lock);
  1527. pos += seq_printf(s, "DeviceId = %d\n", id);
  1528. return pos;
  1529. }
  1530. DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
  1531. static int dbg_protection_show(struct seq_file *s, void *p)
  1532. {
  1533. struct docg3 *docg3 = (struct docg3 *)s->private;
  1534. int pos = 0;
  1535. int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
  1536. mutex_lock(&docg3->cascade->lock);
  1537. protect = doc_register_readb(docg3, DOC_PROTECTION);
  1538. dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
  1539. dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
  1540. dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
  1541. dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
  1542. dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
  1543. dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
  1544. mutex_unlock(&docg3->cascade->lock);
  1545. pos += seq_printf(s, "Protection = 0x%02x (",
  1546. protect);
  1547. if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
  1548. pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
  1549. if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
  1550. pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
  1551. if (protect & DOC_PROTECT_LOCK_INPUT)
  1552. pos += seq_printf(s, "LOCK_INPUT,");
  1553. if (protect & DOC_PROTECT_STICKY_LOCK)
  1554. pos += seq_printf(s, "STICKY_LOCK,");
  1555. if (protect & DOC_PROTECT_PROTECTION_ENABLED)
  1556. pos += seq_printf(s, "PROTECTION ON,");
  1557. if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
  1558. pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
  1559. if (protect & DOC_PROTECT_PROTECTION_ERROR)
  1560. pos += seq_printf(s, "PROTECT_ERR,");
  1561. else
  1562. pos += seq_printf(s, "NO_PROTECT_ERR");
  1563. pos += seq_printf(s, ")\n");
  1564. pos += seq_printf(s, "DPS0 = 0x%02x : "
  1565. "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
  1566. "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
  1567. dps0, dps0_low, dps0_high,
  1568. !!(dps0 & DOC_DPS_OTP_PROTECTED),
  1569. !!(dps0 & DOC_DPS_READ_PROTECTED),
  1570. !!(dps0 & DOC_DPS_WRITE_PROTECTED),
  1571. !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
  1572. !!(dps0 & DOC_DPS_KEY_OK));
  1573. pos += seq_printf(s, "DPS1 = 0x%02x : "
  1574. "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
  1575. "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
  1576. dps1, dps1_low, dps1_high,
  1577. !!(dps1 & DOC_DPS_OTP_PROTECTED),
  1578. !!(dps1 & DOC_DPS_READ_PROTECTED),
  1579. !!(dps1 & DOC_DPS_WRITE_PROTECTED),
  1580. !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
  1581. !!(dps1 & DOC_DPS_KEY_OK));
  1582. return pos;
  1583. }
  1584. DEBUGFS_RO_ATTR(protection, dbg_protection_show);
  1585. static int __init doc_dbg_register(struct docg3 *docg3)
  1586. {
  1587. struct dentry *root, *entry;
  1588. root = debugfs_create_dir("docg3", NULL);
  1589. if (!root)
  1590. return -ENOMEM;
  1591. entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
  1592. &flashcontrol_fops);
  1593. if (entry)
  1594. entry = debugfs_create_file("asic_mode", S_IRUSR, root,
  1595. docg3, &asic_mode_fops);
  1596. if (entry)
  1597. entry = debugfs_create_file("device_id", S_IRUSR, root,
  1598. docg3, &device_id_fops);
  1599. if (entry)
  1600. entry = debugfs_create_file("protection", S_IRUSR, root,
  1601. docg3, &protection_fops);
  1602. if (entry) {
  1603. docg3->debugfs_root = root;
  1604. return 0;
  1605. } else {
  1606. debugfs_remove_recursive(root);
  1607. return -ENOMEM;
  1608. }
  1609. }
  1610. static void __exit doc_dbg_unregister(struct docg3 *docg3)
  1611. {
  1612. debugfs_remove_recursive(docg3->debugfs_root);
  1613. }
  1614. /**
  1615. * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
  1616. * @chip_id: The chip ID of the supported chip
  1617. * @mtd: The structure to fill
  1618. */
  1619. static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
  1620. {
  1621. struct docg3 *docg3 = mtd->priv;
  1622. int cfg;
  1623. cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
  1624. docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
  1625. docg3->reliable = reliable_mode;
  1626. switch (chip_id) {
  1627. case DOC_CHIPID_G3:
  1628. mtd->name = kasprintf(GFP_KERNEL, "docg3.%d",
  1629. docg3->device_id);
  1630. docg3->max_block = 2047;
  1631. break;
  1632. }
  1633. mtd->type = MTD_NANDFLASH;
  1634. mtd->flags = MTD_CAP_NANDFLASH;
  1635. mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
  1636. if (docg3->reliable == 2)
  1637. mtd->size /= 2;
  1638. mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
  1639. if (docg3->reliable == 2)
  1640. mtd->erasesize /= 2;
  1641. mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
  1642. mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
  1643. mtd->owner = THIS_MODULE;
  1644. mtd->_erase = doc_erase;
  1645. mtd->_read = doc_read;
  1646. mtd->_write = doc_write;
  1647. mtd->_read_oob = doc_read_oob;
  1648. mtd->_write_oob = doc_write_oob;
  1649. mtd->_block_isbad = doc_block_isbad;
  1650. mtd->ecclayout = &docg3_oobinfo;
  1651. mtd->ecc_strength = DOC_ECC_BCH_T;
  1652. }
  1653. /**
  1654. * doc_probe_device - Check if a device is available
  1655. * @base: the io space where the device is probed
  1656. * @floor: the floor of the probed device
  1657. * @dev: the device
  1658. * @cascade: the cascade of chips this devices will belong to
  1659. *
  1660. * Checks whether a device at the specified IO range, and floor is available.
  1661. *
  1662. * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
  1663. * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
  1664. * launched.
  1665. */
  1666. static struct mtd_info * __init
  1667. doc_probe_device(struct docg3_cascade *cascade, int floor, struct device *dev)
  1668. {
  1669. int ret, bbt_nbpages;
  1670. u16 chip_id, chip_id_inv;
  1671. struct docg3 *docg3;
  1672. struct mtd_info *mtd;
  1673. ret = -ENOMEM;
  1674. docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
  1675. if (!docg3)
  1676. goto nomem1;
  1677. mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
  1678. if (!mtd)
  1679. goto nomem2;
  1680. mtd->priv = docg3;
  1681. bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
  1682. 8 * DOC_LAYOUT_PAGE_SIZE);
  1683. docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
  1684. if (!docg3->bbt)
  1685. goto nomem3;
  1686. docg3->dev = dev;
  1687. docg3->device_id = floor;
  1688. docg3->cascade = cascade;
  1689. doc_set_device_id(docg3, docg3->device_id);
  1690. if (!floor)
  1691. doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
  1692. doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
  1693. chip_id = doc_register_readw(docg3, DOC_CHIPID);
  1694. chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
  1695. ret = 0;
  1696. if (chip_id != (u16)(~chip_id_inv)) {
  1697. goto nomem3;
  1698. }
  1699. switch (chip_id) {
  1700. case DOC_CHIPID_G3:
  1701. doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
  1702. docg3->cascade->base, floor);
  1703. break;
  1704. default:
  1705. doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
  1706. goto nomem3;
  1707. }
  1708. doc_set_driver_info(chip_id, mtd);
  1709. doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
  1710. doc_reload_bbt(docg3);
  1711. return mtd;
  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, found = 0;
  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. goto noress;
  1822. }
  1823. base = ioremap(ress->start, DOC_IOSPACE_SIZE);
  1824. ret = -ENOMEM;
  1825. cascade = kzalloc(sizeof(*cascade) * DOC_MAX_NBFLOORS,
  1826. GFP_KERNEL);
  1827. if (!cascade)
  1828. goto nomem1;
  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. goto nomem2;
  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. found++;
  1853. }
  1854. ret = doc_register_sysfs(pdev, cascade);
  1855. if (ret)
  1856. goto err_probe;
  1857. if (!found)
  1858. goto notfound;
  1859. platform_set_drvdata(pdev, cascade);
  1860. doc_dbg_register(cascade->floors[0]->priv);
  1861. return 0;
  1862. notfound:
  1863. ret = -ENODEV;
  1864. dev_info(dev, "No supported DiskOnChip found\n");
  1865. err_probe:
  1866. kfree(cascade->bch);
  1867. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
  1868. if (cascade->floors[floor])
  1869. doc_release_device(cascade->floors[floor]);
  1870. nomem2:
  1871. kfree(cascade);
  1872. nomem1:
  1873. iounmap(base);
  1874. noress:
  1875. return ret;
  1876. }
  1877. /**
  1878. * docg3_release - Release the driver
  1879. * @pdev: the platform device
  1880. *
  1881. * Returns 0
  1882. */
  1883. static int __exit docg3_release(struct platform_device *pdev)
  1884. {
  1885. struct docg3_cascade *cascade = platform_get_drvdata(pdev);
  1886. struct docg3 *docg3 = cascade->floors[0]->priv;
  1887. void __iomem *base = cascade->base;
  1888. int floor;
  1889. doc_unregister_sysfs(pdev, cascade);
  1890. doc_dbg_unregister(docg3);
  1891. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
  1892. if (cascade->floors[floor])
  1893. doc_release_device(cascade->floors[floor]);
  1894. free_bch(docg3->cascade->bch);
  1895. kfree(cascade);
  1896. iounmap(base);
  1897. return 0;
  1898. }
  1899. static struct platform_driver g3_driver = {
  1900. .driver = {
  1901. .name = "docg3",
  1902. .owner = THIS_MODULE,
  1903. },
  1904. .suspend = docg3_suspend,
  1905. .resume = docg3_resume,
  1906. .remove = __exit_p(docg3_release),
  1907. };
  1908. static int __init docg3_init(void)
  1909. {
  1910. return platform_driver_probe(&g3_driver, docg3_probe);
  1911. }
  1912. module_init(docg3_init);
  1913. static void __exit docg3_exit(void)
  1914. {
  1915. platform_driver_unregister(&g3_driver);
  1916. }
  1917. module_exit(docg3_exit);
  1918. MODULE_LICENSE("GPL");
  1919. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  1920. MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");