sata_mv.c 94 KB

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