1 /* $NetBSD: satapmp_subr.c,v 1.16 2020/04/13 10:49:34 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.16 2020/04/13 10:49:34 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
satapmp_read_8(struct ata_channel * chp,int port,int reg,uint64_t * value,struct ata_xfer * xfer)51 satapmp_read_8(struct ata_channel *chp, int port, int reg, uint64_t *value,
52 struct ata_xfer *xfer)
53 {
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 ata_channel_lock_owned(chp);
64
65 memset(xfer, 0, sizeof(*xfer));
66 xfer->c_ata_c.r_command = PMPC_READ_PORT;
67 xfer->c_ata_c.r_features = reg;
68 xfer->c_ata_c.r_device = port;
69 xfer->c_ata_c.timeout = 3000; /* 3s */
70 xfer->c_ata_c.r_st_bmask = 0;
71 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
72 xfer->c_ata_c.flags = AT_LBA48 | AT_READREG | AT_WAIT;
73
74 ata_channel_unlock(chp);
75
76 (*atac->atac_bustype_ata->ata_exec_command)(drvp, xfer);
77 ata_wait_cmd(chp, xfer);
78
79 if (xfer->c_ata_c.flags & (AT_TIMEOU | AT_DF)) {
80 aprint_error_dev(chp->atabus,
81 "PMP port %d register %d read failed, flags 0x%x\n",
82 port, reg, xfer->c_ata_c.flags);
83 error = EIO;
84 goto out;
85 }
86 if (xfer->c_ata_c.flags & AT_ERROR) {
87 aprint_verbose_dev(chp->atabus,
88 "PMP port %d register %d read failed, error 0x%x\n",
89 port, reg, xfer->c_ata_c.r_error);
90 error = EIO;
91 goto out;
92 }
93
94 *value = ((uint64_t)((xfer->c_ata_c.r_lba >> 24) & 0xffffff) << 40) |
95 ((uint64_t)((xfer->c_ata_c.r_count >> 8) & 0xff) << 32) |
96 ((uint64_t)((xfer->c_ata_c.r_lba >> 0) & 0xffffff) << 8) |
97 ((uint64_t)((xfer->c_ata_c.r_count >> 0) & 0xff) << 0);
98
99 out:
100 ata_channel_lock(chp);
101 return error;
102 }
103
104 static inline int
satapmp_read(struct ata_channel * chp,int port,int reg,uint32_t * value,struct ata_xfer * xfer)105 satapmp_read(struct ata_channel *chp, int port, int reg, uint32_t *value,
106 struct ata_xfer *xfer)
107 {
108 uint64_t value64;
109 int ret;
110
111 ret = satapmp_read_8(chp, port, reg, &value64, xfer);
112 if (ret)
113 return ret;
114
115 *value = value64 & 0xffffffff;
116 return ret;
117 }
118
119 static int
satapmp_write_8(struct ata_channel * chp,int port,int reg,uint64_t value,struct ata_xfer * xfer)120 satapmp_write_8(struct ata_channel *chp, int port, int reg, uint64_t value,
121 struct ata_xfer *xfer)
122 {
123 struct atac_softc *atac = chp->ch_atac;
124 struct ata_drive_datas *drvp;
125 int error = 0;
126
127 KASSERT(port < PMP_MAX_DRIVES);
128 KASSERT(reg < PMP_GSCR_NREGS);
129 KASSERT(chp->ch_ndrives >= PMP_MAX_DRIVES);
130 drvp = &chp->ch_drive[PMP_PORT_CTL];
131 KASSERT(drvp->drive == PMP_PORT_CTL);
132 ata_channel_lock_owned(chp);
133
134 memset(xfer, 0, sizeof(*xfer));
135 xfer->c_ata_c.r_command = PMPC_WRITE_PORT;
136 xfer->c_ata_c.r_features = reg;
137 xfer->c_ata_c.r_device = port;
138 xfer->c_ata_c.r_lba = (((value >> 40) & 0xffffff) << 24) |
139 (((value >> 8) & 0xffffff) << 0);
140 xfer->c_ata_c.r_count = (((value >> 32) & 0xff) << 8) |
141 (((value >> 0) & 0xff) << 0);
142 xfer->c_ata_c.timeout = 3000; /* 3s */
143 xfer->c_ata_c.r_st_bmask = 0;
144 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
145 xfer->c_ata_c.flags = AT_LBA48 | AT_WAIT;
146
147 ata_channel_unlock(chp);
148
149 (*atac->atac_bustype_ata->ata_exec_command)(drvp, xfer);
150 ata_wait_cmd(chp, xfer);
151
152 if (xfer->c_ata_c.flags & (AT_TIMEOU | AT_DF)) {
153 aprint_error_dev(chp->atabus,
154 "PMP port %d register %d write failed, flags 0x%x\n",
155 port, reg, xfer->c_ata_c.flags);
156 error = EIO;
157 goto out;
158 }
159 if (xfer->c_ata_c.flags & AT_ERROR) {
160 aprint_verbose_dev(chp->atabus,
161 "PMP port %d register %d write failed, error 0x%x\n",
162 port, reg, xfer->c_ata_c.r_error);
163 error = EIO;
164 goto out;
165 }
166
167 out:
168 ata_channel_lock(chp);
169 return error;
170 }
171
172 static inline int
satapmp_write(struct ata_channel * chp,int port,int reg,uint32_t value,struct ata_xfer * xfer)173 satapmp_write(struct ata_channel *chp, int port, int reg, uint32_t value,
174 struct ata_xfer *xfer)
175 {
176 return satapmp_write_8(chp, port, reg, value, xfer);
177 }
178
179 /*
180 * Reset one port's PHY and bring it online
181 * XXX duplicate of sata_reset_interface()
182 */
183 static uint32_t
satapmp_reset_device_port(struct ata_channel * chp,int port,struct ata_xfer * xfer)184 satapmp_reset_device_port(struct ata_channel *chp, int port,
185 struct ata_xfer *xfer)
186 {
187 uint32_t scontrol, sstatus;
188 int i;
189
190 ata_channel_lock_owned(chp);
191
192 /* bring the PHY online */
193 scontrol = SControl_IPM_NONE | SControl_SPD_ANY | SControl_DET_INIT;
194 if (satapmp_write(chp, port, PMP_PSCR_SControl, scontrol, xfer) != 0)
195 return 0;
196
197 ata_delay(chp, 50, "sataup", AT_WAIT);
198 scontrol &= ~SControl_DET_INIT;
199 if (satapmp_write(chp, port, PMP_PSCR_SControl, scontrol, xfer) != 0)
200 return 0;
201 ata_delay(chp, 50, "sataup", AT_WAIT);
202
203 /* wait up to 1s for device to come up */
204 for (i = 0; i < 100; i++) {
205
206 if (satapmp_read(chp, port, PMP_PSCR_SStatus, &sstatus,
207 xfer) != 0)
208 return 0;
209 if ((sstatus & SStatus_DET_mask) == SStatus_DET_DEV)
210 break;
211 ata_delay(chp, 10, "sataup", AT_WAIT);
212 }
213
214 switch (sstatus & SStatus_DET_mask) {
215 case SStatus_DET_NODEV:
216 /* No Device; be silent. */
217 break;
218 case SStatus_DET_DEV_NE:
219 aprint_error("%s PMP port %d: device connected, but "
220 "communication not established\n",
221 device_xname(chp->atabus), port);
222 break;
223 case SStatus_DET_OFFLINE:
224 aprint_error("%s PMP port %d: PHY offline\n",
225 device_xname(chp->atabus), port);
226 break;
227 case SStatus_DET_DEV:
228 aprint_normal("%s PMP port %d: device present, speed: %s\n",
229 device_xname(chp->atabus), port, sata_speed(sstatus));
230 break;
231 default:
232 aprint_error("%s PMP port %d: unknown SStatus: 0x%08x\n",
233 device_xname(chp->atabus), port, sstatus);
234 }
235 return(sstatus & SStatus_DET_mask);
236 }
237
238 static void __noinline
satapmp_rescan(struct ata_channel * chp,struct ata_xfer * xfer)239 satapmp_rescan(struct ata_channel *chp, struct ata_xfer *xfer)
240 {
241 int i;
242 uint32_t sig;
243
244 KASSERT(chp->ch_satapmp_nports <= PMP_PORT_CTL);
245 KASSERT(chp->ch_satapmp_nports <= chp->ch_ndrives);
246 ata_channel_lock_owned(chp);
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, xfer)
251 != SStatus_DET_DEV) {
252 continue;
253 }
254 if (satapmp_write(chp, i, PMP_PSCR_SError, 0xffffffff, xfer)
255 != 0) {
256 aprint_error("%s PMP port %d: can't write SError\n",
257 device_xname(chp->atabus), i);
258 continue;
259 }
260
261 ata_channel_lock_owned(chp);
262 chp->ch_atac->atac_bustype_ata->ata_reset_drive(
263 &chp->ch_drive[i], AT_WAIT, &sig);
264
265 sata_interpret_sig(chp, i, sig);
266 }
267 }
268
269 void
satapmp_attach(struct ata_channel * chp)270 satapmp_attach(struct ata_channel *chp)
271 {
272 uint32_t id, rev, inf;
273 struct ata_xfer *xfer;
274
275 xfer = ata_get_xfer(chp, false);
276 if (xfer == NULL) {
277 aprint_normal_dev(chp->atabus, "no available xfer\n");
278 return;
279 }
280
281 ata_channel_lock(chp);
282
283 if (satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_ID, &id, xfer) != 0 ||
284 satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_REV, &rev, xfer) != 0 ||
285 satapmp_read(chp, PMP_PORT_CTL, PMP_GSCR_INF, &inf, xfer) != 0) {
286 aprint_normal_dev(chp->atabus, "can't read PMP registers\n");
287 goto out;
288 }
289
290 aprint_normal("%s at %s channel %d: SATA port multiplier, %d ports\n",
291 device_xname(chp->atabus),
292 device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
293 PMP_INF_NPORTS(inf));
294 aprint_verbose_dev(chp->atabus,
295 "vendor 0x%04x, product 0x%04x",
296 PMP_ID_VEND(id), PMP_ID_DEV(id));
297 if (rev & PMP_REV_SPEC_11) {
298 aprint_verbose(", revision 1.1");
299 } else if (rev & PMP_REV_SPEC_10) {
300 aprint_verbose(", revision 1.0");
301 } else {
302 aprint_verbose(", unknown revision 0x%x", rev & 0x0f);
303 }
304 aprint_verbose(", level %d\n", PMP_REV_LEVEL(rev));
305
306 chp->ch_satapmp_nports = PMP_INF_NPORTS(inf);
307
308 /* reset and bring up PHYs */
309 satapmp_rescan(chp, xfer);
310
311 out:
312 ata_channel_unlock(chp);
313 ata_free_xfer(chp, xfer);
314 }
315