mbox.c 11.0 KB
Newer Older
B
bigamgic 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*
 * File      : mbox.c
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author         Notes
 * 2019-08-29     zdzn           first version
 * 2020-09-10     bigmagic       add other mbox option
 */

/* mailbox message buffer */
#include "mbox.h"
#include "mmu.h"
//volatile unsigned int  __attribute__((aligned(16))) mbox[36];
volatile unsigned int *mbox = (volatile unsigned int *) MBOX_ADDR;
#define BUS_ADDRESS(phys)	(((phys) & ~0xC0000000)  |  0xC0000000)

/**
 * Make a mailbox call. Returns 0 on failure, non-zero on success
 */
int mbox_call(unsigned char ch, int mmu_enable)
{
    unsigned int r = (((MBOX_ADDR)&~0xF) | (ch&0xF));
    if(mmu_enable)
        r = BUS_ADDRESS(r);
    /* wait until we can write to the mailbox */
    do
    {
        asm volatile("nop");
    } while (*MBOX_STATUS & MBOX_FULL);
    /* write the address of our message to the mailbox with channel identifier */
    *MBOX_WRITE = r;
    /* now wait for the response */
    while(1)
    {
        /* is there a response? */
        do
        {
            asm volatile("nop");
        } while (*MBOX_STATUS & MBOX_EMPTY);
        /* is it a response to our message? */
        if (r == *MBOX_READ){
            /* is it a valid successful response? */
            return mbox[1] == MBOX_RESPONSE;
        }
    }
    return 0;
}

B
bigamgic 已提交
52
int bcm271x_mbox_hardware_get_model(void)
B
bigamgic 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
{
    mbox[0] = 8*4;                          // length of the message
    mbox[1] = MBOX_REQUEST;                 // this is a request message
    
    mbox[2] = MBOX_TAG_HARDWARE_GET_MODEL;
    mbox[3] = 4;                            // buffer size
    mbox[4] = 0;                            // len

    mbox[5] = 0;                            
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    return mbox[5];
}

B
bigamgic 已提交
70
int bcm271x_mbox_hardware_get_revison(void)
B
bigamgic 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
    mbox[0] = 8*4;                          // length of the message
    mbox[1] = MBOX_REQUEST;                 // this is a request message
    
    mbox[2] = MBOX_TAG_HARDWARE_GET_REV;   
    mbox[3] = 4;                            // buffer size
    mbox[4] = 0;                            // len

    mbox[5] = 0;                    
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    return mbox[5];
}

B
bigamgic 已提交
88
int bcm271x_mbox_hardware_get_mac_address(uint8_t * mac)
B
bigamgic 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
{
    mbox[0] = 8*4;                                 // length of the message
    mbox[1] = MBOX_REQUEST;                        // this is a request message
    
    mbox[2] = MBOX_TAG_HARDWARE_GET_MAC_ADDRESS;   
    mbox[3] = 6;                                   // buffer size
    mbox[4] = 0;                                   // len

    mbox[5] = 0;                    
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    char * mac_str = (char *)&mbox[5];
    mac[0] = mac_str[0];
    mac[1] = mac_str[1];
    mac[2] = mac_str[2];
    mac[3] = mac_str[3];
    mac[4] = mac_str[4];
    mac[5] = mac_str[5];
    return 0;
}


B
bigamgic 已提交
114
int bcm271x_mbox_hardware_get_serial(rt_uint64_t* sn)
B
bigamgic 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
{
    mbox[0] = 8*4;                              // length of the message
    mbox[1] = MBOX_REQUEST;                     // this is a request message
    
    mbox[2] = MBOX_TAG_HARDWARE_GET_SERIAL;    
    mbox[3] = 8;                                // buffer size
    mbox[4] = 0;                                // len

    mbox[5] = 0;                    
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    sn = (rt_uint64_t *)&mbox[5];

    return 0;
}

B
bigamgic 已提交
134
int bcm271x_mbox_hardware_get_arm_memory(rt_uint32_t * base, rt_uint32_t * size)
B
bigamgic 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
{
    mbox[0] = 8*4;                                  // length of the message
    mbox[1] = MBOX_REQUEST;                         // this is a request message
    
    mbox[2] = MBOX_TAG_HARDWARE_GET_ARM_MEMORY;   
    mbox[3] = 8;                                    // buffer size
    mbox[4] = 0;                                    // len

    mbox[5] = 0;                    
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    *base = mbox[5];
    *size = mbox[6];
    
    return 0;

}

B
bigamgic 已提交
156
int bcm271x_mbox_hardware_get_vc_memory(rt_uint32_t * base, rt_uint32_t * size)
B
bigamgic 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
{
    mbox[0] = 8*4;                               // length of the message
    mbox[1] = MBOX_REQUEST;                      // this is a request message
    
    mbox[2] = MBOX_TAG_HARDWARE_GET_VC_MEMORY;
    mbox[3] = 8;                                 // buffer size
    mbox[4] = 0;                                 // len

    mbox[5] = 0;                    
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    *base = mbox[5];
    *size = mbox[6];
    
    return 0;
}

B
bigamgic 已提交
177
int bcm271x_mbox_clock_get_turbo(void)
B
bigamgic 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
{
    mbox[0] = 8*4;                      // length of the message
    mbox[1] = MBOX_REQUEST;             // this is a request message
    
    mbox[2] = MBOX_TAG_CLOCK_GET_TURBO; 
    mbox[3] = 8;                        // buffer size
    mbox[4] = 4;                        // len

    mbox[5] = 0;                        // id
    mbox[6] = 0;                        // val

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != 0)
    {
        return -1;
    }

    return mbox[6];
}

B
bigamgic 已提交
200
int bcm271x_mbox_clock_set_turbo(int level)
B
bigamgic 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
{
    mbox[0] = 8*4;                      // length of the message
    mbox[1] = MBOX_REQUEST;             // this is a request message
    
    mbox[2] = MBOX_TAG_CLOCK_SET_TURBO;  
    mbox[3] = 8;                        // buffer size
    mbox[4] = 8;                        // len

    mbox[5] = 0;                        // id
    mbox[6] = level ? 1 : 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != 0)
    {
        return -1;
    }

    return mbox[6];
}

B
bigamgic 已提交
223
int bcm271x_mbox_clock_get_state(int id)
B
bigamgic 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
{
    mbox[0] = 8*4;                       // length of the message
    mbox[1] = MBOX_REQUEST;              // this is a request message
    
    mbox[2] = MBOX_TAG_CLOCK_GET_STATE;
    mbox[3] = 8;                         // buffer size
    mbox[4] = 4;                         // len

    mbox[5] = id;                        // id
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != id)
    {
        return -1;
    }

    return (mbox[6] & 0x3);
}

B
bigamgic 已提交
246
int bcm271x_mbox_clock_set_state(int id, int state)
B
bigamgic 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
{
    mbox[0] = 8*4;                      // length of the message
    mbox[1] = MBOX_REQUEST;             // this is a request message
    
    mbox[2] = MBOX_TAG_CLOCK_SET_STATE;
    mbox[3] = 8;                        // buffer size
    mbox[4] = 8;                        // len

    mbox[5] = id;                       // id
    mbox[6] = state & 0x3;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != id)
    {
        return -1;
    }

    return (mbox[6] & 0x3);
}

B
bigamgic 已提交
269
int bcm271x_mbox_clock_get_rate(int id)
B
bigamgic 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
{
    mbox[0] = 8*4;                      // length of the message
    mbox[1] = MBOX_REQUEST;             // this is a request message
    
    mbox[2] = MBOX_TAG_CLOCK_GET_RATE; 
    mbox[3] = 8;                        // buffer size
    mbox[4] = 4;                        // len

    mbox[5] = id;                       // id
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != id)
    {
        return -1;
    }

    return mbox[6];
}

B
bigamgic 已提交
292
int bcm271x_mbox_clock_set_rate(int id, int rate)
B
bigamgic 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
{
    mbox[0] = 8*4;                      // length of the message
    mbox[1] = MBOX_REQUEST;             // this is a request message
    
    mbox[2] = MBOX_TAG_CLOCK_SET_RATE;
    mbox[3] = 8;                        // buffer size
    mbox[4] = 8;                        // len

    mbox[5] = id;                       // id
    mbox[6] = rate;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != id)
    {
        return -1;
    }

    return mbox[6];
}

B
bigamgic 已提交
315
int bcm271x_mbox_clock_get_max_rate(int id)
B
bigamgic 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
{
    mbox[0] = 8*4;                          // length of the message
    mbox[1] = MBOX_REQUEST;                 // this is a request message
    
    mbox[2] = MBOX_TAG_CLOCK_GET_MAX_RATE;
    mbox[3] = 8;                            // buffer size
    mbox[4] = 4;                            // len

    mbox[5] = id;                           // id
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != id)
    {
        return -1;
    }

    return mbox[6];
}

B
bigamgic 已提交
338
int bcm271x_mbox_clock_get_min_rate(int id)
B
bigamgic 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
{
    mbox[0] = 8*4;                          // length of the message
    mbox[1] = MBOX_REQUEST;                 // this is a request message
    
    mbox[2] = MBOX_TAG_CLOCK_GET_MIN_RATE; 
    mbox[3] = 8;                            // buffer size
    mbox[4] = 4;                            // len

    mbox[5] = id;                           // id
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != id)
    {
        return -1;
    }

    return mbox[6];
}

B
bigamgic 已提交
361
int bcm271x_mbox_power_get_state(int id)
B
bigamgic 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
{
    mbox[0] = 8*4;                  // length of the message
    mbox[1] = MBOX_REQUEST;         // this is a request message
    
    mbox[2] = MBOX_TAG_POWER_GET_STATE;
    mbox[3] = 8;                    // buffer size
    mbox[4] = 4;                    // len

    mbox[5] = id;                   // id
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != id)
    {
        return -1;
    }

    return (mbox[6] & 0x3);
}

B
bigamgic 已提交
384
int bcm271x_mbox_power_set_state(int id, int state)
B
bigamgic 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
{
    mbox[0] = 8*4;                  // length of the message
    mbox[1] = MBOX_REQUEST;         // this is a request message
    
    mbox[2] = MBOX_TAG_POWER_SET_STATE;
    mbox[3] = 8;                    // buffer size
    mbox[4] = 8;                    // len

    mbox[5] = id;                    // id
    mbox[6] = state & 0x3;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != id)
    {
        return -1;
    }

    return (mbox[6] & 0x3);
}

B
bigamgic 已提交
407
int bcm271x_mbox_temp_get(void)
B
bigamgic 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
{
    mbox[0] = 8*4;                  // length of the message
    mbox[1] = MBOX_REQUEST;         // this is a request message
    
    mbox[2] = MBOX_TAG_TEMP_GET;
    mbox[3] = 8;                    // buffer size
    mbox[4] = 4;                    // len

    mbox[5] = 0;                    //id
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != 0)
    {
        return -1;
    }

    return mbox[6];
}

B
bigamgic 已提交
430
int bcm271x_mbox_temp_get_max(void)
B
bigamgic 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
{
    mbox[0] = 8*4;                  // length of the message
    mbox[1] = MBOX_REQUEST;         // this is a request message
    
    mbox[2] = MBOX_TAG_TEMP_GET_MAX;
    mbox[3] = 8;                    // buffer size
    mbox[4] = 4;                    // len

    mbox[5] = 0;                    // id
    mbox[6] = 0;

    mbox[7] = MBOX_TAG_LAST;
    mbox_call(8, MMU_DISABLE);

    if(mbox[5] != 0)
    {
        return -1;
    }

    return mbox[6];
}