]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/h8300/h8300h/v2_0/src/h8_sci.c
RedBoot TX53 Release 2012-02-15
[karo-tx-redboot.git] / packages / hal / h8300 / h8300h / v2_0 / src / h8_sci.c
1 //=============================================================================
2 //
3 //      h8_sci.c
4 //
5 //      Simple driver for the H8/300H Serial Communication Interface (SCI)
6 //
7 //=============================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //=============================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):   ysato
44 // Contributors:ysato
45 // Date:        2002-03-21
46 // Description: Simple driver for the H8/300H Serial Communication Interface
47 //              Clients of this file can configure the behavior with:
48 //              CYGNUM_SCI_PORTS:  number of SCI ports
49 //
50 //####DESCRIPTIONEND####
51 //
52 //=============================================================================
53
54 #include <pkgconf/hal.h>
55
56 #ifdef CYGNUM_HAL_H8300_SCI_PORTS
57
58 #include <cyg/hal/hal_io.h>             // IO macros
59 #include <cyg/hal/drv_api.h>            // CYG_ISR_HANDLED
60 #include <cyg/hal/hal_misc.h>           // Helper functions
61 #include <cyg/hal/hal_intr.h>           // HAL_ENABLE/MASK/UNMASK_INTERRUPTS
62 #include <cyg/hal/hal_arch.h>           // SAVE/RESTORE GP
63 #include <cyg/hal/hal_if.h>             // Calling-if API
64 #include <cyg/hal/mod_regs_sci.h>       // serial register definitions
65
66 #include <cyg/hal/h8_sci.h>             // our header
67
68 //--------------------------------------------------------------------------
69
70 void
71 cyg_hal_plf_sci_init_channel(void* chan)
72 {
73     cyg_uint8 tmp;
74     cyg_uint8* base = ((channel_data_t *)chan)->base;
75
76     // Disable Tx/Rx interrupts, but enable Tx/Rx
77     HAL_WRITE_UINT8(base+_REG_SCSCR,
78                     CYGARC_REG_SCSCR_TE|CYGARC_REG_SCSCR_RE);
79
80     // 8-1-no parity.
81     HAL_WRITE_UINT8(base+_REG_SCSMR, 0);
82
83     // Set speed to CYGNUM_HAL_H8300_H8300H_SCI_DEFAULT_BAUD_RATE
84     HAL_READ_UINT8(base+_REG_SCSMR, tmp);
85     tmp &= ~CYGARC_REG_SCSMR_CKSx_MASK;
86     tmp |= CYGARC_SCBRR_CKSx(CYGNUM_HAL_H8300_H8300H_SCI_BAUD_RATE);
87     HAL_WRITE_UINT8(base+_REG_SCSMR, tmp);
88     HAL_WRITE_UINT8(base+_REG_SCBRR, CYGARC_SCBRR_N(CYGNUM_HAL_H8300_H8300H_SCI_BAUD_RATE));
89 }
90
91 static cyg_bool
92 cyg_hal_plf_sci_getc_nonblock(void* __ch_data, cyg_uint8* ch)
93 {
94     cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
95     cyg_uint8 sr;
96
97     HAL_READ_UINT8(base+_REG_SCSSR, sr);
98     if (sr & CYGARC_REG_SCSSR_ORER) {
99         // Serial RX overrun. Clear error and let caller try again.
100         HAL_WRITE_UINT8(base+_REG_SCSSR, 
101                         CYGARC_REG_SCSSR_CLEARMASK & ~CYGARC_REG_SCSSR_ORER);
102         return false;
103     }
104
105     if ((sr & CYGARC_REG_SCSSR_RDRF) == 0)
106         return false;
107
108     HAL_READ_UINT8(base+_REG_SCRDR, *ch);
109
110     // Clear buffer full flag.
111     HAL_WRITE_UINT8(base+_REG_SCSSR, sr & ~CYGARC_REG_SCSSR_RDRF);
112
113     return true;
114 }
115
116 cyg_uint8
117 cyg_hal_plf_sci_getc(void* __ch_data)
118 {
119     cyg_uint8 ch;
120     CYGARC_HAL_SAVE_GP();
121
122     while(!cyg_hal_plf_sci_getc_nonblock(__ch_data, &ch));
123
124     CYGARC_HAL_RESTORE_GP();
125     return ch;
126 }
127
128 void
129 cyg_hal_plf_sci_putc(void* __ch_data, cyg_uint8 c)
130 {
131     cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
132     cyg_uint8 sr;
133     CYGARC_HAL_SAVE_GP();
134
135     do {
136         HAL_READ_UINT8(base+_REG_SCSSR, sr);
137     } while ((sr & CYGARC_REG_SCSSR_TDRE) == 0);
138
139     HAL_WRITE_UINT8(base+_REG_SCTDR, c);
140
141     // Clear empty flag.
142     HAL_WRITE_UINT8(base+_REG_SCSSR, sr & ~CYGARC_REG_SCSSR_TDRE);
143
144     // Hang around until the character has been safely sent.
145     do {
146         HAL_READ_UINT8(base+_REG_SCSSR, sr);
147     } while ((sr & CYGARC_REG_SCSSR_TDRE) == 0);
148
149     CYGARC_HAL_RESTORE_GP();
150 }
151
152
153 static channel_data_t channels[CYGNUM_HAL_H8300_SCI_PORTS];
154
155 static void
156 cyg_hal_plf_sci_write(void* __ch_data, const cyg_uint8* __buf, 
157                          cyg_uint32 __len)
158 {
159     CYGARC_HAL_SAVE_GP();
160
161     while(__len-- > 0)
162         cyg_hal_plf_sci_putc(__ch_data, *__buf++);
163
164     CYGARC_HAL_RESTORE_GP();
165 }
166
167 static void
168 cyg_hal_plf_sci_read(void* __ch_data, cyg_uint8* __buf, cyg_uint32 __len)
169 {
170     CYGARC_HAL_SAVE_GP();
171
172     while(__len-- > 0)
173         *__buf++ = cyg_hal_plf_sci_getc(__ch_data);
174
175     CYGARC_HAL_RESTORE_GP();
176 }
177
178 cyg_bool
179 cyg_hal_plf_sci_getc_timeout(void* __ch_data, cyg_uint8* ch)
180 {
181     channel_data_t* chan = (channel_data_t*)__ch_data;
182     int delay_count;
183     cyg_bool res;
184     CYGARC_HAL_SAVE_GP();
185
186     delay_count = chan->msec_timeout * 20; // delay in .1 ms steps
187
188     for(;;) {
189         res = cyg_hal_plf_sci_getc_nonblock(__ch_data, ch);
190         if (res || 0 == delay_count--)
191             break;
192         
193         CYGACC_CALL_IF_DELAY_US(50);
194     }
195
196     CYGARC_HAL_RESTORE_GP();
197     return res;
198 }
199
200 static int
201 cyg_hal_plf_sci_control(void *__ch_data, __comm_control_cmd_t __func, ...)
202 {
203     static int irq_state = 0;
204     channel_data_t* chan = (channel_data_t*)__ch_data;
205     cyg_uint8 scr;
206     int ret = 0;
207     CYGARC_HAL_SAVE_GP();
208
209     switch (__func) {
210     case __COMMCTL_IRQ_ENABLE:
211         irq_state = 1;
212         HAL_INTERRUPT_UNMASK(chan->isr_vector);
213         HAL_READ_UINT8(chan->base+_REG_SCSCR, scr);
214         scr |= CYGARC_REG_SCSCR_RIE;
215         HAL_WRITE_UINT8(chan->base+_REG_SCSCR, scr);
216         break;
217     case __COMMCTL_IRQ_DISABLE:
218         ret = irq_state;
219         irq_state = 0;
220         HAL_INTERRUPT_UNMASK(chan->isr_vector);
221         HAL_READ_UINT8(chan->base+_REG_SCSCR, scr);
222         scr &= ~CYGARC_REG_SCSCR_RIE;
223         HAL_WRITE_UINT8(chan->base+_REG_SCSCR, scr);
224         break;
225     case __COMMCTL_DBG_ISR_VECTOR:
226         ret = chan->isr_vector;
227         break;
228     case __COMMCTL_SET_TIMEOUT:
229     {
230         va_list ap;
231
232         va_start(ap, __func);
233
234         ret = chan->msec_timeout;
235         chan->msec_timeout = va_arg(ap, cyg_uint32);
236
237         va_end(ap);
238     }        
239     default:
240         break;
241     }
242     CYGARC_HAL_RESTORE_GP();
243     return ret;
244 }
245
246 static int
247 cyg_hal_plf_sci_isr(void *__ch_data, int* __ctrlc, 
248                     CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
249 {
250     cyg_uint8 c, sr;
251     cyg_uint8* base = ((channel_data_t*)__ch_data)->base;
252     int res = 0;
253     CYGARC_HAL_SAVE_GP();
254
255     *__ctrlc = 0;
256     HAL_READ_UINT8(base+_REG_SCSSR, sr);
257     if (sr & CYGARC_REG_SCSSR_ORER) {
258         // Serial RX overrun. Clear error and hope protocol recovers.
259         HAL_WRITE_UINT8(base+_REG_SCSSR, 
260                         CYGARC_REG_SCSSR_CLEARMASK & ~CYGARC_REG_SCSSR_ORER);
261         res = CYG_ISR_HANDLED;
262     } else if (sr & CYGARC_REG_SCSSR_RDRF) {
263         // Received character
264         HAL_READ_UINT8(base+_REG_SCRDR, c);
265
266         // Clear buffer full flag.
267         HAL_WRITE_UINT8(base+_REG_SCSSR, 
268                         CYGARC_REG_SCSSR_CLEARMASK & ~CYGARC_REG_SCSSR_RDRF);
269
270         if( cyg_hal_is_break( &c , 1 ) )
271             *__ctrlc = 1;
272
273         res = CYG_ISR_HANDLED;
274     }
275
276     CYGARC_HAL_RESTORE_GP();
277     return res;
278 }
279
280 void
281 cyg_hal_plf_sci_init(int sci_index, int comm_index, 
282                      int rcv_vect, cyg_uint8* base)
283 {
284     channel_data_t* chan = &channels[sci_index];
285     hal_virtual_comm_table_t* comm;
286     int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
287
288     // Initialize channel table
289     chan->base = base;
290     chan->isr_vector = rcv_vect;
291     chan->msec_timeout = 1000;
292
293     // Disable interrupts.
294     HAL_INTERRUPT_MASK(chan->isr_vector);
295
296     // Init channel
297     
298     cyg_hal_plf_sci_init_channel(chan);
299
300     // Setup procs in the vector table
301
302     // Initialize channel procs
303     CYGACC_CALL_IF_SET_CONSOLE_COMM(comm_index);
304     comm = CYGACC_CALL_IF_CONSOLE_PROCS();
305     CYGACC_COMM_IF_CH_DATA_SET(*comm, chan);
306     CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_sci_write);
307     CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_sci_read);
308     CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_sci_putc);
309     CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_sci_getc);
310     CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_sci_control);
311     CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_sci_isr);
312     CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_sci_getc_timeout);
313
314     // Restore original console
315     CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
316 }
317
318 #endif // CYGNUM_HAL_H8300_H8300H_SCI_PORTS
319
320 //-----------------------------------------------------------------------------
321 // end of sh_sci.c