Merge tag 'block-5.17-2022-01-21' of git://git.kernel.dk/linux-block
[linux-2.6-microblaze.git] / drivers / ata / pata_marvell.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *      Marvell PATA driver.
4  *
5  *      For the moment we drive the PATA port in legacy mode. That
6  *      isn't making full use of the device functionality but it is
7  *      easy to get working.
8  *
9  *      (c) 2006 Red Hat
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/blkdev.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <scsi/scsi_host.h>
19 #include <linux/libata.h>
20 #include <linux/ata.h>
21
22 #define DRV_NAME        "pata_marvell"
23 #define DRV_VERSION     "0.1.6"
24
25 /**
26  *      marvell_pata_active     -       check if PATA is active
27  *      @pdev: PCI device
28  *
29  *      Returns 1 if the PATA port may be active. We know how to check this
30  *      for the 6145 but not the other devices
31  */
32
33 static int marvell_pata_active(struct pci_dev *pdev)
34 {
35         u32 devices;
36         void __iomem *barp;
37
38         /* We don't yet know how to do this for other devices */
39         if (pdev->device != 0x6145)
40                 return 1;
41
42         barp = pci_iomap(pdev, 5, 0x10);
43         if (barp == NULL)
44                 return -ENOMEM;
45
46         devices = ioread32(barp + 0x0C);
47         pci_iounmap(pdev, barp);
48
49         if (devices & 0x10)
50                 return 1;
51         return 0;
52 }
53
54 /**
55  *      marvell_pre_reset       -       probe begin
56  *      @link: link
57  *      @deadline: deadline jiffies for the operation
58  *
59  *      Perform the PATA port setup we need.
60  */
61
62 static int marvell_pre_reset(struct ata_link *link, unsigned long deadline)
63 {
64         struct ata_port *ap = link->ap;
65         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
66
67         if (pdev->device == 0x6145 && ap->port_no == 0 &&
68                 !marvell_pata_active(pdev))     /* PATA enable ? */
69                         return -ENOENT;
70
71         return ata_sff_prereset(link, deadline);
72 }
73
74 static int marvell_cable_detect(struct ata_port *ap)
75 {
76         /* Cable type */
77         switch(ap->port_no)
78         {
79         case 0:
80                 if (ioread8(ap->ioaddr.bmdma_addr + 1) & 1)
81                         return ATA_CBL_PATA40;
82                 return ATA_CBL_PATA80;
83         case 1: /* Legacy SATA port */
84                 return ATA_CBL_SATA;
85         }
86
87         BUG();
88         return 0;       /* Our BUG macro needs the right markup */
89 }
90
91 /* No PIO or DMA methods needed for this device */
92
93 static struct scsi_host_template marvell_sht = {
94         ATA_BMDMA_SHT(DRV_NAME),
95 };
96
97 static struct ata_port_operations marvell_ops = {
98         .inherits               = &ata_bmdma_port_ops,
99         .cable_detect           = marvell_cable_detect,
100         .prereset               = marvell_pre_reset,
101 };
102
103
104 /**
105  *      marvell_init_one - Register Marvell ATA PCI device with kernel services
106  *      @pdev: PCI device to register
107  *      @id: PCI device ID
108  *
109  *      Called from kernel PCI layer.
110  *
111  *      LOCKING:
112  *      Inherited from PCI layer (may sleep).
113  *
114  *      RETURNS:
115  *      Zero on success, or -ERRNO value.
116  */
117
118 static int marvell_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
119 {
120         static const struct ata_port_info info = {
121                 .flags          = ATA_FLAG_SLAVE_POSS,
122
123                 .pio_mask       = ATA_PIO4,
124                 .mwdma_mask     = ATA_MWDMA2,
125                 .udma_mask      = ATA_UDMA5,
126
127                 .port_ops       = &marvell_ops,
128         };
129         static const struct ata_port_info info_sata = {
130                 /* Slave possible as its magically mapped not real */
131                 .flags          = ATA_FLAG_SLAVE_POSS,
132
133                 .pio_mask       = ATA_PIO4,
134                 .mwdma_mask     = ATA_MWDMA2,
135                 .udma_mask      = ATA_UDMA6,
136
137                 .port_ops       = &marvell_ops,
138         };
139         const struct ata_port_info *ppi[] = { &info, &info_sata };
140
141         if (pdev->device == 0x6101)
142                 ppi[1] = &ata_dummy_port_info;
143
144 #if IS_ENABLED(CONFIG_SATA_AHCI)
145         if (!marvell_pata_active(pdev)) {
146                 dev_info(&pdev->dev,
147                          "PATA port not active, deferring to AHCI driver.\n");
148                 return -ENODEV;
149         }
150 #endif
151         return ata_pci_bmdma_init_one(pdev, ppi, &marvell_sht, NULL, 0);
152 }
153
154 static const struct pci_device_id marvell_pci_tbl[] = {
155         { PCI_DEVICE(0x11AB, 0x6101), },
156         { PCI_DEVICE(0x11AB, 0x6121), },
157         { PCI_DEVICE(0x11AB, 0x6123), },
158         { PCI_DEVICE(0x11AB, 0x6145), },
159         { PCI_DEVICE(0x1B4B, 0x91A0), },
160         { PCI_DEVICE(0x1B4B, 0x91A4), },
161
162         { }     /* terminate list */
163 };
164
165 static struct pci_driver marvell_pci_driver = {
166         .name                   = DRV_NAME,
167         .id_table               = marvell_pci_tbl,
168         .probe                  = marvell_init_one,
169         .remove                 = ata_pci_remove_one,
170 #ifdef CONFIG_PM_SLEEP
171         .suspend                = ata_pci_device_suspend,
172         .resume                 = ata_pci_device_resume,
173 #endif
174 };
175
176 module_pci_driver(marvell_pci_driver);
177
178 MODULE_AUTHOR("Alan Cox");
179 MODULE_DESCRIPTION("SCSI low-level driver for Marvell ATA in legacy mode");
180 MODULE_LICENSE("GPL");
181 MODULE_DEVICE_TABLE(pci, marvell_pci_tbl);
182 MODULE_VERSION(DRV_VERSION);