sunxi: dram: Tweak DEFE port setup to fix A10 screen shaking issue

When driving a 1920x1080-32@60Hz monitor, framebuffer scanout for
screen refresh becomes more bandwidth intensive and challening.
There used to be an old problem, which manifested itself as a
screen shaking effect when CPU or GPU are doing something memory
intensive and competing for the memory bandwidth with the display
controller. A possible explanation for it is that the display
controller just sends the previous scanline over HDMI in the case
if it can't read the current one in time.

Framebuffer scanout can be done either by DEBE or by DEFE. It seems
like DEBE is broken beyond repair. Apparently it is doing isolated
32 byte burst reads at regular intervals to scan out the framebuffer
and send this data over HDMI. The size of this burst is too small.
And if something else (CPU or GPU) is accessing memory at the same
time, we easily get bank conflicts with huge penalties. As these
penalties happen per 32 bytes of data, most of the memory bandwidth
is wasted. The end result is the screen shaking effect and a
significant performance loss for the CPU/GPU. This all is happening
because DEBE is configured to have higher priority than CPU and GPU.
So it can do these tiny bursts and nobody can do anything about it.
If we reduce the priority of DEBE, then the screen shaking effect
becomes much worse.

DEFE is somewhat similar to DEBE. But the key difference is that
it seems to implement some buffering of data and this buffer does
not underrun so easily. So it is possible to drop the priority
of DEFE port and make it the same as CPU/GPU. However in order to
get really perfect results, we need to also increase the "host port
command number" parameter. It is currently set to 0x10 by default
(the same as for CPU and GPU) and from what it looks, it affects
the bandwidth distribution between the ports with equal priority.
To experiment with this, I took A10-OLinuXino-LIME board (which
has a slow 16-bit memory interface) and downclocked DRAM to 408MHz.
Then used lima-memtester and glmark2-es2 programs to reproduce the
screen shaking effect. Increasing the "host port command number"
parameter resulted in a gradual reduction of the image glitches.
And at 0x50, the shaking effect disappeared completely.

So all that we need to do is a simple change "0x1035 -> 0x5031"
for the DEFE host port setup. And even A10-OLinuXino-LIME becomes
able to drive a FullHD monitor at 60Hz and 32bpp without issues
if "fb0_scaler_mode_enable" is set to 1 in the fex file.

Additionally, for A10-OLinuXino-LIME with DRAM clocked at 480MHz
and driving a 1920x1080-32@60Hz monitor in "scaler" mode, the
memset performance improves from 559.3 MB/s to 807.3 MB/s and
the memcpy performance improves from 299.3 MB/s to 317.9 MB/s
after this patch. So the memory bandwidth drain becomes smaller
the the overall system performance improves.

This has been also discussed at:
https://www.mail-archive.com/linux-sunxi@googlegroups.com/msg04085.html

Signed-off-by: Siarhei Siamashka <siarhei.siamashka@gmail.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi: fix SRAM_B/SRAM_D memory map

Move the B and D SRAM bank to their actual location (or at least
where the documentation pretends they are).

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Ian Campbell <ijc@hellion.org.uk>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi: hz parameter to clock_set_pll1 should be an unsigned int.

Requested by Marek during upstream review.

Signed-off-by: Ian Campbell <ijc@hellion.org.uk>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi: add comments to pll1_para array.

The entries must be ordered by frequency and the final entry is a catchall
which configures the maximum supported frequency.

Make pll1_para static at the same time.

Signed-off-by: Ian Campbell <ijc@hellion.org.uk>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
mksunxiboot: Fix loading of files with a size which is not a multiple of 4

We should not be aligning the amount of bytes which we try to read from the
disk, this leads to trying to read more bytes then there are which fails.

file_size is already aligned to BLOCK_SIZE before being stored in
img.header.length, so there is no need for load_size at all.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
mvtwsi: Fix clock programming

The TWSI_FREQUENCY macro was wrong in 2 ways:
1) It was casting the result of the calculations to an u8, while i2c clk
rates are often >= 100Khz which won't fit in a u8, drop the cast.
2) It had an extra factor of 2 in the divider which neither the datasheet nor
the Linux driver have.

The comment for the default value was wrongly saying that m lives in
bits 4-7, while in reality it is in bits 3-6, as can be seen from the correct
shift by 3 used in i2c_init().

While at it remove the unused twsi_actual_speed variable.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
mvtwsi: Remove unnecessary twsi_baud_rate and twsi_slave_address globals

These are used only once, so their is no need to have them global.

This also stops mvtwsi from using any bss vars making it easier to use
before dram init (to talk to the pmic to set the dram voltage).

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi: Use existing mvtwsi code for i2c master

Sunxi uses the same i2c controller, so re-use the existing code instead of
having our own duplicate code.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sun4i: Stop setting up the non-existing mbus register

sun4i does not have a mbus ccmu register.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sun5i: Drop unused AHB_CLK_SRC_AXI define

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi-mmc: Drop unused CONFIG_MMC1_PG code

And while at it make the comment for the mmc3 gpio mux the same as for the
other mmc controllers.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi-mmc: Dump debugging code

This syncs us with what is going upstream.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi: Makefile: Drop extra whitespace at end of file

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Revert "get_ram_size incorrectly returns long, should be unsigned long"

This reverts commit 7d8bc87e7e9b444a47715428df5c9a9792c92fd4.

This change has not been accepted upstream and 2G boards work fine without it,
so lets drop it.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sun4i: Remove Cubieboard_512 board

It uses the exact same memory timings as the regular cubieboard, it just
has lower density dram chips.

get_ram_size will detect that only 512 MB is available on these boards, so
they will work fine with the regular Cubieboard board config.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi: initialize DRAM after AXP

The default dcdc3 voltage is only 1.2V after reset on the Cubietruck.
This might be too low for correct DRAM controller operation at high
clock frequencies and even the initialization itself may theoretically
fail. So we initialize AXP first. And only then move on to the DRAM
setup.

Signed-off-by: Siarhei Siamashka <siarhei.siamashka@gmail.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi: Add new Merrii M2 (A20) board

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi: Add i12-tvbox board

The i12 tvbox is an A20 based android tvbox, with 1G RAM, 4G nand flash,
ap6210 sdio wifi + bt (broadcom BCM43362 sdio wifi + uart attached brcm bt),
2USB host ports using USB-A receptacles and a micro-usb receptacle for USB OTG,
and 100Mbit ethernet.

The PCB is labelled i12-a20 hence I've named the board i12-a20. It is used
in noname allwinner A20 tv-boxes, which are sometimes sold with Q5 or
QT840A as product name.

Note this patch does NOT enable the ethernet port, as the phy-power is
controller through a gpio-pin (PH21) and we don't have support for having
a gpio power pin for the phy (yet) in the GMAC driver (nor in the EMAC one).

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
sunxi: axp209: Fix setting of ldo4 voltage

The logic was wrong, resulting in us always setting the ldo to its lowest
setting of 1.25V.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Revert "gpio: Oscillate option"

This reverts commits d104fc0ee38a and af9f4057a591.

Henrik says in https://groups.google.com/forum/#!msg/linux-sunxi/nSKH4IcTaX8/YBVQLMrzfKwJ:

    GPIO_OSCILLATE is a hack in our u-boot used for measuring PIO performance.
    Should likely be removed.

This is one less thing to worry about for upstreaming, or at least removes
something from the diff...

Signed-off-by: Ian Campbell <ijc@hellion.org.uk>
Acked-by: Henrik Nordstrom <henrik@henriknordstrom.net>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>