sata_mv.c 59 KB

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