Home | History | Annotate | Line # | Download | only in g1
      1  1.6   thorpej /* $NetBSD: wdc_g1.c,v 1.6 2023/12/20 06:36:03 thorpej Exp $ */
      2  1.1   tsutsui 
      3  1.1   tsutsui /*-
      4  1.1   tsutsui  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1   tsutsui  * All rights reserved.
      6  1.1   tsutsui  *
      7  1.1   tsutsui  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1   tsutsui  * by Charles M. Hannum and by Onno van der Linden.
      9  1.1   tsutsui  *
     10  1.1   tsutsui  * Redistribution and use in source and binary forms, with or without
     11  1.1   tsutsui  * modification, are permitted provided that the following conditions
     12  1.1   tsutsui  * are met:
     13  1.1   tsutsui  * 1. Redistributions of source code must retain the above copyright
     14  1.1   tsutsui  *    notice, this list of conditions and the following disclaimer.
     15  1.1   tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   tsutsui  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   tsutsui  *    documentation and/or other materials provided with the distribution.
     18  1.1   tsutsui  *
     19  1.1   tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1   tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1   tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1   tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1   tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1   tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1   tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1   tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1   tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1   tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1   tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1   tsutsui  */
     31  1.1   tsutsui 
     32  1.1   tsutsui #include "opt_ata.h"	/* for ATADEBUG */
     33  1.1   tsutsui 
     34  1.1   tsutsui #include <sys/param.h>
     35  1.1   tsutsui #include <sys/systm.h>
     36  1.1   tsutsui #include <sys/device.h>
     37  1.1   tsutsui #include <sys/bus.h>
     38  1.1   tsutsui 
     39  1.1   tsutsui #include <machine/intr.h>
     40  1.1   tsutsui #include <machine/sysasicvar.h>
     41  1.1   tsutsui 
     42  1.1   tsutsui #include <arch/dreamcast/dev/g1/g1busvar.h>
     43  1.1   tsutsui 
     44  1.1   tsutsui #include <dev/ata/atavar.h>
     45  1.1   tsutsui #include <dev/ic/wdcvar.h>
     46  1.1   tsutsui #include <dev/ata/atareg.h>
     47  1.1   tsutsui 
     48  1.1   tsutsui #define WDC_G1_CMD_ADDR			0x005f7080
     49  1.1   tsutsui #define WDC_G1_REG_NPORTS		8
     50  1.1   tsutsui #define WDC_G1_CTL_ADDR			0x005f7018
     51  1.1   tsutsui #define WDC_G1_AUXREG_NPORTS		1
     52  1.1   tsutsui 
     53  1.1   tsutsui struct wdc_g1_softc {
     54  1.1   tsutsui 	struct	wdc_softc sc_wdcdev;
     55  1.1   tsutsui 	struct	ata_channel *wdc_chanlist[1];
     56  1.1   tsutsui 	struct	ata_channel ata_channel;
     57  1.1   tsutsui 	struct	wdc_regs wdc_regs;
     58  1.1   tsutsui 	void	*sc_ih;
     59  1.1   tsutsui 	int	sc_irq;
     60  1.1   tsutsui };
     61  1.1   tsutsui 
     62  1.1   tsutsui static int	wdc_g1_probe(device_t, cfdata_t, void *);
     63  1.1   tsutsui static void	wdc_g1_attach(device_t, device_t, void *);
     64  1.1   tsutsui static void	wdc_g1_do_reset(struct ata_channel *, int);
     65  1.1   tsutsui static int	wdc_g1_intr(void *);
     66  1.1   tsutsui 
     67  1.1   tsutsui CFATTACH_DECL_NEW(wdc_g1bus, sizeof(struct wdc_g1_softc),
     68  1.1   tsutsui     wdc_g1_probe, wdc_g1_attach, NULL, NULL);
     69  1.1   tsutsui 
     70  1.1   tsutsui static int
     71  1.1   tsutsui wdc_g1_probe(device_t parent, cfdata_t cf, void *aux)
     72  1.1   tsutsui {
     73  1.1   tsutsui 	struct g1bus_attach_args *ga = aux;
     74  1.1   tsutsui 	struct wdc_regs wdr;
     75  1.1   tsutsui 	int result = 0, i;
     76  1.1   tsutsui 
     77  1.1   tsutsui 	*((volatile uint32_t *)0xa05f74e4) = 0x1fffff;
     78  1.1   tsutsui 	for (i = 0; i < 0x200000 / 4; i++)
     79  1.1   tsutsui 		(void)((volatile uint32_t *)0xa0000000)[i];
     80  1.1   tsutsui 
     81  1.1   tsutsui 	wdr.cmd_iot = ga->ga_memt;
     82  1.1   tsutsui 	if (bus_space_map(wdr.cmd_iot, WDC_G1_CMD_ADDR,
     83  1.1   tsutsui 	    WDC_G1_REG_NPORTS * 4, 0, &wdr.cmd_baseioh))
     84  1.1   tsutsui 		goto out;
     85  1.1   tsutsui 
     86  1.1   tsutsui 	for (i = 0; i < WDC_G1_REG_NPORTS; i++) {
     87  1.1   tsutsui 		if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, i * 4,
     88  1.1   tsutsui 		    i == 0 ? 2 : 1, &wdr.cmd_iohs[i]) != 0)
     89  1.1   tsutsui 			goto outunmap;
     90  1.1   tsutsui 	}
     91  1.1   tsutsui 
     92  1.4  jdolecek 	wdc_init_shadow_regs(&wdr);
     93  1.1   tsutsui 
     94  1.1   tsutsui 	wdr.ctl_iot = ga->ga_memt;
     95  1.1   tsutsui 	if (bus_space_map(wdr.ctl_iot, WDC_G1_CTL_ADDR,
     96  1.1   tsutsui 	    WDC_G1_AUXREG_NPORTS, 0, &wdr.ctl_ioh))
     97  1.1   tsutsui 	  goto outunmap;
     98  1.1   tsutsui 
     99  1.5   tsutsui 	result = wdcprobe_with_reset(&wdr, wdc_g1_do_reset);
    100  1.1   tsutsui 
    101  1.1   tsutsui 	bus_space_unmap(wdr.ctl_iot, wdr.ctl_ioh, WDC_G1_AUXREG_NPORTS);
    102  1.1   tsutsui  outunmap:
    103  1.1   tsutsui 	bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_G1_REG_NPORTS);
    104  1.1   tsutsui  out:
    105  1.1   tsutsui 	return result;
    106  1.1   tsutsui }
    107  1.1   tsutsui 
    108  1.1   tsutsui static void
    109  1.1   tsutsui wdc_g1_attach(struct device *parent, struct device *self, void *aux)
    110  1.1   tsutsui {
    111  1.1   tsutsui 	struct wdc_g1_softc *sc = device_private(self);
    112  1.1   tsutsui 	struct wdc_regs *wdr;
    113  1.1   tsutsui 	struct g1bus_attach_args *ga = aux;
    114  1.1   tsutsui 	int i;
    115  1.1   tsutsui 
    116  1.1   tsutsui 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    117  1.1   tsutsui 	sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
    118  1.1   tsutsui 
    119  1.1   tsutsui 	wdr->cmd_iot = ga->ga_memt;
    120  1.1   tsutsui 	wdr->ctl_iot = ga->ga_memt;
    121  1.1   tsutsui 	if (bus_space_map(wdr->cmd_iot, WDC_G1_CMD_ADDR,
    122  1.1   tsutsui 	    WDC_G1_REG_NPORTS * 4, 0, &wdr->cmd_baseioh) ||
    123  1.1   tsutsui 	    bus_space_map(wdr->ctl_iot, WDC_G1_CTL_ADDR,
    124  1.1   tsutsui 	    WDC_G1_AUXREG_NPORTS, 0, &wdr->ctl_ioh)) {
    125  1.1   tsutsui 		aprint_error(": couldn't map registers\n");
    126  1.1   tsutsui 		return;
    127  1.1   tsutsui 	}
    128  1.1   tsutsui 
    129  1.1   tsutsui 	for (i = 0; i < WDC_G1_REG_NPORTS; i++) {
    130  1.1   tsutsui 		if (bus_space_subregion(wdr->cmd_iot,
    131  1.1   tsutsui 		      wdr->cmd_baseioh, i * 4, i == 0 ? 2 : 1,
    132  1.1   tsutsui 		      &wdr->cmd_iohs[i]) != 0) {
    133  1.1   tsutsui 			aprint_error(": couldn't subregion registers\n");
    134  1.1   tsutsui 			return;
    135  1.1   tsutsui 		}
    136  1.1   tsutsui 	}
    137  1.1   tsutsui 
    138  1.1   tsutsui 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_PREATA;
    139  1.1   tsutsui 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
    140  1.1   tsutsui 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
    141  1.1   tsutsui 	sc->wdc_chanlist[0] = &sc->ata_channel;
    142  1.1   tsutsui 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
    143  1.1   tsutsui 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
    144  1.1   tsutsui 	sc->sc_wdcdev.wdc_maxdrives = 2;
    145  1.1   tsutsui 	sc->sc_wdcdev.reset = wdc_g1_do_reset;
    146  1.1   tsutsui 	sc->ata_channel.ch_channel = 0;
    147  1.1   tsutsui 	sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    148  1.3  jdolecek 
    149  1.2  jdolecek 	wdc_init_shadow_regs(wdr);
    150  1.1   tsutsui 
    151  1.1   tsutsui 	aprint_normal(": %s\n", sysasic_intr_string(SYSASIC_IRL9));
    152  1.1   tsutsui 
    153  1.1   tsutsui 	sysasic_intr_establish(SYSASIC_EVENT_GDROM, IPL_BIO, SYSASIC_IRL9,
    154  1.1   tsutsui 	    wdc_g1_intr, &sc->ata_channel);
    155  1.1   tsutsui 
    156  1.1   tsutsui 	wdcattach(&sc->ata_channel);
    157  1.1   tsutsui }
    158  1.1   tsutsui 
    159  1.1   tsutsui int
    160  1.1   tsutsui wdc_g1_intr(void *arg)
    161  1.1   tsutsui {
    162  1.1   tsutsui 
    163  1.1   tsutsui 	return wdcintr(arg);
    164  1.1   tsutsui }
    165  1.1   tsutsui 
    166  1.4  jdolecek /*
    167  1.5   tsutsui  * This does what the generic wdc_do_reset() does, with additional
    168  1.5   tsutsui  * GD-ROM reset. GD-ROM is a very early ATAPI device appeared in 1998
    169  1.5   tsutsui  * and it doesn't reset itself by the WDCTL_RST in AUX_CTLR but requires
    170  1.5   tsutsui  * ATAPI_SOFT_RESET command to reset whole device as a master.
    171  1.4  jdolecek  */
    172  1.1   tsutsui static void
    173  1.1   tsutsui wdc_g1_do_reset(struct ata_channel *chp, int poll)
    174  1.1   tsutsui {
    175  1.1   tsutsui 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
    176  1.1   tsutsui 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
    177  1.1   tsutsui 	int s = 0;
    178  1.1   tsutsui 
    179  1.1   tsutsui 	if (poll != 0)
    180  1.1   tsutsui 		s = splbio();
    181  1.1   tsutsui 
    182  1.1   tsutsui 	/* master */
    183  1.1   tsutsui 	bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    184  1.1   tsutsui 	    WDSD_IBM);
    185  1.1   tsutsui 	delay(10);	/* 400ns delay */
    186  1.1   tsutsui 	/* assert SRST, wait for reset to complete */
    187  1.1   tsutsui 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
    188  1.1   tsutsui 	    WDCTL_RST | WDCTL_4BIT | WDCTL_IDS);
    189  1.1   tsutsui 	delay(2000);
    190  1.1   tsutsui 	(void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0);
    191  1.1   tsutsui 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
    192  1.1   tsutsui 	    WDCTL_4BIT | WDCTL_IDS);
    193  1.1   tsutsui 	delay(10);	/* 400ns delay */
    194  1.1   tsutsui 
    195  1.1   tsutsui 	/* reset GD-ROM at master via ATAPI command */
    196  1.1   tsutsui 	bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
    197  1.1   tsutsui 	    WDSD_IBM);
    198  1.1   tsutsui 	bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0,
    199  1.1   tsutsui 	    ATAPI_SOFT_RESET);
    200  1.1   tsutsui 	delay(100 * 1000);
    201  1.1   tsutsui 
    202  1.1   tsutsui 	if (poll != 0)
    203  1.1   tsutsui 		splx(s);
    204  1.1   tsutsui }
    205