sata_mv.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432
  1. /*
  2. * sata_mv.c - Marvell SATA support
  3. *
  4. * Copyright 2005: EMC Corporation, all rights reserved.
  5. * Copyright 2005 Red Hat, Inc. All rights reserved.
  6. *
  7. * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/pci.h>
  26. #include <linux/init.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/delay.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/sched.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/device.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi_cmnd.h>
  35. #include <linux/libata.h>
  36. #include <asm/io.h>
  37. #define DRV_NAME "sata_mv"
  38. #define DRV_VERSION "0.6"
  39. enum {
  40. /* BAR's are enumerated in terms of pci_resource_start() terms */
  41. MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
  42. MV_IO_BAR = 2, /* offset 0x18: IO space */
  43. MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
  44. MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
  45. MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
  46. MV_PCI_REG_BASE = 0,
  47. MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
  48. MV_SATAHC0_REG_BASE = 0x20000,
  49. MV_FLASH_CTL = 0x1046c,
  50. MV_GPIO_PORT_CTL = 0x104f0,
  51. MV_RESET_CFG = 0x180d8,
  52. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  53. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  54. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  55. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  56. MV_USE_Q_DEPTH = ATA_DEF_QUEUE,
  57. MV_MAX_Q_DEPTH = 32,
  58. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  59. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  60. * CRPB needs alignment on a 256B boundary. Size == 256B
  61. * SG count of 176 leads to MV_PORT_PRIV_DMA_SZ == 4KB
  62. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  63. */
  64. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  65. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  66. MV_MAX_SG_CT = 176,
  67. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  68. MV_PORT_PRIV_DMA_SZ = (MV_CRQB_Q_SZ + MV_CRPB_Q_SZ + MV_SG_TBL_SZ),
  69. MV_PORTS_PER_HC = 4,
  70. /* == (port / MV_PORTS_PER_HC) to determine HC from 0-7 port */
  71. MV_PORT_HC_SHIFT = 2,
  72. /* == (port % MV_PORTS_PER_HC) to determine hard port from 0-7 port */
  73. MV_PORT_MASK = 3,
  74. /* Host Flags */
  75. MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
  76. MV_FLAG_IRQ_COALESCE = (1 << 29), /* IRQ coalescing capability */
  77. MV_COMMON_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  78. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  79. ATA_FLAG_NO_ATAPI),
  80. MV_6XXX_FLAGS = MV_FLAG_IRQ_COALESCE,
  81. CRQB_FLAG_READ = (1 << 0),
  82. CRQB_TAG_SHIFT = 1,
  83. CRQB_CMD_ADDR_SHIFT = 8,
  84. CRQB_CMD_CS = (0x2 << 11),
  85. CRQB_CMD_LAST = (1 << 15),
  86. CRPB_FLAG_STATUS_SHIFT = 8,
  87. EPRD_FLAG_END_OF_TBL = (1 << 31),
  88. /* PCI interface registers */
  89. PCI_COMMAND_OFS = 0xc00,
  90. PCI_MAIN_CMD_STS_OFS = 0xd30,
  91. STOP_PCI_MASTER = (1 << 2),
  92. PCI_MASTER_EMPTY = (1 << 3),
  93. GLOB_SFT_RST = (1 << 4),
  94. MV_PCI_MODE = 0xd00,
  95. MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
  96. MV_PCI_DISC_TIMER = 0xd04,
  97. MV_PCI_MSI_TRIGGER = 0xc38,
  98. MV_PCI_SERR_MASK = 0xc28,
  99. MV_PCI_XBAR_TMOUT = 0x1d04,
  100. MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
  101. MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
  102. MV_PCI_ERR_ATTRIBUTE = 0x1d48,
  103. MV_PCI_ERR_COMMAND = 0x1d50,
  104. PCI_IRQ_CAUSE_OFS = 0x1d58,
  105. PCI_IRQ_MASK_OFS = 0x1d5c,
  106. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  107. HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
  108. HC_MAIN_IRQ_MASK_OFS = 0x1d64,
  109. PORT0_ERR = (1 << 0), /* shift by port # */
  110. PORT0_DONE = (1 << 1), /* shift by port # */
  111. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  112. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  113. PCI_ERR = (1 << 18),
  114. TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
  115. TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
  116. PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
  117. GPIO_INT = (1 << 22),
  118. SELF_INT = (1 << 23),
  119. TWSI_INT = (1 << 24),
  120. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  121. HC_MAIN_MASKED_IRQS = (TRAN_LO_DONE | TRAN_HI_DONE |
  122. PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
  123. HC_MAIN_RSVD),
  124. /* SATAHC registers */
  125. HC_CFG_OFS = 0,
  126. HC_IRQ_CAUSE_OFS = 0x14,
  127. CRPB_DMA_DONE = (1 << 0), /* shift by port # */
  128. HC_IRQ_COAL = (1 << 4), /* IRQ coalescing */
  129. DEV_IRQ = (1 << 8), /* shift by port # */
  130. /* Shadow block registers */
  131. SHD_BLK_OFS = 0x100,
  132. SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
  133. /* SATA registers */
  134. SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
  135. SATA_ACTIVE_OFS = 0x350,
  136. PHY_MODE3 = 0x310,
  137. PHY_MODE4 = 0x314,
  138. PHY_MODE2 = 0x330,
  139. MV5_PHY_MODE = 0x74,
  140. MV5_LT_MODE = 0x30,
  141. MV5_PHY_CTL = 0x0C,
  142. SATA_INTERFACE_CTL = 0x050,
  143. MV_M2_PREAMP_MASK = 0x7e0,
  144. /* Port registers */
  145. EDMA_CFG_OFS = 0,
  146. EDMA_CFG_Q_DEPTH = 0, /* queueing disabled */
  147. EDMA_CFG_NCQ = (1 << 5),
  148. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  149. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  150. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  151. EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
  152. EDMA_ERR_IRQ_MASK_OFS = 0xc,
  153. EDMA_ERR_D_PAR = (1 << 0),
  154. EDMA_ERR_PRD_PAR = (1 << 1),
  155. EDMA_ERR_DEV = (1 << 2),
  156. EDMA_ERR_DEV_DCON = (1 << 3),
  157. EDMA_ERR_DEV_CON = (1 << 4),
  158. EDMA_ERR_SERR = (1 << 5),
  159. EDMA_ERR_SELF_DIS = (1 << 7),
  160. EDMA_ERR_BIST_ASYNC = (1 << 8),
  161. EDMA_ERR_CRBQ_PAR = (1 << 9),
  162. EDMA_ERR_CRPB_PAR = (1 << 10),
  163. EDMA_ERR_INTRL_PAR = (1 << 11),
  164. EDMA_ERR_IORDY = (1 << 12),
  165. EDMA_ERR_LNK_CTRL_RX = (0xf << 13),
  166. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15),
  167. EDMA_ERR_LNK_DATA_RX = (0xf << 17),
  168. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21),
  169. EDMA_ERR_LNK_DATA_TX = (0x1f << 26),
  170. EDMA_ERR_TRANS_PROTO = (1 << 31),
  171. EDMA_ERR_FATAL = (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  172. EDMA_ERR_DEV_DCON | EDMA_ERR_CRBQ_PAR |
  173. EDMA_ERR_CRPB_PAR | EDMA_ERR_INTRL_PAR |
  174. EDMA_ERR_IORDY | EDMA_ERR_LNK_CTRL_RX_2 |
  175. EDMA_ERR_LNK_DATA_RX |
  176. EDMA_ERR_LNK_DATA_TX |
  177. EDMA_ERR_TRANS_PROTO),
  178. EDMA_REQ_Q_BASE_HI_OFS = 0x10,
  179. EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
  180. EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
  181. EDMA_REQ_Q_PTR_SHIFT = 5,
  182. EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
  183. EDMA_RSP_Q_IN_PTR_OFS = 0x20,
  184. EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
  185. EDMA_RSP_Q_PTR_SHIFT = 3,
  186. EDMA_CMD_OFS = 0x28,
  187. EDMA_EN = (1 << 0),
  188. EDMA_DS = (1 << 1),
  189. ATA_RST = (1 << 2),
  190. EDMA_IORDY_TMOUT = 0x34,
  191. EDMA_ARB_CFG = 0x38,
  192. /* Host private flags (hp_flags) */
  193. MV_HP_FLAG_MSI = (1 << 0),
  194. MV_HP_ERRATA_50XXB0 = (1 << 1),
  195. MV_HP_ERRATA_50XXB2 = (1 << 2),
  196. MV_HP_ERRATA_60X1B2 = (1 << 3),
  197. MV_HP_ERRATA_60X1C0 = (1 << 4),
  198. MV_HP_ERRATA_XX42A0 = (1 << 5),
  199. MV_HP_50XX = (1 << 6),
  200. MV_HP_GEN_IIE = (1 << 7),
  201. /* Port private flags (pp_flags) */
  202. MV_PP_FLAG_EDMA_EN = (1 << 0),
  203. MV_PP_FLAG_EDMA_DS_ACT = (1 << 1),
  204. };
  205. #define IS_50XX(hpriv) ((hpriv)->hp_flags & MV_HP_50XX)
  206. #define IS_60XX(hpriv) (((hpriv)->hp_flags & MV_HP_50XX) == 0)
  207. #define IS_GEN_I(hpriv) IS_50XX(hpriv)
  208. #define IS_GEN_II(hpriv) IS_60XX(hpriv)
  209. #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
  210. enum {
  211. /* Our DMA boundary is determined by an ePRD being unable to handle
  212. * anything larger than 64KB
  213. */
  214. MV_DMA_BOUNDARY = 0xffffU,
  215. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  216. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  217. };
  218. enum chip_type {
  219. chip_504x,
  220. chip_508x,
  221. chip_5080,
  222. chip_604x,
  223. chip_608x,
  224. chip_6042,
  225. chip_7042,
  226. };
  227. /* Command ReQuest Block: 32B */
  228. struct mv_crqb {
  229. u32 sg_addr;
  230. u32 sg_addr_hi;
  231. u16 ctrl_flags;
  232. u16 ata_cmd[11];
  233. };
  234. struct mv_crqb_iie {
  235. u32 addr;
  236. u32 addr_hi;
  237. u32 flags;
  238. u32 len;
  239. u32 ata_cmd[4];
  240. };
  241. /* Command ResPonse Block: 8B */
  242. struct mv_crpb {
  243. u16 id;
  244. u16 flags;
  245. u32 tmstmp;
  246. };
  247. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  248. struct mv_sg {
  249. u32 addr;
  250. u32 flags_size;
  251. u32 addr_hi;
  252. u32 reserved;
  253. };
  254. struct mv_port_priv {
  255. struct mv_crqb *crqb;
  256. dma_addr_t crqb_dma;
  257. struct mv_crpb *crpb;
  258. dma_addr_t crpb_dma;
  259. struct mv_sg *sg_tbl;
  260. dma_addr_t sg_tbl_dma;
  261. unsigned req_producer; /* cp of req_in_ptr */
  262. unsigned rsp_consumer; /* cp of rsp_out_ptr */
  263. u32 pp_flags;
  264. };
  265. struct mv_port_signal {
  266. u32 amps;
  267. u32 pre;
  268. };
  269. struct mv_host_priv;
  270. struct mv_hw_ops {
  271. void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
  272. unsigned int port);
  273. void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
  274. void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
  275. void __iomem *mmio);
  276. int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
  277. unsigned int n_hc);
  278. void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
  279. void (*reset_bus)(struct pci_dev *pdev, void __iomem *mmio);
  280. };
  281. struct mv_host_priv {
  282. u32 hp_flags;
  283. struct mv_port_signal signal[8];
  284. const struct mv_hw_ops *ops;
  285. };
  286. static void mv_irq_clear(struct ata_port *ap);
  287. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  288. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  289. static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  290. static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  291. static void mv_phy_reset(struct ata_port *ap);
  292. static void __mv_phy_reset(struct ata_port *ap, int can_sleep);
  293. static void mv_host_stop(struct ata_host_set *host_set);
  294. static int mv_port_start(struct ata_port *ap);
  295. static void mv_port_stop(struct ata_port *ap);
  296. static void mv_qc_prep(struct ata_queued_cmd *qc);
  297. static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
  298. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
  299. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  300. struct pt_regs *regs);
  301. static void mv_eng_timeout(struct ata_port *ap);
  302. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  303. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  304. unsigned int port);
  305. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  306. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  307. void __iomem *mmio);
  308. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  309. unsigned int n_hc);
  310. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  311. static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio);
  312. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  313. unsigned int port);
  314. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  315. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  316. void __iomem *mmio);
  317. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  318. unsigned int n_hc);
  319. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  320. static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio);
  321. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  322. unsigned int port_no);
  323. static void mv_stop_and_reset(struct ata_port *ap);
  324. static struct scsi_host_template mv_sht = {
  325. .module = THIS_MODULE,
  326. .name = DRV_NAME,
  327. .ioctl = ata_scsi_ioctl,
  328. .queuecommand = ata_scsi_queuecmd,
  329. .eh_strategy_handler = ata_scsi_error,
  330. .can_queue = MV_USE_Q_DEPTH,
  331. .this_id = ATA_SHT_THIS_ID,
  332. .sg_tablesize = MV_MAX_SG_CT / 2,
  333. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  334. .emulated = ATA_SHT_EMULATED,
  335. .use_clustering = ATA_SHT_USE_CLUSTERING,
  336. .proc_name = DRV_NAME,
  337. .dma_boundary = MV_DMA_BOUNDARY,
  338. .slave_configure = ata_scsi_slave_config,
  339. .bios_param = ata_std_bios_param,
  340. };
  341. static const struct ata_port_operations mv5_ops = {
  342. .port_disable = ata_port_disable,
  343. .tf_load = ata_tf_load,
  344. .tf_read = ata_tf_read,
  345. .check_status = ata_check_status,
  346. .exec_command = ata_exec_command,
  347. .dev_select = ata_std_dev_select,
  348. .phy_reset = mv_phy_reset,
  349. .qc_prep = mv_qc_prep,
  350. .qc_issue = mv_qc_issue,
  351. .eng_timeout = mv_eng_timeout,
  352. .irq_handler = mv_interrupt,
  353. .irq_clear = mv_irq_clear,
  354. .scr_read = mv5_scr_read,
  355. .scr_write = mv5_scr_write,
  356. .port_start = mv_port_start,
  357. .port_stop = mv_port_stop,
  358. .host_stop = mv_host_stop,
  359. };
  360. static const struct ata_port_operations mv6_ops = {
  361. .port_disable = ata_port_disable,
  362. .tf_load = ata_tf_load,
  363. .tf_read = ata_tf_read,
  364. .check_status = ata_check_status,
  365. .exec_command = ata_exec_command,
  366. .dev_select = ata_std_dev_select,
  367. .phy_reset = mv_phy_reset,
  368. .qc_prep = mv_qc_prep,
  369. .qc_issue = mv_qc_issue,
  370. .eng_timeout = mv_eng_timeout,
  371. .irq_handler = mv_interrupt,
  372. .irq_clear = mv_irq_clear,
  373. .scr_read = mv_scr_read,
  374. .scr_write = mv_scr_write,
  375. .port_start = mv_port_start,
  376. .port_stop = mv_port_stop,
  377. .host_stop = mv_host_stop,
  378. };
  379. static const struct ata_port_operations mv_iie_ops = {
  380. .port_disable = ata_port_disable,
  381. .tf_load = ata_tf_load,
  382. .tf_read = ata_tf_read,
  383. .check_status = ata_check_status,
  384. .exec_command = ata_exec_command,
  385. .dev_select = ata_std_dev_select,
  386. .phy_reset = mv_phy_reset,
  387. .qc_prep = mv_qc_prep_iie,
  388. .qc_issue = mv_qc_issue,
  389. .eng_timeout = mv_eng_timeout,
  390. .irq_handler = mv_interrupt,
  391. .irq_clear = mv_irq_clear,
  392. .scr_read = mv_scr_read,
  393. .scr_write = mv_scr_write,
  394. .port_start = mv_port_start,
  395. .port_stop = mv_port_stop,
  396. .host_stop = mv_host_stop,
  397. };
  398. static const struct ata_port_info mv_port_info[] = {
  399. { /* chip_504x */
  400. .sht = &mv_sht,
  401. .host_flags = MV_COMMON_FLAGS,
  402. .pio_mask = 0x1f, /* pio0-4 */
  403. .udma_mask = 0x7f, /* udma0-6 */
  404. .port_ops = &mv5_ops,
  405. },
  406. { /* chip_508x */
  407. .sht = &mv_sht,
  408. .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
  409. .pio_mask = 0x1f, /* pio0-4 */
  410. .udma_mask = 0x7f, /* udma0-6 */
  411. .port_ops = &mv5_ops,
  412. },
  413. { /* chip_5080 */
  414. .sht = &mv_sht,
  415. .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
  416. .pio_mask = 0x1f, /* pio0-4 */
  417. .udma_mask = 0x7f, /* udma0-6 */
  418. .port_ops = &mv5_ops,
  419. },
  420. { /* chip_604x */
  421. .sht = &mv_sht,
  422. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
  423. .pio_mask = 0x1f, /* pio0-4 */
  424. .udma_mask = 0x7f, /* udma0-6 */
  425. .port_ops = &mv6_ops,
  426. },
  427. { /* chip_608x */
  428. .sht = &mv_sht,
  429. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  430. MV_FLAG_DUAL_HC),
  431. .pio_mask = 0x1f, /* pio0-4 */
  432. .udma_mask = 0x7f, /* udma0-6 */
  433. .port_ops = &mv6_ops,
  434. },
  435. { /* chip_6042 */
  436. .sht = &mv_sht,
  437. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
  438. .pio_mask = 0x1f, /* pio0-4 */
  439. .udma_mask = 0x7f, /* udma0-6 */
  440. .port_ops = &mv_iie_ops,
  441. },
  442. { /* chip_7042 */
  443. .sht = &mv_sht,
  444. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  445. MV_FLAG_DUAL_HC),
  446. .pio_mask = 0x1f, /* pio0-4 */
  447. .udma_mask = 0x7f, /* udma0-6 */
  448. .port_ops = &mv_iie_ops,
  449. },
  450. };
  451. static const struct pci_device_id mv_pci_tbl[] = {
  452. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5040), 0, 0, chip_504x},
  453. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5041), 0, 0, chip_504x},
  454. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5080), 0, 0, chip_5080},
  455. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5081), 0, 0, chip_508x},
  456. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6040), 0, 0, chip_604x},
  457. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6041), 0, 0, chip_604x},
  458. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6042), 0, 0, chip_6042},
  459. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6080), 0, 0, chip_608x},
  460. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6081), 0, 0, chip_608x},
  461. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x0241), 0, 0, chip_604x},
  462. {} /* terminate list */
  463. };
  464. static struct pci_driver mv_pci_driver = {
  465. .name = DRV_NAME,
  466. .id_table = mv_pci_tbl,
  467. .probe = mv_init_one,
  468. .remove = ata_pci_remove_one,
  469. };
  470. static const struct mv_hw_ops mv5xxx_ops = {
  471. .phy_errata = mv5_phy_errata,
  472. .enable_leds = mv5_enable_leds,
  473. .read_preamp = mv5_read_preamp,
  474. .reset_hc = mv5_reset_hc,
  475. .reset_flash = mv5_reset_flash,
  476. .reset_bus = mv5_reset_bus,
  477. };
  478. static const struct mv_hw_ops mv6xxx_ops = {
  479. .phy_errata = mv6_phy_errata,
  480. .enable_leds = mv6_enable_leds,
  481. .read_preamp = mv6_read_preamp,
  482. .reset_hc = mv6_reset_hc,
  483. .reset_flash = mv6_reset_flash,
  484. .reset_bus = mv_reset_pci_bus,
  485. };
  486. /*
  487. * module options
  488. */
  489. static int msi; /* Use PCI msi; either zero (off, default) or non-zero */
  490. /*
  491. * Functions
  492. */
  493. static inline void writelfl(unsigned long data, void __iomem *addr)
  494. {
  495. writel(data, addr);
  496. (void) readl(addr); /* flush to avoid PCI posted write */
  497. }
  498. static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
  499. {
  500. return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
  501. }
  502. static inline unsigned int mv_hc_from_port(unsigned int port)
  503. {
  504. return port >> MV_PORT_HC_SHIFT;
  505. }
  506. static inline unsigned int mv_hardport_from_port(unsigned int port)
  507. {
  508. return port & MV_PORT_MASK;
  509. }
  510. static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
  511. unsigned int port)
  512. {
  513. return mv_hc_base(base, mv_hc_from_port(port));
  514. }
  515. static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
  516. {
  517. return mv_hc_base_from_port(base, port) +
  518. MV_SATAHC_ARBTR_REG_SZ +
  519. (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
  520. }
  521. static inline void __iomem *mv_ap_base(struct ata_port *ap)
  522. {
  523. return mv_port_base(ap->host_set->mmio_base, ap->port_no);
  524. }
  525. static inline int mv_get_hc_count(unsigned long host_flags)
  526. {
  527. return ((host_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
  528. }
  529. static void mv_irq_clear(struct ata_port *ap)
  530. {
  531. }
  532. /**
  533. * mv_start_dma - Enable eDMA engine
  534. * @base: port base address
  535. * @pp: port private data
  536. *
  537. * Verify the local cache of the eDMA state is accurate with a
  538. * WARN_ON.
  539. *
  540. * LOCKING:
  541. * Inherited from caller.
  542. */
  543. static void mv_start_dma(void __iomem *base, struct mv_port_priv *pp)
  544. {
  545. if (!(MV_PP_FLAG_EDMA_EN & pp->pp_flags)) {
  546. writelfl(EDMA_EN, base + EDMA_CMD_OFS);
  547. pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
  548. }
  549. WARN_ON(!(EDMA_EN & readl(base + EDMA_CMD_OFS)));
  550. }
  551. /**
  552. * mv_stop_dma - Disable eDMA engine
  553. * @ap: ATA channel to manipulate
  554. *
  555. * Verify the local cache of the eDMA state is accurate with a
  556. * WARN_ON.
  557. *
  558. * LOCKING:
  559. * Inherited from caller.
  560. */
  561. static void mv_stop_dma(struct ata_port *ap)
  562. {
  563. void __iomem *port_mmio = mv_ap_base(ap);
  564. struct mv_port_priv *pp = ap->private_data;
  565. u32 reg;
  566. int i;
  567. if (MV_PP_FLAG_EDMA_EN & pp->pp_flags) {
  568. /* Disable EDMA if active. The disable bit auto clears.
  569. */
  570. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  571. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  572. } else {
  573. WARN_ON(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS));
  574. }
  575. /* now properly wait for the eDMA to stop */
  576. for (i = 1000; i > 0; i--) {
  577. reg = readl(port_mmio + EDMA_CMD_OFS);
  578. if (!(EDMA_EN & reg)) {
  579. break;
  580. }
  581. udelay(100);
  582. }
  583. if (EDMA_EN & reg) {
  584. printk(KERN_ERR "ata%u: Unable to stop eDMA\n", ap->id);
  585. /* FIXME: Consider doing a reset here to recover */
  586. }
  587. }
  588. #ifdef ATA_DEBUG
  589. static void mv_dump_mem(void __iomem *start, unsigned bytes)
  590. {
  591. int b, w;
  592. for (b = 0; b < bytes; ) {
  593. DPRINTK("%p: ", start + b);
  594. for (w = 0; b < bytes && w < 4; w++) {
  595. printk("%08x ",readl(start + b));
  596. b += sizeof(u32);
  597. }
  598. printk("\n");
  599. }
  600. }
  601. #endif
  602. static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
  603. {
  604. #ifdef ATA_DEBUG
  605. int b, w;
  606. u32 dw;
  607. for (b = 0; b < bytes; ) {
  608. DPRINTK("%02x: ", b);
  609. for (w = 0; b < bytes && w < 4; w++) {
  610. (void) pci_read_config_dword(pdev,b,&dw);
  611. printk("%08x ",dw);
  612. b += sizeof(u32);
  613. }
  614. printk("\n");
  615. }
  616. #endif
  617. }
  618. static void mv_dump_all_regs(void __iomem *mmio_base, int port,
  619. struct pci_dev *pdev)
  620. {
  621. #ifdef ATA_DEBUG
  622. void __iomem *hc_base = mv_hc_base(mmio_base,
  623. port >> MV_PORT_HC_SHIFT);
  624. void __iomem *port_base;
  625. int start_port, num_ports, p, start_hc, num_hcs, hc;
  626. if (0 > port) {
  627. start_hc = start_port = 0;
  628. num_ports = 8; /* shld be benign for 4 port devs */
  629. num_hcs = 2;
  630. } else {
  631. start_hc = port >> MV_PORT_HC_SHIFT;
  632. start_port = port;
  633. num_ports = num_hcs = 1;
  634. }
  635. DPRINTK("All registers for port(s) %u-%u:\n", start_port,
  636. num_ports > 1 ? num_ports - 1 : start_port);
  637. if (NULL != pdev) {
  638. DPRINTK("PCI config space regs:\n");
  639. mv_dump_pci_cfg(pdev, 0x68);
  640. }
  641. DPRINTK("PCI regs:\n");
  642. mv_dump_mem(mmio_base+0xc00, 0x3c);
  643. mv_dump_mem(mmio_base+0xd00, 0x34);
  644. mv_dump_mem(mmio_base+0xf00, 0x4);
  645. mv_dump_mem(mmio_base+0x1d00, 0x6c);
  646. for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
  647. hc_base = mv_hc_base(mmio_base, port >> MV_PORT_HC_SHIFT);
  648. DPRINTK("HC regs (HC %i):\n", hc);
  649. mv_dump_mem(hc_base, 0x1c);
  650. }
  651. for (p = start_port; p < start_port + num_ports; p++) {
  652. port_base = mv_port_base(mmio_base, p);
  653. DPRINTK("EDMA regs (port %i):\n",p);
  654. mv_dump_mem(port_base, 0x54);
  655. DPRINTK("SATA regs (port %i):\n",p);
  656. mv_dump_mem(port_base+0x300, 0x60);
  657. }
  658. #endif
  659. }
  660. static unsigned int mv_scr_offset(unsigned int sc_reg_in)
  661. {
  662. unsigned int ofs;
  663. switch (sc_reg_in) {
  664. case SCR_STATUS:
  665. case SCR_CONTROL:
  666. case SCR_ERROR:
  667. ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
  668. break;
  669. case SCR_ACTIVE:
  670. ofs = SATA_ACTIVE_OFS; /* active is not with the others */
  671. break;
  672. default:
  673. ofs = 0xffffffffU;
  674. break;
  675. }
  676. return ofs;
  677. }
  678. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  679. {
  680. unsigned int ofs = mv_scr_offset(sc_reg_in);
  681. if (0xffffffffU != ofs) {
  682. return readl(mv_ap_base(ap) + ofs);
  683. } else {
  684. return (u32) ofs;
  685. }
  686. }
  687. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  688. {
  689. unsigned int ofs = mv_scr_offset(sc_reg_in);
  690. if (0xffffffffU != ofs) {
  691. writelfl(val, mv_ap_base(ap) + ofs);
  692. }
  693. }
  694. /**
  695. * mv_host_stop - Host specific cleanup/stop routine.
  696. * @host_set: host data structure
  697. *
  698. * Disable ints, cleanup host memory, call general purpose
  699. * host_stop.
  700. *
  701. * LOCKING:
  702. * Inherited from caller.
  703. */
  704. static void mv_host_stop(struct ata_host_set *host_set)
  705. {
  706. struct mv_host_priv *hpriv = host_set->private_data;
  707. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  708. if (hpriv->hp_flags & MV_HP_FLAG_MSI) {
  709. pci_disable_msi(pdev);
  710. } else {
  711. pci_intx(pdev, 0);
  712. }
  713. kfree(hpriv);
  714. ata_host_stop(host_set);
  715. }
  716. static inline void mv_priv_free(struct mv_port_priv *pp, struct device *dev)
  717. {
  718. dma_free_coherent(dev, MV_PORT_PRIV_DMA_SZ, pp->crpb, pp->crpb_dma);
  719. }
  720. static void mv_edma_cfg(struct mv_host_priv *hpriv, void __iomem *port_mmio)
  721. {
  722. u32 cfg = readl(port_mmio + EDMA_CFG_OFS);
  723. /* set up non-NCQ EDMA configuration */
  724. cfg &= ~0x1f; /* clear queue depth */
  725. cfg &= ~EDMA_CFG_NCQ; /* clear NCQ mode */
  726. cfg &= ~(1 << 9); /* disable equeue */
  727. if (IS_GEN_I(hpriv))
  728. cfg |= (1 << 8); /* enab config burst size mask */
  729. else if (IS_GEN_II(hpriv))
  730. cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
  731. else if (IS_GEN_IIE(hpriv)) {
  732. cfg |= (1 << 23); /* dis RX PM port mask */
  733. cfg &= ~(1 << 16); /* dis FIS-based switching (for now) */
  734. cfg &= ~(1 << 19); /* dis 128-entry queue (for now?) */
  735. cfg |= (1 << 18); /* enab early completion */
  736. cfg |= (1 << 17); /* enab host q cache */
  737. cfg |= (1 << 22); /* enab cutthrough */
  738. }
  739. writelfl(cfg, port_mmio + EDMA_CFG_OFS);
  740. }
  741. /**
  742. * mv_port_start - Port specific init/start routine.
  743. * @ap: ATA channel to manipulate
  744. *
  745. * Allocate and point to DMA memory, init port private memory,
  746. * zero indices.
  747. *
  748. * LOCKING:
  749. * Inherited from caller.
  750. */
  751. static int mv_port_start(struct ata_port *ap)
  752. {
  753. struct device *dev = ap->host_set->dev;
  754. struct mv_host_priv *hpriv = ap->host_set->private_data;
  755. struct mv_port_priv *pp;
  756. void __iomem *port_mmio = mv_ap_base(ap);
  757. void *mem;
  758. dma_addr_t mem_dma;
  759. int rc = -ENOMEM;
  760. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  761. if (!pp)
  762. goto err_out;
  763. memset(pp, 0, sizeof(*pp));
  764. mem = dma_alloc_coherent(dev, MV_PORT_PRIV_DMA_SZ, &mem_dma,
  765. GFP_KERNEL);
  766. if (!mem)
  767. goto err_out_pp;
  768. memset(mem, 0, MV_PORT_PRIV_DMA_SZ);
  769. rc = ata_pad_alloc(ap, dev);
  770. if (rc)
  771. goto err_out_priv;
  772. /* First item in chunk of DMA memory:
  773. * 32-slot command request table (CRQB), 32 bytes each in size
  774. */
  775. pp->crqb = mem;
  776. pp->crqb_dma = mem_dma;
  777. mem += MV_CRQB_Q_SZ;
  778. mem_dma += MV_CRQB_Q_SZ;
  779. /* Second item:
  780. * 32-slot command response table (CRPB), 8 bytes each in size
  781. */
  782. pp->crpb = mem;
  783. pp->crpb_dma = mem_dma;
  784. mem += MV_CRPB_Q_SZ;
  785. mem_dma += MV_CRPB_Q_SZ;
  786. /* Third item:
  787. * Table of scatter-gather descriptors (ePRD), 16 bytes each
  788. */
  789. pp->sg_tbl = mem;
  790. pp->sg_tbl_dma = mem_dma;
  791. mv_edma_cfg(hpriv, port_mmio);
  792. writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
  793. writelfl(pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK,
  794. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  795. if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
  796. writelfl(pp->crqb_dma & 0xffffffff,
  797. port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  798. else
  799. writelfl(0, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  800. writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
  801. if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
  802. writelfl(pp->crpb_dma & 0xffffffff,
  803. port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  804. else
  805. writelfl(0, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  806. writelfl(pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK,
  807. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  808. pp->req_producer = pp->rsp_consumer = 0;
  809. /* Don't turn on EDMA here...do it before DMA commands only. Else
  810. * we'll be unable to send non-data, PIO, etc due to restricted access
  811. * to shadow regs.
  812. */
  813. ap->private_data = pp;
  814. return 0;
  815. err_out_priv:
  816. mv_priv_free(pp, dev);
  817. err_out_pp:
  818. kfree(pp);
  819. err_out:
  820. return rc;
  821. }
  822. /**
  823. * mv_port_stop - Port specific cleanup/stop routine.
  824. * @ap: ATA channel to manipulate
  825. *
  826. * Stop DMA, cleanup port memory.
  827. *
  828. * LOCKING:
  829. * This routine uses the host_set lock to protect the DMA stop.
  830. */
  831. static void mv_port_stop(struct ata_port *ap)
  832. {
  833. struct device *dev = ap->host_set->dev;
  834. struct mv_port_priv *pp = ap->private_data;
  835. unsigned long flags;
  836. spin_lock_irqsave(&ap->host_set->lock, flags);
  837. mv_stop_dma(ap);
  838. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  839. ap->private_data = NULL;
  840. ata_pad_free(ap, dev);
  841. mv_priv_free(pp, dev);
  842. kfree(pp);
  843. }
  844. /**
  845. * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
  846. * @qc: queued command whose SG list to source from
  847. *
  848. * Populate the SG list and mark the last entry.
  849. *
  850. * LOCKING:
  851. * Inherited from caller.
  852. */
  853. static void mv_fill_sg(struct ata_queued_cmd *qc)
  854. {
  855. struct mv_port_priv *pp = qc->ap->private_data;
  856. unsigned int i = 0;
  857. struct scatterlist *sg;
  858. ata_for_each_sg(sg, qc) {
  859. dma_addr_t addr;
  860. u32 sg_len, len, offset;
  861. addr = sg_dma_address(sg);
  862. sg_len = sg_dma_len(sg);
  863. while (sg_len) {
  864. offset = addr & MV_DMA_BOUNDARY;
  865. len = sg_len;
  866. if ((offset + sg_len) > 0x10000)
  867. len = 0x10000 - offset;
  868. pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
  869. pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  870. pp->sg_tbl[i].flags_size = cpu_to_le32(len);
  871. sg_len -= len;
  872. addr += len;
  873. if (!sg_len && ata_sg_is_last(sg, qc))
  874. pp->sg_tbl[i].flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
  875. i++;
  876. }
  877. }
  878. }
  879. static inline unsigned mv_inc_q_index(unsigned *index)
  880. {
  881. *index = (*index + 1) & MV_MAX_Q_DEPTH_MASK;
  882. return *index;
  883. }
  884. static inline void mv_crqb_pack_cmd(u16 *cmdw, u8 data, u8 addr, unsigned last)
  885. {
  886. *cmdw = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  887. (last ? CRQB_CMD_LAST : 0);
  888. }
  889. /**
  890. * mv_qc_prep - Host specific command preparation.
  891. * @qc: queued command to prepare
  892. *
  893. * This routine simply redirects to the general purpose routine
  894. * if command is not DMA. Else, it handles prep of the CRQB
  895. * (command request block), does some sanity checking, and calls
  896. * the SG load routine.
  897. *
  898. * LOCKING:
  899. * Inherited from caller.
  900. */
  901. static void mv_qc_prep(struct ata_queued_cmd *qc)
  902. {
  903. struct ata_port *ap = qc->ap;
  904. struct mv_port_priv *pp = ap->private_data;
  905. u16 *cw;
  906. struct ata_taskfile *tf;
  907. u16 flags = 0;
  908. if (ATA_PROT_DMA != qc->tf.protocol)
  909. return;
  910. /* the req producer index should be the same as we remember it */
  911. WARN_ON(((readl(mv_ap_base(qc->ap) + EDMA_REQ_Q_IN_PTR_OFS) >>
  912. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) !=
  913. pp->req_producer);
  914. /* Fill in command request block
  915. */
  916. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  917. flags |= CRQB_FLAG_READ;
  918. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  919. flags |= qc->tag << CRQB_TAG_SHIFT;
  920. pp->crqb[pp->req_producer].sg_addr =
  921. cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
  922. pp->crqb[pp->req_producer].sg_addr_hi =
  923. cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
  924. pp->crqb[pp->req_producer].ctrl_flags = cpu_to_le16(flags);
  925. cw = &pp->crqb[pp->req_producer].ata_cmd[0];
  926. tf = &qc->tf;
  927. /* Sadly, the CRQB cannot accomodate all registers--there are
  928. * only 11 bytes...so we must pick and choose required
  929. * registers based on the command. So, we drop feature and
  930. * hob_feature for [RW] DMA commands, but they are needed for
  931. * NCQ. NCQ will drop hob_nsect.
  932. */
  933. switch (tf->command) {
  934. case ATA_CMD_READ:
  935. case ATA_CMD_READ_EXT:
  936. case ATA_CMD_WRITE:
  937. case ATA_CMD_WRITE_EXT:
  938. case ATA_CMD_WRITE_FUA_EXT:
  939. mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
  940. break;
  941. #ifdef LIBATA_NCQ /* FIXME: remove this line when NCQ added */
  942. case ATA_CMD_FPDMA_READ:
  943. case ATA_CMD_FPDMA_WRITE:
  944. mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
  945. mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
  946. break;
  947. #endif /* FIXME: remove this line when NCQ added */
  948. default:
  949. /* The only other commands EDMA supports in non-queued and
  950. * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
  951. * of which are defined/used by Linux. If we get here, this
  952. * driver needs work.
  953. *
  954. * FIXME: modify libata to give qc_prep a return value and
  955. * return error here.
  956. */
  957. BUG_ON(tf->command);
  958. break;
  959. }
  960. mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
  961. mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
  962. mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
  963. mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
  964. mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
  965. mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
  966. mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
  967. mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
  968. mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
  969. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  970. return;
  971. mv_fill_sg(qc);
  972. }
  973. /**
  974. * mv_qc_prep_iie - Host specific command preparation.
  975. * @qc: queued command to prepare
  976. *
  977. * This routine simply redirects to the general purpose routine
  978. * if command is not DMA. Else, it handles prep of the CRQB
  979. * (command request block), does some sanity checking, and calls
  980. * the SG load routine.
  981. *
  982. * LOCKING:
  983. * Inherited from caller.
  984. */
  985. static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
  986. {
  987. struct ata_port *ap = qc->ap;
  988. struct mv_port_priv *pp = ap->private_data;
  989. struct mv_crqb_iie *crqb;
  990. struct ata_taskfile *tf;
  991. u32 flags = 0;
  992. if (ATA_PROT_DMA != qc->tf.protocol)
  993. return;
  994. /* the req producer index should be the same as we remember it */
  995. WARN_ON(((readl(mv_ap_base(qc->ap) + EDMA_REQ_Q_IN_PTR_OFS) >>
  996. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) !=
  997. pp->req_producer);
  998. /* Fill in Gen IIE command request block
  999. */
  1000. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  1001. flags |= CRQB_FLAG_READ;
  1002. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  1003. flags |= qc->tag << CRQB_TAG_SHIFT;
  1004. crqb = (struct mv_crqb_iie *) &pp->crqb[pp->req_producer];
  1005. crqb->addr = cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
  1006. crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
  1007. crqb->flags = cpu_to_le32(flags);
  1008. tf = &qc->tf;
  1009. crqb->ata_cmd[0] = cpu_to_le32(
  1010. (tf->command << 16) |
  1011. (tf->feature << 24)
  1012. );
  1013. crqb->ata_cmd[1] = cpu_to_le32(
  1014. (tf->lbal << 0) |
  1015. (tf->lbam << 8) |
  1016. (tf->lbah << 16) |
  1017. (tf->device << 24)
  1018. );
  1019. crqb->ata_cmd[2] = cpu_to_le32(
  1020. (tf->hob_lbal << 0) |
  1021. (tf->hob_lbam << 8) |
  1022. (tf->hob_lbah << 16) |
  1023. (tf->hob_feature << 24)
  1024. );
  1025. crqb->ata_cmd[3] = cpu_to_le32(
  1026. (tf->nsect << 0) |
  1027. (tf->hob_nsect << 8)
  1028. );
  1029. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1030. return;
  1031. mv_fill_sg(qc);
  1032. }
  1033. /**
  1034. * mv_qc_issue - Initiate a command to the host
  1035. * @qc: queued command to start
  1036. *
  1037. * This routine simply redirects to the general purpose routine
  1038. * if command is not DMA. Else, it sanity checks our local
  1039. * caches of the request producer/consumer indices then enables
  1040. * DMA and bumps the request producer index.
  1041. *
  1042. * LOCKING:
  1043. * Inherited from caller.
  1044. */
  1045. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
  1046. {
  1047. void __iomem *port_mmio = mv_ap_base(qc->ap);
  1048. struct mv_port_priv *pp = qc->ap->private_data;
  1049. u32 in_ptr;
  1050. if (ATA_PROT_DMA != qc->tf.protocol) {
  1051. /* We're about to send a non-EDMA capable command to the
  1052. * port. Turn off EDMA so there won't be problems accessing
  1053. * shadow block, etc registers.
  1054. */
  1055. mv_stop_dma(qc->ap);
  1056. return ata_qc_issue_prot(qc);
  1057. }
  1058. in_ptr = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  1059. /* the req producer index should be the same as we remember it */
  1060. WARN_ON(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) !=
  1061. pp->req_producer);
  1062. /* until we do queuing, the queue should be empty at this point */
  1063. WARN_ON(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) !=
  1064. ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS) >>
  1065. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
  1066. mv_inc_q_index(&pp->req_producer); /* now incr producer index */
  1067. mv_start_dma(port_mmio, pp);
  1068. /* and write the request in pointer to kick the EDMA to life */
  1069. in_ptr &= EDMA_REQ_Q_BASE_LO_MASK;
  1070. in_ptr |= pp->req_producer << EDMA_REQ_Q_PTR_SHIFT;
  1071. writelfl(in_ptr, port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  1072. return 0;
  1073. }
  1074. /**
  1075. * mv_get_crpb_status - get status from most recently completed cmd
  1076. * @ap: ATA channel to manipulate
  1077. *
  1078. * This routine is for use when the port is in DMA mode, when it
  1079. * will be using the CRPB (command response block) method of
  1080. * returning command completion information. We check indices
  1081. * are good, grab status, and bump the response consumer index to
  1082. * prove that we're up to date.
  1083. *
  1084. * LOCKING:
  1085. * Inherited from caller.
  1086. */
  1087. static u8 mv_get_crpb_status(struct ata_port *ap)
  1088. {
  1089. void __iomem *port_mmio = mv_ap_base(ap);
  1090. struct mv_port_priv *pp = ap->private_data;
  1091. u32 out_ptr;
  1092. out_ptr = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  1093. /* the response consumer index should be the same as we remember it */
  1094. WARN_ON(((out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) !=
  1095. pp->rsp_consumer);
  1096. /* increment our consumer index... */
  1097. pp->rsp_consumer = mv_inc_q_index(&pp->rsp_consumer);
  1098. /* and, until we do NCQ, there should only be 1 CRPB waiting */
  1099. WARN_ON(((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS) >>
  1100. EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) !=
  1101. pp->rsp_consumer);
  1102. /* write out our inc'd consumer index so EDMA knows we're caught up */
  1103. out_ptr &= EDMA_RSP_Q_BASE_LO_MASK;
  1104. out_ptr |= pp->rsp_consumer << EDMA_RSP_Q_PTR_SHIFT;
  1105. writelfl(out_ptr, port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  1106. /* Return ATA status register for completed CRPB */
  1107. return (pp->crpb[pp->rsp_consumer].flags >> CRPB_FLAG_STATUS_SHIFT);
  1108. }
  1109. /**
  1110. * mv_err_intr - Handle error interrupts on the port
  1111. * @ap: ATA channel to manipulate
  1112. *
  1113. * In most cases, just clear the interrupt and move on. However,
  1114. * some cases require an eDMA reset, which is done right before
  1115. * the COMRESET in mv_phy_reset(). The SERR case requires a
  1116. * clear of pending errors in the SATA SERROR register. Finally,
  1117. * if the port disabled DMA, update our cached copy to match.
  1118. *
  1119. * LOCKING:
  1120. * Inherited from caller.
  1121. */
  1122. static void mv_err_intr(struct ata_port *ap)
  1123. {
  1124. void __iomem *port_mmio = mv_ap_base(ap);
  1125. u32 edma_err_cause, serr = 0;
  1126. edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1127. if (EDMA_ERR_SERR & edma_err_cause) {
  1128. serr = scr_read(ap, SCR_ERROR);
  1129. scr_write_flush(ap, SCR_ERROR, serr);
  1130. }
  1131. if (EDMA_ERR_SELF_DIS & edma_err_cause) {
  1132. struct mv_port_priv *pp = ap->private_data;
  1133. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1134. }
  1135. DPRINTK(KERN_ERR "ata%u: port error; EDMA err cause: 0x%08x "
  1136. "SERR: 0x%08x\n", ap->id, edma_err_cause, serr);
  1137. /* Clear EDMA now that SERR cleanup done */
  1138. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1139. /* check for fatal here and recover if needed */
  1140. if (EDMA_ERR_FATAL & edma_err_cause) {
  1141. mv_stop_and_reset(ap);
  1142. }
  1143. }
  1144. /**
  1145. * mv_host_intr - Handle all interrupts on the given host controller
  1146. * @host_set: host specific structure
  1147. * @relevant: port error bits relevant to this host controller
  1148. * @hc: which host controller we're to look at
  1149. *
  1150. * Read then write clear the HC interrupt status then walk each
  1151. * port connected to the HC and see if it needs servicing. Port
  1152. * success ints are reported in the HC interrupt status reg, the
  1153. * port error ints are reported in the higher level main
  1154. * interrupt status register and thus are passed in via the
  1155. * 'relevant' argument.
  1156. *
  1157. * LOCKING:
  1158. * Inherited from caller.
  1159. */
  1160. static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
  1161. unsigned int hc)
  1162. {
  1163. void __iomem *mmio = host_set->mmio_base;
  1164. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1165. struct ata_port *ap;
  1166. struct ata_queued_cmd *qc;
  1167. u32 hc_irq_cause;
  1168. int shift, port, port0, hard_port, handled;
  1169. unsigned int err_mask;
  1170. if (hc == 0) {
  1171. port0 = 0;
  1172. } else {
  1173. port0 = MV_PORTS_PER_HC;
  1174. }
  1175. /* we'll need the HC success int register in most cases */
  1176. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  1177. if (hc_irq_cause) {
  1178. writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  1179. }
  1180. VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
  1181. hc,relevant,hc_irq_cause);
  1182. for (port = port0; port < port0 + MV_PORTS_PER_HC; port++) {
  1183. u8 ata_status = 0;
  1184. ap = host_set->ports[port];
  1185. hard_port = port & MV_PORT_MASK; /* range 0-3 */
  1186. handled = 0; /* ensure ata_status is set if handled++ */
  1187. if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause) {
  1188. /* new CRPB on the queue; just one at a time until NCQ
  1189. */
  1190. ata_status = mv_get_crpb_status(ap);
  1191. handled++;
  1192. } else if ((DEV_IRQ << hard_port) & hc_irq_cause) {
  1193. /* received ATA IRQ; read the status reg to clear INTRQ
  1194. */
  1195. ata_status = readb((void __iomem *)
  1196. ap->ioaddr.status_addr);
  1197. handled++;
  1198. }
  1199. if (ap &&
  1200. (ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR)))
  1201. continue;
  1202. err_mask = ac_err_mask(ata_status);
  1203. shift = port << 1; /* (port * 2) */
  1204. if (port >= MV_PORTS_PER_HC) {
  1205. shift++; /* skip bit 8 in the HC Main IRQ reg */
  1206. }
  1207. if ((PORT0_ERR << shift) & relevant) {
  1208. mv_err_intr(ap);
  1209. err_mask |= AC_ERR_OTHER;
  1210. handled++;
  1211. }
  1212. if (handled && ap) {
  1213. qc = ata_qc_from_tag(ap, ap->active_tag);
  1214. if (NULL != qc) {
  1215. VPRINTK("port %u IRQ found for qc, "
  1216. "ata_status 0x%x\n", port,ata_status);
  1217. /* mark qc status appropriately */
  1218. if (!(qc->tf.ctl & ATA_NIEN)) {
  1219. qc->err_mask |= err_mask;
  1220. ata_qc_complete(qc);
  1221. }
  1222. }
  1223. }
  1224. }
  1225. VPRINTK("EXIT\n");
  1226. }
  1227. /**
  1228. * mv_interrupt -
  1229. * @irq: unused
  1230. * @dev_instance: private data; in this case the host structure
  1231. * @regs: unused
  1232. *
  1233. * Read the read only register to determine if any host
  1234. * controllers have pending interrupts. If so, call lower level
  1235. * routine to handle. Also check for PCI errors which are only
  1236. * reported here.
  1237. *
  1238. * LOCKING:
  1239. * This routine holds the host_set lock while processing pending
  1240. * interrupts.
  1241. */
  1242. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  1243. struct pt_regs *regs)
  1244. {
  1245. struct ata_host_set *host_set = dev_instance;
  1246. unsigned int hc, handled = 0, n_hcs;
  1247. void __iomem *mmio = host_set->mmio_base;
  1248. u32 irq_stat;
  1249. irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
  1250. /* check the cases where we either have nothing pending or have read
  1251. * a bogus register value which can indicate HW removal or PCI fault
  1252. */
  1253. if (!irq_stat || (0xffffffffU == irq_stat)) {
  1254. return IRQ_NONE;
  1255. }
  1256. n_hcs = mv_get_hc_count(host_set->ports[0]->flags);
  1257. spin_lock(&host_set->lock);
  1258. for (hc = 0; hc < n_hcs; hc++) {
  1259. u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
  1260. if (relevant) {
  1261. mv_host_intr(host_set, relevant, hc);
  1262. handled++;
  1263. }
  1264. }
  1265. if (PCI_ERR & irq_stat) {
  1266. printk(KERN_ERR DRV_NAME ": PCI ERROR; PCI IRQ cause=0x%08x\n",
  1267. readl(mmio + PCI_IRQ_CAUSE_OFS));
  1268. DPRINTK("All regs @ PCI error\n");
  1269. mv_dump_all_regs(mmio, -1, to_pci_dev(host_set->dev));
  1270. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1271. handled++;
  1272. }
  1273. spin_unlock(&host_set->lock);
  1274. return IRQ_RETVAL(handled);
  1275. }
  1276. static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
  1277. {
  1278. void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
  1279. unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
  1280. return hc_mmio + ofs;
  1281. }
  1282. static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
  1283. {
  1284. unsigned int ofs;
  1285. switch (sc_reg_in) {
  1286. case SCR_STATUS:
  1287. case SCR_ERROR:
  1288. case SCR_CONTROL:
  1289. ofs = sc_reg_in * sizeof(u32);
  1290. break;
  1291. default:
  1292. ofs = 0xffffffffU;
  1293. break;
  1294. }
  1295. return ofs;
  1296. }
  1297. static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  1298. {
  1299. void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
  1300. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1301. if (ofs != 0xffffffffU)
  1302. return readl(mmio + ofs);
  1303. else
  1304. return (u32) ofs;
  1305. }
  1306. static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  1307. {
  1308. void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
  1309. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1310. if (ofs != 0xffffffffU)
  1311. writelfl(val, mmio + ofs);
  1312. }
  1313. static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio)
  1314. {
  1315. u8 rev_id;
  1316. int early_5080;
  1317. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1318. early_5080 = (pdev->device == 0x5080) && (rev_id == 0);
  1319. if (!early_5080) {
  1320. u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1321. tmp |= (1 << 0);
  1322. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1323. }
  1324. mv_reset_pci_bus(pdev, mmio);
  1325. }
  1326. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1327. {
  1328. writel(0x0fcfffff, mmio + MV_FLASH_CTL);
  1329. }
  1330. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  1331. void __iomem *mmio)
  1332. {
  1333. void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
  1334. u32 tmp;
  1335. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1336. hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
  1337. hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
  1338. }
  1339. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1340. {
  1341. u32 tmp;
  1342. writel(0, mmio + MV_GPIO_PORT_CTL);
  1343. /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
  1344. tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1345. tmp |= ~(1 << 0);
  1346. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1347. }
  1348. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1349. unsigned int port)
  1350. {
  1351. void __iomem *phy_mmio = mv5_phy_base(mmio, port);
  1352. const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
  1353. u32 tmp;
  1354. int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
  1355. if (fix_apm_sq) {
  1356. tmp = readl(phy_mmio + MV5_LT_MODE);
  1357. tmp |= (1 << 19);
  1358. writel(tmp, phy_mmio + MV5_LT_MODE);
  1359. tmp = readl(phy_mmio + MV5_PHY_CTL);
  1360. tmp &= ~0x3;
  1361. tmp |= 0x1;
  1362. writel(tmp, phy_mmio + MV5_PHY_CTL);
  1363. }
  1364. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1365. tmp &= ~mask;
  1366. tmp |= hpriv->signal[port].pre;
  1367. tmp |= hpriv->signal[port].amps;
  1368. writel(tmp, phy_mmio + MV5_PHY_MODE);
  1369. }
  1370. #undef ZERO
  1371. #define ZERO(reg) writel(0, port_mmio + (reg))
  1372. static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
  1373. unsigned int port)
  1374. {
  1375. void __iomem *port_mmio = mv_port_base(mmio, port);
  1376. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  1377. mv_channel_reset(hpriv, mmio, port);
  1378. ZERO(0x028); /* command */
  1379. writel(0x11f, port_mmio + EDMA_CFG_OFS);
  1380. ZERO(0x004); /* timer */
  1381. ZERO(0x008); /* irq err cause */
  1382. ZERO(0x00c); /* irq err mask */
  1383. ZERO(0x010); /* rq bah */
  1384. ZERO(0x014); /* rq inp */
  1385. ZERO(0x018); /* rq outp */
  1386. ZERO(0x01c); /* respq bah */
  1387. ZERO(0x024); /* respq outp */
  1388. ZERO(0x020); /* respq inp */
  1389. ZERO(0x02c); /* test control */
  1390. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
  1391. }
  1392. #undef ZERO
  1393. #define ZERO(reg) writel(0, hc_mmio + (reg))
  1394. static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1395. unsigned int hc)
  1396. {
  1397. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1398. u32 tmp;
  1399. ZERO(0x00c);
  1400. ZERO(0x010);
  1401. ZERO(0x014);
  1402. ZERO(0x018);
  1403. tmp = readl(hc_mmio + 0x20);
  1404. tmp &= 0x1c1c1c1c;
  1405. tmp |= 0x03030303;
  1406. writel(tmp, hc_mmio + 0x20);
  1407. }
  1408. #undef ZERO
  1409. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1410. unsigned int n_hc)
  1411. {
  1412. unsigned int hc, port;
  1413. for (hc = 0; hc < n_hc; hc++) {
  1414. for (port = 0; port < MV_PORTS_PER_HC; port++)
  1415. mv5_reset_hc_port(hpriv, mmio,
  1416. (hc * MV_PORTS_PER_HC) + port);
  1417. mv5_reset_one_hc(hpriv, mmio, hc);
  1418. }
  1419. return 0;
  1420. }
  1421. #undef ZERO
  1422. #define ZERO(reg) writel(0, mmio + (reg))
  1423. static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio)
  1424. {
  1425. u32 tmp;
  1426. tmp = readl(mmio + MV_PCI_MODE);
  1427. tmp &= 0xff00ffff;
  1428. writel(tmp, mmio + MV_PCI_MODE);
  1429. ZERO(MV_PCI_DISC_TIMER);
  1430. ZERO(MV_PCI_MSI_TRIGGER);
  1431. writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
  1432. ZERO(HC_MAIN_IRQ_MASK_OFS);
  1433. ZERO(MV_PCI_SERR_MASK);
  1434. ZERO(PCI_IRQ_CAUSE_OFS);
  1435. ZERO(PCI_IRQ_MASK_OFS);
  1436. ZERO(MV_PCI_ERR_LOW_ADDRESS);
  1437. ZERO(MV_PCI_ERR_HIGH_ADDRESS);
  1438. ZERO(MV_PCI_ERR_ATTRIBUTE);
  1439. ZERO(MV_PCI_ERR_COMMAND);
  1440. }
  1441. #undef ZERO
  1442. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1443. {
  1444. u32 tmp;
  1445. mv5_reset_flash(hpriv, mmio);
  1446. tmp = readl(mmio + MV_GPIO_PORT_CTL);
  1447. tmp &= 0x3;
  1448. tmp |= (1 << 5) | (1 << 6);
  1449. writel(tmp, mmio + MV_GPIO_PORT_CTL);
  1450. }
  1451. /**
  1452. * mv6_reset_hc - Perform the 6xxx global soft reset
  1453. * @mmio: base address of the HBA
  1454. *
  1455. * This routine only applies to 6xxx parts.
  1456. *
  1457. * LOCKING:
  1458. * Inherited from caller.
  1459. */
  1460. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1461. unsigned int n_hc)
  1462. {
  1463. void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
  1464. int i, rc = 0;
  1465. u32 t;
  1466. /* Following procedure defined in PCI "main command and status
  1467. * register" table.
  1468. */
  1469. t = readl(reg);
  1470. writel(t | STOP_PCI_MASTER, reg);
  1471. for (i = 0; i < 1000; i++) {
  1472. udelay(1);
  1473. t = readl(reg);
  1474. if (PCI_MASTER_EMPTY & t) {
  1475. break;
  1476. }
  1477. }
  1478. if (!(PCI_MASTER_EMPTY & t)) {
  1479. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  1480. rc = 1;
  1481. goto done;
  1482. }
  1483. /* set reset */
  1484. i = 5;
  1485. do {
  1486. writel(t | GLOB_SFT_RST, reg);
  1487. t = readl(reg);
  1488. udelay(1);
  1489. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  1490. if (!(GLOB_SFT_RST & t)) {
  1491. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  1492. rc = 1;
  1493. goto done;
  1494. }
  1495. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  1496. i = 5;
  1497. do {
  1498. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  1499. t = readl(reg);
  1500. udelay(1);
  1501. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  1502. if (GLOB_SFT_RST & t) {
  1503. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  1504. rc = 1;
  1505. }
  1506. done:
  1507. return rc;
  1508. }
  1509. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  1510. void __iomem *mmio)
  1511. {
  1512. void __iomem *port_mmio;
  1513. u32 tmp;
  1514. tmp = readl(mmio + MV_RESET_CFG);
  1515. if ((tmp & (1 << 0)) == 0) {
  1516. hpriv->signal[idx].amps = 0x7 << 8;
  1517. hpriv->signal[idx].pre = 0x1 << 5;
  1518. return;
  1519. }
  1520. port_mmio = mv_port_base(mmio, idx);
  1521. tmp = readl(port_mmio + PHY_MODE2);
  1522. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  1523. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  1524. }
  1525. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1526. {
  1527. writel(0x00000060, mmio + MV_GPIO_PORT_CTL);
  1528. }
  1529. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1530. unsigned int port)
  1531. {
  1532. void __iomem *port_mmio = mv_port_base(mmio, port);
  1533. u32 hp_flags = hpriv->hp_flags;
  1534. int fix_phy_mode2 =
  1535. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1536. int fix_phy_mode4 =
  1537. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1538. u32 m2, tmp;
  1539. if (fix_phy_mode2) {
  1540. m2 = readl(port_mmio + PHY_MODE2);
  1541. m2 &= ~(1 << 16);
  1542. m2 |= (1 << 31);
  1543. writel(m2, port_mmio + PHY_MODE2);
  1544. udelay(200);
  1545. m2 = readl(port_mmio + PHY_MODE2);
  1546. m2 &= ~((1 << 16) | (1 << 31));
  1547. writel(m2, port_mmio + PHY_MODE2);
  1548. udelay(200);
  1549. }
  1550. /* who knows what this magic does */
  1551. tmp = readl(port_mmio + PHY_MODE3);
  1552. tmp &= ~0x7F800000;
  1553. tmp |= 0x2A800000;
  1554. writel(tmp, port_mmio + PHY_MODE3);
  1555. if (fix_phy_mode4) {
  1556. u32 m4;
  1557. m4 = readl(port_mmio + PHY_MODE4);
  1558. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1559. tmp = readl(port_mmio + 0x310);
  1560. m4 = (m4 & ~(1 << 1)) | (1 << 0);
  1561. writel(m4, port_mmio + PHY_MODE4);
  1562. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1563. writel(tmp, port_mmio + 0x310);
  1564. }
  1565. /* Revert values of pre-emphasis and signal amps to the saved ones */
  1566. m2 = readl(port_mmio + PHY_MODE2);
  1567. m2 &= ~MV_M2_PREAMP_MASK;
  1568. m2 |= hpriv->signal[port].amps;
  1569. m2 |= hpriv->signal[port].pre;
  1570. m2 &= ~(1 << 16);
  1571. /* according to mvSata 3.6.1, some IIE values are fixed */
  1572. if (IS_GEN_IIE(hpriv)) {
  1573. m2 &= ~0xC30FF01F;
  1574. m2 |= 0x0000900F;
  1575. }
  1576. writel(m2, port_mmio + PHY_MODE2);
  1577. }
  1578. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  1579. unsigned int port_no)
  1580. {
  1581. void __iomem *port_mmio = mv_port_base(mmio, port_no);
  1582. writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
  1583. if (IS_60XX(hpriv)) {
  1584. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1585. ifctl |= (1 << 12) | (1 << 7);
  1586. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1587. }
  1588. udelay(25); /* allow reset propagation */
  1589. /* Spec never mentions clearing the bit. Marvell's driver does
  1590. * clear the bit, however.
  1591. */
  1592. writelfl(0, port_mmio + EDMA_CMD_OFS);
  1593. hpriv->ops->phy_errata(hpriv, mmio, port_no);
  1594. if (IS_50XX(hpriv))
  1595. mdelay(1);
  1596. }
  1597. static void mv_stop_and_reset(struct ata_port *ap)
  1598. {
  1599. struct mv_host_priv *hpriv = ap->host_set->private_data;
  1600. void __iomem *mmio = ap->host_set->mmio_base;
  1601. mv_stop_dma(ap);
  1602. mv_channel_reset(hpriv, mmio, ap->port_no);
  1603. __mv_phy_reset(ap, 0);
  1604. }
  1605. static inline void __msleep(unsigned int msec, int can_sleep)
  1606. {
  1607. if (can_sleep)
  1608. msleep(msec);
  1609. else
  1610. mdelay(msec);
  1611. }
  1612. /**
  1613. * __mv_phy_reset - Perform eDMA reset followed by COMRESET
  1614. * @ap: ATA channel to manipulate
  1615. *
  1616. * Part of this is taken from __sata_phy_reset and modified to
  1617. * not sleep since this routine gets called from interrupt level.
  1618. *
  1619. * LOCKING:
  1620. * Inherited from caller. This is coded to safe to call at
  1621. * interrupt level, i.e. it does not sleep.
  1622. */
  1623. static void __mv_phy_reset(struct ata_port *ap, int can_sleep)
  1624. {
  1625. struct mv_port_priv *pp = ap->private_data;
  1626. struct mv_host_priv *hpriv = ap->host_set->private_data;
  1627. void __iomem *port_mmio = mv_ap_base(ap);
  1628. struct ata_taskfile tf;
  1629. struct ata_device *dev = &ap->device[0];
  1630. unsigned long timeout;
  1631. int retry = 5;
  1632. u32 sstatus;
  1633. VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
  1634. DPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
  1635. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1636. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1637. /* Issue COMRESET via SControl */
  1638. comreset_retry:
  1639. scr_write_flush(ap, SCR_CONTROL, 0x301);
  1640. __msleep(1, can_sleep);
  1641. scr_write_flush(ap, SCR_CONTROL, 0x300);
  1642. __msleep(20, can_sleep);
  1643. timeout = jiffies + msecs_to_jiffies(200);
  1644. do {
  1645. sstatus = scr_read(ap, SCR_STATUS) & 0x3;
  1646. if ((sstatus == 3) || (sstatus == 0))
  1647. break;
  1648. __msleep(1, can_sleep);
  1649. } while (time_before(jiffies, timeout));
  1650. /* work around errata */
  1651. if (IS_60XX(hpriv) &&
  1652. (sstatus != 0x0) && (sstatus != 0x113) && (sstatus != 0x123) &&
  1653. (retry-- > 0))
  1654. goto comreset_retry;
  1655. DPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
  1656. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1657. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1658. if (sata_dev_present(ap)) {
  1659. ata_port_probe(ap);
  1660. } else {
  1661. printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
  1662. ap->id, scr_read(ap, SCR_STATUS));
  1663. ata_port_disable(ap);
  1664. return;
  1665. }
  1666. ap->cbl = ATA_CBL_SATA;
  1667. /* even after SStatus reflects that device is ready,
  1668. * it seems to take a while for link to be fully
  1669. * established (and thus Status no longer 0x80/0x7F),
  1670. * so we poll a bit for that, here.
  1671. */
  1672. retry = 20;
  1673. while (1) {
  1674. u8 drv_stat = ata_check_status(ap);
  1675. if ((drv_stat != 0x80) && (drv_stat != 0x7f))
  1676. break;
  1677. __msleep(500, can_sleep);
  1678. if (retry-- <= 0)
  1679. break;
  1680. }
  1681. tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
  1682. tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
  1683. tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
  1684. tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
  1685. dev->class = ata_dev_classify(&tf);
  1686. if (!ata_dev_present(dev)) {
  1687. VPRINTK("Port disabled post-sig: No device present.\n");
  1688. ata_port_disable(ap);
  1689. }
  1690. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1691. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1692. VPRINTK("EXIT\n");
  1693. }
  1694. static void mv_phy_reset(struct ata_port *ap)
  1695. {
  1696. __mv_phy_reset(ap, 1);
  1697. }
  1698. /**
  1699. * mv_eng_timeout - Routine called by libata when SCSI times out I/O
  1700. * @ap: ATA channel to manipulate
  1701. *
  1702. * Intent is to clear all pending error conditions, reset the
  1703. * chip/bus, fail the command, and move on.
  1704. *
  1705. * LOCKING:
  1706. * This routine holds the host_set lock while failing the command.
  1707. */
  1708. static void mv_eng_timeout(struct ata_port *ap)
  1709. {
  1710. struct ata_queued_cmd *qc;
  1711. printk(KERN_ERR "ata%u: Entering mv_eng_timeout\n",ap->id);
  1712. DPRINTK("All regs @ start of eng_timeout\n");
  1713. mv_dump_all_regs(ap->host_set->mmio_base, ap->port_no,
  1714. to_pci_dev(ap->host_set->dev));
  1715. qc = ata_qc_from_tag(ap, ap->active_tag);
  1716. printk(KERN_ERR "mmio_base %p ap %p qc %p scsi_cmnd %p &cmnd %p\n",
  1717. ap->host_set->mmio_base, ap, qc, qc->scsicmd,
  1718. &qc->scsicmd->cmnd);
  1719. mv_err_intr(ap);
  1720. mv_stop_and_reset(ap);
  1721. qc->err_mask |= AC_ERR_TIMEOUT;
  1722. ata_eh_qc_complete(qc);
  1723. }
  1724. /**
  1725. * mv_port_init - Perform some early initialization on a single port.
  1726. * @port: libata data structure storing shadow register addresses
  1727. * @port_mmio: base address of the port
  1728. *
  1729. * Initialize shadow register mmio addresses, clear outstanding
  1730. * interrupts on the port, and unmask interrupts for the future
  1731. * start of the port.
  1732. *
  1733. * LOCKING:
  1734. * Inherited from caller.
  1735. */
  1736. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  1737. {
  1738. unsigned long shd_base = (unsigned long) port_mmio + SHD_BLK_OFS;
  1739. unsigned serr_ofs;
  1740. /* PIO related setup
  1741. */
  1742. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  1743. port->error_addr =
  1744. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  1745. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  1746. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  1747. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  1748. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  1749. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  1750. port->status_addr =
  1751. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  1752. /* special case: control/altstatus doesn't have ATA_REG_ address */
  1753. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
  1754. /* unused: */
  1755. port->cmd_addr = port->bmdma_addr = port->scr_addr = 0;
  1756. /* Clear any currently outstanding port interrupt conditions */
  1757. serr_ofs = mv_scr_offset(SCR_ERROR);
  1758. writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
  1759. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1760. /* unmask all EDMA error interrupts */
  1761. writelfl(~0, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
  1762. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  1763. readl(port_mmio + EDMA_CFG_OFS),
  1764. readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
  1765. readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
  1766. }
  1767. static int mv_chip_id(struct pci_dev *pdev, struct mv_host_priv *hpriv,
  1768. unsigned int board_idx)
  1769. {
  1770. u8 rev_id;
  1771. u32 hp_flags = hpriv->hp_flags;
  1772. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1773. switch(board_idx) {
  1774. case chip_5080:
  1775. hpriv->ops = &mv5xxx_ops;
  1776. hp_flags |= MV_HP_50XX;
  1777. switch (rev_id) {
  1778. case 0x1:
  1779. hp_flags |= MV_HP_ERRATA_50XXB0;
  1780. break;
  1781. case 0x3:
  1782. hp_flags |= MV_HP_ERRATA_50XXB2;
  1783. break;
  1784. default:
  1785. dev_printk(KERN_WARNING, &pdev->dev,
  1786. "Applying 50XXB2 workarounds to unknown rev\n");
  1787. hp_flags |= MV_HP_ERRATA_50XXB2;
  1788. break;
  1789. }
  1790. break;
  1791. case chip_504x:
  1792. case chip_508x:
  1793. hpriv->ops = &mv5xxx_ops;
  1794. hp_flags |= MV_HP_50XX;
  1795. switch (rev_id) {
  1796. case 0x0:
  1797. hp_flags |= MV_HP_ERRATA_50XXB0;
  1798. break;
  1799. case 0x3:
  1800. hp_flags |= MV_HP_ERRATA_50XXB2;
  1801. break;
  1802. default:
  1803. dev_printk(KERN_WARNING, &pdev->dev,
  1804. "Applying B2 workarounds to unknown rev\n");
  1805. hp_flags |= MV_HP_ERRATA_50XXB2;
  1806. break;
  1807. }
  1808. break;
  1809. case chip_604x:
  1810. case chip_608x:
  1811. hpriv->ops = &mv6xxx_ops;
  1812. switch (rev_id) {
  1813. case 0x7:
  1814. hp_flags |= MV_HP_ERRATA_60X1B2;
  1815. break;
  1816. case 0x9:
  1817. hp_flags |= MV_HP_ERRATA_60X1C0;
  1818. break;
  1819. default:
  1820. dev_printk(KERN_WARNING, &pdev->dev,
  1821. "Applying B2 workarounds to unknown rev\n");
  1822. hp_flags |= MV_HP_ERRATA_60X1B2;
  1823. break;
  1824. }
  1825. break;
  1826. case chip_7042:
  1827. case chip_6042:
  1828. hpriv->ops = &mv6xxx_ops;
  1829. hp_flags |= MV_HP_GEN_IIE;
  1830. switch (rev_id) {
  1831. case 0x0:
  1832. hp_flags |= MV_HP_ERRATA_XX42A0;
  1833. break;
  1834. case 0x1:
  1835. hp_flags |= MV_HP_ERRATA_60X1C0;
  1836. break;
  1837. default:
  1838. dev_printk(KERN_WARNING, &pdev->dev,
  1839. "Applying 60X1C0 workarounds to unknown rev\n");
  1840. hp_flags |= MV_HP_ERRATA_60X1C0;
  1841. break;
  1842. }
  1843. break;
  1844. default:
  1845. printk(KERN_ERR DRV_NAME ": BUG: invalid board index %u\n", board_idx);
  1846. return 1;
  1847. }
  1848. hpriv->hp_flags = hp_flags;
  1849. return 0;
  1850. }
  1851. /**
  1852. * mv_init_host - Perform some early initialization of the host.
  1853. * @pdev: host PCI device
  1854. * @probe_ent: early data struct representing the host
  1855. *
  1856. * If possible, do an early global reset of the host. Then do
  1857. * our port init and clear/unmask all/relevant host interrupts.
  1858. *
  1859. * LOCKING:
  1860. * Inherited from caller.
  1861. */
  1862. static int mv_init_host(struct pci_dev *pdev, struct ata_probe_ent *probe_ent,
  1863. unsigned int board_idx)
  1864. {
  1865. int rc = 0, n_hc, port, hc;
  1866. void __iomem *mmio = probe_ent->mmio_base;
  1867. struct mv_host_priv *hpriv = probe_ent->private_data;
  1868. /* global interrupt mask */
  1869. writel(0, mmio + HC_MAIN_IRQ_MASK_OFS);
  1870. rc = mv_chip_id(pdev, hpriv, board_idx);
  1871. if (rc)
  1872. goto done;
  1873. n_hc = mv_get_hc_count(probe_ent->host_flags);
  1874. probe_ent->n_ports = MV_PORTS_PER_HC * n_hc;
  1875. for (port = 0; port < probe_ent->n_ports; port++)
  1876. hpriv->ops->read_preamp(hpriv, port, mmio);
  1877. rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
  1878. if (rc)
  1879. goto done;
  1880. hpriv->ops->reset_flash(hpriv, mmio);
  1881. hpriv->ops->reset_bus(pdev, mmio);
  1882. hpriv->ops->enable_leds(hpriv, mmio);
  1883. for (port = 0; port < probe_ent->n_ports; port++) {
  1884. if (IS_60XX(hpriv)) {
  1885. void __iomem *port_mmio = mv_port_base(mmio, port);
  1886. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1887. ifctl |= (1 << 12);
  1888. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1889. }
  1890. hpriv->ops->phy_errata(hpriv, mmio, port);
  1891. }
  1892. for (port = 0; port < probe_ent->n_ports; port++) {
  1893. void __iomem *port_mmio = mv_port_base(mmio, port);
  1894. mv_port_init(&probe_ent->port[port], port_mmio);
  1895. }
  1896. for (hc = 0; hc < n_hc; hc++) {
  1897. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1898. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  1899. "(before clear)=0x%08x\n", hc,
  1900. readl(hc_mmio + HC_CFG_OFS),
  1901. readl(hc_mmio + HC_IRQ_CAUSE_OFS));
  1902. /* Clear any currently outstanding hc interrupt conditions */
  1903. writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
  1904. }
  1905. /* Clear any currently outstanding host interrupt conditions */
  1906. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1907. /* and unmask interrupt generation for host regs */
  1908. writelfl(PCI_UNMASK_ALL_IRQS, mmio + PCI_IRQ_MASK_OFS);
  1909. writelfl(~HC_MAIN_MASKED_IRQS, mmio + HC_MAIN_IRQ_MASK_OFS);
  1910. VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
  1911. "PCI int cause/mask=0x%08x/0x%08x\n",
  1912. readl(mmio + HC_MAIN_IRQ_CAUSE_OFS),
  1913. readl(mmio + HC_MAIN_IRQ_MASK_OFS),
  1914. readl(mmio + PCI_IRQ_CAUSE_OFS),
  1915. readl(mmio + PCI_IRQ_MASK_OFS));
  1916. done:
  1917. return rc;
  1918. }
  1919. /**
  1920. * mv_print_info - Dump key info to kernel log for perusal.
  1921. * @probe_ent: early data struct representing the host
  1922. *
  1923. * FIXME: complete this.
  1924. *
  1925. * LOCKING:
  1926. * Inherited from caller.
  1927. */
  1928. static void mv_print_info(struct ata_probe_ent *probe_ent)
  1929. {
  1930. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1931. struct mv_host_priv *hpriv = probe_ent->private_data;
  1932. u8 rev_id, scc;
  1933. const char *scc_s;
  1934. /* Use this to determine the HW stepping of the chip so we know
  1935. * what errata to workaround
  1936. */
  1937. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1938. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  1939. if (scc == 0)
  1940. scc_s = "SCSI";
  1941. else if (scc == 0x01)
  1942. scc_s = "RAID";
  1943. else
  1944. scc_s = "unknown";
  1945. dev_printk(KERN_INFO, &pdev->dev,
  1946. "%u slots %u ports %s mode IRQ via %s\n",
  1947. (unsigned)MV_MAX_Q_DEPTH, probe_ent->n_ports,
  1948. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  1949. }
  1950. /**
  1951. * mv_init_one - handle a positive probe of a Marvell host
  1952. * @pdev: PCI device found
  1953. * @ent: PCI device ID entry for the matched host
  1954. *
  1955. * LOCKING:
  1956. * Inherited from caller.
  1957. */
  1958. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1959. {
  1960. static int printed_version = 0;
  1961. struct ata_probe_ent *probe_ent = NULL;
  1962. struct mv_host_priv *hpriv;
  1963. unsigned int board_idx = (unsigned int)ent->driver_data;
  1964. void __iomem *mmio_base;
  1965. int pci_dev_busy = 0, rc;
  1966. if (!printed_version++)
  1967. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  1968. rc = pci_enable_device(pdev);
  1969. if (rc) {
  1970. return rc;
  1971. }
  1972. rc = pci_request_regions(pdev, DRV_NAME);
  1973. if (rc) {
  1974. pci_dev_busy = 1;
  1975. goto err_out;
  1976. }
  1977. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1978. if (probe_ent == NULL) {
  1979. rc = -ENOMEM;
  1980. goto err_out_regions;
  1981. }
  1982. memset(probe_ent, 0, sizeof(*probe_ent));
  1983. probe_ent->dev = pci_dev_to_dev(pdev);
  1984. INIT_LIST_HEAD(&probe_ent->node);
  1985. mmio_base = pci_iomap(pdev, MV_PRIMARY_BAR, 0);
  1986. if (mmio_base == NULL) {
  1987. rc = -ENOMEM;
  1988. goto err_out_free_ent;
  1989. }
  1990. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1991. if (!hpriv) {
  1992. rc = -ENOMEM;
  1993. goto err_out_iounmap;
  1994. }
  1995. memset(hpriv, 0, sizeof(*hpriv));
  1996. probe_ent->sht = mv_port_info[board_idx].sht;
  1997. probe_ent->host_flags = mv_port_info[board_idx].host_flags;
  1998. probe_ent->pio_mask = mv_port_info[board_idx].pio_mask;
  1999. probe_ent->udma_mask = mv_port_info[board_idx].udma_mask;
  2000. probe_ent->port_ops = mv_port_info[board_idx].port_ops;
  2001. probe_ent->irq = pdev->irq;
  2002. probe_ent->irq_flags = SA_SHIRQ;
  2003. probe_ent->mmio_base = mmio_base;
  2004. probe_ent->private_data = hpriv;
  2005. /* initialize adapter */
  2006. rc = mv_init_host(pdev, probe_ent, board_idx);
  2007. if (rc) {
  2008. goto err_out_hpriv;
  2009. }
  2010. /* Enable interrupts */
  2011. if (msi && pci_enable_msi(pdev) == 0) {
  2012. hpriv->hp_flags |= MV_HP_FLAG_MSI;
  2013. } else {
  2014. pci_intx(pdev, 1);
  2015. }
  2016. mv_dump_pci_cfg(pdev, 0x68);
  2017. mv_print_info(probe_ent);
  2018. if (ata_device_add(probe_ent) == 0) {
  2019. rc = -ENODEV; /* No devices discovered */
  2020. goto err_out_dev_add;
  2021. }
  2022. kfree(probe_ent);
  2023. return 0;
  2024. err_out_dev_add:
  2025. if (MV_HP_FLAG_MSI & hpriv->hp_flags) {
  2026. pci_disable_msi(pdev);
  2027. } else {
  2028. pci_intx(pdev, 0);
  2029. }
  2030. err_out_hpriv:
  2031. kfree(hpriv);
  2032. err_out_iounmap:
  2033. pci_iounmap(pdev, mmio_base);
  2034. err_out_free_ent:
  2035. kfree(probe_ent);
  2036. err_out_regions:
  2037. pci_release_regions(pdev);
  2038. err_out:
  2039. if (!pci_dev_busy) {
  2040. pci_disable_device(pdev);
  2041. }
  2042. return rc;
  2043. }
  2044. static int __init mv_init(void)
  2045. {
  2046. return pci_module_init(&mv_pci_driver);
  2047. }
  2048. static void __exit mv_exit(void)
  2049. {
  2050. pci_unregister_driver(&mv_pci_driver);
  2051. }
  2052. MODULE_AUTHOR("Brett Russ");
  2053. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  2054. MODULE_LICENSE("GPL");
  2055. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  2056. MODULE_VERSION(DRV_VERSION);
  2057. module_param(msi, int, 0444);
  2058. MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
  2059. module_init(mv_init);
  2060. module_exit(mv_exit);