/*-
* 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.
*/
if (len > sizeof(local))
len = sizeof(local);
dst = last = local;
end = src + len - 1;
/* reserve space for '\0' */
if (len < 2)
goto out;
/* skip leading white space */
while (*src != '\0' && src < end && *src == ' ')
++src;
/* copy string, omitting trailing white space */
while (*src != '\0' && src < end) {
*dst++ = *src;
if (*src++ != ' ')
last = dst;
}
out:
*last = '\0';
return local;
}
/* 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 drives = 0x03;
uint8_t drive, cl, ch;
uint8_t ident[DEV_BSIZE];
/*
* 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)
drives &= ~0x01;
if (st1 == 0xff || st1 == (WDSD_IBM | 0x10))
drives &= ~0x02;
if (drives == 0)
return 0;
if (!(st0 & WDCS_DRDY))
drives &= ~0x01;
if (!(st1 & WDCS_DRDY))
drives &= ~0x02;
if (drives == 0)
return 0;
/* if reset failed, there's nothing here */
if (drives == 0)
return 0;
/*
* 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 ((drives & (1 << drive)) == 0)
continue;
/*
* Read one block off the device.
*/
int
wdc_read_block(struct wdc_channel *chp, struct wdc_command *wd_c)
{
int i;
uint16_t *ptr = (uint16_t *)wd_c->data;
if (ptr == NULL)
return EIO;
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 wdc_channel *chp, struct wdc_command *wd_c)
{