Linux kernel driver--Hardware access I/O "original"

Source: Internet
Author: User

Registers and memory

What is the difference between registers and memory?

The main difference between registers and RAM is that the register operation has side effects (side effect or marginal effects):

Reading an address can cause the content to change, such as the interrupt status register for many devices is automatically zeroed out as soon as it is read.

Memory and IO

In the X86 processor has the concept of IO space, IO space is relative memory space, they are independent of each other's address space, in the 32-bit x86 system, the IO space size is only 64K, memory 4G

X86 supports memory space, IO space

ARM only supports memory space

MIPS only supports memory space

PowerPC only supports memory space

IO port:

When a register or memory is in IO space, it is called an IO port.

IO Memory:

When a register or memory is in memory space, it is called IO memory.

Operating I/O ports

The operation of the I/O port needs to be done as follows:

1. Application

2. Visit

3. Release

Request I/O port

The kernel provides a set of functions to allow the driver to request the I/O port he needs, where the core functions are:

struct Resource *request_region (unsigned long first, unsigned long n, const char *name)

This function tells the kernel that you want to use N ports starting with first, and the name parameter is the device. If the request succeeds, returns non-NULL, or NULL if the request fails.

The distribution of ports in the system is recorded in/proc/ioports. Cat/proc/ioports, if you cannot allocate the required ports, you can come here to see who is using it.

accessing I/O ports

The I/O ports can be divided into 8-bit, 16-, 32-bit ports, and the Linux kernel header file (System-dependent header file <asm/io.h>) defines the following inline functions to access the I/O ports:

Unsigned inb (unsigned port) Read byte port (8 bit width)

void Outb (unsigned char byte, unsigned port) (8-bit width)

Unsigned inw (unsigned port)

void outw (unsigned short word, unsigned port) Access 16-bit port

unsigned inl (unsigned port)

void Outl (unsigned long word, unsigned port) Access 32-bit port

Releasing the I/O port

When you run out of a set of I/O ports (usually when the drive is unloaded), return them to the system using the following functions:

void Release_region (unsigned long start, unsigned long N)

Operating I/O memory

Steps:

1. Application

2. Mapping

3. Visit

4. Release

Request I/O memory

The core functions are:

Struct Resource *request_mem_region (unsigned long start, unsigned long len, char *name)

This function applies a memory area that starts at start and is len bytes in length. If successful, returns non-null,

Otherwise, NULL is returned, all existing in the I/O that are already in use/proc/iomem listed in the

Mapping I/O memory

Before accessing IO memory, you must map the physical address to the virtual address,

void *ioremap (unsigned long phys_addr, unsigned long size)

Accessing IO Memory

Read from IO Memory:

unsigned ioread8 (void *addr)

unsigned ioread16 (void *addr)

unsigned ioread32 (void *addr)

Write IO memory

void Iowrite8 (U8 value, void *addr)

void Iowrite16 (U16 value, void *addr)

void Iowrite32 (u32 value, void *addr)

Older versions of I/O memory access functions:

Read from I/O memory,

unsigned readb (address)

unsigned readw (address)

unsigned readl (address)

Write IO memory

Unsigned writeb (unsigned value, address)

Unsigned writew (unsigned value, address)

Unsigned writel (unsigned value, address)

Freeing IO memory

IO memory is no longer required for use when it should be freed, step:

1. void Iounmap (void *addr)

2, void release_mem_region (unsigned long start, unsigned long len)

Welcome to Exchange If there is reprint please indicate the source

Sina Blog: http://blog.sina.com.cn/u/2049150530
Blog Park: http://www.cnblogs.com/sky-heaven/
Know: Http://www.zhihu.com/people/zhang-bing-hua

Linux kernel driver--Hardware access I/O "original"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.