Home | History | Annotate | Line # | Download | only in ata
sata_subr.c revision 1.22
      1  1.22   msaitoh /*	$NetBSD: sata_subr.c,v 1.22 2017/05/10 08:46:39 msaitoh Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4   1.1   thorpej  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1   thorpej  * by Jason R. Thorpe of Wasabi Systems, Inc.
      9   1.1   thorpej  *
     10   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.1   thorpej  * modification, are permitted provided that the following conditions
     12   1.1   thorpej  * are met:
     13   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.1   thorpej  *
     19   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1   thorpej  */
     31   1.1   thorpej 
     32   1.1   thorpej /*
     33   1.1   thorpej  * Common functions for Serial ATA.
     34   1.1   thorpej  */
     35   1.9     lukem #include <sys/cdefs.h>
     36  1.22   msaitoh __KERNEL_RCSID(0, "$NetBSD: sata_subr.c,v 1.22 2017/05/10 08:46:39 msaitoh Exp $");
     37   1.1   thorpej 
     38   1.1   thorpej #include <sys/param.h>
     39   1.3    bouyer #include <sys/kernel.h>
     40   1.3    bouyer #include <sys/proc.h>
     41   1.1   thorpej 
     42   1.1   thorpej #include <dev/ata/satareg.h>
     43   1.1   thorpej #include <dev/ata/satavar.h>
     44  1.20    bouyer #include <dev/ata/satapmpreg.h>
     45   1.1   thorpej 
     46   1.1   thorpej /*
     47   1.1   thorpej  * sata_speed:
     48   1.1   thorpej  *
     49   1.1   thorpej  *	Return a string describing the port speed reported by
     50   1.1   thorpej  *	the port's SStatus register.
     51   1.1   thorpej  */
     52   1.1   thorpej const char *
     53   1.1   thorpej sata_speed(uint32_t sstatus)
     54   1.1   thorpej {
     55  1.13      matt 	static const char * const sata_speedtab[] = {
     56   1.1   thorpej 		"no negotiated speed",
     57   1.1   thorpej 		"1.5Gb/s",
     58   1.8    bouyer 		"3.0Gb/s",
     59  1.12    cegger 		"6.0Gb/s",
     60   1.1   thorpej 		"<unknown 4>",
     61   1.1   thorpej 		"<unknown 5>",
     62   1.1   thorpej 		"<unknown 6>",
     63   1.1   thorpej 		"<unknown 7>",
     64   1.1   thorpej 		"<unknown 8>",
     65   1.1   thorpej 		"<unknown 9>",
     66   1.1   thorpej 		"<unknown 10>",
     67   1.1   thorpej 		"<unknown 11>",
     68   1.1   thorpej 		"<unknown 12>",
     69   1.1   thorpej 		"<unknown 13>",
     70   1.1   thorpej 		"<unknown 14>",
     71   1.1   thorpej 		"<unknown 15>",
     72   1.1   thorpej 	};
     73   1.1   thorpej 
     74   1.1   thorpej 	return (sata_speedtab[(sstatus & SStatus_SPD_mask) >>
     75   1.1   thorpej 			      SStatus_SPD_shift]);
     76   1.1   thorpej }
     77   1.3    bouyer 
     78   1.3    bouyer /*
     79   1.3    bouyer  * reset the PHY and bring it online
     80   1.3    bouyer  */
     81   1.3    bouyer uint32_t
     82   1.3    bouyer sata_reset_interface(struct ata_channel *chp, bus_space_tag_t sata_t,
     83  1.21    bouyer     bus_space_handle_t scontrol_r, bus_space_handle_t sstatus_r, int flags)
     84   1.3    bouyer {
     85   1.3    bouyer 	uint32_t scontrol, sstatus;
     86   1.3    bouyer 	int i;
     87   1.3    bouyer 
     88  1.22   msaitoh 	/*
     89  1.22   msaitoh 	 * bring the PHYs online.
     90  1.14  jakllsch 	 * The work-around for errata #1 of the Intel GD31244 says that we must
     91   1.3    bouyer 	 * write 0 to the port first to be sure of correctly initializing
     92   1.3    bouyer 	 * the device. It doesn't hurt for other devices.
     93   1.3    bouyer 	 */
     94   1.3    bouyer 	bus_space_write_4(sata_t, scontrol_r, 0, 0);
     95   1.3    bouyer 	scontrol = SControl_IPM_NONE | SControl_SPD_ANY | SControl_DET_INIT;
     96  1.22   msaitoh 	bus_space_write_4(sata_t, scontrol_r, 0, scontrol);
     97   1.3    bouyer 
     98  1.21    bouyer 	ata_delay(50, "sataup", flags);
     99   1.3    bouyer 	scontrol &= ~SControl_DET_INIT;
    100   1.3    bouyer 	bus_space_write_4(sata_t, scontrol_r, 0, scontrol);
    101   1.3    bouyer 
    102  1.21    bouyer 	ata_delay(50, "sataup", flags);
    103   1.3    bouyer 	/* wait up to 1s for device to come up */
    104   1.3    bouyer 	for (i = 0; i < 100; i++) {
    105   1.3    bouyer 		sstatus = bus_space_read_4(sata_t, sstatus_r, 0);
    106   1.3    bouyer 		if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
    107   1.3    bouyer 			break;
    108  1.21    bouyer 		ata_delay(10, "sataup", flags);
    109   1.3    bouyer 	}
    110  1.15    bouyer 	/*
    111  1.15    bouyer 	 * if we have a link up without device, wait a few more seconds
    112  1.15    bouyer 	 * for connection to establish
    113  1.15    bouyer 	 */
    114  1.15    bouyer 	if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV_NE) {
    115  1.15    bouyer 		for (i = 0; i < 500; i++) {
    116  1.21    bouyer 			ata_delay(10, "sataup", flags);
    117  1.15    bouyer 			sstatus = bus_space_read_4(sata_t, sstatus_r, 0);
    118  1.15    bouyer 			if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
    119  1.15    bouyer 				break;
    120  1.15    bouyer 		}
    121  1.15    bouyer 	}
    122   1.3    bouyer 
    123   1.3    bouyer 	switch (sstatus & SStatus_DET_mask) {
    124   1.3    bouyer 	case SStatus_DET_NODEV:
    125   1.3    bouyer 		/* No Device; be silent.  */
    126   1.3    bouyer 		break;
    127   1.3    bouyer 
    128   1.3    bouyer 	case SStatus_DET_DEV_NE:
    129   1.5    bouyer 		aprint_error("%s port %d: device connected, but "
    130   1.3    bouyer 		    "communication not established\n",
    131  1.10      cube 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel);
    132   1.3    bouyer 		break;
    133   1.3    bouyer 
    134   1.3    bouyer 	case SStatus_DET_OFFLINE:
    135   1.5    bouyer 		aprint_error("%s port %d: PHY offline\n",
    136  1.10      cube 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel);
    137   1.3    bouyer 		break;
    138   1.3    bouyer 
    139   1.3    bouyer 	case SStatus_DET_DEV:
    140   1.5    bouyer 		aprint_normal("%s port %d: device present, speed: %s\n",
    141  1.10      cube 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
    142   1.3    bouyer 		    sata_speed(sstatus));
    143   1.3    bouyer 		break;
    144   1.3    bouyer 	default:
    145   1.5    bouyer 		aprint_error("%s port %d: unknown SStatus: 0x%08x\n",
    146  1.10      cube 		    device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
    147   1.3    bouyer 		    sstatus);
    148   1.3    bouyer 	}
    149   1.4    bouyer 	return(sstatus & SStatus_DET_mask);
    150   1.3    bouyer }
    151  1.20    bouyer 
    152  1.20    bouyer void
    153  1.20    bouyer sata_interpret_sig(struct ata_channel *chp, int port, uint32_t sig)
    154  1.20    bouyer {
    155  1.20    bouyer 	int err;
    156  1.20    bouyer 	int s;
    157  1.20    bouyer 
    158  1.20    bouyer 	/* some ATAPI devices have bogus lower two bytes, sigh */
    159  1.20    bouyer 	if ((sig & 0xffff0000) == 0xeb140000) {
    160  1.20    bouyer 		sig &= 0xffff0000;
    161  1.20    bouyer 		sig |= 0x00000101;
    162  1.20    bouyer 	}
    163  1.20    bouyer 	if (chp->ch_drive == NULL) {
    164  1.20    bouyer 		if (sig == 0x96690101)
    165  1.20    bouyer 			err = atabus_alloc_drives(chp, PMP_MAX_DRIVES);
    166  1.20    bouyer 		else
    167  1.20    bouyer 			err = atabus_alloc_drives(chp, 1);
    168  1.20    bouyer 		if (err)
    169  1.20    bouyer 			return;
    170  1.20    bouyer 	}
    171  1.20    bouyer 	KASSERT(port < chp->ch_ndrives);
    172  1.20    bouyer 
    173  1.20    bouyer 	s = splbio();
    174  1.20    bouyer 	switch(sig) {
    175  1.20    bouyer 	case 0x96690101:
    176  1.20    bouyer 		KASSERT(port == 0 || port == PMP_PORT_CTL);
    177  1.20    bouyer 		chp->ch_drive[PMP_PORT_CTL].drive_type = ATA_DRIVET_PM;
    178  1.20    bouyer 		break;
    179  1.20    bouyer 	case 0xc33c0101:
    180  1.20    bouyer 		aprint_verbose_dev(chp->atabus, "port %d is SEMB, ignored\n",
    181  1.20    bouyer 		    port);
    182  1.20    bouyer 		break;
    183  1.20    bouyer 	case 0xeb140101:
    184  1.20    bouyer 		chp->ch_drive[port].drive_type = ATA_DRIVET_ATAPI;
    185  1.20    bouyer 		break;
    186  1.20    bouyer 	case 0x00000101:
    187  1.20    bouyer 		chp->ch_drive[port].drive_type = ATA_DRIVET_ATA;
    188  1.20    bouyer 		break;
    189  1.20    bouyer 	case 0xffffffff:
    190  1.20    bouyer 		/* COMRESET time out */
    191  1.20    bouyer 		break;
    192  1.20    bouyer 	default:
    193  1.20    bouyer 		chp->ch_drive[port].drive_type = ATA_DRIVET_ATA;
    194  1.20    bouyer 		aprint_verbose_dev(chp->atabus,
    195  1.20    bouyer 		    "Unrecognized signature 0x%08x on port %d. "
    196  1.20    bouyer 		    "Assuming it's a disk.\n", sig, port);
    197  1.20    bouyer 		break;
    198  1.20    bouyer 	}
    199  1.20    bouyer 	splx(s);
    200  1.20    bouyer }
    201