Clean up the code. Manage the reconnection. Use the lwip error, insted
[bertos.git] / bertos / cpu / dsp56k / drv / ser_dsp56k.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  *
34  * \author Stefano Fedrigo <aleph@develer.com>
35  * \author Giovanni Bajo <rasky@develer.com>
36  *
37  * \brief DSP5680x CPU specific serial I/O driver
38  */
39
40
41 #include <drv/ser.h>
42 #include <drv/ser_p.h>
43 #include <drv/irq.h>
44 #include <cfg/debug.h>
45 #include <hw.h>
46 #include <DSP56F807.h>
47
48 // GPIO E is shared with SPI (in DSP56807). Pins 0&1 are TXD0 and RXD0. To use
49 //  the serial, we need to disable the GPIO functions on them.
50 #define REG_GPIO_SERIAL_0       REG_GPIO_E
51 #define REG_GPIO_SERIAL_MASK_0  0x03
52
53 #define REG_GPIO_SERIAL_1       REG_GPIO_D
54 #define REG_GPIO_SERIAL_MASK_1  0xC0
55
56
57 // Check flag consistency
58 #if (SERRF_PARITYERROR != REG_SCI_SR_PF) || \
59         (SERRF_RXSROVERRUN != REG_SCI_SR_OR) || \
60         (SERRF_FRAMEERROR  != REG_SCI_SR_FE) || \
61         (SERRF_NOISEERROR  != REG_SCI_SR_NF)
62         #error error flags do not match with register bits
63 #endif
64
65 static unsigned char ser0_fifo_rx[CONFIG_SER0_FIFOSIZE_RX];
66 static unsigned char ser0_fifo_tx[CONFIG_SER0_FIFOSIZE_TX];
67 static unsigned char ser1_fifo_rx[CONFIG_SER1_FIFOSIZE_RX];
68 static unsigned char ser1_fifo_tx[CONFIG_SER1_FIFOSIZE_TX];
69
70 #if CONFIG_SER_MULTI
71         #include <kern/sem.h>
72
73         #define MAX_MULTI_GROUPS     1
74
75         struct Semaphore multi_sems[MAX_MULTI_GROUPS];
76 #endif
77
78
79 struct SCI
80 {
81         struct SerialHardware hw;
82         struct Serial* serial;
83         volatile struct REG_SCI_STRUCT* regs;
84         IRQ_VECTOR irq_tx;
85         IRQ_VECTOR irq_rx;
86         int num_group;
87         int id;
88 };
89
90 static inline void enable_tx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
91 {
92         regs->CR |= REG_SCI_CR_TEIE | REG_SCI_CR_TIIE;
93 }
94
95 static inline void enable_rx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
96 {
97         regs->CR |= REG_SCI_CR_RIE;
98 }
99
100 static inline void disable_tx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
101 {
102         regs->CR &= ~(REG_SCI_CR_TEIE | REG_SCI_CR_TIIE);
103 }
104
105 static inline void disable_rx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
106 {
107         regs->CR &= ~(REG_SCI_CR_RIE | REG_SCI_CR_REIE);
108 }
109
110 static inline void disable_tx_irq(struct SerialHardware* _hw)
111 {
112         struct SCI* hw = (struct SCI*)_hw;
113
114         disable_tx_irq_bare(hw->regs);
115 }
116
117 static inline void disable_rx_irq(struct SerialHardware* _hw)
118 {
119         struct SCI* hw = (struct SCI*)_hw;
120
121         disable_rx_irq_bare(hw->regs);
122 }
123
124 static inline void enable_tx_irq(struct SerialHardware* _hw)
125 {
126         struct SCI* hw = (struct SCI*)_hw;
127
128         enable_tx_irq_bare(hw->regs);
129 }
130
131 static inline void enable_rx_irq(struct SerialHardware* _hw)
132 {
133         struct SCI* hw = (struct SCI*)_hw;
134
135         enable_rx_irq_bare(hw->regs);
136 }
137
138 static inline bool tx_irq_enabled(struct SerialHardware* _hw)
139 {
140         struct SCI* hw = (struct SCI*)_hw;
141
142         return (hw->regs->CR & REG_SCI_CR_TEIE);
143 }
144
145 static void tx_isr(const struct SCI *hw)
146 {
147 #pragma interrupt warn
148         volatile struct REG_SCI_STRUCT* regs = hw->regs;
149
150         if (fifo_isempty(&hw->serial->txfifo))
151                 disable_tx_irq_bare(regs);
152         else
153         {
154                 // Clear transmitter flags before sending data
155                 (void)regs->SR;
156                 regs->DR = fifo_pop(&hw->serial->txfifo);
157         }
158 }
159
160 static void rx_isr(const struct SCI *hw)
161 {
162 #pragma interrupt warn
163         volatile struct REG_SCI_STRUCT* regs = hw->regs;
164
165         // Propagate errors
166         hw->serial->status |= regs->SR & (SERRF_PARITYERROR |
167                                           SERRF_RXSROVERRUN |
168                                           SERRF_FRAMEERROR |
169                                           SERRF_NOISEERROR);
170
171         /*
172          * Serial IRQ can happen for two reason: data ready (RDRF) or overrun (OR)
173          * If the data is ready, we need to fetch it from the data register or
174          * the interrupt will retrigger immediatly. In case of overrun, instead,
175          * the value of the data register is meaningless.
176          */
177         if (regs->SR & REG_SCI_SR_RDRF)
178         {
179                 unsigned char data = regs->DR;
180
181                 if (fifo_isfull(&hw->serial->rxfifo))
182                         hw->serial->status |= SERRF_RXFIFOOVERRUN;
183                 else
184                         fifo_push(&hw->serial->rxfifo, data);
185         }
186
187         // Writing anything to the status register clear the error bits.
188         regs->SR = 0;
189 }
190
191 static void init(struct SerialHardware* _hw, struct Serial* ser)
192 {
193         struct SCI* hw = (struct SCI*)_hw;
194         volatile struct REG_SCI_STRUCT* regs = hw->regs;
195
196         // Clear status register (IRQ/status flags)
197         (void)regs->SR;
198         regs->SR = 0;
199
200         // Clear data register
201         (void)regs->DR;
202
203         // Install the handlers and set priorities for both IRQs
204         irq_install(hw->irq_tx, (isr_t)tx_isr, hw);
205         irq_install(hw->irq_rx, (isr_t)rx_isr, hw);
206         irq_setpriority(hw->irq_tx, IRQ_PRIORITY_SCI_TX);
207         irq_setpriority(hw->irq_rx, IRQ_PRIORITY_SCI_RX);
208
209         // Activate the RX error interrupts, and RX/TX transmissions
210         regs->CR = REG_SCI_CR_TE | REG_SCI_CR_RE;
211         enable_rx_irq_bare(regs);
212
213         // Disable GPIO pins for TX and RX lines
214         // \todo this should be divided into serial 0 and 1
215         REG_GPIO_SERIAL_0->PER |= REG_GPIO_SERIAL_MASK_0;
216         REG_GPIO_SERIAL_1->PER |= REG_GPIO_SERIAL_MASK_1;
217
218         hw->serial = ser;
219 }
220
221 static void cleanup(struct SerialHardware* _hw)
222 {
223         struct SCI* hw = (struct SCI*)_hw;
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         .txStart = enable_tx_irq,
301         .txSending = tx_irq_enabled,
302 };
303
304 #if CONFIG_SER_MULTI
305 static const struct SerialHardwareVT SCI_MULTI_VT =
306 {
307         .init = init_lock,
308         .cleanup = cleanup_unlock,
309         .setBaudrate = setbaudrate,
310         .setParity = setparity,
311         .txStart = enable_tx_irq,
312         .txSending = tx_irq_enabled,
313 };
314 #endif /* CONFIG_SER_MULTI */
315
316 #define SCI_DESC_NORMAL(hwch) \
317         { \
318                 .hw = \
319                 { \
320                         .table = &SCI_VT, \
321                         .rxbuffer = ser ## hwch ## _fifo_rx, \
322                         .txbuffer = ser ## hwch ## _fifo_tx, \
323                         .rxbuffer_size = countof(ser ## hwch ## _fifo_rx), \
324                         .txbuffer_size = countof(ser ## hwch ## _fifo_tx), \
325                 }, \
326                 .regs = &REG_SCI[hwch], \
327                 .irq_rx = IRQ_SCI ## hwch ## _RECEIVER_FULL, \
328                 .irq_tx = IRQ_SCI ## hwch ## _TRANSMITTER_READY, \
329                 .num_group = -1, \
330                 .id = -1, \
331         } \
332         /**/
333
334 #if CONFIG_SER_MULTI
335 #define SCI_DESC_MULTI(hwch, group_, id_) \
336         { \
337                 .hw = \
338                 { \
339                         .table = &SCI_MULTI_VT, \
340                         .rxbuffer = ser ## hwch ## _fifo_rx, \
341                         .txbuffer = ser ## hwch ## _fifo_tx, \
342                         .rxbuffer_size = countof(ser ## hwch ## _fifo_rx), \
343                         .txbuffer_size = countof(ser ## hwch ## _fifo_tx), \
344                 }, \
345                 .regs = &REG_SCI[hwch], \
346                 .irq_rx = IRQ_SCI ## hwch ## _RECEIVER_FULL, \
347                 .irq_tx = IRQ_SCI ## hwch ## _TRANSMITTER_READY, \
348                 .num_group = group_, \
349                 .id = id_, \
350         } \
351         /**/
352 #endif /* CONFIG_SER_MULTI */
353
354 // \todo Move this into hw.h, with a little preprocessor magic
355 static struct SCI SCIDescs[] =
356 {
357         SCI_DESC_NORMAL(0),
358         SCI_DESC_MULTI(1, 0, 0),
359         SCI_DESC_MULTI(1, 0, 1),
360 };
361
362 struct SerialHardware* ser_hw_getdesc(int unit)
363 {
364         ASSERT(unit < countof(SCIDescs));
365         return &SCIDescs[unit].hw;
366 }