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