sata_mv.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  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.7"
  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_IRQ_COAL_CAUSE = (MV_IRQ_COAL_REG_BASE + 0x08),
  49. MV_IRQ_COAL_CAUSE_LO = (MV_IRQ_COAL_REG_BASE + 0x88),
  50. MV_IRQ_COAL_CAUSE_HI = (MV_IRQ_COAL_REG_BASE + 0x8c),
  51. MV_IRQ_COAL_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xcc),
  52. MV_IRQ_COAL_TIME_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xd0),
  53. MV_SATAHC0_REG_BASE = 0x20000,
  54. MV_FLASH_CTL = 0x1046c,
  55. MV_GPIO_PORT_CTL = 0x104f0,
  56. MV_RESET_CFG = 0x180d8,
  57. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  58. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  59. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  60. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  61. MV_USE_Q_DEPTH = ATA_DEF_QUEUE,
  62. MV_MAX_Q_DEPTH = 32,
  63. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  64. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  65. * CRPB needs alignment on a 256B boundary. Size == 256B
  66. * SG count of 176 leads to MV_PORT_PRIV_DMA_SZ == 4KB
  67. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  68. */
  69. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  70. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  71. MV_MAX_SG_CT = 176,
  72. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  73. MV_PORT_PRIV_DMA_SZ = (MV_CRQB_Q_SZ + MV_CRPB_Q_SZ + MV_SG_TBL_SZ),
  74. MV_PORTS_PER_HC = 4,
  75. /* == (port / MV_PORTS_PER_HC) to determine HC from 0-7 port */
  76. MV_PORT_HC_SHIFT = 2,
  77. /* == (port % MV_PORTS_PER_HC) to determine hard port from 0-7 port */
  78. MV_PORT_MASK = 3,
  79. /* Host Flags */
  80. MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
  81. MV_FLAG_IRQ_COALESCE = (1 << 29), /* IRQ coalescing capability */
  82. MV_COMMON_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  83. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  84. ATA_FLAG_NO_ATAPI),
  85. MV_6XXX_FLAGS = MV_FLAG_IRQ_COALESCE,
  86. CRQB_FLAG_READ = (1 << 0),
  87. CRQB_TAG_SHIFT = 1,
  88. CRQB_CMD_ADDR_SHIFT = 8,
  89. CRQB_CMD_CS = (0x2 << 11),
  90. CRQB_CMD_LAST = (1 << 15),
  91. CRPB_FLAG_STATUS_SHIFT = 8,
  92. EPRD_FLAG_END_OF_TBL = (1 << 31),
  93. /* PCI interface registers */
  94. PCI_COMMAND_OFS = 0xc00,
  95. PCI_MAIN_CMD_STS_OFS = 0xd30,
  96. STOP_PCI_MASTER = (1 << 2),
  97. PCI_MASTER_EMPTY = (1 << 3),
  98. GLOB_SFT_RST = (1 << 4),
  99. MV_PCI_MODE = 0xd00,
  100. MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
  101. MV_PCI_DISC_TIMER = 0xd04,
  102. MV_PCI_MSI_TRIGGER = 0xc38,
  103. MV_PCI_SERR_MASK = 0xc28,
  104. MV_PCI_XBAR_TMOUT = 0x1d04,
  105. MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
  106. MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
  107. MV_PCI_ERR_ATTRIBUTE = 0x1d48,
  108. MV_PCI_ERR_COMMAND = 0x1d50,
  109. PCI_IRQ_CAUSE_OFS = 0x1d58,
  110. PCI_IRQ_MASK_OFS = 0x1d5c,
  111. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  112. HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
  113. HC_MAIN_IRQ_MASK_OFS = 0x1d64,
  114. PORT0_ERR = (1 << 0), /* shift by port # */
  115. PORT0_DONE = (1 << 1), /* shift by port # */
  116. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  117. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  118. PCI_ERR = (1 << 18),
  119. TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
  120. TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
  121. PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
  122. GPIO_INT = (1 << 22),
  123. SELF_INT = (1 << 23),
  124. TWSI_INT = (1 << 24),
  125. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  126. HC_MAIN_MASKED_IRQS = (TRAN_LO_DONE | TRAN_HI_DONE |
  127. PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
  128. HC_MAIN_RSVD),
  129. /* SATAHC registers */
  130. HC_CFG_OFS = 0,
  131. HC_IRQ_CAUSE_OFS = 0x14,
  132. CRPB_DMA_DONE = (1 << 0), /* shift by port # */
  133. HC_IRQ_COAL = (1 << 4), /* IRQ coalescing */
  134. DEV_IRQ = (1 << 8), /* shift by port # */
  135. /* Shadow block registers */
  136. SHD_BLK_OFS = 0x100,
  137. SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
  138. /* SATA registers */
  139. SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
  140. SATA_ACTIVE_OFS = 0x350,
  141. PHY_MODE3 = 0x310,
  142. PHY_MODE4 = 0x314,
  143. PHY_MODE2 = 0x330,
  144. MV5_PHY_MODE = 0x74,
  145. MV5_LT_MODE = 0x30,
  146. MV5_PHY_CTL = 0x0C,
  147. SATA_INTERFACE_CTL = 0x050,
  148. MV_M2_PREAMP_MASK = 0x7e0,
  149. /* Port registers */
  150. EDMA_CFG_OFS = 0,
  151. EDMA_CFG_Q_DEPTH = 0, /* queueing disabled */
  152. EDMA_CFG_NCQ = (1 << 5),
  153. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  154. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  155. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  156. EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
  157. EDMA_ERR_IRQ_MASK_OFS = 0xc,
  158. EDMA_ERR_D_PAR = (1 << 0),
  159. EDMA_ERR_PRD_PAR = (1 << 1),
  160. EDMA_ERR_DEV = (1 << 2),
  161. EDMA_ERR_DEV_DCON = (1 << 3),
  162. EDMA_ERR_DEV_CON = (1 << 4),
  163. EDMA_ERR_SERR = (1 << 5),
  164. EDMA_ERR_SELF_DIS = (1 << 7),
  165. EDMA_ERR_BIST_ASYNC = (1 << 8),
  166. EDMA_ERR_CRBQ_PAR = (1 << 9),
  167. EDMA_ERR_CRPB_PAR = (1 << 10),
  168. EDMA_ERR_INTRL_PAR = (1 << 11),
  169. EDMA_ERR_IORDY = (1 << 12),
  170. EDMA_ERR_LNK_CTRL_RX = (0xf << 13),
  171. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15),
  172. EDMA_ERR_LNK_DATA_RX = (0xf << 17),
  173. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21),
  174. EDMA_ERR_LNK_DATA_TX = (0x1f << 26),
  175. EDMA_ERR_TRANS_PROTO = (1 << 31),
  176. EDMA_ERR_FATAL = (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  177. EDMA_ERR_DEV_DCON | EDMA_ERR_CRBQ_PAR |
  178. EDMA_ERR_CRPB_PAR | EDMA_ERR_INTRL_PAR |
  179. EDMA_ERR_IORDY | EDMA_ERR_LNK_CTRL_RX_2 |
  180. EDMA_ERR_LNK_DATA_RX |
  181. EDMA_ERR_LNK_DATA_TX |
  182. EDMA_ERR_TRANS_PROTO),
  183. EDMA_REQ_Q_BASE_HI_OFS = 0x10,
  184. EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
  185. EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
  186. EDMA_REQ_Q_PTR_SHIFT = 5,
  187. EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
  188. EDMA_RSP_Q_IN_PTR_OFS = 0x20,
  189. EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
  190. EDMA_RSP_Q_PTR_SHIFT = 3,
  191. EDMA_CMD_OFS = 0x28,
  192. EDMA_EN = (1 << 0),
  193. EDMA_DS = (1 << 1),
  194. ATA_RST = (1 << 2),
  195. EDMA_IORDY_TMOUT = 0x34,
  196. EDMA_ARB_CFG = 0x38,
  197. /* Host private flags (hp_flags) */
  198. MV_HP_FLAG_MSI = (1 << 0),
  199. MV_HP_ERRATA_50XXB0 = (1 << 1),
  200. MV_HP_ERRATA_50XXB2 = (1 << 2),
  201. MV_HP_ERRATA_60X1B2 = (1 << 3),
  202. MV_HP_ERRATA_60X1C0 = (1 << 4),
  203. MV_HP_ERRATA_XX42A0 = (1 << 5),
  204. MV_HP_50XX = (1 << 6),
  205. MV_HP_GEN_IIE = (1 << 7),
  206. /* Port private flags (pp_flags) */
  207. MV_PP_FLAG_EDMA_EN = (1 << 0),
  208. MV_PP_FLAG_EDMA_DS_ACT = (1 << 1),
  209. };
  210. #define IS_50XX(hpriv) ((hpriv)->hp_flags & MV_HP_50XX)
  211. #define IS_60XX(hpriv) (((hpriv)->hp_flags & MV_HP_50XX) == 0)
  212. #define IS_GEN_I(hpriv) IS_50XX(hpriv)
  213. #define IS_GEN_II(hpriv) IS_60XX(hpriv)
  214. #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
  215. enum {
  216. /* Our DMA boundary is determined by an ePRD being unable to handle
  217. * anything larger than 64KB
  218. */
  219. MV_DMA_BOUNDARY = 0xffffU,
  220. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  221. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  222. };
  223. enum chip_type {
  224. chip_504x,
  225. chip_508x,
  226. chip_5080,
  227. chip_604x,
  228. chip_608x,
  229. chip_6042,
  230. chip_7042,
  231. };
  232. /* Command ReQuest Block: 32B */
  233. struct mv_crqb {
  234. u32 sg_addr;
  235. u32 sg_addr_hi;
  236. u16 ctrl_flags;
  237. u16 ata_cmd[11];
  238. };
  239. struct mv_crqb_iie {
  240. u32 addr;
  241. u32 addr_hi;
  242. u32 flags;
  243. u32 len;
  244. u32 ata_cmd[4];
  245. };
  246. /* Command ResPonse Block: 8B */
  247. struct mv_crpb {
  248. u16 id;
  249. u16 flags;
  250. u32 tmstmp;
  251. };
  252. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  253. struct mv_sg {
  254. u32 addr;
  255. u32 flags_size;
  256. u32 addr_hi;
  257. u32 reserved;
  258. };
  259. struct mv_port_priv {
  260. struct mv_crqb *crqb;
  261. dma_addr_t crqb_dma;
  262. struct mv_crpb *crpb;
  263. dma_addr_t crpb_dma;
  264. struct mv_sg *sg_tbl;
  265. dma_addr_t sg_tbl_dma;
  266. u32 pp_flags;
  267. };
  268. struct mv_port_signal {
  269. u32 amps;
  270. u32 pre;
  271. };
  272. struct mv_host_priv;
  273. struct mv_hw_ops {
  274. void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
  275. unsigned int port);
  276. void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
  277. void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
  278. void __iomem *mmio);
  279. int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
  280. unsigned int n_hc);
  281. void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
  282. void (*reset_bus)(struct pci_dev *pdev, void __iomem *mmio);
  283. };
  284. struct mv_host_priv {
  285. u32 hp_flags;
  286. struct mv_port_signal signal[8];
  287. const struct mv_hw_ops *ops;
  288. };
  289. static void mv_irq_clear(struct ata_port *ap);
  290. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  291. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  292. static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  293. static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  294. static void mv_phy_reset(struct ata_port *ap);
  295. static void __mv_phy_reset(struct ata_port *ap, int can_sleep);
  296. static void mv_host_stop(struct ata_host_set *host_set);
  297. static int mv_port_start(struct ata_port *ap);
  298. static void mv_port_stop(struct ata_port *ap);
  299. static void mv_qc_prep(struct ata_queued_cmd *qc);
  300. static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
  301. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
  302. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  303. struct pt_regs *regs);
  304. static void mv_eng_timeout(struct ata_port *ap);
  305. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  306. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  307. unsigned int port);
  308. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  309. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  310. void __iomem *mmio);
  311. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  312. unsigned int n_hc);
  313. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  314. static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio);
  315. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  316. unsigned int port);
  317. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  318. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  319. void __iomem *mmio);
  320. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  321. unsigned int n_hc);
  322. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  323. static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio);
  324. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  325. unsigned int port_no);
  326. static void mv_stop_and_reset(struct ata_port *ap);
  327. static struct scsi_host_template mv_sht = {
  328. .module = THIS_MODULE,
  329. .name = DRV_NAME,
  330. .ioctl = ata_scsi_ioctl,
  331. .queuecommand = ata_scsi_queuecmd,
  332. .can_queue = MV_USE_Q_DEPTH,
  333. .this_id = ATA_SHT_THIS_ID,
  334. .sg_tablesize = MV_MAX_SG_CT / 2,
  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, hc);
  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. /* Don't turn on EDMA here...do it before DMA commands only. Else
  811. * we'll be unable to send non-data, PIO, etc due to restricted access
  812. * to shadow regs.
  813. */
  814. ap->private_data = pp;
  815. return 0;
  816. err_out_priv:
  817. mv_priv_free(pp, dev);
  818. err_out_pp:
  819. kfree(pp);
  820. err_out:
  821. return rc;
  822. }
  823. /**
  824. * mv_port_stop - Port specific cleanup/stop routine.
  825. * @ap: ATA channel to manipulate
  826. *
  827. * Stop DMA, cleanup port memory.
  828. *
  829. * LOCKING:
  830. * This routine uses the host_set lock to protect the DMA stop.
  831. */
  832. static void mv_port_stop(struct ata_port *ap)
  833. {
  834. struct device *dev = ap->host_set->dev;
  835. struct mv_port_priv *pp = ap->private_data;
  836. unsigned long flags;
  837. spin_lock_irqsave(&ap->host_set->lock, flags);
  838. mv_stop_dma(ap);
  839. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  840. ap->private_data = NULL;
  841. ata_pad_free(ap, dev);
  842. mv_priv_free(pp, dev);
  843. kfree(pp);
  844. }
  845. /**
  846. * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
  847. * @qc: queued command whose SG list to source from
  848. *
  849. * Populate the SG list and mark the last entry.
  850. *
  851. * LOCKING:
  852. * Inherited from caller.
  853. */
  854. static void mv_fill_sg(struct ata_queued_cmd *qc)
  855. {
  856. struct mv_port_priv *pp = qc->ap->private_data;
  857. unsigned int i = 0;
  858. struct scatterlist *sg;
  859. ata_for_each_sg(sg, qc) {
  860. dma_addr_t addr;
  861. u32 sg_len, len, offset;
  862. addr = sg_dma_address(sg);
  863. sg_len = sg_dma_len(sg);
  864. while (sg_len) {
  865. offset = addr & MV_DMA_BOUNDARY;
  866. len = sg_len;
  867. if ((offset + sg_len) > 0x10000)
  868. len = 0x10000 - offset;
  869. pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
  870. pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  871. pp->sg_tbl[i].flags_size = cpu_to_le32(len & 0xffff);
  872. sg_len -= len;
  873. addr += len;
  874. if (!sg_len && ata_sg_is_last(sg, qc))
  875. pp->sg_tbl[i].flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
  876. i++;
  877. }
  878. }
  879. }
  880. static inline unsigned mv_inc_q_index(unsigned index)
  881. {
  882. return (index + 1) & MV_MAX_Q_DEPTH_MASK;
  883. }
  884. static inline void mv_crqb_pack_cmd(u16 *cmdw, u8 data, u8 addr, unsigned last)
  885. {
  886. u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  887. (last ? CRQB_CMD_LAST : 0);
  888. *cmdw = cpu_to_le16(tmp);
  889. }
  890. /**
  891. * mv_qc_prep - Host specific command preparation.
  892. * @qc: queued command to prepare
  893. *
  894. * This routine simply redirects to the general purpose routine
  895. * if command is not DMA. Else, it handles prep of the CRQB
  896. * (command request block), does some sanity checking, and calls
  897. * the SG load routine.
  898. *
  899. * LOCKING:
  900. * Inherited from caller.
  901. */
  902. static void mv_qc_prep(struct ata_queued_cmd *qc)
  903. {
  904. struct ata_port *ap = qc->ap;
  905. struct mv_port_priv *pp = ap->private_data;
  906. u16 *cw;
  907. struct ata_taskfile *tf;
  908. u16 flags = 0;
  909. unsigned in_index;
  910. if (ATA_PROT_DMA != qc->tf.protocol)
  911. return;
  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. /* get current queue index from hardware */
  919. in_index = (readl(mv_ap_base(ap) + EDMA_REQ_Q_IN_PTR_OFS)
  920. >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  921. pp->crqb[in_index].sg_addr =
  922. cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
  923. pp->crqb[in_index].sg_addr_hi =
  924. cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
  925. pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
  926. cw = &pp->crqb[in_index].ata_cmd[0];
  927. tf = &qc->tf;
  928. /* Sadly, the CRQB cannot accomodate all registers--there are
  929. * only 11 bytes...so we must pick and choose required
  930. * registers based on the command. So, we drop feature and
  931. * hob_feature for [RW] DMA commands, but they are needed for
  932. * NCQ. NCQ will drop hob_nsect.
  933. */
  934. switch (tf->command) {
  935. case ATA_CMD_READ:
  936. case ATA_CMD_READ_EXT:
  937. case ATA_CMD_WRITE:
  938. case ATA_CMD_WRITE_EXT:
  939. case ATA_CMD_WRITE_FUA_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. unsigned in_index;
  993. u32 flags = 0;
  994. if (ATA_PROT_DMA != qc->tf.protocol)
  995. return;
  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. /* get current queue index from hardware */
  1003. in_index = (readl(mv_ap_base(ap) + EDMA_REQ_Q_IN_PTR_OFS)
  1004. >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  1005. crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
  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. unsigned in_index;
  1051. u32 in_ptr;
  1052. if (ATA_PROT_DMA != qc->tf.protocol) {
  1053. /* We're about to send a non-EDMA capable command to the
  1054. * port. Turn off EDMA so there won't be problems accessing
  1055. * shadow block, etc registers.
  1056. */
  1057. mv_stop_dma(qc->ap);
  1058. return ata_qc_issue_prot(qc);
  1059. }
  1060. in_ptr = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  1061. in_index = (in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  1062. /* until we do queuing, the queue should be empty at this point */
  1063. WARN_ON(in_index != ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS)
  1064. >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
  1065. in_index = mv_inc_q_index(in_index); /* now incr producer index */
  1066. mv_start_dma(port_mmio, pp);
  1067. /* and write the request in pointer to kick the EDMA to life */
  1068. in_ptr &= EDMA_REQ_Q_BASE_LO_MASK;
  1069. in_ptr |= in_index << EDMA_REQ_Q_PTR_SHIFT;
  1070. writelfl(in_ptr, port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  1071. return 0;
  1072. }
  1073. /**
  1074. * mv_get_crpb_status - get status from most recently completed cmd
  1075. * @ap: ATA channel to manipulate
  1076. *
  1077. * This routine is for use when the port is in DMA mode, when it
  1078. * will be using the CRPB (command response block) method of
  1079. * returning command completion information. We check indices
  1080. * are good, grab status, and bump the response consumer index to
  1081. * prove that we're up to date.
  1082. *
  1083. * LOCKING:
  1084. * Inherited from caller.
  1085. */
  1086. static u8 mv_get_crpb_status(struct ata_port *ap)
  1087. {
  1088. void __iomem *port_mmio = mv_ap_base(ap);
  1089. struct mv_port_priv *pp = ap->private_data;
  1090. unsigned out_index;
  1091. u32 out_ptr;
  1092. u8 ata_status;
  1093. out_ptr = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  1094. out_index = (out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  1095. ata_status = le16_to_cpu(pp->crpb[out_index].flags)
  1096. >> CRPB_FLAG_STATUS_SHIFT;
  1097. /* increment our consumer index... */
  1098. out_index = mv_inc_q_index(out_index);
  1099. /* and, until we do NCQ, there should only be 1 CRPB waiting */
  1100. WARN_ON(out_index != ((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
  1101. >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
  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 |= out_index << 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. * @reset_allowed: bool: 0 == don't trigger from reset here
  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, int reset_allowed)
  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 (reset_allowed && (EDMA_ERR_FATAL & edma_err_cause))
  1142. mv_stop_and_reset(ap);
  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 = mv_hardport_from_port(port); /* 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.
  1189. * The cause of this is still under investigation.
  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. /* ignore spurious intr if drive still BUSY */
  1204. if (ata_status & ATA_BUSY) {
  1205. ata_status = 0;
  1206. handled = 0;
  1207. }
  1208. }
  1209. }
  1210. if (ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))
  1211. continue;
  1212. err_mask = ac_err_mask(ata_status);
  1213. shift = port << 1; /* (port * 2) */
  1214. if (port >= MV_PORTS_PER_HC) {
  1215. shift++; /* skip bit 8 in the HC Main IRQ reg */
  1216. }
  1217. if ((PORT0_ERR << shift) & relevant) {
  1218. mv_err_intr(ap, 1);
  1219. err_mask |= AC_ERR_OTHER;
  1220. handled = 1;
  1221. }
  1222. if (handled) {
  1223. qc = ata_qc_from_tag(ap, ap->active_tag);
  1224. if (qc && (qc->flags & ATA_QCFLAG_ACTIVE)) {
  1225. VPRINTK("port %u IRQ found for qc, "
  1226. "ata_status 0x%x\n", port,ata_status);
  1227. /* mark qc status appropriately */
  1228. if (!(qc->tf.ctl & ATA_NIEN)) {
  1229. qc->err_mask |= err_mask;
  1230. ata_qc_complete(qc);
  1231. }
  1232. }
  1233. }
  1234. }
  1235. VPRINTK("EXIT\n");
  1236. }
  1237. /**
  1238. * mv_interrupt -
  1239. * @irq: unused
  1240. * @dev_instance: private data; in this case the host structure
  1241. * @regs: unused
  1242. *
  1243. * Read the read only register to determine if any host
  1244. * controllers have pending interrupts. If so, call lower level
  1245. * routine to handle. Also check for PCI errors which are only
  1246. * reported here.
  1247. *
  1248. * LOCKING:
  1249. * This routine holds the host_set lock while processing pending
  1250. * interrupts.
  1251. */
  1252. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  1253. struct pt_regs *regs)
  1254. {
  1255. struct ata_host_set *host_set = dev_instance;
  1256. unsigned int hc, handled = 0, n_hcs;
  1257. void __iomem *mmio = host_set->mmio_base;
  1258. struct mv_host_priv *hpriv;
  1259. u32 irq_stat;
  1260. irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
  1261. /* check the cases where we either have nothing pending or have read
  1262. * a bogus register value which can indicate HW removal or PCI fault
  1263. */
  1264. if (!irq_stat || (0xffffffffU == irq_stat)) {
  1265. return IRQ_NONE;
  1266. }
  1267. n_hcs = mv_get_hc_count(host_set->ports[0]->flags);
  1268. spin_lock(&host_set->lock);
  1269. for (hc = 0; hc < n_hcs; hc++) {
  1270. u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
  1271. if (relevant) {
  1272. mv_host_intr(host_set, relevant, hc);
  1273. handled++;
  1274. }
  1275. }
  1276. hpriv = host_set->private_data;
  1277. if (IS_60XX(hpriv)) {
  1278. /* deal with the interrupt coalescing bits */
  1279. if (irq_stat & (TRAN_LO_DONE | TRAN_HI_DONE | PORTS_0_7_COAL_DONE)) {
  1280. writelfl(0, mmio + MV_IRQ_COAL_CAUSE_LO);
  1281. writelfl(0, mmio + MV_IRQ_COAL_CAUSE_HI);
  1282. writelfl(0, mmio + MV_IRQ_COAL_CAUSE);
  1283. }
  1284. }
  1285. if (PCI_ERR & irq_stat) {
  1286. printk(KERN_ERR DRV_NAME ": PCI ERROR; PCI IRQ cause=0x%08x\n",
  1287. readl(mmio + PCI_IRQ_CAUSE_OFS));
  1288. DPRINTK("All regs @ PCI error\n");
  1289. mv_dump_all_regs(mmio, -1, to_pci_dev(host_set->dev));
  1290. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1291. handled++;
  1292. }
  1293. spin_unlock(&host_set->lock);
  1294. return IRQ_RETVAL(handled);
  1295. }
  1296. static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
  1297. {
  1298. void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
  1299. unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
  1300. return hc_mmio + ofs;
  1301. }
  1302. static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
  1303. {
  1304. unsigned int ofs;
  1305. switch (sc_reg_in) {
  1306. case SCR_STATUS:
  1307. case SCR_ERROR:
  1308. case SCR_CONTROL:
  1309. ofs = sc_reg_in * sizeof(u32);
  1310. break;
  1311. default:
  1312. ofs = 0xffffffffU;
  1313. break;
  1314. }
  1315. return ofs;
  1316. }
  1317. static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  1318. {
  1319. void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
  1320. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1321. if (ofs != 0xffffffffU)
  1322. return readl(mmio + ofs);
  1323. else
  1324. return (u32) ofs;
  1325. }
  1326. static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  1327. {
  1328. void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
  1329. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1330. if (ofs != 0xffffffffU)
  1331. writelfl(val, mmio + ofs);
  1332. }
  1333. static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio)
  1334. {
  1335. u8 rev_id;
  1336. int early_5080;
  1337. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1338. early_5080 = (pdev->device == 0x5080) && (rev_id == 0);
  1339. if (!early_5080) {
  1340. u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1341. tmp |= (1 << 0);
  1342. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1343. }
  1344. mv_reset_pci_bus(pdev, mmio);
  1345. }
  1346. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1347. {
  1348. writel(0x0fcfffff, mmio + MV_FLASH_CTL);
  1349. }
  1350. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  1351. void __iomem *mmio)
  1352. {
  1353. void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
  1354. u32 tmp;
  1355. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1356. hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
  1357. hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
  1358. }
  1359. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1360. {
  1361. u32 tmp;
  1362. writel(0, mmio + MV_GPIO_PORT_CTL);
  1363. /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
  1364. tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1365. tmp |= ~(1 << 0);
  1366. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1367. }
  1368. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1369. unsigned int port)
  1370. {
  1371. void __iomem *phy_mmio = mv5_phy_base(mmio, port);
  1372. const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
  1373. u32 tmp;
  1374. int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
  1375. if (fix_apm_sq) {
  1376. tmp = readl(phy_mmio + MV5_LT_MODE);
  1377. tmp |= (1 << 19);
  1378. writel(tmp, phy_mmio + MV5_LT_MODE);
  1379. tmp = readl(phy_mmio + MV5_PHY_CTL);
  1380. tmp &= ~0x3;
  1381. tmp |= 0x1;
  1382. writel(tmp, phy_mmio + MV5_PHY_CTL);
  1383. }
  1384. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1385. tmp &= ~mask;
  1386. tmp |= hpriv->signal[port].pre;
  1387. tmp |= hpriv->signal[port].amps;
  1388. writel(tmp, phy_mmio + MV5_PHY_MODE);
  1389. }
  1390. #undef ZERO
  1391. #define ZERO(reg) writel(0, port_mmio + (reg))
  1392. static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
  1393. unsigned int port)
  1394. {
  1395. void __iomem *port_mmio = mv_port_base(mmio, port);
  1396. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  1397. mv_channel_reset(hpriv, mmio, port);
  1398. ZERO(0x028); /* command */
  1399. writel(0x11f, port_mmio + EDMA_CFG_OFS);
  1400. ZERO(0x004); /* timer */
  1401. ZERO(0x008); /* irq err cause */
  1402. ZERO(0x00c); /* irq err mask */
  1403. ZERO(0x010); /* rq bah */
  1404. ZERO(0x014); /* rq inp */
  1405. ZERO(0x018); /* rq outp */
  1406. ZERO(0x01c); /* respq bah */
  1407. ZERO(0x024); /* respq outp */
  1408. ZERO(0x020); /* respq inp */
  1409. ZERO(0x02c); /* test control */
  1410. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
  1411. }
  1412. #undef ZERO
  1413. #define ZERO(reg) writel(0, hc_mmio + (reg))
  1414. static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1415. unsigned int hc)
  1416. {
  1417. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1418. u32 tmp;
  1419. ZERO(0x00c);
  1420. ZERO(0x010);
  1421. ZERO(0x014);
  1422. ZERO(0x018);
  1423. tmp = readl(hc_mmio + 0x20);
  1424. tmp &= 0x1c1c1c1c;
  1425. tmp |= 0x03030303;
  1426. writel(tmp, hc_mmio + 0x20);
  1427. }
  1428. #undef ZERO
  1429. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1430. unsigned int n_hc)
  1431. {
  1432. unsigned int hc, port;
  1433. for (hc = 0; hc < n_hc; hc++) {
  1434. for (port = 0; port < MV_PORTS_PER_HC; port++)
  1435. mv5_reset_hc_port(hpriv, mmio,
  1436. (hc * MV_PORTS_PER_HC) + port);
  1437. mv5_reset_one_hc(hpriv, mmio, hc);
  1438. }
  1439. return 0;
  1440. }
  1441. #undef ZERO
  1442. #define ZERO(reg) writel(0, mmio + (reg))
  1443. static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio)
  1444. {
  1445. u32 tmp;
  1446. tmp = readl(mmio + MV_PCI_MODE);
  1447. tmp &= 0xff00ffff;
  1448. writel(tmp, mmio + MV_PCI_MODE);
  1449. ZERO(MV_PCI_DISC_TIMER);
  1450. ZERO(MV_PCI_MSI_TRIGGER);
  1451. writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
  1452. ZERO(HC_MAIN_IRQ_MASK_OFS);
  1453. ZERO(MV_PCI_SERR_MASK);
  1454. ZERO(PCI_IRQ_CAUSE_OFS);
  1455. ZERO(PCI_IRQ_MASK_OFS);
  1456. ZERO(MV_PCI_ERR_LOW_ADDRESS);
  1457. ZERO(MV_PCI_ERR_HIGH_ADDRESS);
  1458. ZERO(MV_PCI_ERR_ATTRIBUTE);
  1459. ZERO(MV_PCI_ERR_COMMAND);
  1460. }
  1461. #undef ZERO
  1462. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1463. {
  1464. u32 tmp;
  1465. mv5_reset_flash(hpriv, mmio);
  1466. tmp = readl(mmio + MV_GPIO_PORT_CTL);
  1467. tmp &= 0x3;
  1468. tmp |= (1 << 5) | (1 << 6);
  1469. writel(tmp, mmio + MV_GPIO_PORT_CTL);
  1470. }
  1471. /**
  1472. * mv6_reset_hc - Perform the 6xxx global soft reset
  1473. * @mmio: base address of the HBA
  1474. *
  1475. * This routine only applies to 6xxx parts.
  1476. *
  1477. * LOCKING:
  1478. * Inherited from caller.
  1479. */
  1480. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1481. unsigned int n_hc)
  1482. {
  1483. void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
  1484. int i, rc = 0;
  1485. u32 t;
  1486. /* Following procedure defined in PCI "main command and status
  1487. * register" table.
  1488. */
  1489. t = readl(reg);
  1490. writel(t | STOP_PCI_MASTER, reg);
  1491. for (i = 0; i < 1000; i++) {
  1492. udelay(1);
  1493. t = readl(reg);
  1494. if (PCI_MASTER_EMPTY & t) {
  1495. break;
  1496. }
  1497. }
  1498. if (!(PCI_MASTER_EMPTY & t)) {
  1499. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  1500. rc = 1;
  1501. goto done;
  1502. }
  1503. /* set reset */
  1504. i = 5;
  1505. do {
  1506. writel(t | GLOB_SFT_RST, reg);
  1507. t = readl(reg);
  1508. udelay(1);
  1509. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  1510. if (!(GLOB_SFT_RST & t)) {
  1511. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  1512. rc = 1;
  1513. goto done;
  1514. }
  1515. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  1516. i = 5;
  1517. do {
  1518. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  1519. t = readl(reg);
  1520. udelay(1);
  1521. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  1522. if (GLOB_SFT_RST & t) {
  1523. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  1524. rc = 1;
  1525. }
  1526. done:
  1527. return rc;
  1528. }
  1529. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  1530. void __iomem *mmio)
  1531. {
  1532. void __iomem *port_mmio;
  1533. u32 tmp;
  1534. tmp = readl(mmio + MV_RESET_CFG);
  1535. if ((tmp & (1 << 0)) == 0) {
  1536. hpriv->signal[idx].amps = 0x7 << 8;
  1537. hpriv->signal[idx].pre = 0x1 << 5;
  1538. return;
  1539. }
  1540. port_mmio = mv_port_base(mmio, idx);
  1541. tmp = readl(port_mmio + PHY_MODE2);
  1542. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  1543. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  1544. }
  1545. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1546. {
  1547. writel(0x00000060, mmio + MV_GPIO_PORT_CTL);
  1548. }
  1549. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1550. unsigned int port)
  1551. {
  1552. void __iomem *port_mmio = mv_port_base(mmio, port);
  1553. u32 hp_flags = hpriv->hp_flags;
  1554. int fix_phy_mode2 =
  1555. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1556. int fix_phy_mode4 =
  1557. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1558. u32 m2, tmp;
  1559. if (fix_phy_mode2) {
  1560. m2 = readl(port_mmio + PHY_MODE2);
  1561. m2 &= ~(1 << 16);
  1562. m2 |= (1 << 31);
  1563. writel(m2, port_mmio + PHY_MODE2);
  1564. udelay(200);
  1565. m2 = readl(port_mmio + PHY_MODE2);
  1566. m2 &= ~((1 << 16) | (1 << 31));
  1567. writel(m2, port_mmio + PHY_MODE2);
  1568. udelay(200);
  1569. }
  1570. /* who knows what this magic does */
  1571. tmp = readl(port_mmio + PHY_MODE3);
  1572. tmp &= ~0x7F800000;
  1573. tmp |= 0x2A800000;
  1574. writel(tmp, port_mmio + PHY_MODE3);
  1575. if (fix_phy_mode4) {
  1576. u32 m4;
  1577. m4 = readl(port_mmio + PHY_MODE4);
  1578. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1579. tmp = readl(port_mmio + 0x310);
  1580. m4 = (m4 & ~(1 << 1)) | (1 << 0);
  1581. writel(m4, port_mmio + PHY_MODE4);
  1582. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1583. writel(tmp, port_mmio + 0x310);
  1584. }
  1585. /* Revert values of pre-emphasis and signal amps to the saved ones */
  1586. m2 = readl(port_mmio + PHY_MODE2);
  1587. m2 &= ~MV_M2_PREAMP_MASK;
  1588. m2 |= hpriv->signal[port].amps;
  1589. m2 |= hpriv->signal[port].pre;
  1590. m2 &= ~(1 << 16);
  1591. /* according to mvSata 3.6.1, some IIE values are fixed */
  1592. if (IS_GEN_IIE(hpriv)) {
  1593. m2 &= ~0xC30FF01F;
  1594. m2 |= 0x0000900F;
  1595. }
  1596. writel(m2, port_mmio + PHY_MODE2);
  1597. }
  1598. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  1599. unsigned int port_no)
  1600. {
  1601. void __iomem *port_mmio = mv_port_base(mmio, port_no);
  1602. writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
  1603. if (IS_60XX(hpriv)) {
  1604. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1605. ifctl |= (1 << 7); /* enable gen2i speed */
  1606. ifctl = (ifctl & 0xfff) | 0x9b1000; /* from chip spec */
  1607. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1608. }
  1609. udelay(25); /* allow reset propagation */
  1610. /* Spec never mentions clearing the bit. Marvell's driver does
  1611. * clear the bit, however.
  1612. */
  1613. writelfl(0, port_mmio + EDMA_CMD_OFS);
  1614. hpriv->ops->phy_errata(hpriv, mmio, port_no);
  1615. if (IS_50XX(hpriv))
  1616. mdelay(1);
  1617. }
  1618. static void mv_stop_and_reset(struct ata_port *ap)
  1619. {
  1620. struct mv_host_priv *hpriv = ap->host_set->private_data;
  1621. void __iomem *mmio = ap->host_set->mmio_base;
  1622. mv_stop_dma(ap);
  1623. mv_channel_reset(hpriv, mmio, ap->port_no);
  1624. __mv_phy_reset(ap, 0);
  1625. }
  1626. static inline void __msleep(unsigned int msec, int can_sleep)
  1627. {
  1628. if (can_sleep)
  1629. msleep(msec);
  1630. else
  1631. mdelay(msec);
  1632. }
  1633. /**
  1634. * __mv_phy_reset - Perform eDMA reset followed by COMRESET
  1635. * @ap: ATA channel to manipulate
  1636. *
  1637. * Part of this is taken from __sata_phy_reset and modified to
  1638. * not sleep since this routine gets called from interrupt level.
  1639. *
  1640. * LOCKING:
  1641. * Inherited from caller. This is coded to safe to call at
  1642. * interrupt level, i.e. it does not sleep.
  1643. */
  1644. static void __mv_phy_reset(struct ata_port *ap, int can_sleep)
  1645. {
  1646. struct mv_port_priv *pp = ap->private_data;
  1647. struct mv_host_priv *hpriv = ap->host_set->private_data;
  1648. void __iomem *port_mmio = mv_ap_base(ap);
  1649. struct ata_taskfile tf;
  1650. struct ata_device *dev = &ap->device[0];
  1651. unsigned long timeout;
  1652. int retry = 5;
  1653. u32 sstatus;
  1654. VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
  1655. DPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
  1656. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1657. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1658. /* Issue COMRESET via SControl */
  1659. comreset_retry:
  1660. scr_write_flush(ap, SCR_CONTROL, 0x301);
  1661. __msleep(1, can_sleep);
  1662. scr_write_flush(ap, SCR_CONTROL, 0x300);
  1663. __msleep(20, can_sleep);
  1664. timeout = jiffies + msecs_to_jiffies(200);
  1665. do {
  1666. sstatus = scr_read(ap, SCR_STATUS) & 0x3;
  1667. if ((sstatus == 3) || (sstatus == 0))
  1668. break;
  1669. __msleep(1, can_sleep);
  1670. } while (time_before(jiffies, timeout));
  1671. /* work around errata */
  1672. if (IS_60XX(hpriv) &&
  1673. (sstatus != 0x0) && (sstatus != 0x113) && (sstatus != 0x123) &&
  1674. (retry-- > 0))
  1675. goto comreset_retry;
  1676. DPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
  1677. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1678. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1679. if (sata_dev_present(ap)) {
  1680. ata_port_probe(ap);
  1681. } else {
  1682. printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
  1683. ap->id, scr_read(ap, SCR_STATUS));
  1684. ata_port_disable(ap);
  1685. return;
  1686. }
  1687. ap->cbl = ATA_CBL_SATA;
  1688. /* even after SStatus reflects that device is ready,
  1689. * it seems to take a while for link to be fully
  1690. * established (and thus Status no longer 0x80/0x7F),
  1691. * so we poll a bit for that, here.
  1692. */
  1693. retry = 20;
  1694. while (1) {
  1695. u8 drv_stat = ata_check_status(ap);
  1696. if ((drv_stat != 0x80) && (drv_stat != 0x7f))
  1697. break;
  1698. __msleep(500, can_sleep);
  1699. if (retry-- <= 0)
  1700. break;
  1701. }
  1702. tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
  1703. tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
  1704. tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
  1705. tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
  1706. dev->class = ata_dev_classify(&tf);
  1707. if (!ata_dev_present(dev)) {
  1708. VPRINTK("Port disabled post-sig: No device present.\n");
  1709. ata_port_disable(ap);
  1710. }
  1711. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1712. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1713. VPRINTK("EXIT\n");
  1714. }
  1715. static void mv_phy_reset(struct ata_port *ap)
  1716. {
  1717. __mv_phy_reset(ap, 1);
  1718. }
  1719. /**
  1720. * mv_eng_timeout - Routine called by libata when SCSI times out I/O
  1721. * @ap: ATA channel to manipulate
  1722. *
  1723. * Intent is to clear all pending error conditions, reset the
  1724. * chip/bus, fail the command, and move on.
  1725. *
  1726. * LOCKING:
  1727. * This routine holds the host_set lock while failing the command.
  1728. */
  1729. static void mv_eng_timeout(struct ata_port *ap)
  1730. {
  1731. struct ata_queued_cmd *qc;
  1732. printk(KERN_ERR "ata%u: Entering mv_eng_timeout\n",ap->id);
  1733. DPRINTK("All regs @ start of eng_timeout\n");
  1734. mv_dump_all_regs(ap->host_set->mmio_base, ap->port_no,
  1735. to_pci_dev(ap->host_set->dev));
  1736. qc = ata_qc_from_tag(ap, ap->active_tag);
  1737. printk(KERN_ERR "mmio_base %p ap %p qc %p scsi_cmnd %p &cmnd %p\n",
  1738. ap->host_set->mmio_base, ap, qc, qc->scsicmd,
  1739. &qc->scsicmd->cmnd);
  1740. mv_err_intr(ap, 0);
  1741. mv_stop_and_reset(ap);
  1742. WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
  1743. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  1744. qc->err_mask |= AC_ERR_TIMEOUT;
  1745. ata_eh_qc_complete(qc);
  1746. }
  1747. }
  1748. /**
  1749. * mv_port_init - Perform some early initialization on a single port.
  1750. * @port: libata data structure storing shadow register addresses
  1751. * @port_mmio: base address of the port
  1752. *
  1753. * Initialize shadow register mmio addresses, clear outstanding
  1754. * interrupts on the port, and unmask interrupts for the future
  1755. * start of the port.
  1756. *
  1757. * LOCKING:
  1758. * Inherited from caller.
  1759. */
  1760. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  1761. {
  1762. unsigned long shd_base = (unsigned long) port_mmio + SHD_BLK_OFS;
  1763. unsigned serr_ofs;
  1764. /* PIO related setup
  1765. */
  1766. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  1767. port->error_addr =
  1768. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  1769. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  1770. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  1771. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  1772. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  1773. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  1774. port->status_addr =
  1775. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  1776. /* special case: control/altstatus doesn't have ATA_REG_ address */
  1777. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
  1778. /* unused: */
  1779. port->cmd_addr = port->bmdma_addr = port->scr_addr = 0;
  1780. /* Clear any currently outstanding port interrupt conditions */
  1781. serr_ofs = mv_scr_offset(SCR_ERROR);
  1782. writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
  1783. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1784. /* unmask all EDMA error interrupts */
  1785. writelfl(~0, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
  1786. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  1787. readl(port_mmio + EDMA_CFG_OFS),
  1788. readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
  1789. readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
  1790. }
  1791. static int mv_chip_id(struct pci_dev *pdev, struct mv_host_priv *hpriv,
  1792. unsigned int board_idx)
  1793. {
  1794. u8 rev_id;
  1795. u32 hp_flags = hpriv->hp_flags;
  1796. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1797. switch(board_idx) {
  1798. case chip_5080:
  1799. hpriv->ops = &mv5xxx_ops;
  1800. hp_flags |= MV_HP_50XX;
  1801. switch (rev_id) {
  1802. case 0x1:
  1803. hp_flags |= MV_HP_ERRATA_50XXB0;
  1804. break;
  1805. case 0x3:
  1806. hp_flags |= MV_HP_ERRATA_50XXB2;
  1807. break;
  1808. default:
  1809. dev_printk(KERN_WARNING, &pdev->dev,
  1810. "Applying 50XXB2 workarounds to unknown rev\n");
  1811. hp_flags |= MV_HP_ERRATA_50XXB2;
  1812. break;
  1813. }
  1814. break;
  1815. case chip_504x:
  1816. case chip_508x:
  1817. hpriv->ops = &mv5xxx_ops;
  1818. hp_flags |= MV_HP_50XX;
  1819. switch (rev_id) {
  1820. case 0x0:
  1821. hp_flags |= MV_HP_ERRATA_50XXB0;
  1822. break;
  1823. case 0x3:
  1824. hp_flags |= MV_HP_ERRATA_50XXB2;
  1825. break;
  1826. default:
  1827. dev_printk(KERN_WARNING, &pdev->dev,
  1828. "Applying B2 workarounds to unknown rev\n");
  1829. hp_flags |= MV_HP_ERRATA_50XXB2;
  1830. break;
  1831. }
  1832. break;
  1833. case chip_604x:
  1834. case chip_608x:
  1835. hpriv->ops = &mv6xxx_ops;
  1836. switch (rev_id) {
  1837. case 0x7:
  1838. hp_flags |= MV_HP_ERRATA_60X1B2;
  1839. break;
  1840. case 0x9:
  1841. hp_flags |= MV_HP_ERRATA_60X1C0;
  1842. break;
  1843. default:
  1844. dev_printk(KERN_WARNING, &pdev->dev,
  1845. "Applying B2 workarounds to unknown rev\n");
  1846. hp_flags |= MV_HP_ERRATA_60X1B2;
  1847. break;
  1848. }
  1849. break;
  1850. case chip_7042:
  1851. case chip_6042:
  1852. hpriv->ops = &mv6xxx_ops;
  1853. hp_flags |= MV_HP_GEN_IIE;
  1854. switch (rev_id) {
  1855. case 0x0:
  1856. hp_flags |= MV_HP_ERRATA_XX42A0;
  1857. break;
  1858. case 0x1:
  1859. hp_flags |= MV_HP_ERRATA_60X1C0;
  1860. break;
  1861. default:
  1862. dev_printk(KERN_WARNING, &pdev->dev,
  1863. "Applying 60X1C0 workarounds to unknown rev\n");
  1864. hp_flags |= MV_HP_ERRATA_60X1C0;
  1865. break;
  1866. }
  1867. break;
  1868. default:
  1869. printk(KERN_ERR DRV_NAME ": BUG: invalid board index %u\n", board_idx);
  1870. return 1;
  1871. }
  1872. hpriv->hp_flags = hp_flags;
  1873. return 0;
  1874. }
  1875. /**
  1876. * mv_init_host - Perform some early initialization of the host.
  1877. * @pdev: host PCI device
  1878. * @probe_ent: early data struct representing the host
  1879. *
  1880. * If possible, do an early global reset of the host. Then do
  1881. * our port init and clear/unmask all/relevant host interrupts.
  1882. *
  1883. * LOCKING:
  1884. * Inherited from caller.
  1885. */
  1886. static int mv_init_host(struct pci_dev *pdev, struct ata_probe_ent *probe_ent,
  1887. unsigned int board_idx)
  1888. {
  1889. int rc = 0, n_hc, port, hc;
  1890. void __iomem *mmio = probe_ent->mmio_base;
  1891. struct mv_host_priv *hpriv = probe_ent->private_data;
  1892. /* global interrupt mask */
  1893. writel(0, mmio + HC_MAIN_IRQ_MASK_OFS);
  1894. rc = mv_chip_id(pdev, hpriv, board_idx);
  1895. if (rc)
  1896. goto done;
  1897. n_hc = mv_get_hc_count(probe_ent->host_flags);
  1898. probe_ent->n_ports = MV_PORTS_PER_HC * n_hc;
  1899. for (port = 0; port < probe_ent->n_ports; port++)
  1900. hpriv->ops->read_preamp(hpriv, port, mmio);
  1901. rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
  1902. if (rc)
  1903. goto done;
  1904. hpriv->ops->reset_flash(hpriv, mmio);
  1905. hpriv->ops->reset_bus(pdev, mmio);
  1906. hpriv->ops->enable_leds(hpriv, mmio);
  1907. for (port = 0; port < probe_ent->n_ports; port++) {
  1908. if (IS_60XX(hpriv)) {
  1909. void __iomem *port_mmio = mv_port_base(mmio, port);
  1910. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1911. ifctl |= (1 << 7); /* enable gen2i speed */
  1912. ifctl = (ifctl & 0xfff) | 0x9b1000; /* from chip spec */
  1913. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1914. }
  1915. hpriv->ops->phy_errata(hpriv, mmio, port);
  1916. }
  1917. for (port = 0; port < probe_ent->n_ports; port++) {
  1918. void __iomem *port_mmio = mv_port_base(mmio, port);
  1919. mv_port_init(&probe_ent->port[port], port_mmio);
  1920. }
  1921. for (hc = 0; hc < n_hc; hc++) {
  1922. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1923. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  1924. "(before clear)=0x%08x\n", hc,
  1925. readl(hc_mmio + HC_CFG_OFS),
  1926. readl(hc_mmio + HC_IRQ_CAUSE_OFS));
  1927. /* Clear any currently outstanding hc interrupt conditions */
  1928. writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
  1929. }
  1930. /* Clear any currently outstanding host interrupt conditions */
  1931. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1932. /* and unmask interrupt generation for host regs */
  1933. writelfl(PCI_UNMASK_ALL_IRQS, mmio + PCI_IRQ_MASK_OFS);
  1934. writelfl(~HC_MAIN_MASKED_IRQS, mmio + HC_MAIN_IRQ_MASK_OFS);
  1935. VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
  1936. "PCI int cause/mask=0x%08x/0x%08x\n",
  1937. readl(mmio + HC_MAIN_IRQ_CAUSE_OFS),
  1938. readl(mmio + HC_MAIN_IRQ_MASK_OFS),
  1939. readl(mmio + PCI_IRQ_CAUSE_OFS),
  1940. readl(mmio + PCI_IRQ_MASK_OFS));
  1941. done:
  1942. return rc;
  1943. }
  1944. /**
  1945. * mv_print_info - Dump key info to kernel log for perusal.
  1946. * @probe_ent: early data struct representing the host
  1947. *
  1948. * FIXME: complete this.
  1949. *
  1950. * LOCKING:
  1951. * Inherited from caller.
  1952. */
  1953. static void mv_print_info(struct ata_probe_ent *probe_ent)
  1954. {
  1955. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1956. struct mv_host_priv *hpriv = probe_ent->private_data;
  1957. u8 rev_id, scc;
  1958. const char *scc_s;
  1959. /* Use this to determine the HW stepping of the chip so we know
  1960. * what errata to workaround
  1961. */
  1962. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1963. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  1964. if (scc == 0)
  1965. scc_s = "SCSI";
  1966. else if (scc == 0x01)
  1967. scc_s = "RAID";
  1968. else
  1969. scc_s = "unknown";
  1970. dev_printk(KERN_INFO, &pdev->dev,
  1971. "%u slots %u ports %s mode IRQ via %s\n",
  1972. (unsigned)MV_MAX_Q_DEPTH, probe_ent->n_ports,
  1973. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  1974. }
  1975. /**
  1976. * mv_init_one - handle a positive probe of a Marvell host
  1977. * @pdev: PCI device found
  1978. * @ent: PCI device ID entry for the matched host
  1979. *
  1980. * LOCKING:
  1981. * Inherited from caller.
  1982. */
  1983. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1984. {
  1985. static int printed_version = 0;
  1986. struct ata_probe_ent *probe_ent = NULL;
  1987. struct mv_host_priv *hpriv;
  1988. unsigned int board_idx = (unsigned int)ent->driver_data;
  1989. void __iomem *mmio_base;
  1990. int pci_dev_busy = 0, rc;
  1991. if (!printed_version++)
  1992. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  1993. rc = pci_enable_device(pdev);
  1994. if (rc) {
  1995. return rc;
  1996. }
  1997. pci_set_master(pdev);
  1998. rc = pci_request_regions(pdev, DRV_NAME);
  1999. if (rc) {
  2000. pci_dev_busy = 1;
  2001. goto err_out;
  2002. }
  2003. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  2004. if (probe_ent == NULL) {
  2005. rc = -ENOMEM;
  2006. goto err_out_regions;
  2007. }
  2008. memset(probe_ent, 0, sizeof(*probe_ent));
  2009. probe_ent->dev = pci_dev_to_dev(pdev);
  2010. INIT_LIST_HEAD(&probe_ent->node);
  2011. mmio_base = pci_iomap(pdev, MV_PRIMARY_BAR, 0);
  2012. if (mmio_base == NULL) {
  2013. rc = -ENOMEM;
  2014. goto err_out_free_ent;
  2015. }
  2016. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  2017. if (!hpriv) {
  2018. rc = -ENOMEM;
  2019. goto err_out_iounmap;
  2020. }
  2021. memset(hpriv, 0, sizeof(*hpriv));
  2022. probe_ent->sht = mv_port_info[board_idx].sht;
  2023. probe_ent->host_flags = mv_port_info[board_idx].host_flags;
  2024. probe_ent->pio_mask = mv_port_info[board_idx].pio_mask;
  2025. probe_ent->udma_mask = mv_port_info[board_idx].udma_mask;
  2026. probe_ent->port_ops = mv_port_info[board_idx].port_ops;
  2027. probe_ent->irq = pdev->irq;
  2028. probe_ent->irq_flags = SA_SHIRQ;
  2029. probe_ent->mmio_base = mmio_base;
  2030. probe_ent->private_data = hpriv;
  2031. /* initialize adapter */
  2032. rc = mv_init_host(pdev, probe_ent, board_idx);
  2033. if (rc) {
  2034. goto err_out_hpriv;
  2035. }
  2036. /* Enable interrupts */
  2037. if (msi && pci_enable_msi(pdev) == 0) {
  2038. hpriv->hp_flags |= MV_HP_FLAG_MSI;
  2039. } else {
  2040. pci_intx(pdev, 1);
  2041. }
  2042. mv_dump_pci_cfg(pdev, 0x68);
  2043. mv_print_info(probe_ent);
  2044. if (ata_device_add(probe_ent) == 0) {
  2045. rc = -ENODEV; /* No devices discovered */
  2046. goto err_out_dev_add;
  2047. }
  2048. kfree(probe_ent);
  2049. return 0;
  2050. err_out_dev_add:
  2051. if (MV_HP_FLAG_MSI & hpriv->hp_flags) {
  2052. pci_disable_msi(pdev);
  2053. } else {
  2054. pci_intx(pdev, 0);
  2055. }
  2056. err_out_hpriv:
  2057. kfree(hpriv);
  2058. err_out_iounmap:
  2059. pci_iounmap(pdev, mmio_base);
  2060. err_out_free_ent:
  2061. kfree(probe_ent);
  2062. err_out_regions:
  2063. pci_release_regions(pdev);
  2064. err_out:
  2065. if (!pci_dev_busy) {
  2066. pci_disable_device(pdev);
  2067. }
  2068. return rc;
  2069. }
  2070. static int __init mv_init(void)
  2071. {
  2072. return pci_module_init(&mv_pci_driver);
  2073. }
  2074. static void __exit mv_exit(void)
  2075. {
  2076. pci_unregister_driver(&mv_pci_driver);
  2077. }
  2078. MODULE_AUTHOR("Brett Russ");
  2079. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  2080. MODULE_LICENSE("GPL");
  2081. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  2082. MODULE_VERSION(DRV_VERSION);
  2083. module_param(msi, int, 0444);
  2084. MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
  2085. module_init(mv_init);
  2086. module_exit(mv_exit);