- Table of contents
- Example SOM OSPI NOR
Example SOM OSPI NOR¶
Goal¶
The goal of this example is to demonstrate how to use the SOM Octal SPI (OSPI) / Quad SPI (QSPI) NOR Flash storage. Note: not all SOMs include SPI NOR Flash. Check the model number and datasheet to confirm that SPI NOR Flash is available on your device.
This storage can be used for:
- General purpose storage.
- As a boot device
General Purpose Storage¶
Prerequisites¶
- The Device needs to be divided into one or more partitions.
Steps¶
Loading the overlay¶
SPI is not configured by default and must be loaded as an overlay.
Add the correct line to the uEnv.txt file (/run/media/boot-mmcblk1p1/uEnv.txt
).
OSPI: name_overlays=k3-am62x-mitysom-ospi-mt35.dtbo
QSPI: name_overlays=k3-am62x-mitysom-qspi-mt25.dtso
Creating a partition¶
The SPI NOR device partitions should appear as /dev/mtdX, where X is a number starting from 0.
In linux, the SPI NOR device partitions are defined in the kernel device tree block. The default device tree block for the MitySOM uses the following device tree entry for the SPI device. If you need a different partition mapping, you will need to create a new device tree block with the necessary changes.
OSPI:
&ospi0 { pinctrl-names = "default"; pinctrl-0 = <&ospi0_pins_default>; flash@0 { compatible = "jedec,spi-nor"; reg = <0x0>; spi-tx-bus-width = <8>; spi-rx-bus-width = <8>; spi-max-frequency = <25000000>; cdns,tshsl-ns = <60>; cdns,tsd2d-ns = <60>; cdns,tchsh-ns = <60>; cdns,tslch-ns = <60>; cdns,read-delay = <4>; partitions { compatible = "fixed-partitions"; #address-cells = <1>; #size-cells = <1>; partition@0 { label = "ospi.tiboot3"; reg = <0x0 0x80000>; }; partition@80000 { label = "ospi.tispl"; reg = <0x80000 0x200000>; }; partition@280000 { label = "ospi.u-boot"; reg = <0x280000 0x400000>; }; partition@680000 { label = "ospi.env"; reg = <0x680000 0x40000>; }; partition@6c0000 { label = "ospi.env.backup"; reg = <0x6c0000 0x40000>; }; partition@800000 { label = "ospi.rootfs"; reg = <0x800000 0xf7c0000>; }; partition@3fc0000 { label = "ospi.phypattern"; reg = <0xffc0000 0x40000>; }; }; }; };
QSPI:
&ospi0 { pinctrl-names = "default"; pinctrl-0 = <&ospi0_pins_default>; flash@0 { compatible = "n25q128a11", "jedec,spi-nor"; reg = <0x0>; spi-tx-bus-width = <8>; spi-rx-bus-width = <8>; mp25,fast-read; spi-max-frequency = <50000000>; cdns,tshsl-ns = <60>; cdns,tsd2d-ns = <60>; cdns,tchsh-ns = <60>; cdns,tslch-ns = <60>; cdns,read-delay = <2>; partitions { compatible = "fixed-partitions"; #address-cells = <1>; #size-cells = <1>; partition@0 { label = "ospi.tiboot3"; reg = <0x0 0x80000>; }; partition@80000 { label = "ospi.tispl"; reg = <0x80000 0x200000>; }; partition@280000 { label = "ospi.u-boot"; reg = <0x280000 0x400000>; }; partition@680000 { label = "ospi.env"; reg = <0x680000 0x40000>; }; partition@6c0000 { label = "ospi.env.backup"; reg = <0x6c0000 0x40000>; }; }; }; };
When booted to linux, you can check the mtd partition configuration by dumping out the /proc/mtd file:
OSPI:
root@mitysom-am62x:~# cat /proc/mtd dev: size erasesize name mtd0: 00080000 00020000 "ospi.tiboot3" mtd1: 00200000 00020000 "ospi.tispl" mtd2: 00400000 00020000 "ospi.u-boot" mtd3: 00040000 00020000 "ospi.env" mtd4: 00040000 00020000 "ospi.env.backup" mtd5: 0f7c0000 00020000 "ospi.rootfs" mtd6: 00040000 00020000 "ospi.phypattern" root@mitysom-am62x:~#
QSPI:
root@mitysom-am62x:~# cat /proc/mtd dev: size erasesize name mtd0: 00080000 00020000 "ospi.tiboot3" mtd1: 00200000 00020000 "ospi.tispl" mtd2: 00400000 00020000 "ospi.u-boot" mtd3: 00040000 00020000 "ospi.env" mtd4: 00040000 00020000 "ospi.env.backup" root@mitysom-am62x:~#
Raw Access to a partition with mtd_utils¶
NOTE: As the following instructions are written for OSPI, but can be modified to work with QSPI.
The reference Linux image includes mtd_utils, which support accessing low level FLASH devices. Example commands for accessing the memory are shown below.
root@mitysom-am62x:~# cat /proc/mtd /* Should list QSPI partitions */ root@mitysom-am62x:~# flash_erase /dev/mtd5 0 0 /* Erase entire /dev/mtd6 */ root@mitysom-am62x:~# dd if=/dev/random of=tmp_write.txt bs=1 count=1048576 /* 1M Bytes to write to flash */ root@mitysom-am62x:~# mtd_debug write /dev/mtd5 0 1048576 tmp_write.txt /* write to num bytes to flash */ root@mitysom-am62x:~# mtd_debug read /dev/mtd5 0 1048576 tmp_read.txt /* /* read to num bytes to flash */ root@mitysom-am62x:~# diff tmp_read.txt tmp_write.txt /* should be NULL */
Formatting a partition¶
If desired, a partition can formatted to support a standard filesystem. For NOR Flash devices, the UBIFS filesystem is recommended.
To format the partition with a UBIFS filesystem, run the following command.
root@mitysom-am62x:~# ubiformat /dev/mtd5 ubiformat: mtd5 (nor), size 259784704 bytes (247.7 MiB), 1982 eraseblocks of 131072 bytes (128.0 KiB), min. I/O size 1 bytes libscan: scanning eraseblock 1981 -- 100 % complete ubiformat: 172 eraseblocks have valid erase counter, mean value is 0 ubiformat: 3 eraseblocks are supposedly empty ubiformat: warning!: 1807 of 1982 eraseblocks contain non-UBI data ubiformat: continue? (y/N) y ubiformat: warning!: only 172 of 1982 eraseblocks have valid erase counter ubiformat: erase counter 0 will be used for all eraseblocks ubiformat: note, arbitrary erase counter value may be specified using -e option ubiformat: continue? (y/N) y ubiformat: use erase counter 0 for all eraseblocks ubiformat: formatting eraseblock 1981 -- 100 % complete root@mitysom-am62x:~# ubiattach -p /dev/mtd5 [ 3192.529763] ubi0: attaching mtd5 [ 3192.649796] ubi0: scanning is finished [ 3192.658088] ubi0: attached mtd5 (name "ospi.rootfs", size 247 MiB) [ 3192.664330] ubi0: PEB size: 131072 bytes (128 KiB), LEB size: 130944 bytes [ 3192.671290] ubi0: min./max. I/O unit sizes: 1/256, sub-page size 1 [ 3192.677781] ubi0: VID header offset: 64 (aligned 64), data offset: 128 [ 3192.684483] ubi0: good PEBs: 1982, bad PEBs: 0, corrupted PEBs: 0 [ 3192.690631] ubi0: user volume: 0, internal volumes: 1, max. volumes count: 128 [ 3192.697916] ubi0: max/mean erase counter: 0/0, WL threshold: 4096, image sequence number: 1932733684 [ 3192.707074] ubi0: available PEBs: 1978, total reserved PEBs: 4, PEBs reserved for bad PEB handling: 0 [ 3192.716316] ubi0: background thread "ubi_bgt0d" started, PID 1495 UBI device number 0, total 1982 LEBs (259531008 bytes, 247.5 MiB), available 1978 LEBs (259007232 bytes, 247.0 MiB), LEB size 130944 bytes (127.8 KiB) root@mitysom-am62x:~# ubimkvol /dev/ubi0 -N flash_fs -s 200MiB Volume ID 0, size 1602 LEBs (209772288 bytes, 200.0 MiB), LEB size 130944 bytes (127.8 KiB), dynamic, name "flash_fs", alignment 1 root@mitysom-am62x:~# mkdir /mnt/flash
Mounting / locating the filesystem¶
root@mitysom-am62x:~# mount -t ubifs ubi0:flash_fs /mnt/flash [ 3321.243879] UBIFS (ubi0:0): default file-system created [ 3321.249558] UBIFS (ubi0:0): Mounting in unauthenticated mode [ 3321.255521] UBIFS (ubi0:0): background thread "ubifs_bgt0_0" started, PID 1516 [ 3321.292605] UBIFS (ubi0:0): UBIFS: mounted UBI device 0, volume 0, name "flash_fs" [ 3321.300247] UBIFS (ubi0:0): LEB size: 130944 bytes (127 KiB), min./max. I/O unit sizes: 8 bytes/256 bytes [ 3321.311310] UBIFS (ubi0:0): FS size: 208462848 bytes (198 MiB, 1592 LEBs), journal size 10475520 bytes (9 MiB, 80 LEBs) [ 3321.323310] UBIFS (ubi0:0): reserved for root: 4952683 bytes (4836 KiB) [ 3321.331267] UBIFS (ubi0:0): media format: w5/r0 (latest is w5/r0), UUID 8B1E5D53-CB40-47F7-87F0-FA72C87F644C, small LPT model root@mitysom-am62x:~# ls /mnt/flash root@mitysom-am62x:~# cd /mnt/flash root@mitysom-am62x:/mnt/flash# df -h . Filesystem Size Used Avail Use% Mounted on ubi0:flash_fs 184M 16K 179M 1% /mnt/flash
Test Accessing the Device through the filesystem¶
To run a simple write and read test, run the following commands:
root@mitysom-am62x:/mnt/flash# dd if=/dev/urandom of=foo.bin bs=1024 count=1024 1024+0 records in 1024+0 records out 1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.0230557 s, 45.5 MB/s root@mitysom-am62x:/mnt/flash# md5sum foo.bin 4b25e023bcdff31ae21fc892535af659 foo.bin root@mitysom-am62x:/mnt/flash# ls -ltr foo.bin -rw-r--r-- 1 root root 1048576 Feb 22 14:14 foo.bin root@mitysom-am62x:/mnt/flash#
Boot Device¶
See The OSPI Boot Option Page for information on setting up the MitySOM-AM62 to boot from OSPI.
Conclusion¶
This example has demonstrated how to use the OSPI NOR Flash as a regular filesystem and how it could be used as a boot device.
Additional information about the OSPI device support can be found on TI's Processor SDK Site
Go to top