1.\" $NetBSD: wdc.9,v 1.12 2003/10/05 17:48:49 bouyer Exp $ 2.\" 3.\" Copyright (c) 1998 Manuel Bouyer. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 3. All advertising materials mentioning features or use of this software 14.\" must display the following acknowledgement: 15.\" This product includes software developed by Manuel Bouyer. 16.\" 4. The name of the author may not be used to endorse or promote products 17.\" derived from this software without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27.\" INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29.\" 30.\" 31.Dd October 18, 1998 32.Dt WDC 9 33.Os 34.Sh NAME 35.Nm wdc 36.Nd machine-independent IDE/ATAPI driver 37.Sh SYNOPSIS 38.In dev/ata/atavar.h 39.In sys/dev/ic/wdcvar.h 40.Ft int 41.Fn wdcprobe "struct channel_softc * chp" 42.Ft void 43.Fn wdcattach "struct channel_softc * chp" 44.Sh DESCRIPTION 45The 46.Nm 47driver provides the machine independent core functions for driving IDE 48devices. 49IDE devices-specific drivers ( 50.Nm wd 51or 52.Nm atapibus ) 53will use services provided by 54.Nm wdc . 55.Pp 56The machine-dependent bus front-end provides informations to 57.Nm 58with the 59.Va wdc_softc 60and 61.Va channel_softc 62structures. 63The first one defines global controller properties, and the second contains 64per-channel informations. 65.Nm 66returns informations about the attached devices in the 67.Va ata_drive_datas 68structure. 69.Bd -literal 70struct wdc_softc { /* Per controller state */ 71 struct device sc_dev; 72 int cap; 73#define WDC_CAPABILITY_DATA16 0x0001 74#define WDC_CAPABILITY_DATA32 0x0002 75#define WDC_CAPABILITY_MODE 0x0004 76#define WDC_CAPABILITY_DMA 0x0008 77#define WDC_CAPABILITY_UDMA 0x0010 78#define WDC_CAPABILITY_HWLOCK 0x0020 79#define WDC_CAPABILITY_ATA_NOSTREAM 0x0040 80#define WDC_CAPABILITY_ATAPI_NOSTREAM 0x0080 81#define WDC_CAPABILITY_NO_EXTRA_RESETS 0x0100 82#define WDC_CAPABILITY_PREATA 0x0200 83#define WDC_CAPABILITY_IRQACK 0x0400 84#define WDC_CAPABILITY_SINGLE_DRIVE 0x0800 85#define WDC_CAPABILITY_NOIRQ 0x1000 86#define WDC_CAPABILITY_SELECT 0x2000 87 u_int8_t pio_mode; 88 u_int8_t dma_mode; 89 int nchannels; 90 struct channel_softc *channels; 91 92 void *dma_arg; 93 int (*dma_init)(void *, int, int, void *, size_t, int); 94 void (*dma_start)(void *, int, int, int); 95 int (*dma_finish)(void *, int, int, int); 96#define WDC_DMA_READ 0x01 97#define WDC_DMA_POLL 0x02 98 99 int (*claim_hw)(void *, int); 100 void (*free_hw)(void *); 101}; 102 103struct channel_softc { /* Per channel data */ 104 int channel; 105 struct wdc_softc *wdc; 106 bus_space_tag_t cmd_iot; 107 bus_space_handle_t cmd_ioh; 108 bus_space_tag_t ctl_iot; 109 bus_space_handle_t ctl_ioh; 110 bus_space_tag_t data32iot; 111 bus_space_handle_t data32ioh; 112 int ch_flags; 113#define WDCF_ACTIVE 0x01 114#define WDCF_IRQ_WAIT 0x10 115 u_int8_t ch_status; 116 u_int8_t ch_error; 117 struct ata_drive_datas ch_drive[2]; 118 struct channel_queue *ch_queue; 119}; 120 121struct ata_drive_datas { 122 u_int8_t drive; 123 u_int8_t drive_flags; 124#define DRIVE_ATA 0x01 125#define DRIVE_ATAPI 0x02 126#define DRIVE (DRIVE_ATA|DRIVE_ATAPI) 127#define DRIVE_CAP32 0x04 128#define DRIVE_DMA 0x08 129#define DRIVE_UDMA 0x10 130#define DRIVE_MODE 0x20 131 u_int8_t PIO_mode; 132 u_int8_t DMA_mode; 133 u_int8_t UDMA_mode; 134 u_int8_t state; 135 136 struct device *drv_softc; 137 void* chnl_softc; 138}; 139.Ed 140.Pp 141The bus front-end needs to fill in the following elements of 142.Va wdc_softc : 143.Bl -tag -compact -width "dma_finish" -offset "xxxx" 144.It cap 145supports one or more of the WDC_CAPABILITY flags 146.It nchannels 147number of channels supported by this controller 148.It channels 149array of 150.Va struct channel_softc 151of size 152.Va nchannels 153properly initialised 154.El 155The following elements are optional: 156.Bl -tag -compact -width "dma_finish" -offset "xxxx" 157.It pio_mode 158.It dma_mode 159.It dma_arg 160.It dma_init 161.It dma_start 162.It dma_finish 163.It claim_hw 164.It free_hw 165.El 166.Pp 167The 168.Va WDC_CAPABILITY_DATA16 169and 170.Va WDC_CAPABILITY_DATA32 171flags informs 172.Nm 173whether the controller supports 16- or 32-bit I/O accesses on the data port. 174If both are set, a test will be done for each drive using the ATA or 175ATAPI IDENTIFY command, to automatically select the working mode. 176.Pp 177The 178.Va WDC_CAPABILITY_DMA 179and 180.Va WDC_CAPABILITY_UDMA 181flags are set for controllers supporting the DMA and Ultra-DMA modes. 182The bus front-end needs to provide the 183.Fn dma_init , 184.Fn dma_start 185and 186.Fn dma_finish 187functions. 188.Fn dma_init 189is called just before issuing a DMA command to the IDE device. 190The arguments are, respectively: 191.Va dma_arg , 192the channel number, the drive number on this channel, 193the virtual address of the DMA buffer, the size of the transfer, and the 194.Va WDC_DMA 195flags. 196.Fn dma_start 197is called just after issuing a DMA command to the IDE device. 198The arguments are, respectively: 199.Va dma_arg , 200the channel number, the drive number on this channel, and the 201.Va WDC_DMA 202flags. 203.Fn dma_finish 204is called once the transfer is complete. 205The arguments are, respectively: 206.Va dma_arg , 207the channel number, the drive number on this channel, and the 208.Va WDC_DMA 209flags. 210.Va WDC_DMA_READ 211indicates the direction of the data transfer, and 212.Va WDC_DMA_POLL 213indicates if the transfer will use (or used) interrupts. 214.Pp 215The 216.Va WDC_CAPABILITY_MODE 217flag means that the bus front-end can program the PIO and DMA modes, so 218.Nm 219needs to provide back the supported modes for each drive, and set the drives 220modes. 221The 222.Va pio_mode 223and 224.Va dma_mode 225needs to be set to the highest PIO and DMA mode supported. 226If 227.Va WDC_CAPABILITY_UDMA 228is set, then 229.Va dma_mode 230must be set to the highest Ultra-DMA mode supported. 231If 232.Va WDC_CAPABILITY_MODE 233is not set, 234.Nm 235will not attempt to change the current drive's settings, assuming the host's 236firmware has done it right. 237.Pp 238The 239.Va WDC_CAPABILITY_HWLOCK 240flag is set for controllers needing hardware looking before accessing the 241I/O ports. 242If this flag is set, the bus front-end needs to provide the 243.Fn claim_hw 244and 245.Fn free_hw 246functions. 247.Fn claim_hw 248will be called when the driver wants to access the controller ports. 249The second parameter is set to 1 when it is possible to sleep waiting 250for the lock, 0 otherwise. 251It should return 1 when access has been granted, 0 otherwise. 252When access has not been granted and sleep is not allowed, the bus 253front-end shall call 254.Fn wdcrestart 255with the first argument passed to 256.Fn claim_hw 257as argument. 258This arguments will also be the one passed to 259.Fn free_hw . 260This function is called once the transfer is complete, so that the lock can 261be released. 262.Pp 263Accesses to the data port are done by using the bus_space stream functions, 264unless the 265.Va WDC_CAPABILITY_ATA_NOSTREAM 266or 267.Va WDC_CAPABILITY_ATAPI_NOSTREAM 268flags are set. 269This should not be used, unless the data bus is not wired properly (which 270seems common on big-endian systems), and byte-order needs to be preserved 271for compatibility with the host's firmware. 272Also note that the IDE bus is a little-endian bus, so the bus_space 273functions used for the bus_space tag passed in the 274.Va channel_softc 275have to do the appropriate byte-swapping for big-endian systems. 276.Pp 277.Va WDC_CAPABILITY_NO_EXTRA_RESETS 278avoid the controller reset at the end of the disks probe. 279This reset is needed for some controllers, but causes problems with some 280others. 281.Pp 282.Va WDC_CAPABILITY_NOIRQ 283tells the driver that this controller doesn't have its interrupt lines 284wired up usefully, so it should always use polled transfers. 285.Pp 286The bus front-end needs to fill in the following 287elements of 288.Va channel_softc : 289.Bl -tag -compact -width "dma_finish" -offset "xxxx" 290.It channel 291The channel number on the controller 292.It wdc 293A pointer to the controller's wdc_softc 294.It cmd_iot, cmd_ioh 295Bus-space tag and handle for access to the command block registers (which 296includes the 16-bit data port) 297.It ctl_iot, ctl_ioh 298Bus-space tag and handle for access to the control block registers 299.It ch_queue 300A pointer to a 301.Va struct channel_queue . 302This will hold the queues of outstanding commands for this controller. 303.El 304The following elements are optional: 305.Bl -tag -compact -width "dma_finish" -offset "xxxx" 306.It data32iot, data32ioh 307Bus-space tag and handle for 32-bit data accesses. 308Only needed if 309.Va WDC_CAPABILITY_DATA32 310is set in the controller's 311.Va wdc_softc . 312.El 313.Pp 314.Va ch_queue 315can point to a common 316.Va struct channel_queue 317if the controller doesn't support concurrent access to its different channels. 318If all channels are independent, it is recommended that each channel has 319its own 320.Va ch_queue 321(for better performance). 322.Pp 323The bus-specific front-end can use the 324.Fn wdcprobe 325function, with a properly initialised 326.Va struct channel_softc 327as argument ( 328.Va wdc 329can be set to NULL. 330This allows 331.Fn wdcprobe 332to be easily used in bus front-end probe functions). 333This function will return an integer where bit 0 will be set if the master 334device has been found, and 1 if the slave device has been found. 335.Pp 336The bus-specific attach function has to call 337.Fn wdcattach 338for each channel, with a pointer to a properly initialised 339.Va channel softc 340as argument. 341This will probe devices attached to the IDE channel and attach them. 342Once this function returns, the 343.Va ch_drive 344array of the 345.Va channel_softc 346will contain the drive's capabilities. 347This can be used to properly initialise the controller's mode, or disable a 348channel without drives. 349.Pp 350The elements of interest in 351.Va ata_drive_datas 352for a bus front-end are: 353.Bl -tag -compact -width "dma_finish" -offset "xxxx" 354.It drive 355The drive number 356.It drive_flags 357Flags indicating the drive capabilities. 358A null 359.Va drive_flags 360indicate either that no drive is here, or that no driver was 361found for this device. 362.It PIO_mode, DMA_mode, UDMA_mode 363the highest supported modes for this drive compatible with the controller's 364capabilities. 365Needs to be reset to the mode to use by the drive, if known. 366.It drv_softc 367A pointer to the drive's softc. 368Can be used to print the drive's name. 369.El 370.Pp 371.Va drive_flags 372handles the following flags: 373.Bl -tag -compact -width "dma_finish" -offset "xxxx" 374.It DRIVE_ATA, DRIVE_ATAPI 375Gives the drive type, if any. 376The shortcut DRIVE can be used to just test the presence/absence of a drive. 377.It DRIVE_CAP32 378This drive works with 32-bit data I/O. 379.It DRIVE_DMA 380This drive supports DMA. 381.It DRIVE_UDMA 382This drive supports Ultra-DMA. 383.It DRIVE_MODE 384This drive properly reported its PIO and DMA mode. 385.El 386.Pp 387Once the controller has been initialised, it has to reset the 388.Va DRIVE_DMA 389and 390.Va DRIVE_UDMA , 391as well as the values of 392.Va PIO_mode , 393.Va DMA_mode 394and 395.Va UDMA_mode 396if the modes to be used are not highest ones supported by the drive. 397.Sh SEE ALSO 398.Xr wdc 4 , 399.Xr bus_space 9 400.Sh CODE REFERENCES 401The wdc core functions are implemented in 402.Pa sys/dev/ic/wdc.c . 403Low-level ATA and ATAPI support is provided by 404.Pa sys/dev/ata_wdc.c 405and 406.Pa sys/dev/scsipi/atapi_wdc.c 407respectively. 408.Pp 409An example of a simple bus front-end can be found in 410.Pa sys/dev/isapnp/wdc_isapnp.c . 411A more complex one, with multiple channels and bus-master DMA support is 412.Pa sys/dev/pci/pciide.c . 413.Pa sys/arch/atari/dev/wdc_mb.c 414makes use of hardware locking, and also provides an example of bus-front 415end for a big-endian system, which needs byte-swapping bus_space functions. 416