summaryrefslogtreecommitdiffstats
path: root/bsps/powerpc/mvme5500/net/if_1GHz/pci_map.c
blob: 26b143ea1a58ae923e17677703b9d6ec9e99a2eb (plain) (blame)
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
/*	$NetBSD: pci_map.c,v 1.12 2002/05/30 12:06:43 drochner Exp $	*/

/*-
 * Copyright (c) 2004, 2005  Brookhaven National  Laboratory
 *               S. Kate Feng <feng1@bnl.gov>
 *
 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Charles M. Hannum; by William R. Studenmund; by Jason R. Thorpe.
 *
 */

/*
 * PCI device mapping.
 */

#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <rtems/rtems/types.h>

#include <bsp/pci.h>
#include <bsp/pcireg.h>

extern int pci_get_capability(int b, int d, int f, int capid,int *offset,uint32_t *value);
extern int pci_mem_find(int b, int d, int f, int reg, unsigned *basep,unsigned *sizep);
extern int pci_io_find(int b, int d, int f, int reg,unsigned *basep,unsigned *sizep);

int pci_io_find(int b, int d, int f, int reg,unsigned *basep,unsigned *sizep)
{
  uint32_t address, mask;

  if (reg < PCI_MAPREG_START ||
#if 0
	    /*
	     * Can't do this check; some devices have mapping registers
	     * way out in left field.
	     */
	    reg >= PCI_MAPREG_END ||
#endif
	    (reg & 3))
		rtems_panic("pci_io_find: bad request");

  /*
   * Section 6.2.5.1, `Address Maps', tells us that:
   *
   * 1) The builtin software should have already mapped the device in a
   * reasonable way.
   *
   * 2) A device which wants 2^n bytes of memory will hardwire the bottom
   * n bits of the address to 0.  As recommended, we write all 1s and see
   * what we get back.
   */
  pci_read_config_dword(b,d,f,reg, &address);
  if ( !(address & PCI_MAPREG_TYPE_IO)) return(1);
  pci_write_config_dword(b,d,f,reg, 0xffffffff);
  pci_read_config_dword(b,d,f,reg,&mask);
  pci_write_config_dword(b,d,f,reg, address);

  if ( (*sizep = PCI_MAPREG_IO_SIZE(mask))== 0) {
     printk("pci_io_find: void region\n");
     return(1);
  }
  *basep = PCI_MAPREG_IO_ADDR(address);
  return(0);
}

int pci_mem_find(int b, int d, int f, int reg, unsigned *basep,unsigned *sizep)
{
  uint32_t address, mask;

  if (reg < PCI_MAPREG_START ||
#if 0
	    /*
	     * Can't do this check; some devices have mapping registers
	     * way out in left field.
	     */
	    reg >= PCI_MAPREG_END ||
#endif
	    (reg & 3))
      rtems_panic("pci_mem_find: bad request");

  pci_read_config_dword(b,d,f,reg, &address);
  if (address & PCI_MAPREG_TYPE_IO) {
     printk("pci_mem_find: expected type mem, found I/O\n");
     return(1);
  }

  /*
   * Section 6.2.5.1, `Address Maps', tells us that:
   *
   * 1) The builtin software should have already mapped the device in a
   * reasonable way.
   *
   * 2) A device which wants 2^n bytes of memory will hardwire the bottom
   * n bits of the address to 0.  As recommended, we write all 1s and see
   * what we get back.
   */
  pci_write_config_dword(b,d,f,reg, 0xffffffff);
  pci_read_config_dword(b,d,f,reg,&mask);
  pci_write_config_dword(b,d,f,reg, address);
  if ( (*sizep = PCI_MAPREG_MEM_SIZE(mask))== 0) {
     printk("pci_io_find: void region\n");
     return (1);
  }
  *basep = PCI_MAPREG_MEM_ADDR(address);
  return(0);
}

int pci_get_capability(int b, int d, int f, int capid,int *offset,uint32_t *value)
{
  uint32_t reg, ofs;

  /*  i82544EI PCI_CAPLISTPTR_REG */
  pci_read_config_dword(b,d,f,PCI_CAPLISTPTR_REG, &reg);
  ofs = PCI_CAPLIST_PTR(reg);
  while (ofs != 0) {
#ifdef DIAGNOSTIC
    if ((ofs & 3) || (ofs < 0x40))
       panic("pci_get_capability");
#endif
    pci_read_config_dword(b,d,f,ofs, &reg);
    if (PCI_CAPLIST_CAP(reg) == capid) {
       if (offset)
	   *offset = ofs;
       if (value)
	   *value = reg;
       return (1);
    }
    ofs = PCI_CAPLIST_NEXT(reg);
  }
  return (0);
}