cfi_mtd.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * Written by: Piotr Ziecik <kosmo@semihalf.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <flash.h>
  10. #include <malloc.h>
  11. #include <linux/errno.h>
  12. #include <linux/mtd/mtd.h>
  13. #include <linux/mtd/concat.h>
  14. #include <mtd/cfi_flash.h>
  15. static struct mtd_info cfi_mtd_info[CFI_MAX_FLASH_BANKS];
  16. static char cfi_mtd_names[CFI_MAX_FLASH_BANKS][16];
  17. #ifdef CONFIG_MTD_CONCAT
  18. static char c_mtd_name[16];
  19. #endif
  20. static int cfi_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  21. {
  22. flash_info_t *fi = mtd->priv;
  23. size_t a_start = fi->start[0] + instr->addr;
  24. size_t a_end = a_start + instr->len;
  25. int s_first = -1;
  26. int s_last = -1;
  27. int error, sect;
  28. for (sect = 0; sect < fi->sector_count; sect++) {
  29. if (a_start == fi->start[sect])
  30. s_first = sect;
  31. if (sect < fi->sector_count - 1) {
  32. if (a_end == fi->start[sect + 1]) {
  33. s_last = sect;
  34. break;
  35. }
  36. } else {
  37. s_last = sect;
  38. break;
  39. }
  40. }
  41. if (s_first >= 0 && s_first <= s_last) {
  42. instr->state = MTD_ERASING;
  43. flash_set_verbose(0);
  44. error = flash_erase(fi, s_first, s_last);
  45. flash_set_verbose(1);
  46. if (error) {
  47. instr->state = MTD_ERASE_FAILED;
  48. return -EIO;
  49. }
  50. instr->state = MTD_ERASE_DONE;
  51. mtd_erase_callback(instr);
  52. return 0;
  53. }
  54. return -EINVAL;
  55. }
  56. static int cfi_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  57. size_t *retlen, u_char *buf)
  58. {
  59. flash_info_t *fi = mtd->priv;
  60. u_char *f = (u_char*)(fi->start[0]) + from;
  61. memcpy(buf, f, len);
  62. *retlen = len;
  63. return 0;
  64. }
  65. static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  66. size_t *retlen, const u_char *buf)
  67. {
  68. flash_info_t *fi = mtd->priv;
  69. u_long t = fi->start[0] + to;
  70. int error;
  71. flash_set_verbose(0);
  72. error = write_buff(fi, (u_char*)buf, t, len);
  73. flash_set_verbose(1);
  74. if (!error) {
  75. *retlen = len;
  76. return 0;
  77. }
  78. return -EIO;
  79. }
  80. static void cfi_mtd_sync(struct mtd_info *mtd)
  81. {
  82. /*
  83. * This function should wait until all pending operations
  84. * finish. However this driver is fully synchronous, so
  85. * this function returns immediately
  86. */
  87. }
  88. static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  89. {
  90. flash_info_t *fi = mtd->priv;
  91. flash_set_verbose(0);
  92. flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs,
  93. fi->start[0] + ofs + len - 1, fi);
  94. flash_set_verbose(1);
  95. return 0;
  96. }
  97. static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  98. {
  99. flash_info_t *fi = mtd->priv;
  100. flash_set_verbose(0);
  101. flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs,
  102. fi->start[0] + ofs + len - 1, fi);
  103. flash_set_verbose(1);
  104. return 0;
  105. }
  106. static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
  107. {
  108. int sect_size = 0;
  109. int sect_size_old = 0;
  110. int sect;
  111. int regions = 0;
  112. int numblocks = 0;
  113. ulong offset;
  114. ulong base_addr;
  115. /*
  116. * First detect the number of eraseregions so that we can allocate
  117. * the array of eraseregions correctly
  118. */
  119. for (sect = 0; sect < fi->sector_count; sect++) {
  120. if (sect_size_old != flash_sector_size(fi, sect))
  121. regions++;
  122. sect_size_old = flash_sector_size(fi, sect);
  123. }
  124. switch (regions) {
  125. case 0:
  126. return 1;
  127. case 1: /* flash has uniform erase size */
  128. mtd->numeraseregions = 0;
  129. mtd->erasesize = sect_size_old;
  130. return 0;
  131. }
  132. mtd->numeraseregions = regions;
  133. mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions);
  134. /*
  135. * Now detect the largest sector and fill the eraseregions
  136. */
  137. regions = 0;
  138. base_addr = offset = fi->start[0];
  139. sect_size_old = flash_sector_size(fi, 0);
  140. for (sect = 0; sect < fi->sector_count; sect++) {
  141. if (sect_size_old != flash_sector_size(fi, sect)) {
  142. mtd->eraseregions[regions].offset = offset - base_addr;
  143. mtd->eraseregions[regions].erasesize = sect_size_old;
  144. mtd->eraseregions[regions].numblocks = numblocks;
  145. /* Now start counting the next eraseregions */
  146. numblocks = 0;
  147. regions++;
  148. offset = fi->start[sect];
  149. }
  150. numblocks++;
  151. /*
  152. * Select the largest sector size as erasesize (e.g. for UBI)
  153. */
  154. if (flash_sector_size(fi, sect) > sect_size)
  155. sect_size = flash_sector_size(fi, sect);
  156. sect_size_old = flash_sector_size(fi, sect);
  157. }
  158. /*
  159. * Set the last region
  160. */
  161. mtd->eraseregions[regions].offset = offset - base_addr;
  162. mtd->eraseregions[regions].erasesize = sect_size_old;
  163. mtd->eraseregions[regions].numblocks = numblocks;
  164. mtd->erasesize = sect_size;
  165. return 0;
  166. }
  167. int cfi_mtd_init(void)
  168. {
  169. struct mtd_info *mtd;
  170. flash_info_t *fi;
  171. int error, i;
  172. #ifdef CONFIG_MTD_CONCAT
  173. int devices_found = 0;
  174. struct mtd_info *mtd_list[CONFIG_SYS_MAX_FLASH_BANKS];
  175. #endif
  176. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  177. fi = &flash_info[i];
  178. mtd = &cfi_mtd_info[i];
  179. memset(mtd, 0, sizeof(struct mtd_info));
  180. error = cfi_mtd_set_erasesize(mtd, fi);
  181. if (error)
  182. continue;
  183. sprintf(cfi_mtd_names[i], "nor%d", i);
  184. mtd->name = cfi_mtd_names[i];
  185. mtd->type = MTD_NORFLASH;
  186. mtd->flags = MTD_CAP_NORFLASH;
  187. mtd->size = fi->size;
  188. mtd->writesize = 1;
  189. mtd->writebufsize = mtd->writesize;
  190. mtd->_erase = cfi_mtd_erase;
  191. mtd->_read = cfi_mtd_read;
  192. mtd->_write = cfi_mtd_write;
  193. mtd->_sync = cfi_mtd_sync;
  194. mtd->_lock = cfi_mtd_lock;
  195. mtd->_unlock = cfi_mtd_unlock;
  196. mtd->priv = fi;
  197. if (add_mtd_device(mtd))
  198. return -ENOMEM;
  199. #ifdef CONFIG_MTD_CONCAT
  200. mtd_list[devices_found++] = mtd;
  201. #endif
  202. }
  203. #ifdef CONFIG_MTD_CONCAT
  204. if (devices_found > 1) {
  205. /*
  206. * We detected multiple devices. Concatenate them together.
  207. */
  208. sprintf(c_mtd_name, "nor%d", devices_found);
  209. mtd = mtd_concat_create(mtd_list, devices_found, c_mtd_name);
  210. if (mtd == NULL)
  211. return -ENXIO;
  212. if (add_mtd_device(mtd))
  213. return -ENOMEM;
  214. }
  215. #endif /* CONFIG_MTD_CONCAT */
  216. return 0;
  217. }