Home | History | Annotate | Line # | Download | only in boot
wdc.c revision 1.8
      1  1.8  tsutsui /*	$NetBSD: wdc.c,v 1.8 2007/08/03 13:15:57 tsutsui 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.2    lukem #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.5  thorpej static int  wdcprobe(struct wdc_channel *chp);
     53  1.5  thorpej 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.5  thorpej 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.8  tsutsui __wdcwait_reset(struct wdc_channel *chp, int drv_mask)
     62  1.1      cdi {
     63  1.1      cdi 	int timeout;
     64  1.8  tsutsui 	uint8_t st0, st1;
     65  1.1      cdi 
     66  1.1      cdi 	/* wait for BSY to deassert */
     67  1.1      cdi 	for (timeout = 0; timeout < WDCNDELAY_RST; timeout++) {
     68  1.6  tsutsui 		WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM); /* master */
     69  1.1      cdi 		delay(10);
     70  1.6  tsutsui 		st0 = WDC_READ_REG(chp, wd_status);
     71  1.6  tsutsui 		WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM | 0x10); /* slave */
     72  1.1      cdi 		delay(10);
     73  1.6  tsutsui 		st1 = WDC_READ_REG(chp, wd_status);
     74  1.1      cdi 
     75  1.1      cdi 		if ((drv_mask & 0x01) == 0) {
     76  1.1      cdi 			/* no master */
     77  1.1      cdi 			if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) {
     78  1.1      cdi 				/* No master, slave is ready, it's done */
     79  1.1      cdi 				goto end;
     80  1.1      cdi 			}
     81  1.1      cdi 		} else if ((drv_mask & 0x02) == 0) {
     82  1.1      cdi 			/* no slave */
     83  1.1      cdi 			if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) {
     84  1.1      cdi 				/* No slave, master is ready, it's done */
     85  1.1      cdi 				goto end;
     86  1.1      cdi 			}
     87  1.1      cdi 		} else {
     88  1.1      cdi 			/* Wait for both master and slave to be ready */
     89  1.1      cdi 			if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) {
     90  1.1      cdi 				goto end;
     91  1.1      cdi 			}
     92  1.1      cdi 		}
     93  1.1      cdi 
     94  1.1      cdi 		delay(WDCDELAY);
     95  1.1      cdi 	}
     96  1.1      cdi 
     97  1.1      cdi 	/* Reset timed out. Maybe it's because drv_mask was not right */
     98  1.1      cdi 	if (st0 & WDCS_BSY)
     99  1.1      cdi 		drv_mask &= ~0x01;
    100  1.1      cdi 	if (st1 & WDCS_BSY)
    101  1.1      cdi 		drv_mask &= ~0x02;
    102  1.1      cdi 
    103  1.8  tsutsui  end:
    104  1.8  tsutsui 	return drv_mask;
    105  1.1      cdi }
    106  1.1      cdi 
    107  1.1      cdi /* Test to see controller with at last one attached drive is there.
    108  1.1      cdi  * Returns a bit for each possible drive found (0x01 for drive 0,
    109  1.1      cdi  * 0x02 for drive 1).
    110  1.1      cdi  * Logic:
    111  1.1      cdi  * - If a status register is at 0xff, assume there is no drive here
    112  1.1      cdi  *   (ISA has pull-up resistors).  Similarly if the status register has
    113  1.1      cdi  *   the value we last wrote to the bus (for IDE interfaces without pullups).
    114  1.1      cdi  *   If no drive at all -> return.
    115  1.1      cdi  * - reset the controller, wait for it to complete (may take up to 31s !).
    116  1.1      cdi  *   If timeout -> return.
    117  1.1      cdi  */
    118  1.1      cdi static int
    119  1.8  tsutsui wdcprobe(struct wdc_channel *chp)
    120  1.1      cdi {
    121  1.8  tsutsui 	uint8_t st0, st1, sc, sn, cl, ch;
    122  1.8  tsutsui 	uint8_t ret_value = 0x03;
    123  1.8  tsutsui 	uint8_t drive;
    124  1.1      cdi 	int found;
    125  1.1      cdi 
    126  1.1      cdi 	/*
    127  1.1      cdi 	 * Sanity check to see if the wdc channel responds at all.
    128  1.1      cdi 	 */
    129  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM);
    130  1.1      cdi 	delay(10);
    131  1.6  tsutsui 	st0 = WDC_READ_REG(chp, wd_status);
    132  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM | 0x10);
    133  1.1      cdi 	delay(10);
    134  1.6  tsutsui 	st1 = WDC_READ_REG(chp, wd_status);
    135  1.1      cdi 
    136  1.1      cdi 	if (st0 == 0xff || st0 == WDSD_IBM)
    137  1.1      cdi 		ret_value &= ~0x01;
    138  1.1      cdi 	if (st1 == 0xff || st1 == (WDSD_IBM | 0x10))
    139  1.1      cdi 		ret_value &= ~0x02;
    140  1.1      cdi 	if (ret_value == 0)
    141  1.8  tsutsui 		return ENXIO;
    142  1.1      cdi 
    143  1.1      cdi 	/* assert SRST, wait for reset to complete */
    144  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, WDSD_IBM);
    145  1.1      cdi 	delay(10);
    146  1.6  tsutsui 	WDC_WRITE_CTLREG(chp, wd_aux_ctlr, WDCTL_RST | WDCTL_IDS);
    147  1.1      cdi 	delay(1000);
    148  1.6  tsutsui 	WDC_WRITE_CTLREG(chp, wd_aux_ctlr, WDCTL_IDS);
    149  1.1      cdi 	delay(1000);
    150  1.6  tsutsui 	(void) WDC_READ_REG(chp, wd_error);
    151  1.6  tsutsui 	WDC_WRITE_CTLREG(chp, wd_aux_ctlr, WDCTL_4BIT);
    152  1.1      cdi 	delay(10);
    153  1.1      cdi 
    154  1.1      cdi 	ret_value = __wdcwait_reset(chp, ret_value);
    155  1.1      cdi 
    156  1.1      cdi 	/* if reset failed, there's nothing here */
    157  1.1      cdi 	if (ret_value == 0)
    158  1.8  tsutsui 		return ENXIO;
    159  1.1      cdi 
    160  1.1      cdi 	/*
    161  1.1      cdi 	 * Test presence of drives. First test register signatures looking for
    162  1.1      cdi 	 * ATAPI devices. If it's not an ATAPI and reset said there may be
    163  1.1      cdi 	 * something here assume it's ATA or OLD. Ghost will be killed later in
    164  1.1      cdi 	 * attach routine.
    165  1.1      cdi 	 */
    166  1.1      cdi 	found = 0;
    167  1.1      cdi 	for (drive = 0; drive < 2; drive++) {
    168  1.1      cdi 		if ((ret_value & (0x01 << drive)) == 0)
    169  1.1      cdi 			continue;
    170  1.8  tsutsui 		return 0;
    171  1.1      cdi 	}
    172  1.8  tsutsui 	return ENXIO;
    173  1.1      cdi }
    174  1.1      cdi 
    175  1.1      cdi /*
    176  1.1      cdi  * Initialize the device.
    177  1.1      cdi  */
    178  1.1      cdi int
    179  1.8  tsutsui wdc_init(struct wd_softc *sc, u_int *unit)
    180  1.1      cdi {
    181  1.8  tsutsui 
    182  1.1      cdi 	if (pciide_init(&sc->sc_channel, unit) != 0)
    183  1.8  tsutsui 		return ENXIO;
    184  1.1      cdi 	if (wdcprobe(&sc->sc_channel) != 0)
    185  1.8  tsutsui 		return ENXIO;
    186  1.8  tsutsui 	return 0;
    187  1.1      cdi }
    188  1.1      cdi 
    189  1.1      cdi /*
    190  1.1      cdi  * Wait until the device is ready.
    191  1.1      cdi  */
    192  1.1      cdi int
    193  1.8  tsutsui wdc_wait_for_ready(struct wdc_channel *chp)
    194  1.1      cdi {
    195  1.1      cdi 	u_int timeout;
    196  1.8  tsutsui 
    197  1.1      cdi 	for (timeout = WDC_TIMEOUT; timeout > 0; --timeout) {
    198  1.6  tsutsui 		if ((WDC_READ_REG(chp, wd_status) & (WDCS_BSY | WDCS_DRDY))
    199  1.1      cdi 				== WDCS_DRDY)
    200  1.8  tsutsui 			return 0;
    201  1.1      cdi 	}
    202  1.8  tsutsui 	return ENXIO;
    203  1.1      cdi }
    204  1.1      cdi 
    205  1.1      cdi /*
    206  1.1      cdi  * Read one block off the device.
    207  1.1      cdi  */
    208  1.1      cdi int
    209  1.8  tsutsui wdc_read_block(struct wd_softc *sc, struct wdc_command *wd_c)
    210  1.1      cdi {
    211  1.1      cdi 	int i;
    212  1.5  thorpej 	struct wdc_channel *chp = &sc->sc_channel;
    213  1.8  tsutsui 	uint16_t *ptr = (uint16_t *)wd_c->data;
    214  1.1      cdi 
    215  1.1      cdi 	if (ptr == NULL)
    216  1.8  tsutsui 		return 0;
    217  1.1      cdi 
    218  1.8  tsutsui 	for (i = wd_c->bcount; i > 0; i -= sizeof(uint16_t))
    219  1.6  tsutsui 		*ptr++ = WDC_READ_DATA(chp);
    220  1.1      cdi 
    221  1.8  tsutsui 	return 0;
    222  1.1      cdi }
    223  1.1      cdi 
    224  1.1      cdi /*
    225  1.3      wiz  * Send a command to the device (CHS and LBA addressing).
    226  1.1      cdi  */
    227  1.1      cdi int
    228  1.8  tsutsui wdccommand(struct wd_softc *sc, struct wdc_command *wd_c)
    229  1.1      cdi {
    230  1.8  tsutsui 	uint8_t err;
    231  1.5  thorpej 	struct wdc_channel *chp = &sc->sc_channel;
    232  1.1      cdi 
    233  1.1      cdi #if 0
    234  1.1      cdi 	DPRINTF(("wdccommand(%d, %d, %d, %d, %d, %d, %d)\n",
    235  1.8  tsutsui 	    wd_c->drive, wd_c->r_command, wd_c->r_cyl,
    236  1.8  tsutsui 	    wd_c->r_head, wd_c->r_sector, wd_c->bcount,
    237  1.8  tsutsui 	    wd_c->r_precomp));
    238  1.1      cdi #endif
    239  1.1      cdi 
    240  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_precomp, wd_c->r_precomp);
    241  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_seccnt, wd_c->r_count);
    242  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sector, wd_c->r_sector);
    243  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_cyl_lo, wd_c->r_cyl);
    244  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_cyl_hi, wd_c->r_cyl >> 8);
    245  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sdh,
    246  1.6  tsutsui 	    WDSD_IBM | (wd_c->drive << 4) | wd_c->r_head);
    247  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_command, wd_c->r_command);
    248  1.1      cdi 
    249  1.1      cdi 	if (wdc_wait_for_ready(chp) != 0)
    250  1.8  tsutsui 		return ENXIO;
    251  1.1      cdi 
    252  1.6  tsutsui 	if (WDC_READ_REG(chp, wd_status) & WDCS_ERR) {
    253  1.1      cdi 		printf("wd%d: error %x\n", chp->compatchan,
    254  1.8  tsutsui 		    WDC_READ_REG(chp, wd_error));
    255  1.8  tsutsui 		return ENXIO;
    256  1.1      cdi 	}
    257  1.1      cdi 
    258  1.8  tsutsui 	return 0;
    259  1.1      cdi }
    260  1.1      cdi 
    261  1.1      cdi /*
    262  1.1      cdi  * Send a command to the device (LBA48 addressing).
    263  1.1      cdi  */
    264  1.1      cdi int
    265  1.8  tsutsui wdccommandext(struct wd_softc *wd, struct wdc_command *wd_c)
    266  1.1      cdi {
    267  1.8  tsutsui 	uint8_t err;
    268  1.5  thorpej 	struct wdc_channel *chp = &wd->sc_channel;
    269  1.1      cdi 
    270  1.1      cdi 	/* Select drive, head, and addressing mode. */
    271  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_sdh, (wd_c->drive << 4) | WDSD_LBA);
    272  1.1      cdi 
    273  1.1      cdi 	/* previous */
    274  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_features, 0);
    275  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_seccnt, wd_c->r_count >> 8);
    276  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_hi, wd_c->r_blkno >> 40);
    277  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_mi, wd_c->r_blkno >> 32);
    278  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_lo, wd_c->r_blkno >> 24);
    279  1.1      cdi 
    280  1.1      cdi 	/* current */
    281  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_features, 0);
    282  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_seccnt, wd_c->r_count);
    283  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_hi, wd_c->r_blkno >> 16);
    284  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_mi, wd_c->r_blkno >> 8);
    285  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_lba_lo, wd_c->r_blkno);
    286  1.1      cdi 
    287  1.1      cdi 	/* Send command. */
    288  1.6  tsutsui 	WDC_WRITE_REG(chp, wd_command, wd_c->r_command);
    289  1.1      cdi 
    290  1.1      cdi 	if (wdc_wait_for_ready(chp) != 0)
    291  1.8  tsutsui 		return ENXIO;
    292  1.1      cdi 
    293  1.6  tsutsui 	if (WDC_READ_REG(chp, wd_status) & WDCS_ERR) {
    294  1.1      cdi 		printf("wd%d: error %x\n", chp->compatchan,
    295  1.8  tsutsui 		    WDC_READ_REG(chp, wd_error));
    296  1.8  tsutsui 		return ENXIO;
    297  1.1      cdi 	}
    298  1.1      cdi 
    299  1.8  tsutsui 	return 0;
    300  1.1      cdi }
    301  1.1      cdi 
    302  1.1      cdi /*
    303  1.1      cdi  * Issue 'device identify' command.
    304  1.1      cdi  */
    305  1.1      cdi int
    306  1.8  tsutsui wdc_exec_identify(struct wd_softc *wd, void *data)
    307  1.1      cdi {
    308  1.1      cdi 	int error;
    309  1.1      cdi 	struct wdc_command wd_c;
    310  1.1      cdi 
    311  1.1      cdi 	memset(&wd_c, 0, sizeof(wd_c));
    312  1.1      cdi 
    313  1.1      cdi 	wd_c.drive = wd->sc_unit;
    314  1.1      cdi 	wd_c.r_command = WDCC_IDENTIFY;
    315  1.1      cdi 	wd_c.bcount = DEV_BSIZE;
    316  1.1      cdi 	wd_c.data = data;
    317  1.1      cdi 
    318  1.8  tsutsui 	if ((error = wdccommand(wd, &wd_c)) != 0)
    319  1.8  tsutsui 		return error;
    320  1.1      cdi 
    321  1.1      cdi 	return wdc_read_block(wd, &wd_c);
    322  1.1      cdi }
    323  1.1      cdi 
    324  1.1      cdi /*
    325  1.1      cdi  * Issue 'read' command.
    326  1.1      cdi  */
    327  1.1      cdi int
    328  1.8  tsutsui wdc_exec_read(struct wd_softc *wd, uint8_t cmd, daddr_t blkno, void *data)
    329  1.1      cdi {
    330  1.1      cdi 	int error;
    331  1.1      cdi 	struct wdc_command wd_c;
    332  1.1      cdi 
    333  1.1      cdi 	memset(&wd_c, 0, sizeof(wd_c));
    334  1.1      cdi 
    335  1.1      cdi 	if (wd->sc_flags & WDF_LBA48) {
    336  1.1      cdi 		/* LBA48 */
    337  1.1      cdi 		wd_c.r_blkno = blkno;
    338  1.1      cdi 	} else if (wd->sc_flags & WDF_LBA) {
    339  1.1      cdi 		/* LBA */
    340  1.1      cdi 		wd_c.r_sector = (blkno >> 0) & 0xff;
    341  1.1      cdi 		wd_c.r_cyl = (blkno >> 8) & 0xffff;
    342  1.1      cdi 		wd_c.r_head = (blkno >> 24) & 0x0f;
    343  1.1      cdi 		wd_c.r_head |= WDSD_LBA;
    344  1.1      cdi 	} else {
    345  1.1      cdi 		/* LHS */
    346  1.1      cdi 		wd_c.r_sector = blkno % wd->sc_label.d_nsectors;
    347  1.1      cdi 		wd_c.r_sector++;    /* Sectors begin with 1, not 0. */
    348  1.1      cdi 		blkno /= wd->sc_label.d_nsectors;
    349  1.1      cdi 		wd_c.r_head = blkno % wd->sc_label.d_ntracks;
    350  1.1      cdi 		blkno /= wd->sc_label.d_ntracks;
    351  1.1      cdi 		wd_c.r_cyl = blkno;
    352  1.1      cdi 		wd_c.r_head |= WDSD_CHS;
    353  1.1      cdi 	}
    354  1.1      cdi 
    355  1.1      cdi 	wd_c.data = data;
    356  1.1      cdi 	wd_c.r_count = 1;
    357  1.1      cdi 	wd_c.drive = wd->sc_unit;
    358  1.1      cdi 	wd_c.r_command = cmd;
    359  1.1      cdi 	wd_c.bcount = wd->sc_label.d_secsize;
    360  1.1      cdi 
    361  1.1      cdi 	if (wd->sc_flags & WDF_LBA48)
    362  1.1      cdi 		error = wdccommandext(wd, &wd_c);
    363  1.1      cdi 	else
    364  1.1      cdi 		error = wdccommand(wd, &wd_c);
    365  1.1      cdi 
    366  1.1      cdi 	if (error != 0)
    367  1.1      cdi 		return error;
    368  1.1      cdi 
    369  1.1      cdi 	return wdc_read_block(wd, &wd_c);
    370  1.1      cdi }
    371