Dave Liu | c7057b5 | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <command.h> |
| 15 | #include <part.h> |
| 16 | #include <sata.h> |
| 17 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 18 | static int sata_curr_device = -1; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 19 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 20 | 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] | 21 | { |
| 22 | int rc = 0; |
| 23 | |
Nikita Kiryanov | d957c28 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 24 | if (argc == 2 && strcmp(argv[1], "stop") == 0) |
| 25 | return sata_stop(); |
| 26 | |
| 27 | if (argc == 2 && strcmp(argv[1], "init") == 0) { |
| 28 | if (sata_curr_device != -1) |
| 29 | sata_stop(); |
| 30 | |
Mike Frysinger | cf7e399 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 31 | return sata_initialize(); |
Nikita Kiryanov | d957c28 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 32 | } |
Mike Frysinger | cf7e399 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 33 | |
| 34 | /* If the user has not yet run `sata init`, do it now */ |
Tang Yuantian | aa6ab90 | 2016-11-21 10:24:20 +0800 | [diff] [blame] | 35 | if (sata_curr_device == -1) { |
| 36 | rc = sata_initialize(); |
| 37 | if (rc == -1) |
| 38 | return rc; |
| 39 | sata_curr_device = rc; |
| 40 | } |
Mike Frysinger | cf7e399 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 41 | |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 42 | switch (argc) { |
| 43 | case 0: |
| 44 | case 1: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 45 | return CMD_RET_USAGE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 46 | case 2: |
Simon Glass | 2765c4d | 2016-05-01 11:36:01 -0600 | [diff] [blame] | 47 | if (strncmp(argv[1], "inf", 3) == 0) { |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 48 | blk_list_devices(IF_TYPE_SATA); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 49 | return 0; |
Simon Glass | 2765c4d | 2016-05-01 11:36:01 -0600 | [diff] [blame] | 50 | } else if (strncmp(argv[1], "dev", 3) == 0) { |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 51 | if (blk_print_device_num(IF_TYPE_SATA, |
| 52 | sata_curr_device)) { |
| 53 | printf("\nno SATA devices available\n"); |
| 54 | return CMD_RET_FAILURE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 55 | } |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 56 | return 0; |
Simon Glass | 2765c4d | 2016-05-01 11:36:01 -0600 | [diff] [blame] | 57 | } else if (strncmp(argv[1], "part", 4) == 0) { |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 58 | if (blk_list_part(IF_TYPE_SATA)) |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 59 | puts("\nno SATA devices available\n"); |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 60 | return 0; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 61 | } |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 62 | return CMD_RET_USAGE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 63 | case 3: |
| 64 | if (strncmp(argv[1], "dev", 3) == 0) { |
| 65 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 66 | |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 67 | if (!blk_show_device(IF_TYPE_SATA, dev)) { |
| 68 | sata_curr_device = dev; |
| 69 | printf("... is now current device\n"); |
| 70 | } else { |
| 71 | return CMD_RET_FAILURE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 72 | } |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 73 | return 0; |
| 74 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 75 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 76 | |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 77 | if (blk_print_part_devnum(IF_TYPE_SATA, dev)) { |
| 78 | printf("\nSATA device %d not available\n", |
| 79 | dev); |
| 80 | return CMD_RET_FAILURE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 81 | } |
| 82 | return rc; |
| 83 | } |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 84 | return CMD_RET_USAGE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 85 | |
| 86 | default: /* at least 4 args */ |
| 87 | if (strcmp(argv[1], "read") == 0) { |
| 88 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 89 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 90 | ulong n; |
| 91 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 92 | |
| 93 | printf("\nSATA read: device %d block # %ld, count %ld ... ", |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 94 | sata_curr_device, blk, cnt); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 95 | |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 96 | n = blk_read_devnum(IF_TYPE_SATA, sata_curr_device, blk, |
| 97 | cnt, (ulong *)addr); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 98 | |
| 99 | printf("%ld blocks read: %s\n", |
| 100 | n, (n==cnt) ? "OK" : "ERROR"); |
| 101 | return (n == cnt) ? 0 : 1; |
| 102 | } else if (strcmp(argv[1], "write") == 0) { |
| 103 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 104 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 105 | ulong n; |
| 106 | |
| 107 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 108 | |
| 109 | printf("\nSATA write: device %d block # %ld, count %ld ... ", |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 110 | sata_curr_device, blk, cnt); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 111 | |
Simon Glass | d97dc8a | 2016-05-01 11:36:11 -0600 | [diff] [blame] | 112 | n = blk_write_devnum(IF_TYPE_SATA, sata_curr_device, |
| 113 | blk, cnt, (ulong *)addr); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 114 | |
| 115 | printf("%ld blocks written: %s\n", |
| 116 | n, (n == cnt) ? "OK" : "ERROR"); |
| 117 | return (n == cnt) ? 0 : 1; |
| 118 | } else { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 119 | return CMD_RET_USAGE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return rc; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | U_BOOT_CMD( |
| 127 | sata, 5, 1, do_sata, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 128 | "SATA sub system", |
Fabio Estevam | 85dafbb | 2013-02-20 14:35:35 +0000 | [diff] [blame] | 129 | "init - init SATA sub system\n" |
Nikita Kiryanov | d957c28 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 130 | "sata stop - disable SATA sub system\n" |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 131 | "sata info - show available SATA devices\n" |
| 132 | "sata device [dev] - show or set current device\n" |
| 133 | "sata part [dev] - print partition table\n" |
| 134 | "sata read addr blk# cnt\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 135 | "sata write addr blk# cnt" |
| 136 | ); |