sata_mv.c 122 KB

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