0
|
1 /*****************************************************************
|
|
2 * uart.c
|
|
3 * by Zhiyi Huang, hzy@cs.otago.ac.nz
|
|
4 * University of Otago
|
|
5 *
|
|
6 ********************************************************************/
|
|
7
|
|
8
|
|
9
|
|
10 #include "types.h"
|
|
11 #include "defs.h"
|
|
12 #include "memlayout.h"
|
|
13 #include "traps.h"
|
|
14 #include "arm.h"
|
|
15
|
|
16 #define GPFSEL0 (MMIO_VA+0x200000)
|
|
17 #define GPFSEL1 (MMIO_VA+0x200004)
|
|
18 #define GPFSEL2 (MMIO_VA+0x200008)
|
|
19 #define GPFSEL3 (MMIO_VA+0x20000C)
|
|
20 #define GPFSEL4 (MMIO_VA+0x200010)
|
|
21 #define GPFSEL5 (MMIO_VA+0x200014)
|
|
22 #define GPSET0 (MMIO_VA+0x20001C)
|
|
23 #define GPSET1 (MMIO_VA+0x200020)
|
|
24 #define GPCLR0 (MMIO_VA+0x200028)
|
|
25 #define GPCLR1 (MMIO_VA+0x20002C)
|
|
26 #define GPPUD (MMIO_VA+0x200094)
|
|
27 #define GPPUDCLK0 (MMIO_VA+0x200098)
|
|
28 #define GPPUDCLK1 (MMIO_VA+0x20009C)
|
|
29
|
|
30 #define AUX_IRQ (MMIO_VA+0x215000)
|
|
31 #define AUX_ENABLES (MMIO_VA+0x215004)
|
|
32 #define AUX_MU_IO_REG (MMIO_VA+0x215040)
|
|
33 #define AUX_MU_IER_REG (MMIO_VA+0x215044)
|
|
34 #define AUX_MU_IIR_REG (MMIO_VA+0x215048)
|
|
35 #define AUX_MU_LCR_REG (MMIO_VA+0x21504C)
|
|
36 #define AUX_MU_MCR_REG (MMIO_VA+0x215050)
|
|
37 #define AUX_MU_LSR_REG (MMIO_VA+0x215054)
|
|
38 #define AUX_MU_MSR_REG (MMIO_VA+0x215058)
|
|
39 #define AUX_MU_SCRATCH (MMIO_VA+0x21505C)
|
|
40 #define AUX_MU_CNTL_REG (MMIO_VA+0x215060)
|
|
41 #define AUX_MU_STAT_REG (MMIO_VA+0x215064)
|
|
42 #define AUX_MU_BAUD_REG (MMIO_VA+0x215068)
|
|
43
|
|
44 void
|
|
45 setgpioval(uint pin, uint val)
|
|
46 {
|
|
47 uint sel, ssel, rsel, shift;
|
|
48
|
|
49 if(pin > 53) return;
|
|
50 if(pin >= 32) sel = 1; else sel = 0;
|
|
51 ssel = GPSET0 + (sel << 2);
|
|
52 rsel = GPCLR0 + (sel << 2);
|
|
53 if(sel) shift = (pin - 32) & 0x1f;
|
|
54 else shift = pin & 0x1f;
|
|
55 if(val == 0) outw(rsel, 1<<shift);
|
|
56 else outw(ssel, 1<<shift);
|
|
57 }
|
|
58
|
|
59
|
|
60 void
|
|
61 setgpiofunc(uint pin, uint func)
|
|
62 {
|
|
63 uint sel, data, shift;
|
|
64
|
|
65 if(pin > 53) return;
|
|
66 sel = 0;
|
|
67 while (pin > 10) {
|
|
68 pin = pin - 10;
|
|
69 sel++;
|
|
70 }
|
|
71 sel = (sel << 2) + GPFSEL0;
|
|
72 data = inw(sel);
|
|
73 shift = pin + (pin << 1);
|
|
74 data &= ~(7 << shift);
|
|
75 outw(sel, data);
|
|
76 data |= func << shift;
|
|
77 outw(sel, data);
|
|
78 }
|
|
79
|
|
80
|
|
81 void
|
|
82 uartputc(uint c)
|
|
83 {
|
|
84 if(c=='\n') {
|
|
85 while(1) if(inw(AUX_MU_LSR_REG) & 0x20) break;
|
|
86 outw(AUX_MU_IO_REG, 0x0d); // add CR before LF
|
|
87 }
|
|
88 while(1) if(inw(AUX_MU_LSR_REG) & 0x20) break;
|
|
89 outw(AUX_MU_IO_REG, c);
|
|
90 }
|
|
91
|
|
92 static int
|
|
93 uartgetc(void)
|
|
94 {
|
|
95 if(inw(AUX_MU_LSR_REG)&0x1) return inw(AUX_MU_IO_REG);
|
|
96 else return -1;
|
|
97 }
|
|
98
|
|
99 void
|
|
100 enableirqminiuart(void)
|
|
101 {
|
|
102 intctrlregs *ip;
|
|
103
|
|
104 ip = (intctrlregs *)INT_REGS_BASE;
|
|
105 ip->gpuenable[0] |= (1 << 29); // enable the miniuart through Aux
|
|
106 }
|
|
107
|
|
108
|
|
109 void
|
|
110 miniuartintr(void)
|
|
111 {
|
|
112 consoleintr(uartgetc);
|
|
113 }
|
|
114
|
|
115 void
|
|
116 uartinit(void)
|
|
117 {
|
|
118 outw(AUX_ENABLES, 1);
|
|
119 outw(AUX_MU_CNTL_REG, 0);
|
|
120 outw(AUX_MU_LCR_REG, 0x3);
|
|
121 outw(AUX_MU_MCR_REG, 0);
|
|
122 outw(AUX_MU_IER_REG, 0x1);
|
|
123 outw(AUX_MU_IIR_REG, 0xC7);
|
|
124 outw(AUX_MU_BAUD_REG, 270); // (250,000,000/(115200*8))-1 = 270
|
|
125
|
|
126 setgpiofunc(14, 2); // gpio 14, alt 5
|
|
127 setgpiofunc(15, 2); // gpio 15, alt 5
|
|
128
|
|
129 outw(GPPUD, 0);
|
|
130 delay(10);
|
|
131 outw(GPPUDCLK0, (1 << 14) | (1 << 15) );
|
|
132 delay(10);
|
|
133 outw(GPPUDCLK0, 0);
|
|
134
|
|
135 outw(AUX_MU_CNTL_REG, 3);
|
|
136 enableirqminiuart();
|
|
137 }
|