UDI/Drivers - Adding 16550 UART driver
[tpg/acess2.git] / UDI / drivers / uart_16c550 / uart16c550.c
1 /*
2  * UDI Ne2000 NIC Driver
3  * By John Hodge (thePowersGang)
4  *
5  * uart16c550.c
6  * - UDI initialisation 
7  */
8 #include <udi.h>
9 #include <udi_physio.h>
10 #include "uart16c550_common.h"
11
12
13 #include "uart16c550_pio.h"
14
15 #define __EXPJOIN(a,b)  a##b
16 #define _EXPJOIN(a,b)   __EXPJOIN(a,b)
17 #define CONTIN(suffix, call, params, args...)   extern void _EXPJOIN(suffix##__,__LINE__) params;\
18         call(_EXPJOIN(suffix##__,__LINE__), gcb,## args); } \
19         void _EXPJOIN(suffix##__,__LINE__) params { \
20         rdata_t *rdata = gcb->context;
21
22 // --- Management Metalang
23 void uart_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
24 {
25         rdata_t *rdata = UDI_GCB(cb)->context;
26
27         // TODO: Set up region data     
28
29         udi_usage_res(cb);
30 }
31
32 void uart_enumerate_req(udi_enumerate_cb_t *cb, udi_ubit8_t enumeration_level)
33 {
34         rdata_t *rdata = UDI_GCB(cb)->context;
35         udi_instance_attr_list_t *attr_list = cb->attr_list;
36         
37         switch(enumeration_level)
38         {
39         case UDI_ENUMERATE_START:
40         case UDI_ENUMERATE_START_RESCAN:
41                 DPT_SET_ATTR_STRING(attr_list, "if_num", "uart", 4);
42                 attr_list ++;
43                 cb->attr_valid_length = attr_list - cb->attr_list;
44                 udi_enumerate_ack(cb, UDI_ENUMERATE_OK, UART_OPS_GIO);
45                 break;
46         case UDI_ENUMERATE_NEXT:
47                 udi_enumerate_ack(cb, UDI_ENUMERATE_DONE, 0);
48                 break;
49         }
50 }
51 void uart_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
52 {
53 }
54 void uart_final_cleanup_req(udi_mgmt_cb_t *cb)
55 {
56 }
57 // ---
58 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb);
59 void uart_bus_dev_bus_bind_ack(udi_bus_bind_cb_t *cb, udi_dma_constraints_t dma_constraints, udi_ubit8_t perferred_endianness, udi_status_t status);
60 void uart_bus_dev_bind__pio_map(udi_cb_t *cb, udi_pio_handle_t new_pio_handle);
61 void uart_bus_dev_bind__intr_chanel(udi_cb_t *gcb, udi_channel_t new_channel);
62 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb);
63
64 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb)
65 {
66         udi_cb_t        *gcb = UDI_GCB(cb);
67         rdata_t *rdata = gcb->context;
68         
69         switch(cb->event)
70         {
71         case UDI_CHANNEL_CLOSED:
72                 break;
73         case UDI_CHANNEL_BOUND: {
74                 rdata->active_cb = gcb;
75                 udi_bus_bind_cb_t *bus_bind_cb = UDI_MCB(cb->params.parent_bound.bind_cb, udi_bus_bind_cb_t);
76                 udi_bus_bind_req( bus_bind_cb );
77                 // continue at uart_bus_dev_bus_bind_ack
78                 return; }
79         }
80 }
81 void uart_bus_dev_bus_bind_ack(udi_bus_bind_cb_t *cb,
82         udi_dma_constraints_t dma_constraints, udi_ubit8_t perferred_endianness, udi_status_t status)
83 {
84         udi_cb_t        *gcb = UDI_GCB(cb);
85         rdata_t *rdata = gcb->context;
86         
87         // Set up PIO handles
88         rdata->init.pio_index = -1;
89         uart_bus_dev_bind__pio_map(gcb, UDI_NULL_PIO_HANDLE);
90         // V V V V
91 }
92 void uart_bus_dev_bind__pio_map(udi_cb_t *gcb, udi_pio_handle_t new_pio_handle)
93 {
94         rdata_t *rdata = gcb->context;
95         if( rdata->init.pio_index != -1 )
96         {
97                 rdata->pio_handles[rdata->init.pio_index] = new_pio_handle;
98         }
99         
100         rdata->init.pio_index ++;
101         if( rdata->init.pio_index < N_PIO )
102         {
103                 udi_pio_map(uart_bus_dev_bind__pio_map, gcb,
104                         UDI_PCI_BAR_0, 0, 0x8,  // TODO: Use system bus index and offset
105                         uart_pio_ops[rdata->init.pio_index].trans_list,
106                         uart_pio_ops[rdata->init.pio_index].list_length,
107                         UDI_PIO_LITTLE_ENDIAN, 0, 0
108                         );
109                 return ;
110         }
111
112         CONTIN( uart_bus_dev_bind, udi_channel_spawn, (udi_cb_t *gcb, udi_channel_t new_channel), gcb->channel, 0, UART_OPS_IRQ, rdata)
113 //      udi_channel_spawn(uart_bus_dev_bind__intr_chanel, gcb, gcb->channel, 0, UART_OPS_IRQ, rdata);
114 //}
115 //void uart_bus_dev_bind__intr_chanel(udi_cb_t *gcb, udi_channel_t new_channel)
116 //{
117 //      rdata_t *rdata = gcb->context;
118         
119         rdata->interrupt_channel = new_channel;
120         
121         CONTIN( uart_bus_dev_bind, udi_cb_alloc, (udi_cb_t *gcb, udi_cb_t *new_cb), UART_CB_INTR, gcb->channel)
122 //      udi_cb_alloc(uart_bus_dev_bind__intr_attach, gcb, NE2K_CB_INTR, gcb->channel);
123 //      // V V V V
124 //}
125 //void uart_bus_dev_bind__intr_attach(udi_cb_t *gcb, udi_cb_t *new_cb)
126 //{
127 //      rdata_t *rdata = gcb->context;
128         if( !new_cb )
129         {
130                 // Oh...
131                 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
132                         UDI_STAT_RESOURCE_UNAVAIL );
133                 return ;
134         }
135         udi_intr_attach_cb_t    *intr_cb = UDI_MCB(new_cb, udi_intr_attach_cb_t);
136         intr_cb->interrupt_idx = 0;
137         intr_cb->min_event_pend = 2;
138         intr_cb->preprocessing_handle = rdata->pio_handles[PIO_RX];
139         udi_intr_attach_req(intr_cb);
140         // continued in uart_bus_dev_intr_attach_ack
141 }
142 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb)
143 {
144 }
145 void uart_bus_dev_intr_attach_ack(udi_intr_attach_cb_t *intr_attach_cb, udi_status_t status)
146 {
147 }
148 void uart_bus_dev_intr_detach_ack(udi_intr_detach_cb_t *intr_detach_cb)
149 {
150 }
151 // ---
152 void uart_bus_irq_channel_event_ind(udi_channel_event_cb_t *cb)
153 {
154 }
155 void uart_bus_irq_intr_event_ind(udi_intr_event_cb_t *cb, udi_ubit8_t flags)
156 {
157         udi_debug_printf("%s: flags=%x, intr_result=%x\n", __func__, flags, cb->intr_result);
158         if( cb->intr_result & 0x01 )
159         {
160                 //ne2k_intr__rx_ok( UDI_GCB(cb) );
161         }
162         // TODO: TX IRQs
163         udi_intr_event_rdy(cb);
164 }
165 // ---
166 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb)
167 {
168 }
169 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb)
170 {
171 }
172 void uart_gio_prov_unbind_req(udi_gio_bind_cb_t *cb)
173 {
174 }
175 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb)
176 {
177 }
178 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb)
179 {
180 }
181
182 // ====================================================================
183 // - Management ops
184 udi_mgmt_ops_t  uart_mgmt_ops = {
185         uart_usage_ind,
186         uart_enumerate_req,
187         uart_devmgmt_req,
188         uart_final_cleanup_req
189 };
190 udi_ubit8_t     uart_mgmt_op_flags[4] = {0,0,0,0};
191 // - Bus Ops
192 udi_bus_device_ops_t    uart_bus_dev_ops = {
193         uart_bus_dev_channel_event_ind,
194         uart_bus_dev_bus_bind_ack,
195         uart_bus_dev_bus_unbind_ack,
196         uart_bus_dev_intr_attach_ack,
197         uart_bus_dev_intr_detach_ack
198 };
199 udi_ubit8_t     uart_bus_dev_ops_flags[5] = {0};
200 // - IRQ Ops
201 udi_intr_handler_ops_t  uart_bus_irq_ops = {
202         uart_bus_irq_channel_event_ind,
203         uart_bus_irq_intr_event_ind
204 };
205 udi_ubit8_t     uart_bus_irq_ops_flags[2] = {0};
206 // - GIO provider ops
207 udi_gio_provider_ops_t  uart_gio_prov_ops = {
208         uart_gio_prov_channel_event_ind,
209         uart_gio_prov_bind_req,
210         uart_gio_prov_unbind_req,
211         uart_gio_prov_xfer_req,
212         uart_gio_prov_event_res
213 };
214 udi_ubit8_t     uart_gio_prov_ops_flags[5] = {0};
215 // --
216 udi_primary_init_t      uart_pri_init = {
217         .mgmt_ops = &uart_mgmt_ops,
218         .mgmt_op_flags = uart_mgmt_op_flags,
219         .mgmt_scratch_requirement = 0,
220         .enumeration_attr_list_length = 4,
221         .rdata_size = sizeof(rdata_t),
222         .child_data_size = 0,
223         .per_parent_paths = 0
224 };
225 udi_ops_init_t  uart_ops_list[] = {
226         {
227                 UART_OPS_DEV, UART_META_BUS, UDI_BUS_DEVICE_OPS_NUM,
228                 0,
229                 (udi_ops_vector_t*)&uart_bus_dev_ops,
230                 uart_bus_dev_ops_flags
231         },
232         {
233                 UART_OPS_IRQ, UART_META_BUS, UDI_BUS_INTR_HANDLER_OPS_NUM,
234                 0,
235                 (udi_ops_vector_t*)&uart_bus_irq_ops,
236                 uart_bus_irq_ops_flags
237         },
238         {
239                 UART_OPS_GIO, UART_META_GIO, UDI_GIO_PROVIDER_OPS_NUM,
240                 0,
241                 (udi_ops_vector_t*)&uart_gio_prov_ops,
242                 uart_gio_prov_ops_flags
243         },
244         {0}
245 };
246 udi_cb_init_t uart_cb_init_list[] = {
247         {UART_CB_BUS_BIND,   UART_META_BUS, UDI_BUS_BIND_CB_NUM, 0, 0,NULL},
248         {UART_CB_INTR,       UART_META_BUS, UDI_BUS_INTR_ATTACH_CB_NUM, 0, 0,NULL},
249         {UART_CB_INTR_EVENT, UART_META_BUS, UDI_BUS_INTR_EVENT_CB_NUM, 0, 0,NULL},
250         {0}
251 };
252 const udi_init_t        udi_init_info = {
253         .primary_init_info = &uart_pri_init,
254         .ops_init_list = uart_ops_list,
255         .cb_init_list = uart_cb_init_list,
256 };

UCC git Repository :: git.ucc.asn.au