sata_mv.c 63 KB

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