aml_v3_nand.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007
  1. // linux/drivers/amlogic/nand/aml_nand.c
  2. //#define CONFIG_AM_NAND_RBPIN 0
  3. #include <linux/module.h>
  4. #include <linux/types.h>
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/string.h>
  8. #include <linux/ioport.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/bitops.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/nand.h>
  17. #include <linux/mtd/nand_ecc.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <linux/dma-mapping.h>
  20. #include <asm/cache.h>
  21. #include <asm/cacheflush.h>
  22. #include <mach/nand_m3.h>
  23. #define NAND_DEBUG
  24. #ifdef NAND_DEBUG
  25. #define aml_nand_debug(a...) {printk("%s()[%s,%d]",__func__,__FILE__,__LINE__); printk(a);}
  26. #define aml_nand_debug2(a...) //{printk(a);}
  27. #else
  28. #define aml_nand_debug(a...)
  29. #define aml_nand_debug2(a...)
  30. #endif
  31. #define BUG_printk(a...) {printk(a);BUG();}
  32. static char *aml_nand_plane_string[]={
  33. "NAND_SINGLE_PLANE_MODE",
  34. "NAND_TWO_PLANE_MODE",
  35. };
  36. static char *aml_nand_internal_string[]={
  37. "NAND_NONE_INTERLEAVING_MODE",
  38. "NAND_INTERLEAVING_MODE",
  39. };
  40. static struct nand_ecclayout aml_nand_oob_64 = {
  41. .eccbytes = 60,
  42. .eccpos = {
  43. 4, 5, 6, 7, 8, 9, 10, 11,
  44. 12, 13, 14, 15, 16, 17, 18, 19,
  45. 20, 21, 22, 23, 24, 25, 26, 27,
  46. 28, 29, 30, 31, 32, 33, 34, 35,
  47. 36, 37, 38, 39, 40, 41, 42, 43,
  48. 44, 45, 46, 47, 48, 49, 50, 51,
  49. 52, 53, 54, 55, 56, 57, 58, 59,
  50. 60, 61, 62, 63},
  51. .oobfree = {
  52. {.offset = 0,
  53. .length = 4}}
  54. };
  55. static struct nand_ecclayout aml_nand_uboot_oob = {
  56. .eccbytes = 84,
  57. .oobfree = {
  58. {.offset = 0,
  59. .length = 6}}
  60. };
  61. static struct nand_ecclayout aml_nand_oob_64_2info = {
  62. .eccbytes = 56,
  63. .oobfree = {
  64. {.offset = 0,
  65. .length = 8}}
  66. };
  67. static struct nand_ecclayout aml_nand_oob_128 = {
  68. .eccbytes = 120,
  69. .oobfree = {
  70. {.offset = 0,
  71. .length = 8}}
  72. };
  73. static struct nand_ecclayout aml_nand_oob_218 = {
  74. .eccbytes = 200,
  75. .oobfree = {
  76. {.offset = 0,
  77. .length = 8}}
  78. };
  79. static struct nand_ecclayout aml_nand_oob_224 = {
  80. .eccbytes = 208,
  81. .oobfree = {
  82. {.offset = 0,
  83. .length = 8}}
  84. };
  85. static struct nand_ecclayout aml_nand_oob_256 = {
  86. .eccbytes = 240,
  87. .oobfree = {
  88. {.offset = 0,
  89. .length = 8}}
  90. };
  91. static struct nand_ecclayout aml_nand_oob_376 = {
  92. .eccbytes = 352,
  93. .oobfree = {
  94. {.offset = 0,
  95. .length = 8}}
  96. };
  97. static struct nand_ecclayout aml_nand_oob_436 = {
  98. .eccbytes = 352,
  99. .oobfree = {
  100. {.offset = 0,
  101. .length = 8}}
  102. };
  103. static struct nand_ecclayout aml_nand_oob_448 = {
  104. .eccbytes = 416,
  105. .oobfree = {
  106. {.offset = 0,
  107. .length = 8*2}}
  108. };
  109. static struct nand_ecclayout aml_nand_oob_752 = {
  110. .eccbytes = 704,
  111. .oobfree = {
  112. {.offset = 0,
  113. .length = 8}}
  114. };
  115. static struct nand_ecclayout aml_nand_oob_872 = {
  116. .eccbytes = 704,
  117. .oobfree = {
  118. {.offset = 0,
  119. .length = 8}}
  120. };
  121. static struct nand_ecclayout aml_nand_oob_896 = {
  122. .eccbytes = 832,
  123. .oobfree = {
  124. {.offset = 0,
  125. .length = 8}}
  126. };
  127. static struct nand_ecclayout aml_nand_oob_1504 = {
  128. .eccbytes = 1408,
  129. .oobfree = {
  130. {.offset = 0,
  131. .length = 8}}
  132. };
  133. static struct nand_ecclayout aml_nand_oob_1744 = {
  134. .eccbytes = 1664,
  135. .oobfree = {
  136. {.offset = 0,
  137. .length = 8}}
  138. };
  139. static struct nand_ecclayout aml_nand_oob_1792 = {
  140. .eccbytes = 1664,
  141. .oobfree = {
  142. {.offset = 0,
  143. .length = 8}}
  144. };
  145. static struct nand_ecclayout aml_nand_oob_3008 = {
  146. .eccbytes = 2816,
  147. .oobfree = {
  148. {.offset = 0,
  149. .length = 8}}
  150. };
  151. static struct nand_ecclayout aml_nand_oob_3584 = {
  152. .eccbytes = 3328,
  153. .oobfree = {
  154. {.offset = 0,
  155. .length = 8}}
  156. };
  157. static uint8_t nand_boot_flag = 0;
  158. static uint8_t nand_erarly_suspend_flag = 0;
  159. static uint8_t nand_mode_time[6] = {9, 7, 6, 5, 5, 4};
  160. struct aml_nand_flash_dev aml_nand_flash_ids[] = {
  161. {"A revision NAND 2GiB H27UAG8T2A", {NAND_MFR_HYNIX, 0xd5, 0x94, 0x25, 0x44, 0x41}, 4096, 2048, 0x80000, 224, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE)},
  162. {"A revision NAND 4GiB H27UBG8T2A", {NAND_MFR_HYNIX, 0xd7, 0x94, 0x25, 0x44, 0x41}, 4096, 4096, 0x80000, 224, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE)},
  163. {"B revision NAND 2GiB H27UAG8T2B", {NAND_MFR_HYNIX, 0xd5, 0x94, 0x9a, 0x74, 0x42}, 8192, 2048, 0x200000, 448, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE)},
  164. {"B revision NAND 4GiB H27UBG8T2A", {NAND_MFR_HYNIX, 0xd7, 0x94, 0x9a, 0x74, 0x42}, 8192, 4096, 0x200000, 448, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE)},
  165. {"A revision NAND 4GiB MT29F32G-A", {NAND_MFR_MICRON, 0xd7, 0x94, 0x3e, 0x84}, 4096, 4096, 0x80000, 218, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH24_MODE | NAND_TWO_PLANE_MODE)},
  166. {"A revision NAND 16GiB MT29F128G-A", {NAND_MFR_MICRON, 0xd9, 0xd5, 0x3e, 0x88}, 4096, 16384, 0x80000, 218, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH24_MODE | NAND_TWO_PLANE_MODE)},
  167. {"B revision NAND 4GiB MT29F32G-B", {NAND_MFR_MICRON, 0x68, 0x04, 0x46, 0x89}, 4096, 4096, 0x100000, 224, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE)},
  168. {"B revision NAND 16GiB MT29F128G-B", {NAND_MFR_MICRON, 0x88, 0x05, 0xc6, 0x89}, 4096, 16384, 0x100000, 224, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE)},
  169. {"C revision NAND 4GiB MT29F32G-C", {NAND_MFR_MICRON, 0x68, 0x04, 0x4a, 0xa9}, 4096, 4096, 0x100000, 224, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE)},
  170. {"C revision NAND 8GiB MT29F64G-C", {NAND_MFR_MICRON, 0x88, 0x04, 0x4b, 0xa9}, 8192, 8192, 0x200000, 448, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE)},
  171. {"C revision NAND 32GiB MT29F256G-C", {NAND_MFR_MICRON, 0xa8, 0x05, 0xcb, 0xa9}, 8192, 32768, 0x200000, 448, 2, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE | NAND_INTERLEAVING_MODE)},
  172. {"E serials NAND 2GiB TC58NVG4D2ETA00", {NAND_MFR_TOSHIBA, 0xD5, 0x94, 0x32, 0x76, 0x54}, 8192, 2048, 0x100000, 376, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH24_MODE | NAND_TWO_PLANE_MODE)},
  173. {"E serials NAND 4GiB TC58NVG5D2ETA00", {NAND_MFR_TOSHIBA, 0xD7, 0x94, 0x32, 0x76, 0x54}, 8192, 4096, 0x100000, 376, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH24_MODE | NAND_TWO_PLANE_MODE)},
  174. {"F serials NAND 4GiB TC58NVG5D2FTA00", {NAND_MFR_TOSHIBA, 0xD7, 0x94, 0x32, 0x76, 0x55}, 8192, 4096, 0x100000, 448, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE)},
  175. {"F serials NAND 8GiB TC58NVG6D2FTA00", {NAND_MFR_TOSHIBA, 0xDE, 0x94, 0x32, 0x76, 0x55}, 8192, 8192, 0x100000, 448, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH30_MODE | NAND_TWO_PLANE_MODE)},
  176. {"M Generation NAND 2GiB K9GAG08U0M", {NAND_MFR_SAMSUNG, 0xD5, 0x14, 0xb6, 0x74}, 4096, 2048, 0x80000, 128, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH8_512_MODE)},
  177. {"5 Generation NAND 2GiB K9GAG08X0D", {NAND_MFR_SAMSUNG, 0xD5, 0x94, 0x29, 0x34, 0x41}, 4096, 2048, 0x80000, 218, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH24_MODE | NAND_TWO_PLANE_MODE)},
  178. {"6 Generation NAND 2GiB K9GAG08U0E", {NAND_MFR_SAMSUNG, 0xD5, 0x84, 0x72, 0x50, 0x42}, 8192, 2048, 0x100000, 436, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH24_MODE)},
  179. {"6 Generation NAND 4GiB K9LBG08U0E", {NAND_MFR_SAMSUNG, 0xD7, 0xC5, 0x72, 0x54, 0x42}, 8192, 4096, 0x100000, 436, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH24_MODE | NAND_TWO_PLANE_MODE)},
  180. {"6 Generation NAND 8GiB K9HCG08U0E", {NAND_MFR_SAMSUNG, 0xDE, 0xC5, 0x72, 0x54, 0x42}, 8192, 8192, 0x100000, 436, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH24_MODE | NAND_TWO_PLANE_MODE)},
  181. {"2 Generation NAND 4GiB K9GBG08U0A", {NAND_MFR_SAMSUNG, 0xD7, 0x94, 0x7a, 0x54, 0x43}, 8192, 4152, 0x100000, 640, 1, (NAND_TIMING_MODE5 | NAND_ECC_BCH40_MODE | NAND_TWO_PLANE_MODE)},
  182. {"2 Generation NAND 8GiB K9LCG08U0A", {NAND_MFR_SAMSUNG, 0xDE, 0xD5, 0x7a, 0x58, 0x43}, 8192, 8304, 0x100000, 640, 2, (NAND_TIMING_MODE5 | NAND_ECC_BCH40_MODE | NAND_TWO_PLANE_MODE | NAND_INTERLEAVING_MODE)},
  183. {NULL,}
  184. };
  185. static void aml_nand_cmdfunc(struct mtd_info *mtd, unsigned command, int column, int page_addr);
  186. static void aml_platform_hw_init(struct aml_nand_chip *aml_chip)
  187. {
  188. struct mtd_info *mtd = &aml_chip->mtd;
  189. struct nand_chip *chip = &aml_chip->chip;
  190. struct aml_nand_platform *plat = aml_chip->platform;
  191. struct clk *sys_clk;
  192. unsigned sys_clk_rate, sys_time, start_cycle, end_cycle, bus_cycle, time_mode, tmp;
  193. #if 0
  194. if ((aml_chip->chip_num > 1) && !nand_erarly_suspend_flag) {
  195. chip->select_chip(mtd, -1);
  196. CLEAR_CBUS_REG_MASK(PREG_PAD_GPIO3_EN_N, (1 << 5));
  197. CLEAR_CBUS_REG_MASK(PREG_PAD_GPIO3_O, (1 << 5));
  198. SET_CBUS_REG_MASK(PREG_PAD_GPIO3_EN_N, (1 << 16));
  199. if (!(READ_CBUS_REG(PREG_PAD_GPIO3_I) & (1 << 16))) {
  200. SET_CBUS_REG_MASK(PREG_PAD_GPIO3_O, (1 << 5));
  201. if ((READ_CBUS_REG(PREG_PAD_GPIO3_I) & (1 << 16))) {
  202. aml_chip->chip_enable[1] = (aml_chip->chip_enable[1] & aml_chip->chip_enable[2]);
  203. aml_chip->rb_enable[1] = aml_chip->rb_enable[2];
  204. aml_chip->chip_num = 2;
  205. aml_nand_debug("ce1 and ce2 connected\n");
  206. }
  207. }
  208. }
  209. #endif
  210. if (!plat->T_REA)
  211. plat->T_REA = 20;
  212. if (!plat->T_RHOH)
  213. plat->T_RHOH = 15;
  214. time_mode = ((plat->platform_nand_data.chip.options & NAND_TIMING_OPTIONS_MASK) >> 8);
  215. if (time_mode > 5)
  216. time_mode = 5;
  217. if (READ_CBUS_REG(HHI_MPEG_CLK_CNTL)&(1<<8)) {
  218. sys_clk = clk_get_sys(NAND_SYS_CLK_NAME, NULL);
  219. sys_clk_rate = clk_get_rate(sys_clk);
  220. }
  221. else {
  222. time_mode = 0;
  223. sys_clk_rate = 27000000;
  224. }
  225. sys_time = (10000 / (sys_clk_rate / 1000000)); //50,55
  226. bus_cycle = nand_mode_time[time_mode]; //mode=5, bus_c=4;
  227. tmp=300/sys_time;
  228. if(300%sys_time)
  229. tmp++;
  230. if(bus_cycle<tmp)
  231. bus_cycle=tmp;
  232. start_cycle = ((NAND_CYCLE_DELAY + plat->T_REA * 10) / sys_time);
  233. if(((NAND_CYCLE_DELAY + plat->T_REA * 10)%sys_time))
  234. start_cycle++;
  235. end_cycle = ((NAND_CYCLE_DELAY + (bus_cycle*sys_time)/2 + plat->T_RHOH * 10) / sys_time);
  236. /*if (bus_cycle < start_cycle)
  237. adjust = start_cycle - bus_cycle;
  238. else if(bus_cycle > end_cycle) {
  239. if (bus_cycle > 6)
  240. bus_cycle = 6;
  241. adjust = ((((~(bus_cycle - start_cycle) + 1)) & 0xf) | 0x8);
  242. }
  243. else
  244. adjust = 0;*/
  245. bus_cycle -= 1;
  246. tmp=(start_cycle+end_cycle)/2;
  247. NFC_SET_CFG(0);
  248. NFC_SET_TIMING_ASYC(tmp, bus_cycle);
  249. NFC_SEND_CMD(1<<31);
  250. dev_info(aml_chip->device, "time_mode=%d, bus_cycle=%d, tREA=%d, tRHOH=%d, bus_tim=%x system=%d\n",
  251. time_mode, bus_cycle, plat->T_REA, plat->T_RHOH, tmp, (sys_time/10));
  252. }
  253. static int aml_platform_options_confirm(struct aml_nand_chip *aml_chip)
  254. {
  255. struct mtd_info *mtd = &aml_chip->mtd;
  256. struct nand_chip *chip = &aml_chip->chip;
  257. struct aml_nand_platform *plat = aml_chip->platform;
  258. struct ecc_desc_s * ecc_supports=aml_chip->ecc;
  259. unsigned max_ecc=aml_chip->max_ecc;
  260. unsigned options_selected = 0, options_support = 0, ecc_bytes, options_define;
  261. int error = 0,i;
  262. //ecc check
  263. options_selected = (plat->platform_nand_data.chip.options & NAND_ECC_OPTIONS_MASK);
  264. options_define = (aml_chip->options & NAND_ECC_OPTIONS_MASK);
  265. aml_nand_debug("options_selected=%s options_define=%s\n",
  266. ecc_supports[options_selected].name,ecc_supports[options_define].name);
  267. ///caculate the chips support ECC
  268. for(i=max_ecc-1;i>0;i--)//0 always is raw mode
  269. {
  270. ecc_bytes = aml_chip->oob_size / (aml_chip->page_size / ecc_supports[i].size);
  271. if(ecc_bytes>=ecc_supports[i].parity+ecc_supports[i].user)
  272. {
  273. options_support=ecc_supports[i].bch;
  274. break;
  275. }
  276. }
  277. if(options_support==0)
  278. BUG_printk("Could NOT Find out support ecc");
  279. aml_nand_debug("This one can support %s",ecc_supports[options_support].name);
  280. #if 0
  281. ecc_unit_change:
  282. ecc_bytes = aml_chip->oob_size / (aml_chip->page_size / chip->ecc.size);
  283. if (chip->ecc.size == NAND_ECC_UNIT_1KSIZE)
  284. {
  285. if (ecc_bytes >= (NAND_BCH60_ECC_SIZE + 2))
  286. options_support = NAND_ECC_BCH60_MODE;
  287. else
  288. {
  289. aml_nand_debug(" aml_chip->page_size %d chip->ecc.size %d , div res \n",
  290. aml_chip->page_size,chip->ecc.size);
  291. aml_nand_debug("oob_size %d ecc_bytes %d\n",aml_chip->oob_size,ecc_bytes);
  292. aml_nand_debug("oob size is not enough for 1K UNIT ECC mode: "
  293. "%d try 512 UNIT ECC\n", aml_chip->oob_size);
  294. chip->ecc.size = NAND_ECC_UNIT_SIZE;
  295. goto ecc_unit_change;
  296. }
  297. }
  298. else {
  299. /*if (ecc_bytes >= (NAND_BCH16_ECC_SIZE + 2))
  300. options_support = NAND_ECC_BCH16_MODE;
  301. else if (ecc_bytes >= (NAND_BCH12_ECC_SIZE + 2))
  302. options_support = NAND_ECC_BCH12_MODE;
  303. else*/
  304. if (ecc_bytes >= (NAND_BCH8_512_ECC_SIZE + 2))
  305. options_support = NAND_ECC_BCH8_512_MODE;
  306. else {
  307. options_support = NAND_ECC_SOFT_MODE;
  308. aml_nand_debug("page size: %d oob size %d is not enough for HW ECC\n", aml_chip->page_size, aml_chip->oob_size);
  309. }
  310. }
  311. #endif
  312. if (options_define != options_support) {
  313. options_define = options_support;
  314. aml_nand_debug("define oob size: %d could support bch mode: %s\n", aml_chip->oob_size, ecc_supports[options_support].name);
  315. }
  316. if ((options_selected > options_define) && (strncmp((char*)plat->name, NAND_BOOT_NAME, strlen((const char*)NAND_BOOT_NAME)))) {
  317. aml_nand_debug("oob size is not enough for selected bch mode: %s force bch to mode: %s\n",
  318. ecc_supports[options_selected].name,ecc_supports[options_define].name);
  319. options_selected = options_define;
  320. //BUG_printk("Wrong NAND option, Please Check Your setting and confirm it with U Boot");
  321. }
  322. switch (options_selected) {
  323. case NAND_ECC_BCH8_512_MODE:
  324. chip->ecc.size = NAND_ECC_UNIT_SIZE; //our hardware ecc unit is 512bytes
  325. chip->ecc.bytes = NAND_BCH8_512_ECC_SIZE;
  326. aml_chip->bch_mode = NAND_ECC_BCH8_512;
  327. aml_chip->user_byte_mode = 2;
  328. break;
  329. case NAND_ECC_BCH8_1K_MODE:
  330. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  331. chip->ecc.bytes = NAND_BCH8_1K_ECC_SIZE;
  332. aml_chip->bch_mode = NAND_ECC_BCH8_1K;
  333. aml_chip->user_byte_mode = 2;
  334. break;
  335. case NAND_ECC_BCH30_MODE:
  336. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  337. chip->ecc.bytes = NAND_BCH30_ECC_SIZE;
  338. aml_chip->bch_mode = NAND_ECC_BCH30;
  339. aml_chip->user_byte_mode = 2;
  340. break;
  341. case NAND_ECC_BCH40_MODE:
  342. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  343. chip->ecc.bytes = NAND_BCH40_ECC_SIZE;
  344. aml_chip->bch_mode = NAND_ECC_BCH40;
  345. aml_chip->user_byte_mode = 2;
  346. break;
  347. case NAND_ECC_BCH60_MODE:
  348. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  349. chip->ecc.bytes = NAND_BCH60_ECC_SIZE;
  350. aml_chip->bch_mode = NAND_ECC_BCH60;
  351. aml_chip->user_byte_mode = 2;
  352. break;
  353. case NAND_ECC_SHORT_MODE:
  354. chip->ecc.size = NAND_ECC_UNIT_SHORT;
  355. chip->ecc.bytes = NAND_BCH60_ECC_SIZE;
  356. aml_chip->bch_mode = NAND_ECC_BCH60;
  357. aml_chip->user_byte_mode = 2;
  358. break;
  359. case NAND_ECC_BCH16_MODE:
  360. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  361. chip->ecc.bytes = NAND_BCH16_ECC_SIZE;
  362. aml_chip->bch_mode = NAND_ECC_BCH16;
  363. aml_chip->user_byte_mode = 2;
  364. break;
  365. case NAND_ECC_BCH24_MODE:
  366. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  367. chip->ecc.bytes = NAND_BCH24_ECC_SIZE;
  368. aml_chip->bch_mode = NAND_ECC_BCH24;
  369. aml_chip->user_byte_mode = 2;
  370. break;
  371. default :
  372. if ((plat->platform_nand_data.chip.options & NAND_ECC_OPTIONS_MASK) != NAND_ECC_SOFT_MODE) {
  373. aml_nand_debug("soft ecc or none ecc just support in linux self nand base please selected it at platform options\n");
  374. error = -ENXIO;
  375. }
  376. break;
  377. }
  378. //plane check
  379. options_selected = (plat->platform_nand_data.chip.options & NAND_PLANE_OPTIONS_MASK);
  380. options_define = (aml_chip->options & NAND_PLANE_OPTIONS_MASK);
  381. if (options_selected > options_define) {
  382. aml_nand_debug("multi plane error for selected plane mode: %s force plane to : %s\n", aml_nand_plane_string[options_selected >> 4], aml_nand_plane_string[options_define >> 4]);
  383. options_selected = options_define;
  384. }
  385. switch (options_selected) {
  386. case NAND_TWO_PLANE_MODE:
  387. aml_chip->plane_num = 2;
  388. mtd->erasesize *= 2;
  389. mtd->writesize *= 2;
  390. mtd->oobsize *= 2;
  391. chip->page_shift = ffs(mtd->writesize) - 1;
  392. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  393. chip->bbt_erase_shift = chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
  394. printk("enter NAND_TWO_PLANE_MODE : erasesize= 0x%x,writesize= 0x%x,oobsize= 0x%x\n",
  395. mtd->erasesize,mtd->writesize,mtd->oobsize);
  396. break;
  397. default:
  398. aml_chip->plane_num = 1;
  399. break;
  400. }
  401. //interleave check
  402. options_selected = (plat->platform_nand_data.chip.options & NAND_INTERLEAVING_OPTIONS_MASK);
  403. options_define = (aml_chip->options & NAND_INTERLEAVING_OPTIONS_MASK);
  404. if (options_selected > options_define) {
  405. aml_nand_debug("internal mode error for selected internal mode: %s force internal mode to : %s\n", aml_nand_internal_string[options_selected >> 16], aml_nand_internal_string[options_define >> 16]);
  406. options_selected = options_define;
  407. }
  408. switch (options_selected) {
  409. case NAND_INTERLEAVING_MODE:
  410. aml_chip->ops_mode |= AML_INTERLEAVING_MODE;
  411. mtd->erasesize *= aml_chip->internal_chipnr;
  412. mtd->writesize *= aml_chip->internal_chipnr;
  413. mtd->oobsize *= aml_chip->internal_chipnr;
  414. break;
  415. default:
  416. break;
  417. }
  418. return error;
  419. }
  420. static void aml_platform_cmd_ctrl(struct aml_nand_chip *aml_chip, int cmd, unsigned int ctrl)
  421. {
  422. if (cmd == NAND_CMD_NONE)
  423. return;
  424. if (ctrl & NAND_CLE)
  425. cmd=NFC_CMD_CLE(aml_chip->chip_selected, cmd);
  426. else
  427. cmd=NFC_CMD_ALE(aml_chip->chip_selected, cmd);
  428. NFC_SEND_CMD(cmd);
  429. }
  430. static void aml_platform_select_chip(struct aml_nand_chip *aml_chip, int chipnr)
  431. {
  432. //int i;
  433. switch (chipnr) {
  434. case 0:
  435. case 1:
  436. case 2:
  437. case 3:
  438. aml_chip->chip_selected = aml_chip->chip_enable[chipnr];
  439. aml_chip->rb_received = aml_chip->rb_enable[chipnr];
  440. /*for (i=1; i<aml_chip->chip_num; i++) {
  441. if (aml_chip->valid_chip[i]) {
  442. if (!((aml_chip->chip_enable[i] >> 10) & 1))
  443. SET_CBUS_REG_MASK(PERIPHS_PIN_MUX_6, (1 << 4));
  444. if (!((aml_chip->chip_enable[i] >> 10) & 2))
  445. SET_CBUS_REG_MASK(PERIPHS_PIN_MUX_6, (1 << 3));
  446. if (!((aml_chip->chip_enable[i] >> 10) & 4))
  447. SET_CBUS_REG_MASK(PERIPHS_PIN_MUX_6, (1 << 14));
  448. if (!((aml_chip->chip_enable[i] >> 10) & 8))
  449. SET_CBUS_REG_MASK(PERIPHS_PIN_MUX_6, (1 << 13));
  450. if (!((aml_chip->rb_enable[i] >> 10) & 1))
  451. SET_CBUS_REG_MASK(PERIPHS_PIN_MUX_6, (1 << 2));
  452. if (!((aml_chip->rb_enable[i] >> 10) & 2))
  453. SET_CBUS_REG_MASK(PERIPHS_PIN_MUX_6, (1 << 1));
  454. if (!((aml_chip->rb_enable[i] >> 10) & 4))
  455. SET_CBUS_REG_MASK(PERIPHS_PIN_MUX_6, (1 << 12));
  456. if (!((aml_chip->rb_enable[i] >> 10) & 8))
  457. SET_CBUS_REG_MASK(PERIPHS_PIN_MUX_6, (1 << 11));
  458. }
  459. }*/
  460. NFC_SEND_CMD_IDLE(aml_chip->chip_selected, 0);
  461. break;
  462. default:
  463. BUG();
  464. aml_chip->chip_selected = CE_NOT_SEL;
  465. break;
  466. }
  467. return;
  468. }
  469. /*ADD RBPIN NO mode*/
  470. static int aml_platform_wait_devready(struct aml_nand_chip *aml_chip, int chipnr)
  471. {
  472. struct nand_chip *chip = &aml_chip->chip;
  473. struct mtd_info *mtd = &aml_chip->mtd;
  474. unsigned time_out_cnt = 0;
  475. int status;
  476. #if CONFIG_AM_NAND_RBPIN
  477. /* wait until command is processed or timeout occures */
  478. aml_chip->aml_nand_select_chip(aml_chip, chipnr);
  479. do {
  480. if (aml_chip->ops_mode == AML_MULTI_CHIP_SHARE_RB) {
  481. aml_chip->aml_nand_command(aml_chip, NAND_CMD_STATUS, -1, -1, chipnr);
  482. status = (int)chip->read_byte(mtd);
  483. if (status & NAND_STATUS_READY_MULTI)
  484. break;
  485. }
  486. else {
  487. if (chip->dev_ready) {
  488. if (chip->dev_ready(mtd))
  489. break;
  490. } else {
  491. // if(chip->waitfunc(mtd, chip)& NAND_STATUS_READY)
  492. // break;
  493. if (chip->read_byte(mtd) & NAND_STATUS_READY)
  494. break;
  495. }
  496. }
  497. } while (time_out_cnt++ <= AML_NAND_BUSY_TIMEOUT);
  498. if (time_out_cnt > AML_NAND_BUSY_TIMEOUT)
  499. return 0;
  500. #else
  501. // aml_chip->aml_nand_select_chip(aml_chip, chipnr);
  502. // aml_chip->aml_nand_command(aml_chip, NAND_CMD_STATUS, -1, -1, chipnr);
  503. // NFC_SEND_CMD_RBIO(IO6, 10);
  504. #if 0
  505. while (time_out_cnt++ < 0x10000) {
  506. status = (int)chip->read_byte(mtd);
  507. if ((NAND_STATUS_READY|NAND_STATUS_TRUE_READY) ==status& (NAND_STATUS_READY|NAND_STATUS_TRUE_READY))
  508. break;
  509. udelay(9);
  510. }
  511. int state = chip->state;
  512. if( time_out_cnt >0x10000)
  513. {
  514. aml_nand_debug("NAND_CMD_STATUS time out !\n");
  515. if(state ==FL_READING) {
  516. aml_nand_debug("--FL_READING fail !\n");
  517. }
  518. if(state ==FL_ERASING) {
  519. aml_nand_debug("--FL_ERASING fail !\n");
  520. }
  521. else if(state ==FL_WRITING ) {
  522. aml_nand_debug("--FL_WRITING fail !\n");
  523. }
  524. }
  525. else
  526. {
  527. if(status &(NAND_STATUS_FAIL|NAND_STATUS_FAIL_N1))
  528. {
  529. if(state ==FL_ERASING) {
  530. aml_nand_debug("FL_ERASING fail !\n");
  531. }
  532. else if(state ==FL_WRITING ) {
  533. aml_nand_debug("FL_WRITING fail !\n");
  534. }
  535. }
  536. }
  537. return status;
  538. #endif
  539. #endif
  540. return 1;
  541. }
  542. static void aml_platform_get_user_byte(struct aml_nand_chip *aml_chip, unsigned char *oob_buf, int byte_num)
  543. {
  544. int read_times = 0;
  545. aml_nand_debug2("\nrd oob: ");
  546. while (byte_num > 0) {
  547. aml_nand_debug2("0x%08x ",(u32)aml_chip->user_info_buf[read_times]);
  548. aml_nand_debug2("0x%08x ",(u32)aml_chip->user_info_buf[read_times+1]);
  549. *oob_buf++ = (aml_chip->user_info_buf[read_times] & 0xff);
  550. byte_num--;
  551. if (aml_chip->user_byte_mode == 2) {
  552. *oob_buf++ = ((aml_chip->user_info_buf[read_times] >> 8) & 0xff);
  553. byte_num--;
  554. }
  555. read_times+=PER_INFO_BYTE/sizeof(unsigned int); //8 bytes now
  556. }
  557. }
  558. static void aml_platform_set_user_byte(struct aml_nand_chip *aml_chip, unsigned char *oob_buf, int byte_num)
  559. {
  560. int write_times = 0;
  561. aml_nand_debug2("\nwr oob: ");
  562. while (byte_num > 0) {
  563. aml_chip->user_info_buf[write_times] = *oob_buf++;
  564. byte_num--;
  565. if (aml_chip->user_byte_mode == 2) {
  566. aml_chip->user_info_buf[write_times] |= (*oob_buf++ << 8);
  567. byte_num--;
  568. }
  569. aml_nand_debug2("0x%08x ",(u32)aml_chip->user_info_buf[write_times]);
  570. aml_nand_debug2("0x%08x ",(u32)aml_chip->user_info_buf[write_times+1]);
  571. write_times+=PER_INFO_BYTE/sizeof(unsigned int); //8 bytes now
  572. }
  573. }
  574. #if 1
  575. #define Tdbsy 500 //500ns
  576. #define Tbers 3 //3ms
  577. #define Tprog 900 //900us
  578. #define Tr 50 //<=50us
  579. #else
  580. #define Tdbsy 0 //500ns
  581. #define Tbers 0 //3ms
  582. #define Tprog 0 //900us
  583. #define Tr 0 //<=50us
  584. #endif
  585. static void aml_nand_base_command(struct aml_nand_chip *aml_chip, unsigned command, int column, int page_addr, int chipnr)
  586. {
  587. struct nand_chip *chip = &aml_chip->chip;
  588. struct mtd_info *mtd = &aml_chip->mtd;
  589. unsigned command_temp, pages_per_blk_shift, plane_page_addr = 0, plane_blk_addr = 0;
  590. pages_per_blk_shift = (chip->phys_erase_shift - chip->page_shift);
  591. if (page_addr != -1) {
  592. // printk("page_addr = 0x%x\n",page_addr);
  593. // page_addr /= aml_chip->plane_num;
  594. plane_page_addr = page_addr &( (1 << pages_per_blk_shift ) -1);
  595. plane_blk_addr = page_addr >> pages_per_blk_shift;
  596. plane_blk_addr <<=1; //block No in plane 0.(PB block No..)
  597. }
  598. if (aml_chip->plane_num == 2) {
  599. switch (command) {
  600. case NAND_CMD_READ0:
  601. if (aml_chip->mfr_type == NAND_MFR_MICRON) {
  602. command_temp = command;
  603. }
  604. else {
  605. command_temp = NAND_CMD_TWOPLANE_PREVIOS_READ;
  606. column = -1;
  607. }
  608. // printk("plane_blk_addr = 0x%x,plane_page_addr=0x0%x\n",plane_blk_addr,plane_page_addr);
  609. plane_page_addr |= (plane_blk_addr << pages_per_blk_shift);
  610. // printk("read x0:plane_page_addr =0x0%x\n",plane_page_addr);
  611. break;
  612. case NAND_CMD_TWOPLANE_READ1:
  613. command_temp = NAND_CMD_READ0;
  614. #if 0
  615. if (aml_chip->mfr_type == NAND_MFR_MICRON)
  616. //plane_page_addr |= ((plane_blk_addr + 1) << 8);
  617. return;
  618. else
  619. #else
  620. if (aml_chip->mfr_type == NAND_MFR_MICRON) {
  621. command_temp =NAND_CMD_PLANE2_READ_START;
  622. }
  623. #endif
  624. plane_page_addr |= (plane_blk_addr << pages_per_blk_shift);
  625. // printk("read x00:plane_page_addr =0x0%x\n",plane_page_addr);
  626. break;
  627. case NAND_CMD_TWOPLANE_READ2:
  628. if (aml_chip->mfr_type == NAND_MFR_MICRON) {
  629. command_temp =NAND_CMD_PLANE2_READ_START;
  630. }
  631. else
  632. {
  633. command_temp = NAND_CMD_READ0;
  634. }
  635. plane_page_addr |= ((plane_blk_addr + 1) << pages_per_blk_shift);
  636. // printk("read y11:plane_page_addr =0x0%x\n",plane_page_addr);
  637. break;
  638. case NAND_CMD_SEQIN:
  639. command_temp = command;
  640. plane_page_addr |= (plane_blk_addr << pages_per_blk_shift);
  641. // printk("write x0:plane_page_addr =0x0%x\n",plane_page_addr);
  642. break;
  643. case NAND_CMD_TWOPLANE_WRITE2:
  644. if ((aml_chip->mfr_type == NAND_MFR_HYNIX) || (aml_chip->mfr_type == NAND_MFR_SAMSUNG))
  645. command_temp = command;
  646. else
  647. command_temp = NAND_CMD_TWOPLANE_WRITE2_MICRO;
  648. plane_page_addr |= ((plane_blk_addr + 1) << pages_per_blk_shift); //plane 1 page
  649. // printk("write y1:plane_page_addr =0x0%x\n",plane_page_addr);
  650. break;
  651. case NAND_CMD_ERASE1:
  652. command_temp = command;
  653. plane_page_addr |= (plane_blk_addr << pages_per_blk_shift);
  654. // printk("erase x0:plane_page_addr =0x0%x\n",plane_page_addr);
  655. break;
  656. case NAND_CMD_MULTI_CHIP_STATUS:
  657. command_temp = command;
  658. plane_page_addr |= (plane_blk_addr << pages_per_blk_shift);
  659. break;
  660. default:
  661. command_temp = command;
  662. break;
  663. }
  664. chip->cmd_ctrl(mtd, command_temp & 0xff, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  665. //if ((command_temp == NAND_CMD_SEQIN) || (command_temp == NAND_CMD_TWOPLANE_WRITE2) || (command_temp == NAND_CMD_READ0))
  666. //printk(" NAND plane_page_addr: %x plane_blk_addr %x command: %x \n", plane_page_addr, plane_blk_addr, command);
  667. if (column != -1 || page_addr != -1) {
  668. int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  669. /* Serially input address */
  670. if (column != -1) {
  671. /* Adjust columns for 16 bit buswidth */
  672. if (chip->options & NAND_BUSWIDTH_16)
  673. column >>= 1;
  674. chip->cmd_ctrl(mtd, column, ctrl);
  675. ctrl &= ~NAND_CTRL_CHANGE;
  676. chip->cmd_ctrl(mtd, column >> 8, ctrl);
  677. }
  678. if (page_addr != -1) {
  679. chip->cmd_ctrl(mtd, plane_page_addr, ctrl);
  680. chip->cmd_ctrl(mtd, plane_page_addr >> 8, NAND_NCE | NAND_ALE);
  681. /* One more address cycle for devices > 128MiB */
  682. if (chip->chipsize > (128 << 20))
  683. chip->cmd_ctrl(mtd, plane_page_addr >> 16, NAND_NCE | NAND_ALE);
  684. }
  685. }
  686. switch (command) {
  687. case NAND_CMD_READ0:
  688. plane_page_addr = page_addr % (1 << pages_per_blk_shift);
  689. if (aml_chip->mfr_type == NAND_MFR_MICRON) {
  690. chip->cmd_ctrl(mtd, 0x32 & 0xff, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  691. ndelay(Tdbsy);
  692. plane_page_addr |= ((plane_blk_addr + 1) << pages_per_blk_shift);
  693. // printk("read y1:plane_page_addr =0x0%x\n",plane_page_addr);
  694. command_temp = command; //read plane1
  695. chip->cmd_ctrl(mtd, command_temp & 0xff, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  696. }
  697. else {
  698. command_temp = NAND_CMD_TWOPLANE_PREVIOS_READ;
  699. column = -1;
  700. plane_page_addr |= ((plane_blk_addr + 1) << pages_per_blk_shift);
  701. chip->cmd_ctrl(mtd, command_temp & 0xff, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  702. }
  703. break;
  704. case NAND_CMD_TWOPLANE_READ1:
  705. if (aml_chip->mfr_type == NAND_MFR_MICRON) {
  706. page_addr = -1;
  707. column = -1;
  708. }
  709. else {
  710. command_temp = NAND_CMD_RNDOUT;
  711. page_addr = -1;
  712. chip->cmd_ctrl(mtd, command_temp & 0xff, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  713. }
  714. break;
  715. case NAND_CMD_TWOPLANE_READ2:
  716. if (aml_chip->mfr_type == NAND_MFR_MICRON) {
  717. page_addr = -1;
  718. column = -1;
  719. }
  720. else {
  721. command_temp = NAND_CMD_RNDOUT;
  722. page_addr = -1;
  723. chip->cmd_ctrl(mtd, command_temp & 0xff, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  724. }
  725. break;
  726. case NAND_CMD_ERASE1:
  727. if (aml_chip->mfr_type == NAND_MFR_MICRON) {
  728. command_temp = NAND_CMD_ERASE1_END;
  729. chip->cmd_ctrl(mtd, command_temp & 0xff, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  730. ndelay(Tdbsy);
  731. aml_chip->aml_nand_wait_devready(aml_chip, chipnr);
  732. }
  733. command_temp = command;
  734. chip->cmd_ctrl(mtd, command_temp & 0xff, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  735. plane_page_addr = page_addr % (1 << pages_per_blk_shift);
  736. plane_page_addr |= ((plane_blk_addr + 1) << pages_per_blk_shift);
  737. // printk("erase y1:plane_page_addr =0x0%x\n",plane_page_addr);
  738. break;
  739. case NAND_CMD_DUMMY_PROGRAM:
  740. if ((aml_chip->mfr_type == NAND_MFR_MICRON) )
  741. ndelay(Tdbsy);
  742. break;
  743. default:
  744. column = -1;
  745. page_addr = -1;
  746. break;
  747. }
  748. if (column != -1 || page_addr != -1) {
  749. int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  750. /* Serially input address */
  751. if (column != -1) {
  752. /* Adjust columns for 16 bit buswidth */
  753. if (chip->options & NAND_BUSWIDTH_16)
  754. column >>= 1;
  755. chip->cmd_ctrl(mtd, column, ctrl);
  756. ctrl &= ~NAND_CTRL_CHANGE;
  757. chip->cmd_ctrl(mtd, column >> 8, ctrl);
  758. }
  759. if (page_addr != -1) {
  760. //plane_page_addr |= (1 << (pages_per_blk_shift + 1));
  761. //BUG_ON((plane_page_addr & 0x7FF) == 0);
  762. chip->cmd_ctrl(mtd, plane_page_addr, ctrl);
  763. chip->cmd_ctrl(mtd, plane_page_addr >> 8, NAND_NCE | NAND_ALE);
  764. /* One more address cycle for devices > 128MiB */
  765. if (chip->chipsize > (128 << 20))
  766. chip->cmd_ctrl(mtd, plane_page_addr >> 16, NAND_NCE | NAND_ALE);
  767. }
  768. }
  769. if ((command == NAND_CMD_RNDOUT) || (command == NAND_CMD_TWOPLANE_READ2)){
  770. chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  771. // ndelay(Tdbsy);
  772. }
  773. else if ((command == NAND_CMD_TWOPLANE_READ1)) {
  774. chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  775. // ndelay(Tdbsy);
  776. }
  777. else if (command == NAND_CMD_READ0) {
  778. chip->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  779. // udelay(Tr);
  780. }
  781. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  782. }
  783. else {
  784. chip->cmd_ctrl(mtd, command & 0xff, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  785. if (column != -1 || page_addr != -1) {
  786. int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  787. /* Serially input address */
  788. if (column != -1) {
  789. /* Adjust columns for 16 bit buswidth */
  790. if (chip->options & NAND_BUSWIDTH_16)
  791. column >>= 1;
  792. chip->cmd_ctrl(mtd, column, ctrl);
  793. ctrl &= ~NAND_CTRL_CHANGE;
  794. chip->cmd_ctrl(mtd, column >> 8, ctrl);
  795. }
  796. if (page_addr != -1) {
  797. chip->cmd_ctrl(mtd, page_addr, ctrl);
  798. chip->cmd_ctrl(mtd, page_addr >> 8, NAND_NCE | NAND_ALE);
  799. /* One more address cycle for devices > 128MiB */
  800. if (chip->chipsize > (128 << 20))
  801. chip->cmd_ctrl(mtd, page_addr >> 16, NAND_NCE | NAND_ALE);
  802. }
  803. }
  804. if (command == NAND_CMD_RNDOUT)
  805. chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  806. else if (command == NAND_CMD_READ0)
  807. chip->cmd_ctrl(mtd, NAND_CMD_READSTART, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  808. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  809. }
  810. /*
  811. * program and erase have their own busy handlers
  812. * status, sequential in, and deplete1 need no delay
  813. */
  814. switch (command) {
  815. case NAND_CMD_CACHEDPROG:
  816. case NAND_CMD_PAGEPROG:
  817. case NAND_CMD_ERASE1:
  818. case NAND_CMD_ERASE2:
  819. case NAND_CMD_SEQIN:
  820. case NAND_CMD_RNDIN:
  821. case NAND_CMD_STATUS:
  822. case NAND_CMD_DEPLETE1:
  823. return;
  824. /*
  825. * read error status commands require only a short delay
  826. */
  827. case NAND_CMD_STATUS_ERROR:
  828. case NAND_CMD_STATUS_ERROR0:
  829. case NAND_CMD_STATUS_ERROR1:
  830. case NAND_CMD_STATUS_ERROR2:
  831. case NAND_CMD_STATUS_ERROR3:
  832. udelay(chip->chip_delay);
  833. return;
  834. case NAND_CMD_RESET:
  835. if (!aml_chip->aml_nand_wait_devready(aml_chip, chipnr))
  836. aml_nand_debug ("couldn`t found selected chip: %d ready\n", chipnr);
  837. if (chip->dev_ready)
  838. break;
  839. udelay(chip->chip_delay);
  840. chip->cmd_ctrl(mtd, NAND_CMD_STATUS, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  841. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  842. while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
  843. return;
  844. default:
  845. /*
  846. * If we don't have access to the busy pin, we apply the given
  847. * command delay
  848. */
  849. break;
  850. }
  851. /* Apply this short delay always to ensure that we do wait tWB in
  852. * any case on any machine. */
  853. ndelay(100);
  854. }
  855. static int aml_platform_dma_waiting(struct aml_nand_chip *aml_chip)
  856. {
  857. unsigned time_out_cnt = 0;
  858. NFC_SEND_CMD_IDLE(aml_chip->chip_selected, 0);
  859. NFC_SEND_CMD_IDLE(aml_chip->chip_selected, 0);
  860. do {
  861. if (NFC_CMDFIFO_SIZE() <= 0)
  862. break;
  863. }while (time_out_cnt++ <= AML_DMA_BUSY_TIMEOUT);
  864. if (time_out_cnt < AML_DMA_BUSY_TIMEOUT)
  865. return 0;
  866. aml_nand_debug("aml_platform_dma_waiting time out !\n");
  867. return -EBUSY;
  868. }
  869. static int aml_platform_dma_write(struct aml_nand_chip *aml_chip, unsigned char *buf, int len, unsigned bch_mode)
  870. {
  871. int ret = 0;
  872. unsigned count=0,pgsz=0;
  873. //struct mtd_info *mtd = &aml_chip->mtd;
  874. struct nand_chip *chip = &aml_chip->chip;
  875. dma_addr_t data_dma_addr=0;
  876. dma_addr_t user_data_dma_addr=0;
  877. count = len/chip->ecc.size;
  878. if(aml_chip->short_pgsz)
  879. pgsz = chip->ecc.size>>3;
  880. if(!bch_mode)
  881. {
  882. count = 1;
  883. }
  884. // data_dma_addr=dma_map_single(aml_chip->device,(void *)buf,len,DMA_TO_DEVICE);
  885. // user_data_dma_addr=dma_map_single(aml_chip->device,(void *)aml_chip->user_info_buf,count*PER_INFO_BYTE,DMA_TO_DEVICE);
  886. dmac_flush_range((unsigned long )buf, ((unsigned long )buf) + len - 1);
  887. dmac_flush_range((unsigned long )aml_chip->user_info_buf, ((unsigned long )aml_chip->user_info_buf) + count*PER_INFO_BYTE - 1);
  888. wmb();
  889. NFC_SEND_CMD_ADL(aml_chip->aml_nand_dma_buf_dma_addr);
  890. NFC_SEND_CMD_ADH(aml_chip->aml_nand_dma_buf_dma_addr);
  891. NFC_SEND_CMD_AIL(aml_chip->aml_nand_info_dma_addr);
  892. NFC_SEND_CMD_AIH(aml_chip->aml_nand_info_dma_addr);
  893. if(!bch_mode)
  894. NFC_SEND_CMD_M2N_RAW(aml_chip->ran_mode,len);
  895. else
  896. NFC_SEND_CMD_M2N(aml_chip->ran_mode,bch_mode,(aml_chip->short_pgsz==0)?0:1,pgsz,count); //no seed fixme
  897. ret = aml_platform_dma_waiting(aml_chip);
  898. if (ret)
  899. {
  900. aml_nand_debug ("aml_platform_dma_waiting fail\n");
  901. return ret;
  902. }
  903. rmb();
  904. // dma_unmap_single(aml_chip->device,data_dma_addr,len,DMA_TO_DEVICE);
  905. // dma_unmap_single(aml_chip->device,data_dma_addr,count*PER_INFO_BYTE,DMA_FROM_DEVICE);
  906. return ret;
  907. }
  908. static int aml_platform_dma_read(struct aml_nand_chip *aml_chip, unsigned char *buf, int len, unsigned bch_mode)
  909. {
  910. volatile unsigned int * info_buf=0;
  911. volatile int cmp=0;
  912. dma_addr_t data_dma_addr=0;
  913. // dma_addr_t user_data_dma_addr=0;
  914. struct mtd_info *mtd = &aml_chip->mtd;
  915. struct nand_chip *chip = &aml_chip->chip;
  916. // unsigned dma_unit_size=0;
  917. int ret = 0;
  918. unsigned count=0,pgsz=0;
  919. unsigned int slen=PER_INFO_BYTE/sizeof(unsigned int);
  920. count = len/chip->ecc.size;
  921. if(aml_chip->short_pgsz)
  922. pgsz = chip->ecc.size>>3;
  923. if(!bch_mode)
  924. {
  925. count = 1;
  926. }
  927. // data_dma_addr=dma_map_single(aml_chip->device,(void *)buf,len,DMA_BIDIRECTIONAL);
  928. memset((unsigned char *)aml_chip->user_info_buf, 0, count*PER_INFO_BYTE);
  929. dmac_flush_range((unsigned)aml_chip->user_info_buf,count*PER_INFO_BYTE);
  930. clean_dcache_area((unsigned)buf,len);
  931. wmb();
  932. NFC_SEND_CMD_ADL(aml_chip->aml_nand_dma_buf_dma_addr);
  933. NFC_SEND_CMD_ADH(aml_chip->aml_nand_dma_buf_dma_addr);
  934. NFC_SEND_CMD_AIL(aml_chip->aml_nand_info_dma_addr);
  935. NFC_SEND_CMD_AIH(aml_chip->aml_nand_info_dma_addr);
  936. // NFC_SEND_CMD_ADL(data_dma_addr);
  937. // NFC_SEND_CMD_ADH(data_dma_addr);
  938. // NFC_SEND_CMD_AIL(user_data_dma_addr);
  939. // NFC_SEND_CMD_AIH(user_data_dma_addr);
  940. if(!bch_mode)
  941. NFC_SEND_CMD_N2M_RAW(aml_chip->ran_mode,len);
  942. else
  943. NFC_SEND_CMD_N2M(aml_chip->ran_mode,bch_mode,(aml_chip->short_pgsz==0)?0:1,pgsz,count); //FIXME wrong,bch_mode=0x4000
  944. ret = aml_platform_dma_waiting(aml_chip);
  945. if (ret)
  946. {
  947. aml_nand_debug ("aml_platform_dma_waiting fail\n");
  948. return ret;
  949. }
  950. do{
  951. // flush_icache_range((unsigned long )aml_chip->user_info_buf, (unsigned long )aml_chip->user_info_buf+count*PER_INFO_BYTE+64);
  952. info_buf=(volatile unsigned *)&(aml_chip->user_info_buf[(count-1)*slen]);
  953. cmp = *info_buf;
  954. }while((cmp)==0);
  955. rmb();
  956. // dma_sync_single_for_cpu(aml_chip->device,data_dma_addr,len,DMA_BIDIRECTIONAL);
  957. // dma_unmap_single(aml_chip->device,data_dma_addr,len,DMA_BIDIRECTIONAL);
  958. /*
  959. * // while(NAND_INFO_DONE(*info_buf) == 0);
  960. // rmb();
  961. if (buf != aml_chip->aml_nand_data_buf)
  962. memcpy(buf, aml_chip->aml_nand_data_buf, len);
  963. wmb();*/
  964. // dma_unmap_single(aml_chip->device,data_dma_addr,len,DMA_FROM_DEVICE);
  965. // dma_unmap_single(aml_chip->device,data_dma_addr,(mtd->writesize/chip->ecc.size)*PER_INFO_BYTE,DMA_FROM_DEVICE);
  966. //
  967. return 0;
  968. }
  969. static int aml_platform_hwecc_correct(struct aml_nand_chip *aml_chip, unsigned char *buf, unsigned size, unsigned char *oob_buf)
  970. {
  971. struct nand_chip *chip = &aml_chip->chip;
  972. struct mtd_info *mtd = &aml_chip->mtd;
  973. unsigned int len=PER_INFO_BYTE/sizeof(unsigned int);
  974. unsigned ecc_step_num;
  975. //#define INFO_BYTE 8
  976. if (size % chip->ecc.size) {
  977. aml_nand_debug ("error parameter size for ecc correct %x\n", size);
  978. return -EINVAL;
  979. }
  980. for (ecc_step_num = 0; ecc_step_num < (size / chip->ecc.size); ecc_step_num++) {
  981. //check if there have uncorrectable sector
  982. if(NAND_ECC_CNT(*(unsigned *)(&aml_chip->user_info_buf[ecc_step_num*len]))==63)
  983. //if (NAND_ECC_FAIL(aml_chip->user_info_buf[ecc_step_num]))
  984. {
  985. aml_nand_debug ("nand communication have uncorrectable ecc error ecc_step_num=%d\n",ecc_step_num);
  986. return -EIO;
  987. }
  988. else {
  989. mtd->ecc_stats.corrected += NAND_ECC_CNT(*(unsigned *)(&aml_chip->user_info_buf[ecc_step_num*len]));
  990. }
  991. }
  992. return 0;
  993. }
  994. //nandchip cb before scan
  995. static void aml_nand_dma_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  996. {
  997. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  998. aml_chip->aml_nand_dma_read(aml_chip, buf, len, 0);
  999. }
  1000. static void aml_nand_dma_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  1001. {
  1002. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1003. aml_chip->aml_nand_dma_write(aml_chip, (unsigned char *)buf, len, 0);
  1004. }
  1005. static int aml_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  1006. {
  1007. struct nand_chip * chip = mtd->priv;
  1008. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1009. struct mtd_oob_ops aml_oob_ops;
  1010. int32_t ret = 0, read_cnt, page;
  1011. uint32_t failed;
  1012. chip->pagebuf = -1;
  1013. //return 0;
  1014. if (getchip) {
  1015. aml_oob_ops.mode = MTD_OOB_AUTO;
  1016. aml_oob_ops.len = 0;//mtd->writesize;
  1017. aml_oob_ops.ooblen = mtd->oobavail;
  1018. aml_oob_ops.ooboffs = chip->ecc.layout->oobfree[0].offset;
  1019. aml_oob_ops.datbuf = NULL;//chip->buffers->databuf;
  1020. aml_oob_ops.oobbuf = chip->oob_poi;
  1021. for (read_cnt=0; read_cnt<1; read_cnt++) {
  1022. failed=mtd->ecc_stats.failed;
  1023. ret = mtd->read_oob(mtd, ofs, &aml_oob_ops);
  1024. if (ret == -EUCLEAN)
  1025. ret = 0;
  1026. if(ret<0||mtd->ecc_stats.failed!=failed)
  1027. aml_nand_debug(" NAND detect Bad block at %llx %d\n", (uint64_t)ofs,ret);
  1028. if ((aml_oob_ops.oobbuf[chip->badblockpos] != 0) && (ret == 0))
  1029. return 0;
  1030. }
  1031. if (ret < 0) {
  1032. aml_nand_debug(" NAND detect Bad block at %llx %d\n", (uint64_t)ofs, ret);
  1033. return EFAULT;
  1034. }
  1035. if (aml_oob_ops.oobbuf[chip->badblockpos] == 0) {
  1036. memset(aml_chip->aml_nand_data_buf, 0, (mtd->writesize + mtd->oobsize));
  1037. if (!memcmp(aml_chip->aml_nand_data_buf + mtd->writesize, aml_oob_ops.oobbuf, aml_oob_ops.ooblen)) {
  1038. aml_nand_debug(" NAND detect Bad block at %llx \n", (uint64_t)ofs);
  1039. return -EFAULT;
  1040. }
  1041. }
  1042. }
  1043. else {
  1044. page = (int)(ofs >> chip->page_shift);
  1045. failed=mtd->ecc_stats.failed;
  1046. ret = chip->ecc.read_oob(mtd, chip, page, 1);
  1047. if (ret == -EUCLEAN)
  1048. ret = 0;
  1049. if (ret < 0)
  1050. return -EFAULT;
  1051. if (chip->oob_poi[chip->badblockpos] == 0xFF)
  1052. return 0;
  1053. if (chip->oob_poi[chip->badblockpos] == 0||mtd->ecc_stats.failed!=failed) {
  1054. memset(aml_chip->aml_nand_data_buf, 0, (mtd->writesize + mtd->oobsize));
  1055. if (!memcmp(aml_chip->aml_nand_data_buf + mtd->writesize, chip->oob_poi, mtd->oobavail)) {
  1056. aml_nand_debug(" NAND detect Bad block at %llx \n", (uint64_t)ofs);
  1057. return -EFAULT;
  1058. }
  1059. }
  1060. }
  1061. return 0;
  1062. }
  1063. static int aml_nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1064. {
  1065. struct nand_chip * chip = mtd->priv;
  1066. struct mtd_oob_ops aml_oob_ops;
  1067. aml_oob_ops.mode = MTD_OOB_AUTO;
  1068. aml_oob_ops.len = mtd->writesize;
  1069. aml_oob_ops.ooblen = mtd->oobavail;
  1070. aml_oob_ops.ooboffs = chip->ecc.layout->oobfree[0].offset;
  1071. aml_oob_ops.datbuf = chip->buffers->databuf;
  1072. aml_oob_ops.oobbuf = chip->oob_poi;
  1073. chip->pagebuf = -1;
  1074. memset((unsigned char *)aml_oob_ops.datbuf, 0x0, mtd->writesize);
  1075. memset((unsigned char *)aml_oob_ops.oobbuf, 0x0, aml_oob_ops.ooblen);
  1076. return mtd->write_oob(mtd, ofs, &aml_oob_ops);
  1077. }
  1078. static void aml_nand_select_chip(struct mtd_info *mtd, int chipnr)
  1079. {
  1080. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1081. #if 0
  1082. if (((nand_erarly_suspend_flag == 1) && (!(READ_CBUS_REG(HHI_MPEG_CLK_CNTL)&(1<<8))))
  1083. || ((READ_CBUS_REG(HHI_MPEG_CLK_CNTL)&(1<<8)) && (nand_erarly_suspend_flag == 2))){
  1084. aml_chip->aml_nand_hw_init(aml_chip);
  1085. if (nand_erarly_suspend_flag == 1)
  1086. nand_erarly_suspend_flag = 2;
  1087. else if (nand_erarly_suspend_flag == 2)
  1088. nand_erarly_suspend_flag = 0;
  1089. }
  1090. #endif
  1091. switch (chipnr) {
  1092. case -1:
  1093. NFC_SEND_CMD_STANDBY(3);
  1094. // nand_release_chip();
  1095. break;
  1096. case 0:
  1097. // printk("NAND_CMD =0x%x\n",CBUS_REG_ADDR(NAND_CMD));
  1098. // printk("NAND_CFG =0x%x\n",CBUS_REG_ADDR(NAND_CFG));
  1099. nand_get_chip(); /*FIXME A3 rb0 rb1 ce2 ce3*/
  1100. aml_chip->aml_nand_select_chip(aml_chip, chipnr);
  1101. break;
  1102. case 1:
  1103. case 2:
  1104. case 3:
  1105. aml_chip->aml_nand_select_chip(aml_chip, chipnr);
  1106. break;
  1107. default:
  1108. BUG();
  1109. }
  1110. return;
  1111. }
  1112. static void aml_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  1113. {
  1114. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1115. aml_chip->aml_nand_cmd_ctrl(aml_chip, cmd, ctrl);
  1116. }
  1117. #if CONFIG_AM_NAND_RBPIN
  1118. static int aml_nand_dev_ready(struct mtd_info *mtd)
  1119. {
  1120. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1121. if(aml_chip->rbpin_mode)
  1122. return NFC_GET_RB_STATUS(aml_chip->rb_received);
  1123. else
  1124. {
  1125. BUG();
  1126. return 1;
  1127. /*
  1128. *
  1129. *
  1130. if(state==0)
  1131. {
  1132. for( i=0;i<800;i++);
  1133. return 1;
  1134. }
  1135. NFC_SEND_CMD(CE0|IDLE | 0);
  1136. NFC_SEND_CMD(CE0|CLE|0x70);
  1137. NFC_SEND_CMD(CE0|IDLE | 0);
  1138. NFC_SEND_CMD(RB|IO6|28);
  1139. NFC_SEND_CMD(CE0|IDLE | 0);
  1140. while(NFC_CMDFIFO_SIZE()>0);
  1141. // NFC_SEND_CMD(CE0|DRD|3);
  1142. if (state == FL_READING)
  1143. {
  1144. tmp=NFC_CMD_CLE(CE0,0);
  1145. NFC_SEND_CMD(tmp);
  1146. NFC_SEND_CMD(CE0|IDLE | 10);
  1147. for( i=0;i<100;i++);
  1148. }
  1149. while(NFC_CMDFIFO_SIZE()>0);
  1150. return 1;*/
  1151. }
  1152. }
  1153. #endif
  1154. static int aml_nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  1155. {
  1156. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1157. struct nand_chip *chip = mtd->priv;
  1158. chip->read_buf(mtd, aml_chip->aml_nand_data_buf, len);
  1159. if (memcmp(buf, aml_chip->aml_nand_data_buf, len))
  1160. return -EFAULT;
  1161. return 0;
  1162. }
  1163. static uint8_t aml_platform_read_byte(struct mtd_info *mtd)
  1164. {
  1165. struct nand_chip *chip = mtd->priv;
  1166. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1167. // NFC_CMD_FIFO_RESET();
  1168. NFC_SEND_CMD(aml_chip->chip_selected | DRD | 0);
  1169. NFC_SEND_CMD(aml_chip->chip_selected | IDLE | 5);
  1170. while(NFC_CMDFIFO_SIZE()>0);
  1171. return readb(chip->IO_ADDR_R);
  1172. }
  1173. static int aml_nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, uint8_t *buf, int page)
  1174. {
  1175. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1176. unsigned nand_page_size = aml_chip->page_size;
  1177. unsigned nand_oob_size = aml_chip->oob_size;
  1178. uint8_t *oob_buf = chip->oob_poi;
  1179. int i, error = 0, j = 0, page_addr, internal_chipnr = 1;
  1180. page_addr = aml_chip->page_addr;
  1181. if (aml_chip->ops_mode & AML_INTERLEAVING_MODE)
  1182. internal_chipnr = aml_chip->internal_chipnr;
  1183. for (i=0; i<aml_chip->chip_num; i++) {
  1184. if (aml_chip->valid_chip[i]) {
  1185. for (j=0; j<internal_chipnr; j++) {
  1186. if (j > 0) {
  1187. page_addr |= (1 << aml_chip->internal_chip_shift) * j;
  1188. aml_chip->aml_nand_select_chip(aml_chip, i);
  1189. aml_chip->aml_nand_command(aml_chip, NAND_CMD_READ0, 0, page_addr, i);
  1190. }
  1191. if (!aml_chip->aml_nand_wait_devready(aml_chip, i)) {
  1192. aml_nand_debug ("couldn`t found selected chip: %d ready\n", i);
  1193. error = -EBUSY;
  1194. goto exit;
  1195. }
  1196. if (aml_chip->plane_num == 2) {
  1197. aml_chip->aml_nand_command(aml_chip, NAND_CMD_TWOPLANE_READ1, 0x00, page_addr, i);
  1198. // chip->read_buf(mtd, aml_chip->aml_nand_data_buf, (nand_page_size + nand_oob_size));
  1199. aml_chip->aml_nand_dma_read(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size + nand_oob_size, 0);
  1200. memcpy(buf, aml_chip->aml_nand_data_buf, (nand_page_size + nand_oob_size));
  1201. memcpy(oob_buf, aml_chip->aml_nand_data_buf + nand_page_size, nand_oob_size);
  1202. oob_buf += nand_oob_size;
  1203. buf += (nand_page_size + nand_oob_size);
  1204. aml_chip->aml_nand_command(aml_chip, NAND_CMD_TWOPLANE_READ2, 0x00, page_addr, i);
  1205. // chip->read_buf(mtd, aml_chip->aml_nand_data_buf, (nand_page_size + nand_oob_size));
  1206. aml_chip->aml_nand_dma_read(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size + nand_oob_size, 0);
  1207. memcpy(buf, aml_chip->aml_nand_data_buf, (nand_page_size + nand_oob_size));
  1208. memcpy(oob_buf, aml_chip->aml_nand_data_buf + nand_page_size, nand_oob_size);
  1209. oob_buf += nand_oob_size;
  1210. buf += (nand_page_size + nand_oob_size);
  1211. }
  1212. else if (aml_chip->plane_num == 1) {
  1213. // chip->read_buf(mtd, aml_chip->aml_nand_data_buf, (nand_page_size + nand_oob_size));
  1214. aml_chip->aml_nand_dma_read(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size + nand_oob_size, 0);
  1215. memcpy(buf, aml_chip->aml_nand_data_buf, nand_page_size);
  1216. memcpy(oob_buf, aml_chip->aml_nand_data_buf + nand_page_size, nand_oob_size);
  1217. oob_buf += nand_oob_size;
  1218. buf += nand_page_size;
  1219. }
  1220. else {
  1221. error = -ENODEV;
  1222. aml_nand_debug ("plane_num mistake\n");
  1223. goto exit;
  1224. }
  1225. }
  1226. }
  1227. }
  1228. exit:
  1229. return error;
  1230. }
  1231. static void aml_nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, const uint8_t *buf)
  1232. {
  1233. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1234. unsigned nand_page_size = aml_chip->page_size; //real size
  1235. unsigned nand_oob_size = aml_chip->oob_size;
  1236. uint8_t *oob_buf = chip->oob_poi;
  1237. int i, error = 0, j = 0, page_addr, internal_chipnr = 1;
  1238. page_addr = aml_chip->page_addr;
  1239. if (aml_chip->ops_mode & AML_INTERLEAVING_MODE)
  1240. internal_chipnr = aml_chip->internal_chipnr;
  1241. for (i=0; i<aml_chip->chip_num; i++) {
  1242. if (aml_chip->valid_chip[i]) {
  1243. aml_chip->aml_nand_select_chip(aml_chip, i);
  1244. for (j=0; j<internal_chipnr; j++) {
  1245. if (j > 0) {
  1246. page_addr |= (1 << aml_chip->internal_chip_shift) * j;
  1247. aml_chip->aml_nand_command(aml_chip, NAND_CMD_SEQIN, 0, page_addr, i);
  1248. }
  1249. if (aml_chip->plane_num == 2) {
  1250. memcpy(aml_chip->aml_nand_data_buf, buf, nand_page_size);
  1251. memcpy(aml_chip->aml_nand_data_buf + nand_page_size, oob_buf, nand_oob_size);
  1252. // chip->write_buf(mtd, aml_chip->aml_nand_data_buf, (nand_page_size + nand_oob_size));
  1253. aml_chip->aml_nand_dma_write(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size + nand_oob_size, 0);
  1254. aml_chip->aml_nand_command(aml_chip, NAND_CMD_DUMMY_PROGRAM, -1, -1, i);
  1255. oob_buf += nand_oob_size;
  1256. buf += nand_page_size;
  1257. if (!aml_chip->aml_nand_wait_devready(aml_chip, i)) {
  1258. aml_nand_debug ("couldn`t found selected chip: %d ready\n", i);
  1259. error = -EBUSY;
  1260. goto exit;
  1261. }
  1262. memcpy(aml_chip->aml_nand_data_buf, buf, nand_page_size);
  1263. memcpy(aml_chip->aml_nand_data_buf + nand_page_size, oob_buf, nand_oob_size);
  1264. aml_chip->aml_nand_command(aml_chip, NAND_CMD_TWOPLANE_WRITE2, 0x00, page_addr, i);
  1265. // chip->write_buf(mtd, aml_chip->aml_nand_data_buf, (nand_page_size + nand_oob_size));
  1266. aml_chip->aml_nand_dma_write(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size + nand_oob_size, 0);
  1267. aml_chip->aml_nand_command(aml_chip, NAND_CMD_PAGEPROG, -1, -1, i);
  1268. oob_buf += nand_oob_size;
  1269. buf += nand_page_size;
  1270. }
  1271. else if (aml_chip->plane_num == 1) {
  1272. memcpy(aml_chip->aml_nand_data_buf, buf, nand_page_size);
  1273. memcpy(aml_chip->aml_nand_data_buf + nand_page_size, oob_buf, nand_oob_size);
  1274. //chip->write_buf(mtd, aml_chip->aml_nand_data_buf, (nand_page_size + nand_oob_size));
  1275. aml_chip->aml_nand_dma_write(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size + nand_oob_size, 0);
  1276. if (chip->cmdfunc == aml_nand_cmdfunc)
  1277. aml_chip->aml_nand_command(aml_chip, NAND_CMD_PAGEPROG, -1, -1, i);
  1278. oob_buf += nand_oob_size;
  1279. buf += nand_page_size;
  1280. }
  1281. else {
  1282. error = -ENODEV;
  1283. aml_nand_debug ("plane_num mistake\n");
  1284. goto exit;
  1285. }
  1286. }
  1287. }
  1288. }
  1289. exit:
  1290. return ;
  1291. }
  1292. static int aml_nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, uint8_t *buf, int page)
  1293. {
  1294. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1295. uint8_t *oob_buf = chip->oob_poi;
  1296. unsigned nand_page_size = aml_chip->page_size; //real size
  1297. unsigned pages_per_blk_shift = (chip->phys_erase_shift - chip->page_shift);
  1298. int user_byte_num = (((nand_page_size + chip->ecc.size - 1) / chip->ecc.size) * aml_chip->user_byte_mode);
  1299. int error = 0, i = 0, stat = 0, j = 0, page_addr, internal_chipnr = 1;
  1300. if(!strcmp(mtd->name,NAND_BOOT_NAME))
  1301. {
  1302. nand_page_size=3*512; //compate older ,corresponding to applictaion
  1303. }
  1304. page_addr = aml_chip->page_addr;
  1305. if (aml_chip->ops_mode & AML_INTERLEAVING_MODE)
  1306. internal_chipnr = aml_chip->internal_chipnr;
  1307. for (i=0; i<aml_chip->chip_num; i++) {
  1308. if (aml_chip->valid_chip[i]) {
  1309. for (j=0; j<internal_chipnr; j++) {
  1310. if (j > 0) {
  1311. page_addr |= (1 << aml_chip->internal_chip_shift) * j;
  1312. aml_chip->aml_nand_select_chip(aml_chip, i);
  1313. aml_chip->aml_nand_command(aml_chip, NAND_CMD_READ0, 0, page_addr, i);
  1314. }
  1315. if (!aml_chip->aml_nand_wait_devready(aml_chip, i)) {
  1316. aml_nand_debug ("read couldn`t found selected chip: %d ready\n", i);
  1317. error = -EBUSY;
  1318. goto exit;
  1319. }
  1320. if (aml_chip->plane_num == 2) {
  1321. aml_chip->aml_nand_command(aml_chip, NAND_CMD_TWOPLANE_READ1, 0x00, page_addr, i);
  1322. if(0/*buf > PAGE_OFFSET*/)
  1323. error = aml_chip->aml_nand_dma_read(aml_chip, buf, nand_page_size, aml_chip->bch_mode);
  1324. else{
  1325. error = aml_chip->aml_nand_dma_read(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size, aml_chip->bch_mode);
  1326. memcpy(buf, aml_chip->aml_nand_data_buf, nand_page_size);
  1327. }
  1328. if (error){
  1329. aml_nand_debug ("aml_nand_dma_read plane 0 fail\n");
  1330. goto exit;
  1331. }
  1332. aml_chip->aml_nand_get_user_byte(aml_chip, oob_buf, user_byte_num);
  1333. stat = aml_chip->aml_nand_hwecc_correct(aml_chip, buf, nand_page_size, oob_buf);
  1334. if (stat < 0) {
  1335. mtd->ecc_stats.failed++;
  1336. aml_nand_debug("aml_nand_hwecc_correct plane0 failed at page %d chip %d\n", page_addr, i);
  1337. // error = -EIO;goto exit;
  1338. }
  1339. else
  1340. mtd->ecc_stats.corrected += stat;
  1341. oob_buf += user_byte_num;
  1342. buf += nand_page_size;
  1343. aml_chip->aml_nand_command(aml_chip, NAND_CMD_TWOPLANE_READ2, 0x00, page_addr, i);
  1344. if(0/*buf > PAGE_OFFSET*/)
  1345. error = aml_chip->aml_nand_dma_read(aml_chip, buf, nand_page_size, aml_chip->bch_mode);
  1346. else{
  1347. error = aml_chip->aml_nand_dma_read(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size, aml_chip->bch_mode);
  1348. memcpy(buf, aml_chip->aml_nand_data_buf, nand_page_size);
  1349. }
  1350. if (error){
  1351. aml_nand_debug ("aml_nand_dma_read plane 1 fail\n");
  1352. goto exit;
  1353. }
  1354. aml_chip->aml_nand_get_user_byte(aml_chip, oob_buf, user_byte_num);
  1355. stat = aml_chip->aml_nand_hwecc_correct(aml_chip, buf, nand_page_size, oob_buf);
  1356. if (stat < 0) {
  1357. mtd->ecc_stats.failed++;
  1358. aml_nand_debug("aml_nand_hwecc_correct plane1 failed at page %d chip %d\n", page_addr , i);
  1359. // error = -EIO;goto exit;
  1360. }
  1361. else
  1362. mtd->ecc_stats.corrected += stat;
  1363. oob_buf += user_byte_num;
  1364. buf += nand_page_size;
  1365. }
  1366. else if (aml_chip->plane_num == 1) {
  1367. if(0/*buf > PAGE_OFFSET*/)
  1368. error = aml_chip->aml_nand_dma_read(aml_chip, buf, nand_page_size, aml_chip->bch_mode);
  1369. else{
  1370. error = aml_chip->aml_nand_dma_read(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size, aml_chip->bch_mode);
  1371. memcpy(buf, aml_chip->aml_nand_data_buf, nand_page_size);
  1372. }
  1373. if (error)
  1374. {
  1375. aml_nand_debug("aml_nand_dma_read ecc failed at blk %d chip %d\n", (page_addr >> pages_per_blk_shift), i);
  1376. goto exit;
  1377. }
  1378. aml_chip->aml_nand_get_user_byte(aml_chip, oob_buf, user_byte_num);
  1379. stat = aml_chip->aml_nand_hwecc_correct(aml_chip, buf, nand_page_size, oob_buf);
  1380. if (stat < 0) {
  1381. mtd->ecc_stats.failed++;
  1382. aml_nand_debug("aml_nand_hwecc_correct at blk %d chip %d\n", (page_addr >> pages_per_blk_shift), i);
  1383. // error = -EIO;goto exit;
  1384. }
  1385. else
  1386. mtd->ecc_stats.corrected += stat;
  1387. oob_buf += user_byte_num;
  1388. buf += nand_page_size;
  1389. }
  1390. else {
  1391. error = -ENODEV;
  1392. aml_nand_debug ("plane_num mistake\n");
  1393. goto exit;
  1394. }
  1395. }
  1396. }
  1397. }
  1398. exit:
  1399. return error;
  1400. }
  1401. static void aml_nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, const uint8_t *buf)
  1402. {
  1403. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1404. uint8_t *oob_buf = chip->oob_poi;
  1405. unsigned nand_page_size = aml_chip->page_size; //real size
  1406. int user_byte_num = (((nand_page_size + chip->ecc.size - 1) / chip->ecc.size) * aml_chip->user_byte_mode);
  1407. int error = 0, i = 0, j = 0, page_addr, internal_chipnr = 1;
  1408. if(!strcmp(mtd->name,NAND_BOOT_NAME))
  1409. {
  1410. nand_page_size=3*512; //compate older , corresponding to applictaion
  1411. }
  1412. page_addr = aml_chip->page_addr;
  1413. if (aml_chip->ops_mode & AML_INTERLEAVING_MODE)
  1414. internal_chipnr = aml_chip->internal_chipnr;
  1415. for (i=0; i<aml_chip->chip_num; i++) {
  1416. if (aml_chip->valid_chip[i]) {
  1417. aml_chip->aml_nand_select_chip(aml_chip, i);
  1418. if (i > 0) {
  1419. if (!aml_chip->aml_nand_wait_devready(aml_chip, i)) {
  1420. aml_nand_debug ("chip: %d: busy\n", i);
  1421. error = -EBUSY;
  1422. goto exit;
  1423. }
  1424. aml_chip->aml_nand_command(aml_chip, NAND_CMD_SEQIN, 0, page_addr, i);
  1425. }
  1426. for (j=0; j<internal_chipnr; j++) {
  1427. if (j > 0) {
  1428. page_addr |= (1 << aml_chip->internal_chip_shift) * j;
  1429. aml_chip->aml_nand_command(aml_chip, NAND_CMD_SEQIN, 0, page_addr, i);
  1430. }
  1431. if (aml_chip->plane_num == 2) {
  1432. //printk("write buf %x\n", buf);
  1433. aml_chip->aml_nand_set_user_byte(aml_chip, oob_buf, user_byte_num);
  1434. if(0/*buf > PAGE_OFFSET*/)
  1435. error = aml_chip->aml_nand_dma_write(aml_chip, (unsigned char *)buf, nand_page_size, aml_chip->bch_mode);
  1436. else{
  1437. memcpy(aml_chip->aml_nand_data_buf, buf, nand_page_size);
  1438. error = aml_chip->aml_nand_dma_write(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size, aml_chip->bch_mode);
  1439. }
  1440. if (error){
  1441. aml_nand_debug ("aml_nand_dma_write plane 0 fail\n" );
  1442. goto exit;
  1443. }
  1444. aml_chip->aml_nand_command(aml_chip, NAND_CMD_DUMMY_PROGRAM, -1, -1, i);
  1445. oob_buf += user_byte_num;
  1446. buf += nand_page_size;
  1447. if (!aml_chip->aml_nand_wait_devready(aml_chip, i)) {
  1448. aml_nand_debug ("write couldn`t found selected chip: %d ready\n", i);
  1449. error = -EBUSY;
  1450. goto exit;
  1451. }
  1452. aml_chip->aml_nand_command(aml_chip, NAND_CMD_TWOPLANE_WRITE2, 0x00, page_addr, i);
  1453. aml_chip->aml_nand_set_user_byte(aml_chip, oob_buf, user_byte_num);
  1454. if(0/*buf > PAGE_OFFSET*/)
  1455. error = aml_chip->aml_nand_dma_write(aml_chip, (unsigned char *)buf, nand_page_size, aml_chip->bch_mode);
  1456. else{
  1457. memcpy(aml_chip->aml_nand_data_buf, buf, nand_page_size);
  1458. error = aml_chip->aml_nand_dma_write(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size, aml_chip->bch_mode);
  1459. }
  1460. if (error){
  1461. aml_nand_debug ("aml_nand_dma_write plane 1 fail\n");
  1462. goto exit;
  1463. }
  1464. if (aml_chip->cached_prog_status)
  1465. aml_chip->aml_nand_command(aml_chip, NAND_CMD_CACHEDPROG, -1, -1, i);
  1466. else
  1467. aml_chip->aml_nand_command(aml_chip, NAND_CMD_PAGEPROG, -1, -1, i);
  1468. oob_buf += user_byte_num;
  1469. buf += nand_page_size;
  1470. }
  1471. else if (aml_chip->plane_num == 1) {
  1472. aml_chip->aml_nand_set_user_byte(aml_chip, oob_buf, user_byte_num);
  1473. if(0/*buf > PAGE_OFFSET*/)
  1474. error = aml_chip->aml_nand_dma_write(aml_chip, (unsigned char *)buf, nand_page_size, aml_chip->bch_mode);
  1475. else{
  1476. memcpy(aml_chip->aml_nand_data_buf, buf, nand_page_size);
  1477. error = aml_chip->aml_nand_dma_write(aml_chip, aml_chip->aml_nand_data_buf, nand_page_size, aml_chip->bch_mode);
  1478. }
  1479. if (error){
  1480. aml_nand_debug ("aml_nand_dma_write fail\n");
  1481. goto exit;
  1482. }
  1483. if (chip->cmdfunc == aml_nand_cmdfunc) {
  1484. if (aml_chip->cached_prog_status)
  1485. aml_chip->aml_nand_command(aml_chip, NAND_CMD_CACHEDPROG, -1, -1, i);
  1486. else
  1487. aml_chip->aml_nand_command(aml_chip, NAND_CMD_PAGEPROG, -1, -1, i);
  1488. }
  1489. oob_buf += user_byte_num;
  1490. buf += nand_page_size;
  1491. }
  1492. else {
  1493. error = -ENODEV;
  1494. aml_nand_debug ("plane_num mistake\n");
  1495. goto exit;
  1496. }
  1497. }
  1498. }
  1499. }
  1500. exit:
  1501. return;
  1502. }
  1503. static int aml_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip, int page, int sndcmd)
  1504. {
  1505. int32_t error = 0, i, stat = 0, j = 0, page_addr, internal_chipnr = 1;
  1506. unsigned dma_once_size;
  1507. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1508. unsigned char *nand_buffer = aml_chip->aml_nand_data_buf;
  1509. unsigned char *oob_buffer = chip->oob_poi;
  1510. unsigned nand_page_size = aml_chip->page_size; //real size
  1511. unsigned nand_read_size = ((mtd->oobavail / aml_chip->user_byte_mode) * chip->ecc.size);
  1512. unsigned read_chip_num = (((nand_read_size + (aml_chip->plane_num * nand_page_size) - 1) / (aml_chip->plane_num * nand_page_size)));
  1513. unsigned pages_per_blk_shift = (chip->phys_erase_shift - chip->page_shift);
  1514. int user_byte_num = (((nand_page_size + chip->ecc.size - 1) / chip->ecc.size) * aml_chip->user_byte_mode);
  1515. //chip->pagebuf = -1;
  1516. page_addr = page;
  1517. if (aml_chip->ops_mode & AML_INTERLEAVING_MODE) {
  1518. internal_chipnr = aml_chip->internal_chipnr;
  1519. internal_chipnr = (read_chip_num + aml_chip->internal_chipnr - 1) / aml_chip->internal_chipnr;
  1520. read_chip_num = 1;
  1521. }
  1522. if (sndcmd) {
  1523. if (chip->cmdfunc == aml_nand_cmdfunc)
  1524. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page_addr);
  1525. else {
  1526. aml_chip->aml_nand_select_chip(aml_chip, 0);
  1527. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page_addr);
  1528. }
  1529. sndcmd = 0;
  1530. }
  1531. page_addr = aml_chip->page_addr;
  1532. if(!strcmp(mtd->name,NAND_BOOT_NAME))
  1533. {
  1534. nand_read_size=3*512; //compate older ,corresponding to applictaion
  1535. }
  1536. for (i=0; i<read_chip_num; i++) {
  1537. if (aml_chip->valid_chip[i]) {
  1538. if (i > 0) {
  1539. aml_chip->aml_nand_select_chip(aml_chip, i);
  1540. aml_chip->aml_nand_command(aml_chip, NAND_CMD_READ0, 0, page_addr, i);
  1541. }
  1542. for (j=0; j<internal_chipnr; j++) {
  1543. if (j > 0) {
  1544. page_addr |= (1 << aml_chip->internal_chip_shift) * j;
  1545. aml_chip->aml_nand_select_chip(aml_chip, i);
  1546. aml_chip->aml_nand_command(aml_chip, NAND_CMD_READ0, 0, page_addr, i);
  1547. }
  1548. if (!aml_chip->aml_nand_wait_devready(aml_chip, i)) {
  1549. aml_nand_debug ("read oob couldn`t found selected chip: %d ready\n", i);
  1550. error = -EBUSY;
  1551. goto exit;
  1552. }
  1553. if (aml_chip->plane_num == 2) {
  1554. if (nand_read_size < nand_page_size)
  1555. dma_once_size = nand_read_size;
  1556. else
  1557. dma_once_size = nand_page_size;
  1558. aml_chip->aml_nand_command(aml_chip, NAND_CMD_TWOPLANE_READ1, 0x00, page_addr, i);
  1559. error = aml_chip->aml_nand_dma_read(aml_chip, nand_buffer, dma_once_size, aml_chip->bch_mode);
  1560. if (error){
  1561. aml_nand_debug("aml_nand_dma_read plane 0 failed %d\n", error);
  1562. return error;
  1563. }
  1564. aml_chip->aml_nand_get_user_byte(aml_chip, oob_buffer, user_byte_num);
  1565. stat = aml_chip->aml_nand_hwecc_correct(aml_chip, nand_buffer, dma_once_size, oob_buffer);
  1566. if (stat < 0) {
  1567. mtd->ecc_stats.failed++;
  1568. aml_nand_debug("aml_nand_hwecc_correct plane0 failed at page 0x%x\n", page_addr, i);
  1569. // error = -EIO;goto exit;
  1570. }
  1571. else
  1572. mtd->ecc_stats.corrected += stat;
  1573. oob_buffer += user_byte_num;
  1574. nand_read_size -= dma_once_size;
  1575. if (nand_read_size > 0) {
  1576. if (nand_read_size < nand_page_size)
  1577. dma_once_size = nand_read_size;
  1578. else
  1579. dma_once_size = nand_page_size;
  1580. aml_chip->aml_nand_command(aml_chip, NAND_CMD_TWOPLANE_READ2, 0x00, page_addr, i);
  1581. error = aml_chip->aml_nand_dma_read(aml_chip, nand_buffer, dma_once_size, aml_chip->bch_mode);
  1582. if (error){
  1583. aml_nand_debug("aml_nand_dma_read plane 1 failed %d\n", error);
  1584. return error;
  1585. }
  1586. aml_chip->aml_nand_get_user_byte(aml_chip, oob_buffer, user_byte_num);
  1587. stat = aml_chip->aml_nand_hwecc_correct(aml_chip, nand_buffer, dma_once_size, oob_buffer);
  1588. if (stat < 0) {
  1589. mtd->ecc_stats.failed++;
  1590. aml_nand_debug("aml_nand_hwecc_correct plane1 failed at page 0x%x\n", page_addr);
  1591. // error = -EIO;goto exit;
  1592. }
  1593. else
  1594. mtd->ecc_stats.corrected += stat;
  1595. oob_buffer += user_byte_num;
  1596. nand_read_size -= dma_once_size;
  1597. }
  1598. }
  1599. else if (aml_chip->plane_num == 1) {
  1600. if (nand_read_size < nand_page_size)
  1601. dma_once_size = nand_read_size;
  1602. else
  1603. dma_once_size = nand_page_size;
  1604. error = aml_chip->aml_nand_dma_read(aml_chip, nand_buffer, dma_once_size, aml_chip->bch_mode);
  1605. if (error) {
  1606. aml_nand_debug("aml_nand_dma_read failed %d\n", error);
  1607. return error;
  1608. }
  1609. aml_chip->aml_nand_get_user_byte(aml_chip, oob_buffer, user_byte_num);
  1610. stat = aml_chip->aml_nand_hwecc_correct(aml_chip, nand_buffer, dma_once_size, oob_buffer);
  1611. if (stat < 0) {
  1612. mtd->ecc_stats.failed++;
  1613. aml_nand_debug("aml_nand_hwecc_correct failed at page %d xxx\n", page_addr);
  1614. }
  1615. else
  1616. mtd->ecc_stats.corrected += stat;
  1617. oob_buffer += user_byte_num;
  1618. nand_read_size -= dma_once_size;
  1619. }
  1620. else {
  1621. error = -ENODEV;
  1622. aml_nand_debug ("plane_num mistake\n");
  1623. goto exit;
  1624. }
  1625. }
  1626. }
  1627. }
  1628. exit:
  1629. return error;
  1630. }
  1631. static int aml_nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
  1632. {
  1633. aml_nand_debug("our host controller`s structure couldn`t support oob write\n");
  1634. BUG();
  1635. return 0;
  1636. }
  1637. //nandchip cb after scan
  1638. static void aml_nand_cmdfunc(struct mtd_info *mtd, unsigned command, int column, int page_addr)
  1639. {
  1640. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1641. struct nand_chip *chip = &aml_chip->chip;
  1642. int i = 0, valid_page_num = 1, internal_chip;
  1643. int status=0;
  1644. //remember address for lower
  1645. if (page_addr != -1) {
  1646. #if 0
  1647. valid_page_num = (mtd->writesize >> chip->page_shift);
  1648. valid_page_num /= aml_chip->plane_num;
  1649. aml_chip->page_addr = page_addr / valid_page_num; //real address save for next op
  1650. if (unlikely(aml_chip->page_addr >= aml_chip->internal_page_nums)) {
  1651. internal_chip = aml_chip->page_addr / aml_chip->internal_page_nums;
  1652. aml_chip->page_addr -= aml_chip->internal_page_nums;
  1653. aml_chip->page_addr |= (1 << aml_chip->internal_chip_shift) * internal_chip;
  1654. }
  1655. #else
  1656. aml_chip->page_addr = page_addr;
  1657. #endif
  1658. }
  1659. /* Emulate NAND_CMD_READOOB */
  1660. if (command == NAND_CMD_READOOB) {
  1661. command = NAND_CMD_READ0;
  1662. }
  1663. if (command == NAND_CMD_READ0) {
  1664. aml_chip->aml_nand_command(aml_chip, command, column, aml_chip->page_addr, 0);
  1665. #if (!CONFIG_AM_NAND_RBPIN)
  1666. status = chip->waitfunc(mtd, chip);
  1667. chip->cmd_ctrl(mtd, NAND_CMD_READMODE, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  1668. // aml_chip->aml_nand_command(aml_chip, NAND_CMD_READ0, -1, -1, i); //read mode for next data out
  1669. // printk("rd page=0x%x, status = 0x%x\n", aml_chip->page_addr,status);
  1670. #endif
  1671. return;
  1672. }
  1673. if (command == NAND_CMD_PAGEPROG) //filter 0x10
  1674. return;
  1675. if (command == NAND_CMD_SEQIN) {
  1676. aml_chip->aml_nand_select_chip(aml_chip, 0);
  1677. aml_chip->aml_nand_command(aml_chip, command, column, aml_chip->page_addr, 0);
  1678. return;
  1679. }
  1680. for (i=0; i<aml_chip->chip_num; i++) {
  1681. if (aml_chip->valid_chip[i]) {
  1682. //active ce for operation chip and send cmd
  1683. aml_chip->aml_nand_wait_devready(aml_chip, i);
  1684. aml_chip->aml_nand_command(aml_chip, command, column, aml_chip->page_addr, i);
  1685. }
  1686. }
  1687. return;
  1688. }
  1689. static int aml_nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
  1690. {
  1691. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1692. int status[MAX_CHIP_NUM], state = chip->state, i = 0, time_cnt = 0;
  1693. status[0] = 0;
  1694. /* Apply this short delay always to ensure that we do wait tWB in
  1695. * any case on any machine. */
  1696. ndelay(100);
  1697. //for (i=0; i<aml_chip->chip_num; i++) {
  1698. if (aml_chip->valid_chip[i]) {
  1699. //active ce for operation chip and send cmd
  1700. aml_chip->aml_nand_select_chip(aml_chip, i);
  1701. if (aml_chip->ops_mode == AML_MULTI_CHIP_SHARE_RB) {
  1702. time_cnt = 0;
  1703. while (time_cnt++ < 0x10000) {
  1704. if (state == FL_ERASING)
  1705. aml_chip->aml_nand_command(aml_chip, NAND_CMD_STATUS_MULTI, -1, -1, i);
  1706. else
  1707. aml_chip->aml_nand_command(aml_chip, NAND_CMD_STATUS, -1, -1, i);
  1708. status[i] = (int)chip->read_byte(mtd);
  1709. if (status[i] & NAND_STATUS_READY_MULTI)
  1710. break;
  1711. udelay(2);
  1712. }
  1713. }
  1714. else {
  1715. if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
  1716. aml_chip->aml_nand_command(aml_chip, NAND_CMD_STATUS_MULTI, -1, -1, i);
  1717. else
  1718. aml_chip->aml_nand_command(aml_chip, NAND_CMD_STATUS, -1, -1, i);
  1719. time_cnt = 0;
  1720. while (time_cnt++ < 0x10000) {
  1721. udelay(9);
  1722. if (chip->dev_ready) {
  1723. if (chip->dev_ready(mtd))
  1724. break;
  1725. } else {
  1726. status[i] = (int)chip->read_byte(mtd);
  1727. if (status[i] & (NAND_STATUS_READY))
  1728. break;
  1729. }
  1730. }
  1731. }
  1732. status[0] |= status[i];
  1733. }
  1734. //}
  1735. if( time_cnt >0x10000)
  1736. {
  1737. aml_nand_debug("NAND_CMD_STATUS time out !\n");
  1738. if(state ==FL_READING) {
  1739. aml_nand_debug("--FL_READING fail !\n");
  1740. }
  1741. if(state ==FL_ERASING) {
  1742. aml_nand_debug("--FL_ERASING fail !\n");
  1743. }
  1744. else if(state ==FL_WRITING ) {
  1745. aml_nand_debug("--FL_WRITING fail !\n");
  1746. }
  1747. }
  1748. else
  1749. {
  1750. if(status[0] &(NAND_STATUS_FAIL|NAND_STATUS_FAIL_N1))
  1751. {
  1752. if(state ==FL_ERASING) {
  1753. aml_nand_debug("FL_ERASING fail !status=0x%x \n",status[0]);
  1754. }
  1755. else if(state ==FL_WRITING ) {
  1756. aml_nand_debug("FL_WRITING fail !status=0x%x \n",status[0]);
  1757. }
  1758. }
  1759. }
  1760. return status[0];
  1761. }
  1762. static void aml_nand_erase_cmd(struct mtd_info *mtd, int page)
  1763. {
  1764. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1765. struct nand_chip *chip = mtd->priv;
  1766. unsigned pages_per_blk_shift = (chip->phys_erase_shift - chip->page_shift);
  1767. unsigned vt_page_num, i = 0, j = 0, internal_chipnr = 1, page_addr, valid_page_num;
  1768. #if 0
  1769. vt_page_num = (mtd->writesize / (1 << chip->page_shift));
  1770. vt_page_num *= (1 << pages_per_blk_shift);
  1771. if (page % vt_page_num)
  1772. return;
  1773. /* Send commands to erase a block */
  1774. page_addr = page;
  1775. valid_page_num = (mtd->writesize >> chip->page_shift);
  1776. valid_page_num /= aml_chip->plane_num;
  1777. page_addr /= valid_page_num;
  1778. #else
  1779. page_addr = page ;
  1780. // vt_page_num = (1 << pages_per_blk_shift )*aml_chip->plane_num;
  1781. // if (page % vt_page_num){
  1782. // aml_nand_debug("skip page 0x%x!\n" ,page);
  1783. // return;
  1784. // }
  1785. #endif
  1786. if (unlikely(page_addr >= aml_chip->internal_page_nums)) {
  1787. internal_chipnr = page_addr / aml_chip->internal_page_nums;
  1788. page_addr -= aml_chip->internal_page_nums;
  1789. page_addr |= (1 << aml_chip->internal_chip_shift) * internal_chipnr;
  1790. }
  1791. if (unlikely(aml_chip->ops_mode & AML_INTERLEAVING_MODE))
  1792. internal_chipnr = aml_chip->internal_chipnr;
  1793. else
  1794. internal_chipnr = 1;
  1795. for (i=0; i<aml_chip->chip_num; i++) {
  1796. if (aml_chip->valid_chip[i]) {
  1797. aml_chip->aml_nand_select_chip(aml_chip, i);
  1798. for (j=0; j<internal_chipnr; j++) {
  1799. if (j > 0)
  1800. page_addr |= (1 << aml_chip->internal_chip_shift) * j;
  1801. aml_chip->aml_nand_command(aml_chip, NAND_CMD_ERASE1, -1, page_addr, i);
  1802. aml_chip->aml_nand_command(aml_chip, NAND_CMD_ERASE2, -1, -1, i);
  1803. }
  1804. }
  1805. }
  1806. return ;
  1807. }
  1808. static int aml_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, const uint8_t *buf, int page, int cached, int raw)
  1809. {
  1810. int status;
  1811. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1812. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1813. if ((cached) && (chip->options & NAND_CACHEPRG))
  1814. aml_chip->cached_prog_status = 1;
  1815. else
  1816. aml_chip->cached_prog_status = 0;
  1817. if (unlikely(raw))
  1818. chip->ecc.write_page_raw(mtd, chip, buf);
  1819. else
  1820. chip->ecc.write_page(mtd, chip, buf);
  1821. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  1822. //chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1823. status = chip->waitfunc(mtd, chip);
  1824. if(status&(NAND_STATUS_FAIL|NAND_STATUS_FAIL_N1)){
  1825. aml_nand_debug("wr page=0x%x, status = 0x%x\n", page,status);
  1826. }
  1827. /*
  1828. * See if operation failed and additional status checks are
  1829. * available
  1830. */
  1831. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1832. status = chip->errstat(mtd, chip, FL_WRITING, status, page);
  1833. if (status & NAND_STATUS_FAIL)
  1834. {
  1835. aml_nand_debug("wr page=0x%x, status = 0x%x\n", page,status);
  1836. return -EIO;
  1837. }
  1838. } else {
  1839. //chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  1840. status = chip->waitfunc(mtd, chip);
  1841. }
  1842. if(status&(NAND_STATUS_FAIL|NAND_STATUS_FAIL_N1)){
  1843. aml_nand_debug("wr page=0x%x, status = 0x%x\n", page,status);
  1844. }
  1845. aml_chip->cached_prog_status = 0;
  1846. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  1847. /* Send command to read back the data */
  1848. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1849. if (chip->verify_buf(mtd, buf, mtd->writesize)) {
  1850. aml_nand_debug("verify_buf not ok at page 0x%x \n",page);
  1851. return -EIO;
  1852. }
  1853. #endif
  1854. return 0;
  1855. }
  1856. #ifdef CONFIG_HAS_EARLYSUSPEND
  1857. static void aml_platform_nand_suspend(struct early_suspend *nand_early_suspend)
  1858. {
  1859. struct aml_nand_chip *aml_chip = (struct aml_nand_chip *)nand_early_suspend->param;
  1860. struct nand_chip *chip = &aml_chip->chip;
  1861. spinlock_t *lock = &chip->controller->lock;
  1862. if (nand_erarly_suspend_flag)
  1863. return;
  1864. spin_lock(lock);
  1865. nand_erarly_suspend_flag = 1;
  1866. spin_unlock(lock);
  1867. aml_nand_debug("aml_m1_nand_early suspend entered\n");
  1868. return;
  1869. }
  1870. static void aml_platform_nand_resume(struct early_suspend *nand_early_suspend)
  1871. {
  1872. /*struct aml_nand_chip *aml_chip = (struct aml_nand_chip *)nand_early_suspend->param;
  1873. struct nand_chip *chip = &aml_chip->chip;
  1874. spinlock_t *lock = &chip->controller->lock;*/
  1875. aml_nand_debug("aml_m1_nand_early resume entered\n");
  1876. return;
  1877. }
  1878. #endif
  1879. static int aml_nand_suspend(struct mtd_info *mtd)
  1880. {
  1881. /*struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1882. if (nand_erarly_suspend_flag){
  1883. aml_chip->aml_nand_hw_init(aml_chip);
  1884. NFC_SET_TIMING(0, 5, -6);
  1885. if (nand_erarly_suspend_flag)
  1886. nand_erarly_suspend_flag = 0;
  1887. }*/
  1888. aml_nand_debug("aml_nand suspend entered\n");
  1889. return 0;
  1890. }
  1891. static void aml_nand_resume(struct mtd_info *mtd)
  1892. {
  1893. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1894. if (((READ_CBUS_REG(HHI_MPEG_CLK_CNTL)&(1<<8))) && (nand_erarly_suspend_flag == 2)) {
  1895. aml_chip->aml_nand_hw_init(aml_chip);
  1896. nand_erarly_suspend_flag = 0;
  1897. }
  1898. aml_nand_debug("aml_m1_nand resume entered\n");
  1899. return;
  1900. }
  1901. static struct aml_nand_flash_dev *aml_nand_get_flash_type(struct mtd_info *mtd,
  1902. struct nand_chip *chip,
  1903. int busw, int *maf_id)
  1904. {
  1905. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  1906. struct aml_nand_platform *plat = aml_chip->platform;
  1907. struct aml_nand_flash_dev *type = NULL;
  1908. int i, maf_idx;
  1909. u8 dev_id[MAX_ID_LEN];
  1910. //int tmp_id, tmp_manf;
  1911. /* Send the command for reading device ID */
  1912. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  1913. /* Read manufacturer and device IDs */
  1914. for (i=0; i<MAX_ID_LEN; i++) {
  1915. dev_id[i] = chip->read_byte(mtd);
  1916. }
  1917. *maf_id = dev_id[0];
  1918. aml_nand_debug("NAND device id: %x %x %x %x %x %x \n", dev_id[0], dev_id[1], dev_id[2], dev_id[3], dev_id[4], dev_id[5]);
  1919. /* Lookup the flash id */
  1920. for (i = 0; aml_nand_flash_ids[i].name != NULL; i++) {
  1921. if(!strncmp((char*)aml_nand_flash_ids[i].id, (char*)dev_id, strlen((const char*)aml_nand_flash_ids[i].id))){
  1922. type = &aml_nand_flash_ids[i];
  1923. break;
  1924. }
  1925. }
  1926. if (!type) {
  1927. if (plat->nand_flash_dev) {
  1928. if(!strncmp((char*)plat->nand_flash_dev->id, (char*)dev_id, strlen((const char*)plat->nand_flash_dev->id))){
  1929. type = plat->nand_flash_dev;
  1930. }
  1931. }
  1932. if (!type)
  1933. return ERR_PTR(-ENODEV);
  1934. }
  1935. if (!mtd->name)
  1936. mtd->name = type->name;
  1937. chip->chipsize = type->chipsize;
  1938. chip->chipsize = chip->chipsize << 20;
  1939. /* Newer devices have all the information in additional id bytes */
  1940. if (!type->pagesize) {
  1941. int extid;
  1942. /* The 3rd id byte holds MLC / multichip data */
  1943. chip->cellinfo = chip->read_byte(mtd);
  1944. /* The 4th id byte is the important one */
  1945. extid = chip->read_byte(mtd);
  1946. /* Calc pagesize */
  1947. mtd->writesize = 1024 << (extid & 0x3);
  1948. extid >>= 2;
  1949. /* Calc oobsize */
  1950. mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
  1951. extid >>= 2;
  1952. /* Calc blocksize. Blocksize is multiples of 64KiB */
  1953. mtd->erasesize = (64 * 1024) << (extid & 0x03);
  1954. extid >>= 2;
  1955. /* Get buswidth information */
  1956. busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
  1957. } else {
  1958. /*
  1959. * Old devices have chip data hardcoded in the device id table
  1960. */
  1961. mtd->erasesize = type->erasesize;
  1962. mtd->writesize = type->pagesize;
  1963. mtd->oobsize = type->oobsize;
  1964. busw = type->options & NAND_BUSW_OPTIONS_MASK;
  1965. }
  1966. /* Try to identify manufacturer */
  1967. for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
  1968. if (nand_manuf_ids[maf_idx].id == *maf_id)
  1969. break;
  1970. }
  1971. /*
  1972. * Check, if buswidth is correct. Hardware drivers should set
  1973. * chip correct !
  1974. */
  1975. if (busw != (chip->options & NAND_BUSWIDTH_16)) {
  1976. aml_nand_debug(KERN_INFO "NAND device: Manufacturer ID:"
  1977. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
  1978. dev_id[0], nand_manuf_ids[maf_idx].name, mtd->name);
  1979. aml_nand_debug(KERN_WARNING "NAND bus width %d instead %d bit\n",
  1980. (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
  1981. busw ? 16 : 8);
  1982. return ERR_PTR(-EINVAL);
  1983. }
  1984. /* Calculate the address shift from the page size */
  1985. chip->page_shift = ffs(mtd->writesize) - 1;
  1986. /* Convert chipsize to number of pages per chip -1. */
  1987. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  1988. chip->bbt_erase_shift = chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
  1989. chip->chip_shift = ffs(chip->chipsize) - 1;
  1990. /* Set the bad block position */
  1991. chip->badblockpos = AML_BADBLK_POS;
  1992. /* Get chip options, preserve non chip based options */
  1993. chip->options &= ~NAND_CHIPOPTIONS_MSK;
  1994. chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
  1995. /*
  1996. * Set chip as a default. Board drivers can override it, if necessary
  1997. */
  1998. chip->options |= NAND_NO_AUTOINCR;
  1999. /* Check if chip is a not a samsung device. Do not clear the
  2000. * options for chips which are not having an extended id.
  2001. */
  2002. if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
  2003. chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
  2004. aml_nand_debug(KERN_INFO "NAND device: Manufacturer ID:"
  2005. " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id[0],
  2006. nand_manuf_ids[maf_idx].name, type->name);
  2007. return type;
  2008. }
  2009. static int aml_nand_scan_ident(struct mtd_info *mtd, int maxchips)
  2010. {
  2011. int i, busw, nand_maf_id, valid_chip_num = 1;
  2012. struct nand_chip *chip = mtd->priv;
  2013. struct aml_nand_chip *aml_chip = mtd_to_nand_chip(mtd);
  2014. struct aml_nand_flash_dev *aml_type;
  2015. unsigned temp_chip_shift;
  2016. /* Get buswidth to select the correct functions */
  2017. busw = chip->options & NAND_BUSWIDTH_16;
  2018. /* Select the device */
  2019. chip->select_chip(mtd, 0);
  2020. //reset chip for some nand need reset after power up
  2021. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2022. aml_chip->aml_nand_wait_devready(aml_chip, 0);
  2023. /* Read the flash type */
  2024. aml_type = aml_nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
  2025. if (IS_ERR(aml_type)) {
  2026. aml_nand_debug(KERN_WARNING "No NAND device found!!!\n");
  2027. chip->select_chip(mtd, -1);
  2028. return PTR_ERR(aml_type);
  2029. }
  2030. aml_chip->mfr_type = aml_type->id[0];
  2031. /* Check for a chip array */
  2032. for (i = 1; i < maxchips; i++) {
  2033. aml_chip->aml_nand_select_chip(aml_chip, i);
  2034. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2035. aml_chip->aml_nand_wait_devready(aml_chip, i);
  2036. /* Send the command for reading device ID */
  2037. chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
  2038. /* Read manufacturer and device IDs */
  2039. if (nand_maf_id != chip->read_byte(mtd) || aml_type->id[1] != chip->read_byte(mtd))
  2040. //if (nand_maf_id != dev_id[0] || aml_type->id[1] != dev_id[1])
  2041. aml_chip->valid_chip[i] = 0;
  2042. else
  2043. valid_chip_num ++;
  2044. }
  2045. if (i > 1) {
  2046. aml_nand_debug(KERN_INFO "%d NAND chips detected\n", valid_chip_num);
  2047. /*if ((aml_chip->valid_chip[1] == 0) && (aml_chip->valid_chip[2] == 1)) {
  2048. aml_nand_debug("ce1 and ce2 connected\n");
  2049. aml_chip->chip_enable[2] = (aml_chip->chip_enable[1] & aml_chip->chip_enable[2]);
  2050. }*/
  2051. }
  2052. /* Store the number of chips and calc total size for mtd */
  2053. chip->numchips = 1;
  2054. if ((chip->chipsize >> 32) & 0xffffffff)
  2055. chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) * valid_chip_num + 32 - 1;
  2056. else
  2057. chip->chip_shift = ffs((unsigned)chip->chipsize) * valid_chip_num - 1;
  2058. chip->pagemask = ((chip->chipsize * valid_chip_num) >> chip->page_shift) - 1;
  2059. //chip->options |= NAND_CACHEPRG;
  2060. chip->options |= NAND_NO_SUBPAGE_WRITE;
  2061. aml_chip->internal_chipnr = aml_type->internal_chipnr;
  2062. aml_chip->internal_page_nums = chip->chipsize >> chip->page_shift;
  2063. aml_chip->internal_page_nums /=aml_chip->internal_chipnr;
  2064. aml_chip->internal_chip_shift = fls((unsigned)aml_chip->internal_page_nums) - 1;
  2065. temp_chip_shift = ffs((unsigned)aml_chip->internal_page_nums) - 1;
  2066. if (aml_chip->internal_chip_shift != temp_chip_shift) {
  2067. aml_chip->internal_chip_shift += 1;
  2068. chip->chip_shift += 1;
  2069. chip->pagemask = ((1 << (chip->chip_shift + 1)) >> chip->page_shift) - 1;
  2070. }
  2071. aml_chip->options = aml_type->options;
  2072. aml_chip->page_size = aml_type->pagesize;
  2073. aml_chip->block_size = aml_type->erasesize;
  2074. aml_chip->oob_size = aml_type->oobsize;
  2075. mtd->erasesize = valid_chip_num * aml_type->erasesize;
  2076. mtd->writesize = valid_chip_num * aml_type->pagesize;
  2077. mtd->oobsize = valid_chip_num * aml_type->oobsize;
  2078. mtd->size = valid_chip_num * chip->chipsize;
  2079. chip->cmdfunc = aml_nand_cmdfunc;
  2080. chip->waitfunc = aml_nand_wait;
  2081. chip->erase_cmd = aml_nand_erase_cmd;
  2082. chip->write_page = aml_nand_write_page;
  2083. return 0;
  2084. }
  2085. int aml_nand_scan(struct mtd_info *mtd, int maxchips)
  2086. {
  2087. int ret;
  2088. ret = aml_nand_scan_ident(mtd, maxchips);
  2089. if (!ret)
  2090. ret = nand_scan_tail(mtd);
  2091. return ret;
  2092. }
  2093. static int aml_nand_add_partition(struct aml_nand_chip *aml_chip)
  2094. {
  2095. int adjust_offset;
  2096. struct mtd_info *mtd = &aml_chip->mtd;
  2097. struct aml_nand_platform *plat = aml_chip->platform;
  2098. struct platform_nand_chip *chip = &plat->platform_nand_data.chip;
  2099. #ifdef CONFIG_MTD_PARTITIONS
  2100. struct mtd_partition *temp_parts = NULL;
  2101. struct mtd_partition *parts;
  2102. int nr;
  2103. if (chip->set_parts)
  2104. chip->set_parts(mtd->size, chip);
  2105. parts = plat->platform_nand_data.chip.partitions;
  2106. nr = plat->platform_nand_data.chip.nr_partitions;
  2107. if (!strncmp((char*)plat->name, NAND_BOOT_NAME, strlen((const char*)NAND_BOOT_NAME))) {
  2108. if (nr == 0) {
  2109. parts = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
  2110. if (!parts)
  2111. return -ENOMEM;
  2112. }
  2113. parts->name = NAND_BOOT_NAME;
  2114. parts->offset = 0;
  2115. parts->size = (mtd->writesize * 1024);
  2116. nr = 1;
  2117. nand_boot_flag = 1;
  2118. }
  2119. else {
  2120. if (nand_boot_flag) {
  2121. adjust_offset = ((mtd->writesize / (aml_chip->plane_num * aml_chip->page_size)) * aml_chip->page_size * 1024);
  2122. if (parts->offset < adjust_offset) {
  2123. adjust_offset -= parts->offset;
  2124. temp_parts = plat->platform_nand_data.chip.partitions;
  2125. temp_parts->offset += adjust_offset;
  2126. temp_parts->size -= adjust_offset;
  2127. BUG_ON(temp_parts->size < mtd->erasesize);
  2128. }
  2129. }
  2130. }
  2131. return add_mtd_partitions(mtd, parts, nr);
  2132. #else
  2133. return add_mtd_device(mtd);
  2134. #endif
  2135. }
  2136. int aml_nand_init(struct aml_nand_chip *aml_chip)
  2137. {
  2138. struct aml_nand_platform *plat = aml_chip->platform;
  2139. struct nand_chip *chip = &aml_chip->chip;
  2140. struct mtd_info *mtd = &aml_chip->mtd;
  2141. int err = 0, i = 0;
  2142. #ifdef CONFIG_HAS_EARLYSUSPEND
  2143. aml_chip->nand_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
  2144. if (!aml_chip->nand_early_suspend.suspend)
  2145. aml_chip->nand_early_suspend.suspend = aml_platform_nand_suspend;
  2146. if (!aml_chip->nand_early_suspend.resume)
  2147. aml_chip->nand_early_suspend.resume = aml_platform_nand_resume;
  2148. aml_chip->nand_early_suspend.param = aml_chip;
  2149. register_early_suspend(&aml_chip->nand_early_suspend);
  2150. #endif
  2151. aml_chip->ran_mode=plat->ran_mode; //def close, for all part
  2152. aml_chip->rbpin_mode=plat->rbpin_mode;
  2153. aml_chip->short_pgsz=plat->short_pgsz;
  2154. switch (plat->platform_nand_data.chip.options & NAND_ECC_OPTIONS_MASK) {
  2155. case NAND_ECC_SOFT_MODE:
  2156. chip->write_buf = aml_nand_dma_write_buf;
  2157. chip->read_buf = aml_nand_dma_read_buf;
  2158. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2159. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2160. chip->ecc.mode = NAND_ECC_SOFT;
  2161. aml_chip->user_byte_mode = 1;
  2162. aml_chip->bch_mode = 0;
  2163. break;
  2164. case NAND_ECC_BCH8_512_MODE:
  2165. chip->write_buf = aml_nand_dma_write_buf;
  2166. chip->read_buf = aml_nand_dma_read_buf;
  2167. chip->block_bad = aml_nand_block_bad;
  2168. chip->block_markbad = aml_nand_block_markbad;
  2169. chip->ecc.mode = NAND_ECC_HW;
  2170. chip->ecc.size = NAND_ECC_UNIT_SIZE; //our hardware ecc unit is 512bytes
  2171. chip->ecc.bytes = NAND_BCH8_512_ECC_SIZE;
  2172. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2173. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2174. chip->ecc.read_page = aml_nand_read_page_hwecc;
  2175. chip->ecc.write_page = aml_nand_write_page_hwecc;
  2176. chip->ecc.read_oob = aml_nand_read_oob;
  2177. chip->ecc.write_oob = aml_nand_write_oob;
  2178. aml_chip->bch_mode = NAND_ECC_BCH8_512;
  2179. aml_chip->user_byte_mode = 2;
  2180. break;
  2181. case NAND_ECC_BCH8_1K_MODE:
  2182. chip->write_buf = aml_nand_dma_write_buf;
  2183. chip->read_buf = aml_nand_dma_read_buf;
  2184. chip->block_bad = aml_nand_block_bad;
  2185. chip->block_markbad = aml_nand_block_markbad;
  2186. chip->ecc.mode = NAND_ECC_HW;
  2187. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  2188. chip->ecc.bytes = NAND_BCH8_1K_ECC_SIZE;
  2189. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2190. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2191. chip->ecc.read_page = aml_nand_read_page_hwecc;
  2192. chip->ecc.write_page = aml_nand_write_page_hwecc;
  2193. chip->ecc.read_oob = aml_nand_read_oob;
  2194. chip->ecc.write_oob = aml_nand_write_oob;
  2195. aml_chip->bch_mode = NAND_ECC_BCH8_1K;
  2196. aml_chip->user_byte_mode = 2;
  2197. break;
  2198. case NAND_ECC_BCH30_MODE:
  2199. chip->write_buf = aml_nand_dma_write_buf;
  2200. chip->read_buf = aml_nand_dma_read_buf;
  2201. chip->block_bad = aml_nand_block_bad;
  2202. chip->block_markbad = aml_nand_block_markbad;
  2203. chip->ecc.mode = NAND_ECC_HW;
  2204. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  2205. chip->ecc.bytes = NAND_BCH30_ECC_SIZE;
  2206. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2207. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2208. chip->ecc.read_page = aml_nand_read_page_hwecc;
  2209. chip->ecc.write_page = aml_nand_write_page_hwecc;
  2210. chip->ecc.read_oob = aml_nand_read_oob;
  2211. chip->ecc.write_oob = aml_nand_write_oob;
  2212. aml_chip->bch_mode = NAND_ECC_BCH30;
  2213. aml_chip->user_byte_mode = 2;
  2214. break;
  2215. case NAND_ECC_BCH40_MODE:
  2216. chip->write_buf = aml_nand_dma_write_buf;
  2217. chip->read_buf = aml_nand_dma_read_buf;
  2218. chip->block_bad = aml_nand_block_bad;
  2219. chip->block_markbad = aml_nand_block_markbad;
  2220. chip->ecc.mode = NAND_ECC_HW;
  2221. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  2222. chip->ecc.bytes = NAND_BCH40_ECC_SIZE;
  2223. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2224. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2225. chip->ecc.read_page = aml_nand_read_page_hwecc;
  2226. chip->ecc.write_page = aml_nand_write_page_hwecc;
  2227. chip->ecc.read_oob = aml_nand_read_oob;
  2228. chip->ecc.write_oob = aml_nand_write_oob;
  2229. aml_chip->bch_mode = NAND_ECC_BCH40;
  2230. aml_chip->user_byte_mode = 2;
  2231. break;
  2232. case NAND_ECC_BCH60_MODE:
  2233. chip->write_buf = aml_nand_dma_write_buf;
  2234. chip->read_buf = aml_nand_dma_read_buf;
  2235. chip->block_bad = aml_nand_block_bad;
  2236. chip->block_markbad = aml_nand_block_markbad;
  2237. chip->ecc.mode = NAND_ECC_HW;
  2238. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  2239. chip->ecc.bytes = NAND_BCH60_ECC_SIZE;
  2240. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2241. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2242. chip->ecc.read_page = aml_nand_read_page_hwecc;
  2243. chip->ecc.write_page = aml_nand_write_page_hwecc;
  2244. chip->ecc.read_oob = aml_nand_read_oob;
  2245. chip->ecc.write_oob = aml_nand_write_oob;
  2246. aml_chip->bch_mode = NAND_ECC_BCH60;
  2247. aml_chip->user_byte_mode = 2;
  2248. break;
  2249. case NAND_ECC_SHORT_MODE:
  2250. chip->write_buf = aml_nand_dma_write_buf;
  2251. chip->read_buf = aml_nand_dma_read_buf;
  2252. chip->block_bad = aml_nand_block_bad;
  2253. chip->block_markbad = aml_nand_block_markbad;
  2254. chip->ecc.mode = NAND_ECC_HW;
  2255. chip->ecc.size = NAND_ECC_UNIT_SHORT;
  2256. chip->ecc.bytes = NAND_BCH60_ECC_SIZE;
  2257. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2258. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2259. chip->ecc.read_page = aml_nand_read_page_hwecc;
  2260. chip->ecc.write_page = aml_nand_write_page_hwecc;
  2261. chip->ecc.read_oob = aml_nand_read_oob;
  2262. chip->ecc.write_oob = aml_nand_write_oob;
  2263. aml_chip->bch_mode = NAND_ECC_BCH60;
  2264. aml_chip->short_pgsz = 1;
  2265. aml_chip->user_byte_mode = 2;
  2266. break;
  2267. case NAND_ECC_BCH16_MODE:
  2268. chip->write_buf = aml_nand_dma_write_buf;
  2269. chip->read_buf = aml_nand_dma_read_buf;
  2270. chip->block_bad = aml_nand_block_bad;
  2271. chip->block_markbad = aml_nand_block_markbad;
  2272. chip->ecc.mode = NAND_ECC_HW;
  2273. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  2274. chip->ecc.bytes = NAND_BCH16_ECC_SIZE;
  2275. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2276. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2277. chip->ecc.read_page = aml_nand_read_page_hwecc;
  2278. chip->ecc.write_page = aml_nand_write_page_hwecc;
  2279. chip->ecc.read_oob = aml_nand_read_oob;
  2280. chip->ecc.write_oob = aml_nand_write_oob;
  2281. aml_chip->bch_mode = NAND_ECC_BCH16;
  2282. aml_chip->user_byte_mode = 2;
  2283. break;
  2284. case NAND_ECC_BCH24_MODE:
  2285. chip->write_buf = aml_nand_dma_write_buf;
  2286. chip->read_buf = aml_nand_dma_read_buf;
  2287. chip->block_bad = aml_nand_block_bad;
  2288. chip->block_markbad = aml_nand_block_markbad;
  2289. chip->ecc.mode = NAND_ECC_HW;
  2290. chip->ecc.size = NAND_ECC_UNIT_1KSIZE;
  2291. chip->ecc.bytes = NAND_BCH24_ECC_SIZE;
  2292. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2293. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2294. chip->ecc.read_page = aml_nand_read_page_hwecc;
  2295. chip->ecc.write_page = aml_nand_write_page_hwecc;
  2296. chip->ecc.read_oob = aml_nand_read_oob;
  2297. chip->ecc.write_oob = aml_nand_write_oob;
  2298. aml_chip->bch_mode = NAND_ECC_BCH24;
  2299. aml_chip->user_byte_mode = 2;
  2300. break;
  2301. default :
  2302. aml_nand_debug(KERN_WARNING "haven`t found any ecc mode just selected NAND_ECC_NONE\n");
  2303. chip->write_buf = aml_nand_dma_write_buf;
  2304. chip->read_buf = aml_nand_dma_read_buf;
  2305. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2306. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2307. chip->ecc.mode = NAND_ECC_NONE;
  2308. aml_chip->user_byte_mode = 1;
  2309. aml_chip->bch_mode = 0;
  2310. break;
  2311. }
  2312. if (!aml_chip->aml_nand_hw_init)
  2313. aml_chip->aml_nand_hw_init = aml_platform_hw_init;
  2314. if (!aml_chip->aml_nand_options_confirm)
  2315. aml_chip->aml_nand_options_confirm = aml_platform_options_confirm;
  2316. if (!aml_chip->aml_nand_cmd_ctrl)
  2317. aml_chip->aml_nand_cmd_ctrl = aml_platform_cmd_ctrl;
  2318. if (!aml_chip->aml_nand_select_chip)
  2319. aml_chip->aml_nand_select_chip = aml_platform_select_chip;
  2320. if (!aml_chip->aml_nand_wait_devready)
  2321. aml_chip->aml_nand_wait_devready = aml_platform_wait_devready;
  2322. if (!aml_chip->aml_nand_get_user_byte)
  2323. aml_chip->aml_nand_get_user_byte = aml_platform_get_user_byte;
  2324. if (!aml_chip->aml_nand_set_user_byte)
  2325. aml_chip->aml_nand_set_user_byte = aml_platform_set_user_byte;
  2326. if (!aml_chip->aml_nand_command)
  2327. aml_chip->aml_nand_command = aml_nand_base_command;
  2328. if (!aml_chip->aml_nand_dma_read)
  2329. aml_chip->aml_nand_dma_read = aml_platform_dma_read;
  2330. if (!aml_chip->aml_nand_dma_write)
  2331. aml_chip->aml_nand_dma_write = aml_platform_dma_write;
  2332. if (!aml_chip->aml_nand_hwecc_correct)
  2333. aml_chip->aml_nand_hwecc_correct = aml_platform_hwecc_correct;
  2334. if (!chip->IO_ADDR_R)
  2335. chip->IO_ADDR_R = (void __iomem *) CBUS_REG_ADDR(NAND_BUF);
  2336. if (!chip->IO_ADDR_W)
  2337. chip->IO_ADDR_W = (void __iomem *) CBUS_REG_ADDR(NAND_BUF);
  2338. chip->options |= NAND_SKIP_BBTSCAN;
  2339. if (chip->ecc.mode != NAND_ECC_SOFT) {
  2340. if (aml_chip->user_byte_mode == 2)
  2341. chip->ecc.layout = &aml_nand_oob_64_2info;
  2342. else
  2343. chip->ecc.layout = &aml_nand_oob_64;
  2344. }
  2345. chip->select_chip = aml_nand_select_chip;
  2346. chip->cmd_ctrl = aml_nand_cmd_ctrl;
  2347. #if CONFIG_AM_NAND_RBPIN
  2348. chip->dev_ready = aml_nand_dev_ready;
  2349. #endif
  2350. chip->verify_buf = aml_nand_verify_buf;
  2351. chip->read_byte = aml_platform_read_byte;
  2352. aml_chip->chip_num = plat->platform_nand_data.chip.nr_chips;
  2353. if (aml_chip->chip_num > MAX_CHIP_NUM) {
  2354. aml_nand_debug("couldn`t support for so many chips\n");
  2355. err = -ENXIO;
  2356. goto exit_error;
  2357. }
  2358. for (i=0; i<aml_chip->chip_num; i++) {
  2359. aml_chip->valid_chip[i] = 1;
  2360. aml_chip->chip_enable[i] = (((plat->chip_enable_pad >> i*4) & 0xf) << 10);
  2361. aml_chip->rb_enable[i] = (((plat->ready_busy_pad >> i*4) & 0xf) << 10);
  2362. /*if ((i > 0) && (aml_chip->rb_enable[i] == aml_chip->rb_enable[0])) {
  2363. aml_chip->ops_mode = AML_MULTI_CHIP_SHARE_RB;
  2364. }*/
  2365. }
  2366. aml_chip->aml_nand_hw_init(aml_chip);
  2367. if (nand_scan(mtd, aml_chip->chip_num) == -ENODEV) {
  2368. chip->options = 0;
  2369. chip->options |= NAND_SKIP_BBTSCAN;
  2370. if (aml_nand_scan(mtd, aml_chip->chip_num)) {
  2371. err = -ENXIO;
  2372. goto exit_error;
  2373. }
  2374. }
  2375. else {
  2376. for (i=1; i<aml_chip->chip_num; i++) {
  2377. aml_chip->valid_chip[i] = 0;
  2378. }
  2379. aml_chip->options = NAND_DEFAULT_OPTIONS;
  2380. aml_chip->page_size = mtd->writesize;
  2381. aml_chip->block_size = mtd->erasesize;
  2382. aml_chip->oob_size = mtd->oobsize;
  2383. aml_chip->plane_num = 1;
  2384. aml_chip->internal_chipnr = 1;
  2385. chip->ecc.read_page_raw = aml_nand_read_page_raw;
  2386. chip->ecc.write_page_raw = aml_nand_write_page_raw;
  2387. }
  2388. mtd->suspend = aml_nand_suspend;
  2389. mtd->resume = aml_nand_resume;
  2390. if (chip->ecc.mode != NAND_ECC_SOFT) {
  2391. if (aml_chip->aml_nand_options_confirm(aml_chip)) {
  2392. err = -ENXIO;
  2393. goto exit_error;
  2394. }
  2395. }
  2396. if (plat->platform_nand_data.chip.ecclayout) {
  2397. chip->ecc.layout = plat->platform_nand_data.chip.ecclayout;
  2398. }
  2399. else {
  2400. if (!strncmp((char*)plat->name, NAND_BOOT_NAME, strlen((const char*)NAND_BOOT_NAME))) {
  2401. chip->ecc.layout = &aml_nand_uboot_oob;
  2402. }
  2403. else if (chip->ecc.mode != NAND_ECC_SOFT) {
  2404. switch (mtd->oobsize) {
  2405. case 64:
  2406. chip->ecc.layout = &aml_nand_oob_64_2info;
  2407. break;
  2408. case 128:
  2409. chip->ecc.layout = &aml_nand_oob_128;
  2410. break;
  2411. case 218:
  2412. chip->ecc.layout = &aml_nand_oob_218;
  2413. break;
  2414. case 224:
  2415. chip->ecc.layout = &aml_nand_oob_224;
  2416. break;
  2417. case 256:
  2418. chip->ecc.layout = &aml_nand_oob_256;
  2419. break;
  2420. case 376:
  2421. chip->ecc.layout = &aml_nand_oob_376;
  2422. break;
  2423. case 436:
  2424. chip->ecc.layout = &aml_nand_oob_436;
  2425. break;
  2426. case 448:
  2427. chip->ecc.layout = &aml_nand_oob_448;
  2428. break;
  2429. case 752:
  2430. chip->ecc.layout = &aml_nand_oob_752;
  2431. break;
  2432. case 872:
  2433. chip->ecc.layout = &aml_nand_oob_872;
  2434. break;
  2435. case 896:
  2436. chip->ecc.layout = &aml_nand_oob_896;
  2437. break;
  2438. case 1504:
  2439. chip->ecc.layout = &aml_nand_oob_1504;
  2440. break;
  2441. case 1744:
  2442. chip->ecc.layout = &aml_nand_oob_1744;
  2443. break;
  2444. case 1792:
  2445. chip->ecc.layout = &aml_nand_oob_1792;
  2446. break;
  2447. case 3008:
  2448. chip->ecc.layout = &aml_nand_oob_3008;
  2449. break;
  2450. case 3584:
  2451. chip->ecc.layout = &aml_nand_oob_3584;
  2452. break;
  2453. default:
  2454. aml_nand_debug("havn`t found any oob layout use nand base oob layout " "oobsize %d\n", mtd->oobsize);
  2455. chip->ecc.layout = &aml_nand_oob_64_2info;
  2456. break;
  2457. }
  2458. }
  2459. }
  2460. /*
  2461. * The number of bytes available for a client to place data into
  2462. * the out of band area
  2463. */
  2464. chip->ecc.layout->oobavail = 0;
  2465. for (i = 0; chip->ecc.layout->oobfree[i].length && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
  2466. chip->ecc.layout->oobavail += chip->ecc.layout->oobfree[i].length;
  2467. mtd->oobavail = chip->ecc.layout->oobavail;
  2468. mtd->ecclayout = chip->ecc.layout;
  2469. aml_chip->virtual_page_size = mtd->writesize;
  2470. aml_chip->virtual_block_size = mtd->erasesize;
  2471. #if 0
  2472. #else
  2473. aml_chip->aml_nand_data_buf = dma_alloc_coherent(aml_chip->device,(mtd->writesize+mtd->oobsize),
  2474. &(aml_chip->aml_nand_dma_buf_dma_addr), GFP_KERNEL); //2 mean two plane mode
  2475. if (aml_chip->aml_nand_data_buf == NULL){
  2476. dev_err(&aml_chip->device, "no memory for flash info\n");
  2477. err = -ENOMEM;
  2478. goto exit_error;
  2479. }
  2480. aml_chip->user_info_buf = dma_alloc_coherent(aml_chip->device, (mtd->writesize / chip->ecc.size)*sizeof(int), &(aml_chip->aml_nand_info_dma_addr), GFP_KERNEL);
  2481. if (aml_chip->user_info_buf== NULL) {
  2482. dev_err(&aml_chip->device, "no memory for flash info\n");
  2483. err = -ENOMEM;
  2484. goto exit_error;
  2485. }
  2486. aml_nand_debug2("data buffer addr=%p, dma addr=0x%x\n",aml_chip->aml_nand_data_buf,aml_chip->aml_nand_dma_buf_dma_addr);
  2487. aml_nand_debug2("info buffer addr=%p, dma addr=0x%x\n",aml_chip->user_info_buf,aml_chip->aml_nand_info_dma_addr);
  2488. #endif
  2489. if (chip->buffers)
  2490. kfree(chip->buffers);
  2491. if (mtd->oobsize >= NAND_MAX_OOBSIZE)
  2492. chip->buffers = kzalloc((mtd->writesize + 3*mtd->oobsize), GFP_KERNEL);
  2493. else
  2494. chip->buffers = kzalloc((mtd->writesize + 3*NAND_MAX_OOBSIZE), GFP_KERNEL);
  2495. if (chip->buffers == NULL) {
  2496. aml_nand_debug("no memory for flash data buf\n");
  2497. err = -ENOMEM;
  2498. goto exit_error;
  2499. }
  2500. chip->oob_poi = chip->buffers->databuf + mtd->writesize;
  2501. chip->options |= NAND_OWN_BUFFERS;
  2502. if (aml_nand_add_partition(aml_chip) != 0) {
  2503. err = -ENXIO;
  2504. goto exit_error;
  2505. }
  2506. #if 0
  2507. if(aml_chip->mfr_type == NAND_MFR_MICRON)
  2508. {
  2509. unsigned P[4];
  2510. memset(P,0,4);
  2511. P[0] = ((plat->platform_nand_data.chip.options & NAND_TIMING_OPTIONS_MASK) >> 8);
  2512. if (P[0] > 5)
  2513. P[0] = 5;
  2514. memcpy(aml_chip->aml_nand_data_buf , P,4);
  2515. chip->cmd_ctrl(mtd, NAND_CMD_SetFeature, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  2516. chip->cmd_ctrl(mtd, 0x01, NAND_NCE | NAND_CTRL_CHANGE);
  2517. aml_platform_write_bytes(mtd, aml_chip->aml_nand_data_buf, 4);
  2518. udelay(1);
  2519. chip->cmd_ctrl(mtd, NAND_CMD_GetFeature, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  2520. chip->cmd_ctrl(mtd, 0x01, NAND_NCE | NAND_CTRL_CHANGE);
  2521. udelay(1);
  2522. chip->cmd_ctrl(mtd, 0, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  2523. aml_platform_read_bytes(mtd, aml_chip->aml_nand_data_buf, 4);
  2524. if(P[0] !=aml_chip->aml_nand_data_buf[0])
  2525. {
  2526. aml_nand_debug("set timing mode error for device!\n");
  2527. }
  2528. }
  2529. #endif
  2530. aml_nand_debug(" %s initialized ok\n", mtd->name);
  2531. return 0;
  2532. exit_error:
  2533. if (aml_chip->user_info_buf) {
  2534. dma_free_coherent(aml_chip->device, (mtd->writesize / chip->ecc.size)*sizeof(int), aml_chip->user_info_buf, (dma_addr_t)aml_chip->aml_nand_info_dma_addr);
  2535. aml_chip->user_info_buf = NULL;
  2536. }
  2537. if (chip->buffers) {
  2538. kfree(chip->buffers);
  2539. chip->buffers = NULL;
  2540. }
  2541. if (aml_chip->aml_nand_data_buf) {
  2542. dma_free_coherent(aml_chip->device, (mtd->writesize + mtd->oobsize), aml_chip->aml_nand_data_buf, (dma_addr_t)aml_chip->aml_nand_dma_buf_dma_addr);
  2543. aml_chip->aml_nand_data_buf = NULL;
  2544. }
  2545. return err;
  2546. }
  2547. #define DRV_NAME "aml-nand"
  2548. #define DRV_VERSION "1.0"
  2549. #define DRV_AUTHOR "xiaojun_yoyo"
  2550. #define DRV_DESC "Amlogic nand flash host controll driver for m3"
  2551. MODULE_LICENSE("GPL");
  2552. MODULE_AUTHOR(DRV_AUTHOR);
  2553. MODULE_DESCRIPTION(DRV_DESC);
  2554. MODULE_ALIAS("platform:" DRV_NAME);