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