blob: 9c9fe111d12f2b7fe071b02c7c666a5e8389247b [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
12#include <common.h>
Simon Glass6c857ad2017-07-29 11:35:13 -060013#include <ahci.h>
Simon Glass655306c2020-05-10 11:39:58 -060014#include <blk.h>
Simon Glass6c857ad2017-07-29 11:35:13 -060015#include <dm.h>
Dave Liu76596ba2008-03-26 22:49:44 +080016#include <command.h>
17#include <part.h>
18#include <sata.h>
Simon Glass6c857ad2017-07-29 11:35:13 -060019#include <dm/device-internal.h>
20#include <dm/uclass-internal.h>
Dave Liu76596ba2008-03-26 22:49:44 +080021
Kim Phillipsdc00a682012-10-29 13:34:31 +000022static int sata_curr_device = -1;
Dave Liu76596ba2008-03-26 22:49:44 +080023
Simon Glass6c857ad2017-07-29 11:35:13 -060024int sata_remove(int devnum)
25{
26#ifdef CONFIG_AHCI
27 struct udevice *dev;
28 int rc;
29
Simon Glassdbfa32c2022-08-11 19:34:59 -060030 blk_unbind_all(UCLASS_AHCI);
Peng Ma18b9a672019-12-04 10:36:47 +000031
Simon Glass6c857ad2017-07-29 11:35:13 -060032 rc = uclass_find_device(UCLASS_AHCI, devnum, &dev);
33 if (!rc && !dev)
34 rc = uclass_find_first_device(UCLASS_AHCI, &dev);
35 if (rc || !dev) {
36 printf("Cannot find SATA device %d (err=%d)\n", devnum, rc);
37 return CMD_RET_FAILURE;
38 }
39
40 rc = device_remove(dev, DM_REMOVE_NORMAL);
41 if (rc) {
42 printf("Cannot remove SATA device '%s' (err=%d)\n", dev->name,
43 rc);
44 return CMD_RET_FAILURE;
45 }
46
47 return 0;
48#else
49 return sata_stop();
50#endif
51}
52
53int sata_probe(int devnum)
54{
55#ifdef CONFIG_AHCI
56 struct udevice *dev;
Simon Glass6c857ad2017-07-29 11:35:13 -060057 int rc;
58
59 rc = uclass_get_device(UCLASS_AHCI, devnum, &dev);
60 if (rc)
61 rc = uclass_find_first_device(UCLASS_AHCI, &dev);
62 if (rc) {
63 printf("Cannot probe SATA device %d (err=%d)\n", devnum, rc);
64 return CMD_RET_FAILURE;
65 }
Marcel Ziswileradcef3d2019-02-01 16:01:08 +010066 if (!dev) {
67 printf("No SATA device found!\n");
68 return CMD_RET_FAILURE;
69 }
Simon Glass6c857ad2017-07-29 11:35:13 -060070 rc = sata_scan(dev);
71 if (rc) {
72 printf("Cannot scan SATA device %d (err=%d)\n", devnum, rc);
73 return CMD_RET_FAILURE;
74 }
75
Simon Glass6c857ad2017-07-29 11:35:13 -060076 return 0;
77#else
78 return sata_initialize() < 0 ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
79#endif
80}
81
Simon Glassed38aef2020-05-10 11:40:03 -060082static int do_sata(struct cmd_tbl *cmdtp, int flag, int argc,
83 char *const argv[])
Dave Liu76596ba2008-03-26 22:49:44 +080084{
85 int rc = 0;
86
Simon Glass6c857ad2017-07-29 11:35:13 -060087 if (argc >= 2) {
88 int devnum = 0;
Nikita Kiryanov44fd1f92014-11-21 12:47:24 +020089
Simon Glass6c857ad2017-07-29 11:35:13 -060090 if (argc == 3)
Simon Glassff9b9032021-07-24 09:03:30 -060091 devnum = (int)dectoul(argv[2], NULL);
Simon Glass6c857ad2017-07-29 11:35:13 -060092 if (!strcmp(argv[1], "stop"))
93 return sata_remove(devnum);
Nikita Kiryanov44fd1f92014-11-21 12:47:24 +020094
Simon Glass6c857ad2017-07-29 11:35:13 -060095 if (!strcmp(argv[1], "init")) {
96 if (sata_curr_device != -1) {
97 rc = sata_remove(devnum);
98 if (rc)
99 return rc;
100 }
101
102 return sata_probe(devnum);
103 }
Nikita Kiryanov44fd1f92014-11-21 12:47:24 +0200104 }
Mike Frysingere66dc082009-01-27 16:12:21 -0500105
106 /* If the user has not yet run `sata init`, do it now */
Tang Yuantianb450f932016-11-21 10:24:20 +0800107 if (sata_curr_device == -1) {
Simon Glass6c857ad2017-07-29 11:35:13 -0600108 rc = sata_probe(0);
Troy Kisky7f00c2f2018-07-27 16:45:26 -0700109 if (rc)
110 return rc;
Simon Glass6c857ad2017-07-29 11:35:13 -0600111 sata_curr_device = 0;
Tang Yuantianb450f932016-11-21 10:24:20 +0800112 }
Mike Frysingere66dc082009-01-27 16:12:21 -0500113
Simon Glassdbfa32c2022-08-11 19:34:59 -0600114 return blk_common_cmd(argc, argv, UCLASS_AHCI, &sata_curr_device);
Dave Liu76596ba2008-03-26 22:49:44 +0800115}
116
117U_BOOT_CMD(
118 sata, 5, 1, do_sata,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600119 "SATA sub system",
Fabio Estevam6be8b832013-02-20 14:35:35 +0000120 "init - init SATA sub system\n"
Simon Glass6c857ad2017-07-29 11:35:13 -0600121 "sata stop [dev] - disable SATA sub system or device\n"
Dave Liu76596ba2008-03-26 22:49:44 +0800122 "sata info - show available SATA devices\n"
123 "sata device [dev] - show or set current device\n"
124 "sata part [dev] - print partition table\n"
125 "sata read addr blk# cnt\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200126 "sata write addr blk# cnt"
127);