sata_subr.c revision 1.8 1 1.8 bouyer /* $NetBSD: sata_subr.c,v 1.8 2006/11/30 23:07:31 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 * 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.1 thorpej
47 1.1 thorpej #include <dev/ata/satareg.h>
48 1.1 thorpej #include <dev/ata/satavar.h>
49 1.1 thorpej
50 1.1 thorpej /*
51 1.1 thorpej * sata_speed:
52 1.1 thorpej *
53 1.1 thorpej * Return a string describing the port speed reported by
54 1.1 thorpej * the port's SStatus register.
55 1.1 thorpej */
56 1.1 thorpej const char *
57 1.1 thorpej sata_speed(uint32_t sstatus)
58 1.1 thorpej {
59 1.1 thorpej static const char *sata_speedtab[] = {
60 1.1 thorpej "no negotiated speed",
61 1.1 thorpej "1.5Gb/s",
62 1.8 bouyer "3.0Gb/s",
63 1.1 thorpej "<unknown 3>",
64 1.1 thorpej "<unknown 4>",
65 1.1 thorpej "<unknown 5>",
66 1.1 thorpej "<unknown 6>",
67 1.1 thorpej "<unknown 7>",
68 1.1 thorpej "<unknown 8>",
69 1.1 thorpej "<unknown 9>",
70 1.1 thorpej "<unknown 10>",
71 1.1 thorpej "<unknown 11>",
72 1.1 thorpej "<unknown 12>",
73 1.1 thorpej "<unknown 13>",
74 1.1 thorpej "<unknown 14>",
75 1.1 thorpej "<unknown 15>",
76 1.1 thorpej };
77 1.1 thorpej
78 1.1 thorpej return (sata_speedtab[(sstatus & SStatus_SPD_mask) >>
79 1.1 thorpej SStatus_SPD_shift]);
80 1.1 thorpej }
81 1.3 bouyer
82 1.3 bouyer /*
83 1.3 bouyer * reset the PHY and bring it online
84 1.3 bouyer */
85 1.3 bouyer uint32_t
86 1.3 bouyer sata_reset_interface(struct ata_channel *chp, bus_space_tag_t sata_t,
87 1.3 bouyer bus_space_handle_t scontrol_r, bus_space_handle_t sstatus_r)
88 1.3 bouyer {
89 1.3 bouyer uint32_t scontrol, sstatus;
90 1.3 bouyer int i;
91 1.3 bouyer
92 1.3 bouyer /* bring the PHYs online.
93 1.3 bouyer * The work-around for errata #1 for the 31244 says that we must
94 1.3 bouyer * write 0 to the port first to be sure of correctly initializing
95 1.3 bouyer * the device. It doesn't hurt for other devices.
96 1.3 bouyer */
97 1.3 bouyer bus_space_write_4(sata_t, scontrol_r, 0, 0);
98 1.3 bouyer scontrol = SControl_IPM_NONE | SControl_SPD_ANY | SControl_DET_INIT;
99 1.3 bouyer bus_space_write_4 (sata_t, scontrol_r, 0, scontrol);
100 1.3 bouyer
101 1.3 bouyer tsleep(chp, PRIBIO, "sataup", mstohz(50));
102 1.3 bouyer scontrol &= ~SControl_DET_INIT;
103 1.3 bouyer bus_space_write_4(sata_t, scontrol_r, 0, scontrol);
104 1.3 bouyer
105 1.3 bouyer tsleep(chp, PRIBIO, "sataup", mstohz(50));
106 1.3 bouyer /* wait up to 1s for device to come up */
107 1.3 bouyer for (i = 0; i < 100; i++) {
108 1.3 bouyer sstatus = bus_space_read_4(sata_t, sstatus_r, 0);
109 1.3 bouyer if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
110 1.3 bouyer break;
111 1.3 bouyer tsleep(chp, PRIBIO, "sataup", mstohz(10));
112 1.3 bouyer }
113 1.3 bouyer
114 1.3 bouyer switch (sstatus & SStatus_DET_mask) {
115 1.3 bouyer case SStatus_DET_NODEV:
116 1.3 bouyer /* No Device; be silent. */
117 1.3 bouyer break;
118 1.3 bouyer
119 1.3 bouyer case SStatus_DET_DEV_NE:
120 1.5 bouyer aprint_error("%s port %d: device connected, but "
121 1.3 bouyer "communication not established\n",
122 1.3 bouyer chp->ch_atac->atac_dev.dv_xname, chp->ch_channel);
123 1.3 bouyer break;
124 1.3 bouyer
125 1.3 bouyer case SStatus_DET_OFFLINE:
126 1.5 bouyer aprint_error("%s port %d: PHY offline\n",
127 1.3 bouyer chp->ch_atac->atac_dev.dv_xname, chp->ch_channel);
128 1.3 bouyer break;
129 1.3 bouyer
130 1.3 bouyer case SStatus_DET_DEV:
131 1.5 bouyer aprint_normal("%s port %d: device present, speed: %s\n",
132 1.3 bouyer chp->ch_atac->atac_dev.dv_xname, chp->ch_channel,
133 1.3 bouyer sata_speed(sstatus));
134 1.3 bouyer break;
135 1.3 bouyer default:
136 1.5 bouyer aprint_error("%s port %d: unknown SStatus: 0x%08x\n",
137 1.3 bouyer chp->ch_atac->atac_dev.dv_xname, chp->ch_channel,
138 1.3 bouyer sstatus);
139 1.3 bouyer }
140 1.4 bouyer return(sstatus & SStatus_DET_mask);
141 1.3 bouyer }
142