Home | History | Annotate | Line # | Download | only in altboot
pciide.c revision 1.2.2.4
      1  1.2.2.2  rmind /* $NetBSD: pciide.c,v 1.2.2.4 2011/05/31 03:04:16 rmind Exp $ */
      2  1.2.2.2  rmind 
      3  1.2.2.2  rmind /*-
      4  1.2.2.2  rmind  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.2.2.2  rmind  * All rights reserved.
      6  1.2.2.2  rmind  *
      7  1.2.2.2  rmind  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.2.2  rmind  * by Tohru Nishimura.
      9  1.2.2.2  rmind  *
     10  1.2.2.2  rmind  * Redistribution and use in source and binary forms, with or without
     11  1.2.2.2  rmind  * modification, are permitted provided that the following conditions
     12  1.2.2.2  rmind  * are met:
     13  1.2.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.2.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.2.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     18  1.2.2.2  rmind  *
     19  1.2.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2.2.2  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2.2.2  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2.2.2  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2.2.2  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2.2.2  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2.2.2  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2.2.2  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2.2.2  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2.2.2  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2.2.2  rmind  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2.2.2  rmind  */
     31  1.2.2.2  rmind 
     32  1.2.2.2  rmind #include <sys/param.h>
     33  1.2.2.2  rmind 
     34  1.2.2.2  rmind #include <lib/libsa/stand.h>
     35  1.2.2.2  rmind 
     36  1.2.2.2  rmind #include "globals.h"
     37  1.2.2.2  rmind 
     38  1.2.2.3  rmind static int cmdidefix(struct dkdev_ata *);
     39  1.2.2.3  rmind 
     40  1.2.2.2  rmind static uint32_t pciiobase = PCI_XIOBASE;
     41  1.2.2.2  rmind 
     42  1.2.2.2  rmind struct myops {
     43  1.2.2.2  rmind 	int (*chipfix)(struct dkdev_ata *);
     44  1.2.2.2  rmind 	int (*presense)(struct dkdev_ata *, int);
     45  1.2.2.2  rmind };
     46  1.2.2.3  rmind static struct myops defaultops = { NULL, NULL };
     47  1.2.2.2  rmind static struct myops cmdideops = { cmdidefix, NULL };
     48  1.2.2.2  rmind static struct myops *myops;
     49  1.2.2.2  rmind 
     50  1.2.2.2  rmind int
     51  1.2.2.2  rmind pciide_match(unsigned tag, void *data)
     52  1.2.2.2  rmind {
     53  1.2.2.2  rmind 	unsigned v;
     54  1.2.2.2  rmind 
     55  1.2.2.2  rmind 	v = pcicfgread(tag, PCI_ID_REG);
     56  1.2.2.2  rmind 	switch (v) {
     57  1.2.2.2  rmind 	case PCI_DEVICE(0x1095, 0x0680): /* SiI 0680 IDE */
     58  1.2.2.2  rmind 		myops = &cmdideops;
     59  1.2.2.2  rmind 		return 1;
     60  1.2.2.2  rmind 	case PCI_DEVICE(0x1283, 0x8211): /* ITE 8211 IDE */
     61  1.2.2.2  rmind 	case PCI_DEVICE(0x1106, 0x1571): /* VIA 82C586 IDE */
     62  1.2.2.4  rmind 	case PCI_DEVICE(0x1106, 0x3164): /* VIA VT6410 */
     63  1.2.2.2  rmind 	case PCI_DEVICE(0x10ad, 0x0105): /* Symphony Labs 82C105 IDE */
     64  1.2.2.2  rmind 	case PCI_DEVICE(0x10b8, 0x5229): /* ALi IDE */
     65  1.2.2.2  rmind 	case PCI_DEVICE(0x1191, 0x0008): /* ACARD ATP865 */
     66  1.2.2.3  rmind 	case PCI_DEVICE(0x1191, 0x0009): /* ACARD ATP865A */
     67  1.2.2.3  rmind 		myops = &defaultops;
     68  1.2.2.2  rmind 		return 1;
     69  1.2.2.2  rmind 	}
     70  1.2.2.2  rmind 	return 0;
     71  1.2.2.2  rmind }
     72  1.2.2.2  rmind 
     73  1.2.2.2  rmind void *
     74  1.2.2.2  rmind pciide_init(unsigned tag, void *data)
     75  1.2.2.2  rmind {
     76  1.2.2.2  rmind 	int native, n;
     77  1.2.2.2  rmind 	unsigned val;
     78  1.2.2.2  rmind 	struct dkdev_ata *l;
     79  1.2.2.2  rmind 
     80  1.2.2.2  rmind 	l = alloc(sizeof(struct dkdev_ata));
     81  1.2.2.2  rmind 	memset(l, 0, sizeof(struct dkdev_ata));
     82  1.2.2.2  rmind 	l->iobuf = allocaligned(512, 16);
     83  1.2.2.2  rmind 	l->tag = tag;
     84  1.2.2.2  rmind 
     85  1.2.2.2  rmind 	val = pcicfgread(tag, PCI_CLASS_REG);
     86  1.2.2.3  rmind 	native = PCI_CLASS(val) != PCI_CLASS_IDE ||
     87  1.2.2.3  rmind 	    (PCI_INTERFACE(val) & 05) != 0;
     88  1.2.2.2  rmind 	if (native) {
     89  1.2.2.2  rmind 		/* native, use BAR 01234 */
     90  1.2.2.2  rmind 		l->bar[0] = pciiobase + (pcicfgread(tag, 0x10) &~ 01);
     91  1.2.2.2  rmind 		l->bar[1] = pciiobase + (pcicfgread(tag, 0x14) &~ 01);
     92  1.2.2.2  rmind 		l->bar[2] = pciiobase + (pcicfgread(tag, 0x18) &~ 01);
     93  1.2.2.2  rmind 		l->bar[3] = pciiobase + (pcicfgread(tag, 0x1c) &~ 01);
     94  1.2.2.2  rmind 		l->bar[4] = pciiobase + (pcicfgread(tag, 0x20) &~ 01);
     95  1.2.2.2  rmind 		l->chan[0].cmd = l->bar[0];
     96  1.2.2.2  rmind 		l->chan[0].ctl = l->chan[0].alt = l->bar[1] | 02;
     97  1.2.2.2  rmind 		l->chan[0].dma = l->bar[4] + 0x0;
     98  1.2.2.2  rmind 		l->chan[1].cmd = l->bar[2];
     99  1.2.2.2  rmind 		l->chan[1].ctl = l->chan[1].alt = l->bar[3] | 02;
    100  1.2.2.2  rmind 		l->chan[1].dma = l->bar[4] + 0x8;
    101  1.2.2.2  rmind 	}
    102  1.2.2.2  rmind 	else {
    103  1.2.2.2  rmind 		/* legacy */
    104  1.2.2.2  rmind 		l->bar[0]= pciiobase + 0x1f0;
    105  1.2.2.2  rmind 		l->bar[1]= pciiobase + 0x3f4;
    106  1.2.2.2  rmind 		l->bar[2]= pciiobase + 0x170;
    107  1.2.2.2  rmind 		l->bar[3]= pciiobase + 0x374;
    108  1.2.2.2  rmind 		l->chan[0].cmd = l->bar[0];
    109  1.2.2.2  rmind 		l->chan[0].ctl = l->chan[0].alt = l->bar[1] | 02;
    110  1.2.2.2  rmind 		l->chan[0].dma = 0;
    111  1.2.2.2  rmind 		l->chan[1].cmd = l->bar[2];
    112  1.2.2.2  rmind 		l->chan[1].ctl = l->chan[1].alt = l->bar[3] | 02;
    113  1.2.2.2  rmind 		l->chan[1].dma = 0;
    114  1.2.2.2  rmind 	}
    115  1.2.2.2  rmind 
    116  1.2.2.2  rmind 	for (n = 0; n < 2; n++) {
    117  1.2.2.2  rmind 		if (myops->presense && (*myops->presense)(l, n) == 0)
    118  1.2.2.2  rmind 			l->presense[n] = 0; /* found not exist */
    119  1.2.2.2  rmind 		else {
    120  1.2.2.2  rmind 			/* check to see whether soft reset works */
    121  1.2.2.2  rmind 			l->presense[n] = perform_atareset(l, n);
    122  1.2.2.2  rmind 		}
    123  1.2.2.2  rmind 		if (l->presense[n])
    124  1.2.2.2  rmind 			printf("channel %d present\n", n);
    125  1.2.2.2  rmind 	}
    126  1.2.2.2  rmind 
    127  1.2.2.2  rmind 	/* make sure to have PIO0 */
    128  1.2.2.2  rmind 	if (myops->chipfix)
    129  1.2.2.2  rmind 		(*myops->chipfix)(l);
    130  1.2.2.2  rmind 
    131  1.2.2.2  rmind 	return l;
    132  1.2.2.2  rmind }
    133  1.2.2.2  rmind 
    134  1.2.2.2  rmind static int
    135  1.2.2.2  rmind cmdidefix(struct dkdev_ata *l)
    136  1.2.2.2  rmind {
    137  1.2.2.2  rmind 	int v;
    138  1.2.2.2  rmind 
    139  1.2.2.2  rmind 	v = pcicfgread(l->tag, 0x80);
    140  1.2.2.2  rmind 	pcicfgwrite(l->tag, 0x80, (v & ~0xff) | 0x01);
    141  1.2.2.2  rmind 	v = pcicfgread(l->tag, 0x84);
    142  1.2.2.2  rmind 	pcicfgwrite(l->tag, 0x84, (v & ~0xff) | 0x01);
    143  1.2.2.2  rmind 	v = pcicfgread(l->tag, 0xa4);
    144  1.2.2.2  rmind 	pcicfgwrite(l->tag, 0xa4, (v & ~0xffff) | 0x328a);
    145  1.2.2.2  rmind 	v = pcicfgread(l->tag, 0xb4);
    146  1.2.2.2  rmind 	pcicfgwrite(l->tag, 0xb4, (v & ~0xffff) | 0x328a);
    147  1.2.2.2  rmind 
    148  1.2.2.2  rmind 	return 1;
    149  1.2.2.2  rmind }
    150