Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000-2005, DENX Software Engineering |
| 3 | * Wolfgang Denk <wd@denx.de> |
| 4 | * Copyright (C) Procsys. All rights reserved. |
| 5 | * Mushtaq Khan <mushtaq_k@procsys.com> |
| 6 | * <mushtaqk_921@yahoo.co.in> |
| 7 | * Copyright (C) 2008 Freescale Semiconductor, Inc. |
| 8 | * Dave Liu <daveliu@freescale.com> |
| 9 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Simon Glass | 6c857ad | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 14 | #include <ahci.h> |
| 15 | #include <dm.h> |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 16 | #include <command.h> |
| 17 | #include <part.h> |
| 18 | #include <sata.h> |
Simon Glass | 6c857ad | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 19 | #include <dm/device-internal.h> |
| 20 | #include <dm/uclass-internal.h> |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 21 | |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 22 | static int sata_curr_device = -1; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 23 | |
Simon Glass | 6c857ad | 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 | |
| 30 | rc = uclass_find_device(UCLASS_AHCI, devnum, &dev); |
| 31 | if (!rc && !dev) |
| 32 | rc = uclass_find_first_device(UCLASS_AHCI, &dev); |
| 33 | if (rc || !dev) { |
| 34 | printf("Cannot find SATA device %d (err=%d)\n", devnum, rc); |
| 35 | return CMD_RET_FAILURE; |
| 36 | } |
| 37 | |
| 38 | rc = device_remove(dev, DM_REMOVE_NORMAL); |
| 39 | if (rc) { |
| 40 | printf("Cannot remove SATA device '%s' (err=%d)\n", dev->name, |
| 41 | rc); |
| 42 | return CMD_RET_FAILURE; |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | #else |
| 47 | return sata_stop(); |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | int sata_probe(int devnum) |
| 52 | { |
| 53 | #ifdef CONFIG_AHCI |
| 54 | struct udevice *dev; |
| 55 | struct udevice *blk; |
| 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 | } |
| 65 | rc = sata_scan(dev); |
| 66 | if (rc) { |
| 67 | printf("Cannot scan SATA device %d (err=%d)\n", devnum, rc); |
| 68 | return CMD_RET_FAILURE; |
| 69 | } |
| 70 | |
| 71 | rc = blk_get_from_parent(dev, &blk); |
| 72 | if (!rc) { |
| 73 | struct blk_desc *desc = dev_get_uclass_platdata(blk); |
| 74 | |
| 75 | if (desc->lba > 0 && desc->blksz > 0) |
| 76 | part_init(desc); |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | #else |
| 81 | return sata_initialize() < 0 ? CMD_RET_FAILURE : CMD_RET_SUCCESS; |
| 82 | #endif |
| 83 | } |
| 84 | |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 85 | 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] | 86 | { |
| 87 | int rc = 0; |
| 88 | |
Simon Glass | 6c857ad | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 89 | if (argc >= 2) { |
| 90 | int devnum = 0; |
Nikita Kiryanov | 44fd1f9 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 91 | |
Simon Glass | 6c857ad | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 92 | if (argc == 3) |
| 93 | devnum = (int)simple_strtoul(argv[2], NULL, 10); |
| 94 | if (!strcmp(argv[1], "stop")) |
| 95 | return sata_remove(devnum); |
Nikita Kiryanov | 44fd1f9 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 96 | |
Simon Glass | 6c857ad | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 97 | if (!strcmp(argv[1], "init")) { |
| 98 | if (sata_curr_device != -1) { |
| 99 | rc = sata_remove(devnum); |
| 100 | if (rc) |
| 101 | return rc; |
| 102 | } |
| 103 | |
| 104 | return sata_probe(devnum); |
| 105 | } |
Nikita Kiryanov | 44fd1f9 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 106 | } |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 107 | |
| 108 | /* If the user has not yet run `sata init`, do it now */ |
Tang Yuantian | b450f93 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 109 | if (sata_curr_device == -1) { |
Simon Glass | 6c857ad | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 110 | rc = sata_probe(0); |
| 111 | if (rc < 0) |
Gary Bisson | 06a9880 | 2017-01-11 16:51:53 +0100 | [diff] [blame] | 112 | return CMD_RET_FAILURE; |
Simon Glass | 6c857ad | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 113 | sata_curr_device = 0; |
Tang Yuantian | b450f93 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 114 | } |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 115 | |
Simon Glass | 2710c6f | 2017-07-29 11:34:55 -0600 | [diff] [blame] | 116 | return blk_common_cmd(argc, argv, IF_TYPE_SATA, &sata_curr_device); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | U_BOOT_CMD( |
| 120 | sata, 5, 1, do_sata, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 121 | "SATA sub system", |
Fabio Estevam | 6be8b83 | 2013-02-20 14:35:35 +0000 | [diff] [blame] | 122 | "init - init SATA sub system\n" |
Simon Glass | 6c857ad | 2017-07-29 11:35:13 -0600 | [diff] [blame] | 123 | "sata stop [dev] - disable SATA sub system or device\n" |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 124 | "sata info - show available SATA devices\n" |
| 125 | "sata device [dev] - show or set current device\n" |
| 126 | "sata part [dev] - print partition table\n" |
| 127 | "sata read addr blk# cnt\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 128 | "sata write addr blk# cnt" |
| 129 | ); |