b9d6e08134733189f1f752468649273460ebf43b
[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 _EXPLODE(params...)     params
18 #define _ADDGCB(params...)      (udi_cb_t *gcb, params)
19 #define CONTIN(suffix, call, args, params)      extern void _EXPJOIN(suffix##__,__LINE__) _ADDGCB params;\
20         call( _EXPJOIN(suffix##__,__LINE__), gcb, _EXPLODE args); } \
21         void _EXPJOIN(suffix##__,__LINE__) _ADDGCB params { \
22         rdata_t *rdata = gcb->context;
23
24 // --- Management Metalang
25 void uart_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
26 {
27         rdata_t *rdata = UDI_GCB(cb)->context;
28
29         // TODO: Set up region data     
30
31         udi_usage_res(cb);
32 }
33
34 void uart_enumerate_req(udi_enumerate_cb_t *cb, udi_ubit8_t enumeration_level)
35 {
36         rdata_t *rdata = UDI_GCB(cb)->context;
37         udi_instance_attr_list_t *attr_list = cb->attr_list;
38         
39         switch(enumeration_level)
40         {
41         case UDI_ENUMERATE_START:
42         case UDI_ENUMERATE_START_RESCAN:
43                 DPT_SET_ATTR_STRING(attr_list, "gio_type", "uart", 4);
44                 attr_list ++;
45                 cb->attr_valid_length = attr_list - cb->attr_list;
46                 udi_enumerate_ack(cb, UDI_ENUMERATE_OK, UART_OPS_GIO);
47                 break;
48         case UDI_ENUMERATE_NEXT:
49                 udi_enumerate_ack(cb, UDI_ENUMERATE_DONE, 0);
50                 break;
51         }
52 }
53 void uart_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
54 {
55 }
56 void uart_final_cleanup_req(udi_mgmt_cb_t *cb)
57 {
58 }
59 // ---
60 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb);
61 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);
62 void uart_bus_dev_bind__pio_map(udi_cb_t *cb, udi_pio_handle_t new_pio_handle);
63 void uart_bus_dev_bind__intr_chanel(udi_cb_t *gcb, udi_channel_t new_channel);
64 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb);
65
66 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb)
67 {
68         udi_cb_t        *gcb = UDI_GCB(cb);
69         rdata_t *rdata = gcb->context;
70         
71         switch(cb->event)
72         {
73         case UDI_CHANNEL_CLOSED:
74                 break;
75         case UDI_CHANNEL_BOUND: {
76                 rdata->active_cb = gcb;
77                 udi_bus_bind_cb_t *bus_bind_cb = UDI_MCB(cb->params.parent_bound.bind_cb, udi_bus_bind_cb_t);
78                 udi_bus_bind_req( bus_bind_cb );
79                 // continue at uart_bus_dev_bus_bind_ack
80                 return; }
81         }
82 }
83 void uart_bus_dev_bus_bind_ack(udi_bus_bind_cb_t *cb,
84         udi_dma_constraints_t dma_constraints, udi_ubit8_t perferred_endianness, udi_status_t status)
85 {
86         udi_cb_t        *gcb = UDI_GCB(cb);
87         rdata_t *rdata = gcb->context;
88         
89         // Set up PIO handles
90         rdata->init.pio_index = -1;
91         uart_bus_dev_bind__pio_map(gcb, UDI_NULL_PIO_HANDLE);
92         // V V V V
93 }
94 void uart_bus_dev_bind__pio_map(udi_cb_t *gcb, udi_pio_handle_t new_pio_handle)
95 {
96         rdata_t *rdata = gcb->context;
97         if( rdata->init.pio_index != -1 )
98         {
99                 rdata->pio_handles[rdata->init.pio_index] = new_pio_handle;
100         }
101         
102         rdata->init.pio_index ++;
103         if( rdata->init.pio_index < N_PIO )
104         {
105                 udi_pio_map(uart_bus_dev_bind__pio_map, gcb,
106                         UDI_PCI_BAR_0, 0, 0x8,  // TODO: Use system bus index and offset
107                         uart_pio_ops[rdata->init.pio_index].trans_list,
108                         uart_pio_ops[rdata->init.pio_index].list_length,
109                         UDI_PIO_LITTLE_ENDIAN, 0, 0
110                         );
111                 return ;
112         }
113
114         CONTIN( uart_bus_dev_bind, udi_channel_spawn, (gcb->channel, 0, UART_OPS_IRQ, rdata),
115                 (udi_channel_t new_channel))
116         // new function
117         
118         rdata->interrupt_channel = new_channel;
119         
120         CONTIN( uart_bus_dev_bind, udi_cb_alloc, (UART_CB_INTR, gcb->channel),
121                 (udi_cb_t *new_cb))
122         // new function
123         if( !new_cb )
124         {
125                 // Oh...
126                 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
127                         UDI_STAT_RESOURCE_UNAVAIL );
128                 return ;
129         }
130         udi_intr_attach_cb_t    *intr_cb = UDI_MCB(new_cb, udi_intr_attach_cb_t);
131         intr_cb->interrupt_idx = 0;
132         intr_cb->min_event_pend = 2;
133         intr_cb->preprocessing_handle = rdata->pio_handles[PIO_RX];
134         udi_intr_attach_req(intr_cb);
135         // continued in uart_bus_dev_intr_attach_ack
136 }
137 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb)
138 {
139 }
140 void uart_bus_dev_intr_attach_ack(udi_intr_attach_cb_t *intr_attach_cb, udi_status_t status)
141 {
142 }
143 void uart_bus_dev_intr_detach_ack(udi_intr_detach_cb_t *intr_detach_cb)
144 {
145 }
146 // ---
147 void uart_bus_irq_channel_event_ind(udi_channel_event_cb_t *cb)
148 {
149 }
150 void uart_bus_irq_intr_event_ind(udi_intr_event_cb_t *cb, udi_ubit8_t flags)
151 {
152         udi_debug_printf("%s: flags=%x, intr_result=%x\n", __func__, flags, cb->intr_result);
153         if( cb->intr_result & 0x01 )
154         {
155                 //ne2k_intr__rx_ok( UDI_GCB(cb) );
156         }
157         // TODO: TX IRQs
158         udi_intr_event_rdy(cb);
159 }
160 // ---
161 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb);
162 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb);
163 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb);
164 void uart_gio_read_complete(udi_cb_t *gcb, udi_buf_t *new_buf);
165 void uart_gio_write_complete(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res);
166 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb);
167
168 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb)
169 {
170 }
171 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb)
172 {
173         udi_gio_bind_ack(cb, 0, 0, UDI_OK);
174 }
175 void uart_gio_prov_unbind_req(udi_gio_bind_cb_t *cb)
176 {
177         udi_gio_unbind_ack(cb);
178 }
179 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb)
180 {
181         udi_cb_t        *gcb = UDI_GCB(cb);
182         rdata_t *rdata = gcb->context;
183         switch(cb->op)
184         {
185         case UDI_GIO_OP_READ:
186                 // Read from local FIFO
187                 if( rdata->rx_buffer->buf_size < cb->data_buf->buf_size ) {
188                         // Local FIFO was empty
189                         udi_gio_xfer_nak(cb, UDI_STAT_DATA_UNDERRUN);
190                 }
191                 else {
192                         udi_buf_copy(uart_gio_read_complete, gcb,
193                                 rdata->rx_buffer,
194                                 0, cb->data_buf->buf_size,
195                                 cb->data_buf,
196                                 0, cb->data_buf->buf_size,
197                                 UDI_NULL_BUF_PATH);
198                 }
199                 break;
200         case UDI_GIO_OP_WRITE:
201                 // Send to device
202                 udi_pio_trans(uart_gio_write_complete, gcb,
203                         rdata->pio_handles[PIO_TX], 0, cb->data_buf, NULL);
204                 break;
205         default:
206                 udi_gio_xfer_nak(cb, UDI_STAT_NOT_SUPPORTED);
207                 break;
208         }
209 }
210 void uart_gio_read_complete(udi_cb_t *gcb, udi_buf_t *new_buf)
211 {
212         rdata_t *rdata = gcb->context;
213         udi_gio_xfer_cb_t       *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
214         cb->data_buf = new_buf;
215         // Delete read bytes from the RX buffer
216         CONTIN(uart_gio_read_complete, udi_buf_write, (NULL, 0, rdata->rx_buffer, 0, cb->data_buf->buf_size, UDI_NULL_BUF_PATH),
217                 (udi_buf_t *new_buf))
218         udi_gio_xfer_cb_t       *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
219         rdata->rx_buffer = new_buf;
220         udi_gio_xfer_ack(cb);
221 }
222 void uart_gio_write_complete(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res)
223 {
224         udi_gio_xfer_cb_t       *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
225         cb->data_buf = new_buf;
226         udi_gio_xfer_ack(cb);
227 }
228 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb)
229 {
230 }
231
232 // ====================================================================
233 // - Management ops
234 udi_mgmt_ops_t  uart_mgmt_ops = {
235         uart_usage_ind,
236         uart_enumerate_req,
237         uart_devmgmt_req,
238         uart_final_cleanup_req
239 };
240 udi_ubit8_t     uart_mgmt_op_flags[4] = {0,0,0,0};
241 // - Bus Ops
242 udi_bus_device_ops_t    uart_bus_dev_ops = {
243         uart_bus_dev_channel_event_ind,
244         uart_bus_dev_bus_bind_ack,
245         uart_bus_dev_bus_unbind_ack,
246         uart_bus_dev_intr_attach_ack,
247         uart_bus_dev_intr_detach_ack
248 };
249 udi_ubit8_t     uart_bus_dev_ops_flags[5] = {0};
250 // - IRQ Ops
251 udi_intr_handler_ops_t  uart_bus_irq_ops = {
252         uart_bus_irq_channel_event_ind,
253         uart_bus_irq_intr_event_ind
254 };
255 udi_ubit8_t     uart_bus_irq_ops_flags[2] = {0};
256 // - GIO provider ops
257 udi_gio_provider_ops_t  uart_gio_prov_ops = {
258         uart_gio_prov_channel_event_ind,
259         uart_gio_prov_bind_req,
260         uart_gio_prov_unbind_req,
261         uart_gio_prov_xfer_req,
262         uart_gio_prov_event_res
263 };
264 udi_ubit8_t     uart_gio_prov_ops_flags[5] = {0};
265 // --
266 udi_primary_init_t      uart_pri_init = {
267         .mgmt_ops = &uart_mgmt_ops,
268         .mgmt_op_flags = uart_mgmt_op_flags,
269         .mgmt_scratch_requirement = 0,
270         .enumeration_attr_list_length = 4,
271         .rdata_size = sizeof(rdata_t),
272         .child_data_size = 0,
273         .per_parent_paths = 0
274 };
275 udi_ops_init_t  uart_ops_list[] = {
276         {
277                 UART_OPS_DEV, UART_META_BUS, UDI_BUS_DEVICE_OPS_NUM,
278                 0,
279                 (udi_ops_vector_t*)&uart_bus_dev_ops,
280                 uart_bus_dev_ops_flags
281         },
282         {
283                 UART_OPS_IRQ, UART_META_BUS, UDI_BUS_INTR_HANDLER_OPS_NUM,
284                 0,
285                 (udi_ops_vector_t*)&uart_bus_irq_ops,
286                 uart_bus_irq_ops_flags
287         },
288         {
289                 UART_OPS_GIO, UART_META_GIO, UDI_GIO_PROVIDER_OPS_NUM,
290                 0,
291                 (udi_ops_vector_t*)&uart_gio_prov_ops,
292                 uart_gio_prov_ops_flags
293         },
294         {0}
295 };
296 udi_cb_init_t uart_cb_init_list[] = {
297         {UART_CB_BUS_BIND,   UART_META_BUS, UDI_BUS_BIND_CB_NUM, 0, 0,NULL},
298         {UART_CB_INTR,       UART_META_BUS, UDI_BUS_INTR_ATTACH_CB_NUM, 0, 0,NULL},
299         {UART_CB_INTR_EVENT, UART_META_BUS, UDI_BUS_INTR_EVENT_CB_NUM, 0, 0,NULL},
300         {0}
301 };
302 const udi_init_t        udi_init_info = {
303         .primary_init_info = &uart_pri_init,
304         .ops_init_list = uart_ops_list,
305         .cb_init_list = uart_cb_init_list,
306 };

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