sata_subr.c revision 1.21 1 1.21 bouyer /* $NetBSD: sata_subr.c,v 1.21 2013/04/03 17:15:07 bouyer Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of Wasabi Systems, Inc.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej /*
33 1.1 thorpej * Common functions for Serial ATA.
34 1.1 thorpej */
35 1.9 lukem #include <sys/cdefs.h>
36 1.21 bouyer __KERNEL_RCSID(0, "$NetBSD: sata_subr.c,v 1.21 2013/04/03 17:15:07 bouyer Exp $");
37 1.1 thorpej
38 1.1 thorpej #include <sys/param.h>
39 1.3 bouyer #include <sys/kernel.h>
40 1.3 bouyer #include <sys/proc.h>
41 1.1 thorpej
42 1.1 thorpej #include <dev/ata/satareg.h>
43 1.1 thorpej #include <dev/ata/satavar.h>
44 1.20 bouyer #include <dev/ata/satapmpreg.h>
45 1.1 thorpej
46 1.1 thorpej /*
47 1.1 thorpej * sata_speed:
48 1.1 thorpej *
49 1.1 thorpej * Return a string describing the port speed reported by
50 1.1 thorpej * the port's SStatus register.
51 1.1 thorpej */
52 1.1 thorpej const char *
53 1.1 thorpej sata_speed(uint32_t sstatus)
54 1.1 thorpej {
55 1.13 matt static const char * const sata_speedtab[] = {
56 1.1 thorpej "no negotiated speed",
57 1.1 thorpej "1.5Gb/s",
58 1.8 bouyer "3.0Gb/s",
59 1.12 cegger "6.0Gb/s",
60 1.1 thorpej "<unknown 4>",
61 1.1 thorpej "<unknown 5>",
62 1.1 thorpej "<unknown 6>",
63 1.1 thorpej "<unknown 7>",
64 1.1 thorpej "<unknown 8>",
65 1.1 thorpej "<unknown 9>",
66 1.1 thorpej "<unknown 10>",
67 1.1 thorpej "<unknown 11>",
68 1.1 thorpej "<unknown 12>",
69 1.1 thorpej "<unknown 13>",
70 1.1 thorpej "<unknown 14>",
71 1.1 thorpej "<unknown 15>",
72 1.1 thorpej };
73 1.1 thorpej
74 1.1 thorpej return (sata_speedtab[(sstatus & SStatus_SPD_mask) >>
75 1.1 thorpej SStatus_SPD_shift]);
76 1.1 thorpej }
77 1.3 bouyer
78 1.3 bouyer /*
79 1.3 bouyer * reset the PHY and bring it online
80 1.3 bouyer */
81 1.3 bouyer uint32_t
82 1.3 bouyer sata_reset_interface(struct ata_channel *chp, bus_space_tag_t sata_t,
83 1.21 bouyer bus_space_handle_t scontrol_r, bus_space_handle_t sstatus_r, int flags)
84 1.3 bouyer {
85 1.3 bouyer uint32_t scontrol, sstatus;
86 1.3 bouyer int i;
87 1.3 bouyer
88 1.3 bouyer /* bring the PHYs online.
89 1.14 jakllsch * The work-around for errata #1 of the Intel GD31244 says that we must
90 1.3 bouyer * write 0 to the port first to be sure of correctly initializing
91 1.3 bouyer * the device. It doesn't hurt for other devices.
92 1.3 bouyer */
93 1.3 bouyer bus_space_write_4(sata_t, scontrol_r, 0, 0);
94 1.3 bouyer scontrol = SControl_IPM_NONE | SControl_SPD_ANY | SControl_DET_INIT;
95 1.3 bouyer bus_space_write_4 (sata_t, scontrol_r, 0, scontrol);
96 1.3 bouyer
97 1.21 bouyer ata_delay(50, "sataup", flags);
98 1.3 bouyer scontrol &= ~SControl_DET_INIT;
99 1.3 bouyer bus_space_write_4(sata_t, scontrol_r, 0, scontrol);
100 1.3 bouyer
101 1.21 bouyer ata_delay(50, "sataup", flags);
102 1.3 bouyer /* wait up to 1s for device to come up */
103 1.3 bouyer for (i = 0; i < 100; i++) {
104 1.3 bouyer sstatus = bus_space_read_4(sata_t, sstatus_r, 0);
105 1.3 bouyer if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
106 1.3 bouyer break;
107 1.21 bouyer ata_delay(10, "sataup", flags);
108 1.3 bouyer }
109 1.15 bouyer /*
110 1.15 bouyer * if we have a link up without device, wait a few more seconds
111 1.15 bouyer * for connection to establish
112 1.15 bouyer */
113 1.15 bouyer if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV_NE) {
114 1.15 bouyer for (i = 0; i < 500; i++) {
115 1.21 bouyer ata_delay(10, "sataup", flags);
116 1.15 bouyer sstatus = bus_space_read_4(sata_t, sstatus_r, 0);
117 1.15 bouyer if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
118 1.15 bouyer break;
119 1.15 bouyer }
120 1.15 bouyer }
121 1.3 bouyer
122 1.3 bouyer switch (sstatus & SStatus_DET_mask) {
123 1.3 bouyer case SStatus_DET_NODEV:
124 1.3 bouyer /* No Device; be silent. */
125 1.3 bouyer break;
126 1.3 bouyer
127 1.3 bouyer case SStatus_DET_DEV_NE:
128 1.5 bouyer aprint_error("%s port %d: device connected, but "
129 1.3 bouyer "communication not established\n",
130 1.10 cube device_xname(chp->ch_atac->atac_dev), chp->ch_channel);
131 1.3 bouyer break;
132 1.3 bouyer
133 1.3 bouyer case SStatus_DET_OFFLINE:
134 1.5 bouyer aprint_error("%s port %d: PHY offline\n",
135 1.10 cube device_xname(chp->ch_atac->atac_dev), chp->ch_channel);
136 1.3 bouyer break;
137 1.3 bouyer
138 1.3 bouyer case SStatus_DET_DEV:
139 1.5 bouyer aprint_normal("%s port %d: device present, speed: %s\n",
140 1.10 cube device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
141 1.3 bouyer sata_speed(sstatus));
142 1.3 bouyer break;
143 1.3 bouyer default:
144 1.5 bouyer aprint_error("%s port %d: unknown SStatus: 0x%08x\n",
145 1.10 cube device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
146 1.3 bouyer sstatus);
147 1.3 bouyer }
148 1.4 bouyer return(sstatus & SStatus_DET_mask);
149 1.3 bouyer }
150 1.20 bouyer
151 1.20 bouyer void
152 1.20 bouyer sata_interpret_sig(struct ata_channel *chp, int port, uint32_t sig)
153 1.20 bouyer {
154 1.20 bouyer int err;
155 1.20 bouyer int s;
156 1.20 bouyer
157 1.20 bouyer /* some ATAPI devices have bogus lower two bytes, sigh */
158 1.20 bouyer if ((sig & 0xffff0000) == 0xeb140000) {
159 1.20 bouyer sig &= 0xffff0000;
160 1.20 bouyer sig |= 0x00000101;
161 1.20 bouyer }
162 1.20 bouyer if (chp->ch_drive == NULL) {
163 1.20 bouyer if (sig == 0x96690101)
164 1.20 bouyer err = atabus_alloc_drives(chp, PMP_MAX_DRIVES);
165 1.20 bouyer else
166 1.20 bouyer err = atabus_alloc_drives(chp, 1);
167 1.20 bouyer if (err)
168 1.20 bouyer return;
169 1.20 bouyer }
170 1.20 bouyer KASSERT(port < chp->ch_ndrives);
171 1.20 bouyer
172 1.20 bouyer s = splbio();
173 1.20 bouyer switch(sig) {
174 1.20 bouyer case 0x96690101:
175 1.20 bouyer KASSERT(port == 0 || port == PMP_PORT_CTL);
176 1.20 bouyer chp->ch_drive[PMP_PORT_CTL].drive_type = ATA_DRIVET_PM;
177 1.20 bouyer break;
178 1.20 bouyer case 0xc33c0101:
179 1.20 bouyer aprint_verbose_dev(chp->atabus, "port %d is SEMB, ignored\n",
180 1.20 bouyer port);
181 1.20 bouyer break;
182 1.20 bouyer case 0xeb140101:
183 1.20 bouyer chp->ch_drive[port].drive_type = ATA_DRIVET_ATAPI;
184 1.20 bouyer break;
185 1.20 bouyer case 0x00000101:
186 1.20 bouyer chp->ch_drive[port].drive_type = ATA_DRIVET_ATA;
187 1.20 bouyer break;
188 1.20 bouyer case 0xffffffff:
189 1.20 bouyer /* COMRESET time out */
190 1.20 bouyer break;
191 1.20 bouyer default:
192 1.20 bouyer chp->ch_drive[port].drive_type = ATA_DRIVET_ATA;
193 1.20 bouyer aprint_verbose_dev(chp->atabus,
194 1.20 bouyer "Unrecognized signature 0x%08x on port %d. "
195 1.20 bouyer "Assuming it's a disk.\n", sig, port);
196 1.20 bouyer break;
197 1.20 bouyer }
198 1.20 bouyer splx(s);
199 1.20 bouyer }
200