1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2012 Stephen Warren
4  */
5 
6 #include <common.h>
7 #include <memalign.h>
8 #include <phys2bus.h>
9 #include <asm/arch/mbox.h>
10 #include <linux/delay.h>
11 
12 struct 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 
18 struct 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 
24 struct 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 
30 struct 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 
44 struct 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 
bcm2835_power_on_module(u32 module)50 int 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 }
73 
bcm2835_get_mmc_clock(u32 clock_id)74 int bcm2835_get_mmc_clock(u32 clock_id)
75 {
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);
85 	msg_clk->get_clock_rate.body.req.clock_id = clock_id;
86 
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 }
95 
bcm2835_get_video_size(int * widthp,int * heightp)96 int 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 }
114 
bcm2835_set_video_params(int * widthp,int * heightp,int depth_bpp,int pixel_order,int alpha_mode,ulong * fb_basep,ulong * fb_sizep,int * pitchp)115 int 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 }
161 
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  */
bcm2711_notify_vl805_reset(void)168 int 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;
173 
174 	BCM2835_MBOX_INIT_HDR(msg_notify_vl805_reset);
175 	BCM2835_MBOX_INIT_TAG(&msg_notify_vl805_reset->dev_addr,
176 			      NOTIFY_XHCI_RESET);
177 
178 	/*
179 	 * The pci device address is expected like this:
180 	 *
181 	 *   PCI_BUS << 20 | PCI_SLOT << 15 | PCI_FUNC << 12
182 	 *
183 	 * But since RPi4's PCIe setup is hardwired, we know the address in
184 	 * advance.
185 	 */
186 	msg_notify_vl805_reset->dev_addr.body.req.dev_addr = 0x100000;
187 
188 	ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN,
189 				     &msg_notify_vl805_reset->hdr);
190 	if (ret) {
191 		printf("bcm2711: Faild to load vl805's firmware, %d\n", ret);
192 		return -EIO;
193 	}
194 
195 	udelay(200);
196 
197 	return 0;
198 }
199 
200