sata_mv.c 95 KB

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