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