Home | History | Annotate | Line # | Download | only in ata
sata_subr.c revision 1.19
      1 /*	$NetBSD: sata_subr.c,v 1.19 2012/07/26 20:49:47 jakllsch 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.19 2012/07/26 20:49:47 jakllsch 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 
     45 /*
     46  * sata_speed:
     47  *
     48  *	Return a string describing the port speed reported by
     49  *	the port's SStatus register.
     50  */
     51 const char *
     52 sata_speed(uint32_t sstatus)
     53 {
     54 	static const char * const sata_speedtab[] = {
     55 		"no negotiated speed",
     56 		"1.5Gb/s",
     57 		"3.0Gb/s",
     58 		"6.0Gb/s",
     59 		"<unknown 4>",
     60 		"<unknown 5>",
     61 		"<unknown 6>",
     62 		"<unknown 7>",
     63 		"<unknown 8>",
     64 		"<unknown 9>",
     65 		"<unknown 10>",
     66 		"<unknown 11>",
     67 		"<unknown 12>",
     68 		"<unknown 13>",
     69 		"<unknown 14>",
     70 		"<unknown 15>",
     71 	};
     72 
     73 	return (sata_speedtab[(sstatus & SStatus_SPD_mask) >>
     74 			      SStatus_SPD_shift]);
     75 }
     76 
     77 /*
     78  * reset the PHY and bring it online
     79  */
     80 uint32_t
     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)
     83 {
     84 	uint32_t scontrol, sstatus;
     85 	int i;
     86 
     87 	/* bring the PHYs online.
     88 	 * The work-around for errata #1 of the Intel GD31244 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.
     91 	 */
     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);
     95 
     96 	tsleep(chp, PRIBIO, "sataup", mstohz(50));
     97 	scontrol &= ~SControl_DET_INIT;
     98 	bus_space_write_4(sata_t, scontrol_r, 0, scontrol);
     99 
    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)
    105 			break;
    106 		tsleep(chp, PRIBIO, "sataup", mstohz(10));
    107 	}
    108 	/*
    109 	 * if we have a link up without device, wait a few more seconds
    110 	 * for connection to establish
    111 	 */
    112 	if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV_NE) {
    113 		for (i = 0; i < 500; i++) {
    114 			tsleep(chp, PRIBIO, "sataup", mstohz(10));
    115 			sstatus = bus_space_read_4(sata_t, sstatus_r, 0);
    116 			if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
    117 				break;
    118 		}
    119 	}
    120 
    121 	switch (sstatus & SStatus_DET_mask) {
    122 	case SStatus_DET_NODEV:
    123 		/* No Device; be silent.  */
    124 		break;
    125 
    126 	case SStatus_DET_DEV_NE:
    127 		aprint_error("%s port %d: device connected, but "
    128 		    "communication not established\n",
    129 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel);
    130 		break;
    131 
    132 	case SStatus_DET_OFFLINE:
    133 		aprint_error("%s port %d: PHY offline\n",
    134 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel);
    135 		break;
    136 
    137 	case SStatus_DET_DEV:
    138 		aprint_normal("%s port %d: device present, speed: %s\n",
    139 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
    140 		    sata_speed(sstatus));
    141 		break;
    142 	default:
    143 		aprint_error("%s port %d: unknown SStatus: 0x%08x\n",
    144 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
    145 		    sstatus);
    146 	}
    147 	return(sstatus & SStatus_DET_mask);
    148 }
    149