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$");
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	ata_queue wdc_chqueue;
57	struct	wdc_regs wdc_regs;
58	void	*sc_ih;
59};
60
61#define WDC_OBIO_AUXREG_OFFSET (WDC_NREG + 6)
62
63static int wdc_obio_match(device_t, cfdata_t, void *);
64static void wdc_obio_attach(device_t, device_t, void *);
65
66CFATTACH_DECL_NEW(wdc_obio, sizeof(struct wdc_obio_softc),
67    wdc_obio_match, wdc_obio_attach, NULL, NULL);
68
69static bool
70wdc_obio_initregmap(struct wdc_regs *wdr, bus_space_tag_t bst,
71	bus_addr_t addr, bus_size_t size)
72{
73	int error;
74
75	wdr->cmd_iot = bst;
76	wdr->ctl_iot = bst;
77
78	error = bus_space_map(wdr->cmd_iot, addr, size, 0, &wdr->cmd_baseioh);
79	if (error) {
80		wdr->cmd_baseioh = 0;
81		return false;
82	}
83
84	for (u_int i = 0; i < WDC_NREG; i++) {
85		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
86		    i, (i == 0) ? 2 : 1, &wdr->cmd_iohs[i])) {
87			return false;
88		}
89	}
90
91	if (bus_space_subregion(wdr->ctl_iot, wdr->cmd_baseioh,
92	    WDC_OBIO_AUXREG_OFFSET, 1, &wdr->ctl_ioh)) {
93		return false;
94	}
95
96	return true;
97}
98
99static int
100wdc_obio_match(device_t parent, cfdata_t cf, void *aux)
101{
102	struct generic_attach_args * const ga = aux;
103	bus_size_t size = ga->ga_size;
104	struct ata_channel ch;
105	struct wdc_softc wdc;
106	struct wdc_regs wdr;
107	struct device dev;
108	int rv = 0;
109
110	if (ga->ga_addr == OBIOCF_ADDR_DEFAULT)
111		return 0;
112	if (size == OBIOCF_SIZE_DEFAULT  || size > PAGE_SIZE)
113		size = 2 * WDC_NREG;
114
115	/*
116	 * We need to see if a CF is attached in True-IDE mode
117	 */
118	memset(&dev, 0, sizeof(dev));
119	memset(&wdc, 0, sizeof(wdc));
120	memset(&ch, 0, sizeof(ch));
121	memset(&wdr, 0, sizeof(wdr));
122
123	dev.dv_xname[0] = '?';
124	wdc.sc_atac.atac_dev = &dev;
125	ch.ch_atac = &wdc.sc_atac;
126	wdc.regs = &wdr;
127
128	if (wdc_obio_initregmap(&wdr, ga->ga_bst, ga->ga_addr, size)) {
129		wdc_init_shadow_regs(&ch);
130		rv = wdcprobe(&ch);
131		bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, size);
132	}
133
134	return rv;
135}
136
137static void
138wdc_obio_attach(device_t parent, device_t self, void *aux)
139{
140	struct wdc_obio_softc *sc = device_private(self);
141	struct wdc_regs * const wdr = &sc->wdc_regs;
142	struct generic_attach_args * const ga = aux;
143	bus_size_t size = ga->ga_size;
144
145	if (size == OBIOCF_SIZE_DEFAULT || size > PAGE_SIZE)
146		size = 2 * WDC_NREG;
147
148	sc->sc_wdcdev.sc_atac.atac_dev = self;
149	sc->sc_wdcdev.regs = wdr;
150	if (!wdc_obio_initregmap(wdr, ga->ga_bst, ga->ga_addr, size)) {
151		aprint_error(": couldn't map registers\n");
152		return;
153	}
154
155	//sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
156	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
157
158	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
159	sc->wdc_chanlist[0] = &sc->ata_channel;
160	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
161	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
162	sc->ata_channel.ch_channel = 0;
163	sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
164	sc->ata_channel.ch_queue = &sc->wdc_chqueue;
165	sc->ata_channel.ch_ndrive = 2;
166
167	wdc_init_shadow_regs(&sc->ata_channel);
168
169
170	/*
171	 * The interrupt line is controlled by a jumper.  We can't detect
172	 * this, except by allowing a request to time out, so assume that
173	 * if the config file hasn't specified the IRQ, then the jumper isn't
174	 * fitted.
175	 */
176	if (ga->ga_irq != OBIOCF_IRQ_DEFAULT) {
177		sc->sc_ih = intr_establish(ga->ga_irq, IPL_BIO, IST_EDGE,
178		    wdcintr, &sc->ata_channel);
179		aprint_normal(": interrupting at irq %d\n", ga->ga_irq);
180	} else {
181		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_NOIRQ;
182		aprint_normal(": using polled I/O\n");
183	}
184
185	wdcattach(&sc->ata_channel);
186}
187