docg3.c 58 KB

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