1/*	$NetBSD: obio_wdc.c,v 1.12 2022/09/27 06:36:41 skrll Exp $	*/
2
3/* adapted from iq31244/wdc_obio.c:
4 *	NetBSD: wdc_obio.c,v 1.5 2008/04/28 20:23:16 martin Exp
5 */
6
7/*-
8 * Copyright (c) 1998, 2003, 2005 The NetBSD Foundation, Inc.
9 * All rights reserved.
10 *
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Charles M. Hannum and by Onno van der Linden.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: obio_wdc.c,v 1.12 2022/09/27 06:36:41 skrll Exp $");
38
39#include "locators.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/device.h>
44
45#include <sys/bus.h>
46#include <machine/intr.h>
47
48#include <arm/gemini/gemini_var.h>
49#include <arm/gemini/gemini_reg.h>
50#include <arm/gemini/gemini_obiovar.h>
51
52#include <dev/ic/wdcreg.h>
53#include <dev/ata/atavar.h>
54#include <dev/ic/wdcvar.h>
55
56struct wdc_obio_softc {
57	struct	wdc_softc sc_wdcdev;
58	struct	ata_channel *wdc_chanlist[1];
59	struct	ata_channel ata_channel;
60	struct	wdc_regs wdc_regs;
61	void	*sc_ih;
62};
63
64static int	wdc_obio_match(device_t, cfdata_t, void *);
65static void	wdc_obio_attach(device_t, device_t, void *);
66
67CFATTACH_DECL_NEW(wdc_obio, sizeof(struct wdc_obio_softc),
68    wdc_obio_match, wdc_obio_attach, NULL, NULL);
69
70static int
71wdc_obio_match(device_t parent, cfdata_t match, void *aux)
72{
73	struct obio_attach_args * const obio = aux;
74
75	if (obio->obio_addr == GEMINI_MIDE_BASEn(0)
76	||  obio->obio_addr == GEMINI_MIDE_BASEn(1))
77		return 1;
78
79        return 0;
80
81}
82
83static void
84wdc_obio_attach(device_t parent, device_t self, void *aux)
85{
86	struct wdc_obio_softc *sc = device_private(self);
87	struct wdc_regs *wdr;
88	struct obio_attach_args *obio = aux;
89	uint chan;
90	int i;
91
92	/*
93	 * we treat the two channels of the Gemini MIDE controller
94	 * as separate wdc controllers, because they have
95	 * independent interrupts.  'chan' here is a MIDE channel,
96	 * (not to be confused with ATA channel).
97	 */
98	switch (obio->obio_addr) {
99	case  GEMINI_MIDE_BASEn(0):
100		chan = 0;
101		break;
102	case  GEMINI_MIDE_BASEn(1):
103		chan = 1;
104		break;
105	default:
106		panic("wdc_obio_attach: bad address");
107	}
108
109	sc->sc_wdcdev.sc_atac.atac_dev = self;
110	sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
111	wdr->cmd_iot = obio->obio_iot;
112	wdr->ctl_iot = obio->obio_iot;
113	if (bus_space_map(wdr->cmd_iot, obio->obio_addr, GEMINI_MIDE_SIZE,
114	    0, &wdr->cmd_baseioh)) {
115		aprint_error(": couldn't map registers\n");
116		return;
117	}
118
119	for (i = 0; i < 8; i++) {
120		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
121		    GEMINI_MIDE_CMDBLK + i, 1, &wdr->cmd_iohs[i]) != 0) {
122			aprint_error(": couldn't subregion registers\n");
123			return;
124		}
125	}
126
127	if (bus_space_subregion(wdr->ctl_iot, wdr->cmd_baseioh,
128	    GEMINI_MIDE_CTLBLK, 1, &wdr->ctl_ioh) != 0) {
129		aprint_error(": couldn't subregion registers\n");
130		return;
131	}
132
133	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
134
135	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
136	sc->wdc_chanlist[0] = &sc->ata_channel;
137	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
138	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
139	sc->sc_wdcdev.wdc_maxdrives = 2;
140	sc->ata_channel.ch_channel = 0;
141	sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
142
143	wdc_init_shadow_regs(wdr);
144
145	aprint_normal("\n");
146
147	if (obio->obio_intr != OBIOCF_INTR_DEFAULT)
148		sc->sc_ih = intr_establish(obio->obio_intr, IPL_BIO, IST_LEVEL_HIGH,
149			wdcintr, &sc->ata_channel);
150	else {
151		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
152		aprint_normal_dev(self, "Using polled I/O\n");
153	}
154
155	wdcattach(&sc->ata_channel);
156}
157