docg3.c 57 KB

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