sata_mv.c 94 KB

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