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