blob: 6d62ba8f74aaceb9702d9db361e68966b89b304a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dave Liuc7057b52008-03-26 22:49:44 +08002/*
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>
Dave Liuc7057b52008-03-26 22:49:44 +080010 */
11
12#include <common.h>
Simon Glassf19f1ec2017-07-29 11:35:13 -060013#include <ahci.h>
14#include <dm.h>
Dave Liuc7057b52008-03-26 22:49:44 +080015#include <command.h>
16#include <part.h>
17#include <sata.h>
Simon Glassf19f1ec2017-07-29 11:35:13 -060018#include <dm/device-internal.h>
19#include <dm/uclass-internal.h>
Dave Liuc7057b52008-03-26 22:49:44 +080020
Kim Phillips088f1b12012-10-29 13:34:31 +000021static int sata_curr_device = -1;
Dave Liuc7057b52008-03-26 22:49:44 +080022
Simon Glassf19f1ec2017-07-29 11:35:13 -060023int 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;
Simon Glassf19f1ec2017-07-29 11:35:13 -060054 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
Simon Glassf19f1ec2017-07-29 11:35:13 -060069 return 0;
70#else
71 return sata_initialize() < 0 ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
72#endif
73}
74
Kim Phillips088f1b12012-10-29 13:34:31 +000075static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Dave Liuc7057b52008-03-26 22:49:44 +080076{
77 int rc = 0;
78
Simon Glassf19f1ec2017-07-29 11:35:13 -060079 if (argc >= 2) {
80 int devnum = 0;
Nikita Kiryanovd957c282014-11-21 12:47:24 +020081
Simon Glassf19f1ec2017-07-29 11:35:13 -060082 if (argc == 3)
83 devnum = (int)simple_strtoul(argv[2], NULL, 10);
84 if (!strcmp(argv[1], "stop"))
85 return sata_remove(devnum);
Nikita Kiryanovd957c282014-11-21 12:47:24 +020086
Simon Glassf19f1ec2017-07-29 11:35:13 -060087 if (!strcmp(argv[1], "init")) {
88 if (sata_curr_device != -1) {
89 rc = sata_remove(devnum);
90 if (rc)
91 return rc;
92 }
93
94 return sata_probe(devnum);
95 }
Nikita Kiryanovd957c282014-11-21 12:47:24 +020096 }
Mike Frysingercf7e3992009-01-27 16:12:21 -050097
98 /* If the user has not yet run `sata init`, do it now */
Tang Yuantianaa6ab902016-11-21 10:24:20 +080099 if (sata_curr_device == -1) {
Simon Glassf19f1ec2017-07-29 11:35:13 -0600100 rc = sata_probe(0);
Troy Kisky7e83f1d2018-07-27 16:45:26 -0700101 if (rc)
102 return rc;
Simon Glassf19f1ec2017-07-29 11:35:13 -0600103 sata_curr_device = 0;
Tang Yuantianaa6ab902016-11-21 10:24:20 +0800104 }
Mike Frysingercf7e3992009-01-27 16:12:21 -0500105
Simon Glasse29e71e2017-07-29 11:34:55 -0600106 return blk_common_cmd(argc, argv, IF_TYPE_SATA, &sata_curr_device);
Dave Liuc7057b52008-03-26 22:49:44 +0800107}
108
109U_BOOT_CMD(
110 sata, 5, 1, do_sata,
Peter Tyser2fb26042009-01-27 18:03:12 -0600111 "SATA sub system",
Fabio Estevam85dafbb2013-02-20 14:35:35 +0000112 "init - init SATA sub system\n"
Simon Glassf19f1ec2017-07-29 11:35:13 -0600113 "sata stop [dev] - disable SATA sub system or device\n"
Dave Liuc7057b52008-03-26 22:49:44 +0800114 "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"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200118 "sata write addr blk# cnt"
119);