1 /* $NetBSD: sata_subr.c,v 1.11 2008/04/28 20:23:47 martin Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of Wasabi Systems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Common functions for Serial ATA.
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: sata_subr.c,v 1.11 2008/04/28 20:23:47 martin Exp $");
38 #include <sys/param.h>
39 #include <sys/kernel.h>
42 #include <dev/ata/satareg.h>
43 #include <dev/ata/satavar.h>
48 * Return a string describing the port speed reported by
49 * the port's SStatus register.
52 sata_speed(uint32_t sstatus
)
54 static const char *sata_speedtab
[] = {
55 "no negotiated speed",
73 return (sata_speedtab
[(sstatus
& SStatus_SPD_mask
) >>
78 * reset the PHY and bring it online
81 sata_reset_interface(struct ata_channel
*chp
, bus_space_tag_t sata_t
,
82 bus_space_handle_t scontrol_r
, bus_space_handle_t sstatus_r
)
84 uint32_t scontrol
, sstatus
;
87 /* bring the PHYs online.
88 * The work-around for errata #1 for the 31244 says that we must
89 * write 0 to the port first to be sure of correctly initializing
90 * the device. It doesn't hurt for other devices.
92 bus_space_write_4(sata_t
, scontrol_r
, 0, 0);
93 scontrol
= SControl_IPM_NONE
| SControl_SPD_ANY
| SControl_DET_INIT
;
94 bus_space_write_4 (sata_t
, scontrol_r
, 0, scontrol
);
96 tsleep(chp
, PRIBIO
, "sataup", mstohz(50));
97 scontrol
&= ~SControl_DET_INIT
;
98 bus_space_write_4(sata_t
, scontrol_r
, 0, scontrol
);
100 tsleep(chp
, PRIBIO
, "sataup", mstohz(50));
101 /* wait up to 1s for device to come up */
102 for (i
= 0; i
< 100; i
++) {
103 sstatus
= bus_space_read_4(sata_t
, sstatus_r
, 0);
104 if ((sstatus
& SStatus_DET_mask
) == SStatus_DET_DEV
)
106 tsleep(chp
, PRIBIO
, "sataup", mstohz(10));
109 switch (sstatus
& SStatus_DET_mask
) {
110 case SStatus_DET_NODEV
:
111 /* No Device; be silent. */
114 case SStatus_DET_DEV_NE
:
115 aprint_error("%s port %d: device connected, but "
116 "communication not established\n",
117 device_xname(chp
->ch_atac
->atac_dev
), chp
->ch_channel
);
120 case SStatus_DET_OFFLINE
:
121 aprint_error("%s port %d: PHY offline\n",
122 device_xname(chp
->ch_atac
->atac_dev
), chp
->ch_channel
);
125 case SStatus_DET_DEV
:
126 aprint_normal("%s port %d: device present, speed: %s\n",
127 device_xname(chp
->ch_atac
->atac_dev
), chp
->ch_channel
,
128 sata_speed(sstatus
));
131 aprint_error("%s port %d: unknown SStatus: 0x%08x\n",
132 device_xname(chp
->ch_atac
->atac_dev
), chp
->ch_channel
,
135 return(sstatus
& SStatus_DET_mask
);