docg3.c 59 KB

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