/* $NetBSD: hdc9224.c,v 1.62 2021/08/07 16:19:07 thorpej Exp $ */
/*
* Copyright (c) 1996 Ludd, University of Lule}, Sweden.
* All rights reserved.
*
* This code is derived from software contributed to Ludd by Bertram Barth.
*
* 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 AUTHOR ``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 AUTHOR 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.
*/
/*
* with much help from (in alphabetical order):
* Jeremy
* Roger Ivie
* Rick Macklem
* Mike Young
*
* Rewritten by Ragge 25 Jun 2000. New features:
* - Uses interrupts instead of polling to signal ready.
* - Can cooperate with the SCSI routines WRT. the DMA area.
*
* TODO:
* - Floppy support missing.
* - Bad block forwarding missing.
* - Statistics collection.
*/
#undef RDDEBUG
#include <dev/mscp/mscp.h> /* For DEC disk encoding */
#include <vax/vsa/hdc9224.h>
#include "ioconf.h"
#include "locators.h"
/*
* on-disk geometry block
*/
#define _aP __attribute__ ((packed)) /* force byte-alignment */
struct rdgeom {
char mbz[10]; /* 10 bytes of zero */
long xbn_count _aP; /* number of XBNs */
long dbn_count _aP; /* number of DBNs */
long lbn_count _aP; /* number of LBNs (Logical-Block-Numbers) */
long rbn_count _aP; /* number of RBNs (Replacement-Block-Numbers) */
short nspt; /* number of sectors per track */
short ntracks; /* number of tracks */
short ncylinders; /* number of cylinders */
short precomp; /* first cylinder for write precompensation */
short reduced; /* first cylinder for reduced write current */
short seek_rate; /* seek rate or zero for buffered seeks */
short crc_eec; /* 0 if CRC, 1 if ECC is being used */
short rct; /* "replacement control table" (RCT) */
short rct_ncopies; /* number of copies of the RCT */
long media_id _aP; /* media identifier */
short interleave; /* sector-to-sector interleave */
short headskew; /* head-to-head skew */
short cylskew; /* cylinder-to-cylinder skew */
short gap0_size; /* size of GAP 0 in the MFM format */
short gap1_size; /* size of GAP 1 in the MFM format */
short gap2_size; /* size of GAP 2 in the MFM format */
short gap3_size; /* size of GAP 3 in the MFM format */
short sync_value; /* sync value used when formatting */
char reserved[32]; /* reserved for use by the RQDX formatter */
short serial_number; /* serial number */
#if 0 /* we don't need these 412 useless bytes ... */
char fill[412-2]; /* Filler bytes to the end of the block */
short checksum; /* checksum over the XBN */
#endif
};
/*
* Software status
*/
struct rdsoftc {
device_t sc_dev; /* must be here! (pseudo-OOP:) */
struct hdcsoftc *sc_hdc;
struct disk sc_disk; /* disklabel etc. */
struct rdgeom sc_xbn; /* on-disk geometry information */
int sc_drive; /* physical unit number */
};
struct hdcsoftc {
device_t sc_dev; /* must be here (pseudo-OOP:) */
struct evcnt sc_intrcnt;
struct vsbus_dma sc_vd;
vaddr_t sc_regs; /* register addresses */
struct bufq_state *sc_q;
struct buf *sc_active;
struct hdc9224_UDCreg sc_creg; /* (command) registers to be written */
struct hdc9224_UDCreg sc_sreg; /* (status) registers being read */
void * sc_dmabase; /* */
int sc_dmasize;
void *sc_bufaddr; /* Current in-core address */
int sc_diskblk; /* Current block on disk */
int sc_bytecnt; /* How much left to transfer */
int sc_xfer; /* Current transfer size */
int sc_retries;
volatile u_char sc_status; /* last status from interrupt */
char sc_intbit;
};
/* At least 0.7 uS between register accesses */
static int rd_dmasize, inq = 0;
static volatile int u;
#define WAIT __asm("movl %0,%0;movl %0,%0;movl %0,%0; movl %0,%0" :: "m"(u))
/*
* new-config's hdcmatch() is similar to old-config's hdcprobe(),
* thus we probe for the existence of the controller and reset it.
* NB: we can't initialize the controller yet, since space for hdcsoftc
* is not yet allocated. Thus we do this in hdcattach()...
*/
int
hdcmatch(device_t parent, cfdata_t cf, void *aux)
{
struct vsbus_attach_args * const va = aux;
volatile char * const hdc_csr = (volatile char *)va->va_addr;
int i;
hdc_csr[4] = DKC_CMD_RESET; /* reset chip */
for (i = 0; i < 1000; i++) {
DELAY(1000);
if (hdc_csr[4] & DKC_ST_DONE)
break;
}
if (i == 100)
return 0; /* No response to reset */
hdc_csr[4] = DKC_CMD_SETREGPTR|UDC_TERM;
WAIT;
hdc_csr[0] = UDC_TC_CRCPRE|UDC_TC_INTDONE;
WAIT;
hdc_csr[4] = DKC_CMD_DRDESELECT; /* Should be harmless */
DELAY(1000);
return (1);
}
int
hdcprint(void *aux, const char *name)
{
struct hdc_attach_args * const ha = aux;
if (name)
aprint_normal ("RD?? at %s drive %d", name, ha->ha_drive);
return UNCONF;
}
/*
* hdc_attach() probes for all possible devices
*/
void
hdcattach(device_t parent, device_t self, void *aux)
{
struct vsbus_attach_args * const va = aux;
struct hdcsoftc * const sc = device_private(self);
struct hdc_attach_args ha;
int status, i;
/*
* now probe for all possible hard drives
*/
for (i = 0; i < 4; i++) {
if (i == 2) /* Floppy, needs special handling */
continue;
HDC_WCMD(DKC_CMD_DRSELECT | i);
DELAY(1000);
status = HDC_RSTAT;
ha.ha_drive = i;
if ((status & DKC_ST_TERMCOD) == DKC_TC_SUCCESS)
config_found(self, (void *)&ha, hdcprint, CFARGS_NONE);
}
}
/*
* rdmatch() probes for the existence of a RD-type disk/floppy
*/
int
rdmatch(device_t parent, cfdata_t cf, void *aux)
{
struct hdc_attach_args * const ha = aux;
if (cf->cf_loc[HDCCF_DRIVE] != HDCCF_DRIVE_DEFAULT &&
cf->cf_loc[HDCCF_DRIVE] != ha->ha_drive)
return 0;
if (ha->ha_drive == 2) /* Always floppy, not supported */
return 0;
bn = sc->sc_diskblk;
lp = rd->sc_disk.dk_label;
if (bn) {
cn = bn / lp->d_secpercyl;
sn = bn % lp->d_secpercyl;
tn = sn / lp->d_nsectors;
sn = sn % lp->d_nsectors;
} else
cn = sn = tn = 0;
cn++; /* first cylinder is reserved */
memset(p, 0, sizeof(struct hdc9224_UDCreg));
/*
* Tricky thing: the controller do itself only increase the sector
* number, not the track or cylinder number. Therefore the driver
* is not allowed to have transfers that crosses track boundaries.
*/
blks = sc->sc_bytecnt/DEV_BSIZE;
if ((sn + blks) > lp->d_nsectors)
blks = lp->d_nsectors - sn;
/*
* Return the size of a partition, if known, or -1 if not.
*/
int
rdpsize(dev_t dev)
{
struct rdsoftc * const rd = device_lookup_private(&rd_cd, DISKUNIT(dev));
const int part = DISKPART(dev);
if (rd == NULL || part >= rd->sc_disk.dk_label->d_npartitions)
return -1;
/*
* we have to wait 0.7 usec between two accesses to any of the
* dkc-registers, on a VS2000 with 1 MIPS, this is roughly one
* instruction. Thus the loop-overhead will be enough...
*/
static void
hdc_readregs(struct hdcsoftc *sc)
{
int i;
char *p;
HDC_WCMD(DKC_CMD_SETREGPTR);
WAIT;
p = (void*)&sc->sc_sreg;
for (i=0; i<10; i++) {
*p++ = HDC_RREG; /* dkc_reg auto-increments */
WAIT;
}
}
static void
hdc_writeregs(struct hdcsoftc *sc)
{
int i;
char *p;
HDC_WCMD(DKC_CMD_SETREGPTR);
p = (void*)&sc->sc_creg;
for (i=0; i<10; i++) {
HDC_WREG(*p++); /* dkc_reg auto-increments */
WAIT;
}
}
/*
* hdc_command() issues a command and polls the intreq-register
* to find when command has completed
*/
int
hdc_command(struct hdcsoftc *sc, int cmd)
{
hdc_writeregs(sc); /* write the prepared registers */
HDC_WCMD(cmd);
WAIT;
return (0);
}
int
hdc_rdselect(struct hdcsoftc *sc, int unit)
{
struct hdc9224_UDCreg * const p = &sc->sc_creg;
int error;
/*
* bring "creg" in some known-to-work state and
* select the drive with the DRIVE SELECT command.
*/
memset(p, 0, sizeof(*p));