sata_mv.c 95 KB

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