blob: 8e36de107e9a91a01376164accfdb96c4fd235a0 [file] [log] [blame]
wdenke887afc2002-08-27 09:44:07 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenke887afc2002-08-27 09:44:07 +00006 */
7
8/*
9 * SCSI support.
10 */
wdenke887afc2002-08-27 09:44:07 +000011#include <common.h>
12#include <command.h>
wdenke887afc2002-08-27 09:44:07 +000013#include <scsi.h>
wdenke887afc2002-08-27 09:44:07 +000014
15static int scsi_curr_dev; /* current device */
16
Simon Glassc8e89ab2016-05-01 11:36:09 -060017/*
wdenke887afc2002-08-27 09:44:07 +000018 * scsi boot command intepreter. Derived from diskboot
19 */
Michal Simekff238112016-11-30 12:18:36 +010020static int do_scsiboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenke887afc2002-08-27 09:44:07 +000021{
Rob Herringa20cbf92012-09-21 04:02:30 +000022 return common_diskboot(cmdtp, "scsi", argc, argv);
wdenke887afc2002-08-27 09:44:07 +000023}
24
Simon Glassc8e89ab2016-05-01 11:36:09 -060025/*
wdenke887afc2002-08-27 09:44:07 +000026 * scsi command intepreter
27 */
Michal Simekff238112016-11-30 12:18:36 +010028static int do_scsi(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenke887afc2002-08-27 09:44:07 +000029{
Michal Simek5dc196f2016-11-30 12:12:31 +010030 int ret;
31
wdenke887afc2002-08-27 09:44:07 +000032 switch (argc) {
Simon Glassa06dfc72011-12-10 08:44:01 +000033 case 0:
34 case 1:
35 return CMD_RET_USAGE;
Simon Glassa06dfc72011-12-10 08:44:01 +000036 case 2:
Simon Glass5c7bf8e2016-05-01 11:35:58 -060037 if (strncmp(argv[1], "res", 3) == 0) {
38 printf("\nReset SCSI\n");
Bin Meng8aa95762017-06-17 06:36:00 -070039#ifndef CONFIG_DM_SCSI
Simon Glass11b2b622017-06-14 21:28:40 -060040 scsi_bus_reset(NULL);
Bin Meng8aa95762017-06-17 06:36:00 -070041#endif
Simon Glass48228732017-06-14 21:28:41 -060042 ret = scsi_scan(true);
Michal Simek5dc196f2016-11-30 12:12:31 +010043 if (ret)
44 return CMD_RET_FAILURE;
45 return ret;
Simon Glass5c7bf8e2016-05-01 11:35:58 -060046 }
47 if (strncmp(argv[1], "inf", 3) == 0) {
Simon Glassc8e89ab2016-05-01 11:36:09 -060048 blk_list_devices(IF_TYPE_SCSI);
Simon Glass5c7bf8e2016-05-01 11:35:58 -060049 return 0;
50 }
51 if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glassc8e89ab2016-05-01 11:36:09 -060052 if (blk_print_device_num(IF_TYPE_SCSI, scsi_curr_dev)) {
Simon Glass5c7bf8e2016-05-01 11:35:58 -060053 printf("\nno SCSI devices available\n");
Simon Glassc8e89ab2016-05-01 11:36:09 -060054 return CMD_RET_FAILURE;
wdenke887afc2002-08-27 09:44:07 +000055 }
Simon Glassc8e89ab2016-05-01 11:36:09 -060056
Simon Glass5c7bf8e2016-05-01 11:35:58 -060057 return 0;
58 }
59 if (strncmp(argv[1], "scan", 4) == 0) {
Simon Glass48228732017-06-14 21:28:41 -060060 ret = scsi_scan(true);
Michal Simek5dc196f2016-11-30 12:12:31 +010061 if (ret)
62 return CMD_RET_FAILURE;
63 return ret;
Simon Glass5c7bf8e2016-05-01 11:35:58 -060064 }
65 if (strncmp(argv[1], "part", 4) == 0) {
Simon Glassc8e89ab2016-05-01 11:36:09 -060066 if (blk_list_part(IF_TYPE_SCSI))
Simon Glass5c7bf8e2016-05-01 11:35:58 -060067 printf("\nno SCSI devices available\n");
Simon Glassc8e89ab2016-05-01 11:36:09 -060068 return 0;
Simon Glass5c7bf8e2016-05-01 11:35:58 -060069 }
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 Glassc8e89ab2016-05-01 11:36:09 -060074
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 Glass8f81fa92016-05-01 11:35:57 -060080 }
Simon Glass5c7bf8e2016-05-01 11:35:58 -060081 return 0;
82 }
83 if (strncmp(argv[1], "part", 4) == 0) {
84 int dev = (int)simple_strtoul(argv[2], NULL, 10);
Simon Glassc8e89ab2016-05-01 11:36:09 -060085
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 Glass5c7bf8e2016-05-01 11:35:58 -060092 }
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 Glassc8e89ab2016-05-01 11:36:09 -0600101
Simon Glass5c7bf8e2016-05-01 11:35:58 -0600102 printf("\nSCSI read: device %d block # %ld, count %ld ... ",
103 scsi_curr_dev, blk, cnt);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600104 n = blk_read_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk,
105 cnt, (ulong *)addr);
Simon Glass5c7bf8e2016-05-01 11:35:58 -0600106 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 Glassc8e89ab2016-05-01 11:36:09 -0600114
Simon Glass5c7bf8e2016-05-01 11:35:58 -0600115 printf("\nSCSI write: device %d block # %ld, count %ld ... ",
116 scsi_curr_dev, blk, cnt);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600117 n = blk_write_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk,
118 cnt, (ulong *)addr);
Simon Glass5c7bf8e2016-05-01 11:35:58 -0600119 printf("%ld blocks written: %s\n", n,
120 n == cnt ? "OK" : "ERROR");
121 return 0;
122 }
Simon Glass8f81fa92016-05-01 11:35:57 -0600123 } /* switch */
124 return CMD_RET_USAGE;
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000125}
126
Simon Glass8f81fa92016-05-01 11:35:57 -0600127U_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
141U_BOOT_CMD(
142 scsiboot, 3, 1, do_scsiboot,
143 "boot from SCSI device",
144 "loadAddr dev:part"
145);