blob: 8b923f9378b221cbc1002021ecdfd66bbfaa3824 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dave Liu76596ba2008-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 Liu76596ba2008-03-26 22:49:44 +080010 */
11
Simon Glass6c857ad2017-07-29 11:35:13 -060012#include <ahci.h>
Simon Glass655306c2020-05-10 11:39:58 -060013#include <blk.h>
Simon Glass6c857ad2017-07-29 11:35:13 -060014#include <dm.h>
Dave Liu76596ba2008-03-26 22:49:44 +080015#include <command.h>
16#include <part.h>
17#include <sata.h>
Simon Glass6c857ad2017-07-29 11:35:13 -060018#include <dm/device-internal.h>
19#include <dm/uclass-internal.h>
Dave Liu76596ba2008-03-26 22:49:44 +080020
Kim Phillipsdc00a682012-10-29 13:34:31 +000021static int sata_curr_device = -1;
Dave Liu76596ba2008-03-26 22:49:44 +080022
Simon Glass6c857ad2017-07-29 11:35:13 -060023int sata_remove(int devnum)
24{
25#ifdef CONFIG_AHCI
26 struct udevice *dev;
27 int rc;
28
Simon Glassdbfa32c2022-08-11 19:34:59 -060029 blk_unbind_all(UCLASS_AHCI);
Peng Ma18b9a672019-12-04 10:36:47 +000030
Simon Glass6c857ad2017-07-29 11:35:13 -060031 rc = uclass_find_device(UCLASS_AHCI, devnum, &dev);
32 if (!rc && !dev)
33 rc = uclass_find_first_device(UCLASS_AHCI, &dev);
34 if (rc || !dev) {
35 printf("Cannot find SATA device %d (err=%d)\n", devnum, rc);
36 return CMD_RET_FAILURE;
37 }
38
39 rc = device_remove(dev, DM_REMOVE_NORMAL);
40 if (rc) {
41 printf("Cannot remove SATA device '%s' (err=%d)\n", dev->name,
42 rc);
43 return CMD_RET_FAILURE;
44 }
45
46 return 0;
47#else
48 return sata_stop();
49#endif
50}
51
52int sata_probe(int devnum)
53{
54#ifdef CONFIG_AHCI
55 struct udevice *dev;
Simon Glass6c857ad2017-07-29 11:35:13 -060056 int rc;
57
58 rc = uclass_get_device(UCLASS_AHCI, devnum, &dev);
59 if (rc)
60 rc = uclass_find_first_device(UCLASS_AHCI, &dev);
61 if (rc) {
62 printf("Cannot probe SATA device %d (err=%d)\n", devnum, rc);
63 return CMD_RET_FAILURE;
64 }
Marcel Ziswileradcef3d2019-02-01 16:01:08 +010065 if (!dev) {
66 printf("No SATA device found!\n");
67 return CMD_RET_FAILURE;
68 }
Simon Glass6c857ad2017-07-29 11:35:13 -060069 rc = sata_scan(dev);
70 if (rc) {
71 printf("Cannot scan SATA device %d (err=%d)\n", devnum, rc);
72 return CMD_RET_FAILURE;
73 }
74
Simon Glass6c857ad2017-07-29 11:35:13 -060075 return 0;
76#else
77 return sata_initialize() < 0 ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
78#endif
79}
80
Simon Glassed38aef2020-05-10 11:40:03 -060081static int do_sata(struct cmd_tbl *cmdtp, int flag, int argc,
82 char *const argv[])
Dave Liu76596ba2008-03-26 22:49:44 +080083{
84 int rc = 0;
85
Simon Glass6c857ad2017-07-29 11:35:13 -060086 if (argc >= 2) {
87 int devnum = 0;
Nikita Kiryanov44fd1f92014-11-21 12:47:24 +020088
Simon Glass6c857ad2017-07-29 11:35:13 -060089 if (argc == 3)
Simon Glassff9b9032021-07-24 09:03:30 -060090 devnum = (int)dectoul(argv[2], NULL);
Simon Glass6c857ad2017-07-29 11:35:13 -060091 if (!strcmp(argv[1], "stop"))
92 return sata_remove(devnum);
Nikita Kiryanov44fd1f92014-11-21 12:47:24 +020093
Simon Glass6c857ad2017-07-29 11:35:13 -060094 if (!strcmp(argv[1], "init")) {
95 if (sata_curr_device != -1) {
96 rc = sata_remove(devnum);
97 if (rc)
98 return rc;
99 }
100
101 return sata_probe(devnum);
102 }
Nikita Kiryanov44fd1f92014-11-21 12:47:24 +0200103 }
Mike Frysingere66dc082009-01-27 16:12:21 -0500104
105 /* If the user has not yet run `sata init`, do it now */
Tang Yuantianb450f932016-11-21 10:24:20 +0800106 if (sata_curr_device == -1) {
Simon Glass6c857ad2017-07-29 11:35:13 -0600107 rc = sata_probe(0);
Troy Kisky7f00c2f2018-07-27 16:45:26 -0700108 if (rc)
109 return rc;
Simon Glass6c857ad2017-07-29 11:35:13 -0600110 sata_curr_device = 0;
Tang Yuantianb450f932016-11-21 10:24:20 +0800111 }
Mike Frysingere66dc082009-01-27 16:12:21 -0500112
Simon Glassdbfa32c2022-08-11 19:34:59 -0600113 return blk_common_cmd(argc, argv, UCLASS_AHCI, &sata_curr_device);
Dave Liu76596ba2008-03-26 22:49:44 +0800114}
115
116U_BOOT_CMD(
117 sata, 5, 1, do_sata,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600118 "SATA sub system",
Fabio Estevam6be8b832013-02-20 14:35:35 +0000119 "init - init SATA sub system\n"
Simon Glass6c857ad2017-07-29 11:35:13 -0600120 "sata stop [dev] - disable SATA sub system or device\n"
Dave Liu76596ba2008-03-26 22:49:44 +0800121 "sata info - show available SATA devices\n"
122 "sata device [dev] - show or set current device\n"
123 "sata part [dev] - print partition table\n"
124 "sata read addr blk# cnt\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200125 "sata write addr blk# cnt"
126);