]> git.ipfire.org Git - thirdparty/u-boot.git/blame - cmd/sata.c
SPDX: Convert all of our single license tags to Linux Kernel style
[thirdparty/u-boot.git] / cmd / sata.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
c7057b52
DL
2/*
3 * Copyright (C) 2000-2005, DENX Software Engineering
4 * Wolfgang Denk <wd@denx.de>
5 * Copyright (C) Procsys. All rights reserved.
6 * Mushtaq Khan <mushtaq_k@procsys.com>
7 * <mushtaqk_921@yahoo.co.in>
8 * Copyright (C) 2008 Freescale Semiconductor, Inc.
9 * Dave Liu <daveliu@freescale.com>
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 21static int sata_curr_device = -1;
c7057b52 22
f19f1ecb
SG
23int 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
50int sata_probe(int devnum)
51{
52#ifdef CONFIG_AHCI
53 struct udevice *dev;
54 struct udevice *blk;
55 int rc;
56
57 rc = uclass_get_device(UCLASS_AHCI, devnum, &dev);
58 if (rc)
59 rc = uclass_find_first_device(UCLASS_AHCI, &dev);
60 if (rc) {
61 printf("Cannot probe SATA device %d (err=%d)\n", devnum, rc);
62 return CMD_RET_FAILURE;
63 }
64 rc = sata_scan(dev);
65 if (rc) {
66 printf("Cannot scan SATA device %d (err=%d)\n", devnum, rc);
67 return CMD_RET_FAILURE;
68 }
69
70 rc = blk_get_from_parent(dev, &blk);
71 if (!rc) {
72 struct blk_desc *desc = dev_get_uclass_platdata(blk);
73
74 if (desc->lba > 0 && desc->blksz > 0)
75 part_init(desc);
76 }
77
78 return 0;
79#else
80 return sata_initialize() < 0 ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
81#endif
82}
83
088f1b19 84static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
c7057b52
DL
85{
86 int rc = 0;
87
f19f1ecb
SG
88 if (argc >= 2) {
89 int devnum = 0;
90
91 if (argc == 3)
92 devnum = (int)simple_strtoul(argv[2], NULL, 10);
93 if (!strcmp(argv[1], "stop"))
94 return sata_remove(devnum);
d957c28a 95
f19f1ecb
SG
96 if (!strcmp(argv[1], "init")) {
97 if (sata_curr_device != -1) {
98 rc = sata_remove(devnum);
99 if (rc)
100 return rc;
101 }
d957c28a 102
f19f1ecb
SG
103 return sata_probe(devnum);
104 }
d957c28a 105 }
cf7e399f
MF
106
107 /* If the user has not yet run `sata init`, do it now */
aa6ab905 108 if (sata_curr_device == -1) {
f19f1ecb
SG
109 rc = sata_probe(0);
110 if (rc < 0)
8547f45b 111 return CMD_RET_FAILURE;
f19f1ecb 112 sata_curr_device = 0;
aa6ab905 113 }
cf7e399f 114
e29e71e9 115 return blk_common_cmd(argc, argv, IF_TYPE_SATA, &sata_curr_device);
c7057b52
DL
116}
117
118U_BOOT_CMD(
119 sata, 5, 1, do_sata,
2fb2604d 120 "SATA sub system",
85dafbb8 121 "init - init SATA sub system\n"
f19f1ecb 122 "sata stop [dev] - disable SATA sub system or device\n"
c7057b52
DL
123 "sata info - show available SATA devices\n"
124 "sata device [dev] - show or set current device\n"
125 "sata part [dev] - print partition table\n"
126 "sata read addr blk# cnt\n"
a89c33db
WD
127 "sata write addr blk# cnt"
128);