/*-
* Copyright (c) 2003 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Manuel Bouyer.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
static int wdcprobe(struct wdc_channel *chp);
static int wdc_wait_for_ready(struct wdc_channel *chp);
static int wdc_read_block(struct wd_softc *sc, struct wdc_command *wd_c);
static int __wdcwait_reset(struct wdc_channel *chp, int drv_mask);
/*
* Reset the controller.
*/
static int
__wdcwait_reset(struct wdc_channel *chp, int drv_mask)
{
int timeout;
uint8_t st0, st1;
if ((drv_mask & 0x01) == 0) {
/* no master */
if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) {
/* No master, slave is ready, it's done */
goto end;
}
} else if ((drv_mask & 0x02) == 0) {
/* no slave */
if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) {
/* No slave, master is ready, it's done */
goto end;
}
} else {
/* Wait for both master and slave to be ready */
if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) {
goto end;
}
}
delay(WDCDELAY);
}
/* Reset timed out. Maybe it's because drv_mask was not right */
if (st0 & WDCS_BSY)
drv_mask &= ~0x01;
if (st1 & WDCS_BSY)
drv_mask &= ~0x02;
end:
return drv_mask;
}
/* Test to see controller with at last one attached drive is there.
* Returns a bit for each possible drive found (0x01 for drive 0,
* 0x02 for drive 1).
* Logic:
* - If a status register is at 0xff, assume there is no drive here
* (ISA has pull-up resistors). Similarly if the status register has
* the value we last wrote to the bus (for IDE interfaces without pullups).
* If no drive at all -> return.
* - reset the controller, wait for it to complete (may take up to 31s !).
* If timeout -> return.
*/
static int
wdcprobe(struct wdc_channel *chp)
{
uint8_t st0, st1;
uint8_t ret_value = 0x03;
uint8_t drive;
/*
* Sanity check to see if the wdc channel responds at all.
*/
WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM);
delay(10);
st0 = WDC_READ_REG(chp, wd_status);
WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM | 0x10);
delay(10);
st1 = WDC_READ_REG(chp, wd_status);
if (st0 == 0xff || st0 == WDSD_IBM)
ret_value &= ~0x01;
if (st1 == 0xff || st1 == (WDSD_IBM | 0x10))
ret_value &= ~0x02;
if (ret_value == 0)
return ENXIO;
/* if reset failed, there's nothing here */
if (ret_value == 0)
return ENXIO;
/*
* Test presence of drives. First test register signatures looking for
* ATAPI devices. If it's not an ATAPI and reset said there may be
* something here assume it's ATA or OLD. Ghost will be killed later in
* attach routine.
*/
for (drive = 0; drive < 2; drive++) {
if ((ret_value & (0x01 << drive)) == 0)
continue;
return 0;
}
return ENXIO;
}
/*
* Initialize the device.
*/
int
wdc_init(struct wd_softc *sc, u_int *unit)
{
struct wdc_channel *chp = &sc->sc_channel;
uint32_t cmdreg, ctlreg;
int i;
/* XXXX: Shuld reset CF COR here? */
switch (*unit) {
case 0: cmdreg = MMEYE_WDC0_IOBASE; break;
case 1: cmdreg = MMEYE_WDC1_IOBASE; break;
/*
* Read one block off the device.
*/
int
wdc_read_block(struct wd_softc *sc, struct wdc_command *wd_c)
{
int i;
struct wdc_channel *chp = &sc->sc_channel;
uint16_t *ptr = (uint16_t *)wd_c->data;
if (ptr == NULL)
return 0;
if (wd_c->r_command == WDCC_IDENTIFY)
for (i = wd_c->bcount; i > 0; i -= sizeof(uint16_t))
*ptr++ = WDC_READ_DATA(chp);
else
for (i = wd_c->bcount; i > 0; i -= sizeof(uint16_t))
*ptr++ = WDC_READ_DATA_STREAM(chp);
return 0;
}
/*
* Send a command to the device (CHS and LBA addressing).
*/
int
wdccommand(struct wd_softc *sc, struct wdc_command *wd_c)
{
struct wdc_channel *chp = &sc->sc_channel;