sata_mv.c 83 KB

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