1 #include <common.h>
2 #if defined (CONFIG_SAM440EP)
3 #include <configs/Sam440ep.h>
4 #elif defined (CONFIG_SAM440EP_FLEX)
5 #include <configs/Sam440ep_flex.h>
6 #elif defined (CONFIG_SAM460EX)
7 #include <configs/Sam460ex.h>
8 #endif
9 
10 #include <part.h>
11 #include <ide.h>
12 #include <ata.h>
13 #include <pci.h> //We need the PCI primitives.
14 #include <malloc.h>
15 
16 #include "sys_dep.h" //Il marchio di fabbrica.
17 #include "sam_ide.h"
18 #include "misc_utils.h"
19 
20 #ifdef CONFIG_SAM460EX
21 DECLARE_GLOBAL_DATA_PTR;
22 
23 #define BOARD_CANYONLANDS_PCIE	1
24 #define BOARD_CANYONLANDS_SATA	2
25 
26 block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
27 extern ulong sata_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer);
28 #endif
29 
30 /*
31 How to add new IDE boards to this module.
32 This note explains how to add new controller cards to those supported by
33 a1ide.c/UBoot.
34 
35 To add support for a new IDE controller you need to:
36 - add a new entry in the controllers[] array.
37 - write a couple of functions to read from the controllers, one for ATA and
38 one for ATAPI.
39 These are very simple and usually don't need special care, they simply
40 forward the call to the generic reading routines.
41 - write a small function to fetch a unit from those present on the controller;
42 much like the point above.
43 - write a function to initialize the controller card.
44 - add some bits and pieces here and there (all explained below).
45 
46 Let's start with the big chunk: the controllers[] array.
47 It's an array of struct controller_context.
48 There must be one for every supported controller/chip.
49 But first, a word from our spon.... no: a word about UBoot!
50 In "controllers" and elsewhere, it's quite often to find function pointers.
51 Due to the fact that UBoot gets relocated early during the initialization
52 phases, NO function pointer can be used in a static structure
53 or a static variable of a module!!! So if you plan to use function pointers
54 like in controllers, you MUST initialize them at RUN TIME, _NOT COMPILE TIME_!!!
55 
56 The controller_context sturcture:
57 BOOL	cc_present:
58 	set this to TRUE during the initialization routine if
59 	the card was found and it's working.
60 UBYTE	cc_maxunit:
61 	total number of units that can exists on this controller
62 UBYTE	cc_maxbus:
63 	total number of different bus that are handled by this
64 	controller. Usually 2.
65 char *cc_maxbus_var:
66 	pointer to a string with the environment variable used
67 	to limit the buses being used/scanned.
68 BOOL *cc_bus_ok:
69 	an array of BOOLs, allocated at runtime, length is
70 	cc_maxbus: tells which bus have real units.
71 base_io_address *cc_base_io:
72 	array of io_address, allocated at runtime, length is
73 	cc_maxbus: base IO addresses for each bus.
74 block_dev_desc_t * cc_units:
75 	array of device descriptors, allocated at runtime, length is cc_maxunit.
76 char *cc_description:
77 	a string defining the controller itself.
78 unsigned long (* cc_block_read)():
79 	function pointer to the ATA block read routine, see the warning above.
80 unsigned long (* cc_atapi_read)():
81 	function pointer to the ATAPI block read routine, see the warning above.
82 
83 Some of these fields will be filled in by the generic part, so you don't have
84 to care for them.
85 What must be supplied by you are:
86 STATIC (compile-time): cc_maxunit, cc_maxbus, cc_maxbus_var, cc_description.
87 DYNAMIC (run-time): cc_present, cc_base_io, cc_block_read, cc_atapi_read
88 DYNAMIC (run-time), simply allocated and cleared: cc_bus_ok, cc_units.
89 
90 The last two entries refer to the two reading routines, one for ATA and the
91 second for ATAPI.
92 They use common code but, because of compatibility reasons, they cannot have
93 the controller_context structure in their prototipes. But they call upon it,
94 so all you need to do is forward the call to local_ide_read and local_atapi_read,
95 using the appropriate cc_base_io.
96 Have a look at p_sii_block_read and p_sii_atapi_read for an example.
97 The same kind of reasoning is applied to the "get_dev" function: you must write
98 one function like via_get_dev that returns a (block_dev_desc_t *) of your
99 controller. This function is the hook used by cmd_boota to use units on your
100 controller. You decide the name.
101 
102 The initialization function: this is the main part of adding a new controller.
103 This function must:
104 - check if the controller is really present in your system. If not present,
105 	quit immediately. Otherwise:
106 - allocate and clear cc_units, cc_base_io and cc_bus_ok.
107 - really initialize the controller HW. You should know what to do here.
108 - fill in with sensible values cc_base_io.
109 - if you want to let the user swap the primary and secondary bus, call ide_swap().
110 - fill in cc_block_read and cc_atapi_read, linking them with the callbacks for
111 	your controller.
112 - set cc_present to TRUE.
113 
114 Ok, that's it.
115 One more step is required: add a call to the above initialization function
116 from init_controllers.
117 
118 Done!
119 */
120 
121 #undef	IDE_DEBUG
122 #ifdef	IDE_DEBUG
123 #define	PRINTF(fmt,args...)		printf (fmt ,##args)
124 #define	PRINTF2(fmt,args...)
125 #else
126 #define PRINTF(fmt,args...)
127 #define PRINTF2(fmt,args...)
128 #endif
129 
130 #undef	ATAPI_DEBUG
131 #ifdef	ATAPI_DEBUG
132 #define	AT_PRINTF(fmt,args...)	printf (fmt ,##args)
133 #else
134 #define AT_PRINTF(fmt,args...)
135 #endif
136 
137 #define EIEIO	__asm__ volatile ("eieio")
138 
IDE_BUS_ADV(const struct controller_context * const cc,const unsigned dev)139 static inline base_io_address IDE_BUS_ADV(const struct controller_context * const cc, const unsigned dev)
140 {
141 	return cc->cc_base_io[dev/cc->cc_units_per_bus];
142 }
143 
144 //#define ATA_DEVICE_ADV(cc, dev) ((dev & 1)<<4)
ATA_DEVICE_ADV(const struct controller_context * const cc,const unsigned dev)145 static int ATA_DEVICE_ADV(const struct controller_context * const cc, const unsigned dev)
146 {
147     if (strcmp(cc->cc_description,"Sii0680") == 0) return((dev & 1)<<4);
148     else return 0;
149 }
150 
151 #define ATA_CURR_BASE_ADV(cc, dev)	(CONFIG_SYS_ATA_BASE_ADDR+IDE_BUS_ADV(cc,dev))
152 
153 #define IDE_TIME_OUT			2000 /* 2 sec timeout */
154 #define ATAPI_TIME_OUT			7000 /* 7 sec timeout (5 sec seems to work...) */
155 #define IDE_SPIN_UP_TIME_OUT	5000 /* 5 sec spin-up timeout */
156 
157 #ifdef CONFIG_SHOW_BOOT_PROGRESS
158 #include <status_led.h>
159 #define SHOW_BOOT_PROGRESS(arg)	show_boot_progress(arg)
160 #else
161 #define SHOW_BOOT_PROGRESS(arg)
162 #endif
163 
164 struct controller_context controllers[]=
165 {
166 	{ FALSE, MAX_S_SII_UNITS,		MAX_S_SII_BUS,		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
167 	{ FALSE, MAX_S_4_SII_UNITS,		MAX_S_4_SII_BUS,	0, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
168 #ifdef CONFIG_SAM460EX
169 	{ FALSE, MAX_SATA2_460_UNITS,	MAX_SATA2_460_BUS,	0, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
170 #endif
171 	{ FALSE, MAX_P_SII_UNITS,		MAX_P_SII_BUS,		0, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
172 };
173 
174 enum
175 {
176 	S_SII_POS = 0,
177 	S4_SII_POS,
178 #ifdef CONFIG_SAM460EX
179 	SATA2_460_POS,
180 #endif
181 	P_SII_POS
182 };
183 
184 //Forward declarations.
185 static unsigned long s_sii_block_read(int dev, unsigned long start, lbaint_t blkcnt, unsigned long *buffer);
186 static ulong s_sii_atapi_read (int device, lbaint_t blknr, ulong blkcnt, ulong *buffer);
187 
188 static unsigned long s_4_sii_block_read(int dev, unsigned long start, lbaint_t blkcnt, unsigned long *buffer);
189 static ulong s_4_sii_atapi_read (int device, lbaint_t blknr, ulong blkcnt, ulong *buffer);
190 
191 static unsigned long p_sii_block_read(int dev, unsigned long start, lbaint_t blkcnt, unsigned long *buffer);
192 static ulong p_sii_atapi_read (int device, lbaint_t blknr, ulong blkcnt, ulong *buffer);
193 
194 static ulong local_ide_read (int device, lbaint_t blknr, ulong blkcnt, ulong *buffer, const struct controller_context * const cc);
195 static void local_input_swap_data(int dev, ulong *sect_buf, int words, const struct controller_context * const cc);
196 static void local_input_data(int dev, ulong *sect_buf, int words, const struct controller_context * const cc);
197 static void local_ide_ident (block_dev_desc_t *dev_desc, struct controller_context * const ctx);
198 static uchar local_ide_wait (int dev, ulong t, const struct controller_context * const cc);
199 static void local_ident_cpy (unsigned char *dst, unsigned char *src, unsigned int len);
200 
201 //This should between an ifdef ATAPI /endif pair
202 static void local_atapi_inquiry(block_dev_desc_t * dev_desc, struct controller_context * const ctx);
203 static uchar local_atapi_wait_mask (int dev, ulong t,uchar mask, uchar res, const struct controller_context * const cc);
204 static unsigned char local_atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen, const struct controller_context * const cc);
205 
206 static _Bool local_ide_set_pio(const int device, const unsigned char mode, const struct controller_context * const cc);
207 
local_ide_outb(int dev,int port,unsigned char val,const struct controller_context * const cc)208 static void local_ide_outb(int dev, int port, unsigned char val, const struct controller_context * const cc)
209 {
210 	PRINTF2 ("a1_ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
211 		dev, port, val, (ATA_CURR_BASE_ADV(cc, dev)+port));
212 	/* Ensure I/O operations complete */
213 	EIEIO;
214 	*((uchar *)(ATA_CURR_BASE_ADV(cc, dev)+port)) = val;
215 }
216 
local_ide_inb(int dev,int port,const struct controller_context * const cc)217 static unsigned char local_ide_inb(int dev, int port, const struct controller_context * const cc)
218 {
219 	uchar val;
220 	/* Ensure I/O operations complete */
221 	EIEIO;
222 	val = *((uchar *)(ATA_CURR_BASE_ADV(cc, dev)+port));
223 
224 	PRINTF2 ("a1_ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
225 		dev, port, (ATA_CURR_BASE_ADV(cc, dev)+port), val);
226 
227 	return (val);
228 }
229 
230 //--------------
231 
s_sii_early_init(struct controller_context * const ctx)232 static void s_sii_early_init(struct controller_context * const ctx)
233 {
234 	unsigned int bdf;
235 	unsigned int addr;
236 	uint32_t class_rev;
237 
238 	PRINTF ("s_sii_init: START\n");
239 
240 	//Creates the devices description array.
241 
242 	ctx->cc_units = calloc(sizeof(block_dev_desc_t),  ctx->cc_maxunit);
243 	ctx->cc_base_io = calloc(sizeof(base_io_address), ctx->cc_maxbus);
244 	ctx->cc_bus_ok = calloc(sizeof(BOOL), ctx->cc_maxbus);
245 	ctx->cc_maxbus_var = "ssii_maxbus";
246 	ctx->cc_description = "Sii3112";
247 
248 	/* get IDE Controller Device ID */
249 
250 	if ((bdf = pci_find_device(PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_3112, 0)) == -1)
251 	{
252 		if ((bdf = pci_find_device(PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_3512, 0)) == -1)
253 	    {
254 	        return;
255 	    }
256 	}
257 
258 	pci_write_config_dword(bdf, PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
259 
260 	// Reset chip registers to safe values
261 	pci_read_config_dword(bdf, PCI_CLASS_REVISION, &class_rev);
262 	class_rev &= 0xFF;
263 	pci_write_config_byte(bdf, PCI_CACHE_LINE_SIZE, (class_rev) ? 1 : 255);
264 
265 	pci_read_config_dword (bdf, PCI_BASE_ADDRESS_0, &addr);
266 	ctx->cc_base_io[0] = (void *)(addr & PCI_BASE_ADDRESS_IO_MASK);
267 	pci_read_config_dword (bdf, PCI_BASE_ADDRESS_2, &addr);
268 	ctx->cc_base_io[1] = (void *)(addr & PCI_BASE_ADDRESS_IO_MASK);
269 
270 	ctx->cc_present = TRUE;
271 	ctx->cc_block_read = s_sii_block_read;
272 	ctx->cc_atapi_read = s_sii_atapi_read;
273 
274 	PRINTF("Done s_sii initialization, base IO addresses at %08lx, %08lx\n",
275 		ctx->cc_base_io[0], ctx->cc_base_io[1]);
276 }
277 
s_4_sii_early_init(struct controller_context * const ctx)278 static void s_4_sii_early_init(struct controller_context * const ctx)
279 {
280 	unsigned int bdf;
281 	unsigned int addr;
282 	uint32_t class_rev;
283 
284 	PRINTF ("s_4_sii_init: START\n");
285 
286 	//Creates the devices description array.
287 
288 	ctx->cc_units = calloc(sizeof(block_dev_desc_t),  ctx->cc_maxunit);
289 	ctx->cc_base_io = calloc(sizeof(base_io_address), ctx->cc_maxbus);
290 	ctx->cc_bus_ok = calloc(sizeof(BOOL), ctx->cc_maxbus);
291 	ctx->cc_maxbus_var = "s4sii_maxbus";
292 	ctx->cc_description = "Sii3114";
293 
294 	/* get IDE Controller Device ID */
295 
296 	if ((bdf = pci_find_device(PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_3114, 0)) == -1)
297 	{
298 		return;
299 	}
300 
301 	pci_write_config_dword(bdf, PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
302 
303 	// Reset chip registers to safe values
304 	pci_read_config_dword(bdf, PCI_CLASS_REVISION, &class_rev);
305 	class_rev &= 0xFF;
306 	pci_write_config_byte(bdf, PCI_CACHE_LINE_SIZE, (class_rev) ? 1 : 255);
307 	//I really don't know what the above code is all about.....
308 
309 
310 	pci_read_config_dword (bdf, PCI_BASE_ADDRESS_0, &addr);
311 	ctx->cc_base_io[0] = (void *)(addr & PCI_BASE_ADDRESS_IO_MASK);
312 	pci_read_config_dword (bdf, PCI_BASE_ADDRESS_2, &addr);
313 	ctx->cc_base_io[1] = (void *)(addr & PCI_BASE_ADDRESS_IO_MASK);
314 
315 /*
316 	Here we got a problem: since the "autoconfiguration" (ahemm...) of PCI is done via
317 	BAR registers, it's very likely that other cards I/O space overlap with the
318 	3114 "hole" after which the 2 upper buses are placed.
319 	This really happens if a 680 is used at the same time.
320 	So for the time being the two upper buses are disabled.
321 
322 	ctx->cc_base_io[2] = ctx->cc_base_io[0] + SILLY_SIL_4_PORTS_OFFSET;
323 	ctx->cc_base_io[3] = ctx->cc_base_io[1] + SILLY_SIL_4_PORTS_OFFSET;
324 */
325 //	ctx->cc_maxunit = ctx->cc_maxbus = 2; //See the comment above.
326 
327 	ctx->cc_base_io[2] = ctx->cc_base_io[0] + SILLY_SIL_4_PORTS_OFFSET;
328 	ctx->cc_base_io[3] = ctx->cc_base_io[1] + SILLY_SIL_4_PORTS_OFFSET;
329 		ctx->cc_maxunit = ctx->cc_maxbus = 4;
330 
331 	ctx->cc_present = TRUE;
332 	ctx->cc_block_read = s_4_sii_block_read;
333 	ctx->cc_atapi_read = s_4_sii_atapi_read;
334 
335 	PRINTF("Done s_4_sii initialization, base IO addresses at %08lx, %08lx, %08lx, %08lx\n",
336 		ctx->cc_base_io[0], ctx->cc_base_io[1], ctx->cc_base_io[2], ctx->cc_base_io[3]);
337 }
338 
339 #ifdef CONFIG_SAM460EX
sata2_460_early_init(struct controller_context * const ctx)340 static void sata2_460_early_init(struct controller_context * const ctx)
341 {
342 	PRINTF ("sata2_460_init: START\n");
343 
344 	//Creates the devices description array.
345 
346 	ctx->cc_units = NULL;						// later...
347 	ctx->cc_base_io = NULL;						// not used
348 	ctx->cc_bus_ok = calloc(sizeof(BOOL), ctx->cc_maxbus);
349 	ctx->cc_maxbus_var = "sata2_maxbus";
350 	ctx->cc_description = "SATA2-460";
351 
352 	/* get IDE Controller Device ID */
353 
354 	if (gd->board_type != BOARD_CANYONLANDS_SATA) return;
355 
356 	ctx->cc_present = TRUE;
357 	ctx->cc_block_read = sata_read;
358 	ctx->cc_atapi_read = sata_read;
359 
360 	PRINTF("Done sata2_460 initialization\n");
361 }
362 #endif
363 
p_sii_early_init(struct controller_context * const ctx)364 static void p_sii_early_init(struct controller_context * const ctx)
365 {
366 	unsigned int cmd, bdf;
367 	unsigned int addr;
368 	unsigned char tmpbyte = 0;
369 
370 	PRINTF ("p_sii_init: START\n");
371 
372 	//Creates the devices description array.
373 
374 	ctx->cc_units = calloc(sizeof(block_dev_desc_t),  ctx->cc_maxunit);
375 	ctx->cc_base_io = calloc(sizeof(base_io_address), ctx->cc_maxbus);
376 	ctx->cc_bus_ok = calloc(sizeof(BOOL), ctx->cc_maxbus);
377 	ctx->cc_maxbus_var = "psii_maxbus";
378 	ctx->cc_description = "Sii0680";
379 
380 	/* get IDE Controller Device ID */
381 
382 	if ((bdf = pci_find_device(PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_680, 0)) == -1)
383 	{
384 		return;
385 	}
386 
387 	pci_read_config_dword (bdf, PCI_BASE_ADDRESS_0, &addr);
388 	ctx->cc_base_io[0] = (void *)(addr & PCI_BASE_ADDRESS_IO_MASK);
389 	pci_read_config_dword (bdf, PCI_BASE_ADDRESS_2, &addr);
390 	ctx->cc_base_io[1] = (void *)(addr & PCI_BASE_ADDRESS_IO_MASK);
391 
392 	PRINTF("p_sii base addresses at %08lx and %08lx\n", ctx->cc_base_io[0], ctx->cc_base_io[1]);
393 
394 	/* Enable bus mastering in case this has not been done, yet. */
395 
396 	pci_read_config_dword (bdf, PCI_COMMAND, &cmd);
397 	cmd |= PCI_COMMAND_MASTER;
398 	pci_write_config_dword (bdf, PCI_COMMAND, cmd);
399 
400   	/* initialize registers */
401 
402 	pci_write_config_byte (bdf, 0x80, 0x00);
403 	pci_write_config_byte (bdf, 0x84, 0x00);
404 	pci_read_config_byte (bdf, 0x8A, &tmpbyte);
405 	pci_write_config_byte (bdf, 0x8A, tmpbyte | 0x01);
406 
407 	pci_write_config_word (bdf, 0xA2, 0x328A);
408 	pci_write_config_dword (bdf, 0xA4, 0x328A328A);
409 	pci_write_config_dword (bdf, 0xA8, 0x43924392);
410 	pci_write_config_dword (bdf, 0xAC, 0x40094009);
411 	pci_write_config_word (bdf, 0xB2, 0x328A);
412 	pci_write_config_dword (bdf, 0xB4, 0x328A328A);
413 	pci_write_config_dword (bdf, 0xB8, 0x43924392);
414 	pci_write_config_dword (bdf, 0xBC, 0x40094009);
415 
416 	ctx->cc_present = TRUE;
417 	ctx->cc_block_read = p_sii_block_read;
418 	ctx->cc_atapi_read = p_sii_atapi_read;
419 
420 	PRINTF("Done p_sii initialization\n");
421 }
422 
init_controllers(void)423 static void init_controllers(void) //Will fill in controllers[] with the appropriate values.
424 {
425 	unsigned cnt;
426 	s_sii_early_init(&controllers[S_SII_POS]);
427 	s_4_sii_early_init(&controllers[S4_SII_POS]);
428 #ifdef CONFIG_SAM460EX
429 	sata2_460_early_init(&controllers[SATA2_460_POS]);
430 #endif
431 	p_sii_early_init(&controllers[P_SII_POS]);
432 
433 	PRINTF("Looping in early init for a total of %lu controllers to compute units per bus\n",
434 		((sizeof controllers)/(sizeof (struct controller_context))));
435 
436 	for(cnt=0; cnt < ((sizeof controllers)/(sizeof (struct controller_context))); cnt++)
437 	{
438 		controllers[cnt].cc_units_per_bus = controllers[cnt].cc_maxunit/controllers[cnt].cc_maxbus;
439 		PRINTF("Units per bus for bus %u: %u\n", cnt, controllers[cnt].cc_units_per_bus);
440 	}
441 
442 	PRINTF("Done init controllers\n");
443 }
444 
internal_ide_unit_scan(struct controller_context * ctx)445 static void internal_ide_unit_scan(struct controller_context * ctx) //Taken from cmd_ide.c/ide_init, but sort of preprocessed.
446 {
447 	unsigned char c;
448 	int i, bus;
449 
450 	unsigned int max_bus_scan;
451 	unsigned int ata_reset_time;
452 	char *s;
453 
454 	/*
455 	 * Wait for IDE to get ready.
456 	 * According to spec, this can take up to 31 seconds!
457 	 */
458 	PRINTF("Checking for %s\n",ctx->cc_maxbus_var);
459 	s = getenv(ctx->cc_maxbus_var);
460 	if (s)
461 	    max_bus_scan = simple_strtol(s, NULL, 10);
462 	else
463 	    max_bus_scan = ctx->cc_maxbus;
464 
465 	PRINTF("Now looping for a total of %u bus\n", max_bus_scan);
466 
467 	for (bus=0; bus<max_bus_scan; ++bus)
468 	{
469 		int dev = bus * (ctx->cc_maxunit / max_bus_scan);
470         printf ("SATA Device %d: ",dev);
471 
472 		ctx->cc_bus_ok[bus] = 0;
473 
474 		/* Select device
475 		 */
476 		udelay (100000);		/* 100 ms */
477 		local_ide_outb (dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE_ADV(ctx,dev), ctx);
478 		udelay (100000);		/* 100 ms */
479 
480 		ata_reset_time = ATA_RESET_TIME;
481 		s = getenv("ide_reset_timeout");
482 		if (s) ata_reset_time = simple_strtol(s, NULL, 10);
483 
484 		i = 0;
485 		do {
486 			udelay (10000);		/* 10 ms */
487 
488 			c = local_ide_inb (dev, ATA_STATUS, ctx);
489 			i++;
490 
491 			if (i > (ata_reset_time * 100))
492 			{
493 				puts ("* Timeout *\n");
494 
495 				/* If this is the second bus, the first one was OK */
496 				if (bus != 0)
497 				{
498 				    ctx->cc_bus_ok[bus] = FALSE;
499 				    goto skip_bus;
500 				}
501 				return;
502 			}
503 			if ((i >= 100) && ((i%100)==0))
504 			{
505 				putc ('.');
506 				PRINTF ("%x",c);
507 			}
508 		} while (c & ATA_STAT_BUSY);
509 
510 		if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT))
511 		{
512 			puts ("not available ");
513 			PRINTF ("Status = 0x%02X ", c);
514 #ifndef CONFIG_ATAPI /* ATAPI Devices do not set DRDY */
515 		} else  if ((c & ATA_STAT_READY) == 0) {
516 			puts ("not available ");
517 			PRINTF ("Status = 0x%02X ", c);
518 #endif
519 		} else {
520 			puts ("OK ");
521 			ctx->cc_bus_ok[bus] = TRUE;
522 		}
523 	}
524 
525 skip_bus:
526 	putc ('\n');
527 
528 	for (i=0; i<ctx->cc_maxunit; i++)
529 	{
530 		ctx->cc_units[i].type=DEV_TYPE_UNKNOWN;
531 		ctx->cc_units[i].if_type=IF_TYPE_IDE;
532 		ctx->cc_units[i].dev=i;
533 		ctx->cc_units[i].part_type=PART_TYPE_UNKNOWN;
534 		ctx->cc_units[i].blksz=0;
535 		ctx->cc_units[i].lba=0;
536 		ctx->cc_units[i].block_read=ctx->cc_block_read;
537 
538 		if (!ctx->cc_bus_ok[IDE_BUS(i)])
539 			continue;
540 
541 		local_ide_ident(&ctx->cc_units[i], ctx);
542 
543 		dev_print(&ctx->cc_units[i]);
544 
545 		if ((ctx->cc_units[i].lba > 0) && (ctx->cc_units[i].blksz > 0))
546 		{
547 			/* initialize partition type */
548 			init_part (&ctx->cc_units[i]);
549 		}
550 	}
551 }
552 
553 #ifdef CONFIG_SAM460EX
sata_460_initialize(struct controller_context * curr)554 void sata_460_initialize(struct controller_context *curr)
555 {
556 	PRINTF("CALLING init_sata\n");
557 
558 	int rc, i;
559 
560 	for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++)
561 	{
562 		memset(&sata_dev_desc[i], 0, sizeof(struct block_dev_desc));
563 		sata_dev_desc[i].if_type = IF_TYPE_SATA;
564 		sata_dev_desc[i].dev = i;
565 		sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
566 		sata_dev_desc[i].type = DEV_TYPE_HARDDISK;
567 		sata_dev_desc[i].lba = 0;
568 		sata_dev_desc[i].blksz = 512;
569 		sata_dev_desc[i].block_read = sata_read;
570 		//sata_dev_desc[i].block_write = sata_write;
571 
572 		rc = init_sata(i);
573 		rc = scan_sata(i);
574 		if ((sata_dev_desc[i].lba > 0) && (sata_dev_desc[i].blksz > 0))
575 			init_part(&sata_dev_desc[i]);
576 	}
577 
578 	curr->cc_units = &sata_dev_desc;
579 
580 	if (rc == 0)
581 	{
582 		for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i)
583 		{
584 			if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
585 				continue;
586 			printf ("SATA device %d:\n", i);
587 			dev_print(&curr->cc_units[i]);
588 		}
589 	}
590 }
591 #endif
592 
ide_controllers_init(void)593 void ide_controllers_init(void)
594 {
595 	unsigned cnt;
596 
597 	//Activates all the controllers
598 	init_controllers();
599 
600 	//Ok, now tries to scan the various HDD buses.
601 	for(cnt=0; cnt < sizeof(controllers)/sizeof(struct controller_context); cnt++)
602 	{
603 		struct controller_context *curr = &controllers[cnt];
604 
605 		if(curr->cc_present)
606 		{
607 			unsigned unit_index;
608 			PRINTF("doing unit scan for controller n. %u\n", cnt);
609 
610 #ifdef CONFIG_SAM460EX
611 			if (strcmp(curr->cc_description,"SATA2-460") == 0)
612 			{
613 				sata_460_initialize(curr);
614 			}
615 			else
616 #endif
617 			{
618 			    internal_ide_unit_scan(curr);
619 
620 				//For each unit tries to enable PIO4
621 
622 				for(unit_index = 0; unit_index < curr->cc_maxunit; unit_index++)
623 				{
624 					block_dev_desc_t * dev = &curr->cc_units[unit_index];
625 					if(dev->blksz)
626 					{
627 						local_ide_set_pio(unit_index, 4, curr);
628 					}
629 				}
630 			}
631 		}
632 	}
633 }
634 
generic_get_dev(const unsigned unit,const struct controller_context * const cc)635 static block_dev_desc_t * generic_get_dev(const unsigned unit, const struct controller_context * const cc)
636 {
637 	if(unit < cc->cc_maxunit)
638 	{
639 		if(cc->cc_units[unit].block_read)
640 		{
641 			return &cc->cc_units[unit];
642 		}
643 	}
644 	return NULL;
645 }
646 
s_sii_get_dev(const unsigned unit)647 block_dev_desc_t *s_sii_get_dev(const unsigned unit)
648 {
649 	return generic_get_dev(unit, &controllers[S_SII_POS]);
650 }
651 
s_sii_block_read(int dev,unsigned long start,lbaint_t blkcnt,unsigned long * buffer)652 static unsigned long s_sii_block_read(int dev, unsigned long start, lbaint_t blkcnt, unsigned long *buffer)
653 {
654 	return local_ide_read(dev, start, blkcnt, buffer, &controllers[S_SII_POS]);
655 }
656 
s_4_sii_get_dev(const unsigned unit)657 block_dev_desc_t *s_4_sii_get_dev(const unsigned unit)
658 {
659 	return generic_get_dev(unit, &controllers[S4_SII_POS]);
660 }
661 
s_4_sii_block_read(int dev,unsigned long start,lbaint_t blkcnt,unsigned long * buffer)662 static unsigned long s_4_sii_block_read(int dev, unsigned long start, lbaint_t blkcnt, unsigned long *buffer)
663 {
664 	return local_ide_read(dev, start, blkcnt, buffer, &controllers[S4_SII_POS]);
665 }
666 
667 #ifdef CONFIG_SAM460EX
sata2_460_get_dev(const unsigned unit)668 block_dev_desc_t * sata2_460_get_dev(const unsigned unit)
669 {
670 	return generic_get_dev(unit, &controllers[SATA2_460_POS]);
671 }
672 #endif
673 
p_sii_get_dev(const unsigned unit)674 block_dev_desc_t *p_sii_get_dev(const unsigned unit)
675 {
676 	return generic_get_dev(unit, &controllers[P_SII_POS]);
677 }
678 
p_sii_block_read(int dev,unsigned long start,lbaint_t blkcnt,unsigned long * buffer)679 static unsigned long p_sii_block_read(int dev, unsigned long start, lbaint_t blkcnt, unsigned long *buffer)
680 {
681 	return local_ide_read(dev, start, blkcnt, buffer, &controllers[P_SII_POS]);
682 }
683 
local_ide_read(int device,lbaint_t blknr,ulong blkcnt,ulong * buffer,const struct controller_context * const cc)684 static ulong local_ide_read (int device, lbaint_t blknr, ulong blkcnt, ulong *buffer, const struct controller_context * const cc)
685 {
686 	ulong n = 0;
687 	unsigned char c;
688 	unsigned char pwrsave=0; /* power save */
689 #ifdef CONFIG_LBA48
690 	unsigned char lba48 = 0;
691 
692 	if (blknr & (uint64_t)0x0000fffff0000000) {
693 		/* more than 28 bits used, use 48bit mode */
694 		lba48 = 1;
695 	}
696 #endif
697 	PRINTF ("ide_read dev %d start %p, blocks %lX buffer at %p\n",
698 		device, blknr, blkcnt, (ulong)buffer);
699 
700 	/* Select device
701 	 */
702 	local_ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE_ADV(cc,device), cc);
703 	c = local_ide_wait (device, IDE_TIME_OUT, cc);
704 
705 	if (c & ATA_STAT_BUSY) {
706 		printf ("IDE read: device %d not ready\n", device);
707 		goto IDE_READ_E;
708 	}
709 
710 	/* first check if the drive is in Powersaving mode, if yes,
711 	 * increase the timeout value */
712 	local_ide_outb (device, ATA_COMMAND,  ATA_CMD_CHK_PWR, cc);
713 	udelay (50);
714 
715 	c = local_ide_wait (device, IDE_TIME_OUT, cc);	/* can't take over 500 ms */
716 
717 	if (c & ATA_STAT_BUSY) {
718 		printf ("IDE read: device %d not ready\n", device);
719 		goto IDE_READ_E;
720 	}
721 	if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
722 		printf ("No Powersaving mode %X\n", c);
723 	} else {
724 		c = local_ide_inb(device,ATA_SECT_CNT, cc);
725 		PRINTF("Powersaving %02X\n",c);
726 		if(c==0)
727 			pwrsave=1;
728 	}
729 
730 	while (blkcnt-- > 0)
731 	{
732 		c = local_ide_wait (device, IDE_TIME_OUT, cc);
733 
734 		if (c & ATA_STAT_BUSY)
735 		{
736 			printf ("IDE read: device %d not ready\n", device);
737 			break;
738 		}
739 #ifdef CONFIG_LBA48
740 		if (lba48)
741 		{
742 			/* write high bits */
743 			local_ide_outb (device, ATA_SECT_CNT, 0, cc);
744 			local_ide_outb (device, ATA_LBA_LOW,  (blknr >> 24) & 0xFF, cc);
745 			local_ide_outb (device, ATA_LBA_MID,  (blknr >> 32) & 0xFF, cc);
746 			local_ide_outb (device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF, cc);
747 		}
748 #endif
749 		local_ide_outb (device, ATA_SECT_CNT, 1, cc);
750 		local_ide_outb (device, ATA_LBA_LOW,  (blknr >>  0) & 0xFF, cc);
751 		local_ide_outb (device, ATA_LBA_MID,  (blknr >>  8) & 0xFF, cc);
752 		local_ide_outb (device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF, cc);
753 
754 #ifdef CONFIG_LBA48
755 		if (lba48)
756 		{
757 			local_ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE_ADV(cc,device) , cc);
758 			local_ide_outb (device, ATA_COMMAND, ATA_CMD_READ_EXT, cc);
759 
760 		} else
761 #endif
762 		{
763 			local_ide_outb (device, ATA_DEV_HD,   ATA_LBA		|
764 						    ATA_DEVICE_ADV(cc, device)	|
765 						    ((blknr >> 24) & 0xF) , cc);
766 			local_ide_outb (device, ATA_COMMAND,  ATA_CMD_READ, cc);
767 		}
768 
769 		udelay (50);
770 
771 		if(pwrsave)
772 		{
773 			c = local_ide_wait (device, IDE_SPIN_UP_TIME_OUT, cc);	/* may take up to 4 sec */
774 			pwrsave=0;
775 		} else {
776 			c = local_ide_wait (device, IDE_TIME_OUT, cc);	/* can't take over 500 ms */
777 		}
778 
779 		if ((c&(ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR)) != ATA_STAT_DRQ) {
780 #if defined(CONFIG_SYS_64BIT_LBA) && defined(CONFIG_SYS_64BIT_VSPRINTF)
781 			printf ("Error (no IRQ) dev %d blk %qd: status 0x%02x\n",
782 				device, blknr, c);
783 #else
784 			printf ("Error (no IRQ) dev %d blk %ld: status 0x%02x\n",
785 				device, (ulong)blknr, c);
786 #endif
787 			break;
788 		}
789 
790 		local_input_data (device, buffer, ATA_SECTORWORDS, cc);
791 		(void) local_ide_inb (device, ATA_STATUS, cc);	/* clear IRQ */
792 
793 		++n;
794 		++blknr;
795 		buffer += ATA_SECTORWORDS;
796 	}
797 
798 IDE_READ_E:
799 	return (n);
800 }
801 
local_input_swap_data(int dev,ulong * sect_buf,int words,const struct controller_context * const cc)802 static void local_input_swap_data(int dev, ulong *sect_buf, int words, const struct controller_context * const cc)
803 {
804 	volatile ushort	*pbuf = (ushort *)(ATA_CURR_BASE_ADV(cc, dev)+ATA_DATA_REG);
805 	ushort	*dbuf = (ushort *)sect_buf;
806 
807 	PRINTF("in input swap data base for read is %lx\n", (unsigned long) pbuf);
808 
809 	while (words--) {
810 		EIEIO;
811 		*dbuf++ = ld_le16(pbuf);
812 		EIEIO;
813 		*dbuf++ = ld_le16(pbuf);
814 	}
815 }
816 
local_input_data(int dev,ulong * sect_buf,int words,const struct controller_context * const cc)817 static void local_input_data(int dev, ulong *sect_buf, int words, const struct controller_context * const cc)
818 {
819 	ushort	*dbuf;
820 	volatile ushort	*pbuf;
821 
822 	pbuf = (ushort *)(ATA_CURR_BASE_ADV(cc, dev)+ATA_DATA_REG);
823 	dbuf = (ushort *)sect_buf;
824 
825 	PRINTF("in input data base for read is %lx\n", (unsigned long) pbuf);
826 
827 	while (words--) {
828 		EIEIO;
829 		*dbuf++ = *pbuf;
830 		EIEIO;
831 		*dbuf++ = *pbuf;
832 	}
833 }
834 
local_ide_ident(block_dev_desc_t * dev_desc,struct controller_context * const ctx)835 static void local_ide_ident(block_dev_desc_t *dev_desc, struct controller_context * const ctx)
836 {
837 	ulong iobuf[ATA_SECTORWORDS] = { 0 };
838 	hd_driveid_t *iop = (hd_driveid_t *)iobuf;
839 
840 	int ii;
841 	int device;
842 	int max_bus_scan;
843 	int retries = 0;
844 	int do_retry = 0;
845 	char *s;
846 	unsigned char c;
847 
848 	device=dev_desc->dev;
849 	PRINTF("ENTERED local_ide_ident %p %d %p\n",dev_desc,device,ctx);
850 
851  	s = getenv(ctx->cc_maxbus_var);
852 	if (s) {
853 		max_bus_scan = simple_strtol(s, NULL, 10);
854 	} else {
855 		max_bus_scan = CONFIG_SYS_IDE_MAXBUS;
856 	}
857 
858 	if (device >= max_bus_scan*2) {
859 		dev_desc->type=DEV_TYPE_UNKNOWN;
860 		return;
861 	}
862 
863 	// issue a device reset, since it could happen the device is in an unkwown state
864 	//local_ide_outb (device, ATA_COMMAND, 0x08, ctx);
865 	/* wait 750 ms */
866 	//for (ii=0; ii<750; ++ii)
867 	//{
868 	//	udelay (1000);
869 	//}
870 
871 	/* Select device
872 	 */
873 
874 	local_ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE_ADV(ctx,device), ctx);
875 	udelay (100000);		/* 100 ms */
876 
877 	// issue a device reset, since it could happen the device is in an unkwown state
878 	local_ide_outb (device, ATA_COMMAND, 0x08, ctx);
879 	udelay (100000);		/* 100 ms */
880 
881 	dev_desc->if_type=IF_TYPE_IDE;
882 
883 	do_retry = 0;
884 	retries = 0;
885 
886 	/* Warning: This will be tricky to read */
887 	while (retries <= 2)
888 	{
889 		/* check signature */
890 		u8 tmp = local_ide_inb(device,ATA_SECT_NUM, ctx);
891 
892 		if (/*(tmp == 0x00) ||*/
893 			((tmp == 0x01) &&
894 			 (local_ide_inb(device,ATA_CYL_LOW, ctx)  == 0x14) &&
895 			 (local_ide_inb(device,ATA_CYL_HIGH, ctx) == 0xEB)))
896 		{
897 			/* ATAPI Signature found */
898 			PRINTF("ATAPI\n");
899 			dev_desc->if_type=IF_TYPE_ATAPI;
900 			/* Start Ident Command
901 			 */
902 			local_ide_outb (device, ATA_COMMAND, ATAPI_CMD_IDENT, ctx);
903 			/*
904 			 * Wait for completion - ATAPI devices need more time
905 			 * to become ready
906 			 */
907 			c = local_ide_wait (device, ATAPI_TIME_OUT, ctx);
908 		}
909 		else
910 		{
911 			/* Start Ident Command
912 			 */
913 			PRINTF("ATA\n");
914 			local_ide_outb (device, ATA_COMMAND, ATA_CMD_IDENT, ctx);
915 
916 			/* Wait for completion
917 			 */
918 			c = local_ide_wait (device, IDE_TIME_OUT, ctx);
919 		}
920 
921 		if (((c & ATA_STAT_DRQ) == 0) ||
922 		    ((c & (ATA_STAT_FAULT|ATA_STAT_ERR)) != 0) )
923 		{
924 			if (retries <=1) //== 0)
925 			{
926 				do_retry = 1;
927 			}
928 			else
929 			{
930 				return;
931 			}
932 		}
933 
934 		s = getenv("ide_doreset");
935 		if (s && strcmp(s, "on") == 0 && 1 == do_retry)
936 		{
937 			/* Need to soft reset the device in case it's an ATAPI...  */
938 			PRINTF("Retrying...\n");
939 			local_ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE_ADV(ctx,device), ctx);
940 			udelay(100000);
941 			local_ide_outb (device, ATA_COMMAND, 0x08, ctx);
942 			udelay (100000);	/* 100 ms */
943 			retries++;
944 		}
945 		else
946 		{
947 			retries = 100;
948 		}
949 	}	/* see above - ugly to read */
950 
951 	local_input_swap_data (device, iobuf, ATA_SECTORWORDS, ctx);
952 
953 	local_ident_cpy (dev_desc->revision, iop->fw_rev, sizeof(dev_desc->revision));
954 	local_ident_cpy (dev_desc->vendor, iop->model, sizeof(dev_desc->vendor));
955 	local_ident_cpy (dev_desc->product, iop->serial_no, sizeof(dev_desc->product));
956 
957 	if ((iop->config & 0x0080)==0x0080)
958 		dev_desc->removable = 1;
959 	else
960 		dev_desc->removable = 0;
961 
962 #ifdef CONFIG_ATAPI
963 	if (dev_desc->if_type==IF_TYPE_ATAPI)
964 	{
965 		local_atapi_inquiry(dev_desc, ctx);
966 		return;
967 	}
968 #endif /* CONFIG_ATAPI */
969 
970 	/* swap shorts */
971 	dev_desc->lba = (iop->lba_capacity << 16) | (iop->lba_capacity >> 16);
972 
973 #ifdef CONFIG_LBA48
974 	if (iop->command_set_2 & 0x0400) { /* LBA 48 support */
975 		dev_desc->lba48 = 1;
976 		dev_desc->lba = (unsigned long long)iop->lba48_capacity[0] |
977 						  ((unsigned long long)iop->lba48_capacity[1] << 16) |
978 						  ((unsigned long long)iop->lba48_capacity[2] << 32) |
979 						  ((unsigned long long)iop->lba48_capacity[3] << 48);
980 	} else {
981 		dev_desc->lba48 = 0;
982 	}
983 #endif /* CONFIG_LBA48 */
984 	/* assuming HD */
985 	dev_desc->type=DEV_TYPE_HARDDISK;
986 	dev_desc->blksz=ATA_BLOCKSIZE;
987 	dev_desc->lun=0; /* just to fill something in... */
988 }
989 
local_ide_wait(int dev,ulong t,const struct controller_context * const cc)990 static uchar local_ide_wait (int dev, ulong t, const struct controller_context * const cc)
991 {
992 	ulong delay = 10 * t;		/* poll every 100 us */
993 	uchar c;
994 
995 	while ((c = local_ide_inb(dev, ATA_STATUS, cc)) & ATA_STAT_BUSY)
996 	{
997 		udelay (100);
998 		if (delay-- == 0)
999 		{
1000 			break;
1001 		}
1002 	}
1003 	return (c);
1004 }
1005 
local_ident_cpy(unsigned char * dst,unsigned char * src,unsigned int len)1006 static void local_ident_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
1007 {
1008 	unsigned char *end, *last;
1009 
1010 	last = dst;
1011 	end  = src + len - 1;
1012 
1013 	/* reserve space for '\0' */
1014 	if (len < 2)
1015 		goto OUT;
1016 
1017 	/* skip leading white space */
1018 	while ((*src) && (src<end) && (*src==' '))
1019 		++src;
1020 
1021 	/* copy string, omitting trailing white space */
1022 	while ((*src) && (src<end))
1023 	{
1024 		*dst++ = *src;
1025 		if (*src++ != ' ')
1026 			last = dst;
1027 	}
1028 OUT:
1029 	*last = '\0';
1030 }
1031 
1032 #ifdef CONFIG_ATAPI
1033 /****************************************************************************
1034  * ATAPI Support
1035  */
1036 
1037 /* since ATAPI may use commands with not 4 bytes alligned length
1038  * we have our own transfer functions, 2 bytes aligned */
local_output_data_shorts(int dev,ushort * sect_buf,int shorts,const struct controller_context * const cc)1039 static void local_output_data_shorts(int dev, ushort *sect_buf, int shorts, const struct controller_context * const cc)
1040 {
1041 	ushort	*dbuf;
1042 	volatile ushort	*pbuf;
1043 
1044 	pbuf = (ushort *)(ATA_CURR_BASE_ADV(cc, dev)+ATA_DATA_REG);
1045 	dbuf = (ushort *)sect_buf;
1046 
1047 	AT_PRINTF("in output data shorts base for read is %lx\n", (unsigned long) pbuf);
1048 
1049 	while (shorts--)
1050 	{
1051 		EIEIO;
1052 		*pbuf = *dbuf++;
1053 	}
1054 }
1055 
local_input_data_shorts(int dev,ushort * sect_buf,int shorts,const struct controller_context * const cc)1056 static void local_input_data_shorts(int dev, ushort *sect_buf, int shorts, const struct controller_context * const cc)
1057 {
1058 	ushort	*dbuf;
1059 	volatile ushort	*pbuf;
1060 
1061 	pbuf = (ushort *)(ATA_CURR_BASE_ADV(cc, dev)+ATA_DATA_REG);
1062 	dbuf = (ushort *)sect_buf;
1063 
1064 	AT_PRINTF("in input data shorts base for read is %lx\n", (unsigned long) pbuf);
1065 
1066 	while (shorts--)
1067 	{
1068 		EIEIO;
1069 		*dbuf++ = *pbuf;
1070 	}
1071 }
1072 
1073 /*
1074  * Wait until (Status & mask) == res, or timeout (in ms)
1075  * Return last status
1076  * This is used since some ATAPI CD ROMs clears their Busy Bit first
1077  * and then they set their DRQ Bit
1078  */
local_atapi_wait_mask(int dev,ulong t,uchar mask,uchar res,const struct controller_context * const cc)1079 static uchar local_atapi_wait_mask (int dev, ulong t,uchar mask, uchar res, const struct controller_context * const cc)
1080 {
1081 	ulong delay = 10 * t;		/* poll every 100 us */
1082 	uchar c;
1083 
1084 	c = local_ide_inb(dev,ATA_DEV_CTL, cc); /* prevents to read the status before valid */
1085 	while (((c = local_ide_inb(dev, ATA_STATUS, cc)) & mask) != res)
1086 	{
1087 		/* break if error occurs (doesn't make sense to wait more) */
1088 		if((c & ATA_STAT_ERR)==ATA_STAT_ERR)
1089 			break;
1090 		udelay (100);
1091 		if (delay-- == 0)
1092 		{
1093 			break;
1094 		}
1095 	}
1096 	return (c);
1097 }
1098 
1099 /*
1100  * issue an atapi command
1101  */
local_atapi_issue(int device,unsigned char * ccb,int ccblen,unsigned char * buffer,int buflen,const struct controller_context * const cc)1102 static unsigned char local_atapi_issue(int device,unsigned char* ccb,int ccblen, unsigned char * buffer,int buflen, const struct controller_context * const cc)
1103 {
1104 	unsigned char c,err,mask,res;
1105 	int n;
1106 
1107 	/* Select device
1108 	 */
1109 	mask = ATA_STAT_BUSY|ATA_STAT_DRQ;
1110 	res = 0;
1111 /*
1112 #ifdef	CONFIG_AMIGAONEG3SE
1113 # warning THF: Removed LBA mode ???
1114 #endif
1115 */
1116 	local_ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE_ADV(cc,device), cc);
1117 	c = local_atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res, cc);
1118 	if ((c & mask) != res)
1119 	{
1120 		printf ("ATAPI_ISSUE: device %d not ready status %X\n", device,c);
1121 		err=0xFF;
1122 		goto AI_OUT;
1123 	}
1124 	/* write taskfile */
1125 	local_ide_outb (device, ATA_ERROR_REG, 0, cc); /* no DMA, no overlaped */
1126 	local_ide_outb (device, ATA_SECT_CNT, 0, cc);
1127 	local_ide_outb (device, ATA_SECT_NUM, 0, cc);
1128 	local_ide_outb (device, ATA_CYL_LOW,  (unsigned char)(buflen & 0xFF), cc);
1129 	local_ide_outb (device, ATA_CYL_HIGH, (unsigned char)((buflen>>8) & 0xFF), cc);
1130 
1131 /*
1132 #ifdef	CONFIG_AMIGAONEG3SE
1133 # warning THF: Removed LBA mode ???
1134 #endif
1135 */
1136 	local_ide_outb (device, ATA_DEV_HD,   ATA_LBA | ATA_DEVICE_ADV(cc,device), cc);
1137 
1138 	local_ide_outb (device, ATA_COMMAND,  ATAPI_CMD_PACKET, cc);
1139 	udelay (50);
1140 
1141 	mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1142 	res = ATA_STAT_DRQ;
1143 	c = local_atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res, cc);
1144 
1145 	if ((c & mask) != res) { /* DRQ must be 1, BSY 0 */
1146 		printf ("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",device,c);
1147 		err=0xFF;
1148 		goto AI_OUT;
1149 	}
1150 
1151 	local_output_data_shorts (device, (unsigned short *)ccb,ccblen/2, cc); /* write command block */
1152  	/* ATAPI Command written wait for completition */
1153 	/* Was 5000 in the original firmware, for QEMU we could get rid of it */
1154 	udelay (50); /* device must set bsy */
1155 
1156 	mask = ATA_STAT_DRQ|ATA_STAT_BUSY|ATA_STAT_ERR;
1157 	/* if no data wait for DRQ = 0 BSY = 0
1158 	 * if data wait for DRQ = 1 BSY = 0 */
1159 	res=0;
1160 	if(buflen)
1161 		res = ATA_STAT_DRQ;
1162 	c = local_atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res, cc);
1163 	if ((c & mask) != res )
1164 	{
1165 		if (c & ATA_STAT_ERR)
1166 		{
1167 			err=(local_ide_inb(device,ATA_ERROR_REG, cc))>>4;
1168 			AT_PRINTF("atapi_issue 1 returned sense key %X status %02X\n",err,c);
1169 		}
1170 		else
1171 		{
1172 			printf ("ATAPI_ISSUE: (no DRQ) after sending ccb (%x) status 0x%02x\n", ccb[0],c);
1173 			err=0xFF;
1174 		}
1175 		goto AI_OUT;
1176 	}
1177 	n=local_ide_inb(device, ATA_CYL_HIGH, cc);
1178 	n<<=8;
1179 	n+=local_ide_inb(device, ATA_CYL_LOW, cc);
1180 	if(n>buflen)
1181 	{
1182 		printf("ERROR, transfer bytes %d requested only %d\n",n,buflen);
1183 		err=0xff;
1184 		goto AI_OUT;
1185 	}
1186 	if((n==0)&&(buflen<0))
1187 	{
1188 		printf("ERROR, transfer bytes %d requested %d\n",n,buflen);
1189 		err=0xff;
1190 		goto AI_OUT;
1191 	}
1192 	if(n!=buflen)
1193 	{
1194 		AT_PRINTF("WARNING, transfer bytes %d not equal with requested %d\n",n,buflen);
1195 	}
1196 	if(n!=0) { /* data transfer */
1197 		AT_PRINTF("ATAPI_ISSUE: %d Bytes to transfer\n",n);
1198 		 /* we transfer shorts */
1199 		n>>=1;
1200 		/* ok now decide if it is an in or output */
1201 		if ((local_ide_inb(device, ATA_SECT_CNT, cc)&0x02)==0)
1202 		{
1203 			AT_PRINTF("Write to device\n");
1204 			local_output_data_shorts(device,(unsigned short *)buffer,n, cc);
1205 		}
1206 		else
1207 		{
1208 			AT_PRINTF("Read from device @ %p shorts %d\n",buffer,n);
1209 			local_input_data_shorts(device,(unsigned short *)buffer,n, cc);
1210 		}
1211 	}
1212 	/* Was 5000 in the original firmware, for QEMU we could get rid of it */
1213 	udelay(50); /* seems that some CD ROMs need this... */
1214 	mask = ATA_STAT_BUSY|ATA_STAT_ERR;
1215 	res=0;
1216 	c = local_atapi_wait_mask(device,ATAPI_TIME_OUT,mask,res, cc);
1217 	if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1218 	{
1219 		err=(local_ide_inb(device,ATA_ERROR_REG, cc) >> 4);
1220 		AT_PRINTF("atapi_issue 2 returned sense key %X status %X\n",err,c);
1221 	}
1222 	else
1223 	{
1224 		err = 0;
1225 	}
1226 AI_OUT:
1227 	return (err);
1228 }
1229 
1230 /*
1231  * sending the command to atapi_issue. If an status other than good
1232  * returns, an request_sense will be issued
1233  */
1234 
1235 #define ATAPI_DRIVE_NOT_READY 	100
1236 #define ATAPI_UNIT_ATTN		10
1237 
local_atapi_issue_autoreq(int device,unsigned char * ccb,int ccblen,unsigned char * buffer,int buflen,const struct controller_context * const cc)1238 unsigned char local_atapi_issue_autoreq (int device,
1239 				   unsigned char* ccb,
1240 				   int ccblen,
1241 				   unsigned char *buffer,
1242 				   int buflen,
1243 				   const struct controller_context * const cc)
1244 {
1245 	unsigned char sense_data[18],sense_ccb[12];
1246 	unsigned char res,key,asc,ascq;
1247 	int notready,unitattn;
1248 
1249 	char *s;
1250 	unsigned int timeout, retrycnt;
1251 
1252 	s = getenv("ide_cd_timeout");
1253 	timeout = s ? (simple_strtol(s, NULL, 10)*1000000)/5 : 0;
1254 
1255 	retrycnt = 0;
1256 
1257 	unitattn=ATAPI_UNIT_ATTN;
1258 	notready=ATAPI_DRIVE_NOT_READY;
1259 
1260 retry:
1261 	res= local_atapi_issue(device,ccb,ccblen,buffer,buflen, cc);
1262 	if (res==0)
1263 		return (0); /* Ok */
1264 
1265 	if (res==0xFF)
1266 		return (0xFF); /* error */
1267 
1268 	AT_PRINTF("(auto_req)atapi_issue returned sense key %X\n",res);
1269 
1270 	memset(sense_ccb,0,sizeof(sense_ccb));
1271 	memset(sense_data,0,sizeof(sense_data));
1272 	sense_ccb[0]=ATAPI_CMD_REQ_SENSE;
1273 	sense_ccb[4]=18; /* allocation Length */
1274 
1275 	res=local_atapi_issue(device,sense_ccb,12,sense_data,18, cc);
1276 	key=(sense_data[2]&0xF);
1277 	asc=(sense_data[12]);
1278 	ascq=(sense_data[13]);
1279 
1280 	AT_PRINTF("ATAPI_CMD_REQ_SENSE returned %x\n",res);
1281 	AT_PRINTF(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
1282 		sense_data[0],
1283 		key,
1284 		asc,
1285 		ascq);
1286 
1287 	if((key==0))
1288 		return 0; /* ok device ready */
1289 
1290 	if((key==5) && (asc==0x24) && (ascq==00))
1291 	{
1292 		// this patch is required by some slim DVD/CD
1293 		AT_PRINTF("CDB in UFI command contains illegal value\n");
1294 		return 0; /* ok device ready */
1295 	}
1296 
1297 	if((key==6)|| (asc==0x29) || (asc==0x28)) { /* Unit Attention */
1298 		if(unitattn-->0)
1299 		{
1300 			udelay(200*1000);
1301 			goto retry;
1302 		}
1303 		printf("Unit Attention, tried %d\n",ATAPI_UNIT_ATTN);
1304 		goto error;
1305 	}
1306 
1307 	if((asc==0x4) && (ascq==0x1)) { /* not ready, but will be ready soon */
1308 		if (notready-->0)
1309 		{
1310 			udelay(200*1000);
1311 			goto retry;
1312 		}
1313 		printf("Drive not ready, tried %d times\n",ATAPI_DRIVE_NOT_READY);
1314 		goto error;
1315 	}
1316 
1317 	if(asc==0x3a)
1318 	{
1319 		AT_PRINTF("Media not present\n");
1320 		goto error;
1321 	}
1322 
1323 	if ((sense_data[2]&0xF)==0x0B)
1324 	{
1325 		AT_PRINTF("ABORTED COMMAND...retry\n");
1326 		if (retrycnt++ < 4)
1327 			goto retry;
1328 		return (0xFF);
1329 	}
1330 
1331 	if ((sense_data[2]&0xf) == 0x02 &&
1332 	    sense_data[12] == 0x04	&&
1333 	    sense_data[13] == 0x01	)
1334 	{
1335 		AT_PRINTF("Waiting for unit to become active\n");
1336 		udelay(timeout);
1337 		if (retrycnt++ < 4)
1338 			goto retry;
1339 		return 0xFF;
1340 	}
1341 
1342 	printf ("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1343 error:
1344 	AT_PRINTF ("ERROR Sense key %02X ASC %02X ASCQ %02X\n",key,asc,ascq);
1345 	return (0xFF);
1346 }
1347 
1348 
local_atapi_inquiry(block_dev_desc_t * dev_desc,struct controller_context * const ctx)1349 static void local_atapi_inquiry(block_dev_desc_t * dev_desc, struct controller_context * const ctx)
1350 {
1351 	unsigned char ccb[12]; /* Command descriptor block */
1352 	unsigned char iobuf[64]; /* temp buf */
1353 	unsigned char c;
1354 	int device;
1355 
1356 	device=dev_desc->dev;
1357 	dev_desc->type=DEV_TYPE_UNKNOWN; /* not yet valid */
1358 	dev_desc->block_read=ctx->cc_atapi_read;
1359 
1360 	memset(ccb,0,sizeof(ccb));
1361 	memset(iobuf,0,sizeof(iobuf));
1362 
1363 	ccb[0]=ATAPI_CMD_INQUIRY;
1364 	ccb[4]=40; /* allocation Legnth */
1365 	c=local_atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,40, ctx);
1366 
1367 	AT_PRINTF("ATAPI_CMD_INQUIRY returned %x\n",c);
1368 	if (c!=0)
1369 		return;
1370 
1371 	/* copy device ident strings */
1372 	local_ident_cpy(dev_desc->vendor,&iobuf[8],8);
1373 	local_ident_cpy(dev_desc->product,&iobuf[16],16);
1374 	local_ident_cpy(dev_desc->revision,&iobuf[32],5);
1375 
1376 	dev_desc->lun=0;
1377 	dev_desc->lba=0;
1378 	dev_desc->blksz=0;
1379 	dev_desc->type=iobuf[0] & 0x1f;
1380 
1381 	if ((iobuf[1]&0x80)==0x80)
1382 		dev_desc->removable = 1;
1383 	else
1384 		dev_desc->removable = 0;
1385 
1386 	memset(ccb,0,sizeof(ccb));
1387 	memset(iobuf,0,sizeof(iobuf));
1388 	ccb[0]=ATAPI_CMD_START_STOP;
1389 	ccb[4]=0x03; /* start */
1390 
1391 	c=local_atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0, ctx);
1392 
1393 	AT_PRINTF("ATAPI_CMD_START_STOP returned %x\n",c);
1394 	if (c!=0)
1395 		return;
1396 
1397 	memset(ccb,0,sizeof(ccb));
1398 	memset(iobuf,0,sizeof(iobuf));
1399 	c=local_atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,0, ctx);
1400 
1401 	AT_PRINTF("ATAPI_CMD_UNIT_TEST_READY returned %x\n",c);
1402 	if (c!=0)
1403 		return;
1404 
1405 	memset(ccb,0,sizeof(ccb));
1406 	memset(iobuf,0,sizeof(iobuf));
1407 	ccb[0]=ATAPI_CMD_READ_CAP;
1408 	c=local_atapi_issue_autoreq(device,ccb,12,(unsigned char *)iobuf,8, ctx);
1409 	AT_PRINTF("ATAPI_CMD_READ_CAP returned %x\n",c);
1410 	if (c!=0)
1411 		return;
1412 
1413 	AT_PRINTF("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
1414 		iobuf[0],iobuf[1],iobuf[2],iobuf[3],
1415 		iobuf[4],iobuf[5],iobuf[6],iobuf[7]);
1416 
1417 	dev_desc->lba  =((unsigned long)iobuf[0]<<24) +
1418 			((unsigned long)iobuf[1]<<16) +
1419 			((unsigned long)iobuf[2]<< 8) +
1420 			((unsigned long)iobuf[3]);
1421 	dev_desc->blksz=((unsigned long)iobuf[4]<<24) +
1422 			((unsigned long)iobuf[5]<<16) +
1423 			((unsigned long)iobuf[6]<< 8) +
1424 			((unsigned long)iobuf[7]);
1425 #ifdef CONFIG_LBA48
1426 	dev_desc->lba48 = 0; /* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
1427 #endif
1428 	return;
1429 }
1430 
1431 /*
1432  * atapi_read:
1433  * we transfer only one block per command, since the multiple DRQ per
1434  * command is not yet implemented
1435  */
1436 #define ATAPI_READ_MAX_BYTES	2048	/* we read max 2kbytes */
1437 #define ATAPI_READ_BLOCK_SIZE	2048	/* assuming CD part */
1438 #define ATAPI_READ_MAX_BLOCK ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE	/* max blocks */
1439 
local_atapi_read(int device,lbaint_t blknr,ulong blkcnt,ulong * buffer,const struct controller_context * const cc)1440 static ulong local_atapi_read (int device, lbaint_t blknr, ulong blkcnt, ulong *buffer,
1441 				const struct controller_context * const cc)
1442 {
1443 	ulong n = 0;
1444 	unsigned char ccb[12]; /* Command descriptor block */
1445 	ulong cnt;
1446 
1447 	AT_PRINTF("atapi_read dev %d start %lX, blocks %lX buffer at %lX\n",
1448 		device, blknr, blkcnt, (ulong)buffer);
1449 
1450 	do {
1451 		if (blkcnt>ATAPI_READ_MAX_BLOCK)
1452 		{
1453 			cnt=ATAPI_READ_MAX_BLOCK;
1454 		}
1455 		else
1456 		{
1457 			cnt=blkcnt;
1458 		}
1459 		ccb[0]=ATAPI_CMD_READ_12;
1460 		ccb[1]=0; /* reserved */
1461 		ccb[2]=(unsigned char) (blknr>>24) & 0xFF; /* MSB Block */
1462 		ccb[3]=(unsigned char) (blknr>>16) & 0xFF; /*  */
1463 		ccb[4]=(unsigned char) (blknr>> 8) & 0xFF;
1464 		ccb[5]=(unsigned char)  blknr      & 0xFF; /* LSB Block */
1465 		ccb[6]=(unsigned char) (cnt  >>24) & 0xFF; /* MSB Block count */
1466 		ccb[7]=(unsigned char) (cnt  >>16) & 0xFF;
1467 		ccb[8]=(unsigned char) (cnt  >> 8) & 0xFF;
1468 		ccb[9]=(unsigned char)  cnt	   & 0xFF; /* LSB Block */
1469 		ccb[10]=0; /* reserved */
1470 		ccb[11]=0; /* reserved */
1471 
1472 		if (local_atapi_issue_autoreq(device,ccb,12,
1473 					(unsigned char *)buffer,
1474 					cnt*ATAPI_READ_BLOCK_SIZE, cc) == 0xFF)
1475 		{
1476 			return (n);
1477 		}
1478 		n+=cnt;
1479 		blkcnt-=cnt;
1480 		blknr+=cnt;
1481 		buffer+=cnt*(ATAPI_READ_BLOCK_SIZE/4); /* ulong blocksize in ulong */
1482 	}
1483 	while (blkcnt > 0);
1484 
1485 	return (n);
1486 }
1487 
s_sii_atapi_read(int device,lbaint_t blknr,ulong blkcnt,ulong * buffer)1488 static ulong s_sii_atapi_read (int device, lbaint_t blknr, ulong blkcnt, ulong *buffer)
1489 {
1490 	return local_atapi_read(device, blknr, blkcnt, buffer, &controllers[S_SII_POS]);//Note that uboot is broken if lbaint_t is 64 bit!
1491 }
1492 
s_4_sii_atapi_read(int device,lbaint_t blknr,ulong blkcnt,ulong * buffer)1493 static ulong s_4_sii_atapi_read (int device, lbaint_t blknr, ulong blkcnt, ulong *buffer)
1494 {
1495 	return local_atapi_read(device, blknr, blkcnt, buffer, &controllers[S4_SII_POS]);//Note that uboot is broken if lbaint_t is 64 bit!
1496 }
1497 
p_sii_atapi_read(int device,lbaint_t blknr,ulong blkcnt,ulong * buffer)1498 static ulong p_sii_atapi_read (int device, lbaint_t blknr, ulong blkcnt, ulong *buffer)
1499 {
1500 	return local_atapi_read(device, blknr, blkcnt, buffer, &controllers[P_SII_POS]);//Note that uboot is broken if lbaint_t is 64 bit!
1501 }
1502 
1503 /* ------------------------------------------------------------------------- */
1504 
1505 #endif /* CONFIG_ATAPI */
1506 
local_ide_set_pio(const int device,const unsigned char mode,const struct controller_context * const cc)1507 static _Bool local_ide_set_pio(const int device, const unsigned char mode, const struct controller_context * const cc)
1508 {
1509 	_Bool ret = 0;
1510 	unsigned char pwrsave=0, c; // power save
1511 
1512 	PRINTF ("Trying to set PIO mode %u for device %d \n", mode, device);
1513 
1514 	//Select device
1515 
1516 	local_ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE_ADV(cc,device), cc);
1517 	c = local_ide_wait (device, IDE_TIME_OUT, cc);
1518 
1519 	if (c & ATA_STAT_BUSY)
1520 	{
1521 		goto IDE_EXIT;
1522 	}
1523 
1524 	// first check if the drive is in Powersaving mode, if yes, increase the timeout value
1525 	local_ide_outb (device, ATA_COMMAND,  ATA_CMD_CHK_PWR, cc);
1526 	udelay (50);
1527 
1528 	c = local_ide_wait (device, IDE_TIME_OUT, cc);	// can't take over 500 ms
1529 
1530 	if (c & ATA_STAT_BUSY)
1531 	{
1532 		goto IDE_EXIT;
1533 	}
1534 
1535 	if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
1536 	{
1537 	}
1538 	else	{
1539 		c = local_ide_inb(device,ATA_SECT_CNT, cc);
1540 		PRINTF("Powersaving %02X\n",c);
1541 		if(c==0)
1542 			pwrsave=1;
1543 	}
1544 
1545 	//Ok, the real game starts here.
1546 
1547 	c = local_ide_wait (device, IDE_TIME_OUT, cc);
1548 
1549 	if (c & ATA_STAT_BUSY)
1550 	{
1551 		goto IDE_EXIT;
1552 	}
1553 
1554 	local_ide_outb (device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE_ADV(cc,device), cc);
1555 	local_ide_outb (device, ATA_SECT_NUM, 0x00, cc);
1556 	local_ide_outb (device, ATA_CYL_LOW,  0x00, cc);
1557 	local_ide_outb (device, ATA_CYL_HIGH, 0x00, cc);
1558 
1559 	local_ide_outb (device, ATA_ERROR_REG, 0x03, cc); //Set tramsfer mode; this is the "feature" register when writing.
1560 	local_ide_outb (device, ATA_SECT_CNT, 8|mode, cc); //Sets selected PIO mode
1561 
1562 	//This one must be the last one, and it actually starts the command.
1563 	local_ide_outb (device, ATA_COMMAND,  ATA_CMD_SETF, cc);
1564 
1565 	udelay (50);
1566 
1567 	if(pwrsave)
1568 	{
1569 		c = local_ide_wait (device, IDE_SPIN_UP_TIME_OUT, cc);	// may take up to 4 sec
1570 		pwrsave=0;
1571 	}
1572 	 else	{
1573 		c = local_ide_wait (device, IDE_TIME_OUT, cc);	 //can't take over 500 ms
1574 	}
1575 
1576 	if(!(c & ATA_STAT_ERR))
1577 	{
1578 		PRINTF("Done!\n");
1579 		ret = 1; //Done!
1580 	}
1581 	else PRINTF("Status is 0x%02x\n", c);
1582 	(void) local_ide_inb (device, ATA_STATUS, cc);	// clear IRQ
1583 
1584 IDE_EXIT:
1585 	return ret;
1586 }
1587 
1588 int local_do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
1589 //int local_do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
1590 
1591 U_BOOT_CMD(
1592 	sata,  5,  1,  local_do_ide,
1593 	"SATA sub-system",
1594 	"sata reset [x] - init SATA controller 'x'\n"
1595 	"sata info [x] - show available SATA devices on controller 'x'\n"
1596 	"sata device [x] [dev] - select device 'dev' on controller 'x'\n"
1597 	"sata part [x] [dev] - print partition table of device 'dev' on controller 'x'\n"
1598 
1599 );
1600 
1601 /*
1602 U_BOOT_CMD(
1603 	diskboot,	4,	1,	local_do_diskboot,
1604 	"boot from IDE device",
1605 	"loadAddr dev:part [x] - boot from IDE device 'dev' on controller 'x'\n"
1606 );
1607 */
1608 //This one should be inlined, but it's not because of code size concerns.
valid_controller_num(WORD pos,WORD * old)1609 static BOOL valid_controller_num(WORD pos, WORD * old)
1610 {
1611 	int last;
1612 
1613 //#ifdef CONFIG_SAM460EX
1614 	last = SATA2_460_POS;
1615 //#else
1616 	last = S4_SII_POS;
1617 //#endif
1618 
1619     last = P_SII_POS;
1620 
1621 	//if(pos >= 0 && pos <= ((sizeof(controllers) / sizeof(struct controller_context))))
1622 	if ((pos >= 0) && (pos <= last))
1623 	{
1624 		if(controllers[pos].cc_present)
1625 		{
1626 			*old = pos;
1627 			return TRUE;
1628 		}
1629 	}
1630 
1631 	//if(*old >= 0 && *old <= ((sizeof(controllers) / sizeof(struct controller_context))))
1632 	//if ((*old >= 0) && (*old <= last))
1633 	//	return TRUE;
1634 
1635 	return FALSE;
1636 }
1637 
1638 //WARNING: prerequisite is that valid_controller_num was successfull!!
valid_unit_num(const WORD c_controller,WORD devoffset,WORD * old_dev)1639 static BOOL valid_unit_num(const WORD c_controller, WORD devoffset, WORD * old_dev)
1640 {
1641 	struct controller_context * curr_cont = &controllers[c_controller];
1642 
1643 	if(devoffset >= 0 && devoffset <= curr_cont->cc_maxunit)
1644 	{
1645 		if(curr_cont->cc_units[devoffset].type != DEV_TYPE_UNKNOWN && curr_cont->cc_units[devoffset].blksz)
1646 		{
1647 			if(old_dev) *old_dev = devoffset;
1648 			return TRUE;
1649 		}
1650 	}
1651 
1652 	if(old_dev)
1653 	{
1654 		devoffset = *old_dev;
1655 
1656 		if(devoffset >= 0 && devoffset <= curr_cont->cc_maxunit)
1657 		{
1658 			if(curr_cont->cc_units[devoffset].type != DEV_TYPE_UNKNOWN && curr_cont->cc_units[devoffset].blksz)
1659 				return TRUE;
1660 		}
1661 	}
1662 
1663 	return FALSE;
1664 }
1665 
1666 struct match_opt {
1667 	char *	mo_string;
1668 	WORD	mo_value;
1669 	UWORD	mo_minlen;
1670 };
1671 
1672 enum do_ide_vals {
1673 	UNDEFINED = -1,
1674 	VAL_RESET=0,
1675 	VAL_INFO,
1676 	VAL_DEVICE,
1677 	VAL_PART,
1678 };
1679 
1680 struct match_opt do_ide_opts[] ={
1681 	{ "reset",	VAL_RESET,	3},
1682 	{ "info",	VAL_INFO,	3},
1683 	{ "device",	VAL_DEVICE,	3},
1684 	{ "part",	VAL_PART,	4},
1685 	{ "",		UNDEFINED,	1}
1686 };
1687 
find_opt(char * const str,const struct match_opt * const mo,const UWORD len)1688 static WORD find_opt(char * const str, const struct match_opt * const mo, const UWORD len)
1689 {
1690 	UWORD cnt;
1691 	for(cnt=0; cnt < len; cnt++)
1692 	{
1693 		if(!strncmp(str, mo[cnt].mo_string, mo[cnt].mo_minlen))
1694 			return mo[cnt].mo_value;
1695 	}
1696 	return UNDEFINED;
1697 }
1698 
print_no_controller(void)1699 static void print_no_controller(void)
1700 {
1701 	puts("Selected controller doesn't exist\n");
1702 }
1703 
print_controller(const unsigned index)1704 static void print_controller(const unsigned index)
1705 {
1706 	UWORD i;
1707 	struct controller_context * cc = &controllers[index];
1708 
1709 	printf("Units on controller %s\n", cc->cc_description);
1710 
1711 	for (i=0; i< controllers[index].cc_maxunit; ++i)
1712 	{
1713 		if (controllers[index].cc_units[i].type==DEV_TYPE_UNKNOWN)
1714 			continue; // list only known devices
1715 		printf ("SATA device %d: ", i);
1716 		dev_print(&controllers[index].cc_units[i]);
1717 	}
1718 	puts("\n");
1719 }
1720 
1721 static WORD curr_controller = UNDEFINED;
1722 //The variable above holds the currently selected controller.
1723 //It's used by both local_do_ide and local_do_diskboot.
1724 
local_do_ide(cmd_tbl_t * cmdtp,int flag,int argc,char * argv[])1725 int local_do_ide (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1726 {
1727     WORD scanres, val1=0, val2=0;
1728     static WORD curr_device=UNDEFINED;
1729 
1730     if(argc == 0 || argc == 1)
1731 	{
1732 		printf ("Usage:\n%s\n", cmdtp->usage);
1733 		return 1;
1734 	}
1735 
1736 	scanres = find_opt(argv[1], do_ide_opts, sizeof(do_ide_opts)/sizeof(struct match_opt));
1737 	if(argc>2)
1738   	{
1739 		val1 = atoi(argv[2]);
1740 		if(argc>=3)
1741 			val2 = atoi(argv[3]);
1742 	}
1743 
1744 	switch(scanres)
1745   	{
1746 	case -1:
1747 		printf ("Usage:\n%s\n", cmdtp->usage);
1748 		return 1;
1749 
1750 	case VAL_RESET:
1751 		if(valid_controller_num(val1, &curr_controller))
1752 		{
1753 #ifdef CONFIG_SAM460EX
1754 			if (curr_controller == 2)
1755 				sata_460_initialize(&controllers[curr_controller]);
1756 			else
1757 #endif
1758 				internal_ide_unit_scan(&controllers[curr_controller]);
1759 		}
1760 		else
1761 		{
1762 			print_no_controller();
1763 		}
1764 		break;
1765 
1766 	case VAL_INFO:
1767 		if(valid_controller_num(val1, &curr_controller))
1768 		{
1769 			print_controller(curr_controller);
1770 		}
1771 		else print_no_controller();
1772 		break;
1773 
1774 	case VAL_DEVICE:
1775 		if(valid_controller_num(val1, &curr_controller) && valid_unit_num(curr_controller, val2, &curr_device))
1776 		{
1777 			printf("Device %d on %s selected.\n", curr_device, controllers[curr_controller].cc_description);
1778 		}
1779 		break;
1780 
1781 	case VAL_PART:
1782 		if(valid_controller_num(val1, &curr_controller) && valid_unit_num(curr_controller, val2, &curr_device))
1783 		{
1784 			block_dev_desc_t * currdev = &controllers[curr_controller].cc_units[curr_device];
1785 			if(currdev->part_type!=PART_TYPE_UNKNOWN)
1786 			{
1787 				print_part(currdev);
1788 			}
1789 		}
1790 		break;
1791 	default:
1792 		printf("Unknown option.\n");
1793 	}
1794 
1795 	return 1;
1796 }
1797 /*
1798 int local_do_diskboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1799 {
1800 	char *boot_device = NULL;
1801 	char *ep;
1802 	int dev, part = 0;
1803 	ulong cnt;
1804 	ulong addr;
1805 	disk_partition_t info;
1806 	image_header_t *hdr;
1807 	int rcode = 0;
1808 	block_dev_desc_t * target_unit;
1809 
1810 	switch (argc) {
1811 	case 1:
1812 		addr = CONFIG_SYS_LOAD_ADDR;
1813 		boot_device = getenv ("bootdevice");
1814 		break;
1815 	case 2:
1816 		addr = simple_strtoul(argv[1], NULL, 16);
1817 		boot_device = getenv ("bootdevice");
1818 		break;
1819 	case 3:
1820 		addr = simple_strtoul(argv[1], NULL, 16);
1821 		boot_device = argv[2];
1822 		break;
1823 	case 4:
1824 		{
1825 		WORD tentative_controller = atoi(argv[3]);
1826 		if(valid_controller_num(tentative_controller, &curr_controller))
1827 		{
1828 			addr = simple_strtoul(argv[1], NULL, 16);
1829 			boot_device = argv[2];
1830 			break;
1831 		}
1832 		//If the controller entry is not valid, code will fall through to the default entry.
1833 		}
1834 	default:
1835 		printf ("Usage:\n%s\n", cmdtp->usage);
1836 		SHOW_BOOT_PROGRESS (-1);
1837 		return 1;
1838 	}
1839 
1840 	if(curr_controller == UNDEFINED)
1841 	{
1842 		printf ("\n* No valid controller specified *\n", cmdtp->usage);
1843 		SHOW_BOOT_PROGRESS (-1);
1844 		return 1;
1845 	}
1846 
1847 	if (!boot_device) {
1848 		puts ("\n* No boot device *\n");
1849 		SHOW_BOOT_PROGRESS (-1);
1850 		return 1;
1851 	}
1852 
1853 	dev = simple_strtoul(boot_device, &ep, 16);
1854 
1855 	if (!valid_unit_num(curr_controller, dev, NULL))
1856 	{
1857 		printf ("\n* Device %d not available\n", dev);
1858 		SHOW_BOOT_PROGRESS (-1);
1859 		return 1;
1860 	}
1861 
1862 	//Now tries to find the partition to boot from, by moving the cursor upto where
1863 	//the end of the boot_device number was.
1864 	if (*ep) {
1865 		if (*ep != ':') {
1866 			puts ("\n* Invalid boot device, use `dev[:part]' *\n");
1867 			SHOW_BOOT_PROGRESS (-1);
1868 			return 1;
1869 		}
1870 		part = simple_strtoul(++ep, NULL, 16);
1871 	}
1872 
1873 	target_unit = &controllers[curr_controller].cc_units[dev];
1874 
1875 	if (get_partition_info (target_unit, part, &info)) {
1876 		SHOW_BOOT_PROGRESS (-1);
1877 		return 1;
1878 	}
1879 
1880 	if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
1881 	    (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
1882 		printf ("\n* Invalid partition type \"%.32s\""
1883 			" (expect \"" BOOT_PART_TYPE "\")\n",
1884 			info.type);
1885 		SHOW_BOOT_PROGRESS (-1);
1886 		return 1;
1887 	}
1888 
1889 	printf ("\nLoading from IDE device %d, partition %d: "
1890 		"Name: %.32s  Type: %.32s\n",
1891 		dev, part, info.name, info.type);
1892 
1893 	PRINTF ("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
1894 		info.start, info.size, info.blksz);
1895 
1896 	if (target_unit->block_read (dev, info.start, 1, (ulong *)addr) != 1)
1897 	{
1898 		printf ("* Read error on %d:%d\n", dev, part);
1899 		SHOW_BOOT_PROGRESS (-1);
1900 		return 1;
1901 	}
1902 
1903 	hdr = (image_header_t *)addr;
1904 
1905 	if (ntohl(hdr->ih_magic) == IH_MAGIC) {
1906 
1907 		//print_image_hdr (hdr);
1908 
1909 		cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
1910 		cnt += info.blksz - 1;
1911 		cnt /= info.blksz;
1912 		cnt -= 1;
1913 	} else {
1914 		printf("\n* Bad Magic Number *\n");
1915 		SHOW_BOOT_PROGRESS (-1);
1916 		return 1;
1917 	}
1918 
1919 	if (target_unit->block_read (dev, info.start+1, cnt,
1920 		      (ulong *)(addr+info.blksz)) != cnt) {
1921 		printf ("* Read error on %d:%d\n", dev, part);
1922 		SHOW_BOOT_PROGRESS (-1);
1923 		return 1;
1924 	}
1925 
1926 	// Loading ok, update default load address
1927 
1928 	load_addr = addr;
1929 
1930 	// Check if we should attempt an auto-start
1931 	if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1932 		char *local_args[2];
1933 		extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1934 
1935 		local_args[0] = argv[0];
1936 		local_args[1] = NULL;
1937 
1938 		printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
1939 
1940 		do_bootm (cmdtp, 0, 1, local_args);
1941 		rcode = 1;
1942 	}
1943 	return rcode;;
1944 }
1945 */
1946