Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 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> |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 13 | #include <ahci.h> |
| 14 | #include <dm.h> |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 15 | #include <command.h> |
| 16 | #include <part.h> |
| 17 | #include <sata.h> |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
| 19 | #include <dm/uclass-internal.h> |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 20 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 21 | static int sata_curr_device = -1; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 22 | |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 23 | int sata_remove(int devnum) |
| 24 | { |
| 25 | #ifdef CONFIG_AHCI |
| 26 | struct udevice *dev; |
| 27 | int rc; |
| 28 | |
Peng Ma | 2d7818d | 2019-12-04 10:36:47 +0000 | [diff] [blame] | 29 | blk_unbind_all(IF_TYPE_SATA); |
| 30 | |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 31 | 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 | |
| 52 | int sata_probe(int devnum) |
| 53 | { |
| 54 | #ifdef CONFIG_AHCI |
| 55 | struct udevice *dev; |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 56 | 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 Ziswiler | b3860bf | 2019-02-01 16:01:08 +0100 | [diff] [blame] | 65 | if (!dev) { |
| 66 | printf("No SATA device found!\n"); |
| 67 | return CMD_RET_FAILURE; |
| 68 | } |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 69 | 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 Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 75 | return 0; |
| 76 | #else |
| 77 | return sata_initialize() < 0 ? CMD_RET_FAILURE : CMD_RET_SUCCESS; |
| 78 | #endif |
| 79 | } |
| 80 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 81 | static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 82 | { |
| 83 | int rc = 0; |
| 84 | |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 85 | if (argc >= 2) { |
| 86 | int devnum = 0; |
Nikita Kiryanov | d957c28 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 87 | |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 88 | if (argc == 3) |
| 89 | devnum = (int)simple_strtoul(argv[2], NULL, 10); |
| 90 | if (!strcmp(argv[1], "stop")) |
| 91 | return sata_remove(devnum); |
Nikita Kiryanov | d957c28 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 92 | |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 93 | if (!strcmp(argv[1], "init")) { |
| 94 | if (sata_curr_device != -1) { |
| 95 | rc = sata_remove(devnum); |
| 96 | if (rc) |
| 97 | return rc; |
| 98 | } |
| 99 | |
| 100 | return sata_probe(devnum); |
| 101 | } |
Nikita Kiryanov | d957c28 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 102 | } |
Mike Frysinger | cf7e399 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 103 | |
| 104 | /* If the user has not yet run `sata init`, do it now */ |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 105 | if (sata_curr_device == -1) { |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 106 | rc = sata_probe(0); |
Troy Kisky | 7e83f1d | 2018-07-27 16:45:26 -0700 | [diff] [blame] | 107 | if (rc) |
| 108 | return rc; |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 109 | sata_curr_device = 0; |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 110 | } |
Mike Frysinger | cf7e399 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 111 | |
Simon Glass | e29e71e | 2017-07-29 11:34:55 -0600 | [diff] [blame] | 112 | return blk_common_cmd(argc, argv, IF_TYPE_SATA, &sata_curr_device); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | U_BOOT_CMD( |
| 116 | sata, 5, 1, do_sata, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 117 | "SATA sub system", |
Fabio Estevam | 85dafbb | 2013-02-20 14:35:35 +0000 | [diff] [blame] | 118 | "init - init SATA sub system\n" |
Simon Glass | f19f1ec | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 119 | "sata stop [dev] - disable SATA sub system or device\n" |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 120 | "sata info - show available SATA devices\n" |
| 121 | "sata device [dev] - show or set current device\n" |
| 122 | "sata part [dev] - print partition table\n" |
| 123 | "sata read addr blk# cnt\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 124 | "sata write addr blk# cnt" |
| 125 | ); |