docg3.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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. * - block erase
  41. * - page write
  42. * - IPL write
  43. * - ECC fixing (lack of BCH algorith understanding)
  44. * - powerdown / powerup
  45. *
  46. * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
  47. * the driver assumes a 16bits data bus.
  48. *
  49. * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
  50. * - a 1 byte Hamming code stored in the OOB for each page
  51. * - a 7 bytes BCH code stored in the OOB for each page
  52. * The BCH part is only used for check purpose, no correction is available as
  53. * some information is missing. What is known is that :
  54. * - BCH is in GF(2^14)
  55. * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
  56. * + 1 hamming byte)
  57. * - BCH can correct up to 4 bits (t = 4)
  58. * - BCH syndroms are calculated in hardware, and checked in hardware as well
  59. *
  60. */
  61. static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
  62. {
  63. u8 val = readb(docg3->base + reg);
  64. trace_docg3_io(0, 8, reg, (int)val);
  65. return val;
  66. }
  67. static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
  68. {
  69. u16 val = readw(docg3->base + reg);
  70. trace_docg3_io(0, 16, reg, (int)val);
  71. return val;
  72. }
  73. static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
  74. {
  75. writeb(val, docg3->base + reg);
  76. trace_docg3_io(1, 8, reg, val);
  77. }
  78. static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
  79. {
  80. writew(val, docg3->base + reg);
  81. trace_docg3_io(1, 16, reg, val);
  82. }
  83. static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
  84. {
  85. doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
  86. }
  87. static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
  88. {
  89. doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
  90. }
  91. static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
  92. {
  93. doc_writeb(docg3, addr, DOC_FLASHADDRESS);
  94. }
  95. static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
  96. static int doc_register_readb(struct docg3 *docg3, int reg)
  97. {
  98. u8 val;
  99. doc_writew(docg3, reg, DOC_READADDRESS);
  100. val = doc_readb(docg3, reg);
  101. doc_vdbg("Read register %04x : %02x\n", reg, val);
  102. return val;
  103. }
  104. static int doc_register_readw(struct docg3 *docg3, int reg)
  105. {
  106. u16 val;
  107. doc_writew(docg3, reg, DOC_READADDRESS);
  108. val = doc_readw(docg3, reg);
  109. doc_vdbg("Read register %04x : %04x\n", reg, val);
  110. return val;
  111. }
  112. /**
  113. * doc_delay - delay docg3 operations
  114. * @docg3: the device
  115. * @nbNOPs: the number of NOPs to issue
  116. *
  117. * As no specification is available, the right timings between chip commands are
  118. * unknown. The only available piece of information are the observed nops on a
  119. * working docg3 chip.
  120. * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
  121. * friendlier msleep() functions or blocking mdelay().
  122. */
  123. static void doc_delay(struct docg3 *docg3, int nbNOPs)
  124. {
  125. int i;
  126. doc_vdbg("NOP x %d\n", nbNOPs);
  127. for (i = 0; i < nbNOPs; i++)
  128. doc_writeb(docg3, 0, DOC_NOP);
  129. }
  130. static int is_prot_seq_error(struct docg3 *docg3)
  131. {
  132. int ctrl;
  133. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  134. return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
  135. }
  136. static int doc_is_ready(struct docg3 *docg3)
  137. {
  138. int ctrl;
  139. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  140. return ctrl & DOC_CTRL_FLASHREADY;
  141. }
  142. static int doc_wait_ready(struct docg3 *docg3)
  143. {
  144. int maxWaitCycles = 100;
  145. do {
  146. doc_delay(docg3, 4);
  147. cpu_relax();
  148. } while (!doc_is_ready(docg3) && maxWaitCycles--);
  149. doc_delay(docg3, 2);
  150. if (maxWaitCycles > 0)
  151. return 0;
  152. else
  153. return -EIO;
  154. }
  155. static int doc_reset_seq(struct docg3 *docg3)
  156. {
  157. int ret;
  158. doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
  159. doc_flash_sequence(docg3, DOC_SEQ_RESET);
  160. doc_flash_command(docg3, DOC_CMD_RESET);
  161. doc_delay(docg3, 2);
  162. ret = doc_wait_ready(docg3);
  163. doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
  164. return ret;
  165. }
  166. /**
  167. * doc_read_data_area - Read data from data area
  168. * @docg3: the device
  169. * @buf: the buffer to fill in
  170. * @len: the lenght to read
  171. * @first: first time read, DOC_READADDRESS should be set
  172. *
  173. * Reads bytes from flash data. Handles the single byte / even bytes reads.
  174. */
  175. static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
  176. int first)
  177. {
  178. int i, cdr, len4;
  179. u16 data16, *dst16;
  180. u8 data8, *dst8;
  181. doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
  182. cdr = len & 0x3;
  183. len4 = len - cdr;
  184. if (first)
  185. doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
  186. dst16 = buf;
  187. for (i = 0; i < len4; i += 2) {
  188. data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
  189. *dst16 = data16;
  190. dst16++;
  191. }
  192. if (cdr) {
  193. doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
  194. DOC_READADDRESS);
  195. doc_delay(docg3, 1);
  196. dst8 = (u8 *)dst16;
  197. for (i = 0; i < cdr; i++) {
  198. data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
  199. *dst8 = data8;
  200. dst8++;
  201. }
  202. }
  203. }
  204. /**
  205. * doc_set_data_mode - Sets the flash to reliable data mode
  206. * @docg3: the device
  207. *
  208. * The reliable data mode is a bit slower than the fast mode, but less errors
  209. * occur. Entering the reliable mode cannot be done without entering the fast
  210. * mode first.
  211. */
  212. static void doc_set_reliable_mode(struct docg3 *docg3)
  213. {
  214. doc_dbg("doc_set_reliable_mode()\n");
  215. doc_flash_sequence(docg3, DOC_SEQ_SET_MODE);
  216. doc_flash_command(docg3, DOC_CMD_FAST_MODE);
  217. doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
  218. doc_delay(docg3, 2);
  219. }
  220. /**
  221. * doc_set_asic_mode - Set the ASIC mode
  222. * @docg3: the device
  223. * @mode: the mode
  224. *
  225. * The ASIC can work in 3 modes :
  226. * - RESET: all registers are zeroed
  227. * - NORMAL: receives and handles commands
  228. * - POWERDOWN: minimal poweruse, flash parts shut off
  229. */
  230. static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
  231. {
  232. int i;
  233. for (i = 0; i < 12; i++)
  234. doc_readb(docg3, DOC_IOSPACE_IPL);
  235. mode |= DOC_ASICMODE_MDWREN;
  236. doc_dbg("doc_set_asic_mode(%02x)\n", mode);
  237. doc_writeb(docg3, mode, DOC_ASICMODE);
  238. doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
  239. doc_delay(docg3, 1);
  240. }
  241. /**
  242. * doc_set_device_id - Sets the devices id for cascaded G3 chips
  243. * @docg3: the device
  244. * @id: the chip to select (amongst 0, 1, 2, 3)
  245. *
  246. * There can be 4 cascaded G3 chips. This function selects the one which will
  247. * should be the active one.
  248. */
  249. static void doc_set_device_id(struct docg3 *docg3, int id)
  250. {
  251. u8 ctrl;
  252. doc_dbg("doc_set_device_id(%d)\n", id);
  253. doc_writeb(docg3, id, DOC_DEVICESELECT);
  254. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  255. ctrl &= ~DOC_CTRL_VIOLATION;
  256. ctrl |= DOC_CTRL_CE;
  257. doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
  258. }
  259. /**
  260. * doc_set_extra_page_mode - Change flash page layout
  261. * @docg3: the device
  262. *
  263. * Normally, the flash page is split into the data (512 bytes) and the out of
  264. * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
  265. * leveling counters are stored. To access this last area of 4 bytes, a special
  266. * mode must be input to the flash ASIC.
  267. *
  268. * Returns 0 if no error occured, -EIO else.
  269. */
  270. static int doc_set_extra_page_mode(struct docg3 *docg3)
  271. {
  272. int fctrl;
  273. doc_dbg("doc_set_extra_page_mode()\n");
  274. doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
  275. doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
  276. doc_delay(docg3, 2);
  277. fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  278. if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
  279. return -EIO;
  280. else
  281. return 0;
  282. }
  283. /**
  284. * doc_seek - Set both flash planes to the specified block, page for reading
  285. * @docg3: the device
  286. * @block0: the first plane block index
  287. * @block1: the second plane block index
  288. * @page: the page index within the block
  289. * @wear: if true, read will occur on the 4 extra bytes of the wear area
  290. * @ofs: offset in page to read
  291. *
  292. * Programs the flash even and odd planes to the specific block and page.
  293. * Alternatively, programs the flash to the wear area of the specified page.
  294. */
  295. static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
  296. int wear, int ofs)
  297. {
  298. int sector, ret = 0;
  299. doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
  300. block0, block1, page, ofs, wear);
  301. if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
  302. doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
  303. doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
  304. doc_delay(docg3, 2);
  305. } else {
  306. doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
  307. doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
  308. doc_delay(docg3, 2);
  309. }
  310. doc_set_reliable_mode(docg3);
  311. if (wear)
  312. ret = doc_set_extra_page_mode(docg3);
  313. if (ret)
  314. goto out;
  315. sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
  316. doc_flash_sequence(docg3, DOC_SEQ_READ);
  317. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  318. doc_delay(docg3, 1);
  319. doc_flash_address(docg3, sector & 0xff);
  320. doc_flash_address(docg3, (sector >> 8) & 0xff);
  321. doc_flash_address(docg3, (sector >> 16) & 0xff);
  322. doc_delay(docg3, 1);
  323. sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
  324. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  325. doc_delay(docg3, 1);
  326. doc_flash_address(docg3, sector & 0xff);
  327. doc_flash_address(docg3, (sector >> 8) & 0xff);
  328. doc_flash_address(docg3, (sector >> 16) & 0xff);
  329. doc_delay(docg3, 2);
  330. out:
  331. return ret;
  332. }
  333. /**
  334. * doc_read_page_ecc_init - Initialize hardware ECC engine
  335. * @docg3: the device
  336. * @len: the number of bytes covered by the ECC (BCH covered)
  337. *
  338. * The function does initialize the hardware ECC engine to compute the Hamming
  339. * ECC (on 1 byte) and the BCH Syndroms (on 7 bytes).
  340. *
  341. * Return 0 if succeeded, -EIO on error
  342. */
  343. static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
  344. {
  345. doc_writew(docg3, DOC_ECCCONF0_READ_MODE
  346. | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
  347. | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
  348. DOC_ECCCONF0);
  349. doc_delay(docg3, 4);
  350. doc_register_readb(docg3, DOC_FLASHCONTROL);
  351. return doc_wait_ready(docg3);
  352. }
  353. /**
  354. * doc_read_page_prepare - Prepares reading data from a flash page
  355. * @docg3: the device
  356. * @block0: the first plane block index on flash memory
  357. * @block1: the second plane block index on flash memory
  358. * @page: the page index in the block
  359. * @offset: the offset in the page (must be a multiple of 4)
  360. *
  361. * Prepares the page to be read in the flash memory :
  362. * - tell ASIC to map the flash pages
  363. * - tell ASIC to be in read mode
  364. *
  365. * After a call to this method, a call to doc_read_page_finish is mandatory,
  366. * to end the read cycle of the flash.
  367. *
  368. * Read data from a flash page. The length to be read must be between 0 and
  369. * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
  370. * the extra bytes reading is not implemented).
  371. *
  372. * As pages are grouped by 2 (in 2 planes), reading from a page must be done
  373. * in two steps:
  374. * - one read of 512 bytes at offset 0
  375. * - one read of 512 bytes at offset 512 + 16
  376. *
  377. * Returns 0 if successful, -EIO if a read error occured.
  378. */
  379. static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
  380. int page, int offset)
  381. {
  382. int wear_area = 0, ret = 0;
  383. doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
  384. block0, block1, page, offset);
  385. if (offset >= DOC_LAYOUT_WEAR_OFFSET)
  386. wear_area = 1;
  387. if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
  388. return -EINVAL;
  389. doc_set_device_id(docg3, docg3->device_id);
  390. ret = doc_reset_seq(docg3);
  391. if (ret)
  392. goto err;
  393. /* Program the flash address block and page */
  394. ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
  395. if (ret)
  396. goto err;
  397. doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
  398. doc_delay(docg3, 2);
  399. doc_wait_ready(docg3);
  400. doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
  401. doc_delay(docg3, 1);
  402. if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
  403. offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
  404. doc_flash_address(docg3, offset >> 2);
  405. doc_delay(docg3, 1);
  406. doc_wait_ready(docg3);
  407. doc_flash_command(docg3, DOC_CMD_READ_FLASH);
  408. return 0;
  409. err:
  410. doc_writeb(docg3, 0, DOC_DATAEND);
  411. doc_delay(docg3, 2);
  412. return -EIO;
  413. }
  414. /**
  415. * doc_read_page_getbytes - Reads bytes from a prepared page
  416. * @docg3: the device
  417. * @len: the number of bytes to be read (must be a multiple of 4)
  418. * @buf: the buffer to be filled in
  419. * @first: 1 if first time read, DOC_READADDRESS should be set
  420. *
  421. */
  422. static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
  423. int first)
  424. {
  425. doc_read_data_area(docg3, buf, len, first);
  426. doc_delay(docg3, 2);
  427. return len;
  428. }
  429. /**
  430. * doc_get_hw_bch_syndroms - Get hardware calculated BCH syndroms
  431. * @docg3: the device
  432. * @syns: the array of 7 integers where the syndroms will be stored
  433. */
  434. static void doc_get_hw_bch_syndroms(struct docg3 *docg3, int *syns)
  435. {
  436. int i;
  437. for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
  438. syns[i] = doc_register_readb(docg3, DOC_BCH_SYNDROM(i));
  439. }
  440. /**
  441. * doc_read_page_finish - Ends reading of a flash page
  442. * @docg3: the device
  443. *
  444. * As a side effect, resets the chip selector to 0. This ensures that after each
  445. * read operation, the floor 0 is selected. Therefore, if the systems halts, the
  446. * reboot will boot on floor 0, where the IPL is.
  447. */
  448. static void doc_read_page_finish(struct docg3 *docg3)
  449. {
  450. doc_writeb(docg3, 0, DOC_DATAEND);
  451. doc_delay(docg3, 2);
  452. doc_set_device_id(docg3, 0);
  453. }
  454. /**
  455. * calc_block_sector - Calculate blocks, pages and ofs.
  456. * @from: offset in flash
  457. * @block0: first plane block index calculated
  458. * @block1: second plane block index calculated
  459. * @page: page calculated
  460. * @ofs: offset in page
  461. */
  462. static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
  463. int *ofs)
  464. {
  465. uint sector;
  466. sector = from / DOC_LAYOUT_PAGE_SIZE;
  467. *block0 = sector / (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES)
  468. * DOC_LAYOUT_NBPLANES;
  469. *block1 = *block0 + 1;
  470. *page = sector % (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES);
  471. *page /= DOC_LAYOUT_NBPLANES;
  472. if (sector % 2)
  473. *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
  474. else
  475. *ofs = 0;
  476. }
  477. /**
  478. * doc_read - Read bytes from flash
  479. * @mtd: the device
  480. * @from: the offset from first block and first page, in bytes, aligned on page
  481. * size
  482. * @len: the number of bytes to read (must be a multiple of 4)
  483. * @retlen: the number of bytes actually read
  484. * @buf: the filled in buffer
  485. *
  486. * Reads flash memory pages. This function does not read the OOB chunk, but only
  487. * the page data.
  488. *
  489. * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
  490. */
  491. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  492. size_t *retlen, u_char *buf)
  493. {
  494. struct docg3 *docg3 = mtd->priv;
  495. int block0, block1, page, readlen, ret, ofs = 0;
  496. int syn[DOC_ECC_BCH_SIZE], eccconf1;
  497. u8 oob[DOC_LAYOUT_OOB_SIZE];
  498. ret = -EINVAL;
  499. doc_dbg("doc_read(from=%lld, len=%zu, buf=%p)\n", from, len, buf);
  500. if (from % DOC_LAYOUT_PAGE_SIZE)
  501. goto err;
  502. if (len % 4)
  503. goto err;
  504. calc_block_sector(from, &block0, &block1, &page, &ofs);
  505. if (block1 > docg3->max_block)
  506. goto err;
  507. *retlen = 0;
  508. ret = 0;
  509. readlen = min_t(size_t, len, (size_t)DOC_LAYOUT_PAGE_SIZE);
  510. while (!ret && len > 0) {
  511. readlen = min_t(size_t, len, (size_t)DOC_LAYOUT_PAGE_SIZE);
  512. ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
  513. if (ret < 0)
  514. goto err;
  515. ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_COVERED_BYTES);
  516. if (ret < 0)
  517. goto err_in_read;
  518. ret = doc_read_page_getbytes(docg3, readlen, buf, 1);
  519. if (ret < readlen)
  520. goto err_in_read;
  521. ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE,
  522. oob, 0);
  523. if (ret < DOC_LAYOUT_OOB_SIZE)
  524. goto err_in_read;
  525. *retlen += readlen;
  526. buf += readlen;
  527. len -= readlen;
  528. ofs ^= DOC_LAYOUT_PAGE_OOB_SIZE;
  529. if (ofs == 0)
  530. page += 2;
  531. if (page > DOC_ADDR_PAGE_MASK) {
  532. page = 0;
  533. block0 += 2;
  534. block1 += 2;
  535. }
  536. /*
  537. * There should be a BCH bitstream fixing algorithm here ...
  538. * By now, a page read failure is triggered by BCH error
  539. */
  540. doc_get_hw_bch_syndroms(docg3, syn);
  541. eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
  542. doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  543. oob[0], oob[1], oob[2], oob[3], oob[4],
  544. oob[5], oob[6]);
  545. doc_dbg("OOB - HAMMING: %02x\n", oob[7]);
  546. doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  547. oob[8], oob[9], oob[10], oob[11], oob[12],
  548. oob[13], oob[14]);
  549. doc_dbg("OOB - UNUSED: %02x\n", oob[15]);
  550. doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
  551. doc_dbg("ECC BCH syndrom: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  552. syn[0], syn[1], syn[2], syn[3], syn[4], syn[5], syn[6]);
  553. ret = -EBADMSG;
  554. if (block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) {
  555. if (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR)
  556. goto err_in_read;
  557. if (is_prot_seq_error(docg3))
  558. goto err_in_read;
  559. }
  560. doc_read_page_finish(docg3);
  561. }
  562. return 0;
  563. err_in_read:
  564. doc_read_page_finish(docg3);
  565. err:
  566. return ret;
  567. }
  568. /**
  569. * doc_read_oob - Read out of band bytes from flash
  570. * @mtd: the device
  571. * @from: the offset from first block and first page, in bytes, aligned on page
  572. * size
  573. * @ops: the mtd oob structure
  574. *
  575. * Reads flash memory OOB area of pages.
  576. *
  577. * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
  578. */
  579. static int doc_read_oob(struct mtd_info *mtd, loff_t from,
  580. struct mtd_oob_ops *ops)
  581. {
  582. struct docg3 *docg3 = mtd->priv;
  583. int block0, block1, page, ofs, ret;
  584. u8 *buf = ops->oobbuf;
  585. size_t len = ops->ooblen;
  586. doc_dbg("doc_read_oob(from=%lld, buf=%p, len=%zu)\n", from, buf, len);
  587. if (len != DOC_LAYOUT_OOB_SIZE)
  588. return -EINVAL;
  589. switch (ops->mode) {
  590. case MTD_OPS_PLACE_OOB:
  591. buf += ops->ooboffs;
  592. break;
  593. default:
  594. break;
  595. }
  596. calc_block_sector(from, &block0, &block1, &page, &ofs);
  597. if (block1 > docg3->max_block)
  598. return -EINVAL;
  599. ret = doc_read_page_prepare(docg3, block0, block1, page,
  600. ofs + DOC_LAYOUT_PAGE_SIZE);
  601. if (!ret)
  602. ret = doc_read_page_ecc_init(docg3, DOC_LAYOUT_OOB_SIZE);
  603. if (!ret)
  604. ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE,
  605. buf, 1);
  606. doc_read_page_finish(docg3);
  607. if (ret > 0)
  608. ops->oobretlen = ret;
  609. else
  610. ops->oobretlen = 0;
  611. return (ret > 0) ? 0 : ret;
  612. }
  613. static int doc_reload_bbt(struct docg3 *docg3)
  614. {
  615. int block = DOC_LAYOUT_BLOCK_BBT;
  616. int ret = 0, nbpages, page;
  617. u_char *buf = docg3->bbt;
  618. nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
  619. for (page = 0; !ret && (page < nbpages); page++) {
  620. ret = doc_read_page_prepare(docg3, block, block + 1,
  621. page + DOC_LAYOUT_PAGE_BBT, 0);
  622. if (!ret)
  623. ret = doc_read_page_ecc_init(docg3,
  624. DOC_LAYOUT_PAGE_SIZE);
  625. if (!ret)
  626. doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
  627. buf, 1);
  628. buf += DOC_LAYOUT_PAGE_SIZE;
  629. }
  630. doc_read_page_finish(docg3);
  631. return ret;
  632. }
  633. /**
  634. * doc_block_isbad - Checks whether a block is good or not
  635. * @mtd: the device
  636. * @from: the offset to find the correct block
  637. *
  638. * Returns 1 if block is bad, 0 if block is good
  639. */
  640. static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
  641. {
  642. struct docg3 *docg3 = mtd->priv;
  643. int block0, block1, page, ofs, is_good;
  644. calc_block_sector(from, &block0, &block1, &page, &ofs);
  645. doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
  646. from, block0, block1, page, ofs);
  647. if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
  648. return 0;
  649. if (block1 > docg3->max_block)
  650. return -EINVAL;
  651. is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
  652. return !is_good;
  653. }
  654. /**
  655. * doc_get_erase_count - Get block erase count
  656. * @docg3: the device
  657. * @from: the offset in which the block is.
  658. *
  659. * Get the number of times a block was erased. The number is the maximum of
  660. * erase times between first and second plane (which should be equal normally).
  661. *
  662. * Returns The number of erases, or -EINVAL or -EIO on error.
  663. */
  664. static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
  665. {
  666. u8 buf[DOC_LAYOUT_WEAR_SIZE];
  667. int ret, plane1_erase_count, plane2_erase_count;
  668. int block0, block1, page, ofs;
  669. doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
  670. if (from % DOC_LAYOUT_PAGE_SIZE)
  671. return -EINVAL;
  672. calc_block_sector(from, &block0, &block1, &page, &ofs);
  673. if (block1 > docg3->max_block)
  674. return -EINVAL;
  675. ret = doc_reset_seq(docg3);
  676. if (!ret)
  677. ret = doc_read_page_prepare(docg3, block0, block1, page,
  678. ofs + DOC_LAYOUT_WEAR_OFFSET);
  679. if (!ret)
  680. ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
  681. buf, 1);
  682. doc_read_page_finish(docg3);
  683. if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
  684. return -EIO;
  685. plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
  686. | ((u8)(~buf[5]) << 16);
  687. plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
  688. | ((u8)(~buf[7]) << 16);
  689. return max(plane1_erase_count, plane2_erase_count);
  690. }
  691. /*
  692. * Debug sysfs entries
  693. */
  694. static int dbg_flashctrl_show(struct seq_file *s, void *p)
  695. {
  696. struct docg3 *docg3 = (struct docg3 *)s->private;
  697. int pos = 0;
  698. u8 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  699. pos += seq_printf(s,
  700. "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
  701. fctrl,
  702. fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
  703. fctrl & DOC_CTRL_CE ? "active" : "inactive",
  704. fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
  705. fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
  706. fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
  707. return pos;
  708. }
  709. DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
  710. static int dbg_asicmode_show(struct seq_file *s, void *p)
  711. {
  712. struct docg3 *docg3 = (struct docg3 *)s->private;
  713. int pos = 0;
  714. int pctrl = doc_register_readb(docg3, DOC_ASICMODE);
  715. int mode = pctrl & 0x03;
  716. pos += seq_printf(s,
  717. "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
  718. pctrl,
  719. pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
  720. pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
  721. pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
  722. pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
  723. pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
  724. mode >> 1, mode & 0x1);
  725. switch (mode) {
  726. case DOC_ASICMODE_RESET:
  727. pos += seq_printf(s, "reset");
  728. break;
  729. case DOC_ASICMODE_NORMAL:
  730. pos += seq_printf(s, "normal");
  731. break;
  732. case DOC_ASICMODE_POWERDOWN:
  733. pos += seq_printf(s, "powerdown");
  734. break;
  735. }
  736. pos += seq_printf(s, ")\n");
  737. return pos;
  738. }
  739. DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
  740. static int dbg_device_id_show(struct seq_file *s, void *p)
  741. {
  742. struct docg3 *docg3 = (struct docg3 *)s->private;
  743. int pos = 0;
  744. int id = doc_register_readb(docg3, DOC_DEVICESELECT);
  745. pos += seq_printf(s, "DeviceId = %d\n", id);
  746. return pos;
  747. }
  748. DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
  749. static int dbg_protection_show(struct seq_file *s, void *p)
  750. {
  751. struct docg3 *docg3 = (struct docg3 *)s->private;
  752. int pos = 0;
  753. int protect = doc_register_readb(docg3, DOC_PROTECTION);
  754. int dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
  755. int dps0_low = doc_register_readb(docg3, DOC_DPS0_ADDRLOW);
  756. int dps0_high = doc_register_readb(docg3, DOC_DPS0_ADDRHIGH);
  757. int dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
  758. int dps1_low = doc_register_readb(docg3, DOC_DPS1_ADDRLOW);
  759. int dps1_high = doc_register_readb(docg3, DOC_DPS1_ADDRHIGH);
  760. pos += seq_printf(s, "Protection = 0x%02x (",
  761. protect);
  762. if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
  763. pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
  764. if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
  765. pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
  766. if (protect & DOC_PROTECT_LOCK_INPUT)
  767. pos += seq_printf(s, "LOCK_INPUT,");
  768. if (protect & DOC_PROTECT_STICKY_LOCK)
  769. pos += seq_printf(s, "STICKY_LOCK,");
  770. if (protect & DOC_PROTECT_PROTECTION_ENABLED)
  771. pos += seq_printf(s, "PROTECTION ON,");
  772. if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
  773. pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
  774. if (protect & DOC_PROTECT_PROTECTION_ERROR)
  775. pos += seq_printf(s, "PROTECT_ERR,");
  776. else
  777. pos += seq_printf(s, "NO_PROTECT_ERR");
  778. pos += seq_printf(s, ")\n");
  779. pos += seq_printf(s, "DPS0 = 0x%02x : "
  780. "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
  781. "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
  782. dps0, dps0_low, dps0_high,
  783. !!(dps0 & DOC_DPS_OTP_PROTECTED),
  784. !!(dps0 & DOC_DPS_READ_PROTECTED),
  785. !!(dps0 & DOC_DPS_WRITE_PROTECTED),
  786. !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
  787. !!(dps0 & DOC_DPS_KEY_OK));
  788. pos += seq_printf(s, "DPS1 = 0x%02x : "
  789. "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
  790. "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
  791. dps1, dps1_low, dps1_high,
  792. !!(dps1 & DOC_DPS_OTP_PROTECTED),
  793. !!(dps1 & DOC_DPS_READ_PROTECTED),
  794. !!(dps1 & DOC_DPS_WRITE_PROTECTED),
  795. !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
  796. !!(dps1 & DOC_DPS_KEY_OK));
  797. return pos;
  798. }
  799. DEBUGFS_RO_ATTR(protection, dbg_protection_show);
  800. static int __init doc_dbg_register(struct docg3 *docg3)
  801. {
  802. struct dentry *root, *entry;
  803. root = debugfs_create_dir("docg3", NULL);
  804. if (!root)
  805. return -ENOMEM;
  806. entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
  807. &flashcontrol_fops);
  808. if (entry)
  809. entry = debugfs_create_file("asic_mode", S_IRUSR, root,
  810. docg3, &asic_mode_fops);
  811. if (entry)
  812. entry = debugfs_create_file("device_id", S_IRUSR, root,
  813. docg3, &device_id_fops);
  814. if (entry)
  815. entry = debugfs_create_file("protection", S_IRUSR, root,
  816. docg3, &protection_fops);
  817. if (entry) {
  818. docg3->debugfs_root = root;
  819. return 0;
  820. } else {
  821. debugfs_remove_recursive(root);
  822. return -ENOMEM;
  823. }
  824. }
  825. static void __exit doc_dbg_unregister(struct docg3 *docg3)
  826. {
  827. debugfs_remove_recursive(docg3->debugfs_root);
  828. }
  829. /**
  830. * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
  831. * @chip_id: The chip ID of the supported chip
  832. * @mtd: The structure to fill
  833. */
  834. static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
  835. {
  836. struct docg3 *docg3 = mtd->priv;
  837. int cfg;
  838. cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
  839. docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
  840. switch (chip_id) {
  841. case DOC_CHIPID_G3:
  842. mtd->name = "DiskOnChip G3";
  843. docg3->max_block = 2047;
  844. break;
  845. }
  846. mtd->type = MTD_NANDFLASH;
  847. /*
  848. * Once write methods are added, the correct flags will be set.
  849. * mtd->flags = MTD_CAP_NANDFLASH;
  850. */
  851. mtd->flags = MTD_CAP_ROM;
  852. mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
  853. mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
  854. mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
  855. mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
  856. mtd->owner = THIS_MODULE;
  857. mtd->erase = NULL;
  858. mtd->point = NULL;
  859. mtd->unpoint = NULL;
  860. mtd->read = doc_read;
  861. mtd->write = NULL;
  862. mtd->read_oob = doc_read_oob;
  863. mtd->write_oob = NULL;
  864. mtd->sync = NULL;
  865. mtd->block_isbad = doc_block_isbad;
  866. }
  867. /**
  868. * doc_probe - Probe the IO space for a DiskOnChip G3 chip
  869. * @pdev: platform device
  870. *
  871. * Probes for a G3 chip at the specified IO space in the platform data
  872. * ressources.
  873. *
  874. * Returns 0 on success, -ENOMEM, -ENXIO on error
  875. */
  876. static int __init docg3_probe(struct platform_device *pdev)
  877. {
  878. struct device *dev = &pdev->dev;
  879. struct docg3 *docg3;
  880. struct mtd_info *mtd;
  881. struct resource *ress;
  882. int ret, bbt_nbpages;
  883. u16 chip_id, chip_id_inv;
  884. ret = -ENOMEM;
  885. docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
  886. if (!docg3)
  887. goto nomem1;
  888. mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
  889. if (!mtd)
  890. goto nomem2;
  891. mtd->priv = docg3;
  892. ret = -ENXIO;
  893. ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  894. if (!ress) {
  895. dev_err(dev, "No I/O memory resource defined\n");
  896. goto noress;
  897. }
  898. docg3->base = ioremap(ress->start, DOC_IOSPACE_SIZE);
  899. docg3->dev = &pdev->dev;
  900. docg3->device_id = 0;
  901. doc_set_device_id(docg3, docg3->device_id);
  902. doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
  903. doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
  904. chip_id = doc_register_readw(docg3, DOC_CHIPID);
  905. chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
  906. ret = -ENODEV;
  907. if (chip_id != (u16)(~chip_id_inv)) {
  908. doc_info("No device found at IO addr %p\n",
  909. (void *)ress->start);
  910. goto nochipfound;
  911. }
  912. switch (chip_id) {
  913. case DOC_CHIPID_G3:
  914. doc_info("Found a G3 DiskOnChip at addr %p\n",
  915. (void *)ress->start);
  916. break;
  917. default:
  918. doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
  919. goto nochipfound;
  920. }
  921. doc_set_driver_info(chip_id, mtd);
  922. platform_set_drvdata(pdev, mtd);
  923. ret = -ENOMEM;
  924. bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
  925. 8 * DOC_LAYOUT_PAGE_SIZE);
  926. docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
  927. if (!docg3->bbt)
  928. goto nochipfound;
  929. doc_reload_bbt(docg3);
  930. ret = mtd_device_parse_register(mtd, part_probes,
  931. NULL, NULL, 0);
  932. if (ret)
  933. goto register_error;
  934. doc_dbg_register(docg3);
  935. return 0;
  936. register_error:
  937. kfree(docg3->bbt);
  938. nochipfound:
  939. iounmap(docg3->base);
  940. noress:
  941. kfree(mtd);
  942. nomem2:
  943. kfree(docg3);
  944. nomem1:
  945. return ret;
  946. }
  947. /**
  948. * docg3_release - Release the driver
  949. * @pdev: the platform device
  950. *
  951. * Returns 0
  952. */
  953. static int __exit docg3_release(struct platform_device *pdev)
  954. {
  955. struct mtd_info *mtd = platform_get_drvdata(pdev);
  956. struct docg3 *docg3 = mtd->priv;
  957. doc_dbg_unregister(docg3);
  958. mtd_device_unregister(mtd);
  959. iounmap(docg3->base);
  960. kfree(docg3->bbt);
  961. kfree(docg3);
  962. kfree(mtd);
  963. return 0;
  964. }
  965. static struct platform_driver g3_driver = {
  966. .driver = {
  967. .name = "docg3",
  968. .owner = THIS_MODULE,
  969. },
  970. .remove = __exit_p(docg3_release),
  971. };
  972. static int __init docg3_init(void)
  973. {
  974. return platform_driver_probe(&g3_driver, docg3_probe);
  975. }
  976. module_init(docg3_init);
  977. static void __exit docg3_exit(void)
  978. {
  979. platform_driver_unregister(&g3_driver);
  980. }
  981. module_exit(docg3_exit);
  982. MODULE_LICENSE("GPL");
  983. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  984. MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");