211dfd7d0f31356bcc0279bab4968251ff8b36cc
[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 #define UDI_VERSION     0x101
9 #include <udi.h>
10 #include <udi_physio.h>
11 #include "uart16c550_common.h"
12
13 #include "uart16c550_pio.h"
14
15 #if 0
16 # define DEBUG_OUT(fmt, v...)   udi_debug_printf("%s: "fmt"\n", __func__ ,## v) 
17 #else
18 # define DEBUG_OUT(...) do{}while(0)
19 #endif
20
21 #define __EXPJOIN(a,b)  a##b
22 #define _EXPJOIN(a,b)   __EXPJOIN(a,b)
23 #define _EXPLODE(params...)     params
24 #define _ADDGCB(params...)      (udi_cb_t *gcb, params)
25 #define CONTIN(suffix, call, args, params)      extern void _EXPJOIN(suffix##__,__LINE__) _ADDGCB params;\
26         call( _EXPJOIN(suffix##__,__LINE__), gcb, _EXPLODE args); } \
27         void _EXPJOIN(suffix##__,__LINE__) _ADDGCB params { \
28         rdata_t *rdata = gcb->context;
29
30 // --- Management Metalang
31 void uart_usage_ind(udi_usage_cb_t *cb, udi_ubit8_t resource_level)
32 {
33         rdata_t *rdata = UDI_GCB(cb)->context;
34         //udi_trace_write(rdata->init_context, UDI_TREVENT_LOCAL_PROC_ENTRY, 0, );
35
36         // TODO: Set up region data     
37
38         udi_usage_res(cb);
39 }
40
41 void uart_enumerate_req(udi_enumerate_cb_t *cb, udi_ubit8_t enumeration_level)
42 {
43         rdata_t *rdata = UDI_GCB(cb)->context;
44         udi_instance_attr_list_t *attr_list = cb->attr_list;
45         
46         switch(enumeration_level)
47         {
48         case UDI_ENUMERATE_START:
49         case UDI_ENUMERATE_START_RESCAN:
50                 DPT_SET_ATTR_STRING(attr_list, "gio_type", "uart", 4);
51                 attr_list ++;
52                 cb->attr_valid_length = attr_list - cb->attr_list;
53                 udi_enumerate_ack(cb, UDI_ENUMERATE_OK, UART_OPS_GIO);
54                 break;
55         case UDI_ENUMERATE_NEXT:
56                 udi_enumerate_ack(cb, UDI_ENUMERATE_DONE, 0);
57                 break;
58         }
59 }
60 void uart_devmgmt_req(udi_mgmt_cb_t *cb, udi_ubit8_t mgmt_op, udi_ubit8_t parent_ID)
61 {
62 }
63 void uart_final_cleanup_req(udi_mgmt_cb_t *cb)
64 {
65 }
66 // ---
67 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb);
68 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);
69 void uart_bus_dev_bind__pio_map(udi_cb_t *cb, udi_pio_handle_t new_pio_handle);
70 void uart_bus_dev_bind__intr_chanel(udi_cb_t *gcb, udi_channel_t new_channel);
71 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb);
72
73 void uart_bus_dev_channel_event_ind(udi_channel_event_cb_t *cb)
74 {
75         udi_cb_t        *gcb = UDI_GCB(cb);
76         rdata_t *rdata = gcb->context;
77         
78         switch(cb->event)
79         {
80         case UDI_CHANNEL_CLOSED:
81                 break;
82         case UDI_CHANNEL_BOUND: {
83                 rdata->active_cb = gcb;
84                 udi_bus_bind_cb_t *bus_bind_cb = UDI_MCB(cb->params.parent_bound.bind_cb, udi_bus_bind_cb_t);
85                 udi_bus_bind_req( bus_bind_cb );
86                 // continue at uart_bus_dev_bus_bind_ack
87                 return; }
88         }
89 }
90 void uart_bus_dev_bus_bind_ack(udi_bus_bind_cb_t *cb,
91         udi_dma_constraints_t dma_constraints, udi_ubit8_t perferred_endianness, udi_status_t status)
92 {
93         udi_cb_t        *gcb = UDI_GCB(cb);
94         rdata_t *rdata = gcb->context;
95         
96         // Set up PIO handles
97         rdata->init.pio_index = -1;
98         uart_bus_dev_bind__pio_map(gcb, UDI_NULL_PIO_HANDLE);
99         // V V V V
100 }
101 void uart_bus_dev_bind__pio_map(udi_cb_t *gcb, udi_pio_handle_t new_pio_handle)
102 {
103         rdata_t *rdata = gcb->context;
104         if( rdata->init.pio_index != -1 )
105         {
106                 rdata->pio_handles[rdata->init.pio_index] = new_pio_handle;
107         }
108         
109         rdata->init.pio_index ++;
110         if( rdata->init.pio_index < N_PIO )
111         {
112                 udi_pio_map(uart_bus_dev_bind__pio_map, gcb,
113                         UDI_PCI_BAR_0, 0, 0x8,  // TODO: Use system bus index and offset
114                         uart_pio_ops[rdata->init.pio_index].trans_list,
115                         uart_pio_ops[rdata->init.pio_index].list_length,
116                         UDI_PIO_LITTLE_ENDIAN, 0, 0
117                         );
118                 return ;
119         }
120
121         // Spawn the interrupt channel
122         CONTIN( uart_bus_dev_bind, udi_channel_spawn, (gcb->channel, 0, UART_OPS_IRQ, rdata),
123                 (udi_channel_t new_channel))
124         
125         rdata->interrupt_channel = new_channel;
126
127         // Allocate an RX buffer
128         CONTIN( uart_bus_dev_bind, udi_buf_write, (NULL, 0, NULL, 0, 0, UDI_NULL_BUF_PATH),
129                 (udi_buf_t *new_buf));  
130         if( !new_buf )
131         {
132                 // Oh...
133                 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
134                         UDI_STAT_RESOURCE_UNAVAIL );
135                 return ;
136         }
137         rdata->rx_buffer = new_buf;
138
139         // Create interrupt CB
140         CONTIN( uart_bus_dev_bind, udi_cb_alloc, (UART_CB_INTR, gcb->channel),
141                 (udi_cb_t *new_cb))
142         if( !new_cb )
143         {
144                 // Oh...
145                 udi_channel_event_complete( UDI_MCB(rdata->active_cb, udi_channel_event_cb_t),
146                         UDI_STAT_RESOURCE_UNAVAIL );
147                 return ;
148         }
149         udi_intr_attach_cb_t    *intr_cb = UDI_MCB(new_cb, udi_intr_attach_cb_t);
150         intr_cb->interrupt_idx = 0;
151         intr_cb->min_event_pend = 1;
152         intr_cb->preprocessing_handle = rdata->pio_handles[PIO_INTR];
153         
154         // Attach interrupt
155         udi_intr_attach_req(intr_cb);
156         // continued in uart_bus_dev_intr_attach_ack
157 }
158 void uart_bus_dev_bus_unbind_ack(udi_bus_bind_cb_t *cb)
159 {
160 }
161 void uart_bus_dev_intr_attach_ack(udi_intr_attach_cb_t *intr_attach_cb, udi_status_t status)
162 {
163         udi_cb_t        *gcb = UDI_GCB(intr_attach_cb);
164         rdata_t *rdata = gcb->context;
165
166         // Allocate interrupt cbs
167         CONTIN(uart_bus_dev_intr_attach_ack, udi_cb_alloc_batch,
168                 (UART_CB_INTR_EVENT, NUM_INTR_CBS, TRUE, INTR_CB_BUF_SIZE, UDI_NULL_BUF_PATH),
169                 (udi_cb_t *first_new_cb))
170
171         while( first_new_cb )
172         {
173                 udi_intr_event_cb_t *cb = UDI_MCB(first_new_cb, udi_intr_event_cb_t);
174                 first_new_cb = first_new_cb->initiator_context;
175                 // - Set channel (udi_cb_alloc_batch sets the channel to gcb->channel)
176                 cb->gcb.channel = rdata->interrupt_channel;
177                 udi_intr_event_rdy(cb);
178         }
179
180         udi_channel_event_cb_t  *channel_cb = UDI_MCB(rdata->active_cb, udi_channel_event_cb_t);
181         
182         udi_channel_event_complete(channel_cb, UDI_OK);
183         // = = = = =
184 }
185 void uart_bus_dev_intr_detach_ack(udi_intr_detach_cb_t *intr_detach_cb)
186 {
187 }
188 // ---
189 void uart_bus_irq_channel_event_ind(udi_channel_event_cb_t *cb)
190 {
191 }
192 void uart_bus_irq_intr_event_ind(udi_intr_event_cb_t *cb, udi_ubit8_t flags)
193 {
194         udi_cb_t        *gcb = UDI_GCB(cb);
195         rdata_t *rdata = gcb->context;
196         
197         DEBUG_OUT("flags=%x, intr_result=%x", flags, cb->intr_result);
198         if( cb->intr_result == 0 )
199         {
200                 // An IRQ from something other than RX
201                 udi_intr_event_rdy(cb);
202                 return ;
203         }
204
205         if( rdata->rx_buffer && rdata->rx_buffer->buf_size + cb->intr_result > MAX_RX_BUFFER_SIZE )
206         {
207                 // Drop, buffer is full
208                 DEBUG_OUT("dropping %i bytes, full rx buffer", cb->intr_result);
209                 udi_intr_event_rdy(cb);
210                 return ;
211         }
212
213         // Copy cb->intr_result bytes into the RX buffer
214         CONTIN(uart_bus_irq_intr_event_ind, udi_buf_copy,
215                 (cb->event_buf, 0, cb->intr_result,
216                         rdata->rx_buffer, rdata->rx_buffer->buf_size, 0,
217                         UDI_NULL_BUF_PATH),
218                 (udi_buf_t *new_buf));
219         
220         udi_intr_event_cb_t     *cb = UDI_MCB(gcb, udi_intr_event_cb_t);
221         
222         rdata->rx_buffer = new_buf;
223
224         DEBUG_OUT("copied %i bytes", cb->intr_result);
225
226         // Emit a signal to GIO client
227         if( rdata->event_cb && !rdata->event_cb_used )
228         {
229                 rdata->event_pending = FALSE;
230                 rdata->event_cb_used = TRUE;
231                 udi_gio_event_ind(rdata->event_cb);
232         }
233         else
234         {
235                 DEBUG_OUT("event_cb=%p, event_cb_used=%i - Queueing",
236                         rdata->event_cb, rdata->event_cb_used);
237                 rdata->event_pending = TRUE;
238         }
239
240         udi_intr_event_rdy(cb);
241 }
242 // ---
243 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb);
244 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb);
245 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb);
246 void uart_gio_read_complete_short(udi_cb_t *gcb, udi_buf_t *new_buf);
247 void uart_gio_read_complete(udi_cb_t *gcb, udi_buf_t *new_buf);
248 void uart_gio_write_complete(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res);
249 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb);
250
251 void uart_gio_prov_channel_event_ind(udi_channel_event_cb_t *cb)
252 {
253 }
254 void uart_gio_prov_bind_req(udi_gio_bind_cb_t *cb)
255 {
256         udi_cb_t        *gcb = UDI_GCB(cb);
257         rdata_t *rdata = gcb->context;
258         
259         // TODO: Should this device allow multiple children?
260         if( rdata->event_cb ) {
261                 DEBUG_OUT("only one client allowed");
262                 udi_gio_bind_ack(cb, 0, 0, UDI_STAT_CANNOT_BIND_EXCL);
263                 return;
264         }
265         
266         // Allocate the event CB
267         CONTIN( uart_gio_prov_bind_req, udi_cb_alloc, (UART_CB_GIO_EVENT, gcb->channel),
268                 (udi_cb_t *new_cb));
269         udi_gio_bind_cb_t       *cb = UDI_MCB(gcb, udi_gio_bind_cb_t);
270         
271         rdata->event_cb = UDI_MCB(new_cb, udi_gio_event_cb_t);
272         rdata->event_cb_used = FALSE;
273         
274         udi_gio_bind_ack(cb, 0, 0, UDI_OK);
275 }
276 void uart_gio_prov_unbind_req(udi_gio_bind_cb_t *cb)
277 {
278         udi_cb_t        *gcb = UDI_GCB(cb);
279         rdata_t *rdata = gcb->context;
280         
281         rdata->event_cb = NULL;
282         rdata->event_cb_used = FALSE;
283         udi_gio_unbind_ack(cb);
284 }
285 void uart_gio_prov_xfer_req(udi_gio_xfer_cb_t *cb)
286 {
287         udi_cb_t        *gcb = UDI_GCB(cb);
288         rdata_t *rdata = gcb->context;
289         switch(cb->op)
290         {
291         case UDI_GIO_OP_READ: {
292                 udi_buf_copy_call_t     *callback;
293                 udi_size_t      len;
294                 // Read from local FIFO
295                 if( rdata->rx_buffer->buf_size < cb->data_buf->buf_size ) {
296                         len = rdata->rx_buffer->buf_size;
297                         callback = uart_gio_read_complete_short;
298                 }
299                 else {
300                         len = cb->data_buf->buf_size;
301                         callback = uart_gio_read_complete;
302                 }
303                 DEBUG_OUT("Read %i/%i bytes", len, rdata->rx_buffer->buf_size);
304                 // Replace entire destination buffer with `len` bytes from source
305                 udi_buf_copy(callback, gcb,
306                         rdata->rx_buffer, 0, len,
307                         cb->data_buf, 0, cb->data_buf->buf_size,
308                         UDI_NULL_BUF_PATH);
309                 break; }
310         case UDI_GIO_OP_WRITE:
311                 // Send to device
312                 udi_pio_trans(uart_gio_write_complete, gcb,
313                         rdata->pio_handles[PIO_TX], 0, cb->data_buf, &cb->data_buf->buf_size);
314                 break;
315         default:
316                 udi_gio_xfer_nak(cb, UDI_STAT_NOT_SUPPORTED);
317                 break;
318         }
319 }
320 void uart_gio_read_complete_short(udi_cb_t *gcb, udi_buf_t *new_buf)
321 {
322         rdata_t *rdata = gcb->context;
323         udi_gio_xfer_cb_t       *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
324         cb->data_buf = new_buf;
325         
326         // Delete read bytes from the RX buffer
327         CONTIN(uart_gio_read_complete, udi_buf_write,
328                 (NULL, 0, rdata->rx_buffer, 0, cb->data_buf->buf_size, UDI_NULL_BUF_PATH),
329                 (udi_buf_t *new_buf))
330         udi_gio_xfer_cb_t       *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
331         rdata->rx_buffer = new_buf;
332         
333         // Return underrun
334         udi_gio_xfer_nak(cb, UDI_STAT_DATA_UNDERRUN);
335 }
336 void uart_gio_read_complete(udi_cb_t *gcb, udi_buf_t *new_buf)
337 {
338         rdata_t *rdata = gcb->context;
339         udi_gio_xfer_cb_t       *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
340         cb->data_buf = new_buf;
341         // Delete read bytes from the RX buffer
342         CONTIN(uart_gio_read_complete, udi_buf_write,
343                 (NULL, 0, rdata->rx_buffer, 0, cb->data_buf->buf_size, UDI_NULL_BUF_PATH),
344                 (udi_buf_t *new_buf))
345         udi_gio_xfer_cb_t       *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
346         rdata->rx_buffer = new_buf;
347         
348         // Return completed
349         udi_gio_xfer_ack(cb);
350 }
351 void uart_gio_write_complete(udi_cb_t *gcb, udi_buf_t *new_buf, udi_status_t status, udi_ubit16_t res)
352 {
353         udi_gio_xfer_cb_t       *cb = UDI_MCB(gcb, udi_gio_xfer_cb_t);
354         cb->data_buf = new_buf;
355         udi_gio_xfer_ack(cb);
356 }
357 void uart_gio_prov_event_res(udi_gio_event_cb_t *cb)
358 {
359         udi_cb_t        *gcb = UDI_GCB(cb);
360         rdata_t *rdata = gcb->context;
361         
362         // If there was an event during dispatch, re-send
363         if( rdata->event_pending )
364         {
365                 udi_gio_event_ind(cb);
366                 rdata->event_pending = FALSE;
367         }
368         // otherwise, clear 'Event CB used' flag
369         else
370         {
371                 rdata->event_cb_used = FALSE;
372         }
373 }
374
375 // ====================================================================
376 // - Management ops
377 udi_mgmt_ops_t  uart_mgmt_ops = {
378         uart_usage_ind,
379         uart_enumerate_req,
380         uart_devmgmt_req,
381         uart_final_cleanup_req
382 };
383 udi_ubit8_t     uart_mgmt_op_flags[4] = {0,0,0,0};
384 // - Bus Ops
385 udi_bus_device_ops_t    uart_bus_dev_ops = {
386         uart_bus_dev_channel_event_ind,
387         uart_bus_dev_bus_bind_ack,
388         uart_bus_dev_bus_unbind_ack,
389         uart_bus_dev_intr_attach_ack,
390         uart_bus_dev_intr_detach_ack
391 };
392 udi_ubit8_t     uart_bus_dev_ops_flags[5] = {0};
393 // - IRQ Ops
394 udi_intr_handler_ops_t  uart_bus_irq_ops = {
395         uart_bus_irq_channel_event_ind,
396         uart_bus_irq_intr_event_ind
397 };
398 udi_ubit8_t     uart_bus_irq_ops_flags[2] = {0};
399 // - GIO provider ops
400 udi_gio_provider_ops_t  uart_gio_prov_ops = {
401         uart_gio_prov_channel_event_ind,
402         uart_gio_prov_bind_req,
403         uart_gio_prov_unbind_req,
404         uart_gio_prov_xfer_req,
405         uart_gio_prov_event_res
406 };
407 udi_ubit8_t     uart_gio_prov_ops_flags[5] = {0};
408 // --
409 udi_primary_init_t      uart_pri_init = {
410         .mgmt_ops = &uart_mgmt_ops,
411         .mgmt_op_flags = uart_mgmt_op_flags,
412         .mgmt_scratch_requirement = 0,
413         .enumeration_attr_list_length = 4,
414         .rdata_size = sizeof(rdata_t),
415         .child_data_size = 0,
416         .per_parent_paths = 0
417 };
418 udi_ops_init_t  uart_ops_list[] = {
419         {
420                 UART_OPS_DEV, UART_META_BUS, UDI_BUS_DEVICE_OPS_NUM,
421                 0,
422                 (udi_ops_vector_t*)&uart_bus_dev_ops,
423                 uart_bus_dev_ops_flags
424         },
425         {
426                 UART_OPS_IRQ, UART_META_BUS, UDI_BUS_INTR_HANDLER_OPS_NUM,
427                 0,
428                 (udi_ops_vector_t*)&uart_bus_irq_ops,
429                 uart_bus_irq_ops_flags
430         },
431         {
432                 UART_OPS_GIO, UART_META_GIO, UDI_GIO_PROVIDER_OPS_NUM,
433                 0,
434                 (udi_ops_vector_t*)&uart_gio_prov_ops,
435                 uart_gio_prov_ops_flags
436         },
437         {0}
438 };
439 udi_cb_init_t uart_cb_init_list[] = {
440         {UART_CB_BUS_BIND,   UART_META_BUS, UDI_BUS_BIND_CB_NUM, 0, 0,NULL},
441         {UART_CB_INTR,       UART_META_BUS, UDI_BUS_INTR_ATTACH_CB_NUM, 0, 0,NULL},
442         {UART_CB_INTR_EVENT, UART_META_BUS, UDI_BUS_INTR_EVENT_CB_NUM, 0, 0,NULL},
443         {UART_CB_GIO_EVENT,  UART_META_GIO, UDI_GIO_EVENT_CB_NUM, 0, 0,NULL},
444         {0}
445 };
446 const udi_init_t        udi_init_info = {
447         .primary_init_info = &uart_pri_init,
448         .ops_init_list = uart_ops_list,
449         .cb_init_list = uart_cb_init_list,
450 };

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