sata_mv.c 119 KB

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