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"); |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 39 | scsi_bus_reset(NULL); |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 40 | ret = scsi_scan(true); |
Michal Simek | 5dc196f | 2016-11-30 12:12:31 +0100 | [diff] [blame] | 41 | if (ret) |
| 42 | return CMD_RET_FAILURE; |
| 43 | return ret; |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 44 | } |
| 45 | if (strncmp(argv[1], "inf", 3) == 0) { |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 46 | blk_list_devices(IF_TYPE_SCSI); |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | if (strncmp(argv[1], "dev", 3) == 0) { |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 50 | if (blk_print_device_num(IF_TYPE_SCSI, scsi_curr_dev)) { |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 51 | printf("\nno SCSI devices available\n"); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 52 | return CMD_RET_FAILURE; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 53 | } |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 54 | |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | if (strncmp(argv[1], "scan", 4) == 0) { |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 58 | ret = scsi_scan(true); |
Michal Simek | 5dc196f | 2016-11-30 12:12:31 +0100 | [diff] [blame] | 59 | if (ret) |
| 60 | return CMD_RET_FAILURE; |
| 61 | return ret; |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 62 | } |
| 63 | if (strncmp(argv[1], "part", 4) == 0) { |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 64 | if (blk_list_part(IF_TYPE_SCSI)) |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 65 | printf("\nno SCSI devices available\n"); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 66 | return 0; |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 67 | } |
| 68 | return CMD_RET_USAGE; |
| 69 | case 3: |
| 70 | if (strncmp(argv[1], "dev", 3) == 0) { |
| 71 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 72 | |
| 73 | if (!blk_show_device(IF_TYPE_SCSI, dev)) { |
| 74 | scsi_curr_dev = dev; |
| 75 | printf("... is now current device\n"); |
| 76 | } else { |
| 77 | return CMD_RET_FAILURE; |
Simon Glass | 8f81fa9 | 2016-05-01 11:35:57 -0600 | [diff] [blame] | 78 | } |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | if (strncmp(argv[1], "part", 4) == 0) { |
| 82 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 83 | |
| 84 | if (blk_print_part_devnum(IF_TYPE_SCSI, dev)) { |
| 85 | printf("\nSCSI device %d not available\n", |
| 86 | dev); |
| 87 | return CMD_RET_FAILURE; |
| 88 | } |
| 89 | return 0; |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 90 | } |
| 91 | return CMD_RET_USAGE; |
| 92 | default: |
| 93 | /* at least 4 args */ |
| 94 | if (strcmp(argv[1], "read") == 0) { |
| 95 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 96 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 97 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 98 | ulong n; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 99 | |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 100 | printf("\nSCSI read: device %d block # %ld, count %ld ... ", |
| 101 | scsi_curr_dev, blk, cnt); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 102 | n = blk_read_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk, |
| 103 | cnt, (ulong *)addr); |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 104 | printf("%ld blocks read: %s\n", n, |
| 105 | n == cnt ? "OK" : "ERROR"); |
| 106 | return 0; |
| 107 | } else if (strcmp(argv[1], "write") == 0) { |
| 108 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 109 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 110 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 111 | ulong n; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 112 | |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 113 | printf("\nSCSI write: device %d block # %ld, count %ld ... ", |
| 114 | scsi_curr_dev, blk, cnt); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 115 | n = blk_write_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk, |
| 116 | cnt, (ulong *)addr); |
Simon Glass | 5c7bf8e | 2016-05-01 11:35:58 -0600 | [diff] [blame] | 117 | printf("%ld blocks written: %s\n", n, |
| 118 | n == cnt ? "OK" : "ERROR"); |
| 119 | return 0; |
| 120 | } |
Simon Glass | 8f81fa9 | 2016-05-01 11:35:57 -0600 | [diff] [blame] | 121 | } /* switch */ |
| 122 | return CMD_RET_USAGE; |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Simon Glass | 8f81fa9 | 2016-05-01 11:35:57 -0600 | [diff] [blame] | 125 | U_BOOT_CMD( |
| 126 | scsi, 5, 1, do_scsi, |
| 127 | "SCSI sub-system", |
| 128 | "reset - reset SCSI controller\n" |
| 129 | "scsi info - show available SCSI devices\n" |
| 130 | "scsi scan - (re-)scan SCSI bus\n" |
| 131 | "scsi device [dev] - show or set current device\n" |
| 132 | "scsi part [dev] - print partition table of one or all SCSI devices\n" |
| 133 | "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n" |
| 134 | " to memory address `addr'\n" |
| 135 | "scsi write addr blk# cnt - write `cnt' blocks starting at block\n" |
| 136 | " `blk#' from memory address `addr'" |
| 137 | ); |
| 138 | |
| 139 | U_BOOT_CMD( |
| 140 | scsiboot, 3, 1, do_scsiboot, |
| 141 | "boot from SCSI device", |
| 142 | "loadAddr dev:part" |
| 143 | ); |