Dobrica Pavlinušić's random unstructured stuff
CubieBoard: Revision 41
{file: BreadBoard V1.0 03-25 For CubieBoard.pdf}

{toc: }

^ A20

* Debian image sunxi_ss crypto http://www.cubieforums.com/index.php/topic,1275.0.html
* http://www.armbian.com/cubieboard-2/

^ NAND in mainline kernel

* http://linux-sunxi.org/Mainline_NAND_Howto
* http://linux-sunxi.org/MTD_Driver

Samsung K9GBG08U0A NAND

* https://groups.google.com/forum/#!topic/linux-sunxi/PxMmiCe0Deg
* https://www.netdevconf.org/2.1/papers/distributed-switch-architecture.pdf

^^ many bad blocks

http://linux-sunxi.org/Mainline_NAND_Howto#Many_bad_blocks

To fix many bad blocks issue you should:

1. Disable nand-on-flash-bbt in your dts
2. Remove this test: http://lxr.free-electrons.com/source/drivers/mtd/nand/nand_base.c?v=4.7#L2940
3. Boot your new kernel and erase chip with flash_erase /dev/mtd[0-X]
4. Re-introduce the bad block check removed in 2, re-enable nand-on-flash-bbt
and boot the new kernel

Let's try to do this with armbian.

First, enable nand overlay

.pre
dpavlin@cubieboard2:~$ cat /boot/armbianEnv.txt
overlays=nand
.pre

Then remove nand-om-flash-btt:

.pre
# backup original
cp /boot/dtb-4.14.22-sunxi/overlay/sun7i-a20-nand.dtbo /boot/dtb-4.14.22-sunxi/overlay/sun7i-a20-nand.dtbo.orig

dpavlin@cubieboard2:~$ dtc -I dtb -O dts /boot/dtb-4.14.22-sunxi/overlay/sun7i-a20-nand.dtbo | grep -v nand-on-flash-bbt > /tmp/nand.dts

root@cubieboard2:~# dtc -I dts -O dtb /tmp/nand.dts > /boot/dtb-4.14.22-sunxi/overlay/sun7i-a20-nand.dtbo
.pre

Compile armbian kernel with this patch applied

.pre
dpavlin@armbian:~/build$ cp ./patch/kernel/sunxi-dev/nand-disable-badblock-check-for-migration.patch.disabled \
./userpatches/kernel/sunxi-next/nand-disable-badblock-check-for-migration.patch
.pre

Reboot and init nand:

.pre
root@cubieboard2:~# mtd_debug info /dev/mtd0
mtd.type = MTD_MLCNANDFLASH
mtd.flags = MTD_CAP_NANDFLASH
mtd.size = 4284481536 (3G)
mtd.erasesize = 2097152 (2M)
mtd.writesize = 8192 (8K)
mtd.oobsize = 640
regions = 0

flash_erase -j /dev/mtd0 0 0

ubiformat /dev/mtd0

ubiattach -p /dev/mtd0

ubimkvol /dev/ubi0 -s 2GiB -N root

mount /dev/ubi0_0 /mnt/ -t ubifs
.pre

^^ u-boot

.pre
HELLO! BOOT0 is starting!
boot0 version : 1.5.1
dram size =1024
Succeed in opening nand flash.
Succeed in reading Boot1 file head.
The size of Boot1 is 0x0003c000.
The file stored in 0X00000000 of block 2 is perfect.
Check is correct.
Ready to disable icache.
Succeed in loading Boot1.
Jump to Boot1.
[ 0.133] boot1 version : 1.4.0
[ 0.133] pmu type = 3
[ 0.134] bat vol = 0
[ 0.161] axi:ahb:apb=3:2:2
[ 0.161] set dcdc2=1400, clock=1008 successed
[ 0.163] key
[ 0.175] no key found
[ 0.175] flash init start
[ 4.051] flash init finish
[ 4.052] fs init ok
[ 4.053] fattype FAT16
[ 4.053] fs mount ok
[ 4.060] script finish
[ 4.061] power finish
[ 4.069] BootMain start
[ 4.069] 13
[ 4.089] key value = 0
[ 4.089] recovery key high 6, low 4
[ 4.090] unable to find fastboot_key key_max value
[ 4.098] test for multi os boot with display
[ 4.100] show pic finish
[ 4.103] load kernel start
[ 4.127] load kernel successed
[ 4.127] start address = 0x4a00000

U-Boot 2011.09-rc1 (Nov 26 2012 - 14:01:52) Allwinner Technology

CPU: SUNXI Family
Board: A10-EVB
DRAM: 512 MiB
NAND: 3776 MiB
In: serial
Out: serial
Err: serial
--------fastboot partitions--------
-total partitions:11-
-name- -start- -size-
bootloader : 1000000 1000000
env : 2000000 1000000
boot : 3000000 2000000
system : 5000000 14000000
data : 19000000 20000000
misc : 39000000 1000000
recovery : 3a000000 2000000
cache : 3c000000 8000000
private : 44000000 1000000
sysrecovery : 45000000 14000000
UDISK : 59000000 93000000
-----------------------------------
Hit any key to stop autoboot: 0
sunxi#
.pre

^^^ compile

.pre
dpavlin@klin:/klin/u-boot$ cp configs/Cubieboard_defconfig .config

# add NAND, MTD options, A10 for cubieboard1

# compile while defining flash:

dpavlin@klin:/klin/u-boot$ cat build-cubieboard.sh
make V=1 CONFIG_NAND_SUNXI_SPL_ECC_STRENGTH=40 CONFIG_SYS_NAND_PAGE_SIZE=4036 CONFIG_SYS_NAND_OOBSIZE=640 CONFIG_SYS_NAND_BLOCK_SIZE=0x100000


dpavlin@cubieboard:~$ scp 10.60.0.92:/klin/u-boot/u-boot*spl* .

dpavlin@cubieboard:~$ sudo dd if=u-boot-sunxi-with-spl.bin of=/dev/mmcblk0 bs=1024 seek=8
488+1 records in
488+1 records out
500154 bytes (500 kB) copied, 0.0423498 s, 11.8 MB/s

.pre

^ pinout

* http://docs.cubieboard.org/cubieboard1_and_cubieboard2_gpio_pin

^^ U14 (Next to SATA connector)

| | *SPI0* |
| 48 | PI13 (SPI0-MISO/UART6-RX/EINT25) | 47 | PI11 (SPI0-CLK/UART5-RX/EINT23) |
| 46 | PI12 (SPI0-MOSI/UART6-TX/EINT24) | 45 | PI10 (SPI0-CS/UART5-TX/EINT22) |
| | *LCD* |
| 44 | 3.3V (nc in 2012-08-08) | 43 | VCC-5V |
| 42 | Ground | 41 | SPDIF |
| 40 | PB10 (LCD0-SCK/LCD-PIO1) | 39 | PB11 (LCD0-SDA/LCD-PIO2) |
| 38 | Ground | 37 | PH7 (LCD0-BL-EN/LCD-PIO0/UART5-RX/EINT7) |
| 36 | XN_TP (TP-X2) | 35 | YN_TP (TP-Y2) |
| 34 | XP_TP (TP-X1) | 33 | YP_TP (TP-Y1) |
| 32 | PD25 (LCDDE) | 31 | PB2 (PWM0) |
| 30 | PD26 (LCDHSYNC)-VGA-HSYNC | 29 | PD24 (LCDCLK) |
| 28 | PD23 (LCDD23) | 27 | PD27 (LCDVSYNC)-VGA-VSYNC |
| 26 | PD21 (LCDD21) | 25 | PD22 (LCDD22) |
| 24 | PD19 (LCDD19/LVDS1N3) | 23 | PD20 (LCDD20) |
| 22 | PD17 (LCDD17/LVDS1NC) | 21 | PD18 (LCDD18/LVDS1P3) |
| 20 | Ground | 19 | PD16 (LCDD16/LVDS1PC) |
| 18 | PD14 (LCDD14/LVDS1P2) | 17 | PD15 (LCDD15/LVDS1N2) |
| 16 | PD12 (LCDD12/LVDS1P1) | 15 | PD13 (LCDD13/LVDS1N1) |
| 14 | PD10 (LCDD10/LVDS1P0) | 13 | PD11 (LCDD11/LVDS1N0) |
| 12 | PD8 (LCDD8/LVDS0P3) | 11 | PD9 (LCDD9/LVDS0N3) |
| 10 | PD7 (LCDD7/LVDS0NC) | 9 | Ground |
| 8 | PD5 (LCDD5/LVDS0N2) | 7 | PD6 (LCDD6/LVDS0PC) |
| 6 | PD3 (LCDD3/LVDS0N1) | 5 | PD4 (LCDD4/LNVS0P2) |
| 4 | PD1 (LCDD1/LVDS0N0) | 3 | PD2 (LCDD2/LVDS0P1) |
| 2 | Ground | 1 | PD0 (LCDD0/LVDSP0) |

^^ U15 (Between Ethernet port and USB ports)

| | *CSI1/TS* |
| 1 | VCC-5V | 2 | PH15 (CSI1-PWR/EINT15) |
| 3 | CSI1-IO-2V8 | 4 | PH14 (CSI1-RST#/EINT14) |
| 5 | PG0 (CSI1-PCLK/SDC1-CMD) | 6 | PB18 (TWI1-SCK) |
| 7 | PB19 (TWI1-SDA) | 8 | PG3 (CSI1-VSYNC/SDC1-D1) |
| 9 | PG2 (CSI1-HSYNC/SDC1-D0) | 10 | PG1 (CSI1-MCLK/SDC1-CLK) |
| 11 | PG4 (CSI1-D0/SDC1-D2) | 12 | PG5 (CSI1-D1/SDC1-D3) |
| 13 | PG6 (CSI1-D2/UART3-TX) | 14 | PG7 (CSI1-D3/UART3-RX |
| 15 | PG8 (CSI1-D4/UART3-RTS) | 16 | PG9 (CSI1-D5/UART3-CTS) |
| 17 | PG10 (CSI1-D6/UART4-TX) | 18 | PG11 (CSI1-D7/UART4-RX) |
| 19 | Ground | 20 | Ground |
| | *Analog* *SDIO3* |
| 21 | FMINL | 22 | PI4 (SDC3-CMD) |
| 23 | FMINR | 24 | PI5 (SDC3-CLK) |
| 25 | Ground | 26 | PI6 (SDC3-D0) |
| 27 | VGA-R | 28 | PI7 (SDC3-D1) |
| 29 | VGA-G | 30 | PI8 (SDC3-D2) |
| 31 | VGA-B | 32 | PI9 (SDC3-D3) |
| | *CSI0/TS* |
| 33 | LCD1-VSYNC | 34 | PE4 (CSI0-D0) |
| 35 | LCD1-HSYNC | 36 | PE5 (CSI0-D1) |
| 37 | Ground | 38 | PE6 (CSI0-D2) |
| 39 | AVCC | 40 | PE7 (CSI0-D3) |
| 41 | LRADC0 | 42 | PE8 (CSI0-D4) |
| 43 | CVBS | 44 | PE9 (CSI0-D5) |
| 45 | HPL | 46 | PE10 (CSI0-D6) |
| 47 | HPR | 48 | PE11 (CSI0-D7) |

----

everything below line is for legacy kernel and quite old

^ disk speed

^^ NAND

.pre
root@cubieboard2:/home/dpavlin# uname -a
Linux cubieboard2 3.4.109-sun7i #4 SMP PREEMPT Sun Oct 11 14:32:15 CEST 2015 armv7l GNU/Linux
root@cubieboard2:/home/dpavlin# hdparm -t /dev/nand

/dev/nand:
Timing buffered disk reads: 32 MB in 3.22 seconds = 9.94 MB/sec

# this is different, faster sdcard
root@cubieboard2:~# uname -a
Linux cubieboard2 4.3.3-sunxi #3 SMP Mon Dec 28 11:27:16 CET 2015 armv7l GNU/Linux
root@cubieboard2:~# hdparm -tT /dev/mmcblk0

/dev/mmcblk0:
Timing cached reads: 770 MB in 2.00 seconds = 384.59 MB/sec
Timing buffered disk reads: 60 MB in 3.05 seconds = 19.67 MB/sec


.pre

^^ SATA

Powered by 2A Nexus 7 power supply (for SATA)

https://groups.google.com/d/searchin/cubieboard/sata/cubieboard/hKJgJneGVmQ/xme3w4Y7XtsJ?fromplusone=1

.pre
root@debian:~/sunxi-tools# ./bin2fex /boot/script.bin script.fex
fexc-bin: /boot/script.bin: version: 0.1.2
fexc-bin: /boot/script.bin: size: 42144 (76 sections)

root@debian:~/sunxi-tools# cp script.fex script+sata.fex

root@debian:~/sunxi-tools# diff -urw script.fex script+sata.fex
--- script.fex 2013-05-30 10:41:13.979510762 +0000
+++ script+sata.fex 2013-05-30 10:42:36.380244101 +0000
@@ -595,7 +595,7 @@

[sata_para]
sata_used = 1
-sata_power_en =
+sata_power_en = port:PB08<1><default><default><0>

[mmc0_para]
sdc_used = 1

root@debian:~/sunxi-tools# ./fex2bin script+sata.fex /boot/script.bin

root@debian:/etc# git diff
diff --git a/modules b/modules
index d8894c3..9e65a80 100644
--- a/modules
+++ b/modules
@@ -4,3 +4,4 @@
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

+sw_ahci_platform
.pre

Reboot to activate changes and test speed (using 80G intel SSD to have sense of maximum performance)

.pre
root@debian:~# hdparm -i /dev/sda

/dev/sda:

Model=INTEL SSDSA2M080G2GC, FwRev=2CV102HD, SerialNo=CVPO035101VN080JGN
Config={ Fixed }
RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=0
BuffType=unknown, BuffSize=unknown, MaxMultSect=16, MultSect=1
CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=156301488
IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
PIO modes: pio0 pio3 pio4
DMA modes: mdma0 mdma1 mdma2
UDMA modes: udma0 udma1 udma2 udma3 udma4 udma5 *udma6
AdvancedPM=no WriteCache=enabled
Drive conforms to: ATA/ATAPI-7 T13 1532D revision 1: ATA/ATAPI-2,3,4,5,6,7

* signifies the current active mode

root@debian:~# hdparm -tT /dev/sda

/dev/sda:
Timing cached reads: 784 MB in 2.00 seconds = 392.08 MB/sec
Timing buffered disk reads: 460 MB in 3.00 seconds = 153.32 MB/sec
.pre

And test with disk:

.pre
root@debian:~# hdparm -i /dev/sda

/dev/sda:

Model=ST96812AS, FwRev=7.24, SerialNo=3PJ1GCKE
Config={ HardSect NotMFM HdSw>15uSec Fixed DTR>10Mbs RotSpdTol>.5% }
RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=4
BuffType=unknown, BuffSize=8192kB, MaxMultSect=16, MultSect=off
CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=117231408
IORDY=on/off, tPIO={min:240,w/IORDY:120}, tDMA={min:120,rec:120}
PIO modes: pio0 pio1 pio2 pio3 pio4
DMA modes: mdma0 mdma1 mdma2
UDMA modes: udma0 udma1 udma2 udma3 udma4 *udma5
AdvancedPM=yes: unknown setting WriteCache=enabled
Drive conforms to: Unspecified: ATA/ATAPI-1,2,3,4,5,6,7

* signifies the current active mode

root@debian:~# hdparm -tT /dev/sda

/dev/sda:
Timing cached reads: 746 MB in 2.00 seconds = 372.90 MB/sec
Timing buffered disk reads: 120 MB in 3.03 seconds = 39.56 MB/sec
.pre

^ GPIO

* https://github.com/gootoomoon/WiringCB-python

^ Camera

[OV7670] is supported http://linux-sunxi.org/CSI

* https://groups.google.com/forum/#!msg/linux-sunxi/DYWwfUFgVlw/_7A5APHyl7UJ
* https://groups.google.com/forum/#!msg/cubieboard/SWzC9qvpVL8/9cjQ_CPHQzUJ

^ hardware

* http://linux-sunxi.org/Cubieboard/ExpansionPorts
* https://linux-sunxi.org/A20/PIO

pinout: {image: gpio_defination_large.jpg}

https://www.olimex.com/wiki/How_to_add_pwm

.pre
cat /sys/class/pwm-sunxi/pwm0/pin
PB2

echo 10khz > /sys/class/pwm-sunxi/pwm0/period

echo 25 > /sys/class/pwm-sunxi/pwm0/duty_percent

echo 1 > /sys/class/pwm-sunxi/pwm0/run
.pre

^^ LEDs

.pre
dpavlin@cubieboard2:~$ grep . /sys/class/leds/*/trigger
/sys/class/leds/blue:ph21:led2/trigger:none battery-charging-or-full battery-charging battery-full battery-charging-blink-full-solid ac-online usb-online mmc0 timer [heartbeat] backlight cpu0 cpu1 default-on
/sys/class/leds/green:ph20:led1/trigger:[none] battery-charging-or-full battery-charging battery-full battery-charging-blink-full-solid ac-online usb-online mmc0 timer heartbeat backlight cpu0 cpu1 default-on
.pre

^ forum

* ov7670 http://www.cubieforums.com/index.php/topic,3823.0.html
* LVDS http://www.cubieforums.com/index.php/topic,3908.0.html

^ ina219

^^ device tree

* https://github.com/dpavlin/sunxi-DT-overlays/blob/cubieboard2/examples/i2c-ina219.dts

.pre
dpavlin@cubieboard2:~$ sensors
ina219-i2c-1-40
Adapter: mv64xxx_i2c adapter
in0: +0.04 V
in1: +4.64 V
power1: 66.88 W
curr1: +13.64 A

iio_hwmon-isa-0000
Adapter: ISA adapter
temp1: +33.6�C

root@cubieboard2:~# dmesg | grep ina | tail -1
[ 6597.685287] ina2xx 1-0040: power monitor ina219 (Rshunt = 100 uOhm)

# hum, it should be 10uA, so I changed that in device tree

dpavlin@cubieboard2:~$ dmesg | grep ina219
[ 11.594536] ina2xx 1-0040: power monitor ina219 (Rshunt = 10 uOhm)
dpavlin@cubieboard2:~$ sensors
ina219-i2c-1-40
Adapter: mv64xxx_i2c adapter
in0: +0.02 V
in1: +4.75 V
power1: 85.00 W
curr1: +17.97 A

iio_hwmon-isa-0000
Adapter: ISA adapter
temp1: +32.5�C

# still wrong current!

.pre

^^ user-land C

User-land implementation which works: https://github.com/ZigFisher/Glutinium/blob/master/i2c-telemetry/src/ina219.c

modified a little to provide full timestamp and fflush: {file: ina219.c}

.pre
dpavlin@cubieboard2:~$ ./ina219 -b 1 -i 1
2017-10-23T12:40:24 4744mV 262.2mA
2017-10-23T12:40:26 4756mV 249.1mA
2017-10-23T12:40:27 4776mV 223.0mA
2017-10-23T12:40:28 4772mV 223.8mA
2017-10-23T12:40:29 4760mV 224.0mA
2017-10-23T12:40:30 4768mV 223.0mA
2017-10-23T12:40:31 4772mV 223.1mA
2017-10-23T12:40:32 4748mV 224.6mA
2017-10-23T12:40:33 4776mV 223.1mA
2017-10-23T12:40:34 4768mV 223.5mA
.pre