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