nand_read.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * nand flash read
  3. */
  4. #define NFCONF (*(volatile unsigned int *)0x4e000000)
  5. #define rNFCONT (*(volatile unsigned int *)0x4E000004)
  6. #define NFCMD (*(volatile unsigned int *)0x4e000008)
  7. #define NFADDR (*(volatile unsigned char *)0x4e00000C)
  8. #define NFDATA (*(volatile unsigned char *)0x4e000010)
  9. #define NFSTAT (*(volatile unsigned char *)0x4e000020)
  10. #define BUSY 1
  11. #define NAND_SECTOR_SIZE 512
  12. #define NAND_BLOCK_MASK (NAND_SECTOR_SIZE - 1)
  13. void wait_idle(void) {
  14. int i;
  15. while (!(NFSTAT & BUSY)) {
  16. for(i=0; i<10; i++) {
  17. ;
  18. }
  19. }
  20. }
  21. /* low level nand read function */
  22. int nand_read_ll(unsigned char *buf, unsigned long start_addr, int size)
  23. {
  24. int i, j;
  25. /*
  26. * K9F5608UOC asks for 512B per page, and read/write operation must
  27. * do with page. Therefore, first judge whether start_addr and size
  28. * are valid.
  29. */
  30. if ((start_addr & NAND_BLOCK_MASK) || (size & NAND_BLOCK_MASK)) {
  31. return -1; /* invalid alignment */
  32. }
  33. /* chip Enable */
  34. NFCONF &= ~0x800;
  35. for (i=0; i<10; i++) {
  36. ;
  37. }
  38. for (i=start_addr; i < (start_addr + size); i+=NAND_SECTOR_SIZE) {
  39. NFCMD = 0;
  40. /* Write Address */
  41. NFADDR = i & 0xff;
  42. NFADDR = (i >> 9) & 0xff;
  43. NFADDR = (i >> 17) & 0xff;
  44. NFADDR = (i >> 25) & 0xff;
  45. wait_idle();
  46. for(j=0; j < NAND_SECTOR_SIZE; j++) {
  47. *buf++ = (NFDATA & 0xff);
  48. }
  49. }
  50. /* chip Disable */
  51. NFCONF |= 0x800; /* chip disable */
  52. return 0;
  53. }