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