subsys: disk: Refactor disk_access stuff into a directory

Disk IO functions are used by both FS and USB Mass Storage.
This patch refactors those from FS directory to a separate one.
In addition existing, config options were modified to make
stuff meaningful.

Jira: ZEP-1276

Change-Id: Ia2a2e18f3dbbbdb964c3dc0427d8138ad86134cd
Signed-off-by: Jithu Joseph <jithu.joseph@intel.com>
This commit is contained in:
Jithu Joseph 2016-12-08 19:14:55 -08:00 committed by Anas Nashif
parent f6e039062a
commit 89f4bf7c7e
14 changed files with 158 additions and 124 deletions

View file

@ -64,6 +64,28 @@ config SPI_FLASH_W25QXXDV_SPI_NAME
config SPI_FLASH_W25QXXDV_SPI_SLAVE
default 1
if DISK_ACCESS_FLASH
config DISK_FLASH_DEV_NAME
default SPI_FLASH_W25QXXDV_DRV_NAME
config DISK_FLASH_START
default 0x0
config DISK_FLASH_MAX_RW_SIZE
default SPI_FLASH_W25QXXDV_MAX_DATA_LEN
config DISK_ERASE_BLOCK_SIZE
default 0x1000
config DISK_FLASH_ERASE_ALIGNMENT
default 0x1000
config DISK_VOLUME_SIZE
default 0x200000
endif # DISK_ACCESS_FLASH
endif # SPI_FLASH_W25QXXDV
endif # FLASH && SPI

View file

@ -13,19 +13,13 @@ CONFIG_USB_MASS_STORAGE=y
CONFIG_SYS_LOG_USB_LEVEL=3
CONFIG_SYS_LOG_USB_MASS_STORAGE_LEVEL=4
# Not required but can't get the disk access layers to be built without this.
# To be removed once the KConfig coupling is resolved. But if both the USB
# host and embedded target FS code needs to access the disk fs contents,
# this is required.
CONFIG_FILE_SYSTEM=y
# If the target's code needs to do file operations, enable target's
# FAT FS code. (Without this only the host can access the fs contents)
#CONFIG_FILE_SYSTEM=y
#SPI flash related configs
CONFIG_DISK_ACCESS_FLASH=y
CONFIG_FS_FAT_FLASH_DISK_W25QXXDV=y
CONFIG_FLASH=y
CONFIG_SPI=y
CONFIG_SPI_CS_GPIO=y
CONFIG_SPI_0_CS_GPIO_PIN=24
#Want RAM DISK instead ? comment the whole flash block and uncomment
#CONFIG_DISK_ACCESS_RAM=y

View file

@ -21,6 +21,8 @@ source "subsys/usb/Kconfig"
source "subsys/bluetooth/Kconfig"
source "subsys/disk/Kconfig"
source "subsys/net/Kconfig"
source "subsys/logging/Kconfig"
@ -28,3 +30,4 @@ source "subsys/logging/Kconfig"
source "subsys/debug/Kconfig"
source "subsys/shell/Kconfig"

View file

@ -3,5 +3,6 @@ obj-$(CONFIG_USB) += usb/
obj-$(CONFIG_BLUETOOTH) += bluetooth/
obj-$(CONFIG_NET_BUF) += net/
obj-$(CONFIG_CONSOLE_SHELL) += shell/
obj-$(CONFIG_DISK_ACCESS) += disk/
obj-y += logging/
obj-y += debug/

82
subsys/disk/Kconfig Normal file
View file

@ -0,0 +1,82 @@
#
# Copyright (c) 2016 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
menu "Disk"
config DISK_ACCESS
bool
default n
prompt "Enable Disk Interface"
help
Enable disk access over a supported media backend like FLASH or RAM
if DISK_ACCESS
choice
prompt "Storage backend selection"
config DISK_ACCESS_RAM
bool "RAM Disk"
help
RAM buffer used to emulate storage disk.
This option can used to test the file
system.
config DISK_ACCESS_FLASH
bool "Flash"
select FLASH
help
Flash device is used for the file system.
endchoice
if DISK_ACCESS_FLASH
config DISK_FLASH_DEV_NAME
string
prompt "Flash device name to be used as storage backend"
config DISK_FLASH_START
hex
help
This is start address of the flash to be used as storage backend.
config DISK_FLASH_MAX_RW_SIZE
int
help
This is the maximum number of bytes that the
flash_write API can do per invocation.
API.
config DISK_FLASH_ERASE_ALIGNMENT
hex
help
This is the start address alignment required by
the flash component.
config DISK_ERASE_BLOCK_SIZE
hex
help
This is typically the minimum block size that
is erased at one time in flash storage.
config DISK_VOLUME_SIZE
hex
help
This is the file system volume size in bytes.
endif # DISK_ACCESS_FLASH
endif # DISK_ACCESS
endmenu

3
subsys/disk/Makefile Normal file
View file

@ -0,0 +1,3 @@
obj-$(CONFIG_DISK_ACCESS_RAM) += disk_access_ram.o
obj-$(CONFIG_DISK_ACCESS_FLASH) += disk_access_flash.o

View file

@ -28,7 +28,7 @@
static struct device *flash_dev;
/* flash read-copy-erase-write operation */
static uint8_t read_copy_buf[CONFIG_FS_BLOCK_SIZE];
static uint8_t read_copy_buf[CONFIG_DISK_ERASE_BLOCK_SIZE];
static uint8_t *fs_buff = read_copy_buf;
/* calculate number of blocks required for a given size */
@ -42,10 +42,10 @@ static off_t lba_to_address(uint32_t sector_num)
{
off_t flash_addr;
flash_addr = CONFIG_FS_FLASH_START + sector_num * SECTOR_SIZE;
flash_addr = CONFIG_DISK_FLASH_START + sector_num * SECTOR_SIZE;
__ASSERT(flash_addr < (CONFIG_FS_FLASH_START + CONFIG_FS_VOLUME_SIZE),
"FS bound error");
__ASSERT(flash_addr < (CONFIG_DISK_FLASH_START +
CONFIG_DISK_VOLUME_SIZE), "FS bound error");
return flash_addr;
}
@ -65,7 +65,7 @@ int disk_access_init(void)
return 0;
}
flash_dev = device_get_binding(CONFIG_FS_FLASH_DEV_NAME);
flash_dev = device_get_binding(CONFIG_DISK_FLASH_DEV_NAME);
if (!flash_dev) {
return -ENODEV;
}
@ -83,12 +83,12 @@ int disk_access_read(uint8_t *buff, uint32_t start_sector,
fl_addr = lba_to_address(start_sector);
remaining = (sector_count * SECTOR_SIZE);
len = CONFIG_FS_FLASH_MAX_RW_SIZE;
len = CONFIG_DISK_FLASH_MAX_RW_SIZE;
num_read = GET_NUM_BLOCK(remaining, CONFIG_FS_FLASH_MAX_RW_SIZE);
num_read = GET_NUM_BLOCK(remaining, CONFIG_DISK_FLASH_MAX_RW_SIZE);
for (uint32_t i = 0; i < num_read; i++) {
if (remaining < CONFIG_FS_FLASH_MAX_RW_SIZE) {
if (remaining < CONFIG_DISK_FLASH_MAX_RW_SIZE) {
len = remaining;
}
@ -114,24 +114,24 @@ static int read_copy_flash_block(off_t start_addr, uint32_t size,
uint32_t offset = 0;
/* adjust offset if starting address is not erase-aligned address */
if (start_addr & (CONFIG_FS_FLASH_ERASE_ALIGNMENT - 1)) {
offset = start_addr & (CONFIG_FS_FLASH_ERASE_ALIGNMENT - 1);
if (start_addr & (CONFIG_DISK_FLASH_ERASE_ALIGNMENT - 1)) {
offset = start_addr & (CONFIG_DISK_FLASH_ERASE_ALIGNMENT - 1);
}
/* align starting address to an aligned address for flash erase-write */
fl_addr = ROUND_DOWN(start_addr, CONFIG_FS_FLASH_ERASE_ALIGNMENT);
fl_addr = ROUND_DOWN(start_addr, CONFIG_DISK_FLASH_ERASE_ALIGNMENT);
num_read = GET_NUM_BLOCK(CONFIG_FS_BLOCK_SIZE,
CONFIG_FS_FLASH_MAX_RW_SIZE);
num_read = GET_NUM_BLOCK(CONFIG_DISK_ERASE_BLOCK_SIZE,
CONFIG_DISK_FLASH_MAX_RW_SIZE);
/* read one block from flash */
for (uint32_t i = 0; i < num_read; i++) {
int rc;
rc = flash_read(flash_dev,
fl_addr + (CONFIG_FS_FLASH_MAX_RW_SIZE * i),
dest_buff + (CONFIG_FS_FLASH_MAX_RW_SIZE * i),
CONFIG_FS_FLASH_MAX_RW_SIZE);
fl_addr + (CONFIG_DISK_FLASH_MAX_RW_SIZE * i),
dest_buff + (CONFIG_DISK_FLASH_MAX_RW_SIZE * i),
CONFIG_DISK_FLASH_MAX_RW_SIZE);
if (rc != 0) {
return -EIO;
}
@ -143,7 +143,9 @@ static int read_copy_flash_block(off_t start_addr, uint32_t size,
return 0;
}
/* input size is either less or equal to a block size, CONFIG_FS_BLOCK_SIZE. */
/* input size is either less or equal to a block size,
* CONFIG_DISK_ERASE_BLOCK_SIZE.
*/
static int update_flash_block(off_t start_addr, uint32_t size, const void *buff)
{
off_t fl_addr;
@ -151,7 +153,7 @@ static int update_flash_block(off_t start_addr, uint32_t size, const void *buff)
uint32_t num_write;
/* if size is a partial block, perform read-copy with user data */
if (size < CONFIG_FS_BLOCK_SIZE) {
if (size < CONFIG_DISK_ERASE_BLOCK_SIZE) {
int rc;
rc = read_copy_flash_block(start_addr, size, buff, fs_buff);
@ -164,29 +166,30 @@ static int update_flash_block(off_t start_addr, uint32_t size, const void *buff)
}
/* always align starting address for flash write operation */
fl_addr = ROUND_DOWN(start_addr, CONFIG_FS_FLASH_ERASE_ALIGNMENT);
fl_addr = ROUND_DOWN(start_addr, CONFIG_DISK_FLASH_ERASE_ALIGNMENT);
/* disable write-protection first before erase */
flash_write_protection_set(flash_dev, false);
if (flash_erase(flash_dev, fl_addr, CONFIG_FS_BLOCK_SIZE) != 0) {
if (flash_erase(flash_dev, fl_addr, CONFIG_DISK_ERASE_BLOCK_SIZE)
!= 0) {
return -EIO;
}
/* write data to flash */
num_write = GET_NUM_BLOCK(CONFIG_FS_BLOCK_SIZE,
CONFIG_FS_FLASH_MAX_RW_SIZE);
num_write = GET_NUM_BLOCK(CONFIG_DISK_ERASE_BLOCK_SIZE,
CONFIG_DISK_FLASH_MAX_RW_SIZE);
for (uint32_t i = 0; i < num_write; i++) {
/* flash_write reenabled write-protection so disable it again */
flash_write_protection_set(flash_dev, false);
if (flash_write(flash_dev, fl_addr, src,
CONFIG_FS_FLASH_MAX_RW_SIZE) != 0) {
CONFIG_DISK_FLASH_MAX_RW_SIZE) != 0) {
return -EIO;
}
fl_addr += CONFIG_FS_FLASH_MAX_RW_SIZE;
src += CONFIG_FS_FLASH_MAX_RW_SIZE;
fl_addr += CONFIG_DISK_FLASH_MAX_RW_SIZE;
src += CONFIG_DISK_FLASH_MAX_RW_SIZE;
}
return 0;
@ -203,13 +206,13 @@ int disk_access_write(const uint8_t *buff, uint32_t start_sector,
remaining = (sector_count * SECTOR_SIZE);
/* check if start address is erased-aligned address */
if (fl_addr & (CONFIG_FS_FLASH_ERASE_ALIGNMENT - 1)) {
if (fl_addr & (CONFIG_DISK_FLASH_ERASE_ALIGNMENT - 1)) {
off_t block_bnd;
/* not aligned */
/* check if the size goes over flash block boundary */
block_bnd = fl_addr + CONFIG_FS_BLOCK_SIZE;
block_bnd = block_bnd & ~(CONFIG_FS_BLOCK_SIZE - 1);
block_bnd = fl_addr + CONFIG_DISK_ERASE_BLOCK_SIZE;
block_bnd = block_bnd & ~(CONFIG_DISK_ERASE_BLOCK_SIZE - 1);
if ((fl_addr + remaining) < block_bnd) {
/* not over block boundary (a partial block also) */
if (update_flash_block(fl_addr, remaining, buff) != 0) {
@ -219,7 +222,8 @@ int disk_access_write(const uint8_t *buff, uint32_t start_sector,
}
/* write goes over block boundary */
size = GET_SIZE_TO_BOUNDARY(fl_addr, CONFIG_FS_BLOCK_SIZE);
size = GET_SIZE_TO_BOUNDARY(fl_addr,
CONFIG_DISK_ERASE_BLOCK_SIZE);
/* write first partial block */
if (update_flash_block(fl_addr, size, buff) != 0) {
@ -235,18 +239,19 @@ int disk_access_write(const uint8_t *buff, uint32_t start_sector,
while (remaining) {
int rc;
if (remaining < CONFIG_FS_BLOCK_SIZE) {
if (remaining < CONFIG_DISK_ERASE_BLOCK_SIZE) {
break;
}
rc = update_flash_block(fl_addr, CONFIG_FS_BLOCK_SIZE, buff);
rc = update_flash_block(fl_addr, CONFIG_DISK_ERASE_BLOCK_SIZE,
buff);
if (rc != 0) {
return -EIO;
}
fl_addr += CONFIG_FS_BLOCK_SIZE;
remaining -= CONFIG_FS_BLOCK_SIZE;
buff += CONFIG_FS_BLOCK_SIZE;
fl_addr += CONFIG_DISK_ERASE_BLOCK_SIZE;
remaining -= CONFIG_DISK_ERASE_BLOCK_SIZE;
buff += CONFIG_DISK_ERASE_BLOCK_SIZE;
}
/* remaining partial block */
@ -265,16 +270,16 @@ int disk_access_ioctl(uint8_t cmd, void *buff)
case DISK_IOCTL_CTRL_SYNC:
return 0;
case DISK_IOCTL_GET_SECTOR_COUNT:
*(uint32_t *)buff = CONFIG_FS_VOLUME_SIZE / SECTOR_SIZE;
*(uint32_t *)buff = CONFIG_DISK_VOLUME_SIZE / SECTOR_SIZE;
return 0;
case DISK_IOCTL_GET_SECTOR_SIZE:
*(uint32_t *) buff = SECTOR_SIZE;
return 0;
case DISK_IOCTL_GET_ERASE_BLOCK_SZ: /* in sectors */
*(uint32_t *)buff = CONFIG_FS_BLOCK_SIZE / SECTOR_SIZE;
*(uint32_t *)buff = CONFIG_DISK_ERASE_BLOCK_SIZE / SECTOR_SIZE;
return 0;
case DISK_IOCTL_GET_DISK_SIZE:
*(uint32_t *)buff = CONFIG_FS_VOLUME_SIZE;
*(uint32_t *)buff = CONFIG_DISK_VOLUME_SIZE;
return 0;
default:
break;

View file

@ -18,6 +18,7 @@ menu "File System"
config FILE_SYSTEM
bool "File system support"
select DISK_ACCESS
default n
help
Enables support for file system.
@ -37,78 +38,6 @@ config FILE_SYSTEM_FAT
help
Enables FAT file system support.
choice
prompt "Storage backend selection"
config DISK_ACCESS_RAM
bool "RAM Disk"
help
RAM buffer used to emulate storage disk.
This option can used to test the file
system.
config DISK_ACCESS_FLASH
bool "Flash"
help
Flash device is used for the file system.
endchoice
config FS_VOLUME_SIZE
hex
default 0x18000 if DISK_ACCESS_RAM
default 0x200000 if FS_FAT_FLASH_DISK_W25QXXDV
help
This is the file system volume size in bytes.
config FS_BLOCK_SIZE
hex
default 0x1000 if DISK_ACCESS_RAM
default 0x1000 if FS_FAT_FLASH_DISK_W25QXXDV
help
This is typically the minimum block size that
is erased at one time in flash storage.
if DISK_ACCESS_FLASH
config FS_FAT_FLASH_DISK_W25QXXDV
bool "W25QXXDV flash component"
help
Enables to use the W25QXXDV as the storage media
for the file system.
if FS_FAT_FLASH_DISK_W25QXXDV
config FS_FLASH_DEV_NAME
string
prompt "File system flash storage device name"
depends on SPI_FLASH_W25QXXDV
default SPI_FLASH_W25QXXDV_DRV_NAME
config FS_FLASH_START
hex
default 0x0
help
This is start address of the flash for the file
system.
config FS_FLASH_MAX_RW_SIZE
int
default SPI_FLASH_W25QXXDV_MAX_DATA_LEN
help
This is the maximum number of bytes that the
flash_write API can do per invocation.
API.
config FS_FLASH_ERASE_ALIGNMENT
hex
default 0x1000
help
This is the start address alignment required by
the flash component.
endif # FS_FAT_FLASH_DISK_W25QXXDV
endif # DISK_ACCESS_FLASH
endif # FILE_SYSTEM
endmenu

View file

@ -1,5 +1,2 @@
obj-$(CONFIG_FILE_SYSTEM_SHELL) += shell.o
obj-$(CONFIG_FILE_SYSTEM_FAT) += fat_fs.o
obj-$(CONFIG_DISK_ACCESS_RAM) += disk_access_ram.o
obj-$(CONFIG_DISK_ACCESS_FLASH) += disk_access_flash.o

View file

@ -55,6 +55,7 @@ config SYS_LOG_USB_CDC_ACM_LEVEL
config USB_MASS_STORAGE
bool
prompt "USB Mass Storage Device Class Driver"
select DISK_ACCESS
default n
help
USB Mass Storage device class driver

View file

@ -20,5 +20,4 @@ CONFIG_GPIO=y
CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_FAT=y
CONFIG_DISK_ACCESS_FLASH=y
CONFIG_FS_FAT_FLASH_DISK_W25QXXDV=y
CONFIG_FILE_SYSTEM_SHELL=y

View file

@ -1,8 +1,6 @@
CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_FAT=y
CONFIG_DISK_ACCESS_FLASH=y
CONFIG_FS_FAT_FLASH_DISK_W25QXXDV=y
CONFIG_FLASH=y
CONFIG_SPI=y
CONFIG_GPIO=y
#CONFIG_DISK_ACCESS_RAM=y