Home | History | Annotate | Line # | Download | only in boot
wdc.c revision 1.10.4.1
      1  1.10.4.1     yamt /*	$NetBSD: wdc.c,v 1.10.4.1 2008/05/16 02:22:09 yamt Exp $	*/
      2       1.1      cdi 
      3       1.1      cdi /*-
      4       1.1      cdi  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5       1.1      cdi  * All rights reserved.
      6       1.1      cdi  *
      7       1.1      cdi  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1      cdi  * by Manuel Bouyer.
      9       1.1      cdi  *
     10       1.1      cdi  * Redistribution and use in source and binary forms, with or without
     11       1.1      cdi  * modification, are permitted provided that the following conditions
     12       1.1      cdi  * are met:
     13       1.1      cdi  * 1. Redistributions of source code must retain the above copyright
     14       1.1      cdi  *    notice, this list of conditions and the following disclaimer.
     15       1.1      cdi  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      cdi  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      cdi  *    documentation and/or other materials provided with the distribution.
     18       1.1      cdi  *
     19       1.1      cdi  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1      cdi  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1      cdi  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1      cdi  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1      cdi  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1      cdi  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1      cdi  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1      cdi  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1      cdi  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1      cdi  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1      cdi  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1      cdi  */
     31       1.1      cdi 
     32       1.1      cdi #include <sys/types.h>
     33       1.1      cdi #include <sys/disklabel.h>
     34       1.2    lukem #include <sys/bootblock.h>
     35       1.1      cdi 
     36       1.1      cdi #include <lib/libsa/stand.h>
     37       1.1      cdi #include <machine/param.h>
     38       1.1      cdi 
     39       1.1      cdi #include "boot.h"
     40       1.1      cdi #include "wdvar.h"
     41       1.1      cdi 
     42       1.1      cdi #define WDCDELAY	100
     43       1.1      cdi #define WDCNDELAY_RST	31000 * 10
     44       1.1      cdi 
     45       1.5  thorpej static int  wdcprobe(struct wdc_channel *chp);
     46       1.5  thorpej static int  wdc_wait_for_ready(struct wdc_channel *chp);
     47       1.1      cdi static int  wdc_read_block(struct wd_softc *sc, struct wdc_command *wd_c);
     48       1.5  thorpej static int  __wdcwait_reset(struct wdc_channel *chp, int drv_mask);
     49       1.1      cdi 
     50       1.1      cdi /*
     51       1.1      cdi  * Reset the controller.
     52       1.1      cdi  */
     53       1.1      cdi static int
     54       1.8  tsutsui __wdcwait_reset(struct wdc_channel *chp, int drv_mask)
     55       1.1      cdi {
     56       1.1      cdi 	int timeout;
     57       1.8  tsutsui 	uint8_t st0, st1;
     58       1.1      cdi 
     59       1.1      cdi 	/* wait for BSY to deassert */
     60       1.1      cdi 	for (timeout = 0; timeout < WDCNDELAY_RST; timeout++) {
     61       1.6  tsutsui 		WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM); /* master */
     62       1.1      cdi 		delay(10);
     63       1.6  tsutsui 		st0 = WDC_READ_REG(chp, wd_status);
     64       1.6  tsutsui 		WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM | 0x10); /* slave */
     65       1.1      cdi 		delay(10);
     66       1.6  tsutsui 		st1 = WDC_READ_REG(chp, wd_status);
     67       1.1      cdi 
     68       1.1      cdi 		if ((drv_mask & 0x01) == 0) {
     69       1.1      cdi 			/* no master */
     70       1.1      cdi 			if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) {
     71       1.1      cdi 				/* No master, slave is ready, it's done */
     72       1.1      cdi 				goto end;
     73       1.1      cdi 			}
     74       1.1      cdi 		} else if ((drv_mask & 0x02) == 0) {
     75       1.1      cdi 			/* no slave */
     76       1.1      cdi 			if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) {
     77       1.1      cdi 				/* No slave, master is ready, it's done */
     78       1.1      cdi 				goto end;
     79       1.1      cdi 			}
     80       1.1      cdi 		} else {
     81       1.1      cdi 			/* Wait for both master and slave to be ready */
     82       1.1      cdi 			if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) {
     83       1.1      cdi 				goto end;
     84       1.1      cdi 			}
     85       1.1      cdi 		}
     86       1.1      cdi 
     87       1.1      cdi 		delay(WDCDELAY);
     88       1.1      cdi 	}
     89       1.1      cdi 
     90       1.1      cdi 	/* Reset timed out. Maybe it's because drv_mask was not right */
     91       1.1      cdi 	if (st0 & WDCS_BSY)
     92       1.1      cdi 		drv_mask &= ~0x01;
     93       1.1      cdi 	if (st1 & WDCS_BSY)
     94       1.1      cdi 		drv_mask &= ~0x02;
     95       1.1      cdi 
     96       1.8  tsutsui  end:
     97       1.8  tsutsui 	return drv_mask;
     98       1.1      cdi }
     99       1.1      cdi 
    100       1.1      cdi /* Test to see controller with at last one attached drive is there.
    101       1.1      cdi  * Returns a bit for each possible drive found (0x01 for drive 0,
    102       1.1      cdi  * 0x02 for drive 1).
    103       1.1      cdi  * Logic:
    104       1.1      cdi  * - If a status register is at 0xff, assume there is no drive here
    105       1.1      cdi  *   (ISA has pull-up resistors).  Similarly if the status register has
    106       1.1      cdi  *   the value we last wrote to the bus (for IDE interfaces without pullups).
    107       1.1      cdi  *   If no drive at all -> return.
    108       1.1      cdi  * - reset the controller, wait for it to complete (may take up to 31s !).
    109       1.1      cdi  *   If timeout -> return.
    110       1.1      cdi  */
    111       1.1      cdi static int
    112       1.8  tsutsui wdcprobe(struct wdc_channel *chp)
    113       1.1      cdi {
    114      1.10  tsutsui 	uint8_t st0, st1;
    115       1.8  tsutsui 	uint8_t ret_value = 0x03;
    116       1.8  tsutsui 	uint8_t drive;
    117       1.1      cdi 	int found;
    118       1.1      cdi 
    119       1.1      cdi 	/*
    120       1.1      cdi 	 * Sanity check to see if the wdc channel responds at all.
    121       1.1      cdi 	 */
    122       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM);
    123       1.1      cdi 	delay(10);
    124       1.6  tsutsui 	st0 = WDC_READ_REG(chp, wd_status);
    125       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM | 0x10);
    126       1.1      cdi 	delay(10);
    127       1.6  tsutsui 	st1 = WDC_READ_REG(chp, wd_status);
    128       1.1      cdi 
    129       1.1      cdi 	if (st0 == 0xff || st0 == WDSD_IBM)
    130       1.1      cdi 		ret_value &= ~0x01;
    131       1.1      cdi 	if (st1 == 0xff || st1 == (WDSD_IBM | 0x10))
    132       1.1      cdi 		ret_value &= ~0x02;
    133       1.1      cdi 	if (ret_value == 0)
    134       1.8  tsutsui 		return ENXIO;
    135       1.1      cdi 
    136       1.1      cdi 	/* assert SRST, wait for reset to complete */
    137       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM);
    138       1.1      cdi 	delay(10);
    139       1.6  tsutsui 	WDC_WRITE_CTLREG(chp, wd_aux_ctlr, WDCTL_RST | WDCTL_IDS);
    140       1.1      cdi 	delay(1000);
    141       1.6  tsutsui 	WDC_WRITE_CTLREG(chp, wd_aux_ctlr, WDCTL_IDS);
    142       1.1      cdi 	delay(1000);
    143       1.6  tsutsui 	(void) WDC_READ_REG(chp, wd_error);
    144       1.6  tsutsui 	WDC_WRITE_CTLREG(chp, wd_aux_ctlr, WDCTL_4BIT);
    145       1.1      cdi 	delay(10);
    146       1.1      cdi 
    147       1.1      cdi 	ret_value = __wdcwait_reset(chp, ret_value);
    148       1.1      cdi 
    149       1.1      cdi 	/* if reset failed, there's nothing here */
    150       1.1      cdi 	if (ret_value == 0)
    151       1.8  tsutsui 		return ENXIO;
    152       1.1      cdi 
    153       1.1      cdi 	/*
    154       1.1      cdi 	 * Test presence of drives. First test register signatures looking for
    155       1.1      cdi 	 * ATAPI devices. If it's not an ATAPI and reset said there may be
    156       1.1      cdi 	 * something here assume it's ATA or OLD. Ghost will be killed later in
    157       1.1      cdi 	 * attach routine.
    158       1.1      cdi 	 */
    159       1.1      cdi 	found = 0;
    160       1.1      cdi 	for (drive = 0; drive < 2; drive++) {
    161       1.1      cdi 		if ((ret_value & (0x01 << drive)) == 0)
    162       1.1      cdi 			continue;
    163       1.8  tsutsui 		return 0;
    164       1.1      cdi 	}
    165       1.8  tsutsui 	return ENXIO;
    166       1.1      cdi }
    167       1.1      cdi 
    168       1.1      cdi /*
    169       1.1      cdi  * Initialize the device.
    170       1.1      cdi  */
    171       1.1      cdi int
    172       1.8  tsutsui wdc_init(struct wd_softc *sc, u_int *unit)
    173       1.1      cdi {
    174       1.8  tsutsui 
    175       1.1      cdi 	if (pciide_init(&sc->sc_channel, unit) != 0)
    176       1.8  tsutsui 		return ENXIO;
    177       1.1      cdi 	if (wdcprobe(&sc->sc_channel) != 0)
    178       1.8  tsutsui 		return ENXIO;
    179       1.8  tsutsui 	return 0;
    180       1.1      cdi }
    181       1.1      cdi 
    182       1.1      cdi /*
    183       1.1      cdi  * Wait until the device is ready.
    184       1.1      cdi  */
    185       1.1      cdi int
    186       1.8  tsutsui wdc_wait_for_ready(struct wdc_channel *chp)
    187       1.1      cdi {
    188       1.1      cdi 	u_int timeout;
    189       1.8  tsutsui 
    190       1.1      cdi 	for (timeout = WDC_TIMEOUT; timeout > 0; --timeout) {
    191       1.6  tsutsui 		if ((WDC_READ_REG(chp, wd_status) & (WDCS_BSY | WDCS_DRDY))
    192       1.1      cdi 				== WDCS_DRDY)
    193       1.8  tsutsui 			return 0;
    194       1.1      cdi 	}
    195       1.8  tsutsui 	return ENXIO;
    196       1.1      cdi }
    197       1.1      cdi 
    198       1.1      cdi /*
    199       1.1      cdi  * Read one block off the device.
    200       1.1      cdi  */
    201       1.1      cdi int
    202       1.8  tsutsui wdc_read_block(struct wd_softc *sc, struct wdc_command *wd_c)
    203       1.1      cdi {
    204       1.1      cdi 	int i;
    205       1.5  thorpej 	struct wdc_channel *chp = &sc->sc_channel;
    206       1.8  tsutsui 	uint16_t *ptr = (uint16_t *)wd_c->data;
    207       1.1      cdi 
    208       1.1      cdi 	if (ptr == NULL)
    209       1.8  tsutsui 		return 0;
    210       1.1      cdi 
    211       1.8  tsutsui 	for (i = wd_c->bcount; i > 0; i -= sizeof(uint16_t))
    212       1.6  tsutsui 		*ptr++ = WDC_READ_DATA(chp);
    213       1.1      cdi 
    214       1.8  tsutsui 	return 0;
    215       1.1      cdi }
    216       1.1      cdi 
    217       1.1      cdi /*
    218       1.3      wiz  * Send a command to the device (CHS and LBA addressing).
    219       1.1      cdi  */
    220       1.1      cdi int
    221       1.8  tsutsui wdccommand(struct wd_softc *sc, struct wdc_command *wd_c)
    222       1.1      cdi {
    223       1.5  thorpej 	struct wdc_channel *chp = &sc->sc_channel;
    224       1.1      cdi 
    225       1.1      cdi #if 0
    226       1.1      cdi 	DPRINTF(("wdccommand(%d, %d, %d, %d, %d, %d, %d)\n",
    227       1.8  tsutsui 	    wd_c->drive, wd_c->r_command, wd_c->r_cyl,
    228       1.8  tsutsui 	    wd_c->r_head, wd_c->r_sector, wd_c->bcount,
    229       1.8  tsutsui 	    wd_c->r_precomp));
    230       1.1      cdi #endif
    231       1.1      cdi 
    232       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_precomp, wd_c->r_precomp);
    233       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_seccnt, wd_c->r_count);
    234       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sector, wd_c->r_sector);
    235       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_cyl_lo, wd_c->r_cyl);
    236       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_cyl_hi, wd_c->r_cyl >> 8);
    237       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sdh,
    238       1.6  tsutsui 	    WDSD_IBM | (wd_c->drive << 4) | wd_c->r_head);
    239       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_command, wd_c->r_command);
    240       1.1      cdi 
    241       1.1      cdi 	if (wdc_wait_for_ready(chp) != 0)
    242       1.8  tsutsui 		return ENXIO;
    243       1.1      cdi 
    244       1.6  tsutsui 	if (WDC_READ_REG(chp, wd_status) & WDCS_ERR) {
    245       1.1      cdi 		printf("wd%d: error %x\n", chp->compatchan,
    246       1.8  tsutsui 		    WDC_READ_REG(chp, wd_error));
    247       1.8  tsutsui 		return ENXIO;
    248       1.1      cdi 	}
    249       1.1      cdi 
    250       1.8  tsutsui 	return 0;
    251       1.1      cdi }
    252       1.1      cdi 
    253       1.1      cdi /*
    254       1.1      cdi  * Send a command to the device (LBA48 addressing).
    255       1.1      cdi  */
    256       1.1      cdi int
    257       1.8  tsutsui wdccommandext(struct wd_softc *wd, struct wdc_command *wd_c)
    258       1.1      cdi {
    259       1.5  thorpej 	struct wdc_channel *chp = &wd->sc_channel;
    260       1.1      cdi 
    261       1.1      cdi 	/* Select drive, head, and addressing mode. */
    262       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, (wd_c->drive << 4) | WDSD_LBA);
    263       1.1      cdi 
    264       1.1      cdi 	/* previous */
    265       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_features, 0);
    266       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_seccnt, wd_c->r_count >> 8);
    267       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_hi, wd_c->r_blkno >> 40);
    268       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_mi, wd_c->r_blkno >> 32);
    269       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_lo, wd_c->r_blkno >> 24);
    270       1.1      cdi 
    271       1.1      cdi 	/* current */
    272       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_features, 0);
    273       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_seccnt, wd_c->r_count);
    274       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_hi, wd_c->r_blkno >> 16);
    275       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_mi, wd_c->r_blkno >> 8);
    276       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_lo, wd_c->r_blkno);
    277       1.1      cdi 
    278       1.1      cdi 	/* Send command. */
    279       1.6  tsutsui 	WDC_WRITE_REG(chp, wd_command, wd_c->r_command);
    280       1.1      cdi 
    281       1.1      cdi 	if (wdc_wait_for_ready(chp) != 0)
    282       1.8  tsutsui 		return ENXIO;
    283       1.1      cdi 
    284       1.6  tsutsui 	if (WDC_READ_REG(chp, wd_status) & WDCS_ERR) {
    285       1.1      cdi 		printf("wd%d: error %x\n", chp->compatchan,
    286       1.8  tsutsui 		    WDC_READ_REG(chp, wd_error));
    287       1.8  tsutsui 		return ENXIO;
    288       1.1      cdi 	}
    289       1.1      cdi 
    290       1.8  tsutsui 	return 0;
    291       1.1      cdi }
    292       1.1      cdi 
    293       1.1      cdi /*
    294       1.1      cdi  * Issue 'device identify' command.
    295       1.1      cdi  */
    296       1.1      cdi int
    297       1.8  tsutsui wdc_exec_identify(struct wd_softc *wd, void *data)
    298       1.1      cdi {
    299       1.1      cdi 	int error;
    300       1.1      cdi 	struct wdc_command wd_c;
    301       1.1      cdi 
    302       1.1      cdi 	memset(&wd_c, 0, sizeof(wd_c));
    303       1.1      cdi 
    304       1.1      cdi 	wd_c.drive = wd->sc_unit;
    305       1.1      cdi 	wd_c.r_command = WDCC_IDENTIFY;
    306       1.1      cdi 	wd_c.bcount = DEV_BSIZE;
    307       1.1      cdi 	wd_c.data = data;
    308       1.1      cdi 
    309       1.8  tsutsui 	if ((error = wdccommand(wd, &wd_c)) != 0)
    310       1.8  tsutsui 		return error;
    311       1.1      cdi 
    312       1.1      cdi 	return wdc_read_block(wd, &wd_c);
    313       1.1      cdi }
    314       1.1      cdi 
    315       1.1      cdi /*
    316       1.1      cdi  * Issue 'read' command.
    317       1.1      cdi  */
    318       1.1      cdi int
    319       1.8  tsutsui wdc_exec_read(struct wd_softc *wd, uint8_t cmd, daddr_t blkno, void *data)
    320       1.1      cdi {
    321       1.1      cdi 	int error;
    322       1.1      cdi 	struct wdc_command wd_c;
    323       1.1      cdi 
    324       1.1      cdi 	memset(&wd_c, 0, sizeof(wd_c));
    325       1.1      cdi 
    326       1.1      cdi 	if (wd->sc_flags & WDF_LBA48) {
    327       1.1      cdi 		/* LBA48 */
    328       1.1      cdi 		wd_c.r_blkno = blkno;
    329       1.1      cdi 	} else if (wd->sc_flags & WDF_LBA) {
    330       1.1      cdi 		/* LBA */
    331       1.1      cdi 		wd_c.r_sector = (blkno >> 0) & 0xff;
    332       1.1      cdi 		wd_c.r_cyl = (blkno >> 8) & 0xffff;
    333       1.1      cdi 		wd_c.r_head = (blkno >> 24) & 0x0f;
    334       1.1      cdi 		wd_c.r_head |= WDSD_LBA;
    335       1.1      cdi 	} else {
    336       1.1      cdi 		/* LHS */
    337       1.1      cdi 		wd_c.r_sector = blkno % wd->sc_label.d_nsectors;
    338       1.1      cdi 		wd_c.r_sector++;    /* Sectors begin with 1, not 0. */
    339       1.1      cdi 		blkno /= wd->sc_label.d_nsectors;
    340       1.1      cdi 		wd_c.r_head = blkno % wd->sc_label.d_ntracks;
    341       1.1      cdi 		blkno /= wd->sc_label.d_ntracks;
    342       1.1      cdi 		wd_c.r_cyl = blkno;
    343       1.1      cdi 		wd_c.r_head |= WDSD_CHS;
    344       1.1      cdi 	}
    345       1.1      cdi 
    346       1.1      cdi 	wd_c.data = data;
    347       1.1      cdi 	wd_c.r_count = 1;
    348       1.1      cdi 	wd_c.drive = wd->sc_unit;
    349       1.1      cdi 	wd_c.r_command = cmd;
    350       1.1      cdi 	wd_c.bcount = wd->sc_label.d_secsize;
    351       1.1      cdi 
    352       1.1      cdi 	if (wd->sc_flags & WDF_LBA48)
    353       1.1      cdi 		error = wdccommandext(wd, &wd_c);
    354       1.1      cdi 	else
    355       1.1      cdi 		error = wdccommand(wd, &wd_c);
    356       1.1      cdi 
    357       1.1      cdi 	if (error != 0)
    358       1.1      cdi 		return error;
    359       1.1      cdi 
    360       1.1      cdi 	return wdc_read_block(wd, &wd_c);
    361       1.1      cdi }
    362