Posts: 151
Joined: Fri Aug 26, 2011 11:51 am

Sd cards, clock settings.

Wed Jun 06, 2012 7:10 pm

Hi All, I've been trying to understand the SD card issues that everyone seems to be having, hoping that everyone might join in who's also been looking at the code etc. to try and see what is happening/can be done. If there is another thread already open, point me to it and I'll contribute there.

There seem to be various threads that are looking at SD card speeds, with people getting wildly different benchmarks on their Pi from what they're seeing on their PCs. so there is obviously an issue somewhere. Of course once you've ruled out all of the obvious stuff (is it a fake card, am I powering the pi and usb hub correctly) then the only place to start looking is at the pi and it's mmc drivers.

I've noticed that the clock speed for the emmc controller on the pi is defined as 80Mhz in the drivers (sdhci-bcm2708.c, line 77), I've also noticed that the get_max_clock function (sdhci-bcm2708.c line 362) is hardcoded to return 20Mhz for the emmc controller max clock speed. It seems that the broadcom peripherals datasheet describes valid clocks as being 12,25,50,104 and 50ddr Mhz for some SD cards? So where the 20Mhz/80Mhz values we're using on default drivers comes in useful, I'm not sure? It seems to me that we need to query the cards for their respective speeds and then try and set the clock speed accordingly? it seems to me if get_max_clock in the sdhci_bcm2708 is ever used to calculate whether we can hit the requested clock, then we're always going to get 20Mhz. 80Mhz doesn't seem like a sensible clock speed to try and use with dividers to hit the valid clocks above either.

I may be way off base here, so please someone tell me if I am :)

Also looking at bcm2708_mci.c, it's looking for some specific clock speeds too, line 436, the debug messages seem to be looking at 25,26,50,52Mhz speeds.

I don't fully understand how the different parts of the SD driver interact with each other, it seems logical that the driver should startup the card in low speed mode, query the card for it's capabilities and then set the emmc clk speed and dividers accordingly, if it's doing this automatically, is it failing because of the obscure max clock and #define BCM2708_EMMC_CLOCK_FREQ 80000000 speeds not matching sd card speeds that the emmc clock could hit? Or is there a bigger bug in the divider code?

It seems to me that the emmc interface has got a maximum clock speed that can be set, the broadcom datasheet says between 50Mhz and 100Mhz, the logical thing it seems to me would be to try and find out what speeds our cards actually can actually manage, then see if feeding those speeds with appropriate dividers directly to the emmc driver makes any difference.

I believe there is a trans_speed bit(s) in one of the SD card registers, not sure how we get at it easily though, does anyone know if it's possible to query it in userland?


User avatar
Posts: 3879
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Sd cards, clock settings.

Wed Jun 06, 2012 7:33 pm

I thought I saw a recent post about someone who built a custom kernel which clocked the R-Pi SD interface faster than 20 MHz, but I can't find it now. I'm very interested to hear comments from someone knowledgeable about this SD interface (I have no idea about it, except that a surprising number of SD cards seem to be incompatible with the R-Pi). I assume there is more going on, as from the info here about non-standard clock speeds you'd expect no SD cards would work.

By the way, would it be correct to say the 20 MHz limit is a holdover from code for MMC cards, which unlike SD cards, apparently do have a 20 MHz clock speed limit? ... ry_formats

Posts: 151
Joined: Fri Aug 26, 2011 11:51 am

Re: Sd cards, clock settings.

Wed Jun 06, 2012 9:09 pm

Yes there have been people that have clocked the emmc controller higher than 20Mhz, it's actually defined in code to 80Mhz initially but the get_max_clock function returns 20Mhz, which as you say is mmc speeds, it could be a throwback or a typo or it could be that only mmc cards use the get_max_clock function.

I personally think that a lot of people are getting what they think are SD card issues through poorly powered units, once you've got that out of the way you can start to look at speed issues, at least for me, all of my sd card errors went away, they all booted, but they all came up with various errors on different distros, sorting out the power to the pi and the usb hub fixed all of those errors.

Investigating SD cards a little bit more, there is a 128bit CSD field, this should contain the cards tran_speed or max speed bits, to get them, cat /sys/class/mmc_host/mmc0/mmc0*/csd

this will return a 32character string, the tran_speed byte is the 4th from the left end, in my case it was 32(hex), if you look at this document, ... USDHC6.pdf although my card isn't a transcend, you can see that 32h is a reasonable value to expect got a card, so it's entirely possible to just get this information properly, we should then be able to feed that information to the driver. It seems that the driver needs some kind of lookup table for speeds that it can do vs speeds of cards out there, I'm guessing there are a bunch of stock values for the trans_speed bytes.

Return to “Troubleshooting”