Home | History | Annotate | Line # | Download | only in ata
satapmp_subr.c revision 1.14.2.1
      1 /*	$NetBSD: satapmp_subr.c,v 1.14.2.1 2018/09/22 09:22:59 jdolecek Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2012 Manuel Bouyer.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: satapmp_subr.c,v 1.14.2.1 2018/09/22 09:22:59 jdolecek Exp $");
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/kernel.h>
     33 #include <sys/device.h>
     34 #include <sys/conf.h>
     35 #include <sys/fcntl.h>
     36 #include <sys/proc.h>
     37 #include <sys/errno.h>
     38 #include <sys/kmem.h>
     39 #include <sys/intr.h>
     40 
     41 #include <dev/ata/ataconf.h>
     42 #include <dev/ata/atareg.h>
     43 #include <dev/ata/atavar.h>
     44 
     45 #include <dev/ata/satapmpvar.h>
     46 #include <dev/ata/satapmpreg.h>
     47 #include <dev/ata/satavar.h>
     48 #include <dev/ata/satareg.h>
     49 
     50 static int
     51 satapmp_read_8(struct ata_channel *chp, int port, int reg, uint64_t *value,
     52     struct ata_xfer *xfer)
     53 {
     54 	struct atac_softc *atac = chp->ch_atac;
     55 	struct ata_drive_datas *drvp;
     56 	int error = 0;
     57 
     58 	KASSERT(port < PMP_MAX_DRIVES);
     59 	KASSERT(reg < PMP_GSCR_NREGS);
     60 	KASSERT(chp->ch_ndrives >= PMP_MAX_DRIVES);
     61 	drvp = &chp->ch_drive[PMP_PORT_CTL];
     62 	KASSERT(drvp->drive == PMP_PORT_CTL);
     63 	ata_channel_lock_owned(chp);
     64 
     65 	xfer->c_ata_c.r_command = PMPC_READ_PORT;
     66 	xfer->c_ata_c.r_features = reg;
     67 	xfer->c_ata_c.r_device = port;
     68 	xfer->c_ata_c.timeout = 3000; /* 3s */
     69 	xfer->c_ata_c.r_st_bmask = 0;
     70 	xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
     71 	xfer->c_ata_c.flags = AT_LBA48 | AT_READREG | AT_WAIT;
     72 
     73 	ata_channel_unlock(chp);
     74 	if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
     75 	    xfer) != ATACMD_COMPLETE) {
     76 		aprint_error_dev(chp->atabus,
     77 		    "PMP port %d register %d read failed\n", port, reg);
     78 		error = EIO;
     79 		goto out;
     80 	}
     81 	if (xfer->c_ata_c.flags & (AT_TIMEOU | AT_DF)) {
     82 		aprint_error_dev(chp->atabus,
     83 		    "PMP port %d register %d read failed, flags 0x%x\n",
     84 		    port, reg, xfer->c_ata_c.flags);
     85 		error = EIO;
     86 		goto out;
     87 	}
     88 	if (xfer->c_ata_c.flags & AT_ERROR) {
     89 		aprint_verbose_dev(chp->atabus,
     90 		    "PMP port %d register %d read failed, error 0x%x\n",
     91 		    port, reg, xfer->c_ata_c.r_error);
     92 		error = EIO;
     93 		goto out;
     94 	}
     95 
     96 	*value = ((uint64_t)((xfer->c_ata_c.r_lba >> 24) & 0xffffff) << 40) |
     97 		((uint64_t)((xfer->c_ata_c.r_count >> 8) & 0xff) << 32) |
     98 		((uint64_t)((xfer->c_ata_c.r_lba >> 0) & 0xffffff) << 8) |
     99 		((uint64_t)((xfer->c_ata_c.r_count >> 0) & 0xff) << 0);
    100 
    101 out:
    102 	ata_channel_lock(chp);
    103 	return error;
    104 }
    105 
    106 static inline int
    107 satapmp_read(struct ata_channel *chp, int port, int reg, uint32_t *value,
    108     struct ata_xfer *xfer)
    109 {
    110 	uint64_t value64;
    111 	int ret;
    112 
    113 	ret = satapmp_read_8(chp, port, reg, &value64, xfer);
    114 	if (ret)
    115 		return ret;
    116 
    117 	*value = value64 & 0xffffffff;
    118 	return ret;
    119 }
    120 
    121 static int
    122 satapmp_write_8(struct ata_channel *chp, int port, int reg, uint64_t value,
    123     struct ata_xfer *xfer)
    124 {
    125 	struct atac_softc *atac = chp->ch_atac;
    126 	struct ata_drive_datas *drvp;
    127 	int error = 0;
    128 
    129 	KASSERT(port < PMP_MAX_DRIVES);
    130 	KASSERT(reg < PMP_GSCR_NREGS);
    131 	KASSERT(chp->ch_ndrives >= PMP_MAX_DRIVES);
    132 	drvp = &chp->ch_drive[PMP_PORT_CTL];
    133 	KASSERT(drvp->drive == PMP_PORT_CTL);
    134 	ata_channel_lock_owned(chp);
    135 
    136 	xfer->c_ata_c.r_command = PMPC_WRITE_PORT;
    137 	xfer->c_ata_c.r_features = reg;
    138 	xfer->c_ata_c.r_device = port;
    139 	xfer->c_ata_c.r_lba = (((value >> 40) & 0xffffff) << 24) |
    140 		      (((value >>  8) & 0xffffff) <<  0);
    141 	xfer->c_ata_c.r_count = (((value >> 32) & 0xff) << 8) |
    142 			(((value >>  0) & 0xff) << 0);
    143 	xfer->c_ata_c.timeout = 3000; /* 3s */
    144 	xfer->c_ata_c.r_st_bmask = 0;
    145 	xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
    146 	xfer->c_ata_c.flags = AT_LBA48 | AT_WAIT;
    147 
    148 	ata_channel_unlock(chp);
    149 	if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
    150 	    xfer) != ATACMD_COMPLETE) {
    151 		aprint_error_dev(chp->atabus,
    152 		    "PMP port %d register %d write failed\n", port, reg);
    153 		error = EIO;
    154 		goto out;
    155 	}
    156 	if (xfer->c_ata_c.flags & (AT_TIMEOU | AT_DF)) {
    157 		aprint_error_dev(chp->atabus,
    158 		    "PMP port %d register %d write failed, flags 0x%x\n",
    159 		    port, reg, xfer->c_ata_c.flags);
    160 		error = EIO;
    161 		goto out;
    162 	}
    163 	if (xfer->c_ata_c.flags & AT_ERROR) {
    164 		aprint_verbose_dev(chp->atabus,
    165 		    "PMP port %d register %d write failed, error 0x%x\n",
    166 		    port, reg, xfer->c_ata_c.r_error);
    167 		error = EIO;
    168 		goto out;
    169 	}
    170 
    171 out:
    172 	ata_channel_lock(chp);
    173 	return error;
    174 }
    175 
    176 static inline int
    177 satapmp_write(struct ata_channel *chp, int port, int reg, uint32_t value,
    178     struct ata_xfer *xfer)
    179 {
    180 	return satapmp_write_8(chp, port, reg, value, xfer);
    181 }
    182 
    183 /*
    184  * Reset one port's PHY and bring it online
    185  * XXX duplicate of sata_reset_interface()
    186  */
    187 static uint32_t
    188 satapmp_reset_device_port(struct ata_channel *chp, int port,
    189     struct ata_xfer *xfer)
    190 {
    191 	uint32_t scontrol, sstatus;
    192 	int i;
    193 
    194 	ata_channel_lock_owned(chp);
    195 
    196 	/* bring the PHY online */
    197 	scontrol = SControl_IPM_NONE | SControl_SPD_ANY | SControl_DET_INIT;
    198 	if (satapmp_write(chp, port, PMP_PSCR_SControl, scontrol, xfer) != 0)
    199 		return 0;
    200 
    201 	ata_delay(chp, 50, "sataup", AT_WAIT);
    202 	scontrol &= ~SControl_DET_INIT;
    203 	if (satapmp_write(chp, port, PMP_PSCR_SControl, scontrol, xfer) != 0)
    204 		return 0;
    205 	ata_delay(chp, 50, "sataup", AT_WAIT);
    206 
    207 	/* wait up to 1s for device to come up */
    208 	for (i = 0; i < 100; i++) {
    209 
    210 		if (satapmp_read(chp, port, PMP_PSCR_SStatus, &sstatus,
    211 		    xfer) != 0)
    212 			return 0;
    213 		if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
    214 			break;
    215 		ata_delay(chp, 10, "sataup", AT_WAIT);
    216 	}
    217 
    218 	switch (sstatus & SStatus_DET_mask) {
    219 	case SStatus_DET_NODEV:
    220 		/* No Device; be silent.  */
    221 		break;
    222 	case SStatus_DET_DEV_NE:
    223 		aprint_error("%s PMP port %d: device connected, but "
    224 		    "communication not established\n",
    225 		    device_xname(chp->atabus), port);
    226 		break;
    227 	case SStatus_DET_OFFLINE:
    228 		aprint_error("%s PMP port %d: PHY offline\n",
    229 		    device_xname(chp->atabus), port);
    230 		break;
    231 	case SStatus_DET_DEV:
    232 		aprint_normal("%s PMP port %d: device present, speed: %s\n",
    233 		    device_xname(chp->atabus), port, sata_speed(sstatus));
    234 		break;
    235 	default:
    236 		aprint_error("%s PMP port %d: unknown SStatus: 0x%08x\n",
    237 		    device_xname(chp->atabus), port, sstatus);
    238 	}
    239 	return(sstatus & SStatus_DET_mask);
    240 }
    241 
    242 static void __noinline
    243 satapmp_rescan(struct ata_channel *chp, struct ata_xfer *xfer)
    244 {
    245 	int i;
    246 	uint32_t sig;
    247 
    248 	KASSERT(chp->ch_satapmp_nports <= PMP_PORT_CTL);
    249 	KASSERT(chp->ch_satapmp_nports <= chp->ch_ndrives);
    250 	ata_channel_lock_owned(chp);
    251 
    252 	for (i = 0; i < chp->ch_satapmp_nports; i++) {
    253 		if (chp->ch_drive[i].drive_type != ATA_DRIVET_NONE ||
    254 		    satapmp_reset_device_port(chp, i, xfer)
    255 		    != SStatus_DET_DEV) {
    256 			continue;
    257 		}
    258 		if (satapmp_write(chp, i, PMP_PSCR_SError, 0xffffffff, xfer)
    259 		    != 0) {
    260 			aprint_error("%s PMP port %d: can't write SError\n",
    261 			    device_xname(chp->atabus), i);
    262 			continue;
    263 		}
    264 		ata_channel_unlock(chp);
    265 		chp->ch_atac->atac_bustype_ata->ata_reset_drive(
    266 		    &chp->ch_drive[i], AT_WAIT, &sig);
    267 		ata_channel_lock(chp);
    268 
    269 		sata_interpret_sig(chp, i, sig);
    270 	}
    271 }
    272 
    273 void
    274 satapmp_attach(struct ata_channel *chp)
    275 {
    276 	uint32_t id, rev, inf;
    277 	struct ata_xfer *xfer;
    278 
    279 	xfer = ata_get_xfer(chp, false);
    280 	if (xfer == NULL) {
    281 		aprint_normal_dev(chp->atabus, "no available xfer\n");
    282 		return;
    283 	}
    284 
    285 	ata_channel_lock(chp);
    286 
    287 	if (satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_ID, &id, xfer) != 0 ||
    288 	    satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_REV, &rev, xfer) != 0 ||
    289 	    satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_INF, &inf, xfer) != 0) {
    290 		aprint_normal_dev(chp->atabus, "can't read PMP registers\n");
    291 		goto out;
    292 	}
    293 
    294 	aprint_normal("%s at %s channel %d: SATA port multiplier, %d ports\n",
    295 	    device_xname(chp->atabus),
    296 	    device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
    297 	    PMP_INF_NPORTS(inf));
    298 	aprint_verbose_dev(chp->atabus,
    299 	    "vendor 0x%04x, product 0x%04x",
    300 	    PMP_ID_VEND(id), PMP_ID_DEV(id));
    301 	if (rev & PMP_REV_SPEC_11) {
    302 		aprint_verbose(", revision 1.1");
    303 	} else if (rev & PMP_REV_SPEC_10) {
    304 		aprint_verbose(", revision 1.0");
    305 	} else {
    306 		aprint_verbose(", unknown revision 0x%x", rev & 0x0f);
    307 	}
    308 	aprint_verbose(", level %d\n", PMP_REV_LEVEL(rev));
    309 
    310 	chp->ch_satapmp_nports = PMP_INF_NPORTS(inf);
    311 
    312 	/* reset and bring up PHYs */
    313 	satapmp_rescan(chp, xfer);
    314 
    315 out:
    316 	ata_channel_unlock(chp);
    317 	ata_free_xfer(chp, xfer);
    318 }
    319