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