blob: 4c53022ff60a365ea030e80c3e9b112d55f9b50f [file] [log] [blame]
Dave Liu76596ba2008-03-26 22:49:44 +08001/*
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 Denkd79de1d2013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Dave Liu76596ba2008-03-26 22:49:44 +080011 */
12
13#include <common.h>
14#include <command.h>
15#include <part.h>
16#include <sata.h>
17
Kim Phillipsdc00a682012-10-29 13:34:31 +000018static int sata_curr_device = -1;
Dave Liu76596ba2008-03-26 22:49:44 +080019
Kim Phillipsdc00a682012-10-29 13:34:31 +000020static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Dave Liu76596ba2008-03-26 22:49:44 +080021{
22 int rc = 0;
23
Nikita Kiryanov44fd1f92014-11-21 12:47:24 +020024 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
Gary Bisson06a98802017-01-11 16:51:53 +010031 return (sata_initialize() < 0) ?
32 CMD_RET_FAILURE : CMD_RET_SUCCESS;
Nikita Kiryanov44fd1f92014-11-21 12:47:24 +020033 }
Mike Frysingere66dc082009-01-27 16:12:21 -050034
35 /* If the user has not yet run `sata init`, do it now */
Tang Yuantianb450f932016-11-21 10:24:20 +080036 if (sata_curr_device == -1) {
37 rc = sata_initialize();
38 if (rc == -1)
Gary Bisson06a98802017-01-11 16:51:53 +010039 return CMD_RET_FAILURE;
Tang Yuantianb450f932016-11-21 10:24:20 +080040 sata_curr_device = rc;
41 }
Mike Frysingere66dc082009-01-27 16:12:21 -050042
Dave Liu76596ba2008-03-26 22:49:44 +080043 switch (argc) {
44 case 0:
45 case 1:
Simon Glassa06dfc72011-12-10 08:44:01 +000046 return CMD_RET_USAGE;
Dave Liu76596ba2008-03-26 22:49:44 +080047 case 2:
Simon Glass9475e7e2016-05-01 11:36:01 -060048 if (strncmp(argv[1], "inf", 3) == 0) {
Simon Glass3665a402016-05-01 11:36:11 -060049 blk_list_devices(IF_TYPE_SATA);
Dave Liu76596ba2008-03-26 22:49:44 +080050 return 0;
Simon Glass9475e7e2016-05-01 11:36:01 -060051 } else if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glass3665a402016-05-01 11:36:11 -060052 if (blk_print_device_num(IF_TYPE_SATA,
53 sata_curr_device)) {
54 printf("\nno SATA devices available\n");
55 return CMD_RET_FAILURE;
Dave Liu76596ba2008-03-26 22:49:44 +080056 }
Dave Liu76596ba2008-03-26 22:49:44 +080057 return 0;
Simon Glass9475e7e2016-05-01 11:36:01 -060058 } else if (strncmp(argv[1], "part", 4) == 0) {
Simon Glass3665a402016-05-01 11:36:11 -060059 if (blk_list_part(IF_TYPE_SATA))
Dave Liu76596ba2008-03-26 22:49:44 +080060 puts("\nno SATA devices available\n");
Simon Glass3665a402016-05-01 11:36:11 -060061 return 0;
Dave Liu76596ba2008-03-26 22:49:44 +080062 }
Simon Glassa06dfc72011-12-10 08:44:01 +000063 return CMD_RET_USAGE;
Dave Liu76596ba2008-03-26 22:49:44 +080064 case 3:
65 if (strncmp(argv[1], "dev", 3) == 0) {
66 int dev = (int)simple_strtoul(argv[2], NULL, 10);
67
Simon Glass3665a402016-05-01 11:36:11 -060068 if (!blk_show_device(IF_TYPE_SATA, dev)) {
69 sata_curr_device = dev;
70 printf("... is now current device\n");
71 } else {
72 return CMD_RET_FAILURE;
Dave Liu76596ba2008-03-26 22:49:44 +080073 }
Dave Liu76596ba2008-03-26 22:49:44 +080074 return 0;
75 } else if (strncmp(argv[1], "part", 4) == 0) {
76 int dev = (int)simple_strtoul(argv[2], NULL, 10);
77
Simon Glass3665a402016-05-01 11:36:11 -060078 if (blk_print_part_devnum(IF_TYPE_SATA, dev)) {
79 printf("\nSATA device %d not available\n",
80 dev);
81 return CMD_RET_FAILURE;
Dave Liu76596ba2008-03-26 22:49:44 +080082 }
83 return rc;
84 }
Simon Glassa06dfc72011-12-10 08:44:01 +000085 return CMD_RET_USAGE;
Dave Liu76596ba2008-03-26 22:49:44 +080086
87 default: /* at least 4 args */
88 if (strcmp(argv[1], "read") == 0) {
89 ulong addr = simple_strtoul(argv[2], NULL, 16);
90 ulong cnt = simple_strtoul(argv[4], NULL, 16);
91 ulong n;
92 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
93
94 printf("\nSATA read: device %d block # %ld, count %ld ... ",
Mike Frysingerb6ade252009-06-14 21:35:22 -040095 sata_curr_device, blk, cnt);
Dave Liu76596ba2008-03-26 22:49:44 +080096
Simon Glass3665a402016-05-01 11:36:11 -060097 n = blk_read_devnum(IF_TYPE_SATA, sata_curr_device, blk,
98 cnt, (ulong *)addr);
Dave Liu76596ba2008-03-26 22:49:44 +080099
100 printf("%ld blocks read: %s\n",
101 n, (n==cnt) ? "OK" : "ERROR");
102 return (n == cnt) ? 0 : 1;
103 } else if (strcmp(argv[1], "write") == 0) {
104 ulong addr = simple_strtoul(argv[2], NULL, 16);
105 ulong cnt = simple_strtoul(argv[4], NULL, 16);
106 ulong n;
107
108 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
109
110 printf("\nSATA write: device %d block # %ld, count %ld ... ",
Mike Frysingerb6ade252009-06-14 21:35:22 -0400111 sata_curr_device, blk, cnt);
Dave Liu76596ba2008-03-26 22:49:44 +0800112
Simon Glass3665a402016-05-01 11:36:11 -0600113 n = blk_write_devnum(IF_TYPE_SATA, sata_curr_device,
114 blk, cnt, (ulong *)addr);
Dave Liu76596ba2008-03-26 22:49:44 +0800115
116 printf("%ld blocks written: %s\n",
117 n, (n == cnt) ? "OK" : "ERROR");
118 return (n == cnt) ? 0 : 1;
119 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +0000120 return CMD_RET_USAGE;
Dave Liu76596ba2008-03-26 22:49:44 +0800121 }
122
123 return rc;
124 }
125}
126
127U_BOOT_CMD(
128 sata, 5, 1, do_sata,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600129 "SATA sub system",
Fabio Estevam6be8b832013-02-20 14:35:35 +0000130 "init - init SATA sub system\n"
Nikita Kiryanov44fd1f92014-11-21 12:47:24 +0200131 "sata stop - disable SATA sub system\n"
Dave Liu76596ba2008-03-26 22:49:44 +0800132 "sata info - show available SATA devices\n"
133 "sata device [dev] - show or set current device\n"
134 "sata part [dev] - print partition table\n"
135 "sata read addr blk# cnt\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200136 "sata write addr blk# cnt"
137);