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