xref: /netbsd-src/sys/arch/sandpoint/stand/altboot/pciide.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /* $NetBSD: pciide.c,v 1.9 2011/11/02 04:10:33 nisimura 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 static int apoidefix(struct dkdev_ata *);
40 
41 static uint32_t pciiobase = PCI_XIOBASE;
42 
43 struct myops {
44 	int (*chipfix)(struct dkdev_ata *);
45 	int (*presense)(struct dkdev_ata *, int);
46 };
47 static struct myops defaultops = { NULL, NULL };
48 static struct myops cmdideops = { cmdidefix, NULL };
49 static struct myops apoideops = { apoidefix, NULL };
50 static struct myops *myops;
51 
52 int
53 pciide_match(unsigned tag, void *data)
54 {
55 	unsigned v;
56 
57 	v = pcicfgread(tag, PCI_ID_REG);
58 	switch (v) {
59 	case PCI_DEVICE(0x1095, 0x0680): /* SiI 0680 IDE */
60 		myops = &cmdideops;
61 		return 1;
62 	case PCI_DEVICE(0x1106, 0x0571): /* VIA 82C586A/B/686A/B IDE */
63 	case PCI_DEVICE(0x1106, 0x1571): /* VIA 82C586 IDE */
64 	case PCI_DEVICE(0x1106, 0x3164): /* VIA VT6410 RAID IDE */
65 		myops = &apoideops;
66 		return 1;
67 	case PCI_DEVICE(0x1283, 0x8211): /* ITE 8211 IDE */
68 	case PCI_DEVICE(0x10ad, 0x0105): /* Symphony Labs 82C105 IDE */
69 	case PCI_DEVICE(0x10b8, 0x5229): /* ALi IDE */
70 	case PCI_DEVICE(0x1191, 0x0008): /* ACARD ATP865 */
71 	case PCI_DEVICE(0x1191, 0x0009): /* ACARD ATP865A */
72 		myops = &defaultops;
73 		return 1;
74 	}
75 	return 0;
76 }
77 
78 void *
79 pciide_init(unsigned tag, void *data)
80 {
81 	int native, n;
82 	unsigned val;
83 	struct dkdev_ata *l;
84 
85 	l = alloc(sizeof(struct dkdev_ata));
86 	memset(l, 0, sizeof(struct dkdev_ata));
87 	l->iobuf = allocaligned(512, 16);
88 	l->tag = tag;
89 
90 	/* chipset specific fixes */
91 	if (myops->chipfix)
92 		if (!(*myops->chipfix)(l))
93 			return NULL;
94 
95 	val = pcicfgread(tag, PCI_CLASS_REG);
96 	native = PCI_CLASS(val) != PCI_CLASS_IDE ||
97 	    (PCI_INTERFACE(val) & 05) != 0;
98 
99 	if (native) {
100 		/* native, use BAR 01234 */
101 		l->bar[0] = pciiobase + (pcicfgread(tag, 0x10) &~ 01);
102 		l->bar[1] = pciiobase + (pcicfgread(tag, 0x14) &~ 01);
103 		l->bar[2] = pciiobase + (pcicfgread(tag, 0x18) &~ 01);
104 		l->bar[3] = pciiobase + (pcicfgread(tag, 0x1c) &~ 01);
105 		l->bar[4] = pciiobase + (pcicfgread(tag, 0x20) &~ 01);
106 		l->chan[0].cmd = l->bar[0];
107 		l->chan[0].ctl = l->chan[0].alt = l->bar[1] | 02;
108 		l->chan[0].dma = l->bar[4] + 0x0;
109 		l->chan[1].cmd = l->bar[2];
110 		l->chan[1].ctl = l->chan[1].alt = l->bar[3] | 02;
111 		l->chan[1].dma = l->bar[4] + 0x8;
112 	}
113 	else {
114 		/* legacy */
115 		l->bar[0]= pciiobase + 0x1f0;
116 		l->bar[1]= pciiobase + 0x3f4;
117 		l->bar[2]= pciiobase + 0x170;
118 		l->bar[3]= pciiobase + 0x374;
119 		l->chan[0].cmd = l->bar[0];
120 		l->chan[0].ctl = l->chan[0].alt = l->bar[1] | 02;
121 		l->chan[0].dma = 0;
122 		l->chan[1].cmd = l->bar[2];
123 		l->chan[1].ctl = l->chan[1].alt = l->bar[3] | 02;
124 		l->chan[1].dma = 0;
125 	}
126 
127 	for (n = 0; n < 2; n++) {
128 		if (myops->presense && (*myops->presense)(l, n) == 0)
129 			l->presense[n] = 0; /* found not exist */
130 		else {
131 			/* check to see whether soft reset works */
132 			l->presense[n] = perform_atareset(l, n);
133 		}
134 		if (l->presense[n])
135 			printf("channel %d present\n", n);
136 	}
137 
138 	return l;
139 }
140 
141 static int
142 cmdidefix(struct dkdev_ata *l)
143 {
144 	unsigned v;
145 
146 	v = pcicfgread(l->tag, 0x80);
147 	pcicfgwrite(l->tag, 0x80, (v & ~0xff) | 0x01);
148 	v = pcicfgread(l->tag, 0x84);
149 	pcicfgwrite(l->tag, 0x84, (v & ~0xff) | 0x01);
150 	v = pcicfgread(l->tag, 0xa4);
151 	pcicfgwrite(l->tag, 0xa4, (v & ~0xffff) | 0x328a);
152 	v = pcicfgread(l->tag, 0xb4);
153 	pcicfgwrite(l->tag, 0xb4, (v & ~0xffff) | 0x328a);
154 
155 	return 1;
156 }
157 
158 static int
159 apoidefix(struct dkdev_ata *l)
160 {
161 	unsigned v;
162 
163 	/* enable primary and secondary channel */
164 	v = pcicfgread(l->tag, 0x40) & ~0x03;
165 	pcicfgwrite(l->tag, 0x40, v | 0x03);
166 
167 	return 1;
168 }
169