Home | History | Annotate | Line # | Download | only in fdt
      1 /*	$NetBSD: wdc_fdt.c,v 1.1 2026/07/06 23:45:48 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2025 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: wdc_fdt.c,v 1.1 2026/07/06 23:45:48 thorpej Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/device.h>
     38 #include <sys/intr.h>
     39 #include <sys/systm.h>
     40 
     41 #include <dev/fdt/fdtvar.h>
     42 
     43 #include <dev/ic/wdcreg.h>
     44 #include <dev/ata/atavar.h>
     45 #include <dev/ic/wdcvar.h>
     46 
     47 struct wdc_fdt_softc {
     48 	struct wdc_softc sc_wdcdev;
     49 	struct ata_channel *wdc_chanlist[1];
     50 	struct ata_channel ata_channel;
     51 	struct wdc_regs wdc_regs;
     52 	void *sc_ih;
     53 	uint8_t pio_mode[2];
     54 };
     55 
     56 struct wdc_fdt_config {
     57 	void	(*set_modes)(struct ata_channel *);
     58 };
     59 
     60 static const struct device_compatible_entry compat_data[] = {
     61 	{ .compat = "ata-generic" },
     62 	DEVICE_COMPAT_EOL
     63 };
     64 
     65 static int
     66 wdc_fdt_match(device_t parent, cfdata_t cf, void *aux)
     67 {
     68 	struct fdt_attach_args * const faa = aux;
     69 
     70 	return of_compatible_match(faa->faa_phandle, compat_data);
     71 }
     72 
     73 static void
     74 wdc_fdt_attach(device_t parent, device_t self, void *aux)
     75 {
     76 	struct wdc_fdt_softc *sc = device_private(self);
     77 	struct fdt_attach_args * const faa = aux;
     78 	const int phandle = faa->faa_phandle;
     79 	struct wdc_regs *wdr;
     80 	bus_addr_t cmd_addr, ctl_addr;
     81 	bus_size_t cmd_size, ctl_size;
     82 	char intrstr[128];
     83 	int error;
     84 
     85 	const struct wdc_fdt_config *config =
     86             of_compatible_lookup(phandle, compat_data)->data;
     87 
     88 	sc->sc_wdcdev.sc_atac.atac_dev = self;
     89 	sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
     90 	wdr->cmd_iot = faa->faa_bst;
     91 	wdr->ctl_iot = faa->faa_bst;
     92 
     93 	if (fdtbus_get_reg(phandle, 0, &cmd_addr, &cmd_size) != 0) {
     94 		aprint_error(": couldn't get command registers\n");
     95 		return;
     96 	}
     97 
     98 	if (fdtbus_get_reg(phandle, 1, &ctl_addr, &ctl_size) != 0) {
     99 		aprint_error(": couldn't get control registers\n");
    100 		return;
    101 	}
    102 
    103 	error = bus_space_map(wdr->cmd_iot, cmd_addr, cmd_size, 0,
    104 			      &wdr->cmd_baseioh);
    105 	if (error) {
    106 		aprint_error(": couldn't map command registers (error=%d)\n",
    107 		    error);
    108 		return;
    109 	}
    110 
    111 	error = bus_space_map(wdr->ctl_iot, ctl_addr, ctl_size, 0,
    112 			      &wdr->ctl_ioh);
    113 	if (error) {
    114 		aprint_error(": couldn't map control registers (error=%d)\n",
    115 		    error);
    116 		return;
    117 	}
    118 
    119 	bus_size_t data_regsize = 2;
    120 	if (of_getprop_bool(phandle, "ata-generic,use8bit")) {
    121 		data_regsize = 1;
    122 	} else {
    123 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
    124 		if (!of_getprop_bool(phandle, "ata-generic,use16bit")) {
    125 			sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA32;
    126 			data_regsize = 4;
    127 		}
    128 	}
    129 
    130 	uint32_t reg_shift;
    131 	if (of_getprop_uint32(phandle, "reg-shift", &reg_shift) < 0) {
    132 		reg_shift = 0;
    133 	}
    134 
    135 	for (int i = 0; i < WDC_NREG; i++) {
    136 		error = bus_space_subregion(wdr->cmt_iot, wdr->cmd_baseioh,
    137 					    i << reg_shift,
    138 					    i == wd_data ? data_regsize : 1,
    139 					    &wdr->cmd_iohs[i]);
    140 		(void) data_regsize;
    141 		if (error) {
    142 			aprint_error(": couldn't subregion command "
    143 				     "register %d (error=%d)\n", i, error);
    144 			return;
    145 		}
    146 	}
    147 
    148 	uint32_t max_pio;
    149 	if (of_getprop_uint32(phandle, "pio-mode", &max_pio) < 0 ||
    150 	    config == NULL || config->set_modes == NULL) {
    151 		max_pio = 0;
    152 	}
    153 
    154 	sc->sc_wdcdev.sc_atac.atac_pio_cap = max_pio;
    155 	sc->wdc_chanlist[0] = &sc->ata_channel;
    156 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
    157 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
    158 	sc->sc_wdcdev.sc_atac.atac_set_modes = config != NULL ?
    159 	    config->set_modes : NULL;
    160 	sc->sc_wdcdev.wdc_maxdrives = 2;
    161 	sc->ata_channel.ch_channel = 0;
    162 	sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    163 
    164 	wdc_init_shadow_regs(wdr);
    165 
    166 	aprint_normal("\n");
    167 
    168 	if (! fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    169 		goto use_polled_mode;
    170 	}
    171 
    172 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_BIO,
    173 	    0, wdcintr, &sc->ata_channel, device_xname(self));
    174 	if (sc->sc_ih == NULL) {
    175 		aprint_error_dev(self, "failed to establish interrupt at %s\n",
    176 		    intrstr);
    177  use_polled_mode:
    178 		aprint_verbose_dev(self, "using polled I/O\n");
    179 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
    180 	} else {
    181 		aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    182 	}
    183 
    184 	if (wdcprobe(wdr)) {
    185 		wdcattach(&sc->ata_channel);
    186 	} else {
    187 		aprint_normal_dev(self, "no drives present.\n");
    188 	}
    189 }
    190 
    191 CFATTACH_DECL_NEW(wdc_fdt, sizeof(struct wdc_fdt_softc),
    192     wdc_fdt_match, wdc_fdt_attach, NULL, NULL);
    193