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.8 2022/02/12 02:40:28 riastradh 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
52struct 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
62static int wdc_obio_match(device_t, cfdata_t, void *);
63static void wdc_obio_attach(device_t, device_t, void *);
64
65CFATTACH_DECL_NEW(wdc_obio, sizeof(struct wdc_obio_softc),
66    wdc_obio_match, wdc_obio_attach, NULL, NULL);
67
68static bool
69wdc_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
98static int
99wdc_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 wdc_regs wdr;
104	int rv = 0;
105
106	if (ga->ga_addr == OBIOCF_ADDR_DEFAULT)
107		return 0;
108	if (size == OBIOCF_SIZE_DEFAULT  || size > PAGE_SIZE)
109		size = 2 * WDC_NREG;
110
111	/*
112	 * We need to see if a CF is attached in True-IDE mode
113	 */
114	memset(&wdr, 0, sizeof(wdr));
115
116	if (wdc_obio_initregmap(&wdr, ga->ga_bst, ga->ga_addr, size)) {
117		wdc_init_shadow_regs(&wdr);
118		rv = wdcprobe(&wdr);
119		bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, size);
120	}
121
122	return rv;
123}
124
125static void
126wdc_obio_attach(device_t parent, device_t self, void *aux)
127{
128	struct wdc_obio_softc *sc = device_private(self);
129	struct wdc_regs * const wdr = &sc->wdc_regs;
130	struct generic_attach_args * const ga = aux;
131	bus_size_t size = ga->ga_size;
132
133	if (size == OBIOCF_SIZE_DEFAULT || size > PAGE_SIZE)
134		size = 2 * WDC_NREG;
135
136	sc->sc_wdcdev.sc_atac.atac_dev = self;
137	sc->sc_wdcdev.regs = wdr;
138	if (!wdc_obio_initregmap(wdr, ga->ga_bst, ga->ga_addr, size)) {
139		aprint_error(": couldn't map registers\n");
140		return;
141	}
142
143	//sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
144	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
145
146	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
147	sc->wdc_chanlist[0] = &sc->ata_channel;
148	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
149	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
150	sc->sc_wdcdev.wdc_maxdrives = 2;
151	sc->ata_channel.ch_channel = 0;
152	sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
153
154	wdc_init_shadow_regs(wdr);
155
156	/*
157	 * The interrupt line is controlled by a jumper.  We can't detect
158	 * this, except by allowing a request to time out, so assume that
159	 * if the config file hasn't specified the IRQ, then the jumper isn't
160	 * fitted.
161	 */
162	if (ga->ga_irq != OBIOCF_IRQ_DEFAULT) {
163		sc->sc_ih = intr_establish(ga->ga_irq, IPL_BIO, IST_EDGE,
164		    wdcintr, &sc->ata_channel);
165		aprint_normal(": interrupting at irq %d\n", ga->ga_irq);
166	} else {
167		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
168		aprint_normal(": using polled I/O\n");
169	}
170
171	wdcattach(&sc->ata_channel);
172}
173