Dave Liu | c7057b5 | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Dave Liu | c7057b5 | 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 | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 18 | static int sata_curr_device = -1; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 19 | block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE]; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 20 | |
Mike Frysinger | cf7e399 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 21 | int __sata_initialize(void) |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 22 | { |
| 23 | int rc; |
| 24 | int i; |
| 25 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 26 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) { |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 27 | memset(&sata_dev_desc[i], 0, sizeof(struct block_dev_desc)); |
| 28 | sata_dev_desc[i].if_type = IF_TYPE_SATA; |
| 29 | sata_dev_desc[i].dev = i; |
| 30 | sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN; |
| 31 | sata_dev_desc[i].type = DEV_TYPE_HARDDISK; |
| 32 | sata_dev_desc[i].lba = 0; |
| 33 | sata_dev_desc[i].blksz = 512; |
Egbert Eich | 0472fbf | 2013-04-09 21:11:56 +0000 | [diff] [blame] | 34 | sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 35 | sata_dev_desc[i].block_read = sata_read; |
| 36 | sata_dev_desc[i].block_write = sata_write; |
| 37 | |
| 38 | rc = init_sata(i); |
Stefano Babic | 71cadda | 2012-02-22 00:24:37 +0000 | [diff] [blame] | 39 | if (!rc) { |
| 40 | rc = scan_sata(i); |
| 41 | if (!rc && (sata_dev_desc[i].lba > 0) && |
| 42 | (sata_dev_desc[i].blksz > 0)) |
| 43 | init_part(&sata_dev_desc[i]); |
| 44 | } |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 45 | } |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 46 | sata_curr_device = 0; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 47 | return rc; |
| 48 | } |
Mike Frysinger | cf7e399 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 49 | int sata_initialize(void) __attribute__((weak,alias("__sata_initialize"))); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 50 | |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 51 | #ifdef CONFIG_PARTITIONS |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 52 | block_dev_desc_t *sata_get_dev(int dev) |
| 53 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 54 | return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 55 | } |
Matthew McClintock | df3fc52 | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 56 | #endif |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 57 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 58 | static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 59 | { |
| 60 | int rc = 0; |
| 61 | |
Mike Frysinger | cf7e399 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 62 | if (argc == 2 && strcmp(argv[1], "init") == 0) |
| 63 | return sata_initialize(); |
| 64 | |
| 65 | /* If the user has not yet run `sata init`, do it now */ |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 66 | if (sata_curr_device == -1) |
Mike Frysinger | cf7e399 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 67 | if (sata_initialize()) |
| 68 | return 1; |
| 69 | |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 70 | switch (argc) { |
| 71 | case 0: |
| 72 | case 1: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 73 | return CMD_RET_USAGE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 74 | case 2: |
| 75 | if (strncmp(argv[1],"inf", 3) == 0) { |
| 76 | int i; |
| 77 | putc('\n'); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 78 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) { |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 79 | if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN) |
| 80 | continue; |
| 81 | printf ("SATA device %d: ", i); |
| 82 | dev_print(&sata_dev_desc[i]); |
| 83 | } |
| 84 | return 0; |
| 85 | } else if (strncmp(argv[1],"dev", 3) == 0) { |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 86 | if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) { |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 87 | puts("\nno SATA devices available\n"); |
| 88 | return 1; |
| 89 | } |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 90 | printf("\nSATA device %d: ", sata_curr_device); |
| 91 | dev_print(&sata_dev_desc[sata_curr_device]); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 92 | return 0; |
| 93 | } else if (strncmp(argv[1],"part",4) == 0) { |
| 94 | int dev, ok; |
| 95 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) { |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 97 | if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) { |
| 98 | ++ok; |
| 99 | if (dev) |
| 100 | putc ('\n'); |
| 101 | print_part(&sata_dev_desc[dev]); |
| 102 | } |
| 103 | } |
| 104 | if (!ok) { |
| 105 | puts("\nno SATA devices available\n"); |
| 106 | rc ++; |
| 107 | } |
| 108 | return rc; |
| 109 | } |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 110 | return CMD_RET_USAGE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 111 | case 3: |
| 112 | if (strncmp(argv[1], "dev", 3) == 0) { |
| 113 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 114 | |
| 115 | printf("\nSATA device %d: ", dev); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 116 | if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) { |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 117 | puts ("unknown device\n"); |
| 118 | return 1; |
| 119 | } |
| 120 | dev_print(&sata_dev_desc[dev]); |
| 121 | |
| 122 | if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN) |
| 123 | return 1; |
| 124 | |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 125 | sata_curr_device = dev; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 126 | |
| 127 | puts("... is now current device\n"); |
| 128 | |
| 129 | return 0; |
| 130 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 131 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 132 | |
| 133 | if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) { |
| 134 | print_part(&sata_dev_desc[dev]); |
| 135 | } else { |
| 136 | printf("\nSATA device %d not available\n", dev); |
| 137 | rc = 1; |
| 138 | } |
| 139 | return rc; |
| 140 | } |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 141 | return CMD_RET_USAGE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 142 | |
| 143 | default: /* at least 4 args */ |
| 144 | if (strcmp(argv[1], "read") == 0) { |
| 145 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 146 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 147 | ulong n; |
| 148 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 149 | |
| 150 | printf("\nSATA read: device %d block # %ld, count %ld ... ", |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 151 | sata_curr_device, blk, cnt); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 152 | |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 153 | n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 154 | |
| 155 | /* flush cache after read */ |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 156 | flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 157 | |
| 158 | printf("%ld blocks read: %s\n", |
| 159 | n, (n==cnt) ? "OK" : "ERROR"); |
| 160 | return (n == cnt) ? 0 : 1; |
| 161 | } else if (strcmp(argv[1], "write") == 0) { |
| 162 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 163 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 164 | ulong n; |
| 165 | |
| 166 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 167 | |
| 168 | printf("\nSATA write: device %d block # %ld, count %ld ... ", |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 169 | sata_curr_device, blk, cnt); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 170 | |
Mike Frysinger | 569460e | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 171 | n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr); |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 172 | |
| 173 | printf("%ld blocks written: %s\n", |
| 174 | n, (n == cnt) ? "OK" : "ERROR"); |
| 175 | return (n == cnt) ? 0 : 1; |
| 176 | } else { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 177 | return CMD_RET_USAGE; |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | return rc; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | U_BOOT_CMD( |
| 185 | sata, 5, 1, do_sata, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 186 | "SATA sub system", |
Fabio Estevam | 85dafbb | 2013-02-20 14:35:35 +0000 | [diff] [blame] | 187 | "init - init SATA sub system\n" |
Dave Liu | c7057b5 | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 188 | "sata info - show available SATA devices\n" |
| 189 | "sata device [dev] - show or set current device\n" |
| 190 | "sata part [dev] - print partition table\n" |
| 191 | "sata read addr blk# cnt\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 192 | "sata write addr blk# cnt" |
| 193 | ); |