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