mtk_nand.c 211 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507
  1. /******************************************************************************
  2. * mtk_nand.c - MTK NAND Flash Device Driver
  3. *
  4. * Copyright 2009-2012 MediaTek Co.,Ltd.
  5. *
  6. * DESCRIPTION:
  7. * This file provid the other drivers nand relative functions
  8. *
  9. * modification history
  10. * ----------------------------------------
  11. * v3.0, 11 Feb 2010, mtk
  12. * ----------------------------------------
  13. ******************************************************************************/
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/delay.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.h>
  21. #include <linux/types.h>
  22. #include <linux/wait.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/nand.h>
  27. #include <linux/mtd/partitions.h>
  28. #include <linux/mtd/nand_ecc.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/time.h>
  35. #include <linux/mm.h>
  36. /* #include <linux/xlog.h> */
  37. #include <asm/io.h>
  38. #include <asm/cacheflush.h>
  39. #include <asm/uaccess.h>
  40. #include <linux/miscdevice.h>
  41. /* #include <mach/dma.h> */
  42. #include <mt-plat/dma.h>
  43. /* #include <mach/devs.h> */
  44. #ifdef CONFIG_OF
  45. #include <linux/of.h>
  46. #include <linux/of_address.h>
  47. #include <linux/of_irq.h>
  48. #else
  49. #include <mach/mt_reg_base.h>
  50. #endif
  51. /* #include <mach/mt_typedefs.h> */
  52. /* #include <mach/mt_clkmgr.h> */
  53. /* #include <mach/mtk_nand.h> */
  54. /* #include <mach/bmt.h> */
  55. #include <mtk_nand.h>
  56. #include <bmt.h>
  57. /* #include <mach/mt_irq.h> */
  58. /* #include "partition.h" */
  59. /* #include <asm/system.h> */
  60. /* #include <mach/partition_define.h> */
  61. #include <partition_define.h>
  62. /* #include <mach/mt_boot.h> */
  63. #include <mt-plat/mt_boot.h>
  64. /* #include "../../../../../../source/kernel/drivers/aee/ipanic/ipanic.h" */
  65. #include <linux/rtc.h>
  66. /* #include <mach/mt_gpio.h> */
  67. /* #include <mach/mt_pm_ldo.h> */
  68. #ifdef CONFIG_PWR_LOSS_MTK_SPOH
  69. #include <mach/power_loss_test.h>
  70. #endif
  71. /* #include <mach/nand_device_define.h> */
  72. #include <nand_device_define.h>
  73. #ifndef CONFIG_MTK_LEGACY
  74. #include <linux/clk.h>
  75. #include <linux/regulator/consumer.h>
  76. #endif
  77. #ifndef FALSE
  78. #define FALSE (0)
  79. #endif
  80. #ifndef TRUE
  81. #define TRUE (1)
  82. #endif
  83. #ifndef NULL
  84. #define NULL (0)
  85. #endif
  86. #define READ_REGISTER_UINT8(reg) \
  87. (*(volatile unsigned char * const)(reg))
  88. #define READ_REGISTER_UINT16(reg) \
  89. (*(volatile unsigned short * const)(reg))
  90. #define READ_REGISTER_UINT32(reg) \
  91. (*(volatile unsigned int * const)(reg))
  92. #define INREG8(x) READ_REGISTER_UINT8((unsigned char *)((void *)(x)))
  93. #define INREG16(x) READ_REGISTER_UINT16((unsigned short *)((void *)(x)))
  94. #define INREG32(x) READ_REGISTER_UINT32((unsigned int *)((void *)(x)))
  95. #define DRV_Reg8(addr) INREG8(addr)
  96. #define DRV_Reg16(addr) INREG16(addr)
  97. #define DRV_Reg32(addr) INREG32(addr)
  98. #define DRV_Reg(addr) DRV_Reg16(addr)
  99. #define WRITE_REGISTER_UINT8(reg, val) \
  100. ((*(volatile unsigned char * const)(reg)) = (val))
  101. #define WRITE_REGISTER_UINT16(reg, val) \
  102. ((*(volatile unsigned short * const)(reg)) = (val))
  103. #define WRITE_REGISTER_UINT32(reg, val) \
  104. ((*(volatile unsigned int * const)(reg)) = (val))
  105. #define OUTREG8(x, y) WRITE_REGISTER_UINT8((unsigned char *)((void *)(x)), (unsigned char)(y))
  106. #define OUTREG16(x, y) WRITE_REGISTER_UINT16((unsigned short *)((void *)(x)), (unsigned short)(y))
  107. #define OUTREG32(x, y) WRITE_REGISTER_UINT32((unsigned int *)((void *)(x)), (unsigned int)(y))
  108. #define DRV_WriteReg8(addr, data) OUTREG8(addr, data)
  109. #define DRV_WriteReg16(addr, data) OUTREG16(addr, data)
  110. #define DRV_WriteReg32(addr, data) OUTREG32(addr, data)
  111. #define DRV_WriteReg(addr, data) DRV_WriteReg16(addr, data)
  112. static const flashdev_info_t gen_FlashTable_p[] = {
  113. {{0x45, 0xDE, 0x94, 0x93, 0x76, 0x57}, 6, 5, IO_8BIT, 8192, 4096, 16384, 1280, 0x10401011,
  114. 0xC03222, 0x101, 80, VEND_SANDISK, 1024, "SDTNQGAMA008G ", 0,
  115. {SANDISK_16K,
  116. {0xEF, 0xEE, 0xFF, 16, 0x11, 0, 1, RTYPE_SANDISK_19NM, {0x80, 0x00}, {0x80, 0x01} },
  117. {RAND_TYPE_SAMSUNG, {0x2D2D, 1, 1, 1, 1, 1} } } },
  118. {{0x98, 0xD7, 0x84, 0x93, 0x72, 0x00}, 5, 5, IO_8BIT, 4096, 4096, 16384, 1280, 0x10401011,
  119. 0xC03222, 0x101, 80, VEND_TOSHIBA, 1024, "TC58TEG5DCKTA00", 0,
  120. {SANDISK_16K, {0xEF, 0xEE, 0xFF, 7, 0xFF, 7, 0, RTYPE_TOSHIBA, {0x80, 0x00}, {0x80, 0x01} },
  121. {RAND_TYPE_SAMSUNG, {0x2D2D, 1, 1, 1, 1, 1} } } },
  122. {{0x45, 0xDE, 0x94, 0x93, 0x76, 0x00}, 5, 5, IO_8BIT, 8192, 4096, 16384, 1280, 0x10401011,
  123. 0xC03222, 0x101, 80, VEND_SANDISK, 1024, "SDTNRGAMA008GK ", 0,
  124. {SANDISK_16K,
  125. {0xEF, 0xEE, 0x5D, 36, 0x11, 0, 0xFFFFFFFF, RTYPE_SANDISK, {0x80, 0x00}, {0x80, 0x01} },
  126. {RAND_TYPE_SAMSUNG, {0x2D2D, 1, 1, 1, 1, 1} } } },
  127. {{0xAD, 0xDE, 0x14, 0xA7, 0x42, 0x00}, 5, 5, IO_8BIT, 8192, 4096, 16384, 1280, 0x10401011,
  128. 0xC03222, 0x101, 80, VEND_HYNIX, 1024, "H27UCG8T2ETR", 0,
  129. {SANDISK_16K,
  130. {0xFF, 0xFF, 0xFF, 7, 0xFF, 0, 1, RTYPE_HYNIX_16NM, {0XFF, 0xFF}, {0XFF, 0xFF} },
  131. {RAND_TYPE_SAMSUNG, {0x2D2D, 1, 1, 1, 1, 1} } } },
  132. {{0x2C, 0x44, 0x44, 0x4B, 0xA9, 0x00}, 5, 5, IO_8BIT, 4096, 2048, 8192, 640, 0x10401011,
  133. 0xC03222, 0x101, 80, VEND_MICRON, 1024, "MT29F32G08CBADB ", 0,
  134. {MICRON_8K, {0xEF, 0xEE, 0xFF, 7, 0x89, 0, 1, RTYPE_MICRON, {0x1, 0x14}, {0x1, 0x5} },
  135. {RAND_TYPE_SAMSUNG, {0x2D2D, 1, 1, 1, 1, 1} } } },
  136. {{0xAD, 0xDE, 0x94, 0xA7, 0x42, 0x00}, 5, 5, IO_8BIT, 8192, 4096, 16384, 1280, 0x10401011,
  137. 0xC03222, 0x101, 80, VEND_BIWIN, 1024, "BW27UCG8T2ETR", 0,
  138. {SANDISK_16K,
  139. {0xFF, 0xFF, 0xFF, 7, 0xFF, 0, 1, RTYPE_HYNIX_16NM, {0XFF, 0xFF}, {0XFF, 0xFF} },
  140. {RAND_TYPE_SAMSUNG, {0x2D2D, 1, 1, 1, 1, 1} } } },
  141. {{0x45, 0xD7, 0x84, 0x93, 0x72, 0x00}, 5, 5, IO_8BIT, 4096, 4096, 16384, 1280, 0x10401011,
  142. 0xC03222, 0x101, 80, VEND_SANDISK, 1024, "SDTNRGAMA004GK ", 0,
  143. {SANDISK_16K,
  144. {0xEF, 0xEE, 0x5D, 36, 0x11, 0, 0xFFFFFFFF, RTYPE_SANDISK, {0x80, 0x00}, {0x80, 0x01} },
  145. {RAND_TYPE_SAMSUNG, {0x2D2D, 1, 1, 1, 1, 1} } } },
  146. {{0x2C, 0x64, 0x44, 0x4B, 0xA9, 0x00}, 5, 5, IO_8BIT, 8192, 2048, 8192, 640, 0x10401011,
  147. 0xC03222, 0x101, 80, VEND_MICRON, 1024, "MT29F128G08CFABA ", 0,
  148. {MICRON_8K, {0xEF, 0xEE, 0xFF, 7, 0x89, 0, 1, RTYPE_MICRON, {0x1, 0x14}, {0x1, 0x5} },
  149. {RAND_TYPE_SAMSUNG, {0x2D2D, 1, 1, 1, 1, 1} } } },
  150. {{0xAD, 0xD7, 0x94, 0x91, 0x60, 0x00}, 5, 5, IO_8BIT, 4096, 2048, 8192, 640, 0x10401011,
  151. 0xC03222, 0x101, 80, VEND_HYNIX, 1024, "H27UBG8T2CTR", 0,
  152. {HYNIX_8K, {0xFF, 0xFF, 0xFF, 7, 0xFF, 0, 1, RTYPE_HYNIX, {0XFF, 0xFF}, {0XFF, 0xFF} },
  153. {RAND_TYPE_SAMSUNG, {0x2D2D, 1, 1, 1, 1, 1} } } },
  154. {{0x98, 0xDE, 0x94, 0x93, 0x76, 0x00}, 5, 5, IO_8BIT, 8192, 4096, 16384, 1280, 0x10401011,
  155. 0xC03222, 0x101, 80, VEND_TOSHIBA, 1024, "TC58TEG6DDKTA00", 0,
  156. {SANDISK_16K, {0xEF, 0xEE, 0xFF, 7, 0xFF, 7, 0, RTYPE_TOSHIBA, {0x80, 0x00}, {0x80, 0x01} },
  157. {RAND_TYPE_SAMSUNG, {0x2D2D, 1, 1, 1, 1, 1} } } },
  158. };
  159. static unsigned int flash_number = sizeof(gen_FlashTable_p) / sizeof(flashdev_info_t);
  160. #define NFI_DEFAULT_CS (0)
  161. #define mtk_nand_assert(expr) do { \
  162. if (unlikely(!(expr))) { \
  163. pr_crit("MTK nand assert failed in %s at %u (pid %d)\n", \
  164. __func__, __LINE__, current->pid); \
  165. dump_stack(); \
  166. } \
  167. } while (0)
  168. #ifndef CONFIG_MTK_LEGACY
  169. struct clk *nfi_clock = NULL;
  170. struct clk *nfi_ecc_clock = NULL;
  171. struct clk *nfi_bclk_clock = NULL;
  172. struct clk *onfi_sel_clock = NULL;
  173. struct clk *onfi_26m_clock = NULL;
  174. struct clk *syspll2_d2_clock = NULL;
  175. struct clk *syspll_d7_clock = NULL;
  176. struct clk *infra_nfi_sel_clock = NULL;
  177. struct clk *axi_sel_clock = NULL;
  178. struct clk *onfi_d2_clock = NULL;
  179. struct regulator *mtk_nand_regulator = NULL;
  180. #endif
  181. #define VERSION "v2.1 Fix AHB virt2phys error"
  182. #define MODULE_NAME "# MTK NAND #"
  183. #define PROCNAME "driver/nand"
  184. #define _MTK_NAND_DUMMY_DRIVER_
  185. #define __INTERNAL_USE_AHB_MODE__ (1)
  186. #define CFG_FPGA_PLATFORM (0) /* for fpga by bean */
  187. #define CFG_RANDOMIZER (1) /* for randomizer code */
  188. #define CFG_PERFLOG_DEBUG (0) /* for performance log */
  189. #define CFG_2CS_NAND (1) /* for 2CS nand */
  190. #define CFG_COMBO_NAND (1) /* for Combo nand */
  191. #define NFI_TRICKY_CS (1) /* must be 1 or > 1? */
  192. /* #define MANUAL_CORRECT */
  193. #if defined(MTK_MLC_NAND_SUPPORT)
  194. bool MLC_DEVICE = TRUE; /* to build pass xiaolei */
  195. #endif
  196. #ifdef CONFIG_OF
  197. void __iomem *mtk_nfi_base;
  198. void __iomem *mtk_nfiecc_base;
  199. struct device_node *mtk_nfiecc_node = NULL;
  200. unsigned int nfi_irq = 0;
  201. #define MT_NFI_IRQ_ID nfi_irq
  202. void __iomem *mtk_gpio_base;
  203. struct device_node *mtk_gpio_node = NULL;
  204. #define GPIO_BASE mtk_gpio_base
  205. #ifdef CONFIG_MTK_LEGACY
  206. void __iomem *mtk_efuse_base;
  207. struct device_node *mtk_efuse_node = NULL;
  208. #define EFUSE_BASE mtk_efuse_base
  209. #endif
  210. void __iomem *mtk_infra_base;
  211. struct device_node *mtk_infra_node = NULL;
  212. #endif
  213. struct device *mtk_dev;
  214. struct scatterlist mtk_sg;
  215. enum dma_data_direction mtk_dir;
  216. #ifndef CONFIG_MTK_FPGA
  217. #if defined(CONFIG_MTK_LEGACY)
  218. #define PERI_NFI_CLK_SOURCE_SEL ((volatile unsigned int *)(mtk_infra_base+0x098))
  219. /* #define PERI_NFI_MAC_CTRL ((volatile unsigned int *)(PERICFG_BASE+0x428)) */
  220. #define NFI_PAD_1X_CLOCK (0x1 << 10) /* nfi1X */
  221. #endif
  222. #endif
  223. #if defined(NAND_OTP_SUPPORT)
  224. #define SAMSUNG_OTP_SUPPORT 1
  225. #define OTP_MAGIC_NUM 0x4E3AF28B
  226. #define SAMSUNG_OTP_PAGE_NUM 6
  227. static const unsigned int Samsung_OTP_Page[SAMSUNG_OTP_PAGE_NUM] = {
  228. 0x15, 0x16, 0x17, 0x18, 0x19, 0x1b
  229. };
  230. static struct mtk_otp_config g_mtk_otp_fuc;
  231. static spinlock_t g_OTPLock;
  232. #define OTP_MAGIC 'k'
  233. /* NAND OTP IO control number */
  234. #define OTP_GET_LENGTH _IOW(OTP_MAGIC, 1, int)
  235. #define OTP_READ _IOW(OTP_MAGIC, 2, int)
  236. #define OTP_WRITE _IOW(OTP_MAGIC, 3, int)
  237. #define FS_OTP_READ 0
  238. #define FS_OTP_WRITE 1
  239. /* NAND OTP Error codes */
  240. #define OTP_SUCCESS 0
  241. #define OTP_ERROR_OVERSCOPE -1
  242. #define OTP_ERROR_TIMEOUT -2
  243. #define OTP_ERROR_BUSY -3
  244. #define OTP_ERROR_NOMEM -4
  245. #define OTP_ERROR_RESET -5
  246. struct mtk_otp_config {
  247. u32 (*OTPRead)(u32 PageAddr, void *BufferPtr, void *SparePtr);
  248. u32 (*OTPWrite)(u32 PageAddr, void *BufferPtr, void *SparePtr);
  249. u32 (*OTPQueryLength)(u32 *Length);
  250. };
  251. struct otp_ctl {
  252. unsigned int QLength;
  253. unsigned int Offset;
  254. unsigned int Length;
  255. char *BufferPtr;
  256. unsigned int status;
  257. };
  258. #endif
  259. #define ERR_RTN_SUCCESS 1
  260. #define ERR_RTN_FAIL 0
  261. #define ERR_RTN_BCH_FAIL -1
  262. #define NFI_SET_REG32(reg, value) \
  263. do { \
  264. g_value = (DRV_Reg32(reg) | (value));\
  265. DRV_WriteReg32(reg, g_value); \
  266. } while (0)
  267. #define NFI_SET_REG16(reg, value) \
  268. do { \
  269. g_value = (DRV_Reg16(reg) | (value));\
  270. DRV_WriteReg16(reg, g_value); \
  271. } while (0)
  272. #define NFI_CLN_REG32(reg, value) \
  273. do { \
  274. g_value = (DRV_Reg32(reg) & (~(value)));\
  275. DRV_WriteReg32(reg, g_value); \
  276. } while (0)
  277. #define NFI_CLN_REG16(reg, value) \
  278. do { \
  279. g_value = (DRV_Reg16(reg) & (~(value)));\
  280. DRV_WriteReg16(reg, g_value); \
  281. } while (0)
  282. #define NFI_WAIT_STATE_DONE(state) do {; } while (__raw_readl(NFI_STA_REG32) & state)
  283. #define NFI_WAIT_TO_READY() do {; } while (!(__raw_readl(NFI_STA_REG32) & STA_BUSY2READY))
  284. #define FIFO_PIO_READY(x) (0x1 & x)
  285. #define WAIT_NFI_PIO_READY(timeout) \
  286. do {\
  287. while ((!FIFO_PIO_READY(DRV_Reg(NFI_PIO_DIRDY_REG16))) && (--timeout)) \
  288. ;\
  289. } while (0)
  290. #define NAND_SECTOR_SIZE (512)
  291. #define OOB_PER_SECTOR (16)
  292. #define OOB_AVAI_PER_SECTOR (8)
  293. #if defined(MTK_COMBO_NAND_SUPPORT)
  294. /* BMT_POOL_SIZE is not used anymore */
  295. #else
  296. #ifndef PART_SIZE_BMTPOOL
  297. #define BMT_POOL_SIZE (80)
  298. #else
  299. #define BMT_POOL_SIZE (PART_SIZE_BMTPOOL)
  300. #endif
  301. #endif
  302. u8 ecc_threshold;
  303. #define PMT_POOL_SIZE (2)
  304. /*******************************************************************************
  305. * Gloable Varible Definition
  306. *******************************************************************************/
  307. #if CFG_PERFLOG_DEBUG
  308. struct nand_perf_log {
  309. unsigned int ReadPageCount;
  310. suseconds_t ReadPageTotalTime;
  311. unsigned int ReadBusyCount;
  312. suseconds_t ReadBusyTotalTime;
  313. unsigned int ReadDMACount;
  314. suseconds_t ReadDMATotalTime;
  315. unsigned int ReadSubPageCount;
  316. suseconds_t ReadSubPageTotalTime;
  317. unsigned int WritePageCount;
  318. suseconds_t WritePageTotalTime;
  319. unsigned int WriteBusyCount;
  320. suseconds_t WriteBusyTotalTime;
  321. unsigned int WriteDMACount;
  322. suseconds_t WriteDMATotalTime;
  323. unsigned int EraseBlockCount;
  324. suseconds_t EraseBlockTotalTime;
  325. };
  326. #endif
  327. #ifdef PWR_LOSS_SPOH
  328. #define PL_TIME_RAND_PROG(chip, page_addr, time) do { \
  329. if (host->pl.nand_program_wdt_enable == 1) { \
  330. PL_TIME_RAND(page_addr, time, host->pl.last_prog_time); } \
  331. else \
  332. time = 0; \
  333. } while (0)
  334. #define PL_TIME_RAND_ERASE(chip, page_addr, time) do { \
  335. if (host->pl.nand_erase_wdt_enable == 1) { \
  336. PL_TIME_RAND(page_addr, time, host->pl.last_erase_time); \
  337. if (time != 0) \
  338. pr_err("[MVG_TEST]: Erase reset in %d us\n", time); } \
  339. else \
  340. time = 0; \
  341. } while (0)
  342. #define PL_TIME_PROG(duration) \
  343. host->pl.last_prog_time = duration
  344. #define PL_TIME_ERASE(duration) \
  345. host->pl.last_erase_time = duration
  346. #define PL_TIME_PROG_WDT_SET(WDT) \
  347. host->pl.nand_program_wdt_enable = WDT
  348. #define PL_TIME_ERASE_WDT_SET(WDT) \
  349. host->pl.nand_erase_wdt_enable = WDT
  350. #define PL_NAND_BEGIN(time) PL_BEGIN(time)
  351. #define PL_NAND_RESET(time) PL_RESET(time)
  352. #define PL_NAND_END(pl_time_write, duration) PL_END(pl_time_write, duration)
  353. #else
  354. #define PL_TIME_RAND_PROG(chip, page_addr, time)
  355. #define PL_TIME_RAND_ERASE(chip, page_addr, time)
  356. #define PL_TIME_PROG(duration)
  357. #define PL_TIME_ERASE(duration)
  358. #define PL_TIME_PROG_WDT_SET(WDT)
  359. #define PL_TIME_ERASE_WDT_SET(WDT)
  360. #define PL_NAND_BEGIN(time)
  361. #define PL_NAND_RESET(time)
  362. #define PL_NAND_END(pl_time_write, duration)
  363. #endif
  364. #if CFG_PERFLOG_DEBUG
  365. static struct nand_perf_log g_NandPerfLog = { 0 };
  366. static struct timeval g_NandLogTimer = { 0 };
  367. #endif
  368. #ifdef NAND_PFM
  369. static suseconds_t g_PFM_R;
  370. static suseconds_t g_PFM_W;
  371. static suseconds_t g_PFM_E;
  372. static u32 g_PFM_RNum;
  373. static u32 g_PFM_RD;
  374. static u32 g_PFM_WD;
  375. static struct timeval g_now;
  376. #define PFM_BEGIN(time) do { \
  377. do_gettimeofday(&g_now); \
  378. (time) = g_now; \
  379. } while (0)
  380. #define PFM_END_R(time, n) do {\
  381. do_gettimeofday(&g_now); \
  382. g_PFM_R += (g_now.tv_sec * 1000000 + g_now.tv_usec) - (time.tv_sec * 1000000 + time.tv_usec); \
  383. g_PFM_RNum += 1; \
  384. g_PFM_RD += n; \
  385. pr_debug("%s - Read PFM: %lu, data: %d, ReadOOB: %d (%d, %d)\n", \
  386. MODULE_NAME , g_PFM_R, g_PFM_RD, g_kCMD.pureReadOOB, g_kCMD.pureReadOOBNum, g_PFM_RNum);\
  387. } while (0)
  388. #define PFM_END_W(time, n) do {\
  389. do_gettimeofday(&g_now); \
  390. g_PFM_W += (g_now.tv_sec * 1000000 + g_now.tv_usec) - (time.tv_sec * 1000000 + time.tv_usec); \
  391. g_PFM_WD += n; \
  392. pr_debug("%s - Write PFM: %lu, data: %d\n", MODULE_NAME, g_PFM_W, g_PFM_WD);\
  393. } while (0)
  394. #define PFM_END_E(time) do {\
  395. do_gettimeofday(&g_now); \
  396. g_PFM_E += (g_now.tv_sec * 1000000 + g_now.tv_usec) - (time.tv_sec * 1000000 + time.tv_usec); \
  397. pr_debug("%s - Erase PFM: %lu\n", MODULE_NAME, g_PFM_E); \
  398. } while (0)
  399. #else
  400. #define PFM_BEGIN(time)
  401. #define PFM_END_R(time, n)
  402. #define PFM_END_W(time, n)
  403. #define PFM_END_E(time)
  404. #endif
  405. #define TIMEOUT_1 0x1fff
  406. #define TIMEOUT_2 0x8ff
  407. #define TIMEOUT_3 0xffff
  408. #define TIMEOUT_4 0xffff /* 5000 //PIO */
  409. #define NFI_ISSUE_COMMAND(cmd, col_addr, row_addr, col_num, row_num) \
  410. do { \
  411. DRV_WriteReg(NFI_CMD_REG16, cmd);\
  412. while (DRV_Reg32(NFI_STA_REG32) & STA_CMD_STATE)\
  413. ;\
  414. DRV_WriteReg32(NFI_COLADDR_REG32, col_addr);\
  415. DRV_WriteReg32(NFI_ROWADDR_REG32, row_addr);\
  416. DRV_WriteReg(NFI_ADDRNOB_REG16, col_num | (row_num<<ADDR_ROW_NOB_SHIFT))\
  417. ;\
  418. while (DRV_Reg32(NFI_STA_REG32) & STA_ADDR_STATE)\
  419. ;\
  420. } while (0)
  421. /* ------------------------------------------------------------------------------- */
  422. static struct completion g_comp_AHB_Done;
  423. static struct NAND_CMD g_kCMD;
  424. bool g_bInitDone;
  425. static int g_i4Interrupt;
  426. static bool g_bcmdstatus;
  427. /* static bool g_brandstatus; */
  428. static u32 g_value;
  429. static int g_page_size;
  430. static int g_block_size;
  431. static u32 PAGES_PER_BLOCK = 255;
  432. static bool g_bSyncOrToggle;
  433. #ifndef CONFIG_MTK_FPGA
  434. #ifdef CONFIG_MTK_LEGACY
  435. static int g_iNFI2X_CLKSRC = ARMPLL;
  436. #else
  437. static int g_iNFI2X_CLKSRC;
  438. #endif
  439. #endif
  440. /* extern unsigned int flash_number; */
  441. /* extern flashdev_info_t gen_FlashTable_p[MAX_FLASH]; */
  442. #if CFG_2CS_NAND
  443. bool g_b2Die_CS = FALSE; /* for nand base */
  444. static bool g_bTricky_CS = FALSE;
  445. static u32 g_nanddie_pages;
  446. #endif
  447. #if __INTERNAL_USE_AHB_MODE__
  448. unsigned char g_bHwEcc = true;
  449. #else
  450. unsigned char g_bHwEcc = false;
  451. #endif
  452. #define LPAGE 16384
  453. #define LSPARE 2048
  454. static u8 *local_buffer_16_align; /* 16 byte aligned buffer, for HW issue */
  455. __aligned(64)
  456. static u8 local_buffer[LPAGE + LSPARE];
  457. static u8 *temp_buffer_16_align; /* 16 byte aligned buffer, for HW issue */
  458. __aligned(64)
  459. static u8 temp_buffer[LPAGE + LSPARE];
  460. /* static u8 *bean_buffer_16_align; // 16 byte aligned buffer, for HW issue */
  461. /* __attribute__((aligned(64))) static u8 bean_buffer[LPAGE + LSPARE]; */
  462. #if CFG_2CS_NAND
  463. static int mtk_nand_cs_check(struct mtd_info *mtd, u8 *id, u16 cs);
  464. static u32 mtk_nand_cs_on(struct nand_chip *nand_chip, u16 cs, u32 page);
  465. #endif
  466. static bmt_struct *g_bmt;
  467. struct mtk_nand_host *host;
  468. static u8 g_running_dma;
  469. #ifdef DUMP_NATIVE_BACKTRACE
  470. static u32 g_dump_count;
  471. #endif
  472. /* extern struct mtd_partition g_pasStatic_Partition[];//to build pass xiaolei */
  473. /* int part_num = PART_NUM;//to build pass xiaolei NUM_PARTITIONS; */
  474. int manu_id;
  475. int dev_id;
  476. static u8 local_oob_buf[LSPARE];
  477. #ifdef _MTK_NAND_DUMMY_DRIVER_
  478. int dummy_driver_debug;
  479. #endif
  480. flashdev_info_t devinfo;
  481. enum NAND_TYPE_MASK {
  482. TYPE_ASYNC = 0x0,
  483. TYPE_TOGGLE = 0x1,
  484. TYPE_SYNC = 0x2,
  485. TYPE_RESERVED = 0x3,
  486. TYPE_MLC = 0x4, /* 1b0 */
  487. TYPE_SLC = 0x4, /* 1b1 */
  488. };
  489. typedef u32(*GetLowPageNumber) (u32 pageNo);
  490. typedef u32(*TransferPageNumber) (u32 pageNo, bool high_to_low);
  491. GetLowPageNumber functArray[] = {
  492. MICRON_TRANSFER,
  493. HYNIX_TRANSFER,
  494. SANDISK_TRANSFER,
  495. };
  496. TransferPageNumber fsFuncArray[] = {
  497. micron_pairpage_mapping,
  498. hynix_pairpage_mapping,
  499. sandisk_pairpage_mapping,
  500. };
  501. u32 SANDISK_TRANSFER(u32 pageNo)
  502. {
  503. if (0 == pageNo)
  504. return pageNo;
  505. else
  506. return pageNo + pageNo - 1;
  507. }
  508. u32 HYNIX_TRANSFER(u32 pageNo)
  509. {
  510. u32 temp;
  511. if (pageNo < 4)
  512. return pageNo;
  513. temp = pageNo + (pageNo & 0xFFFFFFFE) - 2;
  514. return temp;
  515. }
  516. u32 MICRON_TRANSFER(u32 pageNo)
  517. {
  518. u32 temp;
  519. if (pageNo < 4)
  520. return pageNo;
  521. temp = (pageNo - 4) & 0xFFFFFFFE;
  522. if (pageNo <= 130)
  523. return (pageNo + temp);
  524. else
  525. return (pageNo + temp - 2);
  526. }
  527. u32 sandisk_pairpage_mapping(u32 page, bool high_to_low)
  528. {
  529. if (TRUE == high_to_low) {
  530. if (page == 255)
  531. return page - 2;
  532. if ((page == 0) || (1 == (page % 2)))
  533. return page;
  534. if (page == 2)
  535. return 0;
  536. else
  537. return (page - 3);
  538. } else {
  539. if ((page != 0) && (0 == (page % 2)))
  540. return page;
  541. if (page == 255)
  542. return page;
  543. if (page == 0 || page == 253)
  544. return page + 2;
  545. else
  546. return page + 3;
  547. }
  548. }
  549. u32 hynix_pairpage_mapping(u32 page, bool high_to_low)
  550. {
  551. u32 offset;
  552. if (TRUE == high_to_low) {
  553. /* Micron 256pages */
  554. if (page < 4)
  555. return page;
  556. offset = page % 4;
  557. if (offset == 2 || offset == 3)
  558. return page;
  559. if (page == 4 || page == 5 || page == 254 || page == 255)
  560. return page - 4;
  561. else
  562. return page - 6;
  563. } else {
  564. if (page > 251)
  565. return page;
  566. if (page == 0 || page == 1)
  567. return page + 4;
  568. offset = page % 4;
  569. if (offset == 0 || offset == 1)
  570. return page;
  571. else
  572. return page + 6;
  573. }
  574. }
  575. u32 micron_pairpage_mapping(u32 page, bool high_to_low)
  576. {
  577. u32 offset;
  578. if (TRUE == high_to_low) {
  579. /* Micron 256pages */
  580. if ((page < 4) || (page > 251))
  581. return page;
  582. offset = page % 4;
  583. if (offset == 0 || offset == 1)
  584. return page;
  585. else
  586. return page - 6;
  587. } else {
  588. if ((page == 2) || (page == 3) || (page > 247))
  589. return page;
  590. offset = page % 4;
  591. if (offset == 0 || offset == 1)
  592. return page + 6;
  593. else
  594. return page;
  595. }
  596. }
  597. int mtk_nand_paired_page_transfer(u32 pageNo, bool high_to_low)
  598. {
  599. if (devinfo.vendor != VEND_NONE)
  600. return fsFuncArray[devinfo.feature_set.ptbl_idx] (pageNo, high_to_low);
  601. else
  602. return 0xFFFFFFFF;
  603. }
  604. #ifdef CONFIG_MTK_FPGA
  605. void nand_enable_clock(void)
  606. {
  607. }
  608. void nand_disable_clock(void)
  609. {
  610. }
  611. #else
  612. #define PWR_DOWN 0
  613. #define PWR_ON 1
  614. void nand_enable_clock(void)
  615. {
  616. #if defined(CONFIG_MTK_LEGACY)
  617. /* if(clock_is_on(MT_CG_INFRA_NFI)==PWR_DOWN) */
  618. enable_clock(MT_CG_INFRA_NFI, "NFI");
  619. /* if(clock_is_on(MT_CG_INFRA_NFI_ECC)==PWR_DOWN) */
  620. enable_clock(MT_CG_INFRA_NFI_ECC, "NFI");
  621. /* if(clock_is_on(MT_CG_INFRA_NFI_BCLK)==PWR_DOWN) */
  622. enable_clock(MT_CG_INFRA_NFI_BCLK, "NFI");
  623. #else
  624. clk_prepare_enable(nfi_clock);
  625. clk_prepare_enable(nfi_ecc_clock);
  626. clk_prepare_enable(nfi_bclk_clock);
  627. #endif
  628. }
  629. void nand_disable_clock(void)
  630. {
  631. #if defined(CONFIG_MTK_LEGACY)
  632. /* if(clock_is_on(MT_CG_INFRA_NFI_BCLK)==PWR_ON) */
  633. disable_clock(MT_CG_INFRA_NFI_BCLK, "NFI");
  634. /* if(clock_is_on(MT_CG_INFRA_NFI_ECC)==PWR_ON) */
  635. disable_clock(MT_CG_INFRA_NFI_ECC, "NFI");
  636. /* if(clock_is_on(MT_CG_INFRA_NFI)==PWR_ON) */
  637. disable_clock(MT_CG_INFRA_NFI, "NFI");
  638. #else
  639. clk_disable_unprepare(nfi_clock);
  640. clk_disable_unprepare(nfi_ecc_clock);
  641. clk_disable_unprepare(nfi_bclk_clock);
  642. #endif
  643. }
  644. #endif
  645. static struct nand_ecclayout nand_oob_16 = {
  646. .eccbytes = 8,
  647. .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
  648. .oobfree = {{1, 6}, {0, 0} }
  649. };
  650. struct nand_ecclayout nand_oob_64 = {
  651. .eccbytes = 32,
  652. .eccpos = {32, 33, 34, 35, 36, 37, 38, 39,
  653. 40, 41, 42, 43, 44, 45, 46, 47,
  654. 48, 49, 50, 51, 52, 53, 54, 55,
  655. 56, 57, 58, 59, 60, 61, 62, 63},
  656. .oobfree = {{1, 7}, {9, 7}, {17, 7}, {25, 6}, {0, 0} }
  657. };
  658. struct nand_ecclayout nand_oob_128 = {
  659. .eccbytes = 64,
  660. .eccpos = {
  661. 64, 65, 66, 67, 68, 69, 70, 71,
  662. 72, 73, 74, 75, 76, 77, 78, 79,
  663. 80, 81, 82, 83, 84, 85, 86, 86,
  664. 88, 89, 90, 91, 92, 93, 94, 95,
  665. 96, 97, 98, 99, 100, 101, 102, 103,
  666. 104, 105, 106, 107, 108, 109, 110, 111,
  667. 112, 113, 114, 115, 116, 117, 118, 119,
  668. 120, 121, 122, 123, 124, 125, 126, 127},
  669. .oobfree = {{1, 7}, {9, 7}, {17, 7}, {25, 7}, {33, 7}, {41, 7}, {49, 7}, {57, 6} }
  670. };
  671. /**************************************************************************
  672. * Randomizer
  673. **************************************************************************/
  674. #define SS_SEED_NUM 128
  675. #ifdef CONFIG_MTK_LEGACY
  676. #define EFUSE_RANDOM_CFG ((volatile u32 *)(EFUSE_BASE + 0x01C0))
  677. #endif
  678. #define EFUSE_RANDOM_ENABLE 0x00000004
  679. static bool use_randomizer = FALSE;
  680. static bool pre_randomizer = FALSE;
  681. static unsigned short SS_RANDOM_SEED[SS_SEED_NUM] = {
  682. /* for page 0~127 */
  683. 0x576A, 0x05E8, 0x629D, 0x45A3, 0x649C, 0x4BF0, 0x2342, 0x272E,
  684. 0x7358, 0x4FF3, 0x73EC, 0x5F70, 0x7A60, 0x1AD8, 0x3472, 0x3612,
  685. 0x224F, 0x0454, 0x030E, 0x70A5, 0x7809, 0x2521, 0x484F, 0x5A2D,
  686. 0x492A, 0x043D, 0x7F61, 0x3969, 0x517A, 0x3B42, 0x769D, 0x0647,
  687. 0x7E2A, 0x1383, 0x49D9, 0x07B8, 0x2578, 0x4EEC, 0x4423, 0x352F,
  688. 0x5B22, 0x72B9, 0x367B, 0x24B6, 0x7E8E, 0x2318, 0x6BD0, 0x5519,
  689. 0x1783, 0x18A7, 0x7B6E, 0x7602, 0x4B7F, 0x3648, 0x2C53, 0x6B99,
  690. 0x0C23, 0x67CF, 0x7E0E, 0x4D8C, 0x5079, 0x209D, 0x244A, 0x747B,
  691. 0x350B, 0x0E4D, 0x7004, 0x6AC3, 0x7F3E, 0x21F5, 0x7A15, 0x2379,
  692. 0x1517, 0x1ABA, 0x4E77, 0x15A1, 0x04FA, 0x2D61, 0x253A, 0x1302,
  693. 0x1F63, 0x5AB3, 0x049A, 0x5AE8, 0x1CD7, 0x4A00, 0x30C8, 0x3247,
  694. 0x729C, 0x5034, 0x2B0E, 0x57F2, 0x00E4, 0x575B, 0x6192, 0x38F8,
  695. 0x2F6A, 0x0C14, 0x45FC, 0x41DF, 0x38DA, 0x7AE1, 0x7322, 0x62DF,
  696. 0x5E39, 0x0E64, 0x6D85, 0x5951, 0x5937, 0x6281, 0x33A1, 0x6A32,
  697. 0x3A5A, 0x2BAC, 0x743A, 0x5E74, 0x3B2E, 0x7EC7, 0x4FD2, 0x5D28,
  698. 0x751F, 0x3EF8, 0x39B1, 0x4E49, 0x746B, 0x6EF6, 0x44BE, 0x6DB7
  699. };
  700. #if CFG_PERFLOG_DEBUG
  701. static suseconds_t Cal_timediff(struct timeval *end_time, struct timeval *start_time)
  702. {
  703. struct timeval difference;
  704. difference.tv_sec = end_time->tv_sec - start_time->tv_sec;
  705. difference.tv_usec = end_time->tv_usec - start_time->tv_usec;
  706. /* Using while instead of if below makes the code slightly more robust. */
  707. while (difference.tv_usec < 0) {
  708. difference.tv_usec += 1000000;
  709. difference.tv_sec -= 1;
  710. }
  711. return 1000000LL * difference.tv_sec + difference.tv_usec;
  712. } /* timeval_diff() */
  713. #endif
  714. #if CFG_PERFLOG_DEBUG
  715. void dump_nand_rwcount(void)
  716. {
  717. struct timeval now_time;
  718. do_gettimeofday(&now_time);
  719. if (Cal_timediff(&now_time, &g_NandLogTimer) > (500 * 1000)) { /* Dump per 100ms */
  720. pr_debug(" RPageCnt: %d (%lu us) RSubCnt: %d (%lu us) WPageCnt: %d (%lu us) ECnt: %d mtd(0/512/1K/2K/3K/4K): %d %d %d %d %d %d\n ",
  721. g_NandPerfLog.ReadPageCount,
  722. g_NandPerfLog.ReadPageCount ? (g_NandPerfLog.ReadPageTotalTime /
  723. g_NandPerfLog.ReadPageCount) : 0,
  724. g_NandPerfLog.ReadSubPageCount,
  725. g_NandPerfLog.ReadSubPageCount ? (g_NandPerfLog.ReadSubPageTotalTime /
  726. g_NandPerfLog.ReadSubPageCount) : 0,
  727. g_NandPerfLog.WritePageCount,
  728. g_NandPerfLog.WritePageCount ? (g_NandPerfLog.WritePageTotalTime /
  729. g_NandPerfLog.WritePageCount) : 0,
  730. g_NandPerfLog.EraseBlockCount, g_MtdPerfLog.read_size_0_512,
  731. g_MtdPerfLog.read_size_512_1K, g_MtdPerfLog.read_size_1K_2K,
  732. g_MtdPerfLog.read_size_2K_3K, g_MtdPerfLog.read_size_3K_4K,
  733. g_MtdPerfLog.read_size_Above_4K);
  734. memset(&g_NandPerfLog, 0x00, sizeof(g_NandPerfLog));
  735. memset(&g_MtdPerfLog, 0x00, sizeof(g_MtdPerfLog));
  736. do_gettimeofday(&g_NandLogTimer);
  737. }
  738. }
  739. #endif
  740. void dump_nfi(void)
  741. {
  742. #if __DEBUG_NAND
  743. pr_debug("~~~~Dump NFI Register in Kernel~~~~\n");
  744. pr_debug("NFI_CNFG_REG16: 0x%x\n", DRV_Reg16(NFI_CNFG_REG16));
  745. pr_debug("NFI_PAGEFMT_REG32: 0x%x\n", DRV_Reg32(NFI_PAGEFMT_REG32));
  746. pr_debug("NFI_CON_REG16: 0x%x\n", DRV_Reg16(NFI_CON_REG16));
  747. pr_debug("NFI_ACCCON_REG32: 0x%x\n", DRV_Reg32(NFI_ACCCON_REG32));
  748. pr_debug("NFI_INTR_EN_REG16: 0x%x\n", DRV_Reg16(NFI_INTR_EN_REG16));
  749. pr_debug("NFI_INTR_REG16: 0x%x\n", DRV_Reg16(NFI_INTR_REG16));
  750. pr_debug("NFI_CMD_REG16: 0x%x\n", DRV_Reg16(NFI_CMD_REG16));
  751. pr_debug("NFI_ADDRNOB_REG16: 0x%x\n", DRV_Reg16(NFI_ADDRNOB_REG16));
  752. pr_debug("NFI_COLADDR_REG32: 0x%x\n", DRV_Reg32(NFI_COLADDR_REG32));
  753. pr_debug("NFI_ROWADDR_REG32: 0x%x\n", DRV_Reg32(NFI_ROWADDR_REG32));
  754. pr_debug("NFI_STRDATA_REG16: 0x%x\n", DRV_Reg16(NFI_STRDATA_REG16));
  755. pr_debug("NFI_DATAW_REG32: 0x%x\n", DRV_Reg32(NFI_DATAW_REG32));
  756. pr_debug("NFI_DATAR_REG32: 0x%x\n", DRV_Reg32(NFI_DATAR_REG32));
  757. pr_debug("NFI_PIO_DIRDY_REG16: 0x%x\n", DRV_Reg16(NFI_PIO_DIRDY_REG16));
  758. pr_debug("NFI_STA_REG32: 0x%x\n", DRV_Reg32(NFI_STA_REG32));
  759. pr_debug("NFI_FIFOSTA_REG16: 0x%x\n", DRV_Reg16(NFI_FIFOSTA_REG16));
  760. /* pr_debug("NFI_LOCKSTA_REG16: 0x%x\n", DRV_Reg16(NFI_LOCKSTA_REG16)); */
  761. pr_debug("NFI_ADDRCNTR_REG16: 0x%x\n", DRV_Reg16(NFI_ADDRCNTR_REG16));
  762. pr_debug("NFI_STRADDR_REG32: 0x%x\n", DRV_Reg32(NFI_STRADDR_REG32));
  763. pr_debug("NFI_BYTELEN_REG16: 0x%x\n", DRV_Reg16(NFI_BYTELEN_REG16));
  764. pr_debug("NFI_CSEL_REG16: 0x%x\n", DRV_Reg16(NFI_CSEL_REG16));
  765. pr_debug("NFI_IOCON_REG16: 0x%x\n", DRV_Reg16(NFI_IOCON_REG16));
  766. pr_debug("NFI_FDM0L_REG32: 0x%x\n", DRV_Reg32(NFI_FDM0L_REG32));
  767. pr_debug("NFI_FDM0M_REG32: 0x%x\n", DRV_Reg32(NFI_FDM0M_REG32));
  768. pr_debug("NFI_LOCK_REG16: 0x%x\n", DRV_Reg16(NFI_LOCK_REG16));
  769. pr_debug("NFI_LOCKCON_REG32: 0x%x\n", DRV_Reg32(NFI_LOCKCON_REG32));
  770. pr_debug("NFI_LOCKANOB_REG16: 0x%x\n", DRV_Reg16(NFI_LOCKANOB_REG16));
  771. pr_debug("NFI_FIFODATA0_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA0_REG32));
  772. pr_debug("NFI_FIFODATA1_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA1_REG32));
  773. pr_debug("NFI_FIFODATA2_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA2_REG32));
  774. pr_debug("NFI_FIFODATA3_REG32: 0x%x\n", DRV_Reg32(NFI_FIFODATA3_REG32));
  775. pr_debug("NFI_MASTERSTA_REG16: 0x%x\n", DRV_Reg16(NFI_MASTERSTA_REG16));
  776. pr_debug("NFI_DEBUG_CON1_REG16: 0x%x\n", DRV_Reg16(NFI_DEBUG_CON1_REG16));
  777. pr_debug("ECC_ENCCON_REG16 :%x\n", *ECC_ENCCON_REG16);
  778. pr_debug("ECC_ENCCNFG_REG32 :%x\n", *ECC_ENCCNFG_REG32);
  779. pr_debug("ECC_ENCDIADDR_REG32 :%x\n", *ECC_ENCDIADDR_REG32);
  780. pr_debug("ECC_ENCIDLE_REG32 :%x\n", *ECC_ENCIDLE_REG32);
  781. pr_debug("ECC_ENCPAR0_REG32 :%x\n", *ECC_ENCPAR0_REG32);
  782. pr_debug("ECC_ENCPAR1_REG32 :%x\n", *ECC_ENCPAR1_REG32);
  783. pr_debug("ECC_ENCPAR2_REG32 :%x\n", *ECC_ENCPAR2_REG32);
  784. pr_debug("ECC_ENCPAR3_REG32 :%x\n", *ECC_ENCPAR3_REG32);
  785. pr_debug("ECC_ENCPAR4_REG32 :%x\n", *ECC_ENCPAR4_REG32);
  786. pr_debug("ECC_ENCPAR5_REG32 :%x\n", *ECC_ENCPAR5_REG32);
  787. pr_debug("ECC_ENCPAR6_REG32 :%x\n", *ECC_ENCPAR6_REG32);
  788. pr_debug("ECC_ENCSTA_REG32 :%x\n", *ECC_ENCSTA_REG32);
  789. pr_debug("ECC_ENCIRQEN_REG16 :%x\n", *ECC_ENCIRQEN_REG16);
  790. pr_debug("ECC_ENCIRQSTA_REG16 :%x\n", *ECC_ENCIRQSTA_REG16);
  791. pr_debug("ECC_DECCON_REG16 :%x\n", *ECC_DECCON_REG16);
  792. pr_debug("ECC_DECCNFG_REG32 :%x\n", *ECC_DECCNFG_REG32);
  793. pr_debug("ECC_DECDIADDR_REG32 :%x\n", *ECC_DECDIADDR_REG32);
  794. pr_debug("ECC_DECIDLE_REG16 :%x\n", *ECC_DECIDLE_REG16);
  795. pr_debug("ECC_DECFER_REG16 :%x\n", *ECC_DECFER_REG16);
  796. pr_debug("ECC_DECENUM0_REG32 :%x\n", *ECC_DECENUM0_REG32);
  797. pr_debug("ECC_DECENUM1_REG32 :%x\n", *ECC_DECENUM1_REG32);
  798. pr_debug("ECC_DECDONE_REG16 :%x\n", *ECC_DECDONE_REG16);
  799. pr_debug("ECC_DECEL0_REG32 :%x\n", *ECC_DECEL0_REG32);
  800. pr_debug("ECC_DECEL1_REG32 :%x\n", *ECC_DECEL1_REG32);
  801. pr_debug("ECC_DECEL2_REG32 :%x\n", *ECC_DECEL2_REG32);
  802. pr_debug("ECC_DECEL3_REG32 :%x\n", *ECC_DECEL3_REG32);
  803. pr_debug("ECC_DECEL4_REG32 :%x\n", *ECC_DECEL4_REG32);
  804. pr_debug("ECC_DECEL5_REG32 :%x\n", *ECC_DECEL5_REG32);
  805. pr_debug("ECC_DECEL6_REG32 :%x\n", *ECC_DECEL6_REG32);
  806. pr_debug("ECC_DECEL7_REG32 :%x\n", *ECC_DECEL7_REG32);
  807. pr_debug("ECC_DECIRQEN_REG16 :%x\n", *ECC_DECIRQEN_REG16);
  808. pr_debug("ECC_DECIRQSTA_REG16 :%x\n", *ECC_DECIRQSTA_REG16);
  809. pr_debug("ECC_DECFSM_REG32 :%x\n", *ECC_DECFSM_REG32);
  810. pr_debug("ECC_BYPASS_REG32 :%x\n", *ECC_BYPASS_REG32);
  811. /* pr_debug("NFI clock : %s\n",
  812. (DRV_Reg32((volatile u32 *)(PERICFG_BASE+0x18)) & (0x1))
  813. ? "Clock Disabled" : "Clock Enabled"); */
  814. /* pr_debug("NFI clock SEL (MT8127):0x%x: %s\n",
  815. (PERICFG_BASE+0x5C), (DRV_Reg32((volatile u32 *)(PERICFG_BASE+0x5C)) & (0x1))
  816. ? "Half clock" : "Quarter clock"); */
  817. #endif
  818. }
  819. u8 NFI_DMA_status(void)
  820. {
  821. return g_running_dma;
  822. }
  823. EXPORT_SYMBOL(NFI_DMA_status);
  824. u32 NFI_DMA_address(void)
  825. {
  826. return DRV_Reg32(NFI_STRADDR_REG32);
  827. }
  828. EXPORT_SYMBOL(NFI_DMA_address);
  829. unsigned long nand_virt_to_phys_add(unsigned long va)
  830. {
  831. unsigned long pageOffset = (va & (PAGE_SIZE - 1));
  832. pgd_t *pgd;
  833. pmd_t *pmd;
  834. pte_t *pte;
  835. unsigned long pa;
  836. /* pr_debug("[xl] v2p va 0x%lx\n", va); */
  837. if (virt_addr_valid(va))
  838. return __virt_to_phys(va);
  839. if (NULL == current) {
  840. pr_err("[nand_virt_to_phys_add] ERROR ,current is NULL!\n");
  841. return 0;
  842. }
  843. if (NULL == current->mm) {
  844. pr_err("[nand_virt_to_phys_add] ERROR current->mm is NULL! tgid=0x%x, name=%s\n",
  845. current->tgid, current->comm);
  846. return 0;
  847. }
  848. pgd = pgd_offset(current->mm, va); /* what is tsk->mm */
  849. if (pgd_none(*pgd) || pgd_bad(*pgd)) {
  850. pr_err("[nand_virt_to_phys_add] ERROR, va=0x%lx, pgd invalid!\n", va);
  851. return 0;
  852. }
  853. pmd = pmd_offset((pud_t *) pgd, va);
  854. if (pmd_none(*pmd) || pmd_bad(*pmd)) {
  855. pr_err("[nand_virt_to_phys_add] ERROR, va=0x%lx, pmd invalid!\n", va);
  856. return 0;
  857. }
  858. pte = pte_offset_map(pmd, va);
  859. if (pte_present(*pte)) {
  860. pa = (pte_val(*pte) & (PAGE_MASK)) | pageOffset;
  861. return pa;
  862. }
  863. pr_err("[nand_virt_to_phys_add] ERROR va=0x%lx, pte invalid!\n", va);
  864. return 0;
  865. }
  866. EXPORT_SYMBOL(nand_virt_to_phys_add);
  867. bool get_device_info(u8 *id, flashdev_info_t *devinfo)
  868. {
  869. u32 i, m, n, mismatch;
  870. int target = -1;
  871. u8 target_id_len = 0;
  872. for (i = 0; i < flash_number; i++) {
  873. mismatch = 0;
  874. for (m = 0; m < gen_FlashTable_p[i].id_length; m++) {
  875. if (id[m] != gen_FlashTable_p[i].id[m]) {
  876. mismatch = 1;
  877. break;
  878. }
  879. }
  880. if (mismatch == 0 && gen_FlashTable_p[i].id_length > target_id_len) {
  881. target = i;
  882. target_id_len = gen_FlashTable_p[i].id_length;
  883. }
  884. }
  885. if (target != -1) {
  886. pr_debug("Recognize NAND: ID [");
  887. for (n = 0; n < gen_FlashTable_p[target].id_length; n++) {
  888. devinfo->id[n] = gen_FlashTable_p[target].id[n];
  889. pr_debug("%x ", devinfo->id[n]);
  890. }
  891. pr_debug("], Device Name [%s], Page Size [%d]B Spare Size [%d]B Total Size [%d]MB\n",
  892. gen_FlashTable_p[target].devciename, gen_FlashTable_p[target].pagesize,
  893. gen_FlashTable_p[target].sparesize, gen_FlashTable_p[target].totalsize);
  894. devinfo->id_length = gen_FlashTable_p[target].id_length;
  895. devinfo->blocksize = gen_FlashTable_p[target].blocksize;
  896. devinfo->addr_cycle = gen_FlashTable_p[target].addr_cycle;
  897. devinfo->iowidth = gen_FlashTable_p[target].iowidth;
  898. devinfo->timmingsetting = gen_FlashTable_p[target].timmingsetting;
  899. devinfo->advancedmode = gen_FlashTable_p[target].advancedmode;
  900. devinfo->pagesize = gen_FlashTable_p[target].pagesize;
  901. devinfo->sparesize = gen_FlashTable_p[target].sparesize;
  902. devinfo->totalsize = gen_FlashTable_p[target].totalsize;
  903. devinfo->sectorsize = gen_FlashTable_p[target].sectorsize;
  904. devinfo->s_acccon = gen_FlashTable_p[target].s_acccon;
  905. devinfo->s_acccon1 = gen_FlashTable_p[target].s_acccon1;
  906. devinfo->freq = gen_FlashTable_p[target].freq;
  907. devinfo->vendor = gen_FlashTable_p[target].vendor;
  908. /* devinfo->ttarget = gen_FlashTable[target].ttarget; */
  909. memcpy((u8 *) &devinfo->feature_set, (u8 *) &gen_FlashTable_p[target].feature_set,
  910. sizeof(struct MLC_feature_set));
  911. memcpy(devinfo->devciename, gen_FlashTable_p[target].devciename,
  912. sizeof(devinfo->devciename));
  913. return true;
  914. }
  915. pr_err("Not Found NAND: ID [");
  916. for (n = 0; n < NAND_MAX_ID; n++)
  917. pr_err("%x ", id[n]);
  918. pr_err("]\n");
  919. return false;
  920. }
  921. #ifdef DUMP_NATIVE_BACKTRACE
  922. #define NFI_NATIVE_LOG_SD "/sdcard/NFI_native_log_%s-%02d-%02d-%02d_%02d-%02d-%02d.log"
  923. #define NFI_NATIVE_LOG_DATA "/data/NFI_native_log_%s-%02d-%02d-%02d_%02d-%02d-%02d.log"
  924. static int nfi_flush_log(char *s)
  925. {
  926. mm_segment_t old_fs;
  927. struct rtc_time tm;
  928. struct timeval tv = { 0 };
  929. struct file *filp = NULL;
  930. char name[256];
  931. unsigned int re = 0;
  932. int data_write = 0;
  933. do_gettimeofday(&tv);
  934. rtc_time_to_tm(tv.tv_sec, &tm);
  935. memset(name, 0, sizeof(name));
  936. sprintf(name, NFI_NATIVE_LOG_DATA, s, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  937. tm.tm_hour, tm.tm_min, tm.tm_sec);
  938. old_fs = get_fs();
  939. set_fs(KERNEL_DS);
  940. filp = filp_open(name, O_WRONLY | O_CREAT, 0777);
  941. if (IS_ERR(filp)) {
  942. pr_err("[NFI_flush_log]error create file in %s, IS_ERR:%ld, PTR_ERR:%ld\n", name,
  943. IS_ERR(filp), PTR_ERR(filp));
  944. memset(name, 0, sizeof(name));
  945. sprintf(name, NFI_NATIVE_LOG_SD, s, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  946. tm.tm_hour, tm.tm_min, tm.tm_sec);
  947. filp = filp_open(name, O_WRONLY | O_CREAT, 0777);
  948. if (IS_ERR(filp)) {
  949. pr_err("[NFI_flush_log]error create file in %s, IS_ERR:%ld, PTR_ERR:%ld\n",
  950. name, IS_ERR(filp), PTR_ERR(filp));
  951. set_fs(old_fs);
  952. return -1;
  953. }
  954. }
  955. pr_debug("[NFI_flush_log]log file:%s\n", name);
  956. set_fs(old_fs);
  957. if (!(filp->f_op) || !(filp->f_op->write)) {
  958. pr_debug("[NFI_flush_log] No operation\n");
  959. re = -1;
  960. goto ClOSE_FILE;
  961. }
  962. DumpNativeInfo();
  963. old_fs = get_fs();
  964. set_fs(KERNEL_DS);
  965. data_write = vfs_write(filp, (char __user *)NativeInfo, strlen(NativeInfo), &filp->f_pos);
  966. if (!data_write) {
  967. pr_err("[nfi_flush_log] write fail\n");
  968. re = -1;
  969. }
  970. set_fs(old_fs);
  971. ClOSE_FILE:
  972. if (filp) {
  973. filp_close(filp, current->files);
  974. filp = NULL;
  975. }
  976. return re;
  977. }
  978. #endif
  979. /* extern bool MLC_DEVICE; */
  980. static bool mtk_nand_reset(void);
  981. u32 mtk_nand_page_transform(struct mtd_info *mtd, struct nand_chip *chip, u32 page, u32 *blk,
  982. u32 *map_blk)
  983. {
  984. u32 block_size = 1 << (chip->phys_erase_shift);
  985. u32 page_size = (1 << chip->page_shift);
  986. loff_t start_address;
  987. u32 idx;
  988. u32 block;
  989. u32 page_in_block;
  990. u32 mapped_block;
  991. bool translate = FALSE;
  992. loff_t logical_address = (loff_t) page * (1 << chip->page_shift);
  993. /* MSG(INIT , "[BEAN]%d, %x\n",page,logical_address); */
  994. if (MLC_DEVICE) {
  995. start_address = part_get_startaddress(logical_address, &idx);
  996. /* MSG(INIT , "[start_address]page = 0x%x, start_address=0x%lx\n",page,start_address); */
  997. if (raw_partition(idx))
  998. translate = TRUE;
  999. else
  1000. translate = FALSE;
  1001. }
  1002. if (translate == TRUE) {
  1003. block = (u32) ((u32) (start_address >> chip->phys_erase_shift) +
  1004. (u32) ((logical_address - start_address) >> (chip->phys_erase_shift - 1)));
  1005. page_in_block = ((u32) ((logical_address - start_address) >> chip->page_shift) %
  1006. ((mtd->erasesize / page_size) / 2));
  1007. /* MSG(INIT , "[LOW]0x%x, 0x%x\n",block,page_in_block); */
  1008. if (devinfo.vendor != VEND_NONE) {
  1009. /* page_in_block = devinfo.feature_set.PairPage[page_in_block]; */
  1010. page_in_block = functArray[devinfo.feature_set.ptbl_idx] (page_in_block);
  1011. }
  1012. mapped_block = get_mapping_block_index(block);
  1013. /* MSG(INIT , "[page_in_block]mapped_block=%d, page_in_block=%d\n",mapped_block,page_in_block); */
  1014. *blk = block;
  1015. *map_blk = mapped_block;
  1016. return page_in_block;
  1017. }
  1018. block = page / (block_size / page_size);
  1019. mapped_block = get_mapping_block_index(block);
  1020. page_in_block = page % (block_size / page_size);
  1021. /* MSG(INIT , "[FULL]0x%x, 0x%x 0x%x 0x%x\n",
  1022. block,page_in_block,mapped_block, page_in_block+mapped_block*(block_size/page_size)); */
  1023. *blk = block;
  1024. *map_blk = mapped_block;
  1025. return page_in_block;
  1026. }
  1027. bool mtk_nand_IsRawPartition(loff_t logical_address)
  1028. {
  1029. u32 idx;
  1030. part_get_startaddress(logical_address, &idx);
  1031. if (raw_partition(idx))
  1032. return true;
  1033. else
  1034. return false;
  1035. }
  1036. static int mtk_nand_interface_config(struct mtd_info *mtd)
  1037. {
  1038. u32 timeout;
  1039. u32 val;
  1040. u32 acccon1;
  1041. struct gFeatureSet *feature_set = &(devinfo.feature_set.FeatureSet);
  1042. /* int clksrc = ARMPLL; */
  1043. if (devinfo.iowidth == IO_ONFI || devinfo.iowidth == IO_TOGGLEDDR
  1044. || devinfo.iowidth == IO_TOGGLESDR) {
  1045. nand_enable_clock();
  1046. #ifndef CONFIG_MTK_FPGA
  1047. /* 0:26M 1:182M 2:156M 3:124.8M 4:91M 5:62.4M 6:39M 7:26M */
  1048. if (devinfo.freq == 80) { /* mode 4 */
  1049. #ifdef CONFIG_MTK_LEGACY
  1050. g_iNFI2X_CLKSRC = MSDCPLL; /* 156M */
  1051. #else
  1052. g_iNFI2X_CLKSRC = 2; /* 156M */
  1053. #endif
  1054. } else if (devinfo.freq == 100) { /* mode 5 */
  1055. #ifdef CONFIG_MTK_LEGACY
  1056. g_iNFI2X_CLKSRC = MAINPLL; /* 182M */
  1057. #else
  1058. g_iNFI2X_CLKSRC = 1; /* 182M */
  1059. #endif
  1060. }
  1061. #endif
  1062. /* reset */
  1063. /* pr_debug("[Bean]mode:%d\n", g_iNFI2X_CLKSRC); */
  1064. NFI_ISSUE_COMMAND(NAND_CMD_RESET, 0, 0, 0, 0);
  1065. timeout = TIMEOUT_4;
  1066. while (timeout)
  1067. timeout--;
  1068. mtk_nand_reset();
  1069. /* set feature */
  1070. /* pr_debug("[Interface Config]cmd:0x%X addr:0x%x feature:0x%x\n", */
  1071. /* feature_set->sfeatureCmd, feature_set->Interface.address, feature_set->Interface.feature); */
  1072. /* mtk_nand_GetFeature(mtd, feature_set->gfeatureCmd, \ */
  1073. /* feature_set->Interface.address, &val,4); */
  1074. /* pr_debug("[Interface]0x%X\n", val); */
  1075. mtk_nand_SetFeature(mtd, (u16) feature_set->sfeatureCmd,
  1076. feature_set->Interface.address,
  1077. (u8 *) &feature_set->Interface.feature,
  1078. sizeof(feature_set->Interface.feature));
  1079. mb();
  1080. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16, HWDCM_SWCON_ON);
  1081. /* setup register */
  1082. mb();
  1083. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16, NFI_BYPASS);
  1084. /* clear bypass of ecc */
  1085. mb();
  1086. NFI_CLN_REG32(ECC_BYPASS_REG32, ECC_BYPASS);
  1087. mb();
  1088. #ifndef CONFIG_MTK_FPGA
  1089. /* DRV_WriteReg32(PERICFG_BASE+0x5C, 0x0); // setting default AHB clock */
  1090. /* MSG(INIT, "AHB Clock(0x%x)\n",DRV_Reg32(PERICFG_BASE+0x5C)); */
  1091. mb();
  1092. #if defined(CONFIG_MTK_LEGACY)
  1093. NFI_SET_REG32(PERI_NFI_CLK_SOURCE_SEL, NFI_PAD_1X_CLOCK);
  1094. #else
  1095. clk_set_parent(infra_nfi_sel_clock, onfi_d2_clock);
  1096. #endif
  1097. mb();
  1098. #if defined(CONFIG_MTK_LEGACY)
  1099. clkmux_sel(MT_MUX_ONFI, g_iNFI2X_CLKSRC, "NFI");
  1100. #else
  1101. if (g_iNFI2X_CLKSRC == 1)
  1102. clk_set_parent(onfi_sel_clock, syspll2_d2_clock);
  1103. else if (g_iNFI2X_CLKSRC == 2)
  1104. clk_set_parent(onfi_sel_clock, syspll_d7_clock);
  1105. #endif
  1106. mb();
  1107. #endif
  1108. DRV_WriteReg32(NFI_DLYCTRL_REG32, 0x64011);
  1109. #ifndef CONFIG_MTK_FPGA
  1110. /* DRV_WriteReg32(PERI_NFI_MAC_CTRL, 0x10006); */
  1111. #endif
  1112. while (0 == (DRV_Reg32(NFI_STA_REG32) && STA_FLASH_MACRO_IDLE))
  1113. ;
  1114. if (devinfo.iowidth == IO_ONFI)
  1115. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 2); /* ONFI */
  1116. else
  1117. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 1); /* Toggle */
  1118. /* pr_debug("[Timing]0x%x 0x%x\n", devinfo.s_acccon, devinfo.s_acccon1); */
  1119. acccon1 = DRV_Reg32(NFI_ACCCON1_REG3);
  1120. DRV_WriteReg32(NFI_ACCCON1_REG3, devinfo.s_acccon1);
  1121. DRV_WriteReg32(NFI_ACCCON_REG32, devinfo.s_acccon);
  1122. /* read back confirm */
  1123. mtk_nand_GetFeature(mtd, feature_set->gfeatureCmd,
  1124. feature_set->Interface.address, (u8 *) &val, 4);
  1125. /* pr_debug("[Bean]feature is %x\n", val); */
  1126. if ((val & 0xFF) != (feature_set->Interface.feature & 0xFF)) {
  1127. pr_err("[%s] fail 0x%X\n", __func__, val);
  1128. NFI_ISSUE_COMMAND(NAND_CMD_RESET, 0, 0, 0, 0); /* ASYNC */
  1129. timeout = TIMEOUT_4;
  1130. while (timeout)
  1131. timeout--;
  1132. mtk_nand_reset();
  1133. #ifndef CONFIG_MTK_FPGA
  1134. #if defined(CONFIG_MTK_LEGACY)
  1135. clkmux_sel(MT_MUX_ONFI, MAINPLL, "NFI"); /* 182M */
  1136. #else
  1137. clk_set_parent(onfi_sel_clock, syspll2_d2_clock);
  1138. #endif
  1139. #endif
  1140. NFI_SET_REG32(NFI_DEBUG_CON1_REG16, NFI_BYPASS);
  1141. NFI_SET_REG32(ECC_BYPASS_REG32, ECC_BYPASS);
  1142. #ifndef CONFIG_MTK_FPGA
  1143. #if defined(CONFIG_MTK_LEGACY)
  1144. NFI_CLN_REG32(PERI_NFI_CLK_SOURCE_SEL, NFI_PAD_1X_CLOCK);
  1145. #else
  1146. clk_set_parent(infra_nfi_sel_clock, axi_sel_clock);
  1147. #endif
  1148. /* DRV_WriteReg32(PERICFG_BASE+0x5C, 0x1); // setting AHB clock */
  1149. /* MSG(INIT, "AHB Clock(0x%x)\n",DRV_Reg32(PERICFG_BASE+0x5C)); */
  1150. #endif
  1151. DRV_WriteReg32(NFI_ACCCON1_REG3, acccon1);
  1152. DRV_WriteReg32(NFI_ACCCON_REG32, devinfo.timmingsetting);
  1153. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32, 0); /* Legacy */
  1154. g_bSyncOrToggle = false;
  1155. return 0;
  1156. }
  1157. g_bSyncOrToggle = true;
  1158. pr_notice("[%s] success 0x%X\n", __func__, devinfo.iowidth);
  1159. /* extern void log_boot(char *str); */
  1160. /* log_boot("[Bean]sync mode success!"); */
  1161. } else {
  1162. g_bSyncOrToggle = false;
  1163. pr_notice("[%s] legacy interface\n", __func__);
  1164. return 0;
  1165. }
  1166. return 1;
  1167. }
  1168. #if CFG_RANDOMIZER
  1169. static int mtk_nand_turn_on_randomizer(u32 page, int type, int fgPage)
  1170. {
  1171. u32 u4NFI_CFG = 0;
  1172. u32 u4NFI_RAN_CFG = 0;
  1173. u4NFI_CFG = DRV_Reg32(NFI_CNFG_REG16);
  1174. DRV_WriteReg32(NFI_ENMPTY_THRESH_REG32, 40); /* empty threshold 40 */
  1175. if (type) { /* encode */
  1176. DRV_WriteReg32(NFI_RANDOM_ENSEED01_TS_REG32, 0);
  1177. DRV_WriteReg32(NFI_RANDOM_ENSEED02_TS_REG32, 0);
  1178. DRV_WriteReg32(NFI_RANDOM_ENSEED03_TS_REG32, 0);
  1179. DRV_WriteReg32(NFI_RANDOM_ENSEED04_TS_REG32, 0);
  1180. DRV_WriteReg32(NFI_RANDOM_ENSEED05_TS_REG32, 0);
  1181. DRV_WriteReg32(NFI_RANDOM_ENSEED06_TS_REG32, 0);
  1182. } else {
  1183. DRV_WriteReg32(NFI_RANDOM_DESEED01_TS_REG32, 0);
  1184. DRV_WriteReg32(NFI_RANDOM_DESEED02_TS_REG32, 0);
  1185. DRV_WriteReg32(NFI_RANDOM_DESEED03_TS_REG32, 0);
  1186. DRV_WriteReg32(NFI_RANDOM_DESEED04_TS_REG32, 0);
  1187. DRV_WriteReg32(NFI_RANDOM_DESEED05_TS_REG32, 0);
  1188. DRV_WriteReg32(NFI_RANDOM_DESEED06_TS_REG32, 0);
  1189. }
  1190. u4NFI_CFG |= CNFG_RAN_SEL;
  1191. if (PAGES_PER_BLOCK <= SS_SEED_NUM) {
  1192. if (type) {
  1193. u4NFI_RAN_CFG |=
  1194. RAN_CNFG_ENCODE_SEED(SS_RANDOM_SEED[page & (PAGES_PER_BLOCK - 1)]) |
  1195. RAN_CNFG_ENCODE_EN;
  1196. } else {
  1197. u4NFI_RAN_CFG |=
  1198. RAN_CNFG_DECODE_SEED(SS_RANDOM_SEED[page & (PAGES_PER_BLOCK - 1)]) |
  1199. RAN_CNFG_DECODE_EN;
  1200. }
  1201. } else {
  1202. if (type) {
  1203. u4NFI_RAN_CFG |=
  1204. RAN_CNFG_ENCODE_SEED(SS_RANDOM_SEED[page & (SS_SEED_NUM - 1)]) |
  1205. RAN_CNFG_ENCODE_EN;
  1206. } else {
  1207. u4NFI_RAN_CFG |=
  1208. RAN_CNFG_DECODE_SEED(SS_RANDOM_SEED[page & (SS_SEED_NUM - 1)]) |
  1209. RAN_CNFG_DECODE_EN;
  1210. }
  1211. }
  1212. if (fgPage) /* reload seed for each page */
  1213. u4NFI_CFG &= ~CNFG_RAN_SEC;
  1214. else /* reload seed for each sector */
  1215. u4NFI_CFG |= CNFG_RAN_SEC;
  1216. DRV_WriteReg32(NFI_CNFG_REG16, u4NFI_CFG);
  1217. DRV_WriteReg32(NFI_RANDOM_CNFG_REG32, u4NFI_RAN_CFG);
  1218. /* MSG(INIT, "[K]ran turn on type:%d 0x%x 0x%x\n", type, DRV_Reg32(NFI_RANDOM_CNFG_REG32), page); */
  1219. return 0;
  1220. }
  1221. static bool mtk_nand_israndomizeron(void)
  1222. {
  1223. u32 nfi_ran_cnfg = 0;
  1224. nfi_ran_cnfg = DRV_Reg32(NFI_RANDOM_CNFG_REG32);
  1225. if (nfi_ran_cnfg & (RAN_CNFG_ENCODE_EN | RAN_CNFG_DECODE_EN))
  1226. return TRUE;
  1227. return FALSE;
  1228. }
  1229. static void mtk_nand_turn_off_randomizer(void)
  1230. {
  1231. u32 u4NFI_CFG = DRV_Reg32(NFI_CNFG_REG16);
  1232. u4NFI_CFG &= ~CNFG_RAN_SEL;
  1233. u4NFI_CFG &= ~CNFG_RAN_SEC;
  1234. DRV_WriteReg32(NFI_RANDOM_CNFG_REG32, 0);
  1235. DRV_WriteReg32(NFI_CNFG_REG16, u4NFI_CFG);
  1236. /* MSG(INIT, "[K]ran turn off\n"); */
  1237. }
  1238. #else
  1239. #define mtk_nand_israndomizeron() (FALSE)
  1240. #define mtk_nand_turn_on_randomizer(page, type, fgPage)
  1241. #define mtk_nand_turn_off_randomizer()
  1242. #endif
  1243. /******************************************************************************
  1244. * mtk_nand_irq_handler
  1245. *
  1246. * DESCRIPTION:
  1247. * NAND interrupt handler!
  1248. *
  1249. * PARAMETERS:
  1250. * int irq
  1251. * void *dev_id
  1252. *
  1253. * RETURNS:
  1254. * IRQ_HANDLED : Successfully handle the IRQ
  1255. *
  1256. * NOTES:
  1257. * None
  1258. *
  1259. ******************************************************************************/
  1260. /* Modified for TCM used */
  1261. static irqreturn_t mtk_nand_irq_handler(int irqno, void *dev_id)
  1262. {
  1263. u16 u16IntStatus = DRV_Reg16(NFI_INTR_REG16);
  1264. (void)irqno;
  1265. if (u16IntStatus & (u16) INTR_AHB_DONE_EN)
  1266. complete(&g_comp_AHB_Done);
  1267. return IRQ_HANDLED;
  1268. }
  1269. /******************************************************************************
  1270. * ECC_Config
  1271. *
  1272. * DESCRIPTION:
  1273. * Configure HW ECC!
  1274. *
  1275. * PARAMETERS:
  1276. * struct mtk_nand_host_hw *hw
  1277. *
  1278. * RETURNS:
  1279. * None
  1280. *
  1281. * NOTES:
  1282. * None
  1283. *
  1284. ******************************************************************************/
  1285. static void ECC_Config(struct mtk_nand_host_hw *hw, u32 ecc_bit)
  1286. {
  1287. u32 u4ENCODESize;
  1288. u32 u4DECODESize;
  1289. u32 ecc_bit_cfg = ECC_CNFG_ECC4;
  1290. switch (ecc_bit) {
  1291. #ifndef MTK_COMBO_NAND_SUPPORT
  1292. case 4:
  1293. ecc_bit_cfg = ECC_CNFG_ECC4;
  1294. break;
  1295. case 8:
  1296. ecc_bit_cfg = ECC_CNFG_ECC8;
  1297. break;
  1298. case 10:
  1299. ecc_bit_cfg = ECC_CNFG_ECC10;
  1300. break;
  1301. case 12:
  1302. ecc_bit_cfg = ECC_CNFG_ECC12;
  1303. break;
  1304. case 14:
  1305. ecc_bit_cfg = ECC_CNFG_ECC14;
  1306. break;
  1307. case 16:
  1308. ecc_bit_cfg = ECC_CNFG_ECC16;
  1309. break;
  1310. case 18:
  1311. ecc_bit_cfg = ECC_CNFG_ECC18;
  1312. break;
  1313. case 20:
  1314. ecc_bit_cfg = ECC_CNFG_ECC20;
  1315. break;
  1316. case 22:
  1317. ecc_bit_cfg = ECC_CNFG_ECC22;
  1318. break;
  1319. case 24:
  1320. ecc_bit_cfg = ECC_CNFG_ECC24;
  1321. break;
  1322. #endif
  1323. case 28:
  1324. ecc_bit_cfg = ECC_CNFG_ECC28;
  1325. break;
  1326. case 32:
  1327. ecc_bit_cfg = ECC_CNFG_ECC32;
  1328. break;
  1329. case 36:
  1330. ecc_bit_cfg = ECC_CNFG_ECC36;
  1331. break;
  1332. case 40:
  1333. ecc_bit_cfg = ECC_CNFG_ECC40;
  1334. break;
  1335. case 44:
  1336. ecc_bit_cfg = ECC_CNFG_ECC44;
  1337. break;
  1338. case 48:
  1339. ecc_bit_cfg = ECC_CNFG_ECC48;
  1340. break;
  1341. case 52:
  1342. ecc_bit_cfg = ECC_CNFG_ECC52;
  1343. break;
  1344. case 56:
  1345. ecc_bit_cfg = ECC_CNFG_ECC56;
  1346. break;
  1347. case 60:
  1348. ecc_bit_cfg = ECC_CNFG_ECC60;
  1349. break;
  1350. default:
  1351. break;
  1352. }
  1353. DRV_WriteReg16(ECC_DECCON_REG16, DEC_DE);
  1354. do {
  1355. ;
  1356. } while (!DRV_Reg16(ECC_DECIDLE_REG16));
  1357. DRV_WriteReg16(ECC_ENCCON_REG16, ENC_DE);
  1358. do {
  1359. ;
  1360. } while (!DRV_Reg32(ECC_ENCIDLE_REG32));
  1361. /* setup FDM register base */
  1362. /* DRV_WriteReg32(ECC_FDMADDR_REG32, NFI_FDM0L_REG32); */
  1363. /* Sector + FDM */
  1364. u4ENCODESize = (hw->nand_sec_size + 8) << 3;
  1365. /* Sector + FDM + YAFFS2 meta data bits */
  1366. u4DECODESize = ((hw->nand_sec_size + 8) << 3) + ecc_bit * ECC_PARITY_BIT;
  1367. /* configure ECC decoder && encoder */
  1368. DRV_WriteReg32(ECC_DECCNFG_REG32,
  1369. ecc_bit_cfg | DEC_CNFG_NFI | DEC_CNFG_EMPTY_EN | (u4DECODESize <<
  1370. DEC_CNFG_CODE_SHIFT));
  1371. DRV_WriteReg32(ECC_ENCCNFG_REG32,
  1372. ecc_bit_cfg | ENC_CNFG_NFI | (u4ENCODESize << ENC_CNFG_MSG_SHIFT));
  1373. #ifndef MANUAL_CORRECT
  1374. NFI_SET_REG32(ECC_DECCNFG_REG32, DEC_CNFG_CORRECT);
  1375. #else
  1376. NFI_SET_REG32(ECC_DECCNFG_REG32, DEC_CNFG_EL);
  1377. #endif
  1378. }
  1379. /******************************************************************************
  1380. * ECC_Decode_Start
  1381. *
  1382. * DESCRIPTION:
  1383. * HW ECC Decode Start !
  1384. *
  1385. * PARAMETERS:
  1386. * None
  1387. *
  1388. * RETURNS:
  1389. * None
  1390. *
  1391. * NOTES:
  1392. * None
  1393. *
  1394. ******************************************************************************/
  1395. static void ECC_Decode_Start(void)
  1396. {
  1397. /* wait for device returning idle */
  1398. while (!(DRV_Reg16(ECC_DECIDLE_REG16) & DEC_IDLE))
  1399. ;
  1400. DRV_WriteReg16(ECC_DECCON_REG16, DEC_EN);
  1401. }
  1402. /******************************************************************************
  1403. * ECC_Decode_End
  1404. *
  1405. * DESCRIPTION:
  1406. * HW ECC Decode End !
  1407. *
  1408. * PARAMETERS:
  1409. * None
  1410. *
  1411. * RETURNS:
  1412. * None
  1413. *
  1414. * NOTES:
  1415. * None
  1416. *
  1417. ******************************************************************************/
  1418. static void ECC_Decode_End(void)
  1419. {
  1420. /* wait for device returning idle */
  1421. while (!(DRV_Reg16(ECC_DECIDLE_REG16) & DEC_IDLE))
  1422. ;
  1423. DRV_WriteReg16(ECC_DECCON_REG16, DEC_DE);
  1424. }
  1425. /******************************************************************************
  1426. * ECC_Encode_Start
  1427. *
  1428. * DESCRIPTION:
  1429. * HW ECC Encode Start !
  1430. *
  1431. * PARAMETERS:
  1432. * None
  1433. *
  1434. * RETURNS:
  1435. * None
  1436. *
  1437. * NOTES:
  1438. * None
  1439. *
  1440. ******************************************************************************/
  1441. static void ECC_Encode_Start(void)
  1442. {
  1443. /* wait for device returning idle */
  1444. while (!(DRV_Reg32(ECC_ENCIDLE_REG32) & ENC_IDLE))
  1445. ;
  1446. mb();
  1447. DRV_WriteReg16(ECC_ENCCON_REG16, ENC_EN);
  1448. }
  1449. /******************************************************************************
  1450. * ECC_Encode_End
  1451. *
  1452. * DESCRIPTION:
  1453. * HW ECC Encode End !
  1454. *
  1455. * PARAMETERS:
  1456. * None
  1457. *
  1458. * RETURNS:
  1459. * None
  1460. *
  1461. * NOTES:
  1462. * None
  1463. *
  1464. ******************************************************************************/
  1465. static void ECC_Encode_End(void)
  1466. {
  1467. /* wait for device returning idle */
  1468. while (!(DRV_Reg32(ECC_ENCIDLE_REG32) & ENC_IDLE))
  1469. ;
  1470. mb();
  1471. DRV_WriteReg16(ECC_ENCCON_REG16, ENC_DE);
  1472. }
  1473. /******************************************************************************
  1474. * mtk_nand_check_bch_error
  1475. *
  1476. * DESCRIPTION:
  1477. * Check BCH error or not !
  1478. *
  1479. * PARAMETERS:
  1480. * struct mtd_info *mtd
  1481. * u8* pDataBuf
  1482. * u32 u4SecIndex
  1483. * u32 u4PageAddr
  1484. *
  1485. * RETURNS:
  1486. * None
  1487. *
  1488. * NOTES:
  1489. * None
  1490. *
  1491. ******************************************************************************/
  1492. static bool mtk_nand_check_bch_error(struct mtd_info *mtd, u8 *pDataBuf, u8 *spareBuf,
  1493. u32 u4SecIndex, u32 u4PageAddr, u32 *bitmap)
  1494. {
  1495. bool ret = true;
  1496. u16 u2SectorDoneMask = 1 << u4SecIndex;
  1497. u32 u4ErrorNumDebug0, u4ErrorNumDebug1, i, u4ErrNum;
  1498. #ifdef MANUAL_CORRECT
  1499. u32 j;
  1500. #endif
  1501. u32 timeout = 0xFFFF;
  1502. u32 correct_count = 0;
  1503. u32 page_size = (u4SecIndex + 1) * host->hw->nand_sec_size;
  1504. u32 sec_num = u4SecIndex + 1;
  1505. /* u32 bitflips = sec_num * 39; */
  1506. u16 failed_sec = 0;
  1507. u32 maxSectorBitErr = 0;
  1508. #ifdef MANUAL_CORRECT
  1509. u32 index1, err_pos, temp;
  1510. u32 au4ErrBitLoc[20];
  1511. u32 u4ErrByteLoc, u4BitOffset;
  1512. u32 u4ErrBitLoc1th, u4ErrBitLoc2nd;
  1513. #endif
  1514. while (0 == (u2SectorDoneMask & DRV_Reg16(ECC_DECDONE_REG16))) {
  1515. timeout--;
  1516. if (0 == timeout)
  1517. return false;
  1518. }
  1519. #ifndef MANUAL_CORRECT
  1520. if (0 == (DRV_Reg32(NFI_STA_REG32) & STA_READ_EMPTY)) {
  1521. u4ErrorNumDebug0 = DRV_Reg32(ECC_DECENUM0_REG32);
  1522. u4ErrorNumDebug1 = DRV_Reg32(ECC_DECENUM1_REG32);
  1523. if (0 != (u4ErrorNumDebug0 & 0xFFFFFFFF) || 0 != (u4ErrorNumDebug1 & 0xFFFFFFFF)) {
  1524. for (i = 0; i <= u4SecIndex; ++i) {
  1525. #if 1
  1526. u4ErrNum = (DRV_Reg32((ECC_DECENUM0_REG32 + (i / 4))) >> ((i % 4) * 8)) & ERR_NUM0;
  1527. #else
  1528. if (i < 4)
  1529. u4ErrNum = DRV_Reg32(ECC_DECENUM0_REG32) >> (i * 8);
  1530. else
  1531. u4ErrNum = DRV_Reg32(ECC_DECENUM1_REG32) >> ((i - 4) * 8);
  1532. u4ErrNum &= ERR_NUM0;
  1533. #endif
  1534. /* pr_debug("[XL] errnm %d, sec %d\n", u4ErrNum, i); */
  1535. /* for (index1 = 0; index1 < ((u4ErrNum + 1) >> 1); ++index1) */
  1536. /* { */
  1537. /* au4ErrBitLoc[index1] = DRV_Reg32(ECC_DECEL0_REG32 + index1); */
  1538. /* u4ErrBitLoc1th = au4ErrBitLoc[index1] & 0x3FFF; */
  1539. /* u4ErrBitLoc2nd = (au4ErrBitLoc[index1] >> 16) & 0x3FFF; */
  1540. /* pr_debug("[XL] EL%d = 0x%x EL%d = 0x%x\n",
  1541. i*2, u4ErrBitLoc1th, i*2 + 1, u4ErrBitLoc2nd); */
  1542. /* } */
  1543. if (ERR_NUM0 == u4ErrNum) {
  1544. failed_sec++;
  1545. ret = false;
  1546. pr_debug("UnCorrectable ECC errors at PageAddr=%d, Sector=%d\n", u4PageAddr, i);
  1547. continue;
  1548. }
  1549. if (bitmap)
  1550. *bitmap |= 1 << i;
  1551. if (u4ErrNum) {
  1552. if (maxSectorBitErr < u4ErrNum)
  1553. maxSectorBitErr = u4ErrNum;
  1554. correct_count += u4ErrNum;
  1555. }
  1556. }
  1557. mtd->ecc_stats.failed += failed_sec;
  1558. if ((maxSectorBitErr > ecc_threshold) && (FALSE != ret)) {
  1559. pr_debug("ECC bit flips (0x%x) exceed eccthreshold (0x%x),u4PageAddr 0x%x\n",
  1560. maxSectorBitErr, ecc_threshold, u4PageAddr);
  1561. mtd->ecc_stats.corrected++;
  1562. }
  1563. }
  1564. }
  1565. if (0 != (DRV_Reg32(NFI_STA_REG32) & STA_READ_EMPTY)) {
  1566. ret = true;
  1567. /* MSG(INIT, "empty page, empty buffer returned\n"); */
  1568. memset(pDataBuf, 0xff, page_size);
  1569. memset(spareBuf, 0xff, sec_num * 8);
  1570. maxSectorBitErr = 0;
  1571. failed_sec = 0;
  1572. }
  1573. #else
  1574. for (j = 0; j <= u4SecIndex; ++j) {
  1575. u4ErrNum = (DRV_Reg32((ECC_DECENUM0_REG32 + (j / 4))) >> ((j % 4) * 8)) & ERR_NUM0;
  1576. /* We will manually correct the error bits in the last sector, not all the sectors of the page! */
  1577. memset(au4ErrBitLoc, 0x0, sizeof(au4ErrBitLoc));
  1578. /* u4ErrorNumDebug = DRV_Reg32(ECC_DECENUM_REG32); */
  1579. /* u4ErrNum = (DRV_Reg32((ECC_DECENUM_REG32+(u4SecIndex/4)))>>((u4SecIndex%4)*8))& ERR_NUM0; */
  1580. /* pr_debug("[XL1] errnm %d, sec %d\n", u4ErrNum, j); */
  1581. if (u4ErrNum) {
  1582. if (ERR_NUM0 == u4ErrNum) {
  1583. mtd->ecc_stats.failed++;
  1584. ret = false;
  1585. pr_debug("UnCorrectable at PageAddr=%d\n", u4PageAddr);
  1586. continue;
  1587. }
  1588. for (i = 0; i < ((u4ErrNum + 1) >> 1); i++) {
  1589. /* get error location */
  1590. au4ErrBitLoc[i] = DRV_Reg32(ECC_DECEL0_REG32 + i);
  1591. /* pr_debug("[XL1] errloc[%d] 0x%x\n", i,au4ErrBitLoc[i]); */
  1592. }
  1593. for (i = 0; i < u4ErrNum; i++) {
  1594. /* MCU error correction */
  1595. err_pos = ((au4ErrBitLoc[i >> 1] >> ((i & 0x01) << 4)) & 0x3FFF);
  1596. /* *(data_buff+(err_pos>>3)) ^= (1<<(err_pos&0x7)); */
  1597. u4ErrByteLoc = err_pos >> 3;
  1598. if (u4ErrByteLoc < host->hw->nand_sec_size) {
  1599. pDataBuf[host->hw->nand_sec_size * j + u4ErrByteLoc] ^=
  1600. (1 << (err_pos & 0x7));
  1601. continue;
  1602. }
  1603. /* BytePos is in FDM data and auto-format. */
  1604. u4ErrByteLoc -= host->hw->nand_sec_size;
  1605. if (u4ErrByteLoc < 8) { /* fdm size */
  1606. if (u4ErrByteLoc >= 4) {
  1607. temp = DRV_Reg32(NFI_FDM0M_REG32 + (j << 1));
  1608. u4ErrByteLoc -= 4;
  1609. temp ^= (1 << ((err_pos & 0x7) + (u4ErrByteLoc << 3)));
  1610. DRV_WriteReg32(NFI_FDM0M_REG32 + (j << 1), temp);
  1611. } else {
  1612. temp = DRV_Reg32(NFI_FDM0L_REG32 + (j << 1));
  1613. temp ^= (1 << ((err_pos & 0x7) + (u4ErrByteLoc << 3)));
  1614. DRV_WriteReg32(NFI_FDM0L_REG32 + (j << 1), temp);
  1615. }
  1616. }
  1617. }
  1618. mtd->ecc_stats.corrected++;
  1619. }
  1620. }
  1621. #endif
  1622. return ret;
  1623. }
  1624. /******************************************************************************
  1625. * mtk_nand_RFIFOValidSize
  1626. *
  1627. * DESCRIPTION:
  1628. * Check the Read FIFO data bytes !
  1629. *
  1630. * PARAMETERS:
  1631. * u16 u2Size
  1632. *
  1633. * RETURNS:
  1634. * None
  1635. *
  1636. * NOTES:
  1637. * None
  1638. *
  1639. ******************************************************************************/
  1640. static bool mtk_nand_RFIFOValidSize(u16 u2Size)
  1641. {
  1642. u32 timeout = 0xFFFF;
  1643. while (FIFO_RD_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) < u2Size) {
  1644. timeout--;
  1645. if (0 == timeout)
  1646. return false;
  1647. }
  1648. return true;
  1649. }
  1650. /******************************************************************************
  1651. * mtk_nand_WFIFOValidSize
  1652. *
  1653. * DESCRIPTION:
  1654. * Check the Write FIFO data bytes !
  1655. *
  1656. * PARAMETERS:
  1657. * u16 u2Size
  1658. *
  1659. * RETURNS:
  1660. * None
  1661. *
  1662. * NOTES:
  1663. * None
  1664. *
  1665. ******************************************************************************/
  1666. static bool mtk_nand_WFIFOValidSize(u16 u2Size)
  1667. {
  1668. u32 timeout = 0xFFFF;
  1669. while (FIFO_WR_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) > u2Size) {
  1670. timeout--;
  1671. if (0 == timeout)
  1672. return false;
  1673. }
  1674. return true;
  1675. }
  1676. /******************************************************************************
  1677. * mtk_nand_status_ready
  1678. *
  1679. * DESCRIPTION:
  1680. * Indicate the NAND device is ready or not !
  1681. *
  1682. * PARAMETERS:
  1683. * u32 u4Status
  1684. *
  1685. * RETURNS:
  1686. * None
  1687. *
  1688. * NOTES:
  1689. * None
  1690. *
  1691. ******************************************************************************/
  1692. static bool mtk_nand_status_ready(u32 u4Status)
  1693. {
  1694. u32 timeout = 0xFFFF;
  1695. while ((DRV_Reg32(NFI_STA_REG32) & u4Status) != 0) {
  1696. timeout--;
  1697. if (0 == timeout)
  1698. return false;
  1699. }
  1700. return true;
  1701. }
  1702. /******************************************************************************
  1703. * mtk_nand_reset
  1704. *
  1705. * DESCRIPTION:
  1706. * Reset the NAND device hardware component !
  1707. *
  1708. * PARAMETERS:
  1709. * struct mtk_nand_host *host (Initial setting data)
  1710. *
  1711. * RETURNS:
  1712. * None
  1713. *
  1714. * NOTES:
  1715. * None
  1716. *
  1717. ******************************************************************************/
  1718. static bool mtk_nand_reset(void)
  1719. {
  1720. /* HW recommended reset flow */
  1721. int timeout = 0xFFFF;
  1722. if (DRV_Reg16(NFI_MASTERSTA_REG16) & 0xFFF) { /* master is busy */
  1723. mb();
  1724. DRV_WriteReg32(NFI_CON_REG16, CON_FIFO_FLUSH | CON_NFI_RST);
  1725. while (DRV_Reg16(NFI_MASTERSTA_REG16) & 0xFFF) {
  1726. timeout--;
  1727. if (!timeout)
  1728. pr_notice("Wait for NFI_MASTERSTA timeout\n");
  1729. }
  1730. }
  1731. /* issue reset operation */
  1732. mb();
  1733. DRV_WriteReg32(NFI_CON_REG16, CON_FIFO_FLUSH | CON_NFI_RST);
  1734. return mtk_nand_status_ready(STA_NFI_FSM_MASK | STA_NAND_BUSY) && mtk_nand_RFIFOValidSize(0)
  1735. && mtk_nand_WFIFOValidSize(0);
  1736. }
  1737. /******************************************************************************
  1738. * mtk_nand_set_mode
  1739. *
  1740. * DESCRIPTION:
  1741. * Set the oepration mode !
  1742. *
  1743. * PARAMETERS:
  1744. * u16 u2OpMode (read/write)
  1745. *
  1746. * RETURNS:
  1747. * None
  1748. *
  1749. * NOTES:
  1750. * None
  1751. *
  1752. ******************************************************************************/
  1753. static void mtk_nand_set_mode(u16 u2OpMode)
  1754. {
  1755. u16 u2Mode = DRV_Reg16(NFI_CNFG_REG16);
  1756. u2Mode &= ~CNFG_OP_MODE_MASK;
  1757. u2Mode |= u2OpMode;
  1758. DRV_WriteReg16(NFI_CNFG_REG16, u2Mode);
  1759. }
  1760. /******************************************************************************
  1761. * mtk_nand_set_autoformat
  1762. *
  1763. * DESCRIPTION:
  1764. * Enable/Disable hardware autoformat !
  1765. *
  1766. * PARAMETERS:
  1767. * bool bEnable (Enable/Disable)
  1768. *
  1769. * RETURNS:
  1770. * None
  1771. *
  1772. * NOTES:
  1773. * None
  1774. *
  1775. ******************************************************************************/
  1776. static void mtk_nand_set_autoformat(bool bEnable)
  1777. {
  1778. if (bEnable)
  1779. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AUTO_FMT_EN);
  1780. else
  1781. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AUTO_FMT_EN);
  1782. }
  1783. /******************************************************************************
  1784. * mtk_nand_configure_fdm
  1785. *
  1786. * DESCRIPTION:
  1787. * Configure the FDM data size !
  1788. *
  1789. * PARAMETERS:
  1790. * u16 u2FDMSize
  1791. *
  1792. * RETURNS:
  1793. * None
  1794. *
  1795. * NOTES:
  1796. * None
  1797. *
  1798. ******************************************************************************/
  1799. static void mtk_nand_configure_fdm(u16 u2FDMSize)
  1800. {
  1801. NFI_CLN_REG32(NFI_PAGEFMT_REG32, PAGEFMT_FDM_MASK | PAGEFMT_FDM_ECC_MASK);
  1802. NFI_SET_REG32(NFI_PAGEFMT_REG32, u2FDMSize << PAGEFMT_FDM_SHIFT);
  1803. NFI_SET_REG32(NFI_PAGEFMT_REG32, u2FDMSize << PAGEFMT_FDM_ECC_SHIFT);
  1804. }
  1805. static bool mtk_nand_pio_ready(void)
  1806. {
  1807. int count = 0;
  1808. while (!(DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1)) {
  1809. count++;
  1810. if (count > 0xffff) {
  1811. pr_info("PIO_DIRDY timeout\n");
  1812. return false;
  1813. }
  1814. }
  1815. return true;
  1816. }
  1817. /******************************************************************************
  1818. * mtk_nand_set_command
  1819. *
  1820. * DESCRIPTION:
  1821. * Send hardware commands to NAND devices !
  1822. *
  1823. * PARAMETERS:
  1824. * u16 command
  1825. *
  1826. * RETURNS:
  1827. * None
  1828. *
  1829. * NOTES:
  1830. * None
  1831. *
  1832. ******************************************************************************/
  1833. static bool mtk_nand_set_command(u16 command)
  1834. {
  1835. /* Write command to device */
  1836. mb();
  1837. DRV_WriteReg16(NFI_CMD_REG16, command);
  1838. return mtk_nand_status_ready(STA_CMD_STATE);
  1839. }
  1840. /******************************************************************************
  1841. * mtk_nand_set_address
  1842. *
  1843. * DESCRIPTION:
  1844. * Set the hardware address register !
  1845. *
  1846. * PARAMETERS:
  1847. * struct nand_chip *nand, u32 u4RowAddr
  1848. *
  1849. * RETURNS:
  1850. * None
  1851. *
  1852. * NOTES:
  1853. * None
  1854. *
  1855. ******************************************************************************/
  1856. static bool mtk_nand_set_address(u32 u4ColAddr, u32 u4RowAddr, u16 u2ColNOB, u16 u2RowNOB)
  1857. {
  1858. /* fill cycle addr */
  1859. mb();
  1860. DRV_WriteReg32(NFI_COLADDR_REG32, u4ColAddr);
  1861. DRV_WriteReg32(NFI_ROWADDR_REG32, u4RowAddr);
  1862. DRV_WriteReg16(NFI_ADDRNOB_REG16, u2ColNOB | (u2RowNOB << ADDR_ROW_NOB_SHIFT));
  1863. return mtk_nand_status_ready(STA_ADDR_STATE);
  1864. }
  1865. /* ------------------------------------------------------------------------------- */
  1866. static bool mtk_nand_device_reset(void)
  1867. {
  1868. u32 timeout = 0xFFFF;
  1869. mtk_nand_reset();
  1870. DRV_WriteReg(NFI_CNFG_REG16, CNFG_OP_RESET);
  1871. mtk_nand_set_command(NAND_CMD_RESET);
  1872. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--))
  1873. ;
  1874. if (!timeout)
  1875. return FALSE;
  1876. else
  1877. return TRUE;
  1878. }
  1879. /* ------------------------------------------------------------------------------- */
  1880. /******************************************************************************
  1881. * mtk_nand_check_RW_count
  1882. *
  1883. * DESCRIPTION:
  1884. * Check the RW how many sectors !
  1885. *
  1886. * PARAMETERS:
  1887. * u16 u2WriteSize
  1888. *
  1889. * RETURNS:
  1890. * None
  1891. *
  1892. * NOTES:
  1893. * None
  1894. *
  1895. ******************************************************************************/
  1896. static bool mtk_nand_check_RW_count(u16 u2WriteSize)
  1897. {
  1898. u32 timeout = 0xFFFF;
  1899. u16 u2SecNum = u2WriteSize >> host->hw->nand_sec_shift;
  1900. while (ADDRCNTR_CNTR(DRV_Reg32(NFI_ADDRCNTR_REG16)) < u2SecNum) {
  1901. timeout--;
  1902. if (0 == timeout) {
  1903. pr_info("[%s] timeout\n", __func__);
  1904. return false;
  1905. }
  1906. }
  1907. return true;
  1908. }
  1909. /******************************************************************************
  1910. * mtk_nand_ready_for_read
  1911. *
  1912. * DESCRIPTION:
  1913. * Prepare hardware environment for read !
  1914. *
  1915. * PARAMETERS:
  1916. * struct nand_chip *nand, u32 u4RowAddr
  1917. *
  1918. * RETURNS:
  1919. * None
  1920. *
  1921. * NOTES:
  1922. * None
  1923. *
  1924. ******************************************************************************/
  1925. static bool mtk_nand_ready_for_read(struct nand_chip *nand, u32 u4RowAddr, u32 u4ColAddr,
  1926. u16 sec_num, bool full, u8 *buf)
  1927. {
  1928. /* Reset NFI HW internal state machine and flush NFI in/out FIFO */
  1929. bool bRet = false;
  1930. /* u16 sec_num = 1 << (nand->page_shift - host->hw->nand_sec_shift); */
  1931. u32 col_addr = u4ColAddr;
  1932. u32 colnob = 2, rownob = devinfo.addr_cycle - 2;
  1933. /* u32 reg_val = DRV_Reg32(NFI_MASTERRST_REG32); */
  1934. #if __INTERNAL_USE_AHB_MODE__
  1935. unsigned int phys = 0;
  1936. #endif
  1937. #if CFG_PERFLOG_DEBUG
  1938. struct timeval stimer, etimer;
  1939. do_gettimeofday(&stimer);
  1940. #endif
  1941. if (full) {
  1942. mtk_dir = DMA_FROM_DEVICE;
  1943. sg_init_one(&mtk_sg, buf, (sec_num * (1 << host->hw->nand_sec_shift)));
  1944. dma_map_sg(mtk_dev, &mtk_sg, 1, mtk_dir);
  1945. phys = mtk_sg.dma_address;
  1946. /* pr_debug("[xl] phys va 0x%x\n", phys); */
  1947. }
  1948. if (DRV_Reg32(NFI_NAND_TYPE_CNFG_REG32) & 0x3) {
  1949. NFI_SET_REG16(NFI_MASTERRST_REG32, PAD_MACRO_RST); /* reset */
  1950. NFI_CLN_REG16(NFI_MASTERRST_REG32, PAD_MACRO_RST); /* dereset */
  1951. }
  1952. if (nand->options & NAND_BUSWIDTH_16)
  1953. col_addr /= 2;
  1954. if (!mtk_nand_reset())
  1955. goto cleanup;
  1956. if (g_bHwEcc) {
  1957. /* Enable HW ECC */
  1958. NFI_SET_REG32(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1959. } else {
  1960. NFI_CLN_REG32(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1961. }
  1962. mtk_nand_set_mode(CNFG_OP_READ);
  1963. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  1964. DRV_WriteReg32(NFI_CON_REG16, sec_num << CON_NFI_SEC_SHIFT);
  1965. if (full) {
  1966. #if __INTERNAL_USE_AHB_MODE__
  1967. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1968. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  1969. /* phys = nand_virt_to_phys_add((unsigned long) buf); */
  1970. if (!phys) {
  1971. pr_err("[mtk_nand_ready_for_read]convert virt addr (%lx) to phys add (%x)fail!!!",
  1972. (unsigned long)buf, phys);
  1973. return false;
  1974. }
  1975. DRV_WriteReg32(NFI_STRADDR_REG32, phys);
  1976. #else
  1977. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1978. #endif
  1979. if (g_bHwEcc)
  1980. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1981. else
  1982. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1983. } else {
  1984. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  1985. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  1986. }
  1987. mtk_nand_set_autoformat(full);
  1988. if (full) {
  1989. if (g_bHwEcc)
  1990. ECC_Decode_Start();
  1991. }
  1992. if (!mtk_nand_set_command(NAND_CMD_READ0))
  1993. goto cleanup;
  1994. if (!mtk_nand_set_address(col_addr, u4RowAddr, colnob, rownob))
  1995. goto cleanup;
  1996. if (!mtk_nand_set_command(NAND_CMD_READSTART))
  1997. goto cleanup;
  1998. if (!mtk_nand_status_ready(STA_NAND_BUSY))
  1999. goto cleanup;
  2000. bRet = true;
  2001. cleanup:
  2002. #if CFG_PERFLOG_DEBUG
  2003. do_gettimeofday(&etimer);
  2004. g_NandPerfLog.ReadBusyTotalTime += Cal_timediff(&etimer, &stimer);
  2005. g_NandPerfLog.ReadBusyCount++;
  2006. #endif
  2007. return bRet;
  2008. }
  2009. /******************************************************************************
  2010. * mtk_nand_ready_for_write
  2011. *
  2012. * DESCRIPTION:
  2013. * Prepare hardware environment for write !
  2014. *
  2015. * PARAMETERS:
  2016. * struct nand_chip *nand, u32 u4RowAddr
  2017. *
  2018. * RETURNS:
  2019. * None
  2020. *
  2021. * NOTES:
  2022. * None
  2023. *
  2024. ******************************************************************************/
  2025. static bool mtk_nand_ready_for_write(struct nand_chip *nand, u32 u4RowAddr, u32 col_addr, bool full,
  2026. u8 *buf)
  2027. {
  2028. bool bRet = false;
  2029. u32 sec_num = 1 << (nand->page_shift - host->hw->nand_sec_shift);
  2030. u32 colnob = 2, rownob = devinfo.addr_cycle - 2;
  2031. #if __INTERNAL_USE_AHB_MODE__
  2032. unsigned int phys = 0;
  2033. /* u32 T_phys=0; */
  2034. #endif
  2035. if (full) {
  2036. mtk_dir = DMA_TO_DEVICE;
  2037. sg_init_one(&mtk_sg, buf, (1 << nand->page_shift));
  2038. dma_map_sg(mtk_dev, &mtk_sg, 1, mtk_dir);
  2039. phys = mtk_sg.dma_address;
  2040. /* pr_debug("[xl] phys va 0x%x\n", phys); */
  2041. }
  2042. if (nand->options & NAND_BUSWIDTH_16)
  2043. col_addr /= 2;
  2044. /* Reset NFI HW internal state machine and flush NFI in/out FIFO */
  2045. if (!mtk_nand_reset()) {
  2046. pr_err("[Bean]mtk_nand_ready_for_write (mtk_nand_reset) fail!\n");
  2047. return false;
  2048. }
  2049. mtk_nand_set_mode(CNFG_OP_PRGM);
  2050. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  2051. DRV_WriteReg32(NFI_CON_REG16, sec_num << CON_NFI_SEC_SHIFT);
  2052. if (full) {
  2053. #if __INTERNAL_USE_AHB_MODE__
  2054. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AHB);
  2055. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  2056. /* phys = nand_virt_to_phys_add((unsigned long) buf); */
  2057. /* T_phys=__virt_to_phys(buf); */
  2058. if (!phys) {
  2059. pr_err("[mt65xx_nand_ready_for_write]convert virt addr (%lx) to phys add fail!!!",
  2060. (unsigned long)buf);
  2061. return false;
  2062. }
  2063. DRV_WriteReg32(NFI_STRADDR_REG32, phys);
  2064. #if 0
  2065. if ((T_phys > 0x700000 && T_phys < 0x800000)
  2066. || (phys > 0x700000 && phys < 0x800000)) {
  2067. {
  2068. pr_debug("[NFI_WRITE]ERROR: Forbidden AHB address wrong phys address =0x%x , right phys address=0x%x, virt address= 0x%x (count = %d)\n",
  2069. T_phys, phys, (u32) buf, g_dump_count++);
  2070. show_stack(NULL, NULL);
  2071. }
  2072. BUG_ON(1);
  2073. }
  2074. #endif
  2075. #else
  2076. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  2077. #endif
  2078. if (g_bHwEcc)
  2079. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2080. else
  2081. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2082. } else {
  2083. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2084. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  2085. }
  2086. mtk_nand_set_autoformat(full);
  2087. if (full) {
  2088. if (g_bHwEcc)
  2089. ECC_Encode_Start();
  2090. }
  2091. if (!mtk_nand_set_command(NAND_CMD_SEQIN)) {
  2092. pr_err("[Bean]mtk_nand_ready_for_write (mtk_nand_set_command) fail!\n");
  2093. goto cleanup;
  2094. }
  2095. /* 1 FIXED ME: For Any Kind of AddrCycle */
  2096. if (!mtk_nand_set_address(col_addr, u4RowAddr, colnob, rownob)) {
  2097. pr_err("[Bean]mtk_nand_ready_for_write (mtk_nand_set_address) fail!\n");
  2098. goto cleanup;
  2099. }
  2100. if (!mtk_nand_status_ready(STA_NAND_BUSY)) {
  2101. pr_err("[Bean]mtk_nand_ready_for_write (mtk_nand_status_ready) fail!\n");
  2102. goto cleanup;
  2103. }
  2104. bRet = true;
  2105. cleanup:
  2106. return bRet;
  2107. }
  2108. static bool mtk_nand_check_dececc_done(u32 u4SecNum)
  2109. {
  2110. u32 dec_mask;
  2111. struct timeval timer_timeout, timer_cur;
  2112. do_gettimeofday(&timer_timeout);
  2113. timer_timeout.tv_usec += 800 * 1000; /* 500ms */
  2114. if (timer_timeout.tv_usec >= 1000000) { /* 1 second */
  2115. timer_timeout.tv_usec -= 1000000;
  2116. timer_timeout.tv_sec += 1;
  2117. }
  2118. dec_mask = (1 << (u4SecNum - 1));
  2119. while (dec_mask != (DRV_Reg(ECC_DECDONE_REG16) & dec_mask)) {
  2120. do_gettimeofday(&timer_cur);
  2121. if (timeval_compare(&timer_cur, &timer_timeout) >= 0) {
  2122. pr_notice("ECC_DECDONE: timeout 0x%x %d\n", DRV_Reg(ECC_DECDONE_REG16),
  2123. u4SecNum);
  2124. dump_nfi();
  2125. return false;
  2126. }
  2127. }
  2128. while ((DRV_Reg32(ECC_DECFSM_REG32) & 0x3F3FFF0F) != ECC_DECFSM_IDLE) {
  2129. do_gettimeofday(&timer_cur);
  2130. if (timeval_compare(&timer_cur, &timer_timeout) >= 0) {
  2131. pr_notice("ECC_DECDONE: timeout 0x%x 0x%x %d\n",
  2132. DRV_Reg32(ECC_DECFSM_REG32), DRV_Reg(ECC_DECDONE_REG16), u4SecNum);
  2133. dump_nfi();
  2134. return false;
  2135. }
  2136. }
  2137. return true;
  2138. }
  2139. /******************************************************************************
  2140. * mtk_nand_read_page_data
  2141. *
  2142. * DESCRIPTION:
  2143. * Fill the page data into buffer !
  2144. *
  2145. * PARAMETERS:
  2146. * u8* pDataBuf, u32 u4Size
  2147. *
  2148. * RETURNS:
  2149. * None
  2150. *
  2151. * NOTES:
  2152. * None
  2153. *
  2154. ******************************************************************************/
  2155. static bool mtk_nand_dma_read_data(struct mtd_info *mtd, u8 *buf, u32 length)
  2156. {
  2157. int interrupt_en = g_i4Interrupt;
  2158. int timeout = 0xfffff;
  2159. /* struct scatterlist sg; */
  2160. /* enum dma_data_direction dir = DMA_FROM_DEVICE; */
  2161. #if CFG_PERFLOG_DEBUG
  2162. struct timeval stimer, etimer;
  2163. do_gettimeofday(&stimer);
  2164. #endif
  2165. /* pr_debug("[xl] dma read buf in 0x%lx\n", (unsigned long)buf); */
  2166. /* sg_init_one(&sg, buf, length); */
  2167. /* pr_debug("[xl] dma read buf out 0x%lx\n", (unsigned long)buf); */
  2168. /* dma_map_sg(&(mtd->dev), &sg, 1, dir); */
  2169. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2170. /* DRV_WriteReg32(NFI_STRADDR_REG32, __virt_to_phys(pDataBuf)); */
  2171. if ((unsigned long)buf % 16) { /* TODO: can not use AHB mode here */
  2172. pr_debug("Un-16-aligned address\n");
  2173. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  2174. } else {
  2175. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  2176. }
  2177. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  2178. DRV_Reg16(NFI_INTR_REG16);
  2179. DRV_WriteReg16(NFI_INTR_EN_REG16, INTR_AHB_DONE_EN);
  2180. if (interrupt_en)
  2181. init_completion(&g_comp_AHB_Done);
  2182. /* dmac_inv_range(pDataBuf, pDataBuf + u4Size); */
  2183. mb();
  2184. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BRD);
  2185. g_running_dma = 1;
  2186. if (interrupt_en) {
  2187. /* Wait 10ms for AHB done */
  2188. if (!wait_for_completion_timeout(&g_comp_AHB_Done, 50)) {
  2189. pr_notice("wait for completion timeout happened @ [%s]: %d\n", __func__,
  2190. __LINE__);
  2191. dump_nfi();
  2192. g_running_dma = 0;
  2193. return false;
  2194. }
  2195. g_running_dma = 0;
  2196. while ((length >> host->hw->nand_sec_shift) >
  2197. ((DRV_Reg32(NFI_BYTELEN_REG16) & 0x1f000) >> 12)) {
  2198. timeout--;
  2199. if (0 == timeout) {
  2200. pr_err("[%s] poll BYTELEN error\n", __func__);
  2201. g_running_dma = 0;
  2202. return false; /* 4 // AHB Mode Time Out! */
  2203. }
  2204. }
  2205. } else {
  2206. while (!DRV_Reg16(NFI_INTR_REG16)) {
  2207. timeout--;
  2208. if (0 == timeout) {
  2209. pr_err("[%s] poll nfi_intr error\n", __func__);
  2210. dump_nfi();
  2211. g_running_dma = 0;
  2212. return false; /* 4 // AHB Mode Time Out! */
  2213. }
  2214. }
  2215. g_running_dma = 0;
  2216. while ((length >> host->hw->nand_sec_shift) >
  2217. ((DRV_Reg32(NFI_BYTELEN_REG16) & 0x1f000) >> 12)) {
  2218. timeout--;
  2219. if (0 == timeout) {
  2220. pr_err("[%s] poll BYTELEN error\n", __func__);
  2221. dump_nfi();
  2222. g_running_dma = 0;
  2223. return false; /* 4 // AHB Mode Time Out! */
  2224. }
  2225. }
  2226. }
  2227. /* dma_unmap_sg(&(mtd->dev), &sg, 1, dir); */
  2228. #if CFG_PERFLOG_DEBUG
  2229. do_gettimeofday(&etimer);
  2230. g_NandPerfLog.ReadDMATotalTime += Cal_timediff(&etimer, &stimer);
  2231. g_NandPerfLog.ReadDMACount++;
  2232. #endif
  2233. return true;
  2234. }
  2235. static bool mtk_nand_mcu_read_data(struct mtd_info *mtd, u8 *buf, u32 length)
  2236. {
  2237. int timeout = 0xffff;
  2238. u32 i;
  2239. u32 *buf32 = (u32 *) buf;
  2240. #ifdef TESTTIME
  2241. unsigned long long time1, time2;
  2242. time1 = sched_clock();
  2243. #endif
  2244. if ((unsigned long)buf % 4 || length % 4)
  2245. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2246. else
  2247. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2248. /* DRV_WriteReg32(NFI_STRADDR_REG32, 0); */
  2249. mb();
  2250. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BRD);
  2251. if ((unsigned long)buf % 4 || length % 4) {
  2252. for (i = 0; (i < (length)) && (timeout > 0);) {
  2253. /* if (FIFO_RD_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) >= 4) */
  2254. if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  2255. *buf++ = (u8) DRV_Reg32(NFI_DATAR_REG32);
  2256. i++;
  2257. } else {
  2258. timeout--;
  2259. }
  2260. if (0 == timeout) {
  2261. pr_err("[%s] timeout\n", __func__);
  2262. dump_nfi();
  2263. return false;
  2264. }
  2265. }
  2266. } else {
  2267. for (i = 0; (i < (length >> 2)) && (timeout > 0);) {
  2268. /* if (FIFO_RD_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) >= 4) */
  2269. if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  2270. *buf32++ = DRV_Reg32(NFI_DATAR_REG32);
  2271. i++;
  2272. } else {
  2273. timeout--;
  2274. }
  2275. if (0 == timeout) {
  2276. pr_err("[%s] timeout\n", __func__);
  2277. dump_nfi();
  2278. return false;
  2279. }
  2280. }
  2281. }
  2282. #ifdef TESTTIME
  2283. time2 = sched_clock() - time1;
  2284. if (!readdatatime)
  2285. readdatatime = (time2);
  2286. #endif
  2287. return true;
  2288. }
  2289. static bool mtk_nand_read_page_data(struct mtd_info *mtd, u8 *pDataBuf, u32 u4Size)
  2290. {
  2291. #if (__INTERNAL_USE_AHB_MODE__)
  2292. return mtk_nand_dma_read_data(mtd, pDataBuf, u4Size);
  2293. #else
  2294. return mtk_nand_mcu_read_data(mtd, pDataBuf, u4Size);
  2295. #endif
  2296. }
  2297. /******************************************************************************
  2298. * mtk_nand_write_page_data
  2299. *
  2300. * DESCRIPTION:
  2301. * Fill the page data into buffer !
  2302. *
  2303. * PARAMETERS:
  2304. * u8* pDataBuf, u32 u4Size
  2305. *
  2306. * RETURNS:
  2307. * None
  2308. *
  2309. * NOTES:
  2310. * None
  2311. *
  2312. ******************************************************************************/
  2313. static bool mtk_nand_dma_write_data(struct mtd_info *mtd, u8 *pDataBuf, u32 u4Size)
  2314. {
  2315. int i4Interrupt = 0; /* g_i4Interrupt; */
  2316. u32 timeout = 0xFFFF;
  2317. /* struct scatterlist sg; */
  2318. /* enum dma_data_direction dir = DMA_TO_DEVICE; */
  2319. #if CFG_PERFLOG_DEBUG
  2320. struct timeval stimer, etimer;
  2321. do_gettimeofday(&stimer);
  2322. #endif
  2323. /* pr_debug("[xl] dma write buf in 0x%lx\n", (unsigned long)pDataBuf); */
  2324. /* sg_init_one(&sg, pDataBuf, u4Size); */
  2325. /* pr_debug("[xl] dma write buf out 0x%lx\n", (unsigned long)pDataBuf); */
  2326. /* dma_map_sg(&(mtd->dev), &sg, 1, dir); */
  2327. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2328. DRV_Reg16(NFI_INTR_REG16);
  2329. DRV_WriteReg16(NFI_INTR_EN_REG16, 0);
  2330. /* DRV_WriteReg32(NFI_STRADDR_REG32, (u32*)virt_to_phys(pDataBuf)); */
  2331. if ((unsigned long)pDataBuf % 16) { /* TODO: can not use AHB mode here */
  2332. pr_debug("Un-16-aligned address\n");
  2333. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  2334. } else {
  2335. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  2336. }
  2337. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  2338. if (i4Interrupt) {
  2339. init_completion(&g_comp_AHB_Done);
  2340. DRV_Reg16(NFI_INTR_REG16);
  2341. DRV_WriteReg16(NFI_INTR_EN_REG16, INTR_AHB_DONE_EN);
  2342. }
  2343. /* dmac_clean_range(pDataBuf, pDataBuf + u4Size); */
  2344. mb();
  2345. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BWR);
  2346. g_running_dma = 3;
  2347. if (i4Interrupt) {
  2348. /* Wait 10ms for AHB done */
  2349. if (!wait_for_completion_timeout(&g_comp_AHB_Done, 10)) {
  2350. pr_notice("wait for completion timeout happened @ [%s]: %d\n", __func__,
  2351. __LINE__);
  2352. dump_nfi();
  2353. g_running_dma = 0;
  2354. return false;
  2355. }
  2356. g_running_dma = 0;
  2357. /* wait_for_completion(&g_comp_AHB_Done); */
  2358. } else {
  2359. while ((u4Size >> host->hw->nand_sec_shift) >
  2360. ((DRV_Reg32(NFI_BYTELEN_REG16) & 0x1f000) >> 12)) {
  2361. timeout--;
  2362. if (0 == timeout) {
  2363. pr_err("[%s] poll BYTELEN error\n", __func__);
  2364. g_running_dma = 0;
  2365. return false; /* 4 // AHB Mode Time Out! */
  2366. }
  2367. }
  2368. g_running_dma = 0;
  2369. }
  2370. /* dma_unmap_sg(&(mtd->dev), &sg, 1, dir); */
  2371. #if CFG_PERFLOG_DEBUG
  2372. do_gettimeofday(&etimer);
  2373. g_NandPerfLog.WriteDMATotalTime += Cal_timediff(&etimer, &stimer);
  2374. g_NandPerfLog.WriteDMACount++;
  2375. #endif
  2376. return true;
  2377. }
  2378. static bool mtk_nand_mcu_write_data(struct mtd_info *mtd, const u8 *buf, u32 length)
  2379. {
  2380. u32 timeout = 0xFFFF;
  2381. u32 i;
  2382. u32 *pBuf32;
  2383. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2384. mb();
  2385. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BWR);
  2386. pBuf32 = (u32 *) buf;
  2387. if ((unsigned long)buf % 4 || length % 4)
  2388. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2389. else
  2390. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2391. if ((unsigned long)buf % 4 || length % 4) {
  2392. for (i = 0; (i < (length)) && (timeout > 0);) {
  2393. if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  2394. DRV_WriteReg32(NFI_DATAW_REG32, *buf++);
  2395. i++;
  2396. } else {
  2397. timeout--;
  2398. }
  2399. if (0 == timeout) {
  2400. pr_err("[%s] timeout\n", __func__);
  2401. dump_nfi();
  2402. return false;
  2403. }
  2404. }
  2405. } else {
  2406. for (i = 0; (i < (length >> 2)) && (timeout > 0);) {
  2407. /* if (FIFO_WR_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16)) <= 12) */
  2408. if (DRV_Reg16(NFI_PIO_DIRDY_REG16) & 1) {
  2409. DRV_WriteReg32(NFI_DATAW_REG32, *pBuf32++);
  2410. i++;
  2411. } else {
  2412. timeout--;
  2413. }
  2414. if (0 == timeout) {
  2415. pr_err("[%s] timeout\n", __func__);
  2416. dump_nfi();
  2417. return false;
  2418. }
  2419. }
  2420. }
  2421. return true;
  2422. }
  2423. static bool mtk_nand_write_page_data(struct mtd_info *mtd, u8 *buf, u32 size)
  2424. {
  2425. #if (__INTERNAL_USE_AHB_MODE__)
  2426. return mtk_nand_dma_write_data(mtd, buf, size);
  2427. #else
  2428. return mtk_nand_mcu_write_data(mtd, buf, size);
  2429. #endif
  2430. }
  2431. /******************************************************************************
  2432. * mtk_nand_read_fdm_data
  2433. *
  2434. * DESCRIPTION:
  2435. * Read a fdm data !
  2436. *
  2437. * PARAMETERS:
  2438. * u8* pDataBuf, u32 u4SecNum
  2439. *
  2440. * RETURNS:
  2441. * None
  2442. *
  2443. * NOTES:
  2444. * None
  2445. *
  2446. ******************************************************************************/
  2447. static void mtk_nand_read_fdm_data(u8 *pDataBuf, u32 u4SecNum)
  2448. {
  2449. u32 i;
  2450. u32 *pBuf32 = (u32 *) pDataBuf;
  2451. if (pBuf32) {
  2452. for (i = 0; i < u4SecNum; ++i) {
  2453. *pBuf32++ = DRV_Reg32(NFI_FDM0L_REG32 + (i << 1));
  2454. *pBuf32++ = DRV_Reg32(NFI_FDM0M_REG32 + (i << 1));
  2455. /* *pBuf32++ = DRV_Reg32((u32)NFI_FDM0L_REG32 + (i<<3)); */
  2456. /* *pBuf32++ = DRV_Reg32((u32)NFI_FDM0M_REG32 + (i<<3)); */
  2457. }
  2458. }
  2459. }
  2460. /******************************************************************************
  2461. * mtk_nand_write_fdm_data
  2462. *
  2463. * DESCRIPTION:
  2464. * Write a fdm data !
  2465. *
  2466. * PARAMETERS:
  2467. * u8* pDataBuf, u32 u4SecNum
  2468. *
  2469. * RETURNS:
  2470. * None
  2471. *
  2472. * NOTES:
  2473. * None
  2474. *
  2475. ******************************************************************************/
  2476. static u8 fdm_buf[128];
  2477. static void mtk_nand_write_fdm_data(struct nand_chip *chip, u8 *pDataBuf, u32 u4SecNum)
  2478. {
  2479. u32 i, j;
  2480. u8 checksum = 0;
  2481. bool empty = true;
  2482. struct nand_oobfree *free_entry;
  2483. u32 *pBuf32;
  2484. memcpy(fdm_buf, pDataBuf, u4SecNum * 8);
  2485. free_entry = chip->ecc.layout->oobfree;
  2486. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES && free_entry[i].length; i++) {
  2487. for (j = 0; j < free_entry[i].length; j++) {
  2488. if (pDataBuf[free_entry[i].offset + j] != 0xFF)
  2489. empty = false;
  2490. checksum ^= pDataBuf[free_entry[i].offset + j];
  2491. }
  2492. }
  2493. if (!empty)
  2494. fdm_buf[free_entry[i - 1].offset + free_entry[i - 1].length] = checksum;
  2495. pBuf32 = (u32 *) fdm_buf;
  2496. for (i = 0; i < u4SecNum; ++i) {
  2497. DRV_WriteReg32(NFI_FDM0L_REG32 + (i << 1), *pBuf32++);
  2498. DRV_WriteReg32(NFI_FDM0M_REG32 + (i << 1), *pBuf32++);
  2499. /* DRV_WriteReg32((u32)NFI_FDM0L_REG32 + (i<<3), *pBuf32++); */
  2500. /* DRV_WriteReg32((u32)NFI_FDM0M_REG32 + (i<<3), *pBuf32++); */
  2501. }
  2502. }
  2503. /******************************************************************************
  2504. * mtk_nand_stop_read
  2505. *
  2506. * DESCRIPTION:
  2507. * Stop read operation !
  2508. *
  2509. * PARAMETERS:
  2510. * None
  2511. *
  2512. * RETURNS:
  2513. * None
  2514. *
  2515. * NOTES:
  2516. * None
  2517. *
  2518. ******************************************************************************/
  2519. static void mtk_nand_stop_read(void)
  2520. {
  2521. NFI_CLN_REG32(NFI_CON_REG16, CON_NFI_BRD);
  2522. mtk_nand_reset();
  2523. if (g_bHwEcc)
  2524. ECC_Decode_End();
  2525. DRV_WriteReg16(NFI_INTR_EN_REG16, 0);
  2526. }
  2527. /******************************************************************************
  2528. * mtk_nand_stop_write
  2529. *
  2530. * DESCRIPTION:
  2531. * Stop write operation !
  2532. *
  2533. * PARAMETERS:
  2534. * None
  2535. *
  2536. * RETURNS:
  2537. * None
  2538. *
  2539. * NOTES:
  2540. * None
  2541. *
  2542. ******************************************************************************/
  2543. static void mtk_nand_stop_write(void)
  2544. {
  2545. NFI_CLN_REG32(NFI_CON_REG16, CON_NFI_BWR);
  2546. if (g_bHwEcc)
  2547. ECC_Encode_End();
  2548. DRV_WriteReg16(NFI_INTR_EN_REG16, 0);
  2549. }
  2550. /* --------------------------------------------------------------------------- */
  2551. #define STATUS_READY (0x40)
  2552. #define STATUS_FAIL (0x01)
  2553. #define STATUS_WR_ALLOW (0x80)
  2554. #if 0
  2555. static bool mtk_nand_read_status(void)
  2556. {
  2557. int status = 0; /* , i; */
  2558. unsigned int timeout;
  2559. mtk_nand_reset();
  2560. /* Disable HW ECC */
  2561. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  2562. /* Disable 16-bit I/O */
  2563. NFI_CLN_REG32(NFI_PAGEFMT_REG32, PAGEFMT_DBYTE_EN);
  2564. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_OP_SRD | CNFG_READ_EN | CNFG_BYTE_RW);
  2565. DRV_WriteReg32(NFI_CON_REG16, CON_NFI_SRD | (1 << CON_NFI_NOB_SHIFT));
  2566. DRV_WriteReg32(NFI_CON_REG16, 0x3);
  2567. mtk_nand_set_mode(CNFG_OP_SRD);
  2568. DRV_WriteReg16(NFI_CNFG_REG16, 0x2042);
  2569. mtk_nand_set_command(NAND_CMD_STATUS);
  2570. DRV_WriteReg32(NFI_CON_REG16, 0x90);
  2571. timeout = TIMEOUT_4;
  2572. WAIT_NFI_PIO_READY(timeout);
  2573. if (timeout)
  2574. status = (DRV_Reg16(NFI_DATAR_REG32));
  2575. /* ~ clear NOB */
  2576. DRV_WriteReg32(NFI_CON_REG16, 0);
  2577. if (devinfo.iowidth == 16) {
  2578. NFI_SET_REG32(NFI_PAGEFMT_REG32, PAGEFMT_DBYTE_EN);
  2579. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  2580. }
  2581. /* check READY/BUSY status first */
  2582. if (!(STATUS_READY & status))
  2583. /* MSG(ERR, "status is not ready\n"); */
  2584. /* flash is ready now, check status code */
  2585. if (STATUS_FAIL & status) {
  2586. if (!(STATUS_WR_ALLOW & status)) {
  2587. /* MSG(INIT, "status locked\n"); */
  2588. return FALSE;
  2589. }
  2590. /* MSG(INIT, "status unknown\n"); */
  2591. return FALSE;
  2592. } else {
  2593. return TRUE;
  2594. }
  2595. }
  2596. #endif
  2597. bool mtk_nand_SetFeature(struct mtd_info *mtd, u16 cmd, u32 addr, u8 *value, u8 bytes)
  2598. {
  2599. u16 reg_val = 0;
  2600. u8 write_count = 0;
  2601. u32 reg = 0;
  2602. u32 timeout = TIMEOUT_3; /* 0xffff; */
  2603. /* u32 status; */
  2604. /* struct nand_chip *chip = (struct nand_chip *)mtd->priv; */
  2605. mtk_nand_reset();
  2606. reg = DRV_Reg32(NFI_NAND_TYPE_CNFG_REG32);
  2607. if (!(reg & TYPE_SLC))
  2608. bytes <<= 1;
  2609. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW);
  2610. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2611. mtk_nand_set_command(cmd);
  2612. mtk_nand_set_address(addr, 0, 1, 0);
  2613. mtk_nand_status_ready(STA_NFI_OP_MASK);
  2614. DRV_WriteReg32(NFI_CON_REG16, 1 << CON_NFI_SEC_SHIFT);
  2615. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BWR);
  2616. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  2617. /* pr_debug("Bytes=%d\n", bytes); */
  2618. while ((write_count < bytes) && timeout) {
  2619. WAIT_NFI_PIO_READY(timeout);
  2620. if (timeout == 0)
  2621. break;
  2622. if (reg & TYPE_SLC) {
  2623. /* pr_debug("VALUE1:0x%2X\n", *value); */
  2624. DRV_WriteReg8(NFI_DATAW_REG32, *value++);
  2625. } else if (write_count % 2) {
  2626. /* pr_debug("VALUE2:0x%2X\n", *value); */
  2627. DRV_WriteReg8(NFI_DATAW_REG32, *value++);
  2628. } else {
  2629. /* pr_debug("VALUE3:0x%2X\n", *value); */
  2630. DRV_WriteReg8(NFI_DATAW_REG32, *value);
  2631. }
  2632. write_count++;
  2633. timeout = TIMEOUT_3;
  2634. }
  2635. *NFI_CNRNB_REG16 = 0x81;
  2636. if (!mtk_nand_status_ready(STA_NAND_BUSY_RETURN))
  2637. return FALSE;
  2638. /* mtk_nand_read_status(); */
  2639. /* if(status& 0x1) */
  2640. /* return FALSE; */
  2641. return TRUE;
  2642. }
  2643. bool mtk_nand_GetFeature(struct mtd_info *mtd, u16 cmd, u32 addr, u8 *value, u8 bytes)
  2644. {
  2645. u16 reg_val = 0;
  2646. u8 read_count = 0;
  2647. u32 timeout = TIMEOUT_3; /* 0xffff; */
  2648. /* struct nand_chip *chip = (struct nand_chip *)mtd->priv; */
  2649. mtk_nand_reset();
  2650. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW | CNFG_READ_EN);
  2651. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2652. mtk_nand_set_command(cmd);
  2653. mtk_nand_set_address(addr, 0, 1, 0);
  2654. mtk_nand_status_ready(STA_NFI_OP_MASK);
  2655. *NFI_CNRNB_REG16 = 0x81;
  2656. mtk_nand_status_ready(STA_NAND_BUSY_RETURN);
  2657. /* DRV_WriteReg32(NFI_CON_REG16, 0 << CON_NFI_SEC_SHIFT); */
  2658. reg_val = DRV_Reg32(NFI_CON_REG16);
  2659. reg_val &= ~CON_NFI_NOB_MASK;
  2660. reg_val |= ((4 << CON_NFI_NOB_SHIFT) | CON_NFI_SRD);
  2661. DRV_WriteReg32(NFI_CON_REG16, reg_val);
  2662. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  2663. /* bytes = 20; */
  2664. while ((read_count < bytes) && timeout) {
  2665. WAIT_NFI_PIO_READY(timeout);
  2666. if (timeout == 0)
  2667. break;
  2668. *value++ = DRV_Reg8(NFI_DATAR_REG32);
  2669. /* pr_debug("Value[0x%02X]\n", DRV_Reg8(NFI_DATAR_REG32)); */
  2670. read_count++;
  2671. timeout = TIMEOUT_3;
  2672. }
  2673. /* chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); */
  2674. /* mtk_nand_read_status(); */
  2675. if (timeout != 0)
  2676. return TRUE;
  2677. else
  2678. return FALSE;
  2679. }
  2680. #if 1
  2681. const u8 data_tbl[8][5] = {
  2682. {0x04, 0x04, 0x7C, 0x7E, 0x00},
  2683. {0x00, 0x7C, 0x78, 0x78, 0x00},
  2684. {0x7C, 0x76, 0x74, 0x72, 0x00},
  2685. {0x08, 0x08, 0x00, 0x00, 0x00},
  2686. {0x0B, 0x7E, 0x76, 0x74, 0x00},
  2687. {0x10, 0x76, 0x72, 0x70, 0x00},
  2688. {0x02, 0x7C, 0x7E, 0x70, 0x00},
  2689. {0x00, 0x00, 0x00, 0x00, 0x00}
  2690. };
  2691. static void mtk_nand_modeentry_rrtry(void)
  2692. {
  2693. mtk_nand_reset();
  2694. mtk_nand_set_mode(CNFG_OP_CUST);
  2695. mtk_nand_set_command(0x5C);
  2696. mtk_nand_set_command(0xC5);
  2697. mtk_nand_status_ready(STA_NFI_OP_MASK);
  2698. }
  2699. static void mtk_nand_rren_rrtry(bool needB3)
  2700. {
  2701. mtk_nand_reset();
  2702. mtk_nand_set_mode(CNFG_OP_CUST);
  2703. if (needB3)
  2704. mtk_nand_set_command(0xB3);
  2705. mtk_nand_set_command(0x26);
  2706. mtk_nand_set_command(0x5D);
  2707. mtk_nand_status_ready(STA_NFI_OP_MASK);
  2708. }
  2709. static void mtk_nand_sprmset_rrtry(u32 addr, u32 data)/* single parameter setting */
  2710. {
  2711. u16 reg_val = 0;
  2712. /* u8 write_count = 0; */
  2713. /* u32 reg = 0; */
  2714. u32 timeout = TIMEOUT_3; /* 0xffff; */
  2715. mtk_nand_reset();
  2716. reg_val |= (CNFG_OP_CUST | CNFG_BYTE_RW);
  2717. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2718. mtk_nand_set_command(0x55);
  2719. mtk_nand_set_address(addr, 0, 1, 0);
  2720. mtk_nand_status_ready(STA_NFI_OP_MASK);
  2721. DRV_WriteReg32(NFI_CON_REG16, 1 << CON_NFI_SEC_SHIFT);
  2722. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BWR);
  2723. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  2724. WAIT_NFI_PIO_READY(timeout);
  2725. timeout = TIMEOUT_3;
  2726. DRV_WriteReg8(NFI_DATAW_REG32, data);
  2727. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--))
  2728. ;
  2729. }
  2730. static void mtk_nand_toshiba_rrtry(struct mtd_info *mtd, flashdev_info_t deviceinfo, u32 retryCount,
  2731. bool defValue)
  2732. {
  2733. u32 acccon;
  2734. u8 cnt = 0;
  2735. u8 add_reg[6] = { 0x04, 0x05, 0x06, 0x07, 0x0D };
  2736. acccon = DRV_Reg32(NFI_ACCCON_REG32);
  2737. DRV_WriteReg32(NFI_ACCCON_REG32, 0x31C08669); /* to fit read retry timing */
  2738. if (0 == retryCount)
  2739. mtk_nand_modeentry_rrtry();
  2740. for (cnt = 0; cnt < 5; cnt++)
  2741. mtk_nand_sprmset_rrtry(add_reg[cnt], data_tbl[retryCount][cnt]);
  2742. if (3 == retryCount)
  2743. mtk_nand_rren_rrtry(TRUE);
  2744. else if (6 > retryCount)
  2745. mtk_nand_rren_rrtry(FALSE);
  2746. if (7 == retryCount) { /* to exit */
  2747. mtk_nand_device_reset();
  2748. mtk_nand_reset();
  2749. /* should do NAND DEVICE interface change under sync mode */
  2750. }
  2751. DRV_WriteReg32(NFI_ACCCON_REG32, acccon);
  2752. }
  2753. #endif
  2754. static void mtk_nand_micron_rrtry(struct mtd_info *mtd, flashdev_info_t deviceinfo, u32 feature,
  2755. bool defValue)
  2756. {
  2757. /* u32 feature = deviceinfo.feature_set.FeatureSet.readRetryStart+retryCount; */
  2758. mtk_nand_SetFeature(mtd, deviceinfo.feature_set.FeatureSet.sfeatureCmd,
  2759. deviceinfo.feature_set.FeatureSet.readRetryAddress,
  2760. (u8 *) &feature, 4);
  2761. }
  2762. static int g_sandisk_retry_case; /* for new read retry table case 1,2,3,4 */
  2763. static void mtk_nand_sandisk_rrtry(struct mtd_info *mtd, flashdev_info_t deviceinfo, u32 feature,
  2764. bool defValue)
  2765. {
  2766. /* u32 feature = deviceinfo.feature_set.FeatureSet.readRetryStart+retryCount; */
  2767. if (FALSE == defValue) {
  2768. mtk_nand_reset();
  2769. } else {
  2770. mtk_nand_device_reset();
  2771. mtk_nand_reset();
  2772. /* should do NAND DEVICE interface change under sync mode */
  2773. }
  2774. mtk_nand_SetFeature(mtd, deviceinfo.feature_set.FeatureSet.sfeatureCmd,
  2775. deviceinfo.feature_set.FeatureSet.readRetryAddress,
  2776. (u8 *) &feature, 4);
  2777. if (FALSE == defValue) {
  2778. if (g_sandisk_retry_case > 1) { /* case 3 */
  2779. if (g_sandisk_retry_case == 3) {
  2780. u32 timeout = TIMEOUT_3;
  2781. mtk_nand_reset();
  2782. DRV_WriteReg(NFI_CNFG_REG16, (CNFG_OP_CUST | CNFG_BYTE_RW));
  2783. mtk_nand_set_command(0x5C);
  2784. mtk_nand_set_command(0xC5);
  2785. mtk_nand_set_command(0x55);
  2786. mtk_nand_set_address(0x00, 0, 1, 0); /* test mode entry */
  2787. mtk_nand_status_ready(STA_NFI_OP_MASK);
  2788. DRV_WriteReg32(NFI_CON_REG16, 1 << CON_NFI_SEC_SHIFT);
  2789. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BWR);
  2790. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  2791. WAIT_NFI_PIO_READY(timeout);
  2792. DRV_WriteReg8(NFI_DATAW_REG32, 0x01);
  2793. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN)
  2794. && (timeout--))
  2795. ;
  2796. mtk_nand_reset();
  2797. timeout = TIMEOUT_3;
  2798. mtk_nand_set_command(0x55);
  2799. /* changing parameter LMFLGFIX_NEXT = 1 to all die */
  2800. mtk_nand_set_address(0x23, 0, 1, 0);
  2801. mtk_nand_status_ready(STA_NFI_OP_MASK);
  2802. DRV_WriteReg32(NFI_CON_REG16, 1 << CON_NFI_SEC_SHIFT);
  2803. NFI_SET_REG32(NFI_CON_REG16, CON_NFI_BWR);
  2804. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  2805. WAIT_NFI_PIO_READY(timeout);
  2806. DRV_WriteReg8(NFI_DATAW_REG32, 0xC0);
  2807. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN)
  2808. && (timeout--))
  2809. ;
  2810. mtk_nand_reset();
  2811. pr_debug("Case3# Set LMFLGFIX_NEXT=1\n");
  2812. }
  2813. mtk_nand_set_command(0x25);
  2814. pr_debug("Case2#3# Set cmd 25\n");
  2815. }
  2816. mtk_nand_set_command(deviceinfo.feature_set.FeatureSet.readRetryPreCmd);
  2817. }
  2818. }
  2819. u16 sandisk_19nm_rr_table[18] = {
  2820. 0x0000,
  2821. 0xFF0F, 0xEEFE, 0xDDFD, 0x11EE, /* 04h[7:4] | 07h[7:4] | 04h[3:0] | 05h[7:4] */
  2822. 0x22ED, 0x33DF, 0xCDDE, 0x01DD,
  2823. 0x0211, 0x1222, 0xBD21, 0xAD32,
  2824. 0x9DF0, 0xBCEF, 0xACDC, 0x9CFF,
  2825. 0x0000 /* align */
  2826. };
  2827. static void sandisk_19nm_rr_init(void)
  2828. {
  2829. u32 reg_val = 0;
  2830. u32 count = 0;
  2831. u32 timeout = 0xffff;
  2832. /* u32 u4RandomSetting; */
  2833. u32 acccon;
  2834. acccon = DRV_Reg32(NFI_ACCCON_REG32);
  2835. DRV_WriteReg32(NFI_ACCCON_REG32, 0x31C08669); /* to fit read retry timing */
  2836. mtk_nand_reset();
  2837. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW);
  2838. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2839. mtk_nand_set_command(0x3B);
  2840. mtk_nand_set_command(0xB9);
  2841. for (count = 0; count < 9; count++) {
  2842. mtk_nand_set_command(0x53);
  2843. mtk_nand_set_address((0x04 + count), 0, 1, 0);
  2844. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  2845. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  2846. timeout = 0xffff;
  2847. WAIT_NFI_PIO_READY(timeout);
  2848. DRV_WriteReg32(NFI_DATAW_REG32, 0x00);
  2849. mtk_nand_reset();
  2850. }
  2851. DRV_WriteReg32(NFI_ACCCON_REG32, acccon);
  2852. }
  2853. static void sandisk_19nm_rr_loading(u32 retryCount, bool defValue)
  2854. {
  2855. u32 reg_val = 0;
  2856. u32 timeout = 0xffff;
  2857. u32 acccon;
  2858. u8 count;
  2859. u8 cmd_reg[4] = { 0x4, 0x5, 0x7 };
  2860. acccon = DRV_Reg32(NFI_ACCCON_REG32);
  2861. DRV_WriteReg32(NFI_ACCCON_REG32, 0x31C08669); /* to fit read retry timing */
  2862. mtk_nand_reset();
  2863. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW);
  2864. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2865. if ((0 != retryCount) || defValue)
  2866. mtk_nand_set_command(0xD6);
  2867. mtk_nand_set_command(0x3B);
  2868. mtk_nand_set_command(0xB9);
  2869. for (count = 0; count < 3; count++) {
  2870. mtk_nand_set_command(0x53);
  2871. mtk_nand_set_address(cmd_reg[count], 0, 1, 0);
  2872. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  2873. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  2874. timeout = 0xffff;
  2875. WAIT_NFI_PIO_READY(timeout);
  2876. if (count == 0)
  2877. DRV_WriteReg32(NFI_DATAW_REG32,
  2878. (((sandisk_19nm_rr_table[retryCount] & 0xF000) >> 8) |
  2879. ((sandisk_19nm_rr_table[retryCount] & 0x00F0) >> 4)));
  2880. else if (count == 1)
  2881. DRV_WriteReg32(NFI_DATAW_REG32,
  2882. ((sandisk_19nm_rr_table[retryCount] & 0x000F) << 4));
  2883. else if (count == 2)
  2884. DRV_WriteReg32(NFI_DATAW_REG32,
  2885. ((sandisk_19nm_rr_table[retryCount] & 0x0F00) >> 4));
  2886. mtk_nand_reset();
  2887. }
  2888. if (!defValue)
  2889. mtk_nand_set_command(0xB6);
  2890. DRV_WriteReg32(NFI_ACCCON_REG32, acccon);
  2891. }
  2892. static void mtk_nand_sandisk_19nm_rrtry(struct mtd_info *mtd, flashdev_info_t deviceinfo,
  2893. u32 retryCount, bool defValue)
  2894. {
  2895. if ((retryCount == 0) && (!defValue))
  2896. sandisk_19nm_rr_init();
  2897. sandisk_19nm_rr_loading(retryCount, defValue);
  2898. }
  2899. #define HYNIX_RR_TABLE_SIZE (1026) /* hynix read retry table size */
  2900. #define SINGLE_RR_TABLE_SIZE (64)
  2901. #define READ_RETRY_STEP (devinfo.feature_set.FeatureSet.readRetryCnt + \
  2902. devinfo.feature_set.FeatureSet.readRetryStart) /* 8 step or 12 step to fix read retry table */
  2903. #define HYNIX_16NM_RR_TABLE_SIZE ((READ_RETRY_STEP == 12)?(784):(528)) /* hynix read retry table size */
  2904. #define SINGLE_RR_TABLE_16NM_SIZE ((READ_RETRY_STEP == 12)?(48):(32))
  2905. u8 nand_hynix_rr_table[(HYNIX_RR_TABLE_SIZE + 16) / 16 * 16]; /* align as 16 byte */
  2906. #define NAND_HYX_RR_TBL_BUF nand_hynix_rr_table
  2907. static u8 real_hynix_rr_table_idx;
  2908. static u32 g_hynix_retry_count;
  2909. static bool hynix_rr_table_select(u8 table_index, flashdev_info_t *deviceinfo)
  2910. {
  2911. u32 i;
  2912. u32 table_size = (deviceinfo->feature_set.FeatureSet.rtype ==
  2913. RTYPE_HYNIX_16NM) ? SINGLE_RR_TABLE_16NM_SIZE : SINGLE_RR_TABLE_SIZE;
  2914. for (i = 0; i < table_size; i++) {
  2915. u8 *temp_rr_table = (u8 *) NAND_HYX_RR_TBL_BUF + table_size * table_index * 2 + 2;
  2916. u8 *temp_inversed_rr_table = (u8 *) NAND_HYX_RR_TBL_BUF + table_size * table_index * 2 + table_size + 2;
  2917. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  2918. temp_rr_table += 14;
  2919. temp_inversed_rr_table += 14;
  2920. }
  2921. if (0xFF != (temp_rr_table[i] ^ temp_inversed_rr_table[i]))
  2922. return FALSE; /* error table */
  2923. }
  2924. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM)
  2925. table_size += 16;
  2926. else
  2927. table_size += 2;
  2928. for (i = 0; i < table_size; i++) {
  2929. pr_debug("%02X ", NAND_HYX_RR_TBL_BUF[i]);
  2930. if ((i + 1) % 8 == 0)
  2931. pr_debug("\n");
  2932. }
  2933. return TRUE; /* correct table */
  2934. }
  2935. static void HYNIX_RR_TABLE_READ(flashdev_info_t *deviceinfo)
  2936. {
  2937. u32 reg_val = 0;
  2938. u32 read_count = 0, max_count = HYNIX_RR_TABLE_SIZE;
  2939. u32 timeout = 0xffff;
  2940. u8 *rr_table = (u8 *) (NAND_HYX_RR_TBL_BUF);
  2941. u8 table_index = 0;
  2942. u8 add_reg1[3] = { 0xFF, 0xCC };
  2943. u8 data_reg1[3] = { 0x40, 0x4D };
  2944. u8 cmd_reg[6] = { 0x16, 0x17, 0x04, 0x19, 0x00 };
  2945. u8 add_reg2[6] = { 0x00, 0x00, 0x00, 0x02, 0x00 };
  2946. bool RR_TABLE_EXIST = TRUE;
  2947. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  2948. read_count = 1;
  2949. add_reg1[1] = 0x38;
  2950. data_reg1[1] = 0x52;
  2951. max_count = HYNIX_16NM_RR_TABLE_SIZE;
  2952. if (READ_RETRY_STEP == 12)
  2953. add_reg2[2] = 0x1F;
  2954. }
  2955. mtk_nand_device_reset();
  2956. /* take care under sync mode. need change nand device inferface xiaolei */
  2957. mtk_nand_reset();
  2958. DRV_WriteReg(NFI_CNFG_REG16, (CNFG_OP_CUST | CNFG_BYTE_RW));
  2959. mtk_nand_set_command(0x36);
  2960. for (; read_count < 2; read_count++) {
  2961. mtk_nand_set_address(add_reg1[read_count], 0, 1, 0);
  2962. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  2963. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  2964. timeout = 0xffff;
  2965. WAIT_NFI_PIO_READY(timeout);
  2966. DRV_WriteReg32(NFI_DATAW_REG32, data_reg1[read_count]);
  2967. mtk_nand_reset();
  2968. }
  2969. for (read_count = 0; read_count < 5; read_count++)
  2970. mtk_nand_set_command(cmd_reg[read_count]);
  2971. for (read_count = 0; read_count < 5; read_count++)
  2972. mtk_nand_set_address(add_reg2[read_count], 0, 1, 0);
  2973. mtk_nand_set_command(0x30);
  2974. DRV_WriteReg(NFI_CNRNB_REG16, 0xF1);
  2975. timeout = 0xffff;
  2976. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--))
  2977. ;
  2978. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW | CNFG_READ_EN);
  2979. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2980. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BRD | (2 << CON_NFI_SEC_SHIFT)));
  2981. DRV_WriteReg(NFI_STRDATA_REG16, 0x1);
  2982. timeout = 0xffff;
  2983. read_count = 0; /* how???? */
  2984. while ((read_count < max_count) && timeout) {
  2985. WAIT_NFI_PIO_READY(timeout);
  2986. *rr_table++ = (unsigned char) DRV_Reg32(NFI_DATAR_REG32);
  2987. read_count++;
  2988. timeout = 0xFFFF;
  2989. }
  2990. mtk_nand_device_reset();
  2991. /* take care under sync mode. need change nand device inferface xiaolei */
  2992. reg_val = (CNFG_OP_CUST | CNFG_BYTE_RW);
  2993. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  2994. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  2995. mtk_nand_set_command(0x36);
  2996. mtk_nand_set_address(0x38, 0, 1, 0);
  2997. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  2998. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  2999. WAIT_NFI_PIO_READY(timeout);
  3000. DRV_WriteReg32(NFI_DATAW_REG32, 0x00);
  3001. mtk_nand_reset();
  3002. mtk_nand_set_command(0x16);
  3003. mtk_nand_set_command(0x00);
  3004. mtk_nand_set_address(0x00, 0, 1, 0); /* dummy read, add don't care */
  3005. mtk_nand_set_command(0x30);
  3006. } else {
  3007. DRV_WriteReg(NFI_CNFG_REG16, reg_val);
  3008. mtk_nand_set_command(0x38);
  3009. }
  3010. timeout = 0xffff;
  3011. while (!(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY_RETURN) && (timeout--))
  3012. ;
  3013. rr_table = (u8 *) (NAND_HYX_RR_TBL_BUF);
  3014. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX) {
  3015. if ((rr_table[0] != 8) || (rr_table[1] != 8)) {
  3016. RR_TABLE_EXIST = FALSE;
  3017. mtk_nand_assert(0);
  3018. }
  3019. } else if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  3020. for (read_count = 0; read_count < 8; read_count++) {
  3021. if ((rr_table[read_count] != 8) || (rr_table[read_count + 8] != 4)) {
  3022. RR_TABLE_EXIST = FALSE;
  3023. break;
  3024. }
  3025. }
  3026. }
  3027. if (RR_TABLE_EXIST) {
  3028. for (table_index = 0; table_index < 8; table_index++) {
  3029. if (hynix_rr_table_select(table_index, deviceinfo)) {
  3030. real_hynix_rr_table_idx = table_index;
  3031. pr_debug("Hynix rr_tbl_id %d\n", real_hynix_rr_table_idx);
  3032. break;
  3033. }
  3034. }
  3035. if (table_index == 8)
  3036. mtk_nand_assert(0);
  3037. } else {
  3038. pr_err("Hynix RR table index error!\n");
  3039. }
  3040. }
  3041. static void HYNIX_Set_RR_Para(u32 rr_index, flashdev_info_t *deviceinfo)
  3042. {
  3043. /* u32 reg_val = 0; */
  3044. u32 timeout = 0xffff;
  3045. u8 count, max_count = 8;
  3046. u8 add_reg[9] = { 0xCC, 0xBF, 0xAA, 0xAB, 0xCD, 0xAD, 0xAE, 0xAF };
  3047. u8 *hynix_rr_table =
  3048. (u8 *) NAND_HYX_RR_TBL_BUF + SINGLE_RR_TABLE_SIZE * real_hynix_rr_table_idx * 2 + 2;
  3049. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  3050. add_reg[0] = 0x38; /* 0x38, 0x39, 0x3A, 0x3B */
  3051. for (count = 1; count < 4; count++)
  3052. add_reg[count] = add_reg[0] + count;
  3053. hynix_rr_table += 14;
  3054. max_count = 4;
  3055. }
  3056. mtk_nand_reset();
  3057. DRV_WriteReg(NFI_CNFG_REG16, (CNFG_OP_CUST | CNFG_BYTE_RW));
  3058. /* mtk_nand_set_command(0x36); */
  3059. for (count = 0; count < max_count; count++) {
  3060. mtk_nand_set_command(0x36);
  3061. mtk_nand_set_address(add_reg[count], 0, 1, 0);
  3062. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_BWR | (1 << CON_NFI_SEC_SHIFT)));
  3063. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  3064. timeout = 0xffff;
  3065. WAIT_NFI_PIO_READY(timeout);
  3066. if (timeout == 0) {
  3067. pr_notice("HYNIX_Set_RR_Para timeout\n");
  3068. break;
  3069. }
  3070. DRV_WriteReg32(NFI_DATAW_REG32, hynix_rr_table[rr_index * max_count + count]);
  3071. mtk_nand_reset();
  3072. }
  3073. mtk_nand_set_command(0x16);
  3074. }
  3075. #if 0
  3076. static void HYNIX_Get_RR_Para(u32 rr_index, flashdev_info_t *deviceinfo)
  3077. {
  3078. u32 reg_val = 0;
  3079. u32 timeout = 0xffff;
  3080. u8 count, max_count = 8;
  3081. u8 add_reg[9] = { 0xCC, 0xBF, 0xAA, 0xAB, 0xCD, 0xAD, 0xAE, 0xAF };
  3082. u8 *hynix_rr_table =
  3083. (u8 *) NAND_HYX_RR_TBL_BUF + SINGLE_RR_TABLE_SIZE * real_hynix_rr_table_idx * 2 + 2;
  3084. if (deviceinfo->feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM) {
  3085. add_reg[0] = 0x38; /* 0x38, 0x39, 0x3A, 0x3B */
  3086. for (count = 1; count < 4; count++)
  3087. add_reg[count] = add_reg[0] + count;
  3088. hynix_rr_table += 14;
  3089. max_count = 4;
  3090. }
  3091. mtk_nand_reset();
  3092. DRV_WriteReg(NFI_CNFG_REG16, (CNFG_OP_CUST | CNFG_BYTE_RW | CNFG_READ_EN));
  3093. /* mtk_nand_set_command(0x37); */
  3094. for (count = 0; count < max_count; count++) {
  3095. mtk_nand_set_command(0x37);
  3096. mtk_nand_set_address(add_reg[count], 0, 1, 0);
  3097. DRV_WriteReg(NFI_CON_REG16, (CON_NFI_SRD | (1 << CON_NFI_NOB_SHIFT)));
  3098. DRV_WriteReg(NFI_STRDATA_REG16, 1);
  3099. timeout = 0xffff;
  3100. WAIT_NFI_PIO_READY(timeout);
  3101. if (timeout == 0)
  3102. pr_notice("HYNIX_Get_RR_Para timeout\n");
  3103. /* DRV_WriteReg32(NFI_DATAW_REG32, hynix_rr_table[rr_index*max_count + count]); */
  3104. pr_debug("Get[%02X]%02X\n", add_reg[count], DRV_Reg8(NFI_DATAR_REG32));
  3105. mtk_nand_reset();
  3106. }
  3107. }
  3108. #endif
  3109. static void mtk_nand_hynix_rrtry(struct mtd_info *mtd, flashdev_info_t deviceinfo, u32 retryCount,
  3110. bool defValue)
  3111. {
  3112. if (defValue == FALSE) {
  3113. if (g_hynix_retry_count == READ_RETRY_STEP)
  3114. g_hynix_retry_count = 0;
  3115. pr_debug("Hynix Retry %d\n", g_hynix_retry_count);
  3116. HYNIX_Set_RR_Para(g_hynix_retry_count, &deviceinfo);
  3117. /* HYNIX_Get_RR_Para(g_hynix_retry_count, &deviceinfo); */
  3118. g_hynix_retry_count++;
  3119. }
  3120. }
  3121. static void mtk_nand_hynix_16nm_rrtry(struct mtd_info *mtd, flashdev_info_t deviceinfo,
  3122. u32 retryCount, bool defValue)
  3123. {
  3124. if (defValue == FALSE) {
  3125. if (g_hynix_retry_count == READ_RETRY_STEP)
  3126. g_hynix_retry_count = 0;
  3127. pr_debug("Hynix 16nm Retry %d\n", g_hynix_retry_count);
  3128. HYNIX_Set_RR_Para(g_hynix_retry_count, &deviceinfo);
  3129. /* mb(); */
  3130. /* HYNIX_Get_RR_Para(g_hynix_retry_count, &deviceinfo); */
  3131. g_hynix_retry_count++;
  3132. }
  3133. }
  3134. u32 special_rrtry_setting[37] = {
  3135. 0x00000000, 0x7C00007C, 0x787C0004, 0x74780078,
  3136. 0x7C007C08, 0x787C7C00, 0x74787C7C, 0x70747C00,
  3137. 0x7C007800, 0x787C7800, 0x74787800, 0x70747800,
  3138. 0x6C707800, 0x00040400, 0x7C000400, 0x787C040C,
  3139. 0x7478040C, 0x7C000810, 0x00040810, 0x04040C0C,
  3140. 0x00040C10, 0x00081014, 0x000C1418, 0x7C040C0C,
  3141. 0x74787478, 0x70747478, 0x6C707478, 0x686C7478,
  3142. 0x74787078, 0x70747078, 0x686C7078, 0x6C707078,
  3143. 0x6C706C78, 0x686C6C78, 0x64686C78, 0x686C6874,
  3144. 0x64686874,
  3145. };
  3146. static u32 mtk_nand_rrtry_setting(flashdev_info_t deviceinfo, enum readRetryType type,
  3147. u32 retryStart, u32 loopNo)
  3148. {
  3149. u32 value;
  3150. /* if(RTYPE_MICRON == type || RTYPE_SANDISK== type || RTYPE_TOSHIBA== type || RTYPE_HYNIX== type) */
  3151. {
  3152. if (retryStart != 0xFFFFFFFF)
  3153. value = retryStart + loopNo;
  3154. else
  3155. value = special_rrtry_setting[loopNo];
  3156. }
  3157. return value;
  3158. }
  3159. typedef void (*rrtryFunctionType) (struct mtd_info *mtd, flashdev_info_t deviceinfo, u32 feature,
  3160. bool defValue);
  3161. static rrtryFunctionType rtyFuncArray[] = {
  3162. mtk_nand_micron_rrtry,
  3163. mtk_nand_sandisk_rrtry,
  3164. mtk_nand_sandisk_19nm_rrtry,
  3165. mtk_nand_toshiba_rrtry,
  3166. mtk_nand_hynix_rrtry,
  3167. mtk_nand_hynix_16nm_rrtry
  3168. };
  3169. static void mtk_nand_rrtry_func(struct mtd_info *mtd, flashdev_info_t deviceinfo, u32 feature,
  3170. bool defValue)
  3171. {
  3172. rtyFuncArray[deviceinfo.feature_set.FeatureSet.rtype] (mtd, deviceinfo, feature, defValue);
  3173. }
  3174. /******************************************************************************
  3175. * mtk_nand_exec_read_page
  3176. *
  3177. * DESCRIPTION:
  3178. * Read a page data !
  3179. *
  3180. * PARAMETERS:
  3181. * struct mtd_info *mtd, u32 u4RowAddr, u32 u4PageSize,
  3182. * u8* pPageBuf, u8* pFDMBuf
  3183. *
  3184. * RETURNS:
  3185. * None
  3186. *
  3187. * NOTES:
  3188. * None
  3189. *
  3190. ******************************************************************************/
  3191. int mtk_nand_exec_read_page(struct mtd_info *mtd, u32 u4RowAddr, u32 u4PageSize, u8 *pPageBuf,
  3192. u8 *pFDMBuf)
  3193. {
  3194. u8 *buf;
  3195. int bRet = ERR_RTN_SUCCESS;
  3196. struct nand_chip *nand = mtd->priv;
  3197. u32 u4SecNum = u4PageSize >> host->hw->nand_sec_shift;
  3198. u32 backup_corrected, backup_failed;
  3199. bool readRetry = FALSE;
  3200. int retryCount = 0;
  3201. /* u32 val; */
  3202. u32 tempBitMap;
  3203. #if 0
  3204. u32 bitMap, i;
  3205. #endif
  3206. #ifdef NAND_PFM
  3207. struct timeval pfm_time_read;
  3208. #endif
  3209. #if 0
  3210. unsigned short PageFmt_Reg = 0;
  3211. unsigned int NAND_ECC_Enc_Reg = 0;
  3212. unsigned int NAND_ECC_Dec_Reg = 0;
  3213. #endif
  3214. /* MSG(INIT, "mtk_nand_exec_read_page, host->hw->nand_sec_shift: %d\n", host->hw->nand_sec_shift); */
  3215. /* MSG(INIT, "mtk_nand_exec_read_page,u4RowAddr: 0x%x\n", u4RowAddr); */
  3216. PFM_BEGIN(pfm_time_read);
  3217. tempBitMap = 0;
  3218. /* flush_icache_range(pPageBuf, (pPageBuf + u4PageSize));//flush_cache_all();//cache flush */
  3219. if (((unsigned long)pPageBuf % 16) && local_buffer_16_align) {
  3220. buf = local_buffer_16_align;
  3221. /* pr_debug("[xl] read buf (1) 0x%lx\n",(unsigned long)buf); */
  3222. } else {
  3223. if (virt_addr_valid(pPageBuf) == 0) { /* It should be allocated by vmalloc */
  3224. buf = local_buffer_16_align;
  3225. /* pr_debug("[xl] read buf (2) 0x%lx\n",(unsigned long)buf); */
  3226. } else {
  3227. buf = pPageBuf;
  3228. /* pr_debug("[xl] read buf (3) 0x%lx\n",(unsigned long)buf); */
  3229. }
  3230. }
  3231. backup_corrected = mtd->ecc_stats.corrected;
  3232. backup_failed = mtd->ecc_stats.failed;
  3233. #if CFG_2CS_NAND
  3234. if (g_bTricky_CS)
  3235. u4RowAddr = mtk_nand_cs_on(nand, NFI_TRICKY_CS, u4RowAddr);
  3236. #endif
  3237. do {
  3238. if (use_randomizer && u4RowAddr >= RAND_START_ADDR)
  3239. mtk_nand_turn_on_randomizer(u4RowAddr, 0, 0);
  3240. else if (pre_randomizer && u4RowAddr < RAND_START_ADDR)
  3241. mtk_nand_turn_on_randomizer(u4RowAddr, 0, 0);
  3242. if (mtk_nand_ready_for_read(nand, u4RowAddr, 0, u4SecNum, true, buf)) {
  3243. if (!mtk_nand_read_page_data(mtd, buf, u4PageSize)) {
  3244. pr_err("mtk_nand_read_page_data fail\n");
  3245. bRet = ERR_RTN_FAIL;
  3246. }
  3247. dma_unmap_sg(mtk_dev, &mtk_sg, 1, mtk_dir);
  3248. if (!mtk_nand_status_ready(STA_NAND_BUSY)) {
  3249. pr_err("mtk_nand_status_ready fail\n");
  3250. bRet = ERR_RTN_FAIL;
  3251. }
  3252. if (g_bHwEcc) {
  3253. if (!mtk_nand_check_dececc_done(u4SecNum)) {
  3254. pr_err("mtk_nand_check_dececc_done fail\n");
  3255. bRet = ERR_RTN_FAIL;
  3256. }
  3257. }
  3258. mtk_nand_read_fdm_data(pFDMBuf, u4SecNum);
  3259. if (g_bHwEcc) {
  3260. if (!mtk_nand_check_bch_error(mtd, buf, pFDMBuf,
  3261. u4SecNum - 1, u4RowAddr, &tempBitMap)) {
  3262. if (devinfo.vendor != VEND_NONE)
  3263. readRetry = TRUE;
  3264. pr_debug("mtk_nand_check_bch_error fail, retryCount:%d\n",
  3265. retryCount);
  3266. bRet = ERR_RTN_BCH_FAIL;
  3267. } else {
  3268. if (0 != (DRV_Reg32(NFI_STA_REG32) & STA_READ_EMPTY)
  3269. && 0 != retryCount) { /* if empty */
  3270. pr_err("NFI read retry read empty page, return as uncorrectable\n");
  3271. mtd->ecc_stats.failed += u4SecNum;
  3272. bRet = ERR_RTN_BCH_FAIL;
  3273. }
  3274. }
  3275. }
  3276. mtk_nand_stop_read();
  3277. } else {
  3278. dma_unmap_sg(mtk_dev, &mtk_sg, 1, mtk_dir);
  3279. }
  3280. if (use_randomizer && u4RowAddr >= RAND_START_ADDR)
  3281. mtk_nand_turn_off_randomizer();
  3282. else if (pre_randomizer && u4RowAddr < RAND_START_ADDR)
  3283. mtk_nand_turn_off_randomizer();
  3284. #if 0
  3285. if (bRet == ERR_RTN_BCH_FAIL) {
  3286. tempBitMap -= (tempBitMap & bitMap);
  3287. if (tempBitMap != 0) {
  3288. MSG(INIT, "read retry has partial data correct 0x%x\n", tempBitMap);
  3289. for (i = 0; i < u4SecNum; i++) {
  3290. if ((tempBitMap & (1 << i)) != 0) {
  3291. memcpy((temp_buffer_16_align + (u4SecSize * i)),
  3292. (buf + (u4SecSize * i)), u4SecSize);
  3293. memcpy((temp_buffer_16_align + mtd->writesize +
  3294. (8 * i)), (pFDMBuf + (8 * i)), 8);
  3295. }
  3296. }
  3297. bitMap |= tempBitMap;
  3298. }
  3299. if (bitMap == ((1 << u4SecNum) - 1)) {
  3300. MSG(INIT,
  3301. "read retry has reformat the page data correctly @ page 0x%x\n",
  3302. u4RowAddr);
  3303. memcpy(buf, temp_buffer_16_align, mtd->writesize);
  3304. memcpy(pFDMBuf, (temp_buffer_16_align + mtd->writesize),
  3305. 8 * u4SecNum);
  3306. mtd->ecc_stats.corrected++;
  3307. mtd->ecc_stats.failed = backup_failed;
  3308. bRet = ERR_RTN_SUCCESS;
  3309. }
  3310. }
  3311. #endif
  3312. if (bRet == ERR_RTN_BCH_FAIL) {
  3313. u32 feature;
  3314. tempBitMap = 0;
  3315. /* feature= devinfo.feature_set.FeatureSet.readRetryStart+retryCount; */
  3316. feature = mtk_nand_rrtry_setting(devinfo, devinfo.feature_set.FeatureSet.rtype,
  3317. devinfo.feature_set.FeatureSet.readRetryStart,
  3318. retryCount);
  3319. if (retryCount < devinfo.feature_set.FeatureSet.readRetryCnt) {
  3320. mtd->ecc_stats.corrected = backup_corrected;
  3321. mtd->ecc_stats.failed = backup_failed;
  3322. mtk_nand_rrtry_func(mtd, devinfo, feature, FALSE);
  3323. retryCount++;
  3324. } else {
  3325. feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  3326. /* sandisk case 2/3/4 */
  3327. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_SANDISK)
  3328. && (g_sandisk_retry_case < 3)) {
  3329. g_sandisk_retry_case++;
  3330. pr_debug("Sandisk read retry case#%d\n",
  3331. g_sandisk_retry_case);
  3332. tempBitMap = 0;
  3333. mtd->ecc_stats.corrected = backup_corrected;
  3334. mtd->ecc_stats.failed = backup_failed;
  3335. mtk_nand_rrtry_func(mtd, devinfo, feature, FALSE);
  3336. /* if((g_sandisk_retry_case == 0) || (g_sandisk_retry_case == 2)) */
  3337. /* { */
  3338. /* mtk_nand_set_command(0x26); */
  3339. /* } */
  3340. retryCount = 0;
  3341. } else {
  3342. mtk_nand_rrtry_func(mtd, devinfo, feature, TRUE);
  3343. readRetry = FALSE;
  3344. g_sandisk_retry_case = 0;
  3345. }
  3346. }
  3347. if ((g_sandisk_retry_case == 1) || (g_sandisk_retry_case == 3)) {
  3348. mtk_nand_set_command(0x26);
  3349. pr_debug("Case1#3# Set cmd 26\n");
  3350. }
  3351. } else {
  3352. if ((retryCount != 0) && MLC_DEVICE) {
  3353. u32 feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  3354. mtk_nand_rrtry_func(mtd, devinfo, feature, TRUE);
  3355. }
  3356. readRetry = FALSE;
  3357. g_sandisk_retry_case = 0;
  3358. }
  3359. if (TRUE == readRetry)
  3360. bRet = ERR_RTN_SUCCESS;
  3361. } while (readRetry);
  3362. if (retryCount != 0) {
  3363. u32 feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  3364. if (bRet == ERR_RTN_SUCCESS) {
  3365. pr_debug("u4RowAddr:0x%x read retry pass, retrycnt:%d ENUM0:%x,ENUM1:%x,mtd_ecc(A):%x,mtd_ecc(B):%x\n",
  3366. u4RowAddr, retryCount, DRV_Reg32(ECC_DECENUM1_REG32),
  3367. DRV_Reg32(ECC_DECENUM0_REG32), mtd->ecc_stats.failed, backup_failed);
  3368. mtd->ecc_stats.corrected++;
  3369. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM)
  3370. || (devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX)) {
  3371. g_hynix_retry_count--;
  3372. }
  3373. } else {
  3374. pr_err("u4RowAddr:0x%x read retry fail, mtd_ecc(A):%x ,fail, mtd_ecc(B):%x\n",
  3375. u4RowAddr, mtd->ecc_stats.failed, backup_failed);
  3376. }
  3377. mtk_nand_rrtry_func(mtd, devinfo, feature, TRUE);
  3378. g_sandisk_retry_case = 0;
  3379. }
  3380. /* flush_icache_range(pPageBuf, (pPageBuf + u4PageSize));//flush_cache_all();//cache flush */
  3381. if (buf == local_buffer_16_align) {
  3382. memcpy(pPageBuf, buf, u4PageSize);
  3383. /* pr_debug("[xl] mtk_nand_exec_read_page memcpy 0x%x 0x%x\n", pPageBuf[0],buf[0]); */
  3384. }
  3385. /* else */
  3386. /* pr_debug("[xl] mtk_nand_exec_read_page no memcpy 0x%x 0x%x\n", pPageBuf[0],buf[0]); */
  3387. if (bRet != ERR_RTN_SUCCESS) {
  3388. pr_debug("ECC uncorrectable , fake buffer returned\n");
  3389. memset(pPageBuf, 0xff, u4PageSize);
  3390. memset(pFDMBuf, 0xff, u4SecNum * 8);
  3391. }
  3392. PFM_END_R(pfm_time_read, u4PageSize + 32);
  3393. return bRet;
  3394. }
  3395. bool mtk_nand_exec_read_sector(struct mtd_info *mtd, u32 u4RowAddr, u32 u4ColAddr, u32 u4PageSize,
  3396. u8 *pPageBuf, u8 *pFDMBuf, int subpageno)
  3397. {
  3398. u8 *buf;
  3399. int bRet = ERR_RTN_SUCCESS;
  3400. struct nand_chip *nand = mtd->priv;
  3401. u32 u4SecNum = subpageno;
  3402. u32 backup_corrected, backup_failed;
  3403. bool readRetry = FALSE;
  3404. int retryCount = 0;
  3405. u32 tempBitMap;
  3406. #ifdef NAND_PFM
  3407. struct timeval pfm_time_read;
  3408. #endif
  3409. #if 0
  3410. unsigned short PageFmt_Reg = 0;
  3411. unsigned int NAND_ECC_Enc_Reg = 0;
  3412. unsigned int NAND_ECC_Dec_Reg = 0;
  3413. #endif
  3414. /* MSG(INIT, "mtk_nand_exec_read_page, host->hw->nand_sec_shift: %d\n", host->hw->nand_sec_shift); */
  3415. PFM_BEGIN(pfm_time_read);
  3416. /* flush_icache_range(pPageBuf, (pPageBuf + u4PageSize));//flush_cache_all();//cache flush */
  3417. if (((unsigned long)pPageBuf % 16) && local_buffer_16_align) {
  3418. buf = local_buffer_16_align;
  3419. } else {
  3420. if (virt_addr_valid(pPageBuf) == 0) { /* It should be allocated by vmalloc */
  3421. buf = local_buffer_16_align;
  3422. } else {
  3423. buf = pPageBuf;
  3424. }
  3425. }
  3426. backup_corrected = mtd->ecc_stats.corrected;
  3427. backup_failed = mtd->ecc_stats.failed;
  3428. #if CFG_2CS_NAND
  3429. if (g_bTricky_CS)
  3430. u4RowAddr = mtk_nand_cs_on(nand, NFI_TRICKY_CS, u4RowAddr);
  3431. #endif
  3432. do {
  3433. if (use_randomizer && u4RowAddr >= RAND_START_ADDR)
  3434. mtk_nand_turn_on_randomizer(u4RowAddr, 0, 0);
  3435. else if (pre_randomizer && u4RowAddr < RAND_START_ADDR)
  3436. mtk_nand_turn_on_randomizer(u4RowAddr, 0, 0);
  3437. if (mtk_nand_ready_for_read(nand, u4RowAddr, u4ColAddr, u4SecNum, true, buf)) {
  3438. if (!mtk_nand_read_page_data(mtd, buf, u4PageSize)) {
  3439. pr_err("mtk_nand_read_page_data fail\n");
  3440. bRet = ERR_RTN_FAIL;
  3441. }
  3442. dma_unmap_sg(mtk_dev, &mtk_sg, 1, mtk_dir);
  3443. if (!mtk_nand_status_ready(STA_NAND_BUSY)) {
  3444. pr_err("mtk_nand_status_ready fail\n");
  3445. bRet = ERR_RTN_FAIL;
  3446. }
  3447. if (g_bHwEcc) {
  3448. if (!mtk_nand_check_dececc_done(u4SecNum)) {
  3449. pr_err("mtk_nand_check_dececc_done fail\n");
  3450. bRet = ERR_RTN_FAIL;
  3451. }
  3452. }
  3453. mtk_nand_read_fdm_data(pFDMBuf, u4SecNum);
  3454. if (g_bHwEcc) {
  3455. if (!mtk_nand_check_bch_error(mtd, buf, pFDMBuf, u4SecNum - 1, u4RowAddr, NULL)) {
  3456. if (devinfo.vendor != VEND_NONE)
  3457. readRetry = TRUE;
  3458. pr_debug("mtk_nand_check_bch_error fail, retryCount:%d\n",
  3459. retryCount);
  3460. bRet = ERR_RTN_BCH_FAIL;
  3461. } else {
  3462. if (0 != (DRV_Reg32(NFI_STA_REG32) & STA_READ_EMPTY)
  3463. && 0 != retryCount) { /* if empty */
  3464. pr_notice("NFI read retry read empty page, return as uncorrectable\n");
  3465. mtd->ecc_stats.failed += u4SecNum;
  3466. bRet = ERR_RTN_BCH_FAIL;
  3467. }
  3468. }
  3469. }
  3470. mtk_nand_stop_read();
  3471. } else {
  3472. dma_unmap_sg(mtk_dev, &mtk_sg, 1, mtk_dir);
  3473. }
  3474. if (use_randomizer && u4RowAddr >= RAND_START_ADDR)
  3475. mtk_nand_turn_off_randomizer();
  3476. else if (pre_randomizer && u4RowAddr < RAND_START_ADDR)
  3477. mtk_nand_turn_off_randomizer();
  3478. if (bRet == ERR_RTN_BCH_FAIL) {
  3479. /* u32 feature = devinfo.feature_set.FeatureSet.readRetryStart+retryCount; */
  3480. u32 feature =
  3481. mtk_nand_rrtry_setting(devinfo, devinfo.feature_set.FeatureSet.rtype,
  3482. devinfo.feature_set.FeatureSet.readRetryStart,
  3483. retryCount);
  3484. if (retryCount < devinfo.feature_set.FeatureSet.readRetryCnt) {
  3485. mtd->ecc_stats.corrected = backup_corrected;
  3486. mtd->ecc_stats.failed = backup_failed;
  3487. mtk_nand_rrtry_func(mtd, devinfo, feature, FALSE);
  3488. retryCount++;
  3489. } else {
  3490. feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  3491. /* sandisk case 2/3/4 */
  3492. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_SANDISK)
  3493. && (g_sandisk_retry_case < 3)) {
  3494. g_sandisk_retry_case++;
  3495. pr_debug("Sandisk read retry case#%d\n", g_sandisk_retry_case);
  3496. tempBitMap = 0;
  3497. mtd->ecc_stats.corrected = backup_corrected;
  3498. mtd->ecc_stats.failed = backup_failed;
  3499. mtk_nand_rrtry_func(mtd, devinfo, feature, FALSE);
  3500. /* if((g_sandisk_retry_case == 0) || (g_sandisk_retry_case == 2)) */
  3501. /* { */
  3502. /* mtk_nand_set_command(0x26); */
  3503. /* } */
  3504. retryCount = 0;
  3505. } else {
  3506. mtk_nand_rrtry_func(mtd, devinfo, feature, TRUE);
  3507. readRetry = FALSE;
  3508. g_sandisk_retry_case = 0;
  3509. }
  3510. }
  3511. if ((g_sandisk_retry_case == 1) || (g_sandisk_retry_case == 3)) {
  3512. mtk_nand_set_command(0x26);
  3513. pr_debug("Case1#3# Set cmd 26\n");
  3514. }
  3515. } else {
  3516. if ((retryCount != 0) && MLC_DEVICE) {
  3517. u32 feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  3518. mtk_nand_rrtry_func(mtd, devinfo, feature, TRUE);
  3519. }
  3520. readRetry = FALSE;
  3521. g_sandisk_retry_case = 0;
  3522. }
  3523. if (TRUE == readRetry)
  3524. bRet = ERR_RTN_SUCCESS;
  3525. } while (readRetry);
  3526. if (retryCount != 0) {
  3527. u32 feature = devinfo.feature_set.FeatureSet.readRetryDefault;
  3528. if (bRet == ERR_RTN_SUCCESS) {
  3529. pr_debug("u4RowAddr:0x%x read retry pass, retrycnt:%d ENUM0:%x,ENUM1:%x,\n",
  3530. u4RowAddr, retryCount, DRV_Reg32(ECC_DECENUM1_REG32),
  3531. DRV_Reg32(ECC_DECENUM0_REG32));
  3532. mtd->ecc_stats.corrected++;
  3533. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM)
  3534. || (devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX)) {
  3535. g_hynix_retry_count--;
  3536. }
  3537. }
  3538. mtk_nand_rrtry_func(mtd, devinfo, feature, TRUE);
  3539. g_sandisk_retry_case = 0;
  3540. }
  3541. /* flush_icache_range(pPageBuf, (pPageBuf + u4PageSize));//flush_cache_all();//cache flush */
  3542. if (buf == local_buffer_16_align)
  3543. memcpy(pPageBuf, buf, u4PageSize);
  3544. PFM_END_R(pfm_time_read, u4PageSize + 32);
  3545. if (bRet != ERR_RTN_SUCCESS) {
  3546. pr_debug("ECC uncorrectable , fake buffer returned\n");
  3547. memset(pPageBuf, 0xff, u4PageSize);
  3548. memset(pFDMBuf, 0xff, u4SecNum * 8);
  3549. }
  3550. return bRet;
  3551. }
  3552. /******************************************************************************
  3553. * mtk_nand_exec_write_page
  3554. *
  3555. * DESCRIPTION:
  3556. * Write a page data !
  3557. *
  3558. * PARAMETERS:
  3559. * struct mtd_info *mtd, u32 u4RowAddr, u32 u4PageSize,
  3560. * u8* pPageBuf, u8* pFDMBuf
  3561. *
  3562. * RETURNS:
  3563. * None
  3564. *
  3565. * NOTES:
  3566. * None
  3567. *
  3568. ******************************************************************************/
  3569. int mtk_nand_exec_write_page(struct mtd_info *mtd, u32 u4RowAddr, u32 u4PageSize, u8 *pPageBuf,
  3570. u8 *pFDMBuf)
  3571. {
  3572. struct nand_chip *chip = mtd->priv;
  3573. u32 u4SecNum = u4PageSize >> host->hw->nand_sec_shift;
  3574. u8 *buf;
  3575. u8 status;
  3576. #ifdef PWR_LOSS_SPOH
  3577. u32 time;
  3578. struct timeval pl_time_write;
  3579. suseconds_t duration;
  3580. #endif
  3581. #if 0
  3582. {
  3583. val = devinfo.feature_set.FeatureSet.readRetryDefault;
  3584. mtk_nand_SetFeature(mtd, devinfo.feature_set.FeatureSet.sfeatureCmd,
  3585. devinfo.feature_set.FeatureSet.readRetryAddress,
  3586. (u8 *) &val, 4);
  3587. mtk_nand_GetFeature(mtd, devinfo.feature_set.FeatureSet.gfeatureCmd,
  3588. devinfo.feature_set.FeatureSet.readRetryAddress,
  3589. (u8 *) &val, 4);
  3590. if ((val & 0xFF) != (devinfo.feature_set.FeatureSet.readRetryDefault & 0xFF)) {
  3591. MSG(INIT,
  3592. "mtk_nand_exec_write_page check read retry defalut value fail 0x%x\n",
  3593. val);
  3594. }
  3595. }
  3596. #endif
  3597. /* MSG(INIT, "mtk_nand_exec_write_page, page: 0x%x\n", u4RowAddr); */
  3598. #if CFG_2CS_NAND
  3599. if (g_bTricky_CS)
  3600. u4RowAddr = mtk_nand_cs_on(chip, NFI_TRICKY_CS, u4RowAddr);
  3601. #endif
  3602. if (use_randomizer && u4RowAddr >= RAND_START_ADDR)
  3603. mtk_nand_turn_on_randomizer(u4RowAddr, 1, 0);
  3604. else if (pre_randomizer && u4RowAddr < RAND_START_ADDR)
  3605. mtk_nand_turn_on_randomizer(u4RowAddr, 1, 0);
  3606. #ifdef _MTK_NAND_DUMMY_DRIVER_
  3607. if (dummy_driver_debug) {
  3608. unsigned long long time = sched_clock();
  3609. if (!((time * 123 + 59) % 32768)) {
  3610. pr_err("[NAND_DUMMY_DRIVER] Simulate write error at page: 0x%x\n",
  3611. u4RowAddr);
  3612. return -EIO;
  3613. }
  3614. }
  3615. #endif
  3616. /* flush_icache_range(pPageBuf, (pPageBuf + u4PageSize));//flush_cache_all();//cache flush */
  3617. #ifdef NAND_PFM
  3618. struct timeval pfm_time_write;
  3619. #endif
  3620. PFM_BEGIN(pfm_time_write);
  3621. if (((unsigned long)pPageBuf % 16) && local_buffer_16_align) {
  3622. pr_info("Data buffer not 16 bytes aligned: %p\n", pPageBuf);
  3623. memcpy(local_buffer_16_align, pPageBuf, mtd->writesize);
  3624. buf = local_buffer_16_align;
  3625. } else {
  3626. if (virt_addr_valid(pPageBuf) == 0) { /* It should be allocated by vmalloc */
  3627. memcpy(local_buffer_16_align, pPageBuf, mtd->writesize);
  3628. buf = local_buffer_16_align;
  3629. } else {
  3630. buf = pPageBuf;
  3631. }
  3632. }
  3633. if (mtk_nand_ready_for_write(chip, u4RowAddr, 0, true, buf)) {
  3634. mtk_nand_write_fdm_data(chip, pFDMBuf, u4SecNum);
  3635. (void)mtk_nand_write_page_data(mtd, buf, u4PageSize);
  3636. dma_unmap_sg(mtk_dev, &mtk_sg, 1, mtk_dir);
  3637. (void)mtk_nand_check_RW_count(u4PageSize);
  3638. mtk_nand_stop_write();
  3639. PL_NAND_BEGIN(pl_time_write);
  3640. PL_TIME_RAND_PROG(chip, u4RowAddr, time);
  3641. (void)mtk_nand_set_command(NAND_CMD_PAGEPROG);
  3642. PL_NAND_RESET(time);
  3643. {
  3644. #if CFG_PERFLOG_DEBUG
  3645. struct timeval stimer, etimer;
  3646. do_gettimeofday(&stimer);
  3647. #endif
  3648. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY)
  3649. ;
  3650. #if CFG_PERFLOG_DEBUG
  3651. do_gettimeofday(&etimer);
  3652. /* pr_debug("[Bean]Cal_timediff(&etimer,&stimer):0x%x\n", Cal_timediff(&etimer,&stimer)); */
  3653. g_NandPerfLog.WriteBusyTotalTime += Cal_timediff(&etimer, &stimer);
  3654. g_NandPerfLog.WriteBusyCount++;
  3655. #endif
  3656. }
  3657. } else {
  3658. dma_unmap_sg(mtk_dev, &mtk_sg, 1, mtk_dir);
  3659. pr_err("[Bean]mtk_nand_ready_for_write fail!\n");
  3660. }
  3661. PL_NAND_END(pl_time_write, duration);
  3662. PL_TIME_PROG(duration);
  3663. PFM_END_W(pfm_time_write, u4PageSize + 32);
  3664. if (use_randomizer && u4RowAddr >= RAND_START_ADDR)
  3665. mtk_nand_turn_off_randomizer();
  3666. else if (pre_randomizer && u4RowAddr < RAND_START_ADDR)
  3667. mtk_nand_turn_off_randomizer();
  3668. /* flush_icache_range(pPageBuf, (pPageBuf + u4PageSize));//flush_cache_all();//cache flush */
  3669. status = chip->waitfunc(mtd, chip);
  3670. /* pr_debug("[Bean]status:%d\n", status); */
  3671. if (status & NAND_STATUS_FAIL)
  3672. return -EIO;
  3673. else
  3674. return 0;
  3675. }
  3676. /******************************************************************************
  3677. *
  3678. * Write a page to a logical address
  3679. *
  3680. *****************************************************************************/
  3681. static int mtk_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  3682. uint32_t offset, int data_len, const uint8_t *buf,
  3683. int oob_required, int page, int cached, int raw)
  3684. {
  3685. /* int block_size = 1 << (chip->phys_erase_shift); */
  3686. int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  3687. u32 block;
  3688. u32 page_in_block;
  3689. u32 mapped_block;
  3690. #if CFG_PERFLOG_DEBUG
  3691. struct timeval stimer, etimer;
  3692. do_gettimeofday(&stimer);
  3693. #endif
  3694. page_in_block = mtk_nand_page_transform(mtd, chip, page, &block, &mapped_block);
  3695. /* MSG(INIT,"[WRITE] %d, %d, %d %d\n",mapped_block, block, page_in_block, page_per_block); */
  3696. /* write bad index into oob */
  3697. if (mapped_block != block)
  3698. set_bad_index_to_oob(chip->oob_poi, block);
  3699. else
  3700. set_bad_index_to_oob(chip->oob_poi, FAKE_INDEX);
  3701. /* pr_debug("[xiaolei] mtk_nand_write_page 0x%x\n", (u32)buf); */
  3702. if (mtk_nand_exec_write_page(mtd, page_in_block + mapped_block * page_per_block, mtd->writesize, (u8 *) buf,
  3703. chip->oob_poi)) {
  3704. pr_err("write fail at block: 0x%x, page: 0x%x\n", mapped_block, page_in_block);
  3705. if (update_bmt((u64) ((u64) page_in_block + (u64) mapped_block * page_per_block) <<
  3706. chip->page_shift, UPDATE_WRITE_FAIL, (u8 *) buf, chip->oob_poi)) {
  3707. pr_debug("Update BMT success\n");
  3708. return 0;
  3709. }
  3710. pr_err("Update BMT fail\n");
  3711. return -EIO;
  3712. }
  3713. #if CFG_PERFLOG_DEBUG
  3714. do_gettimeofday(&etimer);
  3715. g_NandPerfLog.WritePageTotalTime += Cal_timediff(&etimer, &stimer);
  3716. g_NandPerfLog.WritePageCount++;
  3717. dump_nand_rwcount();
  3718. #endif
  3719. return 0;
  3720. }
  3721. /* ------------------------------------------------------------------------------- */
  3722. /*
  3723. static void mtk_nand_command_sp(
  3724. struct mtd_info *mtd, unsigned int command, int column, int page_addr)
  3725. {
  3726. g_u4ColAddr = column;
  3727. g_u4RowAddr = page_addr;
  3728. switch(command)
  3729. {
  3730. case NAND_CMD_STATUS:
  3731. break;
  3732. case NAND_CMD_READID:
  3733. break;
  3734. case NAND_CMD_RESET:
  3735. break;
  3736. case NAND_CMD_RNDOUT:
  3737. case NAND_CMD_RNDOUTSTART:
  3738. case NAND_CMD_RNDIN:
  3739. case NAND_CMD_CACHEDPROG:
  3740. case NAND_CMD_STATUS_MULTI:
  3741. default:
  3742. break;
  3743. }
  3744. }
  3745. */
  3746. /******************************************************************************
  3747. * mtk_nand_command_bp
  3748. *
  3749. * DESCRIPTION:
  3750. * Handle the commands from MTD !
  3751. *
  3752. * PARAMETERS:
  3753. * struct mtd_info *mtd, unsigned int command, int column, int page_addr
  3754. *
  3755. * RETURNS:
  3756. * None
  3757. *
  3758. * NOTES:
  3759. * None
  3760. *
  3761. ******************************************************************************/
  3762. static void mtk_nand_command_bp(struct mtd_info *mtd, unsigned int command, int column,
  3763. int page_addr)
  3764. {
  3765. struct nand_chip *nand = mtd->priv;
  3766. #ifdef NAND_PFM
  3767. struct timeval pfm_time_erase;
  3768. #endif
  3769. #if 0
  3770. /* int block_size = 1 << (nand->phys_erase_shift); */
  3771. /* int page_per_block = 1 << (nand->phys_erase_shift - nand->page_shift); */
  3772. /* u32 block; */
  3773. /* u16 page_in_block; */
  3774. /* u32 mapped_block; */
  3775. /* bool rand= FALSE; */
  3776. page_addr = mtk_nand_page_transform(mtd, nand, &block, &mapped_block);
  3777. page_addr = mapped_block * page_per_block + page_addr;
  3778. #endif
  3779. switch (command) {
  3780. case NAND_CMD_SEQIN:
  3781. memset(g_kCMD.au1OOB, 0xFF, sizeof(g_kCMD.au1OOB));
  3782. g_kCMD.pDataBuf = NULL;
  3783. /* } */
  3784. g_kCMD.u4RowAddr = page_addr;
  3785. g_kCMD.u4ColAddr = column;
  3786. break;
  3787. case NAND_CMD_PAGEPROG:
  3788. if (g_kCMD.pDataBuf || (0xFF != g_kCMD.au1OOB[0])) {
  3789. u8 *pDataBuf = g_kCMD.pDataBuf ? g_kCMD.pDataBuf : nand->buffers->databuf;
  3790. /* pr_debug("[xiaolei] mtk_nand_command_bp 0x%x\n", (u32)pDataBuf); */
  3791. mtk_nand_exec_write_page(mtd, g_kCMD.u4RowAddr, mtd->writesize, pDataBuf,
  3792. g_kCMD.au1OOB);
  3793. g_kCMD.u4RowAddr = (u32) -1;
  3794. g_kCMD.u4OOBRowAddr = (u32) -1;
  3795. }
  3796. break;
  3797. case NAND_CMD_READOOB:
  3798. g_kCMD.u4RowAddr = page_addr;
  3799. g_kCMD.u4ColAddr = column + mtd->writesize;
  3800. #ifdef NAND_PFM
  3801. g_kCMD.pureReadOOB = 1;
  3802. g_kCMD.pureReadOOBNum += 1;
  3803. #endif
  3804. break;
  3805. case NAND_CMD_READ0:
  3806. g_kCMD.u4RowAddr = page_addr;
  3807. g_kCMD.u4ColAddr = column;
  3808. #ifdef NAND_PFM
  3809. g_kCMD.pureReadOOB = 0;
  3810. #endif
  3811. break;
  3812. case NAND_CMD_ERASE1:
  3813. PFM_BEGIN(pfm_time_erase);
  3814. (void)mtk_nand_reset();
  3815. mtk_nand_set_mode(CNFG_OP_ERASE);
  3816. (void)mtk_nand_set_command(NAND_CMD_ERASE1);
  3817. (void)mtk_nand_set_address(0, page_addr, 0, devinfo.addr_cycle - 2);
  3818. break;
  3819. case NAND_CMD_ERASE2:
  3820. (void)mtk_nand_set_command(NAND_CMD_ERASE2);
  3821. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY)
  3822. ;
  3823. PFM_END_E(pfm_time_erase);
  3824. break;
  3825. case NAND_CMD_STATUS:
  3826. (void)mtk_nand_reset();
  3827. if (mtk_nand_israndomizeron()) {
  3828. /* g_brandstatus = TRUE; */
  3829. mtk_nand_turn_off_randomizer();
  3830. }
  3831. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_BYTE_RW);
  3832. mtk_nand_set_mode(CNFG_OP_SRD);
  3833. mtk_nand_set_mode(CNFG_READ_EN);
  3834. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  3835. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  3836. (void)mtk_nand_set_command(NAND_CMD_STATUS);
  3837. NFI_CLN_REG32(NFI_CON_REG16, CON_NFI_NOB_MASK);
  3838. mb();
  3839. DRV_WriteReg32(NFI_CON_REG16, CON_NFI_SRD | (1 << CON_NFI_NOB_SHIFT));
  3840. g_bcmdstatus = true;
  3841. break;
  3842. case NAND_CMD_RESET:
  3843. (void)mtk_nand_reset();
  3844. break;
  3845. case NAND_CMD_READID:
  3846. /* Issue NAND chip reset command */
  3847. /* NFI_ISSUE_COMMAND (NAND_CMD_RESET, 0, 0, 0, 0); */
  3848. /* timeout = TIMEOUT_4; */
  3849. /* while (timeout) */
  3850. /* timeout--; */
  3851. mtk_nand_reset();
  3852. /* Disable HW ECC */
  3853. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  3854. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  3855. /* Disable 16-bit I/O */
  3856. /* NFI_CLN_REG16(NFI_PAGEFMT_REG16, PAGEFMT_DBYTE_EN); */
  3857. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN | CNFG_BYTE_RW);
  3858. (void)mtk_nand_reset();
  3859. mb();
  3860. mtk_nand_set_mode(CNFG_OP_SRD);
  3861. (void)mtk_nand_set_command(NAND_CMD_READID);
  3862. (void)mtk_nand_set_address(0, 0, 1, 0);
  3863. DRV_WriteReg32(NFI_CON_REG16, CON_NFI_SRD);
  3864. while (DRV_Reg32(NFI_STA_REG32) & STA_DATAR_STATE)
  3865. ;
  3866. break;
  3867. default:
  3868. BUG();
  3869. break;
  3870. }
  3871. }
  3872. /******************************************************************************
  3873. * mtk_nand_select_chip
  3874. *
  3875. * DESCRIPTION:
  3876. * Select a chip !
  3877. *
  3878. * PARAMETERS:
  3879. * struct mtd_info *mtd, int chip
  3880. *
  3881. * RETURNS:
  3882. * None
  3883. *
  3884. * NOTES:
  3885. * None
  3886. *
  3887. ******************************************************************************/
  3888. static void mtk_nand_select_chip(struct mtd_info *mtd, int chip)
  3889. {
  3890. if (chip == -1 && false == g_bInitDone) {
  3891. struct nand_chip *nand = mtd->priv;
  3892. struct mtk_nand_host *host = nand->priv;
  3893. struct mtk_nand_host_hw *hw = host->hw;
  3894. u32 spare_per_sector = mtd->oobsize / (mtd->writesize / hw->nand_sec_size);
  3895. u32 ecc_bit = 4;
  3896. u32 spare_bit = PAGEFMT_SPARE_16;
  3897. switch (spare_per_sector) {
  3898. #ifndef MTK_COMBO_NAND_SUPPORT
  3899. case 16:
  3900. spare_bit = PAGEFMT_SPARE_16;
  3901. ecc_bit = 4;
  3902. spare_per_sector = 16;
  3903. break;
  3904. case 26:
  3905. case 27:
  3906. case 28:
  3907. spare_bit = PAGEFMT_SPARE_26;
  3908. ecc_bit = 10;
  3909. spare_per_sector = 26;
  3910. break;
  3911. case 32:
  3912. ecc_bit = 12;
  3913. if (MLC_DEVICE == TRUE)
  3914. spare_bit = PAGEFMT_SPARE_32_1KS;
  3915. else
  3916. spare_bit = PAGEFMT_SPARE_32;
  3917. spare_per_sector = 32;
  3918. break;
  3919. case 40:
  3920. ecc_bit = 18;
  3921. spare_bit = PAGEFMT_SPARE_40;
  3922. spare_per_sector = 40;
  3923. break;
  3924. case 44:
  3925. ecc_bit = 20;
  3926. spare_bit = PAGEFMT_SPARE_44;
  3927. spare_per_sector = 44;
  3928. break;
  3929. case 48:
  3930. case 49:
  3931. ecc_bit = 22;
  3932. spare_bit = PAGEFMT_SPARE_48;
  3933. spare_per_sector = 48;
  3934. break;
  3935. case 50:
  3936. case 51:
  3937. ecc_bit = 24;
  3938. spare_bit = PAGEFMT_SPARE_50;
  3939. spare_per_sector = 50;
  3940. break;
  3941. case 52:
  3942. case 54:
  3943. case 56:
  3944. ecc_bit = 24;
  3945. if (MLC_DEVICE == TRUE)
  3946. spare_bit = PAGEFMT_SPARE_52_1KS;
  3947. else
  3948. spare_bit = PAGEFMT_SPARE_52;
  3949. spare_per_sector = 32;
  3950. break;
  3951. #endif
  3952. case 62:
  3953. case 63:
  3954. ecc_bit = 28;
  3955. spare_bit = PAGEFMT_SPARE_62;
  3956. spare_per_sector = 62;
  3957. break;
  3958. case 64:
  3959. ecc_bit = 32;
  3960. if (MLC_DEVICE == TRUE)
  3961. spare_bit = PAGEFMT_SPARE_64_1KS;
  3962. else
  3963. spare_bit = PAGEFMT_SPARE_64;
  3964. spare_per_sector = 64;
  3965. break;
  3966. case 72:
  3967. ecc_bit = 36;
  3968. if (MLC_DEVICE == TRUE)
  3969. spare_bit = PAGEFMT_SPARE_72_1KS;
  3970. spare_per_sector = 72;
  3971. break;
  3972. case 80:
  3973. ecc_bit = 40;
  3974. if (MLC_DEVICE == TRUE)
  3975. spare_bit = PAGEFMT_SPARE_80_1KS;
  3976. spare_per_sector = 80;
  3977. break;
  3978. case 88:
  3979. ecc_bit = 44;
  3980. if (MLC_DEVICE == TRUE)
  3981. spare_bit = PAGEFMT_SPARE_88_1KS;
  3982. spare_per_sector = 88;
  3983. break;
  3984. case 96:
  3985. case 98:
  3986. ecc_bit = 48;
  3987. if (MLC_DEVICE == TRUE)
  3988. spare_bit = PAGEFMT_SPARE_96_1KS;
  3989. spare_per_sector = 96;
  3990. break;
  3991. case 100:
  3992. case 102:
  3993. case 104:
  3994. ecc_bit = 52;
  3995. if (MLC_DEVICE == TRUE)
  3996. spare_bit = PAGEFMT_SPARE_100_1KS;
  3997. spare_per_sector = 100;
  3998. break;
  3999. case 124:
  4000. case 126:
  4001. case 128:
  4002. ecc_bit = 60;
  4003. if (MLC_DEVICE == TRUE)
  4004. spare_bit = PAGEFMT_SPARE_124_1KS;
  4005. spare_per_sector = 124;
  4006. break;
  4007. default:
  4008. pr_notice("[NAND]: NFI not support oobsize: %x\n", spare_per_sector);
  4009. mtk_nand_assert(0);
  4010. }
  4011. mtd->oobsize = spare_per_sector * (mtd->writesize / hw->nand_sec_size);
  4012. pr_debug("[NAND]select ecc bit:%d, sparesize :%d\n", ecc_bit, mtd->oobsize);
  4013. /* Setup PageFormat */
  4014. if (16384 == mtd->writesize) {
  4015. NFI_SET_REG32(NFI_PAGEFMT_REG32,
  4016. (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_16K_1KS);
  4017. nand->cmdfunc = mtk_nand_command_bp;
  4018. } else if (8192 == mtd->writesize) {
  4019. NFI_SET_REG32(NFI_PAGEFMT_REG32,
  4020. (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_8K_1KS);
  4021. nand->cmdfunc = mtk_nand_command_bp;
  4022. } else if (4096 == mtd->writesize) {
  4023. if (MLC_DEVICE == FALSE)
  4024. NFI_SET_REG32(NFI_PAGEFMT_REG32,
  4025. (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_4K);
  4026. else
  4027. NFI_SET_REG32(NFI_PAGEFMT_REG32,
  4028. (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_4K_1KS);
  4029. nand->cmdfunc = mtk_nand_command_bp;
  4030. } else if (2048 == mtd->writesize) {
  4031. if (MLC_DEVICE == FALSE)
  4032. NFI_SET_REG32(NFI_PAGEFMT_REG32,
  4033. (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_2K);
  4034. else
  4035. NFI_SET_REG32(NFI_PAGEFMT_REG32,
  4036. (spare_bit << PAGEFMT_SPARE_SHIFT) | PAGEFMT_2K_1KS);
  4037. nand->cmdfunc = mtk_nand_command_bp;
  4038. }
  4039. ecc_threshold = ecc_bit * 4 / 5;
  4040. ECC_Config(hw, ecc_bit);
  4041. g_bInitDone = true;
  4042. /* xiaolei for kernel3.10 */
  4043. nand->ecc.strength = ecc_bit;
  4044. mtd->bitflip_threshold = nand->ecc.strength;
  4045. }
  4046. switch (chip) {
  4047. case -1:
  4048. break;
  4049. case 0:
  4050. #ifdef CFG_FPGA_PLATFORM /* FPGA NAND is placed at CS1 not CS0 */
  4051. DRV_WriteReg16(NFI_CSEL_REG16, 0);
  4052. break;
  4053. #endif
  4054. case 1:
  4055. DRV_WriteReg16(NFI_CSEL_REG16, chip);
  4056. break;
  4057. }
  4058. }
  4059. /******************************************************************************
  4060. * mtk_nand_read_byte
  4061. *
  4062. * DESCRIPTION:
  4063. * Read a byte of data !
  4064. *
  4065. * PARAMETERS:
  4066. * struct mtd_info *mtd
  4067. *
  4068. * RETURNS:
  4069. * None
  4070. *
  4071. * NOTES:
  4072. * None
  4073. *
  4074. ******************************************************************************/
  4075. static uint8_t mtk_nand_read_byte(struct mtd_info *mtd)
  4076. {
  4077. #if 0
  4078. /* while(0 == FIFO_RD_REMAIN(DRV_Reg16(NFI_FIFOSTA_REG16))); */
  4079. /* Check the PIO bit is ready or not */
  4080. u32 timeout = TIMEOUT_4;
  4081. uint8_t retval = 0;
  4082. WAIT_NFI_PIO_READY(timeout);
  4083. retval = DRV_Reg8(NFI_DATAR_REG32);
  4084. MSG(INIT, "mtk_nand_read_byte (0x%x)\n", retval);
  4085. if (g_bcmdstatus) {
  4086. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AHB);
  4087. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  4088. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  4089. g_bcmdstatus = false;
  4090. }
  4091. return retval;
  4092. #endif
  4093. uint8_t retval = 0;
  4094. if (!mtk_nand_pio_ready()) {
  4095. pr_err("pio ready timeout\n");
  4096. retval = false;
  4097. }
  4098. if (g_bcmdstatus) {
  4099. retval = DRV_Reg8(NFI_DATAR_REG32);
  4100. NFI_CLN_REG32(NFI_CON_REG16, CON_NFI_NOB_MASK);
  4101. mtk_nand_reset();
  4102. #if (__INTERNAL_USE_AHB_MODE__)
  4103. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AHB);
  4104. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  4105. #endif
  4106. if (g_bHwEcc)
  4107. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  4108. else
  4109. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  4110. g_bcmdstatus = false;
  4111. } else
  4112. retval = DRV_Reg8(NFI_DATAR_REG32);
  4113. /*if(g_brandstatus)
  4114. {
  4115. g_brandstatus = FALSE;
  4116. mtk_nand_turn_on_randomizer(g_kCMD.u4RowAddr, g_kCMD.u4ColAddr / devinfo.sectorsize, FALSE);
  4117. } */
  4118. return retval;
  4119. }
  4120. /******************************************************************************
  4121. * mtk_nand_read_buf
  4122. *
  4123. * DESCRIPTION:
  4124. * Read NAND data !
  4125. *
  4126. * PARAMETERS:
  4127. * struct mtd_info *mtd, uint8_t *buf, int len
  4128. *
  4129. * RETURNS:
  4130. * None
  4131. *
  4132. * NOTES:
  4133. * None
  4134. *
  4135. ******************************************************************************/
  4136. static void mtk_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  4137. {
  4138. struct nand_chip *nand = (struct nand_chip *)mtd->priv;
  4139. struct NAND_CMD *pkCMD = &g_kCMD;
  4140. u32 u4ColAddr = pkCMD->u4ColAddr;
  4141. u32 u4PageSize = mtd->writesize;
  4142. if (u4ColAddr < u4PageSize) {
  4143. if ((u4ColAddr == 0) && (len >= u4PageSize)) {
  4144. mtk_nand_exec_read_page(mtd, pkCMD->u4RowAddr, u4PageSize, buf,
  4145. pkCMD->au1OOB);
  4146. if (len > u4PageSize) {
  4147. u32 u4Size = min(len - u4PageSize, (u32) (sizeof(pkCMD->au1OOB)));
  4148. memcpy(buf + u4PageSize, pkCMD->au1OOB, u4Size);
  4149. }
  4150. } else {
  4151. mtk_nand_exec_read_page(mtd, pkCMD->u4RowAddr, u4PageSize,
  4152. nand->buffers->databuf, pkCMD->au1OOB);
  4153. memcpy(buf, nand->buffers->databuf + u4ColAddr, len);
  4154. }
  4155. pkCMD->u4OOBRowAddr = pkCMD->u4RowAddr;
  4156. } else {
  4157. u32 u4Offset = u4ColAddr - u4PageSize;
  4158. u32 u4Size = min(len - u4Offset, (u32) (sizeof(pkCMD->au1OOB)));
  4159. if (pkCMD->u4OOBRowAddr != pkCMD->u4RowAddr) {
  4160. mtk_nand_exec_read_page(mtd, pkCMD->u4RowAddr, u4PageSize,
  4161. nand->buffers->databuf, pkCMD->au1OOB);
  4162. pkCMD->u4OOBRowAddr = pkCMD->u4RowAddr;
  4163. }
  4164. memcpy(buf, pkCMD->au1OOB + u4Offset, u4Size);
  4165. }
  4166. pkCMD->u4ColAddr += len;
  4167. }
  4168. /******************************************************************************
  4169. * mtk_nand_write_buf
  4170. *
  4171. * DESCRIPTION:
  4172. * Write NAND data !
  4173. *
  4174. * PARAMETERS:
  4175. * struct mtd_info *mtd, const uint8_t *buf, int len
  4176. *
  4177. * RETURNS:
  4178. * None
  4179. *
  4180. * NOTES:
  4181. * None
  4182. *
  4183. ******************************************************************************/
  4184. static void mtk_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  4185. {
  4186. struct NAND_CMD *pkCMD = &g_kCMD;
  4187. u32 u4ColAddr = pkCMD->u4ColAddr;
  4188. u32 u4PageSize = mtd->writesize;
  4189. int i4Size, i;
  4190. if (u4ColAddr >= u4PageSize) {
  4191. u32 u4Offset = u4ColAddr - u4PageSize;
  4192. u8 *pOOB = pkCMD->au1OOB + u4Offset;
  4193. i4Size = min(len, (int)(sizeof(pkCMD->au1OOB) - u4Offset));
  4194. for (i = 0; i < i4Size; i++)
  4195. pOOB[i] &= buf[i];
  4196. } else {
  4197. pkCMD->pDataBuf = (u8 *) buf;
  4198. }
  4199. pkCMD->u4ColAddr += len;
  4200. }
  4201. /******************************************************************************
  4202. * mtk_nand_write_page_hwecc
  4203. *
  4204. * DESCRIPTION:
  4205. * Write NAND data with hardware ecc !
  4206. *
  4207. * PARAMETERS:
  4208. * struct mtd_info *mtd, struct nand_chip *chip, const uint8_t *buf
  4209. *
  4210. * RETURNS:
  4211. * None
  4212. *
  4213. * NOTES:
  4214. * None
  4215. *
  4216. ******************************************************************************/
  4217. static int mtk_nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  4218. const uint8_t *buf, int oob_required)
  4219. {
  4220. mtk_nand_write_buf(mtd, buf, mtd->writesize);
  4221. mtk_nand_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  4222. return 0;
  4223. }
  4224. /******************************************************************************
  4225. * mtk_nand_read_page_hwecc
  4226. *
  4227. * DESCRIPTION:
  4228. * Read NAND data with hardware ecc !
  4229. *
  4230. * PARAMETERS:
  4231. * struct mtd_info *mtd, struct nand_chip *chip, uint8_t *buf
  4232. *
  4233. * RETURNS:
  4234. * None
  4235. *
  4236. * NOTES:
  4237. * None
  4238. *
  4239. ******************************************************************************/
  4240. static int mtk_nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, uint8_t *buf,
  4241. int oob_required, int page)
  4242. {
  4243. #if 0
  4244. mtk_nand_read_buf(mtd, buf, mtd->writesize);
  4245. mtk_nand_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  4246. #else
  4247. struct NAND_CMD *pkCMD = &g_kCMD;
  4248. u32 u4ColAddr = pkCMD->u4ColAddr;
  4249. u32 u4PageSize = mtd->writesize;
  4250. if (u4ColAddr == 0) {
  4251. mtk_nand_exec_read_page(mtd, pkCMD->u4RowAddr, u4PageSize, buf, chip->oob_poi);
  4252. pkCMD->u4ColAddr += u4PageSize + mtd->oobsize;
  4253. }
  4254. #endif
  4255. return 0;
  4256. }
  4257. /******************************************************************************
  4258. *
  4259. * Read a page to a logical address
  4260. *
  4261. *****************************************************************************/
  4262. static int mtk_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip, u8 *buf, int page)
  4263. {
  4264. /* int block_size = 1 << (chip->phys_erase_shift); */
  4265. int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  4266. /* int page_per_block1 = page_per_block; */
  4267. u32 block;
  4268. u32 page_in_block;
  4269. u32 mapped_block;
  4270. int bRet = ERR_RTN_SUCCESS;
  4271. #if CFG_PERFLOG_DEBUG
  4272. struct timeval stimer, etimer;
  4273. do_gettimeofday(&stimer);
  4274. #endif
  4275. page_in_block = mtk_nand_page_transform(mtd, chip, page, &block, &mapped_block);
  4276. /* MSG(INIT,"[READ] %d, %d, %d %d\n",mapped_block, block, page_in_block, page_per_block); */
  4277. /* pr_debug("[xl] mtk_nand_read_page buf 0x%lx\n", (unsigned long)buf); */
  4278. bRet =
  4279. mtk_nand_exec_read_page(mtd, page_in_block + mapped_block * page_per_block,
  4280. mtd->writesize, buf, chip->oob_poi);
  4281. if (bRet == ERR_RTN_SUCCESS) {
  4282. #if CFG_PERFLOG_DEBUG
  4283. do_gettimeofday(&etimer);
  4284. g_NandPerfLog.ReadPageTotalTime += Cal_timediff(&etimer, &stimer);
  4285. g_NandPerfLog.ReadPageCount++;
  4286. dump_nand_rwcount();
  4287. #endif
  4288. return 0;
  4289. }
  4290. /* else
  4291. return -EIO; */
  4292. return 0;
  4293. }
  4294. static int mtk_nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, u8 *buf, int page,
  4295. int subpage, int subpageno)
  4296. {
  4297. /* int block_size = 1 << (chip->phys_erase_shift); */
  4298. int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  4299. /* int page_per_block1 = page_per_block; */
  4300. u32 block;
  4301. int coladdr;
  4302. u32 page_in_block;
  4303. u32 mapped_block;
  4304. /* bool readRetry = FALSE; */
  4305. /* int retryCount = 0; */
  4306. int bRet = ERR_RTN_SUCCESS;
  4307. int sec_num = 1 << (chip->page_shift - host->hw->nand_sec_shift);
  4308. int spare_per_sector = mtd->oobsize / sec_num;
  4309. #if CFG_PERFLOG_DEBUG
  4310. struct timeval stimer, etimer;
  4311. do_gettimeofday(&stimer);
  4312. #endif
  4313. page_in_block = mtk_nand_page_transform(mtd, chip, page, &block, &mapped_block);
  4314. coladdr = subpage * (devinfo.sectorsize + spare_per_sector);
  4315. /* coladdr = subpage*(devinfo.sectorsize); */
  4316. /* MSG(INIT,"[Read Subpage] %d, %d, %d %d\n",mapped_block, block, page_in_block, page_per_block); */
  4317. bRet = mtk_nand_exec_read_sector(mtd, page_in_block + mapped_block * page_per_block, coladdr,
  4318. devinfo.sectorsize * subpageno, buf, chip->oob_poi,
  4319. subpageno);
  4320. /* memset(bean_buffer, 0xFF, LPAGE); */
  4321. /* bRet = mtk_nand_exec_read_page(mtd, page, mtd->writesize, bean_buffer, chip->oob_poi); */
  4322. if (bRet == ERR_RTN_SUCCESS) {
  4323. #if CFG_PERFLOG_DEBUG
  4324. do_gettimeofday(&etimer);
  4325. g_NandPerfLog.ReadSubPageTotalTime += Cal_timediff(&etimer, &stimer);
  4326. g_NandPerfLog.ReadSubPageCount++;
  4327. dump_nand_rwcount();
  4328. #endif
  4329. return 0;
  4330. }
  4331. /* memcpy(buf, bean_buffer+coladdr, mtd->writesize); */
  4332. /* else
  4333. return -EIO; */
  4334. return 0;
  4335. }
  4336. /******************************************************************************
  4337. *
  4338. * Erase a block at a logical address
  4339. *
  4340. *****************************************************************************/
  4341. int mtk_nand_erase_hw(struct mtd_info *mtd, int page)
  4342. {
  4343. #ifdef PWR_LOSS_SPOH
  4344. struct timeval pl_time_write;
  4345. suseconds_t duration;
  4346. u32 time;
  4347. #endif
  4348. int result;
  4349. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  4350. #ifdef _MTK_NAND_DUMMY_DRIVER_
  4351. if (dummy_driver_debug) {
  4352. unsigned long long time = sched_clock();
  4353. if (!((time * 123 + 59) % 1024)) {
  4354. pr_err("[NAND_DUMMY_DRIVER] Simulate erase error at page: 0x%x\n",
  4355. page);
  4356. return NAND_STATUS_FAIL;
  4357. }
  4358. }
  4359. #endif
  4360. #if CFG_2CS_NAND
  4361. if (g_bTricky_CS)
  4362. page = mtk_nand_cs_on(chip, NFI_TRICKY_CS, page);
  4363. #endif
  4364. PL_NAND_BEGIN(pl_time_write);
  4365. PL_TIME_RAND_ERASE(chip, page, time);
  4366. result = chip->erase(mtd, page);
  4367. PL_NAND_RESET(time);
  4368. PL_NAND_END(pl_time_write, duration);
  4369. PL_TIME_ERASE(duration);
  4370. return result;
  4371. }
  4372. static int mtk_nand_erase(struct mtd_info *mtd, int page)
  4373. {
  4374. int status;
  4375. struct nand_chip *chip = mtd->priv;
  4376. /* int block_size = 1 << (chip->phys_erase_shift); */
  4377. int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  4378. u32 block;
  4379. u32 page_in_block;
  4380. u32 mapped_block;
  4381. #if CFG_PERFLOG_DEBUG
  4382. struct timeval stimer, etimer;
  4383. do_gettimeofday(&stimer);
  4384. #endif
  4385. page_in_block = mtk_nand_page_transform(mtd, chip, page, &block, &mapped_block);
  4386. /* MSG(INIT, "[ERASE] 0x%x 0x%x\n", mapped_block, page); */
  4387. status = mtk_nand_erase_hw(mtd, page_in_block + page_per_block * mapped_block);
  4388. if (status & NAND_STATUS_FAIL) {
  4389. if (update_bmt((u64) ((u64) page_in_block + (u64) mapped_block * page_per_block) <<
  4390. chip->page_shift, UPDATE_ERASE_FAIL, NULL, NULL)) {
  4391. pr_notice("Erase fail at block: 0x%x, update BMT success\n", mapped_block);
  4392. return 0;
  4393. }
  4394. pr_notice("Erase fail at block: 0x%x, update BMT fail\n", mapped_block);
  4395. return NAND_STATUS_FAIL;
  4396. }
  4397. #if CFG_PERFLOG_DEBUG
  4398. do_gettimeofday(&etimer);
  4399. g_NandPerfLog.EraseBlockTotalTime += Cal_timediff(&etimer, &stimer);
  4400. g_NandPerfLog.EraseBlockCount++;
  4401. dump_nand_rwcount();
  4402. #endif
  4403. return 0;
  4404. }
  4405. /******************************************************************************
  4406. * mtk_nand_read_multi_page_cache
  4407. *
  4408. * description:
  4409. * read multi page data using cache read
  4410. *
  4411. * parameters:
  4412. * struct mtd_info *mtd, struct nand_chip *chip, int page, struct mtd_oob_ops *ops
  4413. *
  4414. * returns:
  4415. * none
  4416. *
  4417. * notes:
  4418. * only available for nand flash support cache read.
  4419. * read main data only.
  4420. *
  4421. *****************************************************************************/
  4422. #if 0
  4423. static int mtk_nand_read_multi_page_cache(struct mtd_info *mtd, struct nand_chip *chip, int page,
  4424. struct mtd_oob_ops *ops)
  4425. {
  4426. int res = -EIO;
  4427. int len = ops->len;
  4428. struct mtd_ecc_stats stat = mtd->ecc_stats;
  4429. uint8_t *buf = ops->datbuf;
  4430. if (!mtk_nand_ready_for_read(chip, page, 0, true, buf))
  4431. return -EIO;
  4432. while (len > 0) {
  4433. mtk_nand_set_mode(CNFG_OP_CUST);
  4434. DRV_WriteReg32(NFI_CON_REG16, 8 << CON_NFI_SEC_SHIFT);
  4435. if (len > mtd->writesize) { /* remained more than one page */
  4436. if (!mtk_nand_set_command(0x31)) /* todo: add cache read command */
  4437. goto ret;
  4438. } else {
  4439. if (!mtk_nand_set_command(0x3f)) /* last page remained */
  4440. goto ret;
  4441. }
  4442. mtk_nand_status_ready(STA_NAND_BUSY);
  4443. #ifdef __INTERNAL_USE_AHB_MODE__
  4444. /* if (!mtk_nand_dma_read_data(buf, mtd->writesize)) */
  4445. if (!mtk_nand_read_page_data(mtd, buf, mtd->writesize))
  4446. goto ret;
  4447. #else
  4448. if (!mtk_nand_mcu_read_data(mtd, buf, mtd->writesize))
  4449. goto ret;
  4450. #endif
  4451. /* get ecc error info */
  4452. mtk_nand_check_bch_error(mtd, buf, 3, page);
  4453. ECC_Decode_End();
  4454. page++;
  4455. len -= mtd->writesize;
  4456. buf += mtd->writesize;
  4457. ops->retlen += mtd->writesize;
  4458. if (len > 0) {
  4459. ECC_Decode_Start();
  4460. mtk_nand_reset();
  4461. }
  4462. }
  4463. res = 0;
  4464. ret:
  4465. mtk_nand_stop_read();
  4466. if (res)
  4467. return res;
  4468. if (mtd->ecc_stats.failed > stat.failed) {
  4469. pr_debug("ecc fail happened\n");
  4470. return -EBADMSG;
  4471. }
  4472. return mtd->ecc_stats.corrected - stat.corrected ? -EUCLEAN : 0;
  4473. }
  4474. #endif
  4475. /******************************************************************************
  4476. * mtk_nand_read_oob_raw
  4477. *
  4478. * DESCRIPTION:
  4479. * Read oob data
  4480. *
  4481. * PARAMETERS:
  4482. * struct mtd_info *mtd, const uint8_t *buf, int addr, int len
  4483. *
  4484. * RETURNS:
  4485. * None
  4486. *
  4487. * NOTES:
  4488. * this function read raw oob data out of flash, so need to re-organise
  4489. * data format before using.
  4490. * len should be times of 8, call this after nand_get_device.
  4491. * Should notice, this function read data without ECC protection.
  4492. *
  4493. *****************************************************************************/
  4494. static int mtk_nand_read_oob_raw(struct mtd_info *mtd, uint8_t *buf, int page_addr, int len)
  4495. {
  4496. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  4497. u32 col_addr = 0;
  4498. u32 sector = 0;
  4499. int res = 0;
  4500. u32 colnob = 2, rawnob = devinfo.addr_cycle - 2;
  4501. int randomread = 0;
  4502. int read_len = 0;
  4503. int sec_num = 1 << (chip->page_shift - host->hw->nand_sec_shift);
  4504. int spare_per_sector = mtd->oobsize / sec_num;
  4505. u32 sector_size = NAND_SECTOR_SIZE;
  4506. if (devinfo.sectorsize == 1024)
  4507. sector_size = 1024;
  4508. if (len > NAND_MAX_OOBSIZE || len % OOB_AVAI_PER_SECTOR || !buf) {
  4509. pr_warn("[%s] invalid parameter, len: %d, buf: %p\n", __func__, len,
  4510. buf);
  4511. return -EINVAL;
  4512. }
  4513. if (len > spare_per_sector)
  4514. randomread = 1;
  4515. if (!randomread || !(devinfo.advancedmode & RAMDOM_READ)) {
  4516. while (len > 0) {
  4517. read_len = min(len, spare_per_sector);
  4518. col_addr = sector_size +
  4519. sector * (sector_size + spare_per_sector); /* TODO: Fix this hard-code 16 */
  4520. if (!mtk_nand_ready_for_read(chip,
  4521. page_addr, col_addr, sec_num, false, NULL)) {
  4522. pr_warn("mtk_nand_ready_for_read return failed\n");
  4523. res = -EIO;
  4524. goto error;
  4525. }
  4526. if (!mtk_nand_mcu_read_data(mtd,
  4527. buf + spare_per_sector * sector, read_len)) { /* TODO: and this 8 */
  4528. pr_warn("mtk_nand_mcu_read_data return failed\n");
  4529. res = -EIO;
  4530. goto error;
  4531. }
  4532. mtk_nand_stop_read();
  4533. /* dump_data(buf + 16 * sector,16); */
  4534. sector++;
  4535. len -= read_len;
  4536. }
  4537. } else { /* should be 64 */
  4538. col_addr = sector_size;
  4539. if (chip->options & NAND_BUSWIDTH_16)
  4540. col_addr /= 2;
  4541. if (!mtk_nand_reset())
  4542. goto error;
  4543. mtk_nand_set_mode(0x6000);
  4544. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  4545. DRV_WriteReg32(NFI_CON_REG16, 4 << CON_NFI_SEC_SHIFT);
  4546. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_AHB);
  4547. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  4548. mtk_nand_set_autoformat(false);
  4549. if (!mtk_nand_set_command(NAND_CMD_READ0))
  4550. goto error;
  4551. /* 1 FIXED ME: For Any Kind of AddrCycle */
  4552. if (!mtk_nand_set_address(col_addr, page_addr, colnob, rawnob))
  4553. goto error;
  4554. if (!mtk_nand_set_command(NAND_CMD_READSTART))
  4555. goto error;
  4556. if (!mtk_nand_status_ready(STA_NAND_BUSY))
  4557. goto error;
  4558. read_len = min(len, spare_per_sector);
  4559. if (!mtk_nand_mcu_read_data(mtd, buf + spare_per_sector * sector, read_len)) { /* TODO: and this 8 */
  4560. pr_warn("mtk_nand_mcu_read_data return failed first 16\n");
  4561. res = -EIO;
  4562. goto error;
  4563. }
  4564. sector++;
  4565. len -= read_len;
  4566. mtk_nand_stop_read();
  4567. while (len > 0) {
  4568. read_len = min(len, spare_per_sector);
  4569. if (!mtk_nand_set_command(0x05))
  4570. goto error;
  4571. col_addr = sector_size + sector * (sector_size + 16); /* :TODO_JP careful 16 */
  4572. if (chip->options & NAND_BUSWIDTH_16)
  4573. col_addr /= 2;
  4574. DRV_WriteReg32(NFI_COLADDR_REG32, col_addr);
  4575. DRV_WriteReg16(NFI_ADDRNOB_REG16, 2);
  4576. DRV_WriteReg32(NFI_CON_REG16, 4 << CON_NFI_SEC_SHIFT);
  4577. if (!mtk_nand_status_ready(STA_ADDR_STATE))
  4578. goto error;
  4579. if (!mtk_nand_set_command(0xE0))
  4580. goto error;
  4581. if (!mtk_nand_status_ready(STA_NAND_BUSY))
  4582. goto error;
  4583. if (!mtk_nand_mcu_read_data(mtd,
  4584. buf + spare_per_sector * sector, read_len)) { /* TODO: and this 8 */
  4585. pr_warn("mtk_nand_mcu_read_data return failed first 16\n");
  4586. res = -EIO;
  4587. goto error;
  4588. }
  4589. mtk_nand_stop_read();
  4590. sector++;
  4591. len -= read_len;
  4592. }
  4593. /* dump_data(&testbuf[16],16); */
  4594. /* pr_debug(KERN_ERR "\n"); */
  4595. }
  4596. error:
  4597. NFI_CLN_REG32(NFI_CON_REG16, CON_NFI_BRD);
  4598. return res;
  4599. }
  4600. static int mtk_nand_write_oob_raw(struct mtd_info *mtd, const uint8_t *buf, int page_addr, int len)
  4601. {
  4602. struct nand_chip *chip = mtd->priv;
  4603. /* int i; */
  4604. u32 col_addr = 0;
  4605. u32 sector = 0;
  4606. /* int res = 0; */
  4607. /* u32 colnob=2, rawnob=devinfo.addr_cycle-2; */
  4608. /* int randomread =0; */
  4609. int write_len = 0;
  4610. int status;
  4611. int sec_num = 1 << (chip->page_shift - host->hw->nand_sec_shift);
  4612. int spare_per_sector = mtd->oobsize / sec_num;
  4613. u32 sector_size = NAND_SECTOR_SIZE;
  4614. if (devinfo.sectorsize == 1024)
  4615. sector_size = 1024;
  4616. if (len > NAND_MAX_OOBSIZE || len % OOB_AVAI_PER_SECTOR || !buf) {
  4617. pr_warn("[%s] invalid parameter, len: %d, buf: %p\n", __func__, len,
  4618. buf);
  4619. return -EINVAL;
  4620. }
  4621. while (len > 0) {
  4622. write_len = min(len, spare_per_sector);
  4623. col_addr = sector * (sector_size + spare_per_sector) + sector_size;
  4624. if (!mtk_nand_ready_for_write(chip, page_addr, col_addr, false, NULL))
  4625. return -EIO;
  4626. if (!mtk_nand_mcu_write_data(mtd, buf + sector * spare_per_sector, write_len))
  4627. return -EIO;
  4628. (void)mtk_nand_check_RW_count(write_len);
  4629. NFI_CLN_REG32(NFI_CON_REG16, CON_NFI_BWR);
  4630. (void)mtk_nand_set_command(NAND_CMD_PAGEPROG);
  4631. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY)
  4632. ;
  4633. status = chip->waitfunc(mtd, chip);
  4634. if (status & NAND_STATUS_FAIL) {
  4635. pr_debug("status: %d\n", status);
  4636. return -EIO;
  4637. }
  4638. len -= write_len;
  4639. sector++;
  4640. }
  4641. return 0;
  4642. }
  4643. static int mtk_nand_write_oob_hw(struct mtd_info *mtd, struct nand_chip *chip, int page)
  4644. {
  4645. /* u8 *buf = chip->oob_poi; */
  4646. int i, iter;
  4647. int sec_num = 1 << (chip->page_shift - host->hw->nand_sec_shift);
  4648. int spare_per_sector = mtd->oobsize / sec_num;
  4649. memcpy(local_oob_buf, chip->oob_poi, mtd->oobsize);
  4650. /* copy ecc data */
  4651. for (i = 0; i < chip->ecc.layout->eccbytes; i++) {
  4652. iter = (i / OOB_AVAI_PER_SECTOR) * spare_per_sector + OOB_AVAI_PER_SECTOR +
  4653. i % OOB_AVAI_PER_SECTOR;
  4654. local_oob_buf[iter] = chip->oob_poi[chip->ecc.layout->eccpos[i]];
  4655. /* chip->oob_poi[chip->ecc.layout->eccpos[i]] = local_oob_buf[iter]; */
  4656. }
  4657. /* copy FDM data */
  4658. for (i = 0; i < sec_num; i++) {
  4659. memcpy(&local_oob_buf[i * spare_per_sector],
  4660. &chip->oob_poi[i * OOB_AVAI_PER_SECTOR], OOB_AVAI_PER_SECTOR);
  4661. }
  4662. return mtk_nand_write_oob_raw(mtd, local_oob_buf, page, mtd->oobsize);
  4663. }
  4664. static int mtk_nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
  4665. {
  4666. /* int block_size = 1 << (chip->phys_erase_shift); */
  4667. int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  4668. /* int page_per_block1 = page_per_block; */
  4669. u32 block;
  4670. u16 page_in_block;
  4671. u32 mapped_block;
  4672. /* block = page / page_per_block1; */
  4673. /* mapped_block = get_mapping_block_index(block); */
  4674. page_in_block = mtk_nand_page_transform(mtd, chip, page, &block, &mapped_block);
  4675. if (mapped_block != block)
  4676. set_bad_index_to_oob(chip->oob_poi, block);
  4677. else
  4678. set_bad_index_to_oob(chip->oob_poi, FAKE_INDEX);
  4679. if (mtk_nand_write_oob_hw(mtd, chip, page_in_block + mapped_block * page_per_block /* page */)) {
  4680. pr_err("write oob fail at block: 0x%x, page: 0x%x\n", mapped_block,
  4681. page_in_block);
  4682. if (update_bmt((u64) ((u64) page_in_block + (u64) mapped_block * page_per_block) <<
  4683. chip->page_shift, UPDATE_WRITE_FAIL, NULL, chip->oob_poi)) {
  4684. pr_debug("Update BMT success\n");
  4685. return 0;
  4686. }
  4687. pr_err("Update BMT fail\n");
  4688. return -EIO;
  4689. }
  4690. return 0;
  4691. }
  4692. int mtk_nand_block_markbad_hw(struct mtd_info *mtd, loff_t offset)
  4693. {
  4694. struct nand_chip *chip = mtd->priv;
  4695. int block = (int)(offset >> chip->phys_erase_shift);
  4696. int page = block * (1 << (chip->phys_erase_shift - chip->page_shift));
  4697. int ret;
  4698. u8 buf[8];
  4699. memset(buf, 0xFF, 8);
  4700. buf[0] = 0;
  4701. ret = mtk_nand_write_oob_raw(mtd, buf, page, 8);
  4702. return ret;
  4703. }
  4704. static int mtk_nand_block_markbad(struct mtd_info *mtd, loff_t offset)
  4705. {
  4706. struct nand_chip *chip = mtd->priv;
  4707. u32 block = (u32) (offset >> chip->phys_erase_shift);
  4708. int page = block * (1 << (chip->phys_erase_shift - chip->page_shift));
  4709. u32 mapped_block;
  4710. int ret;
  4711. nand_get_device(mtd, FL_WRITING);
  4712. /* mapped_block = get_mapping_block_index(block); */
  4713. page = mtk_nand_page_transform(mtd, chip, page, &block, &mapped_block);
  4714. ret = mtk_nand_block_markbad_hw(mtd, mapped_block << chip->phys_erase_shift);
  4715. nand_release_device(mtd);
  4716. return ret;
  4717. }
  4718. int mtk_nand_read_oob_hw(struct mtd_info *mtd, struct nand_chip *chip, int page)
  4719. {
  4720. int i;
  4721. u8 iter = 0;
  4722. int sec_num = 1 << (chip->page_shift - host->hw->nand_sec_shift);
  4723. int spare_per_sector = mtd->oobsize / sec_num;
  4724. #ifdef TESTTIME
  4725. unsigned long long time1, time2;
  4726. time1 = sched_clock();
  4727. #endif
  4728. if (mtk_nand_read_oob_raw(mtd, chip->oob_poi, page, mtd->oobsize)) {
  4729. /* pr_debug(KERN_ERR "[%s]mtk_nand_read_oob_raw return failed\n", __FUNCTION__); */
  4730. return -EIO;
  4731. }
  4732. #ifdef TESTTIME
  4733. time2 = sched_clock() - time1;
  4734. if (!readoobflag) {
  4735. readoobflag = 1;
  4736. pr_err("[%s] time is %llu", __func__, time2);
  4737. }
  4738. #endif
  4739. /* adjust to ecc physical layout to memory layout */
  4740. /*********************************************************/
  4741. /* FDM0 | ECC0 | FDM1 | ECC1 | FDM2 | ECC2 | FDM3 | ECC3 */
  4742. /* 8B | 8B | 8B | 8B | 8B | 8B | 8B | 8B */
  4743. /*********************************************************/
  4744. memcpy(local_oob_buf, chip->oob_poi, mtd->oobsize);
  4745. /* copy ecc data */
  4746. for (i = 0; i < chip->ecc.layout->eccbytes; i++) {
  4747. iter = (i / OOB_AVAI_PER_SECTOR) * spare_per_sector + OOB_AVAI_PER_SECTOR +
  4748. i % OOB_AVAI_PER_SECTOR;
  4749. chip->oob_poi[chip->ecc.layout->eccpos[i]] = local_oob_buf[iter];
  4750. }
  4751. /* copy FDM data */
  4752. for (i = 0; i < sec_num; i++) {
  4753. memcpy(&chip->oob_poi[i * OOB_AVAI_PER_SECTOR],
  4754. &local_oob_buf[i * spare_per_sector], OOB_AVAI_PER_SECTOR);
  4755. }
  4756. return 0;
  4757. }
  4758. static int mtk_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
  4759. {
  4760. /* int block_size = 1 << (chip->phys_erase_shift); */
  4761. /* int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); */
  4762. /* int block; */
  4763. /* u16 page_in_block; */
  4764. /* int mapped_block; */
  4765. /* u8* buf = (u8*)kzalloc(mtd->writesize, GFP_KERNEL); */
  4766. /* page = mtk_nand_page_transform(mtd,chip,page,&block,&mapped_block); */
  4767. #if 0
  4768. if (block_size != mtd->erasesize)
  4769. page_per_block1 = page_per_block >> 1;
  4770. block = page / page_per_block1;
  4771. mapped_block = get_mapping_block_index(block);
  4772. if (block_size != mtd->erasesize)
  4773. page_in_block = devinfo.feature_set.PairPage[page % page_per_block1];
  4774. else
  4775. page_in_block = page % page_per_block1;
  4776. mtk_nand_read_oob_hw(mtd, chip, page_in_block + mapped_block * page_per_block);
  4777. #else
  4778. mtk_nand_read_page(mtd, chip, temp_buffer_16_align, page);
  4779. /* kfree(buf); */
  4780. #endif
  4781. return 0; /* the return value is sndcmd */
  4782. }
  4783. int mtk_nand_block_bad_hw(struct mtd_info *mtd, loff_t ofs)
  4784. {
  4785. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  4786. int page_addr = (int)(ofs >> chip->page_shift);
  4787. u32 block, mapped_block;
  4788. int ret;
  4789. unsigned int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  4790. /* unsigned char oob_buf[128]; */
  4791. /* char* buf = (char*) kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); */
  4792. /* page_addr = mtk_nand_page_transform(mtd, chip, page_addr, &block, &mapped_block); */
  4793. page_addr &= ~(page_per_block - 1);
  4794. /* ret = mtk_nand_read_page(mtd,chip,buf,(ofs >> chip->page_shift)); */
  4795. memset(temp_buffer_16_align, 0xFF, LPAGE);
  4796. ret = mtk_nand_read_subpage(mtd, chip, temp_buffer_16_align, (ofs >> chip->page_shift), 0, 1);
  4797. page_addr = mtk_nand_page_transform(mtd, chip, page_addr, &block, &mapped_block);
  4798. /* ret = mtk_nand_exec_read_page(mtd, page_addr+mapped_block*page_per_block, mtd->writesize, buf, oob_buf); */
  4799. if (0 != ret) {
  4800. pr_warn("mtk_nand_read_oob_raw return error %d\n", ret);
  4801. /* kfree(buf); */
  4802. return 1;
  4803. }
  4804. if (chip->oob_poi[0] != 0xff) {
  4805. pr_debug("Bad block detected at 0x%x, oob_buf[0] is 0x%x\n",
  4806. block * page_per_block, chip->oob_poi[0]);
  4807. /* kfree(buf); */
  4808. /* dump_nfi(); */
  4809. return 1;
  4810. }
  4811. /* kfree(buf); */
  4812. return 0; /* everything is OK, good block */
  4813. }
  4814. static int mtk_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  4815. {
  4816. int chipnr = 0;
  4817. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  4818. int block = (int)(ofs >> chip->phys_erase_shift);
  4819. int mapped_block;
  4820. int page = (int)(ofs >> chip->page_shift);
  4821. int page_in_block;
  4822. int page_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
  4823. int ret;
  4824. if (getchip) {
  4825. chipnr = (int)(ofs >> chip->chip_shift);
  4826. nand_get_device(mtd, FL_READING);
  4827. /* Select the NAND device */
  4828. chip->select_chip(mtd, chipnr);
  4829. }
  4830. /* page = mtk_nand_page_transform(mtd, chip, page, &block, &mapped_block); */
  4831. /* mapped_block = get_mapping_block_index(block); */
  4832. ret = mtk_nand_block_bad_hw(mtd, ofs);
  4833. page_in_block = mtk_nand_page_transform(mtd, chip, page, &block, &mapped_block);
  4834. if (ret) {
  4835. pr_debug("Unmapped bad block: 0x%x %d\n", mapped_block, ret);
  4836. if (update_bmt((u64) ((u64) page_in_block + (u64) mapped_block * page_per_block) <<
  4837. chip->page_shift, UPDATE_UNMAPPED_BLOCK, NULL, NULL)) {
  4838. pr_debug("Update BMT success\n");
  4839. ret = 0;
  4840. } else {
  4841. pr_err("Update BMT fail\n");
  4842. ret = 1;
  4843. }
  4844. }
  4845. if (getchip)
  4846. nand_release_device(mtd);
  4847. return ret;
  4848. }
  4849. /******************************************************************************
  4850. * mtk_nand_init_size
  4851. *
  4852. * DESCRIPTION:
  4853. * initialize the pagesize, oobsize, blocksize
  4854. *
  4855. * PARAMETERS:
  4856. * struct mtd_info *mtd, struct nand_chip *this, u8 *id_data
  4857. *
  4858. * RETURNS:
  4859. * Buswidth
  4860. *
  4861. * NOTES:
  4862. * None
  4863. *
  4864. ******************************************************************************/
  4865. static int mtk_nand_init_size(struct mtd_info *mtd, struct nand_chip *this, u8 *id_data)
  4866. {
  4867. /* Get page size */
  4868. mtd->writesize = devinfo.pagesize;
  4869. /* Get oobsize */
  4870. mtd->oobsize = devinfo.sparesize;
  4871. /* Get blocksize. */
  4872. mtd->erasesize = devinfo.blocksize * 1024;
  4873. /* Get buswidth information */
  4874. if (devinfo.iowidth == 16)
  4875. return NAND_BUSWIDTH_16;
  4876. else
  4877. return 0;
  4878. }
  4879. /******************************************************************************
  4880. * mtk_nand_verify_buf
  4881. *
  4882. * DESCRIPTION:
  4883. * Verify the NAND write data is correct or not !
  4884. *
  4885. * PARAMETERS:
  4886. * struct mtd_info *mtd, const uint8_t *buf, int len
  4887. *
  4888. * RETURNS:
  4889. * None
  4890. *
  4891. * NOTES:
  4892. * None
  4893. *
  4894. ******************************************************************************/
  4895. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  4896. char gacBuf[LPAGE + LSPARE];
  4897. static int mtk_nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  4898. {
  4899. #if 1
  4900. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  4901. struct NAND_CMD *pkCMD = &g_kCMD;
  4902. u32 u4PageSize = mtd->writesize;
  4903. u32 *pSrc, *pDst;
  4904. int i;
  4905. mtk_nand_exec_read_page(mtd, pkCMD->u4RowAddr, u4PageSize, gacBuf, gacBuf + u4PageSize);
  4906. pSrc = (u32 *) buf;
  4907. pDst = (u32 *) gacBuf;
  4908. len = len / sizeof(u32);
  4909. for (i = 0; i < len; ++i) {
  4910. if (*pSrc != *pDst) {
  4911. pr_err("mtk_nand_verify_buf page fail at page %d\n", pkCMD->u4RowAddr);
  4912. return -1;
  4913. }
  4914. pSrc++;
  4915. pDst++;
  4916. }
  4917. pSrc = (u32 *) chip->oob_poi;
  4918. pDst = (u32 *) (gacBuf + u4PageSize);
  4919. if ((pSrc[0] != pDst[0]) || (pSrc[1] != pDst[1]) || (pSrc[2] != pDst[2])
  4920. || (pSrc[3] != pDst[3]) || (pSrc[4] != pDst[4]) || (pSrc[5] != pDst[5]))
  4921. /* TODO: Ask Designer Why? */
  4922. /* (pSrc[6] != pDst[6]) || (pSrc[7] != pDst[7])) */
  4923. {
  4924. pr_err("mtk_nand_verify_buf oob fail at page %d\n", pkCMD->u4RowAddr);
  4925. pr_err("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", pSrc[0], pSrc[1], pSrc[2],
  4926. pSrc[3], pSrc[4], pSrc[5], pSrc[6], pSrc[7]);
  4927. pr_err("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n", pDst[0], pDst[1], pDst[2],
  4928. pDst[3], pDst[4], pDst[5], pDst[6], pDst[7]);
  4929. return -1;
  4930. }
  4931. /*
  4932. for (i = 0; i < len; ++i) {
  4933. if (*pSrc != *pDst) {
  4934. pr_debug(KERN_ERR"mtk_nand_verify_buf oob fail at page %d\n", g_kCMD.u4RowAddr);
  4935. return -1;
  4936. }
  4937. pSrc++;
  4938. pDst++;
  4939. }
  4940. */
  4941. /* pr_debug(KERN_INFO"mtk_nand_verify_buf OK at page %d\n", g_kCMD.u4RowAddr); */
  4942. return 0;
  4943. #else
  4944. return 0;
  4945. #endif
  4946. }
  4947. #endif
  4948. /******************************************************************************
  4949. * mtk_nand_init_hw
  4950. *
  4951. * DESCRIPTION:
  4952. * Initial NAND device hardware component !
  4953. *
  4954. * PARAMETERS:
  4955. * struct mtk_nand_host *host (Initial setting data)
  4956. *
  4957. * RETURNS:
  4958. * None
  4959. *
  4960. * NOTES:
  4961. * None
  4962. *
  4963. ******************************************************************************/
  4964. static void mtk_nand_init_hw(struct mtk_nand_host *host)
  4965. {
  4966. struct mtk_nand_host_hw *hw = host->hw;
  4967. g_bInitDone = false;
  4968. g_kCMD.u4OOBRowAddr = (u32) -1;
  4969. /* Set default NFI access timing control */
  4970. DRV_WriteReg32(NFI_ACCCON_REG32, hw->nfi_access_timing);
  4971. DRV_WriteReg16(NFI_CNFG_REG16, 0);
  4972. DRV_WriteReg32(NFI_PAGEFMT_REG32, 4);
  4973. DRV_WriteReg32(NFI_ENMPTY_THRESH_REG32, 40);
  4974. /* Reset the state machine and data FIFO, because flushing FIFO */
  4975. (void)mtk_nand_reset();
  4976. /* Set the ECC engine */
  4977. if (hw->nand_ecc_mode == NAND_ECC_HW) {
  4978. pr_notice("Use HW ECC\n");
  4979. if (g_bHwEcc)
  4980. NFI_SET_REG32(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  4981. ECC_Config(host->hw, 4);
  4982. mtk_nand_configure_fdm(8);
  4983. }
  4984. /* Initialize interrupt. Clear interrupt, read clear. */
  4985. DRV_Reg16(NFI_INTR_REG16);
  4986. /* Interrupt arise when read data or program data to/from AHB is done. */
  4987. DRV_WriteReg16(NFI_INTR_EN_REG16, 0);
  4988. /* Enable automatic disable ECC clock when NFI is busy state */
  4989. DRV_WriteReg16(NFI_DEBUG_CON1_REG16, (NFI_BYPASS | WBUF_EN | HWDCM_SWCON_ON));
  4990. #ifdef CONFIG_PM
  4991. host->saved_para.suspend_flag = 0;
  4992. #endif
  4993. /* Reset */
  4994. }
  4995. /* ------------------------------------------------------------------------------- */
  4996. static int mtk_nand_dev_ready(struct mtd_info *mtd)
  4997. {
  4998. return !(DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY);
  4999. }
  5000. /******************************************************************************
  5001. * mtk_nand_proc_read
  5002. *
  5003. * DESCRIPTION:
  5004. * Read the proc file to get the interrupt scheme setting !
  5005. *
  5006. * PARAMETERS:
  5007. * char *page, char **start, off_t off, int count, int *eof, void *data
  5008. *
  5009. * RETURNS:
  5010. * None
  5011. *
  5012. * NOTES:
  5013. * None
  5014. *
  5015. ******************************************************************************/
  5016. int mtk_nand_proc_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
  5017. {
  5018. char *p = buffer;
  5019. int len = 0;
  5020. int i;
  5021. p += sprintf(p, "ID:");
  5022. for (i = 0; i < devinfo.id_length; i++)
  5023. p += sprintf(p, " 0x%x", devinfo.id[i]);
  5024. p += sprintf(p, "\n");
  5025. p += sprintf(p, "total size: %dMiB; part number: %s\n", devinfo.totalsize,
  5026. devinfo.devciename);
  5027. p += sprintf(p, "Current working in %s mode\n", g_i4Interrupt ? "interrupt" : "polling");
  5028. p += sprintf(p, "NFI_ACCON=0x%x\n", DRV_Reg32(NFI_ACCCON_REG32));
  5029. p += sprintf(p, "NFI_NAND_TYPE_CNFG_REG32= 0x%x\n", DRV_Reg32(NFI_NAND_TYPE_CNFG_REG32));
  5030. #ifdef CONFIG_MTK_FPGA
  5031. p += sprintf(p, "[FPGA Dummy]DRV_CFG_NFIA(0x0)=0x0\n");
  5032. p += sprintf(p, "[FPGA Dummy]DRV_CFG_NFIB(0x0)=0x0\n");
  5033. #else
  5034. p += sprintf(p, "DRV_CFG_NFIA=0x%x\n", *((volatile u32 *)(GPIO_BASE + 0xC20)));
  5035. p += sprintf(p, "DRV_CFG_NFIB=0x%x\n", *((volatile u32 *)(GPIO_BASE + 0xB50)));
  5036. #endif
  5037. #if CFG_PERFLOG_DEBUG
  5038. p += sprintf(p, "Read Page Count:%d, Read Page totalTime:%lu, Avg. RPage:%lu\r\n",
  5039. g_NandPerfLog.ReadPageCount, g_NandPerfLog.ReadPageTotalTime,
  5040. g_NandPerfLog.ReadPageCount ? (g_NandPerfLog.ReadPageTotalTime /
  5041. g_NandPerfLog.ReadPageCount) : 0);
  5042. p += sprintf(p, "Read subPage Count:%d, Read subPage totalTime:%lu, Avg. RPage:%lu\r\n",
  5043. g_NandPerfLog.ReadSubPageCount, g_NandPerfLog.ReadSubPageTotalTime,
  5044. g_NandPerfLog.ReadSubPageCount ? (g_NandPerfLog.ReadSubPageTotalTime /
  5045. g_NandPerfLog.ReadSubPageCount) : 0);
  5046. p += sprintf(p, "Read Busy Count:%d, Read Busy totalTime:%lu, Avg. R Busy:%lu\r\n",
  5047. g_NandPerfLog.ReadBusyCount, g_NandPerfLog.ReadBusyTotalTime,
  5048. g_NandPerfLog.ReadBusyCount ? (g_NandPerfLog.ReadBusyTotalTime /
  5049. g_NandPerfLog.ReadBusyCount) : 0);
  5050. p += sprintf(p, "Read DMA Count:%d, Read DMA totalTime:%lu, Avg. R DMA:%lu\r\n",
  5051. g_NandPerfLog.ReadDMACount, g_NandPerfLog.ReadDMATotalTime,
  5052. g_NandPerfLog.ReadDMACount ? (g_NandPerfLog.ReadDMATotalTime /
  5053. g_NandPerfLog.ReadDMACount) : 0);
  5054. p += sprintf(p, "Write Page Count:%d, Write Page totalTime:%lu, Avg. WPage:%lu\r\n",
  5055. g_NandPerfLog.WritePageCount, g_NandPerfLog.WritePageTotalTime,
  5056. g_NandPerfLog.WritePageCount ? (g_NandPerfLog.WritePageTotalTime /
  5057. g_NandPerfLog.WritePageCount) : 0);
  5058. p += sprintf(p, "Write Busy Count:%d, Write Busy totalTime:%lu, Avg. W Busy:%lu\r\n",
  5059. g_NandPerfLog.WriteBusyCount, g_NandPerfLog.WriteBusyTotalTime,
  5060. g_NandPerfLog.WriteBusyCount ? (g_NandPerfLog.WriteBusyTotalTime /
  5061. g_NandPerfLog.WriteBusyCount) : 0);
  5062. p += sprintf(p, "Write DMA Count:%d, Write DMA totalTime:%lu, Avg. W DMA:%lu\r\n",
  5063. g_NandPerfLog.WriteDMACount, g_NandPerfLog.WriteDMATotalTime,
  5064. g_NandPerfLog.WriteDMACount ? (g_NandPerfLog.WriteDMATotalTime /
  5065. g_NandPerfLog.WriteDMACount) : 0);
  5066. p += sprintf(p, "EraseBlock Count:%d, EraseBlock totalTime:%lu, Avg. Erase:%lu\r\n",
  5067. g_NandPerfLog.EraseBlockCount, g_NandPerfLog.EraseBlockTotalTime,
  5068. g_NandPerfLog.EraseBlockCount ? (g_NandPerfLog.EraseBlockTotalTime /
  5069. g_NandPerfLog.EraseBlockCount) : 0);
  5070. #endif
  5071. len = p - buffer;
  5072. return len < count ? len : count;
  5073. }
  5074. /******************************************************************************
  5075. * mtk_nand_proc_write
  5076. *
  5077. * DESCRIPTION:
  5078. * Write the proc file to set the interrupt scheme !
  5079. *
  5080. * PARAMETERS:
  5081. * struct file* file, const char* buffer, unsigned long count, void *data
  5082. *
  5083. * RETURNS:
  5084. * None
  5085. *
  5086. * NOTES:
  5087. * None
  5088. *
  5089. ******************************************************************************/
  5090. ssize_t mtk_nand_proc_write(struct file *file, const char __user *buffer, size_t count,
  5091. loff_t *data)
  5092. {
  5093. struct mtd_info *mtd = &host->mtd;
  5094. char buf[16];
  5095. char cmd;
  5096. int value;
  5097. int len = count; /* , n; */
  5098. if (len >= sizeof(buf))
  5099. len = sizeof(buf) - 1;
  5100. if (copy_from_user(buf, buffer, len))
  5101. return -EFAULT;
  5102. if (sscanf(buf, "%c%x", &cmd, &value) != 2)
  5103. return -EINVAL;
  5104. switch (cmd) {
  5105. case 'A': /* NFIA driving setting */
  5106. #ifdef CONFIG_MTK_FPGA
  5107. pr_debug("[FPGA Dummy]NFIA driving setting\n");
  5108. #else
  5109. if ((value >= 0x0) && (value <= 0x7)) { /* driving step */
  5110. pr_debug("[NAND]IO PAD driving setting value(0x%x)\n\n", value);
  5111. *((volatile u32 *)(GPIO_BASE + 0xC20)) = value; /* pad 7 6 4 3 0 1 5 8 2 */
  5112. } else
  5113. pr_err("[NAND]IO PAD driving setting value(0x%x) error\n", value);
  5114. #endif
  5115. break;
  5116. case 'B': /* NFIB driving setting */
  5117. #ifdef CONFIG_MTK_FPGA
  5118. pr_debug("[FPGA Dummy]NFIB driving setting\n");
  5119. #else
  5120. if ((value >= 0x0) && (value <= 0x7)) { /* driving step */
  5121. pr_debug("[NAND]Ctrl PAD driving setting value(0x%x)\n\n", value);
  5122. *((volatile u32 *)(GPIO_BASE + 0xB50)) = value; /* CLE CE1 CE0 RE RB */
  5123. *((volatile u32 *)(GPIO_BASE + 0xC10)) = value; /* ALE */
  5124. *((volatile u32 *)(GPIO_BASE + 0xC00)) = value; /* WE */
  5125. } else
  5126. pr_err("[NAND]Ctrl PAD driving setting value(0x%x) error\n",
  5127. value);
  5128. #endif
  5129. break;
  5130. case 'D':
  5131. #ifdef _MTK_NAND_DUMMY_DRIVER_
  5132. pr_debug("Enable dummy driver\n");
  5133. dummy_driver_debug = 1;
  5134. #endif
  5135. break;
  5136. case 'I': /* Interrupt control */
  5137. if ((value > 0 && !g_i4Interrupt) || (value == 0 && g_i4Interrupt)) {
  5138. nand_get_device(mtd, FL_READING);
  5139. g_i4Interrupt = value;
  5140. if (g_i4Interrupt) {
  5141. DRV_Reg16(NFI_INTR_REG16);
  5142. enable_irq(MT_NFI_IRQ_ID);
  5143. } else
  5144. disable_irq(MT_NFI_IRQ_ID);
  5145. nand_release_device(mtd);
  5146. }
  5147. break;
  5148. case 'P': /* Reset Performance monitor counter */
  5149. #ifdef NAND_PFM
  5150. /* Reset values */
  5151. g_PFM_R = 0;
  5152. g_PFM_W = 0;
  5153. g_PFM_E = 0;
  5154. g_PFM_RD = 0;
  5155. g_PFM_WD = 0;
  5156. g_kCMD.pureReadOOBNum = 0;
  5157. #endif
  5158. break;
  5159. case 'R': /* Reset NFI performance log */
  5160. #if CFG_PERFLOG_DEBUG
  5161. g_NandPerfLog.ReadPageCount = 0;
  5162. g_NandPerfLog.ReadPageTotalTime = 0;
  5163. g_NandPerfLog.ReadBusyCount = 0;
  5164. g_NandPerfLog.ReadBusyTotalTime = 0;
  5165. g_NandPerfLog.ReadDMACount = 0;
  5166. g_NandPerfLog.ReadDMATotalTime = 0;
  5167. g_NandPerfLog.ReadSubPageCount = 0;
  5168. g_NandPerfLog.ReadSubPageTotalTime = 0;
  5169. g_NandPerfLog.WritePageCount = 0;
  5170. g_NandPerfLog.WritePageTotalTime = 0;
  5171. g_NandPerfLog.WriteBusyCount = 0;
  5172. g_NandPerfLog.WriteBusyTotalTime = 0;
  5173. g_NandPerfLog.WriteDMACount = 0;
  5174. g_NandPerfLog.WriteDMATotalTime = 0;
  5175. g_NandPerfLog.EraseBlockCount = 0;
  5176. g_NandPerfLog.EraseBlockTotalTime = 0;
  5177. #endif
  5178. break;
  5179. case 'T': /* ACCCON Setting */
  5180. nand_get_device(mtd, FL_READING);
  5181. DRV_WriteReg32(NFI_ACCCON_REG32, value);
  5182. nand_release_device(mtd);
  5183. break;
  5184. default:
  5185. break;
  5186. }
  5187. return len;
  5188. }
  5189. #ifndef CONFIG_MTK_FPGA
  5190. #if 0
  5191. #define EFUSE_GPIO_CFG ((volatile u32 *)(mtk_efuse_base + 0x01C0))
  5192. #define EFUSE_GPIO_1_8_ENABLE 0x00000008
  5193. static unsigned short NFI_gpio_uffs(unsigned short x)
  5194. {
  5195. unsigned int r = 1;
  5196. if (!x)
  5197. return 0;
  5198. if (!(x & 0xff)) {
  5199. x >>= 8;
  5200. r += 8;
  5201. }
  5202. if (!(x & 0xf)) {
  5203. x >>= 4;
  5204. r += 4;
  5205. }
  5206. if (!(x & 3)) {
  5207. x >>= 2;
  5208. r += 2;
  5209. }
  5210. if (!(x & 1)) {
  5211. x >>= 1;
  5212. r += 1;
  5213. }
  5214. return r;
  5215. }
  5216. static void NFI_GPIO_SET_FIELD(unsigned long reg, unsigned int field, unsigned int val)
  5217. {
  5218. unsigned short tv = (unsigned short)(*(volatile unsigned long *)(reg));
  5219. tv &= ~(field);
  5220. tv |= ((val) << (NFI_gpio_uffs((unsigned short)(field)) - 1));
  5221. (*(volatile unsigned long *)(reg) = (u16) (tv));
  5222. }
  5223. static void mtk_nand_gpio_init(void)
  5224. {
  5225. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xc00,
  5226. 0x700, 0x2); /* pullup with 50Kohm ----PAD_MSDC0_CLK for 1.8v/3.3v */
  5227. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xc10,
  5228. 0x700, 0x3); /* pulldown with 50Kohm ----PAD_MSDC0_CMD for 1.8v/3.3v */
  5229. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xc30,
  5230. 0x70, 0x3); /* pulldown with 50Kohm ----PAD_MSDC0_DAT1 for 1.8v/3.3v */
  5231. mt_set_gpio_mode(GPIO46, GPIO_MODE_06);
  5232. mt_set_gpio_mode(GPIO47, GPIO_MODE_06);
  5233. mt_set_gpio_mode(GPIO48, GPIO_MODE_06);
  5234. mt_set_gpio_mode(GPIO49, GPIO_MODE_06);
  5235. mt_set_gpio_mode(GPIO127, GPIO_MODE_04);
  5236. mt_set_gpio_mode(GPIO128, GPIO_MODE_04);
  5237. mt_set_gpio_mode(GPIO129, GPIO_MODE_04);
  5238. mt_set_gpio_mode(GPIO130, GPIO_MODE_04);
  5239. mt_set_gpio_mode(GPIO131, GPIO_MODE_04);
  5240. mt_set_gpio_mode(GPIO132, GPIO_MODE_04);
  5241. mt_set_gpio_mode(GPIO133, GPIO_MODE_04);
  5242. mt_set_gpio_mode(GPIO134, GPIO_MODE_04);
  5243. mt_set_gpio_mode(GPIO135, GPIO_MODE_04);
  5244. mt_set_gpio_mode(GPIO136, GPIO_MODE_04);
  5245. mt_set_gpio_mode(GPIO137, GPIO_MODE_05);
  5246. mt_set_gpio_mode(GPIO142, GPIO_MODE_01);
  5247. mt_set_gpio_pull_enable(GPIO142, 1);
  5248. mt_set_gpio_pull_select(GPIO142, 1);
  5249. if (!((*EFUSE_GPIO_CFG) & EFUSE_GPIO_1_8_ENABLE)) { /* 3.3v */
  5250. pr_debug("3.3V\n");
  5251. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xd70, 0xf, 0x0a); /* TDSEL change value to 0x0a */
  5252. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xd70, 0x3f0, 0x0c); /* RDSEL change value to 0x0c */
  5253. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xc60, 0xf, 0x0a); /* TDSEL change value to 0x0a */
  5254. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xc60, 0x3f0, 0x0c); /* RDSEL change value to 0x0c */
  5255. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xe20, 0xf000, 0x5); /* BIAS CTRL0 */
  5256. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xe20, 0x000f, 0x5);
  5257. } else { /* 1.8v */
  5258. pr_debug("1.8V\n");
  5259. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xd70, 0xf, 0x0a); /* TDSEL change value to 0x0a */
  5260. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xd70, 0x3f0, 0x00); /* RDSEL change value to 0x0c */
  5261. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xc60, 0xf, 0x0a); /* TDSEL change value to 0x0a */
  5262. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xc60, 0x3f0, 0x00); /* RDSEL change value to 0x0c */
  5263. }
  5264. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xc00, 0x7, 0x3); /* set CLK driving more than 4mA default:0x3 */
  5265. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xc10, 0x7, 0x3); /* set CMD driving more than 4mA */
  5266. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xc20, 0x7, 0x3); /* set DAT driving more than 4mA */
  5267. NFI_GPIO_SET_FIELD(GPIO_BASE + 0xb50, 0x7, 0x3); /* set NFI_PAD driving more than 4mA */
  5268. /* DRV_WriteReg32(GPIO_BASE+0x180, 0x7FFF); */
  5269. /* DRV_WriteReg32(GPIO_BASE+0x280, 0x7FDF); */
  5270. }
  5271. #endif
  5272. #endif
  5273. /******************************************************************************
  5274. * mtk_nand_probe
  5275. *
  5276. * DESCRIPTION:
  5277. * register the nand device file operations !
  5278. *
  5279. * PARAMETERS:
  5280. * struct platform_device *pdev : device structure
  5281. *
  5282. * RETURNS:
  5283. * 0 : Success
  5284. *
  5285. * NOTES:
  5286. * None
  5287. *
  5288. ******************************************************************************/
  5289. #define KERNEL_NAND_UNIT_TEST 0
  5290. #define NAND_READ_PERFORMANCE 0
  5291. #if KERNEL_NAND_UNIT_TEST
  5292. __aligned(64)
  5293. static u8 temp_buffer_xl[LPAGE + LSPARE];
  5294. __aligned(64)
  5295. static u8 temp_buffer_xl_rd[LPAGE + LSPARE];
  5296. int mtk_nand_unit_test(struct nand_chip *nand_chip, struct mtd_info *mtd)
  5297. {
  5298. pr_debug("Begin to Kernel nand unit test ...\n");
  5299. int err = 0;
  5300. int patternbuff[128] = {
  5301. 0x0103D901, 0xFF1802DF, 0x01200400, 0x00000021, 0x02040122, 0x02010122, 0x03020407,
  5302. 0x1A050103,
  5303. 0x00020F1B, 0x08C0C0A1, 0x01550800, 0x201B0AC1, 0x41990155, 0x64F0FFFF, 0x201B0C82,
  5304. 0x4118EA61,
  5305. 0xF00107F6, 0x0301EE1B, 0x0C834118, 0xEA617001, 0x07760301, 0xEE151405, 0x00202020,
  5306. 0x20202020,
  5307. 0x00202020, 0x2000302E, 0x3000FF14, 0x00FF0000, 0x00000000, 0x00000000, 0x00000000,
  5308. 0x00000000,
  5309. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  5310. 0x00000000,
  5311. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  5312. 0x00000000,
  5313. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  5314. 0x00000000,
  5315. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  5316. 0x00000000,
  5317. 0x01D90301, 0xDF0218FF, 0x00042001, 0x21000000, 0x22010402, 0x22010102, 0x07040203,
  5318. 0x0301051A,
  5319. 0x1B0F0200, 0xA1C0C008, 0x00085501, 0xC10A1B20, 0x55019941, 0xFFFFF064, 0x820C1B20,
  5320. 0x61EA1841,
  5321. 0xF60701F0, 0x1BEE0103, 0x1841830C, 0x017061EA, 0x01037607, 0x051415EE, 0x20202000,
  5322. 0x20202020,
  5323. 0x20202000, 0x2E300020, 0x14FF0030, 0x0000FF00, 0x00000000, 0x00000000, 0x00000000,
  5324. 0x00000000,
  5325. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  5326. 0x00000000,
  5327. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  5328. 0x00000000,
  5329. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  5330. 0x00000000,
  5331. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  5332. 0x00000000
  5333. };
  5334. u32 j, k, p = g_block_size / g_page_size, m;
  5335. pr_debug("[P] %x\n", p);
  5336. pr_debug("[xiaolei] bias = 0x%x", *(volatile u32 *)(GPIO_BASE + 0xE20));
  5337. pr_debug("[xiaolei] ACC = 0x%x", *(volatile u32 *)(NFI_BASE + 0x00C));
  5338. struct gFeatureSet *feature_set = &(devinfo.feature_set.FeatureSet);
  5339. u32 val = 0x05, TOTAL = 1000;
  5340. for (m = 0; m < 32; m++)
  5341. memcpy(temp_buffer_xl + 512 * m, (u8 *) patternbuff, 512);
  5342. pr_debug("***************read pl***********************\n");
  5343. memset(temp_buffer_xl_rd, 0xA5, 16384);
  5344. if (mtk_nand_read_page(mtd, nand_chip, temp_buffer_xl_rd, 1 * p))
  5345. pr_debug("Read page 0x%x fail!\n", 1 * p);
  5346. for (m = 0; m < 32; m++)
  5347. pr_debug("[5]0x%x %x %x %x\n", *((int *)temp_buffer_xl_rd + m * 4),
  5348. *((int *)temp_buffer_xl_rd + 1 + m * 4),
  5349. *((int *)temp_buffer_xl_rd + 2 + m * 4),
  5350. *((int *)temp_buffer_xl_rd + 3 + m * 4));
  5351. for (j = 0x400; j < 0x750; j++) {
  5352. /* memset(local_buffer, 0x00, 16384); */
  5353. /* mtk_nand_read_page(mtd, nand_chip, local_buffer, j*p); */
  5354. /* for(m = 0; m < 32; m++) */
  5355. /* MSG(INIT,"[1]0x%x %x %x %x\n",
  5356. *((int *)local_buffer+m*4), *((int *)local_buffer+1+m*4),
  5357. *((int *)local_buffer+2+m*4), *((int *)local_buffer+3+m*4)); */
  5358. mtk_nand_erase(mtd, j * p);
  5359. memset(temp_buffer_xl_rd, 0x00, 16384);
  5360. if (mtk_nand_read_page(mtd, nand_chip, temp_buffer_xl_rd, j * p))
  5361. pr_debug("Read page 0x%x fail!\n", j * p);
  5362. pr_debug("[2]0x%x %x %x %x\n", *(int *)temp_buffer_xl_rd,
  5363. *((int *)temp_buffer_xl_rd + 1), *((int *)temp_buffer_xl_rd + 2),
  5364. *((int *)temp_buffer_xl_rd + 3));
  5365. if (mtk_nand_block_bad(mtd, j * g_block_size, 0)) {
  5366. pr_debug("Bad block at %x\n", j);
  5367. continue;
  5368. }
  5369. for (k = 0; k < p; k++) {
  5370. pr_debug("***************w b***********************\n");
  5371. for (m = 0; m < 32; m++)
  5372. pr_debug("[1]0x%x %x %x %x\n", *((int *)temp_buffer_xl + m * 4),
  5373. *((int *)temp_buffer_xl + 1 + m * 4),
  5374. *((int *)temp_buffer_xl + 2 + m * 4),
  5375. *((int *)temp_buffer_xl + 3 + m * 4));
  5376. if (mtk_nand_write_page(mtd, nand_chip, 0, 0, temp_buffer_xl /*(u8 *)patternbuff */ , 0,
  5377. j * p + k, 0, 0))
  5378. pr_debug("Write page 0x%x fail!\n", j * p + k);
  5379. /* #if 1 */
  5380. /* } */
  5381. /* TOTAL=1000; */
  5382. /* do{ */
  5383. /* for (k = 0; k < p; k++) */
  5384. /* { */
  5385. /* #endif */
  5386. pr_debug("***************r b***********************\n");
  5387. memset(temp_buffer_xl_rd, 0x00, g_page_size);
  5388. if (mtk_nand_read_page(mtd, nand_chip, temp_buffer_xl_rd, j * p + k))
  5389. pr_debug("Read page 0x%x fail!\n", j * p + k);
  5390. for (m = 0; m < 32; m++)
  5391. pr_debug("[3]0x%x %x %x %x\n", *((int *)temp_buffer_xl_rd + m * 4),
  5392. *((int *)temp_buffer_xl_rd + 1 + m * 4),
  5393. *((int *)temp_buffer_xl_rd + 2 + m * 4),
  5394. *((int *)temp_buffer_xl_rd + 3 + m * 4));
  5395. if (memcmp(temp_buffer_xl /*(u8 *)patternbuff */ , temp_buffer_xl_rd,
  5396. 512 /*g_page_size */)) {
  5397. pr_debug("[KERNEL_NAND_UNIT_TEST] compare fail!\n");
  5398. err = -1;
  5399. while (1)
  5400. ;
  5401. } else {
  5402. TOTAL--;
  5403. pr_debug("[KERNEL_NAND_UNIT_TEST] compare OK!\n");
  5404. }
  5405. }
  5406. /* }while(TOTAL); */
  5407. #if 0
  5408. mtk_nand_SetFeature(mtd, (u16) feature_set->sfeatureCmd,
  5409. feature_set->Async_timing.address, (u8 *) &val,
  5410. sizeof(feature_set->Async_timing.feature));
  5411. mtk_nand_GetFeature(mtd, feature_set->gfeatureCmd,
  5412. feature_set->Async_timing.address, (u8 *) &val, 4);
  5413. pr_debug("[ASYNC Interface]0x%X\n", val);
  5414. err = mtk_nand_interface_config(mtd);
  5415. MSG(INIT, "[nand_interface_config] %d\n", err);
  5416. #endif
  5417. }
  5418. return err;
  5419. }
  5420. #endif
  5421. #if CFG_2CS_NAND
  5422. /* #define CHIP_ADDRESS (0x100000) */
  5423. static int mtk_nand_cs_check(struct mtd_info *mtd, u8 *id, u16 cs)
  5424. {
  5425. u8 ids[NAND_MAX_ID];
  5426. int i = 0;
  5427. /* if(devinfo.ttarget == TTYPE_2DIE) */
  5428. /* { */
  5429. /* MSG(INIT,"2 Die Flash\n"); */
  5430. /* g_bTricky_CS = TRUE; */
  5431. /* return 0; */
  5432. /* } */
  5433. DRV_WriteReg16(NFI_CSEL_REG16, cs);
  5434. mtk_nand_command_bp(mtd, NAND_CMD_READID, 0, -1);
  5435. for (i = 0; i < NAND_MAX_ID; i++) {
  5436. ids[i] = mtk_nand_read_byte(mtd);
  5437. if (ids[i] != id[i]) {
  5438. pr_notice("Nand cs[%d] not support(%d,%x)\n", cs, i, ids[i]);
  5439. DRV_WriteReg16(NFI_CSEL_REG16, NFI_DEFAULT_CS);
  5440. return 0;
  5441. }
  5442. }
  5443. DRV_WriteReg16(NFI_CSEL_REG16, NFI_DEFAULT_CS);
  5444. return 1;
  5445. }
  5446. static u32 mtk_nand_cs_on(struct nand_chip *nand_chip, u16 cs, u32 page)
  5447. {
  5448. u32 cs_page = page / g_nanddie_pages;
  5449. if (cs_page) {
  5450. DRV_WriteReg16(NFI_CSEL_REG16, cs);
  5451. /* if(devinfo.ttarget == TTYPE_2DIE) */
  5452. /* return page;//return (page | CHIP_ADDRESS); */
  5453. return (page - g_nanddie_pages);
  5454. }
  5455. DRV_WriteReg16(NFI_CSEL_REG16, NFI_DEFAULT_CS);
  5456. return page;
  5457. }
  5458. #else
  5459. #define mtk_nand_cs_check(mtd, id, cs) (1)
  5460. #define mtk_nand_cs_on(nand_chip, cs, page) (page)
  5461. #endif
  5462. static int mtk_nand_probe(struct platform_device *pdev)
  5463. {
  5464. struct mtk_nand_host_hw *hw;
  5465. struct mtd_info *mtd;
  5466. struct nand_chip *nand_chip;
  5467. /*struct resource *res = pdev->resource; */
  5468. int err = 0;
  5469. #if !defined(CONFIG_MTK_LEGACY)
  5470. int ret = 0;
  5471. #endif
  5472. u8 id[NAND_MAX_ID];
  5473. int i;
  5474. u32 sector_size = NAND_SECTOR_SIZE;
  5475. #if CFG_COMBO_NAND
  5476. int bmt_sz = 0;
  5477. #endif
  5478. #ifdef CONFIG_OF
  5479. /* dt modify */
  5480. mtk_nfi_base = of_iomap(pdev->dev.of_node, 0);
  5481. pr_debug("of_iomap for nfi base @ 0x%p\n", mtk_nfi_base);
  5482. if (mtk_nfiecc_node == NULL) {
  5483. mtk_nfiecc_node = of_find_compatible_node(NULL, NULL, "mediatek,mt8163-nfiecc");
  5484. mtk_nfiecc_base = of_iomap(mtk_nfiecc_node, 0);
  5485. pr_debug("of_iomap for nfiecc base @ 0x%p\n", mtk_nfiecc_base);
  5486. }
  5487. nfi_irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  5488. if (mtk_gpio_node == NULL) {
  5489. /* mtk_gpio_node = of_find_compatible_node(NULL, NULL, "mediatek,GPIO"); */
  5490. mtk_gpio_node = of_find_compatible_node(NULL, NULL, "mediatek,mt8163-pctl-a-syscfg");
  5491. mtk_gpio_base = of_iomap(mtk_gpio_node, 0);
  5492. pr_debug("of_iomap for gpio base @ 0x%p\n", mtk_gpio_base);
  5493. }
  5494. #ifdef CONFIG_MTK_LEGACY
  5495. if (mtk_efuse_node == NULL) {
  5496. mtk_efuse_node = of_find_compatible_node(NULL, NULL, "mediatek,EFUSEC");
  5497. mtk_efuse_base = of_iomap(mtk_efuse_node, 0);
  5498. pr_debug("of_iomap for efuse base @ 0x%p\n", mtk_efuse_base);
  5499. }
  5500. if (mtk_infra_node == NULL) {
  5501. mtk_infra_node = of_find_compatible_node(NULL, NULL, "mediatek,INFRACFG_AO");
  5502. mtk_infra_base = of_iomap(mtk_infra_node, 0);
  5503. pr_debug("of_iomap for infra base @ 0x%p\n", mtk_infra_base);
  5504. }
  5505. #endif
  5506. /* dt modify */
  5507. #endif
  5508. #if !defined(CONFIG_MTK_LEGACY)
  5509. nfi_clock = devm_clk_get(&pdev->dev, "infra_nfi");
  5510. BUG_ON(IS_ERR(nfi_clock));
  5511. nfi_ecc_clock = devm_clk_get(&pdev->dev, "infra_nfi_ecc");
  5512. BUG_ON(IS_ERR(nfi_ecc_clock));
  5513. nfi_bclk_clock = devm_clk_get(&pdev->dev, "infra_nfi_bclk");
  5514. BUG_ON(IS_ERR(nfi_bclk_clock));
  5515. onfi_sel_clock = devm_clk_get(&pdev->dev, "onfi_sel");
  5516. BUG_ON(IS_ERR(onfi_sel_clock));
  5517. onfi_26m_clock = devm_clk_get(&pdev->dev, "onfi_clk26m");
  5518. BUG_ON(IS_ERR(onfi_26m_clock));
  5519. syspll2_d2_clock = devm_clk_get(&pdev->dev, "syspll2_d2");
  5520. BUG_ON(IS_ERR(syspll2_d2_clock));
  5521. syspll_d7_clock = devm_clk_get(&pdev->dev, "syspll_d7");
  5522. BUG_ON(IS_ERR(syspll_d7_clock));
  5523. infra_nfi_sel_clock = devm_clk_get(&pdev->dev, "infra_nfi_sel");
  5524. BUG_ON(IS_ERR(infra_nfi_sel_clock));
  5525. axi_sel_clock = devm_clk_get(&pdev->dev, "axi_sel");
  5526. BUG_ON(IS_ERR(axi_sel_clock));
  5527. onfi_d2_clock = devm_clk_get(&pdev->dev, "onfi_d2");
  5528. BUG_ON(IS_ERR(onfi_d2_clock));
  5529. mtk_nand_regulator = devm_regulator_get(&pdev->dev, "vmch");
  5530. BUG_ON(IS_ERR(mtk_nand_regulator));
  5531. #endif
  5532. #if defined(CONFIG_MTK_LEGACY)
  5533. #ifdef CONFIG_MTK_PMIC_MT6397
  5534. hwPowerOn(MT65XX_POWER_LDO_VMCH, VOL_3300, "NFI");
  5535. #else
  5536. hwPowerOn(MT6323_POWER_LDO_VMCH, VOL_3300, "NFI");
  5537. #endif
  5538. #else
  5539. ret = regulator_set_voltage(mtk_nand_regulator, 3300000, 3300000);
  5540. if (ret != 0)
  5541. pr_err("regulator set vol failed: %d\n", ret);
  5542. ret = regulator_enable(mtk_nand_regulator);
  5543. if (ret != 0)
  5544. pr_err("regulator_enable failed: %d\n", ret);
  5545. #endif
  5546. #ifdef CONFIG_OF
  5547. hw = (struct mtk_nand_host_hw *)pdev->dev.platform_data;
  5548. hw = devm_kzalloc(&pdev->dev, sizeof(*hw), GFP_KERNEL);
  5549. BUG_ON(!hw);
  5550. hw->nfi_bus_width = 8;
  5551. hw->nfi_access_timing = 0x4333;
  5552. hw->nfi_cs_num = 2;
  5553. hw->nand_sec_size = 512;
  5554. hw->nand_sec_shift = 9;
  5555. hw->nand_ecc_size = 2048;
  5556. hw->nand_ecc_bytes = 32;
  5557. hw->nand_ecc_mode = 2;
  5558. #else
  5559. hw = (struct mtk_nand_host_hw *)pdev->dev.platform_data;
  5560. BUG_ON(!hw);
  5561. if (pdev->num_resources != 4 || res[0].flags != IORESOURCE_MEM
  5562. || res[1].flags != IORESOURCE_MEM || res[2].flags != IORESOURCE_IRQ
  5563. || res[3].flags != IORESOURCE_IRQ) {
  5564. pr_err("%s: invalid resource type\n", __func__);
  5565. return -ENODEV;
  5566. }
  5567. /* Request IO memory */
  5568. if (!request_mem_region(res[0].start, res[0].end - res[0].start + 1, pdev->name))
  5569. return -EBUSY;
  5570. if (!request_mem_region(res[1].start, res[1].end - res[1].start + 1, pdev->name))
  5571. return -EBUSY;
  5572. #endif
  5573. /* Allocate memory for the device structure (and zero it) */
  5574. host = kzalloc(sizeof(struct mtk_nand_host), GFP_KERNEL);
  5575. if (!host) {
  5576. /* pr_err("failed to allocate device structure.\n"); */
  5577. return -ENOMEM;
  5578. }
  5579. /* Allocate memory for 16 byte aligned buffer */
  5580. local_buffer_16_align = local_buffer;
  5581. temp_buffer_16_align = temp_buffer;
  5582. /* pr_debug(KERN_INFO "Allocate 16 byte aligned buffer: %p\n", local_buffer_16_align); */
  5583. host->hw = hw;
  5584. PL_TIME_PROG(10);
  5585. PL_TIME_ERASE(10);
  5586. PL_TIME_PROG_WDT_SET(1);
  5587. PL_TIME_ERASE_WDT_SET(1);
  5588. /* init mtd data structure */
  5589. nand_chip = &host->nand_chip;
  5590. nand_chip->priv = host; /* link the private data structures */
  5591. mtd = &host->mtd;
  5592. mtd->priv = nand_chip;
  5593. mtd->owner = THIS_MODULE;
  5594. mtd->name = "MTK-Nand";
  5595. mtd->eraseregions = host->erase_region;
  5596. hw->nand_ecc_mode = NAND_ECC_HW;
  5597. /* Set address of NAND IO lines */
  5598. nand_chip->IO_ADDR_R = (void __iomem *)NFI_DATAR_REG32;
  5599. nand_chip->IO_ADDR_W = (void __iomem *)NFI_DATAW_REG32;
  5600. nand_chip->chip_delay = 20; /* 20us command delay time */
  5601. nand_chip->ecc.mode = hw->nand_ecc_mode; /* enable ECC */
  5602. nand_chip->read_byte = mtk_nand_read_byte;
  5603. nand_chip->read_buf = mtk_nand_read_buf;
  5604. nand_chip->write_buf = mtk_nand_write_buf;
  5605. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  5606. nand_chip->verify_buf = mtk_nand_verify_buf;
  5607. #endif
  5608. nand_chip->select_chip = mtk_nand_select_chip;
  5609. nand_chip->dev_ready = mtk_nand_dev_ready;
  5610. nand_chip->cmdfunc = mtk_nand_command_bp;
  5611. nand_chip->ecc.read_page = mtk_nand_read_page_hwecc;
  5612. nand_chip->ecc.write_page = mtk_nand_write_page_hwecc;
  5613. nand_chip->ecc.layout = &nand_oob_64;
  5614. nand_chip->ecc.size = hw->nand_ecc_size; /* 2048 */
  5615. nand_chip->ecc.bytes = hw->nand_ecc_bytes; /* 32 */
  5616. nand_chip->options = NAND_SKIP_BBTSCAN;
  5617. /* For BMT, we need to revise driver architecture */
  5618. nand_chip->write_page = mtk_nand_write_page;
  5619. nand_chip->read_page = mtk_nand_read_page;
  5620. nand_chip->read_subpage = mtk_nand_read_subpage;
  5621. nand_chip->ecc.write_oob = mtk_nand_write_oob;
  5622. nand_chip->ecc.read_oob = mtk_nand_read_oob;
  5623. /* need to add nand_get_device()/nand_release_device(). */
  5624. nand_chip->block_markbad = mtk_nand_block_markbad;
  5625. nand_chip->erase_hw = mtk_nand_erase;
  5626. nand_chip->block_bad = mtk_nand_block_bad;
  5627. nand_chip->init_size = mtk_nand_init_size;
  5628. #if CFG_FPGA_PLATFORM
  5629. pr_debug("[FPGA Dummy]Enable NFI and NFIECC Clock\n");
  5630. #else
  5631. /* MSG(INIT, "[NAND]Enable NFI and NFIECC Clock\n"); */
  5632. nand_enable_clock();
  5633. #endif
  5634. #ifndef CONFIG_MTK_FPGA
  5635. /* mtk_nand_gpio_init(); */
  5636. #endif
  5637. mtk_nand_init_hw(host);
  5638. /* Select the device */
  5639. nand_chip->select_chip(mtd, NFI_DEFAULT_CS);
  5640. /*
  5641. * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
  5642. * after power-up
  5643. */
  5644. nand_chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  5645. /* Send the command for reading device ID */
  5646. nand_chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  5647. for (i = 0; i < NAND_MAX_ID; i++)
  5648. id[i] = nand_chip->read_byte(mtd);
  5649. manu_id = id[0];
  5650. dev_id = id[1];
  5651. if (!get_device_info(id, &devinfo))
  5652. pr_err("Not Support this Device! \r\n");
  5653. #if CFG_2CS_NAND
  5654. if (mtk_nand_cs_check(mtd, id, NFI_TRICKY_CS)) {
  5655. pr_info("Twins Nand\n");
  5656. g_bTricky_CS = TRUE;
  5657. g_b2Die_CS = TRUE;
  5658. }
  5659. #endif
  5660. if (devinfo.pagesize == 16384) {
  5661. nand_chip->ecc.layout = &nand_oob_128;
  5662. hw->nand_ecc_size = 16384;
  5663. } else if (devinfo.pagesize == 8192) {
  5664. nand_chip->ecc.layout = &nand_oob_128;
  5665. hw->nand_ecc_size = 8192;
  5666. } else if (devinfo.pagesize == 4096) {
  5667. nand_chip->ecc.layout = &nand_oob_128;
  5668. hw->nand_ecc_size = 4096;
  5669. } else if (devinfo.pagesize == 2048) {
  5670. nand_chip->ecc.layout = &nand_oob_64;
  5671. hw->nand_ecc_size = 2048;
  5672. } else if (devinfo.pagesize == 512) {
  5673. nand_chip->ecc.layout = &nand_oob_16;
  5674. hw->nand_ecc_size = 512;
  5675. }
  5676. if (devinfo.sectorsize == 1024) {
  5677. sector_size = 1024;
  5678. hw->nand_sec_shift = 10;
  5679. hw->nand_sec_size = 1024;
  5680. NFI_CLN_REG32(NFI_PAGEFMT_REG32, PAGEFMT_SECTOR_SEL);
  5681. }
  5682. if (devinfo.pagesize <= 4096) {
  5683. nand_chip->ecc.layout->eccbytes =
  5684. devinfo.sparesize - OOB_AVAI_PER_SECTOR * (devinfo.pagesize / sector_size);
  5685. hw->nand_ecc_bytes = nand_chip->ecc.layout->eccbytes;
  5686. /* Modify to fit device character */
  5687. nand_chip->ecc.size = hw->nand_ecc_size;
  5688. nand_chip->ecc.bytes = hw->nand_ecc_bytes;
  5689. } else {
  5690. /* devinfo.sparesize-OOB_AVAI_PER_SECTOR*(devinfo.pagesize/sector_size); */
  5691. nand_chip->ecc.layout->eccbytes = 64;
  5692. hw->nand_ecc_bytes = nand_chip->ecc.layout->eccbytes;
  5693. /* Modify to fit device character */
  5694. nand_chip->ecc.size = hw->nand_ecc_size;
  5695. nand_chip->ecc.bytes = hw->nand_ecc_bytes;
  5696. }
  5697. nand_chip->subpagesize = devinfo.sectorsize;
  5698. nand_chip->subpage_size = devinfo.sectorsize;
  5699. for (i = 0; i < nand_chip->ecc.layout->eccbytes; i++) {
  5700. nand_chip->ecc.layout->eccpos[i] =
  5701. OOB_AVAI_PER_SECTOR * (devinfo.pagesize / sector_size) + i;
  5702. }
  5703. /* MSG(INIT, "[NAND] pagesz:%d , oobsz: %d,eccbytes: %d\n", */
  5704. /* devinfo.pagesize, sizeof(g_kCMD.au1OOB),nand_chip->ecc.layout->eccbytes); */
  5705. /* MSG(INIT, "Support this Device in MTK table! %x \r\n", id); */
  5706. #if CFG_RANDOMIZER
  5707. if (devinfo.vendor != VEND_NONE) {
  5708. /* mtk_nand_randomizer_config(&devinfo.feature_set.randConfig); */
  5709. #if 0
  5710. if ((devinfo.feature_set.randConfig.type == RAND_TYPE_SAMSUNG) ||
  5711. (devinfo.feature_set.randConfig.type == RAND_TYPE_TOSHIBA)) {
  5712. MSG(INIT, "[NAND]USE Randomizer\n");
  5713. use_randomizer = TRUE;
  5714. } else {
  5715. MSG(INIT, "[NAND]OFF Randomizer\n");
  5716. use_randomizer = FALSE;
  5717. }
  5718. #endif /* only charge for efuse bonding */
  5719. #ifdef CONFIG_MTK_LEGACY
  5720. if ((*EFUSE_RANDOM_CFG) & EFUSE_RANDOM_ENABLE) {
  5721. #else
  5722. /* the index of reg:0x102061C0 is 26 */
  5723. if ((get_devinfo_with_index(26)) & EFUSE_RANDOM_ENABLE) {
  5724. #endif
  5725. pr_notice("EFUSE RANDOM CFG is ON\n");
  5726. use_randomizer = TRUE;
  5727. pre_randomizer = TRUE;
  5728. } else {
  5729. pr_notice("EFUSE RANDOM CFG is OFF\n");
  5730. use_randomizer = FALSE;
  5731. pre_randomizer = FALSE;
  5732. }
  5733. }
  5734. #endif
  5735. if ((devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX_16NM)
  5736. || (devinfo.feature_set.FeatureSet.rtype == RTYPE_HYNIX))
  5737. HYNIX_RR_TABLE_READ(&devinfo);
  5738. hw->nfi_bus_width = devinfo.iowidth;
  5739. #if 1
  5740. if (devinfo.vendor == VEND_MICRON) {
  5741. if (devinfo.feature_set.FeatureSet.Async_timing.feature != 0xFF) {
  5742. struct gFeatureSet *feature_set = &(devinfo.feature_set.FeatureSet);
  5743. /* u32 val = 0; */
  5744. mtk_nand_SetFeature(mtd, (u16) feature_set->sfeatureCmd,
  5745. feature_set->Async_timing.address,
  5746. (u8 *) (&feature_set->Async_timing.feature),
  5747. sizeof(feature_set->Async_timing.feature));
  5748. /* mtk_nand_GetFeature(mtd, feature_set->gfeatureCmd, \ */
  5749. /* feature_set->Async_timing.address, (u8 *)(&val),4); */
  5750. /* pr_debug("[ASYNC Interface]0x%X\n", val); */
  5751. #if CFG_2CS_NAND
  5752. if (g_bTricky_CS) {
  5753. DRV_WriteReg16(NFI_CSEL_REG16, NFI_TRICKY_CS);
  5754. mtk_nand_SetFeature(mtd, (u16) feature_set->sfeatureCmd,
  5755. feature_set->Async_timing.address,
  5756. (u8 *) (&feature_set->Async_timing.feature),
  5757. sizeof(feature_set->Async_timing.feature));
  5758. DRV_WriteReg16(NFI_CSEL_REG16, NFI_DEFAULT_CS);
  5759. }
  5760. #endif
  5761. }
  5762. }
  5763. #endif
  5764. /* MSG(INIT, "AHB Clock(0x%x) ",DRV_Reg32(PERICFG_BASE+0x5C)); */
  5765. /* DRV_WriteReg32(PERICFG_BASE+0x5C, 0x1); */
  5766. /* MSG(INIT, "AHB Clock(0x%x)",DRV_Reg32(PERICFG_BASE+0x5C)); */
  5767. DRV_WriteReg32(NFI_ACCCON_REG32, devinfo.timmingsetting);
  5768. /* MSG(INIT, "Kernel Nand Timing:0x%x!\n", DRV_Reg32(NFI_ACCCON_REG32)); */
  5769. /* 16-bit bus width */
  5770. if (hw->nfi_bus_width == 16) {
  5771. pr_notice("Set the 16-bit I/O settings!\n");
  5772. nand_chip->options |= NAND_BUSWIDTH_16;
  5773. }
  5774. mtk_dev = &pdev->dev;
  5775. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  5776. if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
  5777. dev_err(&pdev->dev, "set dma mask fail\n");
  5778. pr_err("set dma mask fail\n");
  5779. } else
  5780. pr_notice("set dma mask ok\n");
  5781. #ifdef CONFIG_OF
  5782. err = request_irq(MT_NFI_IRQ_ID, mtk_nand_irq_handler, IRQF_TRIGGER_NONE, "mtk-nand", NULL);
  5783. #else
  5784. err = request_irq(MT_NFI_IRQ_ID, mtk_nand_irq_handler, IRQF_DISABLED, "mtk-nand", NULL);
  5785. #endif
  5786. if (0 != err) {
  5787. pr_err("Request IRQ fail: err = %d\n", err);
  5788. goto out;
  5789. }
  5790. if (g_i4Interrupt)
  5791. enable_irq(MT_NFI_IRQ_ID);
  5792. else
  5793. disable_irq(MT_NFI_IRQ_ID);
  5794. #if 0
  5795. if (devinfo.advancedmode & CACHE_READ) {
  5796. nand_chip->ecc.read_multi_page_cache = NULL;
  5797. /* nand_chip->ecc.read_multi_page_cache = mtk_nand_read_multi_page_cache; */
  5798. /* MSG(INIT, "Device %x support cache read \r\n",id); */
  5799. } else
  5800. nand_chip->ecc.read_multi_page_cache = NULL;
  5801. #endif
  5802. mtd->oobsize = devinfo.sparesize;
  5803. /* Scan to find existence of the device */
  5804. if (nand_scan(mtd, hw->nfi_cs_num)) {
  5805. pr_err("nand_scan fail.\n");
  5806. err = -ENXIO;
  5807. goto out;
  5808. }
  5809. g_page_size = mtd->writesize;
  5810. g_block_size = devinfo.blocksize << 10;
  5811. PAGES_PER_BLOCK = (u32) (g_block_size / g_page_size);
  5812. /* MSG(INIT, "g_page_size(%d) g_block_size(%d)\n",g_page_size, g_block_size); */
  5813. #if CFG_2CS_NAND
  5814. g_nanddie_pages = (u32) (nand_chip->chipsize >> nand_chip->page_shift);
  5815. /* if(devinfo.ttarget == TTYPE_2DIE) */
  5816. /* { */
  5817. /* g_nanddie_pages = g_nanddie_pages / 2; */
  5818. /* } */
  5819. if (g_b2Die_CS) {
  5820. nand_chip->chipsize <<= 1;
  5821. /* MSG(INIT, "[Bean]%dMB\n", (u32)(nand_chip->chipsize/1024/1024)); */
  5822. }
  5823. /* MSG(INIT, "[Bean]g_nanddie_pages %x\n", g_nanddie_pages); */
  5824. #endif
  5825. #if CFG_COMBO_NAND
  5826. #ifdef PART_SIZE_BMTPOOL
  5827. if (PART_SIZE_BMTPOOL) {
  5828. bmt_sz = (PART_SIZE_BMTPOOL) >> nand_chip->phys_erase_shift;
  5829. } else
  5830. #endif
  5831. {
  5832. bmt_sz = (int)(((u32) (nand_chip->chipsize >> nand_chip->phys_erase_shift)) / 100 * 6);
  5833. }
  5834. /* if (manu_id == 0x45) */
  5835. /* { */
  5836. /* bmt_sz = bmt_sz * 2; */
  5837. /* } */
  5838. #endif
  5839. platform_set_drvdata(pdev, host);
  5840. if (hw->nfi_bus_width == 16)
  5841. NFI_SET_REG32(NFI_PAGEFMT_REG32, PAGEFMT_DBYTE_EN);
  5842. nand_chip->select_chip(mtd, 0);
  5843. #if defined(MTK_COMBO_NAND_SUPPORT)
  5844. #if CFG_COMBO_NAND
  5845. nand_chip->chipsize -= (bmt_sz * g_block_size);
  5846. #else
  5847. nand_chip->chipsize -= (PART_SIZE_BMTPOOL);
  5848. #endif
  5849. /* #if CFG_2CS_NAND */
  5850. /* if(g_b2Die_CS) */
  5851. /* { */
  5852. /* nand_chip->chipsize -= (PART_SIZE_BMTPOOL); // if 2CS nand need cut down again */
  5853. /* } */
  5854. /* #endif */
  5855. #else
  5856. nand_chip->chipsize -= (BMT_POOL_SIZE) << nand_chip->phys_erase_shift;
  5857. #endif
  5858. mtd->size = nand_chip->chipsize;
  5859. #if NAND_READ_PERFORMANCE
  5860. struct timeval stimer, etimer;
  5861. do_gettimeofday(&stimer);
  5862. for (i = 256; i < 512; i++) {
  5863. mtk_nand_read_page(mtd, nand_chip, local_buffer, i);
  5864. pr_debug("[%d]0x%x %x %x %x\n", i, *(int *)local_buffer,
  5865. *((int *)local_buffer + 1), *((int *)local_buffer + 2),
  5866. *((int *)local_buffer + 3));
  5867. pr_debug("[%d]0x%x %x %x %x\n", i, *(int *)local_buffer + 4,
  5868. *((int *)local_buffer + 5), *((int *)local_buffer + 6),
  5869. *((int *)local_buffer + 7));
  5870. pr_debug("[%d]0x%x %x %x %x\n", i, *(int *)local_buffer + 8,
  5871. *((int *)local_buffer + 9), *((int *)local_buffer + 10),
  5872. *((int *)local_buffer + 11));
  5873. pr_debug("[%d]0x%x %x %x %x\n", i, *(int *)local_buffer + 12,
  5874. *((int *)local_buffer + 13), *((int *)local_buffer + 14),
  5875. *((int *)local_buffer + 15));
  5876. }
  5877. do_gettimeofday(&etimer);
  5878. pr_debug("[NAND Read Perf.Test] %ld MB/s\n",
  5879. (g_page_size * 256) / Cal_timediff(&etimer, &stimer));
  5880. #endif
  5881. if (devinfo.vendor != VEND_NONE) {
  5882. err = mtk_nand_interface_config(mtd);
  5883. #if CFG_2CS_NAND
  5884. if (g_bTricky_CS) {
  5885. DRV_WriteReg16(NFI_CSEL_REG16, NFI_TRICKY_CS);
  5886. err = mtk_nand_interface_config(mtd);
  5887. DRV_WriteReg16(NFI_CSEL_REG16, NFI_DEFAULT_CS);
  5888. }
  5889. #endif
  5890. /* MSG(INIT, "[nand_interface_config] %d\n",err); */
  5891. /* u32 regp; */
  5892. /* for (regp = 0xF0206000; regp <= 0xF020631C; regp+=4) */
  5893. /* pr_debug("[%08X]0x%08X\n", regp, DRV_Reg32(regp)); */
  5894. #if NAND_READ_PERFORMANCE
  5895. do_gettimeofday(&stimer);
  5896. for (i = 256; i < 512; i++) {
  5897. mtk_nand_read_page(mtd, nand_chip, local_buffer, i);
  5898. pr_debug("[%d]0x%x %x %x %x\n", i, *(int *)local_buffer,
  5899. *((int *)local_buffer + 1), *((int *)local_buffer + 2),
  5900. *((int *)local_buffer + 3));
  5901. pr_debug("[%d]0x%x %x %x %x\n", i, *(int *)local_buffer + 4,
  5902. *((int *)local_buffer + 5), *((int *)local_buffer + 6),
  5903. *((int *)local_buffer + 7));
  5904. pr_debug("[%d]0x%x %x %x %x\n", i, *(int *)local_buffer + 8,
  5905. *((int *)local_buffer + 9), *((int *)local_buffer + 10),
  5906. *((int *)local_buffer + 11));
  5907. pr_debug("[%d]0x%x %x %x %x\n", i, *(int *)local_buffer + 12,
  5908. *((int *)local_buffer + 13), *((int *)local_buffer + 14),
  5909. *((int *)local_buffer + 15));
  5910. }
  5911. do_gettimeofday(&etimer);
  5912. pr_debug("[NAND Read Perf.Test] %d MB/s\n",
  5913. (g_page_size * 256) / Cal_timediff(&etimer, &stimer));
  5914. while (1)
  5915. ;
  5916. #endif
  5917. }
  5918. if (!g_bmt) {
  5919. #if defined(MTK_COMBO_NAND_SUPPORT)
  5920. #if CFG_COMBO_NAND
  5921. g_bmt = init_bmt(nand_chip, bmt_sz);
  5922. if (!g_bmt) {
  5923. #else
  5924. g_bmt = init_bmt(nand_chip, ((PART_SIZE_BMTPOOL) >> nand_chip->phys_erase_shift));
  5925. if (!g_bmt) {
  5926. #endif
  5927. #else
  5928. g_bmt = init_bmt(nand_chip, BMT_POOL_SIZE);
  5929. if (!g_bmt) {
  5930. #endif
  5931. pr_err("Error: init bmt failed\n");
  5932. return 0;
  5933. }
  5934. }
  5935. nand_chip->chipsize -= (PMT_POOL_SIZE) << nand_chip->phys_erase_shift;
  5936. mtd->size = nand_chip->chipsize;
  5937. #if KERNEL_NAND_UNIT_TEST
  5938. err = mtk_nand_unit_test(nand_chip, mtd);
  5939. if (err == 0)
  5940. pr_debug("Thanks to GOD, UNIT Test OK!\n");
  5941. #endif
  5942. #ifdef PMT
  5943. part_init_pmt(mtd, (u8 *) &g_exist_Partition[0]);
  5944. err = mtd_device_register(mtd, g_exist_Partition, part_num);
  5945. #else
  5946. err = mtd_device_register(mtd, g_pasStatic_Partition, part_num);
  5947. #endif
  5948. #ifdef _MTK_NAND_DUMMY_DRIVER_
  5949. dummy_driver_debug = 0;
  5950. #endif
  5951. /* Successfully!! */
  5952. if (!err) {
  5953. /* MSG(INIT, "[mtk_nand] probe successfully!\n"); */
  5954. nand_disable_clock();
  5955. return err;
  5956. }
  5957. /* Fail!! */
  5958. out:
  5959. pr_err("[NFI] mtk_nand_probe fail, err = %d!\n", err);
  5960. nand_release(mtd);
  5961. platform_set_drvdata(pdev, NULL);
  5962. kfree(host);
  5963. nand_disable_clock();
  5964. return err;
  5965. }
  5966. /******************************************************************************
  5967. * mtk_nand_suspend
  5968. *
  5969. * DESCRIPTION:
  5970. * Suspend the nand device!
  5971. *
  5972. * PARAMETERS:
  5973. * struct platform_device *pdev : device structure
  5974. *
  5975. * RETURNS:
  5976. * 0 : Success
  5977. *
  5978. * NOTES:
  5979. * None
  5980. *
  5981. ******************************************************************************/
  5982. static int mtk_nand_suspend(struct platform_device *pdev, pm_message_t state)
  5983. {
  5984. struct mtk_nand_host *host = platform_get_drvdata(pdev);
  5985. #if !defined(CONFIG_MTK_LEGACY)
  5986. int ret = 0;
  5987. #endif
  5988. /* struct mtd_info *mtd = &host->mtd; */
  5989. /* backup register */
  5990. #ifdef CONFIG_PM
  5991. if (host->saved_para.suspend_flag == 0) {
  5992. nand_enable_clock();
  5993. /* Save NFI register */
  5994. host->saved_para.sNFI_CNFG_REG16 = DRV_Reg16(NFI_CNFG_REG16);
  5995. host->saved_para.sNFI_PAGEFMT_REG32 = DRV_Reg32(NFI_PAGEFMT_REG32);
  5996. host->saved_para.sNFI_CON_REG16 = DRV_Reg32(NFI_CON_REG16);
  5997. host->saved_para.sNFI_ACCCON_REG32 = DRV_Reg32(NFI_ACCCON_REG32);
  5998. host->saved_para.sNFI_INTR_EN_REG16 = DRV_Reg16(NFI_INTR_EN_REG16);
  5999. host->saved_para.sNFI_IOCON_REG16 = DRV_Reg16(NFI_IOCON_REG16);
  6000. host->saved_para.sNFI_CSEL_REG16 = DRV_Reg16(NFI_CSEL_REG16);
  6001. host->saved_para.sNFI_DEBUG_CON1_REG16 = DRV_Reg16(NFI_DEBUG_CON1_REG16);
  6002. /* save ECC register */
  6003. host->saved_para.sECC_ENCCNFG_REG32 = DRV_Reg32(ECC_ENCCNFG_REG32);
  6004. /* host->saved_para.sECC_FDMADDR_REG32 = DRV_Reg32(ECC_FDMADDR_REG32); */
  6005. host->saved_para.sECC_DECCNFG_REG32 = DRV_Reg32(ECC_DECCNFG_REG32);
  6006. /* for sync mode */
  6007. if (g_bSyncOrToggle) {
  6008. host->saved_para.sNFI_DLYCTRL_REG32 = DRV_Reg32(NFI_DLYCTRL_REG32);
  6009. #ifndef CONFIG_MTK_FPGA
  6010. /* host->saved_para.sPERI_NFI_MAC_CTRL = DRV_Reg32(PERI_NFI_MAC_CTRL); */
  6011. #endif
  6012. host->saved_para.sNFI_NAND_TYPE_CNFG_REG32 =
  6013. DRV_Reg32(NFI_NAND_TYPE_CNFG_REG32);
  6014. host->saved_para.sNFI_ACCCON1_REG32 = DRV_Reg32(NFI_ACCCON1_REG3);
  6015. }
  6016. #ifdef CONFIG_MTK_PMIC_MT6397
  6017. hwPowerDown(MT65XX_POWER_LDO_VMCH, "NFI");
  6018. #else
  6019. #if defined(CONFIG_MTK_LEGACY)
  6020. hwPowerDown(MT6323_POWER_LDO_VMCH, "NFI");
  6021. #else
  6022. ret = regulator_disable(mtk_nand_regulator);
  6023. if (ret != 0)
  6024. pr_err("regulator disable failed: %d\n", ret);
  6025. #endif
  6026. #endif
  6027. nand_disable_clock();
  6028. host->saved_para.suspend_flag = 1;
  6029. } else {
  6030. pr_debug("[NFI] Suspend twice !\n");
  6031. }
  6032. #endif
  6033. pr_debug("[NFI] Suspend !\n");
  6034. return 0;
  6035. }
  6036. /******************************************************************************
  6037. * mtk_nand_resume
  6038. *
  6039. * DESCRIPTION:
  6040. * Resume the nand device!
  6041. *
  6042. * PARAMETERS:
  6043. * struct platform_device *pdev : device structure
  6044. *
  6045. * RETURNS:
  6046. * 0 : Success
  6047. *
  6048. * NOTES:
  6049. * None
  6050. *
  6051. ******************************************************************************/
  6052. static int mtk_nand_resume(struct platform_device *pdev)
  6053. {
  6054. struct mtk_nand_host *host = platform_get_drvdata(pdev);
  6055. #if !defined(CONFIG_MTK_LEGACY)
  6056. int ret = 0;
  6057. #endif
  6058. /* struct mtd_info *mtd = &host->mtd; //for test */
  6059. /* struct nand_chip *chip = mtd->priv; */
  6060. /* struct gFeatureSet *feature_set = &(devinfo.feature_set.FeatureSet); //for test */
  6061. /* int val = -1; // for test */
  6062. #ifdef CONFIG_PM
  6063. if (host->saved_para.suspend_flag == 1) {
  6064. /* restore NFI register */
  6065. #ifdef CONFIG_MTK_PMIC_MT6397
  6066. hwPowerOn(MT65XX_POWER_LDO_VMCH, VOL_3300, "NFI");
  6067. #else
  6068. #if defined(CONFIG_MTK_LEGACY)
  6069. hwPowerOn(MT6323_POWER_LDO_VMCH, VOL_3300, "NFI");
  6070. #else
  6071. ret = regulator_set_voltage(mtk_nand_regulator, 3300000, 3300000);
  6072. if (ret != 0)
  6073. pr_err("regulator set vol failed: %d\n", ret);
  6074. #endif
  6075. #endif
  6076. udelay(200);
  6077. pr_debug("[NFI] delay 200us for power on reset flow!\n");
  6078. nand_enable_clock();
  6079. DRV_WriteReg16(NFI_CNFG_REG16, host->saved_para.sNFI_CNFG_REG16);
  6080. DRV_WriteReg32(NFI_PAGEFMT_REG32, host->saved_para.sNFI_PAGEFMT_REG32);
  6081. DRV_WriteReg32(NFI_CON_REG16, host->saved_para.sNFI_CON_REG16);
  6082. DRV_WriteReg32(NFI_ACCCON_REG32, host->saved_para.sNFI_ACCCON_REG32);
  6083. DRV_WriteReg16(NFI_IOCON_REG16, host->saved_para.sNFI_IOCON_REG16);
  6084. DRV_WriteReg16(NFI_CSEL_REG16, host->saved_para.sNFI_CSEL_REG16);
  6085. DRV_WriteReg16(NFI_DEBUG_CON1_REG16, host->saved_para.sNFI_DEBUG_CON1_REG16);
  6086. /* restore ECC register */
  6087. DRV_WriteReg32(ECC_ENCCNFG_REG32, host->saved_para.sECC_ENCCNFG_REG32);
  6088. /* DRV_WriteReg32(ECC_FDMADDR_REG32 ,host->saved_para.sECC_FDMADDR_REG32); */
  6089. DRV_WriteReg32(ECC_DECCNFG_REG32, host->saved_para.sECC_DECCNFG_REG32);
  6090. /* Reset NFI and ECC state machine */
  6091. /* Reset the state machine and data FIFO, because flushing FIFO */
  6092. (void)mtk_nand_reset();
  6093. /* Reset ECC */
  6094. DRV_WriteReg16(ECC_DECCON_REG16, DEC_DE);
  6095. while (!DRV_Reg16(ECC_DECIDLE_REG16))
  6096. ;
  6097. DRV_WriteReg16(ECC_ENCCON_REG16, ENC_DE);
  6098. while (!DRV_Reg32(ECC_ENCIDLE_REG32))
  6099. ;
  6100. /* Initialize interrupt. Clear interrupt, read clear. */
  6101. DRV_Reg16(NFI_INTR_REG16);
  6102. DRV_WriteReg16(NFI_INTR_EN_REG16, host->saved_para.sNFI_INTR_EN_REG16);
  6103. /* mtk_nand_interface_config(&host->mtd); */
  6104. if (g_bSyncOrToggle) {
  6105. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16, HWDCM_SWCON_ON);
  6106. NFI_CLN_REG32(NFI_DEBUG_CON1_REG16, NFI_BYPASS);
  6107. NFI_CLN_REG32(ECC_BYPASS_REG32, ECC_BYPASS);
  6108. #ifndef CONFIG_MTK_FPGA
  6109. #if defined(CONFIG_MTK_LEGACY)
  6110. /* DRV_WriteReg32(PERICFG_BASE+0x5C, 0x0); */
  6111. NFI_SET_REG32(PERI_NFI_CLK_SOURCE_SEL, NFI_PAD_1X_CLOCK);
  6112. #else
  6113. clk_set_parent(infra_nfi_sel_clock, onfi_d2_clock);
  6114. #endif
  6115. #if defined(CONFIG_MTK_LEGACY)
  6116. clkmux_sel(MT_MUX_ONFI, g_iNFI2X_CLKSRC, "NFI");
  6117. #else
  6118. if (g_iNFI2X_CLKSRC == 0)
  6119. clk_set_parent(onfi_sel_clock, onfi_26m_clock);
  6120. else if (g_iNFI2X_CLKSRC == 1)
  6121. clk_set_parent(onfi_sel_clock, syspll2_d2_clock);
  6122. else if (g_iNFI2X_CLKSRC == 2)
  6123. clk_set_parent(onfi_sel_clock, syspll_d7_clock);
  6124. #endif
  6125. #endif
  6126. DRV_WriteReg32(NFI_DLYCTRL_REG32, host->saved_para.sNFI_DLYCTRL_REG32);
  6127. #ifndef CONFIG_MTK_FPGA
  6128. /* DRV_WriteReg32(PERI_NFI_MAC_CTRL, host->saved_para.sPERI_NFI_MAC_CTRL); */
  6129. #endif
  6130. while (0 == (DRV_Reg32(NFI_STA_REG32) && STA_FLASH_MACRO_IDLE))
  6131. ;
  6132. DRV_WriteReg16(NFI_NAND_TYPE_CNFG_REG32,
  6133. host->saved_para.sNFI_NAND_TYPE_CNFG_REG32);
  6134. DRV_WriteReg32(NFI_ACCCON1_REG3, host->saved_para.sNFI_ACCCON1_REG32);
  6135. }
  6136. /* mtk_nand_GetFeature(mtd, feature_set->gfeatureCmd, \ */
  6137. /* feature_set->Interface.address, (u8 *)&val,4); */
  6138. /* MSG(POWERCTL, "[NFI] Resume feature %d!\n", val); */
  6139. mtk_nand_device_reset();
  6140. nand_disable_clock();
  6141. host->saved_para.suspend_flag = 0;
  6142. } else {
  6143. pr_debug("[NFI] Resume twice !\n");
  6144. }
  6145. #endif
  6146. pr_debug("[NFI] Resume !\n");
  6147. return 0;
  6148. }
  6149. /******************************************************************************
  6150. * mtk_nand_remove
  6151. *
  6152. * DESCRIPTION:
  6153. * unregister the nand device file operations !
  6154. *
  6155. * PARAMETERS:
  6156. * struct platform_device *pdev : device structure
  6157. *
  6158. * RETURNS:
  6159. * 0 : Success
  6160. *
  6161. * NOTES:
  6162. * None
  6163. *
  6164. ******************************************************************************/
  6165. static int mtk_nand_remove(struct platform_device *pdev)
  6166. {
  6167. struct mtk_nand_host *host = platform_get_drvdata(pdev);
  6168. struct mtd_info *mtd = &host->mtd;
  6169. nand_release(mtd);
  6170. kfree(host);
  6171. nand_disable_clock();
  6172. return 0;
  6173. }
  6174. /******************************************************************************
  6175. * NAND OTP operations
  6176. * ***************************************************************************/
  6177. #if (defined(NAND_OTP_SUPPORT) && SAMSUNG_OTP_SUPPORT)
  6178. unsigned int samsung_OTPQueryLength(unsigned int *QLength)
  6179. {
  6180. *QLength = SAMSUNG_OTP_PAGE_NUM * g_page_size;
  6181. return 0;
  6182. }
  6183. unsigned int samsung_OTPRead(unsigned int PageAddr, void *BufferPtr, void *SparePtr)
  6184. {
  6185. struct mtd_info *mtd = &host->mtd;
  6186. unsigned int rowaddr, coladdr;
  6187. unsigned int u4Size = g_page_size;
  6188. unsigned int timeout = 0xFFFF;
  6189. unsigned int bRet;
  6190. unsigned int sec_num = mtd->writesize >> host->hw->nand_sec_shift;
  6191. if (PageAddr >= SAMSUNG_OTP_PAGE_NUM)
  6192. return OTP_ERROR_OVERSCOPE;
  6193. /* Col -> Row; LSB first */
  6194. coladdr = 0x00000000;
  6195. rowaddr = Samsung_OTP_Page[PageAddr];
  6196. pr_debug("[%s]:(COLADDR) [0x%08x]/(ROWADDR)[0x%08x]\n", __func__, coladdr, rowaddr);
  6197. /* Power on NFI HW component. */
  6198. nand_get_device(mtd, FL_READING);
  6199. mtk_nand_reset();
  6200. (void)mtk_nand_set_command(0x30);
  6201. mtk_nand_reset();
  6202. (void)mtk_nand_set_command(0x65);
  6203. pr_debug("[%s]: Start to read data from OTP area\n", __func__);
  6204. if (!mtk_nand_reset()) {
  6205. bRet = OTP_ERROR_RESET;
  6206. goto cleanup;
  6207. }
  6208. mtk_nand_set_mode(CNFG_OP_READ);
  6209. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  6210. DRV_WriteReg32(NFI_CON_REG16, sec_num << CON_NFI_SEC_SHIFT);
  6211. DRV_WriteReg32(NFI_STRADDR_REG32, __virt_to_phys(BufferPtr));
  6212. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AHB);
  6213. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  6214. if (g_bHwEcc)
  6215. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  6216. else
  6217. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  6218. mtk_nand_set_autoformat(true);
  6219. if (g_bHwEcc)
  6220. ECC_Decode_Start();
  6221. if (!mtk_nand_set_command(NAND_CMD_READ0)) {
  6222. bRet = OTP_ERROR_BUSY;
  6223. goto cleanup;
  6224. }
  6225. if (!mtk_nand_set_address(coladdr, rowaddr, 2, 3)) {
  6226. bRet = OTP_ERROR_BUSY;
  6227. goto cleanup;
  6228. }
  6229. if (!mtk_nand_set_command(NAND_CMD_READSTART)) {
  6230. bRet = OTP_ERROR_BUSY;
  6231. goto cleanup;
  6232. }
  6233. if (!mtk_nand_status_ready(STA_NAND_BUSY)) {
  6234. bRet = OTP_ERROR_BUSY;
  6235. goto cleanup;
  6236. }
  6237. if (!mtk_nand_read_page_data(mtd, BufferPtr, u4Size)) {
  6238. bRet = OTP_ERROR_BUSY;
  6239. goto cleanup;
  6240. }
  6241. if (!mtk_nand_status_ready(STA_NAND_BUSY)) {
  6242. bRet = OTP_ERROR_BUSY;
  6243. goto cleanup;
  6244. }
  6245. mtk_nand_read_fdm_data(SparePtr, sec_num);
  6246. mtk_nand_stop_read();
  6247. pr_debug("[%s]: End to read data from OTP area\n", __func__);
  6248. bRet = OTP_SUCCESS;
  6249. cleanup:
  6250. mtk_nand_reset();
  6251. (void)mtk_nand_set_command(0xFF);
  6252. nand_release_device(mtd);
  6253. return bRet;
  6254. }
  6255. unsigned int samsung_OTPWrite(unsigned int PageAddr, void *BufferPtr, void *SparePtr)
  6256. {
  6257. struct mtd_info *mtd = &host->mtd;
  6258. unsigned int rowaddr, coladdr;
  6259. unsigned int u4Size = g_page_size;
  6260. unsigned int timeout = 0xFFFF;
  6261. unsigned int bRet;
  6262. unsigned int sec_num = mtd->writesize >> 9;
  6263. if (PageAddr >= SAMSUNG_OTP_PAGE_NUM)
  6264. return OTP_ERROR_OVERSCOPE;
  6265. /* Col -> Row; LSB first */
  6266. coladdr = 0x00000000;
  6267. rowaddr = Samsung_OTP_Page[PageAddr];
  6268. pr_debug("[%s]:(COLADDR) [0x%08x]/(ROWADDR)[0x%08x]\n", __func__, coladdr, rowaddr);
  6269. nand_get_device(mtd, FL_READING);
  6270. mtk_nand_reset();
  6271. (void)mtk_nand_set_command(0x30);
  6272. mtk_nand_reset();
  6273. (void)mtk_nand_set_command(0x65);
  6274. pr_debug("[%s]: Start to write data to OTP area\n", __func__);
  6275. if (!mtk_nand_reset()) {
  6276. bRet = OTP_ERROR_RESET;
  6277. goto cleanup;
  6278. }
  6279. mtk_nand_set_mode(CNFG_OP_PRGM);
  6280. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_READ_EN);
  6281. DRV_WriteReg32(NFI_CON_REG16, sec_num << CON_NFI_SEC_SHIFT);
  6282. DRV_WriteReg32(NFI_STRADDR_REG32, __virt_to_phys(BufferPtr));
  6283. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_AHB);
  6284. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_DMA_BURST_EN);
  6285. if (g_bHwEcc)
  6286. NFI_SET_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  6287. else
  6288. NFI_CLN_REG16(NFI_CNFG_REG16, CNFG_HW_ECC_EN);
  6289. mtk_nand_set_autoformat(true);
  6290. ECC_Encode_Start();
  6291. if (!mtk_nand_set_command(NAND_CMD_SEQIN)) {
  6292. bRet = OTP_ERROR_BUSY;
  6293. goto cleanup;
  6294. }
  6295. if (!mtk_nand_set_address(coladdr, rowaddr, 2, 3)) {
  6296. bRet = OTP_ERROR_BUSY;
  6297. goto cleanup;
  6298. }
  6299. if (!mtk_nand_status_ready(STA_NAND_BUSY)) {
  6300. bRet = OTP_ERROR_BUSY;
  6301. goto cleanup;
  6302. }
  6303. mtk_nand_write_fdm_data((struct nand_chip *)mtd->priv, BufferPtr, sec_num);
  6304. (void)mtk_nand_write_page_data(mtd, BufferPtr, u4Size);
  6305. if (!mtk_nand_check_RW_count(u4Size)) {
  6306. pr_debug("[%s]: Check RW count timeout !\n", __func__);
  6307. bRet = OTP_ERROR_TIMEOUT;
  6308. goto cleanup;
  6309. }
  6310. mtk_nand_stop_write();
  6311. (void)mtk_nand_set_command(NAND_CMD_PAGEPROG);
  6312. while (DRV_Reg32(NFI_STA_REG32) & STA_NAND_BUSY)
  6313. ;
  6314. bRet = OTP_SUCCESS;
  6315. pr_debug("[%s]: End to write data to OTP area\n", __func__);
  6316. cleanup:
  6317. mtk_nand_reset();
  6318. (void)mtk_nand_set_command(NAND_CMD_RESET);
  6319. nand_release_device(mtd);
  6320. return bRet;
  6321. }
  6322. static int mt_otp_open(struct inode *inode, struct file *filp)
  6323. {
  6324. pr_debug("[%s]:(MAJOR)%d:(MINOR)%d\n", __func__, MAJOR(inode->i_rdev),
  6325. MINOR(inode->i_rdev));
  6326. filp->private_data = (int *)OTP_MAGIC_NUM;
  6327. return 0;
  6328. }
  6329. static int mt_otp_release(struct inode *inode, struct file *filp)
  6330. {
  6331. pr_debug("[%s]:(MAJOR)%d:(MINOR)%d\n", __func__, MAJOR(inode->i_rdev),
  6332. MINOR(inode->i_rdev));
  6333. return 0;
  6334. }
  6335. static int mt_otp_access(unsigned int access_type, unsigned int offset, void *buff_ptr,
  6336. unsigned int length, unsigned int *status)
  6337. {
  6338. unsigned int i = 0, ret = 0;
  6339. char *BufAddr = (char *)buff_ptr;
  6340. unsigned int PageAddr, AccessLength = 0;
  6341. int Status = 0;
  6342. static char *p_D_Buff;
  6343. char S_Buff[64];
  6344. p_D_Buff = kmalloc(g_page_size, GFP_KERNEL);
  6345. if (!p_D_Buff) {
  6346. ret = -ENOMEM;
  6347. *status = OTP_ERROR_NOMEM;
  6348. goto exit;
  6349. }
  6350. pr_debug("[%s]: %s (0x%x) length:(%d bytes) !\n", __func__, access_type ? "WRITE" : "READ",
  6351. offset, length);
  6352. while (1) {
  6353. PageAddr = offset / g_page_size;
  6354. if (FS_OTP_READ == access_type) {
  6355. memset(p_D_Buff, 0xff, g_page_size);
  6356. memset(S_Buff, 0xff, (sizeof(char) * 64));
  6357. pr_debug("[%s]: Read Access of page (%d)\n", __func__, PageAddr);
  6358. Status = g_mtk_otp_fuc.OTPRead(PageAddr, p_D_Buff, &S_Buff);
  6359. *status = Status;
  6360. if (OTP_SUCCESS != Status) {
  6361. pr_debug("[%s]: Read status (%d)\n", __func__, Status);
  6362. break;
  6363. }
  6364. AccessLength = g_page_size - (offset % g_page_size);
  6365. if (length >= AccessLength) {
  6366. memcpy(BufAddr, (p_D_Buff + (offset % g_page_size)), AccessLength);
  6367. } else {
  6368. /* last time */
  6369. memcpy(BufAddr, (p_D_Buff + (offset % g_page_size)), length);
  6370. }
  6371. } else if (FS_OTP_WRITE == access_type) {
  6372. AccessLength = g_page_size - (offset % g_page_size);
  6373. memset(p_D_Buff, 0xff, g_page_size);
  6374. memset(S_Buff, 0xff, (sizeof(char) * 64));
  6375. if (length >= AccessLength) {
  6376. memcpy((p_D_Buff + (offset % g_page_size)), BufAddr, AccessLength);
  6377. } else {
  6378. /* last time */
  6379. memcpy((p_D_Buff + (offset % g_page_size)), BufAddr, length);
  6380. }
  6381. Status = g_mtk_otp_fuc.OTPWrite(PageAddr, p_D_Buff, &S_Buff);
  6382. *status = Status;
  6383. if (OTP_SUCCESS != Status) {
  6384. pr_debug("[%s]: Write status (%d)\n", __func__, Status);
  6385. break;
  6386. }
  6387. } else {
  6388. pr_err("[%s]: Error, not either read nor write operations !\n", __func__);
  6389. break;
  6390. }
  6391. offset += AccessLength;
  6392. BufAddr += AccessLength;
  6393. if (length <= AccessLength) {
  6394. length = 0;
  6395. break;
  6396. }
  6397. length -= AccessLength;
  6398. pr_debug("[%s]: Remaining %s (%d) !\n", __func__,
  6399. access_type ? "WRITE" : "READ", length);
  6400. }
  6401. error:
  6402. kfree(p_D_Buff);
  6403. exit:
  6404. return ret;
  6405. }
  6406. static long mt_otp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  6407. {
  6408. int ret = 0, i = 0;
  6409. static char *pbuf;
  6410. void __user *uarg = (void __user *)arg;
  6411. struct otp_ctl otpctl;
  6412. /* Lock */
  6413. spin_lock(&g_OTPLock);
  6414. if (copy_from_user(&otpctl, uarg, sizeof(struct otp_ctl))) {
  6415. ret = -EFAULT;
  6416. goto exit;
  6417. }
  6418. if (false == g_bInitDone) {
  6419. pr_err("ERROR: NAND Flash Not initialized !!\n");
  6420. ret = -EFAULT;
  6421. goto exit;
  6422. }
  6423. pbuf = kmalloc_array(otpctl.Length, sizeof(char), GFP_KERNEL);
  6424. if (!pbuf) {
  6425. ret = -ENOMEM;
  6426. goto exit;
  6427. }
  6428. switch (cmd) {
  6429. case OTP_GET_LENGTH:
  6430. pr_debug("OTP IOCTL: OTP_GET_LENGTH\n");
  6431. g_mtk_otp_fuc.OTPQueryLength(&otpctl.QLength);
  6432. otpctl.status = OTP_SUCCESS;
  6433. pr_debug("OTP IOCTL: The Length is %d\n", otpctl.QLength);
  6434. break;
  6435. case OTP_READ:
  6436. pr_debug("OTP IOCTL: OTP_READ Offset(0x%x), Length(0x%x)\n", otpctl.Offset,
  6437. otpctl.Length);
  6438. memset(pbuf, 0xff, sizeof(char) * otpctl.Length);
  6439. mt_otp_access(FS_OTP_READ, otpctl.Offset, pbuf, otpctl.Length, &otpctl.status);
  6440. if (copy_to_user(otpctl.BufferPtr, pbuf, (sizeof(char) * otpctl.Length))) {
  6441. pr_err("OTP IOCTL: Copy to user buffer Error !\n");
  6442. goto error;
  6443. }
  6444. break;
  6445. case OTP_WRITE:
  6446. pr_debug("OTP IOCTL: OTP_WRITE Offset(0x%x), Length(0x%x)\n", otpctl.Offset,
  6447. otpctl.Length);
  6448. if (copy_from_user(pbuf, otpctl.BufferPtr, (sizeof(char) * otpctl.Length))) {
  6449. pr_err("OTP IOCTL: Copy from user buffer Error !\n");
  6450. goto error;
  6451. }
  6452. mt_otp_access(FS_OTP_WRITE, otpctl.Offset, pbuf, otpctl.Length, &otpctl.status);
  6453. break;
  6454. default:
  6455. ret = -EINVAL;
  6456. }
  6457. ret = copy_to_user(uarg, &otpctl, sizeof(struct otp_ctl));
  6458. error:
  6459. kfree(pbuf);
  6460. exit:
  6461. spin_unlock(&g_OTPLock);
  6462. return ret;
  6463. }
  6464. static const struct file_operations nand_otp_fops = {
  6465. .owner = THIS_MODULE,
  6466. .unlocked_ioctl = mt_otp_ioctl,
  6467. .open = mt_otp_open,
  6468. .release = mt_otp_release,
  6469. };
  6470. static struct miscdevice nand_otp_dev = {
  6471. .minor = MISC_DYNAMIC_MINOR,
  6472. .name = "otp",
  6473. .fops = &nand_otp_fops,
  6474. };
  6475. #endif
  6476. /******************************************************************************
  6477. Device driver structure
  6478. ******************************************************************************/
  6479. #ifdef CONFIG_OF
  6480. static const struct of_device_id mtk_nand_of_ids[] = {
  6481. {.compatible = "mediatek,mt8163-nfi",},
  6482. {}
  6483. };
  6484. #endif
  6485. static struct platform_driver mtk_nand_driver = {
  6486. .probe = mtk_nand_probe,
  6487. .remove = mtk_nand_remove,
  6488. .suspend = mtk_nand_suspend,
  6489. .resume = mtk_nand_resume,
  6490. .driver = {
  6491. .name = "mtk-nand",
  6492. .owner = THIS_MODULE,
  6493. #ifdef CONFIG_OF
  6494. .of_match_table = mtk_nand_of_ids,
  6495. #endif
  6496. },
  6497. };
  6498. /******************************************************************************
  6499. * mtk_nand_init
  6500. *
  6501. * DESCRIPTION:
  6502. * Init the device driver !
  6503. *
  6504. * PARAMETERS:
  6505. * None
  6506. *
  6507. * RETURNS:
  6508. * None
  6509. *
  6510. * NOTES:
  6511. * None
  6512. *
  6513. ******************************************************************************/
  6514. #define SEQ_printf(m, x...) \
  6515. do { \
  6516. if (m) \
  6517. seq_printf(m, x); \
  6518. else \
  6519. pr_debug(x); \
  6520. } while (0)
  6521. int mtk_nand_proc_show(struct seq_file *m, void *v)
  6522. {
  6523. int i;
  6524. SEQ_printf(m, "ID:");
  6525. for (i = 0; i < devinfo.id_length; i++)
  6526. SEQ_printf(m, " 0x%x", devinfo.id[i]);
  6527. SEQ_printf(m, "\n");
  6528. SEQ_printf(m, "total size: %dMiB; part number: %s\n", devinfo.totalsize,
  6529. devinfo.devciename);
  6530. SEQ_printf(m, "Current working in %s mode\n", g_i4Interrupt ? "interrupt" : "polling");
  6531. SEQ_printf(m, "NFI_ACCON=0x%x\n", DRV_Reg32(NFI_ACCCON_REG32));
  6532. SEQ_printf(m, "NFI_NAND_TYPE_CNFG_REG32= 0x%x\n", DRV_Reg32(NFI_NAND_TYPE_CNFG_REG32));
  6533. #ifdef CONFIG_MTK_FPGA
  6534. SEQ_printf(m, "[FPGA Dummy]DRV_CFG_NFIA(0x0)=0x0\n");
  6535. SEQ_printf(m, "[FPGA Dummy]DRV_CFG_NFIB(0x0)=0x0\n");
  6536. #else
  6537. SEQ_printf(m, "DRV_CFG_NFIA=0x%x\n", *((volatile u32 *)(GPIO_BASE + 0xC20)));
  6538. SEQ_printf(m, "DRV_CFG_NFIB=0x%x\n", *((volatile u32 *)(GPIO_BASE + 0xB50)));
  6539. #endif
  6540. #if CFG_PERFLOG_DEBUG
  6541. SEQ_printf(m, "Read Page Count:%d, Read Page totalTime:%lu, Avg. RPage:%lu\r\n",
  6542. g_NandPerfLog.ReadPageCount, g_NandPerfLog.ReadPageTotalTime,
  6543. g_NandPerfLog.ReadPageCount ? (g_NandPerfLog.ReadPageTotalTime /
  6544. g_NandPerfLog.ReadPageCount) : 0);
  6545. SEQ_printf(m, "Read subPage Count:%d, Read subPage totalTime:%lu, Avg. RPage:%lu\r\n",
  6546. g_NandPerfLog.ReadSubPageCount, g_NandPerfLog.ReadSubPageTotalTime,
  6547. g_NandPerfLog.ReadSubPageCount ? (g_NandPerfLog.ReadSubPageTotalTime /
  6548. g_NandPerfLog.ReadSubPageCount) : 0);
  6549. SEQ_printf(m, "Read Busy Count:%d, Read Busy totalTime:%lu, Avg. R Busy:%lu\r\n",
  6550. g_NandPerfLog.ReadBusyCount, g_NandPerfLog.ReadBusyTotalTime,
  6551. g_NandPerfLog.ReadBusyCount ? (g_NandPerfLog.ReadBusyTotalTime /
  6552. g_NandPerfLog.ReadBusyCount) : 0);
  6553. SEQ_printf(m, "Read DMA Count:%d, Read DMA totalTime:%lu, Avg. R DMA:%lu\r\n",
  6554. g_NandPerfLog.ReadDMACount, g_NandPerfLog.ReadDMATotalTime,
  6555. g_NandPerfLog.ReadDMACount ? (g_NandPerfLog.ReadDMATotalTime /
  6556. g_NandPerfLog.ReadDMACount) : 0);
  6557. SEQ_printf(m, "Write Page Count:%d, Write Page totalTime:%lu, Avg. WPage:%lu\r\n",
  6558. g_NandPerfLog.WritePageCount, g_NandPerfLog.WritePageTotalTime,
  6559. g_NandPerfLog.WritePageCount ? (g_NandPerfLog.WritePageTotalTime /
  6560. g_NandPerfLog.WritePageCount) : 0);
  6561. SEQ_printf(m, "Write Busy Count:%d, Write Busy totalTime:%lu, Avg. W Busy:%lu\r\n",
  6562. g_NandPerfLog.WriteBusyCount, g_NandPerfLog.WriteBusyTotalTime,
  6563. g_NandPerfLog.WriteBusyCount ? (g_NandPerfLog.WriteBusyTotalTime /
  6564. g_NandPerfLog.WriteBusyCount) : 0);
  6565. SEQ_printf(m, "Write DMA Count:%d, Write DMA totalTime:%lu, Avg. W DMA:%lu\r\n",
  6566. g_NandPerfLog.WriteDMACount, g_NandPerfLog.WriteDMATotalTime,
  6567. g_NandPerfLog.WriteDMACount ? (g_NandPerfLog.WriteDMATotalTime /
  6568. g_NandPerfLog.WriteDMACount) : 0);
  6569. SEQ_printf(m, "EraseBlock Count:%d, EraseBlock totalTime:%lu, Avg. Erase:%lu\r\n",
  6570. g_NandPerfLog.EraseBlockCount, g_NandPerfLog.EraseBlockTotalTime,
  6571. g_NandPerfLog.EraseBlockCount ? (g_NandPerfLog.EraseBlockTotalTime /
  6572. g_NandPerfLog.EraseBlockCount) : 0);
  6573. #endif
  6574. return 0;
  6575. }
  6576. static int mt_nand_proc_open(struct inode *inode, struct file *file)
  6577. {
  6578. return single_open(file, mtk_nand_proc_show, inode->i_private);
  6579. }
  6580. static const struct file_operations mtk_nand_fops = {
  6581. .open = mt_nand_proc_open,
  6582. .write = mtk_nand_proc_write,
  6583. .read = seq_read,
  6584. .llseek = seq_lseek,
  6585. .release = single_release,
  6586. };
  6587. static int __init mtk_nand_init(void)
  6588. {
  6589. struct proc_dir_entry *entry;
  6590. g_i4Interrupt = 0;
  6591. #if defined(NAND_OTP_SUPPORT)
  6592. int err = 0;
  6593. pr_debug("OTP: register NAND OTP device ...\n");
  6594. err = misc_register(&nand_otp_dev);
  6595. if (unlikely(err)) {
  6596. pr_err("OTP: failed to register NAND OTP device!\n");
  6597. return err;
  6598. }
  6599. spin_lock_init(&g_OTPLock);
  6600. #endif
  6601. #if (defined(NAND_OTP_SUPPORT) && SAMSUNG_OTP_SUPPORT)
  6602. g_mtk_otp_fuc.OTPQueryLength = samsung_OTPQueryLength;
  6603. g_mtk_otp_fuc.OTPRead = samsung_OTPRead;
  6604. g_mtk_otp_fuc.OTPWrite = samsung_OTPWrite;
  6605. #endif
  6606. entry = proc_create(PROCNAME, 0664, NULL, &mtk_nand_fops);
  6607. #if 0 /* removed in kernel 3.10 */
  6608. entry = create_proc_entry(PROCNAME, 0664, NULL);
  6609. if (entry == NULL) {
  6610. MSG(INIT, "MTK Nand : unable to create /proc entry\n");
  6611. return -ENOMEM;
  6612. }
  6613. entry->read_proc = mtk_nand_proc_read;
  6614. entry->write_proc = mtk_nand_proc_write;
  6615. #endif
  6616. /* pr_debug("MediaTek Nand driver init, version %s\n", VERSION); */
  6617. return platform_driver_register(&mtk_nand_driver);
  6618. }
  6619. /******************************************************************************
  6620. * mtk_nand_exit
  6621. *
  6622. * DESCRIPTION:
  6623. * Free the device driver !
  6624. *
  6625. * PARAMETERS:
  6626. * None
  6627. *
  6628. * RETURNS:
  6629. * None
  6630. *
  6631. * NOTES:
  6632. * None
  6633. *
  6634. ******************************************************************************/
  6635. static void __exit mtk_nand_exit(void)
  6636. {
  6637. pr_debug("MediaTek Nand driver exit, version %s\n", VERSION);
  6638. #if defined(NAND_OTP_SUPPORT)
  6639. misc_deregister(&nand_otp_dev);
  6640. #endif
  6641. #ifdef SAMSUNG_OTP_SUPPORT
  6642. g_mtk_otp_fuc.OTPQueryLength = NULL;
  6643. g_mtk_otp_fuc.OTPRead = NULL;
  6644. g_mtk_otp_fuc.OTPWrite = NULL;
  6645. #endif
  6646. platform_driver_unregister(&mtk_nand_driver);
  6647. remove_proc_entry(PROCNAME, NULL);
  6648. }
  6649. late_initcall(mtk_nand_init);
  6650. module_exit(mtk_nand_exit);
  6651. MODULE_LICENSE("GPL");