kernel/nata: Deal with ATA_DEV() and atadev->unit.
[dragonfly.git] / sys / dev / disk / nata / chipsets / ata-ite.c
1 /*-
2  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /* local prototypes */
28 static int ata_ite_chipinit(device_t dev);
29 static void ata_ite_setmode(device_t dev, int mode);
30
31 /*
32  * Integrated Technology Express Inc. (ITE) chipset support functions
33  */
34 int
35 ata_ite_ident(device_t dev)
36 {
37     struct ata_pci_controller *ctlr = device_get_softc(dev);
38     static const struct ata_chip_id ids[] =
39     {{ ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8212F" },
40      { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8211F" },
41      { 0, 0, 0, 0, 0, 0}};
42
43     if (pci_get_vendor(dev) != ATA_ITE_ID)
44         return ENXIO;
45
46     if (!(ctlr->chip = ata_match_chip(dev, ids)))
47         return ENXIO;
48
49     ata_set_desc(dev);
50     ctlr->chipinit = ata_ite_chipinit;
51     return 0;
52 }
53
54 static int
55 ata_ite_chipinit(device_t dev)
56 {
57     struct ata_pci_controller *ctlr = device_get_softc(dev);
58
59     if (ata_setup_interrupt(dev, ata_generic_intr))
60         return ENXIO;
61
62     ctlr->setmode = ata_ite_setmode;
63
64     /* set PCI mode and 66Mhz reference clock */
65     pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
66
67     /* set default active & recover timings */
68     pci_write_config(dev, 0x54, 0x31, 1);
69     pci_write_config(dev, 0x56, 0x31, 1);
70     return 0;
71 }
72
73 static void
74 ata_ite_setmode(device_t dev, int mode)
75 {
76     device_t gparent = GRANDPARENT(dev);
77     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
78     struct ata_device *atadev = device_get_softc(dev);
79     int devno = (ch->unit << 1) + atadev->unit;
80     int error;
81         static const uint8_t udmatiming[] =
82                 { 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
83         static const uint8_t chtiming[] =
84                 { 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
85
86     /* correct the mode for what the HW supports */
87     mode = ata_limit_mode(dev, mode, ATA_UDMA6);
88
89     /* check the CBLID bits for 80 conductor cable detection */
90     if (mode > ATA_UDMA2 && (pci_read_config(gparent, 0x40, 2) &
91                              (ch->unit ? (1<<3) : (1<<2)))) {
92         ata_print_cable(dev, "controller");
93         mode = ATA_UDMA2;
94     }
95
96     /* set the wanted mode on the device */
97     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
98
99     if (bootverbose)
100         device_printf(dev, "%s setting %s on ITE8212F chip\n",
101                       (error) ? "failed" : "success", ata_mode2str(mode));
102
103     /* if the device accepted the mode change, setup the HW accordingly */
104     if (!error) {
105         if (mode >= ATA_UDMA0) {
106             /* enable UDMA mode */
107             pci_write_config(gparent, 0x50,
108                              pci_read_config(gparent, 0x50, 1) &
109                              ~(1 << (devno + 3)), 1);
110
111             /* set UDMA timing */
112             pci_write_config(gparent,
113                              0x56 + (ch->unit << 2) + atadev->unit,
114                              udmatiming[mode & ATA_MODE_MASK], 1);
115         }
116         else {
117             /* disable UDMA mode */
118             pci_write_config(gparent, 0x50,
119                              pci_read_config(gparent, 0x50, 1) |
120                              (1 << (devno + 3)), 1);
121
122             /* set active and recover timing (shared between master & slave) */
123             if (pci_read_config(gparent, 0x54 + (ch->unit << 2), 1) <
124                 chtiming[ata_mode2idx(mode)])
125                 pci_write_config(gparent, 0x54 + (ch->unit << 2),
126                                  chtiming[ata_mode2idx(mode)], 1);
127         }
128         atadev->mode = mode;
129     }
130 }