/* $NetBSD: aubus.c,v 1.27 2022/09/29 07:00:46 skrll Exp $ */

/*-
* Copyright (c) 2006 Itronix Inc.
* All rights reserved.
*
* Portions written by Garrett D'Amore for Itronix Inc.
*
* 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.
* 3. The name of Itronix Inc. may not be used to endorse
*    or promote products derived from this software without specific
*    prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``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 ITRONIX INC. 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.
*/

/*
* Copyright 2001 Wasabi Systems, Inc.
* All rights reserved.
*
* Written by Simon Burge for Wasabi Systems, Inc.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*      This product includes software developed for the NetBSD Project by
*      Wasabi Systems, Inc.
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
*    or promote products derived from this software without specific prior
*    written permission.
*
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
* 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.
*/

/*
* Copyright (c) 1996 Christopher G. Demetriou.  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.
* 3. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*      This product includes software developed by Christopher G. Demetriou
*      for the NetBSD Project.
* 4. The name of the author may not be used to endorse or promote products
*    derived from this software without specific prior written permission
*
* 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.
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: aubus.c,v 1.27 2022/09/29 07:00:46 skrll Exp $");

#include "locators.h"
#define _MIPS_BUS_DMA_PRIVATE

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/extent.h>
#include <sys/systm.h>

#include <mips/locore.h>
#include <mips/alchemy/include/aureg.h>
#include <mips/alchemy/include/auvar.h>
#include <mips/alchemy/include/aubusvar.h>

static int      aubus_match(device_t, cfdata_t, void *);
static void     aubus_attach(device_t, device_t, void *);
static int      aubus_print(void *, const char *);

CFATTACH_DECL_NEW(aubus, 0,
   aubus_match, aubus_attach, NULL, NULL);

bus_space_tag_t aubus_st;               /* XXX */
struct mips_bus_dma_tag aubus_mdt = {
       ._dmamap_ops = _BUS_DMAMAP_OPS_INITIALIZER,
       ._dmamem_ops = _BUS_DMAMEM_OPS_INITIALIZER,
       ._dmatag_ops = _BUS_DMATAG_OPS_INITIALIZER,
};

/*
* Probe for the aubus; always succeeds.
*/
static int
aubus_match(device_t parent, cfdata_t match, void *aux)
{

       return 1;
}

/*
* Attach the aubus.
*/
static void
aubus_attach(device_t parent, device_t self, void *aux)
{
       struct aubus_attach_args aa;
       device_t sc = self;
       struct au_chipdep *chip;
       const struct au_dev *ad;
       int locs[AUBUSCF_NLOCS];

       printf("\n");

       chip = au_chipdep();
       KASSERT(chip != NULL);

       aubus_mdt._cookie = sc;

       for (ad = chip->devices; ad->name != NULL; ad++) {
               aa.aa_name = ad->name;
               aa.aa_st = aubus_st;
               aa.aa_dt = &aubus_mdt;
               aa.aa_addrs[0] = ad->addr[0];
               aa.aa_addrs[1] = ad->addr[1];
               aa.aa_addrs[2] = ad->addr[2];
               aa.aa_irq[0] = ad->irq[0];
               aa.aa_irq[1] = ad->irq[1];

               locs[AUBUSCF_ADDR] = ad->addr[0];

               config_found(self, &aa, aubus_print,
                   CFARGS(.submatch = config_stdsubmatch,
                          .locators = locs));
       }
}

static int
aubus_print(void *aux, const char *pnp)
{
       struct aubus_attach_args *aa = aux;

       if (pnp)
               aprint_normal("%s at %s", aa->aa_name, pnp);

       if (aa->aa_addr != AUBUSCF_ADDR_DEFAULT)
               aprint_normal(" addr 0x%"PRIxBUSADDR, aa->aa_addr);
       if (aa->aa_irq[0] >= 0)
               aprint_normal(" irq %d", aa->aa_irq[0]);
       if (aa->aa_irq[1] >= 0)
               aprint_normal(",%d", aa->aa_irq[1]);
       return (UNCONF);
}

#if 0
void
aubus_alloc_dma_tag(device_t sc, bus_dma_tag_t pdt)
{
       bus_dma_tag_t   t;

       t = pdt;
       t->_cookie = sc;
       t->_wbase = 0;  /* XXX */
       t->_physbase = 0;       /* XXX */
       t->_wsize = MIPS_KSEG1_START - MIPS_KSEG0_START;
       t->_dmamap_create = _bus_dmamap_create;
       t->_dmamap_destroy = _bus_dmamap_destroy;
       t->_dmamap_load = _bus_dmamap_load;
       t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf;
       t->_dmamap_load_uio = _bus_dmamap_load_uio;
       t->_dmamap_load_raw = _bus_dmamap_load_raw;
       t->_dmamap_unload = _bus_dmamap_unload;
       t->_dmamap_sync = _bus_dmamap_sync;
       t->_dmamem_alloc = _bus_dmamem_alloc;
       t->_dmamem_free = _bus_dmamem_free;
       t->_dmamem_map = _bus_dmamem_map;
       t->_dmamem_unmap = _bus_dmamem_unmap;
       t->_dmamem_mmap = _bus_dmamem_mmap;
}
#endif