wdc_obio.c revision 1.5 1 /*-
2 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas of 3am Software Foundry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "locators.h"
31
32 #include <sys/cdefs.h>
33
34 __KERNEL_RCSID(0, "$NetBSD: wdc_obio.c,v 1.5 2017/10/07 19:58:54 jdolecek Exp $");
35
36 #include <sys/param.h>
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39 #include <sys/tty.h>
40
41 #include "ioconf.h"
42
43 #include <sys/intr.h>
44 #include <sys/bus.h>
45
46 #include <powerpc/booke/cpuvar.h>
47
48 #include <dev/ic/wdcreg.h>
49 #include <dev/ata/atavar.h>
50 #include <dev/ic/wdcvar.h>
51
52 struct wdc_obio_softc {
53 struct wdc_softc sc_wdcdev;
54 struct ata_channel *wdc_chanlist[1];
55 struct ata_channel ata_channel;
56 struct wdc_regs wdc_regs;
57 void *sc_ih;
58 };
59
60 #define WDC_OBIO_AUXREG_OFFSET (WDC_NREG + 6)
61
62 static int wdc_obio_match(device_t, cfdata_t, void *);
63 static void wdc_obio_attach(device_t, device_t, void *);
64
65 CFATTACH_DECL_NEW(wdc_obio, sizeof(struct wdc_obio_softc),
66 wdc_obio_match, wdc_obio_attach, NULL, NULL);
67
68 static bool
69 wdc_obio_initregmap(struct wdc_regs *wdr, bus_space_tag_t bst,
70 bus_addr_t addr, bus_size_t size)
71 {
72 int error;
73
74 wdr->cmd_iot = bst;
75 wdr->ctl_iot = bst;
76
77 error = bus_space_map(wdr->cmd_iot, addr, size, 0, &wdr->cmd_baseioh);
78 if (error) {
79 wdr->cmd_baseioh = 0;
80 return false;
81 }
82
83 for (u_int i = 0; i < WDC_NREG; i++) {
84 if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
85 i, (i == 0) ? 2 : 1, &wdr->cmd_iohs[i])) {
86 return false;
87 }
88 }
89
90 if (bus_space_subregion(wdr->ctl_iot, wdr->cmd_baseioh,
91 WDC_OBIO_AUXREG_OFFSET, 1, &wdr->ctl_ioh)) {
92 return false;
93 }
94
95 return true;
96 }
97
98 static int
99 wdc_obio_match(device_t parent, cfdata_t cf, void *aux)
100 {
101 struct generic_attach_args * const ga = aux;
102 bus_size_t size = ga->ga_size;
103 struct ata_channel ch;
104 struct wdc_softc wdc;
105 struct wdc_regs wdr;
106 struct device dev;
107 int rv = 0;
108
109 if (ga->ga_addr == OBIOCF_ADDR_DEFAULT)
110 return 0;
111 if (size == OBIOCF_SIZE_DEFAULT || size > PAGE_SIZE)
112 size = 2 * WDC_NREG;
113
114 /*
115 * We need to see if a CF is attached in True-IDE mode
116 */
117 memset(&dev, 0, sizeof(dev));
118 memset(&wdc, 0, sizeof(wdc));
119 memset(&ch, 0, sizeof(ch));
120 memset(&wdr, 0, sizeof(wdr));
121
122 dev.dv_xname[0] = '?';
123 wdc.sc_atac.atac_dev = &dev;
124 ch.ch_atac = &wdc.sc_atac;
125 wdc.regs = &wdr;
126
127 if (wdc_obio_initregmap(&wdr, ga->ga_bst, ga->ga_addr, size)) {
128 wdc_init_shadow_regs(&ch);
129 rv = wdcprobe(&ch);
130 bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, size);
131 }
132
133 return rv;
134 }
135
136 static void
137 wdc_obio_attach(device_t parent, device_t self, void *aux)
138 {
139 struct wdc_obio_softc *sc = device_private(self);
140 struct wdc_regs * const wdr = &sc->wdc_regs;
141 struct generic_attach_args * const ga = aux;
142 bus_size_t size = ga->ga_size;
143
144 if (size == OBIOCF_SIZE_DEFAULT || size > PAGE_SIZE)
145 size = 2 * WDC_NREG;
146
147 sc->sc_wdcdev.sc_atac.atac_dev = self;
148 sc->sc_wdcdev.regs = wdr;
149 if (!wdc_obio_initregmap(wdr, ga->ga_bst, ga->ga_addr, size)) {
150 aprint_error(": couldn't map registers\n");
151 return;
152 }
153
154 //sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
155 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
156
157 sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
158 sc->wdc_chanlist[0] = &sc->ata_channel;
159 sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
160 sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
161 sc->sc_wdcdev.wdc_maxdrives = 2;
162 sc->ata_channel.ch_channel = 0;
163 sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
164 sc->ata_channel.ch_queue = ata_queue_alloc(1);
165 wdc_init_shadow_regs(wdr);
166
167
168 /*
169 * The interrupt line is controlled by a jumper. We can't detect
170 * this, except by allowing a request to time out, so assume that
171 * if the config file hasn't specified the IRQ, then the jumper isn't
172 * fitted.
173 */
174 if (ga->ga_irq != OBIOCF_IRQ_DEFAULT) {
175 sc->sc_ih = intr_establish(ga->ga_irq, IPL_BIO, IST_EDGE,
176 wdcintr, &sc->ata_channel);
177 aprint_normal(": interrupting at irq %d\n", ga->ga_irq);
178 } else {
179 sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
180 aprint_normal(": using polled I/O\n");
181 }
182
183 wdcattach(&sc->ata_channel);
184 }
185