sata_mv.c 87 KB

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