sata_mv.c 110 KB

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