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 | * |
| 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 | |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 31 | int sata_curr_device = -1; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 32 | block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE]; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 33 | |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 34 | int __sata_initialize(void) |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 35 | { |
| 36 | int rc; |
| 37 | int i; |
| 38 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 39 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) { |
Dave Liu | 76596ba | 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; |
| 47 | sata_dev_desc[i].block_read = sata_read; |
| 48 | sata_dev_desc[i].block_write = sata_write; |
| 49 | |
| 50 | rc = init_sata(i); |
| 51 | rc = scan_sata(i); |
| 52 | if ((sata_dev_desc[i].lba > 0) && (sata_dev_desc[i].blksz > 0)) |
| 53 | init_part(&sata_dev_desc[i]); |
| 54 | } |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 55 | sata_curr_device = 0; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 56 | return rc; |
| 57 | } |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 58 | int sata_initialize(void) __attribute__((weak,alias("__sata_initialize"))); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 59 | |
| 60 | block_dev_desc_t *sata_get_dev(int dev) |
| 61 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 63 | } |
| 64 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 65 | 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] | 66 | { |
| 67 | int rc = 0; |
| 68 | |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 69 | if (argc == 2 && strcmp(argv[1], "init") == 0) |
| 70 | return sata_initialize(); |
| 71 | |
| 72 | /* If the user has not yet run `sata init`, do it now */ |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 73 | if (sata_curr_device == -1) |
Mike Frysinger | e66dc08 | 2009-01-27 16:12:21 -0500 | [diff] [blame] | 74 | if (sata_initialize()) |
| 75 | return 1; |
| 76 | |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 77 | switch (argc) { |
| 78 | case 0: |
| 79 | case 1: |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 80 | return cmd_usage(cmdtp); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 81 | case 2: |
| 82 | if (strncmp(argv[1],"inf", 3) == 0) { |
| 83 | int i; |
| 84 | putc('\n'); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 85 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) { |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 86 | if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN) |
| 87 | continue; |
| 88 | printf ("SATA device %d: ", i); |
| 89 | dev_print(&sata_dev_desc[i]); |
| 90 | } |
| 91 | return 0; |
| 92 | } else if (strncmp(argv[1],"dev", 3) == 0) { |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 93 | if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) { |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 94 | puts("\nno SATA devices available\n"); |
| 95 | return 1; |
| 96 | } |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 97 | printf("\nSATA device %d: ", sata_curr_device); |
| 98 | dev_print(&sata_dev_desc[sata_curr_device]); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 99 | return 0; |
| 100 | } else if (strncmp(argv[1],"part",4) == 0) { |
| 101 | int dev, ok; |
| 102 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 103 | for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) { |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 104 | if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) { |
| 105 | ++ok; |
| 106 | if (dev) |
| 107 | putc ('\n'); |
| 108 | print_part(&sata_dev_desc[dev]); |
| 109 | } |
| 110 | } |
| 111 | if (!ok) { |
| 112 | puts("\nno SATA devices available\n"); |
| 113 | rc ++; |
| 114 | } |
| 115 | return rc; |
| 116 | } |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 117 | return cmd_usage(cmdtp); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 118 | case 3: |
| 119 | if (strncmp(argv[1], "dev", 3) == 0) { |
| 120 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 121 | |
| 122 | printf("\nSATA device %d: ", dev); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 123 | if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) { |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 124 | puts ("unknown device\n"); |
| 125 | return 1; |
| 126 | } |
| 127 | dev_print(&sata_dev_desc[dev]); |
| 128 | |
| 129 | if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN) |
| 130 | return 1; |
| 131 | |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 132 | sata_curr_device = dev; |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 133 | |
| 134 | puts("... is now current device\n"); |
| 135 | |
| 136 | return 0; |
| 137 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 138 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 139 | |
| 140 | if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) { |
| 141 | print_part(&sata_dev_desc[dev]); |
| 142 | } else { |
| 143 | printf("\nSATA device %d not available\n", dev); |
| 144 | rc = 1; |
| 145 | } |
| 146 | return rc; |
| 147 | } |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 148 | return cmd_usage(cmdtp); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 149 | |
| 150 | default: /* at least 4 args */ |
| 151 | if (strcmp(argv[1], "read") == 0) { |
| 152 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 153 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 154 | ulong n; |
| 155 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 156 | |
| 157 | printf("\nSATA read: device %d block # %ld, count %ld ... ", |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 158 | sata_curr_device, blk, cnt); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 159 | |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 160 | n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 161 | |
| 162 | /* flush cache after read */ |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 163 | flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 164 | |
| 165 | printf("%ld blocks read: %s\n", |
| 166 | n, (n==cnt) ? "OK" : "ERROR"); |
| 167 | return (n == cnt) ? 0 : 1; |
| 168 | } else if (strcmp(argv[1], "write") == 0) { |
| 169 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 170 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 171 | ulong n; |
| 172 | |
| 173 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 174 | |
| 175 | printf("\nSATA write: device %d block # %ld, count %ld ... ", |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 176 | sata_curr_device, blk, cnt); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 177 | |
Mike Frysinger | b6ade25 | 2009-06-14 21:35:22 -0400 | [diff] [blame] | 178 | n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 179 | |
| 180 | printf("%ld blocks written: %s\n", |
| 181 | n, (n == cnt) ? "OK" : "ERROR"); |
| 182 | return (n == cnt) ? 0 : 1; |
| 183 | } else { |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 184 | return cmd_usage(cmdtp); |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return rc; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | U_BOOT_CMD( |
| 192 | sata, 5, 1, do_sata, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 193 | "SATA sub system", |
| 194 | "sata init - init SATA sub system\n" |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 195 | "sata info - show available SATA devices\n" |
| 196 | "sata device [dev] - show or set current device\n" |
| 197 | "sata part [dev] - print partition table\n" |
| 198 | "sata read addr blk# cnt\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 199 | "sata write addr blk# cnt" |
| 200 | ); |