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