siisata.c revision 1.6 1 1.6 phx /* $NetBSD: siisata.c,v 1.6 2015/09/30 14:14:32 phx Exp $ */
2 1.1 nisimura
3 1.1 nisimura /*-
4 1.1 nisimura * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 nisimura * All rights reserved.
6 1.1 nisimura *
7 1.1 nisimura * This code is derived from software contributed to The NetBSD Foundation
8 1.1 nisimura * by Tohru Nishimura.
9 1.1 nisimura *
10 1.1 nisimura * Redistribution and use in source and binary forms, with or without
11 1.1 nisimura * modification, are permitted provided that the following conditions
12 1.1 nisimura * are met:
13 1.1 nisimura * 1. Redistributions of source code must retain the above copyright
14 1.1 nisimura * notice, this list of conditions and the following disclaimer.
15 1.1 nisimura * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 nisimura * notice, this list of conditions and the following disclaimer in the
17 1.1 nisimura * documentation and/or other materials provided with the distribution.
18 1.1 nisimura *
19 1.1 nisimura * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 nisimura * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 nisimura * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 nisimura * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 nisimura * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 nisimura * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 nisimura * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 nisimura * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 nisimura * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 nisimura * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 nisimura * POSSIBILITY OF SUCH DAMAGE.
30 1.1 nisimura */
31 1.1 nisimura
32 1.1 nisimura #include <sys/param.h>
33 1.1 nisimura
34 1.1 nisimura #include <lib/libsa/stand.h>
35 1.1 nisimura
36 1.1 nisimura #include "globals.h"
37 1.1 nisimura
38 1.5 phx /*
39 1.5 phx * - no vtophys() translation, vaddr_t == paddr_t.
40 1.5 phx */
41 1.5 phx #define CSR_READ_4(r) in32rb(r)
42 1.5 phx #define CSR_WRITE_4(r,v) out32rb(r,v)
43 1.5 phx
44 1.5 phx static int satapresense(struct dkdev_ata *, int);
45 1.5 phx
46 1.1 nisimura static uint32_t pciiobase = PCI_XIOBASE;
47 1.1 nisimura
48 1.6 phx int sata_delay[4] = { 3, 3, 3, 3 }; /* drive power-up delay per channel */
49 1.6 phx
50 1.1 nisimura int
51 1.1 nisimura siisata_match(unsigned tag, void *data)
52 1.1 nisimura {
53 1.1 nisimura unsigned v;
54 1.1 nisimura
55 1.1 nisimura v = pcicfgread(tag, PCI_ID_REG);
56 1.1 nisimura switch (v) {
57 1.1 nisimura case PCI_DEVICE(0x1095, 0x3112): /* SiI 3112 SATALink */
58 1.1 nisimura case PCI_DEVICE(0x1095, 0x3512): /* 3512 SATALink */
59 1.1 nisimura case PCI_DEVICE(0x1095, 0x3114): /* SiI 3114 SATALink */
60 1.1 nisimura return 1;
61 1.1 nisimura }
62 1.1 nisimura return 0;
63 1.1 nisimura }
64 1.1 nisimura
65 1.1 nisimura void *
66 1.1 nisimura siisata_init(unsigned tag, void *data)
67 1.1 nisimura {
68 1.1 nisimura unsigned idreg;
69 1.5 phx int n, nchan, retries/*waitforspinup*/;
70 1.1 nisimura struct dkdev_ata *l;
71 1.1 nisimura
72 1.1 nisimura l = alloc(sizeof(struct dkdev_ata));
73 1.1 nisimura memset(l, 0, sizeof(struct dkdev_ata));
74 1.1 nisimura l->iobuf = allocaligned(512, 16);
75 1.1 nisimura l->tag = tag;
76 1.1 nisimura
77 1.1 nisimura idreg = pcicfgread(tag, PCI_ID_REG);
78 1.1 nisimura l->bar[0] = pciiobase + (pcicfgread(tag, 0x10) &~ 01);
79 1.1 nisimura l->bar[1] = pciiobase + (pcicfgread(tag, 0x14) &~ 01);
80 1.1 nisimura l->bar[2] = pciiobase + (pcicfgread(tag, 0x18) &~ 01);
81 1.1 nisimura l->bar[3] = pciiobase + (pcicfgread(tag, 0x1c) &~ 01);
82 1.1 nisimura l->bar[4] = pciiobase + (pcicfgread(tag, 0x20) &~ 01);
83 1.1 nisimura l->bar[5] = pcicfgread(tag, 0x24) &~ 0x3ff;
84 1.1 nisimura
85 1.1 nisimura if ((PCI_PRODUCT(idreg) & 0xf) == 0x2) {
86 1.1 nisimura /* 3112/3512 */
87 1.1 nisimura l->chan[0].cmd = l->bar[0];
88 1.1 nisimura l->chan[0].ctl = l->chan[0].alt = l->bar[1] | 02;
89 1.1 nisimura l->chan[0].dma = l->bar[4] + 0x0;
90 1.1 nisimura l->chan[1].cmd = l->bar[2];
91 1.1 nisimura l->chan[1].ctl = l->chan[1].alt = l->bar[3] | 02;
92 1.1 nisimura l->chan[1].dma = l->bar[4] + 0x8;
93 1.1 nisimura nchan = 2;
94 1.1 nisimura }
95 1.1 nisimura else {
96 1.1 nisimura /* 3114 - assume BA5 access is possible XXX */
97 1.1 nisimura l->chan[0].cmd = l->bar[5] + 0x080;
98 1.1 nisimura l->chan[0].ctl = l->chan[0].alt = (l->bar[5] + 0x088) | 02;
99 1.1 nisimura l->chan[1].cmd = l->bar[5] + 0x0c0;
100 1.1 nisimura l->chan[1].ctl = l->chan[1].alt = (l->bar[5] + 0x0c8) | 02;
101 1.1 nisimura l->chan[2].cmd = l->bar[5] + 0x280;
102 1.1 nisimura l->chan[2].ctl = l->chan[2].alt = (l->bar[5] + 0x288) | 02;
103 1.1 nisimura l->chan[3].cmd = l->bar[5] + 0x2c0;
104 1.1 nisimura l->chan[3].ctl = l->chan[3].alt = (l->bar[5] + 0x2c8) | 02;
105 1.1 nisimura nchan = 4;
106 1.1 nisimura }
107 1.1 nisimura
108 1.1 nisimura /* configure PIO transfer mode */
109 1.1 nisimura pcicfgwrite(tag, 0x80, 0x00);
110 1.1 nisimura pcicfgwrite(tag, 0x84, 0x00);
111 1.1 nisimura
112 1.5 phx for (n = 0; n < nchan; n++) {
113 1.5 phx l->presense[n] = satapresense(l, n);
114 1.5 phx if (l->presense[n] == 0) {
115 1.6 phx /* wait some seconds to power-up the drive */
116 1.6 phx for (retries = 0; retries < sata_delay[n]; retries++) {
117 1.6 phx wakeup_drive(l, n);
118 1.6 phx printf("Waiting %2d seconds for powering up "
119 1.6 phx "port %d.\r", sata_delay[n] - retries, n);
120 1.6 phx delay(1000 * 1000);
121 1.6 phx if ((l->presense[n] = satapresense(l, n)) != 0)
122 1.6 phx break;
123 1.6 phx }
124 1.6 phx putchar('\n');
125 1.6 phx if (l->presense[n] == 0) {
126 1.6 phx DPRINTF(("port %d not present\n", n));
127 1.6 phx continue;
128 1.6 phx }
129 1.5 phx }
130 1.5 phx if (atachkpwr(l, n) != ATA_PWR_ACTIVE) {
131 1.5 phx /* drive is probably sleeping, wake it up */
132 1.6 phx for (retries = 0; retries < 20; retries++) {
133 1.5 phx wakeup_drive(l, n);
134 1.5 phx DPRINTF(("port %d spinning up...\n", n));
135 1.5 phx delay(1000 * 1000);
136 1.5 phx l->presense[n] = perform_atareset(l, n);
137 1.5 phx if (atachkpwr(l, n) == ATA_PWR_ACTIVE)
138 1.5 phx break;
139 1.5 phx }
140 1.5 phx } else {
141 1.5 phx /* check to see whether soft reset works */
142 1.5 phx DPRINTF(("port %d active\n", n));
143 1.6 phx for (retries = 0; retries < 20; retries++) {
144 1.5 phx l->presense[n] = perform_atareset(l, n);
145 1.5 phx if (l->presense[n] != 0)
146 1.4 phx break;
147 1.6 phx wakeup_drive(l, n);
148 1.5 phx DPRINTF(("port %d cold-starting...\n", n));
149 1.5 phx delay(1000 * 1000);
150 1.1 nisimura }
151 1.4 phx }
152 1.5 phx
153 1.5 phx if (l->presense[n])
154 1.5 phx printf("port %d present\n", n);
155 1.1 nisimura }
156 1.1 nisimura return l;
157 1.1 nisimura }
158 1.5 phx
159 1.5 phx static int
160 1.5 phx satapresense(struct dkdev_ata *l, int n)
161 1.5 phx {
162 1.5 phx #define VND_CH(n) (((n&02)<<8)+((n&01)<<7))
163 1.5 phx #define VND_SC(n) (0x100+VND_CH(n))
164 1.5 phx #define VND_SS(n) (0x104+VND_CH(n))
165 1.5 phx
166 1.5 phx uint32_t sc = l->bar[5] + VND_SC(n);
167 1.5 phx uint32_t ss = l->bar[5] + VND_SS(n);
168 1.5 phx unsigned val;
169 1.5 phx
170 1.5 phx val = (00 << 4) | (03 << 8); /* any speed, no pwrmgt */
171 1.5 phx CSR_WRITE_4(sc, val | 01); /* perform init */
172 1.5 phx delay(50 * 1000);
173 1.5 phx CSR_WRITE_4(sc, val);
174 1.5 phx delay(50 * 1000);
175 1.5 phx val = CSR_READ_4(ss); /* has completed */
176 1.5 phx return ((val & 03) == 03); /* active drive found */
177 1.5 phx }
178