blob: 01a8ed2a7b1f84f275303bc182be59200abe85de [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass558ec672017-04-05 16:23:36 -06002/*
3 * (C) Copyright 2012 Stephen Warren
Simon Glass558ec672017-04-05 16:23:36 -06004 */
5
6#include <common.h>
7#include <memalign.h>
Simon Glass31efc382017-04-05 16:23:40 -06008#include <phys2bus.h>
Simon Glass558ec672017-04-05 16:23:36 -06009#include <asm/arch/mbox.h>
Nicolas Saenz Julienne2ae573d2020-06-29 18:37:22 +020010#include <linux/delay.h>
Simon Glass558ec672017-04-05 16:23:36 -060011
12struct msg_set_power_state {
13 struct bcm2835_mbox_hdr hdr;
14 struct bcm2835_mbox_tag_set_power_state set_power_state;
15 u32 end_tag;
16};
17
Simon Glass13ddef82017-04-05 16:23:37 -060018struct msg_get_clock_rate {
19 struct bcm2835_mbox_hdr hdr;
20 struct bcm2835_mbox_tag_get_clock_rate get_clock_rate;
21 u32 end_tag;
22};
23
Simon Glass31efc382017-04-05 16:23:40 -060024struct msg_query {
25 struct bcm2835_mbox_hdr hdr;
26 struct bcm2835_mbox_tag_physical_w_h physical_w_h;
27 u32 end_tag;
28};
29
Simon Glass929df012017-04-05 16:23:41 -060030struct msg_setup {
31 struct bcm2835_mbox_hdr hdr;
32 struct bcm2835_mbox_tag_physical_w_h physical_w_h;
33 struct bcm2835_mbox_tag_virtual_w_h virtual_w_h;
34 struct bcm2835_mbox_tag_depth depth;
35 struct bcm2835_mbox_tag_pixel_order pixel_order;
36 struct bcm2835_mbox_tag_alpha_mode alpha_mode;
37 struct bcm2835_mbox_tag_virtual_offset virtual_offset;
38 struct bcm2835_mbox_tag_overscan overscan;
39 struct bcm2835_mbox_tag_allocate_buffer allocate_buffer;
40 struct bcm2835_mbox_tag_pitch pitch;
41 u32 end_tag;
42};
43
Nicolas Saenz Julienne2ae573d2020-06-29 18:37:22 +020044struct msg_notify_vl805_reset {
45 struct bcm2835_mbox_hdr hdr;
46 struct bcm2835_mbox_tag_pci_dev_addr dev_addr;
47 u32 end_tag;
48};
49
Simon Glass558ec672017-04-05 16:23:36 -060050int bcm2835_power_on_module(u32 module)
51{
52 ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1);
53 int ret;
54
55 BCM2835_MBOX_INIT_HDR(msg_pwr);
56 BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state,
57 SET_POWER_STATE);
58 msg_pwr->set_power_state.body.req.device_id = module;
59 msg_pwr->set_power_state.body.req.state =
60 BCM2835_MBOX_SET_POWER_STATE_REQ_ON |
61 BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT;
62
63 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
64 &msg_pwr->hdr);
65 if (ret) {
66 printf("bcm2835: Could not set module %u power state\n",
67 module);
68 return -EIO;
69 }
70
71 return 0;
72}
Simon Glass13ddef82017-04-05 16:23:37 -060073
Jonathan Grayf98c4852018-03-17 16:15:48 +110074int bcm2835_get_mmc_clock(u32 clock_id)
Simon Glass13ddef82017-04-05 16:23:37 -060075{
76 ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
77 int ret;
78
79 ret = bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI);
80 if (ret)
81 return ret;
82
83 BCM2835_MBOX_INIT_HDR(msg_clk);
84 BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE);
Jonathan Grayf98c4852018-03-17 16:15:48 +110085 msg_clk->get_clock_rate.body.req.clock_id = clock_id;
Simon Glass13ddef82017-04-05 16:23:37 -060086
87 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr);
88 if (ret) {
89 printf("bcm2835: Could not query eMMC clock rate\n");
90 return -EIO;
91 }
92
93 return msg_clk->get_clock_rate.body.resp.rate_hz;
94}
Simon Glass31efc382017-04-05 16:23:40 -060095
96int bcm2835_get_video_size(int *widthp, int *heightp)
97{
98 ALLOC_CACHE_ALIGN_BUFFER(struct msg_query, msg_query, 1);
99 int ret;
100
101 BCM2835_MBOX_INIT_HDR(msg_query);
102 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg_query->physical_w_h,
103 GET_PHYSICAL_W_H);
104 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_query->hdr);
105 if (ret) {
106 printf("bcm2835: Could not query display resolution\n");
107 return ret;
108 }
109 *widthp = msg_query->physical_w_h.body.resp.width;
110 *heightp = msg_query->physical_w_h.body.resp.height;
111
112 return 0;
113}
Simon Glass929df012017-04-05 16:23:41 -0600114
115int bcm2835_set_video_params(int *widthp, int *heightp, int depth_bpp,
116 int pixel_order, int alpha_mode, ulong *fb_basep,
117 ulong *fb_sizep, int *pitchp)
118{
119 ALLOC_CACHE_ALIGN_BUFFER(struct msg_setup, msg_setup, 1);
120 int ret;
121
122 BCM2835_MBOX_INIT_HDR(msg_setup);
123 BCM2835_MBOX_INIT_TAG(&msg_setup->physical_w_h, SET_PHYSICAL_W_H);
124 msg_setup->physical_w_h.body.req.width = *widthp;
125 msg_setup->physical_w_h.body.req.height = *heightp;
126 BCM2835_MBOX_INIT_TAG(&msg_setup->virtual_w_h, SET_VIRTUAL_W_H);
127 msg_setup->virtual_w_h.body.req.width = *widthp;
128 msg_setup->virtual_w_h.body.req.height = *heightp;
129 BCM2835_MBOX_INIT_TAG(&msg_setup->depth, SET_DEPTH);
130 msg_setup->depth.body.req.bpp = 32;
131 BCM2835_MBOX_INIT_TAG(&msg_setup->pixel_order, SET_PIXEL_ORDER);
132 msg_setup->pixel_order.body.req.order = pixel_order;
133 BCM2835_MBOX_INIT_TAG(&msg_setup->alpha_mode, SET_ALPHA_MODE);
134 msg_setup->alpha_mode.body.req.alpha = alpha_mode;
135 BCM2835_MBOX_INIT_TAG(&msg_setup->virtual_offset, SET_VIRTUAL_OFFSET);
136 msg_setup->virtual_offset.body.req.x = 0;
137 msg_setup->virtual_offset.body.req.y = 0;
138 BCM2835_MBOX_INIT_TAG(&msg_setup->overscan, SET_OVERSCAN);
139 msg_setup->overscan.body.req.top = 0;
140 msg_setup->overscan.body.req.bottom = 0;
141 msg_setup->overscan.body.req.left = 0;
142 msg_setup->overscan.body.req.right = 0;
143 BCM2835_MBOX_INIT_TAG(&msg_setup->allocate_buffer, ALLOCATE_BUFFER);
144 msg_setup->allocate_buffer.body.req.alignment = 0x100;
145 BCM2835_MBOX_INIT_TAG_NO_REQ(&msg_setup->pitch, GET_PITCH);
146
147 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_setup->hdr);
148 if (ret) {
149 printf("bcm2835: Could not configure display\n");
150 return ret;
151 }
152 *widthp = msg_setup->physical_w_h.body.resp.width;
153 *heightp = msg_setup->physical_w_h.body.resp.height;
154 *pitchp = msg_setup->pitch.body.resp.pitch;
155 *fb_basep = bus_to_phys(
156 msg_setup->allocate_buffer.body.resp.fb_address);
157 *fb_sizep = msg_setup->allocate_buffer.body.resp.fb_size;
158
159 return 0;
160}
Nicolas Saenz Julienne2ae573d2020-06-29 18:37:22 +0200161
162/*
163 * On the Raspberry Pi 4, after a PCI reset, VL805's (the xHCI chip) firmware
164 * may either be loaded directly from an EEPROM or, if not present, by the
165 * SoC's VideoCore. This informs VideoCore that VL805 needs its firmware
166 * loaded.
167 */
168int bcm2711_notify_vl805_reset(void)
169{
170 ALLOC_CACHE_ALIGN_BUFFER(struct msg_notify_vl805_reset,
171 msg_notify_vl805_reset, 1);
172 int ret;
Marek Szyprowski34d298c2021-09-17 10:19:43 +0200173 static int done = false;
174
175 if (done)
176 return 0;
177
178 done = true;
Nicolas Saenz Julienne2ae573d2020-06-29 18:37:22 +0200179
180 BCM2835_MBOX_INIT_HDR(msg_notify_vl805_reset);
181 BCM2835_MBOX_INIT_TAG(&msg_notify_vl805_reset->dev_addr,
182 NOTIFY_XHCI_RESET);
183
184 /*
185 * The pci device address is expected like this:
186 *
187 * PCI_BUS << 20 | PCI_SLOT << 15 | PCI_FUNC << 12
188 *
189 * But since RPi4's PCIe setup is hardwired, we know the address in
190 * advance.
191 */
192 msg_notify_vl805_reset->dev_addr.body.req.dev_addr = 0x100000;
193
194 ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
195 &msg_notify_vl805_reset->hdr);
196 if (ret) {
197 printf("bcm2711: Faild to load vl805's firmware, %d\n", ret);
198 return -EIO;
199 }
200
201 udelay(200);
202
203 return 0;
204}