]>
Commit | Line | Data |
---|---|---|
c7057b52 DL |
1 | /* |
2 | * Copyright (C) 2000-2005, DENX Software Engineering | |
3 | * Wolfgang Denk <[email protected]> | |
4 | * Copyright (C) Procsys. All rights reserved. | |
5 | * Mushtaq Khan <[email protected]> | |
6 | * <[email protected]> | |
7 | * Copyright (C) 2008 Freescale Semiconductor, Inc. | |
8 | * Dave Liu <[email protected]> | |
9 | * | |
1a459660 | 10 | * SPDX-License-Identifier: GPL-2.0+ |
c7057b52 DL |
11 | */ |
12 | ||
13 | #include <common.h> | |
14 | #include <command.h> | |
15 | #include <part.h> | |
16 | #include <sata.h> | |
17 | ||
088f1b19 | 18 | static int sata_curr_device = -1; |
4101f687 | 19 | struct blk_desc sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE]; |
c7057b52 | 20 | |
4101f687 | 21 | static unsigned long sata_bread(struct blk_desc *block_dev, lbaint_t start, |
7c4213f6 SW |
22 | lbaint_t blkcnt, void *dst) |
23 | { | |
24 | return sata_read(block_dev->dev, start, blkcnt, dst); | |
25 | } | |
26 | ||
4101f687 | 27 | static unsigned long sata_bwrite(struct blk_desc *block_dev, lbaint_t start, |
7c4213f6 SW |
28 | lbaint_t blkcnt, const void *buffer) |
29 | { | |
30 | return sata_write(block_dev->dev, start, blkcnt, buffer); | |
31 | } | |
32 | ||
cf7e399f | 33 | int __sata_initialize(void) |
c7057b52 DL |
34 | { |
35 | int rc; | |
36 | int i; | |
37 | ||
6d0f6bcf | 38 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) { |
4101f687 | 39 | memset(&sata_dev_desc[i], 0, sizeof(struct blk_desc)); |
c7057b52 DL |
40 | sata_dev_desc[i].if_type = IF_TYPE_SATA; |
41 | sata_dev_desc[i].dev = i; | |
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; | |
0472fbfd | 46 | sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz); |
7c4213f6 SW |
47 | sata_dev_desc[i].block_read = sata_bread; |
48 | sata_dev_desc[i].block_write = sata_bwrite; | |
c7057b52 DL |
49 | |
50 | rc = init_sata(i); | |
71cadda3 SB |
51 | if (!rc) { |
52 | rc = scan_sata(i); | |
53 | if (!rc && (sata_dev_desc[i].lba > 0) && | |
54 | (sata_dev_desc[i].blksz > 0)) | |
3e8bd469 | 55 | part_init(&sata_dev_desc[i]); |
71cadda3 | 56 | } |
c7057b52 | 57 | } |
569460eb | 58 | sata_curr_device = 0; |
c7057b52 DL |
59 | return rc; |
60 | } | |
cf7e399f | 61 | int sata_initialize(void) __attribute__((weak,alias("__sata_initialize"))); |
c7057b52 | 62 | |
d957c28a NK |
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 | ||
df3fc526 | 77 | #ifdef CONFIG_PARTITIONS |
4101f687 | 78 | struct blk_desc *sata_get_dev(int dev) |
c7057b52 | 79 | { |
6d0f6bcf | 80 | return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL; |
c7057b52 | 81 | } |
df3fc526 | 82 | #endif |
c7057b52 | 83 | |
088f1b19 | 84 | static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
c7057b52 DL |
85 | { |
86 | int rc = 0; | |
87 | ||
d957c28a NK |
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 | ||
cf7e399f | 95 | return sata_initialize(); |
d957c28a | 96 | } |
cf7e399f MF |
97 | |
98 | /* If the user has not yet run `sata init`, do it now */ | |
569460eb | 99 | if (sata_curr_device == -1) |
cf7e399f MF |
100 | if (sata_initialize()) |
101 | return 1; | |
102 | ||
c7057b52 DL |
103 | switch (argc) { |
104 | case 0: | |
105 | case 1: | |
4c12eeb8 | 106 | return CMD_RET_USAGE; |
c7057b52 DL |
107 | case 2: |
108 | if (strncmp(argv[1],"inf", 3) == 0) { | |
109 | int i; | |
110 | putc('\n'); | |
6d0f6bcf | 111 | for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) { |
c7057b52 DL |
112 | if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN) |
113 | continue; | |
114 | printf ("SATA device %d: ", i); | |
115 | dev_print(&sata_dev_desc[i]); | |
116 | } | |
117 | return 0; | |
118 | } else if (strncmp(argv[1],"dev", 3) == 0) { | |
569460eb | 119 | if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) { |
c7057b52 DL |
120 | puts("\nno SATA devices available\n"); |
121 | return 1; | |
122 | } | |
569460eb MF |
123 | printf("\nSATA device %d: ", sata_curr_device); |
124 | dev_print(&sata_dev_desc[sata_curr_device]); | |
c7057b52 DL |
125 | return 0; |
126 | } else if (strncmp(argv[1],"part",4) == 0) { | |
127 | int dev, ok; | |
128 | ||
6d0f6bcf | 129 | for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) { |
c7057b52 DL |
130 | if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) { |
131 | ++ok; | |
132 | if (dev) | |
133 | putc ('\n'); | |
3e8bd469 | 134 | part_print(&sata_dev_desc[dev]); |
c7057b52 DL |
135 | } |
136 | } | |
137 | if (!ok) { | |
138 | puts("\nno SATA devices available\n"); | |
139 | rc ++; | |
140 | } | |
141 | return rc; | |
142 | } | |
4c12eeb8 | 143 | return CMD_RET_USAGE; |
c7057b52 DL |
144 | case 3: |
145 | if (strncmp(argv[1], "dev", 3) == 0) { | |
146 | int dev = (int)simple_strtoul(argv[2], NULL, 10); | |
147 | ||
148 | printf("\nSATA device %d: ", dev); | |
6d0f6bcf | 149 | if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) { |
c7057b52 DL |
150 | puts ("unknown device\n"); |
151 | return 1; | |
152 | } | |
153 | dev_print(&sata_dev_desc[dev]); | |
154 | ||
155 | if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN) | |
156 | return 1; | |
157 | ||
569460eb | 158 | sata_curr_device = dev; |
c7057b52 DL |
159 | |
160 | puts("... is now current device\n"); | |
161 | ||
162 | return 0; | |
163 | } else if (strncmp(argv[1], "part", 4) == 0) { | |
164 | int dev = (int)simple_strtoul(argv[2], NULL, 10); | |
165 | ||
166 | if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) { | |
3e8bd469 | 167 | part_print(&sata_dev_desc[dev]); |
c7057b52 DL |
168 | } else { |
169 | printf("\nSATA device %d not available\n", dev); | |
170 | rc = 1; | |
171 | } | |
172 | return rc; | |
173 | } | |
4c12eeb8 | 174 | return CMD_RET_USAGE; |
c7057b52 DL |
175 | |
176 | default: /* at least 4 args */ | |
177 | if (strcmp(argv[1], "read") == 0) { | |
178 | ulong addr = simple_strtoul(argv[2], NULL, 16); | |
179 | ulong cnt = simple_strtoul(argv[4], NULL, 16); | |
180 | ulong n; | |
181 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); | |
182 | ||
183 | printf("\nSATA read: device %d block # %ld, count %ld ... ", | |
569460eb | 184 | sata_curr_device, blk, cnt); |
c7057b52 | 185 | |
569460eb | 186 | n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr); |
c7057b52 DL |
187 | |
188 | /* flush cache after read */ | |
569460eb | 189 | flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz); |
c7057b52 DL |
190 | |
191 | printf("%ld blocks read: %s\n", | |
192 | n, (n==cnt) ? "OK" : "ERROR"); | |
193 | return (n == cnt) ? 0 : 1; | |
194 | } else if (strcmp(argv[1], "write") == 0) { | |
195 | ulong addr = simple_strtoul(argv[2], NULL, 16); | |
196 | ulong cnt = simple_strtoul(argv[4], NULL, 16); | |
197 | ulong n; | |
198 | ||
199 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); | |
200 | ||
201 | printf("\nSATA write: device %d block # %ld, count %ld ... ", | |
569460eb | 202 | sata_curr_device, blk, cnt); |
c7057b52 | 203 | |
569460eb | 204 | n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr); |
c7057b52 DL |
205 | |
206 | printf("%ld blocks written: %s\n", | |
207 | n, (n == cnt) ? "OK" : "ERROR"); | |
208 | return (n == cnt) ? 0 : 1; | |
209 | } else { | |
4c12eeb8 | 210 | return CMD_RET_USAGE; |
c7057b52 DL |
211 | } |
212 | ||
213 | return rc; | |
214 | } | |
215 | } | |
216 | ||
217 | U_BOOT_CMD( | |
218 | sata, 5, 1, do_sata, | |
2fb2604d | 219 | "SATA sub system", |
85dafbb8 | 220 | "init - init SATA sub system\n" |
d957c28a | 221 | "sata stop - disable SATA sub system\n" |
c7057b52 DL |
222 | "sata info - show available SATA devices\n" |
223 | "sata device [dev] - show or set current device\n" | |
224 | "sata part [dev] - print partition table\n" | |
225 | "sata read addr blk# cnt\n" | |
a89c33db WD |
226 | "sata write addr blk# cnt" |
227 | ); |