flashkw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * sheevaplug nand flash driver
  3. *
  4. * for now separate from (inferno's) os/port/flashnand.c because the flash
  5. * seems newer, and has different commands, but that is nand-chip specific,
  6. * not sheevaplug-specific. they should be merged in future.
  7. *
  8. * the sheevaplug has a hynix 4gbit flash chip: hy27uf084g2m.
  9. * 2048 byte pages, with 64 spare bytes each; erase block size is 128k.
  10. *
  11. * it has a "glueless" interface, at 0xf9000000. that's the address
  12. * of the data register. the command and address registers are those
  13. * or'ed with 1 and 2 respectively.
  14. *
  15. * linux uses this layout for the nand flash (from address 0 onwards):
  16. * 1mb for u-boot
  17. * 4mb for kernel
  18. * 507mb for file system
  19. *
  20. * this is not so relevant here except for ecc. the first two areas
  21. * (u-boot and kernel) are expected to have 4-bit ecc per 512 bytes
  22. * (but calculated from last byte to first), bad erase blocks skipped.
  23. * the file system area has 1-bit ecc per 256 bytes.
  24. */
  25. #include "u.h"
  26. #include "../port/lib.h"
  27. #include "mem.h"
  28. #include "dat.h"
  29. #include "fns.h"
  30. #include "io.h"
  31. #include "../port/error.h"
  32. #include "../port/flashif.h"
  33. #include "../port/nandecc.h"
  34. #define NANDFREG ((Nandreg*)AddrNandf)
  35. enum {
  36. Debug = 0,
  37. /* vendors */
  38. Hynix = 0xad,
  39. Samsung = 0xec,
  40. /* chips */
  41. Hy27UF084G2M = 0xdc,
  42. NandActCEBoot = 1<<1,
  43. };
  44. typedef struct Nandreg Nandreg;
  45. typedef struct Nandtab Nandtab;
  46. typedef struct Cache Cache;
  47. struct Nandreg { /* hw registers */
  48. ulong rdparms;
  49. ulong wrparms;
  50. uchar _pad0[0x70 - 0x20];
  51. ulong ctl;
  52. };
  53. struct Nandtab {
  54. int vid;
  55. int did;
  56. vlong size;
  57. char* name;
  58. };
  59. struct Cache {
  60. Flash *flif;
  61. ulong pageno;
  62. ulong pgsize; /* r->pagesize */
  63. char *page; /* of pgsize bytes */
  64. };
  65. enum {
  66. /* commands */
  67. Readstatus = 0x70,
  68. Readid = 0x90, /* needs 1 0-address write */
  69. Resetf = 0xff,
  70. /*
  71. * needs 5 address writes followed by Readstart,
  72. * Readstartcache or Restartcopy.
  73. */
  74. Read = 0x00,
  75. Readstart = 0x30,
  76. Readstartcache = 0x31,
  77. Readstartcopy = 0x35,
  78. /* after Readstartcache, to stop reading next pages */
  79. Readstopcache = 0x34,
  80. /* needs 5 address writes, the data, and -start or -cache */
  81. Program = 0x80,
  82. Programstart = 0x10,
  83. Programcache = 0x15,
  84. Copyback = 0x85, /* followed by Programstart */
  85. /* 3 address writes for block followed by Erasestart */
  86. Erase = 0x60,
  87. Erasestart = 0xd0,
  88. Randomread = 0x85,
  89. Randomwrite = 0x05,
  90. Randomwritestart= 0xe0,
  91. /* status bits */
  92. SFail = 1<<0,
  93. SCachefail = 1<<1,
  94. SIdle = 1<<5, /* doesn't seem to come on ever */
  95. SReady = 1<<6,
  96. SNotprotected = 1<<7,
  97. Srdymask = SReady, /* was SIdle|SReady */
  98. };
  99. Nandtab nandtab[] = {
  100. {Hynix, Hy27UF084G2M, 512*MB, "Hy27UF084G2M"},
  101. {Samsung, 0xdc, 512*MB, "Samsung 2Gb"},
  102. };
  103. static Cache cache;
  104. static void
  105. nandcmd(Flash *f, uchar b)
  106. {
  107. uchar *p = (uchar *)((ulong)f->addr|1);
  108. *p = b;
  109. coherence();
  110. }
  111. static void
  112. nandaddr(Flash *f, uchar b)
  113. {
  114. uchar *p = (uchar *)((ulong)f->addr|2);
  115. *p = b;
  116. coherence();
  117. }
  118. static uchar
  119. nandread(Flash *f)
  120. {
  121. return *(uchar *)f->addr;
  122. }
  123. static void
  124. nandreadn(Flash *f, uchar *buf, long n)
  125. {
  126. uchar *p = f->addr;
  127. while(n-- > 0)
  128. *buf++ = *p;
  129. }
  130. static void
  131. nandwrite(Flash *f, uchar b)
  132. {
  133. *(uchar *)f->addr = b;
  134. coherence();
  135. }
  136. static void
  137. nandwriten(Flash *f, uchar *buf, long n)
  138. {
  139. uchar *p = f->addr;
  140. while(n-- > 0)
  141. *p = *buf++;
  142. coherence();
  143. }
  144. static void
  145. nandclaim(Flash*)
  146. {
  147. NANDFREG->ctl |= NandActCEBoot;
  148. coherence();
  149. }
  150. static void
  151. nandunclaim(Flash*)
  152. {
  153. NANDFREG->ctl &= ~NandActCEBoot;
  154. coherence();
  155. }
  156. void mmuidmap(uintptr phys, int mbs);
  157. Nandtab *
  158. findflash(Flash *f, uintptr pa, uchar *id4p)
  159. {
  160. int i;
  161. ulong sts;
  162. uchar maker, device, id3, id4;
  163. Nandtab *chip;
  164. mmuidmap(pa, 16);
  165. f->addr = (void *)pa;
  166. /* make sure controller is idle */
  167. nandclaim(f);
  168. nandcmd(f, Resetf);
  169. nandunclaim(f);
  170. nandclaim(f);
  171. nandcmd(f, Readstatus);
  172. sts = nandread(f);
  173. nandunclaim(f);
  174. for (i = 10; i > 0 && !(sts & SReady); i--) {
  175. delay(50);
  176. nandclaim(f);
  177. nandcmd(f, Readstatus);
  178. sts = nandread(f);
  179. nandunclaim(f);
  180. }
  181. if(!(sts & SReady))
  182. return nil;
  183. nandclaim(f);
  184. nandcmd(f, Readid);
  185. nandaddr(f, 0);
  186. maker = nandread(f);
  187. device = nandread(f);
  188. id3 = nandread(f);
  189. USED(id3);
  190. id4 = nandread(f);
  191. nandunclaim(f);
  192. if (id4p)
  193. *id4p = id4;
  194. for(i = 0; i < nelem(nandtab); i++) {
  195. chip = &nandtab[i];
  196. if(chip->vid == maker && chip->did == device)
  197. return chip;
  198. }
  199. return nil;
  200. }
  201. int
  202. flashat(Flash *f, uintptr pa)
  203. {
  204. return findflash(f, pa, nil) != nil;
  205. }
  206. static int
  207. idchip(Flash *f)
  208. {
  209. uchar id4;
  210. Flashregion *r;
  211. Nandtab *chip;
  212. static int blocksizes[4] = { 64*1024, 128*1024, 256*1024, 0 };
  213. static int pagesizes[4] = { 1024, 2*1024, 0, 0 };
  214. static int spares[2] = { 8, 16 }; /* per 512 bytes */
  215. f->id = 0;
  216. f->devid = 0;
  217. f->width = 1;
  218. chip = findflash(f, (uintptr)f->addr, &id4);
  219. if (chip == nil)
  220. return -1;
  221. f->id = chip->vid;
  222. f->devid = chip->did;
  223. f->size = chip->size;
  224. f->width = 1;
  225. f->nr = 1;
  226. r = &f->regions[0];
  227. r->pagesize = pagesizes[id4 & MASK(2)];
  228. r->erasesize = blocksizes[(id4 >> 4) & MASK(2)];
  229. if (r->pagesize == 0 || r->erasesize == 0) {
  230. iprint("flashkw: bogus flash sizes\n");
  231. return -1;
  232. }
  233. r->n = f->size / r->erasesize;
  234. r->start = 0;
  235. r->end = f->size;
  236. if (cache.page == nil) {
  237. cache.pgsize = r->pagesize;
  238. cache.page = smalloc(r->pagesize);
  239. }
  240. r->spares = r->pagesize / 512 * spares[(id4 >> 2) & 1];
  241. print("#F0: kwnand: %s %,lud bytes pagesize %lud erasesize %,lud"
  242. " spares per page %lud\n", chip->name, f->size, r->pagesize,
  243. r->erasesize, r->spares);
  244. return 0;
  245. }
  246. static int
  247. ctlrwait(Flash *f)
  248. {
  249. int sts, cnt;
  250. nandclaim(f);
  251. for (;;) {
  252. nandcmd(f, Readstatus);
  253. for(cnt = 100; cnt > 0 && (nandread(f) & Srdymask) != Srdymask;
  254. cnt--)
  255. microdelay(50);
  256. nandcmd(f, Readstatus);
  257. sts = nandread(f);
  258. if((sts & Srdymask) == Srdymask)
  259. break;
  260. print("flashkw: flash ctlr busy, sts %#ux: resetting\n", sts);
  261. nandcmd(f, Resetf);
  262. }
  263. nandunclaim(f);
  264. return 0;
  265. }
  266. static int
  267. erasezone(Flash *f, Flashregion *r, ulong offset)
  268. {
  269. int i;
  270. ulong page, block;
  271. uchar s;
  272. if (Debug) {
  273. print("flashkw: erasezone: offset %#lux, region nblocks %d,"
  274. " start %#lux, end %#lux\n", offset, r->n, r->start,
  275. r->end);
  276. print(" erasesize %lud, pagesize %lud\n",
  277. r->erasesize, r->pagesize);
  278. }
  279. assert(r->erasesize != 0);
  280. if(offset & (r->erasesize - 1)) {
  281. print("flashkw: erase offset %lud not block aligned\n", offset);
  282. return -1;
  283. }
  284. page = offset >> 11;
  285. block = page >> 6;
  286. if (Debug)
  287. print("flashkw: erase: block %#lux\n", block);
  288. /* make sure controller is idle */
  289. if(ctlrwait(f) < 0) {
  290. print("flashkw: erase: flash busy\n");
  291. return -1;
  292. }
  293. /* start erasing */
  294. nandclaim(f);
  295. nandcmd(f, Erase);
  296. nandaddr(f, page>>0);
  297. nandaddr(f, page>>8);
  298. nandaddr(f, page>>16);
  299. nandcmd(f, Erasestart);
  300. /* have to wait until flash is done. typically ~2ms */
  301. delay(1);
  302. nandcmd(f, Readstatus);
  303. for(i = 0; i < 100; i++) {
  304. s = nandread(f);
  305. if(s & SReady) {
  306. nandunclaim(f);
  307. if(s & SFail) {
  308. print("flashkw: erase: failed, block %#lux\n",
  309. block);
  310. return -1;
  311. }
  312. return 0;
  313. }
  314. microdelay(50);
  315. }
  316. print("flashkw: erase timeout, block %#lux\n", block);
  317. nandunclaim(f);
  318. return -1;
  319. }
  320. static void
  321. flcachepage(Flash *f, ulong page, uchar *buf)
  322. {
  323. Flashregion *r = &f->regions[0];
  324. assert(cache.pgsize == r->pagesize);
  325. cache.flif = f;
  326. cache.pageno = page;
  327. /* permit i/o directly to or from the cache */
  328. if (buf != (uchar *)cache.page)
  329. memmove(cache.page, buf, cache.pgsize);
  330. }
  331. static int
  332. write1page(Flash *f, ulong offset, void *buf)
  333. {
  334. int i;
  335. ulong page, v;
  336. uchar s;
  337. uchar *eccp, *p;
  338. Flashregion *r = &f->regions[0];
  339. static uchar *oob;
  340. if (oob == nil)
  341. oob = smalloc(r->spares);
  342. page = offset >> 11;
  343. if (Debug)
  344. print("flashkw: write nand offset %#lux page %#lux\n",
  345. offset, page);
  346. if(offset & (r->pagesize - 1)) {
  347. print("flashkw: write offset %lud not page aligned\n", offset);
  348. return -1;
  349. }
  350. p = buf;
  351. memset(oob, 0xff, r->spares);
  352. assert(r->spares >= 24);
  353. eccp = oob + r->spares - 24;
  354. for(i = 0; i < r->pagesize / 256; i++) {
  355. v = nandecc(p);
  356. *eccp++ = v>>8;
  357. *eccp++ = v>>0;
  358. *eccp++ = v>>16;
  359. p += 256;
  360. }
  361. if(ctlrwait(f) < 0) {
  362. print("flashkw: write: nand not ready & idle\n");
  363. return -1;
  364. }
  365. /* write, only whole pages for now, no sub-pages */
  366. nandclaim(f);
  367. nandcmd(f, Program);
  368. nandaddr(f, 0);
  369. nandaddr(f, 0);
  370. nandaddr(f, page>>0);
  371. nandaddr(f, page>>8);
  372. nandaddr(f, page>>16);
  373. nandwriten(f, buf, r->pagesize);
  374. nandwriten(f, oob, r->spares);
  375. nandcmd(f, Programstart);
  376. microdelay(100);
  377. nandcmd(f, Readstatus);
  378. for(i = 0; i < 100; i++) {
  379. s = nandread(f);
  380. if(s & SReady) {
  381. nandunclaim(f);
  382. if(s & SFail) {
  383. print("flashkw: write failed, page %#lux\n",
  384. page);
  385. return -1;
  386. }
  387. return 0;
  388. }
  389. microdelay(10);
  390. }
  391. nandunclaim(f);
  392. flcachepage(f, page, buf);
  393. print("flashkw: write timeout for page %#lux\n", page);
  394. return -1;
  395. }
  396. static int
  397. read1page(Flash *f, ulong offset, void *buf)
  398. {
  399. int i;
  400. ulong addr, page, w;
  401. uchar *eccp, *p;
  402. Flashregion *r = &f->regions[0];
  403. static uchar *oob;
  404. if (oob == nil)
  405. oob = smalloc(r->spares);
  406. assert(r->pagesize != 0);
  407. addr = offset & (r->pagesize - 1);
  408. page = offset >> 11;
  409. if(addr != 0) {
  410. print("flashkw: read1page: read addr %#lux:"
  411. " must read aligned page\n", addr);
  412. return -1;
  413. }
  414. /* satisfy request from cache if possible */
  415. if (f == cache.flif && page == cache.pageno &&
  416. r->pagesize == cache.pgsize) {
  417. memmove(buf, cache.page, r->pagesize);
  418. return 0;
  419. }
  420. if (Debug)
  421. print("flashkw: read offset %#lux addr %#lux page %#lux\n",
  422. offset, addr, page);
  423. nandclaim(f);
  424. nandcmd(f, Read);
  425. nandaddr(f, addr>>0);
  426. nandaddr(f, addr>>8);
  427. nandaddr(f, page>>0);
  428. nandaddr(f, page>>8);
  429. nandaddr(f, page>>16);
  430. nandcmd(f, Readstart);
  431. microdelay(50);
  432. nandreadn(f, buf, r->pagesize);
  433. nandreadn(f, oob, r->spares);
  434. nandunclaim(f);
  435. flcachepage(f, page, buf);
  436. /* verify/correct data. last 8*3 bytes is ecc, per 256 bytes. */
  437. p = buf;
  438. assert(r->spares >= 24);
  439. eccp = oob + r->spares - 24;
  440. for(i = 0; i < r->pagesize / 256; i++) {
  441. w = eccp[0] << 8 | eccp[1] << 0 | eccp[2] << 16;
  442. eccp += 3;
  443. switch(nandecccorrect(p, nandecc(p), &w, 1)) {
  444. case NandEccErrorBad:
  445. print("(page %d)\n", i);
  446. return -1;
  447. case NandEccErrorOneBit:
  448. case NandEccErrorOneBitInEcc:
  449. print("(page %d)\n", i);
  450. /* fall through */
  451. case NandEccErrorGood:
  452. break;
  453. }
  454. p += 256;
  455. }
  456. return 0;
  457. }
  458. /*
  459. * read a page at offset into cache, copy fragment from buf into it
  460. * at pagoff, and rewrite that page.
  461. */
  462. static int
  463. rewrite(Flash *f, ulong offset, ulong pagoff, void *buf, ulong size)
  464. {
  465. if (read1page(f, offset, cache.page) < 0)
  466. return -1;
  467. memmove(&cache.page[pagoff], buf, size);
  468. return write1page(f, offset, cache.page);
  469. }
  470. /* there are no alignment constraints on offset, buf, nor n */
  471. static int
  472. write(Flash *f, ulong offset, void *buf, long n)
  473. {
  474. uint un, frag, pagoff;
  475. ulong pgsize;
  476. uchar *p;
  477. Flashregion *r = &f->regions[0];
  478. if(n <= 0)
  479. panic("flashkw: write: non-positive count %ld", n);
  480. un = n;
  481. assert(r->pagesize != 0);
  482. pgsize = r->pagesize;
  483. /* if a partial first page exists, update the first page with it. */
  484. p = buf;
  485. pagoff = offset % pgsize;
  486. if (pagoff != 0) {
  487. frag = pgsize - pagoff;
  488. if (frag > un) /* might not extend to end of page */
  489. frag = un;
  490. if (rewrite(f, offset - pagoff, pagoff, p, frag) < 0)
  491. return -1;
  492. offset += frag;
  493. p += frag;
  494. un -= frag;
  495. }
  496. /* copy whole pages */
  497. while (un >= pgsize) {
  498. if (write1page(f, offset, p) < 0)
  499. return -1;
  500. offset += pgsize;
  501. p += pgsize;
  502. un -= pgsize;
  503. }
  504. /* if a partial last page exists, update the last page with it. */
  505. if (un > 0)
  506. return rewrite(f, offset, 0, p, un);
  507. return 0;
  508. }
  509. /* there are no alignment constraints on offset, buf, nor n */
  510. static int
  511. read(Flash *f, ulong offset, void *buf, long n)
  512. {
  513. uint un, frag, pagoff;
  514. ulong pgsize;
  515. uchar *p;
  516. Flashregion *r = &f->regions[0];
  517. if(n <= 0)
  518. panic("flashkw: read: non-positive count %ld", n);
  519. un = n;
  520. assert(r->pagesize != 0);
  521. pgsize = r->pagesize;
  522. /* if partial 1st page, read it into cache & copy fragment to buf */
  523. p = buf;
  524. pagoff = offset % pgsize;
  525. if (pagoff != 0) {
  526. frag = pgsize - pagoff;
  527. if (frag > un) /* might not extend to end of page */
  528. frag = un;
  529. if (read1page(f, offset - pagoff, cache.page) < 0)
  530. return -1;
  531. offset += frag;
  532. memmove(p, &cache.page[pagoff], frag);
  533. p += frag;
  534. un -= frag;
  535. }
  536. /* copy whole pages */
  537. while (un >= pgsize) {
  538. if (read1page(f, offset, p) < 0)
  539. return -1;
  540. offset += pgsize;
  541. p += pgsize;
  542. un -= pgsize;
  543. }
  544. /* if partial last page, read into cache & copy initial fragment to buf */
  545. if (un > 0) {
  546. if (read1page(f, offset, cache.page) < 0)
  547. return -1;
  548. memmove(p, cache.page, un);
  549. }
  550. return 0;
  551. }
  552. static int
  553. reset(Flash *f)
  554. {
  555. if(f->data != nil)
  556. return 1;
  557. f->write = write;
  558. f->read = read;
  559. f->eraseall = nil;
  560. f->erasezone = erasezone;
  561. f->suspend = nil;
  562. f->resume = nil;
  563. f->sort = "nand";
  564. return idchip(f);
  565. }
  566. void
  567. flashkwlink(void)
  568. {
  569. addflashcard("nand", reset);
  570. }