sata_mv.c 117 KB

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