Remove unneeded include.
[bertos.git] / cpu / arm / drv / ser_at91.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  * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief ARM UART and SPI I/O driver
35  *
36  *
37  * \version $Id: ser_amr.c 18280 2007-10-11 15:14:20Z asterix $
38  * \author Daniele Basile <asterix@develer.com>
39  */
40
41 #include <io/arm.h>
42
43 #include <drv/ser.h>
44 #include <drv/ser_p.h>
45
46 #include <hw/hw_ser.h>  /* Required for bus macros overrides */
47 #include <hw/hw_cpu.h>  /* CLOCK_FREQ */
48
49 #include <mware/fifobuf.h>
50 #include <cfg/debug.h>
51
52 #include <appconfig.h>
53
54 #define SERIRQ_PRIORITY 4 ///< default priority for serial irqs.
55
56 /**
57  * \name Overridable serial bus hooks
58  *
59  * These can be redefined in hw.h to implement
60  * special bus policies such as half-duplex, 485, etc.
61  *
62  *
63  * \code
64  *  TXBEGIN      TXCHAR      TXEND  TXOFF
65  *    |   __________|__________ |     |
66  *    |   |   |   |   |   |   | |     |
67  *    v   v   v   v   v   v   v v     v
68  * ______  __  __  __  __  __  __  ________________
69  *       \/  \/  \/  \/  \/  \/  \/
70  * ______/\__/\__/\__/\__/\__/\__/
71  *
72  * \endcode
73  *
74  * \{
75  */
76
77 #ifndef SER_UART0_IRQ_INIT
78         /**
79          * Default IRQ INIT macro - invoked in uart0_init()
80          *
81          * - Disable all interrupt
82          * - Register USART0 interrupt
83          * - Enable USART0 clock.
84          */
85         #define SER_UART0_IRQ_INIT do { \
86                 US0_IDR = 0xFFFFFFFF; \
87                 /* Set the vector. */ \
88                 AIC_SVR(US0_ID) = uart0_irq_dispatcher; \
89                 /* Initialize to edge triggered with defined priority. */ \
90                 AIC_SMR(US0_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED | SERIRQ_PRIORITY; \
91                 /* Enable the USART IRQ */ \
92                 AIC_IECR = BV(US0_ID); \
93                 PMC_PCER = BV(US0_ID); \
94         } while (0)
95 #endif
96
97 #ifndef SER_UART0_BUS_TXINIT
98         /**
99          * Default TXINIT macro - invoked in uart0_init()
100          *
101          * - Disable GPIO on USART0 tx/rx pins
102          * - Reset USART0
103          * - Set serial param: mode Normal, 8bit data, 1bit stop
104          * - Enable both the receiver and the transmitter
105          * - Enable only the RX complete interrupt
106          */
107         #if CPU_ARM_AT91
108                 #define SER_UART0_BUS_TXINIT do { \
109                         PIOA_PDR = BV(5) | BV(6); \
110                         US0_CR = BV(US_RSTRX) | BV(US_RSTTX); \
111                         US0_MR = US_CHMODE_NORMAL | US_CHRL_8 | US_NBSTOP_1; \
112                         US0_CR = BV(US_RXEN) | BV(US_TXEN); \
113                         US0_IER = BV(US_RXRDY); \
114                 } while (0)
115         /*#elif  Add other ARM families here */
116         #else
117                 #error Unknown CPU
118         #endif
119
120 #endif
121
122 #ifndef SER_UART0_BUS_TXBEGIN
123         /**
124          * Invoked before starting a transmission
125          *
126          * - Enable both the receiver and the transmitter
127          * - Enable both the RX complete and TX empty interrupts
128          */
129         #define SER_UART0_BUS_TXBEGIN do { \
130                 US0_CR = BV(US_RXEN) | BV(US_TXEN); \
131                 US0_IER = BV(US_TXRDY) | BV(US_RXRDY); \
132         } while (0)
133 #endif
134
135 #ifndef SER_UART0_BUS_TXCHAR
136         /**
137          * Invoked to send one character.
138          */
139         #define SER_UART0_BUS_TXCHAR(c) do { \
140                 US0_THR = (c); \
141         } while (0)
142 #endif
143
144 #ifndef SER_UART0_BUS_TXEND
145         /**
146          * Invoked as soon as the txfifo becomes empty
147          *
148          * - Keep both the receiver and the transmitter enabled
149          * - Keep the RX complete interrupt enabled
150          * - Disable the TX empty interrupts
151          */
152         #define SER_UART0_BUS_TXEND do { \
153                 US0_CR = BV(US_RXEN) | BV(US_TXEN); \
154                 US0_IER = BV(US_RXRDY); \
155                 US0_IDR = BV(US_TXRDY); \
156         } while (0)
157 #endif
158
159 /* End USART0 macros */
160
161 #ifndef SER_UART1_IRQ_INIT
162         /** \sa SER_UART0_BUS_TXINIT */
163         #define SER_UART1_IRQ_INIT do { \
164                 US1_IDR = 0xFFFFFFFF; \
165                 /* Set the vector. */ \
166                 AIC_SVR(US1_ID) = uart1_irq_dispatcher; \
167                 /* Initialize to edge triggered with defined priority. */ \
168                 AIC_SMR(US1_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED | SERIRQ_PRIORITY; \
169                 /* Enable the USART IRQ */ \
170                 AIC_IECR = BV(US1_ID); \
171                 PMC_PCER = BV(US1_ID); \
172         } while (0)
173 #endif
174
175 #ifndef SER_UART1_BUS_TXINIT
176         /** \sa SER_UART1_BUS_TXINIT */
177         #if CPU_ARM_AT91
178                 #define SER_UART1_BUS_TXINIT do { \
179                         PIOA_PDR = BV(21) | BV(22); \
180                         US1_CR = BV(US_RSTRX) | BV(US_RSTTX); \
181                         US1_MR = US_CHMODE_NORMAL | US_CHRL_8 | US_NBSTOP_1; \
182                         US1_CR = BV(US_RXEN) | BV(US_TXEN); \
183                         US1_IER = BV(US_RXRDY); \
184                 } while (0)
185         /*#elif  Add other ARM families here */
186         #else
187                 #error Unknown CPU
188         #endif
189
190 #endif
191
192 #ifndef SER_UART1_BUS_TXBEGIN
193         /** \sa SER_UART1_BUS_TXBEGIN */
194         #define SER_UART1_BUS_TXBEGIN do { \
195                 US1_CR = BV(US_RXEN) | BV(US_TXEN); \
196                 US1_IER = BV(US_TXRDY) | BV(US_RXRDY); \
197         } while (0)
198 #endif
199
200 #ifndef SER_UART1_BUS_TXCHAR
201         /** \sa SER_UART1_BUS_TXCHAR */
202         #define SER_UART1_BUS_TXCHAR(c) do { \
203                 US1_THR = (c); \
204         } while (0)
205 #endif
206
207 #ifndef SER_UART1_BUS_TXEND
208         /** \sa SER_UART1_BUS_TXEND */
209         #define SER_UART1_BUS_TXEND do { \
210                 US1_CR = BV(US_RXEN) | BV(US_TXEN); \
211                 US1_IER = BV(US_RXRDY); \
212                 US1_IDR = BV(US_TXRDY); \
213         } while (0)
214 #endif
215
216 /**
217  * \def CONFIG_SER_STROBE
218  *
219  * This is a debug facility that can be used to
220  * monitor SER interrupt activity on an external pin.
221  *
222  * To use strobes, redefine the macros SER_STROBE_ON,
223  * SER_STROBE_OFF and SER_STROBE_INIT and set
224  * CONFIG_SER_STROBE to 1.
225  */
226 #if !defined(CONFIG_SER_STROBE) || !CONFIG_SER_STROBE
227         #define SER_STROBE_ON    do {/*nop*/} while(0)
228         #define SER_STROBE_OFF   do {/*nop*/} while(0)
229         #define SER_STROBE_INIT  do {/*nop*/} while(0)
230 #endif
231
232
233 /* From the high-level serial driver */
234 extern struct Serial ser_handles[SER_CNT];
235
236 /* TX and RX buffers */
237 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
238 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
239
240 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
241 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
242
243 /**
244  * Internal hardware state structure
245  *
246  * The \a sending variable is true while the transmission
247  * interrupt is retriggering itself.
248  *
249  * For the USARTs the \a sending flag is useful for taking specific
250  * actions before sending a burst of data, at the start of a trasmission
251  * but not before every char sent.
252  *
253  * For the SPI, this flag is necessary because the SPI sends and receives
254  * bytes at the same time and the SPI IRQ is unique for send/receive.
255  * The only way to start transmission is to write data in SPDR (this
256  * is done by spi_starttx()). We do this *only* if a transfer is
257  * not already started.
258  */
259 struct ArmSerial
260 {
261         struct SerialHardware hw;
262         volatile bool sending;
263 };
264
265
266 /*
267  * These are to trick GCC into *not* using absolute addressing mode
268  * when accessing ser_handles, which is very expensive.
269  *
270  * Accessing through these pointers generates much shorter
271  * (and hopefully faster) code.
272  */
273 struct Serial *ser_uart0 = &ser_handles[SER_UART0];
274 struct Serial *ser_uart1 = &ser_handles[SER_UART1];
275
276 /**
277  * Serial 0 TX interrupt handler
278  */
279 static void uart0_irq_tx(void)
280 {
281         SER_STROBE_ON;
282
283         struct FIFOBuffer * const txfifo = &ser_uart0->txfifo;
284
285         if (fifo_isempty(txfifo))
286         {
287                 SER_UART0_BUS_TXEND;
288         }
289         else
290         {
291                 char c = fifo_pop(txfifo);
292                 SER_UART0_BUS_TXCHAR(c);
293         }
294
295         SER_STROBE_OFF;
296 }
297
298 /**
299  * Serial 0 RX complete interrupt handler.
300  */
301 static void uart0_irq_rx(void)
302 {
303         SER_STROBE_ON;
304
305         /* Should be read before US_CRS */
306         ser_uart0->status |= US0_CSR & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
307
308         char c = US0_RHR;
309         struct FIFOBuffer * const rxfifo = &ser_uart0->rxfifo;
310
311         if (fifo_isfull(rxfifo))
312                 ser_uart0->status |= SERRF_RXFIFOOVERRUN;
313         else
314                 fifo_push(rxfifo, c);
315
316         SER_STROBE_OFF;
317 }
318
319 /**
320  * Serial IRQ dispatcher for USART0.
321  */
322 static void uart0_irq_dispatcher(void) __attribute__ ((naked));
323 static void uart0_irq_dispatcher(void)
324 {
325         IRQ_ENTRY();
326
327         if (US0_IMR & BV(US_RXRDY))
328                 uart0_irq_rx();
329
330         if (US0_IMR & BV(US_TXRDY))
331                 uart0_irq_tx();
332
333         IRQ_EXIT();
334 }
335
336 /**
337  * Serial 1 TX interrupt handler
338  */
339 static void uart1_irq_tx(void)
340 {
341         SER_STROBE_ON;
342
343         struct FIFOBuffer * const txfifo = &ser_uart1->txfifo;
344
345         if (fifo_isempty(txfifo))
346         {
347                 SER_UART1_BUS_TXEND;
348         }
349         else
350         {
351                 char c = fifo_pop(txfifo);
352                 SER_UART1_BUS_TXCHAR(c);
353         }
354
355         SER_STROBE_OFF;
356 }
357
358 /**
359  * Serial 1 RX complete interrupt handler.
360  */
361 static void uart1_irq_rx(void)
362 {
363         SER_STROBE_ON;
364
365         /* Should be read before US_CRS */
366         ser_uart1->status |= US1_CSR & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
367
368         char c = US1_RHR;
369         struct FIFOBuffer * const rxfifo = &ser_uart1->rxfifo;
370
371         if (fifo_isfull(rxfifo))
372                 ser_uart1->status |= SERRF_RXFIFOOVERRUN;
373         else
374                 fifo_push(rxfifo, c);
375
376         SER_STROBE_OFF;
377 }
378
379 /**
380  * Serial IRQ dispatcher for USART1.
381  */
382 static void uart1_irq_dispatcher(void) __attribute__ ((naked));
383 static void uart1_irq_dispatcher(void)
384 {
385         IRQ_ENTRY();
386
387         if (US1_IMR & BV(US_RXRDY))
388                 uart1_irq_rx();
389
390         if (US1_IMR & BV(US_TXRDY))
391                 uart1_irq_tx();
392
393         IRQ_EXIT();
394 }
395 /*
396  * Callbacks for USART0
397  */
398 static void uart0_init(
399         UNUSED_ARG(struct SerialHardware *, _hw),
400         UNUSED_ARG(struct Serial *, ser))
401 {
402         SER_UART0_IRQ_INIT;
403         SER_UART0_BUS_TXINIT;
404         SER_STROBE_INIT;
405 }
406
407 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
408 {
409         US0_CR = BV(US_RSTRX) | BV(US_RSTTX) | BV(US_RXDIS) | BV(US_TXDIS) | BV(US_RSTSTA);
410 }
411
412 static void uart0_enabletxirq(struct SerialHardware *_hw)
413 {
414         struct ArmSerial *hw = (struct ArmSerial *)_hw;
415
416         /*
417          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
418          * when it runs with an empty fifo.  The order of statements in the
419          * if-block matters.
420          */
421         if (!hw->sending)
422         {
423                 hw->sending = true;
424                 SER_UART0_BUS_TXBEGIN;
425         }
426 }
427
428 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
429 {
430         /* Compute baud-rate period */
431         US0_BRGR = CLOCK_FREQ / (16 * rate);
432         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
433 }
434
435 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
436 {
437         /* Set UART parity */
438         switch(parity)
439         {
440                 case SER_PARITY_NONE:
441                 {
442             /* Parity mode. */
443                         US0_MR |= US_PAR_MASK;
444                         break;
445                 }
446                 case SER_PARITY_EVEN:
447                 {
448             /* Even parity.*/
449                         US0_MR |= US_PAR_EVEN;
450                         break;
451                 }
452                 case SER_PARITY_ODD:
453                 {
454             /* Odd parity.*/
455                         US0_MR |= US_PAR_ODD;
456                         break;
457                 }
458         }
459
460 }
461 /*
462  * Callbacks for USART1
463  */
464 static void uart1_init(
465         UNUSED_ARG(struct SerialHardware *, _hw),
466         UNUSED_ARG(struct Serial *, ser))
467 {
468         SER_UART1_IRQ_INIT;
469         SER_UART1_BUS_TXINIT;
470         SER_STROBE_INIT;
471 }
472
473 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
474 {
475         US1_CR = BV(US_RSTRX) | BV(US_RSTTX) | BV(US_RXDIS) | BV(US_TXDIS) | BV(US_RSTSTA);
476 }
477
478 static void uart1_enabletxirq(struct SerialHardware *_hw)
479 {
480         struct ArmSerial *hw = (struct ArmSerial *)_hw;
481
482         /*
483          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
484          * when it runs with an empty fifo.  The order of statements in the
485          * if-block matters.
486          */
487         if (!hw->sending)
488         {
489                 hw->sending = true;
490                 SER_UART1_BUS_TXBEGIN;
491         }
492 }
493
494 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
495 {
496         /* Compute baud-rate period */
497         US1_BRGR = CLOCK_FREQ / (16 * rate);
498         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
499 }
500
501 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
502 {
503         /* Set UART parity */
504         switch(parity)
505         {
506                 case SER_PARITY_NONE:
507                 {
508             /* Parity mode. */
509                         US1_MR |= US_PAR_MASK;
510                         break;
511                 }
512                 case SER_PARITY_EVEN:
513                 {
514             /* Even parity.*/
515                         US1_MR |= US_PAR_EVEN;
516                         break;
517                 }
518                 case SER_PARITY_ODD:
519                 {
520             /* Odd parity.*/
521                         US1_MR |= US_PAR_ODD;
522                         break;
523                 }
524         }
525
526 }
527
528 static bool tx_sending(struct SerialHardware* _hw)
529 {
530         struct ArmSerial *hw = (struct ArmSerial *)_hw;
531         return hw->sending;
532 }
533
534 // FIXME: move into compiler.h?  Ditch?
535 #if COMPILER_C99
536         #define C99INIT(name,val) .name = val
537 #elif defined(__GNUC__)
538         #define C99INIT(name,val) name: val
539 #else
540         #warning No designated initializers, double check your code
541         #define C99INIT(name,val) (val)
542 #endif
543
544 /*
545  * High-level interface data structures
546  */
547 static const struct SerialHardwareVT UART0_VT =
548 {
549         C99INIT(init, uart0_init),
550         C99INIT(cleanup, uart0_cleanup),
551         C99INIT(setBaudrate, uart0_setbaudrate),
552         C99INIT(setParity, uart0_setparity),
553         C99INIT(txStart, uart0_enabletxirq),
554         C99INIT(txSending, tx_sending),
555 };
556
557 static const struct SerialHardwareVT UART1_VT =
558 {
559         C99INIT(init, uart1_init),
560         C99INIT(cleanup, uart1_cleanup),
561         C99INIT(setBaudrate, uart1_setbaudrate),
562         C99INIT(setParity, uart1_setparity),
563         C99INIT(txStart, uart1_enabletxirq),
564         C99INIT(txSending, tx_sending),
565 };
566
567 static struct ArmSerial UARTDescs[SER_CNT] =
568 {
569         {
570                 C99INIT(hw, /**/) {
571                         C99INIT(table, &UART0_VT),
572                         C99INIT(txbuffer, uart0_txbuffer),
573                         C99INIT(rxbuffer, uart0_rxbuffer),
574                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
575                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
576                 },
577                 C99INIT(sending, false),
578         },
579         {
580                 C99INIT(hw, /**/) {
581                         C99INIT(table, &UART1_VT),
582                         C99INIT(txbuffer, uart1_txbuffer),
583                         C99INIT(rxbuffer, uart1_rxbuffer),
584                         C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
585                         C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
586                 },
587                 C99INIT(sending, false),
588         }
589 };
590
591 struct SerialHardware *ser_hw_getdesc(int unit)
592 {
593         ASSERT(unit < SER_CNT);
594         return &UARTDescs[unit].hw;
595 }