sata_mv.c 63 KB

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