summaryrefslogtreecommitdiff
path: root/cpu/amd/geode_lx/gplvsa_ii/legacy/ide.c
blob: 9e6d00c0a72d5c9f5f0ce17a342dc9b261abf1eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2006-2008 Advanced Micro Devices,Inc. ("AMD").
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.

* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA 
*/

//*	 Function:                                                          *
//*     This file contains code for power-managing the ATA drives.


#include "vsa2.h"
#include "isa.h"

extern UCHAR pascal in_8(USHORT);
extern void pascal out_8(USHORT, UCHAR);


// NOTE: Win95 touches CD-ROMs every 4.2 seconds for AutoDetect

#define HDD0		1
#define HDD1		2
#define HDD2		4 
#define HDD3		8

#define IDE_BSY		0x80		// Status register
#define IDE_RDY		0x40		// Status register
#define IDE_MSK		IDE_BSY+IDE_RDY

#define	IDE_CMD_SPINDOWN			0xE0	// Spindown Command
#define	IDE_CMD_SPINUP				0xE1	// Spinup   Command
#define	IDE_CMD_CHECK_POWER_MODE	0xE5	// Get Power Management State Command
#define IDE_CMD_SLEEP				0xE6

UCHAR DrivesPresent = 0, DrvHd;


//*************************************************************************
// Issues specified command to one or more drive units
//*************************************************************************
void pascal IDE_Command(UCHAR DriveUnit, UCHAR Command)
{ UCHAR DriveSelect, Mask = 1;
  USHORT IO_Port;
  ULONG Timeout; 

  // Only spindown drives that are present
  DriveUnit &= DrivesPresent;

  while (DriveUnit) {

    // Is drive present ?
    if (DriveUnit & Mask) {

      // Yes, determine I/O port
      IO_Port = PRIMARY_IDE;
      if (Mask & (HDD2+HDD3)) {
        IO_Port = SECONDARY_IDE;
      }

      // Determine master/slave
      DriveSelect = 0xA0;					// Master drive
      if (Mask & (HDD1+HDD3)) {
        DriveSelect = 0xB0;					// Slave drive
      }

      // Wait for IDE channel READY
      for (Timeout=0xFFFFF; Timeout; Timeout--) {
        if ((IDE_MSK & in_8(IO_Port+1)) == IDE_RDY)
          break;
      }

      DrvHd = in_8(IO_Port);     	 		// Save Drive/Head register

      out_8(IO_Port, DriveSelect);			// Select drive to spin down
      out_8(IO_Port+1, Command);			// Issue command to the the drive

	  for (Timeout=0xFFFFF; Timeout; Timeout--) {
		if (!(in_8(IO_Port+1) & IDE_BSY))
			break;
	  }

	  in_8(IO_Port+1);						// Clear IRQ

      DriveUnit &= ~Mask;
    }

    // Next drive
    Mask <<= 1;
  }
}

//*************************************************************************
// Spins down specified drive(s)
//*************************************************************************
void pascal SpinDownHardDrive(UCHAR DriveUnit)
{
  IDE_Command(DriveUnit, IDE_CMD_SPINDOWN);
}


//*************************************************************************
// Puts drive(s) into sleep mode
//*************************************************************************
void pascal DriveSleep(UCHAR DriveUnit)
{
//  IDE_Command(DriveUnit, IDE_CMD_SLEEP);
}

//*************************************************************************
// Determines what hard drives are present
// Input:  I/O address of IDE Drive/Head register
//*************************************************************************
UCHAR Check_IDE_Channel(USHORT IDE_DrvHd)
{ UCHAR HDD_Status, DrivesPresent=0;

  DrvHd = in_8(IDE_DrvHd);					// Save Drive/Head register

  out_8(IDE_DrvHd, (UCHAR)(DrvHd & ~0x10));	// Select master drive
  HDD_Status = in_8(IDE_DrvHd);
  if (in_8(IDE_DrvHd+1) == 0x50) {			// Is drive present ?
    DrivesPresent |= HDD0;

    out_8(IDE_DrvHd, (UCHAR)(DrvHd | 0x10));	// Select slave drive
    HDD_Status = in_8(IDE_DrvHd);
    if (in_8(IDE_DrvHd+1) == 0x50) {			// Is drive present ?
      DrivesPresent |= HDD1;
    }
  }

  // Restore Drive/Head register
  out_8(IDE_DrvHd, DrvHd);
  return DrivesPresent;
}


//*************************************************************************
// Determines what hard drives are present
// Input:  I/O address of IDE Drive/Head register
//*************************************************************************
UCHAR GetDrivesPresent(void)
{
  DrivesPresent  = Check_IDE_Channel(PRIMARY_IDE);
  DrivesPresent |= Check_IDE_Channel(SECONDARY_IDE) << 2;

  return DrivesPresent;
}