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

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