Home | History | Annotate | Line # | Download | only in nvidia
tegra_ahcisata.c revision 1.8
      1  1.8  jmcneill /* $NetBSD: tegra_ahcisata.c,v 1.8 2015/12/13 17:39:19 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.8  jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_ahcisata.c,v 1.8 2015/12/13 17:39:19 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/intr.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/kernel.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <dev/ata/atavar.h>
     40  1.1  jmcneill #include <dev/ic/ahcisatavar.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include <arm/nvidia/tegra_var.h>
     43  1.3  jmcneill #include <arm/nvidia/tegra_ahcisatareg.h>
     44  1.3  jmcneill 
     45  1.8  jmcneill #include <dev/fdt/fdtvar.h>
     46  1.8  jmcneill 
     47  1.3  jmcneill #define TEGRA_AHCISATA_OFFSET	0x7000
     48  1.1  jmcneill 
     49  1.1  jmcneill static int	tegra_ahcisata_match(device_t, cfdata_t, void *);
     50  1.1  jmcneill static void	tegra_ahcisata_attach(device_t, device_t, void *);
     51  1.1  jmcneill 
     52  1.1  jmcneill struct tegra_ahcisata_softc {
     53  1.1  jmcneill 	struct ahci_softc	sc;
     54  1.3  jmcneill 	bus_space_tag_t		sc_bst;
     55  1.3  jmcneill 	bus_space_handle_t	sc_bsh;
     56  1.1  jmcneill 	void			*sc_ih;
     57  1.4  jmcneill 
     58  1.4  jmcneill 	struct tegra_gpio_pin	*sc_pin_power;
     59  1.1  jmcneill };
     60  1.1  jmcneill 
     61  1.8  jmcneill static const char * const tegra_ahcisata_supplies[] = {
     62  1.8  jmcneill     "hvdd-supply",
     63  1.8  jmcneill     "vddio-supply",
     64  1.8  jmcneill     "avdd-supply",
     65  1.8  jmcneill     "target-5v-supply",
     66  1.8  jmcneill     "target-12v-supply"
     67  1.8  jmcneill };
     68  1.8  jmcneill 
     69  1.3  jmcneill static void	tegra_ahcisata_init(struct tegra_ahcisata_softc *);
     70  1.3  jmcneill 
     71  1.1  jmcneill CFATTACH_DECL_NEW(tegra_ahcisata, sizeof(struct tegra_ahcisata_softc),
     72  1.1  jmcneill 	tegra_ahcisata_match, tegra_ahcisata_attach, NULL, NULL);
     73  1.1  jmcneill 
     74  1.1  jmcneill static int
     75  1.1  jmcneill tegra_ahcisata_match(device_t parent, cfdata_t cf, void *aux)
     76  1.1  jmcneill {
     77  1.8  jmcneill 	const char * const compatible[] = { "nvidia,tegra124-ahci", NULL };
     78  1.8  jmcneill 	struct fdt_attach_args * const faa = aux;
     79  1.8  jmcneill 
     80  1.8  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
     81  1.1  jmcneill }
     82  1.1  jmcneill 
     83  1.1  jmcneill static void
     84  1.1  jmcneill tegra_ahcisata_attach(device_t parent, device_t self, void *aux)
     85  1.1  jmcneill {
     86  1.1  jmcneill 	struct tegra_ahcisata_softc * const sc = device_private(self);
     87  1.8  jmcneill 	struct fdt_attach_args * const faa = aux;
     88  1.8  jmcneill 	const int phandle = faa->faa_phandle;
     89  1.8  jmcneill 	bus_addr_t ahci_addr, sata_addr;
     90  1.8  jmcneill 	bus_size_t ahci_size, sata_size;
     91  1.8  jmcneill 	struct fdtbus_regulator *reg;
     92  1.8  jmcneill 	char intrstr[128];
     93  1.8  jmcneill 	int error, n;
     94  1.8  jmcneill 
     95  1.8  jmcneill 	if (fdtbus_get_reg(phandle, 0, &ahci_addr, &ahci_size) != 0) {
     96  1.8  jmcneill 		aprint_error(": couldn't get ahci registers\n");
     97  1.8  jmcneill 		return;
     98  1.8  jmcneill 	}
     99  1.8  jmcneill 	if (fdtbus_get_reg(phandle, 1, &sata_addr, &sata_size) != 0) {
    100  1.8  jmcneill 		aprint_error(": couldn't get sata registers\n");
    101  1.8  jmcneill 		return;
    102  1.8  jmcneill 	}
    103  1.8  jmcneill 
    104  1.8  jmcneill 	sc->sc_bst = faa->faa_bst;
    105  1.8  jmcneill 	error = bus_space_map(sc->sc_bst, sata_addr, sata_size, 0, &sc->sc_bsh);
    106  1.8  jmcneill 	if (error) {
    107  1.8  jmcneill 		aprint_error(": couldn't map sata registers: %d\n", error);
    108  1.8  jmcneill 		return;
    109  1.8  jmcneill 	}
    110  1.3  jmcneill 
    111  1.1  jmcneill 	sc->sc.sc_atac.atac_dev = self;
    112  1.8  jmcneill 	sc->sc.sc_dmat = faa->faa_dmat;
    113  1.8  jmcneill 	sc->sc.sc_ahcit = faa->faa_bst;
    114  1.8  jmcneill 	sc->sc.sc_ahcis = ahci_size;
    115  1.8  jmcneill 	error = bus_space_map(sc->sc.sc_ahcit, ahci_addr, ahci_size, 0,
    116  1.8  jmcneill 	    &sc->sc.sc_ahcih);
    117  1.8  jmcneill 	if (error) {
    118  1.8  jmcneill 		aprint_error(": couldn't map ahci registers: %d\n", error);
    119  1.8  jmcneill 		return;
    120  1.8  jmcneill 	}
    121  1.6  jmcneill 	sc->sc.sc_ahci_quirks = AHCI_QUIRK_SKIP_RESET;
    122  1.1  jmcneill 
    123  1.1  jmcneill 	aprint_naive("\n");
    124  1.1  jmcneill 	aprint_normal(": SATA\n");
    125  1.1  jmcneill 
    126  1.8  jmcneill 	for (n = 0; n < __arraycount(tegra_ahcisata_supplies); n++) {
    127  1.8  jmcneill 		const char *supply = tegra_ahcisata_supplies[n];
    128  1.8  jmcneill 		reg = fdtbus_regulator_acquire(phandle, supply);
    129  1.8  jmcneill 		if (reg == NULL) {
    130  1.8  jmcneill 			aprint_error_dev(self, "couldn't acquire %s\n", supply);
    131  1.8  jmcneill 			continue;
    132  1.8  jmcneill 		}
    133  1.8  jmcneill 		if (fdtbus_regulator_enable(reg) != 0) {
    134  1.8  jmcneill 			aprint_error_dev(self, "couldn't enable %s\n", supply);
    135  1.8  jmcneill 		}
    136  1.8  jmcneill 		fdtbus_regulator_release(reg);
    137  1.4  jmcneill 	}
    138  1.4  jmcneill 
    139  1.3  jmcneill 	tegra_car_periph_sata_enable();
    140  1.3  jmcneill 
    141  1.5  jmcneill 	tegra_xusbpad_sata_enable();
    142  1.5  jmcneill 
    143  1.3  jmcneill 	tegra_ahcisata_init(sc);
    144  1.3  jmcneill 
    145  1.8  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    146  1.8  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    147  1.8  jmcneill 		return;
    148  1.8  jmcneill 	}
    149  1.8  jmcneill 
    150  1.8  jmcneill 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_BIO, 0,
    151  1.1  jmcneill 	    ahci_intr, &sc->sc);
    152  1.1  jmcneill 	if (sc->sc_ih == NULL) {
    153  1.8  jmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    154  1.8  jmcneill 		    intrstr);
    155  1.1  jmcneill 		return;
    156  1.1  jmcneill 	}
    157  1.8  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    158  1.1  jmcneill 
    159  1.1  jmcneill 	ahci_attach(&sc->sc);
    160  1.1  jmcneill }
    161  1.3  jmcneill 
    162  1.3  jmcneill static void
    163  1.3  jmcneill tegra_ahcisata_init(struct tegra_ahcisata_softc *sc)
    164  1.3  jmcneill {
    165  1.3  jmcneill 	bus_space_tag_t bst = sc->sc_bst;
    166  1.3  jmcneill 	bus_space_handle_t bsh = sc->sc_bsh;
    167  1.3  jmcneill 
    168  1.5  jmcneill 	const u_int gen1_tx_amp = 0x18;
    169  1.5  jmcneill 	const u_int gen1_tx_peak = 0x04;
    170  1.5  jmcneill 	const u_int gen2_tx_amp = 0x18;
    171  1.5  jmcneill 	const u_int gen2_tx_peak = 0x0a;
    172  1.5  jmcneill 
    173  1.7  jmcneill 	/* Set RX idle detection source and disable RX idle detection interrupt */
    174  1.7  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
    175  1.7  jmcneill 	    TEGRA_SATA_AUX_MISC_CNTL_1_AUX_OR_CORE_IDLE_STATUS_SEL, 0);
    176  1.7  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_RX_STAT_INT_REG,
    177  1.7  jmcneill 	    TEGRA_SATA_AUX_RX_STAT_INT_SATA_RX_STAT_INT_DISABLE, 0);
    178  1.7  jmcneill 
    179  1.7  jmcneill 	/* Prevent automatic OOB sequence when coming out of reset */
    180  1.7  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
    181  1.7  jmcneill 	    0, TEGRA_SATA_AUX_MISC_CNTL_1_OOB_ON_POR);
    182  1.7  jmcneill 
    183  1.7  jmcneill 	/* Disable device sleep */
    184  1.7  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_SATA_AUX_MISC_CNTL_1_REG,
    185  1.7  jmcneill 	    0, TEGRA_SATA_AUX_MISC_CNTL_1_SDS_SUPPORT);
    186  1.7  jmcneill 
    187  1.3  jmcneill 	/* Enable IFPS device block */
    188  1.3  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_SATA_CONFIGURATION_REG,
    189  1.3  jmcneill 	    TEGRA_SATA_CONFIGURATION_EN_FPCI, 0);
    190  1.3  jmcneill 
    191  1.5  jmcneill 	/* PHY config */
    192  1.5  jmcneill 	bus_space_write_4(bst, bsh, TEGRA_T_SATA0_INDEX_REG,
    193  1.5  jmcneill 	    TEGRA_T_SATA0_INDEX_CH1);
    194  1.5  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_REG,
    195  1.5  jmcneill 	    __SHIFTIN(gen1_tx_amp, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP) |
    196  1.5  jmcneill 	    __SHIFTIN(gen1_tx_peak, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK),
    197  1.5  jmcneill 	    TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP |
    198  1.5  jmcneill 	    TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK);
    199  1.5  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_REG,
    200  1.5  jmcneill 	    __SHIFTIN(gen2_tx_amp, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP) |
    201  1.5  jmcneill 	    __SHIFTIN(gen2_tx_peak, TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK),
    202  1.5  jmcneill 	    TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP |
    203  1.5  jmcneill 	    TEGRA_T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK);
    204  1.5  jmcneill 	bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL11_REG,
    205  1.5  jmcneill 	    __SHIFTIN(0x2800, TEGRA_T_SATA0_CHX_PHY_CTRL11_GEN2_RX_EQ));
    206  1.5  jmcneill 	bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CHX_PHY_CTRL2_REG,
    207  1.5  jmcneill 	    __SHIFTIN(0x23, TEGRA_T_SATA0_CHX_PHY_CTRL2_CDR_CNTL_GEN1));
    208  1.5  jmcneill 	bus_space_write_4(bst, bsh, TEGRA_T_SATA0_INDEX_REG, 0);
    209  1.5  jmcneill 
    210  1.3  jmcneill 	/* Backdoor update the programming interface field and class code */
    211  1.3  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_SATA_REG,
    212  1.3  jmcneill 	    TEGRA_T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN, 0);
    213  1.3  jmcneill 	bus_space_write_4(bst, bsh, TEGRA_T_SATA0_BKDOOR_CC_REG,
    214  1.3  jmcneill 	    __SHIFTIN(0x1016, TEGRA_T_SATA0_BKDOOR_CC_CLASS_CODE) |
    215  1.3  jmcneill 	    __SHIFTIN(0x1, TEGRA_T_SATA0_BKDOOR_CC_PROG_IF));
    216  1.3  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG_SATA_REG,
    217  1.3  jmcneill 	    0, TEGRA_T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN);
    218  1.3  jmcneill 
    219  1.3  jmcneill 	/* Enable access and bus mastering */
    220  1.3  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_T_SATA0_CFG1_REG,
    221  1.3  jmcneill 	    TEGRA_T_SATA0_CFG1_SERR |
    222  1.3  jmcneill 	    TEGRA_T_SATA0_CFG1_BUS_MASTER |
    223  1.3  jmcneill 	    TEGRA_T_SATA0_CFG1_MEM_SPACE |
    224  1.3  jmcneill 	    TEGRA_T_SATA0_CFG1_IO_SPACE,
    225  1.3  jmcneill 	    0);
    226  1.3  jmcneill 
    227  1.3  jmcneill 	/* MMIO setup */
    228  1.3  jmcneill 	bus_space_write_4(bst, bsh, TEGRA_SATA_FPCI_BAR5_REG,
    229  1.3  jmcneill 	    __SHIFTIN(0x10000, TEGRA_SATA_FPCI_BAR_START));
    230  1.3  jmcneill 	bus_space_write_4(bst, bsh, TEGRA_T_SATA0_CFG9_REG,
    231  1.3  jmcneill 	    __SHIFTIN(0x8000, TEGRA_T_SATA0_CFG9_BASE_ADDRESS));
    232  1.3  jmcneill 
    233  1.3  jmcneill 	/* Enable interrupts */
    234  1.3  jmcneill 	tegra_reg_set_clear(bst, bsh, TEGRA_SATA_INTR_MASK_REG,
    235  1.3  jmcneill 	    TEGRA_SATA_INTR_MASK_IP_INT, 0);
    236  1.3  jmcneill }
    237