Add dual-license information.
[bertos.git] / drv / ser_dsp56k.c
1 /*!
2  * \file
3  * <!--
4  * Copyright (C) 2003,2004 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See devlib/README for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Stefano Fedrigo <aleph@develer.com>
11  * \author Giovanni Bajo <rasky@develer.com>
12  *
13  * \brief DSP5680x CPU specific serial I/O driver
14  */
15
16 /*
17  * $Log$
18  * Revision 1.3  2004/06/03 11:27:09  bernie
19  * Add dual-license information.
20  *
21  * Revision 1.2  2004/05/23 18:21:53  bernie
22  * Trim CVS logs and cleanup header info.
23  *
24  */
25
26 #include "ser.h"
27 #include "ser_p.h"
28 #include <drv/kdebug.h>
29 #include <hw.h>
30 #include <DSP56F807.H>
31
32 // GPIO E is shared with SPI (in DSP56807). Pins 0&1 are TXD0 and RXD0. To use
33 //  the serial, we need to disable the GPIO functions on them.
34 #define REG_GPIO_SERIAL         REG_GPIO_E
35 #define REG_GPIO_SERIAL_MASK    0x3
36
37 // Check flag consistency
38 #if (SERRF_PARITYERROR != REG_SCI_SR_PF) || \
39         (SERRF_RXSROVERRUN != REG_SCI_SR_OR) || \
40         (SERRF_FRAMEERROR  != REG_SCI_SR_FE) || \
41         (SERRF_NOISEERROR  != REG_SCI_SR_NF)
42         #error error flags do not match with register bits
43 #endif
44
45 struct SCI
46 {
47         struct SerialHardware hw;
48         struct Serial* serial;
49         volatile struct REG_SCI_STRUCT* regs;
50         uint16_t irq_tx;
51         uint16_t irq_rx;
52 };
53
54
55 static inline void enable_tx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
56 {
57         regs->CR |= REG_SCI_CR_TEIE | REG_SCI_CR_TIIE;
58 }
59
60 static inline void enable_rx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
61 {
62         regs->CR |= REG_SCI_CR_RIE | REG_SCI_CR_REIE;
63 }
64
65 static inline void disable_tx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
66 {
67         regs->CR &= ~(REG_SCI_CR_TEIE | REG_SCI_CR_TIIE);
68 }
69
70 static inline void disable_rx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
71 {
72         regs->CR &= ~(REG_SCI_CR_RIE | REG_SCI_CR_REIE);
73 }
74
75 static inline void disable_tx_irq(struct SerialHardware* _hw)
76 {
77         struct SCI* hw = (struct SCI*)_hw;
78         volatile struct REG_SCI_STRUCT* regs = hw->regs;
79
80         disable_tx_irq_bare(regs);
81 }
82
83 static inline void enable_tx_irq(struct SerialHardware* _hw)
84 {
85         struct SCI* hw = (struct SCI*)_hw;
86         volatile struct REG_SCI_STRUCT* regs = hw->regs;
87
88         enable_tx_irq_bare(regs);
89 }
90
91 static inline void enable_rx_irq(struct SerialHardware* _hw)
92 {
93         struct SCI* hw = (struct SCI*)_hw;
94         volatile struct REG_SCI_STRUCT* regs = hw->regs;
95
96         enable_rx_irq_bare(regs);
97 }
98
99 INLINE void tx_isr(struct SCI *hw)
100 {
101         volatile struct REG_SCI_STRUCT* regs = hw->regs;
102
103         if (fifo_isempty(&hw->serial->txfifo))
104                 disable_tx_irq_bare(regs);
105         else
106         {
107                 // Clear transmitter flags before sending data
108                 (void)regs->SR;
109                 regs->DR = fifo_pop(&hw->serial->txfifo);
110         }
111 }
112
113 INLINE void rx_isr(struct SCI *hw)
114 {
115         volatile struct REG_SCI_STRUCT* regs = hw->regs;
116
117         hw->serial->status |= regs->SR & (SERRF_PARITYERROR |
118                                           SERRF_RXSROVERRUN |
119                                           SERRF_FRAMEERROR |
120                                           SERRF_NOISEERROR);
121
122         if (fifo_isfull(&hw->serial->rxfifo))
123                 hw->serial->status |= SERRF_RXFIFOOVERRUN;
124         else
125                 fifo_push(&hw->serial->rxfifo, regs->DR);
126
127         // Writing anything to the status register clear the
128         //  error bits.
129         regs->SR = 0;
130 }
131
132 static void init(struct SerialHardware* _hw, struct Serial* ser)
133 {
134         struct SCI* hw = (struct SCI*)_hw;
135         volatile struct REG_SCI_STRUCT* regs = hw->regs;
136
137         // Clear status register (IRQ/status flags)
138         (void)regs->SR;
139         regs->SR = 0;
140
141         // Clear data register
142         (void)regs->DR;
143
144         // Set priorities for both IRQs
145         irq_setpriority(hw->irq_tx, IRQ_PRIORITY_SCI_TX);
146         irq_setpriority(hw->irq_rx, IRQ_PRIORITY_SCI_RX);
147
148         // Activate the RX error interrupts, and RX/TX transmissions
149         regs->CR = REG_SCI_CR_TE | REG_SCI_CR_RE;
150         enable_rx_irq_bare(regs);
151
152         // Disable GPIO pins for TX and RX lines
153         REG_GPIO_SERIAL->PER |= REG_GPIO_SERIAL_MASK;
154
155         hw->serial = ser;
156 }
157
158 static void cleanup(struct SerialHardware* _hw)
159 {
160         // TODO!
161         ASSERT(0);
162 }
163
164 static void setbaudrate(struct SerialHardware* _hw, unsigned long rate)
165 {
166         struct SCI* hw = (struct SCI*)_hw;
167
168         // SCI has an internal 16x divider on the input clock, which comes
169         //  from the IPbus (see the scheme in user manual, 12.7.3). We apply
170         //  it to calculate the period to store in the register.
171         hw->regs->BR = (IPBUS_FREQ + rate * 8ul) / (rate * 16ul);
172 }
173
174 static void setparity(struct SerialHardware* _hw, int parity)
175 {
176         // ???
177         ASSERT(0);
178 }
179
180
181 static const struct SerialHardwareVT SCI_VT = 
182 {
183         .init = init,
184         .cleanup = cleanup,
185         .setbaudrate = setbaudrate,
186         .setparity = setparity,
187         .enabletxirq = enable_tx_irq,
188 };
189
190 static struct SCI SCIDescs[2] =
191 {
192         {
193                 .hw = { .table = &SCI_VT },
194                 .regs = &REG_SCI[0],
195                 .irq_rx = IRQ_SCI0_RECEIVER_FULL,
196                 .irq_tx = IRQ_SCI0_TRANSMITTER_READY,
197         },
198
199         {
200                 .hw = { .table = &SCI_VT },
201                 .regs = &REG_SCI[1],
202                 .irq_rx = IRQ_SCI1_RECEIVER_FULL,
203                 .irq_tx = IRQ_SCI1_TRANSMITTER_READY,
204         },
205 };
206
207
208
209 void ser_hw_tx_isr_0(void);
210 void ser_hw_tx_isr_0(void)
211 {
212 #pragma interrupt warn
213         tx_isr(&SCIDescs[0]);
214 }
215
216 void ser_hw_rx_isr_0(void);
217 void ser_hw_rx_isr_0(void)
218 {
219 #pragma interrupt warn
220         rx_isr(&SCIDescs[0]);
221 }
222
223 void ser_hw_tx_isr_1(void);
224 void ser_hw_tx_isr_1(void)
225 {
226 #pragma interrupt warn
227         tx_isr(&SCIDescs[1]);
228 }
229
230 void ser_hw_rx_isr_1(void);
231 void ser_hw_rx_isr_1(void)
232 {
233 #pragma interrupt warn
234         rx_isr(&SCIDescs[1]);
235 }
236
237 struct SerialHardware* ser_hw_getdesc(int unit)
238 {
239         ASSERT(unit < 2);
240         return &SCIDescs[unit].hw;
241 }