Programming for I/O and StorageXin-Xin Yang
Chapter OutlineI/O device and I/O controller 819
Category of I/O devices 819
Subordination category 819
Usage category 819
Characteristic category 820
Information transferring unit category 820
I/O controller 821
Memory-mapped I/O and DMA 822
Flash, SD/SDHC and disk drive 825
Flash memory 825
Hard disk drive 826
Solid-state drive 828
Network-attached storage 828
I/O programming 829I/O control mode 829
Polling mode 829
Interrupt control mode 830
DMA control mode 832
Channel control mode 833
I/O software goals 834
Device independence 834
Uniform naming 834
Exception handling 835
Synchronous blocking and asynchronous transfer 835
I/O software layer 835
Interrupt service routine 836
Device driver 836
Device-independent I/O software 837
User-level I/O software 837
Case study: device driver in Linux 838
Register and un-register device number 840
The file_operations structure 841Linux character device driver composition 842
817Software Engineering for Embedded Systems.
2013 Elsevier Inc. All rights reserved.
Storage programming 844I/O for block devices 845
The block_device_operations structure 845
Gendisk structure 847
Block I/O 848
Block device registration and un-registration 849
Flash device programming 850
MTD system in Linux 850
NOR flash driver 856
NAND flash device driver 859
Flash translation layer and flash file system 863
SATA device driver 864
Performance improvement of storage systems 865Case study 1: performance optimization on SDHC 866
SD/SDHC Linux driver 866
Test result 868
Case study 2: performance optimization on NAS 868
Performance optimization method analysis 869
Test result 875
Input and output (I/O) devices are very important components in the embedded system. In
this book, I/O devices refer to all the components in the embedded system except the CPU
and memory, e.g., device controllers and I/O channels. This I/O diversity makes the I/O
management in the embedded system a very complicated subsystem. One of the basic
functions of the embedded OS is to control and manage all of the I/O devices, and to
coordinate multiple processes accessing I/O devices simultaneously.
The storage in this book refers to external storage devices such as NOR/NAND flash,
eSDHC, U-Disk, HDD and SSD, which are commonly used in embedded systems. With the
recent development of cloud computing, storage technology plays a more and more
important role in the whole system and is moving faster and faster.
The key task for device management is to control the I/O implementation between the CPU
and the devices, in a way which meets the users I/O application requirement. The
operating system must send commands to the devices, respond to interrupts and handle
exceptions from the devices. It should also provide a simple and easily used interface
between the devices and other parts of the system. Therefore, the I/O management module
needs to improve the parallel process capability between the CPU and I/O devices, I/O
devices and I/O devices, to get the best utilization efficiency of the system resources.
818 Chapter 23
The I/O management module should provide a unified, transparent, independent and
scalable I/O interface.
This chapter introduces the data transfer mode between the CPU and I/O devices, interrupt
technology, the I/O control process and the device driver implementation process. In the
second part of this chapter, a programming model for storage devices is introduced,
including feature support and performance optimization.
I/O device and I/O controller
Category of I/O devices
There are many types of I/O devices in embedded systems with complicated structures and
different working models. In order to manage them efficiently, the OS usually classifies
these devices from different perspectives.
System device: standard devices which are already registered in the system when the OS
boots up. Examples include NOR/NAND flash, touch panel, etc. There are device drivers
and management programs in the OS for these devices. The user applications only need to
call the standard commands or functions provided by the OS in order to use these devices.
User device: non-standard devices which are not registered in the system when the OS
boots up. Usually the device drivers are provided by the user. The user must transfer the
control of these devices in some way to the OS for management. Typical devices include
SD card, USB disk, etc.
Exclusive device: a device which can only be used by one process at a time. For multiple
concurrent processes, each process uses the device mutually exclusively. Once the OS
assigns the device to a specific process, it will be owned by this process exclusively till the
process releases it after usage.
Shared device: a device which can be addressed by multiple processes at a time. The shared
device must be addressable and addressed randomly. The shared device mechanism can
improve the utilization of each device.
Virtual device: a device which is transferred from a physical device to multiple logical devices
by utilizing virtualization technology. The transferred devices are called virtual devices.
Programming for I/O and Storage 819
Storage device: this type of device is used for storing information. Typical examples in the
embedded system include hard disk, solid-state disk, NOR/NAND flash.
I/O device: this type of device includes two groups input devices and output devices.The input device is responsible for inputting information from an external source to the
internal system, such as a touch panel, barcode scanner, etc. In contrast, the output device is
responsible for outputting the information processed by the embedded system to the
external world, such as an LCD display, speaker, etc.
Information transferring unit category
Block device: this type of device organizes and exchanges data in units of data block, so it
is called a block device. It is a kind of structural device. The typical device is a hard disk.
In the I/O operation, the whole block of data should be read or written even if it is only a
Character device: this type of device organizes and exchanges data in units of character, so
it is called a character device. It is a kind of non-structural device. There are a lot of types
of character device, e.g., serial port, touch panel, printer. The basic feature of the character
device is that the transfer rate is low and its not addressable. An interrupt is often used
when a character device executes an I/O operation.
Therefore, we can see that there are many types of I/O devices. The features and
performance of different devices vary significantly. The most obvious difference is the data
transfer rate. Table 23.1 shows the theoretical transfer rate of some common devices in
Table 23.1: Theoretical maximum data transfer rate of typical I/O
I/O Device Theoretical Date Rate
Keyboard 10 B/sRS232 1.2 KB/s
802.11g WLAN 6.75 MB/sFast Ethernet 12.5 MB/s
eSDHC 25 MB/sUSB2.0 60 MB/s
10G Ethernet 125 MB/sSATAII 370 MB/s
PCI Express2.0 625 MB/sSerial Rapid IO 781 MB/s
820 Chapter 23
Usually the I/O device is composed of two parts a mechanical part and an electronic part.The electronic part is called the device controller or adapter. In the embedded system, it
usually exists as a peripheral chip or on the PCB in an expansion slot. The mechanical part
is the device itself such as a disk or a memory stick, etc.
In the embedded system, I/O controllers (the electronic part) are usually connected to the
system bus. Figure 23.1 illustrates the I/O structure and how I/O controllers are connected
in the system.
The device controller is an interface between the CPU and the device. It accepts commands
from the CPU, controls the operation of the I/O device and achieves data transfer between
the system memory and device. In this way, it can free up the high-frequency CPU from the
control of the low-speed peripherals.
Figure 23.2 shows the basic structure of the device controller.
It contains the following parts:
Data registers: they contain the data needing to be input or output.
Control/status registers: the control registers are used to select a specific function of the
external devices, i.e., select the function of a multiplexed pin or determine whether the
CRC is enabled. The status registers are used to reflect the status of the current device,
i.e., whether the command is finished or whether an error has occurred.
I/O control logic: this is used to control the operation of the devices.
Interface to CPU: this is used to transfer commands and data between the controller and
Processor System Memory