forked from pfsense/FreeBSD-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathahci_fsl_fdt.c
419 lines (354 loc) · 11.9 KB
/
ahci_fsl_fdt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2020 Alstom Group
* Copyright (c) 2020 Semihalf
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* AHCI controller driver for NXP QorIQ Layerscape SoCs. */
#include <sys/cdefs.h>
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <sys/rman.h>
#include <sys/unistd.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ahci/ahci.h>
#include <dev/clk/clk.h>
#define AHCI_FSL_REG_PHY1 0xa8
#define AHCI_FSL_REG_PHY2 0xac
#define AHCI_FSL_REG_PHY3 0xb0
#define AHCI_FSL_REG_PHY4 0xb4
#define AHCI_FSL_REG_PHY5 0xb8
#define AHCI_FSL_REG_AXICC 0xbc
#define AHCI_FSL_REG_PTC 0xc8
#define AHCI_FSL_LS1021A_AXICC 0xc0
#define AHCI_FSL_REG_PHY1_TTA_MASK 0x0001ffff
#define AHCI_FSL_REG_PHY1_SNM (1 << 17)
#define AHCI_FSL_REG_PHY1_SNR (1 << 18)
#define AHCI_FSL_REG_PHY1_FPR (1 << 20)
#define AHCI_FSL_REG_PHY1_PBPS_LBP 0
#define AHCI_FSL_REG_PHY1_PBPS_LFTP (0x01 << 21)
#define AHCI_FSL_REG_PHY1_PBPS_MFTP (0x02 << 21)
#define AHCI_FSL_REG_PHY1_PBPS_HFTP (0x03 << 21)
#define AHCI_FSL_REG_PHY1_PBPS_PRBS (0x04 << 21)
#define AHCI_FSL_REG_PHY1_PBPS_BIST (0x05 << 21)
#define AHCI_FSL_REG_PHY1_PBPE (1 << 24)
#define AHCI_FSL_REG_PHY1_PBCE (1 << 25)
#define AHCI_FSL_REG_PHY1_PBPNA (1 << 26)
#define AHCI_FSL_REG_PHY1_STB (1 << 27)
#define AHCI_FSL_REG_PHY1_PSSO (1 << 28)
#define AHCI_FSL_REG_PHY1_PSS (1 << 29)
#define AHCI_FSL_REG_PHY1_ERSN (1 << 30)
#define AHCI_FSL_REG_PHY1_ESDF (1 << 31)
#define AHCI_FSL_REG_PHY_MASK 0xff
#define AHCI_FSL_PHY2_CIBGMN_SHIFT 0
#define AHCI_FSL_PHY2_CIBGMX_SHIFT 8
#define AHCI_FSL_PHY2_CIBGN_SHIFT 16
#define AHCI_FSL_PHY2_CINMP_SHIFT 24
#define AHCI_FSL_PHY3_CWBGMN_SHIFT 0
#define AHCI_FSL_PHY3_CWBGMX_SHIFT 8
#define AHCI_FSL_PHY3_CWBGN_SHIFT 16
#define AHCI_FSL_PHY3_CWNMP_SHIFT 24
/* Only in LS1021A */
#define AHCI_FSL_PHY4_BMX_SHIFT 0
#define AHCI_FSL_PHY4_BNM_SHIFT 8
#define AHCI_FSL_PHY4_SFD_SHIFT 16
#define AHCI_FSL_PHY4_PTST_SHIFT 24
/* Only in LS1021A */
#define AHCI_FSL_PHY5_RIT_SHIFT 0
#define AHCI_FSL_PHY5_RCT_SHIFT 20
#define AHCI_FSL_REG_PTC_RXWM_MASK 0x0000007f
#define AHCI_FSL_REG_PTC_ENBD (1 << 8)
#define AHCI_FSL_REG_PTC_ITM (1 << 9)
#define AHCI_FSL_REG_PHY1_CFG \
((0x1fffe & AHCI_FSL_REG_PHY1_TTA_MASK) | \
AHCI_FSL_REG_PHY1_SNM | AHCI_FSL_REG_PHY1_PSS | AHCI_FSL_REG_PHY1_ESDF)
#define AHCI_FSL_REG_PHY2_CFG \
((0x1f << AHCI_FSL_PHY2_CIBGMN_SHIFT) | \
(0x4d << AHCI_FSL_PHY2_CIBGMX_SHIFT) | \
(0x18 << AHCI_FSL_PHY2_CIBGN_SHIFT) | \
(0x28 << AHCI_FSL_PHY2_CINMP_SHIFT))
#define AHCI_FSL_REG_PHY2_CFG_LS1021A \
((0x14 << AHCI_FSL_PHY2_CIBGMN_SHIFT) | \
(0x34 << AHCI_FSL_PHY2_CIBGMX_SHIFT) | \
(0x18 << AHCI_FSL_PHY2_CIBGN_SHIFT) | \
(0x28 << AHCI_FSL_PHY2_CINMP_SHIFT))
#define AHCI_FSL_REG_PHY3_CFG \
((0x09 << AHCI_FSL_PHY3_CWBGMN_SHIFT) | \
(0x15 << AHCI_FSL_PHY3_CWBGMX_SHIFT) | \
(0x08 << AHCI_FSL_PHY3_CWBGN_SHIFT) | \
(0x0e << AHCI_FSL_PHY3_CWNMP_SHIFT))
#define AHCI_FSL_REG_PHY3_CFG_LS1021A \
((0x06 << AHCI_FSL_PHY3_CWBGMN_SHIFT) | \
(0x0e << AHCI_FSL_PHY3_CWBGMX_SHIFT) | \
(0x08 << AHCI_FSL_PHY3_CWBGN_SHIFT) | \
(0x0e << AHCI_FSL_PHY3_CWNMP_SHIFT))
#define AHCI_FSL_REG_PHY4_CFG_LS1021A \
((0x0b << AHCI_FSL_PHY4_BMX_SHIFT) | \
(0x08 << AHCI_FSL_PHY4_BNM_SHIFT) | \
(0x4a << AHCI_FSL_PHY4_SFD_SHIFT) | \
(0x06 << AHCI_FSL_PHY4_PTST_SHIFT))
#define AHCI_FSL_REG_PHY5_CFG_LS1021A \
((0x86470 << AHCI_FSL_PHY5_RIT_SHIFT) | \
(0x2aa << AHCI_FSL_PHY5_RCT_SHIFT))
/* Bit 27 enabled so value of reserved bits remains as in documentation. */
#define AHCI_FSL_REG_PTC_CFG \
((0x29 & AHCI_FSL_REG_PTC_RXWM_MASK) | (1 << 27))
#define AHCI_FSL_REG_AXICC_CFG 0x3fffffff
#define AHCI_FSL_REG_ECC 0x0
#define AHCI_FSL_REG_ECC_LS1021A 0x00020000
#define AHCI_FSL_REG_ECC_LS1043A 0x80000000
#define AHCI_FSL_REG_ECC_LS1028A 0x40000000
#define QORIQ_AHCI_LS1021A 1
#define QORIQ_AHCI_LS1028A 2
#define QORIQ_AHCI_LS1043A 3
#define QORIQ_AHCI_LS2080A 4
#define QORIQ_AHCI_LS1046A 5
#define QORIQ_AHCI_LS1088A 6
#define QORIQ_AHCI_LS2088A 7
#define QORIQ_AHCI_LX2160A 8
struct ahci_fsl_fdt_controller {
struct ahci_controller ctlr; /* Must be the first field. */
int soc_type;
struct resource *r_ecc;
int r_ecc_rid;
};
static const struct ofw_compat_data ahci_fsl_fdt_compat_data[] = {
{"fsl,ls1021a-ahci", QORIQ_AHCI_LS1021A},
{"fsl,ls1028a-ahci", QORIQ_AHCI_LS1028A},
{"fsl,ls1043a-ahci", QORIQ_AHCI_LS1043A},
{"fsl,ls2080a-ahci", QORIQ_AHCI_LS2080A},
{"fsl,ls1046a-ahci", QORIQ_AHCI_LS1046A},
{"fsl,ls1088a-ahci", QORIQ_AHCI_LS1088A},
{"fsl,ls2088a-ahci", QORIQ_AHCI_LS2088A},
{"fsl,lx2160a-ahci", QORIQ_AHCI_LX2160A},
{NULL, 0}
};
static bool ecc_inited;
static int
ahci_fsl_fdt_ecc_init(struct ahci_fsl_fdt_controller *ctrl)
{
uint32_t val;
switch (ctrl->soc_type) {
case QORIQ_AHCI_LS2080A:
case QORIQ_AHCI_LS2088A:
return (0);
case QORIQ_AHCI_LS1021A:
if (!ecc_inited && ctrl->r_ecc == NULL)
return (ENXIO);
if (!ecc_inited)
ATA_OUTL(ctrl->r_ecc, AHCI_FSL_REG_ECC,
AHCI_FSL_REG_ECC_LS1021A);
break;
case QORIQ_AHCI_LS1043A:
case QORIQ_AHCI_LS1046A:
if (!ecc_inited && ctrl->r_ecc == NULL)
return (ENXIO);
if (!ecc_inited) {
val = ATA_INL(ctrl->r_ecc, AHCI_FSL_REG_ECC);
val = AHCI_FSL_REG_ECC_LS1043A;
ATA_OUTL(ctrl->r_ecc, AHCI_FSL_REG_ECC, val);
}
break;
case QORIQ_AHCI_LS1028A:
case QORIQ_AHCI_LS1088A:
case QORIQ_AHCI_LX2160A:
if (!ecc_inited && ctrl->r_ecc == NULL)
return (ENXIO);
if (!ecc_inited) {
val = ATA_INL(ctrl->r_ecc, AHCI_FSL_REG_ECC);
val |= AHCI_FSL_REG_ECC_LS1028A;
ATA_OUTL(ctrl->r_ecc, AHCI_FSL_REG_ECC, val);
}
break;
default:
panic("Unimplemented SOC type: %d", ctrl->soc_type);
}
ecc_inited = true;
return (0);
}
static void
ahci_fsl_fdt_phy_init(struct ahci_fsl_fdt_controller *ctrl)
{
struct ahci_controller *ahci;
ahci = &ctrl->ctlr;
if (ctrl->soc_type == QORIQ_AHCI_LS1021A) {
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_PHY1,
AHCI_FSL_REG_PHY1_CFG);
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_PHY2,
AHCI_FSL_REG_PHY2_CFG_LS1021A);
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_PHY3,
AHCI_FSL_REG_PHY3_CFG_LS1021A);
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_PHY4,
AHCI_FSL_REG_PHY4_CFG_LS1021A);
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_PHY5,
AHCI_FSL_REG_PHY5_CFG_LS1021A);
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_PTC,
AHCI_FSL_REG_PTC_CFG);
if (ctrl->ctlr.dma_coherent)
ATA_OUTL(ahci->r_mem, AHCI_FSL_LS1021A_AXICC,
AHCI_FSL_REG_AXICC_CFG);
} else {
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_PHY1,
AHCI_FSL_REG_PHY1_CFG);
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_PHY2,
AHCI_FSL_REG_PHY2_CFG);
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_PHY3,
AHCI_FSL_REG_PHY3_CFG);
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_PTC,
AHCI_FSL_REG_PTC_CFG);
if (ctrl->ctlr.dma_coherent)
ATA_OUTL(ahci->r_mem, AHCI_FSL_REG_AXICC,
AHCI_FSL_REG_AXICC_CFG);
}
}
static int
ahci_fsl_fdt_probe(device_t dev)
{
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (!ofw_bus_search_compatible(dev, ahci_fsl_fdt_compat_data)->ocd_data)
return (ENXIO);
device_set_desc(dev, "NXP QorIQ Layerscape AHCI controller");
return (BUS_PROBE_DEFAULT);
}
static int
ahci_fsl_fdt_attach(device_t dev)
{
struct ahci_fsl_fdt_controller *ctlr;
struct ahci_controller *ahci;
phandle_t node;
clk_t clock;
int ret;
node = ofw_bus_get_node(dev);
ctlr = device_get_softc(dev);
ctlr->soc_type =
ofw_bus_search_compatible(dev, ahci_fsl_fdt_compat_data)->ocd_data;
ahci = &ctlr->ctlr;
ahci->dev = dev;
ahci->r_rid = 0;
ahci->quirks = AHCI_Q_NOPMP;
ahci->dma_coherent = OF_hasprop(node, "dma-coherent");
ret = clk_get_by_ofw_index(dev, node, 0, &clock);
if (ret != 0) {
device_printf(dev, "No clock found.\n");
return (ENXIO);
}
ret = clk_enable(clock);
if (ret !=0) {
device_printf(dev, "Could not enable clock.\n");
return (ENXIO);
}
if (OF_hasprop(node, "reg-names") && ofw_bus_find_string_index(node,
"reg-names", "ahci", &ahci->r_rid)) {
device_printf(dev, "Could not locate 'ahci' string in the "
"'reg-names' property");
return (ENOENT);
}
ahci->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
&ahci->r_rid, RF_ACTIVE);
if (!ahci->r_mem) {
device_printf(dev,
"Could not allocate resources for controller\n");
return (ENOMEM);
}
ret = ofw_bus_find_string_index(node, "reg-names", "sata-ecc",
&ctlr->r_ecc_rid);
if (ret == 0) {
ctlr->r_ecc = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
&ctlr->r_ecc_rid, RF_ACTIVE| RF_SHAREABLE);
if (!ctlr->r_ecc) {
device_printf(dev,
"Could not allocate resources for controller\n");
ret = ENOMEM;
goto err_free_mem;
}
} else if (ret != ENOENT) {
device_printf(dev, "Could not locate 'sata-ecc' string in "
"the 'reg-names' property");
goto err_free_mem;
}
ret = ahci_fsl_fdt_ecc_init(ctlr);
if (ret != 0) {
device_printf(dev, "Could not initialize 'ecc' registers");
goto err_free_mem;
}
/* Setup controller defaults. */
ahci->numirqs = 1;
ahci_fsl_fdt_phy_init(ctlr);
/* Reset controller. */
ret = ahci_ctlr_reset(dev);
if (ret)
goto err_free_mem;
ret = ahci_attach(dev);
if (ret) {
device_printf(dev,
"Could not initialize AHCI, with error: %d\n", ret);
goto err_free_ecc;
}
return (0);
err_free_mem:
bus_free_resource(dev, SYS_RES_MEMORY, ahci->r_mem);
err_free_ecc:
if (ctlr->r_ecc)
bus_free_resource(dev, SYS_RES_MEMORY, ctlr->r_ecc);
return (ret);
}
static int
ahci_fsl_fdt_detach(device_t dev)
{
struct ahci_fsl_fdt_controller *ctlr;
ctlr = device_get_softc(dev);
if (ctlr->r_ecc)
bus_free_resource(dev, SYS_RES_MEMORY, ctlr->r_ecc);
return ahci_detach(dev);
}
static const device_method_t ahci_fsl_fdt_methods[] = {
DEVMETHOD(device_probe, ahci_fsl_fdt_probe),
DEVMETHOD(device_attach, ahci_fsl_fdt_attach),
DEVMETHOD(device_detach, ahci_fsl_fdt_detach),
DEVMETHOD(bus_alloc_resource, ahci_alloc_resource),
DEVMETHOD(bus_release_resource, ahci_release_resource),
DEVMETHOD(bus_setup_intr, ahci_setup_intr),
DEVMETHOD(bus_teardown_intr, ahci_teardown_intr),
DEVMETHOD(bus_print_child, ahci_print_child),
DEVMETHOD(bus_child_location, ahci_child_location),
DEVMETHOD(bus_get_dma_tag, ahci_get_dma_tag),
DEVMETHOD_END
};
static driver_t ahci_fsl_fdt_driver = {
"ahci",
ahci_fsl_fdt_methods,
sizeof(struct ahci_fsl_fdt_controller),
};
DRIVER_MODULE(ahci_fsl, simplebus, ahci_fsl_fdt_driver, NULL, NULL);
DRIVER_MODULE(ahci_fsl, ofwbus, ahci_fsl_fdt_driver, NULL, NULL);