007-mtd.patch 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949
  1. --- a/drivers/mtd/chips/Kconfig
  2. +++ b/drivers/mtd/chips/Kconfig
  3. @@ -220,6 +220,13 @@
  4. This option enables basic support for ROM chips accessed through
  5. a bus mapping driver.
  6. +config MTD_SERIAL
  7. + tristate "Support for Serial chips in bus mapping"
  8. + depends on MTD
  9. + help
  10. + This option enables basic support for Serial chips accessed through
  11. + a bus mapping driver.
  12. +
  13. config MTD_ABSENT
  14. tristate "Support for absent chips in bus mapping"
  15. help
  16. --- a/drivers/mtd/chips/cfi_cmdset_0002.c
  17. +++ b/drivers/mtd/chips/cfi_cmdset_0002.c
  18. @@ -39,10 +39,15 @@
  19. #include <linux/mtd/cfi.h>
  20. #include <linux/mtd/xip.h>
  21. +//****** Storlink SoC ******
  22. #define AMD_BOOTLOC_BUG
  23. -#define FORCE_WORD_WRITE 0
  24. -
  25. -#define MAX_WORD_RETRIES 3
  26. +//#define FORCE_WORD_WRITE 0
  27. +#define FORCE_WORD_WRITE 1
  28. +#define FORCE_FAST_PROG 0
  29. +
  30. +//#define MAX_WORD_RETRIES 3
  31. +#define MAX_WORD_RETRIES 3 // CONFIG_MTD_CFI_AMDSTD_RETRY
  32. +//**************************
  33. #define MANUFACTURER_AMD 0x0001
  34. #define MANUFACTURER_ATMEL 0x001F
  35. @@ -322,6 +327,13 @@
  36. #endif
  37. bootloc = extp->TopBottom;
  38. +//****** Storlink SoC ******
  39. + if(bootloc == 5)
  40. + {
  41. + bootloc = 3;
  42. + extp->TopBottom = 3;
  43. + }
  44. +//**************************
  45. if ((bootloc != 2) && (bootloc != 3)) {
  46. printk(KERN_WARNING "%s: CFI does not contain boot "
  47. "bank location. Assuming top.\n", map->name);
  48. @@ -340,6 +352,9 @@
  49. cfi->cfiq->EraseRegionInfo[j] = swap;
  50. }
  51. }
  52. +#ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
  53. + cfi->device_type = CFI_DEVICETYPE_X8;
  54. +#endif
  55. /* Set the default CFI lock/unlock addresses */
  56. cfi->addr_unlock1 = 0x555;
  57. cfi->addr_unlock2 = 0x2aa;
  58. @@ -461,6 +476,7 @@
  59. map_word d, t;
  60. d = map_read(map, addr);
  61. + udelay(20); //Storlink SoC
  62. t = map_read(map, addr);
  63. return map_word_equal(map, d, t);
  64. @@ -626,7 +642,9 @@
  65. default:
  66. printk(KERN_ERR "MTD: put_chip() called with oldstate %d!!\n", chip->oldstate);
  67. }
  68. +//****** Storlink SoC ******
  69. wake_up(&chip->wq);
  70. +//**************************
  71. }
  72. #ifdef CONFIG_MTD_XIP
  73. @@ -940,7 +958,9 @@
  74. cfi_send_gen_cmd(0x90, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  75. cfi_send_gen_cmd(0x00, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  76. +//****** Storlink SoC ******
  77. wake_up(&chip->wq);
  78. +//**************************
  79. spin_unlock(chip->mutex);
  80. return 0;
  81. @@ -1005,7 +1025,10 @@
  82. */
  83. unsigned long uWriteTimeout = ( HZ / 1000 ) + 1;
  84. int ret = 0;
  85. - map_word oldd;
  86. +//****** Storlink SoC ******
  87. +// map_word oldd;
  88. + map_word oldd, tmp;
  89. +//**************************
  90. int retry_cnt = 0;
  91. adr += chip->start;
  92. @@ -1037,9 +1060,15 @@
  93. ENABLE_VPP(map);
  94. xip_disable(map, chip, adr);
  95. retry:
  96. +//****** Storlink SoC ******
  97. +#if FORCE_FAST_PROG /* Unlock bypass */
  98. + cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  99. +#else
  100. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  101. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  102. cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  103. +#endif
  104. +//**************************
  105. map_write(map, datum, adr);
  106. chip->state = FL_WRITING;
  107. @@ -1072,7 +1101,13 @@
  108. }
  109. if (chip_ready(map, adr))
  110. - break;
  111. + {
  112. + tmp = map_read(map, adr);
  113. + if(map_word_equal(map, tmp, datum))
  114. +// goto op_done;
  115. + break;
  116. +
  117. + }
  118. /* Latency issues. Drop the lock, wait a while and retry */
  119. UDELAY(map, chip, adr, 1);
  120. @@ -1084,8 +1119,17 @@
  121. /* FIXME - should have reset delay before continuing */
  122. if (++retry_cnt <= MAX_WORD_RETRIES)
  123. + {
  124. +//****** Storlink SoC ******
  125. +#if FORCE_FAST_PROG
  126. + cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  127. + cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  128. + cfi_send_gen_cmd(0x20, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  129. + //udelay(1);
  130. +#endif
  131. + udelay(1);
  132. goto retry;
  133. -
  134. + }
  135. ret = -EIO;
  136. }
  137. xip_enable(map, chip, adr);
  138. @@ -1171,7 +1215,14 @@
  139. return 0;
  140. }
  141. }
  142. -
  143. +//****** Storlink SoC ******
  144. + map_write( map, CMD(0xF0), chipstart );
  145. +#if FORCE_FAST_PROG
  146. + cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
  147. + cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chipstart, map, cfi, cfi->device_type, NULL);
  148. + cfi_send_gen_cmd(0x20, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
  149. +#endif
  150. +//**************************
  151. /* We are now aligned, write as much as possible */
  152. while(len >= map_bankwidth(map)) {
  153. map_word datum;
  154. @@ -1181,7 +1232,15 @@
  155. ret = do_write_oneword(map, &cfi->chips[chipnum],
  156. ofs, datum);
  157. if (ret)
  158. + {
  159. +//****** Storlink SoC ******
  160. +#if FORCE_FAST_PROG
  161. + /* Get out of unlock bypass mode */
  162. + cfi_send_gen_cmd(0x90, 0, chipstart, map, cfi, cfi->device_type, NULL);
  163. + cfi_send_gen_cmd(0x00, 0, chipstart, map, cfi, cfi->device_type, NULL);
  164. +#endif
  165. return ret;
  166. + }
  167. ofs += map_bankwidth(map);
  168. buf += map_bankwidth(map);
  169. @@ -1189,19 +1248,38 @@
  170. len -= map_bankwidth(map);
  171. if (ofs >> cfi->chipshift) {
  172. +//****** Storlink SoC ******
  173. +#if FORCE_FAST_PROG
  174. + /* Get out of unlock bypass mode */
  175. + cfi_send_gen_cmd(0x90, 0, chipstart, map, cfi, cfi->device_type, NULL);
  176. + cfi_send_gen_cmd(0x00, 0, chipstart, map, cfi, cfi->device_type, NULL);
  177. +#endif
  178. chipnum ++;
  179. ofs = 0;
  180. if (chipnum == cfi->numchips)
  181. return 0;
  182. chipstart = cfi->chips[chipnum].start;
  183. +#if FORCE_FAST_PROG
  184. + /* Go into unlock bypass mode for next set of chips */
  185. + cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
  186. + cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chipstart, map, cfi, cfi->device_type, NULL);
  187. + cfi_send_gen_cmd(0x20, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
  188. +#endif
  189. }
  190. }
  191. +#if FORCE_FAST_PROG
  192. + /* Get out of unlock bypass mode */
  193. + cfi_send_gen_cmd(0x90, 0, chipstart, map, cfi, cfi->device_type, NULL);
  194. + cfi_send_gen_cmd(0x00, 0, chipstart, map, cfi, cfi->device_type, NULL);
  195. +#endif
  196. +
  197. /* Write the trailing bytes if any */
  198. if (len & (map_bankwidth(map)-1)) {
  199. map_word tmp_buf;
  200. retry1:
  201. +
  202. spin_lock(cfi->chips[chipnum].mutex);
  203. if (cfi->chips[chipnum].state != FL_READY) {
  204. @@ -1221,7 +1299,11 @@
  205. #endif
  206. goto retry1;
  207. }
  208. -
  209. +#if FORCE_FAST_PROG
  210. + cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
  211. + cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chipstart, map, cfi, cfi->device_type, NULL);
  212. + cfi_send_gen_cmd(0x20, cfi->addr_unlock1, chipstart, map, cfi, cfi->device_type, NULL);
  213. +#endif
  214. tmp_buf = map_read(map, ofs + chipstart);
  215. spin_unlock(cfi->chips[chipnum].mutex);
  216. @@ -1231,11 +1313,23 @@
  217. ret = do_write_oneword(map, &cfi->chips[chipnum],
  218. ofs, tmp_buf);
  219. if (ret)
  220. + {
  221. +#if FORCE_FAST_PROG
  222. + /* Get out of unlock bypass mode */
  223. + cfi_send_gen_cmd(0x90, 0, chipstart, map, cfi, cfi->device_type, NULL);
  224. + cfi_send_gen_cmd(0x00, 0, chipstart, map, cfi, cfi->device_type, NULL);
  225. +#endif
  226. return ret;
  227. -
  228. + }
  229. +#if FORCE_FAST_PROG
  230. + /* Get out of unlock bypass mode */
  231. + cfi_send_gen_cmd(0x90, 0, chipstart, map, cfi, cfi->device_type, NULL);
  232. + cfi_send_gen_cmd(0x00, 0, chipstart, map, cfi, cfi->device_type, NULL);
  233. +#endif
  234. (*retlen) += len;
  235. }
  236. + map_write( map, CMD(0xF0), chipstart );
  237. return 0;
  238. }
  239. @@ -1275,6 +1369,7 @@
  240. ENABLE_VPP(map);
  241. xip_disable(map, chip, cmd_adr);
  242. + map_write( map, CMD(0xF0), chip->start ); //Storlink
  243. cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  244. cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, cfi->device_type, NULL);
  245. //cfi_send_gen_cmd(0xA0, cfi->addr_unlock1, chip->start, map, cfi, cfi->device_type, NULL);
  246. @@ -1535,6 +1630,9 @@
  247. DECLARE_WAITQUEUE(wait, current);
  248. int ret = 0;
  249. +#ifdef CONFIG_SL2312_SHARE_PIN
  250. + mtd_lock(); // sl2312 share pin lock
  251. +#endif
  252. adr += chip->start;
  253. spin_lock(chip->mutex);
  254. @@ -1613,6 +1711,9 @@
  255. chip->state = FL_READY;
  256. put_chip(map, chip, adr);
  257. spin_unlock(chip->mutex);
  258. +#ifdef CONFIG_SL2312_SHARE_PIN
  259. + mtd_unlock(); // sl2312 share pin lock
  260. +#endif
  261. return ret;
  262. }
  263. --- /dev/null
  264. +++ b/drivers/mtd/chips/map_serial.c
  265. @@ -0,0 +1,188 @@
  266. +/*
  267. + * Common code to handle map devices which are simple ROM
  268. + * (C) 2000 Red Hat. GPL'd.
  269. + * $Id: map_serial.c,v 1.3 2006/06/05 02:34:54 middle Exp $
  270. + */
  271. +
  272. +#include <linux/version.h>
  273. +#include <linux/module.h>
  274. +#include <linux/types.h>
  275. +#include <linux/kernel.h>
  276. +#include <asm/io.h>
  277. +
  278. +#include <asm/byteorder.h>
  279. +#include <linux/errno.h>
  280. +#include <linux/slab.h>
  281. +
  282. +#include <asm/hardware.h>
  283. +#include <linux/mtd/map.h>
  284. +#include <linux/mtd/mtd.h>
  285. +#include <linux/init.h> //add
  286. +#include <asm/arch/sl2312.h>
  287. +#include <asm/arch/flash.h>
  288. +
  289. +static int mapserial_erase(struct mtd_info *mtd, struct erase_info *instr);
  290. +static int mapserial_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
  291. +static int mapserial_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
  292. +static void mapserial_nop (struct mtd_info *);
  293. +struct mtd_info *map_serial_probe(struct map_info *map);
  294. +
  295. +extern int m25p80_sector_erase(__u32 address, __u32 schip_en);
  296. +
  297. +static struct mtd_chip_driver mapserial_chipdrv = {
  298. + probe: map_serial_probe,
  299. + name: "map_serial",
  300. + module: THIS_MODULE
  301. +};
  302. +
  303. +struct mtd_info *map_serial_probe(struct map_info *map)
  304. +{
  305. + struct mtd_info *mtd;
  306. +
  307. + mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
  308. + if (!mtd)
  309. + return NULL;
  310. +
  311. + memset(mtd, 0, sizeof(*mtd));
  312. +
  313. + map->fldrv = &mapserial_chipdrv;
  314. + mtd->priv = map;
  315. + mtd->name = map->name;
  316. + mtd->type = MTD_OTHER;
  317. + mtd->erase = mapserial_erase;
  318. + mtd->size = map->size;
  319. + mtd->read = mapserial_read;
  320. + mtd->write = mapserial_write;
  321. + mtd->sync = mapserial_nop;
  322. + mtd->flags = (MTD_WRITEABLE|MTD_ERASEABLE);
  323. +// mtd->erasesize = 512; // page size;
  324. +#ifdef CONFIG_MTD_SL2312_SERIAL_ST
  325. + mtd->erasesize = M25P80_SECTOR_SIZE; // block size;
  326. +#else
  327. + mtd->erasesize = 0x1000; // block size;
  328. +#endif
  329. +
  330. + __module_get(THIS_MODULE);
  331. + //MOD_INC_USE_COUNT;
  332. + return mtd;
  333. +}
  334. +
  335. +#define FLASH_ACCESS_OFFSET 0x00000010
  336. +#define FLASH_ADDRESS_OFFSET 0x00000014
  337. +#define FLASH_WRITE_DATA_OFFSET 0x00000018
  338. +#define FLASH_READ_DATA_OFFSET 0x00000018
  339. +
  340. +static __u32 readflash_ctrl_reg(__u32 ofs)
  341. +{
  342. + __u32 *base;
  343. +
  344. + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
  345. + return __raw_readl(base);
  346. +}
  347. +
  348. +static void writeflash_ctrl_reg(__u32 data, __u32 ofs)
  349. +{
  350. + __u32 *base;
  351. +
  352. + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
  353. + __raw_writel(data, base);
  354. +}
  355. +
  356. +static int mapserial_erase_block(struct map_info *map,unsigned int block)
  357. +{
  358. +
  359. + __u32 address;
  360. +#ifdef CONFIG_MTD_SL2312_SERIAL_ST
  361. +
  362. + if(!m25p80_sector_erase(block, 0))
  363. + return (MTD_ERASE_DONE);
  364. +#else
  365. + __u32 opcode;
  366. + __u32 count=0;
  367. +// __u8 status;
  368. +
  369. + // printk("mapserial_erase_block : erase block %d \n",block);
  370. +// opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | cmd;
  371. + opcode = 0x80000000 | 0x0200 | 0x50;
  372. + address = (block << 13);
  373. + writeflash_ctrl_reg(address,FLASH_ADDRESS_OFFSET);
  374. + writeflash_ctrl_reg(opcode,FLASH_ACCESS_OFFSET);
  375. + opcode=readflash_ctrl_reg(FLASH_ACCESS_OFFSET);
  376. + while(opcode&0x80000000)
  377. + {
  378. + opcode = readflash_ctrl_reg(FLASH_ACCESS_OFFSET);
  379. + count++;
  380. + if (count > 10000)
  381. + {
  382. + return (MTD_ERASE_FAILED);
  383. + }
  384. + }
  385. + return (MTD_ERASE_DONE);
  386. +#endif
  387. +}
  388. +
  389. +static int mapserial_erase(struct mtd_info *mtd, struct erase_info *instr)
  390. +{
  391. + struct map_info *map = (struct map_info *)mtd->priv;
  392. + unsigned int addr;
  393. + int len;
  394. + unsigned int block;
  395. + unsigned int ret=0;
  396. +
  397. + addr = instr->addr;
  398. + len = instr->len;
  399. + while (len > 0)
  400. + {
  401. + block = addr / mtd->erasesize;
  402. +#ifdef CONFIG_MTD_SL2312_SERIAL_ST
  403. + ret = mapserial_erase_block(map,addr);
  404. +#else
  405. + ret = mapserial_erase_block(map,block);
  406. +#endif
  407. + addr = addr + mtd->erasesize;
  408. + len = len - mtd->erasesize;
  409. + }
  410. + return (ret);
  411. +}
  412. +
  413. +static int mapserial_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
  414. +{
  415. + struct map_info *map = (struct map_info *)mtd->priv;
  416. +// printk("mapserial_read : \n");
  417. + map->copy_from(map, buf, from, len);
  418. + *retlen = len;
  419. + return 0;
  420. +}
  421. +
  422. +static void mapserial_nop(struct mtd_info *mtd)
  423. +{
  424. + /* Nothing to see here */
  425. +}
  426. +
  427. +static int mapserial_write (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
  428. +{
  429. + struct map_info *map = (struct map_info *)mtd->priv;
  430. +// printk("mapserial_write : buf %x to %x len %x \n",(int)buf, (int)to, (int)len);
  431. + //map->copy_to(map, buf, to, len);
  432. + map->copy_to(map, to, buf, len);
  433. + *retlen = len;
  434. + return 0;
  435. +}
  436. +
  437. +int __init map_serial_init(void)
  438. +{
  439. + register_mtd_chip_driver(&mapserial_chipdrv);
  440. + return 0;
  441. +}
  442. +
  443. +static void __exit map_serial_exit(void)
  444. +{
  445. + unregister_mtd_chip_driver(&mapserial_chipdrv);
  446. +}
  447. +
  448. +module_init(map_serial_init);
  449. +module_exit(map_serial_exit);
  450. +
  451. +MODULE_LICENSE("GPL");
  452. +MODULE_AUTHOR("David Woodhouse <[email protected]>");
  453. +MODULE_DESCRIPTION("MTD chip driver for ROM chips");
  454. --- a/drivers/mtd/maps/Kconfig
  455. +++ b/drivers/mtd/maps/Kconfig
  456. @@ -614,5 +614,30 @@
  457. This selection automatically selects the map_ram driver.
  458. +#***************************************************************************************
  459. +# Storlink parallel/Serial Flash configuration
  460. +#***************************************************************************************
  461. +config MTD_SL2312_CFI
  462. + tristate "CFI Flash device mapped on SL2312"
  463. + depends on MTD_CFI
  464. + help
  465. + Map driver for SL2312 demo board.
  466. +
  467. +config MTD_SL2312_SERIAL_ATMEL
  468. + tristate "ATMEL Serial Flash device mapped on SL2312"
  469. + depends on MTD_PARTITIONS && ARCH_SL2312
  470. + help
  471. + Map driver for SL2312 demo board.
  472. +
  473. +config MTD_SL2312_SERIAL_ST
  474. + tristate "ST Serial Flash device mapped on SL2312"
  475. + depends on MTD_PARTITIONS && ARCH_SL2312
  476. + help
  477. + Map driver for SL2312 demo board.
  478. +
  479. +config SL2312_SHARE_PIN
  480. + tristate "Parallel Flash share pin on SL2312 ASIC"
  481. + depends on SL3516_ASIC
  482. +
  483. endmenu
  484. --- /dev/null
  485. +++ b/drivers/mtd/maps/sl2312-flash-atmel.c
  486. @@ -0,0 +1,554 @@
  487. +/*
  488. + * $Id: sl2312-flash-atmel.c,v 1.2 2006/06/05 02:35:57 middle Exp $
  489. + *
  490. + * Flash and EPROM on Hitachi Solution Engine and similar boards.
  491. + *
  492. + * (C) 2001 Red Hat, Inc.
  493. + *
  494. + * GPL'd
  495. + */
  496. +
  497. +#include <linux/module.h>
  498. +#include <linux/types.h>
  499. +#include <linux/kernel.h>
  500. +
  501. +#include <asm/io.h>
  502. +#include <linux/mtd/mtd.h>
  503. +#include <linux/mtd/map.h>
  504. +#include <linux/mtd/partitions.h>
  505. +#include <asm/hardware.h>
  506. +
  507. +#include <asm/arch/sl2312.h>
  508. +#include <asm/arch/flash.h>
  509. +#include <linux/init.h> //add
  510. +
  511. +
  512. +#define g_page_addr AT45DB321_PAGE_SHIFT //321 : shift 10 ; 642 : shift 11
  513. +#define g_chipen SERIAL_FLASH_CHIP0_EN //atmel
  514. +
  515. +extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts);
  516. +
  517. +void address_to_page(__u32 address, __u16 *page, __u16 *offset)
  518. +{
  519. + *page = address / SPAGE_SIZE;
  520. + *offset = address % SPAGE_SIZE;
  521. +}
  522. +
  523. +static __u32 read_flash_ctrl_reg(__u32 ofs)
  524. +{
  525. + __u32 *base;
  526. +
  527. + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
  528. + return __raw_readl(base);
  529. +}
  530. +
  531. +static void write_flash_ctrl_reg(__u32 ofs,__u32 data)
  532. +{
  533. + __u32 *base;
  534. +
  535. + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
  536. + __raw_writel(data, base);
  537. +}
  538. +
  539. +void atmel_read_status(__u8 cmd, __u8 *data)
  540. +{
  541. + __u32 opcode;
  542. + __u32 value;
  543. +
  544. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | cmd | g_chipen;
  545. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  546. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  547. + while(opcode&0x80000000)
  548. + {
  549. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  550. + flash_delay();
  551. + schedule();
  552. + }
  553. +
  554. + value=read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  555. + *data = value & 0xff;
  556. +}
  557. +
  558. +void main_memory_page_read(__u8 cmd, __u16 page, __u16 offset, __u8 *data)
  559. +{
  560. + __u32 opcode;
  561. + __u32 address;
  562. + __u32 value;
  563. +
  564. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS_4X_DATA | cmd | g_chipen;
  565. + address = (page << g_page_addr) + offset;
  566. + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
  567. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  568. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  569. + while(opcode&0x80000000)
  570. + {
  571. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  572. + flash_delay();
  573. + schedule();
  574. + }
  575. +
  576. + value=read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  577. + *data = value & 0xff;
  578. +}
  579. +
  580. +void buffer_to_main_memory(__u8 cmd, __u16 page)
  581. +{
  582. + __u32 opcode;
  583. + __u32 address;
  584. + __u8 status;
  585. +
  586. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | cmd | g_chipen;
  587. + address = (page << g_page_addr);
  588. + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
  589. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  590. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  591. + while(opcode&0x80000000)
  592. + {
  593. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  594. + flash_delay();
  595. + schedule();
  596. + }
  597. + atmel_read_status(READ_STATUS_SPI, &status);
  598. + while(!(status&0x80))
  599. + {
  600. + atmel_read_status(READ_STATUS_SPI, &status);
  601. + flash_delay();
  602. + schedule();
  603. + }
  604. +
  605. +}
  606. +
  607. +
  608. +void atmel_flash_read_page(__u32 address, __u8 *buffer, __u32 len)
  609. +{
  610. + __u8 byte;
  611. + __u16 page, offset;
  612. + __u16 i;
  613. +
  614. + address_to_page(address, &page, &offset);
  615. +
  616. + for(i=0; i<len; i++,offset++)
  617. + {
  618. + main_memory_page_read(MAIN_MEMORY_PAGE_READ_SPI , page, offset, &byte);
  619. + buffer [i]= byte;
  620. + }
  621. +}
  622. +
  623. +void atmel_flash_program_page(__u32 address, __u8 *buffer, __u32 len)
  624. +{
  625. + __u8 pattern;
  626. + __u16 page, offset;
  627. + __u32 i;
  628. +
  629. + address_to_page(address, &page, &offset);
  630. + // printk("atmel_flash_program_page: offset %x len %x page %x \n", offset, len, page);
  631. +
  632. + if(offset)
  633. + main_memory_to_buffer(MAIN_MEMORY_TO_BUFFER1,page);
  634. +
  635. + for(i=0; i<len; i++,offset++)
  636. + {
  637. + pattern = buffer[i];
  638. + atmel_buffer_write(BUFFER1_WRITE,offset,pattern);
  639. + }
  640. +
  641. + // printk("atmel_flash_program_page: offset %x \n", offset);
  642. + buffer_to_main_memory(BUFFER1_TO_MAIN_MEMORY, page);
  643. + // printk("atmel_flash_program_page: buffer_to_main_memory %x page\n", page);
  644. +
  645. +}
  646. +
  647. +
  648. +void main_memory_to_buffer(__u8 cmd, __u16 page)
  649. +{
  650. + __u32 opcode;
  651. + __u32 address;
  652. + __u8 status;
  653. +
  654. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | cmd | g_chipen;
  655. + address = (page << g_page_addr);
  656. + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
  657. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  658. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  659. + while(opcode&0x80000000)
  660. + {
  661. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  662. + flash_delay();
  663. + schedule();
  664. + }
  665. + atmel_read_status(READ_STATUS_SPI, &status);
  666. + while(!(status&0x80))
  667. + {
  668. + atmel_read_status(READ_STATUS_SPI, &status);
  669. + flash_delay();
  670. + schedule();
  671. + }
  672. +
  673. +}
  674. +
  675. +void main_memory_page_program(__u8 cmd, __u16 page, __u16 offset, __u8 data)
  676. +{
  677. + __u32 opcode;
  678. + __u32 address;
  679. + __u8 status;
  680. +
  681. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS_DATA | cmd | g_chipen;
  682. + address = (page << g_page_addr) + offset;
  683. + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
  684. + write_flash_ctrl_reg(FLASH_WRITE_DATA_OFFSET, data);
  685. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  686. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  687. + while(opcode&0x80000000)
  688. + {
  689. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  690. + flash_delay();
  691. + schedule();
  692. + }
  693. + atmel_read_status(READ_STATUS_SPI, &status);
  694. + while(!(status&0x80))
  695. + {
  696. + atmel_read_status(READ_STATUS_SPI, &status);
  697. + flash_delay();
  698. + schedule();
  699. + }
  700. +}
  701. +
  702. +void atmel_buffer_write(__u8 cmd, __u16 offset, __u8 data)
  703. +{
  704. + __u32 opcode;
  705. + __u32 address;
  706. +
  707. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS_DATA | cmd | g_chipen;
  708. + address = offset;
  709. + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
  710. + write_flash_ctrl_reg(FLASH_WRITE_DATA_OFFSET, data);
  711. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  712. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  713. + while(opcode&0x80000000)
  714. + {
  715. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  716. + flash_delay();
  717. + schedule();
  718. + }
  719. +
  720. +}
  721. +
  722. +void atmel_erase_page(__u8 cmd, __u16 page)
  723. +{
  724. + __u32 opcode;
  725. + __u32 address;
  726. + __u8 status;
  727. +
  728. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | cmd | g_chipen;
  729. + address = (page << g_page_addr);
  730. + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
  731. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  732. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  733. + while(opcode&0x80000000)
  734. + {
  735. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  736. + flash_delay();
  737. + schedule();
  738. + }
  739. + atmel_read_status(READ_STATUS_SPI, &status);
  740. + while(!(status&0x80))
  741. + {
  742. + atmel_read_status(READ_STATUS_SPI, &status);
  743. + flash_delay();
  744. + schedule();
  745. + }
  746. +
  747. +}
  748. +
  749. +void atmel_erase_block(__u8 cmd, __u16 block)
  750. +{
  751. + __u32 opcode;
  752. + __u32 address;
  753. + __u8 status;
  754. +
  755. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | cmd | g_chipen;
  756. + address = (block << 13);
  757. + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
  758. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  759. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  760. + while(opcode&0x80000000)
  761. + {
  762. + opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  763. + flash_delay();
  764. + schedule();
  765. + }
  766. + atmel_read_status(READ_STATUS_SPI, &status);
  767. + while(!(status&0x80))
  768. + {
  769. + atmel_read_status(READ_STATUS_SPI, &status);
  770. + flash_delay();
  771. + schedule();
  772. + }
  773. +
  774. +}
  775. +
  776. +void flash_delay(void)
  777. +{
  778. + int i;
  779. +
  780. + for(i=0; i<50; i++)
  781. + i=i;
  782. +}
  783. +
  784. +
  785. +
  786. +
  787. +__u32 sl2312_read32(struct map_info *map, unsigned long ofs)
  788. +{
  789. +
  790. +#if 0
  791. + __u16 page, offset;
  792. + __u32 pattern;
  793. + __u8 byte, i;
  794. +
  795. + pattern = 0;
  796. + address_to_page(ofs, &page, &offset);
  797. + for(i=0; i<4; i++, offset++)
  798. + {
  799. + pattern = pattern << 8;
  800. + main_memory_page_read(MAIN_MEMORY_PAGE_READ_SPI , page, offset, &byte);
  801. +//printk("sl2312_read32:: address = %08x data = %c \n",ofs,byte);
  802. + pattern += byte;
  803. + }
  804. + return pattern;
  805. +#else
  806. + return read_flash_ctrl_reg(ofs);
  807. +#endif
  808. +
  809. +}
  810. +
  811. +__u8 sl2312_read8(struct map_info *map, unsigned long ofs)
  812. +{
  813. + __u16 page, offset;
  814. + __u8 byte;
  815. +
  816. + address_to_page(ofs, &page, &offset);
  817. + main_memory_page_read(MAIN_MEMORY_PAGE_READ_SPI , page, offset, &byte);
  818. + //printk("sl2312_read8:: address = %08x data = %c \n",ofs,byte);
  819. + return byte;
  820. +
  821. +}
  822. +
  823. +void sl2312_write32(struct map_info *map, __u32 d, unsigned long ofs)
  824. +{
  825. +#if 0
  826. + __u16 page, offset;
  827. + __u8 byte, i;
  828. +
  829. + address_to_page(ofs, &page, &offset);
  830. + for(i=0; i<4; i++, offset++)
  831. + {
  832. + byte = d & 0xff;
  833. + main_memory_page_program(MAIN_MEMORY_PROGRAM_BUFFER1, page, offset, byte);
  834. + d = d >> 8;
  835. +//printk("sl2312_write32:: address = %08x data = %c \n",ofs,byte);
  836. + }
  837. +#else
  838. + write_flash_ctrl_reg(ofs, d);
  839. +#endif
  840. +}
  841. +
  842. +void sl2312_write8(struct map_info *map, __u8 d, unsigned long ofs)
  843. +{
  844. + __u16 page, offset;
  845. +
  846. + address_to_page(ofs, &page, &offset);
  847. + main_memory_page_program(MAIN_MEMORY_PROGRAM_BUFFER1, page, offset, d);
  848. +//printk("sl2312_write8:: address = %08x data = %c \n",ofs,d);
  849. +
  850. +}
  851. +
  852. +void sl2312_copy_from(struct map_info *map, void *buf, unsigned long ofs, ssize_t len)
  853. +{
  854. + __u32 size;
  855. + __u8 *buffer;
  856. + __u32 length;//i, j,
  857. +
  858. + //printk("sl2312_copy_from:: address = %08x datalen = %d \n",ofs,len);
  859. +
  860. + length = len;
  861. + buffer = (__u8 *)buf;
  862. + while(len)
  863. + {
  864. + size = SPAGE_SIZE - (ofs%SPAGE_SIZE);
  865. + if(size > len)
  866. + size = len;
  867. + atmel_flash_read_page(ofs, buffer, size);
  868. + buffer+=size;
  869. + ofs+=size;
  870. + len -= size;
  871. + }
  872. +
  873. +#if 0
  874. + buffer = (__u8 *)buf;
  875. + for(i=0; i<length; i+=16)
  876. + {
  877. + for(j=0; j<16; j++,buffer++)
  878. + {
  879. + if((i*16+j)<length)
  880. + printk("%x ",(int)*buffer);
  881. + }
  882. + printk("\n");
  883. + }
  884. +
  885. + printk("\n");
  886. +#endif
  887. +
  888. +}
  889. +
  890. +
  891. +void sl2312_copy_to(struct map_info *map, unsigned long ofs, void *buf, ssize_t len)
  892. +{
  893. + __u32 size;
  894. + __u8 *buffer;
  895. +
  896. + buffer = (__u8 *)buf;
  897. + //printk("sl2312_copy_to:offset %x len %x \n", ofs, len);
  898. +// printk("sl2312_copy_to:buf is %x \n", (int)buf);
  899. +
  900. + while(len)
  901. + {
  902. + size = SPAGE_SIZE - (ofs%SPAGE_SIZE);
  903. + if(size > len)
  904. + size = len;
  905. + atmel_flash_program_page(ofs, buffer, size);
  906. + buffer+=size;
  907. + ofs+=size;
  908. + len-=size;
  909. + }
  910. +
  911. +
  912. +}
  913. +
  914. +
  915. +static struct mtd_info *serial_mtd;
  916. +
  917. +static struct mtd_partition *parsed_parts;
  918. +
  919. +static struct map_info sl2312_serial_map = {
  920. +// name: "SL2312 serial flash",
  921. +// size: 4194304, //0x400000,
  922. +// //buswidth: 4,
  923. +// bankwidth: 4,
  924. +// phys: SL2312_FLASH_BASE,
  925. +//#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
  926. +// //read32: sl2312_read32,
  927. +// //read8: sl2312_read8,
  928. +// copy_from: sl2312_copy_from,
  929. +// //write8: sl2312_write8,
  930. +// //write32: sl2312_write32,
  931. +// read: sl2312_read32,
  932. +// write: sl2312_write32,
  933. +// copy_to: sl2312_copy_to
  934. +//#endif
  935. + .name = "SL2312 serial flash",
  936. + .size = 4194304, //0x400000,
  937. + //buswidth: 4,
  938. + .bankwidth = 4,
  939. + .phys = SL2312_FLASH_BASE,
  940. +#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
  941. + //read32: sl2312_read32,
  942. + //read8: sl2312_read8,
  943. + .copy_from = sl2312_copy_from,
  944. + //write8: sl2312_write8,
  945. + //write32: sl2312_write32,
  946. + .read = sl2312_read32,
  947. + .write = sl2312_write32,
  948. + .copy_to = sl2312_copy_to
  949. +#endif
  950. +};
  951. +
  952. +
  953. +
  954. +static struct mtd_partition sl2312_partitions[] = {
  955. +
  956. +
  957. + ///* boot code */
  958. + //{ name: "bootloader", offset: 0x00000000, size: 0x20000, },
  959. + ///* kernel image */
  960. + //{ name: "kerel image", offset: 0x000020000, size: 0x2E0000 },
  961. + ///* All else is writable (e.g. JFFS) */
  962. + //{ name: "user data", offset: 0x00300000, size: 0x00100000, },
  963. + /* boot code */
  964. + { .name = "bootloader", .offset = 0x00000000, .size = 0x20000, },
  965. + /* kernel image */
  966. + { .name = "kerel image", .offset = 0x000020000, .size = 0xE0000 },
  967. + /* All else is writable (e.g. JFFS) */
  968. + { .name = "user data", .offset = 0x00100000, .size = 0x00300000, },
  969. +
  970. +
  971. +};
  972. +
  973. +
  974. +
  975. +static int __init init_sl2312_maps(void)
  976. +{
  977. + int nr_parts = 0;
  978. + struct mtd_partition *parts;
  979. +
  980. + serial_mtd = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
  981. + if (!serial_mtd)
  982. + return NULL;
  983. +
  984. + memset(serial_mtd, 0, sizeof(struct mtd_info));
  985. + //sl2312flash_map.virt = (unsigned long)ioremap(SL2312_FLASH_BASE, FLASH_SIZE);
  986. + //sl2312_serial_map.map_priv_1 = (unsigned long)ioremap(SL2312_FLASH_BASE, SFLASH_SIZE);//(unsigned long)FLASH_VBASE;
  987. + sl2312_serial_map.virt = (unsigned long)ioremap(SL2312_FLASH_BASE, SFLASH_SIZE);//(unsigned long)ioremap(FLASH_START, SFLASH_SIZE);
  988. + if (!sl2312_serial_map.virt) {
  989. + printk(" failed to ioremap \n");
  990. + return -EIO;
  991. + }
  992. + serial_mtd = do_map_probe("map_serial", &sl2312_serial_map);
  993. + if (serial_mtd) {
  994. + //serial_mtd->module = THIS_MODULE;
  995. + serial_mtd->owner = THIS_MODULE;
  996. +
  997. + }
  998. +
  999. +#ifdef CONFIG_MTD_REDBOOT_PARTS
  1000. + nr_parts = parse_redboot_partitions(serial_mtd, &parsed_parts);
  1001. + if (nr_parts > 0)
  1002. + printk(KERN_NOTICE "Found RedBoot partition table.\n");
  1003. + else if (nr_parts < 0)
  1004. + printk(KERN_NOTICE "Error looking for RedBoot partitions.\n");
  1005. +#else
  1006. + parsed_parts = sl2312_partitions;
  1007. + parts = sl2312_partitions;
  1008. + nr_parts = sizeof(sl2312_partitions)/sizeof(*parts);
  1009. + nr_parts = sizeof(sl2312_partitions)/sizeof(*parsed_parts);
  1010. +#endif /* CONFIG_MTD_REDBOOT_PARTS */
  1011. +
  1012. + if (nr_parts > 0)
  1013. + add_mtd_partitions(serial_mtd, parsed_parts, nr_parts);
  1014. + else
  1015. + add_mtd_device(serial_mtd);
  1016. +
  1017. + return 0;
  1018. +}
  1019. +
  1020. +static void __exit cleanup_sl2312_maps(void)
  1021. +{
  1022. + if (parsed_parts)
  1023. + del_mtd_partitions(serial_mtd);
  1024. + else
  1025. + del_mtd_device(serial_mtd);
  1026. +
  1027. + map_destroy(serial_mtd);
  1028. +
  1029. +
  1030. +}
  1031. +
  1032. +module_init(init_sl2312_maps);
  1033. +module_exit(cleanup_sl2312_maps);
  1034. +
  1035. +
  1036. +
  1037. +MODULE_LICENSE("GPL");
  1038. +MODULE_AUTHOR("Plus Chen <[email protected]>");
  1039. +MODULE_DESCRIPTION("MTD map driver for Storlink Sword boards");
  1040. +
  1041. --- /dev/null
  1042. +++ b/drivers/mtd/maps/sl2312-flash-cfi.c
  1043. @@ -0,0 +1,370 @@
  1044. +/*======================================================================
  1045. +
  1046. + This program is free software; you can redistribute it and/or modify
  1047. + it under the terms of the GNU General Public License as published by
  1048. + the Free Software Foundation; either version 2 of the License, or
  1049. + (at your option) any later version.
  1050. +
  1051. + This program is distributed in the hope that it will be useful,
  1052. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  1053. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1054. + GNU General Public License for more details.
  1055. +
  1056. + You should have received a copy of the GNU General Public License
  1057. + along with this program; if not, write to the Free Software
  1058. + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  1059. +======================================================================*/
  1060. +
  1061. +#include <linux/module.h>
  1062. +#include <linux/types.h>
  1063. +#include <linux/kernel.h>
  1064. +#include <linux/slab.h>
  1065. +#include <linux/ioport.h>
  1066. +#include <linux/init.h>
  1067. +#include <linux/string.h>
  1068. +
  1069. +#include <linux/mtd/mtd.h>
  1070. +#include <linux/mtd/map.h>
  1071. +#include <linux/mtd/partitions.h>
  1072. +
  1073. +#include <asm/hardware.h>
  1074. +#include <asm/io.h>
  1075. +#include <asm/system.h>
  1076. +#include <asm/arch/sl2312.h>
  1077. +#include <linux/mtd/kvctl.h>
  1078. +#include "sl2312_flashmap.h"
  1079. +
  1080. +
  1081. +//extern int parse_afs_partitions(struct mtd_info *, struct mtd_partition **);
  1082. +
  1083. +/* the base address of FLASH control register */
  1084. +#define FLASH_CONTROL_BASE_ADDR (IO_ADDRESS(SL2312_FLASH_CTRL_BASE))
  1085. +#define SL2312_GLOBAL_BASE_ADDR (IO_ADDRESS(SL2312_GLOBAL_BASE))
  1086. +
  1087. +/* define read/write register utility */
  1088. +#define FLASH_READ_REG(offset) (__raw_readl(offset+FLASH_CONTROL_BASE_ADDR))
  1089. +#define FLASH_WRITE_REG(offset,val) (__raw_writel(val,offset+FLASH_CONTROL_BASE_ADDR))
  1090. +
  1091. +/* the offset of FLASH control register */
  1092. +enum EMAC_REGISTER {
  1093. + FLASH_ID = 0x0000,
  1094. + FLASH_STATUS = 0x0008,
  1095. + FLASH_TYPE = 0x000c,
  1096. + FLASH_ACCESS = 0x0020,
  1097. + FLASH_ADDRESS = 0x0024,
  1098. + FLASH_DATA = 0x0028,
  1099. + FLASH_TIMING = 0x002c,
  1100. +};
  1101. +
  1102. +//#define FLASH_BASE FLASH_CONTROL_BASE_ADDR
  1103. +//#define FLASH_SIZE 0x00800000 //INTEGRATOR_FLASH_SIZE
  1104. +
  1105. +//#define FLASH_PART_SIZE 8388608
  1106. +
  1107. +static unsigned int flash_indirect_access = 0;
  1108. +
  1109. +#ifdef CONFIG_SL2312_SHARE_PIN
  1110. +static unsigned int chip_en = 0x00000000;
  1111. +
  1112. +void sl2312flash_enable_parallel_flash(void)
  1113. +{
  1114. + unsigned int reg_val;
  1115. +
  1116. + reg_val = readl(SL2312_GLOBAL_BASE_ADDR + 0x30);
  1117. + reg_val = reg_val & 0xfffffffd;
  1118. + writel(reg_val,SL2312_GLOBAL_BASE_ADDR + 0x30);
  1119. + return;
  1120. +}
  1121. +
  1122. +void sl2312flash_disable_parallel_flash(void)
  1123. +{
  1124. + unsigned int reg_val;
  1125. +
  1126. + reg_val = readl(SL2312_GLOBAL_BASE_ADDR + 0x30);
  1127. + reg_val = reg_val | 0x00000002;
  1128. + writel(reg_val,SL2312_GLOBAL_BASE_ADDR + 0x30);
  1129. + return;
  1130. +}
  1131. +#endif
  1132. +
  1133. +
  1134. +static struct map_info sl2312flash_map =
  1135. +{
  1136. + name: "SL2312 CFI Flash",
  1137. + size: FLASH_SIZE,
  1138. + bankwidth: 2,
  1139. + //bankwidth: 1, //for 8 bits width
  1140. + phys: SL2312_FLASH_BASE,
  1141. +};
  1142. +
  1143. +static struct mtd_info *mtd;
  1144. +#if 0
  1145. +static struct mtd_partition sl2312_partitions[] = {
  1146. + /* boot code */
  1147. + {
  1148. + name: "bootloader",
  1149. + offset: 0x00000000,
  1150. + size: 0x20000,
  1151. +// mask_flags: MTD_WRITEABLE,
  1152. + },
  1153. + /* kernel image */
  1154. + {
  1155. + name: "kerel image",
  1156. + offset: 0x00020000,
  1157. + size: 0x2E0000
  1158. + },
  1159. + /* All else is writable (e.g. JFFS) */
  1160. + {
  1161. + name: "user data",
  1162. + offset: 0x00300000,
  1163. + size: 0x00100000,
  1164. + }
  1165. +};
  1166. +#endif
  1167. +
  1168. +
  1169. +
  1170. +static int __init sl2312flash_init(void)
  1171. +{
  1172. + struct mtd_partition *parts;
  1173. + int nr_parts = 0;
  1174. + int ret;
  1175. +#ifndef CONFIG_SL2312_SHARE_PIN
  1176. + unsigned int reg_val;
  1177. +#endif
  1178. +
  1179. + printk("SL2312 MTD Driver Init.......\n");
  1180. +
  1181. +#ifndef CONFIG_SL2312_SHARE_PIN
  1182. + /* enable flash */
  1183. + reg_val = readl(SL2312_GLOBAL_BASE_ADDR + 0x30);
  1184. + reg_val = reg_val & 0xfffffffd;
  1185. + writel(reg_val,SL2312_GLOBAL_BASE_ADDR + 0x30);
  1186. +#else
  1187. + sl2312flash_enable_parallel_flash(); /* enable Parallel FLASH */
  1188. +#endif
  1189. + FLASH_WRITE_REG(FLASH_ACCESS,0x00004000); /* parallel flash direct access mode */
  1190. + ret = FLASH_READ_REG(FLASH_ACCESS);
  1191. + if (ret == 0x00004000)
  1192. + {
  1193. + flash_indirect_access = 0; /* parallel flash direct access */
  1194. + }
  1195. + else
  1196. + {
  1197. + flash_indirect_access = 1; /* parallel flash indirect access */
  1198. + }
  1199. +
  1200. + /*
  1201. + * Also, the CFI layer automatically works out what size
  1202. + * of chips we have, and does the necessary identification
  1203. + * for us automatically.
  1204. + */
  1205. +#ifdef CONFIG_GEMINI_IPI
  1206. + sl2312flash_map.virt = FLASH_VBASE;//(unsigned int *)ioremap(SL2312_FLASH_BASE, FLASH_SIZE);
  1207. +#else
  1208. + sl2312flash_map.virt = (unsigned int *)ioremap(SL2312_FLASH_BASE, FLASH_SIZE);
  1209. +#endif
  1210. + //printk("sl2312flash_map.virt = %08x\n",(unsigned int)sl2312flash_map.virt);
  1211. +
  1212. +// simple_map_init(&sl2312flash_map);
  1213. +
  1214. + mtd = do_map_probe("cfi_probe", &sl2312flash_map);
  1215. + if (!mtd)
  1216. + {
  1217. +#ifdef CONFIG_SL2312_SHARE_PIN
  1218. + sl2312flash_disable_parallel_flash(); /* disable Parallel FLASH */
  1219. +#endif
  1220. + return -ENXIO;
  1221. + }
  1222. + mtd->owner = THIS_MODULE;
  1223. +// mtd->erase = flash_erase;
  1224. +// mtd->read = flash_read;
  1225. +// mtd->write = flash_write;
  1226. +
  1227. + parts = sl2312_partitions;
  1228. + nr_parts = sizeof(sl2312_partitions)/sizeof(*parts);
  1229. + ret = add_mtd_partitions(mtd, parts, nr_parts);
  1230. + /*If we got an error, free all resources.*/
  1231. + if (ret < 0) {
  1232. + del_mtd_partitions(mtd);
  1233. + map_destroy(mtd);
  1234. + }
  1235. +#ifdef CONFIG_SL2312_SHARE_PIN
  1236. + sl2312flash_disable_parallel_flash(); /* disable Parallel FLASH */
  1237. +#endif
  1238. + printk("SL2312 MTD Driver Init Success ......\n");
  1239. + return ret;
  1240. +}
  1241. +
  1242. +static void __exit sl2312flash_exit(void)
  1243. +{
  1244. + if (mtd) {
  1245. + del_mtd_partitions(mtd);
  1246. + map_destroy(mtd);
  1247. + }
  1248. +
  1249. + if (sl2312flash_map.virt) {
  1250. + iounmap((void *)sl2312flash_map.virt);
  1251. + sl2312flash_map.virt = 0;
  1252. + }
  1253. +}
  1254. +
  1255. +char chrtohex(char c)
  1256. +{
  1257. + char val;
  1258. + if ((c >= '0') && (c <= '9'))
  1259. + {
  1260. + val = c - '0';
  1261. + return val;
  1262. + }
  1263. + else if ((c >= 'a') && (c <= 'f'))
  1264. + {
  1265. + val = 10 + (c - 'a');
  1266. + return val;
  1267. + }
  1268. + else if ((c >= 'A') && (c <= 'F'))
  1269. + {
  1270. + val = 10 + (c - 'A');
  1271. + return val;
  1272. + }
  1273. + printk("<1>Error number\n");
  1274. + return 0;
  1275. +}
  1276. +
  1277. +
  1278. +int get_vlaninfo(vlaninfo* vlan)
  1279. +{
  1280. + vctl_mheader head;
  1281. + vctl_entry entry;
  1282. + struct mtd_info *mymtd=NULL;
  1283. + int i, j, loc = 0;
  1284. + char *payload=0, *tmp1, *tmp2, tmp3[9];
  1285. + size_t retlen;
  1286. +
  1287. + #ifdef CONFIG_SL2312_SHARE_PIN
  1288. + sl2312flash_enable_parallel_flash();
  1289. + #endif
  1290. + for(i=0;i<MAX_MTD_DEVICES;i++)
  1291. + {
  1292. + mymtd=get_mtd_device(NULL,i);
  1293. + // printk("mymtd->name: %s\n", mymtd->name);
  1294. + if(mymtd && !strcmp(mymtd->name,"VCTL"))
  1295. + {
  1296. + // printk("%s\n", mymtd->name);
  1297. + break;
  1298. + }
  1299. + }
  1300. + if( i >= MAX_MTD_DEVICES)
  1301. + {
  1302. + printk("Can't find version control\n");
  1303. + #ifdef CONFIG_SL2312_SHARE_PIN
  1304. + sl2312flash_disable_parallel_flash();
  1305. + #endif
  1306. + return 0;
  1307. + }
  1308. +
  1309. + if (!mymtd | !mymtd->read)
  1310. + {
  1311. + printk("<1>Can't read Version Configuration\n");
  1312. + #ifdef CONFIG_SL2312_SHARE_PIN
  1313. + sl2312flash_disable_parallel_flash();
  1314. + #endif
  1315. + return 0;
  1316. + }
  1317. +
  1318. + mymtd->read(mymtd, 0, VCTL_HEAD_SIZE, &retlen, (u_char*)&head);
  1319. + // printk("entry header: %c%c%c%c\n", head.header[0], head.header[1], head.header[2], head.header[3]);
  1320. + // printk("entry number: %x\n", head.entry_num);
  1321. + if ( strncmp(head.header, "FLFM", 4) )
  1322. + {
  1323. + printk("VCTL is a erase block\n");
  1324. + #ifdef CONFIG_SL2312_SHARE_PIN
  1325. + sl2312flash_disable_parallel_flash();
  1326. + #endif
  1327. + return 0;
  1328. + }
  1329. + loc += retlen;
  1330. + for (i = 0; i < head.entry_num; i++)
  1331. + {
  1332. + mymtd->read(mymtd, loc, VCTL_ENTRY_LEN, &retlen, (u_char*)&entry);
  1333. + // printk("type: %x\n", entry.type);
  1334. + // printk("size: %x\n", entry.size);
  1335. + strncpy(tmp3, entry.header, 4);
  1336. + if (entry.type == VCT_VLAN)
  1337. + {
  1338. + for (j = 0; j < 6 ; j++)
  1339. + {
  1340. + vlan[0].mac[j] = 0;
  1341. + vlan[1].mac[j] = 0;
  1342. + }
  1343. + vlan[0].vlanid = 1;
  1344. + vlan[1].vlanid = 2;
  1345. + vlan[0].vlanmap = 0x7F;
  1346. + vlan[1].vlanmap = 0x80;
  1347. +
  1348. + payload = (char *)kmalloc(entry.size - VCTL_ENTRY_LEN, GFP_KERNEL);
  1349. + loc += VCTL_ENTRY_LEN;
  1350. + mymtd->read(mymtd, loc, entry.size - VCTL_ENTRY_LEN, &retlen, payload);
  1351. + // printk("%s\n", payload);
  1352. + tmp1 = strstr(payload, "MAC1:");
  1353. + tmp2 = strstr(payload, "MAC2:");
  1354. + if(!tmp1||!tmp2){
  1355. + kfree(payload);
  1356. + #ifdef CONFIG_SL2312_SHARE_PIN
  1357. + sl2312flash_disable_parallel_flash();
  1358. + #endif
  1359. + printk("Error VCTL format!!\n");
  1360. + return 0;
  1361. + }
  1362. + tmp1 += 7;
  1363. + tmp2 += 7;
  1364. +
  1365. +
  1366. + for (j = 0; j < 6; j++)
  1367. + {
  1368. + vlan[0].mac[j] = chrtohex(tmp1[2*j])*16 + chrtohex(tmp1[(2*j)+1]);
  1369. + vlan[1].mac[j] = chrtohex(tmp2[2*j])*16 + chrtohex(tmp2[(2*j)+1]);
  1370. + }
  1371. + tmp1 = strstr(payload, "ID1:");
  1372. + tmp2 = strstr(payload, "ID2:");
  1373. + tmp1 += 4;
  1374. + tmp2 += 4;
  1375. + vlan[0].vlanid = tmp1[0] - '0';
  1376. + vlan[1].vlanid = tmp2[0] - '0';
  1377. + tmp1 = strstr(payload, "MAP1:");
  1378. + tmp2 = strstr(payload, "MAP2:");
  1379. + tmp1 += 7;
  1380. + tmp2 += 7;
  1381. + vlan[0].vlanmap = chrtohex(tmp1[0]) * 16 + chrtohex(tmp1[1]);
  1382. + vlan[1].vlanmap = chrtohex(tmp2[0]) * 16 + chrtohex(tmp2[1]);
  1383. + // printk("Vlan1 id:%x map:%02x mac:%x%x%x%x%x%x\n", vlan[0].vlanid, vlan[0].vlanmap, vlan[0].mac[0], vlan[0].mac[1], vlan[0].mac[2], vlan[0].mac[3], vlan[0].mac[4], vlan[0].mac[5]);
  1384. + // printk("Vlan2 id:%x map:%02x mac:%x%x%x%x%x%x\n", vlan[1].vlanid, vlan[1].vlanmap, vlan[1].mac[0], vlan[1].mac[1], vlan[1].mac[2], vlan[1].mac[3], vlan[1].mac[4], vlan[1].mac[5]);
  1385. + break;
  1386. + }
  1387. + loc += entry.size;
  1388. + }
  1389. + if ( entry.type == VCT_VLAN )
  1390. + {
  1391. + #ifdef CONFIG_SL2312_SHARE_PIN
  1392. + sl2312flash_disable_parallel_flash();
  1393. + #endif
  1394. + kfree(payload);
  1395. + return 1;
  1396. + }
  1397. + if (i >= head.entry_num)
  1398. + printk("Can't find vlan information\n");
  1399. + #ifdef CONFIG_SL2312_SHARE_PIN
  1400. + sl2312flash_disable_parallel_flash();
  1401. + #endif
  1402. + return 0;
  1403. +}
  1404. +
  1405. +EXPORT_SYMBOL(get_vlaninfo);
  1406. +
  1407. +
  1408. +module_init(sl2312flash_init);
  1409. +module_exit(sl2312flash_exit);
  1410. +
  1411. +MODULE_AUTHOR("Storlink Ltd");
  1412. +MODULE_DESCRIPTION("CFI map driver");
  1413. +MODULE_LICENSE("GPL");
  1414. --- /dev/null
  1415. +++ b/drivers/mtd/maps/sl2312-flash-m25p80.c
  1416. @@ -0,0 +1,498 @@
  1417. +/*
  1418. + * $Id: sl2312-flash-m25p80.c,v 1.2 2006/06/02 08:46:02 middle Exp $
  1419. + *
  1420. + * Flash and EPROM on Hitachi Solution Engine and similar boards.
  1421. + *
  1422. + * (C) 2001 Red Hat, Inc.
  1423. + *
  1424. + * GPL'd
  1425. + */
  1426. +
  1427. +#include <linux/module.h>
  1428. +#include <linux/types.h>
  1429. +#include <linux/kernel.h>
  1430. +
  1431. +#include <asm/io.h>
  1432. +#include <linux/mtd/mtd.h>
  1433. +#include <linux/mtd/map.h>
  1434. +#include <linux/mtd/partitions.h>
  1435. +#include <asm/hardware.h>
  1436. +
  1437. +#include <asm/arch/sl2312.h>
  1438. +#include <asm/arch/flash.h>
  1439. +#include <linux/init.h> //add
  1440. +#define g_chipen SERIAL_FLASH_CHIP0_EN //ST
  1441. +
  1442. +//static int m25p80_page_program(__u32 address, __u8 data, __u32 schip_en);
  1443. +static void m25p80_write_cmd(__u8 cmd, __u32 schip_en);
  1444. +extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts);
  1445. +
  1446. +
  1447. +static __u32 read_flash_ctrl_reg(__u32 ofs)
  1448. +{
  1449. + __u32 *base;
  1450. +
  1451. + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
  1452. + return __raw_readl(base);
  1453. +}
  1454. +
  1455. +static void write_flash_ctrl_reg(__u32 ofs,__u32 data)
  1456. +{
  1457. + __u32 *base;
  1458. +
  1459. + base = (__u32 *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE + ofs));
  1460. + __raw_writel(data, base);
  1461. +}
  1462. +
  1463. +static void m25p80_read(__u32 address, __u8 *data, __u32 schip_en)
  1464. +{
  1465. + __u32 opcode,status;
  1466. + __u32 value;
  1467. +
  1468. + //opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | M25P80_READ;
  1469. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS_DATA | M25P80_READ;
  1470. + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
  1471. +
  1472. + opcode|=g_chipen;
  1473. +
  1474. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1475. + status=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1476. + while(status&0x80000000)
  1477. + {
  1478. + status=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1479. + flash_delay();
  1480. + schedule();
  1481. + }
  1482. +
  1483. + value=read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1484. + *data = value & 0xff;
  1485. +}
  1486. +
  1487. +static int m25p80_page_program(__u32 address, __u8 *data, __u32 schip_en)
  1488. +{
  1489. + __u32 opcode;
  1490. + __u32 status;
  1491. + __u32 tmp;
  1492. + int res = FLASH_ERR_OK;
  1493. + //volatile FLASH_DATA_T* data_ptr = (volatile FLASH_DATA_T*) data;
  1494. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | M25P80_READ_STATUS;
  1495. +
  1496. + opcode|=g_chipen;
  1497. +
  1498. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1499. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1500. + while(tmp&0x80000000)
  1501. + {
  1502. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1503. + flash_delay();
  1504. + schedule();
  1505. + }
  1506. + //middle delay_ms(130);
  1507. + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1508. + if((status&0x02)==0x02)
  1509. + {
  1510. + //middle delay_ms(100);
  1511. + m25p80_write_cmd(M25P80_WRITE_DISABLE, schip_en);
  1512. + }
  1513. +
  1514. +
  1515. + m25p80_write_cmd(M25P80_WRITE_ENABLE, schip_en);
  1516. + ////middle delay_ms(10);
  1517. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS_DATA | M25P80_PAGE_PROGRAM;
  1518. + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
  1519. + write_flash_ctrl_reg(FLASH_WRITE_DATA_OFFSET, *data);
  1520. +
  1521. + //status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1522. + //while(status!=data)
  1523. + //{
  1524. + // status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1525. + // //middle delay_ms(10);
  1526. + //}
  1527. +
  1528. + opcode|=g_chipen;
  1529. +
  1530. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1531. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1532. + while(tmp&0x80000000)
  1533. + {
  1534. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1535. + flash_delay();
  1536. + schedule();
  1537. + }
  1538. + //opcode=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1539. +
  1540. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | M25P80_READ_STATUS;
  1541. +
  1542. + opcode|=g_chipen;
  1543. +
  1544. +
  1545. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1546. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1547. + while(tmp&0x80000000)
  1548. + {
  1549. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1550. + flash_delay();
  1551. + schedule();
  1552. + }
  1553. + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1554. + //while(status&0xfd)
  1555. + while(status&0x01)
  1556. + {
  1557. + //if((status&0x9c)!=0)
  1558. + // printf(" m25p80_page_program Protect Status = %x\n",status);
  1559. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1560. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1561. + while(tmp&0x80000000)
  1562. + {
  1563. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1564. + flash_delay();
  1565. + schedule();
  1566. + }
  1567. + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1568. + flash_delay();
  1569. + schedule();
  1570. + //middle delay_ms(50);
  1571. + }
  1572. + //printf("status = %x, data = %x\n",status,data);
  1573. + if((status&0x02)==0x02)
  1574. + {
  1575. + //middle delay_ms(100);
  1576. + m25p80_write_cmd(M25P80_WRITE_DISABLE, schip_en);
  1577. + }
  1578. + //};//while (len > 0)
  1579. + return res;
  1580. +}
  1581. +
  1582. +void m25p80_copy_from(struct map_info *map, void *buf, unsigned long ofs, ssize_t len)
  1583. +{
  1584. +// __u32 size;
  1585. + __u8 *buffer;
  1586. + __u32 length;//i, j,
  1587. +
  1588. + length = len;
  1589. + buffer = (__u8 *)buf;
  1590. + while(len)
  1591. + {
  1592. + m25p80_read(ofs, buffer, g_chipen);
  1593. + buffer++;
  1594. + ofs++;
  1595. + len --;
  1596. + } ;
  1597. +
  1598. +}
  1599. +
  1600. +__u32 m25p80_read32(struct map_info *map, unsigned long ofs)
  1601. +{
  1602. +
  1603. + return read_flash_ctrl_reg(ofs);
  1604. +
  1605. +
  1606. +}
  1607. +
  1608. +void m25p80_write32(struct map_info *map, __u32 d, unsigned long ofs)
  1609. +{
  1610. +
  1611. + write_flash_ctrl_reg(ofs, d);
  1612. +
  1613. +}
  1614. +
  1615. +void m25p80_copy_to(struct map_info *map, unsigned long ofs, void *buf, ssize_t len)
  1616. +{
  1617. + __u32 size, i, ret;
  1618. +
  1619. + while(len > 0)
  1620. + {
  1621. + if(len >= M25P80_PAGE_SIZE)
  1622. + size = M25P80_PAGE_SIZE;
  1623. + else
  1624. + size = len;
  1625. +
  1626. + for(i=0;i<size;i++)
  1627. + {
  1628. + ret = m25p80_page_program( (ofs+i), (buf+i), g_chipen);
  1629. + }
  1630. + buf+=M25P80_PAGE_SIZE;
  1631. + ofs+=M25P80_PAGE_SIZE;
  1632. + len-=M25P80_PAGE_SIZE;
  1633. +
  1634. + };
  1635. +
  1636. +
  1637. +}
  1638. +
  1639. +static struct mtd_info *serial_mtd;
  1640. +
  1641. +static struct mtd_partition *parsed_parts;
  1642. +
  1643. +static struct map_info m25p80_map = {
  1644. +
  1645. + .name = "SL2312 serial flash m25p80",
  1646. + .size = 1048576, //0x100000,
  1647. + //buswidth: 4,
  1648. + .bankwidth = 4,
  1649. + .phys = SL2312_FLASH_BASE,
  1650. +#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
  1651. + .copy_from = m25p80_copy_from,
  1652. + .read = m25p80_read32,
  1653. + .write = m25p80_write32,
  1654. + .copy_to = m25p80_copy_to
  1655. +#endif
  1656. +};
  1657. +
  1658. +
  1659. +
  1660. +static struct mtd_partition m25p80_partitions[] = {
  1661. +
  1662. + /* boot code */
  1663. + { .name = "bootloader", .offset = 0x00000000, .size = 0x20000, },
  1664. + /* kernel image */
  1665. + { .name = "kerel image", .offset = 0x000020000, .size = 0xC0000 },
  1666. + /* All else is writable (e.g. JFFS) */
  1667. + { .name = "user data", .offset = 0x000E0000, .size = 0x00010000, },
  1668. +
  1669. +
  1670. +};
  1671. +
  1672. +void flash_delay()
  1673. +{
  1674. + int i,j;
  1675. + for(i=0;i<0x100;i++)
  1676. + j=i*3+5;
  1677. +}
  1678. +
  1679. +int m25p80_sector_erase(__u32 address, __u32 schip_en)
  1680. +{
  1681. + __u32 opcode;
  1682. + __u32 status;
  1683. + __u32 tmp;
  1684. + int res = FLASH_ERR_OK;
  1685. + //printf("\n-->m25p80_sector_erase");
  1686. + if(address >= FLASH_START)
  1687. + address-=FLASH_START;
  1688. +
  1689. + m25p80_write_cmd(M25P80_WRITE_ENABLE, schip_en);
  1690. + //printf("\n m25p80_sector_erase : after we-en");
  1691. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_SHIFT_ADDRESS | M25P80_SECTOR_ERASE;
  1692. + write_flash_ctrl_reg(FLASH_ADDRESS_OFFSET, address);
  1693. + #ifdef MIDWAY_DIAG
  1694. + opcode|=schip_en;
  1695. + #endif
  1696. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1697. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1698. + while(tmp&0x80000000)
  1699. + {
  1700. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1701. + flash_delay();
  1702. + schedule();
  1703. + }
  1704. +
  1705. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | M25P80_READ_STATUS;
  1706. + #ifdef MIDWAY_DIAG
  1707. + opcode|=schip_en;
  1708. + #endif
  1709. +
  1710. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1711. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1712. + while(tmp&0x80000000)
  1713. + {
  1714. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1715. + flash_delay();
  1716. + schedule();
  1717. + }
  1718. + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1719. + //while(status&0xfd)
  1720. + while(status&0x01)
  1721. + {
  1722. + //if((status&0x9c)!=0)
  1723. + // printf(" m25p80_sector_erase Protect Status = %x\n",status);
  1724. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1725. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1726. + while(tmp&0x80000000)
  1727. + {
  1728. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1729. + flash_delay();
  1730. + schedule();
  1731. + }
  1732. + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1733. + flash_delay();
  1734. + schedule();
  1735. + //middle delay_ms(50);
  1736. + }
  1737. + if((status&0x02)==0x02)
  1738. + {
  1739. + //middle delay_ms(100);
  1740. + m25p80_write_cmd(M25P80_WRITE_DISABLE, schip_en);
  1741. + }
  1742. + //printf("\n<--m25p80_sector_erase");
  1743. + return res;
  1744. +}
  1745. +
  1746. +static void m25p80_write_cmd(__u8 cmd, __u32 schip_en)
  1747. +{
  1748. + __u32 opcode,tmp;
  1749. + __u32 status;
  1750. +
  1751. +
  1752. +
  1753. +
  1754. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE | cmd;
  1755. +
  1756. + opcode|=g_chipen;
  1757. +
  1758. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1759. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1760. + while(tmp&0x80000000)
  1761. + {
  1762. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1763. + flash_delay();
  1764. + schedule();
  1765. + }
  1766. + //////
  1767. + opcode = 0x80000000 | FLASH_ACCESS_ACTION_OPCODE_DATA | M25P80_READ_STATUS;
  1768. +
  1769. + opcode|=g_chipen;
  1770. +
  1771. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1772. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1773. + while(tmp&0x80000000)
  1774. + {
  1775. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1776. + flash_delay();
  1777. + schedule();
  1778. + }
  1779. + //middle delay_ms(130);
  1780. + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1781. + //printf("\ncmd =%x status = %x",cmd,status);
  1782. + if(cmd==M25P80_WRITE_ENABLE)
  1783. + {
  1784. + //printf("\n**-->enable** status = %x",status);
  1785. + //middle delay_ms(100);
  1786. + while((status&0x03) != 2)
  1787. + {
  1788. + //if((status&0x9c)!=0)
  1789. + // printf(" M25P80_WRITE_ENABLE Protect Status = %x\n",status);
  1790. +
  1791. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1792. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1793. + while(tmp&0x80000000)
  1794. + {
  1795. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1796. + //flash_delay();
  1797. + }
  1798. + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1799. + //printf("\n**enable** status = %x",status);
  1800. + flash_delay();
  1801. + schedule();
  1802. + //middle delay_ms(100);
  1803. + }
  1804. + }
  1805. + else if(cmd==M25P80_WRITE_DISABLE)
  1806. + {
  1807. + //while((status&0x03) == 2)
  1808. + // printf("\n**disable** status = %x",status);
  1809. + //middle delay_ms(100);
  1810. + while((status&0x03) != 0)
  1811. + {
  1812. + //m25p80_write_status((status&0xfd),schip_en);
  1813. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1814. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1815. + while(tmp&0x80000000)
  1816. + {
  1817. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1818. + flash_delay();
  1819. + schedule();
  1820. + }
  1821. + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1822. + //printf("\n**disable** status = %x",status);
  1823. + flash_delay();
  1824. + schedule();
  1825. + //middle delay_ms(50);
  1826. + }
  1827. + }
  1828. + else
  1829. + {
  1830. + //while((status&0x01) !=0)
  1831. + while((status&0x01) !=0)
  1832. + {
  1833. + write_flash_ctrl_reg(FLASH_ACCESS_OFFSET, opcode);
  1834. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1835. + while(tmp&0x80000000)
  1836. + {
  1837. + tmp=read_flash_ctrl_reg(FLASH_ACCESS_OFFSET);
  1838. + flash_delay();
  1839. + schedule();
  1840. + }
  1841. + status = read_flash_ctrl_reg(FLASH_READ_DATA_OFFSET);
  1842. + flash_delay();
  1843. + schedule();
  1844. + //middle delay_ms(50);
  1845. + }
  1846. + }
  1847. + //////
  1848. +
  1849. + //printf("\n<-- status = %x",status);
  1850. +}
  1851. +
  1852. +static int __init init_sl2312_m25p80(void)
  1853. +{
  1854. + int nr_parts = 0;
  1855. + struct mtd_partition *parts;
  1856. +
  1857. + serial_mtd = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
  1858. + if (!serial_mtd)
  1859. + return NULL;
  1860. +
  1861. + memset(serial_mtd, 0, sizeof(struct mtd_info));
  1862. + m25p80_map.virt = (unsigned long)ioremap(SL2312_FLASH_BASE, SFLASH_SIZE);//(unsigned long)ioremap(FLASH_START, SFLASH_SIZE);
  1863. + if (!m25p80_map.virt) {
  1864. + printk(" failed to ioremap \n");
  1865. + return -EIO;
  1866. + }
  1867. + serial_mtd = do_map_probe("map_serial", &m25p80_map);
  1868. + if (serial_mtd) {
  1869. + serial_mtd->owner = THIS_MODULE;
  1870. +
  1871. + }
  1872. +
  1873. +#ifdef CONFIG_MTD_REDBOOT_PARTS
  1874. + nr_parts = parse_redboot_partitions(serial_mtd, &parsed_parts);
  1875. + if (nr_parts > 0)
  1876. + printk(KERN_NOTICE "Found RedBoot partition table.\n");
  1877. + else if (nr_parts < 0)
  1878. + printk(KERN_NOTICE "Error looking for RedBoot partitions.\n");
  1879. +#else
  1880. + parsed_parts = m25p80_partitions;
  1881. + parts = m25p80_partitions;
  1882. + nr_parts = sizeof(m25p80_partitions)/sizeof(*parts);
  1883. + nr_parts = sizeof(m25p80_partitions)/sizeof(*parsed_parts);
  1884. +#endif /* CONFIG_MTD_REDBOOT_PARTS */
  1885. +
  1886. + if (nr_parts > 0)
  1887. + add_mtd_partitions(serial_mtd, parsed_parts, nr_parts);
  1888. + else
  1889. + add_mtd_device(serial_mtd);
  1890. +
  1891. + return 0;
  1892. +}
  1893. +
  1894. +static void __exit cleanup_sl2312_m25p80(void)
  1895. +{
  1896. + if (parsed_parts)
  1897. + del_mtd_partitions(serial_mtd);
  1898. + else
  1899. + del_mtd_device(serial_mtd);
  1900. +
  1901. + map_destroy(serial_mtd);
  1902. +
  1903. +
  1904. +}
  1905. +
  1906. +module_init(init_sl2312_m25p80);
  1907. +module_exit(cleanup_sl2312_m25p80);
  1908. +
  1909. +
  1910. +
  1911. +MODULE_LICENSE("GPL");
  1912. +MODULE_AUTHOR("Plus Chen <[email protected]>");
  1913. +MODULE_DESCRIPTION("MTD map driver for Storlink Sword boards");
  1914. +
  1915. --- /dev/null
  1916. +++ b/drivers/mtd/maps/sl2312_flashmap.h
  1917. @@ -0,0 +1,21 @@
  1918. +/*
  1919. + * Please note that the name are used in mkflash script. Therefore
  1920. + * don't change them. If you want to add different partitions, you
  1921. + * will need to modify mkflash script as well so that the end image
  1922. + * is what you include here!
  1923. + *
  1924. + * Also, the 7th item is always the size, so please don't add extra
  1925. + * spaces in the name or other items.
  1926. + *
  1927. + * - Alan
  1928. + */
  1929. +
  1930. +static struct mtd_partition sl2312_partitions[] = {
  1931. + { name: "RedBoot", offset: 0x00000000, size: 0x00020000, },
  1932. + { name: "kernel", offset: 0x00020000, size: 0x00100000, },
  1933. + { name: "rootfs", offset: 0x00120000, size: 0x00500000, },
  1934. + { name: "rootfs_data", offset: 0x00620000, size: 0x001A0000, },
  1935. + { name: "VCTL", offset: 0x007C0000, size: 0x00010000, },
  1936. + { name: "cfg", offset: 0x007D0000, size: 0x00020000, },
  1937. + { name: "FIS directory", offset: 0x007F0000, size: 0x00010000, }
  1938. +};
  1939. --- /dev/null
  1940. +++ b/drivers/mtd/maps/sl2312_flashmap.h.16MB
  1941. @@ -0,0 +1,21 @@
  1942. +/*
  1943. + * Please note that the name are used in mkflash script. Therefore
  1944. + * don't change them. If you want to add different partitions, you
  1945. + * will need to modify mkflash script as well so that the end image
  1946. + * is what you include here!
  1947. + *
  1948. + * Also, the 7th item is always the size, so please don't add extra
  1949. + * spaces in the name or other items.
  1950. + *
  1951. + * - Alan
  1952. + */
  1953. +
  1954. +static struct mtd_partition sl2312_partitions[] = {
  1955. + { name: "RedBoot", offset: 0x00000000, size: 0x00020000, },
  1956. + { name: "Kernel", offset: 0x00020000, size: 0x00300000, },
  1957. + { name: "Ramdisk", offset: 0x00320000, size: 0x00600000, },
  1958. + { name: "Application", offset: 0x00920000, size: 0x00600000, },
  1959. + { name: "VCTL", offset: 0x00F20000, size: 0x00020000, },
  1960. + { name: "CurConf", offset: 0x00F40000, size: 0x000A0000, },
  1961. + { name: "FIS directory", offset: 0x00FE0000, size: 0x00020000, }
  1962. +};
  1963. --- /dev/null
  1964. +++ b/drivers/mtd/maps/sl2312_flashmap.h.8MB
  1965. @@ -0,0 +1,21 @@
  1966. +/*
  1967. + * Please note that the name are used in mkflash script. Therefore
  1968. + * don't change them. If you want to add different partitions, you
  1969. + * will need to modify mkflash script as well so that the end image
  1970. + * is what you include here!
  1971. + *
  1972. + * Also, the 7th item is always the size, so please don't add extra
  1973. + * spaces in the name or other items.
  1974. + *
  1975. + * - Alan
  1976. + */
  1977. +
  1978. +static struct mtd_partition sl2312_partitions[] = {
  1979. + { name: "RedBoot", offset: 0x00000000, size: 0x00020000, },
  1980. + { name: "Kernel", offset: 0x00020000, size: 0x00200000, },
  1981. + { name: "Ramdisk", offset: 0x00220000, size: 0x00280000, },
  1982. + { name: "Application", offset: 0x004A0000, size: 0x00300000, },
  1983. + { name: "VCTL", offset: 0x007A0000, size: 0x00020000, },
  1984. + { name: "CurConf", offset: 0x007C0000, size: 0x00020000, },
  1985. + { name: "FIS directory", offset: 0x007E0000, size: 0x00020000, }
  1986. +};
  1987. --- a/drivers/mtd/mtdchar.c
  1988. +++ b/drivers/mtd/mtdchar.c
  1989. @@ -59,6 +59,77 @@
  1990. enum mtd_file_modes mode;
  1991. };
  1992. +/***********************************************************************
  1993. +/* Storlink SoC -- flash
  1994. +/***********************************************************************/
  1995. +#ifdef CONFIG_SL2312_SHARE_PIN
  1996. +unsigned int share_pin_flag=0; // bit0:FLASH, bit1:UART, bit2:EMAC, bit3-4:IDE
  1997. +unsigned int check_sleep_flag=0; // bit0:FLASH, bit1:IDE
  1998. +static spinlock_t sl2312_flash_lock = SPIN_LOCK_UNLOCKED;
  1999. +EXPORT_SYMBOL(share_pin_flag);
  2000. +int dbg=0;
  2001. +DECLARE_WAIT_QUEUE_HEAD(wq);
  2002. +extern struct wait_queue_head_t *flash_wait;
  2003. +unsigned int flash_req=0;
  2004. +void mtd_lock()
  2005. +{
  2006. + struct task_struct *tsk = current;
  2007. + unsigned int value ;
  2008. + unsigned long flags;
  2009. + flash_req = 1;
  2010. + DECLARE_WAITQUEUE(wait, tsk);
  2011. + add_wait_queue(&wq, &wait);
  2012. + for(;;)
  2013. + {
  2014. + set_task_state(tsk, TASK_INTERRUPTIBLE);
  2015. + spin_lock_irqsave(&sl2312_flash_lock,flags);
  2016. + if((share_pin_flag&0x1E)){//||(check_sleep_flag&0x00000002)) {
  2017. + spin_unlock_irqrestore(&sl2312_flash_lock, flags);
  2018. + check_sleep_flag |= 0x00000001;
  2019. + if(dbg)
  2020. + printk("mtd yield %x %x\n",share_pin_flag,check_sleep_flag);
  2021. + wake_up_interruptible(&flash_wait);
  2022. + schedule();
  2023. + }
  2024. + else {
  2025. + check_sleep_flag &= ~0x01;
  2026. + share_pin_flag |= 0x00000001 ; // set share pin flag
  2027. + spin_unlock_irqrestore(&sl2312_flash_lock, flags);
  2028. + value = readl(IO_ADDRESS((SL2312_GLOBAL_BASE+GLOBAL_MISC_REG)));
  2029. + value = value & (~PFLASH_SHARE_BIT) ;
  2030. + writel(value,IO_ADDRESS((SL2312_GLOBAL_BASE+GLOBAL_MISC_REG)));
  2031. + if(dbg)
  2032. + printk("mtd Go %x %x\n",share_pin_flag,check_sleep_flag);
  2033. + tsk->state = TASK_RUNNING;
  2034. + remove_wait_queue(&wq, &wait);
  2035. + return ;
  2036. + }
  2037. + }
  2038. +}
  2039. +
  2040. +void mtd_unlock()
  2041. +{
  2042. + unsigned int value ;
  2043. + unsigned long flags;
  2044. +
  2045. + spin_lock_irqsave(&sl2312_flash_lock,flags); // Disable IRQ
  2046. + value = readl(IO_ADDRESS((SL2312_GLOBAL_BASE+GLOBAL_MISC_REG)));
  2047. + value = value | PFLASH_SHARE_BIT ; // Disable Flash PADs
  2048. + writel(value,IO_ADDRESS((SL2312_GLOBAL_BASE+GLOBAL_MISC_REG)));
  2049. + share_pin_flag &= ~(0x00000001); // clear share pin flag
  2050. + check_sleep_flag &= ~0x00000001;
  2051. + spin_unlock_irqrestore(&sl2312_flash_lock, flags); // Restore IRQ
  2052. + if (check_sleep_flag & 0x00000002)
  2053. + {
  2054. + check_sleep_flag &= ~(0x00000002);
  2055. + wake_up_interruptible(&flash_wait);
  2056. + }
  2057. + DEBUG(MTD_DEBUG_LEVEL0, "Flash Unlock...\n");
  2058. + flash_req = 0;
  2059. +}
  2060. +#endif
  2061. +/***********************************************************************/
  2062. +
  2063. static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
  2064. {
  2065. struct mtd_file_info *mfi = file->private_data;
  2066. @@ -162,13 +233,21 @@
  2067. int len;
  2068. char *kbuf;
  2069. +#ifdef CONFIG_SL2312_SHARE_PIN
  2070. + mtd_lock(); // sl2312 share pin lock
  2071. +#endif
  2072. +
  2073. DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
  2074. if (*ppos + count > mtd->size)
  2075. count = mtd->size - *ppos;
  2076. - if (!count)
  2077. + if (!count){
  2078. +#ifdef CONFIG_SL2312_SHARE_PIN
  2079. + mtd_unlock(); // sl2312 share pin lock
  2080. +#endif
  2081. return 0;
  2082. + }
  2083. /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
  2084. and pass them directly to the MTD functions */
  2085. @@ -178,8 +257,12 @@
  2086. else
  2087. kbuf=kmalloc(count, GFP_KERNEL);
  2088. - if (!kbuf)
  2089. + if (!kbuf) {
  2090. +#ifdef CONFIG_SL2312_SHARE_PIN
  2091. + mtd_unlock(); // sl2312 share pin lock
  2092. +#endif
  2093. return -ENOMEM;
  2094. + }
  2095. while (count) {
  2096. @@ -224,6 +307,9 @@
  2097. *ppos += retlen;
  2098. if (copy_to_user(buf, kbuf, retlen)) {
  2099. kfree(kbuf);
  2100. +#ifdef CONFIG_SL2312_SHARE_PIN
  2101. + mtd_unlock(); // sl2312 share pin lock
  2102. +#endif
  2103. return -EFAULT;
  2104. }
  2105. else
  2106. @@ -235,13 +321,19 @@
  2107. count = 0;
  2108. }
  2109. else {
  2110. - kfree(kbuf);
  2111. + kfree(kbuf);
  2112. +#ifdef CONFIG_SL2312_SHARE_PIN
  2113. + mtd_unlock(); // sl2312 share pin lock
  2114. +#endif
  2115. return ret;
  2116. }
  2117. }
  2118. kfree(kbuf);
  2119. +#ifdef CONFIG_SL2312_SHARE_PIN
  2120. + mtd_unlock(); // sl2312 share pin lock
  2121. +#endif
  2122. return total_retlen;
  2123. } /* mtd_read */
  2124. @@ -255,24 +347,40 @@
  2125. int ret=0;
  2126. int len;
  2127. +#ifdef CONFIG_SL2312_SHARE_PIN
  2128. + mtd_lock(); // sl2312 share pin lock
  2129. +#endif
  2130. +
  2131. DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
  2132. - if (*ppos == mtd->size)
  2133. + if (*ppos == mtd->size){
  2134. +#ifdef CONFIG_SL2312_SHARE_PIN
  2135. + mtd_unlock(); // sl2312 share pin lock
  2136. +#endif
  2137. return -ENOSPC;
  2138. + }
  2139. if (*ppos + count > mtd->size)
  2140. count = mtd->size - *ppos;
  2141. - if (!count)
  2142. + if (!count){
  2143. +#ifdef CONFIG_SL2312_SHARE_PIN
  2144. + mtd_unlock(); // sl2312 share pin lock
  2145. +#endif
  2146. return 0;
  2147. + }
  2148. if (count > MAX_KMALLOC_SIZE)
  2149. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  2150. else
  2151. kbuf=kmalloc(count, GFP_KERNEL);
  2152. - if (!kbuf)
  2153. + if (!kbuf) {
  2154. +#ifdef CONFIG_SL2312_SHARE_PIN
  2155. + mtd_unlock(); // sl2312 share pin lock
  2156. +#endif
  2157. return -ENOMEM;
  2158. + }
  2159. while (count) {
  2160. @@ -283,6 +391,9 @@
  2161. if (copy_from_user(kbuf, buf, len)) {
  2162. kfree(kbuf);
  2163. +#ifdef CONFIG_SL2312_SHARE_PIN
  2164. + mtd_unlock(); // sl2312 share pin lock
  2165. +#endif
  2166. return -EFAULT;
  2167. }
  2168. @@ -323,11 +434,17 @@
  2169. }
  2170. else {
  2171. kfree(kbuf);
  2172. +#ifdef CONFIG_SL2312_SHARE_PIN
  2173. + mtd_unlock(); // sl2312 share pin lock
  2174. +#endif
  2175. return ret;
  2176. }
  2177. }
  2178. kfree(kbuf);
  2179. +#ifdef CONFIG_SL2312_SHARE_PIN
  2180. + mtd_unlock(); // sl2312 share pin lock
  2181. +#endif
  2182. return total_retlen;
  2183. } /* mtd_write */
  2184. @@ -381,36 +498,67 @@
  2185. u_long size;
  2186. struct mtd_info_user info;
  2187. +#ifdef CONFIG_SL2312_SHARE_PIN
  2188. + mtd_lock(); // sl2312 share pin lock
  2189. +#endif
  2190. +
  2191. DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
  2192. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  2193. if (cmd & IOC_IN) {
  2194. if (!access_ok(VERIFY_READ, argp, size))
  2195. + {
  2196. +#ifdef CONFIG_SL2312_SHARE_PIN
  2197. + mtd_unlock(); // sl2312 share pin lock
  2198. +#endif
  2199. return -EFAULT;
  2200. + }
  2201. }
  2202. if (cmd & IOC_OUT) {
  2203. if (!access_ok(VERIFY_WRITE, argp, size))
  2204. + {
  2205. +#ifdef CONFIG_SL2312_SHARE_PIN
  2206. + mtd_unlock(); // sl2312 share pin lock
  2207. +#endif
  2208. return -EFAULT;
  2209. + }
  2210. }
  2211. switch (cmd) {
  2212. case MEMGETREGIONCOUNT:
  2213. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  2214. + {
  2215. +#ifdef CONFIG_SL2312_SHARE_PIN
  2216. + mtd_unlock(); // sl2312 share pin lock
  2217. +#endif
  2218. return -EFAULT;
  2219. + }
  2220. break;
  2221. case MEMGETREGIONINFO:
  2222. {
  2223. struct region_info_user ur;
  2224. - if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
  2225. + if (copy_from_user(&ur, argp, sizeof(struct region_info_user))) {
  2226. +#ifdef CONFIG_SL2312_SHARE_PIN
  2227. + mtd_unlock(); // sl2312 share pin lock
  2228. +#endif
  2229. return -EFAULT;
  2230. + }
  2231. - if (ur.regionindex >= mtd->numeraseregions)
  2232. + if (ur.regionindex >= mtd->numeraseregions) {
  2233. +#ifdef CONFIG_SL2312_SHARE_PIN
  2234. + mtd_unlock(); // sl2312 share pin lock
  2235. +#endif
  2236. return -EINVAL;
  2237. + }
  2238. if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
  2239. - sizeof(struct mtd_erase_region_info)))
  2240. + sizeof(struct mtd_erase_region_info))) {
  2241. +#ifdef CONFIG_SL2312_SHARE_PIN
  2242. + mtd_unlock(); // sl2312 share pin lock
  2243. +#endif
  2244. return -EFAULT;
  2245. + }
  2246. break;
  2247. }
  2248. @@ -433,7 +581,12 @@
  2249. struct erase_info *erase;
  2250. if(!(file->f_mode & 2))
  2251. + {
  2252. +#ifdef CONFIG_SL2312_SHARE_PIN
  2253. + mtd_unlock(); // sl2312 share pin lock
  2254. +#endif
  2255. return -EPERM;
  2256. + }
  2257. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  2258. if (!erase)
  2259. @@ -447,6 +600,9 @@
  2260. if (copy_from_user(&erase->addr, argp,
  2261. sizeof(struct erase_info_user))) {
  2262. kfree(erase);
  2263. +#ifdef CONFIG_SL2312_SHARE_PIN
  2264. + mtd_unlock(); // sl2312 share pin lock
  2265. +#endif
  2266. return -EFAULT;
  2267. }
  2268. erase->mtd = mtd;
  2269. @@ -484,14 +640,26 @@
  2270. struct mtd_oob_buf buf;
  2271. struct mtd_oob_ops ops;
  2272. - if(!(file->f_mode & 2))
  2273. + if(!(file->f_mode & 2)) {
  2274. +#ifdef CONFIG_SL2312_SHARE_PIN
  2275. + mtd_unlock(); // sl2312 share pin lock
  2276. +#endif
  2277. return -EPERM;
  2278. + }
  2279. - if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
  2280. + if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf))) {
  2281. +#ifdef CONFIG_SL2312_SHARE_PIN
  2282. + mtd_unlock(); // sl2312 share pin lock
  2283. +#endif
  2284. return -EFAULT;
  2285. + }
  2286. - if (buf.length > 4096)
  2287. + if (buf.length > 4096) {
  2288. +#ifdef CONFIG_SL2312_SHARE_PIN
  2289. + mtd_unlock(); // sl2312 share pin lock
  2290. +#endif
  2291. return -EINVAL;
  2292. + }
  2293. if (!mtd->write_oob)
  2294. ret = -EOPNOTSUPP;
  2295. @@ -499,8 +667,12 @@
  2296. ret = access_ok(VERIFY_READ, buf.ptr,
  2297. buf.length) ? 0 : EFAULT;
  2298. - if (ret)
  2299. + if (ret) {
  2300. +#ifdef CONFIG_SL2312_SHARE_PIN
  2301. + mtd_unlock(); // sl2312 share pin lock
  2302. +#endif
  2303. return ret;
  2304. + }
  2305. ops.ooblen = buf.length;
  2306. ops.ooboffs = buf.start & (mtd->oobsize - 1);
  2307. @@ -536,19 +708,35 @@
  2308. struct mtd_oob_buf buf;
  2309. struct mtd_oob_ops ops;
  2310. - if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
  2311. + if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf))) {
  2312. +#ifdef CONFIG_SL2312_SHARE_PIN
  2313. + mtd_unlock(); // sl2312 share pin lock
  2314. +#endif
  2315. return -EFAULT;
  2316. + }
  2317. - if (buf.length > 4096)
  2318. + if (buf.length > 4096) {
  2319. +#ifdef CONFIG_SL2312_SHARE_PIN
  2320. + mtd_unlock(); // sl2312 share pin lock
  2321. +#endif
  2322. return -EINVAL;
  2323. + }
  2324. - if (!mtd->read_oob)
  2325. + if (!mtd->read_oob) {
  2326. +#ifdef CONFIG_SL2312_SHARE_PIN
  2327. + mtd_unlock(); // sl2312 share pin lock
  2328. +#endif
  2329. ret = -EOPNOTSUPP;
  2330. + }
  2331. else
  2332. ret = access_ok(VERIFY_WRITE, buf.ptr,
  2333. buf.length) ? 0 : -EFAULT;
  2334. - if (ret)
  2335. + if (ret) {
  2336. +#ifdef CONFIG_SL2312_SHARE_PIN
  2337. + mtd_unlock(); // sl2312 share pin lock
  2338. +#endif
  2339. return ret;
  2340. + }
  2341. ops.ooblen = buf.length;
  2342. ops.ooboffs = buf.start & (mtd->oobsize - 1);
  2343. @@ -580,7 +768,12 @@
  2344. struct erase_info_user info;
  2345. if (copy_from_user(&info, argp, sizeof(info)))
  2346. + {
  2347. +#ifdef CONFIG_SL2312_SHARE_PIN
  2348. + mtd_unlock(); // sl2312 share pin lock
  2349. +#endif
  2350. return -EFAULT;
  2351. + }
  2352. if (!mtd->lock)
  2353. ret = -EOPNOTSUPP;
  2354. @@ -594,7 +787,12 @@
  2355. struct erase_info_user info;
  2356. if (copy_from_user(&info, argp, sizeof(info)))
  2357. + {
  2358. +#ifdef CONFIG_SL2312_SHARE_PIN
  2359. + mtd_unlock(); // sl2312 share pin lock
  2360. +#endif
  2361. return -EFAULT;
  2362. + }
  2363. if (!mtd->unlock)
  2364. ret = -EOPNOTSUPP;
  2365. @@ -629,11 +827,21 @@
  2366. loff_t offs;
  2367. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  2368. + {
  2369. +#ifdef CONFIG_SL2312_SHARE_PIN
  2370. + mtd_unlock(); // sl2312 share pin lock
  2371. +#endif
  2372. return -EFAULT;
  2373. + }
  2374. if (!mtd->block_isbad)
  2375. ret = -EOPNOTSUPP;
  2376. else
  2377. + {
  2378. +#ifdef CONFIG_SL2312_SHARE_PIN
  2379. + mtd_unlock(); // sl2312 share pin lock
  2380. +#endif
  2381. return mtd->block_isbad(mtd, offs);
  2382. + }
  2383. break;
  2384. }
  2385. @@ -642,11 +850,21 @@
  2386. loff_t offs;
  2387. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  2388. + {
  2389. +#ifdef CONFIG_SL2312_SHARE_PIN
  2390. + mtd_unlock(); // sl2312 share pin lock
  2391. +#endif
  2392. return -EFAULT;
  2393. + }
  2394. if (!mtd->block_markbad)
  2395. ret = -EOPNOTSUPP;
  2396. else
  2397. + {
  2398. +#ifdef CONFIG_SL2312_SHARE_PIN
  2399. + mtd_unlock(); // sl2312 share pin lock
  2400. +#endif
  2401. return mtd->block_markbad(mtd, offs);
  2402. + }
  2403. break;
  2404. }
  2405. @@ -654,8 +872,12 @@
  2406. case OTPSELECT:
  2407. {
  2408. int mode;
  2409. - if (copy_from_user(&mode, argp, sizeof(int)))
  2410. + if (copy_from_user(&mode, argp, sizeof(int))) {
  2411. +#ifdef CONFIG_SL2312_SHARE_PIN
  2412. + mtd_unlock(); // sl2312 share pin lock
  2413. +#endif
  2414. return -EFAULT;
  2415. + }
  2416. mfi->mode = MTD_MODE_NORMAL;
  2417. @@ -670,7 +892,12 @@
  2418. {
  2419. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  2420. if (!buf)
  2421. + {
  2422. +#ifdef CONFIG_SL2312_SHARE_PIN
  2423. + mtd_unlock(); // sl2312 share pin lock
  2424. +#endif
  2425. return -ENOMEM;
  2426. + }
  2427. ret = -EOPNOTSUPP;
  2428. switch (mfi->mode) {
  2429. case MTD_MODE_OTP_FACTORY:
  2430. @@ -701,12 +928,24 @@
  2431. {
  2432. struct otp_info info;
  2433. - if (mfi->mode != MTD_MODE_OTP_USER)
  2434. + if (mfi->mode != MTD_MODE_OTP_USER) {
  2435. +#ifdef CONFIG_SL2312_SHARE_PIN
  2436. + mtd_unlock(); // sl2312 share pin lock
  2437. +#endif
  2438. return -EINVAL;
  2439. - if (copy_from_user(&info, argp, sizeof(info)))
  2440. + }
  2441. + if (copy_from_user(&info, argp, sizeof(info))) {
  2442. +#ifdef CONFIG_SL2312_SHARE_PIN
  2443. + mtd_unlock(); // sl2312 share pin lock
  2444. +#endif
  2445. return -EFAULT;
  2446. - if (!mtd->lock_user_prot_reg)
  2447. + }
  2448. + if (!mtd->lock_user_prot_reg) {
  2449. +#ifdef CONFIG_SL2312_SHARE_PIN
  2450. + mtd_unlock(); // sl2312 share pin lock
  2451. +#endif
  2452. return -EOPNOTSUPP;
  2453. + }
  2454. ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
  2455. break;
  2456. }
  2457. @@ -742,8 +981,12 @@
  2458. break;
  2459. case MTD_MODE_RAW:
  2460. - if (!mtd->read_oob || !mtd->write_oob)
  2461. + if (!mtd->read_oob || !mtd->write_oob) {
  2462. +#ifdef CONFIG_SL2312_SHARE_PIN
  2463. + mtd_unlock(); // sl2312 share pin lock
  2464. +#endif
  2465. return -EOPNOTSUPP;
  2466. + }
  2467. mfi->mode = arg;
  2468. case MTD_MODE_NORMAL:
  2469. @@ -766,6 +1009,10 @@
  2470. ret = -ENOTTY;
  2471. }
  2472. +#ifdef CONFIG_SL2312_SHARE_PIN
  2473. + mtd_unlock(); // sl2312 share pin lock
  2474. +#endif
  2475. +
  2476. return ret;
  2477. } /* memory_ioctl */
  2478. --- a/drivers/mtd/nand/Kconfig
  2479. +++ b/drivers/mtd/nand/Kconfig
  2480. @@ -44,6 +44,13 @@
  2481. This enables the driver for the autronix autcpu12 board to
  2482. access the SmartMediaCard.
  2483. +config MTD_NAND_SL2312
  2484. + tristate "NAND Flash device on Storlink board"
  2485. + depends on ARM && MTD_NAND && ARCH_SL2312
  2486. + help
  2487. + This enables the driver for the Storlink board to
  2488. + access the nand device.
  2489. +
  2490. config MTD_NAND_EDB7312
  2491. tristate "Support for Cirrus Logic EBD7312 evaluation board"
  2492. depends on ARCH_EDB7312
  2493. --- /dev/null
  2494. +++ b/drivers/mtd/nand/sl2312-flash-nand.c
  2495. @@ -0,0 +1,2287 @@
  2496. +/*
  2497. + * drivers/mtd/sl2312.c
  2498. + *
  2499. + * $Id: sl2312-flash-nand.c,v 1.5 2006/06/15 07:02:29 middle Exp $
  2500. + *
  2501. + * Copyright (C) 2001 Toshiba Corporation
  2502. + *
  2503. + * 2003 (c) MontaVista Software, Inc. This file is licensed under
  2504. + * the terms of the GNU General Public License version 2. This program
  2505. + * is licensed "as is" without any warranty of any kind, whether express
  2506. + * or implied.
  2507. + *
  2508. + */
  2509. +
  2510. +#include <linux/slab.h>
  2511. +#include <linux/init.h>
  2512. +#include <linux/module.h>
  2513. +#include <linux/mtd/mtd.h>
  2514. +#include <linux/mtd/nand.h>
  2515. +#include <linux/mtd/nand_ecc.h>
  2516. +#include <linux/mtd/partitions.h>
  2517. +#include <linux/delay.h>
  2518. +#include <asm/io.h>
  2519. +#include <asm/hardware.h>
  2520. +#include <asm/arch/sl2312.h>
  2521. +#include "sl2312-flash-nand.h"
  2522. +
  2523. +
  2524. +#include <linux/errno.h>
  2525. +#include <linux/sched.h>
  2526. +#include <linux/types.h>
  2527. +#include <linux/mtd/compatmac.h>
  2528. +#include <linux/interrupt.h>
  2529. +#include <linux/bitops.h>
  2530. +
  2531. +
  2532. +/*
  2533. + * NAND low-level MTD interface functions
  2534. + */
  2535. +static void sl2312_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
  2536. +static void sl2312_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
  2537. +static int sl2312_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
  2538. +
  2539. +static int sl2312_nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
  2540. +static int sl2312_nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
  2541. +static int sl2312_nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf);
  2542. +static int sl2312_nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf);
  2543. +static int sl2312_nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
  2544. + size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel);
  2545. +static int sl2312_nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf);
  2546. +static int sl2312_nand_writev (struct mtd_info *mtd, const struct kvec *vecs,
  2547. + unsigned long count, loff_t to, size_t * retlen);
  2548. +static int sl2312_nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
  2549. + unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel);
  2550. +static int sl2312_nand_erase (struct mtd_info *mtd, struct erase_info *instr, int allowbbt);
  2551. +static void sl2312_nand_sync (struct mtd_info *mtd);
  2552. +static int sl2312_nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf, struct nand_oobinfo *oobsel);
  2553. +static int sl2312_nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt);
  2554. +static int sl2312_nand_erase_block(struct mtd_info *mtd, int page);
  2555. +
  2556. +/*
  2557. + * MTD structure for sl2312 NDFMC
  2558. + */
  2559. +static struct mtd_info *sl2312_mtd = NULL;
  2560. +static int nand_page=0,nand_col=0;
  2561. +
  2562. +/* Define default oob placement schemes for large and small page devices */
  2563. +static struct nand_oobinfo nand_oob_8 = {
  2564. + .useecc = MTD_NANDECC_AUTOPLACE,
  2565. + .eccbytes = 3,
  2566. + .eccpos = {0, 1, 2},
  2567. + .oobfree = { {3, 2}, {6, 2} }
  2568. +};
  2569. +
  2570. +static struct nand_oobinfo nand_oob_16 = {
  2571. + .useecc = MTD_NANDECC_AUTOPLACE,
  2572. + .eccbytes = 6,
  2573. + .eccpos = {0, 1, 2, 3, 6, 7},
  2574. + .oobfree = { {8, 8} }
  2575. +};
  2576. +
  2577. +static struct nand_oobinfo nand_oob_64 = {
  2578. + .useecc = MTD_NANDECC_AUTOPLACE,
  2579. + .eccbytes = 24,
  2580. + .eccpos = {
  2581. + 40, 41, 42, 43, 44, 45, 46, 47,
  2582. + 48, 49, 50, 51, 52, 53, 54, 55,
  2583. + 56, 57, 58, 59, 60, 61, 62, 63},
  2584. + .oobfree = { {2, 38} }
  2585. +};
  2586. +
  2587. +
  2588. +/*
  2589. + * Define partitions for flash device
  2590. + */
  2591. +/* the base address of FLASH control register */
  2592. +#define FLASH_CONTROL_BASE_ADDR (IO_ADDRESS(SL2312_FLASH_CTRL_BASE))
  2593. +#define SL2312_GLOBAL_BASE_ADDR (IO_ADDRESS(SL2312_GLOBAL_BASE))
  2594. +//#define SL2312_FLASH_BASE_ADDR (IO_ADDRESS(SL2312_FLASH_BASE))
  2595. +#define SL2312_FLASH_BASE_ADDR FLASH_VADDR(SL2312_FLASH_BASE)
  2596. +static unsigned int CHIP_EN;
  2597. +/* define read/write register utility */
  2598. +//#define FLASH_READ_REG(offset) (__raw_readl(offset+FLASH_CONTROL_BASE_ADDR))
  2599. +//#define FLASH_WRITE_REG(offset,val) (__raw_writel(val,offset+FLASH_CONTROL_BASE_ADDR))
  2600. +//#define FLASH_READ_DATA(offset) (__raw_readb(offset+SL2312_FLASH_BASE_ADDR))
  2601. +//#define FLASH_WRITE_DATA(offset,val) (__raw_writeb(val,offset+SL2312_FLASH_BASE_ADDR))
  2602. +
  2603. +unsigned int FLASH_READ_REG(unsigned int addr)
  2604. +{
  2605. + unsigned int *base;
  2606. + unsigned int data;
  2607. +
  2608. + base = (unsigned int *)(FLASH_CONTROL_BASE_ADDR + addr);
  2609. + data = *base;
  2610. + return (data);
  2611. +}
  2612. +
  2613. +void FLASH_WRITE_REG(unsigned int addr,unsigned int data)
  2614. +{
  2615. + unsigned int *base;
  2616. +
  2617. + base = (unsigned int *)(FLASH_CONTROL_BASE_ADDR + addr);
  2618. + *base = data;
  2619. + return;
  2620. +}
  2621. +
  2622. +unsigned int FLASH_READ_DATA(unsigned int addr)
  2623. +{
  2624. + unsigned char *base;
  2625. + unsigned int data;
  2626. +
  2627. + base = (unsigned char *)(SL2312_FLASH_BASE_ADDR + addr);
  2628. + data = *base;
  2629. + return (data);
  2630. +}
  2631. +
  2632. +void FLASH_WRITE_DATA(unsigned int addr,unsigned int data)
  2633. +{
  2634. + unsigned char *base;
  2635. +
  2636. + base = (unsigned char *)(SL2312_FLASH_BASE_ADDR + addr);
  2637. + *base = data;
  2638. + return;
  2639. +}
  2640. +
  2641. +/* the offset of FLASH control register */
  2642. +enum NFLASH_REGISTER {
  2643. + NFLASH_ID = 0x0000,
  2644. + NFLASH_STATUS = 0x0008,
  2645. + NFLASH_TYPE = 0x000c,
  2646. + NFLASH_ACCESS = 0x0030,
  2647. + NFLASH_COUNT = 0x0034,
  2648. + NFLASH_CMD_ADDR = 0x0038,
  2649. + NFLASH_ADDRESS = 0x003C,
  2650. + NFLASH_DATA = 0x0040,
  2651. + NFLASH_TIMING = 0x004C,
  2652. + NFLASH_ECC_STATUS = 0x0050,
  2653. + NFLASH_ECC_CONTROL = 0x0054,
  2654. + NFLASH_ECC_OOB = 0x005c,
  2655. + NFLASH_ECC_CODE_GEN0 = 0x0060,
  2656. + NFLASH_ECC_CODE_GEN1 = 0x0064,
  2657. + NFLASH_ECC_CODE_GEN2 = 0x0068,
  2658. + NFLASH_ECC_CODE_GEN3 = 0x006C,
  2659. + NFLASH_FIFO_CONTROL = 0x0070,
  2660. + NFLASH_FIFO_STATUS = 0x0074,
  2661. + NFLASH_FIFO_ADDRESS = 0x0078,
  2662. + NFLASH_FIFO_DATA = 0x007c,
  2663. +};
  2664. +
  2665. +
  2666. +
  2667. +//#define FLASH_BASE FLASH_CONTROL_BASE_ADDR
  2668. +//#define FLASH_SIZE 0x00800000 //INTEGRATOR_FLASH_SIZE
  2669. +
  2670. +//#define FLASH_PART_SIZE 8388608
  2671. +
  2672. +//static unsigned int flash_indirect_access = 0;
  2673. +
  2674. +
  2675. +#ifdef CONFIG_SL2312_SHARE_PIN
  2676. +void sl2312flash_enable_nand_flash(void)
  2677. +{
  2678. + unsigned int reg_val;
  2679. +
  2680. + reg_val = readl(SL2312_GLOBAL_BASE_ADDR + 0x30);
  2681. + reg_val = reg_val & 0xfffffffb;
  2682. + writel(reg_val,SL2312_GLOBAL_BASE_ADDR + 0x30);
  2683. + return;
  2684. +}
  2685. +
  2686. +void sl2312flash_disable_nand_flash(void)
  2687. +{
  2688. + unsigned int reg_val;
  2689. +
  2690. + reg_val = readl(SL2312_GLOBAL_BASE_ADDR + 0x30);
  2691. + reg_val = reg_val | 0x00000004;
  2692. + writel(reg_val,SL2312_GLOBAL_BASE_ADDR + 0x30);
  2693. + return;
  2694. +}
  2695. +#endif
  2696. +
  2697. +extern struct nand_oobinfo jffs2_oobinfo;
  2698. +/*
  2699. + * Define partitions for flash devices
  2700. + */
  2701. +
  2702. +static struct mtd_partition sl2312_partitions[] = {
  2703. + { name: "RedBoot", offset: 0x00000000, size: 0x0020000, },
  2704. + { name: "Kernel", offset: 0x00020000, size: 0x00200000, },
  2705. + { name: "Ramdisk", offset: 0x00220000, size: 0x00280000, },
  2706. + { name: "Application", offset: 0x004A0000, size: 0x00320000, },
  2707. + { name: "VCTL", offset: 0x007C0000, size: 0x20000, },
  2708. + { name: "CurConf", offset: 0x007E0000, size: 0x20000, },
  2709. + { name: "FIS directory", offset: 0x007e0000, size: 0x00020000, }
  2710. +
  2711. +};
  2712. +
  2713. +
  2714. +/*
  2715. + * hardware specific access to control-lines
  2716. +*/
  2717. +static void sl2312_hwcontrol(struct mtd_info *mtd, int cmd)
  2718. +{
  2719. +
  2720. + return ;
  2721. +}
  2722. +
  2723. +static int sl2312_nand_scan_bbt(struct mtd_info *mtd)
  2724. +{
  2725. + return 0;
  2726. +}
  2727. +
  2728. +/**
  2729. + * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad
  2730. + * @mtd: MTD device structure
  2731. + * @ofs: offset relative to mtd start
  2732. + */
  2733. +static int sl2312_nand_block_isbad (struct mtd_info *mtd, loff_t ofs)
  2734. +{
  2735. + /* Check for invalid offset */
  2736. + if (ofs > mtd->size)
  2737. + return -EINVAL;
  2738. +
  2739. + return sl2312_nand_block_checkbad (mtd, ofs, 1, 0);
  2740. +}
  2741. +
  2742. +/**
  2743. + * nand_block_checkbad - [GENERIC] Check if a block is marked bad
  2744. + * @mtd: MTD device structure
  2745. + * @ofs: offset from device start
  2746. + * @getchip: 0, if the chip is already selected
  2747. + * @allowbbt: 1, if its allowed to access the bbt area
  2748. + *
  2749. + * Check, if the block is bad. Either by reading the bad block table or
  2750. + * calling of the scan function.
  2751. + */
  2752. +
  2753. +static int sl2312_nand_erase_block(struct mtd_info *mtd, int page)
  2754. +{
  2755. + int opcode;
  2756. + /* Send commands to erase a page */
  2757. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
  2758. +
  2759. + if(mtd->oobblock > 528)
  2760. + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f0fff21); // 3 address & 2 command
  2761. + else
  2762. + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f0fff11); // 2 address & 2 command
  2763. +
  2764. + FLASH_WRITE_REG(NFLASH_CMD_ADDR, 0x0000d060); // write read id command
  2765. + FLASH_WRITE_REG(NFLASH_ADDRESS, page); //write address 0x00
  2766. +
  2767. +
  2768. +
  2769. + /* read maker code */
  2770. + opcode = 0x80003000|DWIDTH|CHIP_EN; //set start bit & 8bits write command
  2771. + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
  2772. +
  2773. + while(opcode&0x80000000) //polling flash access 31b
  2774. + {
  2775. + opcode=FLASH_READ_REG(NFLASH_ACCESS);
  2776. + //sl2312_flash_delay();
  2777. + schedule();
  2778. + //cond_resched();
  2779. + }
  2780. +}
  2781. +
  2782. +void sl2312_flash_delay(void)
  2783. +{
  2784. + int i;
  2785. +
  2786. + for(i=0; i<50; i++)
  2787. + i=i;
  2788. +}
  2789. +
  2790. +static int sl2312_nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt)
  2791. +{
  2792. + struct nand_chip *this = mtd->priv;
  2793. +
  2794. + if (!this->bbt)
  2795. + return this->block_bad(mtd, ofs, getchip);
  2796. +
  2797. + /* Return info from the table */
  2798. + return nand_isbad_bbt (mtd, ofs, allowbbt);
  2799. +}
  2800. +
  2801. +/**
  2802. + * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad
  2803. + * @mtd: MTD device structure
  2804. + * @ofs: offset relative to mtd start
  2805. + */
  2806. +static int sl2312_nand_block_markbad (struct mtd_info *mtd, loff_t ofs)
  2807. +{
  2808. + struct nand_chip *this = mtd->priv;
  2809. + int ret;
  2810. +
  2811. + if ((ret = sl2312_nand_block_isbad(mtd, ofs))) {
  2812. + /* If it was bad already, return success and do nothing. */
  2813. + if (ret > 0)
  2814. + return 0;
  2815. + return ret;
  2816. + }
  2817. +
  2818. + return this->block_markbad(mtd, ofs);
  2819. +}
  2820. +
  2821. +/*
  2822. + * Get chip for selected access
  2823. + */
  2824. +static inline void sl2312_nand_get_chip (struct nand_chip *this, struct mtd_info *mtd, int new_state, int *erase_state)
  2825. +{
  2826. +
  2827. + DECLARE_WAITQUEUE (wait, current);
  2828. +
  2829. + /*
  2830. + * Grab the lock and see if the device is available
  2831. + * For erasing, we keep the spinlock until the
  2832. + * erase command is written.
  2833. + */
  2834. +retry:
  2835. + spin_lock_bh (&this->chip_lock);
  2836. +
  2837. + if (this->state == FL_READY) {
  2838. + this->state = new_state;
  2839. + if (new_state != FL_ERASING)
  2840. + spin_unlock_bh (&this->chip_lock);
  2841. + return;
  2842. + }
  2843. +
  2844. + if (this->state == FL_ERASING) {
  2845. + if (new_state != FL_ERASING) {
  2846. + this->state = new_state;
  2847. + spin_unlock_bh (&this->chip_lock);
  2848. + this->select_chip(mtd, 0); /* select in any case */
  2849. + this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2850. + return;
  2851. + }
  2852. + }
  2853. +
  2854. + set_current_state (TASK_UNINTERRUPTIBLE);
  2855. + add_wait_queue (&this->wq, &wait);
  2856. + spin_unlock_bh (&this->chip_lock);
  2857. + schedule ();
  2858. + remove_wait_queue (&this->wq, &wait);
  2859. + goto retry;
  2860. +}
  2861. +
  2862. +/*
  2863. +* read device ready pin
  2864. +*/
  2865. +static int sl2312_device_ready(struct mtd_info *mtd)
  2866. +{
  2867. + int ready;
  2868. +
  2869. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
  2870. + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f000070); //set only command no address and two data
  2871. +
  2872. + FLASH_WRITE_REG(NFLASH_CMD_ADDR, 0x00000070); //write read status command
  2873. +
  2874. +
  2875. + ready = 0x80002000|DWIDTH|CHIP_EN; //set start bit & 8bits read command
  2876. + FLASH_WRITE_REG(NFLASH_ACCESS, ready);
  2877. +
  2878. + while(ready&0x80000000) //polling flash access 31b
  2879. + {
  2880. + ready=FLASH_READ_REG(NFLASH_ACCESS);
  2881. + //sl2312_flash_delay();
  2882. + schedule();
  2883. + }
  2884. + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
  2885. + ready=FLASH_READ_REG(NFLASH_DATA)&0xff;
  2886. + return ready;
  2887. +}
  2888. +void sl2312_enable_hwecc(struct mtd_info *mtd, int mode)
  2889. +{
  2890. + /* reset first */
  2891. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x80000001); //set 31b = 0
  2892. +
  2893. +}
  2894. +
  2895. +
  2896. +void sl2312_device_setup(void)
  2897. +{
  2898. +
  2899. +}
  2900. +static u_char sl2312_nand_read_byte(struct mtd_info *mtd)
  2901. +{
  2902. +
  2903. + unsigned int data=0, page=0, col=0, tmp, i;
  2904. +
  2905. + printk ("**************************sl2312_nand_read_byte !! \n");
  2906. + //page = FLASH_READ_REG(NFLASH_ADDRESS)&0xffffff00;
  2907. + //col = FLASH_READ_REG(NFLASH_ADDRESS)&0x000000ff;
  2908. + page = nand_page;
  2909. + col = nand_col;
  2910. + for(i=0;i<(mtd->oobblock+mtd->oobsize);i++)
  2911. + {
  2912. + if(i==col)
  2913. + data = FLASH_READ_DATA(page*mtd->oobblock +i);
  2914. + else
  2915. + tmp = FLASH_READ_DATA(page*mtd->oobblock +i);
  2916. + }
  2917. + return data&0xff;
  2918. +}
  2919. +
  2920. +static void sl2312_nand_write_byte(struct mtd_info *mtd, u_char byte)
  2921. +{
  2922. + //struct nand_chip *this = mtd->priv;
  2923. + unsigned int page=0, col=0, i;
  2924. + u_char *databuf,oobbuf[mtd->oobsize];
  2925. + size_t retlen;
  2926. + retlen=0;
  2927. + printk ("********************sl2312_nand_write_byte !! \n");
  2928. + page = nand_page;
  2929. + col = nand_col;
  2930. + databuf = kmalloc (mtd->oobsize+mtd->oobblock,GFP_KERNEL);
  2931. +
  2932. + if (!databuf) {
  2933. + printk ("sl2312_nand_write_byte : Unable to allocate SL2312 NAND MTD device structure.\n");
  2934. +
  2935. + }
  2936. +
  2937. + for(i=0;i<(mtd->oobblock+mtd->oobsize);i++)
  2938. + databuf[i] = FLASH_READ_DATA(page*mtd->oobblock +i);
  2939. +
  2940. + databuf[col] = byte;
  2941. + sl2312_nand_write_ecc (mtd, page, mtd->oobblock, &retlen, databuf, oobbuf, NULL);
  2942. +
  2943. +}
  2944. +
  2945. +static void sl2312_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  2946. +{
  2947. + int i, page=0,col=0;
  2948. + struct nand_chip *this = mtd->priv;
  2949. + u_char *databuf, *oobbuf;
  2950. + size_t retlen;
  2951. + retlen=0;
  2952. +
  2953. +
  2954. + printk ("***********************sl2312_nand_write_buf !! \n");
  2955. + databuf = &(this->data_buf[0]);
  2956. + oobbuf = &(this->data_buf[mtd->oobblock]);
  2957. + for (i = 0; i < mtd->oobsize; i++)
  2958. + oobbuf[i] = 0xff;
  2959. +
  2960. + if(len < mtd->oobblock)
  2961. + {
  2962. + //addr = FLASH_READ_REG(NFLASH_ADDRESS);
  2963. + //page = FLASH_READ_REG(NFLASH_ADDRESS)&0xffffff00;
  2964. + //col = FLASH_READ_REG(NFLASH_ADDRESS)&0x000000ff;
  2965. + page = nand_page;
  2966. + col = nand_col;
  2967. +
  2968. + sl2312_nand_read_ecc (mtd, page, mtd->oobblock , &retlen, databuf, oobbuf, NULL);
  2969. +
  2970. + for(i=col;i<len;i++)
  2971. + databuf[col+i] = buf[i];
  2972. +
  2973. + sl2312_nand_write_ecc (mtd, page, mtd->oobblock, &retlen, databuf, oobbuf, NULL);
  2974. +
  2975. + }
  2976. +
  2977. +}
  2978. +
  2979. +static void sl2312_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  2980. +{
  2981. + int i, page=0,col=0,addr=0,tmp=0;
  2982. + //struct nand_chip *this = mtd->priv;
  2983. + printk ("********************sl2312_nand_read_buf !! \n");
  2984. + if(len < mtd->oobblock)
  2985. + {
  2986. + //addr = FLASH_READ_REG(NFLASH_ADDRESS);
  2987. + //page = FLASH_READ_REG(NFLASH_ADDRESS)&0xffffff00;
  2988. + //col = FLASH_READ_REG(NFLASH_ADDRESS)&0x000000ff;
  2989. + page = nand_page;
  2990. + col = nand_col;
  2991. + for (i=col; i<((mtd->oobblock+mtd->oobsize)-col); i++)
  2992. + {
  2993. + if(i<len)
  2994. + buf[i] = FLASH_READ_DATA(addr+i);
  2995. + else
  2996. + tmp = FLASH_READ_DATA(addr+i);
  2997. + }
  2998. + }
  2999. +}
  3000. +
  3001. +static int sl2312_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  3002. +{
  3003. + int i;
  3004. + //struct nand_chip *this = mtd->priv;
  3005. + u_char *datatmp, *oobtmp;
  3006. + size_t retlen;
  3007. + retlen=0;
  3008. +
  3009. + datatmp = kmalloc (mtd->oobblock,GFP_KERNEL);
  3010. + oobtmp = kmalloc (mtd->oobsize,GFP_KERNEL);
  3011. +
  3012. + if ((!datatmp)||(!oobtmp)) {
  3013. + printk ("sl2312_nand_verify_buf : Unable to allocate SL2312 NAND MTD device structure.\n");
  3014. +
  3015. + }
  3016. + //page = nand_page;
  3017. + for(i=0;i<mtd->oobblock;i++)
  3018. + datatmp[i] = FLASH_READ_DATA(nand_page*mtd->oobblock +i);
  3019. + /* read oobdata */
  3020. + for (i = 0; i < mtd->oobsize; i++)
  3021. + oobtmp[i] = FLASH_READ_DATA(nand_page*mtd->oobblock + mtd->oobblock + i);
  3022. +
  3023. + if(len==mtd->oobblock)
  3024. + {
  3025. + for (i=0; i<len; i++)
  3026. + {
  3027. + if (buf[i] != datatmp[i])
  3028. + {
  3029. + kfree(datatmp);
  3030. + kfree(oobtmp);
  3031. + printk("Data verify error -> page: %x, byte: %x \n",nand_page,i);
  3032. + return i;
  3033. + }
  3034. + }
  3035. + }
  3036. + else if(len == mtd->oobsize)
  3037. + {
  3038. + for (i=0; i<len; i++)
  3039. + {
  3040. + if (buf[i] != oobtmp[i])
  3041. + {
  3042. + kfree(datatmp);
  3043. + kfree(oobtmp);
  3044. + printk("OOB verify error -> page: %x, byte: %x \n",nand_page,i);
  3045. + return i;
  3046. + }
  3047. + }
  3048. + }
  3049. + else
  3050. + {
  3051. + printk (KERN_WARNING "sl2312_nand_verify_buf : verify length not match 0x%08x\n", len);
  3052. + kfree(datatmp);
  3053. + kfree(oobtmp);
  3054. + return -1;
  3055. + }
  3056. +
  3057. + kfree(datatmp);
  3058. + kfree(oobtmp);
  3059. + return 0;
  3060. +}
  3061. +
  3062. +/*
  3063. + * Send command to NAND device
  3064. + */
  3065. +static void sl2312_nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
  3066. +{
  3067. + register struct nand_chip *this = mtd->priv;
  3068. + int opcode;
  3069. +
  3070. +
  3071. + /*
  3072. + * program and erase have their own busy handlers
  3073. + * status and sequential in needs no delay
  3074. + */
  3075. + switch (command) {
  3076. +
  3077. + case NAND_CMD_PAGEPROG:
  3078. + case NAND_CMD_ERASE1:
  3079. + case NAND_CMD_ERASE2:
  3080. + case NAND_CMD_SEQIN:
  3081. + case NAND_CMD_STATUS:
  3082. + case NAND_CMD_READ0:
  3083. +
  3084. + /*
  3085. + * Write out the command to the device.
  3086. + */
  3087. + if (column != -1 || page_addr != -1) {
  3088. +
  3089. + /* Serially input address */
  3090. + if (column != -1)
  3091. + //FLASH_WRITE_REG(NFLASH_ADDRESS,column);
  3092. + nand_col=column;
  3093. +
  3094. + opcode = FLASH_READ_REG(NFLASH_ADDRESS);
  3095. +
  3096. + if (page_addr != -1)
  3097. + //FLASH_WRITE_REG(NFLASH_ADDRESS,opcode|(page_addr<<8));
  3098. + nand_page = page_addr;
  3099. +
  3100. + }
  3101. + return;
  3102. +
  3103. + case NAND_CMD_RESET:
  3104. + if (this->dev_ready)
  3105. + break;
  3106. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
  3107. + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f0fff70); //set only command and no other data
  3108. + FLASH_WRITE_REG(NFLASH_CMD_ADDR, NAND_CMD_RESET); //write reset command
  3109. +
  3110. + opcode = 0x80002000|DWIDTH|CHIP_EN; //set start bit & 8bits read command
  3111. + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
  3112. +
  3113. + while(opcode&0x80000000) //polling flash access 31b
  3114. + {
  3115. + opcode=FLASH_READ_REG(NFLASH_ACCESS);
  3116. + //sl2312_flash_delay();
  3117. + schedule();
  3118. + }
  3119. + while ( !(sl2312_device_ready(mtd) & 0x40));
  3120. + {
  3121. + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
  3122. + //sl2312_flash_delay();
  3123. + schedule();
  3124. + return;
  3125. + }
  3126. + /* This applies to read commands */
  3127. + default:
  3128. + /*
  3129. + * If we don't have access to the busy pin, we apply the given
  3130. + * command delay
  3131. + */
  3132. + if (!this->dev_ready) {
  3133. + udelay (this->chip_delay);
  3134. + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
  3135. + return;
  3136. + }
  3137. + }
  3138. +
  3139. + /* wait until command is processed */
  3140. + while (!this->dev_ready(mtd));
  3141. +
  3142. +}
  3143. +/*Add function*/
  3144. +static void nand_read_id(int chip_no, unsigned char *id)
  3145. +{
  3146. + unsigned int opcode, i;
  3147. +
  3148. + if(chip_no==0)
  3149. + CHIP_EN = NFLASH_CHIP0_EN;
  3150. + else
  3151. + CHIP_EN = NFLASH_CHIP1_EN;
  3152. +
  3153. + opcode = FLASH_READ_REG(NFLASH_TYPE);
  3154. +
  3155. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
  3156. + if((opcode&0x00000300)<=0x00000100)
  3157. + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f000100); //set only command & address and two data
  3158. + else
  3159. + FLASH_WRITE_REG(NFLASH_COUNT, 0x7f000300); //set only command & address and 4 data
  3160. +
  3161. + FLASH_WRITE_REG(NFLASH_CMD_ADDR, 0x00000090); //write read id command
  3162. + FLASH_WRITE_REG(NFLASH_ADDRESS, 0x00000000); //write address 0x00
  3163. +
  3164. + /* read maker code */
  3165. + opcode = 0x80002000|DWIDTH|CHIP_EN;//|chip0_en; //set start bit & 8bits read command
  3166. + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
  3167. + opcode=FLASH_READ_REG(NFLASH_ACCESS);
  3168. + while(opcode&0x80000000) //polling flash access 31b
  3169. + {
  3170. + opcode=FLASH_READ_REG(NFLASH_ACCESS);
  3171. + //sl2312_flash_delay();
  3172. + schedule();
  3173. + }
  3174. +
  3175. + opcode = FLASH_READ_REG(NFLASH_DATA);
  3176. + if(DWIDTH==NFLASH_WiDTH16)
  3177. + {
  3178. + id[0] = opcode&0xff;
  3179. + id[1] = (opcode&0xff00)>>8;
  3180. + }
  3181. + else
  3182. + {
  3183. + id[0] = opcode&0xff;
  3184. + opcode = 0x80002000|DWIDTH|CHIP_EN;//|chip0_en; //set start bit & 8bits read command
  3185. + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
  3186. + opcode=FLASH_READ_REG(NFLASH_ACCESS);
  3187. + while(opcode&0x80000000) //polling flash access 31b
  3188. + {
  3189. + opcode=FLASH_READ_REG(NFLASH_ACCESS);
  3190. + //sl2312_flash_delay();
  3191. + schedule();
  3192. + }
  3193. + opcode = FLASH_READ_REG(NFLASH_DATA);
  3194. + id[1] = (opcode&0xff00)>>8;
  3195. +
  3196. + opcode=FLASH_READ_REG(NFLASH_TYPE);
  3197. + if((opcode&0x300)>0x100)
  3198. + {
  3199. + for(i=0;i<2;i++)
  3200. + {
  3201. + //data cycle 3 & 4 ->not use
  3202. + opcode = 0x80002000|DWIDTH|CHIP_EN;//set start bit & 8bits read command
  3203. + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
  3204. + opcode=FLASH_READ_REG(NFLASH_ACCESS);
  3205. + while(opcode&0x80000000) //polling flash access 31b
  3206. + {
  3207. + opcode=FLASH_READ_REG(NFLASH_ACCESS);
  3208. + //sl2312_flash_delay();
  3209. + schedule();
  3210. + }
  3211. +
  3212. + opcode=FLASH_READ_REG(NFLASH_DATA);
  3213. + id[2+i] = (opcode&(0xff0000<<i*8))>>(8*(2+i));
  3214. + }
  3215. + }
  3216. + }
  3217. + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
  3218. +}
  3219. +
  3220. +/*
  3221. + * NAND erase a block
  3222. + */
  3223. +static int sl2312_nand_erase (struct mtd_info *mtd, struct erase_info *instr, int allowbbt)
  3224. +{
  3225. + int page, len, status, pages_per_block, ret, chipnr;
  3226. + struct nand_chip *this = mtd->priv;
  3227. +
  3228. + DEBUG (MTD_DEBUG_LEVEL3,
  3229. + "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len);
  3230. +
  3231. + /* Start address must align on block boundary */
  3232. + if (instr->addr & ((1 << this->phys_erase_shift) - 1)) {
  3233. + DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
  3234. + return -EINVAL;
  3235. + }
  3236. +
  3237. + /* Length must align on block boundary */
  3238. + if (instr->len & ((1 << this->phys_erase_shift) - 1)) {
  3239. + DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n");
  3240. + return -EINVAL;
  3241. + }
  3242. +
  3243. + /* Do not allow erase past end of device */
  3244. + if ((instr->len + instr->addr) > mtd->size) {
  3245. + DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n");
  3246. + return -EINVAL;
  3247. + }
  3248. +
  3249. + instr->fail_addr = 0xffffffff;
  3250. +
  3251. + /* Grab the lock and see if the device is available */
  3252. + sl2312_nand_get_chip (this, mtd, FL_ERASING, NULL);
  3253. +
  3254. + /* Shift to get first page */
  3255. + page = (int) (instr->addr >> this->page_shift);
  3256. + chipnr = (int) (instr->addr >> this->chip_shift);
  3257. +
  3258. + /* Calculate pages in each block */
  3259. + pages_per_block = 1 << (this->phys_erase_shift - this->page_shift);
  3260. +
  3261. + /* Select the NAND device */
  3262. + //this->select_chip(mtd, chipnr);
  3263. + this->select_chip(mtd, 0);
  3264. +
  3265. + /* Check the WP bit */
  3266. + /* Check, if it is write protected */
  3267. + status = sl2312_device_ready(mtd);
  3268. + if (!(status & 0x80)) {
  3269. + DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n");
  3270. + instr->state = MTD_ERASE_FAILED;
  3271. + goto erase_exit;
  3272. + }
  3273. +
  3274. + /* Loop through the pages */
  3275. + len = instr->len;
  3276. +
  3277. + instr->state = MTD_ERASING;
  3278. +
  3279. + while (len) {
  3280. + /* Check if we have a bad block, we do not erase bad blocks ! */
  3281. + if (this->block_bad(mtd, ((loff_t) page) << this->page_shift, 0)) {
  3282. + printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page);
  3283. + //instr->state = MTD_ERASE_FAILED;
  3284. + //goto erase_exit;
  3285. + }
  3286. +
  3287. + /* Invalidate the page cache, if we erase the block which contains
  3288. + the current cached page */
  3289. + if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block))
  3290. + this->pagebuf = -1;
  3291. + /////////
  3292. +
  3293. + ///* Send commands to erase a page */
  3294. + //FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
  3295. + //
  3296. + //if(mtd->oobblock > 528)
  3297. + // FLASH_WRITE_REG(NFLASH_COUNT, 0x7f0fff21); // 3 address & 2 command
  3298. + //else
  3299. + // FLASH_WRITE_REG(NFLASH_COUNT, 0x7f0fff11); // 2 address & 2 command
  3300. + //
  3301. + //FLASH_WRITE_REG(NFLASH_CMD_ADDR, 0x0000d060); // write read id command
  3302. + //FLASH_WRITE_REG(NFLASH_ADDRESS, page); //write address 0x00
  3303. + //
  3304. + //
  3305. + //
  3306. + ///* read maker code */
  3307. + //opcode = 0x80003000|DWIDTH|CHIP_EN; //set start bit & 8bits write command
  3308. + //FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
  3309. + //
  3310. + //while(opcode&0x80000000) //polling flash access 31b
  3311. + //{
  3312. + // opcode=FLASH_READ_REG(NFLASH_ACCESS);
  3313. + // //sl2312_flash_delay();
  3314. + // schedule();
  3315. + // //cond_resched();
  3316. + //}
  3317. + sl2312_nand_erase_block(mtd, page);
  3318. + //////////////
  3319. + status = this->waitfunc (mtd, this, FL_ERASING);
  3320. + /* See if block erase succeeded */
  3321. + if (status & 0x01) {
  3322. + DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page);
  3323. + instr->state = MTD_ERASE_FAILED;
  3324. + instr->fail_addr = (page << this->page_shift);
  3325. + goto erase_exit;
  3326. + }
  3327. +
  3328. + /* Increment page address and decrement length */
  3329. + len -= (1 << this->phys_erase_shift);
  3330. + page += pages_per_block;
  3331. +
  3332. + /* Check, if we cross a chip boundary */
  3333. + if (len && !(page & this->pagemask)) {
  3334. + chipnr++;
  3335. + this->select_chip(mtd, 0);
  3336. + this->select_chip(mtd, 0);
  3337. + }
  3338. + //sl2312_flash_delay();
  3339. + schedule();
  3340. + //cond_resched();
  3341. + }
  3342. + instr->state = MTD_ERASE_DONE;
  3343. +
  3344. +erase_exit:
  3345. + /* De-select the NAND device */
  3346. + this->select_chip(mtd, 0);
  3347. + spin_unlock_bh (&this->chip_lock);
  3348. +
  3349. + ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;;
  3350. + /* Do call back function */
  3351. + if (!ret && instr->callback)
  3352. + instr->callback (instr);
  3353. +
  3354. + /* The device is ready */
  3355. + spin_lock_bh (&this->chip_lock);
  3356. + this->state = FL_READY;
  3357. + spin_unlock_bh (&this->chip_lock);
  3358. + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
  3359. + /* Return more or less happy */
  3360. + return ret;
  3361. +}
  3362. +
  3363. +static void sl2312_nand_select_chip(struct mtd_info *mtd, int chip)
  3364. +{
  3365. + //struct nand_chip *this = mtd->priv;
  3366. +
  3367. + switch(chip) {
  3368. + case -1:
  3369. + CHIP_EN = NFLASH_CHIP0_EN;
  3370. + break;
  3371. + case 0:
  3372. + CHIP_EN = NFLASH_CHIP0_EN;
  3373. + break;
  3374. + case 1:
  3375. + CHIP_EN = NFLASH_CHIP1_EN;
  3376. + break;
  3377. + default:
  3378. + CHIP_EN = NFLASH_CHIP0_EN;
  3379. + break;
  3380. + }
  3381. +}
  3382. +
  3383. +/**
  3384. + * nand_default_block_markbad - [DEFAULT] mark a block bad
  3385. + * @mtd: MTD device structure
  3386. + * @ofs: offset from device start
  3387. + *
  3388. + * This is the default implementation, which can be overridden by
  3389. + * a hardware specific driver.
  3390. +*/
  3391. +static int sl2312_nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
  3392. +{
  3393. + struct nand_chip *this = mtd->priv;
  3394. + u_char buf[2] = {0, 0};
  3395. + size_t retlen;
  3396. + int block;
  3397. +
  3398. + /* Get block number */
  3399. + block = ((int) ofs) >> this->bbt_erase_shift;
  3400. + this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  3401. +
  3402. + /* Do we have a flash based bad block table ? */
  3403. + if (this->options & NAND_USE_FLASH_BBT)
  3404. + return nand_update_bbt (mtd, ofs);
  3405. +
  3406. + /* We write two bytes, so we dont have to mess with 16 bit access */
  3407. + ofs += mtd->oobsize + (this->badblockpos & ~0x01);
  3408. + return sl2312_nand_write_oob (mtd, ofs , 2, &retlen, buf);
  3409. +}
  3410. +
  3411. +/* Appropriate chip should already be selected */
  3412. +static int sl2312_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)//(struct mtd_info *mtd, unsigned long page, )
  3413. +{
  3414. + u_char *buf, *oobbuf;
  3415. + size_t retlen;
  3416. + unsigned long page, chipnr;
  3417. + struct nand_chip *this = mtd->priv;
  3418. +
  3419. + if (getchip) {
  3420. + page = (int)(ofs >> this->page_shift);
  3421. + chipnr = (int)(ofs >> this->chip_shift);
  3422. +
  3423. + /* Grab the lock and see if the device is available */
  3424. + sl2312_nand_get_chip (this, mtd, FL_READING, NULL);
  3425. + /* Select the NAND device */
  3426. + this->select_chip(mtd, chipnr);
  3427. + } else
  3428. + page = (int) ofs;
  3429. +
  3430. + buf = kmalloc (mtd->oobblock,GFP_KERNEL);
  3431. + oobbuf = kmalloc (mtd->oobsize,GFP_KERNEL);
  3432. +
  3433. + if ((!buf)||(!oobbuf)) {
  3434. + printk ("sl2312_nand_block_bad : Unable to allocate SL2312 NAND MTD device structure.\n");
  3435. +
  3436. + }
  3437. +
  3438. + sl2312_nand_read_ecc (mtd, page, mtd->oobblock , &retlen, buf, oobbuf, NULL);
  3439. +
  3440. +
  3441. + if(((mtd->oobblock < 528)&&(oobbuf[5] != 0xff))||((mtd->oobblock > 528)&&(oobbuf[0] != 0xff)))
  3442. + {
  3443. + kfree(buf);
  3444. + kfree(oobbuf);
  3445. + return 1;
  3446. + }
  3447. +
  3448. + kfree(buf);
  3449. + kfree(oobbuf);
  3450. + return 0;
  3451. +}
  3452. +
  3453. +/*
  3454. +* Use NAND read ECC
  3455. +*/
  3456. +static int sl2312_nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
  3457. +{
  3458. + return sl2312_nand_read_ecc (mtd, from, len, retlen, buf, NULL, NULL);
  3459. +}
  3460. +
  3461. +/*
  3462. + * NAND read with ECC
  3463. + */
  3464. +static int sl2312_nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
  3465. + size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel)
  3466. +{
  3467. + int j, col, page, opcode, i;
  3468. + int end=0;//, ecc=0;//, end_page=0;
  3469. + int erase_state = 0;
  3470. + int read = 0, oob = 0, ecc_failed = 0;//, ecc_status = 0
  3471. + struct nand_chip *this = mtd->priv;
  3472. + u_char *data_poi, *oob_data = oob_buf;
  3473. + //u_char ecc_calc[6];
  3474. + //u_char ecc_code[6];
  3475. + int eccmode;
  3476. + int *oob_config;
  3477. +
  3478. +
  3479. +
  3480. + // use chip default if zero
  3481. + if (oobsel == NULL)
  3482. + oobsel = &mtd->oobinfo;
  3483. +
  3484. + eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
  3485. + oob_config = oobsel->eccpos;
  3486. +
  3487. + DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  3488. +
  3489. + /* Do not allow reads past end of device */
  3490. + if ((from + len) > mtd->size) {
  3491. + DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n");
  3492. + *retlen = 0;
  3493. + return -EINVAL;
  3494. + }
  3495. +
  3496. + /* Grab the lock and see if the device is available */
  3497. + sl2312_nand_get_chip (this, mtd ,FL_READING, &erase_state);
  3498. +
  3499. + /* Select the NAND device */
  3500. + this->select_chip(mtd, 0);
  3501. +
  3502. + /* First we calculate the starting page */
  3503. + page = from >> this->page_shift;
  3504. +
  3505. + //end_page = mtd->oobblock + mtd->oobsize;
  3506. + end = mtd->oobblock;
  3507. + //ecc = mtd->eccsize;
  3508. + /* Get raw starting column */
  3509. + col = (from & (mtd->oobblock - 1));
  3510. +
  3511. +
  3512. + /* Send the read command */
  3513. + //this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page);
  3514. +
  3515. + /* Loop until all data read */
  3516. + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
  3517. + while (read < len) {
  3518. +
  3519. + //udelay(1200);
  3520. + /* If we have consequent page reads, apply delay or wait for ready/busy pin */
  3521. + if (read) {
  3522. + if (!this->dev_ready)
  3523. + udelay (this->chip_delay);
  3524. + else
  3525. + while (!this->dev_ready(mtd));
  3526. + }
  3527. +
  3528. + /*
  3529. + * If the read is not page aligned, we have to read into data buffer
  3530. + * due to ecc, else we read into return buffer direct
  3531. + */
  3532. + if (!col && (len - read) >= end)
  3533. + data_poi = &buf[read];
  3534. + else
  3535. + data_poi = this->data_buf;
  3536. +
  3537. + /* get oob area, if we have no oob buffer from fs-driver */
  3538. + if (!oob_buf) {
  3539. + oob_data = &this->data_buf[end];
  3540. + oob = 0;
  3541. + }
  3542. +
  3543. + j = 0;
  3544. + switch (eccmode) {
  3545. + case NAND_ECC_NONE: { /* No ECC, Read in a page */
  3546. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
  3547. + break;
  3548. + }
  3549. +
  3550. + case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */
  3551. + break;
  3552. +
  3553. + case NAND_ECC_HW3_256: /* Hardware ECC 3 byte /256 byte data: Read in first 256 byte, get ecc, */
  3554. + break;
  3555. +
  3556. + case NAND_ECC_HW3_512:
  3557. + case NAND_ECC_HW6_512: /* Hardware ECC 3/6 byte / 512 byte data : Read in a page */
  3558. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x80000001); //set 31b = 0
  3559. + break;
  3560. +
  3561. + default:
  3562. + printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
  3563. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0);
  3564. + //BUG();
  3565. + }//end switch
  3566. +
  3567. + for(i=0;i<end;i++)
  3568. + {
  3569. + //udelay(7);
  3570. + data_poi[i] = FLASH_READ_DATA(page*mtd->oobblock +i);
  3571. + }
  3572. + /* read oobdata */
  3573. + for (i = 0; i < mtd->oobsize; i++)
  3574. + {
  3575. + //udelay(7);
  3576. + oob_data[oob + i] = FLASH_READ_DATA(page*mtd->oobblock +end+i);
  3577. + }
  3578. +
  3579. + /* Skip ECC, if not active */
  3580. + if (eccmode == NAND_ECC_NONE)
  3581. + goto readdata;
  3582. +
  3583. + // compare ecc and correct data
  3584. +
  3585. + opcode=FLASH_READ_REG(NFLASH_ECC_STATUS);
  3586. + while(!(opcode&0x80000000)) //polling flash access 31b
  3587. + {
  3588. + opcode=FLASH_READ_REG(NFLASH_ECC_STATUS);
  3589. + //sl2312_flash_delay();
  3590. + schedule();
  3591. + }
  3592. + for(j=0;j<(end/512);j++)
  3593. + {//for 2k page
  3594. +
  3595. + opcode = 0x00000000|oob_data[mtd->oobsize-3-4*j]<<16|oob_data[mtd->oobsize-2-4*j]<<8|oob_data[mtd->oobsize-1-4*j];
  3596. +
  3597. + //opcode=FLASH_READ_REG(NFLASH_ECC_CODE_GEN0+(j*4));
  3598. +
  3599. + FLASH_WRITE_REG(NFLASH_ECC_OOB, opcode);
  3600. + opcode = 0x00000000|(j<<8); //select ECC code generation 0
  3601. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, opcode); //???
  3602. +
  3603. + opcode=FLASH_READ_REG(NFLASH_ECC_STATUS);
  3604. + if((opcode&0x00000003)==0x03)
  3605. + {
  3606. + printk (KERN_WARNING "\nPageRead Uncorrectable error !!\n");
  3607. + ecc_failed++;
  3608. + }
  3609. + else if((opcode&0x00000003)==0x01)
  3610. + {
  3611. + printk (KERN_WARNING "\nPageRead One bit data error !!");
  3612. + // correct data
  3613. + if((data_poi[(opcode&0xff80)>>7]>>((opcode&0x38)>>3))%1)
  3614. + data_poi[(opcode&0xff80)>>7] &= ~(1<<((opcode&0x38)>>3));
  3615. + else
  3616. + data_poi[(opcode&0xff80)>>7] |= (1<<((opcode&0x38)>>3));
  3617. +
  3618. + }
  3619. + else if((opcode&0x00000003)==0x02)
  3620. + {
  3621. + printk (KERN_WARNING "\nPageRead One bit ECC error !!\n");
  3622. + }
  3623. + else if((opcode&0x00000003)==0x00)
  3624. + {
  3625. +
  3626. + }
  3627. +
  3628. + }//for 2k page
  3629. +readdata:
  3630. + if (col || (len - read) < end) {
  3631. + for (j = col; j < end && read < len; j++)
  3632. + buf[read++] = data_poi[j];
  3633. + } else
  3634. + read += mtd->oobblock;
  3635. + /* For subsequent reads align to page boundary. */
  3636. + col = 0;
  3637. + /* Increment page address */
  3638. + page++;
  3639. + schedule();
  3640. + }
  3641. + /* De-select the NAND device */
  3642. + //this->select_chip(mtd, -1);
  3643. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
  3644. + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_INDIRECT);
  3645. + /* Wake up anyone waiting on the device */
  3646. + spin_lock_bh (&this->chip_lock);
  3647. + this->state = FL_READY;
  3648. + wake_up (&this->wq);
  3649. + spin_unlock_bh (&this->chip_lock);
  3650. +
  3651. + /*
  3652. + * Return success, if no ECC failures, else -EIO
  3653. + * fs driver will take care of that, because
  3654. + * retlen == desired len and result == -EIO
  3655. + */
  3656. + *retlen = read;
  3657. + return ecc_failed ? -EIO : 0;
  3658. +}
  3659. +
  3660. +/*
  3661. + * Wait for command done. This applies to erase and program only
  3662. + * Erase can take up to 400ms and program up to 20ms according to
  3663. + * general NAND and SmartMedia specs
  3664. + *
  3665. +*/
  3666. +static int sl2312_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this, int state)
  3667. +{
  3668. + unsigned long timeo = jiffies;
  3669. + int status, opcode;
  3670. +
  3671. + if (state == FL_ERASING)
  3672. + timeo += (HZ * 400) / 1000;
  3673. + else
  3674. + timeo += (HZ * 20) / 1000;
  3675. +
  3676. + spin_lock_bh (&this->chip_lock);
  3677. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000); //set 31b = 0
  3678. + FLASH_WRITE_REG(NFLASH_COUNT, 0x007f000070); //set only command no address and two data
  3679. +
  3680. + FLASH_WRITE_REG(NFLASH_CMD_ADDR, 0x00000070); //write read status command
  3681. +
  3682. +
  3683. + opcode = 0x80002000|DWIDTH|CHIP_EN; //set start bit & 8bits read command
  3684. + FLASH_WRITE_REG(NFLASH_ACCESS, opcode);
  3685. +
  3686. + while(opcode&0x80000000) //polling flash access 31b
  3687. + {
  3688. + opcode=FLASH_READ_REG(NFLASH_ACCESS);
  3689. + //sl2312_flash_delay();
  3690. + schedule();
  3691. + }
  3692. +
  3693. + while (time_before(jiffies, timeo)) {
  3694. + /* Check, if we were interrupted */
  3695. + if (this->state != state) {
  3696. + spin_unlock_bh (&this->chip_lock);
  3697. + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
  3698. + return 0;
  3699. + }
  3700. + if (this->dev_ready) {
  3701. + if (this->dev_ready(mtd))
  3702. + break;
  3703. + }
  3704. + if (FLASH_READ_REG(NFLASH_DATA) & 0x40)
  3705. + break;
  3706. +
  3707. + spin_unlock_bh (&this->chip_lock);
  3708. + yield ();
  3709. + spin_lock_bh (&this->chip_lock);
  3710. + }
  3711. + status = FLASH_READ_REG(NFLASH_DATA)&0xff;
  3712. + spin_unlock_bh (&this->chip_lock);
  3713. + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
  3714. + return status;
  3715. +}
  3716. +
  3717. +static int sl2312_nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf)
  3718. +{
  3719. + int i, col, page, j=0;
  3720. + //int erase_state = 0;
  3721. + struct nand_chip *this = mtd->priv;
  3722. + u_char *databuf, *oobbuf;
  3723. +
  3724. + databuf = &this->data_buf[0];
  3725. + oobbuf = &this->data_buf[mtd->oobblock];
  3726. + for (i = 0; i < mtd->oobsize; i++)
  3727. + oobbuf[i] = 0xff;
  3728. +
  3729. + DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len);
  3730. +
  3731. + /* Shift to get page */
  3732. + page = ((int) from) >> this->page_shift;
  3733. +
  3734. + /* Mask to get column */
  3735. + col = from & (mtd->oobsize-1); //0x0f;
  3736. +
  3737. + /* Initialize return length value */
  3738. + *retlen = 0;
  3739. + sl2312_nand_read_ecc (mtd, page, mtd->oobblock , retlen, databuf, oobbuf, NULL);
  3740. + for(i=col,j=0;i<mtd->oobsize||i<(col+len);i++,j++)
  3741. + buf[j] = oobbuf[i];
  3742. +
  3743. + *retlen = j ;
  3744. + return 0;
  3745. +}
  3746. +
  3747. +#define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0
  3748. +/*
  3749. +* Use NAND write ECC
  3750. +*/
  3751. +static int sl2312_nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
  3752. +{
  3753. + return (sl2312_nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL));
  3754. +}
  3755. +
  3756. +/*
  3757. + * NAND write with ECC
  3758. + */
  3759. +static int sl2312_nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
  3760. + size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel)
  3761. +{
  3762. + int page, ret = 0, oob = 0, written = 0;
  3763. + struct nand_chip *this = mtd->priv;
  3764. +
  3765. + DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  3766. +
  3767. +
  3768. + /* Do not allow write past end of device */
  3769. + if ((to + len) > mtd->size) {
  3770. + DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n");
  3771. + return -EINVAL;
  3772. + }
  3773. +
  3774. + /* reject writes, which are not page aligned */
  3775. + if (NOTALIGNED (to) || NOTALIGNED(len)) {
  3776. + printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
  3777. + return -EINVAL;
  3778. + }
  3779. +
  3780. + // if oobsel is NULL, use chip defaults
  3781. + if (oobsel == NULL)
  3782. + oobsel = &mtd->oobinfo;
  3783. +
  3784. + /* Shift to get page */
  3785. + page = ((int) to) >> this->page_shift;
  3786. +
  3787. + /* Grab the lock and see if the device is available */
  3788. + sl2312_nand_get_chip (this, mtd, FL_WRITING, NULL);
  3789. +
  3790. + /* Select the NAND device */
  3791. + this->select_chip(mtd, 0);
  3792. +
  3793. + /* Check the WP bit */
  3794. + if (!(sl2312_device_ready(mtd) & 0x80)) {
  3795. + DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Device is write protected!!!\n");
  3796. + ret = -EIO;
  3797. + goto out;
  3798. + }
  3799. +
  3800. + /* Loop until all data is written */
  3801. + while (written < len) {
  3802. + //udelay(100);
  3803. + int cnt = mtd->oobblock;
  3804. + this->data_poi = (u_char*) &buf[written];
  3805. + /* We use the same function for write and writev */
  3806. + if (eccbuf) {
  3807. + ret = sl2312_nand_write_page (mtd, this, page, &eccbuf[oob], oobsel);
  3808. + oob += mtd->oobsize;
  3809. + } else
  3810. + ret = sl2312_nand_write_page (mtd, this, page, NULL, oobsel);
  3811. +
  3812. + if (ret)
  3813. + goto out;
  3814. +
  3815. + /* Update written bytes count */
  3816. + written += cnt;
  3817. + /* Increment page address */
  3818. + page++;
  3819. + }
  3820. +
  3821. +out:
  3822. + /* De-select the NAND device */
  3823. + //this->select_chip(mtd, -1);
  3824. +
  3825. + /* Wake up anyone waiting on the device */
  3826. + spin_lock_bh (&this->chip_lock);
  3827. + this->state = FL_READY;
  3828. + wake_up (&this->wq);
  3829. + spin_unlock_bh (&this->chip_lock);
  3830. +
  3831. + *retlen = written;
  3832. + return ret;
  3833. +}
  3834. +
  3835. +/*
  3836. + * Nand_page_program function is used for write and writev !
  3837. + * This function will always program a full page of data
  3838. + * If you call it with a non page aligned buffer, you're lost :)
  3839. + */
  3840. +static int sl2312_nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf, struct nand_oobinfo *oobsel)
  3841. +{
  3842. + int i, j, status, opcode;
  3843. + u_char ecc_code[16], *oob_data;
  3844. + int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE;
  3845. + //int *oob_config = oobsel->eccpos;
  3846. +
  3847. + /* pad oob area, if we have no oob buffer from fs-driver */
  3848. + if (!oob_buf) {
  3849. + oob_data = &this->data_buf[mtd->oobblock];
  3850. + for (i = 0; i < mtd->oobsize; i++)
  3851. + oob_data[i] = 0xff;
  3852. + } else
  3853. + oob_data = oob_buf;
  3854. +
  3855. + /* Send command to begin auto page programming */
  3856. +
  3857. + memset(oob_data,0xff,mtd->oobsize);
  3858. + /* Write out complete page of data, take care of eccmode */
  3859. + switch (eccmode) {
  3860. + /* No ecc and software ecc 3/256, write all */
  3861. + case NAND_ECC_NONE:
  3862. + printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n");
  3863. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
  3864. + break;
  3865. + case NAND_ECC_SOFT:
  3866. + break;
  3867. +
  3868. + /* Hardware ecc 3 byte / 256 data, write first half, get ecc, then second, if 512 byte pagesize */
  3869. + case NAND_ECC_HW3_256:
  3870. + break;
  3871. +
  3872. + /* Hardware ecc 3 byte / 512 byte data, write full page */
  3873. + case NAND_ECC_HW3_512:
  3874. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x80000001); //set 31b = 0
  3875. +
  3876. + /* Hardware ecc 6 byte / 512 byte data, write full page */
  3877. + case NAND_ECC_HW6_512:
  3878. + break;
  3879. +
  3880. + default:
  3881. + printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
  3882. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
  3883. + //BUG();
  3884. + }
  3885. +
  3886. + FLASH_WRITE_REG(NFLASH_ACCESS, NFLASH_DIRECT);
  3887. +
  3888. + for(i=0;i<mtd->oobblock;i++)
  3889. + {
  3890. + //udelay(5);
  3891. + FLASH_WRITE_DATA((page*mtd->oobblock)+i,this->data_poi[i]);
  3892. + }
  3893. + ///////////////
  3894. + if(eccmode!=NAND_ECC_NONE)
  3895. + {
  3896. + opcode=FLASH_READ_REG(NFLASH_ECC_STATUS);
  3897. + while(!(opcode&0x80000000)) //polling flash access 31b
  3898. + {
  3899. + opcode=FLASH_READ_REG(NFLASH_ECC_STATUS);
  3900. + //sl2312_flash_delay();
  3901. + schedule();
  3902. + }
  3903. +
  3904. +
  3905. + for(i=0;i<(mtd->oobblock/512);i++)
  3906. + {
  3907. + opcode=FLASH_READ_REG(NFLASH_ECC_CODE_GEN0+(i*4));
  3908. +
  3909. + for(j=3;j>0;j--)
  3910. + oob_data[(mtd->oobsize-j-(i*4))] = (opcode<<((4-j)*8)) >>24;
  3911. +
  3912. + for(j=0;j<4;j++)
  3913. + {
  3914. + ecc_code[15-i*4] = opcode;
  3915. + ecc_code[15-i*4-1] = opcode>>8;
  3916. + ecc_code[15-i*4-2] = opcode>>16;
  3917. + }
  3918. + }
  3919. +
  3920. + //disable ecc
  3921. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x00000000);
  3922. +
  3923. + /* Write out OOB data */
  3924. + for(i=0;i<mtd->oobsize;i++)
  3925. + {
  3926. + //udelay(5);
  3927. + FLASH_WRITE_DATA((page*mtd->oobblock)+mtd->oobblock+i,oob_data[i]);
  3928. + }
  3929. + }
  3930. + else
  3931. + {
  3932. + for(i=0;i<mtd->oobsize;i++)
  3933. + {
  3934. + //udelay(5);
  3935. + FLASH_WRITE_DATA((page*mtd->oobblock)+mtd->oobblock+i,0xff);
  3936. + }
  3937. + }
  3938. +
  3939. +
  3940. + /* call wait ready function */
  3941. + status = this->waitfunc (mtd, this, FL_WRITING);
  3942. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
  3943. + /* See if device thinks it succeeded */
  3944. + if (status & 0x01) {
  3945. + DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page);
  3946. + FLASH_WRITE_REG(NFLASH_ECC_CONTROL, 0x0); //set 31b = 0
  3947. + return -EIO;
  3948. + }
  3949. +
  3950. +#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  3951. + /*
  3952. + * The NAND device assumes that it is always writing to
  3953. + * a cleanly erased page. Hence, it performs its internal
  3954. + * write verification only on bits that transitioned from
  3955. + * 1 to 0. The device does NOT verify the whole page on a
  3956. + * byte by byte basis. It is possible that the page was
  3957. + * not completely erased or the page is becoming unusable
  3958. + * due to wear. The read with ECC would catch the error
  3959. + * later when the ECC page check fails, but we would rather
  3960. + * catch it early in the page write stage. Better to write
  3961. + * no data than invalid data.
  3962. + */
  3963. +
  3964. + /* Send command to read back the page */
  3965. + this->cmdfunc (mtd, NAND_CMD_READ0, 0, page);
  3966. + /* Loop through and verify the data */
  3967. + if (this->verify_buf(mtd, this->data_poi, mtd->oobblock)) {
  3968. + DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
  3969. + return -EIO;
  3970. + }
  3971. +
  3972. + /* check, if we have a fs-supplied oob-buffer */
  3973. + if (oob_buf) {
  3974. + if (this->verify_buf(mtd, oob_data, mtd->oobsize)) {
  3975. + DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page);
  3976. + return -EIO;
  3977. + }
  3978. + } else {
  3979. + if (eccmode != NAND_ECC_NONE) {
  3980. + int ecc_bytes = 0;
  3981. +
  3982. + switch (this->eccmode) {
  3983. + case NAND_ECC_SOFT:
  3984. + case NAND_ECC_HW3_256: ecc_bytes = (mtd->oobblock == 512) ? 6 : 3; break;
  3985. + case NAND_ECC_HW3_512: ecc_bytes = 3; break;
  3986. + case NAND_ECC_HW6_512: ecc_bytes = 6; break;
  3987. + }
  3988. +
  3989. +
  3990. +
  3991. + for(i=0;i < (mtd->oobblock+mtd->oobsize);i++)
  3992. + {
  3993. + if(i>=mtd->oobblock)
  3994. + oob_data[i-mtd->oobblock] = FLASH_READ_DATA((page*mtd->oobblock) +i);
  3995. + else
  3996. + oob_data[0] = FLASH_READ_DATA((page*mtd->oobblock) +i);
  3997. + }
  3998. +
  3999. + if(this->eccmode == NAND_ECC_HW3_512)
  4000. + {
  4001. + for(i=0;i<(mtd->oobblock/512);i++)
  4002. + {
  4003. + for(j=0;j<3;j++)
  4004. + {
  4005. + if (oob_data[mtd->oobsize-1-j-4*i] != ecc_code[15-j-4*i]) {
  4006. + DEBUG (MTD_DEBUG_LEVEL0,
  4007. + "%s: Failed ECC write "
  4008. + "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
  4009. + return -EIO;
  4010. + }
  4011. + }
  4012. + }
  4013. + }
  4014. + }//eccmode != NAND_ECC_NONE
  4015. + }
  4016. + /*
  4017. + * Terminate the read command. This is faster than sending a reset command or
  4018. + * applying a 20us delay before issuing the next programm sequence.
  4019. + * This is not a problem for all chips, but I have found a bunch of them.
  4020. + */
  4021. + //this->select_chip(mtd, -1);
  4022. + //this->select_chip(mtd, 0);
  4023. +#endif
  4024. +
  4025. + return 0;
  4026. +}
  4027. +
  4028. +/*
  4029. + * NAND write with iovec
  4030. + */
  4031. +static int sl2312_nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
  4032. + loff_t to, size_t * retlen)
  4033. +{
  4034. + return (sl2312_nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, 0));
  4035. +}
  4036. +
  4037. +static int sl2312_nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count,
  4038. + loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel)
  4039. +{
  4040. + int i, page, len, total_len, ret = 0, written = 0;
  4041. + struct nand_chip *this = mtd->priv;
  4042. +
  4043. + /* Calculate total length of data */
  4044. + total_len = 0;
  4045. + for (i = 0; i < count; i++)
  4046. + total_len += (int) vecs[i].iov_len;
  4047. +
  4048. + DEBUG (MTD_DEBUG_LEVEL3,
  4049. + "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count);
  4050. +
  4051. + /* Do not allow write past end of page */
  4052. + if ((to + total_len) > mtd->size) {
  4053. + DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n");
  4054. + return -EINVAL;
  4055. + }
  4056. +
  4057. + /* reject writes, which are not page aligned */
  4058. + if (NOTALIGNED (to) || NOTALIGNED(total_len)) {
  4059. + printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n");
  4060. + return -EINVAL;
  4061. + }
  4062. +
  4063. + // if oobsel is NULL, use chip defaults
  4064. + if (oobsel == NULL)
  4065. + oobsel = &mtd->oobinfo;
  4066. +
  4067. + /* Shift to get page */
  4068. + page = ((int) to) >> this->page_shift;
  4069. +
  4070. + /* Grab the lock and see if the device is available */
  4071. + sl2312_nand_get_chip (this, mtd, FL_WRITING, NULL);
  4072. +
  4073. + /* Select the NAND device */
  4074. + this->select_chip(mtd, 0);
  4075. +
  4076. + /* Check the WP bit */
  4077. + if (!(sl2312_device_ready(mtd) & 0x80)) {
  4078. + DEBUG (MTD_DEBUG_LEVEL0, "sl2312_nand_writev_ecc: Device is write protected!!!\n");
  4079. + ret = -EIO;
  4080. + goto out;
  4081. + }
  4082. +
  4083. + /* Loop until all iovecs' data has been written */
  4084. + len = 0;
  4085. + while (count) {
  4086. + /*
  4087. + * Check, if the tuple gives us not enough data for a
  4088. + * full page write. Then we can use the iov direct,
  4089. + * else we have to copy into data_buf.
  4090. + */
  4091. + if ((vecs->iov_len - len) >= mtd->oobblock) {
  4092. + this->data_poi = (u_char *) vecs->iov_base;
  4093. + this->data_poi += len;
  4094. + len += mtd->oobblock;
  4095. + /* Check, if we have to switch to the next tuple */
  4096. + if (len >= (int) vecs->iov_len) {
  4097. + vecs++;
  4098. + len = 0;
  4099. + count--;
  4100. + }
  4101. + } else {
  4102. + /*
  4103. + * Read data out of each tuple until we have a full page
  4104. + * to write or we've read all the tuples.
  4105. + */
  4106. + int cnt = 0;
  4107. + while ((cnt < mtd->oobblock) && count) {
  4108. + if (vecs->iov_base != NULL && vecs->iov_len) {
  4109. + this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++];
  4110. + }
  4111. + /* Check, if we have to switch to the next tuple */
  4112. + if (len >= (int) vecs->iov_len) {
  4113. + vecs++;
  4114. + len = 0;
  4115. + count--;
  4116. + }
  4117. + }
  4118. + this->data_poi = this->data_buf;
  4119. + }
  4120. +
  4121. + /* We use the same function for write and writev !) */
  4122. + ret = sl2312_nand_write_page (mtd, this, page, NULL, oobsel);
  4123. + if (ret)
  4124. + goto out;
  4125. +
  4126. + /* Update written bytes count */
  4127. + written += mtd->oobblock;;
  4128. +
  4129. + /* Increment page address */
  4130. + page++;
  4131. + }
  4132. +
  4133. +out:
  4134. + /* De-select the NAND device */
  4135. + //this->select_chip(mtd, -1);
  4136. +
  4137. + /* Wake up anyone waiting on the device */
  4138. + spin_lock_bh (&this->chip_lock);
  4139. + this->state = FL_READY;
  4140. + wake_up (&this->wq);
  4141. + spin_unlock_bh (&this->chip_lock);
  4142. +
  4143. + *retlen = written;
  4144. + return ret;
  4145. +}
  4146. +
  4147. +/*
  4148. +static u_char ffchars[] = {
  4149. + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  4150. + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  4151. +};
  4152. +*/
  4153. +/*
  4154. + * NAND write out-of-band
  4155. + */
  4156. +static int sl2312_nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf)
  4157. +{
  4158. + int column, page, status, ret = 0, j=0;
  4159. + struct nand_chip *this = mtd->priv;
  4160. + u_char *databuf, *oobbuf;
  4161. +
  4162. +
  4163. + databuf = &this->data_buf[0];
  4164. + oobbuf = &this->data_buf[mtd->oobblock];
  4165. + for (j = 0; j < mtd->oobsize; j++)
  4166. + oobbuf[j] = 0xff;
  4167. +//#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  4168. +// int i;
  4169. +//#endif
  4170. +
  4171. + DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len);
  4172. +
  4173. + /* Shift to get page */
  4174. + page = ((int) to) >> this->page_shift;
  4175. +
  4176. + /* Mask to get column */
  4177. + column = to & 0x1f;
  4178. +
  4179. + /* Initialize return length value */
  4180. + *retlen = 0;
  4181. +
  4182. + /* Do not allow write past end of page */
  4183. + if ((column + len) > mtd->oobsize) {
  4184. + DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n");
  4185. + return -EINVAL;
  4186. + }
  4187. +
  4188. + /* Grab the lock and see if the device is available */
  4189. + sl2312_nand_get_chip (this, mtd, FL_WRITING, NULL);
  4190. +
  4191. + /* Select the NAND device */
  4192. + this->select_chip(mtd, 0);
  4193. +
  4194. + /* Reset the chip. Some chips (like the Toshiba TC5832DC found
  4195. + in one of my DiskOnChip 2000 test units) will clear the whole
  4196. + data page too if we don't do this. I have no clue why, but
  4197. + I seem to have 'fixed' it in the doc2000 driver in
  4198. + August 1999. dwmw2. */
  4199. + this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  4200. +
  4201. + /* Check the WP bit */
  4202. + if (!(sl2312_device_ready(mtd) & 0x80)) {
  4203. + DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Device is write protected!!!\n");
  4204. + ret = -EIO;
  4205. + goto out;
  4206. + }
  4207. + /* Write out desired data */
  4208. + this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page);
  4209. +
  4210. + sl2312_nand_read_ecc (mtd, page, mtd->oobblock , retlen, databuf, oobbuf, NULL);
  4211. +
  4212. + for(j=column;j<(column+len);j++)
  4213. + oobbuf[j] = buf[j-column];
  4214. + sl2312_nand_write_ecc (mtd, page, mtd->oobblock, retlen, databuf, oobbuf, NULL);
  4215. +
  4216. + status = this->waitfunc (mtd, this, FL_WRITING);
  4217. +
  4218. + /* See if device thinks it succeeded */
  4219. + if (status & 0x01) {
  4220. + DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page);
  4221. + ret = -EIO;
  4222. + goto out;
  4223. + }
  4224. + /* Return happy */
  4225. + *retlen = len;
  4226. +
  4227. +
  4228. +out:
  4229. + /* De-select the NAND device */
  4230. + //this->select_chip(mtd, -1);
  4231. +
  4232. + /* Wake up anyone waiting on the device */
  4233. + spin_lock_bh (&this->chip_lock);
  4234. + this->state = FL_READY;
  4235. + wake_up (&this->wq);
  4236. + spin_unlock_bh (&this->chip_lock);
  4237. +
  4238. + return ret;
  4239. +}
  4240. +
  4241. +/*
  4242. + * NAND sync
  4243. + */
  4244. +static void sl2312_nand_sync (struct mtd_info *mtd)
  4245. +{
  4246. + struct nand_chip *this = mtd->priv;
  4247. + DECLARE_WAITQUEUE (wait, current);
  4248. +
  4249. + DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
  4250. +
  4251. +retry:
  4252. + /* Grab the spinlock */
  4253. + spin_lock_bh (&this->chip_lock);
  4254. +
  4255. + /* See what's going on */
  4256. + switch (this->state) {
  4257. + case FL_READY:
  4258. + case FL_SYNCING:
  4259. + this->state = FL_SYNCING;
  4260. + spin_unlock_bh (&this->chip_lock);
  4261. + break;
  4262. +
  4263. + default:
  4264. + /* Not an idle state */
  4265. + add_wait_queue (&this->wq, &wait);
  4266. + spin_unlock_bh (&this->chip_lock);
  4267. + schedule ();
  4268. +
  4269. + remove_wait_queue (&this->wq, &wait);
  4270. + goto retry;
  4271. + }
  4272. +
  4273. + /* Lock the device */
  4274. + spin_lock_bh (&this->chip_lock);
  4275. +
  4276. + /* Set the device to be ready again */
  4277. + if (this->state == FL_SYNCING) {
  4278. + this->state = FL_READY;
  4279. + wake_up (&this->wq);
  4280. + }
  4281. +
  4282. + /* Unlock the device */
  4283. + spin_unlock_bh (&this->chip_lock);
  4284. +}
  4285. +
  4286. +
  4287. +/*
  4288. + * Scan for the NAND device
  4289. + */
  4290. +int sl2312_nand_scan (struct mtd_info *mtd, int maxchips)
  4291. +{
  4292. + int i, j, nand_maf_id, nand_dev_id, busw;
  4293. + struct nand_chip *this = mtd->priv;
  4294. + unsigned char id[4];
  4295. +
  4296. + /* Get buswidth to select the correct functions*/
  4297. + busw = this->options & NAND_BUSWIDTH_16;
  4298. +
  4299. + /* check for proper chip_delay setup, set 20us if not */
  4300. + if (!this->chip_delay)
  4301. + this->chip_delay = 20;
  4302. +
  4303. + /* check, if a user supplied command function given */
  4304. + if (this->cmdfunc == NULL)
  4305. + this->cmdfunc = sl2312_nand_command;
  4306. +
  4307. + /* check, if a user supplied wait function given */
  4308. + if (this->waitfunc == NULL)
  4309. + this->waitfunc = sl2312_nand_waitfunc;
  4310. +
  4311. + if (!this->select_chip)
  4312. + this->select_chip = sl2312_nand_select_chip;
  4313. + if (!this->write_byte)
  4314. + this->write_byte = sl2312_nand_write_byte; //busw ? nand_write_byte16 : nand_write_byte;
  4315. + if (!this->read_byte)
  4316. + this->read_byte = sl2312_nand_read_byte; //busw ? nand_read_byte16 : nand_read_byte;
  4317. +// if (!this->write_word)
  4318. +// this->write_word = nand_write_word;
  4319. +// if (!this->read_word)
  4320. +// this->read_word = nand_read_word;
  4321. +// if (!this->block_bad)
  4322. + this->block_bad = sl2312_nand_block_bad; //nand_block_bad;
  4323. + if (!this->block_markbad)
  4324. + this->block_markbad = sl2312_nand_default_block_markbad;
  4325. + if (!this->write_buf)
  4326. + this->write_buf = sl2312_nand_write_buf; //busw ? nand_write_buf16 : nand_write_buf;
  4327. + if (!this->read_buf)
  4328. + this->read_buf = sl2312_nand_read_buf; //busw ? nand_read_buf16 : nand_read_buf;
  4329. + if (!this->verify_buf)
  4330. + this->verify_buf = sl2312_nand_verify_buf; //busw ? nand_verify_buf16 : nand_verify_buf;
  4331. + if (!this->scan_bbt)
  4332. + this->scan_bbt = sl2312_nand_scan_bbt;
  4333. +
  4334. + /* Select the device */
  4335. + this->select_chip(mtd, 0);
  4336. +
  4337. + /* Read manufacturer and device IDs */
  4338. + nand_read_id(0,id);
  4339. +
  4340. + nand_maf_id = id[0];
  4341. + nand_dev_id = id[1];
  4342. +
  4343. + /* Print and store flash device information */
  4344. + for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  4345. +
  4346. + if (nand_dev_id != nand_flash_ids[i].id)
  4347. + continue;
  4348. +
  4349. + if (!mtd->name) mtd->name = nand_flash_ids[i].name;
  4350. + this->chipsize = nand_flash_ids[i].chipsize << 20;
  4351. +
  4352. + /* New devices have all the information in additional id bytes */
  4353. + if (!nand_flash_ids[i].pagesize) {
  4354. + int extid;
  4355. +
  4356. + /* The 4th id byte is the important one */
  4357. + extid = id[3];
  4358. + /* Calc pagesize */
  4359. + mtd->oobblock = 1024 << (extid & 0x3);
  4360. + extid >>= 2;
  4361. + /* Calc oobsize */
  4362. + mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512);
  4363. + extid >>= 2;
  4364. + /* Calc blocksize. Blocksize is multiples of 64KiB */
  4365. + mtd->erasesize = (64 * 1024) << (extid & 0x03);
  4366. + extid >>= 2;
  4367. + /* Get buswidth information */
  4368. + busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  4369. +
  4370. + } else {
  4371. + /* Old devices have this data hardcoded in the
  4372. + * device id table */
  4373. + mtd->erasesize = nand_flash_ids[i].erasesize;
  4374. + mtd->oobblock = nand_flash_ids[i].pagesize;
  4375. + mtd->oobsize = mtd->oobblock / 32;
  4376. + busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16;
  4377. + }
  4378. +
  4379. + /* Check, if buswidth is correct. Hardware drivers should set
  4380. + * this correct ! */
  4381. + if (busw != (this->options & NAND_BUSWIDTH_16)) {
  4382. + printk (KERN_INFO "NAND device: Manufacturer ID:"
  4383. + " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
  4384. + nand_manuf_ids[i].name , mtd->name);
  4385. + printk (KERN_WARNING
  4386. + "NAND bus width %d instead %d bit\n",
  4387. + (this->options & NAND_BUSWIDTH_16) ? 16 : 8,
  4388. + busw ? 16 : 8);
  4389. + this->select_chip(mtd, -1);
  4390. + return 1;
  4391. + }
  4392. +
  4393. + /* Calculate the address shift from the page size */
  4394. + this->page_shift = ffs(mtd->oobblock) - 1;
  4395. + this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1;
  4396. + this->chip_shift = ffs(this->chipsize) - 1;
  4397. +
  4398. + /* Set the bad block position */
  4399. + this->badblockpos = mtd->oobblock > 512 ?
  4400. + NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
  4401. +
  4402. + /* Get chip options, preserve non chip based options */
  4403. + this->options &= ~NAND_CHIPOPTIONS_MSK;
  4404. + this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK;
  4405. + /* Set this as a default. Board drivers can override it, if neccecary */
  4406. + this->options |= NAND_NO_AUTOINCR;
  4407. + /* Check if this is a not a samsung device. Do not clear the options
  4408. + * for chips which are not having an extended id.
  4409. + */
  4410. + if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize)
  4411. + this->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  4412. +
  4413. + /* Check for AND chips with 4 page planes */
  4414. + // if (this->options & NAND_4PAGE_ARRAY)
  4415. + // this->erase_cmd = multi_erase_cmd;
  4416. + // else
  4417. + // this->erase_cmd = single_erase_cmd;
  4418. +
  4419. + /* Do not replace user supplied command function ! */
  4420. + // if (mtd->oobblock > 512 && this->cmdfunc == nand_command)
  4421. + // this->cmdfunc = nand_command_lp;
  4422. +
  4423. + /* Try to identify manufacturer */
  4424. + for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
  4425. + if (nand_manuf_ids[j].id == nand_maf_id)
  4426. + break;
  4427. + }
  4428. + printk (KERN_INFO "NAND device: Manufacturer ID:"
  4429. + " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id,
  4430. + nand_manuf_ids[j].name , nand_flash_ids[i].name);
  4431. + break;
  4432. + }
  4433. + /////////////////////////////
  4434. +
  4435. + for (i=1; i < maxchips; i++) {
  4436. + this->select_chip(mtd, i);
  4437. +
  4438. + /* Send the command for reading device ID */
  4439. + nand_read_id(1,id);
  4440. +
  4441. + /* Read manufacturer and device IDs */
  4442. + if (nand_maf_id != id[0] ||
  4443. + nand_dev_id != id[1])
  4444. + break;
  4445. + }
  4446. + if (i > 1)
  4447. + printk(KERN_INFO "%d NAND chips detected\n", i);
  4448. +
  4449. + /* Allocate buffers, if neccecary */
  4450. + if (!this->oob_buf) {
  4451. + size_t len;
  4452. + len = mtd->oobsize << (this->phys_erase_shift - this->page_shift);
  4453. + this->oob_buf = kmalloc (len, GFP_KERNEL);
  4454. + if (!this->oob_buf) {
  4455. + printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n");
  4456. + return -ENOMEM;
  4457. + }
  4458. + this->options |= NAND_OOBBUF_ALLOC;
  4459. + }
  4460. +
  4461. + if (!this->data_buf) {
  4462. + size_t len;
  4463. + len = mtd->oobblock + mtd->oobsize;
  4464. + this->data_buf = kmalloc (len, GFP_KERNEL);
  4465. + if (!this->data_buf) {
  4466. + if (this->options & NAND_OOBBUF_ALLOC)
  4467. + kfree (this->oob_buf);
  4468. + printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n");
  4469. + return -ENOMEM;
  4470. + }
  4471. + this->options |= NAND_DATABUF_ALLOC;
  4472. + }
  4473. +
  4474. + /* Store the number of chips and calc total size for mtd */
  4475. + this->numchips = i;
  4476. + mtd->size = i * this->chipsize;
  4477. + /* Convert chipsize to number of pages per chip -1. */
  4478. + this->pagemask = (this->chipsize >> this->page_shift) - 1;
  4479. + /* Preset the internal oob buffer */
  4480. + memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift));
  4481. +
  4482. + /* If no default placement scheme is given, select an
  4483. + * appropriate one */
  4484. + if (!this->autooob) {
  4485. + /* Select the appropriate default oob placement scheme for
  4486. + * placement agnostic filesystems */
  4487. + switch (mtd->oobsize) {
  4488. + case 8:
  4489. + this->autooob = &nand_oob_8;
  4490. + break;
  4491. + case 16:
  4492. + this->autooob = &nand_oob_16;
  4493. + break;
  4494. + case 64:
  4495. + this->autooob = &nand_oob_64;
  4496. + break;
  4497. + default:
  4498. + printk (KERN_WARNING "No oob scheme defined for oobsize %d\n",
  4499. + mtd->oobsize);
  4500. + BUG();
  4501. + }
  4502. + }
  4503. +
  4504. + /* The number of bytes available for the filesystem to place fs dependend
  4505. + * oob data */
  4506. + if (this->options & NAND_BUSWIDTH_16) {
  4507. + mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 2);
  4508. + if (this->autooob->eccbytes & 0x01)
  4509. + mtd->oobavail--;
  4510. + } else
  4511. + mtd->oobavail = mtd->oobsize - (this->autooob->eccbytes + 1);
  4512. +
  4513. +
  4514. + /*
  4515. + * check ECC mode, default to software
  4516. + * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize
  4517. + * fallback to software ECC
  4518. + */
  4519. + this->eccsize = 256; /* set default eccsize */
  4520. + this->eccbytes = 3;
  4521. +
  4522. + switch (this->eccmode) {
  4523. + case NAND_ECC_HW12_2048:
  4524. + if (mtd->oobblock < 2048) {
  4525. + printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
  4526. + mtd->oobblock);
  4527. + this->eccmode = NAND_ECC_SOFT;
  4528. + this->calculate_ecc = nand_calculate_ecc;
  4529. + this->correct_data = nand_correct_data;
  4530. + } else
  4531. + this->eccsize = 2048;
  4532. + break;
  4533. +
  4534. + case NAND_ECC_HW3_512:
  4535. + case NAND_ECC_HW6_512:
  4536. + case NAND_ECC_HW8_512:
  4537. + if (mtd->oobblock == 256) {
  4538. + printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n");
  4539. + this->eccmode = NAND_ECC_SOFT;
  4540. + this->calculate_ecc = nand_calculate_ecc;
  4541. + this->correct_data = nand_correct_data;
  4542. + } else
  4543. + this->eccsize = 512; /* set eccsize to 512 */
  4544. + break;
  4545. +
  4546. + case NAND_ECC_HW3_256:
  4547. + break;
  4548. +
  4549. + case NAND_ECC_NONE:
  4550. + printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n");
  4551. + this->eccmode = NAND_ECC_NONE;
  4552. + break;
  4553. +
  4554. + case NAND_ECC_SOFT:
  4555. + this->calculate_ecc = nand_calculate_ecc;
  4556. + this->correct_data = nand_correct_data;
  4557. + break;
  4558. +
  4559. + default:
  4560. + printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode);
  4561. + BUG();
  4562. + }
  4563. +
  4564. + /* Check hardware ecc function availability and adjust number of ecc bytes per
  4565. + * calculation step
  4566. + */
  4567. + switch (this->eccmode) {
  4568. + case NAND_ECC_HW12_2048:
  4569. + this->eccbytes += 4;
  4570. + case NAND_ECC_HW8_512:
  4571. + this->eccbytes += 2;
  4572. + case NAND_ECC_HW6_512:
  4573. + this->eccbytes += 3;
  4574. +// case NAND_ECC_HW3_512:
  4575. + case NAND_ECC_HW3_256:
  4576. + if (this->calculate_ecc && this->correct_data && this->enable_hwecc)
  4577. + break;
  4578. + printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n");
  4579. + BUG();
  4580. + }
  4581. +
  4582. + mtd->eccsize = this->eccsize;
  4583. +
  4584. + /* Set the number of read / write steps for one page to ensure ECC generation */
  4585. + switch (this->eccmode) {
  4586. + case NAND_ECC_HW12_2048:
  4587. + this->eccsteps = mtd->oobblock / 2048;
  4588. + break;
  4589. + case NAND_ECC_HW3_512:
  4590. + case NAND_ECC_HW6_512:
  4591. + case NAND_ECC_HW8_512:
  4592. + this->eccsteps = mtd->oobblock / 512;
  4593. + break;
  4594. + case NAND_ECC_HW3_256:
  4595. + case NAND_ECC_SOFT:
  4596. + this->eccsteps = mtd->oobblock / 256;
  4597. + break;
  4598. +
  4599. + case NAND_ECC_NONE:
  4600. + this->eccsteps = 1;
  4601. + break;
  4602. + }
  4603. +
  4604. + /* Initialize state, waitqueue and spinlock */
  4605. + this->state = FL_READY;
  4606. + init_waitqueue_head (&this->wq);
  4607. + spin_lock_init (&this->chip_lock);
  4608. +
  4609. + /* De-select the device */
  4610. + this->select_chip(mtd, 0);
  4611. +
  4612. + /* Print warning message for no device */
  4613. + if (!mtd->size) {
  4614. + printk (KERN_WARNING "No NAND device found!!!\n");
  4615. + return 1;
  4616. + }
  4617. +
  4618. + /* Fill in remaining MTD driver data */
  4619. + mtd->type = MTD_NANDFLASH;
  4620. + mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC;
  4621. + mtd->ecctype = MTD_ECC_SW;
  4622. + mtd->erase = sl2312_nand_erase;
  4623. + mtd->point = NULL;
  4624. + mtd->unpoint = NULL;
  4625. + mtd->read = sl2312_nand_read;
  4626. + mtd->write = sl2312_nand_write;
  4627. + mtd->read_ecc = sl2312_nand_read_ecc;
  4628. + mtd->write_ecc = sl2312_nand_write_ecc;
  4629. + mtd->read_oob = sl2312_nand_read_oob;
  4630. + mtd->write_oob = sl2312_nand_write_oob;
  4631. + mtd->readv = NULL;
  4632. + mtd->writev = sl2312_nand_writev;
  4633. + mtd->writev_ecc = sl2312_nand_writev_ecc;
  4634. + mtd->sync = sl2312_nand_sync;
  4635. + mtd->lock = NULL;
  4636. + mtd->unlock = NULL;
  4637. + mtd->suspend = NULL;
  4638. + mtd->resume = NULL;
  4639. + mtd->block_isbad = sl2312_nand_block_isbad;
  4640. + mtd->block_markbad = sl2312_nand_block_markbad;
  4641. +
  4642. + /* and make the autooob the default one */
  4643. + memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo));
  4644. +
  4645. + mtd->owner = THIS_MODULE;
  4646. +
  4647. + /* Build bad block table */
  4648. + return this->scan_bbt (mtd);
  4649. +}
  4650. +
  4651. +/*End Add function*/
  4652. +
  4653. +/*
  4654. + * Main initialization routine
  4655. + */
  4656. +extern int nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc);
  4657. +
  4658. +int __init sl2312_mtd_init (void)
  4659. +{
  4660. + struct nand_chip *this;
  4661. + int err = 0;
  4662. + struct mtd_partition *parts;
  4663. + int nr_parts = 0;
  4664. + int ret, data, *base;
  4665. +
  4666. + printk("NAND MTD Driver Start Init ......\n");
  4667. +
  4668. + base = (unsigned int *)(IO_ADDRESS(SL2312_GLOBAL_BASE) + 0x30);
  4669. + data = *base;
  4670. + data&=0xffffffeb;
  4671. + data|=0x3; //disable p & s flash
  4672. + *base = data;
  4673. +
  4674. + /* Allocate memory for MTD device structure and private data */
  4675. + sl2312_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
  4676. + if (!sl2312_mtd) {
  4677. + printk ("Unable to allocate SL2312 NAND MTD device structure.\n");
  4678. + err = -ENOMEM;
  4679. + goto out;
  4680. + }
  4681. +
  4682. + // sl2312_device_setup();
  4683. +
  4684. + /* io is indirect via a register so don't need to ioremap address */
  4685. +
  4686. + /* Get pointer to private data */
  4687. + this = (struct nand_chip *) (&sl2312_mtd[1]);
  4688. +
  4689. + /* Initialize structures */
  4690. + memset((char *) sl2312_mtd, 0, sizeof(struct mtd_info));
  4691. + memset((char *) this, 0, sizeof(struct nand_chip));
  4692. +
  4693. + /* Link the private data with the MTD structure */
  4694. + sl2312_mtd->priv = this;
  4695. + sl2312_mtd->name = "sl2312-nand";
  4696. +
  4697. + /* Set address of NAND IO lines */
  4698. + this->IO_ADDR_R = (void __iomem *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE+NFLASH_DATA)); //(unsigned long)&(sl2312_ndfmcptr->dtr);
  4699. + this->IO_ADDR_W = (void __iomem *)IO_ADDRESS((SL2312_FLASH_CTRL_BASE+NFLASH_DATA)); //(unsigned long)&(sl2312_ndfmcptr->dtr);
  4700. + this->read_byte = sl2312_nand_read_byte;
  4701. + this->write_byte = sl2312_nand_write_byte;
  4702. + this->write_buf = sl2312_nand_write_buf;
  4703. + this->read_buf = sl2312_nand_read_buf;
  4704. + this->verify_buf = sl2312_nand_verify_buf;
  4705. + this->select_chip = sl2312_nand_select_chip;
  4706. + this->block_bad = sl2312_nand_block_bad;
  4707. + this->hwcontrol = sl2312_hwcontrol;
  4708. + this->dev_ready = sl2312_device_ready;
  4709. + this->cmdfunc = sl2312_nand_command;
  4710. + this->waitfunc = sl2312_nand_waitfunc;
  4711. + //this->calculate_ecc = sl2312_readecc;
  4712. + this->enable_hwecc = sl2312_enable_hwecc;
  4713. + this->eccmode = NAND_ECC_HW3_512;
  4714. + /*this->eccsize = 512; */
  4715. + /* 20 us command delay time */
  4716. + this->chip_delay = 20;
  4717. +
  4718. + this->correct_data = nand_correct_data;
  4719. +// this->scan_bbt = sl2312_nand_scan_bbt;
  4720. +
  4721. + /* Allocate memory for internal data buffer */
  4722. + this->data_buf = kmalloc (sizeof(u_char) * (sl2312_mtd->oobblock + sl2312_mtd->oobsize), GFP_KERNEL);
  4723. + if (!this->data_buf) {
  4724. + printk ("Unable to allocate NAND data buffer.\n");
  4725. + err = -ENOMEM;
  4726. + goto out_ior;
  4727. + }
  4728. +
  4729. + /* Scan to find existance of the device */
  4730. + if (sl2312_nand_scan(sl2312_mtd, 1)) {
  4731. + err = -ENXIO;
  4732. + goto out_ior;
  4733. + }
  4734. +
  4735. + /* Register the partitions */
  4736. + parts = sl2312_partitions;
  4737. + nr_parts = sizeof(sl2312_partitions)/sizeof(*parts);
  4738. +
  4739. + ret = add_mtd_partitions(sl2312_mtd, sl2312_partitions, nr_parts);
  4740. + /*If we got an error, free all resources.*/
  4741. + if (ret < 0) {
  4742. + del_mtd_partitions(sl2312_mtd);
  4743. + map_destroy(sl2312_mtd);
  4744. + }
  4745. + goto out;
  4746. +
  4747. +//out_buf:
  4748. +// kfree (this->data_buf);
  4749. +out_ior:
  4750. +out:
  4751. + printk("NAND MTD Driver Init Success ......\n");
  4752. + return err;
  4753. +}
  4754. +
  4755. +module_init(sl2312_mtd_init);
  4756. +
  4757. +/*
  4758. + * Clean up routine
  4759. + */
  4760. +#ifdef MODULE
  4761. +static void __exit sl2312_cleanup (void)
  4762. +{
  4763. + struct nand_chip *this = (struct nand_chip *) &sl2312_mtd[1];
  4764. +
  4765. + /* Unregister partitions */
  4766. + del_mtd_partitions(sl2312_mtd);
  4767. +
  4768. + /* Unregister the device */
  4769. + del_mtd_device (sl2312_mtd);
  4770. +
  4771. + /* Free internal data buffers */
  4772. + kfree (this->data_buf);
  4773. +
  4774. + /* Free the MTD device structure */
  4775. + kfree (sl2312_mtd);
  4776. +}
  4777. +module_exit(sl2312_cleanup);
  4778. +#endif
  4779. +
  4780. +MODULE_LICENSE("GPL");
  4781. +MODULE_AUTHOR("Alice Hennessy <[email protected]>");
  4782. +MODULE_DESCRIPTION("Glue layer for SmartMediaCard on Toshiba RBsl2312");
  4783. --- /dev/null
  4784. +++ b/drivers/mtd/nand/sl2312-flash-nand.h
  4785. @@ -0,0 +1,24 @@
  4786. +#ifndef SL2312_FLASH_NAND_H
  4787. +#define SL2312_FLASH_NAND_H
  4788. +
  4789. +#include <linux/wait.h>
  4790. +#include <linux/spinlock.h>
  4791. +
  4792. +/*Add function*/
  4793. +static void nand_read_id(int chip_no,unsigned char *id);
  4794. +
  4795. +
  4796. +
  4797. +#define NFLASH_WiDTH8 0x00000000
  4798. +#define NFLASH_WiDTH16 0x00000400
  4799. +#define NFLASH_WiDTH32 0x00000800
  4800. +#define NFLASH_CHIP0_EN 0x00000000 // 16th bit = 0
  4801. +#define NFLASH_CHIP1_EN 0x00010000 // 16th bit = 1
  4802. +#define NFLASH_DIRECT 0x00004000
  4803. +#define NFLASH_INDIRECT 0x00000000
  4804. +
  4805. +
  4806. +#define DWIDTH NFLASH_WiDTH8
  4807. +
  4808. +
  4809. +#endif /* SL2312_FLASH_NAND_H */
  4810. --- /dev/null
  4811. +++ b/include/linux/mtd/kvctl.h
  4812. @@ -0,0 +1,40 @@
  4813. +#ifndef KVCTL_H
  4814. +#define KVCTL_H
  4815. +
  4816. +#define VCTL_HEAD_SIZE 8
  4817. +#define VCTL_ENTRY_LEN 20
  4818. +
  4819. +typedef struct
  4820. +{
  4821. + char header[4];
  4822. + unsigned int entry_num;
  4823. +} vctl_mheader;
  4824. +
  4825. +typedef struct
  4826. +{
  4827. + char header[4];
  4828. + unsigned int size;
  4829. + unsigned int type;
  4830. + char majorver[4];
  4831. + char minorver[4];
  4832. + unsigned char *payload;
  4833. +} vctl_entry;
  4834. +
  4835. +typedef struct
  4836. +{
  4837. + unsigned char mac[6];
  4838. + unsigned char vlanid;
  4839. + unsigned char vlanmap;
  4840. +} vlaninfo;
  4841. +
  4842. +#define VCT_VENDORSPEC 0
  4843. +#define VCT_BOOTLOADER 1
  4844. +#define VCT_KERNEL 2
  4845. +#define VCT_VERCTL 3
  4846. +#define VCT_CURRCONF 4
  4847. +#define VCT_DEFAULTCONF 5
  4848. +#define VCT_ROOTFS 6
  4849. +#define VCT_APP 7
  4850. +#define VCT_VLAN 8
  4851. +
  4852. +#endif
  4853. --- a/drivers/mtd/maps/Makefile
  4854. +++ b/drivers/mtd/maps/Makefile
  4855. @@ -71,3 +71,7 @@
  4856. obj-$(CONFIG_MTD_OMAP_NOR) += omap_nor.o
  4857. obj-$(CONFIG_MTD_MTX1) += mtx-1_flash.o
  4858. obj-$(CONFIG_MTD_TQM834x) += tqm834x.o
  4859. +###### for Storlink Soc #######
  4860. +obj-$(CONFIG_MTD_SL2312_CFI) += sl2312-flash-cfi.o
  4861. +obj-$(CONFIG_MTD_SL2312_SERIAL_ATMEL) += sl2312-flash-atmel.o
  4862. +obj-$(CONFIG_MTD_SL2312_SERIAL_ST) += sl2312-flash-m25p80.o