disk_access_nor.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2016 Actions Corporation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * Copyright (c) 2016 Intel Corporation.
  18. *
  19. * SPDX-License-Identifier: Apache-2.0
  20. */
  21. #include <string.h>
  22. #include <zephyr/types.h>
  23. #include <sys/__assert.h>
  24. #include <sys/util.h>
  25. #include <disk/disk_access.h>
  26. #include <errno.h>
  27. #include <init.h>
  28. #include <device.h>
  29. #include <drivers/flash.h>
  30. #include <partition/partition.h>
  31. #include <board_cfg.h>
  32. #define NOR_SECTOR_SIZE 512
  33. #define NOR_ERASE_SIZE 4096
  34. #define NOR_CACHE_NUM_SECTOR (NOR_ERASE_SIZE/NOR_SECTOR_SIZE)
  35. #define STA_DISK_OK 0x08 /* Medium OK in the drive */
  36. const struct device *nor_disk;
  37. /* lock to protect cache buf */
  38. static struct k_mutex n_mutex;
  39. #ifdef CONFIG_SOC_NO_PSRAM
  40. __in_section_unique(diskio.cache.pool)
  41. #endif
  42. static u8_t __aligned(4) nor_cache_data[NOR_ERASE_SIZE];
  43. static bool b_cache_valid, b_write_valid;
  44. static uint32_t nor_cache_start_sec;
  45. static int nor_disk_status(struct disk_info *disk)
  46. {
  47. if (!nor_disk) {
  48. return DISK_STATUS_NOMEDIA;
  49. }
  50. return DISK_STATUS_OK;
  51. }
  52. static int nor_disk_initialize(struct disk_info *disk)
  53. {
  54. if (nor_disk) {
  55. return 0;
  56. }
  57. return -ENODEV;
  58. }
  59. static int nor_cache_flush(void)
  60. {
  61. int ret = 0;
  62. if(b_write_valid){
  63. ret = flash_erase(nor_disk, nor_cache_start_sec<<9, NOR_ERASE_SIZE);
  64. ret |= flash_write(nor_disk, nor_cache_start_sec<<9, nor_cache_data, NOR_ERASE_SIZE);
  65. b_write_valid = false;
  66. //qprintk("nor flush off=0x%x\n", nor_cache_start_sec*512);
  67. }
  68. return ret;
  69. }
  70. static void nor_cache_buf_update(uint32_t off_sec)
  71. {
  72. if(off_sec & (NOR_CACHE_NUM_SECTOR-1)){
  73. printk("-----error, off_sec=0x%x not align to erase size-----\n", off_sec);
  74. return;
  75. }
  76. if(b_cache_valid){
  77. if(off_sec != nor_cache_start_sec){
  78. nor_cache_flush();
  79. nor_cache_start_sec = off_sec;
  80. flash_read(nor_disk, nor_cache_start_sec<<9, nor_cache_data, NOR_ERASE_SIZE);
  81. }
  82. }else{
  83. nor_cache_start_sec = off_sec;
  84. flash_read(nor_disk, nor_cache_start_sec<<9, nor_cache_data, NOR_ERASE_SIZE);
  85. b_cache_valid = true;
  86. }
  87. }
  88. static int nor_disk_read(struct disk_info *disk, uint8_t *buff, uint32_t start_sector,
  89. uint32_t sector_count)
  90. {
  91. int ret = 0;
  92. uint32_t nsec, off_sec, sec_index;
  93. if (!nor_disk ) {
  94. return -ENODEV;
  95. }
  96. if ((start_sector >= disk->sector_cnt) || (sector_count == 0)){
  97. return -EIO;
  98. }
  99. if (start_sector + sector_count > disk->sector_cnt) {
  100. sector_count = disk->sector_cnt - start_sector;
  101. }
  102. start_sector += disk->sector_offset;
  103. //printk("-nor disk read off=0x%x , sec=0x%x\n", start_sector , sector_count);
  104. k_mutex_lock(&n_mutex, K_FOREVER);
  105. if(b_cache_valid){
  106. off_sec = start_sector & (~(NOR_CACHE_NUM_SECTOR-1));
  107. if(off_sec == nor_cache_start_sec){
  108. sec_index = (start_sector & (NOR_CACHE_NUM_SECTOR-1));
  109. nsec = NOR_CACHE_NUM_SECTOR - sec_index;
  110. if(nsec > sector_count)
  111. nsec = sector_count;
  112. memcpy(buff, nor_cache_data+(sec_index<<9), nsec<<9);
  113. buff += nsec<<9;
  114. start_sector += nsec;
  115. sector_count -= nsec;
  116. }
  117. }
  118. if(sector_count){
  119. //nor_cache_flush();
  120. ret = flash_read(nor_disk, start_sector<<9, buff, sector_count<<9);
  121. }
  122. k_mutex_unlock(&n_mutex);
  123. if(ret)
  124. return -EIO;
  125. return 0;
  126. }
  127. static int nor_disk_write(struct disk_info *disk, const uint8_t *buff, uint32_t start_sector,
  128. uint32_t sector_count)
  129. {
  130. int ret = 0;
  131. uint32_t nsec, off_sec, sec_index;
  132. if (!nor_disk ) {
  133. return -ENODEV;
  134. }
  135. if ((start_sector >= disk->sector_cnt) || (sector_count == 0)){
  136. return -EIO;
  137. }
  138. if (start_sector + sector_count > disk->sector_cnt) {
  139. sector_count = disk->sector_cnt - start_sector;
  140. }
  141. start_sector += disk->sector_offset;
  142. //printk("-nor disk write off=0x%x , sec=0x%x\n", start_sector*512 , sector_count);
  143. k_mutex_lock(&n_mutex, K_FOREVER);
  144. sec_index = (start_sector & (NOR_CACHE_NUM_SECTOR-1));
  145. if(sec_index) { // offset not align to erase sector
  146. off_sec = start_sector & (~(NOR_CACHE_NUM_SECTOR-1)) ;
  147. nor_cache_buf_update(off_sec);
  148. nsec = NOR_CACHE_NUM_SECTOR - sec_index;
  149. if(nsec > sector_count)
  150. nsec = sector_count;
  151. memcpy(nor_cache_data+(sec_index<<9), buff, nsec<<9);
  152. buff += nsec<<9;
  153. start_sector += nsec;
  154. sector_count -= nsec;
  155. b_write_valid = true;
  156. //printk("head off=0x%x\n", nor_cache_start_sec*512);
  157. }
  158. if(sector_count >= NOR_CACHE_NUM_SECTOR){
  159. nsec = sector_count & (~(NOR_CACHE_NUM_SECTOR-1));
  160. ret = flash_erase(nor_disk, start_sector<<9, nsec<<9);
  161. ret |= flash_write(nor_disk, start_sector<<9, buff, nsec<<9);
  162. if(b_cache_valid && (start_sector <= nor_cache_start_sec) && (nor_cache_start_sec <= start_sector +nsec)){
  163. b_cache_valid = false;
  164. b_write_valid = false;
  165. printk("nor cache lost off=0x%x\n", nor_cache_start_sec*512);
  166. }
  167. buff += nsec<<9;
  168. start_sector += nsec;
  169. sector_count -= nsec;
  170. }
  171. if(sector_count){ // size not align to erase sector
  172. nor_cache_buf_update(start_sector);
  173. memcpy(nor_cache_data, buff, sector_count<<9);
  174. //printk("tail off=0x%x\n", nor_cache_start_sec*512);
  175. b_write_valid = true;
  176. }
  177. k_mutex_unlock(&n_mutex);
  178. if(ret)
  179. return -EIO;
  180. return 0;
  181. }
  182. static int nor_disk_ioctl(struct disk_info *disk, uint8_t cmd, void *buff)
  183. {
  184. int ret = 0;
  185. if (!nor_disk )
  186. return -ENODEV;
  187. switch (cmd) {
  188. case DISK_IOCTL_CTRL_SYNC:
  189. printk("-nor sync\n");
  190. k_mutex_lock(&n_mutex, K_FOREVER);
  191. nor_cache_flush();
  192. k_mutex_unlock(&n_mutex);
  193. break;
  194. case DISK_IOCTL_GET_SECTOR_COUNT:
  195. *(uint32_t *)buff = disk->sector_cnt;
  196. break;
  197. case DISK_IOCTL_GET_SECTOR_SIZE:
  198. *(uint32_t *)buff = NOR_SECTOR_SIZE;
  199. break;
  200. case DISK_IOCTL_GET_ERASE_BLOCK_SZ:
  201. *(uint32_t *)buff = NOR_SECTOR_SIZE;
  202. break;
  203. case DISK_IOCTL_HW_DETECT:
  204. *(uint8_t *)buff = STA_DISK_OK;
  205. break;
  206. default:
  207. return -EINVAL;
  208. }
  209. return ret;
  210. }
  211. struct disk_operations disk_nor_operation = {
  212. .init = nor_disk_initialize,
  213. .status = nor_disk_status,
  214. .read = nor_disk_read,
  215. .write = nor_disk_write,
  216. .ioctl = nor_disk_ioctl,
  217. };
  218. struct disk_info disk_nor_mass = {
  219. .name = "NOR",
  220. .sector_size = NOR_SECTOR_SIZE,
  221. .sector_offset = 0,
  222. .sector_cnt = 0,
  223. .ops = &disk_nor_operation,
  224. };
  225. static int nor_disk_init(const struct device *dev)
  226. {
  227. const struct partition_entry *parti;
  228. ARG_UNUSED(dev);
  229. k_mutex_init(&n_mutex);
  230. b_write_valid = b_cache_valid = false;
  231. nor_disk = NULL;
  232. parti = partition_get_stf_part(STORAGE_ID_DATA_NOR, PARTITION_FILE_ID_UDISK);
  233. if(parti == NULL){
  234. printk("nor disk:ext nor\n");
  235. parti = partition_get_stf_part(STORAGE_ID_NOR, PARTITION_FILE_ID_UDISK);
  236. if(parti)
  237. nor_disk = device_get_binding(CONFIG_SPI_FLASH_NAME);
  238. }else{
  239. printk("nor disk:data nor\n");
  240. nor_disk = device_get_binding(CONFIG_SPI_FLASH_2_NAME);
  241. }
  242. if (!nor_disk) {
  243. printk("nor disk: not nor dev\n");
  244. return -ENODEV;
  245. }
  246. if(parti != NULL){
  247. disk_nor_mass.sector_offset = parti->offset >> 9;
  248. disk_nor_mass.sector_cnt = parti->size >> 9;
  249. }
  250. printk("nor_disk_init,size=0x%x, off=0x%x secotr\n", disk_nor_mass.sector_cnt, disk_nor_mass.sector_offset);
  251. disk_access_register(&disk_nor_mass);
  252. return 0;
  253. }
  254. SYS_INIT(nor_disk_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);