sata_mv.c 95 KB

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