007-mtd.patch 136 KB

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