sata_mv.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467
  1. /*
  2. * sata_mv.c - Marvell SATA support
  3. *
  4. * Copyright 2008-2009: Marvell Corporation, all rights reserved.
  5. * Copyright 2005: EMC Corporation, all rights reserved.
  6. * Copyright 2005 Red Hat, Inc. All rights reserved.
  7. *
  8. * Originally written by Brett Russ.
  9. * Extensive overhaul and enhancement by Mark Lord <mlord@pobox.com>.
  10. *
  11. * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; version 2 of the License.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. /*
  28. * sata_mv TODO list:
  29. *
  30. * --> Develop a low-power-consumption strategy, and implement it.
  31. *
  32. * --> Add sysfs attributes for per-chip / per-HC IRQ coalescing thresholds.
  33. *
  34. * --> [Experiment, Marvell value added] Is it possible to use target
  35. * mode to cross-connect two Linux boxes with Marvell cards? If so,
  36. * creating LibATA target mode support would be very interesting.
  37. *
  38. * Target mode, for those without docs, is the ability to directly
  39. * connect two SATA ports.
  40. */
  41. /*
  42. * 80x1-B2 errata PCI#11:
  43. *
  44. * Users of the 6041/6081 Rev.B2 chips (current is C0)
  45. * should be careful to insert those cards only onto PCI-X bus #0,
  46. * and only in device slots 0..7, not higher. The chips may not
  47. * work correctly otherwise (note: this is a pretty rare condition).
  48. */
  49. #include <linux/kernel.h>
  50. #include <linux/module.h>
  51. #include <linux/pci.h>
  52. #include <linux/init.h>
  53. #include <linux/blkdev.h>
  54. #include <linux/delay.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/dmapool.h>
  57. #include <linux/dma-mapping.h>
  58. #include <linux/device.h>
  59. #include <linux/clk.h>
  60. #include <linux/platform_device.h>
  61. #include <linux/ata_platform.h>
  62. #include <linux/mbus.h>
  63. #include <linux/bitops.h>
  64. #include <linux/gfp.h>
  65. #include <linux/of.h>
  66. #include <linux/of_irq.h>
  67. #include <scsi/scsi_host.h>
  68. #include <scsi/scsi_cmnd.h>
  69. #include <scsi/scsi_device.h>
  70. #include <linux/libata.h>
  71. #define DRV_NAME "sata_mv"
  72. #define DRV_VERSION "1.28"
  73. /*
  74. * module options
  75. */
  76. #ifdef CONFIG_PCI
  77. static int msi;
  78. module_param(msi, int, S_IRUGO);
  79. MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
  80. #endif
  81. static int irq_coalescing_io_count;
  82. module_param(irq_coalescing_io_count, int, S_IRUGO);
  83. MODULE_PARM_DESC(irq_coalescing_io_count,
  84. "IRQ coalescing I/O count threshold (0..255)");
  85. static int irq_coalescing_usecs;
  86. module_param(irq_coalescing_usecs, int, S_IRUGO);
  87. MODULE_PARM_DESC(irq_coalescing_usecs,
  88. "IRQ coalescing time threshold in usecs");
  89. enum {
  90. /* BAR's are enumerated in terms of pci_resource_start() terms */
  91. MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
  92. MV_IO_BAR = 2, /* offset 0x18: IO space */
  93. MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
  94. MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
  95. MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
  96. /* For use with both IRQ coalescing methods ("all ports" or "per-HC" */
  97. COAL_CLOCKS_PER_USEC = 150, /* for calculating COAL_TIMEs */
  98. MAX_COAL_TIME_THRESHOLD = ((1 << 24) - 1), /* internal clocks count */
  99. MAX_COAL_IO_COUNT = 255, /* completed I/O count */
  100. MV_PCI_REG_BASE = 0,
  101. /*
  102. * Per-chip ("all ports") interrupt coalescing feature.
  103. * This is only for GEN_II / GEN_IIE hardware.
  104. *
  105. * Coalescing defers the interrupt until either the IO_THRESHOLD
  106. * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
  107. */
  108. COAL_REG_BASE = 0x18000,
  109. IRQ_COAL_CAUSE = (COAL_REG_BASE + 0x08),
  110. ALL_PORTS_COAL_IRQ = (1 << 4), /* all ports irq event */
  111. IRQ_COAL_IO_THRESHOLD = (COAL_REG_BASE + 0xcc),
  112. IRQ_COAL_TIME_THRESHOLD = (COAL_REG_BASE + 0xd0),
  113. /*
  114. * Registers for the (unused here) transaction coalescing feature:
  115. */
  116. TRAN_COAL_CAUSE_LO = (COAL_REG_BASE + 0x88),
  117. TRAN_COAL_CAUSE_HI = (COAL_REG_BASE + 0x8c),
  118. SATAHC0_REG_BASE = 0x20000,
  119. FLASH_CTL = 0x1046c,
  120. GPIO_PORT_CTL = 0x104f0,
  121. RESET_CFG = 0x180d8,
  122. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  123. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  124. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  125. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  126. MV_MAX_Q_DEPTH = 32,
  127. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  128. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  129. * CRPB needs alignment on a 256B boundary. Size == 256B
  130. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  131. */
  132. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  133. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  134. MV_MAX_SG_CT = 256,
  135. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  136. /* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
  137. MV_PORT_HC_SHIFT = 2,
  138. MV_PORTS_PER_HC = (1 << MV_PORT_HC_SHIFT), /* 4 */
  139. /* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
  140. MV_PORT_MASK = (MV_PORTS_PER_HC - 1), /* 3 */
  141. /* Host Flags */
  142. MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
  143. MV_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_PIO_POLLING,
  144. MV_GEN_I_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NO_ATAPI,
  145. MV_GEN_II_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NCQ |
  146. ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA,
  147. MV_GEN_IIE_FLAGS = MV_GEN_II_FLAGS | ATA_FLAG_AN,
  148. CRQB_FLAG_READ = (1 << 0),
  149. CRQB_TAG_SHIFT = 1,
  150. CRQB_IOID_SHIFT = 6, /* CRQB Gen-II/IIE IO Id shift */
  151. CRQB_PMP_SHIFT = 12, /* CRQB Gen-II/IIE PMP shift */
  152. CRQB_HOSTQ_SHIFT = 17, /* CRQB Gen-II/IIE HostQueTag shift */
  153. CRQB_CMD_ADDR_SHIFT = 8,
  154. CRQB_CMD_CS = (0x2 << 11),
  155. CRQB_CMD_LAST = (1 << 15),
  156. CRPB_FLAG_STATUS_SHIFT = 8,
  157. CRPB_IOID_SHIFT_6 = 5, /* CRPB Gen-II IO Id shift */
  158. CRPB_IOID_SHIFT_7 = 7, /* CRPB Gen-IIE IO Id shift */
  159. EPRD_FLAG_END_OF_TBL = (1 << 31),
  160. /* PCI interface registers */
  161. MV_PCI_COMMAND = 0xc00,
  162. MV_PCI_COMMAND_MWRCOM = (1 << 4), /* PCI Master Write Combining */
  163. MV_PCI_COMMAND_MRDTRIG = (1 << 7), /* PCI Master Read Trigger */
  164. PCI_MAIN_CMD_STS = 0xd30,
  165. STOP_PCI_MASTER = (1 << 2),
  166. PCI_MASTER_EMPTY = (1 << 3),
  167. GLOB_SFT_RST = (1 << 4),
  168. MV_PCI_MODE = 0xd00,
  169. MV_PCI_MODE_MASK = 0x30,
  170. MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
  171. MV_PCI_DISC_TIMER = 0xd04,
  172. MV_PCI_MSI_TRIGGER = 0xc38,
  173. MV_PCI_SERR_MASK = 0xc28,
  174. MV_PCI_XBAR_TMOUT = 0x1d04,
  175. MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
  176. MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
  177. MV_PCI_ERR_ATTRIBUTE = 0x1d48,
  178. MV_PCI_ERR_COMMAND = 0x1d50,
  179. PCI_IRQ_CAUSE = 0x1d58,
  180. PCI_IRQ_MASK = 0x1d5c,
  181. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  182. PCIE_IRQ_CAUSE = 0x1900,
  183. PCIE_IRQ_MASK = 0x1910,
  184. PCIE_UNMASK_ALL_IRQS = 0x40a, /* assorted bits */
  185. /* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
  186. PCI_HC_MAIN_IRQ_CAUSE = 0x1d60,
  187. PCI_HC_MAIN_IRQ_MASK = 0x1d64,
  188. SOC_HC_MAIN_IRQ_CAUSE = 0x20020,
  189. SOC_HC_MAIN_IRQ_MASK = 0x20024,
  190. ERR_IRQ = (1 << 0), /* shift by (2 * port #) */
  191. DONE_IRQ = (1 << 1), /* shift by (2 * port #) */
  192. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  193. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  194. DONE_IRQ_0_3 = 0x000000aa, /* DONE_IRQ ports 0,1,2,3 */
  195. DONE_IRQ_4_7 = (DONE_IRQ_0_3 << HC_SHIFT), /* 4,5,6,7 */
  196. PCI_ERR = (1 << 18),
  197. TRAN_COAL_LO_DONE = (1 << 19), /* transaction coalescing */
  198. TRAN_COAL_HI_DONE = (1 << 20), /* transaction coalescing */
  199. PORTS_0_3_COAL_DONE = (1 << 8), /* HC0 IRQ coalescing */
  200. PORTS_4_7_COAL_DONE = (1 << 17), /* HC1 IRQ coalescing */
  201. ALL_PORTS_COAL_DONE = (1 << 21), /* GEN_II(E) IRQ coalescing */
  202. GPIO_INT = (1 << 22),
  203. SELF_INT = (1 << 23),
  204. TWSI_INT = (1 << 24),
  205. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  206. HC_MAIN_RSVD_5 = (0x1fff << 19), /* bits 31-19 */
  207. HC_MAIN_RSVD_SOC = (0x3fffffb << 6), /* bits 31-9, 7-6 */
  208. /* SATAHC registers */
  209. HC_CFG = 0x00,
  210. HC_IRQ_CAUSE = 0x14,
  211. DMA_IRQ = (1 << 0), /* shift by port # */
  212. HC_COAL_IRQ = (1 << 4), /* IRQ coalescing */
  213. DEV_IRQ = (1 << 8), /* shift by port # */
  214. /*
  215. * Per-HC (Host-Controller) interrupt coalescing feature.
  216. * This is present on all chip generations.
  217. *
  218. * Coalescing defers the interrupt until either the IO_THRESHOLD
  219. * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
  220. */
  221. HC_IRQ_COAL_IO_THRESHOLD = 0x000c,
  222. HC_IRQ_COAL_TIME_THRESHOLD = 0x0010,
  223. SOC_LED_CTRL = 0x2c,
  224. SOC_LED_CTRL_BLINK = (1 << 0), /* Active LED blink */
  225. SOC_LED_CTRL_ACT_PRESENCE = (1 << 2), /* Multiplex dev presence */
  226. /* with dev activity LED */
  227. /* Shadow block registers */
  228. SHD_BLK = 0x100,
  229. SHD_CTL_AST = 0x20, /* ofs from SHD_BLK */
  230. /* SATA registers */
  231. SATA_STATUS = 0x300, /* ctrl, err regs follow status */
  232. SATA_ACTIVE = 0x350,
  233. FIS_IRQ_CAUSE = 0x364,
  234. FIS_IRQ_CAUSE_AN = (1 << 9), /* async notification */
  235. LTMODE = 0x30c, /* requires read-after-write */
  236. LTMODE_BIT8 = (1 << 8), /* unknown, but necessary */
  237. PHY_MODE2 = 0x330,
  238. PHY_MODE3 = 0x310,
  239. PHY_MODE4 = 0x314, /* requires read-after-write */
  240. PHY_MODE4_CFG_MASK = 0x00000003, /* phy internal config field */
  241. PHY_MODE4_CFG_VALUE = 0x00000001, /* phy internal config field */
  242. PHY_MODE4_RSVD_ZEROS = 0x5de3fffa, /* Gen2e always write zeros */
  243. PHY_MODE4_RSVD_ONES = 0x00000005, /* Gen2e always write ones */
  244. SATA_IFCTL = 0x344,
  245. SATA_TESTCTL = 0x348,
  246. SATA_IFSTAT = 0x34c,
  247. VENDOR_UNIQUE_FIS = 0x35c,
  248. FISCFG = 0x360,
  249. FISCFG_WAIT_DEV_ERR = (1 << 8), /* wait for host on DevErr */
  250. FISCFG_SINGLE_SYNC = (1 << 16), /* SYNC on DMA activation */
  251. PHY_MODE9_GEN2 = 0x398,
  252. PHY_MODE9_GEN1 = 0x39c,
  253. PHYCFG_OFS = 0x3a0, /* only in 65n devices */
  254. MV5_PHY_MODE = 0x74,
  255. MV5_LTMODE = 0x30,
  256. MV5_PHY_CTL = 0x0C,
  257. SATA_IFCFG = 0x050,
  258. MV_M2_PREAMP_MASK = 0x7e0,
  259. /* Port registers */
  260. EDMA_CFG = 0,
  261. EDMA_CFG_Q_DEPTH = 0x1f, /* max device queue depth */
  262. EDMA_CFG_NCQ = (1 << 5), /* for R/W FPDMA queued */
  263. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  264. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  265. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  266. EDMA_CFG_EDMA_FBS = (1 << 16), /* EDMA FIS-Based Switching */
  267. EDMA_CFG_FBS = (1 << 26), /* FIS-Based Switching */
  268. EDMA_ERR_IRQ_CAUSE = 0x8,
  269. EDMA_ERR_IRQ_MASK = 0xc,
  270. EDMA_ERR_D_PAR = (1 << 0), /* UDMA data parity err */
  271. EDMA_ERR_PRD_PAR = (1 << 1), /* UDMA PRD parity err */
  272. EDMA_ERR_DEV = (1 << 2), /* device error */
  273. EDMA_ERR_DEV_DCON = (1 << 3), /* device disconnect */
  274. EDMA_ERR_DEV_CON = (1 << 4), /* device connected */
  275. EDMA_ERR_SERR = (1 << 5), /* SError bits [WBDST] raised */
  276. EDMA_ERR_SELF_DIS = (1 << 7), /* Gen II/IIE self-disable */
  277. EDMA_ERR_SELF_DIS_5 = (1 << 8), /* Gen I self-disable */
  278. EDMA_ERR_BIST_ASYNC = (1 << 8), /* BIST FIS or Async Notify */
  279. EDMA_ERR_TRANS_IRQ_7 = (1 << 8), /* Gen IIE transprt layer irq */
  280. EDMA_ERR_CRQB_PAR = (1 << 9), /* CRQB parity error */
  281. EDMA_ERR_CRPB_PAR = (1 << 10), /* CRPB parity error */
  282. EDMA_ERR_INTRL_PAR = (1 << 11), /* internal parity error */
  283. EDMA_ERR_IORDY = (1 << 12), /* IORdy timeout */
  284. EDMA_ERR_LNK_CTRL_RX = (0xf << 13), /* link ctrl rx error */
  285. EDMA_ERR_LNK_CTRL_RX_0 = (1 << 13), /* transient: CRC err */
  286. EDMA_ERR_LNK_CTRL_RX_1 = (1 << 14), /* transient: FIFO err */
  287. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15), /* fatal: caught SYNC */
  288. EDMA_ERR_LNK_CTRL_RX_3 = (1 << 16), /* transient: FIS rx err */
  289. EDMA_ERR_LNK_DATA_RX = (0xf << 17), /* link data rx error */
  290. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21), /* link ctrl tx error */
  291. EDMA_ERR_LNK_CTRL_TX_0 = (1 << 21), /* transient: CRC err */
  292. EDMA_ERR_LNK_CTRL_TX_1 = (1 << 22), /* transient: FIFO err */
  293. EDMA_ERR_LNK_CTRL_TX_2 = (1 << 23), /* transient: caught SYNC */
  294. EDMA_ERR_LNK_CTRL_TX_3 = (1 << 24), /* transient: caught DMAT */
  295. EDMA_ERR_LNK_CTRL_TX_4 = (1 << 25), /* transient: FIS collision */
  296. EDMA_ERR_LNK_DATA_TX = (0x1f << 26), /* link data tx error */
  297. EDMA_ERR_TRANS_PROTO = (1 << 31), /* transport protocol error */
  298. EDMA_ERR_OVERRUN_5 = (1 << 5),
  299. EDMA_ERR_UNDERRUN_5 = (1 << 6),
  300. EDMA_ERR_IRQ_TRANSIENT = EDMA_ERR_LNK_CTRL_RX_0 |
  301. EDMA_ERR_LNK_CTRL_RX_1 |
  302. EDMA_ERR_LNK_CTRL_RX_3 |
  303. EDMA_ERR_LNK_CTRL_TX,
  304. EDMA_EH_FREEZE = EDMA_ERR_D_PAR |
  305. EDMA_ERR_PRD_PAR |
  306. EDMA_ERR_DEV_DCON |
  307. EDMA_ERR_DEV_CON |
  308. EDMA_ERR_SERR |
  309. EDMA_ERR_SELF_DIS |
  310. EDMA_ERR_CRQB_PAR |
  311. EDMA_ERR_CRPB_PAR |
  312. EDMA_ERR_INTRL_PAR |
  313. EDMA_ERR_IORDY |
  314. EDMA_ERR_LNK_CTRL_RX_2 |
  315. EDMA_ERR_LNK_DATA_RX |
  316. EDMA_ERR_LNK_DATA_TX |
  317. EDMA_ERR_TRANS_PROTO,
  318. EDMA_EH_FREEZE_5 = EDMA_ERR_D_PAR |
  319. EDMA_ERR_PRD_PAR |
  320. EDMA_ERR_DEV_DCON |
  321. EDMA_ERR_DEV_CON |
  322. EDMA_ERR_OVERRUN_5 |
  323. EDMA_ERR_UNDERRUN_5 |
  324. EDMA_ERR_SELF_DIS_5 |
  325. EDMA_ERR_CRQB_PAR |
  326. EDMA_ERR_CRPB_PAR |
  327. EDMA_ERR_INTRL_PAR |
  328. EDMA_ERR_IORDY,
  329. EDMA_REQ_Q_BASE_HI = 0x10,
  330. EDMA_REQ_Q_IN_PTR = 0x14, /* also contains BASE_LO */
  331. EDMA_REQ_Q_OUT_PTR = 0x18,
  332. EDMA_REQ_Q_PTR_SHIFT = 5,
  333. EDMA_RSP_Q_BASE_HI = 0x1c,
  334. EDMA_RSP_Q_IN_PTR = 0x20,
  335. EDMA_RSP_Q_OUT_PTR = 0x24, /* also contains BASE_LO */
  336. EDMA_RSP_Q_PTR_SHIFT = 3,
  337. EDMA_CMD = 0x28, /* EDMA command register */
  338. EDMA_EN = (1 << 0), /* enable EDMA */
  339. EDMA_DS = (1 << 1), /* disable EDMA; self-negated */
  340. EDMA_RESET = (1 << 2), /* reset eng/trans/link/phy */
  341. EDMA_STATUS = 0x30, /* EDMA engine status */
  342. EDMA_STATUS_CACHE_EMPTY = (1 << 6), /* GenIIe command cache empty */
  343. EDMA_STATUS_IDLE = (1 << 7), /* GenIIe EDMA enabled/idle */
  344. EDMA_IORDY_TMOUT = 0x34,
  345. EDMA_ARB_CFG = 0x38,
  346. EDMA_HALTCOND = 0x60, /* GenIIe halt conditions */
  347. EDMA_UNKNOWN_RSVD = 0x6C, /* GenIIe unknown/reserved */
  348. BMDMA_CMD = 0x224, /* bmdma command register */
  349. BMDMA_STATUS = 0x228, /* bmdma status register */
  350. BMDMA_PRD_LOW = 0x22c, /* bmdma PRD addr 31:0 */
  351. BMDMA_PRD_HIGH = 0x230, /* bmdma PRD addr 63:32 */
  352. /* Host private flags (hp_flags) */
  353. MV_HP_FLAG_MSI = (1 << 0),
  354. MV_HP_ERRATA_50XXB0 = (1 << 1),
  355. MV_HP_ERRATA_50XXB2 = (1 << 2),
  356. MV_HP_ERRATA_60X1B2 = (1 << 3),
  357. MV_HP_ERRATA_60X1C0 = (1 << 4),
  358. MV_HP_GEN_I = (1 << 6), /* Generation I: 50xx */
  359. MV_HP_GEN_II = (1 << 7), /* Generation II: 60xx */
  360. MV_HP_GEN_IIE = (1 << 8), /* Generation IIE: 6042/7042 */
  361. MV_HP_PCIE = (1 << 9), /* PCIe bus/regs: 7042 */
  362. MV_HP_CUT_THROUGH = (1 << 10), /* can use EDMA cut-through */
  363. MV_HP_FLAG_SOC = (1 << 11), /* SystemOnChip, no PCI */
  364. MV_HP_QUIRK_LED_BLINK_EN = (1 << 12), /* is led blinking enabled? */
  365. /* Port private flags (pp_flags) */
  366. MV_PP_FLAG_EDMA_EN = (1 << 0), /* is EDMA engine enabled? */
  367. MV_PP_FLAG_NCQ_EN = (1 << 1), /* is EDMA set up for NCQ? */
  368. MV_PP_FLAG_FBS_EN = (1 << 2), /* is EDMA set up for FBS? */
  369. MV_PP_FLAG_DELAYED_EH = (1 << 3), /* delayed dev err handling */
  370. MV_PP_FLAG_FAKE_ATA_BUSY = (1 << 4), /* ignore initial ATA_DRDY */
  371. };
  372. #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
  373. #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
  374. #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
  375. #define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
  376. #define IS_SOC(hpriv) ((hpriv)->hp_flags & MV_HP_FLAG_SOC)
  377. #define WINDOW_CTRL(i) (0x20030 + ((i) << 4))
  378. #define WINDOW_BASE(i) (0x20034 + ((i) << 4))
  379. enum {
  380. /* DMA boundary 0xffff is required by the s/g splitting
  381. * we need on /length/ in mv_fill-sg().
  382. */
  383. MV_DMA_BOUNDARY = 0xffffU,
  384. /* mask of register bits containing lower 32 bits
  385. * of EDMA request queue DMA address
  386. */
  387. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  388. /* ditto, for response queue */
  389. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  390. };
  391. enum chip_type {
  392. chip_504x,
  393. chip_508x,
  394. chip_5080,
  395. chip_604x,
  396. chip_608x,
  397. chip_6042,
  398. chip_7042,
  399. chip_soc,
  400. };
  401. /* Command ReQuest Block: 32B */
  402. struct mv_crqb {
  403. __le32 sg_addr;
  404. __le32 sg_addr_hi;
  405. __le16 ctrl_flags;
  406. __le16 ata_cmd[11];
  407. };
  408. struct mv_crqb_iie {
  409. __le32 addr;
  410. __le32 addr_hi;
  411. __le32 flags;
  412. __le32 len;
  413. __le32 ata_cmd[4];
  414. };
  415. /* Command ResPonse Block: 8B */
  416. struct mv_crpb {
  417. __le16 id;
  418. __le16 flags;
  419. __le32 tmstmp;
  420. };
  421. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  422. struct mv_sg {
  423. __le32 addr;
  424. __le32 flags_size;
  425. __le32 addr_hi;
  426. __le32 reserved;
  427. };
  428. /*
  429. * We keep a local cache of a few frequently accessed port
  430. * registers here, to avoid having to read them (very slow)
  431. * when switching between EDMA and non-EDMA modes.
  432. */
  433. struct mv_cached_regs {
  434. u32 fiscfg;
  435. u32 ltmode;
  436. u32 haltcond;
  437. u32 unknown_rsvd;
  438. };
  439. struct mv_port_priv {
  440. struct mv_crqb *crqb;
  441. dma_addr_t crqb_dma;
  442. struct mv_crpb *crpb;
  443. dma_addr_t crpb_dma;
  444. struct mv_sg *sg_tbl[MV_MAX_Q_DEPTH];
  445. dma_addr_t sg_tbl_dma[MV_MAX_Q_DEPTH];
  446. unsigned int req_idx;
  447. unsigned int resp_idx;
  448. u32 pp_flags;
  449. struct mv_cached_regs cached;
  450. unsigned int delayed_eh_pmp_map;
  451. };
  452. struct mv_port_signal {
  453. u32 amps;
  454. u32 pre;
  455. };
  456. struct mv_host_priv {
  457. u32 hp_flags;
  458. unsigned int board_idx;
  459. u32 main_irq_mask;
  460. struct mv_port_signal signal[8];
  461. const struct mv_hw_ops *ops;
  462. int n_ports;
  463. void __iomem *base;
  464. void __iomem *main_irq_cause_addr;
  465. void __iomem *main_irq_mask_addr;
  466. u32 irq_cause_offset;
  467. u32 irq_mask_offset;
  468. u32 unmask_all_irqs;
  469. #if defined(CONFIG_HAVE_CLK)
  470. struct clk *clk;
  471. struct clk **port_clks;
  472. #endif
  473. /*
  474. * These consistent DMA memory pools give us guaranteed
  475. * alignment for hardware-accessed data structures,
  476. * and less memory waste in accomplishing the alignment.
  477. */
  478. struct dma_pool *crqb_pool;
  479. struct dma_pool *crpb_pool;
  480. struct dma_pool *sg_tbl_pool;
  481. };
  482. struct mv_hw_ops {
  483. void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
  484. unsigned int port);
  485. void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
  486. void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
  487. void __iomem *mmio);
  488. int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
  489. unsigned int n_hc);
  490. void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
  491. void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
  492. };
  493. static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
  494. static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
  495. static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
  496. static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
  497. static int mv_port_start(struct ata_port *ap);
  498. static void mv_port_stop(struct ata_port *ap);
  499. static int mv_qc_defer(struct ata_queued_cmd *qc);
  500. static void mv_qc_prep(struct ata_queued_cmd *qc);
  501. static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
  502. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
  503. static int mv_hardreset(struct ata_link *link, unsigned int *class,
  504. unsigned long deadline);
  505. static void mv_eh_freeze(struct ata_port *ap);
  506. static void mv_eh_thaw(struct ata_port *ap);
  507. static void mv6_dev_config(struct ata_device *dev);
  508. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  509. unsigned int port);
  510. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  511. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  512. void __iomem *mmio);
  513. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  514. unsigned int n_hc);
  515. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  516. static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
  517. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  518. unsigned int port);
  519. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  520. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  521. void __iomem *mmio);
  522. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  523. unsigned int n_hc);
  524. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  525. static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
  526. void __iomem *mmio);
  527. static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
  528. void __iomem *mmio);
  529. static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
  530. void __iomem *mmio, unsigned int n_hc);
  531. static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
  532. void __iomem *mmio);
  533. static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
  534. static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv,
  535. void __iomem *mmio, unsigned int port);
  536. static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
  537. static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
  538. unsigned int port_no);
  539. static int mv_stop_edma(struct ata_port *ap);
  540. static int mv_stop_edma_engine(void __iomem *port_mmio);
  541. static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma);
  542. static void mv_pmp_select(struct ata_port *ap, int pmp);
  543. static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
  544. unsigned long deadline);
  545. static int mv_softreset(struct ata_link *link, unsigned int *class,
  546. unsigned long deadline);
  547. static void mv_pmp_error_handler(struct ata_port *ap);
  548. static void mv_process_crpb_entries(struct ata_port *ap,
  549. struct mv_port_priv *pp);
  550. static void mv_sff_irq_clear(struct ata_port *ap);
  551. static int mv_check_atapi_dma(struct ata_queued_cmd *qc);
  552. static void mv_bmdma_setup(struct ata_queued_cmd *qc);
  553. static void mv_bmdma_start(struct ata_queued_cmd *qc);
  554. static void mv_bmdma_stop(struct ata_queued_cmd *qc);
  555. static u8 mv_bmdma_status(struct ata_port *ap);
  556. static u8 mv_sff_check_status(struct ata_port *ap);
  557. /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
  558. * because we have to allow room for worst case splitting of
  559. * PRDs for 64K boundaries in mv_fill_sg().
  560. */
  561. #ifdef CONFIG_PCI
  562. static struct scsi_host_template mv5_sht = {
  563. ATA_BASE_SHT(DRV_NAME),
  564. .sg_tablesize = MV_MAX_SG_CT / 2,
  565. .dma_boundary = MV_DMA_BOUNDARY,
  566. };
  567. #endif
  568. static struct scsi_host_template mv6_sht = {
  569. ATA_NCQ_SHT(DRV_NAME),
  570. .can_queue = MV_MAX_Q_DEPTH - 1,
  571. .sg_tablesize = MV_MAX_SG_CT / 2,
  572. .dma_boundary = MV_DMA_BOUNDARY,
  573. };
  574. static struct ata_port_operations mv5_ops = {
  575. .inherits = &ata_sff_port_ops,
  576. .lost_interrupt = ATA_OP_NULL,
  577. .qc_defer = mv_qc_defer,
  578. .qc_prep = mv_qc_prep,
  579. .qc_issue = mv_qc_issue,
  580. .freeze = mv_eh_freeze,
  581. .thaw = mv_eh_thaw,
  582. .hardreset = mv_hardreset,
  583. .scr_read = mv5_scr_read,
  584. .scr_write = mv5_scr_write,
  585. .port_start = mv_port_start,
  586. .port_stop = mv_port_stop,
  587. };
  588. static struct ata_port_operations mv6_ops = {
  589. .inherits = &ata_bmdma_port_ops,
  590. .lost_interrupt = ATA_OP_NULL,
  591. .qc_defer = mv_qc_defer,
  592. .qc_prep = mv_qc_prep,
  593. .qc_issue = mv_qc_issue,
  594. .dev_config = mv6_dev_config,
  595. .freeze = mv_eh_freeze,
  596. .thaw = mv_eh_thaw,
  597. .hardreset = mv_hardreset,
  598. .softreset = mv_softreset,
  599. .pmp_hardreset = mv_pmp_hardreset,
  600. .pmp_softreset = mv_softreset,
  601. .error_handler = mv_pmp_error_handler,
  602. .scr_read = mv_scr_read,
  603. .scr_write = mv_scr_write,
  604. .sff_check_status = mv_sff_check_status,
  605. .sff_irq_clear = mv_sff_irq_clear,
  606. .check_atapi_dma = mv_check_atapi_dma,
  607. .bmdma_setup = mv_bmdma_setup,
  608. .bmdma_start = mv_bmdma_start,
  609. .bmdma_stop = mv_bmdma_stop,
  610. .bmdma_status = mv_bmdma_status,
  611. .port_start = mv_port_start,
  612. .port_stop = mv_port_stop,
  613. };
  614. static struct ata_port_operations mv_iie_ops = {
  615. .inherits = &mv6_ops,
  616. .dev_config = ATA_OP_NULL,
  617. .qc_prep = mv_qc_prep_iie,
  618. };
  619. static const struct ata_port_info mv_port_info[] = {
  620. { /* chip_504x */
  621. .flags = MV_GEN_I_FLAGS,
  622. .pio_mask = ATA_PIO4,
  623. .udma_mask = ATA_UDMA6,
  624. .port_ops = &mv5_ops,
  625. },
  626. { /* chip_508x */
  627. .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
  628. .pio_mask = ATA_PIO4,
  629. .udma_mask = ATA_UDMA6,
  630. .port_ops = &mv5_ops,
  631. },
  632. { /* chip_5080 */
  633. .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
  634. .pio_mask = ATA_PIO4,
  635. .udma_mask = ATA_UDMA6,
  636. .port_ops = &mv5_ops,
  637. },
  638. { /* chip_604x */
  639. .flags = MV_GEN_II_FLAGS,
  640. .pio_mask = ATA_PIO4,
  641. .udma_mask = ATA_UDMA6,
  642. .port_ops = &mv6_ops,
  643. },
  644. { /* chip_608x */
  645. .flags = MV_GEN_II_FLAGS | MV_FLAG_DUAL_HC,
  646. .pio_mask = ATA_PIO4,
  647. .udma_mask = ATA_UDMA6,
  648. .port_ops = &mv6_ops,
  649. },
  650. { /* chip_6042 */
  651. .flags = MV_GEN_IIE_FLAGS,
  652. .pio_mask = ATA_PIO4,
  653. .udma_mask = ATA_UDMA6,
  654. .port_ops = &mv_iie_ops,
  655. },
  656. { /* chip_7042 */
  657. .flags = MV_GEN_IIE_FLAGS,
  658. .pio_mask = ATA_PIO4,
  659. .udma_mask = ATA_UDMA6,
  660. .port_ops = &mv_iie_ops,
  661. },
  662. { /* chip_soc */
  663. .flags = MV_GEN_IIE_FLAGS,
  664. .pio_mask = ATA_PIO4,
  665. .udma_mask = ATA_UDMA6,
  666. .port_ops = &mv_iie_ops,
  667. },
  668. };
  669. static const struct pci_device_id mv_pci_tbl[] = {
  670. { PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
  671. { PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
  672. { PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
  673. { PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
  674. /* RocketRAID 1720/174x have different identifiers */
  675. { PCI_VDEVICE(TTI, 0x1720), chip_6042 },
  676. { PCI_VDEVICE(TTI, 0x1740), chip_6042 },
  677. { PCI_VDEVICE(TTI, 0x1742), chip_6042 },
  678. { PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
  679. { PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
  680. { PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
  681. { PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
  682. { PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
  683. { PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
  684. /* Adaptec 1430SA */
  685. { PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
  686. /* Marvell 7042 support */
  687. { PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
  688. /* Highpoint RocketRAID PCIe series */
  689. { PCI_VDEVICE(TTI, 0x2300), chip_7042 },
  690. { PCI_VDEVICE(TTI, 0x2310), chip_7042 },
  691. { } /* terminate list */
  692. };
  693. static const struct mv_hw_ops mv5xxx_ops = {
  694. .phy_errata = mv5_phy_errata,
  695. .enable_leds = mv5_enable_leds,
  696. .read_preamp = mv5_read_preamp,
  697. .reset_hc = mv5_reset_hc,
  698. .reset_flash = mv5_reset_flash,
  699. .reset_bus = mv5_reset_bus,
  700. };
  701. static const struct mv_hw_ops mv6xxx_ops = {
  702. .phy_errata = mv6_phy_errata,
  703. .enable_leds = mv6_enable_leds,
  704. .read_preamp = mv6_read_preamp,
  705. .reset_hc = mv6_reset_hc,
  706. .reset_flash = mv6_reset_flash,
  707. .reset_bus = mv_reset_pci_bus,
  708. };
  709. static const struct mv_hw_ops mv_soc_ops = {
  710. .phy_errata = mv6_phy_errata,
  711. .enable_leds = mv_soc_enable_leds,
  712. .read_preamp = mv_soc_read_preamp,
  713. .reset_hc = mv_soc_reset_hc,
  714. .reset_flash = mv_soc_reset_flash,
  715. .reset_bus = mv_soc_reset_bus,
  716. };
  717. static const struct mv_hw_ops mv_soc_65n_ops = {
  718. .phy_errata = mv_soc_65n_phy_errata,
  719. .enable_leds = mv_soc_enable_leds,
  720. .reset_hc = mv_soc_reset_hc,
  721. .reset_flash = mv_soc_reset_flash,
  722. .reset_bus = mv_soc_reset_bus,
  723. };
  724. /*
  725. * Functions
  726. */
  727. static inline void writelfl(unsigned long data, void __iomem *addr)
  728. {
  729. writel(data, addr);
  730. (void) readl(addr); /* flush to avoid PCI posted write */
  731. }
  732. static inline unsigned int mv_hc_from_port(unsigned int port)
  733. {
  734. return port >> MV_PORT_HC_SHIFT;
  735. }
  736. static inline unsigned int mv_hardport_from_port(unsigned int port)
  737. {
  738. return port & MV_PORT_MASK;
  739. }
  740. /*
  741. * Consolidate some rather tricky bit shift calculations.
  742. * This is hot-path stuff, so not a function.
  743. * Simple code, with two return values, so macro rather than inline.
  744. *
  745. * port is the sole input, in range 0..7.
  746. * shift is one output, for use with main_irq_cause / main_irq_mask registers.
  747. * hardport is the other output, in range 0..3.
  748. *
  749. * Note that port and hardport may be the same variable in some cases.
  750. */
  751. #define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport) \
  752. { \
  753. shift = mv_hc_from_port(port) * HC_SHIFT; \
  754. hardport = mv_hardport_from_port(port); \
  755. shift += hardport * 2; \
  756. }
  757. static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
  758. {
  759. return (base + SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
  760. }
  761. static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
  762. unsigned int port)
  763. {
  764. return mv_hc_base(base, mv_hc_from_port(port));
  765. }
  766. static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
  767. {
  768. return mv_hc_base_from_port(base, port) +
  769. MV_SATAHC_ARBTR_REG_SZ +
  770. (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
  771. }
  772. static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
  773. {
  774. void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
  775. unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
  776. return hc_mmio + ofs;
  777. }
  778. static inline void __iomem *mv_host_base(struct ata_host *host)
  779. {
  780. struct mv_host_priv *hpriv = host->private_data;
  781. return hpriv->base;
  782. }
  783. static inline void __iomem *mv_ap_base(struct ata_port *ap)
  784. {
  785. return mv_port_base(mv_host_base(ap->host), ap->port_no);
  786. }
  787. static inline int mv_get_hc_count(unsigned long port_flags)
  788. {
  789. return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
  790. }
  791. /**
  792. * mv_save_cached_regs - (re-)initialize cached port registers
  793. * @ap: the port whose registers we are caching
  794. *
  795. * Initialize the local cache of port registers,
  796. * so that reading them over and over again can
  797. * be avoided on the hotter paths of this driver.
  798. * This saves a few microseconds each time we switch
  799. * to/from EDMA mode to perform (eg.) a drive cache flush.
  800. */
  801. static void mv_save_cached_regs(struct ata_port *ap)
  802. {
  803. void __iomem *port_mmio = mv_ap_base(ap);
  804. struct mv_port_priv *pp = ap->private_data;
  805. pp->cached.fiscfg = readl(port_mmio + FISCFG);
  806. pp->cached.ltmode = readl(port_mmio + LTMODE);
  807. pp->cached.haltcond = readl(port_mmio + EDMA_HALTCOND);
  808. pp->cached.unknown_rsvd = readl(port_mmio + EDMA_UNKNOWN_RSVD);
  809. }
  810. /**
  811. * mv_write_cached_reg - write to a cached port register
  812. * @addr: hardware address of the register
  813. * @old: pointer to cached value of the register
  814. * @new: new value for the register
  815. *
  816. * Write a new value to a cached register,
  817. * but only if the value is different from before.
  818. */
  819. static inline void mv_write_cached_reg(void __iomem *addr, u32 *old, u32 new)
  820. {
  821. if (new != *old) {
  822. unsigned long laddr;
  823. *old = new;
  824. /*
  825. * Workaround for 88SX60x1-B2 FEr SATA#13:
  826. * Read-after-write is needed to prevent generating 64-bit
  827. * write cycles on the PCI bus for SATA interface registers
  828. * at offsets ending in 0x4 or 0xc.
  829. *
  830. * Looks like a lot of fuss, but it avoids an unnecessary
  831. * +1 usec read-after-write delay for unaffected registers.
  832. */
  833. laddr = (long)addr & 0xffff;
  834. if (laddr >= 0x300 && laddr <= 0x33c) {
  835. laddr &= 0x000f;
  836. if (laddr == 0x4 || laddr == 0xc) {
  837. writelfl(new, addr); /* read after write */
  838. return;
  839. }
  840. }
  841. writel(new, addr); /* unaffected by the errata */
  842. }
  843. }
  844. static void mv_set_edma_ptrs(void __iomem *port_mmio,
  845. struct mv_host_priv *hpriv,
  846. struct mv_port_priv *pp)
  847. {
  848. u32 index;
  849. /*
  850. * initialize request queue
  851. */
  852. pp->req_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
  853. index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
  854. WARN_ON(pp->crqb_dma & 0x3ff);
  855. writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI);
  856. writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
  857. port_mmio + EDMA_REQ_Q_IN_PTR);
  858. writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR);
  859. /*
  860. * initialize response queue
  861. */
  862. pp->resp_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
  863. index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
  864. WARN_ON(pp->crpb_dma & 0xff);
  865. writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI);
  866. writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR);
  867. writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
  868. port_mmio + EDMA_RSP_Q_OUT_PTR);
  869. }
  870. static void mv_write_main_irq_mask(u32 mask, struct mv_host_priv *hpriv)
  871. {
  872. /*
  873. * When writing to the main_irq_mask in hardware,
  874. * we must ensure exclusivity between the interrupt coalescing bits
  875. * and the corresponding individual port DONE_IRQ bits.
  876. *
  877. * Note that this register is really an "IRQ enable" register,
  878. * not an "IRQ mask" register as Marvell's naming might suggest.
  879. */
  880. if (mask & (ALL_PORTS_COAL_DONE | PORTS_0_3_COAL_DONE))
  881. mask &= ~DONE_IRQ_0_3;
  882. if (mask & (ALL_PORTS_COAL_DONE | PORTS_4_7_COAL_DONE))
  883. mask &= ~DONE_IRQ_4_7;
  884. writelfl(mask, hpriv->main_irq_mask_addr);
  885. }
  886. static void mv_set_main_irq_mask(struct ata_host *host,
  887. u32 disable_bits, u32 enable_bits)
  888. {
  889. struct mv_host_priv *hpriv = host->private_data;
  890. u32 old_mask, new_mask;
  891. old_mask = hpriv->main_irq_mask;
  892. new_mask = (old_mask & ~disable_bits) | enable_bits;
  893. if (new_mask != old_mask) {
  894. hpriv->main_irq_mask = new_mask;
  895. mv_write_main_irq_mask(new_mask, hpriv);
  896. }
  897. }
  898. static void mv_enable_port_irqs(struct ata_port *ap,
  899. unsigned int port_bits)
  900. {
  901. unsigned int shift, hardport, port = ap->port_no;
  902. u32 disable_bits, enable_bits;
  903. MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
  904. disable_bits = (DONE_IRQ | ERR_IRQ) << shift;
  905. enable_bits = port_bits << shift;
  906. mv_set_main_irq_mask(ap->host, disable_bits, enable_bits);
  907. }
  908. static void mv_clear_and_enable_port_irqs(struct ata_port *ap,
  909. void __iomem *port_mmio,
  910. unsigned int port_irqs)
  911. {
  912. struct mv_host_priv *hpriv = ap->host->private_data;
  913. int hardport = mv_hardport_from_port(ap->port_no);
  914. void __iomem *hc_mmio = mv_hc_base_from_port(
  915. mv_host_base(ap->host), ap->port_no);
  916. u32 hc_irq_cause;
  917. /* clear EDMA event indicators, if any */
  918. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
  919. /* clear pending irq events */
  920. hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
  921. writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE);
  922. /* clear FIS IRQ Cause */
  923. if (IS_GEN_IIE(hpriv))
  924. writelfl(0, port_mmio + FIS_IRQ_CAUSE);
  925. mv_enable_port_irqs(ap, port_irqs);
  926. }
  927. static void mv_set_irq_coalescing(struct ata_host *host,
  928. unsigned int count, unsigned int usecs)
  929. {
  930. struct mv_host_priv *hpriv = host->private_data;
  931. void __iomem *mmio = hpriv->base, *hc_mmio;
  932. u32 coal_enable = 0;
  933. unsigned long flags;
  934. unsigned int clks, is_dual_hc = hpriv->n_ports > MV_PORTS_PER_HC;
  935. const u32 coal_disable = PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
  936. ALL_PORTS_COAL_DONE;
  937. /* Disable IRQ coalescing if either threshold is zero */
  938. if (!usecs || !count) {
  939. clks = count = 0;
  940. } else {
  941. /* Respect maximum limits of the hardware */
  942. clks = usecs * COAL_CLOCKS_PER_USEC;
  943. if (clks > MAX_COAL_TIME_THRESHOLD)
  944. clks = MAX_COAL_TIME_THRESHOLD;
  945. if (count > MAX_COAL_IO_COUNT)
  946. count = MAX_COAL_IO_COUNT;
  947. }
  948. spin_lock_irqsave(&host->lock, flags);
  949. mv_set_main_irq_mask(host, coal_disable, 0);
  950. if (is_dual_hc && !IS_GEN_I(hpriv)) {
  951. /*
  952. * GEN_II/GEN_IIE with dual host controllers:
  953. * one set of global thresholds for the entire chip.
  954. */
  955. writel(clks, mmio + IRQ_COAL_TIME_THRESHOLD);
  956. writel(count, mmio + IRQ_COAL_IO_THRESHOLD);
  957. /* clear leftover coal IRQ bit */
  958. writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE);
  959. if (count)
  960. coal_enable = ALL_PORTS_COAL_DONE;
  961. clks = count = 0; /* force clearing of regular regs below */
  962. }
  963. /*
  964. * All chips: independent thresholds for each HC on the chip.
  965. */
  966. hc_mmio = mv_hc_base_from_port(mmio, 0);
  967. writel(clks, hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD);
  968. writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD);
  969. writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE);
  970. if (count)
  971. coal_enable |= PORTS_0_3_COAL_DONE;
  972. if (is_dual_hc) {
  973. hc_mmio = mv_hc_base_from_port(mmio, MV_PORTS_PER_HC);
  974. writel(clks, hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD);
  975. writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD);
  976. writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE);
  977. if (count)
  978. coal_enable |= PORTS_4_7_COAL_DONE;
  979. }
  980. mv_set_main_irq_mask(host, 0, coal_enable);
  981. spin_unlock_irqrestore(&host->lock, flags);
  982. }
  983. /**
  984. * mv_start_edma - Enable eDMA engine
  985. * @base: port base address
  986. * @pp: port private data
  987. *
  988. * Verify the local cache of the eDMA state is accurate with a
  989. * WARN_ON.
  990. *
  991. * LOCKING:
  992. * Inherited from caller.
  993. */
  994. static void mv_start_edma(struct ata_port *ap, void __iomem *port_mmio,
  995. struct mv_port_priv *pp, u8 protocol)
  996. {
  997. int want_ncq = (protocol == ATA_PROT_NCQ);
  998. if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
  999. int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
  1000. if (want_ncq != using_ncq)
  1001. mv_stop_edma(ap);
  1002. }
  1003. if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
  1004. struct mv_host_priv *hpriv = ap->host->private_data;
  1005. mv_edma_cfg(ap, want_ncq, 1);
  1006. mv_set_edma_ptrs(port_mmio, hpriv, pp);
  1007. mv_clear_and_enable_port_irqs(ap, port_mmio, DONE_IRQ|ERR_IRQ);
  1008. writelfl(EDMA_EN, port_mmio + EDMA_CMD);
  1009. pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
  1010. }
  1011. }
  1012. static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
  1013. {
  1014. void __iomem *port_mmio = mv_ap_base(ap);
  1015. const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
  1016. const int per_loop = 5, timeout = (15 * 1000 / per_loop);
  1017. int i;
  1018. /*
  1019. * Wait for the EDMA engine to finish transactions in progress.
  1020. * No idea what a good "timeout" value might be, but measurements
  1021. * indicate that it often requires hundreds of microseconds
  1022. * with two drives in-use. So we use the 15msec value above
  1023. * as a rough guess at what even more drives might require.
  1024. */
  1025. for (i = 0; i < timeout; ++i) {
  1026. u32 edma_stat = readl(port_mmio + EDMA_STATUS);
  1027. if ((edma_stat & empty_idle) == empty_idle)
  1028. break;
  1029. udelay(per_loop);
  1030. }
  1031. /* ata_port_info(ap, "%s: %u+ usecs\n", __func__, i); */
  1032. }
  1033. /**
  1034. * mv_stop_edma_engine - Disable eDMA engine
  1035. * @port_mmio: io base address
  1036. *
  1037. * LOCKING:
  1038. * Inherited from caller.
  1039. */
  1040. static int mv_stop_edma_engine(void __iomem *port_mmio)
  1041. {
  1042. int i;
  1043. /* Disable eDMA. The disable bit auto clears. */
  1044. writelfl(EDMA_DS, port_mmio + EDMA_CMD);
  1045. /* Wait for the chip to confirm eDMA is off. */
  1046. for (i = 10000; i > 0; i--) {
  1047. u32 reg = readl(port_mmio + EDMA_CMD);
  1048. if (!(reg & EDMA_EN))
  1049. return 0;
  1050. udelay(10);
  1051. }
  1052. return -EIO;
  1053. }
  1054. static int mv_stop_edma(struct ata_port *ap)
  1055. {
  1056. void __iomem *port_mmio = mv_ap_base(ap);
  1057. struct mv_port_priv *pp = ap->private_data;
  1058. int err = 0;
  1059. if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
  1060. return 0;
  1061. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1062. mv_wait_for_edma_empty_idle(ap);
  1063. if (mv_stop_edma_engine(port_mmio)) {
  1064. ata_port_err(ap, "Unable to stop eDMA\n");
  1065. err = -EIO;
  1066. }
  1067. mv_edma_cfg(ap, 0, 0);
  1068. return err;
  1069. }
  1070. #ifdef ATA_DEBUG
  1071. static void mv_dump_mem(void __iomem *start, unsigned bytes)
  1072. {
  1073. int b, w;
  1074. for (b = 0; b < bytes; ) {
  1075. DPRINTK("%p: ", start + b);
  1076. for (w = 0; b < bytes && w < 4; w++) {
  1077. printk("%08x ", readl(start + b));
  1078. b += sizeof(u32);
  1079. }
  1080. printk("\n");
  1081. }
  1082. }
  1083. #endif
  1084. #if defined(ATA_DEBUG) || defined(CONFIG_PCI)
  1085. static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
  1086. {
  1087. #ifdef ATA_DEBUG
  1088. int b, w;
  1089. u32 dw;
  1090. for (b = 0; b < bytes; ) {
  1091. DPRINTK("%02x: ", b);
  1092. for (w = 0; b < bytes && w < 4; w++) {
  1093. (void) pci_read_config_dword(pdev, b, &dw);
  1094. printk("%08x ", dw);
  1095. b += sizeof(u32);
  1096. }
  1097. printk("\n");
  1098. }
  1099. #endif
  1100. }
  1101. #endif
  1102. static void mv_dump_all_regs(void __iomem *mmio_base, int port,
  1103. struct pci_dev *pdev)
  1104. {
  1105. #ifdef ATA_DEBUG
  1106. void __iomem *hc_base = mv_hc_base(mmio_base,
  1107. port >> MV_PORT_HC_SHIFT);
  1108. void __iomem *port_base;
  1109. int start_port, num_ports, p, start_hc, num_hcs, hc;
  1110. if (0 > port) {
  1111. start_hc = start_port = 0;
  1112. num_ports = 8; /* shld be benign for 4 port devs */
  1113. num_hcs = 2;
  1114. } else {
  1115. start_hc = port >> MV_PORT_HC_SHIFT;
  1116. start_port = port;
  1117. num_ports = num_hcs = 1;
  1118. }
  1119. DPRINTK("All registers for port(s) %u-%u:\n", start_port,
  1120. num_ports > 1 ? num_ports - 1 : start_port);
  1121. if (NULL != pdev) {
  1122. DPRINTK("PCI config space regs:\n");
  1123. mv_dump_pci_cfg(pdev, 0x68);
  1124. }
  1125. DPRINTK("PCI regs:\n");
  1126. mv_dump_mem(mmio_base+0xc00, 0x3c);
  1127. mv_dump_mem(mmio_base+0xd00, 0x34);
  1128. mv_dump_mem(mmio_base+0xf00, 0x4);
  1129. mv_dump_mem(mmio_base+0x1d00, 0x6c);
  1130. for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
  1131. hc_base = mv_hc_base(mmio_base, hc);
  1132. DPRINTK("HC regs (HC %i):\n", hc);
  1133. mv_dump_mem(hc_base, 0x1c);
  1134. }
  1135. for (p = start_port; p < start_port + num_ports; p++) {
  1136. port_base = mv_port_base(mmio_base, p);
  1137. DPRINTK("EDMA regs (port %i):\n", p);
  1138. mv_dump_mem(port_base, 0x54);
  1139. DPRINTK("SATA regs (port %i):\n", p);
  1140. mv_dump_mem(port_base+0x300, 0x60);
  1141. }
  1142. #endif
  1143. }
  1144. static unsigned int mv_scr_offset(unsigned int sc_reg_in)
  1145. {
  1146. unsigned int ofs;
  1147. switch (sc_reg_in) {
  1148. case SCR_STATUS:
  1149. case SCR_CONTROL:
  1150. case SCR_ERROR:
  1151. ofs = SATA_STATUS + (sc_reg_in * sizeof(u32));
  1152. break;
  1153. case SCR_ACTIVE:
  1154. ofs = SATA_ACTIVE; /* active is not with the others */
  1155. break;
  1156. default:
  1157. ofs = 0xffffffffU;
  1158. break;
  1159. }
  1160. return ofs;
  1161. }
  1162. static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
  1163. {
  1164. unsigned int ofs = mv_scr_offset(sc_reg_in);
  1165. if (ofs != 0xffffffffU) {
  1166. *val = readl(mv_ap_base(link->ap) + ofs);
  1167. return 0;
  1168. } else
  1169. return -EINVAL;
  1170. }
  1171. static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
  1172. {
  1173. unsigned int ofs = mv_scr_offset(sc_reg_in);
  1174. if (ofs != 0xffffffffU) {
  1175. void __iomem *addr = mv_ap_base(link->ap) + ofs;
  1176. if (sc_reg_in == SCR_CONTROL) {
  1177. /*
  1178. * Workaround for 88SX60x1 FEr SATA#26:
  1179. *
  1180. * COMRESETs have to take care not to accidentally
  1181. * put the drive to sleep when writing SCR_CONTROL.
  1182. * Setting bits 12..15 prevents this problem.
  1183. *
  1184. * So if we see an outbound COMMRESET, set those bits.
  1185. * Ditto for the followup write that clears the reset.
  1186. *
  1187. * The proprietary driver does this for
  1188. * all chip versions, and so do we.
  1189. */
  1190. if ((val & 0xf) == 1 || (readl(addr) & 0xf) == 1)
  1191. val |= 0xf000;
  1192. }
  1193. writelfl(val, addr);
  1194. return 0;
  1195. } else
  1196. return -EINVAL;
  1197. }
  1198. static void mv6_dev_config(struct ata_device *adev)
  1199. {
  1200. /*
  1201. * Deal with Gen-II ("mv6") hardware quirks/restrictions:
  1202. *
  1203. * Gen-II does not support NCQ over a port multiplier
  1204. * (no FIS-based switching).
  1205. */
  1206. if (adev->flags & ATA_DFLAG_NCQ) {
  1207. if (sata_pmp_attached(adev->link->ap)) {
  1208. adev->flags &= ~ATA_DFLAG_NCQ;
  1209. ata_dev_info(adev,
  1210. "NCQ disabled for command-based switching\n");
  1211. }
  1212. }
  1213. }
  1214. static int mv_qc_defer(struct ata_queued_cmd *qc)
  1215. {
  1216. struct ata_link *link = qc->dev->link;
  1217. struct ata_port *ap = link->ap;
  1218. struct mv_port_priv *pp = ap->private_data;
  1219. /*
  1220. * Don't allow new commands if we're in a delayed EH state
  1221. * for NCQ and/or FIS-based switching.
  1222. */
  1223. if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
  1224. return ATA_DEFER_PORT;
  1225. /* PIO commands need exclusive link: no other commands [DMA or PIO]
  1226. * can run concurrently.
  1227. * set excl_link when we want to send a PIO command in DMA mode
  1228. * or a non-NCQ command in NCQ mode.
  1229. * When we receive a command from that link, and there are no
  1230. * outstanding commands, mark a flag to clear excl_link and let
  1231. * the command go through.
  1232. */
  1233. if (unlikely(ap->excl_link)) {
  1234. if (link == ap->excl_link) {
  1235. if (ap->nr_active_links)
  1236. return ATA_DEFER_PORT;
  1237. qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
  1238. return 0;
  1239. } else
  1240. return ATA_DEFER_PORT;
  1241. }
  1242. /*
  1243. * If the port is completely idle, then allow the new qc.
  1244. */
  1245. if (ap->nr_active_links == 0)
  1246. return 0;
  1247. /*
  1248. * The port is operating in host queuing mode (EDMA) with NCQ
  1249. * enabled, allow multiple NCQ commands. EDMA also allows
  1250. * queueing multiple DMA commands but libata core currently
  1251. * doesn't allow it.
  1252. */
  1253. if ((pp->pp_flags & MV_PP_FLAG_EDMA_EN) &&
  1254. (pp->pp_flags & MV_PP_FLAG_NCQ_EN)) {
  1255. if (ata_is_ncq(qc->tf.protocol))
  1256. return 0;
  1257. else {
  1258. ap->excl_link = link;
  1259. return ATA_DEFER_PORT;
  1260. }
  1261. }
  1262. return ATA_DEFER_PORT;
  1263. }
  1264. static void mv_config_fbs(struct ata_port *ap, int want_ncq, int want_fbs)
  1265. {
  1266. struct mv_port_priv *pp = ap->private_data;
  1267. void __iomem *port_mmio;
  1268. u32 fiscfg, *old_fiscfg = &pp->cached.fiscfg;
  1269. u32 ltmode, *old_ltmode = &pp->cached.ltmode;
  1270. u32 haltcond, *old_haltcond = &pp->cached.haltcond;
  1271. ltmode = *old_ltmode & ~LTMODE_BIT8;
  1272. haltcond = *old_haltcond | EDMA_ERR_DEV;
  1273. if (want_fbs) {
  1274. fiscfg = *old_fiscfg | FISCFG_SINGLE_SYNC;
  1275. ltmode = *old_ltmode | LTMODE_BIT8;
  1276. if (want_ncq)
  1277. haltcond &= ~EDMA_ERR_DEV;
  1278. else
  1279. fiscfg |= FISCFG_WAIT_DEV_ERR;
  1280. } else {
  1281. fiscfg = *old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR);
  1282. }
  1283. port_mmio = mv_ap_base(ap);
  1284. mv_write_cached_reg(port_mmio + FISCFG, old_fiscfg, fiscfg);
  1285. mv_write_cached_reg(port_mmio + LTMODE, old_ltmode, ltmode);
  1286. mv_write_cached_reg(port_mmio + EDMA_HALTCOND, old_haltcond, haltcond);
  1287. }
  1288. static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq)
  1289. {
  1290. struct mv_host_priv *hpriv = ap->host->private_data;
  1291. u32 old, new;
  1292. /* workaround for 88SX60x1 FEr SATA#25 (part 1) */
  1293. old = readl(hpriv->base + GPIO_PORT_CTL);
  1294. if (want_ncq)
  1295. new = old | (1 << 22);
  1296. else
  1297. new = old & ~(1 << 22);
  1298. if (new != old)
  1299. writel(new, hpriv->base + GPIO_PORT_CTL);
  1300. }
  1301. /**
  1302. * mv_bmdma_enable - set a magic bit on GEN_IIE to allow bmdma
  1303. * @ap: Port being initialized
  1304. *
  1305. * There are two DMA modes on these chips: basic DMA, and EDMA.
  1306. *
  1307. * Bit-0 of the "EDMA RESERVED" register enables/disables use
  1308. * of basic DMA on the GEN_IIE versions of the chips.
  1309. *
  1310. * This bit survives EDMA resets, and must be set for basic DMA
  1311. * to function, and should be cleared when EDMA is active.
  1312. */
  1313. static void mv_bmdma_enable_iie(struct ata_port *ap, int enable_bmdma)
  1314. {
  1315. struct mv_port_priv *pp = ap->private_data;
  1316. u32 new, *old = &pp->cached.unknown_rsvd;
  1317. if (enable_bmdma)
  1318. new = *old | 1;
  1319. else
  1320. new = *old & ~1;
  1321. mv_write_cached_reg(mv_ap_base(ap) + EDMA_UNKNOWN_RSVD, old, new);
  1322. }
  1323. /*
  1324. * SOC chips have an issue whereby the HDD LEDs don't always blink
  1325. * during I/O when NCQ is enabled. Enabling a special "LED blink" mode
  1326. * of the SOC takes care of it, generating a steady blink rate when
  1327. * any drive on the chip is active.
  1328. *
  1329. * Unfortunately, the blink mode is a global hardware setting for the SOC,
  1330. * so we must use it whenever at least one port on the SOC has NCQ enabled.
  1331. *
  1332. * We turn "LED blink" off when NCQ is not in use anywhere, because the normal
  1333. * LED operation works then, and provides better (more accurate) feedback.
  1334. *
  1335. * Note that this code assumes that an SOC never has more than one HC onboard.
  1336. */
  1337. static void mv_soc_led_blink_enable(struct ata_port *ap)
  1338. {
  1339. struct ata_host *host = ap->host;
  1340. struct mv_host_priv *hpriv = host->private_data;
  1341. void __iomem *hc_mmio;
  1342. u32 led_ctrl;
  1343. if (hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN)
  1344. return;
  1345. hpriv->hp_flags |= MV_HP_QUIRK_LED_BLINK_EN;
  1346. hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
  1347. led_ctrl = readl(hc_mmio + SOC_LED_CTRL);
  1348. writel(led_ctrl | SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL);
  1349. }
  1350. static void mv_soc_led_blink_disable(struct ata_port *ap)
  1351. {
  1352. struct ata_host *host = ap->host;
  1353. struct mv_host_priv *hpriv = host->private_data;
  1354. void __iomem *hc_mmio;
  1355. u32 led_ctrl;
  1356. unsigned int port;
  1357. if (!(hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN))
  1358. return;
  1359. /* disable led-blink only if no ports are using NCQ */
  1360. for (port = 0; port < hpriv->n_ports; port++) {
  1361. struct ata_port *this_ap = host->ports[port];
  1362. struct mv_port_priv *pp = this_ap->private_data;
  1363. if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
  1364. return;
  1365. }
  1366. hpriv->hp_flags &= ~MV_HP_QUIRK_LED_BLINK_EN;
  1367. hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
  1368. led_ctrl = readl(hc_mmio + SOC_LED_CTRL);
  1369. writel(led_ctrl & ~SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL);
  1370. }
  1371. static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma)
  1372. {
  1373. u32 cfg;
  1374. struct mv_port_priv *pp = ap->private_data;
  1375. struct mv_host_priv *hpriv = ap->host->private_data;
  1376. void __iomem *port_mmio = mv_ap_base(ap);
  1377. /* set up non-NCQ EDMA configuration */
  1378. cfg = EDMA_CFG_Q_DEPTH; /* always 0x1f for *all* chips */
  1379. pp->pp_flags &=
  1380. ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
  1381. if (IS_GEN_I(hpriv))
  1382. cfg |= (1 << 8); /* enab config burst size mask */
  1383. else if (IS_GEN_II(hpriv)) {
  1384. cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
  1385. mv_60x1_errata_sata25(ap, want_ncq);
  1386. } else if (IS_GEN_IIE(hpriv)) {
  1387. int want_fbs = sata_pmp_attached(ap);
  1388. /*
  1389. * Possible future enhancement:
  1390. *
  1391. * The chip can use FBS with non-NCQ, if we allow it,
  1392. * But first we need to have the error handling in place
  1393. * for this mode (datasheet section 7.3.15.4.2.3).
  1394. * So disallow non-NCQ FBS for now.
  1395. */
  1396. want_fbs &= want_ncq;
  1397. mv_config_fbs(ap, want_ncq, want_fbs);
  1398. if (want_fbs) {
  1399. pp->pp_flags |= MV_PP_FLAG_FBS_EN;
  1400. cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
  1401. }
  1402. cfg |= (1 << 23); /* do not mask PM field in rx'd FIS */
  1403. if (want_edma) {
  1404. cfg |= (1 << 22); /* enab 4-entry host queue cache */
  1405. if (!IS_SOC(hpriv))
  1406. cfg |= (1 << 18); /* enab early completion */
  1407. }
  1408. if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
  1409. cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
  1410. mv_bmdma_enable_iie(ap, !want_edma);
  1411. if (IS_SOC(hpriv)) {
  1412. if (want_ncq)
  1413. mv_soc_led_blink_enable(ap);
  1414. else
  1415. mv_soc_led_blink_disable(ap);
  1416. }
  1417. }
  1418. if (want_ncq) {
  1419. cfg |= EDMA_CFG_NCQ;
  1420. pp->pp_flags |= MV_PP_FLAG_NCQ_EN;
  1421. }
  1422. writelfl(cfg, port_mmio + EDMA_CFG);
  1423. }
  1424. static void mv_port_free_dma_mem(struct ata_port *ap)
  1425. {
  1426. struct mv_host_priv *hpriv = ap->host->private_data;
  1427. struct mv_port_priv *pp = ap->private_data;
  1428. int tag;
  1429. if (pp->crqb) {
  1430. dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
  1431. pp->crqb = NULL;
  1432. }
  1433. if (pp->crpb) {
  1434. dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
  1435. pp->crpb = NULL;
  1436. }
  1437. /*
  1438. * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
  1439. * For later hardware, we have one unique sg_tbl per NCQ tag.
  1440. */
  1441. for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
  1442. if (pp->sg_tbl[tag]) {
  1443. if (tag == 0 || !IS_GEN_I(hpriv))
  1444. dma_pool_free(hpriv->sg_tbl_pool,
  1445. pp->sg_tbl[tag],
  1446. pp->sg_tbl_dma[tag]);
  1447. pp->sg_tbl[tag] = NULL;
  1448. }
  1449. }
  1450. }
  1451. /**
  1452. * mv_port_start - Port specific init/start routine.
  1453. * @ap: ATA channel to manipulate
  1454. *
  1455. * Allocate and point to DMA memory, init port private memory,
  1456. * zero indices.
  1457. *
  1458. * LOCKING:
  1459. * Inherited from caller.
  1460. */
  1461. static int mv_port_start(struct ata_port *ap)
  1462. {
  1463. struct device *dev = ap->host->dev;
  1464. struct mv_host_priv *hpriv = ap->host->private_data;
  1465. struct mv_port_priv *pp;
  1466. unsigned long flags;
  1467. int tag;
  1468. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1469. if (!pp)
  1470. return -ENOMEM;
  1471. ap->private_data = pp;
  1472. pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
  1473. if (!pp->crqb)
  1474. return -ENOMEM;
  1475. memset(pp->crqb, 0, MV_CRQB_Q_SZ);
  1476. pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
  1477. if (!pp->crpb)
  1478. goto out_port_free_dma_mem;
  1479. memset(pp->crpb, 0, MV_CRPB_Q_SZ);
  1480. /* 6041/6081 Rev. "C0" (and newer) are okay with async notify */
  1481. if (hpriv->hp_flags & MV_HP_ERRATA_60X1C0)
  1482. ap->flags |= ATA_FLAG_AN;
  1483. /*
  1484. * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
  1485. * For later hardware, we need one unique sg_tbl per NCQ tag.
  1486. */
  1487. for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
  1488. if (tag == 0 || !IS_GEN_I(hpriv)) {
  1489. pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
  1490. GFP_KERNEL, &pp->sg_tbl_dma[tag]);
  1491. if (!pp->sg_tbl[tag])
  1492. goto out_port_free_dma_mem;
  1493. } else {
  1494. pp->sg_tbl[tag] = pp->sg_tbl[0];
  1495. pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
  1496. }
  1497. }
  1498. spin_lock_irqsave(ap->lock, flags);
  1499. mv_save_cached_regs(ap);
  1500. mv_edma_cfg(ap, 0, 0);
  1501. spin_unlock_irqrestore(ap->lock, flags);
  1502. return 0;
  1503. out_port_free_dma_mem:
  1504. mv_port_free_dma_mem(ap);
  1505. return -ENOMEM;
  1506. }
  1507. /**
  1508. * mv_port_stop - Port specific cleanup/stop routine.
  1509. * @ap: ATA channel to manipulate
  1510. *
  1511. * Stop DMA, cleanup port memory.
  1512. *
  1513. * LOCKING:
  1514. * This routine uses the host lock to protect the DMA stop.
  1515. */
  1516. static void mv_port_stop(struct ata_port *ap)
  1517. {
  1518. unsigned long flags;
  1519. spin_lock_irqsave(ap->lock, flags);
  1520. mv_stop_edma(ap);
  1521. mv_enable_port_irqs(ap, 0);
  1522. spin_unlock_irqrestore(ap->lock, flags);
  1523. mv_port_free_dma_mem(ap);
  1524. }
  1525. /**
  1526. * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
  1527. * @qc: queued command whose SG list to source from
  1528. *
  1529. * Populate the SG list and mark the last entry.
  1530. *
  1531. * LOCKING:
  1532. * Inherited from caller.
  1533. */
  1534. static void mv_fill_sg(struct ata_queued_cmd *qc)
  1535. {
  1536. struct mv_port_priv *pp = qc->ap->private_data;
  1537. struct scatterlist *sg;
  1538. struct mv_sg *mv_sg, *last_sg = NULL;
  1539. unsigned int si;
  1540. mv_sg = pp->sg_tbl[qc->tag];
  1541. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  1542. dma_addr_t addr = sg_dma_address(sg);
  1543. u32 sg_len = sg_dma_len(sg);
  1544. while (sg_len) {
  1545. u32 offset = addr & 0xffff;
  1546. u32 len = sg_len;
  1547. if (offset + len > 0x10000)
  1548. len = 0x10000 - offset;
  1549. mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
  1550. mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  1551. mv_sg->flags_size = cpu_to_le32(len & 0xffff);
  1552. mv_sg->reserved = 0;
  1553. sg_len -= len;
  1554. addr += len;
  1555. last_sg = mv_sg;
  1556. mv_sg++;
  1557. }
  1558. }
  1559. if (likely(last_sg))
  1560. last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
  1561. mb(); /* ensure data structure is visible to the chipset */
  1562. }
  1563. static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
  1564. {
  1565. u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  1566. (last ? CRQB_CMD_LAST : 0);
  1567. *cmdw = cpu_to_le16(tmp);
  1568. }
  1569. /**
  1570. * mv_sff_irq_clear - Clear hardware interrupt after DMA.
  1571. * @ap: Port associated with this ATA transaction.
  1572. *
  1573. * We need this only for ATAPI bmdma transactions,
  1574. * as otherwise we experience spurious interrupts
  1575. * after libata-sff handles the bmdma interrupts.
  1576. */
  1577. static void mv_sff_irq_clear(struct ata_port *ap)
  1578. {
  1579. mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), ERR_IRQ);
  1580. }
  1581. /**
  1582. * mv_check_atapi_dma - Filter ATAPI cmds which are unsuitable for DMA.
  1583. * @qc: queued command to check for chipset/DMA compatibility.
  1584. *
  1585. * The bmdma engines cannot handle speculative data sizes
  1586. * (bytecount under/over flow). So only allow DMA for
  1587. * data transfer commands with known data sizes.
  1588. *
  1589. * LOCKING:
  1590. * Inherited from caller.
  1591. */
  1592. static int mv_check_atapi_dma(struct ata_queued_cmd *qc)
  1593. {
  1594. struct scsi_cmnd *scmd = qc->scsicmd;
  1595. if (scmd) {
  1596. switch (scmd->cmnd[0]) {
  1597. case READ_6:
  1598. case READ_10:
  1599. case READ_12:
  1600. case WRITE_6:
  1601. case WRITE_10:
  1602. case WRITE_12:
  1603. case GPCMD_READ_CD:
  1604. case GPCMD_SEND_DVD_STRUCTURE:
  1605. case GPCMD_SEND_CUE_SHEET:
  1606. return 0; /* DMA is safe */
  1607. }
  1608. }
  1609. return -EOPNOTSUPP; /* use PIO instead */
  1610. }
  1611. /**
  1612. * mv_bmdma_setup - Set up BMDMA transaction
  1613. * @qc: queued command to prepare DMA for.
  1614. *
  1615. * LOCKING:
  1616. * Inherited from caller.
  1617. */
  1618. static void mv_bmdma_setup(struct ata_queued_cmd *qc)
  1619. {
  1620. struct ata_port *ap = qc->ap;
  1621. void __iomem *port_mmio = mv_ap_base(ap);
  1622. struct mv_port_priv *pp = ap->private_data;
  1623. mv_fill_sg(qc);
  1624. /* clear all DMA cmd bits */
  1625. writel(0, port_mmio + BMDMA_CMD);
  1626. /* load PRD table addr. */
  1627. writel((pp->sg_tbl_dma[qc->tag] >> 16) >> 16,
  1628. port_mmio + BMDMA_PRD_HIGH);
  1629. writelfl(pp->sg_tbl_dma[qc->tag],
  1630. port_mmio + BMDMA_PRD_LOW);
  1631. /* issue r/w command */
  1632. ap->ops->sff_exec_command(ap, &qc->tf);
  1633. }
  1634. /**
  1635. * mv_bmdma_start - Start a BMDMA transaction
  1636. * @qc: queued command to start DMA on.
  1637. *
  1638. * LOCKING:
  1639. * Inherited from caller.
  1640. */
  1641. static void mv_bmdma_start(struct ata_queued_cmd *qc)
  1642. {
  1643. struct ata_port *ap = qc->ap;
  1644. void __iomem *port_mmio = mv_ap_base(ap);
  1645. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  1646. u32 cmd = (rw ? 0 : ATA_DMA_WR) | ATA_DMA_START;
  1647. /* start host DMA transaction */
  1648. writelfl(cmd, port_mmio + BMDMA_CMD);
  1649. }
  1650. /**
  1651. * mv_bmdma_stop - Stop BMDMA transfer
  1652. * @qc: queued command to stop DMA on.
  1653. *
  1654. * Clears the ATA_DMA_START flag in the bmdma control register
  1655. *
  1656. * LOCKING:
  1657. * Inherited from caller.
  1658. */
  1659. static void mv_bmdma_stop_ap(struct ata_port *ap)
  1660. {
  1661. void __iomem *port_mmio = mv_ap_base(ap);
  1662. u32 cmd;
  1663. /* clear start/stop bit */
  1664. cmd = readl(port_mmio + BMDMA_CMD);
  1665. if (cmd & ATA_DMA_START) {
  1666. cmd &= ~ATA_DMA_START;
  1667. writelfl(cmd, port_mmio + BMDMA_CMD);
  1668. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  1669. ata_sff_dma_pause(ap);
  1670. }
  1671. }
  1672. static void mv_bmdma_stop(struct ata_queued_cmd *qc)
  1673. {
  1674. mv_bmdma_stop_ap(qc->ap);
  1675. }
  1676. /**
  1677. * mv_bmdma_status - Read BMDMA status
  1678. * @ap: port for which to retrieve DMA status.
  1679. *
  1680. * Read and return equivalent of the sff BMDMA status register.
  1681. *
  1682. * LOCKING:
  1683. * Inherited from caller.
  1684. */
  1685. static u8 mv_bmdma_status(struct ata_port *ap)
  1686. {
  1687. void __iomem *port_mmio = mv_ap_base(ap);
  1688. u32 reg, status;
  1689. /*
  1690. * Other bits are valid only if ATA_DMA_ACTIVE==0,
  1691. * and the ATA_DMA_INTR bit doesn't exist.
  1692. */
  1693. reg = readl(port_mmio + BMDMA_STATUS);
  1694. if (reg & ATA_DMA_ACTIVE)
  1695. status = ATA_DMA_ACTIVE;
  1696. else if (reg & ATA_DMA_ERR)
  1697. status = (reg & ATA_DMA_ERR) | ATA_DMA_INTR;
  1698. else {
  1699. /*
  1700. * Just because DMA_ACTIVE is 0 (DMA completed),
  1701. * this does _not_ mean the device is "done".
  1702. * So we should not yet be signalling ATA_DMA_INTR
  1703. * in some cases. Eg. DSM/TRIM, and perhaps others.
  1704. */
  1705. mv_bmdma_stop_ap(ap);
  1706. if (ioread8(ap->ioaddr.altstatus_addr) & ATA_BUSY)
  1707. status = 0;
  1708. else
  1709. status = ATA_DMA_INTR;
  1710. }
  1711. return status;
  1712. }
  1713. static void mv_rw_multi_errata_sata24(struct ata_queued_cmd *qc)
  1714. {
  1715. struct ata_taskfile *tf = &qc->tf;
  1716. /*
  1717. * Workaround for 88SX60x1 FEr SATA#24.
  1718. *
  1719. * Chip may corrupt WRITEs if multi_count >= 4kB.
  1720. * Note that READs are unaffected.
  1721. *
  1722. * It's not clear if this errata really means "4K bytes",
  1723. * or if it always happens for multi_count > 7
  1724. * regardless of device sector_size.
  1725. *
  1726. * So, for safety, any write with multi_count > 7
  1727. * gets converted here into a regular PIO write instead:
  1728. */
  1729. if ((tf->flags & ATA_TFLAG_WRITE) && is_multi_taskfile(tf)) {
  1730. if (qc->dev->multi_count > 7) {
  1731. switch (tf->command) {
  1732. case ATA_CMD_WRITE_MULTI:
  1733. tf->command = ATA_CMD_PIO_WRITE;
  1734. break;
  1735. case ATA_CMD_WRITE_MULTI_FUA_EXT:
  1736. tf->flags &= ~ATA_TFLAG_FUA; /* ugh */
  1737. /* fall through */
  1738. case ATA_CMD_WRITE_MULTI_EXT:
  1739. tf->command = ATA_CMD_PIO_WRITE_EXT;
  1740. break;
  1741. }
  1742. }
  1743. }
  1744. }
  1745. /**
  1746. * mv_qc_prep - Host specific command preparation.
  1747. * @qc: queued command to prepare
  1748. *
  1749. * This routine simply redirects to the general purpose routine
  1750. * if command is not DMA. Else, it handles prep of the CRQB
  1751. * (command request block), does some sanity checking, and calls
  1752. * the SG load routine.
  1753. *
  1754. * LOCKING:
  1755. * Inherited from caller.
  1756. */
  1757. static void mv_qc_prep(struct ata_queued_cmd *qc)
  1758. {
  1759. struct ata_port *ap = qc->ap;
  1760. struct mv_port_priv *pp = ap->private_data;
  1761. __le16 *cw;
  1762. struct ata_taskfile *tf = &qc->tf;
  1763. u16 flags = 0;
  1764. unsigned in_index;
  1765. switch (tf->protocol) {
  1766. case ATA_PROT_DMA:
  1767. if (tf->command == ATA_CMD_DSM)
  1768. return;
  1769. /* fall-thru */
  1770. case ATA_PROT_NCQ:
  1771. break; /* continue below */
  1772. case ATA_PROT_PIO:
  1773. mv_rw_multi_errata_sata24(qc);
  1774. return;
  1775. default:
  1776. return;
  1777. }
  1778. /* Fill in command request block
  1779. */
  1780. if (!(tf->flags & ATA_TFLAG_WRITE))
  1781. flags |= CRQB_FLAG_READ;
  1782. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  1783. flags |= qc->tag << CRQB_TAG_SHIFT;
  1784. flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
  1785. /* get current queue index from software */
  1786. in_index = pp->req_idx;
  1787. pp->crqb[in_index].sg_addr =
  1788. cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
  1789. pp->crqb[in_index].sg_addr_hi =
  1790. cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
  1791. pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
  1792. cw = &pp->crqb[in_index].ata_cmd[0];
  1793. /* Sadly, the CRQB cannot accommodate all registers--there are
  1794. * only 11 bytes...so we must pick and choose required
  1795. * registers based on the command. So, we drop feature and
  1796. * hob_feature for [RW] DMA commands, but they are needed for
  1797. * NCQ. NCQ will drop hob_nsect, which is not needed there
  1798. * (nsect is used only for the tag; feat/hob_feat hold true nsect).
  1799. */
  1800. switch (tf->command) {
  1801. case ATA_CMD_READ:
  1802. case ATA_CMD_READ_EXT:
  1803. case ATA_CMD_WRITE:
  1804. case ATA_CMD_WRITE_EXT:
  1805. case ATA_CMD_WRITE_FUA_EXT:
  1806. mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
  1807. break;
  1808. case ATA_CMD_FPDMA_READ:
  1809. case ATA_CMD_FPDMA_WRITE:
  1810. mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
  1811. mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
  1812. break;
  1813. default:
  1814. /* The only other commands EDMA supports in non-queued and
  1815. * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
  1816. * of which are defined/used by Linux. If we get here, this
  1817. * driver needs work.
  1818. *
  1819. * FIXME: modify libata to give qc_prep a return value and
  1820. * return error here.
  1821. */
  1822. BUG_ON(tf->command);
  1823. break;
  1824. }
  1825. mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
  1826. mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
  1827. mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
  1828. mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
  1829. mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
  1830. mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
  1831. mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
  1832. mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
  1833. mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
  1834. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1835. return;
  1836. mv_fill_sg(qc);
  1837. }
  1838. /**
  1839. * mv_qc_prep_iie - Host specific command preparation.
  1840. * @qc: queued command to prepare
  1841. *
  1842. * This routine simply redirects to the general purpose routine
  1843. * if command is not DMA. Else, it handles prep of the CRQB
  1844. * (command request block), does some sanity checking, and calls
  1845. * the SG load routine.
  1846. *
  1847. * LOCKING:
  1848. * Inherited from caller.
  1849. */
  1850. static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
  1851. {
  1852. struct ata_port *ap = qc->ap;
  1853. struct mv_port_priv *pp = ap->private_data;
  1854. struct mv_crqb_iie *crqb;
  1855. struct ata_taskfile *tf = &qc->tf;
  1856. unsigned in_index;
  1857. u32 flags = 0;
  1858. if ((tf->protocol != ATA_PROT_DMA) &&
  1859. (tf->protocol != ATA_PROT_NCQ))
  1860. return;
  1861. if (tf->command == ATA_CMD_DSM)
  1862. return; /* use bmdma for this */
  1863. /* Fill in Gen IIE command request block */
  1864. if (!(tf->flags & ATA_TFLAG_WRITE))
  1865. flags |= CRQB_FLAG_READ;
  1866. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  1867. flags |= qc->tag << CRQB_TAG_SHIFT;
  1868. flags |= qc->tag << CRQB_HOSTQ_SHIFT;
  1869. flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
  1870. /* get current queue index from software */
  1871. in_index = pp->req_idx;
  1872. crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
  1873. crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
  1874. crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
  1875. crqb->flags = cpu_to_le32(flags);
  1876. crqb->ata_cmd[0] = cpu_to_le32(
  1877. (tf->command << 16) |
  1878. (tf->feature << 24)
  1879. );
  1880. crqb->ata_cmd[1] = cpu_to_le32(
  1881. (tf->lbal << 0) |
  1882. (tf->lbam << 8) |
  1883. (tf->lbah << 16) |
  1884. (tf->device << 24)
  1885. );
  1886. crqb->ata_cmd[2] = cpu_to_le32(
  1887. (tf->hob_lbal << 0) |
  1888. (tf->hob_lbam << 8) |
  1889. (tf->hob_lbah << 16) |
  1890. (tf->hob_feature << 24)
  1891. );
  1892. crqb->ata_cmd[3] = cpu_to_le32(
  1893. (tf->nsect << 0) |
  1894. (tf->hob_nsect << 8)
  1895. );
  1896. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1897. return;
  1898. mv_fill_sg(qc);
  1899. }
  1900. /**
  1901. * mv_sff_check_status - fetch device status, if valid
  1902. * @ap: ATA port to fetch status from
  1903. *
  1904. * When using command issue via mv_qc_issue_fis(),
  1905. * the initial ATA_BUSY state does not show up in the
  1906. * ATA status (shadow) register. This can confuse libata!
  1907. *
  1908. * So we have a hook here to fake ATA_BUSY for that situation,
  1909. * until the first time a BUSY, DRQ, or ERR bit is seen.
  1910. *
  1911. * The rest of the time, it simply returns the ATA status register.
  1912. */
  1913. static u8 mv_sff_check_status(struct ata_port *ap)
  1914. {
  1915. u8 stat = ioread8(ap->ioaddr.status_addr);
  1916. struct mv_port_priv *pp = ap->private_data;
  1917. if (pp->pp_flags & MV_PP_FLAG_FAKE_ATA_BUSY) {
  1918. if (stat & (ATA_BUSY | ATA_DRQ | ATA_ERR))
  1919. pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY;
  1920. else
  1921. stat = ATA_BUSY;
  1922. }
  1923. return stat;
  1924. }
  1925. /**
  1926. * mv_send_fis - Send a FIS, using the "Vendor-Unique FIS" register
  1927. * @fis: fis to be sent
  1928. * @nwords: number of 32-bit words in the fis
  1929. */
  1930. static unsigned int mv_send_fis(struct ata_port *ap, u32 *fis, int nwords)
  1931. {
  1932. void __iomem *port_mmio = mv_ap_base(ap);
  1933. u32 ifctl, old_ifctl, ifstat;
  1934. int i, timeout = 200, final_word = nwords - 1;
  1935. /* Initiate FIS transmission mode */
  1936. old_ifctl = readl(port_mmio + SATA_IFCTL);
  1937. ifctl = 0x100 | (old_ifctl & 0xf);
  1938. writelfl(ifctl, port_mmio + SATA_IFCTL);
  1939. /* Send all words of the FIS except for the final word */
  1940. for (i = 0; i < final_word; ++i)
  1941. writel(fis[i], port_mmio + VENDOR_UNIQUE_FIS);
  1942. /* Flag end-of-transmission, and then send the final word */
  1943. writelfl(ifctl | 0x200, port_mmio + SATA_IFCTL);
  1944. writelfl(fis[final_word], port_mmio + VENDOR_UNIQUE_FIS);
  1945. /*
  1946. * Wait for FIS transmission to complete.
  1947. * This typically takes just a single iteration.
  1948. */
  1949. do {
  1950. ifstat = readl(port_mmio + SATA_IFSTAT);
  1951. } while (!(ifstat & 0x1000) && --timeout);
  1952. /* Restore original port configuration */
  1953. writelfl(old_ifctl, port_mmio + SATA_IFCTL);
  1954. /* See if it worked */
  1955. if ((ifstat & 0x3000) != 0x1000) {
  1956. ata_port_warn(ap, "%s transmission error, ifstat=%08x\n",
  1957. __func__, ifstat);
  1958. return AC_ERR_OTHER;
  1959. }
  1960. return 0;
  1961. }
  1962. /**
  1963. * mv_qc_issue_fis - Issue a command directly as a FIS
  1964. * @qc: queued command to start
  1965. *
  1966. * Note that the ATA shadow registers are not updated
  1967. * after command issue, so the device will appear "READY"
  1968. * if polled, even while it is BUSY processing the command.
  1969. *
  1970. * So we use a status hook to fake ATA_BUSY until the drive changes state.
  1971. *
  1972. * Note: we don't get updated shadow regs on *completion*
  1973. * of non-data commands. So avoid sending them via this function,
  1974. * as they will appear to have completed immediately.
  1975. *
  1976. * GEN_IIE has special registers that we could get the result tf from,
  1977. * but earlier chipsets do not. For now, we ignore those registers.
  1978. */
  1979. static unsigned int mv_qc_issue_fis(struct ata_queued_cmd *qc)
  1980. {
  1981. struct ata_port *ap = qc->ap;
  1982. struct mv_port_priv *pp = ap->private_data;
  1983. struct ata_link *link = qc->dev->link;
  1984. u32 fis[5];
  1985. int err = 0;
  1986. ata_tf_to_fis(&qc->tf, link->pmp, 1, (void *)fis);
  1987. err = mv_send_fis(ap, fis, ARRAY_SIZE(fis));
  1988. if (err)
  1989. return err;
  1990. switch (qc->tf.protocol) {
  1991. case ATAPI_PROT_PIO:
  1992. pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
  1993. /* fall through */
  1994. case ATAPI_PROT_NODATA:
  1995. ap->hsm_task_state = HSM_ST_FIRST;
  1996. break;
  1997. case ATA_PROT_PIO:
  1998. pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
  1999. if (qc->tf.flags & ATA_TFLAG_WRITE)
  2000. ap->hsm_task_state = HSM_ST_FIRST;
  2001. else
  2002. ap->hsm_task_state = HSM_ST;
  2003. break;
  2004. default:
  2005. ap->hsm_task_state = HSM_ST_LAST;
  2006. break;
  2007. }
  2008. if (qc->tf.flags & ATA_TFLAG_POLLING)
  2009. ata_sff_queue_pio_task(link, 0);
  2010. return 0;
  2011. }
  2012. /**
  2013. * mv_qc_issue - Initiate a command to the host
  2014. * @qc: queued command to start
  2015. *
  2016. * This routine simply redirects to the general purpose routine
  2017. * if command is not DMA. Else, it sanity checks our local
  2018. * caches of the request producer/consumer indices then enables
  2019. * DMA and bumps the request producer index.
  2020. *
  2021. * LOCKING:
  2022. * Inherited from caller.
  2023. */
  2024. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
  2025. {
  2026. static int limit_warnings = 10;
  2027. struct ata_port *ap = qc->ap;
  2028. void __iomem *port_mmio = mv_ap_base(ap);
  2029. struct mv_port_priv *pp = ap->private_data;
  2030. u32 in_index;
  2031. unsigned int port_irqs;
  2032. pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY; /* paranoia */
  2033. switch (qc->tf.protocol) {
  2034. case ATA_PROT_DMA:
  2035. if (qc->tf.command == ATA_CMD_DSM) {
  2036. if (!ap->ops->bmdma_setup) /* no bmdma on GEN_I */
  2037. return AC_ERR_OTHER;
  2038. break; /* use bmdma for this */
  2039. }
  2040. /* fall thru */
  2041. case ATA_PROT_NCQ:
  2042. mv_start_edma(ap, port_mmio, pp, qc->tf.protocol);
  2043. pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
  2044. in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
  2045. /* Write the request in pointer to kick the EDMA to life */
  2046. writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
  2047. port_mmio + EDMA_REQ_Q_IN_PTR);
  2048. return 0;
  2049. case ATA_PROT_PIO:
  2050. /*
  2051. * Errata SATA#16, SATA#24: warn if multiple DRQs expected.
  2052. *
  2053. * Someday, we might implement special polling workarounds
  2054. * for these, but it all seems rather unnecessary since we
  2055. * normally use only DMA for commands which transfer more
  2056. * than a single block of data.
  2057. *
  2058. * Much of the time, this could just work regardless.
  2059. * So for now, just log the incident, and allow the attempt.
  2060. */
  2061. if (limit_warnings > 0 && (qc->nbytes / qc->sect_size) > 1) {
  2062. --limit_warnings;
  2063. ata_link_warn(qc->dev->link, DRV_NAME
  2064. ": attempting PIO w/multiple DRQ: "
  2065. "this may fail due to h/w errata\n");
  2066. }
  2067. /* drop through */
  2068. case ATA_PROT_NODATA:
  2069. case ATAPI_PROT_PIO:
  2070. case ATAPI_PROT_NODATA:
  2071. if (ap->flags & ATA_FLAG_PIO_POLLING)
  2072. qc->tf.flags |= ATA_TFLAG_POLLING;
  2073. break;
  2074. }
  2075. if (qc->tf.flags & ATA_TFLAG_POLLING)
  2076. port_irqs = ERR_IRQ; /* mask device interrupt when polling */
  2077. else
  2078. port_irqs = ERR_IRQ | DONE_IRQ; /* unmask all interrupts */
  2079. /*
  2080. * We're about to send a non-EDMA capable command to the
  2081. * port. Turn off EDMA so there won't be problems accessing
  2082. * shadow block, etc registers.
  2083. */
  2084. mv_stop_edma(ap);
  2085. mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), port_irqs);
  2086. mv_pmp_select(ap, qc->dev->link->pmp);
  2087. if (qc->tf.command == ATA_CMD_READ_LOG_EXT) {
  2088. struct mv_host_priv *hpriv = ap->host->private_data;
  2089. /*
  2090. * Workaround for 88SX60x1 FEr SATA#25 (part 2).
  2091. *
  2092. * After any NCQ error, the READ_LOG_EXT command
  2093. * from libata-eh *must* use mv_qc_issue_fis().
  2094. * Otherwise it might fail, due to chip errata.
  2095. *
  2096. * Rather than special-case it, we'll just *always*
  2097. * use this method here for READ_LOG_EXT, making for
  2098. * easier testing.
  2099. */
  2100. if (IS_GEN_II(hpriv))
  2101. return mv_qc_issue_fis(qc);
  2102. }
  2103. return ata_bmdma_qc_issue(qc);
  2104. }
  2105. static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
  2106. {
  2107. struct mv_port_priv *pp = ap->private_data;
  2108. struct ata_queued_cmd *qc;
  2109. if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
  2110. return NULL;
  2111. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  2112. if (qc && !(qc->tf.flags & ATA_TFLAG_POLLING))
  2113. return qc;
  2114. return NULL;
  2115. }
  2116. static void mv_pmp_error_handler(struct ata_port *ap)
  2117. {
  2118. unsigned int pmp, pmp_map;
  2119. struct mv_port_priv *pp = ap->private_data;
  2120. if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) {
  2121. /*
  2122. * Perform NCQ error analysis on failed PMPs
  2123. * before we freeze the port entirely.
  2124. *
  2125. * The failed PMPs are marked earlier by mv_pmp_eh_prep().
  2126. */
  2127. pmp_map = pp->delayed_eh_pmp_map;
  2128. pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH;
  2129. for (pmp = 0; pmp_map != 0; pmp++) {
  2130. unsigned int this_pmp = (1 << pmp);
  2131. if (pmp_map & this_pmp) {
  2132. struct ata_link *link = &ap->pmp_link[pmp];
  2133. pmp_map &= ~this_pmp;
  2134. ata_eh_analyze_ncq_error(link);
  2135. }
  2136. }
  2137. ata_port_freeze(ap);
  2138. }
  2139. sata_pmp_error_handler(ap);
  2140. }
  2141. static unsigned int mv_get_err_pmp_map(struct ata_port *ap)
  2142. {
  2143. void __iomem *port_mmio = mv_ap_base(ap);
  2144. return readl(port_mmio + SATA_TESTCTL) >> 16;
  2145. }
  2146. static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map)
  2147. {
  2148. struct ata_eh_info *ehi;
  2149. unsigned int pmp;
  2150. /*
  2151. * Initialize EH info for PMPs which saw device errors
  2152. */
  2153. ehi = &ap->link.eh_info;
  2154. for (pmp = 0; pmp_map != 0; pmp++) {
  2155. unsigned int this_pmp = (1 << pmp);
  2156. if (pmp_map & this_pmp) {
  2157. struct ata_link *link = &ap->pmp_link[pmp];
  2158. pmp_map &= ~this_pmp;
  2159. ehi = &link->eh_info;
  2160. ata_ehi_clear_desc(ehi);
  2161. ata_ehi_push_desc(ehi, "dev err");
  2162. ehi->err_mask |= AC_ERR_DEV;
  2163. ehi->action |= ATA_EH_RESET;
  2164. ata_link_abort(link);
  2165. }
  2166. }
  2167. }
  2168. static int mv_req_q_empty(struct ata_port *ap)
  2169. {
  2170. void __iomem *port_mmio = mv_ap_base(ap);
  2171. u32 in_ptr, out_ptr;
  2172. in_ptr = (readl(port_mmio + EDMA_REQ_Q_IN_PTR)
  2173. >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  2174. out_ptr = (readl(port_mmio + EDMA_REQ_Q_OUT_PTR)
  2175. >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  2176. return (in_ptr == out_ptr); /* 1 == queue_is_empty */
  2177. }
  2178. static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap)
  2179. {
  2180. struct mv_port_priv *pp = ap->private_data;
  2181. int failed_links;
  2182. unsigned int old_map, new_map;
  2183. /*
  2184. * Device error during FBS+NCQ operation:
  2185. *
  2186. * Set a port flag to prevent further I/O being enqueued.
  2187. * Leave the EDMA running to drain outstanding commands from this port.
  2188. * Perform the post-mortem/EH only when all responses are complete.
  2189. * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2).
  2190. */
  2191. if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) {
  2192. pp->pp_flags |= MV_PP_FLAG_DELAYED_EH;
  2193. pp->delayed_eh_pmp_map = 0;
  2194. }
  2195. old_map = pp->delayed_eh_pmp_map;
  2196. new_map = old_map | mv_get_err_pmp_map(ap);
  2197. if (old_map != new_map) {
  2198. pp->delayed_eh_pmp_map = new_map;
  2199. mv_pmp_eh_prep(ap, new_map & ~old_map);
  2200. }
  2201. failed_links = hweight16(new_map);
  2202. ata_port_info(ap,
  2203. "%s: pmp_map=%04x qc_map=%04x failed_links=%d nr_active_links=%d\n",
  2204. __func__, pp->delayed_eh_pmp_map,
  2205. ap->qc_active, failed_links,
  2206. ap->nr_active_links);
  2207. if (ap->nr_active_links <= failed_links && mv_req_q_empty(ap)) {
  2208. mv_process_crpb_entries(ap, pp);
  2209. mv_stop_edma(ap);
  2210. mv_eh_freeze(ap);
  2211. ata_port_info(ap, "%s: done\n", __func__);
  2212. return 1; /* handled */
  2213. }
  2214. ata_port_info(ap, "%s: waiting\n", __func__);
  2215. return 1; /* handled */
  2216. }
  2217. static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap)
  2218. {
  2219. /*
  2220. * Possible future enhancement:
  2221. *
  2222. * FBS+non-NCQ operation is not yet implemented.
  2223. * See related notes in mv_edma_cfg().
  2224. *
  2225. * Device error during FBS+non-NCQ operation:
  2226. *
  2227. * We need to snapshot the shadow registers for each failed command.
  2228. * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3).
  2229. */
  2230. return 0; /* not handled */
  2231. }
  2232. static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause)
  2233. {
  2234. struct mv_port_priv *pp = ap->private_data;
  2235. if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
  2236. return 0; /* EDMA was not active: not handled */
  2237. if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN))
  2238. return 0; /* FBS was not active: not handled */
  2239. if (!(edma_err_cause & EDMA_ERR_DEV))
  2240. return 0; /* non DEV error: not handled */
  2241. edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT;
  2242. if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS))
  2243. return 0; /* other problems: not handled */
  2244. if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
  2245. /*
  2246. * EDMA should NOT have self-disabled for this case.
  2247. * If it did, then something is wrong elsewhere,
  2248. * and we cannot handle it here.
  2249. */
  2250. if (edma_err_cause & EDMA_ERR_SELF_DIS) {
  2251. ata_port_warn(ap, "%s: err_cause=0x%x pp_flags=0x%x\n",
  2252. __func__, edma_err_cause, pp->pp_flags);
  2253. return 0; /* not handled */
  2254. }
  2255. return mv_handle_fbs_ncq_dev_err(ap);
  2256. } else {
  2257. /*
  2258. * EDMA should have self-disabled for this case.
  2259. * If it did not, then something is wrong elsewhere,
  2260. * and we cannot handle it here.
  2261. */
  2262. if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) {
  2263. ata_port_warn(ap, "%s: err_cause=0x%x pp_flags=0x%x\n",
  2264. __func__, edma_err_cause, pp->pp_flags);
  2265. return 0; /* not handled */
  2266. }
  2267. return mv_handle_fbs_non_ncq_dev_err(ap);
  2268. }
  2269. return 0; /* not handled */
  2270. }
  2271. static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled)
  2272. {
  2273. struct ata_eh_info *ehi = &ap->link.eh_info;
  2274. char *when = "idle";
  2275. ata_ehi_clear_desc(ehi);
  2276. if (edma_was_enabled) {
  2277. when = "EDMA enabled";
  2278. } else {
  2279. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
  2280. if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
  2281. when = "polling";
  2282. }
  2283. ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when);
  2284. ehi->err_mask |= AC_ERR_OTHER;
  2285. ehi->action |= ATA_EH_RESET;
  2286. ata_port_freeze(ap);
  2287. }
  2288. /**
  2289. * mv_err_intr - Handle error interrupts on the port
  2290. * @ap: ATA channel to manipulate
  2291. *
  2292. * Most cases require a full reset of the chip's state machine,
  2293. * which also performs a COMRESET.
  2294. * Also, if the port disabled DMA, update our cached copy to match.
  2295. *
  2296. * LOCKING:
  2297. * Inherited from caller.
  2298. */
  2299. static void mv_err_intr(struct ata_port *ap)
  2300. {
  2301. void __iomem *port_mmio = mv_ap_base(ap);
  2302. u32 edma_err_cause, eh_freeze_mask, serr = 0;
  2303. u32 fis_cause = 0;
  2304. struct mv_port_priv *pp = ap->private_data;
  2305. struct mv_host_priv *hpriv = ap->host->private_data;
  2306. unsigned int action = 0, err_mask = 0;
  2307. struct ata_eh_info *ehi = &ap->link.eh_info;
  2308. struct ata_queued_cmd *qc;
  2309. int abort = 0;
  2310. /*
  2311. * Read and clear the SError and err_cause bits.
  2312. * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear
  2313. * the FIS_IRQ_CAUSE register before clearing edma_err_cause.
  2314. */
  2315. sata_scr_read(&ap->link, SCR_ERROR, &serr);
  2316. sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
  2317. edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE);
  2318. if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
  2319. fis_cause = readl(port_mmio + FIS_IRQ_CAUSE);
  2320. writelfl(~fis_cause, port_mmio + FIS_IRQ_CAUSE);
  2321. }
  2322. writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE);
  2323. if (edma_err_cause & EDMA_ERR_DEV) {
  2324. /*
  2325. * Device errors during FIS-based switching operation
  2326. * require special handling.
  2327. */
  2328. if (mv_handle_dev_err(ap, edma_err_cause))
  2329. return;
  2330. }
  2331. qc = mv_get_active_qc(ap);
  2332. ata_ehi_clear_desc(ehi);
  2333. ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x",
  2334. edma_err_cause, pp->pp_flags);
  2335. if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
  2336. ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause);
  2337. if (fis_cause & FIS_IRQ_CAUSE_AN) {
  2338. u32 ec = edma_err_cause &
  2339. ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT);
  2340. sata_async_notification(ap);
  2341. if (!ec)
  2342. return; /* Just an AN; no need for the nukes */
  2343. ata_ehi_push_desc(ehi, "SDB notify");
  2344. }
  2345. }
  2346. /*
  2347. * All generations share these EDMA error cause bits:
  2348. */
  2349. if (edma_err_cause & EDMA_ERR_DEV) {
  2350. err_mask |= AC_ERR_DEV;
  2351. action |= ATA_EH_RESET;
  2352. ata_ehi_push_desc(ehi, "dev error");
  2353. }
  2354. if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  2355. EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
  2356. EDMA_ERR_INTRL_PAR)) {
  2357. err_mask |= AC_ERR_ATA_BUS;
  2358. action |= ATA_EH_RESET;
  2359. ata_ehi_push_desc(ehi, "parity error");
  2360. }
  2361. if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
  2362. ata_ehi_hotplugged(ehi);
  2363. ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
  2364. "dev disconnect" : "dev connect");
  2365. action |= ATA_EH_RESET;
  2366. }
  2367. /*
  2368. * Gen-I has a different SELF_DIS bit,
  2369. * different FREEZE bits, and no SERR bit:
  2370. */
  2371. if (IS_GEN_I(hpriv)) {
  2372. eh_freeze_mask = EDMA_EH_FREEZE_5;
  2373. if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
  2374. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  2375. ata_ehi_push_desc(ehi, "EDMA self-disable");
  2376. }
  2377. } else {
  2378. eh_freeze_mask = EDMA_EH_FREEZE;
  2379. if (edma_err_cause & EDMA_ERR_SELF_DIS) {
  2380. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  2381. ata_ehi_push_desc(ehi, "EDMA self-disable");
  2382. }
  2383. if (edma_err_cause & EDMA_ERR_SERR) {
  2384. ata_ehi_push_desc(ehi, "SError=%08x", serr);
  2385. err_mask |= AC_ERR_ATA_BUS;
  2386. action |= ATA_EH_RESET;
  2387. }
  2388. }
  2389. if (!err_mask) {
  2390. err_mask = AC_ERR_OTHER;
  2391. action |= ATA_EH_RESET;
  2392. }
  2393. ehi->serror |= serr;
  2394. ehi->action |= action;
  2395. if (qc)
  2396. qc->err_mask |= err_mask;
  2397. else
  2398. ehi->err_mask |= err_mask;
  2399. if (err_mask == AC_ERR_DEV) {
  2400. /*
  2401. * Cannot do ata_port_freeze() here,
  2402. * because it would kill PIO access,
  2403. * which is needed for further diagnosis.
  2404. */
  2405. mv_eh_freeze(ap);
  2406. abort = 1;
  2407. } else if (edma_err_cause & eh_freeze_mask) {
  2408. /*
  2409. * Note to self: ata_port_freeze() calls ata_port_abort()
  2410. */
  2411. ata_port_freeze(ap);
  2412. } else {
  2413. abort = 1;
  2414. }
  2415. if (abort) {
  2416. if (qc)
  2417. ata_link_abort(qc->dev->link);
  2418. else
  2419. ata_port_abort(ap);
  2420. }
  2421. }
  2422. static bool mv_process_crpb_response(struct ata_port *ap,
  2423. struct mv_crpb *response, unsigned int tag, int ncq_enabled)
  2424. {
  2425. u8 ata_status;
  2426. u16 edma_status = le16_to_cpu(response->flags);
  2427. /*
  2428. * edma_status from a response queue entry:
  2429. * LSB is from EDMA_ERR_IRQ_CAUSE (non-NCQ only).
  2430. * MSB is saved ATA status from command completion.
  2431. */
  2432. if (!ncq_enabled) {
  2433. u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
  2434. if (err_cause) {
  2435. /*
  2436. * Error will be seen/handled by
  2437. * mv_err_intr(). So do nothing at all here.
  2438. */
  2439. return false;
  2440. }
  2441. }
  2442. ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
  2443. if (!ac_err_mask(ata_status))
  2444. return true;
  2445. /* else: leave it for mv_err_intr() */
  2446. return false;
  2447. }
  2448. static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
  2449. {
  2450. void __iomem *port_mmio = mv_ap_base(ap);
  2451. struct mv_host_priv *hpriv = ap->host->private_data;
  2452. u32 in_index;
  2453. bool work_done = false;
  2454. u32 done_mask = 0;
  2455. int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
  2456. /* Get the hardware queue position index */
  2457. in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR)
  2458. >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  2459. /* Process new responses from since the last time we looked */
  2460. while (in_index != pp->resp_idx) {
  2461. unsigned int tag;
  2462. struct mv_crpb *response = &pp->crpb[pp->resp_idx];
  2463. pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
  2464. if (IS_GEN_I(hpriv)) {
  2465. /* 50xx: no NCQ, only one command active at a time */
  2466. tag = ap->link.active_tag;
  2467. } else {
  2468. /* Gen II/IIE: get command tag from CRPB entry */
  2469. tag = le16_to_cpu(response->id) & 0x1f;
  2470. }
  2471. if (mv_process_crpb_response(ap, response, tag, ncq_enabled))
  2472. done_mask |= 1 << tag;
  2473. work_done = true;
  2474. }
  2475. if (work_done) {
  2476. ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
  2477. /* Update the software queue position index in hardware */
  2478. writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
  2479. (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
  2480. port_mmio + EDMA_RSP_Q_OUT_PTR);
  2481. }
  2482. }
  2483. static void mv_port_intr(struct ata_port *ap, u32 port_cause)
  2484. {
  2485. struct mv_port_priv *pp;
  2486. int edma_was_enabled;
  2487. /*
  2488. * Grab a snapshot of the EDMA_EN flag setting,
  2489. * so that we have a consistent view for this port,
  2490. * even if something we call of our routines changes it.
  2491. */
  2492. pp = ap->private_data;
  2493. edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
  2494. /*
  2495. * Process completed CRPB response(s) before other events.
  2496. */
  2497. if (edma_was_enabled && (port_cause & DONE_IRQ)) {
  2498. mv_process_crpb_entries(ap, pp);
  2499. if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
  2500. mv_handle_fbs_ncq_dev_err(ap);
  2501. }
  2502. /*
  2503. * Handle chip-reported errors, or continue on to handle PIO.
  2504. */
  2505. if (unlikely(port_cause & ERR_IRQ)) {
  2506. mv_err_intr(ap);
  2507. } else if (!edma_was_enabled) {
  2508. struct ata_queued_cmd *qc = mv_get_active_qc(ap);
  2509. if (qc)
  2510. ata_bmdma_port_intr(ap, qc);
  2511. else
  2512. mv_unexpected_intr(ap, edma_was_enabled);
  2513. }
  2514. }
  2515. /**
  2516. * mv_host_intr - Handle all interrupts on the given host controller
  2517. * @host: host specific structure
  2518. * @main_irq_cause: Main interrupt cause register for the chip.
  2519. *
  2520. * LOCKING:
  2521. * Inherited from caller.
  2522. */
  2523. static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
  2524. {
  2525. struct mv_host_priv *hpriv = host->private_data;
  2526. void __iomem *mmio = hpriv->base, *hc_mmio;
  2527. unsigned int handled = 0, port;
  2528. /* If asserted, clear the "all ports" IRQ coalescing bit */
  2529. if (main_irq_cause & ALL_PORTS_COAL_DONE)
  2530. writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE);
  2531. for (port = 0; port < hpriv->n_ports; port++) {
  2532. struct ata_port *ap = host->ports[port];
  2533. unsigned int p, shift, hardport, port_cause;
  2534. MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
  2535. /*
  2536. * Each hc within the host has its own hc_irq_cause register,
  2537. * where the interrupting ports bits get ack'd.
  2538. */
  2539. if (hardport == 0) { /* first port on this hc ? */
  2540. u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND;
  2541. u32 port_mask, ack_irqs;
  2542. /*
  2543. * Skip this entire hc if nothing pending for any ports
  2544. */
  2545. if (!hc_cause) {
  2546. port += MV_PORTS_PER_HC - 1;
  2547. continue;
  2548. }
  2549. /*
  2550. * We don't need/want to read the hc_irq_cause register,
  2551. * because doing so hurts performance, and
  2552. * main_irq_cause already gives us everything we need.
  2553. *
  2554. * But we do have to *write* to the hc_irq_cause to ack
  2555. * the ports that we are handling this time through.
  2556. *
  2557. * This requires that we create a bitmap for those
  2558. * ports which interrupted us, and use that bitmap
  2559. * to ack (only) those ports via hc_irq_cause.
  2560. */
  2561. ack_irqs = 0;
  2562. if (hc_cause & PORTS_0_3_COAL_DONE)
  2563. ack_irqs = HC_COAL_IRQ;
  2564. for (p = 0; p < MV_PORTS_PER_HC; ++p) {
  2565. if ((port + p) >= hpriv->n_ports)
  2566. break;
  2567. port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2);
  2568. if (hc_cause & port_mask)
  2569. ack_irqs |= (DMA_IRQ | DEV_IRQ) << p;
  2570. }
  2571. hc_mmio = mv_hc_base_from_port(mmio, port);
  2572. writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE);
  2573. handled = 1;
  2574. }
  2575. /*
  2576. * Handle interrupts signalled for this port:
  2577. */
  2578. port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
  2579. if (port_cause)
  2580. mv_port_intr(ap, port_cause);
  2581. }
  2582. return handled;
  2583. }
  2584. static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
  2585. {
  2586. struct mv_host_priv *hpriv = host->private_data;
  2587. struct ata_port *ap;
  2588. struct ata_queued_cmd *qc;
  2589. struct ata_eh_info *ehi;
  2590. unsigned int i, err_mask, printed = 0;
  2591. u32 err_cause;
  2592. err_cause = readl(mmio + hpriv->irq_cause_offset);
  2593. dev_err(host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n", err_cause);
  2594. DPRINTK("All regs @ PCI error\n");
  2595. mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
  2596. writelfl(0, mmio + hpriv->irq_cause_offset);
  2597. for (i = 0; i < host->n_ports; i++) {
  2598. ap = host->ports[i];
  2599. if (!ata_link_offline(&ap->link)) {
  2600. ehi = &ap->link.eh_info;
  2601. ata_ehi_clear_desc(ehi);
  2602. if (!printed++)
  2603. ata_ehi_push_desc(ehi,
  2604. "PCI err cause 0x%08x", err_cause);
  2605. err_mask = AC_ERR_HOST_BUS;
  2606. ehi->action = ATA_EH_RESET;
  2607. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  2608. if (qc)
  2609. qc->err_mask |= err_mask;
  2610. else
  2611. ehi->err_mask |= err_mask;
  2612. ata_port_freeze(ap);
  2613. }
  2614. }
  2615. return 1; /* handled */
  2616. }
  2617. /**
  2618. * mv_interrupt - Main interrupt event handler
  2619. * @irq: unused
  2620. * @dev_instance: private data; in this case the host structure
  2621. *
  2622. * Read the read only register to determine if any host
  2623. * controllers have pending interrupts. If so, call lower level
  2624. * routine to handle. Also check for PCI errors which are only
  2625. * reported here.
  2626. *
  2627. * LOCKING:
  2628. * This routine holds the host lock while processing pending
  2629. * interrupts.
  2630. */
  2631. static irqreturn_t mv_interrupt(int irq, void *dev_instance)
  2632. {
  2633. struct ata_host *host = dev_instance;
  2634. struct mv_host_priv *hpriv = host->private_data;
  2635. unsigned int handled = 0;
  2636. int using_msi = hpriv->hp_flags & MV_HP_FLAG_MSI;
  2637. u32 main_irq_cause, pending_irqs;
  2638. spin_lock(&host->lock);
  2639. /* for MSI: block new interrupts while in here */
  2640. if (using_msi)
  2641. mv_write_main_irq_mask(0, hpriv);
  2642. main_irq_cause = readl(hpriv->main_irq_cause_addr);
  2643. pending_irqs = main_irq_cause & hpriv->main_irq_mask;
  2644. /*
  2645. * Deal with cases where we either have nothing pending, or have read
  2646. * a bogus register value which can indicate HW removal or PCI fault.
  2647. */
  2648. if (pending_irqs && main_irq_cause != 0xffffffffU) {
  2649. if (unlikely((pending_irqs & PCI_ERR) && !IS_SOC(hpriv)))
  2650. handled = mv_pci_error(host, hpriv->base);
  2651. else
  2652. handled = mv_host_intr(host, pending_irqs);
  2653. }
  2654. /* for MSI: unmask; interrupt cause bits will retrigger now */
  2655. if (using_msi)
  2656. mv_write_main_irq_mask(hpriv->main_irq_mask, hpriv);
  2657. spin_unlock(&host->lock);
  2658. return IRQ_RETVAL(handled);
  2659. }
  2660. static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
  2661. {
  2662. unsigned int ofs;
  2663. switch (sc_reg_in) {
  2664. case SCR_STATUS:
  2665. case SCR_ERROR:
  2666. case SCR_CONTROL:
  2667. ofs = sc_reg_in * sizeof(u32);
  2668. break;
  2669. default:
  2670. ofs = 0xffffffffU;
  2671. break;
  2672. }
  2673. return ofs;
  2674. }
  2675. static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
  2676. {
  2677. struct mv_host_priv *hpriv = link->ap->host->private_data;
  2678. void __iomem *mmio = hpriv->base;
  2679. void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
  2680. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  2681. if (ofs != 0xffffffffU) {
  2682. *val = readl(addr + ofs);
  2683. return 0;
  2684. } else
  2685. return -EINVAL;
  2686. }
  2687. static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
  2688. {
  2689. struct mv_host_priv *hpriv = link->ap->host->private_data;
  2690. void __iomem *mmio = hpriv->base;
  2691. void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
  2692. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  2693. if (ofs != 0xffffffffU) {
  2694. writelfl(val, addr + ofs);
  2695. return 0;
  2696. } else
  2697. return -EINVAL;
  2698. }
  2699. static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
  2700. {
  2701. struct pci_dev *pdev = to_pci_dev(host->dev);
  2702. int early_5080;
  2703. early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
  2704. if (!early_5080) {
  2705. u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2706. tmp |= (1 << 0);
  2707. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2708. }
  2709. mv_reset_pci_bus(host, mmio);
  2710. }
  2711. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  2712. {
  2713. writel(0x0fcfffff, mmio + FLASH_CTL);
  2714. }
  2715. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  2716. void __iomem *mmio)
  2717. {
  2718. void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
  2719. u32 tmp;
  2720. tmp = readl(phy_mmio + MV5_PHY_MODE);
  2721. hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
  2722. hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
  2723. }
  2724. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  2725. {
  2726. u32 tmp;
  2727. writel(0, mmio + GPIO_PORT_CTL);
  2728. /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
  2729. tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2730. tmp |= ~(1 << 0);
  2731. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2732. }
  2733. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  2734. unsigned int port)
  2735. {
  2736. void __iomem *phy_mmio = mv5_phy_base(mmio, port);
  2737. const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
  2738. u32 tmp;
  2739. int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
  2740. if (fix_apm_sq) {
  2741. tmp = readl(phy_mmio + MV5_LTMODE);
  2742. tmp |= (1 << 19);
  2743. writel(tmp, phy_mmio + MV5_LTMODE);
  2744. tmp = readl(phy_mmio + MV5_PHY_CTL);
  2745. tmp &= ~0x3;
  2746. tmp |= 0x1;
  2747. writel(tmp, phy_mmio + MV5_PHY_CTL);
  2748. }
  2749. tmp = readl(phy_mmio + MV5_PHY_MODE);
  2750. tmp &= ~mask;
  2751. tmp |= hpriv->signal[port].pre;
  2752. tmp |= hpriv->signal[port].amps;
  2753. writel(tmp, phy_mmio + MV5_PHY_MODE);
  2754. }
  2755. #undef ZERO
  2756. #define ZERO(reg) writel(0, port_mmio + (reg))
  2757. static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
  2758. unsigned int port)
  2759. {
  2760. void __iomem *port_mmio = mv_port_base(mmio, port);
  2761. mv_reset_channel(hpriv, mmio, port);
  2762. ZERO(0x028); /* command */
  2763. writel(0x11f, port_mmio + EDMA_CFG);
  2764. ZERO(0x004); /* timer */
  2765. ZERO(0x008); /* irq err cause */
  2766. ZERO(0x00c); /* irq err mask */
  2767. ZERO(0x010); /* rq bah */
  2768. ZERO(0x014); /* rq inp */
  2769. ZERO(0x018); /* rq outp */
  2770. ZERO(0x01c); /* respq bah */
  2771. ZERO(0x024); /* respq outp */
  2772. ZERO(0x020); /* respq inp */
  2773. ZERO(0x02c); /* test control */
  2774. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
  2775. }
  2776. #undef ZERO
  2777. #define ZERO(reg) writel(0, hc_mmio + (reg))
  2778. static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  2779. unsigned int hc)
  2780. {
  2781. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  2782. u32 tmp;
  2783. ZERO(0x00c);
  2784. ZERO(0x010);
  2785. ZERO(0x014);
  2786. ZERO(0x018);
  2787. tmp = readl(hc_mmio + 0x20);
  2788. tmp &= 0x1c1c1c1c;
  2789. tmp |= 0x03030303;
  2790. writel(tmp, hc_mmio + 0x20);
  2791. }
  2792. #undef ZERO
  2793. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  2794. unsigned int n_hc)
  2795. {
  2796. unsigned int hc, port;
  2797. for (hc = 0; hc < n_hc; hc++) {
  2798. for (port = 0; port < MV_PORTS_PER_HC; port++)
  2799. mv5_reset_hc_port(hpriv, mmio,
  2800. (hc * MV_PORTS_PER_HC) + port);
  2801. mv5_reset_one_hc(hpriv, mmio, hc);
  2802. }
  2803. return 0;
  2804. }
  2805. #undef ZERO
  2806. #define ZERO(reg) writel(0, mmio + (reg))
  2807. static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
  2808. {
  2809. struct mv_host_priv *hpriv = host->private_data;
  2810. u32 tmp;
  2811. tmp = readl(mmio + MV_PCI_MODE);
  2812. tmp &= 0xff00ffff;
  2813. writel(tmp, mmio + MV_PCI_MODE);
  2814. ZERO(MV_PCI_DISC_TIMER);
  2815. ZERO(MV_PCI_MSI_TRIGGER);
  2816. writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
  2817. ZERO(MV_PCI_SERR_MASK);
  2818. ZERO(hpriv->irq_cause_offset);
  2819. ZERO(hpriv->irq_mask_offset);
  2820. ZERO(MV_PCI_ERR_LOW_ADDRESS);
  2821. ZERO(MV_PCI_ERR_HIGH_ADDRESS);
  2822. ZERO(MV_PCI_ERR_ATTRIBUTE);
  2823. ZERO(MV_PCI_ERR_COMMAND);
  2824. }
  2825. #undef ZERO
  2826. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  2827. {
  2828. u32 tmp;
  2829. mv5_reset_flash(hpriv, mmio);
  2830. tmp = readl(mmio + GPIO_PORT_CTL);
  2831. tmp &= 0x3;
  2832. tmp |= (1 << 5) | (1 << 6);
  2833. writel(tmp, mmio + GPIO_PORT_CTL);
  2834. }
  2835. /**
  2836. * mv6_reset_hc - Perform the 6xxx global soft reset
  2837. * @mmio: base address of the HBA
  2838. *
  2839. * This routine only applies to 6xxx parts.
  2840. *
  2841. * LOCKING:
  2842. * Inherited from caller.
  2843. */
  2844. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  2845. unsigned int n_hc)
  2846. {
  2847. void __iomem *reg = mmio + PCI_MAIN_CMD_STS;
  2848. int i, rc = 0;
  2849. u32 t;
  2850. /* Following procedure defined in PCI "main command and status
  2851. * register" table.
  2852. */
  2853. t = readl(reg);
  2854. writel(t | STOP_PCI_MASTER, reg);
  2855. for (i = 0; i < 1000; i++) {
  2856. udelay(1);
  2857. t = readl(reg);
  2858. if (PCI_MASTER_EMPTY & t)
  2859. break;
  2860. }
  2861. if (!(PCI_MASTER_EMPTY & t)) {
  2862. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  2863. rc = 1;
  2864. goto done;
  2865. }
  2866. /* set reset */
  2867. i = 5;
  2868. do {
  2869. writel(t | GLOB_SFT_RST, reg);
  2870. t = readl(reg);
  2871. udelay(1);
  2872. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  2873. if (!(GLOB_SFT_RST & t)) {
  2874. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  2875. rc = 1;
  2876. goto done;
  2877. }
  2878. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  2879. i = 5;
  2880. do {
  2881. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  2882. t = readl(reg);
  2883. udelay(1);
  2884. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  2885. if (GLOB_SFT_RST & t) {
  2886. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  2887. rc = 1;
  2888. }
  2889. done:
  2890. return rc;
  2891. }
  2892. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  2893. void __iomem *mmio)
  2894. {
  2895. void __iomem *port_mmio;
  2896. u32 tmp;
  2897. tmp = readl(mmio + RESET_CFG);
  2898. if ((tmp & (1 << 0)) == 0) {
  2899. hpriv->signal[idx].amps = 0x7 << 8;
  2900. hpriv->signal[idx].pre = 0x1 << 5;
  2901. return;
  2902. }
  2903. port_mmio = mv_port_base(mmio, idx);
  2904. tmp = readl(port_mmio + PHY_MODE2);
  2905. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  2906. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  2907. }
  2908. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  2909. {
  2910. writel(0x00000060, mmio + GPIO_PORT_CTL);
  2911. }
  2912. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  2913. unsigned int port)
  2914. {
  2915. void __iomem *port_mmio = mv_port_base(mmio, port);
  2916. u32 hp_flags = hpriv->hp_flags;
  2917. int fix_phy_mode2 =
  2918. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  2919. int fix_phy_mode4 =
  2920. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  2921. u32 m2, m3;
  2922. if (fix_phy_mode2) {
  2923. m2 = readl(port_mmio + PHY_MODE2);
  2924. m2 &= ~(1 << 16);
  2925. m2 |= (1 << 31);
  2926. writel(m2, port_mmio + PHY_MODE2);
  2927. udelay(200);
  2928. m2 = readl(port_mmio + PHY_MODE2);
  2929. m2 &= ~((1 << 16) | (1 << 31));
  2930. writel(m2, port_mmio + PHY_MODE2);
  2931. udelay(200);
  2932. }
  2933. /*
  2934. * Gen-II/IIe PHY_MODE3 errata RM#2:
  2935. * Achieves better receiver noise performance than the h/w default:
  2936. */
  2937. m3 = readl(port_mmio + PHY_MODE3);
  2938. m3 = (m3 & 0x1f) | (0x5555601 << 5);
  2939. /* Guideline 88F5182 (GL# SATA-S11) */
  2940. if (IS_SOC(hpriv))
  2941. m3 &= ~0x1c;
  2942. if (fix_phy_mode4) {
  2943. u32 m4 = readl(port_mmio + PHY_MODE4);
  2944. /*
  2945. * Enforce reserved-bit restrictions on GenIIe devices only.
  2946. * For earlier chipsets, force only the internal config field
  2947. * (workaround for errata FEr SATA#10 part 1).
  2948. */
  2949. if (IS_GEN_IIE(hpriv))
  2950. m4 = (m4 & ~PHY_MODE4_RSVD_ZEROS) | PHY_MODE4_RSVD_ONES;
  2951. else
  2952. m4 = (m4 & ~PHY_MODE4_CFG_MASK) | PHY_MODE4_CFG_VALUE;
  2953. writel(m4, port_mmio + PHY_MODE4);
  2954. }
  2955. /*
  2956. * Workaround for 60x1-B2 errata SATA#13:
  2957. * Any write to PHY_MODE4 (above) may corrupt PHY_MODE3,
  2958. * so we must always rewrite PHY_MODE3 after PHY_MODE4.
  2959. * Or ensure we use writelfl() when writing PHY_MODE4.
  2960. */
  2961. writel(m3, port_mmio + PHY_MODE3);
  2962. /* Revert values of pre-emphasis and signal amps to the saved ones */
  2963. m2 = readl(port_mmio + PHY_MODE2);
  2964. m2 &= ~MV_M2_PREAMP_MASK;
  2965. m2 |= hpriv->signal[port].amps;
  2966. m2 |= hpriv->signal[port].pre;
  2967. m2 &= ~(1 << 16);
  2968. /* according to mvSata 3.6.1, some IIE values are fixed */
  2969. if (IS_GEN_IIE(hpriv)) {
  2970. m2 &= ~0xC30FF01F;
  2971. m2 |= 0x0000900F;
  2972. }
  2973. writel(m2, port_mmio + PHY_MODE2);
  2974. }
  2975. /* TODO: use the generic LED interface to configure the SATA Presence */
  2976. /* & Acitivy LEDs on the board */
  2977. static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
  2978. void __iomem *mmio)
  2979. {
  2980. return;
  2981. }
  2982. static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
  2983. void __iomem *mmio)
  2984. {
  2985. void __iomem *port_mmio;
  2986. u32 tmp;
  2987. port_mmio = mv_port_base(mmio, idx);
  2988. tmp = readl(port_mmio + PHY_MODE2);
  2989. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  2990. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  2991. }
  2992. #undef ZERO
  2993. #define ZERO(reg) writel(0, port_mmio + (reg))
  2994. static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
  2995. void __iomem *mmio, unsigned int port)
  2996. {
  2997. void __iomem *port_mmio = mv_port_base(mmio, port);
  2998. mv_reset_channel(hpriv, mmio, port);
  2999. ZERO(0x028); /* command */
  3000. writel(0x101f, port_mmio + EDMA_CFG);
  3001. ZERO(0x004); /* timer */
  3002. ZERO(0x008); /* irq err cause */
  3003. ZERO(0x00c); /* irq err mask */
  3004. ZERO(0x010); /* rq bah */
  3005. ZERO(0x014); /* rq inp */
  3006. ZERO(0x018); /* rq outp */
  3007. ZERO(0x01c); /* respq bah */
  3008. ZERO(0x024); /* respq outp */
  3009. ZERO(0x020); /* respq inp */
  3010. ZERO(0x02c); /* test control */
  3011. writel(0x800, port_mmio + EDMA_IORDY_TMOUT);
  3012. }
  3013. #undef ZERO
  3014. #define ZERO(reg) writel(0, hc_mmio + (reg))
  3015. static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
  3016. void __iomem *mmio)
  3017. {
  3018. void __iomem *hc_mmio = mv_hc_base(mmio, 0);
  3019. ZERO(0x00c);
  3020. ZERO(0x010);
  3021. ZERO(0x014);
  3022. }
  3023. #undef ZERO
  3024. static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
  3025. void __iomem *mmio, unsigned int n_hc)
  3026. {
  3027. unsigned int port;
  3028. for (port = 0; port < hpriv->n_ports; port++)
  3029. mv_soc_reset_hc_port(hpriv, mmio, port);
  3030. mv_soc_reset_one_hc(hpriv, mmio);
  3031. return 0;
  3032. }
  3033. static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
  3034. void __iomem *mmio)
  3035. {
  3036. return;
  3037. }
  3038. static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
  3039. {
  3040. return;
  3041. }
  3042. static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv,
  3043. void __iomem *mmio, unsigned int port)
  3044. {
  3045. void __iomem *port_mmio = mv_port_base(mmio, port);
  3046. u32 reg;
  3047. reg = readl(port_mmio + PHY_MODE3);
  3048. reg &= ~(0x3 << 27); /* SELMUPF (bits 28:27) to 1 */
  3049. reg |= (0x1 << 27);
  3050. reg &= ~(0x3 << 29); /* SELMUPI (bits 30:29) to 1 */
  3051. reg |= (0x1 << 29);
  3052. writel(reg, port_mmio + PHY_MODE3);
  3053. reg = readl(port_mmio + PHY_MODE4);
  3054. reg &= ~0x1; /* SATU_OD8 (bit 0) to 0, reserved bit 16 must be set */
  3055. reg |= (0x1 << 16);
  3056. writel(reg, port_mmio + PHY_MODE4);
  3057. reg = readl(port_mmio + PHY_MODE9_GEN2);
  3058. reg &= ~0xf; /* TXAMP[3:0] (bits 3:0) to 8 */
  3059. reg |= 0x8;
  3060. reg &= ~(0x1 << 14); /* TXAMP[4] (bit 14) to 0 */
  3061. writel(reg, port_mmio + PHY_MODE9_GEN2);
  3062. reg = readl(port_mmio + PHY_MODE9_GEN1);
  3063. reg &= ~0xf; /* TXAMP[3:0] (bits 3:0) to 8 */
  3064. reg |= 0x8;
  3065. reg &= ~(0x1 << 14); /* TXAMP[4] (bit 14) to 0 */
  3066. writel(reg, port_mmio + PHY_MODE9_GEN1);
  3067. }
  3068. /**
  3069. * soc_is_65 - check if the soc is 65 nano device
  3070. *
  3071. * Detect the type of the SoC, this is done by reading the PHYCFG_OFS
  3072. * register, this register should contain non-zero value and it exists only
  3073. * in the 65 nano devices, when reading it from older devices we get 0.
  3074. */
  3075. static bool soc_is_65n(struct mv_host_priv *hpriv)
  3076. {
  3077. void __iomem *port0_mmio = mv_port_base(hpriv->base, 0);
  3078. if (readl(port0_mmio + PHYCFG_OFS))
  3079. return true;
  3080. return false;
  3081. }
  3082. static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
  3083. {
  3084. u32 ifcfg = readl(port_mmio + SATA_IFCFG);
  3085. ifcfg = (ifcfg & 0xf7f) | 0x9b1000; /* from chip spec */
  3086. if (want_gen2i)
  3087. ifcfg |= (1 << 7); /* enable gen2i speed */
  3088. writelfl(ifcfg, port_mmio + SATA_IFCFG);
  3089. }
  3090. static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
  3091. unsigned int port_no)
  3092. {
  3093. void __iomem *port_mmio = mv_port_base(mmio, port_no);
  3094. /*
  3095. * The datasheet warns against setting EDMA_RESET when EDMA is active
  3096. * (but doesn't say what the problem might be). So we first try
  3097. * to disable the EDMA engine before doing the EDMA_RESET operation.
  3098. */
  3099. mv_stop_edma_engine(port_mmio);
  3100. writelfl(EDMA_RESET, port_mmio + EDMA_CMD);
  3101. if (!IS_GEN_I(hpriv)) {
  3102. /* Enable 3.0gb/s link speed: this survives EDMA_RESET */
  3103. mv_setup_ifcfg(port_mmio, 1);
  3104. }
  3105. /*
  3106. * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
  3107. * link, and physical layers. It resets all SATA interface registers
  3108. * (except for SATA_IFCFG), and issues a COMRESET to the dev.
  3109. */
  3110. writelfl(EDMA_RESET, port_mmio + EDMA_CMD);
  3111. udelay(25); /* allow reset propagation */
  3112. writelfl(0, port_mmio + EDMA_CMD);
  3113. hpriv->ops->phy_errata(hpriv, mmio, port_no);
  3114. if (IS_GEN_I(hpriv))
  3115. mdelay(1);
  3116. }
  3117. static void mv_pmp_select(struct ata_port *ap, int pmp)
  3118. {
  3119. if (sata_pmp_supported(ap)) {
  3120. void __iomem *port_mmio = mv_ap_base(ap);
  3121. u32 reg = readl(port_mmio + SATA_IFCTL);
  3122. int old = reg & 0xf;
  3123. if (old != pmp) {
  3124. reg = (reg & ~0xf) | pmp;
  3125. writelfl(reg, port_mmio + SATA_IFCTL);
  3126. }
  3127. }
  3128. }
  3129. static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
  3130. unsigned long deadline)
  3131. {
  3132. mv_pmp_select(link->ap, sata_srst_pmp(link));
  3133. return sata_std_hardreset(link, class, deadline);
  3134. }
  3135. static int mv_softreset(struct ata_link *link, unsigned int *class,
  3136. unsigned long deadline)
  3137. {
  3138. mv_pmp_select(link->ap, sata_srst_pmp(link));
  3139. return ata_sff_softreset(link, class, deadline);
  3140. }
  3141. static int mv_hardreset(struct ata_link *link, unsigned int *class,
  3142. unsigned long deadline)
  3143. {
  3144. struct ata_port *ap = link->ap;
  3145. struct mv_host_priv *hpriv = ap->host->private_data;
  3146. struct mv_port_priv *pp = ap->private_data;
  3147. void __iomem *mmio = hpriv->base;
  3148. int rc, attempts = 0, extra = 0;
  3149. u32 sstatus;
  3150. bool online;
  3151. mv_reset_channel(hpriv, mmio, ap->port_no);
  3152. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  3153. pp->pp_flags &=
  3154. ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
  3155. /* Workaround for errata FEr SATA#10 (part 2) */
  3156. do {
  3157. const unsigned long *timing =
  3158. sata_ehc_deb_timing(&link->eh_context);
  3159. rc = sata_link_hardreset(link, timing, deadline + extra,
  3160. &online, NULL);
  3161. rc = online ? -EAGAIN : rc;
  3162. if (rc)
  3163. return rc;
  3164. sata_scr_read(link, SCR_STATUS, &sstatus);
  3165. if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
  3166. /* Force 1.5gb/s link speed and try again */
  3167. mv_setup_ifcfg(mv_ap_base(ap), 0);
  3168. if (time_after(jiffies + HZ, deadline))
  3169. extra = HZ; /* only extend it once, max */
  3170. }
  3171. } while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
  3172. mv_save_cached_regs(ap);
  3173. mv_edma_cfg(ap, 0, 0);
  3174. return rc;
  3175. }
  3176. static void mv_eh_freeze(struct ata_port *ap)
  3177. {
  3178. mv_stop_edma(ap);
  3179. mv_enable_port_irqs(ap, 0);
  3180. }
  3181. static void mv_eh_thaw(struct ata_port *ap)
  3182. {
  3183. struct mv_host_priv *hpriv = ap->host->private_data;
  3184. unsigned int port = ap->port_no;
  3185. unsigned int hardport = mv_hardport_from_port(port);
  3186. void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
  3187. void __iomem *port_mmio = mv_ap_base(ap);
  3188. u32 hc_irq_cause;
  3189. /* clear EDMA errors on this port */
  3190. writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
  3191. /* clear pending irq events */
  3192. hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
  3193. writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE);
  3194. mv_enable_port_irqs(ap, ERR_IRQ);
  3195. }
  3196. /**
  3197. * mv_port_init - Perform some early initialization on a single port.
  3198. * @port: libata data structure storing shadow register addresses
  3199. * @port_mmio: base address of the port
  3200. *
  3201. * Initialize shadow register mmio addresses, clear outstanding
  3202. * interrupts on the port, and unmask interrupts for the future
  3203. * start of the port.
  3204. *
  3205. * LOCKING:
  3206. * Inherited from caller.
  3207. */
  3208. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  3209. {
  3210. void __iomem *serr, *shd_base = port_mmio + SHD_BLK;
  3211. /* PIO related setup
  3212. */
  3213. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  3214. port->error_addr =
  3215. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  3216. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  3217. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  3218. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  3219. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  3220. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  3221. port->status_addr =
  3222. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  3223. /* special case: control/altstatus doesn't have ATA_REG_ address */
  3224. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST;
  3225. /* Clear any currently outstanding port interrupt conditions */
  3226. serr = port_mmio + mv_scr_offset(SCR_ERROR);
  3227. writelfl(readl(serr), serr);
  3228. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
  3229. /* unmask all non-transient EDMA error interrupts */
  3230. writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK);
  3231. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  3232. readl(port_mmio + EDMA_CFG),
  3233. readl(port_mmio + EDMA_ERR_IRQ_CAUSE),
  3234. readl(port_mmio + EDMA_ERR_IRQ_MASK));
  3235. }
  3236. static unsigned int mv_in_pcix_mode(struct ata_host *host)
  3237. {
  3238. struct mv_host_priv *hpriv = host->private_data;
  3239. void __iomem *mmio = hpriv->base;
  3240. u32 reg;
  3241. if (IS_SOC(hpriv) || !IS_PCIE(hpriv))
  3242. return 0; /* not PCI-X capable */
  3243. reg = readl(mmio + MV_PCI_MODE);
  3244. if ((reg & MV_PCI_MODE_MASK) == 0)
  3245. return 0; /* conventional PCI mode */
  3246. return 1; /* chip is in PCI-X mode */
  3247. }
  3248. static int mv_pci_cut_through_okay(struct ata_host *host)
  3249. {
  3250. struct mv_host_priv *hpriv = host->private_data;
  3251. void __iomem *mmio = hpriv->base;
  3252. u32 reg;
  3253. if (!mv_in_pcix_mode(host)) {
  3254. reg = readl(mmio + MV_PCI_COMMAND);
  3255. if (reg & MV_PCI_COMMAND_MRDTRIG)
  3256. return 0; /* not okay */
  3257. }
  3258. return 1; /* okay */
  3259. }
  3260. static void mv_60x1b2_errata_pci7(struct ata_host *host)
  3261. {
  3262. struct mv_host_priv *hpriv = host->private_data;
  3263. void __iomem *mmio = hpriv->base;
  3264. /* workaround for 60x1-B2 errata PCI#7 */
  3265. if (mv_in_pcix_mode(host)) {
  3266. u32 reg = readl(mmio + MV_PCI_COMMAND);
  3267. writelfl(reg & ~MV_PCI_COMMAND_MWRCOM, mmio + MV_PCI_COMMAND);
  3268. }
  3269. }
  3270. static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
  3271. {
  3272. struct pci_dev *pdev = to_pci_dev(host->dev);
  3273. struct mv_host_priv *hpriv = host->private_data;
  3274. u32 hp_flags = hpriv->hp_flags;
  3275. switch (board_idx) {
  3276. case chip_5080:
  3277. hpriv->ops = &mv5xxx_ops;
  3278. hp_flags |= MV_HP_GEN_I;
  3279. switch (pdev->revision) {
  3280. case 0x1:
  3281. hp_flags |= MV_HP_ERRATA_50XXB0;
  3282. break;
  3283. case 0x3:
  3284. hp_flags |= MV_HP_ERRATA_50XXB2;
  3285. break;
  3286. default:
  3287. dev_warn(&pdev->dev,
  3288. "Applying 50XXB2 workarounds to unknown rev\n");
  3289. hp_flags |= MV_HP_ERRATA_50XXB2;
  3290. break;
  3291. }
  3292. break;
  3293. case chip_504x:
  3294. case chip_508x:
  3295. hpriv->ops = &mv5xxx_ops;
  3296. hp_flags |= MV_HP_GEN_I;
  3297. switch (pdev->revision) {
  3298. case 0x0:
  3299. hp_flags |= MV_HP_ERRATA_50XXB0;
  3300. break;
  3301. case 0x3:
  3302. hp_flags |= MV_HP_ERRATA_50XXB2;
  3303. break;
  3304. default:
  3305. dev_warn(&pdev->dev,
  3306. "Applying B2 workarounds to unknown rev\n");
  3307. hp_flags |= MV_HP_ERRATA_50XXB2;
  3308. break;
  3309. }
  3310. break;
  3311. case chip_604x:
  3312. case chip_608x:
  3313. hpriv->ops = &mv6xxx_ops;
  3314. hp_flags |= MV_HP_GEN_II;
  3315. switch (pdev->revision) {
  3316. case 0x7:
  3317. mv_60x1b2_errata_pci7(host);
  3318. hp_flags |= MV_HP_ERRATA_60X1B2;
  3319. break;
  3320. case 0x9:
  3321. hp_flags |= MV_HP_ERRATA_60X1C0;
  3322. break;
  3323. default:
  3324. dev_warn(&pdev->dev,
  3325. "Applying B2 workarounds to unknown rev\n");
  3326. hp_flags |= MV_HP_ERRATA_60X1B2;
  3327. break;
  3328. }
  3329. break;
  3330. case chip_7042:
  3331. hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
  3332. if (pdev->vendor == PCI_VENDOR_ID_TTI &&
  3333. (pdev->device == 0x2300 || pdev->device == 0x2310))
  3334. {
  3335. /*
  3336. * Highpoint RocketRAID PCIe 23xx series cards:
  3337. *
  3338. * Unconfigured drives are treated as "Legacy"
  3339. * by the BIOS, and it overwrites sector 8 with
  3340. * a "Lgcy" metadata block prior to Linux boot.
  3341. *
  3342. * Configured drives (RAID or JBOD) leave sector 8
  3343. * alone, but instead overwrite a high numbered
  3344. * sector for the RAID metadata. This sector can
  3345. * be determined exactly, by truncating the physical
  3346. * drive capacity to a nice even GB value.
  3347. *
  3348. * RAID metadata is at: (dev->n_sectors & ~0xfffff)
  3349. *
  3350. * Warn the user, lest they think we're just buggy.
  3351. */
  3352. printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
  3353. " BIOS CORRUPTS DATA on all attached drives,"
  3354. " regardless of if/how they are configured."
  3355. " BEWARE!\n");
  3356. printk(KERN_WARNING DRV_NAME ": For data safety, do not"
  3357. " use sectors 8-9 on \"Legacy\" drives,"
  3358. " and avoid the final two gigabytes on"
  3359. " all RocketRAID BIOS initialized drives.\n");
  3360. }
  3361. /* drop through */
  3362. case chip_6042:
  3363. hpriv->ops = &mv6xxx_ops;
  3364. hp_flags |= MV_HP_GEN_IIE;
  3365. if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
  3366. hp_flags |= MV_HP_CUT_THROUGH;
  3367. switch (pdev->revision) {
  3368. case 0x2: /* Rev.B0: the first/only public release */
  3369. hp_flags |= MV_HP_ERRATA_60X1C0;
  3370. break;
  3371. default:
  3372. dev_warn(&pdev->dev,
  3373. "Applying 60X1C0 workarounds to unknown rev\n");
  3374. hp_flags |= MV_HP_ERRATA_60X1C0;
  3375. break;
  3376. }
  3377. break;
  3378. case chip_soc:
  3379. if (soc_is_65n(hpriv))
  3380. hpriv->ops = &mv_soc_65n_ops;
  3381. else
  3382. hpriv->ops = &mv_soc_ops;
  3383. hp_flags |= MV_HP_FLAG_SOC | MV_HP_GEN_IIE |
  3384. MV_HP_ERRATA_60X1C0;
  3385. break;
  3386. default:
  3387. dev_err(host->dev, "BUG: invalid board index %u\n", board_idx);
  3388. return 1;
  3389. }
  3390. hpriv->hp_flags = hp_flags;
  3391. if (hp_flags & MV_HP_PCIE) {
  3392. hpriv->irq_cause_offset = PCIE_IRQ_CAUSE;
  3393. hpriv->irq_mask_offset = PCIE_IRQ_MASK;
  3394. hpriv->unmask_all_irqs = PCIE_UNMASK_ALL_IRQS;
  3395. } else {
  3396. hpriv->irq_cause_offset = PCI_IRQ_CAUSE;
  3397. hpriv->irq_mask_offset = PCI_IRQ_MASK;
  3398. hpriv->unmask_all_irqs = PCI_UNMASK_ALL_IRQS;
  3399. }
  3400. return 0;
  3401. }
  3402. /**
  3403. * mv_init_host - Perform some early initialization of the host.
  3404. * @host: ATA host to initialize
  3405. *
  3406. * If possible, do an early global reset of the host. Then do
  3407. * our port init and clear/unmask all/relevant host interrupts.
  3408. *
  3409. * LOCKING:
  3410. * Inherited from caller.
  3411. */
  3412. static int mv_init_host(struct ata_host *host)
  3413. {
  3414. int rc = 0, n_hc, port, hc;
  3415. struct mv_host_priv *hpriv = host->private_data;
  3416. void __iomem *mmio = hpriv->base;
  3417. rc = mv_chip_id(host, hpriv->board_idx);
  3418. if (rc)
  3419. goto done;
  3420. if (IS_SOC(hpriv)) {
  3421. hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE;
  3422. hpriv->main_irq_mask_addr = mmio + SOC_HC_MAIN_IRQ_MASK;
  3423. } else {
  3424. hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE;
  3425. hpriv->main_irq_mask_addr = mmio + PCI_HC_MAIN_IRQ_MASK;
  3426. }
  3427. /* initialize shadow irq mask with register's value */
  3428. hpriv->main_irq_mask = readl(hpriv->main_irq_mask_addr);
  3429. /* global interrupt mask: 0 == mask everything */
  3430. mv_set_main_irq_mask(host, ~0, 0);
  3431. n_hc = mv_get_hc_count(host->ports[0]->flags);
  3432. for (port = 0; port < host->n_ports; port++)
  3433. if (hpriv->ops->read_preamp)
  3434. hpriv->ops->read_preamp(hpriv, port, mmio);
  3435. rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
  3436. if (rc)
  3437. goto done;
  3438. hpriv->ops->reset_flash(hpriv, mmio);
  3439. hpriv->ops->reset_bus(host, mmio);
  3440. hpriv->ops->enable_leds(hpriv, mmio);
  3441. for (port = 0; port < host->n_ports; port++) {
  3442. struct ata_port *ap = host->ports[port];
  3443. void __iomem *port_mmio = mv_port_base(mmio, port);
  3444. mv_port_init(&ap->ioaddr, port_mmio);
  3445. }
  3446. for (hc = 0; hc < n_hc; hc++) {
  3447. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  3448. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  3449. "(before clear)=0x%08x\n", hc,
  3450. readl(hc_mmio + HC_CFG),
  3451. readl(hc_mmio + HC_IRQ_CAUSE));
  3452. /* Clear any currently outstanding hc interrupt conditions */
  3453. writelfl(0, hc_mmio + HC_IRQ_CAUSE);
  3454. }
  3455. if (!IS_SOC(hpriv)) {
  3456. /* Clear any currently outstanding host interrupt conditions */
  3457. writelfl(0, mmio + hpriv->irq_cause_offset);
  3458. /* and unmask interrupt generation for host regs */
  3459. writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_offset);
  3460. }
  3461. /*
  3462. * enable only global host interrupts for now.
  3463. * The per-port interrupts get done later as ports are set up.
  3464. */
  3465. mv_set_main_irq_mask(host, 0, PCI_ERR);
  3466. mv_set_irq_coalescing(host, irq_coalescing_io_count,
  3467. irq_coalescing_usecs);
  3468. done:
  3469. return rc;
  3470. }
  3471. static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
  3472. {
  3473. hpriv->crqb_pool = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
  3474. MV_CRQB_Q_SZ, 0);
  3475. if (!hpriv->crqb_pool)
  3476. return -ENOMEM;
  3477. hpriv->crpb_pool = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
  3478. MV_CRPB_Q_SZ, 0);
  3479. if (!hpriv->crpb_pool)
  3480. return -ENOMEM;
  3481. hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
  3482. MV_SG_TBL_SZ, 0);
  3483. if (!hpriv->sg_tbl_pool)
  3484. return -ENOMEM;
  3485. return 0;
  3486. }
  3487. static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
  3488. const struct mbus_dram_target_info *dram)
  3489. {
  3490. int i;
  3491. for (i = 0; i < 4; i++) {
  3492. writel(0, hpriv->base + WINDOW_CTRL(i));
  3493. writel(0, hpriv->base + WINDOW_BASE(i));
  3494. }
  3495. for (i = 0; i < dram->num_cs; i++) {
  3496. const struct mbus_dram_window *cs = dram->cs + i;
  3497. writel(((cs->size - 1) & 0xffff0000) |
  3498. (cs->mbus_attr << 8) |
  3499. (dram->mbus_dram_target_id << 4) | 1,
  3500. hpriv->base + WINDOW_CTRL(i));
  3501. writel(cs->base, hpriv->base + WINDOW_BASE(i));
  3502. }
  3503. }
  3504. /**
  3505. * mv_platform_probe - handle a positive probe of an soc Marvell
  3506. * host
  3507. * @pdev: platform device found
  3508. *
  3509. * LOCKING:
  3510. * Inherited from caller.
  3511. */
  3512. static int mv_platform_probe(struct platform_device *pdev)
  3513. {
  3514. const struct mv_sata_platform_data *mv_platform_data;
  3515. const struct mbus_dram_target_info *dram;
  3516. const struct ata_port_info *ppi[] =
  3517. { &mv_port_info[chip_soc], NULL };
  3518. struct ata_host *host;
  3519. struct mv_host_priv *hpriv;
  3520. struct resource *res;
  3521. int n_ports = 0, irq = 0;
  3522. int rc;
  3523. #if defined(CONFIG_HAVE_CLK)
  3524. int port;
  3525. #endif
  3526. ata_print_version_once(&pdev->dev, DRV_VERSION);
  3527. /*
  3528. * Simple resource validation ..
  3529. */
  3530. if (unlikely(pdev->num_resources != 2)) {
  3531. dev_err(&pdev->dev, "invalid number of resources\n");
  3532. return -EINVAL;
  3533. }
  3534. /*
  3535. * Get the register base first
  3536. */
  3537. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3538. if (res == NULL)
  3539. return -EINVAL;
  3540. /* allocate host */
  3541. if (pdev->dev.of_node) {
  3542. of_property_read_u32(pdev->dev.of_node, "nr-ports", &n_ports);
  3543. irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  3544. } else {
  3545. mv_platform_data = pdev->dev.platform_data;
  3546. n_ports = mv_platform_data->n_ports;
  3547. irq = platform_get_irq(pdev, 0);
  3548. }
  3549. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  3550. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  3551. if (!host || !hpriv)
  3552. return -ENOMEM;
  3553. #if defined(CONFIG_HAVE_CLK)
  3554. hpriv->port_clks = devm_kzalloc(&pdev->dev,
  3555. sizeof(struct clk *) * n_ports,
  3556. GFP_KERNEL);
  3557. if (!hpriv->port_clks)
  3558. return -ENOMEM;
  3559. #endif
  3560. host->private_data = hpriv;
  3561. hpriv->n_ports = n_ports;
  3562. hpriv->board_idx = chip_soc;
  3563. host->iomap = NULL;
  3564. hpriv->base = devm_ioremap(&pdev->dev, res->start,
  3565. resource_size(res));
  3566. hpriv->base -= SATAHC0_REG_BASE;
  3567. #if defined(CONFIG_HAVE_CLK)
  3568. hpriv->clk = clk_get(&pdev->dev, NULL);
  3569. if (IS_ERR(hpriv->clk))
  3570. dev_notice(&pdev->dev, "cannot get optional clkdev\n");
  3571. else
  3572. clk_prepare_enable(hpriv->clk);
  3573. for (port = 0; port < n_ports; port++) {
  3574. char port_number[16];
  3575. sprintf(port_number, "%d", port);
  3576. hpriv->port_clks[port] = clk_get(&pdev->dev, port_number);
  3577. if (!IS_ERR(hpriv->port_clks[port]))
  3578. clk_prepare_enable(hpriv->port_clks[port]);
  3579. }
  3580. #endif
  3581. /*
  3582. * (Re-)program MBUS remapping windows if we are asked to.
  3583. */
  3584. dram = mv_mbus_dram_info();
  3585. if (dram)
  3586. mv_conf_mbus_windows(hpriv, dram);
  3587. rc = mv_create_dma_pools(hpriv, &pdev->dev);
  3588. if (rc)
  3589. goto err;
  3590. /* initialize adapter */
  3591. rc = mv_init_host(host);
  3592. if (rc)
  3593. goto err;
  3594. dev_info(&pdev->dev, "slots %u ports %d\n",
  3595. (unsigned)MV_MAX_Q_DEPTH, host->n_ports);
  3596. rc = ata_host_activate(host, irq, mv_interrupt, IRQF_SHARED, &mv6_sht);
  3597. if (!rc)
  3598. return 0;
  3599. err:
  3600. #if defined(CONFIG_HAVE_CLK)
  3601. if (!IS_ERR(hpriv->clk)) {
  3602. clk_disable_unprepare(hpriv->clk);
  3603. clk_put(hpriv->clk);
  3604. }
  3605. for (port = 0; port < n_ports; port++) {
  3606. if (!IS_ERR(hpriv->port_clks[port])) {
  3607. clk_disable_unprepare(hpriv->port_clks[port]);
  3608. clk_put(hpriv->port_clks[port]);
  3609. }
  3610. }
  3611. #endif
  3612. return rc;
  3613. }
  3614. /*
  3615. *
  3616. * mv_platform_remove - unplug a platform interface
  3617. * @pdev: platform device
  3618. *
  3619. * A platform bus SATA device has been unplugged. Perform the needed
  3620. * cleanup. Also called on module unload for any active devices.
  3621. */
  3622. static int mv_platform_remove(struct platform_device *pdev)
  3623. {
  3624. struct ata_host *host = platform_get_drvdata(pdev);
  3625. #if defined(CONFIG_HAVE_CLK)
  3626. struct mv_host_priv *hpriv = host->private_data;
  3627. int port;
  3628. #endif
  3629. ata_host_detach(host);
  3630. #if defined(CONFIG_HAVE_CLK)
  3631. if (!IS_ERR(hpriv->clk)) {
  3632. clk_disable_unprepare(hpriv->clk);
  3633. clk_put(hpriv->clk);
  3634. }
  3635. for (port = 0; port < host->n_ports; port++) {
  3636. if (!IS_ERR(hpriv->port_clks[port])) {
  3637. clk_disable_unprepare(hpriv->port_clks[port]);
  3638. clk_put(hpriv->port_clks[port]);
  3639. }
  3640. }
  3641. #endif
  3642. return 0;
  3643. }
  3644. #ifdef CONFIG_PM
  3645. static int mv_platform_suspend(struct platform_device *pdev, pm_message_t state)
  3646. {
  3647. struct ata_host *host = platform_get_drvdata(pdev);
  3648. if (host)
  3649. return ata_host_suspend(host, state);
  3650. else
  3651. return 0;
  3652. }
  3653. static int mv_platform_resume(struct platform_device *pdev)
  3654. {
  3655. struct ata_host *host = platform_get_drvdata(pdev);
  3656. const struct mbus_dram_target_info *dram;
  3657. int ret;
  3658. if (host) {
  3659. struct mv_host_priv *hpriv = host->private_data;
  3660. /*
  3661. * (Re-)program MBUS remapping windows if we are asked to.
  3662. */
  3663. dram = mv_mbus_dram_info();
  3664. if (dram)
  3665. mv_conf_mbus_windows(hpriv, dram);
  3666. /* initialize adapter */
  3667. ret = mv_init_host(host);
  3668. if (ret) {
  3669. printk(KERN_ERR DRV_NAME ": Error during HW init\n");
  3670. return ret;
  3671. }
  3672. ata_host_resume(host);
  3673. }
  3674. return 0;
  3675. }
  3676. #else
  3677. #define mv_platform_suspend NULL
  3678. #define mv_platform_resume NULL
  3679. #endif
  3680. #ifdef CONFIG_OF
  3681. static struct of_device_id mv_sata_dt_ids[] = {
  3682. { .compatible = "marvell,orion-sata", },
  3683. {},
  3684. };
  3685. MODULE_DEVICE_TABLE(of, mv_sata_dt_ids);
  3686. #endif
  3687. static struct platform_driver mv_platform_driver = {
  3688. .probe = mv_platform_probe,
  3689. .remove = mv_platform_remove,
  3690. .suspend = mv_platform_suspend,
  3691. .resume = mv_platform_resume,
  3692. .driver = {
  3693. .name = DRV_NAME,
  3694. .owner = THIS_MODULE,
  3695. .of_match_table = of_match_ptr(mv_sata_dt_ids),
  3696. },
  3697. };
  3698. #ifdef CONFIG_PCI
  3699. static int mv_pci_init_one(struct pci_dev *pdev,
  3700. const struct pci_device_id *ent);
  3701. #ifdef CONFIG_PM
  3702. static int mv_pci_device_resume(struct pci_dev *pdev);
  3703. #endif
  3704. static struct pci_driver mv_pci_driver = {
  3705. .name = DRV_NAME,
  3706. .id_table = mv_pci_tbl,
  3707. .probe = mv_pci_init_one,
  3708. .remove = ata_pci_remove_one,
  3709. #ifdef CONFIG_PM
  3710. .suspend = ata_pci_device_suspend,
  3711. .resume = mv_pci_device_resume,
  3712. #endif
  3713. };
  3714. /* move to PCI layer or libata core? */
  3715. static int pci_go_64(struct pci_dev *pdev)
  3716. {
  3717. int rc;
  3718. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  3719. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  3720. if (rc) {
  3721. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3722. if (rc) {
  3723. dev_err(&pdev->dev,
  3724. "64-bit DMA enable failed\n");
  3725. return rc;
  3726. }
  3727. }
  3728. } else {
  3729. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3730. if (rc) {
  3731. dev_err(&pdev->dev, "32-bit DMA enable failed\n");
  3732. return rc;
  3733. }
  3734. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3735. if (rc) {
  3736. dev_err(&pdev->dev,
  3737. "32-bit consistent DMA enable failed\n");
  3738. return rc;
  3739. }
  3740. }
  3741. return rc;
  3742. }
  3743. /**
  3744. * mv_print_info - Dump key info to kernel log for perusal.
  3745. * @host: ATA host to print info about
  3746. *
  3747. * FIXME: complete this.
  3748. *
  3749. * LOCKING:
  3750. * Inherited from caller.
  3751. */
  3752. static void mv_print_info(struct ata_host *host)
  3753. {
  3754. struct pci_dev *pdev = to_pci_dev(host->dev);
  3755. struct mv_host_priv *hpriv = host->private_data;
  3756. u8 scc;
  3757. const char *scc_s, *gen;
  3758. /* Use this to determine the HW stepping of the chip so we know
  3759. * what errata to workaround
  3760. */
  3761. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  3762. if (scc == 0)
  3763. scc_s = "SCSI";
  3764. else if (scc == 0x01)
  3765. scc_s = "RAID";
  3766. else
  3767. scc_s = "?";
  3768. if (IS_GEN_I(hpriv))
  3769. gen = "I";
  3770. else if (IS_GEN_II(hpriv))
  3771. gen = "II";
  3772. else if (IS_GEN_IIE(hpriv))
  3773. gen = "IIE";
  3774. else
  3775. gen = "?";
  3776. dev_info(&pdev->dev, "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
  3777. gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
  3778. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  3779. }
  3780. /**
  3781. * mv_pci_init_one - handle a positive probe of a PCI Marvell host
  3782. * @pdev: PCI device found
  3783. * @ent: PCI device ID entry for the matched host
  3784. *
  3785. * LOCKING:
  3786. * Inherited from caller.
  3787. */
  3788. static int mv_pci_init_one(struct pci_dev *pdev,
  3789. const struct pci_device_id *ent)
  3790. {
  3791. unsigned int board_idx = (unsigned int)ent->driver_data;
  3792. const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
  3793. struct ata_host *host;
  3794. struct mv_host_priv *hpriv;
  3795. int n_ports, port, rc;
  3796. ata_print_version_once(&pdev->dev, DRV_VERSION);
  3797. /* allocate host */
  3798. n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
  3799. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  3800. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  3801. if (!host || !hpriv)
  3802. return -ENOMEM;
  3803. host->private_data = hpriv;
  3804. hpriv->n_ports = n_ports;
  3805. hpriv->board_idx = board_idx;
  3806. /* acquire resources */
  3807. rc = pcim_enable_device(pdev);
  3808. if (rc)
  3809. return rc;
  3810. rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
  3811. if (rc == -EBUSY)
  3812. pcim_pin_device(pdev);
  3813. if (rc)
  3814. return rc;
  3815. host->iomap = pcim_iomap_table(pdev);
  3816. hpriv->base = host->iomap[MV_PRIMARY_BAR];
  3817. rc = pci_go_64(pdev);
  3818. if (rc)
  3819. return rc;
  3820. rc = mv_create_dma_pools(hpriv, &pdev->dev);
  3821. if (rc)
  3822. return rc;
  3823. for (port = 0; port < host->n_ports; port++) {
  3824. struct ata_port *ap = host->ports[port];
  3825. void __iomem *port_mmio = mv_port_base(hpriv->base, port);
  3826. unsigned int offset = port_mmio - hpriv->base;
  3827. ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
  3828. ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
  3829. }
  3830. /* initialize adapter */
  3831. rc = mv_init_host(host);
  3832. if (rc)
  3833. return rc;
  3834. /* Enable message-switched interrupts, if requested */
  3835. if (msi && pci_enable_msi(pdev) == 0)
  3836. hpriv->hp_flags |= MV_HP_FLAG_MSI;
  3837. mv_dump_pci_cfg(pdev, 0x68);
  3838. mv_print_info(host);
  3839. pci_set_master(pdev);
  3840. pci_try_set_mwi(pdev);
  3841. return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
  3842. IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
  3843. }
  3844. #ifdef CONFIG_PM
  3845. static int mv_pci_device_resume(struct pci_dev *pdev)
  3846. {
  3847. struct ata_host *host = pci_get_drvdata(pdev);
  3848. int rc;
  3849. rc = ata_pci_device_do_resume(pdev);
  3850. if (rc)
  3851. return rc;
  3852. /* initialize adapter */
  3853. rc = mv_init_host(host);
  3854. if (rc)
  3855. return rc;
  3856. ata_host_resume(host);
  3857. return 0;
  3858. }
  3859. #endif
  3860. #endif
  3861. static int mv_platform_probe(struct platform_device *pdev);
  3862. static int mv_platform_remove(struct platform_device *pdev);
  3863. static int __init mv_init(void)
  3864. {
  3865. int rc = -ENODEV;
  3866. #ifdef CONFIG_PCI
  3867. rc = pci_register_driver(&mv_pci_driver);
  3868. if (rc < 0)
  3869. return rc;
  3870. #endif
  3871. rc = platform_driver_register(&mv_platform_driver);
  3872. #ifdef CONFIG_PCI
  3873. if (rc < 0)
  3874. pci_unregister_driver(&mv_pci_driver);
  3875. #endif
  3876. return rc;
  3877. }
  3878. static void __exit mv_exit(void)
  3879. {
  3880. #ifdef CONFIG_PCI
  3881. pci_unregister_driver(&mv_pci_driver);
  3882. #endif
  3883. platform_driver_unregister(&mv_platform_driver);
  3884. }
  3885. MODULE_AUTHOR("Brett Russ");
  3886. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  3887. MODULE_LICENSE("GPL");
  3888. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  3889. MODULE_VERSION(DRV_VERSION);
  3890. MODULE_ALIAS("platform:" DRV_NAME);
  3891. module_init(mv_init);
  3892. module_exit(mv_exit);