Blame view

cmd/sata.c 2.88 KB
c7057b529   Dave Liu   ata: add the supp...
1
2
3
4
5
6
7
8
9
  /*
   * Copyright (C) 2000-2005, DENX Software Engineering
   *		Wolfgang Denk <wd@denx.de>
   * Copyright (C) Procsys. All rights reserved.
   *		Mushtaq Khan <mushtaq_k@procsys.com>
   *			<mushtaqk_921@yahoo.co.in>
   * Copyright (C) 2008 Freescale Semiconductor, Inc.
   *		Dave Liu <daveliu@freescale.com>
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
10
   * SPDX-License-Identifier:	GPL-2.0+
c7057b529   Dave Liu   ata: add the supp...
11
12
13
   */
  
  #include <common.h>
f19f1ecb6   Simon Glass   dm: sata: Support...
14
15
  #include <ahci.h>
  #include <dm.h>
c7057b529   Dave Liu   ata: add the supp...
16
17
18
  #include <command.h>
  #include <part.h>
  #include <sata.h>
f19f1ecb6   Simon Glass   dm: sata: Support...
19
20
  #include <dm/device-internal.h>
  #include <dm/uclass-internal.h>
c7057b529   Dave Liu   ata: add the supp...
21

088f1b199   Kim Phillips   common/cmd_*.c: s...
22
  static int sata_curr_device = -1;
c7057b529   Dave Liu   ata: add the supp...
23

f19f1ecb6   Simon Glass   dm: sata: Support...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  int sata_remove(int devnum)
  {
  #ifdef CONFIG_AHCI
  	struct udevice *dev;
  	int rc;
  
  	rc = uclass_find_device(UCLASS_AHCI, devnum, &dev);
  	if (!rc && !dev)
  		rc = uclass_find_first_device(UCLASS_AHCI, &dev);
  	if (rc || !dev) {
  		printf("Cannot find SATA device %d (err=%d)
  ", devnum, rc);
  		return CMD_RET_FAILURE;
  	}
  
  	rc = device_remove(dev, DM_REMOVE_NORMAL);
  	if (rc) {
  		printf("Cannot remove SATA device '%s' (err=%d)
  ", dev->name,
  		       rc);
  		return CMD_RET_FAILURE;
  	}
  
  	return 0;
  #else
  	return sata_stop();
  #endif
  }
  
  int sata_probe(int devnum)
  {
  #ifdef CONFIG_AHCI
  	struct udevice *dev;
  	struct udevice *blk;
  	int rc;
  
  	rc = uclass_get_device(UCLASS_AHCI, devnum, &dev);
  	if (rc)
  		rc = uclass_find_first_device(UCLASS_AHCI, &dev);
  	if (rc) {
  		printf("Cannot probe SATA device %d (err=%d)
  ", devnum, rc);
  		return CMD_RET_FAILURE;
  	}
  	rc = sata_scan(dev);
  	if (rc) {
  		printf("Cannot scan SATA device %d (err=%d)
  ", devnum, rc);
  		return CMD_RET_FAILURE;
  	}
  
  	rc = blk_get_from_parent(dev, &blk);
  	if (!rc) {
  		struct blk_desc *desc = dev_get_uclass_platdata(blk);
  
  		if (desc->lba > 0 && desc->blksz > 0)
  			part_init(desc);
  	}
  
  	return 0;
  #else
  	return sata_initialize() < 0 ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
  #endif
  }
088f1b199   Kim Phillips   common/cmd_*.c: s...
88
  static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
c7057b529   Dave Liu   ata: add the supp...
89
90
  {
  	int rc = 0;
f19f1ecb6   Simon Glass   dm: sata: Support...
91
92
93
94
95
  	if (argc >= 2) {
  		int devnum = 0;
  
  		if (argc == 3)
  			devnum = (int)simple_strtoul(argv[2], NULL, 10);
9bccfd01c   Ye Li   MLK-14930-1 cmd: ...
96
97
  		if (!strcmp(argv[1], "stop")) {
  			sata_curr_device = -1;
f19f1ecb6   Simon Glass   dm: sata: Support...
98
  			return sata_remove(devnum);
9bccfd01c   Ye Li   MLK-14930-1 cmd: ...
99
  		}
d957c28a7   Nikita Kiryanov   cmd_sata: impleme...
100

f19f1ecb6   Simon Glass   dm: sata: Support...
101
102
103
104
105
106
  		if (!strcmp(argv[1], "init")) {
  			if (sata_curr_device != -1) {
  				rc = sata_remove(devnum);
  				if (rc)
  					return rc;
  			}
d957c28a7   Nikita Kiryanov   cmd_sata: impleme...
107

9bccfd01c   Ye Li   MLK-14930-1 cmd: ...
108
109
110
111
112
  			rc = sata_probe(devnum);
  			if (rc < 0)
  				return CMD_RET_FAILURE;
  			sata_curr_device = rc;
  			return CMD_RET_SUCCESS;
f19f1ecb6   Simon Glass   dm: sata: Support...
113
  		}
d957c28a7   Nikita Kiryanov   cmd_sata: impleme...
114
  	}
cf7e399fb   Mike Frysinger   SATA: do not auto...
115
116
  
  	/* If the user has not yet run `sata init`, do it now */
aa6ab905b   Tang Yuantian   sata: fix sata co...
117
  	if (sata_curr_device == -1) {
f19f1ecb6   Simon Glass   dm: sata: Support...
118
119
  		rc = sata_probe(0);
  		if (rc < 0)
8547f45bc   Gary Bisson   cmd: sata: fix in...
120
  			return CMD_RET_FAILURE;
f19f1ecb6   Simon Glass   dm: sata: Support...
121
  		sata_curr_device = 0;
aa6ab905b   Tang Yuantian   sata: fix sata co...
122
  	}
cf7e399fb   Mike Frysinger   SATA: do not auto...
123

e29e71e93   Simon Glass   dm: sata: Adjust ...
124
  	return blk_common_cmd(argc, argv, IF_TYPE_SATA, &sata_curr_device);
c7057b529   Dave Liu   ata: add the supp...
125
126
127
128
  }
  
  U_BOOT_CMD(
  	sata, 5, 1, do_sata,
2fb2604d5   Peter Tyser   Command usage cle...
129
  	"SATA sub system",
85dafbb8b   Fabio Estevam   common: cmd_sata:...
130
131
  	"init - init SATA sub system
  "
f19f1ecb6   Simon Glass   dm: sata: Support...
132
133
  	"sata stop [dev] - disable SATA sub system or device
  "
c7057b529   Dave Liu   ata: add the supp...
134
135
136
137
138
139
140
141
  	"sata info - show available SATA devices
  "
  	"sata device [dev] - show or set current device
  "
  	"sata part [dev] - print partition table
  "
  	"sata read addr blk# cnt
  "
a89c33db9   Wolfgang Denk   General help mess...
142
143
  	"sata write addr blk# cnt"
  );