12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951 |
- /*****************************************************************************
- *
- * Copyright Andes Technology Corporation 2007-2008
- * All Rights Reserved.
- *
- * Revision History:
- *
- * Aug.21.2007 Created.
- ****************************************************************************/
- /*****************************************************************************
- *
- * FILE NAME VERSION
- *
- * sdd.c
- *
- * DESCRIPTION
- *
- * SD driver implementation. (Nucleus I/O Driver Architecture)
- *
- * SDC CONTROL LOGIC
- *
- * -------------------------------------------------------------------------
- * SDC controls <-> SDC Registers <-> SD In-Card Controller
- * -------------------------------------------------------------------------
- * SD commands <-> SDC CMD/RSP reg <-> SD Command/Data Line
- * -------------------------------------------------------------------------
- * SD data <-> SDC Data Window <-> SD Memory
- * -------------------------------------------------------------------------
- *
- * DATA STRUCTURES
- *
- * None
- *
- * DEPENDENCIES
- *
- * sdd.h SD driver common header file
- *
- ****************************************************************************/
- #include "sdd.h"
- #include "sdd_sd.h"
- #include "../../library/ndsvfs/sys_arch.h"
- #include "../../library/ndsvfs/include/ndsbdev.h"
- #include "bsp_hal.h"
- static uint32_t sdd_hisr_stack[SDD_HISR_STACK_SIZE];
- /* driver context */
- static SDD_DATA sdd __attribute__ ((aligned(4))) = { 0};
- #if (SDD_VFS_SUPPORT)
- // sdc block device context
- static NDS_BDEV sdd_bdev __attribute__((aligned(4))) = {0};
- #endif // SDD_VFS_SUPPORT
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_alloc_dma_channel
- *
- * DESCRIPTION
- *
- *
- * This function allocate a dma channel for use of sd data transfer.
- *
- * NOTE
- *
- *
- * INPUTS
- *
- * None
- *
- * OUTPUTS
- *
- * None
- *
- ****************************************************************************/
- static inline uint32_t _sdd_alloc_dma_channel(void)
- {
- uint32_t status = HAL_SUCCESS;
- /*
- * This is function is for code path simplification so it will not
- * check validity of sdd struct again.
- */
- /* Try APB DMA first ... */
- /* (in) DMAD_DMAC_AHB_CORE, DMAD_DMAC_APB_CORE */
- sdd.dma_ch.controller = DMAD_DMAC_APB_CORE;
- /* (in) Burst mode (0: no burst 1-, 1: burst 4- data cycles per dma cycle) */
- sdd.dma_ch.apbch_req.burst_mode = 0;
- /* (in) APBBR_DATAWIDTH_4(word), APBBR_DATAWIDTH_2(half-word), APBBR_DATAWIDTH_1(byte) */
- sdd.dma_ch.apbch_req.data_width = APBBR_DATAWIDTH_4;
- /* (in) APBBR_ADDRINC_xxx */
- sdd.dma_ch.apbch_req.src_addr_ctrl = APBBR_ADDRINC_FIXED;
- /* (in) APBBR_REQN_xxx (also used to help determine bus selection) */
- sdd.dma_ch.apbch_req.src_index = _dmad_get_reqn(sdd.dma_ch.controller, APB_SDC);
- /* (in) APBBR_ADDRINC_xxx */
- sdd.dma_ch.apbch_req.dst_addr_ctrl = APBBR_ADDRINC_I4X;
- /* (in) APBBR_REQN_xxx (also used to help determine bus selection) */
- sdd.dma_ch.apbch_req.dst_index = APBBR_REQN_NONE;
- status = _dmad_channel_alloc(&sdd.dma_ch, HAL_TRUE);
- if (status != HAL_SUCCESS) {
- if (status != HAL_ERR_UNAVAILABLE)
- return status;
- /* Try AHB DMAC again for lucky ... */
- /* (in) DMAD_DMAC_AHB_CORE, DMAD_DMAC_APB_CORE */
- sdd.dma_ch.controller = DMAD_DMAC_AHB_CORE;
- /* (in) non-zero if src and dst have different clock domain */
- sdd.dma_ch.ahbch_req.sync = 1;
- /* (in) DMAC_CSR_CHPRI_0 (lowest) ~ DMAC_CSR_CHPRI_3 (highest) */
- sdd.dma_ch.ahbch_req.priority = DMAC_CSR_CHPRI_0;
- /* (in) non-zero to enable hardware handshake mode */
- sdd.dma_ch.ahbch_req.hw_handshake = 1;
- /* (in) DMAC_CSR_SIZE_1 ~ DMAC_CSR_SIZE_256 */
- sdd.dma_ch.ahbch_req.burst_size = DMAC_CSR_SIZE_1;
- /* (in) DMAC_CSR_WIDTH_8, DMAC_CSR_WIDTH_16, or DMAC_CSR_WIDTH_32 */
- sdd.dma_ch.ahbch_req.src_width = DMAC_CSR_WIDTH_32;
- /* (in) DMAC_CSR_AD_INC, DMAC_CSR_AD_DEC, or DMAC_CSR_AD_FIX */
- sdd.dma_ch.ahbch_req.src_addr_ctrl = DMAC_CSR_AD_FIX;
- /* (in) DMAC_REQN_xxx (also used to help determine channel number) */
- sdd.dma_ch.ahbch_req.src_index = DMAC_REQN_SDC;
- sdd.dma_ch.ahbch_req.src_reqn = _dmad_get_reqn(sdd.dma_ch.controller, AHB_SDC);
- /* (in) DMAC_CSR_WIDTH_8, DMAC_CSR_WIDTH_16, or DMAC_CSR_WIDTH_32 */
- sdd.dma_ch.ahbch_req.dst_width = DMAC_CSR_WIDTH_32;
- /* (in) DMAC_CSR_AD_INC, DMAC_CSR_AD_DEC, or DMAC_CSR_AD_FIX */
- sdd.dma_ch.ahbch_req.dst_addr_ctrl = DMAC_CSR_AD_INC;
- /* (in) DMAC_REQN_xxx (also used to help determine channel number) */
- sdd.dma_ch.ahbch_req.dst_index = DMAC_REQN_NONE;
- sdd.dma_ch.ahbch_req.dst_reqn = AHB_REQN_NONE;
- status = _dmad_channel_alloc(&sdd.dma_ch, HAL_TRUE);
- }
- SDD_TRACE(("sdd dma channel(%d) controller(%d)\r\n",
- sdd.dma_ch.channel, sdd.dma_ch.controller));
- return status;
- }
- static inline void _sdd_free_dma_channel(void)
- {
- _dmad_channel_free(&sdd.dma_ch);
- }
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_cd_reset
- *
- * DESCRIPTION
- *
- * This function performs card-detection initialization and card remove
- * clean-up tasks.
- *
- * NOTE
- *
- *
- * INPUTS
- *
- * insert : non-zero to perform card-inserting tasks, zero for removing.
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SSPD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static uint32_t _sdd_cd_reset(uint8_t insert)
- {
- uint32_t status = HAL_SUCCESS; /* Return status code */
- SD_R32 sd_rsp32; /* SD card command response 32-bit */
- SD_R128 sd_rsp128; /* SD card command response 128-bit */
- char *tmp_str;
- uint32_t tmp_val;
- SDD_TRACE(("_sdd_cd_reset\r\n"));
- /* ------------ */
- /* Reset host SDC */
- SETB32(SDC_CMD, SDC_SDC_RST_BIT);
- while (GETB32(SDC_CMD, SDC_SDC_RST_BIT) != 0) ;
- /* Perform card removal tasks */
- if (insert != HAL_TRUE) {
- /* Stop pending DMA ? */
- /* Turn off SD bus power */
- CLRB32(SDC_PWR_CTL, SDC_SD_POWER_ON_BIT);
- /* Turn off SD bus clock */
- SETB32(SDC_CLK_CTL, SDC_CLK_DIS_BIT);
- /* Setup removed flag */
- sdd.card_desc.rca = 0;
- #if (SDD_VFS_SUPPORT)
- // notify file system layer, if any ...
- if (sdd_bdev.propagate_event != HAL_NULL)
- sdd_bdev.propagate_event(&sdd_bdev, NDSBDEV_DEVICE_UNPLUG, HAL_NULL);
- #endif // SDD_VFS_SUPPORT
- return HAL_SUCCESS;
- }
- /* ------------ */
- /* Turn on SD card power using default voltage level */
- SETB32(SDC_PWR_CTL, SDC_SD_POWER_ON_BIT);
- _nds_kwait(0x1000);
- /* ------------ */
- /* Turn on SD bus clock, apply max freq-division value (smallest frequency) */
- OUT32(SDC_CLK_CTL, (SDC_CLK_ON << SDC_CLK_DIS_BIT) |
- (SDC_CLK_SD << SDC_CLK_SD_BIT) | SDC_CLK_DIV_MASK);
- _nds_kwait(0x1000);
- SDD_TRACE(("power-on & clock-on!\r\n"));
- /* Perform card initialization & identification process */
- /* Idle State -> Ready State */
- /* - CMD0 */
- SDD_TRACE(("CMD0\r\n"));
- status = _sd_cmd0();
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* - CMD8 */
- SDD_TRACE(("CMD8\r\n"));
- status = _sd_cmd8(SD_CMD8_MAKE_ARG(SD_VHS_2_7V_3_6V, SD_CMD8_DEFAULT_PTN), &sd_rsp32);
- if (status != HAL_SUCCESS) {
- if ((status != SDD_RSP_TIMEOUT) && (status != SDD_CMD_TIMEOUT))
- goto _err_exit;
- sdd.card_desc.version = SDD_SPEC_1XX;
- }
- else {
- /* version 2.0 or later card */
- /* validates CMD8 response */
- if ((SD_R7_GET_PTN(sd_rsp32) != 0xaa) || (SD_R7_GET_VHS(sd_rsp32) == 0x00)) {
- /* unusable card */
- status = SDD_INVALID_MEDIA;
- goto _err_exit;
- }
- sdd.card_desc.version = SDD_SPEC_200;
- }
- /* - ACMD41 */
- SDD_TRACE(("ACMD41\r\n"));
- {
- uint32_t retry = 0;
- uint32_t timeout = 1;
- uint32_t sd_hcs = SD_HCS_SD; /* SD(0) or SDHC(1) for ACMD41 */
- /*
- * HCS should be 0 if CMD8 does not response.
- * HCS is 1 if host supports SDHC (AG101 does not support SDHC).
- */
- if (sdd.card_desc.version == SDD_SPEC_200)
- {
- sd_hcs = SD_HCS_SDHC;
- }
- /* issue ACMD41 to get OCR */
- while (retry++ < SD_ACMD41_MAX_RETRY_COUNT) {
- /* cases: */
- /* - v2.0 or latter SD memory card - voltage mismatch */
- /* - v1.x SD memory card */
- /* - not SD memory card */
- SDD_TRACE(("cmd55\r\n"));
- /* notify card we're going to send an ACMD, RCA is 0x00 in card's idle state */
- status = _sd_cmd55(0x00, &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- if (SD_CSR_GET_APP_CMD(sd_rsp32) == 0) {
- /* error if card was not expecting ACMD */
- status = SDD_NOT_SUPPORT_ACMD;
- goto _err_exit;
- }
- SDD_TRACE(("acmd41\r\n"));
- status = _sd_acmd41(SD_ACMD41_MAKE_ARG(sd_hcs), &sd_rsp32);
- if (status != HAL_SUCCESS) {
- /* no response - not a SD memory card */
- goto _err_exit;
- }
- /* Continue if the OCR power state is not ready yet */
- if (SD_OCR_GET_BUSY(sd_rsp32) == 1) {
- /* card power up status is ready */
- sdd.card_desc.card_ccs =
- ((SD_OCR_GET_CCS(sd_rsp32) == SD_CCS_SDHC) ? SDD_CCS_SDHC : SDD_CCS_SD);
- sdd.card_desc.vdd_window = SD_OCR_GET_VDD(sd_rsp32);
- timeout = 0;
- break;
- }
- }
- if (timeout) {
- /*
- * unusable card
- * - no compatible voltage range (go to inactive state)
- * - timeout (no resposne or timeout)
- */
- SDD_TRACE(("timeout!\r\n"));
- status = SDD_INVALID_MEDIA;
- goto _err_exit;
- }
- }
- /* Ready State -> Identification State */
- /* - CMD2 */
- SDD_TRACE(("CMD2\r\n"));
- status = _sd_cmd2(&sd_rsp128);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- sdd.card_desc.mfg_id = SD_CID_GET_MID(sd_rsp128); /* ID */
- tmp_str = (char *)SD_CID_GET_OID_PTR(sd_rsp128); /* string */
- sdd.card_desc.oem_id[0] = tmp_str[0];
- sdd.card_desc.oem_id[1] = tmp_str[1];
- sdd.card_desc.oem_id[2] = 0x00;
- tmp_str = (char *)SD_CID_GET_PNM_PTR(sd_rsp128); /* string */
- sdd.card_desc.prod_name[0] = tmp_str[0];
- sdd.card_desc.prod_name[1] = tmp_str[1];
- sdd.card_desc.prod_name[2] = tmp_str[2];
- sdd.card_desc.prod_name[3] = tmp_str[3];
- sdd.card_desc.prod_name[4] = tmp_str[4];
- sdd.card_desc.prod_name[5] = 0x00;
- tmp_val = (uint32_t) SD_CID_GET_PRV(sd_rsp128); /* BCD */
- sdd.card_desc.prod_rev[0] = (char)((tmp_val >> 4) + 0x30);
- sdd.card_desc.prod_rev[1] = (char)'.';
- sdd.card_desc.prod_rev[2] = (char)((tmp_val & 0x0f) + 0x30);
- sdd.card_desc.prod_rev[3] = 0x00;
- sdd.card_desc.prod_sn = (uint32_t) SD_CID_GET_PSN(sd_rsp128); /* 32-bit word value */
- tmp_val = (uint32_t) SD_CID_GET_MDT(sd_rsp128); /* 12-bit value */
- sdd.card_desc.mfg_year = (uint16_t) (tmp_val >> 4) + 2000;
- sdd.card_desc.mfg_month = (uint16_t) (tmp_val & 0x0f);
- SDD_TRACE(("oem_id : 0x%081x\r\n", sdd.card_desc.oem_id));
- SDD_TRACE(("prod_name : %s\r\n", sdd.card_desc.prod_name));
- SDD_TRACE(("prod_rev : 0x%081x\r\n",sdd.card_desc.prod_rev));
- SDD_TRACE(("prod_sn : 0x%08lx\r\n", sdd.card_desc.prod_sn));
- SDD_TRACE(("mfg_year : %d\r\n", sdd.card_desc.mfg_year));
- SDD_TRACE(("mfg_month : %d\r\n", sdd.card_desc.mfg_month));
- /* Identification -> Standby State */
- /* - CMD3 */
- SDD_TRACE(("CMD3\r\n"));
- status = _sd_cmd3(&sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- if (SD_R6_GET_CSR_ERR(sd_rsp32)) {
- status = SDD_CMD_ERROR;
- goto _err_exit;
- }
- sdd.card_desc.rca = SD_R6_GET_RCA(sd_rsp32);
- SDD_TRACE(("New RCA: 0x%08lx\r\n", sdd.card_desc.rca));
- /*
- * Get CSD register (Standby -> Standby)
- * (Majorly we need the data access timing and SD bus clock)
- */
- /* - CMD9 */
- SDD_TRACE(("Get CSD (CMD9)\r\n"));
- status = _sd_cmd9(sdd.card_desc.rca, &sd_rsp128);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- tmp_val = SD_CSD_GET_CSD_STRUCTURE(sd_rsp128);
- SDD_TRACE(("CSD r0: 0x%08lx\r\n", sd_rsp128.r[0]));
- SDD_TRACE(("CSD r1: 0x%08lx\r\n", sd_rsp128.r[1]));
- SDD_TRACE(("CSD r2: 0x%08lx\r\n", sd_rsp128.r[2]));
- SDD_TRACE(("CSD r3: 0x%08lx\r\n", sd_rsp128.r[3]));
- {
- static uint32_t taac_tu2ns[] = {
- /*
- * Note: Due to minimum is 1, taac did not divided by 10 initially.
- * Values derived from this taac has to be divided by 10.
- */
- 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
- };
- static uint32_t ts_tu2bps[] = {
- /*
- * Note: Reserved index (4~7) will map to 0,
- * others are divided by 10 to avoid floating point operation.
- */
- 1000, 100000, 1000000, 10000000, 0, 0, 0, 0
- };
- static uint32_t taac_ts_tv2flt[] = {
- /*
- * Note: Reserved index (0) will map to 0,
- * other value are multiplied with 10 to avoid floating point operation.
- */
- 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60,
- 70, 80
- };
- if (tmp_val == 0x00) {
- /* Standard SD */
- float sdc_clk_ns; /* SDC to card bus clock */
- float sdc_read_to_ns, sdc_write_to_ns; /* read/write timeout (ns) */
- SDD_TRACE(("CSD v1.0\r\n"));
- SDD_TRACE(("taac : 0x%08lx\r\n", SD_CSD_GET_TAAC(sd_rsp128)));
- SDD_TRACE(("nsac : 0x%08lx\r\n", SD_CSD_GET_NSAC(sd_rsp128)));
- /* CSD structure version */
- sdd.card_desc.csd_ver = 0x01;
- /* obtain card maximum transfer speed (bps) */
- tmp_val = SD_CSD_GET_TRAN_SPEED(sd_rsp128);
- sdd.card_desc.max_dataline_rate =
- (taac_ts_tv2flt[SD_TRAN_SPEED_TV(tmp_val)] *
- ts_tu2bps[SD_TRAN_SPEED_TU(tmp_val)]);
- SDD_TRACE(("line_speed : 0x%08lx (bps)\r\n", sdd.card_desc.max_dataline_rate));
- /* AG101 SDC transfer speed limits */
- // if (sdd.card_desc.max_dataline_rate > 25000000)
- // sdd.card_desc.max_dataline_rate = 25000000;
- /*
- * assume we would like to reach the maximum transfer speed,
- * backward derive SDC_CLK_DIC ...
- */
- sdd.card_desc.sdc_clk_div =
- MB_PCLK / (sdd.card_desc.max_dataline_rate << 1);
- if (sdd.card_desc.sdc_clk_div > 0)
- sdd.card_desc.sdc_clk_div -= 1;
- /* obtain real SDC clock frequency */
- sdd.card_desc.sdc_clk_freq =
- MB_PCLK / ((sdd.card_desc.sdc_clk_div + 1) << 1);
- /*
- * obtain card read access time 1 (TAAC, ns)
- * note that this is 10x value, use of taac has to be divide by 10.
- */
- tmp_val = SD_CSD_GET_TAAC(sd_rsp128);
- sdd.card_desc.async_access_time =
- taac_ts_tv2flt[SD_TAAC_TV(tmp_val)] *
- taac_tu2ns[SD_TAAC_TU(tmp_val)];
- /* obtain card read access time 2 (NSAC * 100, clocks) */
- sdd.card_desc.read_access_clks = SD_CSD_GET_NSAC(sd_rsp128) * 100; /* (NSAC * 100) Hz */
- /* obtain programming time multiplication factor */
- sdd.card_desc.prog_factor = (1 << SD_CSD_GET_R2W_FACTOR(sd_rsp128));
- /* obtain total read (TAAC + NSAC) & write (R2W * read) access timeout (x100) */
- sdc_clk_ns = 1000000000.0f / (float)sdd.card_desc.sdc_clk_freq; /* SDC clock period (ns) */
- sdc_read_to_ns =
- (float)sdd.card_desc.read_access_clks * sdc_clk_ns * 100.0f +
- (float)sdd.card_desc.async_access_time * 10.0f;
- sdc_write_to_ns = (float)sdd.card_desc.prog_factor * sdc_read_to_ns;
- /* obtain read and write timeout value */
- /* read_timeout = min(100ms, 100 x read_access_time_ms) */
- if (sdc_read_to_ns > 100000000.0f)
- sdc_read_to_ns = 100000000.0f;
- sdd.card_desc.read_timeout_clks = (uint32_t) (sdc_read_to_ns / sdc_clk_ns);
- /* write_timeout = min(100ms, 100 x write_access_time_ms) */
- if (sdc_write_to_ns > 100000000.0f)
- sdc_write_to_ns = 100000000.0f;
- sdd.card_desc.write_timeout_clks = (uint32_t) (sdc_write_to_ns / sdc_clk_ns);
- /* command class support list */
- sdd.card_desc.cmd_class = SD_CSD_GET_CCC(sd_rsp128);
- /* read parameters */
- /* should be 512, 1024, or 2048 */
- sdd.card_desc.max_read_block_len = (uint16_t) (1 << SD_CSD_GET_READ_BL_LEN(sd_rsp128));
- /* should be 1 for SD memory card */
- sdd.card_desc.partial_block_read = (uint8_t) SD_CSD_GET_READ_BL_PARTIAL(sd_rsp128);
- sdd.card_desc.read_block_misalign = (uint8_t) SD_CSD_GET_READ_BLK_MISALIGN(sd_rsp128); /* 0 or 1 */
- /* write parameters */
- sdd.card_desc.max_write_block_len = (uint16_t) (1 << SD_CSD_GET_WRITE_BL_LEN(sd_rsp128)); /* should be same as read-block size in SD memory card */
- sdd.card_desc.partial_block_write = (uint8_t) SD_CSD_GET_WRITE_BL_PARTIAL(sd_rsp128); /* 0 or 1 */
- sdd.card_desc.write_block_misalign = (uint8_t) SD_CSD_GET_WRITE_BLK_MISALIGN(sd_rsp128); /* 0 or 1 */
- /* erase parameters */
- sdd.card_desc.erase_single_block = (uint8_t) SD_CSD_GET_ERASE_BLK_EN(sd_rsp128); /* 0 or 1 */
- sdd.card_desc.erase_sector_size = (uint8_t) SD_CSD_GET_SECTOR_SIZE(sd_rsp128); /* 0~127 means 1~128 number of write block size */
- sdd.card_desc.file_format = (uint8_t) SD_CSD_GET_FILE_FORMAT(sd_rsp128);
- /* write protect parameters */
- sdd.card_desc.wp_group_size = (uint8_t) SD_CSD_GET_WP_GRP_SIZE(sd_rsp128); /* 0~127 means 1~128 number of erase sector size */
- sdd.card_desc.wp_group_enable = (uint8_t) SD_CSD_GET_WP_GRP_ENABLE(sd_rsp128); /* 0 or 1 */
- sdd.card_desc.wp_permanent = (uint8_t) SD_CSD_GET_PERM_WRITE_PROTECT(sd_rsp128); /* 0 or 1 */
- sdd.card_desc.wp_temp = (uint8_t) SD_CSD_GET_TMP_WRITE_PROTECT(sd_rsp128); /* 0 or 1 */
- /* other parameters */
- sdd.card_desc.copy = (uint8_t) SD_CSD_GET_COPY(sd_rsp128); /* 0 or 1 */
- sdd.card_desc.dsr_imp = (uint8_t) SD_CSD_GET_DSR_IMP(sd_rsp128); /* 0 or 1 */
- /* card capacity parameters */
- sdd.card_desc.c_size = (uint32_t) SD_CSD1_GET_C_SIZE(sd_rsp128); /* 12-bit value */
- sdd.card_desc.c_size_mult = (uint32_t) SD_CSD1_GET_C_SIZE_MULT(sd_rsp128); /* 3-bit value */
- /* calculate card capacity of user data (unit of 512-bytes) */
- sdd.card_desc.card_capacity =
- ((sdd.card_desc.c_size +
- 1) * (1 << (sdd.card_desc.c_size_mult + 2))) >> 9;
- sdd.card_desc.card_capacity *=
- sdd.card_desc.max_read_block_len;
- }
- else if (tmp_val == 0x01) {
- /* SDHC (or Standard SD with CSD 2.0?) */
- /*static uint32_t soc_pclk = 50000000; // (assume PCLK 50M) todo: fix it! */
- float sdc_clk_ns;
- SDD_TRACE(("CSD v2.0\r\n"));
- SDD_TRACE(("taac(r/o) : 0x%08lx\r\n",
- SD_CSD_GET_TAAC(sd_rsp128)));
- SDD_TRACE(("nsac(r/o) : 0x%08lx\r\n",
- SD_CSD_GET_NSAC(sd_rsp128)));
- /* CSD structure version */
- sdd.card_desc.csd_ver = 0x02;
- /* obtain card maximum transfer speed (bps) */
- tmp_val = SD_CSD_GET_TRAN_SPEED(sd_rsp128); /* same as 1.0 */
- sdd.card_desc.max_dataline_rate =
- (taac_ts_tv2flt[SD_TRAN_SPEED_TV(tmp_val)] *
- ts_tu2bps[SD_TRAN_SPEED_TU(tmp_val)]);
- SDD_TRACE(("line_speed : 0x%08lx (bps)\r\n",
- sdd.card_desc.max_dataline_rate));
- /* AG101 SDC transfer speed limits */
- // if (sdd.card_desc.max_dataline_rate > 25000000)
- // sdd.card_desc.max_dataline_rate = 25000000;
- /*
- * assume we would like to reach the maximum transfer speed,
- * backward derive SDC_CLK_DIC ...
- */
- sdd.card_desc.sdc_clk_div =
- MB_PCLK / (sdd.card_desc.max_dataline_rate << 1);
- if (sdd.card_desc.sdc_clk_div > 0)
- sdd.card_desc.sdc_clk_div -= 1;
- /* obtain real SDC clock frequency */
- sdd.card_desc.sdc_clk_freq =
- MB_PCLK / ((sdd.card_desc.sdc_clk_div + 1) << 1);
- /* obtain card read access time 1 (TAAC, ns) */
- /* note that this is 10x value, use of taac has to be divide by 10. */
- // tmp_val = SD_CSD_GET_TAAC(sd_rsp128);
- // sdd.card_desc.async_access_time =
- // taac_ts_tv2flt[SD_TAAC_TV(tmp_val)] * taac_tu2ns[SD_TAAC_TU(tmp_val)];
- /* obtain card read access time 2 (NSAC * 100, clocks) */
- // sdd.card_desc.read_access_clks = SD_CSD_GET_NSAC(sd_rsp128) * 100; // (NSAC * 100) Hz */
- /* obtain programming time multiplication factor */
- sdd.card_desc.prog_factor = (1 << SD_CSD_GET_R2W_FACTOR(sd_rsp128)); /* should be 4 */
- /* obtain total read (TAAC + NSAC) & write (R2W * read) access timeout (x100) */
- sdc_clk_ns = 1000000000.0f / (float)sdd.card_desc.sdc_clk_freq; /* SDC clock period (ns) */
- /* obtain read and write timeout value */
- sdd.card_desc.read_timeout_clks = (uint32_t) (100000000.0f / sdc_clk_ns); /* 100ms according to spec 2.0 */
- sdd.card_desc.write_timeout_clks = (uint32_t) (250000000.0f / sdc_clk_ns); /* 250ms according to spec 2.0 */
- /* command class support list */
- sdd.card_desc.cmd_class = SD_CSD_GET_CCC(sd_rsp128); /* same as 1.0 */
- /* read parameters */
- sdd.card_desc.max_read_block_len = (uint16_t) (1 << SD_CSD_GET_READ_BL_LEN(sd_rsp128)); /* should be 512 bytes */
- sdd.card_desc.partial_block_read = (uint8_t) SD_CSD_GET_READ_BL_PARTIAL(sd_rsp128); /* should be 0 */
- sdd.card_desc.read_block_misalign = (uint8_t) SD_CSD_GET_READ_BLK_MISALIGN(sd_rsp128); /* should be 0 */
- /* write parameters */
- sdd.card_desc.max_write_block_len = (uint16_t) (1 << SD_CSD_GET_WRITE_BL_LEN(sd_rsp128)); /* should be 512 bytes */
- sdd.card_desc.partial_block_write = (uint8_t) SD_CSD_GET_WRITE_BL_PARTIAL(sd_rsp128); /* should be 0 */
- sdd.card_desc.write_block_misalign = (uint8_t) SD_CSD_GET_WRITE_BLK_MISALIGN(sd_rsp128); /* should be 0 */
- /* erase parameters */
- sdd.card_desc.erase_single_block = (uint8_t) SD_CSD_GET_ERASE_BLK_EN(sd_rsp128); /* should be 1 */
- sdd.card_desc.erase_sector_size = (uint8_t) SD_CSD_GET_SECTOR_SIZE(sd_rsp128); /* should be 64 KBytes (not relate to erase) */
- sdd.card_desc.file_format = (uint8_t) SD_CSD_GET_FILE_FORMAT(sd_rsp128); /* should be 0 */
- /* write protect parameters */
- sdd.card_desc.wp_group_size = (uint8_t) SD_CSD_GET_WP_GRP_SIZE(sd_rsp128); /* should be 0 */
- sdd.card_desc.wp_group_enable = (uint8_t) SD_CSD_GET_WP_GRP_ENABLE(sd_rsp128); /* should be 0 */
- sdd.card_desc.wp_permanent = (uint8_t) SD_CSD_GET_PERM_WRITE_PROTECT(sd_rsp128); /* same as 1.0 */
- sdd.card_desc.wp_temp = (uint8_t) SD_CSD_GET_TMP_WRITE_PROTECT(sd_rsp128); /* same as 1.0 */
- /* other parameters */
- sdd.card_desc.copy = (uint8_t) SD_CSD_GET_COPY(sd_rsp128); /* same as 1.0 */
- sdd.card_desc.dsr_imp = (uint8_t) SD_CSD_GET_DSR_IMP(sd_rsp128); /* same as 1.0 */
- /* card capacity parameters */
- sdd.card_desc.c_size =
- (uint32_t) SD_CSD2_GET_C_SIZE(sd_rsp128);
- /* calculate card capacity of user data (unit of 512-bytes) */
- sdd.card_desc.card_capacity = sdd.card_desc.c_size + 1; /* (c_size + 1) * 512 / 512 (KBytes) */
- sdd.card_desc.card_capacity *=
- sdd.card_desc.max_read_block_len;
- }
- else {
- SDD_TRACE(("Unknown CSD version!\r\n"));
- sdd.card_desc.csd_ver = 0x00;
- status = SDD_INVALID_MEDIA;
- goto _err_exit;
- }
- }
- SDD_TRACE(("card_capacity: 0x%08lx (KB)\r\n", sdd.card_desc.card_capacity));
- SDD_TRACE(("clk_div: 0x%08lx\r\n", sdd.card_desc.sdc_clk_div));
- SDD_TRACE(("clk_freq: %d Hz\r\n", sdd.card_desc.sdc_clk_freq));
- SDD_TRACE(("read_timeout_clks: 0x%08lx\r\n", sdd.card_desc.read_timeout_clks));
- SDD_TRACE(("write_timeout_clks: 0x%08lx\r\n", sdd.card_desc.write_timeout_clks));
- /* ------------ */
- /* Now we know how to setup SD bus clock for maximum data transfer rate ... */
- OUT32(SDC_CLK_CTL, (SDC_CLK_ON << SDC_CLK_DIS_BIT) | (SDC_CLK_SD << SDC_CLK_SD_BIT) |
- ((sdd.card_desc.sdc_clk_div << SDC_CLK_DIV_SHIFT) & SDC_CLK_DIV_MASK));
- _nds_kwait(0x1000);
- /* ------------ */
- /*
- * Get SCR register (Standby -> Transfer -> Standby)
- * (Majorly we need the data bus width)
- */
- SDD_TRACE(("Get SCR\r\n"));
- status = _sd_select_card(sdd.card_desc.rca); /* standby -> transfer */
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /*
- * send SD command to setup read-block-size
- * (SD memory card read block size could smaller than 512)
- */
- SDD_TRACE(("cmd16\r\n"));
- status = _sd_cmd16(8, &sd_rsp32); /* read 64 bits data */
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* set SDC data timeout register */
- // OUT32(SDC_DATA_TIMER, 0xffffffff); /* use sdd.card_desc.read_timeout_clks ? */
- OUT32(SDC_DATA_TIMER, sdd.card_desc.read_timeout_clks);
- /* set SDC data length register */
- OUT32(SDC_DATA_LEN, 8); /* read 64 bits data */
- /* set SDC data control register */
- OUT32(SDC_DATA_CR, SDC_DATA_EN_MASK | ((3 << SDC_BLK_SIZE_SHIFT) & SDC_BLK_SIZE_MASK)); /* 2^3 = 8 bytes */
- /* send SD command to readback SCR register */
- SDD_TRACE(("cmd55\r\n"));
- status = _sd_cmd55(sdd.card_desc.rca, &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- SDD_TRACE(("acmd51\r\n"));
- status = _sd_acmd51(&sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* pio-mode read-back */
- tmp_val = 0;
- while (tmp_val++ < SD_READ_MAX_RETRY_COUNT) {
- uint32_t sdc_status = IN32(SDC_STATUS);
- if (sdc_status & (SDC_SR_CARD_DETECT_MASK | SDC_SR_DATA_TIMEOUT_MASK | SDC_SR_DATA_CRC_FAIL_MASK)) {
- status = SDD_READ_DATA_ERROR;
- goto _err_exit;
- }
- if (sdc_status & (SDC_SR_FIFO_ORUN_MASK | SDC_SR_DATA_END_MASK))
- break;
- }
- /* ACMD51/ACMD13 data are MSB first (big-endian) */
- sd_rsp128.r[3] = IN32(SDC_DATA_WIN); /* [63:32] BE */
- sd_rsp128.r[2] = IN32(SDC_DATA_WIN); /* [31:00] BE */
- /* reorder byte endian inside the 32-bit word */
- sd_rsp128.b[0] = sd_rsp128.b[11];
- sd_rsp128.b[1] = sd_rsp128.b[10];
- sd_rsp128.b[2] = sd_rsp128.b[9];
- sd_rsp128.b[3] = sd_rsp128.b[8];
- sd_rsp128.b[4] = sd_rsp128.b[15];
- sd_rsp128.b[5] = sd_rsp128.b[14];
- sd_rsp128.b[6] = sd_rsp128.b[13];
- sd_rsp128.b[7] = sd_rsp128.b[12];
- sdd.card_desc.scr_ver = (uint8_t) SD_SCR_GET_SCR_STRUCTURE(sd_rsp128);
- sdd.card_desc.spec_ver = (uint8_t) SD_SCR_GET_SD_SPEC(sd_rsp128);
- sdd.card_desc.erase_val = (uint8_t) SD_SCR_GET_DATA_STAT_AFTER_ERASE(sd_rsp128);
- sdd.card_desc.security_ver = (uint8_t) SD_SCR_GET_SD_SECURITY(sd_rsp128);
- sdd.card_desc.bus_width = (uint8_t) SD_SCR_GET_SD_BUS_WIDTHS(sd_rsp128);
- SDD_TRACE(("lw : 0x%08lx\r\n", sd_rsp128.r[2]));
- SDD_TRACE(("hw : 0x%08lx\r\n", sd_rsp128.r[3]));
- SDD_TRACE(("SCR_ver : 0x%08lx\r\n", sdd.card_desc.scr_ver));
- SDD_TRACE(("spec_ver : 0x%08lx\r\n", sdd.card_desc.spec_ver));
- SDD_TRACE(("erase_val: 0x%08lx\r\n", sdd.card_desc.erase_val));
- SDD_TRACE(("secu_ver : 0x%08lx\r\n", sdd.card_desc.security_ver));
- SDD_TRACE(("bus_width: 0x%08lx\r\n", sdd.card_desc.bus_width));
- if ((IN32(SDC_BUS_WIDTH) & SDC_WIDE_BUS_SUPPORT_MASK) &&
- (sdd.card_desc.bus_width & 0x04)) {
- SDD_TRACE(("SDC configured to wide bus!\r\n"));
- SDD_TRACE(("cmd55\r\n"));
- status = _sd_cmd55(sdd.card_desc.rca, &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- SDD_TRACE(("acmd6\r\n"));
- status = _sd_acmd6(SD_ACMD6_MAKE_ARG(SD_BUS_WIDTH_ARG_4BIT), &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- SDD_TRACE(("sdc_bus_width\r\n"));
- OUT32(SDC_BUS_WIDTH, SDC_WIDE_BUS_SUPPORT_MASK | SDC_WIDE_BUS_MASK);
- }
- else {
- SDD_TRACE(("SDC configured to single bus!\r\n"));
- SDD_TRACE(("cmd55\r\n"));
- status = _sd_cmd55(sdd.card_desc.rca, &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- SDD_TRACE(("acmd6\r\n"));
- status = _sd_acmd6(SD_ACMD6_MAKE_ARG(SD_BUS_WIDTH_ARG_1BIT), &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- OUT32(SDC_BUS_WIDTH, SDC_WIDE_BUS_SUPPORT_MASK | SDC_SINGLE_BUS_MASK);
- }
- /* transfer -> standby */
- // status = _sd_deselect_card(sdd.card_desc.rca);
- // if (status != HAL_SUCCESS)
- // goto _err_exit;
- /*
- * Turn on SD card power using voltage level obtained from card OCR
- * todo: use default? flib doesn't setup voltage value?
- */
- // SETB32(SDC_PWR_CTL, SDC_SD_POWER_ON_BIT);
- // _nds_kwait(0x1000);
- return HAL_SUCCESS;
- _err_exit:
- SDD_TRACE(("_err_exit!\r\n"));
- /* Turn off SD bus power */
- CLRB32(SDC_PWR_CTL, SDC_SD_POWER_ON_BIT);
- /* Turn off SD bus clock */
- SETB32(SDC_CLK_CTL, SDC_CLK_DIS_BIT);
- /* Setup removed flag */
- sdd.card_desc.rca = 0;
- #if (SDD_VFS_SUPPORT)
- // notify file system layer, if any ...
- if (sdd_bdev.propagate_event != HAL_NULL)
- sdd_bdev.propagate_event(&sdd_bdev, NDSBDEV_DEVICE_UNPLUG, HAL_NULL);
- #endif // SDD_VFS_SUPPORT
- return status;
- }
- #if 0
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_read_sector_pio
- *
- * DESCRIPTION
- *
- * This function performs a sector of data PIO transfer from the SD memory
- * card.
- *
- * NOTE
- *
- *
- * INPUTS
- *
- * sector :
- * sector_cnt :
- * io_buff :
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SSPD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- uint32_t _sdd_read_sector_pio(uint32_t sector, uint8_t * io_buff)
- {
- uint32_t status = SDD_INVALID_FUNCTION;
- uint32_t sdc_status;
- SD_R32 sd_rsp32;
- uint32_t retry = 0;
- uint32_t word_cnt;
- uint32_t fifo_depth;
- SDD_TRACE(("\r\n_sdd_read_sector_pio\r\n"));
- if (sector > (sdd.card_desc.card_capacity << 1)) {
- SDD_TRACE(("Invalid sector address!\r\n"));
- return SDD_INVLAID_ADDRESS;
- }
- /* todo: sector size */
- /* standby -> transfer */
- SDD_TRACE(("- select_card\r\n"));
- status = _sd_select_card(sdd.card_desc.rca);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* send SD command to setup read-block-size */
- /* todo: assume sector size is 512 bytes */
- SDD_TRACE(("- cmd16\r\n"));
- status = _sd_cmd16(SDD_SECTOR_SIZE, &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* set SDC data timeout register */
- OUT32(SDC_DATA_TIMER, sdd.card_desc.read_timeout_clks);
- /* set SDC data length register */
- OUT32(SDC_DATA_LEN, SDD_SECTOR_SIZE);
- /* set SDC data control register */
- OUT32(SDC_DATA_CR, SDC_DATA_EN_MASK | ((SDD_POWER_OF_SECTOR_SIZE << SDC_BLK_SIZE_SHIFT) & SDC_BLK_SIZE_MASK)); /* 2^9 = 512 bytes */
- /* send SD command to readback data */
- SDD_TRACE(("- cmd17\r\n"));
- if (sdd.card_desc.card_ccs == SDD_CCS_SD)
- status = _sd_cmd17(sector * SDD_SECTOR_SIZE, &sd_rsp32); /* address unit is byte for SD */
- else
- status = _sd_cmd17(sector, &sd_rsp32); /* address unit is 512 bytes for SDHC */
- if (status != HAL_SUCCESS)
- goto _err_exit;
- SDD_TRACE(("- wait until card is sending out data\r\n"));
- _nds_kwait(0x1000); /* hw need delay ? */
- /*status = _sd_wait_sending_state(sdd.card_desc.rca); */
- /*if (status != HAL_SUCCESS) */
- /* goto _err_exit; */
- SDD_TRACE(("- read back data\r\n"));
- /* per 32-bit word read back */
- word_cnt = SDD_SECTOR_SIZE >> 2;
- while (word_cnt > 0) {
- retry = 0;
- while (retry++ < SD_READ_MAX_RETRY_COUNT) {
- sdc_status = IN32(SDC_STATUS);
- if (sdc_status &
- (SDC_SR_CARD_DETECT_MASK | SDC_SR_DATA_TIMEOUT_MASK
- | SDC_SR_DATA_CRC_FAIL_MASK)) {
- SDD_TRACE(("- data error/timeout or card removed, sdc_status: 0x%08lx\r\n", sdc_status));
- status = SDD_READ_DATA_ERROR;
- goto _err_exit;
- }
- if (sdc_status &
- (SDC_SR_FIFO_ORUN_MASK | SDC_SR_DATA_END_MASK)) {
- SETB32(SDC_CLEAR, SDC_FIFO_ORUN_BIT);
- retry = 0xffffffff; /* success */
- break;
- }
- }
- if (retry != 0xffffffff) {
- /* wait data timeout */
- SDD_TRACE(("- timeout, sdc_status: 0x%08lx\r\n",
- sdc_status));
- status = SDD_READ_DATA_ERROR;
- goto _err_exit;
- }
- fifo_depth = SDC_FIFO_WORD_DEPTH;
- while (fifo_depth-- > 0) {
- /* CMD17/18/24/25 ACMD18/25 data are LSB first (little-endian) */
- *((uint32_t *) io_buff) = IN32(SDC_DATA_WIN); /* just mem-move so no data endian issue. */
- io_buff += 4;
- if (--word_cnt == 0)
- break;
- }
- }
- SDD_TRACE(("- data read completed\r\n"));
- return HAL_SUCCESS;
- _err_exit:
- SDD_TRACE(("- error on data read\r\n"));
- return status;
- }
- #endif
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_read_sectors_pio
- *
- * DESCRIPTION
- *
- * This function performs PIO data transfer from the SD memory card in
- * multiple unit of sectors.
- *
- * NOTE
- *
- *
- * INPUTS
- *
- * sector :
- * sector_cnt :
- * io_buff :
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SSPD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static uint32_t _sdd_read_sectors_pio(uint32_t sector, uint32_t sector_cnt,
- uint8_t * io_buff)
- {
- uint32_t status = SDD_INVALID_FUNCTION;
- uint32_t sdc_status;
- SD_R32 sd_rsp32;
- uint32_t retry = 0;
- uint32_t word_cnt;
- uint32_t fifo_depth;
- SDD_TRACE(("\r\n_sdd_read_sectors_pio\r\n"));
- if (sector > (sdd.card_desc.card_capacity << 1)) {
- SDD_TRACE(("Invalid sector address!\r\n"));
- return SDD_INVLAID_ADDRESS;
- }
- if ((sector_cnt == 0) || (((sector + sector_cnt) > (sdd.card_desc.card_capacity << 1)))) {
- SDD_TRACE(("Invalid sector address range!\r\n"));
- return SDD_INVLAID_ADDR_RANGE;
- }
- /* todo: sector size */
- /* standby -> transfer */
- SDD_TRACE(("- select_card\r\n"));
- status = _sd_select_card(sdd.card_desc.rca);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* send SD command to setup read-block-size */
- /* todo: assume sector size is 512 bytes */
- SDD_TRACE(("- cmd16\r\n"));
- status = _sd_cmd16(SDD_SECTOR_SIZE, &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* set SDC data timeout register */
- OUT32(SDC_DATA_TIMER, sdd.card_desc.read_timeout_clks);
- /* set SDC data length register */
- OUT32(SDC_DATA_LEN, sector_cnt * SDD_SECTOR_SIZE);
- /* set SDC data control register */
- OUT32(SDC_DATA_CR, SDC_DATA_EN_MASK | ((SDD_POWER_OF_SECTOR_SIZE << SDC_BLK_SIZE_SHIFT) & SDC_BLK_SIZE_MASK)); /* 2^9 = 512 bytes */
- /* send SD command to readback data */
- SDD_TRACE(("- cmd18\r\n"));
- if (sdd.card_desc.card_ccs == SDD_CCS_SD)
- status = _sd_cmd18(sector * SDD_SECTOR_SIZE, &sd_rsp32); /* address unit is byte for SD */
- else
- status = _sd_cmd18(sector, &sd_rsp32); /* address unit is 512 bytes for SDHC */
- if (status != HAL_SUCCESS)
- goto _err_exit;
- SDD_TRACE(("- wait until card is sending out data\r\n"));
- _nds_kwait(0x1000); /* hw need delay ? */
- /* This method will dirty SDC_STATUS so not been used. */
- // status = _sd_wait_sending_state(sdd.card_desc.rca);
- // if (status != HAL_SUCCESS)
- // goto _err_exit;
- /* per sector PIO read back */
- OUT32(SDC_CLEAR, 0x3ff);
- SDD_TRACE(("- read back data\r\n"));
- while (sector_cnt-- > 0) {
- /* per 32-bit word read back */
- word_cnt = SDD_SECTOR_SIZE >> 2;
- while (word_cnt > 0) {
- retry = 0;
- while (retry++ < SD_READ_MAX_RETRY_COUNT) {
- sdc_status = IN32(SDC_STATUS);
- if (sdc_status &
- (SDC_SR_CARD_DETECT_MASK |
- SDC_SR_DATA_TIMEOUT_MASK |
- SDC_SR_DATA_CRC_FAIL_MASK)) {
- SDD_TRACE(("- data error/timeout or card removed, sdc_status: 0x%08lx\r\n", sdc_status));
- status = SDD_READ_DATA_ERROR;
- goto _err_exit;
- }
- if (sdc_status & (SDC_SR_FIFO_ORUN_MASK | SDC_SR_DATA_END_MASK)) {
- retry = 0xffffffff; /* success */
- break;
- }
- }
- if (retry != 0xffffffff) {
- /* wait data timeout */
- SDD_TRACE(("- timeout, sdc_status: 0x%08lx\r\n", sdc_status));
- status = SDD_READ_DATA_ERROR;
- goto _err_exit;
- }
- fifo_depth = SDC_FIFO_WORD_DEPTH;
- SETB32(SDC_CLEAR, SDC_FIFO_ORUN_BIT);
- while (fifo_depth-- > 0) {
- /* CMD17/18/24/25 ACMD18/25 data are LSB first (little-endian) */
- *((uint32_t *) io_buff) = IN32(SDC_DATA_WIN); /* just mem-move so no data endian issue. */
- io_buff += 4;
- if (--word_cnt == 0)
- break;
- }
- }
- }
- _nds_kwait(0x1000); /* hw need delay ? */
- /* Stop data transmission */
- SDD_TRACE(("- stop transmission\r\n"));
- status = _sd_stop_transmission(sdd.card_desc.rca);
- /* FIXME this part is marked for qemu */
- #if 0
- if (status != HAL_SUCCESS)
- goto _err_exit;
- #endif
- SDD_TRACE(("- data read completed\r\n"));
- return HAL_SUCCESS;
- _err_exit:
- SDD_TRACE(("- error on data read! status(0x%08lx)\r\n", status));
- return status;
- }
- extern int dmad_apb_config_dir(const DMAD_CHANNEL_REQUEST_DESC *ch_req, uint8_t dir);
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_read_sectors_dma
- *
- * DESCRIPTION
- *
- * This function performs DMA data transfer from the SD memory card in
- * multiple unit of sectors.
- *
- * NOTE
- *
- *
- * INPUTS
- *
- * sector :
- * sector_cnt :
- * io_buff :
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SSPD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static uint32_t _sdd_read_sectors_dma(uint32_t sector, uint32_t sector_cnt,
- uint8_t * io_buff)
- {
- uint32_t status = SDD_INVALID_FUNCTION;
- SD_R32 sd_rsp32;
- DMAD_DRB *drb;
- SDD_TRACE(("\r\n_sdd_read_sectors_dma\r\n"));
- if (sector > (sdd.card_desc.card_capacity << 1)) {
- SDD_TRACE(("Invalid sector address!\r\n"));
- return SDD_INVLAID_ADDRESS;
- }
- if ((sector_cnt == 0) || (((sector + sector_cnt) > (sdd.card_desc.card_capacity << 1)))) {
- SDD_TRACE(("Invalid sector address range!\r\n"));
- return SDD_INVLAID_ADDR_RANGE;
- }
- /* todo: sector size */
- /* standby -> transfer */
- SDD_TRACE(("- select_card\r\n"));
- status = _sd_select_card(sdd.card_desc.rca);
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- /* send SD command to setup read-block-size */
- /* todo: assume sector size is 512 bytes */
- SDD_TRACE(("- cmd16\r\n"));
- status = _sd_cmd16(SDD_SECTOR_SIZE, &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- /* set SDC data timeout register */
- OUT32(SDC_DATA_TIMER, sdd.card_desc.read_timeout_clks);
- /* set SDC data length register */
- OUT32(SDC_DATA_LEN, sector_cnt * SDD_SECTOR_SIZE);
- /* set SDC data control register */
- OUT32(SDC_DATA_CR, SDC_DATA_EN_MASK | SDC_DMA_EN_MASK | ((SDD_POWER_OF_SECTOR_SIZE << SDC_BLK_SIZE_SHIFT) & SDC_BLK_SIZE_MASK)); /* 2^9 = 512 bytes */
- /* send SD command to readback data */
- SDD_TRACE(("- cmd18\r\n"));
- if (sdd.card_desc.card_ccs == SDD_CCS_SD)
- status = _sd_cmd18(sector * SDD_SECTOR_SIZE, &sd_rsp32); /* address unit is byte for SD */
- else
- status = _sd_cmd18(sector, &sd_rsp32); /* address unit is 512 bytes for SDHC */
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- SDD_TRACE(("- wait until card is sending out data\r\n"));
- // _nds_kwait(0x1000); /* hw need delay ? */
- /* This method will dirty SDC_STATUS so not been used. */
- // status = _sd_wait_sending_state(sdd.card_desc.rca);
- // if (status != HAL_SUCCESS)
- // goto _err_exit;
- /* per sector DMA read back */
- /* CMD17/18/24/25 ACMD18/25 data are LSB first (little-endian) */
- SDD_TRACE(("- read back data\r\n"));
- dmad_apb_config_dir(&sdd.dma_ch,DMAD_DIR_A0_TO_A1);
- while (sector_cnt-- > 0) {
- status = _dmad_alloc_drb(&sdd.dma_ch, &drb);
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- drb->src_addr = (void *)SDC_DATA_WIN;
- drb->dst_addr = (void *)io_buff;
- drb->req_size = SDD_SECTOR_SIZE >> 2; /* multiple counts of transfer width */
- if (sector_cnt == 0)
- drb->completion_sem = &sdd.dma_sem;
- /* Submit DRB */
- status = _dmad_submit_request(&sdd.dma_ch, drb);
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- /* Point to next buffer location */
- io_buff += SDD_SECTOR_SIZE;
- }
- /* Wait DMA completion */
- /* - method 1: sync with semaphore */
- /* - method 2: check SDC status register SDC_STATUS[7] */
- if (1){
- DEBUG(0, 1, "- wait dma completion ...\n");
- status = hal_pend_semaphore(&sdd.dma_sem, 300);
- if (status == HAL_ERR_TIMEOUT) {
- DEBUG(1, 1, "- wait dma completion timeout (might not an error)\n");
- goto _safe_exit;
- }
- else if (status != HAL_SUCCESS) {
- DEBUG(1, 1, "- wait dma completion failed! (0x%08lx)\n", status);
- goto _safe_exit;
- }
- }
- else {
- uint32_t retry = 0;
- uint32_t sdc_status;
- SDD_TRACE(("- polling dma completion status ...\r\n"));
- while (retry++ < SD_READ_MAX_RETRY_COUNT) {
- sdc_status = IN32(SDC_STATUS);
- if (sdc_status &
- (SDC_SR_CARD_DETECT_MASK | SDC_SR_DATA_TIMEOUT_MASK
- | SDC_SR_DATA_CRC_FAIL_MASK)) {
- SDD_TRACE(("- data error/timeout or card removed, sdc_status: 0x%08lx\r\n", sdc_status));
- status = SDD_READ_DATA_ERROR;
- break; /* todo: report error to upper layer? */
- }
- if (sdc_status & (SDC_SR_FIFO_ORUN_MASK | SDC_SR_DATA_END_MASK)) {
- SETB32(SDC_CLEAR, SDC_FIFO_ORUN_BIT);
- retry = 0xffffffff; /* success */
- break;
- }
- }
- }
- /* Stop data transmission anyway after waiting. */
- SDD_TRACE(("- stop transmission\r\n"));
- status = _sd_stop_transmission(sdd.card_desc.rca);
- /* FIXME this part is marked for qemu */
- #if 0
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- #endif
- status = HAL_SUCCESS;
- _safe_exit:
- #if (SDD_DEBUG_TRACE)
- if (status == HAL_SUCCESS)
- SDD_TRACE(("- data read completed\r\n"));
- else
- SDD_TRACE(("- error on data read\r\n"));
- #endif /* SDD_DEBUG_TRACE */
- return status;
- }
- #if 0
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_write_sector_pio
- *
- * DESCRIPTION
- *
- * This function performs a sector of data PIO transfer to the SD memory
- * card.
- *
- * NOTE
- *
- *
- * INPUTS
- *
- * sector :
- * io_buff :
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SSPD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- uint32_t _sdd_write_sector_pio(uint32_t sector, uint8_t * io_buff)
- {
- uint32_t status = SDD_INVALID_FUNCTION;
- uint32_t sdc_status;
- SD_R32 sd_rsp32;
- uint32_t retry = 0;
- uint32_t word_cnt;
- uint32_t fifo_depth;
- SDD_TRACE(("\r\n_sdd_write_sector_pio\r\n"));
- if (sector > (sdd.card_desc.card_capacity << 1)) {
- SDD_TRACE(("Invalid sector address!\r\n"));
- return SDD_INVLAID_ADDRESS;
- }
- if (GETB32(SDC_STATUS, SDC_SR_WRITE_PROT_BIT)) {
- SDD_TRACE(("Card is write protected!\r\n"));
- return SDD_WRITE_PROTECTED;
- }
- /* todo: sector size */
- /* standby -> transfer */
- SDD_TRACE(("- select_card\r\n"));
- status = _sd_select_card(sdd.card_desc.rca);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* send SD command to setup write-block-size */
- /* todo: assume sector size is 512 bytes */
- SDD_TRACE(("- cmd16\r\n"));
- status = _sd_cmd16(SDD_SECTOR_SIZE, &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* set SDC data timeout register */
- OUT32(SDC_DATA_TIMER, sdd.card_desc.write_timeout_clks);
- /* set SDC data length register */
- OUT32(SDC_DATA_LEN, SDD_SECTOR_SIZE);
- /* set SDC data control register */
- OUT32(SDC_DATA_CR, SDC_DATA_EN_MASK | SDC_DATA_WRITE_MASK | ((SDD_POWER_OF_SECTOR_SIZE << SDC_BLK_SIZE_SHIFT) & SDC_BLK_SIZE_MASK)); /* 2^9 = 512 bytes */
- /* send SD command to write data */
- SDD_TRACE(("- cmd24\r\n"));
- if (sdd.card_desc.card_ccs == SDD_CCS_SD)
- status = _sd_cmd24(sector * SDD_SECTOR_SIZE, &sd_rsp32); /* address unit is byte for SD */
- else
- status = _sd_cmd24(sector, &sd_rsp32); /* address unit is 512 bytes for SDHC */
- if (status != HAL_SUCCESS)
- goto _err_exit;
- SDD_TRACE(("- wait ready to write out data\r\n"));
- _nds_kwait(0x1000); /* hw need delay ? */
- /*status = _sd_wait_receiving_state(sdd.card_desc.rca); // This method look not necessary for writing. */
- /*if (status != HAL_SUCCESS) */
- /* goto _err_exit; */
- /* per sector PIO write out */
- SDD_TRACE(("- write out data\r\n"));
- /* clear SDC status bits before sending data */
- OUT32(SDC_CLEAR, SDC_CLEAR_ALL);
- /* per 32-bit word write out */
- word_cnt = SDD_SECTOR_SIZE >> 2;
- while (word_cnt > 0) {
- fifo_depth = SDC_FIFO_WORD_DEPTH;
- while (fifo_depth-- > 0) {
- /* CMD17/18/24/25 ACMD18/25 data are LSB first (little-endian) */
- OUT32(SDC_DATA_WIN, *((uint32_t *) io_buff)); /* just mem-move so no data endian issue. */
- io_buff += 4;
- if (--word_cnt == 0)
- goto _complete_exit;
- }
- retry = 0;
- while (retry++ < SD_WRITE_MAX_RETRY_COUNT) {
- sdc_status = IN32(SDC_STATUS);
- if (sdc_status &
- (SDC_SR_CARD_DETECT_MASK | SDC_SR_DATA_TIMEOUT_MASK
- | SDC_SR_DATA_CRC_FAIL_MASK)) {
- status = SDD_WRITE_DATA_ERROR;
- goto _err_exit;
- }
- if (sdc_status &
- (SDC_SR_FIFO_URUN_MASK | SDC_SR_DATA_END_MASK)) {
- SETB32(SDC_CLEAR, SDC_FIFO_URUN_BIT);
- retry = 0xffffffff; /* success */
- break;
- }
- }
- if (retry != 0xffffffff) {
- /* wait data timeout */
- SDD_TRACE(("- timeout, sdc_status: 0x%08lx\r\n",
- sdc_status));
- /*SDD_TRACE(("- wait again by polling CSR\r\n")); */
- /*status = _sd_wait_receiving_state(sdd.card_desc.rca); */
- /*if (status != HAL_SUCCESS) */
- goto _err_exit;
- /*SDD_TRACE(("- card ready, sdc_status: 0x%08lx\r\n", IN32(SDC_STATUS))); */
- }
- }
- _complete_exit:
- /* Wait host SDC shift-out FIFO data */
- SDD_TRACE(("- final check sdc_status\r\n"));
- retry = 0;
- while (retry++ < SD_WRITE_MAX_RETRY_COUNT) {
- sdc_status = IN32(SDC_STATUS);
- if (sdc_status &
- (SDC_SR_CARD_DETECT_MASK | SDC_SR_DATA_TIMEOUT_MASK |
- SDC_SR_DATA_CRC_FAIL_MASK)) {
- status = SDD_WRITE_DATA_ERROR;
- goto _err_exit;
- }
- if ((sdc_status == 0) || (sdc_status & SDC_SR_DATA_END_MASK)) {
- break;
- }
- }
- SDD_TRACE(("- data write completed\r\n"));
- return HAL_SUCCESS;
- _err_exit:
- SDD_TRACE(("- error on data write\r\n"));
- return status;
- }
- #endif
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_write_sectors_pio
- *
- * DESCRIPTION
- *
- * This function performs PIO data transfer to the SD memory card in
- * multiple unit of sectors.
- *
- * NOTE
- *
- *
- * INPUTS
- *
- * sector :
- * sector_cnt :
- * io_buff :
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SSPD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static uint32_t _sdd_write_sectors_pio(uint32_t sector, uint32_t sector_cnt,
- uint8_t * io_buff)
- {
- uint32_t status = SDD_INVALID_FUNCTION;
- uint32_t sdc_status;
- SD_R32 sd_rsp32;
- uint32_t retry = 0;
- uint32_t word_cnt;
- uint32_t fifo_depth;
- if (sector > (sdd.card_desc.card_capacity << 1)) {
- SDD_TRACE(("Invalid sector address!\r\n"));
- return SDD_INVLAID_ADDRESS;
- }
- if ((sector_cnt == 0) || (((sector + sector_cnt) > (sdd.card_desc.card_capacity << 1)))) {
- SDD_TRACE(("Invalid sector address range!\r\n"));
- return SDD_INVLAID_ADDR_RANGE;
- }
- if (GETB32(SDC_STATUS, SDC_SR_WRITE_PROT_BIT)) {
- SDD_TRACE(("Card is write protected!\r\n"));
- return SDD_WRITE_PROTECTED;
- }
- /* todo: sector size */
- /* standby -> transfer */
- SDD_TRACE(("- select_card\r\n"));
- status = _sd_select_card(sdd.card_desc.rca);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* send SD command to setup write-block-size */
- /* todo: assume sector size is 512 bytes */
- SDD_TRACE(("- cmd16\r\n"));
- status = _sd_cmd16(SDD_SECTOR_SIZE, &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _err_exit;
- /* set SDC data timeout register */
- OUT32(SDC_DATA_TIMER, sdd.card_desc.write_timeout_clks);
- /* set SDC data length register */
- OUT32(SDC_DATA_LEN, sector_cnt * SDD_SECTOR_SIZE);
- /* set SDC data control register */
- OUT32(SDC_DATA_CR, SDC_DATA_EN_MASK | SDC_DATA_WRITE_MASK | ((SDD_POWER_OF_SECTOR_SIZE << SDC_BLK_SIZE_SHIFT) & SDC_BLK_SIZE_MASK)); /* 2^9 = 512 bytes */
- /* send SD command to write data */
- SDD_TRACE(("- cmd25\r\n"));
- if (sdd.card_desc.card_ccs == SDD_CCS_SD)
- status = _sd_cmd25(sector * SDD_SECTOR_SIZE, &sd_rsp32); /* address unit is byte for SD */
- else
- status = _sd_cmd25(sector, &sd_rsp32); /* address unit is 512 bytes for SDHC */
- if (status != HAL_SUCCESS)
- goto _err_exit;
- SDD_TRACE(("- wait ready to write out data\r\n"));
- _nds_kwait(0x1000); /* hw need delay ? */
- /* This method look not necessary for writing. */
- // status = _sd_wait_receiving_state(sdd.card_desc.rca);
- // if (status != HAL_SUCCESS) */
- // goto _err_exit; */
- /* per sector PIO write out */
- OUT32(SDC_CLEAR, 0x3ff);
- SDD_TRACE(("- write out data\r\n"));
- while (sector_cnt-- > 0) {
- /* per 32-bit word write out */
- word_cnt = SDD_SECTOR_SIZE >> 2;
- while (word_cnt > 0) {
- SDD_TRACE(("- word_cnt: 0x%08lx\r\n", word_cnt));
- fifo_depth = SDC_FIFO_WORD_DEPTH;
- while ((fifo_depth-- > 0) && (word_cnt-- > 0)) {
- /* CMD17/18/24/25 ACMD18/25 data are LSB first (little-endian) */
- OUT32(SDC_DATA_WIN, *((uint32_t *) io_buff)); /* just mem-move so no data endian issue. */
- io_buff += 4;
- }
- if (word_cnt == 0)
- break;
- retry = 0;
- while (retry++ < SD_WRITE_MAX_RETRY_COUNT) {
- sdc_status = IN32(SDC_STATUS);
- if (sdc_status &
- (SDC_SR_CARD_DETECT_MASK |
- SDC_SR_DATA_TIMEOUT_MASK |
- SDC_SR_DATA_CRC_FAIL_MASK)) {
- status = SDD_WRITE_DATA_ERROR;
- goto _err_exit;
- }
- if (sdc_status &
- (SDC_SR_FIFO_URUN_MASK |
- SDC_SR_DATA_END_MASK)) {
- SETB32(SDC_CLEAR, SDC_FIFO_URUN_BIT);
- retry = 0xffffffff; /* success */
- break;
- }
- }
- if (retry != 0xffffffff) {
- /* wait data timeout */
- SDD_TRACE(("- timeout, sdc_status: 0x%08lx\r\n", sdc_status));
- // SDD_TRACE(("- wait again by polling CSR\r\n"));
- // status = _sd_wait_receiving_state(sdd.card_desc.rca);
- // if (status != HAL_SUCCESS)
- goto _err_exit;
- SDD_TRACE(("- card ready, sdc_status: 0x%08lx\r\n", IN32(SDC_STATUS)));
- }
- }
- }
- /* Wait host SDC shift-out FIFO data */
- SDD_TRACE(("- final check sdc_status\r\n"));
- retry = 0;
- while (retry++ < SD_WRITE_MAX_RETRY_COUNT) {
- sdc_status = IN32(SDC_STATUS);
- if (sdc_status &
- (SDC_SR_CARD_DETECT_MASK | SDC_SR_DATA_TIMEOUT_MASK |
- SDC_SR_DATA_CRC_FAIL_MASK)) {
- status = SDD_WRITE_DATA_ERROR;
- goto _err_exit;
- }
- if ((sdc_status == 0) || (sdc_status & SDC_SR_DATA_END_MASK))
- break;
- }
- /* Stop data transmission */
- SDD_TRACE(("- stop transmission\r\n"));
- status = _sd_stop_transmission(sdd.card_desc.rca);
- /* FIXME this part is marked for qemu */
- #if 0
- if (status != HAL_SUCCESS)
- goto _err_exit;
- #endif
- SDD_TRACE(("- data write completed\r\n"));
- return HAL_SUCCESS;
- _err_exit:
- SDD_TRACE(("- error on data write\r\n"));
- return status;
- }
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_write_sectors_dma
- *
- * DESCRIPTION
- *
- * This function performs DMA data transfer to the SD memory card in
- * multiple unit of sectors.
- *
- * NOTE
- *
- *
- * INPUTS
- *
- * sector :
- * sector_cnt :
- * io_buff :
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SSPD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static uint32_t _sdd_write_sectors_dma(uint32_t sector, uint32_t sector_cnt,
- uint8_t * io_buff)
- {
- uint32_t status = SDD_INVALID_FUNCTION;
- SD_R32 sd_rsp32;
- uint32_t retry;
- uint32_t sdc_status;
- DMAD_DRB *drb;
- #if 0
- if (sector > (sdd.card_desc.card_capacity << 1)) {
- SDD_TRACE(("Invalid sector address!\r\n"));
- return SDD_INVLAID_ADDRESS;
- }
- if ((sector_cnt == 0) || (((sector + sector_cnt) > (sdd.card_desc.card_capacity << 1)))) {
- SDD_TRACE(("Invalid sector address range!\r\n"));
- return SDD_INVLAID_ADDR_RANGE;
- }
- #endif
- if (GETB32(SDC_STATUS, SDC_SR_WRITE_PROT_BIT)) {
- SDD_TRACE(("Card is write protected!\r\n"));
- return SDD_WRITE_PROTECTED;
- }
- /* todo: sector size */
- /* reset dam wait event */
- // hal_set_semaphore(&sdd.dma_sem, 0);
- /* standby -> transfer */
- SDD_TRACE(("- select_card\r\n"));
- status = _sd_select_card(sdd.card_desc.rca);
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- /* send SD command to setup write-block-size */
- /* todo: assume sector size is 512 bytes */
- SDD_TRACE(("- cmd16\r\n"));
- status = _sd_cmd16(SDD_SECTOR_SIZE, &sd_rsp32);
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- /* set SDC data timeout register */
- OUT32(SDC_DATA_TIMER, sdd.card_desc.write_timeout_clks);
- /* set SDC data length register */
- OUT32(SDC_DATA_LEN, sector_cnt * SDD_SECTOR_SIZE);
- /* set SDC data control register */
- OUT32(SDC_DATA_CR, SDC_DATA_EN_MASK | SDC_DATA_WRITE_MASK | SDC_DMA_EN_MASK | ((SDD_POWER_OF_SECTOR_SIZE << SDC_BLK_SIZE_SHIFT) & SDC_BLK_SIZE_MASK)); /* 2^9 = 512 bytes */
- /* send SD command to write data */
- SDD_TRACE(("- cmd25\r\n"));
- if (sdd.card_desc.card_ccs == SDD_CCS_SD)
- status = _sd_cmd25(sector * SDD_SECTOR_SIZE, &sd_rsp32); /* address unit is byte for SD */
- else
- status = _sd_cmd25(sector, &sd_rsp32); /* address unit is 512 bytes for SDHC */
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- SDD_TRACE(("- wait ready to write out data\r\n"));
- _nds_kwait(0x1000); /* hw need delay ? */
- /* This method look not necessary for writing. */
- // status = _sd_wait_receiving_state(sdd.card_desc.rca);
- // if (status != HAL_SUCCESS) */
- // goto _err_exit; */
- /* per sector DMA write out */
- SDD_TRACE(("- write out data\r\n"));
-
- dmad_apb_config_dir(&sdd.dma_ch, DMAD_DIR_A1_TO_A0);
- if(hal_create_semaphore(&sdd.dma_sem, 0, 0) == HAL_FAILURE)
- SDD_TRACE(("Failed to create dma semaphore\r\n"));
- while (sector_cnt-- > 0) {
- SDD_TRACE(("sector_cnt: %ld\r\n", sector_cnt));
- /* Prepare DRB */
- status = _dmad_alloc_drb(&sdd.dma_ch, &drb);
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- drb->src_addr = (void *)io_buff;
- drb->dst_addr = (void *)SDC_DATA_WIN;
- drb->req_size = SDD_SECTOR_SIZE >> 2; /* multiple counts of transfer width */
- if (sector_cnt == 0)
- drb->completion_sem = &sdd.dma_sem; /* Register DMA completion notification */
- /* Submit DRB */
- status = _dmad_submit_request(&sdd.dma_ch, drb);
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- /* Point to next buffer location */
- io_buff += SDD_SECTOR_SIZE;
- }
- /*
- * Wait DMA completion
- * - method 1: sync with semaphore
- * - method 2: check SDC status register SDC_STATUS[7]
- */
- status = hal_pend_semaphore(&sdd.dma_sem, 600); /* 6 sec */
- /* Check waiting status */
- if (status == HAL_ERR_TIMEOUT) {
- SDD_TRACE(("- wait dma completion timeout (might not an error)\r\n"));
- goto _safe_exit;
- }
- else if (status != HAL_SUCCESS) {
- SDD_TRACE(("- wait dma completion failed\r\n"));
- goto _safe_exit;
- }
- /* Wait host SDC shift-out FIFO data */
- SDD_TRACE(("- final check sdc_status\r\n"));
- retry = 0;
- while (retry++ < SD_WRITE_MAX_RETRY_COUNT) {
- sdc_status = IN32(SDC_STATUS);
- if (sdc_status &
- (SDC_SR_CARD_DETECT_MASK | SDC_SR_DATA_TIMEOUT_MASK |
- SDC_SR_DATA_CRC_FAIL_MASK)) {
- status = SDD_WRITE_DATA_ERROR;
- goto _safe_exit;
- }
- if ((sdc_status == 0) || (sdc_status & SDC_SR_DATA_END_MASK))
- break;
- }
- /* Stop data transmission */
- SDD_TRACE(("- stop transmission\r\n"));
- status = _sd_stop_transmission(sdd.card_desc.rca);
- /* FIXME this part is marked for qemu */
- #if 0
- if (status != HAL_SUCCESS)
- goto _safe_exit;
- #endif
- status = HAL_SUCCESS;
- _safe_exit:
- #if (SDD_DEBUG_TRACE)
- if (status == HAL_SUCCESS)
- SDD_TRACE(("- data read completed\r\n"));
- else
- SDD_TRACE(("- error on data read\r\n"));
- #endif /* SDD_DEBUG_TRACE */
- return status;
- }
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_lisr
- *
- * DESCRIPTION
- *
- * This is the ISR that services SD card detection interrupt on the NDS32
- * evb platform.
- *
- * NOTE
- *
- * Card Detection Procedures (LISR)
- * LISR set HISR activation state flag
- * LISR activates HISR
- * call _sdd_cd_reset() to perform card insertion or removal tasks.
- *
- * Card Detection Procedures (_sdd_init)
- * Perform basic SD controller register setup
- * Checks SDC_SR_CARD_DETECT_BIT bit
- * call _sdd_cd_reset() to perform card insertion or removal tasks.
- *
- * INPUTS
- *
- * vector : Interrupt vector number
- *
- * OUTPUTS
- *
- * none
- *
- ****************************************************************************/
- void _sdd_lisr(int vector)
- {
- DEBUG(1, 1, "_sdd_lisr\r\n");
- printf("hal_raise_bh0\n");
- /* Fault invocation checking */
- if (vector != IRQ_SDC_VECTOR )
- hal_system_error(HAL_ERR_UNHANDLED_INTERRUPT);
- /* Mask : Disable SDC interrupt */
- hal_intc_irq_disable(IRQ_SDC_VECTOR);
- /* Ack : Clean SDC interrupt pending */
- hal_intc_irq_clean(IRQ_SDC_VECTOR);
-
- printf("hal_raise_bh1\n");
- if (GETB32(SDC_STATUS, SDC_SR_CARD_CHANGE_BIT)) {
- SETB32(SDC_CLEAR, SDC_CARD_CHANGE_BIT);
- /* Set HISR status register to get channel number */
- sdd.hisr_as |= SDD_HISR_AS_CD;
- printf("hal_raise_bh2\n");
- hal_raise_bh(&sdd.hisr); /* Activate HISR to complete deferred tasks */
- }
- else {
- printf("hal_raise_bh3\n");
- /* todo: handle other interrupts. */
- OUT32(SDC_CLEAR, SDC_CLEAR_ALL);
- }
- /* Unmask : Enable SDC interrupt */
- hal_intc_irq_enable(IRQ_SDC_VECTOR);
- }
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_hisr
- *
- * DESCRIPTION
- *
- * This is the HISR that services SD card detection interrupt on the NDS32
- * evb platform.
- *
- * NOTE
- *
- *
- * INPUTS
- *
- *
- *
- * OUTPUTS
- *
- * none
- *
- ****************************************************************************/
- void _sdd_hisr(void *param)
- {
- hal_bh_t *bh = (hal_bh_t *)param;
- uint32_t core_intl;
- uint8_t hisr_as; /* HISR activation state - Card Detection */
- while (1){
- DEBUG(1, 1, "_sdd_hisr before\n");
- hal_pend_semaphore(&bh->sem, HAL_SUSPEND);
- /*
- * Disable CPU interrupt
- * Todo: frequently int-disable due to frequently HISR-call may cause problems?
- */
- core_intl = hal_global_int_ctl(HAL_DISABLE_INTERRUPTS);
- /* Clone HISR activation state */
- hisr_as = sdd.hisr_as;
- sdd.hisr_as &= ~(uint32_t) SDD_HISR_AS_CD;
- /* Enable CPU interrupt */
- hal_global_int_ctl(core_intl);
- /* Card detect initialization */
- if (hisr_as & SDD_HISR_AS_CD) {
- if (GETB32(SDC_STATUS, SDC_SR_CARD_DETECT_BIT)) {
- SDD_TRACE(("SD card removed!\r\n"));
- /* Perform card-remove tasks (turn off clock ...) */
- _sdd_cd_reset(HAL_FALSE);
- }
- else {
- SDD_TRACE(("SD card inserted!\r\n"));
- /* Reset card and get device parameters */
- _sdd_cd_reset(HAL_TRUE);
- }
- }
- }
- }
- #if (SDD_VFS_SUPPORT)
- //STATUS _sdd_read_sectors(SDD_IOCTL_READ_SECTORS_PARAM *iop);
- STATUS _sdd_read_sectors_bdev(NDS_BDEV *bdev, UINT32 sector, UINT32 sector_count, void *buffer)
- {
- STATUS io_status;
- SDD_DEVICE sdd_dev; // FIXME, NDS_SD_ReadSectors doesn't use this argument actully
- BDEV_TRACE(("_sdd_read_sectors_bdev()\r\n"));
- BDEV_TRACE((" read start at sector (0x%08lx + 0x%08lx = 0x%08lx) count (0x%08lx)\r\n",
- bdev->start, sector, bdev->start + sector, sector_count));
- #ifndef CONFIG_PLAT_QEMU
- io_status = NDS_SD_ReadSectors(&sdd_dev, bdev->start + sector, sector_count, 512, buffer);
- #else
- io_status = NDS_SD_ReadSectors(&sdd_dev, sector, sector_count, 512, buffer);
- #endif
- #if 0
- if (NU_Current_Task_Pointer() != HAL_NULL)
- {
- // obtain exclusive access to driver
- request.nu_function = NU_ASSIGN;
- request.nu_timeout = NU_SUSPEND;
- status = NU_Request_Driver(&sdd_dcb, &request);
- if (status != NU_SUCCESS)
- return status;
- }
- // perform i/o operation
- BDEV_TRACE((" read start at sector (0x%08lx + 0x%08lx = 0x%08lx) count (0x%08lx)\r\n",
- bdev->start, sector, bdev->start + sector, sector_count));
- iop.lba_sector = bdev->start + sector;
- iop.sector_count = sector_count;
- iop.sector_size = SDD_SECTOR_SIZE;
- iop.io_buff = buffer;
- io_status = _sdd_read_sectors(&iop);
- if (NU_Current_Task_Pointer() != HAL_NULL)
- {
- // release exclusive access to driver
- request.nu_function = NU_RELEASE;
- status = NU_Request_Driver(&sdd_dcb, &request);
- if (status != NU_SUCCESS)
- return status;
- }
- #endif
- return io_status;
- }
- //STATUS _sdd_write_sectors(SDD_IOCTL_WRITE_SECTORS_PARAM *iop);
- STATUS _sdd_write_sectors_bdev(NDS_BDEV *bdev, UINT32 sector, UINT32 sector_count, void *buffer)
- {
- STATUS io_status;
- SDD_DEVICE sdd_dev; // FIXME, NDS_SD_ReadSectors doesn't use this argument actully
- BDEV_TRACE(("_sdd_write_sectors_bdev()\r\n"));
- #ifndef CONFIG_PLAT_QEMU
- io_status = NDS_SD_WriteSectors(&sdd_dev, bdev->start + sector, sector_count, 512, buffer);
- #else
- io_status = NDS_SD_WriteSectors(&sdd_dev, sector, sector_count, 512, buffer);
- #endif
- #if 0
- if (NU_Current_Task_Pointer() != HAL_NULL)
- {
- // obtain exclusive access to driver
- request.nu_function = NU_ASSIGN;
- request.nu_timeout = NU_SUSPEND;
- status = NU_Request_Driver(&sdd_dcb, &request);
- if (status != NU_SUCCESS)
- return status;
- }
- // perform i/o operation
- BDEV_TRACE((" write start at sector (0x%08lx + 0x%08lx = 0x%08lx) count (0x%08lx)\r\n",
- bdev->start, sector, bdev->start + sector, sector_count));
- iop.lba_sector = bdev->start + sector;
- iop.sector_count = sector_count;
- iop.sector_size = SDD_SECTOR_SIZE;
- iop.io_buff = buffer;
- io_status = _sdd_write_sectors(&iop);
- if (NU_Current_Task_Pointer() != HAL_NULL)
- {
- // release exclusive access to driver
- request.nu_function = NU_RELEASE;
- status = NU_Request_Driver(&sdd_dcb, &request);
- if (status != NU_SUCCESS)
- return status;
- }
- #endif
- BDEV_TRACE((" status (0x%08lx)\r\n", io_status));
- return io_status;
- }
- #endif // SDD_VFS_SUPPORT
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_init
- *
- * DESCRIPTION
- *
- * This function initializes the SDC device.
- *
- * NOTE
- *
- * SD/MEM : PC-Card Memory mode, PC-Card I/O mode, True-IDE.
- * SDIO : Todo.
- * SDHC : Not supported.
- * MMC : Todo.
- *
- * Todo : Resource leaks is not recovered if error happens
- * during init process. Client currently has to do
- * TERMINATE request anyway, if driver load/unload
- * resource control is a necessary, no-matter the
- * initialization process is successful or not.
- *
- * INPUTS
- *
- * sdd_dev : user device struct
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SDD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static uint32_t _sdd_init(SDD_DEVICE * sdd_dev)
- {
- uint32_t status = HAL_SUCCESS;
- uint32_t core_intl;
- SDD_TRACE(("_sdd_init()\r\n"));
-
- /* Perform Parameter Checking */
- #if (!SDD_SMALL_FOOTPRINT)
- /* Check supplemental init structure */
- if (sdd_dev == HAL_NULL) {
- SDD_TRACE(("null sdd_dev!\r\n"));
- return HAL_ERR_INVALID_POINTER;
- }
- /* Check duplicated device initialization */
- if (sdd.valid != 0) {
- /* Error if the device has been initialized */
- SDD_TRACE(("sdd.valid is non-zero!\r\n"));
- return SDD_INVALID_INIT;
- }
- #endif /* (!SDD_SMALL_FOOTPRINT) */
- /* Initialize SDC driver resources */
- /* Allocate DMA channel if user requested static DMA channel mode */
- switch (sdd_dev->dma) {
- case SDD_DMA_NONE:
- case SDD_DMA_DCH:
- break;
- case SDD_DMA_SCH:
- status = _sdd_alloc_dma_channel();
- if (status != HAL_SUCCESS)
- return status;
- break;
- default:
- return SDD_INVALID_PARAMETER;
- }
- sdd.dma = sdd_dev->dma;
- /*
- * Allocate mutex (a semaphore with init count == 1)
- * for driver access control
- */
- status = hal_create_mutex(&sdd.semaphore, "sdd_sem");
- if (status != HAL_SUCCESS) {
- SDD_TRACE(("Failed to create SD driver semaphore!\r\n"));
- return status;
- }
- /*
- * Allocate synchronization object for SDD to receive DMA completion notfication
- * (init count is 0 so any obtain have to wait first)
- */
- status = hal_create_semaphore(&sdd.dma_sem, 0, 0);
- if (status != HAL_SUCCESS) {
- SDD_TRACE(("Failed to allocate SD driver dma semaphore!\r\n"));
- hal_destroy_mutex(&sdd.semaphore);
- return status;
- }
- /* Register LISR to receive SDC events */
- if (sdd.lisr_registered == 0) {
- /* Register LISR for SDC events interrupt */
- status = hal_register_isr(IRQ_SDC_VECTOR, _sdd_lisr, (void*)0);
- if (status != HAL_SUCCESS) {
- SDD_TRACE(("Failed to register SD driver LISR!\r\n"));
- hal_destroy_mutex(&sdd.semaphore);
- hal_destroy_semaphore(&sdd.dma_sem);
- return status;
- }
- sdd.lisr_registered = 1;
- }
- /* Register HISR to perform deferred SDC ISR tasks */
- if (sdd.hisr_registered == 0) {
- sdd.hisr.th.fn = _sdd_hisr;
- sdd.hisr.th.arg = &sdd.hisr;
- sdd.hisr.th.ptos = &sdd_hisr_stack[SDD_HISR_STACK_SIZE];
- sdd.hisr.th.stack_size = sizeof(sdd_hisr_stack);
- sdd.hisr.th.prio = CONFIG_SDD_HISR_PRIORITY;
- sdd.hisr.th.name = "SD BH";
- status = hal_create_bh(&sdd.hisr);
- if (status != HAL_SUCCESS) {
- DEBUG(1, 1, "Failed to create SD driver HISR!\r\n");
- hal_destroy_mutex(&sdd.semaphore);
- hal_destroy_semaphore(&sdd.dma_sem);
- return status;
- }
- sdd.hisr_registered = 1;
- }
- /* Update driver data */
- #if (SDD_VFS_SUPPORT)
- // Init SDD-specific bdev object
- sdd_bdev.next = HAL_NULL;
- sdd_bdev.dev_id = 0;
- sdd_bdev.vol_id = 0;
- sdd_bdev.type = 0;
- sdd_bdev.start = 0;
- sdd_bdev.blocks = 0;
- sdd_bdev.propagate_event = HAL_NULL;
- sdd_bdev.read_sectors = _sdd_read_sectors_bdev;
- sdd_bdev.write_sectors = _sdd_write_sectors_bdev;
- sdd_dev->bdev_id = &sdd_bdev;
- #endif // SDD_VFS_SUPPORT
-
- /*
- * Initialize SDC device
- *
- * SD card initialization and identification are performed either at
- * - when the card was inserted (i.e., triggered from ISR and HISR), or
- * - when this init routine was invoked and the card was already in the slot.
- */
- /* Disable CPU interrupt */
- core_intl = hal_global_int_ctl(HAL_DISABLE_INTERRUPTS);
- /* - Disable SDC interrupt */
- hal_intc_irq_disable(IRQ_SDC_VECTOR);
-
- /* - Clear SDC interrupt status */
- hal_intc_irq_clean(IRQ_SDC_VECTOR);
-
- /* - Setup SDC interrupt trigger mode - level trigger */
- /* - Setup SDC interrupt trigger level - assert high */
- hal_intc_irq_config(IRQ_SDC_VECTOR,IRQ_LEVEL_TRIGGER, IRQ_ACTIVE_HIGH);
- /* - Enable SDC interrupt */
- hal_intc_irq_enable(IRQ_SDC_VECTOR);
- #if 0
- #if(NO_EXTERNAL_INT_CTL==1)
- /* FIXME
- * - Setup SDC interrupt trigger mode */
- /* - Enable SDC interrupt */
- SR_SETB32(NDS32_SR_INT_MASK2,IRQ_SDC_VECTOR );
- #else
- /* INTC */
- /* - Disable SDC interrupt */
- CLRB32(INTC_HW1_ER, IRQ_SDC_VECTOR);
- /* - Clear SDC interrupt status */
- SETB32(INTC_HW1_CLR, IRQ_SDC_VECTOR);
-
- /* - Setup SDC interrupt trigger mode - level trigger */
- CLRB32(INTC_HW1_TMR, IRQ_SDC_VECTOR);
-
- /* - Setup SDC interrupt trigger level - assert high */
- CLRB32(INTC_HW1_TLR, IRQ_SDC_VECTOR);
-
- /* - Enable SDC interrupt */
- SETB32(INTC_HW1_ER, IRQ_SDC_VECTOR);
- #endif
- #endif
- /* APBMCLKOFF */
- /* todo */
- /*
- * SDC_PWR_CTL
- * SD_POWER 0 (default: 0x00) (leave bootup default?)
- * SD_POWER_ON 0 (off) (default: 0x10)
- */
- CLRB32(SDC_PWR_CTL, SDC_SD_POWER_ON_BIT);
- _nds_kwait(0x4000);
- /*
- * SDC_CLK_CTL
- * CLK_DIV 0x7f (default: 0x7f, result in 97.66K ~ 683.59K SD bus freq)
- * CLK_SD 1 (default: 1 (SD))
- * CLK_DIS 1 (off) (default: 0x01)
- */
- OUT32(SDC_CLK_CTL, (SDC_CLK_OFF << SDC_CLK_DIS_BIT) |
- (SDC_CLK_SD << SDC_CLK_SD_BIT) | SDC_CLK_DIV_MASK);
- /*
- * SDC_MASK (note: spec "mask" means "enable", spec uses wrong term to describe this register)
- * RSP_CRC_FAIL mask int
- * DATA_CRC_FAIL mask int
- * RSP_TIMEOUT mask int
- * DATA_TIMEOUT mask int
- * RSP_CRC_OK mask int
- * DATA_CRC_OK mask int
- * CMD_SENT mask int
- * DATA_END mask int
- * FIFO_U_RUN mask int
- * FIFO_O_RUN mask int
- * CARD_CHANGE enable int
- */
- OUT32(SDC_MASK, SDC_CARD_CHANGE_MASK);
- /* SDC_CLEAR */
- /* Clear all */
- OUT32(SDC_CLEAR, SDC_CLEAR_ALL);
- /*
- * Following host controller register register setting needs information
- * from the card, hence is postponed in the card initialization and
- * identification routine.
- *
- * Data setup (need CSD/SCR)
- *
- * SDC_DATA_CR (setup before data transfer)
- * SDC_DATA_TIMER (TAAC/NSAC)
- * SDC_DATA_LEN (READ/WRITE/ERASE_BL_LEN, SECTOR_SIZE)
- * SDC_BUS_WIDTH (SCR::SD_BUS_WIDTHS)
- *
- * Power/Clock settings (need OCR)
- *
- * SDC_PWR_CTL (OCR::Vdd)
- * SDC_CLK_CTL (fod, fpp)
- */
- /*
- * Detect card and perform card-detection initialization, if the is
- * already in the slot before we re-enable interrupt.
- */
- if (GETB32(SDC_STATUS, SDC_SR_CARD_DETECT_BIT)) {
- /* Perform card-remove tasks (turn off clock ...) */
- SDD_TRACE(("SD card is not in slot!\r\n"));
- _sdd_cd_reset(HAL_FALSE);
- }
- else {
- /* Reset card and get device parameters */
- SDD_TRACE(("SD card is in slot!\r\n"));
- _sdd_cd_reset(HAL_TRUE);
- //_sdd_cd_reset(HAL_TRUE);
- //_sdd_cd_reset(HAL_TRUE);
- //_sdd_cd_reset(HAL_TRUE);
- // FIXME
- }
- /* Restore CPU interrupt controller to previous level */
- hal_global_int_ctl(core_intl);
- /* Mark driver state as valid */
- sdd.valid = 1;
- return HAL_SUCCESS;
- }
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_unload
- *
- * DESCRIPTION
- *
- * Remove all the device resource so the driver could be removed safely.
- *
- * NOTE
- *
- * INPUTS
- *
- * none
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SDD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static uint32_t _sdd_unload(void)
- {
- uint32_t status;
- #if (!SDD_SMALL_FOOTPRINT)
- /* Validate to terminate an already initialized driver state */
- if (sdd.valid == 0)
- return HAL_ERR_INVALID_DRIVER;
- #endif /* (!SDD_SMALL_FOOTPRINT) */
- // Notify file system
- #if (SDD_VFS_SUPPORT)
- if (sdd_bdev.propagate_event != HAL_NULL)
- sdd_bdev.propagate_event(&sdd_bdev, NDSBDEV_DEVICE_UNPLUG, HAL_NULL);
- sdd_bdev.next = HAL_NULL;
- sdd_bdev.dev_id = 0;
- sdd_bdev.vol_id = 0;
- sdd_bdev.type = 0;
- sdd_bdev.start = 0;
- sdd_bdev.blocks = 0;
- sdd_bdev.propagate_event = HAL_NULL;
- sdd_bdev.read_sectors = HAL_NULL;
- sdd_bdev.write_sectors = HAL_NULL;
- #endif // SDD_VFS_SUPPORT
- /* Shutdown device operation */
- /* Release driver resources */
- if (sdd.dma == SDD_DMA_SCH)
- _sdd_free_dma_channel();
- status = hal_destroy_bh(&sdd.hisr);
- if (status != HAL_SUCCESS)
- return status;
- status = hal_destroy_semaphore(&sdd.dma_sem);
- if (status != HAL_SUCCESS)
- return status;
- status = hal_destroy_mutex(&sdd.semaphore);
- if (status != HAL_SUCCESS)
- return status;
- sdd.valid = 0;
- return HAL_SUCCESS;
- }
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_lock
- *
- * DESCRIPTION
- *
- * Obtain exclusive access to the devcie driver.
- *
- * NOTE
- *
- * INPUTS
- *
- * none
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SDD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static inline uint32_t _sdd_lock(void)
- {
- #if (!SDD_SMALL_FOOTPRINT)
- if (sdd.valid == 0)
- return HAL_ERR_INVALID_DRIVER;
- #endif /* (!SDD_SMALL_FOOTPRINT) */
- return hal_wait_for_mutex(&sdd.semaphore, HAL_SUSPEND);
- }
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_unlock
- *
- * DESCRIPTION
- *
- * Release exclusive access to the device driver.
- *
- * NOTE
- *
- * INPUTS
- *
- * none
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SDD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static inline uint32_t _sdd_unlock(void)
- {
- #if (!SDD_SMALL_FOOTPRINT)
- if (sdd.valid == 0)
- return HAL_ERR_INVALID_DRIVER;
- #endif /* (!SDD_SMALL_FOOTPRINT) */
- return hal_release_mutex(&sdd.semaphore);
- }
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_read_sectors
- *
- * DESCRIPTION
- *
- * Read sectors of data from the card. This is the DMA/PIO mode data-
- * read dispatch function for client IOCTL request.
- *
- * NOTE
- *
- * INPUTS
- *
- * iop : pointer to SDD_IOCTL_READ_SECTORS_PARAM struct which
- * specifies the IOCTL parameters.
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SDD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static uint32_t _sdd_read_sectors(SDD_IOCTL_READ_SECTORS_PARAM * iop)
- {
- uint32_t sector = iop->lba_sector;
- uint32_t sector_cnt = iop->sector_count;
- uint8_t *io_buff = (uint8_t *) iop->io_buff;
- uint32_t status;
- uint32_t core_intl;
- uint32_t rca = 0;
- SDD_TRACE(("\r\n_sdd_read_sectors\r\n"));
- /* Disable CPU interrupt */
- core_intl = hal_global_int_ctl(HAL_DISABLE_INTERRUPTS);
- /* Check if SD card has been initialized to transfer state */
- rca = sdd.card_desc.rca;
- /* Restore CPU interrupt controller to previous level */
- hal_global_int_ctl(core_intl);
- /* Reject if SD card is not initialized to transfer state */
- if (rca == 0)
- return SDD_INVALID_INIT;
- /* Data transfer ... */
- if ((sdd.dma != SDD_DMA_NONE) /* && (hal_current() != HAL_NULL) */ ) {
- SDD_TRACE(("dma - sector(%ld) counts(%ld)\r\n", sector,
- sector_cnt));
- if (sdd.dma == SDD_DMA_DCH) {
- status = _sdd_alloc_dma_channel();
- if (status != HAL_SUCCESS)
- return status;
- }
- while (sector_cnt > 0) {
- /*
- * SDC data length register max is 24 bits (32768 sectors).
- * Divided by 2 for safe in case sector size is larger than 512 bytes.
- * (so max sector size should be less than 1024 bytes)
- */
- if (sector_cnt >= 16384) {
- /* Use SD multiple block transfer for sectors. */
- status = _sdd_read_sectors_dma(sector, 16384, io_buff);
- if (status != HAL_SUCCESS) {
- /* Release DMA channel */
- if (sdd.dma == SDD_DMA_DCH)
- _sdd_free_dma_channel();
- return status;
- }
- sector += 16384;
- sector_cnt -= 16384;
- io_buff += SDD_SECTOR_SIZE * 16384;
- }
- else {
- /* Use SD multiple block transfer for all sectors. */
- status = _sdd_read_sectors_dma(sector, sector_cnt, io_buff);
- sector_cnt = 0;
- }
- }
- /* Wait for DMA completion signal */
- /* Release DMA channel */
- if (sdd.dma == SDD_DMA_DCH)
- _sdd_free_dma_channel();
- }
- else {
- /* PIO mode data transfer */
- SDD_TRACE(("pio - sector(%ld) counts(%ld)\r\n", sector, sector_cnt));
- while (sector_cnt > 0) {
- /*
- * SDC data length register max is 24 bits (32768 sectors).
- * Divided by 2 for safe in case sector size is larger than 512 bytes.
- * (so max sector size should be less than 1024 bytes)
- */
- if (sector_cnt >= 16384) {
- /* Use SD multiple block transfer for sectors. */
- status = _sdd_read_sectors_pio(sector, 16384, io_buff);
- if (status != HAL_SUCCESS)
- return status;
- sector += 16384;
- sector_cnt -= 16384;
- io_buff += SDD_SECTOR_SIZE * 16384;
- }
- else {
- /* Use SD multiple block transfer for all sectors. */
- status = _sdd_read_sectors_pio(sector, sector_cnt, io_buff);
- sector_cnt = 0;
- }
- }
- }
- return HAL_SUCCESS;
- }
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_write_sectors
- *
- * DESCRIPTION
- *
- * Write sectors of data to the card. This is the DMA/PIO mode data-
- * write dispatch function for client IOCTL request.
- *
- * NOTE
- *
- * INPUTS
- *
- * iop : pointer to SDD_IOCTL_READ_SECTORS_PARAM struct which
- * specifies the IOCTL parameters.
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SDD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static uint32_t _sdd_write_sectors(SDD_IOCTL_WRITE_SECTORS_PARAM * iop)
- {
- uint32_t sector = iop->lba_sector;
- uint32_t sector_cnt = iop->sector_count;
- uint8_t *io_buff = (uint8_t *) iop->io_buff;
- uint32_t status;
- uint32_t core_intl;
- uint32_t rca = 0;
- SDD_TRACE(("\r\n_sdd_write_sectors\r\n"));
- /* Disable CPU interrupt */
- core_intl = hal_global_int_ctl(HAL_DISABLE_INTERRUPTS);
- /* Check if SD card has been initialized to transfer state */
- rca = sdd.card_desc.rca;
- /* Restore CPU interrupt controller to previous level */
- hal_global_int_ctl(core_intl);
- /* Reject if SD card is not initialized to transfer state */
- if (rca == 0)
- return SDD_INVALID_INIT;
- /* Data transfer ... */
- if ((sdd.dma != SDD_DMA_NONE) /* && (hal_current() != HAL_NULL) */ ) {
- SDD_TRACE(("dma - sector(%ld) counts(%ld)\r\n", sector,
- sector_cnt));
- if (sdd.dma != SDD_DMA_SCH) {
- status = _sdd_alloc_dma_channel();
- if (status != HAL_SUCCESS)
- return status;
- }
- while (sector_cnt > 0) {
- /*
- * SDC data length register max is 24 bits (32768 sectors).
- * Divided by 2 for safe in case sector size is larger than 512 bytes.
- * (so max sector size should be less than 1024 bytes)
- */
- if (sector_cnt >= 16384) {
- /* Use SD multiple block transfer for sectors. */
- status = _sdd_write_sectors_dma(sector, 16384, io_buff);
- if (status != HAL_SUCCESS) {
- if (sdd.dma == SDD_DMA_DCH)
- _sdd_free_dma_channel();
- return status;
- }
- sector += 16384;
- sector_cnt -= 16384;
- io_buff += SDD_SECTOR_SIZE * 16384;
- }
- else {
- /* Use SD multiple block transfer for all sectors. */
- status = _sdd_write_sectors_dma(sector, sector_cnt, io_buff);
- sector_cnt = 0;
- }
- }
- /* Wait for DMA completion signal */
- /* Release DMA channel */
- if (sdd.dma == SDD_DMA_DCH)
- _sdd_free_dma_channel();
- }
- else {
- /* PIO mode data transfer */
- SDD_TRACE(("pio - sector(%ld) counts(%ld)\r\n", sector,
- sector_cnt));
- while (sector_cnt > 0) {
- /*
- * SDC data length register max is 24 bits (32768 sectors).
- * Divided by 2 for safe in case sector size is larger than 512 bytes.
- * (so max sector size should be less than 1024 bytes)
- */
- if (sector_cnt >= 16384) {
- /* Use SD multiple block transfer for sectors. */
- status = _sdd_write_sectors_pio(sector, 16384, io_buff);
- if (status != HAL_SUCCESS)
- return status;
- sector += 16384;
- sector_cnt -= 16384;
- io_buff += SDD_SECTOR_SIZE * 16384;
- }
- else {
- /* Use SD multiple block transfer for all sectors. */
- status = _sdd_write_sectors_pio(sector, sector_cnt, io_buff);
- sector_cnt = 0;
- }
- }
- }
- return HAL_SUCCESS;
- }
- /*****************************************************************************
- * FUNCTION
- *
- * _sdd_ioctl
- *
- * DESCRIPTION
- *
- * This function execute I/O control commands to SDC driver.
- *
- * NOTE
- *
- *
- * INPUTS
- *
- * driver : device object/control_block
- * request : request function and supplemental parameters
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else positive value is SDD-specific error code,
- * else negative value is NU system error code.
- *
- ****************************************************************************/
- static inline uint32_t _sdd_ioctl(uint32_t ioctl, void *param)
- {
- uint32_t status = HAL_SUCCESS;
- #if (!SDD_SMALL_FOOTPRINT)
- /* Validate to terminate an already initialized driver state */
- if (sdd.valid == 0)
- return HAL_ERR_INVALID_DRIVER;
- #endif /* (!UARTD_SMALL_FOOTPRINT) */
- if (param == HAL_NULL)
- return SDD_INVALID_PARAMETER;
- switch (ioctl) {
- case SDD_IOCTL_READ_SECTORS:
- /* Parameter: pointer to SDD_IOCTL_READ_SECTORS_PARAM struct */
- status = _sdd_read_sectors((SDD_IOCTL_READ_SECTORS_PARAM *) param);
- break;
- case SDD_IOCTL_WRITE_SECTORS:
- /* Parameter: pointer to SDD_IOCTL_READ_SECTORS_PARAM struct */
- status = _sdd_write_sectors((SDD_IOCTL_WRITE_SECTORS_PARAM *) param);
- break;
- default:
- status = SDD_INVALID_IOCTL;
- break;
- }
- return status;
- }
- /*****************************************************************************
- * FUNCTION
- *
- * NDS_SD_Init
- *
- * DESCRIPTION
- *
- * This function initializes the SD device.
- *
- * INPUTS
- *
- * sdd_dev : User mode SDD device structure.
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else a negative value is returned.
- *
- ****************************************************************************/
- uint32_t NDS_SD_Init(SDD_DEVICE * sdd_dev)
- {
- return _sdd_init(sdd_dev);
- }
- /*****************************************************************************
- * FUNCTION
-
- * NDS_SD_Unload
- *
- * DESCRIPTION
- *
- * This function terminates all SD transaction and unload the driver.
- *
- * INPUTS
- *
- * None.
- *
- * OUTPUTS
- *
- * None.
- *
- ****************************************************************************/
- void NDS_SD_Unload()
- {
- _sdd_unload();
- }
- /*****************************************************************************
- * FUNCTION
- *
- * NDS_SD_ReadSectors
- *
- * DESCRIPTION
- *
- * This function read sectors of data from the SD memory card.
- *
- * INPUTS
- *
- * sdd_dev : User mode SDD device structure.
- * sector : The starting LBA sector number.
- * sector_count : Number of sectors to read.
- * sector_size : Size of a sector in bytes.
- * buffer : Pointer to a user buffer to hold the readback data.
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else a negative value is returned.
- *
- ****************************************************************************/
- uint32_t NDS_SD_ReadSectors(SDD_DEVICE * sdd_dev, uint32_t sector,
- uint32_t sector_count, uint32_t sector_size,
- void *buffer)
- {
- uint32_t status;
- uint32_t io_status;
- SDD_IOCTL_READ_SECTORS_PARAM iop;
- status = _sdd_lock();
- if (status != HAL_SUCCESS)
- hal_system_error(status);
- iop.lba_sector = sector;
- iop.sector_count = sector_count;
- iop.sector_size = sector_size;
- iop.io_buff = buffer;
- io_status = _sdd_read_sectors(&iop);
- status = _sdd_unlock();
- if (status != HAL_SUCCESS)
- hal_system_error(status);
- return io_status;
- }
- /*****************************************************************************
- * FUNCTION
- *
- * NDS_SD_WriteSectors
- *
- * DESCRIPTION
- *
- * This function write sectors of data to the SD memory card.
- *
- * INPUTS
- *
- * sdd_dev : User mode SDD device structure.
- * sector : The starting LBA sector number.
- * sector_count : Number of sectors to write.
- * sector_size : Size of a sector in bytes.
- * buffer : Pointer to a user buffer of data to be written to the card.
- *
- * OUTPUTS
- *
- * uint32_t : Returns HAL_SUCCESS if successful initialization,
- * else a negative value is returned.
- *
- ****************************************************************************/
- uint32_t NDS_SD_WriteSectors(SDD_DEVICE * sdd_dev, uint32_t sector,
- uint32_t sector_count, uint32_t sector_size,
- void *buffer)
- {
- uint32_t status;
- uint32_t io_status;
- SDD_IOCTL_WRITE_SECTORS_PARAM iop;
- status = _sdd_lock();
- if (status != HAL_SUCCESS)
- hal_system_error(status);
- iop.lba_sector = sector;
- iop.sector_count = sector_count;
- iop.sector_size = sector_size;
- iop.io_buff = buffer;
- io_status = _sdd_write_sectors(&iop);
- status = _sdd_unlock();
- if (status != HAL_SUCCESS)
- hal_system_error(status);
- return io_status;
- }
|