sata_mv.c 94 KB

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