sata_mv.c 82 KB

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