35
35
* Pre-processor Definitions
36
36
****************************************************************************/
37
37
38
- #define PCI_CFG_ADDR 0xcf8
39
- #define PCI_DATA_ADDR 0xcfc
40
- #define PCI_CFG_EN (1 << 31)
38
+ #define PCI_CFG_ADDR 0xcf8
39
+ #define PCI_DATA_ADDR 0xcfc
40
+ #define PCI_CFG_EN (1 << 31)
41
41
42
42
/****************************************************************************
43
43
* Private Functions Definitions
44
44
****************************************************************************/
45
45
46
- static void x86_64_pci_cfg_write (struct pci_dev_s * dev , int reg ,
47
- uint32_t val , int width );
48
- static uint32_t x86_64_pci_cfg_read (struct pci_dev_s * dev , int reg ,
49
- int width );
50
- static int x86_64_pci_map_bar (uint64_t addr , uint64_t len );
51
- static uint32_t x86_64_pci_io_read (const volatile void * addr , int width );
52
- static void x86_64_pci_io_write (const volatile void * addr , uint32_t val ,
53
- int width );
46
+ static int x86_64_pci_write (struct pci_bus_s * bus , unsigned int devfn ,
47
+ int where , int size , uint32_t val );
48
+ static int x86_64_pci_read (struct pci_bus_s * bus , unsigned int devfn ,
49
+ int where , int size , uint32_t * val );
50
+ static uintptr_t x86_64_pci_map (struct pci_bus_s * bus , uintptr_t start ,
51
+ uintptr_t end );
52
+ static int x86_64_pci_read_io (struct pci_bus_s * bus , uintptr_t addr ,
53
+ int size , uint32_t * val );
54
+ static int x86_64_pci_write_io (struct pci_bus_s * bus , uintptr_t addr ,
55
+ int size , uint32_t val );
54
56
55
57
/****************************************************************************
56
58
* Private Data
57
59
****************************************************************************/
58
60
59
- static const struct pci_bus_ops_s g_x86_64_pci_bus_ops =
61
+ static const struct pci_ops_s g_x86_64_pci_ops =
60
62
{
61
- .pci_cfg_write = x86_64_pci_cfg_write ,
62
- .pci_cfg_read = x86_64_pci_cfg_read ,
63
- .pci_map_bar = x86_64_pci_map_bar ,
64
- .pci_io_read = x86_64_pci_io_read ,
65
- .pci_io_write = x86_64_pci_io_write ,
63
+ .write = x86_64_pci_write ,
64
+ .read = x86_64_pci_read ,
65
+ .map = x86_64_pci_map ,
66
+ .read_io = x86_64_pci_read_io ,
67
+ .write_io = x86_64_pci_write_io ,
66
68
};
67
69
68
- static struct pci_bus_s g_x86_64_pci_bus =
70
+ static struct pci_controller_s g_x86_64_pci =
69
71
{
70
- .ops = & g_x86_64_pci_bus_ops ,
72
+ .ops = & g_x86_64_pci_ops
71
73
};
72
74
73
75
/****************************************************************************
74
76
* Private Functions
75
77
****************************************************************************/
76
78
77
79
/****************************************************************************
78
- * Name: x86_64_pci_cfg_write
80
+ * Name: x86_64_pci_write
79
81
*
80
82
* Description:
81
- * Write 8, 16, 32, 64 bits data to PCI-E configuration space of device
83
+ * Write 8, 16, 32, 64 bits data to PCI configuration space of device
82
84
* specified by dev
83
85
*
84
86
* Input Parameters:
85
- * bdf - Device private data
86
- * reg - A pointer to the read-only buffer of data to be written
87
- * size - The number of bytes to send from the buffer
87
+ * bus - Bus that PCI device resides
88
+ * devfn - The device and function bit field of bdf
89
+ * where - Offset in the specify PCI device cfg address space
90
+ * size - The number of bytes to send from the buffer
91
+ * val - The value to write
88
92
*
89
93
* Returned Value:
90
94
* 0: success, <0: A negated errno
91
95
*
92
96
****************************************************************************/
93
97
94
- static void x86_64_pci_cfg_write (struct pci_dev_s * dev , int reg ,
95
- uint32_t val , int width )
98
+ static int x86_64_pci_write (struct pci_bus_s * bus , unsigned int devfn ,
99
+ int where , int size , uint32_t val )
96
100
{
97
- uint8_t offset_mask = (4 - width );
101
+ uint8_t offset_mask = (4 - size );
98
102
99
- outl (PCI_CFG_EN | (dev -> bdf << 8 ) | reg , PCI_CFG_ADDR );
100
- switch (width )
103
+ outl (PCI_CFG_EN | (bus -> number << 16 ) | (devfn << 8 ) | where ,
104
+ PCI_CFG_ADDR );
105
+
106
+ switch (size )
101
107
{
102
108
case 1 :
103
- outb (val , PCI_DATA_ADDR + (reg & offset_mask ));
104
- return ;
109
+ outb (val , PCI_DATA_ADDR + (where & offset_mask ));
110
+ break ;
105
111
case 2 :
106
- outw (val , PCI_DATA_ADDR + (reg & offset_mask ));
107
- return ;
112
+ outw (val , PCI_DATA_ADDR + (where & offset_mask ));
113
+ break ;
108
114
case 4 :
109
115
outl (val , PCI_DATA_ADDR );
110
- return ;
116
+ break ;
111
117
default :
112
- pcierr ("Invalid cfg write width %d\n" , width );
118
+ pcierr ("Invalid cfg write size %d\n" , size );
119
+ return - EINVAL ;
113
120
}
121
+
122
+ return 0 ;
114
123
}
115
124
116
125
/****************************************************************************
117
- * Name: x86_64_pci_cfg_read
126
+ * Name: x86_64_pci_read
118
127
*
119
128
* Description:
120
- * Read 8, 16, 32, 64 bits data from PCI-E configuration space of device
129
+ * Read 8, 16, 32, 64 bits data from PCI configuration space of device
121
130
* specified by dev
122
131
*
123
132
* Input Parameters:
124
- * dev - Device private data
125
- * buffer - A pointer to a buffer to receive the data from the device
133
+ * bus - Bus that PCI device resides
134
+ * devfn - The device and function bit field of bdf
135
+ * where - Offset in the specify PCI device cfg address space
126
136
* size - The requested number of bytes to be read
137
+ * val - A pointer to a buffer to receive the data from the device
127
138
*
128
139
* Returned Value:
129
140
* 0: success, <0: A negated errno
130
141
*
131
142
****************************************************************************/
132
143
133
- static uint32_t x86_64_pci_cfg_read (struct pci_dev_s * dev , int reg ,
134
- int width )
144
+ static int x86_64_pci_read (struct pci_bus_s * bus , unsigned int devfn ,
145
+ int where , int size , uint32_t * val )
135
146
{
136
- uint32_t ret ;
137
- uint8_t offset_mask = 4 - width ;
147
+ uint8_t offset_mask = 4 - size ;
138
148
139
- outl (PCI_CFG_EN | (dev -> bdf << 8 ) | reg , PCI_CFG_ADDR );
149
+ outl (PCI_CFG_EN | (bus -> number << 16 ) | (devfn << 8 ) | where ,
150
+ PCI_CFG_ADDR );
140
151
141
- switch (width )
152
+ switch (size )
142
153
{
143
154
case 1 :
144
- ret = inb (PCI_DATA_ADDR + (reg & offset_mask ));
145
- return ret ;
146
-
155
+ * val = inb (PCI_DATA_ADDR + (where & offset_mask ));
156
+ break ;
147
157
case 2 :
148
- ret = inw (PCI_DATA_ADDR + (reg & offset_mask ));
149
- return ret ;
158
+ * val = inw (PCI_DATA_ADDR + (where & offset_mask ));
159
+ break ;
150
160
case 4 :
151
- ret = inl (PCI_DATA_ADDR );
152
- return ret ;
161
+ * val = inl (PCI_DATA_ADDR );
162
+ break ;
153
163
default :
154
- pcierr ("Invalid cfg read width %d\n" , width );
164
+ * val = 0 ;
165
+ pcierr ("Invalid cfg read size %d\n" , size );
166
+ return - EINVAL ;
155
167
}
156
168
157
- return 0 ;
169
+ return OK ;
158
170
}
159
171
160
- static uint32_t x86_64_pci_io_read (const volatile void * addr , int width )
172
+ /****************************************************************************
173
+ * Name: x86_64_pci_read_io
174
+ *
175
+ * Description:
176
+ * Read 8, 16, 32, 64 bits data from PCI io address space of x86 64 device
177
+ *
178
+ * Input Parameters:
179
+ * bus - Bus that PCI device resides
180
+ * addr - The address to received data
181
+ * size - The requested number of bytes to be read
182
+ * val - A pointer to a buffer to receive the data from the device
183
+ *
184
+ * Returned Value:
185
+ * 0: success, <0: A negated errno
186
+ *
187
+ ****************************************************************************/
188
+
189
+ static int x86_64_pci_read_io (struct pci_bus_s * bus , uintptr_t addr ,
190
+ int size , uint32_t * val )
161
191
{
162
- uint16_t portaddr = (uint16_t )( intptr_t ) addr ;
192
+ uint16_t portaddr = (uint16_t )addr ;
163
193
164
- switch (width )
194
+ switch (size )
165
195
{
166
196
case 1 :
167
- return (uint32_t )inb (portaddr );
197
+ * val = (uint32_t )inb (portaddr );
198
+ break ;
168
199
case 2 :
169
- return (uint32_t )inw (portaddr );
200
+ * val = (uint32_t )inw (portaddr );
201
+ break ;
170
202
case 4 :
171
- return (uint32_t )inl (portaddr );
203
+ * val = (uint32_t )inl (portaddr );
204
+ break ;
172
205
default :
173
- pcierr ("Invalid read width %d\n" , width );
206
+ * val = 0 ;
207
+ pcierr ("Invalid read size %d\n" , size );
174
208
DEBUGPANIC ();
209
+ return - EINVAL ;
175
210
}
176
211
177
- return 0 ;
212
+ return OK ;
178
213
}
179
214
180
- static void x86_64_pci_io_write (const volatile void * addr , uint32_t val ,
181
- int width )
215
+ /****************************************************************************
216
+ * Name: x86_64_pci_write_io
217
+ *
218
+ * Description:
219
+ * Write 8, 16, 32, 64 bits data to PCI io address space of x86 64 device
220
+ *
221
+ * Input Parameters:
222
+ * bus - Bus that PCI device resides
223
+ * addr - The address to write data
224
+ * size - The requested number of bytes to be write
225
+ * val - The value to write
226
+ *
227
+ * Returned Value:
228
+ * 0: success, <0: A negated errno
229
+ *
230
+ ****************************************************************************/
231
+
232
+ static int x86_64_pci_write_io (struct pci_bus_s * bus , uintptr_t addr ,
233
+ int size , uint32_t val )
182
234
{
183
- uint16_t portaddr = (uint16_t )( intptr_t ) addr ;
235
+ uint16_t portaddr = (uint16_t )addr ;
184
236
185
- switch (width )
237
+ switch (size )
186
238
{
187
239
case 1 :
188
240
outb ((uint8_t )val , portaddr );
189
- return ;
241
+ break ;
190
242
case 2 :
191
243
outw ((uint16_t )val , portaddr );
192
- return ;
244
+ break ;
193
245
case 4 :
194
246
outl ((uint32_t )val , portaddr );
195
- return ;
247
+ break ;
196
248
default :
197
- pcierr ("Invalid write width %d\n" , width );
249
+ pcierr ("Invalid write size %d\n" , size );
198
250
DEBUGPANIC ();
251
+ return - EINVAL ;
199
252
}
253
+
254
+ return OK ;
200
255
}
201
256
202
- static int x86_64_pci_map_bar (uint64_t addr , uint64_t len )
257
+ /****************************************************************************
258
+ * Name: x86_64_pci_map
259
+ *
260
+ * Description:
261
+ * Map a memory region
262
+ *
263
+ * Input Parameters:
264
+ * bus - Bus that PCI device resides
265
+ * start - The start address to map
266
+ * end - The end address to map
267
+ *
268
+ * Returned Value:
269
+ * >0: success, 0: A positive value errno
270
+ *
271
+ ****************************************************************************/
272
+
273
+ static uintptr_t x86_64_pci_map (struct pci_bus_s * bus , uintptr_t start ,
274
+ uintptr_t end )
203
275
{
204
- up_map_region ((void * )(uintptr_t )addr , len ,
205
- X86_PAGE_WR | X86_PAGE_PRESENT | X86_PAGE_NOCACHE | X86_PAGE_GLOBAL );
206
- return OK ;
276
+ int ret ;
277
+
278
+ ret = up_map_region ((void * )start , end - start + 1 , X86_PAGE_WR |
279
+ X86_PAGE_PRESENT | X86_PAGE_NOCACHE | X86_PAGE_GLOBAL );
280
+
281
+ return ret < 0 ? 0 : start ;
207
282
}
208
283
209
284
/****************************************************************************
@@ -214,12 +289,11 @@ static int x86_64_pci_map_bar(uint64_t addr, uint64_t len)
214
289
* Name: x86_64_pci_init
215
290
*
216
291
* Description:
217
- * Initialize the PCI-E bus
292
+ * Initialize the PCI bus
218
293
*
219
294
****************************************************************************/
220
295
221
296
void x86_64_pci_init (void )
222
297
{
223
- pciinfo ("Initializing PCI Bus\n" );
224
- pci_initialize (& g_x86_64_pci_bus );
298
+ pci_register_controller (& g_x86_64_pci );
225
299
}
0 commit comments