docg3.c 56 KB

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