sata_mv.c 53 KB

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