sata_mv.c 63 KB

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