1 /*
2 * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3 */
4
5 #include <aos/mtdnor.h>
6 #include <drv/spiflash.h>
7
_nor_erase(aos_mtd_t * mtd,off_t addr,size_t len)8 static int _nor_erase(aos_mtd_t *mtd, off_t addr, size_t len)
9 {
10 (void) mtd;
11
12 return csi_spiflash_erase(NULL, addr, len);
13 }
14
_nor_read(aos_mtd_t * mtd,off_t from,struct mtd_io_desc * desc)15 static int _nor_read(aos_mtd_t *mtd, off_t from, struct mtd_io_desc *desc)
16 {
17 int ret;
18
19 (void) mtd;
20
21 ret = csi_spiflash_read(NULL, from, desc->datbuf, desc->datlen);
22 if (ret > 0) {
23 desc->datretlen = ret;
24 }
25
26 return ret;
27 }
28
_nor_write(aos_mtd_t * mtd,off_t to,struct mtd_io_desc * desc)29 static int _nor_write(aos_mtd_t *mtd, off_t to, struct mtd_io_desc *desc)
30 {
31 int ret;
32
33 (void) mtd;
34
35 ret = csi_spiflash_program(NULL, to, desc->datbuf, desc->datlen);
36 if (ret > 0) {
37 desc->datretlen = ret;
38 }
39
40 return ret;
41 }
42
43 static const struct mtd_ops _ops =
44 {
45 _nor_erase,
46 _nor_read,
47 _nor_write,
48 NULL,
49 NULL
50 };
51
aos_mtd_nor_init(aos_mtd_t * nor,int blksize,int sector_size)52 int aos_mtd_nor_init(aos_mtd_t *nor, int blksize, int sector_size)
53 {
54 nor->sector_size = sector_size > 0 ? sector_size : 1;
55 nor->block_size = blksize;
56 nor->ops = &_ops;
57 nor->type = MTD_TYPE_NOR;
58 nor->oob_size = 0;
59
60 return 0;
61 }
62