docg3.c 52 KB

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