199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2004 Hidetoshi Shimokawa <simokawa@FreeBSD.ORG>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FireWire disk device handling.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/bootinfo.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdarg.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <bootstrap.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <btxv86.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <libi386.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "fwohci.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <dev/dcons/dcons.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* XXX */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define BIT4x2(x,y) uint8_t y:4, x:4
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define BIT16x2(x,y) uint32_t y:16, x:16
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define _KERNEL
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <dev/firewire/iec13213.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern uint32_t dcons_paddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern struct console dconsole;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct crom_src_buf {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_src src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_chunk root;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_chunk vendor;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_chunk hw;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* for dcons */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_chunk unit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_chunk spec;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_chunk ver;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fw_init(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fw_strategy(void *devdata, int flag, daddr_t dblk,
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size, char *buf, size_t *rsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fw_open(struct open_file *f, ...);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fw_close(struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void fw_print(int verbose);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void fw_cleanup(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid fw_enable(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct devsw fwohci = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "FW1394", /* 7 chars at most */
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEVT_NET,
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_init,
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_strategy,
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_open,
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_close,
199767f8919635c4928607450d9e0abb932109ceToomas Soome noioctl,
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_print,
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_cleanup
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct fwohci_softc fwinfo[MAX_OHCI];
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int fw_initialized = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_probe(int index, struct fwohci_softc *sc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->state = FWOHCI_STATE_INIT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome err = biospci_find_devclass(
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0x0c0010 /* Serial:FireWire:OHCI */,
199767f8919635c4928607450d9e0abb932109ceToomas Soome index /* index */,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &sc->locator);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (err != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->state = FWOHCI_STATE_DEAD;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome biospci_write_config(sc->locator,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0x4 /* command */,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 0x6 /* enable bus master and memory mapped I/O */,
199767f8919635c4928607450d9e0abb932109ceToomas Soome 1 /* word */);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome biospci_read_config(sc->locator, 0x00 /*devid*/, 2 /*dword*/,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &sc->devid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome biospci_read_config(sc->locator, 0x10 /*base_addr*/, 2 /*dword*/,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &sc->base_addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->handle = (uint32_t)PTOV(sc->base_addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->bus_id = OREAD(sc, OHCI_BUS_ID);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_init(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i, avail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct fwohci_softc *sc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fw_initialized)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome avail = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < MAX_OHCI; i ++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc = &fwinfo[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_probe(i, sc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sc->state == FWOHCI_STATE_DEAD)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome avail ++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_initialized = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Print information about OHCI chips
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_print(int verbose)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct fwohci_softc *sc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < MAX_OHCI; i ++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc = &fwinfo[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sc->state == FWOHCI_STATE_DEAD)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%d: locator=0x%04x devid=0x%08x"
199767f8919635c4928607450d9e0abb932109ceToomas Soome " base_addr=0x%08x handle=0x%08x bus_id=0x%08x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome i, sc->locator, sc->devid,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->base_addr, sc->handle, sc->bus_id);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_open(struct open_file *f, ...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_list ap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct i386_devdesc *dev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct open_disk *od;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_start(ap, f);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev = va_arg(ap, struct i386_devdesc *);
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_end(ap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENXIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_close(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_cleanup()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct dcons_buf *db;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* invalidate dcons buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dcons_paddr) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome db = (struct dcons_buf *)PTOV(dcons_paddr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome db->magic = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_init_crom(struct fwohci_softc *sc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_src *src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("fw_init_crom\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->crom_src_buf = (struct crom_src_buf *)
199767f8919635c4928607450d9e0abb932109ceToomas Soome malloc(sizeof(struct crom_src_buf));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sc->crom_src_buf == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome src = &sc->crom_src_buf->src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(src, sizeof(struct crom_src));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* BUS info sample */
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->hdr.info_len = 4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.bus_name = CSR_BUS_NAME_IEEE1394;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.irmc = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.cmc = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.isc = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.bmc = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.pmc = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.cyc_clk_acc = 100;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.max_rec = sc->maxrec;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.max_rom = MAXROM_4;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define FW_GENERATION_CHANGEABLE 2
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.generation = FW_GENERATION_CHANGEABLE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.link_spd = sc->speed;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.eui64.hi = sc->eui.hi;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.eui64.lo = sc->eui.lo;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INIT(&src->chunk_list);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->crom_src = src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->crom_root = &sc->crom_src_buf->root;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_reset_crom(struct fwohci_softc *sc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_src_buf *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_src *src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_chunk *root;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("fw_reset\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sc->crom_src_buf == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_init_crom(sc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = sc->crom_src_buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src = sc->crom_src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome root = sc->crom_root;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INIT(&src->chunk_list);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(root, sizeof(struct crom_chunk));
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_chunk(src, NULL, root, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_entry(root, CSRKEY_NCAP, 0x0083c0); /* XXX */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* private company_id */
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_entry(root, CSRKEY_VENDOR, CSRVAL_VENDOR_PRIVATE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef __DragonFly__
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_simple_text(src, root, &buf->vendor, "DragonFly Project");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_simple_text(src, root, &buf->vendor, "FreeBSD Project");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ADDR_HI(x) (((x) >> 24) & 0xffffff)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ADDR_LO(x) ((x) & 0xffffff)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomedcons_crom(struct fwohci_softc *sc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_src_buf *buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_src *src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_chunk *root;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf = sc->crom_src_buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome src = sc->crom_src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome root = sc->crom_root;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(&buf->unit, sizeof(struct crom_chunk));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_chunk(src, root, &buf->unit, CROM_UDIR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_entry(&buf->unit, CSRKEY_SPEC, CSRVAL_VENDOR_PRIVATE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_simple_text(src, &buf->unit, &buf->spec, "FreeBSD");
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_entry(&buf->unit, CSRKEY_VER, DCONS_CSR_VAL_VER);
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_simple_text(src, &buf->unit, &buf->ver, "dcons");
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_entry(&buf->unit, DCONS_CSR_KEY_HI, ADDR_HI(dcons_paddr));
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_add_entry(&buf->unit, DCONS_CSR_KEY_LO, ADDR_LO(dcons_paddr));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_crom(struct fwohci_softc *sc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct crom_src *src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome void *newrom;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_reset_crom(sc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dcons_crom(sc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome newrom = malloc(CROMSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome src = &sc->crom_src_buf->src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_load(src, (uint32_t *)newrom, CROMSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bcmp(newrom, sc->config_rom, CROMSIZE) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Bump generation and reload. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.generation++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Handle generation count wraps. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (src->businfo.generation < 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome src->businfo.generation = 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Recalculate CRC to account for generation change. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome crom_load(src, (uint32_t *)newrom, CROMSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(newrom, (void *)sc->config_rom, CROMSIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(newrom);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_busreset(struct fwohci_softc *sc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sc->state < FWOHCI_STATE_ENABLED) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("fwohci not enabled\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_crom(sc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fwohci_ibr(sc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome count = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (sc->state< FWOHCI_STATE_NORMAL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fwohci_poll(sc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome count ++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (count > 1000) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("give up to wait bus initialize\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("poll count = %d\n", count);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_enable(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct fwohci_softc *sc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fw_initialized == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_init();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < MAX_OHCI; i ++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc = &fwinfo[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sc->state != FWOHCI_STATE_INIT)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->config_rom = (uint32_t *)
199767f8919635c4928607450d9e0abb932109ceToomas Soome (((uint32_t)sc->config_rom_buf
199767f8919635c4928607450d9e0abb932109ceToomas Soome + (CROMSIZE - 1)) & ~(CROMSIZE - 1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("configrom: %08p %08p\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->config_rom_buf, sc->config_rom);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fwohci_init(sc, 0) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->state = FWOHCI_STATE_ENABLED;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_busreset(sc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc->state = FWOHCI_STATE_DEAD;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_poll(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct fwohci_softc *sc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fw_initialized == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < MAX_OHCI; i ++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc = &fwinfo[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sc->state < FWOHCI_STATE_ENABLED)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fwohci_poll(sc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if 0 /* for debug */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_busreset_cmd(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct fwohci_softc *sc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < MAX_OHCI; i ++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sc = &fwinfo[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sc->state < FWOHCI_STATE_INIT)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_busreset(sc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_poll_cmd(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_poll();
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomefw_enable_cmd(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_print(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_enable();
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomedcons_enable(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome dconsole.c_init(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome fw_enable();
199767f8919635c4928607450d9e0abb932109ceToomas Soome dconsole.c_flags |= C_ACTIVEIN | C_ACTIVEOUT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomedcons_read(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (dconsole.c_ready()) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = dconsole.c_in();
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%c", c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\r\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomedcons_write(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len, i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc < 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(argv[1]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < len; i ++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome dconsole.c_out(argv[1][i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome dconsole.c_out('\r');
199767f8919635c4928607450d9e0abb932109ceToomas Soome dconsole.c_out('\n');
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(firewire, "firewire", "enable firewire", fw_enable_cmd);
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(fwbusreset, "fwbusreset", "firewire busreset", fw_busreset_cmd);
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(fwpoll, "fwpoll", "firewire poll", fw_poll_cmd);
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(dcons, "dcons", "enable dcons", dcons_enable);
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(dread, "dread", "read from dcons", dcons_read);
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(dwrite, "dwrite", "write to dcons", dcons_write);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif