cfi_mtd.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <dma.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. if (dma_memcpy(buf, f, len) < 0)
  62. memcpy(buf, f, len);
  63. *retlen = len;
  64. return 0;
  65. }
  66. static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  67. size_t *retlen, const u_char *buf)
  68. {
  69. flash_info_t *fi = mtd->priv;
  70. u_long t = fi->start[0] + to;
  71. int error;
  72. flash_set_verbose(0);
  73. error = write_buff(fi, (u_char*)buf, t, len);
  74. flash_set_verbose(1);
  75. if (!error) {
  76. *retlen = len;
  77. return 0;
  78. }
  79. return -EIO;
  80. }
  81. static void cfi_mtd_sync(struct mtd_info *mtd)
  82. {
  83. /*
  84. * This function should wait until all pending operations
  85. * finish. However this driver is fully synchronous, so
  86. * this function returns immediately
  87. */
  88. }
  89. static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  90. {
  91. flash_info_t *fi = mtd->priv;
  92. flash_set_verbose(0);
  93. flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs,
  94. fi->start[0] + ofs + len - 1, fi);
  95. flash_set_verbose(1);
  96. return 0;
  97. }
  98. static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  99. {
  100. flash_info_t *fi = mtd->priv;
  101. flash_set_verbose(0);
  102. flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs,
  103. fi->start[0] + ofs + len - 1, fi);
  104. flash_set_verbose(1);
  105. return 0;
  106. }
  107. static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
  108. {
  109. int sect_size = 0;
  110. int sect_size_old = 0;
  111. int sect;
  112. int regions = 0;
  113. int numblocks = 0;
  114. ulong offset;
  115. ulong base_addr;
  116. /*
  117. * First detect the number of eraseregions so that we can allocate
  118. * the array of eraseregions correctly
  119. */
  120. for (sect = 0; sect < fi->sector_count; sect++) {
  121. if (sect_size_old != flash_sector_size(fi, sect))
  122. regions++;
  123. sect_size_old = flash_sector_size(fi, sect);
  124. }
  125. switch (regions) {
  126. case 0:
  127. return 1;
  128. case 1: /* flash has uniform erase size */
  129. mtd->numeraseregions = 0;
  130. mtd->erasesize = sect_size_old;
  131. return 0;
  132. }
  133. mtd->numeraseregions = regions;
  134. mtd->eraseregions = malloc(sizeof(struct mtd_erase_region_info) * regions);
  135. /*
  136. * Now detect the largest sector and fill the eraseregions
  137. */
  138. regions = 0;
  139. base_addr = offset = fi->start[0];
  140. sect_size_old = flash_sector_size(fi, 0);
  141. for (sect = 0; sect < fi->sector_count; sect++) {
  142. if (sect_size_old != flash_sector_size(fi, sect)) {
  143. mtd->eraseregions[regions].offset = offset - base_addr;
  144. mtd->eraseregions[regions].erasesize = sect_size_old;
  145. mtd->eraseregions[regions].numblocks = numblocks;
  146. /* Now start counting the next eraseregions */
  147. numblocks = 0;
  148. regions++;
  149. offset = fi->start[sect];
  150. }
  151. numblocks++;
  152. /*
  153. * Select the largest sector size as erasesize (e.g. for UBI)
  154. */
  155. if (flash_sector_size(fi, sect) > sect_size)
  156. sect_size = flash_sector_size(fi, sect);
  157. sect_size_old = flash_sector_size(fi, sect);
  158. }
  159. /*
  160. * Set the last region
  161. */
  162. mtd->eraseregions[regions].offset = offset - base_addr;
  163. mtd->eraseregions[regions].erasesize = sect_size_old;
  164. mtd->eraseregions[regions].numblocks = numblocks;
  165. mtd->erasesize = sect_size;
  166. return 0;
  167. }
  168. int cfi_mtd_init(void)
  169. {
  170. struct mtd_info *mtd;
  171. flash_info_t *fi;
  172. int error, i;
  173. #ifdef CONFIG_MTD_CONCAT
  174. int devices_found = 0;
  175. struct mtd_info *mtd_list[CONFIG_SYS_MAX_FLASH_BANKS];
  176. #endif
  177. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  178. fi = &flash_info[i];
  179. mtd = &cfi_mtd_info[i];
  180. memset(mtd, 0, sizeof(struct mtd_info));
  181. error = cfi_mtd_set_erasesize(mtd, fi);
  182. if (error)
  183. continue;
  184. sprintf(cfi_mtd_names[i], "nor%d", i);
  185. mtd->name = cfi_mtd_names[i];
  186. mtd->type = MTD_NORFLASH;
  187. mtd->flags = MTD_CAP_NORFLASH;
  188. mtd->size = fi->size;
  189. mtd->writesize = 1;
  190. mtd->writebufsize = mtd->writesize;
  191. mtd->_erase = cfi_mtd_erase;
  192. mtd->_read = cfi_mtd_read;
  193. mtd->_write = cfi_mtd_write;
  194. mtd->_sync = cfi_mtd_sync;
  195. mtd->_lock = cfi_mtd_lock;
  196. mtd->_unlock = cfi_mtd_unlock;
  197. mtd->priv = fi;
  198. if (add_mtd_device(mtd))
  199. return -ENOMEM;
  200. #ifdef CONFIG_MTD_CONCAT
  201. mtd_list[devices_found++] = mtd;
  202. #endif
  203. }
  204. #ifdef CONFIG_MTD_CONCAT
  205. if (devices_found > 1) {
  206. /*
  207. * We detected multiple devices. Concatenate them together.
  208. */
  209. sprintf(c_mtd_name, "nor%d", devices_found);
  210. mtd = mtd_concat_create(mtd_list, devices_found, c_mtd_name);
  211. if (mtd == NULL)
  212. return -ENXIO;
  213. if (add_mtd_device(mtd))
  214. return -ENOMEM;
  215. }
  216. #endif /* CONFIG_MTD_CONCAT */
  217. return 0;
  218. }