Simon Glass | 70997d8 | 2017-04-05 16:23:36 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2012 Stephen Warren |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <memalign.h> |
Simon Glass | 2e4170b | 2017-04-05 16:23:40 -0600 | [diff] [blame] | 9 | #include <phys2bus.h> |
Simon Glass | 70997d8 | 2017-04-05 16:23:36 -0600 | [diff] [blame] | 10 | #include <asm/arch/mbox.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 | |
Simon Glass | c660651 | 2017-04-05 16:23:37 -0600 | [diff] [blame] | 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 | |
Simon Glass | 2e4170b | 2017-04-05 16:23:40 -0600 | [diff] [blame] | 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 | |
Simon Glass | 7cac2fc | 2017-04-05 16:23:41 -0600 | [diff] [blame] | 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 | |
Simon Glass | 70997d8 | 2017-04-05 16:23:36 -0600 | [diff] [blame] | 44 | int bcm2835_power_on_module(u32 module) |
| 45 | { |
| 46 | ALLOC_CACHE_ALIGN_BUFFER(struct msg_set_power_state, msg_pwr, 1); |
| 47 | int ret; |
| 48 | |
| 49 | BCM2835_MBOX_INIT_HDR(msg_pwr); |
| 50 | BCM2835_MBOX_INIT_TAG(&msg_pwr->set_power_state, |
| 51 | SET_POWER_STATE); |
| 52 | msg_pwr->set_power_state.body.req.device_id = module; |
| 53 | msg_pwr->set_power_state.body.req.state = |
| 54 | BCM2835_MBOX_SET_POWER_STATE_REQ_ON | |
| 55 | BCM2835_MBOX_SET_POWER_STATE_REQ_WAIT; |
| 56 | |
| 57 | ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, |
| 58 | &msg_pwr->hdr); |
| 59 | if (ret) { |
| 60 | printf("bcm2835: Could not set module %u power state\n", |
| 61 | module); |
| 62 | return -EIO; |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
Simon Glass | c660651 | 2017-04-05 16:23:37 -0600 | [diff] [blame] | 67 | |
| 68 | int bcm2835_get_mmc_clock(void) |
| 69 | { |
| 70 | ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1); |
| 71 | int ret; |
| 72 | |
| 73 | ret = bcm2835_power_on_module(BCM2835_MBOX_POWER_DEVID_SDHCI); |
| 74 | if (ret) |
| 75 | return ret; |
| 76 | |
| 77 | BCM2835_MBOX_INIT_HDR(msg_clk); |
| 78 | BCM2835_MBOX_INIT_TAG(&msg_clk->get_clock_rate, GET_CLOCK_RATE); |
| 79 | msg_clk->get_clock_rate.body.req.clock_id = BCM2835_MBOX_CLOCK_ID_EMMC; |
| 80 | |
| 81 | ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_clk->hdr); |
| 82 | if (ret) { |
| 83 | printf("bcm2835: Could not query eMMC clock rate\n"); |
| 84 | return -EIO; |
| 85 | } |
| 86 | |
| 87 | return msg_clk->get_clock_rate.body.resp.rate_hz; |
| 88 | } |
Simon Glass | 2e4170b | 2017-04-05 16:23:40 -0600 | [diff] [blame] | 89 | |
| 90 | int bcm2835_get_video_size(int *widthp, int *heightp) |
| 91 | { |
| 92 | ALLOC_CACHE_ALIGN_BUFFER(struct msg_query, msg_query, 1); |
| 93 | int ret; |
| 94 | |
| 95 | BCM2835_MBOX_INIT_HDR(msg_query); |
| 96 | BCM2835_MBOX_INIT_TAG_NO_REQ(&msg_query->physical_w_h, |
| 97 | GET_PHYSICAL_W_H); |
| 98 | ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_query->hdr); |
| 99 | if (ret) { |
| 100 | printf("bcm2835: Could not query display resolution\n"); |
| 101 | return ret; |
| 102 | } |
| 103 | *widthp = msg_query->physical_w_h.body.resp.width; |
| 104 | *heightp = msg_query->physical_w_h.body.resp.height; |
| 105 | |
| 106 | return 0; |
| 107 | } |
Simon Glass | 7cac2fc | 2017-04-05 16:23:41 -0600 | [diff] [blame] | 108 | |
| 109 | int bcm2835_set_video_params(int *widthp, int *heightp, int depth_bpp, |
| 110 | int pixel_order, int alpha_mode, ulong *fb_basep, |
| 111 | ulong *fb_sizep, int *pitchp) |
| 112 | { |
| 113 | ALLOC_CACHE_ALIGN_BUFFER(struct msg_setup, msg_setup, 1); |
| 114 | int ret; |
| 115 | |
| 116 | BCM2835_MBOX_INIT_HDR(msg_setup); |
| 117 | BCM2835_MBOX_INIT_TAG(&msg_setup->physical_w_h, SET_PHYSICAL_W_H); |
| 118 | msg_setup->physical_w_h.body.req.width = *widthp; |
| 119 | msg_setup->physical_w_h.body.req.height = *heightp; |
| 120 | BCM2835_MBOX_INIT_TAG(&msg_setup->virtual_w_h, SET_VIRTUAL_W_H); |
| 121 | msg_setup->virtual_w_h.body.req.width = *widthp; |
| 122 | msg_setup->virtual_w_h.body.req.height = *heightp; |
| 123 | BCM2835_MBOX_INIT_TAG(&msg_setup->depth, SET_DEPTH); |
| 124 | msg_setup->depth.body.req.bpp = 32; |
| 125 | BCM2835_MBOX_INIT_TAG(&msg_setup->pixel_order, SET_PIXEL_ORDER); |
| 126 | msg_setup->pixel_order.body.req.order = pixel_order; |
| 127 | BCM2835_MBOX_INIT_TAG(&msg_setup->alpha_mode, SET_ALPHA_MODE); |
| 128 | msg_setup->alpha_mode.body.req.alpha = alpha_mode; |
| 129 | BCM2835_MBOX_INIT_TAG(&msg_setup->virtual_offset, SET_VIRTUAL_OFFSET); |
| 130 | msg_setup->virtual_offset.body.req.x = 0; |
| 131 | msg_setup->virtual_offset.body.req.y = 0; |
| 132 | BCM2835_MBOX_INIT_TAG(&msg_setup->overscan, SET_OVERSCAN); |
| 133 | msg_setup->overscan.body.req.top = 0; |
| 134 | msg_setup->overscan.body.req.bottom = 0; |
| 135 | msg_setup->overscan.body.req.left = 0; |
| 136 | msg_setup->overscan.body.req.right = 0; |
| 137 | BCM2835_MBOX_INIT_TAG(&msg_setup->allocate_buffer, ALLOCATE_BUFFER); |
| 138 | msg_setup->allocate_buffer.body.req.alignment = 0x100; |
| 139 | BCM2835_MBOX_INIT_TAG_NO_REQ(&msg_setup->pitch, GET_PITCH); |
| 140 | |
| 141 | ret = bcm2835_mbox_call_prop(BCM2835_MBOX_PROP_CHAN, &msg_setup->hdr); |
| 142 | if (ret) { |
| 143 | printf("bcm2835: Could not configure display\n"); |
| 144 | return ret; |
| 145 | } |
| 146 | *widthp = msg_setup->physical_w_h.body.resp.width; |
| 147 | *heightp = msg_setup->physical_w_h.body.resp.height; |
| 148 | *pitchp = msg_setup->pitch.body.resp.pitch; |
| 149 | *fb_basep = bus_to_phys( |
| 150 | msg_setup->allocate_buffer.body.resp.fb_address); |
| 151 | *fb_sizep = msg_setup->allocate_buffer.body.resp.fb_size; |
| 152 | |
| 153 | return 0; |
| 154 | } |