Dave Liu | 76596ba | 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 | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Dave Liu | 76596ba | 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 | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 18 | static int sata_curr_device = -1; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 19 | struct blk_desc sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE]; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 20 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 21 | static unsigned long sata_bread(struct blk_desc *block_dev, lbaint_t start, |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 22 | lbaint_t blkcnt, void *dst) |
| 23 | { |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 24 | return sata_read(block_dev->devnum, start, blkcnt, dst); |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 27 | static unsigned long sata_bwrite(struct blk_desc *block_dev, lbaint_t start, |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 28 | lbaint_t blkcnt, const void *buffer) |
| 29 | { |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 30 | return sata_write(block_dev->devnum, start, blkcnt, buffer); |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 33 | int __sata_initialize(void) |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 34 | { |
| 35 | int rc; |
| 36 | int i; |
| 37 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 38 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 39 | memset(&sata_dev_desc[i], 0, sizeof(struct blk_desc)); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 40 | sata_dev_desc[i].if_type = IF_TYPE_SATA; |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 41 | sata_dev_desc[i].devnum = i; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 42 | sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN; |
| 43 | sata_dev_desc[i].type = DEV_TYPE_HARDDISK; |
| 44 | sata_dev_desc[i].lba = 0; |
| 45 | sata_dev_desc[i].blksz = 512; |
Egbert Eich | 2eec2ab | 2013-04-09 21:11:56 +0000 | [diff] [blame] | 46 | sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz); |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 47 | sata_dev_desc[i].block_read = sata_bread; |
| 48 | sata_dev_desc[i].block_write = sata_bwrite; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 49 | |
| 50 | rc = init_sata(i); |
Stefano Babic | f889c33 | 2012-02-22 00:24:37 +0000 | [diff] [blame] | 51 | if (!rc) { |
| 52 | rc = scan_sata(i); |
| 53 | if (!rc && (sata_dev_desc[i].lba > 0) && |
| 54 | (sata_dev_desc[i].blksz > 0)) |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 55 | part_init(&sata_dev_desc[i]); |
Stefano Babic | f889c33 | 2012-02-22 00:24:37 +0000 | [diff] [blame] | 56 | } |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 57 | } |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 58 | sata_curr_device = 0; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 59 | return rc; |
| 60 | } |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 61 | int sata_initialize(void) __attribute__((weak,alias("__sata_initialize"))); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 62 | |
Nikita Kiryanov | 44fd1f9 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 63 | __weak int __sata_stop(void) |
| 64 | { |
| 65 | int i, err = 0; |
| 66 | |
| 67 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) |
| 68 | err |= reset_sata(i); |
| 69 | |
| 70 | if (err) |
| 71 | printf("Could not reset some SATA devices\n"); |
| 72 | |
| 73 | return err; |
| 74 | } |
| 75 | int sata_stop(void) __attribute__((weak, alias("__sata_stop"))); |
| 76 | |
Matthew McClintock | 6252b4f | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 77 | #ifdef CONFIG_PARTITIONS |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 78 | struct blk_desc *sata_get_dev(int dev) |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 79 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 80 | return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 81 | } |
Matthew McClintock | 6252b4f | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 82 | #endif |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 83 | |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 84 | static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 85 | { |
| 86 | int rc = 0; |
| 87 | |
Nikita Kiryanov | 44fd1f9 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 88 | if (argc == 2 && strcmp(argv[1], "stop") == 0) |
| 89 | return sata_stop(); |
| 90 | |
| 91 | if (argc == 2 && strcmp(argv[1], "init") == 0) { |
| 92 | if (sata_curr_device != -1) |
| 93 | sata_stop(); |
| 94 | |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 95 | return sata_initialize(); |
Nikita Kiryanov | 44fd1f9 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 96 | } |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 97 | |
| 98 | /* If the user has not yet run `sata init`, do it now */ |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 99 | if (sata_curr_device == -1) |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 100 | if (sata_initialize()) |
| 101 | return 1; |
| 102 | |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 103 | switch (argc) { |
| 104 | case 0: |
| 105 | case 1: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 106 | return CMD_RET_USAGE; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 107 | case 2: |
Simon Glass | 9475e7e | 2016-05-01 11:36:01 -0600 | [diff] [blame^] | 108 | if (strncmp(argv[1], "inf", 3) == 0) { |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 109 | int i; |
Simon Glass | 9475e7e | 2016-05-01 11:36:01 -0600 | [diff] [blame^] | 110 | |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 111 | putc('\n'); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 112 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) { |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 113 | if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN) |
| 114 | continue; |
Simon Glass | 9475e7e | 2016-05-01 11:36:01 -0600 | [diff] [blame^] | 115 | printf("SATA device %d: ", i); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 116 | dev_print(&sata_dev_desc[i]); |
| 117 | } |
| 118 | return 0; |
Simon Glass | 9475e7e | 2016-05-01 11:36:01 -0600 | [diff] [blame^] | 119 | } else if (strncmp(argv[1], "dev", 3) == 0) { |
| 120 | if (sata_curr_device < 0 || |
| 121 | sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE) { |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 122 | puts("\nno SATA devices available\n"); |
| 123 | return 1; |
| 124 | } |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 125 | printf("\nSATA device %d: ", sata_curr_device); |
| 126 | dev_print(&sata_dev_desc[sata_curr_device]); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 127 | return 0; |
Simon Glass | 9475e7e | 2016-05-01 11:36:01 -0600 | [diff] [blame^] | 128 | } else if (strncmp(argv[1], "part", 4) == 0) { |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 129 | int dev, ok; |
| 130 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 131 | for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) { |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 132 | if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) { |
| 133 | ++ok; |
| 134 | if (dev) |
| 135 | putc ('\n'); |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 136 | part_print(&sata_dev_desc[dev]); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | if (!ok) { |
| 140 | puts("\nno SATA devices available\n"); |
| 141 | rc ++; |
| 142 | } |
| 143 | return rc; |
| 144 | } |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 145 | return CMD_RET_USAGE; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 146 | case 3: |
| 147 | if (strncmp(argv[1], "dev", 3) == 0) { |
| 148 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 149 | |
| 150 | printf("\nSATA device %d: ", dev); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 151 | if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) { |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 152 | puts ("unknown device\n"); |
| 153 | return 1; |
| 154 | } |
| 155 | dev_print(&sata_dev_desc[dev]); |
| 156 | |
| 157 | if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN) |
| 158 | return 1; |
| 159 | |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 160 | sata_curr_device = dev; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 161 | |
| 162 | puts("... is now current device\n"); |
| 163 | |
| 164 | return 0; |
| 165 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 166 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 167 | |
| 168 | if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) { |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 169 | part_print(&sata_dev_desc[dev]); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 170 | } else { |
| 171 | printf("\nSATA device %d not available\n", dev); |
| 172 | rc = 1; |
| 173 | } |
| 174 | return rc; |
| 175 | } |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 176 | return CMD_RET_USAGE; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 177 | |
| 178 | default: /* at least 4 args */ |
| 179 | if (strcmp(argv[1], "read") == 0) { |
| 180 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 181 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 182 | ulong n; |
| 183 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 184 | |
| 185 | printf("\nSATA read: device %d block # %ld, count %ld ... ", |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 186 | sata_curr_device, blk, cnt); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 187 | |
Eric Nelson | 544c7b5 | 2016-03-27 12:00:15 -0700 | [diff] [blame] | 188 | n = blk_dread(&sata_dev_desc[sata_curr_device], |
| 189 | blk, cnt, (u32 *)addr); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 190 | |
| 191 | /* flush cache after read */ |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 192 | flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 193 | |
| 194 | printf("%ld blocks read: %s\n", |
| 195 | n, (n==cnt) ? "OK" : "ERROR"); |
| 196 | return (n == cnt) ? 0 : 1; |
| 197 | } else if (strcmp(argv[1], "write") == 0) { |
| 198 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 199 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 200 | ulong n; |
| 201 | |
| 202 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 203 | |
| 204 | printf("\nSATA write: device %d block # %ld, count %ld ... ", |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 205 | sata_curr_device, blk, cnt); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 206 | |
Eric Nelson | 544c7b5 | 2016-03-27 12:00:15 -0700 | [diff] [blame] | 207 | n = blk_dwrite(&sata_dev_desc[sata_curr_device], |
| 208 | blk, cnt, (u32 *)addr); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 209 | |
| 210 | printf("%ld blocks written: %s\n", |
| 211 | n, (n == cnt) ? "OK" : "ERROR"); |
| 212 | return (n == cnt) ? 0 : 1; |
| 213 | } else { |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 214 | return CMD_RET_USAGE; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return rc; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | U_BOOT_CMD( |
| 222 | sata, 5, 1, do_sata, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 223 | "SATA sub system", |
Fabio Estevam | 6be8b83 | 2013-02-20 14:35:35 +0000 | [diff] [blame] | 224 | "init - init SATA sub system\n" |
Nikita Kiryanov | 44fd1f9 | 2014-11-21 12:47:24 +0200 | [diff] [blame] | 225 | "sata stop - disable SATA sub system\n" |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 226 | "sata info - show available SATA devices\n" |
| 227 | "sata device [dev] - show or set current device\n" |
| 228 | "sata part [dev] - print partition table\n" |
| 229 | "sata read addr blk# cnt\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 230 | "sata write addr blk# cnt" |
| 231 | ); |