sata_mv.c 64 KB

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