OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc3/] [or1ksim/] [peripheral/] [mc.c] - Blame information for rev 1778

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 239 markom
/* mc.c -- Simulation of Memory Controller
2
         Copyright (C) 2001 by Marko Mlinar, markom@opencores.org
3
 
4
         This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
         This program is free software; you can redistribute it and/or modify
7
         it under the terms of the GNU General Public License as published by
8
         the Free Software Foundation; either version 2 of the License, or
9
         (at your option) any later version.
10
 
11
         This program is distributed in the hope that it will be useful,
12
         but WITHOUT ANY WARRANTY; without even the implied warranty of
13
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
14
         GNU General Public License for more details.
15
 
16
         You should have received a copy of the GNU General Public License
17
         along with this program; if not, write to the Free Software
18
         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
*/
20
 
21
/* Enable memory controller, via:
22
  section mc
23
    enable = 1
24
    POC = 0x13243545
25
  end
26
 
27 261 markom
   Limitations:
28
    - memory refresh is not simulated
29 742 ivang
*/
30 239 markom
 
31 1308 phoenix
#include <string.h>
32
 
33 1350 nogj
#include "config.h"
34
 
35
#ifdef HAVE_INTTYPES_H
36
#include <inttypes.h>
37
#endif
38
 
39
#include "port.h"
40
#include "arch.h"
41 239 markom
#include "mc.h"
42
#include "abstract.h"
43 261 markom
#include "sim-config.h"
44 1308 phoenix
#include "debug.h"
45 261 markom
 
46 1490 nogj
DEFAULT_DEBUG_CHANNEL(mc);
47
 
48 1486 nogj
struct mc_area {
49
  struct dev_memarea *mem;
50
  unsigned int cs;
51
  int mc;
52
  struct mc_area *next;
53
};
54 539 simons
 
55 1486 nogj
struct mc {
56 1557 nogj
  uint32_t csr;
57
  uint32_t poc;
58
  uint32_t ba_mask;
59
  uint32_t csc[N_CE];
60
  uint32_t tms[N_CE];
61 1486 nogj
  oraddr_t baseaddr;
62
  int enabled;
63
 
64
  /* Index of this memory controler amongst all the memory controlers */
65
  int index;
66
  /* List of memory devices under this mc's control */
67
  struct mc_area *mc_areas;
68
 
69
  struct mc *next;
70
};
71
 
72
static struct mc *mcs = NULL;
73
 
74
/* List used to temporarily hold memory areas registered with the mc, while the
75
 * mc configureation has not been loaded */
76
static struct mc_area *mc_areas = NULL;
77
 
78 1557 nogj
void set_csc_tms (int cs, uint32_t csc, uint32_t tms, struct mc *mc)
79 1486 nogj
{
80
  struct mc_area *cur = mc->mc_areas;
81 539 simons
 
82 1486 nogj
  while (cur) {
83
    if (cur->cs == cs) {
84
      /* FIXME: No peripheral should _ever_ acess a dev_memarea structure
85
       * directly */
86 1519 nogj
      TRACE("Remapping %"PRIxADDR"-%"PRIxADDR" to %"PRIxADDR"-%"PRIxADDR"\n",
87
            cur->mem->addr_compare,
88
            cur->mem->addr_compare | cur->mem->size_mask,
89
            (csc >> MC_CSC_SEL_OFFSET) << 22,
90
            ((csc >> MC_CSC_SEL_OFFSET) << 22) | cur->mem->size_mask);
91
 
92 1486 nogj
      cur->mem->addr_mask = mc->ba_mask << 22;
93
      cur->mem->addr_compare = ((csc >> MC_CSC_SEL_OFFSET) /* & 0xff*/) << 22;
94
      set_mem_valid(cur->mem, (csc >> MC_CSC_EN_OFFSET) & 0x01);
95 539 simons
 
96
      if ((csc >> MC_CSC_MEMTYPE_OFFSET) && 0x07 == MC_CSC_MEMTYPE_ASYNC) {
97 1486 nogj
        adjust_rw_delay(cur->mem, (tms & 0xff) + ((tms >> 8) & 0x0f),
98
                        ((tms >> 12)  & 0x0f) + ((tms >> 16) & 0x0f) + ((tms >> 20) & 0x3f));
99 539 simons
      } else if ((csc >> MC_CSC_MEMTYPE_OFFSET) && 0x07 == MC_CSC_MEMTYPE_SDRAM) {
100 1486 nogj
        adjust_rw_delay(cur->mem, 3 + ((tms >> 4) & 0x03),
101
                        3 + ((tms >> 4) & 0x03));
102 539 simons
      } else if ((csc >> MC_CSC_MEMTYPE_OFFSET) && 0x07 == MC_CSC_MEMTYPE_SSRAM) {
103 1486 nogj
        adjust_rw_delay(cur->mem, 2, 2);
104 539 simons
      } else if ((csc >> MC_CSC_MEMTYPE_OFFSET) && 0x07 == MC_CSC_MEMTYPE_SYNC) {
105 1486 nogj
        adjust_rw_delay(cur->mem, 2, 2);
106 539 simons
      }
107 543 simons
      return;
108 539 simons
    }
109 1486 nogj
    cur = cur->next;
110 539 simons
  }
111 261 markom
}
112
 
113
/* Set a specific MC register with value. */
114 1359 nogj
void mc_write_word(oraddr_t addr, uint32_t value, void *dat)
115 261 markom
{
116 1373 nogj
    struct mc *mc = dat;
117 261 markom
        int chipsel;
118
 
119 1490 nogj
        TRACE("mc_write_word(%"PRIxADDR",%08"PRIx32")\n", addr, value);
120 261 markom
 
121
        switch (addr) {
122
          case MC_CSR:
123 1373 nogj
            mc->csr = value;
124 261 markom
            break;
125
          case MC_POC:
126 1490 nogj
            WARN("warning: write to MC's POC register!");
127 261 markom
            break;
128
          case MC_BA_MASK:
129 1373 nogj
            mc->ba_mask = value & MC_BA_MASK_VALID;
130 543 simons
      for (chipsel = 0; chipsel < N_CE; chipsel++)
131 1373 nogj
        set_csc_tms (chipsel, mc->csc[chipsel], mc->tms[chipsel], mc);
132 261 markom
            break;
133
                default:
134
                  if (addr >= MC_CSC(0) && addr <= MC_TMS(N_CE - 1)) {
135
                    addr -= MC_CSC(0);
136
                    if ((addr >> 2) & 1)
137 1373 nogj
                      mc->tms[addr >> 3] = value;
138 261 markom
                    else
139 1373 nogj
                      mc->csc[addr >> 3] = value;
140 261 markom
 
141 1373 nogj
                    set_csc_tms (addr >> 3, mc->csc[addr >> 3], mc->tms[addr >> 3], mc);
142 261 markom
                    break;
143
                  } else
144 1490 nogj
                        TRACE("write out of range (addr %"PRIxADDR")\n", addr + mc->baseaddr);
145 261 markom
        }
146
}
147
 
148
/* Read a specific MC register. */
149 1359 nogj
uint32_t mc_read_word(oraddr_t addr, void *dat)
150 261 markom
{
151 1373 nogj
    struct mc *mc = dat;
152 1350 nogj
        uint32_t value = 0;
153 261 markom
 
154 1490 nogj
        TRACE("mc_read_word(%"PRIxADDR")", addr);
155 261 markom
 
156
        switch (addr) {
157
          case MC_CSR:
158 1373 nogj
            value = mc->csr;
159 261 markom
            break;
160
          case MC_POC:
161 1373 nogj
            value = mc->poc;
162 261 markom
            break;
163
          case MC_BA_MASK:
164 1373 nogj
            value = mc->ba_mask;
165 261 markom
            break;
166
                default:
167
                  if (addr >= MC_CSC(0) && addr <= MC_TMS(N_CE - 1)) {
168
                    addr -= MC_CSC(0);
169
                    if ((addr >> 2) & 1)
170 1373 nogj
                      value = mc->tms[addr >> 3];
171 261 markom
                    else
172 1373 nogj
                      value = mc->csc[addr >> 3];
173 261 markom
                  } else
174 1490 nogj
                        TRACE(" read out of range (addr %"PRIxADDR")\n", addr + mc->baseaddr);
175 261 markom
            break;
176
        }
177 1490 nogj
        TRACE(" value(%"PRIx32")\n", value);
178 261 markom
        return value;
179
}
180
 
181
/* Read POC register and init memory controler regs. */
182 1373 nogj
void mc_reset(void *dat)
183 261 markom
{
184 1373 nogj
  struct mc *mc = dat;
185 1486 nogj
  struct mc_area *cur, *prev, *tmp;
186 543 simons
 
187 1373 nogj
  PRINTF("Resetting memory controller.\n");
188 261 markom
 
189 1373 nogj
  memset(mc->csc, 0, sizeof(mc->csc));
190
  memset(mc->tms, 0, sizeof(mc->tms));
191 539 simons
 
192 1373 nogj
  mc->csr = 0;
193
  mc->ba_mask = 0;
194 539 simons
 
195 1373 nogj
  /* Set CS0 */
196
  mc->csc[0] = (((mc->poc & 0x0c) >> 2) << MC_CSC_MEMTYPE_OFFSET) | ((mc->poc & 0x03) << MC_CSC_BW_OFFSET) | 1;
197 539 simons
 
198 1373 nogj
  if ((mc->csc[0] >> MC_CSC_MEMTYPE_OFFSET) && 0x07 == MC_CSC_MEMTYPE_ASYNC) {
199
    mc->tms[0] = MC_TMS_ASYNC_VALID;
200
  } else if ((mc->csc[0] >> MC_CSC_MEMTYPE_OFFSET) && 0x07 == MC_CSC_MEMTYPE_SDRAM) {
201
    mc->tms[0] = MC_TMS_SDRAM_VALID;
202
  } else if ((mc->csc[0] >> MC_CSC_MEMTYPE_OFFSET) && 0x07 == MC_CSC_MEMTYPE_SSRAM) {
203
    mc->tms[0] = MC_TMS_SSRAM_VALID;
204
  } else if ((mc->csc[0] >> MC_CSC_MEMTYPE_OFFSET) && 0x07 == MC_CSC_MEMTYPE_SYNC) {
205
    mc->tms[0] = MC_TMS_SYNC_VALID;
206
  }
207 543 simons
 
208 1486 nogj
  /* Grab control over all the devices we are destined to control */
209
  cur = mc_areas;
210
  prev = NULL;
211
  while (cur) {
212
    if (cur->mc == mc->index) {
213
      if (prev) prev->next = cur->next;
214
      else mc_areas = cur->next;
215
      prev = cur;
216
      tmp = cur->next;
217
      cur->next = mc->mc_areas;
218
      mc->mc_areas = cur;
219
      cur = tmp;
220
    } else {
221
      prev = cur;
222
      cur = cur->next;
223
    }
224 261 markom
  }
225
 
226 1486 nogj
  for (cur = mc->mc_areas; cur; cur = cur->next)
227
    set_mem_valid(cur->mem, 0);
228
 
229 1373 nogj
  set_csc_tms (0, mc->csc[0], mc->tms[0], mc);
230 261 markom
}
231 742 ivang
 
232 1373 nogj
void mc_status(void *dat)
233 742 ivang
{
234 1373 nogj
    struct mc *mc = dat;
235 742 ivang
    int i;
236
 
237 1373 nogj
    PRINTF( "\nMemory Controller at 0x%"PRIxADDR":\n", mc->baseaddr );
238 1557 nogj
    PRINTF( "POC: 0x%"PRIx32"\n", mc->poc );
239
    PRINTF( "BAS: 0x%"PRIx32"\n", mc->ba_mask );
240
    PRINTF( "CSR: 0x%"PRIx32"\n", mc->csr );
241 742 ivang
 
242
    for (i=0; i<N_CE; i++) {
243 1557 nogj
        PRINTF( "CE %02d -  CSC: 0x%"PRIx32"  TMS: 0x%"PRIx32"\n", i,
244
                mc->csc[i], mc->tms[i]);
245 742 ivang
    }
246
}
247 1358 nogj
 
248 1486 nogj
/*--------------------------------------------[ Peripheral<->MC interface ]---*/
249
/* Registers some memory to be under the memory controllers control */
250
void mc_reg_mem_area(struct dev_memarea *mem, unsigned int cs, int mc)
251
{
252
  struct mc_area *new;
253
 
254
  if(!(new = malloc(sizeof(struct mc_area)))) {
255
    fprintf(stderr, "Out-of-memory\n");
256
    exit(-1);
257
  }
258
  new->cs = cs;
259
  new->mem = mem;
260
  new->mc = mc;
261
 
262
  new->next = mc_areas;
263
  mc_areas = new;
264
}
265
 
266
/*-----------------------------------------------------[ MC configuration ]---*/
267 1358 nogj
void mc_enabled(union param_val val, void *dat)
268
{
269 1373 nogj
  struct mc *mc = dat;
270
  mc->enabled = val.int_val;
271 1358 nogj
}
272
 
273
void mc_baseaddr(union param_val val, void *dat)
274
{
275 1373 nogj
  struct mc *mc = dat;
276
  mc->baseaddr = val.addr_val;
277 1358 nogj
}
278
 
279
void mc_POC(union param_val val, void *dat)
280
{
281 1373 nogj
  struct mc *mc = dat;
282
  mc->poc = val.int_val;
283 1358 nogj
}
284
 
285 1486 nogj
void mc_index(union param_val val, void *dat)
286
{
287
  struct mc *mc = dat;
288
  mc->index = val.int_val;
289
}
290
 
291 1373 nogj
void *mc_sec_start(void)
292
{
293
  struct mc *new = malloc(sizeof(struct mc));
294
 
295
  if(!new) {
296
    fprintf(stderr, "Peripheral MC: Run out of memory\n");
297
    exit(-1);
298
  }
299
 
300 1486 nogj
  new->index = 0;
301 1373 nogj
  new->enabled = 0;
302 1486 nogj
  new->mc_areas = NULL;
303 1373 nogj
 
304
  return new;
305
}
306
 
307
void mc_sec_end(void *dat)
308
{
309
  struct mc *mc = dat;
310 1486 nogj
  struct mem_ops ops;
311 1373 nogj
 
312 1461 nogj
  if(!mc->enabled) {
313
    free(dat);
314
    return;
315 1373 nogj
  }
316 1461 nogj
 
317 1486 nogj
  /* FIXME: Check to see that the index given to this mc is unique */
318
 
319
  mc->next = mcs;
320
  mcs = mc;
321
 
322
  memset(&ops, 0, sizeof(struct mem_ops));
323
 
324
  ops.readfunc32 = mc_read_word;
325
  ops.writefunc32 = mc_write_word;
326
  ops.write_dat32 = dat;
327
  ops.read_dat32 = dat;
328
 
329
  /* FIXME: Correct delays? */
330
  ops.delayr = 2;
331
  ops.delayw = 2;
332
 
333
  reg_mem_area(mc->baseaddr, MC_ADDR_SPACE, 1, &ops);
334 1461 nogj
  reg_sim_reset(mc_reset, dat);
335
  reg_sim_stat(mc_status, dat);
336 1373 nogj
}
337
 
338 1358 nogj
void reg_mc_sec(void)
339
{
340 1373 nogj
  struct config_section *sec = reg_config_sec("mc", mc_sec_start, mc_sec_end);
341 1358 nogj
 
342
  reg_config_param(sec, "enabled", paramt_int, mc_enabled);
343
  reg_config_param(sec, "baseaddr", paramt_addr, mc_baseaddr);
344
  reg_config_param(sec, "POC", paramt_int, mc_POC);
345 1486 nogj
  reg_config_param(sec, "index", paramt_int, mc_index);
346 1358 nogj
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.