]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
c7057b52 DL |
2 | /* |
3 | * Copyright (C) 2000-2005, DENX Software Engineering | |
4 | * Wolfgang Denk <[email protected]> | |
5 | * Copyright (C) Procsys. All rights reserved. | |
6 | * Mushtaq Khan <[email protected]> | |
7 | * <[email protected]> | |
8 | * Copyright (C) 2008 Freescale Semiconductor, Inc. | |
9 | * Dave Liu <[email protected]> | |
c7057b52 DL |
10 | */ |
11 | ||
12 | #include <common.h> | |
f19f1ecb SG |
13 | #include <ahci.h> |
14 | #include <dm.h> | |
c7057b52 DL |
15 | #include <command.h> |
16 | #include <part.h> | |
17 | #include <sata.h> | |
f19f1ecb SG |
18 | #include <dm/device-internal.h> |
19 | #include <dm/uclass-internal.h> | |
c7057b52 | 20 | |
088f1b19 | 21 | static int sata_curr_device = -1; |
c7057b52 | 22 | |
f19f1ecb SG |
23 | int sata_remove(int devnum) |
24 | { | |
25 | #ifdef CONFIG_AHCI | |
26 | struct udevice *dev; | |
27 | int rc; | |
28 | ||
29 | rc = uclass_find_device(UCLASS_AHCI, devnum, &dev); | |
30 | if (!rc && !dev) | |
31 | rc = uclass_find_first_device(UCLASS_AHCI, &dev); | |
32 | if (rc || !dev) { | |
33 | printf("Cannot find SATA device %d (err=%d)\n", devnum, rc); | |
34 | return CMD_RET_FAILURE; | |
35 | } | |
36 | ||
37 | rc = device_remove(dev, DM_REMOVE_NORMAL); | |
38 | if (rc) { | |
39 | printf("Cannot remove SATA device '%s' (err=%d)\n", dev->name, | |
40 | rc); | |
41 | return CMD_RET_FAILURE; | |
42 | } | |
43 | ||
44 | return 0; | |
45 | #else | |
46 | return sata_stop(); | |
47 | #endif | |
48 | } | |
49 | ||
50 | int sata_probe(int devnum) | |
51 | { | |
52 | #ifdef CONFIG_AHCI | |
53 | struct udevice *dev; | |
f19f1ecb SG |
54 | int rc; |
55 | ||
56 | rc = uclass_get_device(UCLASS_AHCI, devnum, &dev); | |
57 | if (rc) | |
58 | rc = uclass_find_first_device(UCLASS_AHCI, &dev); | |
59 | if (rc) { | |
60 | printf("Cannot probe SATA device %d (err=%d)\n", devnum, rc); | |
61 | return CMD_RET_FAILURE; | |
62 | } | |
63 | rc = sata_scan(dev); | |
64 | if (rc) { | |
65 | printf("Cannot scan SATA device %d (err=%d)\n", devnum, rc); | |
66 | return CMD_RET_FAILURE; | |
67 | } | |
68 | ||
f19f1ecb SG |
69 | return 0; |
70 | #else | |
71 | return sata_initialize() < 0 ? CMD_RET_FAILURE : CMD_RET_SUCCESS; | |
72 | #endif | |
73 | } | |
74 | ||
088f1b19 | 75 | static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
c7057b52 DL |
76 | { |
77 | int rc = 0; | |
78 | ||
f19f1ecb SG |
79 | if (argc >= 2) { |
80 | int devnum = 0; | |
81 | ||
82 | if (argc == 3) | |
83 | devnum = (int)simple_strtoul(argv[2], NULL, 10); | |
84 | if (!strcmp(argv[1], "stop")) | |
85 | return sata_remove(devnum); | |
d957c28a | 86 | |
f19f1ecb SG |
87 | if (!strcmp(argv[1], "init")) { |
88 | if (sata_curr_device != -1) { | |
89 | rc = sata_remove(devnum); | |
90 | if (rc) | |
91 | return rc; | |
92 | } | |
d957c28a | 93 | |
f19f1ecb SG |
94 | return sata_probe(devnum); |
95 | } | |
d957c28a | 96 | } |
cf7e399f MF |
97 | |
98 | /* If the user has not yet run `sata init`, do it now */ | |
aa6ab905 | 99 | if (sata_curr_device == -1) { |
f19f1ecb | 100 | rc = sata_probe(0); |
7e83f1d5 TK |
101 | if (rc) |
102 | return rc; | |
f19f1ecb | 103 | sata_curr_device = 0; |
aa6ab905 | 104 | } |
cf7e399f | 105 | |
e29e71e9 | 106 | return blk_common_cmd(argc, argv, IF_TYPE_SATA, &sata_curr_device); |
c7057b52 DL |
107 | } |
108 | ||
109 | U_BOOT_CMD( | |
110 | sata, 5, 1, do_sata, | |
2fb2604d | 111 | "SATA sub system", |
85dafbb8 | 112 | "init - init SATA sub system\n" |
f19f1ecb | 113 | "sata stop [dev] - disable SATA sub system or device\n" |
c7057b52 DL |
114 | "sata info - show available SATA devices\n" |
115 | "sata device [dev] - show or set current device\n" | |
116 | "sata part [dev] - print partition table\n" | |
117 | "sata read addr blk# cnt\n" | |
a89c33db WD |
118 | "sata write addr blk# cnt" |
119 | ); |