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