sata_subr.c revision 1.6 1 1.6 dogcow /* $NetBSD: sata_subr.c,v 1.6 2006/11/22 22:14:23 dogcow 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 * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.1 thorpej * This product includes software developed by the NetBSD
21 1.1 thorpej * Foundation, Inc. and its contributors.
22 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej /*
40 1.1 thorpej * Common functions for Serial ATA.
41 1.1 thorpej */
42 1.1 thorpej
43 1.1 thorpej #include <sys/param.h>
44 1.3 bouyer #include <sys/kernel.h>
45 1.3 bouyer #include <sys/proc.h>
46 1.6 dogcow #include <sys/device.h>
47 1.1 thorpej
48 1.1 thorpej #include <dev/ata/satareg.h>
49 1.1 thorpej #include <dev/ata/satavar.h>
50 1.1 thorpej
51 1.1 thorpej /*
52 1.1 thorpej * sata_speed:
53 1.1 thorpej *
54 1.1 thorpej * Return a string describing the port speed reported by
55 1.1 thorpej * the port's SStatus register.
56 1.1 thorpej */
57 1.1 thorpej const char *
58 1.1 thorpej sata_speed(uint32_t sstatus)
59 1.1 thorpej {
60 1.1 thorpej static const char *sata_speedtab[] = {
61 1.1 thorpej "no negotiated speed",
62 1.1 thorpej "1.5Gb/s",
63 1.1 thorpej "3.0GB/s",
64 1.1 thorpej "<unknown 3>",
65 1.1 thorpej "<unknown 4>",
66 1.1 thorpej "<unknown 5>",
67 1.1 thorpej "<unknown 6>",
68 1.1 thorpej "<unknown 7>",
69 1.1 thorpej "<unknown 8>",
70 1.1 thorpej "<unknown 9>",
71 1.1 thorpej "<unknown 10>",
72 1.1 thorpej "<unknown 11>",
73 1.1 thorpej "<unknown 12>",
74 1.1 thorpej "<unknown 13>",
75 1.1 thorpej "<unknown 14>",
76 1.1 thorpej "<unknown 15>",
77 1.1 thorpej };
78 1.1 thorpej
79 1.1 thorpej return (sata_speedtab[(sstatus & SStatus_SPD_mask) >>
80 1.1 thorpej SStatus_SPD_shift]);
81 1.1 thorpej }
82 1.3 bouyer
83 1.3 bouyer /*
84 1.3 bouyer * reset the PHY and bring it online
85 1.3 bouyer */
86 1.3 bouyer uint32_t
87 1.3 bouyer sata_reset_interface(struct ata_channel *chp, bus_space_tag_t sata_t,
88 1.3 bouyer bus_space_handle_t scontrol_r, bus_space_handle_t sstatus_r)
89 1.3 bouyer {
90 1.3 bouyer uint32_t scontrol, sstatus;
91 1.3 bouyer int i;
92 1.3 bouyer
93 1.3 bouyer /* bring the PHYs online.
94 1.3 bouyer * The work-around for errata #1 for the 31244 says that we must
95 1.3 bouyer * write 0 to the port first to be sure of correctly initializing
96 1.3 bouyer * the device. It doesn't hurt for other devices.
97 1.3 bouyer */
98 1.3 bouyer bus_space_write_4(sata_t, scontrol_r, 0, 0);
99 1.3 bouyer scontrol = SControl_IPM_NONE | SControl_SPD_ANY | SControl_DET_INIT;
100 1.3 bouyer bus_space_write_4 (sata_t, scontrol_r, 0, scontrol);
101 1.3 bouyer
102 1.3 bouyer tsleep(chp, PRIBIO, "sataup", mstohz(50));
103 1.3 bouyer scontrol &= ~SControl_DET_INIT;
104 1.3 bouyer bus_space_write_4(sata_t, scontrol_r, 0, scontrol);
105 1.3 bouyer
106 1.3 bouyer tsleep(chp, PRIBIO, "sataup", mstohz(50));
107 1.3 bouyer /* wait up to 1s for device to come up */
108 1.3 bouyer for (i = 0; i < 100; i++) {
109 1.3 bouyer sstatus = bus_space_read_4(sata_t, sstatus_r, 0);
110 1.3 bouyer if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
111 1.3 bouyer break;
112 1.3 bouyer tsleep(chp, PRIBIO, "sataup", mstohz(10));
113 1.3 bouyer }
114 1.3 bouyer
115 1.3 bouyer switch (sstatus & SStatus_DET_mask) {
116 1.3 bouyer case SStatus_DET_NODEV:
117 1.3 bouyer /* No Device; be silent. */
118 1.3 bouyer break;
119 1.3 bouyer
120 1.3 bouyer case SStatus_DET_DEV_NE:
121 1.5 bouyer aprint_error("%s port %d: device connected, but "
122 1.3 bouyer "communication not established\n",
123 1.3 bouyer chp->ch_atac->atac_dev.dv_xname, chp->ch_channel);
124 1.3 bouyer break;
125 1.3 bouyer
126 1.3 bouyer case SStatus_DET_OFFLINE:
127 1.5 bouyer aprint_error("%s port %d: PHY offline\n",
128 1.3 bouyer chp->ch_atac->atac_dev.dv_xname, chp->ch_channel);
129 1.3 bouyer break;
130 1.3 bouyer
131 1.3 bouyer case SStatus_DET_DEV:
132 1.5 bouyer aprint_normal("%s port %d: device present, speed: %s\n",
133 1.3 bouyer chp->ch_atac->atac_dev.dv_xname, chp->ch_channel,
134 1.3 bouyer sata_speed(sstatus));
135 1.3 bouyer break;
136 1.3 bouyer default:
137 1.5 bouyer aprint_error("%s port %d: unknown SStatus: 0x%08x\n",
138 1.3 bouyer chp->ch_atac->atac_dev.dv_xname, chp->ch_channel,
139 1.3 bouyer sstatus);
140 1.3 bouyer }
141 1.4 bouyer return(sstatus & SStatus_DET_mask);
142 1.3 bouyer }
143