Add support for multiplexed serial ports.
[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.9  2004/12/08 09:42:55  bernie
19  *#* Add support for multiplexed serial ports.
20  *#*
21  *#* Revision 1.8  2004/10/26 09:00:49  bernie
22  *#* Don't access serial data register twice.
23  *#*
24  *#* Revision 1.7  2004/10/19 08:57:15  bernie
25  *#* Bugfixes for DSP56K serial driver from scfirm.
26  *#*
27  *#* Revision 1.5  2004/08/25 14:12:08  rasky
28  *#* Aggiornato il comment block dei log RCS
29  *#*
30  *#* Revision 1.4  2004/07/30 14:27:49  rasky
31  *#* Aggiornati alcuni file DSP56k per la nuova libreria di IRQ management
32  *#*
33  *#* Revision 1.3  2004/06/03 11:27:09  bernie
34  *#* Add dual-license information.
35  *#*
36  *#* Revision 1.2  2004/05/23 18:21:53  bernie
37  *#* Trim CVS logs and cleanup header info.
38  *#*/
39
40 #include "ser.h"
41 #include "ser_p.h"
42 #include <drv/irq.h>
43 #include <debug.h>
44 #include <hw.h>
45 #include <DSP56F807.h>
46
47 // GPIO E is shared with SPI (in DSP56807). Pins 0&1 are TXD0 and RXD0. To use
48 //  the serial, we need to disable the GPIO functions on them.
49 #define REG_GPIO_SERIAL_0       REG_GPIO_E
50 #define REG_GPIO_SERIAL_MASK_0  0x03
51
52 #define REG_GPIO_SERIAL_1       REG_GPIO_D
53 #define REG_GPIO_SERIAL_MASK_1  0xC0
54
55
56 // Check flag consistency
57 #if (SERRF_PARITYERROR != REG_SCI_SR_PF) || \
58         (SERRF_RXSROVERRUN != REG_SCI_SR_OR) || \
59         (SERRF_FRAMEERROR  != REG_SCI_SR_FE) || \
60         (SERRF_NOISEERROR  != REG_SCI_SR_NF)
61         #error error flags do not match with register bits
62 #endif
63
64 static unsigned char ser0_fifo_rx[CONFIG_SER0_FIFOSIZE_RX];
65 static unsigned char ser0_fifo_tx[CONFIG_SER0_FIFOSIZE_TX];
66 static unsigned char ser1_fifo_rx[CONFIG_SER1_FIFOSIZE_RX];
67 static unsigned char ser1_fifo_tx[CONFIG_SER1_FIFOSIZE_TX];
68
69 #if CONFIG_SER_MULTI
70         #include <kern/sem.h>
71
72         #define MAX_MULTI_GROUPS     1
73
74         struct Semaphore multi_sems[MAX_MULTI_GROUPS];
75 #endif
76
77
78 struct SCI
79 {
80         struct SerialHardware hw;
81         struct Serial* serial;
82         volatile struct REG_SCI_STRUCT* regs;
83         IRQ_VECTOR irq_tx;
84         IRQ_VECTOR irq_rx;
85         int num_group;
86         int id;
87 };
88
89 static inline void enable_tx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
90 {
91         regs->CR |= REG_SCI_CR_TEIE | REG_SCI_CR_TIIE;
92 }
93
94 static inline void enable_rx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
95 {
96         regs->CR |= REG_SCI_CR_RIE;
97 }
98
99 static inline void disable_tx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
100 {
101         regs->CR &= ~(REG_SCI_CR_TEIE | REG_SCI_CR_TIIE);
102 }
103
104 static inline void disable_rx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
105 {
106         regs->CR &= ~(REG_SCI_CR_RIE | REG_SCI_CR_REIE);
107 }
108
109 static inline void disable_tx_irq(struct SerialHardware* _hw)
110 {
111         struct SCI* hw = (struct SCI*)_hw;
112         volatile struct REG_SCI_STRUCT* regs = hw->regs;
113
114         disable_tx_irq_bare(regs);
115 }
116
117 static inline void disable_rx_irq(struct SerialHardware* _hw)
118 {
119         struct SCI* hw = (struct SCI*)_hw;
120         volatile struct REG_SCI_STRUCT* regs = hw->regs;
121
122         disable_rx_irq_bare(regs);
123 }
124
125 static inline void enable_tx_irq(struct SerialHardware* _hw)
126 {
127         struct SCI* hw = (struct SCI*)_hw;
128         volatile struct REG_SCI_STRUCT* regs = hw->regs;
129
130         enable_tx_irq_bare(regs);
131 }
132
133 static inline void enable_rx_irq(struct SerialHardware* _hw)
134 {
135         struct SCI* hw = (struct SCI*)_hw;
136         volatile struct REG_SCI_STRUCT* regs = hw->regs;
137
138         enable_rx_irq_bare(regs);
139 }
140
141 static void tx_isr(const struct SCI *hw)
142 {
143 #pragma interrupt warn
144         volatile struct REG_SCI_STRUCT* regs = hw->regs;
145
146         if (fifo_isempty(&hw->serial->txfifo))
147                 disable_tx_irq_bare(regs);
148         else
149         {
150                 // Clear transmitter flags before sending data
151                 (void)regs->SR;
152                 regs->DR = fifo_pop(&hw->serial->txfifo);
153         }
154 }
155
156 static void rx_isr(const struct SCI *hw)
157 {
158 #pragma interrupt warn
159         volatile struct REG_SCI_STRUCT* regs = hw->regs;
160
161         // Propagate errors
162         hw->serial->status |= regs->SR & (SERRF_PARITYERROR |
163                                           SERRF_RXSROVERRUN |
164                                           SERRF_FRAMEERROR |
165                                           SERRF_NOISEERROR);
166
167         /*
168          * Serial IRQ can happen for two reason: data ready (RDRF) or overrun (OR)
169          * If the data is ready, we need to fetch it from the data register or
170          * the interrupt will retrigger immediatly. In case of overrun, instead,
171          * the value of the data register is meaningless.
172          */
173         if (regs->SR & REG_SCI_SR_RDRF)
174         {
175                 unsigned char data = regs->DR;
176
177                 if (fifo_isfull(&hw->serial->rxfifo))
178                         hw->serial->status |= SERRF_RXFIFOOVERRUN;
179                 else
180                         fifo_push(&hw->serial->rxfifo, data);
181         }
182
183         // Writing anything to the status register clear the error bits.
184         regs->SR = 0;
185 }
186
187 static void init(struct SerialHardware* _hw, struct Serial* ser)
188 {
189         struct SCI* hw = (struct SCI*)_hw;
190         volatile struct REG_SCI_STRUCT* regs = hw->regs;
191
192         // Clear status register (IRQ/status flags)
193         (void)regs->SR;
194         regs->SR = 0;
195
196         // Clear data register
197         (void)regs->DR;
198
199         // Install the handlers and set priorities for both IRQs
200         irq_install(hw->irq_tx, (isr_t)tx_isr, hw);
201         irq_install(hw->irq_rx, (isr_t)rx_isr, hw);
202         irq_setpriority(hw->irq_tx, IRQ_PRIORITY_SCI_TX);
203         irq_setpriority(hw->irq_rx, IRQ_PRIORITY_SCI_RX);
204
205         // Activate the RX error interrupts, and RX/TX transmissions
206         regs->CR = REG_SCI_CR_TE | REG_SCI_CR_RE;
207         enable_rx_irq_bare(regs);
208
209         // Disable GPIO pins for TX and RX lines
210         // \todo this should be divided into serial 0 and 1
211         REG_GPIO_SERIAL_0->PER |= REG_GPIO_SERIAL_MASK_0;
212         REG_GPIO_SERIAL_1->PER |= REG_GPIO_SERIAL_MASK_1;
213
214         hw->serial = ser;
215 }
216
217 static void cleanup(struct SerialHardware* _hw)
218 {
219         struct SCI* hw = (struct SCI*)_hw;
220
221         // Wait until we finish sending everything
222         ser_drain(hw->serial);
223         ser_purge(hw->serial);
224
225         // Uninstall the ISRs
226         disable_rx_irq(_hw);
227         disable_tx_irq(_hw);
228         irq_uninstall(hw->irq_tx);
229         irq_uninstall(hw->irq_rx);
230
231
232 static void setbaudrate(struct SerialHardware* _hw, unsigned long rate)
233 {
234         struct SCI* hw = (struct SCI*)_hw;
235
236         // SCI has an internal 16x divider on the input clock, which comes
237         //  from the IPbus (see the scheme in user manual, 12.7.3). We apply
238         //  it to calculate the period to store in the register.
239         hw->regs->BR = (IPBUS_FREQ + rate * 8ul) / (rate * 16ul);
240 }
241
242 static void setparity(struct SerialHardware* _hw, int parity)
243 {
244         // ???
245         ASSERT(0);
246 }
247
248
249 #if CONFIG_SER_MULTI
250
251 static void multi_init(void)
252 {
253         static bool flag = false;
254         int i;
255
256         if (flag)
257                 return;
258
259         for (i = 0; i < MAX_MULTI_GROUPS; ++i)
260                 sem_init(&multi_sems[i]);
261         flag = true;
262 }
263
264 static void init_lock(struct SerialHardware* _hw, struct Serial *ser)
265 {
266         struct SCI* hw = (struct SCI*)_hw;
267
268         // Initialize the multi engine (if needed)
269         multi_init();
270
271         // Acquire the lock of the semaphore for this group
272         ASSERT(hw->num_group >= 0);
273         ASSERT(hw->num_group < MAX_MULTI_GROUPS);
274         sem_obtain(&multi_sems[hw->num_group]);
275
276         // Do a hardware switch to the given serial
277         ser_hw_switch(hw->num_group, hw->id);
278
279         init(_hw, ser);
280 }
281
282 static void cleanup_unlock(struct SerialHardware* _hw)
283 {
284         struct SCI* hw = (struct SCI*)_hw;
285
286         cleanup(_hw);
287
288         sem_release(&multi_sems[hw->num_group]);
289 }
290
291 #endif /* CONFIG_SER_MULTI */
292
293
294 static const struct SerialHardwareVT SCI_VT =
295 {
296         .init = init,
297         .cleanup = cleanup,
298         .setbaudrate = setbaudrate,
299         .setparity = setparity,
300         .enabletxirq = enable_tx_irq,
301 };
302
303 #if CONFIG_SER_MULTI
304 static const struct SerialHardwareVT SCI_MULTI_VT =
305 {
306         .init = init_lock,
307         .cleanup = cleanup_unlock,
308         .setbaudrate = setbaudrate,
309         .setparity = setparity,
310         .enabletxirq = enable_tx_irq,
311 };
312 #endif /* CONFIG_SER_MULTI */
313
314 #define SCI_DESC_NORMAL(hwch) \
315         { \
316                 .hw = \
317                 { \
318                         .table = &SCI_VT, \
319                         .rxbuffer = ser ## hwch ## _fifo_rx, \
320                         .txbuffer = ser ## hwch ## _fifo_tx, \
321                         .rxbuffer_size = countof(ser ## hwch ## _fifo_rx), \
322                         .txbuffer_size = countof(ser ## hwch ## _fifo_tx), \
323                 }, \
324                 .regs = &REG_SCI[hwch], \
325                 .irq_rx = IRQ_SCI ## hwch ## _RECEIVER_FULL, \
326                 .irq_tx = IRQ_SCI ## hwch ## _TRANSMITTER_READY, \
327                 .num_group = -1, \
328                 .id = -1, \
329         } \
330         /**/
331
332 #if CONFIG_SER_MULTI
333 #define SCI_DESC_MULTI(hwch, group_, id_) \
334         { \
335                 .hw = \
336                 { \
337                         .table = &SCI_MULTI_VT, \
338                         .rxbuffer = ser ## hwch ## _fifo_rx, \
339                         .txbuffer = ser ## hwch ## _fifo_tx, \
340                         .rxbuffer_size = countof(ser ## hwch ## _fifo_rx), \
341                         .txbuffer_size = countof(ser ## hwch ## _fifo_tx), \
342                 }, \
343                 .regs = &REG_SCI[hwch], \
344                 .irq_rx = IRQ_SCI ## hwch ## _RECEIVER_FULL, \
345                 .irq_tx = IRQ_SCI ## hwch ## _TRANSMITTER_READY, \
346                 .num_group = group_, \
347                 .id = id_, \
348         } \
349         /**/
350 #endif /* CONFIG_SER_MULTI */
351
352 // \todo Move this into hw.h, with a little preprocessor magic
353 static struct SCI SCIDescs[] =
354 {
355         SCI_DESC_NORMAL(0),
356         SCI_DESC_MULTI(1, 0, 0),
357         SCI_DESC_MULTI(1, 0, 1),
358 };
359
360 struct SerialHardware* ser_hw_getdesc(int unit)
361 {
362         ASSERT(unit < countof(SCIDescs));
363         return &SCIDescs[unit].hw;
364 }