xref: /netbsd-src/sys/arch/arm/gemini/obio_wdc.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /*	$NetBSD: obio_wdc.c,v 1.8 2017/10/20 07:06:06 jdolecek 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.8 2017/10/20 07:06:06 jdolecek Exp $");
38 
39 #include "locators.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45 
46 #include <sys/bus.h>
47 #include <machine/intr.h>
48 
49 #include <arm/gemini/gemini_var.h>
50 #include <arm/gemini/gemini_reg.h>
51 #include <arm/gemini/gemini_obiovar.h>
52 
53 #include <dev/ic/wdcreg.h>
54 #include <dev/ata/atavar.h>
55 #include <dev/ic/wdcvar.h>
56 
57 struct wdc_obio_softc {
58 	struct	wdc_softc sc_wdcdev;
59 	struct	ata_channel *wdc_chanlist[1];
60 	struct	ata_channel ata_channel;
61 	struct	wdc_regs wdc_regs;
62 	void	*sc_ih;
63 };
64 
65 static int	wdc_obio_match(device_t, cfdata_t, void *);
66 static void	wdc_obio_attach(device_t, device_t, void *);
67 
68 CFATTACH_DECL_NEW(wdc_obio, sizeof(struct wdc_obio_softc),
69     wdc_obio_match, wdc_obio_attach, NULL, NULL);
70 
71 static int
72 wdc_obio_match(device_t parent, cfdata_t match, void *aux)
73 {
74 	struct obio_attach_args * const obio = aux;
75 
76 	if (obio->obio_addr == GEMINI_MIDE_BASEn(0)
77 	||  obio->obio_addr == GEMINI_MIDE_BASEn(1))
78 		return 1;
79 
80         return 0;
81 
82 }
83 
84 static void
85 wdc_obio_attach(device_t parent, device_t self, void *aux)
86 {
87 	struct wdc_obio_softc *sc = device_private(self);
88 	struct wdc_regs *wdr;
89 	struct obio_attach_args *obio = aux;
90 	uint chan;
91 	int i;
92 
93 	/*
94 	 * we treat the two channels of the Gemini MIDE controller
95 	 * as seperate wdc controllers, because they have
96 	 * independent interrupts.  'chan' here is an MIDE chanel,
97 	 * (not to be confused with ATA channel).
98 	 */
99 	switch (obio->obio_addr) {
100 	case  GEMINI_MIDE_BASEn(0):
101 		chan = 0;
102 		break;
103 	case  GEMINI_MIDE_BASEn(1):
104 		chan = 1;
105 		break;
106 	default:
107 		panic("wdc_obio_attach: bad address");
108 	}
109 
110 	sc->sc_wdcdev.sc_atac.atac_dev = self;
111 	sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
112 	wdr->cmd_iot = obio->obio_iot;
113 	wdr->ctl_iot = obio->obio_iot;
114 	if (bus_space_map(wdr->cmd_iot, obio->obio_addr, GEMINI_MIDE_SIZE,
115 	    0, &wdr->cmd_baseioh)) {
116 		aprint_error(": couldn't map registers\n");
117 		return;
118 	}
119 
120 	for (i = 0; i < 8; i++) {
121 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
122 		    GEMINI_MIDE_CMDBLK + i, 1, &wdr->cmd_iohs[i]) != 0) {
123 			aprint_error(": couldn't subregion registers\n");
124 			return;
125 		}
126 	}
127 
128 	if (bus_space_subregion(wdr->ctl_iot, wdr->cmd_baseioh,
129 	    GEMINI_MIDE_CTLBLK, 1, &wdr->ctl_ioh) != 0) {
130 		aprint_error(": couldn't subregion registers\n");
131 		return;
132 	}
133 
134 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
135 
136 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
137 	sc->wdc_chanlist[0] = &sc->ata_channel;
138 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
139 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
140 	sc->sc_wdcdev.wdc_maxdrives = 2;
141 	sc->ata_channel.ch_channel = 0;
142 	sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
143 
144 	wdc_init_shadow_regs(wdr);
145 
146 	aprint_normal("\n");
147 
148 	if (obio->obio_intr != OBIOCF_INTR_DEFAULT)
149 		sc->sc_ih = intr_establish(obio->obio_intr, IPL_BIO, IST_LEVEL_HIGH,
150 			wdcintr, &sc->ata_channel);
151 	else {
152 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
153 		aprint_normal_dev(self, "Using polled I/O\n");
154 	}
155 
156 	wdcattach(&sc->ata_channel);
157 }
158