ba39596a506b91b4bc46ef2f2ef20c7bb7fb95f5
[rpikern] / src / rpi.c
1 #include "rpi.h"
2 #include "asm.h"
3
4 #define IOREG(offs)             (*(volatile uint32_t*)(rpi_iobase | offs))
5
6 /* System timer */
7 #define STM_CTL_REG             IOREG(0x3000)
8 #define STM_STAT_REG    STM_CTL_REG
9 #define STM_LCNT_REG    IOREG(0x3004)
10 #define STM_HCNT_REG    IOREG(0x3008)
11 #define STM_CMP0_REG    IOREG(0x300c)
12 #define STM_CMP1_REG    IOREG(0x3010)
13 #define STM_CMP2_REG    IOREG(0x3014)
14 #define STM_CMP3_REG    IOREG(0x3018)
15
16 #define STMCTL_M0               1
17 #define STMCTL_M1               2
18 #define STMCTL_M2               4
19 #define STMCTL_M3               8
20
21 /* TIMER */
22 #define TM_LOAD_REG             IOREG(0xb400)
23 #define TM_VALUE_REG    IOREG(0xb404)
24 #define TM_CTL_REG              IOREG(0xb408)
25 #define TM_ICLR_REG             IOREG(0xb40c)
26 #define TM_IRAW_REG             IOREG(0xb410)
27 #define TM_IMSK_REG             IOREG(0xb414)
28 #define TM_RELOAD_REG   IOREG(0xb418)
29 #define TM_PREDIV_REG   IOREG(0xb41c)
30 #define TM_COUNT_REG    IOREG(0xb420)
31
32 #define TMCTL_23BIT             0x000002
33 #define TMCTL_DIV16             0x000004
34 #define TMCTL_DIV256    0x000008
35 #define TMCTL_DIV1              0x00000c
36 #define TMCTL_IEN               0x000020
37 #define TMCTL_EN                0x000080
38 #define TMCTL_DBGHALT   0x000100
39 #define TMCTL_CNTEN             0x000200
40
41 #define TMCTL_PRESCALER(x)      (((uint32_t)(x) & 0xff) << 16)
42
43
44 /* MAILBOX */
45 #define MBOX_READ_REG   IOREG(0xb880)
46 #define MBOX_POLL_REG   IOREG(0xb890)
47 #define MBOX_SENDER_REG IOREG(0xb894)
48 #define MBOX_STATUS_REG IOREG(0xb898)
49 #define MBOX_CFG_REG    IOREG(0xb89c)
50 #define MBOX_WRITE_REG  IOREG(0xb8a0)
51
52 /* the full bit is set when there's no space to append messages */
53 #define MBOX_STAT_FULL  0x80000000
54 /* the empty bit is set when there are no pending messages to be read */
55 #define MBOX_STAT_EMPTY 0x40000000
56
57 static int detect(void);
58
59 int rpi_model;
60 uint32_t rpi_iobase;
61 uint32_t rpi_memsize, rpi_vc_memsize;
62
63 void rpi_init(void)
64 {
65         if((rpi_model = detect()) == -1) {
66                 for(;;) halt_cpu();
67         }
68
69         /* TODO */
70 }
71
72 static int detect(void)
73 {
74         int i, j;
75         uint32_t tm0, tm1;
76         static uint32_t base[] = {0x20000000, 0x3f000000, 0xfe000000};
77
78         for(i=0; i<3; i++) {
79                 rpi_iobase = base[i];
80                 tm0 = STM_LCNT_REG;
81                 for(j=0; j<256; j++) {
82                         tm1 = STM_LCNT_REG;
83                 }
84                 if(tm0 != tm1) {
85                         return i + 1;
86                 }
87         }
88         return -1;
89 }
90
91
92 void rpi_mbox_send(int chan, uint32_t msg)
93 {
94         while(MBOX_STATUS_REG & MBOX_STAT_FULL);
95         MBOX_WRITE_REG = (msg & 0xfffffff0) | chan;
96 }
97
98 uint32_t rpi_mbox_recv(int chan)
99 {
100         uint32_t msg;
101         do {
102                 while(MBOX_STATUS_REG & MBOX_STAT_EMPTY);
103                 msg = MBOX_READ_REG;
104         } while((msg & 0xf) != chan);
105         return msg & 0xfffffff0;
106 }
107
108 int rpi_mbox_pending(int chan)
109 {
110         return (MBOX_STATUS_REG & MBOX_STAT_EMPTY) == 0;
111 }