docg3.c 58 KB

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