source: mainline/kernel/genarch/src/drivers/bcm2835/mbox.c

Last change on this file was 1b7b7af, checked in by Maurizio Lombardi <mlombard@…>, 4 years ago

raspberrypi: map the mbox address to memory

We should memory-map the mbox to memory
before trying to access it.
bcm2835_prop_get_memory() is an exception
because it's called early during the boot process
and we can't use km_map().

Signed-off-by: Maurizio Lombardi <mlombard@…>

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/*
2 * Copyright (c) 2013 Beniamino Galvani
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28/** @addtogroup kernel_genarch
29 * @{
30 */
31/**
32 * @file
33 * @brief BCM2835 mailbox communication routines
34 */
35
36#include <mm/km.h>
37#include <typedefs.h>
38#include <genarch/drivers/bcm2835/mbox.h>
39
40static void mbox_write(bcm2835_mbox_t *mbox, uint8_t chan, uint32_t value)
41{
42 while (mbox->status & MBOX_STATUS_FULL)
43 ;
44 mbox->write = MBOX_COMPOSE(chan, value);
45}
46
47static uint32_t mbox_read(bcm2835_mbox_t *mbox, uint8_t chan)
48{
49 uint32_t msg;
50
51 do {
52 while (mbox->status & MBOX_STATUS_EMPTY)
53 ;
54 msg = mbox->read;
55 } while (MBOX_MSG_CHAN(msg) != chan);
56
57 return MBOX_MSG_VALUE(msg);
58}
59
60bool bcm2835_prop_get_memory(uint32_t *base, uint32_t *size)
61{
62 bool ret;
63 MBOX_BUFF_ALLOC(req, mbox_getmem_buf_t);
64
65 req->buf_hdr.size = sizeof(mbox_getmem_buf_t);
66 req->buf_hdr.code = MBOX_PROP_CODE_REQ;
67 req->tag_hdr.tag_id = TAG_GET_ARM_MEMORY;
68 req->tag_hdr.buf_size = sizeof(mbox_tag_getmem_resp_t);
69 req->tag_hdr.val_len = 0;
70 req->zero = 0;
71
72 mbox_write((bcm2835_mbox_t *)BCM2835_MBOX0_ADDR,
73 MBOX_CHAN_PROP_A2V, KA2VCA((uint32_t)req));
74 mbox_read((bcm2835_mbox_t *)BCM2835_MBOX0_ADDR,
75 MBOX_CHAN_PROP_A2V);
76
77 if (req->buf_hdr.code == MBOX_PROP_CODE_RESP_OK) {
78 *base = req->data.base;
79 *size = req->data.size;
80 ret = true;
81 } else {
82 ret = false;
83 }
84
85 return ret;
86}
87
88bool bcm2835_fb_init(fb_properties_t *prop, uint32_t width, uint32_t height)
89{
90 bcm2835_mbox_t *fb_mbox;
91 bool ret = false;
92 MBOX_BUFF_ALLOC(fb_desc, bcm2835_fb_desc_t);
93
94 fb_mbox = (void *) km_map(BCM2835_MBOX0_ADDR, sizeof(bcm2835_mbox_t),
95 KM_NATURAL_ALIGNMENT, PAGE_NOT_CACHEABLE);
96
97 fb_desc->width = width;
98 fb_desc->height = height;
99 fb_desc->virt_width = fb_desc->width;
100 fb_desc->virt_height = fb_desc->height;
101 fb_desc->pitch = 0; /* Set by VC */
102 fb_desc->bpp = 16;
103 fb_desc->x_offset = 0;
104 fb_desc->y_offset = 0;
105 fb_desc->addr = 0; /* Set by VC */
106 fb_desc->size = 0; /* Set by VC */
107
108 mbox_write(fb_mbox, MBOX_CHAN_FB, KA2VCA(fb_desc));
109
110 if (mbox_read(fb_mbox, MBOX_CHAN_FB)) {
111 printf("BCM2835 framebuffer initialization failed\n");
112 goto out;
113 }
114
115 prop->addr = fb_desc->addr;
116 prop->offset = 0;
117 prop->x = fb_desc->width;
118 prop->y = fb_desc->height;
119 prop->scan = fb_desc->pitch;
120 prop->visual = VISUAL_RGB_5_6_5_LE;
121
122 printf("BCM2835 framebuffer at 0x%08x (%dx%d)\n", prop->addr,
123 prop->x, prop->y);
124 ret = true;
125out:
126 km_unmap((uintptr_t)fb_mbox, sizeof(bcm2835_mbox_t));
127 return ret;
128}
129
130bool bcm2835_mbox_get_fb_size(uint32_t *w, uint32_t *h)
131{
132 bool r;
133 MBOX_BUFF_ALLOC(msg, mbox_getfbsize_buf_t);
134 bcm2835_mbox_t *mbox;
135
136 mbox = (void *) km_map(BCM2835_MBOX0_ADDR, sizeof(bcm2835_mbox_t),
137 KM_NATURAL_ALIGNMENT, PAGE_NOT_CACHEABLE);
138 assert(mbox);
139
140 msg->buf_hdr.size = sizeof(mbox_getfbsize_buf_t);
141 msg->buf_hdr.code = MBOX_PROP_CODE_REQ;
142 msg->tag_hdr.tag_id = MBOX_TAG_GET_PHYS_W_H;
143 msg->tag_hdr.buf_size = sizeof(msg->body);
144 msg->tag_hdr.val_len = 0;
145 msg->zero = 0;
146
147 mbox_write(mbox,
148 MBOX_CHAN_PROP_A2V, KA2VCA((uint32_t)msg));
149 mbox_read(mbox, MBOX_CHAN_PROP_A2V);
150
151 r = msg->buf_hdr.code == MBOX_PROP_CODE_RESP_OK;
152 if (r) {
153 *h = msg->body.height;
154 *w = msg->body.width;
155 }
156
157 km_unmap((uintptr_t) mbox, sizeof(bcm2835_mbox_t));
158 return r;
159}
160
161/**
162 * @}
163 */
Note: See TracBrowser for help on using the repository browser.