pci.h (2577B)
1 /* Copyright 1999, Brian J. Swetland. All rights reserved. 2 ** Distributed under the terms of the OpenBLT License 3 */ 4 5 #ifndef _PCI_H 6 #define _PCI_H 7 8 #include <blt/types.h> 9 10 #ifdef __cplusplus 11 extern "C" { 12 #endif 13 14 typedef struct 15 { 16 /* normal header stuff */ 17 uint16 vendor_id; 18 uint16 device_id; 19 20 uint16 command; 21 uint16 status; 22 23 uint8 revision_id; 24 uint8 interface; 25 uint8 sub_class; 26 uint8 base_class; 27 28 uint8 cache_line_size; 29 uint8 latency_timer; 30 uint8 header_type; 31 uint8 bist; 32 33 /* device info */ 34 uint8 bus; 35 uint8 dev; 36 uint8 func; 37 uint8 irq; 38 39 /* base registers */ 40 uint32 base[6]; 41 uint32 size[6]; 42 43 } pci_cfg; 44 45 uint32 pci_read(int bus, int dev, int func, int reg, int bytes); 46 void pci_write(int bus, int dev, int func, int reg, uint32 v, int bytes); 47 int pci_probe(int bus, int dev, int func, pci_cfg *cfg); 48 49 #ifdef __cplusplus 50 } 51 #endif 52 53 #ifdef PCI_STUB 54 55 #include <blt/Connection.h> 56 #include <blt/Message.h> 57 58 namespace BLT { 59 60 class PCI 61 { 62 public: 63 int read(int bus, int dev, int func, int reg, uint32 *v, int bytes); 64 int write(int bus, int dev, int func, int reg, uint32 v, int bytes); 65 int get_nth_cfg(int n, pci_cfg *cfg); 66 67 static PCI *FindService(); 68 ~PCI(); 69 70 private: 71 PCI(Connection *pci); 72 Connection *pci; 73 }; 74 75 inline int 76 PCI::read(int bus, int dev, int func, int reg, uint32 *v, int bytes) 77 { 78 int32 res; 79 Message msg; 80 msg.PutInt32('code',2); 81 msg.PutInt32('arg0',bus); 82 msg.PutInt32('arg1',dev); 83 msg.PutInt32('arg2',func); 84 msg.PutInt32('arg3',reg); 85 msg.PutInt32('arg4',bytes); 86 87 if((res = pci->Call(&msg,&msg))) return res; 88 msg.GetInt32('ret0',(int32*) v); 89 msg.GetInt32('resp',&res); 90 91 return res; 92 } 93 94 inline int 95 PCI::write(int bus, int dev, int func, int reg, uint32 v, int bytes) 96 { 97 int32 res; 98 Message msg; 99 msg.PutInt32('code',3); 100 msg.PutInt32('arg0',bus); 101 msg.PutInt32('arg1',dev); 102 msg.PutInt32('arg2',func); 103 msg.PutInt32('arg3',reg); 104 msg.PutInt32('arg4',(int32)v); 105 msg.PutInt32('arg5',bytes); 106 107 if((res = pci->Call(&msg,&msg))) return res; 108 msg.GetInt32('resp',&res); 109 110 return res; 111 } 112 113 inline int 114 PCI::get_nth_cfg(int n, pci_cfg *cfg) 115 { 116 int32 res; 117 Message msg; 118 msg.PutInt32('code',1); 119 msg.PutInt32('arg0',n); 120 121 if((res = pci->Call(&msg,&msg))) return res; 122 if((res = msg.GetData('ret0','pcic',cfg,sizeof(pci_cfg)))) return res; 123 msg.GetInt32('resp',&res); 124 125 return res; 126 } 127 128 inline PCI * 129 PCI::FindService() 130 { 131 Connection *cnxn = Connection::FindService("pci"); 132 if(cnxn) { 133 return new PCI(cnxn); 134 } else { 135 return 0; 136 } 137 } 138 139 PCI::PCI(Connection *cnxn){ 140 pci = cnxn; 141 } 142 143 PCI::~PCI(){ 144 delete pci; 145 } 146 147 } 148 149 #endif 150 151 #endif