sata_mv.c 113 KB

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