1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2011
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 #include <common.h>
8 #include <ata.h>
9 #include <blk.h>
10 #include <dm.h>
11 #include <ide.h>
12 #include <log.h>
13 #include <part.h>
14 #include <watchdog.h>
15 #include <asm/io.h>
16 #include <linux/delay.h>
17 
18 #ifdef __PPC__
19 # define EIEIO		__asm__ volatile ("eieio")
20 # define SYNC		__asm__ volatile ("sync")
21 #else
22 # define EIEIO		/* nothing */
23 # define SYNC		/* nothing */
24 #endif
25 
26 /* Current offset for IDE0 / IDE1 bus access	*/
27 ulong ide_bus_offset[CONFIG_SYS_IDE_MAXBUS] = {
28 #if defined(CONFIG_SYS_ATA_IDE0_OFFSET)
29 	CONFIG_SYS_ATA_IDE0_OFFSET,
30 #endif
31 #if defined(CONFIG_SYS_ATA_IDE1_OFFSET) && (CONFIG_SYS_IDE_MAXBUS > 1)
32 	CONFIG_SYS_ATA_IDE1_OFFSET,
33 #endif
34 };
35 
36 static int ide_bus_ok[CONFIG_SYS_IDE_MAXBUS];
37 
38 struct blk_desc ide_dev_desc[CONFIG_SYS_IDE_MAXDEVICE];
39 
40 #define IDE_TIME_OUT	2000	/* 2 sec timeout */
41 
42 #define ATAPI_TIME_OUT	7000	/* 7 sec timeout (5 sec seems to work...) */
43 
44 #define IDE_SPIN_UP_TIME_OUT 5000 /* 5 sec spin-up timeout */
45 
46 #ifndef CONFIG_SYS_ATA_PORT_ADDR
47 #define CONFIG_SYS_ATA_PORT_ADDR(port) (port)
48 #endif
49 
50 #ifdef CONFIG_IDE_RESET
51 extern void ide_set_reset(int idereset);
52 
ide_reset(void)53 static void ide_reset(void)
54 {
55 	int i;
56 
57 	for (i = 0; i < CONFIG_SYS_IDE_MAXBUS; ++i)
58 		ide_bus_ok[i] = 0;
59 	for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i)
60 		ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
61 
62 	ide_set_reset(1);	/* assert reset */
63 
64 	/* the reset signal shall be asserted for et least 25 us */
65 	udelay(25);
66 
67 	WATCHDOG_RESET();
68 
69 	/* de-assert RESET signal */
70 	ide_set_reset(0);
71 
72 	/* wait 250 ms */
73 	for (i = 0; i < 250; ++i)
74 		udelay(1000);
75 }
76 #else
77 #define ide_reset()	/* dummy */
78 #endif /* CONFIG_IDE_RESET */
79 
80 /*
81  * Wait until Busy bit is off, or timeout (in ms)
82  * Return last status
83  */
ide_wait(int dev,ulong t)84 static uchar ide_wait(int dev, ulong t)
85 {
86 	ulong delay = 10 * t;	/* poll every 100 us */
87 	uchar c;
88 
89 	while ((c = ide_inb(dev, ATA_STATUS)) & ATA_STAT_BUSY) {
90 		udelay(100);
91 		if (delay-- == 0)
92 			break;
93 	}
94 	return c;
95 }
96 
97 /*
98  * copy src to dest, skipping leading and trailing blanks and null
99  * terminate the string
100  * "len" is the size of available memory including the terminating '\0'
101  */
ident_cpy(unsigned char * dst,unsigned char * src,unsigned int len)102 static void ident_cpy(unsigned char *dst, unsigned char *src,
103 		      unsigned int len)
104 {
105 	unsigned char *end, *last;
106 
107 	last = dst;
108 	end = src + len - 1;
109 
110 	/* reserve space for '\0' */
111 	if (len < 2)
112 		goto OUT;
113 
114 	/* skip leading white space */
115 	while ((*src) && (src < end) && (*src == ' '))
116 		++src;
117 
118 	/* copy string, omitting trailing white space */
119 	while ((*src) && (src < end)) {
120 		*dst++ = *src;
121 		if (*src++ != ' ')
122 			last = dst;
123 	}
124 OUT:
125 	*last = '\0';
126 }
127 
128 #ifdef CONFIG_ATAPI
129 /****************************************************************************
130  * ATAPI Support
131  */
132 
133 #if defined(CONFIG_IDE_SWAP_IO)
134 /* since ATAPI may use commands with not 4 bytes alligned length
135  * we have our own transfer functions, 2 bytes alligned */
ide_output_data_shorts(int dev,ushort * sect_buf,int shorts)136 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
137 {
138 	ushort *dbuf;
139 	volatile ushort *pbuf;
140 
141 	pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
142 	dbuf = (ushort *)sect_buf;
143 
144 	debug("in output data shorts base for read is %lx\n",
145 	      (unsigned long) pbuf);
146 
147 	while (shorts--) {
148 		EIEIO;
149 		*pbuf = *dbuf++;
150 	}
151 }
152 
ide_input_data_shorts(int dev,ushort * sect_buf,int shorts)153 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
154 {
155 	ushort *dbuf;
156 	volatile ushort *pbuf;
157 
158 	pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
159 	dbuf = (ushort *)sect_buf;
160 
161 	debug("in input data shorts base for read is %lx\n",
162 	      (unsigned long) pbuf);
163 
164 	while (shorts--) {
165 		EIEIO;
166 		*dbuf++ = *pbuf;
167 	}
168 }
169 
170 #else  /* ! CONFIG_IDE_SWAP_IO */
ide_output_data_shorts(int dev,ushort * sect_buf,int shorts)171 __weak void ide_output_data_shorts(int dev, ushort *sect_buf, int shorts)
172 {
173 	outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
174 }
175 
ide_input_data_shorts(int dev,ushort * sect_buf,int shorts)176 __weak void ide_input_data_shorts(int dev, ushort *sect_buf, int shorts)
177 {
178 	insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, shorts);
179 }
180 
181 #endif /* CONFIG_IDE_SWAP_IO */
182 
183 /*
184  * Wait until (Status & mask) == res, or timeout (in ms)
185  * Return last status
186  * This is used since some ATAPI CD ROMs clears their Busy Bit first
187  * and then they set their DRQ Bit
188  */
atapi_wait_mask(int dev,ulong t,uchar mask,uchar res)189 static uchar atapi_wait_mask(int dev, ulong t, uchar mask, uchar res)
190 {
191 	ulong delay = 10 * t;	/* poll every 100 us */
192 	uchar c;
193 
194 	/* prevents to read the status before valid */
195 	c = ide_inb(dev, ATA_DEV_CTL);
196 
197 	while (((c = ide_inb(dev, ATA_STATUS)) & mask) != res) {
198 		/* break if error occurs (doesn't make sense to wait more) */
199 		if ((c & ATA_STAT_ERR) == ATA_STAT_ERR)
200 			break;
201 		udelay(100);
202 		if (delay-- == 0)
203 			break;
204 	}
205 	return c;
206 }
207 
208 /*
209  * issue an atapi command
210  */
atapi_issue(int device,unsigned char * ccb,int ccblen,unsigned char * buffer,int buflen)211 unsigned char atapi_issue(int device, unsigned char *ccb, int ccblen,
212 			  unsigned char *buffer, int buflen)
213 {
214 	unsigned char c, err, mask, res;
215 	int n;
216 
217 	/* Select device
218 	 */
219 	mask = ATA_STAT_BUSY | ATA_STAT_DRQ;
220 	res = 0;
221 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
222 	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
223 	if ((c & mask) != res) {
224 		printf("ATAPI_ISSUE: device %d not ready status %X\n", device,
225 		       c);
226 		err = 0xFF;
227 		goto AI_OUT;
228 	}
229 	/* write taskfile */
230 	ide_outb(device, ATA_ERROR_REG, 0);	/* no DMA, no overlaped */
231 	ide_outb(device, ATA_SECT_CNT, 0);
232 	ide_outb(device, ATA_SECT_NUM, 0);
233 	ide_outb(device, ATA_CYL_LOW, (unsigned char) (buflen & 0xFF));
234 	ide_outb(device, ATA_CYL_HIGH,
235 		 (unsigned char) ((buflen >> 8) & 0xFF));
236 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
237 
238 	ide_outb(device, ATA_COMMAND, ATA_CMD_PACKET);
239 	udelay(50);
240 
241 	mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
242 	res = ATA_STAT_DRQ;
243 	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
244 
245 	if ((c & mask) != res) {	/* DRQ must be 1, BSY 0 */
246 		printf("ATAPI_ISSUE: Error (no IRQ) before sending ccb dev %d status 0x%02x\n",
247 		       device, c);
248 		err = 0xFF;
249 		goto AI_OUT;
250 	}
251 
252 	/* write command block */
253 	ide_output_data_shorts(device, (unsigned short *)ccb, ccblen / 2);
254 
255 	/* ATAPI Command written wait for completition */
256 	udelay(5000);		/* device must set bsy */
257 
258 	mask = ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR;
259 	/*
260 	 * if no data wait for DRQ = 0 BSY = 0
261 	 * if data wait for DRQ = 1 BSY = 0
262 	 */
263 	res = 0;
264 	if (buflen)
265 		res = ATA_STAT_DRQ;
266 	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
267 	if ((c & mask) != res) {
268 		if (c & ATA_STAT_ERR) {
269 			err = (ide_inb(device, ATA_ERROR_REG)) >> 4;
270 			debug("atapi_issue 1 returned sense key %X status %02X\n",
271 			      err, c);
272 		} else {
273 			printf("ATAPI_ISSUE: (no DRQ) after sending ccb (%x)  status 0x%02x\n",
274 			       ccb[0], c);
275 			err = 0xFF;
276 		}
277 		goto AI_OUT;
278 	}
279 	n = ide_inb(device, ATA_CYL_HIGH);
280 	n <<= 8;
281 	n += ide_inb(device, ATA_CYL_LOW);
282 	if (n > buflen) {
283 		printf("ERROR, transfer bytes %d requested only %d\n", n,
284 		       buflen);
285 		err = 0xff;
286 		goto AI_OUT;
287 	}
288 	if ((n == 0) && (buflen < 0)) {
289 		printf("ERROR, transfer bytes %d requested %d\n", n, buflen);
290 		err = 0xff;
291 		goto AI_OUT;
292 	}
293 	if (n != buflen) {
294 		debug("WARNING, transfer bytes %d not equal with requested %d\n",
295 		      n, buflen);
296 	}
297 	if (n != 0) {		/* data transfer */
298 		debug("ATAPI_ISSUE: %d Bytes to transfer\n", n);
299 		/* we transfer shorts */
300 		n >>= 1;
301 		/* ok now decide if it is an in or output */
302 		if ((ide_inb(device, ATA_SECT_CNT) & 0x02) == 0) {
303 			debug("Write to device\n");
304 			ide_output_data_shorts(device, (unsigned short *)buffer,
305 					       n);
306 		} else {
307 			debug("Read from device @ %p shorts %d\n", buffer, n);
308 			ide_input_data_shorts(device, (unsigned short *)buffer,
309 					      n);
310 		}
311 	}
312 	udelay(5000);		/* seems that some CD ROMs need this... */
313 	mask = ATA_STAT_BUSY | ATA_STAT_ERR;
314 	res = 0;
315 	c = atapi_wait_mask(device, ATAPI_TIME_OUT, mask, res);
316 	if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
317 		err = (ide_inb(device, ATA_ERROR_REG) >> 4);
318 		debug("atapi_issue 2 returned sense key %X status %X\n", err,
319 		      c);
320 	} else {
321 		err = 0;
322 	}
323 AI_OUT:
324 	return err;
325 }
326 
327 /*
328  * sending the command to atapi_issue. If an status other than good
329  * returns, an request_sense will be issued
330  */
331 
332 #define ATAPI_DRIVE_NOT_READY	100
333 #define ATAPI_UNIT_ATTN		10
334 
atapi_issue_autoreq(int device,unsigned char * ccb,int ccblen,unsigned char * buffer,int buflen)335 unsigned char atapi_issue_autoreq(int device,
336 				  unsigned char *ccb,
337 				  int ccblen,
338 				  unsigned char *buffer, int buflen)
339 {
340 	unsigned char sense_data[18], sense_ccb[12];
341 	unsigned char res, key, asc, ascq;
342 	int notready, unitattn;
343 
344 	unitattn = ATAPI_UNIT_ATTN;
345 	notready = ATAPI_DRIVE_NOT_READY;
346 
347 retry:
348 	res = atapi_issue(device, ccb, ccblen, buffer, buflen);
349 	if (res == 0)
350 		return 0;	/* Ok */
351 
352 	if (res == 0xFF)
353 		return 0xFF;	/* error */
354 
355 	debug("(auto_req)atapi_issue returned sense key %X\n", res);
356 
357 	memset(sense_ccb, 0, sizeof(sense_ccb));
358 	memset(sense_data, 0, sizeof(sense_data));
359 	sense_ccb[0] = ATAPI_CMD_REQ_SENSE;
360 	sense_ccb[4] = 18;	/* allocation Length */
361 
362 	res = atapi_issue(device, sense_ccb, 12, sense_data, 18);
363 	key = (sense_data[2] & 0xF);
364 	asc = (sense_data[12]);
365 	ascq = (sense_data[13]);
366 
367 	debug("ATAPI_CMD_REQ_SENSE returned %x\n", res);
368 	debug(" Sense page: %02X key %02X ASC %02X ASCQ %02X\n",
369 	      sense_data[0], key, asc, ascq);
370 
371 	if ((key == 0))
372 		return 0;	/* ok device ready */
373 
374 	if ((key == 6) || (asc == 0x29) || (asc == 0x28)) { /* Unit Attention */
375 		if (unitattn-- > 0) {
376 			udelay(200 * 1000);
377 			goto retry;
378 		}
379 		printf("Unit Attention, tried %d\n", ATAPI_UNIT_ATTN);
380 		goto error;
381 	}
382 	if ((asc == 0x4) && (ascq == 0x1)) {
383 		/* not ready, but will be ready soon */
384 		if (notready-- > 0) {
385 			udelay(200 * 1000);
386 			goto retry;
387 		}
388 		printf("Drive not ready, tried %d times\n",
389 		       ATAPI_DRIVE_NOT_READY);
390 		goto error;
391 	}
392 	if (asc == 0x3a) {
393 		debug("Media not present\n");
394 		goto error;
395 	}
396 
397 	printf("ERROR: Unknown Sense key %02X ASC %02X ASCQ %02X\n", key, asc,
398 	       ascq);
399 error:
400 	debug("ERROR Sense key %02X ASC %02X ASCQ %02X\n", key, asc, ascq);
401 	return 0xFF;
402 }
403 
404 /*
405  * atapi_read:
406  * we transfer only one block per command, since the multiple DRQ per
407  * command is not yet implemented
408  */
409 #define ATAPI_READ_MAX_BYTES	2048	/* we read max 2kbytes */
410 #define ATAPI_READ_BLOCK_SIZE	2048	/* assuming CD part */
411 #define ATAPI_READ_MAX_BLOCK	(ATAPI_READ_MAX_BYTES/ATAPI_READ_BLOCK_SIZE)
412 
atapi_read(struct blk_desc * block_dev,lbaint_t blknr,lbaint_t blkcnt,void * buffer)413 ulong atapi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
414 		 void *buffer)
415 {
416 	int device = block_dev->devnum;
417 	ulong n = 0;
418 	unsigned char ccb[12];	/* Command descriptor block */
419 	ulong cnt;
420 
421 	debug("atapi_read dev %d start " LBAF " blocks " LBAF
422 	      " buffer at %lX\n", device, blknr, blkcnt, (ulong) buffer);
423 
424 	do {
425 		if (blkcnt > ATAPI_READ_MAX_BLOCK)
426 			cnt = ATAPI_READ_MAX_BLOCK;
427 		else
428 			cnt = blkcnt;
429 
430 		ccb[0] = ATAPI_CMD_READ_12;
431 		ccb[1] = 0;	/* reserved */
432 		ccb[2] = (unsigned char) (blknr >> 24) & 0xFF;	/* MSB Block */
433 		ccb[3] = (unsigned char) (blknr >> 16) & 0xFF;	/*  */
434 		ccb[4] = (unsigned char) (blknr >> 8) & 0xFF;
435 		ccb[5] = (unsigned char) blknr & 0xFF;	/* LSB Block */
436 		ccb[6] = (unsigned char) (cnt >> 24) & 0xFF; /* MSB Block cnt */
437 		ccb[7] = (unsigned char) (cnt >> 16) & 0xFF;
438 		ccb[8] = (unsigned char) (cnt >> 8) & 0xFF;
439 		ccb[9] = (unsigned char) cnt & 0xFF;	/* LSB Block */
440 		ccb[10] = 0;	/* reserved */
441 		ccb[11] = 0;	/* reserved */
442 
443 		if (atapi_issue_autoreq(device, ccb, 12,
444 					(unsigned char *)buffer,
445 					cnt * ATAPI_READ_BLOCK_SIZE)
446 		    == 0xFF) {
447 			return n;
448 		}
449 		n += cnt;
450 		blkcnt -= cnt;
451 		blknr += cnt;
452 		buffer += (cnt * ATAPI_READ_BLOCK_SIZE);
453 	} while (blkcnt > 0);
454 	return n;
455 }
456 
atapi_inquiry(struct blk_desc * dev_desc)457 static void atapi_inquiry(struct blk_desc *dev_desc)
458 {
459 	unsigned char ccb[12];	/* Command descriptor block */
460 	unsigned char iobuf[64];	/* temp buf */
461 	unsigned char c;
462 	int device;
463 
464 	device = dev_desc->devnum;
465 	dev_desc->type = DEV_TYPE_UNKNOWN;	/* not yet valid */
466 #ifndef CONFIG_BLK
467 	dev_desc->block_read = atapi_read;
468 #endif
469 
470 	memset(ccb, 0, sizeof(ccb));
471 	memset(iobuf, 0, sizeof(iobuf));
472 
473 	ccb[0] = ATAPI_CMD_INQUIRY;
474 	ccb[4] = 40;		/* allocation Legnth */
475 	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 40);
476 
477 	debug("ATAPI_CMD_INQUIRY returned %x\n", c);
478 	if (c != 0)
479 		return;
480 
481 	/* copy device ident strings */
482 	ident_cpy((unsigned char *)dev_desc->vendor, &iobuf[8], 8);
483 	ident_cpy((unsigned char *)dev_desc->product, &iobuf[16], 16);
484 	ident_cpy((unsigned char *)dev_desc->revision, &iobuf[32], 5);
485 
486 	dev_desc->lun = 0;
487 	dev_desc->lba = 0;
488 	dev_desc->blksz = 0;
489 	dev_desc->log2blksz = LOG2_INVALID(typeof(dev_desc->log2blksz));
490 	dev_desc->type = iobuf[0] & 0x1f;
491 
492 	if ((iobuf[1] & 0x80) == 0x80)
493 		dev_desc->removable = 1;
494 	else
495 		dev_desc->removable = 0;
496 
497 	memset(ccb, 0, sizeof(ccb));
498 	memset(iobuf, 0, sizeof(iobuf));
499 	ccb[0] = ATAPI_CMD_START_STOP;
500 	ccb[4] = 0x03;		/* start */
501 
502 	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
503 
504 	debug("ATAPI_CMD_START_STOP returned %x\n", c);
505 	if (c != 0)
506 		return;
507 
508 	memset(ccb, 0, sizeof(ccb));
509 	memset(iobuf, 0, sizeof(iobuf));
510 	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 0);
511 
512 	debug("ATAPI_CMD_UNIT_TEST_READY returned %x\n", c);
513 	if (c != 0)
514 		return;
515 
516 	memset(ccb, 0, sizeof(ccb));
517 	memset(iobuf, 0, sizeof(iobuf));
518 	ccb[0] = ATAPI_CMD_READ_CAP;
519 	c = atapi_issue_autoreq(device, ccb, 12, (unsigned char *)iobuf, 8);
520 	debug("ATAPI_CMD_READ_CAP returned %x\n", c);
521 	if (c != 0)
522 		return;
523 
524 	debug("Read Cap: LBA %02X%02X%02X%02X blksize %02X%02X%02X%02X\n",
525 	      iobuf[0], iobuf[1], iobuf[2], iobuf[3],
526 	      iobuf[4], iobuf[5], iobuf[6], iobuf[7]);
527 
528 	dev_desc->lba = ((unsigned long) iobuf[0] << 24) +
529 		((unsigned long) iobuf[1] << 16) +
530 		((unsigned long) iobuf[2] << 8) + ((unsigned long) iobuf[3]);
531 	dev_desc->blksz = ((unsigned long) iobuf[4] << 24) +
532 		((unsigned long) iobuf[5] << 16) +
533 		((unsigned long) iobuf[6] << 8) + ((unsigned long) iobuf[7]);
534 	dev_desc->log2blksz = LOG2(dev_desc->blksz);
535 #ifdef CONFIG_LBA48
536 	/* ATAPI devices cannot use 48bit addressing (ATA/ATAPI v7) */
537 	dev_desc->lba48 = 0;
538 #endif
539 	return;
540 }
541 
542 #endif /* CONFIG_ATAPI */
543 
ide_ident(struct blk_desc * dev_desc)544 static void ide_ident(struct blk_desc *dev_desc)
545 {
546 	unsigned char c;
547 	hd_driveid_t iop;
548 
549 #ifdef CONFIG_ATAPI
550 	int retries = 0;
551 #endif
552 	int device;
553 
554 	device = dev_desc->devnum;
555 	printf("  Device %d: ", device);
556 
557 	/* Select device
558 	 */
559 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
560 	dev_desc->if_type = IF_TYPE_IDE;
561 #ifdef CONFIG_ATAPI
562 
563 	retries = 0;
564 
565 	/* Warning: This will be tricky to read */
566 	while (retries <= 1) {
567 		/* check signature */
568 		if ((ide_inb(device, ATA_SECT_CNT) == 0x01) &&
569 		    (ide_inb(device, ATA_SECT_NUM) == 0x01) &&
570 		    (ide_inb(device, ATA_CYL_LOW) == 0x14) &&
571 		    (ide_inb(device, ATA_CYL_HIGH) == 0xEB)) {
572 			/* ATAPI Signature found */
573 			dev_desc->if_type = IF_TYPE_ATAPI;
574 			/*
575 			 * Start Ident Command
576 			 */
577 			ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATAPI);
578 			/*
579 			 * Wait for completion - ATAPI devices need more time
580 			 * to become ready
581 			 */
582 			c = ide_wait(device, ATAPI_TIME_OUT);
583 		} else
584 #endif
585 		{
586 			/*
587 			 * Start Ident Command
588 			 */
589 			ide_outb(device, ATA_COMMAND, ATA_CMD_ID_ATA);
590 
591 			/*
592 			 * Wait for completion
593 			 */
594 			c = ide_wait(device, IDE_TIME_OUT);
595 		}
596 
597 		if (((c & ATA_STAT_DRQ) == 0) ||
598 		    ((c & (ATA_STAT_FAULT | ATA_STAT_ERR)) != 0)) {
599 #ifdef CONFIG_ATAPI
600 			{
601 				/*
602 				 * Need to soft reset the device
603 				 * in case it's an ATAPI...
604 				 */
605 				debug("Retrying...\n");
606 				ide_outb(device, ATA_DEV_HD,
607 					 ATA_LBA | ATA_DEVICE(device));
608 				udelay(100000);
609 				ide_outb(device, ATA_COMMAND, 0x08);
610 				udelay(500000);	/* 500 ms */
611 			}
612 			/*
613 			 * Select device
614 			 */
615 			ide_outb(device, ATA_DEV_HD,
616 				 ATA_LBA | ATA_DEVICE(device));
617 			retries++;
618 #else
619 			return;
620 #endif
621 		}
622 #ifdef CONFIG_ATAPI
623 		else
624 			break;
625 	}			/* see above - ugly to read */
626 
627 	if (retries == 2)	/* Not found */
628 		return;
629 #endif
630 
631 	ide_input_swap_data(device, (ulong *)&iop, ATA_SECTORWORDS);
632 
633 	ident_cpy((unsigned char *)dev_desc->revision, iop.fw_rev,
634 		  sizeof(dev_desc->revision));
635 	ident_cpy((unsigned char *)dev_desc->vendor, iop.model,
636 		  sizeof(dev_desc->vendor));
637 	ident_cpy((unsigned char *)dev_desc->product, iop.serial_no,
638 		  sizeof(dev_desc->product));
639 #ifdef __LITTLE_ENDIAN
640 	/*
641 	 * firmware revision, model, and serial number have Big Endian Byte
642 	 * order in Word. Convert all three to little endian.
643 	 *
644 	 * See CF+ and CompactFlash Specification Revision 2.0:
645 	 * 6.2.1.6: Identify Drive, Table 39 for more details
646 	 */
647 
648 	strswab(dev_desc->revision);
649 	strswab(dev_desc->vendor);
650 	strswab(dev_desc->product);
651 #endif /* __LITTLE_ENDIAN */
652 
653 	if ((iop.config & 0x0080) == 0x0080)
654 		dev_desc->removable = 1;
655 	else
656 		dev_desc->removable = 0;
657 
658 #ifdef CONFIG_ATAPI
659 	if (dev_desc->if_type == IF_TYPE_ATAPI) {
660 		atapi_inquiry(dev_desc);
661 		return;
662 	}
663 #endif /* CONFIG_ATAPI */
664 
665 #ifdef __BIG_ENDIAN
666 	/* swap shorts */
667 	dev_desc->lba = (iop.lba_capacity << 16) | (iop.lba_capacity >> 16);
668 #else  /* ! __BIG_ENDIAN */
669 	/*
670 	 * do not swap shorts on little endian
671 	 *
672 	 * See CF+ and CompactFlash Specification Revision 2.0:
673 	 * 6.2.1.6: Identfy Drive, Table 39, Word Address 57-58 for details.
674 	 */
675 	dev_desc->lba = iop.lba_capacity;
676 #endif /* __BIG_ENDIAN */
677 
678 #ifdef CONFIG_LBA48
679 	if (iop.command_set_2 & 0x0400) {	/* LBA 48 support */
680 		dev_desc->lba48 = 1;
681 		dev_desc->lba = (unsigned long long) iop.lba48_capacity[0] |
682 			((unsigned long long) iop.lba48_capacity[1] << 16) |
683 			((unsigned long long) iop.lba48_capacity[2] << 32) |
684 			((unsigned long long) iop.lba48_capacity[3] << 48);
685 	} else {
686 		dev_desc->lba48 = 0;
687 	}
688 #endif /* CONFIG_LBA48 */
689 	/* assuming HD */
690 	dev_desc->type = DEV_TYPE_HARDDISK;
691 	dev_desc->blksz = ATA_BLOCKSIZE;
692 	dev_desc->log2blksz = LOG2(dev_desc->blksz);
693 	dev_desc->lun = 0;	/* just to fill something in... */
694 
695 #if 0				/* only used to test the powersaving mode,
696 				 * if enabled, the drive goes after 5 sec
697 				 * in standby mode */
698 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
699 	c = ide_wait(device, IDE_TIME_OUT);
700 	ide_outb(device, ATA_SECT_CNT, 1);
701 	ide_outb(device, ATA_LBA_LOW, 0);
702 	ide_outb(device, ATA_LBA_MID, 0);
703 	ide_outb(device, ATA_LBA_HIGH, 0);
704 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
705 	ide_outb(device, ATA_COMMAND, 0xe3);
706 	udelay(50);
707 	c = ide_wait(device, IDE_TIME_OUT);	/* can't take over 500 ms */
708 #endif
709 }
710 
ide_outb(int dev,int port,unsigned char val)711 __weak void ide_outb(int dev, int port, unsigned char val)
712 {
713 	debug("ide_outb (dev= %d, port= 0x%x, val= 0x%02x) : @ 0x%08lx\n",
714 	      dev, port, val,
715 	      (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
716 
717 #if defined(CONFIG_IDE_AHB)
718 	if (port) {
719 		/* write command */
720 		ide_write_register(dev, port, val);
721 	} else {
722 		/* write data */
723 		outb(val, (ATA_CURR_BASE(dev)));
724 	}
725 #else
726 	outb(val, (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
727 #endif
728 }
729 
ide_inb(int dev,int port)730 __weak unsigned char ide_inb(int dev, int port)
731 {
732 	uchar val;
733 
734 #if defined(CONFIG_IDE_AHB)
735 	val = ide_read_register(dev, port);
736 #else
737 	val = inb((ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)));
738 #endif
739 
740 	debug("ide_inb (dev= %d, port= 0x%x) : @ 0x%08lx -> 0x%02x\n",
741 	      dev, port,
742 	      (ATA_CURR_BASE(dev) + CONFIG_SYS_ATA_PORT_ADDR(port)), val);
743 	return val;
744 }
745 
ide_init(void)746 void ide_init(void)
747 {
748 	unsigned char c;
749 	int i, bus;
750 
751 #ifdef CONFIG_IDE_PREINIT
752 	WATCHDOG_RESET();
753 
754 	if (ide_preinit()) {
755 		puts("ide_preinit failed\n");
756 		return;
757 	}
758 #endif /* CONFIG_IDE_PREINIT */
759 
760 	WATCHDOG_RESET();
761 
762 	/* ATAPI Drives seems to need a proper IDE Reset */
763 	ide_reset();
764 
765 	/*
766 	 * Wait for IDE to get ready.
767 	 * According to spec, this can take up to 31 seconds!
768 	 */
769 	for (bus = 0; bus < CONFIG_SYS_IDE_MAXBUS; ++bus) {
770 		int dev =
771 			bus * (CONFIG_SYS_IDE_MAXDEVICE /
772 			       CONFIG_SYS_IDE_MAXBUS);
773 
774 		printf("Bus %d: ", bus);
775 
776 		ide_bus_ok[bus] = 0;
777 
778 		/* Select device
779 		 */
780 		udelay(100000);	/* 100 ms */
781 		ide_outb(dev, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(dev));
782 		udelay(100000);	/* 100 ms */
783 		i = 0;
784 		do {
785 			udelay(10000);	/* 10 ms */
786 
787 			c = ide_inb(dev, ATA_STATUS);
788 			i++;
789 			if (i > (ATA_RESET_TIME * 100)) {
790 				puts("** Timeout **\n");
791 				return;
792 			}
793 			if ((i >= 100) && ((i % 100) == 0))
794 				putc('.');
795 
796 		} while (c & ATA_STAT_BUSY);
797 
798 		if (c & (ATA_STAT_BUSY | ATA_STAT_FAULT)) {
799 			puts("not available  ");
800 			debug("Status = 0x%02X ", c);
801 #ifndef CONFIG_ATAPI		/* ATAPI Devices do not set DRDY */
802 		} else if ((c & ATA_STAT_READY) == 0) {
803 			puts("not available  ");
804 			debug("Status = 0x%02X ", c);
805 #endif
806 		} else {
807 			puts("OK ");
808 			ide_bus_ok[bus] = 1;
809 		}
810 		WATCHDOG_RESET();
811 	}
812 
813 	putc('\n');
814 
815 	for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; ++i) {
816 		ide_dev_desc[i].type = DEV_TYPE_UNKNOWN;
817 		ide_dev_desc[i].if_type = IF_TYPE_IDE;
818 		ide_dev_desc[i].devnum = i;
819 		ide_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
820 		ide_dev_desc[i].blksz = 0;
821 		ide_dev_desc[i].log2blksz =
822 			LOG2_INVALID(typeof(ide_dev_desc[i].log2blksz));
823 		ide_dev_desc[i].lba = 0;
824 #ifndef CONFIG_BLK
825 		ide_dev_desc[i].block_read = ide_read;
826 		ide_dev_desc[i].block_write = ide_write;
827 #endif
828 		if (!ide_bus_ok[IDE_BUS(i)])
829 			continue;
830 		ide_ident(&ide_dev_desc[i]);
831 		dev_print(&ide_dev_desc[i]);
832 
833 #ifndef CONFIG_BLK
834 		if ((ide_dev_desc[i].lba > 0) && (ide_dev_desc[i].blksz > 0)) {
835 			/* initialize partition type */
836 			part_init(&ide_dev_desc[i]);
837 		}
838 #endif
839 	}
840 	WATCHDOG_RESET();
841 
842 #ifdef CONFIG_BLK
843 	struct udevice *dev;
844 
845 	uclass_first_device(UCLASS_IDE, &dev);
846 #endif
847 }
848 
849 /* We only need to swap data if we are running on a big endian cpu. */
850 #if defined(__LITTLE_ENDIAN)
ide_input_swap_data(int dev,ulong * sect_buf,int words)851 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
852 {
853 	ide_input_data(dev, sect_buf, words);
854 }
855 #else
ide_input_swap_data(int dev,ulong * sect_buf,int words)856 __weak void ide_input_swap_data(int dev, ulong *sect_buf, int words)
857 {
858 	volatile ushort *pbuf =
859 		(ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
860 	ushort *dbuf = (ushort *)sect_buf;
861 
862 	debug("in input swap data base for read is %lx\n",
863 	      (unsigned long) pbuf);
864 
865 	while (words--) {
866 #ifdef __MIPS__
867 		*dbuf++ = swab16p((u16 *)pbuf);
868 		*dbuf++ = swab16p((u16 *)pbuf);
869 #else
870 		*dbuf++ = ld_le16(pbuf);
871 		*dbuf++ = ld_le16(pbuf);
872 #endif /* !MIPS */
873 	}
874 }
875 #endif /* __LITTLE_ENDIAN */
876 
877 
878 #if defined(CONFIG_IDE_SWAP_IO)
ide_output_data(int dev,const ulong * sect_buf,int words)879 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
880 {
881 	ushort *dbuf;
882 	volatile ushort *pbuf;
883 
884 	pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
885 	dbuf = (ushort *)sect_buf;
886 	while (words--) {
887 		EIEIO;
888 		*pbuf = *dbuf++;
889 		EIEIO;
890 		*pbuf = *dbuf++;
891 	}
892 }
893 #else  /* ! CONFIG_IDE_SWAP_IO */
ide_output_data(int dev,const ulong * sect_buf,int words)894 __weak void ide_output_data(int dev, const ulong *sect_buf, int words)
895 {
896 #if defined(CONFIG_IDE_AHB)
897 	ide_write_data(dev, sect_buf, words);
898 #else
899 	outsw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
900 #endif
901 }
902 #endif /* CONFIG_IDE_SWAP_IO */
903 
904 #if defined(CONFIG_IDE_SWAP_IO)
ide_input_data(int dev,ulong * sect_buf,int words)905 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
906 {
907 	ushort *dbuf;
908 	volatile ushort *pbuf;
909 
910 	pbuf = (ushort *)(ATA_CURR_BASE(dev) + ATA_DATA_REG);
911 	dbuf = (ushort *)sect_buf;
912 
913 	debug("in input data base for read is %lx\n", (unsigned long) pbuf);
914 
915 	while (words--) {
916 		EIEIO;
917 		*dbuf++ = *pbuf;
918 		EIEIO;
919 		*dbuf++ = *pbuf;
920 	}
921 }
922 #else  /* ! CONFIG_IDE_SWAP_IO */
ide_input_data(int dev,ulong * sect_buf,int words)923 __weak void ide_input_data(int dev, ulong *sect_buf, int words)
924 {
925 #if defined(CONFIG_IDE_AHB)
926 	ide_read_data(dev, sect_buf, words);
927 #else
928 	insw(ATA_CURR_BASE(dev) + ATA_DATA_REG, sect_buf, words << 1);
929 #endif
930 }
931 
932 #endif /* CONFIG_IDE_SWAP_IO */
933 
934 #ifdef CONFIG_BLK
ide_read(struct udevice * dev,lbaint_t blknr,lbaint_t blkcnt,void * buffer)935 ulong ide_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
936 	       void *buffer)
937 #else
938 ulong ide_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
939 	       void *buffer)
940 #endif
941 {
942 #ifdef CONFIG_BLK
943 	struct blk_desc *block_dev = dev_get_uclass_plat(dev);
944 #endif
945 	int device = block_dev->devnum;
946 	ulong n = 0;
947 	unsigned char c;
948 	unsigned char pwrsave = 0;	/* power save */
949 
950 #ifdef CONFIG_LBA48
951 	unsigned char lba48 = 0;
952 
953 	if (blknr & 0x0000fffff0000000ULL) {
954 		/* more than 28 bits used, use 48bit mode */
955 		lba48 = 1;
956 	}
957 #endif
958 	debug("ide_read dev %d start " LBAF ", blocks " LBAF " buffer at %lX\n",
959 	      device, blknr, blkcnt, (ulong) buffer);
960 
961 	/* Select device
962 	 */
963 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
964 	c = ide_wait(device, IDE_TIME_OUT);
965 
966 	if (c & ATA_STAT_BUSY) {
967 		printf("IDE read: device %d not ready\n", device);
968 		goto IDE_READ_E;
969 	}
970 
971 	/* first check if the drive is in Powersaving mode, if yes,
972 	 * increase the timeout value */
973 	ide_outb(device, ATA_COMMAND, ATA_CMD_CHK_POWER);
974 	udelay(50);
975 
976 	c = ide_wait(device, IDE_TIME_OUT);	/* can't take over 500 ms */
977 
978 	if (c & ATA_STAT_BUSY) {
979 		printf("IDE read: device %d not ready\n", device);
980 		goto IDE_READ_E;
981 	}
982 	if ((c & ATA_STAT_ERR) == ATA_STAT_ERR) {
983 		printf("No Powersaving mode %X\n", c);
984 	} else {
985 		c = ide_inb(device, ATA_SECT_CNT);
986 		debug("Powersaving %02X\n", c);
987 		if (c == 0)
988 			pwrsave = 1;
989 	}
990 
991 
992 	while (blkcnt-- > 0) {
993 		c = ide_wait(device, IDE_TIME_OUT);
994 
995 		if (c & ATA_STAT_BUSY) {
996 			printf("IDE read: device %d not ready\n", device);
997 			break;
998 		}
999 #ifdef CONFIG_LBA48
1000 		if (lba48) {
1001 			/* write high bits */
1002 			ide_outb(device, ATA_SECT_CNT, 0);
1003 			ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1004 #ifdef CONFIG_SYS_64BIT_LBA
1005 			ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1006 			ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1007 #else
1008 			ide_outb(device, ATA_LBA_MID, 0);
1009 			ide_outb(device, ATA_LBA_HIGH, 0);
1010 #endif
1011 		}
1012 #endif
1013 		ide_outb(device, ATA_SECT_CNT, 1);
1014 		ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1015 		ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1016 		ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1017 
1018 #ifdef CONFIG_LBA48
1019 		if (lba48) {
1020 			ide_outb(device, ATA_DEV_HD,
1021 				 ATA_LBA | ATA_DEVICE(device));
1022 			ide_outb(device, ATA_COMMAND, ATA_CMD_READ_EXT);
1023 
1024 		} else
1025 #endif
1026 		{
1027 			ide_outb(device, ATA_DEV_HD, ATA_LBA |
1028 				 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1029 			ide_outb(device, ATA_COMMAND, ATA_CMD_READ);
1030 		}
1031 
1032 		udelay(50);
1033 
1034 		if (pwrsave) {
1035 			/* may take up to 4 sec */
1036 			c = ide_wait(device, IDE_SPIN_UP_TIME_OUT);
1037 			pwrsave = 0;
1038 		} else {
1039 			/* can't take over 500 ms */
1040 			c = ide_wait(device, IDE_TIME_OUT);
1041 		}
1042 
1043 		if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1044 		    ATA_STAT_DRQ) {
1045 			printf("Error (no IRQ) dev %d blk " LBAF
1046 			       ": status %#02x\n", device, blknr, c);
1047 			break;
1048 		}
1049 
1050 		ide_input_data(device, buffer, ATA_SECTORWORDS);
1051 		(void) ide_inb(device, ATA_STATUS);	/* clear IRQ */
1052 
1053 		++n;
1054 		++blknr;
1055 		buffer += ATA_BLOCKSIZE;
1056 	}
1057 IDE_READ_E:
1058 	return n;
1059 }
1060 
1061 #ifdef CONFIG_BLK
ide_write(struct udevice * dev,lbaint_t blknr,lbaint_t blkcnt,const void * buffer)1062 ulong ide_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
1063 		const void *buffer)
1064 #else
1065 ulong ide_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt,
1066 		const void *buffer)
1067 #endif
1068 {
1069 #ifdef CONFIG_BLK
1070 	struct blk_desc *block_dev = dev_get_uclass_plat(dev);
1071 #endif
1072 	int device = block_dev->devnum;
1073 	ulong n = 0;
1074 	unsigned char c;
1075 
1076 #ifdef CONFIG_LBA48
1077 	unsigned char lba48 = 0;
1078 
1079 	if (blknr & 0x0000fffff0000000ULL) {
1080 		/* more than 28 bits used, use 48bit mode */
1081 		lba48 = 1;
1082 	}
1083 #endif
1084 
1085 	/* Select device
1086 	 */
1087 	ide_outb(device, ATA_DEV_HD, ATA_LBA | ATA_DEVICE(device));
1088 
1089 	while (blkcnt-- > 0) {
1090 		c = ide_wait(device, IDE_TIME_OUT);
1091 
1092 		if (c & ATA_STAT_BUSY) {
1093 			printf("IDE read: device %d not ready\n", device);
1094 			goto WR_OUT;
1095 		}
1096 #ifdef CONFIG_LBA48
1097 		if (lba48) {
1098 			/* write high bits */
1099 			ide_outb(device, ATA_SECT_CNT, 0);
1100 			ide_outb(device, ATA_LBA_LOW, (blknr >> 24) & 0xFF);
1101 #ifdef CONFIG_SYS_64BIT_LBA
1102 			ide_outb(device, ATA_LBA_MID, (blknr >> 32) & 0xFF);
1103 			ide_outb(device, ATA_LBA_HIGH, (blknr >> 40) & 0xFF);
1104 #else
1105 			ide_outb(device, ATA_LBA_MID, 0);
1106 			ide_outb(device, ATA_LBA_HIGH, 0);
1107 #endif
1108 		}
1109 #endif
1110 		ide_outb(device, ATA_SECT_CNT, 1);
1111 		ide_outb(device, ATA_LBA_LOW, (blknr >> 0) & 0xFF);
1112 		ide_outb(device, ATA_LBA_MID, (blknr >> 8) & 0xFF);
1113 		ide_outb(device, ATA_LBA_HIGH, (blknr >> 16) & 0xFF);
1114 
1115 #ifdef CONFIG_LBA48
1116 		if (lba48) {
1117 			ide_outb(device, ATA_DEV_HD,
1118 				 ATA_LBA | ATA_DEVICE(device));
1119 			ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE_EXT);
1120 
1121 		} else
1122 #endif
1123 		{
1124 			ide_outb(device, ATA_DEV_HD, ATA_LBA |
1125 				 ATA_DEVICE(device) | ((blknr >> 24) & 0xF));
1126 			ide_outb(device, ATA_COMMAND, ATA_CMD_WRITE);
1127 		}
1128 
1129 		udelay(50);
1130 
1131 		/* can't take over 500 ms */
1132 		c = ide_wait(device, IDE_TIME_OUT);
1133 
1134 		if ((c & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR)) !=
1135 		    ATA_STAT_DRQ) {
1136 			printf("Error (no IRQ) dev %d blk " LBAF
1137 			       ": status %#02x\n", device, blknr, c);
1138 			goto WR_OUT;
1139 		}
1140 
1141 		ide_output_data(device, buffer, ATA_SECTORWORDS);
1142 		c = ide_inb(device, ATA_STATUS);	/* clear IRQ */
1143 		++n;
1144 		++blknr;
1145 		buffer += ATA_BLOCKSIZE;
1146 	}
1147 WR_OUT:
1148 	return n;
1149 }
1150 
1151 #if defined(CONFIG_OF_IDE_FIXUP)
ide_device_present(int dev)1152 int ide_device_present(int dev)
1153 {
1154 	if (dev >= CONFIG_SYS_IDE_MAXBUS)
1155 		return 0;
1156 	return ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN ? 0 : 1;
1157 }
1158 #endif
1159 
1160 #ifdef CONFIG_BLK
ide_blk_probe(struct udevice * udev)1161 static int ide_blk_probe(struct udevice *udev)
1162 {
1163 	struct blk_desc *desc = dev_get_uclass_plat(udev);
1164 
1165 	/* fill in device vendor/product/rev strings */
1166 	strncpy(desc->vendor, ide_dev_desc[desc->devnum].vendor,
1167 		BLK_VEN_SIZE);
1168 	desc->vendor[BLK_VEN_SIZE] = '\0';
1169 	strncpy(desc->product, ide_dev_desc[desc->devnum].product,
1170 		BLK_PRD_SIZE);
1171 	desc->product[BLK_PRD_SIZE] = '\0';
1172 	strncpy(desc->revision, ide_dev_desc[desc->devnum].revision,
1173 		BLK_REV_SIZE);
1174 	desc->revision[BLK_REV_SIZE] = '\0';
1175 
1176 	return 0;
1177 }
1178 
1179 static const struct blk_ops ide_blk_ops = {
1180 	.read	= ide_read,
1181 	.write	= ide_write,
1182 };
1183 
1184 U_BOOT_DRIVER(ide_blk) = {
1185 	.name		= "ide_blk",
1186 	.id		= UCLASS_BLK,
1187 	.ops		= &ide_blk_ops,
1188 	.probe		= ide_blk_probe,
1189 };
1190 
ide_probe(struct udevice * udev)1191 static int ide_probe(struct udevice *udev)
1192 {
1193 	struct udevice *blk_dev;
1194 	char name[20];
1195 	int blksz;
1196 	lbaint_t size;
1197 	int i;
1198 	int ret;
1199 
1200 	for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
1201 		if (ide_dev_desc[i].type != DEV_TYPE_UNKNOWN) {
1202 			sprintf(name, "blk#%d", i);
1203 
1204 			blksz = ide_dev_desc[i].blksz;
1205 			size = blksz * ide_dev_desc[i].lba;
1206 
1207 			/*
1208 			 * With CDROM, if there is no CD inserted, blksz will
1209 			 * be zero, don't bother to create IDE block device.
1210 			 */
1211 			if (!blksz)
1212 				continue;
1213 			ret = blk_create_devicef(udev, "ide_blk", name,
1214 						 IF_TYPE_IDE, i,
1215 						 blksz, size, &blk_dev);
1216 			if (ret)
1217 				return ret;
1218 		}
1219 	}
1220 
1221 	return 0;
1222 }
1223 
1224 U_BOOT_DRIVER(ide) = {
1225 	.name		= "ide",
1226 	.id		= UCLASS_IDE,
1227 	.probe		= ide_probe,
1228 };
1229 
1230 struct pci_device_id ide_supported[] = {
1231 	{ PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xffff00) },
1232 	{ }
1233 };
1234 
1235 U_BOOT_PCI_DEVICE(ide, ide_supported);
1236 
1237 UCLASS_DRIVER(ide) = {
1238 	.name		= "ide",
1239 	.id		= UCLASS_IDE,
1240 };
1241 #else
1242 U_BOOT_LEGACY_BLK(ide) = {
1243 	.if_typename	= "ide",
1244 	.if_type	= IF_TYPE_IDE,
1245 	.max_devs	= CONFIG_SYS_IDE_MAXDEVICE,
1246 	.desc		= ide_dev_desc,
1247 };
1248 #endif
1249