/* $NetBSD: mal.c,v 1.5 2023/06/19 08:40:30 msaitoh Exp $ */
/*
* Copyright (c) 2010 KIYOHARA Takashi
* All rights reserved.
*
* 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.
*/
static int mal_txeob_intr(void *);
static int mal_rxeob_intr(void *);
static int mal_txde_intr(void *);
static int mal_rxde_intr(void *);
static int mal_serr_intr(void *);
/* Max channel is 4 on 440GX. Others is 2 or 1. */
static void *iargs[4];
void
mal_attach(int pvr)
{
int i, to;
for (i = 0; i < __arraycount(maltbl); i++)
if (maltbl[i].pvr == pvr)
break;
if (i == __arraycount(maltbl)) {
aprint_error("%s: unknown pvr 0x%x\n", __func__, pvr);
return;
}
/*
* Reset MAL.
* We wait for the completion of reset in maximums for five seconds.
*/
mtdcr(DCR_MAL0_CFG, MAL0_CFG_SR);
to = 0;
while (mfdcr(DCR_MAL0_CFG) & MAL0_CFG_SR) {
if (to > 5000) {
aprint_error("%s: Soft reset failed\n", __func__);
return;
}
delay(1000); /* delay 1m sec */
to++;
}
static int
mal_serr_intr(void *arg)
{
uint32_t esr;
esr = mfdcr(DCR_MAL0_ESR);
/* not yet... */
aprint_error("MAL SERR: ESR 0x%08x\n", esr);
/* Clear the interrupt status bits. */
mtdcr(DCR_MAL0_ESR, esr);
return 1;
}
void
mal_intr_establish(int chan, void *arg)
{
if (chan >= __arraycount(iargs))
panic("MAL channel %d not support (max %d)\n",
chan, __arraycount(iargs));
iargs[chan] = arg;
}
int
mal_start(int chan, uint32_t cdtxaddr, uint32_t cdrxaddr)
{
/*
* Give the transmit and receive rings to the MAL.
* And set the receive channel buffer size (in units of 16 bytes).
*/
#if MCLBYTES > (4096 - 16) /* XXX! */
# error MCLBYTES > max rx channel buffer size
#endif
/* The mtdcr() allows only the constant in the first argument... */
switch (chan) {
case 0:
mtdcr(DCR_MAL0_TXCTP0R, cdtxaddr);
mtdcr(DCR_MAL0_RXCTP0R, cdrxaddr);
mtdcr(DCR_MAL0_RCBS0, MCLBYTES / 16);
break;
/* Enable the transmit and receive channel on the MAL. */
mtdcr(DCR_MAL0_RXCASR, MAL0__XCAR_CHAN(chan));
mtdcr(DCR_MAL0_TXCASR, MAL0__XCAR_CHAN(chan));
return 0;
}
void
mal_stop(int chan)
{
/* Disable the receive and transmit channels. */
mtdcr(DCR_MAL0_RXCARR, MAL0__XCAR_CHAN(chan));
mtdcr(DCR_MAL0_TXCARR, MAL0__XCAR_CHAN(chan));
}