mtd_subpagetest.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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/sched.h>
  27. #define PRINT_PREF KERN_INFO "mtd_subpagetest: "
  28. static int dev;
  29. module_param(dev, int, S_IRUGO);
  30. MODULE_PARM_DESC(dev, "MTD device number to use");
  31. static struct mtd_info *mtd;
  32. static unsigned char *writebuf;
  33. static unsigned char *readbuf;
  34. static unsigned char *bbt;
  35. static int subpgsize;
  36. static int bufsize;
  37. static int ebcnt;
  38. static int pgcnt;
  39. static int errcnt;
  40. static unsigned long next = 1;
  41. static inline unsigned int simple_rand(void)
  42. {
  43. next = next * 1103515245 + 12345;
  44. return (unsigned int)((next / 65536) % 32768);
  45. }
  46. static inline void simple_srand(unsigned long seed)
  47. {
  48. next = seed;
  49. }
  50. static void set_random_data(unsigned char *buf, size_t len)
  51. {
  52. size_t i;
  53. for (i = 0; i < len; ++i)
  54. buf[i] = simple_rand();
  55. }
  56. static inline void clear_data(unsigned char *buf, size_t len)
  57. {
  58. memset(buf, 0, len);
  59. }
  60. static int erase_eraseblock(int ebnum)
  61. {
  62. int err;
  63. struct erase_info ei;
  64. loff_t addr = ebnum * mtd->erasesize;
  65. memset(&ei, 0, sizeof(struct erase_info));
  66. ei.mtd = mtd;
  67. ei.addr = addr;
  68. ei.len = mtd->erasesize;
  69. err = mtd->erase(mtd, &ei);
  70. if (err) {
  71. printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
  72. return err;
  73. }
  74. if (ei.state == MTD_ERASE_FAILED) {
  75. printk(PRINT_PREF "some erase error occurred at EB %d\n",
  76. ebnum);
  77. return -EIO;
  78. }
  79. return 0;
  80. }
  81. static int erase_whole_device(void)
  82. {
  83. int err;
  84. unsigned int i;
  85. printk(PRINT_PREF "erasing whole device\n");
  86. for (i = 0; i < ebcnt; ++i) {
  87. if (bbt[i])
  88. continue;
  89. err = erase_eraseblock(i);
  90. if (err)
  91. return err;
  92. cond_resched();
  93. }
  94. printk(PRINT_PREF "erased %u eraseblocks\n", i);
  95. return 0;
  96. }
  97. static int write_eraseblock(int ebnum)
  98. {
  99. size_t written = 0;
  100. int err = 0;
  101. loff_t addr = ebnum * mtd->erasesize;
  102. set_random_data(writebuf, subpgsize);
  103. err = mtd->write(mtd, addr, subpgsize, &written, writebuf);
  104. if (unlikely(err || written != subpgsize)) {
  105. printk(PRINT_PREF "error: write failed at %#llx\n",
  106. (long long)addr);
  107. if (written != subpgsize) {
  108. printk(PRINT_PREF " write size: %#x\n", subpgsize);
  109. printk(PRINT_PREF " written: %#zx\n", written);
  110. }
  111. return err ? err : -1;
  112. }
  113. addr += subpgsize;
  114. set_random_data(writebuf, subpgsize);
  115. err = mtd->write(mtd, addr, subpgsize, &written, writebuf);
  116. if (unlikely(err || written != subpgsize)) {
  117. printk(PRINT_PREF "error: write failed at %#llx\n",
  118. (long long)addr);
  119. if (written != subpgsize) {
  120. printk(PRINT_PREF " write size: %#x\n", subpgsize);
  121. printk(PRINT_PREF " written: %#zx\n", written);
  122. }
  123. return err ? err : -1;
  124. }
  125. return err;
  126. }
  127. static int write_eraseblock2(int ebnum)
  128. {
  129. size_t written = 0;
  130. int err = 0, k;
  131. loff_t addr = ebnum * mtd->erasesize;
  132. for (k = 1; k < 33; ++k) {
  133. if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
  134. break;
  135. set_random_data(writebuf, subpgsize * k);
  136. err = mtd->write(mtd, addr, subpgsize * k, &written, writebuf);
  137. if (unlikely(err || written != subpgsize * k)) {
  138. printk(PRINT_PREF "error: write failed at %#llx\n",
  139. (long long)addr);
  140. if (written != subpgsize) {
  141. printk(PRINT_PREF " write size: %#x\n",
  142. subpgsize * k);
  143. printk(PRINT_PREF " written: %#08zx\n",
  144. written);
  145. }
  146. return err ? err : -1;
  147. }
  148. addr += subpgsize * k;
  149. }
  150. return err;
  151. }
  152. static void print_subpage(unsigned char *p)
  153. {
  154. int i, j;
  155. for (i = 0; i < subpgsize; ) {
  156. for (j = 0; i < subpgsize && j < 32; ++i, ++j)
  157. printk("%02x", *p++);
  158. printk("\n");
  159. }
  160. }
  161. static int verify_eraseblock(int ebnum)
  162. {
  163. size_t read = 0;
  164. int err = 0;
  165. loff_t addr = ebnum * mtd->erasesize;
  166. set_random_data(writebuf, subpgsize);
  167. clear_data(readbuf, subpgsize);
  168. read = 0;
  169. err = mtd->read(mtd, addr, subpgsize, &read, readbuf);
  170. if (unlikely(err || read != subpgsize)) {
  171. if (err == -EUCLEAN && 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. read = 0;
  195. err = mtd->read(mtd, addr, subpgsize, &read, readbuf);
  196. if (unlikely(err || read != subpgsize)) {
  197. if (err == -EUCLEAN && read == subpgsize) {
  198. printk(PRINT_PREF "ECC correction at %#llx\n",
  199. (long long)addr);
  200. err = 0;
  201. } else {
  202. printk(PRINT_PREF "error: read failed at %#llx\n",
  203. (long long)addr);
  204. return err ? err : -1;
  205. }
  206. }
  207. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  208. printk(PRINT_PREF "error: verify failed at %#llx\n",
  209. (long long)addr);
  210. printk(PRINT_PREF "------------- written----------------\n");
  211. print_subpage(writebuf);
  212. printk(PRINT_PREF "------------- read ------------------\n");
  213. print_subpage(readbuf);
  214. printk(PRINT_PREF "-------------------------------------\n");
  215. errcnt += 1;
  216. }
  217. return err;
  218. }
  219. static int verify_eraseblock2(int ebnum)
  220. {
  221. size_t read = 0;
  222. int err = 0, k;
  223. loff_t addr = ebnum * mtd->erasesize;
  224. for (k = 1; k < 33; ++k) {
  225. if (addr + (subpgsize * k) > (ebnum + 1) * mtd->erasesize)
  226. break;
  227. set_random_data(writebuf, subpgsize * k);
  228. clear_data(readbuf, subpgsize * k);
  229. read = 0;
  230. err = mtd->read(mtd, addr, subpgsize * k, &read, readbuf);
  231. if (unlikely(err || read != subpgsize * k)) {
  232. if (err == -EUCLEAN && read == subpgsize * k) {
  233. printk(PRINT_PREF "ECC correction at %#llx\n",
  234. (long long)addr);
  235. err = 0;
  236. } else {
  237. printk(PRINT_PREF "error: read failed at "
  238. "%#llx\n", (long long)addr);
  239. return err ? err : -1;
  240. }
  241. }
  242. if (unlikely(memcmp(readbuf, writebuf, subpgsize * k))) {
  243. printk(PRINT_PREF "error: verify failed at %#llx\n",
  244. (long long)addr);
  245. errcnt += 1;
  246. }
  247. addr += subpgsize * k;
  248. }
  249. return err;
  250. }
  251. static int verify_eraseblock_ff(int ebnum)
  252. {
  253. uint32_t j;
  254. size_t read = 0;
  255. int err = 0;
  256. loff_t addr = ebnum * mtd->erasesize;
  257. memset(writebuf, 0xff, subpgsize);
  258. for (j = 0; j < mtd->erasesize / subpgsize; ++j) {
  259. clear_data(readbuf, subpgsize);
  260. read = 0;
  261. err = mtd->read(mtd, addr, subpgsize, &read, readbuf);
  262. if (unlikely(err || read != subpgsize)) {
  263. if (err == -EUCLEAN && read == subpgsize) {
  264. printk(PRINT_PREF "ECC correction at %#llx\n",
  265. (long long)addr);
  266. err = 0;
  267. } else {
  268. printk(PRINT_PREF "error: read failed at "
  269. "%#llx\n", (long long)addr);
  270. return err ? err : -1;
  271. }
  272. }
  273. if (unlikely(memcmp(readbuf, writebuf, subpgsize))) {
  274. printk(PRINT_PREF "error: verify 0xff failed at "
  275. "%#llx\n", (long long)addr);
  276. errcnt += 1;
  277. }
  278. addr += subpgsize;
  279. }
  280. return err;
  281. }
  282. static int verify_all_eraseblocks_ff(void)
  283. {
  284. int err;
  285. unsigned int i;
  286. printk(PRINT_PREF "verifying all eraseblocks for 0xff\n");
  287. for (i = 0; i < ebcnt; ++i) {
  288. if (bbt[i])
  289. continue;
  290. err = verify_eraseblock_ff(i);
  291. if (err)
  292. return err;
  293. if (i % 256 == 0)
  294. printk(PRINT_PREF "verified up to eraseblock %u\n", i);
  295. cond_resched();
  296. }
  297. printk(PRINT_PREF "verified %u eraseblocks\n", i);
  298. return 0;
  299. }
  300. static int is_block_bad(int ebnum)
  301. {
  302. loff_t addr = ebnum * mtd->erasesize;
  303. int ret;
  304. ret = mtd->block_isbad(mtd, addr);
  305. if (ret)
  306. printk(PRINT_PREF "block %d is bad\n", ebnum);
  307. return ret;
  308. }
  309. static int scan_for_bad_eraseblocks(void)
  310. {
  311. int i, bad = 0;
  312. bbt = kmalloc(ebcnt, GFP_KERNEL);
  313. if (!bbt) {
  314. printk(PRINT_PREF "error: cannot allocate memory\n");
  315. return -ENOMEM;
  316. }
  317. memset(bbt, 0 , ebcnt);
  318. printk(PRINT_PREF "scanning for bad eraseblocks\n");
  319. for (i = 0; i < ebcnt; ++i) {
  320. bbt[i] = is_block_bad(i) ? 1 : 0;
  321. if (bbt[i])
  322. bad += 1;
  323. cond_resched();
  324. }
  325. printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
  326. return 0;
  327. }
  328. static int __init mtd_subpagetest_init(void)
  329. {
  330. int err = 0;
  331. uint32_t i;
  332. uint64_t tmp;
  333. printk(KERN_INFO "\n");
  334. printk(KERN_INFO "=================================================\n");
  335. printk(PRINT_PREF "MTD device: %d\n", dev);
  336. mtd = get_mtd_device(NULL, dev);
  337. if (IS_ERR(mtd)) {
  338. err = PTR_ERR(mtd);
  339. printk(PRINT_PREF "error: cannot get MTD device\n");
  340. return err;
  341. }
  342. if (mtd->type != MTD_NANDFLASH) {
  343. printk(PRINT_PREF "this test requires NAND flash\n");
  344. goto out;
  345. }
  346. subpgsize = mtd->writesize >> mtd->subpage_sft;
  347. printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
  348. "page size %u, subpage size %u, count of eraseblocks %u, "
  349. "pages per eraseblock %u, OOB size %u\n",
  350. (unsigned long long)mtd->size, mtd->erasesize,
  351. mtd->writesize, subpgsize, ebcnt, pgcnt, mtd->oobsize);
  352. err = -ENOMEM;
  353. bufsize = subpgsize * 32;
  354. writebuf = kmalloc(bufsize, GFP_KERNEL);
  355. if (!writebuf) {
  356. printk(PRINT_PREF "error: cannot allocate memory\n");
  357. goto out;
  358. }
  359. readbuf = kmalloc(bufsize, GFP_KERNEL);
  360. if (!readbuf) {
  361. printk(PRINT_PREF "error: cannot allocate memory\n");
  362. goto out;
  363. }
  364. tmp = mtd->size;
  365. do_div(tmp, mtd->erasesize);
  366. ebcnt = tmp;
  367. pgcnt = mtd->erasesize / mtd->writesize;
  368. err = scan_for_bad_eraseblocks();
  369. if (err)
  370. goto out;
  371. err = erase_whole_device();
  372. if (err)
  373. goto out;
  374. printk(PRINT_PREF "writing whole device\n");
  375. simple_srand(1);
  376. for (i = 0; i < ebcnt; ++i) {
  377. if (bbt[i])
  378. continue;
  379. err = write_eraseblock(i);
  380. if (unlikely(err))
  381. goto out;
  382. if (i % 256 == 0)
  383. printk(PRINT_PREF "written up to eraseblock %u\n", i);
  384. cond_resched();
  385. }
  386. printk(PRINT_PREF "written %u eraseblocks\n", i);
  387. simple_srand(1);
  388. printk(PRINT_PREF "verifying all eraseblocks\n");
  389. for (i = 0; i < ebcnt; ++i) {
  390. if (bbt[i])
  391. continue;
  392. err = verify_eraseblock(i);
  393. if (unlikely(err))
  394. goto out;
  395. if (i % 256 == 0)
  396. printk(PRINT_PREF "verified up to eraseblock %u\n", i);
  397. cond_resched();
  398. }
  399. printk(PRINT_PREF "verified %u eraseblocks\n", i);
  400. err = erase_whole_device();
  401. if (err)
  402. goto out;
  403. err = verify_all_eraseblocks_ff();
  404. if (err)
  405. goto out;
  406. /* Write all eraseblocks */
  407. simple_srand(3);
  408. printk(PRINT_PREF "writing whole device\n");
  409. for (i = 0; i < ebcnt; ++i) {
  410. if (bbt[i])
  411. continue;
  412. err = write_eraseblock2(i);
  413. if (unlikely(err))
  414. goto out;
  415. if (i % 256 == 0)
  416. printk(PRINT_PREF "written up to eraseblock %u\n", i);
  417. cond_resched();
  418. }
  419. printk(PRINT_PREF "written %u eraseblocks\n", i);
  420. /* Check all eraseblocks */
  421. simple_srand(3);
  422. printk(PRINT_PREF "verifying all eraseblocks\n");
  423. for (i = 0; i < ebcnt; ++i) {
  424. if (bbt[i])
  425. continue;
  426. err = verify_eraseblock2(i);
  427. if (unlikely(err))
  428. goto out;
  429. if (i % 256 == 0)
  430. printk(PRINT_PREF "verified up to eraseblock %u\n", i);
  431. cond_resched();
  432. }
  433. printk(PRINT_PREF "verified %u eraseblocks\n", i);
  434. err = erase_whole_device();
  435. if (err)
  436. goto out;
  437. err = verify_all_eraseblocks_ff();
  438. if (err)
  439. goto out;
  440. printk(PRINT_PREF "finished with %d errors\n", errcnt);
  441. out:
  442. kfree(bbt);
  443. kfree(readbuf);
  444. kfree(writebuf);
  445. put_mtd_device(mtd);
  446. if (err)
  447. printk(PRINT_PREF "error %d occurred\n", err);
  448. printk(KERN_INFO "=================================================\n");
  449. return err;
  450. }
  451. module_init(mtd_subpagetest_init);
  452. static void __exit mtd_subpagetest_exit(void)
  453. {
  454. return;
  455. }
  456. module_exit(mtd_subpagetest_exit);
  457. MODULE_DESCRIPTION("Subpage test module");
  458. MODULE_AUTHOR("Adrian Hunter");
  459. MODULE_LICENSE("GPL");