1 /******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation
3 * Copyright (c) 2009 Pattrick Hueper <phueper@hueper.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * IBM Corporation - initial implementation
33 *****************************************************************************/
36 #include "compat/rtas.h"
37 #include "compat/time.h"
40 #include <x86emu/x86emu.h>
41 #include <device/oprom/include/io.h>
44 #include <device/pci.h>
45 #include <device/pci_ops.h>
46 #include <device/resource.h>
50 #if CONFIG(YABEL_DIRECTHW)
51 u8
my_inb(X86EMU_pioAddr addr
)
56 DEBUG_PRINTF_IO("inb(0x%04x) = 0x%02x\n", addr
, val
);
61 u16
my_inw(X86EMU_pioAddr addr
)
66 DEBUG_PRINTF_IO("inw(0x%04x) = 0x%04x\n", addr
, val
);
71 u32
my_inl(X86EMU_pioAddr addr
)
76 DEBUG_PRINTF_IO("inl(0x%04x) = 0x%08x\n", addr
, val
);
81 void my_outb(X86EMU_pioAddr addr
, u8 val
)
83 DEBUG_PRINTF_IO("outb(0x%02x, 0x%04x)\n", val
, addr
);
87 void my_outw(X86EMU_pioAddr addr
, u16 val
)
89 DEBUG_PRINTF_IO("outw(0x%04x, 0x%04x)\n", val
, addr
);
93 void my_outl(X86EMU_pioAddr addr
, u32 val
)
95 DEBUG_PRINTF_IO("outl(0x%08x, 0x%04x)\n", val
, addr
);
102 read_io(void *addr
, size_t sz
)
105 /* since we are using inb instructions, we need the port number as 16bit value */
106 u16 port
= (u16
)(uintptr_t) addr
;
126 write_io(void *addr
, unsigned int value
, size_t sz
)
128 u16 port
= (u16
)(uintptr_t) addr
;
130 /* since we are using inb instructions, we need the port number as 16bit value */
147 u32
pci_cfg_read(X86EMU_pioAddr addr
, u8 size
);
148 void pci_cfg_write(X86EMU_pioAddr addr
, u32 val
, u8 size
);
149 u8
handle_port_61h(void);
152 my_inb(X86EMU_pioAddr addr
)
155 unsigned long translated_addr
= addr
;
156 u8 translated
= biosemu_dev_translate_address(IORESOURCE_IO
, &translated_addr
);
157 if (translated
!= 0) {
158 //translation successful, access Device I/O (BAR or Legacy...)
159 DEBUG_PRINTF_IO("%s(%x): access to Device I/O\n", __func__
,
161 //DEBUG_PRINTF_IO("%s(%04x): translated_addr: %llx\n", __func__, addr, translated_addr);
162 rval
= read_io((void *)translated_addr
, 1);
163 DEBUG_PRINTF_IO("%s(%04x) Device I/O --> %02x\n", __func__
,
169 //8254 KB Controller / Timer Port
170 // rval = handle_port_61h();
172 //DEBUG_PRINTF_IO("%s(%04x) KB / Timer Port B --> %02x\n", __func__, addr, rval);
179 // PCI Config Mechanism 1 Ports
180 return (u8
) pci_cfg_read(addr
, 1);
183 CHECK_DBG(DEBUG_INTR
) {
186 M
.x86
.debug
&= ~DEBUG_DECODE_NOPRINT_F
;
190 ("%s(%04x) reading from bios_device.io_buffer\n",
192 rval
= *((u8
*) (bios_device
.io_buffer
+ addr
));
193 DEBUG_PRINTF_IO("%s(%04x) I/O Buffer --> %02x\n",
194 __func__
, addr
, rval
);
202 my_inw(X86EMU_pioAddr addr
)
204 unsigned long translated_addr
= addr
;
205 u8 translated
= biosemu_dev_translate_address(IORESOURCE_IO
, &translated_addr
);
206 if (translated
!= 0) {
207 //translation successful, access Device I/O (BAR or Legacy...)
208 DEBUG_PRINTF_IO("%s(%x): access to Device I/O\n", __func__
,
210 //DEBUG_PRINTF_IO("%s(%04x): translated_addr: %llx\n", __func__, addr, translated_addr);
212 if ((translated_addr
& (u64
) 0x1) == 0) {
213 // 16 bit aligned access...
214 u16 tempval
= read_io((void *)translated_addr
, 2);
215 //little endian conversion
216 rval
= in16le((void *) &tempval
);
218 // unaligned access, read single bytes, little-endian
219 rval
= (read_io((void *)translated_addr
, 1) << 8)
220 | (read_io((void *)(translated_addr
+ 1), 1));
222 DEBUG_PRINTF_IO("%s(%04x) Device I/O --> %04x\n", __func__
,
229 //PCI Config Mechanism 1
230 return (u16
) pci_cfg_read(addr
, 2);
234 ("%s(%04x) reading from bios_device.io_buffer\n",
237 in16le((void *) bios_device
.io_buffer
+ addr
);
238 DEBUG_PRINTF_IO("%s(%04x) I/O Buffer --> %04x\n",
239 __func__
, addr
, rval
);
247 my_inl(X86EMU_pioAddr addr
)
249 unsigned long translated_addr
= addr
;
250 u8 translated
= biosemu_dev_translate_address(IORESOURCE_IO
, &translated_addr
);
251 if (translated
!= 0) {
252 //translation successful, access Device I/O (BAR or Legacy...)
253 DEBUG_PRINTF_IO("%s(%x): access to Device I/O\n", __func__
,
255 //DEBUG_PRINTF_IO("%s(%04x): translated_addr: %llx\n", __func__, addr, translated_addr);
257 if ((translated_addr
& (u64
) 0x3) == 0) {
258 // 32 bit aligned access...
259 u32 tempval
= read_io((void *) translated_addr
, 4);
260 //little endian conversion
261 rval
= in32le((void *) &tempval
);
263 // unaligned access, read single bytes, little-endian
264 rval
= (read_io((void *)(translated_addr
), 1) << 24)
265 | (read_io((void *)(translated_addr
+ 1), 1) << 16)
266 | (read_io((void *)(translated_addr
+ 2), 1) << 8)
267 | (read_io((void *)(translated_addr
+ 3), 1));
269 DEBUG_PRINTF_IO("%s(%04x) Device I/O --> %08x\n", __func__
,
275 //PCI Config Mechanism 1
276 return pci_cfg_read(addr
, 4);
280 ("%s(%04x) reading from bios_device.io_buffer\n",
283 in32le((void *) bios_device
.io_buffer
+ addr
);
284 DEBUG_PRINTF_IO("%s(%04x) I/O Buffer --> %08x\n",
285 __func__
, addr
, rval
);
293 my_outb(X86EMU_pioAddr addr
, u8 val
)
295 unsigned long translated_addr
= addr
;
296 u8 translated
= biosemu_dev_translate_address(IORESOURCE_IO
, &translated_addr
);
297 if (translated
!= 0) {
298 //translation successful, access Device I/O (BAR or Legacy...)
299 DEBUG_PRINTF_IO("%s(%x, %x): access to Device I/O\n",
300 __func__
, addr
, val
);
301 //DEBUG_PRINTF_IO("%s(%04x): translated_addr: %llx\n", __func__, addr, translated_addr);
302 write_io((void *) translated_addr
, val
, 1);
303 DEBUG_PRINTF_IO("%s(%04x) Device I/O <-- %02x\n", __func__
,
311 // PCI Config Mechanism 1 Ports
312 pci_cfg_write(addr
, val
, 1);
316 ("%s(%04x,%02x) writing to bios_device.io_buffer\n",
317 __func__
, addr
, val
);
318 *((u8
*) (bios_device
.io_buffer
+ addr
)) = val
;
325 my_outw(X86EMU_pioAddr addr
, u16 val
)
327 unsigned long translated_addr
= addr
;
328 u8 translated
= biosemu_dev_translate_address(IORESOURCE_IO
, &translated_addr
);
329 if (translated
!= 0) {
330 //translation successful, access Device I/O (BAR or Legacy...)
331 DEBUG_PRINTF_IO("%s(%x, %x): access to Device I/O\n",
332 __func__
, addr
, val
);
333 //DEBUG_PRINTF_IO("%s(%04x): translated_addr: %llx\n", __func__, addr, translated_addr);
334 if ((translated_addr
& (u64
) 0x1) == 0) {
335 // little-endian conversion
336 u16 tempval
= in16le((void *) &val
);
337 // 16 bit aligned access...
338 write_io((void *) translated_addr
, tempval
, 2);
340 // unaligned access, write single bytes, little-endian
341 write_io(((void *) (translated_addr
+ 1)),
342 (u8
) ((val
& 0xFF00) >> 8), 1);
343 write_io(((void *) translated_addr
),
344 (u8
) (val
& 0x00FF), 1);
346 DEBUG_PRINTF_IO("%s(%04x) Device I/O <-- %04x\n", __func__
,
352 // PCI Config Mechanism 1 Ports
353 pci_cfg_write(addr
, val
, 2);
357 ("%s(%04x,%04x) writing to bios_device.io_buffer\n",
358 __func__
, addr
, val
);
359 out16le((void *) bios_device
.io_buffer
+ addr
, val
);
366 my_outl(X86EMU_pioAddr addr
, u32 val
)
368 unsigned long translated_addr
= addr
;
369 u8 translated
= biosemu_dev_translate_address(IORESOURCE_IO
, &translated_addr
);
370 if (translated
!= 0) {
371 //translation successful, access Device I/O (BAR or Legacy...)
372 DEBUG_PRINTF_IO("%s(%x, %x): access to Device I/O\n",
373 __func__
, addr
, val
);
374 //DEBUG_PRINTF_IO("%s(%04x): translated_addr: %llx\n", __func__, addr, translated_addr);
375 if ((translated_addr
& (u64
) 0x3) == 0) {
376 // little-endian conversion
377 u32 tempval
= in32le((void *) &val
);
378 // 32 bit aligned access...
379 write_io((void *) translated_addr
, tempval
, 4);
381 // unaligned access, write single bytes, little-endian
382 write_io(((void *) translated_addr
+ 3),
383 (u8
) ((val
& 0xFF000000) >> 24), 1);
384 write_io(((void *) translated_addr
+ 2),
385 (u8
) ((val
& 0x00FF0000) >> 16), 1);
386 write_io(((void *) translated_addr
+ 1),
387 (u8
) ((val
& 0x0000FF00) >> 8), 1);
388 write_io(((void *) translated_addr
),
389 (u8
) (val
& 0x000000FF), 1);
391 DEBUG_PRINTF_IO("%s(%04x) Device I/O <-- %08x\n", __func__
,
396 // PCI Config Mechanism 1 Ports
397 pci_cfg_write(addr
, val
, 4);
401 ("%s(%04x,%08x) writing to bios_device.io_buffer\n",
402 __func__
, addr
, val
);
403 out32le((void *) bios_device
.io_buffer
+ addr
, val
);
410 pci_cfg_read(X86EMU_pioAddr addr
, u8 size
)
412 u32 port_cf8_val
= 0;
413 u32 rval
= 0xFFFFFFFF;
414 struct device
*dev
= NULL
;
417 // PCI Configuration Mechanism 1 step 1
418 // write to 0xCF8, sets bus, device, function and Config Space offset
419 // later read from 0xCFC-0xCFF returns the value...
420 if ((addr
>= 0xCFC) && ((addr
+ size
) <= 0xD00))
421 port_cf8_val
= my_inl(0xCF8);
423 if ((port_cf8_val
& 0x80000000) == 0)
426 //highest bit enables config space mapping
427 bus
= (port_cf8_val
& 0x00FF0000) >> 16;
428 devfn
= (port_cf8_val
& 0x0000FF00) >> 8;
429 offs
= (port_cf8_val
& 0x000000FF);
430 offs
+= (addr
- 0xCFC); // if addr is not 0xcfc, the offset is moved accordingly
431 DEBUG_PRINTF_INTR("%s(): PCI Config Read from device: bus: %02x, devfn: %02x, offset: %02x\n",
432 __func__
, bus
, devfn
, offs
);
434 if ((bus
== bios_device
.bus
) && (devfn
== bios_device
.devfn
)) {
435 dev
= bios_device
.dev
;
436 } else if (CONFIG(YABEL_PCI_ACCESS_OTHER_DEVICES
)) {
437 dev
= pcidev_path_on_bus(bus
, devfn
);
438 DEBUG_PRINTF_INTR("%s(): pcidev_path_on_bus() returned: %s\n",
439 __func__
, dev_path(dev
));
444 ("%s(): Config read access invalid device! bus: %02x (%02x), devfn: %02x (%02x), offs: %02x\n",
445 __func__
, bus
, bios_device
.bus
, devfn
,
446 bios_device
.devfn
, offs
);
452 if (CONFIG(PCI_OPTION_ROM_RUN_YABEL
)) {
455 rval
= pci_read_config8(dev
, offs
);
458 rval
= pci_read_config16(dev
, offs
);
461 rval
= pci_read_config32(dev
, offs
);
465 rval
= (u32
) rtas_pci_config_read(bios_device
.puid
, size
, bus
, devfn
, offs
);
469 ("%s(%04x) PCI Config Read @%02x, size: %d --> 0x%08x\n",
470 __func__
, addr
, offs
, size
, rval
);
476 pci_cfg_write(X86EMU_pioAddr addr
, u32 val
, u8 size
)
478 struct device
*dev
= NULL
;
479 u32 port_cf8_val
= 0;
482 // PCI Configuration Mechanism 1 step 1
483 // write to 0xCF8, sets bus, device, function and Config Space offset
484 // later write to 0xCFC-0xCFF sets the value...
486 if ((addr
>= 0xCFC) && ((addr
+ size
) <= 0xD00))
487 port_cf8_val
= my_inl(0xCF8);
489 if ((port_cf8_val
& 0x80000000) == 0)
492 //highest bit enables config space mapping
493 bus
= (port_cf8_val
& 0x00FF0000) >> 16;
494 devfn
= (port_cf8_val
& 0x0000FF00) >> 8;
495 offs
= (port_cf8_val
& 0x000000FF);
496 offs
+= (addr
- 0xCFC); // if addr is not 0xcfc, the offset is moved accordingly
498 if ((bus
== bios_device
.bus
) && (devfn
== bios_device
.devfn
)) {
499 dev
= bios_device
.dev
;
502 ("Config write access invalid! PCI device %x:%x.%x, offs: %x\n",
503 bus
, devfn
>> 3, devfn
& 7, offs
);
505 if (CONFIG(YABEL_PCI_FAKE_WRITING_OTHER_DEVICES_CONFIG
))
507 // fail accesses to any device but ours...
511 if (CONFIG(PCI_OPTION_ROM_RUN_YABEL
)) {
514 pci_write_config8(dev
, offs
, val
);
517 pci_write_config16(dev
, offs
, val
);
520 pci_write_config32(dev
, offs
, val
);
524 rtas_pci_config_write(bios_device
.puid
, size
, bus
, devfn
, offs
, val
);
528 ("%s(%04x) PCI Config Write @%02x, size: %d <-- 0x%08x\n",
529 __func__
, addr
, offs
, size
, val
);
533 handle_port_61h(void)
535 static u64 last_time
= 0;
536 u64 curr_time
= get_time();
537 u64 time_diff
; // time since last call
538 u32 period_ticks
; // length of a period in ticks
539 u32 nr_periods
; //number of periods passed since last call
540 // bit 4 should toggle with every (DRAM) refresh cycle... (66kHz??)
541 time_diff
= curr_time
- last_time
;
542 // at 66kHz a period is ~ 15 ns long, converted to ticks: (tb_freq is ticks/second)
543 // TODO: as long as the frequency does not change, we should not calculate this every time
544 period_ticks
= (15 * tb_freq
) / 1000000;
545 nr_periods
= time_diff
/ period_ticks
;
546 // if the number if ticks passed since last call is odd, we toggle bit 4
547 if ((nr_periods
% 2) != 0) {
548 *((u8
*) (bios_device
.io_buffer
+ 0x61)) ^= 0x10;
550 //finally read the value from the io_buffer
551 return *((u8
*) (bios_device
.io_buffer
+ 0x61));