Merge libata upstream (which includes C/H/S support) include irq-pio branch.
[linux-2.6] / drivers / scsi / sata_mv.c
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
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/blkdev.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/sched.h>
31 #include <linux/dma-mapping.h>
32 #include "scsi.h"
33 #include <scsi/scsi_host.h>
34 #include <linux/libata.h>
35 #include <asm/io.h>
36
37 #define DRV_NAME        "sata_mv"
38 #define DRV_VERSION     "0.12"
39
40 enum {
41         /* BAR's are enumerated in terms of pci_resource_start() terms */
42         MV_PRIMARY_BAR          = 0,    /* offset 0x10: memory space */
43         MV_IO_BAR               = 2,    /* offset 0x18: IO space */
44         MV_MISC_BAR             = 3,    /* offset 0x1c: FLASH, NVRAM, SRAM */
45
46         MV_MAJOR_REG_AREA_SZ    = 0x10000,      /* 64KB */
47         MV_MINOR_REG_AREA_SZ    = 0x2000,       /* 8KB */
48
49         MV_PCI_REG_BASE         = 0,
50         MV_IRQ_COAL_REG_BASE    = 0x18000,      /* 6xxx part only */
51         MV_SATAHC0_REG_BASE     = 0x20000,
52
53         MV_PCI_REG_SZ           = MV_MAJOR_REG_AREA_SZ,
54         MV_SATAHC_REG_SZ        = MV_MAJOR_REG_AREA_SZ,
55         MV_SATAHC_ARBTR_REG_SZ  = MV_MINOR_REG_AREA_SZ,         /* arbiter */
56         MV_PORT_REG_SZ          = MV_MINOR_REG_AREA_SZ,
57
58         MV_Q_CT                 = 32,
59         MV_CRQB_SZ              = 32,
60         MV_CRPB_SZ              = 8,
61
62         MV_DMA_BOUNDARY         = 0xffffffffU,
63         SATAHC_MASK             = (~(MV_SATAHC_REG_SZ - 1)),
64
65         MV_PORTS_PER_HC         = 4,
66         /* == (port / MV_PORTS_PER_HC) to determine HC from 0-7 port */
67         MV_PORT_HC_SHIFT        = 2,
68         /* == (port % MV_PORTS_PER_HC) to determine port from 0-7 port */
69         MV_PORT_MASK            = 3,
70
71         /* Host Flags */
72         MV_FLAG_DUAL_HC         = (1 << 30),  /* two SATA Host Controllers */
73         MV_FLAG_IRQ_COALESCE    = (1 << 29),  /* IRQ coalescing capability */
74         MV_FLAG_BDMA            = (1 << 28),  /* Basic DMA */
75
76         chip_504x               = 0,
77         chip_508x               = 1,
78         chip_604x               = 2,
79         chip_608x               = 3,
80
81         /* PCI interface registers */
82
83         PCI_MAIN_CMD_STS_OFS    = 0xd30,
84         STOP_PCI_MASTER         = (1 << 2),
85         PCI_MASTER_EMPTY        = (1 << 3),
86         GLOB_SFT_RST            = (1 << 4),
87
88         PCI_IRQ_CAUSE_OFS       = 0x1d58,
89         PCI_IRQ_MASK_OFS        = 0x1d5c,
90         PCI_UNMASK_ALL_IRQS     = 0x7fffff,     /* bits 22-0 */
91
92         HC_MAIN_IRQ_CAUSE_OFS   = 0x1d60,
93         HC_MAIN_IRQ_MASK_OFS    = 0x1d64,
94         PORT0_ERR               = (1 << 0),     /* shift by port # */
95         PORT0_DONE              = (1 << 1),     /* shift by port # */
96         HC0_IRQ_PEND            = 0x1ff,        /* bits 0-8 = HC0's ports */
97         HC_SHIFT                = 9,            /* bits 9-17 = HC1's ports */
98         PCI_ERR                 = (1 << 18),
99         TRAN_LO_DONE            = (1 << 19),    /* 6xxx: IRQ coalescing */
100         TRAN_HI_DONE            = (1 << 20),    /* 6xxx: IRQ coalescing */
101         PORTS_0_7_COAL_DONE     = (1 << 21),    /* 6xxx: IRQ coalescing */
102         GPIO_INT                = (1 << 22),
103         SELF_INT                = (1 << 23),
104         TWSI_INT                = (1 << 24),
105         HC_MAIN_RSVD            = (0x7f << 25), /* bits 31-25 */
106         HC_MAIN_MASKED_IRQS     = (TRAN_LO_DONE | TRAN_HI_DONE | 
107                                    PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
108                                    HC_MAIN_RSVD),
109
110         /* SATAHC registers */
111         HC_CFG_OFS              = 0,
112
113         HC_IRQ_CAUSE_OFS        = 0x14,
114         CRBP_DMA_DONE           = (1 << 0),     /* shift by port # */
115         HC_IRQ_COAL             = (1 << 4),     /* IRQ coalescing */
116         DEV_IRQ                 = (1 << 8),     /* shift by port # */
117
118         /* Shadow block registers */
119         SHD_PIO_DATA_OFS        = 0x100,
120         SHD_FEA_ERR_OFS         = 0x104,
121         SHD_SECT_CNT_OFS        = 0x108,
122         SHD_LBA_L_OFS           = 0x10C,
123         SHD_LBA_M_OFS           = 0x110,
124         SHD_LBA_H_OFS           = 0x114,
125         SHD_DEV_HD_OFS          = 0x118,
126         SHD_CMD_STA_OFS         = 0x11C,
127         SHD_CTL_AST_OFS         = 0x120,
128
129         /* SATA registers */
130         SATA_STATUS_OFS         = 0x300,  /* ctrl, err regs follow status */
131         SATA_ACTIVE_OFS         = 0x350,
132
133         /* Port registers */
134         EDMA_CFG_OFS            = 0,
135
136         EDMA_ERR_IRQ_CAUSE_OFS  = 0x8,
137         EDMA_ERR_IRQ_MASK_OFS   = 0xc,
138         EDMA_ERR_D_PAR          = (1 << 0),
139         EDMA_ERR_PRD_PAR        = (1 << 1),
140         EDMA_ERR_DEV            = (1 << 2),
141         EDMA_ERR_DEV_DCON       = (1 << 3),
142         EDMA_ERR_DEV_CON        = (1 << 4),
143         EDMA_ERR_SERR           = (1 << 5),
144         EDMA_ERR_SELF_DIS       = (1 << 7),
145         EDMA_ERR_BIST_ASYNC     = (1 << 8),
146         EDMA_ERR_CRBQ_PAR       = (1 << 9),
147         EDMA_ERR_CRPB_PAR       = (1 << 10),
148         EDMA_ERR_INTRL_PAR      = (1 << 11),
149         EDMA_ERR_IORDY          = (1 << 12),
150         EDMA_ERR_LNK_CTRL_RX    = (0xf << 13),
151         EDMA_ERR_LNK_CTRL_RX_2  = (1 << 15),
152         EDMA_ERR_LNK_DATA_RX    = (0xf << 17),
153         EDMA_ERR_LNK_CTRL_TX    = (0x1f << 21),
154         EDMA_ERR_LNK_DATA_TX    = (0x1f << 26),
155         EDMA_ERR_TRANS_PROTO    = (1 << 31),
156         EDMA_ERR_FATAL          = (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR | 
157                                    EDMA_ERR_DEV_DCON | EDMA_ERR_CRBQ_PAR |
158                                    EDMA_ERR_CRPB_PAR | EDMA_ERR_INTRL_PAR |
159                                    EDMA_ERR_IORDY | EDMA_ERR_LNK_CTRL_RX_2 | 
160                                    EDMA_ERR_LNK_DATA_RX |
161                                    EDMA_ERR_LNK_DATA_TX | 
162                                    EDMA_ERR_TRANS_PROTO),
163
164         EDMA_CMD_OFS            = 0x28,
165         EDMA_EN                 = (1 << 0),
166         EDMA_DS                 = (1 << 1),
167         ATA_RST                 = (1 << 2),
168
169         /* BDMA is 6xxx part only */
170         BDMA_CMD_OFS            = 0x224,
171         BDMA_START              = (1 << 0),
172
173         MV_UNDEF                = 0,
174 };
175
176 struct mv_port_priv {
177
178 };
179
180 struct mv_host_priv {
181
182 };
183
184 static void mv_irq_clear(struct ata_port *ap);
185 static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
186 static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
187 static void mv_phy_reset(struct ata_port *ap);
188 static int mv_master_reset(void __iomem *mmio_base);
189 static irqreturn_t mv_interrupt(int irq, void *dev_instance,
190                                 struct pt_regs *regs);
191 static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
192
193 static Scsi_Host_Template mv_sht = {
194         .module                 = THIS_MODULE,
195         .name                   = DRV_NAME,
196         .ioctl                  = ata_scsi_ioctl,
197         .queuecommand           = ata_scsi_queuecmd,
198         .eh_strategy_handler    = ata_scsi_error,
199         .can_queue              = ATA_DEF_QUEUE,
200         .this_id                = ATA_SHT_THIS_ID,
201         .sg_tablesize           = MV_UNDEF,
202         .max_sectors            = ATA_MAX_SECTORS,
203         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
204         .emulated               = ATA_SHT_EMULATED,
205         .use_clustering         = MV_UNDEF,
206         .proc_name              = DRV_NAME,
207         .dma_boundary           = MV_DMA_BOUNDARY,
208         .slave_configure        = ata_scsi_slave_config,
209         .bios_param             = ata_std_bios_param,
210         .ordered_flush          = 1,
211 };
212
213 static struct ata_port_operations mv_ops = {
214         .port_disable           = ata_port_disable,
215
216         .tf_load                = ata_tf_load,
217         .tf_read                = ata_tf_read,
218         .check_status           = ata_check_status,
219         .exec_command           = ata_exec_command,
220         .dev_select             = ata_std_dev_select,
221
222         .phy_reset              = mv_phy_reset,
223
224         .qc_prep                = ata_qc_prep,
225         .qc_issue               = ata_qc_issue_prot,
226
227         .eng_timeout            = ata_eng_timeout,
228
229         .irq_handler            = mv_interrupt,
230         .irq_clear              = mv_irq_clear,
231
232         .scr_read               = mv_scr_read,
233         .scr_write              = mv_scr_write,
234
235         .port_start             = ata_port_start,
236         .port_stop              = ata_port_stop,
237         .host_stop              = ata_host_stop,
238 };
239
240 static struct ata_port_info mv_port_info[] = {
241         {  /* chip_504x */
242                 .sht            = &mv_sht,
243                 .host_flags     = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
244                                    ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
245                                    ATA_FLAG_PIO_POLLING),
246                 .pio_mask       = 0x1f, /* pio4-0 */
247                 .udma_mask      = 0,    /* 0x7f (udma6-0 disabled for now) */
248                 .port_ops       = &mv_ops,
249         },
250         {  /* chip_508x */
251                 .sht            = &mv_sht,
252                 .host_flags     = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
253                                    ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO | 
254                                    ATA_FLAG_PIO_POLLING | MV_FLAG_DUAL_HC),
255                 .pio_mask       = 0x1f, /* pio4-0 */
256                 .udma_mask      = 0,    /* 0x7f (udma6-0 disabled for now) */
257                 .port_ops       = &mv_ops,
258         },
259         {  /* chip_604x */
260                 .sht            = &mv_sht,
261                 .host_flags     = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
262                                    ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO | 
263                                    ATA_FLAG_PIO_POLLING |
264                                    MV_FLAG_IRQ_COALESCE | MV_FLAG_BDMA),
265                 .pio_mask       = 0x1f, /* pio4-0 */
266                 .udma_mask      = 0,    /* 0x7f (udma6-0 disabled for now) */
267                 .port_ops       = &mv_ops,
268         },
269         {  /* chip_608x */
270                 .sht            = &mv_sht,
271                 .host_flags     = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
272                                    ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
273                                    ATA_FLAG_PIO_POLLING |
274                                    MV_FLAG_IRQ_COALESCE | MV_FLAG_DUAL_HC |
275                                    MV_FLAG_BDMA),
276                 .pio_mask       = 0x1f, /* pio4-0 */
277                 .udma_mask      = 0,    /* 0x7f (udma6-0 disabled for now) */
278                 .port_ops       = &mv_ops,
279         },
280 };
281
282 static struct pci_device_id mv_pci_tbl[] = {
283         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5040), 0, 0, chip_504x},
284         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5041), 0, 0, chip_504x},
285         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5080), 0, 0, chip_508x},
286         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5081), 0, 0, chip_508x},
287
288         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6040), 0, 0, chip_604x},
289         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6041), 0, 0, chip_604x},
290         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6080), 0, 0, chip_608x},
291         {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6081), 0, 0, chip_608x},
292         {}                      /* terminate list */
293 };
294
295 static struct pci_driver mv_pci_driver = {
296         .name                   = DRV_NAME,
297         .id_table               = mv_pci_tbl,
298         .probe                  = mv_init_one,
299         .remove                 = ata_pci_remove_one,
300 };
301
302 /*
303  * Functions
304  */
305
306 static inline void writelfl(unsigned long data, void __iomem *addr)
307 {
308         writel(data, addr);
309         (void) readl(addr);     /* flush to avoid PCI posted write */
310 }
311
312 static inline void __iomem *mv_port_addr_to_hc_base(void __iomem *port_mmio)
313 {
314         return ((void __iomem *)((unsigned long)port_mmio & 
315                                  (unsigned long)SATAHC_MASK));
316 }
317
318 static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
319 {
320         return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
321 }
322
323 static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
324 {
325         return (mv_hc_base(base, port >> MV_PORT_HC_SHIFT) +
326                 MV_SATAHC_ARBTR_REG_SZ + 
327                 ((port & MV_PORT_MASK) * MV_PORT_REG_SZ));
328 }
329
330 static inline void __iomem *mv_ap_base(struct ata_port *ap)
331 {
332         return mv_port_base(ap->host_set->mmio_base, ap->port_no);
333 }
334
335 static inline int mv_get_hc_count(unsigned long flags)
336 {
337         return ((flags & MV_FLAG_DUAL_HC) ? 2 : 1);
338 }
339
340 static inline int mv_is_edma_active(struct ata_port *ap)
341 {
342         void __iomem *port_mmio = mv_ap_base(ap);
343         return (EDMA_EN & readl(port_mmio + EDMA_CMD_OFS));
344 }
345
346 static inline int mv_port_bdma_capable(struct ata_port *ap)
347 {
348         return (ap->flags & MV_FLAG_BDMA);
349 }
350
351 static void mv_irq_clear(struct ata_port *ap)
352 {
353 }
354
355 static unsigned int mv_scr_offset(unsigned int sc_reg_in)
356 {
357         unsigned int ofs;
358
359         switch (sc_reg_in) {
360         case SCR_STATUS:
361         case SCR_CONTROL:
362         case SCR_ERROR:
363                 ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
364                 break;
365         case SCR_ACTIVE:
366                 ofs = SATA_ACTIVE_OFS;   /* active is not with the others */
367                 break;
368         default:
369                 ofs = 0xffffffffU;
370                 break;
371         }
372         return ofs;
373 }
374
375 static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
376 {
377         unsigned int ofs = mv_scr_offset(sc_reg_in);
378
379         if (0xffffffffU != ofs) {
380                 return readl(mv_ap_base(ap) + ofs);
381         } else {
382                 return (u32) ofs;
383         }
384 }
385
386 static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
387 {
388         unsigned int ofs = mv_scr_offset(sc_reg_in);
389
390         if (0xffffffffU != ofs) {
391                 writelfl(val, mv_ap_base(ap) + ofs);
392         }
393 }
394
395 static int mv_master_reset(void __iomem *mmio_base)
396 {
397         void __iomem *reg = mmio_base + PCI_MAIN_CMD_STS_OFS;
398         int i, rc = 0;
399         u32 t;
400
401         VPRINTK("ENTER\n");
402
403         /* Following procedure defined in PCI "main command and status
404          * register" table.
405          */
406         t = readl(reg);
407         writel(t | STOP_PCI_MASTER, reg);
408
409         for (i = 0; i < 100; i++) {
410                 msleep(10);
411                 t = readl(reg);
412                 if (PCI_MASTER_EMPTY & t) {
413                         break;
414                 }
415         }
416         if (!(PCI_MASTER_EMPTY & t)) {
417                 printk(KERN_ERR DRV_NAME "PCI master won't flush\n");
418                 rc = 1;         /* broken HW? */
419                 goto done;
420         }
421
422         /* set reset */
423         i = 5;
424         do {
425                 writel(t | GLOB_SFT_RST, reg);
426                 t = readl(reg);
427                 udelay(1);
428         } while (!(GLOB_SFT_RST & t) && (i-- > 0));
429
430         if (!(GLOB_SFT_RST & t)) {
431                 printk(KERN_ERR DRV_NAME "can't set global reset\n");
432                 rc = 1;         /* broken HW? */
433                 goto done;
434         }
435
436         /* clear reset */
437         i = 5;
438         do {
439                 writel(t & ~GLOB_SFT_RST, reg);
440                 t = readl(reg);
441                 udelay(1);
442         } while ((GLOB_SFT_RST & t) && (i-- > 0));
443
444         if (GLOB_SFT_RST & t) {
445                 printk(KERN_ERR DRV_NAME "can't clear global reset\n");
446                 rc = 1;         /* broken HW? */
447         }
448
449  done:
450         VPRINTK("EXIT, rc = %i\n", rc);
451         return rc;
452 }
453
454 static void mv_err_intr(struct ata_port *ap)
455 {
456         void __iomem *port_mmio;
457         u32 edma_err_cause, serr = 0;
458
459         /* bug here b/c we got an err int on a port we don't know about,
460          * so there's no way to clear it
461          */
462         BUG_ON(NULL == ap);
463         port_mmio = mv_ap_base(ap);
464
465         edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
466
467         if (EDMA_ERR_SERR & edma_err_cause) {
468                 serr = scr_read(ap, SCR_ERROR);
469                 scr_write_flush(ap, SCR_ERROR, serr);
470         }
471         DPRINTK("port %u error; EDMA err cause: 0x%08x SERR: 0x%08x\n", 
472                 ap->port_no, edma_err_cause, serr);
473
474         /* Clear EDMA now that SERR cleanup done */
475         writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
476
477         /* check for fatal here and recover if needed */
478         if (EDMA_ERR_FATAL & edma_err_cause) {
479                 mv_phy_reset(ap);
480         }
481 }
482
483 /* Handle any outstanding interrupts in a single SATAHC 
484  */
485 static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
486                          unsigned int hc)
487 {
488         void __iomem *mmio = host_set->mmio_base;
489         void __iomem *hc_mmio = mv_hc_base(mmio, hc);
490         struct ata_port *ap;
491         struct ata_queued_cmd *qc;
492         u32 hc_irq_cause;
493         int shift, port, port0, hard_port;
494         u8 ata_status;
495
496         if (hc == 0) {
497                 port0 = 0;
498         } else {
499                 port0 = MV_PORTS_PER_HC;
500         }
501
502         /* we'll need the HC success int register in most cases */
503         hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
504         if (hc_irq_cause) {
505                 writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
506         }
507
508         VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
509                 hc,relevant,hc_irq_cause);
510
511         for (port = port0; port < port0 + MV_PORTS_PER_HC; port++) {
512                 ap = host_set->ports[port];
513                 hard_port = port & MV_PORT_MASK;        /* range 0-3 */
514                 ata_status = 0xffU;
515
516                 if (((CRBP_DMA_DONE | DEV_IRQ) << hard_port) & hc_irq_cause) {
517                         BUG_ON(NULL == ap);
518                         /* rcv'd new resp, basic DMA complete, or ATA IRQ */
519                         /* This is needed to clear the ATA INTRQ.
520                          * FIXME: don't read the status reg in EDMA mode!
521                          */
522                         ata_status = readb((void __iomem *)
523                                            ap->ioaddr.status_addr);
524                 }
525
526                 shift = port * 2;
527                 if (port >= MV_PORTS_PER_HC) {
528                         shift++;        /* skip bit 8 in the HC Main IRQ reg */
529                 }
530                 if ((PORT0_ERR << shift) & relevant) {
531                         mv_err_intr(ap);
532                         /* FIXME: smart to OR in ATA_ERR? */
533                         ata_status = readb((void __iomem *)
534                                            ap->ioaddr.status_addr) | ATA_ERR;
535                 }
536                 
537                 if (ap) {
538                         qc = ata_qc_from_tag(ap, ap->active_tag);
539                         if (NULL != qc) {
540                                 VPRINTK("port %u IRQ found for qc, "
541                                         "ata_status 0x%x\n", port,ata_status);
542                                 BUG_ON(0xffU == ata_status);
543                                 /* mark qc status appropriately */
544                                 ata_qc_complete(qc, ata_status);
545                         }
546                 }
547         }
548         VPRINTK("EXIT\n");
549 }
550
551 static irqreturn_t mv_interrupt(int irq, void *dev_instance,
552                                 struct pt_regs *regs)
553 {
554         struct ata_host_set *host_set = dev_instance;
555         unsigned int hc, handled = 0, n_hcs;
556         void __iomem *mmio;
557         u32 irq_stat;
558
559         mmio = host_set->mmio_base;
560         irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
561         n_hcs = mv_get_hc_count(host_set->ports[0]->flags);
562
563         /* check the cases where we either have nothing pending or have read
564          * a bogus register value which can indicate HW removal or PCI fault
565          */
566         if (!irq_stat || (0xffffffffU == irq_stat)) {
567                 return IRQ_NONE;
568         }
569
570         spin_lock(&host_set->lock);
571
572         for (hc = 0; hc < n_hcs; hc++) {
573                 u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
574                 if (relevant) {
575                         mv_host_intr(host_set, relevant, hc);
576                         handled = 1;
577                 }
578         }
579         if (PCI_ERR & irq_stat) {
580                 /* FIXME: these are all masked by default, but still need
581                  * to recover from them properly.
582                  */
583         }
584
585         spin_unlock(&host_set->lock);
586
587         return IRQ_RETVAL(handled);
588 }
589
590 static void mv_phy_reset(struct ata_port *ap)
591 {
592         void __iomem *port_mmio = mv_ap_base(ap);
593         struct ata_taskfile tf;
594         struct ata_device *dev = &ap->device[0];
595         u32 edma = 0, bdma;
596
597         VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
598
599         edma = readl(port_mmio + EDMA_CMD_OFS);
600         if (EDMA_EN & edma) {
601                 /* disable EDMA if active */
602                 edma &= ~EDMA_EN;
603                 writelfl(edma | EDMA_DS, port_mmio + EDMA_CMD_OFS);
604                 udelay(1);
605         } else if (mv_port_bdma_capable(ap) &&
606                    (bdma = readl(port_mmio + BDMA_CMD_OFS)) & BDMA_START) {
607                 /* disable BDMA if active */
608                 writelfl(bdma & ~BDMA_START, port_mmio + BDMA_CMD_OFS);
609         }
610
611         writelfl(edma | ATA_RST, port_mmio + EDMA_CMD_OFS);
612         udelay(25);             /* allow reset propagation */
613
614         /* Spec never mentions clearing the bit.  Marvell's driver does
615          * clear the bit, however.
616          */
617         writelfl(edma & ~ATA_RST, port_mmio + EDMA_CMD_OFS);
618
619         VPRINTK("Done.  Now calling __sata_phy_reset()\n");
620
621         /* proceed to init communications via the scr_control reg */
622         __sata_phy_reset(ap);
623
624         if (ap->flags & ATA_FLAG_PORT_DISABLED) {
625                 VPRINTK("Port disabled pre-sig.  Exiting.\n");
626                 return;
627         }
628
629         tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
630         tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
631         tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
632         tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
633
634         dev->class = ata_dev_classify(&tf);
635         if (!ata_dev_present(dev)) {
636                 VPRINTK("Port disabled post-sig: No device present.\n");
637                 ata_port_disable(ap);
638         }
639         VPRINTK("EXIT\n");
640 }
641
642 static void mv_port_init(struct ata_ioports *port, unsigned long base)
643 {
644         /* PIO related setup */
645         port->data_addr = base + SHD_PIO_DATA_OFS;
646         port->error_addr = port->feature_addr = base + SHD_FEA_ERR_OFS;
647         port->nsect_addr = base + SHD_SECT_CNT_OFS;
648         port->lbal_addr = base + SHD_LBA_L_OFS;
649         port->lbam_addr = base + SHD_LBA_M_OFS;
650         port->lbah_addr = base + SHD_LBA_H_OFS;
651         port->device_addr = base + SHD_DEV_HD_OFS;
652         port->status_addr = port->command_addr = base + SHD_CMD_STA_OFS;
653         port->altstatus_addr = port->ctl_addr = base + SHD_CTL_AST_OFS;
654         /* unused */
655         port->cmd_addr = port->bmdma_addr = port->scr_addr = 0;
656
657         /* unmask all EDMA error interrupts */
658         writel(~0, (void __iomem *)base + EDMA_ERR_IRQ_MASK_OFS);
659
660         VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n", 
661                 readl((void __iomem *)base + EDMA_CFG_OFS),
662                 readl((void __iomem *)base + EDMA_ERR_IRQ_CAUSE_OFS),
663                 readl((void __iomem *)base + EDMA_ERR_IRQ_MASK_OFS));
664 }
665
666 static int mv_host_init(struct ata_probe_ent *probe_ent)
667 {
668         int rc = 0, n_hc, port, hc;
669         void __iomem *mmio = probe_ent->mmio_base;
670         void __iomem *port_mmio;
671
672         if (mv_master_reset(probe_ent->mmio_base)) {
673                 rc = 1;
674                 goto done;
675         }
676
677         n_hc = mv_get_hc_count(probe_ent->host_flags);
678         probe_ent->n_ports = MV_PORTS_PER_HC * n_hc;
679
680         for (port = 0; port < probe_ent->n_ports; port++) {
681                 port_mmio = mv_port_base(mmio, port);
682                 mv_port_init(&probe_ent->port[port], (unsigned long)port_mmio);
683         }
684
685         for (hc = 0; hc < n_hc; hc++) {
686                 VPRINTK("HC%i: HC config=0x%08x HC IRQ cause=0x%08x\n", hc,
687                         readl(mv_hc_base(mmio, hc) + HC_CFG_OFS),
688                         readl(mv_hc_base(mmio, hc) + HC_IRQ_CAUSE_OFS));
689         }
690
691         writel(~HC_MAIN_MASKED_IRQS, mmio + HC_MAIN_IRQ_MASK_OFS);
692         writel(PCI_UNMASK_ALL_IRQS, mmio + PCI_IRQ_MASK_OFS);
693
694         VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
695                 "PCI int cause/mask=0x%08x/0x%08x\n", 
696                 readl(mmio + HC_MAIN_IRQ_CAUSE_OFS),
697                 readl(mmio + HC_MAIN_IRQ_MASK_OFS),
698                 readl(mmio + PCI_IRQ_CAUSE_OFS),
699                 readl(mmio + PCI_IRQ_MASK_OFS));
700
701  done:
702         return rc;
703 }
704
705 static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
706 {
707         static int printed_version = 0;
708         struct ata_probe_ent *probe_ent = NULL;
709         struct mv_host_priv *hpriv;
710         unsigned int board_idx = (unsigned int)ent->driver_data;
711         void __iomem *mmio_base;
712         int pci_dev_busy = 0;
713         int rc;
714
715         if (!printed_version++) {
716                 printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
717         }
718
719         VPRINTK("ENTER for PCI Bus:Slot.Func=%u:%u.%u\n", pdev->bus->number,
720                 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
721
722         rc = pci_enable_device(pdev);
723         if (rc) {
724                 return rc;
725         }
726
727         rc = pci_request_regions(pdev, DRV_NAME);
728         if (rc) {
729                 pci_dev_busy = 1;
730                 goto err_out;
731         }
732
733         pci_intx(pdev, 1);
734
735         probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
736         if (probe_ent == NULL) {
737                 rc = -ENOMEM;
738                 goto err_out_regions;
739         }
740
741         memset(probe_ent, 0, sizeof(*probe_ent));
742         probe_ent->dev = pci_dev_to_dev(pdev);
743         INIT_LIST_HEAD(&probe_ent->node);
744
745         mmio_base = ioremap_nocache(pci_resource_start(pdev, MV_PRIMARY_BAR),
746                                     pci_resource_len(pdev, MV_PRIMARY_BAR));
747         if (mmio_base == NULL) {
748                 rc = -ENOMEM;
749                 goto err_out_free_ent;
750         }
751
752         hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
753         if (!hpriv) {
754                 rc = -ENOMEM;
755                 goto err_out_iounmap;
756         }
757         memset(hpriv, 0, sizeof(*hpriv));
758
759         probe_ent->sht = mv_port_info[board_idx].sht;
760         probe_ent->host_flags = mv_port_info[board_idx].host_flags;
761         probe_ent->pio_mask = mv_port_info[board_idx].pio_mask;
762         probe_ent->udma_mask = mv_port_info[board_idx].udma_mask;
763         probe_ent->port_ops = mv_port_info[board_idx].port_ops;
764
765         probe_ent->irq = pdev->irq;
766         probe_ent->irq_flags = SA_SHIRQ;
767         probe_ent->mmio_base = mmio_base;
768         probe_ent->private_data = hpriv;
769
770         /* initialize adapter */
771         rc = mv_host_init(probe_ent);
772         if (rc) {
773                 goto err_out_hpriv;
774         }
775 /*      mv_print_info(probe_ent); */
776
777         {
778                 int b, w;
779                 u32 dw[4];      /* hold a line of 16b */
780                 VPRINTK("PCI config space:\n");
781                 for (b = 0; b < 0x40; ) {
782                         for (w = 0; w < 4; w++) {
783                                 (void) pci_read_config_dword(pdev,b,&dw[w]);
784                                 b += sizeof(*dw);
785                         }
786                         VPRINTK("%08x %08x %08x %08x\n",
787                                 dw[0],dw[1],dw[2],dw[3]);
788                 }
789         }
790
791         /* FIXME: check ata_device_add return value */
792         ata_device_add(probe_ent);
793         kfree(probe_ent);
794
795         return 0;
796
797  err_out_hpriv:
798         kfree(hpriv);
799  err_out_iounmap:
800         iounmap(mmio_base);
801  err_out_free_ent:
802         kfree(probe_ent);
803  err_out_regions:
804         pci_release_regions(pdev);
805  err_out:
806         if (!pci_dev_busy) {
807                 pci_disable_device(pdev);
808         }
809
810         return rc;
811 }
812
813 static int __init mv_init(void)
814 {
815         return pci_module_init(&mv_pci_driver);
816 }
817
818 static void __exit mv_exit(void)
819 {
820         pci_unregister_driver(&mv_pci_driver);
821 }
822
823 MODULE_AUTHOR("Brett Russ");
824 MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
825 MODULE_LICENSE("GPL");
826 MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
827 MODULE_VERSION(DRV_VERSION);
828
829 module_init(mv_init);
830 module_exit(mv_exit);