----------------------------------------------------------------------------------------------------------------------------
内核版本:linux 5.2.8根文件系统:busybox 1.25.0u-boot:2016.05----------------------------------------------------------------------------------------------------------------------------
linux内核将块设备相关的驱动放在drivers/block路径下:
root@zhengyang:/work/sambashare/linux-5.2.8# ls drivers/block/
amiflop.c built-in.a loop.c nbd.c pktcdvd.c rsxx swim_asm.S virtio_blk.c zram
aoe cryptoloop.c loop.h null_blk.h ps3disk.c skd_main.c swim.c xen-blkback
ataflop.c drbd loop.o null_blk_main.c ps3vram.c skd_s1120.h sx8.c xen-blkfront.c
brd.c floppy.c Makefile null_blk_zoned.c rbd.c sunvdc.c umem.c xsysace.c
brd.o Kconfig mtip32xx paride rbd_types.h swim3.c umem.h z2ram.c
这里我们以块设备驱动z2ram为例进行讲解分析。z2ram驱动是实用RAM模拟一段块设备,也就是ramdisk。
一、Kconfig文件
1.1 AMIGA_Z2RAM
我们首先查看一下drivers/block/Kconfig文件配置,找到Z2RAM相关配置:
config AMIGA_Z2RAM
tristate "Amiga Zorro II ramdisk support"
depends on ZORRO
help
This enables support for using Chip RAM and Zorro II RAM as a
ramdisk or as a swap partition. Say Y if you want to include this
driver in the kernel.
To compile this driver as a module, choose M here: the
module will be called z2ram.
可以看到如果我们配置了AMIGA_Z2RAM,这支持使用芯片RAM和Zero II RAM作为ramdisk或者交换分区,此外AMIGA_Z2RAM还依赖于模块ZORRO。
ZORRO配置在板载配置文件arch/m68k/configs/amiga_defconfig中定义为y,而我们使用的Mini2440开发板并不支持该配置:
root@zhengyang:/work/sambashare/linux-5.2.8# grep "CONFIG_ZORRO" * -nR
arch/m68k/amiga/config.c:174:#ifdef CONFIG_ZORRO
arch/m68k/amiga/config.c:185:#endif /* CONFIG_ZORRO */
arch/m68k/amiga/config.c:844:#ifdef CONFIG_ZORRO
arch/m68k/amiga/config.c:850:#endif /* CONFIG_ZORRO */
arch/m68k/amiga/platform.c:19:#ifdef CONFIG_ZORRO
arch/m68k/amiga/platform.c:80:#else /* !CONFIG_ZORRO */
arch/m68k/amiga/platform.c:84:#endif /* !CONFIG_ZORRO */
arch/m68k/configs/amiga_defconfig:22:CONFIG_ZORRO=y
arch/m68k/configs/amiga_defconfig:24:CONFIG_ZORRO_NAMES=y
arch/m68k/configs/amiga_defconfig:398:CONFIG_ZORRO8390=y
arch/m68k/configs/multi_defconfig:32:CONFIG_ZORRO=y
arch/m68k/configs/multi_defconfig:35:CONFIG_ZORRO_NAMES=y
arch/m68k/configs/multi_defconfig:447:CONFIG_ZORRO8390=y
drivers/zorro/zorro.h:3:#ifdef CONFIG_ZORRO_NAMES
drivers/zorro/Makefile:6:obj-$(CONFIG_ZORRO) += zorro.o zorro-driver.o zorro-sysfs.o
drivers/zorro/Makefile:8:obj-$(CONFIG_ZORRO_NAMES) += names.o
drivers/video/fbdev/cirrusfb.c:47:#ifdef CONFIG_ZORRO
drivers/video/fbdev/cirrusfb.c:276:#ifdef CONFIG_ZORRO
drivers/video/fbdev/cirrusfb.c:345:#endif /* CONFIG_ZORRO */
drivers/video/fbdev/cirrusfb.c:1053:#ifdef CONFIG_ZORRO
drivers/video/fbdev/cirrusfb.c:1133:#elif defined(CONFIG_ZORRO)
drivers/video/fbdev/cirrusfb.c:1134: /* FIXME: CONFIG_PCI and CONFIG_ZORRO may be defined both */
drivers/video/fbdev/cirrusfb.c:1535:#ifdef CONFIG_ZORRO
drivers/video/fbdev/cirrusfb.c:1670:#ifdef CONFIG_ZORRO /* only works on Zorro boards */
drivers/video/fbdev/cirrusfb.c:1712:#endif /* CONFIG_ZORRO */
drivers/video/fbdev/cirrusfb.c:1943:#ifdef CONFIG_ZORRO
drivers/video/fbdev/cirrusfb.c:1956:#endif /* CONFIG_ZORRO */
drivers/video/fbdev/cirrusfb.c:2197:#ifdef CONFIG_ZORRO
drivers/video/fbdev/cirrusfb.c:2327:#endif /* CONFIG_ZORRO */
drivers/video/fbdev/cirrusfb.c:2372:#ifdef CONFIG_ZORRO
drivers/video/fbdev/cirrusfb.c:2386:#ifdef CONFIG_ZORRO
drivers/video/fbdev/cirrusfb.c:2511:#ifdef CONFIG_ZORRO
drivers/video/fbdev/cirrusfb.c:2521:#ifdef CONFIG_ZORRO
drivers/Makefile:99:obj-$(CONFIG_ZORRO) += zorro/
drivers/net/ethernet/8390/Makefile:20:obj-$(CONFIG_ZORRO8390) += zorro8390.o
如果定义了CONFIG_ZORRO将会编译ZORRO模块,关于zooro相关文件定义:
root@zhengyang:/work/sambashare/linux-5.2.8# find -name zorro.*
./arch/m68k/include/asm/zorro.h
./include/uapi/linux/zorro.h
./include/linux/zorro.h
./drivers/zorro/zorro.h
./drivers/zorro/zorro.c
./drivers/zorro/zorro.ids
./Documentation/zorro.txt
这里我们粗略看一下drivers/zorro/zorro.c文件,咱么也不做过多的解读,因为这不是这一节的重点,了解即可。
1.2 zorro.c
/*
* Zorro Bus Services
*
* Copyright (C) 1995-2003 Geert Uytterhoeven
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of this archive
* for more details.
*/
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/zorro.h>
#include <linux/bitops.h>
#include <linux/string.h>
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <asm/byteorder.h>
#include <asm/setup.h>
#include <asm/amigahw.h>
#include "zorro.h"
/*
* Zorro Expansion Devices
*/
unsigned int zorro_num_autocon;
struct zorro_dev_init zorro_autocon_init[ZORRO_NUM_AUTO] __initdata;
struct zorro_dev *zorro_autocon;
/*
* Zorro bus
*/
struct zorro_bus {
struct device dev;
struct zorro_dev devices[0];
};
/*
* Find Zorro Devices
*/
struct zorro_dev *zorro_find_device(zorro_id id, struct zorro_dev *from)
{
struct zorro_dev *z;
if (!zorro_num_autocon)
return NULL;
for (z = from ? from+1 : &zorro_autocon[0];
z < zorro_autocon+zorro_num_autocon;
z++)
if (id == ZORRO_WILDCARD || id == z->id)
return z;
return NULL;
}
EXPORT_SYMBOL(zorro_find_device);
/*
* Bitmask indicating portions of available Zorro II RAM that are unused
* by the system. Every bit represents a 64K chunk, for a maximum of 8MB
* (128 chunks, physical 0x00200000-0x009fffff).
*
* If you want to use (= allocate) portions of this RAM, you should clear
* the corresponding bits.
*
* Possible uses:
* - z2ram device
* - SCSI DMA bounce buffers
*
* FIXME: use the normal resource management
*/
DECLARE_BITMAP(zorro_unused_z2ram, 128);
EXPORT_SYMBOL(zorro_unused_z2ram);
static void __init mark_region(unsigned long start, unsigned long end,
int flag)
{
if (flag)
start += Z2RAM_CHUNKMASK;
else
end += Z2RAM_CHUNKMASK;
start &= ~Z2RAM_CHUNKMASK;
end &= ~Z2RAM_CHUNKMASK;
if (end <= Z2RAM_START || start >= Z2RAM_END)
return;
start = start < Z2RAM_START ? 0x00000000 : start-Z2RAM_START;
end = end > Z2RAM_END ? Z2RAM_SIZE : end-Z2RAM_START;
while (start < end) {
u32 chunk = start>>Z2RAM_CHUNKSHIFT;
if (flag)
set_bit(chunk, zorro_unused_z2ram);
else
clear_bit(chunk, zorro_unused_z2ram);
start += Z2RAM_CHUNKSIZE;
}
}
static struct resource __init *zorro_find_parent_resource(
struct platform_device *bridge, struct zorro_dev *z)
{
int i;
for (i = 0; i < bridge->num_resources; i++) {
struct resource *r = &bridge->resource[i];
if (zorro_resource_start(z) >= r->start &&
zorro_resource_end(z) <= r->end)
return r;
}
return &iomem_resource;
}
static int __init amiga_zorro_probe(struct platform_device *pdev)
{
struct zorro_bus *bus;
struct zorro_dev_init *zi;
struct zorro_dev *z;
struct resource *r;
unsigned int i;
int error;
/* Initialize the Zorro bus */
bus = kzalloc(struct_size(bus, devices, zorro_num_autocon),
GFP_KERNEL);
if (!bus)
return -ENOMEM;
zorro_autocon = bus->devices;
bus->dev.parent = &pdev->dev;
dev_set_name(&bus->dev, zorro_bus_type.name);
error = device_register(&bus->dev);
if (error) {
pr_err("Zorro: Error registering zorro_bus\n");
put_device(&bus->dev);
kfree(bus);
return error;
}
platform_set_drvdata(pdev, bus);
pr_info("Zorro: Probing AutoConfig expansion devices: %u device%s\n",
zorro_num_autocon, zorro_num_autocon == 1 ? "" : "s");
/* First identify all devices ... */
for (i = 0; i < zorro_num_autocon; i++) {
zi = &zorro_autocon_init[i];
z = &zorro_autocon[i];
z->rom = zi->rom;
z->id = (be16_to_cpu(z->rom.er_Manufacturer) << 16) |
(z->rom.er_Product << 8);
if (z->id == ZORRO_PROD_GVP_EPC_BASE) {
/* GVP quirk */
unsigned long magic = zi->boardaddr + 0x8000;
z->id |= *(u16 *)ZTWO_VADDR(magic) & GVP_PRODMASK;
}
z->slotaddr = zi->slotaddr;
z->slotsize = zi->slotsize;
sprintf(z->name, "Zorro device %08x", z->id);
zorro_name_device(z);
z->resource.start = zi->boardaddr;
z->resource.end = zi->boardaddr + zi->boardsize - 1;
z->resource.name = z->name;
r = zorro_find_parent_resource(pdev, z);
error = request_resource(r, &z->resource);
if (error)
dev_err(&bus->dev,
"Address space collision on device %s %pR\n",
z->name, &z->resource);
z->dev.parent = &bus->dev;
z->dev.bus = &zorro_bus_type;
z->dev.id = i;
switch (z->rom.er_Type & ERT_TYPEMASK) {
case ERT_ZORROIII:
z->dev.coherent_dma_mask = DMA_BIT_MASK(32);
break;
case ERT_ZORROII:
default:
z->dev.coherent_dma_mask = DMA_BIT_MASK(24);
break;
}
z->dev.dma_mask = &z->dev.coherent_dma_mask;
}
/* ... then register them */
for (i = 0; i < zorro_num_autocon; i++) {
z = &zorro_autocon[i];
error = device_register(&z->dev);
if (error) {
dev_err(&bus->dev, "Error registering device %s\n",
z->name);
put_device(&z->dev);
continue;
}
}
/* Mark all available Zorro II memory */
zorro_for_each_dev(z) {
if (z->rom.er_Type & ERTF_MEMLIST)
mark_region(zorro_resource_start(z),
zorro_resource_end(z)+1, 1);
}
/* Unmark all used Zorro II memory */
for (i = 0; i <