Lines Matching full:ctx
53 static inline bool flashdisk_with_erase(const struct flashdisk_data *ctx) in flashdisk_with_erase() argument
55 ARG_UNUSED(ctx); in flashdisk_with_erase()
58 return ctx->erase_required; in flashdisk_with_erase()
66 static inline void flashdisk_probe_erase(struct flashdisk_data *ctx) in flashdisk_probe_erase() argument
69 ctx->erase_required = in flashdisk_probe_erase()
70 flash_params_get_erase_cap(flash_get_parameters(ctx->info.dev)) & in flashdisk_probe_erase()
73 ARG_UNUSED(ctx); in flashdisk_probe_erase()
87 static int flashdisk_init_runtime(struct flashdisk_data *ctx, in flashdisk_init_runtime() argument
94 flashdisk_probe_erase(ctx); in flashdisk_init_runtime()
96 if (IS_ENABLED(CONFIG_FLASHDISK_VERIFY_PAGE_LAYOUT) && flashdisk_with_erase(ctx)) { in flashdisk_init_runtime()
97 rc = flash_get_page_info_by_offs(ctx->info.dev, ctx->offset, &page); in flashdisk_init_runtime()
103 ctx->page_size = page.size; in flashdisk_init_runtime()
105 ctx->page_size = DEFAULT_BLOCK_SIZE; in flashdisk_init_runtime()
108 LOG_INF("Initialize device %s", ctx->info.name); in flashdisk_init_runtime()
110 (long)ctx->offset, ctx->sector_size, ctx->page_size, ctx->size); in flashdisk_init_runtime()
112 if (ctx->cache_size == 0) { in flashdisk_init_runtime()
114 LOG_INF("%s is read-only", ctx->info.name); in flashdisk_init_runtime()
118 if (IS_ENABLED(CONFIG_FLASHDISK_VERIFY_PAGE_LAYOUT) && flashdisk_with_erase(ctx)) { in flashdisk_init_runtime()
119 if (ctx->offset != page.start_offset) { in flashdisk_init_runtime()
121 ctx->info.name); in flashdisk_init_runtime()
125 offset = ctx->offset + page.size; in flashdisk_init_runtime()
126 while (offset < ctx->offset + ctx->size) { in flashdisk_init_runtime()
127 rc = flash_get_page_info_by_offs(ctx->info.dev, offset, &page); in flashdisk_init_runtime()
132 if (page.size != ctx->page_size) { in flashdisk_init_runtime()
139 if (offset != ctx->offset + ctx->size) { in flashdisk_init_runtime()
141 ctx->info.name); in flashdisk_init_runtime()
146 if (ctx->page_size > ctx->cache_size) { in flashdisk_init_runtime()
148 ctx->cache_size, ctx->page_size); in flashdisk_init_runtime()
157 struct flashdisk_data *ctx; in disk_flash_access_init() local
161 ctx = CONTAINER_OF(disk, struct flashdisk_data, info); in disk_flash_access_init()
163 rc = flash_area_open(ctx->area_id, &fap); in disk_flash_access_init()
165 LOG_ERR("Flash area %u open error %d", ctx->area_id, rc); in disk_flash_access_init()
169 k_mutex_lock(&ctx->lock, K_FOREVER); in disk_flash_access_init()
173 rc = flashdisk_init_runtime(ctx, fap); in disk_flash_access_init()
177 k_mutex_unlock(&ctx->lock); in disk_flash_access_init()
182 static bool sectors_in_range(struct flashdisk_data *ctx, in sectors_in_range() argument
187 start = ctx->offset + (start_sector * ctx->sector_size); in sectors_in_range()
188 end = start + (sector_count * ctx->sector_size); in sectors_in_range()
190 if ((end >= start) && (start >= ctx->offset) && (end <= ctx->offset + ctx->size)) { in sectors_in_range()
202 struct flashdisk_data *ctx; in disk_flash_access_read() local
209 ctx = CONTAINER_OF(disk, struct flashdisk_data, info); in disk_flash_access_read()
211 if (!sectors_in_range(ctx, start_sector, sector_count)) { in disk_flash_access_read()
215 fl_addr = ctx->offset + start_sector * ctx->sector_size; in disk_flash_access_read()
216 remaining = (sector_count * ctx->sector_size); in disk_flash_access_read()
218 k_mutex_lock(&ctx->lock, K_FOREVER); in disk_flash_access_read()
221 offset = fl_addr & (ctx->page_size - 1); in disk_flash_access_read()
222 fl_addr = ROUND_DOWN(fl_addr, ctx->page_size); in disk_flash_access_read()
225 len = ctx->page_size - offset; in disk_flash_access_read()
231 if (ctx->cache_valid && ctx->cached_addr == fl_addr) { in disk_flash_access_read()
232 memcpy(buff, &ctx->cache[offset], len); in disk_flash_access_read()
238 fl_addr += ctx->page_size; in disk_flash_access_read()
243 len = ctx->page_size; in disk_flash_access_read()
248 k_mutex_unlock(&ctx->lock); in disk_flash_access_read()
253 static int flashdisk_cache_commit(struct flashdisk_data *ctx) in flashdisk_cache_commit() argument
255 if (!ctx->cache_valid || !ctx->cache_dirty) { in flashdisk_cache_commit()
260 if (flashdisk_with_erase(ctx)) { in flashdisk_cache_commit()
261 if (flash_erase(ctx->info.dev, ctx->cached_addr, ctx->page_size) < 0) { in flashdisk_cache_commit()
267 if (flash_write(ctx->info.dev, ctx->cached_addr, ctx->cache, ctx->page_size) < 0) { in flashdisk_cache_commit()
271 ctx->cache_dirty = false; in flashdisk_cache_commit()
275 static int flashdisk_cache_load(struct flashdisk_data *ctx, off_t fl_addr) in flashdisk_cache_load() argument
279 __ASSERT_NO_MSG((fl_addr & (ctx->page_size - 1)) == 0); in flashdisk_cache_load()
281 if (ctx->cache_valid) { in flashdisk_cache_load()
282 if (ctx->cached_addr == fl_addr) { in flashdisk_cache_load()
287 rc = flashdisk_cache_commit(ctx); in flashdisk_cache_load()
295 ctx->cache_valid = false; in flashdisk_cache_load()
296 ctx->cache_dirty = false; in flashdisk_cache_load()
297 ctx->cached_addr = fl_addr; in flashdisk_cache_load()
298 rc = flash_read(ctx->info.dev, fl_addr, ctx->cache, ctx->page_size); in flashdisk_cache_load()
301 ctx->cache_valid = true; in flashdisk_cache_load()
308 /* input size is either less or equal to a block size (ctx->page_size)
311 static int flashdisk_cache_write(struct flashdisk_data *ctx, off_t start_addr, in flashdisk_cache_write() argument
319 offset = start_addr & (ctx->page_size - 1); in flashdisk_cache_write()
322 fl_addr = ROUND_DOWN(start_addr, ctx->page_size); in flashdisk_cache_write()
327 __ASSERT_NO_MSG(fl_addr + ctx->page_size >= start_addr + size); in flashdisk_cache_write()
329 rc = flashdisk_cache_load(ctx, fl_addr); in flashdisk_cache_write()
337 if (ctx->cache_dirty || memcmp(&ctx->cache[offset], buff, size)) { in flashdisk_cache_write()
339 memcpy(&ctx->cache[offset], buff, size); in flashdisk_cache_write()
340 ctx->cache_dirty = true; in flashdisk_cache_write()
349 struct flashdisk_data *ctx; in disk_flash_access_write() local
355 ctx = CONTAINER_OF(disk, struct flashdisk_data, info); in disk_flash_access_write()
357 if (ctx->cache_size == 0) { in disk_flash_access_write()
361 if (!sectors_in_range(ctx, start_sector, sector_count)) { in disk_flash_access_write()
365 fl_addr = ctx->offset + start_sector * ctx->sector_size; in disk_flash_access_write()
366 remaining = (sector_count * ctx->sector_size); in disk_flash_access_write()
368 k_mutex_lock(&ctx->lock, K_FOREVER); in disk_flash_access_write()
371 if (fl_addr & (ctx->page_size - 1)) { in disk_flash_access_write()
376 block_bnd = fl_addr + ctx->page_size; in disk_flash_access_write()
377 block_bnd = block_bnd & ~(ctx->page_size - 1); in disk_flash_access_write()
380 if (flashdisk_cache_write(ctx, fl_addr, remaining, buff) < 0) { in disk_flash_access_write()
387 size = GET_SIZE_TO_BOUNDARY(fl_addr, ctx->page_size); in disk_flash_access_write()
390 if (flashdisk_cache_write(ctx, fl_addr, size, buff) < 0) { in disk_flash_access_write()
402 if (remaining < ctx->page_size) { in disk_flash_access_write()
406 if (flashdisk_cache_write(ctx, fl_addr, ctx->page_size, buff) < 0) { in disk_flash_access_write()
411 fl_addr += ctx->page_size; in disk_flash_access_write()
412 remaining -= ctx->page_size; in disk_flash_access_write()
413 buff += ctx->page_size; in disk_flash_access_write()
418 if (flashdisk_cache_write(ctx, fl_addr, remaining, buff) < 0) { in disk_flash_access_write()
425 k_mutex_unlock(&ctx->lock); in disk_flash_access_write()
433 struct flashdisk_data *ctx; in disk_flash_access_ioctl() local
435 ctx = CONTAINER_OF(disk, struct flashdisk_data, info); in disk_flash_access_ioctl()
440 k_mutex_lock(&ctx->lock, K_FOREVER); in disk_flash_access_ioctl()
441 rc = flashdisk_cache_commit(ctx); in disk_flash_access_ioctl()
442 k_mutex_unlock(&ctx->lock); in disk_flash_access_ioctl()
445 *(uint32_t *)buff = ctx->size / ctx->sector_size; in disk_flash_access_ioctl()
448 *(uint32_t *)buff = ctx->sector_size; in disk_flash_access_ioctl()
451 k_mutex_lock(&ctx->lock, K_FOREVER); in disk_flash_access_ioctl()
452 *(uint32_t *)buff = ctx->page_size / ctx->sector_size; in disk_flash_access_ioctl()
453 k_mutex_unlock(&ctx->lock); in disk_flash_access_ioctl()