mtd_subpagetest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright (C) 2006-2007 Nokia Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; see the file COPYING. If not, write to the Free Software
  15. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Test sub-page read and write on MTD device.
  18. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  19. *
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/err.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #define PRINT_PREF KERN_INFO "mtd_subpagetest: "
  29. static int dev = -EINVAL;
  30. module_param(dev, int, S_IRUGO);
  31. MODULE_PARM_DESC(dev, "MTD device number to use");
  32. static struct mtd_info *mtd;
  33. static unsigned char *writebuf;
  34. static unsigned char *readbuf;
  35. static unsigned char *bbt;
  36. static int subpgsize;
  37. static int bufsize;
  38. static int ebcnt;
  39. static int pgcnt;
  40. static int errcnt;
  41. static unsigned long next = 1;
  42. static inline unsigned int simple_rand(void)
  43. {
  44. next = next * 1103515245 + 12345;
  45. return (unsigned int)((next / 65536) % 32768);
  46. }
  47. static inline void simple_srand(unsigned long seed)
  48. {
  49. next = seed;
  50. }
  51. static void set_random_data(unsigned char *buf, size_t len)
  52. {
  53. size_t i;
  54. for (i = 0; i < len; ++i)
  55. buf[i] = simple_rand();
  56. }
  57. static inline void clear_data(unsigned char *buf, size_t len)
  58. {
  59. memset(buf, 0, len);
  60. }
  61. static int erase_eraseblock(int ebnum)
  62. {
  63. int err;
  64. struct erase_info ei;
  65. loff_t addr = ebnum * mtd->erasesize;
  66. memset(&ei, 0, sizeof(struct erase_info));
  67. ei.mtd = mtd;
  68. ei.addr = addr;
  69. ei.len = mtd->erasesize;
  70. err = mtd_erase(mtd, &ei);
  71. if (err) {
  72. printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
  73. return err;
  74. }
  75. if (ei.state == MTD_ERASE_FAILED) {
  76. printk(PRINT_PREF "some erase error occurred at EB %d\n",
  77. ebnum);
  78. return -EIO;
  79. }
  80. return 0;
  81. }
  82. static int erase_whole_device(void)
  83. {
  84. int err;
  85. unsigned int i;
  86. printk(PRINT_PREF "erasing whole device\n");
  87. for (i = 0; i < ebcnt; ++i) {
  88. if (bbt[i])
  89. continue;
  90. err = erase_eraseblock(i);
  91. if (err)
  92. return err;
  93. cond_resched();
  94. }
  95. printk(PRINT_PREF "erased %u eraseblocks\n", i);
  96. return 0;
  97. }
  98. static int write_eraseblock(int ebnum)
  99. {
  100. size_t written;
  101. int err = 0;
  102. loff_t addr = ebnum * mtd->erasesize;
  103. set_random_data(writebuf, subpgsize);
  104. err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
  105. if (unlikely(err || written != subpgsize)) {
  106. printk(PRINT_PREF "error: write failed at %#llx\n",
  107. (long long)addr);
  108. if (written != subpgsize) {
  109. printk(PRINT_PREF " write size: %#x\n", subpgsize);
  110. printk(PRINT_PREF " written: %#zx\n", written);
  111. }
  112. return err ? err : -1;
  113. }
  114. addr += subpgsize;
  115. set_random_data(writebuf, subpgsize);
  116. err = mtd_write(mtd, addr, subpgsize, &written, writebuf);
  117. if (unlikely(err || written != subpgsize)) {
  118. printk(PRINT_PREF "error: write failed at %#llx\n",
  119. (long long)addr);
  120. if (written != subpgsize) {
  121. printk(PRINT_PREF " write size: %#x\n", subpgsize);
  122. printk(PRINT_PREF " written: %#zx\n", written);
  123. }
  124. return err ? err : -1;
  125. }
  126. return err;
  127. }
  128. static int write_eraseblock2(int ebnum)
  129. {
  130. size_t written;
  131. int err = 0, k;
  132. loff_t addr = ebnum * mtd->erasesize;
  133. for (k = 1; k < 33; ++k) {
  134. if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
  135. break;
  136. set_random_data(writebuf, subpgsize * k);
  137. err = mtd_write(mtd, addr, subpgsize * k, &written, writebuf);
  138. if (unlikely(err || written != subpgsize * k)) {
  139. printk(PRINT_PREF "error: write failed at %#llx\n",
  140. (long long)addr);
  141. if (written != subpgsize) {
  142. printk(PRINT_PREF " write size: %#x\n",
  143. subpgsize * k);
  144. printk(PRINT_PREF " written: %#08zx\n",
  145. written);
  146. }
  147. return err ? err : -1;
  148. }
  149. addr += subpgsize * k;
  150. }
  151. return err;
  152. }
  153. static void print_subpage(unsigned char *p)
  154. {
  155. int i, j;
  156. for (i = 0; i < subpgsize; ) {
  157. for (j = 0; i < subpgsize && j < 32; ++i, ++j)
  158. printk("%02x", *p++);
  159. printk("\n");
  160. }
  161. }
  162. static int verify_eraseblock(int ebnum)
  163. {
  164. size_t read;
  165. int err = 0;
  166. loff_t addr = ebnum * mtd->erasesize;
  167. set_random_data(writebuf, subpgsize);
  168. clear_data(readbuf, subpgsize);
  169. err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
  170. if (unlikely(err || read != subpgsize)) {
  171. if (mtd_is_bitflip(err) && read == subpgsize) {
  172. printk(PRINT_PREF "ECC correction at %#llx\n",
  173. (long long)addr);
  174. err = 0;
  175. } else {
  176. printk(PRINT_PREF "error: read failed at %#llx\n",
  177. (long long)addr);
  178. return err ? err : -1;
  179. }
  180. }
  181. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  182. printk(PRINT_PREF "error: verify failed at %#llx\n",
  183. (long long)addr);
  184. printk(PRINT_PREF "------------- written----------------\n");
  185. print_subpage(writebuf);
  186. printk(PRINT_PREF "------------- read ------------------\n");
  187. print_subpage(readbuf);
  188. printk(PRINT_PREF "-------------------------------------\n");
  189. errcnt += 1;
  190. }
  191. addr += subpgsize;
  192. set_random_data(writebuf, subpgsize);
  193. clear_data(readbuf, subpgsize);
  194. err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
  195. if (unlikely(err || read != subpgsize)) {
  196. if (mtd_is_bitflip(err) && read == subpgsize) {
  197. printk(PRINT_PREF "ECC correction at %#llx\n",
  198. (long long)addr);
  199. err = 0;
  200. } else {
  201. printk(PRINT_PREF "error: read failed at %#llx\n",
  202. (long long)addr);
  203. return err ? err : -1;
  204. }
  205. }
  206. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  207. printk(PRINT_PREF "error: verify failed at %#llx\n",
  208. (long long)addr);
  209. printk(PRINT_PREF "------------- written----------------\n");
  210. print_subpage(writebuf);
  211. printk(PRINT_PREF "------------- read ------------------\n");
  212. print_subpage(readbuf);
  213. printk(PRINT_PREF "-------------------------------------\n");
  214. errcnt += 1;
  215. }
  216. return err;
  217. }
  218. static int verify_eraseblock2(int ebnum)
  219. {
  220. size_t read;
  221. int err = 0, k;
  222. loff_t addr = ebnum * mtd->erasesize;
  223. for (k = 1; k < 33; ++k) {
  224. if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
  225. break;
  226. set_random_data(writebuf, subpgsize * k);
  227. clear_data(readbuf, subpgsize * k);
  228. err = mtd_read(mtd, addr, subpgsize * k, &read, readbuf);
  229. if (unlikely(err || read != subpgsize * k)) {
  230. if (mtd_is_bitflip(err) && read == subpgsize * k) {
  231. printk(PRINT_PREF "ECC correction at %#llx\n",
  232. (long long)addr);
  233. err = 0;
  234. } else {
  235. printk(PRINT_PREF "error: read failed at "
  236. "%#llx\n", (long long)addr);
  237. return err ? err : -1;
  238. }
  239. }
  240. if (unlikely(memcmp(readbuf, writebuf, subpgsize * k))) {
  241. printk(PRINT_PREF "error: verify failed at %#llx\n",
  242. (long long)addr);
  243. errcnt += 1;
  244. }
  245. addr += subpgsize * k;
  246. }
  247. return err;
  248. }
  249. static int verify_eraseblock_ff(int ebnum)
  250. {
  251. uint32_t j;
  252. size_t read;
  253. int err = 0;
  254. loff_t addr = ebnum * mtd->erasesize;
  255. memset(writebuf, 0xff, subpgsize);
  256. for (j = 0; j < mtd->erasesize / subpgsize; ++j) {
  257. clear_data(readbuf, subpgsize);
  258. err = mtd_read(mtd, addr, subpgsize, &read, readbuf);
  259. if (unlikely(err || read != subpgsize)) {
  260. if (mtd_is_bitflip(err) && read == subpgsize) {
  261. printk(PRINT_PREF "ECC correction at %#llx\n",
  262. (long long)addr);
  263. err = 0;
  264. } else {
  265. printk(PRINT_PREF "error: read failed at "
  266. "%#llx\n", (long long)addr);
  267. return err ? err : -1;
  268. }
  269. }
  270. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  271. printk(PRINT_PREF "error: verify 0xff failed at "
  272. "%#llx\n", (long long)addr);
  273. errcnt += 1;
  274. }
  275. addr += subpgsize;
  276. }
  277. return err;
  278. }
  279. static int verify_all_eraseblocks_ff(void)
  280. {
  281. int err;
  282. unsigned int i;
  283. printk(PRINT_PREF "verifying all eraseblocks for 0xff\n");
  284. for (i = 0; i < ebcnt; ++i) {
  285. if (bbt[i])
  286. continue;
  287. err = verify_eraseblock_ff(i);
  288. if (err)
  289. return err;
  290. if (i % 256 == 0)
  291. printk(PRINT_PREF "verified up to eraseblock %u\n", i);
  292. cond_resched();
  293. }
  294. printk(PRINT_PREF "verified %u eraseblocks\n", i);
  295. return 0;
  296. }
  297. static int is_block_bad(int ebnum)
  298. {
  299. loff_t addr = ebnum * mtd->erasesize;
  300. int ret;
  301. ret = mtd_block_isbad(mtd, addr);
  302. if (ret)
  303. printk(PRINT_PREF "block %d is bad\n", ebnum);
  304. return ret;
  305. }
  306. static int scan_for_bad_eraseblocks(void)
  307. {
  308. int i, bad = 0;
  309. bbt = kzalloc(ebcnt, GFP_KERNEL);
  310. if (!bbt) {
  311. printk(PRINT_PREF "error: cannot allocate memory\n");
  312. return -ENOMEM;
  313. }
  314. printk(PRINT_PREF "scanning for bad eraseblocks\n");
  315. for (i = 0; i < ebcnt; ++i) {
  316. bbt[i] = is_block_bad(i) ? 1 : 0;
  317. if (bbt[i])
  318. bad += 1;
  319. cond_resched();
  320. }
  321. printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
  322. return 0;
  323. }
  324. static int __init mtd_subpagetest_init(void)
  325. {
  326. int err = 0;
  327. uint32_t i;
  328. uint64_t tmp;
  329. printk(KERN_INFO "\n");
  330. printk(KERN_INFO "=================================================\n");
  331. if (dev < 0) {
  332. printk(PRINT_PREF "Please specify a valid mtd-device via module paramter\n");
  333. printk(KERN_CRIT "CAREFUL: This test wipes all data on the specified MTD device!\n");
  334. return -EINVAL;
  335. }
  336. printk(PRINT_PREF "MTD device: %d\n", dev);
  337. mtd = get_mtd_device(NULL, dev);
  338. if (IS_ERR(mtd)) {
  339. err = PTR_ERR(mtd);
  340. printk(PRINT_PREF "error: cannot get MTD device\n");
  341. return err;
  342. }
  343. if (mtd->type != MTD_NANDFLASH) {
  344. printk(PRINT_PREF "this test requires NAND flash\n");
  345. goto out;
  346. }
  347. subpgsize = mtd->writesize >> mtd->subpage_sft;
  348. tmp = mtd->size;
  349. do_div(tmp, mtd->erasesize);
  350. ebcnt = tmp;
  351. pgcnt = mtd->erasesize / mtd->writesize;
  352. printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
  353. "page size %u, subpage size %u, count of eraseblocks %u, "
  354. "pages per eraseblock %u, OOB size %u\n",
  355. (unsigned long long)mtd->size, mtd->erasesize,
  356. mtd->writesize, subpgsize, ebcnt, pgcnt, mtd->oobsize);
  357. err = -ENOMEM;
  358. bufsize = subpgsize * 32;
  359. writebuf = kmalloc(bufsize, GFP_KERNEL);
  360. if (!writebuf) {
  361. printk(PRINT_PREF "error: cannot allocate memory\n");
  362. goto out;
  363. }
  364. readbuf = kmalloc(bufsize, GFP_KERNEL);
  365. if (!readbuf) {
  366. printk(PRINT_PREF "error: cannot allocate memory\n");
  367. goto out;
  368. }
  369. err = scan_for_bad_eraseblocks();
  370. if (err)
  371. goto out;
  372. err = erase_whole_device();
  373. if (err)
  374. goto out;
  375. printk(PRINT_PREF "writing whole device\n");
  376. simple_srand(1);
  377. for (i = 0; i < ebcnt; ++i) {
  378. if (bbt[i])
  379. continue;
  380. err = write_eraseblock(i);
  381. if (unlikely(err))
  382. goto out;
  383. if (i % 256 == 0)
  384. printk(PRINT_PREF "written up to eraseblock %u\n", i);
  385. cond_resched();
  386. }
  387. printk(PRINT_PREF "written %u eraseblocks\n", i);
  388. simple_srand(1);
  389. printk(PRINT_PREF "verifying all eraseblocks\n");
  390. for (i = 0; i < ebcnt; ++i) {
  391. if (bbt[i])
  392. continue;
  393. err = verify_eraseblock(i);
  394. if (unlikely(err))
  395. goto out;
  396. if (i % 256 == 0)
  397. printk(PRINT_PREF "verified up to eraseblock %u\n", i);
  398. cond_resched();
  399. }
  400. printk(PRINT_PREF "verified %u eraseblocks\n", i);
  401. err = erase_whole_device();
  402. if (err)
  403. goto out;
  404. err = verify_all_eraseblocks_ff();
  405. if (err)
  406. goto out;
  407. /* Write all eraseblocks */
  408. simple_srand(3);
  409. printk(PRINT_PREF "writing whole device\n");
  410. for (i = 0; i < ebcnt; ++i) {
  411. if (bbt[i])
  412. continue;
  413. err = write_eraseblock2(i);
  414. if (unlikely(err))
  415. goto out;
  416. if (i % 256 == 0)
  417. printk(PRINT_PREF "written up to eraseblock %u\n", i);
  418. cond_resched();
  419. }
  420. printk(PRINT_PREF "written %u eraseblocks\n", i);
  421. /* Check all eraseblocks */
  422. simple_srand(3);
  423. printk(PRINT_PREF "verifying all eraseblocks\n");
  424. for (i = 0; i < ebcnt; ++i) {
  425. if (bbt[i])
  426. continue;
  427. err = verify_eraseblock2(i);
  428. if (unlikely(err))
  429. goto out;
  430. if (i % 256 == 0)
  431. printk(PRINT_PREF "verified up to eraseblock %u\n", i);
  432. cond_resched();
  433. }
  434. printk(PRINT_PREF "verified %u eraseblocks\n", i);
  435. err = erase_whole_device();
  436. if (err)
  437. goto out;
  438. err = verify_all_eraseblocks_ff();
  439. if (err)
  440. goto out;
  441. printk(PRINT_PREF "finished with %d errors\n", errcnt);
  442. out:
  443. kfree(bbt);
  444. kfree(readbuf);
  445. kfree(writebuf);
  446. put_mtd_device(mtd);
  447. if (err)
  448. printk(PRINT_PREF "error %d occurred\n", err);
  449. printk(KERN_INFO "=================================================\n");
  450. return err;
  451. }
  452. module_init(mtd_subpagetest_init);
  453. static void __exit mtd_subpagetest_exit(void)
  454. {
  455. return;
  456. }
  457. module_exit(mtd_subpagetest_exit);
  458. MODULE_DESCRIPTION("Subpage test module");
  459. MODULE_AUTHOR("Adrian Hunter");
  460. MODULE_LICENSE("GPL");