docg3.c 57 KB

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