Home | History | Annotate | Line # | Download | only in marvell
mvsata_mv.c revision 1.3.10.2
      1 /*	$NetBSD: mvsata_mv.c,v 1.3.10.2 2014/05/22 11:40:23 yamt Exp $	*/
      2 /*
      3  * Copyright (c) 2008 KIYOHARA Takashi
      4  * 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
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: mvsata_mv.c,v 1.3.10.2 2014/05/22 11:40:23 yamt Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/bus.h>
     33 #include <sys/device.h>
     34 #include <sys/errno.h>
     35 
     36 #include <dev/ata/atareg.h>
     37 #include <dev/ata/atavar.h>
     38 #include <dev/ic/wdcvar.h>
     39 
     40 #include <dev/ic/mvsatareg.h>
     41 #include <dev/ic/mvsatavar.h>
     42 
     43 #include <dev/marvell/marvellreg.h>
     44 #include <dev/marvell/marvellvar.h>
     45 
     46 #include "locators.h"
     47 
     48 
     49 #define MVSATAHC_SIZE			0x8000
     50 
     51 #define MVSATAHC_NWINDOW		4
     52 
     53 #define MVSATAHC_MICR			0x20 /* Main Interrupt Cause */
     54 #define MVSATAHC_MIMR			0x24 /* Main Interrupt Mask */
     55 #define MVSATAHC_MI_SATAERR(p)			(1 << ((p) * 2))
     56 #define MVSATAHC_MI_SATADONE(p)			(1 << (((p) * 2) + 1))
     57 #define MVSATAHC_MI_SATADMADONE(p)		(1 << ((p) + 4))
     58 #define MVSATAHC_MI_SATACOALDONE		(1 << 8)
     59 #define MVSATAHC_WCR(n)			(0x30 + (n) * 0x10) /* WinN Control */
     60 #define MVSATAHC_WCR_WINEN			(1 << 0)
     61 #define MVSATAHC_WCR_TARGET(t)			(((t) & 0xf) << 4)
     62 #define MVSATAHC_WCR_ATTR(a)			(((a) & 0xff) << 8)
     63 #define MVSATAHC_WCR_SIZE(s)			(((s) - 1) & 0xffff0000)
     64 #define MVSATAHC_WBR(n)			(0x34 + (n) * 0x10) /* WinN Base */
     65 #define MVSATAHC_WBR_BASE(b)			((b) & 0xffff0000)
     66 
     67 
     68 static int mvsatahc_match(device_t, cfdata_t, void *);
     69 static void mvsatahc_attach(device_t, device_t, void *);
     70 
     71 static int mvsatahc_intr(void *);
     72 
     73 static void mvsatahc_enable_intr(struct mvsata_port *, int);
     74 static void mvsatahc_wininit(struct mvsata_softc *, enum marvell_tags *);
     75 
     76 CFATTACH_DECL_NEW(mvsata_gt, sizeof(struct mvsata_softc),
     77     mvsatahc_match, mvsatahc_attach, NULL, NULL);
     78 CFATTACH_DECL_NEW(mvsata_mbus, sizeof(struct mvsata_softc),
     79     mvsatahc_match, mvsatahc_attach, NULL, NULL);
     80 
     81 
     82 struct mvsata_product mvsata_products[] = {
     83 #if 0
     84 	/* Discovery VI */
     85 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV64660, ?, ?, gen2?, 0 },
     86 #endif
     87 
     88 	/* Orion */
     89 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88F5082, 1, 1, gen2e, 0 },
     90 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88F5182, 1, 2, gen2e, 0 },
     91 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88F6082, 1, 1, gen2e, 0 },
     92 
     93 	/* Kirkwood */
     94 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88F6192, 1, 2, gen2e, 0 },
     95 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88F6281, 1, 2, gen2e, 0 },
     96 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88F6282, 1, 2, gen2e, 0 },
     97 
     98 	/* Discovery Innovation */
     99 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV78100, 1, 2, gen2e, 0 },
    100 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV78200, 1, 2, gen2e, 0 },
    101 
    102 	/* Armada XP */
    103 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV78130, 1, 2, gen2e, 0 },
    104 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV78160, 1, 2, gen2e, 0 },
    105 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV78230, 1, 2, gen2e, 0 },
    106 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV78260, 1, 2, gen2e, 0 },
    107 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV78460, 1, 2, gen2e, 0 },
    108 
    109 	/* Armada 370 */
    110 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV6707, 1, 2, gen2e, 0 },
    111 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV6710, 1, 2, gen2e, 0 },
    112 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_MV6W11, 1, 2, gen2e, 0 },
    113 };
    114 
    115 
    116 /* ARGSUSED */
    117 static int
    118 mvsatahc_match(device_t parent, cfdata_t match, void *aux)
    119 {
    120 	struct marvell_attach_args *mva = aux;
    121 	int i;
    122 
    123 	if (strcmp(mva->mva_name, match->cf_name) != 0)
    124 		return 0;
    125 	if (mva->mva_offset == MVA_OFFSET_DEFAULT ||
    126 	    mva->mva_irq == MVA_IRQ_DEFAULT)
    127 		    return 0;
    128 
    129 	for (i = 0; i < __arraycount(mvsata_products); i++)
    130 		if (mva->mva_model == mvsata_products[i].model) {
    131 			mva->mva_size = MVSATAHC_SIZE;
    132 			return 1;
    133 		}
    134 	return 0;
    135 }
    136 
    137 /* ARGSUSED */
    138 static void
    139 mvsatahc_attach(device_t parent, device_t self, void *aux)
    140 {
    141 	struct mvsata_softc *sc = device_private(self);
    142 	struct marvell_attach_args *mva = aux;
    143 	uint32_t mask;
    144 	int port, i;
    145 
    146 	aprint_normal(": Marvell Serial-ATA Host Controller (SATAHC)\n");
    147 	aprint_naive("\n");
    148 
    149 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    150 	sc->sc_model = mva->mva_model;
    151 	sc->sc_iot = mva->mva_iot;
    152         if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
    153 	    mva->mva_size, &sc->sc_ioh)) {
    154 		aprint_error_dev(self, "can't map registers\n");
    155 		return;
    156 	}
    157 	sc->sc_dmat = mva->mva_dmat;
    158 	sc->sc_enable_intr = mvsatahc_enable_intr;
    159 
    160 	mvsatahc_wininit(sc, mva->mva_tags);
    161 
    162 	for (i = 0; i < __arraycount(mvsata_products); i++)
    163 		if (mva->mva_model == mvsata_products[i].model)
    164 			break;
    165 	KASSERT(i < __arraycount(mvsata_products));
    166 
    167 	if (mvsata_attach(sc, &mvsata_products[i], NULL, NULL, 0) != 0)
    168 		return;
    169 
    170 	marvell_intr_establish(mva->mva_irq, IPL_BIO, mvsatahc_intr, sc);
    171 	mask = 0;
    172 	for (port = 0; port < sc->sc_port; port++)
    173 		mask |=
    174 		    MVSATAHC_MI_SATAERR(port) |
    175 		    MVSATAHC_MI_SATADONE(port);
    176 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSATAHC_MIMR, mask);
    177 }
    178 
    179 static int
    180 mvsatahc_intr(void *arg)
    181 {
    182 	struct mvsata_softc *sc = (struct mvsata_softc *)arg;
    183 	struct mvsata_hc *mvhc = &sc->sc_hcs[0];
    184 	uint32_t cause, handled = 0;
    185 
    186 	cause = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSATAHC_MICR);
    187 	if (cause & MVSATAHC_MI_SATAERR(0))
    188 		handled |= mvsata_error(mvhc->hc_ports[0]);
    189 	if (cause & MVSATAHC_MI_SATAERR(1))
    190 		handled |= mvsata_error(mvhc->hc_ports[1]);
    191 	if (cause & (MVSATAHC_MI_SATADONE(0) | MVSATAHC_MI_SATADONE(1)))
    192 		handled |= mvsata_intr(mvhc);
    193 
    194 	return handled;
    195 }
    196 
    197 
    198 static void
    199 mvsatahc_enable_intr(struct mvsata_port *mvport, int on)
    200 {
    201 	struct mvsata_softc *sc =
    202 	    device_private(mvport->port_ata_channel.ch_atac->atac_dev);
    203 	uint32_t mask;
    204 
    205 	mask = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MVSATAHC_MIMR);
    206 	if (on)
    207 		mask |= MVSATAHC_MI_SATADONE(mvport->port);
    208 	else
    209 		mask &= ~MVSATAHC_MI_SATADONE(mvport->port);
    210 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, MVSATAHC_MIMR, mask);
    211 }
    212 
    213 static void
    214 mvsatahc_wininit(struct mvsata_softc *sc, enum marvell_tags *tags)
    215 {
    216 	device_t pdev = device_parent(sc->sc_wdcdev.sc_atac.atac_dev);
    217 	uint64_t base;
    218 	uint32_t size;
    219 	int window, target, attr, rv, i;
    220 
    221 	for (window = 0, i = 0;
    222 	    tags[i] != MARVELL_TAG_UNDEFINED && window < MVSATAHC_NWINDOW;
    223 	    i++) {
    224 		rv = marvell_winparams_by_tag(pdev, tags[i],
    225 		    &target, &attr, &base, &size);
    226 		if (rv != 0 || size == 0)
    227 			continue;
    228 		if (base > 0xffffffffULL) {
    229 			aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
    230 			    "tag %d address 0x%llx not support\n",
    231 			    tags[i], base);
    232 			continue;
    233 		}
    234 
    235 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    236 		    MVSATAHC_WCR(window),
    237 		    MVSATAHC_WCR_WINEN |
    238 		    MVSATAHC_WCR_TARGET(target) |
    239 		    MVSATAHC_WCR_ATTR(attr) |
    240 		    MVSATAHC_WCR_SIZE(size));
    241 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    242 		    MVSATAHC_WBR(window), MVSATAHC_WBR_BASE(base));
    243 		window++;
    244 	}
    245 	for (; window < MVSATAHC_NWINDOW; window++)
    246 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    247 		    MVSATAHC_WCR(window), 0);
    248 }
    249