sata_mv.c 62 KB

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