sata_mv.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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 "scsi.h"
  32. #include <scsi/scsi_host.h>
  33. #include <linux/libata.h>
  34. #include <asm/io.h>
  35. #define DRV_NAME "sata_mv"
  36. #define DRV_VERSION "0.23"
  37. enum {
  38. /* BAR's are enumerated in terms of pci_resource_start() terms */
  39. MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
  40. MV_IO_BAR = 2, /* offset 0x18: IO space */
  41. MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
  42. MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
  43. MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
  44. MV_PCI_REG_BASE = 0,
  45. MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
  46. MV_SATAHC0_REG_BASE = 0x20000,
  47. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  48. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  49. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  50. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  51. MV_USE_Q_DEPTH = ATA_DEF_QUEUE,
  52. MV_MAX_Q_DEPTH = 32,
  53. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  54. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  55. * CRPB needs alignment on a 256B boundary. Size == 256B
  56. * SG count of 176 leads to MV_PORT_PRIV_DMA_SZ == 4KB
  57. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  58. */
  59. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  60. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  61. MV_MAX_SG_CT = 176,
  62. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  63. MV_PORT_PRIV_DMA_SZ = (MV_CRQB_Q_SZ + MV_CRPB_Q_SZ + MV_SG_TBL_SZ),
  64. /* Our DMA boundary is determined by an ePRD being unable to handle
  65. * anything larger than 64KB
  66. */
  67. MV_DMA_BOUNDARY = 0xffffU,
  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_FLAG_GLBL_SFT_RST = (1 << 28), /* Global Soft Reset support */
  77. MV_COMMON_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  78. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO),
  79. MV_6XXX_FLAGS = (MV_FLAG_IRQ_COALESCE |
  80. MV_FLAG_GLBL_SFT_RST),
  81. chip_504x = 0,
  82. chip_508x = 1,
  83. chip_604x = 2,
  84. chip_608x = 3,
  85. CRQB_FLAG_READ = (1 << 0),
  86. CRQB_TAG_SHIFT = 1,
  87. CRQB_CMD_ADDR_SHIFT = 8,
  88. CRQB_CMD_CS = (0x2 << 11),
  89. CRQB_CMD_LAST = (1 << 15),
  90. CRPB_FLAG_STATUS_SHIFT = 8,
  91. EPRD_FLAG_END_OF_TBL = (1 << 31),
  92. /* PCI interface registers */
  93. PCI_COMMAND_OFS = 0xc00,
  94. PCI_MAIN_CMD_STS_OFS = 0xd30,
  95. STOP_PCI_MASTER = (1 << 2),
  96. PCI_MASTER_EMPTY = (1 << 3),
  97. GLOB_SFT_RST = (1 << 4),
  98. PCI_IRQ_CAUSE_OFS = 0x1d58,
  99. PCI_IRQ_MASK_OFS = 0x1d5c,
  100. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  101. HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
  102. HC_MAIN_IRQ_MASK_OFS = 0x1d64,
  103. PORT0_ERR = (1 << 0), /* shift by port # */
  104. PORT0_DONE = (1 << 1), /* shift by port # */
  105. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  106. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  107. PCI_ERR = (1 << 18),
  108. TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
  109. TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
  110. PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
  111. GPIO_INT = (1 << 22),
  112. SELF_INT = (1 << 23),
  113. TWSI_INT = (1 << 24),
  114. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  115. HC_MAIN_MASKED_IRQS = (TRAN_LO_DONE | TRAN_HI_DONE |
  116. PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
  117. HC_MAIN_RSVD),
  118. /* SATAHC registers */
  119. HC_CFG_OFS = 0,
  120. HC_IRQ_CAUSE_OFS = 0x14,
  121. CRPB_DMA_DONE = (1 << 0), /* shift by port # */
  122. HC_IRQ_COAL = (1 << 4), /* IRQ coalescing */
  123. DEV_IRQ = (1 << 8), /* shift by port # */
  124. /* Shadow block registers */
  125. SHD_BLK_OFS = 0x100,
  126. SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
  127. /* SATA registers */
  128. SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
  129. SATA_ACTIVE_OFS = 0x350,
  130. /* Port registers */
  131. EDMA_CFG_OFS = 0,
  132. EDMA_CFG_Q_DEPTH = 0, /* queueing disabled */
  133. EDMA_CFG_NCQ = (1 << 5),
  134. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  135. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  136. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  137. EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
  138. EDMA_ERR_IRQ_MASK_OFS = 0xc,
  139. EDMA_ERR_D_PAR = (1 << 0),
  140. EDMA_ERR_PRD_PAR = (1 << 1),
  141. EDMA_ERR_DEV = (1 << 2),
  142. EDMA_ERR_DEV_DCON = (1 << 3),
  143. EDMA_ERR_DEV_CON = (1 << 4),
  144. EDMA_ERR_SERR = (1 << 5),
  145. EDMA_ERR_SELF_DIS = (1 << 7),
  146. EDMA_ERR_BIST_ASYNC = (1 << 8),
  147. EDMA_ERR_CRBQ_PAR = (1 << 9),
  148. EDMA_ERR_CRPB_PAR = (1 << 10),
  149. EDMA_ERR_INTRL_PAR = (1 << 11),
  150. EDMA_ERR_IORDY = (1 << 12),
  151. EDMA_ERR_LNK_CTRL_RX = (0xf << 13),
  152. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15),
  153. EDMA_ERR_LNK_DATA_RX = (0xf << 17),
  154. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21),
  155. EDMA_ERR_LNK_DATA_TX = (0x1f << 26),
  156. EDMA_ERR_TRANS_PROTO = (1 << 31),
  157. EDMA_ERR_FATAL = (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  158. EDMA_ERR_DEV_DCON | EDMA_ERR_CRBQ_PAR |
  159. EDMA_ERR_CRPB_PAR | EDMA_ERR_INTRL_PAR |
  160. EDMA_ERR_IORDY | EDMA_ERR_LNK_CTRL_RX_2 |
  161. EDMA_ERR_LNK_DATA_RX |
  162. EDMA_ERR_LNK_DATA_TX |
  163. EDMA_ERR_TRANS_PROTO),
  164. EDMA_REQ_Q_BASE_HI_OFS = 0x10,
  165. EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
  166. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  167. EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
  168. EDMA_REQ_Q_PTR_SHIFT = 5,
  169. EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
  170. EDMA_RSP_Q_IN_PTR_OFS = 0x20,
  171. EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
  172. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  173. EDMA_RSP_Q_PTR_SHIFT = 3,
  174. EDMA_CMD_OFS = 0x28,
  175. EDMA_EN = (1 << 0),
  176. EDMA_DS = (1 << 1),
  177. ATA_RST = (1 << 2),
  178. /* Host private flags (hp_flags) */
  179. MV_HP_FLAG_MSI = (1 << 0),
  180. /* Port private flags (pp_flags) */
  181. MV_PP_FLAG_EDMA_EN = (1 << 0),
  182. MV_PP_FLAG_EDMA_DS_ACT = (1 << 1),
  183. };
  184. /* Command ReQuest Block: 32B */
  185. struct mv_crqb {
  186. u32 sg_addr;
  187. u32 sg_addr_hi;
  188. u16 ctrl_flags;
  189. u16 ata_cmd[11];
  190. };
  191. /* Command ResPonse Block: 8B */
  192. struct mv_crpb {
  193. u16 id;
  194. u16 flags;
  195. u32 tmstmp;
  196. };
  197. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  198. struct mv_sg {
  199. u32 addr;
  200. u32 flags_size;
  201. u32 addr_hi;
  202. u32 reserved;
  203. };
  204. struct mv_port_priv {
  205. struct mv_crqb *crqb;
  206. dma_addr_t crqb_dma;
  207. struct mv_crpb *crpb;
  208. dma_addr_t crpb_dma;
  209. struct mv_sg *sg_tbl;
  210. dma_addr_t sg_tbl_dma;
  211. unsigned req_producer; /* cp of req_in_ptr */
  212. unsigned rsp_consumer; /* cp of rsp_out_ptr */
  213. u32 pp_flags;
  214. };
  215. struct mv_host_priv {
  216. u32 hp_flags;
  217. };
  218. static void mv_irq_clear(struct ata_port *ap);
  219. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  220. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  221. static u8 mv_check_err(struct ata_port *ap);
  222. static void mv_phy_reset(struct ata_port *ap);
  223. static void mv_host_stop(struct ata_host_set *host_set);
  224. static int mv_port_start(struct ata_port *ap);
  225. static void mv_port_stop(struct ata_port *ap);
  226. static void mv_qc_prep(struct ata_queued_cmd *qc);
  227. static int mv_qc_issue(struct ata_queued_cmd *qc);
  228. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  229. struct pt_regs *regs);
  230. static void mv_eng_timeout(struct ata_port *ap);
  231. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  232. static Scsi_Host_Template mv_sht = {
  233. .module = THIS_MODULE,
  234. .name = DRV_NAME,
  235. .ioctl = ata_scsi_ioctl,
  236. .queuecommand = ata_scsi_queuecmd,
  237. .eh_strategy_handler = ata_scsi_error,
  238. .can_queue = MV_USE_Q_DEPTH,
  239. .this_id = ATA_SHT_THIS_ID,
  240. .sg_tablesize = MV_MAX_SG_CT,
  241. .max_sectors = ATA_MAX_SECTORS,
  242. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  243. .emulated = ATA_SHT_EMULATED,
  244. .use_clustering = ATA_SHT_USE_CLUSTERING,
  245. .proc_name = DRV_NAME,
  246. .dma_boundary = MV_DMA_BOUNDARY,
  247. .slave_configure = ata_scsi_slave_config,
  248. .bios_param = ata_std_bios_param,
  249. .ordered_flush = 1,
  250. };
  251. static struct ata_port_operations mv_ops = {
  252. .port_disable = ata_port_disable,
  253. .tf_load = ata_tf_load,
  254. .tf_read = ata_tf_read,
  255. .check_status = ata_check_status,
  256. .check_err = mv_check_err,
  257. .exec_command = ata_exec_command,
  258. .dev_select = ata_std_dev_select,
  259. .phy_reset = mv_phy_reset,
  260. .qc_prep = mv_qc_prep,
  261. .qc_issue = mv_qc_issue,
  262. .eng_timeout = mv_eng_timeout,
  263. .irq_handler = mv_interrupt,
  264. .irq_clear = mv_irq_clear,
  265. .scr_read = mv_scr_read,
  266. .scr_write = mv_scr_write,
  267. .port_start = mv_port_start,
  268. .port_stop = mv_port_stop,
  269. .host_stop = mv_host_stop,
  270. };
  271. static struct ata_port_info mv_port_info[] = {
  272. { /* chip_504x */
  273. .sht = &mv_sht,
  274. .host_flags = MV_COMMON_FLAGS,
  275. .pio_mask = 0x1f, /* pio0-4 */
  276. .udma_mask = 0, /* 0x7f (udma0-6 disabled for now) */
  277. .port_ops = &mv_ops,
  278. },
  279. { /* chip_508x */
  280. .sht = &mv_sht,
  281. .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
  282. .pio_mask = 0x1f, /* pio0-4 */
  283. .udma_mask = 0, /* 0x7f (udma0-6 disabled for now) */
  284. .port_ops = &mv_ops,
  285. },
  286. { /* chip_604x */
  287. .sht = &mv_sht,
  288. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
  289. .pio_mask = 0x1f, /* pio0-4 */
  290. .udma_mask = 0x7f, /* udma0-6 */
  291. .port_ops = &mv_ops,
  292. },
  293. { /* chip_608x */
  294. .sht = &mv_sht,
  295. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  296. MV_FLAG_DUAL_HC),
  297. .pio_mask = 0x1f, /* pio0-4 */
  298. .udma_mask = 0x7f, /* udma0-6 */
  299. .port_ops = &mv_ops,
  300. },
  301. };
  302. static struct pci_device_id mv_pci_tbl[] = {
  303. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5040), 0, 0, chip_504x},
  304. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5041), 0, 0, chip_504x},
  305. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5080), 0, 0, chip_508x},
  306. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5081), 0, 0, chip_508x},
  307. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6040), 0, 0, chip_604x},
  308. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6041), 0, 0, chip_604x},
  309. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6080), 0, 0, chip_608x},
  310. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6081), 0, 0, chip_608x},
  311. {} /* terminate list */
  312. };
  313. static struct pci_driver mv_pci_driver = {
  314. .name = DRV_NAME,
  315. .id_table = mv_pci_tbl,
  316. .probe = mv_init_one,
  317. .remove = ata_pci_remove_one,
  318. };
  319. /*
  320. * Functions
  321. */
  322. static inline void writelfl(unsigned long data, void __iomem *addr)
  323. {
  324. writel(data, addr);
  325. (void) readl(addr); /* flush to avoid PCI posted write */
  326. }
  327. static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
  328. {
  329. return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
  330. }
  331. static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
  332. {
  333. return (mv_hc_base(base, port >> MV_PORT_HC_SHIFT) +
  334. MV_SATAHC_ARBTR_REG_SZ +
  335. ((port & MV_PORT_MASK) * MV_PORT_REG_SZ));
  336. }
  337. static inline void __iomem *mv_ap_base(struct ata_port *ap)
  338. {
  339. return mv_port_base(ap->host_set->mmio_base, ap->port_no);
  340. }
  341. static inline int mv_get_hc_count(unsigned long hp_flags)
  342. {
  343. return ((hp_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
  344. }
  345. static void mv_irq_clear(struct ata_port *ap)
  346. {
  347. }
  348. static void mv_start_dma(void __iomem *base, struct mv_port_priv *pp)
  349. {
  350. if (!(MV_PP_FLAG_EDMA_EN & pp->pp_flags)) {
  351. writelfl(EDMA_EN, base + EDMA_CMD_OFS);
  352. pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
  353. }
  354. assert(EDMA_EN & readl(base + EDMA_CMD_OFS));
  355. }
  356. static void mv_stop_dma(struct ata_port *ap)
  357. {
  358. void __iomem *port_mmio = mv_ap_base(ap);
  359. struct mv_port_priv *pp = ap->private_data;
  360. u32 reg;
  361. int i;
  362. if (MV_PP_FLAG_EDMA_EN & pp->pp_flags) {
  363. /* Disable EDMA if active. The disable bit auto clears.
  364. */
  365. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  366. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  367. } else {
  368. assert(!(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS)));
  369. }
  370. /* now properly wait for the eDMA to stop */
  371. for (i = 1000; i > 0; i--) {
  372. reg = readl(port_mmio + EDMA_CMD_OFS);
  373. if (!(EDMA_EN & reg)) {
  374. break;
  375. }
  376. udelay(100);
  377. }
  378. if (EDMA_EN & reg) {
  379. printk(KERN_ERR "ata%u: Unable to stop eDMA\n", ap->id);
  380. /* FIXME: Consider doing a reset here to recover */
  381. }
  382. }
  383. static void mv_dump_mem(void __iomem *start, unsigned bytes)
  384. {
  385. #ifdef ATA_DEBUG
  386. int b, w;
  387. for (b = 0; b < bytes; ) {
  388. DPRINTK("%p: ", start + b);
  389. for (w = 0; b < bytes && w < 4; w++) {
  390. printk("%08x ",readl(start + b));
  391. b += sizeof(u32);
  392. }
  393. printk("\n");
  394. }
  395. #endif
  396. }
  397. static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
  398. {
  399. #ifdef ATA_DEBUG
  400. int b, w;
  401. u32 dw;
  402. for (b = 0; b < bytes; ) {
  403. DPRINTK("%02x: ", b);
  404. for (w = 0; b < bytes && w < 4; w++) {
  405. (void) pci_read_config_dword(pdev,b,&dw);
  406. printk("%08x ",dw);
  407. b += sizeof(u32);
  408. }
  409. printk("\n");
  410. }
  411. #endif
  412. }
  413. static void mv_dump_all_regs(void __iomem *mmio_base, int port,
  414. struct pci_dev *pdev)
  415. {
  416. #ifdef ATA_DEBUG
  417. void __iomem *hc_base = mv_hc_base(mmio_base,
  418. port >> MV_PORT_HC_SHIFT);
  419. void __iomem *port_base;
  420. int start_port, num_ports, p, start_hc, num_hcs, hc;
  421. if (0 > port) {
  422. start_hc = start_port = 0;
  423. num_ports = 8; /* shld be benign for 4 port devs */
  424. num_hcs = 2;
  425. } else {
  426. start_hc = port >> MV_PORT_HC_SHIFT;
  427. start_port = port;
  428. num_ports = num_hcs = 1;
  429. }
  430. DPRINTK("All registers for port(s) %u-%u:\n", start_port,
  431. num_ports > 1 ? num_ports - 1 : start_port);
  432. if (NULL != pdev) {
  433. DPRINTK("PCI config space regs:\n");
  434. mv_dump_pci_cfg(pdev, 0x68);
  435. }
  436. DPRINTK("PCI regs:\n");
  437. mv_dump_mem(mmio_base+0xc00, 0x3c);
  438. mv_dump_mem(mmio_base+0xd00, 0x34);
  439. mv_dump_mem(mmio_base+0xf00, 0x4);
  440. mv_dump_mem(mmio_base+0x1d00, 0x6c);
  441. for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
  442. hc_base = mv_hc_base(mmio_base, port >> MV_PORT_HC_SHIFT);
  443. DPRINTK("HC regs (HC %i):\n", hc);
  444. mv_dump_mem(hc_base, 0x1c);
  445. }
  446. for (p = start_port; p < start_port + num_ports; p++) {
  447. port_base = mv_port_base(mmio_base, p);
  448. DPRINTK("EDMA regs (port %i):\n",p);
  449. mv_dump_mem(port_base, 0x54);
  450. DPRINTK("SATA regs (port %i):\n",p);
  451. mv_dump_mem(port_base+0x300, 0x60);
  452. }
  453. #endif
  454. }
  455. static unsigned int mv_scr_offset(unsigned int sc_reg_in)
  456. {
  457. unsigned int ofs;
  458. switch (sc_reg_in) {
  459. case SCR_STATUS:
  460. case SCR_CONTROL:
  461. case SCR_ERROR:
  462. ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
  463. break;
  464. case SCR_ACTIVE:
  465. ofs = SATA_ACTIVE_OFS; /* active is not with the others */
  466. break;
  467. default:
  468. ofs = 0xffffffffU;
  469. break;
  470. }
  471. return ofs;
  472. }
  473. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  474. {
  475. unsigned int ofs = mv_scr_offset(sc_reg_in);
  476. if (0xffffffffU != ofs) {
  477. return readl(mv_ap_base(ap) + ofs);
  478. } else {
  479. return (u32) ofs;
  480. }
  481. }
  482. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  483. {
  484. unsigned int ofs = mv_scr_offset(sc_reg_in);
  485. if (0xffffffffU != ofs) {
  486. writelfl(val, mv_ap_base(ap) + ofs);
  487. }
  488. }
  489. /* This routine only applies to 6xxx parts */
  490. static int mv_global_soft_reset(void __iomem *mmio_base)
  491. {
  492. void __iomem *reg = mmio_base + PCI_MAIN_CMD_STS_OFS;
  493. int i, rc = 0;
  494. u32 t;
  495. /* Following procedure defined in PCI "main command and status
  496. * register" table.
  497. */
  498. t = readl(reg);
  499. writel(t | STOP_PCI_MASTER, reg);
  500. for (i = 0; i < 1000; i++) {
  501. udelay(1);
  502. t = readl(reg);
  503. if (PCI_MASTER_EMPTY & t) {
  504. break;
  505. }
  506. }
  507. if (!(PCI_MASTER_EMPTY & t)) {
  508. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  509. rc = 1;
  510. goto done;
  511. }
  512. /* set reset */
  513. i = 5;
  514. do {
  515. writel(t | GLOB_SFT_RST, reg);
  516. t = readl(reg);
  517. udelay(1);
  518. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  519. if (!(GLOB_SFT_RST & t)) {
  520. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  521. rc = 1;
  522. goto done;
  523. }
  524. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  525. i = 5;
  526. do {
  527. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  528. t = readl(reg);
  529. udelay(1);
  530. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  531. if (GLOB_SFT_RST & t) {
  532. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  533. rc = 1;
  534. }
  535. done:
  536. return rc;
  537. }
  538. static void mv_host_stop(struct ata_host_set *host_set)
  539. {
  540. struct mv_host_priv *hpriv = host_set->private_data;
  541. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  542. if (hpriv->hp_flags & MV_HP_FLAG_MSI) {
  543. pci_disable_msi(pdev);
  544. } else {
  545. pci_intx(pdev, 0);
  546. }
  547. kfree(hpriv);
  548. ata_host_stop(host_set);
  549. }
  550. static int mv_port_start(struct ata_port *ap)
  551. {
  552. struct device *dev = ap->host_set->dev;
  553. struct mv_port_priv *pp;
  554. void __iomem *port_mmio = mv_ap_base(ap);
  555. void *mem;
  556. dma_addr_t mem_dma;
  557. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  558. if (!pp) {
  559. return -ENOMEM;
  560. }
  561. memset(pp, 0, sizeof(*pp));
  562. mem = dma_alloc_coherent(dev, MV_PORT_PRIV_DMA_SZ, &mem_dma,
  563. GFP_KERNEL);
  564. if (!mem) {
  565. kfree(pp);
  566. return -ENOMEM;
  567. }
  568. memset(mem, 0, MV_PORT_PRIV_DMA_SZ);
  569. /* First item in chunk of DMA memory:
  570. * 32-slot command request table (CRQB), 32 bytes each in size
  571. */
  572. pp->crqb = mem;
  573. pp->crqb_dma = mem_dma;
  574. mem += MV_CRQB_Q_SZ;
  575. mem_dma += MV_CRQB_Q_SZ;
  576. /* Second item:
  577. * 32-slot command response table (CRPB), 8 bytes each in size
  578. */
  579. pp->crpb = mem;
  580. pp->crpb_dma = mem_dma;
  581. mem += MV_CRPB_Q_SZ;
  582. mem_dma += MV_CRPB_Q_SZ;
  583. /* Third item:
  584. * Table of scatter-gather descriptors (ePRD), 16 bytes each
  585. */
  586. pp->sg_tbl = mem;
  587. pp->sg_tbl_dma = mem_dma;
  588. writelfl(EDMA_CFG_Q_DEPTH | EDMA_CFG_RD_BRST_EXT |
  589. EDMA_CFG_WR_BUFF_LEN, port_mmio + EDMA_CFG_OFS);
  590. writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
  591. writelfl(pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK,
  592. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  593. writelfl(0, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  594. writelfl(0, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  595. writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
  596. writelfl(pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK,
  597. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  598. pp->req_producer = pp->rsp_consumer = 0;
  599. /* Don't turn on EDMA here...do it before DMA commands only. Else
  600. * we'll be unable to send non-data, PIO, etc due to restricted access
  601. * to shadow regs.
  602. */
  603. ap->private_data = pp;
  604. return 0;
  605. }
  606. static void mv_port_stop(struct ata_port *ap)
  607. {
  608. struct device *dev = ap->host_set->dev;
  609. struct mv_port_priv *pp = ap->private_data;
  610. unsigned long flags;
  611. spin_lock_irqsave(&ap->host_set->lock, flags);
  612. mv_stop_dma(ap);
  613. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  614. ap->private_data = NULL;
  615. dma_free_coherent(dev, MV_PORT_PRIV_DMA_SZ, pp->crpb, pp->crpb_dma);
  616. kfree(pp);
  617. }
  618. static void mv_fill_sg(struct ata_queued_cmd *qc)
  619. {
  620. struct mv_port_priv *pp = qc->ap->private_data;
  621. unsigned int i;
  622. for (i = 0; i < qc->n_elem; i++) {
  623. u32 sg_len;
  624. dma_addr_t addr;
  625. addr = sg_dma_address(&qc->sg[i]);
  626. sg_len = sg_dma_len(&qc->sg[i]);
  627. pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
  628. pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  629. assert(0 == (sg_len & ~MV_DMA_BOUNDARY));
  630. pp->sg_tbl[i].flags_size = cpu_to_le32(sg_len);
  631. }
  632. if (0 < qc->n_elem) {
  633. pp->sg_tbl[qc->n_elem - 1].flags_size |= EPRD_FLAG_END_OF_TBL;
  634. }
  635. }
  636. static inline unsigned mv_inc_q_index(unsigned *index)
  637. {
  638. *index = (*index + 1) & MV_MAX_Q_DEPTH_MASK;
  639. return *index;
  640. }
  641. static inline void mv_crqb_pack_cmd(u16 *cmdw, u8 data, u8 addr, unsigned last)
  642. {
  643. *cmdw = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  644. (last ? CRQB_CMD_LAST : 0);
  645. }
  646. static void mv_qc_prep(struct ata_queued_cmd *qc)
  647. {
  648. struct ata_port *ap = qc->ap;
  649. struct mv_port_priv *pp = ap->private_data;
  650. u16 *cw;
  651. struct ata_taskfile *tf;
  652. u16 flags = 0;
  653. if (ATA_PROT_DMA != qc->tf.protocol) {
  654. return;
  655. }
  656. /* the req producer index should be the same as we remember it */
  657. assert(((readl(mv_ap_base(qc->ap) + EDMA_REQ_Q_IN_PTR_OFS) >>
  658. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  659. pp->req_producer);
  660. /* Fill in command request block
  661. */
  662. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  663. flags |= CRQB_FLAG_READ;
  664. }
  665. assert(MV_MAX_Q_DEPTH > qc->tag);
  666. flags |= qc->tag << CRQB_TAG_SHIFT;
  667. pp->crqb[pp->req_producer].sg_addr =
  668. cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
  669. pp->crqb[pp->req_producer].sg_addr_hi =
  670. cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
  671. pp->crqb[pp->req_producer].ctrl_flags = cpu_to_le16(flags);
  672. cw = &pp->crqb[pp->req_producer].ata_cmd[0];
  673. tf = &qc->tf;
  674. /* Sadly, the CRQB cannot accomodate all registers--there are
  675. * only 11 bytes...so we must pick and choose required
  676. * registers based on the command. So, we drop feature and
  677. * hob_feature for [RW] DMA commands, but they are needed for
  678. * NCQ. NCQ will drop hob_nsect.
  679. */
  680. switch (tf->command) {
  681. case ATA_CMD_READ:
  682. case ATA_CMD_READ_EXT:
  683. case ATA_CMD_WRITE:
  684. case ATA_CMD_WRITE_EXT:
  685. mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
  686. break;
  687. #ifdef LIBATA_NCQ /* FIXME: remove this line when NCQ added */
  688. case ATA_CMD_FPDMA_READ:
  689. case ATA_CMD_FPDMA_WRITE:
  690. mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
  691. mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
  692. break;
  693. #endif /* FIXME: remove this line when NCQ added */
  694. default:
  695. /* The only other commands EDMA supports in non-queued and
  696. * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
  697. * of which are defined/used by Linux. If we get here, this
  698. * driver needs work.
  699. *
  700. * FIXME: modify libata to give qc_prep a return value and
  701. * return error here.
  702. */
  703. BUG_ON(tf->command);
  704. break;
  705. }
  706. mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
  707. mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
  708. mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
  709. mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
  710. mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
  711. mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
  712. mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
  713. mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
  714. mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
  715. if (!(qc->flags & ATA_QCFLAG_DMAMAP)) {
  716. return;
  717. }
  718. mv_fill_sg(qc);
  719. }
  720. static int mv_qc_issue(struct ata_queued_cmd *qc)
  721. {
  722. void __iomem *port_mmio = mv_ap_base(qc->ap);
  723. struct mv_port_priv *pp = qc->ap->private_data;
  724. u32 in_ptr;
  725. if (ATA_PROT_DMA != qc->tf.protocol) {
  726. /* We're about to send a non-EDMA capable command to the
  727. * port. Turn off EDMA so there won't be problems accessing
  728. * shadow block, etc registers.
  729. */
  730. mv_stop_dma(qc->ap);
  731. return ata_qc_issue_prot(qc);
  732. }
  733. in_ptr = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  734. /* the req producer index should be the same as we remember it */
  735. assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  736. pp->req_producer);
  737. /* until we do queuing, the queue should be empty at this point */
  738. assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  739. ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS) >>
  740. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
  741. mv_inc_q_index(&pp->req_producer); /* now incr producer index */
  742. mv_start_dma(port_mmio, pp);
  743. /* and write the request in pointer to kick the EDMA to life */
  744. in_ptr &= EDMA_REQ_Q_BASE_LO_MASK;
  745. in_ptr |= pp->req_producer << EDMA_REQ_Q_PTR_SHIFT;
  746. writelfl(in_ptr, port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  747. return 0;
  748. }
  749. static u8 mv_get_crpb_status(struct ata_port *ap)
  750. {
  751. void __iomem *port_mmio = mv_ap_base(ap);
  752. struct mv_port_priv *pp = ap->private_data;
  753. u32 out_ptr;
  754. out_ptr = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  755. /* the response consumer index should be the same as we remember it */
  756. assert(((out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  757. pp->rsp_consumer);
  758. /* increment our consumer index... */
  759. pp->rsp_consumer = mv_inc_q_index(&pp->rsp_consumer);
  760. /* and, until we do NCQ, there should only be 1 CRPB waiting */
  761. assert(((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS) >>
  762. EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  763. pp->rsp_consumer);
  764. /* write out our inc'd consumer index so EDMA knows we're caught up */
  765. out_ptr &= EDMA_RSP_Q_BASE_LO_MASK;
  766. out_ptr |= pp->rsp_consumer << EDMA_RSP_Q_PTR_SHIFT;
  767. writelfl(out_ptr, port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  768. /* Return ATA status register for completed CRPB */
  769. return (pp->crpb[pp->rsp_consumer].flags >> CRPB_FLAG_STATUS_SHIFT);
  770. }
  771. static void mv_err_intr(struct ata_port *ap)
  772. {
  773. void __iomem *port_mmio = mv_ap_base(ap);
  774. u32 edma_err_cause, serr = 0;
  775. edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  776. if (EDMA_ERR_SERR & edma_err_cause) {
  777. serr = scr_read(ap, SCR_ERROR);
  778. scr_write_flush(ap, SCR_ERROR, serr);
  779. }
  780. if (EDMA_ERR_SELF_DIS & edma_err_cause) {
  781. struct mv_port_priv *pp = ap->private_data;
  782. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  783. }
  784. DPRINTK(KERN_ERR "ata%u: port error; EDMA err cause: 0x%08x "
  785. "SERR: 0x%08x\n", ap->id, edma_err_cause, serr);
  786. /* Clear EDMA now that SERR cleanup done */
  787. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  788. /* check for fatal here and recover if needed */
  789. if (EDMA_ERR_FATAL & edma_err_cause) {
  790. mv_phy_reset(ap);
  791. }
  792. }
  793. /* Handle any outstanding interrupts in a single SATAHC */
  794. static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
  795. unsigned int hc)
  796. {
  797. void __iomem *mmio = host_set->mmio_base;
  798. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  799. struct ata_port *ap;
  800. struct ata_queued_cmd *qc;
  801. u32 hc_irq_cause;
  802. int shift, port, port0, hard_port, handled;
  803. u8 ata_status = 0;
  804. if (hc == 0) {
  805. port0 = 0;
  806. } else {
  807. port0 = MV_PORTS_PER_HC;
  808. }
  809. /* we'll need the HC success int register in most cases */
  810. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  811. if (hc_irq_cause) {
  812. writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  813. }
  814. VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
  815. hc,relevant,hc_irq_cause);
  816. for (port = port0; port < port0 + MV_PORTS_PER_HC; port++) {
  817. ap = host_set->ports[port];
  818. hard_port = port & MV_PORT_MASK; /* range 0-3 */
  819. handled = 0; /* ensure ata_status is set if handled++ */
  820. if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause) {
  821. /* new CRPB on the queue; just one at a time until NCQ
  822. */
  823. ata_status = mv_get_crpb_status(ap);
  824. handled++;
  825. } else if ((DEV_IRQ << hard_port) & hc_irq_cause) {
  826. /* received ATA IRQ; read the status reg to clear INTRQ
  827. */
  828. ata_status = readb((void __iomem *)
  829. ap->ioaddr.status_addr);
  830. handled++;
  831. }
  832. shift = port << 1; /* (port * 2) */
  833. if (port >= MV_PORTS_PER_HC) {
  834. shift++; /* skip bit 8 in the HC Main IRQ reg */
  835. }
  836. if ((PORT0_ERR << shift) & relevant) {
  837. mv_err_intr(ap);
  838. /* OR in ATA_ERR to ensure libata knows we took one */
  839. ata_status = readb((void __iomem *)
  840. ap->ioaddr.status_addr) | ATA_ERR;
  841. handled++;
  842. }
  843. if (handled && ap) {
  844. qc = ata_qc_from_tag(ap, ap->active_tag);
  845. if (NULL != qc) {
  846. VPRINTK("port %u IRQ found for qc, "
  847. "ata_status 0x%x\n", port,ata_status);
  848. /* mark qc status appropriately */
  849. ata_qc_complete(qc, ata_status);
  850. }
  851. }
  852. }
  853. VPRINTK("EXIT\n");
  854. }
  855. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  856. struct pt_regs *regs)
  857. {
  858. struct ata_host_set *host_set = dev_instance;
  859. unsigned int hc, handled = 0, n_hcs;
  860. void __iomem *mmio = host_set->mmio_base;
  861. u32 irq_stat;
  862. irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
  863. /* check the cases where we either have nothing pending or have read
  864. * a bogus register value which can indicate HW removal or PCI fault
  865. */
  866. if (!irq_stat || (0xffffffffU == irq_stat)) {
  867. return IRQ_NONE;
  868. }
  869. n_hcs = mv_get_hc_count(host_set->ports[0]->flags);
  870. spin_lock(&host_set->lock);
  871. for (hc = 0; hc < n_hcs; hc++) {
  872. u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
  873. if (relevant) {
  874. mv_host_intr(host_set, relevant, hc);
  875. handled++;
  876. }
  877. }
  878. if (PCI_ERR & irq_stat) {
  879. printk(KERN_ERR DRV_NAME ": PCI ERROR; PCI IRQ cause=0x%08x\n",
  880. readl(mmio + PCI_IRQ_CAUSE_OFS));
  881. DPRINTK("All regs @ PCI error\n");
  882. mv_dump_all_regs(mmio, -1, to_pci_dev(host_set->dev));
  883. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  884. handled++;
  885. }
  886. spin_unlock(&host_set->lock);
  887. return IRQ_RETVAL(handled);
  888. }
  889. static u8 mv_check_err(struct ata_port *ap)
  890. {
  891. mv_stop_dma(ap); /* can't read shadow regs if DMA on */
  892. return readb((void __iomem *) ap->ioaddr.error_addr);
  893. }
  894. /* Part of this is taken from __sata_phy_reset and modified to not sleep
  895. * since this routine gets called from interrupt level.
  896. */
  897. static void mv_phy_reset(struct ata_port *ap)
  898. {
  899. void __iomem *port_mmio = mv_ap_base(ap);
  900. struct ata_taskfile tf;
  901. struct ata_device *dev = &ap->device[0];
  902. unsigned long timeout;
  903. VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
  904. mv_stop_dma(ap);
  905. writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
  906. udelay(25); /* allow reset propagation */
  907. /* Spec never mentions clearing the bit. Marvell's driver does
  908. * clear the bit, however.
  909. */
  910. writelfl(0, port_mmio + EDMA_CMD_OFS);
  911. VPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
  912. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  913. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  914. /* proceed to init communications via the scr_control reg */
  915. scr_write_flush(ap, SCR_CONTROL, 0x301);
  916. mdelay(1);
  917. scr_write_flush(ap, SCR_CONTROL, 0x300);
  918. timeout = jiffies + (HZ * 1);
  919. do {
  920. mdelay(10);
  921. if ((scr_read(ap, SCR_STATUS) & 0xf) != 1)
  922. break;
  923. } while (time_before(jiffies, timeout));
  924. VPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
  925. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  926. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  927. if (sata_dev_present(ap)) {
  928. ata_port_probe(ap);
  929. } else {
  930. printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
  931. ap->id, scr_read(ap, SCR_STATUS));
  932. ata_port_disable(ap);
  933. return;
  934. }
  935. ap->cbl = ATA_CBL_SATA;
  936. tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
  937. tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
  938. tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
  939. tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
  940. dev->class = ata_dev_classify(&tf);
  941. if (!ata_dev_present(dev)) {
  942. VPRINTK("Port disabled post-sig: No device present.\n");
  943. ata_port_disable(ap);
  944. }
  945. VPRINTK("EXIT\n");
  946. }
  947. static void mv_eng_timeout(struct ata_port *ap)
  948. {
  949. struct ata_queued_cmd *qc;
  950. unsigned long flags;
  951. printk(KERN_ERR "ata%u: Entering mv_eng_timeout\n",ap->id);
  952. DPRINTK("All regs @ start of eng_timeout\n");
  953. mv_dump_all_regs(ap->host_set->mmio_base, ap->port_no,
  954. to_pci_dev(ap->host_set->dev));
  955. qc = ata_qc_from_tag(ap, ap->active_tag);
  956. printk(KERN_ERR "mmio_base %p ap %p qc %p scsi_cmnd %p &cmnd %p\n",
  957. ap->host_set->mmio_base, ap, qc, qc->scsicmd,
  958. &qc->scsicmd->cmnd);
  959. mv_err_intr(ap);
  960. mv_phy_reset(ap);
  961. if (!qc) {
  962. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  963. ap->id);
  964. } else {
  965. /* hack alert! We cannot use the supplied completion
  966. * function from inside the ->eh_strategy_handler() thread.
  967. * libata is the only user of ->eh_strategy_handler() in
  968. * any kernel, so the default scsi_done() assumes it is
  969. * not being called from the SCSI EH.
  970. */
  971. spin_lock_irqsave(&ap->host_set->lock, flags);
  972. qc->scsidone = scsi_finish_command;
  973. ata_qc_complete(qc, ATA_ERR);
  974. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  975. }
  976. }
  977. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  978. {
  979. unsigned long shd_base = (unsigned long) port_mmio + SHD_BLK_OFS;
  980. unsigned serr_ofs;
  981. /* PIO related setup
  982. */
  983. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  984. port->error_addr =
  985. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  986. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  987. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  988. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  989. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  990. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  991. port->status_addr =
  992. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  993. /* special case: control/altstatus doesn't have ATA_REG_ address */
  994. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
  995. /* unused: */
  996. port->cmd_addr = port->bmdma_addr = port->scr_addr = 0;
  997. /* Clear any currently outstanding port interrupt conditions */
  998. serr_ofs = mv_scr_offset(SCR_ERROR);
  999. writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
  1000. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1001. /* unmask all EDMA error interrupts */
  1002. writelfl(~0, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
  1003. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  1004. readl(port_mmio + EDMA_CFG_OFS),
  1005. readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
  1006. readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
  1007. }
  1008. static int mv_host_init(struct ata_probe_ent *probe_ent)
  1009. {
  1010. int rc = 0, n_hc, port, hc;
  1011. void __iomem *mmio = probe_ent->mmio_base;
  1012. void __iomem *port_mmio;
  1013. if ((MV_FLAG_GLBL_SFT_RST & probe_ent->host_flags) &&
  1014. mv_global_soft_reset(probe_ent->mmio_base)) {
  1015. rc = 1;
  1016. goto done;
  1017. }
  1018. n_hc = mv_get_hc_count(probe_ent->host_flags);
  1019. probe_ent->n_ports = MV_PORTS_PER_HC * n_hc;
  1020. for (port = 0; port < probe_ent->n_ports; port++) {
  1021. port_mmio = mv_port_base(mmio, port);
  1022. mv_port_init(&probe_ent->port[port], port_mmio);
  1023. }
  1024. for (hc = 0; hc < n_hc; hc++) {
  1025. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1026. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  1027. "(before clear)=0x%08x\n", hc,
  1028. readl(hc_mmio + HC_CFG_OFS),
  1029. readl(hc_mmio + HC_IRQ_CAUSE_OFS));
  1030. /* Clear any currently outstanding hc interrupt conditions */
  1031. writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
  1032. }
  1033. /* Clear any currently outstanding host interrupt conditions */
  1034. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1035. /* and unmask interrupt generation for host regs */
  1036. writelfl(PCI_UNMASK_ALL_IRQS, mmio + PCI_IRQ_MASK_OFS);
  1037. writelfl(~HC_MAIN_MASKED_IRQS, mmio + HC_MAIN_IRQ_MASK_OFS);
  1038. VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
  1039. "PCI int cause/mask=0x%08x/0x%08x\n",
  1040. readl(mmio + HC_MAIN_IRQ_CAUSE_OFS),
  1041. readl(mmio + HC_MAIN_IRQ_MASK_OFS),
  1042. readl(mmio + PCI_IRQ_CAUSE_OFS),
  1043. readl(mmio + PCI_IRQ_MASK_OFS));
  1044. done:
  1045. return rc;
  1046. }
  1047. /* FIXME: complete this */
  1048. static void mv_print_info(struct ata_probe_ent *probe_ent)
  1049. {
  1050. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1051. struct mv_host_priv *hpriv = probe_ent->private_data;
  1052. u8 rev_id, scc;
  1053. const char *scc_s;
  1054. /* Use this to determine the HW stepping of the chip so we know
  1055. * what errata to workaround
  1056. */
  1057. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1058. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  1059. if (scc == 0)
  1060. scc_s = "SCSI";
  1061. else if (scc == 0x01)
  1062. scc_s = "RAID";
  1063. else
  1064. scc_s = "unknown";
  1065. printk(KERN_INFO DRV_NAME
  1066. "(%s) %u slots %u ports %s mode IRQ via %s\n",
  1067. pci_name(pdev), (unsigned)MV_MAX_Q_DEPTH, probe_ent->n_ports,
  1068. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  1069. }
  1070. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1071. {
  1072. static int printed_version = 0;
  1073. struct ata_probe_ent *probe_ent = NULL;
  1074. struct mv_host_priv *hpriv;
  1075. unsigned int board_idx = (unsigned int)ent->driver_data;
  1076. void __iomem *mmio_base;
  1077. int pci_dev_busy = 0, rc;
  1078. if (!printed_version++) {
  1079. printk(KERN_INFO DRV_NAME " version " DRV_VERSION "\n");
  1080. }
  1081. rc = pci_enable_device(pdev);
  1082. if (rc) {
  1083. return rc;
  1084. }
  1085. rc = pci_request_regions(pdev, DRV_NAME);
  1086. if (rc) {
  1087. pci_dev_busy = 1;
  1088. goto err_out;
  1089. }
  1090. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1091. if (probe_ent == NULL) {
  1092. rc = -ENOMEM;
  1093. goto err_out_regions;
  1094. }
  1095. memset(probe_ent, 0, sizeof(*probe_ent));
  1096. probe_ent->dev = pci_dev_to_dev(pdev);
  1097. INIT_LIST_HEAD(&probe_ent->node);
  1098. mmio_base = pci_iomap(pdev, MV_PRIMARY_BAR, 0);
  1099. if (mmio_base == NULL) {
  1100. rc = -ENOMEM;
  1101. goto err_out_free_ent;
  1102. }
  1103. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1104. if (!hpriv) {
  1105. rc = -ENOMEM;
  1106. goto err_out_iounmap;
  1107. }
  1108. memset(hpriv, 0, sizeof(*hpriv));
  1109. probe_ent->sht = mv_port_info[board_idx].sht;
  1110. probe_ent->host_flags = mv_port_info[board_idx].host_flags;
  1111. probe_ent->pio_mask = mv_port_info[board_idx].pio_mask;
  1112. probe_ent->udma_mask = mv_port_info[board_idx].udma_mask;
  1113. probe_ent->port_ops = mv_port_info[board_idx].port_ops;
  1114. probe_ent->irq = pdev->irq;
  1115. probe_ent->irq_flags = SA_SHIRQ;
  1116. probe_ent->mmio_base = mmio_base;
  1117. probe_ent->private_data = hpriv;
  1118. /* initialize adapter */
  1119. rc = mv_host_init(probe_ent);
  1120. if (rc) {
  1121. goto err_out_hpriv;
  1122. }
  1123. /* Enable interrupts */
  1124. if (pci_enable_msi(pdev) == 0) {
  1125. hpriv->hp_flags |= MV_HP_FLAG_MSI;
  1126. } else {
  1127. pci_intx(pdev, 1);
  1128. }
  1129. mv_dump_pci_cfg(pdev, 0x68);
  1130. mv_print_info(probe_ent);
  1131. if (ata_device_add(probe_ent) == 0) {
  1132. rc = -ENODEV; /* No devices discovered */
  1133. goto err_out_dev_add;
  1134. }
  1135. kfree(probe_ent);
  1136. return 0;
  1137. err_out_dev_add:
  1138. if (MV_HP_FLAG_MSI & hpriv->hp_flags) {
  1139. pci_disable_msi(pdev);
  1140. } else {
  1141. pci_intx(pdev, 0);
  1142. }
  1143. err_out_hpriv:
  1144. kfree(hpriv);
  1145. err_out_iounmap:
  1146. pci_iounmap(pdev, mmio_base);
  1147. err_out_free_ent:
  1148. kfree(probe_ent);
  1149. err_out_regions:
  1150. pci_release_regions(pdev);
  1151. err_out:
  1152. if (!pci_dev_busy) {
  1153. pci_disable_device(pdev);
  1154. }
  1155. return rc;
  1156. }
  1157. static int __init mv_init(void)
  1158. {
  1159. return pci_module_init(&mv_pci_driver);
  1160. }
  1161. static void __exit mv_exit(void)
  1162. {
  1163. pci_unregister_driver(&mv_pci_driver);
  1164. }
  1165. MODULE_AUTHOR("Brett Russ");
  1166. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  1167. MODULE_LICENSE("GPL");
  1168. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  1169. MODULE_VERSION(DRV_VERSION);
  1170. module_init(mv_init);
  1171. module_exit(mv_exit);