Don't access serial data register twice.
[bertos.git] / drv / ser_dsp56k.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 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.8  2004/10/26 09:00:49  bernie
19  *#* Don't access serial data register twice.
20  *#*
21  *#* Revision 1.7  2004/10/19 08:57:15  bernie
22  *#* Bugfixes for DSP56K serial driver from scfirm.
23  *#*
24  *#* Revision 1.5  2004/08/25 14:12:08  rasky
25  *#* Aggiornato il comment block dei log RCS
26  *#*
27  *#* Revision 1.4  2004/07/30 14:27:49  rasky
28  *#* Aggiornati alcuni file DSP56k per la nuova libreria di IRQ management
29  *#*
30  *#* Revision 1.3  2004/06/03 11:27:09  bernie
31  *#* Add dual-license information.
32  *#*
33  *#* Revision 1.2  2004/05/23 18:21:53  bernie
34  *#* Trim CVS logs and cleanup header info.
35  *#*/
36
37 #include "ser.h"
38 #include "ser_p.h"
39 #include <drv/irq.h>
40 #include <debug.h>
41 #include <hw.h>
42 #include <DSP56F807.h>
43
44 // GPIO E is shared with SPI (in DSP56807). Pins 0&1 are TXD0 and RXD0. To use
45 //  the serial, we need to disable the GPIO functions on them.
46 #define REG_GPIO_SERIAL         REG_GPIO_E
47 #define REG_GPIO_SERIAL_MASK    0x3
48
49 // Check flag consistency
50 #if (SERRF_PARITYERROR != REG_SCI_SR_PF) || \
51         (SERRF_RXSROVERRUN != REG_SCI_SR_OR) || \
52         (SERRF_FRAMEERROR  != REG_SCI_SR_FE) || \
53         (SERRF_NOISEERROR  != REG_SCI_SR_NF)
54         #error error flags do not match with register bits
55 #endif
56
57 static unsigned char ser0_fifo_rx[CONFIG_SER0_FIFOSIZE_RX];
58 static unsigned char ser0_fifo_tx[CONFIG_SER0_FIFOSIZE_TX];
59 static unsigned char ser1_fifo_rx[CONFIG_SER1_FIFOSIZE_RX];
60 static unsigned char ser1_fifo_tx[CONFIG_SER1_FIFOSIZE_TX];
61
62 struct SCI
63 {
64         struct SerialHardware hw;
65         struct Serial* serial;
66         volatile struct REG_SCI_STRUCT* regs;
67         IRQ_VECTOR irq_tx;
68         IRQ_VECTOR irq_rx;
69 };
70
71 static inline void enable_tx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
72 {
73         regs->CR |= REG_SCI_CR_TEIE | REG_SCI_CR_TIIE;
74 }
75
76 static inline void enable_rx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
77 {
78         regs->CR |= REG_SCI_CR_RIE;
79 }
80
81 static inline void disable_tx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
82 {
83         regs->CR &= ~(REG_SCI_CR_TEIE | REG_SCI_CR_TIIE);
84 }
85
86 static inline void disable_rx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
87 {
88         regs->CR &= ~(REG_SCI_CR_RIE | REG_SCI_CR_REIE);
89 }
90
91 static inline void disable_tx_irq(struct SerialHardware* _hw)
92 {
93         struct SCI* hw = (struct SCI*)_hw;
94         volatile struct REG_SCI_STRUCT* regs = hw->regs;
95
96         disable_tx_irq_bare(regs);
97 }
98
99 static inline void enable_tx_irq(struct SerialHardware* _hw)
100 {
101         struct SCI* hw = (struct SCI*)_hw;
102         volatile struct REG_SCI_STRUCT* regs = hw->regs;
103
104         enable_tx_irq_bare(regs);
105 }
106
107 static inline void enable_rx_irq(struct SerialHardware* _hw)
108 {
109         struct SCI* hw = (struct SCI*)_hw;
110         volatile struct REG_SCI_STRUCT* regs = hw->regs;
111
112         enable_rx_irq_bare(regs);
113 }
114
115 static void tx_isr(const struct SCI *hw)
116 {
117 #pragma interrupt warn
118         volatile struct REG_SCI_STRUCT* regs = hw->regs;
119
120         if (fifo_isempty(&hw->serial->txfifo))
121                 disable_tx_irq_bare(regs);
122         else
123         {
124                 // Clear transmitter flags before sending data
125                 (void)regs->SR;
126                 regs->DR = fifo_pop(&hw->serial->txfifo);
127         }
128 }
129
130 static void rx_isr(const struct SCI *hw)
131 {
132 #pragma interrupt warn
133         volatile struct REG_SCI_STRUCT* regs = hw->regs;
134
135         // Propagate errors
136         hw->serial->status |= regs->SR & (SERRF_PARITYERROR |
137                                           SERRF_RXSROVERRUN |
138                                           SERRF_FRAMEERROR |
139                                           SERRF_NOISEERROR);
140
141         /*
142          * Serial IRQ can happen for two reason: data ready (RDRF) or overrun (OR)
143          * If the data is ready, we need to fetch it from the data register or
144          * the interrupt will retrigger immediatly. In case of overrun, instead,
145          * the value of the data register is meaningless.
146          */
147         if (regs->SR & REG_SCI_SR_RDRF)
148         {
149                 unsigned char data = regs->DR;
150
151                 if (fifo_isfull(&hw->serial->rxfifo))
152                         hw->serial->status |= SERRF_RXFIFOOVERRUN;
153                 else
154                         fifo_push(&hw->serial->rxfifo, data);
155         }
156
157         // Writing anything to the status register clear the error bits.
158         regs->SR = 0;
159 }
160
161 static void init(struct SerialHardware* _hw, struct Serial* ser)
162 {
163         struct SCI* hw = (struct SCI*)_hw;
164         volatile struct REG_SCI_STRUCT* regs = hw->regs;
165
166         // Clear status register (IRQ/status flags)
167         (void)regs->SR;
168         regs->SR = 0;
169
170         // Clear data register
171         (void)regs->DR;
172
173         // Install the handlers and set priorities for both IRQs
174         irq_install(hw->irq_tx, (isr_t)tx_isr, hw);
175         irq_install(hw->irq_rx, (isr_t)rx_isr, hw);
176         irq_setpriority(hw->irq_tx, IRQ_PRIORITY_SCI_TX);
177         irq_setpriority(hw->irq_rx, IRQ_PRIORITY_SCI_RX);
178
179         // Activate the RX error interrupts, and RX/TX transmissions
180         regs->CR = REG_SCI_CR_TE | REG_SCI_CR_RE;
181         enable_rx_irq_bare(regs);
182
183         // Disable GPIO pins for TX and RX lines
184         REG_GPIO_SERIAL->PER |= REG_GPIO_SERIAL_MASK;
185
186         hw->serial = ser;
187 }
188
189 static void cleanup(struct SerialHardware* _hw)
190 {
191         // TODO!
192         ASSERT(0);
193 }
194
195 static void setbaudrate(struct SerialHardware* _hw, unsigned long rate)
196 {
197         struct SCI* hw = (struct SCI*)_hw;
198
199         // SCI has an internal 16x divider on the input clock, which comes
200         //  from the IPbus (see the scheme in user manual, 12.7.3). We apply
201         //  it to calculate the period to store in the register.
202         hw->regs->BR = (IPBUS_FREQ + rate * 8ul) / (rate * 16ul);
203 }
204
205 static void setparity(struct SerialHardware* _hw, int parity)
206 {
207         // ???
208         ASSERT(0);
209 }
210
211
212 static const struct SerialHardwareVT SCI_VT = 
213 {
214         .init = init,
215         .cleanup = cleanup,
216         .setbaudrate = setbaudrate,
217         .setparity = setparity,
218         .enabletxirq = enable_tx_irq,
219 };
220
221 static struct SCI SCIDescs[2] =
222 {
223         {
224                 .hw =
225                 {
226                         .table = &SCI_VT,
227                         .rxbuffer = ser0_fifo_rx,
228                         .txbuffer = ser0_fifo_tx,
229                         .rxbuffer_size = countof(ser0_fifo_rx),
230                         .txbuffer_size = countof(ser0_fifo_tx),
231                 },
232                 .regs = &REG_SCI[0],
233                 .irq_rx = IRQ_SCI0_RECEIVER_FULL,
234                 .irq_tx = IRQ_SCI0_TRANSMITTER_READY,
235         },
236
237         {
238                 .hw =
239                 {
240                         .table = &SCI_VT,
241                         .rxbuffer = ser1_fifo_rx,
242                         .txbuffer = ser1_fifo_tx,
243                         .rxbuffer_size = countof(ser1_fifo_rx),
244                         .txbuffer_size = countof(ser1_fifo_tx),
245                 },
246                 .regs = &REG_SCI[1],
247                 .irq_rx = IRQ_SCI1_RECEIVER_FULL,
248                 .irq_tx = IRQ_SCI1_TRANSMITTER_READY,
249         },
250 };
251
252
253 struct SerialHardware* ser_hw_getdesc(int unit)
254 {
255         ASSERT(unit < 2);
256         return &SCIDescs[unit].hw;
257 }