wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Denis Peter, MPL AG Switzerland |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * SCSI support. |
| 10 | */ |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <command.h> |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 13 | #include <scsi.h> |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 14 | |
| 15 | static int scsi_curr_dev; /* current device */ |
| 16 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 17 | /* |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 18 | * scsi boot command intepreter. Derived from diskboot |
| 19 | */ |
Michal Simek | ff23811 | 2016-11-30 12:18:36 +0100 | [diff] [blame] | 20 | static int do_scsiboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 21 | { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 22 | return common_diskboot(cmdtp, "scsi", argc, argv); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 25 | /* |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 26 | * scsi command intepreter |
| 27 | */ |
Michal Simek | ff23811 | 2016-11-30 12:18:36 +0100 | [diff] [blame] | 28 | static int do_scsi(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 29 | { |
Michal Simek | 5dc196f | 2016-11-30 12:12:31 +0100 | [diff] [blame] | 30 | int ret; |
| 31 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 32 | switch (argc) { |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 33 | case 0: |
| 34 | case 1: |
| 35 | return CMD_RET_USAGE; |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 36 | case 2: |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 37 | if (strncmp(argv[1], "res", 3) == 0) { |
| 38 | printf("\nReset SCSI\n"); |
Bin Meng | 8aa9576 | 2017-06-17 06:36:00 -0700 | [diff] [blame^] | 39 | #ifndef CONFIG_DM_SCSI |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 40 | scsi_bus_reset(NULL); |
Bin Meng | 8aa9576 | 2017-06-17 06:36:00 -0700 | [diff] [blame^] | 41 | #endif |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 42 | ret = scsi_scan(true); |
Michal Simek | 5dc196f | 2016-11-30 12:12:31 +0100 | [diff] [blame] | 43 | if (ret) |
| 44 | return CMD_RET_FAILURE; |
| 45 | return ret; |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 46 | } |
| 47 | if (strncmp(argv[1], "inf", 3) == 0) { |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 48 | blk_list_devices(IF_TYPE_SCSI); |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | if (strncmp(argv[1], "dev", 3) == 0) { |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 52 | if (blk_print_device_num(IF_TYPE_SCSI, scsi_curr_dev)) { |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 53 | printf("\nno SCSI devices available\n"); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 54 | return CMD_RET_FAILURE; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 55 | } |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 56 | |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | if (strncmp(argv[1], "scan", 4) == 0) { |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 60 | ret = scsi_scan(true); |
Michal Simek | 5dc196f | 2016-11-30 12:12:31 +0100 | [diff] [blame] | 61 | if (ret) |
| 62 | return CMD_RET_FAILURE; |
| 63 | return ret; |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 64 | } |
| 65 | if (strncmp(argv[1], "part", 4) == 0) { |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 66 | if (blk_list_part(IF_TYPE_SCSI)) |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 67 | printf("\nno SCSI devices available\n"); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 68 | return 0; |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 69 | } |
| 70 | return CMD_RET_USAGE; |
| 71 | case 3: |
| 72 | if (strncmp(argv[1], "dev", 3) == 0) { |
| 73 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 74 | |
| 75 | if (!blk_show_device(IF_TYPE_SCSI, dev)) { |
| 76 | scsi_curr_dev = dev; |
| 77 | printf("... is now current device\n"); |
| 78 | } else { |
| 79 | return CMD_RET_FAILURE; |
Simon Glass | 8f81fa9 | 2016-05-01 11:35:57 -0600 | [diff] [blame] | 80 | } |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | if (strncmp(argv[1], "part", 4) == 0) { |
| 84 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 85 | |
| 86 | if (blk_print_part_devnum(IF_TYPE_SCSI, dev)) { |
| 87 | printf("\nSCSI device %d not available\n", |
| 88 | dev); |
| 89 | return CMD_RET_FAILURE; |
| 90 | } |
| 91 | return 0; |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 92 | } |
| 93 | return CMD_RET_USAGE; |
| 94 | default: |
| 95 | /* at least 4 args */ |
| 96 | if (strcmp(argv[1], "read") == 0) { |
| 97 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 98 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 99 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 100 | ulong n; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 101 | |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 102 | printf("\nSCSI read: device %d block # %ld, count %ld ... ", |
| 103 | scsi_curr_dev, blk, cnt); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 104 | n = blk_read_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk, |
| 105 | cnt, (ulong *)addr); |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 106 | printf("%ld blocks read: %s\n", n, |
| 107 | n == cnt ? "OK" : "ERROR"); |
| 108 | return 0; |
| 109 | } else if (strcmp(argv[1], "write") == 0) { |
| 110 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 111 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 112 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 113 | ulong n; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 114 | |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 115 | printf("\nSCSI write: device %d block # %ld, count %ld ... ", |
| 116 | scsi_curr_dev, blk, cnt); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 117 | n = blk_write_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk, |
| 118 | cnt, (ulong *)addr); |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 119 | printf("%ld blocks written: %s\n", n, |
| 120 | n == cnt ? "OK" : "ERROR"); |
| 121 | return 0; |
| 122 | } |
Simon Glass | 8f81fa9 | 2016-05-01 11:35:57 -0600 | [diff] [blame] | 123 | } /* switch */ |
| 124 | return CMD_RET_USAGE; |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Simon Glass | 8f81fa9 | 2016-05-01 11:35:57 -0600 | [diff] [blame] | 127 | U_BOOT_CMD( |
| 128 | scsi, 5, 1, do_scsi, |
| 129 | "SCSI sub-system", |
| 130 | "reset - reset SCSI controller\n" |
| 131 | "scsi info - show available SCSI devices\n" |
| 132 | "scsi scan - (re-)scan SCSI bus\n" |
| 133 | "scsi device [dev] - show or set current device\n" |
| 134 | "scsi part [dev] - print partition table of one or all SCSI devices\n" |
| 135 | "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n" |
| 136 | " to memory address `addr'\n" |
| 137 | "scsi write addr blk# cnt - write `cnt' blocks starting at block\n" |
| 138 | " `blk#' from memory address `addr'" |
| 139 | ); |
| 140 | |
| 141 | U_BOOT_CMD( |
| 142 | scsiboot, 3, 1, do_scsiboot, |
| 143 | "boot from SCSI device", |
| 144 | "loadAddr dev:part" |
| 145 | ); |