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