summaryrefslogtreecommitdiff
path: root/cpu/amd/geode_lx/gplvsa_ii/legacy/flash.c
blob: 398a5797919241c5016e148a52be370fcc0ecbf0 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* 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 
*/

//******************************************************************************
//*    Routines related to the Flash device.  
//******************************************************************************





#include "VSA2.H"
#include "CHIPSET.H"
#include "SYSMGR.H"
#include "MDD.H"
#include "PCI.H"
#include "GX2.H"
#include "LEGACY.H"
#include "PROTOS.H"

// Function prototypes:
extern void Hide_IDE_Hdr(void);
extern void Hide_Flash_Hdr(void);


// External variables:
extern ULONG MDD_Base;
extern USHORT Flash_Function, IDE_Function;
extern UCHAR Flash_Allocated;



//***********************************************************************
// Returns non-zero if Flash device is enabled
//***********************************************************************
UCHAR FlashIsEnabled(void)
{ UCHAR Pin;

  Pin = (UCHAR)Read_MSR_LO(MDD_Base + MSR_PIN_OPTS) & PIN_OPT_IDE;
  Pin ^= PIN_OPT_IDE;
  return Pin;
}


//***********************************************************************
// Allocates Flash BARs
//***********************************************************************
void Allocate_Flash_BARs(void)
{ ULONG MsrAddr, MsrData[2], Size;
  USHORT Bar;
  UCHAR Resource;
  static UCHAR Flash_LBARs[4] = {
    MSR_LBAR_FLSH0,
    MSR_LBAR_FLSH1,
    MSR_LBAR_FLSH2,
    MSR_LBAR_FLSH3,
  };


  // BARs are only to be allocated once
  if (Flash_Allocated == 0) {

    Flash_Allocated = 1;
    // Determine type/size of PCI BAR for each Flash device
    MsrAddr = MDD_Base;
    for (Bar = BAR0; Bar <= BAR3; Bar += 4) {
      // Read the Flash LBAR
      (UCHAR)MsrAddr = Flash_LBARs[(Bar-BAR0)/4];
      Read_MSR(MsrAddr, MsrData);

      // If LBAR size has not been programmed, then don't configure the PCI BAR
      Size = MsrData[1] & ~(MEM_IO | NOR_NAND | LBAR_EN);
      if (Size == 0x00000000) {
        continue;
      }

      // MMIO or IO resource
      if (MsrData[1] & MEM_IO ) {
        // MMIO mode
        Size &= LBAR_MEM_MASK;
        Resource = RESOURCE_MMIO;
      } else {
        // IO mode
        // Required for proper byte count calculation
        Size |= 0xFFFF0000;
        Resource = RESOURCE_IO;
      }
      // Convert mask to byte count
      Size = ~Size + 1;
      // Allocate BAR
      SYS_ALLOCATE_RESOURCE(Resource, Bar, Size, DEVICE_ID_AMD_FLASH, ID_MDD);
    }
  }
}



//***********************************************************************
// Switches between Flash and IDE headers
//***********************************************************************
void pascal Flash_IDE_Switch(USHORT Function, ULONG Data)
{ ULONG PinOptions, PinMsr, FlashMsr, LBar[2], SavedLBar[2];

  // Switch pins to Flash mode
  PinMsr = MDD_Base + MSR_PIN_OPTS;
  PinOptions = Read_MSR_LO(PinMsr);

  if (Function == Flash_Function) {
    // Currently in Flash mode

    // Switching to IDE mode ?
    if (Data == 0xBEEFDEAD) {

      // Hide the Flash header
      Hide_Flash_Hdr();

      // Switch pins to IDE mode
      PinOptions |= PIN_OPT_IDE;
      Write_MSR_LO(PinMsr, PinOptions);

      // Disconnect the ROM from the DMA pins
      in_8(0x3F6);
    }

  }	else {

    // Currently in IDE mode

    // Switching to Flash mode ?
    if (Data == 0xDEADBEEF) {

      // Hide the IDE header
      Hide_IDE_Hdr();

      // Allocate Flash BARs
      Allocate_Flash_BARs();

      // Enable Flash mode
      PinOptions &= ~PIN_OPT_IDE;
      Write_MSR_LO(PinMsr, PinOptions);

      // Handle Canary circuit

      // Set CS1# to respond to I/O address 22h
      FlashMsr = MDD_Base + MSR_LBAR_FLSH1;
      Read_MSR(FlashMsr, SavedLBar);
      LBar[0] = 0x00000022;
      LBar[1] = 0x0000FFF0 | LBAR_EN;
      Write_MSR(FlashMsr, LBar);

      // Turn Canary circuit on
      in_8(0x22);

      // Restore CS1 LBAR
      Write_MSR(FlashMsr, SavedLBar);
    }
  }
}