/*-
* Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
/*
* Device driver for the ISA on-board DMA controller.
*/
/*
* DMA Channel to Address Page Register offset mapping
*
* Offset from IO_DMAPG is stored in this 2D array -- first dimension is
* the DMA controller, second dimension is the DMA channel.
*
* e.g. dmapageport[0][1] gives us the offset for DMA ch 1 on DMA1
*/
static const int dmapageport[2][4] = {
{0x6, 0x2, 0x0, 0x1},
{0xe, 0xa, 0x8, 0x9}
};
static const u_int8_t dmamode[] = {
/* write to device/read from device */
DMA37MD_READ | DMA37MD_SINGLE,
DMA37MD_WRITE | DMA37MD_SINGLE,
/* write to device/read from device */
DMA37MD_READ | DMA37MD_DEMAND,
DMA37MD_WRITE | DMA37MD_DEMAND,
/* write to device/read from device - DMAMODE_LOOP */
DMA37MD_READ | DMA37MD_SINGLE | DMA37MD_LOOP,
DMA37MD_WRITE | DMA37MD_SINGLE | DMA37MD_LOOP,
/* write to device/read from device - DMAMODE_LOOPDEMAND */
DMA37MD_READ | DMA37MD_DEMAND | DMA37MD_LOOP,
DMA37MD_WRITE | DMA37MD_DEMAND | DMA37MD_LOOP,
};
static inline void
_isa_dmaunmask(struct isa_dma_state *ids, int chan)
{
int ochan = chan & 3;
ISA_DMA_MASK_CLR(ids, chan);
/*
* If DMA is frozen, don't unmask it now. It will be
* unmasked when DMA is thawed again.
*/
if (ids->ids_frozen)
return;
/* set dma channel mode, and set dma channel mode */
if ((chan & 4) == 0)
bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
DMA1_SMSK, ochan | DMA37SM_CLEAR);
else
bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
DMA2_SMSK, ochan | DMA37SM_CLEAR);
}
static inline void
_isa_dmamask(struct isa_dma_state *ids, int chan)
{
int ochan = chan & 3;
ISA_DMA_MASK_SET(ids, chan);
/*
* XXX Should we avoid masking the channel if DMA is
* XXX frozen? It seems like what we're doing should
* XXX be safe, and we do need to reset FFC...
*/
/*
* _isa_dmainit(): Initialize the isa_dma_state for this chipset.
*/
void
_isa_dmainit(struct isa_dma_state *ids, bus_space_tag_t bst, bus_dma_tag_t dmat, device_t dev)
{
int chan;
ids->ids_dev = dev;
if (ids->ids_initialized) {
/*
* Some systems may have e.g. `ofisa' (OpenFirmware
* configuration of ISA bus) and a regular `isa'.
* We allow both to call the initialization function,
* and take the device name from the last caller
* (assuming it will be the indirect ISA bus). Since
* `ofisa' and `isa' are the same bus with different
* configuration mechanisms, the space and dma tags
* must be the same!
*/
if (!bus_space_is_equal(ids->ids_bst, bst) ||
ids->ids_dmat != dmat)
panic("_isa_dmainit: inconsistent ISA tags");
} else {
ids->ids_bst = bst;
ids->ids_dmat = dmat;
/*
* Map the registers used by the ISA DMA controller.
*/
if (bus_space_map(ids->ids_bst, IO_DMA1, DMA1_IOSIZE, 0,
&ids->ids_dma1h))
panic("_isa_dmainit: unable to map DMA controller #1");
if (bus_space_map(ids->ids_bst, IO_DMA2, DMA2_IOSIZE, 0,
&ids->ids_dma2h))
panic("_isa_dmainit: unable to map DMA controller #2");
if (bus_space_map(ids->ids_bst, IO_DMAPG, DMAPG_IOSIZE, 0,
&ids->ids_dmapgh))
panic("_isa_dmainit: unable to map DMA page registers");
/*
* All 8 DMA channels start out "masked".
*/
ids->ids_masked = 0xff;
/*
* Initialize the max transfer size for each channel, if
* it is not initialized already (i.e. by a bus-dependent
* front-end).
*/
for (chan = 0; chan < 8; chan++) {
if (ids->ids_maxsize[chan] == 0)
ids->ids_maxsize[chan] =
ISA_DMA_MAXSIZE_DEFAULT(chan);
}
ids->ids_initialized = 1;
/*
* DRQ 4 is used to chain the two 8237s together; make
* sure it's always cascaded, and that it will be unmasked
* when DMA is thawed.
*/
_isa_dmacascade(ids, 4);
}
}
void
_isa_dmadestroy(struct isa_dma_state *ids)
{
if (!ids->ids_initialized)
return;
_isa_dmacascade_stop(ids, 4);
/*
* Unmap the registers used by the ISA DMA controller.
*/
bus_space_unmap(ids->ids_bst, ids->ids_dmapgh, DMAPG_IOSIZE);
bus_space_unmap(ids->ids_bst, ids->ids_dma2h, DMA2_IOSIZE);
bus_space_unmap(ids->ids_bst, ids->ids_dma1h, DMA1_IOSIZE);
ids->ids_initialized = 0;
}
/*
* _isa_dmacascade(): program 8237 DMA controller channel to accept
* external dma control by a board.
*/
int
_isa_dmacascade(struct isa_dma_state *ids, int chan)
{
int ochan = chan & 3;
if (!ISA_DMA_DRQ_ISFREE(ids, chan)) {
printf("%s: DRQ %d is not free\n", device_xname(ids->ids_dev),
chan);
return (EAGAIN);
}
ISA_DMA_DRQ_ALLOC(ids, chan);
/* set dma channel mode, and set dma channel mode */
if ((chan & 4) == 0)
bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
DMA1_MODE, ochan | DMA37MD_CASCADE);
else
bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
DMA2_MODE, ochan | DMA37MD_CASCADE);
_isa_dmaunmask(ids, chan);
return (0);
}
/*
* _isa_dmacascade_stop(): turn off cascading on the 8237 DMA controller channel
* external dma control by a board.
*/
int
_isa_dmacascade_stop(struct isa_dma_state *ids, int chan)
{
if (chan < 0 || chan > 7) {
printf("%s: bogus drq %d\n", device_xname(ids->ids_dev), chan);
return EINVAL;
}
if (ISA_DMA_DRQ_ISFREE(ids, chan))
return 0;
_isa_dmamask(ids, chan);
ISA_DMA_DRQ_FREE(ids, chan);
return 0;
}
int
_isa_drq_alloc(struct isa_dma_state *ids, int chan)
{
if (!ISA_DMA_DRQ_ISFREE(ids, chan))
return EBUSY;
ISA_DMA_DRQ_ALLOC(ids, chan);
return 0;
}
int
_isa_drq_free(struct isa_dma_state *ids, int chan)
{
if (ISA_DMA_DRQ_ISFREE(ids, chan))
return EINVAL;
ISA_DMA_DRQ_FREE(ids, chan);
return 0;
}
bus_size_t
_isa_dmamaxsize(struct isa_dma_state *ids, int chan)
{
/*
* _isa_dmastart(): program 8237 DMA controller channel and set it
* in motion.
*/
int
_isa_dmastart(struct isa_dma_state *ids, int chan, void *addr, bus_size_t nbytes, struct proc *p, int flags, int busdmaflags)
{
bus_dmamap_t dmam;
bus_addr_t dmaaddr;
int waport;
int ochan = chan & 3;
int error;
/*
* We have to shift the byte count by 1. If we're in auto-initialize
* mode, the count may have wrapped around to the initial value. We
* can't use the TC bit to check for this case, so instead we compare
* against the original byte count.
* If we're not in auto-initialize mode, then the count will wrap to
* -1, so we also handle that case.
*/
if ((chan & 4) == 0) {
waport = DMA1_CHN(ochan);
nbytes = bus_space_read_1(ids->ids_bst, ids->ids_dma1h,
waport + 1) + 1;
nbytes += bus_space_read_1(ids->ids_bst, ids->ids_dma1h,
waport + 1) << 8;
nbytes &= 0xffff;
} else {
waport = DMA2_CHN(ochan);
nbytes = bus_space_read_1(ids->ids_bst, ids->ids_dma2h,
waport + 2) + 1;
nbytes += bus_space_read_1(ids->ids_bst, ids->ids_dma2h,
waport + 2) << 8;
nbytes <<= 1;
nbytes &= 0x1ffff;
}
if (nbytes == ids->ids_dmalength[chan])
nbytes = 0;
_isa_dmaunmask(ids, chan);
return (nbytes);
}
int
_isa_dmafinished(struct isa_dma_state *ids, int chan)
{
int
_isa_dmamem_alloc(struct isa_dma_state *ids, int chan, bus_size_t size, bus_addr_t *addrp, int flags)
{
bus_dma_segment_t seg;
int error, boundary, rsegs;