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