docg4_spl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * SPL driver for Diskonchip G4 nand flash
  3. *
  4. * Copyright (C) 2013 Mike Dunn <mikedunn@newsguy.com>
  5. *
  6. * This file is released under the terms of GPL v2 and any later version.
  7. * See the file COPYING in the root directory of the source tree for details.
  8. *
  9. *
  10. * This driver basically mimics the load functionality of a typical IPL (initial
  11. * program loader) resident in the 2k NOR-like region of the docg4 that is
  12. * mapped to the reset vector. It allows the u-boot SPL to continue loading if
  13. * the IPL loads a fixed number of flash blocks that is insufficient to contain
  14. * the entire u-boot image. In this case, a concatenated spl + u-boot image is
  15. * written at the flash offset from which the IPL loads an image, and when the
  16. * IPL jumps to the SPL, the SPL resumes loading where the IPL left off. See
  17. * the palmtreo680 for an example.
  18. *
  19. * This driver assumes that the data was written to the flash using the device's
  20. * "reliable" mode, and also assumes that each 512 byte page is stored
  21. * redundantly in the subsequent page. This storage format is likely to be used
  22. * by all boards that boot from the docg4. The format compensates for the lack
  23. * of ecc in the IPL.
  24. *
  25. * Reliable mode reduces the capacity of a block by half, and the redundant
  26. * pages reduce it by half again. As a result, the normal 256k capacity of a
  27. * block is reduced to 64k for the purposes of the IPL/SPL.
  28. */
  29. #include <asm/io.h>
  30. #include <linux/mtd/docg4.h>
  31. /* forward declarations */
  32. static inline void write_nop(void __iomem *docptr);
  33. static int poll_status(void __iomem *docptr);
  34. static void write_addr(void __iomem *docptr, uint32_t docg4_addr);
  35. static void address_sequence(unsigned int g4_page, unsigned int g4_index,
  36. void __iomem *docptr);
  37. static int docg4_load_block_reliable(uint32_t flash_offset, void *dest_addr);
  38. int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
  39. {
  40. void *load_addr = dst;
  41. uint32_t flash_offset = offs;
  42. const unsigned int block_count =
  43. (size + DOCG4_BLOCK_CAPACITY_SPL - 1)
  44. / DOCG4_BLOCK_CAPACITY_SPL;
  45. int i;
  46. for (i = 0; i < block_count; i++) {
  47. int ret = docg4_load_block_reliable(flash_offset, load_addr);
  48. if (ret)
  49. return ret;
  50. load_addr += DOCG4_BLOCK_CAPACITY_SPL;
  51. flash_offset += DOCG4_BLOCK_SIZE;
  52. }
  53. return 0;
  54. }
  55. static inline void write_nop(void __iomem *docptr)
  56. {
  57. writew(0, docptr + DOC_NOP);
  58. }
  59. static int poll_status(void __iomem *docptr)
  60. {
  61. /*
  62. * Busy-wait for the FLASHREADY bit to be set in the FLASHCONTROL
  63. * register. Operations known to take a long time (e.g., block erase)
  64. * should sleep for a while before calling this.
  65. */
  66. uint8_t flash_status;
  67. /* hardware quirk requires reading twice initially */
  68. flash_status = readb(docptr + DOC_FLASHCONTROL);
  69. do {
  70. flash_status = readb(docptr + DOC_FLASHCONTROL);
  71. } while (!(flash_status & DOC_CTRL_FLASHREADY));
  72. return 0;
  73. }
  74. static void write_addr(void __iomem *docptr, uint32_t docg4_addr)
  75. {
  76. /* write the four address bytes packed in docg4_addr to the device */
  77. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  78. docg4_addr >>= 8;
  79. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  80. docg4_addr >>= 8;
  81. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  82. docg4_addr >>= 8;
  83. writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
  84. }
  85. static void address_sequence(unsigned int g4_page, unsigned int g4_index,
  86. void __iomem *docptr)
  87. {
  88. writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
  89. writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
  90. write_nop(docptr);
  91. write_addr(docptr, ((uint32_t)g4_page << 16) | g4_index);
  92. write_nop(docptr);
  93. }
  94. static int docg4_load_block_reliable(uint32_t flash_offset, void *dest_addr)
  95. {
  96. void __iomem *docptr = (void *)CONFIG_SYS_NAND_BASE;
  97. unsigned int g4_page = flash_offset >> 11; /* 2k page */
  98. const unsigned int last_g4_page = g4_page + 0x80; /* last in block */
  99. int g4_index = 0;
  100. uint16_t flash_status;
  101. uint16_t *buf;
  102. uint16_t discard, magic_high, magic_low;
  103. /* flash_offset must be aligned to the start of a block */
  104. if (flash_offset & 0x3ffff)
  105. return -1;
  106. writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
  107. writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
  108. write_nop(docptr);
  109. write_nop(docptr);
  110. poll_status(docptr);
  111. write_nop(docptr);
  112. writew(0x45, docptr + DOC_FLASHSEQUENCE);
  113. writew(0xa3, docptr + DOC_FLASHCOMMAND);
  114. write_nop(docptr);
  115. writew(0x22, docptr + DOC_FLASHCOMMAND);
  116. write_nop(docptr);
  117. /* read 1st 4 oob bytes of first subpage of block */
  118. address_sequence(g4_page, 0x0100, docptr); /* index at oob */
  119. write_nop(docptr);
  120. flash_status = readw(docptr + DOC_FLASHCONTROL);
  121. flash_status = readw(docptr + DOC_FLASHCONTROL);
  122. if (flash_status & 0x06) /* sequence or protection errors */
  123. return -1;
  124. writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
  125. write_nop(docptr);
  126. write_nop(docptr);
  127. poll_status(docptr);
  128. writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
  129. write_nop(docptr);
  130. write_nop(docptr);
  131. write_nop(docptr);
  132. write_nop(docptr);
  133. write_nop(docptr);
  134. /*
  135. * Here we read the first four oob bytes of the first page of the block.
  136. * The IPL on the palmtreo680 requires that this contain a 32 bit magic
  137. * number, or the load aborts. We'll ignore it.
  138. */
  139. discard = readw(docptr + 0x103c); /* hw quirk; 1st read discarded */
  140. magic_low = readw(docptr + 0x103c);
  141. magic_high = readw(docptr + DOCG4_MYSTERY_REG);
  142. writew(0, docptr + DOC_DATAEND);
  143. write_nop(docptr);
  144. write_nop(docptr);
  145. /* load contents of block to memory */
  146. buf = (uint16_t *)dest_addr;
  147. do {
  148. int i;
  149. address_sequence(g4_page, g4_index, docptr);
  150. writew(DOCG4_CMD_READ2,
  151. docptr + DOC_FLASHCOMMAND);
  152. write_nop(docptr);
  153. write_nop(docptr);
  154. poll_status(docptr);
  155. writew(DOC_ECCCONF0_READ_MODE |
  156. DOC_ECCCONF0_ECC_ENABLE |
  157. DOCG4_BCH_SIZE,
  158. docptr + DOC_ECCCONF0);
  159. write_nop(docptr);
  160. write_nop(docptr);
  161. write_nop(docptr);
  162. write_nop(docptr);
  163. write_nop(docptr);
  164. /* read the 512 bytes of page data, 2 bytes at a time */
  165. discard = readw(docptr + 0x103c);
  166. for (i = 0; i < 256; i++)
  167. *buf++ = readw(docptr + 0x103c);
  168. /* read oob, but discard it */
  169. for (i = 0; i < 7; i++)
  170. discard = readw(docptr + 0x103c);
  171. discard = readw(docptr + DOCG4_OOB_6_7);
  172. discard = readw(docptr + DOCG4_OOB_6_7);
  173. writew(0, docptr + DOC_DATAEND);
  174. write_nop(docptr);
  175. write_nop(docptr);
  176. if (!(g4_index & 0x100)) {
  177. /* not redundant subpage read; check for ecc error */
  178. write_nop(docptr);
  179. flash_status = readw(docptr + DOC_ECCCONF1);
  180. flash_status = readw(docptr + DOC_ECCCONF1);
  181. if (flash_status & 0x80) { /* ecc error */
  182. g4_index += 0x108; /* read redundant subpage */
  183. buf -= 256; /* back up ram ptr */
  184. continue;
  185. } else /* no ecc error */
  186. g4_index += 0x210; /* skip redundant subpage */
  187. } else /* redundant page was just read; skip ecc error check */
  188. g4_index += 0x108;
  189. if (g4_index == 0x420) { /* finished with 2k page */
  190. g4_index = 0;
  191. g4_page += 2; /* odd-numbered 2k pages skipped */
  192. }
  193. } while (g4_page != last_g4_page); /* while still on same block */
  194. return 0;
  195. }