Home | History | Annotate | Line # | Download | only in ata
satapmp_subr.c revision 1.12.24.2
      1 /*	$NetBSD: satapmp_subr.c,v 1.12.24.2 2017/06/17 14:01:36 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.12.24.2 2017/06/17 14:01:36 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 {
     53 	struct ata_xfer xfer;
     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 
     64 	ata_xfer_init(&xfer, true);
     65 
     66 	xfer.c_ata_c.r_command = PMPC_READ_PORT;
     67 	xfer.c_ata_c.r_features = reg;
     68 	xfer.c_ata_c.r_device = port;
     69 	xfer.c_ata_c.timeout = 3000; /* 3s */
     70 	xfer.c_ata_c.r_st_bmask = 0;
     71 	xfer.c_ata_c.r_st_pmask = WDCS_DRDY;
     72 	xfer.c_ata_c.flags = AT_LBA48 | AT_READREG | AT_WAIT;
     73 
     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_xfer_destroy(&xfer);
    103 	return error;
    104 }
    105 
    106 static inline int
    107 satapmp_read(struct ata_channel *chp, int port, int reg, uint32_t *value)
    108 {
    109 	uint64_t value64;
    110 	int ret;
    111 
    112 	ret = satapmp_read_8(chp, port, reg, &value64);
    113 	if (ret)
    114 		return ret;
    115 
    116 	*value = value64 & 0xffffffff;
    117 	return ret;
    118 }
    119 
    120 static int
    121 satapmp_write_8(struct ata_channel *chp, int port, int reg, uint64_t value)
    122 {
    123 	struct ata_xfer xfer;
    124 	struct atac_softc *atac = chp->ch_atac;
    125 	struct ata_drive_datas *drvp;
    126 	int error = 0;
    127 
    128 	KASSERT(port < PMP_MAX_DRIVES);
    129 	KASSERT(reg < PMP_GSCR_NREGS);
    130 	KASSERT(chp->ch_ndrives >= PMP_MAX_DRIVES);
    131 	drvp = &chp->ch_drive[PMP_PORT_CTL];
    132 	KASSERT(drvp->drive == PMP_PORT_CTL);
    133 
    134 	ata_xfer_init(&xfer, true);
    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 	if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
    149 	    &xfer) != ATACMD_COMPLETE) {
    150 		aprint_error_dev(chp->atabus,
    151 		    "PMP port %d register %d write failed\n", port, reg);
    152 		error = EIO;
    153 		goto out;
    154 	}
    155 	if (xfer.c_ata_c.flags & (AT_TIMEOU | AT_DF)) {
    156 		aprint_error_dev(chp->atabus,
    157 		    "PMP port %d register %d write failed, flags 0x%x\n",
    158 		    port, reg, xfer.c_ata_c.flags);
    159 		error = EIO;
    160 		goto out;
    161 	}
    162 	if (xfer.c_ata_c.flags & AT_ERROR) {
    163 		aprint_verbose_dev(chp->atabus,
    164 		    "PMP port %d register %d write failed, error 0x%x\n",
    165 		    port, reg, xfer.c_ata_c.r_error);
    166 		error = EIO;
    167 		goto out;
    168 	}
    169 
    170 out:
    171 	ata_xfer_destroy(&xfer);
    172 	return error;
    173 }
    174 
    175 static inline int
    176 satapmp_write(struct ata_channel *chp, int port, int reg, uint32_t value)
    177 {
    178 	return satapmp_write_8(chp, port, reg, value);
    179 }
    180 
    181 /*
    182  * Reset one port's PHY and bring it online
    183  * XXX duplicate of sata_reset_interface()
    184  */
    185 static uint32_t
    186 satapmp_reset_device_port(struct ata_channel *chp, int port)
    187 {
    188 	uint32_t scontrol, sstatus;
    189 	int i;
    190 
    191 	/* bring the PHY online */
    192 	scontrol = SControl_IPM_NONE | SControl_SPD_ANY | SControl_DET_INIT;
    193 	if (satapmp_write(chp, port, PMP_PSCR_SControl, scontrol) != 0)
    194 		return 0;
    195 
    196 	tsleep(chp, PRIBIO, "sataup", mstohz(50));
    197 	scontrol &= ~SControl_DET_INIT;
    198 	if (satapmp_write(chp, port, PMP_PSCR_SControl, scontrol) != 0)
    199 		return 0;
    200 	tsleep(chp, PRIBIO, "sataup", mstohz(50));
    201 
    202 	/* wait up to 1s for device to come up */
    203 	for (i = 0; i < 100; i++) {
    204 
    205 		if (satapmp_read(chp, port, PMP_PSCR_SStatus, &sstatus) != 0)
    206 			return 0;
    207 		if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
    208 			break;
    209 		tsleep(chp, PRIBIO, "sataup", mstohz(10));
    210 	}
    211 
    212 	switch (sstatus & SStatus_DET_mask) {
    213 	case SStatus_DET_NODEV:
    214 		/* No Device; be silent.  */
    215 		break;
    216 	case SStatus_DET_DEV_NE:
    217 		aprint_error("%s PMP port %d: device connected, but "
    218 		    "communication not established\n",
    219 		    device_xname(chp->atabus), port);
    220 		break;
    221 	case SStatus_DET_OFFLINE:
    222 		aprint_error("%s PMP port %d: PHY offline\n",
    223 		    device_xname(chp->atabus), port);
    224 		break;
    225 	case SStatus_DET_DEV:
    226 		aprint_normal("%s PMP port %d: device present, speed: %s\n",
    227 		    device_xname(chp->atabus), port, sata_speed(sstatus));
    228 		break;
    229 	default:
    230 		aprint_error("%s PMP port %d: unknown SStatus: 0x%08x\n",
    231 		    device_xname(chp->atabus), port, sstatus);
    232 	}
    233 	return(sstatus & SStatus_DET_mask);
    234 }
    235 
    236 void
    237 satapmp_rescan(struct ata_channel *chp) {
    238 	int i;
    239 	uint32_t sig;
    240 
    241 	KASSERT(chp->ch_satapmp_nports <= PMP_PORT_CTL);
    242 	KASSERT(chp->ch_satapmp_nports <= chp->ch_ndrives);
    243 
    244 	for (i = 0; i < chp->ch_satapmp_nports; i++) {
    245 		if (chp->ch_drive[i].drive_type != ATA_DRIVET_NONE ||
    246 		    satapmp_reset_device_port(chp, i) != SStatus_DET_DEV) {
    247 			continue;
    248 		}
    249 		if (satapmp_write(chp, i, PMP_PSCR_SError, 0xffffffff) != 0) {
    250 			aprint_error("%s PMP port %d: can't write SError\n",
    251 			    device_xname(chp->atabus), i);
    252 			continue;
    253 		}
    254 		chp->ch_atac->atac_bustype_ata->ata_reset_drive(
    255 		    &chp->ch_drive[i], AT_WAIT, &sig);
    256 
    257 		sata_interpret_sig(chp, i, sig);
    258 	}
    259 }
    260 
    261 void
    262 satapmp_attach(struct ata_channel *chp)
    263 {
    264 	uint32_t id, rev, inf;
    265 
    266 	if (satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_ID, &id) != 0 ||
    267 	    satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_REV, &rev) != 0 ||
    268 	    satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_INF, &inf) != 0) {
    269 		aprint_normal_dev(chp->atabus, "can't read PMP registers\n");
    270 		return;
    271 	}
    272 
    273 	aprint_normal_dev(chp->atabus,
    274 	    "SATA port multiplier, %d ports\n", PMP_INF_NPORTS(inf));
    275 	aprint_verbose_dev(chp->atabus,
    276 	    "vendor 0x%04x, product 0x%04x",
    277 	    PMP_ID_VEND(id), PMP_ID_DEV(id));
    278 	if (rev & PMP_REV_SPEC_11) {
    279 		aprint_verbose(", revision 1.1");
    280 	} else if (rev & PMP_REV_SPEC_10) {
    281 		aprint_verbose(", revision 1.0");
    282 	} else {
    283 		aprint_verbose(", unknown revision 0x%x", rev & 0x0f);
    284 	}
    285 	aprint_verbose(", level %d\n", PMP_REV_LEVEL(rev));
    286 
    287 	chp->ch_satapmp_nports = PMP_INF_NPORTS(inf);
    288 
    289 	/* reset and bring up PHYs */
    290 	satapmp_rescan(chp);
    291 }
    292