Initial cut of the open ath9k htc firmware.
[open-ath9k-htc-firmware.git] / target_firmware / magpie_fw_dev / target / wlan / wlan_pci.c
1 #include <osapi.h>
2 #include <Magpie_api.h>
3 #include <adf_os_types.h>
4 #include <adf_os_pci.h>
5 #include <adf_net.h>
6 #include <sys_cfg.h>
7
8 #if MAGPIE_ENABLE_WLAN == 0
9 A_PCI_INIT_FUNC g_pci_init_func;
10 #endif
11
12 #if MAGPIE_ENABLE_PCIE == 0
13 #define EMULATE_PCI_CONFIG
14 #endif
15
16 #define PCI_CONFIG_BASE_ADDR        0x14000000 
17
18 extern A_PCI_INIT_FUNC g_pci_init_func;
19 adf_drv_info_t* g_wlan_drv = NULL;
20 adf_drv_handle_t g_wlan_drv_handle = NULL;
21 adf_os_drv_intr g_wlan_intr = NULL;
22
23 void wlan_pci_module_init(void)
24 {
25         if (g_pci_init_func != NULL) {
26                 g_pci_init_func();
27         }
28 }
29
30 void wlan_pci_register_drv(adf_drv_info_t *drv)
31 {
32         g_wlan_drv = drv;
33 }
34
35 #define ATHEROS_VENDOR_ID 0x168c
36 #define AR5416_DEVID_PCIE 0x24  
37
38 void wlan_pci_probe(void)
39 {
40         __adf_softc_t           *sc;
41         adf_os_resource_t       drv_res = {0};
42         adf_os_attach_data_t    drv_data = {{0}};   
43         int vendor_id;
44         int device_id;
45
46         A_PRINTF("<wlan_pci_probe>: Attaching the driver\n");
47
48 #if MAGPIE_ENABLE_PCIE == 0
49         vendor_id = ATHEROS_VENDOR_ID;
50         device_id = AR5416_DEVID_PCIE;
51 #else    
52         vendor_id = wlan_pci_config_read(0, 2);
53         device_id = wlan_pci_config_read(2, 2);
54 #endif    
55         A_PRINTF("<wlan_pci_probe>: Vendor id 0x%x Dev id 0x%x\n", vendor_id, device_id);    
56     
57         if (vendor_id != ATHEROS_VENDOR_ID) {
58                 A_PRINTF("<wlan_pci_probe>: Atheros card not found\n"); 
59                 return;
60         }
61             
62         /**
63          * Allocate the sc & zero down
64          */
65         sc = A_ALLOCRAM(sizeof(__adf_softc_t));
66         if (!sc) {
67                 A_PRINTF("Cannot malloc softc\n");
68                 goto mem_fail;
69         }
70     
71 #define AR5416_DEVID_PCIE 0x24          
72
73         drv_data.pci.device    = AR5416_DEVID_PCIE;
74         drv_data.pci.vendor    = 0x168c;
75         drv_data.pci.subvendor = 0;
76         drv_data.pci.subdevice = 0;
77     
78         drv_res.start  = (a_uint32_t) 0;
79         drv_res.end    = 0;
80         drv_res.type   = ADF_OS_RESOURCE_TYPE_MEM;
81         
82         g_wlan_drv_handle = g_wlan_drv->drv_attach(&drv_res, 1, &drv_data, NULL);
83         
84         return;
85 mem_fail:
86         return;        
87 }
88
89 int wlan_pci_config_write(int offset, a_uint32_t val, int width)
90 {
91 #if MAGPIE_ENABLE_PCIE == 1    
92         unsigned long addr = ( PCI_CONFIG_BASE_ADDR + offset ) & 0xfffffffc;
93         A_UINT8 *ptr = (A_UINT8 *)addr;   
94         A_UINT8 *valptr = (A_UINT8 *)&val; 
95         int idx = offset & 0x3;
96         int i;
97     
98         for (i = 0; i < width; i++) {
99                 ptr[idx + i] = valptr[3-i];
100         }            
101 #endif
102     
103         return 0;    
104 }
105
106 int wlan_pci_config_read(int offset, int width)
107 {
108 #if MAGPIE_ENABLE_PCIE == 0    
109         return 0;    
110 #else
111         unsigned long addr = ( PCI_CONFIG_BASE_ADDR + offset ) & 0xfffffffc;
112         unsigned long value = *((unsigned long *)addr);
113         A_UINT8 *ptr = (A_UINT8 *)&value;   
114         int idx = offset & 0x3;
115         int result = 0;
116         int i;
117     
118         for (i = 0; i < width; i++) {
119                 result |= (ptr[ 3 - (idx + i)] << (8*i));
120         }            
121     
122         return result;    
123 #endif    
124 }
125
126 void wlan_pci_isr()
127 {
128         if (g_wlan_intr != NULL && g_wlan_drv_handle != NULL) {
129                 g_wlan_intr(g_wlan_drv_handle);
130         }
131 }