Home | History | Annotate | Line # | Download | only in ata
      1 /*	$NetBSD: sata_subr.c,v 1.24 2018/06/21 21:52:15 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.24 2018/06/21 21:52:15 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 	sata_interpret_det(chp, sstatus);
    125 
    126 	return (sstatus & SStatus_DET_mask);
    127 }
    128 
    129 void
    130 sata_interpret_det(struct ata_channel *chp, uint32_t sstatus)
    131 {
    132 	switch (sstatus & SStatus_DET_mask) {
    133 	case SStatus_DET_NODEV:
    134 		/* No Device; be silent.  */
    135 		break;
    136 
    137 	case SStatus_DET_DEV_NE:
    138 		aprint_error("%s port %d: device connected, but "
    139 		    "communication not established\n",
    140 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel);
    141 		break;
    142 
    143 	case SStatus_DET_OFFLINE:
    144 		aprint_normal("%s port %d: PHY offline\n",
    145 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel);
    146 		break;
    147 
    148 	case SStatus_DET_DEV:
    149 		aprint_normal("%s port %d: device present, speed: %s\n",
    150 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
    151 		    sata_speed(sstatus));
    152 		break;
    153 	default:
    154 		aprint_error("%s port %d: unknown SStatus: 0x%08x\n",
    155 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
    156 		    sstatus);
    157 	}
    158 }
    159 
    160 void
    161 sata_interpret_sig(struct ata_channel *chp, int port, uint32_t sig)
    162 {
    163 	int err;
    164 
    165 	ata_channel_lock_owned(chp);
    166 
    167 	/* some ATAPI devices have bogus lower two bytes, sigh */
    168 	if ((sig & 0xffff0000) == 0xeb140000) {
    169 		sig &= 0xffff0000;
    170 		sig |= 0x00000101;
    171 	}
    172 	if (chp->ch_drive == NULL) {
    173 		if (sig == 0x96690101)
    174 			err = atabus_alloc_drives(chp, PMP_MAX_DRIVES);
    175 		else
    176 			err = atabus_alloc_drives(chp, 1);
    177 		if (err)
    178 			return;
    179 	}
    180 	KASSERT(port < chp->ch_ndrives);
    181 
    182 	switch(sig) {
    183 	case 0x96690101:
    184 		KASSERT(port == 0 || port == PMP_PORT_CTL);
    185 		chp->ch_drive[PMP_PORT_CTL].drive_type = ATA_DRIVET_PM;
    186 		break;
    187 	case 0xc33c0101:
    188 		aprint_verbose_dev(chp->atabus, "port %d is SEMB, ignored\n",
    189 		    port);
    190 		break;
    191 	case 0xeb140101:
    192 		chp->ch_drive[port].drive_type = ATA_DRIVET_ATAPI;
    193 		break;
    194 	case 0x00000101:
    195 		chp->ch_drive[port].drive_type = ATA_DRIVET_ATA;
    196 		break;
    197 	case 0xffffffff:
    198 		/* COMRESET time out */
    199 		break;
    200 	default:
    201 		chp->ch_drive[port].drive_type = ATA_DRIVET_ATA;
    202 		aprint_verbose_dev(chp->atabus,
    203 		    "Unrecognized signature 0x%08x on port %d. "
    204 		    "Assuming it's a disk.\n", sig, port);
    205 		break;
    206 	}
    207 }
    208