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