cfi_mtd.c 5.6 KB

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