Quickfix to repair 'make clean; make menuconfig' (trivial).
[coreboot.git] / src / pc80 / ide / ide.c
1 /* Derived from Etherboot 5.1 */
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include <console/console.h>
6 #include <arch/io.h>
7 #include <pc80/ide.h>
8 #include <device/device.h>
9 #include <device/pci.h>
10 #include <delay.h>
11 #include <arch/byteorder.h>
12
13 #define BSY_SET_DURING_SPINUP 1
14
15 static unsigned short ide_base[] = {
16         IDE_BASE0,
17         IDE_BASE1, 
18         IDE_BASE2, 
19         IDE_BASE3, 
20         0
21 };
22
23 static struct controller controllers[IDE_MAX_CONTROLLERS];
24 static struct harddisk_info harddisk_info[IDE_MAX_DRIVES];
25
26 static unsigned char ide_buffer[IDE_SECTOR_SIZE];
27
28 static int await_ide(int (*done)(struct controller *ctrl), 
29         struct controller *ctrl, unsigned long timeout)
30 {
31         int result;
32         for(;;) {
33                 result = done(ctrl);
34                 if (result) {
35                         return 0;
36                 }
37                 //poll_interruptions();
38                 if (timeout-- <= 0) {
39                         break;
40                 }
41                 udelay(1000); /* Added to avoid spinning GRW */
42         }
43         printk_info("IDE time out\n");
44         return -1;
45 }
46
47 /* The maximum time any IDE command can last 31 seconds,
48  * So if any IDE commands takes this long we know we have problems.
49  */
50 #define IDE_TIMEOUT (32*1000)
51
52 static int not_bsy(struct controller *ctrl)
53 {
54         return !(inb(IDE_REG_STATUS(ctrl)) & IDE_STATUS_BSY);
55 }
56
57 /* IDE drives assert BSY bit within 400 nsec when SRST is set.
58  * Use 2 msec since our tick is 1 msec */
59 #define IDE_RESET_PULSE 2
60
61 static int bsy(struct controller *ctrl)
62 {
63         return inb(IDE_REG_STATUS(ctrl)) & IDE_STATUS_BSY;
64 }
65
66 #if  !BSY_SET_DURING_SPINUP
67 static int timeout(struct controller *ctrl)
68 {
69         return 0;
70 }
71 #endif
72
73 static void print_status(struct controller *ctrl)
74 {
75         printk_debug("IDE: status=%#x, err=%#x\n",
76                         inb(IDE_REG_STATUS(ctrl)), inb(IDE_REG_ERROR(ctrl)));
77 }
78
79 static int ide_software_reset(struct controller *ctrl)
80 {
81         /* Wait a little bit in case this is immediately after
82          * hardware reset.
83          */
84         mdelay(2);
85         /* A software reset should not be delivered while the bsy bit
86          * is set.  If the bsy bit does not clear in a reasonable
87          * amount of time give up.
88          */
89         printk_debug("Waiting for ide%d to become ready for reset... ",
90                         ctrl - controllers);
91         if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
92                 printk_debug("failed\n");
93                 return -1;
94         }
95         printk_debug("ok\n");
96
97         /* Disable Interrupts and reset the ide bus */
98         outb(IDE_CTRL_HD15 | IDE_CTRL_SRST | IDE_CTRL_NIEN, 
99                 IDE_REG_DEVICE_CONTROL(ctrl));
100         /* If BSY bit is not asserted within 400ns, no device there */
101         if (await_ide(bsy, ctrl, IDE_RESET_PULSE) < 0) {
102                 return -1;
103         }
104         outb(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(ctrl));
105         mdelay(2);
106         if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
107                 return -1;
108         }
109         return 0;
110 }
111
112 static void pio_set_registers(
113         struct controller *ctrl, const struct ide_pio_command *cmd)
114 {
115         uint8_t device;
116         /* Disable Interrupts */
117         outb(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(ctrl));
118
119         /* Possibly switch selected device */
120         device = inb(IDE_REG_DEVICE(ctrl));
121         outb(cmd->device,          IDE_REG_DEVICE(ctrl));
122         if ((device & (1UL << 4)) != (cmd->device & (1UL << 4))) {
123                 /* Allow time for the selected drive to switch,
124                  * The linux ide code suggests 50ms is the right
125                  * amount of time to use here.
126                  */
127                 mdelay(50); 
128         }
129         outb(cmd->feature,         IDE_REG_FEATURE(ctrl));
130         if (cmd->command == IDE_CMD_READ_SECTORS_EXT) {
131                 outb(cmd->sector_count2,   IDE_REG_SECTOR_COUNT(ctrl));
132                 outb(cmd->lba_low2,        IDE_REG_LBA_LOW(ctrl));
133                 outb(cmd->lba_mid2,        IDE_REG_LBA_MID(ctrl));
134                 outb(cmd->lba_high2,       IDE_REG_LBA_HIGH(ctrl));
135         }
136         outb(cmd->sector_count,    IDE_REG_SECTOR_COUNT(ctrl));
137         outb(cmd->lba_low,         IDE_REG_LBA_LOW(ctrl));
138         outb(cmd->lba_mid,         IDE_REG_LBA_MID(ctrl));
139         outb(cmd->lba_high,        IDE_REG_LBA_HIGH(ctrl));
140         outb(cmd->command,         IDE_REG_COMMAND(ctrl));
141 }
142
143
144 static int pio_non_data(struct controller *ctrl, const struct ide_pio_command *cmd)
145 {
146         /* Wait until the busy bit is clear */
147         if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
148                 return -1;
149         }
150
151         pio_set_registers(ctrl, cmd);
152         udelay(1);
153         if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
154                 return -1;
155         }
156         /* FIXME is there more error checking I could do here? */
157         return 0;
158 }
159
160 static int pio_data_in(struct controller *ctrl, 
161         const struct ide_pio_command *cmd,
162         void *buffer, size_t bytes)
163 {
164         unsigned int status;
165
166         /* FIXME handle commands with multiple blocks */
167         /* Wait until the busy bit is clear */
168         if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
169                 return -1;
170         }
171
172         /* How do I tell if INTRQ is asserted? */
173         pio_set_registers(ctrl, cmd);
174         udelay(1);
175         if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
176                 return -1;
177         }
178         status = inb(IDE_REG_STATUS(ctrl));
179         if (!(status & IDE_STATUS_DRQ)) {
180                 print_status(ctrl);
181                 return -1;
182         }
183         insw(IDE_REG_DATA(ctrl), buffer, bytes/2);
184         status = inb(IDE_REG_STATUS(ctrl));
185         if (status & IDE_STATUS_DRQ) {
186                 print_status(ctrl);
187                 return -1;
188         }
189         return 0;
190 }
191
192 #ifdef __BIG_ENDIAN
193 static int pio_data_in_sw(struct controller *ctrl, 
194         const struct ide_pio_command *cmd,
195         void *buffer, size_t bytes)
196 {
197         unsigned int status;
198
199         /* FIXME handle commands with multiple blocks */
200         /* Wait until the busy bit is clear */
201         if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
202                 return -1;
203         }
204
205         /* How do I tell if INTRQ is asserted? */
206         pio_set_registers(ctrl, cmd);
207         udelay(1);
208         if (await_ide(not_bsy, ctrl, IDE_TIMEOUT) < 0) {
209                 return -1;
210         }
211         status = inb(IDE_REG_STATUS(ctrl));
212         if (!(status & IDE_STATUS_DRQ)) {
213                 print_status(ctrl);
214                 return -1;
215         }
216         insw_ns(IDE_REG_DATA(ctrl), buffer, bytes/2);
217         status = inb(IDE_REG_STATUS(ctrl));
218         if (status & IDE_STATUS_DRQ) {
219                 print_status(ctrl);
220                 return -1;
221         }
222         return 0;
223 }
224 #endif /* __BIG_ENDIAN */
225
226 static int pio_packet(struct harddisk_info *info, int in,
227         const void *packet, int packet_len,
228         void *buffer, int buffer_len)
229 {
230         unsigned int status;
231         struct ide_pio_command cmd;
232
233         memset(&cmd, 0, sizeof(cmd));
234
235         /* Wait until the busy bit is clear */
236         if (await_ide(not_bsy, info->ctrl, IDE_TIMEOUT) < 0) {
237                 return -1;
238         }
239
240         /* Issue a PACKET command */
241         cmd.lba_mid = (uint8_t) buffer_len;
242         cmd.lba_high = (uint8_t) (buffer_len >> 8);
243         cmd.device = IDE_DH_DEFAULT | info->slave;
244         cmd.command = IDE_CMD_PACKET;
245         pio_set_registers(info->ctrl, &cmd);
246         udelay(1);
247         if (await_ide(not_bsy, info->ctrl, IDE_TIMEOUT) < 0) {
248                 return -1;
249         }
250         status = inb(IDE_REG_STATUS(info->ctrl));
251         if (!(status & IDE_STATUS_DRQ)) {
252                 printk_debug("no drq after PACKET\n");
253                 print_status(info->ctrl);
254                 return -1;
255         }
256
257         /* Send the packet */
258 #ifdef __BIG_ENDIAN
259         outsw_ns(IDE_REG_DATA(info->ctrl), packet, packet_len/2);
260 #else /* __BIG_ENDIAN */
261         outsw(IDE_REG_DATA(info->ctrl), packet, packet_len/2);
262 #endif /* __BIG_ENDIAN */
263
264         if (await_ide(not_bsy, info->ctrl, IDE_TIMEOUT) < 0) {
265                 return -1;
266         }
267         status = inb(IDE_REG_STATUS(info->ctrl));
268         if (buffer_len == 0) {
269                 if (status & IDE_STATUS_DRQ) {
270                         printk_debug("drq after non-data command\n");
271                         print_status(info->ctrl);
272                         return -1;
273                 }
274                 return 0;
275         }
276
277         if (!(status & IDE_STATUS_DRQ)) {
278                 printk_debug("no drq after sending packet\n");
279                 print_status(info->ctrl);
280                 return -1;
281         }
282
283 #ifdef __BIG_ENDIAN
284         insw_ns(IDE_REG_DATA(info->ctrl), buffer, buffer_len/2);
285 #else /* __BIG_ENDIAN */
286         insw(IDE_REG_DATA(info->ctrl), buffer, buffer_len/2);
287 #endif /* __BIG_ENDIAN */
288
289         status = inb(IDE_REG_STATUS(info->ctrl));
290         if (status & IDE_STATUS_DRQ) {
291                 printk_debug("drq after insw\n");
292                 print_status(info->ctrl);
293                 return -1;
294         }
295         return 0;
296 }
297
298 static inline int ide_read_sector_chs(
299         struct harddisk_info *info, void *buffer, unsigned long sector)
300 {
301         struct ide_pio_command cmd;
302         unsigned int track;
303         unsigned int offset;
304         unsigned int cylinder;
305                 
306         memset(&cmd, 0, sizeof(cmd));
307         cmd.sector_count = 1;
308
309         //printk_debug("ide_read_sector_chs: sector= %ld.\n",sector);
310
311         track = sector / info->sectors_per_track;
312         /* Sector number */
313         offset = 1 + (sector % info->sectors_per_track);
314         cylinder = track / info->heads;
315         cmd.lba_low = offset;
316         cmd.lba_mid = cylinder & 0xff;
317         cmd.lba_high = (cylinder >> 8) & 0xff;
318         cmd.device = IDE_DH_DEFAULT |
319                 IDE_DH_HEAD(track % info->heads) |
320                 info->slave |
321                 IDE_DH_CHS;
322         cmd.command = IDE_CMD_READ_SECTORS;
323 #ifdef __BIG_ENDIAN
324         return pio_data_in_sw(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
325 #else /* __BIG_ENDIAN */
326         return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
327 #endif /* __BIG_ENDIAN */
328 }
329
330 static inline int ide_read_sector_lba(
331         struct harddisk_info *info, void *buffer, unsigned long sector)
332 {
333         struct ide_pio_command cmd;
334         memset(&cmd, 0, sizeof(cmd));
335
336         cmd.sector_count = 1;
337         cmd.lba_low = sector & 0xff;
338         cmd.lba_mid = (sector >> 8) & 0xff;
339         cmd.lba_high = (sector >> 16) & 0xff;
340         cmd.device = IDE_DH_DEFAULT |
341                 ((sector >> 24) & 0x0f) |
342                 info->slave | 
343                 IDE_DH_LBA;
344         cmd.command = IDE_CMD_READ_SECTORS;
345         //printk_debug("%s: sector= %ld, device command= 0x%x.\n",__FUNCTION__,(unsigned long) sector, cmd.device);
346 #ifdef __BIG_ENDIAN
347         return pio_data_in_sw(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
348 #else /* __BIG_ENDIAN */
349         return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
350 #endif /* __BIG_ENDIAN */
351 }
352
353 static inline int ide_read_sector_lba48(
354         struct harddisk_info *info, void *buffer, sector_t sector)
355 {
356         struct ide_pio_command cmd;
357         memset(&cmd, 0, sizeof(cmd));
358         //printk_debug("ide_read_sector_lba48: sector= %ld.\n",(unsigned long) sector);
359
360         cmd.sector_count = 1;
361         cmd.lba_low = sector & 0xff;
362         cmd.lba_mid = (sector >> 8) & 0xff;
363         cmd.lba_high = (sector >> 16) & 0xff;
364         cmd.lba_low2 = (sector >> 24) & 0xff;
365         cmd.lba_mid2 = (sector >> 32) & 0xff;
366         cmd.lba_high2 = (sector >> 40) & 0xff;
367         cmd.device =  info->slave | IDE_DH_LBA;
368         cmd.command = IDE_CMD_READ_SECTORS_EXT;
369 #ifdef __BIG_ENDIAN
370         return pio_data_in_sw(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
371 #else /* __BIG_ENDIAN */
372         return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
373 #endif /* __BIG_ENDIAN */
374 }
375
376 static inline int ide_read_sector_packet(
377         struct harddisk_info *info, void *buffer, sector_t sector)
378 {
379         char packet[12];
380         static uint8_t cdbuffer[CDROM_SECTOR_SIZE];
381         static struct harddisk_info *last_disk = 0;
382         static sector_t last_sector = (sector_t) -1;
383         uint8_t *buf;
384         uint32_t hw_sector;
385
386         //printk_debug("sector=%Ld\n", sector);
387
388         if (info->hw_sector_size == CDROM_SECTOR_SIZE) {
389                 buf = cdbuffer;
390                 hw_sector = sector >> 2;
391         } else {
392                 buf = buffer;
393                 hw_sector = sector;
394         }
395
396         if (buf==buffer || info != last_disk || hw_sector != last_sector) {
397                 //printk_debug("hw_sector=%u\n", hw_sector);
398                 memset(packet, 0, sizeof packet);
399                 packet[0] = 0x28; /* READ */
400                 packet[2] = hw_sector >> 24;
401                 packet[3] = hw_sector >> 16;
402                 packet[4] = hw_sector >> 8;
403                 packet[5] = hw_sector >> 0;
404                 packet[7] = 0;
405                 packet[8] = 1; /* length */
406
407                 if (pio_packet(info, 1, packet, sizeof packet,
408                                         buf, info->hw_sector_size) != 0) {
409                         printk_debug("read error\n");
410                         return -1;
411                 }
412                 last_disk = info;
413                 last_sector = hw_sector;
414         }
415
416         if (buf != buffer)
417                 memcpy(buffer, &cdbuffer[(sector & 3) << 9], IDE_SECTOR_SIZE);
418         return 0;
419 }
420
421 int ide_read(int drive, sector_t sector, void *buffer)
422 {
423         struct harddisk_info *info = &harddisk_info[drive];
424         int result;
425
426         //printk_debug("drive=%d, sector=%ld\n",drive,(unsigned long) sector);
427         /* Report the buffer is empty */
428         if (sector > info->sectors) {
429                 return -1;
430         }
431         if (info->address_mode == ADDRESS_MODE_CHS) {
432                 result = ide_read_sector_chs(info, buffer, sector);
433         }
434         else if (info->address_mode == ADDRESS_MODE_LBA) {
435                 result = ide_read_sector_lba(info, buffer, sector);
436         }
437         else if (info->address_mode == ADDRESS_MODE_LBA48) {
438                 result = ide_read_sector_lba48(info, buffer, sector);
439         }
440         else if (info->address_mode == ADDRESS_MODE_PACKET) {
441                 result = ide_read_sector_packet(info, buffer, sector);
442         }
443         else {
444                 result = -1;
445         }
446         return result;
447 }
448
449 static int init_drive(struct harddisk_info *info, struct controller *ctrl,
450                 int slave, int drive, unsigned char *buffer, int ident_command)
451 {
452         uint16_t* drive_info;
453         struct ide_pio_command cmd;
454         int i;
455
456         info->ctrl = ctrl;
457         info->heads = 0u;
458         info->cylinders = 0u;
459         info->sectors_per_track = 0u;
460         info->address_mode = IDE_DH_CHS;
461         info->sectors = 0ul;
462         info->drive_exists = 0;
463         info->slave_absent = 0;
464         info->removable = 0;
465         info->hw_sector_size = IDE_SECTOR_SIZE;
466         info->slave = slave?IDE_DH_SLAVE: IDE_DH_MASTER;
467
468         printk_debug("Testing for hd%c\n", 'a'+drive);
469
470         /* Select the drive that we are testing */
471         outb(IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave, 
472                 IDE_REG_DEVICE(ctrl));
473         mdelay(50);
474
475         /* Test to see if the drive registers exist,
476          * In many cases this quickly rules out a missing drive.
477          */
478         for(i = 0; i < 4; i++) {
479                 outb(0xaa + i, (ctrl->cmd_base) + 2 + i);
480         }
481         for(i = 0; i < 4; i++) {
482                 if (inb((ctrl->cmd_base) + 2 + i) != 0xaa + i) {
483                         return 1;
484                 }
485         }
486         for(i = 0; i < 4; i++) {
487                 outb(0x55 + i, (ctrl->cmd_base) + 2 + i);
488         }
489         for(i = 0; i < 4; i++) {
490                 if (inb((ctrl->cmd_base) + 2 + i) != 0x55 + i) {
491                         return 1;
492                 }
493         }
494         printk_debug("Probing for hd%c\n", 'a'+drive);
495         
496         memset(&cmd, 0, sizeof(cmd));
497         cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave;
498         cmd.command = ident_command;
499
500         if (pio_data_in(ctrl, &cmd, buffer, IDE_SECTOR_SIZE) < 0) {
501                 /* Well, if that command didn't work, we probably don't have drive. */
502                 return 1;
503         }
504
505         /* Now suck the data out */
506         drive_info = (uint16_t *)buffer;
507         if (drive_info[2] == 0x37C8) {
508                 /* If the response is incomplete spin up the drive... */
509                 memset(&cmd, 0, sizeof(cmd));
510                 cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
511                         info->slave;
512                 cmd.feature = IDE_FEATURE_STANDBY_SPINUP_DRIVE;
513                 if (pio_non_data(ctrl, &cmd) < 0) {
514                         /* If the command doesn't work give up on the drive */
515                         return 1;
516                 }
517                 
518         }
519         if ((drive_info[2] == 0x37C8) || (drive_info[2] == 0x8C73)) {
520                 /* The response is incomplete retry the drive info command */
521                 memset(&cmd, 0, sizeof(cmd));
522                 cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
523                         info->slave;
524                 cmd.command = ident_command;
525                 if (pio_data_in(ctrl, &cmd, buffer, IDE_SECTOR_SIZE) < 0) {
526                         /* If the command didn't work give up on the drive. */
527                         return 1;
528                 }
529         }
530         if ((drive_info[2] != 0x37C8) &&
531                 (drive_info[2] != 0x738C) &&
532                 (drive_info[2] != 0x8C73) &&
533                 (drive_info[2] != 0xC837) &&
534                 (drive_info[2] != 0x0000)) {
535                 printk_info("Invalid IDE Configuration: %hx\n", drive_info[2]);
536                 return 1;
537         }
538         for(i = 27; i < 47; i++) {
539                 info->model_number[((i-27)<< 1)] = (drive_info[i] >> 8) & 0xff;
540                 info->model_number[((i-27)<< 1)+1] = drive_info[i] & 0xff;
541         }
542         info->model_number[40] = '\0';
543         info->drive_exists = 1;
544
545         /* See if LBA is supported */
546         if (ident_command == IDE_CMD_IDENTIFY_PACKET_DEVICE) {
547                 info->address_mode = ADDRESS_MODE_PACKET;
548                 info->removable = 1; /* XXX */
549         } else if (drive_info[49] & (1 << 9)) {
550                 info->address_mode = ADDRESS_MODE_LBA;
551                 info->sectors = (drive_info[61] << 16) | (drive_info[60]);
552                 printk_debug("LBA mode, sectors=%Ld\n", info->sectors);
553                 /* Enable LBA48 mode if it is present */
554                 if (drive_info[83] & (1 <<10)) {
555                         /* Should LBA48 depend on LBA? */
556                         info->address_mode = ADDRESS_MODE_LBA48;
557                         info->sectors = 
558                                 (((sector_t)drive_info[103]) << 48) |
559                                 (((sector_t)drive_info[102]) << 32) |
560                                 (((sector_t)drive_info[101]) << 16) |
561                                 (((sector_t)drive_info[100]) <<  0);
562                         printk_debug("LBA48 mode, sectors=%Ld\n", info->sectors);
563                 }
564         } else {
565                 info->address_mode = ADDRESS_MODE_CHS;
566                 info->heads = drive_info[3];
567                 info->cylinders = drive_info[1];
568                 info->sectors_per_track = drive_info[6];
569                 info->sectors = 
570                         info->sectors_per_track *
571                         info->heads *
572                         info->cylinders;
573                 printk_debug("CHS mode, sectors_per_track=[%d], heads=[%d], cylinders=[%d]\n",
574                         info->sectors_per_track,
575                         info->heads,
576                         info->cylinders);
577                 printk_debug("sectors=%Ld\n", info->sectors);
578         }
579         /* See if we have a slave */
580         if (!info->slave && (((drive_info[93] >> 14) & 3) == 1)) {
581                 info->slave_absent = !(drive_info[93] & (1 << 5));
582         }
583
584         /* See if we need to put the device in CFA power mode 1 */
585         if ((drive_info[160] & ((1 << 15) | (1 << 13)| (1 << 12))) ==
586                 ((1 << 15) | (1 << 13)| (1 << 12))) {
587                 memset(&cmd, 0, sizeof(cmd));
588                 cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave;
589                 cmd.feature = IDE_FEATURE_CFA_ENABLE_POWER_MODE1;
590                 if (pio_non_data(ctrl, &cmd) < 0) {
591                         /* If I need to power up the drive, and I can't
592                          * give up.
593                          */
594                         printk_info("Cannot power up CFA device\n");
595                         return 1;
596                 }
597         }
598
599         /* Some extra steps for older drives.. */
600         if (info->address_mode != ADDRESS_MODE_PACKET) {
601                 /* Initialize drive parameters
602                  * This is an obsolete command (disappeared as of ATA-6)
603                  * but old drives need it before accessing media. */
604                 memset(&cmd, 0, sizeof(cmd));
605                 cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(drive_info[3] - 1)
606                     | info->slave;
607                 cmd.sector_count = drive_info[6];
608                 cmd.command = IDE_CMD_INITIALIZE_DRIVE_PARAMETERS;
609                 printk_debug("Init device params... ");
610                 if (pio_non_data(ctrl, &cmd) < 0)
611                         printk_debug("failed (ok for newer drives)\n");
612                 else
613                         printk_debug("ok\n");
614         }
615
616         printk_info("hd%c: %s",
617                 'a'+drive,
618                 (info->address_mode==ADDRESS_MODE_CHS) ? "CHS" :
619                 (info->address_mode==ADDRESS_MODE_LBA) ? "LBA" :
620                 (info->address_mode==ADDRESS_MODE_LBA48) ? "LBA48" :
621                 (info->address_mode==ADDRESS_MODE_PACKET) ? "ATAPI" : "???");
622
623         if (info->sectors > (10LL*1000*1000*1000/512))
624                 printk_info(" %uGB", (unsigned) (info->sectors / (1000*1000*1000/512)));
625         else if (info->sectors > (10*1000*1000/512))
626                 printk_info(" %uMB", (unsigned) (info->sectors / (1000*1000/512)));
627         else if (info->sectors > 0)
628                 printk_info(" %uKB", (unsigned) (info->sectors / 2));
629         printk_info(": %s\n", info->model_number);
630
631         return 0;
632 }
633
634 /* Experimental floating bus detection
635  * As Eric mentions, we get stuck when the bus has no drive
636  * and floating high. To avoid this, try some heuristics.
637  * This is based on a paper on Phoenix website. --ts1 */
638 static int ide_bus_floating(struct controller *ctrl)
639 {
640         unsigned long timeout;
641         unsigned char status;
642
643         /* Test 1: if status reads 0xff, probably no device is present
644          * on the bus. Repeat this for 20msec. */
645         timeout = 20;
646         status = 0;
647         do {
648                 /* Take logical OR to avoid chattering */
649                 status |= inb(IDE_REG_STATUS(ctrl));
650                 /* If it makes 0xff, it's possible to be floating, 
651                  * do test2 to ensure. */
652                 if (status == 0xff)
653                         goto test2;
654                 /* If BSY bit is not set, it's harmless to continue probing. */
655                 if ((status & IDE_STATUS_BSY) == 0)
656                         return 0;
657                 udelay(1000);
658         } while (timeout > 0);
659         /* Timed out. Logical ORed status didn't make 0xFF.
660          * We have something there. */
661         return 0;
662
663 test2:
664         /* Test 2: write something to registers, then read back and 
665          * compare. Note that ATA spec inhibits this while BSY is set,
666          * but for many drives this works. This is a confirmation step anyway.
667          */
668         outb(0xaa, ctrl->cmd_base + 2);
669         outb(0x55, ctrl->cmd_base + 3);
670         outb(0xff, ctrl->cmd_base + 4);
671         if (inb(ctrl->cmd_base+2) == 0xaa
672                         && inb(ctrl->cmd_base+3) == 0x55
673                         && inb(ctrl->cmd_base+4) == 0xff) {
674                 /* We have some registers there. 
675                  * Though this does not mean it is not a NIC or something... */
676                 return 0;
677         }
678
679         /* Status port is 0xFF, and other registers are not there.
680          * Most certainly this bus is floating. */
681         printk_info("Detected floating bus\n");
682         return 1;
683 }
684
685 static int init_controller(struct controller *ctrl, int drive, unsigned char *buffer) 
686 {
687         struct harddisk_info *info;
688
689         /* Put the drives ide channel in a know state and wait
690          * for the drives to spinup.  
691          *
692          * In practice IDE disks tend not to respond to commands until
693          * they have spun up.  This makes IDE hard to deal with
694          * immediately after power up, as the delays can be quite
695          * long, so we must be very careful here.
696          *
697          * There are two pathological cases that must be dealt with:
698          *
699          * - The BSY bit not being set while the IDE drives spin up.
700          *   In this cases only a hard coded delay will work.  As
701          *   I have not reproduced it, and this is out of spec for
702          *   IDE drives the work around can be enabled by setting
703          *   BSY_SET_DURING_SPINUP to 0.
704          *
705          * - The BSY bit floats high when no drives are plugged in.
706          *   This case will not be detected except by timing out but
707          *   we avoid the problems by only probing devices we are
708          *   supposed to boot from.  If we don't do the probe we
709          *   will not experience the problem.
710          *
711          * So speed wise I am only slow if the BSY bit is not set
712          * or not reported by the IDE controller during spinup, which
713          * is quite rare.
714          * 
715          */
716 #if !BSY_SET_DURING_SPINUP
717         if (await_ide(timeout, ctrl, IDE_TIMEOUT) < 0) {
718                 return -1;
719         }
720 #endif
721         /* ts1: Try some heuristics to avoid waiting for floating bus */
722         if (ide_bus_floating(ctrl))
723                 return -1;
724
725         if (ide_software_reset(ctrl) < 0) {
726                 return -1;
727         }
728
729         /* Note: I have just done a software reset.  It may be
730          * reasonable to just read the boot time signatures 
731          * off of the drives to see if they are present.
732          *
733          * For now I will go with just sending commands to the drives
734          * and assuming filtering out missing drives by detecting registers
735          * that won't set and commands that fail to execute properly.
736          */
737
738         /* Now initialize the individual drives */
739         info = &harddisk_info[drive];
740         init_drive(info, ctrl, 0, drive, buffer, IDE_CMD_IDENTIFY_DEVICE);
741         if (!info->drive_exists)
742                 init_drive(info, ctrl, 0, drive, buffer,
743                                 IDE_CMD_IDENTIFY_PACKET_DEVICE);
744 #ifdef CHECK_FOR_SLAVES
745         if (info->drive_exists && !info->slave_absent) {
746                 drive++;
747                 info++;
748                 init_drive(info, ctrl, 1, drive, buffer,
749                                 IDE_CMD_IDENTIFY_DEVICE);
750                 if (!info->drive_exists)
751                         init_drive(info, ctrl, 1, drive, buffer,
752                                         IDE_CMD_IDENTIFY_PACKET_DEVICE);
753         }
754 #endif
755
756         return 0;
757 }
758
759 static int
760 atapi_request_sense(struct harddisk_info *info, uint8_t *asc, uint8_t *ascq)
761 {
762         uint8_t packet[12];
763         uint8_t buf[18];
764
765         memset(packet, 0, sizeof packet);
766         packet[0] = 0x03; /* REQUEST SENSE */
767         packet[4] = sizeof buf;
768         if (pio_packet(info, 1, packet, sizeof packet, buf, sizeof buf) != 0)
769                 return -1;
770
771         if (asc)
772                 *asc = buf[12];
773         if (ascq)
774                 *ascq = buf[13];
775         return 0;
776 }
777
778 static int atapi_detect_medium(struct harddisk_info *info)
779 {
780         uint8_t packet[12];
781         uint8_t buf[8];
782         uint32_t block_len, sectors;
783         unsigned long timeout;
784         uint8_t asc, ascq;
785         int in_progress;
786
787         memset(packet, 0, sizeof packet);
788         packet[0] = 0x25; /* READ CAPACITY */
789
790         /* Retry READ CAPACITY for 5 seconds unless MEDIUM NOT PRESENT
791          * is reported by the drive. If the drive reports "IN PROGRESS",
792          * 30 seconds is added. */
793         timeout =  5000;
794         in_progress = 0;
795         while (timeout > 0) {
796                 if (pio_packet(info, 1, packet, sizeof packet, buf, sizeof buf)
797                                 == 0)
798                         goto ok;
799
800                 if (atapi_request_sense(info, &asc, &ascq) == 0) {
801                         if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
802                                 printk_debug("Device reports MEDIUM NOT PRESENT\n");
803                                 return -1;
804                         }
805
806                         if (asc == 0x04 && ascq == 0x01 && !in_progress) {
807                                         /* IN PROGRESS OF BECOMING READY */
808                                 printk_info("Waiting for drive to detect "
809                                                 "the medium... ");
810                                 /* Allow 30 seconds more */
811                                 timeout =  30000;
812                                 in_progress = 1;
813                         }
814                 }
815
816                 udelay(1000);
817         }
818         printk_debug("read capacity failed\n");
819         return -1;
820 ok:
821
822         block_len = (uint32_t) buf[4] << 24
823                 | (uint32_t) buf[5] << 16
824                 | (uint32_t) buf[6] << 8
825                 | (uint32_t) buf[7] << 0;
826         if (block_len != IDE_SECTOR_SIZE && block_len != CDROM_SECTOR_SIZE) {
827                 printk_info("Unsupported sector size %u\n", block_len);
828                 return -1;
829         }
830         info->hw_sector_size = block_len;
831
832         sectors = (uint32_t) buf[0] << 24
833                 | (uint32_t) buf[1] << 16
834                 | (uint32_t) buf[2] << 8
835                 | (uint32_t) buf[3] << 0;
836
837         if (info->hw_sector_size == CDROM_SECTOR_SIZE)
838                 sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
839         if (sectors != info->sectors)
840                 printk_info("%uMB medium detected\n", sectors>>(20-9));
841         info->sectors = sectors;
842         return 0;
843 }
844
845 static int detect_medium(struct harddisk_info *info)
846 {
847         if (info->address_mode == ADDRESS_MODE_PACKET) {
848                 if (atapi_detect_medium(info) != 0)
849                         return -1;
850         } else {
851                 printk_debug("not implemented for non-ATAPI device\n");
852                 return -1;
853         }
854         return 0;
855 }
856
857 static int find_ide_controller_compat(struct controller *ctrl, int index)
858 {
859         if (index >= IDE_MAX_CONTROLLERS)
860                 return -1;
861         ctrl->cmd_base  = ide_base[index];
862         ctrl->ctrl_base = ide_base[index] + IDE_REG_EXTENDED_OFFSET;
863         return 0;
864 }
865
866 static int find_ide_controller(struct controller *ctrl, int ctrl_index)
867 {
868         int pci_index;
869         struct device *dev = 0;
870         unsigned int mask;
871         unsigned int prog_if;
872
873         /* A PCI IDE controller has two channels (pri, sec) */
874         pci_index = ctrl_index >> 1;
875
876         for (;;) {
877                 /* Find a IDE storage class device */
878                 dev = dev_find_class(0x010100, dev);
879                 if (!dev) {
880                         printk_debug("PCI IDE #%d not found\n", pci_index);
881                         return -1;
882                 }
883
884                 if (pci_index-- == 0)
885                         break;
886         }
887
888         prog_if = dev->class & 0xff;
889         printk_debug("found PCI IDE controller %04x:%04x prog_if=%#x\n",
890                         dev->vendor, dev->device, prog_if);
891
892         /* See how this controller is configured */
893         mask = (ctrl_index & 1) ? 4 : 1;
894         printk_debug("%s channel: ", (ctrl_index & 1) ? "secondary" : "primary");
895         if (prog_if & mask) {
896                 printk_debug("native PCI mode\n");
897                 if ((ctrl_index & 1) == 0) {
898                         /* Primary channel */
899                         ctrl->cmd_base = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
900                         ctrl->ctrl_base = pci_read_config32(dev, PCI_BASE_ADDRESS_1);
901                 } else {
902                         /* Secondary channel */
903                         ctrl->cmd_base = pci_read_config32(dev, PCI_BASE_ADDRESS_2);
904                         ctrl->ctrl_base = pci_read_config32(dev, PCI_BASE_ADDRESS_3);
905                 }
906                 ctrl->cmd_base &= ~3;
907                 ctrl->ctrl_base &= ~3;
908         } else {
909                 printk_debug("compatibility mode\n");
910                 if (find_ide_controller_compat(ctrl, ctrl_index) != 0)
911                         return -1;
912         }
913         return 0;
914 }
915
916 int ide_probe(int drive)
917 {
918         struct controller *ctrl;
919         int ctrl_index;
920         struct harddisk_info *info;
921
922         if (drive >= IDE_MAX_DRIVES) {
923                 printk_info("Unsupported drive number\n");
924                 return -1;
925         }
926
927         /* A controller has two drives (master, slave) */
928         ctrl_index = drive >> 1;
929
930         ctrl = &controllers[ctrl_index];
931         if (ctrl->cmd_base == 0) {
932                 if (find_ide_controller(ctrl, ctrl_index) != 0) {
933                         printk_info("IDE channel %d not found\n", ctrl_index);
934                         return -1;
935                 }
936                 if (init_controller(ctrl, drive & ~1, ide_buffer) != 0) {
937                         printk_info("No drive detected on IDE channel %d\n",
938                                         ctrl_index);
939                         return -1;
940                 }
941         }
942         info = &harddisk_info[drive];
943         if (!info->drive_exists) {
944                 printk_info("Drive %d does not exist\n", drive);
945                 return -1;
946         }
947
948         if (info->removable) {
949                 if (detect_medium(info) != 0) {
950                         printk_info("Media detection failed\n");
951                         return -1;
952                 }
953         }
954
955         return 0;
956 }
957
958 /* vim:set sts=8 sw=8: */