oscarshiang
Posts: 3
Joined: Sat Mar 27, 2021 2:08 pm

Read/write SD card on Raspberry Pi 4?

Sun Mar 28, 2021 4:19 am

Hello all,

I am currently developing a kernel on Raspberry Pi 4.

I followed this tutorial https://github.com/bztsrc/raspi3-tutorial but failed when I try to read and write something to the SD card.
It seems to have some problem when sending the eMMC command to the SD card module.

I am wondering is there any implementation of reading/writing content of SD card on Raspberry Pi 4?

Oscar

Herbie-C
Posts: 70
Joined: Sat Jun 27, 2020 4:08 pm

Re: Read/write SD card on Raspberry Pi 4?

Tue Mar 30, 2021 5:56 pm

Are you sure your SD-card is in writeable mode?
I hate programming.
I hate programming.
Holy sh*t it works!
I love programming.

cleverca22
Posts: 7282
Joined: Sat Aug 18, 2012 2:33 pm

Re: Read/write SD card on Raspberry Pi 4?

Tue Mar 30, 2021 9:53 pm

Code: Select all

#define EMMC_ARG2           ((volatile unsigned int*)(MMIO_BASE+0x00300000))
from the code in the link

Image
from my own research

that code, is for driving the old SDHCI controller
on the pi3, it could be muxed to either wifi or SD via the normal gpio altfunc array, which that code is doing via sd_init()

but on the pi4, the SD slot isnt part of the gpio array anymore
there is a new high-speed mux dedicated to the SD interface, that will switch it between the old SDHCI and the new EMMC2

both are following the same standard, and should work with the same drivers
https://github.com/librerpi/rpi-open-fi ... xt#L63-L78

you either need to use the new mux to route the SD slot to SDHCI, or modify the driver to use EMMC2's base address

rst
Posts: 534
Joined: Sat Apr 20, 2013 6:42 pm
Location: Germany

Re: Read/write SD card on Raspberry Pi 4?

Fri Apr 02, 2021 8:31 pm

I was trying for months to get my SD card driver working on the RPi 4. The relatively obvious modifications required are to use the base address of EMMC2 at 0xFE340000 and to fetch the base clock rate from the firmware with clock ID 12 for EMMC2. But this was not enough. Then I found that there is difference between the EMMC controllers of the RPi 1-3 and RPi 4. The RPi 4's controller is more compliant with the standard and requires to set the SD bus power VDD1 to 3.3V at initialization, which was not needed on the RPi 1-3, like that:

Code: Select all

#define EMMC_CONTROL0	(0xFE340000 + 0x28)

u32 control0 = read32 (EMMC_CONTROL0);
control0 |= 0x0F << 8;
write32 (EMMC_CONTROL0, control0);

cleverca22
Posts: 7282
Joined: Sat Aug 18, 2012 2:33 pm

Re: Read/write SD card on Raspberry Pi 4?

Fri Apr 02, 2021 9:04 pm

rst wrote:
Fri Apr 02, 2021 8:31 pm
The RPi 4's controller is more compliant with the standard and requires to set the SD bus power VDD1 to 3.3V at initialization,
there are also 2 external GPIO lines, to control the IO voltage and SD power, which must be accessed via the mailbox

the engineers have been a bit dodgy with questions, but i think the io voltage one is using a mosfet to switch an IOREF pin between 1.8v and 3.3v
that IOREF goes back into the SoC, and sets the level for the SD pins coming back out

the SD power gpio, then just lets you enable/disable the main 3.3v supply to the uSD socket, but earlier revisions lack that
once a card is in 1.8v mode, you have to power-cycle it to get it back into 3.3v mode
the official firmware does that for you, and runs your kernel while the card is still in a 3.3v mode i believe

rst
Posts: 534
Joined: Sat Apr 20, 2013 6:42 pm
Location: Germany

Re: Read/write SD card on Raspberry Pi 4?

Sat Apr 03, 2021 5:14 am

cleverca22 wrote:
Fri Apr 02, 2021 9:04 pm
there are also 2 external GPIO lines, to control the IO voltage and SD power, which must be accessed via the mailbox

Yes, I forgot to mention, that the driver is disabling the 1.8V power, by writing a 0 to the external GPIO pin 4 using the property mailbox function 0x38041, because it is not using the 1.8V. I added 0x80 to the pin number for the external GPIO.

Not sure, if it is really necessary, but the driver works on a RPi 4B v1.1 and RPi 400 here and I did not hear about problems with it.

bzt
Posts: 635
Joined: Sat Oct 14, 2017 9:57 pm

Re: Read/write SD card on Raspberry Pi 4?

Fri Apr 09, 2021 7:06 pm

Hi @rst,

do you have a working source code somewhere?

I've searched for 0x38041 in Circle to see what mailbox call you were referring to, but I had no luck finding it.

In order to make my code RPi4 compatible, I've changed the address to eMMC2, I've added the voltage config in EMMC_CONTROL0 you mentioned, but it is unclear what to do with these mailbox calls (my code for RPi3 does not query the eMMC freq, it only sets the speed by writing registers. Is this going to be a problem on RPi4?) Sadly I don't have a RPi4 board, so I'm flying blind.

Cheers,
bzt

rst
Posts: 534
Joined: Sat Apr 20, 2013 6:42 pm
Location: Germany

Re: Read/write SD card on Raspberry Pi 4?

Fri Apr 09, 2021 8:49 pm

bzt wrote:
Fri Apr 09, 2021 7:06 pm
do you have a working source code somewhere?
Hi @bzt, here it is.
I've searched for 0x38041 in Circle to see what mailbox call you were referring to, but I had no luck finding it.
Try 0x00038041 and it will work. ;)
In order to make my code RPi4 compatible, I've changed the address to eMMC2, I've added the voltage config in EMMC_CONTROL0 you mentioned, but it is unclear what to do with these mailbox calls (my code for RPi3 does not query the eMMC freq, it only sets the speed by writing registers. Is this going to be a problem on RPi4?) Sadly I don't have a RPi4 board, so I'm flying blind.
It's probably not easy to get this working without the hardware. :(

Cheers

bzt
Posts: 635
Joined: Sat Oct 14, 2017 9:57 pm

Re: Read/write SD card on Raspberry Pi 4?

Sat Apr 10, 2021 3:39 pm

rst wrote:
Fri Apr 09, 2021 8:49 pm
Hi @bzt, here it is.
Thank you!!!
rst wrote:
Fri Apr 09, 2021 8:49 pm
Try 0x00038041 and it will work. ;)
Ahhh, I've tried 38041 too but that's not working either since github search only matches whole words as it turned out... Thank you!
rst wrote:
Fri Apr 09, 2021 8:49 pm
It's probably not easy to get this working without the hardware. :(
Yes, absolutely. I miss the trial-and-error experiments very much. I'll try to learn from your code, adapt that to my code and then get an RPi4 somehow to actually test if it's working.

Cheers,
bzt

Return to “Bare metal, Assembly language”