1 /* $NetBSD: sata_subr.c,v 1.23 2017/10/07 16:05:32 jdolecek 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.23 2017/10/07 16:05:32 jdolecek 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 * 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 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 ata_channel_lock_owned(chp); 89 90 /* bring the PHYs online. 91 * The work-around for errata #1 of the Intel GD31244 says that we must 92 * write 0 to the port first to be sure of correctly initializing 93 * the device. It doesn't hurt for other devices. 94 */ 95 bus_space_write_4(sata_t, scontrol_r, 0, 0); 96 scontrol = SControl_IPM_NONE | SControl_SPD_ANY | SControl_DET_INIT; 97 bus_space_write_4(sata_t, scontrol_r, 0, scontrol); 98 99 ata_delay(chp, 50, "sataup", flags); 100 scontrol &= ~SControl_DET_INIT; 101 bus_space_write_4(sata_t, scontrol_r, 0, scontrol); 102 103 ata_delay(chp, 50, "sataup", flags); 104 /* wait up to 1s for device to come up */ 105 for (i = 0; i < 100; i++) { 106 sstatus = bus_space_read_4(sata_t, sstatus_r, 0); 107 if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV) 108 break; 109 ata_delay(chp, 10, "sataup", flags); 110 } 111 /* 112 * if we have a link up without device, wait a few more seconds 113 * for connection to establish 114 */ 115 if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV_NE) { 116 for (i = 0; i < 500; i++) { 117 ata_delay(chp, 10, "sataup", flags); 118 sstatus = bus_space_read_4(sata_t, sstatus_r, 0); 119 if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV) 120 break; 121 } 122 } 123 124 switch (sstatus & SStatus_DET_mask) { 125 case SStatus_DET_NODEV: 126 /* No Device; be silent. */ 127 break; 128 129 case SStatus_DET_DEV_NE: 130 aprint_error("%s port %d: device connected, but " 131 "communication not established\n", 132 device_xname(chp->ch_atac->atac_dev), chp->ch_channel); 133 break; 134 135 case SStatus_DET_OFFLINE: 136 aprint_error("%s port %d: PHY offline\n", 137 device_xname(chp->ch_atac->atac_dev), chp->ch_channel); 138 break; 139 140 case SStatus_DET_DEV: 141 aprint_normal("%s port %d: device present, speed: %s\n", 142 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 143 sata_speed(sstatus)); 144 break; 145 default: 146 aprint_error("%s port %d: unknown SStatus: 0x%08x\n", 147 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 148 sstatus); 149 } 150 return(sstatus & SStatus_DET_mask); 151 } 152 153 void 154 sata_interpret_sig(struct ata_channel *chp, int port, uint32_t sig) 155 { 156 int err; 157 158 ata_channel_lock_owned(chp); 159 160 /* some ATAPI devices have bogus lower two bytes, sigh */ 161 if ((sig & 0xffff0000) == 0xeb140000) { 162 sig &= 0xffff0000; 163 sig |= 0x00000101; 164 } 165 if (chp->ch_drive == NULL) { 166 if (sig == 0x96690101) 167 err = atabus_alloc_drives(chp, PMP_MAX_DRIVES); 168 else 169 err = atabus_alloc_drives(chp, 1); 170 if (err) 171 return; 172 } 173 KASSERT(port < chp->ch_ndrives); 174 175 switch(sig) { 176 case 0x96690101: 177 KASSERT(port == 0 || port == PMP_PORT_CTL); 178 chp->ch_drive[PMP_PORT_CTL].drive_type = ATA_DRIVET_PM; 179 break; 180 case 0xc33c0101: 181 aprint_verbose_dev(chp->atabus, "port %d is SEMB, ignored\n", 182 port); 183 break; 184 case 0xeb140101: 185 chp->ch_drive[port].drive_type = ATA_DRIVET_ATAPI; 186 break; 187 case 0x00000101: 188 chp->ch_drive[port].drive_type = ATA_DRIVET_ATA; 189 break; 190 case 0xffffffff: 191 /* COMRESET time out */ 192 break; 193 default: 194 chp->ch_drive[port].drive_type = ATA_DRIVET_ATA; 195 aprint_verbose_dev(chp->atabus, 196 "Unrecognized signature 0x%08x on port %d. " 197 "Assuming it's a disk.\n", sig, port); 198 break; 199 } 200 } 201