linux mtd设备使用

作者

QQ群:852283276
微信:arm80x86
微信公众号:青儿创客基地
B站:主页 https://space.bilibili.com/208826118

参考

mtd-physmap
General MTD documentation

问题

开机启动打印,

jffs2: Write of 109 bytes at 0x0160f61c failed. returned -5, retlen 108
jffs2: Write of 109 bytes at 0x0160f68c failed. returned -5, retlen 108
jffs2: Write of 45 bytes at 0x0160f784 failed. returned -5, retlen 44
jffs2: Write of 45 bytes at 0x0160f7b4 failed. returned -5, retlen 44
jffs2: Write of 77 bytes at 0x0160f828 failed. returned -5, retlen 76
jffs2: Write of 77 bytes at 0x0160f878 failed. returned -5, retlen 76

写flash打印,

root@t2080rdb:~# fw_setenv flag 1
MTD erase error on /dev/mtd6: Input/output error
Error: can't write fw_env to flash

其中Input/output error就是代表returned -5

分析

用户层发起ioctl传到驱动drivers\mtd\mtdchar.c

mtdchar_ioctl

cfi-flash对应驱动代码为drivers\mtd\maps\physmap_of.c

Nor Flash设备树

CFI or JEDEC memory-mapped NOR flash, MTD-RAM (NVRAM…)

Flash chips (Memory Technology Devices) are often used for solid state file systems on embedded devices.

  • compatible : should contain the specific model of mtd chip(s) used, if known, followed by either “cfi-flash”, “jedec-flash”, “mtd-ram” or “mtd-rom”.
  • reg : Address range(s) of the mtd chip(s) It’s possible to (optionally) define multiple “reg” tuples so that non-identical chips can be described in one node.
  • bank-width : Width (in bytes) of the bank. Equal to the device width times the number of interleaved chips.
  • device-width : (optional) Width of a single mtd chip. If omitted, assumed to be equal to ‘bank-width’.
  • #address-cells, #size-cells : Must be present if the device has sub-nodes representing partitions (see below). In this case both #address-cells and #size-cells must be equal to 1.
  • no-unaligned-direct-access: boolean to disable the default direct mapping of the flash. On some platforms (e.g. MPC5200) a direct 1:1 mapping may cause problems with JFFS2 usage, as the local bus (LPB) doesn’t support unaligned accesses as implemented in the JFFS2 code via memcpy(). By defining “no-unaligned-direct-access”, the flash will not be exposed directly to the MTD users (e.g. JFFS2) any more.
  • linux,mtd-name: allow to specify the mtd name for retro capability with physmap-flash drivers as boot loader pass the mtd partition via the old device name physmap-flash.
  • use-advanced-sector-protection: boolean to enable support for the advanced sector protection (Spansion: PPB - Persistent Protection Bits) locking.
  • addr-gpios : (optional) List of GPIO descriptors that will be used to address the MSBs address lines. The order goes from LSB to MSB.

For JEDEC compatible devices, the following additional properties are defined:

  • vendor-id : Contains the flash chip’s vendor id (1 byte).
  • device-id : Contains the flash chip’s device id (1 byte).

For ROM compatible devices (and ROM fallback from cfi-flash), the following additional (optional) property is defined:

  • erase-size : The chip’s physical erase block size in bytes.

The device tree may optionally contain endianness property. little-endian or big-endian : It Represents the endianness that should be used by the controller to properly read/write data from/to the flash. If this property is missing, the endianness is chosen by the system (potentially based on extra configuration options).
The device tree may optionally contain sub-nodes describing partitions of the address space. See partition.txt for more detail.
Example:

	flash@ff000000 {
		compatible = "amd,am29lv128ml", "cfi-flash";
		reg = <ff000000 01000000>;
		bank-width = <4>;
		device-width = <1>;
		#address-cells = <1>;
		#size-cells = <1>;
		fs@0 {
			label = "fs";
			reg = <0 f80000>;
		};
		firmware@f80000 {
			label ="firmware";
			reg = <f80000 80000>;
			read-only;
		};
	};

Here an example with multiple “reg” tuples:

	flash@f0000000,0 {
		#address-cells = <1>;
		#size-cells = <1>;
		compatible = "intel,PC48F4400P0VB", "cfi-flash";
		reg = <0 0x00000000 0x02000000
		       0 0x02000000 0x02000000>;
		bank-width = <2>;
		partition@0 {
			label = "test-part1";
			reg = <0 0x04000000>;
		};
	};

An example using SRAM:

	sram@2,0 {
		compatible = "samsung,k6f1616u6a", "mtd-ram";
		reg = <2 0 0x00200000>;
		bank-width = <2>;
	};

分区的描述,

Representing flash partitions in devicetree
Partitions can be represented by sub-nodes of an mtd device. This can be used on platforms which have strong conventions about which portions of a flash are used for what purposes, but which don't use an on-flash partition table such as RedBoot. NOTE: if the sub-node has a compatible string, then it is not a partition.
#address-cells & #size-cells must both be present in the mtd device. There are two valid values for both: <1>: for partitions that require a single 32-bit cell to represent their size/address (aka the value is below 4 GiB) <2>: for partitions that require two 32-bit cells to represent their size/address (aka the value is 4 GiB or greater).
Required properties:
- reg : The partition's offset and size within the mtd bank.
Optional properties:
- label : The label / name for this partition.  If omitted, the label is taken from the node name (excluding the unit address).
- read-only : This parameter, if present, is a hint to Linux that this partition should only be mounted read-only. This is usually used for flash partitions containing early-boot firmware images or data which should not be clobbered.
Examples:
flash@0 { #address-cells = <1>;
	#size-cells = <1>;
	partition@0 { label = "u-boot";
		reg = <0x0000000 0x100000>;
		read-only; };
	uimage@100000 { reg = <0x0100000 0x200000>; }; };
flash@1 { #address-cells = <1>;
	#size-cells = <2>;
	/* a 4 GiB partition */
	partition@0 { label = "filesystem";
		reg = <0x00000000 0x1 0x00000000>; }; };
flash@2 { #address-cells = <2>;
	#size-cells = <2>;
	/* an 8 GiB partition */
	partition@0 { label = "filesystem #1";
		reg = <0x0 0x00000000 0x2 0x00000000>; };
	/* a 4 GiB partition */
	partition@200000000 { label = "filesystem #2";
		reg = <0x2 0x00000000 0x1 0x00000000>; }; }; 

测试

root@zynq:~# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 000c0000 00001000 "boot"
mtd1: 00020000 00001000 "bootenv"
mtd2: 00020000 00001000 "bootenvredund"
mtd3: 01300000 00001000 "pl"
mtd4: 00c00000 00001000 "kernel"
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值