sata_mv.c 95 KB

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