satapmp_subr.c revision 1.12.24.4 1 /* $NetBSD: satapmp_subr.c,v 1.12.24.4 2017/06/20 20:58:22 jdolecek Exp $ */
2
3 /*
4 * Copyright (c) 2012 Manuel Bouyer. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: satapmp_subr.c,v 1.12.24.4 2017/06/20 20:58:22 jdolecek Exp $");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/device.h>
34 #include <sys/conf.h>
35 #include <sys/fcntl.h>
36 #include <sys/proc.h>
37 #include <sys/errno.h>
38 #include <sys/kmem.h>
39 #include <sys/intr.h>
40
41 #include <dev/ata/ataconf.h>
42 #include <dev/ata/atareg.h>
43 #include <dev/ata/atavar.h>
44
45 #include <dev/ata/satapmpvar.h>
46 #include <dev/ata/satapmpreg.h>
47 #include <dev/ata/satavar.h>
48 #include <dev/ata/satareg.h>
49
50 static int
51 satapmp_read_8(struct ata_channel *chp, int port, int reg, uint64_t *value)
52 {
53 struct ata_xfer *xfer;
54 struct atac_softc *atac = chp->ch_atac;
55 struct ata_drive_datas *drvp;
56 int error = 0;
57
58 KASSERT(port < PMP_MAX_DRIVES);
59 KASSERT(reg < PMP_GSCR_NREGS);
60 KASSERT(chp->ch_ndrives >= PMP_MAX_DRIVES);
61 drvp = &chp->ch_drive[PMP_PORT_CTL];
62 KASSERT(drvp->drive == PMP_PORT_CTL);
63
64 xfer = ata_get_xfer(chp);
65 if (xfer == NULL)
66 return EINTR;
67
68 xfer->c_ata_c.r_command = PMPC_READ_PORT;
69 xfer->c_ata_c.r_features = reg;
70 xfer->c_ata_c.r_device = port;
71 xfer->c_ata_c.timeout = 3000; /* 3s */
72 xfer->c_ata_c.r_st_bmask = 0;
73 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
74 xfer->c_ata_c.flags = AT_LBA48 | AT_READREG | AT_WAIT;
75
76 if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
77 xfer) != ATACMD_COMPLETE) {
78 aprint_error_dev(chp->atabus,
79 "PMP port %d register %d read failed\n", port, reg);
80 error = EIO;
81 goto out;
82 }
83 if (xfer->c_ata_c.flags & (AT_TIMEOU | AT_DF)) {
84 aprint_error_dev(chp->atabus,
85 "PMP port %d register %d read failed, flags 0x%x\n",
86 port, reg, xfer->c_ata_c.flags);
87 error = EIO;
88 goto out;
89 }
90 if (xfer->c_ata_c.flags & AT_ERROR) {
91 aprint_verbose_dev(chp->atabus,
92 "PMP port %d register %d read failed, error 0x%x\n",
93 port, reg, xfer->c_ata_c.r_error);
94 error = EIO;
95 goto out;
96 }
97
98 *value = ((uint64_t)((xfer->c_ata_c.r_lba >> 24) & 0xffffff) << 40) |
99 ((uint64_t)((xfer->c_ata_c.r_count >> 8) & 0xff) << 32) |
100 ((uint64_t)((xfer->c_ata_c.r_lba >> 0) & 0xffffff) << 8) |
101 ((uint64_t)((xfer->c_ata_c.r_count >> 0) & 0xff) << 0);
102
103 out:
104 ata_free_xfer(chp, xfer);
105 return error;
106 }
107
108 static inline int
109 satapmp_read(struct ata_channel *chp, int port, int reg, uint32_t *value)
110 {
111 uint64_t value64;
112 int ret;
113
114 ret = satapmp_read_8(chp, port, reg, &value64);
115 if (ret)
116 return ret;
117
118 *value = value64 & 0xffffffff;
119 return ret;
120 }
121
122 static int
123 satapmp_write_8(struct ata_channel *chp, int port, int reg, uint64_t value)
124 {
125 struct ata_xfer *xfer;
126 struct atac_softc *atac = chp->ch_atac;
127 struct ata_drive_datas *drvp;
128 int error = 0;
129
130 KASSERT(port < PMP_MAX_DRIVES);
131 KASSERT(reg < PMP_GSCR_NREGS);
132 KASSERT(chp->ch_ndrives >= PMP_MAX_DRIVES);
133 drvp = &chp->ch_drive[PMP_PORT_CTL];
134 KASSERT(drvp->drive == PMP_PORT_CTL);
135
136 xfer = ata_get_xfer(chp);
137 if (xfer == NULL)
138 return EINTR;
139
140 xfer->c_ata_c.r_command = PMPC_WRITE_PORT;
141 xfer->c_ata_c.r_features = reg;
142 xfer->c_ata_c.r_device = port;
143 xfer->c_ata_c.r_lba = (((value >> 40) & 0xffffff) << 24) |
144 (((value >> 8) & 0xffffff) << 0);
145 xfer->c_ata_c.r_count = (((value >> 32) & 0xff) << 8) |
146 (((value >> 0) & 0xff) << 0);
147 xfer->c_ata_c.timeout = 3000; /* 3s */
148 xfer->c_ata_c.r_st_bmask = 0;
149 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
150 xfer->c_ata_c.flags = AT_LBA48 | AT_WAIT;
151
152 if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
153 xfer) != ATACMD_COMPLETE) {
154 aprint_error_dev(chp->atabus,
155 "PMP port %d register %d write failed\n", port, reg);
156 error = EIO;
157 goto out;
158 }
159 if (xfer->c_ata_c.flags & (AT_TIMEOU | AT_DF)) {
160 aprint_error_dev(chp->atabus,
161 "PMP port %d register %d write failed, flags 0x%x\n",
162 port, reg, xfer->c_ata_c.flags);
163 error = EIO;
164 goto out;
165 }
166 if (xfer->c_ata_c.flags & AT_ERROR) {
167 aprint_verbose_dev(chp->atabus,
168 "PMP port %d register %d write failed, error 0x%x\n",
169 port, reg, xfer->c_ata_c.r_error);
170 error = EIO;
171 goto out;
172 }
173
174 out:
175 ata_free_xfer(chp, xfer);
176 return error;
177 }
178
179 static inline int
180 satapmp_write(struct ata_channel *chp, int port, int reg, uint32_t value)
181 {
182 return satapmp_write_8(chp, port, reg, value);
183 }
184
185 /*
186 * Reset one port's PHY and bring it online
187 * XXX duplicate of sata_reset_interface()
188 */
189 static uint32_t
190 satapmp_reset_device_port(struct ata_channel *chp, int port)
191 {
192 uint32_t scontrol, sstatus;
193 int i;
194
195 /* bring the PHY online */
196 scontrol = SControl_IPM_NONE | SControl_SPD_ANY | SControl_DET_INIT;
197 if (satapmp_write(chp, port, PMP_PSCR_SControl, scontrol) != 0)
198 return 0;
199
200 tsleep(chp, PRIBIO, "sataup", mstohz(50));
201 scontrol &= ~SControl_DET_INIT;
202 if (satapmp_write(chp, port, PMP_PSCR_SControl, scontrol) != 0)
203 return 0;
204 tsleep(chp, PRIBIO, "sataup", mstohz(50));
205
206 /* wait up to 1s for device to come up */
207 for (i = 0; i < 100; i++) {
208
209 if (satapmp_read(chp, port, PMP_PSCR_SStatus, &sstatus) != 0)
210 return 0;
211 if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
212 break;
213 tsleep(chp, PRIBIO, "sataup", mstohz(10));
214 }
215
216 switch (sstatus & SStatus_DET_mask) {
217 case SStatus_DET_NODEV:
218 /* No Device; be silent. */
219 break;
220 case SStatus_DET_DEV_NE:
221 aprint_error("%s PMP port %d: device connected, but "
222 "communication not established\n",
223 device_xname(chp->atabus), port);
224 break;
225 case SStatus_DET_OFFLINE:
226 aprint_error("%s PMP port %d: PHY offline\n",
227 device_xname(chp->atabus), port);
228 break;
229 case SStatus_DET_DEV:
230 aprint_normal("%s PMP port %d: device present, speed: %s\n",
231 device_xname(chp->atabus), port, sata_speed(sstatus));
232 break;
233 default:
234 aprint_error("%s PMP port %d: unknown SStatus: 0x%08x\n",
235 device_xname(chp->atabus), port, sstatus);
236 }
237 return(sstatus & SStatus_DET_mask);
238 }
239
240 void
241 satapmp_rescan(struct ata_channel *chp) {
242 int i;
243 uint32_t sig;
244
245 KASSERT(chp->ch_satapmp_nports <= PMP_PORT_CTL);
246 KASSERT(chp->ch_satapmp_nports <= chp->ch_ndrives);
247
248 for (i = 0; i < chp->ch_satapmp_nports; i++) {
249 if (chp->ch_drive[i].drive_type != ATA_DRIVET_NONE ||
250 satapmp_reset_device_port(chp, i) != SStatus_DET_DEV) {
251 continue;
252 }
253 if (satapmp_write(chp, i, PMP_PSCR_SError, 0xffffffff) != 0) {
254 aprint_error("%s PMP port %d: can't write SError\n",
255 device_xname(chp->atabus), i);
256 continue;
257 }
258 chp->ch_atac->atac_bustype_ata->ata_reset_drive(
259 &chp->ch_drive[i], AT_WAIT, &sig);
260
261 sata_interpret_sig(chp, i, sig);
262 }
263 }
264
265 void
266 satapmp_attach(struct ata_channel *chp)
267 {
268 uint32_t id, rev, inf;
269
270 if (satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_ID, &id) != 0 ||
271 satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_REV, &rev) != 0 ||
272 satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_INF, &inf) != 0) {
273 aprint_normal_dev(chp->atabus, "can't read PMP registers\n");
274 return;
275 }
276
277 aprint_normal_dev(chp->atabus,
278 "SATA port multiplier, %d ports\n", PMP_INF_NPORTS(inf));
279 aprint_verbose_dev(chp->atabus,
280 "vendor 0x%04x, product 0x%04x",
281 PMP_ID_VEND(id), PMP_ID_DEV(id));
282 if (rev & PMP_REV_SPEC_11) {
283 aprint_verbose(", revision 1.1");
284 } else if (rev & PMP_REV_SPEC_10) {
285 aprint_verbose(", revision 1.0");
286 } else {
287 aprint_verbose(", unknown revision 0x%x", rev & 0x0f);
288 }
289 aprint_verbose(", level %d\n", PMP_REV_LEVEL(rev));
290
291 chp->ch_satapmp_nports = PMP_INF_NPORTS(inf);
292
293 /* reset and bring up PHYs */
294 satapmp_rescan(chp);
295 }
296