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