sata_mv.c 62 KB

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