docg3.c 48 KB

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