1 /*
2  *   OpenBIOS polled ide driver
3  *
4  *   Copyright (C) 2004 Jens Axboe <axboe@suse.de>
5  *   Copyright (C) 2005 Stefan Reinauer
6  *
7  *   Credit goes to Hale Landis for his excellent ata demo software
8  *   OF node handling and some fixes by Stefan Reinauer
9  *
10  *   This program is free software; you can redistribute it and/or
11  *   modify it under the terms of the GNU General Public License
12  *   version 2
13  *
14  */
15 
16 #include "config.h"
17 #include "libopenbios/bindings.h"
18 #include "kernel/kernel.h"
19 #include "libc/byteorder.h"
20 #include "libc/vsprintf.h"
21 
22 #include "drivers/drivers.h"
23 #include "ide.h"
24 #include "hdreg.h"
25 #include "timer.h"
26 
27 #ifdef CONFIG_DEBUG_IDE
28 #define IDE_DPRINTF(fmt, args...) \
29 do { printk("IDE - %s: " fmt, __func__ , ##args); } while (0)
30 #else
31 #define IDE_DPRINTF(fmt, args...) do { } while (0)
32 #endif
33 
34 /* DECLARE data structures for the nodes.  */
35 DECLARE_UNNAMED_NODE( ob_ide, 0, sizeof(struct ide_drive*) );
36 DECLARE_UNNAMED_NODE( ob_ide_ctrl, 0, sizeof(int));
37 
38 /*
39  * define to 2 for the standard 2 channels only
40  */
41 #ifndef CONFIG_IDE_NUM_CHANNELS
42 #define IDE_NUM_CHANNELS 4
43 #else
44 #define IDE_NUM_CHANNELS CONFIG_IDE_NUM_CHANNELS
45 #endif
46 #define IDE_MAX_CHANNELS 4
47 
48 #ifndef CONFIG_IDE_FIRST_UNIT
49 #define FIRST_UNIT 0
50 #else
51 #define FIRST_UNIT CONFIG_IDE_FIRST_UNIT
52 #endif
53 
54 #ifndef CONFIG_IDE_DEV_TYPE
55 #define DEV_TYPE "ide"
56 #else
57 #define DEV_TYPE CONFIG_IDE_DEV_TYPE
58 #endif
59 
60 #ifndef CONFIG_IDE_DEV_NAME
61 #define DEV_NAME "ide"
62 #else
63 #define DEV_NAME CONFIG_IDE_DEV_NAME
64 #endif
65 
66 static int current_channel = FIRST_UNIT;
67 
68 /*
69  * don't be pedantic
70  */
71 #undef ATA_PEDANTIC
72 
dump_drive(struct ide_drive * drive)73 static void dump_drive(struct ide_drive *drive)
74 {
75 #ifdef CONFIG_DEBUG_IDE
76 	printk("IDE DRIVE @%lx:\n", (unsigned long)drive);
77 	printk("unit: %d\n",drive->unit);
78 	printk("present: %d\n",drive->present);
79 	printk("type: %d\n",drive->type);
80 	printk("media: %d\n",drive->media);
81 	printk("model: %s\n",drive->model);
82 	printk("nr: %d\n",drive->nr);
83 	printk("cyl: %d\n",drive->cyl);
84 	printk("head: %d\n",drive->head);
85 	printk("sect: %d\n",drive->sect);
86 	printk("bs: %d\n",drive->bs);
87 #endif
88 }
89 
90 /*
91  * old style io port operations
92  */
93 static unsigned char
ob_ide_inb(struct ide_channel * chan,unsigned int port)94 ob_ide_inb(struct ide_channel *chan, unsigned int port)
95 {
96 	return inb(chan->io_regs[port]);
97 }
98 
99 static void
ob_ide_outb(struct ide_channel * chan,unsigned char data,unsigned int port)100 ob_ide_outb(struct ide_channel *chan, unsigned char data, unsigned int port)
101 {
102 	outb(data, chan->io_regs[port]);
103 }
104 
105 static void
ob_ide_insw(struct ide_channel * chan,unsigned int port,unsigned char * addr,unsigned int count)106 ob_ide_insw(struct ide_channel *chan,
107 	    unsigned int port, unsigned char *addr, unsigned int count)
108 {
109 	insw(chan->io_regs[port], addr, count);
110 }
111 
112 static void
ob_ide_outsw(struct ide_channel * chan,unsigned int port,unsigned char * addr,unsigned int count)113 ob_ide_outsw(struct ide_channel *chan,
114 	     unsigned int port, unsigned char *addr, unsigned int count)
115 {
116 	outsw(chan->io_regs[port], addr, count);
117 }
118 
119 static inline unsigned char
ob_ide_pio_readb(struct ide_drive * drive,unsigned int offset)120 ob_ide_pio_readb(struct ide_drive *drive, unsigned int offset)
121 {
122 	struct ide_channel *chan = drive->channel;
123 
124 	return chan->obide_inb(chan, offset);
125 }
126 
127 static inline void
ob_ide_pio_writeb(struct ide_drive * drive,unsigned int offset,unsigned char data)128 ob_ide_pio_writeb(struct ide_drive *drive, unsigned int offset,
129 		  unsigned char data)
130 {
131 	struct ide_channel *chan = drive->channel;
132 
133 	chan->obide_outb(chan, data, offset);
134 }
135 
136 static inline void
ob_ide_pio_insw(struct ide_drive * drive,unsigned int offset,unsigned char * addr,unsigned int len)137 ob_ide_pio_insw(struct ide_drive *drive, unsigned int offset,
138 		unsigned char *addr, unsigned int len)
139 {
140 	struct ide_channel *chan = drive->channel;
141 
142 	if (len & 1) {
143 		IDE_DPRINTF("%d: command not word aligned\n", drive->nr);
144 		return;
145 	}
146 
147 	chan->obide_insw(chan, offset, addr, len / 2);
148 }
149 
150 static inline void
ob_ide_pio_outsw(struct ide_drive * drive,unsigned int offset,unsigned char * addr,unsigned int len)151 ob_ide_pio_outsw(struct ide_drive *drive, unsigned int offset,
152 		unsigned char *addr, unsigned int len)
153 {
154 	struct ide_channel *chan = drive->channel;
155 
156 	if (len & 1) {
157 		IDE_DPRINTF("%d: command not word aligned\n", drive->nr);
158 		return;
159 	}
160 
161 	chan->obide_outsw(chan, offset, addr, len / 2);
162 }
163 
164 static void
ob_ide_400ns_delay(struct ide_drive * drive)165 ob_ide_400ns_delay(struct ide_drive *drive)
166 {
167 	(void) ob_ide_pio_readb(drive, IDEREG_ASTATUS);
168 	(void) ob_ide_pio_readb(drive, IDEREG_ASTATUS);
169 	(void) ob_ide_pio_readb(drive, IDEREG_ASTATUS);
170 	(void) ob_ide_pio_readb(drive, IDEREG_ASTATUS);
171 
172 	udelay(1);
173 }
174 
175 static void
ob_ide_error(struct ide_drive * drive,unsigned char stat,const char * msg)176 ob_ide_error(struct ide_drive *drive, unsigned char stat, const char *msg)
177 {
178 #ifdef CONFIG_DEBUG_IDE
179 	struct ide_channel *chan = drive->channel;
180 	unsigned char err;
181 #endif
182 
183 	if (!stat)
184 		stat = ob_ide_pio_readb(drive, IDEREG_STATUS);
185 
186 	IDE_DPRINTF("ob_ide_error drive<%d>: %s:\n", drive->nr, msg);
187 	IDE_DPRINTF("    cmd=%x, stat=%x", chan->ata_cmd.command, stat);
188 
189 	if ((stat & (BUSY_STAT | ERR_STAT)) == ERR_STAT) {
190 #ifdef CONFIG_DEBUG_IDE
191                 err =
192 #endif
193                     ob_ide_pio_readb(drive, IDEREG_ERROR);
194 		IDE_DPRINTF(", err=%x", err);
195 	}
196 	IDE_DPRINTF("\n");
197 
198 #ifdef CONFIG_DEBUG_IDE
199 	/*
200 	 * see if sense is valid and dump that
201 	 */
202 	if (chan->ata_cmd.command == WIN_PACKET) {
203 		struct atapi_command *cmd = &chan->atapi_cmd;
204 		unsigned char old_cdb = cmd->cdb[0];
205 
206 		if (cmd->cdb[0] == ATAPI_REQ_SENSE) {
207 			old_cdb = cmd->old_cdb;
208 
209 			IDE_DPRINTF("    atapi opcode=%02x", old_cdb);
210 		} else {
211 			int i;
212 
213 			IDE_DPRINTF("    cdb: ");
214 			for (i = 0; i < sizeof(cmd->cdb); i++)
215 				IDE_DPRINTF("%02x ", cmd->cdb[i]);
216 		}
217 		if (cmd->sense_valid)
218 			IDE_DPRINTF(", sense: %02x/%02x/%02x",
219                                     cmd->sense.sense_key, cmd->sense.asc,
220                                     cmd->sense.ascq);
221 		else
222 			IDE_DPRINTF(", no sense");
223 		IDE_DPRINTF("\n");
224 	}
225 #endif
226 }
227 
228 /*
229  * wait for 'stat' to be set. returns 1 if failed, 0 if succesful
230  */
231 static int
ob_ide_wait_stat(struct ide_drive * drive,unsigned char ok_stat,unsigned char bad_stat,unsigned char * ret_stat)232 ob_ide_wait_stat(struct ide_drive *drive, unsigned char ok_stat,
233                  unsigned char bad_stat, unsigned char *ret_stat)
234 {
235 	unsigned char stat;
236 	int i;
237 
238 	ob_ide_400ns_delay(drive);
239 
240 	for (i = 0; i < 5000; i++) {
241 		stat = ob_ide_pio_readb(drive, IDEREG_STATUS);
242 		if (!(stat & BUSY_STAT))
243 			break;
244 
245 		udelay(1000);
246 	}
247 
248 	if (ret_stat)
249 		*ret_stat = stat;
250 
251 	if (stat & bad_stat)
252 		return 1;
253 
254 	if ((stat & ok_stat) || !ok_stat)
255 		return 0;
256 
257 	return 1;
258 }
259 
260 static int
ob_ide_select_drive(struct ide_drive * drive)261 ob_ide_select_drive(struct ide_drive *drive)
262 {
263 	struct ide_channel *chan = drive->channel;
264 	unsigned char control = IDEHEAD_DEV0;
265 
266 	if (ob_ide_wait_stat(drive, 0, BUSY_STAT, NULL)) {
267 		IDE_DPRINTF("select_drive: timed out\n");
268 		return 1;
269 	}
270 
271 	/*
272 	 * don't select drive if already active. Note: we always
273 	 * wait for BUSY clear
274 	 */
275 	if (drive->unit == chan->selected)
276 		return 0;
277 
278 	if (drive->unit)
279 		control = IDEHEAD_DEV1;
280 
281 	ob_ide_pio_writeb(drive, IDEREG_CURRENT, control);
282 	ob_ide_400ns_delay(drive);
283 
284 	if (ob_ide_wait_stat(drive, 0, BUSY_STAT, NULL)) {
285 		IDE_DPRINTF("select_drive: timed out\n");
286 		return 1;
287 	}
288 
289 	chan->selected = drive->unit;
290 	return 0;
291 }
292 
293 static void
ob_ide_write_tasklet(struct ide_drive * drive,struct ata_command * cmd)294 ob_ide_write_tasklet(struct ide_drive *drive, struct ata_command *cmd)
295 {
296 	ob_ide_pio_writeb(drive, IDEREG_FEATURE, cmd->task[1]);
297 	ob_ide_pio_writeb(drive, IDEREG_NSECTOR, cmd->task[3]);
298 	ob_ide_pio_writeb(drive, IDEREG_SECTOR, cmd->task[7]);
299 	ob_ide_pio_writeb(drive, IDEREG_LCYL, cmd->task[8]);
300 	ob_ide_pio_writeb(drive, IDEREG_HCYL, cmd->task[9]);
301 
302 	ob_ide_pio_writeb(drive, IDEREG_FEATURE, cmd->task[0]);
303 	ob_ide_pio_writeb(drive, IDEREG_NSECTOR, cmd->task[2]);
304 	ob_ide_pio_writeb(drive, IDEREG_SECTOR, cmd->task[4]);
305 	ob_ide_pio_writeb(drive, IDEREG_LCYL, cmd->task[5]);
306 	ob_ide_pio_writeb(drive, IDEREG_HCYL, cmd->task[6]);
307 
308 	if (drive->unit)
309 		cmd->device_head |= IDEHEAD_DEV1;
310 
311 	ob_ide_pio_writeb(drive, IDEREG_CURRENT, cmd->device_head);
312 
313 	ob_ide_pio_writeb(drive, IDEREG_COMMAND, cmd->command);
314 	ob_ide_400ns_delay(drive);
315 }
316 
317 static void
ob_ide_write_registers(struct ide_drive * drive,struct ata_command * cmd)318 ob_ide_write_registers(struct ide_drive *drive, struct ata_command *cmd)
319 {
320 	/*
321 	 * we are _always_ polled
322 	 */
323 	ob_ide_pio_writeb(drive, IDEREG_CONTROL, cmd->control | IDECON_NIEN);
324 
325 	ob_ide_pio_writeb(drive, IDEREG_FEATURE, cmd->feature);
326 	ob_ide_pio_writeb(drive, IDEREG_NSECTOR, cmd->nsector);
327 	ob_ide_pio_writeb(drive, IDEREG_SECTOR, cmd->sector);
328 	ob_ide_pio_writeb(drive, IDEREG_LCYL, cmd->lcyl);
329 	ob_ide_pio_writeb(drive, IDEREG_HCYL, cmd->hcyl);
330 
331 	if (drive->unit)
332 		cmd->device_head |= IDEHEAD_DEV1;
333 
334 	ob_ide_pio_writeb(drive, IDEREG_CURRENT, cmd->device_head);
335 
336 	ob_ide_pio_writeb(drive, IDEREG_COMMAND, cmd->command);
337 	ob_ide_400ns_delay(drive);
338 }
339 
340 /*
341  * execute command with "pio non data" protocol
342  */
343 #if 0
344 static int
345 ob_ide_pio_non_data(struct ide_drive *drive, struct ata_command *cmd)
346 {
347 	if (ob_ide_select_drive(drive))
348 		return 1;
349 
350 	ob_ide_write_registers(drive, cmd);
351 
352 	if (ob_ide_wait_stat(drive, 0, BUSY_STAT, NULL))
353 		return 1;
354 
355 	return 0;
356 }
357 #endif
358 
359 /*
360  * execute given command with a pio data-in phase.
361  */
362 static int
ob_ide_pio_data_in(struct ide_drive * drive,struct ata_command * cmd)363 ob_ide_pio_data_in(struct ide_drive *drive, struct ata_command *cmd)
364 {
365 	unsigned char stat;
366 	unsigned int bytes, timeout;
367 
368 	if (ob_ide_select_drive(drive))
369 		return 1;
370 
371 	/*
372 	 * ATA must set ready and seek stat, ATAPI need only clear busy
373 	 */
374 	timeout = 0;
375 	do {
376 		stat = ob_ide_pio_readb(drive, IDEREG_STATUS);
377 
378 		if (drive->type == ide_type_ata) {
379 			/*
380 			 * this is BIOS code, don't be too pedantic
381 			 */
382 #ifdef ATA_PEDANTIC
383 			if ((stat & (BUSY_STAT | READY_STAT | SEEK_STAT)) ==
384 			    (READY_STAT | SEEK_STAT))
385 				break;
386 #else
387 			if ((stat & (BUSY_STAT | READY_STAT)) == READY_STAT)
388 				break;
389 #endif
390 		} else {
391 			if (!(stat & BUSY_STAT))
392 				break;
393 		}
394 		ob_ide_400ns_delay(drive);
395 	} while (timeout++ < 1000);
396 
397 	if (timeout >= 1000) {
398 		ob_ide_error(drive, stat, "drive timed out");
399 		cmd->stat = stat;
400 		return 1;
401 	}
402 
403 	ob_ide_write_registers(drive, cmd);
404 
405 	/*
406 	 * now read the data
407 	 */
408 	bytes = cmd->buflen;
409 	do {
410 		unsigned count = cmd->buflen;
411 
412 		if (count > drive->bs)
413 			count = drive->bs;
414 
415 		/* delay 100ms for ATAPI? */
416 
417 		/*
418 		 * wait for BUSY clear
419 		 */
420 		if (ob_ide_wait_stat(drive, 0, BUSY_STAT | ERR_STAT, &stat)) {
421 			ob_ide_error(drive, stat, "timed out waiting for BUSY clear");
422 			cmd->stat = stat;
423 			break;
424 		}
425 
426 		/*
427 		 * transfer the data
428 		 */
429 		if ((stat & (BUSY_STAT | DRQ_STAT)) == DRQ_STAT) {
430 			ob_ide_pio_insw(drive, IDEREG_DATA, cmd->buffer, count);
431 			cmd->bytes -= count;
432 			cmd->buffer += count;
433 			bytes -= count;
434 
435 			ob_ide_400ns_delay(drive);
436 		}
437 
438 		if (stat & (BUSY_STAT | WRERR_STAT | ERR_STAT)) {
439 			cmd->stat = stat;
440 			break;
441 		}
442 
443 		if (!(stat & DRQ_STAT)) {
444 			cmd->stat = stat;
445 			break;
446 		}
447 	} while (bytes);
448 
449 	if (bytes)
450 		IDE_DPRINTF("bytes=%d, stat=%x\n", bytes, stat);
451 
452 	return bytes ? 1 : 0;
453 }
454 
455 /*
456  * execute ata command with pio packet protocol
457  */
458 static int
ob_ide_pio_packet(struct ide_drive * drive,struct atapi_command * cmd)459 ob_ide_pio_packet(struct ide_drive *drive, struct atapi_command *cmd)
460 {
461 	unsigned char stat, reason, lcyl, hcyl;
462 	struct ata_command *acmd = &drive->channel->ata_cmd;
463 	unsigned char *buffer;
464 	unsigned int bytes;
465 
466 	if (ob_ide_select_drive(drive))
467 		return 1;
468 
469 	if (cmd->buflen && cmd->data_direction == atapi_ddir_none)
470 		IDE_DPRINTF("non-zero buflen but no data direction\n");
471 
472 	memset(acmd, 0, sizeof(*acmd));
473 	acmd->lcyl = cmd->buflen & 0xff;
474 	acmd->hcyl = (cmd->buflen >> 8) & 0xff;
475 	acmd->command = WIN_PACKET;
476 	ob_ide_write_registers(drive, acmd);
477 
478 	/*
479 	 * BUSY must be set, _or_ DRQ | ERR
480 	 */
481 	stat = ob_ide_pio_readb(drive, IDEREG_ASTATUS);
482 	if ((stat & BUSY_STAT) == 0) {
483 		if (!(stat & (DRQ_STAT | ERR_STAT))) {
484 			ob_ide_error(drive, stat, "bad stat in atapi cmd");
485 			cmd->stat = stat;
486 			return 1;
487 		}
488 	}
489 
490 	if (ob_ide_wait_stat(drive, 0, BUSY_STAT | ERR_STAT, &stat)) {
491 		ob_ide_error(drive, stat, "timeout, ATAPI BUSY clear");
492 		cmd->stat = stat;
493 		return 1;
494 	}
495 
496 	if ((stat & (BUSY_STAT | DRQ_STAT | ERR_STAT)) != DRQ_STAT) {
497 		/*
498 		 * if command isn't request sense, then we have a problem. if
499 		 * we are doing a sense, ERR_STAT == CHECK_CONDITION
500 		 */
501 		if (cmd->cdb[0] != ATAPI_REQ_SENSE) {
502 			IDE_DPRINTF("odd, drive didn't want to transfer %x\n",
503                                      stat);
504 			return 1;
505 		}
506 	}
507 
508 	/*
509 	 * transfer cdb
510 	 */
511 	ob_ide_pio_outsw(drive, IDEREG_DATA, cmd->cdb,sizeof(cmd->cdb));
512 	ob_ide_400ns_delay(drive);
513 
514 	/*
515 	 * ok, cdb was sent to drive, now do data transfer (if any)
516 	 */
517 	bytes = cmd->buflen;
518 	buffer = cmd->buffer;
519 	do {
520 		unsigned int bc;
521 
522 		if (ob_ide_wait_stat(drive, 0, BUSY_STAT | ERR_STAT, &stat)) {
523 			ob_ide_error(drive, stat, "busy not clear after cdb");
524 			cmd->stat = stat;
525 			break;
526 		}
527 
528 		/*
529 		 * transfer complete!
530 		 */
531 		if ((stat & (BUSY_STAT | DRQ_STAT)) == 0)
532 			break;
533 
534 		if ((stat & (BUSY_STAT | DRQ_STAT)) != DRQ_STAT)
535 			break;
536 
537 		reason = ob_ide_pio_readb(drive, IDEREG_NSECTOR);
538 		lcyl = ob_ide_pio_readb(drive, IDEREG_LCYL);
539 		hcyl = ob_ide_pio_readb(drive, IDEREG_HCYL);
540 
541 		/*
542 		 * check if the drive wants to transfer data in the same
543 		 * direction as we do...
544 		 */
545 		if ((reason & IREASON_CD) && cmd->data_direction != atapi_ddir_read) {
546 			ob_ide_error(drive, stat, "atapi, bad transfer ddir");
547 			break;
548 		}
549 
550 		bc = (hcyl << 8) | lcyl;
551 		if (!bc)
552 			break;
553 
554 		if (bc > bytes)
555 			bc = bytes;
556 
557 		if (cmd->data_direction == atapi_ddir_read)
558 			ob_ide_pio_insw(drive, IDEREG_DATA, buffer, bc);
559 		else
560 			ob_ide_pio_outsw(drive, IDEREG_DATA, buffer, bc);
561 
562 		bytes -= bc;
563 		buffer += bc;
564 
565 		ob_ide_400ns_delay(drive);
566 	} while (bytes);
567 
568 	if (cmd->data_direction != atapi_ddir_none)
569 		(void) ob_ide_wait_stat(drive, 0, BUSY_STAT, &stat);
570 
571 	if (bytes)
572 		IDE_DPRINTF("cdb failed, bytes=%d, stat=%x\n", bytes, stat);
573 
574 	return (stat & ERR_STAT) || bytes;
575 }
576 
577 /*
578  * execute a packet command, with retries if appropriate
579  */
580 static int
ob_ide_atapi_packet(struct ide_drive * drive,struct atapi_command * cmd)581 ob_ide_atapi_packet(struct ide_drive *drive, struct atapi_command *cmd)
582 {
583 	int retries = 5, ret;
584 
585 	if (drive->type != ide_type_atapi)
586 		return 1;
587 	if (cmd->buflen > 0xffff)
588 		return 1;
589 
590 	/*
591 	 * retry loop
592 	 */
593 	do {
594 		ret = ob_ide_pio_packet(drive, cmd);
595 		if (!ret)
596 			break;
597 
598 		/*
599 		 * request sense failed, bummer
600 		 */
601 		if (cmd->cdb[0] == ATAPI_REQ_SENSE)
602 			break;
603 
604 		if (ob_ide_atapi_request_sense(drive))
605 			break;
606 
607 		/*
608 		 * we know sense is valid. retry if the drive isn't ready,
609 		 * otherwise don't bother.
610 		 */
611 		if (cmd->sense.sense_key != ATAPI_SENSE_NOT_READY)
612 			break;
613 		/*
614 		 * ... except 'medium not present'
615 		 */
616 		if (cmd->sense.asc == 0x3a)
617 			break;
618 
619 		udelay(1000000);
620 	} while (retries--);
621 
622 	if (ret)
623 		ob_ide_error(drive, 0, "atapi command");
624 
625 	return ret;
626 }
627 
628 static int
ob_ide_atapi_request_sense(struct ide_drive * drive)629 ob_ide_atapi_request_sense(struct ide_drive *drive)
630 {
631 	struct atapi_command *cmd = &drive->channel->atapi_cmd;
632 	unsigned char old_cdb;
633 
634 	/*
635 	 * save old cdb for debug error
636 	 */
637 	old_cdb = cmd->cdb[0];
638 
639 	memset(cmd, 0, sizeof(*cmd));
640 	cmd->cdb[0] = ATAPI_REQ_SENSE;
641 	cmd->cdb[4] = 18;
642 	cmd->buffer = (unsigned char *) &cmd->sense;
643 	cmd->buflen = 18;
644 	cmd->data_direction = atapi_ddir_read;
645 	cmd->old_cdb = old_cdb;
646 
647 	if (ob_ide_atapi_packet(drive, cmd))
648 		return 1;
649 
650 	cmd->sense_valid = 1;
651 	return 0;
652 }
653 
654 /*
655  * make sure drive is ready and media loaded
656  */
657 static int
ob_ide_atapi_drive_ready(struct ide_drive * drive)658 ob_ide_atapi_drive_ready(struct ide_drive *drive)
659 {
660 	struct atapi_command *cmd = &drive->channel->atapi_cmd;
661 	struct atapi_capacity cap;
662 
663 	IDE_DPRINTF("ob_ide_atapi_drive_ready\n");
664 
665 	/*
666 	 * Test Unit Ready is like a ping
667 	 */
668 	memset(cmd, 0, sizeof(*cmd));
669 	cmd->cdb[0] = ATAPI_TUR;
670 
671 	if (ob_ide_atapi_packet(drive, cmd)) {
672 		IDE_DPRINTF("%d: TUR failed\n", drive->nr);
673 		return 1;
674 	}
675 
676 	/*
677 	 * don't force load of tray (bit 2 in byte 4 of cdb), it's
678 	 * annoying and we don't want to deal with errors from drives
679 	 * that cannot do it
680 	 */
681 	memset(cmd, 0, sizeof(*cmd));
682 	cmd->cdb[0] = ATAPI_START_STOP_UNIT;
683 	cmd->cdb[4] = 0x01;
684 
685 	if (ob_ide_atapi_packet(drive, cmd)) {
686 		IDE_DPRINTF("%d: START_STOP unit failed\n", drive->nr);
687 		return 1;
688 	}
689 
690 	/*
691 	 * finally, get capacity and block size
692 	 */
693 	memset(cmd, 0, sizeof(*cmd));
694 	memset(&cap, 0, sizeof(cap));
695 
696 	cmd->cdb[0] = ATAPI_READ_CAPACITY;
697 	cmd->buffer = (unsigned char *) &cap;
698 	cmd->buflen = sizeof(cap);
699 	cmd->data_direction = atapi_ddir_read;
700 
701 	if (ob_ide_atapi_packet(drive, cmd)) {
702 		drive->sectors = 0x1fffff;
703 		drive->bs = 2048;
704 		return 1;
705 	}
706 
707 	drive->sectors = __be32_to_cpu(cap.lba) + 1;
708 	drive->bs = __be32_to_cpu(cap.block_size);
709 	return 0;
710 }
711 
712 /*
713  * read from an atapi device, using READ_10
714  */
715 static int
ob_ide_read_atapi(struct ide_drive * drive,unsigned long long block,unsigned char * buf,unsigned int sectors)716 ob_ide_read_atapi(struct ide_drive *drive, unsigned long long block,
717                   unsigned char *buf, unsigned int sectors)
718 {
719 	struct atapi_command *cmd = &drive->channel->atapi_cmd;
720 
721 	if (ob_ide_atapi_drive_ready(drive))
722 		return 1;
723 
724 	memset(cmd, 0, sizeof(*cmd));
725 
726 	/*
727 	 * READ_10 should work on generally any atapi device
728 	 */
729 	cmd->cdb[0] = ATAPI_READ_10;
730 	cmd->cdb[2] = (block >> 24) & 0xff;
731 	cmd->cdb[3] = (block >> 16) & 0xff;
732 	cmd->cdb[4] = (block >>  8) & 0xff;
733 	cmd->cdb[5] = block & 0xff;
734 	cmd->cdb[7] = (sectors >> 8) & 0xff;
735 	cmd->cdb[8] = sectors & 0xff;
736 
737 	cmd->buffer = buf;
738 	cmd->buflen = sectors * 2048;
739 	cmd->data_direction = atapi_ddir_read;
740 
741 	return ob_ide_atapi_packet(drive, cmd);
742 }
743 
744 static int
ob_ide_read_ata_chs(struct ide_drive * drive,unsigned long long block,unsigned char * buf,unsigned int sectors)745 ob_ide_read_ata_chs(struct ide_drive *drive, unsigned long long block,
746                     unsigned char *buf, unsigned int sectors)
747 {
748 	struct ata_command *cmd = &drive->channel->ata_cmd;
749 	unsigned int track = (block / drive->sect);
750 	unsigned int sect = (block % drive->sect) + 1;
751 	unsigned int head = (track % drive->head);
752 	unsigned int cyl = (track / drive->head);
753 
754 	/*
755 	 * fill in chs command to read from disk at given location
756 	 */
757 	cmd->buffer = buf;
758 	cmd->buflen = sectors * 512;
759 
760 	cmd->nsector = sectors & 0xff;
761 	cmd->sector = sect;
762 	cmd->lcyl = cyl;
763 	cmd->hcyl = cyl >> 8;
764 	cmd->device_head = head;
765 
766 	cmd->command = WIN_READ;
767 
768 	return ob_ide_pio_data_in(drive, cmd);
769 }
770 
771 static int
ob_ide_read_ata_lba28(struct ide_drive * drive,unsigned long long block,unsigned char * buf,unsigned int sectors)772 ob_ide_read_ata_lba28(struct ide_drive *drive, unsigned long long block,
773                       unsigned char *buf, unsigned int sectors)
774 {
775 	struct ata_command *cmd = &drive->channel->ata_cmd;
776 
777 	memset(cmd, 0, sizeof(*cmd));
778 
779 	/*
780 	 * fill in 28-bit lba command to read from disk at given location
781 	 */
782 	cmd->buffer = buf;
783 	cmd->buflen = sectors * 512;
784 
785 	cmd->nsector = sectors;
786 	cmd->sector = block;
787 	cmd->lcyl = block >>= 8;
788 	cmd->hcyl = block >>= 8;
789 	cmd->device_head = ((block >> 8) & 0x0f);
790 	cmd->device_head |= (1 << 6);
791 
792 	cmd->command = WIN_READ;
793 
794 	return ob_ide_pio_data_in(drive, cmd);
795 }
796 
797 static int
ob_ide_read_ata_lba48(struct ide_drive * drive,unsigned long long block,unsigned char * buf,unsigned int sectors)798 ob_ide_read_ata_lba48(struct ide_drive *drive, unsigned long long block,
799                       unsigned char *buf, unsigned int sectors)
800 {
801 	struct ata_command *cmd = &drive->channel->ata_cmd;
802 
803 	memset(cmd, 0, sizeof(*cmd));
804 
805 	cmd->buffer = buf;
806 	cmd->buflen = sectors * 512;
807 
808 	/*
809 	 * we are using tasklet addressing here
810 	 */
811 	cmd->task[2] = sectors;
812 	cmd->task[3] = sectors >> 8;
813 	cmd->task[4] = block;
814 	cmd->task[5] = block >>  8;
815 	cmd->task[6] = block >> 16;
816 	cmd->task[7] = block >> 24;
817 	cmd->task[8] = (u64) block >> 32;
818 	cmd->task[9] = (u64) block >> 40;
819 
820 	cmd->command = WIN_READ_EXT;
821 
822 	ob_ide_write_tasklet(drive, cmd);
823 
824 	return ob_ide_pio_data_in(drive, cmd);
825 }
826 /*
827  * read 'sectors' sectors from ata device
828  */
829 static int
ob_ide_read_ata(struct ide_drive * drive,unsigned long long block,unsigned char * buf,unsigned int sectors)830 ob_ide_read_ata(struct ide_drive *drive, unsigned long long block,
831                 unsigned char *buf, unsigned int sectors)
832 {
833 	unsigned long long end_block = block + sectors;
834 	const int need_lba48 = (end_block > (1ULL << 28)) || (sectors > 255);
835 
836 	if (end_block > drive->sectors)
837 		return 1;
838 	if (need_lba48 && drive->addressing != ide_lba48)
839 		return 1;
840 
841 	/*
842 	 * use lba48 if we have to, otherwise use the faster lba28
843 	 */
844 	if (need_lba48)
845 		return ob_ide_read_ata_lba48(drive, block, buf, sectors);
846 	else if (drive->addressing != ide_chs)
847 		return ob_ide_read_ata_lba28(drive, block, buf, sectors);
848 
849 	return ob_ide_read_ata_chs(drive, block, buf, sectors);
850 }
851 
852 static int
ob_ide_read_sectors(struct ide_drive * drive,unsigned long long block,unsigned char * buf,unsigned int sectors)853 ob_ide_read_sectors(struct ide_drive *drive, unsigned long long block,
854                     unsigned char *buf, unsigned int sectors)
855 {
856 	if (!sectors)
857 		return 1;
858 	if (block + sectors > drive->sectors)
859 		return 1;
860 
861 	IDE_DPRINTF("ob_ide_read_sectors: block=%lu sectors=%u\n",
862 	            (unsigned long) block, sectors);
863 
864 	if (drive->type == ide_type_ata)
865 		return ob_ide_read_ata(drive, block, buf, sectors);
866 	else
867 		return ob_ide_read_atapi(drive, block, buf, sectors);
868 }
869 
870 /*
871  * byte swap the string if necessay, and strip leading/trailing blanks
872  */
873 static void
ob_ide_fixup_string(unsigned char * s,unsigned int len)874 ob_ide_fixup_string(unsigned char *s, unsigned int len)
875 {
876 	unsigned char *p = s, *end = &s[len & ~1];
877 
878 	/*
879 	 * if big endian arch, byte swap the string
880 	 */
881 #ifdef CONFIG_BIG_ENDIAN
882 	for (p = end ; p != s;) {
883 		unsigned short *pp = (unsigned short *) (p -= 2);
884 		*pp = __le16_to_cpu(*pp);
885 	}
886 #endif
887 
888 	while (s != end && *s == ' ')
889 		++s;
890 	while (s != end && *s)
891 		if (*s++ != ' ' || (s != end && *s && *s != ' '))
892 			*p++ = *(s-1);
893 	while (p != end)
894 		*p++ = '\0';
895 }
896 
897 /*
898  * it's big endian, we need to swap (if on little endian) the items we use
899  */
900 static int
ob_ide_fixup_id(struct hd_driveid * id)901 ob_ide_fixup_id(struct hd_driveid *id)
902 {
903 	ob_ide_fixup_string(id->model, 40);
904 	id->config = __le16_to_cpu(id->config);
905 	id->lba_capacity = __le32_to_cpu(id->lba_capacity);
906 	id->cyls = __le16_to_cpu(id->cyls);
907 	id->heads = __le16_to_cpu(id->heads);
908 	id->sectors = __le16_to_cpu(id->sectors);
909 	id->command_set_2 = __le16_to_cpu(id->command_set_2);
910 	id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
911 
912 	return 0;
913 }
914 
915 static int
ob_ide_identify_drive(struct ide_drive * drive)916 ob_ide_identify_drive(struct ide_drive *drive)
917 {
918 	struct ata_command *cmd = &drive->channel->ata_cmd;
919 	struct hd_driveid id;
920 
921 	memset(cmd, 0, sizeof(*cmd));
922 	cmd->buffer = (unsigned char *) &id;
923 	cmd->buflen = 512;
924 
925 	if (drive->type == ide_type_ata)
926 		cmd->command = WIN_IDENTIFY;
927 	else if (drive->type == ide_type_atapi)
928 		cmd->command = WIN_IDENTIFY_PACKET;
929 	else {
930 		IDE_DPRINTF("%s: called with bad device type %d\n",
931                             __FUNCTION__, drive->type);
932 		return 1;
933 	}
934 
935 	if (ob_ide_pio_data_in(drive, cmd))
936 		return 1;
937 
938 	ob_ide_fixup_id(&id);
939 
940 	if (drive->type == ide_type_atapi) {
941 		drive->media = (id.config >> 8) & 0x1f;
942 		drive->sectors = 0x7fffffff;
943 		drive->bs = 2048;
944 		drive->max_sectors = 31;
945 	} else {
946 		drive->media = ide_media_disk;
947 		drive->sectors = id.lba_capacity;
948 		drive->bs = 512;
949 		drive->max_sectors = 255;
950 
951 #ifdef CONFIG_IDE_LBA48
952 		if ((id.command_set_2 & 0x0400) && (id.cfs_enable_2 & 0x0400)) {
953 			drive->addressing = ide_lba48;
954 			drive->max_sectors = 65535;
955 		} else
956 #endif
957 		if (id.capability & 2)
958 			drive->addressing = ide_lba28;
959 		else {
960 			drive->addressing = ide_chs;
961 		}
962 
963 		/* only set these in chs mode? */
964 		drive->cyl = id.cyls;
965 		drive->head = id.heads;
966 		drive->sect = id.sectors;
967 	}
968 
969 	strncpy(drive->model, (char*)id.model, sizeof(drive->model));
970 	drive->model[40] = '\0';
971 	return 0;
972 }
973 
974 /*
975  * identify type of devices on channel. must have already been probed.
976  */
977 static void
ob_ide_identify_drives(struct ide_channel * chan)978 ob_ide_identify_drives(struct ide_channel *chan)
979 {
980 	struct ide_drive *drive;
981 	int i;
982 
983 	for (i = 0; i < 2; i++) {
984 		drive = &chan->drives[i];
985 
986 		if (!drive->present)
987 			continue;
988 
989 		ob_ide_identify_drive(drive);
990 	}
991 }
992 
993 /*
994  * software reset (ATA-4, section 8.3)
995  */
996 static void
ob_ide_software_reset(struct ide_drive * drive)997 ob_ide_software_reset(struct ide_drive *drive)
998 {
999 	struct ide_channel *chan = drive->channel;
1000 
1001 	ob_ide_pio_writeb(drive, IDEREG_CONTROL, IDECON_NIEN | IDECON_SRST);
1002 	ob_ide_400ns_delay(drive);
1003 	ob_ide_pio_writeb(drive, IDEREG_CONTROL, IDECON_NIEN);
1004 	ob_ide_400ns_delay(drive);
1005 
1006 	/*
1007 	 * if master is present, wait for BUSY clear
1008 	 */
1009 	if (chan->drives[0].present)
1010 		ob_ide_wait_stat(drive, 0, BUSY_STAT, NULL);
1011 
1012 	/*
1013 	 * if slave is present, wait until it allows register access
1014 	 */
1015 	if (chan->drives[1].present) {
1016 		unsigned char sectorn, sectorc;
1017 		int timeout = 1000;
1018 
1019 		do {
1020 			/*
1021 			 * select it
1022 			 */
1023 			ob_ide_pio_writeb(drive, IDEREG_CURRENT, IDEHEAD_DEV1);
1024 			ob_ide_400ns_delay(drive);
1025 
1026 			sectorn = ob_ide_pio_readb(drive, IDEREG_SECTOR);
1027 			sectorc = ob_ide_pio_readb(drive, IDEREG_NSECTOR);
1028 
1029 			if (sectorc == 0x01 && sectorn == 0x01)
1030 				break;
1031 
1032 		} while (--timeout);
1033 	}
1034 
1035 	/*
1036 	 * reset done, reselect original device
1037 	 */
1038 	drive->channel->selected = -1;
1039 	ob_ide_select_drive(drive);
1040 }
1041 
1042 /*
1043  * this serves as both a device check, and also to verify that the drives
1044  * we initially "found" are really there
1045  */
1046 static void
ob_ide_device_type_check(struct ide_drive * drive)1047 ob_ide_device_type_check(struct ide_drive *drive)
1048 {
1049 	unsigned char sc, sn, cl, ch, st;
1050 
1051 	if (ob_ide_select_drive(drive))
1052 		return;
1053 
1054 	sc = ob_ide_pio_readb(drive, IDEREG_NSECTOR);
1055 	sn = ob_ide_pio_readb(drive, IDEREG_SECTOR);
1056 
1057 	if (sc == 0x01 && sn == 0x01) {
1058 		/*
1059 		 * read device signature
1060 		 */
1061 		cl = ob_ide_pio_readb(drive, IDEREG_LCYL);
1062 		ch = ob_ide_pio_readb(drive, IDEREG_HCYL);
1063 		st = ob_ide_pio_readb(drive, IDEREG_STATUS);
1064 		if (cl == 0x14 && ch == 0xeb)
1065 			drive->type = ide_type_atapi;
1066 		else if (cl == 0x00 && ch == 0x00 && st != 0x00)
1067 			drive->type = ide_type_ata;
1068 		else
1069 			drive->present = 0;
1070 	} else
1071 		drive->present = 0;
1072 }
1073 
1074 /*
1075  * pure magic
1076  */
1077 static void
ob_ide_device_check(struct ide_drive * drive)1078 ob_ide_device_check(struct ide_drive *drive)
1079 {
1080 	unsigned char sc, sn;
1081 
1082 	/*
1083 	 * non-existing io port should return 0xff, don't probe this
1084 	 * channel at all then
1085 	 */
1086 	if (ob_ide_pio_readb(drive, IDEREG_STATUS) == 0xff) {
1087 		drive->channel->present = 0;
1088 		return;
1089 	}
1090 
1091 	if (ob_ide_select_drive(drive))
1092 		return;
1093 
1094 	ob_ide_pio_writeb(drive, IDEREG_NSECTOR, 0x55);
1095 	ob_ide_pio_writeb(drive, IDEREG_SECTOR, 0xaa);
1096 	ob_ide_pio_writeb(drive, IDEREG_NSECTOR, 0xaa);
1097 	ob_ide_pio_writeb(drive, IDEREG_SECTOR, 0x55);
1098 	ob_ide_pio_writeb(drive, IDEREG_NSECTOR, 0x55);
1099 	ob_ide_pio_writeb(drive, IDEREG_SECTOR, 0xaa);
1100 
1101 	sc = ob_ide_pio_readb(drive, IDEREG_NSECTOR);
1102 	sn = ob_ide_pio_readb(drive, IDEREG_SECTOR);
1103 
1104 	/*
1105 	 * we _think_ the device is there, we will make sure later
1106 	 */
1107 	if (sc == 0x55 && sn == 0xaa) {
1108 		drive->present = 1;
1109 		drive->type = ide_type_unknown;
1110 	}
1111 }
1112 
1113 /*
1114  * probe the legacy ide ports and find attached devices.
1115  */
1116 static void
ob_ide_probe(struct ide_channel * chan)1117 ob_ide_probe(struct ide_channel *chan)
1118 {
1119 	struct ide_drive *drive;
1120 	int i;
1121 
1122 	for (i = 0; i < 2; i++) {
1123 		drive = &chan->drives[i];
1124 
1125 		ob_ide_device_check(drive);
1126 
1127 		/*
1128 		 * no point in continuing
1129 		 */
1130 		if (!chan->present)
1131 			break;
1132 
1133 		if (!drive->present)
1134 			continue;
1135 
1136 		/*
1137 		 * select and reset device
1138 		 */
1139 		if (ob_ide_select_drive(drive))
1140 			continue;
1141 
1142 		ob_ide_software_reset(drive);
1143 
1144 		ob_ide_device_type_check(drive);
1145 	}
1146 }
1147 
1148 /*
1149  * The following functions are interfacing with OpenBIOS. They
1150  * are device node methods. Thus they have to do proper stack handling.
1151  *
1152  */
1153 
1154 /*
1155  * 255 sectors for ata lba28, 65535 for lba48, and 31 sectors for atapi
1156  */
1157 static void
ob_ide_max_transfer(int * idx)1158 ob_ide_max_transfer(int *idx)
1159 {
1160 	struct ide_drive *drive = *(struct ide_drive **)idx;
1161 
1162 	IDE_DPRINTF("max_transfer %x\n", drive->max_sectors * drive->bs);
1163 
1164 	PUSH(drive->max_sectors * drive->bs);
1165 }
1166 
1167 static void
ob_ide_read_blocks(int * idx)1168 ob_ide_read_blocks(int *idx)
1169 {
1170 	cell n = POP(), cnt=n;
1171 	ucell blk = POP();
1172 	unsigned char *dest = (unsigned char *)cell2pointer(POP());
1173 	struct ide_drive *drive = *(struct ide_drive **)idx;
1174 
1175         IDE_DPRINTF("ob_ide_read_blocks %lx block=%ld n=%ld\n",
1176                     (unsigned long)dest, (unsigned long)blk, (long)n);
1177 
1178 	while (n) {
1179 		int len = n;
1180 		if (len > drive->max_sectors)
1181 			len = drive->max_sectors;
1182 
1183 		if (ob_ide_read_sectors(drive, blk, dest, len)) {
1184 			IDE_DPRINTF("ob_ide_read_blocks: error\n");
1185 			RET(0);
1186 		}
1187 
1188 		dest += len * drive->bs;
1189 		n -= len;
1190 		blk += len;
1191 	}
1192 
1193 	PUSH(cnt);
1194 }
1195 
1196 static void
ob_ide_block_size(int * idx)1197 ob_ide_block_size(int *idx)
1198 {
1199 	struct ide_drive *drive = *(struct ide_drive **)idx;
1200 
1201 	IDE_DPRINTF("ob_ide_block_size: block size %x\n", drive->bs);
1202 
1203 	PUSH(drive->bs);
1204 }
1205 
1206 static void
ob_ide_open(int * idx)1207 ob_ide_open(int *idx)
1208 {
1209 	int ret=1;
1210 	phandle_t ph;
1211 	struct ide_drive *drive;
1212 
1213 	PUSH(find_ih_method("drive", my_self()));
1214 	fword("execute");
1215 	drive = cell2pointer(POP());
1216 	*(struct ide_drive **)idx = drive;
1217 
1218 	IDE_DPRINTF("opening channel %d unit %d\n", idx[1], idx[0]);
1219 	dump_drive(drive);
1220 
1221 	if (drive->type != ide_type_ata)
1222 		ret= !ob_ide_atapi_drive_ready(drive);
1223 
1224 	selfword("open-deblocker");
1225 
1226 	/* interpose disk-label */
1227 	ph = find_dev("/packages/disk-label");
1228 	fword("my-args");
1229 	PUSH_ph( ph );
1230 	fword("interpose");
1231 
1232 	RET ( -ret );
1233 }
1234 
1235 static void
ob_ide_close(struct ide_drive * drive)1236 ob_ide_close(struct ide_drive *drive)
1237 {
1238 	selfword("close-deblocker");
1239 }
1240 
1241 static void
ob_ide_dma_alloc(int * idx)1242 ob_ide_dma_alloc(int *idx)
1243 {
1244     call_parent_method("dma-alloc");
1245 }
1246 
1247 static void
ob_ide_dma_free(int * idx)1248 ob_ide_dma_free(int *idx)
1249 {
1250     call_parent_method("dma-free");
1251 }
1252 
1253 static void
ob_ide_dma_map_in(int * idx)1254 ob_ide_dma_map_in(int *idx)
1255 {
1256     call_parent_method("dma-map-in");
1257 }
1258 
1259 static void
ob_ide_dma_map_out(int * idx)1260 ob_ide_dma_map_out(int *idx)
1261 {
1262     call_parent_method("dma-map-out");
1263 }
1264 
1265 static void
ob_ide_dma_sync(int * idx)1266 ob_ide_dma_sync(int *idx)
1267 {
1268     call_parent_method("dma-sync");
1269 }
1270 
1271 NODE_METHODS(ob_ide) = {
1272 	{ "open",		ob_ide_open		},
1273 	{ "close",		ob_ide_close		},
1274 	{ "read-blocks",	ob_ide_read_blocks	},
1275 	{ "block-size",		ob_ide_block_size	},
1276 	{ "max-transfer",	ob_ide_max_transfer	},
1277 	{ "dma-alloc",		ob_ide_dma_alloc	},
1278 	{ "dma-free",		ob_ide_dma_free		},
1279 	{ "dma-map-in",		ob_ide_dma_map_in	},
1280 	{ "dma-map-out",	ob_ide_dma_map_out	},
1281 	{ "dma-sync",		ob_ide_dma_sync		},
1282 };
1283 
1284 static void
ob_ide_ctrl_decodeunit(int * idx)1285 ob_ide_ctrl_decodeunit(int *idx)
1286 {
1287 	fword("parse-hex");
1288 }
1289 
1290 static void
ob_ide_ctrl_open(int * idx)1291 ob_ide_ctrl_open(int *idx)
1292 {
1293 	RET(-1);
1294 }
1295 
1296 static void
ob_ide_ctrl_close(int * idx)1297 ob_ide_ctrl_close(int *idx)
1298 {
1299 }
1300 
1301 NODE_METHODS(ob_ide_ctrl) = {
1302 	{ "open",		ob_ide_ctrl_open		},
1303 	{ "close",		ob_ide_ctrl_close		},
1304 	{ "decode-unit",	ob_ide_ctrl_decodeunit  },
1305 	{ "dma-alloc",		ob_ide_dma_alloc	},
1306 	{ "dma-free",		ob_ide_dma_free		},
1307 	{ "dma-map-in",		ob_ide_dma_map_in	},
1308 	{ "dma-map-out",	ob_ide_dma_map_out	},
1309 	{ "dma-sync",		ob_ide_dma_sync		},
1310 };
1311 
set_cd_alias(const char * path)1312 static void set_cd_alias(const char *path)
1313 {
1314 	phandle_t aliases;
1315 
1316 	aliases = find_dev("/aliases");
1317 
1318 	if (get_property(aliases, "cd", NULL))
1319 		return;
1320 
1321 	set_property(aliases, "cd", path, strlen(path) + 1);
1322 	set_property(aliases, "cdrom", path, strlen(path) + 1);
1323 }
1324 
set_hd_alias(const char * path)1325 static void set_hd_alias(const char *path)
1326 {
1327 	phandle_t aliases;
1328 
1329 	aliases = find_dev("/aliases");
1330 
1331 	if (get_property(aliases, "hd", NULL))
1332 		return;
1333 
1334 	set_property(aliases, "hd", path, strlen(path) + 1);
1335 	set_property(aliases, "disk", path, strlen(path) + 1);
1336 }
1337 
set_ide_alias(const char * path)1338 static void set_ide_alias(const char *path)
1339 {
1340 	phandle_t aliases;
1341 	static int ide_counter = 0;
1342 	char idestr[8];
1343 
1344 	aliases = find_dev("/aliases");
1345 
1346 	snprintf(idestr, sizeof(idestr), "ide%d", ide_counter++);
1347 	set_property(aliases, idestr, path, strlen(path) + 1);
1348 }
1349 
ob_ide_init(const char * path,uint32_t io_port0,uint32_t ctl_port0,uint32_t io_port1,uint32_t ctl_port1)1350 int ob_ide_init(const char *path, uint32_t io_port0, uint32_t ctl_port0,
1351 		uint32_t io_port1, uint32_t ctl_port1)
1352 {
1353 	int i, j;
1354 	char nodebuff[128];
1355 	phandle_t dnode;
1356 	struct ide_channel *chan;
1357 	int io_ports[IDE_MAX_CHANNELS];
1358 	int ctl_ports[IDE_MAX_CHANNELS];
1359 	u32 props[6];
1360 
1361 	io_ports[0] = io_port0;
1362 	ctl_ports[0] = ctl_port0;
1363 	io_ports[1] = io_port1;
1364 	ctl_ports[1] = ctl_port1;
1365 
1366 	for (i = 0; i < IDE_NUM_CHANNELS; i++, current_channel++) {
1367 
1368 		chan = malloc(sizeof(struct ide_channel));
1369 
1370 		chan->mmio = 0;
1371 
1372 		for (j = 0; j < 8; j++)
1373 			chan->io_regs[j] = io_ports[i] + j;
1374 
1375 		chan->io_regs[8] = ctl_ports[i];
1376 		chan->io_regs[9] = ctl_ports[i] + 1;
1377 
1378 		chan->obide_inb = ob_ide_inb;
1379 		chan->obide_insw = ob_ide_insw;
1380 		chan->obide_outb = ob_ide_outb;
1381 		chan->obide_outsw = ob_ide_outsw;
1382 
1383 		chan->selected = -1;
1384 
1385 		/*
1386 		 * assume it's there, if not io port dead check will clear
1387 		 */
1388 		chan->present = 1;
1389 
1390 		for (j = 0; j < 2; j++) {
1391 			chan->drives[j].present = 0;
1392 			chan->drives[j].unit = j;
1393 			chan->drives[j].channel = chan;
1394 			/* init with a decent value */
1395 			chan->drives[j].bs = 512;
1396 
1397 			chan->drives[j].nr = i * 2 + j;
1398 		}
1399 
1400 		ob_ide_probe(chan);
1401 
1402 		if (!chan->present)
1403 			continue;
1404 
1405 		ob_ide_identify_drives(chan);
1406 
1407 		fword("new-device");
1408 		dnode = get_cur_dev();
1409 
1410 		PUSH(pointer2cell(chan));
1411 		feval("value chan");
1412 
1413 		BIND_NODE_METHODS(get_cur_dev(), ob_ide_ctrl);
1414 
1415 #if !defined(CONFIG_PPC) && !defined(CONFIG_SPARC64)
1416 		props[0]=14; props[1]=0;
1417 		set_property(dnode, "interrupts",
1418 			     (char *)&props, 2*sizeof(props[0]));
1419 #endif
1420 
1421 		props[0] = __cpu_to_be32(current_channel);
1422 		props[1] = __cpu_to_be32(0);
1423 		props[2] = 0;
1424 		set_property(dnode, "reg", (char *)&props, 3*sizeof(props[0]));
1425 
1426 		set_int_property(dnode, "#address-cells", 1);
1427 		set_int_property(dnode, "#size-cells", 0);
1428 
1429 		push_str(DEV_NAME);
1430 		fword("device-name");
1431 
1432 		push_str(DEV_TYPE);
1433 		fword("device-type");
1434 
1435 		IDE_DPRINTF(DEV_NAME": [io ports 0x%x-0x%x,0x%x]\n",
1436 		            current_channel, chan->io_regs[0],
1437 		            chan->io_regs[0] + 7, chan->io_regs[8]);
1438 
1439 		for (j = 0; j < 2; j++) {
1440 			struct ide_drive *drive = &chan->drives[j];
1441                         const char *media = "UNKNOWN";
1442 
1443 			if (!drive->present)
1444 				continue;
1445 
1446 			IDE_DPRINTF("    drive%d [ATA%s ", j,
1447 			            drive->type == ide_type_atapi ? "PI" : "");
1448 			switch (drive->media) {
1449 				case ide_media_floppy:
1450 					media = "floppy";
1451 					break;
1452 				case ide_media_cdrom:
1453 					media = "cdrom";
1454 					break;
1455 				case ide_media_optical:
1456 					media = "mo";
1457 					break;
1458 				case ide_media_disk:
1459 					media = "disk";
1460 					break;
1461 			}
1462 
1463 			IDE_DPRINTF("%s]: %s\n", media, drive->model);
1464 
1465 			fword("new-device");
1466 			dnode = get_cur_dev();
1467 			set_int_property(dnode, "reg", j);
1468 			push_str(media);
1469 			fword("device-name");
1470 
1471 			push_str("block");
1472 			fword("device-type");
1473 
1474 			PUSH(pointer2cell(drive));
1475 			feval("value drive");
1476 
1477 			BIND_NODE_METHODS(dnode, ob_ide);
1478 			fword("is-deblocker");
1479 
1480 			fword("finish-device");
1481 
1482 			/* create aliases */
1483 			snprintf(nodebuff, sizeof(nodebuff), "%s",
1484 				 get_path_from_ph(dnode));
1485 			set_ide_alias(nodebuff);
1486 			if (drive->media == ide_media_cdrom)
1487 				set_cd_alias(nodebuff);
1488 			if (drive->media == ide_media_disk)
1489 				set_hd_alias(nodebuff);
1490 		}
1491 
1492 		fword("finish-device");
1493 	}
1494 
1495 	return 0;
1496 }
1497 
ob_ide_quiesce(void)1498 void ob_ide_quiesce(void)
1499 {
1500 	phandle_t ph = 0, xt;
1501 	struct ide_drive *drive;
1502 
1503 	while ((ph = dt_iterate_type(ph, "block"))) {
1504 		xt = find_package_method("drive", ph);
1505 
1506 		if (xt) {
1507 			PUSH(xt);
1508 			fword("execute");
1509 			drive = cell2pointer(POP());
1510 
1511 			ob_ide_select_drive(drive);
1512 			ob_ide_software_reset(drive);
1513 			ob_ide_device_type_check(drive);
1514 		}
1515 	}
1516 }
1517 
1518 #if defined(CONFIG_DRIVER_MACIO)
1519 static unsigned char
macio_ide_inb(struct ide_channel * chan,unsigned int port)1520 macio_ide_inb(struct ide_channel *chan, unsigned int port)
1521 {
1522 	return in_8((unsigned char*)(chan->mmio + (port << 4)));
1523 }
1524 
1525 static void
macio_ide_outb(struct ide_channel * chan,unsigned char data,unsigned int port)1526 macio_ide_outb(struct ide_channel *chan, unsigned char data, unsigned int port)
1527 {
1528 	out_8((unsigned char*)(chan->mmio + (port << 4)), data);
1529 }
1530 
1531 static void
macio_ide_insw(struct ide_channel * chan,unsigned int port,unsigned char * addr,unsigned int count)1532 macio_ide_insw(struct ide_channel *chan,
1533 	       unsigned int port, unsigned char *addr, unsigned int count)
1534 {
1535 	_insw((uint16_t*)(chan->mmio + (port << 4)), addr, count);
1536 }
1537 
1538 static void
macio_ide_outsw(struct ide_channel * chan,unsigned int port,unsigned char * addr,unsigned int count)1539 macio_ide_outsw(struct ide_channel *chan,
1540 		unsigned int port, unsigned char *addr, unsigned int count)
1541 {
1542 	_outsw((uint16_t*)(chan->mmio + (port << 4)), addr, count);
1543 }
1544 
1545 #define MACIO_IDE_OFFSET	0x00020000
1546 #define MACIO_IDE_SIZE		0x00001000
1547 
macio_ide_init(const char * path,uint32_t addr,int nb_channels)1548 int macio_ide_init(const char *path, uint32_t addr, int nb_channels)
1549 {
1550 	int i, j;
1551 	char nodebuff[128];
1552 	phandle_t dnode;
1553 	u32 props[8];
1554 	struct ide_channel *chan;
1555 
1556 	/* IDE ports on Macs are numbered from 3.
1557 	 * Also see comments in pci.c:ob_pci_host_set_interrupt_map() */
1558 	current_channel = 3;
1559 
1560 	for (i = 0; i < nb_channels; i++) {
1561 
1562 		chan = malloc(sizeof(struct ide_channel));
1563 
1564 		chan->mmio = addr + MACIO_IDE_OFFSET + i * MACIO_IDE_SIZE;
1565 
1566 		chan->obide_inb = macio_ide_inb;
1567 		chan->obide_insw = macio_ide_insw;
1568 		chan->obide_outb = macio_ide_outb;
1569 		chan->obide_outsw = macio_ide_outsw;
1570 
1571 		chan->selected = -1;
1572 
1573 		/*
1574 		 * assume it's there, if not io port dead check will clear
1575 		 */
1576 		chan->present = 1;
1577 
1578 		for (j = 0; j < 2; j++) {
1579 			chan->drives[j].present = 0;
1580 			chan->drives[j].unit = j;
1581 			chan->drives[j].channel = chan;
1582 			/* init with a decent value */
1583 			chan->drives[j].bs = 512;
1584 
1585 			chan->drives[j].nr = i * 2 + j;
1586 		}
1587 
1588 		ob_ide_probe(chan);
1589 
1590 		if (!chan->present) {
1591 			free(chan);
1592 			continue;
1593 		}
1594 
1595 		ob_ide_identify_drives(chan);
1596 
1597 		fword("new-device");
1598 		dnode = get_cur_dev();
1599 
1600 		PUSH(pointer2cell(chan));
1601 		feval("value chan");
1602 
1603 		snprintf(nodebuff, sizeof(nodebuff), DEV_NAME "-%d", current_channel);
1604 		push_str(nodebuff);
1605 		fword("device-name");
1606 
1607 		push_str(DEV_TYPE);
1608 		fword("device-type");
1609 
1610 		set_int_property(dnode, "#address-cells", 1);
1611 		set_int_property(dnode, "#size-cells", 0);
1612 
1613 		set_property(dnode, "compatible", (is_oldworld() ?
1614 			     "heathrow-ata" : "keylargo-ata"), 13);
1615 
1616 		set_property(dnode, "model", ((current_channel == 3) ?
1617 			     "ata-3" : "ata-4"), strlen("ata-*") + 1);
1618 
1619 		set_property(dnode, "AAPL,connector", "ata",
1620                              strlen("ata") + 1);
1621 
1622 		props[0] = 0x00000526;
1623 		props[1] = 0x00000085;
1624 		props[2] = 0x00000025;
1625 		props[3] = 0x00000025;
1626 		props[4] = 0x00000025;
1627 		props[5] = 0x00000000;
1628 		props[6] = 0x00000000;
1629 		props[7] = 0x00000000;
1630 		set_property(dnode, "AAPL,pio-timing",
1631 				      (char *)&props, 8*sizeof(props[0]));
1632 
1633 		/* The first interrupt entry is the ide interrupt, the second
1634 		   the dbdma interrupt */
1635 		switch (i) {
1636 		case 0:
1637 			props[0] = 0x0000000d;
1638 			props[2] = 0x00000002;
1639 			break;
1640 		case 1:
1641 			props[0] = 0x0000000e;
1642 			props[2] = 0x00000003;
1643 			break;
1644 		case 2:
1645 			props[0] = 0x0000000f;
1646 			props[2] = 0x00000004;
1647 			break;
1648 		default:
1649 			props[0] = 0x00000000;
1650 			props[2] = 0x00000000;
1651 			break;
1652 		}
1653 		props[1] = 0x00000001;
1654 		props[3] = 0x00000000;
1655 		NEWWORLD(set_property(dnode, "interrupts",
1656 			     (char *)&props, 4*sizeof(props[0])));
1657 		NEWWORLD(set_int_property(dnode, "#interrupt-cells", 2));
1658 
1659 		props[1] = props[2];
1660 		OLDWORLD(set_property(dnode, "AAPL,interrupts",
1661 				      (char *)&props, 2*sizeof(props[0])));
1662 
1663 		props[0] = MACIO_IDE_OFFSET + i * MACIO_IDE_SIZE;
1664 		props[1] = MACIO_IDE_SIZE;
1665 		props[2] = 0x00008b00 + i * 0x0200;
1666 		props[3] = 0x0200;
1667 		set_property(dnode, "reg", (char *)&props, 4*sizeof(props[0]));
1668 
1669 		props[0] = addr + MACIO_IDE_OFFSET  + i * MACIO_IDE_SIZE;
1670 		props[1] = addr + 0x00008b00 + i * 0x0200;
1671 		OLDWORLD(set_property(dnode, "AAPL,address",
1672 				      (char *)&props, 2*sizeof(props[0])));
1673 
1674 		props[0] = i;
1675 		set_property(dnode, "AAPL,bus-id", (char*)props,
1676 			 1 * sizeof(props[0]));
1677 		IDE_DPRINTF(DEV_NAME": [io ports 0x%lx]\n",
1678 		            current_channel, chan->mmio);
1679 
1680 		BIND_NODE_METHODS(dnode, ob_ide_ctrl);
1681 
1682 		for (j = 0; j < 2; j++) {
1683 			struct ide_drive *drive = &chan->drives[j];
1684                         const char *media = "UNKNOWN";
1685 
1686 			if (!drive->present)
1687 				continue;
1688 
1689 			IDE_DPRINTF("    drive%d [ATA%s ", j,
1690 			            drive->type == ide_type_atapi ? "PI" : "");
1691 			switch (drive->media) {
1692 				case ide_media_floppy:
1693 					media = "floppy";
1694 					break;
1695 				case ide_media_cdrom:
1696 					media = "cdrom";
1697 					break;
1698 				case ide_media_optical:
1699 					media = "mo";
1700 					break;
1701 				case ide_media_disk:
1702 					media = "disk";
1703 					break;
1704 			}
1705 			IDE_DPRINTF("%s]: %s\n", media, drive->model);
1706 
1707 			fword("new-device");
1708 			dnode = get_cur_dev();
1709 			set_int_property(dnode, "reg", j);
1710 			push_str(media);
1711 			fword("device-name");
1712 
1713 			push_str("block");
1714 			fword("device-type");
1715 
1716 			PUSH(pointer2cell(drive));
1717 			feval("value drive");
1718 
1719 			BIND_NODE_METHODS(dnode, ob_ide);
1720 			fword("is-deblocker");
1721 
1722 			fword("finish-device");
1723 
1724 			/* create aliases */
1725 			snprintf(nodebuff, sizeof(nodebuff), "%s",
1726 				 get_path_from_ph(dnode));
1727 			set_ide_alias(nodebuff);
1728 			if (drive->media == ide_media_cdrom)
1729 				set_cd_alias(nodebuff);
1730 			if (drive->media == ide_media_disk)
1731 				set_hd_alias(nodebuff);
1732 		}
1733 
1734 		fword("finish-device");
1735 	}
1736 
1737 	return 0;
1738 }
1739 #endif /* CONFIG_DRIVER_MACIO */
1740