sata_mv.c 83 KB

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